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