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 (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed
) ; Initialize state vector
254 (add-hook 'post-command-hook
'ibuffer-auto-update-changed
))
256 (defun ibuffer-auto-update-changed ()
257 (when (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed
)
258 (dolist (buf (buffer-list))
260 (with-current-buffer buf
261 (when (and ibuffer-auto-mode
262 (derived-mode-p 'ibuffer-mode
))
263 (ibuffer-update nil t
)))))))
266 (defun ibuffer-mouse-filter-by-mode (event)
267 "Enable or disable filtering by the major mode chosen via mouse."
269 (ibuffer-interactive-filter-by-mode event
))
272 (defun ibuffer-interactive-filter-by-mode (event-or-point)
273 "Enable or disable filtering by the major mode at point."
275 (if (eventp event-or-point
)
276 (posn-set-point (event-end event-or-point
))
277 (goto-char event-or-point
))
278 (let ((buf (ibuffer-current-buffer)))
279 (if (assq 'mode ibuffer-filtering-qualifiers
)
280 (setq ibuffer-filtering-qualifiers
281 (ibuffer-delete-alist 'mode ibuffer-filtering-qualifiers
))
282 (ibuffer-push-filter (cons 'mode
(buffer-local-value 'major-mode buf
)))))
283 (ibuffer-update nil t
))
286 (defun ibuffer-mouse-toggle-filter-group (event)
287 "Toggle the display status of the filter group chosen with the mouse."
289 (ibuffer-toggle-filter-group-1 (save-excursion
290 (mouse-set-point event
)
294 (defun ibuffer-toggle-filter-group ()
295 "Toggle the display status of the filter group on this line."
297 (ibuffer-toggle-filter-group-1 (point)))
299 (defun ibuffer-toggle-filter-group-1 (posn)
300 (let ((name (get-text-property posn
'ibuffer-filter-group-name
)))
301 (unless (stringp name
)
302 (error "No filtering group name present"))
303 (if (member name ibuffer-hidden-filter-groups
)
304 (setq ibuffer-hidden-filter-groups
305 (delete name ibuffer-hidden-filter-groups
))
306 (push name ibuffer-hidden-filter-groups
))
307 (ibuffer-update nil t
)))
310 (defun ibuffer-forward-filter-group (&optional count
)
311 "Move point forwards by COUNT filtering groups."
316 (when (get-text-property (point) 'ibuffer-filter-group-name
)
317 (goto-char (next-single-property-change
318 (point) 'ibuffer-filter-group-name
320 (goto-char (next-single-property-change
321 (point) 'ibuffer-filter-group-name
323 (ibuffer-forward-filter-group (1- count
)))
324 (ibuffer-forward-line 0))
327 (defun ibuffer-backward-filter-group (&optional count
)
328 "Move point backwards by COUNT filtering groups."
333 (when (get-text-property (point) 'ibuffer-filter-group-name
)
334 (goto-char (previous-single-property-change
335 (point) 'ibuffer-filter-group-name
337 (goto-char (previous-single-property-change
338 (point) 'ibuffer-filter-group-name
340 (ibuffer-backward-filter-group (1- count
)))
341 (when (= (point) (point-min))
342 (goto-char (point-max))
343 (ibuffer-backward-filter-group 1))
344 (ibuffer-forward-line 0))
346 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe "ibuf-ext")
347 (define-ibuffer-op shell-command-pipe
(command)
348 "Pipe the contents of each marked buffer to shell command COMMAND."
349 (:interactive
"sPipe to shell command: "
350 :opstring
"Shell command executed on"
352 (shell-command-on-region
353 (point-min) (point-max) command
354 (get-buffer-create "* ibuffer-shell-output*")))
356 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe-replace "ibuf-ext")
357 (define-ibuffer-op shell-command-pipe-replace
(command)
358 "Replace the contents of marked buffers with output of pipe to COMMAND."
359 (:interactive
"sPipe to shell command (replace): "
360 :opstring
"Buffer contents replaced in"
361 :active-opstring
"replace buffer contents in"
364 (with-current-buffer buf
365 (shell-command-on-region (point-min) (point-max)
368 ;;;###autoload (autoload 'ibuffer-do-shell-command-file "ibuf-ext")
369 (define-ibuffer-op shell-command-file
(command)
370 "Run shell command COMMAND separately on files of marked buffers."
371 (:interactive
"sShell command on buffer's file: "
372 :opstring
"Shell command executed on"
374 (shell-command (concat command
" "
375 (shell-quote-argument
381 (min 10 (length (buffer-name)))))))
382 (write-region nil nil file nil
0)
385 ;;;###autoload (autoload 'ibuffer-do-eval "ibuf-ext")
386 (define-ibuffer-op eval
(form)
387 "Evaluate FORM in each of the buffers.
388 Does not display the buffer during evaluation. See
389 `ibuffer-do-view-and-eval' for that."
392 (read-from-minibuffer
393 "Eval in buffers (form): "
394 nil read-expression-map t
'read-expression-history
))
395 :opstring
"evaluated in"
399 ;;;###autoload (autoload 'ibuffer-do-view-and-eval "ibuf-ext")
400 (define-ibuffer-op view-and-eval
(form)
401 "Evaluate FORM while displaying each of the marked buffers.
402 To evaluate a form without viewing the buffer, see `ibuffer-do-eval'."
405 (read-from-minibuffer
406 "Eval viewing in buffers (form): "
407 nil read-expression-map t
'read-expression-history
))
408 :opstring
"evaluated in"
411 (let ((ibuffer-buf (current-buffer)))
414 (switch-to-buffer buf
)
416 (switch-to-buffer ibuffer-buf
))))
418 ;;;###autoload (autoload 'ibuffer-do-rename-uniquely "ibuf-ext")
419 (define-ibuffer-op rename-uniquely
()
420 "Rename marked buffers as with `rename-uniquely'."
425 ;;;###autoload (autoload 'ibuffer-do-revert "ibuf-ext")
426 (define-ibuffer-op revert
()
427 "Revert marked buffers as with `revert-buffer'."
430 :active-opstring
"revert"
434 ;;;###autoload (autoload 'ibuffer-do-isearch "ibuf-ext")
435 (define-ibuffer-op ibuffer-do-isearch
()
436 "Perform a `isearch-forward' in marked buffers."
438 :opstring
"searched in"
441 (multi-isearch-buffers (ibuffer-get-marked-buffers)))
443 ;;;###autoload (autoload 'ibuffer-do-isearch-regexp "ibuf-ext")
444 (define-ibuffer-op ibuffer-do-isearch-regexp
()
445 "Perform a `isearch-forward-regexp' in marked buffers."
447 :opstring
"searched regexp in"
450 (multi-isearch-buffers-regexp (ibuffer-get-marked-buffers)))
452 ;;;###autoload (autoload 'ibuffer-do-replace-regexp "ibuf-ext")
453 (define-ibuffer-op replace-regexp
(from-str to-str
)
454 "Perform a `replace-regexp' in marked buffers."
456 (let* ((from-str (read-from-minibuffer "Replace regexp: "))
457 (to-str (read-from-minibuffer (concat "Replace " from-str
459 (list from-str to-str
))
460 :opstring
"replaced in"
463 (save-window-excursion
464 (switch-to-buffer buf
)
466 (goto-char (point-min))
467 (let ((case-fold-search ibuffer-case-fold-search
))
468 (while (re-search-forward from-str nil t
)
469 (replace-match to-str
))))
472 ;;;###autoload (autoload 'ibuffer-do-query-replace "ibuf-ext")
473 (define-ibuffer-op query-replace
(&rest args
)
474 "Perform a `query-replace' in marked buffers."
476 (query-replace-read-args "Query replace" t t
)
477 :opstring
"replaced in"
480 (save-window-excursion
481 (switch-to-buffer buf
)
483 (let ((case-fold-search ibuffer-case-fold-search
))
484 (goto-char (point-min))
485 (apply #'query-replace args
)))
488 ;;;###autoload (autoload 'ibuffer-do-query-replace-regexp "ibuf-ext")
489 (define-ibuffer-op query-replace-regexp
(&rest args
)
490 "Perform a `query-replace-regexp' in marked buffers."
492 (query-replace-read-args "Query replace regexp" t t
)
493 :opstring
"replaced in"
496 (save-window-excursion
497 (switch-to-buffer buf
)
499 (let ((case-fold-search ibuffer-case-fold-search
))
500 (goto-char (point-min))
501 (apply #'query-replace-regexp args
)))
504 ;;;###autoload (autoload 'ibuffer-do-print "ibuf-ext")
505 (define-ibuffer-op print
()
506 "Print marked buffers as with `print-buffer'."
512 (defun ibuffer-included-in-filters-p (buf filters
)
514 (memq nil
;; a filter will return nil if it failed
516 ;; filter should be like (TYPE . QUALIFIER), or
517 ;; (or (TYPE . QUALIFIER) (TYPE . QUALIFIER) ...)
519 (ibuffer-included-in-filter-p buf qual
))
522 (defun ibuffer-included-in-filter-p (buf filter
)
523 (if (eq (car filter
) 'not
)
524 (not (ibuffer-included-in-filter-p-1 buf
(cdr filter
)))
525 (ibuffer-included-in-filter-p-1 buf filter
)))
527 (defun ibuffer-included-in-filter-p-1 (buf filter
)
532 (memq t
(mapcar #'(lambda (x)
533 (ibuffer-included-in-filter-p buf x
))
538 ibuffer-saved-filters
)))
540 (ibuffer-filter-disable t
)
541 (error "Unknown saved filter %s" (cdr filter
)))
542 (ibuffer-included-in-filters-p buf
(cadr data
))))
544 (pcase-let ((`(,_type
,_desc
,func
)
545 (assq (car filter
) ibuffer-filtering-alist
)))
547 (ibuffer-filter-disable t
)
548 (error "Undefined filter %s" (car filter
)))
549 (funcall func buf
(cdr filter
))))))))
551 (defun ibuffer-generate-filter-groups (bmarklist &optional noempty nodefault
)
552 (let ((filter-group-alist (if nodefault
553 ibuffer-filter-groups
554 (append ibuffer-filter-groups
555 (list (cons "Default" nil
))))))
556 ;; (dolist (hidden ibuffer-hidden-filter-groups)
557 ;; (setq filter-group-alist (ibuffer-delete-alist
558 ;; hidden filter-group-alist)))
559 (let ((vec (make-vector (length filter-group-alist
) nil
))
561 (dolist (filtergroup filter-group-alist
)
562 (let ((filterset (cdr filtergroup
)))
563 (cl-multiple-value-bind (hip-crowd lamers
)
565 (ibuffer-split-list (lambda (bufmark)
566 (ibuffer-included-in-filters-p (car bufmark
)
569 (aset vec i hip-crowd
)
571 (setq bmarklist lamers
))))
574 (let ((bufs (aref vec j
)))
575 (unless (and noempty
(null bufs
))
576 (push (cons (car (nth j filter-group-alist
))
582 (defun ibuffer-filters-to-filter-group (name)
583 "Make the current filters into a filtering group."
584 (interactive "sName for filtering group: ")
585 (when (null ibuffer-filtering-qualifiers
)
586 (error "No filters in effect"))
587 (push (cons name ibuffer-filtering-qualifiers
) ibuffer-filter-groups
)
588 (ibuffer-filter-disable))
591 (defun ibuffer-set-filter-groups-by-mode ()
592 "Set the current filter groups to filter by mode."
594 (setq ibuffer-filter-groups
595 (mapcar (lambda (mode)
596 (cons (format "%s" mode
) `((mode .
,mode
))))
598 (ibuffer-remove-duplicates
599 (mapcar (lambda (buf)
600 (buffer-local-value 'major-mode buf
))
602 (if ibuffer-view-ibuffer
604 (delq 'ibuffer-mode modes
)))))
605 (ibuffer-update nil t
))
608 (defun ibuffer-pop-filter-group ()
609 "Remove the first filter group."
611 (when (null ibuffer-filter-groups
)
612 (error "No filter groups active"))
613 (setq ibuffer-hidden-filter-groups
614 (delete (pop ibuffer-filter-groups
)
615 ibuffer-hidden-filter-groups
))
616 (ibuffer-update nil t
))
618 (defun ibuffer-read-filter-group-name (msg &optional nodefault noerror
)
619 (when (and (not noerror
) (null ibuffer-filter-groups
))
620 (error "No filter groups active"))
621 ;; `ibuffer-generate-filter-groups' returns all non-hidden filter
622 ;; groups, possibly excluding empty groups or Default.
623 ;; We add `ibuffer-hidden-filter-groups' to the list, excluding
624 ;; Default if necessary.
625 (completing-read msg
(nconc
626 (ibuffer-generate-filter-groups
627 (ibuffer-current-state-list)
628 (not ibuffer-show-empty-filter-groups
)
631 (remove "Default" ibuffer-hidden-filter-groups
)
632 ibuffer-hidden-filter-groups
))
636 (defun ibuffer-decompose-filter-group (group)
637 "Decompose the filter group GROUP into active filters."
639 (list (ibuffer-read-filter-group-name "Decompose filter group: " t
)))
640 (let ((data (cdr (assoc group ibuffer-filter-groups
))))
641 (setq ibuffer-filter-groups
(ibuffer-delete-alist
642 group ibuffer-filter-groups
)
643 ibuffer-filtering-qualifiers data
))
644 (ibuffer-update nil t
))
647 (defun ibuffer-clear-filter-groups ()
648 "Remove all filter groups."
650 (setq ibuffer-filter-groups nil
651 ibuffer-hidden-filter-groups nil
)
652 (ibuffer-update nil t
))
654 (defun ibuffer-current-filter-groups-with-position ()
656 (goto-char (point-min))
659 (while (and (not (eobp))
660 (setq pos
(next-single-property-change
661 (point) 'ibuffer-filter-group-name
)))
663 (push (cons (get-text-property (point) 'ibuffer-filter-group-name
)
666 (goto-char (next-single-property-change
667 pos
'ibuffer-filter-group-name
)))
671 (defun ibuffer-jump-to-filter-group (name)
672 "Move point to the filter group whose name is NAME."
674 (list (ibuffer-read-filter-group-name "Jump to filter group: ")))
675 (ibuffer-aif (assoc name
(ibuffer-current-filter-groups-with-position))
677 (error "No filter group with name %s" name
)))
680 (defun ibuffer-kill-filter-group (name)
681 "Kill the filter group named NAME.
682 The group will be added to `ibuffer-filter-group-kill-ring'."
683 (interactive (list (ibuffer-read-filter-group-name "Kill filter group: " t
)))
684 (when (equal name
"Default")
685 (error "Can't kill default filter group"))
686 (ibuffer-aif (assoc name ibuffer-filter-groups
)
688 (push (copy-tree it
) ibuffer-filter-group-kill-ring
)
689 (setq ibuffer-filter-groups
(ibuffer-delete-alist
690 name ibuffer-filter-groups
))
691 (setq ibuffer-hidden-filter-groups
692 (delete name ibuffer-hidden-filter-groups
)))
693 (error "No filter group with name \"%s\"" name
))
694 (ibuffer-update nil t
))
697 (defun ibuffer-kill-line (&optional arg interactive-p
)
698 "Kill the filter group at point.
699 See also `ibuffer-kill-filter-group'."
701 (ibuffer-aif (save-excursion
702 (ibuffer-forward-line 0)
703 (get-text-property (point) 'ibuffer-filter-group-name
))
705 (ibuffer-kill-filter-group it
))
706 (funcall (if interactive-p
#'call-interactively
#'funcall
)
709 (defun ibuffer-insert-filter-group-before (newgroup group
)
711 (pos (let ((groups (mapcar #'car ibuffer-filter-groups
))
714 (if (equal (car groups
) group
)
718 (setq groups
(cdr groups
))))
721 (setq ibuffer-filter-groups
722 (nconc ibuffer-filter-groups
(list newgroup
))))
724 (push newgroup ibuffer-filter-groups
))
726 (let ((cell (nthcdr pos ibuffer-filter-groups
)))
727 (setf (cdr cell
) (cons (car cell
) (cdr cell
)))
728 (setf (car cell
) newgroup
))))))
731 (defun ibuffer-yank ()
732 "Yank the last killed filter group before group at point."
734 (ibuffer-yank-filter-group
735 (or (get-text-property (point) 'ibuffer-filter-group-name
)
736 (get-text-property (point) 'ibuffer-filter-group
)
737 (error "No filter group at point"))))
740 (defun ibuffer-yank-filter-group (name)
741 "Yank the last killed filter group before group named NAME."
742 (interactive (list (ibuffer-read-filter-group-name
743 "Yank filter group before group: ")))
744 (unless ibuffer-filter-group-kill-ring
745 (error "The Ibuffer filter group kill-ring is empty"))
747 (ibuffer-forward-line 0)
748 (ibuffer-insert-filter-group-before (pop ibuffer-filter-group-kill-ring
)
750 (ibuffer-update nil t
))
753 (defun ibuffer-save-filter-groups (name groups
)
754 "Save all active filter groups GROUPS as NAME.
755 They are added to `ibuffer-saved-filter-groups'. Interactively,
756 prompt for NAME, and use the current filters."
758 (if (null ibuffer-filter-groups
)
759 (error "No filter groups active")
761 (read-from-minibuffer "Save current filter groups as: ")
762 ibuffer-filter-groups
)))
763 (ibuffer-aif (assoc name ibuffer-saved-filter-groups
)
765 (push (cons name groups
) ibuffer-saved-filter-groups
))
766 (ibuffer-maybe-save-stuff))
769 (defun ibuffer-delete-saved-filter-groups (name)
770 "Delete saved filter groups with NAME.
771 They are removed from `ibuffer-saved-filter-groups'."
774 (if (null ibuffer-saved-filter-groups
)
775 (error "No saved filter groups")
776 (completing-read "Delete saved filter group: "
777 ibuffer-saved-filter-groups nil t
))))
778 (setq ibuffer-saved-filter-groups
779 (ibuffer-delete-alist name ibuffer-saved-filter-groups
))
780 (ibuffer-maybe-save-stuff)
781 (ibuffer-update nil t
))
784 (defun ibuffer-switch-to-saved-filter-groups (name)
785 "Set this buffer's filter groups to saved version with NAME.
786 The value from `ibuffer-saved-filter-groups' is used."
789 (cond ((null ibuffer-saved-filter-groups
)
790 (error "No saved filters"))
791 ;; `ibuffer-saved-filter-groups' is a user variable that defaults
792 ;; to nil. We assume that with one element in this list the user
793 ;; knows what she wants. See bug#12331.
794 ((null (cdr ibuffer-saved-filter-groups
))
795 (caar ibuffer-saved-filter-groups
))
797 (completing-read "Switch to saved filter group: "
798 ibuffer-saved-filter-groups nil t
)))))
799 (setq ibuffer-filter-groups
(cdr (assoc name ibuffer-saved-filter-groups
))
800 ibuffer-hidden-filter-groups nil
)
801 (ibuffer-update nil t
))
804 (defun ibuffer-filter-disable (&optional delete-filter-groups
)
805 "Disable all filters currently in effect in this buffer.
806 With optional arg DELETE-FILTER-GROUPS non-nil, delete all filter
807 group definitions by setting `ibuffer-filter-groups' to nil."
809 (setq ibuffer-filtering-qualifiers nil
)
810 (if delete-filter-groups
811 (setq ibuffer-filter-groups nil
))
812 (let ((buf (ibuffer-current-buffer)))
813 (ibuffer-update nil t
)
815 (ibuffer-jump-to-buffer (buffer-name buf
)))))
818 (defun ibuffer-pop-filter ()
819 "Remove the top filter in this buffer."
821 (when (null ibuffer-filtering-qualifiers
)
822 (error "No filters in effect"))
823 (pop ibuffer-filtering-qualifiers
)
824 (let ((buf (ibuffer-current-buffer)))
825 (ibuffer-update nil t
)
827 (ibuffer-jump-to-buffer (buffer-name buf
)))))
829 (defun ibuffer-push-filter (qualifier)
830 "Add QUALIFIER to `ibuffer-filtering-qualifiers'."
831 (push qualifier ibuffer-filtering-qualifiers
))
834 (defun ibuffer-decompose-filter ()
835 "Separate the top compound filter (OR, NOT, or SAVED) in this buffer.
837 This means that the topmost filter on the filtering stack, which must
838 be a complex filter like (OR [name: foo] [mode: bar-mode]), will be
839 turned into two separate filters [name: foo] and [mode: bar-mode]."
841 (when (null ibuffer-filtering-qualifiers
)
842 (error "No filters in effect"))
843 (let ((lim (pop ibuffer-filtering-qualifiers
)))
846 (setq ibuffer-filtering-qualifiers
(append
848 ibuffer-filtering-qualifiers
)))
852 ibuffer-saved-filters
)))
854 (ibuffer-filter-disable)
855 (error "Unknown saved filter %s" (cdr lim
)))
856 (setq ibuffer-filtering-qualifiers
(append
858 ibuffer-filtering-qualifiers
))))
861 ibuffer-filtering-qualifiers
))
863 (error "Filter type %s is not compound" (car lim
)))))
864 (ibuffer-update nil t
))
867 (defun ibuffer-exchange-filters ()
868 "Exchange the top two filters on the stack in this buffer."
870 (when (< (length ibuffer-filtering-qualifiers
)
872 (error "Need two filters to exchange"))
873 (let ((first (pop ibuffer-filtering-qualifiers
))
874 (second (pop ibuffer-filtering-qualifiers
)))
875 (push first ibuffer-filtering-qualifiers
)
876 (push second ibuffer-filtering-qualifiers
))
877 (ibuffer-update nil t
))
880 (defun ibuffer-negate-filter ()
881 "Negate the sense of the top filter in the current buffer."
883 (when (null ibuffer-filtering-qualifiers
)
884 (error "No filters in effect"))
885 (let ((lim (pop ibuffer-filtering-qualifiers
)))
886 (push (if (eq (car lim
) 'not
)
889 ibuffer-filtering-qualifiers
))
890 (ibuffer-update nil t
))
893 (defun ibuffer-or-filter (&optional reverse
)
894 "Replace the top two filters in this buffer with their logical OR.
895 If optional argument REVERSE is non-nil, instead break the top OR
900 (when (or (null ibuffer-filtering-qualifiers
)
901 (not (eq 'or
(caar ibuffer-filtering-qualifiers
))))
902 (error "Top filter is not an OR"))
903 (let ((lim (pop ibuffer-filtering-qualifiers
)))
904 (setq ibuffer-filtering-qualifiers
905 (nconc (cdr lim
) ibuffer-filtering-qualifiers
))))
906 (when (< (length ibuffer-filtering-qualifiers
) 2)
907 (error "Need two filters to OR"))
908 ;; If the second filter is an OR, just add to it.
909 (let ((first (pop ibuffer-filtering-qualifiers
))
910 (second (pop ibuffer-filtering-qualifiers
)))
911 (if (eq 'or
(car second
))
912 (push (nconc (list 'or first
) (cdr second
))
913 ibuffer-filtering-qualifiers
)
914 (push (list 'or first second
)
915 ibuffer-filtering-qualifiers
))))
916 (ibuffer-update nil t
))
918 (defun ibuffer-maybe-save-stuff ()
919 (when ibuffer-save-with-custom
920 (if (fboundp 'customize-save-variable
)
922 (customize-save-variable 'ibuffer-saved-filters
923 ibuffer-saved-filters
)
924 (customize-save-variable 'ibuffer-saved-filter-groups
925 ibuffer-saved-filter-groups
))
926 (message "Not saved permanently: Customize not available"))))
929 (defun ibuffer-save-filters (name filters
)
930 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'.
931 Interactively, prompt for NAME, and use the current filters."
933 (if (null ibuffer-filtering-qualifiers
)
934 (error "No filters currently in effect")
936 (read-from-minibuffer "Save current filters as: ")
937 ibuffer-filtering-qualifiers
)))
938 (ibuffer-aif (assoc name ibuffer-saved-filters
)
940 (push (list name filters
) ibuffer-saved-filters
))
941 (ibuffer-maybe-save-stuff))
944 (defun ibuffer-delete-saved-filters (name)
945 "Delete saved filters with NAME from `ibuffer-saved-filters'."
948 (if (null ibuffer-saved-filters
)
949 (error "No saved filters")
950 (completing-read "Delete saved filters: "
951 ibuffer-saved-filters nil t
))))
952 (setq ibuffer-saved-filters
953 (ibuffer-delete-alist name ibuffer-saved-filters
))
954 (ibuffer-maybe-save-stuff)
955 (ibuffer-update nil t
))
958 (defun ibuffer-add-saved-filters (name)
959 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
962 (if (null ibuffer-saved-filters
)
963 (error "No saved filters")
964 (completing-read "Add saved filters: "
965 ibuffer-saved-filters nil t
))))
966 (push (cons 'saved name
) ibuffer-filtering-qualifiers
)
967 (ibuffer-update nil t
))
970 (defun ibuffer-switch-to-saved-filters (name)
971 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'."
974 (if (null ibuffer-saved-filters
)
975 (error "No saved filters")
976 (completing-read "Switch to saved filters: "
977 ibuffer-saved-filters nil t
))))
978 (setq ibuffer-filtering-qualifiers
(list (cons 'saved name
)))
979 (ibuffer-update nil t
))
981 (defun ibuffer-format-filter-group-data (filter)
982 (if (equal filter
"Default")
984 (concat "Filter:" (mapconcat #'ibuffer-format-qualifier
985 (cdr (assq filter ibuffer-filter-groups
))
988 (defun ibuffer-format-qualifier (qualifier)
989 (if (eq (car-safe qualifier
) 'not
)
990 (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier
)) "]")
991 (ibuffer-format-qualifier-1 qualifier
)))
993 (defun ibuffer-format-qualifier-1 (qualifier)
994 (pcase (car qualifier
)
996 (concat " [filter: " (cdr qualifier
) "]"))
998 (concat " [OR" (mapconcat #'ibuffer-format-qualifier
999 (cdr qualifier
) "") "]"))
1001 (let ((type (assq (car qualifier
) ibuffer-filtering-alist
)))
1003 (error "Ibuffer: bad qualifier %s" qualifier
))
1004 (concat " [" (cadr type
) ": " (format "%s]" (cdr qualifier
)))))))
1007 (defun ibuffer-list-buffer-modes (&optional include-parents
)
1008 "Create a completion table of buffer modes currently in use.
1009 If INCLUDE-PARENTS is non-nil then include parent modes."
1011 (dolist (buf (buffer-list))
1012 (let ((this-mode (buffer-local-value 'major-mode buf
)))
1013 (while (and this-mode
(not (memq this-mode modes
)))
1014 (push this-mode modes
)
1015 (setq this-mode
(and include-parents
1016 (get this-mode
'derived-mode-parent
))))))
1017 (mapcar #'symbol-name modes
)))
1020 ;;; Extra operation definitions
1022 ;;;###autoload (autoload 'ibuffer-filter-by-mode "ibuf-ext")
1023 (define-ibuffer-filter mode
1024 "Toggle current view to buffers with major mode QUALIFIER."
1025 (:description
"major mode"
1027 (let* ((buf (ibuffer-current-buffer))
1028 (default (if (and buf
(buffer-live-p buf
))
1029 (symbol-name (buffer-local-value
1030 'major-mode buf
)))))
1034 (format "Filter by major mode (default %s): " default
)
1035 "Filter by major mode: ")
1038 (string-match "-mode\\'" (symbol-name e
)))
1039 t nil nil default
))))
1040 (eq qualifier
(buffer-local-value 'major-mode buf
)))
1042 ;;;###autoload (autoload 'ibuffer-filter-by-used-mode "ibuf-ext")
1043 (define-ibuffer-filter used-mode
1044 "Toggle current view to buffers with major mode QUALIFIER.
1045 Called interactively, this function allows selection of modes
1046 currently used by buffers."
1047 (:description
"major mode in use"
1049 (let* ((buf (ibuffer-current-buffer))
1050 (default (if (and buf
(buffer-live-p buf
))
1051 (symbol-name (buffer-local-value
1052 'major-mode buf
)))))
1056 (format "Filter by major mode (default %s): " default
)
1057 "Filter by major mode: ")
1058 (ibuffer-list-buffer-modes) nil t nil nil default
))))
1059 (eq qualifier
(buffer-local-value 'major-mode buf
)))
1061 ;;;###autoload (autoload 'ibuffer-filter-by-derived-mode "ibuf-ext")
1062 (define-ibuffer-filter derived-mode
1063 "Toggle current view to buffers whose major mode inherits from QUALIFIER."
1064 (:description
"derived mode"
1067 (completing-read "Filter by derived mode: "
1068 (ibuffer-list-buffer-modes t
)
1070 (with-current-buffer buf
(derived-mode-p qualifier
)))
1072 ;;;###autoload (autoload 'ibuffer-filter-by-name "ibuf-ext")
1073 (define-ibuffer-filter name
1074 "Toggle current view to buffers with name matching QUALIFIER."
1075 (:description
"buffer name"
1076 :reader
(read-from-minibuffer "Filter by name (regexp): "))
1077 (string-match qualifier
(buffer-name buf
)))
1079 ;;;###autoload (autoload 'ibuffer-filter-by-filename "ibuf-ext")
1080 (define-ibuffer-filter filename
1081 "Toggle current view to buffers with filename matching QUALIFIER."
1082 (:description
"filename"
1083 :reader
(read-from-minibuffer "Filter by filename (regexp): "))
1084 (ibuffer-awhen (with-current-buffer buf
(ibuffer-buffer-file-name))
1085 (string-match qualifier it
)))
1087 ;;;###autoload (autoload 'ibuffer-filter-by-size-gt "ibuf-ext")
1088 (define-ibuffer-filter size-gt
1089 "Toggle current view to buffers with size greater than QUALIFIER."
1090 (:description
"size greater than"
1092 (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
1093 (> (with-current-buffer buf
(buffer-size))
1096 ;;;###autoload (autoload 'ibuffer-filter-by-size-lt "ibuf-ext")
1097 (define-ibuffer-filter size-lt
1098 "Toggle current view to buffers with size less than QUALIFIER."
1099 (:description
"size less than"
1101 (string-to-number (read-from-minibuffer "Filter by size less than: ")))
1102 (< (with-current-buffer buf
(buffer-size))
1105 ;;;###autoload (autoload 'ibuffer-filter-by-content "ibuf-ext")
1106 (define-ibuffer-filter content
1107 "Toggle current view to buffers whose contents match QUALIFIER."
1108 (:description
"content"
1109 :reader
(read-from-minibuffer "Filter by content (regexp): "))
1110 (with-current-buffer buf
1112 (goto-char (point-min))
1113 (re-search-forward qualifier nil t
))))
1115 ;;;###autoload (autoload 'ibuffer-filter-by-predicate "ibuf-ext")
1116 (define-ibuffer-filter predicate
1117 "Toggle current view to buffers for which QUALIFIER returns non-nil."
1118 (:description
"predicate"
1119 :reader
(read-minibuffer "Filter by predicate (form): "))
1120 (with-current-buffer buf
1126 (defun ibuffer-toggle-sorting-mode ()
1127 "Toggle the current sorting mode.
1128 Default sorting modes are:
1129 Recency - the last time the buffer was viewed
1130 Name - the name of the buffer
1131 Major Mode - the name of the major mode of the buffer
1132 Size - the size of the buffer"
1134 (let ((modes (mapcar #'car ibuffer-sorting-functions-alist
)))
1135 (cl-pushnew 'recency modes
)
1136 (setq modes
(sort modes
#'string-lessp
))
1137 (let ((next (or (car-safe (cdr-safe (memq ibuffer-sorting-mode modes
)))
1139 (setq ibuffer-sorting-mode next
)
1140 (message "Sorting by %s" next
)))
1141 (ibuffer-redisplay t
))
1144 (defun ibuffer-invert-sorting ()
1145 "Toggle whether or not sorting is in reverse order."
1147 (setq ibuffer-sorting-reversep
(not ibuffer-sorting-reversep
))
1148 (message "Sorting order %s"
1149 (if ibuffer-sorting-reversep
1152 (ibuffer-redisplay t
))
1154 ;;;###autoload (autoload 'ibuffer-do-sort-by-major-mode "ibuf-ext")
1155 (define-ibuffer-sorter major-mode
1156 "Sort the buffers by major modes.
1157 Ordering is lexicographic."
1158 (:description
"major mode")
1159 (string-lessp (downcase
1160 (symbol-name (buffer-local-value 'major-mode
(car a
))))
1162 (symbol-name (buffer-local-value 'major-mode
(car b
))))))
1164 ;;;###autoload (autoload 'ibuffer-do-sort-by-mode-name "ibuf-ext")
1165 (define-ibuffer-sorter mode-name
1166 "Sort the buffers by their mode name.
1167 Ordering is lexicographic."
1168 (:description
"major mode name")
1169 (string-lessp (downcase
1170 (with-current-buffer
1172 (format-mode-line mode-name
)))
1174 (with-current-buffer
1176 (format-mode-line mode-name
)))))
1178 ;;;###autoload (autoload 'ibuffer-do-sort-by-alphabetic "ibuf-ext")
1179 (define-ibuffer-sorter alphabetic
1180 "Sort the buffers by their names.
1181 Ordering is lexicographic."
1182 (:description
"buffer name")
1184 (buffer-name (car a
))
1185 (buffer-name (car b
))))
1187 ;;;###autoload (autoload 'ibuffer-do-sort-by-size "ibuf-ext")
1188 (define-ibuffer-sorter size
1189 "Sort the buffers by their size."
1190 (:description
"size")
1191 (< (with-current-buffer (car a
)
1193 (with-current-buffer (car b
)
1196 ;;;###autoload (autoload 'ibuffer-do-sort-by-filename/process "ibuf-ext")
1197 (define-ibuffer-sorter filename
/process
1198 "Sort the buffers by their file name/process name."
1199 (:description
"file name")
1201 ;; FIXME: For now just compare the file name and the process name
1202 ;; (if it exists). Is there a better way to do this?
1203 (or (buffer-file-name (car a
))
1204 (let ((pr-a (get-buffer-process (car a
))))
1205 (and (processp pr-a
) (process-name pr-a
))))
1206 (or (buffer-file-name (car b
))
1207 (let ((pr-b (get-buffer-process (car b
))))
1208 (and (processp pr-b
) (process-name pr-b
))))))
1210 ;;; Functions to emulate bs.el
1213 (defun ibuffer-bs-show ()
1214 "Emulate `bs-show' from the bs.el package."
1216 (ibuffer t
"*Ibuffer-bs*" '((filename .
".*")) nil t
)
1217 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all
))
1219 (defun ibuffer-bs-toggle-all ()
1220 "Emulate `bs-toggle-show-all' from the bs.el package."
1222 (if ibuffer-filtering-qualifiers
1223 (ibuffer-pop-filter)
1224 (progn (ibuffer-push-filter '(filename .
".*"))
1225 (ibuffer-update nil t
))))
1230 (defun ibuffer-add-to-tmp-hide (regexp)
1231 "Add REGEXP to `ibuffer-tmp-hide-regexps'.
1232 This means that buffers whose name matches REGEXP will not be shown
1233 for this Ibuffer session."
1236 (read-from-minibuffer "Never show buffers matching: "
1237 (regexp-quote (buffer-name (ibuffer-current-buffer t
))))))
1238 (push regexp ibuffer-tmp-hide-regexps
))
1241 (defun ibuffer-add-to-tmp-show (regexp)
1242 "Add REGEXP to `ibuffer-tmp-show-regexps'.
1243 This means that buffers whose name matches REGEXP will always be shown
1244 for this Ibuffer session."
1247 (read-from-minibuffer "Always show buffers matching: "
1248 (regexp-quote (buffer-name (ibuffer-current-buffer t
))))))
1249 (push regexp ibuffer-tmp-show-regexps
))
1252 (defun ibuffer-forward-next-marked (&optional count mark direction
)
1253 "Move forward by COUNT marked buffers (default 1).
1255 If MARK is non-nil, it should be a character denoting the type of mark
1256 to move by. The default is `ibuffer-marked-char'.
1258 If DIRECTION is non-nil, it should be an integer; negative integers
1259 mean move backwards, non-negative integers mean move forwards."
1264 (setq mark ibuffer-marked-char
))
1268 (ibuffer-forward-line 0)
1269 (let ((opos (point)))
1270 (ibuffer-forward-line direction
)
1271 (while (not (or (= (point) opos
)
1272 (eq (ibuffer-current-mark) mark
)))
1273 (ibuffer-forward-line direction
))
1274 (when (and (= (point) opos
)
1275 (not (eq (ibuffer-current-mark) mark
)))
1276 (error "No buffers with mark %c" mark
))))
1279 (defun ibuffer-backwards-next-marked (&optional count mark
)
1280 "Move backwards by COUNT marked buffers (default 1).
1282 If MARK is non-nil, it should be a character denoting the type of mark
1283 to move by. The default is `ibuffer-marked-char'."
1285 (ibuffer-forward-next-marked count mark -
1))
1288 (defun ibuffer-do-kill-lines ()
1289 "Hide all of the currently marked lines."
1291 (if (= (ibuffer-count-marked-lines) 0)
1292 (message "No buffers marked; use 'm' to mark a buffer")
1294 (ibuffer-map-marked-lines
1295 #'(lambda (_buf _mark
)
1297 (message "Killed %s lines" count
))))
1300 (defun ibuffer-jump-to-buffer (name)
1301 "Move point to the buffer whose name is NAME.
1303 If called interactively, prompt for a buffer name and go to the
1304 corresponding line in the Ibuffer buffer. If said buffer is in a
1305 hidden group filter, open it.
1307 If `ibuffer-jump-offer-only-visible-buffers' is non-nil, only offer
1308 visible buffers in the completion list. Calling the command with
1309 a prefix argument reverses the meaning of that variable."
1311 (let ((only-visible ibuffer-jump-offer-only-visible-buffers
))
1312 (when current-prefix-arg
1313 (setq only-visible
(not only-visible
)))
1315 (let ((table (mapcar #'(lambda (x)
1316 (buffer-name (car x
)))
1317 (ibuffer-current-state-list))))
1319 (error "No buffers!"))
1320 (completing-read "Jump to buffer: "
1322 (read-buffer "Jump to buffer: " nil t
)))))
1323 (when (not (string= "" name
))
1325 ;; Blindly search for our buffer: it is very likely that it is
1326 ;; not in a hidden filter group.
1327 (ibuffer-map-lines #'(lambda (buf _marks
)
1328 (when (string= (buffer-name buf
) name
)
1329 (setq buf-point
(point))
1334 (not (null ibuffer-hidden-filter-groups
)))
1335 ;; We did not find our buffer. It must be in a hidden filter
1336 ;; group, so go through all hidden filter groups to find it.
1338 (dolist (group ibuffer-hidden-filter-groups
)
1339 (ibuffer-jump-to-filter-group group
)
1340 (ibuffer-toggle-filter-group)
1341 (ibuffer-map-lines #'(lambda (buf _marks
)
1342 (when (string= (buffer-name buf
) name
)
1343 (setq buf-point
(point))
1348 (ibuffer-toggle-filter-group)))))
1349 (if (null buf-point
)
1350 ;; Still not found even though we expanded all hidden filter
1351 ;; groups: that must be because it's hidden by predicate:
1352 ;; we won't bother trying to display it.
1353 (error "No buffer with name %s" name
)
1354 (goto-char buf-point
)))))
1356 (declare-function diff-sentinel
"diff"
1357 (code &optional old-temp-file new-temp-file
))
1359 (defun ibuffer-diff-buffer-with-file-1 (buffer)
1360 (let ((bufferfile (buffer-local-value 'buffer-file-name buffer
))
1361 (tempfile (make-temp-file "buffer-content-")))
1365 (with-current-buffer buffer
1366 (write-region nil nil tempfile nil
'nomessage
))
1367 (let* ((old (expand-file-name bufferfile
))
1368 (new (expand-file-name tempfile
))
1369 (oldtmp (file-local-copy old
))
1370 (newtmp (file-local-copy new
))
1371 (switches diff-switches
)
1376 ;; Use explicitly specified switches
1377 ,@(if (listp switches
) switches
(list switches
))
1379 (list "-L" (shell-quote-argument old
)
1380 "-L" (shell-quote-argument
1381 (format "Buffer %s" (buffer-name buffer
)))))
1382 ,(shell-quote-argument (or oldtmp old
))
1383 ,(shell-quote-argument (or newtmp new
)))
1385 (let ((inhibit-read-only t
))
1386 (insert command
"\n")
1388 (call-process shell-file-name nil
1389 (current-buffer) nil
1390 shell-command-switch command
))
1393 (when (file-exists-p tempfile
)
1394 (delete-file tempfile
)))))
1397 (defun ibuffer-diff-with-file ()
1398 "View the differences between marked buffers and their associated files.
1399 If no buffers are marked, use buffer at point.
1400 This requires the external program \"diff\" to be in your `exec-path'."
1403 (let ((marked-bufs (ibuffer-get-marked-buffers)))
1404 (when (null marked-bufs
)
1405 (setq marked-bufs
(list (ibuffer-current-buffer t
))))
1406 (with-current-buffer (get-buffer-create "*Ibuffer Diff*")
1407 (setq buffer-read-only nil
)
1408 (buffer-disable-undo (current-buffer))
1410 (buffer-enable-undo (current-buffer))
1412 (dolist (buf marked-bufs
)
1413 (unless (buffer-live-p buf
)
1414 (error "Buffer %s has been killed" buf
))
1415 (ibuffer-diff-buffer-with-file-1 buf
))
1416 (setq buffer-read-only t
)))
1417 (switch-to-buffer "*Ibuffer Diff*"))
1420 (defun ibuffer-copy-filename-as-kill (&optional arg
)
1421 "Copy filenames of marked buffers into the kill ring.
1423 The names are separated by a space.
1424 If a buffer has no filename, it is ignored.
1426 With no prefix arg, use the filename sans its directory of each marked file.
1427 With a zero prefix arg, use the complete filename of each marked file.
1428 With \\[universal-argument], use the filename of each marked file relative
1429 to `ibuffer-default-directory' if non-nil, otherwise `default-directory'.
1431 You can then feed the file name(s) to other commands with \\[yank]."
1433 (if (zerop (ibuffer-count-marked-lines))
1434 (message "No buffers marked; use 'm' to mark a buffer")
1436 (type (cond ((or (null arg
) (zerop arg
))
1442 (ibuffer-map-marked-lines
1443 #'(lambda (buf _mark
)
1446 (let ((name (buffer-file-name buf
)))
1454 name
(or ibuffer-default-directory
1455 default-directory
)))
1457 (file-name-nondirectory name
))) " "))
1459 (when (not (zerop (length result
)))
1461 (substring result
0 -
1)))
1463 (message "%s" result
))))
1466 (defun ibuffer-copy-buffername-as-kill ()
1467 "Copy buffer names of marked buffers into the kill ring.
1468 The names are separated by a space.
1469 You can then feed the file name(s) to other commands with \\[yank]."
1471 (if (zerop (ibuffer-count-marked-lines))
1472 (message "No buffers marked; use 'm' to mark a buffer")
1474 (ibuffer-map-marked-lines
1475 #'(lambda (buf _mark
)
1476 (setq res
(concat res
(buffer-name buf
) " "))))
1477 (when (not (zerop (length res
)))
1478 (setq res
(substring res
0 -
1)))
1482 (defun ibuffer-mark-on-buffer (func &optional ibuffer-mark-on-buffer-mark group
)
1485 #'(lambda (buf _mark
)
1486 (when (funcall func buf
)
1487 (ibuffer-set-mark-1 (or ibuffer-mark-on-buffer-mark
1488 ibuffer-marked-char
))
1492 (ibuffer-redisplay t
)
1493 (unless (eq ibuffer-mark-on-buffer-mark ?\s
)
1494 (message "Marked %s buffers" count
))))
1497 (defun ibuffer-mark-by-name-regexp (regexp)
1498 "Mark all buffers whose name matches REGEXP."
1499 (interactive "sMark by name (regexp): ")
1500 (ibuffer-mark-on-buffer
1502 (string-match regexp
(buffer-name buf
)))))
1504 (defun ibuffer-locked-buffer-p (&optional buf
)
1505 "Return non-nil if BUF is locked.
1506 When BUF nil, default to the buffer at current line."
1507 (let ((cbuffer (or buf
(ibuffer-current-buffer))))
1509 (with-current-buffer cbuffer
1510 (and (boundp 'emacs-lock-mode
) emacs-lock-mode
)))))
1513 (defun ibuffer-mark-by-locked ()
1514 "Mark all locked buffers."
1516 (when (featurep 'emacs-lock
)
1517 (ibuffer-mark-on-buffer
1519 (ibuffer-locked-buffer-p buf
)))))
1522 (defun ibuffer-mark-by-mode-regexp (regexp)
1523 "Mark all buffers whose major mode matches REGEXP."
1524 (interactive "sMark by major mode (regexp): ")
1525 (ibuffer-mark-on-buffer
1527 (with-current-buffer buf
1528 (string-match regexp
(format-mode-line mode-name nil nil buf
))))))
1531 (defun ibuffer-mark-by-file-name-regexp (regexp)
1532 "Mark all buffers whose file name matches REGEXP."
1533 (interactive "sMark by file name (regexp): ")
1534 (ibuffer-mark-on-buffer
1536 (let ((name (or (buffer-file-name buf
)
1537 (with-current-buffer buf
1539 (boundp 'dired-directory
)
1540 (stringp dired-directory
)
1541 dired-directory
)))))
1543 (string-match regexp name
))))))
1546 (defun ibuffer-mark-by-content-regexp (regexp &optional all-buffers
)
1547 "Mark all buffers whose content matches REGEXP.
1548 Optional arg ALL-BUFFERS, if non-nil, then search in all buffers.
1549 Otherwise buffers whose name matches an element of
1550 `ibuffer-never-search-content-name' or whose major mode is on
1551 `ibuffer-never-search-content-mode' are excluded."
1552 (interactive (let ((reg (read-string "Mark by content (regexp): ")))
1553 (list reg current-prefix-arg
)))
1554 (ibuffer-mark-on-buffer
1556 (let ((mode (with-current-buffer buf major-mode
))
1558 (cond ((and (not all-buffers
)
1560 (memq mode ibuffer-never-search-content-mode
)
1561 (cl-some (lambda (x) (string-match x
(buffer-name buf
)))
1562 ibuffer-never-search-content-name
)))
1565 (with-current-buffer buf
1566 (save-mark-and-excursion
1567 (goto-char (point-min))
1568 (setq res
(re-search-forward regexp nil t
)))))) res
))))
1571 (defun ibuffer-mark-by-mode (mode)
1572 "Mark all buffers whose major mode equals MODE."
1574 (let* ((buf (ibuffer-current-buffer))
1575 (default (if (and buf
(buffer-live-p buf
))
1576 (symbol-name (buffer-local-value
1577 'major-mode buf
)))))
1581 (format "Mark by major mode (default %s): " default
)
1582 "Mark by major mode: ")
1583 (ibuffer-list-buffer-modes) nil t nil nil default
)))))
1584 (ibuffer-mark-on-buffer
1586 (eq (buffer-local-value 'major-mode buf
) mode
))))
1589 (defun ibuffer-mark-modified-buffers ()
1590 "Mark all modified buffers."
1592 (ibuffer-mark-on-buffer
1593 #'(lambda (buf) (buffer-modified-p buf
))))
1596 (defun ibuffer-mark-unsaved-buffers ()
1597 "Mark all modified buffers that have an associated file."
1599 (ibuffer-mark-on-buffer
1600 #'(lambda (buf) (and (buffer-local-value 'buffer-file-name buf
)
1601 (buffer-modified-p buf
)))))
1604 (defun ibuffer-mark-dissociated-buffers ()
1605 "Mark all buffers whose associated file does not exist."
1607 (ibuffer-mark-on-buffer
1609 (with-current-buffer buf
1611 (and buffer-file-name
1612 (not (file-exists-p buffer-file-name
)))
1613 (and (eq major-mode
'dired-mode
)
1614 (boundp 'dired-directory
)
1615 (stringp dired-directory
)
1616 (not (file-exists-p (file-name-directory dired-directory
)))))))))
1619 (defun ibuffer-mark-help-buffers ()
1620 "Mark buffers whose major mode is in variable `ibuffer-help-buffer-modes'."
1622 (ibuffer-mark-on-buffer
1624 (with-current-buffer buf
1625 (memq major-mode ibuffer-help-buffer-modes
)))))
1628 (defun ibuffer-mark-compressed-file-buffers ()
1629 "Mark buffers whose associated file is compressed."
1631 (ibuffer-mark-on-buffer
1633 (with-current-buffer buf
1634 (and buffer-file-name
1635 (string-match ibuffer-compressed-file-name-regexp
1636 buffer-file-name
))))))
1639 (defun ibuffer-mark-old-buffers ()
1640 "Mark buffers which have not been viewed in `ibuffer-old-time' hours."
1642 (ibuffer-mark-on-buffer
1644 (with-current-buffer buf
1645 ;; hacked from midnight.el
1646 (when buffer-display-time
1647 (let* ((now (float-time))
1648 (then (float-time buffer-display-time
)))
1649 (> (- now then
) (* 60 60 ibuffer-old-time
))))))))
1652 (defun ibuffer-mark-special-buffers ()
1653 "Mark all buffers whose name begins and ends with `*'."
1655 (ibuffer-mark-on-buffer
1656 #'(lambda (buf) (string-match "^\\*.+\\*$"
1657 (buffer-name buf
)))))
1660 (defun ibuffer-mark-read-only-buffers ()
1661 "Mark all read-only buffers."
1663 (ibuffer-mark-on-buffer
1664 #'(lambda (buf) (buffer-local-value 'buffer-read-only buf
))))
1667 (defun ibuffer-mark-dired-buffers ()
1668 "Mark all `dired' buffers."
1670 (ibuffer-mark-on-buffer
1671 #'(lambda (buf) (eq (buffer-local-value 'major-mode buf
) 'dired-mode
))))
1674 (defun ibuffer-do-occur (regexp &optional nlines
)
1675 "View lines which match REGEXP in all marked buffers.
1676 Optional argument NLINES says how many lines of context to display: it
1678 (interactive (occur-read-primary-args))
1679 (if (or (not (integerp nlines
))
1682 (when (zerop (ibuffer-count-marked-lines))
1683 (ibuffer-set-mark ibuffer-marked-char
))
1684 (let ((ibuffer-do-occur-bufs nil
))
1685 ;; Accumulate a list of marked buffers
1686 (ibuffer-map-marked-lines
1687 #'(lambda (buf _mark
)
1688 (push buf ibuffer-do-occur-bufs
)))
1689 (occur-1 regexp nlines ibuffer-do-occur-bufs
)))
1694 ;; generated-autoload-file: "ibuffer-loaddefs.el"
1697 ;;; ibuf-ext.el ends here