Don't require isearch-update before isearch-done
[emacs.git] / lisp / ibuffer.el
blob8e246297ff760601a5c0d151833e3261d7801f7c
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 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 "25.2"
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 "25.2"
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 (ibuffer-get-marked-buffers)))
1147 (when (null marked-bufs)
1148 (setq marked-bufs (list (ibuffer-current-buffer t))))
1149 (unless (and (eq type 'other-frame)
1150 (not ibuffer-expert)
1151 (> (length marked-bufs) 3)
1152 (not (y-or-n-p (format "Really create a new frame for %s buffers? "
1153 (length marked-bufs)))))
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);FIXME:arg unused!
1303 "Toggle read only status in marked buffers.
1304 With optional ARG, make read-only only if ARG is not negative."
1305 (:opstring "toggled read only status in"
1306 :interactive "P"
1307 :modifier-p t)
1308 (read-only-mode 'toggle))
1310 (define-ibuffer-op ibuffer-do-delete ()
1311 "Kill marked buffers as with `kill-this-buffer'."
1312 (:opstring "killed"
1313 :active-opstring "kill"
1314 :dangerous t
1315 :complex t
1316 :modifier-p t)
1317 (if (kill-buffer buf)
1318 'kill
1319 nil))
1321 (define-ibuffer-op ibuffer-do-kill-on-deletion-marks ()
1322 "Kill buffers marked for deletion as with `kill-this-buffer'."
1323 (:opstring "killed"
1324 :active-opstring "kill"
1325 :dangerous t
1326 :complex t
1327 :mark :deletion
1328 :modifier-p t)
1329 (if (kill-buffer buf)
1330 'kill
1331 nil))
1333 (defun ibuffer-unmark-all (mark)
1334 "Unmark all buffers with mark MARK."
1335 (interactive "cRemove marks (RET means all):")
1336 (if (= (ibuffer-count-marked-lines t) 0)
1337 (message "No buffers marked; use 'm' to mark a buffer")
1338 (cond
1339 ((char-equal mark ibuffer-marked-char)
1340 (ibuffer-map-marked-lines
1341 (lambda (_buf _mark)
1342 (ibuffer-set-mark-1 ?\s)
1343 t)))
1344 ((char-equal mark ibuffer-deletion-char)
1345 (ibuffer-map-deletion-lines
1346 (lambda (_buf _mark)
1347 (ibuffer-set-mark-1 ?\s)
1348 t)))
1349 ((not (char-equal mark ?\r))
1350 (ibuffer-map-lines
1351 (lambda (_buf cmark)
1352 (when (char-equal cmark mark)
1353 (ibuffer-set-mark-1 ?\s))
1354 t)))
1356 (ibuffer-map-lines
1357 (lambda (_buf mark)
1358 (when (not (char-equal mark ?\s))
1359 (ibuffer-set-mark-1 ?\s))
1360 t)))))
1361 (ibuffer-redisplay t))
1363 (defun ibuffer-unmark-all-marks ()
1364 "Remove all marks from all marked buffers in Ibuffer."
1365 (interactive)
1366 ;; hm. we could probably do this in a better fashion
1367 (ibuffer-unmark-all ?\r))
1369 (defun ibuffer-toggle-marks (&optional group)
1370 "Toggle which buffers are marked.
1371 In other words, unmarked buffers become marked, and marked buffers
1372 become unmarked.
1373 If point is on a group name, then this function operates on that
1374 group."
1375 (interactive)
1376 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
1377 (setq group it))
1378 (let ((count
1379 (ibuffer-map-lines
1380 (lambda (_buf mark)
1381 (cond ((eq mark ibuffer-marked-char)
1382 (ibuffer-set-mark-1 ?\s)
1383 nil)
1384 ((eq mark ?\s)
1385 (ibuffer-set-mark-1 ibuffer-marked-char)
1388 nil)))
1389 nil group)))
1390 (message "%s buffers marked" count))
1391 (ibuffer-redisplay t))
1393 (defun ibuffer-change-marks (&optional old new)
1394 "Change all OLD marks to NEW marks.
1395 OLD and NEW are both characters used to mark buffers."
1396 (interactive
1397 (let* ((cursor-in-echo-area t)
1398 (old (progn (message "Change (old mark): ") (read-char)))
1399 (new (progn (message "Change %c marks to (new mark): " old)
1400 (read-char))))
1401 (list old new)))
1402 (if (or (eq old ?\r) (eq new ?\r))
1403 (ding)
1404 (let ((count
1405 (ibuffer-map-lines
1406 (lambda (_buf mark)
1407 (when (eq mark old)
1408 (ibuffer-set-mark new) t)))))
1409 (message "%s marks changed" count))))
1411 (defsubst ibuffer-get-region-and-prefix ()
1412 (let ((arg (prefix-numeric-value current-prefix-arg)))
1413 (if (use-region-p) (list (region-beginning) (region-end) arg)
1414 (list nil nil arg))))
1416 (defun ibuffer-mark-forward (start end arg)
1417 "Mark the buffers in the region, or ARG buffers.
1418 If point is on a group name, this function operates on that group."
1419 (interactive (ibuffer-get-region-and-prefix))
1420 (ibuffer-mark-region-or-n-with-char start end arg ibuffer-marked-char))
1422 (defun ibuffer-unmark-forward (start end arg)
1423 "Unmark the buffers in the region, or ARG buffers.
1424 If point is on a group name, this function operates on that group."
1425 (interactive (ibuffer-get-region-and-prefix))
1426 (ibuffer-mark-region-or-n-with-char start end arg ?\s))
1428 (defun ibuffer-unmark-backward (arg)
1429 "Unmark the ARG previous buffers.
1430 If point is on a group name, this function operates on that group."
1431 (interactive "p")
1432 (ibuffer-unmark-forward nil nil (- arg)))
1434 (defun ibuffer-mark-region-or-n-with-char (start end arg mark-char)
1435 (if (use-region-p)
1436 (let ((cur (point)) (line-count (count-lines start end)))
1437 (goto-char start)
1438 (ibuffer-mark-interactive line-count mark-char)
1439 (goto-char cur))
1440 (ibuffer-mark-interactive arg mark-char)))
1442 (defun ibuffer-mark-interactive (arg mark &optional movement)
1443 (ibuffer-assert-ibuffer-mode)
1444 (or arg (setq arg 1))
1445 ;; deprecated movement argument
1446 (when (and movement (< movement 0))
1447 (setq arg (- arg)))
1448 (ibuffer-forward-line 0)
1449 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
1450 (progn
1451 (require 'ibuf-ext)
1452 (ibuffer-mark-on-buffer #'identity mark it))
1453 (ibuffer-forward-line 0 t)
1454 (while (> arg 0)
1455 (ibuffer-set-mark mark)
1456 (ibuffer-forward-line 1 t)
1457 (setq arg (1- arg)))
1458 (while (< arg 0)
1459 (ibuffer-forward-line -1 t)
1460 (ibuffer-set-mark mark)
1461 (setq arg (1+ arg)))))
1463 (defun ibuffer-set-mark (mark)
1464 (ibuffer-assert-ibuffer-mode)
1465 (let ((inhibit-read-only t))
1466 (ibuffer-set-mark-1 mark)
1467 (setq ibuffer-did-modification t)
1468 (ibuffer-redisplay-current)
1469 (beginning-of-line)))
1471 (defun ibuffer-set-mark-1 (mark)
1472 (let ((beg (line-beginning-position))
1473 (end (line-end-position)))
1474 (put-text-property beg end 'ibuffer-properties
1475 (list (ibuffer-current-buffer)
1476 mark))))
1478 (defun ibuffer-mark-for-delete (start end arg)
1479 "Mark for deletion the buffers in the region, or ARG buffers.
1480 If point is on a group name, this function operates on that group."
1481 (interactive (ibuffer-get-region-and-prefix))
1482 (ibuffer-mark-region-or-n-with-char start end arg ibuffer-deletion-char))
1484 (defun ibuffer-mark-for-delete-backwards (arg)
1485 "Mark for deletion the ARG previous buffers.
1486 If point is on a group name, this function operates on that group."
1487 (interactive "p")
1488 (ibuffer-mark-interactive arg ibuffer-deletion-char -1))
1490 (defun ibuffer-current-buffer (&optional must-be-live)
1491 (let ((buf (car (get-text-property (line-beginning-position)
1492 'ibuffer-properties))))
1493 (when must-be-live
1494 (if (bufferp buf)
1495 (unless (buffer-live-p buf)
1496 (error "Buffer %s has been killed; %s" buf (substitute-command-keys "use `\\[ibuffer-update]' to update")))
1497 (error "No buffer on this line")))
1498 buf))
1500 (defun ibuffer-active-formats-name ()
1501 (if (boundp 'ibuffer-filter-format-alist)
1502 (let ((ret nil))
1503 (dolist (filter ibuffer-filtering-qualifiers ret)
1504 (let ((val (assq (car filter) ibuffer-filter-format-alist)))
1505 (when val
1506 (setq ret (car filter)))))
1507 (if ret
1509 :ibuffer-formats))
1510 :ibuffer-formats))
1512 (defun ibuffer-current-formats (uncompiledp)
1513 (let* ((name (ibuffer-active-formats-name)))
1514 (ibuffer-check-formats)
1515 (if (eq name :ibuffer-formats)
1516 (if uncompiledp
1517 ibuffer-formats
1518 ibuffer-compiled-formats)
1519 (cadr (assq name
1520 (if uncompiledp
1521 ibuffer-filter-format-alist
1522 ibuffer-compiled-filter-formats))))))
1524 (defun ibuffer-current-format (&optional uncompiledp)
1525 (or ibuffer-current-format
1526 (setq ibuffer-current-format 0))
1527 (nth ibuffer-current-format (ibuffer-current-formats uncompiledp)))
1529 (defun ibuffer-expand-format-entry (form)
1530 (if (or (consp form)
1531 (symbolp form))
1532 (let ((sym (intern (concat "ibuffer-make-column-"
1533 (symbol-name (if (consp form)
1534 (car form)
1535 form))))))
1536 (unless (or (fboundp sym)
1537 (assq sym ibuffer-inline-columns))
1538 (error "Unknown column %s in ibuffer-formats" form))
1539 (let (min max align elide)
1540 (if (consp form)
1541 (setq min (or (nth 1 form) 0)
1542 max (or (nth 2 form) -1)
1543 align (or (nth 3 form) :left)
1544 elide (or (nth 4 form) nil))
1545 (setq min 0
1546 max -1
1547 align :left
1548 elide nil))
1549 (list sym min max align elide)))
1550 form))
1552 (defun ibuffer-compile-make-eliding-form (strvar elide from-end-p)
1553 (let ((ellipsis (propertize ibuffer-eliding-string 'font-lock-face 'bold)))
1554 (if (or elide (with-no-warnings ibuffer-elide-long-columns))
1555 `(if (> strlen 5)
1556 ,(if from-end-p
1557 `(concat ,ellipsis
1558 (substring ,strvar
1559 (length ibuffer-eliding-string)))
1560 `(concat
1561 (substring ,strvar 0 (- strlen ,(length ellipsis)))
1562 ,ellipsis))
1563 ,strvar)
1564 strvar)))
1566 (defun ibuffer-compile-make-substring-form (strvar maxvar from-end-p)
1567 (if from-end-p
1568 `(substring str
1569 (- strlen ,maxvar))
1570 `(substring ,strvar 0 ,maxvar)))
1572 (defun ibuffer-compile-make-format-form (strvar widthform alignment)
1573 (let* ((left `(make-string tmp2 ?\s))
1574 (right `(make-string (- tmp1 tmp2) ?\s)))
1575 `(progn
1576 (setq tmp1 ,widthform
1577 tmp2 (/ tmp1 2))
1578 ,(pcase alignment
1579 (:right `(concat ,left ,right ,strvar))
1580 (:center `(concat ,left ,strvar ,right))
1581 (:left `(concat ,strvar ,left ,right))
1582 (_ (error "Invalid alignment %s" alignment))))))
1584 (defun ibuffer-compile-format (format)
1585 (let ((result nil)
1586 ;; We use these variables to keep track of which variables
1587 ;; inside the generated function we need to bind, since
1588 ;; binding variables in Emacs takes time.
1589 (vars-used ()))
1590 (dolist (form format)
1591 (push
1592 ;; Generate a form based on a particular format entry, like
1593 ;; " ", mark, or (mode 16 16 :right).
1594 (if (stringp form)
1595 ;; It's a string; all we need to do is insert it.
1596 `(insert ,form)
1597 (let* ((form (ibuffer-expand-format-entry form))
1598 (sym (nth 0 form))
1599 (min (nth 1 form))
1600 (max (nth 2 form))
1601 (align (nth 3 form))
1602 (elide (nth 4 form)))
1603 (let* ((from-end-p (when (cl-minusp min)
1604 (setq min (- min))
1606 (letbindings nil)
1607 (outforms nil)
1608 minform
1609 maxform
1610 min-used max-used strlen-used)
1611 (when (or (not (integerp min)) (>= min 0))
1612 ;; This is a complex case; they want it limited to a
1613 ;; minimum size.
1614 (setq min-used t)
1615 (setq strlen-used t)
1616 (setq vars-used '(str strlen tmp1 tmp2))
1617 ;; Generate code to limit the string to a minimum size.
1618 (setq minform `(progn
1619 (setq str
1620 ,(ibuffer-compile-make-format-form
1621 'str
1622 `(- ,(if (integerp min)
1624 'min)
1625 strlen)
1626 align)))))
1627 (when (or (not (integerp max)) (> max 0))
1628 (setq max-used t)
1629 (cl-pushnew 'str vars-used)
1630 ;; Generate code to limit the string to a maximum size.
1631 (setq maxform `(progn
1632 (setq str
1633 ,(ibuffer-compile-make-substring-form
1634 'str
1635 (if (integerp max)
1637 'max)
1638 from-end-p))
1639 (setq strlen (length str))
1640 (setq str
1641 ,(ibuffer-compile-make-eliding-form
1642 'str elide from-end-p)))))
1643 ;; Now, put these forms together with the rest of the code.
1644 (let ((callform
1645 ;; Is this an "inline" column? This means we have
1646 ;; to get the code from the
1647 ;; `ibuffer-inline-columns' alist and insert it
1648 ;; into our generated code. Otherwise, we just
1649 ;; generate a call to the column function.
1650 (ibuffer-aif (assq sym ibuffer-inline-columns)
1651 (nth 1 it)
1652 `(,sym buffer mark)))
1653 ;; You're not expected to understand this. Hell, I
1654 ;; don't even understand it, and I wrote it five
1655 ;; minutes ago.
1656 (insertgenfn
1657 (if (get sym 'ibuffer-column-summarizer)
1658 ;; I really, really wish Emacs Lisp had closures.
1659 ;; FIXME: Elisp does have them now.
1660 (lambda (arg sym)
1661 `(insert
1662 (let ((ret ,arg))
1663 (put ',sym 'ibuffer-column-summary
1664 (cons ret (get ',sym
1665 'ibuffer-column-summary)))
1666 ret)))
1667 (lambda (arg _sym)
1668 `(insert ,arg))))
1669 (mincompform `(< strlen ,(if (integerp min)
1671 'min)))
1672 (maxcompform `(> strlen ,(if (integerp max)
1674 'max))))
1675 (if (or min-used max-used)
1676 ;; The complex case, where we have to limit the
1677 ;; form to a maximum or minimum size.
1678 (progn
1679 (when (and min-used (not (integerp min)))
1680 (push `(min ,min) letbindings))
1681 (when (and max-used (not (integerp max)))
1682 (push `(max ,max) letbindings))
1683 (push
1684 (if (and min-used max-used)
1685 `(if ,mincompform
1686 ,minform
1687 (if ,maxcompform
1688 ,maxform))
1689 (if min-used
1690 `(when ,mincompform
1691 ,minform)
1692 `(when ,maxcompform
1693 ,maxform)))
1694 outforms)
1695 (push `(setq str ,callform
1696 ,@(when strlen-used
1697 `(strlen (length str))))
1698 outforms)
1699 (setq outforms
1700 (append outforms
1701 (list (funcall insertgenfn 'str sym)))))
1702 ;; The simple case; just insert the string.
1703 (push (funcall insertgenfn callform sym) outforms))
1704 ;; Finally, return a `let' form which binds the
1705 ;; variables in `letbindings', and contains all the
1706 ;; code in `outforms'.
1707 `(let ,letbindings
1708 ,@outforms)))))
1709 result))
1710 ;; We don't want to unconditionally load the byte-compiler.
1711 (funcall (if (or ibuffer-always-compile-formats
1712 (featurep 'bytecomp))
1713 #'byte-compile
1714 #'identity)
1715 ;; Here, we actually create a lambda form which
1716 ;; inserts all the generated forms for each entry
1717 ;; in the format string.
1718 `(lambda (buffer mark)
1719 (let ,vars-used
1720 ,@(nreverse result))))))
1722 (defun ibuffer-recompile-formats ()
1723 "Recompile `ibuffer-formats'."
1724 (interactive)
1725 (setq ibuffer-compiled-formats
1726 (mapcar #'ibuffer-compile-format ibuffer-formats))
1727 (when (boundp 'ibuffer-filter-format-alist)
1728 (setq ibuffer-compiled-filter-formats
1729 (mapcar (lambda (entry)
1730 (cons (car entry)
1731 (mapcar (lambda (formats)
1732 (mapcar #'ibuffer-compile-format formats))
1733 (cdr entry))))
1734 ibuffer-filter-format-alist))))
1736 (defun ibuffer-clear-summary-columns (format)
1737 (dolist (form format)
1738 (when (and (consp form)
1739 (get (car form) 'ibuffer-column-summarizer))
1740 (put (car form) 'ibuffer-column-summary nil))))
1742 (defun ibuffer-check-formats ()
1743 (when (null ibuffer-formats)
1744 (error "No formats!"))
1745 (let ((ext-loaded (featurep 'ibuf-ext)))
1746 (when (or (null ibuffer-compiled-formats)
1747 (null ibuffer-cached-formats)
1748 (not (eq ibuffer-cached-formats ibuffer-formats))
1749 (null ibuffer-cached-eliding-string)
1750 (not (equal ibuffer-cached-eliding-string ibuffer-eliding-string))
1751 (eql 0 ibuffer-cached-elide-long-columns)
1752 (not (eql ibuffer-cached-elide-long-columns
1753 (with-no-warnings ibuffer-elide-long-columns)))
1754 (and ext-loaded
1755 (not (eq ibuffer-cached-filter-formats
1756 ibuffer-filter-format-alist))
1757 (and ibuffer-filter-format-alist
1758 (null ibuffer-compiled-filter-formats))))
1759 (message "Formats have changed, recompiling...")
1760 (ibuffer-recompile-formats)
1761 (setq ibuffer-cached-formats ibuffer-formats
1762 ibuffer-cached-eliding-string ibuffer-eliding-string
1763 ibuffer-cached-elide-long-columns (with-no-warnings ibuffer-elide-long-columns))
1764 (when ext-loaded
1765 (setq ibuffer-cached-filter-formats ibuffer-filter-format-alist))
1766 (message "Formats have changed, recompiling...done"))))
1768 (defvar ibuffer-inline-columns nil)
1770 (defface ibuffer-locked-buffer
1771 '((((background dark)) (:foreground "RosyBrown"))
1772 (t (:foreground "brown4")))
1773 "*Face used for locked buffers in Ibuffer."
1774 :version "25.2"
1775 :group 'ibuffer
1776 :group 'font-lock-highlighting-faces)
1777 (defvar ibuffer-locked-buffer 'ibuffer-locked-buffer)
1779 (define-ibuffer-column mark (:name " " :inline t)
1780 (string mark))
1782 (define-ibuffer-column read-only (:name "R" :inline t)
1783 (if buffer-read-only
1784 (string ibuffer-read-only-char)
1785 " "))
1787 (define-ibuffer-column locked
1788 (:name "L" :inline t :props ('font-lock-face 'ibuffer-locked-buffer))
1789 (if (and (boundp 'emacs-lock-mode) emacs-lock-mode)
1790 (string ibuffer-locked-char)
1791 " "))
1793 (define-ibuffer-column modified (:name "M" :inline t)
1794 (if (buffer-modified-p)
1795 (string ibuffer-modified-char)
1796 " "))
1798 (define-ibuffer-column name
1799 (:inline t
1800 :header-mouse-map ibuffer-name-header-map
1801 :props
1802 ('mouse-face 'highlight 'keymap ibuffer-name-map
1803 'ibuffer-name-column t
1804 'help-echo '(if tooltip-mode
1805 "mouse-1: mark this buffer\nmouse-2: select this buffer\nmouse-3: operate on this buffer"
1806 "mouse-1: mark buffer mouse-2: select buffer mouse-3: operate"))
1807 :summarizer
1808 (lambda (strings)
1809 (let ((bufs (length strings)))
1810 (cond ((zerop bufs) "No buffers")
1811 ((= 1 bufs) "1 buffer")
1812 (t (format "%s buffers" bufs))))))
1813 (let ((string (propertize (buffer-name)
1814 'font-lock-face
1815 (ibuffer-buffer-name-face buffer mark))))
1816 (if (not (seq-position string ?\n))
1817 string
1818 (replace-regexp-in-string
1819 "\n" (propertize "^J" 'font-lock-face 'escape-glyph) string))))
1821 (define-ibuffer-column size
1822 (:inline t
1823 :header-mouse-map ibuffer-size-header-map
1824 :summarizer
1825 (lambda (column-strings)
1826 (let ((total 0))
1827 (dolist (string column-strings)
1828 (setq total
1829 ;; like, ewww ...
1830 (+ (float (string-to-number string))
1831 total)))
1832 (format "%.0f" total))))
1833 (format "%s" (buffer-size)))
1835 (define-ibuffer-column mode
1836 (:inline t
1837 :header-mouse-map ibuffer-mode-header-map
1838 :props
1839 ('mouse-face 'highlight
1840 'keymap ibuffer-mode-name-map
1841 'help-echo "mouse-2: filter by this mode"))
1842 (format-mode-line mode-name nil nil (current-buffer)))
1844 (define-ibuffer-column process
1845 (:summarizer
1846 (lambda (strings)
1847 (let ((total (length (delete "" strings))))
1848 (cond ((zerop total) "No processes")
1849 ((= 1 total) "1 process")
1850 (t (format "%d processes" total))))))
1851 (ibuffer-aif (get-buffer-process buffer)
1852 (format "(%s %s)" it (process-status it))
1853 ""))
1855 (define-ibuffer-column filename
1856 (:summarizer
1857 (lambda (strings)
1858 (let ((total (length (delete "" strings))))
1859 (cond ((zerop total) "No files")
1860 ((= 1 total) "1 file")
1861 (t (format "%d files" total))))))
1862 (let ((directory-abbrev-alist ibuffer-directory-abbrev-alist))
1863 (abbreviate-file-name
1864 (or (ibuffer-buffer-file-name) ""))))
1866 (define-ibuffer-column filename-and-process
1867 (:name "Filename/Process"
1868 :header-mouse-map ibuffer-filename/process-header-map
1869 :summarizer
1870 (lambda (strings)
1871 (setq strings (delete "" strings))
1872 (let ((procs 0)
1873 (files 0))
1874 (dolist (string strings)
1875 (if (string-match "\\(?:\\`([[:ascii:]]+)\\)" string)
1876 (progn (setq procs (1+ procs))
1877 (if (< (match-end 0) (length string))
1878 (setq files (1+ files))))
1879 (setq files (1+ files))))
1880 (concat (cond ((zerop files) "No files")
1881 ((= 1 files) "1 file")
1882 (t (format "%d files" files)))
1883 ", "
1884 (cond ((zerop procs) "no processes")
1885 ((= 1 procs) "1 process")
1886 (t (format "%d processes" procs)))))))
1887 (let ((proc (get-buffer-process buffer))
1888 (filename (ibuffer-make-column-filename buffer mark)))
1889 (if proc
1890 (concat (propertize (format "(%s %s)" proc (process-status proc))
1891 'font-lock-face 'italic)
1892 (if (> (length filename) 0)
1893 (format " %s" filename)
1894 ""))
1895 filename)))
1897 (defun ibuffer-format-column (str width alignment)
1898 (let ((left (make-string (/ width 2) ?\s))
1899 (right (make-string (- width (/ width 2)) ?\s)))
1900 (pcase alignment
1901 (:right (concat left right str))
1902 (:center (concat left str right))
1903 (_ (concat str left right)))))
1905 (defun ibuffer-buffer-name-face (buf mark)
1906 (cond ((char-equal mark ibuffer-marked-char)
1907 ibuffer-marked-face)
1908 ((char-equal mark ibuffer-deletion-char)
1909 ibuffer-deletion-face)
1911 (let ((level -1)
1912 result)
1913 (dolist (e ibuffer-fontification-alist result)
1914 (when (and (> (car e) level)
1915 (with-current-buffer buf
1916 (eval (nth 1 e))))
1917 (setq level (car e)
1918 result (nth 2 e))))))))
1920 (defun ibuffer-insert-buffer-line (buffer mark format)
1921 "Insert a line describing BUFFER and MARK using FORMAT."
1922 (ibuffer-assert-ibuffer-mode)
1923 (let ((beg (point)))
1924 (funcall format buffer mark)
1925 (put-text-property beg (point) 'ibuffer-properties (list buffer mark)))
1926 (insert "\n"))
1928 ;; This function knows a bit too much of the internals. It would be
1929 ;; nice if it was all abstracted away.
1930 (defun ibuffer-redisplay-current ()
1931 (ibuffer-assert-ibuffer-mode)
1932 (when (eobp)
1933 (forward-line -1))
1934 (beginning-of-line)
1935 (let ((curformat (mapcar #'ibuffer-expand-format-entry
1936 (ibuffer-current-format t))))
1937 (ibuffer-clear-summary-columns curformat)
1938 (let ((buf (ibuffer-current-buffer)))
1939 (when buf
1940 (let ((mark (ibuffer-current-mark)))
1941 (save-excursion
1942 (delete-region (point) (1+ (line-end-position)))
1943 (ibuffer-insert-buffer-line
1944 buf mark
1945 (ibuffer-current-format)))
1946 (when ibuffer-shrink-to-minimum-size
1947 (ibuffer-shrink-to-fit)))))))
1949 (defun ibuffer-map-on-mark (mark func)
1950 (ibuffer-map-lines
1951 (lambda (buf mk)
1952 (if (char-equal mark mk)
1953 (funcall func buf mark)
1954 nil))))
1956 (defun ibuffer-map-lines (function &optional nomodify group)
1957 "Call FUNCTION for each buffer.
1958 Set the ibuffer modification flag unless NOMODIFY is non-nil.
1960 If optional argument GROUP is non-nil, then only call FUNCTION on
1961 buffers in filtering group GROUP.
1963 FUNCTION is called with two arguments:
1964 the buffer object itself and the current mark symbol."
1965 (ibuffer-assert-ibuffer-mode)
1966 (ibuffer-forward-line 0)
1967 (let* ((orig-target-line (1+ (count-lines (save-excursion
1968 (goto-char (point-min))
1969 (ibuffer-forward-line 0)
1970 (point))
1971 (point))))
1972 (target-line-offset orig-target-line)
1973 (ibuffer-map-lines-total 0)
1974 (ibuffer-map-lines-count 0))
1975 (unwind-protect
1976 (progn
1977 (setq buffer-read-only nil)
1978 (goto-char (point-min))
1979 (ibuffer-forward-line 0 t)
1980 (while (and (not (eobp))
1981 (not (get-text-property (point) 'ibuffer-summary))
1982 (progn
1983 (ibuffer-forward-line 0 t)
1984 (and (not (eobp))
1985 (not (get-text-property (point) 'ibuffer-summary)))))
1986 (let ((result
1987 (if (buffer-live-p (ibuffer-current-buffer))
1988 (when (or (null group)
1989 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group)
1990 (equal group it)))
1991 (save-excursion
1992 (funcall function
1993 (ibuffer-current-buffer)
1994 (ibuffer-current-mark))))
1995 ;; Kill the line if the buffer is dead
1996 'kill)))
1997 ;; A given mapping function should return:
1998 ;; nil if it chose not to affect the buffer
1999 ;; `kill' means the remove line from the buffer list
2000 ;; t otherwise
2001 (cl-incf ibuffer-map-lines-total)
2002 (cond ((null result)
2003 (forward-line 1))
2004 ((eq result 'kill)
2005 (delete-region (line-beginning-position)
2006 (1+ (line-end-position)))
2007 (cl-incf ibuffer-map-lines-count)
2008 (when (< ibuffer-map-lines-total
2009 orig-target-line)
2010 (cl-decf target-line-offset)))
2012 (cl-incf ibuffer-map-lines-count)
2013 (forward-line 1)))))
2014 ibuffer-map-lines-count)
2015 (progn
2016 (setq buffer-read-only t)
2017 (unless nomodify
2018 (set-buffer-modified-p nil))
2019 (goto-char (point-min))
2020 (ibuffer-forward-line 0)
2021 (ibuffer-forward-line (1- target-line-offset))))))
2023 (defun ibuffer-get-marked-buffers ()
2024 "Return a list of buffer objects currently marked."
2025 (delq nil
2026 (mapcar (lambda (e)
2027 (when (eq (cdr e) ibuffer-marked-char)
2028 (car e)))
2029 (ibuffer-current-state-list))))
2031 (defun ibuffer-current-state-list (&optional pos)
2032 "Return a list like (BUF . MARK) of all buffers in an ibuffer.
2033 If POS is non-nil, return a list like (BUF MARK POINT), where POINT is
2034 the value of point at the beginning of the line for that buffer."
2035 (let ((ibuffer-current-state-list-tmp '()))
2036 ;; ah, if only we had closures. I bet this will mysteriously
2037 ;; break later. Don't blame me.
2038 (if pos
2039 (ibuffer-map-lines-nomodify
2040 (lambda (buf mark)
2041 (when (buffer-live-p buf)
2042 (push (list buf mark (point)) ibuffer-current-state-list-tmp))))
2043 (ibuffer-map-lines-nomodify
2044 (lambda (buf mark)
2045 (when (buffer-live-p buf)
2046 (push (cons buf mark) ibuffer-current-state-list-tmp)))))
2047 (nreverse ibuffer-current-state-list-tmp)))
2049 (defun ibuffer-current-buffers-with-marks (curbufs)
2050 "Return a list like (BUF . MARK) of all open buffers."
2051 (let ((bufs (ibuffer-current-state-list)))
2052 (mapcar (lambda (buf) (let ((e (assq buf bufs)))
2053 (if e
2055 (cons buf ?\s))))
2056 curbufs)))
2058 (defun ibuffer-buf-matches-predicates (buf predicates)
2059 (let ((hit nil)
2060 (name (buffer-name buf)))
2061 (dolist (pred predicates)
2062 (when (if (stringp pred)
2063 (string-match pred name)
2064 (funcall pred buf))
2065 (setq hit t)))
2066 hit))
2068 (defun ibuffer-filter-buffers (ibuffer-buf last bmarklist all)
2069 (let ((ext-loaded (featurep 'ibuf-ext)))
2070 (delq nil
2071 (mapcar
2072 ;; element should be like (BUFFER . MARK)
2073 (lambda (e)
2074 (let* ((buf (car e)))
2075 (when
2076 ;; This takes precedence over anything else
2077 (or (and ibuffer-always-show-last-buffer
2078 (eq last buf))
2079 (funcall (if ext-loaded
2080 #'ibuffer-ext-visible-p
2081 #'ibuffer-visible-p)
2082 buf all ibuffer-buf))
2083 e)))
2084 bmarklist))))
2086 (defun ibuffer-visible-p (buf all &optional ibuffer-buf)
2087 (and (or all
2088 (not
2089 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates)))
2090 (or ibuffer-view-ibuffer
2091 (and ibuffer-buf
2092 (not (eq ibuffer-buf buf))))))
2094 ;; This function is a special case; it's not defined by
2095 ;; `define-ibuffer-sorter'.
2096 (defun ibuffer-do-sort-by-recency ()
2097 "Sort the buffers by last view time."
2098 (interactive)
2099 (setq ibuffer-sorting-mode 'recency)
2100 (when (eq ibuffer-last-sorting-mode 'recency)
2101 (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep)))
2102 (ibuffer-update nil t)
2103 (setq ibuffer-last-sorting-mode 'recency))
2105 (defun ibuffer-update-format ()
2106 (when (null ibuffer-current-format)
2107 (setq ibuffer-current-format 0))
2108 (when (null ibuffer-formats)
2109 (error "Ibuffer error: no formats!")))
2111 (defun ibuffer-switch-format ()
2112 "Switch the current display format."
2113 (interactive)
2114 (ibuffer-assert-ibuffer-mode)
2115 (unless (consp ibuffer-formats)
2116 (error "Ibuffer error: No formats!"))
2117 (setq ibuffer-current-format
2118 (if (>= ibuffer-current-format (1- (length (ibuffer-current-formats nil))))
2120 (1+ ibuffer-current-format)))
2121 (ibuffer-update-format)
2122 (ibuffer-redisplay t))
2124 (defun ibuffer-update-title-and-summary (format)
2125 (ibuffer-assert-ibuffer-mode)
2126 ;; Don't do funky font-lock stuff here
2127 (let ((inhibit-modification-hooks t))
2128 (if (get-text-property (point-min) 'ibuffer-title)
2129 (delete-region (point-min)
2130 (next-single-property-change
2131 (point-min) 'ibuffer-title)))
2132 (goto-char (point-min))
2133 (add-text-properties
2134 (point)
2135 (progn
2136 (let ((opos (point)))
2137 ;; Insert the title names.
2138 (dolist (element format)
2139 (insert
2140 (if (stringp element)
2141 element
2142 (pcase-let ((`(,sym ,min ,_max ,align) element))
2143 ;; Ignore a negative min when we're inserting the title
2144 (when (cl-minusp min)
2145 (setq min (- min)))
2146 (let* ((name (or (get sym 'ibuffer-column-name)
2147 (error "Unknown column %s in ibuffer-formats" sym)))
2148 (len (length name))
2149 (hmap (get sym 'header-mouse-map))
2150 (strname (if (< len min)
2151 (ibuffer-format-column name
2152 (- min len)
2153 align)
2154 name)))
2155 (when hmap
2156 (setq
2157 strname
2158 (propertize strname 'mouse-face 'highlight 'keymap hmap)))
2159 strname)))))
2160 (add-text-properties opos (point) `(ibuffer-title-header t))
2161 (insert "\n")
2162 ;; Add the underlines
2163 (let ((str (save-excursion
2164 (forward-line -1)
2165 (beginning-of-line)
2166 (buffer-substring (point) (line-end-position)))))
2167 (apply #'insert (mapcar
2168 (lambda (c)
2169 (if (not (or (char-equal c ?\s)
2170 (char-equal c ?\n)))
2172 ?\s))
2173 str)))
2174 (insert "\n"))
2175 (point))
2176 `(ibuffer-title t font-lock-face ,ibuffer-title-face))
2177 ;; Now, insert the summary columns.
2178 (goto-char (point-max))
2179 (if (get-text-property (1- (point-max)) 'ibuffer-summary)
2180 (delete-region (previous-single-property-change
2181 (point-max) 'ibuffer-summary)
2182 (point-max)))
2183 (if ibuffer-display-summary
2184 (add-text-properties
2185 (point)
2186 (progn
2187 (insert "\n")
2188 (dolist (element format)
2189 (insert
2190 (if (stringp element)
2191 (make-string (length element) ?\s)
2192 (pcase-let ((`(,sym ,min ,_max ,align) element))
2193 ;; Ignore a negative min when we're inserting the title.
2194 (when (cl-minusp min)
2195 (setq min (- min)))
2196 (let* ((summary
2197 (if (get sym 'ibuffer-column-summarizer)
2198 (funcall (get sym 'ibuffer-column-summarizer)
2199 (get sym 'ibuffer-column-summary))
2200 (make-string
2201 (length (get sym 'ibuffer-column-name))
2202 ?\s)))
2203 (len (length summary)))
2204 (if (< len min)
2205 (ibuffer-format-column summary
2206 (- min len)
2207 align)
2208 summary))))))
2209 (point))
2210 `(ibuffer-summary t)))))
2213 (defun ibuffer-redisplay (&optional silent)
2214 "Redisplay the current list of buffers.
2215 This does not show new buffers; use `ibuffer-update' for that.
2217 If optional arg SILENT is non-nil, do not display progress messages."
2218 (interactive)
2219 (ibuffer-forward-line 0)
2220 (unless silent
2221 (message "Redisplaying current buffer list..."))
2222 (let ((blist (ibuffer-current-state-list)))
2223 (when (and (null blist)
2224 (featurep 'ibuf-ext)
2225 (or ibuffer-filtering-qualifiers ibuffer-hidden-filter-groups))
2226 (message "No buffers! (note: filtering in effect)"))
2227 (ibuffer-redisplay-engine blist t)
2228 (unless silent
2229 (message "Redisplaying current buffer list...done"))
2230 (ibuffer-forward-line 0)))
2232 (defun ibuffer-update (arg &optional silent)
2233 "Regenerate the list of all buffers.
2235 Prefix arg non-nil means to toggle whether buffers that match
2236 `ibuffer-maybe-show-predicates' should be displayed.
2238 If optional arg SILENT is non-nil, do not display progress messages."
2239 (interactive "P")
2240 (if arg
2241 (setq ibuffer-display-maybe-show-predicates
2242 (not ibuffer-display-maybe-show-predicates)))
2243 (ibuffer-forward-line 0)
2244 (let* ((bufs (buffer-list))
2245 (blist (ibuffer-filter-buffers
2246 (current-buffer)
2247 (if (and
2248 (cadr bufs)
2249 (eq ibuffer-always-show-last-buffer
2250 :nomini)
2251 (minibufferp (cadr bufs)))
2252 (nth 2 bufs)
2253 (cadr bufs))
2254 (ibuffer-current-buffers-with-marks bufs)
2255 ibuffer-display-maybe-show-predicates)))
2256 (and (null blist)
2257 (featurep 'ibuf-ext)
2258 ibuffer-filtering-qualifiers
2259 (message "No buffers! (note: filtering in effect)"))
2260 (unless silent
2261 (message "Updating buffer list..."))
2262 (ibuffer-redisplay-engine blist arg)
2263 (unless silent
2264 (message "Updating buffer list...done")))
2265 (if (eq ibuffer-shrink-to-minimum-size 'onewindow)
2266 (ibuffer-shrink-to-fit t)
2267 (when ibuffer-shrink-to-minimum-size
2268 (ibuffer-shrink-to-fit)))
2269 (ibuffer-forward-line 0)
2270 ;; I tried to update this automatically from the mode-line-process format,
2271 ;; but changing nil-ness of header-line-format while computing
2272 ;; mode-line-format is asking a bit too much it seems. --Stef
2273 (setq header-line-format
2274 (and ibuffer-use-header-line
2275 ibuffer-filtering-qualifiers
2276 ibuffer-header-line-format)))
2278 (defun ibuffer-sort-bufferlist (bmarklist)
2279 (unless ibuffer-sorting-functions-alist
2280 ;; make sure the sorting functions are loaded
2281 (require 'ibuf-ext))
2282 (let* ((sortdat (assq ibuffer-sorting-mode
2283 ibuffer-sorting-functions-alist))
2284 (func (nth 2 sortdat)))
2285 (let ((result
2286 ;; actually sort the buffers
2287 (if (and sortdat func)
2288 (sort bmarklist func)
2289 bmarklist)))
2290 ;; perhaps reverse the sorted buffer list
2291 (if ibuffer-sorting-reversep
2292 (nreverse result)
2293 result))))
2295 (defun ibuffer-insert-filter-group (name display-name filter-string format bmarklist)
2296 (add-text-properties
2297 (point)
2298 (progn
2299 (insert "[ " display-name " ]")
2300 (point))
2301 `(ibuffer-filter-group-name
2302 ,name
2303 font-lock-face ,ibuffer-filter-group-name-face
2304 keymap ,ibuffer-mode-filter-group-map
2305 mouse-face highlight
2306 help-echo ,(let ((echo '(if tooltip-mode
2307 "mouse-1: toggle marks in this group\nmouse-2: hide/show this filtering group"
2308 "mouse-1: toggle marks mouse-2: hide/show")))
2309 (if (> (length filter-string) 0)
2310 `(concat ,filter-string
2311 (if tooltip-mode "\n" " ")
2312 ,echo)
2313 echo))))
2314 (insert "\n")
2315 (when bmarklist
2316 (put-text-property
2317 (point)
2318 (progn
2319 (dolist (entry bmarklist)
2320 (ibuffer-insert-buffer-line (car entry) (cdr entry) format))
2321 (point))
2322 'ibuffer-filter-group
2323 name)))
2325 (defun ibuffer-redisplay-engine (bmarklist &optional _ignore)
2326 (ibuffer-assert-ibuffer-mode)
2327 (let* ((--ibuffer-insert-buffers-and-marks-format
2328 (ibuffer-current-format))
2329 (--ibuffer-expanded-format (mapcar #'ibuffer-expand-format-entry
2330 (ibuffer-current-format t)))
2331 (orig (count-lines (point-min) (point)))
2332 ;; Inhibit font-lock caching tricks, since we're modifying the
2333 ;; entire buffer at once
2334 (inhibit-modification-hooks t)
2335 (ext-loaded (featurep 'ibuf-ext))
2336 (bgroups (if ext-loaded
2337 (ibuffer-generate-filter-groups bmarklist)
2338 (list (cons "Default" bmarklist)))))
2339 (ibuffer-clear-summary-columns --ibuffer-expanded-format)
2340 (unwind-protect
2341 (progn
2342 (setq buffer-read-only nil)
2343 (erase-buffer)
2344 (ibuffer-update-format)
2345 (dolist (group (nreverse bgroups))
2346 (let* ((name (car group))
2347 (disabled (and ext-loaded
2348 (member name ibuffer-hidden-filter-groups)))
2349 (bmarklist (cdr group)))
2350 (unless (and (null bmarklist)
2351 (not disabled)
2352 ext-loaded
2353 (null ibuffer-show-empty-filter-groups))
2354 (ibuffer-insert-filter-group
2355 name
2356 (if disabled (concat name " ...") name)
2357 (if ext-loaded
2358 (ibuffer-format-filter-group-data name)
2360 --ibuffer-insert-buffers-and-marks-format
2361 (if disabled
2363 (ibuffer-sort-bufferlist bmarklist))))))
2364 (ibuffer-update-title-and-summary --ibuffer-expanded-format))
2365 (setq buffer-read-only t)
2366 (set-buffer-modified-p ibuffer-did-modification)
2367 (setq ibuffer-did-modification nil)
2368 (goto-char (point-min))
2369 (forward-line orig))))
2371 ;;;###autoload
2372 (defun ibuffer-list-buffers (&optional files-only)
2373 "Display a list of buffers, in another window.
2374 If optional argument FILES-ONLY is non-nil, then add a filter for
2375 buffers which are visiting a file."
2376 (interactive "P")
2377 (ibuffer t nil (when files-only
2378 '((filename . ".*"))) t))
2380 ;;;###autoload
2381 (defun ibuffer-other-window (&optional files-only)
2382 "Like `ibuffer', but displayed in another window by default.
2383 If optional argument FILES-ONLY is non-nil, then add a filter for
2384 buffers which are visiting a file."
2385 (interactive "P")
2386 (ibuffer t nil (when files-only
2387 '((filename . ".*")))))
2389 ;;;###autoload
2390 (defun ibuffer (&optional other-window-p name qualifiers noselect
2391 shrink filter-groups formats)
2392 "Begin using Ibuffer to edit a list of buffers.
2393 Type `h' after entering ibuffer for more information.
2395 All arguments are optional.
2396 OTHER-WINDOW-P says to use another window.
2397 NAME specifies the name of the buffer (defaults to \"*Ibuffer*\").
2398 QUALIFIERS is an initial set of filtering qualifiers to use;
2399 see `ibuffer-filtering-qualifiers'.
2400 NOSELECT means don't select the Ibuffer buffer.
2401 SHRINK means shrink the buffer to minimal size. The special
2402 value `onewindow' means always use another window.
2403 FILTER-GROUPS is an initial set of filtering groups to use;
2404 see `ibuffer-filter-groups'.
2405 FORMATS is the value to use for `ibuffer-formats'.
2406 If specified, then the variable `ibuffer-formats' will have
2407 that value locally in this buffer."
2408 (interactive "P")
2409 (when ibuffer-use-other-window
2410 (setq other-window-p t))
2411 (let ((buf (get-buffer-create (or name "*Ibuffer*"))))
2412 (if other-window-p
2413 (or (and noselect (display-buffer buf t))
2414 (pop-to-buffer buf t))
2415 (funcall (if noselect #'display-buffer #'switch-to-buffer) buf))
2416 (with-current-buffer buf
2417 (save-selected-window
2418 ;; We switch to the buffer's window in order to be able
2419 ;; to modify the value of point
2420 (select-window (get-buffer-window buf 0))
2421 (or (derived-mode-p 'ibuffer-mode)
2422 (ibuffer-mode))
2423 (when shrink
2424 (setq ibuffer-shrink-to-minimum-size shrink))
2425 (when qualifiers
2426 (require 'ibuf-ext)
2427 (setq ibuffer-filtering-qualifiers qualifiers))
2428 (when filter-groups
2429 (require 'ibuf-ext)
2430 (setq ibuffer-filter-groups filter-groups))
2431 (when formats
2432 (set (make-local-variable 'ibuffer-formats) formats))
2433 (ibuffer-update nil)
2434 ;; Skip the group name by default.
2435 (ibuffer-forward-line 0 t)
2436 (unwind-protect
2437 (progn
2438 (setq buffer-read-only nil)
2439 (run-hooks 'ibuffer-hook))
2440 (setq buffer-read-only t))
2441 (unless ibuffer-expert
2442 (message "Commands: m, u, t, RET, g, k, S, D, Q; q to quit; h for help"))))))
2444 (put 'ibuffer-mode 'mode-class 'special)
2445 (define-derived-mode ibuffer-mode special-mode "IBuffer"
2446 "A major mode for viewing a list of buffers.
2447 In Ibuffer, you can conveniently perform many operations on the
2448 currently open buffers, in addition to filtering your view to a
2449 particular subset of them, and sorting by various criteria.
2451 Operations on marked buffers:
2452 \\<ibuffer-mode-map>
2453 `\\[ibuffer-do-save]' - Save the marked buffers
2454 `\\[ibuffer-do-view]' - View the marked buffers in this frame.
2455 `\\[ibuffer-do-view-other-frame]' - View the marked buffers in another frame.
2456 `\\[ibuffer-do-revert]' - Revert the marked buffers.
2457 `\\[ibuffer-do-toggle-read-only]' - Toggle read-only state of marked buffers.
2458 `\\[ibuffer-do-delete]' - Kill the marked buffers.
2459 `\\[ibuffer-do-isearch]' - Do incremental search in the marked buffers.
2460 `\\[ibuffer-do-isearch-regexp]' - Isearch for regexp in the marked buffers.
2461 `\\[ibuffer-do-replace-regexp]' - Replace by regexp in each of the marked
2462 buffers.
2463 `\\[ibuffer-do-query-replace]' - Query replace in each of the marked buffers.
2464 `\\[ibuffer-do-query-replace-regexp]' - As above, with a regular expression.
2465 `\\[ibuffer-do-print]' - Print the marked buffers.
2466 `\\[ibuffer-do-occur]' - List lines in all marked buffers which match
2467 a given regexp (like the function `occur').
2468 `\\[ibuffer-do-shell-command-pipe]' - Pipe the contents of the marked
2469 buffers to a shell command.
2470 `\\[ibuffer-do-shell-command-pipe-replace]' - Replace the contents of the marked
2471 buffers with the output of a shell command.
2472 `\\[ibuffer-do-shell-command-file]' - Run a shell command with the
2473 buffer's file as an argument.
2474 `\\[ibuffer-do-eval]' - Evaluate a form in each of the marked buffers. This
2475 is a very flexible command. For example, if you want to make all
2476 of the marked buffers read only, try using (read-only-mode 1) as
2477 the input form.
2478 `\\[ibuffer-do-view-and-eval]' - As above, but view each buffer while the form
2479 is evaluated.
2480 `\\[ibuffer-do-kill-lines]' - Remove the marked lines from the *Ibuffer* buffer,
2481 but don't kill the associated buffer.
2482 `\\[ibuffer-do-kill-on-deletion-marks]' - Kill all buffers marked for deletion.
2484 Marking commands:
2486 `\\[ibuffer-mark-forward]' - Mark the buffer at point.
2487 `\\[ibuffer-toggle-marks]' - Unmark all currently marked buffers, and mark
2488 all unmarked buffers.
2489 `\\[ibuffer-change-marks]' - Change the mark used on marked buffers.
2490 `\\[ibuffer-unmark-forward]' - Unmark the buffer at point.
2491 `\\[ibuffer-unmark-backward]' - Unmark the buffer at point, and move to the
2492 previous line.
2493 `\\[ibuffer-unmark-all]' - Unmark buffers marked with MARK.
2494 `\\[ibuffer-unmark-all-marks]' - Unmark all marked buffers.
2495 `\\[ibuffer-mark-by-mode]' - Mark buffers by major mode.
2496 `\\[ibuffer-mark-unsaved-buffers]' - Mark all \"unsaved\" buffers.
2497 This means that the buffer is modified, and has an associated file.
2498 `\\[ibuffer-mark-modified-buffers]' - Mark all modified buffers,
2499 regardless of whether or not they have an associated file.
2500 `\\[ibuffer-mark-special-buffers]' - Mark all buffers whose name begins and
2501 ends with `*'.
2502 `\\[ibuffer-mark-dissociated-buffers]' - Mark all buffers which have
2503 an associated file, but that file doesn't currently exist.
2504 `\\[ibuffer-mark-read-only-buffers]' - Mark all read-only buffers.
2505 `\\[ibuffer-mark-dired-buffers]' - Mark buffers in `dired' mode.
2506 `\\[ibuffer-mark-help-buffers]' - Mark buffers in `help-mode', `apropos-mode', etc.
2507 `\\[ibuffer-mark-old-buffers]' - Mark buffers older than `ibuffer-old-time'.
2508 `\\[ibuffer-mark-for-delete]' - Mark the buffer at point for deletion.
2509 `\\[ibuffer-mark-by-name-regexp]' - Mark buffers by their name, using a regexp.
2510 `\\[ibuffer-mark-by-mode-regexp]' - Mark buffers by their major mode, using a regexp.
2511 `\\[ibuffer-mark-by-file-name-regexp]' - Mark buffers by their filename, using a regexp.
2512 `\\[ibuffer-mark-by-content-regexp]' - Mark buffers by their content, using a regexp.
2513 `\\[ibuffer-mark-by-locked]' - Mark all locked buffers.
2515 Filtering commands:
2517 `\\[ibuffer-filter-by-mode]' - Add a filter by any major mode.
2518 `\\[ibuffer-filter-by-used-mode]' - Add a filter by a major mode now in use.
2519 `\\[ibuffer-filter-by-derived-mode]' - Add a filter by derived mode.
2520 `\\[ibuffer-filter-by-name]' - Add a filter by buffer name.
2521 `\\[ibuffer-filter-by-content]' - Add a filter by buffer content.
2522 `\\[ibuffer-filter-by-filename]' - Add a filter by filename.
2523 `\\[ibuffer-filter-by-size-gt]' - Add a filter by buffer size.
2524 `\\[ibuffer-filter-by-size-lt]' - Add a filter by buffer size.
2525 `\\[ibuffer-filter-by-predicate]' - Add a filter by an arbitrary Lisp predicate.
2526 `\\[ibuffer-save-filters]' - Save the current filters with a name.
2527 `\\[ibuffer-switch-to-saved-filters]' - Switch to previously saved filters.
2528 `\\[ibuffer-add-saved-filters]' - Add saved filters to current filters.
2529 `\\[ibuffer-or-filter]' - Replace the top two filters with their logical OR.
2530 `\\[ibuffer-pop-filter]' - Remove the top filter.
2531 `\\[ibuffer-negate-filter]' - Invert the logical sense of the top filter.
2532 `\\[ibuffer-decompose-filter]' - Break down the topmost filter.
2533 `\\[ibuffer-filter-disable]' - Remove all filtering currently in effect.
2535 Filter group commands:
2537 `\\[ibuffer-filters-to-filter-group]' - Create filter group from filters.
2538 `\\[ibuffer-pop-filter-group]' - Remove top filter group.
2539 `\\[ibuffer-forward-filter-group]' - Move to the next filter group.
2540 `\\[ibuffer-backward-filter-group]' - Move to the previous filter group.
2541 `\\[ibuffer-clear-filter-groups]' - Remove all active filter groups.
2542 `\\[ibuffer-save-filter-groups]' - Save the current groups with a name.
2543 `\\[ibuffer-switch-to-saved-filter-groups]' - Restore previously saved groups.
2544 `\\[ibuffer-delete-saved-filter-groups]' - Delete previously saved groups.
2546 Sorting commands:
2548 `\\[ibuffer-toggle-sorting-mode]' - Rotate between the various sorting modes.
2549 `\\[ibuffer-invert-sorting]' - Reverse the current sorting order.
2550 `\\[ibuffer-do-sort-by-alphabetic]' - Sort the buffers lexicographically.
2551 `\\[ibuffer-do-sort-by-filename/process]' - Sort the buffers by the file name.
2552 `\\[ibuffer-do-sort-by-recency]' - Sort the buffers by last viewing time.
2553 `\\[ibuffer-do-sort-by-size]' - Sort the buffers by size.
2554 `\\[ibuffer-do-sort-by-major-mode]' - Sort the buffers by major mode.
2556 Other commands:
2558 `\\[ibuffer-update]' - Regenerate the list of all buffers.
2559 Prefix arg means to toggle whether buffers that match
2560 `ibuffer-maybe-show-predicates' should be displayed.
2562 `\\[ibuffer-switch-format]' - Change the current display format.
2563 `\\[forward-line]' - Move point to the next line.
2564 `\\[previous-line]' - Move point to the previous line.
2565 `\\[describe-mode]' - This help.
2566 `\\[ibuffer-diff-with-file]' - View the differences between this buffer
2567 and its associated file.
2568 `\\[ibuffer-visit-buffer]' - View the buffer on this line.
2569 `\\[ibuffer-visit-buffer-other-window]' - As above, but in another window.
2570 `\\[ibuffer-visit-buffer-other-window-noselect]' - As both above, but don't select
2571 the new window.
2572 `\\[ibuffer-bury-buffer]' - Bury (not kill!) the buffer on this line.
2574 ** Information on Filtering:
2576 You can filter your ibuffer view via different criteria. Each Ibuffer
2577 buffer has its own stack of active filters. For example, suppose you
2578 are working on an Emacs Lisp project. You can create an Ibuffer
2579 buffer displays buffers in just `emacs-lisp' modes via
2580 `\\[ibuffer-filter-by-mode] emacs-lisp-mode RET'. In this case, there
2581 is just one entry on the filtering stack.
2583 You can also combine filters. The various filtering commands push a
2584 new filter onto the stack, and the filters combine to show just
2585 buffers which satisfy ALL criteria on the stack. For example, suppose
2586 you only want to see buffers in `emacs-lisp' mode, whose names begin
2587 with \"gnus\". You can accomplish this via:
2589 \\[ibuffer-filter-by-mode] emacs-lisp-mode RET
2590 \\[ibuffer-filter-by-name] ^gnus RET
2592 Additionally, you can OR the top two filters together with
2593 `\\[ibuffer-or-filters]'. To see all buffers in either
2594 `emacs-lisp-mode' or `lisp-interaction-mode', type:
2596 \\[ibuffer-filter-by-mode] emacs-lisp-mode RET
2597 \\[ibuffer-filter-by-mode] lisp-interaction-mode RET
2598 \\[ibuffer-or-filters]
2600 Filters can also be saved and restored using mnemonic names: see the
2601 functions `ibuffer-save-filters' and `ibuffer-switch-to-saved-filters'.
2603 To remove the top filter on the stack, use `\\[ibuffer-pop-filter]', and
2604 to disable all filtering currently in effect, use
2605 `\\[ibuffer-filter-disable]'.
2607 ** Filter Groups:
2609 Once one has mastered filters, the next logical step up is \"filter
2610 groups\". A filter group is basically a named group of buffers which
2611 match a filter, which are displayed together in an Ibuffer buffer. To
2612 create a filter group, simply use the regular functions to create a
2613 filter, and then type `\\[ibuffer-filters-to-filter-group]'.
2615 A quick example will make things clearer. Suppose that one wants to
2616 group all of one's Emacs Lisp buffers together. To do this, type:
2618 \\[ibuffer-filter-by-mode] emacs-lisp-mode RET
2619 \\[ibuffer-filters-to-filter-group] emacs lisp buffers RET
2621 You may, of course, name the group whatever you want; it doesn't have
2622 to be \"emacs lisp buffers\". Filter groups may be composed of any
2623 arbitrary combination of filters.
2625 Just like filters themselves, filter groups act as a stack. Buffers
2626 will not be displayed multiple times if they would be included in
2627 multiple filter groups; instead, the first filter group is used. The
2628 filter groups are displayed in this order of precedence.
2630 You may rearrange filter groups by using the regular
2631 `\\[ibuffer-kill-line]' and `\\[ibuffer-yank]' pair. Yanked groups
2632 will be inserted before the group at point."
2633 ;; Include state info next to the mode name.
2634 (set (make-local-variable 'mode-line-process)
2635 '(" by "
2636 (ibuffer-sorting-mode (:eval (symbol-name ibuffer-sorting-mode))
2637 "view time")
2638 (ibuffer-sorting-reversep " [rev]")
2639 (ibuffer-auto-mode " (Auto)")
2640 ;; Only list the filters if they're not already in the header-line.
2641 (header-line-format
2643 (:eval (if (functionp 'ibuffer-format-qualifier)
2644 (mapconcat 'ibuffer-format-qualifier
2645 ibuffer-filtering-qualifiers ""))))))
2646 ;; `ibuffer-update' puts this on header-line-format when needed.
2647 (setq ibuffer-header-line-format
2648 ;; Display the part that won't be in the mode-line.
2649 `("" ,mode-name
2650 ,@(mapcar (lambda (elem)
2651 (if (eq (car-safe elem) 'header-line-format)
2652 (nth 2 elem) elem))
2653 mode-line-process)))
2655 (setq buffer-read-only t)
2656 (buffer-disable-undo)
2657 (setq truncate-lines ibuffer-truncate-lines)
2658 ;; This makes things less ugly for Emacs 21 users with a non-nil
2659 ;; `show-trailing-whitespace'.
2660 (setq show-trailing-whitespace nil)
2661 ;; disable `show-paren-mode' buffer-locally
2662 (if (bound-and-true-p show-paren-mode)
2663 (set (make-local-variable 'show-paren-mode) nil))
2664 (set (make-local-variable 'revert-buffer-function)
2665 #'ibuffer-update)
2666 (set (make-local-variable 'ibuffer-sorting-mode)
2667 ibuffer-default-sorting-mode)
2668 (set (make-local-variable 'ibuffer-sorting-reversep)
2669 ibuffer-default-sorting-reversep)
2670 (set (make-local-variable 'ibuffer-shrink-to-minimum-size)
2671 ibuffer-default-shrink-to-minimum-size)
2672 (set (make-local-variable 'ibuffer-display-maybe-show-predicates)
2673 ibuffer-default-display-maybe-show-predicates)
2674 (set (make-local-variable 'ibuffer-filtering-qualifiers) nil)
2675 (set (make-local-variable 'ibuffer-filter-groups) nil)
2676 (set (make-local-variable 'ibuffer-filter-group-kill-ring) nil)
2677 (set (make-local-variable 'ibuffer-hidden-filter-groups) nil)
2678 (set (make-local-variable 'ibuffer-compiled-formats) nil)
2679 (set (make-local-variable 'ibuffer-cached-formats) nil)
2680 (set (make-local-variable 'ibuffer-cached-eliding-string) nil)
2681 (set (make-local-variable 'ibuffer-cached-elide-long-columns) nil)
2682 (set (make-local-variable 'ibuffer-current-format) nil)
2683 (set (make-local-variable 'ibuffer-did-modification) nil)
2684 (set (make-local-variable 'ibuffer-tmp-hide-regexps) nil)
2685 (set (make-local-variable 'ibuffer-tmp-show-regexps) nil)
2686 (define-key ibuffer-mode-map [menu-bar edit] 'undefined)
2687 (define-key ibuffer-mode-map [menu-bar operate] (cons "Operate" ibuffer-mode-operate-map))
2688 (ibuffer-update-format)
2689 (when ibuffer-default-directory
2690 (setq default-directory ibuffer-default-directory))
2691 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t))
2693 (provide 'ibuffer)
2695 (run-hooks 'ibuffer-load-hook)
2697 ;;; ibuffer.el ends here