* epa.texi (Caching Passphrases): New section.
[emacs.git] / lisp / buff-menu.el
blobe0f00d3553dd9e4244aef07ef99816941aa3ebd1
1 ;;; buff-menu.el --- buffer menu main function and support functions -*- coding:utf-8 -*-
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: convenience
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Edit, delete, or change attributes of all currently active Emacs
28 ;; buffers from a list summarizing their state. A good way to browse
29 ;; any special or scratch buffers you have loaded, since you can't find
30 ;; them by filename. The single entry point is `list-buffers',
31 ;; normally bound to C-x C-b.
33 ;;; Change Log:
35 ;; Buffer-menu-view: New function
36 ;; Buffer-menu-view-other-window: New function
38 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
40 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
42 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
43 ;; current entry and then move to previous one.
45 ;; Based on FSF code dating back to 1985.
47 ;;; Code:
49 ;;Trying to preserve the old window configuration works well in
50 ;;simple scenarios, when you enter the buffer menu, use it, and exit it.
51 ;;But it does strange things when you switch back to the buffer list buffer
52 ;;with C-x b, later on, when the window configuration is different.
53 ;;The choice seems to be, either restore the window configuration
54 ;;in all cases, or in no cases.
55 ;;I decided it was better not to restore the window config at all. -- rms.
57 ;;But since then, I changed buffer-menu to use the selected window,
58 ;;so q now once again goes back to the previous window configuration.
60 ;;(defvar Buffer-menu-window-config nil
61 ;; "Window configuration saved from entry to `buffer-menu'.")
63 ;; Put buffer *Buffer List* into proper mode right away
64 ;; so that from now on even list-buffers is enough to get a buffer menu.
66 (defgroup Buffer-menu nil
67 "Show a menu of all buffers in a buffer."
68 :group 'tools
69 :group 'convenience)
71 (defcustom Buffer-menu-use-header-line t
72 "Non-nil means to use an immovable header-line."
73 :type 'boolean
74 :group 'Buffer-menu)
76 (defface buffer-menu-buffer
77 '((t (:weight bold)))
78 "Face used to highlight buffer names in the buffer menu."
79 :group 'Buffer-menu)
80 (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer)
82 (defcustom Buffer-menu-buffer+size-width 26
83 "How wide to jointly make the buffer name and size columns."
84 :type 'number
85 :group 'Buffer-menu)
87 (defcustom Buffer-menu-mode-width 16
88 "How wide to make the mode name column."
89 :type 'number
90 :group 'Buffer-menu)
92 (defcustom Buffer-menu-use-frame-buffer-list t
93 "If non-nil, the Buffer Menu uses the selected frame's buffer list.
94 Buffers that were never selected in that frame are listed at the end.
95 If the value is nil, the Buffer Menu uses the global buffer list.
96 This variable matters if the Buffer Menu is sorted by visited order,
97 as it is by default."
98 :type 'boolean
99 :group 'Buffer-menu
100 :version "22.1")
102 ;; This should get updated & resorted when you click on a column heading
103 (defvar Buffer-menu-sort-column nil
104 "Which column to sort the menu on.
105 Use 2 to sort by buffer names, or 5 to sort by file names.
106 A nil value means sort by visited order (the default).")
108 (defconst Buffer-menu-buffer-column 4)
110 (defvar Buffer-menu-files-only nil
111 "Non-nil if the current buffer-menu lists only file buffers.
112 This variable determines whether reverting the buffer lists only
113 file buffers. It affects both manual reverting and reverting by
114 Auto Revert Mode.")
116 (make-variable-buffer-local 'Buffer-menu-files-only)
118 (defvar Info-current-file) ;; from info.el
119 (defvar Info-current-node) ;; from info.el
121 (defvar Buffer-menu-mode-map
122 (let ((map (make-keymap))
123 (menu-map (make-sparse-keymap)))
124 (suppress-keymap map t)
125 (define-key map "v" 'Buffer-menu-select)
126 (define-key map "2" 'Buffer-menu-2-window)
127 (define-key map "1" 'Buffer-menu-1-window)
128 (define-key map "f" 'Buffer-menu-this-window)
129 (define-key map "e" 'Buffer-menu-this-window)
130 (define-key map "\C-m" 'Buffer-menu-this-window)
131 (define-key map "o" 'Buffer-menu-other-window)
132 (define-key map "\C-o" 'Buffer-menu-switch-other-window)
133 (define-key map "s" 'Buffer-menu-save)
134 (define-key map "d" 'Buffer-menu-delete)
135 (define-key map "k" 'Buffer-menu-delete)
136 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
137 (define-key map "\C-k" 'Buffer-menu-delete)
138 (define-key map "x" 'Buffer-menu-execute)
139 (define-key map " " 'next-line)
140 (define-key map "n" 'next-line)
141 (define-key map "p" 'previous-line)
142 (define-key map "\177" 'Buffer-menu-backup-unmark)
143 (define-key map "~" 'Buffer-menu-not-modified)
144 (define-key map "u" 'Buffer-menu-unmark)
145 (define-key map "m" 'Buffer-menu-mark)
146 (define-key map "t" 'Buffer-menu-visit-tags-table)
147 (define-key map "%" 'Buffer-menu-toggle-read-only)
148 (define-key map "b" 'Buffer-menu-bury)
149 (define-key map "V" 'Buffer-menu-view)
150 (define-key map "T" 'Buffer-menu-toggle-files-only)
151 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
152 (define-key map [follow-link] 'mouse-face)
153 (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers)
154 (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp)
155 (define-key map [menu-bar Buffer-menu-mode] (cons (purecopy "Buffer-Menu") menu-map))
156 (define-key menu-map [quit]
157 `(menu-item ,(purecopy "Quit") quit-window
158 :help ,(purecopy "Remove the buffer menu from the display")))
159 (define-key menu-map [rev]
160 `(menu-item ,(purecopy "Refresh") revert-buffer
161 :help ,(purecopy "Refresh the *Buffer List* buffer contents")))
162 (define-key menu-map [s0] menu-bar-separator)
163 (define-key menu-map [tf]
164 `(menu-item ,(purecopy "Show only file buffers") Buffer-menu-toggle-files-only
165 :button (:toggle . Buffer-menu-files-only)
166 :help ,(purecopy "Toggle whether the current buffer-menu displays only file buffers")))
167 (define-key menu-map [s1] menu-bar-separator)
168 ;; FIXME: The "Select" entries could use better names...
169 (define-key menu-map [sel]
170 `(menu-item ,(purecopy "Select marked") Buffer-menu-select
171 :help ,(purecopy "Select this line's buffer; also display buffers marked with `>'")))
172 (define-key menu-map [bm2]
173 `(menu-item ,(purecopy "Select two") Buffer-menu-2-window
174 :help ,(purecopy "Select this line's buffer, with previous buffer in second window")))
175 (define-key menu-map [bm1]
176 `(menu-item ,(purecopy "Select current") Buffer-menu-1-window
177 :help ,(purecopy "Select this line's buffer, alone, in full frame")))
178 (define-key menu-map [ow]
179 `(menu-item ,(purecopy "Select in other window") Buffer-menu-other-window
180 :help ,(purecopy "Select this line's buffer in other window, leaving buffer menu visible")))
181 (define-key menu-map [tw]
182 `(menu-item ,(purecopy "Select in current window") Buffer-menu-this-window
183 :help ,(purecopy "Select this line's buffer in this window")))
184 (define-key menu-map [s2] menu-bar-separator)
185 (define-key menu-map [is]
186 `(menu-item ,(purecopy "Regexp Isearch marked buffers") Buffer-menu-isearch-buffers-regexp
187 :help ,(purecopy "Search for a regexp through all marked buffers using Isearch")))
188 (define-key menu-map [ir]
189 `(menu-item ,(purecopy "Isearch marked buffers") Buffer-menu-isearch-buffers
190 :help ,(purecopy "Search for a string through all marked buffers using Isearch")))
191 (define-key menu-map [s3] menu-bar-separator)
192 (define-key menu-map [by]
193 `(menu-item ,(purecopy "Bury") Buffer-menu-bury
194 :help ,(purecopy "Bury the buffer listed on this line")))
195 (define-key menu-map [vt]
196 `(menu-item ,(purecopy "Set unmodified") Buffer-menu-not-modified
197 :help ,(purecopy "Mark buffer on this line as unmodified (no changes to save)")))
198 (define-key menu-map [ex]
199 `(menu-item ,(purecopy "Execute") Buffer-menu-execute
200 :help ,(purecopy "Save and/or delete buffers marked with s or k commands")))
201 (define-key menu-map [s4] menu-bar-separator)
202 (define-key menu-map [delb]
203 `(menu-item ,(purecopy "Mark for delete and move backwards") Buffer-menu-delete-backwards
204 :help ,(purecopy "Mark buffer on this line to be deleted by x command and move up one line")))
205 (define-key menu-map [del]
206 `(menu-item ,(purecopy "Mark for delete") Buffer-menu-delete
207 :help ,(purecopy "Mark buffer on this line to be deleted by x command")))
209 (define-key menu-map [sv]
210 `(menu-item ,(purecopy "Mark for save") Buffer-menu-save
211 :help ,(purecopy "Mark buffer on this line to be saved by x command")))
212 (define-key menu-map [umk]
213 `(menu-item ,(purecopy "Unmark") Buffer-menu-unmark
214 :help ,(purecopy "Cancel all requested operations on buffer on this line and move down")))
215 (define-key menu-map [mk]
216 `(menu-item ,(purecopy "Mark") Buffer-menu-mark
217 :help ,(purecopy "Mark buffer on this line for being displayed by v command")))
218 map)
219 "Local keymap for `Buffer-menu-mode' buffers.")
221 ;; Buffer Menu mode is suitable only for specially formatted data.
222 (put 'Buffer-menu-mode 'mode-class 'special)
224 (define-derived-mode Buffer-menu-mode special-mode "Buffer Menu"
225 "Major mode for editing a list of buffers.
226 Each line describes one of the buffers in Emacs.
227 Letters do not insert themselves; instead, they are commands.
228 \\<Buffer-menu-mode-map>
229 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
230 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
231 \\[Buffer-menu-other-window] -- select that buffer in another window,
232 so the buffer menu buffer remains visible in its window.
233 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
234 \\[Buffer-menu-view-other-window] -- select that buffer in
235 another window, in view-mode.
236 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
237 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
238 \\[Buffer-menu-select] -- select current line's buffer.
239 Also show buffers marked with m, in other windows.
240 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
241 \\[Buffer-menu-2-window] -- select that buffer in one window,
242 together with buffer selected before this one in another window.
243 \\[Buffer-menu-isearch-buffers] -- Do incremental search in the marked buffers.
244 \\[Buffer-menu-isearch-buffers-regexp] -- Isearch for regexp in the marked buffers.
245 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
246 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
247 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
248 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
249 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
250 \\[Buffer-menu-execute] -- delete or save marked buffers.
251 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
252 With prefix argument, also move up one line.
253 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
254 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
255 \\[revert-buffer] -- update the list of buffers.
256 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
257 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
258 (set (make-local-variable 'revert-buffer-function)
259 'Buffer-menu-revert-function)
260 (set (make-local-variable 'buffer-stale-function)
261 #'(lambda (&optional noconfirm) 'fast))
262 (setq truncate-lines t)
263 (setq buffer-read-only t))
265 (define-obsolete-variable-alias 'buffer-menu-mode-hook
266 'Buffer-menu-mode-hook "23.1")
268 (defun Buffer-menu-revert-function (ignore1 ignore2)
269 (or (eq buffer-undo-list t)
270 (setq buffer-undo-list nil))
271 ;; We can not use save-excursion here. The buffer gets erased.
272 (let ((opoint (point))
273 (eobp (eobp))
274 (ocol (current-column))
275 (oline (progn (move-to-column 4)
276 (get-text-property (point) 'buffer)))
277 (prop (point-min))
278 ;; do not make undo records for the reversion.
279 (buffer-undo-list t))
280 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
281 ;; temporarily the current buffer. Make sure that the
282 ;; interactively current buffer is correctly identified with a `.'
283 ;; by `list-buffers-noselect'.
284 (with-current-buffer (window-buffer)
285 (list-buffers-noselect Buffer-menu-files-only))
286 (if oline
287 (while (setq prop (next-single-property-change prop 'buffer))
288 (when (eq (get-text-property prop 'buffer) oline)
289 (goto-char prop)
290 (move-to-column ocol)))
291 (goto-char (if eobp (point-max) opoint)))))
293 (defun Buffer-menu-toggle-files-only (arg)
294 "Toggle whether the current buffer-menu displays only file buffers.
295 With a positive ARG display only file buffers. With zero or
296 negative ARG, display other buffers as well."
297 (interactive "P")
298 (setq Buffer-menu-files-only
299 (cond ((not arg) (not Buffer-menu-files-only))
300 ((> (prefix-numeric-value arg) 0) t)))
301 (revert-buffer))
304 (defun Buffer-menu-buffer (error-if-non-existent-p)
305 "Return buffer described by this line of buffer menu."
306 (let* ((where (save-excursion
307 (beginning-of-line)
308 (+ (point) Buffer-menu-buffer-column)))
309 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
310 (buf (and (not (eobp)) (get-text-property where 'buffer))))
311 (if name
312 (or (get-buffer name)
313 (and buf (buffer-name buf) buf)
314 (if error-if-non-existent-p
315 (error "No buffer named `%s'" name)
316 nil))
317 (or (and buf (buffer-name buf) buf)
318 (if error-if-non-existent-p
319 (error "No buffer on this line")
320 nil)))))
322 (defun buffer-menu (&optional arg)
323 "Make a menu of buffers so you can save, delete or select them.
324 With argument, show only buffers that are visiting files.
325 Type ? after invocation to get help on commands available.
326 Type q to remove the buffer menu from the display.
328 The first column shows `>' for a buffer you have
329 marked to be displayed, `D' for one you have marked for
330 deletion, and `.' for the current buffer.
332 The C column has a `.' for the buffer from which you came.
333 The R column has a `%' if the buffer is read-only.
334 The M column has a `*' if it is modified,
335 or `S' if you have marked it for saving.
336 After this come the buffer name, its size in characters,
337 its major mode, and the visited file name (if any)."
338 (interactive "P")
339 ;;; (setq Buffer-menu-window-config (current-window-configuration))
340 (switch-to-buffer (list-buffers-noselect arg))
341 (message
342 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
344 (defun buffer-menu-other-window (&optional arg)
345 "Display a list of buffers in another window.
346 With the buffer list buffer, you can save, delete or select the buffers.
347 With argument, show only buffers that are visiting files.
348 Type ? after invocation to get help on commands available.
349 Type q to remove the buffer menu from the display.
350 For more information, see the function `buffer-menu'."
351 (interactive "P")
352 ;;; (setq Buffer-menu-window-config (current-window-configuration))
353 (switch-to-buffer-other-window (list-buffers-noselect arg))
354 (message
355 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
357 (defun Buffer-menu-no-header ()
358 (beginning-of-line)
359 (if (or Buffer-menu-use-header-line
360 (not (eq (char-after) ?C)))
362 (ding)
363 (forward-line 1)
364 nil))
366 (defun Buffer-menu-mark ()
367 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
368 (interactive)
369 (when (Buffer-menu-no-header)
370 (let ((inhibit-read-only t))
371 (delete-char 1)
372 (insert ?>)
373 (forward-line 1))))
375 (defun Buffer-menu-unmark (&optional backup)
376 "Cancel all requested operations on buffer on this line and move down.
377 Optional prefix arg means move up."
378 (interactive "P")
379 (when (Buffer-menu-no-header)
380 (let* ((buf (Buffer-menu-buffer t))
381 (mod (buffer-modified-p buf))
382 (readonly (with-current-buffer buf buffer-read-only))
383 (inhibit-read-only t))
384 (delete-char 3)
385 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
386 (forward-line (if backup -1 1)))
388 (defun Buffer-menu-backup-unmark ()
389 "Move up and cancel all requested operations on buffer on line above."
390 (interactive)
391 (forward-line -1)
392 (Buffer-menu-unmark)
393 (forward-line -1))
395 (defun Buffer-menu-delete (&optional arg)
396 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
397 Prefix arg is how many buffers to delete.
398 Negative arg means delete backwards."
399 (interactive "p")
400 (when (Buffer-menu-no-header)
401 (let ((inhibit-read-only t))
402 (if (or (null arg) (= arg 0))
403 (setq arg 1))
404 (while (> arg 0)
405 (delete-char 1)
406 (insert ?D)
407 (forward-line 1)
408 (setq arg (1- arg)))
409 (while (and (< arg 0)
410 (Buffer-menu-no-header))
411 (delete-char 1)
412 (insert ?D)
413 (forward-line -1)
414 (setq arg (1+ arg))))))
416 (defun Buffer-menu-delete-backwards (&optional arg)
417 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
418 and then move up one line. Prefix arg means move that many lines."
419 (interactive "p")
420 (Buffer-menu-delete (- (or arg 1))))
422 (defun Buffer-menu-save ()
423 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
424 (interactive)
425 (when (Buffer-menu-no-header)
426 (let ((inhibit-read-only t))
427 (forward-char 2)
428 (delete-char 1)
429 (insert ?S)
430 (forward-line 1))))
432 (defun Buffer-menu-not-modified (&optional arg)
433 "Mark buffer on this line as unmodified (no changes to save)."
434 (interactive "P")
435 (with-current-buffer (Buffer-menu-buffer t)
436 (set-buffer-modified-p arg))
437 (save-excursion
438 (beginning-of-line)
439 (forward-char 2)
440 (if (= (char-after) (if arg ?\s ?*))
441 (let ((inhibit-read-only t))
442 (delete-char 1)
443 (insert (if arg ?* ?\s))))))
445 (defun Buffer-menu-beginning ()
446 (goto-char (point-min))
447 (unless Buffer-menu-use-header-line
448 (forward-line)))
450 (defun Buffer-menu-execute ()
451 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
452 (interactive)
453 (save-excursion
454 (Buffer-menu-beginning)
455 (while (re-search-forward "^..S" nil t)
456 (let ((modp nil))
457 (with-current-buffer (Buffer-menu-buffer t)
458 (save-buffer)
459 (setq modp (buffer-modified-p)))
460 (let ((inhibit-read-only t))
461 (delete-char -1)
462 (insert (if modp ?* ?\s))))))
463 (save-excursion
464 (Buffer-menu-beginning)
465 (let ((buff-menu-buffer (current-buffer))
466 (inhibit-read-only t))
467 (while (re-search-forward "^D" nil t)
468 (forward-char -1)
469 (let ((buf (Buffer-menu-buffer nil)))
470 (or (eq buf nil)
471 (eq buf buff-menu-buffer)
472 (save-excursion (kill-buffer buf)))
473 (if (and buf (buffer-name buf))
474 (progn (delete-char 1)
475 (insert ?\s))
476 (delete-region (point) (progn (forward-line 1) (point)))
477 (unless (bobp)
478 (forward-char -1))))))))
480 (defun Buffer-menu-select ()
481 "Select this line's buffer; also display buffers marked with `>'.
482 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
483 This command deletes and replaces all the previously existing windows
484 in the selected frame."
485 (interactive)
486 (let ((buff (Buffer-menu-buffer t))
487 (menu (current-buffer))
488 (others ())
489 tem)
490 (Buffer-menu-beginning)
491 (while (re-search-forward "^>" nil t)
492 (setq tem (Buffer-menu-buffer t))
493 (let ((inhibit-read-only t))
494 (delete-char -1)
495 (insert ?\s))
496 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
497 (setq others (nreverse others)
498 tem (/ (1- (frame-height)) (1+ (length others))))
499 (delete-other-windows)
500 (switch-to-buffer buff)
501 (or (eq menu buff)
502 (bury-buffer menu))
503 (if (equal (length others) 0)
504 (progn
505 ;;; ;; Restore previous window configuration before displaying
506 ;;; ;; selected buffers.
507 ;;; (if Buffer-menu-window-config
508 ;;; (progn
509 ;;; (set-window-configuration Buffer-menu-window-config)
510 ;;; (setq Buffer-menu-window-config nil)))
511 (switch-to-buffer buff))
512 (while others
513 (split-window nil tem)
514 (other-window 1)
515 (switch-to-buffer (car others))
516 (setq others (cdr others)))
517 (other-window 1) ;back to the beginning!
520 (defun Buffer-menu-marked-buffers ()
521 "Return a list of buffers marked with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
522 (let (buffers)
523 (Buffer-menu-beginning)
524 (while (re-search-forward "^>" nil t)
525 (setq buffers (cons (Buffer-menu-buffer t) buffers)))
526 (nreverse buffers)))
528 (defun Buffer-menu-isearch-buffers ()
529 "Search for a string through all marked buffers using Isearch."
530 (interactive)
531 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
533 (defun Buffer-menu-isearch-buffers-regexp ()
534 "Search for a regexp through all marked buffers using Isearch."
535 (interactive)
536 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
539 (defun Buffer-menu-visit-tags-table ()
540 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
541 (interactive)
542 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
543 (if file
544 (visit-tags-table file)
545 (error "Specified buffer has no file"))))
547 (defun Buffer-menu-1-window ()
548 "Select this line's buffer, alone, in full frame."
549 (interactive)
550 (switch-to-buffer (Buffer-menu-buffer t))
551 (bury-buffer (other-buffer))
552 (delete-other-windows))
554 (defun Buffer-menu-mouse-select (event)
555 "Select the buffer whose line you click on."
556 (interactive "e")
557 (let (buffer)
558 (with-current-buffer (window-buffer (posn-window (event-end event)))
559 (save-excursion
560 (goto-char (posn-point (event-end event)))
561 (setq buffer (Buffer-menu-buffer t))))
562 (select-window (posn-window (event-end event)))
563 (if (and (window-dedicated-p (selected-window))
564 (eq (selected-window) (frame-root-window)))
565 (switch-to-buffer-other-frame buffer)
566 (switch-to-buffer buffer))))
568 (defun Buffer-menu-this-window ()
569 "Select this line's buffer in this window."
570 (interactive)
571 (switch-to-buffer (Buffer-menu-buffer t)))
573 (defun Buffer-menu-other-window ()
574 "Select this line's buffer in other window, leaving buffer menu visible."
575 (interactive)
576 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
578 (defun Buffer-menu-switch-other-window ()
579 "Make the other window select this line's buffer.
580 The current window remains selected."
581 (interactive)
582 (let ((pop-up-windows t)
583 same-window-buffer-names
584 same-window-regexps)
585 (display-buffer (Buffer-menu-buffer t))))
587 (defun Buffer-menu-2-window ()
588 "Select this line's buffer, with previous buffer in second window."
589 (interactive)
590 (let ((buff (Buffer-menu-buffer t))
591 (menu (current-buffer))
592 (pop-up-windows t)
593 same-window-buffer-names
594 same-window-regexps)
595 (delete-other-windows)
596 (switch-to-buffer (other-buffer))
597 (pop-to-buffer buff)
598 (bury-buffer menu)))
600 (defun Buffer-menu-toggle-read-only ()
601 "Toggle read-only status of buffer on this line, perhaps via version control."
602 (interactive)
603 (let (char)
604 (with-current-buffer (Buffer-menu-buffer t)
605 (toggle-read-only)
606 (setq char (if buffer-read-only ?% ?\s)))
607 (save-excursion
608 (beginning-of-line)
609 (forward-char 1)
610 (if (/= (following-char) char)
611 (let ((inhibit-read-only t))
612 (delete-char 1)
613 (insert char))))))
615 (defun Buffer-menu-bury ()
616 "Bury the buffer listed on this line."
617 (interactive)
618 (when (Buffer-menu-no-header)
619 (save-excursion
620 (beginning-of-line)
621 (bury-buffer (Buffer-menu-buffer t))
622 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
623 (inhibit-read-only t))
624 (delete-region (point) (progn (forward-line -1) (point)))
625 (goto-char (point-max))
626 (insert line))
627 (message "Buried buffer moved to the end"))))
630 (defun Buffer-menu-view ()
631 "View this line's buffer in View mode."
632 (interactive)
633 (view-buffer (Buffer-menu-buffer t)))
636 (defun Buffer-menu-view-other-window ()
637 "View this line's buffer in View mode in another window."
638 (interactive)
639 (view-buffer-other-window (Buffer-menu-buffer t)))
642 ;;;###autoload
643 (define-key ctl-x-map "\C-b" 'list-buffers)
645 ;;;###autoload
646 (defun list-buffers (&optional files-only)
647 "Display a list of names of existing buffers.
648 The list is displayed in a buffer named `*Buffer List*'.
649 Note that buffers with names starting with spaces are omitted.
650 Non-null optional arg FILES-ONLY means mention only file buffers.
652 For more information, see the function `buffer-menu'."
653 (interactive "P")
654 (display-buffer (list-buffers-noselect files-only)))
656 (defconst Buffer-menu-short-ellipsis
657 ;; This file is preloaded, so we can't use char-displayable-p here
658 ;; because we don't know yet what display we're going to connect to.
659 ":" ;; (if (char-displayable-p ?…) "…" ":")
662 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
663 (if (> (+ (string-width name) (string-width size) 2)
664 Buffer-menu-buffer+size-width)
665 (setq name
666 (let ((tail
667 (if (string-match "<[0-9]+>$" name)
668 (match-string 0 name)
669 "")))
670 (concat (truncate-string-to-width
671 name
672 (- Buffer-menu-buffer+size-width
673 (max (string-width size) 3)
674 (string-width tail)
676 Buffer-menu-short-ellipsis
677 tail)))
678 ;; Don't put properties on (buffer-name).
679 (setq name (copy-sequence name)))
680 (add-text-properties 0 (length name) name-props name)
681 (add-text-properties 0 (length size) size-props size)
682 (let ((name+space-width (- Buffer-menu-buffer+size-width
683 (string-width size))))
684 (concat name
685 (propertize (make-string (- name+space-width (string-width name))
686 ?\s)
687 'display `(space :align-to ,(+ 4 name+space-width)))
688 size)))
690 (defun Buffer-menu-sort (column)
691 "Sort the buffer menu by COLUMN."
692 (interactive "P")
693 (when column
694 (setq column (prefix-numeric-value column))
695 (if (< column 2) (setq column 2))
696 (if (> column 5) (setq column 5)))
697 (setq Buffer-menu-sort-column column)
698 (let ((inhibit-read-only t) l buf m1 m2)
699 (save-excursion
700 (Buffer-menu-beginning)
701 (while (not (eobp))
702 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
703 (setq m1 (char-after)
704 m1 (if (memq m1 '(?> ?D)) m1)
705 m2 (char-after (+ (point) 2))
706 m2 (if (eq m2 ?S) m2))
707 (if (or m1 m2)
708 (push (list buf m1 m2) l)))
709 (forward-line)))
710 (revert-buffer)
711 (save-excursion
712 (Buffer-menu-beginning)
713 (while (not (eobp))
714 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
715 (setq m1 (cadr buf)
716 m2 (cadr (cdr buf)))
717 (when m1
718 (delete-char 1)
719 (insert m1)
720 (backward-char 1))
721 (when m2
722 (forward-char 2)
723 (delete-char 1)
724 (insert m2)))
725 (forward-line)))))
727 (defun Buffer-menu-sort-by-column (&optional e)
728 "Sort the buffer menu by the column clicked on."
729 (interactive (list last-input-event))
730 (if e (mouse-select-window e))
731 (let* ((pos (event-start e))
732 (obj (posn-object pos))
733 (col (if obj
734 (get-text-property (cdr obj) 'column (car obj))
735 (get-text-property (posn-point pos) 'column))))
736 (Buffer-menu-sort col)))
738 (defvar Buffer-menu-sort-button-map
739 (let ((map (make-sparse-keymap)))
740 ;; This keymap handles both nil and non-nil values for
741 ;; Buffer-menu-use-header-line.
742 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
743 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
744 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
745 (define-key map [follow-link] 'mouse-face)
746 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
747 map)
748 "Local keymap for Buffer menu sort buttons.")
750 (defun Buffer-menu-make-sort-button (name column)
751 (if (equal column Buffer-menu-sort-column) (setq column nil))
752 (propertize name
753 'column column
754 'help-echo (concat
755 (if Buffer-menu-use-header-line
756 "mouse-1, mouse-2: sort by "
757 "mouse-2, RET: sort by ")
758 (if column (downcase name) "visited order"))
759 'mouse-face 'highlight
760 'keymap Buffer-menu-sort-button-map))
762 (defun list-buffers-noselect (&optional files-only buffer-list)
763 "Create and return a buffer with a list of names of existing buffers.
764 The buffer is named `*Buffer List*'.
765 Note that buffers with names starting with spaces are omitted.
766 Non-null optional arg FILES-ONLY means mention only file buffers.
768 If BUFFER-LIST is non-nil, it should be a list of buffers;
769 it means list those buffers and no others.
771 For more information, see the function `buffer-menu'."
772 (let* ((old-buffer (current-buffer))
773 (standard-output standard-output)
774 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
775 (header (concat "CRM "
776 (Buffer-menu-buffer+size
777 (Buffer-menu-make-sort-button "Buffer" 2)
778 (Buffer-menu-make-sort-button "Size" 3))
780 (Buffer-menu-make-sort-button "Mode" 4) mode-end
781 (Buffer-menu-make-sort-button "File" 5) "\n"))
782 list desired-point)
783 (when Buffer-menu-use-header-line
784 (let ((pos 0))
785 ;; Turn whitespace chars in the header into stretch specs so
786 ;; they work regardless of the header-line face.
787 (while (string-match "[ \t\n]+" header pos)
788 (setq pos (match-end 0))
789 (put-text-property (match-beginning 0) pos 'display
790 ;; Assume fixed-size chars in the buffer.
791 (list 'space :align-to pos)
792 header)))
793 ;; Try to better align the one-char headers.
794 (put-text-property 0 3 'face 'fixed-pitch header)
795 ;; Add a "dummy" leading space to align the beginning of the header
796 ;; line with the beginning of the text (rather than with the left
797 ;; scrollbar or the left fringe). --Stef
798 (setq header (concat (propertize " " 'display '(space :align-to 0))
799 header)))
800 (with-current-buffer (get-buffer-create "*Buffer List*")
801 (setq buffer-read-only nil)
802 (erase-buffer)
803 (setq standard-output (current-buffer))
804 (unless Buffer-menu-use-header-line
805 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
806 ;; (i.e. U+002D, HYPHEN-MINUS).
807 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
808 (insert header
809 (apply 'string
810 (mapcar (lambda (c)
811 (if (memq c '(?\n ?\s)) c underline))
812 header)))))
813 ;; Collect info for every buffer we're interested in.
814 (dolist (buffer (or buffer-list
815 (buffer-list
816 (when Buffer-menu-use-frame-buffer-list
817 (selected-frame)))))
818 (with-current-buffer buffer
819 (let ((name (buffer-name))
820 (file buffer-file-name))
821 (unless (and (not buffer-list)
823 ;; Don't mention internal buffers.
824 (and (string= (substring name 0 1) " ") (null file))
825 ;; Maybe don't mention buffers without files.
826 (and files-only (not file))
827 (string= name "*Buffer List*")))
828 ;; Otherwise output info.
829 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
830 (if mode-line-process
831 (format-mode-line mode-line-process
832 nil nil buffer))))
833 (bits (string
834 (if (eq buffer old-buffer) ?. ?\s)
835 ;; Handle readonly status. The output buffer
836 ;; is special cased to appear readonly; it is
837 ;; actually made so at a later date.
838 (if (or (eq buffer standard-output)
839 buffer-read-only)
840 ?% ?\s)
841 ;; Identify modified buffers.
842 (if (buffer-modified-p) ?* ?\s)
843 ;; Space separator.
844 ?\s)))
845 (unless file
846 ;; No visited file. Check local value of
847 ;; list-buffers-directory and, for Info buffers,
848 ;; Info-current-file.
849 (cond ((and (boundp 'list-buffers-directory)
850 list-buffers-directory)
851 (setq file list-buffers-directory))
852 ((eq major-mode 'Info-mode)
853 (setq file Info-current-file)
854 (cond
855 ((equal file "dir")
856 (setq file "*Info Directory*"))
857 ((eq file 'apropos)
858 (setq file "*Info Apropos*"))
859 ((eq file 'history)
860 (setq file "*Info History*"))
861 ((eq file 'toc)
862 (setq file "*Info TOC*"))
863 ((not (stringp file)) ;; avoid errors
864 (setq file nil))
866 (setq file (concat "("
867 (file-name-nondirectory file)
868 ") "
869 Info-current-node)))))))
870 (push (list buffer bits name (buffer-size) mode file)
871 list))))))
872 ;; Preserve the original buffer-list ordering, just in case.
873 (setq list (nreverse list))
874 ;; Place the buffers's info in the output buffer, sorted if necessary.
875 (dolist (buffer
876 (if Buffer-menu-sort-column
877 (sort list
878 (if (eq Buffer-menu-sort-column 3)
879 (lambda (a b)
880 (< (nth Buffer-menu-sort-column a)
881 (nth Buffer-menu-sort-column b)))
882 (lambda (a b)
883 (string< (nth Buffer-menu-sort-column a)
884 (nth Buffer-menu-sort-column b)))))
885 list))
886 (if (eq (car buffer) old-buffer)
887 (setq desired-point (point)))
888 (insert (cadr buffer)
889 ;; Put the buffer name into a text property
890 ;; so we don't have to extract it from the text.
891 ;; This way we avoid problems with unusual buffer names.
892 (let ((name (nth 2 buffer))
893 (size (int-to-string (nth 3 buffer))))
894 (Buffer-menu-buffer+size name size
895 `(buffer-name ,name
896 buffer ,(car buffer)
897 font-lock-face buffer-menu-buffer
898 mouse-face highlight
899 help-echo
900 ,(if (>= (length name)
901 (- Buffer-menu-buffer+size-width
902 (max (length size) 3)
904 name
905 "mouse-2: select this buffer"))))
907 (if (> (string-width (nth 4 buffer)) Buffer-menu-mode-width)
908 (truncate-string-to-width (nth 4 buffer)
909 Buffer-menu-mode-width)
910 (nth 4 buffer)))
911 (when (nth 5 buffer)
912 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
913 Buffer-menu-mode-width 4) 1)
914 (princ (abbreviate-file-name (nth 5 buffer))))
915 (princ "\n"))
916 (Buffer-menu-mode)
917 (when Buffer-menu-use-header-line
918 (setq header-line-format header))
919 ;; DESIRED-POINT doesn't have to be set; it is not when the
920 ;; current buffer is not displayed for some reason.
921 (and desired-point
922 (goto-char desired-point))
923 (setq Buffer-menu-files-only files-only)
924 (set-buffer-modified-p nil)
925 (current-buffer))))
927 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
928 ;;; buff-menu.el ends here