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