Mercurial > emacs
annotate lisp/ibuf-macs.el @ 42810:e0a03741d896
Only require fontset when new-fontset is bound.
author | Jason Rumney <jasonr@gnu.org> |
---|---|
date | Thu, 17 Jan 2002 19:24:39 +0000 |
parents | a7530850a26c |
children | 7f95aeaa47b3 |
rev | line source |
---|---|
42702 | 1 ;;; ibuf-macs.el --- macros for ibuffer |
2 | |
42770
a7530850a26c
(toplevel): Require `cl' when compiling.
Colin Walters <walters@gnu.org>
parents:
42702
diff
changeset
|
3 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. |
42702 | 4 |
5 ;; Author: Colin Walters <walters@verbum.org> | |
6 ;; Created: 6 Dec 2001 | |
7 ;; Keywords: buffer, convenience | |
8 | |
9 ;; This file is not currently part of GNU Emacs. | |
10 | |
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. | |
15 | |
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. | |
20 | |
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. | |
25 | |
26 ;;; Code: | |
27 | |
42770
a7530850a26c
(toplevel): Require `cl' when compiling.
Colin Walters <walters@gnu.org>
parents:
42702
diff
changeset
|
28 (eval-when-compile |
a7530850a26c
(toplevel): Require `cl' when compiling.
Colin Walters <walters@gnu.org>
parents:
42702
diff
changeset
|
29 (require 'cl)) |
a7530850a26c
(toplevel): Require `cl' when compiling.
Colin Walters <walters@gnu.org>
parents:
42702
diff
changeset
|
30 |
42702 | 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'. | |
36 Compare with `if'." | |
37 (let ((sym (gensym "--ibuffer-aif-"))) | |
38 `(let ((,sym ,test)) | |
39 (if ,sym | |
40 (let ((it ,sym)) | |
41 ,true-body) | |
42 (progn | |
43 ,@false-body))))) | |
44 ;; (put 'ibuffer-aif 'lisp-indent-function 2) | |
45 | |
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." | |
49 `(ibuffer-aif ,test | |
50 (progn ,@body) | |
51 nil)) | |
52 ;; (put 'ibuffer-awhen 'lisp-indent-function 1) | |
53 | |
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))) | |
59 (unwind-protect | |
60 (progn | |
61 (save-excursion | |
62 ,@body)) | |
63 (with-current-buffer ,bufsym | |
64 (ibuffer-insert-buffers-and-marks | |
65 ;; Get rid of dead buffers | |
66 (delq nil | |
67 (mapcar #'(lambda (e) (when (buffer-live-p (car e)) | |
68 e)) | |
69 ibuffer-save-marks-tmp-mark-list))) | |
70 (ibuffer-redisplay t)))))) | |
71 ;; (put 'ibuffer-save-marks 'lisp-indent-function 0) | |
72 | |
73 ;;;###autoload | |
74 (defmacro* define-ibuffer-column (symbol (&key name inline props) &rest body) | |
75 "Define a column SYMBOL for use with `ibuffer-formats'. | |
76 | |
77 BODY will be called with `buffer' bound to the buffer object, and | |
78 `mark' bound to the current mark on the buffer. The current buffer | |
79 will be `buffer'. | |
80 | |
81 If NAME is given, it will be used as a title for the column. | |
82 Otherwise, the title will default to a capitalized version of the | |
83 SYMBOL's name. PROPS is a plist of additional properties to add to | |
84 the text, such as `mouse-face'. | |
85 | |
86 Note that this macro expands into a `defun' for a function named | |
87 ibuffer-make-column-NAME. If INLINE is non-nil, then the form will be | |
88 inlined into the compiled format versions. This means that if you | |
89 change its definition, you should explicitly call | |
90 `ibuffer-recompile-formats'." | |
91 (let* ((sym (intern (concat "ibuffer-make-column-" | |
92 (symbol-name symbol)))) | |
93 (bod-1 `(with-current-buffer buffer | |
94 ,@body)) | |
95 (bod (if props | |
96 `(propertize | |
97 ,bod-1 | |
98 ,@props) | |
99 bod-1))) | |
100 `(progn | |
101 ,(if inline | |
102 `(push '(,sym ,bod) ibuffer-inline-columns) | |
103 `(defun ,sym (buffer mark) | |
104 ,bod)) | |
105 (put (quote ,sym) 'ibuffer-column-name | |
106 ,(if (stringp name) | |
107 name | |
108 (capitalize (symbol-name symbol)))) | |
109 :autoload-end))) | |
110 ;; (put 'define-ibuffer-column 'lisp-indent-function 'defun) | |
111 | |
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 | |
122 For sorting, the forms in BODY will be evaluated with `a' bound to one | |
123 buffer object, and `b' bound to another. BODY should return a non-nil | |
124 value if and only if `a' is \"less than\" `b'." | |
125 `(progn | |
126 (defun ,(intern (concat "ibuffer-do-sort-by-" (symbol-name name))) () | |
127 ,(or documentation "No :documentation specified for this sorting method.") | |
128 (interactive) | |
129 (setq ibuffer-sorting-mode ',name) | |
130 (ibuffer-redisplay t)) | |
131 (push (list ',name ,description | |
132 #'(lambda (a b) | |
133 ,@body)) | |
134 ibuffer-sorting-functions-alist) | |
135 :autoload-end)) | |
136 ;; (put 'define-ibuffer-sorter 'lisp-indent-function 1) | |
137 | |
138 ;;;###autoload | |
139 (defmacro* define-ibuffer-op (op args | |
140 documentation | |
141 (&key | |
142 interactive | |
143 mark | |
144 modifier-p | |
145 dangerous | |
146 (opstring "operated on") | |
147 (active-opstring "Operate on") | |
148 complex) | |
149 &rest body) | |
150 "Generate a function named `ibuffer-do-OP', which operates on a buffer. | |
151 When an operation is performed, this function will be called once for | |
152 each marked buffer, with that buffer current. | |
153 | |
154 ARGS becomes the formal parameters of the function. | |
155 DOCUMENTATION becomes the docstring of the function. | |
156 INTERACTIVE becomes the interactive specification of the function. | |
157 MARK describes which type of mark (:deletion, or nil) this operation | |
158 uses. :deletion means the function operates on buffers marked for | |
159 deletion, otherwise it acts on normally marked buffers. | |
160 MODIFIER-P describes how the function modifies buffers. This is used | |
161 to set the modification flag of the Ibuffer buffer itself. Valid | |
162 values are: | |
163 nil - the function never modifiers buffers | |
164 t - the function it always modifies buffers | |
165 :maybe - attempt to discover this information by comparing the | |
166 buffer's modification flag. | |
167 DANGEROUS is a boolean which should be set if the user should be | |
168 prompted before performing this operation. | |
169 OPSTRING is a string which will be displayed to the user after the | |
170 operation is complete, in the form: | |
171 \"Operation complete; OPSTRING x buffers\" | |
172 ACTIVE-OPSTRING is a string which will be displayed to the user in a | |
173 confirmation message, in the form: | |
174 \"Really ACTIVE-OPSTRING x buffers?\" | |
175 COMPLEX means this function is special; see the source code of this | |
176 macro for exactly what it does." | |
177 `(progn | |
178 (defun ,(intern (concat "ibuffer-do-" (symbol-name op))) ,args | |
179 ,(if (stringp documentation) | |
180 documentation | |
181 (format "%s marked buffers." active-opstring)) | |
182 ,(if (not (null interactive)) | |
183 `(interactive ,interactive) | |
184 '(interactive)) | |
185 (assert (eq major-mode 'ibuffer-mode)) | |
186 (setq ibuffer-did-modification nil) | |
187 (let ((marked-names (,(case mark | |
188 (:deletion | |
189 'ibuffer-deletion-marked-buffer-names) | |
190 (t | |
191 'ibuffer-marked-buffer-names))))) | |
192 (when (null marked-names) | |
193 (setq marked-names (list (buffer-name (ibuffer-current-buffer)))) | |
194 (ibuffer-set-mark ,(case mark | |
195 (:deletion | |
196 'ibuffer-deletion-char) | |
197 (t | |
198 'ibuffer-marked-char)))) | |
199 ,(let* ((finish (append | |
200 '(progn) | |
201 (if (eq modifier-p t) | |
202 '((setq ibuffer-did-modification t)) | |
203 ()) | |
204 `((ibuffer-redisplay t) | |
205 (message ,(concat "Operation finished; " opstring " %s buffers") count)))) | |
206 (inner-body (if complex | |
207 `(progn ,@body) | |
208 `(progn | |
209 (with-current-buffer buf | |
210 (save-excursion | |
211 ,@body)) | |
212 t))) | |
213 (body `(let ((count | |
214 (,(case mark | |
215 (:deletion | |
216 'ibuffer-map-deletion-lines) | |
217 (t | |
218 'ibuffer-map-marked-lines)) | |
219 #'(lambda (buf mark beg end) | |
220 ,(if (eq modifier-p :maybe) | |
221 `(let ((ibuffer-tmp-previous-buffer-modification | |
222 (buffer-modified-p buf))) | |
223 (prog1 ,inner-body | |
224 (when (not (eq ibuffer-tmp-previous-buffer-modification | |
225 (buffer-modified-p buf))) | |
226 (setq ibuffer-did-modification t)))) | |
227 inner-body))))) | |
228 ,finish))) | |
229 (if dangerous | |
230 `(when (ibuffer-confirm-operation-on ,active-opstring marked-names) | |
231 ,body) | |
232 body)))) | |
233 :autoload-end)) | |
234 ;; (put 'define-ibuffer-op 'lisp-indent-function 2) | |
235 | |
236 ;;;###autoload | |
237 (defmacro* define-ibuffer-filter (name documentation | |
238 (&key | |
239 reader | |
240 description) | |
241 &rest body) | |
242 "Define a filter named NAME. | |
243 DOCUMENTATION is the documentation of the function. | |
244 READER is a form which should read a qualifier from the user. | |
245 DESCRIPTION is a short string describing the filter. | |
246 | |
247 BODY should contain forms which will be evaluated to test whether or | |
248 not a particular buffer should be displayed or not. The forms in BODY | |
249 will be evaluated with BUF bound to the buffer object, and QUALIFIER | |
250 bound to the current value of the filter." | |
251 (let ((fn-name (intern (concat "ibuffer-filter-by-" (symbol-name name))))) | |
252 `(progn | |
253 (defun ,fn-name (qualifier) | |
254 ,(concat (or documentation "This filter is not documented.")) | |
255 (interactive (list ,reader)) | |
256 (ibuffer-push-filter (cons ',name qualifier)) | |
257 (message | |
258 (format ,(concat (format "Filter by %s added: " description) | |
259 " %s") | |
260 qualifier)) | |
261 (ibuffer-update nil t)) | |
262 (push (list ',name ,description | |
263 #'(lambda (buf qualifier) | |
264 ,@body)) | |
265 ibuffer-filtering-alist) | |
266 :autoload-end))) | |
267 ;; (put 'define-ibuffer-filter 'lisp-indent-function 2) | |
268 | |
269 (provide 'ibuf-macs) | |
270 | |
271 ;;; ibuf-macs.el ends here |