dired-mark-extension: Unmark if called with C-u prefix
[emacs.git] / lisp / ibuffer.el
blobe9655358c26218b5025774c67c2b389aa51ee157
1 ;;; ibuffer.el --- operate on buffers like dired -*- lexical-binding:t -*-
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
5 ;; Author: Colin Walters <walters@verbum.org>
6 ;; Maintainer: John Paul Wallington <jpw@gnu.org>
7 ;; Created: 8 Sep 2000
8 ;; Keywords: buffer, convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; ibuffer.el is an advanced replacement for the `buffer-menu' which
28 ;; is normally distributed with Emacs. Its interface is intended to
29 ;; be analogous to that of Dired.
31 ;;; Code:
33 (eval-when-compile
34 (require 'cl-lib)
35 (require 'ibuf-macs)
36 (require 'dired))
38 (require 'font-core)
39 (require 'seq)
41 (require 'ibuffer-loaddefs)
42 ;; These come from ibuf-ext.el, which can not be require'd at compile time
43 ;; because it has a recursive dependency on ibuffer.el
44 (defvar ibuffer-auto-mode)
45 (defvar ibuffer-cached-filter-formats)
46 (defvar ibuffer-compiled-filter-formats)
47 (defvar ibuffer-filter-format-alist)
48 (defvar ibuffer-filter-group-kill-ring)
49 (defvar ibuffer-filter-groups)
50 (defvar ibuffer-filtering-qualifiers)
51 (defvar ibuffer-header-line-format)
52 (defvar ibuffer-hidden-filter-groups)
53 (defvar ibuffer-inline-columns)
54 (defvar ibuffer-show-empty-filter-groups)
55 (defvar ibuffer-tmp-hide-regexps)
56 (defvar ibuffer-tmp-show-regexps)
58 (declare-function ibuffer-ext-visible-p "ibuf-ext"
59 (buf all &optional ibuffer-buf))
60 (declare-function ibuffer-mark-on-buffer "ibuf-ext"
61 (func &optional ibuffer-mark-on-buffer-mark group))
62 (declare-function ibuffer-generate-filter-groups "ibuf-ext"
63 (bmarklist &optional noempty nodefault))
64 (declare-function ibuffer-format-filter-group-data "ibuf-ext" (filter))
66 (defgroup ibuffer nil
67 "Advanced replacement for `buffer-menu'.
68 Ibuffer lets you operate on buffers in a Dired-like way,
69 with the ability to sort, mark by regular expression,
70 and filter displayed buffers by various criteria."
71 :version "22.1"
72 :group 'convenience)
74 (defcustom ibuffer-formats '((mark modified read-only locked
75 " " (name 18 18 :left :elide)
76 " " (size 9 -1 :right)
77 " " (mode 16 16 :left :elide) " " filename-and-process)
78 (mark " " (name 16 -1) " " filename))
79 "A list of ways to display buffer lines.
81 With Ibuffer, you are not limited to displaying just certain
82 attributes of a buffer such as size, name, and mode in a particular
83 order. Through this variable, you can completely customize and
84 control the appearance of an Ibuffer buffer. See also
85 `define-ibuffer-column', which allows you to define your own columns
86 for display.
88 This variable has the form
89 ((COLUMN COLUMN ...) (COLUMN COLUMN ...) ...)
90 Each element in `ibuffer-formats' should be a list containing COLUMN
91 specifiers. A COLUMN can be any of the following:
93 SYMBOL - A symbol naming the column. Predefined columns are:
94 mark modified read-only name size mode process filename
95 When you define your own columns using `define-ibuffer-column', just
96 use their name like the predefined columns here. This entry can
97 also be a function of two arguments, which should return a string.
98 The first argument is the buffer object, and the second is the mark
99 on that buffer.
101 \"STRING\" - A literal string to display.
103 (SYMBOL MIN-SIZE MAX-SIZE &optional ALIGN ELIDE) - SYMBOL is a
104 symbol naming the column, and MIN-SIZE and MAX-SIZE are integers (or
105 functions of no arguments returning an integer) which constrict the
106 size of a column. If MAX-SIZE is -1, there is no upper bound. The
107 default values are 0 and -1, respectively. If MIN-SIZE is negative,
108 use the end of the string. The optional element ALIGN describes the
109 alignment of the column; it can be :left, :center or :right. The
110 optional element ELIDE describes whether or not to elide the column
111 if it is too long; valid values are :elide and nil. The default is
112 nil (don't elide).
114 Some example of valid entries in `ibuffer-formats', with
115 description (also, feel free to try them out, and experiment with your
116 own!):
118 (mark \" \" name)
119 This format just displays the current mark (if any) and the name of
120 the buffer, separated by a space.
121 (mark modified read-only \" \" (name 16 16 :left) \" \" (size 6 -1 :right))
122 This format displays the current mark (if any), its modification and
123 read-only status, as well as the name of the buffer and its size. In
124 this format, the name is restricted to 16 characters (longer names
125 will be truncated, and shorter names will be padded with spaces), and
126 the name is also aligned to the left. The size of the buffer will
127 be padded with spaces up to a minimum of six characters, but there is
128 no upper limit on its size. The size will also be aligned to the
129 right.
131 Thus, if you wanted to use these two formats, the appropriate
132 value for this variable would be
134 \\='((mark \" \" name)
135 (mark modified read-only
136 (name 16 16 :left)
137 (size 6 -1 :right)))
139 Using \\[ibuffer-switch-format], you can rotate the display between
140 the specified formats in the list."
141 :version "25.2"
142 :type '(repeat sexp)
143 :group 'ibuffer)
145 (defcustom ibuffer-always-compile-formats (featurep 'bytecomp)
146 "If non-nil, then use the byte-compiler to optimize `ibuffer-formats'.
147 This will increase the redisplay speed, at the cost of loading the
148 elisp byte-compiler."
149 :type 'boolean
150 :group 'ibuffer)
152 (defcustom ibuffer-fontification-alist
153 `((10 buffer-read-only font-lock-constant-face)
154 (15 (and buffer-file-name
155 (string-match ibuffer-compressed-file-name-regexp
156 buffer-file-name))
157 font-lock-doc-face)
158 (20 (string-match "^*" (buffer-name)) font-lock-keyword-face)
159 (25 (and (string-match "^ " (buffer-name))
160 (null buffer-file-name))
161 italic)
162 (30 (memq major-mode ibuffer-help-buffer-modes) font-lock-comment-face)
163 (35 (derived-mode-p 'dired-mode) font-lock-function-name-face)
164 (40 (and (boundp 'emacs-lock-mode) emacs-lock-mode) ibuffer-locked-buffer))
165 "An alist describing how to fontify buffers.
166 Each element should be of the form (PRIORITY FORM FACE), where
167 PRIORITY is an integer, FORM is an arbitrary form to evaluate in the
168 buffer, and FACE is the face to use for fontification. If the FORM
169 evaluates to non-nil, then FACE will be put on the buffer name. The
170 element with the highest PRIORITY takes precedence.
172 If you change this variable, you must kill the Ibuffer buffer and
173 recreate it for the change to take effect."
174 :type '(repeat
175 (list (integer :tag "Priority")
176 (sexp :tag "Test Form")
177 face))
178 :group 'ibuffer)
180 (defcustom ibuffer-use-other-window nil
181 "If non-nil, display Ibuffer in another window by default."
182 :type 'boolean
183 :group 'ibuffer)
185 (defcustom ibuffer-default-shrink-to-minimum-size nil
186 "If non-nil, minimize the size of the Ibuffer window by default."
187 :type 'boolean
188 :group 'ibuffer)
189 (defvar ibuffer-shrink-to-minimum-size nil)
191 (defcustom ibuffer-display-summary t
192 "If non-nil, summarize Ibuffer columns."
193 :type 'boolean
194 :group 'ibuffer)
196 (defcustom ibuffer-truncate-lines t
197 "If non-nil, do not display continuation lines."
198 :type 'boolean
199 :group 'ibuffer)
201 (defcustom ibuffer-case-fold-search case-fold-search
202 "If non-nil, ignore case when searching."
203 :type 'boolean
204 :group 'ibuffer)
206 (defcustom ibuffer-default-sorting-mode 'recency
207 "The criteria by which to sort the buffers.
209 Note that this variable is local to each Ibuffer buffer. Thus, you
210 can have multiple Ibuffer buffers open, each with a different sorted
211 view of the buffers."
212 :type '(choice (const :tag "Last view time" :value recency)
213 (const :tag "Lexicographic" :value alphabetic)
214 (const :tag "Buffer size" :value size)
215 (const :tag "File name" :value filename/process)
216 (const :tag "Major mode" :value major-mode))
217 :group 'ibuffer)
218 (defvar ibuffer-sorting-mode nil)
219 (defvar ibuffer-last-sorting-mode nil)
221 (defcustom ibuffer-default-sorting-reversep nil
222 "If non-nil, reverse the default sorting order."
223 :type 'boolean
224 :group 'ibuffer)
225 (defvar ibuffer-sorting-reversep nil)
227 (defcustom ibuffer-elide-long-columns nil
228 "If non-nil, then elide column entries which exceed their max length."
229 :type 'boolean
230 :group 'ibuffer)
231 (make-obsolete-variable 'ibuffer-elide-long-columns
232 "use the :elide argument of `ibuffer-formats'."
233 "22.1")
235 (defcustom ibuffer-eliding-string "..."
236 "The string to use for eliding long columns."
237 :type 'string
238 :group 'ibuffer)
240 (defcustom ibuffer-maybe-show-predicates `(,(lambda (buf)
241 (and (string-match "^ " (buffer-name buf))
242 (null buffer-file-name))))
243 "A list of predicates for buffers to display conditionally.
245 A predicate can be a regexp or a function.
246 If a regexp, then it will be matched against the buffer's name.
247 If a function, it will be called with the buffer as an argument, and
248 should return non-nil if this buffer should be shown.
250 Viewing of buffers hidden because of these predicates may be customized
251 via `ibuffer-default-display-maybe-show-predicates' and is toggled by
252 giving a non-nil prefix argument to `ibuffer-update'.
253 Note that this specialized filtering occurs before real filtering."
254 :type '(repeat (choice regexp function))
255 :group 'ibuffer)
257 (defcustom ibuffer-default-display-maybe-show-predicates nil
258 "Non-nil means show buffers that match `ibuffer-maybe-show-predicates'."
259 :type 'boolean
260 :group 'ibuffer)
262 (defvar ibuffer-display-maybe-show-predicates nil)
264 (defvar ibuffer-current-format nil)
266 (defcustom ibuffer-movement-cycle t
267 "If non-nil, then forward and backwards movement commands cycle."
268 :type 'boolean
269 :group 'ibuffer)
271 (defcustom ibuffer-modified-char ?*
272 "The character to display for modified buffers."
273 :type 'character
274 :group 'ibuffer)
276 (defcustom ibuffer-read-only-char ?%
277 "The character to display for read-only buffers."
278 :type 'character
279 :group 'ibuffer)
281 (defcustom ibuffer-marked-char ?>
282 "The character to display for marked buffers."
283 :type 'character
284 :group 'ibuffer)
286 (defcustom ibuffer-locked-char ?L
287 "The character to display for locked buffers."
288 :version "25.2"
289 :type 'character
290 :group 'ibuffer)
292 (defcustom ibuffer-deletion-char ?D
293 "The character to display for buffers marked for deletion."
294 :type 'character
295 :group 'ibuffer)
297 (defcustom ibuffer-expert nil
298 "If non-nil, don't ask for confirmation of \"dangerous\" operations."
299 :type 'boolean
300 :group 'ibuffer)
302 (defcustom ibuffer-view-ibuffer nil
303 "If non-nil, display the current Ibuffer buffer itself.
304 Note that this has a drawback - the data about the current Ibuffer
305 buffer will most likely be inaccurate. This includes modification
306 state, size, etc."
307 :type 'boolean
308 :group 'ibuffer)
310 (defcustom ibuffer-always-show-last-buffer nil
311 "If non-nil, always display the previous buffer.
312 This variable takes precedence over filtering, and even
313 `ibuffer-never-show-predicates'."
314 :type '(choice (const :tag "Always" :value t)
315 (const :tag "Never" :value nil)
316 (const :tag "Always except minibuffer" :value :nomini))
317 :group 'ibuffer)
319 (defcustom ibuffer-jump-offer-only-visible-buffers nil
320 "If non-nil, only offer buffers visible in the Ibuffer buffer
321 in completion lists of the `ibuffer-jump-to-buffer' command."
322 :type 'boolean
323 :group 'ibuffer)
325 (defcustom ibuffer-use-header-line (boundp 'header-line-format)
326 "If non-nil, display a header line containing current filters."
327 :type 'boolean
328 :group 'ibuffer)
330 (defcustom ibuffer-default-directory nil
331 "The default directory to use for a new Ibuffer buffer.
332 If nil, inherit the directory of the buffer in which `ibuffer' was
333 called. Otherwise, this variable should be a string naming a
334 directory, like `default-directory'."
335 :type '(choice (const :tag "Inherit" :value nil)
336 string)
337 :group 'ibuffer)
339 (defcustom ibuffer-help-buffer-modes
340 '(help-mode apropos-mode Info-mode Info-edit-mode)
341 "List of \"Help\" major modes."
342 :type '(repeat function)
343 :group 'ibuffer)
345 (defcustom ibuffer-compressed-file-name-regexp
346 "\\.\\(arj\\|bgz\\|bz2\\|gz\\|lzh\\|taz\\|tgz\\|xz\\|zip\\|z\\)$"
347 "Regexp to match compressed file names."
348 :version "24.1" ; added xz
349 :type 'regexp
350 :group 'ibuffer)
352 (define-obsolete-variable-alias 'ibuffer-hooks 'ibuffer-hook "22.1")
354 (defcustom ibuffer-hook nil
355 "Hook run when `ibuffer' is called."
356 :type 'hook
357 :group 'ibuffer)
359 (define-obsolete-variable-alias 'ibuffer-mode-hooks 'ibuffer-mode-hook "22.1")
361 (defcustom ibuffer-mode-hook nil
362 "Hook run upon entry into `ibuffer-mode'."
363 :type 'hook
364 :options '(ibuffer-auto-mode)
365 :group 'ibuffer)
367 (defcustom ibuffer-load-hook nil
368 "Hook run when Ibuffer is loaded."
369 :type 'hook
370 :group 'ibuffer)
372 (defcustom ibuffer-marked-face 'warning
373 "Face used for displaying marked buffers."
374 :type 'face
375 :group 'ibuffer)
377 (defcustom ibuffer-deletion-face 'error
378 "Face used for displaying buffers marked for deletion."
379 :type 'face
380 :group 'ibuffer)
382 (defcustom ibuffer-title-face 'font-lock-type-face
383 "Face used for the title string."
384 :type 'face
385 :group 'ibuffer)
387 (defcustom ibuffer-filter-group-name-face 'bold
388 "Face used for displaying filtering group names."
389 :type 'face
390 :group 'ibuffer)
392 (defcustom ibuffer-directory-abbrev-alist nil
393 "An alist of file name abbreviations like `directory-abbrev-alist'."
394 :type '(repeat (cons :format "%v"
395 :value ("" . "")
396 (regexp :tag "From")
397 (regexp :tag "To")))
398 :group 'ibuffer)
400 (defvar ibuffer-mode-groups-popup
401 (let ((groups-map (make-sparse-keymap "Filter Groups")))
402 ;; Filter groups
404 (define-key-after groups-map [filters-to-filter-group]
405 '(menu-item "Create filter group from current filters..."
406 ibuffer-filters-to-filter-group
407 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)))
408 (define-key-after groups-map [forward-filter-group]
409 '(menu-item "Move point to the next filter group"
410 ibuffer-forward-filter-group))
411 (define-key-after groups-map [backward-filter-group]
412 '(menu-item "Move point to the previous filter group"
413 ibuffer-backward-filter-group))
414 (define-key-after groups-map [jump-to-filter-group]
415 '(menu-item "Move point to a specific filter group..."
416 ibuffer-jump-to-filter-group))
417 (define-key-after groups-map [kill-filter-group]
418 '(menu-item "Kill filter group named..."
419 ibuffer-kill-filter-group
420 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)))
421 (define-key-after groups-map [yank-filter-group]
422 '(menu-item "Yank last killed filter group before..."
423 ibuffer-yank-filter-group
424 :enable (and (featurep 'ibuf-ext) ibuffer-filter-group-kill-ring)))
425 (define-key-after groups-map [pop-filter-group]
426 '(menu-item "Remove top filter group"
427 ibuffer-pop-filter-group
428 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)))
429 (define-key-after groups-map [clear-filter-groups]
430 '(menu-item "Remove all filter groups"
431 ibuffer-clear-filter-groups
432 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)))
433 (define-key-after groups-map [pop-filter-group]
434 '(menu-item "Decompose filter group..."
435 ibuffer-pop-filter-group
436 :help "\"Unmake\" a filter group"
437 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)))
438 (define-key-after groups-map [save-filter-groups]
439 '(menu-item "Save current filter groups permanently..."
440 ibuffer-save-filter-groups
441 :enable (and (featurep 'ibuf-ext) ibuffer-filter-groups)
442 :help "Use a mnemonic name to store current filter groups"))
443 (define-key-after groups-map [switch-to-saved-filter-groups]
444 '(menu-item "Restore permanently saved filters..."
445 ibuffer-switch-to-saved-filter-groups
446 :enable (and (featurep 'ibuf-ext) ibuffer-saved-filter-groups)
447 :help "Replace current filters with a saved stack"))
448 (define-key-after groups-map [delete-saved-filter-groups]
449 '(menu-item "Delete permanently saved filter groups..."
450 ibuffer-delete-saved-filter-groups
451 :enable (and (featurep 'ibuf-ext) ibuffer-saved-filter-groups)))
452 (define-key-after groups-map [set-filter-groups-by-mode]
453 '(menu-item "Set current filter groups to filter by mode"
454 ibuffer-set-filter-groups-by-mode))
456 groups-map))
458 (defvar ibuffer-mode-map
459 (let ((map (make-keymap)))
460 (define-key map (kbd "0") 'digit-argument)
461 (define-key map (kbd "1") 'digit-argument)
462 (define-key map (kbd "2") 'digit-argument)
463 (define-key map (kbd "3") 'digit-argument)
464 (define-key map (kbd "4") 'digit-argument)
465 (define-key map (kbd "5") 'digit-argument)
466 (define-key map (kbd "6") 'digit-argument)
467 (define-key map (kbd "7") 'digit-argument)
468 (define-key map (kbd "8") 'digit-argument)
469 (define-key map (kbd "9") 'digit-argument)
471 (define-key map (kbd "m") 'ibuffer-mark-forward)
472 (define-key map (kbd "t") 'ibuffer-toggle-marks)
473 (define-key map (kbd "u") 'ibuffer-unmark-forward)
474 (define-key map (kbd "=") 'ibuffer-diff-with-file)
475 (define-key map (kbd "j") 'ibuffer-jump-to-buffer)
476 (define-key map (kbd "M-g") 'ibuffer-jump-to-buffer)
477 (define-key map (kbd "M-s a C-s") 'ibuffer-do-isearch)
478 (define-key map (kbd "M-s a M-C-s") 'ibuffer-do-isearch-regexp)
479 (define-key map (kbd "M-s a C-o") 'ibuffer-do-occur)
480 (define-key map (kbd "DEL") 'ibuffer-unmark-backward)
481 (define-key map (kbd "M-DEL") 'ibuffer-unmark-all)
482 (define-key map (kbd "* *") 'ibuffer-unmark-all)
483 (define-key map (kbd "* c") 'ibuffer-change-marks)
484 (define-key map (kbd "U") 'ibuffer-unmark-all-marks)
485 (define-key map (kbd "* M") 'ibuffer-mark-by-mode)
486 (define-key map (kbd "* m") 'ibuffer-mark-modified-buffers)
487 (define-key map (kbd "* u") 'ibuffer-mark-unsaved-buffers)
488 (define-key map (kbd "* s") 'ibuffer-mark-special-buffers)
489 (define-key map (kbd "* r") 'ibuffer-mark-read-only-buffers)
490 (define-key map (kbd "* /") 'ibuffer-mark-dired-buffers)
491 (define-key map (kbd "* e") 'ibuffer-mark-dissociated-buffers)
492 (define-key map (kbd "* h") 'ibuffer-mark-help-buffers)
493 (define-key map (kbd "* z") 'ibuffer-mark-compressed-file-buffers)
494 (define-key map (kbd ".") 'ibuffer-mark-old-buffers)
496 (define-key map (kbd "d") 'ibuffer-mark-for-delete)
497 (define-key map (kbd "C-d") 'ibuffer-mark-for-delete-backwards)
498 (define-key map (kbd "k") 'ibuffer-mark-for-delete)
499 (define-key map (kbd "x") 'ibuffer-do-kill-on-deletion-marks)
501 ;; immediate operations
502 (define-key map (kbd "n") 'ibuffer-forward-line)
503 (define-key map (kbd "SPC") 'forward-line)
504 (define-key map (kbd "p") 'ibuffer-backward-line)
505 (define-key map (kbd "M-}") 'ibuffer-forward-next-marked)
506 (define-key map (kbd "M-{") 'ibuffer-backwards-next-marked)
507 (define-key map (kbd "l") 'ibuffer-redisplay)
508 (define-key map (kbd "g") 'ibuffer-update)
509 (define-key map "`" 'ibuffer-switch-format)
510 (define-key map "-" 'ibuffer-add-to-tmp-hide)
511 (define-key map "+" 'ibuffer-add-to-tmp-show)
512 (define-key map "b" 'ibuffer-bury-buffer)
513 (define-key map (kbd ",") 'ibuffer-toggle-sorting-mode)
514 (define-key map (kbd "s i") 'ibuffer-invert-sorting)
515 (define-key map (kbd "s a") 'ibuffer-do-sort-by-alphabetic)
516 (define-key map (kbd "s v") 'ibuffer-do-sort-by-recency)
517 (define-key map (kbd "s s") 'ibuffer-do-sort-by-size)
518 (define-key map (kbd "s f") 'ibuffer-do-sort-by-filename/process)
519 (define-key map (kbd "s m") 'ibuffer-do-sort-by-major-mode)
521 (define-key map (kbd "/ m") 'ibuffer-filter-by-used-mode)
522 (define-key map (kbd "/ M") 'ibuffer-filter-by-derived-mode)
523 (define-key map (kbd "/ n") 'ibuffer-filter-by-name)
524 (define-key map (kbd "/ c") 'ibuffer-filter-by-content)
525 (define-key map (kbd "/ e") 'ibuffer-filter-by-predicate)
526 (define-key map (kbd "/ f") 'ibuffer-filter-by-filename)
527 (define-key map (kbd "/ >") 'ibuffer-filter-by-size-gt)
528 (define-key map (kbd "/ <") 'ibuffer-filter-by-size-lt)
529 (define-key map (kbd "/ r") 'ibuffer-switch-to-saved-filters)
530 (define-key map (kbd "/ a") 'ibuffer-add-saved-filters)
531 (define-key map (kbd "/ x") 'ibuffer-delete-saved-filters)
532 (define-key map (kbd "/ d") 'ibuffer-decompose-filter)
533 (define-key map (kbd "/ s") 'ibuffer-save-filters)
534 (define-key map (kbd "/ p") 'ibuffer-pop-filter)
535 (define-key map (kbd "/ !") 'ibuffer-negate-filter)
536 (define-key map (kbd "/ t") 'ibuffer-exchange-filters)
537 (define-key map (kbd "/ TAB") 'ibuffer-exchange-filters)
538 (define-key map (kbd "/ o") 'ibuffer-or-filter)
539 (define-key map (kbd "/ g") 'ibuffer-filters-to-filter-group)
540 (define-key map (kbd "/ P") 'ibuffer-pop-filter-group)
541 (define-key map (kbd "/ D") 'ibuffer-decompose-filter-group)
542 (define-key map (kbd "/ /") 'ibuffer-filter-disable)
544 (define-key map (kbd "M-n") 'ibuffer-forward-filter-group)
545 (define-key map "\t" 'ibuffer-forward-filter-group)
546 (define-key map (kbd "M-p") 'ibuffer-backward-filter-group)
547 (define-key map [backtab] 'ibuffer-backward-filter-group)
548 (define-key map (kbd "M-j") 'ibuffer-jump-to-filter-group)
549 (define-key map (kbd "C-k") 'ibuffer-kill-line)
550 (define-key map (kbd "C-y") 'ibuffer-yank)
551 (define-key map (kbd "/ S") 'ibuffer-save-filter-groups)
552 (define-key map (kbd "/ R") 'ibuffer-switch-to-saved-filter-groups)
553 (define-key map (kbd "/ X") 'ibuffer-delete-saved-filter-groups)
554 (define-key map (kbd "/ \\") 'ibuffer-clear-filter-groups)
556 (define-key map (kbd "% n") 'ibuffer-mark-by-name-regexp)
557 (define-key map (kbd "% m") 'ibuffer-mark-by-mode-regexp)
558 (define-key map (kbd "% f") 'ibuffer-mark-by-file-name-regexp)
559 (define-key map (kbd "% g") 'ibuffer-mark-by-content-regexp)
560 (define-key map (kbd "% L") 'ibuffer-mark-by-locked)
562 (define-key map (kbd "C-t") 'ibuffer-visit-tags-table)
564 (define-key map (kbd "|") 'ibuffer-do-shell-command-pipe)
565 (define-key map (kbd "!") 'ibuffer-do-shell-command-file)
566 (define-key map (kbd "~") 'ibuffer-do-toggle-modified)
567 ;; marked operations
568 (define-key map (kbd "A") 'ibuffer-do-view)
569 (define-key map (kbd "D") 'ibuffer-do-delete)
570 (define-key map (kbd "E") 'ibuffer-do-eval)
571 (define-key map (kbd "F") 'ibuffer-do-shell-command-file)
572 (define-key map (kbd "I") 'ibuffer-do-query-replace-regexp)
573 (define-key map (kbd "H") 'ibuffer-do-view-other-frame)
574 (define-key map (kbd "N") 'ibuffer-do-shell-command-pipe-replace)
575 (define-key map (kbd "M") 'ibuffer-do-toggle-modified)
576 (define-key map (kbd "O") 'ibuffer-do-occur)
577 (define-key map (kbd "P") 'ibuffer-do-print)
578 (define-key map (kbd "Q") 'ibuffer-do-query-replace)
579 (define-key map (kbd "R") 'ibuffer-do-rename-uniquely)
580 (define-key map (kbd "S") 'ibuffer-do-save)
581 (define-key map (kbd "T") 'ibuffer-do-toggle-read-only)
582 (define-key map (kbd "r") 'ibuffer-do-replace-regexp)
583 (define-key map (kbd "V") 'ibuffer-do-revert)
584 (define-key map (kbd "W") 'ibuffer-do-view-and-eval)
585 (define-key map (kbd "X") 'ibuffer-do-shell-command-pipe)
587 (define-key map (kbd "k") 'ibuffer-do-kill-lines)
588 (define-key map (kbd "w") 'ibuffer-copy-filename-as-kill)
589 (define-key map (kbd "B") 'ibuffer-copy-buffername-as-kill)
591 (define-key map (kbd "RET") 'ibuffer-visit-buffer)
592 (define-key map (kbd "e") 'ibuffer-visit-buffer)
593 (define-key map (kbd "f") 'ibuffer-visit-buffer)
594 (define-key map (kbd "C-x C-f") 'ibuffer-find-file)
595 (define-key map (kbd "o") 'ibuffer-visit-buffer-other-window)
596 (define-key map (kbd "C-o") 'ibuffer-visit-buffer-other-window-noselect)
597 (define-key map (kbd "M-o") 'ibuffer-visit-buffer-1-window)
598 (define-key map (kbd "v") 'ibuffer-do-view)
599 (define-key map (kbd "C-x v") 'ibuffer-do-view-horizontally)
600 (define-key map (kbd "C-c C-a") 'ibuffer-auto-mode)
601 (define-key map (kbd "C-x 4 RET") 'ibuffer-visit-buffer-other-window)
602 (define-key map (kbd "C-x 5 RET") 'ibuffer-visit-buffer-other-frame)
604 (define-key map [menu-bar view]
605 (cons "View" (make-sparse-keymap "View")))
607 (define-key-after map [menu-bar view visit-buffer]
608 '(menu-item "View this buffer" ibuffer-visit-buffer))
609 (define-key-after map [menu-bar view visit-buffer-other-window]
610 '(menu-item "View (other window)" ibuffer-visit-buffer-other-window))
611 (define-key-after map [menu-bar view visit-buffer-other-frame]
612 '(menu-item "View (other frame)" ibuffer-visit-buffer-other-frame))
613 (define-key-after map [menu-bar view ibuffer-update]
614 '(menu-item "Update" ibuffer-update
615 :help "Regenerate the list of buffers"))
616 (define-key-after map [menu-bar view switch-format]
617 '(menu-item "Switch display format" ibuffer-switch-format
618 :help "Toggle between available values of `ibuffer-formats'"))
620 (define-key-after map [menu-bar view dashes]
621 '("--"))
623 (define-key-after map [menu-bar view sort]
624 (cons "Sort" (make-sparse-keymap "Sort")))
626 (define-key-after map [menu-bar view sort do-sort-by-major-mode]
627 '(menu-item "Sort by major mode" ibuffer-do-sort-by-major-mode))
628 (define-key-after map [menu-bar view sort do-sort-by-size]
629 '(menu-item "Sort by buffer size" ibuffer-do-sort-by-size))
630 (define-key-after map [menu-bar view sort do-sort-by-alphabetic]
631 '(menu-item "Sort lexicographically" ibuffer-do-sort-by-alphabetic
632 :help "Sort by the alphabetic order of buffer name"))
633 (define-key-after map [menu-bar view sort do-sort-by-recency]
634 '(menu-item "Sort by view time" ibuffer-do-sort-by-recency
635 :help "Sort by the last time the buffer was displayed"))
636 (define-key-after map [menu-bar view sort dashes]
637 '("--"))
638 (define-key-after map [menu-bar view sort invert-sorting]
639 '(menu-item "Reverse sorting order" ibuffer-invert-sorting))
640 (define-key-after map [menu-bar view sort toggle-sorting-mode]
641 '(menu-item "Switch sorting mode" ibuffer-toggle-sorting-mode
642 :help "Switch between the various sorting criteria"))
644 (define-key-after map [menu-bar view filter]
645 (cons "Filter" (make-sparse-keymap "Filter")))
647 (define-key-after map [menu-bar view filter filter-disable]
648 '(menu-item "Disable all filtering" ibuffer-filter-disable
649 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)))
650 (define-key-after map [menu-bar view filter filter-by-mode]
651 '(menu-item "Add filter by any major mode..." ibuffer-filter-by-mode))
652 (define-key-after map [menu-bar view filter filter-by-used-mode]
653 '(menu-item "Add filter by a major mode in use..."
654 ibuffer-filter-by-used-mode))
655 (define-key-after map [menu-bar view filter filter-by-derived-mode]
656 '(menu-item "Add filter by derived mode..."
657 ibuffer-filter-by-derived-mode))
658 (define-key-after map [menu-bar view filter filter-by-name]
659 '(menu-item "Add filter by buffer name..." ibuffer-filter-by-name))
660 (define-key-after map [menu-bar view filter filter-by-filename]
661 '(menu-item "Add filter by filename..." ibuffer-filter-by-filename))
662 (define-key-after map [menu-bar view filter filter-by-size-lt]
663 '(menu-item "Add filter by size less than..." ibuffer-filter-by-size-lt))
664 (define-key-after map [menu-bar view filter filter-by-size-gt]
665 '(menu-item "Add filter by size greater than..."
666 ibuffer-filter-by-size-gt))
667 (define-key-after map [menu-bar view filter filter-by-content]
668 '(menu-item "Add filter by content (regexp)..."
669 ibuffer-filter-by-content))
670 (define-key-after map [menu-bar view filter filter-by-predicate]
671 '(menu-item "Add filter by Lisp predicate..."
672 ibuffer-filter-by-predicate))
673 (define-key-after map [menu-bar view filter pop-filter]
674 '(menu-item "Remove top filter" ibuffer-pop-filter
675 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)))
676 (define-key-after map [menu-bar view filter or-filter]
677 '(menu-item "OR top two filters" ibuffer-or-filter
678 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers
679 (cdr ibuffer-filtering-qualifiers))
680 :help
681 "Create a new filter which is the logical OR of the top two filters"))
682 (define-key-after map [menu-bar view filter negate-filter]
683 '(menu-item "Negate top filter" ibuffer-negate-filter
684 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)))
685 (define-key-after map [menu-bar view filter decompose-filter]
686 '(menu-item "Decompose top filter" ibuffer-decompose-filter
687 :enable (and (featurep 'ibuf-ext)
688 (memq (car ibuffer-filtering-qualifiers) '(or saved not)))
689 :help "Break down a complex filter like OR or NOT"))
690 (define-key-after map [menu-bar view filter exchange-filters]
691 '(menu-item "Swap top two filters" ibuffer-exchange-filters
692 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers
693 (cdr ibuffer-filtering-qualifiers))))
694 (define-key-after map [menu-bar view filter save-filters]
695 '(menu-item "Save current filters permanently..." ibuffer-save-filters
696 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)
697 :help "Use a mnemonic name to store current filter stack"))
698 (define-key-after map [menu-bar view filter switch-to-saved-filters]
699 '(menu-item "Restore permanently saved filters..."
700 ibuffer-switch-to-saved-filters
701 :enable (and (featurep 'ibuf-ext) ibuffer-saved-filters)
702 :help "Replace current filters with a saved stack"))
703 (define-key-after map [menu-bar view filter add-saved-filters]
704 '(menu-item "Add to permanently saved filters..."
705 ibuffer-add-saved-filters
706 :enable (and (featurep 'ibuf-ext) ibuffer-filtering-qualifiers)
707 :help "Include already saved stack with current filters"))
708 (define-key-after map [menu-bar view filter delete-saved-filters]
709 '(menu-item "Delete permanently saved filters..."
710 ibuffer-delete-saved-filters
711 :enable (and (featurep 'ibuf-ext) ibuffer-saved-filters)))
713 (define-key-after map [menu-bar view filter-groups]
714 (cons "Filter Groups" ibuffer-mode-groups-popup))
716 (define-key-after map [menu-bar view dashes2]
717 '("--"))
718 (define-key-after map [menu-bar view auto-mode]
719 '(menu-item "Auto Mode" ibuffer-auto-mode
720 :button (:toggle . ibuffer-auto-mode)
721 :help "Attempt to automatically update the Ibuffer buffer"))
723 (define-key-after map [menu-bar mark]
724 (cons "Mark" (make-sparse-keymap "Mark")))
726 (define-key-after map [menu-bar mark toggle-marks]
727 '(menu-item "Toggle marks" ibuffer-toggle-marks
728 :help "Unmark marked buffers, and mark unmarked buffers"))
729 (define-key-after map [menu-bar mark change-marks]
730 '(menu-item "Change marks" ibuffer-change-marks
731 :help "Change OLD mark for marked buffers with NEW"))
732 (define-key-after map [menu-bar mark mark-forward]
733 '(menu-item "Mark" ibuffer-mark-forward
734 :help "Mark the buffer at point"))
735 (define-key-after map [menu-bar mark unmark-forward]
736 '(menu-item "Unmark" ibuffer-unmark-forward
737 :help "Unmark the buffer at point"))
738 (define-key-after map [menu-bar mark mark-by-mode]
739 '(menu-item "Mark by mode..." ibuffer-mark-by-mode
740 :help "Mark all buffers in a particular major mode"))
741 (define-key-after map [menu-bar mark mark-modified-buffers]
742 '(menu-item "Mark modified buffers" ibuffer-mark-modified-buffers
743 :help "Mark all buffers which have been modified"))
744 (define-key-after map [menu-bar mark mark-unsaved-buffers]
745 '(menu-item "Mark unsaved buffers" ibuffer-mark-unsaved-buffers
746 :help "Mark all buffers which have a file and are modified"))
747 (define-key-after map [menu-bar mark mark-read-only-buffers]
748 '(menu-item "Mark read-only buffers" ibuffer-mark-read-only-buffers
749 :help "Mark all buffers which are read-only"))
750 (define-key-after map [menu-bar mark mark-special-buffers]
751 '(menu-item "Mark special buffers" ibuffer-mark-special-buffers
752 :help "Mark all buffers whose name begins with a *"))
753 (define-key-after map [menu-bar mark mark-dired-buffers]
754 '(menu-item "Mark dired buffers" ibuffer-mark-dired-buffers
755 :help "Mark buffers in dired-mode"))
756 (define-key-after map [menu-bar mark mark-dissociated-buffers]
757 '(menu-item "Mark dissociated buffers" ibuffer-mark-dissociated-buffers
758 :help "Mark buffers with a non-existent associated file"))
759 (define-key-after map [menu-bar mark mark-help-buffers]
760 '(menu-item "Mark help buffers" ibuffer-mark-help-buffers
761 :help "Mark buffers in help-mode"))
762 (define-key-after map [menu-bar mark mark-compressed-file-buffers]
763 '(menu-item "Mark compressed file buffers"
764 ibuffer-mark-compressed-file-buffers
765 :help "Mark buffers which have a file that is compressed"))
766 (define-key-after map [menu-bar mark mark-old-buffers]
767 '(menu-item "Mark old buffers" ibuffer-mark-old-buffers
768 :help "Mark buffers which have not been viewed recently"))
769 (define-key-after map [menu-bar mark unmark-all]
770 '(menu-item "Unmark All" ibuffer-unmark-all))
771 (define-key-after map [menu-bar mark unmark-all-marks]
772 '(menu-item "Unmark All buffers" ibuffer-unmark-all-marks))
774 (define-key-after map [menu-bar mark dashes]
775 '("--"))
777 (define-key-after map [menu-bar mark mark-by-name-regexp]
778 '(menu-item "Mark by buffer name (regexp)..." ibuffer-mark-by-name-regexp
779 :help "Mark buffers whose name matches a regexp"))
780 (define-key-after map [menu-bar mark mark-by-mode-regexp]
781 '(menu-item "Mark by major mode (regexp)..." ibuffer-mark-by-mode-regexp
782 :help "Mark buffers whose major mode name matches a regexp"))
783 (define-key-after map [menu-bar mark mark-by-file-name-regexp]
784 '(menu-item "Mark by file name (regexp)..."
785 ibuffer-mark-by-file-name-regexp
786 :help "Mark buffers whose file name matches a regexp"))
787 (define-key-after map [menu-bar mark ibuffer-mark-by-content-regexp]
788 '(menu-item "Mark by content (regexp)..."
789 ibuffer-mark-by-content-regexp
790 :help "Mark buffers whose content matches a regexp"))
791 (define-key-after map [menu-bar mark mark-by-locked]
792 '(menu-item "Mark by locked buffers..." ibuffer-mark-by-locked
793 :help "Mark all locked buffers"))
795 map))
797 (defvar ibuffer-mode-operate-map
798 (let ((operate-map (make-sparse-keymap "Operate")))
799 (define-key-after operate-map [do-view]
800 '(menu-item "View" ibuffer-do-view))
801 (define-key-after operate-map [do-view-other-frame]
802 '(menu-item "View (separate frame)" ibuffer-do-view-other-frame))
803 (define-key-after operate-map [do-save]
804 '(menu-item "Save" ibuffer-do-save))
805 (define-key-after operate-map [do-replace-regexp]
806 '(menu-item "Replace (regexp)..." ibuffer-do-replace-regexp
807 :help "Replace text inside marked buffers"))
808 (define-key-after operate-map [do-query-replace]
809 '(menu-item "Query Replace..." ibuffer-do-query-replace
810 :help "Replace text in marked buffers, asking each time"))
811 (define-key-after operate-map [do-query-replace-regexp]
812 '(menu-item "Query Replace (regexp)..." ibuffer-do-query-replace-regexp
813 :help "Replace text in marked buffers by regexp, asking each time"))
814 (define-key-after operate-map [do-print]
815 '(menu-item "Print" ibuffer-do-print))
816 (define-key-after operate-map [do-toggle-modified]
817 '(menu-item "Toggle modification flag" ibuffer-do-toggle-modified))
818 (define-key-after operate-map [do-revert]
819 '(menu-item "Revert" ibuffer-do-revert
820 :help "Revert marked buffers to their associated file"))
821 (define-key-after operate-map [do-rename-uniquely]
822 '(menu-item "Rename Uniquely" ibuffer-do-rename-uniquely
823 :help "Rename marked buffers to a new, unique name"))
824 (define-key-after operate-map [do-delete]
825 '(menu-item "Kill" ibuffer-do-delete))
826 (define-key-after operate-map [do-occur]
827 '(menu-item "List lines matching..." ibuffer-do-occur
828 :help "View all lines in marked buffers matching a regexp"))
829 (define-key-after operate-map [do-shell-command-pipe]
830 '(menu-item "Pipe to shell command..." ibuffer-do-shell-command-pipe
831 :help "For each marked buffer, send its contents to a shell command"))
832 (define-key-after operate-map [do-shell-command-pipe-replace]
833 '(menu-item "Pipe to shell command (replace)..." ibuffer-do-shell-command-pipe-replace
834 :help "For each marked buffer, replace its contents with output of shell command"))
835 (define-key-after operate-map [do-shell-command-file]
836 '(menu-item "Shell command on buffer's file..." ibuffer-do-shell-command-file
837 :help "For each marked buffer, run a shell command with its file as argument"))
838 (define-key-after operate-map [do-eval]
839 '(menu-item "Eval..." ibuffer-do-eval
840 :help "Evaluate a Lisp form in each marked buffer"))
841 (define-key-after operate-map [do-view-and-eval]
842 '(menu-item "Eval (viewing buffer)..." ibuffer-do-view-and-eval
843 :help "Evaluate a Lisp form in each marked buffer while viewing it"))
844 (define-key-after operate-map [diff-with-file]
845 '(menu-item "Diff with file" ibuffer-diff-with-file
846 :help "View the differences between this buffer and its file"))
848 operate-map))
850 (define-key ibuffer-mode-groups-popup [kill-filter-group]
851 '(menu-item "Kill filter group"
852 ibuffer-kill-line
853 :enable (and (featurep 'ibuf-ext)
854 ibuffer-filter-groups)))
855 (define-key ibuffer-mode-groups-popup [yank-filter-group]
856 '(menu-item "Yank last killed filter group"
857 ibuffer-yank
858 :enable (and (featurep 'ibuf-ext)
859 ibuffer-filter-group-kill-ring)))
861 (defvar ibuffer-name-map
862 (let ((map (make-sparse-keymap)))
863 (define-key map [(mouse-1)] 'ibuffer-mouse-toggle-mark)
864 (define-key map [(mouse-2)] 'ibuffer-mouse-visit-buffer)
865 (define-key map [down-mouse-3] 'ibuffer-mouse-popup-menu)
866 map))
868 (defvar ibuffer-filename/process-header-map
869 (let ((map (make-sparse-keymap)))
870 (define-key map [(mouse-1)] 'ibuffer-do-sort-by-filename/process)
871 map))
873 (defvar ibuffer-mode-name-map
874 (let ((map (make-sparse-keymap)))
875 (define-key map [(mouse-2)] 'ibuffer-mouse-filter-by-mode)
876 (define-key map (kbd "RET") 'ibuffer-interactive-filter-by-mode)
877 map))
879 (defvar ibuffer-name-header-map
880 (let ((map (make-sparse-keymap)))
881 (define-key map [(mouse-1)] 'ibuffer-do-sort-by-alphabetic)
882 map))
884 (defvar ibuffer-size-header-map
885 (let ((map (make-sparse-keymap)))
886 (define-key map [(mouse-1)] 'ibuffer-do-sort-by-size)
887 map))
889 (defvar ibuffer-mode-header-map
890 (let ((map (make-sparse-keymap)))
891 (define-key map [(mouse-1)] 'ibuffer-do-sort-by-major-mode)
892 map))
894 (defvar ibuffer-mode-filter-group-map
895 (let ((map (make-sparse-keymap)))
896 (define-key map [(mouse-1)] 'ibuffer-mouse-toggle-mark)
897 (define-key map [(mouse-2)] 'ibuffer-mouse-toggle-filter-group)
898 (define-key map (kbd "RET") 'ibuffer-toggle-filter-group)
899 (define-key map [down-mouse-3] 'ibuffer-mouse-popup-menu)
900 map))
902 (defvar ibuffer-did-modification nil)
904 (defvar ibuffer-compiled-formats nil)
905 (defvar ibuffer-cached-formats nil)
906 (defvar ibuffer-cached-eliding-string nil)
907 (defvar ibuffer-cached-elide-long-columns 0)
909 (defvar ibuffer-sorting-functions-alist nil
910 "An alist of functions which describe how to sort buffers.
912 Note: You most likely do not want to modify this variable directly;
913 use `define-ibuffer-sorter' instead.
915 The alist elements are constructed like (NAME DESCRIPTION FUNCTION)
916 Where NAME is a symbol describing the sorting method, DESCRIPTION is a
917 short string which will be displayed in the minibuffer and menu, and
918 FUNCTION is a function of two arguments, which will be the buffers to
919 compare.")
921 ;;; Utility functions
922 (defun ibuffer-columnize-and-insert-list (list &optional pad-width)
923 "Insert LIST into the current buffer in as many columns as possible.
924 The maximum number of columns is determined by the current window
925 width and the longest string in LIST."
926 (unless pad-width
927 (setq pad-width 3))
928 (let ((width (window-width))
929 (max (+ (apply #'max (mapcar #'length list))
930 pad-width)))
931 (let ((columns (/ width max)))
932 (when (zerop columns)
933 (setq columns 1))
934 (while list
935 (dotimes (_ (1- columns))
936 (insert (concat (car list) (make-string (- max (length (car list)))
937 ?\s)))
938 (setq list (cdr list)))
939 (when (not (null list))
940 (insert (pop list)))
941 (insert "\n")))))
943 (defsubst ibuffer-current-mark ()
944 (cadr (get-text-property (line-beginning-position)
945 'ibuffer-properties)))
947 (defun ibuffer-mouse-toggle-mark (event)
948 "Toggle the marked status of the buffer chosen with the mouse."
949 (interactive "e")
950 (unwind-protect
951 (let ((pt (save-excursion
952 (mouse-set-point event)
953 (point))))
954 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
955 (ibuffer-toggle-marks it)
956 (goto-char pt)
957 (let ((mark (ibuffer-current-mark)))
958 (setq buffer-read-only nil)
959 (if (eq mark ibuffer-marked-char)
960 (ibuffer-set-mark ?\s)
961 (ibuffer-set-mark ibuffer-marked-char)))))
962 (setq buffer-read-only t)))
964 (defun ibuffer-find-file (file &optional wildcards)
965 "Like `find-file', but default to the directory of the buffer at point."
966 (interactive
967 (let ((default-directory (let ((buf (ibuffer-current-buffer)))
968 (if (buffer-live-p buf)
969 (with-current-buffer buf
970 default-directory)
971 default-directory))))
972 (list (read-file-name "Find file: " default-directory)
973 t)))
974 (find-file file wildcards))
976 (defun ibuffer-mouse-visit-buffer (event)
977 "Visit the buffer chosen with the mouse."
978 (interactive "e")
979 (switch-to-buffer
980 (save-excursion
981 (mouse-set-point event)
982 (ibuffer-current-buffer t))))
984 (defun ibuffer-mouse-popup-menu (event)
985 "Display a menu of operations."
986 (interactive "e")
987 (let ((eventpt (posn-point (event-end event)))
988 (origpt (point)))
989 (unwind-protect
990 (if (get-text-property eventpt 'ibuffer-filter-group-name)
991 (progn
992 (goto-char eventpt)
993 (popup-menu ibuffer-mode-groups-popup))
994 (let ((inhibit-read-only t))
995 (ibuffer-save-marks
996 (ibuffer-unmark-all-marks)
997 (save-excursion
998 (goto-char eventpt)
999 (ibuffer-set-mark ibuffer-marked-char))
1000 (save-excursion
1001 (popup-menu ibuffer-mode-operate-map)))))
1002 (setq buffer-read-only t)
1003 (if (= eventpt (point))
1004 (goto-char origpt)))))
1006 (defun ibuffer-skip-properties (props direction)
1007 (while (and (not (eobp))
1008 (let ((hit nil))
1009 (dolist (prop props hit)
1010 (when (get-text-property (point) prop)
1011 (setq hit t)))))
1012 (forward-line direction)
1013 (beginning-of-line)))
1015 (defun ibuffer-customize ()
1016 "Begin customizing Ibuffer interactively."
1017 (interactive)
1018 (customize-group 'ibuffer))
1020 (defun ibuffer-backward-line (&optional arg skip-group-names)
1021 "Move backwards ARG lines, wrapping around the list if necessary."
1022 (interactive "P")
1023 (or arg (setq arg 1))
1024 (beginning-of-line)
1025 (while (> arg 0)
1026 (forward-line -1)
1027 (when (and ibuffer-movement-cycle
1028 (or (get-text-property (point) 'ibuffer-title)
1029 (and skip-group-names
1030 (get-text-property (point)
1031 'ibuffer-filter-group-name))))
1032 (goto-char (point-max))
1033 (beginning-of-line))
1034 (ibuffer-skip-properties (append '(ibuffer-summary)
1035 (when skip-group-names
1036 '(ibuffer-filter-group-name)))
1038 ;; Handle the special case of no buffers.
1039 (when (get-text-property (point) 'ibuffer-title)
1040 (forward-line 1)
1041 (setq arg 1))
1042 (cl-decf arg)))
1044 (defun ibuffer-forward-line (&optional arg skip-group-names)
1045 "Move forward ARG lines, wrapping around the list if necessary."
1046 (interactive "P")
1047 (or arg (setq arg 1))
1048 (beginning-of-line)
1049 (when (and ibuffer-movement-cycle
1050 (or (eobp)
1051 (get-text-property (point) 'ibuffer-summary)))
1052 (goto-char (point-min)))
1053 (when (or (get-text-property (point) 'ibuffer-title)
1054 (and skip-group-names
1055 (get-text-property (point) 'ibuffer-filter-group-name)))
1056 (when (> arg 0)
1057 (cl-decf arg))
1058 (ibuffer-skip-properties (append '(ibuffer-title)
1059 (when skip-group-names
1060 '(ibuffer-filter-group-name)))
1062 (if (< arg 0)
1063 (ibuffer-backward-line (- arg))
1064 (while (> arg 0)
1065 (forward-line 1)
1066 (when (and ibuffer-movement-cycle
1067 (or (eobp)
1068 (get-text-property (point) 'ibuffer-summary)))
1069 (goto-char (point-min)))
1070 (cl-decf arg)
1071 (ibuffer-skip-properties (append '(ibuffer-title)
1072 (when skip-group-names
1073 '(ibuffer-filter-group-name)))
1074 1))))
1076 (defun ibuffer-visit-buffer (&optional single)
1077 "Visit the buffer on this line.
1078 If optional argument SINGLE is non-nil, then also ensure there is only
1079 one window."
1080 (interactive "P")
1081 (let ((buf (ibuffer-current-buffer t)))
1082 (switch-to-buffer buf)
1083 (when single
1084 (delete-other-windows))))
1086 (defun ibuffer-visit-buffer-other-window (&optional noselect)
1087 "Visit the buffer on this line in another window."
1088 (interactive)
1089 (let ((buf (ibuffer-current-buffer t)))
1090 (bury-buffer (current-buffer))
1091 (if noselect
1092 (let ((curwin (selected-window)))
1093 (pop-to-buffer buf)
1094 (select-window curwin))
1095 (switch-to-buffer-other-window buf))))
1097 (defun ibuffer-visit-buffer-other-window-noselect ()
1098 "Visit the buffer on this line in another window, but don't select it."
1099 (interactive)
1100 (ibuffer-visit-buffer-other-window t))
1102 (defun ibuffer-visit-buffer-other-frame ()
1103 "Visit the buffer on this line in another frame."
1104 (interactive)
1105 (let ((buf (ibuffer-current-buffer t)))
1106 (bury-buffer (current-buffer))
1107 (switch-to-buffer-other-frame buf)))
1109 (defun ibuffer-visit-buffer-1-window ()
1110 "Visit the buffer on this line, and delete other windows."
1111 (interactive)
1112 (ibuffer-visit-buffer t))
1114 (defun ibuffer-bury-buffer ()
1115 "Bury the buffer on this line."
1116 (interactive)
1117 (let ((buf (ibuffer-current-buffer t))
1118 (line (+ 1 (count-lines 1 (point)))))
1119 (bury-buffer buf)
1120 (ibuffer-update nil t)
1121 (goto-char (point-min))
1122 (forward-line (1- line))))
1124 (defun ibuffer-visit-tags-table ()
1125 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
1126 (interactive)
1127 (let ((file (buffer-file-name (ibuffer-current-buffer t))))
1128 (if file
1129 (visit-tags-table file)
1130 (error "Specified buffer has no file"))))
1132 (defun ibuffer-do-view (&optional other-frame)
1133 "View marked buffers, or the buffer on the current line.
1134 If optional argument OTHER-FRAME is non-nil, then display each
1135 marked buffer in a new frame. Otherwise, display each buffer as
1136 a new window in the current frame, splitting vertically."
1137 (interactive)
1138 (ibuffer-do-view-1 (if other-frame 'other-frame 'vertically)))
1140 (defun ibuffer-do-view-horizontally (&optional other-frame)
1141 "As `ibuffer-do-view', but split windows horizontally."
1142 (interactive)
1143 (ibuffer-do-view-1 (if other-frame 'other-frame 'horizontally)))
1145 (defun ibuffer-do-view-1 (type)
1146 (let ((marked-bufs (or (ibuffer-get-marked-buffers)
1147 (list (ibuffer-current-buffer t)))))
1148 (unless (and (eq type 'other-frame)
1149 (not ibuffer-expert)
1150 (> (length marked-bufs) 3)
1151 (not (y-or-n-p (format "Really create a new frame for %s buffers? "
1152 (length marked-bufs)))))
1153 (unless (eq type 'other-frame)
1154 (set-buffer-modified-p nil)
1155 (delete-other-windows)
1156 (switch-to-buffer (pop marked-bufs)))
1157 (let ((height (/ (1- (if (eq type 'horizontally) (frame-width)
1158 (frame-height)))
1159 (1+ (length marked-bufs)))))
1160 (mapcar (if (eq type 'other-frame)
1161 (lambda (buf)
1162 (let ((curframe (selected-frame)))
1163 (select-frame (make-frame))
1164 (switch-to-buffer buf)
1165 (select-frame curframe)))
1166 (lambda (buf)
1167 (split-window nil height (eq type 'horizontally))
1168 (other-window 1)
1169 (switch-to-buffer buf)))
1170 marked-bufs)))))
1172 (defun ibuffer-do-view-other-frame ()
1173 "View each of the marked buffers in a separate frame."
1174 (interactive)
1175 (ibuffer-do-view t))
1177 (defsubst ibuffer-map-marked-lines (func)
1178 (prog1 (ibuffer-map-on-mark ibuffer-marked-char func)
1179 (ibuffer-redisplay t)))
1181 (defun ibuffer-shrink-to-fit (&optional owin)
1182 ;; Make sure that redisplay is performed, otherwise there can be a
1183 ;; bad interaction with code in the window-scroll-functions hook
1184 (redisplay t)
1185 (fit-window-to-buffer nil (when owin (/ (frame-height)
1186 (length (window-list (selected-frame)))))))
1188 (defun ibuffer-confirm-operation-on (operation names)
1189 "Display a buffer asking whether to perform OPERATION on NAMES."
1190 (or ibuffer-expert
1191 (if (= (length names) 1)
1192 (y-or-n-p (format "Really %s buffer %s? " operation (car names)))
1193 (let ((buf (get-buffer-create "*Ibuffer confirmation*")))
1194 (with-current-buffer buf
1195 (setq buffer-read-only nil)
1196 (erase-buffer)
1197 (ibuffer-columnize-and-insert-list names)
1198 (goto-char (point-min))
1199 (setq buffer-read-only t))
1200 (let ((lastwin (car (last (window-list nil 'nomini)))))
1201 ;; Now attempt to display the buffer...
1202 (save-window-excursion
1203 (select-window lastwin)
1204 ;; The window might be too small to split; in that case,
1205 ;; try a few times to increase its size before giving up.
1206 (let ((attempts 0)
1207 (trying t))
1208 (while trying
1209 (condition-case err
1210 (progn
1211 (split-window)
1212 (setq trying nil))
1213 (error
1214 ;; Handle a failure
1215 (if (or (> (cl-incf attempts) 4)
1216 (and (stringp (cadr err))
1217 ;; This definitely falls in the
1218 ;; ghetto hack category...
1219 (not (string-match-p "too small" (cadr err)))))
1220 (signal (car err) (cdr err))
1221 (enlarge-window 3))))))
1222 (select-window (next-window))
1223 (switch-to-buffer buf)
1224 (unwind-protect
1225 (progn
1226 (fit-window-to-buffer)
1227 (y-or-n-p (format "Really %s %d buffers? "
1228 operation (length names))))
1229 (kill-buffer buf))))))))
1231 (defsubst ibuffer-map-lines-nomodify (function)
1232 "As `ibuffer-map-lines', but don't set the modification flag."
1233 (ibuffer-map-lines function t))
1235 (defun ibuffer-buffer-names-with-mark (mark)
1236 (let ((ibuffer-buffer-names-with-mark-result nil))
1237 (ibuffer-map-lines-nomodify
1238 (lambda (buf mk)
1239 (when (char-equal mark mk)
1240 (push (buffer-name buf)
1241 ibuffer-buffer-names-with-mark-result))))
1242 ibuffer-buffer-names-with-mark-result))
1244 (defsubst ibuffer-marked-buffer-names ()
1245 (ibuffer-buffer-names-with-mark ibuffer-marked-char))
1247 (defsubst ibuffer-deletion-marked-buffer-names ()
1248 (ibuffer-buffer-names-with-mark ibuffer-deletion-char))
1250 (defun ibuffer-count-marked-lines (&optional all)
1251 (if all
1252 (ibuffer-map-lines-nomodify
1253 (lambda (_buf mark)
1254 (not (char-equal mark ?\s))))
1255 (ibuffer-map-lines-nomodify
1256 (lambda (_buf mark)
1257 (char-equal mark ibuffer-marked-char)))))
1259 (defsubst ibuffer-count-deletion-lines ()
1260 (ibuffer-map-lines-nomodify
1261 (lambda (_buf mark)
1262 (char-equal mark ibuffer-deletion-char))))
1264 (defsubst ibuffer-map-deletion-lines (func)
1265 (ibuffer-map-on-mark ibuffer-deletion-char func))
1267 (defsubst ibuffer-assert-ibuffer-mode ()
1268 (cl-assert (derived-mode-p 'ibuffer-mode)))
1270 (defun ibuffer-buffer-file-name ()
1271 (or buffer-file-name
1272 (let ((dirname (or (and (boundp 'dired-directory)
1273 (if (stringp dired-directory)
1274 dired-directory
1275 (car dired-directory)))
1276 (bound-and-true-p list-buffers-directory))))
1277 (and dirname (expand-file-name dirname)))))
1279 (define-ibuffer-op ibuffer-do-save ()
1280 "Save marked buffers as with `save-buffer'."
1281 (:complex t
1282 :opstring "saved"
1283 :modifier-p :maybe)
1284 (when (buffer-modified-p buf)
1285 (if (not (with-current-buffer buf
1286 buffer-file-name))
1287 ;; handle the case where we're prompted
1288 ;; for a file name
1289 (save-window-excursion
1290 (switch-to-buffer buf)
1291 (save-buffer))
1292 (with-current-buffer buf
1293 (save-buffer))))
1296 (define-ibuffer-op ibuffer-do-toggle-modified ()
1297 "Toggle modification flag of marked buffers."
1298 (:opstring "(un)marked as modified"
1299 :modifier-p t)
1300 (set-buffer-modified-p (not (buffer-modified-p))))
1302 (define-ibuffer-op ibuffer-do-toggle-read-only (&optional arg)
1303 "Toggle read only status in marked buffers.
1304 If optional ARG is a non-negative integer, make buffers read only.
1305 If ARG is a negative integer or 0, make buffers writable.
1306 Otherwise, toggle read only status."
1307 (:opstring "toggled read only status in"
1308 :interactive "P"
1309 :modifier-p t)
1310 (read-only-mode (if (integerp arg) arg 'toggle)))
1312 (define-ibuffer-op ibuffer-do-delete ()
1313 "Kill marked buffers as with `kill-this-buffer'."
1314 (:opstring "killed"
1315 :active-opstring "kill"
1316 :dangerous t
1317 :complex t
1318 :modifier-p t)
1319 (if (kill-buffer buf)
1320 'kill
1321 nil))
1323 (define-ibuffer-op ibuffer-do-kill-on-deletion-marks ()
1324 "Kill buffers marked for deletion as with `kill-this-buffer'."
1325 (:opstring "killed"
1326 :active-opstring "kill"
1327 :dangerous t
1328 :complex t
1329 :mark :deletion
1330 :modifier-p t)
1331 (if (kill-buffer buf)
1332 'kill
1333 nil))
1335 (defun ibuffer-unmark-all (mark)
1336 "Unmark all buffers with mark MARK."
1337 (interactive "cRemove marks (RET means all):")
1338 (if (= (ibuffer-count-marked-lines t) 0)
1339 (message "No buffers marked; use 'm' to mark a buffer")
1340 (cond
1341 ((char-equal mark ibuffer-marked-char)
1342 (ibuffer-map-marked-lines
1343 (lambda (_buf _mark)
1344 (ibuffer-set-mark-1 ?\s)
1345 t)))
1346 ((char-equal mark ibuffer-deletion-char)
1347 (ibuffer-map-deletion-lines
1348 (lambda (_buf _mark)
1349 (ibuffer-set-mark-1 ?\s)
1350 t)))
1351 ((not (char-equal mark ?\r))
1352 (ibuffer-map-lines
1353 (lambda (_buf cmark)
1354 (when (char-equal cmark mark)
1355 (ibuffer-set-mark-1 ?\s))
1356 t)))
1358 (ibuffer-map-lines
1359 (lambda (_buf mark)
1360 (when (not (char-equal mark ?\s))
1361 (ibuffer-set-mark-1 ?\s))
1362 t)))))
1363 (ibuffer-redisplay t))
1365 (defun ibuffer-unmark-all-marks ()
1366 "Remove all marks from all marked buffers in Ibuffer."
1367 (interactive)
1368 ;; hm. we could probably do this in a better fashion
1369 (ibuffer-unmark-all ?\r))
1371 (defun ibuffer-toggle-marks (&optional group)
1372 "Toggle which buffers are marked.
1373 In other words, unmarked buffers become marked, and marked buffers
1374 become unmarked.
1375 If point is on a group name, then this function operates on that
1376 group."
1377 (interactive)
1378 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
1379 (setq group it))
1380 (let ((count
1381 (ibuffer-map-lines
1382 (lambda (_buf mark)
1383 (cond ((eq mark ibuffer-marked-char)
1384 (ibuffer-set-mark-1 ?\s)
1385 nil)
1386 ((eq mark ?\s)
1387 (ibuffer-set-mark-1 ibuffer-marked-char)
1390 nil)))
1391 nil group)))
1392 (message "%s buffers marked" count))
1393 (ibuffer-redisplay t))
1395 (defun ibuffer-change-marks (&optional old new)
1396 "Change all OLD marks to NEW marks.
1397 OLD and NEW are both characters used to mark buffers."
1398 (interactive
1399 (let* ((cursor-in-echo-area t)
1400 (old (progn (message "Change (old mark): ") (read-char)))
1401 (new (progn (message "Change %c marks to (new mark): " old)
1402 (read-char))))
1403 (list old new)))
1404 (if (or (eq old ?\r) (eq new ?\r))
1405 (ding)
1406 (let ((count
1407 (ibuffer-map-lines
1408 (lambda (_buf mark)
1409 (when (eq mark old)
1410 (ibuffer-set-mark new) t)))))
1411 (message "%s marks changed" count))))
1413 (defsubst ibuffer-get-region-and-prefix ()
1414 (let ((arg (prefix-numeric-value current-prefix-arg)))
1415 (if (use-region-p) (list (region-beginning) (region-end) arg)
1416 (list nil nil arg))))
1418 (defun ibuffer-mark-forward (start end arg)
1419 "Mark the buffers in the region, or ARG buffers.
1420 If point is on a group name, this function operates on that group."
1421 (interactive (ibuffer-get-region-and-prefix))
1422 (ibuffer-mark-region-or-n-with-char start end arg ibuffer-marked-char))
1424 (defun ibuffer-unmark-forward (start end arg)
1425 "Unmark the buffers in the region, or ARG buffers.
1426 If point is on a group name, this function operates on that group."
1427 (interactive (ibuffer-get-region-and-prefix))
1428 (ibuffer-mark-region-or-n-with-char start end arg ?\s))
1430 (defun ibuffer-unmark-backward (arg)
1431 "Unmark the ARG previous buffers.
1432 If point is on a group name, this function operates on that group."
1433 (interactive "p")
1434 (ibuffer-unmark-forward nil nil (- arg)))
1436 (defun ibuffer-mark-region-or-n-with-char (start end arg mark-char)
1437 (if (use-region-p)
1438 (let ((cur (point)) (line-count (count-lines start end)))
1439 (goto-char start)
1440 (ibuffer-mark-interactive line-count mark-char)
1441 (goto-char cur))
1442 (ibuffer-mark-interactive arg mark-char)))
1444 (defun ibuffer-mark-interactive (arg mark &optional movement)
1445 (ibuffer-assert-ibuffer-mode)
1446 (or arg (setq arg 1))
1447 ;; deprecated movement argument
1448 (when (and movement (< movement 0))
1449 (setq arg (- arg)))
1450 (ibuffer-forward-line 0)
1451 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group-name)
1452 (progn
1453 (require 'ibuf-ext)
1454 (ibuffer-mark-on-buffer #'identity mark it))
1455 (ibuffer-forward-line 0 t)
1456 (while (> arg 0)
1457 (ibuffer-set-mark mark)
1458 (ibuffer-forward-line 1 t)
1459 (setq arg (1- arg)))
1460 (while (< arg 0)
1461 (ibuffer-forward-line -1 t)
1462 (ibuffer-set-mark mark)
1463 (setq arg (1+ arg)))))
1465 (defun ibuffer-set-mark (mark)
1466 (ibuffer-assert-ibuffer-mode)
1467 (let ((inhibit-read-only t))
1468 (ibuffer-set-mark-1 mark)
1469 (setq ibuffer-did-modification t)
1470 (ibuffer-redisplay-current)
1471 (beginning-of-line)))
1473 (defun ibuffer-set-mark-1 (mark)
1474 (let ((beg (line-beginning-position))
1475 (end (line-end-position)))
1476 (put-text-property beg end 'ibuffer-properties
1477 (list (ibuffer-current-buffer)
1478 mark))))
1480 (defun ibuffer-mark-for-delete (start end arg)
1481 "Mark for deletion the buffers in the region, or ARG buffers.
1482 If point is on a group name, this function operates on that group."
1483 (interactive (ibuffer-get-region-and-prefix))
1484 (ibuffer-mark-region-or-n-with-char start end arg ibuffer-deletion-char))
1486 (defun ibuffer-mark-for-delete-backwards (arg)
1487 "Mark for deletion the ARG previous buffers.
1488 If point is on a group name, this function operates on that group."
1489 (interactive "p")
1490 (ibuffer-mark-interactive arg ibuffer-deletion-char -1))
1492 (defun ibuffer-current-buffer (&optional must-be-live)
1493 (let ((buf (car (get-text-property (line-beginning-position)
1494 'ibuffer-properties))))
1495 (when must-be-live
1496 (if (bufferp buf)
1497 (unless (buffer-live-p buf)
1498 (error "Buffer %s has been killed; %s" buf (substitute-command-keys "use `\\[ibuffer-update]' to update")))
1499 (error "No buffer on this line")))
1500 buf))
1502 (defun ibuffer-active-formats-name ()
1503 (if (boundp 'ibuffer-filter-format-alist)
1504 (let ((ret nil))
1505 (dolist (filter ibuffer-filtering-qualifiers ret)
1506 (let ((val (assq (car filter) ibuffer-filter-format-alist)))
1507 (when val
1508 (setq ret (car filter)))))
1509 (if ret
1511 :ibuffer-formats))
1512 :ibuffer-formats))
1514 (defun ibuffer-current-formats (uncompiledp)
1515 (let* ((name (ibuffer-active-formats-name)))
1516 (ibuffer-check-formats)
1517 (if (eq name :ibuffer-formats)
1518 (if uncompiledp
1519 ibuffer-formats
1520 ibuffer-compiled-formats)
1521 (cadr (assq name
1522 (if uncompiledp
1523 ibuffer-filter-format-alist
1524 ibuffer-compiled-filter-formats))))))
1526 (defun ibuffer-current-format (&optional uncompiledp)
1527 (or ibuffer-current-format
1528 (setq ibuffer-current-format 0))
1529 (nth ibuffer-current-format (ibuffer-current-formats uncompiledp)))
1531 (defun ibuffer-expand-format-entry (form)
1532 (if (or (consp form)
1533 (symbolp form))
1534 (let ((sym (intern (concat "ibuffer-make-column-"
1535 (symbol-name (if (consp form)
1536 (car form)
1537 form))))))
1538 (unless (or (fboundp sym)
1539 (assq sym ibuffer-inline-columns))
1540 (error "Unknown column %s in ibuffer-formats" form))
1541 (let (min max align elide)
1542 (if (consp form)
1543 (setq min (or (nth 1 form) 0)
1544 max (or (nth 2 form) -1)
1545 align (or (nth 3 form) :left)
1546 elide (or (nth 4 form) nil))
1547 (setq min 0
1548 max -1
1549 align :left
1550 elide nil))
1551 (list sym min max align elide)))
1552 form))
1554 (defun ibuffer-compile-make-eliding-form (strvar elide from-end-p)
1555 (let ((ellipsis (propertize ibuffer-eliding-string 'font-lock-face 'bold)))
1556 (if (or elide (with-no-warnings ibuffer-elide-long-columns))
1557 `(if (> strlen 5)
1558 ,(if from-end-p
1559 `(concat ,ellipsis
1560 (substring ,strvar
1561 (length ibuffer-eliding-string)))
1562 `(concat
1563 (substring ,strvar 0 (- strlen ,(length ellipsis)))
1564 ,ellipsis))
1565 ,strvar)
1566 strvar)))
1568 (defun ibuffer-compile-make-substring-form (strvar maxvar from-end-p)
1569 (if from-end-p
1570 `(substring str
1571 (- strlen ,maxvar))
1572 `(substring ,strvar 0 ,maxvar)))
1574 (defun ibuffer-compile-make-format-form (strvar widthform alignment)
1575 (let* ((left `(make-string tmp2 ?\s))
1576 (right `(make-string (- tmp1 tmp2) ?\s)))
1577 `(progn
1578 (setq tmp1 ,widthform
1579 tmp2 (/ tmp1 2))
1580 ,(pcase alignment
1581 (:right `(concat ,left ,right ,strvar))
1582 (:center `(concat ,left ,strvar ,right))
1583 (:left `(concat ,strvar ,left ,right))
1584 (_ (error "Invalid alignment %s" alignment))))))
1586 (defun ibuffer-compile-format (format)
1587 (let ((result nil)
1588 ;; We use these variables to keep track of which variables
1589 ;; inside the generated function we need to bind, since
1590 ;; binding variables in Emacs takes time.
1591 (vars-used ()))
1592 (dolist (form format)
1593 (push
1594 ;; Generate a form based on a particular format entry, like
1595 ;; " ", mark, or (mode 16 16 :right).
1596 (if (stringp form)
1597 ;; It's a string; all we need to do is insert it.
1598 `(insert ,form)
1599 (let* ((form (ibuffer-expand-format-entry form))
1600 (sym (nth 0 form))
1601 (min (nth 1 form))
1602 (max (nth 2 form))
1603 (align (nth 3 form))
1604 (elide (nth 4 form)))
1605 (let* ((from-end-p (when (cl-minusp min)
1606 (setq min (- min))
1608 (letbindings nil)
1609 (outforms nil)
1610 minform
1611 maxform
1612 min-used max-used strlen-used)
1613 (when (or (not (integerp min)) (>= min 0))
1614 ;; This is a complex case; they want it limited to a
1615 ;; minimum size.
1616 (setq min-used t)
1617 (setq strlen-used t)
1618 (setq vars-used '(str strlen tmp1 tmp2))
1619 ;; Generate code to limit the string to a minimum size.
1620 (setq minform `(progn
1621 (setq str
1622 ,(ibuffer-compile-make-format-form
1623 'str
1624 `(- ,(if (integerp min)
1626 'min)
1627 strlen)
1628 align)))))
1629 (when (or (not (integerp max)) (> max 0))
1630 (setq max-used t)
1631 (cl-pushnew 'str vars-used)
1632 ;; Generate code to limit the string to a maximum size.
1633 (setq maxform `(progn
1634 (setq str
1635 ,(ibuffer-compile-make-substring-form
1636 'str
1637 (if (integerp max)
1639 'max)
1640 from-end-p))
1641 (setq strlen (length str))
1642 (setq str
1643 ,(ibuffer-compile-make-eliding-form
1644 'str elide from-end-p)))))
1645 ;; Now, put these forms together with the rest of the code.
1646 (let ((callform
1647 ;; Is this an "inline" column? This means we have
1648 ;; to get the code from the
1649 ;; `ibuffer-inline-columns' alist and insert it
1650 ;; into our generated code. Otherwise, we just
1651 ;; generate a call to the column function.
1652 (ibuffer-aif (assq sym ibuffer-inline-columns)
1653 (nth 1 it)
1654 `(,sym buffer mark)))
1655 ;; You're not expected to understand this. Hell, I
1656 ;; don't even understand it, and I wrote it five
1657 ;; minutes ago.
1658 (insertgenfn
1659 (if (get sym 'ibuffer-column-summarizer)
1660 ;; I really, really wish Emacs Lisp had closures.
1661 ;; FIXME: Elisp does have them now.
1662 (lambda (arg sym)
1663 `(insert
1664 (let ((ret ,arg))
1665 (put ',sym 'ibuffer-column-summary
1666 (cons ret (get ',sym
1667 'ibuffer-column-summary)))
1668 ret)))
1669 (lambda (arg _sym)
1670 `(insert ,arg))))
1671 (mincompform `(< strlen ,(if (integerp min)
1673 'min)))
1674 (maxcompform `(> strlen ,(if (integerp max)
1676 'max))))
1677 (if (or min-used max-used)
1678 ;; The complex case, where we have to limit the
1679 ;; form to a maximum or minimum size.
1680 (progn
1681 (when (and min-used (not (integerp min)))
1682 (push `(min ,min) letbindings))
1683 (when (and max-used (not (integerp max)))
1684 (push `(max ,max) letbindings))
1685 (push
1686 (if (and min-used max-used)
1687 `(if ,mincompform
1688 ,minform
1689 (if ,maxcompform
1690 ,maxform))
1691 (if min-used
1692 `(when ,mincompform
1693 ,minform)
1694 `(when ,maxcompform
1695 ,maxform)))
1696 outforms)
1697 (push `(setq str ,callform
1698 ,@(when strlen-used
1699 `(strlen (length str))))
1700 outforms)
1701 (setq outforms
1702 (append outforms
1703 (list (funcall insertgenfn 'str sym)))))
1704 ;; The simple case; just insert the string.
1705 (push (funcall insertgenfn callform sym) outforms))
1706 ;; Finally, return a `let' form which binds the
1707 ;; variables in `letbindings', and contains all the
1708 ;; code in `outforms'.
1709 `(let ,letbindings
1710 ,@outforms)))))
1711 result))
1712 ;; We don't want to unconditionally load the byte-compiler.
1713 (funcall (if (or ibuffer-always-compile-formats
1714 (featurep 'bytecomp))
1715 #'byte-compile
1716 #'identity)
1717 ;; Here, we actually create a lambda form which
1718 ;; inserts all the generated forms for each entry
1719 ;; in the format string.
1720 `(lambda (buffer mark)
1721 (let ,vars-used
1722 ,@(nreverse result))))))
1724 (defun ibuffer-recompile-formats ()
1725 "Recompile `ibuffer-formats'."
1726 (interactive)
1727 (setq ibuffer-compiled-formats
1728 (mapcar #'ibuffer-compile-format ibuffer-formats))
1729 (when (boundp 'ibuffer-filter-format-alist)
1730 (setq ibuffer-compiled-filter-formats
1731 (mapcar (lambda (entry)
1732 (cons (car entry)
1733 (mapcar (lambda (formats)
1734 (mapcar #'ibuffer-compile-format formats))
1735 (cdr entry))))
1736 ibuffer-filter-format-alist))))
1738 (defun ibuffer-clear-summary-columns (format)
1739 (dolist (form format)
1740 (when (and (consp form)
1741 (get (car form) 'ibuffer-column-summarizer))
1742 (put (car form) 'ibuffer-column-summary nil))))
1744 (defun ibuffer-check-formats ()
1745 (when (null ibuffer-formats)
1746 (error "No formats!"))
1747 (let ((ext-loaded (featurep 'ibuf-ext)))
1748 (when (or (null ibuffer-compiled-formats)
1749 (null ibuffer-cached-formats)
1750 (not (eq ibuffer-cached-formats ibuffer-formats))
1751 (null ibuffer-cached-eliding-string)
1752 (not (equal ibuffer-cached-eliding-string ibuffer-eliding-string))
1753 (eql 0 ibuffer-cached-elide-long-columns)
1754 (not (eql ibuffer-cached-elide-long-columns
1755 (with-no-warnings ibuffer-elide-long-columns)))
1756 (and ext-loaded
1757 (not (eq ibuffer-cached-filter-formats
1758 ibuffer-filter-format-alist))
1759 (and ibuffer-filter-format-alist
1760 (null ibuffer-compiled-filter-formats))))
1761 (message "Formats have changed, recompiling...")
1762 (ibuffer-recompile-formats)
1763 (setq ibuffer-cached-formats ibuffer-formats
1764 ibuffer-cached-eliding-string ibuffer-eliding-string
1765 ibuffer-cached-elide-long-columns (with-no-warnings ibuffer-elide-long-columns))
1766 (when ext-loaded
1767 (setq ibuffer-cached-filter-formats ibuffer-filter-format-alist))
1768 (message "Formats have changed, recompiling...done"))))
1770 (defvar ibuffer-inline-columns nil)
1772 (defface ibuffer-locked-buffer
1773 '((((background dark)) (:foreground "RosyBrown"))
1774 (t (:foreground "brown4")))
1775 "*Face used for locked buffers in Ibuffer."
1776 :version "25.2"
1777 :group 'ibuffer
1778 :group 'font-lock-highlighting-faces)
1779 (defvar ibuffer-locked-buffer 'ibuffer-locked-buffer)
1781 (define-ibuffer-column mark (:name " " :inline t)
1782 (string mark))
1784 (define-ibuffer-column read-only (:name "R" :inline t)
1785 (if buffer-read-only
1786 (string ibuffer-read-only-char)
1787 " "))
1789 (define-ibuffer-column locked
1790 (:name "L" :inline t :props ('font-lock-face 'ibuffer-locked-buffer))
1791 (if (and (boundp 'emacs-lock-mode) emacs-lock-mode)
1792 (string ibuffer-locked-char)
1793 " "))
1795 (define-ibuffer-column modified (:name "M" :inline t)
1796 (if (buffer-modified-p)
1797 (string ibuffer-modified-char)
1798 " "))
1800 (define-ibuffer-column name
1801 (:inline t
1802 :header-mouse-map ibuffer-name-header-map
1803 :props
1804 ('mouse-face 'highlight 'keymap ibuffer-name-map
1805 'ibuffer-name-column t
1806 'help-echo '(if tooltip-mode
1807 "mouse-1: mark this buffer\nmouse-2: select this buffer\nmouse-3: operate on this buffer"
1808 "mouse-1: mark buffer mouse-2: select buffer mouse-3: operate"))
1809 :summarizer
1810 (lambda (strings)
1811 (let ((bufs (length strings)))
1812 (cond ((zerop bufs) "No buffers")
1813 ((= 1 bufs) "1 buffer")
1814 (t (format "%s buffers" bufs))))))
1815 (let ((string (propertize (buffer-name)
1816 'font-lock-face
1817 (ibuffer-buffer-name-face buffer mark))))
1818 (if (not (seq-position string ?\n))
1819 string
1820 (replace-regexp-in-string
1821 "\n" (propertize "^J" 'font-lock-face 'escape-glyph) string))))
1823 (define-ibuffer-column size
1824 (:inline t
1825 :header-mouse-map ibuffer-size-header-map
1826 :summarizer
1827 (lambda (column-strings)
1828 (let ((total 0))
1829 (dolist (string column-strings)
1830 (setq total
1831 ;; like, ewww ...
1832 (+ (float (string-to-number string))
1833 total)))
1834 (format "%.0f" total))))
1835 (format "%s" (buffer-size)))
1837 (define-ibuffer-column mode
1838 (:inline t
1839 :header-mouse-map ibuffer-mode-header-map
1840 :props
1841 ('mouse-face 'highlight
1842 'keymap ibuffer-mode-name-map
1843 'help-echo "mouse-2: filter by this mode"))
1844 (format-mode-line mode-name nil nil (current-buffer)))
1846 (define-ibuffer-column process
1847 (:summarizer
1848 (lambda (strings)
1849 (let ((total (length (delete "" strings))))
1850 (cond ((zerop total) "No processes")
1851 ((= 1 total) "1 process")
1852 (t (format "%d processes" total))))))
1853 (ibuffer-aif (get-buffer-process buffer)
1854 (format "(%s %s)" it (process-status it))
1855 ""))
1857 (define-ibuffer-column filename
1858 (:summarizer
1859 (lambda (strings)
1860 (let ((total (length (delete "" strings))))
1861 (cond ((zerop total) "No files")
1862 ((= 1 total) "1 file")
1863 (t (format "%d files" total))))))
1864 (let ((directory-abbrev-alist ibuffer-directory-abbrev-alist))
1865 (abbreviate-file-name
1866 (or (ibuffer-buffer-file-name) ""))))
1868 (define-ibuffer-column filename-and-process
1869 (:name "Filename/Process"
1870 :header-mouse-map ibuffer-filename/process-header-map
1871 :summarizer
1872 (lambda (strings)
1873 (setq strings (delete "" strings))
1874 (let ((procs 0)
1875 (files 0))
1876 (dolist (string strings)
1877 (if (string-match "\\(?:\\`([[:ascii:]]+)\\)" string)
1878 (progn (setq procs (1+ procs))
1879 (if (< (match-end 0) (length string))
1880 (setq files (1+ files))))
1881 (setq files (1+ files))))
1882 (concat (cond ((zerop files) "No files")
1883 ((= 1 files) "1 file")
1884 (t (format "%d files" files)))
1885 ", "
1886 (cond ((zerop procs) "no processes")
1887 ((= 1 procs) "1 process")
1888 (t (format "%d processes" procs)))))))
1889 (let ((proc (get-buffer-process buffer))
1890 (filename (ibuffer-make-column-filename buffer mark)))
1891 (if proc
1892 (concat (propertize (format "(%s %s)" proc (process-status proc))
1893 'font-lock-face 'italic)
1894 (if (> (length filename) 0)
1895 (format " %s" filename)
1896 ""))
1897 filename)))
1899 (defun ibuffer-format-column (str width alignment)
1900 (let ((left (make-string (/ width 2) ?\s))
1901 (right (make-string (- width (/ width 2)) ?\s)))
1902 (pcase alignment
1903 (:right (concat left right str))
1904 (:center (concat left str right))
1905 (_ (concat str left right)))))
1907 (defun ibuffer-buffer-name-face (buf mark)
1908 (cond ((char-equal mark ibuffer-marked-char)
1909 ibuffer-marked-face)
1910 ((char-equal mark ibuffer-deletion-char)
1911 ibuffer-deletion-face)
1913 (let ((level -1)
1914 result)
1915 (dolist (e ibuffer-fontification-alist result)
1916 (when (and (> (car e) level)
1917 (with-current-buffer buf
1918 (eval (nth 1 e))))
1919 (setq level (car e)
1920 result (nth 2 e))))))))
1922 (defun ibuffer-insert-buffer-line (buffer mark format)
1923 "Insert a line describing BUFFER and MARK using FORMAT."
1924 (ibuffer-assert-ibuffer-mode)
1925 (let ((beg (point)))
1926 (funcall format buffer mark)
1927 (put-text-property beg (point) 'ibuffer-properties (list buffer mark)))
1928 (insert "\n"))
1930 ;; This function knows a bit too much of the internals. It would be
1931 ;; nice if it was all abstracted away.
1932 (defun ibuffer-redisplay-current ()
1933 (ibuffer-assert-ibuffer-mode)
1934 (when (eobp)
1935 (forward-line -1))
1936 (beginning-of-line)
1937 (let ((curformat (mapcar #'ibuffer-expand-format-entry
1938 (ibuffer-current-format t))))
1939 (ibuffer-clear-summary-columns curformat)
1940 (let ((buf (ibuffer-current-buffer)))
1941 (when buf
1942 (let ((mark (ibuffer-current-mark)))
1943 (save-excursion
1944 (delete-region (point) (1+ (line-end-position)))
1945 (ibuffer-insert-buffer-line
1946 buf mark
1947 (ibuffer-current-format)))
1948 (when ibuffer-shrink-to-minimum-size
1949 (ibuffer-shrink-to-fit)))))))
1951 (defun ibuffer-map-on-mark (mark func)
1952 (ibuffer-map-lines
1953 (lambda (buf mk)
1954 (if (char-equal mark mk)
1955 (funcall func buf mark)
1956 nil))))
1958 (defun ibuffer-map-lines (function &optional nomodify group)
1959 "Call FUNCTION for each buffer.
1960 Set the ibuffer modification flag unless NOMODIFY is non-nil.
1962 If optional argument GROUP is non-nil, then only call FUNCTION on
1963 buffers in filtering group GROUP.
1965 FUNCTION is called with two arguments:
1966 the buffer object itself and the current mark symbol."
1967 (ibuffer-assert-ibuffer-mode)
1968 (ibuffer-forward-line 0)
1969 (let* ((orig-target-line (1+ (count-lines (save-excursion
1970 (goto-char (point-min))
1971 (ibuffer-forward-line 0)
1972 (point))
1973 (point))))
1974 (target-line-offset orig-target-line)
1975 (ibuffer-map-lines-total 0)
1976 (ibuffer-map-lines-count 0))
1977 (unwind-protect
1978 (progn
1979 (setq buffer-read-only nil)
1980 (goto-char (point-min))
1981 (ibuffer-forward-line 0 t)
1982 (while (and (not (eobp))
1983 (not (get-text-property (point) 'ibuffer-summary))
1984 (progn
1985 (ibuffer-forward-line 0 t)
1986 (and (not (eobp))
1987 (not (get-text-property (point) 'ibuffer-summary)))))
1988 (let ((result
1989 (if (buffer-live-p (ibuffer-current-buffer))
1990 (when (or (null group)
1991 (ibuffer-aif (get-text-property (point) 'ibuffer-filter-group)
1992 (equal group it)))
1993 (save-excursion
1994 (funcall function
1995 (ibuffer-current-buffer)
1996 (ibuffer-current-mark))))
1997 ;; Kill the line if the buffer is dead
1998 'kill)))
1999 ;; A given mapping function should return:
2000 ;; nil if it chose not to affect the buffer
2001 ;; `kill' means the remove line from the buffer list
2002 ;; t otherwise
2003 (cl-incf ibuffer-map-lines-total)
2004 (cond ((null result)
2005 (forward-line 1))
2006 ((eq result 'kill)
2007 (delete-region (line-beginning-position)
2008 (1+ (line-end-position)))
2009 (cl-incf ibuffer-map-lines-count)
2010 (when (< ibuffer-map-lines-total
2011 orig-target-line)
2012 (cl-decf target-line-offset)))
2014 (cl-incf ibuffer-map-lines-count)
2015 (forward-line 1)))))
2016 ibuffer-map-lines-count)
2017 (progn
2018 (setq buffer-read-only t)
2019 (unless nomodify
2020 (set-buffer-modified-p nil))
2021 (goto-char (point-min))
2022 (ibuffer-forward-line 0)
2023 (ibuffer-forward-line (1- target-line-offset))))))
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 (char-equal c ?\s)
2172 (char-equal 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