ibuffer-decompose-filter: Avoid side effects on error
[emacs.git] / lisp / ibuf-ext.el
blobcc2942c911902557363911e688f101a4baf6f422
1 ;;; ibuf-ext.el --- extensions for ibuffer -*- lexical-binding:t -*-
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
5 ;; Author: Colin Walters <walters@verbum.org>
6 ;; Maintainer: John Paul Wallington <jpw@gnu.org>
7 ;; Created: 2 Dec 2001
8 ;; Keywords: buffer, convenience
9 ;; Package: ibuffer
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; These functions should be automatically loaded when called, but you
29 ;; can explicitly (require 'ibuf-ext) in your ~/.emacs to have them
30 ;; preloaded.
32 ;;; Code:
34 (require 'ibuffer)
36 (eval-when-compile
37 (require 'ibuf-macs)
38 (require 'cl-lib)
39 (require 'subr-x))
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))
49 ;; borrowed from Gnus
50 (defun ibuffer-remove-duplicates (list)
51 "Return a copy of LIST with duplicate elements removed."
52 (let ((new nil)
53 (tail list))
54 (while tail
55 (or (member (car tail) new)
56 (setq new (cons (car tail) new)))
57 (setq tail (cdr tail)))
58 (nreverse new)))
60 (defun ibuffer-split-list (ibuffer-split-list-fn ibuffer-split-list-elts)
61 (let ((hip-crowd nil)
62 (lamers nil))
63 (dolist (ibuffer-split-list-elt ibuffer-split-list-elts)
64 (if (funcall ibuffer-split-list-fn ibuffer-split-list-elt)
65 (push ibuffer-split-list-elt hip-crowd)
66 (push ibuffer-split-list-elt lamers)))
67 ;; Too bad Emacs Lisp doesn't have multiple values.
68 (list (nreverse hip-crowd) (nreverse lamers))))
70 (defcustom ibuffer-never-show-predicates nil
71 "A list of predicates (a regexp or function) for buffers not to display.
72 If a regexp, then it will be matched against the buffer's name.
73 If a function, it will be called with the buffer as an argument, and
74 should return non-nil if this buffer should not be shown."
75 :type '(repeat (choice regexp function))
76 :require 'ibuf-ext
77 :group 'ibuffer)
79 (defcustom ibuffer-always-show-predicates nil
80 "A list of predicates (a regexp or function) for buffers to always display.
81 If a regexp, then it will be matched against the buffer's name.
82 If a function, it will be called with the buffer as an argument, and
83 should return non-nil if this buffer should be shown.
84 Note that buffers matching one of these predicates will be shown
85 regardless of any active filters in this buffer."
86 :type '(repeat (choice regexp function))
87 :group 'ibuffer)
89 (defcustom ibuffer-never-search-content-name
90 (let* ((names '("Completions" "Help" "Messages" "Pp Eval Output"
91 "CompileLog" "Info" "Buffer List" "Ibuffer" "Apropos"))
92 (partial '("Customize Option: " "Async Shell Command\\*"
93 "Shell Command Output\\*" "ediff "))
94 (beg "\\`\\*")
95 (end "\\*\\'")
96 (excluded (mapcar (lambda (x)
97 (format "%s%s" beg x)) partial)))
98 (dolist (str names (nreverse excluded))
99 (push (format "%s%s%s" beg str end) excluded)))
100 "A list of regexps for buffers ignored by `ibuffer-mark-by-content-regexp'.
101 Buffers whose name matches a regexp in this list, are not searched."
102 :version "26.1"
103 :type '(repeat regexp)
104 :require 'ibuf-ext
105 :group 'ibuffer)
107 (defcustom ibuffer-never-search-content-mode '(dired-mode)
108 "A list of major modes ignored by `ibuffer-mark-by-content-regexp'.
109 Buffers whose major mode is in this list, are not searched."
110 :version "26.1"
111 :type '(repeat regexp)
112 :require 'ibuf-ext
113 :group 'ibuffer)
115 (defvar ibuffer-tmp-hide-regexps nil
116 "A list of regexps which should match buffer names to not show.")
118 (defvar ibuffer-tmp-show-regexps nil
119 "A list of regexps which should match buffer names to always show.")
121 (defvar ibuffer-auto-buffers-changed nil)
123 (defun ibuffer-update-saved-filters-format (filters)
124 "Transforms alist from old to new `ibuffer-saved-filters' format.
126 Specifically, converts old-format alist with values of the
127 form (STRING (FILTER-SPECS...)) to alist with values of the
128 form (STRING FILTER-SPECS...), where each filter spec should be a
129 cons cell with a symbol in the car. Any elements in the latter
130 form are kept as is.
132 Returns (OLD-FORMAT-DETECTED . UPDATED-SAVED-FILTERS-LIST)."
133 (when filters
134 (let* ((old-format-detected nil)
135 (fix-filter (lambda (filter-spec)
136 (if (symbolp (car (cadr filter-spec)))
137 filter-spec
138 (setq old-format-detected t) ; side-effect
139 (cons (car filter-spec) (cadr filter-spec)))))
140 (fixed (mapcar fix-filter filters)))
141 (cons old-format-detected fixed))))
143 (defcustom ibuffer-saved-filters '(("gnus"
144 (or (mode . message-mode)
145 (mode . mail-mode)
146 (mode . gnus-group-mode)
147 (mode . gnus-summary-mode)
148 (mode . gnus-article-mode)))
149 ("programming"
150 (or (mode . emacs-lisp-mode)
151 (mode . cperl-mode)
152 (mode . c-mode)
153 (mode . java-mode)
154 (mode . idl-mode)
155 (mode . lisp-mode))))
157 "An alist mapping saved filter names to filter specifications.
159 Each element should look like (\"NAME\" . FILTER-LIST), where
160 FILTER-LIST has the same structure as the variable
161 `ibuffer-filtering-qualifiers', which see. The filters defined
162 here are joined with an implicit logical `and' and associated
163 with NAME. The combined specification can be used by name in
164 other filter specifications via the `saved' qualifier (again, see
165 `ibuffer-filtering-qualifiers'). They can also be switched to by
166 name (see the functions `ibuffer-switch-to-saved-filters' and
167 `ibuffer-save-filters'). The variable `ibuffer-save-with-custom'
168 affects how this information is saved for future sessions. This
169 variable can be set directly from lisp code."
170 :version "26.1"
171 :type '(alist :key-type (string :tag "Filter name")
172 :value-type (repeat :tag "Filter specification" sexp))
173 :set (lambda (symbol value)
174 ;; Just set-default but update legacy old-style format
175 (set-default symbol (cdr (ibuffer-update-saved-filters-format value))))
176 :group 'ibuffer)
178 (defvar ibuffer-old-saved-filters-warning
179 (concat "Deprecated format detected for variable `ibuffer-saved-filters'.
181 The format has been repaired and the variable modified accordingly.
182 You can save the current value through the customize system by
183 either clicking or hitting return "
184 (make-text-button
185 "here" nil
186 'face '(:weight bold :inherit button)
187 'mouse-face '(:weight normal :background "gray50" :inherit button)
188 'follow-link t
189 'help-echo "Click or RET: save new value in customize"
190 'action (lambda (_)
191 (if (not (fboundp 'customize-save-variable))
192 (message "Customize not available; value not saved")
193 (customize-save-variable 'ibuffer-saved-filters
194 ibuffer-saved-filters)
195 (message "Saved updated ibuffer-saved-filters."))))
196 ". See below for
197 an explanation and alternative ways to save the repaired value.
199 Explanation: For the list variable `ibuffer-saved-filters',
200 elements of the form (STRING (FILTER-SPECS...)) are deprecated
201 and should instead have the form (STRING FILTER-SPECS...), where
202 each filter spec is a cons cell with a symbol in the car. See
203 `ibuffer-saved-filters' for details. The repaired value fixes
204 this format without changing the meaning of the saved filters.
206 Alternative ways to save the repaired value:
208 1. Do M-x customize-variable and entering `ibuffer-saved-filters'
209 when prompted.
211 2. Set the updated value manually by copying the
212 following emacs-lisp form to your emacs init file.
217 (defvar ibuffer-filtering-qualifiers nil
218 "A list like (SYMBOL . QUALIFIER) which filters the current buffer list.
219 See also `ibuffer-filtering-alist'.")
221 ;; This is now frobbed by `define-ibuffer-filter'.
222 (defvar ibuffer-filtering-alist nil
223 "An alist of (SYMBOL DESCRIPTION FUNCTION) which describes a filter.
225 You most likely do not want to modify this variable directly; see
226 `define-ibuffer-filter'.
228 SYMBOL is the symbolic name of the filter. DESCRIPTION is used when
229 displaying information to the user. FUNCTION is given a buffer and
230 the value of the qualifier, and returns non-nil if and only if the
231 buffer should be displayed.")
233 (defcustom ibuffer-filter-format-alist nil
234 "An alist which has special formats used when a filter is active.
235 The contents of this variable should look like:
236 ((FILTER (FORMAT FORMAT ...)) (FILTER (FORMAT FORMAT ...)) ...)
238 For example, suppose that when you add a filter for buffers whose
239 major mode is `emacs-lisp-mode', you only want to see the mark and the
240 name of the buffer. You could accomplish that by adding:
241 (mode ((mark \" \" name)))
242 to this variable."
243 :type '(repeat (list :tag "Association" (symbol :tag "Filter")
244 (list :tag "Formats" (repeat (sexp :tag "Format")))))
245 :group 'ibuffer)
247 (defvar ibuffer-cached-filter-formats nil)
248 (defvar ibuffer-compiled-filter-formats nil)
250 (defvar ibuffer-filter-groups nil
251 "A list like ((\"NAME\" ((SYMBOL . QUALIFIER) ...) ...) which groups buffers.
252 The SYMBOL should be one from `ibuffer-filtering-alist'.
253 The QUALIFIER should be the same as QUALIFIER in
254 `ibuffer-filtering-qualifiers'.")
256 (defcustom ibuffer-show-empty-filter-groups t
257 "If non-nil, then show the names of filter groups which are empty."
258 :type 'boolean
259 :group 'ibuffer)
261 (defcustom ibuffer-saved-filter-groups nil
262 "An alist of filtering groups to switch between.
264 This variable should look like ((\"STRING\" QUALIFIERS)
265 (\"STRING\" QUALIFIERS) ...), where
266 QUALIFIERS is a list of the same form as
267 `ibuffer-filtering-qualifiers'.
269 See also the variables `ibuffer-filter-groups',
270 `ibuffer-filtering-qualifiers', `ibuffer-filtering-alist', and the
271 functions `ibuffer-switch-to-saved-filter-groups',
272 `ibuffer-save-filter-groups'."
273 :type '(repeat sexp)
274 :group 'ibuffer)
276 (defvar ibuffer-hidden-filter-groups nil
277 "A list of filtering groups which are currently hidden.")
279 (defvar ibuffer-filter-group-kill-ring nil)
281 (defcustom ibuffer-old-time 72
282 "The number of hours before a buffer is considered \"old\"."
283 :type '(choice (const :tag "72 hours (3 days)" 72)
284 (const :tag "48 hours (2 days)" 48)
285 (const :tag "24 hours (1 day)" 24)
286 (integer :tag "hours"))
287 :group 'ibuffer)
289 (defcustom ibuffer-save-with-custom t
290 "If non-nil, then use Custom to save interactively changed variables.
291 Currently, this only applies to `ibuffer-saved-filters' and
292 `ibuffer-saved-filter-groups'."
293 :type 'boolean
294 :group 'ibuffer)
296 (defun ibuffer-repair-saved-filters ()
297 "Updates `ibuffer-saved-filters' to its new-style format, if needed.
299 If this list has any elements of the old-style format, a
300 deprecation warning is raised, with a button allowing persistent
301 update. Any updated filters retain their meaning in the new
302 format. See `ibuffer-update-saved-filters-format' and
303 `ibuffer-saved-filters' for details of the old and new formats."
304 (interactive)
305 (when (and (boundp 'ibuffer-saved-filters) ibuffer-saved-filters)
306 (let ((fixed (ibuffer-update-saved-filters-format ibuffer-saved-filters)))
307 (prog1
308 (setq ibuffer-saved-filters (cdr fixed))
309 (when-let (old-format-detected (car fixed))
310 (let ((warning-series t)
311 (updated-form
312 (with-output-to-string
313 (pp `(setq ibuffer-saved-filters ',ibuffer-saved-filters)))))
314 (display-warning
315 'ibuffer
316 (format ibuffer-old-saved-filters-warning updated-form))))))))
318 (defun ibuffer-ext-visible-p (buf all &optional ibuffer-buf)
320 (ibuffer-buf-matches-predicates buf ibuffer-tmp-show-regexps)
321 (and (not
323 (ibuffer-buf-matches-predicates buf ibuffer-tmp-hide-regexps)
324 (ibuffer-buf-matches-predicates buf ibuffer-never-show-predicates)))
325 (or all
326 (not
327 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates)))
328 (or ibuffer-view-ibuffer
329 (and ibuffer-buf
330 (not (eq ibuffer-buf buf))))
332 (ibuffer-included-in-filters-p buf ibuffer-filtering-qualifiers)
333 (ibuffer-buf-matches-predicates buf ibuffer-always-show-predicates)))))
335 ;;;###autoload
336 (define-minor-mode ibuffer-auto-mode
337 "Toggle use of Ibuffer's auto-update facility (Ibuffer Auto mode).
338 With a prefix argument ARG, enable Ibuffer Auto mode if ARG is
339 positive, and disable it otherwise. If called from Lisp, enable
340 the mode if ARG is omitted or nil."
341 nil nil nil
342 (unless (derived-mode-p 'ibuffer-mode)
343 (error "This buffer is not in Ibuffer mode"))
344 (cond (ibuffer-auto-mode
345 (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed) ; Initialize state vector
346 (add-hook 'post-command-hook 'ibuffer-auto-update-changed))
348 (remove-hook 'post-command-hook 'ibuffer-auto-update-changed))))
350 (defun ibuffer-auto-update-changed ()
351 (when (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed)
352 (dolist (buf (buffer-list))
353 (ignore-errors
354 (with-current-buffer buf
355 (when (and ibuffer-auto-mode
356 (derived-mode-p 'ibuffer-mode))
357 (ibuffer-update nil t)))))))
359 ;;;###autoload
360 (defun ibuffer-mouse-filter-by-mode (event)
361 "Enable or disable filtering by the major mode chosen via mouse."
362 (interactive "e")
363 (ibuffer-interactive-filter-by-mode event))
365 ;;;###autoload
366 (defun ibuffer-interactive-filter-by-mode (event-or-point)
367 "Enable or disable filtering by the major mode at point."
368 (interactive "d")
369 (if (eventp event-or-point)
370 (posn-set-point (event-end event-or-point))
371 (goto-char event-or-point))
372 (let ((buf (ibuffer-current-buffer)))
373 (if (assq 'mode ibuffer-filtering-qualifiers)
374 (setq ibuffer-filtering-qualifiers
375 (ibuffer-delete-alist 'mode ibuffer-filtering-qualifiers))
376 (ibuffer-push-filter (cons 'mode (buffer-local-value 'major-mode buf)))))
377 (ibuffer-update nil t))
379 ;;;###autoload
380 (defun ibuffer-mouse-toggle-filter-group (event)
381 "Toggle the display status of the filter group chosen with the mouse."
382 (interactive "e")
383 (ibuffer-toggle-filter-group-1 (save-excursion
384 (mouse-set-point event)
385 (point))))
387 ;;;###autoload
388 (defun ibuffer-toggle-filter-group ()
389 "Toggle the display status of the filter group on this line."
390 (interactive)
391 (ibuffer-toggle-filter-group-1 (point)))
393 (defun ibuffer-toggle-filter-group-1 (posn)
394 (let ((name (get-text-property posn 'ibuffer-filter-group-name)))
395 (unless (stringp name)
396 (error "No filtering group name present"))
397 (if (member name ibuffer-hidden-filter-groups)
398 (setq ibuffer-hidden-filter-groups
399 (delete name ibuffer-hidden-filter-groups))
400 (push name ibuffer-hidden-filter-groups))
401 (ibuffer-update nil t)))
403 ;;;###autoload
404 (defun ibuffer-forward-filter-group (&optional count)
405 "Move point forwards by COUNT filtering groups."
406 (interactive "P")
407 (unless count
408 (setq count 1))
409 (when (> count 0)
410 (when (get-text-property (point) 'ibuffer-filter-group-name)
411 (goto-char (next-single-property-change
412 (point) 'ibuffer-filter-group-name
413 nil (point-max))))
414 (goto-char (next-single-property-change
415 (point) 'ibuffer-filter-group-name
416 nil (point-max)))
417 (ibuffer-forward-filter-group (1- count)))
418 (ibuffer-forward-line 0))
420 ;;;###autoload
421 (defun ibuffer-backward-filter-group (&optional count)
422 "Move point backwards by COUNT filtering groups."
423 (interactive "P")
424 (unless count
425 (setq count 1))
426 (when (> count 0)
427 (when (get-text-property (point) 'ibuffer-filter-group-name)
428 (goto-char (previous-single-property-change
429 (point) 'ibuffer-filter-group-name
430 nil (point-min))))
431 (goto-char (previous-single-property-change
432 (point) 'ibuffer-filter-group-name
433 nil (point-min)))
434 (ibuffer-backward-filter-group (1- count)))
435 (when (= (point) (point-min))
436 (goto-char (point-max))
437 (ibuffer-backward-filter-group 1))
438 (ibuffer-forward-line 0))
440 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe "ibuf-ext")
441 (define-ibuffer-op shell-command-pipe (command)
442 "Pipe the contents of each marked buffer to shell command COMMAND."
443 (:interactive "sPipe to shell command: "
444 :opstring "Shell command executed on"
445 :modifier-p nil)
446 (shell-command-on-region
447 (point-min) (point-max) command))
449 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe-replace "ibuf-ext")
450 (define-ibuffer-op shell-command-pipe-replace (command)
451 "Replace the contents of marked buffers with output of pipe to COMMAND."
452 (:interactive "sPipe to shell command (replace): "
453 :opstring "Buffer contents replaced in"
454 :active-opstring "replace buffer contents in"
455 :dangerous t
456 :modifier-p t)
457 (with-current-buffer buf
458 (shell-command-on-region (point-min) (point-max)
459 command nil t)))
461 ;;;###autoload (autoload 'ibuffer-do-shell-command-file "ibuf-ext")
462 (define-ibuffer-op shell-command-file (command)
463 "Run shell command COMMAND separately on files of marked buffers."
464 (:interactive "sShell command on buffer's file: "
465 :opstring "Shell command executed on"
466 :modifier-p nil)
467 (shell-command (concat command " "
468 (shell-quote-argument
469 (or buffer-file-name
470 (let ((file
471 (make-temp-file
472 (substring
473 (buffer-name) 0
474 (min 10 (length (buffer-name)))))))
475 (write-region nil nil file nil 0)
476 file))))))
478 ;;;###autoload (autoload 'ibuffer-do-eval "ibuf-ext")
479 (define-ibuffer-op eval (form)
480 "Evaluate FORM in each of the buffers.
481 Does not display the buffer during evaluation. See
482 `ibuffer-do-view-and-eval' for that."
483 (:interactive
484 (list
485 (read-from-minibuffer
486 "Eval in buffers (form): "
487 nil read-expression-map t 'read-expression-history))
488 :opstring "evaluated in"
489 :modifier-p :maybe)
490 (eval form))
492 ;;;###autoload (autoload 'ibuffer-do-view-and-eval "ibuf-ext")
493 (define-ibuffer-op view-and-eval (form)
494 "Evaluate FORM while displaying each of the marked buffers.
495 To evaluate a form without viewing the buffer, see `ibuffer-do-eval'."
496 (:interactive
497 (list
498 (read-from-minibuffer
499 "Eval viewing in buffers (form): "
500 nil read-expression-map t 'read-expression-history))
501 :opstring "evaluated in"
502 :complex t
503 :modifier-p :maybe)
504 (let ((ibuffer-buf (current-buffer)))
505 (unwind-protect
506 (progn
507 (switch-to-buffer buf)
508 (eval form))
509 (switch-to-buffer ibuffer-buf))))
511 ;;;###autoload (autoload 'ibuffer-do-rename-uniquely "ibuf-ext")
512 (define-ibuffer-op rename-uniquely ()
513 "Rename marked buffers as with `rename-uniquely'."
514 (:opstring "renamed"
515 :modifier-p t)
516 (rename-uniquely))
518 ;;;###autoload (autoload 'ibuffer-do-revert "ibuf-ext")
519 (define-ibuffer-op revert ()
520 "Revert marked buffers as with `revert-buffer'."
521 (:dangerous t
522 :opstring "reverted"
523 :active-opstring "revert"
524 :modifier-p :maybe)
525 (revert-buffer t t))
527 ;;;###autoload (autoload 'ibuffer-do-isearch "ibuf-ext")
528 (define-ibuffer-op ibuffer-do-isearch ()
529 "Perform a `isearch-forward' in marked buffers."
530 (:interactive ()
531 :opstring "searched in"
532 :complex t
533 :modifier-p :maybe)
534 (multi-isearch-buffers (ibuffer-get-marked-buffers)))
536 ;;;###autoload (autoload 'ibuffer-do-isearch-regexp "ibuf-ext")
537 (define-ibuffer-op ibuffer-do-isearch-regexp ()
538 "Perform a `isearch-forward-regexp' in marked buffers."
539 (:interactive ()
540 :opstring "searched regexp in"
541 :complex t
542 :modifier-p :maybe)
543 (multi-isearch-buffers-regexp (ibuffer-get-marked-buffers)))
545 ;;;###autoload (autoload 'ibuffer-do-replace-regexp "ibuf-ext")
546 (define-ibuffer-op replace-regexp (from-str to-str)
547 "Perform a `replace-regexp' in marked buffers."
548 (:interactive
549 (let* ((from-str (read-from-minibuffer "Replace regexp: "))
550 (to-str (read-from-minibuffer (concat "Replace " from-str
551 " with: "))))
552 (list from-str to-str))
553 :opstring "replaced in"
554 :complex t
555 :modifier-p :maybe)
556 (save-window-excursion
557 (switch-to-buffer buf)
558 (save-excursion
559 (goto-char (point-min))
560 (let ((case-fold-search ibuffer-case-fold-search))
561 (while (re-search-forward from-str nil t)
562 (replace-match to-str))))
565 ;;;###autoload (autoload 'ibuffer-do-query-replace "ibuf-ext")
566 (define-ibuffer-op query-replace (&rest args)
567 "Perform a `query-replace' in marked buffers."
568 (:interactive
569 (query-replace-read-args "Query replace" t t)
570 :opstring "replaced in"
571 :complex t
572 :modifier-p :maybe)
573 (save-window-excursion
574 (switch-to-buffer buf)
575 (save-excursion
576 (let ((case-fold-search ibuffer-case-fold-search))
577 (goto-char (point-min))
578 (apply #'query-replace args)))
581 ;;;###autoload (autoload 'ibuffer-do-query-replace-regexp "ibuf-ext")
582 (define-ibuffer-op query-replace-regexp (&rest args)
583 "Perform a `query-replace-regexp' in marked buffers."
584 (:interactive
585 (query-replace-read-args "Query replace regexp" t t)
586 :opstring "replaced in"
587 :complex t
588 :modifier-p :maybe)
589 (save-window-excursion
590 (switch-to-buffer buf)
591 (save-excursion
592 (let ((case-fold-search ibuffer-case-fold-search))
593 (goto-char (point-min))
594 (apply #'query-replace-regexp args)))
597 ;;;###autoload (autoload 'ibuffer-do-print "ibuf-ext")
598 (define-ibuffer-op print ()
599 "Print marked buffers as with `print-buffer'."
600 (:opstring "printed"
601 :modifier-p nil)
602 (print-buffer))
604 ;;;###autoload
605 (defun ibuffer-included-in-filters-p (buf filters)
606 (not
607 (memq nil ;; a filter will return nil if it failed
608 (mapcar
609 ;; filter should be like (TYPE . QUALIFIER), or
610 ;; (or (TYPE . QUALIFIER) (TYPE . QUALIFIER) ...)
611 #'(lambda (qual)
612 (ibuffer-included-in-filter-p buf qual))
613 filters))))
615 (defun ibuffer-included-in-filter-p (buf filter)
616 (if (eq (car filter) 'not)
617 (not (ibuffer-included-in-filter-p-1 buf (cdr filter)))
618 (ibuffer-included-in-filter-p-1 buf filter)))
620 (defun ibuffer-included-in-filter-p-1 (buf filter)
621 (not
622 (not
623 (pcase (car filter)
624 (`or
625 (memq t (mapcar #'(lambda (x)
626 (ibuffer-included-in-filter-p buf x))
627 (cdr filter))))
628 (`saved
629 (let ((data (assoc (cdr filter) ibuffer-saved-filters)))
630 (unless data
631 (ibuffer-filter-disable t)
632 (error "Unknown saved filter %s" (cdr filter)))
633 (ibuffer-included-in-filters-p buf (cdr data))))
635 (pcase-let ((`(,_type ,_desc ,func)
636 (assq (car filter) ibuffer-filtering-alist)))
637 (unless func
638 (ibuffer-filter-disable t)
639 (error "Undefined filter %s" (car filter)))
640 (funcall func buf (cdr filter))))))))
642 (defun ibuffer-generate-filter-groups (bmarklist &optional noempty nodefault)
643 (let ((filter-group-alist (if nodefault
644 ibuffer-filter-groups
645 (append ibuffer-filter-groups
646 (list (cons "Default" nil))))))
647 ;; (dolist (hidden ibuffer-hidden-filter-groups)
648 ;; (setq filter-group-alist (ibuffer-delete-alist
649 ;; hidden filter-group-alist)))
650 (let ((vec (make-vector (length filter-group-alist) nil))
651 (i 0))
652 (dolist (filtergroup filter-group-alist)
653 (let ((filterset (cdr filtergroup)))
654 (cl-multiple-value-bind (hip-crowd lamers)
655 (cl-values-list
656 (ibuffer-split-list (lambda (bufmark)
657 (ibuffer-included-in-filters-p (car bufmark)
658 filterset))
659 bmarklist))
660 (aset vec i hip-crowd)
661 (cl-incf i)
662 (setq bmarklist lamers))))
663 (let (ret)
664 (dotimes (j i)
665 (let ((bufs (aref vec j)))
666 (unless (and noempty (null bufs))
667 (push (cons (car (nth j filter-group-alist))
668 bufs)
669 ret))))
670 ret))))
672 ;;;###autoload
673 (defun ibuffer-filters-to-filter-group (name)
674 "Make the current filters into a filtering group."
675 (interactive "sName for filtering group: ")
676 (when (null ibuffer-filtering-qualifiers)
677 (error "No filters in effect"))
678 (push (cons name ibuffer-filtering-qualifiers) ibuffer-filter-groups)
679 (ibuffer-filter-disable))
681 ;;;###autoload
682 (defun ibuffer-set-filter-groups-by-mode ()
683 "Set the current filter groups to filter by mode."
684 (interactive)
685 (setq ibuffer-filter-groups
686 (mapcar (lambda (mode)
687 (cons (format "%s" mode) `((mode . ,mode))))
688 (let ((modes
689 (ibuffer-remove-duplicates
690 (mapcar (lambda (buf)
691 (buffer-local-value 'major-mode buf))
692 (buffer-list)))))
693 (if ibuffer-view-ibuffer
694 modes
695 (delq 'ibuffer-mode modes)))))
696 (ibuffer-update nil t))
698 ;;;###autoload
699 (defun ibuffer-pop-filter-group ()
700 "Remove the first filter group."
701 (interactive)
702 (when (null ibuffer-filter-groups)
703 (error "No filter groups active"))
704 (setq ibuffer-hidden-filter-groups
705 (delete (pop ibuffer-filter-groups)
706 ibuffer-hidden-filter-groups))
707 (ibuffer-update nil t))
709 (defun ibuffer-read-filter-group-name (msg &optional nodefault noerror)
710 (when (and (not noerror) (null ibuffer-filter-groups))
711 (error "No filter groups active"))
712 ;; `ibuffer-generate-filter-groups' returns all non-hidden filter
713 ;; groups, possibly excluding empty groups or Default.
714 ;; We add `ibuffer-hidden-filter-groups' to the list, excluding
715 ;; Default if necessary.
716 (completing-read msg (nconc
717 (ibuffer-generate-filter-groups
718 (ibuffer-current-state-list)
719 (not ibuffer-show-empty-filter-groups)
720 nodefault)
721 (if nodefault
722 (remove "Default" ibuffer-hidden-filter-groups)
723 ibuffer-hidden-filter-groups))
724 nil t))
726 ;;;###autoload
727 (defun ibuffer-decompose-filter-group (group)
728 "Decompose the filter group GROUP into active filters."
729 (interactive
730 (list (ibuffer-read-filter-group-name "Decompose filter group: " t)))
731 (let ((data (cdr (assoc group ibuffer-filter-groups))))
732 (setq ibuffer-filter-groups (ibuffer-delete-alist
733 group ibuffer-filter-groups)
734 ibuffer-filtering-qualifiers data))
735 (ibuffer-update nil t))
737 ;;;###autoload
738 (defun ibuffer-clear-filter-groups ()
739 "Remove all filter groups."
740 (interactive)
741 (setq ibuffer-filter-groups nil
742 ibuffer-hidden-filter-groups nil)
743 (ibuffer-update nil t))
745 (defun ibuffer-current-filter-groups-with-position ()
746 (save-excursion
747 (goto-char (point-min))
748 (let ((pos nil)
749 (result nil))
750 (while (and (not (eobp))
751 (setq pos (next-single-property-change
752 (point) 'ibuffer-filter-group-name)))
753 (goto-char pos)
754 (push (cons (get-text-property (point) 'ibuffer-filter-group-name)
755 pos)
756 result)
757 (goto-char (next-single-property-change
758 pos 'ibuffer-filter-group-name)))
759 (nreverse result))))
761 ;;;###autoload
762 (defun ibuffer-jump-to-filter-group (name)
763 "Move point to the filter group whose name is NAME."
764 (interactive
765 (list (ibuffer-read-filter-group-name "Jump to filter group: ")))
766 (ibuffer-aif (assoc name (ibuffer-current-filter-groups-with-position))
767 (goto-char (cdr it))
768 (error "No filter group with name %s" name)))
770 ;;;###autoload
771 (defun ibuffer-kill-filter-group (name)
772 "Kill the filter group named NAME.
773 The group will be added to `ibuffer-filter-group-kill-ring'."
774 (interactive (list (ibuffer-read-filter-group-name "Kill filter group: " t)))
775 (when (equal name "Default")
776 (error "Can't kill default filter group"))
777 (ibuffer-aif (assoc name ibuffer-filter-groups)
778 (progn
779 (push (copy-tree it) ibuffer-filter-group-kill-ring)
780 (setq ibuffer-filter-groups (ibuffer-delete-alist
781 name ibuffer-filter-groups))
782 (setq ibuffer-hidden-filter-groups
783 (delete name ibuffer-hidden-filter-groups)))
784 (error "No filter group with name \"%s\"" name))
785 (ibuffer-update nil t))
787 ;;;###autoload
788 (defun ibuffer-kill-line (&optional arg interactive-p)
789 "Kill the filter group at point.
790 See also `ibuffer-kill-filter-group'."
791 (interactive "P\np")
792 (ibuffer-aif (save-excursion
793 (ibuffer-forward-line 0)
794 (get-text-property (point) 'ibuffer-filter-group-name))
795 (progn
796 (ibuffer-kill-filter-group it))
797 (funcall (if interactive-p #'call-interactively #'funcall)
798 #'kill-line arg)))
800 (defun ibuffer-insert-filter-group-before (newgroup group)
801 (let* ((found nil)
802 (pos (let ((groups (mapcar #'car ibuffer-filter-groups))
803 (res 0))
804 (while groups
805 (if (equal (car groups) group)
806 (setq found t
807 groups nil)
808 (cl-incf res)
809 (setq groups (cdr groups))))
810 res)))
811 (cond ((not found)
812 (setq ibuffer-filter-groups
813 (nconc ibuffer-filter-groups (list newgroup))))
814 ((zerop pos)
815 (push newgroup ibuffer-filter-groups))
817 (let ((cell (nthcdr pos ibuffer-filter-groups)))
818 (setf (cdr cell) (cons (car cell) (cdr cell)))
819 (setf (car cell) newgroup))))))
821 ;;;###autoload
822 (defun ibuffer-yank ()
823 "Yank the last killed filter group before group at point."
824 (interactive)
825 (ibuffer-yank-filter-group
826 (or (get-text-property (point) 'ibuffer-filter-group-name)
827 (get-text-property (point) 'ibuffer-filter-group)
828 (error "No filter group at point"))))
830 ;;;###autoload
831 (defun ibuffer-yank-filter-group (name)
832 "Yank the last killed filter group before group named NAME."
833 (interactive (list (ibuffer-read-filter-group-name
834 "Yank filter group before group: ")))
835 (unless ibuffer-filter-group-kill-ring
836 (error "The Ibuffer filter group kill-ring is empty"))
837 (save-excursion
838 (ibuffer-forward-line 0)
839 (ibuffer-insert-filter-group-before (pop ibuffer-filter-group-kill-ring)
840 name))
841 (ibuffer-update nil t))
843 ;;;###autoload
844 (defun ibuffer-save-filter-groups (name groups)
845 "Save all active filter groups GROUPS as NAME.
846 They are added to `ibuffer-saved-filter-groups'. Interactively,
847 prompt for NAME, and use the current filters."
848 (interactive
849 (if (null ibuffer-filter-groups)
850 (error "No filter groups active")
851 (list
852 (read-from-minibuffer "Save current filter groups as: ")
853 ibuffer-filter-groups)))
854 (ibuffer-aif (assoc name ibuffer-saved-filter-groups)
855 (setcdr it groups)
856 (push (cons name groups) ibuffer-saved-filter-groups))
857 (ibuffer-maybe-save-stuff))
859 ;;;###autoload
860 (defun ibuffer-delete-saved-filter-groups (name)
861 "Delete saved filter groups with NAME.
862 They are removed from `ibuffer-saved-filter-groups'."
863 (interactive
864 (list
865 (if (null ibuffer-saved-filter-groups)
866 (error "No saved filter groups")
867 (completing-read "Delete saved filter group: "
868 ibuffer-saved-filter-groups nil t))))
869 (setq ibuffer-saved-filter-groups
870 (ibuffer-delete-alist name ibuffer-saved-filter-groups))
871 (ibuffer-maybe-save-stuff)
872 (ibuffer-update nil t))
874 ;;;###autoload
875 (defun ibuffer-switch-to-saved-filter-groups (name)
876 "Set this buffer's filter groups to saved version with NAME.
877 The value from `ibuffer-saved-filter-groups' is used."
878 (interactive
879 (list
880 (cond ((null ibuffer-saved-filter-groups)
881 (error "No saved filters"))
882 ;; `ibuffer-saved-filter-groups' is a user variable that defaults
883 ;; to nil. We assume that with one element in this list the user
884 ;; knows what she wants. See bug#12331.
885 ((null (cdr ibuffer-saved-filter-groups))
886 (caar ibuffer-saved-filter-groups))
888 (completing-read "Switch to saved filter group: "
889 ibuffer-saved-filter-groups nil t)))))
890 (setq ibuffer-filter-groups (cdr (assoc name ibuffer-saved-filter-groups))
891 ibuffer-hidden-filter-groups nil)
892 (ibuffer-update nil t))
894 ;;;###autoload
895 (defun ibuffer-filter-disable (&optional delete-filter-groups)
896 "Disable all filters currently in effect in this buffer.
897 With optional arg DELETE-FILTER-GROUPS non-nil, delete all filter
898 group definitions by setting `ibuffer-filter-groups' to nil."
899 (interactive)
900 (setq ibuffer-filtering-qualifiers nil)
901 (if delete-filter-groups
902 (setq ibuffer-filter-groups nil))
903 (let ((buf (ibuffer-current-buffer)))
904 (ibuffer-update nil t)
905 (when buf
906 (ibuffer-jump-to-buffer (buffer-name buf)))))
908 ;;;###autoload
909 (defun ibuffer-pop-filter ()
910 "Remove the top filter in this buffer."
911 (interactive)
912 (when (null ibuffer-filtering-qualifiers)
913 (error "No filters in effect"))
914 (pop ibuffer-filtering-qualifiers)
915 (let ((buf (ibuffer-current-buffer)))
916 (ibuffer-update nil t)
917 (when buf
918 (ibuffer-jump-to-buffer (buffer-name buf)))))
920 (defun ibuffer-push-filter (qualifier)
921 "Add QUALIFIER to `ibuffer-filtering-qualifiers'."
922 (push qualifier ibuffer-filtering-qualifiers))
924 ;;;###autoload
925 (defun ibuffer-decompose-filter ()
926 "Separate the top compound filter (OR, NOT, or SAVED) in this buffer.
928 This means that the topmost filter on the filtering stack, which must
929 be a complex filter like (OR [name: foo] [mode: bar-mode]), will be
930 turned into two separate filters [name: foo] and [mode: bar-mode]."
931 (interactive)
932 (unless ibuffer-filtering-qualifiers
933 (error "No filters in effect"))
934 (let* ((filters ibuffer-filtering-qualifiers)
935 (head (cdar filters))
936 (tail (cdr filters))
937 (value
938 (pcase (caar filters)
939 (`or (nconc head tail))
940 (`saved
941 (let ((data (assoc head ibuffer-saved-filters)))
942 (unless data
943 (ibuffer-filter-disable)
944 (error "Unknown saved filter %s" head))
945 (append (cadr data) tail)))
946 (`not (cons head tail))
948 (error "Filter type %s is not compound" (caar filters))))))
949 (setq ibuffer-filtering-qualifiers value))
950 (ibuffer-update nil t))
952 ;;;###autoload
953 (defun ibuffer-exchange-filters ()
954 "Exchange the top two filters on the stack in this buffer."
955 (interactive)
956 (let ((filters ibuffer-filtering-qualifiers))
957 (when (< (length filters) 2)
958 (error "Need two filters to exchange"))
959 (cl-rotatef (car filters) (cadr filters))
960 (ibuffer-update nil t)))
962 ;;;###autoload
963 (defun ibuffer-negate-filter ()
964 "Negate the sense of the top filter in the current buffer."
965 (interactive)
966 (when (null ibuffer-filtering-qualifiers)
967 (error "No filters in effect"))
968 (let ((lim (pop ibuffer-filtering-qualifiers)))
969 (push (if (eq (car lim) 'not)
970 (cdr lim)
971 (cons 'not lim))
972 ibuffer-filtering-qualifiers))
973 (ibuffer-update nil t))
975 ;;;###autoload
976 (defun ibuffer-or-filter (&optional reverse)
977 "Replace the top two filters in this buffer with their logical OR.
978 If optional argument REVERSE is non-nil, instead break the top OR
979 filter into parts."
980 (interactive "P")
981 (if reverse
982 (progn
983 (when (or (null ibuffer-filtering-qualifiers)
984 (not (eq 'or (caar ibuffer-filtering-qualifiers))))
985 (error "Top filter is not an OR"))
986 (let ((lim (pop ibuffer-filtering-qualifiers)))
987 (setq ibuffer-filtering-qualifiers
988 (nconc (cdr lim) ibuffer-filtering-qualifiers))))
989 (when (< (length ibuffer-filtering-qualifiers) 2)
990 (error "Need two filters to OR"))
991 ;; If the second filter is an OR, just add to it.
992 (let ((first (pop ibuffer-filtering-qualifiers))
993 (second (pop ibuffer-filtering-qualifiers)))
994 (if (eq 'or (car second))
995 (push (nconc (list 'or first) (cdr second))
996 ibuffer-filtering-qualifiers)
997 (push (list 'or first second)
998 ibuffer-filtering-qualifiers))))
999 (ibuffer-update nil t))
1001 (defun ibuffer-maybe-save-stuff ()
1002 (when ibuffer-save-with-custom
1003 (if (fboundp 'customize-save-variable)
1004 (progn
1005 (customize-save-variable 'ibuffer-saved-filters
1006 ibuffer-saved-filters)
1007 (customize-save-variable 'ibuffer-saved-filter-groups
1008 ibuffer-saved-filter-groups))
1009 (message "Not saved permanently: Customize not available"))))
1011 ;;;###autoload
1012 (defun ibuffer-save-filters (name filters)
1013 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'.
1014 Interactively, prompt for NAME, and use the current filters."
1015 (interactive
1016 (if (null ibuffer-filtering-qualifiers)
1017 (error "No filters currently in effect")
1018 (list
1019 (read-from-minibuffer "Save current filters as: ")
1020 ibuffer-filtering-qualifiers)))
1021 (ibuffer-aif (assoc name ibuffer-saved-filters)
1022 (setcdr it filters)
1023 (push (cons name filters) ibuffer-saved-filters))
1024 (ibuffer-maybe-save-stuff))
1026 ;;;###autoload
1027 (defun ibuffer-delete-saved-filters (name)
1028 "Delete saved filters with NAME from `ibuffer-saved-filters'."
1029 (interactive
1030 (list
1031 (if (null ibuffer-saved-filters)
1032 (error "No saved filters")
1033 (completing-read "Delete saved filters: "
1034 ibuffer-saved-filters nil t))))
1035 (setq ibuffer-saved-filters
1036 (ibuffer-delete-alist name ibuffer-saved-filters))
1037 (ibuffer-maybe-save-stuff)
1038 (ibuffer-update nil t))
1040 ;;;###autoload
1041 (defun ibuffer-add-saved-filters (name)
1042 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
1043 (interactive
1044 (list
1045 (if (null ibuffer-saved-filters)
1046 (error "No saved filters")
1047 (completing-read "Add saved filters: "
1048 ibuffer-saved-filters nil t))))
1049 (push (cons 'saved name) ibuffer-filtering-qualifiers)
1050 (ibuffer-update nil t))
1052 ;;;###autoload
1053 (defun ibuffer-switch-to-saved-filters (name)
1054 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'."
1055 (interactive
1056 (list
1057 (if (null ibuffer-saved-filters)
1058 (error "No saved filters")
1059 (completing-read "Switch to saved filters: "
1060 ibuffer-saved-filters nil t))))
1061 (setq ibuffer-filtering-qualifiers (list (cons 'saved name)))
1062 (ibuffer-update nil t))
1064 (defun ibuffer-format-filter-group-data (filter)
1065 (if (equal filter "Default")
1067 (concat "Filter:" (mapconcat #'ibuffer-format-qualifier
1068 (cdr (assq filter ibuffer-filter-groups))
1069 " "))))
1071 (defun ibuffer-format-qualifier (qualifier)
1072 (if (eq (car-safe qualifier) 'not)
1073 (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier)) "]")
1074 (ibuffer-format-qualifier-1 qualifier)))
1076 (defun ibuffer-format-qualifier-1 (qualifier)
1077 (pcase (car qualifier)
1078 (`saved
1079 (concat " [filter: " (cdr qualifier) "]"))
1080 (`or
1081 (concat " [OR" (mapconcat #'ibuffer-format-qualifier
1082 (cdr qualifier) "") "]"))
1084 (let ((type (assq (car qualifier) ibuffer-filtering-alist)))
1085 (unless qualifier
1086 (error "Ibuffer: bad qualifier %s" qualifier))
1087 (concat " [" (cadr type) ": " (format "%s]" (cdr qualifier)))))))
1090 (defun ibuffer-list-buffer-modes (&optional include-parents)
1091 "Create a completion table of buffer modes currently in use.
1092 If INCLUDE-PARENTS is non-nil then include parent modes."
1093 (let ((modes))
1094 (dolist (buf (buffer-list))
1095 (let ((this-mode (buffer-local-value 'major-mode buf)))
1096 (while (and this-mode (not (memq this-mode modes)))
1097 (push this-mode modes)
1098 (setq this-mode (and include-parents
1099 (get this-mode 'derived-mode-parent))))))
1100 (mapcar #'symbol-name modes)))
1103 ;;; Extra operation definitions
1105 ;;;###autoload (autoload 'ibuffer-filter-by-mode "ibuf-ext")
1106 (define-ibuffer-filter mode
1107 "Toggle current view to buffers with major mode QUALIFIER."
1108 (:description "major mode"
1109 :reader
1110 (let* ((buf (ibuffer-current-buffer))
1111 (default (if (and buf (buffer-live-p buf))
1112 (symbol-name (buffer-local-value
1113 'major-mode buf)))))
1114 (intern
1115 (completing-read
1116 (if default
1117 (format "Filter by major mode (default %s): " default)
1118 "Filter by major mode: ")
1119 obarray
1120 #'(lambda (e)
1121 (string-match "-mode\\'" (symbol-name e)))
1122 t nil nil default))))
1123 (eq qualifier (buffer-local-value 'major-mode buf)))
1125 ;;;###autoload (autoload 'ibuffer-filter-by-used-mode "ibuf-ext")
1126 (define-ibuffer-filter used-mode
1127 "Toggle current view to buffers with major mode QUALIFIER.
1128 Called interactively, this function allows selection of modes
1129 currently used by buffers."
1130 (:description "major mode in use"
1131 :reader
1132 (let* ((buf (ibuffer-current-buffer))
1133 (default (if (and buf (buffer-live-p buf))
1134 (symbol-name (buffer-local-value
1135 'major-mode buf)))))
1136 (intern
1137 (completing-read
1138 (if default
1139 (format "Filter by major mode (default %s): " default)
1140 "Filter by major mode: ")
1141 (ibuffer-list-buffer-modes) nil t nil nil default))))
1142 (eq qualifier (buffer-local-value 'major-mode buf)))
1144 ;;;###autoload (autoload 'ibuffer-filter-by-derived-mode "ibuf-ext")
1145 (define-ibuffer-filter derived-mode
1146 "Toggle current view to buffers whose major mode inherits from QUALIFIER."
1147 (:description "derived mode"
1148 :reader
1149 (intern
1150 (completing-read "Filter by derived mode: "
1151 (ibuffer-list-buffer-modes t)
1152 nil t)))
1153 (with-current-buffer buf (derived-mode-p qualifier)))
1155 ;;;###autoload (autoload 'ibuffer-filter-by-name "ibuf-ext")
1156 (define-ibuffer-filter name
1157 "Toggle current view to buffers with name matching QUALIFIER."
1158 (:description "buffer name"
1159 :reader (read-from-minibuffer "Filter by name (regexp): "))
1160 (string-match qualifier (buffer-name buf)))
1162 ;;;###autoload (autoload 'ibuffer-filter-by-filename "ibuf-ext")
1163 (define-ibuffer-filter filename
1164 "Toggle current view to buffers with filename matching QUALIFIER."
1165 (:description "filename"
1166 :reader (read-from-minibuffer "Filter by filename (regexp): "))
1167 (ibuffer-awhen (with-current-buffer buf (ibuffer-buffer-file-name))
1168 (string-match qualifier it)))
1170 ;;;###autoload (autoload 'ibuffer-filter-by-size-gt "ibuf-ext")
1171 (define-ibuffer-filter size-gt
1172 "Toggle current view to buffers with size greater than QUALIFIER."
1173 (:description "size greater than"
1174 :reader
1175 (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
1176 (> (with-current-buffer buf (buffer-size))
1177 qualifier))
1179 ;;;###autoload (autoload 'ibuffer-filter-by-size-lt "ibuf-ext")
1180 (define-ibuffer-filter size-lt
1181 "Toggle current view to buffers with size less than QUALIFIER."
1182 (:description "size less than"
1183 :reader
1184 (string-to-number (read-from-minibuffer "Filter by size less than: ")))
1185 (< (with-current-buffer buf (buffer-size))
1186 qualifier))
1188 ;;;###autoload (autoload 'ibuffer-filter-by-content "ibuf-ext")
1189 (define-ibuffer-filter content
1190 "Toggle current view to buffers whose contents match QUALIFIER."
1191 (:description "content"
1192 :reader (read-from-minibuffer "Filter by content (regexp): "))
1193 (with-current-buffer buf
1194 (save-excursion
1195 (goto-char (point-min))
1196 (re-search-forward qualifier nil t))))
1198 ;;;###autoload (autoload 'ibuffer-filter-by-predicate "ibuf-ext")
1199 (define-ibuffer-filter predicate
1200 "Toggle current view to buffers for which QUALIFIER returns non-nil."
1201 (:description "predicate"
1202 :reader (read-minibuffer "Filter by predicate (form): "))
1203 (with-current-buffer buf
1204 (eval qualifier)))
1206 ;;; Sorting
1208 ;;;###autoload
1209 (defun ibuffer-toggle-sorting-mode ()
1210 "Toggle the current sorting mode.
1211 Default sorting modes are:
1212 Recency - the last time the buffer was viewed
1213 Name - the name of the buffer
1214 Major Mode - the name of the major mode of the buffer
1215 Size - the size of the buffer"
1216 (interactive)
1217 (let ((modes (mapcar #'car ibuffer-sorting-functions-alist)))
1218 (cl-pushnew 'recency modes)
1219 (setq modes (sort modes #'string-lessp))
1220 (let ((next (or (car-safe (cdr-safe (memq ibuffer-sorting-mode modes)))
1221 (car modes))))
1222 (setq ibuffer-sorting-mode next)
1223 (message "Sorting by %s" next)))
1224 (ibuffer-redisplay t))
1226 ;;;###autoload
1227 (defun ibuffer-invert-sorting ()
1228 "Toggle whether or not sorting is in reverse order."
1229 (interactive)
1230 (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep))
1231 (message "Sorting order %s"
1232 (if ibuffer-sorting-reversep
1233 "reversed"
1234 "normal"))
1235 (ibuffer-redisplay t))
1237 ;;;###autoload (autoload 'ibuffer-do-sort-by-major-mode "ibuf-ext")
1238 (define-ibuffer-sorter major-mode
1239 "Sort the buffers by major modes.
1240 Ordering is lexicographic."
1241 (:description "major mode")
1242 (string-lessp (downcase
1243 (symbol-name (buffer-local-value 'major-mode (car a))))
1244 (downcase
1245 (symbol-name (buffer-local-value 'major-mode (car b))))))
1247 ;;;###autoload (autoload 'ibuffer-do-sort-by-mode-name "ibuf-ext")
1248 (define-ibuffer-sorter mode-name
1249 "Sort the buffers by their mode name.
1250 Ordering is lexicographic."
1251 (:description "major mode name")
1252 (string-lessp (downcase
1253 (with-current-buffer
1254 (car a)
1255 (format-mode-line mode-name)))
1256 (downcase
1257 (with-current-buffer
1258 (car b)
1259 (format-mode-line mode-name)))))
1261 ;;;###autoload (autoload 'ibuffer-do-sort-by-alphabetic "ibuf-ext")
1262 (define-ibuffer-sorter alphabetic
1263 "Sort the buffers by their names.
1264 Ordering is lexicographic."
1265 (:description "buffer name")
1266 (string-lessp
1267 (buffer-name (car a))
1268 (buffer-name (car b))))
1270 ;;;###autoload (autoload 'ibuffer-do-sort-by-size "ibuf-ext")
1271 (define-ibuffer-sorter size
1272 "Sort the buffers by their size."
1273 (:description "size")
1274 (< (with-current-buffer (car a)
1275 (buffer-size))
1276 (with-current-buffer (car b)
1277 (buffer-size))))
1279 ;;;###autoload (autoload 'ibuffer-do-sort-by-filename/process "ibuf-ext")
1280 (define-ibuffer-sorter filename/process
1281 "Sort the buffers by their file name/process name."
1282 (:description "file name")
1283 (string-lessp
1284 ;; FIXME: For now just compare the file name and the process name
1285 ;; (if it exists). Is there a better way to do this?
1286 (or (buffer-file-name (car a))
1287 (let ((pr-a (get-buffer-process (car a))))
1288 (and (processp pr-a) (process-name pr-a))))
1289 (or (buffer-file-name (car b))
1290 (let ((pr-b (get-buffer-process (car b))))
1291 (and (processp pr-b) (process-name pr-b))))))
1293 ;;; Functions to emulate bs.el
1295 ;;;###autoload
1296 (defun ibuffer-bs-show ()
1297 "Emulate `bs-show' from the bs.el package."
1298 (interactive)
1299 (ibuffer t "*Ibuffer-bs*" '((filename . ".*")) nil t)
1300 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all))
1302 (defun ibuffer-bs-toggle-all ()
1303 "Emulate `bs-toggle-show-all' from the bs.el package."
1304 (interactive)
1305 (if ibuffer-filtering-qualifiers
1306 (ibuffer-pop-filter)
1307 (progn (ibuffer-push-filter '(filename . ".*"))
1308 (ibuffer-update nil t))))
1310 ;;; Handy functions
1312 ;;;###autoload
1313 (defun ibuffer-add-to-tmp-hide (regexp)
1314 "Add REGEXP to `ibuffer-tmp-hide-regexps'.
1315 This means that buffers whose name matches REGEXP will not be shown
1316 for this Ibuffer session."
1317 (interactive
1318 (list
1319 (read-from-minibuffer "Never show buffers matching: "
1320 (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
1321 (push regexp ibuffer-tmp-hide-regexps))
1323 ;;;###autoload
1324 (defun ibuffer-add-to-tmp-show (regexp)
1325 "Add REGEXP to `ibuffer-tmp-show-regexps'.
1326 This means that buffers whose name matches REGEXP will always be shown
1327 for this Ibuffer session."
1328 (interactive
1329 (list
1330 (read-from-minibuffer "Always show buffers matching: "
1331 (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
1332 (push regexp ibuffer-tmp-show-regexps))
1334 ;;;###autoload
1335 (defun ibuffer-forward-next-marked (&optional count mark direction)
1336 "Move forward by COUNT marked buffers (default 1).
1338 If MARK is non-nil, it should be a character denoting the type of mark
1339 to move by. The default is `ibuffer-marked-char'.
1341 If DIRECTION is non-nil, it should be an integer; negative integers
1342 mean move backwards, non-negative integers mean move forwards."
1343 (interactive "P")
1344 (unless count
1345 (setq count 1))
1346 (unless mark
1347 (setq mark ibuffer-marked-char))
1348 (unless direction
1349 (setq direction 1))
1350 ;; Skip the title
1351 (ibuffer-forward-line 0)
1352 (let ((opos (point)))
1353 (ibuffer-forward-line direction)
1354 (while (not (or (= (point) opos)
1355 (eq (ibuffer-current-mark) mark)))
1356 (ibuffer-forward-line direction))
1357 (when (and (= (point) opos)
1358 (not (eq (ibuffer-current-mark) mark)))
1359 (error "No buffers with mark %c" mark))))
1361 ;;;###autoload
1362 (defun ibuffer-backwards-next-marked (&optional count mark)
1363 "Move backwards by COUNT marked buffers (default 1).
1365 If MARK is non-nil, it should be a character denoting the type of mark
1366 to move by. The default is `ibuffer-marked-char'."
1367 (interactive "P")
1368 (ibuffer-forward-next-marked count mark -1))
1370 ;;;###autoload
1371 (defun ibuffer-do-kill-lines ()
1372 "Hide all of the currently marked lines."
1373 (interactive)
1374 (if (= (ibuffer-count-marked-lines) 0)
1375 (message "No buffers marked; use 'm' to mark a buffer")
1376 (let ((count
1377 (ibuffer-map-marked-lines
1378 #'(lambda (_buf _mark)
1379 'kill))))
1380 (message "Killed %s lines" count))))
1382 ;;;###autoload
1383 (defun ibuffer-jump-to-buffer (name)
1384 "Move point to the buffer whose name is NAME.
1386 If called interactively, prompt for a buffer name and go to the
1387 corresponding line in the Ibuffer buffer. If said buffer is in a
1388 hidden group filter, open it.
1390 If `ibuffer-jump-offer-only-visible-buffers' is non-nil, only offer
1391 visible buffers in the completion list. Calling the command with
1392 a prefix argument reverses the meaning of that variable."
1393 (interactive (list
1394 (let ((only-visible ibuffer-jump-offer-only-visible-buffers))
1395 (when current-prefix-arg
1396 (setq only-visible (not only-visible)))
1397 (if only-visible
1398 (let ((table (mapcar #'(lambda (x)
1399 (buffer-name (car x)))
1400 (ibuffer-current-state-list))))
1401 (when (null table)
1402 (error "No buffers!"))
1403 (completing-read "Jump to buffer: "
1404 table nil t))
1405 (read-buffer "Jump to buffer: " nil t)))))
1406 (when (not (string= "" name))
1407 (let (buf-point)
1408 ;; Blindly search for our buffer: it is very likely that it is
1409 ;; not in a hidden filter group.
1410 (ibuffer-map-lines #'(lambda (buf _marks)
1411 (when (string= (buffer-name buf) name)
1412 (setq buf-point (point))
1413 nil))
1414 t nil)
1415 (when (and
1416 (null buf-point)
1417 (not (null ibuffer-hidden-filter-groups)))
1418 ;; We did not find our buffer. It must be in a hidden filter
1419 ;; group, so go through all hidden filter groups to find it.
1420 (catch 'found
1421 (dolist (group ibuffer-hidden-filter-groups)
1422 (ibuffer-jump-to-filter-group group)
1423 (ibuffer-toggle-filter-group)
1424 (ibuffer-map-lines #'(lambda (buf _marks)
1425 (when (string= (buffer-name buf) name)
1426 (setq buf-point (point))
1427 nil))
1428 t group)
1429 (if buf-point
1430 (throw 'found nil)
1431 (ibuffer-toggle-filter-group)))))
1432 (if (null buf-point)
1433 ;; Still not found even though we expanded all hidden filter
1434 ;; groups: that must be because it's hidden by predicate:
1435 ;; we won't bother trying to display it.
1436 (error "No buffer with name %s" name)
1437 (goto-char buf-point)))))
1439 (declare-function diff-sentinel "diff"
1440 (code &optional old-temp-file new-temp-file))
1442 (defun ibuffer-diff-buffer-with-file-1 (buffer)
1443 (let ((bufferfile (buffer-local-value 'buffer-file-name buffer))
1444 (tempfile (make-temp-file "buffer-content-")))
1445 (when bufferfile
1446 (unwind-protect
1447 (progn
1448 (with-current-buffer buffer
1449 (write-region nil nil tempfile nil 'nomessage))
1450 (let* ((old (expand-file-name bufferfile))
1451 (new (expand-file-name tempfile))
1452 (oldtmp (file-local-copy old))
1453 (newtmp (file-local-copy new))
1454 (switches diff-switches)
1455 (command
1456 (mapconcat
1457 'identity
1458 `(,diff-command
1459 ;; Use explicitly specified switches
1460 ,@(if (listp switches) switches (list switches))
1461 ,@(if (or old new)
1462 (list "-L" (shell-quote-argument old)
1463 "-L" (shell-quote-argument
1464 (format "Buffer %s" (buffer-name buffer)))))
1465 ,(shell-quote-argument (or oldtmp old))
1466 ,(shell-quote-argument (or newtmp new)))
1467 " ")))
1468 (let ((inhibit-read-only t))
1469 (insert command "\n")
1470 (diff-sentinel
1471 (call-process shell-file-name nil
1472 (current-buffer) nil
1473 shell-command-switch command))
1474 (insert "\n")))))
1475 (sit-for 0)
1476 (when (file-exists-p tempfile)
1477 (delete-file tempfile)))))
1479 ;;;###autoload
1480 (defun ibuffer-diff-with-file ()
1481 "View the differences between marked buffers and their associated files.
1482 If no buffers are marked, use buffer at point.
1483 This requires the external program \"diff\" to be in your `exec-path'."
1484 (interactive)
1485 (require 'diff)
1486 (let ((marked-bufs (ibuffer-get-marked-buffers)))
1487 (when (null marked-bufs)
1488 (setq marked-bufs (list (ibuffer-current-buffer t))))
1489 (with-current-buffer (get-buffer-create "*Ibuffer Diff*")
1490 (setq buffer-read-only nil)
1491 (buffer-disable-undo (current-buffer))
1492 (erase-buffer)
1493 (buffer-enable-undo (current-buffer))
1494 (diff-mode)
1495 (dolist (buf marked-bufs)
1496 (unless (buffer-live-p buf)
1497 (error "Buffer %s has been killed" buf))
1498 (ibuffer-diff-buffer-with-file-1 buf))
1499 (setq buffer-read-only t)))
1500 (switch-to-buffer "*Ibuffer Diff*"))
1502 ;;;###autoload
1503 (defun ibuffer-copy-filename-as-kill (&optional arg)
1504 "Copy filenames of marked (or next ARG) buffers into the kill ring.
1506 The names are separated by a space.
1507 If a buffer has no filename, it is ignored.
1509 With no prefix arg, use the filename sans its directory of each marked file.
1510 With a zero prefix arg, use the complete filename of each marked file.
1511 With \\[universal-argument], use the filename of each marked file relative
1512 to `ibuffer-default-directory' if non-nil, otherwise `default-directory'.
1514 You can then feed the file name(s) to other commands with \\[yank]."
1515 (interactive "P")
1516 (let* ((buffers (cond ((and (integerp arg) (not (zerop arg)))
1517 (ibuffer--near-buffers arg))
1519 (or (ibuffer-get-marked-buffers)
1520 (list (ibuffer-current-buffer))))))
1521 (file-names
1522 (mapcar
1523 (lambda (buf)
1524 (let ((name (with-current-buffer buf
1525 (ibuffer-buffer-file-name))))
1526 (if (null name)
1528 (cond ((and (integerp arg) (zerop arg)) name)
1529 ((consp arg)
1530 (file-relative-name
1531 name (or ibuffer-default-directory
1532 default-directory)))
1533 (t (file-name-nondirectory name))))))
1534 buffers))
1535 (string
1536 (mapconcat 'identity (delete "" file-names) " ")))
1537 (unless (string= string "")
1538 (if (eq last-command 'kill-region)
1539 (kill-append string nil)
1540 (kill-new string))
1541 (message "%s" string))))
1543 ;;;###autoload
1544 (defun ibuffer-copy-buffername-as-kill (&optional arg)
1545 "Copy buffer names of marked (or next ARG) buffers into the kill ring.
1546 The names are separated by a space.
1547 You can then feed the file name(s) to other commands with \\[yank]."
1548 (interactive "P")
1549 (let* ((buffers (cond ((and (integerp arg) (not (zerop arg)))
1550 (ibuffer--near-buffers arg))
1552 (or (ibuffer-get-marked-buffers)
1553 (list (ibuffer-current-buffer))))))
1554 (string (mapconcat #'buffer-name buffers " ")))
1555 (unless (string= string "")
1556 (if (eq last-command 'kill-region)
1557 (kill-append string nil)
1558 (kill-new string))
1559 (message "%s" string))))
1561 (defun ibuffer-mark-on-buffer (func &optional ibuffer-mark-on-buffer-mark group)
1562 (let ((count
1563 (ibuffer-map-lines
1564 #'(lambda (buf _mark)
1565 (when (funcall func buf)
1566 (ibuffer-set-mark-1 (or ibuffer-mark-on-buffer-mark
1567 ibuffer-marked-char))
1570 group)))
1571 (ibuffer-redisplay t)
1572 (unless (eq ibuffer-mark-on-buffer-mark ?\s)
1573 (message "Marked %s buffers" count))))
1575 ;;;###autoload
1576 (defun ibuffer-mark-by-name-regexp (regexp)
1577 "Mark all buffers whose name matches REGEXP."
1578 (interactive "sMark by name (regexp): ")
1579 (ibuffer-mark-on-buffer
1580 #'(lambda (buf)
1581 (string-match regexp (buffer-name buf)))))
1583 (defun ibuffer-locked-buffer-p (&optional buf)
1584 "Return non-nil if BUF is locked.
1585 When BUF nil, default to the buffer at current line."
1586 (let ((cbuffer (or buf (ibuffer-current-buffer))))
1587 (when cbuffer
1588 (with-current-buffer cbuffer
1589 (and (boundp 'emacs-lock-mode) emacs-lock-mode)))))
1591 ;;;###autoload
1592 (defun ibuffer-mark-by-locked ()
1593 "Mark all locked buffers."
1594 (interactive)
1595 (when (featurep 'emacs-lock)
1596 (ibuffer-mark-on-buffer
1597 (lambda (buf)
1598 (ibuffer-locked-buffer-p buf)))))
1600 ;;;###autoload
1601 (defun ibuffer-mark-by-mode-regexp (regexp)
1602 "Mark all buffers whose major mode matches REGEXP."
1603 (interactive "sMark by major mode (regexp): ")
1604 (ibuffer-mark-on-buffer
1605 #'(lambda (buf)
1606 (with-current-buffer buf
1607 (string-match regexp (format-mode-line mode-name nil nil buf))))))
1609 ;;;###autoload
1610 (defun ibuffer-mark-by-file-name-regexp (regexp)
1611 "Mark all buffers whose file name matches REGEXP."
1612 (interactive "sMark by file name (regexp): ")
1613 (ibuffer-mark-on-buffer
1614 #'(lambda (buf)
1615 (let ((name (or (buffer-file-name buf)
1616 (with-current-buffer buf
1617 (and
1618 (boundp 'dired-directory)
1619 (stringp dired-directory)
1620 dired-directory)))))
1621 (when name
1622 (string-match regexp name))))))
1624 ;;;###autoload
1625 (defun ibuffer-mark-by-content-regexp (regexp &optional all-buffers)
1626 "Mark all buffers whose content matches REGEXP.
1627 Optional arg ALL-BUFFERS, if non-nil, then search in all buffers.
1628 Otherwise buffers whose name matches an element of
1629 `ibuffer-never-search-content-name' or whose major mode is on
1630 `ibuffer-never-search-content-mode' are excluded."
1631 (interactive (let ((reg (read-string "Mark by content (regexp): ")))
1632 (list reg current-prefix-arg)))
1633 (ibuffer-mark-on-buffer
1634 #'(lambda (buf)
1635 (let ((mode (with-current-buffer buf major-mode))
1636 res)
1637 (cond ((and (not all-buffers)
1639 (memq mode ibuffer-never-search-content-mode)
1640 (cl-some (lambda (x) (string-match x (buffer-name buf)))
1641 ibuffer-never-search-content-name)))
1642 (setq res nil))
1644 (with-current-buffer buf
1645 (save-mark-and-excursion
1646 (goto-char (point-min))
1647 (setq res (re-search-forward regexp nil t)))))) res))))
1649 ;;;###autoload
1650 (defun ibuffer-mark-by-mode (mode)
1651 "Mark all buffers whose major mode equals MODE."
1652 (interactive
1653 (let* ((buf (ibuffer-current-buffer))
1654 (default (if (and buf (buffer-live-p buf))
1655 (symbol-name (buffer-local-value
1656 'major-mode buf)))))
1657 (list (intern
1658 (completing-read
1659 (if default
1660 (format "Mark by major mode (default %s): " default)
1661 "Mark by major mode: ")
1662 (ibuffer-list-buffer-modes) nil t nil nil default)))))
1663 (ibuffer-mark-on-buffer
1664 #'(lambda (buf)
1665 (eq (buffer-local-value 'major-mode buf) mode))))
1667 ;;;###autoload
1668 (defun ibuffer-mark-modified-buffers ()
1669 "Mark all modified buffers."
1670 (interactive)
1671 (ibuffer-mark-on-buffer
1672 #'(lambda (buf) (buffer-modified-p buf))))
1674 ;;;###autoload
1675 (defun ibuffer-mark-unsaved-buffers ()
1676 "Mark all modified buffers that have an associated file."
1677 (interactive)
1678 (ibuffer-mark-on-buffer
1679 #'(lambda (buf) (and (buffer-local-value 'buffer-file-name buf)
1680 (buffer-modified-p buf)))))
1682 ;;;###autoload
1683 (defun ibuffer-mark-dissociated-buffers ()
1684 "Mark all buffers whose associated file does not exist."
1685 (interactive)
1686 (ibuffer-mark-on-buffer
1687 #'(lambda (buf)
1688 (with-current-buffer buf
1690 (and buffer-file-name
1691 (not (file-exists-p buffer-file-name)))
1692 (and (eq major-mode 'dired-mode)
1693 (boundp 'dired-directory)
1694 (stringp dired-directory)
1695 (not (file-exists-p (file-name-directory dired-directory)))))))))
1697 ;;;###autoload
1698 (defun ibuffer-mark-help-buffers ()
1699 "Mark buffers whose major mode is in variable `ibuffer-help-buffer-modes'."
1700 (interactive)
1701 (ibuffer-mark-on-buffer
1702 #'(lambda (buf)
1703 (with-current-buffer buf
1704 (memq major-mode ibuffer-help-buffer-modes)))))
1706 ;;;###autoload
1707 (defun ibuffer-mark-compressed-file-buffers ()
1708 "Mark buffers whose associated file is compressed."
1709 (interactive)
1710 (ibuffer-mark-on-buffer
1711 #'(lambda (buf)
1712 (with-current-buffer buf
1713 (and buffer-file-name
1714 (string-match ibuffer-compressed-file-name-regexp
1715 buffer-file-name))))))
1717 ;;;###autoload
1718 (defun ibuffer-mark-old-buffers ()
1719 "Mark buffers which have not been viewed in `ibuffer-old-time' hours."
1720 (interactive)
1721 (ibuffer-mark-on-buffer
1722 #'(lambda (buf)
1723 (with-current-buffer buf
1724 ;; hacked from midnight.el
1725 (when buffer-display-time
1726 (let* ((now (float-time))
1727 (then (float-time buffer-display-time)))
1728 (> (- now then) (* 60 60 ibuffer-old-time))))))))
1730 ;;;###autoload
1731 (defun ibuffer-mark-special-buffers ()
1732 "Mark all buffers whose name begins and ends with `*'."
1733 (interactive)
1734 (ibuffer-mark-on-buffer
1735 #'(lambda (buf) (string-match "^\\*.+\\*$"
1736 (buffer-name buf)))))
1738 ;;;###autoload
1739 (defun ibuffer-mark-read-only-buffers ()
1740 "Mark all read-only buffers."
1741 (interactive)
1742 (ibuffer-mark-on-buffer
1743 #'(lambda (buf) (buffer-local-value 'buffer-read-only buf))))
1745 ;;;###autoload
1746 (defun ibuffer-mark-dired-buffers ()
1747 "Mark all `dired' buffers."
1748 (interactive)
1749 (ibuffer-mark-on-buffer
1750 #'(lambda (buf) (eq (buffer-local-value 'major-mode buf) 'dired-mode))))
1752 ;;;###autoload
1753 (defun ibuffer-do-occur (regexp &optional nlines)
1754 "View lines which match REGEXP in all marked buffers.
1755 Optional argument NLINES says how many lines of context to display: it
1756 defaults to one."
1757 (interactive (occur-read-primary-args))
1758 (if (or (not (integerp nlines))
1759 (< nlines 0))
1760 (setq nlines 0))
1761 (when (zerop (ibuffer-count-marked-lines))
1762 (ibuffer-set-mark ibuffer-marked-char))
1763 (let ((ibuffer-do-occur-bufs nil))
1764 ;; Accumulate a list of marked buffers
1765 (ibuffer-map-marked-lines
1766 #'(lambda (buf _mark)
1767 (push buf ibuffer-do-occur-bufs)))
1768 (occur-1 regexp nlines ibuffer-do-occur-bufs)))
1770 (provide 'ibuf-ext)
1772 ;; Local Variables:
1773 ;; generated-autoload-file: "ibuffer-loaddefs.el"
1774 ;; End:
1776 ;;; ibuf-ext.el ends here