1 ;;; ibuf-ext.el --- extensions for ibuffer -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Colin Walters <walters@verbum.org>
7 ;; Keywords: buffer, convenience
9 ;; This file is not currently part of GNU Emacs.
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program ; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; These functions should be automatically loaded when called, but you
29 ;; can explicity (require 'ibuf-ext) in your ~/.emacs to have them
42 (defun ibuffer-delete-alist (key alist
)
43 "Delete all entries in ALIST that have a key equal to KEY."
45 (while (setq entry
(assoc key alist
))
46 (setq alist
(delete entry alist
)))
49 (defun ibuffer-depropertize-string (str &optional nocopy
)
50 "Return a copy of STR with text properties removed.
51 If optional argument NOCOPY is non-nil, actually modify the string directly."
54 (copy-sequence str
))))
55 (set-text-properties 0 (length str
) nil str
)
58 (defcustom ibuffer-never-show-predicates nil
59 "A list of predicates (a regexp or function) for buffers not to display.
60 If a regexp, then it will be matched against the buffer's name.
61 If a function, it will be called with the buffer as an argument, and
62 should return non-nil if this buffer should not be shown."
63 :type
'(repeat (choice regexp function
))
66 (defcustom ibuffer-always-show-predicates nil
67 "A list of predicates (a regexp or function) for buffers to always display.
68 If a regexp, then it will be matched against the buffer's name.
69 If a function, it will be called with the buffer as an argument, and
70 should return non-nil if this buffer should be shown.
71 Note that buffers matching one of these predicates will be shown
72 regardless of any active filters in this buffer."
73 :type
'(repeat (choice regexp function
))
76 (defvar ibuffer-tmp-hide-regexps nil
77 "A list of regexps which should match buffer names to not show.")
79 (defvar ibuffer-tmp-show-regexps nil
80 "A list of regexps which should match buffer names to always show.")
82 (defvar ibuffer-auto-mode nil
83 "If non-nil, Ibuffer auto-mode should be enabled for this buffer.
84 Do not set this variable directly! Use the function
85 `ibuffer-auto-mode' instead.")
87 (defvar ibuffer-auto-buffers-changed nil
)
89 (defcustom ibuffer-occur-match-face
'font-lock-warning-face
90 "Face used for displaying matched strings for `ibuffer-do-occur'."
94 (defcustom ibuffer-saved-filters
'(("gnus"
95 ((or (mode . message-mode
)
97 (mode . gnus-group-mode
)
98 (mode . gnus-summary-mode
)
99 (mode . gnus-article-mode
))))
101 ((or (mode . emacs-lisp-mode
)
106 (mode . lisp-mode
)))))
108 "An alist of filter qualifiers to switch between.
110 This variable should look like ((\"STRING\" QUALIFIERS)
111 (\"STRING\" QUALIFIERS) ...), where
112 QUALIFIERS is a list of the same form as
113 `ibuffer-filtering-qualifiers'.
114 See also the variables `ibuffer-filtering-qualifiers',
115 `ibuffer-filtering-alist', and the functions
116 `ibuffer-switch-to-saved-filters', `ibuffer-save-filters'."
120 (defvar ibuffer-filtering-qualifiers nil
121 "A list like (SYMBOL . QUALIFIER) which filters the current buffer list.
122 See also `ibuffer-filtering-alist'.")
124 ;; This is now frobbed by `define-ibuffer-filter'.
125 (defvar ibuffer-filtering-alist nil
126 "An alist of (SYMBOL DESCRIPTION FUNCTION) which describes a filter.
128 You most likely do not want to modify this variable directly; see
129 `define-ibuffer-filter'.
131 SYMBOL is the symbolic name of the filter. DESCRIPTION is used when
132 displaying information to the user. FUNCTION is given a buffer and
133 the value of the qualifier, and returns non-nil if and only if the
134 buffer should be displayed.")
136 (defcustom ibuffer-filter-format-alist nil
137 "An alist which has special formats used when a filter is active.
138 The contents of this variable should look like:
139 ((FILTER (FORMAT FORMAT ...)) (FILTER (FORMAT FORMAT ...)) ...)
141 For example, suppose that when you add a filter for buffers whose
142 major mode is `emacs-lisp-mode', you only want to see the mark and the
143 name of the buffer. You could accomplish that by adding:
144 (mode ((mark \" \" name)))
147 (defvar ibuffer-cached-filter-formats nil
)
148 (defvar ibuffer-compiled-filter-formats nil
)
150 (defcustom ibuffer-old-time
72
151 "The number of hours before a buffer is considered \"old\"."
152 :type
'(choice (const :tag
"72 hours (3 days)" 72)
153 (const :tag
"48 hours (2 days)" 48)
154 (const :tag
"24 hours (1 day)" 24)
155 (integer :tag
"hours"))
158 (defcustom ibuffer-save-with-custom t
159 "If non-nil, then use Custom to save interactively changed variables.
160 Currently, this only applies to `ibuffer-saved-filters'."
164 (defun ibuffer-ext-visible-p (buf all
&optional ibuffer-buf
)
166 (ibuffer-buf-matches-predicates buf ibuffer-tmp-show-regexps
)
169 (ibuffer-buf-matches-predicates buf ibuffer-tmp-hide-regexps
)
170 (ibuffer-buf-matches-predicates buf ibuffer-never-show-predicates
)))
173 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates
)))
174 (or ibuffer-view-ibuffer
176 (not (eq ibuffer-buf buf
))))
178 (ibuffer-included-in-filters-p buf ibuffer-filtering-qualifiers
)
179 (ibuffer-buf-matches-predicates buf ibuffer-always-show-predicates
)))))
181 (defun ibuffer-auto-update-changed ()
182 (when ibuffer-auto-buffers-changed
183 (setq ibuffer-auto-buffers-changed nil
)
184 (mapcar #'(lambda (buf)
186 (with-current-buffer buf
187 (when (and ibuffer-auto-mode
188 (eq major-mode
'ibuffer-mode
))
189 (ibuffer-update nil t
)))))
193 (defun ibuffer-auto-mode (&optional arg
)
194 "Toggle use of Ibuffer's auto-update facility.
195 With numeric ARG, enable auto-update if and only if ARG is positive."
197 (unless (eq major-mode
'ibuffer-mode
)
198 (error "This buffer is not in Ibuffer mode"))
199 (set (make-local-variable 'ibuffer-auto-mode
)
202 (not ibuffer-auto-mode
)))
203 (defadvice get-buffer-create
(after ibuffer-notify-create activate
)
204 (setq ibuffer-auto-buffers-changed t
))
205 (defadvice kill-buffer
(after ibuffer-notify-kill activate
)
206 (setq ibuffer-auto-buffers-changed t
))
207 (add-hook 'post-command-hook
'ibuffer-auto-update-changed
)
208 (ibuffer-update-mode-name))
211 (defun ibuffer-mouse-filter-by-mode (event)
212 "Enable or disable filtering by the major mode chosen via mouse."
214 (ibuffer-interactive-filter-by-mode event
))
217 (defun ibuffer-interactive-filter-by-mode (event-or-point)
218 "Enable or disable filtering by the major mode at point."
220 (if (eventp event-or-point
)
221 (mouse-set-point event-or-point
)
222 (goto-char event-or-point
))
223 (let ((buf (ibuffer-current-buffer)))
224 (if (assq 'mode ibuffer-filtering-qualifiers
)
225 (setq ibuffer-filtering-qualifiers
226 (ibuffer-delete-alist 'mode ibuffer-filtering-qualifiers
))
227 (ibuffer-push-filter (cons 'mode
228 (with-current-buffer buf
230 (ibuffer-update nil t
))
232 (define-ibuffer-op shell-command-pipe
(command)
233 "Pipe the contents of each marked buffer to shell command COMMAND."
234 (:interactive
"sPipe to shell command: "
235 :opstring
"Shell command executed on"
237 (shell-command-on-region
238 (point-min) (point-max) command
239 (get-buffer-create "* ibuffer-shell-output*")))
241 (define-ibuffer-op shell-command-pipe-replace
(command)
242 "Replace the contents of marked buffers with output of pipe to COMMAND."
243 (:interactive
"sPipe to shell command (replace): "
244 :opstring
"Buffer contents replaced in"
245 :active-opstring
"replace buffer contents in"
248 (with-current-buffer buf
249 (shell-command-on-region (point-min) (point-max)
252 (define-ibuffer-op shell-command-file
(command)
253 "Run shell command COMMAND separately on files of marked buffers."
254 (:interactive
"sShell command on buffer's file: "
255 :opstring
"Shell command executed on"
257 (shell-command (concat command
" "
258 (shell-quote-argument
262 (substring (buffer-name) 0 (min 10 (length (buffer-name))))))))))
264 (define-ibuffer-op eval
(form)
265 "Evaluate FORM in each of the buffers.
266 Does not display the buffer during evaluation. See
267 `ibuffer-do-view-and-eval' for that."
268 (:interactive
"xEval in buffers (form): "
269 :opstring
"evaluated in"
273 (define-ibuffer-op view-and-eval
(form)
274 "Evaluate FORM while displaying each of the marked buffers.
275 To evaluate a form without viewing the buffer, see `ibuffer-do-eval'."
276 (:interactive
"xEval viewing buffers (form): "
277 :opstring
"evaluated in"
280 (let ((ibuffer-buf (current-buffer)))
283 (switch-to-buffer buf
)
285 (switch-to-buffer ibuffer-buf
))))
287 (define-ibuffer-op rename-uniquely
()
288 "Rename marked buffers as with `rename-uniquely'."
293 (define-ibuffer-op revert
()
294 "Revert marked buffers as with `revert-buffer'."
297 :active-opstring
"revert"
301 (define-ibuffer-op replace-regexp
(from-str to-str
)
302 "Perform a `replace-regexp' in marked buffers."
304 (let* ((from-str (read-from-minibuffer "Replace regexp: "))
305 (to-str (read-from-minibuffer (concat "Replace " from-str
307 (list from-str to-str
))
308 :opstring
"replaced in"
311 (save-window-excursion
312 (switch-to-buffer buf
)
314 (goto-char (point-min))
315 (let ((case-fold-search ibuffer-case-fold-search
))
316 (while (re-search-forward from-str nil t
)
317 (replace-match to-str
))))
320 (define-ibuffer-op query-replace
(&rest args
)
321 "Perform a `query-replace' in marked buffers."
323 (query-replace-read-args "Query replace" t
)
324 :opstring
"replaced in"
327 (save-window-excursion
328 (switch-to-buffer buf
)
330 (let ((case-fold-search ibuffer-case-fold-search
))
331 (goto-char (point-min))
332 (apply #'query-replace args
)))
335 (define-ibuffer-op query-replace-regexp
(&rest args
)
336 "Perform a `query-replace-regexp' in marked buffers."
338 (query-replace-read-args "Query replace regexp" t
)
339 :opstring
"replaced in"
342 (save-window-excursion
343 (switch-to-buffer buf
)
345 (let ((case-fold-search ibuffer-case-fold-search
))
346 (goto-char (point-min))
347 (apply #'query-replace-regexp args
)))
350 (define-ibuffer-op print
()
351 "Print marked buffers as with `print-buffer'."
357 (defun ibuffer-included-in-filters-p (buf filters
)
359 (memq nil
;; a filter will return nil if it failed
361 ;; filter should be like (TYPE . QUALIFIER), or
362 ;; (or (TYPE . QUALIFIER) (TYPE . QUALIFIER) ...)
364 (ibuffer-included-in-filter-p buf qual
))
367 (defun ibuffer-included-in-filter-p (buf filter
)
368 (if (eq (car filter
) 'not
)
369 (not (ibuffer-included-in-filter-p-1 buf
(cdr filter
)))
370 (ibuffer-included-in-filter-p-1 buf filter
)))
372 (defun ibuffer-included-in-filter-p-1 (buf filter
)
377 (memq t
(mapcar #'(lambda (x)
378 (ibuffer-included-in-filter-p buf x
))
383 ibuffer-saved-filters
)))
385 (ibuffer-filter-disable)
386 (error "Unknown saved filter %s" (cdr filter
)))
387 (ibuffer-included-in-filters-p buf
(cadr data
))))
389 (let ((filterdat (assq (car filter
)
390 ibuffer-filtering-alist
)))
391 ;; filterdat should be like (TYPE DESCRIPTION FUNC)
392 ;; just a sanity check
394 (ibuffer-filter-disable)
395 (error "Undefined filter %s" (car filter
)))
398 (funcall (caddr filterdat
)
400 (cdr filter
))))))))))
403 (defun ibuffer-filter-disable ()
404 "Disable all filters currently in effect in this buffer."
406 (setq ibuffer-filtering-qualifiers nil
)
407 (ibuffer-update nil t
))
410 (defun ibuffer-pop-filter ()
411 "Remove the top filter in this buffer."
413 (when (null ibuffer-filtering-qualifiers
)
414 (error "No filters in effect"))
415 (pop ibuffer-filtering-qualifiers
)
416 (ibuffer-update nil t
))
418 (defun ibuffer-push-filter (qualifier)
419 "Add QUALIFIER to `ibuffer-filtering-qualifiers'."
420 (push qualifier ibuffer-filtering-qualifiers
))
423 (defun ibuffer-decompose-filter ()
424 "Separate the top compound filter (OR, NOT, or SAVED) in this buffer.
426 This means that the topmost filter on the filtering stack, which must
427 be a complex filter like (OR [name: foo] [mode: bar-mode]), will be
428 turned into two separate filters [name: foo] and [mode: bar-mode]."
430 (when (null ibuffer-filtering-qualifiers
)
431 (error "No filters in effect"))
432 (let ((lim (pop ibuffer-filtering-qualifiers
)))
435 (setq ibuffer-filtering-qualifiers
(append
437 ibuffer-filtering-qualifiers
)))
441 ibuffer-saved-filters
)))
443 (ibuffer-filter-disable)
444 (error "Unknown saved filter %s" (cdr lim
)))
445 (setq ibuffer-filtering-qualifiers
(append
447 ibuffer-filtering-qualifiers
))))
450 ibuffer-filtering-qualifiers
))
452 (error "Filter type %s is not compound" (car lim
)))))
453 (ibuffer-update nil t
))
456 (defun ibuffer-exchange-filters ()
457 "Exchange the top two filters on the stack in this buffer."
459 (when (< (length ibuffer-filtering-qualifiers
)
461 (error "Need two filters to exchange"))
462 (let ((first (pop ibuffer-filtering-qualifiers
))
463 (second (pop ibuffer-filtering-qualifiers
)))
464 (push first ibuffer-filtering-qualifiers
)
465 (push second ibuffer-filtering-qualifiers
))
466 (ibuffer-update nil t
))
469 (defun ibuffer-negate-filter ()
470 "Negate the sense of the top filter in the current buffer."
472 (when (null ibuffer-filtering-qualifiers
)
473 (error "No filters in effect"))
474 (let ((lim (pop ibuffer-filtering-qualifiers
)))
475 (push (if (eq (car lim
) 'not
)
478 ibuffer-filtering-qualifiers
))
479 (ibuffer-update nil t
))
482 (defun ibuffer-or-filter (&optional reverse
)
483 "Replace the top two filters in this buffer with their logical OR.
484 If optional argument REVERSE is non-nil, instead break the top OR
489 (when (or (null ibuffer-filtering-qualifiers
)
490 (not (eq 'or
(caar ibuffer-filtering-qualifiers
))))
491 (error "Top filter is not an OR"))
492 (let ((lim (pop ibuffer-filtering-qualifiers
)))
493 (setq ibuffer-filtering-qualifiers
(nconc (cdr lim
) ibuffer-filtering-qualifiers
))))
494 (when (< (length ibuffer-filtering-qualifiers
) 2)
495 (error "Need two filters to OR"))
496 ;; If the second filter is an OR, just add to it.
497 (let ((first (pop ibuffer-filtering-qualifiers
))
498 (second (pop ibuffer-filtering-qualifiers
)))
499 (if (eq 'or
(car second
))
500 (push (nconc (list 'or first
) (cdr second
)) ibuffer-filtering-qualifiers
)
501 (push (list 'or first second
)
502 ibuffer-filtering-qualifiers
))))
503 (ibuffer-update nil t
))
505 (defun ibuffer-maybe-save-saved-filters ()
506 (when ibuffer-save-with-custom
507 (if (fboundp 'customize-save-variable
)
509 (customize-save-variable 'ibuffer-saved-filters
510 ibuffer-saved-filters
))
511 (message "Not saved permanently: Customize not available"))))
514 (defun ibuffer-save-filters (name filters
)
515 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'.
516 Interactively, prompt for NAME, and use the current filters."
518 (if (null ibuffer-filtering-qualifiers
)
519 (error "No filters currently in effect")
521 (read-from-minibuffer "Save current filters as: ")
522 ibuffer-filtering-qualifiers
)))
523 (ibuffer-aif (assoc name ibuffer-saved-filters
)
525 (push (list name filters
) ibuffer-saved-filters
))
526 (ibuffer-maybe-save-saved-filters)
527 (ibuffer-update-mode-name))
530 (defun ibuffer-delete-saved-filters (name)
531 "Delete saved filters with NAME from `ibuffer-saved-filters'."
534 (if (null ibuffer-saved-filters
)
535 (error "No saved filters")
536 (completing-read "Delete saved filters: "
537 ibuffer-saved-filters nil t
))))
538 (setq ibuffer-saved-filters
539 (ibuffer-delete-alist name ibuffer-saved-filters
))
540 (ibuffer-maybe-save-saved-filters)
541 (ibuffer-update nil t
))
544 (defun ibuffer-add-saved-filters (name)
545 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
548 (if (null ibuffer-saved-filters
)
549 (error "No saved filters")
550 (completing-read "Add saved filters: "
551 ibuffer-saved-filters nil t
))))
552 (push (cons 'saved name
) ibuffer-filtering-qualifiers
)
553 (ibuffer-update nil t
))
556 (defun ibuffer-switch-to-saved-filters (name)
557 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'.
558 If prefix argument ADD is non-nil, then add the saved filters instead
559 of replacing the current filters."
562 (if (null ibuffer-saved-filters
)
563 (error "No saved filters")
564 (completing-read "Switch to saved filters: "
565 ibuffer-saved-filters nil t
))))
566 (setq ibuffer-filtering-qualifiers
(list (cons 'saved name
)))
567 (ibuffer-update nil t
))
569 (defun ibuffer-format-qualifier (qualifier)
570 (if (eq (car-safe qualifier
) 'not
)
571 (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier
)) "]")
572 (ibuffer-format-qualifier-1 qualifier
)))
574 (defun ibuffer-format-qualifier-1 (qualifier)
575 (case (car qualifier
)
577 (concat " [filter: " (cdr qualifier
) "]"))
579 (concat " [OR" (mapconcat #'ibuffer-format-qualifier
580 (cdr qualifier
) "") "]"))
582 (let ((type (assq (car qualifier
) ibuffer-filtering-alist
)))
584 (error "Ibuffer: bad qualifier %s" qualifier
))
585 (concat " [" (cadr type
) ": " (format "%s]" (cdr qualifier
)))))))
587 ;;; Extra operation definitions
589 (define-ibuffer-filter mode
590 "Toggle current view to buffers with major mode QUALIFIER."
591 (:description
"major mode"
594 (completing-read "Filter by major mode: " obarray
596 (string-match "-mode$"
599 (let ((buf (ibuffer-current-buffer)))
600 (if (and buf
(buffer-live-p buf
))
601 (with-current-buffer buf
602 (symbol-name major-mode
))
604 (eq qualifier
(with-current-buffer buf major-mode
)))
606 (define-ibuffer-filter name
607 "Toggle current view to buffers with name matching QUALIFIER."
608 (:description
"buffer name"
610 (read-from-minibuffer "Filter by name (regexp): "))
611 (string-match qualifier
(buffer-name buf
)))
613 (define-ibuffer-filter filename
614 "Toggle current view to buffers with filename matching QUALIFIER."
615 (:description
"filename"
617 (read-from-minibuffer "Filter by filename (regexp): "))
618 (ibuffer-awhen (buffer-file-name buf
)
619 (string-match qualifier it
)))
621 (define-ibuffer-filter size-gt
622 "Toggle current view to buffers with size greater than QUALIFIER."
623 (:description
"size greater than"
625 (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
626 (> (with-current-buffer buf
(buffer-size))
629 (define-ibuffer-filter size-lt
630 "Toggle current view to buffers with size less than QUALIFIER."
631 (:description
"size less than"
633 (string-to-number (read-from-minibuffer "Filter by size less than: ")))
634 (< (with-current-buffer buf
(buffer-size))
637 (define-ibuffer-filter content
638 "Toggle current view to buffers whose contents match QUALIFIER."
639 (:description
"content"
641 (read-from-minibuffer "Filter by content (regexp): "))
642 (with-current-buffer buf
644 (goto-char (point-min))
645 (re-search-forward qualifier nil t
))))
647 (define-ibuffer-filter predicate
648 "Toggle current view to buffers for which QUALIFIER returns non-nil."
649 (:description
"predicate"
651 (read-minibuffer "Filter by predicate (form): "))
652 (with-current-buffer buf
658 (defun ibuffer-toggle-sorting-mode ()
659 "Toggle the current sorting mode.
660 Default sorting modes are:
661 Recency - the last time the buffer was viewed
662 Name - the name of the buffer
663 Major Mode - the name of the major mode of the buffer
664 Size - the size of the buffer"
666 (let ((modes (mapcar 'car ibuffer-sorting-functions-alist
)))
667 (add-to-list 'modes
'recency
)
668 (setq modes
(sort modes
'string-lessp
))
669 (let ((next (or (find-if
670 (lambda (x) (string-lessp ibuffer-sorting-mode x
)) modes
)
672 (setq ibuffer-sorting-mode next
)
673 (message "Sorting by %s" next
)))
674 (ibuffer-redisplay t
))
677 (defun ibuffer-invert-sorting ()
678 "Toggle whether or not sorting is in reverse order."
680 (setq ibuffer-sorting-reversep
(not ibuffer-sorting-reversep
))
681 (message "Sorting order %s"
682 (if ibuffer-sorting-reversep
685 (ibuffer-redisplay t
))
687 (define-ibuffer-sorter major-mode
688 "Sort the buffers by major modes.
689 Ordering is lexicographic."
690 (:description
"major mode")
691 (string-lessp (downcase
692 (symbol-name (with-current-buffer
696 (symbol-name (with-current-buffer
700 (define-ibuffer-sorter mode-name
701 "Sort the buffers by their mode name.
702 Ordering is lexicographic."
703 (:description
"mode name")
704 (string-lessp (downcase
705 (symbol-name (with-current-buffer
709 (symbol-name (with-current-buffer
713 (define-ibuffer-sorter alphabetic
714 "Sort the buffers by their names.
715 Ordering is lexicographic."
716 (:description
"buffer name")
718 (buffer-name (car a
))
719 (buffer-name (car b
))))
721 (define-ibuffer-sorter size
722 "Sort the buffers by their size."
723 (:description
"size")
724 (< (with-current-buffer (car a
)
726 (with-current-buffer (car b
)
729 ;;; Functions to emulate bs.el
732 (defun ibuffer-bs-show ()
733 "Emulate `bs-show' from the bs.el package."
735 (ibuffer t
"*Ibuffer-bs*" '((filename .
".*")) nil t
)
736 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all
))
738 (defun ibuffer-bs-toggle-all ()
739 "Emulate `bs-toggle-show-all' from the bs.el package."
741 (if ibuffer-filtering-qualifiers
743 (progn (ibuffer-push-filter '(filename .
".*"))
744 (ibuffer-update nil t
))))
749 (defun ibuffer-add-to-tmp-hide (regexp)
750 "Add REGEXP to `ibuffer-tmp-hide-regexps'.
751 This means that buffers whose name matches REGEXP will not be shown
752 for this ibuffer session."
755 (read-from-minibuffer "Never show buffers matching: "
756 (regexp-quote (buffer-name (ibuffer-current-buffer t
))))))
757 (push regexp ibuffer-tmp-hide-regexps
))
760 (defun ibuffer-add-to-tmp-show (regexp)
761 "Add REGEXP to `ibuffer-tmp-show-regexps'.
762 This means that buffers whose name matches REGEXP will always be shown
763 for this ibuffer session."
766 (read-from-minibuffer "Always show buffers matching: "
767 (regexp-quote (buffer-name (ibuffer-current-buffer t
))))))
768 (push regexp ibuffer-tmp-show-regexps
))
771 (defun ibuffer-forward-next-marked (&optional count mark direction
)
772 "Move forward by COUNT marked buffers (default 1).
774 If MARK is non-nil, it should be a character denoting the type of mark
775 to move by. The default is `ibuffer-marked-char'.
777 If DIRECTION is non-nil, it should be an integer; negative integers
778 mean move backwards, non-negative integers mean move forwards."
783 (setq mark ibuffer-marked-char
))
787 (ibuffer-forward-line 0)
790 (ibuffer-forward-line direction
)
791 (while (not (or (= (point) opos
)
792 (eq (setq curmark
(ibuffer-current-mark))
794 (ibuffer-forward-line direction
))
795 (when (and (= (point) opos
)
796 (not (eq (ibuffer-current-mark) mark
)))
797 (error "No buffers with mark %c" mark
))))
800 (defun ibuffer-backwards-next-marked (&optional count mark
)
801 "Move backwards by COUNT marked buffers (default 1).
803 If MARK is non-nil, it should be a character denoting the type of mark
804 to move by. The default is `ibuffer-marked-char'."
806 (ibuffer-forward-next-marked count mark -
1))
809 (defun ibuffer-do-kill-lines ()
810 "Hide all of the currently marked lines."
812 (if (= (ibuffer-count-marked-lines) 0)
813 (message "No buffers marked; use 'm' to mark a buffer")
815 (ibuffer-map-marked-lines
816 #'(lambda (buf mark beg end
)
818 (message "Killed %s lines" count
))))
821 (defun ibuffer-jump-to-buffer (name)
822 "Move point to the buffer whose name is NAME."
823 (interactive (list nil
))
824 (let ((table (mapcar #'(lambda (x)
825 (cons (buffer-name (car x
))
827 (ibuffer-current-state-list t
))))
829 (error "No buffers!"))
830 (when (interactive-p)
831 (setq name
(completing-read "Jump to buffer: " table nil t
)))
832 (ibuffer-aif (assoc name table
)
834 (error "No buffer with name %s" name
))))
837 (defun ibuffer-diff-with-file ()
838 "View the differences between this buffer and its associated file.
839 This requires the external program \"diff\" to be in your `exec-path'."
841 (let* ((buf (ibuffer-current-buffer))
842 (buf-filename (with-current-buffer buf
844 (unless (buffer-live-p buf
)
845 (error "Buffer %s has been killed" buf
))
847 (error "Buffer %s has no associated file" buf
))
848 (let ((diff-buf (get-buffer-create "*Ibuffer-diff*")))
849 (with-current-buffer diff-buf
850 (setq buffer-read-only nil
)
852 (let ((tempfile (make-temp-file "ibuffer-diff-")))
855 (with-current-buffer buf
856 (write-region (point-min) (point-max) tempfile nil
'nomessage
))
858 (apply #'call-process
"diff" nil diff-buf nil
860 (when (and (boundp 'ediff-custom-diff-options
)
861 (stringp ediff-custom-diff-options
))
862 (list ediff-custom-diff-options
))
863 (list buf-filename tempfile
))))
864 (message "No differences found")
866 (with-current-buffer diff-buf
867 (goto-char (point-min))
868 (if (fboundp 'diff-mode
)
871 (display-buffer diff-buf
))))
872 (when (file-exists-p tempfile
)
873 (delete-file tempfile
)))))
877 (defun ibuffer-copy-filename-as-kill (&optional arg
)
878 "Copy filenames of marked buffers into the kill ring.
879 The names are separated by a space.
880 If a buffer has no filename, it is ignored.
881 With a zero prefix arg, use the complete pathname of each marked file.
883 You can then feed the file name(s) to other commands with C-y.
885 [ This docstring shamelessly stolen from the
886 `dired-copy-filename-as-kill' in \"dired-x\". ]"
887 ;; Add to docstring later:
888 ;; With C-u, use the relative pathname of each marked file.
890 (if (= (ibuffer-count-marked-lines) 0)
891 (message "No buffers marked; use 'm' to mark a buffer")
892 (let ((ibuffer-copy-filename-as-kill-result "")
893 (type (cond ((eql arg
0)
899 (ibuffer-map-marked-lines
900 #'(lambda (buf mark beg end
)
901 (setq ibuffer-copy-filename-as-kill-result
902 (concat ibuffer-copy-filename-as-kill-result
903 (let ((name (buffer-file-name buf
)))
909 (file-name-nondirectory name
)))
912 (push ibuffer-copy-filename-as-kill-result kill-ring
))))
914 (defun ibuffer-mark-on-buffer (func)
917 #'(lambda (buf mark beg end
)
918 (when (funcall func buf
)
919 (ibuffer-set-mark-1 ibuffer-marked-char
)
921 (ibuffer-redisplay t
)
922 (message "Marked %s buffers" count
)))
925 (defun ibuffer-mark-by-name-regexp (regexp)
926 "Mark all buffers whose name matches REGEXP."
927 (interactive "sMark by name (regexp): ")
928 (ibuffer-mark-on-buffer
930 (string-match regexp
(buffer-name buf
)))))
933 (defun ibuffer-mark-by-mode-regexp (regexp)
934 "Mark all buffers whose major mode matches REGEXP."
935 (interactive "sMark by major mode (regexp): ")
936 (ibuffer-mark-on-buffer
938 (with-current-buffer buf
939 (string-match regexp mode-name
)))))
942 (defun ibuffer-mark-by-file-name-regexp (regexp)
943 "Mark all buffers whose file name matches REGEXP."
944 (interactive "sMark by file name (regexp): ")
945 (ibuffer-mark-on-buffer
947 (let ((name (or (buffer-file-name buf
)
948 (with-current-buffer buf
950 (boundp 'dired-directory
)
951 (stringp dired-directory
)
954 (string-match regexp name
))))))
957 (defun ibuffer-mark-by-mode (mode)
958 "Mark all buffers whose major mode equals MODE."
960 (list (intern (completing-read "Mark by major mode: " obarray
964 (string-match "-mode$"
967 (let ((buf (ibuffer-current-buffer)))
968 (if (and buf
(buffer-live-p buf
))
969 (with-current-buffer buf
970 (cons (symbol-name major-mode
)
973 (ibuffer-mark-on-buffer
975 (with-current-buffer buf
976 (eq major-mode mode
)))))
979 (defun ibuffer-mark-modified-buffers ()
980 "Mark all modified buffers."
982 (ibuffer-mark-on-buffer
983 #'(lambda (buf) (buffer-modified-p buf
))))
986 (defun ibuffer-mark-unsaved-buffers ()
987 "Mark all modified buffers that have an associated file."
989 (ibuffer-mark-on-buffer
990 #'(lambda (buf) (and (with-current-buffer buf buffer-file-name
)
991 (buffer-modified-p buf
)))))
994 (defun ibuffer-mark-dissociated-buffers ()
995 "Mark all buffers whose associated file does not exist."
997 (ibuffer-mark-on-buffer
999 (with-current-buffer buf
1001 (and buffer-file-name
1002 (not (file-exists-p buffer-file-name
)))
1003 (and (eq major-mode
'dired-mode
)
1004 (boundp 'dired-directory
)
1005 (stringp dired-directory
)
1006 (not (file-exists-p (file-name-directory dired-directory
)))))))))
1009 (defun ibuffer-mark-help-buffers ()
1010 "Mark buffers like *Help*, *Apropos*, *Info*."
1012 (ibuffer-mark-on-buffer
1014 (with-current-buffer buf
1016 (eq major-mode
'apropos-mode
)
1017 (eq major-mode
'help-mode
)
1018 (eq major-mode
'info-mode
))))))
1021 (defun ibuffer-mark-old-buffers ()
1022 "Mark buffers which have not been viewed in `ibuffer-old-time' days."
1024 (ibuffer-mark-on-buffer
1026 (with-current-buffer buf
1027 ;; hacked from midnight.el
1028 (when buffer-display-time
1029 (let* ((tm (current-time))
1030 (now (+ (* (float (ash 1 16)) (car tm
))
1031 (float (cadr tm
)) (* 0.0000001 (caddr tm
))))
1032 (then (+ (* (float (ash 1 16))
1033 (car buffer-display-time
))
1034 (float (cadr buffer-display-time
))
1035 (* 0.0000001 (caddr buffer-display-time
)))))
1036 (> (- now then
) (* 60 60 ibuffer-old-time
))))))))
1039 (defun ibuffer-mark-special-buffers ()
1040 "Mark all buffers whose name begins and ends with '*'."
1042 (ibuffer-mark-on-buffer
1043 #'(lambda (buf) (string-match "^\\*.+\\*$"
1044 (buffer-name buf
)))))
1047 (defun ibuffer-mark-read-only-buffers ()
1048 "Mark all read-only buffers."
1050 (ibuffer-mark-on-buffer
1052 (with-current-buffer buf
1053 buffer-read-only
))))
1056 (defun ibuffer-mark-dired-buffers ()
1057 "Mark all `dired' buffers."
1059 (ibuffer-mark-on-buffer
1061 (with-current-buffer buf
1062 (eq major-mode
'dired-mode
)))))
1064 ;;; An implementation of multi-buffer `occur'
1066 (defvar ibuffer-occur-props nil
)
1068 (define-derived-mode ibuffer-occur-mode occur-mode
"Ibuffer-Occur"
1069 "A special form of Occur mode for multiple buffers.
1070 Note this major mode is not meant for interactive use!
1071 See also `occur-mode'."
1072 (define-key ibuffer-occur-mode-map
(kbd "n") 'forward-line
)
1073 (define-key ibuffer-occur-mode-map
(kbd "q") 'bury-buffer
)
1074 (define-key ibuffer-occur-mode-map
(kbd "p") 'previous-line
)
1075 (define-key ibuffer-occur-mode-map
(kbd "RET") 'ibuffer-occur-display-occurence
)
1076 (define-key ibuffer-occur-mode-map
(kbd "f") 'ibuffer-occur-goto-occurence
)
1077 (define-key ibuffer-occur-mode-map
[(mouse-2)] 'ibuffer-occur-mouse-display-occurence
)
1078 (set (make-local-variable 'revert-buffer-function
)
1079 #'ibuffer-occur-revert-buffer-function
)
1080 (set (make-local-variable 'ibuffer-occur-props
) nil
)
1081 (setq buffer-read-only nil
)
1083 (setq buffer-read-only t
)
1086 (if (or (and (< 21 emacs-major-version
)
1090 "to display an occurence.")))
1092 (defun ibuffer-occur-mouse-display-occurence (e)
1093 "Display occurence on this line in another window."
1095 (let* ((occurbuf (save-window-excursion (mouse-select-window e
)
1097 (target (with-current-buffer occurbuf
1098 (get-text-property (save-excursion
1101 'ibuffer-occur-target
))))
1103 (error "No occurence on this line"))
1104 (let ((buf (car target
))
1105 (line (cdr target
)))
1106 (switch-to-buffer occurbuf
)
1107 (delete-other-windows)
1111 (defun ibuffer-occur-goto-occurence ()
1112 "Switch to the buffer which has the occurence on this line."
1114 (ibuffer-occur-display-occurence t
))
1116 (defun ibuffer-occur-display-occurence (&optional goto
)
1117 "Display occurence on this line in another window."
1119 (let ((target (get-text-property (point) 'ibuffer-occur-target
)))
1121 (error "No occurence on this line"))
1122 (let ((buf (car target
))
1123 (line (cdr target
)))
1124 (delete-other-windows)
1126 (switch-to-buffer buf
)
1127 (pop-to-buffer buf
))
1131 (defun ibuffer-do-occur (regexp &optional nlines
)
1132 "View lines which match REGEXP in all marked buffers.
1133 Optional argument NLINES says how many lines of context to display: it
1136 (list (let* ((default (car regexp-history
))
1138 (read-from-minibuffer
1140 (format "List lines matching regexp (default `%s'): "
1142 "List lines matching regexp: ")
1147 (if (equal input
"")
1150 current-prefix-arg
))
1151 (if (or (not (integerp nlines
))
1154 (when (zerop (ibuffer-count-marked-lines))
1155 (ibuffer-set-mark ibuffer-marked-char
))
1156 (let ((ibuffer-do-occur-bufs nil
))
1157 ;; Accumulate a list of marked buffers
1158 (ibuffer-map-marked-lines
1159 #'(lambda (buf mark beg end
)
1160 (push buf ibuffer-do-occur-bufs
)))
1161 (ibuffer-do-occur-1 regexp ibuffer-do-occur-bufs
1162 (get-buffer-create "*Ibuffer-occur*")
1165 (defun ibuffer-do-occur-1 (regexp buffers out-buf nlines
)
1166 (let ((count (ibuffer-occur-engine regexp buffers out-buf nlines
)))
1169 (switch-to-buffer out-buf
)
1170 (setq buffer-read-only t
)
1171 (delete-other-windows)
1172 (goto-char (point-min))
1173 (message "Found %s matches in %s buffers" count
(length buffers
)))
1174 (message "No matches found"))))
1177 (defun ibuffer-occur-revert-buffer-function (ignore-auto noconfirm
)
1178 "Update the *Ibuffer occur* buffer."
1179 (assert (eq major-mode
'ibuffer-occur-mode
))
1180 (ibuffer-do-occur-1 (car ibuffer-occur-props
)
1181 (cadr ibuffer-occur-props
)
1183 (caddr ibuffer-occur-props
)))
1185 (defun ibuffer-occur-engine (regexp buffers out-buf nlines
)
1186 (macrolet ((insert-get-point
1193 `(when (ibuffer-use-fontification)
1194 (overlay-put ,over
,prop
,value
)))
1195 (maybe-ibuffer-propertize
1197 (let ((objsym (gensym "--maybe-ibuffer-propertize-")))
1198 `(let ((,objsym
,obj
))
1199 (if (ibuffer-use-fontification)
1200 (propertize ,objsym
,@args
)
1202 (with-current-buffer out-buf
1203 (ibuffer-occur-mode)
1204 (setq buffer-read-only nil
)
1205 (let ((globalcount 0))
1206 ;; Map over all the buffers
1207 (dolist (buf buffers
)
1208 (when (buffer-live-p buf
)
1209 (let ((c 0) ;; count of matched lines
1211 (headerpt (with-current-buffer out-buf
(point))))
1215 (goto-char (point-min)) ;; begin searching in the buffer
1217 ;; The line we're matching against
1218 (let ((curline (buffer-substring
1219 (line-beginning-position)
1220 (line-end-position))))
1221 (when (string-match regexp curline
)
1222 (incf c
) ;; increment match count
1224 ;; Depropertize the string, and maybe highlight the matches
1227 (ibuffer-depropertize-string curline t
)
1228 (when (ibuffer-use-fontification)
1229 (let ((len (length curline
))
1231 (while (and (< start len
)
1232 (string-match regexp curline start
))
1233 (put-text-property (match-beginning 0)
1235 'face ibuffer-occur-match-face
1237 (setq start
(match-end 0)))))
1239 ;; Generate the string to insert for this match
1242 ;; The simple display style
1243 (concat (maybe-ibuffer-propertize
1248 ;; The complex multi-line display style
1249 (let ((prevlines (nreverse
1250 (ibuffer-accumulate-lines (- nlines
))))
1251 (nextlines (ibuffer-accumulate-lines nlines
))
1252 ;; The lack of `flet' seriously sucks.
1253 (fun #'(lambda (lines)
1256 (concat " :" line
"\n"))
1258 (setq prevlines
(funcall fun prevlines
))
1259 (setq nextlines
(funcall fun nextlines
))
1260 ;; Yes, I am trying to win the award for the
1267 (maybe-ibuffer-propertize
1274 ;; Actually insert the match display data
1275 (with-current-buffer out-buf
1277 (end (insert-get-point
1279 (unless (= nlines
1)
1280 (insert "-------\n"))
1282 beg
(1- end
) 'ibuffer-occur-target
(cons buf l
))
1284 beg
(1- end
) 'mouse-face
'highlight
))))))
1285 ;; On to the next line...
1288 (when (not (zerop c
)) ;; is the count zero?
1289 (with-current-buffer out-buf
1290 (goto-char headerpt
)
1292 (end (insert-get-point
1293 (format "%d lines matching \"%s\" in buffer %s\n"
1294 c regexp
(buffer-name buf
)))))
1295 (let ((o (make-overlay beg end
)))
1296 (maybe-put-overlay o
'face
'underline
)))
1297 (goto-char (point-max)))))))
1298 (setq ibuffer-occur-props
(list regexp buffers nlines
))
1299 ;; Return the number of matches
1304 ;;; ibuf-ext.el ends here