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