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 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.
33 ;; From Paul Graham's "ANSI Common Lisp", adapted for Emacs Lisp here.
34 (defmacro ibuffer-aif
(test true-body
&rest false-body
)
35 "Evaluate TRUE-BODY or FALSE-BODY depending on value of TEST.
36 If TEST returns non-nil, bind `it' to the value, and evaluate
37 TRUE-BODY. Otherwise, evaluate forms in FALSE-BODY as if in `progn'.
39 (let ((sym (gensym "--ibuffer-aif-")))
46 ;; (put 'ibuffer-aif 'lisp-indent-function 2)
48 (defmacro ibuffer-awhen
(test &rest body
)
49 "Evaluate BODY if TEST returns non-nil.
50 During evaluation of body, bind `it' to the value returned by TEST."
54 ;; (put 'ibuffer-awhen 'lisp-indent-function 1)
56 (defmacro ibuffer-save-marks
(&rest body
)
57 "Save the marked status of the buffers and execute BODY; restore marks."
58 (let ((bufsym (gensym)))
59 `(let ((,bufsym
(current-buffer))
60 (ibuffer-save-marks-tmp-mark-list (ibuffer-current-state-list)))
65 (with-current-buffer ,bufsym
66 (ibuffer-redisplay-engine
67 ;; Get rid of dead buffers
69 (mapcar #'(lambda (e) (when (buffer-live-p (car e
))
71 ibuffer-save-marks-tmp-mark-list
)))
72 (ibuffer-redisplay t
))))))
73 ;; (put 'ibuffer-save-marks 'lisp-indent-function 0)
76 (defmacro* define-ibuffer-column
(symbol (&key name inline props
77 summarizer
) &rest body
)
78 "Define a column SYMBOL for use with `ibuffer-formats'.
80 BODY will be called with `buffer' bound to the buffer object, and
81 `mark' bound to the current mark on the buffer. The current buffer
84 If NAME is given, it will be used as a title for the column.
85 Otherwise, the title will default to a capitalized version of the
86 SYMBOL's name. PROPS is a plist of additional properties to add to
87 the text, such as `mouse-face'. And SUMMARIZER, if given, is a
88 function which will be passed a list of all the strings in its column;
89 it should return a string to display at the bottom.
91 Note that this macro expands into a `defun' for a function named
92 ibuffer-make-column-NAME. If INLINE is non-nil, then the form will be
93 inlined into the compiled format versions. This means that if you
94 change its definition, you should explicitly call
95 `ibuffer-recompile-formats'."
96 (let* ((sym (intern (concat "ibuffer-make-column-"
97 (symbol-name symbol
))))
98 (bod-1 `(with-current-buffer buffer
107 `(push '(,sym
,bod
) ibuffer-inline-columns
)
108 `(defun ,sym
(buffer mark
)
110 (put (quote ,sym
) 'ibuffer-column-name
113 (capitalize (symbol-name symbol
))))
115 ;; Store the name of the summarizing function.
116 `(put (quote ,sym
) 'ibuffer-column-summarizer
117 (quote ,summarizer
)))
119 ;; This will store the actual values of the column
121 `(put (quote ,sym
) 'ibuffer-column-summary nil
))
123 ;; (put 'define-ibuffer-column 'lisp-indent-function 'defun)
126 (defmacro* define-ibuffer-sorter
(name documentation
130 "Define a method of sorting named NAME.
131 DOCUMENTATION is the documentation of the function, which will be called
132 `ibuffer-do-sort-by-NAME'.
133 DESCRIPTION is a short string describing the sorting method.
135 For sorting, the forms in BODY will be evaluated with `a' bound to one
136 buffer object, and `b' bound to another. BODY should return a non-nil
137 value if and only if `a' is \"less than\" `b'."
139 (defun ,(intern (concat "ibuffer-do-sort-by-" (symbol-name name
))) ()
140 ,(or documentation
"No :documentation specified for this sorting method.")
142 (setq ibuffer-sorting-mode
',name
)
143 (ibuffer-redisplay t
))
144 (push (list ',name
,description
147 ibuffer-sorting-functions-alist
)
149 ;; (put 'define-ibuffer-sorter 'lisp-indent-function 1)
152 (defmacro* define-ibuffer-op
(op args
159 (opstring "operated on")
160 (active-opstring "Operate on")
163 "Generate a function named `ibuffer-do-OP', which operates on a buffer.
164 When an operation is performed, this function will be called once for
165 each marked buffer, with that buffer current.
167 ARGS becomes the formal parameters of the function.
168 DOCUMENTATION becomes the docstring of the function.
169 INTERACTIVE becomes the interactive specification of the function.
170 MARK describes which type of mark (:deletion, or nil) this operation
171 uses. :deletion means the function operates on buffers marked for
172 deletion, otherwise it acts on normally marked buffers.
173 MODIFIER-P describes how the function modifies buffers. This is used
174 to set the modification flag of the Ibuffer buffer itself. Valid
176 nil - the function never modifiers buffers
177 t - the function it always modifies buffers
178 :maybe - attempt to discover this information by comparing the
179 buffer's modification flag.
180 DANGEROUS is a boolean which should be set if the user should be
181 prompted before performing this operation.
182 OPSTRING is a string which will be displayed to the user after the
183 operation is complete, in the form:
184 \"Operation complete; OPSTRING x buffers\"
185 ACTIVE-OPSTRING is a string which will be displayed to the user in a
186 confirmation message, in the form:
187 \"Really ACTIVE-OPSTRING x buffers?\"
188 COMPLEX means this function is special; see the source code of this
189 macro for exactly what it does."
191 (defun ,(intern (concat "ibuffer-do-" (symbol-name op
))) ,args
192 ,(if (stringp documentation
)
194 (format "%s marked buffers." active-opstring
))
195 ,(if (not (null interactive
))
196 `(interactive ,interactive
)
198 (assert (eq major-mode
'ibuffer-mode
))
199 (setq ibuffer-did-modification nil
)
200 (let ((marked-names (,(case mark
202 'ibuffer-deletion-marked-buffer-names
)
204 'ibuffer-marked-buffer-names
)))))
205 (when (null marked-names
)
206 (setq marked-names
(list (buffer-name (ibuffer-current-buffer))))
207 (ibuffer-set-mark ,(case mark
209 'ibuffer-deletion-char
)
211 'ibuffer-marked-char
))))
212 ,(let* ((finish (append
214 (if (eq modifier-p t
)
215 '((setq ibuffer-did-modification t
))
217 `((ibuffer-redisplay t
)
218 (message ,(concat "Operation finished; " opstring
" %s buffers") count
))))
219 (inner-body (if complex
222 (with-current-buffer buf
229 'ibuffer-map-deletion-lines
)
231 'ibuffer-map-marked-lines
))
233 ,(if (eq modifier-p
:maybe
)
234 `(let ((ibuffer-tmp-previous-buffer-modification
235 (buffer-modified-p buf
)))
237 (when (not (eq ibuffer-tmp-previous-buffer-modification
238 (buffer-modified-p buf
)))
239 (setq ibuffer-did-modification t
))))
243 `(when (ibuffer-confirm-operation-on ,active-opstring marked-names
)
247 ;; (put 'define-ibuffer-op 'lisp-indent-function 2)
250 (defmacro* define-ibuffer-filter
(name documentation
255 "Define a filter named NAME.
256 DOCUMENTATION is the documentation of the function.
257 READER is a form which should read a qualifier from the user.
258 DESCRIPTION is a short string describing the filter.
260 BODY should contain forms which will be evaluated to test whether or
261 not a particular buffer should be displayed or not. The forms in BODY
262 will be evaluated with BUF bound to the buffer object, and QUALIFIER
263 bound to the current value of the filter."
264 (let ((fn-name (intern (concat "ibuffer-filter-by-" (symbol-name name
)))))
266 (defun ,fn-name
(qualifier)
267 ,(concat (or documentation
"This filter is not documented."))
268 (interactive (list ,reader
))
269 (ibuffer-push-filter (cons ',name qualifier
))
271 (format ,(concat (format "Filter by %s added: " description
)
274 (ibuffer-update nil t
))
275 (push (list ',name
,description
276 #'(lambda (buf qualifier
)
278 ibuffer-filtering-alist
)
280 ;; (put 'define-ibuffer-filter 'lisp-indent-function 2)
284 ;;; ibuf-macs.el ends here