; Merge from origin/emacs-25
[emacs.git] / lisp / ibuf-ext.el
blobf93957ecb781d93c8dbb98805fc37059ee846021
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 "25.2"
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 "25.2"
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 (when (< (length ibuffer-filtering-qualifiers)
874 (error "Need two filters to exchange"))
875 (let ((first (pop ibuffer-filtering-qualifiers))
876 (second (pop ibuffer-filtering-qualifiers)))
877 (push first ibuffer-filtering-qualifiers)
878 (push second ibuffer-filtering-qualifiers))
879 (ibuffer-update nil t))
881 ;;;###autoload
882 (defun ibuffer-negate-filter ()
883 "Negate the sense of the top filter in the current buffer."
884 (interactive)
885 (when (null ibuffer-filtering-qualifiers)
886 (error "No filters in effect"))
887 (let ((lim (pop ibuffer-filtering-qualifiers)))
888 (push (if (eq (car lim) 'not)
889 (cdr lim)
890 (cons 'not lim))
891 ibuffer-filtering-qualifiers))
892 (ibuffer-update nil t))
894 ;;;###autoload
895 (defun ibuffer-or-filter (&optional reverse)
896 "Replace the top two filters in this buffer with their logical OR.
897 If optional argument REVERSE is non-nil, instead break the top OR
898 filter into parts."
899 (interactive "P")
900 (if reverse
901 (progn
902 (when (or (null ibuffer-filtering-qualifiers)
903 (not (eq 'or (caar ibuffer-filtering-qualifiers))))
904 (error "Top filter is not an OR"))
905 (let ((lim (pop ibuffer-filtering-qualifiers)))
906 (setq ibuffer-filtering-qualifiers
907 (nconc (cdr lim) ibuffer-filtering-qualifiers))))
908 (when (< (length ibuffer-filtering-qualifiers) 2)
909 (error "Need two filters to OR"))
910 ;; If the second filter is an OR, just add to it.
911 (let ((first (pop ibuffer-filtering-qualifiers))
912 (second (pop ibuffer-filtering-qualifiers)))
913 (if (eq 'or (car second))
914 (push (nconc (list 'or first) (cdr second))
915 ibuffer-filtering-qualifiers)
916 (push (list 'or first second)
917 ibuffer-filtering-qualifiers))))
918 (ibuffer-update nil t))
920 (defun ibuffer-maybe-save-stuff ()
921 (when ibuffer-save-with-custom
922 (if (fboundp 'customize-save-variable)
923 (progn
924 (customize-save-variable 'ibuffer-saved-filters
925 ibuffer-saved-filters)
926 (customize-save-variable 'ibuffer-saved-filter-groups
927 ibuffer-saved-filter-groups))
928 (message "Not saved permanently: Customize not available"))))
930 ;;;###autoload
931 (defun ibuffer-save-filters (name filters)
932 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'.
933 Interactively, prompt for NAME, and use the current filters."
934 (interactive
935 (if (null ibuffer-filtering-qualifiers)
936 (error "No filters currently in effect")
937 (list
938 (read-from-minibuffer "Save current filters as: ")
939 ibuffer-filtering-qualifiers)))
940 (ibuffer-aif (assoc name ibuffer-saved-filters)
941 (setcdr it filters)
942 (push (list name filters) ibuffer-saved-filters))
943 (ibuffer-maybe-save-stuff))
945 ;;;###autoload
946 (defun ibuffer-delete-saved-filters (name)
947 "Delete saved filters with NAME from `ibuffer-saved-filters'."
948 (interactive
949 (list
950 (if (null ibuffer-saved-filters)
951 (error "No saved filters")
952 (completing-read "Delete saved filters: "
953 ibuffer-saved-filters nil t))))
954 (setq ibuffer-saved-filters
955 (ibuffer-delete-alist name ibuffer-saved-filters))
956 (ibuffer-maybe-save-stuff)
957 (ibuffer-update nil t))
959 ;;;###autoload
960 (defun ibuffer-add-saved-filters (name)
961 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
962 (interactive
963 (list
964 (if (null ibuffer-saved-filters)
965 (error "No saved filters")
966 (completing-read "Add saved filters: "
967 ibuffer-saved-filters nil t))))
968 (push (cons 'saved name) ibuffer-filtering-qualifiers)
969 (ibuffer-update nil t))
971 ;;;###autoload
972 (defun ibuffer-switch-to-saved-filters (name)
973 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'."
974 (interactive
975 (list
976 (if (null ibuffer-saved-filters)
977 (error "No saved filters")
978 (completing-read "Switch to saved filters: "
979 ibuffer-saved-filters nil t))))
980 (setq ibuffer-filtering-qualifiers (list (cons 'saved name)))
981 (ibuffer-update nil t))
983 (defun ibuffer-format-filter-group-data (filter)
984 (if (equal filter "Default")
986 (concat "Filter:" (mapconcat #'ibuffer-format-qualifier
987 (cdr (assq filter ibuffer-filter-groups))
988 " "))))
990 (defun ibuffer-format-qualifier (qualifier)
991 (if (eq (car-safe qualifier) 'not)
992 (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier)) "]")
993 (ibuffer-format-qualifier-1 qualifier)))
995 (defun ibuffer-format-qualifier-1 (qualifier)
996 (pcase (car qualifier)
997 (`saved
998 (concat " [filter: " (cdr qualifier) "]"))
999 (`or
1000 (concat " [OR" (mapconcat #'ibuffer-format-qualifier
1001 (cdr qualifier) "") "]"))
1003 (let ((type (assq (car qualifier) ibuffer-filtering-alist)))
1004 (unless qualifier
1005 (error "Ibuffer: bad qualifier %s" qualifier))
1006 (concat " [" (cadr type) ": " (format "%s]" (cdr qualifier)))))))
1009 (defun ibuffer-list-buffer-modes (&optional include-parents)
1010 "Create a completion table of buffer modes currently in use.
1011 If INCLUDE-PARENTS is non-nil then include parent modes."
1012 (let ((modes))
1013 (dolist (buf (buffer-list))
1014 (let ((this-mode (buffer-local-value 'major-mode buf)))
1015 (while (and this-mode (not (memq this-mode modes)))
1016 (push this-mode modes)
1017 (setq this-mode (and include-parents
1018 (get this-mode 'derived-mode-parent))))))
1019 (mapcar #'symbol-name modes)))
1022 ;;; Extra operation definitions
1024 ;;;###autoload (autoload 'ibuffer-filter-by-mode "ibuf-ext")
1025 (define-ibuffer-filter mode
1026 "Toggle current view to buffers with major mode QUALIFIER."
1027 (:description "major mode"
1028 :reader
1029 (let* ((buf (ibuffer-current-buffer))
1030 (default (if (and buf (buffer-live-p buf))
1031 (symbol-name (buffer-local-value
1032 'major-mode buf)))))
1033 (intern
1034 (completing-read
1035 (if default
1036 (format "Filter by major mode (default %s): " default)
1037 "Filter by major mode: ")
1038 obarray
1039 #'(lambda (e)
1040 (string-match "-mode\\'" (symbol-name e)))
1041 t nil nil default))))
1042 (eq qualifier (buffer-local-value 'major-mode buf)))
1044 ;;;###autoload (autoload 'ibuffer-filter-by-used-mode "ibuf-ext")
1045 (define-ibuffer-filter used-mode
1046 "Toggle current view to buffers with major mode QUALIFIER.
1047 Called interactively, this function allows selection of modes
1048 currently used by buffers."
1049 (:description "major mode in use"
1050 :reader
1051 (let* ((buf (ibuffer-current-buffer))
1052 (default (if (and buf (buffer-live-p buf))
1053 (symbol-name (buffer-local-value
1054 'major-mode buf)))))
1055 (intern
1056 (completing-read
1057 (if default
1058 (format "Filter by major mode (default %s): " default)
1059 "Filter by major mode: ")
1060 (ibuffer-list-buffer-modes) nil t nil nil default))))
1061 (eq qualifier (buffer-local-value 'major-mode buf)))
1063 ;;;###autoload (autoload 'ibuffer-filter-by-derived-mode "ibuf-ext")
1064 (define-ibuffer-filter derived-mode
1065 "Toggle current view to buffers whose major mode inherits from QUALIFIER."
1066 (:description "derived mode"
1067 :reader
1068 (intern
1069 (completing-read "Filter by derived mode: "
1070 (ibuffer-list-buffer-modes t)
1071 nil t)))
1072 (with-current-buffer buf (derived-mode-p qualifier)))
1074 ;;;###autoload (autoload 'ibuffer-filter-by-name "ibuf-ext")
1075 (define-ibuffer-filter name
1076 "Toggle current view to buffers with name matching QUALIFIER."
1077 (:description "buffer name"
1078 :reader (read-from-minibuffer "Filter by name (regexp): "))
1079 (string-match qualifier (buffer-name buf)))
1081 ;;;###autoload (autoload 'ibuffer-filter-by-filename "ibuf-ext")
1082 (define-ibuffer-filter filename
1083 "Toggle current view to buffers with filename matching QUALIFIER."
1084 (:description "filename"
1085 :reader (read-from-minibuffer "Filter by filename (regexp): "))
1086 (ibuffer-awhen (with-current-buffer buf (ibuffer-buffer-file-name))
1087 (string-match qualifier it)))
1089 ;;;###autoload (autoload 'ibuffer-filter-by-size-gt "ibuf-ext")
1090 (define-ibuffer-filter size-gt
1091 "Toggle current view to buffers with size greater than QUALIFIER."
1092 (:description "size greater than"
1093 :reader
1094 (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
1095 (> (with-current-buffer buf (buffer-size))
1096 qualifier))
1098 ;;;###autoload (autoload 'ibuffer-filter-by-size-lt "ibuf-ext")
1099 (define-ibuffer-filter size-lt
1100 "Toggle current view to buffers with size less than QUALIFIER."
1101 (:description "size less than"
1102 :reader
1103 (string-to-number (read-from-minibuffer "Filter by size less than: ")))
1104 (< (with-current-buffer buf (buffer-size))
1105 qualifier))
1107 ;;;###autoload (autoload 'ibuffer-filter-by-content "ibuf-ext")
1108 (define-ibuffer-filter content
1109 "Toggle current view to buffers whose contents match QUALIFIER."
1110 (:description "content"
1111 :reader (read-from-minibuffer "Filter by content (regexp): "))
1112 (with-current-buffer buf
1113 (save-excursion
1114 (goto-char (point-min))
1115 (re-search-forward qualifier nil t))))
1117 ;;;###autoload (autoload 'ibuffer-filter-by-predicate "ibuf-ext")
1118 (define-ibuffer-filter predicate
1119 "Toggle current view to buffers for which QUALIFIER returns non-nil."
1120 (:description "predicate"
1121 :reader (read-minibuffer "Filter by predicate (form): "))
1122 (with-current-buffer buf
1123 (eval qualifier)))
1125 ;;; Sorting
1127 ;;;###autoload
1128 (defun ibuffer-toggle-sorting-mode ()
1129 "Toggle the current sorting mode.
1130 Default sorting modes are:
1131 Recency - the last time the buffer was viewed
1132 Name - the name of the buffer
1133 Major Mode - the name of the major mode of the buffer
1134 Size - the size of the buffer"
1135 (interactive)
1136 (let ((modes (mapcar #'car ibuffer-sorting-functions-alist)))
1137 (cl-pushnew 'recency modes)
1138 (setq modes (sort modes #'string-lessp))
1139 (let ((next (or (car-safe (cdr-safe (memq ibuffer-sorting-mode modes)))
1140 (car modes))))
1141 (setq ibuffer-sorting-mode next)
1142 (message "Sorting by %s" next)))
1143 (ibuffer-redisplay t))
1145 ;;;###autoload
1146 (defun ibuffer-invert-sorting ()
1147 "Toggle whether or not sorting is in reverse order."
1148 (interactive)
1149 (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep))
1150 (message "Sorting order %s"
1151 (if ibuffer-sorting-reversep
1152 "reversed"
1153 "normal"))
1154 (ibuffer-redisplay t))
1156 ;;;###autoload (autoload 'ibuffer-do-sort-by-major-mode "ibuf-ext")
1157 (define-ibuffer-sorter major-mode
1158 "Sort the buffers by major modes.
1159 Ordering is lexicographic."
1160 (:description "major mode")
1161 (string-lessp (downcase
1162 (symbol-name (buffer-local-value 'major-mode (car a))))
1163 (downcase
1164 (symbol-name (buffer-local-value 'major-mode (car b))))))
1166 ;;;###autoload (autoload 'ibuffer-do-sort-by-mode-name "ibuf-ext")
1167 (define-ibuffer-sorter mode-name
1168 "Sort the buffers by their mode name.
1169 Ordering is lexicographic."
1170 (:description "major mode name")
1171 (string-lessp (downcase
1172 (with-current-buffer
1173 (car a)
1174 (format-mode-line mode-name)))
1175 (downcase
1176 (with-current-buffer
1177 (car b)
1178 (format-mode-line mode-name)))))
1180 ;;;###autoload (autoload 'ibuffer-do-sort-by-alphabetic "ibuf-ext")
1181 (define-ibuffer-sorter alphabetic
1182 "Sort the buffers by their names.
1183 Ordering is lexicographic."
1184 (:description "buffer name")
1185 (string-lessp
1186 (buffer-name (car a))
1187 (buffer-name (car b))))
1189 ;;;###autoload (autoload 'ibuffer-do-sort-by-size "ibuf-ext")
1190 (define-ibuffer-sorter size
1191 "Sort the buffers by their size."
1192 (:description "size")
1193 (< (with-current-buffer (car a)
1194 (buffer-size))
1195 (with-current-buffer (car b)
1196 (buffer-size))))
1198 ;;;###autoload (autoload 'ibuffer-do-sort-by-filename/process "ibuf-ext")
1199 (define-ibuffer-sorter filename/process
1200 "Sort the buffers by their file name/process name."
1201 (:description "file name")
1202 (string-lessp
1203 ;; FIXME: For now just compare the file name and the process name
1204 ;; (if it exists). Is there a better way to do this?
1205 (or (buffer-file-name (car a))
1206 (let ((pr-a (get-buffer-process (car a))))
1207 (and (processp pr-a) (process-name pr-a))))
1208 (or (buffer-file-name (car b))
1209 (let ((pr-b (get-buffer-process (car b))))
1210 (and (processp pr-b) (process-name pr-b))))))
1212 ;;; Functions to emulate bs.el
1214 ;;;###autoload
1215 (defun ibuffer-bs-show ()
1216 "Emulate `bs-show' from the bs.el package."
1217 (interactive)
1218 (ibuffer t "*Ibuffer-bs*" '((filename . ".*")) nil t)
1219 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all))
1221 (defun ibuffer-bs-toggle-all ()
1222 "Emulate `bs-toggle-show-all' from the bs.el package."
1223 (interactive)
1224 (if ibuffer-filtering-qualifiers
1225 (ibuffer-pop-filter)
1226 (progn (ibuffer-push-filter '(filename . ".*"))
1227 (ibuffer-update nil t))))
1229 ;;; Handy functions
1231 ;;;###autoload
1232 (defun ibuffer-add-to-tmp-hide (regexp)
1233 "Add REGEXP to `ibuffer-tmp-hide-regexps'.
1234 This means that buffers whose name matches REGEXP will not be shown
1235 for this Ibuffer session."
1236 (interactive
1237 (list
1238 (read-from-minibuffer "Never show buffers matching: "
1239 (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
1240 (push regexp ibuffer-tmp-hide-regexps))
1242 ;;;###autoload
1243 (defun ibuffer-add-to-tmp-show (regexp)
1244 "Add REGEXP to `ibuffer-tmp-show-regexps'.
1245 This means that buffers whose name matches REGEXP will always be shown
1246 for this Ibuffer session."
1247 (interactive
1248 (list
1249 (read-from-minibuffer "Always show buffers matching: "
1250 (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
1251 (push regexp ibuffer-tmp-show-regexps))
1253 ;;;###autoload
1254 (defun ibuffer-forward-next-marked (&optional count mark direction)
1255 "Move forward by COUNT marked buffers (default 1).
1257 If MARK is non-nil, it should be a character denoting the type of mark
1258 to move by. The default is `ibuffer-marked-char'.
1260 If DIRECTION is non-nil, it should be an integer; negative integers
1261 mean move backwards, non-negative integers mean move forwards."
1262 (interactive "P")
1263 (unless count
1264 (setq count 1))
1265 (unless mark
1266 (setq mark ibuffer-marked-char))
1267 (unless direction
1268 (setq direction 1))
1269 ;; Skip the title
1270 (ibuffer-forward-line 0)
1271 (let ((opos (point)))
1272 (ibuffer-forward-line direction)
1273 (while (not (or (= (point) opos)
1274 (eq (ibuffer-current-mark) mark)))
1275 (ibuffer-forward-line direction))
1276 (when (and (= (point) opos)
1277 (not (eq (ibuffer-current-mark) mark)))
1278 (error "No buffers with mark %c" mark))))
1280 ;;;###autoload
1281 (defun ibuffer-backwards-next-marked (&optional count mark)
1282 "Move backwards by COUNT marked buffers (default 1).
1284 If MARK is non-nil, it should be a character denoting the type of mark
1285 to move by. The default is `ibuffer-marked-char'."
1286 (interactive "P")
1287 (ibuffer-forward-next-marked count mark -1))
1289 ;;;###autoload
1290 (defun ibuffer-do-kill-lines ()
1291 "Hide all of the currently marked lines."
1292 (interactive)
1293 (if (= (ibuffer-count-marked-lines) 0)
1294 (message "No buffers marked; use 'm' to mark a buffer")
1295 (let ((count
1296 (ibuffer-map-marked-lines
1297 #'(lambda (_buf _mark)
1298 'kill))))
1299 (message "Killed %s lines" count))))
1301 ;;;###autoload
1302 (defun ibuffer-jump-to-buffer (name)
1303 "Move point to the buffer whose name is NAME.
1305 If called interactively, prompt for a buffer name and go to the
1306 corresponding line in the Ibuffer buffer. If said buffer is in a
1307 hidden group filter, open it.
1309 If `ibuffer-jump-offer-only-visible-buffers' is non-nil, only offer
1310 visible buffers in the completion list. Calling the command with
1311 a prefix argument reverses the meaning of that variable."
1312 (interactive (list
1313 (let ((only-visible ibuffer-jump-offer-only-visible-buffers))
1314 (when current-prefix-arg
1315 (setq only-visible (not only-visible)))
1316 (if only-visible
1317 (let ((table (mapcar #'(lambda (x)
1318 (buffer-name (car x)))
1319 (ibuffer-current-state-list))))
1320 (when (null table)
1321 (error "No buffers!"))
1322 (completing-read "Jump to buffer: "
1323 table nil t))
1324 (read-buffer "Jump to buffer: " nil t)))))
1325 (when (not (string= "" name))
1326 (let (buf-point)
1327 ;; Blindly search for our buffer: it is very likely that it is
1328 ;; not in a hidden filter group.
1329 (ibuffer-map-lines #'(lambda (buf _marks)
1330 (when (string= (buffer-name buf) name)
1331 (setq buf-point (point))
1332 nil))
1333 t nil)
1334 (when (and
1335 (null buf-point)
1336 (not (null ibuffer-hidden-filter-groups)))
1337 ;; We did not find our buffer. It must be in a hidden filter
1338 ;; group, so go through all hidden filter groups to find it.
1339 (catch 'found
1340 (dolist (group ibuffer-hidden-filter-groups)
1341 (ibuffer-jump-to-filter-group group)
1342 (ibuffer-toggle-filter-group)
1343 (ibuffer-map-lines #'(lambda (buf _marks)
1344 (when (string= (buffer-name buf) name)
1345 (setq buf-point (point))
1346 nil))
1347 t group)
1348 (if buf-point
1349 (throw 'found nil)
1350 (ibuffer-toggle-filter-group)))))
1351 (if (null buf-point)
1352 ;; Still not found even though we expanded all hidden filter
1353 ;; groups: that must be because it's hidden by predicate:
1354 ;; we won't bother trying to display it.
1355 (error "No buffer with name %s" name)
1356 (goto-char buf-point)))))
1358 (declare-function diff-sentinel "diff"
1359 (code &optional old-temp-file new-temp-file))
1361 (defun ibuffer-diff-buffer-with-file-1 (buffer)
1362 (let ((bufferfile (buffer-local-value 'buffer-file-name buffer))
1363 (tempfile (make-temp-file "buffer-content-")))
1364 (when bufferfile
1365 (unwind-protect
1366 (progn
1367 (with-current-buffer buffer
1368 (write-region nil nil tempfile nil 'nomessage))
1369 (let* ((old (expand-file-name bufferfile))
1370 (new (expand-file-name tempfile))
1371 (oldtmp (file-local-copy old))
1372 (newtmp (file-local-copy new))
1373 (switches diff-switches)
1374 (command
1375 (mapconcat
1376 'identity
1377 `(,diff-command
1378 ;; Use explicitly specified switches
1379 ,@(if (listp switches) switches (list switches))
1380 ,@(if (or old new)
1381 (list "-L" (shell-quote-argument old)
1382 "-L" (shell-quote-argument
1383 (format "Buffer %s" (buffer-name buffer)))))
1384 ,(shell-quote-argument (or oldtmp old))
1385 ,(shell-quote-argument (or newtmp new)))
1386 " ")))
1387 (let ((inhibit-read-only t))
1388 (insert command "\n")
1389 (diff-sentinel
1390 (call-process shell-file-name nil
1391 (current-buffer) nil
1392 shell-command-switch command))
1393 (insert "\n")))))
1394 (sit-for 0)
1395 (when (file-exists-p tempfile)
1396 (delete-file tempfile)))))
1398 ;;;###autoload
1399 (defun ibuffer-diff-with-file ()
1400 "View the differences between marked buffers and their associated files.
1401 If no buffers are marked, use buffer at point.
1402 This requires the external program \"diff\" to be in your `exec-path'."
1403 (interactive)
1404 (require 'diff)
1405 (let ((marked-bufs (ibuffer-get-marked-buffers)))
1406 (when (null marked-bufs)
1407 (setq marked-bufs (list (ibuffer-current-buffer t))))
1408 (with-current-buffer (get-buffer-create "*Ibuffer Diff*")
1409 (setq buffer-read-only nil)
1410 (buffer-disable-undo (current-buffer))
1411 (erase-buffer)
1412 (buffer-enable-undo (current-buffer))
1413 (diff-mode)
1414 (dolist (buf marked-bufs)
1415 (unless (buffer-live-p buf)
1416 (error "Buffer %s has been killed" buf))
1417 (ibuffer-diff-buffer-with-file-1 buf))
1418 (setq buffer-read-only t)))
1419 (switch-to-buffer "*Ibuffer Diff*"))
1421 ;;;###autoload
1422 (defun ibuffer-copy-filename-as-kill (&optional arg)
1423 "Copy filenames of marked buffers into the kill ring.
1425 The names are separated by a space.
1426 If a buffer has no filename, it is ignored.
1428 With no prefix arg, use the filename sans its directory of each marked file.
1429 With a zero prefix arg, use the complete filename of each marked file.
1430 With \\[universal-argument], use the filename of each marked file relative
1431 to `ibuffer-default-directory' if non-nil, otherwise `default-directory'.
1433 You can then feed the file name(s) to other commands with \\[yank]."
1434 (interactive "p")
1435 (if (zerop (ibuffer-count-marked-lines))
1436 (message "No buffers marked; use 'm' to mark a buffer")
1437 (let ((result "")
1438 (type (cond ((or (null arg) (zerop arg))
1439 'full)
1440 ((= arg 4)
1441 'relative)
1443 'name))))
1444 (ibuffer-map-marked-lines
1445 #'(lambda (buf _mark)
1446 (setq result
1447 (concat result
1448 (let ((name (buffer-file-name buf)))
1449 (cond (name
1450 (concat
1451 (pcase type
1452 (`full
1453 name)
1454 (`relative
1455 (file-relative-name
1456 name (or ibuffer-default-directory
1457 default-directory)))
1459 (file-name-nondirectory name))) " "))
1460 (t "")))))))
1461 (when (not (zerop (length result)))
1462 (setq result
1463 (substring result 0 -1)))
1464 (kill-new result)
1465 (message "%s" result))))
1467 ;;;###autoload
1468 (defun ibuffer-copy-buffername-as-kill ()
1469 "Copy buffer names of marked buffers into the kill ring.
1470 The names are separated by a space.
1471 You can then feed the file name(s) to other commands with \\[yank]."
1472 (interactive)
1473 (if (zerop (ibuffer-count-marked-lines))
1474 (message "No buffers marked; use 'm' to mark a buffer")
1475 (let ((res ""))
1476 (ibuffer-map-marked-lines
1477 #'(lambda (buf _mark)
1478 (setq res (concat res (buffer-name buf) " "))))
1479 (when (not (zerop (length res)))
1480 (setq res (substring res 0 -1)))
1481 (kill-new res)
1482 (message res))))
1484 (defun ibuffer-mark-on-buffer (func &optional ibuffer-mark-on-buffer-mark group)
1485 (let ((count
1486 (ibuffer-map-lines
1487 #'(lambda (buf _mark)
1488 (when (funcall func buf)
1489 (ibuffer-set-mark-1 (or ibuffer-mark-on-buffer-mark
1490 ibuffer-marked-char))
1493 group)))
1494 (ibuffer-redisplay t)
1495 (unless (eq ibuffer-mark-on-buffer-mark ?\s)
1496 (message "Marked %s buffers" count))))
1498 ;;;###autoload
1499 (defun ibuffer-mark-by-name-regexp (regexp)
1500 "Mark all buffers whose name matches REGEXP."
1501 (interactive "sMark by name (regexp): ")
1502 (ibuffer-mark-on-buffer
1503 #'(lambda (buf)
1504 (string-match regexp (buffer-name buf)))))
1506 (defun ibuffer-locked-buffer-p (&optional buf)
1507 "Return non-nil if BUF is locked.
1508 When BUF nil, default to the buffer at current line."
1509 (let ((cbuffer (or buf (ibuffer-current-buffer))))
1510 (when cbuffer
1511 (with-current-buffer cbuffer
1512 (and (boundp 'emacs-lock-mode) emacs-lock-mode)))))
1514 ;;;###autoload
1515 (defun ibuffer-mark-by-locked ()
1516 "Mark all locked buffers."
1517 (interactive)
1518 (when (featurep 'emacs-lock)
1519 (ibuffer-mark-on-buffer
1520 (lambda (buf)
1521 (ibuffer-locked-buffer-p buf)))))
1523 ;;;###autoload
1524 (defun ibuffer-mark-by-mode-regexp (regexp)
1525 "Mark all buffers whose major mode matches REGEXP."
1526 (interactive "sMark by major mode (regexp): ")
1527 (ibuffer-mark-on-buffer
1528 #'(lambda (buf)
1529 (with-current-buffer buf
1530 (string-match regexp (format-mode-line mode-name nil nil buf))))))
1532 ;;;###autoload
1533 (defun ibuffer-mark-by-file-name-regexp (regexp)
1534 "Mark all buffers whose file name matches REGEXP."
1535 (interactive "sMark by file name (regexp): ")
1536 (ibuffer-mark-on-buffer
1537 #'(lambda (buf)
1538 (let ((name (or (buffer-file-name buf)
1539 (with-current-buffer buf
1540 (and
1541 (boundp 'dired-directory)
1542 (stringp dired-directory)
1543 dired-directory)))))
1544 (when name
1545 (string-match regexp name))))))
1547 ;;;###autoload
1548 (defun ibuffer-mark-by-content-regexp (regexp &optional all-buffers)
1549 "Mark all buffers whose content matches REGEXP.
1550 Optional arg ALL-BUFFERS, if non-nil, then search in all buffers.
1551 Otherwise buffers whose name matches an element of
1552 `ibuffer-never-search-content-name' or whose major mode is on
1553 `ibuffer-never-search-content-mode' are excluded."
1554 (interactive (let ((reg (read-string "Mark by content (regexp): ")))
1555 (list reg current-prefix-arg)))
1556 (ibuffer-mark-on-buffer
1557 #'(lambda (buf)
1558 (let ((mode (with-current-buffer buf major-mode))
1559 res)
1560 (cond ((and (not all-buffers)
1562 (memq mode ibuffer-never-search-content-mode)
1563 (cl-some (lambda (x) (string-match x (buffer-name buf)))
1564 ibuffer-never-search-content-name)))
1565 (setq res nil))
1567 (with-current-buffer buf
1568 (save-mark-and-excursion
1569 (goto-char (point-min))
1570 (setq res (re-search-forward regexp nil t)))))) res))))
1572 ;;;###autoload
1573 (defun ibuffer-mark-by-mode (mode)
1574 "Mark all buffers whose major mode equals MODE."
1575 (interactive
1576 (let* ((buf (ibuffer-current-buffer))
1577 (default (if (and buf (buffer-live-p buf))
1578 (symbol-name (buffer-local-value
1579 'major-mode buf)))))
1580 (list (intern
1581 (completing-read
1582 (if default
1583 (format "Mark by major mode (default %s): " default)
1584 "Mark by major mode: ")
1585 (ibuffer-list-buffer-modes) nil t nil nil default)))))
1586 (ibuffer-mark-on-buffer
1587 #'(lambda (buf)
1588 (eq (buffer-local-value 'major-mode buf) mode))))
1590 ;;;###autoload
1591 (defun ibuffer-mark-modified-buffers ()
1592 "Mark all modified buffers."
1593 (interactive)
1594 (ibuffer-mark-on-buffer
1595 #'(lambda (buf) (buffer-modified-p buf))))
1597 ;;;###autoload
1598 (defun ibuffer-mark-unsaved-buffers ()
1599 "Mark all modified buffers that have an associated file."
1600 (interactive)
1601 (ibuffer-mark-on-buffer
1602 #'(lambda (buf) (and (buffer-local-value 'buffer-file-name buf)
1603 (buffer-modified-p buf)))))
1605 ;;;###autoload
1606 (defun ibuffer-mark-dissociated-buffers ()
1607 "Mark all buffers whose associated file does not exist."
1608 (interactive)
1609 (ibuffer-mark-on-buffer
1610 #'(lambda (buf)
1611 (with-current-buffer buf
1613 (and buffer-file-name
1614 (not (file-exists-p buffer-file-name)))
1615 (and (eq major-mode 'dired-mode)
1616 (boundp 'dired-directory)
1617 (stringp dired-directory)
1618 (not (file-exists-p (file-name-directory dired-directory)))))))))
1620 ;;;###autoload
1621 (defun ibuffer-mark-help-buffers ()
1622 "Mark buffers whose major mode is in variable `ibuffer-help-buffer-modes'."
1623 (interactive)
1624 (ibuffer-mark-on-buffer
1625 #'(lambda (buf)
1626 (with-current-buffer buf
1627 (memq major-mode ibuffer-help-buffer-modes)))))
1629 ;;;###autoload
1630 (defun ibuffer-mark-compressed-file-buffers ()
1631 "Mark buffers whose associated file is compressed."
1632 (interactive)
1633 (ibuffer-mark-on-buffer
1634 #'(lambda (buf)
1635 (with-current-buffer buf
1636 (and buffer-file-name
1637 (string-match ibuffer-compressed-file-name-regexp
1638 buffer-file-name))))))
1640 ;;;###autoload
1641 (defun ibuffer-mark-old-buffers ()
1642 "Mark buffers which have not been viewed in `ibuffer-old-time' hours."
1643 (interactive)
1644 (ibuffer-mark-on-buffer
1645 #'(lambda (buf)
1646 (with-current-buffer buf
1647 ;; hacked from midnight.el
1648 (when buffer-display-time
1649 (let* ((now (float-time))
1650 (then (float-time buffer-display-time)))
1651 (> (- now then) (* 60 60 ibuffer-old-time))))))))
1653 ;;;###autoload
1654 (defun ibuffer-mark-special-buffers ()
1655 "Mark all buffers whose name begins and ends with `*'."
1656 (interactive)
1657 (ibuffer-mark-on-buffer
1658 #'(lambda (buf) (string-match "^\\*.+\\*$"
1659 (buffer-name buf)))))
1661 ;;;###autoload
1662 (defun ibuffer-mark-read-only-buffers ()
1663 "Mark all read-only buffers."
1664 (interactive)
1665 (ibuffer-mark-on-buffer
1666 #'(lambda (buf) (buffer-local-value 'buffer-read-only buf))))
1668 ;;;###autoload
1669 (defun ibuffer-mark-dired-buffers ()
1670 "Mark all `dired' buffers."
1671 (interactive)
1672 (ibuffer-mark-on-buffer
1673 #'(lambda (buf) (eq (buffer-local-value 'major-mode buf) 'dired-mode))))
1675 ;;;###autoload
1676 (defun ibuffer-do-occur (regexp &optional nlines)
1677 "View lines which match REGEXP in all marked buffers.
1678 Optional argument NLINES says how many lines of context to display: it
1679 defaults to one."
1680 (interactive (occur-read-primary-args))
1681 (if (or (not (integerp nlines))
1682 (< nlines 0))
1683 (setq nlines 0))
1684 (when (zerop (ibuffer-count-marked-lines))
1685 (ibuffer-set-mark ibuffer-marked-char))
1686 (let ((ibuffer-do-occur-bufs nil))
1687 ;; Accumulate a list of marked buffers
1688 (ibuffer-map-marked-lines
1689 #'(lambda (buf _mark)
1690 (push buf ibuffer-do-occur-bufs)))
1691 (occur-1 regexp nlines ibuffer-do-occur-bufs)))
1693 (provide 'ibuf-ext)
1695 ;; Local Variables:
1696 ;; generated-autoload-file: "ibuffer-loaddefs.el"
1697 ;; End:
1699 ;;; ibuf-ext.el ends here