(do_check_ram_size): Don't hardcode the lisp address space size.
[emacs.git] / lisp / ibuffer.el
blob668db8ea0413d227788fd2bc993916ae125f6079
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 ;; Maintainer: John Paul Wallington <jpw@gnu.org>
7 ;; Created: 8 Sep 2000
8 ;; Keywords: buffer, convenience
10 ;; This file is part of GNU Emacs.
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program ; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; ibuffer.el is an advanced replacement for the `buffer-menu' which
30 ;; is normally distributed with Emacs. Its interface is intended to
31 ;; be analogous to that of Dired.
33 ;;; Code:
35 (eval-when-compile
36 (require 'cl)
37 (require 'ibuf-macs)
38 (require 'dired))
40 (require 'font-core)
42 (defgroup ibuffer nil
43 "An advanced replacement for `buffer-menu'.
45 Ibuffer allows you to operate on buffers in a manner much like Dired.
46 Operations include sorting, marking by regular expression, and
47 the ability to filter the displayed buffers by various criteria."
48 :group 'convenience)
50 (defcustom ibuffer-formats '((mark modified read-only " " (name 18 18 :left :elide)
51 " " (size 9 -1 :right)
52 " " (mode 16 16 :right :elide) " " filename-and-process)
53 (mark " " (name 16 -1) " " filename))
54 "A list of ways to display buffer lines.
56 With Ibuffer, you are not limited to displaying just certain
57 attributes of a buffer such as size, name, and mode in a particular
58 order. Through this variable, you can completely customize and
59 control the appearance of an Ibuffer buffer. See also
60 `define-ibuffer-column', which allows you to define your own columns
61 for display.
63 This variable has the form
64 ((COLUMN COLUMN ...) (COLUMN COLUMN ...) ...)
65 Each element in `ibuffer-formats' should be a list containing COLUMN
66 specifiers. A COLUMN can be any of the following:
68 SYMBOL - A symbol naming the column. Predefined columns are:
69 mark modified read-only name size mode process filename
70 When you define your own columns using `define-ibuffer-column', just
71 use their name like the predefined columns here. This entry can
72 also be a function of two arguments, which should return a string.
73 The first argument is the buffer object, and the second is the mark
74 on that buffer.
76 \"STRING\" - A literal string to display.
78 (SYMBOL MIN-SIZE MAX-SIZE &optional ALIGN ELIDE) - SYMBOL is a
79 symbol naming the column, and MIN-SIZE and MAX-SIZE are integers (or
80 functions of no arguments returning an integer) which constrict the
81 size of a column. If MAX-SIZE is -1, there is no upper bound. The
82 default values are 0 and -1, respectively. If MIN-SIZE is negative,
83 use the end of the string. The optional element ALIGN describes the
84 alignment of the column; it can be :left, :center or :right. The
85 optional element ELIDE describes whether or not to elide the column
86 if it is too long; valid values are :elide and nil. The default is
87 nil (don't elide).
89 Some example of valid entries in `ibuffer-formats', with
90 description (also, feel free to try them out, and experiment with your
91 own!):
93 (mark \" \" name)
94 This format just displays the current mark (if any) and the name of
95 the buffer, separated by a space.
96 (mark modified read-only \" \" (name 16 16 :left) \" \" (size 6 -1 :right))
97 This format displays the current mark (if any), its modification and
98 read-only status, as well as the name of the buffer and its size. In
99 this format, the name is restricted to 16 characters (longer names
100 will be truncated, and shorter names will be padded with spaces), and
101 the name is also aligned to the left. The size of the buffer will
102 be padded with spaces up to a minimum of six characters, but there is
103 no upper limit on its size. The size will also be aligned to the
104 right.
106 Thus, if you wanted to use these two formats, add
108 (setq ibuffer-formats '((mark \" \" name)
109 (mark modified read-only
110 (name 16 16 :left) (size 6 -1 :right))))
112 to your ~/.emacs file.
114 Using \\[ibuffer-switch-format], you can rotate the display between
115 the specified formats in the list."
116 :type '(repeat sexp)
117 :group 'ibuffer)
119 (defcustom ibuffer-always-compile-formats (featurep 'bytecomp)
120 "If non-nil, then use the byte-compiler to optimize `ibuffer-formats'.
121 This will increase the redisplay speed, at the cost of loading the
122 elisp byte-compiler."
123 :type 'boolean
124 :group 'ibuffer)
126 (defcustom ibuffer-fontification-alist
127 `((10 buffer-read-only font-lock-constant-face)
128 (15 (string-match "^*" (buffer-name)) font-lock-keyword-face)
129 (20 (and (string-match "^ " (buffer-name))
130 (null buffer-file-name))
131 italic)
132 (25 (memq major-mode ibuffer-help-buffer-modes) font-lock-comment-face)
133 (30 (eq major-mode 'dired-mode) font-lock-function-name-face))
134 "An alist describing how to fontify buffers.
135 Each element should be of the form (PRIORITY FORM FACE), where
136 PRIORITY is an integer, FORM is an arbitrary form to evaluate in the
137 buffer, and FACE is the face to use for fontification. If the FORM
138 evaluates to non-nil, then FACE will be put on the buffer name. The
139 element with the highest PRIORITY takes precedence.
141 If you change this variable, you must kill the Ibuffer buffer and
142 recreate it for the change to take effect."
143 :type '(repeat
144 (list (integer :tag "Priority")
145 (sexp :tag "Test Form")
146 face))
147 :group 'ibuffer)
149 (defcustom ibuffer-use-other-window nil
150 "If non-nil, display Ibuffer in another window by default."
151 :type 'boolean
152 :group 'ibuffer)
154 (defcustom ibuffer-default-shrink-to-minimum-size nil
155 "If non-nil, minimize the size of the Ibuffer window by default."
156 :type 'boolean
157 :group 'ibuffer)
158 (defvar ibuffer-shrink-to-minimum-size nil)
160 (defcustom ibuffer-display-summary t
161 "If non-nil, summarize Ibuffer columns."
162 :type 'boolean
163 :group 'ibuffer)
165 (defcustom ibuffer-truncate-lines t
166 "If non-nil, do not display continuation lines."
167 :type 'boolean
168 :group 'ibuffer)
170 (defcustom ibuffer-case-fold-search case-fold-search
171 "If non-nil, ignore case when searching."
172 :type 'boolean
173 :group 'ibuffer)
175 (defcustom ibuffer-default-sorting-mode 'recency
176 "The criteria by which to sort the buffers.
178 Note that this variable is local to each Ibuffer buffer. Thus, you
179 can have multiple Ibuffer buffers open, each with a different sorted
180 view of the buffers."
181 :type '(choice (const :tag "Last view time" :value recency)
182 (const :tag "Lexicographic" :value alphabetic)
183 (const :tag "Buffer size" :value size)
184 (const :tag "Major mode" :value major-mode))
185 :group 'ibuffer)
186 (defvar ibuffer-sorting-mode nil)
188 (defcustom ibuffer-default-sorting-reversep nil
189 "If non-nil, reverse the default sorting order."
190 :type 'boolean
191 :group 'ibuffer)
192 (defvar ibuffer-sorting-reversep nil)
194 (defcustom ibuffer-elide-long-columns nil
195 "If non-nil, then elide column entries which exceed their max length.
196 This variable is deprecated; use the :elide argument of
197 `ibuffer-formats' to elide just certain columns."
198 :type 'boolean
199 :group 'ibuffer)
201 (defcustom ibuffer-eliding-string "..."
202 "The string to use for eliding long columns."
203 :type 'string
204 :group 'ibuffer)
206 (defcustom ibuffer-maybe-show-predicates `(,(lambda (buf)
207 (and (string-match "^ " (buffer-name buf))
208 (null buffer-file-name))))
209 "A list of predicates for buffers to display conditionally.
211 A predicate can be a regexp or a function.
212 If a regexp, then it will be matched against the buffer's name.
213 If a function, it will be called with the buffer as an argument, and
214 should return non-nil if this buffer should be shown.
216 Viewing of buffers hidden because of these predicates is enabled by
217 giving a non-nil prefix argument to `ibuffer-update'. Note that this
218 specialized filtering occurs before real filtering."
219 :type '(repeat (choice regexp function))
220 :group 'ibuffer)
222 (defvar ibuffer-current-format nil)
224 (defcustom ibuffer-movement-cycle t
225 "If non-nil, then forward and backwards movement commands cycle."
226 :type 'boolean
227 :group 'ibuffer)
229 (defcustom ibuffer-modified-char ?*
230 "The character to display for modified buffers."
231 :type 'character
232 :group 'ibuffer)
234 (defcustom ibuffer-read-only-char ?%
235 "The character to display for read-only buffers."
236 :type 'character
237 :group 'ibuffer)
239 (defcustom ibuffer-marked-char ?>
240 "The character to display for marked buffers."
241 :type 'character
242 :group 'ibuffer)
244 (defcustom ibuffer-deletion-char ?D
245 "The character to display for buffers marked for deletion."
246 :type 'character
247 :group 'ibuffer)
249 (defcustom ibuffer-expert nil
250 "If non-nil, don't ask for confirmation of \"dangerous\" operations."
251 :type 'boolean
252 :group 'ibuffer)
254 (defcustom ibuffer-view-ibuffer nil
255 "If non-nil, display the current Ibuffer buffer itself.
256 Note that this has a drawback - the data about the current Ibuffer
257 buffer will most likely be inaccurate. This includes modification
258 state, size, etc."
259 :type 'boolean
260 :group 'ibuffer)
262 (defcustom ibuffer-always-show-last-buffer nil
263 "If non-nil, always display the previous buffer.
264 This variable takes precedence over filtering, and even
265 `ibuffer-never-show-predicates'."
266 :type '(choice (const :tag "Always" :value t)
267 (const :tag "Never" :value nil)
268 (const :tag "Always except minibuffer" :value :nomini))
269 :group 'ibuffer)
271 (defcustom ibuffer-use-header-line (boundp 'header-line-format)
272 "If non-nil, display a header line containing current filters."
273 :type 'boolean
274 :group 'ibuffer)
276 (defcustom ibuffer-default-directory nil
277 "The default directory to use for a new Ibuffer buffer.
278 If nil, inherit the directory of the buffer in which `ibuffer' was
279 called. Otherwise, this variable should be a string naming a
280 directory, like `default-directory'."
281 :type '(choice (const :tag "Inherit" :value nil)
282 string)
283 :group 'ibuffer)
285 (defcustom ibuffer-help-buffer-modes
286 '(help-mode apropos-mode Info-mode Info-edit-mode)
287 "List of \"Help\" major modes."
288 :type '(repeat function)
289 :group 'ibuffer)
291 (defcustom ibuffer-hook nil
292 "Hook run when `ibuffer' is called."
293 :type 'hook
294 :group 'ibuffer)
295 (defvaralias 'ibuffer-hooks 'ibuffer-hook)
297 (defcustom ibuffer-mode-hook nil
298 "Hook run upon entry into `ibuffer-mode'."
299 :type 'hook
300 :group 'ibuffer)
301 (defvaralias 'ibuffer-mode-hooks 'ibuffer-mode-hook)
303 (defcustom ibuffer-load-hook nil
304 "Hook run when Ibuffer is loaded."
305 :type 'hook
306 :group 'ibuffer)
308 (defcustom ibuffer-marked-face 'font-lock-warning-face
309 "Face used for displaying marked buffers."
310 :type 'face
311 :group 'ibuffer)
313 (defcustom ibuffer-deletion-face 'font-lock-type-face
314 "Face used for displaying buffers marked for deletion."
315 :type 'face
316 :group 'ibuffer)
318 (defcustom ibuffer-title-face 'font-lock-type-face
319 "Face used for the title string."
320 :type 'face
321 :group 'ibuffer)
323 (defcustom ibuffer-filter-group-name-face 'bold
324 "Face used for displaying filtering group names."
325 :type 'face
326 :group 'ibuffer)
328 (defcustom ibuffer-directory-abbrev-alist nil
329 "An alist of file name abbreviations like `directory-abbrev-alist'."
330 :type '(repeat (cons :format "%v"
331 :value ("" . "")
332 (regexp :tag "From")
333 (regexp :tag "To")))
334 :group 'ibuffer)
337 (defvar ibuffer-mode-map nil)
338 (defvar ibuffer-mode-operate-map nil)
339 (defvar ibuffer-mode-groups-popup nil)
340 (unless ibuffer-mode-map
341 (let ((map (make-sparse-keymap))
342 (operate-map (make-sparse-keymap "Operate"))
343 (groups-map (make-sparse-keymap "Filter Groups")))
344 (define-key map (kbd "0") 'digit-argument)
345 (define-key map (kbd "1") 'digit-argument)
346 (define-key map (kbd "2") 'digit-argument)
347 (define-key map (kbd "3") 'digit-argument)
348 (define-key map (kbd "4") 'digit-argument)
349 (define-key map (kbd "5") 'digit-argument)
350 (define-key map (kbd "6") 'digit-argument)
351 (define-key map (kbd "7") 'digit-argument)
352 (define-key map (kbd "8") 'digit-argument)
353 (define-key map (kbd "9") 'digit-argument)
355 (define-key map (kbd "m") 'ibuffer-mark-forward)
356 (define-key map (kbd "t") 'ibuffer-toggle-marks)
357 (define-key map (kbd "u") 'ibuffer-unmark-forward)
358 (define-key map (kbd "=") 'ibuffer-diff-with-file)
359 (define-key map (kbd "j") 'ibuffer-jump-to-buffer)
360 (define-key map (kbd "DEL") 'ibuffer-unmark-backward)
361 (define-key map (kbd "M-DEL") 'ibuffer-unmark-all)
362 (define-key map (kbd "* *") 'ibuffer-unmark-all)
363 (define-key map (kbd "* M") 'ibuffer-mark-by-mode)
364 (define-key map (kbd "* m") 'ibuffer-mark-modified-buffers)
365 (define-key map (kbd "* u") 'ibuffer-mark-unsaved-buffers)
366 (define-key map (kbd "* s") 'ibuffer-mark-special-buffers)
367 (define-key map (kbd "* r") 'ibuffer-mark-read-only-buffers)
368 (define-key map (kbd "* /") 'ibuffer-mark-dired-buffers)
369 (define-key map (kbd "* e") 'ibuffer-mark-dissociated-buffers)
370 (define-key map (kbd "* h") 'ibuffer-mark-help-buffers)
371 (define-key map (kbd ".") 'ibuffer-mark-old-buffers)
373 (define-key map (kbd "d") 'ibuffer-mark-for-delete)
374 (define-key map (kbd "C-d") 'ibuffer-mark-for-delete-backwards)
375 (define-key map (kbd "k") 'ibuffer-mark-for-delete)
376 (define-key map (kbd "x") 'ibuffer-do-kill-on-deletion-marks)
378 ;; immediate operations
379 (define-key map (kbd "n") 'ibuffer-forward-line)
380 (define-key map (kbd "<down>") 'ibuffer-forward-line)
381 (define-key map (kbd "SPC") 'forward-line)
382 (define-key map (kbd "p") 'ibuffer-backward-line)
383 (define-key map (kbd "<up>") 'ibuffer-backward-line)
384 (define-key map (kbd "M-}") 'ibuffer-forward-next-marked)
385 (define-key map (kbd "M-{") 'ibuffer-backwards-next-marked)
386 (define-key map (kbd "l") 'ibuffer-redisplay)
387 (define-key map (kbd "g") 'ibuffer-update)
388 (define-key map "`" 'ibuffer-switch-format)
389 (define-key map "-" 'ibuffer-add-to-tmp-hide)
390 (define-key map "+" 'ibuffer-add-to-tmp-show)
391 (define-key map "b" 'ibuffer-bury-buffer)
392 (define-key map (kbd ",") 'ibuffer-toggle-sorting-mode)
393 (define-key map (kbd "s i") 'ibuffer-invert-sorting)
394 (define-key map (kbd "s a") 'ibuffer-do-sort-by-alphabetic)
395 (define-key map (kbd "s v") 'ibuffer-do-sort-by-recency)
396 (define-key map (kbd "s s") 'ibuffer-do-sort-by-size)
397 (define-key map (kbd "s m") 'ibuffer-do-sort-by-major-mode)
399 (define-key map (kbd "/ m") 'ibuffer-filter-by-mode)
400 (define-key map (kbd "/ M") 'ibuffer-filter-by-used-mode)
401 (define-key map (kbd "/ n") 'ibuffer-filter-by-name)
402 (define-key map (kbd "/ c") 'ibuffer-filter-by-content)
403 (define-key map (kbd "/ e") 'ibuffer-filter-by-predicate)
404 (define-key map (kbd "/ f") 'ibuffer-filter-by-filename)
405 (define-key map (kbd "/ >") 'ibuffer-filter-by-size-gt)
406 (define-key map (kbd "/ <") 'ibuffer-filter-by-size-lt)
407 (define-key map (kbd "/ r") 'ibuffer-switch-to-saved-filters)
408 (define-key map (kbd "/ a") 'ibuffer-add-saved-filters)
409 (define-key map (kbd "/ x") 'ibuffer-delete-saved-filters)
410 (define-key map (kbd "/ d") 'ibuffer-decompose-filter)
411 (define-key map (kbd "/ s") 'ibuffer-save-filters)
412 (define-key map (kbd "/ p") 'ibuffer-pop-filter)
413 (define-key map (kbd "/ !") 'ibuffer-negate-filter)
414 (define-key map (kbd "/ t") 'ibuffer-exchange-filters)
415 (define-key map (kbd "/ TAB") 'ibuffer-exchange-filters)
416 (define-key map (kbd "/ o") 'ibuffer-or-filter)
417 (define-key map (kbd "/ g") 'ibuffer-filters-to-filter-group)
418 (define-key map (kbd "/ P") 'ibuffer-pop-filter-group)
419 (define-key map (kbd "/ D") 'ibuffer-decompose-filter-group)
420 (define-key map (kbd "/ /") 'ibuffer-filter-disable)
422 (define-key map (kbd "M-n") 'ibuffer-forward-filter-group)
423 (define-key map (kbd "<right>") 'ibuffer-forward-filter-group)
424 (define-key map (kbd "M-p") 'ibuffer-backward-filter-group)
425 (define-key map (kbd "<left>") 'ibuffer-backward-filter-group)
426 (define-key map (kbd "M-j") 'ibuffer-jump-to-filter-group)
427 (define-key map (kbd "C-k") 'ibuffer-kill-line)
428 (define-key map (kbd "C-y") 'ibuffer-yank)
429 (define-key map (kbd "/ S") 'ibuffer-save-filter-groups)
430 (define-key map (kbd "/ R") 'ibuffer-switch-to-saved-filter-groups)
431 (define-key map (kbd "/ X") 'ibuffer-delete-saved-filter-groups)
432 (define-key map (kbd "/ \\") 'ibuffer-clear-filter-groups)
434 (define-key map (kbd "q") 'ibuffer-quit)
435 (define-key map (kbd "h") 'describe-mode)
436 (define-key map (kbd "?") 'describe-mode)
438 (define-key map (kbd "% n") 'ibuffer-mark-by-name-regexp)
439 (define-key map (kbd "% m") 'ibuffer-mark-by-mode-regexp)
440 (define-key map (kbd "% f") 'ibuffer-mark-by-file-name-regexp)
442 (define-key map (kbd "C-t") 'ibuffer-visit-tags-table)
444 (define-key map (kbd "|") 'ibuffer-do-shell-command-pipe)
445 (define-key map (kbd "!") 'ibuffer-do-shell-command-file)
446 (define-key map (kbd "~") 'ibuffer-do-toggle-modified)
447 ;; marked operations
448 (define-key map (kbd "A") 'ibuffer-do-view)
449 (define-key map (kbd "D") 'ibuffer-do-delete)
450 (define-key map (kbd "E") 'ibuffer-do-eval)
451 (define-key map (kbd "F") 'ibuffer-do-shell-command-file)
452 (define-key map (kbd "I") 'ibuffer-do-query-replace-regexp)
453 (define-key map (kbd "H") 'ibuffer-do-view-other-frame)
454 (define-key map (kbd "N") 'ibuffer-do-shell-command-pipe-replace)
455 (define-key map (kbd "M") 'ibuffer-do-toggle-modified)
456 (define-key map (kbd "O") 'ibuffer-do-occur)
457 (define-key map (kbd "P") 'ibuffer-do-print)
458 (define-key map (kbd "Q") 'ibuffer-do-query-replace)
459 (define-key map (kbd "R") 'ibuffer-do-rename-uniquely)
460 (define-key map (kbd "S") 'ibuffer-do-save)
461 (define-key map (kbd "T") 'ibuffer-do-toggle-read-only)
462 (define-key map (kbd "U") 'ibuffer-do-replace-regexp)
463 (define-key map (kbd "V") 'ibuffer-do-revert)
464 (define-key map (kbd "W") 'ibuffer-do-view-and-eval)
465 (define-key map (kbd "X") 'ibuffer-do-shell-command-pipe)
467 (define-key map (kbd "k") 'ibuffer-do-kill-lines)
468 (define-key map (kbd "w") 'ibuffer-copy-filename-as-kill)
470 (define-key map (kbd "RET") 'ibuffer-visit-buffer)
471 (define-key map (kbd "e") 'ibuffer-visit-buffer)
472 (define-key map (kbd "f") 'ibuffer-visit-buffer)
473 (define-key map (kbd "C-x C-f") 'ibuffer-find-file)
474 (define-key map (kbd "o") 'ibuffer-visit-buffer-other-window)
475 (define-key map (kbd "C-o") 'ibuffer-visit-buffer-other-window-noselect)
476 (define-key map (kbd "M-o") 'ibuffer-visit-buffer-1-window)
477 (define-key map (kbd "v") 'ibuffer-do-view)
478 (define-key map (kbd "C-x v") 'ibuffer-do-view-horizontally)
479 (define-key map (kbd "C-c C-a") 'ibuffer-auto-mode)
480 (define-key map (kbd "C-x 4 RET") 'ibuffer-visit-buffer-other-window)
481 (define-key map (kbd "C-x 5 RET") 'ibuffer-visit-buffer-other-frame)
483 (define-key map [menu-bar view]
484 (cons "View" (make-sparse-keymap "View")))
486 (define-key-after map [menu-bar view visit-buffer]
487 '(menu-item "View this buffer" ibuffer-visit-buffer))
488 (define-key-after map [menu-bar view visit-buffer-other-window]
489 '(menu-item "View (other window)" ibuffer-visit-buffer-other-window))
490 (define-key-after map [menu-bar view visit-buffer-other-frame]
491 '(menu-item "View (other frame)" ibuffer-visit-buffer-other-frame))
492 (define-key-after map [menu-bar view ibuffer-update]
493 '(menu-item "Update" ibuffer-update
494 :help "Regenerate the list of buffers"))
495 (define-key-after map [menu-bar view switch-format]
496 '(menu-item "Switch display format" ibuffer-switch-format
497 :help "Toggle between available values of `ibuffer-formats'"))
499 (define-key-after map [menu-bar view dashes]
500 '("--"))
502 (define-key-after map [menu-bar view sort]
503 (cons "Sort" (make-sparse-keymap "Sort")))
505 (define-key-after map [menu-bar view sort do-sort-by-major-mode]
506 '(menu-item "Sort by major mode" ibuffer-do-sort-by-major-mode))
507 (define-key-after map [menu-bar view sort do-sort-by-size]
508 '(menu-item "Sort by buffer size" ibuffer-do-sort-by-size))
509 (define-key-after map [menu-bar view sort do-sort-by-alphabetic]
510 '(menu-item "Sort lexicographically" ibuffer-do-sort-by-alphabetic
511 :help "Sort by the alphabetic order of buffer name"))
512 (define-key-after map [menu-bar view sort do-sort-by-recency]
513 '(menu-item "Sort by view time" ibuffer-do-sort-by-recency
514 :help "Sort by the last time the buffer was displayed"))
515 (define-key-after map [menu-bar view sort dashes]
516 '("--"))
517 (define-key-after map [menu-bar view sort invert-sorting]
518 '(menu-item "Reverse sorting order" ibuffer-invert-sorting))
519 (define-key-after map [menu-bar view sort toggle-sorting-mode]
520 '(menu-item "Switch sorting mode" ibuffer-toggle-sorting-mode
521 :help "Switch between the various sorting criteria"))
523 (define-key-after map [menu-bar view filter]
524 (cons "Filter" (make-sparse-keymap "Filter")))
526 (define-key-after map [menu-bar view filter filter-disable]
527 '(menu-item "Disable all filtering" ibuffer-filter-disable
528 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)))
529 (define-key-after map [menu-bar view filter filter-by-mode]
530 '(menu-item "Add filter by major mode..." ibuffer-filter-by-mode))
531 (define-key-after map [menu-bar view filter filter-by-mode]
532 '(menu-item "Add filter by major mode in use..." ibuffer-filter-by-used-mode))
533 (define-key-after map [menu-bar view filter filter-by-name]
534 '(menu-item "Add filter by buffer name..." ibuffer-filter-by-name))
535 (define-key-after map [menu-bar view filter filter-by-filename]
536 '(menu-item "Add filter by filename..." ibuffer-filter-by-filename))
537 (define-key-after map [menu-bar view filter filter-by-size-lt]
538 '(menu-item "Add filter by size less than..." ibuffer-filter-by-size-lt))
539 (define-key-after map [menu-bar view filter filter-by-size-gt]
540 '(menu-item "Add filter by size greater than..." ibuffer-filter-by-size-gt))
541 (define-key-after map [menu-bar view filter filter-by-content]
542 '(menu-item "Add filter by content (regexp)..." ibuffer-filter-by-content))
543 (define-key-after map [menu-bar view filter filter-by-predicate]
544 '(menu-item "Add filter by Lisp predicate..." ibuffer-filter-by-predicate))
545 (define-key-after map [menu-bar view filter pop-filter]
546 '(menu-item "Remove top filter" ibuffer-pop-filter
547 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)))
548 (define-key-after map [menu-bar view filter or-filter]
549 '(menu-item "OR top two filters" ibuffer-or-filter
550 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers
551 (cdr ibuffer-filtering-qualifiers))
552 :help "Create a new filter which is the logical OR of the top two filters"))
553 (define-key-after map [menu-bar view filter negate-filter]
554 '(menu-item "Negate top filter" ibuffer-negate-filter
555 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)))
556 (define-key-after map [menu-bar view filter decompose-filter]
557 '(menu-item "Decompose top filter" ibuffer-decompose-filter
558 :enable (and (featurep 'ibuf-ext) (memq (car ibuffer-filtering-qualifiers) '(or saved not)))
559 :help "Break down a complex filter like OR or NOT"))
560 (define-key-after map [menu-bar view filter exchange-filters]
561 '(menu-item "Swap top two filters" ibuffer-exchange-filters
562 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers
563 (cdr ibuffer-filtering-qualifiers))))
564 (define-key-after map [menu-bar view filter save-filters]
565 '(menu-item "Save current filters permanently..." ibuffer-save-filters
566 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)
567 :help "Use a mnemnonic name to store current filter stack"))
568 (define-key-after map [menu-bar view filter switch-to-saved-filters]
569 '(menu-item "Restore permanently saved filters..." ibuffer-switch-to-saved-filters
570 :enable (and (featurep 'ibuf-ext) ibuffer-saved-filters)
571 :help "Replace current filters with a saved stack"))
572 (define-key-after map [menu-bar view filter add-saved-filters]
573 '(menu-item "Add to permanently saved filters..." ibuffer-add-saved-filters
574 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)
575 :help "Include already saved stack with current filters"))
576 (define-key-after map [menu-bar view filter delete-saved-filters]
577 '(menu-item "Delete permanently saved filters..."
578 ibuffer-delete-saved-filters
579 :enable (and (featurep 'ibuf-ext) ibuffer-saved-filters)))
581 ;; Filter groups
583 (define-key-after groups-map [filters-to-filter-group]
584 '(menu-item "Create filter group from current filters..."
585 ibuffer-filters-to-filter-group
586 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)))
587 (define-key-after groups-map [forward-filter-group]
588 '(menu-item "Move point to the next filter group"
589 ibuffer-forward-filter-group))
590 (define-key-after groups-map [backward-filter-group]
591 '(menu-item "Move point to the previous filter group"
592 ibuffer-backward-filter-group))
593 (define-key-after groups-map [jump-to-filter-group]
594 '(menu-item "Move point to a specific filter group..."
595 ibuffer-jump-to-filter-group))
596 (define-key-after groups-map [kill-filter-group]
597 '(menu-item "Kill filter group named..."
598 ibuffer-kill-filter-group
599 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)))
600 (define-key-after groups-map [yank-filter-group]
601 '(menu-item "Yank last killed filter group before..."
602 ibuffer-yank-filter-group
603 :enable (and (featurep 'ibuf-ext) ibuffer-filter-group-kill-ring)))
604 (define-key-after groups-map [pop-filter-group]
605 '(menu-item "Remove top filter group"
606 ibuffer-pop-filter-group
607 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)))
608 (define-key-after groups-map [clear-filter-groups]
609 '(menu-item "Remove all filter groups"
610 ibuffer-clear-filter-groups
611 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)))
612 (define-key-after groups-map [pop-filter-group]
613 '(menu-item "Decompose filter group..."
614 ibuffer-pop-filter-group
615 :help "\"Unmake\" a filter group"
616 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)))
617 (define-key-after groups-map [save-filter-groups]
618 '(menu-item "Save current filter groups permanently..."
619 ibuffer-save-filter-groups
620 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)
621 :help "Use a mnemnonic name to store current filter groups"))
622 (define-key-after groups-map [switch-to-saved-filter-groups]
623 '(menu-item "Restore permanently saved filters..."
624 ibuffer-switch-to-saved-filter-groups
625 :enable (and (featurep 'ibuf-ext) ibuffer-saved-filter-groups)
626 :help "Replace current filters with a saved stack"))
627 (define-key-after groups-map [delete-saved-filter-groups]
628 '(menu-item "Delete permanently saved filter groups..."
629 ibuffer-delete-saved-filter-groups
630 :enable (and (featurep 'ibuf-ext) ibuffer-saved-filter-groups)))
631 (define-key-after groups-map [set-filter-groups-by-mode]
632 '(menu-item "Set current filter groups to filter by mode"
633 ibuffer-set-filter-groups-by-mode))
635 (define-key-after map [menu-bar view filter-groups]
636 (cons "Filter Groups" groups-map))
638 (define-key-after map [menu-bar view dashes2]
639 '("--"))
640 (define-key-after map [menu-bar view diff-with-file]
641 '(menu-item "Diff with file" ibuffer-diff-with-file
642 :help "View the differences between this buffer and its file"))
643 (define-key-after map [menu-bar view auto-mode]
644 '(menu-item "Toggle Auto Mode" ibuffer-auto-mode
645 :help "Attempt to automatically update the Ibuffer buffer"))
646 (define-key-after map [menu-bar view customize]
647 '(menu-item "Customize Ibuffer" ibuffer-customize
648 :help "Use Custom to customize Ibuffer"))
650 (define-key-after map [menu-bar mark]
651 (cons "Mark" (make-sparse-keymap "Mark")))
653 (define-key-after map [menu-bar mark toggle-marks]
654 '(menu-item "Toggle marks" ibuffer-toggle-marks
655 :help "Unmark marked buffers, and mark unmarked buffers"))
656 (define-key-after map [menu-bar mark mark-forward]
657 '(menu-item "Mark" ibuffer-mark-forward
658 :help "Mark the buffer at point"))
659 (define-key-after map [menu-bar mark unmark-forward]
660 '(menu-item "Unmark" ibuffer-unmark-forward
661 :help "Unmark the buffer at point"))
662 (define-key-after map [menu-bar mark mark-by-mode]
663 '(menu-item "Mark by mode..." ibuffer-mark-by-mode
664 :help "Mark all buffers in a particular major mode"))
665 (define-key-after map [menu-bar mark mark-modified-buffers]
666 '(menu-item "Mark modified buffers" ibuffer-mark-modified-buffers
667 :help "Mark all buffers which have been modified"))
668 (define-key-after map [menu-bar mark mark-unsaved-buffers]
669 '(menu-item "Mark unsaved buffers" ibuffer-mark-unsaved-buffers
670 :help "Mark all buffers which have a file and are modified"))
671 (define-key-after map [menu-bar mark mark-read-only-buffers]
672 '(menu-item "Mark read-only buffers" ibuffer-mark-read-only-buffers
673 :help "Mark all buffers which are read-only"))
674 (define-key-after map [menu-bar mark mark-special-buffers]
675 '(menu-item "Mark special buffers" ibuffer-mark-special-buffers
676 :help "Mark all buffers whose name begins with a *"))
677 (define-key-after map [menu-bar mark mark-dired-buffers]
678 '(menu-item "Mark dired buffers" ibuffer-mark-dired-buffers
679 :help "Mark buffers in dired-mode"))
680 (define-key-after map [menu-bar mark mark-dissociated-buffers]
681 '(menu-item "Mark dissociated buffers" ibuffer-mark-dissociated-buffers
682 :help "Mark buffers with a non-existent associated file"))
683 (define-key-after map [menu-bar mark mark-help-buffers]
684 '(menu-item "Mark help buffers" ibuffer-mark-help-buffers
685 :help "Mark buffers in help-mode"))
686 (define-key-after map [menu-bar mark mark-old-buffers]
687 '(menu-item "Mark old buffers" ibuffer-mark-old-buffers
688 :help "Mark buffers which have not been viewed recently"))
689 (define-key-after map [menu-bar mark unmark-all]
690 '(menu-item "Unmark All" ibuffer-unmark-all))
692 (define-key-after map [menu-bar mark dashes]
693 '("--"))
695 (define-key-after map [menu-bar mark mark-by-name-regexp]
696 '(menu-item "Mark by buffer name (regexp)..." ibuffer-mark-by-name-regexp
697 :help "Mark buffers whose name matches a regexp"))
698 (define-key-after map [menu-bar mark mark-by-mode-regexp]
699 '(menu-item "Mark by major mode (regexp)..." ibuffer-mark-by-mode-regexp
700 :help "Mark buffers whose major mode name matches a regexp"))
701 (define-key-after map [menu-bar mark mark-by-file-name-regexp]
702 '(menu-item "Mark by file name (regexp)..." ibuffer-mark-by-file-name-regexp
703 :help "Mark buffers whose file name matches a regexp"))
705 ;; Operate map is added later
707 (define-key-after operate-map [do-view]
708 '(menu-item "View" ibuffer-do-view))
709 (define-key-after operate-map [do-view-other-frame]
710 '(menu-item "View (separate frame)" ibuffer-do-view-other-frame))
711 (define-key-after operate-map [do-save]
712 '(menu-item "Save" ibuffer-do-save))
713 (define-key-after operate-map [do-replace-regexp]
714 '(menu-item "Replace (regexp)..." ibuffer-do-replace-regexp
715 :help "Replace text inside marked buffers"))
716 (define-key-after operate-map [do-query-replace]
717 '(menu-item "Query Replace..." ibuffer-do-query-replace
718 :help "Replace text in marked buffers, asking each time"))
719 (define-key-after operate-map [do-query-replace-regexp]
720 '(menu-item "Query Replace (regexp)..." ibuffer-do-query-replace-regexp
721 :help "Replace text in marked buffers by regexp, asking each time"))
722 (define-key-after operate-map [do-print]
723 '(menu-item "Print" ibuffer-do-print))
724 (define-key-after operate-map [do-toggle-modified]
725 '(menu-item "Toggle modification flag" ibuffer-do-toggle-modified))
726 (define-key-after operate-map [do-revert]
727 '(menu-item "Revert" ibuffer-do-revert
728 :help "Revert marked buffers to their associated file"))
729 (define-key-after operate-map [do-rename-uniquely]
730 '(menu-item "Rename Uniquely" ibuffer-do-rename-uniquely
731 :help "Rename marked buffers to a new, unique name"))
732 (define-key-after operate-map [do-delete]
733 '(menu-item "Kill" ibuffer-do-delete))
734 (define-key-after operate-map [do-occur]
735 '(menu-item "List lines matching..." ibuffer-do-occur
736 :help "View all lines in marked buffers matching a regexp"))
737 (define-key-after operate-map [do-shell-command-pipe]
738 '(menu-item "Pipe to shell command..." ibuffer-do-shell-command-pipe
739 :help "For each marked buffer, send its contents to a shell command"))
740 (define-key-after operate-map [do-shell-command-pipe-replace]
741 '(menu-item "Pipe to shell command (replace)..." ibuffer-do-shell-command-pipe-replace
742 :help "For each marked buffer, replace its contents with output of shell command"))
743 (define-key-after operate-map [do-shell-command-file]
744 '(menu-item "Shell command on buffer's file..." ibuffer-do-shell-command-file
745 :help "For each marked buffer, run a shell command with its file as argument"))
746 (define-key-after operate-map [do-eval]
747 '(menu-item "Eval..." ibuffer-do-eval
748 :help "Evaluate a Lisp form in each marked buffer"))
749 (define-key-after operate-map [do-view-and-eval]
750 '(menu-item "Eval (viewing buffer)..." ibuffer-do-view-and-eval
751 :help "Evaluate a Lisp form in each marked buffer while viewing it"))
753 (setq ibuffer-mode-map map
754 ibuffer-mode-operate-map operate-map
755 ibuffer-mode-groups-popup (copy-keymap groups-map))))
757 (define-key ibuffer-mode-groups-popup [kill-filter-group]
758 '(menu-item "Kill filter group"
759 ibuffer-kill-line
760 :enable (and (featurep 'ibuf-ext)
761 ibuffer-filter-groups)))
762 (define-key ibuffer-mode-groups-popup [yank-filter-group]
763 '(menu-item "Yank last killed filter group"
764 ibuffer-yank
765 :enable (and (featurep 'ibuf-ext)
766 ibuffer-filter-group-kill-ring)))
768 (defvar ibuffer-name-map
769 (let ((map (make-sparse-keymap)))
770 (define-key map [(mouse-1)] 'ibuffer-mouse-toggle-mark)
771 (define-key map [(mouse-2)] 'ibuffer-mouse-visit-buffer)
772 (define-key map [down-mouse-3] 'ibuffer-mouse-popup-menu)
773 map))
775 (defvar ibuffer-mode-name-map
776 (let ((map (make-sparse-keymap)))
777 (define-key map [(mouse-2)] 'ibuffer-mouse-filter-by-mode)
778 (define-key map (kbd "RET") 'ibuffer-interactive-filter-by-mode)
779 map))
781 (defvar ibuffer-mode-filter-group-map
782 (let ((map (make-sparse-keymap)))
783 (define-key map [(mouse-1)] 'ibuffer-mouse-toggle-mark)
784 (define-key map [(mouse-2)] 'ibuffer-mouse-toggle-filter-group)
785 (define-key map (kbd "RET") 'ibuffer-toggle-filter-group)
786 (define-key map [down-mouse-3] 'ibuffer-mouse-popup-menu)
787 map))
789 (defvar ibuffer-delete-window-on-quit nil
790 "Whether or not to delete the window upon exiting `ibuffer'.")
792 (defvar ibuffer-did-modification nil)
794 (defvar ibuffer-sorting-functions-alist nil
795 "An alist of functions which describe how to sort buffers.
797 Note: You most likely do not want to modify this variable directly;
798 use `define-ibuffer-sorter' instead.
800 The alist elements are constructed like (NAME DESCRIPTION FUNCTION)
801 Where NAME is a symbol describing the sorting method, DESCRIPTION is a
802 short string which will be displayed in the minibuffer and menu, and
803 FUNCTION is a function of two arguments, which will be the buffers to
804 compare.")
806 ;;; Utility functions
807 (defun ibuffer-columnize-and-insert-list (list &optional pad-width)
808 "Insert LIST into the current buffer in as many columns as possible.
809 The maximum number of columns is determined by the current window
810 width and the longest string in LIST."
811 (unless pad-width
812 (setq pad-width 3))
813 (let ((width (window-width))
814 (max (+ (apply #'max (mapcar #'length list))
815 pad-width)))
816 (let ((columns (/ width max)))
817 (when (zerop columns)
818 (setq columns 1))
819 (while list
820 (dotimes (i (1- columns))
821 (insert (concat (car list) (make-string (- max (length (car list)))
822 ? )))
823 (setq list (cdr list)))
824 (when (not (null list))
825 (insert (pop list)))
826 (insert "\n")))))
828 (defsubst ibuffer-current-mark ()
829 (cadr (get-text-property (line-beginning-position)
830 'ibuffer-properties)))
832 (defun ibuffer-mouse-toggle-mark (event)
833 "Toggle the marked status of the buffer chosen with the mouse."
834 (interactive "e")
835 (unwind-protect
836 (let ((pt (save-excursion
837 (mouse-set-point event)
838 (point))))
839 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
840 (ibuffer-toggle-marks it)
841 (goto-char pt)
842 (let ((mark (ibuffer-current-mark)))
843 (setq buffer-read-only nil)
844 (if (eq mark ibuffer-marked-char)
845 (ibuffer-set-mark ? )
846 (ibuffer-set-mark ibuffer-marked-char)))))
847 (setq buffer-read-only t)))
849 (defun ibuffer-find-file (file &optional wildcards)
850 "Like `find-file', but default to the directory of the buffer at point."
851 (interactive
852 (let ((default-directory (let ((buf (ibuffer-current-buffer)))
853 (if (buffer-live-p buf)
854 (with-current-buffer buf
855 default-directory)
856 default-directory))))
857 (list (read-file-name "Find file: " default-directory)
858 current-prefix-arg)))
859 (find-file file (or wildcards (interactive-p))))
861 (defun ibuffer-mouse-visit-buffer (event)
862 "Visit the buffer chosen with the mouse."
863 (interactive "e")
864 (switch-to-buffer
865 (save-excursion
866 (mouse-set-point event)
867 (ibuffer-current-buffer t))))
869 (defun ibuffer-mouse-popup-menu (event)
870 "Display a menu of operations."
871 (interactive "e")
872 (let ((eventpt (posn-point (event-end event)))
873 (origpt (point)))
874 (unwind-protect
875 (if (get-text-property eventpt 'ibuffer-filter-group-name)
876 (progn
877 (goto-char eventpt)
878 (popup-menu ibuffer-mode-groups-popup))
879 (let ((inhibit-read-only t))
880 (ibuffer-save-marks
881 ;; hm. we could probably do this in a better fashion
882 (ibuffer-unmark-all ?\r)
883 (save-excursion
884 (goto-char eventpt)
885 (ibuffer-set-mark ibuffer-marked-char))
886 (save-excursion
887 (popup-menu ibuffer-mode-operate-map)))))
888 (setq buffer-read-only t)
889 (if (= eventpt (point))
890 (goto-char origpt)))))
892 (defun ibuffer-skip-properties (props direction)
893 (while (and (not (eobp))
894 (let ((hit nil))
895 (dolist (prop props hit)
896 (when (get-text-property (point) prop)
897 (setq hit t)))))
898 (forward-line direction)
899 (beginning-of-line)))
901 (defun ibuffer-customize ()
902 "Begin customizing Ibuffer interactively."
903 (interactive)
904 (customize-group 'ibuffer))
906 (defun ibuffer-backward-line (&optional arg skip-group-names)
907 "Move backwards ARG lines, wrapping around the list if necessary."
908 (interactive "P")
909 (or arg (setq arg 1))
910 (beginning-of-line)
911 (while (> arg 0)
912 (forward-line -1)
913 (when (and ibuffer-movement-cycle
914 (or (get-text-property (point) 'ibuffer-title)
915 (and skip-group-names
916 (get-text-property (point)
917 'ibuffer-filter-group-name))))
918 (goto-char (point-max))
919 (beginning-of-line))
920 (ibuffer-skip-properties (append '(ibuffer-summary)
921 (when skip-group-names
922 '(ibuffer-filter-group-name)))
924 ;; Handle the special case of no buffers.
925 (when (get-text-property (point) 'ibuffer-title)
926 (forward-line 1)
927 (setq arg 1))
928 (decf arg)))
930 (defun ibuffer-forward-line (&optional arg skip-group-names)
931 "Move forward ARG lines, wrapping around the list if necessary."
932 (interactive "P")
933 (or arg (setq arg 1))
934 (beginning-of-line)
935 (when (and ibuffer-movement-cycle
936 (or (eobp)
937 (get-text-property (point) 'ibuffer-summary)))
938 (goto-char (point-min)))
939 (when (or (get-text-property (point) 'ibuffer-title)
940 (and skip-group-names
941 (get-text-property (point) 'ibuffer-filter-group-name)))
942 (when (> arg 0)
943 (decf arg))
944 (ibuffer-skip-properties (append '(ibuffer-title)
945 (when skip-group-names
946 '(ibuffer-filter-group-name)))
948 (if (< arg 0)
949 (ibuffer-backward-line (- arg))
950 (while (> arg 0)
951 (forward-line 1)
952 (when (and ibuffer-movement-cycle
953 (or (eobp)
954 (get-text-property (point) 'ibuffer-summary)))
955 (goto-char (point-min)))
956 (decf arg)
957 (ibuffer-skip-properties (append '(ibuffer-title)
958 (when skip-group-names
959 '(ibuffer-filter-group-name)))
960 1))))
962 (defun ibuffer-visit-buffer (&optional single)
963 "Visit the buffer on this line.
964 If optional argument SINGLE is non-nil, then also ensure there is only
965 one window."
966 (interactive "P")
967 (let ((buf (ibuffer-current-buffer t)))
968 (bury-buffer (current-buffer))
969 (switch-to-buffer buf)
970 (when single
971 (delete-other-windows))))
973 (defun ibuffer-visit-buffer-other-window (&optional noselect)
974 "Visit the buffer on this line in another window."
975 (interactive)
976 (let ((buf (ibuffer-current-buffer t)))
977 (bury-buffer (current-buffer))
978 (if noselect
979 (let ((curwin (selected-window)))
980 (pop-to-buffer buf)
981 (select-window curwin))
982 (switch-to-buffer-other-window buf))))
984 (defun ibuffer-visit-buffer-other-window-noselect ()
985 "Visit the buffer on this line in another window, but don't select it."
986 (interactive)
987 (ibuffer-visit-buffer-other-window t))
989 (defun ibuffer-visit-buffer-other-frame ()
990 "Visit the buffer on this line in another frame."
991 (interactive)
992 (let ((buf (ibuffer-current-buffer t)))
993 (bury-buffer (current-buffer))
994 (switch-to-buffer-other-frame buf)))
996 (defun ibuffer-visit-buffer-1-window ()
997 "Visit the buffer on this line, and delete other windows."
998 (interactive)
999 (ibuffer-visit-buffer t))
1001 (defun ibuffer-bury-buffer ()
1002 "Bury the buffer on this line."
1003 (interactive)
1004 (let ((buf (ibuffer-current-buffer t))
1005 (line (+ 1 (count-lines 1 (point)))))
1006 (bury-buffer buf)
1007 (ibuffer-update nil t)
1008 (goto-line line)))
1010 (defun ibuffer-visit-tags-table ()
1011 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
1012 (interactive)
1013 (let ((file (buffer-file-name (ibuffer-current-buffer t))))
1014 (if file
1015 (visit-tags-table file)
1016 (error "Specified buffer has no file"))))
1018 (defun ibuffer-do-view (&optional other-frame)
1019 "View marked buffers, or the buffer on the current line.
1020 If optional argument OTHER-FRAME is non-nil, then display each
1021 marked buffer in a new frame. Otherwise, display each buffer as
1022 a new window in the current frame, splitting vertically."
1023 (interactive)
1024 (ibuffer-do-view-1 (if other-frame 'other-frame 'vertically)))
1026 (defun ibuffer-do-view-horizontally (&optional other-frame)
1027 "As `ibuffer-do-view', but split windows horizontally."
1028 (interactive)
1029 (ibuffer-do-view-1 (if other-frame 'other-frame 'horizontally)))
1031 (defun ibuffer-do-view-1 (type)
1032 (let ((marked-bufs (ibuffer-get-marked-buffers)))
1033 (when (null marked-bufs)
1034 (setq marked-bufs (list (ibuffer-current-buffer t))))
1035 (unless (and (eq type 'other-frame)
1036 (not ibuffer-expert)
1037 (> (length marked-bufs) 3)
1038 (not (y-or-n-p (format "Really create a new frame for %s buffers? "
1039 (length marked-bufs)))))
1040 (set-buffer-modified-p nil)
1041 (delete-other-windows)
1042 (switch-to-buffer (pop marked-bufs))
1043 (let ((height (/ (1- (if (eq type 'horizontally) (frame-width)
1044 (frame-height)))
1045 (1+ (length marked-bufs)))))
1046 (mapcar (if (eq type 'other-frame)
1047 #'(lambda (buf)
1048 (let ((curframe (selected-frame)))
1049 (select-frame (make-frame))
1050 (switch-to-buffer buf)
1051 (select-frame curframe)))
1052 #'(lambda (buf)
1053 (split-window nil height (eq type 'horizontally))
1054 (other-window 1)
1055 (switch-to-buffer buf)))
1056 marked-bufs)))))
1058 (defun ibuffer-do-view-other-frame ()
1059 "View each of the marked buffers in a separate frame."
1060 (interactive)
1061 (ibuffer-do-view t))
1063 (defsubst ibuffer-map-marked-lines (func)
1064 (prog1 (ibuffer-map-on-mark ibuffer-marked-char func)
1065 (ibuffer-redisplay t)))
1067 (defun ibuffer-shrink-to-fit (&optional owin)
1068 (fit-window-to-buffer nil (when owin (/ (frame-height)
1069 (length (window-list (selected-frame)))))))
1071 (defun ibuffer-confirm-operation-on (operation names)
1072 "Display a buffer asking whether to perform OPERATION on NAMES."
1073 (or ibuffer-expert
1074 (if (= (length names) 1)
1075 (y-or-n-p (format "Really %s buffer %s? " operation (car names)))
1076 (let ((buf (get-buffer-create "*Ibuffer confirmation*")))
1077 (with-current-buffer buf
1078 (setq buffer-read-only nil)
1079 (erase-buffer)
1080 (ibuffer-columnize-and-insert-list names)
1081 (goto-char (point-min))
1082 (setq buffer-read-only t))
1083 (let ((lastwin (car (last (window-list nil 'nomini)))))
1084 ;; Now attempt to display the buffer...
1085 (save-window-excursion
1086 (select-window lastwin)
1087 ;; The window might be too small to split; in that case,
1088 ;; try a few times to increase its size before giving up.
1089 (let ((attempts 0)
1090 (trying t))
1091 (while trying
1092 (condition-case err
1093 (progn
1094 (split-window)
1095 (setq trying nil))
1096 (error
1097 ;; Handle a failure
1098 (if (or (> (incf attempts) 4)
1099 (and (stringp (cadr err))
1100 ;; This definitely falls in the
1101 ;; ghetto hack category...
1102 (not (string-match "too small" (cadr err)))))
1103 (apply #'signal err)
1104 (enlarge-window 3))))))
1105 (select-window (next-window))
1106 (switch-to-buffer buf)
1107 (unwind-protect
1108 (progn
1109 (fit-window-to-buffer)
1110 (y-or-n-p (format "Really %s %d buffers? "
1111 operation (length names))))
1112 (kill-buffer buf))))))))
1114 (defsubst ibuffer-map-lines-nomodify (function)
1115 "As `ibuffer-map-lines', but don't set the modification flag."
1116 (ibuffer-map-lines function t))
1118 (defun ibuffer-buffer-names-with-mark (mark)
1119 (let ((ibuffer-buffer-names-with-mark-result nil))
1120 (ibuffer-map-lines-nomodify
1121 #'(lambda (buf mk)
1122 (when (char-equal mark mk)
1123 (push (buffer-name buf)
1124 ibuffer-buffer-names-with-mark-result))))
1125 ibuffer-buffer-names-with-mark-result))
1127 (defsubst ibuffer-marked-buffer-names ()
1128 (ibuffer-buffer-names-with-mark ibuffer-marked-char))
1130 (defsubst ibuffer-deletion-marked-buffer-names ()
1131 (ibuffer-buffer-names-with-mark ibuffer-deletion-char))
1133 (defun ibuffer-count-marked-lines (&optional all)
1134 (if all
1135 (ibuffer-map-lines-nomodify
1136 #'(lambda (buf mark)
1137 (not (char-equal mark ? ))))
1138 (ibuffer-map-lines-nomodify
1139 #'(lambda (buf mark)
1140 (char-equal mark ibuffer-marked-char)))))
1142 (defsubst ibuffer-count-deletion-lines ()
1143 (ibuffer-map-lines-nomodify
1144 #'(lambda (buf mark)
1145 (char-equal mark ibuffer-deletion-char))))
1147 (defsubst ibuffer-map-deletion-lines (func)
1148 (ibuffer-map-on-mark ibuffer-deletion-char func))
1150 (define-ibuffer-op ibuffer-do-save ()
1151 "Save marked buffers as with `save-buffer'."
1152 (:complex t
1153 :opstring "saved"
1154 :modifier-p :maybe)
1155 (when (buffer-modified-p buf)
1156 (if (not (with-current-buffer buf
1157 buffer-file-name))
1158 ;; handle the case where we're prompted
1159 ;; for a file name
1160 (save-window-excursion
1161 (switch-to-buffer buf)
1162 (save-buffer))
1163 (with-current-buffer buf
1164 (save-buffer))))
1167 (define-ibuffer-op ibuffer-do-toggle-modified ()
1168 "Toggle modification flag of marked buffers."
1169 (:opstring "(un)marked as modified"
1170 :modifier-p t)
1171 (set-buffer-modified-p (not (buffer-modified-p))))
1173 (define-ibuffer-op ibuffer-do-toggle-read-only ()
1174 "Toggle read only status in marked buffers."
1175 (:opstring "toggled read only status in"
1176 :modifier-p t)
1177 (toggle-read-only))
1179 (define-ibuffer-op ibuffer-do-delete ()
1180 "Kill marked buffers as with `kill-this-buffer'."
1181 (:opstring "killed"
1182 :active-opstring "kill"
1183 :dangerous t
1184 :complex t
1185 :modifier-p t)
1186 (if (kill-buffer buf)
1187 'kill
1188 nil))
1190 (define-ibuffer-op ibuffer-do-kill-on-deletion-marks ()
1191 "Kill buffers marked for deletion as with `kill-this-buffer'."
1192 (:opstring "killed"
1193 :active-opstring "kill"
1194 :dangerous t
1195 :complex t
1196 :mark :deletion
1197 :modifier-p t)
1198 (if (kill-buffer buf)
1199 'kill
1200 nil))
1202 (defun ibuffer-unmark-all (mark)
1203 "Unmark all buffers with mark MARK."
1204 (interactive "cRemove marks (RET means all):")
1205 (if (= (ibuffer-count-marked-lines t) 0)
1206 (message "No buffers marked; use 'm' to mark a buffer")
1207 (cond
1208 ((char-equal mark ibuffer-marked-char)
1209 (ibuffer-map-marked-lines
1210 #'(lambda (buf mark)
1211 (ibuffer-set-mark-1 ? )
1212 t)))
1213 ((char-equal mark ibuffer-deletion-char)
1214 (ibuffer-map-deletion-lines
1215 #'(lambda (buf mark)
1216 (ibuffer-set-mark-1 ? )
1217 t)))
1219 (ibuffer-map-lines
1220 #'(lambda (buf mark)
1221 (when (not (char-equal mark ? ))
1222 (ibuffer-set-mark-1 ? ))
1223 t)))))
1224 (ibuffer-redisplay t))
1226 (defun ibuffer-toggle-marks (&optional group)
1227 "Toggle which buffers are marked.
1228 In other words, unmarked buffers become marked, and marked buffers
1229 become unmarked.
1230 If point is on a group name, then this function operates on that
1231 group."
1232 (interactive)
1233 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
1234 (setq group it))
1235 (let ((count
1236 (ibuffer-map-lines
1237 #'(lambda (buf mark)
1238 (cond ((eq mark ibuffer-marked-char)
1239 (ibuffer-set-mark-1 ? )
1240 nil)
1241 ((eq mark ? )
1242 (ibuffer-set-mark-1 ibuffer-marked-char)
1245 nil)))
1246 nil group)))
1247 (message "%s buffers marked" count))
1248 (ibuffer-redisplay t))
1250 (defun ibuffer-mark-forward (arg)
1251 "Mark the buffer on this line, and move forward ARG lines.
1252 If point is on a group name, this function operates on that group."
1253 (interactive "P")
1254 (ibuffer-mark-interactive arg ibuffer-marked-char 1))
1256 (defun ibuffer-unmark-forward (arg)
1257 "Unmark the buffer on this line, and move forward ARG lines.
1258 If point is on a group name, this function operates on that group."
1259 (interactive "P")
1260 (ibuffer-mark-interactive arg ? 1))
1262 (defun ibuffer-unmark-backward (arg)
1263 "Unmark the buffer on this line, and move backward ARG lines.
1264 If point is on a group name, this function operates on that group."
1265 (interactive "P")
1266 (ibuffer-mark-interactive arg ? -1))
1268 (defun ibuffer-mark-interactive (arg mark movement)
1269 (assert (eq major-mode 'ibuffer-mode))
1270 (or arg (setq arg 1))
1271 (ibuffer-forward-line 0)
1272 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
1273 (progn
1274 (require 'ibuf-ext)
1275 (ibuffer-mark-on-buffer #'identity mark it))
1276 (ibuffer-forward-line 0 t)
1277 (let ((inhibit-read-only t))
1278 (while (> arg 0)
1279 (ibuffer-set-mark mark)
1280 (ibuffer-forward-line movement t)
1281 (setq arg (1- arg))))))
1283 (defun ibuffer-set-mark (mark)
1284 (assert (eq major-mode 'ibuffer-mode))
1285 (let ((inhibit-read-only t))
1286 (ibuffer-set-mark-1 mark)
1287 (setq ibuffer-did-modification t)
1288 (ibuffer-redisplay-current)
1289 (beginning-of-line)))
1291 (defun ibuffer-set-mark-1 (mark)
1292 (let ((beg (line-beginning-position))
1293 (end (line-end-position)))
1294 (put-text-property beg end 'ibuffer-properties
1295 (list (ibuffer-current-buffer)
1296 mark))))
1298 (defun ibuffer-mark-for-delete (arg)
1299 "Mark the buffers on ARG lines forward for deletion.
1300 If point is on a group name, this function operates on that group."
1301 (interactive "P")
1302 (ibuffer-mark-interactive arg ibuffer-deletion-char 1))
1304 (defun ibuffer-mark-for-delete-backwards (arg)
1305 "Mark the buffers on ARG lines backward for deletion.
1306 If point is on a group name, this function operates on that group."
1307 (interactive "P")
1308 (ibuffer-mark-interactive arg ibuffer-deletion-char -1))
1310 (defun ibuffer-current-buffer (&optional must-be-live)
1311 (let ((buf (car (get-text-property (line-beginning-position)
1312 'ibuffer-properties))))
1313 (when must-be-live
1314 (if (bufferp buf)
1315 (unless (buffer-live-p buf)
1316 (error (substitute-command-keys "Buffer %s has been killed; use `\\[ibuffer-update]' to update") buf))
1317 (error "No buffer on this line")))
1318 buf))
1320 (defun ibuffer-active-formats-name ()
1321 (if (boundp 'ibuffer-filter-format-alist)
1322 (let ((ret nil))
1323 (dolist (filter ibuffer-filtering-qualifiers ret)
1324 (let ((val (assq (car filter) ibuffer-filter-format-alist)))
1325 (when val
1326 (setq ret (car filter)))))
1327 (if ret
1329 :ibuffer-formats))
1330 :ibuffer-formats))
1332 (defun ibuffer-current-formats (uncompiledp)
1333 (let* ((name (ibuffer-active-formats-name)))
1334 (ibuffer-check-formats)
1335 (if (eq name :ibuffer-formats)
1336 (if uncompiledp
1337 ibuffer-formats
1338 ibuffer-compiled-formats)
1339 (cadr (assq name
1340 (if uncompiledp
1341 ibuffer-filter-format-alist
1342 ibuffer-compiled-filter-formats))))))
1344 (defun ibuffer-current-format (&optional uncompiledp)
1345 (or ibuffer-current-format
1346 (setq ibuffer-current-format 0))
1347 (nth ibuffer-current-format (ibuffer-current-formats uncompiledp)))
1349 (defun ibuffer-expand-format-entry (form)
1350 (if (or (consp form)
1351 (symbolp form))
1352 (let ((sym (intern (concat "ibuffer-make-column-"
1353 (symbol-name (if (consp form)
1354 (car form)
1355 form))))))
1356 (unless (or (fboundp sym)
1357 (assq sym ibuffer-inline-columns))
1358 (error "Unknown column %s in ibuffer-formats" form))
1359 (let (min max align elide)
1360 (if (consp form)
1361 (setq min (or (nth 1 form) 0)
1362 max (or (nth 2 form) -1)
1363 align (or (nth 3 form) :left)
1364 elide (or (nth 4 form) nil))
1365 (setq min 0
1366 max -1
1367 align :left
1368 elide nil))
1369 (list sym min max align elide)))
1370 form))
1372 (defun ibuffer-compile-make-eliding-form (strvar elide from-end-p)
1373 (let ((ellipsis (propertize ibuffer-eliding-string 'font-lock-face 'bold)))
1374 (if (or elide ibuffer-elide-long-columns)
1375 `(if (> strlen 5)
1376 ,(if from-end-p
1377 `(concat ,ellipsis
1378 (substring ,strvar
1379 (length ibuffer-eliding-string)))
1380 `(concat
1381 (substring ,strvar 0 (- strlen ,(length ellipsis)))
1382 ,ellipsis))
1383 ,strvar)
1384 strvar)))
1386 (defun ibuffer-compile-make-substring-form (strvar maxvar from-end-p)
1387 (if from-end-p
1388 `(substring str
1389 (- strlen ,maxvar))
1390 `(substring ,strvar 0 ,maxvar)))
1392 (defun ibuffer-compile-make-format-form (strvar widthform alignment)
1393 (let* ((left `(make-string tmp2 ? ))
1394 (right `(make-string (- tmp1 tmp2) ? )))
1395 `(progn
1396 (setq tmp1 ,widthform
1397 tmp2 (/ tmp1 2))
1398 ,(case alignment
1399 (:right `(concat ,left ,right ,strvar))
1400 (:center `(concat ,left ,strvar ,right))
1401 (:left `(concat ,strvar ,left ,right))
1402 (t (error "Invalid alignment %s" alignment))))))
1404 (defun ibuffer-compile-format (format)
1405 (let ((result nil)
1406 ;; We use these variables to keep track of which variables
1407 ;; inside the generated function we need to bind, since
1408 ;; binding variables in Emacs takes time.
1409 str-used tmp1-used tmp2-used global-strlen-used)
1410 (dolist (form format)
1411 (push
1412 ;; Generate a form based on a particular format entry, like
1413 ;; " ", mark, or (mode 16 16 :right).
1414 (if (stringp form)
1415 ;; It's a string; all we need to do is insert it.
1416 `(insert ,form)
1417 (let* ((form (ibuffer-expand-format-entry form))
1418 (sym (nth 0 form))
1419 (min (nth 1 form))
1420 (max (nth 2 form))
1421 (align (nth 3 form))
1422 (elide (nth 4 form)))
1423 (let* ((from-end-p (when (minusp min)
1424 (setq min (- min))
1426 (letbindings nil)
1427 (outforms nil)
1428 minform
1429 maxform
1430 min-used max-used strlen-used)
1431 (when (or (not (integerp min)) (>= min 0))
1432 ;; This is a complex case; they want it limited to a
1433 ;; minimum size.
1434 (setq min-used t)
1435 (setq str-used t strlen-used t global-strlen-used t
1436 tmp1-used t tmp2-used t)
1437 ;; Generate code to limit the string to a minimum size.
1438 (setq minform `(progn
1439 (setq str
1440 ,(ibuffer-compile-make-format-form
1441 'str
1442 `(- ,(if (integerp min)
1444 'min)
1445 strlen)
1446 align)))))
1447 (when (or (not (integerp max)) (> max 0))
1448 (setq str-used t max-used t)
1449 ;; Generate code to limit the string to a maximum size.
1450 (setq maxform `(progn
1451 (setq str
1452 ,(ibuffer-compile-make-substring-form
1453 'str
1454 (if (integerp max)
1456 'max)
1457 from-end-p))
1458 (setq strlen (length str))
1459 (setq str
1460 ,(ibuffer-compile-make-eliding-form 'str
1461 elide
1462 from-end-p)))))
1463 ;; Now, put these forms together with the rest of the code.
1464 (let ((callform
1465 ;; Is this an "inline" column? This means we have
1466 ;; to get the code from the
1467 ;; `ibuffer-inline-columns' alist and insert it
1468 ;; into our generated code. Otherwise, we just
1469 ;; generate a call to the column function.
1470 (ibuffer-aif (assq sym ibuffer-inline-columns)
1471 (nth 1 it)
1472 `(,sym buffer mark)))
1473 ;; You're not expected to understand this. Hell, I
1474 ;; don't even understand it, and I wrote it five
1475 ;; minutes ago.
1476 (insertgenfn (ibuffer-aif (get sym 'ibuffer-column-summarizer)
1477 ;; I really, really wish Emacs Lisp had closures.
1478 (lambda (arg sym)
1479 `(insert
1480 (let ((ret ,arg))
1481 (put ',sym 'ibuffer-column-summary
1482 (cons ret (get ',sym 'ibuffer-column-summary)))
1483 ret)))
1484 (lambda (arg sym)
1485 `(insert ,arg))))
1486 (mincompform `(< strlen ,(if (integerp min)
1488 'min)))
1489 (maxcompform `(> strlen ,(if (integerp max)
1491 'max))))
1492 (if (or min-used max-used)
1493 ;; The complex case, where we have to limit the
1494 ;; form to a maximum or minimum size.
1495 (progn
1496 (when (and min-used (not (integerp min)))
1497 (push `(min ,min) letbindings))
1498 (when (and max-used (not (integerp max)))
1499 (push `(max ,max) letbindings))
1500 (push
1501 (if (and min-used max-used)
1502 `(if ,mincompform
1503 ,minform
1504 (if ,maxcompform
1505 ,maxform))
1506 (if min-used
1507 `(when ,mincompform
1508 ,minform)
1509 `(when ,maxcompform
1510 ,maxform)))
1511 outforms)
1512 (push (append
1513 `(setq str ,callform)
1514 (when strlen-used
1515 `(strlen (length str))))
1516 outforms)
1517 (setq outforms
1518 (append outforms (list (funcall insertgenfn 'str sym)))))
1519 ;; The simple case; just insert the string.
1520 (push (funcall insertgenfn callform sym) outforms))
1521 ;; Finally, return a `let' form which binds the
1522 ;; variables in `letbindings', and contains all the
1523 ;; code in `outforms'.
1524 `(let ,letbindings
1525 ,@outforms)))))
1526 result))
1527 (setq result
1528 ;; We don't want to unconditionally load the byte-compiler.
1529 (funcall (if (or ibuffer-always-compile-formats
1530 (featurep 'bytecomp))
1531 #'byte-compile
1532 #'identity)
1533 ;; Here, we actually create a lambda form which
1534 ;; inserts all the generated forms for each entry
1535 ;; in the format string.
1536 (nconc (list 'lambda '(buffer mark))
1537 `((let ,(append (when str-used
1538 '(str))
1539 (when global-strlen-used
1540 '(strlen))
1541 (when tmp1-used
1542 '(tmp1))
1543 (when tmp2-used
1544 '(tmp2)))
1545 ,@(nreverse result))))))))
1547 (defvar ibuffer-compiled-formats nil)
1548 (defvar ibuffer-cached-formats nil)
1549 (defvar ibuffer-cached-eliding-string nil)
1550 (defvar ibuffer-cached-elide-long-columns 0)
1552 (defun ibuffer-recompile-formats ()
1553 "Recompile `ibuffer-formats'."
1554 (interactive)
1555 (setq ibuffer-compiled-formats
1556 (mapcar #'ibuffer-compile-format ibuffer-formats))
1557 (when (boundp 'ibuffer-filter-format-alist)
1558 (setq ibuffer-compiled-filter-formats
1559 (mapcar #'(lambda (entry)
1560 (cons (car entry)
1561 (mapcar #'(lambda (formats)
1562 (mapcar #'ibuffer-compile-format formats))
1563 (cdr entry))))
1564 ibuffer-filter-format-alist))))
1566 (defun ibuffer-clear-summary-columns (format)
1567 (dolist (form format)
1568 (ibuffer-awhen (and (consp form)
1569 (get (car form) 'ibuffer-column-summarizer))
1570 (put (car form) 'ibuffer-column-summary nil))))
1572 (defun ibuffer-check-formats ()
1573 (when (null ibuffer-formats)
1574 (error "No formats!"))
1575 (let ((ext-loaded (featurep 'ibuf-ext)))
1576 (when (or (null ibuffer-compiled-formats)
1577 (null ibuffer-cached-formats)
1578 (not (eq ibuffer-cached-formats ibuffer-formats))
1579 (null ibuffer-cached-eliding-string)
1580 (not (equal ibuffer-cached-eliding-string ibuffer-eliding-string))
1581 (eql 0 ibuffer-cached-elide-long-columns)
1582 (not (eql ibuffer-cached-elide-long-columns
1583 ibuffer-elide-long-columns))
1584 (and ext-loaded
1585 (not (eq ibuffer-cached-filter-formats
1586 ibuffer-filter-format-alist))
1587 (and ibuffer-filter-format-alist
1588 (null ibuffer-compiled-filter-formats))))
1589 (message "Formats have changed, recompiling...")
1590 (ibuffer-recompile-formats)
1591 (setq ibuffer-cached-formats ibuffer-formats
1592 ibuffer-cached-eliding-string ibuffer-eliding-string
1593 ibuffer-cached-elide-long-columns ibuffer-elide-long-columns)
1594 (when ext-loaded
1595 (setq ibuffer-cached-filter-formats ibuffer-filter-format-alist))
1596 (message "Formats have changed, recompiling...done"))))
1598 (defvar ibuffer-inline-columns nil)
1600 (define-ibuffer-column mark (:name " " :inline t)
1601 (string mark))
1603 (define-ibuffer-column read-only (:name "R" :inline t)
1604 (if buffer-read-only
1605 (string ibuffer-read-only-char)
1606 " "))
1608 (define-ibuffer-column modified (:name "M" :inline t)
1609 (if (buffer-modified-p)
1610 (string ibuffer-modified-char)
1611 " "))
1613 (define-ibuffer-column name
1614 (:inline t
1615 :props
1616 ('mouse-face 'highlight 'keymap ibuffer-name-map
1617 'ibuffer-name-column t
1618 'help-echo '(if tooltip-mode
1619 "mouse-1: mark this buffer\nmouse-2: select this buffer\nmouse-3: operate on this buffer"
1620 "mouse-1: mark buffer mouse-2: select buffer mouse-3: operate"))
1621 :summarizer
1622 (lambda (strings)
1623 (let ((bufs (length strings)))
1624 (cond ((zerop bufs) "No buffers")
1625 ((= 1 bufs) "1 buffer")
1626 (t (format "%s buffers" bufs))))))
1627 (propertize (buffer-name) 'font-lock-face (ibuffer-buffer-name-face buffer mark)))
1629 (define-ibuffer-column size
1630 (:inline t
1631 :summarizer
1632 (lambda (column-strings)
1633 (let ((total 0))
1634 (dolist (string column-strings)
1635 (setq total
1636 ;; like, ewww ...
1637 (+ (float (string-to-int string))
1638 total)))
1639 (format "%.0f" total))))
1640 (format "%s" (buffer-size)))
1642 (define-ibuffer-column mode (:inline t
1643 :props
1644 ('mouse-face 'highlight
1645 'keymap ibuffer-mode-name-map
1646 'help-echo "mouse-2: filter by this mode"))
1647 (format "%s" mode-name))
1649 (define-ibuffer-column process
1650 (:summarizer
1651 (lambda (strings)
1652 (let ((total (length (delete "" strings))))
1653 (cond ((zerop total) "No processes")
1654 ((= 1 total) "1 process")
1655 (t (format "%d processes" total))))))
1656 (ibuffer-aif (get-buffer-process buffer)
1657 (format "(%s %s)" it (process-status it))
1658 ""))
1660 (define-ibuffer-column filename
1661 (:summarizer
1662 (lambda (strings)
1663 (let ((total (length (delete "" strings))))
1664 (cond ((zerop total) "No files")
1665 ((= 1 total) "1 file")
1666 (t (format "%d files" total))))))
1667 (let ((directory-abbrev-alist ibuffer-directory-abbrev-alist))
1668 (abbreviate-file-name
1669 (or buffer-file-name
1670 (and (boundp 'dired-directory)
1671 dired-directory)
1672 ""))))
1674 (define-ibuffer-column filename-and-process
1675 (:name "Filename/Process"
1676 :summarizer
1677 (lambda (strings)
1678 (setq strings (delete "" strings))
1679 (let ((procs 0)
1680 (files 0))
1681 (dolist (string strings)
1682 (if (string-match "\\(\?:\\`(\[\[:ascii:\]\]\+)\\)" string)
1683 (progn (setq procs (1+ procs))
1684 (if (< (match-end 0) (length string))
1685 (setq files (1+ files))))
1686 (setq files (1+ files))))
1687 (concat (cond ((zerop files) "No files")
1688 ((= 1 files) "1 file")
1689 (t (format "%d files" files)))
1690 ", "
1691 (cond ((zerop procs) "no processes")
1692 ((= 1 procs) "1 process")
1693 (t (format "%d processes" procs)))))))
1694 (let ((proc (get-buffer-process buffer))
1695 (filename (ibuffer-make-column-filename buffer mark)))
1696 (if proc
1697 (concat (propertize (format "(%s %s)" proc (process-status proc))
1698 'font-lock-face 'italic)
1699 (if (> (length filename) 0)
1700 (format " %s" filename)
1701 ""))
1702 filename)))
1704 (defun ibuffer-format-column (str width alignment)
1705 (let ((left (make-string (/ width 2) ? ))
1706 (right (make-string (- width (/ width 2)) ? )))
1707 (case alignment
1708 (:right (concat left right str))
1709 (:center (concat left str right))
1710 (t (concat str left right)))))
1712 (defun ibuffer-buffer-name-face (buf mark)
1713 (cond ((char-equal mark ibuffer-marked-char)
1714 ibuffer-marked-face)
1715 ((char-equal mark ibuffer-deletion-char)
1716 ibuffer-deletion-face)
1718 (let ((level -1)
1719 result)
1720 (dolist (e ibuffer-fontification-alist result)
1721 (when (and (> (car e) level)
1722 (with-current-buffer buf
1723 (eval (nth 1 e))))
1724 (setq level (car e)
1725 result (nth 2 e))))))))
1727 (defun ibuffer-insert-buffer-line (buffer mark format)
1728 "Insert a line describing BUFFER and MARK using FORMAT."
1729 (assert (eq major-mode 'ibuffer-mode))
1730 (let ((beg (point)))
1731 (funcall format buffer mark)
1732 (put-text-property beg (point) 'ibuffer-properties (list buffer mark)))
1733 (insert "\n"))
1735 ;; This function knows a bit too much of the internals. It would be
1736 ;; nice if it was all abstracted away.
1737 (defun ibuffer-redisplay-current ()
1738 (assert (eq major-mode 'ibuffer-mode))
1739 (when (eobp)
1740 (forward-line -1))
1741 (beginning-of-line)
1742 (let ((curformat (mapcar #'ibuffer-expand-format-entry
1743 (ibuffer-current-format t))))
1744 (ibuffer-clear-summary-columns curformat)
1745 (let ((buf (ibuffer-current-buffer)))
1746 (when buf
1747 (let ((mark (ibuffer-current-mark)))
1748 (save-excursion
1749 (delete-region (point) (1+ (line-end-position)))
1750 (ibuffer-insert-buffer-line
1751 buf mark
1752 (ibuffer-current-format)))
1753 (when ibuffer-shrink-to-minimum-size
1754 (ibuffer-shrink-to-fit)))))))
1756 (defun ibuffer-map-on-mark (mark func)
1757 (ibuffer-map-lines
1758 #'(lambda (buf mk)
1759 (if (char-equal mark mk)
1760 (funcall func buf mark)
1761 nil))))
1763 (defun ibuffer-map-lines (function &optional nomodify group)
1764 "Call FUNCTION for each buffer.
1765 Don't set the ibuffer modification flag iff NOMODIFY is non-nil.
1767 If optional argument GROUP is non-nil, then only call FUNCTION on
1768 buffers in filtering group GROUP.
1770 FUNCTION is called with two arguments:
1771 the buffer object itself and the current mark symbol."
1772 (assert (eq major-mode 'ibuffer-mode))
1773 (ibuffer-forward-line 0)
1774 (let* ((orig-target-line (1+ (count-lines (save-excursion
1775 (goto-char (point-min))
1776 (ibuffer-forward-line 0)
1777 (point))
1778 (point))))
1779 (target-line-offset orig-target-line)
1780 (ibuffer-map-lines-total 0)
1781 (ibuffer-map-lines-count 0))
1782 (unwind-protect
1783 (progn
1784 (setq buffer-read-only nil)
1785 (goto-char (point-min))
1786 (ibuffer-forward-line 0 t)
1787 (while (and (not (eobp))
1788 (not (get-text-property (point) 'ibuffer-summary))
1789 (progn
1790 (ibuffer-forward-line 0 t)
1791 (and (not (eobp))
1792 (not (get-text-property (point) 'ibuffer-summary)))))
1793 (let ((result
1794 (if (buffer-live-p (ibuffer-current-buffer))
1795 (when (or (null group)
1796 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group)
1797 (equal group it)))
1798 (save-excursion
1799 (funcall function
1800 (ibuffer-current-buffer)
1801 (ibuffer-current-mark))))
1802 ;; Kill the line if the buffer is dead
1803 'kill)))
1804 ;; A given mapping function should return:
1805 ;; `nil' if it chose not to affect the buffer
1806 ;; `kill' means the remove line from the buffer list
1807 ;; `t' otherwise
1808 (incf ibuffer-map-lines-total)
1809 (cond ((null result)
1810 (forward-line 1))
1811 ((eq result 'kill)
1812 (delete-region (line-beginning-position)
1813 (1+ (line-end-position)))
1814 (incf ibuffer-map-lines-count)
1815 (when (< ibuffer-map-lines-total
1816 orig-target-line)
1817 (decf target-line-offset)))
1819 (incf ibuffer-map-lines-count)
1820 (forward-line 1)))))
1821 ibuffer-map-lines-count)
1822 (progn
1823 (setq buffer-read-only t)
1824 (unless nomodify
1825 (set-buffer-modified-p nil))
1826 (goto-char (point-min))
1827 (ibuffer-forward-line 0)
1828 (ibuffer-forward-line (1- target-line-offset))))))
1830 (defun ibuffer-get-marked-buffers ()
1831 "Return a list of buffer objects currently marked."
1832 (delq nil
1833 (mapcar #'(lambda (e)
1834 (when (eq (cdr e) ibuffer-marked-char)
1835 (car e)))
1836 (ibuffer-current-state-list))))
1838 (defun ibuffer-current-state-list (&optional pos)
1839 "Return a list like (BUF . MARK) of all buffers in an ibuffer.
1840 If POS is non-nil, return a list like (BUF MARK POINT), where POINT is
1841 the value of point at the beginning of the line for that buffer."
1842 (let ((ibuffer-current-state-list-tmp '()))
1843 ;; ah, if only we had closures. I bet this will mysteriously
1844 ;; break later. Don't blame me.
1845 (if pos
1846 (ibuffer-map-lines-nomodify
1847 #'(lambda (buf mark)
1848 (when (buffer-live-p buf)
1849 (push (list buf mark (point)) ibuffer-current-state-list-tmp))))
1850 (ibuffer-map-lines-nomodify
1851 #'(lambda (buf mark)
1852 (when (buffer-live-p buf)
1853 (push (cons buf mark) ibuffer-current-state-list-tmp)))))
1854 (nreverse ibuffer-current-state-list-tmp)))
1856 (defun ibuffer-current-buffers-with-marks (curbufs)
1857 "Return a list like (BUF . MARK) of all open buffers."
1858 (let ((bufs (ibuffer-current-state-list)))
1859 (mapcar #'(lambda (buf) (let ((e (assq buf bufs)))
1860 (if e
1862 (cons buf ? ))))
1863 curbufs)))
1865 (defun ibuffer-buf-matches-predicates (buf predicates)
1866 (let ((hit nil)
1867 (name (buffer-name buf)))
1868 (dolist (pred predicates)
1869 (when (if (stringp pred)
1870 (string-match pred name)
1871 (funcall pred buf))
1872 (setq hit t)))
1873 hit))
1875 (defun ibuffer-filter-buffers (ibuffer-buf last bmarklist all)
1876 (let ((ext-loaded (featurep 'ibuf-ext)))
1877 (delq nil
1878 (mapcar
1879 ;; element should be like (BUFFER . MARK)
1880 #'(lambda (e)
1881 (let* ((buf (car e)))
1882 (when
1883 ;; This takes precedence over anything else
1884 (or (and ibuffer-always-show-last-buffer
1885 (eq last buf))
1886 (funcall (if ext-loaded
1887 #'ibuffer-ext-visible-p
1888 #'ibuffer-visible-p)
1889 buf all ibuffer-buf))
1890 e)))
1891 bmarklist))))
1893 (defun ibuffer-visible-p (buf all &optional ibuffer-buf)
1894 (and (or all
1895 (not
1896 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates)))
1897 (or ibuffer-view-ibuffer
1898 (and ibuffer-buf
1899 (not (eq ibuffer-buf buf))))))
1901 ;; This function is a special case; it's not defined by
1902 ;; `ibuffer-define-sorter'.
1903 (defun ibuffer-do-sort-by-recency ()
1904 "Sort the buffers by last view time."
1905 (interactive)
1906 (setq ibuffer-sorting-mode 'recency)
1907 (ibuffer-redisplay t))
1909 (defun ibuffer-update-format ()
1910 (when (null ibuffer-current-format)
1911 (setq ibuffer-current-format 0))
1912 (when (null ibuffer-formats)
1913 (error "Ibuffer error: no formats!")))
1915 (defun ibuffer-switch-format ()
1916 "Switch the current display format."
1917 (interactive)
1918 (assert (eq major-mode 'ibuffer-mode))
1919 (unless (consp ibuffer-formats)
1920 (error "Ibuffer error: No formats!"))
1921 (setq ibuffer-current-format
1922 (if (>= ibuffer-current-format (1- (length (ibuffer-current-formats nil))))
1924 (1+ ibuffer-current-format)))
1925 (ibuffer-update-format)
1926 (ibuffer-redisplay t))
1928 (defun ibuffer-update-title-and-summary (format)
1929 (assert (eq major-mode 'ibuffer-mode))
1930 ;; Don't do funky font-lock stuff here
1931 (let ((after-change-functions nil))
1932 (if (get-text-property (point-min) 'ibuffer-title)
1933 (delete-region (point-min)
1934 (next-single-property-change
1935 (point-min) 'ibuffer-title)))
1936 (goto-char (point-min))
1937 (add-text-properties
1938 (point)
1939 (progn
1940 (let ((opos (point)))
1941 ;; Insert the title names.
1942 (dolist (element format)
1943 (insert
1944 (if (stringp element)
1945 element
1946 (let ((sym (car element))
1947 (min (cadr element))
1948 ;; (max (caddr element))
1949 (align (cadddr element)))
1950 ;; Ignore a negative min when we're inserting the title
1951 (when (minusp min)
1952 (setq min (- min)))
1953 (let* ((name (or (get sym 'ibuffer-column-name)
1954 (error "Unknown column %s in ibuffer-formats" sym)))
1955 (len (length name)))
1956 (if (< len min)
1957 (ibuffer-format-column name
1958 (- min len)
1959 align)
1960 name))))))
1961 (add-text-properties opos (point) `(ibuffer-title-header t))
1962 (insert "\n")
1963 ;; Add the underlines
1964 (let ((str (save-excursion
1965 (forward-line -1)
1966 (beginning-of-line)
1967 (buffer-substring (point) (line-end-position)))))
1968 (apply #'insert (mapcar
1969 #'(lambda (c)
1970 (if (not (or (char-equal c ? )
1971 (char-equal c ?\n)))
1973 ? ))
1974 str)))
1975 (insert "\n"))
1976 (point))
1977 `(ibuffer-title t font-lock-face ,ibuffer-title-face))
1978 ;; Now, insert the summary columns.
1979 (goto-char (point-max))
1980 (if (get-text-property (1- (point-max)) 'ibuffer-summary)
1981 (delete-region (previous-single-property-change
1982 (point-max) 'ibuffer-summary)
1983 (point-max)))
1984 (if ibuffer-display-summary
1985 (add-text-properties
1986 (point)
1987 (progn
1988 (insert "\n")
1989 (dolist (element format)
1990 (insert
1991 (if (stringp element)
1992 (make-string (length element) ? )
1993 (let ((sym (car element)))
1994 (let ((min (cadr element))
1995 ;; (max (caddr element))
1996 (align (cadddr element)))
1997 ;; Ignore a negative min when we're inserting the title
1998 (when (minusp min)
1999 (setq min (- min)))
2000 (let* ((summary (if (get sym 'ibuffer-column-summarizer)
2001 (funcall (get sym 'ibuffer-column-summarizer)
2002 (get sym 'ibuffer-column-summary))
2003 (make-string (length (get sym 'ibuffer-column-name))
2004 ? )))
2005 (len (length summary)))
2006 (if (< len min)
2007 (ibuffer-format-column summary
2008 (- min len)
2009 align)
2010 summary)))))))
2011 (point))
2012 `(ibuffer-summary t)))))
2014 (defun ibuffer-update-mode-name ()
2015 (setq mode-name (format "Ibuffer by %s" (if ibuffer-sorting-mode
2016 ibuffer-sorting-mode
2017 "view time")))
2018 (when ibuffer-sorting-reversep
2019 (setq mode-name (concat mode-name " [rev]")))
2020 (when (and (featurep 'ibuf-ext)
2021 ibuffer-auto-mode)
2022 (setq mode-name (concat mode-name " (Auto)")))
2023 (let ((result ""))
2024 (when (featurep 'ibuf-ext)
2025 (dolist (qualifier ibuffer-filtering-qualifiers)
2026 (setq result
2027 (concat result (ibuffer-format-qualifier qualifier))))
2028 (if ibuffer-use-header-line
2029 (setq header-line-format
2030 (when ibuffer-filtering-qualifiers
2031 (replace-regexp-in-string "%" "%%"
2032 (concat mode-name result))))
2033 (progn
2034 (setq mode-name (concat mode-name result))
2035 (when (boundp 'header-line-format)
2036 (setq header-line-format nil)))))))
2038 (defun ibuffer-redisplay (&optional silent)
2039 "Redisplay the current list of buffers.
2040 This does not show new buffers; use `ibuffer-update' for that.
2042 If optional arg SILENT is non-nil, do not display progress messages."
2043 (interactive)
2044 (ibuffer-forward-line 0)
2045 (unless silent
2046 (message "Redisplaying current buffer list..."))
2047 (let ((blist (ibuffer-current-state-list)))
2048 (when (null blist)
2049 (if (and (featurep 'ibuf-ext)
2050 (or ibuffer-filtering-qualifiers ibuffer-hidden-filter-groups))
2051 (message "No buffers! (note: filtering in effect)")
2052 (error "No buffers!")))
2053 (ibuffer-redisplay-engine blist t)
2054 (ibuffer-update-mode-name)
2055 (unless silent
2056 (message "Redisplaying current buffer list...done"))
2057 (ibuffer-forward-line 0)))
2059 (defun ibuffer-update (arg &optional silent)
2060 "Regenerate the list of all buffers.
2061 Display buffers whose name matches one of `ibuffer-maybe-show-predicates'
2062 iff arg ARG is non-nil.
2064 If optional arg SILENT is non-nil, do not display progress messages."
2065 (interactive "P")
2066 (ibuffer-forward-line 0)
2067 (let* ((bufs (buffer-list))
2068 (blist (ibuffer-filter-buffers
2069 (current-buffer)
2070 (if (and
2071 (cadr bufs)
2072 (eq ibuffer-always-show-last-buffer
2073 :nomini)
2074 (minibufferp (cadr bufs)))
2075 (caddr bufs)
2076 (cadr bufs))
2077 (ibuffer-current-buffers-with-marks bufs)
2078 arg)))
2079 (when (null blist)
2080 (if (and (featurep 'ibuf-ext)
2081 ibuffer-filtering-qualifiers)
2082 (message "No buffers! (note: filtering in effect)")
2083 (error "No buffers!")))
2084 (unless silent
2085 (message "Updating buffer list..."))
2086 (ibuffer-redisplay-engine blist arg)
2087 (ibuffer-update-mode-name)
2088 (unless silent
2089 (message "Updating buffer list...done")))
2090 (if (eq ibuffer-shrink-to-minimum-size 'onewindow)
2091 (ibuffer-shrink-to-fit t)
2092 (when ibuffer-shrink-to-minimum-size
2093 (ibuffer-shrink-to-fit)))
2094 (ibuffer-forward-line 0))
2096 (defun ibuffer-sort-bufferlist (bmarklist)
2097 (let* ((sortdat (assq ibuffer-sorting-mode
2098 ibuffer-sorting-functions-alist))
2099 (func (caddr sortdat)))
2100 (let ((result
2101 ;; actually sort the buffers
2102 (if (and sortdat func)
2103 (sort bmarklist func)
2104 bmarklist)))
2105 ;; perhaps reverse the sorted buffer list
2106 (if ibuffer-sorting-reversep
2107 (nreverse result)
2108 result))))
2110 (defun ibuffer-insert-filter-group (name display-name filter-string format bmarklist)
2111 (add-text-properties
2112 (point)
2113 (progn
2114 (insert "[ " display-name " ]")
2115 (point))
2116 `(ibuffer-filter-group-name
2117 ,name
2118 font-lock-face ,ibuffer-filter-group-name-face
2119 keymap ,ibuffer-mode-filter-group-map
2120 mouse-face highlight
2121 help-echo ,(let ((echo '(if tooltip-mode
2122 "mouse-1: toggle marks in this group\nmouse-2: hide/show this filtering group"
2123 "mouse-1: toggle marks mouse-2: hide/show")))
2124 (if (> (length filter-string) 0)
2125 `(concat ,filter-string
2126 (if tooltip-mode "\n" " ")
2127 ,echo)
2128 echo))))
2129 (insert "\n")
2130 (when bmarklist
2131 (put-text-property
2132 (point)
2133 (progn
2134 (dolist (entry bmarklist)
2135 (ibuffer-insert-buffer-line (car entry) (cdr entry) format))
2136 (point))
2137 'ibuffer-filter-group
2138 name)))
2140 (defun ibuffer-redisplay-engine (bmarklist &optional all)
2141 (assert (eq major-mode 'ibuffer-mode))
2142 (let* ((--ibuffer-insert-buffers-and-marks-format
2143 (ibuffer-current-format))
2144 (--ibuffer-expanded-format (mapcar #'ibuffer-expand-format-entry
2145 (ibuffer-current-format t)))
2146 (orig (count-lines (point-min) (point)))
2147 ;; Inhibit font-lock caching tricks, since we're modifying the
2148 ;; entire buffer at once
2149 (after-change-functions nil)
2150 (ext-loaded (featurep 'ibuf-ext))
2151 (bgroups (if ext-loaded
2152 (ibuffer-generate-filter-groups bmarklist)
2153 (list (cons "Default" bmarklist)))))
2154 (ibuffer-clear-summary-columns --ibuffer-expanded-format)
2155 (unwind-protect
2156 (progn
2157 (setq buffer-read-only nil)
2158 (erase-buffer)
2159 (ibuffer-update-format)
2160 (dolist (group (nreverse bgroups))
2161 (let* ((name (car group))
2162 (disabled (and ext-loaded
2163 (member name ibuffer-hidden-filter-groups)))
2164 (bmarklist (cdr group)))
2165 (unless (and (null bmarklist)
2166 ext-loaded
2167 (null ibuffer-show-empty-filter-groups))
2168 (ibuffer-insert-filter-group
2169 name
2170 (if disabled (concat name " ...") name)
2171 (if ext-loaded
2172 (ibuffer-format-filter-group-data name)
2174 --ibuffer-insert-buffers-and-marks-format
2175 (if disabled
2177 (ibuffer-sort-bufferlist bmarklist))))))
2178 (ibuffer-update-title-and-summary --ibuffer-expanded-format))
2179 (setq buffer-read-only t)
2180 (set-buffer-modified-p ibuffer-did-modification)
2181 (setq ibuffer-did-modification nil)
2182 (goto-line (1+ orig)))))
2184 (defun ibuffer-quit ()
2185 "Quit this `ibuffer' session.
2186 Delete the current window iff `ibuffer-delete-window-on-quit' is non-nil."
2187 (interactive)
2188 (if ibuffer-delete-window-on-quit
2189 (progn
2190 (bury-buffer)
2191 (unless (= (count-windows) 1)
2192 (delete-window)))
2193 (bury-buffer)))
2195 ;;;###autoload
2196 (defun ibuffer-list-buffers (&optional files-only)
2197 "Display a list of buffers, in another window.
2198 If optional argument FILES-ONLY is non-nil, then add a filter for
2199 buffers which are visiting a file."
2200 (interactive "P")
2201 (ibuffer t nil (when files-only
2202 '((filename . ".*"))) t))
2204 ;;;###autoload
2205 (defun ibuffer-other-window (&optional files-only)
2206 "Like `ibuffer', but displayed in another window by default.
2207 If optional argument FILES-ONLY is non-nil, then add a filter for
2208 buffers which are visiting a file."
2209 (interactive "P")
2210 (ibuffer t nil (when files-only
2211 '((filename . ".*")))))
2213 ;;;###autoload
2214 (defun ibuffer (&optional other-window-p name qualifiers noselect
2215 shrink filter-groups formats)
2216 "Begin using `ibuffer' to edit a list of buffers.
2217 Type 'h' after entering ibuffer for more information.
2219 Optional argument OTHER-WINDOW-P says to use another window.
2220 Optional argument NAME specifies the name of the buffer; it defaults
2221 to \"*Ibuffer*\".
2222 Optional argument QUALIFIERS is an initial set of filtering qualifiers
2223 to use; see `ibuffer-filtering-qualifiers'.
2224 Optional argument NOSELECT means don't select the Ibuffer buffer.
2225 Optional argument SHRINK means shrink the buffer to minimal size. The
2226 special value `onewindow' means always use another window.
2227 Optional argument FILTER-GROUPS is an initial set of filtering
2228 groups to use; see `ibuffer-filter-groups'.
2229 Optional argument FORMATS is the value to use for `ibuffer-formats'.
2230 If specified, then the variable `ibuffer-formats' will have that value
2231 locally in this buffer."
2232 (interactive "P")
2233 (when ibuffer-use-other-window
2234 (setq other-window-p t))
2235 (let ((buf (get-buffer-create (or name "*Ibuffer*"))))
2236 (if other-window-p
2237 (funcall (if noselect #'(lambda (buf) (display-buffer buf t)) #'pop-to-buffer) buf)
2238 (funcall (if noselect #'display-buffer #'switch-to-buffer) buf))
2239 (with-current-buffer buf
2240 (save-selected-window
2241 ;; We switch to the buffer's window in order to be able
2242 ;; to modify the value of point
2243 (select-window (get-buffer-window buf))
2244 (or (eq major-mode 'ibuffer-mode)
2245 (ibuffer-mode))
2246 (setq ibuffer-delete-window-on-quit other-window-p)
2247 (when shrink
2248 (setq ibuffer-shrink-to-minimum-size shrink))
2249 (when qualifiers
2250 (require 'ibuf-ext)
2251 (setq ibuffer-filtering-qualifiers qualifiers))
2252 (when filter-groups
2253 (require 'ibuf-ext)
2254 (setq ibuffer-filter-groups filter-groups))
2255 (when formats
2256 (set (make-local-variable 'ibuffer-formats) formats))
2257 (ibuffer-update nil)
2258 ;; Skip the group name by default.
2259 (ibuffer-forward-line 0 t)
2260 (unwind-protect
2261 (progn
2262 (setq buffer-read-only nil)
2263 (run-hooks 'ibuffer-hook))
2264 (setq buffer-read-only t))
2265 (unless ibuffer-expert
2266 (message "Commands: m, u, t, RET, g, k, S, D, Q; q to quit; h for help"))))))
2268 (put 'ibuffer-mode 'mode-class 'special)
2269 (defun ibuffer-mode ()
2270 "A major mode for viewing a list of buffers.
2271 In ibuffer, you can conveniently perform many operations on the
2272 currently open buffers, in addition to filtering your view to a
2273 particular subset of them, and sorting by various criteria.
2275 Operations on marked buffers:
2277 '\\[ibuffer-do-save]' - Save the marked buffers
2278 '\\[ibuffer-do-view]' - View the marked buffers in this frame.
2279 '\\[ibuffer-do-view-other-frame]' - View the marked buffers in another frame.
2280 '\\[ibuffer-do-revert]' - Revert the marked buffers.
2281 '\\[ibuffer-do-toggle-read-only]' - Toggle read-only state of marked buffers.
2282 '\\[ibuffer-do-delete]' - Kill the marked buffers.
2283 '\\[ibuffer-do-replace-regexp]' - Replace by regexp in each of the marked
2284 buffers.
2285 '\\[ibuffer-do-query-replace]' - Query replace in each of the marked buffers.
2286 '\\[ibuffer-do-query-replace-regexp]' - As above, with a regular expression.
2287 '\\[ibuffer-do-print]' - Print the marked buffers.
2288 '\\[ibuffer-do-occur]' - List lines in all marked buffers which match
2289 a given regexp (like the function `occur').
2290 '\\[ibuffer-do-shell-command-pipe]' - Pipe the contents of the marked
2291 buffers to a shell command.
2292 '\\[ibuffer-do-shell-command-pipe-replace]' - Replace the contents of the marked
2293 buffers with the output of a shell command.
2294 '\\[ibuffer-do-shell-command-file]' - Run a shell command with the
2295 buffer's file as an argument.
2296 '\\[ibuffer-do-eval]' - Evaluate a form in each of the marked buffers. This
2297 is a very flexible command. For example, if you want to make all
2298 of the marked buffers read only, try using (toggle-read-only 1) as
2299 the input form.
2300 '\\[ibuffer-do-view-and-eval]' - As above, but view each buffer while the form
2301 is evaluated.
2302 '\\[ibuffer-do-kill-lines]' - Remove the marked lines from the *Ibuffer* buffer,
2303 but don't kill the associated buffer.
2304 '\\[ibuffer-do-kill-on-deletion-marks]' - Kill all buffers marked for deletion.
2306 Marking commands:
2308 '\\[ibuffer-mark-forward]' - Mark the buffer at point.
2309 '\\[ibuffer-toggle-marks]' - Unmark all currently marked buffers, and mark
2310 all unmarked buffers.
2311 '\\[ibuffer-unmark-forward]' - Unmark the buffer at point.
2312 '\\[ibuffer-unmark-backward]' - Unmark the buffer at point, and move to the
2313 previous line.
2314 '\\[ibuffer-unmark-all]' - Unmark all marked buffers.
2315 '\\[ibuffer-mark-by-mode]' - Mark buffers by major mode.
2316 '\\[ibuffer-mark-unsaved-buffers]' - Mark all \"unsaved\" buffers.
2317 This means that the buffer is modified, and has an associated file.
2318 '\\[ibuffer-mark-modified-buffers]' - Mark all modified buffers,
2319 regardless of whether or not they have an associated file.
2320 '\\[ibuffer-mark-special-buffers]' - Mark all buffers whose name begins and
2321 ends with '*'.
2322 '\\[ibuffer-mark-dissociated-buffers]' - Mark all buffers which have
2323 an associated file, but that file doesn't currently exist.
2324 '\\[ibuffer-mark-read-only-buffers]' - Mark all read-only buffers.
2325 '\\[ibuffer-mark-dired-buffers]' - Mark buffers in `dired' mode.
2326 '\\[ibuffer-mark-help-buffers]' - Mark buffers in `help-mode', `apropos-mode', etc.
2327 '\\[ibuffer-mark-old-buffers]' - Mark buffers older than `ibuffer-old-time'.
2328 '\\[ibuffer-mark-for-delete]' - Mark the buffer at point for deletion.
2329 '\\[ibuffer-mark-by-name-regexp]' - Mark buffers by their name, using a regexp.
2330 '\\[ibuffer-mark-by-mode-regexp]' - Mark buffers by their major mode, using a regexp.
2331 '\\[ibuffer-mark-by-file-name-regexp]' - Mark buffers by their filename, using a regexp.
2333 Filtering commands:
2335 '\\[ibuffer-filter-by-mode]' - Add a filter by major mode.
2336 '\\[ibuffer-filter-by-used-mode]' - Add a filter by major mode now in use.
2337 '\\[ibuffer-filter-by-name]' - Add a filter by buffer name.
2338 '\\[ibuffer-filter-by-content]' - Add a filter by buffer content.
2339 '\\[ibuffer-filter-by-filename]' - Add a filter by filename.
2340 '\\[ibuffer-filter-by-size-gt]' - Add a filter by buffer size.
2341 '\\[ibuffer-filter-by-size-lt]' - Add a filter by buffer size.
2342 '\\[ibuffer-filter-by-predicate]' - Add a filter by an arbitrary Lisp predicate.
2343 '\\[ibuffer-save-filters]' - Save the current filters with a name.
2344 '\\[ibuffer-switch-to-saved-filters]' - Switch to previously saved filters.
2345 '\\[ibuffer-add-saved-filters]' - Add saved filters to current filters.
2346 '\\[ibuffer-or-filter]' - Replace the top two filters with their logical OR.
2347 '\\[ibuffer-pop-filter]' - Remove the top filter.
2348 '\\[ibuffer-negate-filter]' - Invert the logical sense of the top filter.
2349 '\\[ibuffer-decompose-filter]' - Break down the topmost filter.
2350 '\\[ibuffer-filter-disable]' - Remove all filtering currently in effect.
2352 Filter group commands:
2354 '\\[ibuffer-filters-to-filter-group]' - Create filter group from filters.
2355 '\\[ibuffer-pop-filter-group]' - Remove top filter group.
2356 '\\[ibuffer-forward-filter-group]' - Move to the next filter group.
2357 '\\[ibuffer-backward-filter-group]' - Move to the previous filter group.
2358 '\\[ibuffer-clear-filter-groups]' - Remove all active filter groups.
2359 '\\[ibuffer-save-filter-groups]' - Save the current groups with a name.
2360 '\\[ibuffer-switch-to-saved-filter-groups]' - Restore previously saved groups.
2361 '\\[ibuffer-delete-saved-filter-groups]' - Delete previously saved groups.
2363 Sorting commands:
2365 '\\[ibuffer-toggle-sorting-mode]' - Rotate between the various sorting modes.
2366 '\\[ibuffer-invert-sorting]' - Reverse the current sorting order.
2367 '\\[ibuffer-do-sort-by-alphabetic]' - Sort the buffers lexicographically.
2368 '\\[ibuffer-do-sort-by-recency]' - Sort the buffers by last viewing time.
2369 '\\[ibuffer-do-sort-by-size]' - Sort the buffers by size.
2370 '\\[ibuffer-do-sort-by-major-mode]' - Sort the buffers by major mode.
2372 Other commands:
2374 '\\[ibuffer-switch-format]' - Change the current display format.
2375 '\\[forward-line]' - Move point to the next line.
2376 '\\[previous-line]' - Move point to the previous line.
2377 '\\[ibuffer-update]' - As above, but add new buffers to the list.
2378 '\\[ibuffer-quit]' - Bury the Ibuffer buffer.
2379 '\\[describe-mode]' - This help.
2380 '\\[ibuffer-diff-with-file]' - View the differences between this buffer
2381 and its associated file.
2382 '\\[ibuffer-visit-buffer]' - View the buffer on this line.
2383 '\\[ibuffer-visit-buffer-other-window]' - As above, but in another window.
2384 '\\[ibuffer-visit-buffer-other-window-noselect]' - As both above, but don't select
2385 the new window.
2386 '\\[ibuffer-bury-buffer]' - Bury (not kill!) the buffer on this line.
2388 ** Information on Filtering:
2390 You can filter your ibuffer view via different critera. Each Ibuffer
2391 buffer has its own stack of active filters. For example, suppose you
2392 are working on an Emacs Lisp project. You can create an Ibuffer
2393 buffer displays buffers in just `emacs-lisp' modes via
2394 '\\[ibuffer-filter-by-mode] emacs-lisp-mode RET'. In this case, there
2395 is just one entry on the filtering stack.
2397 You can also combine filters. The various filtering commands push a
2398 new filter onto the stack, and the filters combine to show just
2399 buffers which satisfy ALL criteria on the stack. For example, suppose
2400 you only want to see buffers in `emacs-lisp' mode, whose names begin
2401 with \"gnus\". You can accomplish this via:
2402 '\\[ibuffer-filter-by-mode] emacs-lisp-mode RET
2403 \\[ibuffer-filter-by-name] ^gnus RET'.
2405 Additionally, you can OR the top two filters together with
2406 '\\[ibuffer-or-filters]'. To see all buffers in either
2407 `emacs-lisp-mode' or `lisp-interaction-mode', type:
2409 '\\[ibuffer-filter-by-mode] emacs-lisp-mode RET \\[ibuffer-filter-by-mode] lisp-interaction-mode RET \\[ibuffer-or-filters]'.
2411 Filters can also be saved and restored using mnemonic names: see the
2412 functions `ibuffer-save-filters' and `ibuffer-switch-to-saved-filters'.
2414 To remove the top filter on the stack, use '\\[ibuffer-pop-filter]', and
2415 to disable all filtering currently in effect, use
2416 '\\[ibuffer-filter-disable]'.
2418 ** Filter Groups:
2420 Once one has mastered filters, the next logical step up is \"filter
2421 groups\". A filter group is basically a named group of buffers which
2422 match a filter, which are displayed together in an Ibuffer buffer. To
2423 create a filter group, simply use the regular functions to create a
2424 filter, and then type '\\[ibuffer-filters-to-filter-group]'.
2426 A quick example will make things clearer. Suppose that one wants to
2427 group all of one's Emacs Lisp buffers together. To do this, type
2429 '\\[ibuffer-filter-by-mode] emacs-lisp-mode RET \\[ibuffer-filters-to-filter-group] RET emacs lisp buffers RET'
2431 You may, of course, name the group whatever you want; it doesn't have
2432 to be \"emacs lisp buffers\". Filter groups may be composed of any
2433 arbitrary combination of filters.
2435 Just like filters themselves, filter groups act as a stack. Buffers
2436 will not be displayed multiple times if they would be included in
2437 multiple filter groups; instead, the first filter group is used. The
2438 filter groups are displayed in this order of precedence.
2440 You may rearrange filter groups by using the regular
2441 '\\[ibuffer-kill-line]' and '\\[ibuffer-yank]' pair. Yanked groups
2442 will be inserted before the group at point."
2443 (kill-all-local-variables)
2444 (use-local-map ibuffer-mode-map)
2445 (setq major-mode 'ibuffer-mode)
2446 (setq mode-name "Ibuffer")
2447 (setq buffer-read-only t)
2448 (buffer-disable-undo)
2449 (setq truncate-lines ibuffer-truncate-lines)
2450 ;; This makes things less ugly for Emacs 21 users with a non-nil
2451 ;; `show-trailing-whitespace'.
2452 (setq show-trailing-whitespace nil)
2453 ;; disable `show-paren-mode' buffer-locally
2454 (if (bound-and-true-p show-paren-mode)
2455 (set (make-local-variable 'show-paren-mode) nil))
2456 (set (make-local-variable 'revert-buffer-function)
2457 #'ibuffer-update)
2458 (set (make-local-variable 'ibuffer-sorting-mode)
2459 ibuffer-default-sorting-mode)
2460 (set (make-local-variable 'ibuffer-sorting-reversep)
2461 ibuffer-default-sorting-reversep)
2462 (set (make-local-variable 'ibuffer-shrink-to-minimum-size)
2463 ibuffer-default-shrink-to-minimum-size)
2464 (set (make-local-variable 'ibuffer-filtering-qualifiers) nil)
2465 (set (make-local-variable 'ibuffer-filter-groups) nil)
2466 (set (make-local-variable 'ibuffer-filter-group-kill-ring) nil)
2467 (set (make-local-variable 'ibuffer-hidden-filter-groups) nil)
2468 (set (make-local-variable 'ibuffer-compiled-formats) nil)
2469 (set (make-local-variable 'ibuffer-cached-formats) nil)
2470 (set (make-local-variable 'ibuffer-cached-eliding-string) nil)
2471 (set (make-local-variable 'ibuffer-cached-elide-long-columns) nil)
2472 (set (make-local-variable 'ibuffer-current-format) nil)
2473 (set (make-local-variable 'ibuffer-delete-window-on-quit) nil)
2474 (set (make-local-variable 'ibuffer-did-modification) nil)
2475 (set (make-local-variable 'ibuffer-tmp-hide-regexps) nil)
2476 (set (make-local-variable 'ibuffer-tmp-show-regexps) nil)
2477 (define-key ibuffer-mode-map [menu-bar edit] 'undefined)
2478 (define-key ibuffer-mode-map [menu-bar operate] (cons "Operate" ibuffer-mode-operate-map))
2479 (ibuffer-update-format)
2480 (when ibuffer-default-directory
2481 (setq default-directory ibuffer-default-directory))
2482 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
2483 (run-hooks 'ibuffer-mode-hook)
2484 ;; called after mode hooks to allow the user to add filters
2485 (ibuffer-update-mode-name))
2487 (provide 'ibuffer)
2489 (run-hooks 'ibuffer-load-hook)
2491 ;; Local Variables:
2492 ;; coding: iso-8859-1
2493 ;; End:
2495 ;;; arch-tag: 72581688-0603-4954-b8cf-837c700f62e8
2496 ;;; ibuffer.el ends here