* emacs-lisp/ert.el (ert-run-tests-interactively):
[emacs.git] / lisp / buff-menu.el
blob58fde695b39f9c7b79a3cd959c674c98534606e5
1 ;;; buff-menu.el --- Interface for viewing and manipulating buffers
3 ;; Copyright (C) 1985-1987, 1993-1995, 2000-2013 Free Software
4 ;; 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 ;; The Buffer Menu is used to view, edit, delete, or change attributes
28 ;; of buffers. The entry points are C-x C-b (`list-buffers') and
29 ;; M-x buffer-menu.
31 ;;; Code:
33 (require 'tabulated-list)
35 (defgroup Buffer-menu nil
36 "Show a menu of all buffers in a buffer."
37 :group 'tools
38 :group 'convenience)
40 (defcustom Buffer-menu-use-header-line t
41 "If non-nil, use the header line to display Buffer Menu column titles."
42 :type 'boolean
43 :group 'Buffer-menu)
45 (defface buffer-menu-buffer
46 '((t (:weight bold)))
47 "Face for buffer names in the Buffer Menu."
48 :group 'Buffer-menu)
49 (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer)
51 (defcustom Buffer-menu-buffer+size-width nil
52 "Combined width of buffer name and size columns in Buffer Menu.
53 If nil, use `Buffer-menu-name-width' and `Buffer-menu-size-width'.
55 If non-nil, the value of `Buffer-menu-name-width' is overridden;
56 the name column is assigned width `Buffer-menu-buffer+size-width'
57 minus `Buffer-menu-size-width'. This use is deprecated."
58 :type '(choice (const nil) number)
59 :group 'Buffer-menu
60 :version "24.3")
62 (make-obsolete-variable 'Buffer-menu-buffer+size-width
63 "use `Buffer-menu-name-width' and `Buffer-menu-size-width' instead."
64 "24.3")
66 (defcustom Buffer-menu-name-width 19
67 "Width of buffer name column in the Buffer Menu."
68 :type 'number
69 :group 'Buffer-menu
70 :version "24.3")
72 (defcustom Buffer-menu-size-width 7
73 "Width of buffer size column in the Buffer Menu."
74 :type 'number
75 :group 'Buffer-menu
76 :version "24.3")
78 (defcustom Buffer-menu-mode-width 16
79 "Width of mode name column in the Buffer Menu."
80 :type 'number
81 :group 'Buffer-menu)
83 (defcustom Buffer-menu-use-frame-buffer-list t
84 "If non-nil, the Buffer Menu uses the selected frame's buffer list.
85 Buffers that were never selected in that frame are listed at the end.
86 If the value is nil, the Buffer Menu uses the global buffer list.
87 This variable matters if the Buffer Menu is sorted by visited order,
88 as it is by default."
89 :type 'boolean
90 :group 'Buffer-menu
91 :version "22.1")
93 (defvar Buffer-menu-files-only nil
94 "Non-nil if the current Buffer Menu lists only file buffers.
95 This is set by the prefix argument to `buffer-menu' and related
96 commands.")
97 (make-variable-buffer-local 'Buffer-menu-files-only)
99 (defvar Info-current-file) ; from info.el
100 (defvar Info-current-node) ; from info.el
102 (defvar Buffer-menu-mode-map
103 (let ((map (make-sparse-keymap))
104 (menu-map (make-sparse-keymap)))
105 (set-keymap-parent map tabulated-list-mode-map)
106 (define-key map "v" 'Buffer-menu-select)
107 (define-key map "2" 'Buffer-menu-2-window)
108 (define-key map "1" 'Buffer-menu-1-window)
109 (define-key map "f" 'Buffer-menu-this-window)
110 (define-key map "e" 'Buffer-menu-this-window)
111 (define-key map "\C-m" 'Buffer-menu-this-window)
112 (define-key map "o" 'Buffer-menu-other-window)
113 (define-key map "\C-o" 'Buffer-menu-switch-other-window)
114 (define-key map "s" 'Buffer-menu-save)
115 (define-key map "d" 'Buffer-menu-delete)
116 (define-key map "k" 'Buffer-menu-delete)
117 (define-key map "\C-k" 'Buffer-menu-delete)
118 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
119 (define-key map "x" 'Buffer-menu-execute)
120 (define-key map " " 'next-line)
121 (define-key map "\177" 'Buffer-menu-backup-unmark)
122 (define-key map "~" 'Buffer-menu-not-modified)
123 (define-key map "u" 'Buffer-menu-unmark)
124 (define-key map "m" 'Buffer-menu-mark)
125 (define-key map "t" 'Buffer-menu-visit-tags-table)
126 (define-key map "%" 'Buffer-menu-toggle-read-only)
127 (define-key map "b" 'Buffer-menu-bury)
128 (define-key map "V" 'Buffer-menu-view)
129 (define-key map "T" 'Buffer-menu-toggle-files-only)
130 (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers)
131 (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp)
132 (define-key map (kbd "M-s a C-o") 'Buffer-menu-multi-occur)
134 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
135 (define-key map [follow-link] 'mouse-face)
137 (define-key map [menu-bar Buffer-menu-mode] (cons (purecopy "Buffer-Menu") menu-map))
138 (bindings--define-key menu-map [quit]
139 '(menu-item "Quit" quit-window
140 :help "Remove the buffer menu from the display"))
141 (bindings--define-key menu-map [rev]
142 '(menu-item "Refresh" revert-buffer
143 :help "Refresh the *Buffer List* buffer contents"))
144 (bindings--define-key menu-map [s0] menu-bar-separator)
145 (bindings--define-key menu-map [tf]
146 '(menu-item "Show Only File Buffers" Buffer-menu-toggle-files-only
147 :button (:toggle . Buffer-menu-files-only)
148 :help "Toggle whether the current buffer-menu displays only file buffers"))
149 (bindings--define-key menu-map [s1] menu-bar-separator)
150 ;; FIXME: The "Select" entries could use better names...
151 (bindings--define-key menu-map [sel]
152 '(menu-item "Select Marked" Buffer-menu-select
153 :help "Select this line's buffer; also display buffers marked with `>'"))
154 (bindings--define-key menu-map [bm2]
155 '(menu-item "Select Two" Buffer-menu-2-window
156 :help "Select this line's buffer, with previous buffer in second window"))
157 (bindings--define-key menu-map [bm1]
158 '(menu-item "Select Current" Buffer-menu-1-window
159 :help "Select this line's buffer, alone, in full frame"))
160 (bindings--define-key menu-map [ow]
161 '(menu-item "Select in Other Window" Buffer-menu-other-window
162 :help "Select this line's buffer in other window, leaving buffer menu visible"))
163 (bindings--define-key menu-map [tw]
164 '(menu-item "Select in Current Window" Buffer-menu-this-window
165 :help "Select this line's buffer in this window"))
166 (bindings--define-key menu-map [s2] menu-bar-separator)
167 (bindings--define-key menu-map [is]
168 '(menu-item "Regexp Isearch Marked Buffers..." Buffer-menu-isearch-buffers-regexp
169 :help "Search for a regexp through all marked buffers using Isearch"))
170 (bindings--define-key menu-map [ir]
171 '(menu-item "Isearch Marked Buffers..." Buffer-menu-isearch-buffers
172 :help "Search for a string through all marked buffers using Isearch"))
173 (bindings--define-key menu-map [mo]
174 '(menu-item "Multi Occur Marked Buffers..." Buffer-menu-multi-occur
175 :help "Show lines matching a regexp in marked buffers using Occur"))
176 (bindings--define-key menu-map [s3] menu-bar-separator)
177 (bindings--define-key menu-map [by]
178 '(menu-item "Bury" Buffer-menu-bury
179 :help "Bury the buffer listed on this line"))
180 (bindings--define-key menu-map [vt]
181 '(menu-item "Set Unmodified" Buffer-menu-not-modified
182 :help "Mark buffer on this line as unmodified (no changes to save)"))
183 (bindings--define-key menu-map [ex]
184 '(menu-item "Execute" Buffer-menu-execute
185 :help "Save and/or delete buffers marked with s or k commands"))
186 (bindings--define-key menu-map [s4] menu-bar-separator)
187 (bindings--define-key menu-map [delb]
188 '(menu-item "Mark for Delete and Move Backwards" Buffer-menu-delete-backwards
189 :help "Mark buffer on this line to be deleted by x command and move up one line"))
190 (bindings--define-key menu-map [del]
191 '(menu-item "Mark for Delete" Buffer-menu-delete
192 :help "Mark buffer on this line to be deleted by x command"))
194 (bindings--define-key menu-map [sv]
195 '(menu-item "Mark for Save" Buffer-menu-save
196 :help "Mark buffer on this line to be saved by x command"))
197 (bindings--define-key menu-map [umk]
198 '(menu-item "Unmark" Buffer-menu-unmark
199 :help "Cancel all requested operations on buffer on this line and move down"))
200 (bindings--define-key menu-map [mk]
201 '(menu-item "Mark" Buffer-menu-mark
202 :help "Mark buffer on this line for being displayed by v command"))
203 map)
204 "Local keymap for `Buffer-menu-mode' buffers.")
206 (define-obsolete-variable-alias 'buffer-menu-mode-hook
207 'Buffer-menu-mode-hook "23.1")
209 (define-derived-mode Buffer-menu-mode tabulated-list-mode "Buffer Menu"
210 "Major mode for Buffer Menu buffers.
211 The Buffer Menu is invoked by the commands \\[list-buffers],
212 \\[buffer-menu], and \\[buffer-menu-other-window].
213 See `buffer-menu' for a description of its contents.
215 In Buffer Menu mode, the following commands are defined:
216 \\<Buffer-menu-mode-map>
217 \\[quit-window] Remove the Buffer Menu from the display.
218 \\[Buffer-menu-this-window] Select current line's buffer in place of the buffer menu.
219 \\[Buffer-menu-other-window] Select that buffer in another window,
220 so the Buffer Menu remains visible in its window.
221 \\[Buffer-menu-view] Select current line's buffer, in View mode.
222 \\[Buffer-menu-view-other-window] Select that buffer in
223 another window, in view-mode.
224 \\[Buffer-menu-switch-other-window] Make another window display that buffer.
225 \\[Buffer-menu-mark] Mark current line's buffer to be displayed.
226 \\[Buffer-menu-select] Select current line's buffer.
227 Also show buffers marked with m, in other windows.
228 \\[Buffer-menu-1-window] Select that buffer in full-frame window.
229 \\[Buffer-menu-2-window] Select that buffer in one window, together with the
230 buffer selected before this one in another window.
231 \\[Buffer-menu-isearch-buffers] Incremental search in the marked buffers.
232 \\[Buffer-menu-isearch-buffers-regexp] Isearch for regexp in the marked buffers.
233 \\[Buffer-menu-multi-occur] Show lines matching regexp in the marked buffers.
234 \\[Buffer-menu-visit-tags-table] visit-tags-table this buffer.
235 \\[Buffer-menu-not-modified] Clear modified-flag on that buffer.
236 \\[Buffer-menu-save] Mark that buffer to be saved, and move down.
237 \\[Buffer-menu-delete] Mark that buffer to be deleted, and move down.
238 \\[Buffer-menu-delete-backwards] Mark that buffer to be deleted, and move up.
239 \\[Buffer-menu-execute] Delete or save marked buffers.
240 \\[Buffer-menu-unmark] Remove all marks from current line.
241 With prefix argument, also move up one line.
242 \\[Buffer-menu-backup-unmark] Back up a line and remove marks.
243 \\[Buffer-menu-toggle-read-only] Toggle read-only status of buffer on this line.
244 \\[revert-buffer] Update the list of buffers.
245 \\[Buffer-menu-toggle-files-only] Toggle whether the menu displays only file buffers.
246 \\[Buffer-menu-bury] Bury the buffer listed on this line."
247 (set (make-local-variable 'buffer-stale-function)
248 (lambda (&optional _noconfirm) 'fast))
249 (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t))
251 (defun buffer-menu (&optional arg)
252 "Switch to the Buffer Menu.
253 By default, the Buffer Menu lists all buffers except those whose
254 names start with a space (which are for internal use). With
255 prefix argument ARG, show only buffers that are visiting files.
257 In the Buffer Menu, the first column (denoted \"C\") shows \".\"
258 for the buffer from which you came, \">\" for buffers you mark to
259 be displayed, and \"D\" for those you mark for deletion.
261 The \"R\" column has a \"%\" if the buffer is read-only.
262 The \"M\" column has a \"*\" if it is modified, or \"S\" if you
263 have marked it for saving.
265 The remaining columns show the buffer name, the buffer size in
266 characters, its major mode, and the visited file name (if any).
268 See `Buffer-menu-mode' for the keybindings available the Buffer
269 Menu."
270 (interactive "P")
271 (switch-to-buffer (list-buffers-noselect arg))
272 (message
273 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
275 (defun buffer-menu-other-window (&optional arg)
276 "Display the Buffer Menu in another window.
277 See `buffer-menu' for a description of the Buffer Menu.
279 By default, all buffers are listed except those whose names start
280 with a space (which are for internal use). With prefix argument
281 ARG, show only buffers that are visiting files."
282 (interactive "P")
283 (switch-to-buffer-other-window (list-buffers-noselect arg))
284 (message
285 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
287 ;;;###autoload
288 (defun list-buffers (&optional arg)
289 "Display a list of existing buffers.
290 The list is displayed in a buffer named \"*Buffer List*\".
291 See `buffer-menu' for a description of the Buffer Menu.
293 By default, all buffers are listed except those whose names start
294 with a space (which are for internal use). With prefix argument
295 ARG, show only buffers that are visiting files."
296 (interactive "P")
297 (display-buffer (list-buffers-noselect arg)))
299 (defun Buffer-menu-toggle-files-only (arg)
300 "Toggle whether the current buffer-menu displays only file buffers.
301 With a positive ARG, display only file buffers. With zero or
302 negative ARG, display other buffers as well."
303 (interactive "P")
304 (setq Buffer-menu-files-only
305 (cond ((not arg) (not Buffer-menu-files-only))
306 ((> (prefix-numeric-value arg) 0) t)))
307 (message (if Buffer-menu-files-only
308 "Showing only file-visiting buffers."
309 "Showing all non-internal buffers."))
310 (revert-buffer))
312 (defalias 'Buffer-menu-sort 'tabulated-list-sort)
315 (defun Buffer-menu-buffer (&optional error-if-non-existent-p)
316 "Return the buffer described by the current Buffer Menu line.
317 If there is no buffer here, return nil if ERROR-IF-NON-EXISTENT-P
318 is nil or omitted, and signal an error otherwise."
319 (let ((buffer (tabulated-list-get-id)))
320 (cond ((null buffer)
321 (if error-if-non-existent-p
322 (error "No buffer on this line")))
323 ((not (buffer-live-p buffer))
324 (if error-if-non-existent-p
325 (error "This buffer has been killed")))
326 (t buffer))))
328 (defun Buffer-menu-no-header ()
329 (beginning-of-line)
330 (if (or Buffer-menu-use-header-line
331 (not (eq (char-after) ?C)))
333 (ding)
334 (forward-line 1)
335 nil))
337 (defun Buffer-menu-beginning ()
338 (goto-char (point-min))
339 (unless Buffer-menu-use-header-line
340 (forward-line)))
343 ;;; Commands for modifying Buffer Menu entries.
345 (defun Buffer-menu-mark ()
346 "Mark the Buffer menu entry at point for later display.
347 It will be displayed by the \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
348 (interactive)
349 (tabulated-list-set-col 0 ">" t)
350 (forward-line))
352 (defun Buffer-menu-unmark (&optional backup)
353 "Cancel all requested operations on buffer on this line and move down.
354 Optional prefix arg means move up."
355 (interactive "P")
356 (tabulated-list-set-col 0 " " t)
357 (forward-line (if backup -1 1)))
359 (defun Buffer-menu-backup-unmark ()
360 "Move up and cancel all requested operations on buffer on line above."
361 (interactive)
362 (forward-line -1)
363 (tabulated-list-set-col 0 " " t))
365 (defun Buffer-menu-delete (&optional arg)
366 "Mark the buffer on this Buffer Menu buffer line for deletion.
367 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
368 will delete it.
370 If prefix argument ARG is non-nil, it specifies the number of
371 buffers to delete; a negative ARG means to delete backwards."
372 (interactive "p")
373 (if (or (null arg) (= arg 0))
374 (setq arg 1))
375 (while (> arg 0)
376 (when (Buffer-menu-buffer)
377 (tabulated-list-set-col 0 "D" t))
378 (forward-line 1)
379 (setq arg (1- arg)))
380 (while (< arg 0)
381 (when (Buffer-menu-buffer)
382 (tabulated-list-set-col 0 "D" t))
383 (forward-line -1)
384 (setq arg (1+ arg))))
386 (defun Buffer-menu-delete-backwards (&optional arg)
387 "Mark the buffer on this Buffer Menu line for deletion, and move up.
388 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]'
389 command will delete the marked buffer. Prefix ARG means move
390 that many lines."
391 (interactive "p")
392 (Buffer-menu-delete (- (or arg 1))))
394 (defun Buffer-menu-save ()
395 "Mark the buffer on this Buffer Menu line for saving.
396 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
397 will save it."
398 (interactive)
399 (when (Buffer-menu-buffer)
400 (tabulated-list-set-col 2 "S" t)
401 (forward-line 1)))
403 (defun Buffer-menu-not-modified (&optional arg)
404 "Mark the buffer on this line as unmodified (no changes to save).
405 If ARG is non-nil (interactively, with a prefix argument), mark
406 it as modified."
407 (interactive "P")
408 (with-current-buffer (Buffer-menu-buffer t)
409 (set-buffer-modified-p arg))
410 (tabulated-list-set-col 2 (if arg "*" " ") t))
412 (defun Buffer-menu-execute ()
413 "Save and/or delete marked buffers in the Buffer Menu.
414 Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-save]' are saved.
415 Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-delete]' are deleted."
416 (interactive)
417 (save-excursion
418 (Buffer-menu-beginning)
419 (while (not (eobp))
420 (let ((buffer (tabulated-list-get-id))
421 (entry (tabulated-list-get-entry)))
422 (cond ((null entry)
423 (forward-line 1))
424 ((not (buffer-live-p buffer))
425 (tabulated-list-delete-entry))
427 (let ((delete (eq (char-after) ?D)))
428 (when (equal (aref entry 2) "S")
429 (condition-case nil
430 (progn
431 (with-current-buffer buffer
432 (save-buffer))
433 (tabulated-list-set-col 2 " " t))
434 (error (warn "Error saving %s" buffer))))
435 (if delete
436 (unless (eq buffer (current-buffer))
437 (kill-buffer buffer)
438 (tabulated-list-delete-entry))
439 (forward-line 1)))))))))
441 (defun Buffer-menu-select ()
442 "Select this line's buffer; also, display buffers marked with `>'.
443 You can mark buffers with the \\<Buffer-menu-mode-map>`\\[Buffer-menu-mark]' command.
444 This command deletes and replaces all the previously existing windows
445 in the selected frame."
446 (interactive)
447 (let* ((this-buffer (Buffer-menu-buffer t))
448 (menu-buffer (current-buffer))
449 (others (delq this-buffer (Buffer-menu-marked-buffers t)))
450 (height (/ (1- (frame-height)) (1+ (length others)))))
451 (delete-other-windows)
452 (switch-to-buffer this-buffer)
453 (unless (eq menu-buffer this-buffer)
454 (bury-buffer menu-buffer))
455 (dolist (buffer others)
456 (split-window nil height)
457 (other-window 1)
458 (switch-to-buffer buffer))
459 ;; Back to the beginning!
460 (other-window 1)))
462 (defun Buffer-menu-marked-buffers (&optional unmark)
463 "Return the list of buffers marked with `Buffer-menu-mark'.
464 If UNMARK is non-nil, unmark them."
465 (let (buffers)
466 (Buffer-menu-beginning)
467 (while (re-search-forward "^>" nil t)
468 (let ((buffer (Buffer-menu-buffer)))
469 (if (and buffer unmark)
470 (tabulated-list-set-col 0 " " t))
471 (if (buffer-live-p buffer)
472 (push buffer buffers))))
473 (nreverse buffers)))
475 (defun Buffer-menu-isearch-buffers ()
476 "Search for a string through all marked buffers using Isearch."
477 (interactive)
478 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
480 (defun Buffer-menu-isearch-buffers-regexp ()
481 "Search for a regexp through all marked buffers using Isearch."
482 (interactive)
483 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
485 (defun Buffer-menu-multi-occur (regexp &optional nlines)
486 "Show all lines in marked buffers containing a match for a regexp."
487 (interactive (occur-read-primary-args))
488 (multi-occur (Buffer-menu-marked-buffers) regexp nlines))
491 (defun Buffer-menu-visit-tags-table ()
492 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
493 (interactive)
494 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
495 (if file
496 (visit-tags-table file)
497 (error "Specified buffer has no file"))))
499 (defun Buffer-menu-1-window ()
500 "Select this line's buffer, alone, in full frame."
501 (interactive)
502 (switch-to-buffer (Buffer-menu-buffer t))
503 (bury-buffer (other-buffer))
504 (delete-other-windows))
506 (defun Buffer-menu-this-window ()
507 "Select this line's buffer in this window."
508 (interactive)
509 (switch-to-buffer (Buffer-menu-buffer t)))
511 (defun Buffer-menu-other-window ()
512 "Select this line's buffer in other window, leaving buffer menu visible."
513 (interactive)
514 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
516 (defun Buffer-menu-switch-other-window ()
517 "Make the other window select this line's buffer.
518 The current window remains selected."
519 (interactive)
520 (display-buffer (Buffer-menu-buffer t) t))
522 (defun Buffer-menu-2-window ()
523 "Select this line's buffer, with previous buffer in second window."
524 (interactive)
525 (let ((buff (Buffer-menu-buffer t))
526 (menu (current-buffer)))
527 (delete-other-windows)
528 (switch-to-buffer (other-buffer))
529 (switch-to-buffer-other-window buff)
530 (bury-buffer menu)))
532 (defun Buffer-menu-toggle-read-only ()
533 "Toggle read-only status of buffer on this line.
534 This behaves like invoking \\[toggle-read-only] in that buffer."
535 (interactive)
536 (let ((read-only
537 (with-current-buffer (Buffer-menu-buffer t)
538 (read-only-mode 'toggle)
539 buffer-read-only)))
540 (tabulated-list-set-col 1 (if read-only "%" " ") t)))
542 (defun Buffer-menu-bury ()
543 "Bury the buffer listed on this line."
544 (interactive)
545 (let ((buffer (tabulated-list-get-id)))
546 (cond ((null buffer))
547 ((buffer-live-p buffer)
548 (bury-buffer buffer)
549 (save-excursion
550 (let ((elt (tabulated-list-delete-entry)))
551 (goto-char (point-max))
552 (apply 'tabulated-list-print-entry elt)))
553 (message "Buffer buried."))
555 (tabulated-list-delete-entry)
556 (message "Buffer is dead; removing from list.")))))
558 (defun Buffer-menu-view ()
559 "View this line's buffer in View mode."
560 (interactive)
561 (view-buffer (Buffer-menu-buffer t)))
563 (defun Buffer-menu-view-other-window ()
564 "View this line's buffer in View mode in another window."
565 (interactive)
566 (view-buffer-other-window (Buffer-menu-buffer t)))
568 ;;; Functions for populating the Buffer Menu.
570 ;;;###autoload
571 (defun list-buffers-noselect (&optional files-only buffer-list)
572 "Create and return a Buffer Menu buffer.
573 This is called by `buffer-menu' and others as a subroutine.
575 If FILES-ONLY is non-nil, show only file-visiting buffers.
576 If BUFFER-LIST is non-nil, it should be a list of buffers; it
577 means list those buffers and no others."
578 (let ((old-buffer (current-buffer))
579 (buffer (get-buffer-create "*Buffer List*")))
580 (with-current-buffer buffer
581 (Buffer-menu-mode)
582 (setq Buffer-menu-files-only
583 (and files-only (>= (prefix-numeric-value files-only) 0)))
584 (list-buffers--refresh buffer-list old-buffer)
585 (tabulated-list-print))
586 buffer))
588 (defun Buffer-menu-mouse-select (event)
589 "Select the buffer whose line you click on."
590 (interactive "e")
591 (select-window (posn-window (event-end event)))
592 (let ((buffer (tabulated-list-get-id (posn-point (event-end event)))))
593 (when (buffer-live-p buffer)
594 (if (and (window-dedicated-p)
595 (eq (selected-window) (frame-root-window)))
596 (switch-to-buffer-other-frame buffer)
597 (switch-to-buffer buffer)))))
599 (defun list-buffers--refresh (&optional buffer-list old-buffer)
600 ;; Set up `tabulated-list-format'.
601 (let ((name-width Buffer-menu-name-width)
602 (size-width Buffer-menu-size-width))
603 ;; Handle obsolete variable:
604 (if Buffer-menu-buffer+size-width
605 (setq name-width (- Buffer-menu-buffer+size-width size-width)))
606 (setq tabulated-list-format
607 (vector '("C" 1 t :pad-right 0)
608 '("R" 1 t :pad-right 0)
609 '("M" 1 t)
610 `("Buffer" ,name-width t)
611 `("Size" ,size-width tabulated-list-entry-size->
612 :right-align t)
613 `("Mode" ,Buffer-menu-mode-width t)
614 '("File" 1 t))))
615 (setq tabulated-list-use-header-line Buffer-menu-use-header-line)
616 ;; Collect info for each buffer we're interested in.
617 (let ((buffer-menu-buffer (current-buffer))
618 (show-non-file (not Buffer-menu-files-only))
619 entries)
620 (dolist (buffer (or buffer-list
621 (buffer-list (if Buffer-menu-use-frame-buffer-list
622 (selected-frame)))))
623 (with-current-buffer buffer
624 (let* ((name (buffer-name))
625 (file buffer-file-name))
626 (when (and (buffer-live-p buffer)
627 (or buffer-list
628 (and (not (string= (substring name 0 1) " "))
629 (not (eq buffer buffer-menu-buffer))
630 (or file show-non-file))))
631 (push (list buffer
632 (vector (if (eq buffer old-buffer) "." " ")
633 (if buffer-read-only "%" " ")
634 (if (buffer-modified-p) "*" " ")
635 (Buffer-menu--pretty-name name)
636 (number-to-string (buffer-size))
637 (concat (format-mode-line mode-name nil nil buffer)
638 (if mode-line-process
639 (format-mode-line mode-line-process
640 nil nil buffer)))
641 (Buffer-menu--pretty-file-name file)))
642 entries)))))
643 (setq tabulated-list-entries (nreverse entries)))
644 (tabulated-list-init-header))
646 (defun tabulated-list-entry-size-> (entry1 entry2)
647 (> (string-to-number (aref (cadr entry1) 4))
648 (string-to-number (aref (cadr entry2) 4))))
650 (defun Buffer-menu--pretty-name (name)
651 (propertize name
652 'font-lock-face 'buffer-menu-buffer
653 'mouse-face 'highlight))
655 (defun Buffer-menu--pretty-file-name (file)
656 (cond (file
657 (abbreviate-file-name file))
658 ((and (boundp 'list-buffers-directory)
659 list-buffers-directory)
660 list-buffers-directory)
661 ((eq major-mode 'Info-mode)
662 (Buffer-menu-info-node-description Info-current-file))
663 (t "")))
665 (defun Buffer-menu-info-node-description (file)
666 (cond
667 ((equal file "dir") "*Info Directory*")
668 ((eq file 'apropos) "*Info Apropos*")
669 ((eq file 'history) "*Info History*")
670 ((eq file 'toc) "*Info TOC*")
671 ((not (stringp file)) "") ; Avoid errors
673 (concat "(" (file-name-nondirectory file) ") " Info-current-node))))
675 ;;; buff-menu.el ends here