(x-select-text, x-cut-buffer-or-selection-value): Check if any of the
[emacs.git] / lisp / ibuffer.el
blobe7fb35496252ba80d9c0c90b0036695ebe736a1f
1 ;;; ibuffer.el --- operate on buffers like dired
3 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Colin Walters <walters@verbum.org>
6 ;; Created: 8 Sep 2000
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.
26 ;;; Commentary:
28 ;; ibuffer.el is an advanced replacement for the `buffer-menu' which
29 ;; is normally distributed with Emacs. Its interface is intended to
30 ;; be analogous to that of Dired.
32 ;;; Code:
34 (eval-when-compile
35 (require 'cl)
36 (require 'ibuf-macs)
37 (require 'dired))
39 ;;; Compatibility
40 (eval-and-compile
41 (if (fboundp 'window-list)
42 (defun ibuffer-window-list ()
43 (window-list nil 'nomini))
44 (defun ibuffer-window-list ()
45 (let ((ibuffer-window-list-result nil))
46 (walk-windows #'(lambda (win) (push win ibuffer-window-list-result)) 'nomini)
47 (nreverse ibuffer-window-list-result))))
49 (cond ((boundp 'global-font-lock-mode)
50 (defsubst ibuffer-use-fontification ()
51 (when (boundp 'font-lock-mode)
52 font-lock-mode)))
53 ((boundp 'font-lock-auto-fontify)
54 (defsubst ibuffer-use-fontification ()
55 font-lock-auto-fontify))
57 (defsubst ibuffer-use-fontification ()
58 nil))))
60 (defgroup ibuffer nil
61 "An advanced replacement for `buffer-menu'.
63 Ibuffer allows you to operate on buffers in a manner much like Dired.
64 Operations include sorting, marking by regular expression, and
65 the ability to filter the displayed buffers by various criteria."
66 :group 'convenience)
68 (defcustom ibuffer-formats '((mark modified read-only " " (name 16 16 :left :elide)
69 " " (size 6 -1 :right)
70 " " (mode 16 16 :right :elide) " " filename)
71 (mark " " (name 16 -1) " " filename))
72 "A list of ways to display buffer lines.
74 With Ibuffer, you are not limited to displaying just certain
75 attributes of a buffer such as size, name, and mode in a particular
76 fashion. Through this variable, you can completely customize and
77 control the appearance of an Ibuffer buffer. See also
78 `define-ibuffer-column', which allows you to define your own columns
79 for display.
81 This variable has the form
82 ((COLUMN COLUMN ...) (COLUMN COLUMN ...) ...)
83 Each element in `ibuffer-formats' should be a list containing COLUMN
84 specifiers. A COLUMN can be any of the following:
86 SYMBOL - A symbol naming the column. Predefined columns are:
87 mark modified read-only name size mode process filename
88 When you define your own columns using `define-ibuffer-column', just
89 use their name like the predefined columns here. This entry can
90 also be a function of two arguments, which should return a string.
91 The first argument is the buffer object, and the second is the mark
92 on that buffer.
94 \"STRING\" - A literal string to display.
96 (SYMBOL MIN-SIZE MAX-SIZE &optional ALIGN ELIDE) - SYMBOL is a
97 symbol naming the column, and MIN-SIZE and MAX-SIZE are integers (or
98 functions of no arguments returning an integer) which constrict the
99 size of a column. If MAX-SIZE is -1, there is no upper bound. The
100 default values are 0 and -1, respectively. If MIN-SIZE is negative,
101 use the end of the string. The optional element ALIGN describes the
102 alignment of the column; it can be :left, :center or :right. The
103 optional element ELIDE describes whether or not to elide the column
104 if it is too long; valid values are :elide and nil. The default is
105 nil (don't elide).
107 Some example of valid entries in `ibuffer-formats', with
108 description (also, feel free to try them out, and experiment with your
109 own!):
111 (mark \" \" name)
112 This format just displays the current mark (if any) and the name of
113 the buffer, separated by a space.
114 (mark modified read-only \" \" (name 16 16 :left) \" \" (size 6 -1 :right))
115 This format displays the current mark (if any), its modification and
116 read-only status, as well as the name of the buffer and its size. In
117 this format, the name is restricted to 16 characters (longer names
118 will be truncated, and shorter names will be padded with spaces), and
119 the name is also aligned to the right. The size of the buffer will
120 be padded with spaces up to a minimum of six characters, but there is
121 no upper limit on its size. The size will also be aligned to the
122 right.
124 Thus, if you wanted to use these two formats, add
126 (setq ibuffer-formats '((mark \" \" name)
127 (mark modified read-only
128 (name 16 16 :left) (size 6 -1 :right))))
130 to your ~/.emacs file.
132 Using \\[ibuffer-switch-format], you can rotate the display between
133 the specified formats in the list."
134 :type '(repeat sexp)
135 :group 'ibuffer)
137 (defcustom ibuffer-always-compile-formats (featurep 'bytecomp)
138 "If non-nil, then use the byte-compiler to optimize `ibuffer-formats'.
139 This will increase the redisplay speed, at the cost of loading the
140 elisp byte-compiler."
141 :type 'boolean
142 :group 'ibuffer)
144 (defcustom ibuffer-fontification-alist
145 `((10 buffer-read-only font-lock-reference-face)
146 (15 (string-match "^*" (buffer-name)) font-lock-keyword-face)
147 (20 (string-match "^ " (buffer-name)) font-lock-warning-face)
148 (25 (memq major-mode ibuffer-help-buffer-modes) font-lock-comment-face)
149 (30 (eq major-mode 'dired-mode) font-lock-function-name-face))
150 "An alist describing how to fontify buffers.
151 Each element should be of the form (PRIORITY FORM FACE), where
152 PRIORITY is an integer, FORM is an arbitrary form to evaluate in the
153 buffer, and FACE is the face to use for fontification. If the FORM
154 evaluates to non-nil, then FACE will be put on the buffer name. The
155 element with the highest PRIORITY takes precedence."
156 :type '(repeat
157 (list (integer :tag "Priority")
158 (sexp :tag "Test Form")
159 face))
160 :group 'ibuffer)
162 (defcustom ibuffer-use-other-window nil
163 "If non-nil, display the Ibuffer in another window by default."
164 :type 'boolean
165 :group 'ibuffer)
167 (defcustom ibuffer-default-shrink-to-minimum-size nil
168 "If non-nil, minimize the size of the Ibuffer window by default."
169 :type 'boolean
170 :group 'ibuffer)
171 (defvar ibuffer-shrink-to-minimum-size nil)
173 (defcustom ibuffer-truncate-lines t
174 "If non-nil, do not display continuation lines."
175 :type 'boolean
176 :group 'ibuffer)
178 (defcustom ibuffer-case-fold-search case-fold-search
179 "If non-nil, ignore case when searching."
180 :type 'boolean
181 :group 'ibuffer)
183 (defcustom ibuffer-default-sorting-mode 'recency
184 "The criteria by which to sort the buffers.
186 Note that this variable is local to each ibuffer buffer. Thus, you
187 can have multiple ibuffer buffers open, each with a different sorted
188 view of the buffers."
189 :type '(choice (const :tag "Last view time" :value recency)
190 (const :tag "Lexicographic" :value alphabetic)
191 (const :tag "Buffer size" :value size)
192 (const :tag "Major mode" :value major-mode))
193 :group 'ibuffer)
194 (defvar ibuffer-sorting-mode nil)
196 (defcustom ibuffer-default-sorting-reversep nil
197 "If non-nil, reverse the default sorting order."
198 :type 'boolean
199 :group 'ibuffer)
200 (defvar ibuffer-sorting-reversep nil)
202 (defcustom ibuffer-elide-long-columns nil
203 "If non-nil, then elide column entries which exceed their max length.
204 This variable is deprecated; use the :elide argument of
205 `ibuffer-formats' to elide just certain columns."
206 :type 'boolean
207 :group 'ibuffer)
209 (defcustom ibuffer-eliding-string "..."
210 "The string to use for eliding long columns."
211 :type 'string
212 :group 'ibuffer)
214 (defcustom ibuffer-maybe-show-predicates `(,(lambda (buf)
215 (and (string-match "^ " (buffer-name buf))
216 (null buffer-file-name))))
217 "A list of predicates (a regexp or function) for buffers to display conditionally.
218 If a regexp, then it will be matched against the buffer's name.
219 If a function, it will be called with the buffer as an argument, and
220 should return non-nil if this buffer should be shown.
222 Viewing of buffers hidden because of these predicates is enabled by
223 giving a non-nil prefix argument to `ibuffer-update'. Note that this
224 specialized filtering occurs before real filtering."
225 :type '(repeat (choice regexp function))
226 :group 'ibuffer)
228 (defvar ibuffer-current-format nil)
230 (defcustom ibuffer-modified-char ?*
231 "The character to display for modified buffers."
232 :type 'character
233 :group 'ibuffer)
235 (defcustom ibuffer-read-only-char ?%
236 "The character to display for read-only buffers."
237 :type 'character
238 :group 'ibuffer)
240 (defcustom ibuffer-marked-char ?>
241 "The character to display for marked buffers."
242 :type 'character
243 :group 'ibuffer)
245 (defcustom ibuffer-deletion-char ?D
246 "The character to display for buffers marked for deletion."
247 :type 'character
248 :group 'ibuffer)
250 (defcustom ibuffer-expert nil
251 "If non-nil, don't ask for confirmation of \"dangerous\" operations."
252 :type 'boolean
253 :group 'ibuffer)
255 (defcustom ibuffer-view-ibuffer nil
256 "If non-nil, display the current Ibuffer buffer itself.
257 Note that this has a drawback - the data about the current Ibuffer
258 buffer will most likely be inaccurate. This includes modification
259 state, size, etc."
260 :type 'boolean
261 :group 'ibuffer)
263 (defcustom ibuffer-always-show-last-buffer nil
264 "If non-nil, always display the previous buffer. This variable
265 takes precedence over filtering, and even
266 `ibuffer-never-show-predicates'."
267 :type '(choice (const :tag "Always" :value t)
268 (const :tag "Never" :value nil)
269 (const :tag "Always except minibuffer" :value :nomini))
270 :group 'ibuffer)
272 (defcustom ibuffer-use-header-line (boundp 'header-line-format)
273 "If non-nil, display a header line containing current filters.
274 This feature only works on Emacs 21 or later."
275 :type 'boolean
276 :group 'ibuffer)
278 (defcustom ibuffer-default-directory nil
279 "The default directory to use for a new ibuffer buffer.
280 If nil, inherit the directory of the buffer in which `ibuffer' was
281 called. Otherwise, this variable should be a string naming a
282 directory, like `default-directory'."
283 :type '(choice (const :tag "Inherit" :value nil)
284 string)
285 :group 'ibuffer)
287 (defcustom ibuffer-help-buffer-modes '(help-mode apropos-mode
288 Info-mode Info-edit-mode)
289 "List of \"Help\" major modes."
290 :type '(repeat function)
291 :group 'ibuffer)
293 (defcustom ibuffer-hooks nil
294 "Hooks run when `ibuffer' is called."
295 :type 'hook
296 :group 'ibuffer)
298 (defcustom ibuffer-mode-hooks nil
299 "Hooks run upon entry into `ibuffer-mode'."
300 :type 'hook
301 :group 'ibuffer)
303 (defcustom ibuffer-marked-face 'font-lock-warning-face
304 "Face used for displaying marked buffers."
305 :type 'face
306 :group 'ibuffer)
308 (defcustom ibuffer-deletion-face 'font-lock-type-face
309 "Face used for displaying buffers marked for deletion."
310 :type 'face
311 :group 'ibuffer)
313 (defcustom ibuffer-title-face 'font-lock-type-face
314 "Face used for the title string."
315 :type 'face
316 :group 'ibuffer)
318 (defcustom ibuffer-directory-abbrev-alist nil
319 "An alist of file name abbreviations like `directory-abbrev-alist'."
320 :type '(repeat (cons :format "%v"
321 :value ("" . "")
322 (regexp :tag "From")
323 (regexp :tag "To")))
324 :group 'ibuffer)
326 (defvar ibuffer-mode-map nil)
327 (defvar ibuffer-mode-operate-map nil)
328 (unless ibuffer-mode-map
329 (let ((map (make-sparse-keymap))
330 (operate-map (make-sparse-keymap "Operate")))
331 (define-key map (kbd "0") 'digit-argument)
332 (define-key map (kbd "1") 'digit-argument)
333 (define-key map (kbd "2") 'digit-argument)
334 (define-key map (kbd "3") 'digit-argument)
335 (define-key map (kbd "4") 'digit-argument)
336 (define-key map (kbd "5") 'digit-argument)
337 (define-key map (kbd "6") 'digit-argument)
338 (define-key map (kbd "7") 'digit-argument)
339 (define-key map (kbd "8") 'digit-argument)
340 (define-key map (kbd "9") 'digit-argument)
342 (define-key map (kbd "m") 'ibuffer-mark-forward)
343 (define-key map (kbd "t") 'ibuffer-toggle-marks)
344 (define-key map (kbd "u") 'ibuffer-unmark-forward)
345 (define-key map (kbd "=") 'ibuffer-diff-with-file)
346 (define-key map (kbd "j") 'ibuffer-jump-to-buffer)
347 (define-key map (kbd "DEL") 'ibuffer-unmark-backward)
348 (define-key map (kbd "M-DEL") 'ibuffer-unmark-all)
349 (define-key map (kbd "* *") 'ibuffer-unmark-all)
350 (define-key map (kbd "* M") 'ibuffer-mark-by-mode)
351 (define-key map (kbd "* m") 'ibuffer-mark-modified-buffers)
352 (define-key map (kbd "* u") 'ibuffer-mark-unsaved-buffers)
353 (define-key map (kbd "* s") 'ibuffer-mark-special-buffers)
354 (define-key map (kbd "* r") 'ibuffer-mark-read-only-buffers)
355 (define-key map (kbd "* /") 'ibuffer-mark-dired-buffers)
356 (define-key map (kbd "* e") 'ibuffer-mark-dissociated-buffers)
357 (define-key map (kbd "* h") 'ibuffer-mark-help-buffers)
358 (define-key map (kbd ".") 'ibuffer-mark-old-buffers)
360 (define-key map (kbd "d") 'ibuffer-mark-for-delete)
361 (define-key map (kbd "C-d") 'ibuffer-mark-for-delete-backwards)
362 (define-key map (kbd "k") 'ibuffer-mark-for-delete)
363 (define-key map (kbd "x") 'ibuffer-do-kill-on-deletion-marks)
365 ;; immediate operations
366 (define-key map (kbd "n") 'ibuffer-forward-line)
367 (define-key map (kbd "SPC") 'forward-line)
368 (define-key map (kbd "p") 'ibuffer-backward-line)
369 (define-key map (kbd "M-}") 'ibuffer-forward-next-marked)
370 (define-key map (kbd "M-{") 'ibuffer-backwards-next-marked)
371 (define-key map (kbd "l") 'ibuffer-redisplay)
372 (define-key map (kbd "g") 'ibuffer-update)
373 (define-key map "`" 'ibuffer-switch-format)
374 (define-key map "-" 'ibuffer-add-to-tmp-hide)
375 (define-key map "+" 'ibuffer-add-to-tmp-show)
376 (define-key map "b" 'ibuffer-bury-buffer)
377 (define-key map (kbd ",") 'ibuffer-toggle-sorting-mode)
378 (define-key map (kbd "s i") 'ibuffer-invert-sorting)
379 (define-key map (kbd "s a") 'ibuffer-do-sort-by-alphabetic)
380 (define-key map (kbd "s v") 'ibuffer-do-sort-by-recency)
381 (define-key map (kbd "s s") 'ibuffer-do-sort-by-size)
382 (define-key map (kbd "s m") 'ibuffer-do-sort-by-major-mode)
384 (define-key map (kbd "/ m") 'ibuffer-filter-by-mode)
385 (define-key map (kbd "/ n") 'ibuffer-filter-by-name)
386 (define-key map (kbd "/ c") 'ibuffer-filter-by-content)
387 (define-key map (kbd "/ e") 'ibuffer-filter-by-predicate)
388 (define-key map (kbd "/ f") 'ibuffer-filter-by-filename)
389 (define-key map (kbd "/ >") 'ibuffer-filter-by-size-gt)
390 (define-key map (kbd "/ <") 'ibuffer-filter-by-size-lt)
391 (define-key map (kbd "/ r") 'ibuffer-switch-to-saved-filters)
392 (define-key map (kbd "/ a") 'ibuffer-add-saved-filters)
393 (define-key map (kbd "/ x") 'ibuffer-delete-saved-filters)
394 (define-key map (kbd "/ d") 'ibuffer-decompose-filter)
395 (define-key map (kbd "/ s") 'ibuffer-save-filters)
396 (define-key map (kbd "/ p") 'ibuffer-pop-filter)
397 (define-key map (kbd "/ !") 'ibuffer-negate-filter)
398 (define-key map (kbd "/ t") 'ibuffer-exchange-filters)
399 (define-key map (kbd "/ TAB") 'ibuffer-exchange-filters)
400 (define-key map (kbd "/ o") 'ibuffer-or-filter)
401 (define-key map (kbd "/ /") 'ibuffer-filter-disable)
403 (define-key map (kbd "q") 'ibuffer-quit)
404 (define-key map (kbd "h") 'describe-mode)
405 (define-key map (kbd "?") 'describe-mode)
407 (define-key map (kbd "% n") 'ibuffer-mark-by-name-regexp)
408 (define-key map (kbd "% m") 'ibuffer-mark-by-mode-regexp)
409 (define-key map (kbd "% f") 'ibuffer-mark-by-file-name-regexp)
411 (define-key map (kbd "C-t") 'ibuffer-visit-tags-table)
413 (define-key map (kbd "|") 'ibuffer-do-shell-command-pipe)
414 (define-key map (kbd "!") 'ibuffer-do-shell-command-file)
415 (define-key map (kbd "~") 'ibuffer-do-toggle-modified)
416 ;; marked operations
417 (define-key map (kbd "A") 'ibuffer-do-view)
418 (define-key map (kbd "D") 'ibuffer-do-delete)
419 (define-key map (kbd "E") 'ibuffer-do-eval)
420 (define-key map (kbd "F") 'ibuffer-do-shell-command-file)
421 (define-key map (kbd "I") 'ibuffer-do-query-replace-regexp)
422 (define-key map (kbd "H") 'ibuffer-do-view-other-frame)
423 (define-key map (kbd "N") 'ibuffer-do-shell-command-pipe-replace)
424 (define-key map (kbd "M") 'ibuffer-do-toggle-modified)
425 (define-key map (kbd "O") 'ibuffer-do-occur)
426 (define-key map (kbd "P") 'ibuffer-do-print)
427 (define-key map (kbd "Q") 'ibuffer-do-query-replace)
428 (define-key map (kbd "R") 'ibuffer-do-rename-uniquely)
429 (define-key map (kbd "S") 'ibuffer-do-save)
430 (define-key map (kbd "T") 'ibuffer-do-toggle-read-only)
431 (define-key map (kbd "U") 'ibuffer-do-replace-regexp)
432 (define-key map (kbd "V") 'ibuffer-do-revert)
433 (define-key map (kbd "W") 'ibuffer-do-view-and-eval)
434 (define-key map (kbd "X") 'ibuffer-do-shell-command-pipe)
436 (define-key map (kbd "k") 'ibuffer-do-kill-lines)
437 (define-key map (kbd "w") 'ibuffer-copy-filename-as-kill)
439 (define-key map (kbd "RET") 'ibuffer-visit-buffer)
440 (define-key map (kbd "e") 'ibuffer-visit-buffer)
441 (define-key map (kbd "f") 'ibuffer-visit-buffer)
442 (define-key map (kbd "C-x C-f") 'ibuffer-find-file)
443 (define-key map (kbd "o") 'ibuffer-visit-buffer-other-window)
444 (define-key map (kbd "C-o") 'ibuffer-visit-buffer-other-window-noselect)
445 (define-key map (kbd "M-o") 'ibuffer-visit-buffer-1-window)
446 (define-key map (kbd "v") 'ibuffer-do-view)
447 (define-key map (kbd "C-x v") 'ibuffer-do-view-horizontally)
448 (define-key map (kbd "C-c C-a") 'ibuffer-auto-mode)
449 (define-key map (kbd "C-x 4 RET") 'ibuffer-visit-buffer-other-window)
450 (define-key map (kbd "C-x 5 RET") 'ibuffer-visit-buffer-other-frame)
452 (define-key map [menu-bar view]
453 (cons "View" (make-sparse-keymap "View")))
455 (define-key-after map [menu-bar view visit-buffer]
456 '(menu-item "View this buffer" ibuffer-visit-buffer))
457 (define-key-after map [menu-bar view visit-buffer-other-window]
458 '(menu-item "View (other window)" ibuffer-visit-buffer-other-window))
459 (define-key-after map [menu-bar view visit-buffer-other-frame]
460 '(menu-item "View (other frame)" ibuffer-visit-buffer-other-frame))
461 (define-key-after map [menu-bar view ibuffer-update]
462 '(menu-item "Update" ibuffer-update
463 :help "Regenerate the list of buffers"))
464 (define-key-after map [menu-bar view switch-format]
465 '(menu-item "Switch display format" ibuffer-switch-format
466 :help "Toggle between available values of `ibuffer-formats'"))
468 (define-key-after map [menu-bar view dashes]
469 '("--"))
471 (define-key-after map [menu-bar view sort]
472 (cons "Sort" (make-sparse-keymap "Sort")))
474 (define-key-after map [menu-bar view sort do-sort-by-major-mode]
475 '(menu-item "Sort by major mode" ibuffer-do-sort-by-major-mode
476 :help "Sort by the alphabetic order of the buffer's major mode"))
477 (define-key-after map [menu-bar view sort do-sort-by-size]
478 '(menu-item "Sort by buffer size" ibuffer-do-sort-by-size
479 :help "Sort by the size of the buffer"))
480 (define-key-after map [menu-bar view sort do-sort-by-alphabetic]
481 '(menu-item "Sort lexicographically" ibuffer-do-sort-by-alphabetic
482 :help "Sort by the alphabetic order of buffer name"))
483 (define-key-after map [menu-bar view sort do-sort-by-recency]
484 '(menu-item "Sort by view time" ibuffer-do-sort-by-recency
485 :help "Sort by the last time the buffer was displayed"))
486 (define-key-after map [menu-bar view sort invert-sorting]
487 '(menu-item "Reverse sorting order" ibuffer-invert-sorting))
488 (define-key-after map [menu-bar view sort toggle-sorting-mode]
489 '(menu-item "Switch sorting mode" ibuffer-toggle-sorting-mode
490 :help "Switch between the various sorting criteria"))
492 (define-key-after map [menu-bar view filter]
493 (cons "Filter" (make-sparse-keymap "Filter")))
495 (define-key-after map [menu-bar view filter filter-disable]
496 '(menu-item "Disable all filtering" ibuffer-filter-disable))
497 (define-key-after map [menu-bar view filter filter-by-mode]
498 '(menu-item "Add filter by major mode..." ibuffer-filter-by-mode
499 :help "Show only buffers in a major mode"))
500 (define-key-after map [menu-bar view filter filter-by-name]
501 '(menu-item "Add filter by buffer name..." ibuffer-filter-by-name
502 :help "Show only buffers whose name matches a regexp"))
503 (define-key-after map [menu-bar view filter filter-by-filename]
504 '(menu-item "Add filter by filename..." ibuffer-filter-by-filename
505 :help "Show only buffers whose filename matches a regexp"))
506 (define-key-after map [menu-bar view filter filter-by-size-lt]
507 '(menu-item "Add filter by size less than..." ibuffer-filter-by-size-lt
508 :help "Show only buffers of size less than..."))
509 (define-key-after map [menu-bar view filter filter-by-size-gt]
510 '(menu-item "Add filter by size greater than..." ibuffer-filter-by-size-gt
511 :help "Show only buffers of size greater than..."))
512 (define-key-after map [menu-bar view filter filter-by-content]
513 '(menu-item "Add filter by content (regexp)..." ibuffer-filter-by-content
514 :help "Show only buffers containing a regexp"))
515 (define-key-after map [menu-bar view filter filter-by-predicate]
516 '(menu-item "Add filter by Lisp predicate..." ibuffer-filter-by-predicate
517 :help "Show only buffers for which a predicate is true"))
518 (define-key-after map [menu-bar view filter pop-filter]
519 '(menu-item "Remove top filter" ibuffer-pop-filter))
520 (define-key-after map [menu-bar view filter or-filter]
521 '(menu-item "OR top two filters" ibuffer-or-filter
522 :help "Create a new filter which is the logical OR of the top two filters"))
523 (define-key-after map [menu-bar view filter negate-filter]
524 '(menu-item "Negate top filter" ibuffer-negate-filter))
525 (define-key-after map [menu-bar view filter decompose-filter]
526 '(menu-item "Decompose top filter" ibuffer-decompose-filter
527 :help "Break down a complex filter like OR or NOT"))
528 (define-key-after map [menu-bar view filter exchange-filters]
529 '(menu-item "Swap top two filters" ibuffer-exchange-filters))
530 (define-key-after map [menu-bar view filter save-filters]
531 '(menu-item "Save current filters permanently..." ibuffer-save-filters
532 :help "Use a mnemnonic name to store current filter stack"))
533 (define-key-after map [menu-bar view filter switch-to-saved-filters]
534 '(menu-item "Restore permanently saved filters..." ibuffer-switch-to-saved-filters
535 :help "Replace current filters with a saved stack"))
536 (define-key-after map [menu-bar view filter add-saved-filters]
537 '(menu-item "Add to permanently saved filters..." ibuffer-add-saved-filters
538 :help "Include current filters in an already saved stack"))
539 (define-key-after map [menu-bar view filter delete-saved-filters]
540 '(menu-item "Delete permanently saved filters..." ibuffer-delete-saved-filters
541 :help "Remove stack of filters from saved list"))
542 (define-key-after map [menu-bar view dashes2]
543 '("--"))
544 (define-key-after map [menu-bar view diff-with-file]
545 '(menu-item "Diff with file" ibuffer-diff-with-file
546 :help "View the differences between this buffer and its file"))
547 (define-key-after map [menu-bar view auto-mode]
548 '(menu-item "Toggle Auto Mode" ibuffer-auto-mode
549 :help "Attempt to automatically update the Ibuffer buffer"))
550 (define-key-after map [menu-bar view customize]
551 '(menu-item "Customize Ibuffer" (lambda () (interactive)
552 (customize-group 'ibuffer))
553 :help "Use Custom to customize Ibuffer"))
555 (define-key-after map [menu-bar mark]
556 (cons "Mark" (make-sparse-keymap "Mark")))
558 (define-key-after map [menu-bar mark toggle-marks]
559 '(menu-item "Toggle marks" ibuffer-toggle-marks
560 :help "Unmark marked buffers, and mark unmarked buffers"))
561 (define-key-after map [menu-bar mark mark-forward]
562 '(menu-item "Mark" ibuffer-mark-forward
563 :help "Mark the buffer at point"))
564 (define-key-after map [menu-bar mark unmark-forward]
565 '(menu-item "Unmark" ibuffer-unmark-forward
566 :help "Unmark the buffer at point"))
567 (define-key-after map [menu-bar mark mark-by-mode]
568 '(menu-item "Mark by mode..." ibuffer-mark-by-mode
569 :help "Mark all buffers in a particular major mode"))
570 (define-key-after map [menu-bar mark mark-modified-buffers]
571 '(menu-item "Mark modified buffers" ibuffer-mark-modified-buffers
572 :help "Mark all buffers which have been modified"))
573 (define-key-after map [menu-bar mark mark-unsaved-buffers]
574 '(menu-item "Mark unsaved buffers" ibuffer-mark-unsaved-buffers
575 :help "Mark all buffers which have a file and are modified"))
576 (define-key-after map [menu-bar mark mark-read-only-buffers]
577 '(menu-item "Mark read-only buffers" ibuffer-mark-read-only-buffers
578 :help "Mark all buffers which are read-only"))
579 (define-key-after map [menu-bar mark mark-special-buffers]
580 '(menu-item "Mark special buffers" ibuffer-mark-special-buffers
581 :help "Mark all buffers whose name begins with a *"))
582 (define-key-after map [menu-bar mark mark-dired-buffers]
583 '(menu-item "Mark dired buffers" ibuffer-mark-dired-buffers
584 :help "Mark buffers in dired-mode"))
585 (define-key-after map [menu-bar mark mark-dissociated-buffers]
586 '(menu-item "Mark dissociated buffers" ibuffer-mark-dissociated-buffers
587 :help "Mark buffers with a non-existent associated file"))
588 (define-key-after map [menu-bar mark mark-help-buffers]
589 '(menu-item "Mark help buffers" ibuffer-mark-help-buffers
590 :help "Mark buffers in help-mode"))
591 (define-key-after map [menu-bar mark mark-old-buffers]
592 '(menu-item "Mark old buffers" ibuffer-mark-old-buffers
593 :help "Mark buffers which have not been viewed recently"))
594 (define-key-after map [menu-bar mark unmark-all]
595 '(menu-item "Unmark All" ibuffer-unmark-all))
597 (define-key-after map [menu-bar mark dashes]
598 '("--"))
600 (define-key-after map [menu-bar mark mark-by-name-regexp]
601 '(menu-item "Mark by buffer name (regexp)..." ibuffer-mark-by-name-regexp
602 :help "Mark buffers whose name matches a regexp"))
603 (define-key-after map [menu-bar mark mark-by-mode-regexp]
604 '(menu-item "Mark by major mode (regexp)..." ibuffer-mark-by-mode-regexp
605 :help "Mark buffers whose major mode name matches a regexp"))
606 (define-key-after map [menu-bar mark mark-by-file-name-regexp]
607 '(menu-item "Mark by file name (regexp)..." ibuffer-mark-by-file-name-regexp
608 :help "Mark buffers whose file name matches a regexp"))
610 ;; Operate map is added later
612 (define-key-after operate-map [do-view]
613 '(menu-item "View" ibuffer-do-view))
614 (define-key-after operate-map [do-view-other-frame]
615 '(menu-item "View (separate frame)" ibuffer-do-view-other-frame))
616 (define-key-after operate-map [do-save]
617 '(menu-item "Save" ibuffer-do-save))
618 (define-key-after operate-map [do-replace-regexp]
619 '(menu-item "Replace (regexp)..." ibuffer-do-replace-regexp
620 :help "Replace text inside marked buffers"))
621 (define-key-after operate-map [do-query-replace]
622 '(menu-item "Query Replace..." ibuffer-do-query-replace
623 :help "Replace text in marked buffers, asking each time"))
624 (define-key-after operate-map [do-query-replace-regexp]
625 '(menu-item "Query Replace (regexp)..." ibuffer-do-query-replace-regexp
626 :help "Replace text in marked buffers by regexp, asking each time"))
627 (define-key-after operate-map [do-print]
628 '(menu-item "Print" ibuffer-do-print))
629 (define-key-after operate-map [do-toggle-modified]
630 '(menu-item "Toggle modification flag" ibuffer-do-toggle-modified))
631 (define-key-after operate-map [do-revert]
632 '(menu-item "Revert" ibuffer-do-revert
633 :help "Revert marked buffers to their associated file"))
634 (define-key-after operate-map [do-rename-uniquely]
635 '(menu-item "Rename Uniquely" ibuffer-do-rename-uniquely
636 :help "Rename marked buffers to a new, unique name"))
637 (define-key-after operate-map [do-delete]
638 '(menu-item "Kill" ibuffer-do-delete))
639 (define-key-after operate-map [do-occur]
640 '(menu-item "List lines matching..." ibuffer-do-occur
641 :help "View all lines in marked buffers matching a regexp"))
642 (define-key-after operate-map [do-shell-command-pipe]
643 '(menu-item "Pipe to shell command..." ibuffer-do-shell-command-pipe
644 :help "For each marked buffer, send its contents to a shell command"))
645 (define-key-after operate-map [do-shell-command-pipe-replace]
646 '(menu-item "Pipe to shell command (replace)..." ibuffer-do-shell-command-pipe-replace
647 :help "For each marked buffer, replace its contents with output of shell command"))
648 (define-key-after operate-map [do-shell-command-file]
649 '(menu-item "Shell command on buffer's file..." ibuffer-do-shell-command-file
650 :help "For each marked buffer, run a shell command with its file as argument"))
651 (define-key-after operate-map [do-eval]
652 '(menu-item "Eval..." ibuffer-do-eval
653 :help "Evaluate a Lisp form in each marked buffer"))
654 (define-key-after operate-map [do-view-and-eval]
655 '(menu-item "Eval (viewing buffer)..." ibuffer-do-view-and-eval
656 :help "Evaluate a Lisp form in each marked buffer while viewing it"))
658 (setq ibuffer-mode-map map
659 ibuffer-mode-operate-map operate-map)))
661 (defvar ibuffer-name-map nil)
662 (unless ibuffer-name-map
663 (let ((map (make-sparse-keymap)))
664 (set-keymap-parent map ibuffer-mode-map)
665 (define-key map [(mouse-1)] 'ibuffer-mouse-toggle-mark)
666 (define-key map [(mouse-2)] 'ibuffer-mouse-visit-buffer)
667 (define-key map [down-mouse-3] 'ibuffer-mouse-popup-menu)
668 (setq ibuffer-name-map map)))
670 (defvar ibuffer-mode-name-map nil)
671 (unless ibuffer-mode-name-map
672 (let ((map (make-sparse-keymap)))
673 (set-keymap-parent map ibuffer-mode-map)
674 (define-key map [(mouse-2)] 'ibuffer-mouse-filter-by-mode)
675 (define-key map (kbd "RET") 'ibuffer-interactive-filter-by-mode)
676 (setq ibuffer-mode-name-map map)))
678 ;; quiet the byte-compiler
679 (defvar ibuffer-mode-operate-menu nil)
680 (defvar ibuffer-mode-mark-menu nil)
681 (defvar ibuffer-mode-view-menu nil)
683 (defvar ibuffer-mode-hooks nil)
685 (defvar ibuffer-delete-window-on-quit nil
686 "Whether or not to delete the window upon exiting `ibuffer'.")
688 (defvar ibuffer-did-modification nil)
690 (defvar ibuffer-sorting-functions-alist nil
691 "An alist of functions which describe how to sort buffers.
693 Note: You most likely do not want to modify this variable directly;
694 use `define-ibuffer-sorter' instead.
696 The alist elements are constructed like (NAME DESCRIPTION FUNCTION)
697 Where NAME is a symbol describing the sorting method, DESCRIPTION is a
698 short string which will be displayed in the minibuffer and menu, and
699 FUNCTION is a function of two arguments, which will be the buffers to
700 compare.")
702 ;;; Utility functions
703 (defun ibuffer-columnize-and-insert-list (list &optional pad-width)
704 "Insert LIST into the current buffer in as many columns as possible.
705 The maximum number of columns is determined by the current window
706 width and the longest string in LIST."
707 (unless pad-width
708 (setq pad-width 3))
709 (let ((width (window-width))
710 (max (+ (apply #'max (mapcar #'length list))
711 pad-width)))
712 (let ((columns (/ width max)))
713 (when (zerop columns)
714 (setq columns 1))
715 (while list
716 (dotimes (i (1- columns))
717 (insert (concat (car list) (make-string (- max (length (car list)))
718 ? )))
719 (setq list (cdr list)))
720 (when (not (null list))
721 (insert (pop list)))
722 (insert "\n")))))
724 (defsubst ibuffer-current-mark ()
725 (cadr (get-text-property (line-beginning-position)
726 'ibuffer-properties)))
728 (defun ibuffer-mouse-toggle-mark (event)
729 "Toggle the marked status of the buffer chosen with the mouse."
730 (interactive "e")
731 (unwind-protect
732 (save-excursion
733 (mouse-set-point event)
734 (let ((mark (ibuffer-current-mark)))
735 (setq buffer-read-only nil)
736 (if (eq mark ibuffer-marked-char)
737 (ibuffer-set-mark ? )
738 (ibuffer-set-mark ibuffer-marked-char))))
739 (setq buffer-read-only t)))
741 (defun ibuffer-find-file (file &optional wildcards)
742 "Like `find-file', but default to the directory of the buffer at point."
743 (interactive
744 (let ((default-directory (let ((buf (ibuffer-current-buffer)))
745 (if (buffer-live-p buf)
746 (with-current-buffer buf
747 default-directory)
748 default-directory))))
749 (list (read-file-name "Find file: " default-directory)
750 current-prefix-arg)))
751 (find-file file wildcards))
753 (defun ibuffer-mouse-visit-buffer (event)
754 "Visit the buffer chosen with the mouse."
755 (interactive "e")
756 (switch-to-buffer
757 (save-excursion
758 (mouse-set-point event)
759 (ibuffer-current-buffer))))
761 (defun ibuffer-mouse-popup-menu (event)
762 "Display a menu of operations."
763 (interactive "e")
764 (let ((origline (count-lines (point-min) (point))))
765 (unwind-protect
766 (progn
767 (setq buffer-read-only nil)
768 (ibuffer-save-marks
769 ;; hm. we could probably do this in a better fashion
770 (ibuffer-unmark-all ?\r)
771 (setq buffer-read-only nil)
772 (mouse-set-point event)
773 (ibuffer-set-mark ibuffer-marked-char)
774 (setq buffer-read-only nil)
775 (save-excursion
776 (popup-menu ibuffer-mode-operate-map))))
777 (progn
778 (setq buffer-read-only t)
779 (goto-line (1+ origline))))))
781 (defun ibuffer-backward-line (&optional arg)
782 "Move backwards ARG lines, wrapping around the list if necessary."
783 (interactive "P")
784 (unless arg
785 (setq arg 1))
786 (beginning-of-line)
787 (while (> arg 0)
788 (forward-line -1)
789 (when (get-text-property (point) 'ibuffer-title)
790 (goto-char (point-max))
791 (beginning-of-line))
792 (while (get-text-property (point) 'ibuffer-summary)
793 (forward-line -1)
794 (beginning-of-line))
795 ;; Handle the special case of no buffers.
796 (when (get-text-property (point) 'ibuffer-title)
797 (forward-line 1)
798 (setq arg 1))
799 (decf arg)))
801 (defun ibuffer-forward-line (&optional arg)
802 "Move forward ARG lines, wrapping around the list if necessary."
803 (interactive "P")
804 (unless arg
805 (setq arg 1))
806 (beginning-of-line)
807 (when (or (eobp)
808 (get-text-property (point) 'ibuffer-summary))
809 (goto-char (point-min)))
810 (when (get-text-property (point) 'ibuffer-title)
811 (if (> arg 0)
812 (decf arg))
813 (while (get-text-property (point) 'ibuffer-title)
814 (forward-line 1)))
815 (if (< arg 0)
816 (ibuffer-backward-line (- arg))
817 (while (> arg 0)
818 (forward-line 1)
819 (when (or (eobp)
820 (get-text-property (point) 'ibuffer-summary))
821 (goto-char (point-min)))
822 (while (get-text-property (point) 'ibuffer-title)
823 (forward-line 1))
824 (decf arg))))
826 (defun ibuffer-visit-buffer (&optional single)
827 "Visit the buffer on this line.
829 If optional argument SINGLE is non-nil, then also ensure there is only
830 one window."
831 (interactive "P")
832 (let ((buf (ibuffer-current-buffer)))
833 (if (bufferp buf)
834 (unless (buffer-live-p buf)
835 (error "Buffer %s has been killed!" buf))
836 (error "No buffer on this line"))
837 (bury-buffer (current-buffer))
838 (switch-to-buffer buf)
839 (when single
840 (delete-other-windows))))
842 (defun ibuffer-visit-buffer-other-window (&optional noselect)
843 "Visit the buffer on this line in another window."
844 (interactive)
845 (let ((buf (ibuffer-current-buffer)))
846 (unless (buffer-live-p buf)
847 (error "Buffer %s has been killed!" buf))
848 (bury-buffer (current-buffer))
849 (if noselect
850 (let ((curwin (selected-window)))
851 (pop-to-buffer buf)
852 (select-window curwin))
853 (switch-to-buffer-other-window buf))))
855 (defun ibuffer-visit-buffer-other-window-noselect ()
856 "Visit the buffer on this line in another window, but don't select it."
857 (interactive)
858 (ibuffer-visit-buffer-other-window t))
860 (defun ibuffer-visit-buffer-other-frame ()
861 "Visit the buffer on this line in another frame."
862 (interactive)
863 (let ((buf (ibuffer-current-buffer)))
864 (unless (buffer-live-p buf)
865 (error "Buffer %s has been killed!" buf))
866 (bury-buffer (current-buffer))
867 (switch-to-buffer-other-frame buf)))
869 (defun ibuffer-visit-buffer-1-window ()
870 "Visit the buffer on this line, and delete other windows."
871 (interactive)
872 (ibuffer-visit-buffer t))
874 (defun ibuffer-bury-buffer ()
875 "Bury the buffer on this line."
876 (interactive)
877 (let ((buf (ibuffer-current-buffer))
878 (line (+ 1 (count-lines 1 (point)))))
879 (unless (buffer-live-p buf)
880 (error "Buffer %s has been killed!" buf))
881 (bury-buffer buf)
882 (ibuffer-update nil t)
883 (goto-line line)))
885 (defun ibuffer-visit-tags-table ()
886 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
887 (interactive)
888 (let ((file (buffer-file-name (ibuffer-current-buffer))))
889 (if file
890 (visit-tags-table file)
891 (error "Specified buffer has no file"))))
893 (defun ibuffer-do-view (&optional other-frame)
894 "View marked buffers, or the buffer on the current line.
895 If optional argument OTHER-FRAME is non-nil, then display each
896 marked buffer in a new frame. Otherwise, display each buffer as
897 a new window in the current frame, splitting vertically."
898 (interactive)
899 (ibuffer-do-view-1 (if other-frame 'other-frame 'vertically)))
901 (defun ibuffer-do-view-horizontally (&optional other-frame)
902 "As `ibuffer-do-view', but split windows horizontally."
903 (interactive)
904 (ibuffer-do-view-1 (if other-frame 'other-frame 'horizontally)))
906 (defun ibuffer-do-view-1 (type)
907 (let ((marked-bufs (ibuffer-get-marked-buffers)))
908 (when (null marked-bufs)
909 (setq marked-bufs (list (ibuffer-current-buffer))))
910 (unless (and (eq type 'other-frame)
911 (not ibuffer-expert)
912 (> (length marked-bufs) 3)
913 (not (y-or-n-p (format "Really create a new frame for %s buffers? "
914 (length marked-bufs)))))
915 (set-buffer-modified-p nil)
916 (delete-other-windows)
917 (switch-to-buffer (pop marked-bufs))
918 (let ((height (/ (1- (if (eq type 'horizontally) (frame-width)
919 (frame-height)))
920 (1+ (length marked-bufs)))))
921 (mapcar (if (eq type 'other-frame)
922 #'(lambda (buf)
923 (let ((curframe (selected-frame)))
924 (select-frame (new-frame))
925 (switch-to-buffer buf)
926 (select-frame curframe)))
927 #'(lambda (buf)
928 (split-window nil height (eq type 'horizontally))
929 (other-window 1)
930 (switch-to-buffer buf)))
931 marked-bufs)))))
933 (defun ibuffer-do-view-other-frame ()
934 "View each of the marked buffers in a separate frame."
935 (interactive)
936 (ibuffer-do-view t))
938 (defsubst ibuffer-map-marked-lines (func)
939 (prog1 (ibuffer-map-on-mark ibuffer-marked-char func)
940 (ibuffer-redisplay t)))
942 (defun ibuffer-shrink-to-fit (&optional owin)
943 (fit-window-to-buffer nil (when owin (/ (frame-height)
944 (length (window-list (selected-frame)))))))
946 (defun ibuffer-confirm-operation-on (operation names)
947 "Display a buffer asking whether to perform OPERATION on NAMES."
948 (or ibuffer-expert
949 (if (= (length names) 1)
950 (y-or-n-p (format "Really %s buffer %s? " operation (car names)))
951 (let ((buf (get-buffer-create "*Ibuffer confirmation*")))
952 (with-current-buffer buf
953 (setq buffer-read-only nil)
954 (erase-buffer)
955 (ibuffer-columnize-and-insert-list names)
956 (goto-char (point-min))
957 (setq buffer-read-only t))
958 (let ((lastwin (car (last (ibuffer-window-list)))))
959 ;; Now attempt to display the buffer...
960 (save-window-excursion
961 (select-window lastwin)
962 ;; The window might be too small to split; in that case,
963 ;; try a few times to increase its size before giving up.
964 (let ((attempts 0)
965 (trying t))
966 (while trying
967 (condition-case err
968 (progn
969 (split-window)
970 (setq trying nil))
971 (error
972 ;; Handle a failure
973 (if (or (> (incf attempts) 4)
974 (and (stringp (cadr err))
975 ;; This definitely falls in the ghetto hack category...
976 (not (string-match "too small" (cadr err)))))
977 (apply #'signal err)
978 (enlarge-window 3))))))
979 ;; This part doesn't work correctly sometimes under XEmacs.
980 (select-window (next-window))
981 (switch-to-buffer buf)
982 (unwind-protect
983 (progn
984 (fit-window-to-buffer)
985 (y-or-n-p (format "Really %s %d buffers? "
986 operation (length names))))
987 (kill-buffer buf))))))))
989 (defsubst ibuffer-map-lines-nomodify (function)
990 "As `ibuffer-map-lines', but don't set the modification flag."
991 (ibuffer-map-lines function t))
993 (defun ibuffer-buffer-names-with-mark (mark)
994 (let ((ibuffer-buffer-names-with-mark-result nil))
995 (ibuffer-map-lines-nomodify
996 #'(lambda (buf mk)
997 (when (char-equal mark mk)
998 (push (buffer-name buf)
999 ibuffer-buffer-names-with-mark-result))))
1000 ibuffer-buffer-names-with-mark-result))
1002 (defsubst ibuffer-marked-buffer-names ()
1003 (ibuffer-buffer-names-with-mark ibuffer-marked-char))
1005 (defsubst ibuffer-deletion-marked-buffer-names ()
1006 (ibuffer-buffer-names-with-mark ibuffer-deletion-char))
1008 (defun ibuffer-count-marked-lines (&optional all)
1009 (if all
1010 (ibuffer-map-lines-nomodify
1011 #'(lambda (buf mark)
1012 (not (char-equal mark ? ))))
1013 (ibuffer-map-lines-nomodify
1014 #'(lambda (buf mark)
1015 (char-equal mark ibuffer-marked-char)))))
1017 (defsubst ibuffer-count-deletion-lines ()
1018 (ibuffer-map-lines-nomodify
1019 #'(lambda (buf mark)
1020 (char-equal mark ibuffer-deletion-char))))
1022 (defsubst ibuffer-map-deletion-lines (func)
1023 (ibuffer-map-on-mark ibuffer-deletion-char func))
1025 (define-ibuffer-op save ()
1026 "Save marked buffers as with `save-buffer'."
1027 (:complex t
1028 :opstring "saved"
1029 :modifier-p :maybe)
1030 (when (buffer-modified-p buf)
1031 (if (not (with-current-buffer buf
1032 buffer-file-name))
1033 ;; handle the case where we're prompted
1034 ;; for a file name
1035 (save-window-excursion
1036 (switch-to-buffer buf)
1037 (save-buffer))
1038 (with-current-buffer buf
1039 (save-buffer))))
1042 (define-ibuffer-op toggle-modified ()
1043 "Toggle modification flag of marked buffers."
1044 (:opstring "(un)marked as modified"
1045 :modifier-p t)
1046 (set-buffer-modified-p (not (buffer-modified-p))))
1048 (define-ibuffer-op toggle-read-only ()
1049 "Toggle read only status in marked buffers."
1050 (:opstring "toggled read only status in"
1051 :modifier-p t)
1052 (toggle-read-only))
1054 (define-ibuffer-op delete ()
1055 "Kill marked buffers as with `kill-this-buffer'."
1056 (:opstring "killed"
1057 :active-opstring "kill"
1058 :dangerous t
1059 :complex t
1060 :modifier-p t)
1061 (if (kill-buffer buf)
1062 'kill
1063 nil))
1065 (define-ibuffer-op kill-on-deletion-marks ()
1066 "Kill buffers marked for deletion as with `kill-this-buffer'."
1067 (:opstring "killed"
1068 :active-opstring "kill"
1069 :dangerous t
1070 :complex t
1071 :mark :deletion
1072 :modifier-p t)
1073 (if (kill-buffer buf)
1074 'kill
1075 nil))
1077 (defun ibuffer-unmark-all (mark)
1078 "Unmark all buffers with mark MARK."
1079 (interactive "cRemove marks (RET means all):")
1080 (if (= (ibuffer-count-marked-lines t) 0)
1081 (message "No buffers marked; use 'm' to mark a buffer")
1082 (cond
1083 ((char-equal mark ibuffer-marked-char)
1084 (ibuffer-map-marked-lines
1085 #'(lambda (buf mark)
1086 (ibuffer-set-mark-1 ? )
1087 t)))
1088 ((char-equal mark ibuffer-deletion-char)
1089 (ibuffer-map-deletion-lines
1090 #'(lambda (buf mark)
1091 (ibuffer-set-mark-1 ? )
1092 t)))
1094 (ibuffer-map-lines
1095 #'(lambda (buf mark)
1096 (when (not (char-equal mark ? ))
1097 (ibuffer-set-mark-1 ? ))
1098 t)))))
1099 (ibuffer-redisplay t))
1101 (defun ibuffer-toggle-marks ()
1102 "Toggle which buffers are marked.
1103 In other words, unmarked buffers become marked, and marked buffers
1104 become unmarked."
1105 (interactive)
1106 (let ((count
1107 (ibuffer-map-lines
1108 #'(lambda (buf mark)
1109 (cond ((eq mark ibuffer-marked-char)
1110 (ibuffer-set-mark-1 ? )
1111 nil)
1112 ((eq mark ? )
1113 (ibuffer-set-mark-1 ibuffer-marked-char)
1116 nil))))))
1117 (message "%s buffers marked" count))
1118 (ibuffer-redisplay t))
1120 (defun ibuffer-mark-forward (arg)
1121 "Mark the buffer on this line, and move forward ARG lines."
1122 (interactive "P")
1123 (ibuffer-mark-interactive arg ibuffer-marked-char 1))
1125 (defun ibuffer-unmark-forward (arg)
1126 "Unmark the buffer on this line, and move forward ARG lines."
1127 (interactive "P")
1128 (ibuffer-mark-interactive arg ? 1))
1130 (defun ibuffer-unmark-backward (arg)
1131 "Unmark the buffer on this line, and move backward ARG lines."
1132 (interactive "P")
1133 (ibuffer-mark-interactive arg ? -1))
1135 (defun ibuffer-mark-interactive (arg mark movement)
1136 (assert (eq major-mode 'ibuffer-mode))
1137 (unless arg
1138 (setq arg 1))
1139 (ibuffer-forward-line 0)
1140 (let ((inhibit-read-only t))
1141 (while (> arg 0)
1142 (ibuffer-set-mark mark)
1143 (ibuffer-forward-line movement)
1144 (setq arg (1- arg)))))
1146 (defun ibuffer-set-mark (mark)
1147 (assert (eq major-mode 'ibuffer-mode))
1148 (let ((inhibit-read-only t))
1149 (ibuffer-set-mark-1 mark)
1150 (setq ibuffer-did-modification t)
1151 (ibuffer-redisplay-current)
1152 (beginning-of-line)))
1154 (defun ibuffer-set-mark-1 (mark)
1155 (let ((beg (line-beginning-position))
1156 (end (line-end-position)))
1157 (put-text-property beg end 'ibuffer-properties
1158 (list (ibuffer-current-buffer)
1159 mark))))
1161 (defun ibuffer-mark-for-delete (arg)
1162 "Mark the buffers on ARG lines forward for deletion."
1163 (interactive "P")
1164 (ibuffer-mark-interactive arg ibuffer-deletion-char 1))
1166 (defun ibuffer-mark-for-delete-backwards (arg)
1167 "Mark the buffers on ARG lines backward for deletion."
1168 (interactive "P")
1169 (ibuffer-mark-interactive arg ibuffer-deletion-char -1))
1171 (defun ibuffer-current-buffer (&optional must-be-live)
1172 (let ((buf (car (get-text-property (line-beginning-position)
1173 'ibuffer-properties))))
1174 (when (and must-be-live
1175 (not (buffer-live-p buf)))
1176 (error "Buffer %s has been killed!" buf))
1177 buf))
1179 (defun ibuffer-active-formats-name ()
1180 (if (boundp 'ibuffer-filter-format-alist)
1181 (let ((ret nil))
1182 (dolist (filter ibuffer-filtering-qualifiers ret)
1183 (let ((val (assq (car filter) ibuffer-filter-format-alist)))
1184 (when val
1185 (setq ret (car filter)))))
1186 (if ret
1188 :ibuffer-formats))
1189 :ibuffer-formats))
1191 (defun ibuffer-current-formats (uncompiledp)
1192 (let* ((name (ibuffer-active-formats-name)))
1193 (ibuffer-check-formats)
1194 (if (eq name :ibuffer-formats)
1195 (if uncompiledp
1196 ibuffer-formats
1197 ibuffer-compiled-formats)
1198 (cadr (assq name
1199 (if uncompiledp
1200 ibuffer-filter-format-alist
1201 ibuffer-compiled-filter-formats))))))
1203 (defun ibuffer-current-format (&optional uncompiledp)
1204 (or ibuffer-current-format
1205 (setq ibuffer-current-format 0))
1206 (nth ibuffer-current-format (ibuffer-current-formats uncompiledp)))
1208 (defun ibuffer-expand-format-entry (form)
1209 (if (or (consp form)
1210 (symbolp form))
1211 (let ((sym (intern (concat "ibuffer-make-column-"
1212 (symbol-name (if (consp form)
1213 (car form)
1214 form))))))
1215 (unless (or (fboundp sym)
1216 (assq sym ibuffer-inline-columns))
1217 (error "Unknown column %s in ibuffer-formats" form))
1218 (let (min max align elide)
1219 (if (consp form)
1220 (setq min (or (nth 1 form) 0)
1221 max (or (nth 2 form) -1)
1222 align (or (nth 3 form) :left)
1223 elide (or (nth 4 form) nil))
1224 (setq min 0
1225 max -1
1226 align :left
1227 elide nil))
1228 (list sym min max align elide)))
1229 form))
1231 (defun ibuffer-compile-make-eliding-form (strvar elide from-end-p)
1232 (let ((ellipsis (if (ibuffer-use-fontification)
1233 (propertize ibuffer-eliding-string 'face 'bold)
1234 ibuffer-eliding-string)))
1235 (if (or elide ibuffer-elide-long-columns)
1236 `(if (> strlen 5)
1237 ,(if from-end-p
1238 `(concat ,ellipsis
1239 (substring ,strvar
1240 (length ibuffer-eliding-string)))
1241 `(concat
1242 (substring ,strvar 0 (- strlen ,(length ellipsis)))
1243 ,ellipsis))
1244 ,strvar)
1245 strvar)))
1247 (defun ibuffer-compile-make-substring-form (strvar maxvar from-end-p)
1248 (if from-end-p
1249 `(substring str
1250 (- strlen ,maxvar))
1251 `(substring ,strvar 0 ,maxvar)))
1253 (defun ibuffer-compile-make-format-form (strvar widthform alignment)
1254 (let* ((left `(make-string tmp2 ? ))
1255 (right `(make-string (- tmp1 tmp2) ? )))
1256 `(progn
1257 (setq tmp1 ,widthform
1258 tmp2 (/ tmp1 2))
1259 ,(case alignment
1260 (:right `(concat ,left ,right ,strvar))
1261 (:center `(concat ,left ,strvar ,right))
1262 (:left `(concat ,strvar ,left ,right))
1263 (t (error "Invalid alignment %s" alignment))))))
1265 (defun ibuffer-compile-format (format)
1266 (let ((result nil)
1267 ;; We use these variables to keep track of which variables
1268 ;; inside the generated function we need to bind, since
1269 ;; binding variables in Emacs takes time.
1270 str-used tmp1-used tmp2-used global-strlen-used)
1271 (dolist (form format)
1272 (push
1273 ;; Generate a form based on a particular format entry, like
1274 ;; " ", mark, or (mode 16 16 :right).
1275 (if (stringp form)
1276 ;; It's a string; all we need to do is insert it.
1277 `(insert ,form)
1278 (let* ((form (ibuffer-expand-format-entry form))
1279 (sym (nth 0 form))
1280 (min (nth 1 form))
1281 (max (nth 2 form))
1282 (align (nth 3 form))
1283 (elide (nth 4 form)))
1284 (let* ((from-end-p (when (minusp min)
1285 (setq min (- min))
1287 (letbindings nil)
1288 (outforms nil)
1289 minform
1290 maxform
1291 min-used max-used strlen-used)
1292 (when (or (not (integerp min)) (>= min 0))
1293 ;; This is a complex case; they want it limited to a
1294 ;; minimum size.
1295 (setq min-used t)
1296 (setq str-used t strlen-used t global-strlen-used t
1297 tmp1-used t tmp2-used t)
1298 ;; Generate code to limit the string to a minimum size.
1299 (setq minform `(progn
1300 (setq str
1301 ,(ibuffer-compile-make-format-form
1302 'str
1303 `(- ,(if (integerp min)
1305 'min)
1306 strlen)
1307 align)))))
1308 (when (or (not (integerp max)) (> max 0))
1309 (setq str-used t max-used t)
1310 ;; Generate code to limit the string to a maximum size.
1311 (setq maxform `(progn
1312 (setq str
1313 ,(ibuffer-compile-make-substring-form
1314 'str
1315 (if (integerp max)
1317 'max)
1318 from-end-p))
1319 (setq strlen (length str))
1320 (setq str
1321 ,(ibuffer-compile-make-eliding-form 'str
1322 elide
1323 from-end-p)))))
1324 ;; Now, put these forms together with the rest of the code.
1325 (let ((callform
1326 ;; Is this an "inline" column? This means we have
1327 ;; to get the code from the
1328 ;; `ibuffer-inline-columns' alist and insert it
1329 ;; into our generated code. Otherwise, we just
1330 ;; generate a call to the column function.
1331 (ibuffer-aif (assq sym ibuffer-inline-columns)
1332 (nth 1 it)
1333 `(,sym buffer mark)))
1334 ;; You're not expected to understand this. Hell, I
1335 ;; don't even understand it, and I wrote it five
1336 ;; minutes ago.
1337 (insertgenfn (ibuffer-aif (get sym 'ibuffer-column-summarizer)
1338 ;; I really, really wish Emacs Lisp had closures.
1339 (lambda (arg sym)
1340 `(insert
1341 (let ((ret ,arg))
1342 (put ',sym 'ibuffer-column-summary
1343 (cons ret (get ',sym 'ibuffer-column-summary)))
1344 ret)))
1345 (lambda (arg sym)
1346 `(insert ,arg))))
1347 (mincompform `(< strlen ,(if (integerp min)
1349 'min)))
1350 (maxcompform `(> strlen ,(if (integerp max)
1352 'max))))
1353 (if (or min-used max-used)
1354 ;; The complex case, where we have to limit the
1355 ;; form to a maximum or minimum size.
1356 (progn
1357 (when (and min-used (not (integerp min)))
1358 (push `(min ,min) letbindings))
1359 (when (and max-used (not (integerp max)))
1360 (push `(max ,max) letbindings))
1361 (push
1362 (if (and min-used max-used)
1363 `(if ,mincompform
1364 ,minform
1365 (if ,maxcompform
1366 ,maxform))
1367 (if min-used
1368 `(when ,mincompform
1369 ,minform)
1370 `(when ,maxcompform
1371 ,maxform)))
1372 outforms)
1373 (push (append
1374 `(setq str ,callform)
1375 (when strlen-used
1376 `(strlen (length str))))
1377 outforms)
1378 (setq outforms
1379 (append outforms (list (funcall insertgenfn 'str sym)))))
1380 ;; The simple case; just insert the string.
1381 (push (funcall insertgenfn callform sym) outforms))
1382 ;; Finally, return a `let' form which binds the
1383 ;; variables in `letbindings', and contains all the
1384 ;; code in `outforms'.
1385 `(let ,letbindings
1386 ,@outforms)))))
1387 result))
1388 (setq result
1389 ;; We don't want to unconditionally load the byte-compiler.
1390 (funcall (if (or ibuffer-always-compile-formats
1391 (featurep 'bytecomp))
1392 #'byte-compile
1393 #'identity)
1394 ;; Here, we actually create a lambda form which
1395 ;; inserts all the generated forms for each entry
1396 ;; in the format string.
1397 (nconc (list 'lambda '(buffer mark))
1398 `((let ,(append (when str-used
1399 '(str))
1400 (when global-strlen-used
1401 '(strlen))
1402 (when tmp1-used
1403 '(tmp1))
1404 (when tmp2-used
1405 '(tmp2)))
1406 ,@(nreverse result))))))))
1408 (defvar ibuffer-compiled-formats nil)
1409 (defvar ibuffer-cached-formats nil)
1410 (defvar ibuffer-cached-eliding-string nil)
1411 (defvar ibuffer-cached-elide-long-columns 0)
1413 (defun ibuffer-recompile-formats ()
1414 "Recompile `ibuffer-formats'."
1415 (interactive)
1416 (setq ibuffer-compiled-formats
1417 (mapcar #'ibuffer-compile-format ibuffer-formats))
1418 (when (boundp 'ibuffer-filter-format-alist)
1419 (setq ibuffer-compiled-filter-formats
1420 (mapcar #'(lambda (entry)
1421 (cons (car entry)
1422 (mapcar #'(lambda (formats)
1423 (mapcar #'ibuffer-compile-format formats))
1424 (cdr entry))))
1425 ibuffer-filter-format-alist))))
1427 (defun ibuffer-clear-summary-columns (format)
1428 (dolist (form format)
1429 (ibuffer-awhen (and (consp form)
1430 (get (car form) 'ibuffer-column-summarizer))
1431 (put (car form) 'ibuffer-column-summary nil))))
1433 (defun ibuffer-check-formats ()
1434 (when (null ibuffer-formats)
1435 (error "No formats!"))
1436 (when (or (null ibuffer-compiled-formats)
1437 (null ibuffer-cached-formats)
1438 (not (eq ibuffer-cached-formats ibuffer-formats))
1439 (null ibuffer-cached-eliding-string)
1440 (not (equal ibuffer-cached-eliding-string ibuffer-eliding-string))
1441 (eql 0 ibuffer-cached-elide-long-columns)
1442 (not (eql ibuffer-cached-elide-long-columns
1443 ibuffer-elide-long-columns))
1444 (not (eq ibuffer-cached-filter-formats
1445 ibuffer-filter-format-alist))
1446 (and ibuffer-filter-format-alist
1447 (null ibuffer-compiled-filter-formats)))
1448 (message "Formats have changed, recompiling...")
1449 (ibuffer-recompile-formats)
1450 (setq ibuffer-cached-formats ibuffer-formats
1451 ibuffer-cached-eliding-string ibuffer-eliding-string
1452 ibuffer-cached-elide-long-columns ibuffer-elide-long-columns
1453 ibuffer-cached-filter-formats ibuffer-filter-format-alist)
1454 (message "Formats have changed, recompiling...done")))
1456 (defvar ibuffer-inline-columns nil)
1458 (define-ibuffer-column mark (:name " " :inline t)
1459 (string mark))
1461 (define-ibuffer-column read-only (:name "R" :inline t)
1462 (if buffer-read-only
1464 " "))
1466 (define-ibuffer-column modified (:name "M" :inline t)
1467 (if (buffer-modified-p)
1468 (string ibuffer-modified-char)
1469 " "))
1471 (define-ibuffer-column name (:inline t
1472 :props
1473 ('mouse-face 'highlight 'keymap ibuffer-name-map
1474 'ibuffer-name-column t
1475 'help-echo "mouse-1: mark this buffer\nmouse-2: select this buffer\nmouse-3: operate on this buffer"))
1476 (buffer-name))
1478 (define-ibuffer-column size (:inline t)
1479 (format "%s" (buffer-size)))
1481 (define-ibuffer-column mode (:inline t
1482 :props
1483 ('mouse-face 'highlight
1484 'keymap ibuffer-mode-name-map
1485 'help-echo "mouse-2: filter by this mode"))
1486 (format "%s" mode-name))
1488 (define-ibuffer-column process ()
1489 (let ((proc (get-buffer-process buffer)))
1490 (if proc
1491 (format "(%s %s)" proc (process-status proc))
1492 "none")))
1494 (define-ibuffer-column filename ()
1495 (let ((directory-abbrev-alist ibuffer-directory-abbrev-alist))
1496 (abbreviate-file-name
1497 (or buffer-file-name
1498 (and (boundp 'dired-directory)
1499 dired-directory)
1500 ""))))
1502 (defun ibuffer-format-column (str width alignment)
1503 (let ((left (make-string (/ width 2) ? ))
1504 (right (make-string (- width (/ width 2)) ? )))
1505 (case alignment
1506 (:right (concat left right str))
1507 (:center (concat left str right))
1508 (t (concat str left right)))))
1510 (defun ibuffer-fontify-region-function (beg end &optional verbose)
1511 (when verbose (message "Fontifying..."))
1512 (let ((inhibit-read-only t))
1513 (save-excursion
1514 (goto-char beg)
1515 (beginning-of-line)
1516 (while (< (point) end)
1517 (if (get-text-property (point) 'ibuffer-title-header)
1518 (put-text-property (point) (line-end-position) 'face ibuffer-title-face)
1519 (unless (or (get-text-property (point) 'ibuffer-title)
1520 (get-text-property (point) 'ibuffer-summary))
1521 (multiple-value-bind (buf mark)
1522 (get-text-property (point) 'ibuffer-properties)
1523 (let* ((namebeg (next-single-property-change (point) 'ibuffer-name-column
1524 nil (line-end-position)))
1525 (nameend (next-single-property-change namebeg 'ibuffer-name-column
1526 nil (line-end-position))))
1527 (put-text-property namebeg
1528 nameend
1529 'face
1530 (cond ((char-equal mark ibuffer-marked-char)
1531 ibuffer-marked-face)
1532 ((char-equal mark ibuffer-deletion-char)
1533 ibuffer-deletion-face)
1535 (let ((level -1)
1536 result)
1537 (dolist (e ibuffer-fontification-alist result)
1538 (when (and (> (car e) level)
1539 (with-current-buffer buf
1540 (eval (cadr e))))
1541 (setq level (car e)
1542 result
1543 (if (symbolp (caddr e))
1544 (if (facep (caddr e))
1545 (caddr e)
1546 (symbol-value (caddr e)))))))))))))))
1547 (forward-line 1))))
1548 (when verbose (message "Fontifying...done")))
1550 (defun ibuffer-unfontify-region-function (beg end)
1551 (let ((inhibit-read-only t))
1552 (remove-text-properties beg end '(face nil))))
1554 (defun ibuffer-insert-buffer-line (buffer mark format)
1555 "Insert a line describing BUFFER and MARK using FORMAT."
1556 (assert (eq major-mode 'ibuffer-mode))
1557 (let ((beg (point)))
1558 (funcall format buffer mark)
1559 (put-text-property beg (point) 'ibuffer-properties (list buffer mark)))
1560 (insert "\n"))
1562 ;; This function knows a bit too much of the internals. It would be
1563 ;; nice if it was all abstracted away into
1564 ;; `ibuffer-insert-buffers-and-marks'.
1565 (defun ibuffer-redisplay-current ()
1566 (assert (eq major-mode 'ibuffer-mode))
1567 (when (eobp)
1568 (forward-line -1))
1569 (beginning-of-line)
1570 (let ((curformat (mapcar #'ibuffer-expand-format-entry
1571 (ibuffer-current-format t))))
1572 (ibuffer-clear-summary-columns curformat)
1573 (let ((buf (ibuffer-current-buffer)))
1574 (when buf
1575 (let ((mark (ibuffer-current-mark)))
1576 (save-excursion
1577 (delete-region (point) (1+ (line-end-position)))
1578 (ibuffer-insert-buffer-line
1579 buf mark
1580 (ibuffer-current-format)))
1581 (when ibuffer-shrink-to-minimum-size
1582 (ibuffer-shrink-to-fit)))))))
1584 (defun ibuffer-map-on-mark (mark func)
1585 (ibuffer-map-lines
1586 #'(lambda (buf mk)
1587 (if (char-equal mark mk)
1588 (funcall func buf mark)
1589 nil))))
1591 (defun ibuffer-map-lines (function &optional nomodify)
1592 "Call FUNCTION for each buffer in an ibuffer.
1593 Don't set the ibuffer modification flag iff NOMODIFY is non-nil.
1595 FUNCTION is called with four arguments: the buffer object itself, the
1596 current mark symbol, and the beginning and ending line positions."
1597 (assert (eq major-mode 'ibuffer-mode))
1598 (let ((orig-target-line (count-lines (point-min)
1599 (line-beginning-position)))
1600 (target-buf-count 0)
1601 (ibuffer-map-lines-total 0)
1602 (ibuffer-map-lines-count 0))
1603 (unwind-protect
1604 (progn
1605 (setq buffer-read-only nil)
1606 (goto-char (point-min))
1607 (ibuffer-forward-line 0)
1608 (setq orig-target-line (1+ (- orig-target-line
1609 (count-lines (point-min) (point))))
1610 target-buf-count orig-target-line)
1611 (while (and (not (eobp))
1612 (not (get-text-property (point) 'ibuffer-summary)))
1613 (let ((result
1614 (if (buffer-live-p (ibuffer-current-buffer))
1615 (save-excursion
1616 (funcall function
1617 (ibuffer-current-buffer)
1618 (ibuffer-current-mark)))
1619 ;; Kill the line if the buffer is dead
1620 'kill)))
1621 ;; A given mapping function should return:
1622 ;; `nil' if it chose not to affect the buffer
1623 ;; `kill' means the remove line from the buffer list
1624 ;; `t' otherwise
1625 (incf ibuffer-map-lines-total)
1626 (cond ((null result)
1627 (forward-line 1))
1628 ((eq result 'kill)
1629 (delete-region (line-beginning-position)
1630 (1+ (line-end-position)))
1631 (incf ibuffer-map-lines-count)
1632 (when (< ibuffer-map-lines-total
1633 orig-target-line)
1634 (decf target-buf-count)))
1636 (incf ibuffer-map-lines-count)
1637 (forward-line 1)))))
1638 ibuffer-map-lines-count)
1639 (progn
1640 (setq buffer-read-only t)
1641 (unless nomodify
1642 (set-buffer-modified-p nil))
1643 (goto-char (point-min))
1644 (ibuffer-forward-line 0)
1645 (ibuffer-forward-line (1- target-buf-count))))))
1647 (defun ibuffer-get-marked-buffers ()
1648 "Return a list of buffer objects currently marked."
1649 (delq nil
1650 (mapcar #'(lambda (e)
1651 (when (eq (cdr e) ibuffer-marked-char)
1652 (car e)))
1653 (ibuffer-current-state-list))))
1655 (defun ibuffer-current-state-list (&optional pos)
1656 "Return a list like (BUF . MARK) of all buffers in an ibuffer.
1657 If POS is non-nil, return a list like (BUF MARK POINT), where POINT is
1658 the value of point at the beginning of the line for that buffer."
1659 (let ((ibuffer-current-state-list-tmp '()))
1660 ;; ah, if only we had closures. I bet this will mysteriously
1661 ;; break later. Don't blame me.
1662 (if pos
1663 (ibuffer-map-lines-nomodify
1664 #'(lambda (buf mark)
1665 (when (buffer-live-p buf)
1666 (push (list buf mark (point)) ibuffer-current-state-list-tmp))))
1667 (ibuffer-map-lines-nomodify
1668 #'(lambda (buf mark)
1669 (when (buffer-live-p buf)
1670 (push (cons buf mark) ibuffer-current-state-list-tmp)))))
1671 (nreverse ibuffer-current-state-list-tmp)))
1673 (defun ibuffer-current-buffers-with-marks (curbufs)
1674 "Return a list like (BUF . MARK) of all open buffers."
1675 (let ((bufs (ibuffer-current-state-list)))
1676 (mapcar #'(lambda (buf) (let ((e (assq buf bufs)))
1677 (if e
1679 (cons buf ? ))))
1680 curbufs)))
1682 (defun ibuffer-buf-matches-predicates (buf predicates)
1683 (let ((hit nil)
1684 (name (buffer-name buf)))
1685 (dolist (pred predicates)
1686 (when (if (stringp pred)
1687 (string-match pred name)
1688 (funcall pred buf))
1689 (setq hit t)))
1690 hit))
1692 (defun ibuffer-filter-buffers (ibuffer-buf last bmarklist all)
1693 (let ((ext-loaded (featurep 'ibuf-ext)))
1694 (delq nil
1695 (mapcar
1696 ;; element should be like (BUFFER . MARK)
1697 #'(lambda (e)
1698 (let* ((buf (car e)))
1699 (when
1700 ;; This takes precedence over anything else
1701 (or (and ibuffer-always-show-last-buffer
1702 (eq last buf))
1703 (funcall (if ext-loaded
1704 #'ibuffer-ext-visible-p
1705 #'ibuffer-visible-p)
1706 buf all ibuffer-buf))
1707 e)))
1708 bmarklist))))
1710 (defun ibuffer-visible-p (buf all &optional ibuffer-buf)
1711 (and (or all
1712 (not
1713 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates)))
1714 (or ibuffer-view-ibuffer
1715 (and ibuffer-buf
1716 (not (eq ibuffer-buf buf))))))
1718 ;; This function is a special case; it's not defined by
1719 ;; `ibuffer-define-sorter'.
1720 (defun ibuffer-do-sort-by-recency ()
1721 "Sort the buffers by last view time."
1722 (interactive)
1723 (setq ibuffer-sorting-mode 'recency)
1724 (ibuffer-redisplay t))
1726 (defun ibuffer-update-format ()
1727 (when (null ibuffer-current-format)
1728 (setq ibuffer-current-format 0))
1729 (when (null ibuffer-formats)
1730 (error "Ibuffer error: no formats!")))
1732 (defun ibuffer-switch-format ()
1733 "Switch the current display format."
1734 (interactive)
1735 (assert (eq major-mode 'ibuffer-mode))
1736 (unless (consp ibuffer-formats)
1737 (error "Ibuffer error: No formats!"))
1738 (setq ibuffer-current-format
1739 (if (>= ibuffer-current-format (1- (length (ibuffer-current-formats nil))))
1741 (1+ ibuffer-current-format)))
1742 (ibuffer-update-format)
1743 (ibuffer-redisplay t))
1745 (defun ibuffer-update-title-and-summary (format)
1746 (assert (eq major-mode 'ibuffer-mode))
1747 ;; Don't do funky font-lock stuff here
1748 (let ((after-change-functions nil))
1749 (if (get-text-property (point-min) 'ibuffer-title)
1750 (delete-region (point-min)
1751 (next-single-property-change
1752 (point-min) 'ibuffer-title)))
1753 (goto-char (point-min))
1754 (put-text-property
1755 (point)
1756 (progn
1757 (let ((opos (point)))
1758 ;; Insert the title names.
1759 (dolist (element format)
1760 (insert
1761 (if (stringp element)
1762 element
1763 (let ((sym (car element))
1764 (min (cadr element))
1765 ;; (max (caddr element))
1766 (align (cadddr element)))
1767 ;; Ignore a negative min when we're inserting the title
1768 (when (minusp min)
1769 (setq min (- min)))
1770 (let* ((name (or (get sym 'ibuffer-column-name)
1771 (error "Unknown column %s in ibuffer-formats" sym)))
1772 (len (length name)))
1773 (if (< len min)
1774 (ibuffer-format-column name
1775 (- min len)
1776 align)
1777 name))))))
1778 (put-text-property opos (point) 'ibuffer-title-header t)
1779 (insert "\n")
1780 ;; Add the underlines
1781 (let ((str (save-excursion
1782 (forward-line -1)
1783 (beginning-of-line)
1784 (buffer-substring (point) (line-end-position)))))
1785 (apply #'insert (mapcar
1786 #'(lambda (c)
1787 (if (not (or (char-equal c ? )
1788 (char-equal c ?\n)))
1790 ? ))
1791 str)))
1792 (insert "\n"))
1793 (point))
1794 'ibuffer-title t)
1795 ;; Now, insert the summary columns.
1796 (goto-char (point-max))
1797 (if (get-text-property (1- (point-max)) 'ibuffer-summary)
1798 (delete-region (previous-single-property-change
1799 (point-max) 'ibuffer-summary)
1800 (point-max)))
1801 (put-text-property
1802 (point)
1803 (progn
1804 (insert "\n")
1805 (dolist (element format)
1806 (insert
1807 (if (stringp element)
1808 (make-string (length element) ? )
1809 (let ((sym (car element)))
1810 (let ((min (cadr element))
1811 ;; (max (caddr element))
1812 (align (cadddr element)))
1813 ;; Ignore a negative min when we're inserting the title
1814 (when (minusp min)
1815 (setq min (- min)))
1816 (let* ((summary (if (get sym 'ibuffer-column-summarizer)
1817 (funcall (get sym 'ibuffer-column-summarizer)
1818 (get sym 'ibuffer-column-summary))
1819 (make-string (length (get sym 'ibuffer-column-name))
1820 ? )))
1821 (len (length summary)))
1822 (if (< len min)
1823 (ibuffer-format-column summary
1824 (- min len)
1825 align)
1826 summary)))))))
1827 (point))
1828 'ibuffer-summary t)))
1830 (defun ibuffer-update-mode-name ()
1831 (setq mode-name (format "Ibuffer by %s" (if ibuffer-sorting-mode
1832 ibuffer-sorting-mode
1833 "view time")))
1834 (when ibuffer-sorting-reversep
1835 (setq mode-name (concat mode-name " [rev]")))
1836 (when (and (featurep 'ibuf-ext)
1837 ibuffer-auto-mode)
1838 (setq mode-name (concat mode-name " (Auto)")))
1839 (let ((result ""))
1840 (when (featurep 'ibuf-ext)
1841 (dolist (qualifier ibuffer-filtering-qualifiers)
1842 (setq result
1843 (concat result (ibuffer-format-qualifier qualifier))))
1844 (if ibuffer-use-header-line
1845 (setq header-line-format
1846 (when ibuffer-filtering-qualifiers
1847 (replace-regexp-in-string "%" "%%"
1848 (concat mode-name result))))
1849 (progn
1850 (setq mode-name (concat mode-name result))
1851 (when (boundp 'header-line-format)
1852 (setq header-line-format nil)))))))
1854 (defun ibuffer-redisplay (&optional silent)
1855 "Redisplay the current list of buffers.
1857 This does not show new buffers; use `ibuffer-update' for that.
1859 If SILENT is non-`nil', do not generate progress messages."
1860 (interactive)
1861 (ibuffer-forward-line 0)
1862 (unless silent
1863 (message "Redisplaying current buffer list..."))
1864 (let ((blist (ibuffer-current-state-list)))
1865 (when (null blist)
1866 (if (and (featurep 'ibuf-ext)
1867 ibuffer-filtering-qualifiers)
1868 (message "No buffers! (note: filtering in effect)")
1869 (error "No buffers!")))
1870 (ibuffer-insert-buffers-and-marks blist t)
1871 (ibuffer-update-mode-name)
1872 (unless silent
1873 (message "Redisplaying current buffer list...done"))
1874 (ibuffer-forward-line 0)))
1876 (defun ibuffer-update (arg &optional silent)
1877 "Regenerate the list of all buffers.
1879 Display buffers whose name matches one of `ibuffer-maybe-show-predicates'
1880 iff arg ARG is non-nil.
1882 Do not display messages if SILENT is non-nil."
1883 (interactive "P")
1884 (ibuffer-forward-line 0)
1885 (let* ((bufs (buffer-list))
1886 (blist (ibuffer-filter-buffers
1887 (current-buffer)
1888 (if (and
1889 (cadr bufs)
1890 (eq ibuffer-always-show-last-buffer
1891 :nomini)
1892 ;; This is a hack.
1893 (string-match " \\*Minibuf"
1894 (buffer-name (cadr bufs))))
1895 (caddr bufs)
1896 (cadr bufs))
1897 (ibuffer-current-buffers-with-marks bufs)
1898 arg)))
1899 (when (null blist)
1900 (if (and (featurep 'ibuf-ext)
1901 ibuffer-filtering-qualifiers)
1902 (message "No buffers! (note: filtering in effect)")
1903 (error "No buffers!")))
1904 (unless silent
1905 (message "Updating buffer list..."))
1906 (ibuffer-insert-buffers-and-marks blist
1907 arg)
1908 (ibuffer-update-mode-name)
1909 (unless silent
1910 (message "Updating buffer list...done")))
1911 (if (eq ibuffer-shrink-to-minimum-size 'onewindow)
1912 (ibuffer-shrink-to-fit t)
1913 (when ibuffer-shrink-to-minimum-size
1914 (ibuffer-shrink-to-fit)))
1915 (ibuffer-forward-line 0))
1917 (defun ibuffer-insert-buffers-and-marks (bmarklist &optional all)
1918 (assert (eq major-mode 'ibuffer-mode))
1919 (let ((--ibuffer-insert-buffers-and-marks-format
1920 (ibuffer-current-format))
1921 (--ibuffer-expanded-format (mapcar #'ibuffer-expand-format-entry
1922 (ibuffer-current-format t)))
1923 (orig (count-lines (point-min) (point)))
1924 ;; Inhibit font-lock caching tricks, since we're modifying the
1925 ;; entire buffer at once
1926 (after-change-functions nil))
1927 (ibuffer-clear-summary-columns --ibuffer-expanded-format)
1928 (unwind-protect
1929 (progn
1930 (setq buffer-read-only nil)
1931 (erase-buffer)
1932 (ibuffer-update-format)
1933 (let ((entries
1934 (let* ((sortdat (assq ibuffer-sorting-mode
1935 ibuffer-sorting-functions-alist))
1936 (func (caddr sortdat)))
1937 (let ((result
1938 ;; actually sort the buffers
1939 (if (and sortdat func)
1940 (sort bmarklist func)
1941 bmarklist)))
1942 ;; perhaps reverse the sorted buffer list
1943 (if ibuffer-sorting-reversep
1944 (nreverse result)
1945 result)))))
1946 (dolist (entry entries)
1947 (ibuffer-insert-buffer-line
1948 (car entry)
1949 (cdr entry)
1950 --ibuffer-insert-buffers-and-marks-format)))
1951 (ibuffer-update-title-and-summary --ibuffer-expanded-format))
1952 (setq buffer-read-only t)
1953 (set-buffer-modified-p ibuffer-did-modification)
1954 (setq ibuffer-did-modification nil)
1955 (goto-line (1+ orig)))))
1957 (defun ibuffer-quit ()
1958 "Quit this `ibuffer' session.
1959 Delete the current window iff `ibuffer-delete-window-on-quit' is non-nil."
1960 (interactive)
1961 (if ibuffer-delete-window-on-quit
1962 (progn
1963 (bury-buffer)
1964 (unless (= (count-windows) 1)
1965 (delete-window)))
1966 (bury-buffer)))
1968 ;;;###autoload
1969 (defun ibuffer-list-buffers (&optional files-only)
1970 "Display a list of buffers, in another window.
1971 If optional argument FILES-ONLY is non-nil, then add a filter for
1972 buffers which are visiting a file."
1973 (interactive "P")
1974 (ibuffer t nil (when files-only
1975 '((filename . ".*"))) t))
1977 ;;;###autoload
1978 (defun ibuffer-other-window (&optional files-only)
1979 "Like `ibuffer', but displayed in another window by default.
1980 If optional argument FILES-ONLY is non-nil, then add a filter for
1981 buffers which are visiting a file."
1982 (interactive "P")
1983 (ibuffer t nil (when files-only
1984 '((filename . ".*")))))
1986 ;;;###autoload
1987 (defun ibuffer (&optional other-window-p name qualifiers noselect shrink)
1988 "Begin using `ibuffer' to edit a list of buffers.
1989 Type 'h' after entering ibuffer for more information.
1991 Optional argument OTHER-WINDOW-P says to use another window.
1992 Optional argument NAME specifies the name of the buffer; it defaults
1993 to \"*Ibuffer*\".
1994 Optional argument QUALIFIERS is an initial set of filtering qualifiers
1995 to use; see `ibuffer-filtering-qualifiers'.
1996 Optional argument NOSELECT means don't select the Ibuffer buffer.
1997 Optional argument SHRINK means shrink the buffer to minimal size. The
1998 special value `onewindow' means always use another window."
1999 (interactive "P")
2001 ;; The individual functions are lazy-loaded, via byte-compile-dynamic,
2002 ;; so we may as well load the file unconditionally now.
2003 (require 'ibuf-ext)
2005 (when ibuffer-use-other-window
2006 (setq other-window-p t))
2007 (let* ((buf (get-buffer-create (or name "*Ibuffer*")))
2008 (already-in (eq (current-buffer) buf))
2009 (need-update nil))
2010 (if other-window-p
2011 (funcall (if noselect #'(lambda (buf) (display-buffer buf t)) #'pop-to-buffer) buf)
2012 (funcall (if noselect #'display-buffer #'switch-to-buffer) buf))
2013 (with-current-buffer buf
2014 (let ((owin (selected-window)))
2015 (unwind-protect
2016 (progn
2017 ;; We switch to the buffer's window in order to be able
2018 ;; to modify the value of point
2019 (select-window (get-buffer-window buf))
2020 (unless (eq major-mode 'ibuffer-mode)
2021 (ibuffer-mode)
2022 (setq need-update t))
2023 (when (ibuffer-use-fontification)
2024 (require 'font-lock))
2025 (setq ibuffer-delete-window-on-quit other-window-p)
2026 (when shrink
2027 (setq ibuffer-shrink-to-minimum-size shrink))
2028 (when qualifiers
2029 (setq ibuffer-filtering-qualifiers qualifiers))
2030 (ibuffer-update nil)
2031 (unwind-protect
2032 (progn
2033 (setq buffer-read-only nil)
2034 (run-hooks 'ibuffer-hooks))
2035 (setq buffer-read-only t))
2036 (unless ibuffer-expert
2037 (message "Commands: m, u, t, RET, g, k, S, D, Q; q to quit; h for help")))
2038 (select-window owin))))))
2040 (put 'ibuffer-mode 'mode-class 'special)
2041 (defun ibuffer-mode ()
2042 "A major mode for viewing a list of buffers.
2043 In ibuffer, you can conveniently perform many operations on the
2044 currently open buffers, in addition to filtering your view to a
2045 particular subset of them, and sorting by various criteria.
2047 Operations on marked buffers:
2049 '\\[ibuffer-do-save]' - Save the marked buffers
2050 '\\[ibuffer-do-view]' - View the marked buffers in this frame.
2051 '\\[ibuffer-do-view-other-frame]' - View the marked buffers in another frame.
2052 '\\[ibuffer-do-revert]' - Revert the marked buffers.
2053 '\\[ibuffer-do-toggle-read-only]' - Toggle read-only state of marked buffers.
2054 '\\[ibuffer-do-delete]' - Kill the marked buffers.
2055 '\\[ibuffer-do-replace-regexp]' - Replace by regexp in each of the marked
2056 buffers.
2057 '\\[ibuffer-do-query-replace]' - Query replace in each of the marked buffers.
2058 '\\[ibuffer-do-query-replace-regexp]' - As above, with a regular expression.
2059 '\\[ibuffer-do-print]' - Print the marked buffers.
2060 '\\[ibuffer-do-occur]' - List lines in all marked buffers which match
2061 a given regexp (like the function `occur').
2062 '\\[ibuffer-do-shell-command-pipe]' - Pipe the contents of the marked
2063 buffers to a shell command.
2064 '\\[ibuffer-do-shell-command-pipe-replace]' - Replace the contents of the marked
2065 buffers with the output of a shell command.
2066 '\\[ibuffer-do-shell-command-file]' - Run a shell command with the
2067 buffer's file as an argument.
2068 '\\[ibuffer-do-eval]' - Evaluate a form in each of the marked buffers. This
2069 is a very flexible command. For example, if you want to make all
2070 of the marked buffers read only, try using (toggle-read-only 1) as
2071 the input form.
2072 '\\[ibuffer-do-view-and-eval]' - As above, but view each buffer while the form
2073 is evaluated.
2074 '\\[ibuffer-do-kill-lines]' - Remove the marked lines from the *Ibuffer* buffer,
2075 but don't kill the associated buffer.
2076 '\\[ibuffer-do-kill-on-deletion-marks]' - Kill all buffers marked for deletion.
2078 Marking commands:
2080 '\\[ibuffer-mark-forward]' - Mark the buffer at point.
2081 '\\[ibuffer-toggle-marks]' - Unmark all currently marked buffers, and mark
2082 all unmarked buffers.
2083 '\\[ibuffer-unmark-forward]' - Unmark the buffer at point.
2084 '\\[ibuffer-unmark-backward]' - Unmark the buffer at point, and move to the
2085 previous line.
2086 '\\[ibuffer-unmark-all]' - Unmark all marked buffers.
2087 '\\[ibuffer-mark-by-mode]' - Mark buffers by major mode.
2088 '\\[ibuffer-mark-unsaved-buffers]' - Mark all \"unsaved\" buffers.
2089 This means that the buffer is modified, and has an associated file.
2090 '\\[ibuffer-mark-modified-buffers]' - Mark all modified buffers,
2091 regardless of whether or not they have an associated file.
2092 '\\[ibuffer-mark-special-buffers]' - Mark all buffers whose name begins and
2093 ends with '*'.
2094 '\\[ibuffer-mark-dissociated-buffers]' - Mark all buffers which have
2095 an associated file, but that file doesn't currently exist.
2096 '\\[ibuffer-mark-read-only-buffers]' - Mark all read-only buffers.
2097 '\\[ibuffer-mark-dired-buffers]' - Mark buffers in `dired' mode.
2098 '\\[ibuffer-mark-help-buffers]' - Mark buffers in `help-mode', `apropos-mode', etc.
2099 '\\[ibuffer-mark-old-buffers]' - Mark buffers older than `ibuffer-old-time'.
2100 '\\[ibuffer-mark-for-delete]' - Mark the buffer at point for deletion.
2101 '\\[ibuffer-mark-by-name-regexp]' - Mark buffers by their name, using a regexp.
2102 '\\[ibuffer-mark-by-mode-regexp]' - Mark buffers by their major mode, using a regexp.
2103 '\\[ibuffer-mark-by-file-name-regexp]' - Mark buffers by their filename, using a regexp.
2105 Filtering commands:
2107 '\\[ibuffer-filter-by-mode]' - Add a filter by major mode.
2108 '\\[ibuffer-filter-by-name]' - Add a filter by buffer name.
2109 '\\[ibuffer-filter-by-content]' - Add a filter by buffer content.
2110 '\\[ibuffer-filter-by-filename]' - Add a filter by filename.
2111 '\\[ibuffer-filter-by-size-gt]' - Add a filter by buffer size.
2112 '\\[ibuffer-filter-by-size-lt]' - Add a filter by buffer size.
2113 '\\[ibuffer-filter-by-predicate]' - Add a filter by an arbitrary Lisp predicate.
2114 '\\[ibuffer-save-filters]' - Save the current filters with a name.
2115 '\\[ibuffer-switch-to-saved-filters]' - Switch to previously saved filters.
2116 '\\[ibuffer-add-saved-filters]' - Add saved filters to current filters.
2117 '\\[ibuffer-or-filter]' - Replace the top two filters with their logical OR.
2118 '\\[ibuffer-pop-filter]' - Remove the top filter.
2119 '\\[ibuffer-negate-filter]' - Invert the logical sense of the top filter.
2120 '\\[ibuffer-decompose-filter]' - Break down the topmost filter.
2121 '\\[ibuffer-filter-disable]' - Remove all filtering currently in effect.
2123 Sorting commands:
2125 '\\[ibuffer-toggle-sorting-mode]' - Rotate between the various sorting modes.
2126 '\\[ibuffer-invert-sorting]' - Reverse the current sorting order.
2127 '\\[ibuffer-do-sort-by-alphabetic]' - Sort the buffers lexicographically.
2128 '\\[ibuffer-do-sort-by-recency]' - Sort the buffers by last viewing time.
2129 '\\[ibuffer-do-sort-by-size]' - Sort the buffers by size.
2130 '\\[ibuffer-do-sort-by-major-mode]' - Sort the buffers by major mode.
2132 Other commands:
2134 '\\[ibuffer-switch-format]' - Change the current display format.
2135 '\\[forward-line]' - Move point to the next line.
2136 '\\[previous-line]' - Move point to the previous line.
2137 '\\[ibuffer-update]' - As above, but add new buffers to the list.
2138 '\\[ibuffer-quit]' - Bury the Ibuffer buffer.
2139 '\\[describe-mode]' - This help.
2140 '\\[ibuffer-diff-with-file]' - View the differences between this buffer
2141 and its associated file.
2142 '\\[ibuffer-visit-buffer]' - View the buffer on this line.
2143 '\\[ibuffer-visit-buffer-other-window]' - As above, but in another window.
2144 '\\[ibuffer-visit-buffer-other-window-noselect]' - As both above, but don't select
2145 the new window.
2146 '\\[ibuffer-bury-buffer]' - Bury (not kill!) the buffer on this line.
2148 Information on Filtering:
2150 You can filter your ibuffer view via different critera. Each Ibuffer
2151 buffer has its own stack of active filters. For example, suppose you
2152 are working on an Emacs Lisp project. You can create an Ibuffer
2153 buffer displays buffers in just `emacs-lisp' modes via
2154 '\\[ibuffer-filter-by-mode] emacs-lisp-mode RET'. In this case, there
2155 is just one entry on the filtering stack.
2157 You can also combine filters. The various filtering commands push a
2158 new filter onto the stack, and the filters combine to show just
2159 buffers which satisfy ALL criteria on the stack. For example, suppose
2160 you only want to see buffers in `emacs-lisp' mode, whose names begin
2161 with \"gnus\". You can accomplish this via:
2162 '\\[ibuffer-filter-by-mode] emacs-lisp-mode RET
2163 \\[ibuffer-filter-by-name] ^gnus RET'.
2165 Additionally, you can OR the top two filters together with
2166 '\\[ibuffer-or-filters]'. To see all buffers in either
2167 `emacs-lisp-mode' or `lisp-interaction-mode', type:
2169 '\\[ibuffer-filter-by-mode] emacs-lisp-mode RET \\[ibuffer-filter-by-mode] lisp-interaction-mode RET \\[ibuffer-or-filters]'.
2171 Filters can also be saved and restored using mnemonic names: see the
2172 functions `ibuffer-save-filters' and `ibuffer-switch-to-saved-filters'.
2174 To remove the top filter on the stack, use '\\[ibuffer-pop-filter]', and
2175 to disable all filtering currently in effect, use
2176 '\\[ibuffer-filter-disable]'."
2177 (kill-all-local-variables)
2178 (use-local-map ibuffer-mode-map)
2179 (setq major-mode 'ibuffer-mode)
2180 (setq mode-name "Ibuffer")
2181 (setq buffer-read-only t)
2182 (buffer-disable-undo)
2183 (setq truncate-lines ibuffer-truncate-lines)
2184 ;; This makes things less ugly for Emacs 21 users with a non-nil
2185 ;; `show-trailing-whitespace'.
2186 (setq show-trailing-whitespace nil)
2187 ;; Dummy font-lock-defaults to make font-lock turn on. We want this
2188 ;; so we know when to enable ibuffer's internal fontification.
2189 (set (make-local-variable 'font-lock-defaults)
2190 '(nil t nil nil nil
2191 (font-lock-fontify-region-function . ibuffer-fontify-region-function)
2192 (font-lock-unfontify-region-function . ibuffer-unfontify-region-function)))
2193 (set (make-local-variable 'revert-buffer-function)
2194 #'ibuffer-update)
2195 (set (make-local-variable 'ibuffer-sorting-mode)
2196 ibuffer-default-sorting-mode)
2197 (set (make-local-variable 'ibuffer-sorting-reversep)
2198 ibuffer-default-sorting-reversep)
2199 (set (make-local-variable 'ibuffer-shrink-to-minimum-size)
2200 ibuffer-default-shrink-to-minimum-size)
2201 (set (make-local-variable 'ibuffer-filtering-qualifiers) nil)
2202 (set (make-local-variable 'ibuffer-compiled-formats) nil)
2203 (set (make-local-variable 'ibuffer-cached-formats) nil)
2204 (set (make-local-variable 'ibuffer-cached-eliding-string) nil)
2205 (set (make-local-variable 'ibuffer-cached-elide-long-columns) nil)
2206 (set (make-local-variable 'ibuffer-current-format) nil)
2207 (set (make-local-variable 'ibuffer-did-modifiction) nil)
2208 (set (make-local-variable 'ibuffer-delete-window-on-quit) nil)
2209 (set (make-local-variable 'ibuffer-did-modification) nil)
2210 (when (featurep 'ibuf-ext)
2211 (set (make-local-variable 'ibuffer-tmp-hide-regexps) nil)
2212 (set (make-local-variable 'ibuffer-tmp-show-regexps) nil))
2213 (define-key ibuffer-mode-map [menu-bar edit] 'undefined)
2214 (define-key ibuffer-mode-map [menu-bar operate] (cons "Operate" ibuffer-mode-operate-map))
2215 (ibuffer-update-format)
2216 (when ibuffer-default-directory
2217 (setq default-directory ibuffer-default-directory))
2218 (run-hooks 'ibuffer-mode-hooks)
2219 ;; called after mode hooks to allow the user to add filters
2220 (ibuffer-update-mode-name))
2222 (provide 'ibuffer)
2224 ;; Local Variables:
2225 ;; coding: iso-8859-1
2226 ;; End:
2228 ;;; ibuffer.el ends here