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