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