Mercurial > emacs
annotate lisp/ibuf-ext.el @ 43488:395cff3e82cd
(calc-invent-variables): Convert integer to string.
author | Colin Walters <walters@gnu.org> |
---|---|
date | Sat, 23 Feb 2002 21:33:19 +0000 |
parents | 3fce95e542b1 |
children | a08f4763cba4 |
rev | line source |
---|---|
42813
2fa4141b1dee
Enable byte-compile-dynamic.
Richard M. Stallman <rms@gnu.org>
parents:
42804
diff
changeset
|
1 ;;; ibuf-ext.el --- extensions for ibuffer -*-byte-compile-dynamic: t;-*- |
42702 | 2 |
42771
ed597889bfc8
(toplevel): Remove X-RCS, URL, Compatibility headers. Update
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: 2 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 ;;; Commentary: | |
27 | |
28 ;; These functions should be automatically loaded when called, but you | |
29 ;; can explicity (require 'ibuf-ext) in your ~/.emacs to have them | |
30 ;; preloaded. | |
31 | |
32 ;;; Code: | |
33 | |
34 (require 'ibuffer) | |
35 | |
36 (eval-when-compile | |
37 (require 'derived) | |
38 (require 'ibuf-macs) | |
39 (require 'cl)) | |
40 | |
41 ;;; Utility functions | |
42 (defun ibuffer-delete-alist (key alist) | |
43 "Delete all entries in ALIST that have a key equal to KEY." | |
44 (let (entry) | |
45 (while (setq entry (assoc key alist)) | |
46 (setq alist (delete entry alist))) | |
47 alist)) | |
48 | |
49 (defun ibuffer-depropertize-string (str &optional nocopy) | |
50 "Return a copy of STR with text properties removed. | |
51 If optional argument NOCOPY is non-nil, actually modify the string directly." | |
52 (let ((str (if nocopy | |
53 str | |
54 (copy-sequence str)))) | |
55 (set-text-properties 0 (length str) nil str) | |
56 str)) | |
57 | |
58 (defcustom ibuffer-never-show-predicates nil | |
59 "A list of predicates (a regexp or function) for buffers not to display. | |
60 If a regexp, then it will be matched against the buffer's name. | |
61 If a function, it will be called with the buffer as an argument, and | |
62 should return non-nil if this buffer should not be shown." | |
63 :type '(repeat (choice regexp function)) | |
64 :group 'ibuffer) | |
65 | |
66 (defcustom ibuffer-always-show-predicates nil | |
67 "A list of predicates (a regexp or function) for buffers to always display. | |
68 If a regexp, then it will be matched against the buffer's name. | |
69 If a function, it will be called with the buffer as an argument, and | |
70 should return non-nil if this buffer should be shown. | |
71 Note that buffers matching one of these predicates will be shown | |
72 regardless of any active filters in this buffer." | |
73 :type '(repeat (choice regexp function)) | |
74 :group 'ibuffer) | |
75 | |
76 (defvar ibuffer-tmp-hide-regexps nil | |
77 "A list of regexps which should match buffer names to not show.") | |
78 | |
79 (defvar ibuffer-tmp-show-regexps nil | |
80 "A list of regexps which should match buffer names to always show.") | |
81 | |
82 (defvar ibuffer-auto-mode nil | |
83 "If non-nil, Ibuffer auto-mode should be enabled for this buffer. | |
84 Do not set this variable directly! Use the function | |
85 `ibuffer-auto-mode' instead.") | |
86 | |
87 (defvar ibuffer-auto-buffers-changed nil) | |
88 | |
89 (defcustom ibuffer-occur-match-face 'font-lock-warning-face | |
90 "Face used for displaying matched strings for `ibuffer-do-occur'." | |
91 :type 'face | |
92 :group 'ibuffer) | |
93 | |
94 (defcustom ibuffer-saved-filters '(("gnus" | |
95 ((or (mode . message-mode) | |
96 (mode . mail-mode) | |
97 (mode . gnus-group-mode) | |
98 (mode . gnus-summary-mode) | |
99 (mode . gnus-article-mode)))) | |
100 ("programming" | |
101 ((or (mode . emacs-lisp-mode) | |
102 (mode . cperl-mode) | |
103 (mode . c-mode) | |
104 (mode . java-mode) | |
105 (mode . idl-mode) | |
106 (mode . lisp-mode))))) | |
107 | |
108 "An alist of filter qualifiers to switch between. | |
109 | |
110 This variable should look like ((\"STRING\" QUALIFIERS) | |
111 (\"STRING\" QUALIFIERS) ...), where | |
112 QUALIFIERS is a list of the same form as | |
113 `ibuffer-filtering-qualifiers'. | |
114 See also the variables `ibuffer-filtering-qualifiers', | |
115 `ibuffer-filtering-alist', and the functions | |
116 `ibuffer-switch-to-saved-filters', `ibuffer-save-filters'." | |
117 :type '(repeat sexp) | |
118 :group 'ibuffer) | |
119 | |
120 (defvar ibuffer-filtering-qualifiers nil | |
121 "A list like (SYMBOL . QUALIFIER) which filters the current buffer list. | |
122 See also `ibuffer-filtering-alist'.") | |
123 | |
124 ;; This is now frobbed by `define-ibuffer-filter'. | |
125 (defvar ibuffer-filtering-alist nil | |
126 "An alist of (SYMBOL DESCRIPTION FUNCTION) which describes a filter. | |
127 | |
128 You most likely do not want to modify this variable directly; see | |
129 `define-ibuffer-filter'. | |
130 | |
131 SYMBOL is the symbolic name of the filter. DESCRIPTION is used when | |
132 displaying information to the user. FUNCTION is given a buffer and | |
133 the value of the qualifier, and returns non-nil if and only if the | |
134 buffer should be displayed.") | |
135 | |
43102
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
136 (defcustom ibuffer-filter-format-alist nil |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
137 "An alist which has special formats used when a filter is active. |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
138 The contents of this variable should look like: |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
139 ((FILTER (FORMAT FORMAT ...)) (FILTER (FORMAT FORMAT ...)) ...) |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
140 |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
141 For example, suppose that when you add a filter for buffers whose |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
142 major mode is `emacs-lisp-mode', you only want to see the mark and the |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
143 name of the buffer. You could accomplish that by adding: |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
144 (mode ((mark \" \" name))) |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
145 to this variable.") |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
146 |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
147 (defvar ibuffer-cached-filter-formats nil) |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
148 (defvar ibuffer-compiled-filter-formats nil) |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
149 |
42702 | 150 (defcustom ibuffer-old-time 3 |
151 "The number of days before a buffer is considered \"old\"." | |
152 :type 'integer | |
153 :group 'ibuffer) | |
154 | |
155 (defcustom ibuffer-save-with-custom t | |
156 "If non-nil, then use Custom to save interactively changed variables. | |
157 Currently, this only applies to `ibuffer-saved-filters'." | |
158 :type 'boolean | |
159 :group 'ibuffer) | |
160 | |
161 (defun ibuffer-ext-visible-p (buf all &optional ibuffer-buf) | |
162 (or | |
163 (ibuffer-buf-matches-predicates buf ibuffer-tmp-show-regexps) | |
164 (and (not | |
165 (or | |
166 (ibuffer-buf-matches-predicates buf ibuffer-tmp-hide-regexps) | |
167 (ibuffer-buf-matches-predicates buf ibuffer-never-show-predicates))) | |
168 (or all | |
169 (not | |
170 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates))) | |
171 (or ibuffer-view-ibuffer | |
172 (and ibuffer-buf | |
173 (not (eq ibuffer-buf buf)))) | |
174 (or | |
175 (ibuffer-included-in-filters-p buf ibuffer-filtering-qualifiers) | |
176 (ibuffer-buf-matches-predicates buf ibuffer-always-show-predicates))))) | |
177 | |
178 (defun ibuffer-auto-update-changed () | |
179 (when ibuffer-auto-buffers-changed | |
180 (setq ibuffer-auto-buffers-changed nil) | |
181 (mapcar #'(lambda (buf) | |
182 (ignore-errors | |
183 (with-current-buffer buf | |
184 (when (and ibuffer-auto-mode | |
185 (eq major-mode 'ibuffer-mode)) | |
186 (ibuffer-update nil t))))) | |
187 (buffer-list)))) | |
188 | |
189 ;;;###autoload | |
190 (defun ibuffer-auto-mode (&optional arg) | |
191 "Toggle use of Ibuffer's auto-update facility. | |
192 With numeric ARG, enable auto-update if and only if ARG is positive." | |
193 (interactive) | |
194 (unless (eq major-mode 'ibuffer-mode) | |
195 (error "This buffer is not in Ibuffer mode")) | |
196 (set (make-local-variable 'ibuffer-auto-mode) | |
197 (if arg | |
198 (plusp arg) | |
199 (not ibuffer-auto-mode))) | |
200 (defadvice get-buffer-create (after ibuffer-notify-create activate) | |
201 (setq ibuffer-auto-buffers-changed t)) | |
202 (defadvice kill-buffer (after ibuffer-notify-kill activate) | |
203 (setq ibuffer-auto-buffers-changed t)) | |
204 (add-hook 'post-command-hook 'ibuffer-auto-update-changed) | |
205 (ibuffer-update-mode-name)) | |
206 | |
207 ;;;###autoload | |
208 (defun ibuffer-mouse-filter-by-mode (event) | |
209 "Enable or disable filtering by the major mode chosen via mouse." | |
210 (interactive "e") | |
211 (ibuffer-interactive-filter-by-mode event)) | |
212 | |
213 ;;;###autoload | |
214 (defun ibuffer-interactive-filter-by-mode (event-or-point) | |
215 "Enable or disable filtering by the major mode at point." | |
216 (interactive "d") | |
217 (if (eventp event-or-point) | |
218 (mouse-set-point event-or-point) | |
219 (goto-char event-or-point)) | |
220 (let ((buf (ibuffer-current-buffer))) | |
221 (if (assq 'mode ibuffer-filtering-qualifiers) | |
222 (setq ibuffer-filtering-qualifiers | |
223 (ibuffer-delete-alist 'mode ibuffer-filtering-qualifiers)) | |
224 (ibuffer-push-filter (cons 'mode | |
225 (with-current-buffer buf | |
226 major-mode))))) | |
227 (ibuffer-update nil t)) | |
228 | |
229 (define-ibuffer-op shell-command-pipe (command) | |
230 "Pipe the contents of each marked buffer to shell command COMMAND." | |
231 (:interactive "sPipe to shell command: " | |
232 :opstring "Shell command executed on" | |
233 :modifier-p nil) | |
234 (shell-command-on-region | |
235 (point-min) (point-max) command | |
236 (get-buffer-create "* ibuffer-shell-output*"))) | |
237 | |
238 (define-ibuffer-op shell-command-pipe-replace (command) | |
239 "Replace the contents of marked buffers with output of pipe to COMMAND." | |
240 (:interactive "sPipe to shell command (replace): " | |
241 :opstring "Buffer contents replaced in" | |
242 :active-opstring "replace buffer contents in" | |
243 :dangerous t | |
244 :modifier-p t) | |
245 (with-current-buffer buf | |
246 (shell-command-on-region (point-min) (point-max) | |
247 command nil t))) | |
248 | |
249 (define-ibuffer-op shell-command-file (command) | |
250 "Run shell command COMMAND separately on files of marked buffers." | |
251 (:interactive "sShell command on buffer's file: " | |
252 :opstring "Shell command executed on" | |
253 :modifier-p nil) | |
254 (shell-command (concat command " " | |
255 (shell-quote-argument | |
256 (if buffer-file-name | |
257 buffer-file-name | |
258 (make-temp-file | |
259 (substring (buffer-name) 0 (min 10 (length (buffer-name)))))))))) | |
260 | |
261 (define-ibuffer-op eval (form) | |
262 "Evaluate FORM in each of the buffers. | |
263 Does not display the buffer during evaluation. See | |
264 `ibuffer-do-view-and-eval' for that." | |
265 (:interactive "xEval in buffers (form): " | |
266 :opstring "evaluated in" | |
267 :modifier-p :maybe) | |
268 (eval form)) | |
269 | |
270 (define-ibuffer-op view-and-eval (form) | |
271 "Evaluate FORM while displaying each of the marked buffers. | |
272 To evaluate a form without viewing the buffer, see `ibuffer-do-eval'." | |
273 (:interactive "xEval viewing buffers (form): " | |
274 :opstring "evaluated in" | |
275 :complex t | |
276 :modifier-p :maybe) | |
277 (let ((ibuffer-buf (current-buffer))) | |
278 (unwind-protect | |
279 (progn | |
280 (switch-to-buffer buf) | |
281 (eval form)) | |
282 (switch-to-buffer ibuffer-buf)))) | |
283 | |
284 (define-ibuffer-op rename-uniquely () | |
285 "Rename marked buffers as with `rename-uniquely'." | |
286 (:opstring "renamed" | |
287 :modifier-p t) | |
288 (rename-uniquely)) | |
289 | |
290 (define-ibuffer-op revert () | |
291 "Revert marked buffers as with `revert-buffer'." | |
292 (:dangerous t | |
293 :opstring "reverted" | |
294 :active-opstring "revert" | |
295 :modifier-p :maybe) | |
296 (revert-buffer t t)) | |
297 | |
298 (define-ibuffer-op replace-regexp (from-str to-str) | |
299 "Perform a `replace-regexp' in marked buffers." | |
300 (:interactive | |
301 (let* ((from-str (read-from-minibuffer "Replace regexp: ")) | |
302 (to-str (read-from-minibuffer (concat "Replace " from-str | |
303 " with: ")))) | |
304 (list from-str to-str)) | |
305 :opstring "replaced in" | |
306 :complex t | |
307 :modifier-p :maybe) | |
308 (save-window-excursion | |
309 (switch-to-buffer buf) | |
310 (save-excursion | |
311 (goto-char (point-min)) | |
312 (let ((case-fold-search ibuffer-case-fold-search)) | |
313 (while (re-search-forward from-str nil t) | |
314 (replace-match to-str)))) | |
315 t)) | |
316 | |
317 (define-ibuffer-op query-replace (&rest args) | |
318 "Perform a `query-replace' in marked buffers." | |
319 (:interactive | |
320 (query-replace-read-args "Query replace" t) | |
321 :opstring "replaced in" | |
322 :complex t | |
323 :modifier-p :maybe) | |
324 (save-window-excursion | |
325 (switch-to-buffer buf) | |
326 (save-excursion | |
327 (let ((case-fold-search ibuffer-case-fold-search)) | |
328 (goto-char (point-min)) | |
329 (apply #'query-replace args))) | |
330 t)) | |
331 | |
332 (define-ibuffer-op query-replace-regexp (&rest args) | |
333 "Perform a `query-replace-regexp' in marked buffers." | |
334 (:interactive | |
335 (query-replace-read-args "Query replace regexp" t) | |
336 :opstring "replaced in" | |
337 :complex t | |
338 :modifier-p :maybe) | |
339 (save-window-excursion | |
340 (switch-to-buffer buf) | |
341 (save-excursion | |
342 (let ((case-fold-search ibuffer-case-fold-search)) | |
343 (goto-char (point-min)) | |
344 (apply #'query-replace-regexp args))) | |
345 t)) | |
346 | |
347 (define-ibuffer-op print () | |
348 "Print marked buffers as with `print-buffer'." | |
349 (:opstring "printed" | |
350 :modifier-p nil) | |
351 (print-buffer)) | |
352 | |
353 ;;;###autoload | |
354 (defun ibuffer-included-in-filters-p (buf filters) | |
355 (not | |
356 (memq nil ;; a filter will return nil if it failed | |
357 (mapcar | |
358 ;; filter should be like (TYPE . QUALIFIER), or | |
359 ;; (or (TYPE . QUALIFIER) (TYPE . QUALIFIER) ...) | |
360 #'(lambda (qual) | |
361 (ibuffer-included-in-filter-p buf qual)) | |
362 filters)))) | |
363 | |
364 (defun ibuffer-included-in-filter-p (buf filter) | |
365 (if (eq (car filter) 'not) | |
366 (not (ibuffer-included-in-filter-p-1 buf (cdr filter))) | |
367 (ibuffer-included-in-filter-p-1 buf filter))) | |
368 | |
369 (defun ibuffer-included-in-filter-p-1 (buf filter) | |
370 (not | |
371 (not | |
372 (case (car filter) | |
373 (or | |
374 (memq t (mapcar #'(lambda (x) | |
375 (ibuffer-included-in-filter-p buf x)) | |
376 (cdr filter)))) | |
377 (saved | |
378 (let ((data | |
379 (assoc (cdr filter) | |
380 ibuffer-saved-filters))) | |
381 (unless data | |
382 (ibuffer-filter-disable) | |
383 (error "Unknown saved filter %s" (cdr filter))) | |
384 (ibuffer-included-in-filters-p buf (cadr data)))) | |
385 (t | |
386 (let ((filterdat (assq (car filter) | |
387 ibuffer-filtering-alist))) | |
388 ;; filterdat should be like (TYPE DESCRIPTION FUNC) | |
389 ;; just a sanity check | |
390 (unless filterdat | |
391 (ibuffer-filter-disable) | |
392 (error "Undefined filter %s" (car filter))) | |
393 (not | |
394 (not | |
395 (funcall (caddr filterdat) | |
396 buf | |
397 (cdr filter)))))))))) | |
398 | |
399 ;;;###autoload | |
400 (defun ibuffer-filter-disable () | |
401 "Disable all filters currently in effect in this buffer." | |
402 (interactive) | |
403 (setq ibuffer-filtering-qualifiers nil) | |
404 (ibuffer-update nil t)) | |
405 | |
406 ;;;###autoload | |
407 (defun ibuffer-pop-filter () | |
408 "Remove the top filter in this buffer." | |
409 (interactive) | |
410 (when (null ibuffer-filtering-qualifiers) | |
411 (error "No filters in effect")) | |
412 (pop ibuffer-filtering-qualifiers) | |
413 (ibuffer-update nil t)) | |
414 | |
415 (defun ibuffer-push-filter (qualifier) | |
416 "Add QUALIFIER to `ibuffer-filtering-qualifiers'." | |
417 (push qualifier ibuffer-filtering-qualifiers)) | |
418 | |
419 ;;;###autoload | |
420 (defun ibuffer-decompose-filter () | |
421 "Separate the top compound filter (OR, NOT, or SAVED) in this buffer. | |
422 | |
423 This means that the topmost filter on the filtering stack, which must | |
424 be a complex filter like (OR [name: foo] [mode: bar-mode]), will be | |
425 turned into two separate filters [name: foo] and [mode: bar-mode]." | |
426 (interactive) | |
427 (when (null ibuffer-filtering-qualifiers) | |
428 (error "No filters in effect")) | |
429 (let ((lim (pop ibuffer-filtering-qualifiers))) | |
430 (case (car lim) | |
431 (or | |
432 (setq ibuffer-filtering-qualifiers (append | |
433 (cdr lim) | |
434 ibuffer-filtering-qualifiers))) | |
435 (saved | |
436 (let ((data | |
437 (assoc (cdr lim) | |
438 ibuffer-saved-filters))) | |
439 (unless data | |
440 (ibuffer-filter-disable) | |
441 (error "Unknown saved filter %s" (cdr lim))) | |
442 (setq ibuffer-filtering-qualifiers (append | |
443 (cadr data) | |
444 ibuffer-filtering-qualifiers)))) | |
445 (not | |
446 (push (cdr lim) | |
447 ibuffer-filtering-qualifiers)) | |
448 (t | |
449 (error "Filter type %s is not compound" (car lim))))) | |
450 (ibuffer-update nil t)) | |
451 | |
452 ;;;###autoload | |
453 (defun ibuffer-exchange-filters () | |
454 "Exchange the top two filters on the stack in this buffer." | |
455 (interactive) | |
456 (when (< (length ibuffer-filtering-qualifiers) | |
457 2) | |
458 (error "Need two filters to exchange")) | |
459 (let ((first (pop ibuffer-filtering-qualifiers)) | |
460 (second (pop ibuffer-filtering-qualifiers))) | |
461 (push first ibuffer-filtering-qualifiers) | |
462 (push second ibuffer-filtering-qualifiers)) | |
463 (ibuffer-update nil t)) | |
464 | |
465 ;;;###autoload | |
466 (defun ibuffer-negate-filter () | |
467 "Negate the sense of the top filter in the current buffer." | |
468 (interactive) | |
469 (when (null ibuffer-filtering-qualifiers) | |
470 (error "No filters in effect")) | |
471 (let ((lim (pop ibuffer-filtering-qualifiers))) | |
472 (push (if (eq (car lim) 'not) | |
473 (cdr lim) | |
474 (cons 'not lim)) | |
475 ibuffer-filtering-qualifiers)) | |
476 (ibuffer-update nil t)) | |
477 | |
478 ;;;###autoload | |
479 (defun ibuffer-or-filter (&optional reverse) | |
480 "Replace the top two filters in this buffer with their logical OR. | |
481 If optional argument REVERSE is non-nil, instead break the top OR | |
482 filter into parts." | |
483 (interactive "P") | |
484 (if reverse | |
485 (progn | |
486 (when (or (null ibuffer-filtering-qualifiers) | |
487 (not (eq 'or (caar ibuffer-filtering-qualifiers)))) | |
488 (error "Top filter is not an OR")) | |
489 (let ((lim (pop ibuffer-filtering-qualifiers))) | |
490 (setq ibuffer-filtering-qualifiers (nconc (cdr lim) ibuffer-filtering-qualifiers)))) | |
491 (when (< (length ibuffer-filtering-qualifiers) 2) | |
492 (error "Need two filters to OR")) | |
493 ;; If the second filter is an OR, just add to it. | |
494 (let ((first (pop ibuffer-filtering-qualifiers)) | |
495 (second (pop ibuffer-filtering-qualifiers))) | |
496 (if (eq 'or (car second)) | |
497 (push (nconc (list 'or first) (cdr second)) ibuffer-filtering-qualifiers) | |
498 (push (list 'or first second) | |
499 ibuffer-filtering-qualifiers)))) | |
500 (ibuffer-update nil t)) | |
501 | |
502 (defun ibuffer-maybe-save-saved-filters () | |
503 (when ibuffer-save-with-custom | |
504 (if (fboundp 'customize-save-variable) | |
505 (progn | |
506 (customize-save-variable 'ibuffer-saved-filters | |
507 ibuffer-saved-filters)) | |
508 (message "Not saved permanently: Customize not available")))) | |
509 | |
510 ;;;###autoload | |
511 (defun ibuffer-save-filters (name filters) | |
512 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'. | |
513 Interactively, prompt for NAME, and use the current filters." | |
514 (interactive | |
515 (if (null ibuffer-filtering-qualifiers) | |
516 (error "No filters currently in effect") | |
517 (list | |
518 (read-from-minibuffer "Save current filters as: ") | |
519 ibuffer-filtering-qualifiers))) | |
520 (ibuffer-aif (assoc name ibuffer-saved-filters) | |
521 (setcdr it filters) | |
522 (push (list name filters) ibuffer-saved-filters)) | |
523 (ibuffer-maybe-save-saved-filters) | |
524 (ibuffer-update-mode-name)) | |
525 | |
526 ;;;###autoload | |
527 (defun ibuffer-delete-saved-filters (name) | |
528 "Delete saved filters with NAME from `ibuffer-saved-filters'." | |
529 (interactive | |
530 (list | |
531 (if (null ibuffer-saved-filters) | |
532 (error "No saved filters") | |
533 (completing-read "Delete saved filters: " | |
534 ibuffer-saved-filters nil t)))) | |
535 (setq ibuffer-saved-filters | |
536 (ibuffer-delete-alist name ibuffer-saved-filters)) | |
537 (ibuffer-maybe-save-saved-filters) | |
538 (ibuffer-update nil t)) | |
539 | |
540 ;;;###autoload | |
541 (defun ibuffer-add-saved-filters (name) | |
542 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters." | |
543 (interactive | |
544 (list | |
545 (if (null ibuffer-saved-filters) | |
546 (error "No saved filters") | |
547 (completing-read "Add saved filters: " | |
548 ibuffer-saved-filters nil t)))) | |
549 (push (cons 'saved name) ibuffer-filtering-qualifiers) | |
550 (ibuffer-update nil t)) | |
551 | |
552 ;;;###autoload | |
553 (defun ibuffer-switch-to-saved-filters (name) | |
554 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'. | |
555 If prefix argument ADD is non-nil, then add the saved filters instead | |
556 of replacing the current filters." | |
557 (interactive | |
558 (list | |
559 (if (null ibuffer-saved-filters) | |
560 (error "No saved filters") | |
561 (completing-read "Switch to saved filters: " | |
562 ibuffer-saved-filters nil t)))) | |
563 (setq ibuffer-filtering-qualifiers (list (cons 'saved name))) | |
564 (ibuffer-update nil t)) | |
565 | |
566 (defun ibuffer-format-qualifier (qualifier) | |
567 (if (eq (car-safe qualifier) 'not) | |
568 (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier)) "]") | |
569 (ibuffer-format-qualifier-1 qualifier))) | |
570 | |
571 (defun ibuffer-format-qualifier-1 (qualifier) | |
572 (case (car qualifier) | |
573 (saved | |
574 (concat " [filter: " (cdr qualifier) "]")) | |
575 (or | |
576 (concat " [OR" (mapconcat #'ibuffer-format-qualifier | |
577 (cdr qualifier) "") "]")) | |
578 (t | |
579 (let ((type (assq (car qualifier) ibuffer-filtering-alist))) | |
580 (unless qualifier | |
581 (error "Ibuffer: bad qualifier %s" qualifier)) | |
582 (concat " [" (cadr type) ": " (format "%s]" (cdr qualifier))))))) | |
583 | |
584 ;;; Extra operation definitions | |
585 | |
586 (define-ibuffer-filter mode | |
587 "Toggle current view to buffers with major mode QUALIFIER." | |
588 (:description "major mode" | |
589 :reader | |
590 (intern | |
591 (completing-read "Filter by major mode: " obarray | |
592 #'(lambda (e) | |
593 (string-match "-mode$" | |
594 (symbol-name e))) | |
595 t | |
596 (let ((buf (ibuffer-current-buffer))) | |
597 (if (and buf (buffer-live-p buf)) | |
598 (with-current-buffer buf | |
599 (symbol-name major-mode)) | |
600 ""))))) | |
601 (eq qualifier (with-current-buffer buf major-mode))) | |
602 | |
603 (define-ibuffer-filter name | |
604 "Toggle current view to buffers with name matching QUALIFIER." | |
605 (:description "buffer name" | |
606 :reader | |
607 (read-from-minibuffer "Filter by name (regexp): ")) | |
608 (string-match qualifier (buffer-name buf))) | |
609 | |
610 (define-ibuffer-filter filename | |
611 "Toggle current view to buffers with filename matching QUALIFIER." | |
612 (:description "filename" | |
613 :reader | |
614 (read-from-minibuffer "Filter by filename (regexp): ")) | |
615 (ibuffer-awhen (buffer-file-name buf) | |
616 (string-match qualifier it))) | |
617 | |
618 (define-ibuffer-filter size-gt | |
619 "Toggle current view to buffers with size greater than QUALIFIER." | |
620 (:description "size greater than" | |
621 :reader | |
622 (string-to-number (read-from-minibuffer "Filter by size greater than: "))) | |
623 (> (with-current-buffer buf (buffer-size)) | |
624 qualifier)) | |
625 | |
626 (define-ibuffer-filter size-lt | |
627 "Toggle current view to buffers with size less than QUALIFIER." | |
628 (:description "size less than" | |
629 :reader | |
630 (string-to-number (read-from-minibuffer "Filter by size less than: "))) | |
631 (< (with-current-buffer buf (buffer-size)) | |
632 qualifier)) | |
633 | |
634 (define-ibuffer-filter content | |
635 "Toggle current view to buffers whose contents match QUALIFIER." | |
636 (:description "content" | |
637 :reader | |
638 (read-from-minibuffer "Filter by content (regexp): ")) | |
639 (with-current-buffer buf | |
640 (save-excursion | |
641 (goto-char (point-min)) | |
642 (re-search-forward qualifier nil t)))) | |
643 | |
644 (define-ibuffer-filter predicate | |
645 "Toggle current view to buffers for which QUALIFIER returns non-nil." | |
646 (:description "predicate" | |
647 :reader | |
648 (read-minibuffer "Filter by predicate (form): ")) | |
649 (with-current-buffer buf | |
650 (eval qualifier))) | |
651 | |
652 ;;; Sorting | |
653 | |
654 ;;;###autoload | |
655 (defun ibuffer-toggle-sorting-mode () | |
656 "Toggle the current sorting mode. | |
43379
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
657 Default sorting modes are: |
42702 | 658 Recency - the last time the buffer was viewed |
659 Name - the name of the buffer | |
660 Major Mode - the name of the major mode of the buffer | |
661 Size - the size of the buffer" | |
662 (interactive) | |
43379
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
663 (let ((modes (mapcar 'car ibuffer-sorting-functions-alist))) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
664 (add-to-list 'modes 'recency) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
665 (setq modes (sort modes 'string-lessp)) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
666 (let ((next (or (find-if |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
667 (lambda (x) (string-lessp ibuffer-sorting-mode x)) modes) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
668 (car modes)))) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
669 (setq ibuffer-sorting-mode next) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
670 (message "Sorting by %s" next))) |
42702 | 671 (ibuffer-redisplay t)) |
672 | |
673 ;;;###autoload | |
674 (defun ibuffer-invert-sorting () | |
675 "Toggle whether or not sorting is in reverse order." | |
676 (interactive) | |
677 (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep)) | |
678 (message "Sorting order %s" | |
679 (if ibuffer-sorting-reversep | |
680 "reversed" | |
681 "normal")) | |
682 (ibuffer-redisplay t)) | |
683 | |
684 (define-ibuffer-sorter major-mode | |
685 "Sort the buffers by major modes. | |
686 Ordering is lexicographic." | |
687 (:description "major mode") | |
688 (string-lessp (downcase | |
689 (symbol-name (with-current-buffer | |
690 (car a) | |
691 major-mode))) | |
692 (downcase | |
693 (symbol-name (with-current-buffer | |
694 (car b) | |
695 major-mode))))) | |
696 | |
697 (define-ibuffer-sorter alphabetic | |
698 "Sort the buffers by their names. | |
699 Ordering is lexicographic." | |
700 (:description "buffer name") | |
701 (string-lessp | |
702 (buffer-name (car a)) | |
703 (buffer-name (car b)))) | |
704 | |
705 (define-ibuffer-sorter size | |
706 "Sort the buffers by their size." | |
707 (:description "size") | |
708 (< (with-current-buffer (car a) | |
709 (buffer-size)) | |
710 (with-current-buffer (car b) | |
711 (buffer-size)))) | |
712 | |
713 ;;; Functions to emulate bs.el | |
714 | |
715 ;;;###autoload | |
716 (defun ibuffer-bs-show () | |
717 "Emulate `bs-show' from the bs.el package." | |
718 (interactive) | |
719 (ibuffer t "*Ibuffer-bs*" '((filename . ".*")) nil t) | |
720 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all)) | |
721 | |
722 (defun ibuffer-bs-toggle-all () | |
723 "Emulate `bs-toggle-show-all' from the bs.el package." | |
724 (interactive) | |
725 (if ibuffer-filtering-qualifiers | |
726 (ibuffer-pop-filter) | |
727 (progn (ibuffer-push-filter '(filename . ".*")) | |
728 (ibuffer-update nil t)))) | |
729 | |
730 ;;; Handy functions | |
731 | |
732 ;;;###autoload | |
733 (defun ibuffer-add-to-tmp-hide (regexp) | |
734 "Add REGEXP to `ibuffer-tmp-hide-regexps'. | |
735 This means that buffers whose name matches REGEXP will not be shown | |
736 for this ibuffer session." | |
737 (interactive | |
738 (list | |
739 (read-from-minibuffer "Never show buffers matching: " | |
740 (regexp-quote (buffer-name (ibuffer-current-buffer t)))))) | |
741 (push regexp ibuffer-tmp-hide-regexps)) | |
742 | |
743 ;;;###autoload | |
744 (defun ibuffer-add-to-tmp-show (regexp) | |
745 "Add REGEXP to `ibuffer-tmp-show-regexps'. | |
746 This means that buffers whose name matches REGEXP will always be shown | |
747 for this ibuffer session." | |
748 (interactive | |
749 (list | |
750 (read-from-minibuffer "Always show buffers matching: " | |
751 (regexp-quote (buffer-name (ibuffer-current-buffer t)))))) | |
752 (push regexp ibuffer-tmp-show-regexps)) | |
753 | |
754 ;;;###autoload | |
755 (defun ibuffer-forward-next-marked (&optional count mark direction) | |
756 "Move forward by COUNT marked buffers (default 1). | |
757 | |
758 If MARK is non-nil, it should be a character denoting the type of mark | |
759 to move by. The default is `ibuffer-marked-char'. | |
760 | |
761 If DIRECTION is non-nil, it should be an integer; negative integers | |
762 mean move backwards, non-negative integers mean move forwards." | |
763 (interactive "P") | |
764 (unless count | |
765 (setq count 1)) | |
766 (unless mark | |
767 (setq mark ibuffer-marked-char)) | |
768 (unless direction | |
769 (setq direction 1)) | |
770 ;; Skip the title | |
771 (ibuffer-forward-line 0) | |
772 (let ((opos (point)) | |
773 curmark) | |
774 (ibuffer-forward-line direction) | |
775 (while (not (or (= (point) opos) | |
776 (eq (setq curmark (ibuffer-current-mark)) | |
777 mark))) | |
778 (ibuffer-forward-line direction)) | |
779 (when (and (= (point) opos) | |
780 (not (eq (ibuffer-current-mark) mark))) | |
781 (error "No buffers with mark %c" mark)))) | |
782 | |
783 ;;;###autoload | |
784 (defun ibuffer-backwards-next-marked (&optional count mark) | |
785 "Move backwards by COUNT marked buffers (default 1). | |
786 | |
787 If MARK is non-nil, it should be a character denoting the type of mark | |
788 to move by. The default is `ibuffer-marked-char'." | |
789 (interactive "P") | |
790 (ibuffer-forward-next-marked count mark -1)) | |
791 | |
792 ;;;###autoload | |
793 (defun ibuffer-do-kill-lines () | |
794 "Hide all of the currently marked lines." | |
795 (interactive) | |
796 (if (= (ibuffer-count-marked-lines) 0) | |
797 (message "No buffers marked; use 'm' to mark a buffer") | |
798 (let ((count | |
799 (ibuffer-map-marked-lines | |
800 #'(lambda (buf mark beg end) | |
801 'kill)))) | |
802 (message "Killed %s lines" count)))) | |
803 | |
804 ;;;###autoload | |
805 (defun ibuffer-jump-to-buffer (name) | |
806 "Move point to the buffer whose name is NAME." | |
807 (interactive (list nil)) | |
808 (let ((table (mapcar #'(lambda (x) | |
809 (cons (buffer-name (car x)) | |
810 (caddr x))) | |
811 (ibuffer-current-state-list t)))) | |
812 (when (null table) | |
813 (error "No buffers!")) | |
814 (when (interactive-p) | |
815 (setq name (completing-read "Jump to buffer: " table nil t))) | |
816 (ibuffer-aif (assoc name table) | |
817 (goto-char (cdr it)) | |
818 (error "No buffer with name %s" name)))) | |
819 | |
820 ;;;###autoload | |
821 (defun ibuffer-diff-with-file () | |
822 "View the differences between this buffer and its associated file. | |
823 This requires the external program \"diff\" to be in your `exec-path'." | |
824 (interactive) | |
825 (let* ((buf (ibuffer-current-buffer)) | |
826 (buf-filename (with-current-buffer buf | |
827 buffer-file-name))) | |
828 (unless (buffer-live-p buf) | |
829 (error "Buffer %s has been killed" buf)) | |
830 (unless buf-filename | |
831 (error "Buffer %s has no associated file" buf)) | |
832 (let ((diff-buf (get-buffer-create "*Ibuffer-diff*"))) | |
833 (with-current-buffer diff-buf | |
834 (setq buffer-read-only nil) | |
835 (erase-buffer)) | |
836 (let ((tempfile (make-temp-file "ibuffer-diff-"))) | |
837 (unwind-protect | |
838 (progn | |
839 (with-current-buffer buf | |
840 (write-region (point-min) (point-max) tempfile nil 'nomessage)) | |
841 (if (zerop | |
842 (apply #'call-process "diff" nil diff-buf nil | |
843 (append | |
844 (when (and (boundp 'ediff-custom-diff-options) | |
845 (stringp ediff-custom-diff-options)) | |
846 (list ediff-custom-diff-options)) | |
847 (list buf-filename tempfile)))) | |
848 (message "No differences found") | |
849 (progn | |
850 (with-current-buffer diff-buf | |
851 (goto-char (point-min)) | |
852 (if (fboundp 'diff-mode) | |
853 (diff-mode) | |
854 (fundamental-mode))) | |
855 (display-buffer diff-buf)))) | |
856 (when (file-exists-p tempfile) | |
857 (delete-file tempfile))))) | |
858 nil)) | |
859 | |
860 ;;;###autoload | |
861 (defun ibuffer-copy-filename-as-kill (&optional arg) | |
862 "Copy filenames of marked buffers into the kill ring. | |
863 The names are separated by a space. | |
864 If a buffer has no filename, it is ignored. | |
865 With a zero prefix arg, use the complete pathname of each marked file. | |
866 | |
867 You can then feed the file name(s) to other commands with C-y. | |
868 | |
869 [ This docstring shamelessly stolen from the | |
870 `dired-copy-filename-as-kill' in \"dired-x\". ]" | |
871 ;; Add to docstring later: | |
872 ;; With C-u, use the relative pathname of each marked file. | |
873 (interactive "P") | |
874 (if (= (ibuffer-count-marked-lines) 0) | |
875 (message "No buffers marked; use 'm' to mark a buffer") | |
876 (let ((ibuffer-copy-filename-as-kill-result "") | |
877 (type (cond ((eql arg 0) | |
878 'full) | |
879 ;; ((eql arg 4) | |
880 ;; 'relative) | |
881 (t | |
882 'name)))) | |
883 (ibuffer-map-marked-lines | |
884 #'(lambda (buf mark beg end) | |
885 (setq ibuffer-copy-filename-as-kill-result | |
886 (concat ibuffer-copy-filename-as-kill-result | |
887 (let ((name (buffer-file-name buf))) | |
888 (if name | |
889 (case type | |
890 (full | |
891 name) | |
892 (t | |
893 (file-name-nondirectory name))) | |
894 "")) | |
895 " ")))) | |
896 (push ibuffer-copy-filename-as-kill-result kill-ring)))) | |
897 | |
898 (defun ibuffer-mark-on-buffer (func) | |
899 (let ((count | |
900 (ibuffer-map-lines | |
901 #'(lambda (buf mark beg end) | |
902 (when (funcall func buf) | |
903 (ibuffer-set-mark-1 ibuffer-marked-char) | |
904 t))))) | |
905 (ibuffer-redisplay t) | |
906 (message "Marked %s buffers" count))) | |
907 | |
908 ;;;###autoload | |
909 (defun ibuffer-mark-by-name-regexp (regexp) | |
910 "Mark all buffers whose name matches REGEXP." | |
911 (interactive "sMark by name (regexp): ") | |
912 (ibuffer-mark-on-buffer | |
913 #'(lambda (buf) | |
914 (string-match regexp (buffer-name buf))))) | |
915 | |
916 ;;;###autoload | |
917 (defun ibuffer-mark-by-mode-regexp (regexp) | |
918 "Mark all buffers whose major mode matches REGEXP." | |
919 (interactive "sMark by major mode (regexp): ") | |
920 (ibuffer-mark-on-buffer | |
921 #'(lambda (buf) | |
922 (with-current-buffer buf | |
923 (string-match regexp mode-name))))) | |
924 | |
925 ;;;###autoload | |
926 (defun ibuffer-mark-by-file-name-regexp (regexp) | |
927 "Mark all buffers whose file name matches REGEXP." | |
928 (interactive "sMark by file name (regexp): ") | |
929 (ibuffer-mark-on-buffer | |
930 #'(lambda (buf) | |
931 (let ((name (or (buffer-file-name buf) | |
932 (with-current-buffer buf | |
933 (and | |
934 (boundp 'dired-directory) | |
935 (stringp dired-directory) | |
936 dired-directory))))) | |
937 (when name | |
938 (string-match regexp name)))))) | |
939 | |
940 ;;;###autoload | |
941 (defun ibuffer-mark-by-mode (mode) | |
942 "Mark all buffers whose major mode equals MODE." | |
943 (interactive | |
944 (list (intern (completing-read "Mark by major mode: " obarray | |
945 #'(lambda (e) | |
946 ;; kind of a hack... | |
947 (and (fboundp e) | |
948 (string-match "-mode$" | |
949 (symbol-name e)))) | |
950 t | |
951 (let ((buf (ibuffer-current-buffer))) | |
952 (if (and buf (buffer-live-p buf)) | |
953 (with-current-buffer buf | |
954 (cons (symbol-name major-mode) | |
955 0)) | |
956 "")))))) | |
957 (ibuffer-mark-on-buffer | |
958 #'(lambda (buf) | |
959 (with-current-buffer buf | |
960 (eq major-mode mode))))) | |
961 | |
962 ;;;###autoload | |
963 (defun ibuffer-mark-modified-buffers () | |
964 "Mark all modified buffers." | |
965 (interactive) | |
966 (ibuffer-mark-on-buffer | |
967 #'(lambda (buf) (buffer-modified-p buf)))) | |
968 | |
969 ;;;###autoload | |
970 (defun ibuffer-mark-unsaved-buffers () | |
971 "Mark all modified buffers that have an associated file." | |
972 (interactive) | |
973 (ibuffer-mark-on-buffer | |
974 #'(lambda (buf) (and (with-current-buffer buf buffer-file-name) | |
975 (buffer-modified-p buf))))) | |
976 | |
977 ;;;###autoload | |
978 (defun ibuffer-mark-dissociated-buffers () | |
979 "Mark all buffers whose associated file does not exist." | |
980 (interactive) | |
981 (ibuffer-mark-on-buffer | |
982 #'(lambda (buf) | |
983 (with-current-buffer buf | |
984 (or | |
985 (and buffer-file-name | |
986 (not (file-exists-p buffer-file-name))) | |
987 (and (eq major-mode 'dired-mode) | |
988 (boundp 'dired-directory) | |
989 (stringp dired-directory) | |
990 (not (file-exists-p (file-name-directory dired-directory))))))))) | |
991 | |
992 ;;;###autoload | |
993 (defun ibuffer-mark-help-buffers () | |
994 "Mark buffers like *Help*, *Apropos*, *Info*." | |
995 (interactive) | |
996 (ibuffer-mark-on-buffer | |
997 #'(lambda (buf) | |
998 (with-current-buffer buf | |
999 (or | |
1000 (eq major-mode 'apropos-mode) | |
1001 (eq major-mode 'help-mode) | |
1002 (eq major-mode 'info-mode)))))) | |
1003 | |
1004 ;;;###autoload | |
1005 (defun ibuffer-mark-old-buffers () | |
1006 "Mark buffers which have not been viewed in `ibuffer-old-time' days." | |
1007 (interactive) | |
1008 (ibuffer-mark-on-buffer | |
1009 #'(lambda (buf) | |
1010 (with-current-buffer buf | |
1011 ;; hacked from midnight.el | |
1012 (when buffer-display-time | |
1013 (let* ((tm (current-time)) | |
1014 (now (+ (* (float (ash 1 16)) (car tm)) | |
1015 (float (cadr tm)) (* 0.0000001 (caddr tm)))) | |
1016 (then (+ (* (float (ash 1 16)) | |
1017 (car buffer-display-time)) | |
1018 (float (cadr buffer-display-time)) | |
1019 (* 0.0000001 (caddr buffer-display-time))))) | |
1020 (> (- now then) (* 24 60 60 ibuffer-old-time)))))))) | |
1021 | |
1022 ;;;###autoload | |
1023 (defun ibuffer-mark-special-buffers () | |
1024 "Mark all buffers whose name begins and ends with '*'." | |
1025 (interactive) | |
1026 (ibuffer-mark-on-buffer | |
1027 #'(lambda (buf) (string-match "^\\*.+\\*$" | |
1028 (buffer-name buf))))) | |
1029 | |
1030 ;;;###autoload | |
1031 (defun ibuffer-mark-read-only-buffers () | |
1032 "Mark all read-only buffers." | |
1033 (interactive) | |
1034 (ibuffer-mark-on-buffer | |
1035 #'(lambda (buf) | |
1036 (with-current-buffer buf | |
1037 buffer-read-only)))) | |
1038 | |
1039 ;;;###autoload | |
1040 (defun ibuffer-mark-dired-buffers () | |
1041 "Mark all `dired' buffers." | |
1042 (interactive) | |
1043 (ibuffer-mark-on-buffer | |
1044 #'(lambda (buf) | |
1045 (with-current-buffer buf | |
1046 (eq major-mode 'dired-mode))))) | |
1047 | |
1048 ;;; An implementation of multi-buffer `occur' | |
1049 | |
1050 (defvar ibuffer-occur-props nil) | |
1051 | |
1052 (define-derived-mode ibuffer-occur-mode occur-mode "Ibuffer-Occur" | |
1053 "A special form of Occur mode for multiple buffers. | |
1054 Note this major mode is not meant for interactive use! | |
1055 See also `occur-mode'." | |
1056 (define-key ibuffer-occur-mode-map (kbd "n") 'forward-line) | |
1057 (define-key ibuffer-occur-mode-map (kbd "q") 'bury-buffer) | |
1058 (define-key ibuffer-occur-mode-map (kbd "p") 'previous-line) | |
1059 (define-key ibuffer-occur-mode-map (kbd "RET") 'ibuffer-occur-display-occurence) | |
1060 (define-key ibuffer-occur-mode-map (kbd "f") 'ibuffer-occur-goto-occurence) | |
1061 (define-key ibuffer-occur-mode-map [(mouse-2)] 'ibuffer-occur-mouse-display-occurence) | |
1062 (set (make-local-variable 'revert-buffer-function) | |
1063 #'ibuffer-occur-revert-buffer-function) | |
1064 (set (make-local-variable 'ibuffer-occur-props) nil) | |
1065 (setq buffer-read-only nil) | |
1066 (erase-buffer) | |
1067 (setq buffer-read-only t) | |
1068 (message (concat | |
1069 "Use RET " | |
1070 (if (or (and (< 21 emacs-major-version) | |
1071 window-system) | |
1072 (featurep 'mouse)) | |
1073 "or mouse-2 ") | |
1074 "to display an occurence."))) | |
1075 | |
1076 (defun ibuffer-occur-mouse-display-occurence (e) | |
1077 "Display occurence on this line in another window." | |
1078 (interactive "e") | |
1079 (let* ((occurbuf (save-window-excursion (mouse-select-window e) | |
1080 (selected-window))) | |
1081 (target (with-current-buffer occurbuf | |
1082 (get-text-property (save-excursion | |
1083 (mouse-set-point e) | |
1084 (point)) | |
1085 'ibuffer-occur-target)))) | |
1086 (unless target | |
1087 (error "No occurence on this line")) | |
1088 (let ((buf (car target)) | |
1089 (line (cdr target))) | |
1090 (switch-to-buffer occurbuf) | |
1091 (delete-other-windows) | |
1092 (pop-to-buffer buf) | |
1093 (goto-line line)))) | |
1094 | |
1095 (defun ibuffer-occur-goto-occurence () | |
1096 "Switch to the buffer which has the occurence on this line." | |
1097 (interactive) | |
1098 (ibuffer-occur-display-occurence t)) | |
1099 | |
1100 (defun ibuffer-occur-display-occurence (&optional goto) | |
1101 "Display occurence on this line in another window." | |
1102 (interactive "P") | |
1103 (let ((target (get-text-property (point) 'ibuffer-occur-target))) | |
1104 (unless target | |
1105 (error "No occurence on this line")) | |
1106 (let ((buf (car target)) | |
1107 (line (cdr target))) | |
1108 (delete-other-windows) | |
1109 (if goto | |
1110 (switch-to-buffer buf) | |
1111 (pop-to-buffer buf)) | |
1112 (goto-line line)))) | |
1113 | |
1114 ;;;###autoload | |
1115 (defun ibuffer-do-occur (regexp &optional nlines) | |
1116 "View lines which match REGEXP in all marked buffers. | |
1117 Optional argument NLINES says how many lines of context to display: it | |
1118 defaults to one." | |
1119 (interactive | |
1120 (list (let* ((default (car regexp-history)) | |
1121 (input | |
1122 (read-from-minibuffer | |
1123 (if default | |
1124 (format "List lines matching regexp (default `%s'): " | |
1125 default) | |
1126 "List lines matching regexp: ") | |
1127 nil | |
1128 nil | |
1129 nil | |
1130 'regexp-history))) | |
1131 (if (equal input "") | |
1132 default | |
1133 input)) | |
1134 current-prefix-arg)) | |
1135 (if (or (not (integerp nlines)) | |
1136 (< nlines 0)) | |
1137 (setq nlines 1)) | |
1138 (when (zerop (ibuffer-count-marked-lines)) | |
1139 (ibuffer-set-mark 'ibuffer-marked-char)) | |
1140 (let ((ibuffer-do-occur-bufs nil)) | |
1141 ;; Accumulate a list of marked buffers | |
1142 (ibuffer-map-marked-lines | |
1143 #'(lambda (buf mark beg end) | |
1144 (push buf ibuffer-do-occur-bufs))) | |
1145 (ibuffer-do-occur-1 regexp ibuffer-do-occur-bufs | |
1146 (get-buffer-create "*Ibuffer-occur*") | |
1147 nlines))) | |
1148 | |
1149 (defun ibuffer-do-occur-1 (regexp buffers out-buf nlines) | |
1150 (let ((count (ibuffer-occur-engine regexp buffers out-buf nlines))) | |
1151 (if (> count 0) | |
1152 (progn | |
1153 (switch-to-buffer out-buf) | |
1154 (setq buffer-read-only t) | |
1155 (delete-other-windows) | |
1156 (goto-char (point-min)) | |
1157 (message "Found %s matches in %s buffers" count (length buffers))) | |
1158 (message "No matches found")))) | |
1159 | |
1160 | |
1161 (defun ibuffer-occur-revert-buffer-function (ignore-auto noconfirm) | |
1162 "Update the *Ibuffer occur* buffer." | |
1163 (assert (eq major-mode 'ibuffer-occur-mode)) | |
1164 (ibuffer-do-occur-1 (car ibuffer-occur-props) | |
1165 (cadr ibuffer-occur-props) | |
1166 (current-buffer) | |
1167 (caddr ibuffer-occur-props))) | |
1168 | |
1169 (defun ibuffer-occur-engine (regexp buffers out-buf nlines) | |
1170 (macrolet ((insert-get-point | |
1171 (&rest args) | |
1172 `(progn | |
1173 (insert ,@args) | |
1174 (point))) | |
1175 (maybe-put-overlay | |
1176 (over prop value) | |
1177 `(when (ibuffer-use-fontification) | |
1178 (overlay-put ,over ,prop ,value))) | |
1179 (maybe-ibuffer-propertize | |
1180 (obj &rest args) | |
1181 (let ((objsym (gensym "--maybe-ibuffer-propertize-"))) | |
1182 `(let ((,objsym ,obj)) | |
1183 (if (ibuffer-use-fontification) | |
1184 (propertize ,objsym ,@args) | |
1185 ,objsym))))) | |
1186 (with-current-buffer out-buf | |
1187 (ibuffer-occur-mode) | |
1188 (setq buffer-read-only nil) | |
1189 (let ((globalcount 0)) | |
1190 ;; Map over all the buffers | |
1191 (dolist (buf buffers) | |
1192 (when (buffer-live-p buf) | |
1193 (let ((c 0) ;; count of matched lines | |
1194 (l 1) ;; line count | |
1195 (headerpt (with-current-buffer out-buf (point)))) | |
1196 (save-excursion | |
1197 (set-buffer buf) | |
1198 (save-excursion | |
1199 (goto-char (point-min)) ;; begin searching in the buffer | |
1200 (while (not (eobp)) | |
1201 ;; The line we're matching against | |
1202 (let ((curline (buffer-substring | |
1203 (line-beginning-position) | |
1204 (line-end-position)))) | |
1205 (when (string-match regexp curline) | |
1206 (incf c) ;; increment match count | |
1207 (incf globalcount) | |
1208 ;; Depropertize the string, and maybe highlight the matches | |
1209 (setq curline | |
1210 (progn | |
1211 (ibuffer-depropertize-string curline t) | |
1212 (when (ibuffer-use-fontification) | |
1213 (let ((len (length curline)) | |
1214 (start 0)) | |
1215 (while (and (< start len) | |
1216 (string-match regexp curline start)) | |
1217 (put-text-property (match-beginning 0) | |
1218 (match-end 0) | |
1219 'face ibuffer-occur-match-face | |
1220 curline) | |
1221 (setq start (match-end 0))))) | |
1222 curline)) | |
1223 ;; Generate the string to insert for this match | |
1224 (let ((data | |
1225 (if (= nlines 1) | |
1226 ;; The simple display style | |
1227 (concat (maybe-ibuffer-propertize | |
1228 (format "%-6d:" l) | |
1229 'face 'bold) | |
1230 curline | |
1231 "\n") | |
1232 ;; The complex multi-line display style | |
1233 (let ((prevlines (nreverse | |
1234 (ibuffer-accumulate-lines (- nlines)))) | |
1235 (nextlines (ibuffer-accumulate-lines nlines)) | |
1236 ;; The lack of `flet' seriously sucks. | |
1237 (fun #'(lambda (lines) | |
1238 (mapcar | |
1239 #'(lambda (line) | |
1240 (concat " :" line "\n")) | |
1241 lines)))) | |
1242 (setq prevlines (funcall fun prevlines)) | |
1243 (setq nextlines (funcall fun nextlines)) | |
1244 ;; Yes, I am trying to win the award for the | |
1245 ;; most consing. | |
1246 (apply #'concat | |
1247 (nconc | |
1248 prevlines | |
1249 (list | |
1250 (concat | |
1251 (maybe-ibuffer-propertize | |
1252 (format "%-6d" l) | |
1253 'face 'bold) | |
1254 ":" | |
1255 curline | |
1256 "\n")) | |
1257 nextlines)))))) | |
1258 ;; Actually insert the match display data | |
1259 (with-current-buffer out-buf | |
1260 (let ((beg (point)) | |
1261 (end (insert-get-point | |
1262 data))) | |
1263 (unless (= nlines 1) | |
1264 (insert "-------\n")) | |
1265 (put-text-property | |
1266 beg (1- end) 'ibuffer-occur-target (cons buf l)) | |
1267 (put-text-property | |
1268 beg (1- end) 'mouse-face 'highlight)))))) | |
1269 ;; On to the next line... | |
1270 (incf l) | |
1271 (forward-line 1)))) | |
1272 (when (not (zerop c)) ;; is the count zero? | |
1273 (with-current-buffer out-buf | |
1274 (goto-char headerpt) | |
1275 (let ((beg (point)) | |
1276 (end (insert-get-point | |
1277 (format "%d lines matching \"%s\" in buffer %s\n" | |
1278 c regexp (buffer-name buf))))) | |
1279 (let ((o (make-overlay beg end))) | |
1280 (maybe-put-overlay o 'face 'underline))) | |
1281 (goto-char (point-max))))))) | |
1282 (setq ibuffer-occur-props (list regexp buffers nlines)) | |
1283 ;; Return the number of matches | |
1284 globalcount)))) | |
1285 | |
1286 (provide 'ibuf-ext) | |
1287 | |
1288 ;;; ibuf-ext.el ends here |