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