Remove unnecessary explicit subword-mode use from isearch
[emacs.git] / lisp / ibuf-ext.el
blob91d9acb3a3c2e3f3d4560014eb6363b9b326d96c
1 ;;; ibuf-ext.el --- extensions for ibuffer -*- lexical-binding:t -*-
3 ;; Copyright (C) 2000-2018 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 <https://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 ;; For details on the structure of ibuffer filters and filter groups,
33 ;; see the documentation for variables `ibuffer-filtering-qualifiers',
34 ;; `ibuffer-filter-groups', and `ibuffer-saved-filters' in that order.
35 ;; The variable `ibuffer-filtering-alist' contains names and
36 ;; descriptions of the currently defined filters; also see the macro
37 ;; `define-ibuffer-filter'.
39 ;;; Code:
41 (require 'ibuffer)
43 (eval-when-compile
44 (require 'ibuf-macs)
45 (require 'cl-lib)
46 (require 'subr-x))
48 ;;; Utility functions
49 (defun ibuffer-remove-alist (key alist)
50 "Remove all entries in ALIST that have a key equal to KEY."
51 (while (ibuffer-awhen (assoc key alist)
52 (setq alist (remove it alist)) it))
53 alist)
55 ;; borrowed from Gnus
56 (defun ibuffer-remove-duplicates (list)
57 "Return a copy of LIST with duplicate elements removed."
58 (let ((new nil)
59 (tail list))
60 (while tail
61 (or (member (car tail) new)
62 (setq new (cons (car tail) new)))
63 (setq tail (cdr tail)))
64 (nreverse new)))
66 (defun ibuffer-split-list (ibuffer-split-list-fn ibuffer-split-list-elts)
67 (let ((hip-crowd nil)
68 (lamers nil))
69 (dolist (ibuffer-split-list-elt ibuffer-split-list-elts)
70 (if (funcall ibuffer-split-list-fn ibuffer-split-list-elt)
71 (push ibuffer-split-list-elt hip-crowd)
72 (push ibuffer-split-list-elt lamers)))
73 ;; Too bad Emacs Lisp doesn't have multiple values.
74 (list (nreverse hip-crowd) (nreverse lamers))))
76 (defcustom ibuffer-never-show-predicates nil
77 "A list of predicates (a regexp or function) for buffers not to display.
78 If a regexp, then it will be matched against the buffer's name.
79 If a function, it will be called with the buffer as an argument, and
80 should return non-nil if this buffer should not be shown."
81 :type '(repeat (choice regexp function))
82 :require 'ibuf-ext
83 :group 'ibuffer)
85 (defcustom ibuffer-always-show-predicates nil
86 "A list of predicates (a regexp or function) for buffers to always display.
87 If a regexp, then it will be matched against the buffer's name.
88 If a function, it will be called with the buffer as an argument, and
89 should return non-nil if this buffer should be shown.
90 Note that buffers matching one of these predicates will be shown
91 regardless of any active filters in this buffer."
92 :type '(repeat (choice regexp function))
93 :group 'ibuffer)
95 (defcustom ibuffer-never-search-content-name
96 (let* ((names '("Completions" "Help" "Messages" "Pp Eval Output"
97 "CompileLog" "Info" "Buffer List" "Ibuffer" "Apropos"))
98 (partial '("Customize Option: " "Async Shell Command\\*"
99 "Shell Command Output\\*" "ediff "))
100 (beg "\\`\\*")
101 (end "\\*\\'")
102 (excluded (mapcar (lambda (x)
103 (format "%s%s" beg x)) partial)))
104 (dolist (str names (nreverse excluded))
105 (push (format "%s%s%s" beg str end) excluded)))
106 "A list of regexps for buffers ignored by `ibuffer-mark-by-content-regexp'.
107 Buffers whose name matches a regexp in this list, are not searched."
108 :version "26.1"
109 :type '(repeat regexp)
110 :require 'ibuf-ext
111 :group 'ibuffer)
113 (defcustom ibuffer-never-search-content-mode '(dired-mode)
114 "A list of major modes ignored by `ibuffer-mark-by-content-regexp'.
115 Buffers whose major mode is in this list, are not searched."
116 :version "26.1"
117 :type '(repeat regexp)
118 :require 'ibuf-ext
119 :group 'ibuffer)
121 (defvar ibuffer-tmp-hide-regexps nil
122 "A list of regexps which should match buffer names to not show.")
124 (defvar ibuffer-tmp-show-regexps nil
125 "A list of regexps which should match buffer names to always show.")
127 (defvar ibuffer-auto-buffers-changed nil)
129 (defun ibuffer-update-saved-filters-format (filters)
130 "Transforms alist from old to new `ibuffer-saved-filters' format.
132 Specifically, converts old-format alist with values of the
133 form (STRING (FILTER-SPECS...)) to alist with values of the
134 form (STRING FILTER-SPECS...), where each filter spec should be a
135 cons cell with a symbol in the car. Any elements in the latter
136 form are kept as is.
138 Returns (OLD-FORMAT-DETECTED . UPDATED-SAVED-FILTERS-LIST)."
139 (when filters
140 (let* ((old-format-detected nil)
141 (fix-filter (lambda (filter-spec)
142 (if (symbolp (car (cadr filter-spec)))
143 filter-spec
144 (setq old-format-detected t) ; side-effect
145 (cons (car filter-spec) (cadr filter-spec)))))
146 (fixed (mapcar fix-filter filters)))
147 (cons old-format-detected fixed))))
149 (defcustom ibuffer-saved-filters '(("programming"
150 (or (derived-mode . prog-mode)
151 (mode . ess-mode)
152 (mode . compilation-mode)))
153 ("text document"
154 (and (derived-mode . text-mode)
155 (not (starred-name))))
156 ("TeX"
157 (or (derived-mode . tex-mode)
158 (mode . latex-mode)
159 (mode . context-mode)
160 (mode . ams-tex-mode)
161 (mode . bibtex-mode)))
162 ("web"
163 (or (derived-mode . sgml-mode)
164 (derived-mode . css-mode)
165 (mode . javascript-mode)
166 (mode . js2-mode)
167 (mode . scss-mode)
168 (derived-mode . haml-mode)
169 (mode . sass-mode)))
170 ("gnus"
171 (or (mode . message-mode)
172 (mode . mail-mode)
173 (mode . gnus-group-mode)
174 (mode . gnus-summary-mode)
175 (mode . gnus-article-mode))))
177 "An alist mapping saved filter names to filter specifications.
179 Each element should look like (\"NAME\" . FILTER-LIST), where
180 FILTER-LIST has the same structure as the variable
181 `ibuffer-filtering-qualifiers', which see. The filters defined
182 here are joined with an implicit logical `and' and associated
183 with NAME. The combined specification can be used by name in
184 other filter specifications via the `saved' qualifier (again, see
185 `ibuffer-filtering-qualifiers'). They can also be switched to by
186 name (see the functions `ibuffer-switch-to-saved-filters' and
187 `ibuffer-save-filters'). The variable `ibuffer-save-with-custom'
188 affects how this information is saved for future sessions. This
189 variable can be set directly from lisp code."
190 :version "26.1"
191 :type '(alist :key-type (string :tag "Filter name")
192 :value-type (repeat :tag "Filter specification" sexp))
193 :set (lambda (symbol value)
194 ;; Just set-default but update legacy old-style format
195 (set-default symbol (cdr (ibuffer-update-saved-filters-format value))))
196 :group 'ibuffer)
198 (defvar ibuffer-old-saved-filters-warning
199 (concat "Deprecated format detected for variable `ibuffer-saved-filters'.
201 The format has been repaired and the variable modified accordingly.
202 You can save the current value through the customize system by
203 either clicking or hitting return "
204 (make-text-button
205 "here" nil
206 'face '(:weight bold :inherit button)
207 'mouse-face '(:weight normal :background "gray50" :inherit button)
208 'follow-link t
209 'help-echo "Click or RET: save new value in customize"
210 'action (lambda (_)
211 (if (not (fboundp 'customize-save-variable))
212 (message "Customize not available; value not saved")
213 (customize-save-variable 'ibuffer-saved-filters
214 ibuffer-saved-filters)
215 (message "Saved updated ibuffer-saved-filters."))))
216 ". See below for
217 an explanation and alternative ways to save the repaired value.
219 Explanation: For the list variable `ibuffer-saved-filters',
220 elements of the form (STRING (FILTER-SPECS...)) are deprecated
221 and should instead have the form (STRING FILTER-SPECS...), where
222 each filter spec is a cons cell with a symbol in the car. See
223 `ibuffer-saved-filters' for details. The repaired value fixes
224 this format without changing the meaning of the saved filters.
226 Alternative ways to save the repaired value:
228 1. Do M-x customize-variable and entering `ibuffer-saved-filters'
229 when prompted.
231 2. Set the updated value manually by copying the
232 following emacs-lisp form to your emacs init file.
237 (defvar ibuffer-filtering-qualifiers nil
238 "A list specifying the filters currently acting on the buffer list.
240 If this list is nil, then no filters are currently in
241 effect. Otherwise, each element of this list specifies a single
242 filter, and all of the specified filters in the list are applied
243 successively to the buffer list.
245 Each filter specification can be of two types: simple or compound.
247 A simple filter specification has the form (SYMBOL . QUALIFIER),
248 where SYMBOL is a key in the alist `ibuffer-filtering-alist' that
249 determines the filter function to use and QUALIFIER is the data
250 passed to that function (along with the buffer being considered).
252 A compound filter specification can have one of four forms:
254 -- (not FILTER-SPEC)
256 Represents the logical complement of FILTER-SPEC, which
257 is any single filter specification, simple or compound.
258 The form (not . FILTER-SPEC) is also accepted here.
260 -- (and FILTER-SPECS...)
262 Represents the logical-and of the filters defined by one or
263 more filter specifications FILTER-SPECS..., where each
264 specification can be simple or compound. Note that and is
265 implicitly applied to the filters in the top-level list.
267 -- (or FILTER-SPECS...)
269 Represents the logical-or of the filters defined by one or
270 more filter specifications FILTER-SPECS..., where each
271 specification can be simple or compound.
273 -- (saved . \"NAME\")
275 Represents the filter saved under the string NAME
276 in the alist `ibuffer-saved-filters'. It is an
277 error to name a filter that has not been saved.
279 This variable is local to each ibuffer buffer.")
281 ;; This is now frobbed by `define-ibuffer-filter'.
282 (defvar ibuffer-filtering-alist nil
283 "An alist of (SYMBOL DESCRIPTION FUNCTION) which describes a filter.
285 You most likely do not want to modify this variable directly; see
286 `define-ibuffer-filter'.
288 SYMBOL is the symbolic name of the filter. DESCRIPTION is used when
289 displaying information to the user. FUNCTION is given a buffer and
290 the value of the qualifier, and returns non-nil if and only if the
291 buffer should be displayed.")
293 (defcustom ibuffer-filter-format-alist nil
294 "An alist which has special formats used when a filter is active.
295 The contents of this variable should look like:
296 ((FILTER (FORMAT FORMAT ...)) (FILTER (FORMAT FORMAT ...)) ...)
298 For example, suppose that when you add a filter for buffers whose
299 major mode is `emacs-lisp-mode', you only want to see the mark and the
300 name of the buffer. You could accomplish that by adding:
301 (mode ((mark \" \" name)))
302 to this variable."
303 :type '(repeat (list :tag "Association" (symbol :tag "Filter")
304 (list :tag "Formats" (repeat (sexp :tag "Format")))))
305 :group 'ibuffer)
307 (defvar ibuffer-cached-filter-formats nil)
308 (defvar ibuffer-compiled-filter-formats nil)
310 (defvar ibuffer-filter-groups nil
311 "An alist giving this buffer's active filter groups, or nil if none.
313 This alist maps filter group labels to filter specification
314 lists. Each element has the form (\"LABEL\" FILTER-SPECS...),
315 where FILTER-SPECS... represents one or more filter
316 specifications of the same form as allowed as elements of
317 `ibuffer-filtering-qualifiers'.
319 Each filter group is displayed as a separate section in the
320 ibuffer list, headed by LABEL and displaying only the buffers
321 that pass through all the filters associated with NAME in this
322 list.")
324 (defcustom ibuffer-show-empty-filter-groups t
325 "If non-nil, then show the names of filter groups which are empty."
326 :type 'boolean
327 :group 'ibuffer)
329 (defcustom ibuffer-saved-filter-groups nil
330 "An alist of filtering groups to switch between.
332 Each element is of the form (\"NAME\" . FILTER-GROUP-LIST),
333 where NAME is a unique but arbitrary name and FILTER-GROUP-LIST
334 is a list of filter groups with the same structure as
335 allowed for `ibuffer-filter-groups'.
337 See also the functions `ibuffer-save-filter-groups' and
338 `ibuffer-switch-to-saved-filter-groups' for saving and switching
339 between sets of filter groups, and the variable
340 `ibuffer-save-with-custom' that affects how this information is
341 saved."
342 :type '(repeat sexp)
343 :group 'ibuffer)
345 (defvar ibuffer-hidden-filter-groups nil
346 "The list of filter groups that are currently hidden.")
348 (defvar ibuffer-filter-group-kill-ring nil)
350 (defcustom ibuffer-old-time 72
351 "The number of hours before a buffer is considered \"old\"."
352 :type '(choice (const :tag "72 hours (3 days)" 72)
353 (const :tag "48 hours (2 days)" 48)
354 (const :tag "24 hours (1 day)" 24)
355 (integer :tag "hours"))
356 :group 'ibuffer)
358 (defcustom ibuffer-save-with-custom t
359 "If non-nil, then use Custom to save interactively changed variables.
360 Currently, this only applies to `ibuffer-saved-filters' and
361 `ibuffer-saved-filter-groups'."
362 :type 'boolean
363 :group 'ibuffer)
365 (defun ibuffer-repair-saved-filters ()
366 "Updates `ibuffer-saved-filters' to its new-style format, if needed.
368 If this list has any elements of the old-style format, a
369 deprecation warning is raised, with a button allowing persistent
370 update. Any updated filters retain their meaning in the new
371 format. See `ibuffer-update-saved-filters-format' and
372 `ibuffer-saved-filters' for details of the old and new formats."
373 (interactive)
374 (when (and (boundp 'ibuffer-saved-filters) ibuffer-saved-filters)
375 (let ((fixed (ibuffer-update-saved-filters-format ibuffer-saved-filters)))
376 (prog1
377 (setq ibuffer-saved-filters (cdr fixed))
378 (when-let* ((old-format-detected (car fixed)))
379 (let ((warning-series t)
380 (updated-form
381 (with-output-to-string
382 (pp `(setq ibuffer-saved-filters ',ibuffer-saved-filters)))))
383 (display-warning
384 'ibuffer
385 (format ibuffer-old-saved-filters-warning updated-form))))))))
387 (defun ibuffer-ext-visible-p (buf all &optional ibuffer-buf)
389 (ibuffer-buf-matches-predicates buf ibuffer-tmp-show-regexps)
390 (and (not
392 (ibuffer-buf-matches-predicates buf ibuffer-tmp-hide-regexps)
393 (ibuffer-buf-matches-predicates buf ibuffer-never-show-predicates)))
394 (or all
395 (not
396 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates)))
397 (or ibuffer-view-ibuffer
398 (and ibuffer-buf
399 (not (eq ibuffer-buf buf))))
401 (ibuffer-included-in-filters-p buf ibuffer-filtering-qualifiers)
402 (ibuffer-buf-matches-predicates buf ibuffer-always-show-predicates)))))
404 ;;;###autoload
405 (define-minor-mode ibuffer-auto-mode
406 "Toggle use of Ibuffer's auto-update facility (Ibuffer Auto mode).
407 With a prefix argument ARG, enable Ibuffer Auto mode if ARG is
408 positive, and disable it otherwise. If called from Lisp, enable
409 the mode if ARG is omitted or nil."
410 nil nil nil
411 (unless (derived-mode-p 'ibuffer-mode)
412 (error "This buffer is not in Ibuffer mode"))
413 (cond (ibuffer-auto-mode
414 (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed) ; Initialize state vector
415 (add-hook 'post-command-hook 'ibuffer-auto-update-changed))
417 (remove-hook 'post-command-hook 'ibuffer-auto-update-changed))))
419 (defun ibuffer-auto-update-changed ()
420 (when (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed)
421 (dolist (buf (buffer-list))
422 (ignore-errors
423 (with-current-buffer buf
424 (when (and ibuffer-auto-mode
425 (derived-mode-p 'ibuffer-mode))
426 (ibuffer-update nil t)))))))
428 ;;;###autoload
429 (defun ibuffer-mouse-filter-by-mode (event)
430 "Enable or disable filtering by the major mode chosen via mouse."
431 (interactive "e")
432 (ibuffer-interactive-filter-by-mode event))
434 ;;;###autoload
435 (defun ibuffer-interactive-filter-by-mode (event-or-point)
436 "Enable or disable filtering by the major mode at point."
437 (interactive "d")
438 (if (eventp event-or-point)
439 (posn-set-point (event-end event-or-point))
440 (goto-char event-or-point))
441 (let ((buf (ibuffer-current-buffer)))
442 (if (assq 'mode ibuffer-filtering-qualifiers)
443 (setq ibuffer-filtering-qualifiers
444 (ibuffer-remove-alist 'mode ibuffer-filtering-qualifiers))
445 (ibuffer-push-filter (cons 'mode (buffer-local-value 'major-mode buf)))))
446 (ibuffer-update nil t))
448 ;;;###autoload
449 (defun ibuffer-mouse-toggle-filter-group (event)
450 "Toggle the display status of the filter group chosen with the mouse."
451 (interactive "e")
452 (ibuffer-toggle-filter-group-1 (save-excursion
453 (mouse-set-point event)
454 (point))))
456 ;;;###autoload
457 (defun ibuffer-toggle-filter-group ()
458 "Toggle the display status of the filter group on this line."
459 (interactive)
460 (ibuffer-toggle-filter-group-1 (point)))
462 (defun ibuffer-toggle-filter-group-1 (posn)
463 (let ((name (get-text-property posn 'ibuffer-filter-group-name)))
464 (unless (stringp name)
465 (error "No filtering group name present"))
466 (if (member name ibuffer-hidden-filter-groups)
467 (setq ibuffer-hidden-filter-groups
468 (delete name ibuffer-hidden-filter-groups))
469 (push name ibuffer-hidden-filter-groups))
470 (ibuffer-update nil t)))
472 ;;;###autoload
473 (defun ibuffer-forward-filter-group (&optional count)
474 "Move point forwards by COUNT filtering groups."
475 (interactive "P")
476 (unless count
477 (setq count 1))
478 (when (> count 0)
479 (when (get-text-property (point) 'ibuffer-filter-group-name)
480 (goto-char (next-single-property-change
481 (point) 'ibuffer-filter-group-name
482 nil (point-max))))
483 (goto-char (next-single-property-change
484 (point) 'ibuffer-filter-group-name
485 nil (point-max)))
486 (ibuffer-forward-filter-group (1- count)))
487 (ibuffer-forward-line 0))
489 ;;;###autoload
490 (defun ibuffer-backward-filter-group (&optional count)
491 "Move point backwards by COUNT filtering groups."
492 (interactive "P")
493 (unless count
494 (setq count 1))
495 (when (> count 0)
496 (when (get-text-property (point) 'ibuffer-filter-group-name)
497 (goto-char (previous-single-property-change
498 (point) 'ibuffer-filter-group-name
499 nil (point-min))))
500 (goto-char (previous-single-property-change
501 (point) 'ibuffer-filter-group-name
502 nil (point-min)))
503 (ibuffer-backward-filter-group (1- count)))
504 (when (= (point) (point-min))
505 (goto-char (point-max))
506 (ibuffer-backward-filter-group 1))
507 (ibuffer-forward-line 0))
509 (defun ibuffer--maybe-erase-shell-cmd-output ()
510 (let ((buf (get-buffer "*Shell Command Output*")))
511 (when (and (buffer-live-p buf)
512 (not shell-command-dont-erase-buffer)
513 (not (zerop (buffer-size buf))))
514 (with-current-buffer buf (erase-buffer)))))
516 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe "ibuf-ext")
517 (define-ibuffer-op shell-command-pipe (command)
518 "Pipe the contents of each marked buffer to shell command COMMAND."
519 (:interactive "sPipe to shell command: "
520 :opstring "Shell command executed on"
521 :before (ibuffer--maybe-erase-shell-cmd-output)
522 :modifier-p nil)
523 (let ((out-buf (get-buffer-create "*Shell Command Output*")))
524 (with-current-buffer out-buf (goto-char (point-max)))
525 (call-shell-region (point-min) (point-max)
526 command nil out-buf)))
528 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe-replace "ibuf-ext")
529 (define-ibuffer-op shell-command-pipe-replace (command)
530 "Replace the contents of marked buffers with output of pipe to COMMAND."
531 (:interactive "sPipe to shell command (replace): "
532 :opstring "Buffer contents replaced in"
533 :active-opstring "replace buffer contents in"
534 :dangerous t
535 :modifier-p t)
536 (call-shell-region (point-min) (point-max)
537 command 'delete buf))
539 ;;;###autoload (autoload 'ibuffer-do-shell-command-file "ibuf-ext")
540 (define-ibuffer-op shell-command-file (command)
541 "Run shell command COMMAND separately on files of marked buffers."
542 (:interactive "sShell command on buffer's file: "
543 :opstring "Shell command executed on"
544 :before (ibuffer--maybe-erase-shell-cmd-output)
545 :modifier-p nil)
546 (let ((file (and (not (buffer-modified-p))
547 buffer-file-name))
548 (out-buf (get-buffer-create "*Shell Command Output*")))
549 (unless (and file (file-exists-p file))
550 (setq file
551 (make-temp-file
552 (substring
553 (buffer-name) 0
554 (min 10 (length (buffer-name))))))
555 (write-region nil nil file nil 0))
556 (with-current-buffer out-buf (goto-char (point-max)))
557 (call-process-shell-command
558 (format "%s %s"
559 command
560 (shell-quote-argument file))
561 nil out-buf nil)))
563 ;;;###autoload (autoload 'ibuffer-do-eval "ibuf-ext")
564 (define-ibuffer-op eval (form)
565 "Evaluate FORM in each of the buffers.
566 Does not display the buffer during evaluation. See
567 `ibuffer-do-view-and-eval' for that."
568 (:interactive
569 (list
570 (read-from-minibuffer
571 "Eval in buffers (form): "
572 nil read-expression-map t 'read-expression-history))
573 :opstring "evaluated in"
574 :modifier-p :maybe)
575 (eval form))
577 ;;;###autoload (autoload 'ibuffer-do-view-and-eval "ibuf-ext")
578 (define-ibuffer-op view-and-eval (form)
579 "Evaluate FORM while displaying each of the marked buffers.
580 To evaluate a form without viewing the buffer, see `ibuffer-do-eval'."
581 (:interactive
582 (list
583 (read-from-minibuffer
584 "Eval viewing in buffers (form): "
585 nil read-expression-map t 'read-expression-history))
586 :opstring "evaluated in"
587 :complex t
588 :modifier-p :maybe)
589 (let ((ibuffer-buf (current-buffer)))
590 (unwind-protect
591 (progn
592 (switch-to-buffer buf)
593 (eval form))
594 (switch-to-buffer ibuffer-buf))))
596 ;;;###autoload (autoload 'ibuffer-do-rename-uniquely "ibuf-ext")
597 (define-ibuffer-op rename-uniquely ()
598 "Rename marked buffers as with `rename-uniquely'."
599 (:opstring "renamed"
600 :modifier-p t)
601 (rename-uniquely))
603 ;;;###autoload (autoload 'ibuffer-do-revert "ibuf-ext")
604 (define-ibuffer-op revert ()
605 "Revert marked buffers as with `revert-buffer'."
606 (:dangerous t
607 :opstring "reverted"
608 :active-opstring "revert"
609 :modifier-p :maybe)
610 (revert-buffer t t))
612 ;;;###autoload (autoload 'ibuffer-do-isearch "ibuf-ext")
613 (define-ibuffer-op ibuffer-do-isearch ()
614 "Perform a `isearch-forward' in marked buffers."
615 (:interactive ()
616 :opstring "searched in"
617 :complex t
618 :modifier-p :maybe)
619 (multi-isearch-buffers (ibuffer-get-marked-buffers)))
621 ;;;###autoload (autoload 'ibuffer-do-isearch-regexp "ibuf-ext")
622 (define-ibuffer-op ibuffer-do-isearch-regexp ()
623 "Perform a `isearch-forward-regexp' in marked buffers."
624 (:interactive ()
625 :opstring "searched regexp in"
626 :complex t
627 :modifier-p :maybe)
628 (multi-isearch-buffers-regexp (ibuffer-get-marked-buffers)))
630 ;;;###autoload (autoload 'ibuffer-do-replace-regexp "ibuf-ext")
631 (define-ibuffer-op replace-regexp (from-str to-str)
632 "Perform a `replace-regexp' in marked buffers."
633 (:interactive
634 (let* ((from-str (read-from-minibuffer "Replace regexp: "))
635 (to-str (read-from-minibuffer (concat "Replace " from-str
636 " with: "))))
637 (list from-str to-str))
638 :opstring "replaced in"
639 :complex t
640 :modifier-p :maybe)
641 (save-window-excursion
642 (switch-to-buffer buf)
643 (save-excursion
644 (goto-char (point-min))
645 (let ((case-fold-search ibuffer-case-fold-search))
646 (while (re-search-forward from-str nil t)
647 (replace-match to-str))))
650 ;;;###autoload (autoload 'ibuffer-do-query-replace "ibuf-ext")
651 (define-ibuffer-op query-replace (&rest args)
652 "Perform a `query-replace' in marked buffers."
653 (:interactive
654 (query-replace-read-args "Query replace" t t)
655 :opstring "replaced in"
656 :complex t
657 :modifier-p :maybe)
658 (save-window-excursion
659 (switch-to-buffer buf)
660 (save-excursion
661 (let ((case-fold-search ibuffer-case-fold-search))
662 (goto-char (point-min))
663 (apply #'query-replace args)))
666 ;;;###autoload (autoload 'ibuffer-do-query-replace-regexp "ibuf-ext")
667 (define-ibuffer-op query-replace-regexp (&rest args)
668 "Perform a `query-replace-regexp' in marked buffers."
669 (:interactive
670 (query-replace-read-args "Query replace regexp" t t)
671 :opstring "replaced in"
672 :complex t
673 :modifier-p :maybe)
674 (save-window-excursion
675 (switch-to-buffer buf)
676 (save-excursion
677 (let ((case-fold-search ibuffer-case-fold-search))
678 (goto-char (point-min))
679 (apply #'query-replace-regexp args)))
682 ;;;###autoload (autoload 'ibuffer-do-print "ibuf-ext")
683 (define-ibuffer-op print ()
684 "Print marked buffers as with `print-buffer'."
685 (:opstring "printed"
686 :modifier-p nil)
687 (print-buffer))
689 ;;;###autoload
690 (defun ibuffer-included-in-filters-p (buf filters)
691 "Return non-nil if BUF passes all FILTERS.
693 BUF is a lisp buffer object, and FILTERS is a list of filter
694 specifications with the same structure as
695 `ibuffer-filtering-qualifiers'."
696 (not
697 (memq nil ;; a filter will return nil if it failed
698 (mapcar #'(lambda (filter)
699 (ibuffer-included-in-filter-p buf filter))
700 filters))))
702 (defun ibuffer-unary-operand (filter)
703 "Extracts operand from a unary compound FILTER specification.
705 FILTER should be a cons cell of either form (f . d) or (f d),
706 where operand d is itself a cons cell, or nil. Returns d."
707 (let* ((tail (cdr filter))
708 (maybe-q (car-safe tail)))
709 (if (consp maybe-q) maybe-q tail)))
711 (defun ibuffer-included-in-filter-p (buf filter)
712 "Return non-nil if BUF pass FILTER.
714 BUF is a lisp buffer object, and FILTER is a filter
715 specification, with the same structure as an element of the list
716 `ibuffer-filtering-qualifiers'."
717 (if (eq (car filter) 'not)
718 (let ((inner (ibuffer-unary-operand filter)))
719 ;; Allows (not (not ...)) etc, which may be overkill
720 (if (eq (car inner) 'not)
721 (ibuffer-included-in-filter-p buf (ibuffer-unary-operand inner))
722 (not (ibuffer-included-in-filter-p-1 buf inner))))
723 (ibuffer-included-in-filter-p-1 buf filter)))
725 (defun ibuffer-included-in-filter-p-1 (buf filter)
726 (not
727 (not
728 (pcase (car filter)
729 (`or
730 ;;; ATTN: Short-circuiting alternative with parallel structure w/`and
731 ;;(catch 'has-match
732 ;; (dolist (filter-spec (cdr filter) nil)
733 ;; (when (ibuffer-included-in-filter-p buf filter-spec)
734 ;; (throw 'has-match t))))
735 (memq t (mapcar #'(lambda (x)
736 (ibuffer-included-in-filter-p buf x))
737 (cdr filter))))
738 (`and
739 (catch 'no-match
740 (dolist (filter-spec (cdr filter) t)
741 (unless (ibuffer-included-in-filter-p buf filter-spec)
742 (throw 'no-match nil)))))
743 (`saved
744 (let ((data (assoc (cdr filter) ibuffer-saved-filters)))
745 (unless data
746 (ibuffer-filter-disable t)
747 (error "Unknown saved filter %s" (cdr filter)))
748 (ibuffer-included-in-filters-p buf (cdr data))))
750 (pcase-let ((`(,_type ,_desc ,func)
751 (assq (car filter) ibuffer-filtering-alist)))
752 (unless func
753 (ibuffer-filter-disable t)
754 (error "Undefined filter %s" (car filter)))
755 (funcall func buf (cdr filter))))))))
757 (defun ibuffer-generate-filter-groups (bmarklist &optional noempty nodefault)
758 (let ((filter-group-alist (if nodefault
759 ibuffer-filter-groups
760 (append ibuffer-filter-groups
761 (list (cons "Default" nil))))))
762 ;; (dolist (hidden ibuffer-hidden-filter-groups)
763 ;; (setq filter-group-alist (ibuffer-remove-alist
764 ;; hidden filter-group-alist)))
765 (let ((vec (make-vector (length filter-group-alist) nil))
766 (i 0))
767 (dolist (filtergroup filter-group-alist)
768 (let ((filterset (cdr filtergroup)))
769 (cl-multiple-value-bind (hip-crowd lamers)
770 (cl-values-list
771 (ibuffer-split-list (lambda (bufmark)
772 (ibuffer-included-in-filters-p (car bufmark)
773 filterset))
774 bmarklist))
775 (aset vec i hip-crowd)
776 (cl-incf i)
777 (setq bmarklist lamers))))
778 (let (ret)
779 (dotimes (j i)
780 (let ((bufs (aref vec j)))
781 (unless (and noempty (null bufs))
782 (push (cons (car (nth j filter-group-alist))
783 bufs)
784 ret))))
785 ret))))
787 ;;;###autoload
788 (defun ibuffer-filters-to-filter-group (name)
789 "Make the current filters into a filtering group."
790 (interactive "sName for filtering group: ")
791 (when (null ibuffer-filtering-qualifiers)
792 (error "No filters in effect"))
793 (push (cons name ibuffer-filtering-qualifiers) ibuffer-filter-groups)
794 (ibuffer-filter-disable))
796 ;;;###autoload
797 (defun ibuffer-set-filter-groups-by-mode ()
798 "Set the current filter groups to filter by mode."
799 (interactive)
800 (setq ibuffer-filter-groups
801 (mapcar (lambda (mode)
802 (cons (format "%s" mode) `((mode . ,mode))))
803 (let ((modes
804 (ibuffer-remove-duplicates
805 (mapcar (lambda (buf)
806 (buffer-local-value 'major-mode buf))
807 (buffer-list)))))
808 (if ibuffer-view-ibuffer
809 modes
810 (delq 'ibuffer-mode modes)))))
811 (ibuffer-update nil t))
813 ;;;###autoload
814 (defun ibuffer-pop-filter-group ()
815 "Remove the first filter group."
816 (interactive)
817 (when (null ibuffer-filter-groups)
818 (error "No filter groups active"))
819 (setq ibuffer-hidden-filter-groups
820 (delete (pop ibuffer-filter-groups)
821 ibuffer-hidden-filter-groups))
822 (ibuffer-update nil t))
824 (defun ibuffer-read-filter-group-name (msg &optional nodefault noerror)
825 (when (and (not noerror) (null ibuffer-filter-groups))
826 (error "No filter groups active"))
827 ;; `ibuffer-generate-filter-groups' returns all non-hidden filter
828 ;; groups, possibly excluding empty groups or Default.
829 ;; We add `ibuffer-hidden-filter-groups' to the list, excluding
830 ;; Default if necessary.
831 (completing-read msg (nconc
832 (ibuffer-generate-filter-groups
833 (ibuffer-current-state-list)
834 (not ibuffer-show-empty-filter-groups)
835 nodefault)
836 (if nodefault
837 (remove "Default" ibuffer-hidden-filter-groups)
838 ibuffer-hidden-filter-groups))
839 nil t))
841 ;;;###autoload
842 (defun ibuffer-decompose-filter-group (group)
843 "Decompose the filter group GROUP into active filters."
844 (interactive
845 (list (ibuffer-read-filter-group-name "Decompose filter group: " t)))
846 (let ((data (cdr (assoc group ibuffer-filter-groups))))
847 (setq ibuffer-filter-groups (ibuffer-remove-alist
848 group ibuffer-filter-groups)
849 ibuffer-filtering-qualifiers data))
850 (ibuffer-update nil t))
852 ;;;###autoload
853 (defun ibuffer-clear-filter-groups ()
854 "Remove all filter groups."
855 (interactive)
856 (setq ibuffer-filter-groups nil
857 ibuffer-hidden-filter-groups nil)
858 (ibuffer-update nil t))
860 (defun ibuffer-current-filter-groups-with-position ()
861 (save-excursion
862 (goto-char (point-min))
863 (let ((pos nil)
864 (result nil))
865 (while (and (not (eobp))
866 (setq pos (next-single-property-change
867 (point) 'ibuffer-filter-group-name)))
868 (goto-char pos)
869 (push (cons (get-text-property (point) 'ibuffer-filter-group-name)
870 pos)
871 result)
872 (goto-char (next-single-property-change
873 pos 'ibuffer-filter-group-name)))
874 (nreverse result))))
876 ;;;###autoload
877 (defun ibuffer-jump-to-filter-group (name)
878 "Move point to the filter group whose name is NAME."
879 (interactive
880 (list (ibuffer-read-filter-group-name "Jump to filter group: ")))
881 (ibuffer-aif (assoc name (ibuffer-current-filter-groups-with-position))
882 (goto-char (cdr it))
883 (error "No filter group with name %s" name)))
885 ;;;###autoload
886 (defun ibuffer-kill-filter-group (name)
887 "Kill the filter group named NAME.
888 The group will be added to `ibuffer-filter-group-kill-ring'."
889 (interactive (list (ibuffer-read-filter-group-name "Kill filter group: " t)))
890 (when (equal name "Default")
891 (error "Can't kill default filter group"))
892 (ibuffer-aif (assoc name ibuffer-filter-groups)
893 (progn
894 (push (copy-tree it) ibuffer-filter-group-kill-ring)
895 (setq ibuffer-filter-groups (ibuffer-remove-alist
896 name ibuffer-filter-groups))
897 (setq ibuffer-hidden-filter-groups
898 (delete name ibuffer-hidden-filter-groups)))
899 (error "No filter group with name \"%s\"" name))
900 (ibuffer-update nil t))
902 ;;;###autoload
903 (defun ibuffer-kill-line (&optional arg interactive-p)
904 "Kill the filter group at point.
905 See also `ibuffer-kill-filter-group'."
906 (interactive "P\np")
907 (ibuffer-aif (save-excursion
908 (ibuffer-forward-line 0)
909 (get-text-property (point) 'ibuffer-filter-group-name))
910 (progn
911 (ibuffer-kill-filter-group it))
912 (funcall (if interactive-p #'call-interactively #'funcall)
913 #'kill-line arg)))
915 (defun ibuffer-insert-filter-group-before (newgroup group)
916 (let* ((found nil)
917 (pos (let ((groups (mapcar #'car ibuffer-filter-groups))
918 (res 0))
919 (while groups
920 (if (equal (car groups) group)
921 (setq found t
922 groups nil)
923 (cl-incf res)
924 (setq groups (cdr groups))))
925 res)))
926 (cond ((not found)
927 (setq ibuffer-filter-groups
928 (nconc ibuffer-filter-groups (list newgroup))))
929 ((zerop pos)
930 (push newgroup ibuffer-filter-groups))
932 (let ((cell (nthcdr pos ibuffer-filter-groups)))
933 (setf (cdr cell) (cons (car cell) (cdr cell)))
934 (setf (car cell) newgroup))))))
936 ;;;###autoload
937 (defun ibuffer-yank ()
938 "Yank the last killed filter group before group at point."
939 (interactive)
940 (ibuffer-yank-filter-group
941 (or (get-text-property (point) 'ibuffer-filter-group-name)
942 (get-text-property (point) 'ibuffer-filter-group)
943 (error "No filter group at point"))))
945 ;;;###autoload
946 (defun ibuffer-yank-filter-group (name)
947 "Yank the last killed filter group before group named NAME."
948 (interactive (list (ibuffer-read-filter-group-name
949 "Yank filter group before group: ")))
950 (unless ibuffer-filter-group-kill-ring
951 (error "The Ibuffer filter group kill-ring is empty"))
952 (save-excursion
953 (ibuffer-forward-line 0)
954 (ibuffer-insert-filter-group-before (pop ibuffer-filter-group-kill-ring)
955 name))
956 (ibuffer-update nil t))
958 ;;;###autoload
959 (defun ibuffer-save-filter-groups (name groups)
960 "Save all active filter groups GROUPS as NAME.
961 They are added to `ibuffer-saved-filter-groups'. Interactively,
962 prompt for NAME, and use the current filters."
963 (interactive
964 (if (null ibuffer-filter-groups)
965 (error "No filter groups active")
966 (list
967 (read-from-minibuffer "Save current filter groups as: ")
968 ibuffer-filter-groups)))
969 (ibuffer-aif (assoc name ibuffer-saved-filter-groups)
970 (setcdr it groups)
971 (push (cons name groups) ibuffer-saved-filter-groups))
972 (ibuffer-maybe-save-stuff))
974 ;;;###autoload
975 (defun ibuffer-delete-saved-filter-groups (name)
976 "Delete saved filter groups with NAME.
977 They are removed from `ibuffer-saved-filter-groups'."
978 (interactive
979 (list
980 (if (null ibuffer-saved-filter-groups)
981 (error "No saved filter groups")
982 (completing-read "Delete saved filter group: "
983 ibuffer-saved-filter-groups nil t))))
984 (setq ibuffer-saved-filter-groups
985 (ibuffer-remove-alist name ibuffer-saved-filter-groups))
986 (ibuffer-maybe-save-stuff)
987 (ibuffer-update nil t))
989 ;;;###autoload
990 (defun ibuffer-switch-to-saved-filter-groups (name)
991 "Set this buffer's filter groups to saved version with NAME.
992 The value from `ibuffer-saved-filter-groups' is used."
993 (interactive
994 (list
995 (cond ((null ibuffer-saved-filter-groups)
996 (error "No saved filters"))
997 ;; `ibuffer-saved-filter-groups' is a user variable that defaults
998 ;; to nil. We assume that with one element in this list the user
999 ;; knows what she wants. See bug#12331.
1000 ((null (cdr ibuffer-saved-filter-groups))
1001 (caar ibuffer-saved-filter-groups))
1003 (completing-read "Switch to saved filter group: "
1004 ibuffer-saved-filter-groups nil t)))))
1005 (setq ibuffer-filter-groups (cdr (assoc name ibuffer-saved-filter-groups))
1006 ibuffer-hidden-filter-groups nil)
1007 (ibuffer-update nil t))
1009 ;;;###autoload
1010 (defun ibuffer-filter-disable (&optional delete-filter-groups)
1011 "Disable all filters currently in effect in this buffer.
1012 With optional arg DELETE-FILTER-GROUPS non-nil, delete all filter
1013 group definitions by setting `ibuffer-filter-groups' to nil."
1014 (interactive)
1015 (setq ibuffer-filtering-qualifiers nil)
1016 (if delete-filter-groups
1017 (setq ibuffer-filter-groups nil))
1018 (let ((buf (ibuffer-current-buffer)))
1019 (ibuffer-update nil t)
1020 (when buf
1021 (ibuffer-jump-to-buffer (buffer-name buf)))))
1023 ;;;###autoload
1024 (defun ibuffer-pop-filter ()
1025 "Remove the top filter in this buffer."
1026 (interactive)
1027 (when (null ibuffer-filtering-qualifiers)
1028 (error "No filters in effect"))
1029 (pop ibuffer-filtering-qualifiers)
1030 (let ((buf (ibuffer-current-buffer)))
1031 (ibuffer-update nil t)
1032 (when buf
1033 (ibuffer-jump-to-buffer (buffer-name buf)))))
1035 (defun ibuffer-push-filter (filter-specification)
1036 "Add FILTER-SPECIFICATION to `ibuffer-filtering-qualifiers'.
1037 If FILTER-SPECIFICATION is already in the list then return nil. Otherwise,
1038 return the updated list."
1039 (unless (member filter-specification ibuffer-filtering-qualifiers)
1040 (push filter-specification ibuffer-filtering-qualifiers)))
1042 ;;;###autoload
1043 (defun ibuffer-decompose-filter ()
1044 "Separate this buffer's top compound filter (AND, OR, NOT, or SAVED).
1046 This means that the topmost filter on the filtering stack, which must
1047 be a complex filter like (OR [name: foo] [mode: bar-mode]), will be
1048 turned into separate filters, like [name: foo] and [mode: bar-mode]."
1049 (interactive)
1050 (unless ibuffer-filtering-qualifiers
1051 (error "No filters in effect"))
1052 (let* ((filters ibuffer-filtering-qualifiers)
1053 (head (cdar filters))
1054 (tail (cdr filters))
1055 (value
1056 (pcase (caar filters)
1057 ((or `or 'and) (nconc head tail))
1058 (`saved
1059 (let ((data (assoc head ibuffer-saved-filters)))
1060 (unless data
1061 (ibuffer-filter-disable)
1062 (error "Unknown saved filter %s" head))
1063 (append (cdr data) tail)))
1064 (`not (cons (ibuffer-unary-operand (car filters)) tail))
1066 (error "Filter type %s is not compound" (caar filters))))))
1067 (setq ibuffer-filtering-qualifiers value))
1068 (ibuffer-update nil t))
1070 ;;;###autoload
1071 (defun ibuffer-exchange-filters ()
1072 "Exchange the top two filters on the stack in this buffer."
1073 (interactive)
1074 (let ((filters ibuffer-filtering-qualifiers))
1075 (when (< (length filters) 2)
1076 (error "Need two filters to exchange"))
1077 (cl-rotatef (car filters) (cadr filters))
1078 (ibuffer-update nil t)))
1080 ;;;###autoload
1081 (defun ibuffer-negate-filter ()
1082 "Negate the sense of the top filter in the current buffer."
1083 (interactive)
1084 (when (null ibuffer-filtering-qualifiers)
1085 (error "No filters in effect"))
1086 (let ((lim (pop ibuffer-filtering-qualifiers)))
1087 (push (if (eq (car lim) 'not)
1088 (cdr lim)
1089 (cons 'not lim))
1090 ibuffer-filtering-qualifiers))
1091 (ibuffer-update nil t))
1093 (defun ibuffer--or-and-filter (op decompose)
1094 (if decompose
1095 (if (eq op (caar ibuffer-filtering-qualifiers))
1096 (ibuffer-decompose-filter)
1097 (error "Top filter is not an %s" (upcase (symbol-name op))))
1098 (when (< (length ibuffer-filtering-qualifiers) 2)
1099 (error "Need two filters to %s" (upcase (symbol-name op))))
1100 ;; If either filter is an op, eliminate unnecessary nesting.
1101 (let ((first (pop ibuffer-filtering-qualifiers))
1102 (second (pop ibuffer-filtering-qualifiers)))
1103 (push (nconc (if (eq op (car first)) first (list op first))
1104 (if (eq op (car second)) (cdr second) (list second)))
1105 ibuffer-filtering-qualifiers)))
1106 (ibuffer-update nil t))
1108 ;;;###autoload
1109 (defun ibuffer-or-filter (&optional decompose)
1110 "Replace the top two filters in this buffer with their logical OR.
1111 If optional argument DECOMPOSE is non-nil, instead break the top OR
1112 filter into parts."
1113 (interactive "P")
1114 (ibuffer--or-and-filter 'or decompose))
1116 ;;;###autoload
1117 (defun ibuffer-and-filter (&optional decompose)
1118 "Replace the top two filters in this buffer with their logical AND.
1119 If optional argument DECOMPOSE is non-nil, instead break the top AND
1120 filter into parts."
1121 (interactive "P")
1122 (ibuffer--or-and-filter 'and decompose))
1124 (defun ibuffer-maybe-save-stuff ()
1125 (when ibuffer-save-with-custom
1126 (if (fboundp 'customize-save-variable)
1127 (progn
1128 (customize-save-variable 'ibuffer-saved-filters
1129 ibuffer-saved-filters)
1130 (customize-save-variable 'ibuffer-saved-filter-groups
1131 ibuffer-saved-filter-groups))
1132 (message "Not saved permanently: Customize not available"))))
1134 ;;;###autoload
1135 (defun ibuffer-save-filters (name filters)
1136 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'.
1137 Interactively, prompt for NAME, and use the current filters."
1138 (interactive
1139 (if (null ibuffer-filtering-qualifiers)
1140 (error "No filters currently in effect")
1141 (list
1142 (read-from-minibuffer "Save current filters as: ")
1143 ibuffer-filtering-qualifiers)))
1144 (ibuffer-aif (assoc name ibuffer-saved-filters)
1145 (setcdr it filters)
1146 (push (cons name filters) ibuffer-saved-filters))
1147 (ibuffer-maybe-save-stuff))
1149 ;;;###autoload
1150 (defun ibuffer-delete-saved-filters (name)
1151 "Delete saved filters with NAME from `ibuffer-saved-filters'."
1152 (interactive
1153 (list
1154 (if (null ibuffer-saved-filters)
1155 (error "No saved filters")
1156 (completing-read "Delete saved filters: "
1157 ibuffer-saved-filters nil t))))
1158 (setq ibuffer-saved-filters
1159 (ibuffer-remove-alist name ibuffer-saved-filters))
1160 (ibuffer-maybe-save-stuff)
1161 (ibuffer-update nil t))
1163 ;;;###autoload
1164 (defun ibuffer-add-saved-filters (name)
1165 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
1166 (interactive
1167 (list
1168 (if (null ibuffer-saved-filters)
1169 (error "No saved filters")
1170 (completing-read "Add saved filters: "
1171 ibuffer-saved-filters nil t))))
1172 (push (cons 'saved name) ibuffer-filtering-qualifiers)
1173 (ibuffer-update nil t))
1175 ;;;###autoload
1176 (defun ibuffer-switch-to-saved-filters (name)
1177 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'."
1178 (interactive
1179 (list
1180 (if (null ibuffer-saved-filters)
1181 (error "No saved filters")
1182 (completing-read "Switch to saved filters: "
1183 ibuffer-saved-filters nil t))))
1184 (setq ibuffer-filtering-qualifiers (list (cons 'saved name)))
1185 (ibuffer-update nil t))
1187 (defun ibuffer-format-filter-group-data (filter)
1188 (if (equal filter "Default")
1190 (concat "Filter:" (mapconcat #'ibuffer-format-qualifier
1191 (cdr (assq filter ibuffer-filter-groups))
1192 " "))))
1194 (defun ibuffer-format-qualifier (qualifier)
1195 (if (eq (car-safe qualifier) 'not)
1196 (concat " [NOT"
1197 (ibuffer-format-qualifier-1 (ibuffer-unary-operand qualifier))
1198 "]")
1199 (ibuffer-format-qualifier-1 qualifier)))
1201 (defun ibuffer-format-qualifier-1 (qualifier)
1202 (pcase (car qualifier)
1203 (`saved
1204 (concat " [filter: " (cdr qualifier) "]"))
1205 (`or
1206 (concat " [OR" (mapconcat #'ibuffer-format-qualifier
1207 (cdr qualifier) "") "]"))
1208 (`and
1209 (concat " [AND" (mapconcat #'ibuffer-format-qualifier
1210 (cdr qualifier) "") "]"))
1212 (let ((type (assq (car qualifier) ibuffer-filtering-alist)))
1213 (unless qualifier
1214 (error "Ibuffer: bad qualifier %s" qualifier))
1215 (concat " [" (cadr type) ": " (format "%s]" (cdr qualifier)))))))
1217 (defun ibuffer-list-buffer-modes (&optional include-parents)
1218 "Create a completion table of buffer modes currently in use.
1219 If INCLUDE-PARENTS is non-nil then include parent modes."
1220 (let ((modes))
1221 (dolist (buf (buffer-list))
1222 (let ((this-mode (buffer-local-value 'major-mode buf)))
1223 (while (and this-mode (not (memq this-mode modes)))
1224 (push this-mode modes)
1225 (setq this-mode (and include-parents
1226 (get this-mode 'derived-mode-parent))))))
1227 (mapcar #'symbol-name modes)))
1230 ;;; Extra operation definitions
1232 ;;;###autoload (autoload 'ibuffer-filter-by-mode "ibuf-ext")
1233 (define-ibuffer-filter mode
1234 "Limit current view to buffers with major mode QUALIFIER."
1235 (:description "major mode"
1236 :reader
1237 (let* ((buf (ibuffer-current-buffer))
1238 (default (if (and buf (buffer-live-p buf))
1239 (symbol-name (buffer-local-value
1240 'major-mode buf)))))
1241 (intern
1242 (completing-read
1243 (if default
1244 (format "Filter by major mode (default %s): " default)
1245 "Filter by major mode: ")
1246 obarray
1247 #'(lambda (e)
1248 (string-match "-mode\\'" (symbol-name e)))
1249 t nil nil default))))
1250 (eq qualifier (buffer-local-value 'major-mode buf)))
1252 ;;;###autoload (autoload 'ibuffer-filter-by-used-mode "ibuf-ext")
1253 (define-ibuffer-filter used-mode
1254 "Limit current view to buffers with major mode QUALIFIER.
1255 Called interactively, this function allows selection of modes
1256 currently used by buffers."
1257 (:description "major mode in use"
1258 :reader
1259 (let* ((buf (ibuffer-current-buffer))
1260 (default (if (and buf (buffer-live-p buf))
1261 (symbol-name (buffer-local-value
1262 'major-mode buf)))))
1263 (intern
1264 (completing-read
1265 (if default
1266 (format "Filter by major mode (default %s): " default)
1267 "Filter by major mode: ")
1268 (ibuffer-list-buffer-modes) nil t nil nil default))))
1269 (eq qualifier (buffer-local-value 'major-mode buf)))
1271 ;;;###autoload (autoload 'ibuffer-filter-by-derived-mode "ibuf-ext")
1272 (define-ibuffer-filter derived-mode
1273 "Limit current view to buffers whose major mode inherits from QUALIFIER."
1274 (:description "derived mode"
1275 :reader
1276 (intern
1277 (completing-read "Filter by derived mode: "
1278 (ibuffer-list-buffer-modes t)
1279 nil t)))
1280 (with-current-buffer buf (derived-mode-p qualifier)))
1282 ;;;###autoload (autoload 'ibuffer-filter-by-name "ibuf-ext")
1283 (define-ibuffer-filter name
1284 "Limit current view to buffers with name matching QUALIFIER."
1285 (:description "buffer name"
1286 :reader (read-from-minibuffer "Filter by name (regexp): "))
1287 (string-match qualifier (buffer-name buf)))
1289 ;;;###autoload (autoload 'ibuffer-filter-by-process "ibuf-ext")
1290 (define-ibuffer-filter process
1291 "Limit current view to buffers running a process."
1292 (:description "process")
1293 (get-buffer-process buf))
1295 ;;;###autoload (autoload 'ibuffer-filter-by-starred-name "ibuf-ext")
1296 (define-ibuffer-filter starred-name
1297 "Limit current view to buffers with name beginning and ending
1298 with *, along with an optional suffix of the form digits or
1299 <digits>."
1300 (:description "starred buffer name"
1301 :reader nil)
1302 (string-match "\\`\\*[^*]+\\*\\(?:<[[:digit:]]+>\\)?\\'" (buffer-name buf)))
1304 ;;;###autoload (autoload 'ibuffer-filter-by-filename "ibuf-ext")
1305 (define-ibuffer-filter filename
1306 "Limit current view to buffers with full file name matching QUALIFIER.
1308 For example, for a buffer associated with file '/a/b/c.d', this
1309 matches against '/a/b/c.d'."
1310 (:description "full file name"
1311 :reader (read-from-minibuffer "Filter by full file name (regexp): "))
1312 (ibuffer-awhen (with-current-buffer buf (ibuffer-buffer-file-name))
1313 (string-match qualifier it)))
1315 ;;;###autoload (autoload 'ibuffer-filter-by-basename "ibuf-ext")
1316 (define-ibuffer-filter basename
1317 "Limit current view to buffers with file basename matching QUALIFIER.
1319 For example, for a buffer associated with file '/a/b/c.d', this
1320 matches against 'c.d'."
1321 (:description "file basename"
1322 :reader (read-from-minibuffer
1323 "Filter by file name, without directory part (regex): "))
1324 (ibuffer-awhen (with-current-buffer buf (ibuffer-buffer-file-name))
1325 (string-match qualifier (file-name-nondirectory it))))
1327 ;;;###autoload (autoload 'ibuffer-filter-by-file-extension "ibuf-ext")
1328 (define-ibuffer-filter file-extension
1329 "Limit current view to buffers with filename extension matching QUALIFIER.
1331 The separator character (typically `.') is not part of the
1332 pattern. For example, for a buffer associated with file
1333 '/a/b/c.d', this matches against 'd'."
1334 (:description "filename extension"
1335 :reader (read-from-minibuffer
1336 "Filter by filename extension without separator (regex): "))
1337 (ibuffer-awhen (with-current-buffer buf (ibuffer-buffer-file-name))
1338 (string-match qualifier (or (file-name-extension it) ""))))
1340 ;;;###autoload (autoload 'ibuffer-filter-by-directory "ibuf-ext")
1341 (define-ibuffer-filter directory
1342 "Limit current view to buffers with directory matching QUALIFIER.
1344 For a buffer associated with file '/a/b/c.d', this matches
1345 against '/a/b'. For a buffer not associated with a file, this
1346 matches against the value of `default-directory' in that buffer."
1347 (:description "directory name"
1348 :reader (read-from-minibuffer "Filter by directory name (regex): "))
1349 (ibuffer-aif (with-current-buffer buf (ibuffer-buffer-file-name))
1350 (let ((dirname (file-name-directory it)))
1351 (when dirname (string-match qualifier dirname)))
1352 (when default-directory (string-match qualifier default-directory))))
1354 ;;;###autoload (autoload 'ibuffer-filter-by-size-gt "ibuf-ext")
1355 (define-ibuffer-filter size-gt
1356 "Limit current view to buffers with size greater than QUALIFIER."
1357 (:description "size greater than"
1358 :reader
1359 (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
1360 (> (with-current-buffer buf (buffer-size))
1361 qualifier))
1363 ;;;###autoload (autoload 'ibuffer-filter-by-size-lt "ibuf-ext")
1364 (define-ibuffer-filter size-lt
1365 "Limit current view to buffers with size less than QUALIFIER."
1366 (:description "size less than"
1367 :reader
1368 (string-to-number (read-from-minibuffer "Filter by size less than: ")))
1369 (< (with-current-buffer buf (buffer-size))
1370 qualifier))
1372 ;;;###autoload (autoload 'ibuffer-filter-by-modified "ibuf-ext")
1373 (define-ibuffer-filter modified
1374 "Limit current view to buffers that are marked as modified."
1375 (:description "modified"
1376 :reader nil)
1377 (buffer-modified-p buf))
1379 ;;;###autoload (autoload 'ibuffer-filter-by-visiting-file "ibuf-ext")
1380 (define-ibuffer-filter visiting-file
1381 "Limit current view to buffers that are visiting a file."
1382 (:description "visiting a file"
1383 :reader nil)
1384 (with-current-buffer buf (buffer-file-name)))
1386 ;;;###autoload (autoload 'ibuffer-filter-by-content "ibuf-ext")
1387 (define-ibuffer-filter content
1388 "Limit current view to buffers whose contents match QUALIFIER."
1389 (:description "content"
1390 :reader (read-from-minibuffer "Filter by content (regexp): "))
1391 (with-current-buffer buf
1392 (save-excursion
1393 (goto-char (point-min))
1394 (re-search-forward qualifier nil t))))
1396 ;;;###autoload (autoload 'ibuffer-filter-by-predicate "ibuf-ext")
1397 (define-ibuffer-filter predicate
1398 "Limit current view to buffers for which QUALIFIER returns non-nil."
1399 (:description "predicate"
1400 :reader (read-minibuffer "Filter by predicate (form): "))
1401 (with-current-buffer buf
1402 (eval qualifier)))
1404 ;;;###autoload (autoload 'ibuffer-filter-chosen-by-completion "ibuf-ext")
1405 (defun ibuffer-filter-chosen-by-completion ()
1406 "Select and apply filter chosen by completion against available filters.
1407 Indicates corresponding key sequences in echo area after filtering.
1409 The completion matches against the filter description text of
1410 each filter in `ibuffer-filtering-alist'."
1411 (interactive)
1412 (let* ((filters (mapcar (lambda (x) (cons (cadr x) (car x)))
1413 ibuffer-filtering-alist))
1414 (match (completing-read "Filter by: " filters nil t))
1415 (filter (cdr (assoc match filters)))
1416 (command (intern (concat "ibuffer-filter-by-" (symbol-name filter)))))
1417 (call-interactively command)
1418 (message "%s can be run with key sequences: %s"
1419 command
1420 (mapconcat #'key-description
1421 (where-is-internal command ibuffer-mode-map nil t)
1422 "or "))))
1425 ;;; Sorting
1427 ;;;###autoload
1428 (defun ibuffer-toggle-sorting-mode ()
1429 "Toggle the current sorting mode.
1430 Default sorting modes are:
1431 Recency - the last time the buffer was viewed
1432 Name - the name of the buffer
1433 Major Mode - the name of the major mode of the buffer
1434 Size - the size of the buffer"
1435 (interactive)
1436 (let ((modes (mapcar #'car ibuffer-sorting-functions-alist)))
1437 (cl-pushnew 'recency modes)
1438 (setq modes (sort modes #'string-lessp))
1439 (let ((next (or (car-safe (cdr-safe (memq ibuffer-sorting-mode modes)))
1440 (car modes))))
1441 (setq ibuffer-sorting-mode next)
1442 (message "Sorting by %s" next)))
1443 (ibuffer-redisplay t))
1445 ;;;###autoload
1446 (defun ibuffer-invert-sorting ()
1447 "Toggle whether or not sorting is in reverse order."
1448 (interactive)
1449 (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep))
1450 (message "Sorting order %s"
1451 (if ibuffer-sorting-reversep
1452 "reversed"
1453 "normal"))
1454 (ibuffer-redisplay t))
1456 ;;;###autoload (autoload 'ibuffer-do-sort-by-major-mode "ibuf-ext")
1457 (define-ibuffer-sorter major-mode
1458 "Sort the buffers by major modes.
1459 Ordering is lexicographic."
1460 (:description "major mode")
1461 (string-lessp (downcase
1462 (symbol-name (buffer-local-value 'major-mode (car a))))
1463 (downcase
1464 (symbol-name (buffer-local-value 'major-mode (car b))))))
1466 ;;;###autoload (autoload 'ibuffer-do-sort-by-mode-name "ibuf-ext")
1467 (define-ibuffer-sorter mode-name
1468 "Sort the buffers by their mode name.
1469 Ordering is lexicographic."
1470 (:description "major mode name")
1471 (string-lessp (downcase
1472 (with-current-buffer
1473 (car a)
1474 (format-mode-line mode-name)))
1475 (downcase
1476 (with-current-buffer
1477 (car b)
1478 (format-mode-line mode-name)))))
1480 ;;;###autoload (autoload 'ibuffer-do-sort-by-alphabetic "ibuf-ext")
1481 (define-ibuffer-sorter alphabetic
1482 "Sort the buffers by their names.
1483 Ordering is lexicographic."
1484 (:description "buffer name")
1485 (string-lessp
1486 (buffer-name (car a))
1487 (buffer-name (car b))))
1489 ;;;###autoload (autoload 'ibuffer-do-sort-by-size "ibuf-ext")
1490 (define-ibuffer-sorter size
1491 "Sort the buffers by their size."
1492 (:description "size")
1493 (< (with-current-buffer (car a)
1494 (buffer-size))
1495 (with-current-buffer (car b)
1496 (buffer-size))))
1498 ;;;###autoload (autoload 'ibuffer-do-sort-by-filename/process "ibuf-ext")
1499 (define-ibuffer-sorter filename/process
1500 "Sort the buffers by their file name/process name."
1501 (:description "file name")
1502 (string-lessp
1503 ;; FIXME: For now just compare the file name and the process name
1504 ;; (if it exists). Is there a better way to do this?
1505 (or (buffer-file-name (car a))
1506 (let ((pr-a (get-buffer-process (car a))))
1507 (and (processp pr-a) (process-name pr-a))))
1508 (or (buffer-file-name (car b))
1509 (let ((pr-b (get-buffer-process (car b))))
1510 (and (processp pr-b) (process-name pr-b))))))
1512 ;;; Functions to emulate bs.el
1514 ;;;###autoload
1515 (defun ibuffer-bs-show ()
1516 "Emulate `bs-show' from the bs.el package."
1517 (interactive)
1518 (ibuffer t "*Ibuffer-bs*" '((filename . ".*")) nil t)
1519 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all))
1521 (defun ibuffer-bs-toggle-all ()
1522 "Emulate `bs-toggle-show-all' from the bs.el package."
1523 (interactive)
1524 (if ibuffer-filtering-qualifiers
1525 (ibuffer-pop-filter)
1526 (progn (ibuffer-push-filter '(filename . ".*"))
1527 (ibuffer-update nil t))))
1529 ;;; Handy functions
1531 ;;;###autoload
1532 (defun ibuffer-add-to-tmp-hide (regexp)
1533 "Add REGEXP to `ibuffer-tmp-hide-regexps'.
1534 This means that buffers whose name matches REGEXP will not be shown
1535 for this Ibuffer session."
1536 (interactive
1537 (list
1538 (read-from-minibuffer "Never show buffers matching: "
1539 (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
1540 (push regexp ibuffer-tmp-hide-regexps))
1542 ;;;###autoload
1543 (defun ibuffer-add-to-tmp-show (regexp)
1544 "Add REGEXP to `ibuffer-tmp-show-regexps'.
1545 This means that buffers whose name matches REGEXP will always be shown
1546 for this Ibuffer session."
1547 (interactive
1548 (list
1549 (read-from-minibuffer "Always show buffers matching: "
1550 (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
1551 (push regexp ibuffer-tmp-show-regexps))
1553 ;;;###autoload
1554 (defun ibuffer-forward-next-marked (&optional count mark direction)
1555 "Move forward by COUNT marked buffers (default 1).
1557 If MARK is non-nil, it should be a character denoting the type of mark
1558 to move by. The default is `ibuffer-marked-char'.
1560 If DIRECTION is non-nil, it should be an integer; negative integers
1561 mean move backwards, non-negative integers mean move forwards."
1562 (interactive "P")
1563 (unless count
1564 (setq count 1))
1565 (unless mark
1566 (setq mark ibuffer-marked-char))
1567 (unless direction
1568 (setq direction 1))
1569 ;; Skip the title
1570 (ibuffer-forward-line 0)
1571 (let ((opos (point)))
1572 (ibuffer-forward-line direction)
1573 (while (not (or (= (point) opos)
1574 (eq (ibuffer-current-mark) mark)))
1575 (ibuffer-forward-line direction))
1576 (when (and (= (point) opos)
1577 (not (eq (ibuffer-current-mark) mark)))
1578 (error "No buffers with mark %c" mark))))
1580 ;;;###autoload
1581 (defun ibuffer-backwards-next-marked (&optional count mark)
1582 "Move backwards by COUNT marked buffers (default 1).
1584 If MARK is non-nil, it should be a character denoting the type of mark
1585 to move by. The default is `ibuffer-marked-char'."
1586 (interactive "P")
1587 (ibuffer-forward-next-marked count mark -1))
1589 ;;;###autoload
1590 (defun ibuffer-do-kill-lines ()
1591 "Hide all of the currently marked lines."
1592 (interactive)
1593 (if (= (ibuffer-count-marked-lines) 0)
1594 (message "No buffers marked; use 'm' to mark a buffer")
1595 (let ((count
1596 (ibuffer-map-marked-lines
1597 #'(lambda (_buf _mark)
1598 'kill))))
1599 (message "Killed %s lines" count))))
1601 ;;;###autoload
1602 (defun ibuffer-jump-to-buffer (name)
1603 "Move point to the buffer whose name is NAME.
1605 If called interactively, prompt for a buffer name and go to the
1606 corresponding line in the Ibuffer buffer. If said buffer is in a
1607 hidden group filter, open it.
1609 If `ibuffer-jump-offer-only-visible-buffers' is non-nil, only offer
1610 visible buffers in the completion list. Calling the command with
1611 a prefix argument reverses the meaning of that variable."
1612 (interactive (list
1613 (let ((only-visible ibuffer-jump-offer-only-visible-buffers))
1614 (when current-prefix-arg
1615 (setq only-visible (not only-visible)))
1616 (if only-visible
1617 (let ((table (mapcar #'(lambda (x)
1618 (buffer-name (car x)))
1619 (ibuffer-current-state-list))))
1620 (when (null table)
1621 (error "No buffers!"))
1622 (completing-read "Jump to buffer: "
1623 table nil t))
1624 (read-buffer "Jump to buffer: " nil t)))))
1625 (when (not (string= "" name))
1626 (let (buf-point)
1627 ;; Blindly search for our buffer: it is very likely that it is
1628 ;; not in a hidden filter group.
1629 (ibuffer-map-lines #'(lambda (buf _marks)
1630 (when (string= (buffer-name buf) name)
1631 (setq buf-point (point))
1632 nil))
1633 t nil)
1634 (when (and
1635 (null buf-point)
1636 (not (null ibuffer-hidden-filter-groups)))
1637 ;; We did not find our buffer. It must be in a hidden filter
1638 ;; group, so go through all hidden filter groups to find it.
1639 (catch 'found
1640 (dolist (group ibuffer-hidden-filter-groups)
1641 (ibuffer-jump-to-filter-group group)
1642 (ibuffer-toggle-filter-group)
1643 (ibuffer-map-lines #'(lambda (buf _marks)
1644 (when (string= (buffer-name buf) name)
1645 (setq buf-point (point))
1646 nil))
1647 t group)
1648 (if buf-point
1649 (throw 'found nil)
1650 (ibuffer-toggle-filter-group)))))
1651 (if (null buf-point)
1652 ;; Still not found even though we expanded all hidden filter
1653 ;; groups: that must be because it's hidden by predicate:
1654 ;; we won't bother trying to display it.
1655 (error "No buffer with name %s" name)
1656 (goto-char buf-point)))))
1658 (declare-function diff-sentinel "diff"
1659 (code &optional old-temp-file new-temp-file))
1661 (defun ibuffer-diff-buffer-with-file-1 (buffer)
1662 (let ((bufferfile (buffer-local-value 'buffer-file-name buffer))
1663 (tempfile (make-temp-file "buffer-content-")))
1664 (when bufferfile
1665 (unwind-protect
1666 (progn
1667 (with-current-buffer buffer
1668 (write-region nil nil tempfile nil 'nomessage))
1669 (let* ((old (expand-file-name bufferfile))
1670 (new (expand-file-name tempfile))
1671 (oldtmp (file-local-copy old))
1672 (newtmp (file-local-copy new))
1673 (switches diff-switches)
1674 (command
1675 (mapconcat
1676 'identity
1677 `(,diff-command
1678 ;; Use explicitly specified switches
1679 ,@(if (listp switches) switches (list switches))
1680 ,@(if (or old new)
1681 (list "-L" (shell-quote-argument old)
1682 "-L" (shell-quote-argument
1683 (format "Buffer %s" (buffer-name buffer)))))
1684 ,(shell-quote-argument (or oldtmp old))
1685 ,(shell-quote-argument (or newtmp new)))
1686 " ")))
1687 (let ((inhibit-read-only t))
1688 (insert command "\n")
1689 (diff-sentinel
1690 (call-process shell-file-name nil
1691 (current-buffer) nil
1692 shell-command-switch command))
1693 (insert "\n")))))
1694 (sit-for 0)
1695 (when (file-exists-p tempfile)
1696 (delete-file tempfile)))))
1698 ;;;###autoload
1699 (defun ibuffer-diff-with-file ()
1700 "View the differences between marked buffers and their associated files.
1701 If no buffers are marked, use buffer at point.
1702 This requires the external program \"diff\" to be in your `exec-path'."
1703 (interactive)
1704 (require 'diff)
1705 (let ((marked-bufs (ibuffer-get-marked-buffers)))
1706 (when (null marked-bufs)
1707 (setq marked-bufs (list (ibuffer-current-buffer t))))
1708 (with-current-buffer (get-buffer-create "*Ibuffer Diff*")
1709 (setq buffer-read-only nil)
1710 (buffer-disable-undo (current-buffer))
1711 (erase-buffer)
1712 (buffer-enable-undo (current-buffer))
1713 (diff-mode)
1714 (dolist (buf marked-bufs)
1715 (unless (buffer-live-p buf)
1716 (error "Buffer %s has been killed" buf))
1717 (ibuffer-diff-buffer-with-file-1 buf))
1718 (setq buffer-read-only t)))
1719 (switch-to-buffer "*Ibuffer Diff*"))
1721 ;;;###autoload
1722 (defun ibuffer-copy-filename-as-kill (&optional arg)
1723 "Copy filenames of marked (or next ARG) buffers into the kill ring.
1725 The names are separated by a space.
1726 If a buffer has no filename, it is ignored.
1728 With no prefix arg, use the filename sans its directory of each marked file.
1729 With a zero prefix arg, use the complete filename of each marked file.
1730 With \\[universal-argument], use the filename of each marked file relative
1731 to `ibuffer-default-directory' if non-nil, otherwise `default-directory'.
1733 You can then feed the file name(s) to other commands with \\[yank]."
1734 (interactive "P")
1735 (let* ((buffers (cond ((and (integerp arg) (not (zerop arg)))
1736 (ibuffer--near-buffers arg))
1738 (or (ibuffer-get-marked-buffers)
1739 (list (ibuffer-current-buffer))))))
1740 (file-names
1741 (mapcar
1742 (lambda (buf)
1743 (let ((name (with-current-buffer buf
1744 (ibuffer-buffer-file-name))))
1745 (if (null name)
1747 (cond ((and (integerp arg) (zerop arg)) name)
1748 ((consp arg)
1749 (file-relative-name
1750 name (or ibuffer-default-directory
1751 default-directory)))
1752 (t (file-name-nondirectory name))))))
1753 buffers))
1754 (string
1755 (mapconcat 'identity (delete "" file-names) " ")))
1756 (unless (string= string "")
1757 (if (eq last-command 'kill-region)
1758 (kill-append string nil)
1759 (kill-new string))
1760 (message "%s" string))))
1762 ;;;###autoload
1763 (defun ibuffer-copy-buffername-as-kill (&optional arg)
1764 "Copy buffer names of marked (or next ARG) buffers into the kill ring.
1765 The names are separated by a space.
1766 You can then feed the file name(s) to other commands with \\[yank]."
1767 (interactive "P")
1768 (let* ((buffers (cond ((and (integerp arg) (not (zerop arg)))
1769 (ibuffer--near-buffers arg))
1771 (or (ibuffer-get-marked-buffers)
1772 (list (ibuffer-current-buffer))))))
1773 (string (mapconcat #'buffer-name buffers " ")))
1774 (unless (string= string "")
1775 (if (eq last-command 'kill-region)
1776 (kill-append string nil)
1777 (kill-new string))
1778 (message "%s" string))))
1780 (defun ibuffer-mark-on-buffer (func &optional ibuffer-mark-on-buffer-mark group)
1781 (let ((count
1782 (ibuffer-map-lines
1783 #'(lambda (buf _mark)
1784 (when (funcall func buf)
1785 (ibuffer-set-mark-1 (or ibuffer-mark-on-buffer-mark
1786 ibuffer-marked-char))
1789 group)))
1790 (ibuffer-redisplay t)
1791 (unless (eq ibuffer-mark-on-buffer-mark ?\s)
1792 (message "Marked %s buffers" count))))
1794 ;;;###autoload
1795 (defun ibuffer-mark-by-name-regexp (regexp)
1796 "Mark all buffers whose name matches REGEXP."
1797 (interactive "sMark by name (regexp): ")
1798 (ibuffer-mark-on-buffer
1799 #'(lambda (buf)
1800 (string-match regexp (buffer-name buf)))))
1802 (defun ibuffer-locked-buffer-p (&optional buf)
1803 "Return non-nil if BUF is locked.
1804 When BUF nil, default to the buffer at current line."
1805 (let ((cbuffer (or buf (ibuffer-current-buffer))))
1806 (when cbuffer
1807 (with-current-buffer cbuffer
1808 (and (boundp 'emacs-lock-mode) emacs-lock-mode)))))
1810 ;;;###autoload
1811 (defun ibuffer-mark-by-locked ()
1812 "Mark all locked buffers."
1813 (interactive)
1814 (when (featurep 'emacs-lock)
1815 (ibuffer-mark-on-buffer
1816 (lambda (buf)
1817 (ibuffer-locked-buffer-p buf)))))
1819 ;;;###autoload
1820 (defun ibuffer-mark-by-mode-regexp (regexp)
1821 "Mark all buffers whose major mode matches REGEXP."
1822 (interactive "sMark by major mode (regexp): ")
1823 (ibuffer-mark-on-buffer
1824 #'(lambda (buf)
1825 (with-current-buffer buf
1826 (string-match regexp (format-mode-line mode-name nil nil buf))))))
1828 ;;;###autoload
1829 (defun ibuffer-mark-by-file-name-regexp (regexp)
1830 "Mark all buffers whose file name matches REGEXP."
1831 (interactive "sMark by file name (regexp): ")
1832 (ibuffer-mark-on-buffer
1833 #'(lambda (buf)
1834 (let ((name (or (buffer-file-name buf)
1835 (with-current-buffer buf
1836 (and
1837 (boundp 'dired-directory)
1838 (stringp dired-directory)
1839 dired-directory)))))
1840 (when name
1841 (string-match regexp name))))))
1843 ;;;###autoload
1844 (defun ibuffer-mark-by-content-regexp (regexp &optional all-buffers)
1845 "Mark all buffers whose content matches REGEXP.
1846 Optional arg ALL-BUFFERS, if non-nil, then search in all buffers.
1847 Otherwise buffers whose name matches an element of
1848 `ibuffer-never-search-content-name' or whose major mode is on
1849 `ibuffer-never-search-content-mode' are excluded."
1850 (interactive (let ((reg (read-string "Mark by content (regexp): ")))
1851 (list reg current-prefix-arg)))
1852 (ibuffer-mark-on-buffer
1853 #'(lambda (buf)
1854 (let ((mode (with-current-buffer buf major-mode))
1855 res)
1856 (cond ((and (not all-buffers)
1858 (memq mode ibuffer-never-search-content-mode)
1859 (cl-some (lambda (x) (string-match x (buffer-name buf)))
1860 ibuffer-never-search-content-name)))
1861 (setq res nil))
1863 (with-current-buffer buf
1864 (save-mark-and-excursion
1865 (goto-char (point-min))
1866 (setq res (re-search-forward regexp nil t)))))) res))))
1868 ;;;###autoload
1869 (defun ibuffer-mark-by-mode (mode)
1870 "Mark all buffers whose major mode equals MODE."
1871 (interactive
1872 (let* ((buf (ibuffer-current-buffer))
1873 (default (if (and buf (buffer-live-p buf))
1874 (symbol-name (buffer-local-value
1875 'major-mode buf)))))
1876 (list (intern
1877 (completing-read
1878 (if default
1879 (format "Mark by major mode (default %s): " default)
1880 "Mark by major mode: ")
1881 (ibuffer-list-buffer-modes) nil t nil nil default)))))
1882 (ibuffer-mark-on-buffer
1883 #'(lambda (buf)
1884 (eq (buffer-local-value 'major-mode buf) mode))))
1886 ;;;###autoload
1887 (defun ibuffer-mark-modified-buffers ()
1888 "Mark all modified buffers."
1889 (interactive)
1890 (ibuffer-mark-on-buffer
1891 #'(lambda (buf) (buffer-modified-p buf))))
1893 ;;;###autoload
1894 (defun ibuffer-mark-unsaved-buffers ()
1895 "Mark all modified buffers that have an associated file."
1896 (interactive)
1897 (ibuffer-mark-on-buffer
1898 #'(lambda (buf) (and (buffer-local-value 'buffer-file-name buf)
1899 (buffer-modified-p buf)))))
1901 ;;;###autoload
1902 (defun ibuffer-mark-dissociated-buffers ()
1903 "Mark all buffers whose associated file does not exist."
1904 (interactive)
1905 (ibuffer-mark-on-buffer
1906 #'(lambda (buf)
1907 (with-current-buffer buf
1909 (and buffer-file-name
1910 (not (file-exists-p buffer-file-name)))
1911 (and (eq major-mode 'dired-mode)
1912 (boundp 'dired-directory)
1913 (stringp dired-directory)
1914 (not (file-exists-p (file-name-directory dired-directory)))))))))
1916 ;;;###autoload
1917 (defun ibuffer-mark-help-buffers ()
1918 "Mark buffers whose major mode is in variable `ibuffer-help-buffer-modes'."
1919 (interactive)
1920 (ibuffer-mark-on-buffer
1921 #'(lambda (buf)
1922 (with-current-buffer buf
1923 (memq major-mode ibuffer-help-buffer-modes)))))
1925 ;;;###autoload
1926 (defun ibuffer-mark-compressed-file-buffers ()
1927 "Mark buffers whose associated file is compressed."
1928 (interactive)
1929 (ibuffer-mark-on-buffer
1930 #'(lambda (buf)
1931 (with-current-buffer buf
1932 (and buffer-file-name
1933 (string-match ibuffer-compressed-file-name-regexp
1934 buffer-file-name))))))
1936 ;;;###autoload
1937 (defun ibuffer-mark-old-buffers ()
1938 "Mark buffers which have not been viewed in `ibuffer-old-time' hours."
1939 (interactive)
1940 (ibuffer-mark-on-buffer
1941 #'(lambda (buf)
1942 (with-current-buffer buf
1943 ;; hacked from midnight.el
1944 (when buffer-display-time
1945 (let* ((now (float-time))
1946 (then (float-time buffer-display-time)))
1947 (> (- now then) (* 60 60 ibuffer-old-time))))))))
1949 ;;;###autoload
1950 (defun ibuffer-mark-special-buffers ()
1951 "Mark all buffers whose name begins and ends with `*'."
1952 (interactive)
1953 (ibuffer-mark-on-buffer
1954 #'(lambda (buf) (string-match "^\\*.+\\*$"
1955 (buffer-name buf)))))
1957 ;;;###autoload
1958 (defun ibuffer-mark-read-only-buffers ()
1959 "Mark all read-only buffers."
1960 (interactive)
1961 (ibuffer-mark-on-buffer
1962 #'(lambda (buf) (buffer-local-value 'buffer-read-only buf))))
1964 ;;;###autoload
1965 (defun ibuffer-mark-dired-buffers ()
1966 "Mark all `dired' buffers."
1967 (interactive)
1968 (ibuffer-mark-on-buffer
1969 #'(lambda (buf) (eq (buffer-local-value 'major-mode buf) 'dired-mode))))
1971 ;;;###autoload
1972 (defun ibuffer-do-occur (regexp &optional nlines)
1973 "View lines which match REGEXP in all marked buffers.
1974 Optional argument NLINES says how many lines of context to display: it
1975 defaults to one."
1976 (interactive (occur-read-primary-args))
1977 (if (or (not (integerp nlines))
1978 (< nlines 0))
1979 (setq nlines 0))
1980 (when (zerop (ibuffer-count-marked-lines))
1981 (ibuffer-set-mark ibuffer-marked-char))
1982 (let ((ibuffer-do-occur-bufs nil))
1983 ;; Accumulate a list of marked buffers
1984 (ibuffer-map-marked-lines
1985 #'(lambda (buf _mark)
1986 (push buf ibuffer-do-occur-bufs)))
1987 (occur-1 regexp nlines ibuffer-do-occur-bufs)))
1989 (provide 'ibuf-ext)
1991 ;; Local Variables:
1992 ;; generated-autoload-file: "ibuffer-loaddefs.el"
1993 ;; End:
1995 ;;; ibuf-ext.el ends here