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