1 ;;; ibuf-macs.el --- macros for ibuffer
3 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Colin Walters <walters@verbum.org>
7 ;; Keywords: buffer, convenience
9 ;; This file is not currently part of GNU Emacs.
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program ; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
31 ;; From Paul Graham's "ANSI Common Lisp", adapted for Emacs Lisp here.
32 (defmacro ibuffer-aif
(test true-body
&rest false-body
)
33 "Evaluate TRUE-BODY or FALSE-BODY depending on value of TEST.
34 If TEST returns non-nil, bind `it' to the value, and evaluate
35 TRUE-BODY. Otherwise, evaluate forms in FALSE-BODY as if in `progn'.
37 (let ((sym (gensym "--ibuffer-aif-")))
44 ;; (put 'ibuffer-aif 'lisp-indent-function 2)
46 (defmacro ibuffer-awhen
(test &rest body
)
47 "Evaluate BODY if TEST returns non-nil.
48 During evaluation of body, bind `it' to the value returned by TEST."
52 ;; (put 'ibuffer-awhen 'lisp-indent-function 1)
54 (defmacro ibuffer-save-marks
(&rest body
)
55 "Save the marked status of the buffers and execute BODY; restore marks."
56 (let ((bufsym (gensym)))
57 `(let ((,bufsym
(current-buffer))
58 (ibuffer-save-marks-tmp-mark-list (ibuffer-current-state-list)))
63 (with-current-buffer ,bufsym
64 (ibuffer-insert-buffers-and-marks
65 ;; Get rid of dead buffers
67 (mapcar #'(lambda (e) (when (buffer-live-p (car e
))
69 ibuffer-save-marks-tmp-mark-list
)))
70 (ibuffer-redisplay t
))))))
71 ;; (put 'ibuffer-save-marks 'lisp-indent-function 0)
74 (defmacro* define-ibuffer-column
(symbol (&key name inline props
75 summarizer
) &rest body
)
76 "Define a column SYMBOL for use with `ibuffer-formats'.
78 BODY will be called with `buffer' bound to the buffer object, and
79 `mark' bound to the current mark on the buffer. The current buffer
82 If NAME is given, it will be used as a title for the column.
83 Otherwise, the title will default to a capitalized version of the
84 SYMBOL's name. PROPS is a plist of additional properties to add to
85 the text, such as `mouse-face'. And SUMMARIZER, if given, is a
86 function which will be passed a list of all the strings in its column;
87 it should return a string to display at the bottom.
89 Note that this macro expands into a `defun' for a function named
90 ibuffer-make-column-NAME. If INLINE is non-nil, then the form will be
91 inlined into the compiled format versions. This means that if you
92 change its definition, you should explicitly call
93 `ibuffer-recompile-formats'."
94 (let* ((sym (intern (concat "ibuffer-make-column-"
95 (symbol-name symbol
))))
96 (bod-2 `(with-current-buffer buffer
101 ,(intern (format "ibuffer-summary-for-column-%s"
111 `(push '(,sym
,bod
) ibuffer-inline-columns
)
112 `(defun ,sym
(buffer mark
)
114 (put (quote ,sym
) 'ibuffer-column-name
117 (capitalize (symbol-name symbol
))))
119 `(put (quote ,sym
) 'ibuffer-column-summarizer
120 (quote ,summarizer
)))
122 `(defvar ,(intern (format "ibuffer-summary-for-column-%s"
126 ;; (put 'define-ibuffer-column 'lisp-indent-function 'defun)
129 (defmacro* define-ibuffer-sorter
(name documentation
133 "Define a method of sorting named NAME.
134 DOCUMENTATION is the documentation of the function, which will be called
135 `ibuffer-do-sort-by-NAME'.
136 DESCRIPTION is a short string describing the sorting method.
138 For sorting, the forms in BODY will be evaluated with `a' bound to one
139 buffer object, and `b' bound to another. BODY should return a non-nil
140 value if and only if `a' is \"less than\" `b'."
142 (defun ,(intern (concat "ibuffer-do-sort-by-" (symbol-name name
))) ()
143 ,(or documentation
"No :documentation specified for this sorting method.")
145 (setq ibuffer-sorting-mode
',name
)
146 (ibuffer-redisplay t
))
147 (push (list ',name
,description
150 ibuffer-sorting-functions-alist
)
152 ;; (put 'define-ibuffer-sorter 'lisp-indent-function 1)
155 (defmacro* define-ibuffer-op
(op args
162 (opstring "operated on")
163 (active-opstring "Operate on")
166 "Generate a function named `ibuffer-do-OP', which operates on a buffer.
167 When an operation is performed, this function will be called once for
168 each marked buffer, with that buffer current.
170 ARGS becomes the formal parameters of the function.
171 DOCUMENTATION becomes the docstring of the function.
172 INTERACTIVE becomes the interactive specification of the function.
173 MARK describes which type of mark (:deletion, or nil) this operation
174 uses. :deletion means the function operates on buffers marked for
175 deletion, otherwise it acts on normally marked buffers.
176 MODIFIER-P describes how the function modifies buffers. This is used
177 to set the modification flag of the Ibuffer buffer itself. Valid
179 nil - the function never modifiers buffers
180 t - the function it always modifies buffers
181 :maybe - attempt to discover this information by comparing the
182 buffer's modification flag.
183 DANGEROUS is a boolean which should be set if the user should be
184 prompted before performing this operation.
185 OPSTRING is a string which will be displayed to the user after the
186 operation is complete, in the form:
187 \"Operation complete; OPSTRING x buffers\"
188 ACTIVE-OPSTRING is a string which will be displayed to the user in a
189 confirmation message, in the form:
190 \"Really ACTIVE-OPSTRING x buffers?\"
191 COMPLEX means this function is special; see the source code of this
192 macro for exactly what it does."
194 (defun ,(intern (concat "ibuffer-do-" (symbol-name op
))) ,args
195 ,(if (stringp documentation
)
197 (format "%s marked buffers." active-opstring
))
198 ,(if (not (null interactive
))
199 `(interactive ,interactive
)
201 (assert (eq major-mode
'ibuffer-mode
))
202 (setq ibuffer-did-modification nil
)
203 (let ((marked-names (,(case mark
205 'ibuffer-deletion-marked-buffer-names
)
207 'ibuffer-marked-buffer-names
)))))
208 (when (null marked-names
)
209 (setq marked-names
(list (buffer-name (ibuffer-current-buffer))))
210 (ibuffer-set-mark ,(case mark
212 'ibuffer-deletion-char
)
214 'ibuffer-marked-char
))))
215 ,(let* ((finish (append
217 (if (eq modifier-p t
)
218 '((setq ibuffer-did-modification t
))
220 `((ibuffer-redisplay t
)
221 (message ,(concat "Operation finished; " opstring
" %s buffers") count
))))
222 (inner-body (if complex
225 (with-current-buffer buf
232 'ibuffer-map-deletion-lines
)
234 'ibuffer-map-marked-lines
))
235 #'(lambda (buf mark beg end
)
236 ,(if (eq modifier-p
:maybe
)
237 `(let ((ibuffer-tmp-previous-buffer-modification
238 (buffer-modified-p buf
)))
240 (when (not (eq ibuffer-tmp-previous-buffer-modification
241 (buffer-modified-p buf
)))
242 (setq ibuffer-did-modification t
))))
246 `(when (ibuffer-confirm-operation-on ,active-opstring marked-names
)
250 ;; (put 'define-ibuffer-op 'lisp-indent-function 2)
253 (defmacro* define-ibuffer-filter
(name documentation
258 "Define a filter named NAME.
259 DOCUMENTATION is the documentation of the function.
260 READER is a form which should read a qualifier from the user.
261 DESCRIPTION is a short string describing the filter.
263 BODY should contain forms which will be evaluated to test whether or
264 not a particular buffer should be displayed or not. The forms in BODY
265 will be evaluated with BUF bound to the buffer object, and QUALIFIER
266 bound to the current value of the filter."
267 (let ((fn-name (intern (concat "ibuffer-filter-by-" (symbol-name name
)))))
269 (defun ,fn-name
(qualifier)
270 ,(concat (or documentation
"This filter is not documented."))
271 (interactive (list ,reader
))
272 (ibuffer-push-filter (cons ',name qualifier
))
274 (format ,(concat (format "Filter by %s added: " description
)
277 (ibuffer-update nil t
))
278 (push (list ',name
,description
279 #'(lambda (buf qualifier
)
281 ibuffer-filtering-alist
)
283 ;; (put 'define-ibuffer-filter 'lisp-indent-function 2)
287 ;;; ibuf-macs.el ends here