ibuf-macs.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. ;;; ibuf-macs.el --- macros for ibuffer
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Colin Walters <walters@verbum.org>
  4. ;; Maintainer: John Paul Wallington <jpw@gnu.org>
  5. ;; Created: 6 Dec 2001
  6. ;; Keywords: buffer, convenience
  7. ;; Package: ibuffer
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;; Code:
  21. (eval-when-compile
  22. (require 'cl))
  23. ;; From Paul Graham's "ANSI Common Lisp", adapted for Emacs Lisp here.
  24. (defmacro ibuffer-aif (test true-body &rest false-body)
  25. "Evaluate TRUE-BODY or FALSE-BODY depending on value of TEST.
  26. If TEST returns non-nil, bind `it' to the value, and evaluate
  27. TRUE-BODY. Otherwise, evaluate forms in FALSE-BODY as if in `progn'.
  28. Compare with `if'."
  29. (declare (indent 2))
  30. (let ((sym (make-symbol "ibuffer-aif-sym")))
  31. `(let ((,sym ,test))
  32. (if ,sym
  33. (let ((it ,sym))
  34. ,true-body)
  35. (progn
  36. ,@false-body)))))
  37. (defmacro ibuffer-awhen (test &rest body)
  38. "Evaluate BODY if TEST returns non-nil.
  39. During evaluation of body, bind `it' to the value returned by TEST."
  40. (declare (indent 1))
  41. `(ibuffer-aif ,test
  42. (progn ,@body)
  43. nil))
  44. (defmacro ibuffer-save-marks (&rest body)
  45. "Save the marked status of the buffers and execute BODY; restore marks."
  46. (declare (indent 0))
  47. (let ((bufsym (make-symbol "bufsym")))
  48. `(let ((,bufsym (current-buffer))
  49. (ibuffer-save-marks-tmp-mark-list (ibuffer-current-state-list)))
  50. (unwind-protect
  51. (progn
  52. (save-excursion
  53. ,@body))
  54. (with-current-buffer ,bufsym
  55. (ibuffer-redisplay-engine
  56. ;; Get rid of dead buffers
  57. (delq nil
  58. (mapcar #'(lambda (e) (when (buffer-live-p (car e))
  59. e))
  60. ibuffer-save-marks-tmp-mark-list)))
  61. (ibuffer-redisplay t))))))
  62. ;;;###autoload
  63. (defmacro* define-ibuffer-column (symbol (&key name inline props summarizer
  64. header-mouse-map) &rest body)
  65. "Define a column SYMBOL for use with `ibuffer-formats'.
  66. BODY will be called with `buffer' bound to the buffer object, and
  67. `mark' bound to the current mark on the buffer. The original ibuffer
  68. buffer will be bound to `ibuffer-buf'.
  69. If NAME is given, it will be used as a title for the column.
  70. Otherwise, the title will default to a capitalized version of the
  71. SYMBOL's name. PROPS is a plist of additional properties to add to
  72. the text, such as `mouse-face'. And SUMMARIZER, if given, is a
  73. function which will be passed a list of all the strings in its column;
  74. it should return a string to display at the bottom.
  75. If HEADER-MOUSE-MAP is given, it will be used as a keymap for the
  76. title of the column.
  77. Note that this macro expands into a `defun' for a function named
  78. ibuffer-make-column-NAME. If INLINE is non-nil, then the form will be
  79. inlined into the compiled format versions. This means that if you
  80. change its definition, you should explicitly call
  81. `ibuffer-recompile-formats'.
  82. \(fn SYMBOL (&key NAME INLINE PROPS SUMMARIZER) &rest BODY)"
  83. (declare (indent defun))
  84. (let* ((sym (intern (concat "ibuffer-make-column-"
  85. (symbol-name symbol))))
  86. (bod-1 `(with-current-buffer buffer
  87. ,@body))
  88. (bod (if props
  89. `(propertize
  90. ,bod-1
  91. ,@props)
  92. bod-1)))
  93. `(progn
  94. ,(if inline
  95. `(push '(,sym ,bod) ibuffer-inline-columns)
  96. `(defun ,sym (buffer mark)
  97. ,bod))
  98. (put (quote ,sym) 'ibuffer-column-name
  99. ,(if (stringp name)
  100. name
  101. (capitalize (symbol-name symbol))))
  102. ,(if header-mouse-map `(put (quote ,sym) 'header-mouse-map ,header-mouse-map))
  103. ,(if summarizer
  104. ;; Store the name of the summarizing function.
  105. `(put (quote ,sym) 'ibuffer-column-summarizer
  106. (quote ,summarizer)))
  107. ,(if summarizer
  108. ;; This will store the actual values of the column
  109. ;; summary.
  110. `(put (quote ,sym) 'ibuffer-column-summary nil))
  111. :autoload-end)))
  112. ;;;###autoload
  113. (defmacro* define-ibuffer-sorter (name documentation
  114. (&key
  115. description)
  116. &rest body)
  117. "Define a method of sorting named NAME.
  118. DOCUMENTATION is the documentation of the function, which will be called
  119. `ibuffer-do-sort-by-NAME'.
  120. DESCRIPTION is a short string describing the sorting method.
  121. For sorting, the forms in BODY will be evaluated with `a' bound to one
  122. buffer object, and `b' bound to another. BODY should return a non-nil
  123. value if and only if `a' is \"less than\" `b'.
  124. \(fn NAME DOCUMENTATION (&key DESCRIPTION) &rest BODY)"
  125. (declare (indent 1))
  126. `(progn
  127. (defun ,(intern (concat "ibuffer-do-sort-by-" (symbol-name name))) ()
  128. ,(or documentation "No :documentation specified for this sorting method.")
  129. (interactive)
  130. (setq ibuffer-sorting-mode ',name)
  131. (when (eq ibuffer-sorting-mode ibuffer-last-sorting-mode)
  132. (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep)))
  133. (ibuffer-redisplay t)
  134. (setq ibuffer-last-sorting-mode ',name))
  135. (push (list ',name ,description
  136. #'(lambda (a b)
  137. ,@body))
  138. ibuffer-sorting-functions-alist)
  139. :autoload-end))
  140. ;;;###autoload
  141. (defmacro* define-ibuffer-op (op args
  142. documentation
  143. (&key
  144. interactive
  145. mark
  146. modifier-p
  147. dangerous
  148. (opstring "operated on")
  149. (active-opstring "Operate on")
  150. complex)
  151. &rest body)
  152. "Generate a function which operates on a buffer.
  153. OP becomes the name of the function; if it doesn't begin with
  154. `ibuffer-do-', then that is prepended to it.
  155. When an operation is performed, this function will be called once for
  156. each marked buffer, with that buffer current.
  157. ARGS becomes the formal parameters of the function.
  158. DOCUMENTATION becomes the docstring of the function.
  159. INTERACTIVE becomes the interactive specification of the function.
  160. MARK describes which type of mark (:deletion, or nil) this operation
  161. uses. :deletion means the function operates on buffers marked for
  162. deletion, otherwise it acts on normally marked buffers.
  163. MODIFIER-P describes how the function modifies buffers. This is used
  164. to set the modification flag of the Ibuffer buffer itself. Valid
  165. values are:
  166. nil - the function never modifiers buffers
  167. t - the function it always modifies buffers
  168. :maybe - attempt to discover this information by comparing the
  169. buffer's modification flag.
  170. DANGEROUS is a boolean which should be set if the user should be
  171. prompted before performing this operation.
  172. OPSTRING is a string which will be displayed to the user after the
  173. operation is complete, in the form:
  174. \"Operation complete; OPSTRING x buffers\"
  175. ACTIVE-OPSTRING is a string which will be displayed to the user in a
  176. confirmation message, in the form:
  177. \"Really ACTIVE-OPSTRING x buffers?\"
  178. COMPLEX means this function is special; see the source code of this
  179. macro for exactly what it does.
  180. \(fn OP ARGS DOCUMENTATION (&key INTERACTIVE MARK MODIFIER-P DANGEROUS OPSTRING ACTIVE-OPSTRING COMPLEX) &rest BODY)"
  181. (declare (indent 2))
  182. `(progn
  183. (defun ,(intern (concat (if (string-match "^ibuffer-do" (symbol-name op))
  184. "" "ibuffer-do-") (symbol-name op)))
  185. ,args
  186. ,(if (stringp documentation)
  187. documentation
  188. (format "%s marked buffers." active-opstring))
  189. ,(if (not (null interactive))
  190. `(interactive ,interactive)
  191. '(interactive))
  192. (assert (derived-mode-p 'ibuffer-mode))
  193. (setq ibuffer-did-modification nil)
  194. (let ((marked-names (,(case mark
  195. (:deletion
  196. 'ibuffer-deletion-marked-buffer-names)
  197. (t
  198. 'ibuffer-marked-buffer-names)))))
  199. (when (null marked-names)
  200. (setq marked-names (list (buffer-name (ibuffer-current-buffer))))
  201. (ibuffer-set-mark ,(case mark
  202. (:deletion
  203. 'ibuffer-deletion-char)
  204. (t
  205. 'ibuffer-marked-char))))
  206. ,(let* ((finish (append
  207. '(progn)
  208. (if (eq modifier-p t)
  209. '((setq ibuffer-did-modification t))
  210. ())
  211. `((ibuffer-redisplay t)
  212. (message ,(concat "Operation finished; " opstring " %s buffers") count))))
  213. (inner-body (if complex
  214. `(progn ,@body)
  215. `(progn
  216. (with-current-buffer buf
  217. (save-excursion
  218. ,@body))
  219. t)))
  220. (body `(let ((count
  221. (,(case mark
  222. (:deletion
  223. 'ibuffer-map-deletion-lines)
  224. (t
  225. 'ibuffer-map-marked-lines))
  226. #'(lambda (buf mark)
  227. ,(if (eq modifier-p :maybe)
  228. `(let ((ibuffer-tmp-previous-buffer-modification
  229. (buffer-modified-p buf)))
  230. (prog1 ,inner-body
  231. (when (not (eq ibuffer-tmp-previous-buffer-modification
  232. (buffer-modified-p buf)))
  233. (setq ibuffer-did-modification t))))
  234. inner-body)))))
  235. ,finish)))
  236. (if dangerous
  237. `(when (ibuffer-confirm-operation-on ,active-opstring marked-names)
  238. ,body)
  239. body))))
  240. :autoload-end))
  241. ;;;###autoload
  242. (defmacro* define-ibuffer-filter (name documentation
  243. (&key
  244. reader
  245. description)
  246. &rest body)
  247. "Define a filter named NAME.
  248. DOCUMENTATION is the documentation of the function.
  249. READER is a form which should read a qualifier from the user.
  250. DESCRIPTION is a short string describing the filter.
  251. BODY should contain forms which will be evaluated to test whether or
  252. not a particular buffer should be displayed or not. The forms in BODY
  253. will be evaluated with BUF bound to the buffer object, and QUALIFIER
  254. bound to the current value of the filter.
  255. \(fn NAME DOCUMENTATION (&key READER DESCRIPTION) &rest BODY)"
  256. (declare (indent 2))
  257. (let ((fn-name (intern (concat "ibuffer-filter-by-" (symbol-name name)))))
  258. `(progn
  259. (defun ,fn-name (qualifier)
  260. ,(or documentation "This filter is not documented.")
  261. (interactive (list ,reader))
  262. (ibuffer-push-filter (cons ',name qualifier))
  263. (message "%s"
  264. (format ,(concat (format "Filter by %s added: " description)
  265. " %s")
  266. qualifier))
  267. (ibuffer-update nil t))
  268. (push (list ',name ,description
  269. #'(lambda (buf qualifier)
  270. ,@body))
  271. ibuffer-filtering-alist)
  272. :autoload-end)))
  273. (provide 'ibuf-macs)
  274. ;;; ibuf-macs.el ends here