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 (let ((filters ibuffer-filtering-qualifiers
))
873 (when (< (length filters
) 2)
874 (error "Need two filters to exchange"))
875 (cl-rotatef (car filters
) (cadr filters
))
876 (ibuffer-update nil t
)))
879 (defun ibuffer-negate-filter ()
880 "Negate the sense of the top filter in the current buffer."
882 (when (null ibuffer-filtering-qualifiers
)
883 (error "No filters in effect"))
884 (let ((lim (pop ibuffer-filtering-qualifiers
)))
885 (push (if (eq (car lim
) 'not
)
888 ibuffer-filtering-qualifiers
))
889 (ibuffer-update nil t
))
892 (defun ibuffer-or-filter (&optional reverse
)
893 "Replace the top two filters in this buffer with their logical OR.
894 If optional argument REVERSE is non-nil, instead break the top OR
899 (when (or (null ibuffer-filtering-qualifiers
)
900 (not (eq 'or
(caar ibuffer-filtering-qualifiers
))))
901 (error "Top filter is not an OR"))
902 (let ((lim (pop ibuffer-filtering-qualifiers
)))
903 (setq ibuffer-filtering-qualifiers
904 (nconc (cdr lim
) ibuffer-filtering-qualifiers
))))
905 (when (< (length ibuffer-filtering-qualifiers
) 2)
906 (error "Need two filters to OR"))
907 ;; If the second filter is an OR, just add to it.
908 (let ((first (pop ibuffer-filtering-qualifiers
))
909 (second (pop ibuffer-filtering-qualifiers
)))
910 (if (eq 'or
(car second
))
911 (push (nconc (list 'or first
) (cdr second
))
912 ibuffer-filtering-qualifiers
)
913 (push (list 'or first second
)
914 ibuffer-filtering-qualifiers
))))
915 (ibuffer-update nil t
))
917 (defun ibuffer-maybe-save-stuff ()
918 (when ibuffer-save-with-custom
919 (if (fboundp 'customize-save-variable
)
921 (customize-save-variable 'ibuffer-saved-filters
922 ibuffer-saved-filters
)
923 (customize-save-variable 'ibuffer-saved-filter-groups
924 ibuffer-saved-filter-groups
))
925 (message "Not saved permanently: Customize not available"))))
928 (defun ibuffer-save-filters (name filters
)
929 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'.
930 Interactively, prompt for NAME, and use the current filters."
932 (if (null ibuffer-filtering-qualifiers
)
933 (error "No filters currently in effect")
935 (read-from-minibuffer "Save current filters as: ")
936 ibuffer-filtering-qualifiers
)))
937 (ibuffer-aif (assoc name ibuffer-saved-filters
)
939 (push (list name filters
) ibuffer-saved-filters
))
940 (ibuffer-maybe-save-stuff))
943 (defun ibuffer-delete-saved-filters (name)
944 "Delete saved filters with NAME from `ibuffer-saved-filters'."
947 (if (null ibuffer-saved-filters
)
948 (error "No saved filters")
949 (completing-read "Delete saved filters: "
950 ibuffer-saved-filters nil t
))))
951 (setq ibuffer-saved-filters
952 (ibuffer-delete-alist name ibuffer-saved-filters
))
953 (ibuffer-maybe-save-stuff)
954 (ibuffer-update nil t
))
957 (defun ibuffer-add-saved-filters (name)
958 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
961 (if (null ibuffer-saved-filters
)
962 (error "No saved filters")
963 (completing-read "Add saved filters: "
964 ibuffer-saved-filters nil t
))))
965 (push (cons 'saved name
) ibuffer-filtering-qualifiers
)
966 (ibuffer-update nil t
))
969 (defun ibuffer-switch-to-saved-filters (name)
970 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'."
973 (if (null ibuffer-saved-filters
)
974 (error "No saved filters")
975 (completing-read "Switch to saved filters: "
976 ibuffer-saved-filters nil t
))))
977 (setq ibuffer-filtering-qualifiers
(list (cons 'saved name
)))
978 (ibuffer-update nil t
))
980 (defun ibuffer-format-filter-group-data (filter)
981 (if (equal filter
"Default")
983 (concat "Filter:" (mapconcat #'ibuffer-format-qualifier
984 (cdr (assq filter ibuffer-filter-groups
))
987 (defun ibuffer-format-qualifier (qualifier)
988 (if (eq (car-safe qualifier
) 'not
)
989 (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier
)) "]")
990 (ibuffer-format-qualifier-1 qualifier
)))
992 (defun ibuffer-format-qualifier-1 (qualifier)
993 (pcase (car qualifier
)
995 (concat " [filter: " (cdr qualifier
) "]"))
997 (concat " [OR" (mapconcat #'ibuffer-format-qualifier
998 (cdr qualifier
) "") "]"))
1000 (let ((type (assq (car qualifier
) ibuffer-filtering-alist
)))
1002 (error "Ibuffer: bad qualifier %s" qualifier
))
1003 (concat " [" (cadr type
) ": " (format "%s]" (cdr qualifier
)))))))
1006 (defun ibuffer-list-buffer-modes (&optional include-parents
)
1007 "Create a completion table of buffer modes currently in use.
1008 If INCLUDE-PARENTS is non-nil then include parent modes."
1010 (dolist (buf (buffer-list))
1011 (let ((this-mode (buffer-local-value 'major-mode buf
)))
1012 (while (and this-mode
(not (memq this-mode modes
)))
1013 (push this-mode modes
)
1014 (setq this-mode
(and include-parents
1015 (get this-mode
'derived-mode-parent
))))))
1016 (mapcar #'symbol-name modes
)))
1019 ;;; Extra operation definitions
1021 ;;;###autoload (autoload 'ibuffer-filter-by-mode "ibuf-ext")
1022 (define-ibuffer-filter mode
1023 "Toggle current view to buffers with major mode QUALIFIER."
1024 (:description
"major mode"
1026 (let* ((buf (ibuffer-current-buffer))
1027 (default (if (and buf
(buffer-live-p buf
))
1028 (symbol-name (buffer-local-value
1029 'major-mode buf
)))))
1033 (format "Filter by major mode (default %s): " default
)
1034 "Filter by major mode: ")
1037 (string-match "-mode\\'" (symbol-name e
)))
1038 t nil nil default
))))
1039 (eq qualifier
(buffer-local-value 'major-mode buf
)))
1041 ;;;###autoload (autoload 'ibuffer-filter-by-used-mode "ibuf-ext")
1042 (define-ibuffer-filter used-mode
1043 "Toggle current view to buffers with major mode QUALIFIER.
1044 Called interactively, this function allows selection of modes
1045 currently used by buffers."
1046 (:description
"major mode in use"
1048 (let* ((buf (ibuffer-current-buffer))
1049 (default (if (and buf
(buffer-live-p buf
))
1050 (symbol-name (buffer-local-value
1051 'major-mode buf
)))))
1055 (format "Filter by major mode (default %s): " default
)
1056 "Filter by major mode: ")
1057 (ibuffer-list-buffer-modes) nil t nil nil default
))))
1058 (eq qualifier
(buffer-local-value 'major-mode buf
)))
1060 ;;;###autoload (autoload 'ibuffer-filter-by-derived-mode "ibuf-ext")
1061 (define-ibuffer-filter derived-mode
1062 "Toggle current view to buffers whose major mode inherits from QUALIFIER."
1063 (:description
"derived mode"
1066 (completing-read "Filter by derived mode: "
1067 (ibuffer-list-buffer-modes t
)
1069 (with-current-buffer buf
(derived-mode-p qualifier
)))
1071 ;;;###autoload (autoload 'ibuffer-filter-by-name "ibuf-ext")
1072 (define-ibuffer-filter name
1073 "Toggle current view to buffers with name matching QUALIFIER."
1074 (:description
"buffer name"
1075 :reader
(read-from-minibuffer "Filter by name (regexp): "))
1076 (string-match qualifier
(buffer-name buf
)))
1078 ;;;###autoload (autoload 'ibuffer-filter-by-filename "ibuf-ext")
1079 (define-ibuffer-filter filename
1080 "Toggle current view to buffers with filename matching QUALIFIER."
1081 (:description
"filename"
1082 :reader
(read-from-minibuffer "Filter by filename (regexp): "))
1083 (ibuffer-awhen (with-current-buffer buf
(ibuffer-buffer-file-name))
1084 (string-match qualifier it
)))
1086 ;;;###autoload (autoload 'ibuffer-filter-by-size-gt "ibuf-ext")
1087 (define-ibuffer-filter size-gt
1088 "Toggle current view to buffers with size greater than QUALIFIER."
1089 (:description
"size greater than"
1091 (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
1092 (> (with-current-buffer buf
(buffer-size))
1095 ;;;###autoload (autoload 'ibuffer-filter-by-size-lt "ibuf-ext")
1096 (define-ibuffer-filter size-lt
1097 "Toggle current view to buffers with size less than QUALIFIER."
1098 (:description
"size less than"
1100 (string-to-number (read-from-minibuffer "Filter by size less than: ")))
1101 (< (with-current-buffer buf
(buffer-size))
1104 ;;;###autoload (autoload 'ibuffer-filter-by-content "ibuf-ext")
1105 (define-ibuffer-filter content
1106 "Toggle current view to buffers whose contents match QUALIFIER."
1107 (:description
"content"
1108 :reader
(read-from-minibuffer "Filter by content (regexp): "))
1109 (with-current-buffer buf
1111 (goto-char (point-min))
1112 (re-search-forward qualifier nil t
))))
1114 ;;;###autoload (autoload 'ibuffer-filter-by-predicate "ibuf-ext")
1115 (define-ibuffer-filter predicate
1116 "Toggle current view to buffers for which QUALIFIER returns non-nil."
1117 (:description
"predicate"
1118 :reader
(read-minibuffer "Filter by predicate (form): "))
1119 (with-current-buffer buf
1125 (defun ibuffer-toggle-sorting-mode ()
1126 "Toggle the current sorting mode.
1127 Default sorting modes are:
1128 Recency - the last time the buffer was viewed
1129 Name - the name of the buffer
1130 Major Mode - the name of the major mode of the buffer
1131 Size - the size of the buffer"
1133 (let ((modes (mapcar #'car ibuffer-sorting-functions-alist
)))
1134 (cl-pushnew 'recency modes
)
1135 (setq modes
(sort modes
#'string-lessp
))
1136 (let ((next (or (car-safe (cdr-safe (memq ibuffer-sorting-mode modes
)))
1138 (setq ibuffer-sorting-mode next
)
1139 (message "Sorting by %s" next
)))
1140 (ibuffer-redisplay t
))
1143 (defun ibuffer-invert-sorting ()
1144 "Toggle whether or not sorting is in reverse order."
1146 (setq ibuffer-sorting-reversep
(not ibuffer-sorting-reversep
))
1147 (message "Sorting order %s"
1148 (if ibuffer-sorting-reversep
1151 (ibuffer-redisplay t
))
1153 ;;;###autoload (autoload 'ibuffer-do-sort-by-major-mode "ibuf-ext")
1154 (define-ibuffer-sorter major-mode
1155 "Sort the buffers by major modes.
1156 Ordering is lexicographic."
1157 (:description
"major mode")
1158 (string-lessp (downcase
1159 (symbol-name (buffer-local-value 'major-mode
(car a
))))
1161 (symbol-name (buffer-local-value 'major-mode
(car b
))))))
1163 ;;;###autoload (autoload 'ibuffer-do-sort-by-mode-name "ibuf-ext")
1164 (define-ibuffer-sorter mode-name
1165 "Sort the buffers by their mode name.
1166 Ordering is lexicographic."
1167 (:description
"major mode name")
1168 (string-lessp (downcase
1169 (with-current-buffer
1171 (format-mode-line mode-name
)))
1173 (with-current-buffer
1175 (format-mode-line mode-name
)))))
1177 ;;;###autoload (autoload 'ibuffer-do-sort-by-alphabetic "ibuf-ext")
1178 (define-ibuffer-sorter alphabetic
1179 "Sort the buffers by their names.
1180 Ordering is lexicographic."
1181 (:description
"buffer name")
1183 (buffer-name (car a
))
1184 (buffer-name (car b
))))
1186 ;;;###autoload (autoload 'ibuffer-do-sort-by-size "ibuf-ext")
1187 (define-ibuffer-sorter size
1188 "Sort the buffers by their size."
1189 (:description
"size")
1190 (< (with-current-buffer (car a
)
1192 (with-current-buffer (car b
)
1195 ;;;###autoload (autoload 'ibuffer-do-sort-by-filename/process "ibuf-ext")
1196 (define-ibuffer-sorter filename
/process
1197 "Sort the buffers by their file name/process name."
1198 (:description
"file name")
1200 ;; FIXME: For now just compare the file name and the process name
1201 ;; (if it exists). Is there a better way to do this?
1202 (or (buffer-file-name (car a
))
1203 (let ((pr-a (get-buffer-process (car a
))))
1204 (and (processp pr-a
) (process-name pr-a
))))
1205 (or (buffer-file-name (car b
))
1206 (let ((pr-b (get-buffer-process (car b
))))
1207 (and (processp pr-b
) (process-name pr-b
))))))
1209 ;;; Functions to emulate bs.el
1212 (defun ibuffer-bs-show ()
1213 "Emulate `bs-show' from the bs.el package."
1215 (ibuffer t
"*Ibuffer-bs*" '((filename .
".*")) nil t
)
1216 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all
))
1218 (defun ibuffer-bs-toggle-all ()
1219 "Emulate `bs-toggle-show-all' from the bs.el package."
1221 (if ibuffer-filtering-qualifiers
1222 (ibuffer-pop-filter)
1223 (progn (ibuffer-push-filter '(filename .
".*"))
1224 (ibuffer-update nil t
))))
1229 (defun ibuffer-add-to-tmp-hide (regexp)
1230 "Add REGEXP to `ibuffer-tmp-hide-regexps'.
1231 This means that buffers whose name matches REGEXP will not be shown
1232 for this Ibuffer session."
1235 (read-from-minibuffer "Never show buffers matching: "
1236 (regexp-quote (buffer-name (ibuffer-current-buffer t
))))))
1237 (push regexp ibuffer-tmp-hide-regexps
))
1240 (defun ibuffer-add-to-tmp-show (regexp)
1241 "Add REGEXP to `ibuffer-tmp-show-regexps'.
1242 This means that buffers whose name matches REGEXP will always be shown
1243 for this Ibuffer session."
1246 (read-from-minibuffer "Always show buffers matching: "
1247 (regexp-quote (buffer-name (ibuffer-current-buffer t
))))))
1248 (push regexp ibuffer-tmp-show-regexps
))
1251 (defun ibuffer-forward-next-marked (&optional count mark direction
)
1252 "Move forward by COUNT marked buffers (default 1).
1254 If MARK is non-nil, it should be a character denoting the type of mark
1255 to move by. The default is `ibuffer-marked-char'.
1257 If DIRECTION is non-nil, it should be an integer; negative integers
1258 mean move backwards, non-negative integers mean move forwards."
1263 (setq mark ibuffer-marked-char
))
1267 (ibuffer-forward-line 0)
1268 (let ((opos (point)))
1269 (ibuffer-forward-line direction
)
1270 (while (not (or (= (point) opos
)
1271 (eq (ibuffer-current-mark) mark
)))
1272 (ibuffer-forward-line direction
))
1273 (when (and (= (point) opos
)
1274 (not (eq (ibuffer-current-mark) mark
)))
1275 (error "No buffers with mark %c" mark
))))
1278 (defun ibuffer-backwards-next-marked (&optional count mark
)
1279 "Move backwards by COUNT marked buffers (default 1).
1281 If MARK is non-nil, it should be a character denoting the type of mark
1282 to move by. The default is `ibuffer-marked-char'."
1284 (ibuffer-forward-next-marked count mark -
1))
1287 (defun ibuffer-do-kill-lines ()
1288 "Hide all of the currently marked lines."
1290 (if (= (ibuffer-count-marked-lines) 0)
1291 (message "No buffers marked; use 'm' to mark a buffer")
1293 (ibuffer-map-marked-lines
1294 #'(lambda (_buf _mark
)
1296 (message "Killed %s lines" count
))))
1299 (defun ibuffer-jump-to-buffer (name)
1300 "Move point to the buffer whose name is NAME.
1302 If called interactively, prompt for a buffer name and go to the
1303 corresponding line in the Ibuffer buffer. If said buffer is in a
1304 hidden group filter, open it.
1306 If `ibuffer-jump-offer-only-visible-buffers' is non-nil, only offer
1307 visible buffers in the completion list. Calling the command with
1308 a prefix argument reverses the meaning of that variable."
1310 (let ((only-visible ibuffer-jump-offer-only-visible-buffers
))
1311 (when current-prefix-arg
1312 (setq only-visible
(not only-visible
)))
1314 (let ((table (mapcar #'(lambda (x)
1315 (buffer-name (car x
)))
1316 (ibuffer-current-state-list))))
1318 (error "No buffers!"))
1319 (completing-read "Jump to buffer: "
1321 (read-buffer "Jump to buffer: " nil t
)))))
1322 (when (not (string= "" name
))
1324 ;; Blindly search for our buffer: it is very likely that it is
1325 ;; not in a hidden filter group.
1326 (ibuffer-map-lines #'(lambda (buf _marks
)
1327 (when (string= (buffer-name buf
) name
)
1328 (setq buf-point
(point))
1333 (not (null ibuffer-hidden-filter-groups
)))
1334 ;; We did not find our buffer. It must be in a hidden filter
1335 ;; group, so go through all hidden filter groups to find it.
1337 (dolist (group ibuffer-hidden-filter-groups
)
1338 (ibuffer-jump-to-filter-group group
)
1339 (ibuffer-toggle-filter-group)
1340 (ibuffer-map-lines #'(lambda (buf _marks
)
1341 (when (string= (buffer-name buf
) name
)
1342 (setq buf-point
(point))
1347 (ibuffer-toggle-filter-group)))))
1348 (if (null buf-point
)
1349 ;; Still not found even though we expanded all hidden filter
1350 ;; groups: that must be because it's hidden by predicate:
1351 ;; we won't bother trying to display it.
1352 (error "No buffer with name %s" name
)
1353 (goto-char buf-point
)))))
1355 (declare-function diff-sentinel
"diff"
1356 (code &optional old-temp-file new-temp-file
))
1358 (defun ibuffer-diff-buffer-with-file-1 (buffer)
1359 (let ((bufferfile (buffer-local-value 'buffer-file-name buffer
))
1360 (tempfile (make-temp-file "buffer-content-")))
1364 (with-current-buffer buffer
1365 (write-region nil nil tempfile nil
'nomessage
))
1366 (let* ((old (expand-file-name bufferfile
))
1367 (new (expand-file-name tempfile
))
1368 (oldtmp (file-local-copy old
))
1369 (newtmp (file-local-copy new
))
1370 (switches diff-switches
)
1375 ;; Use explicitly specified switches
1376 ,@(if (listp switches
) switches
(list switches
))
1378 (list "-L" (shell-quote-argument old
)
1379 "-L" (shell-quote-argument
1380 (format "Buffer %s" (buffer-name buffer
)))))
1381 ,(shell-quote-argument (or oldtmp old
))
1382 ,(shell-quote-argument (or newtmp new
)))
1384 (let ((inhibit-read-only t
))
1385 (insert command
"\n")
1387 (call-process shell-file-name nil
1388 (current-buffer) nil
1389 shell-command-switch command
))
1392 (when (file-exists-p tempfile
)
1393 (delete-file tempfile
)))))
1396 (defun ibuffer-diff-with-file ()
1397 "View the differences between marked buffers and their associated files.
1398 If no buffers are marked, use buffer at point.
1399 This requires the external program \"diff\" to be in your `exec-path'."
1402 (let ((marked-bufs (ibuffer-get-marked-buffers)))
1403 (when (null marked-bufs
)
1404 (setq marked-bufs
(list (ibuffer-current-buffer t
))))
1405 (with-current-buffer (get-buffer-create "*Ibuffer Diff*")
1406 (setq buffer-read-only nil
)
1407 (buffer-disable-undo (current-buffer))
1409 (buffer-enable-undo (current-buffer))
1411 (dolist (buf marked-bufs
)
1412 (unless (buffer-live-p buf
)
1413 (error "Buffer %s has been killed" buf
))
1414 (ibuffer-diff-buffer-with-file-1 buf
))
1415 (setq buffer-read-only t
)))
1416 (switch-to-buffer "*Ibuffer Diff*"))
1419 (defun ibuffer-copy-filename-as-kill (&optional arg
)
1420 "Copy filenames of marked (or next ARG) buffers into the kill ring.
1422 The names are separated by a space.
1423 If a buffer has no filename, it is ignored.
1425 With no prefix arg, use the filename sans its directory of each marked file.
1426 With a zero prefix arg, use the complete filename of each marked file.
1427 With \\[universal-argument], use the filename of each marked file relative
1428 to `ibuffer-default-directory' if non-nil, otherwise `default-directory'.
1430 You can then feed the file name(s) to other commands with \\[yank]."
1432 (let* ((buffers (cond ((and (integerp arg
) (not (zerop arg
)))
1433 (ibuffer--near-buffers arg
))
1435 (or (ibuffer-get-marked-buffers)
1436 (list (ibuffer-current-buffer))))))
1440 (let ((name (with-current-buffer buf
1441 (ibuffer-buffer-file-name))))
1444 (cond ((and (integerp arg
) (zerop arg
)) name
)
1447 name
(or ibuffer-default-directory
1448 default-directory
)))
1449 (t (file-name-nondirectory name
))))))
1452 (mapconcat 'identity
(delete "" file-names
) " ")))
1453 (unless (string= string
"")
1454 (if (eq last-command
'kill-region
)
1455 (kill-append string nil
)
1457 (message "%s" string
))))
1460 (defun ibuffer-copy-buffername-as-kill (&optional arg
)
1461 "Copy buffer names of marked (or next ARG) buffers into the kill ring.
1462 The names are separated by a space.
1463 You can then feed the file name(s) to other commands with \\[yank]."
1465 (let* ((buffers (cond ((and (integerp arg
) (not (zerop arg
)))
1466 (ibuffer--near-buffers arg
))
1468 (or (ibuffer-get-marked-buffers)
1469 (list (ibuffer-current-buffer))))))
1470 (string (mapconcat #'buffer-name buffers
" ")))
1471 (unless (string= string
"")
1472 (if (eq last-command
'kill-region
)
1473 (kill-append string nil
)
1475 (message "%s" string
))))
1477 (defun ibuffer-mark-on-buffer (func &optional ibuffer-mark-on-buffer-mark group
)
1480 #'(lambda (buf _mark
)
1481 (when (funcall func buf
)
1482 (ibuffer-set-mark-1 (or ibuffer-mark-on-buffer-mark
1483 ibuffer-marked-char
))
1487 (ibuffer-redisplay t
)
1488 (unless (eq ibuffer-mark-on-buffer-mark ?\s
)
1489 (message "Marked %s buffers" count
))))
1492 (defun ibuffer-mark-by-name-regexp (regexp)
1493 "Mark all buffers whose name matches REGEXP."
1494 (interactive "sMark by name (regexp): ")
1495 (ibuffer-mark-on-buffer
1497 (string-match regexp
(buffer-name buf
)))))
1499 (defun ibuffer-locked-buffer-p (&optional buf
)
1500 "Return non-nil if BUF is locked.
1501 When BUF nil, default to the buffer at current line."
1502 (let ((cbuffer (or buf
(ibuffer-current-buffer))))
1504 (with-current-buffer cbuffer
1505 (and (boundp 'emacs-lock-mode
) emacs-lock-mode
)))))
1508 (defun ibuffer-mark-by-locked ()
1509 "Mark all locked buffers."
1511 (when (featurep 'emacs-lock
)
1512 (ibuffer-mark-on-buffer
1514 (ibuffer-locked-buffer-p buf
)))))
1517 (defun ibuffer-mark-by-mode-regexp (regexp)
1518 "Mark all buffers whose major mode matches REGEXP."
1519 (interactive "sMark by major mode (regexp): ")
1520 (ibuffer-mark-on-buffer
1522 (with-current-buffer buf
1523 (string-match regexp
(format-mode-line mode-name nil nil buf
))))))
1526 (defun ibuffer-mark-by-file-name-regexp (regexp)
1527 "Mark all buffers whose file name matches REGEXP."
1528 (interactive "sMark by file name (regexp): ")
1529 (ibuffer-mark-on-buffer
1531 (let ((name (or (buffer-file-name buf
)
1532 (with-current-buffer buf
1534 (boundp 'dired-directory
)
1535 (stringp dired-directory
)
1536 dired-directory
)))))
1538 (string-match regexp name
))))))
1541 (defun ibuffer-mark-by-content-regexp (regexp &optional all-buffers
)
1542 "Mark all buffers whose content matches REGEXP.
1543 Optional arg ALL-BUFFERS, if non-nil, then search in all buffers.
1544 Otherwise buffers whose name matches an element of
1545 `ibuffer-never-search-content-name' or whose major mode is on
1546 `ibuffer-never-search-content-mode' are excluded."
1547 (interactive (let ((reg (read-string "Mark by content (regexp): ")))
1548 (list reg current-prefix-arg
)))
1549 (ibuffer-mark-on-buffer
1551 (let ((mode (with-current-buffer buf major-mode
))
1553 (cond ((and (not all-buffers
)
1555 (memq mode ibuffer-never-search-content-mode
)
1556 (cl-some (lambda (x) (string-match x
(buffer-name buf
)))
1557 ibuffer-never-search-content-name
)))
1560 (with-current-buffer buf
1561 (save-mark-and-excursion
1562 (goto-char (point-min))
1563 (setq res
(re-search-forward regexp nil t
)))))) res
))))
1566 (defun ibuffer-mark-by-mode (mode)
1567 "Mark all buffers whose major mode equals MODE."
1569 (let* ((buf (ibuffer-current-buffer))
1570 (default (if (and buf
(buffer-live-p buf
))
1571 (symbol-name (buffer-local-value
1572 'major-mode buf
)))))
1576 (format "Mark by major mode (default %s): " default
)
1577 "Mark by major mode: ")
1578 (ibuffer-list-buffer-modes) nil t nil nil default
)))))
1579 (ibuffer-mark-on-buffer
1581 (eq (buffer-local-value 'major-mode buf
) mode
))))
1584 (defun ibuffer-mark-modified-buffers ()
1585 "Mark all modified buffers."
1587 (ibuffer-mark-on-buffer
1588 #'(lambda (buf) (buffer-modified-p buf
))))
1591 (defun ibuffer-mark-unsaved-buffers ()
1592 "Mark all modified buffers that have an associated file."
1594 (ibuffer-mark-on-buffer
1595 #'(lambda (buf) (and (buffer-local-value 'buffer-file-name buf
)
1596 (buffer-modified-p buf
)))))
1599 (defun ibuffer-mark-dissociated-buffers ()
1600 "Mark all buffers whose associated file does not exist."
1602 (ibuffer-mark-on-buffer
1604 (with-current-buffer buf
1606 (and buffer-file-name
1607 (not (file-exists-p buffer-file-name
)))
1608 (and (eq major-mode
'dired-mode
)
1609 (boundp 'dired-directory
)
1610 (stringp dired-directory
)
1611 (not (file-exists-p (file-name-directory dired-directory
)))))))))
1614 (defun ibuffer-mark-help-buffers ()
1615 "Mark buffers whose major mode is in variable `ibuffer-help-buffer-modes'."
1617 (ibuffer-mark-on-buffer
1619 (with-current-buffer buf
1620 (memq major-mode ibuffer-help-buffer-modes
)))))
1623 (defun ibuffer-mark-compressed-file-buffers ()
1624 "Mark buffers whose associated file is compressed."
1626 (ibuffer-mark-on-buffer
1628 (with-current-buffer buf
1629 (and buffer-file-name
1630 (string-match ibuffer-compressed-file-name-regexp
1631 buffer-file-name
))))))
1634 (defun ibuffer-mark-old-buffers ()
1635 "Mark buffers which have not been viewed in `ibuffer-old-time' hours."
1637 (ibuffer-mark-on-buffer
1639 (with-current-buffer buf
1640 ;; hacked from midnight.el
1641 (when buffer-display-time
1642 (let* ((now (float-time))
1643 (then (float-time buffer-display-time
)))
1644 (> (- now then
) (* 60 60 ibuffer-old-time
))))))))
1647 (defun ibuffer-mark-special-buffers ()
1648 "Mark all buffers whose name begins and ends with `*'."
1650 (ibuffer-mark-on-buffer
1651 #'(lambda (buf) (string-match "^\\*.+\\*$"
1652 (buffer-name buf
)))))
1655 (defun ibuffer-mark-read-only-buffers ()
1656 "Mark all read-only buffers."
1658 (ibuffer-mark-on-buffer
1659 #'(lambda (buf) (buffer-local-value 'buffer-read-only buf
))))
1662 (defun ibuffer-mark-dired-buffers ()
1663 "Mark all `dired' buffers."
1665 (ibuffer-mark-on-buffer
1666 #'(lambda (buf) (eq (buffer-local-value 'major-mode buf
) 'dired-mode
))))
1669 (defun ibuffer-do-occur (regexp &optional nlines
)
1670 "View lines which match REGEXP in all marked buffers.
1671 Optional argument NLINES says how many lines of context to display: it
1673 (interactive (occur-read-primary-args))
1674 (if (or (not (integerp nlines
))
1677 (when (zerop (ibuffer-count-marked-lines))
1678 (ibuffer-set-mark ibuffer-marked-char
))
1679 (let ((ibuffer-do-occur-bufs nil
))
1680 ;; Accumulate a list of marked buffers
1681 (ibuffer-map-marked-lines
1682 #'(lambda (buf _mark
)
1683 (push buf ibuffer-do-occur-bufs
)))
1684 (occur-1 regexp nlines ibuffer-do-occur-bufs
)))
1689 ;; generated-autoload-file: "ibuffer-loaddefs.el"
1692 ;;; ibuf-ext.el ends here