buffer-sets.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. ;;; buffer-sets.el --- Sets of Buffers for Buffer Management
  2. ;; Copyright (C) 2016 Samuel Flint
  3. ;; Author: Samuel W. Flint <swflint@flintfam.org>
  4. ;; Version: 2.0
  5. ;; Package-Requires: ((cl-lib "0.5"))
  6. ;; Keywords: buffer-management
  7. ;; URL: http://github.com/swflint/buffer-sets
  8. ;;; Commentary:
  9. ;;
  10. ;;; Code:
  11. (require 'cl-lib)
  12. ;;; Variables and Structures
  13. (cl-defstruct buffer-set
  14. name
  15. files
  16. select
  17. on-apply
  18. on-apply-source
  19. on-remove
  20. on-remove-source)
  21. (defvar *buffer-sets* nil
  22. "List of all defined buffer sets.")
  23. (defvar *buffer-sets-applied* nil
  24. "List of applied buffer-sets.")
  25. (defvar *buffer-set-definitions* nil
  26. "List of all buffer set definitions.")
  27. (defvar *buffer-set-buffers* nil
  28. "List of buffers in loaded buffer sets.")
  29. (defvar buffer-sets-mode-p nil)
  30. (defvar buffer-sets-load-set-hook '()
  31. "Hook run on set load.")
  32. (defvar buffer-sets-unload-hook '()
  33. "Hook run on set unload.")
  34. (defcustom buffer-set-file "~/.emacs.d/buffer-set-definitions.el"
  35. "The file to store buffer set definitions in."
  36. :type 'file :group 'editing)
  37. ;;; Utility Functions
  38. (defun buffer-sets-applied-p (set)
  39. "Returns true if SET is applied."
  40. (member set *buffer-sets-applied*))
  41. (defun buffer-set--get-buffer-set-definition (set-name)
  42. (car (cl-remove-if-not (lambda (set)
  43. (eq set-name (buffer-set-name set))) *buffer-set-definitions*)))
  44. (defun buffer-set--generate-buffers-list (set-name)
  45. (intern (format "*buffer-set-%s--buffers*" set-name)))
  46. ;;; Set Definition
  47. ;;;###autoload
  48. (cl-defmacro define-buffer-set (name &key files select on-apply on-remove)
  49. "Define a buffer set named NAME, taking FILES, RUN-ON-APPLY, RUN-ON-REMOVE and BUFFER-TO-SELECT as keyword arguments."
  50. `(progn
  51. (cl-pushnew ',name *buffer-sets*)
  52. (cl-pushnew (make-buffer-set :name ',name
  53. :files ',files
  54. :select ,select
  55. :on-apply-source ',on-apply
  56. :on-remove-source ',on-remove
  57. :on-apply (lambda () ,@on-apply)
  58. :on-remove (lambda () ,@on-remove))
  59. *buffer-set-definitions*
  60. :key #'buffer-set-name)
  61. (defvar ,(buffer-set--generate-buffers-list name) nil)
  62. ',name))
  63. ;;; Interactive Functions
  64. ;;;###autoload
  65. (defun buffer-sets-load-set (name)
  66. (interactive (list (intern (completing-read "Set Name: "
  67. (cl-remove-if #'(lambda (set) (member set *buffer-sets-applied*)) *buffer-sets*)
  68. nil t))))
  69. (let ((set-definition (buffer-set--get-buffer-set-definition name)))
  70. (if (not (buffer-set-p set-definition))
  71. (error "Set Undefined: %s" name)
  72. (let ((files (buffer-set-files set-definition))
  73. (select (buffer-set-select set-definition))
  74. (on-apply (buffer-set-on-apply set-definition))
  75. (buffers-list (buffer-set--generate-buffers-list name)))
  76. (setf (symbol-value buffers-list) (mapcar #'find-file files))
  77. (funcall on-apply)
  78. (when (stringp select)
  79. (switch-to-buffer select))
  80. (add-to-list '*buffer-sets-applied* name)
  81. (run-hooks 'buffer-sets-load-set-hook)
  82. (message "Applied buffer set %s." name)))))
  83. (defalias 'load-buffer-set 'buffer-sets-load-set)
  84. ;;;###autoload
  85. (defun buffer-sets-unload-buffer-set (name)
  86. "Unload Buffer Set named NAME."
  87. (interactive (list (intern (completing-read "Set Name: " *buffer-sets-applied*))))
  88. (let ((set-definition (buffer-set--get-buffer-set-definition name)))
  89. (if (not (buffer-set-p set-definition))
  90. (error "Set Undefined: %s" name)
  91. (let ((buffers-list (buffer-set--generate-buffers-list name))
  92. (on-remove (buffer-set-on-remove set-definition)))
  93. (mapc (lambda (buffer)
  94. (when (buffer-live-p buffer)
  95. (with-current-buffer buffer
  96. (save-buffer)
  97. (kill-buffer buffer))))
  98. (symbol-value buffers-list))
  99. (funcall on-remove)
  100. (setf (symbol-value buffers-list) nil)
  101. (setq *buffer-sets-applied* (delq name *buffer-sets-applied*))
  102. (run-hooks 'buffer-sets-unload-hook)
  103. (message "Removed Buffer Set: %s" name)))))
  104. ;;;###autoload
  105. (defun buffer-sets-list ()
  106. "Produce a list of defined buffer sets."
  107. (interactive)
  108. (when (buffer-live-p "*Buffer Sets*")
  109. (kill-buffer "*Buffer Sets*"))
  110. (with-help-window "*Buffer Sets*"
  111. (with-current-buffer "*Buffer Sets*"
  112. (insert "Defined Buffer Sets:\n\n")
  113. (dolist (set *buffer-sets*)
  114. (if (not (buffer-sets-applied-p set))
  115. (insert (format " - %s\n" set))
  116. (insert (format " - %s (Applied)\n" set)))
  117. (dolist (buffer (symbol-value (buffer-set--generate-buffers-list set)))
  118. (if (null (get-buffer-window-list buffer nil t))
  119. (insert (format " - %s\n" (buffer-name buffer)))
  120. (insert (format " - %s (visible)\n" (buffer-name buffer)))))))))
  121. ;;;###autoload
  122. (defun buffer-sets-unload-all-buffer-sets ()
  123. "Unload all loaded buffer sets."
  124. (interactive)
  125. (dolist (buffer-set *buffer-sets-applied*)
  126. (buffer-sets-unload-buffer-set buffer-set)))
  127. ;;;###autoload
  128. (defun buffer-sets-create-set (name)
  129. "Create a new set."
  130. (interactive "SNew Set Name: ")
  131. (when (not (member name *buffer-sets*))
  132. (cl-pushnew name *buffer-sets*)
  133. (setf (symbol-value (buffer-set--generate-buffers-list name)) nil)
  134. (cl-pushnew (make-buffer-set :name name
  135. :on-apply (lambda () nil)
  136. :on-remove (lambda () nil)) *buffer-set-definitions*)))
  137. ;;;###autoload
  138. (defun buffer-sets-add-file-to-set (name file)
  139. "Add a file to the set."
  140. (interactive (list
  141. (intern (completing-read "Set Name: " *buffer-sets* nil t))
  142. (read-file-name "File Name: ")))
  143. (let ((set (buffer-set--get-buffer-set-definition name)))
  144. (setf (buffer-set-files set) (append (buffer-set-files set) (list file)))))
  145. ;;;###autoload
  146. (defun buffer-sets-add-directory-to-set (name directory)
  147. (interactive (list
  148. (intern (completing-read "Set Name: " *buffer-sets* nil t))
  149. (read-directory-name "Directory: ")))
  150. (let ((set (buffer-set--get-buffer-set-definition name)))
  151. (setf (buffer-set-files set) (append (buffer-set-files set) (list directory)))))
  152. ;;;###autoload
  153. (defun buffer-sets-add-buffer-to-set (name buffer)
  154. "Add a buffer to the given set."
  155. (interactive (list
  156. (intern (completing-read "Set Name: " *buffer-sets* nil t))
  157. (get-buffer (read-buffer "Buffer: " (current-buffer)))))
  158. (let ((set (buffer-set--get-buffer-set-definition name))
  159. (file (buffer-file-name buffer)))
  160. (setf (buffer-set-files set) (append (buffer-set-files set) (list file)))))
  161. ;; (defun buffer-sets-edit-load-actions (set)
  162. ;; "Edit the actions to be preformed on buffer set load."
  163. ;; (interactive (list (completing-read "Set: " *buffer-sets* nil t))))
  164. ;; (defun buffer-sets-edit-remove-actions (set)
  165. ;; "Edit the actions to be preformed on buffer set removal."
  166. ;; (interactive (list (completing-read "Set: " *buffer-sets* nil t))))
  167. ;;;###autoload
  168. (defun buffer-sets-set-buffer-to-select (set)
  169. "Set the buffer to automatically select."
  170. (interactive (list (intern (completing-read "Set Name: " *buffer-sets* nil t))))
  171. (let* ((set (buffer-set--get-buffer-set-definition set))
  172. (files (buffer-set-files set)))
  173. (setf (buffer-set-select set)
  174. (completing-read "File: " files nil t))))
  175. ;;;###autoload
  176. (defun buffer-sets-remove-file (set)
  177. (interactive (list (intern (completing-read "Set Name: " *buffer-sets* nil t))))
  178. (let ((set (buffer-set--get-buffer-set-definition set)))
  179. (setf (buffer-set-files set)
  180. (delq (completing-read "File: " (buffer-set-files set) nil t)
  181. (buffer-set-files set)))))
  182. ;;; File Functions
  183. (defun buffer-sets-save (the-set)
  184. "Save defined buffer sets."
  185. (insert (format "%S\n\n" (let ((name (buffer-set-name the-set))
  186. (files (buffer-set-files the-set))
  187. (select (buffer-set-select the-set))
  188. (on-apply (buffer-set-on-apply-source the-set))
  189. (on-remove (buffer-set-on-remove-source the-set)))
  190. `(define-buffer-set ,name
  191. :files ,files
  192. :select ,select
  193. :on-apply ,on-apply
  194. :on-remove ,on-remove)))))
  195. ;;;###autoload
  196. (defun buffer-sets-load-definitions-file ()
  197. "Load buffer set definitions file."
  198. (interactive)
  199. (load buffer-set-file t t)
  200. (message "Loaded Buffer Set Definitions."))
  201. ;;;###autoload
  202. (defun buffer-sets-save-definitions ()
  203. (interactive)
  204. (with-current-buffer (find-file buffer-set-file)
  205. (kill-region (buffer-end -1) (buffer-end 1))
  206. (mapc #'buffer-sets-save (reverse *buffer-set-definitions*))
  207. (save-buffer)
  208. (kill-buffer))
  209. (message "Saved Buffer Set Definitions."))
  210. ;;; Mode Definition
  211. (defvar buffer-sets-map
  212. (let ((keymap (make-keymap)))
  213. (mapc (lambda (pair)
  214. (cl-destructuring-bind (key . command) pair
  215. (define-key keymap (kbd key) command)))
  216. '(("l" . buffer-sets-load-set)
  217. ("L" . buffer-sets-list)
  218. ("u" . buffer-sets-unload-buffer-set)
  219. ("U" . buffer-sets-unload-all-buffer-sets)
  220. ("c" . buffer-sets-create-set)
  221. ("f" . buffer-sets-add-file-to-set)
  222. ("b" . buffer-sets-add-buffer-to-set)
  223. ("d" . buffer-sets-add-directory-to-set)
  224. ("R" . buffer-sets-remove-file)
  225. ("s" . buffer-sets-set-buffer-to-select)
  226. ("C-f" . buffer-sets-load-definitions-file)
  227. ("C-s" . buffer-sets-save-definitions)
  228. ;; ("a" . buffer-sets-edit-load-actions)
  229. ;; ("r" . buffer-sets-edit-remove-actions)
  230. ))
  231. keymap)
  232. "Keymap for buffer-set commands.")
  233. ;;;###autoload
  234. (define-minor-mode buffer-sets-mode
  235. "A mode for managing sets of buffers."
  236. :lighter " BSM" :global t :variable buffer-sets-mode-p
  237. (if buffer-sets-mode-p
  238. (progn
  239. (buffer-sets-load-definitions-file)
  240. (define-key ctl-x-map (kbd "L") buffer-sets-map)
  241. (add-hook 'kill-emacs-hook #'buffer-sets-unload-all-buffer-sets)
  242. (add-hook 'kill-emacs-hook #'buffer-sets-save-definitions))
  243. (progn
  244. (buffer-sets-save-definitions)
  245. (define-key ctl-x-map (kbd "L") nil)
  246. (remove-hook 'kill-emacs-hook #'buffer-sets-unload-all-buffer-sets)
  247. (remove-hook 'kill-emacs-hook #'buffer-sets-save-definitions))))
  248. (provide 'buffer-sets)
  249. ;;; buffer-sets.el ends here