Fix the test suite
[emacs.git] / lisp / ibuf-ext.el
blob5ef074697722106adeeac11848d6fc48142cdb5c
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))
40 ;;; Utility functions
41 (defun ibuffer-delete-alist (key alist)
42 "Delete all entries in ALIST that have a key equal to KEY."
43 (let (entry)
44 (while (setq entry (assoc key alist))
45 (setq alist (delete entry alist)))
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 (defcustom ibuffer-saved-filters '(("gnus"
123 ((or (mode . message-mode)
124 (mode . mail-mode)
125 (mode . gnus-group-mode)
126 (mode . gnus-summary-mode)
127 (mode . gnus-article-mode))))
128 ("programming"
129 ((or (mode . emacs-lisp-mode)
130 (mode . cperl-mode)
131 (mode . c-mode)
132 (mode . java-mode)
133 (mode . idl-mode)
134 (mode . lisp-mode)))))
136 "An alist of filter qualifiers to switch between.
138 This variable should look like ((\"STRING\" QUALIFIERS)
139 (\"STRING\" QUALIFIERS) ...), where
140 QUALIFIERS is a list of the same form as
141 `ibuffer-filtering-qualifiers'.
142 See also the variables `ibuffer-filtering-qualifiers',
143 `ibuffer-filtering-alist', and the functions
144 `ibuffer-switch-to-saved-filters', `ibuffer-save-filters'."
145 :type '(repeat sexp)
146 :group 'ibuffer)
148 (defvar ibuffer-filtering-qualifiers nil
149 "A list like (SYMBOL . QUALIFIER) which filters the current buffer list.
150 See also `ibuffer-filtering-alist'.")
152 ;; This is now frobbed by `define-ibuffer-filter'.
153 (defvar ibuffer-filtering-alist nil
154 "An alist of (SYMBOL DESCRIPTION FUNCTION) which describes a filter.
156 You most likely do not want to modify this variable directly; see
157 `define-ibuffer-filter'.
159 SYMBOL is the symbolic name of the filter. DESCRIPTION is used when
160 displaying information to the user. FUNCTION is given a buffer and
161 the value of the qualifier, and returns non-nil if and only if the
162 buffer should be displayed.")
164 (defcustom ibuffer-filter-format-alist nil
165 "An alist which has special formats used when a filter is active.
166 The contents of this variable should look like:
167 ((FILTER (FORMAT FORMAT ...)) (FILTER (FORMAT FORMAT ...)) ...)
169 For example, suppose that when you add a filter for buffers whose
170 major mode is `emacs-lisp-mode', you only want to see the mark and the
171 name of the buffer. You could accomplish that by adding:
172 (mode ((mark \" \" name)))
173 to this variable."
174 :type '(repeat (list :tag "Association" (symbol :tag "Filter")
175 (list :tag "Formats" (repeat (sexp :tag "Format")))))
176 :group 'ibuffer)
178 (defvar ibuffer-cached-filter-formats nil)
179 (defvar ibuffer-compiled-filter-formats nil)
181 (defvar ibuffer-filter-groups nil
182 "A list like ((\"NAME\" ((SYMBOL . QUALIFIER) ...) ...) which groups buffers.
183 The SYMBOL should be one from `ibuffer-filtering-alist'.
184 The QUALIFIER should be the same as QUALIFIER in
185 `ibuffer-filtering-qualifiers'.")
187 (defcustom ibuffer-show-empty-filter-groups t
188 "If non-nil, then show the names of filter groups which are empty."
189 :type 'boolean
190 :group 'ibuffer)
192 (defcustom ibuffer-saved-filter-groups nil
193 "An alist of filtering groups to switch between.
195 This variable should look like ((\"STRING\" QUALIFIERS)
196 (\"STRING\" QUALIFIERS) ...), where
197 QUALIFIERS is a list of the same form as
198 `ibuffer-filtering-qualifiers'.
200 See also the variables `ibuffer-filter-groups',
201 `ibuffer-filtering-qualifiers', `ibuffer-filtering-alist', and the
202 functions `ibuffer-switch-to-saved-filter-groups',
203 `ibuffer-save-filter-groups'."
204 :type '(repeat sexp)
205 :group 'ibuffer)
207 (defvar ibuffer-hidden-filter-groups nil
208 "A list of filtering groups which are currently hidden.")
210 (defvar ibuffer-filter-group-kill-ring nil)
212 (defcustom ibuffer-old-time 72
213 "The number of hours before a buffer is considered \"old\"."
214 :type '(choice (const :tag "72 hours (3 days)" 72)
215 (const :tag "48 hours (2 days)" 48)
216 (const :tag "24 hours (1 day)" 24)
217 (integer :tag "hours"))
218 :group 'ibuffer)
220 (defcustom ibuffer-save-with-custom t
221 "If non-nil, then use Custom to save interactively changed variables.
222 Currently, this only applies to `ibuffer-saved-filters' and
223 `ibuffer-saved-filter-groups'."
224 :type 'boolean
225 :group 'ibuffer)
227 (defun ibuffer-ext-visible-p (buf all &optional ibuffer-buf)
229 (ibuffer-buf-matches-predicates buf ibuffer-tmp-show-regexps)
230 (and (not
232 (ibuffer-buf-matches-predicates buf ibuffer-tmp-hide-regexps)
233 (ibuffer-buf-matches-predicates buf ibuffer-never-show-predicates)))
234 (or all
235 (not
236 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates)))
237 (or ibuffer-view-ibuffer
238 (and ibuffer-buf
239 (not (eq ibuffer-buf buf))))
241 (ibuffer-included-in-filters-p buf ibuffer-filtering-qualifiers)
242 (ibuffer-buf-matches-predicates buf ibuffer-always-show-predicates)))))
244 ;;;###autoload
245 (define-minor-mode ibuffer-auto-mode
246 "Toggle use of Ibuffer's auto-update facility (Ibuffer Auto mode).
247 With a prefix argument ARG, enable Ibuffer Auto mode if ARG is
248 positive, and disable it otherwise. If called from Lisp, enable
249 the mode if ARG is omitted or nil."
250 nil nil nil
251 (unless (derived-mode-p 'ibuffer-mode)
252 (error "This buffer is not in Ibuffer mode"))
253 (cond (ibuffer-auto-mode
254 (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed) ; Initialize state vector
255 (add-hook 'post-command-hook 'ibuffer-auto-update-changed))
257 (remove-hook 'post-command-hook 'ibuffer-auto-update-changed))))
259 (defun ibuffer-auto-update-changed ()
260 (when (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed)
261 (dolist (buf (buffer-list))
262 (ignore-errors
263 (with-current-buffer buf
264 (when (and ibuffer-auto-mode
265 (derived-mode-p 'ibuffer-mode))
266 (ibuffer-update nil t)))))))
268 ;;;###autoload
269 (defun ibuffer-mouse-filter-by-mode (event)
270 "Enable or disable filtering by the major mode chosen via mouse."
271 (interactive "e")
272 (ibuffer-interactive-filter-by-mode event))
274 ;;;###autoload
275 (defun ibuffer-interactive-filter-by-mode (event-or-point)
276 "Enable or disable filtering by the major mode at point."
277 (interactive "d")
278 (if (eventp event-or-point)
279 (posn-set-point (event-end event-or-point))
280 (goto-char event-or-point))
281 (let ((buf (ibuffer-current-buffer)))
282 (if (assq 'mode ibuffer-filtering-qualifiers)
283 (setq ibuffer-filtering-qualifiers
284 (ibuffer-delete-alist 'mode ibuffer-filtering-qualifiers))
285 (ibuffer-push-filter (cons 'mode (buffer-local-value 'major-mode buf)))))
286 (ibuffer-update nil t))
288 ;;;###autoload
289 (defun ibuffer-mouse-toggle-filter-group (event)
290 "Toggle the display status of the filter group chosen with the mouse."
291 (interactive "e")
292 (ibuffer-toggle-filter-group-1 (save-excursion
293 (mouse-set-point event)
294 (point))))
296 ;;;###autoload
297 (defun ibuffer-toggle-filter-group ()
298 "Toggle the display status of the filter group on this line."
299 (interactive)
300 (ibuffer-toggle-filter-group-1 (point)))
302 (defun ibuffer-toggle-filter-group-1 (posn)
303 (let ((name (get-text-property posn 'ibuffer-filter-group-name)))
304 (unless (stringp name)
305 (error "No filtering group name present"))
306 (if (member name ibuffer-hidden-filter-groups)
307 (setq ibuffer-hidden-filter-groups
308 (delete name ibuffer-hidden-filter-groups))
309 (push name ibuffer-hidden-filter-groups))
310 (ibuffer-update nil t)))
312 ;;;###autoload
313 (defun ibuffer-forward-filter-group (&optional count)
314 "Move point forwards by COUNT filtering groups."
315 (interactive "P")
316 (unless count
317 (setq count 1))
318 (when (> count 0)
319 (when (get-text-property (point) 'ibuffer-filter-group-name)
320 (goto-char (next-single-property-change
321 (point) 'ibuffer-filter-group-name
322 nil (point-max))))
323 (goto-char (next-single-property-change
324 (point) 'ibuffer-filter-group-name
325 nil (point-max)))
326 (ibuffer-forward-filter-group (1- count)))
327 (ibuffer-forward-line 0))
329 ;;;###autoload
330 (defun ibuffer-backward-filter-group (&optional count)
331 "Move point backwards by COUNT filtering groups."
332 (interactive "P")
333 (unless count
334 (setq count 1))
335 (when (> count 0)
336 (when (get-text-property (point) 'ibuffer-filter-group-name)
337 (goto-char (previous-single-property-change
338 (point) 'ibuffer-filter-group-name
339 nil (point-min))))
340 (goto-char (previous-single-property-change
341 (point) 'ibuffer-filter-group-name
342 nil (point-min)))
343 (ibuffer-backward-filter-group (1- count)))
344 (when (= (point) (point-min))
345 (goto-char (point-max))
346 (ibuffer-backward-filter-group 1))
347 (ibuffer-forward-line 0))
349 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe "ibuf-ext")
350 (define-ibuffer-op shell-command-pipe (command)
351 "Pipe the contents of each marked buffer to shell command COMMAND."
352 (:interactive "sPipe to shell command: "
353 :opstring "Shell command executed on"
354 :modifier-p nil)
355 (shell-command-on-region
356 (point-min) (point-max) command))
358 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe-replace "ibuf-ext")
359 (define-ibuffer-op shell-command-pipe-replace (command)
360 "Replace the contents of marked buffers with output of pipe to COMMAND."
361 (:interactive "sPipe to shell command (replace): "
362 :opstring "Buffer contents replaced in"
363 :active-opstring "replace buffer contents in"
364 :dangerous t
365 :modifier-p t)
366 (with-current-buffer buf
367 (shell-command-on-region (point-min) (point-max)
368 command nil t)))
370 ;;;###autoload (autoload 'ibuffer-do-shell-command-file "ibuf-ext")
371 (define-ibuffer-op shell-command-file (command)
372 "Run shell command COMMAND separately on files of marked buffers."
373 (:interactive "sShell command on buffer's file: "
374 :opstring "Shell command executed on"
375 :modifier-p nil)
376 (shell-command (concat command " "
377 (shell-quote-argument
378 (or buffer-file-name
379 (let ((file
380 (make-temp-file
381 (substring
382 (buffer-name) 0
383 (min 10 (length (buffer-name)))))))
384 (write-region nil nil file nil 0)
385 file))))))
387 ;;;###autoload (autoload 'ibuffer-do-eval "ibuf-ext")
388 (define-ibuffer-op eval (form)
389 "Evaluate FORM in each of the buffers.
390 Does not display the buffer during evaluation. See
391 `ibuffer-do-view-and-eval' for that."
392 (:interactive
393 (list
394 (read-from-minibuffer
395 "Eval in buffers (form): "
396 nil read-expression-map t 'read-expression-history))
397 :opstring "evaluated in"
398 :modifier-p :maybe)
399 (eval form))
401 ;;;###autoload (autoload 'ibuffer-do-view-and-eval "ibuf-ext")
402 (define-ibuffer-op view-and-eval (form)
403 "Evaluate FORM while displaying each of the marked buffers.
404 To evaluate a form without viewing the buffer, see `ibuffer-do-eval'."
405 (:interactive
406 (list
407 (read-from-minibuffer
408 "Eval viewing in buffers (form): "
409 nil read-expression-map t 'read-expression-history))
410 :opstring "evaluated in"
411 :complex t
412 :modifier-p :maybe)
413 (let ((ibuffer-buf (current-buffer)))
414 (unwind-protect
415 (progn
416 (switch-to-buffer buf)
417 (eval form))
418 (switch-to-buffer ibuffer-buf))))
420 ;;;###autoload (autoload 'ibuffer-do-rename-uniquely "ibuf-ext")
421 (define-ibuffer-op rename-uniquely ()
422 "Rename marked buffers as with `rename-uniquely'."
423 (:opstring "renamed"
424 :modifier-p t)
425 (rename-uniquely))
427 ;;;###autoload (autoload 'ibuffer-do-revert "ibuf-ext")
428 (define-ibuffer-op revert ()
429 "Revert marked buffers as with `revert-buffer'."
430 (:dangerous t
431 :opstring "reverted"
432 :active-opstring "revert"
433 :modifier-p :maybe)
434 (revert-buffer t t))
436 ;;;###autoload (autoload 'ibuffer-do-isearch "ibuf-ext")
437 (define-ibuffer-op ibuffer-do-isearch ()
438 "Perform a `isearch-forward' in marked buffers."
439 (:interactive ()
440 :opstring "searched in"
441 :complex t
442 :modifier-p :maybe)
443 (multi-isearch-buffers (ibuffer-get-marked-buffers)))
445 ;;;###autoload (autoload 'ibuffer-do-isearch-regexp "ibuf-ext")
446 (define-ibuffer-op ibuffer-do-isearch-regexp ()
447 "Perform a `isearch-forward-regexp' in marked buffers."
448 (:interactive ()
449 :opstring "searched regexp in"
450 :complex t
451 :modifier-p :maybe)
452 (multi-isearch-buffers-regexp (ibuffer-get-marked-buffers)))
454 ;;;###autoload (autoload 'ibuffer-do-replace-regexp "ibuf-ext")
455 (define-ibuffer-op replace-regexp (from-str to-str)
456 "Perform a `replace-regexp' in marked buffers."
457 (:interactive
458 (let* ((from-str (read-from-minibuffer "Replace regexp: "))
459 (to-str (read-from-minibuffer (concat "Replace " from-str
460 " with: "))))
461 (list from-str to-str))
462 :opstring "replaced in"
463 :complex t
464 :modifier-p :maybe)
465 (save-window-excursion
466 (switch-to-buffer buf)
467 (save-excursion
468 (goto-char (point-min))
469 (let ((case-fold-search ibuffer-case-fold-search))
470 (while (re-search-forward from-str nil t)
471 (replace-match to-str))))
474 ;;;###autoload (autoload 'ibuffer-do-query-replace "ibuf-ext")
475 (define-ibuffer-op query-replace (&rest args)
476 "Perform a `query-replace' in marked buffers."
477 (:interactive
478 (query-replace-read-args "Query replace" t t)
479 :opstring "replaced in"
480 :complex t
481 :modifier-p :maybe)
482 (save-window-excursion
483 (switch-to-buffer buf)
484 (save-excursion
485 (let ((case-fold-search ibuffer-case-fold-search))
486 (goto-char (point-min))
487 (apply #'query-replace args)))
490 ;;;###autoload (autoload 'ibuffer-do-query-replace-regexp "ibuf-ext")
491 (define-ibuffer-op query-replace-regexp (&rest args)
492 "Perform a `query-replace-regexp' in marked buffers."
493 (:interactive
494 (query-replace-read-args "Query replace regexp" t t)
495 :opstring "replaced in"
496 :complex t
497 :modifier-p :maybe)
498 (save-window-excursion
499 (switch-to-buffer buf)
500 (save-excursion
501 (let ((case-fold-search ibuffer-case-fold-search))
502 (goto-char (point-min))
503 (apply #'query-replace-regexp args)))
506 ;;;###autoload (autoload 'ibuffer-do-print "ibuf-ext")
507 (define-ibuffer-op print ()
508 "Print marked buffers as with `print-buffer'."
509 (:opstring "printed"
510 :modifier-p nil)
511 (print-buffer))
513 ;;;###autoload
514 (defun ibuffer-included-in-filters-p (buf filters)
515 (not
516 (memq nil ;; a filter will return nil if it failed
517 (mapcar
518 ;; filter should be like (TYPE . QUALIFIER), or
519 ;; (or (TYPE . QUALIFIER) (TYPE . QUALIFIER) ...)
520 #'(lambda (qual)
521 (ibuffer-included-in-filter-p buf qual))
522 filters))))
524 (defun ibuffer-included-in-filter-p (buf filter)
525 (if (eq (car filter) 'not)
526 (not (ibuffer-included-in-filter-p-1 buf (cdr filter)))
527 (ibuffer-included-in-filter-p-1 buf filter)))
529 (defun ibuffer-included-in-filter-p-1 (buf filter)
530 (not
531 (not
532 (pcase (car filter)
533 (`or
534 (memq t (mapcar #'(lambda (x)
535 (ibuffer-included-in-filter-p buf x))
536 (cdr filter))))
537 (`saved
538 (let ((data
539 (assoc (cdr filter)
540 ibuffer-saved-filters)))
541 (unless data
542 (ibuffer-filter-disable t)
543 (error "Unknown saved filter %s" (cdr filter)))
544 (ibuffer-included-in-filters-p buf (cadr data))))
546 (pcase-let ((`(,_type ,_desc ,func)
547 (assq (car filter) ibuffer-filtering-alist)))
548 (unless func
549 (ibuffer-filter-disable t)
550 (error "Undefined filter %s" (car filter)))
551 (funcall func buf (cdr filter))))))))
553 (defun ibuffer-generate-filter-groups (bmarklist &optional noempty nodefault)
554 (let ((filter-group-alist (if nodefault
555 ibuffer-filter-groups
556 (append ibuffer-filter-groups
557 (list (cons "Default" nil))))))
558 ;; (dolist (hidden ibuffer-hidden-filter-groups)
559 ;; (setq filter-group-alist (ibuffer-delete-alist
560 ;; hidden filter-group-alist)))
561 (let ((vec (make-vector (length filter-group-alist) nil))
562 (i 0))
563 (dolist (filtergroup filter-group-alist)
564 (let ((filterset (cdr filtergroup)))
565 (cl-multiple-value-bind (hip-crowd lamers)
566 (cl-values-list
567 (ibuffer-split-list (lambda (bufmark)
568 (ibuffer-included-in-filters-p (car bufmark)
569 filterset))
570 bmarklist))
571 (aset vec i hip-crowd)
572 (cl-incf i)
573 (setq bmarklist lamers))))
574 (let (ret)
575 (dotimes (j i)
576 (let ((bufs (aref vec j)))
577 (unless (and noempty (null bufs))
578 (push (cons (car (nth j filter-group-alist))
579 bufs)
580 ret))))
581 ret))))
583 ;;;###autoload
584 (defun ibuffer-filters-to-filter-group (name)
585 "Make the current filters into a filtering group."
586 (interactive "sName for filtering group: ")
587 (when (null ibuffer-filtering-qualifiers)
588 (error "No filters in effect"))
589 (push (cons name ibuffer-filtering-qualifiers) ibuffer-filter-groups)
590 (ibuffer-filter-disable))
592 ;;;###autoload
593 (defun ibuffer-set-filter-groups-by-mode ()
594 "Set the current filter groups to filter by mode."
595 (interactive)
596 (setq ibuffer-filter-groups
597 (mapcar (lambda (mode)
598 (cons (format "%s" mode) `((mode . ,mode))))
599 (let ((modes
600 (ibuffer-remove-duplicates
601 (mapcar (lambda (buf)
602 (buffer-local-value 'major-mode buf))
603 (buffer-list)))))
604 (if ibuffer-view-ibuffer
605 modes
606 (delq 'ibuffer-mode modes)))))
607 (ibuffer-update nil t))
609 ;;;###autoload
610 (defun ibuffer-pop-filter-group ()
611 "Remove the first filter group."
612 (interactive)
613 (when (null ibuffer-filter-groups)
614 (error "No filter groups active"))
615 (setq ibuffer-hidden-filter-groups
616 (delete (pop ibuffer-filter-groups)
617 ibuffer-hidden-filter-groups))
618 (ibuffer-update nil t))
620 (defun ibuffer-read-filter-group-name (msg &optional nodefault noerror)
621 (when (and (not noerror) (null ibuffer-filter-groups))
622 (error "No filter groups active"))
623 ;; `ibuffer-generate-filter-groups' returns all non-hidden filter
624 ;; groups, possibly excluding empty groups or Default.
625 ;; We add `ibuffer-hidden-filter-groups' to the list, excluding
626 ;; Default if necessary.
627 (completing-read msg (nconc
628 (ibuffer-generate-filter-groups
629 (ibuffer-current-state-list)
630 (not ibuffer-show-empty-filter-groups)
631 nodefault)
632 (if nodefault
633 (remove "Default" ibuffer-hidden-filter-groups)
634 ibuffer-hidden-filter-groups))
635 nil t))
637 ;;;###autoload
638 (defun ibuffer-decompose-filter-group (group)
639 "Decompose the filter group GROUP into active filters."
640 (interactive
641 (list (ibuffer-read-filter-group-name "Decompose filter group: " t)))
642 (let ((data (cdr (assoc group ibuffer-filter-groups))))
643 (setq ibuffer-filter-groups (ibuffer-delete-alist
644 group ibuffer-filter-groups)
645 ibuffer-filtering-qualifiers data))
646 (ibuffer-update nil t))
648 ;;;###autoload
649 (defun ibuffer-clear-filter-groups ()
650 "Remove all filter groups."
651 (interactive)
652 (setq ibuffer-filter-groups nil
653 ibuffer-hidden-filter-groups nil)
654 (ibuffer-update nil t))
656 (defun ibuffer-current-filter-groups-with-position ()
657 (save-excursion
658 (goto-char (point-min))
659 (let ((pos nil)
660 (result nil))
661 (while (and (not (eobp))
662 (setq pos (next-single-property-change
663 (point) 'ibuffer-filter-group-name)))
664 (goto-char pos)
665 (push (cons (get-text-property (point) 'ibuffer-filter-group-name)
666 pos)
667 result)
668 (goto-char (next-single-property-change
669 pos 'ibuffer-filter-group-name)))
670 (nreverse result))))
672 ;;;###autoload
673 (defun ibuffer-jump-to-filter-group (name)
674 "Move point to the filter group whose name is NAME."
675 (interactive
676 (list (ibuffer-read-filter-group-name "Jump to filter group: ")))
677 (ibuffer-aif (assoc name (ibuffer-current-filter-groups-with-position))
678 (goto-char (cdr it))
679 (error "No filter group with name %s" name)))
681 ;;;###autoload
682 (defun ibuffer-kill-filter-group (name)
683 "Kill the filter group named NAME.
684 The group will be added to `ibuffer-filter-group-kill-ring'."
685 (interactive (list (ibuffer-read-filter-group-name "Kill filter group: " t)))
686 (when (equal name "Default")
687 (error "Can't kill default filter group"))
688 (ibuffer-aif (assoc name ibuffer-filter-groups)
689 (progn
690 (push (copy-tree it) ibuffer-filter-group-kill-ring)
691 (setq ibuffer-filter-groups (ibuffer-delete-alist
692 name ibuffer-filter-groups))
693 (setq ibuffer-hidden-filter-groups
694 (delete name ibuffer-hidden-filter-groups)))
695 (error "No filter group with name \"%s\"" name))
696 (ibuffer-update nil t))
698 ;;;###autoload
699 (defun ibuffer-kill-line (&optional arg interactive-p)
700 "Kill the filter group at point.
701 See also `ibuffer-kill-filter-group'."
702 (interactive "P\np")
703 (ibuffer-aif (save-excursion
704 (ibuffer-forward-line 0)
705 (get-text-property (point) 'ibuffer-filter-group-name))
706 (progn
707 (ibuffer-kill-filter-group it))
708 (funcall (if interactive-p #'call-interactively #'funcall)
709 #'kill-line arg)))
711 (defun ibuffer-insert-filter-group-before (newgroup group)
712 (let* ((found nil)
713 (pos (let ((groups (mapcar #'car ibuffer-filter-groups))
714 (res 0))
715 (while groups
716 (if (equal (car groups) group)
717 (setq found t
718 groups nil)
719 (cl-incf res)
720 (setq groups (cdr groups))))
721 res)))
722 (cond ((not found)
723 (setq ibuffer-filter-groups
724 (nconc ibuffer-filter-groups (list newgroup))))
725 ((zerop pos)
726 (push newgroup ibuffer-filter-groups))
728 (let ((cell (nthcdr pos ibuffer-filter-groups)))
729 (setf (cdr cell) (cons (car cell) (cdr cell)))
730 (setf (car cell) newgroup))))))
732 ;;;###autoload
733 (defun ibuffer-yank ()
734 "Yank the last killed filter group before group at point."
735 (interactive)
736 (ibuffer-yank-filter-group
737 (or (get-text-property (point) 'ibuffer-filter-group-name)
738 (get-text-property (point) 'ibuffer-filter-group)
739 (error "No filter group at point"))))
741 ;;;###autoload
742 (defun ibuffer-yank-filter-group (name)
743 "Yank the last killed filter group before group named NAME."
744 (interactive (list (ibuffer-read-filter-group-name
745 "Yank filter group before group: ")))
746 (unless ibuffer-filter-group-kill-ring
747 (error "The Ibuffer filter group kill-ring is empty"))
748 (save-excursion
749 (ibuffer-forward-line 0)
750 (ibuffer-insert-filter-group-before (pop ibuffer-filter-group-kill-ring)
751 name))
752 (ibuffer-update nil t))
754 ;;;###autoload
755 (defun ibuffer-save-filter-groups (name groups)
756 "Save all active filter groups GROUPS as NAME.
757 They are added to `ibuffer-saved-filter-groups'. Interactively,
758 prompt for NAME, and use the current filters."
759 (interactive
760 (if (null ibuffer-filter-groups)
761 (error "No filter groups active")
762 (list
763 (read-from-minibuffer "Save current filter groups as: ")
764 ibuffer-filter-groups)))
765 (ibuffer-aif (assoc name ibuffer-saved-filter-groups)
766 (setcdr it groups)
767 (push (cons name groups) ibuffer-saved-filter-groups))
768 (ibuffer-maybe-save-stuff))
770 ;;;###autoload
771 (defun ibuffer-delete-saved-filter-groups (name)
772 "Delete saved filter groups with NAME.
773 They are removed from `ibuffer-saved-filter-groups'."
774 (interactive
775 (list
776 (if (null ibuffer-saved-filter-groups)
777 (error "No saved filter groups")
778 (completing-read "Delete saved filter group: "
779 ibuffer-saved-filter-groups nil t))))
780 (setq ibuffer-saved-filter-groups
781 (ibuffer-delete-alist name ibuffer-saved-filter-groups))
782 (ibuffer-maybe-save-stuff)
783 (ibuffer-update nil t))
785 ;;;###autoload
786 (defun ibuffer-switch-to-saved-filter-groups (name)
787 "Set this buffer's filter groups to saved version with NAME.
788 The value from `ibuffer-saved-filter-groups' is used."
789 (interactive
790 (list
791 (cond ((null ibuffer-saved-filter-groups)
792 (error "No saved filters"))
793 ;; `ibuffer-saved-filter-groups' is a user variable that defaults
794 ;; to nil. We assume that with one element in this list the user
795 ;; knows what she wants. See bug#12331.
796 ((null (cdr ibuffer-saved-filter-groups))
797 (caar ibuffer-saved-filter-groups))
799 (completing-read "Switch to saved filter group: "
800 ibuffer-saved-filter-groups nil t)))))
801 (setq ibuffer-filter-groups (cdr (assoc name ibuffer-saved-filter-groups))
802 ibuffer-hidden-filter-groups nil)
803 (ibuffer-update nil t))
805 ;;;###autoload
806 (defun ibuffer-filter-disable (&optional delete-filter-groups)
807 "Disable all filters currently in effect in this buffer.
808 With optional arg DELETE-FILTER-GROUPS non-nil, delete all filter
809 group definitions by setting `ibuffer-filter-groups' to nil."
810 (interactive)
811 (setq ibuffer-filtering-qualifiers nil)
812 (if delete-filter-groups
813 (setq ibuffer-filter-groups nil))
814 (let ((buf (ibuffer-current-buffer)))
815 (ibuffer-update nil t)
816 (when buf
817 (ibuffer-jump-to-buffer (buffer-name buf)))))
819 ;;;###autoload
820 (defun ibuffer-pop-filter ()
821 "Remove the top filter in this buffer."
822 (interactive)
823 (when (null ibuffer-filtering-qualifiers)
824 (error "No filters in effect"))
825 (pop ibuffer-filtering-qualifiers)
826 (let ((buf (ibuffer-current-buffer)))
827 (ibuffer-update nil t)
828 (when buf
829 (ibuffer-jump-to-buffer (buffer-name buf)))))
831 (defun ibuffer-push-filter (qualifier)
832 "Add QUALIFIER to `ibuffer-filtering-qualifiers'."
833 (push qualifier ibuffer-filtering-qualifiers))
835 ;;;###autoload
836 (defun ibuffer-decompose-filter ()
837 "Separate the top compound filter (OR, NOT, or SAVED) in this buffer.
839 This means that the topmost filter on the filtering stack, which must
840 be a complex filter like (OR [name: foo] [mode: bar-mode]), will be
841 turned into two separate filters [name: foo] and [mode: bar-mode]."
842 (interactive)
843 (when (null ibuffer-filtering-qualifiers)
844 (error "No filters in effect"))
845 (let ((lim (pop ibuffer-filtering-qualifiers)))
846 (pcase (car lim)
847 (`or
848 (setq ibuffer-filtering-qualifiers (append
849 (cdr lim)
850 ibuffer-filtering-qualifiers)))
851 (`saved
852 (let ((data
853 (assoc (cdr lim)
854 ibuffer-saved-filters)))
855 (unless data
856 (ibuffer-filter-disable)
857 (error "Unknown saved filter %s" (cdr lim)))
858 (setq ibuffer-filtering-qualifiers (append
859 (cadr data)
860 ibuffer-filtering-qualifiers))))
861 (`not
862 (push (cdr lim)
863 ibuffer-filtering-qualifiers))
865 (error "Filter type %s is not compound" (car lim)))))
866 (ibuffer-update nil t))
868 ;;;###autoload
869 (defun ibuffer-exchange-filters ()
870 "Exchange the top two filters on the stack in this buffer."
871 (interactive)
872 (let ((filters ibuffer-filtering-qualifiers))
873 (when (< (length filters) 2)
874 (error "Need two filters to exchange"))
875 (cl-rotatef (car filters) (cadr filters))
876 (ibuffer-update nil t)))
878 ;;;###autoload
879 (defun ibuffer-negate-filter ()
880 "Negate the sense of the top filter in the current buffer."
881 (interactive)
882 (when (null ibuffer-filtering-qualifiers)
883 (error "No filters in effect"))
884 (let ((lim (pop ibuffer-filtering-qualifiers)))
885 (push (if (eq (car lim) 'not)
886 (cdr lim)
887 (cons 'not lim))
888 ibuffer-filtering-qualifiers))
889 (ibuffer-update nil t))
891 ;;;###autoload
892 (defun ibuffer-or-filter (&optional reverse)
893 "Replace the top two filters in this buffer with their logical OR.
894 If optional argument REVERSE is non-nil, instead break the top OR
895 filter into parts."
896 (interactive "P")
897 (if reverse
898 (progn
899 (when (or (null ibuffer-filtering-qualifiers)
900 (not (eq 'or (caar ibuffer-filtering-qualifiers))))
901 (error "Top filter is not an OR"))
902 (let ((lim (pop ibuffer-filtering-qualifiers)))
903 (setq ibuffer-filtering-qualifiers
904 (nconc (cdr lim) ibuffer-filtering-qualifiers))))
905 (when (< (length ibuffer-filtering-qualifiers) 2)
906 (error "Need two filters to OR"))
907 ;; If the second filter is an OR, just add to it.
908 (let ((first (pop ibuffer-filtering-qualifiers))
909 (second (pop ibuffer-filtering-qualifiers)))
910 (if (eq 'or (car second))
911 (push (nconc (list 'or first) (cdr second))
912 ibuffer-filtering-qualifiers)
913 (push (list 'or first second)
914 ibuffer-filtering-qualifiers))))
915 (ibuffer-update nil t))
917 (defun ibuffer-maybe-save-stuff ()
918 (when ibuffer-save-with-custom
919 (if (fboundp 'customize-save-variable)
920 (progn
921 (customize-save-variable 'ibuffer-saved-filters
922 ibuffer-saved-filters)
923 (customize-save-variable 'ibuffer-saved-filter-groups
924 ibuffer-saved-filter-groups))
925 (message "Not saved permanently: Customize not available"))))
927 ;;;###autoload
928 (defun ibuffer-save-filters (name filters)
929 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'.
930 Interactively, prompt for NAME, and use the current filters."
931 (interactive
932 (if (null ibuffer-filtering-qualifiers)
933 (error "No filters currently in effect")
934 (list
935 (read-from-minibuffer "Save current filters as: ")
936 ibuffer-filtering-qualifiers)))
937 (ibuffer-aif (assoc name ibuffer-saved-filters)
938 (setcdr it filters)
939 (push (list name filters) ibuffer-saved-filters))
940 (ibuffer-maybe-save-stuff))
942 ;;;###autoload
943 (defun ibuffer-delete-saved-filters (name)
944 "Delete saved filters with NAME from `ibuffer-saved-filters'."
945 (interactive
946 (list
947 (if (null ibuffer-saved-filters)
948 (error "No saved filters")
949 (completing-read "Delete saved filters: "
950 ibuffer-saved-filters nil t))))
951 (setq ibuffer-saved-filters
952 (ibuffer-delete-alist name ibuffer-saved-filters))
953 (ibuffer-maybe-save-stuff)
954 (ibuffer-update nil t))
956 ;;;###autoload
957 (defun ibuffer-add-saved-filters (name)
958 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
959 (interactive
960 (list
961 (if (null ibuffer-saved-filters)
962 (error "No saved filters")
963 (completing-read "Add saved filters: "
964 ibuffer-saved-filters nil t))))
965 (push (cons 'saved name) ibuffer-filtering-qualifiers)
966 (ibuffer-update nil t))
968 ;;;###autoload
969 (defun ibuffer-switch-to-saved-filters (name)
970 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'."
971 (interactive
972 (list
973 (if (null ibuffer-saved-filters)
974 (error "No saved filters")
975 (completing-read "Switch to saved filters: "
976 ibuffer-saved-filters nil t))))
977 (setq ibuffer-filtering-qualifiers (list (cons 'saved name)))
978 (ibuffer-update nil t))
980 (defun ibuffer-format-filter-group-data (filter)
981 (if (equal filter "Default")
983 (concat "Filter:" (mapconcat #'ibuffer-format-qualifier
984 (cdr (assq filter ibuffer-filter-groups))
985 " "))))
987 (defun ibuffer-format-qualifier (qualifier)
988 (if (eq (car-safe qualifier) 'not)
989 (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier)) "]")
990 (ibuffer-format-qualifier-1 qualifier)))
992 (defun ibuffer-format-qualifier-1 (qualifier)
993 (pcase (car qualifier)
994 (`saved
995 (concat " [filter: " (cdr qualifier) "]"))
996 (`or
997 (concat " [OR" (mapconcat #'ibuffer-format-qualifier
998 (cdr qualifier) "") "]"))
1000 (let ((type (assq (car qualifier) ibuffer-filtering-alist)))
1001 (unless qualifier
1002 (error "Ibuffer: bad qualifier %s" qualifier))
1003 (concat " [" (cadr type) ": " (format "%s]" (cdr qualifier)))))))
1006 (defun ibuffer-list-buffer-modes (&optional include-parents)
1007 "Create a completion table of buffer modes currently in use.
1008 If INCLUDE-PARENTS is non-nil then include parent modes."
1009 (let ((modes))
1010 (dolist (buf (buffer-list))
1011 (let ((this-mode (buffer-local-value 'major-mode buf)))
1012 (while (and this-mode (not (memq this-mode modes)))
1013 (push this-mode modes)
1014 (setq this-mode (and include-parents
1015 (get this-mode 'derived-mode-parent))))))
1016 (mapcar #'symbol-name modes)))
1019 ;;; Extra operation definitions
1021 ;;;###autoload (autoload 'ibuffer-filter-by-mode "ibuf-ext")
1022 (define-ibuffer-filter mode
1023 "Toggle current view to buffers with major mode QUALIFIER."
1024 (:description "major mode"
1025 :reader
1026 (let* ((buf (ibuffer-current-buffer))
1027 (default (if (and buf (buffer-live-p buf))
1028 (symbol-name (buffer-local-value
1029 'major-mode buf)))))
1030 (intern
1031 (completing-read
1032 (if default
1033 (format "Filter by major mode (default %s): " default)
1034 "Filter by major mode: ")
1035 obarray
1036 #'(lambda (e)
1037 (string-match "-mode\\'" (symbol-name e)))
1038 t nil nil default))))
1039 (eq qualifier (buffer-local-value 'major-mode buf)))
1041 ;;;###autoload (autoload 'ibuffer-filter-by-used-mode "ibuf-ext")
1042 (define-ibuffer-filter used-mode
1043 "Toggle current view to buffers with major mode QUALIFIER.
1044 Called interactively, this function allows selection of modes
1045 currently used by buffers."
1046 (:description "major mode in use"
1047 :reader
1048 (let* ((buf (ibuffer-current-buffer))
1049 (default (if (and buf (buffer-live-p buf))
1050 (symbol-name (buffer-local-value
1051 'major-mode buf)))))
1052 (intern
1053 (completing-read
1054 (if default
1055 (format "Filter by major mode (default %s): " default)
1056 "Filter by major mode: ")
1057 (ibuffer-list-buffer-modes) nil t nil nil default))))
1058 (eq qualifier (buffer-local-value 'major-mode buf)))
1060 ;;;###autoload (autoload 'ibuffer-filter-by-derived-mode "ibuf-ext")
1061 (define-ibuffer-filter derived-mode
1062 "Toggle current view to buffers whose major mode inherits from QUALIFIER."
1063 (:description "derived mode"
1064 :reader
1065 (intern
1066 (completing-read "Filter by derived mode: "
1067 (ibuffer-list-buffer-modes t)
1068 nil t)))
1069 (with-current-buffer buf (derived-mode-p qualifier)))
1071 ;;;###autoload (autoload 'ibuffer-filter-by-name "ibuf-ext")
1072 (define-ibuffer-filter name
1073 "Toggle current view to buffers with name matching QUALIFIER."
1074 (:description "buffer name"
1075 :reader (read-from-minibuffer "Filter by name (regexp): "))
1076 (string-match qualifier (buffer-name buf)))
1078 ;;;###autoload (autoload 'ibuffer-filter-by-filename "ibuf-ext")
1079 (define-ibuffer-filter filename
1080 "Toggle current view to buffers with filename matching QUALIFIER."
1081 (:description "filename"
1082 :reader (read-from-minibuffer "Filter by filename (regexp): "))
1083 (ibuffer-awhen (with-current-buffer buf (ibuffer-buffer-file-name))
1084 (string-match qualifier it)))
1086 ;;;###autoload (autoload 'ibuffer-filter-by-size-gt "ibuf-ext")
1087 (define-ibuffer-filter size-gt
1088 "Toggle current view to buffers with size greater than QUALIFIER."
1089 (:description "size greater than"
1090 :reader
1091 (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
1092 (> (with-current-buffer buf (buffer-size))
1093 qualifier))
1095 ;;;###autoload (autoload 'ibuffer-filter-by-size-lt "ibuf-ext")
1096 (define-ibuffer-filter size-lt
1097 "Toggle current view to buffers with size less than QUALIFIER."
1098 (:description "size less than"
1099 :reader
1100 (string-to-number (read-from-minibuffer "Filter by size less than: ")))
1101 (< (with-current-buffer buf (buffer-size))
1102 qualifier))
1104 ;;;###autoload (autoload 'ibuffer-filter-by-content "ibuf-ext")
1105 (define-ibuffer-filter content
1106 "Toggle current view to buffers whose contents match QUALIFIER."
1107 (:description "content"
1108 :reader (read-from-minibuffer "Filter by content (regexp): "))
1109 (with-current-buffer buf
1110 (save-excursion
1111 (goto-char (point-min))
1112 (re-search-forward qualifier nil t))))
1114 ;;;###autoload (autoload 'ibuffer-filter-by-predicate "ibuf-ext")
1115 (define-ibuffer-filter predicate
1116 "Toggle current view to buffers for which QUALIFIER returns non-nil."
1117 (:description "predicate"
1118 :reader (read-minibuffer "Filter by predicate (form): "))
1119 (with-current-buffer buf
1120 (eval qualifier)))
1122 ;;; Sorting
1124 ;;;###autoload
1125 (defun ibuffer-toggle-sorting-mode ()
1126 "Toggle the current sorting mode.
1127 Default sorting modes are:
1128 Recency - the last time the buffer was viewed
1129 Name - the name of the buffer
1130 Major Mode - the name of the major mode of the buffer
1131 Size - the size of the buffer"
1132 (interactive)
1133 (let ((modes (mapcar #'car ibuffer-sorting-functions-alist)))
1134 (cl-pushnew 'recency modes)
1135 (setq modes (sort modes #'string-lessp))
1136 (let ((next (or (car-safe (cdr-safe (memq ibuffer-sorting-mode modes)))
1137 (car modes))))
1138 (setq ibuffer-sorting-mode next)
1139 (message "Sorting by %s" next)))
1140 (ibuffer-redisplay t))
1142 ;;;###autoload
1143 (defun ibuffer-invert-sorting ()
1144 "Toggle whether or not sorting is in reverse order."
1145 (interactive)
1146 (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep))
1147 (message "Sorting order %s"
1148 (if ibuffer-sorting-reversep
1149 "reversed"
1150 "normal"))
1151 (ibuffer-redisplay t))
1153 ;;;###autoload (autoload 'ibuffer-do-sort-by-major-mode "ibuf-ext")
1154 (define-ibuffer-sorter major-mode
1155 "Sort the buffers by major modes.
1156 Ordering is lexicographic."
1157 (:description "major mode")
1158 (string-lessp (downcase
1159 (symbol-name (buffer-local-value 'major-mode (car a))))
1160 (downcase
1161 (symbol-name (buffer-local-value 'major-mode (car b))))))
1163 ;;;###autoload (autoload 'ibuffer-do-sort-by-mode-name "ibuf-ext")
1164 (define-ibuffer-sorter mode-name
1165 "Sort the buffers by their mode name.
1166 Ordering is lexicographic."
1167 (:description "major mode name")
1168 (string-lessp (downcase
1169 (with-current-buffer
1170 (car a)
1171 (format-mode-line mode-name)))
1172 (downcase
1173 (with-current-buffer
1174 (car b)
1175 (format-mode-line mode-name)))))
1177 ;;;###autoload (autoload 'ibuffer-do-sort-by-alphabetic "ibuf-ext")
1178 (define-ibuffer-sorter alphabetic
1179 "Sort the buffers by their names.
1180 Ordering is lexicographic."
1181 (:description "buffer name")
1182 (string-lessp
1183 (buffer-name (car a))
1184 (buffer-name (car b))))
1186 ;;;###autoload (autoload 'ibuffer-do-sort-by-size "ibuf-ext")
1187 (define-ibuffer-sorter size
1188 "Sort the buffers by their size."
1189 (:description "size")
1190 (< (with-current-buffer (car a)
1191 (buffer-size))
1192 (with-current-buffer (car b)
1193 (buffer-size))))
1195 ;;;###autoload (autoload 'ibuffer-do-sort-by-filename/process "ibuf-ext")
1196 (define-ibuffer-sorter filename/process
1197 "Sort the buffers by their file name/process name."
1198 (:description "file name")
1199 (string-lessp
1200 ;; FIXME: For now just compare the file name and the process name
1201 ;; (if it exists). Is there a better way to do this?
1202 (or (buffer-file-name (car a))
1203 (let ((pr-a (get-buffer-process (car a))))
1204 (and (processp pr-a) (process-name pr-a))))
1205 (or (buffer-file-name (car b))
1206 (let ((pr-b (get-buffer-process (car b))))
1207 (and (processp pr-b) (process-name pr-b))))))
1209 ;;; Functions to emulate bs.el
1211 ;;;###autoload
1212 (defun ibuffer-bs-show ()
1213 "Emulate `bs-show' from the bs.el package."
1214 (interactive)
1215 (ibuffer t "*Ibuffer-bs*" '((filename . ".*")) nil t)
1216 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all))
1218 (defun ibuffer-bs-toggle-all ()
1219 "Emulate `bs-toggle-show-all' from the bs.el package."
1220 (interactive)
1221 (if ibuffer-filtering-qualifiers
1222 (ibuffer-pop-filter)
1223 (progn (ibuffer-push-filter '(filename . ".*"))
1224 (ibuffer-update nil t))))
1226 ;;; Handy functions
1228 ;;;###autoload
1229 (defun ibuffer-add-to-tmp-hide (regexp)
1230 "Add REGEXP to `ibuffer-tmp-hide-regexps'.
1231 This means that buffers whose name matches REGEXP will not be shown
1232 for this Ibuffer session."
1233 (interactive
1234 (list
1235 (read-from-minibuffer "Never show buffers matching: "
1236 (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
1237 (push regexp ibuffer-tmp-hide-regexps))
1239 ;;;###autoload
1240 (defun ibuffer-add-to-tmp-show (regexp)
1241 "Add REGEXP to `ibuffer-tmp-show-regexps'.
1242 This means that buffers whose name matches REGEXP will always be shown
1243 for this Ibuffer session."
1244 (interactive
1245 (list
1246 (read-from-minibuffer "Always show buffers matching: "
1247 (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
1248 (push regexp ibuffer-tmp-show-regexps))
1250 ;;;###autoload
1251 (defun ibuffer-forward-next-marked (&optional count mark direction)
1252 "Move forward by COUNT marked buffers (default 1).
1254 If MARK is non-nil, it should be a character denoting the type of mark
1255 to move by. The default is `ibuffer-marked-char'.
1257 If DIRECTION is non-nil, it should be an integer; negative integers
1258 mean move backwards, non-negative integers mean move forwards."
1259 (interactive "P")
1260 (unless count
1261 (setq count 1))
1262 (unless mark
1263 (setq mark ibuffer-marked-char))
1264 (unless direction
1265 (setq direction 1))
1266 ;; Skip the title
1267 (ibuffer-forward-line 0)
1268 (let ((opos (point)))
1269 (ibuffer-forward-line direction)
1270 (while (not (or (= (point) opos)
1271 (eq (ibuffer-current-mark) mark)))
1272 (ibuffer-forward-line direction))
1273 (when (and (= (point) opos)
1274 (not (eq (ibuffer-current-mark) mark)))
1275 (error "No buffers with mark %c" mark))))
1277 ;;;###autoload
1278 (defun ibuffer-backwards-next-marked (&optional count mark)
1279 "Move backwards by COUNT marked buffers (default 1).
1281 If MARK is non-nil, it should be a character denoting the type of mark
1282 to move by. The default is `ibuffer-marked-char'."
1283 (interactive "P")
1284 (ibuffer-forward-next-marked count mark -1))
1286 ;;;###autoload
1287 (defun ibuffer-do-kill-lines ()
1288 "Hide all of the currently marked lines."
1289 (interactive)
1290 (if (= (ibuffer-count-marked-lines) 0)
1291 (message "No buffers marked; use 'm' to mark a buffer")
1292 (let ((count
1293 (ibuffer-map-marked-lines
1294 #'(lambda (_buf _mark)
1295 'kill))))
1296 (message "Killed %s lines" count))))
1298 ;;;###autoload
1299 (defun ibuffer-jump-to-buffer (name)
1300 "Move point to the buffer whose name is NAME.
1302 If called interactively, prompt for a buffer name and go to the
1303 corresponding line in the Ibuffer buffer. If said buffer is in a
1304 hidden group filter, open it.
1306 If `ibuffer-jump-offer-only-visible-buffers' is non-nil, only offer
1307 visible buffers in the completion list. Calling the command with
1308 a prefix argument reverses the meaning of that variable."
1309 (interactive (list
1310 (let ((only-visible ibuffer-jump-offer-only-visible-buffers))
1311 (when current-prefix-arg
1312 (setq only-visible (not only-visible)))
1313 (if only-visible
1314 (let ((table (mapcar #'(lambda (x)
1315 (buffer-name (car x)))
1316 (ibuffer-current-state-list))))
1317 (when (null table)
1318 (error "No buffers!"))
1319 (completing-read "Jump to buffer: "
1320 table nil t))
1321 (read-buffer "Jump to buffer: " nil t)))))
1322 (when (not (string= "" name))
1323 (let (buf-point)
1324 ;; Blindly search for our buffer: it is very likely that it is
1325 ;; not in a hidden filter group.
1326 (ibuffer-map-lines #'(lambda (buf _marks)
1327 (when (string= (buffer-name buf) name)
1328 (setq buf-point (point))
1329 nil))
1330 t nil)
1331 (when (and
1332 (null buf-point)
1333 (not (null ibuffer-hidden-filter-groups)))
1334 ;; We did not find our buffer. It must be in a hidden filter
1335 ;; group, so go through all hidden filter groups to find it.
1336 (catch 'found
1337 (dolist (group ibuffer-hidden-filter-groups)
1338 (ibuffer-jump-to-filter-group group)
1339 (ibuffer-toggle-filter-group)
1340 (ibuffer-map-lines #'(lambda (buf _marks)
1341 (when (string= (buffer-name buf) name)
1342 (setq buf-point (point))
1343 nil))
1344 t group)
1345 (if buf-point
1346 (throw 'found nil)
1347 (ibuffer-toggle-filter-group)))))
1348 (if (null buf-point)
1349 ;; Still not found even though we expanded all hidden filter
1350 ;; groups: that must be because it's hidden by predicate:
1351 ;; we won't bother trying to display it.
1352 (error "No buffer with name %s" name)
1353 (goto-char buf-point)))))
1355 (declare-function diff-sentinel "diff"
1356 (code &optional old-temp-file new-temp-file))
1358 (defun ibuffer-diff-buffer-with-file-1 (buffer)
1359 (let ((bufferfile (buffer-local-value 'buffer-file-name buffer))
1360 (tempfile (make-temp-file "buffer-content-")))
1361 (when bufferfile
1362 (unwind-protect
1363 (progn
1364 (with-current-buffer buffer
1365 (write-region nil nil tempfile nil 'nomessage))
1366 (let* ((old (expand-file-name bufferfile))
1367 (new (expand-file-name tempfile))
1368 (oldtmp (file-local-copy old))
1369 (newtmp (file-local-copy new))
1370 (switches diff-switches)
1371 (command
1372 (mapconcat
1373 'identity
1374 `(,diff-command
1375 ;; Use explicitly specified switches
1376 ,@(if (listp switches) switches (list switches))
1377 ,@(if (or old new)
1378 (list "-L" (shell-quote-argument old)
1379 "-L" (shell-quote-argument
1380 (format "Buffer %s" (buffer-name buffer)))))
1381 ,(shell-quote-argument (or oldtmp old))
1382 ,(shell-quote-argument (or newtmp new)))
1383 " ")))
1384 (let ((inhibit-read-only t))
1385 (insert command "\n")
1386 (diff-sentinel
1387 (call-process shell-file-name nil
1388 (current-buffer) nil
1389 shell-command-switch command))
1390 (insert "\n")))))
1391 (sit-for 0)
1392 (when (file-exists-p tempfile)
1393 (delete-file tempfile)))))
1395 ;;;###autoload
1396 (defun ibuffer-diff-with-file ()
1397 "View the differences between marked buffers and their associated files.
1398 If no buffers are marked, use buffer at point.
1399 This requires the external program \"diff\" to be in your `exec-path'."
1400 (interactive)
1401 (require 'diff)
1402 (let ((marked-bufs (ibuffer-get-marked-buffers)))
1403 (when (null marked-bufs)
1404 (setq marked-bufs (list (ibuffer-current-buffer t))))
1405 (with-current-buffer (get-buffer-create "*Ibuffer Diff*")
1406 (setq buffer-read-only nil)
1407 (buffer-disable-undo (current-buffer))
1408 (erase-buffer)
1409 (buffer-enable-undo (current-buffer))
1410 (diff-mode)
1411 (dolist (buf marked-bufs)
1412 (unless (buffer-live-p buf)
1413 (error "Buffer %s has been killed" buf))
1414 (ibuffer-diff-buffer-with-file-1 buf))
1415 (setq buffer-read-only t)))
1416 (switch-to-buffer "*Ibuffer Diff*"))
1418 ;;;###autoload
1419 (defun ibuffer-copy-filename-as-kill (&optional arg)
1420 "Copy filenames of marked (or next ARG) buffers into the kill ring.
1422 The names are separated by a space.
1423 If a buffer has no filename, it is ignored.
1425 With no prefix arg, use the filename sans its directory of each marked file.
1426 With a zero prefix arg, use the complete filename of each marked file.
1427 With \\[universal-argument], use the filename of each marked file relative
1428 to `ibuffer-default-directory' if non-nil, otherwise `default-directory'.
1430 You can then feed the file name(s) to other commands with \\[yank]."
1431 (interactive "P")
1432 (let* ((buffers (cond ((and (integerp arg) (not (zerop arg)))
1433 (ibuffer--near-buffers arg))
1435 (or (ibuffer-get-marked-buffers)
1436 (list (ibuffer-current-buffer))))))
1437 (file-names
1438 (mapcar
1439 (lambda (buf)
1440 (let ((name (with-current-buffer buf
1441 (ibuffer-buffer-file-name))))
1442 (if (null name)
1444 (cond ((and (integerp arg) (zerop arg)) name)
1445 ((consp arg)
1446 (file-relative-name
1447 name (or ibuffer-default-directory
1448 default-directory)))
1449 (t (file-name-nondirectory name))))))
1450 buffers))
1451 (string
1452 (mapconcat 'identity (delete "" file-names) " ")))
1453 (unless (string= string "")
1454 (if (eq last-command 'kill-region)
1455 (kill-append string nil)
1456 (kill-new string))
1457 (message "%s" string))))
1459 ;;;###autoload
1460 (defun ibuffer-copy-buffername-as-kill (&optional arg)
1461 "Copy buffer names of marked (or next ARG) buffers into the kill ring.
1462 The names are separated by a space.
1463 You can then feed the file name(s) to other commands with \\[yank]."
1464 (interactive "P")
1465 (let* ((buffers (cond ((and (integerp arg) (not (zerop arg)))
1466 (ibuffer--near-buffers arg))
1468 (or (ibuffer-get-marked-buffers)
1469 (list (ibuffer-current-buffer))))))
1470 (string (mapconcat #'buffer-name buffers " ")))
1471 (unless (string= string "")
1472 (if (eq last-command 'kill-region)
1473 (kill-append string nil)
1474 (kill-new string))
1475 (message "%s" string))))
1477 (defun ibuffer-mark-on-buffer (func &optional ibuffer-mark-on-buffer-mark group)
1478 (let ((count
1479 (ibuffer-map-lines
1480 #'(lambda (buf _mark)
1481 (when (funcall func buf)
1482 (ibuffer-set-mark-1 (or ibuffer-mark-on-buffer-mark
1483 ibuffer-marked-char))
1486 group)))
1487 (ibuffer-redisplay t)
1488 (unless (eq ibuffer-mark-on-buffer-mark ?\s)
1489 (message "Marked %s buffers" count))))
1491 ;;;###autoload
1492 (defun ibuffer-mark-by-name-regexp (regexp)
1493 "Mark all buffers whose name matches REGEXP."
1494 (interactive "sMark by name (regexp): ")
1495 (ibuffer-mark-on-buffer
1496 #'(lambda (buf)
1497 (string-match regexp (buffer-name buf)))))
1499 (defun ibuffer-locked-buffer-p (&optional buf)
1500 "Return non-nil if BUF is locked.
1501 When BUF nil, default to the buffer at current line."
1502 (let ((cbuffer (or buf (ibuffer-current-buffer))))
1503 (when cbuffer
1504 (with-current-buffer cbuffer
1505 (and (boundp 'emacs-lock-mode) emacs-lock-mode)))))
1507 ;;;###autoload
1508 (defun ibuffer-mark-by-locked ()
1509 "Mark all locked buffers."
1510 (interactive)
1511 (when (featurep 'emacs-lock)
1512 (ibuffer-mark-on-buffer
1513 (lambda (buf)
1514 (ibuffer-locked-buffer-p buf)))))
1516 ;;;###autoload
1517 (defun ibuffer-mark-by-mode-regexp (regexp)
1518 "Mark all buffers whose major mode matches REGEXP."
1519 (interactive "sMark by major mode (regexp): ")
1520 (ibuffer-mark-on-buffer
1521 #'(lambda (buf)
1522 (with-current-buffer buf
1523 (string-match regexp (format-mode-line mode-name nil nil buf))))))
1525 ;;;###autoload
1526 (defun ibuffer-mark-by-file-name-regexp (regexp)
1527 "Mark all buffers whose file name matches REGEXP."
1528 (interactive "sMark by file name (regexp): ")
1529 (ibuffer-mark-on-buffer
1530 #'(lambda (buf)
1531 (let ((name (or (buffer-file-name buf)
1532 (with-current-buffer buf
1533 (and
1534 (boundp 'dired-directory)
1535 (stringp dired-directory)
1536 dired-directory)))))
1537 (when name
1538 (string-match regexp name))))))
1540 ;;;###autoload
1541 (defun ibuffer-mark-by-content-regexp (regexp &optional all-buffers)
1542 "Mark all buffers whose content matches REGEXP.
1543 Optional arg ALL-BUFFERS, if non-nil, then search in all buffers.
1544 Otherwise buffers whose name matches an element of
1545 `ibuffer-never-search-content-name' or whose major mode is on
1546 `ibuffer-never-search-content-mode' are excluded."
1547 (interactive (let ((reg (read-string "Mark by content (regexp): ")))
1548 (list reg current-prefix-arg)))
1549 (ibuffer-mark-on-buffer
1550 #'(lambda (buf)
1551 (let ((mode (with-current-buffer buf major-mode))
1552 res)
1553 (cond ((and (not all-buffers)
1555 (memq mode ibuffer-never-search-content-mode)
1556 (cl-some (lambda (x) (string-match x (buffer-name buf)))
1557 ibuffer-never-search-content-name)))
1558 (setq res nil))
1560 (with-current-buffer buf
1561 (save-mark-and-excursion
1562 (goto-char (point-min))
1563 (setq res (re-search-forward regexp nil t)))))) res))))
1565 ;;;###autoload
1566 (defun ibuffer-mark-by-mode (mode)
1567 "Mark all buffers whose major mode equals MODE."
1568 (interactive
1569 (let* ((buf (ibuffer-current-buffer))
1570 (default (if (and buf (buffer-live-p buf))
1571 (symbol-name (buffer-local-value
1572 'major-mode buf)))))
1573 (list (intern
1574 (completing-read
1575 (if default
1576 (format "Mark by major mode (default %s): " default)
1577 "Mark by major mode: ")
1578 (ibuffer-list-buffer-modes) nil t nil nil default)))))
1579 (ibuffer-mark-on-buffer
1580 #'(lambda (buf)
1581 (eq (buffer-local-value 'major-mode buf) mode))))
1583 ;;;###autoload
1584 (defun ibuffer-mark-modified-buffers ()
1585 "Mark all modified buffers."
1586 (interactive)
1587 (ibuffer-mark-on-buffer
1588 #'(lambda (buf) (buffer-modified-p buf))))
1590 ;;;###autoload
1591 (defun ibuffer-mark-unsaved-buffers ()
1592 "Mark all modified buffers that have an associated file."
1593 (interactive)
1594 (ibuffer-mark-on-buffer
1595 #'(lambda (buf) (and (buffer-local-value 'buffer-file-name buf)
1596 (buffer-modified-p buf)))))
1598 ;;;###autoload
1599 (defun ibuffer-mark-dissociated-buffers ()
1600 "Mark all buffers whose associated file does not exist."
1601 (interactive)
1602 (ibuffer-mark-on-buffer
1603 #'(lambda (buf)
1604 (with-current-buffer buf
1606 (and buffer-file-name
1607 (not (file-exists-p buffer-file-name)))
1608 (and (eq major-mode 'dired-mode)
1609 (boundp 'dired-directory)
1610 (stringp dired-directory)
1611 (not (file-exists-p (file-name-directory dired-directory)))))))))
1613 ;;;###autoload
1614 (defun ibuffer-mark-help-buffers ()
1615 "Mark buffers whose major mode is in variable `ibuffer-help-buffer-modes'."
1616 (interactive)
1617 (ibuffer-mark-on-buffer
1618 #'(lambda (buf)
1619 (with-current-buffer buf
1620 (memq major-mode ibuffer-help-buffer-modes)))))
1622 ;;;###autoload
1623 (defun ibuffer-mark-compressed-file-buffers ()
1624 "Mark buffers whose associated file is compressed."
1625 (interactive)
1626 (ibuffer-mark-on-buffer
1627 #'(lambda (buf)
1628 (with-current-buffer buf
1629 (and buffer-file-name
1630 (string-match ibuffer-compressed-file-name-regexp
1631 buffer-file-name))))))
1633 ;;;###autoload
1634 (defun ibuffer-mark-old-buffers ()
1635 "Mark buffers which have not been viewed in `ibuffer-old-time' hours."
1636 (interactive)
1637 (ibuffer-mark-on-buffer
1638 #'(lambda (buf)
1639 (with-current-buffer buf
1640 ;; hacked from midnight.el
1641 (when buffer-display-time
1642 (let* ((now (float-time))
1643 (then (float-time buffer-display-time)))
1644 (> (- now then) (* 60 60 ibuffer-old-time))))))))
1646 ;;;###autoload
1647 (defun ibuffer-mark-special-buffers ()
1648 "Mark all buffers whose name begins and ends with `*'."
1649 (interactive)
1650 (ibuffer-mark-on-buffer
1651 #'(lambda (buf) (string-match "^\\*.+\\*$"
1652 (buffer-name buf)))))
1654 ;;;###autoload
1655 (defun ibuffer-mark-read-only-buffers ()
1656 "Mark all read-only buffers."
1657 (interactive)
1658 (ibuffer-mark-on-buffer
1659 #'(lambda (buf) (buffer-local-value 'buffer-read-only buf))))
1661 ;;;###autoload
1662 (defun ibuffer-mark-dired-buffers ()
1663 "Mark all `dired' buffers."
1664 (interactive)
1665 (ibuffer-mark-on-buffer
1666 #'(lambda (buf) (eq (buffer-local-value 'major-mode buf) 'dired-mode))))
1668 ;;;###autoload
1669 (defun ibuffer-do-occur (regexp &optional nlines)
1670 "View lines which match REGEXP in all marked buffers.
1671 Optional argument NLINES says how many lines of context to display: it
1672 defaults to one."
1673 (interactive (occur-read-primary-args))
1674 (if (or (not (integerp nlines))
1675 (< nlines 0))
1676 (setq nlines 0))
1677 (when (zerop (ibuffer-count-marked-lines))
1678 (ibuffer-set-mark ibuffer-marked-char))
1679 (let ((ibuffer-do-occur-bufs nil))
1680 ;; Accumulate a list of marked buffers
1681 (ibuffer-map-marked-lines
1682 #'(lambda (buf _mark)
1683 (push buf ibuffer-do-occur-bufs)))
1684 (occur-1 regexp nlines ibuffer-do-occur-bufs)))
1686 (provide 'ibuf-ext)
1688 ;; Local Variables:
1689 ;; generated-autoload-file: "ibuffer-loaddefs.el"
1690 ;; End:
1692 ;;; ibuf-ext.el ends here