buffer-layers.el 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. ;;; buffer-layers.el --- Layered Buffers for Buffer Management
  2. ;; Copyright (C) 2016 Samuel Flint
  3. ;; Author: Samuel W. Flint <swflint@flintfam.org>
  4. ;; Version: 1.7
  5. ;; Package-Requires: ((cl-lib "0.5"))
  6. ;; Keywords: buffer-management
  7. ;; URL: http://github.com/swflint/buffer-layers
  8. ;;; Commentary:
  9. ;;
  10. ;; This is Buffer Layers, a simple, layer-based buffer management system.
  11. ;;
  12. ;; It works by defining buffer layers using a fairly simple macro. The following would be put in a file called `org.layer`
  13. ;;
  14. ;; ```elisp
  15. ;; ;; -*- emacs-lisp -*-
  16. ;; (define-buffer-layer org
  17. ;; :files ("~/org/"
  18. ;; "~/org/main.org")
  19. ;; :buffer-to-select "main.org"
  20. ;; :run-on-apply ((my/find-current-notes-file)))
  21. ;; ```
  22. ;;
  23. ;; It can be loaded with `(load-buffer-layer "/path/to/org.layer" nil)`. If the final `nil` is changed to `t`, it will load and apply the layer.
  24. ;;
  25. ;; Buffer Layer Definitions take the following arguments:
  26. ;;
  27. ;; - `:files`: A list. This is the list of files that are loaded when the buffer layer is applied.
  28. ;; - `:buffer-to-select`: The buffer to select after files are loaded, and the given forms to run on application are executed.
  29. ;; - `:run-on-apply`: This is a list of forms to be executed in between finding files and selecting the given buffer.
  30. ;; - `:run-on-remove`: This is a list of forms to be executed after killing the buffers that have been loaded.
  31. ;;
  32. ;; To manipulate buffer layers, execute `buffer-layer-mode`, and then you can use the following keybindings:
  33. ;;
  34. ;; - `C-x L l`: Load a buffer layer, if defined, otherwise, load from the given file.
  35. ;; - `C-x L u`: Unload a loaded buffer layer.
  36. ;; - `C-x L U`: Unload all loaded buffer layers.
  37. ;; - `C-x L L`: List defined buffer layers, noting if they've been applied.
  38. ;;
  39. ;; The following are the user-facing functions:
  40. ;;
  41. ;; - `define-buffer-layer`
  42. ;; - `buffer-layer-load-buffer-layer`, also known as `load-buffer-layer`
  43. ;; - `buffer-layers-unload-buffer-layer`
  44. ;; - `buffer-layers-list`
  45. ;; - `buffer-layers-unload-all-buffer-layers`
  46. ;; - `buffer-layers-mode`
  47. ;;
  48. ;; On enabling `buffer-layer-mode`, the map is placed onto `C-x L`, and `buffer-layers-unload-all-buffer-layers` is added to the `kill-emacs-hook`, and on disabling the mode, they are removed.
  49. ;;; Code:
  50. (require 'cl-lib)
  51. (cl-defstruct buffer-layer
  52. name
  53. files
  54. select
  55. on-apply
  56. on-apply-source
  57. on-remove
  58. on-remove-source)
  59. (defvar *buffer-layers* nil
  60. "List of all defined buffer layers.")
  61. (defvar *buffer-layers-applied* nil
  62. "List of applied buffer-layers.")
  63. (defvar *buffer-layer-definitions* nil
  64. "List of all buffer layer definitions.")
  65. (defvar *buffer-layer-buffers* nil
  66. "List of buffers in loaded buffer layers.")
  67. (defvar *buffer-layer-file* "~/.emacs.d/buffer-layer-definitions.el"
  68. "The file to store buffer layer definitions in.")
  69. (defun buffer-layers-applied-p (layer)
  70. "Returns true if LAYER is applied."
  71. (member layer *buffer-layers-applied*))
  72. (defun buffer-layer--get-buffer-layer-definition (layer-name)
  73. (first (cl-remove-if-not (lambda (layer)
  74. (eq layer-name (buffer-layer-name layer))) *buffer-layer-definitions*)))
  75. (cl-defmacro define-buffer-layer (name &key files select on-apply on-remove)
  76. "Define a buffer layer named NAME, taking FILES, RUN-ON-APPLY, RUN-ON-REMOVE and BUFFER-TO-SELECT as keyword arguments."
  77. `(progn
  78. (cl-pushnew ',name *buffer-layers*)
  79. (cl-pushnew (make-buffer-layer :name ',name
  80. :files ',files
  81. :select ,select
  82. :on-apply-source ',on-apply
  83. :on-remove-source ',on-remove
  84. :on-apply (lambda () ,@on-apply)
  85. :on-remove (lambda () ,@on-remove))
  86. *buffer-layer-definitions*
  87. :key #'buffer-layer-name)
  88. ',name))
  89. (defun buffer-layers-load-layer (name)
  90. (interactive (list (completing-read "Layer Name: " *buffer-layers* ;; (cl-remove-if #'(lambda (layer) (member layer *buffer-layers-applied*)) *buffer-layers*)
  91. nil t)))
  92. (let* ((record (rest (assoc name *buffer-layer-definitions*)))
  93. (files (rest (assoc :files record)))
  94. (on-apply (rest (assoc :on-apply-lambda record)))
  95. (buffer-select (rest (assoc :select-buffer record))))
  96. (let ((buffer-list nil))
  97. (dolist (file files)
  98. (cl-pushnew (find-file file) buffer-list))
  99. (add-to-list '*buffer-layer-buffers* `(,name ,@buffer-list)))
  100. (when (functionp on-apply)
  101. (funcall on-apply))
  102. (when buffer-select
  103. (switch-to-buffer buffer-select))
  104. (message "Applied Buffer Layer %s." name)))
  105. (defun buffer-layers-load-buffer-layer (name-or-path load-it-p)
  106. "Load a buffer named NAME-OR-PATH, and if a file, apply if LOAD-IT-P is true."
  107. (interactive (list (completing-read "Buffer Layer Name or Path: " (cl-remove-if #'(lambda (layer)
  108. (member layer *buffer-layers-applied*))
  109. *buffer-layers*))
  110. nil))
  111. (if (functionp (buffer-layers--applier-name name-or-path))
  112. (funcall (buffer-layers--applier-name name-or-path))
  113. (load name-or-path)
  114. (when load-it-p
  115. (funcall current-buffer-applier))))
  116. (defalias 'load-buffer-layer 'buffer-layers-load-buffer-layer)
  117. (defun buffer-layers-unload-buffer-layer (name)
  118. "Unload Buffer Layer named NAME."
  119. (interactive (list (completing-read "Buffer Layer Name: " *buffer-layers-applied*)))
  120. (funcall (buffer-layers--remover-name name)))
  121. (defun buffer-layers-list ()
  122. "Produce a list of defined buffer layers."
  123. (interactive)
  124. (when (buffer-live-p "*Buffer Layers*")
  125. (kill-buffer "*Buffer Layers*"))
  126. (with-help-window "*Buffer Layers*"
  127. (with-current-buffer "*Buffer Layers*"
  128. (insert "Defined Buffer Layers:\n\n")
  129. (dolist (layer *buffer-layers*)
  130. (if (not (buffer-layers-applied-p layer))
  131. (insert (format " - %s\n" layer))
  132. (insert (format " - %s (Applied)\n" layer)))
  133. (dolist (buffer (symbol-value (buffer-layers--buffer-list-name layer)))
  134. (if (null (get-buffer-window-list buffer nil t))
  135. (insert (format " - %s\n" (buffer-name buffer)))
  136. (insert (format " - %s (visible)\n" (buffer-name buffer)))))))))
  137. (defun buffer-layers-unload-all-buffer-layers ()
  138. "Unload all loaded buffer layers."
  139. (interactive)
  140. (dolist (buffer-layer *buffer-layers-applied*)
  141. (buffer-layers-unload-buffer-layer buffer-layer)))
  142. (defun buffer-layers-create-layer (name)
  143. "Create a new layer."
  144. (interactive "SNew Layer Name: "))
  145. (defun buffer-layers-add-file-to-layer (name file)
  146. "Add a file to the layer."
  147. (interactive (list
  148. (completing-read "Layer: " *buffer-layers* nil t)
  149. (read-file-name "File Name: "))))
  150. (defun buffer-layers-add-buffer-to-layer (name buffer)
  151. "Add a buffer to the given layer."
  152. (interactive (list
  153. (completing-read "Layer: " *buffer-layers* nil t)
  154. (read-buffer "Buffer: " (current-buffer)))))
  155. (defun buffer-layers-edit-load-actions (layer)
  156. "Edit the actions to be preformed on buffer layer load."
  157. (interactive (list (completing-read "Layer: " *buffer-layers* nil t))))
  158. (defun buffer-layers-edit-remove-actions (layer)
  159. "Edit the actions to be preformed on buffer layer removal."
  160. (interactive (list (completing-read "Layer: " *buffer-layers* nil t))))
  161. (defun buffer-layers-set-buffer-to-select (layer)
  162. "Set the buffer to automatically select."
  163. (interactive (list (completing-read "Layer: " *buffer-layers* nil t))))
  164. (defun buffer-layers-save ()
  165. "Save defined buffer layers."
  166. (interactive)
  167. (insert (format "%S\n\n" (let ((name (buffer-layer-name the-layer))
  168. (files (buffer-layer-files the-layer))
  169. (select (buffer-layer-select the-layer))
  170. (on-apply (buffer-layer-on-apply-source the-layer))
  171. (on-remove (buffer-layer-on-remove-source the-layer)))
  172. `(define-buffer-layer* ,name
  173. :files ,files
  174. :select ,select
  175. :on-apply ,on-apply
  176. :on-remove ,on-remove)))))
  177. (defvar buffer-layers-map (make-keymap)
  178. "Keymap for buffer-layer commands.")
  179. (define-key buffer-layers-map (kbd "l") #'buffer-layers-load-buffer-layer)
  180. (define-key buffer-layers-map (kbd "L") #'buffer-layers-list)
  181. (define-key buffer-layers-map (kbd "u") #'buffer-layers-unload-buffer-layer)
  182. (define-key buffer-layers-map (kbd "U") #'buffer-layers-unload-all-buffer-layers)
  183. (define-key buffer-layers-map (kbd "c") #'buffer-layers-create-layer)
  184. (define-key buffer-layers-map (kbd "f") #'buffer-layers-add-file-to-layer)
  185. (define-key buffer-layers-map (kbd "b") #'buffer-layers-add-buffer-to-layer)
  186. (define-key buffer-layers-map (kbd "a") #'buffer-layers-edit-load-actions)
  187. (define-key buffer-layers-map (kbd "r") #'buffer-layers-edit-remove-actions)
  188. (define-key buffer-layers-map (kbd "s") #'buffer-layers-set-buffer-to-select)
  189. (define-key buffer-layers-map (kbd "C-s") #'buffer-layers-save)
  190. (define-minor-mode buffer-layers-mode
  191. "A mode for managing layers of buffers."
  192. :lighter " BLM" :global t :variable buffer-layers-mode-p
  193. (if buffer-layers-mode-p
  194. (progn
  195. (define-key ctl-x-map (kbd "L") buffer-layers-map)
  196. (add-hook 'kill-emacs-hook #'buffer-layers-unload-all-buffer-layers))
  197. (progn
  198. (define-key ctl-x-map (kbd "L") nil)
  199. (remove-hook 'kill-emacs-hook #'buffer-layers-unload-all-buffer-layers))))
  200. (provide 'buffer-layers)
  201. ;;; buffer-layers.el ends here