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