* lisp/gnus/mm-decode.el (shr-image-map): Compilation fix.
[emacs.git] / lisp / ibuffer.el
blob9a1f3b9a0dfb85178ee0258392a8cef1764b5315
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.
98 or
99 \"STRING\" - A literal string to display.
100 or
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 diff-with-file]
704 '(menu-item "Diff with file" ibuffer-diff-with-file
705 :help "View the differences between this buffer and its file"))
706 (define-key-after map [menu-bar view auto-mode]
707 '(menu-item "Auto Mode" ibuffer-auto-mode
708 :button (:toggle . ibuffer-auto-mode)
709 :help "Attempt to automatically update the Ibuffer buffer"))
710 (define-key-after map [menu-bar view customize]
711 '(menu-item "Customize Ibuffer" ibuffer-customize
712 :help "Use Custom to customize Ibuffer"))
714 (define-key-after map [menu-bar mark]
715 (cons "Mark" (make-sparse-keymap "Mark")))
717 (define-key-after map [menu-bar mark toggle-marks]
718 '(menu-item "Toggle marks" ibuffer-toggle-marks
719 :help "Unmark marked buffers, and mark unmarked buffers"))
720 (define-key-after map [menu-bar mark mark-forward]
721 '(menu-item "Mark" ibuffer-mark-forward
722 :help "Mark the buffer at point"))
723 (define-key-after map [menu-bar mark unmark-forward]
724 '(menu-item "Unmark" ibuffer-unmark-forward
725 :help "Unmark the buffer at point"))
726 (define-key-after map [menu-bar mark mark-by-mode]
727 '(menu-item "Mark by mode..." ibuffer-mark-by-mode
728 :help "Mark all buffers in a particular major mode"))
729 (define-key-after map [menu-bar mark mark-modified-buffers]
730 '(menu-item "Mark modified buffers" ibuffer-mark-modified-buffers
731 :help "Mark all buffers which have been modified"))
732 (define-key-after map [menu-bar mark mark-unsaved-buffers]
733 '(menu-item "Mark unsaved buffers" ibuffer-mark-unsaved-buffers
734 :help "Mark all buffers which have a file and are modified"))
735 (define-key-after map [menu-bar mark mark-read-only-buffers]
736 '(menu-item "Mark read-only buffers" ibuffer-mark-read-only-buffers
737 :help "Mark all buffers which are read-only"))
738 (define-key-after map [menu-bar mark mark-special-buffers]
739 '(menu-item "Mark special buffers" ibuffer-mark-special-buffers
740 :help "Mark all buffers whose name begins with a *"))
741 (define-key-after map [menu-bar mark mark-dired-buffers]
742 '(menu-item "Mark dired buffers" ibuffer-mark-dired-buffers
743 :help "Mark buffers in dired-mode"))
744 (define-key-after map [menu-bar mark mark-dissociated-buffers]
745 '(menu-item "Mark dissociated buffers" ibuffer-mark-dissociated-buffers
746 :help "Mark buffers with a non-existent associated file"))
747 (define-key-after map [menu-bar mark mark-help-buffers]
748 '(menu-item "Mark help buffers" ibuffer-mark-help-buffers
749 :help "Mark buffers in help-mode"))
750 (define-key-after map [menu-bar mark mark-compressed-file-buffers]
751 '(menu-item "Mark compressed file buffers"
752 ibuffer-mark-compressed-file-buffers
753 :help "Mark buffers which have a file that is compressed"))
754 (define-key-after map [menu-bar mark mark-old-buffers]
755 '(menu-item "Mark old buffers" ibuffer-mark-old-buffers
756 :help "Mark buffers which have not been viewed recently"))
757 (define-key-after map [menu-bar mark unmark-all]
758 '(menu-item "Unmark All" ibuffer-unmark-all))
760 (define-key-after map [menu-bar mark dashes]
761 '("--"))
763 (define-key-after map [menu-bar mark mark-by-name-regexp]
764 '(menu-item "Mark by buffer name (regexp)..." ibuffer-mark-by-name-regexp
765 :help "Mark buffers whose name matches a regexp"))
766 (define-key-after map [menu-bar mark mark-by-mode-regexp]
767 '(menu-item "Mark by major mode (regexp)..." ibuffer-mark-by-mode-regexp
768 :help "Mark buffers whose major mode name matches a regexp"))
769 (define-key-after map [menu-bar mark mark-by-file-name-regexp]
770 '(menu-item "Mark by file name (regexp)..."
771 ibuffer-mark-by-file-name-regexp
772 :help "Mark buffers whose file name matches a regexp"))
774 map))
776 (defvar ibuffer-mode-operate-map
777 (let ((operate-map (make-sparse-keymap "Operate")))
778 (define-key-after operate-map [do-view]
779 '(menu-item "View" ibuffer-do-view))
780 (define-key-after operate-map [do-view-other-frame]
781 '(menu-item "View (separate frame)" ibuffer-do-view-other-frame))
782 (define-key-after operate-map [do-save]
783 '(menu-item "Save" ibuffer-do-save))
784 (define-key-after operate-map [do-replace-regexp]
785 '(menu-item "Replace (regexp)..." ibuffer-do-replace-regexp
786 :help "Replace text inside marked buffers"))
787 (define-key-after operate-map [do-query-replace]
788 '(menu-item "Query Replace..." ibuffer-do-query-replace
789 :help "Replace text in marked buffers, asking each time"))
790 (define-key-after operate-map [do-query-replace-regexp]
791 '(menu-item "Query Replace (regexp)..." ibuffer-do-query-replace-regexp
792 :help "Replace text in marked buffers by regexp, asking each time"))
793 (define-key-after operate-map [do-print]
794 '(menu-item "Print" ibuffer-do-print))
795 (define-key-after operate-map [do-toggle-modified]
796 '(menu-item "Toggle modification flag" ibuffer-do-toggle-modified))
797 (define-key-after operate-map [do-revert]
798 '(menu-item "Revert" ibuffer-do-revert
799 :help "Revert marked buffers to their associated file"))
800 (define-key-after operate-map [do-rename-uniquely]
801 '(menu-item "Rename Uniquely" ibuffer-do-rename-uniquely
802 :help "Rename marked buffers to a new, unique name"))
803 (define-key-after operate-map [do-delete]
804 '(menu-item "Kill" ibuffer-do-delete))
805 (define-key-after operate-map [do-occur]
806 '(menu-item "List lines matching..." ibuffer-do-occur
807 :help "View all lines in marked buffers matching a regexp"))
808 (define-key-after operate-map [do-shell-command-pipe]
809 '(menu-item "Pipe to shell command..." ibuffer-do-shell-command-pipe
810 :help "For each marked buffer, send its contents to a shell command"))
811 (define-key-after operate-map [do-shell-command-pipe-replace]
812 '(menu-item "Pipe to shell command (replace)..." ibuffer-do-shell-command-pipe-replace
813 :help "For each marked buffer, replace its contents with output of shell command"))
814 (define-key-after operate-map [do-shell-command-file]
815 '(menu-item "Shell command on buffer's file..." ibuffer-do-shell-command-file
816 :help "For each marked buffer, run a shell command with its file as argument"))
817 (define-key-after operate-map [do-eval]
818 '(menu-item "Eval..." ibuffer-do-eval
819 :help "Evaluate a Lisp form in each marked buffer"))
820 (define-key-after operate-map [do-view-and-eval]
821 '(menu-item "Eval (viewing buffer)..." ibuffer-do-view-and-eval
822 :help "Evaluate a Lisp form in each marked buffer while viewing it"))
824 operate-map))
826 (define-key ibuffer-mode-groups-popup [kill-filter-group]
827 '(menu-item "Kill filter group"
828 ibuffer-kill-line
829 :enable (and (featurep 'ibuf-ext)
830 ibuffer-filter-groups)))
831 (define-key ibuffer-mode-groups-popup [yank-filter-group]
832 '(menu-item "Yank last killed filter group"
833 ibuffer-yank
834 :enable (and (featurep 'ibuf-ext)
835 ibuffer-filter-group-kill-ring)))
837 (defvar ibuffer-name-map
838 (let ((map (make-sparse-keymap)))
839 (define-key map [(mouse-1)] 'ibuffer-mouse-toggle-mark)
840 (define-key map [(mouse-2)] 'ibuffer-mouse-visit-buffer)
841 (define-key map [down-mouse-3] 'ibuffer-mouse-popup-menu)
842 map))
844 (defvar ibuffer-filename/process-header-map
845 (let ((map (make-sparse-keymap)))
846 (define-key map [(mouse-1)] 'ibuffer-do-sort-by-filename/process)
847 map))
849 (defvar ibuffer-mode-name-map
850 (let ((map (make-sparse-keymap)))
851 (define-key map [(mouse-2)] 'ibuffer-mouse-filter-by-mode)
852 (define-key map (kbd "RET") 'ibuffer-interactive-filter-by-mode)
853 map))
855 (defvar ibuffer-name-header-map
856 (let ((map (make-sparse-keymap)))
857 (define-key map [(mouse-1)] 'ibuffer-do-sort-by-alphabetic)
858 map))
860 (defvar ibuffer-size-header-map
861 (let ((map (make-sparse-keymap)))
862 (define-key map [(mouse-1)] 'ibuffer-do-sort-by-size)
863 map))
865 (defvar ibuffer-mode-header-map
866 (let ((map (make-sparse-keymap)))
867 (define-key map [(mouse-1)] 'ibuffer-do-sort-by-major-mode)
868 map))
870 (defvar ibuffer-mode-filter-group-map
871 (let ((map (make-sparse-keymap)))
872 (define-key map [(mouse-1)] 'ibuffer-mouse-toggle-mark)
873 (define-key map [(mouse-2)] 'ibuffer-mouse-toggle-filter-group)
874 (define-key map (kbd "RET") 'ibuffer-toggle-filter-group)
875 (define-key map [down-mouse-3] 'ibuffer-mouse-popup-menu)
876 map))
878 (defvar ibuffer-did-modification nil)
880 (defvar ibuffer-compiled-formats nil)
881 (defvar ibuffer-cached-formats nil)
882 (defvar ibuffer-cached-eliding-string nil)
883 (defvar ibuffer-cached-elide-long-columns 0)
885 (defvar ibuffer-sorting-functions-alist nil
886 "An alist of functions which describe how to sort buffers.
888 Note: You most likely do not want to modify this variable directly;
889 use `define-ibuffer-sorter' instead.
891 The alist elements are constructed like (NAME DESCRIPTION FUNCTION)
892 Where NAME is a symbol describing the sorting method, DESCRIPTION is a
893 short string which will be displayed in the minibuffer and menu, and
894 FUNCTION is a function of two arguments, which will be the buffers to
895 compare.")
897 ;;; Utility functions
898 (defun ibuffer-columnize-and-insert-list (list &optional pad-width)
899 "Insert LIST into the current buffer in as many columns as possible.
900 The maximum number of columns is determined by the current window
901 width and the longest string in LIST."
902 (unless pad-width
903 (setq pad-width 3))
904 (let ((width (window-width))
905 (max (+ (apply #'max (mapcar #'length list))
906 pad-width)))
907 (let ((columns (/ width max)))
908 (when (zerop columns)
909 (setq columns 1))
910 (while list
911 (dotimes (_ (1- columns))
912 (insert (concat (car list) (make-string (- max (length (car list)))
913 ?\s)))
914 (setq list (cdr list)))
915 (when (not (null list))
916 (insert (pop list)))
917 (insert "\n")))))
919 (defsubst ibuffer-current-mark ()
920 (cadr (get-text-property (line-beginning-position)
921 'ibuffer-properties)))
923 (defun ibuffer-mouse-toggle-mark (event)
924 "Toggle the marked status of the buffer chosen with the mouse."
925 (interactive "e")
926 (unwind-protect
927 (let ((pt (save-excursion
928 (mouse-set-point event)
929 (point))))
930 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
931 (ibuffer-toggle-marks it)
932 (goto-char pt)
933 (let ((mark (ibuffer-current-mark)))
934 (setq buffer-read-only nil)
935 (if (eq mark ibuffer-marked-char)
936 (ibuffer-set-mark ?\s)
937 (ibuffer-set-mark ibuffer-marked-char)))))
938 (setq buffer-read-only t)))
940 (defun ibuffer-find-file (file &optional wildcards)
941 "Like `find-file', but default to the directory of the buffer at point."
942 (interactive
943 (let ((default-directory (let ((buf (ibuffer-current-buffer)))
944 (if (buffer-live-p buf)
945 (with-current-buffer buf
946 default-directory)
947 default-directory))))
948 (list (read-file-name "Find file: " default-directory)
949 t)))
950 (find-file file wildcards))
952 (defun ibuffer-mouse-visit-buffer (event)
953 "Visit the buffer chosen with the mouse."
954 (interactive "e")
955 (switch-to-buffer
956 (save-excursion
957 (mouse-set-point event)
958 (ibuffer-current-buffer t))))
960 (defun ibuffer-mouse-popup-menu (event)
961 "Display a menu of operations."
962 (interactive "e")
963 (let ((eventpt (posn-point (event-end event)))
964 (origpt (point)))
965 (unwind-protect
966 (if (get-text-property eventpt 'ibuffer-filter-group-name)
967 (progn
968 (goto-char eventpt)
969 (popup-menu ibuffer-mode-groups-popup))
970 (let ((inhibit-read-only t))
971 (ibuffer-save-marks
972 ;; hm. we could probably do this in a better fashion
973 (ibuffer-unmark-all ?\r)
974 (save-excursion
975 (goto-char eventpt)
976 (ibuffer-set-mark ibuffer-marked-char))
977 (save-excursion
978 (popup-menu ibuffer-mode-operate-map)))))
979 (setq buffer-read-only t)
980 (if (= eventpt (point))
981 (goto-char origpt)))))
983 (defun ibuffer-skip-properties (props direction)
984 (while (and (not (eobp))
985 (let ((hit nil))
986 (dolist (prop props hit)
987 (when (get-text-property (point) prop)
988 (setq hit t)))))
989 (forward-line direction)
990 (beginning-of-line)))
992 (defun ibuffer-customize ()
993 "Begin customizing Ibuffer interactively."
994 (interactive)
995 (customize-group 'ibuffer))
997 (defun ibuffer-backward-line (&optional arg skip-group-names)
998 "Move backwards ARG lines, wrapping around the list if necessary."
999 (interactive "P")
1000 (or arg (setq arg 1))
1001 (beginning-of-line)
1002 (while (> arg 0)
1003 (forward-line -1)
1004 (when (and ibuffer-movement-cycle
1005 (or (get-text-property (point) 'ibuffer-title)
1006 (and skip-group-names
1007 (get-text-property (point)
1008 'ibuffer-filter-group-name))))
1009 (goto-char (point-max))
1010 (beginning-of-line))
1011 (ibuffer-skip-properties (append '(ibuffer-summary)
1012 (when skip-group-names
1013 '(ibuffer-filter-group-name)))
1014 -1)
1015 ;; Handle the special case of no buffers.
1016 (when (get-text-property (point) 'ibuffer-title)
1017 (forward-line 1)
1018 (setq arg 1))
1019 (cl-decf arg)))
1021 (defun ibuffer-forward-line (&optional arg skip-group-names)
1022 "Move forward ARG lines, wrapping around the list if necessary."
1023 (interactive "P")
1024 (or arg (setq arg 1))
1025 (beginning-of-line)
1026 (when (and ibuffer-movement-cycle
1027 (or (eobp)
1028 (get-text-property (point) 'ibuffer-summary)))
1029 (goto-char (point-min)))
1030 (when (or (get-text-property (point) 'ibuffer-title)
1031 (and skip-group-names
1032 (get-text-property (point) 'ibuffer-filter-group-name)))
1033 (when (> arg 0)
1034 (cl-decf arg))
1035 (ibuffer-skip-properties (append '(ibuffer-title)
1036 (when skip-group-names
1037 '(ibuffer-filter-group-name)))
1038 1))
1039 (if (< arg 0)
1040 (ibuffer-backward-line (- arg))
1041 (while (> arg 0)
1042 (forward-line 1)
1043 (when (and ibuffer-movement-cycle
1044 (or (eobp)
1045 (get-text-property (point) 'ibuffer-summary)))
1046 (goto-char (point-min)))
1047 (cl-decf arg)
1048 (ibuffer-skip-properties (append '(ibuffer-title)
1049 (when skip-group-names
1050 '(ibuffer-filter-group-name)))
1051 1))))
1053 (defun ibuffer-visit-buffer (&optional single)
1054 "Visit the buffer on this line.
1055 If optional argument SINGLE is non-nil, then also ensure there is only
1056 one window."
1057 (interactive "P")
1058 (let ((buf (ibuffer-current-buffer t)))
1059 (switch-to-buffer buf)
1060 (when single
1061 (delete-other-windows))))
1063 (defun ibuffer-visit-buffer-other-window (&optional noselect)
1064 "Visit the buffer on this line in another window."
1065 (interactive)
1066 (let ((buf (ibuffer-current-buffer t)))
1067 (bury-buffer (current-buffer))
1068 (if noselect
1069 (let ((curwin (selected-window)))
1070 (pop-to-buffer buf)
1071 (select-window curwin))
1072 (switch-to-buffer-other-window buf))))
1074 (defun ibuffer-visit-buffer-other-window-noselect ()
1075 "Visit the buffer on this line in another window, but don't select it."
1076 (interactive)
1077 (ibuffer-visit-buffer-other-window t))
1079 (defun ibuffer-visit-buffer-other-frame ()
1080 "Visit the buffer on this line in another frame."
1081 (interactive)
1082 (let ((buf (ibuffer-current-buffer t)))
1083 (bury-buffer (current-buffer))
1084 (switch-to-buffer-other-frame buf)))
1086 (defun ibuffer-visit-buffer-1-window ()
1087 "Visit the buffer on this line, and delete other windows."
1088 (interactive)
1089 (ibuffer-visit-buffer t))
1091 (defun ibuffer-bury-buffer ()
1092 "Bury the buffer on this line."
1093 (interactive)
1094 (let ((buf (ibuffer-current-buffer t))
1095 (line (+ 1 (count-lines 1 (point)))))
1096 (bury-buffer buf)
1097 (ibuffer-update nil t)
1098 (goto-char (point-min))
1099 (forward-line (1- line))))
1101 (defun ibuffer-visit-tags-table ()
1102 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
1103 (interactive)
1104 (let ((file (buffer-file-name (ibuffer-current-buffer t))))
1105 (if file
1106 (visit-tags-table file)
1107 (error "Specified buffer has no file"))))
1109 (defun ibuffer-do-view (&optional other-frame)
1110 "View marked buffers, or the buffer on the current line.
1111 If optional argument OTHER-FRAME is non-nil, then display each
1112 marked buffer in a new frame. Otherwise, display each buffer as
1113 a new window in the current frame, splitting vertically."
1114 (interactive)
1115 (ibuffer-do-view-1 (if other-frame 'other-frame 'vertically)))
1117 (defun ibuffer-do-view-horizontally (&optional other-frame)
1118 "As `ibuffer-do-view', but split windows horizontally."
1119 (interactive)
1120 (ibuffer-do-view-1 (if other-frame 'other-frame 'horizontally)))
1122 (defun ibuffer-do-view-1 (type)
1123 (let ((marked-bufs (ibuffer-get-marked-buffers)))
1124 (when (null marked-bufs)
1125 (setq marked-bufs (list (ibuffer-current-buffer t))))
1126 (unless (and (eq type 'other-frame)
1127 (not ibuffer-expert)
1128 (> (length marked-bufs) 3)
1129 (not (y-or-n-p (format "Really create a new frame for %s buffers? "
1130 (length marked-bufs)))))
1131 (set-buffer-modified-p nil)
1132 (delete-other-windows)
1133 (switch-to-buffer (pop marked-bufs))
1134 (let ((height (/ (1- (if (eq type 'horizontally) (frame-width)
1135 (frame-height)))
1136 (1+ (length marked-bufs)))))
1137 (mapcar (if (eq type 'other-frame)
1138 (lambda (buf)
1139 (let ((curframe (selected-frame)))
1140 (select-frame (make-frame))
1141 (switch-to-buffer buf)
1142 (select-frame curframe)))
1143 (lambda (buf)
1144 (split-window nil height (eq type 'horizontally))
1145 (other-window 1)
1146 (switch-to-buffer buf)))
1147 marked-bufs)))))
1149 (defun ibuffer-do-view-other-frame ()
1150 "View each of the marked buffers in a separate frame."
1151 (interactive)
1152 (ibuffer-do-view t))
1154 (defsubst ibuffer-map-marked-lines (func)
1155 (prog1 (ibuffer-map-on-mark ibuffer-marked-char func)
1156 (ibuffer-redisplay t)))
1158 (defun ibuffer-shrink-to-fit (&optional owin)
1159 ;; Make sure that redisplay is performed, otherwise there can be a
1160 ;; bad interaction with code in the window-scroll-functions hook
1161 (redisplay t)
1162 (fit-window-to-buffer nil (when owin (/ (frame-height)
1163 (length (window-list (selected-frame)))))))
1165 (defun ibuffer-confirm-operation-on (operation names)
1166 "Display a buffer asking whether to perform OPERATION on NAMES."
1167 (or ibuffer-expert
1168 (if (= (length names) 1)
1169 (y-or-n-p (format "Really %s buffer %s? " operation (car names)))
1170 (let ((buf (get-buffer-create "*Ibuffer confirmation*")))
1171 (with-current-buffer buf
1172 (setq buffer-read-only nil)
1173 (erase-buffer)
1174 (ibuffer-columnize-and-insert-list names)
1175 (goto-char (point-min))
1176 (setq buffer-read-only t))
1177 (let ((lastwin (car (last (window-list nil 'nomini)))))
1178 ;; Now attempt to display the buffer...
1179 (save-window-excursion
1180 (select-window lastwin)
1181 ;; The window might be too small to split; in that case,
1182 ;; try a few times to increase its size before giving up.
1183 (let ((attempts 0)
1184 (trying t))
1185 (while trying
1186 (condition-case err
1187 (progn
1188 (split-window)
1189 (setq trying nil))
1190 (error
1191 ;; Handle a failure
1192 (if (or (> (cl-incf attempts) 4)
1193 (and (stringp (cadr err))
1194 ;; This definitely falls in the
1195 ;; ghetto hack category...
1196 (not (string-match-p "too small" (cadr err)))))
1197 (signal (car err) (cdr err))
1198 (enlarge-window 3))))))
1199 (select-window (next-window))
1200 (switch-to-buffer buf)
1201 (unwind-protect
1202 (progn
1203 (fit-window-to-buffer)
1204 (y-or-n-p (format "Really %s %d buffers? "
1205 operation (length names))))
1206 (kill-buffer buf))))))))
1208 (defsubst ibuffer-map-lines-nomodify (function)
1209 "As `ibuffer-map-lines', but don't set the modification flag."
1210 (ibuffer-map-lines function t))
1212 (defun ibuffer-buffer-names-with-mark (mark)
1213 (let ((ibuffer-buffer-names-with-mark-result nil))
1214 (ibuffer-map-lines-nomodify
1215 (lambda (buf mk)
1216 (when (char-equal mark mk)
1217 (push (buffer-name buf)
1218 ibuffer-buffer-names-with-mark-result))))
1219 ibuffer-buffer-names-with-mark-result))
1221 (defsubst ibuffer-marked-buffer-names ()
1222 (ibuffer-buffer-names-with-mark ibuffer-marked-char))
1224 (defsubst ibuffer-deletion-marked-buffer-names ()
1225 (ibuffer-buffer-names-with-mark ibuffer-deletion-char))
1227 (defun ibuffer-count-marked-lines (&optional all)
1228 (if all
1229 (ibuffer-map-lines-nomodify
1230 (lambda (_buf mark)
1231 (not (char-equal mark ?\s))))
1232 (ibuffer-map-lines-nomodify
1233 (lambda (_buf mark)
1234 (char-equal mark ibuffer-marked-char)))))
1236 (defsubst ibuffer-count-deletion-lines ()
1237 (ibuffer-map-lines-nomodify
1238 (lambda (_buf mark)
1239 (char-equal mark ibuffer-deletion-char))))
1241 (defsubst ibuffer-map-deletion-lines (func)
1242 (ibuffer-map-on-mark ibuffer-deletion-char func))
1244 (defsubst ibuffer-assert-ibuffer-mode ()
1245 (cl-assert (derived-mode-p 'ibuffer-mode)))
1247 (defun ibuffer-buffer-file-name ()
1248 (or buffer-file-name
1249 (let ((dirname (or (and (boundp 'dired-directory)
1250 (if (stringp dired-directory)
1251 dired-directory
1252 (car dired-directory)))
1253 (bound-and-true-p list-buffers-directory))))
1254 (and dirname (expand-file-name dirname)))))
1256 (define-ibuffer-op ibuffer-do-save ()
1257 "Save marked buffers as with `save-buffer'."
1258 (:complex t
1259 :opstring "saved"
1260 :modifier-p :maybe)
1261 (when (buffer-modified-p buf)
1262 (if (not (with-current-buffer buf
1263 buffer-file-name))
1264 ;; handle the case where we're prompted
1265 ;; for a file name
1266 (save-window-excursion
1267 (switch-to-buffer buf)
1268 (save-buffer))
1269 (with-current-buffer buf
1270 (save-buffer))))
1271 t)
1273 (define-ibuffer-op ibuffer-do-toggle-modified ()
1274 "Toggle modification flag of marked buffers."
1275 (:opstring "(un)marked as modified"
1276 :modifier-p t)
1277 (set-buffer-modified-p (not (buffer-modified-p))))
1279 (define-ibuffer-op ibuffer-do-toggle-read-only (&optional _arg);FIXME:arg unused!
1280 "Toggle read only status in marked buffers.
1281 With optional ARG, make read-only only if ARG is not negative."
1282 (:opstring "toggled read only status in"
1283 :interactive "P"
1284 :modifier-p t)
1285 (read-only-mode 'toggle))
1287 (define-ibuffer-op ibuffer-do-delete ()
1288 "Kill marked buffers as with `kill-this-buffer'."
1289 (:opstring "killed"
1290 :active-opstring "kill"
1291 :dangerous t
1292 :complex t
1293 :modifier-p t)
1294 (if (kill-buffer buf)
1295 'kill
1296 nil))
1298 (define-ibuffer-op ibuffer-do-kill-on-deletion-marks ()
1299 "Kill buffers marked for deletion as with `kill-this-buffer'."
1300 (:opstring "killed"
1301 :active-opstring "kill"
1302 :dangerous t
1303 :complex t
1304 :mark :deletion
1305 :modifier-p t)
1306 (if (kill-buffer buf)
1307 'kill
1308 nil))
1310 (defun ibuffer-unmark-all (mark)
1311 "Unmark all buffers with mark MARK."
1312 (interactive "cRemove marks (RET means all):")
1313 (if (= (ibuffer-count-marked-lines t) 0)
1314 (message "No buffers marked; use 'm' to mark a buffer")
1315 (cond
1316 ((char-equal mark ibuffer-marked-char)
1317 (ibuffer-map-marked-lines
1318 (lambda (_buf _mark)
1319 (ibuffer-set-mark-1 ?\s)
1320 t)))
1321 ((char-equal mark ibuffer-deletion-char)
1322 (ibuffer-map-deletion-lines
1323 (lambda (_buf _mark)
1324 (ibuffer-set-mark-1 ?\s)
1325 t)))
1326 (t
1327 (ibuffer-map-lines
1328 (lambda (_buf mark)
1329 (when (not (char-equal mark ?\s))
1330 (ibuffer-set-mark-1 ?\s))
1331 t)))))
1332 (ibuffer-redisplay t))
1334 (defun ibuffer-toggle-marks (&optional group)
1335 "Toggle which buffers are marked.
1336 In other words, unmarked buffers become marked, and marked buffers
1337 become unmarked.
1338 If point is on a group name, then this function operates on that
1339 group."
1340 (interactive)
1341 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
1342 (setq group it))
1343 (let ((count
1344 (ibuffer-map-lines
1345 (lambda (_buf mark)
1346 (cond ((eq mark ibuffer-marked-char)
1347 (ibuffer-set-mark-1 ?\s)
1348 nil)
1349 ((eq mark ?\s)
1350 (ibuffer-set-mark-1 ibuffer-marked-char)
1351 t)
1352 (t
1353 nil)))
1354 nil group)))
1355 (message "%s buffers marked" count))
1356 (ibuffer-redisplay t))
1358 (defsubst ibuffer-get-region-and-prefix ()
1359 (let ((arg (prefix-numeric-value current-prefix-arg)))
1360 (if (use-region-p) (list (region-beginning) (region-end) arg)
1361 (list nil nil arg))))
1363 (defun ibuffer-mark-forward (start end arg)
1364 "Mark the buffers in the region, or ARG buffers.
1365 If point is on a group name, this function operates on that group."
1366 (interactive (ibuffer-get-region-and-prefix))
1367 (ibuffer-mark-region-or-n-with-char start end arg ibuffer-marked-char))
1369 (defun ibuffer-unmark-forward (start end arg)
1370 "Unmark the buffers in the region, or ARG buffers.
1371 If point is on a group name, this function operates on that group."
1372 (interactive (ibuffer-get-region-and-prefix))
1373 (ibuffer-mark-region-or-n-with-char start end arg ?\s))
1375 (defun ibuffer-unmark-backward (arg)
1376 "Unmark the ARG previous buffers.
1377 If point is on a group name, this function operates on that group."
1378 (interactive "p")
1379 (ibuffer-unmark-forward nil nil (- arg)))
1381 (defun ibuffer-mark-region-or-n-with-char (start end arg mark-char)
1382 (if (use-region-p)
1383 (let ((cur (point)) (line-count (count-lines start end)))
1384 (goto-char start)
1385 (ibuffer-mark-interactive line-count mark-char)
1386 (goto-char cur))
1387 (ibuffer-mark-interactive arg mark-char)))
1389 (defun ibuffer-mark-interactive (arg mark &optional movement)
1390 (ibuffer-assert-ibuffer-mode)
1391 (or arg (setq arg 1))
1392 ;; deprecated movement argument
1393 (when (and movement (< movement 0))
1394 (setq arg (- arg)))
1395 (ibuffer-forward-line 0)
1396 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
1397 (progn
1398 (require 'ibuf-ext)
1399 (ibuffer-mark-on-buffer #'identity mark it))
1400 (ibuffer-forward-line 0 t)
1401 (let ((inhibit-read-only t))
1402 (while (> arg 0)
1403 (ibuffer-set-mark mark)
1404 (ibuffer-forward-line 1 t)
1405 (setq arg (1- arg)))
1406 (while (< arg 0)
1407 (ibuffer-forward-line -1 t)
1408 (ibuffer-set-mark mark)
1409 (setq arg (1+ arg))))))
1411 (defun ibuffer-set-mark (mark)
1412 (ibuffer-assert-ibuffer-mode)
1413 (let ((inhibit-read-only t))
1414 (ibuffer-set-mark-1 mark)
1415 (setq ibuffer-did-modification t)
1416 (ibuffer-redisplay-current)
1417 (beginning-of-line)))
1419 (defun ibuffer-set-mark-1 (mark)
1420 (let ((beg (line-beginning-position))
1421 (end (line-end-position)))
1422 (put-text-property beg end 'ibuffer-properties
1423 (list (ibuffer-current-buffer)
1424 mark))))
1426 (defun ibuffer-mark-for-delete (start end arg)
1427 "Mark for deletion the buffers in the region, or ARG buffers.
1428 If point is on a group name, this function operates on that group."
1429 (interactive (ibuffer-get-region-and-prefix))
1430 (ibuffer-mark-region-or-n-with-char start end arg ibuffer-deletion-char))
1432 (defun ibuffer-mark-for-delete-backwards (arg)
1433 "Mark for deletion the ARG previous buffers.
1434 If point is on a group name, this function operates on that group."
1435 (interactive "p")
1436 (ibuffer-mark-interactive arg ibuffer-deletion-char -1))
1438 (defun ibuffer-current-buffer (&optional must-be-live)
1439 (let ((buf (car (get-text-property (line-beginning-position)
1440 'ibuffer-properties))))
1441 (when must-be-live
1442 (if (bufferp buf)
1443 (unless (buffer-live-p buf)
1444 (error "Buffer %s has been killed; %s" buf (substitute-command-keys "use `\\[ibuffer-update]' to update")))
1445 (error "No buffer on this line")))
1446 buf))
1448 (defun ibuffer-active-formats-name ()
1449 (if (boundp 'ibuffer-filter-format-alist)
1450 (let ((ret nil))
1451 (dolist (filter ibuffer-filtering-qualifiers ret)
1452 (let ((val (assq (car filter) ibuffer-filter-format-alist)))
1453 (when val
1454 (setq ret (car filter)))))
1455 (if ret
1456 ret
1457 :ibuffer-formats))
1458 :ibuffer-formats))
1460 (defun ibuffer-current-formats (uncompiledp)
1461 (let* ((name (ibuffer-active-formats-name)))
1462 (ibuffer-check-formats)
1463 (if (eq name :ibuffer-formats)
1464 (if uncompiledp
1465 ibuffer-formats
1466 ibuffer-compiled-formats)
1467 (cadr (assq name
1468 (if uncompiledp
1469 ibuffer-filter-format-alist
1470 ibuffer-compiled-filter-formats))))))
1472 (defun ibuffer-current-format (&optional uncompiledp)
1473 (or ibuffer-current-format
1474 (setq ibuffer-current-format 0))
1475 (nth ibuffer-current-format (ibuffer-current-formats uncompiledp)))
1477 (defun ibuffer-expand-format-entry (form)
1478 (if (or (consp form)
1479 (symbolp form))
1480 (let ((sym (intern (concat "ibuffer-make-column-"
1481 (symbol-name (if (consp form)
1482 (car form)
1483 form))))))
1484 (unless (or (fboundp sym)
1485 (assq sym ibuffer-inline-columns))
1486 (error "Unknown column %s in ibuffer-formats" form))
1487 (let (min max align elide)
1488 (if (consp form)
1489 (setq min (or (nth 1 form) 0)
1490 max (or (nth 2 form) -1)
1491 align (or (nth 3 form) :left)
1492 elide (or (nth 4 form) nil))
1493 (setq min 0
1494 max -1
1495 align :left
1496 elide nil))
1497 (list sym min max align elide)))
1498 form))
1500 (defun ibuffer-compile-make-eliding-form (strvar elide from-end-p)
1501 (let ((ellipsis (propertize ibuffer-eliding-string 'font-lock-face 'bold)))
1502 (if (or elide (with-no-warnings ibuffer-elide-long-columns))
1503 `(if (> strlen 5)
1504 ,(if from-end-p
1505 `(concat ,ellipsis
1506 (substring ,strvar
1507 (length ibuffer-eliding-string)))
1508 `(concat
1509 (substring ,strvar 0 (- strlen ,(length ellipsis)))
1510 ,ellipsis))
1511 ,strvar)
1512 strvar)))
1514 (defun ibuffer-compile-make-substring-form (strvar maxvar from-end-p)
1515 (if from-end-p
1516 `(substring str
1517 (- strlen ,maxvar))
1518 `(substring ,strvar 0 ,maxvar)))
1520 (defun ibuffer-compile-make-format-form (strvar widthform alignment)
1521 (let* ((left `(make-string tmp2 ?\s))
1522 (right `(make-string (- tmp1 tmp2) ?\s)))
1523 `(progn
1524 (setq tmp1 ,widthform
1525 tmp2 (/ tmp1 2))
1526 ,(pcase alignment
1527 (:right `(concat ,left ,right ,strvar))
1528 (:center `(concat ,left ,strvar ,right))
1529 (:left `(concat ,strvar ,left ,right))
1530 (_ (error "Invalid alignment %s" alignment))))))
1532 (defun ibuffer-compile-format (format)
1533 (let ((result nil)
1534 ;; We use these variables to keep track of which variables
1535 ;; inside the generated function we need to bind, since
1536 ;; binding variables in Emacs takes time.
1537 (vars-used ()))
1538 (dolist (form format)
1539 (push
1540 ;; Generate a form based on a particular format entry, like
1541 ;; " ", mark, or (mode 16 16 :right).
1542 (if (stringp form)
1543 ;; It's a string; all we need to do is insert it.
1544 `(insert ,form)
1545 (let* ((form (ibuffer-expand-format-entry form))
1546 (sym (nth 0 form))
1547 (min (nth 1 form))
1548 (max (nth 2 form))
1549 (align (nth 3 form))
1550 (elide (nth 4 form)))
1551 (let* ((from-end-p (when (cl-minusp min)
1552 (setq min (- min))
1553 t))
1554 (letbindings nil)
1555 (outforms nil)
1556 minform
1557 maxform
1558 min-used max-used strlen-used)
1559 (when (or (not (integerp min)) (>= min 0))
1560 ;; This is a complex case; they want it limited to a
1561 ;; minimum size.
1562 (setq min-used t)
1563 (setq strlen-used t)
1564 (setq vars-used '(str strlen tmp1 tmp2))
1565 ;; Generate code to limit the string to a minimum size.
1566 (setq minform `(progn
1567 (setq str
1568 ,(ibuffer-compile-make-format-form
1569 'str
1570 `(- ,(if (integerp min)
1571 min
1572 'min)
1573 strlen)
1574 align)))))
1575 (when (or (not (integerp max)) (> max 0))
1576 (setq max-used t)
1577 (cl-pushnew 'str vars-used)
1578 ;; Generate code to limit the string to a maximum size.
1579 (setq maxform `(progn
1580 (setq str
1581 ,(ibuffer-compile-make-substring-form
1582 'str
1583 (if (integerp max)
1584 max
1585 'max)
1586 from-end-p))
1587 (setq strlen (length str))
1588 (setq str
1589 ,(ibuffer-compile-make-eliding-form
1590 'str elide from-end-p)))))
1591 ;; Now, put these forms together with the rest of the code.
1592 (let ((callform
1593 ;; Is this an "inline" column? This means we have
1594 ;; to get the code from the
1595 ;; `ibuffer-inline-columns' alist and insert it
1596 ;; into our generated code. Otherwise, we just
1597 ;; generate a call to the column function.
1598 (ibuffer-aif (assq sym ibuffer-inline-columns)
1599 (nth 1 it)
1600 `(,sym buffer mark)))
1601 ;; You're not expected to understand this. Hell, I
1602 ;; don't even understand it, and I wrote it five
1603 ;; minutes ago.
1604 (insertgenfn
1605 (if (get sym 'ibuffer-column-summarizer)
1606 ;; I really, really wish Emacs Lisp had closures.
1607 ;; FIXME: Elisp does have them now.
1608 (lambda (arg sym)
1609 `(insert
1610 (let ((ret ,arg))
1611 (put ',sym 'ibuffer-column-summary
1612 (cons ret (get ',sym
1613 'ibuffer-column-summary)))
1614 ret)))
1615 (lambda (arg _sym)
1616 `(insert ,arg))))
1617 (mincompform `(< strlen ,(if (integerp min)
1618 min
1619 'min)))
1620 (maxcompform `(> strlen ,(if (integerp max)
1621 max
1622 'max))))
1623 (if (or min-used max-used)
1624 ;; The complex case, where we have to limit the
1625 ;; form to a maximum or minimum size.
1626 (progn
1627 (when (and min-used (not (integerp min)))
1628 (push `(min ,min) letbindings))
1629 (when (and max-used (not (integerp max)))
1630 (push `(max ,max) letbindings))
1631 (push
1632 (if (and min-used max-used)
1633 `(if ,mincompform
1634 ,minform
1635 (if ,maxcompform
1636 ,maxform))
1637 (if min-used
1638 `(when ,mincompform
1639 ,minform)
1640 `(when ,maxcompform
1641 ,maxform)))
1642 outforms)
1643 (push `(setq str ,callform
1644 ,@(when strlen-used
1645 `(strlen (length str))))
1646 outforms)
1647 (setq outforms
1648 (append outforms
1649 (list (funcall insertgenfn 'str sym)))))
1650 ;; The simple case; just insert the string.
1651 (push (funcall insertgenfn callform sym) outforms))
1652 ;; Finally, return a `let' form which binds the
1653 ;; variables in `letbindings', and contains all the
1654 ;; code in `outforms'.
1655 `(let ,letbindings
1656 ,@outforms)))))
1657 result))
1658 ;; We don't want to unconditionally load the byte-compiler.
1659 (funcall (if (or ibuffer-always-compile-formats
1660 (featurep 'bytecomp))
1661 #'byte-compile
1662 #'identity)
1663 ;; Here, we actually create a lambda form which
1664 ;; inserts all the generated forms for each entry
1665 ;; in the format string.
1666 `(lambda (buffer mark)
1667 (let ,vars-used
1668 ,@(nreverse result))))))
1670 (defun ibuffer-recompile-formats ()
1671 "Recompile `ibuffer-formats'."
1672 (interactive)
1673 (setq ibuffer-compiled-formats
1674 (mapcar #'ibuffer-compile-format ibuffer-formats))
1675 (when (boundp 'ibuffer-filter-format-alist)
1676 (setq ibuffer-compiled-filter-formats
1677 (mapcar (lambda (entry)
1678 (cons (car entry)
1679 (mapcar (lambda (formats)
1680 (mapcar #'ibuffer-compile-format formats))
1681 (cdr entry))))
1682 ibuffer-filter-format-alist))))
1684 (defun ibuffer-clear-summary-columns (format)
1685 (dolist (form format)
1686 (when (and (consp form)
1687 (get (car form) 'ibuffer-column-summarizer))
1688 (put (car form) 'ibuffer-column-summary nil))))
1690 (defun ibuffer-check-formats ()
1691 (when (null ibuffer-formats)
1692 (error "No formats!"))
1693 (let ((ext-loaded (featurep 'ibuf-ext)))
1694 (when (or (null ibuffer-compiled-formats)
1695 (null ibuffer-cached-formats)
1696 (not (eq ibuffer-cached-formats ibuffer-formats))
1697 (null ibuffer-cached-eliding-string)
1698 (not (equal ibuffer-cached-eliding-string ibuffer-eliding-string))
1699 (eql 0 ibuffer-cached-elide-long-columns)
1700 (not (eql ibuffer-cached-elide-long-columns
1701 (with-no-warnings ibuffer-elide-long-columns)))
1702 (and ext-loaded
1703 (not (eq ibuffer-cached-filter-formats
1704 ibuffer-filter-format-alist))
1705 (and ibuffer-filter-format-alist
1706 (null ibuffer-compiled-filter-formats))))
1707 (message "Formats have changed, recompiling...")
1708 (ibuffer-recompile-formats)
1709 (setq ibuffer-cached-formats ibuffer-formats
1710 ibuffer-cached-eliding-string ibuffer-eliding-string
1711 ibuffer-cached-elide-long-columns (with-no-warnings ibuffer-elide-long-columns))
1712 (when ext-loaded
1713 (setq ibuffer-cached-filter-formats ibuffer-filter-format-alist))
1714 (message "Formats have changed, recompiling...done"))))
1716 (defvar ibuffer-inline-columns nil)
1718 (define-ibuffer-column mark (:name " " :inline t)
1719 (string mark))
1721 (define-ibuffer-column read-only (:name "R" :inline t)
1722 (if buffer-read-only
1723 (string ibuffer-read-only-char)
1724 " "))
1726 (define-ibuffer-column modified (:name "M" :inline t)
1727 (if (buffer-modified-p)
1728 (string ibuffer-modified-char)
1729 " "))
1731 (define-ibuffer-column name
1732 (:inline t
1733 :header-mouse-map ibuffer-name-header-map
1734 :props
1735 ('mouse-face 'highlight 'keymap ibuffer-name-map
1736 'ibuffer-name-column t
1737 'help-echo '(if tooltip-mode
1738 "mouse-1: mark this buffer\nmouse-2: select this buffer\nmouse-3: operate on this buffer"
1739 "mouse-1: mark buffer mouse-2: select buffer mouse-3: operate"))
1740 :summarizer
1741 (lambda (strings)
1742 (let ((bufs (length strings)))
1743 (cond ((zerop bufs) "No buffers")
1744 ((= 1 bufs) "1 buffer")
1745 (t (format "%s buffers" bufs))))))
1746 (propertize (buffer-name) 'font-lock-face (ibuffer-buffer-name-face buffer mark)))
1748 (define-ibuffer-column size
1749 (:inline t
1750 :header-mouse-map ibuffer-size-header-map
1751 :summarizer
1752 (lambda (column-strings)
1753 (let ((total 0))
1754 (dolist (string column-strings)
1755 (setq total
1756 ;; like, ewww ...
1757 (+ (float (string-to-number string))
1758 total)))
1759 (format "%.0f" total))))
1760 (format "%s" (buffer-size)))
1762 (define-ibuffer-column mode
1763 (:inline t
1764 :header-mouse-map ibuffer-mode-header-map
1765 :props
1766 ('mouse-face 'highlight
1767 'keymap ibuffer-mode-name-map
1768 'help-echo "mouse-2: filter by this mode"))
1769 (format-mode-line mode-name nil nil (current-buffer)))
1771 (define-ibuffer-column process
1772 (:summarizer
1773 (lambda (strings)
1774 (let ((total (length (delete "" strings))))
1775 (cond ((zerop total) "No processes")
1776 ((= 1 total) "1 process")
1777 (t (format "%d processes" total))))))
1778 (ibuffer-aif (get-buffer-process buffer)
1779 (format "(%s %s)" it (process-status it))
1780 ""))
1782 (define-ibuffer-column filename
1783 (:summarizer
1784 (lambda (strings)
1785 (let ((total (length (delete "" strings))))
1786 (cond ((zerop total) "No files")
1787 ((= 1 total) "1 file")
1788 (t (format "%d files" total))))))
1789 (let ((directory-abbrev-alist ibuffer-directory-abbrev-alist))
1790 (abbreviate-file-name
1791 (or (ibuffer-buffer-file-name) ""))))
1793 (define-ibuffer-column filename-and-process
1794 (:name "Filename/Process"
1795 :header-mouse-map ibuffer-filename/process-header-map
1796 :summarizer
1797 (lambda (strings)
1798 (setq strings (delete "" strings))
1799 (let ((procs 0)
1800 (files 0))
1801 (dolist (string strings)
1802 (if (string-match "\\(?:\\`([[:ascii:]]+)\\)" string)
1803 (progn (setq procs (1+ procs))
1804 (if (< (match-end 0) (length string))
1805 (setq files (1+ files))))
1806 (setq files (1+ files))))
1807 (concat (cond ((zerop files) "No files")
1808 ((= 1 files) "1 file")
1809 (t (format "%d files" files)))
1810 ", "
1811 (cond ((zerop procs) "no processes")
1812 ((= 1 procs) "1 process")
1813 (t (format "%d processes" procs)))))))
1814 (let ((proc (get-buffer-process buffer))
1815 (filename (ibuffer-make-column-filename buffer mark)))
1816 (if proc
1817 (concat (propertize (format "(%s %s)" proc (process-status proc))
1818 'font-lock-face 'italic)
1819 (if (> (length filename) 0)
1820 (format " %s" filename)
1821 ""))
1822 filename)))
1824 (defun ibuffer-format-column (str width alignment)
1825 (let ((left (make-string (/ width 2) ?\s))
1826 (right (make-string (- width (/ width 2)) ?\s)))
1827 (pcase alignment
1828 (:right (concat left right str))
1829 (:center (concat left str right))
1830 (_ (concat str left right)))))
1832 (defun ibuffer-buffer-name-face (buf mark)
1833 (cond ((char-equal mark ibuffer-marked-char)
1834 ibuffer-marked-face)
1835 ((char-equal mark ibuffer-deletion-char)
1836 ibuffer-deletion-face)
1837 (t
1838 (let ((level -1)
1839 result)
1840 (dolist (e ibuffer-fontification-alist result)
1841 (when (and (> (car e) level)
1842 (with-current-buffer buf
1843 (eval (nth 1 e))))
1844 (setq level (car e)
1845 result (nth 2 e))))))))
1847 (defun ibuffer-insert-buffer-line (buffer mark format)
1848 "Insert a line describing BUFFER and MARK using FORMAT."
1849 (ibuffer-assert-ibuffer-mode)
1850 (let ((beg (point)))
1851 (funcall format buffer mark)
1852 (put-text-property beg (point) 'ibuffer-properties (list buffer mark)))
1853 (insert "\n"))
1855 ;; This function knows a bit too much of the internals. It would be
1856 ;; nice if it was all abstracted away.
1857 (defun ibuffer-redisplay-current ()
1858 (ibuffer-assert-ibuffer-mode)
1859 (when (eobp)
1860 (forward-line -1))
1861 (beginning-of-line)
1862 (let ((curformat (mapcar #'ibuffer-expand-format-entry
1863 (ibuffer-current-format t))))
1864 (ibuffer-clear-summary-columns curformat)
1865 (let ((buf (ibuffer-current-buffer)))
1866 (when buf
1867 (let ((mark (ibuffer-current-mark)))
1868 (save-excursion
1869 (delete-region (point) (1+ (line-end-position)))
1870 (ibuffer-insert-buffer-line
1871 buf mark
1872 (ibuffer-current-format)))
1873 (when ibuffer-shrink-to-minimum-size
1874 (ibuffer-shrink-to-fit)))))))
1876 (defun ibuffer-map-on-mark (mark func)
1877 (ibuffer-map-lines
1878 (lambda (buf mk)
1879 (if (char-equal mark mk)
1880 (funcall func buf mark)
1881 nil))))
1883 (defun ibuffer-map-lines (function &optional nomodify group)
1884 "Call FUNCTION for each buffer.
1885 Set the ibuffer modification flag unless NOMODIFY is non-nil.
1887 If optional argument GROUP is non-nil, then only call FUNCTION on
1888 buffers in filtering group GROUP.
1890 FUNCTION is called with two arguments:
1891 the buffer object itself and the current mark symbol."
1892 (ibuffer-assert-ibuffer-mode)
1893 (ibuffer-forward-line 0)
1894 (let* ((orig-target-line (1+ (count-lines (save-excursion
1895 (goto-char (point-min))
1896 (ibuffer-forward-line 0)
1897 (point))
1898 (point))))
1899 (target-line-offset orig-target-line)
1900 (ibuffer-map-lines-total 0)
1901 (ibuffer-map-lines-count 0))
1902 (unwind-protect
1903 (progn
1904 (setq buffer-read-only nil)
1905 (goto-char (point-min))
1906 (ibuffer-forward-line 0 t)
1907 (while (and (not (eobp))
1908 (not (get-text-property (point) 'ibuffer-summary))
1909 (progn
1910 (ibuffer-forward-line 0 t)
1911 (and (not (eobp))
1912 (not (get-text-property (point) 'ibuffer-summary)))))
1913 (let ((result
1914 (if (buffer-live-p (ibuffer-current-buffer))
1915 (when (or (null group)
1916 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group)
1917 (equal group it)))
1918 (save-excursion
1919 (funcall function
1920 (ibuffer-current-buffer)
1921 (ibuffer-current-mark))))
1922 ;; Kill the line if the buffer is dead
1923 'kill)))
1924 ;; A given mapping function should return:
1925 ;; nil if it chose not to affect the buffer
1926 ;; `kill' means the remove line from the buffer list
1927 ;; t otherwise
1928 (cl-incf ibuffer-map-lines-total)
1929 (cond ((null result)
1930 (forward-line 1))
1931 ((eq result 'kill)
1932 (delete-region (line-beginning-position)
1933 (1+ (line-end-position)))
1934 (cl-incf ibuffer-map-lines-count)
1935 (when (< ibuffer-map-lines-total
1936 orig-target-line)
1937 (cl-decf target-line-offset)))
1938 (t
1939 (cl-incf ibuffer-map-lines-count)
1940 (forward-line 1)))))
1941 ibuffer-map-lines-count)
1942 (progn
1943 (setq buffer-read-only t)
1944 (unless nomodify
1945 (set-buffer-modified-p nil))
1946 (goto-char (point-min))
1947 (ibuffer-forward-line 0)
1948 (ibuffer-forward-line (1- target-line-offset))))))
1950 (defun ibuffer-get-marked-buffers ()
1951 "Return a list of buffer objects currently marked."
1952 (delq nil
1953 (mapcar (lambda (e)
1954 (when (eq (cdr e) ibuffer-marked-char)
1955 (car e)))
1956 (ibuffer-current-state-list))))
1958 (defun ibuffer-current-state-list (&optional pos)
1959 "Return a list like (BUF . MARK) of all buffers in an ibuffer.
1960 If POS is non-nil, return a list like (BUF MARK POINT), where POINT is
1961 the value of point at the beginning of the line for that buffer."
1962 (let ((ibuffer-current-state-list-tmp '()))
1963 ;; ah, if only we had closures. I bet this will mysteriously
1964 ;; break later. Don't blame me.
1965 (if pos
1966 (ibuffer-map-lines-nomodify
1967 (lambda (buf mark)
1968 (when (buffer-live-p buf)
1969 (push (list buf mark (point)) ibuffer-current-state-list-tmp))))
1970 (ibuffer-map-lines-nomodify
1971 (lambda (buf mark)
1972 (when (buffer-live-p buf)
1973 (push (cons buf mark) ibuffer-current-state-list-tmp)))))
1974 (nreverse ibuffer-current-state-list-tmp)))
1976 (defun ibuffer-current-buffers-with-marks (curbufs)
1977 "Return a list like (BUF . MARK) of all open buffers."
1978 (let ((bufs (ibuffer-current-state-list)))
1979 (mapcar (lambda (buf) (let ((e (assq buf bufs)))
1980 (if e
1982 (cons buf ?\s))))
1983 curbufs)))
1985 (defun ibuffer-buf-matches-predicates (buf predicates)
1986 (let ((hit nil)
1987 (name (buffer-name buf)))
1988 (dolist (pred predicates)
1989 (when (if (stringp pred)
1990 (string-match pred name)
1991 (funcall pred buf))
1992 (setq hit t)))
1993 hit))
1995 (defun ibuffer-filter-buffers (ibuffer-buf last bmarklist all)
1996 (let ((ext-loaded (featurep 'ibuf-ext)))
1997 (delq nil
1998 (mapcar
1999 ;; element should be like (BUFFER . MARK)
2000 (lambda (e)
2001 (let* ((buf (car e)))
2002 (when
2003 ;; This takes precedence over anything else
2004 (or (and ibuffer-always-show-last-buffer
2005 (eq last buf))
2006 (funcall (if ext-loaded
2007 #'ibuffer-ext-visible-p
2008 #'ibuffer-visible-p)
2009 buf all ibuffer-buf))
2010 e)))
2011 bmarklist))))
2013 (defun ibuffer-visible-p (buf all &optional ibuffer-buf)
2014 (and (or all
2015 (not
2016 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates)))
2017 (or ibuffer-view-ibuffer
2018 (and ibuffer-buf
2019 (not (eq ibuffer-buf buf))))))
2021 ;; This function is a special case; it's not defined by
2022 ;; `define-ibuffer-sorter'.
2023 (defun ibuffer-do-sort-by-recency ()
2024 "Sort the buffers by last view time."
2025 (interactive)
2026 (setq ibuffer-sorting-mode 'recency)
2027 (when (eq ibuffer-last-sorting-mode 'recency)
2028 (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep)))
2029 (ibuffer-update nil t)
2030 (setq ibuffer-last-sorting-mode 'recency))
2032 (defun ibuffer-update-format ()
2033 (when (null ibuffer-current-format)
2034 (setq ibuffer-current-format 0))
2035 (when (null ibuffer-formats)
2036 (error "Ibuffer error: no formats!")))
2038 (defun ibuffer-switch-format ()
2039 "Switch the current display format."
2040 (interactive)
2041 (ibuffer-assert-ibuffer-mode)
2042 (unless (consp ibuffer-formats)
2043 (error "Ibuffer error: No formats!"))
2044 (setq ibuffer-current-format
2045 (if (>= ibuffer-current-format (1- (length (ibuffer-current-formats nil))))
2047 (1+ ibuffer-current-format)))
2048 (ibuffer-update-format)
2049 (ibuffer-redisplay t))
2051 (defun ibuffer-update-title-and-summary (format)
2052 (ibuffer-assert-ibuffer-mode)
2053 ;; Don't do funky font-lock stuff here
2054 (let ((inhibit-modification-hooks t))
2055 (if (get-text-property (point-min) 'ibuffer-title)
2056 (delete-region (point-min)
2057 (next-single-property-change
2058 (point-min) 'ibuffer-title)))
2059 (goto-char (point-min))
2060 (add-text-properties
2061 (point)
2062 (progn
2063 (let ((opos (point)))
2064 ;; Insert the title names.
2065 (dolist (element format)
2066 (insert
2067 (if (stringp element)
2068 element
2069 (pcase-let ((`(,sym ,min ,_max ,align) element))
2070 ;; Ignore a negative min when we're inserting the title
2071 (when (cl-minusp min)
2072 (setq min (- min)))
2073 (let* ((name (or (get sym 'ibuffer-column-name)
2074 (error "Unknown column %s in ibuffer-formats" sym)))
2075 (len (length name))
2076 (hmap (get sym 'header-mouse-map))
2077 (strname (if (< len min)
2078 (ibuffer-format-column name
2079 (- min len)
2080 align)
2081 name)))
2082 (when hmap
2083 (setq
2084 strname
2085 (propertize strname 'mouse-face 'highlight 'keymap hmap)))
2086 strname)))))
2087 (add-text-properties opos (point) `(ibuffer-title-header t))
2088 (insert "\n")
2089 ;; Add the underlines
2090 (let ((str (save-excursion
2091 (forward-line -1)
2092 (beginning-of-line)
2093 (buffer-substring (point) (line-end-position)))))
2094 (apply #'insert (mapcar
2095 (lambda (c)
2096 (if (not (or (char-equal c ?\s)
2097 (char-equal c ?\n)))
2098 ?-
2099 ?\s))
2100 str)))
2101 (insert "\n"))
2102 (point))
2103 `(ibuffer-title t font-lock-face ,ibuffer-title-face))
2104 ;; Now, insert the summary columns.
2105 (goto-char (point-max))
2106 (if (get-text-property (1- (point-max)) 'ibuffer-summary)
2107 (delete-region (previous-single-property-change
2108 (point-max) 'ibuffer-summary)
2109 (point-max)))
2110 (if ibuffer-display-summary
2111 (add-text-properties
2112 (point)
2113 (progn
2114 (insert "\n")
2115 (dolist (element format)
2116 (insert
2117 (if (stringp element)
2118 (make-string (length element) ?\s)
2119 (pcase-let ((`(,sym ,min ,_max ,align) element))
2120 ;; Ignore a negative min when we're inserting the title.
2121 (when (cl-minusp min)
2122 (setq min (- min)))
2123 (let* ((summary
2124 (if (get sym 'ibuffer-column-summarizer)
2125 (funcall (get sym 'ibuffer-column-summarizer)
2126 (get sym 'ibuffer-column-summary))
2127 (make-string
2128 (length (get sym 'ibuffer-column-name))
2129 ?\s)))
2130 (len (length summary)))
2131 (if (< len min)
2132 (ibuffer-format-column summary
2133 (- min len)
2134 align)
2135 summary))))))
2136 (point))
2137 `(ibuffer-summary t)))))
2140 (defun ibuffer-redisplay (&optional silent)
2141 "Redisplay the current list of buffers.
2142 This does not show new buffers; use `ibuffer-update' for that.
2144 If optional arg SILENT is non-nil, do not display progress messages."
2145 (interactive)
2146 (ibuffer-forward-line 0)
2147 (unless silent
2148 (message "Redisplaying current buffer list..."))
2149 (let ((blist (ibuffer-current-state-list)))
2150 (when (and (null blist)
2151 (featurep 'ibuf-ext)
2152 (or ibuffer-filtering-qualifiers ibuffer-hidden-filter-groups))
2153 (message "No buffers! (note: filtering in effect)"))
2154 (ibuffer-redisplay-engine blist t)
2155 (unless silent
2156 (message "Redisplaying current buffer list...done"))
2157 (ibuffer-forward-line 0)))
2159 (defun ibuffer-update (arg &optional silent)
2160 "Regenerate the list of all buffers.
2162 Prefix arg non-nil means to toggle whether buffers that match
2163 `ibuffer-maybe-show-predicates' should be displayed.
2165 If optional arg SILENT is non-nil, do not display progress messages."
2166 (interactive "P")
2167 (if arg
2168 (setq ibuffer-display-maybe-show-predicates
2169 (not ibuffer-display-maybe-show-predicates)))
2170 (ibuffer-forward-line 0)
2171 (let* ((bufs (buffer-list))
2172 (blist (ibuffer-filter-buffers
2173 (current-buffer)
2174 (if (and
2175 (cadr bufs)
2176 (eq ibuffer-always-show-last-buffer
2177 :nomini)
2178 (minibufferp (cadr bufs)))
2179 (nth 2 bufs)
2180 (cadr bufs))
2181 (ibuffer-current-buffers-with-marks bufs)
2182 ibuffer-display-maybe-show-predicates)))
2183 (and (null blist)
2184 (featurep 'ibuf-ext)
2185 ibuffer-filtering-qualifiers
2186 (message "No buffers! (note: filtering in effect)"))
2187 (unless silent
2188 (message "Updating buffer list..."))
2189 (ibuffer-redisplay-engine blist arg)
2190 (unless silent
2191 (message "Updating buffer list...done")))
2192 (if (eq ibuffer-shrink-to-minimum-size 'onewindow)
2193 (ibuffer-shrink-to-fit t)
2194 (when ibuffer-shrink-to-minimum-size
2195 (ibuffer-shrink-to-fit)))
2196 (ibuffer-forward-line 0)
2197 ;; I tried to update this automatically from the mode-line-process format,
2198 ;; but changing nil-ness of header-line-format while computing
2199 ;; mode-line-format is asking a bit too much it seems. --Stef
2200 (setq header-line-format
2201 (and ibuffer-use-header-line
2202 ibuffer-filtering-qualifiers
2203 ibuffer-header-line-format)))
2205 (defun ibuffer-sort-bufferlist (bmarklist)
2206 (unless ibuffer-sorting-functions-alist
2207 ;; make sure the sorting functions are loaded
2208 (require 'ibuf-ext))
2209 (let* ((sortdat (assq ibuffer-sorting-mode
2210 ibuffer-sorting-functions-alist))
2211 (func (nth 2 sortdat)))
2212 (let ((result
2213 ;; actually sort the buffers
2214 (if (and sortdat func)
2215 (sort bmarklist func)
2216 bmarklist)))
2217 ;; perhaps reverse the sorted buffer list
2218 (if ibuffer-sorting-reversep
2219 (nreverse result)
2220 result))))
2222 (defun ibuffer-insert-filter-group (name display-name filter-string format bmarklist)
2223 (add-text-properties
2224 (point)
2225 (progn
2226 (insert "[ " display-name " ]")
2227 (point))
2228 `(ibuffer-filter-group-name
2229 ,name
2230 font-lock-face ,ibuffer-filter-group-name-face
2231 keymap ,ibuffer-mode-filter-group-map
2232 mouse-face highlight
2233 help-echo ,(let ((echo '(if tooltip-mode
2234 "mouse-1: toggle marks in this group\nmouse-2: hide/show this filtering group"
2235 "mouse-1: toggle marks mouse-2: hide/show")))
2236 (if (> (length filter-string) 0)
2237 `(concat ,filter-string
2238 (if tooltip-mode "\n" " ")
2239 ,echo)
2240 echo))))
2241 (insert "\n")
2242 (when bmarklist
2243 (put-text-property
2244 (point)
2245 (progn
2246 (dolist (entry bmarklist)
2247 (ibuffer-insert-buffer-line (car entry) (cdr entry) format))
2248 (point))
2249 'ibuffer-filter-group
2250 name)))
2252 (defun ibuffer-redisplay-engine (bmarklist &optional _ignore)
2253 (ibuffer-assert-ibuffer-mode)
2254 (let* ((--ibuffer-insert-buffers-and-marks-format
2255 (ibuffer-current-format))
2256 (--ibuffer-expanded-format (mapcar #'ibuffer-expand-format-entry
2257 (ibuffer-current-format t)))
2258 (orig (count-lines (point-min) (point)))
2259 ;; Inhibit font-lock caching tricks, since we're modifying the
2260 ;; entire buffer at once
2261 (inhibit-modification-hooks t)
2262 (ext-loaded (featurep 'ibuf-ext))
2263 (bgroups (if ext-loaded
2264 (ibuffer-generate-filter-groups bmarklist)
2265 (list (cons "Default" bmarklist)))))
2266 (ibuffer-clear-summary-columns --ibuffer-expanded-format)
2267 (unwind-protect
2268 (progn
2269 (setq buffer-read-only nil)
2270 (erase-buffer)
2271 (ibuffer-update-format)
2272 (dolist (group (nreverse bgroups))
2273 (let* ((name (car group))
2274 (disabled (and ext-loaded
2275 (member name ibuffer-hidden-filter-groups)))
2276 (bmarklist (cdr group)))
2277 (unless (and (null bmarklist)
2278 (not disabled)
2279 ext-loaded
2280 (null ibuffer-show-empty-filter-groups))
2281 (ibuffer-insert-filter-group
2282 name
2283 (if disabled (concat name " ...") name)
2284 (if ext-loaded
2285 (ibuffer-format-filter-group-data name)
2286 "")
2287 --ibuffer-insert-buffers-and-marks-format
2288 (if disabled
2289 nil
2290 (ibuffer-sort-bufferlist bmarklist))))))
2291 (ibuffer-update-title-and-summary --ibuffer-expanded-format))
2292 (setq buffer-read-only t)
2293 (set-buffer-modified-p ibuffer-did-modification)
2294 (setq ibuffer-did-modification nil)
2295 (goto-char (point-min))
2296 (forward-line orig))))
2298 ;;;###autoload
2299 (defun ibuffer-list-buffers (&optional files-only)
2300 "Display a list of buffers, in another window.
2301 If optional argument FILES-ONLY is non-nil, then add a filter for
2302 buffers which are visiting a file."
2303 (interactive "P")
2304 (ibuffer t nil (when files-only
2305 '((filename . ".*"))) t))
2307 ;;;###autoload
2308 (defun ibuffer-other-window (&optional files-only)
2309 "Like `ibuffer', but displayed in another window by default.
2310 If optional argument FILES-ONLY is non-nil, then add a filter for
2311 buffers which are visiting a file."
2312 (interactive "P")
2313 (ibuffer t nil (when files-only
2314 '((filename . ".*")))))
2316 ;;;###autoload
2317 (defun ibuffer (&optional other-window-p name qualifiers noselect
2318 shrink filter-groups formats)
2319 "Begin using Ibuffer to edit a list of buffers.
2320 Type `h' after entering ibuffer for more information.
2322 All arguments are optional.
2323 OTHER-WINDOW-P says to use another window.
2324 NAME specifies the name of the buffer (defaults to \"*Ibuffer*\").
2325 QUALIFIERS is an initial set of filtering qualifiers to use;
2326 see `ibuffer-filtering-qualifiers'.
2327 NOSELECT means don't select the Ibuffer buffer.
2328 SHRINK means shrink the buffer to minimal size. The special
2329 value `onewindow' means always use another window.
2330 FILTER-GROUPS is an initial set of filtering groups to use;
2331 see `ibuffer-filter-groups'.
2332 FORMATS is the value to use for `ibuffer-formats'.
2333 If specified, then the variable `ibuffer-formats' will have
2334 that value locally in this buffer."
2335 (interactive "P")
2336 (when ibuffer-use-other-window
2337 (setq other-window-p t))
2338 (let ((buf (get-buffer-create (or name "*Ibuffer*"))))
2339 (if other-window-p
2340 (funcall (if noselect (lambda (buf) (display-buffer buf t)) #'pop-to-buffer) buf)
2341 (funcall (if noselect #'display-buffer #'switch-to-buffer) buf))
2342 (with-current-buffer buf
2343 (save-selected-window
2344 ;; We switch to the buffer's window in order to be able
2345 ;; to modify the value of point
2346 (select-window (get-buffer-window buf 0))
2347 (or (derived-mode-p 'ibuffer-mode)
2348 (ibuffer-mode))
2349 (when shrink
2350 (setq ibuffer-shrink-to-minimum-size shrink))
2351 (when qualifiers
2352 (require 'ibuf-ext)
2353 (setq ibuffer-filtering-qualifiers qualifiers))
2354 (when filter-groups
2355 (require 'ibuf-ext)
2356 (setq ibuffer-filter-groups filter-groups))
2357 (when formats
2358 (set (make-local-variable 'ibuffer-formats) formats))
2359 (ibuffer-update nil)
2360 ;; Skip the group name by default.
2361 (ibuffer-forward-line 0 t)
2362 (unwind-protect
2363 (progn
2364 (setq buffer-read-only nil)
2365 (run-hooks 'ibuffer-hook))
2366 (setq buffer-read-only t))
2367 (unless ibuffer-expert
2368 (message "Commands: m, u, t, RET, g, k, S, D, Q; q to quit; h for help"))))))
2370 (put 'ibuffer-mode 'mode-class 'special)
2371 (define-derived-mode ibuffer-mode special-mode "IBuffer"
2372 "A major mode for viewing a list of buffers.
2373 In Ibuffer, you can conveniently perform many operations on the
2374 currently open buffers, in addition to filtering your view to a
2375 particular subset of them, and sorting by various criteria.
2377 Operations on marked buffers:
2378 \\<ibuffer-mode-map>
2379 `\\[ibuffer-do-save]' - Save the marked buffers
2380 `\\[ibuffer-do-view]' - View the marked buffers in this frame.
2381 `\\[ibuffer-do-view-other-frame]' - View the marked buffers in another frame.
2382 `\\[ibuffer-do-revert]' - Revert the marked buffers.
2383 `\\[ibuffer-do-toggle-read-only]' - Toggle read-only state of marked buffers.
2384 `\\[ibuffer-do-delete]' - Kill the marked buffers.
2385 `\\[ibuffer-do-isearch]' - Do incremental search in the marked buffers.
2386 `\\[ibuffer-do-isearch-regexp]' - Isearch for regexp in the marked buffers.
2387 `\\[ibuffer-do-replace-regexp]' - Replace by regexp in each of the marked
2388 buffers.
2389 `\\[ibuffer-do-query-replace]' - Query replace in each of the marked buffers.
2390 `\\[ibuffer-do-query-replace-regexp]' - As above, with a regular expression.
2391 `\\[ibuffer-do-print]' - Print the marked buffers.
2392 `\\[ibuffer-do-occur]' - List lines in all marked buffers which match
2393 a given regexp (like the function `occur').
2394 `\\[ibuffer-do-shell-command-pipe]' - Pipe the contents of the marked
2395 buffers to a shell command.
2396 `\\[ibuffer-do-shell-command-pipe-replace]' - Replace the contents of the marked
2397 buffers with the output of a shell command.
2398 `\\[ibuffer-do-shell-command-file]' - Run a shell command with the
2399 buffer's file as an argument.
2400 `\\[ibuffer-do-eval]' - Evaluate a form in each of the marked buffers. This
2401 is a very flexible command. For example, if you want to make all
2402 of the marked buffers read only, try using (read-only-mode 1) as
2403 the input form.
2404 `\\[ibuffer-do-view-and-eval]' - As above, but view each buffer while the form
2405 is evaluated.
2406 `\\[ibuffer-do-kill-lines]' - Remove the marked lines from the *Ibuffer* buffer,
2407 but don't kill the associated buffer.
2408 `\\[ibuffer-do-kill-on-deletion-marks]' - Kill all buffers marked for deletion.
2410 Marking commands:
2412 `\\[ibuffer-mark-forward]' - Mark the buffer at point.
2413 `\\[ibuffer-toggle-marks]' - Unmark all currently marked buffers, and mark
2414 all unmarked buffers.
2415 `\\[ibuffer-unmark-forward]' - Unmark the buffer at point.
2416 `\\[ibuffer-unmark-backward]' - Unmark the buffer at point, and move to the
2417 previous line.
2418 `\\[ibuffer-unmark-all]' - Unmark all marked buffers.
2419 `\\[ibuffer-mark-by-mode]' - Mark buffers by major mode.
2420 `\\[ibuffer-mark-unsaved-buffers]' - Mark all \"unsaved\" buffers.
2421 This means that the buffer is modified, and has an associated file.
2422 `\\[ibuffer-mark-modified-buffers]' - Mark all modified buffers,
2423 regardless of whether or not they have an associated file.
2424 `\\[ibuffer-mark-special-buffers]' - Mark all buffers whose name begins and
2425 ends with `*'.
2426 `\\[ibuffer-mark-dissociated-buffers]' - Mark all buffers which have
2427 an associated file, but that file doesn't currently exist.
2428 `\\[ibuffer-mark-read-only-buffers]' - Mark all read-only buffers.
2429 `\\[ibuffer-mark-dired-buffers]' - Mark buffers in `dired' mode.
2430 `\\[ibuffer-mark-help-buffers]' - Mark buffers in `help-mode', `apropos-mode', etc.
2431 `\\[ibuffer-mark-old-buffers]' - Mark buffers older than `ibuffer-old-time'.
2432 `\\[ibuffer-mark-for-delete]' - Mark the buffer at point for deletion.
2433 `\\[ibuffer-mark-by-name-regexp]' - Mark buffers by their name, using a regexp.
2434 `\\[ibuffer-mark-by-mode-regexp]' - Mark buffers by their major mode, using a regexp.
2435 `\\[ibuffer-mark-by-file-name-regexp]' - Mark buffers by their filename, using a regexp.
2437 Filtering commands:
2439 `\\[ibuffer-filter-by-mode]' - Add a filter by any major mode.
2440 `\\[ibuffer-filter-by-used-mode]' - Add a filter by a major mode now in use.
2441 `\\[ibuffer-filter-by-derived-mode]' - Add a filter by derived mode.
2442 `\\[ibuffer-filter-by-name]' - Add a filter by buffer name.
2443 `\\[ibuffer-filter-by-content]' - Add a filter by buffer content.
2444 `\\[ibuffer-filter-by-filename]' - Add a filter by filename.
2445 `\\[ibuffer-filter-by-size-gt]' - Add a filter by buffer size.
2446 `\\[ibuffer-filter-by-size-lt]' - Add a filter by buffer size.
2447 `\\[ibuffer-filter-by-predicate]' - Add a filter by an arbitrary Lisp predicate.
2448 `\\[ibuffer-save-filters]' - Save the current filters with a name.
2449 `\\[ibuffer-switch-to-saved-filters]' - Switch to previously saved filters.
2450 `\\[ibuffer-add-saved-filters]' - Add saved filters to current filters.
2451 `\\[ibuffer-or-filter]' - Replace the top two filters with their logical OR.
2452 `\\[ibuffer-pop-filter]' - Remove the top filter.
2453 `\\[ibuffer-negate-filter]' - Invert the logical sense of the top filter.
2454 `\\[ibuffer-decompose-filter]' - Break down the topmost filter.
2455 `\\[ibuffer-filter-disable]' - Remove all filtering currently in effect.
2457 Filter group commands:
2459 `\\[ibuffer-filters-to-filter-group]' - Create filter group from filters.
2460 `\\[ibuffer-pop-filter-group]' - Remove top filter group.
2461 `\\[ibuffer-forward-filter-group]' - Move to the next filter group.
2462 `\\[ibuffer-backward-filter-group]' - Move to the previous filter group.
2463 `\\[ibuffer-clear-filter-groups]' - Remove all active filter groups.
2464 `\\[ibuffer-save-filter-groups]' - Save the current groups with a name.
2465 `\\[ibuffer-switch-to-saved-filter-groups]' - Restore previously saved groups.
2466 `\\[ibuffer-delete-saved-filter-groups]' - Delete previously saved groups.
2468 Sorting commands:
2470 `\\[ibuffer-toggle-sorting-mode]' - Rotate between the various sorting modes.
2471 `\\[ibuffer-invert-sorting]' - Reverse the current sorting order.
2472 `\\[ibuffer-do-sort-by-alphabetic]' - Sort the buffers lexicographically.
2473 `\\[ibuffer-do-sort-by-filename/process]' - Sort the buffers by the file name.
2474 `\\[ibuffer-do-sort-by-recency]' - Sort the buffers by last viewing time.
2475 `\\[ibuffer-do-sort-by-size]' - Sort the buffers by size.
2476 `\\[ibuffer-do-sort-by-major-mode]' - Sort the buffers by major mode.
2478 Other commands:
2480 `\\[ibuffer-update]' - Regenerate the list of all buffers.
2481 Prefix arg means to toggle whether buffers that match
2482 `ibuffer-maybe-show-predicates' should be displayed.
2484 `\\[ibuffer-switch-format]' - Change the current display format.
2485 `\\[forward-line]' - Move point to the next line.
2486 `\\[previous-line]' - Move point to the previous line.
2487 `\\[describe-mode]' - This help.
2488 `\\[ibuffer-diff-with-file]' - View the differences between this buffer
2489 and its associated file.
2490 `\\[ibuffer-visit-buffer]' - View the buffer on this line.
2491 `\\[ibuffer-visit-buffer-other-window]' - As above, but in another window.
2492 `\\[ibuffer-visit-buffer-other-window-noselect]' - As both above, but don't select
2493 the new window.
2494 `\\[ibuffer-bury-buffer]' - Bury (not kill!) the buffer on this line.
2496 ** Information on Filtering:
2498 You can filter your ibuffer view via different criteria. Each Ibuffer
2499 buffer has its own stack of active filters. For example, suppose you
2500 are working on an Emacs Lisp project. You can create an Ibuffer
2501 buffer displays buffers in just `emacs-lisp' modes via
2502 `\\[ibuffer-filter-by-mode] emacs-lisp-mode RET'. In this case, there
2503 is just one entry on the filtering stack.
2505 You can also combine filters. The various filtering commands push a
2506 new filter onto the stack, and the filters combine to show just
2507 buffers which satisfy ALL criteria on the stack. For example, suppose
2508 you only want to see buffers in `emacs-lisp' mode, whose names begin
2509 with \"gnus\". You can accomplish this via:
2511 \\[ibuffer-filter-by-mode] emacs-lisp-mode RET
2512 \\[ibuffer-filter-by-name] ^gnus RET
2514 Additionally, you can OR the top two filters together with
2515 `\\[ibuffer-or-filters]'. To see all buffers in either
2516 `emacs-lisp-mode' or `lisp-interaction-mode', type:
2518 \\[ibuffer-filter-by-mode] emacs-lisp-mode RET
2519 \\[ibuffer-filter-by-mode] lisp-interaction-mode RET
2520 \\[ibuffer-or-filters]
2522 Filters can also be saved and restored using mnemonic names: see the
2523 functions `ibuffer-save-filters' and `ibuffer-switch-to-saved-filters'.
2525 To remove the top filter on the stack, use `\\[ibuffer-pop-filter]', and
2526 to disable all filtering currently in effect, use
2527 `\\[ibuffer-filter-disable]'.
2529 ** Filter Groups:
2531 Once one has mastered filters, the next logical step up is \"filter
2532 groups\". A filter group is basically a named group of buffers which
2533 match a filter, which are displayed together in an Ibuffer buffer. To
2534 create a filter group, simply use the regular functions to create a
2535 filter, and then type `\\[ibuffer-filters-to-filter-group]'.
2537 A quick example will make things clearer. Suppose that one wants to
2538 group all of one's Emacs Lisp buffers together. To do this, type:
2540 \\[ibuffer-filter-by-mode] emacs-lisp-mode RET
2541 \\[ibuffer-filters-to-filter-group] emacs lisp buffers RET
2543 You may, of course, name the group whatever you want; it doesn't have
2544 to be \"emacs lisp buffers\". Filter groups may be composed of any
2545 arbitrary combination of filters.
2547 Just like filters themselves, filter groups act as a stack. Buffers
2548 will not be displayed multiple times if they would be included in
2549 multiple filter groups; instead, the first filter group is used. The
2550 filter groups are displayed in this order of precedence.
2552 You may rearrange filter groups by using the regular
2553 `\\[ibuffer-kill-line]' and `\\[ibuffer-yank]' pair. Yanked groups
2554 will be inserted before the group at point."
2555 ;; Include state info next to the mode name.
2556 (set (make-local-variable 'mode-line-process)
2557 '(" by "
2558 (ibuffer-sorting-mode (:eval (symbol-name ibuffer-sorting-mode))
2559 "view time")
2560 (ibuffer-sorting-reversep " [rev]")
2561 (ibuffer-auto-mode " (Auto)")
2562 ;; Only list the filters if they're not already in the header-line.
2563 (header-line-format
2564 ""
2565 (:eval (if (functionp 'ibuffer-format-qualifier)
2566 (mapconcat 'ibuffer-format-qualifier
2567 ibuffer-filtering-qualifiers ""))))))
2568 ;; `ibuffer-update' puts this on header-line-format when needed.
2569 (setq ibuffer-header-line-format
2570 ;; Display the part that won't be in the mode-line.
2571 `("" ,mode-name
2572 ,@(mapcar (lambda (elem)
2573 (if (eq (car-safe elem) 'header-line-format)
2574 (nth 2 elem) elem))
2575 mode-line-process)))
2577 (setq buffer-read-only t)
2578 (buffer-disable-undo)
2579 (setq truncate-lines ibuffer-truncate-lines)
2580 ;; This makes things less ugly for Emacs 21 users with a non-nil
2581 ;; `show-trailing-whitespace'.
2582 (setq show-trailing-whitespace nil)
2583 ;; disable `show-paren-mode' buffer-locally
2584 (if (bound-and-true-p show-paren-mode)
2585 (set (make-local-variable 'show-paren-mode) nil))
2586 (set (make-local-variable 'revert-buffer-function)
2587 #'ibuffer-update)
2588 (set (make-local-variable 'ibuffer-sorting-mode)
2589 ibuffer-default-sorting-mode)
2590 (set (make-local-variable 'ibuffer-sorting-reversep)
2591 ibuffer-default-sorting-reversep)
2592 (set (make-local-variable 'ibuffer-shrink-to-minimum-size)
2593 ibuffer-default-shrink-to-minimum-size)
2594 (set (make-local-variable 'ibuffer-display-maybe-show-predicates)
2595 ibuffer-default-display-maybe-show-predicates)
2596 (set (make-local-variable 'ibuffer-filtering-qualifiers) nil)
2597 (set (make-local-variable 'ibuffer-filter-groups) nil)
2598 (set (make-local-variable 'ibuffer-filter-group-kill-ring) nil)
2599 (set (make-local-variable 'ibuffer-hidden-filter-groups) nil)
2600 (set (make-local-variable 'ibuffer-compiled-formats) nil)
2601 (set (make-local-variable 'ibuffer-cached-formats) nil)
2602 (set (make-local-variable 'ibuffer-cached-eliding-string) nil)
2603 (set (make-local-variable 'ibuffer-cached-elide-long-columns) nil)
2604 (set (make-local-variable 'ibuffer-current-format) nil)
2605 (set (make-local-variable 'ibuffer-did-modification) nil)
2606 (set (make-local-variable 'ibuffer-tmp-hide-regexps) nil)
2607 (set (make-local-variable 'ibuffer-tmp-show-regexps) nil)
2608 (define-key ibuffer-mode-map [menu-bar edit] 'undefined)
2609 (define-key ibuffer-mode-map [menu-bar operate] (cons "Operate" ibuffer-mode-operate-map))
2610 (ibuffer-update-format)
2611 (when ibuffer-default-directory
2612 (setq default-directory ibuffer-default-directory))
2613 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t))
2615 (provide 'ibuffer)
2617 (run-hooks 'ibuffer-load-hook)
2619 ;;; ibuffer.el ends here