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>
8 ;; Keywords: buffer, convenience
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/>.
28 ;; These functions should be automatically loaded when called, but you
29 ;; can explicitly (require 'ibuf-ext) in your ~/.emacs to have them
41 (defun ibuffer-delete-alist (key alist
)
42 "Delete all entries in ALIST that have a key equal to KEY."
44 (while (setq entry
(assoc key alist
))
45 (setq alist
(delete entry alist
)))
49 (defun ibuffer-remove-duplicates (list)
50 "Return a copy of LIST with duplicate elements removed."
54 (or (member (car tail
) new
)
55 (setq new
(cons (car tail
) new
)))
56 (setq tail
(cdr tail
)))
59 (defun ibuffer-split-list (ibuffer-split-list-fn ibuffer-split-list-elts
)
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
))
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
))
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 "))
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."
102 :type
'(repeat regexp
)
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."
110 :type
'(repeat regexp
)
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
)
125 (mode . gnus-group-mode
)
126 (mode . gnus-summary-mode
)
127 (mode . gnus-article-mode
))))
129 ((or (mode . emacs-lisp-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'."
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)))
174 :type
'(repeat (list :tag
"Association" (symbol :tag
"Filter")
175 (list :tag
"Formats" (repeat (sexp :tag
"Format")))))
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."
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'."
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"))
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'."
227 (defun ibuffer-ext-visible-p (buf all
&optional ibuffer-buf
)
229 (ibuffer-buf-matches-predicates buf ibuffer-tmp-show-regexps
)
232 (ibuffer-buf-matches-predicates buf ibuffer-tmp-hide-regexps
)
233 (ibuffer-buf-matches-predicates buf ibuffer-never-show-predicates
)))
236 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates
)))
237 (or ibuffer-view-ibuffer
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
)))))
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."
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))
263 (with-current-buffer buf
264 (when (and ibuffer-auto-mode
265 (derived-mode-p 'ibuffer-mode
))
266 (ibuffer-update nil t
)))))))
269 (defun ibuffer-mouse-filter-by-mode (event)
270 "Enable or disable filtering by the major mode chosen via mouse."
272 (ibuffer-interactive-filter-by-mode event
))
275 (defun ibuffer-interactive-filter-by-mode (event-or-point)
276 "Enable or disable filtering by the major mode at point."
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
))
289 (defun ibuffer-mouse-toggle-filter-group (event)
290 "Toggle the display status of the filter group chosen with the mouse."
292 (ibuffer-toggle-filter-group-1 (save-excursion
293 (mouse-set-point event
)
297 (defun ibuffer-toggle-filter-group ()
298 "Toggle the display status of the filter group on this line."
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
)))
313 (defun ibuffer-forward-filter-group (&optional count
)
314 "Move point forwards by COUNT filtering groups."
319 (when (get-text-property (point) 'ibuffer-filter-group-name
)
320 (goto-char (next-single-property-change
321 (point) 'ibuffer-filter-group-name
323 (goto-char (next-single-property-change
324 (point) 'ibuffer-filter-group-name
326 (ibuffer-forward-filter-group (1- count
)))
327 (ibuffer-forward-line 0))
330 (defun ibuffer-backward-filter-group (&optional count
)
331 "Move point backwards by COUNT filtering groups."
336 (when (get-text-property (point) 'ibuffer-filter-group-name
)
337 (goto-char (previous-single-property-change
338 (point) 'ibuffer-filter-group-name
340 (goto-char (previous-single-property-change
341 (point) 'ibuffer-filter-group-name
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"
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"
366 (with-current-buffer buf
367 (shell-command-on-region (point-min) (point-max)
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"
376 (shell-command (concat command
" "
377 (shell-quote-argument
383 (min 10 (length (buffer-name)))))))
384 (write-region nil nil file nil
0)
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."
394 (read-from-minibuffer
395 "Eval in buffers (form): "
396 nil read-expression-map t
'read-expression-history
))
397 :opstring
"evaluated in"
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'."
407 (read-from-minibuffer
408 "Eval viewing in buffers (form): "
409 nil read-expression-map t
'read-expression-history
))
410 :opstring
"evaluated in"
413 (let ((ibuffer-buf (current-buffer)))
416 (switch-to-buffer buf
)
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'."
427 ;;;###autoload (autoload 'ibuffer-do-revert "ibuf-ext")
428 (define-ibuffer-op revert
()
429 "Revert marked buffers as with `revert-buffer'."
432 :active-opstring
"revert"
436 ;;;###autoload (autoload 'ibuffer-do-isearch "ibuf-ext")
437 (define-ibuffer-op ibuffer-do-isearch
()
438 "Perform a `isearch-forward' in marked buffers."
440 :opstring
"searched in"
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."
449 :opstring
"searched regexp in"
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."
458 (let* ((from-str (read-from-minibuffer "Replace regexp: "))
459 (to-str (read-from-minibuffer (concat "Replace " from-str
461 (list from-str to-str
))
462 :opstring
"replaced in"
465 (save-window-excursion
466 (switch-to-buffer buf
)
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."
478 (query-replace-read-args "Query replace" t t
)
479 :opstring
"replaced in"
482 (save-window-excursion
483 (switch-to-buffer buf
)
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."
494 (query-replace-read-args "Query replace regexp" t t
)
495 :opstring
"replaced in"
498 (save-window-excursion
499 (switch-to-buffer buf
)
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'."
514 (defun ibuffer-included-in-filters-p (buf filters
)
516 (memq nil
;; a filter will return nil if it failed
518 ;; filter should be like (TYPE . QUALIFIER), or
519 ;; (or (TYPE . QUALIFIER) (TYPE . QUALIFIER) ...)
521 (ibuffer-included-in-filter-p buf qual
))
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
)
534 (memq t
(mapcar #'(lambda (x)
535 (ibuffer-included-in-filter-p buf x
))
540 ibuffer-saved-filters
)))
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
)))
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
))
563 (dolist (filtergroup filter-group-alist
)
564 (let ((filterset (cdr filtergroup
)))
565 (cl-multiple-value-bind (hip-crowd lamers
)
567 (ibuffer-split-list (lambda (bufmark)
568 (ibuffer-included-in-filters-p (car bufmark
)
571 (aset vec i hip-crowd
)
573 (setq bmarklist lamers
))))
576 (let ((bufs (aref vec j
)))
577 (unless (and noempty
(null bufs
))
578 (push (cons (car (nth j filter-group-alist
))
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))
593 (defun ibuffer-set-filter-groups-by-mode ()
594 "Set the current filter groups to filter by mode."
596 (setq ibuffer-filter-groups
597 (mapcar (lambda (mode)
598 (cons (format "%s" mode
) `((mode .
,mode
))))
600 (ibuffer-remove-duplicates
601 (mapcar (lambda (buf)
602 (buffer-local-value 'major-mode buf
))
604 (if ibuffer-view-ibuffer
606 (delq 'ibuffer-mode modes
)))))
607 (ibuffer-update nil t
))
610 (defun ibuffer-pop-filter-group ()
611 "Remove the first filter group."
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
)
633 (remove "Default" ibuffer-hidden-filter-groups
)
634 ibuffer-hidden-filter-groups
))
638 (defun ibuffer-decompose-filter-group (group)
639 "Decompose the filter group GROUP into active filters."
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
))
649 (defun ibuffer-clear-filter-groups ()
650 "Remove all filter groups."
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 ()
658 (goto-char (point-min))
661 (while (and (not (eobp))
662 (setq pos
(next-single-property-change
663 (point) 'ibuffer-filter-group-name
)))
665 (push (cons (get-text-property (point) 'ibuffer-filter-group-name
)
668 (goto-char (next-single-property-change
669 pos
'ibuffer-filter-group-name
)))
673 (defun ibuffer-jump-to-filter-group (name)
674 "Move point to the filter group whose name is NAME."
676 (list (ibuffer-read-filter-group-name "Jump to filter group: ")))
677 (ibuffer-aif (assoc name
(ibuffer-current-filter-groups-with-position))
679 (error "No filter group with name %s" name
)))
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
)
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
))
699 (defun ibuffer-kill-line (&optional arg interactive-p
)
700 "Kill the filter group at point.
701 See also `ibuffer-kill-filter-group'."
703 (ibuffer-aif (save-excursion
704 (ibuffer-forward-line 0)
705 (get-text-property (point) 'ibuffer-filter-group-name
))
707 (ibuffer-kill-filter-group it
))
708 (funcall (if interactive-p
#'call-interactively
#'funcall
)
711 (defun ibuffer-insert-filter-group-before (newgroup group
)
713 (pos (let ((groups (mapcar #'car ibuffer-filter-groups
))
716 (if (equal (car groups
) group
)
720 (setq groups
(cdr groups
))))
723 (setq ibuffer-filter-groups
724 (nconc ibuffer-filter-groups
(list newgroup
))))
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
))))))
733 (defun ibuffer-yank ()
734 "Yank the last killed filter group before group at point."
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"))))
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"))
749 (ibuffer-forward-line 0)
750 (ibuffer-insert-filter-group-before (pop ibuffer-filter-group-kill-ring
)
752 (ibuffer-update nil t
))
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."
760 (if (null ibuffer-filter-groups
)
761 (error "No filter groups active")
763 (read-from-minibuffer "Save current filter groups as: ")
764 ibuffer-filter-groups
)))
765 (ibuffer-aif (assoc name ibuffer-saved-filter-groups
)
767 (push (cons name groups
) ibuffer-saved-filter-groups
))
768 (ibuffer-maybe-save-stuff))
771 (defun ibuffer-delete-saved-filter-groups (name)
772 "Delete saved filter groups with NAME.
773 They are removed from `ibuffer-saved-filter-groups'."
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
))
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."
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
))
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."
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
)
817 (ibuffer-jump-to-buffer (buffer-name buf
)))))
820 (defun ibuffer-pop-filter ()
821 "Remove the top filter in this buffer."
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
)
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
))
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]."
843 (when (null ibuffer-filtering-qualifiers
)
844 (error "No filters in effect"))
845 (let ((lim (pop ibuffer-filtering-qualifiers
)))
848 (setq ibuffer-filtering-qualifiers
(append
850 ibuffer-filtering-qualifiers
)))
854 ibuffer-saved-filters
)))
856 (ibuffer-filter-disable)
857 (error "Unknown saved filter %s" (cdr lim
)))
858 (setq ibuffer-filtering-qualifiers
(append
860 ibuffer-filtering-qualifiers
))))
863 ibuffer-filtering-qualifiers
))
865 (error "Filter type %s is not compound" (car lim
)))))
866 (ibuffer-update nil t
))
869 (defun ibuffer-exchange-filters ()
870 "Exchange the top two filters on the stack in this buffer."
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
))
882 (defun ibuffer-negate-filter ()
883 "Negate the sense of the top filter in the current buffer."
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
)
891 ibuffer-filtering-qualifiers
))
892 (ibuffer-update nil t
))
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
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
)
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"))))
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."
935 (if (null ibuffer-filtering-qualifiers
)
936 (error "No filters currently in effect")
938 (read-from-minibuffer "Save current filters as: ")
939 ibuffer-filtering-qualifiers
)))
940 (ibuffer-aif (assoc name ibuffer-saved-filters
)
942 (push (list name filters
) ibuffer-saved-filters
))
943 (ibuffer-maybe-save-stuff))
946 (defun ibuffer-delete-saved-filters (name)
947 "Delete saved filters with NAME from `ibuffer-saved-filters'."
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
))
960 (defun ibuffer-add-saved-filters (name)
961 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
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
))
972 (defun ibuffer-switch-to-saved-filters (name)
973 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'."
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
))
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
)
998 (concat " [filter: " (cdr qualifier
) "]"))
1000 (concat " [OR" (mapconcat #'ibuffer-format-qualifier
1001 (cdr qualifier
) "") "]"))
1003 (let ((type (assq (car qualifier
) ibuffer-filtering-alist
)))
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."
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"
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
)))))
1036 (format "Filter by major mode (default %s): " default
)
1037 "Filter by major mode: ")
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"
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
)))))
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"
1069 (completing-read "Filter by derived mode: "
1070 (ibuffer-list-buffer-modes 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"
1094 (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
1095 (> (with-current-buffer buf
(buffer-size))
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"
1103 (string-to-number (read-from-minibuffer "Filter by size less than: ")))
1104 (< (with-current-buffer buf
(buffer-size))
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
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
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"
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
)))
1141 (setq ibuffer-sorting-mode next
)
1142 (message "Sorting by %s" next
)))
1143 (ibuffer-redisplay t
))
1146 (defun ibuffer-invert-sorting ()
1147 "Toggle whether or not sorting is in reverse order."
1149 (setq ibuffer-sorting-reversep
(not ibuffer-sorting-reversep
))
1150 (message "Sorting order %s"
1151 (if ibuffer-sorting-reversep
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
))))
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
1174 (format-mode-line mode-name
)))
1176 (with-current-buffer
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")
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
)
1195 (with-current-buffer (car b
)
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")
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
1215 (defun ibuffer-bs-show ()
1216 "Emulate `bs-show' from the bs.el package."
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."
1224 (if ibuffer-filtering-qualifiers
1225 (ibuffer-pop-filter)
1226 (progn (ibuffer-push-filter '(filename .
".*"))
1227 (ibuffer-update nil t
))))
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."
1238 (read-from-minibuffer "Never show buffers matching: "
1239 (regexp-quote (buffer-name (ibuffer-current-buffer t
))))))
1240 (push regexp ibuffer-tmp-hide-regexps
))
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."
1249 (read-from-minibuffer "Always show buffers matching: "
1250 (regexp-quote (buffer-name (ibuffer-current-buffer t
))))))
1251 (push regexp ibuffer-tmp-show-regexps
))
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."
1266 (setq mark ibuffer-marked-char
))
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
))))
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'."
1287 (ibuffer-forward-next-marked count mark -
1))
1290 (defun ibuffer-do-kill-lines ()
1291 "Hide all of the currently marked lines."
1293 (if (= (ibuffer-count-marked-lines) 0)
1294 (message "No buffers marked; use 'm' to mark a buffer")
1296 (ibuffer-map-marked-lines
1297 #'(lambda (_buf _mark
)
1299 (message "Killed %s lines" count
))))
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."
1313 (let ((only-visible ibuffer-jump-offer-only-visible-buffers
))
1314 (when current-prefix-arg
1315 (setq only-visible
(not only-visible
)))
1317 (let ((table (mapcar #'(lambda (x)
1318 (buffer-name (car x
)))
1319 (ibuffer-current-state-list))))
1321 (error "No buffers!"))
1322 (completing-read "Jump to buffer: "
1324 (read-buffer "Jump to buffer: " nil t
)))))
1325 (when (not (string= "" name
))
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))
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.
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))
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-")))
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
)
1378 ;; Use explicitly specified switches
1379 ,@(if (listp switches
) switches
(list switches
))
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
)))
1387 (let ((inhibit-read-only t
))
1388 (insert command
"\n")
1390 (call-process shell-file-name nil
1391 (current-buffer) nil
1392 shell-command-switch command
))
1395 (when (file-exists-p tempfile
)
1396 (delete-file tempfile
)))))
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'."
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))
1412 (buffer-enable-undo (current-buffer))
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*"))
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]."
1435 (if (zerop (ibuffer-count-marked-lines))
1436 (message "No buffers marked; use 'm' to mark a buffer")
1438 (type (cond ((or (null arg
) (zerop arg
))
1444 (ibuffer-map-marked-lines
1445 #'(lambda (buf _mark
)
1448 (let ((name (buffer-file-name buf
)))
1456 name
(or ibuffer-default-directory
1457 default-directory
)))
1459 (file-name-nondirectory name
))) " "))
1461 (when (not (zerop (length result
)))
1463 (substring result
0 -
1)))
1465 (message "%s" result
))))
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]."
1473 (if (zerop (ibuffer-count-marked-lines))
1474 (message "No buffers marked; use 'm' to mark a buffer")
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)))
1484 (defun ibuffer-mark-on-buffer (func &optional ibuffer-mark-on-buffer-mark group
)
1487 #'(lambda (buf _mark
)
1488 (when (funcall func buf
)
1489 (ibuffer-set-mark-1 (or ibuffer-mark-on-buffer-mark
1490 ibuffer-marked-char
))
1494 (ibuffer-redisplay t
)
1495 (unless (eq ibuffer-mark-on-buffer-mark ?\s
)
1496 (message "Marked %s buffers" count
))))
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
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))))
1511 (with-current-buffer cbuffer
1512 (and (boundp 'emacs-lock-mode
) emacs-lock-mode
)))))
1515 (defun ibuffer-mark-by-locked ()
1516 "Mark all locked buffers."
1518 (when (featurep 'emacs-lock
)
1519 (ibuffer-mark-on-buffer
1521 (ibuffer-locked-buffer-p buf
)))))
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
1529 (with-current-buffer buf
1530 (string-match regexp
(format-mode-line mode-name nil nil buf
))))))
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
1538 (let ((name (or (buffer-file-name buf
)
1539 (with-current-buffer buf
1541 (boundp 'dired-directory
)
1542 (stringp dired-directory
)
1543 dired-directory
)))))
1545 (string-match regexp name
))))))
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
1558 (let ((mode (with-current-buffer buf major-mode
))
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
)))
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
))))
1573 (defun ibuffer-mark-by-mode (mode)
1574 "Mark all buffers whose major mode equals MODE."
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
)))))
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
1588 (eq (buffer-local-value 'major-mode buf
) mode
))))
1591 (defun ibuffer-mark-modified-buffers ()
1592 "Mark all modified buffers."
1594 (ibuffer-mark-on-buffer
1595 #'(lambda (buf) (buffer-modified-p buf
))))
1598 (defun ibuffer-mark-unsaved-buffers ()
1599 "Mark all modified buffers that have an associated file."
1601 (ibuffer-mark-on-buffer
1602 #'(lambda (buf) (and (buffer-local-value 'buffer-file-name buf
)
1603 (buffer-modified-p buf
)))))
1606 (defun ibuffer-mark-dissociated-buffers ()
1607 "Mark all buffers whose associated file does not exist."
1609 (ibuffer-mark-on-buffer
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
)))))))))
1621 (defun ibuffer-mark-help-buffers ()
1622 "Mark buffers whose major mode is in variable `ibuffer-help-buffer-modes'."
1624 (ibuffer-mark-on-buffer
1626 (with-current-buffer buf
1627 (memq major-mode ibuffer-help-buffer-modes
)))))
1630 (defun ibuffer-mark-compressed-file-buffers ()
1631 "Mark buffers whose associated file is compressed."
1633 (ibuffer-mark-on-buffer
1635 (with-current-buffer buf
1636 (and buffer-file-name
1637 (string-match ibuffer-compressed-file-name-regexp
1638 buffer-file-name
))))))
1641 (defun ibuffer-mark-old-buffers ()
1642 "Mark buffers which have not been viewed in `ibuffer-old-time' hours."
1644 (ibuffer-mark-on-buffer
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
))))))))
1654 (defun ibuffer-mark-special-buffers ()
1655 "Mark all buffers whose name begins and ends with `*'."
1657 (ibuffer-mark-on-buffer
1658 #'(lambda (buf) (string-match "^\\*.+\\*$"
1659 (buffer-name buf
)))))
1662 (defun ibuffer-mark-read-only-buffers ()
1663 "Mark all read-only buffers."
1665 (ibuffer-mark-on-buffer
1666 #'(lambda (buf) (buffer-local-value 'buffer-read-only buf
))))
1669 (defun ibuffer-mark-dired-buffers ()
1670 "Mark all `dired' buffers."
1672 (ibuffer-mark-on-buffer
1673 #'(lambda (buf) (eq (buffer-local-value 'major-mode buf
) 'dired-mode
))))
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
1680 (interactive (occur-read-primary-args))
1681 (if (or (not (integerp nlines
))
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
)))
1696 ;; generated-autoload-file: "ibuffer-loaddefs.el"
1699 ;;; ibuf-ext.el ends here