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