* minibuf.texi (Basic Completion): Clarify list form of completion table.
[emacs.git] / lisp / buff-menu.el
blob6ab6e548ab5a51169a43eb0a5688c39f3393ac95
1 ;;; buff-menu.el --- Interface for viewing and manipulating buffers
3 ;; Copyright (C) 1985-1987, 1993-1995, 2000-2012
4 ;; 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 ;; 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 'number
59 :group 'Buffer-menu
60 :version "24.3")
62 (make-obsolete-variable 'Buffer-menu-buffer+size-width
63 "`Buffer-menu-name-width' and `Buffer-menu-size-width'"
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)
133 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
134 (define-key map [follow-link] 'mouse-face)
136 (define-key map [menu-bar Buffer-menu-mode] (cons (purecopy "Buffer-Menu") menu-map))
137 (bindings--define-key menu-map [quit]
138 '(menu-item "Quit" quit-window
139 :help "Remove the buffer menu from the display"))
140 (bindings--define-key menu-map [rev]
141 '(menu-item "Refresh" revert-buffer
142 :help "Refresh the *Buffer List* buffer contents"))
143 (bindings--define-key menu-map [s0] menu-bar-separator)
144 (bindings--define-key menu-map [tf]
145 '(menu-item "Show Only File Buffers" Buffer-menu-toggle-files-only
146 :button (:toggle . Buffer-menu-files-only)
147 :help "Toggle whether the current buffer-menu displays only file buffers"))
148 (bindings--define-key menu-map [s1] menu-bar-separator)
149 ;; FIXME: The "Select" entries could use better names...
150 (bindings--define-key menu-map [sel]
151 '(menu-item "Select Marked" Buffer-menu-select
152 :help "Select this line's buffer; also display buffers marked with `>'"))
153 (bindings--define-key menu-map [bm2]
154 '(menu-item "Select Two" Buffer-menu-2-window
155 :help "Select this line's buffer, with previous buffer in second window"))
156 (bindings--define-key menu-map [bm1]
157 '(menu-item "Select Current" Buffer-menu-1-window
158 :help "Select this line's buffer, alone, in full frame"))
159 (bindings--define-key menu-map [ow]
160 '(menu-item "Select in Other Window" Buffer-menu-other-window
161 :help "Select this line's buffer in other window, leaving buffer menu visible"))
162 (bindings--define-key menu-map [tw]
163 '(menu-item "Select in Current Window" Buffer-menu-this-window
164 :help "Select this line's buffer in this window"))
165 (bindings--define-key menu-map [s2] menu-bar-separator)
166 (bindings--define-key menu-map [is]
167 '(menu-item "Regexp Isearch Marked Buffers..." Buffer-menu-isearch-buffers-regexp
168 :help "Search for a regexp through all marked buffers using Isearch"))
169 (bindings--define-key menu-map [ir]
170 '(menu-item "Isearch Marked Buffers..." Buffer-menu-isearch-buffers
171 :help "Search for a string through all marked buffers using Isearch"))
172 (bindings--define-key menu-map [s3] menu-bar-separator)
173 (bindings--define-key menu-map [by]
174 '(menu-item "Bury" Buffer-menu-bury
175 :help "Bury the buffer listed on this line"))
176 (bindings--define-key menu-map [vt]
177 '(menu-item "Set Unmodified" Buffer-menu-not-modified
178 :help "Mark buffer on this line as unmodified (no changes to save)"))
179 (bindings--define-key menu-map [ex]
180 '(menu-item "Execute" Buffer-menu-execute
181 :help "Save and/or delete buffers marked with s or k commands"))
182 (bindings--define-key menu-map [s4] menu-bar-separator)
183 (bindings--define-key menu-map [delb]
184 '(menu-item "Mark for Delete and Move Backwards" Buffer-menu-delete-backwards
185 :help "Mark buffer on this line to be deleted by x command and move up one line"))
186 (bindings--define-key menu-map [del]
187 '(menu-item "Mark for Delete" Buffer-menu-delete
188 :help "Mark buffer on this line to be deleted by x command"))
190 (bindings--define-key menu-map [sv]
191 '(menu-item "Mark for Save" Buffer-menu-save
192 :help "Mark buffer on this line to be saved by x command"))
193 (bindings--define-key menu-map [umk]
194 '(menu-item "Unmark" Buffer-menu-unmark
195 :help "Cancel all requested operations on buffer on this line and move down"))
196 (bindings--define-key menu-map [mk]
197 '(menu-item "Mark" Buffer-menu-mark
198 :help "Mark buffer on this line for being displayed by v command"))
199 map)
200 "Local keymap for `Buffer-menu-mode' buffers.")
202 (define-obsolete-variable-alias 'buffer-menu-mode-hook
203 'Buffer-menu-mode-hook "23.1")
205 (define-derived-mode Buffer-menu-mode tabulated-list-mode "Buffer Menu"
206 "Major mode for Buffer Menu buffers.
207 The Buffer Menu is invoked by the commands \\[list-buffers], \\[buffer-menu], and
208 \\[buffer-menu-other-window]. See `buffer-menu' for details."
209 (set (make-local-variable 'buffer-stale-function)
210 (lambda (&optional _noconfirm) 'fast))
211 (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t))
213 (defun buffer-menu (&optional arg)
214 "Switch to the Buffer Menu.
215 By default, all buffers are listed except those whose names start
216 with a space (which are for internal use). With prefix argument
217 ARG, show only buffers that are visiting files.
219 The first column (denoted \"C\") shows \".\" for the buffer from
220 which you came. It shows \">\" for buffers you mark to be
221 displayed, and \"D\" for those you mark for deletion.
223 The \"R\" column has a \"%\" if the buffer is read-only.
224 The \"M\" column has a \"*\" if it is modified, or \"S\" if you
225 have marked it for saving.
227 After this come the buffer name, its size in characters, its
228 major mode, and the visited file name (if any).
231 In the Buffer Menu, the following commands are defined:
232 \\<Buffer-menu-mode-map>
233 \\[quit-window] Remove the Buffer Menu from the display.
234 \\[Buffer-menu-this-window] Select current line's buffer in place of the buffer menu.
235 \\[Buffer-menu-other-window] Select that buffer in another window,
236 so the Buffer Menu remains visible in its window.
237 \\[Buffer-menu-view] Select current line's buffer, in View mode.
238 \\[Buffer-menu-view-other-window] Select that buffer in
239 another window, in view-mode.
240 \\[Buffer-menu-switch-other-window] Make another window display that buffer.
241 \\[Buffer-menu-mark] Mark current line's buffer to be displayed.
242 \\[Buffer-menu-select] Select current line's buffer.
243 Also show buffers marked with m, in other windows.
244 \\[Buffer-menu-1-window] Select that buffer in full-frame window.
245 \\[Buffer-menu-2-window] Select that buffer in one window, together with the
246 buffer selected before this one in another window.
247 \\[Buffer-menu-isearch-buffers] Incremental search in the marked buffers.
248 \\[Buffer-menu-isearch-buffers-regexp] Isearch for regexp in the marked buffers.
249 \\[Buffer-menu-visit-tags-table] visit-tags-table this buffer.
250 \\[Buffer-menu-not-modified] Clear modified-flag on that buffer.
251 \\[Buffer-menu-save] Mark that buffer to be saved, and move down.
252 \\[Buffer-menu-delete] Mark that buffer to be deleted, and move down.
253 \\[Buffer-menu-delete-backwards] Mark that buffer to be deleted, and move up.
254 \\[Buffer-menu-execute] Delete or save marked buffers.
255 \\[Buffer-menu-unmark] Remove all marks from current line.
256 With prefix argument, also move up one line.
257 \\[Buffer-menu-backup-unmark] Back up a line and remove marks.
258 \\[Buffer-menu-toggle-read-only] Toggle read-only status of buffer on this line.
259 \\[revert-buffer] Update the list of buffers.
260 \\[Buffer-menu-toggle-files-only] Toggle whether the menu displays only file buffers.
261 \\[Buffer-menu-bury] Bury the buffer listed on this line."
262 (interactive "P")
263 (switch-to-buffer (list-buffers-noselect arg))
264 (message
265 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
267 (defun buffer-menu-other-window (&optional arg)
268 "Display the Buffer Menu in another window.
269 See `buffer-menu' for a description of the Buffer Menu.
271 By default, all buffers are listed except those whose names start
272 with a space (which are for internal use). With prefix argument
273 ARG, show only buffers that are visiting files."
274 (interactive "P")
275 (switch-to-buffer-other-window (list-buffers-noselect arg))
276 (message
277 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
279 ;;;###autoload
280 (defun list-buffers (&optional arg)
281 "Display a list of existing buffers.
282 The list is displayed in a buffer named \"*Buffer List*\".
283 See `buffer-menu' for details about the Buffer Menu buffer.
285 By default, all buffers are listed except those whose names start
286 with a space (which are for internal use). With prefix argument
287 ARG, show only buffers that are visiting files."
288 (interactive "P")
289 (display-buffer (list-buffers-noselect arg)))
291 (defun Buffer-menu-toggle-files-only (arg)
292 "Toggle whether the current buffer-menu displays only file buffers.
293 With a positive ARG, display only file buffers. With zero or
294 negative ARG, display other buffers as well."
295 (interactive "P")
296 (setq Buffer-menu-files-only
297 (cond ((not arg) (not Buffer-menu-files-only))
298 ((> (prefix-numeric-value arg) 0) t)))
299 (message (if Buffer-menu-files-only
300 "Showing only file-visiting buffers."
301 "Showing all non-internal buffers."))
302 (revert-buffer))
304 (defalias 'Buffer-menu-sort 'tabulated-list-sort)
307 (defun Buffer-menu-buffer (&optional error-if-non-existent-p)
308 "Return the buffer described by the current Buffer Menu line.
309 If there is no buffer here, return nil if ERROR-IF-NON-EXISTENT-P
310 is nil or omitted, and signal an error otherwise."
311 (let ((buffer (tabulated-list-get-id)))
312 (cond ((null buffer)
313 (if error-if-non-existent-p
314 (error "No buffer on this line")))
315 ((not (buffer-live-p buffer))
316 (if error-if-non-existent-p
317 (error "This buffer has been killed")))
318 (t buffer))))
320 (defun Buffer-menu-no-header ()
321 (beginning-of-line)
322 (if (or Buffer-menu-use-header-line
323 (not (eq (char-after) ?C)))
325 (ding)
326 (forward-line 1)
327 nil))
329 (defun Buffer-menu-beginning ()
330 (goto-char (point-min))
331 (unless Buffer-menu-use-header-line
332 (forward-line)))
335 ;;; Commands for modifying Buffer Menu entries.
337 (defun Buffer-menu-mark ()
338 "Mark the Buffer menu entry at point for later display.
339 It will be displayed by the \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
340 (interactive)
341 (tabulated-list-set-col 0 ">" t)
342 (forward-line))
344 (defun Buffer-menu-unmark (&optional backup)
345 "Cancel all requested operations on buffer on this line and move down.
346 Optional prefix arg means move up."
347 (interactive "P")
348 (tabulated-list-set-col 0 " " t)
349 (forward-line (if backup -1 1)))
351 (defun Buffer-menu-backup-unmark ()
352 "Move up and cancel all requested operations on buffer on line above."
353 (interactive)
354 (forward-line -1)
355 (tabulated-list-set-col 0 " " t))
357 (defun Buffer-menu-delete (&optional arg)
358 "Mark the buffer on this Buffer Menu buffer line for deletion.
359 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
360 will delete it.
362 If prefix argument ARG is non-nil, it specifies the number of
363 buffers to delete; a negative ARG means to delete backwards."
364 (interactive "p")
365 (if (or (null arg) (= arg 0))
366 (setq arg 1))
367 (while (> arg 0)
368 (when (Buffer-menu-buffer)
369 (tabulated-list-set-col 0 "D" t))
370 (forward-line 1)
371 (setq arg (1- arg)))
372 (while (< arg 0)
373 (when (Buffer-menu-buffer)
374 (tabulated-list-set-col 0 "D" t))
375 (forward-line -1)
376 (setq arg (1+ arg))))
378 (defun Buffer-menu-delete-backwards (&optional arg)
379 "Mark the buffer on this Buffer Menu line for deletion, and move up.
380 Prefix ARG means move that many lines."
381 (interactive "p")
382 (Buffer-menu-delete (- (or arg 1))))
384 (defun Buffer-menu-save ()
385 "Mark the buffer on this Buffer Menu line for saving.
386 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
387 will save it."
388 (interactive)
389 (when (Buffer-menu-buffer)
390 (tabulated-list-set-col 2 "S" t)
391 (forward-line 1)))
393 (defun Buffer-menu-not-modified (&optional arg)
394 "Mark the buffer on this line as unmodified (no changes to save).
395 If ARG is non-nil (interactively, with a prefix argument), mark
396 it as modified."
397 (interactive "P")
398 (with-current-buffer (Buffer-menu-buffer t)
399 (set-buffer-modified-p arg))
400 (tabulated-list-set-col 2 (if arg "*" " ") t))
402 (defun Buffer-menu-execute ()
403 "Save and/or delete marked buffers in the Buffer Menu.
404 Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-save]' are saved.
405 Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-delete]' are deleted."
406 (interactive)
407 (save-excursion
408 (Buffer-menu-beginning)
409 (while (not (eobp))
410 (let ((buffer (tabulated-list-get-id))
411 (entry (tabulated-list-get-entry)))
412 (cond ((null entry)
413 (forward-line 1))
414 ((not (buffer-live-p buffer))
415 (tabulated-list-delete-entry))
417 (let ((delete (eq (char-after) ?D)))
418 (when (equal (aref entry 2) "S")
419 (condition-case nil
420 (progn
421 (with-current-buffer buffer
422 (save-buffer))
423 (tabulated-list-set-col 2 " " t))
424 (error (warn "Error saving %s" buffer))))
425 (if delete
426 (unless (eq buffer (current-buffer))
427 (kill-buffer buffer)
428 (tabulated-list-delete-entry))
429 (forward-line 1)))))))))
431 (defun Buffer-menu-select ()
432 "Select this line's buffer; also, display buffers marked with `>'.
433 You can mark buffers with the \\<Buffer-menu-mode-map>`\\[Buffer-menu-mark]' command.
434 This command deletes and replaces all the previously existing windows
435 in the selected frame."
436 (interactive)
437 (let* ((this-buffer (Buffer-menu-buffer t))
438 (menu-buffer (current-buffer))
439 (others (delq this-buffer (Buffer-menu-marked-buffers t)))
440 (height (/ (1- (frame-height)) (1+ (length others)))))
441 (delete-other-windows)
442 (switch-to-buffer this-buffer)
443 (unless (eq menu-buffer this-buffer)
444 (bury-buffer menu-buffer))
445 (dolist (buffer others)
446 (split-window nil height)
447 (other-window 1)
448 (switch-to-buffer buffer))
449 ;; Back to the beginning!
450 (other-window 1)))
452 (defun Buffer-menu-marked-buffers (&optional unmark)
453 "Return the list of buffers marked with `Buffer-menu-mark'.
454 If UNMARK is non-nil, unmark them."
455 (let (buffers)
456 (Buffer-menu-beginning)
457 (while (re-search-forward "^>" nil t)
458 (let ((buffer (Buffer-menu-buffer)))
459 (if (and buffer unmark)
460 (tabulated-list-set-col 0 " " t))
461 (if (buffer-live-p buffer)
462 (push buffer buffers))))
463 (nreverse buffers)))
465 (defun Buffer-menu-isearch-buffers ()
466 "Search for a string through all marked buffers using Isearch."
467 (interactive)
468 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
470 (defun Buffer-menu-isearch-buffers-regexp ()
471 "Search for a regexp through all marked buffers using Isearch."
472 (interactive)
473 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
476 (defun Buffer-menu-visit-tags-table ()
477 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
478 (interactive)
479 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
480 (if file
481 (visit-tags-table file)
482 (error "Specified buffer has no file"))))
484 (defun Buffer-menu-1-window ()
485 "Select this line's buffer, alone, in full frame."
486 (interactive)
487 (switch-to-buffer (Buffer-menu-buffer t))
488 (bury-buffer (other-buffer))
489 (delete-other-windows))
491 (defun Buffer-menu-this-window ()
492 "Select this line's buffer in this window."
493 (interactive)
494 (switch-to-buffer (Buffer-menu-buffer t)))
496 (defun Buffer-menu-other-window ()
497 "Select this line's buffer in other window, leaving buffer menu visible."
498 (interactive)
499 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
501 (defun Buffer-menu-switch-other-window ()
502 "Make the other window select this line's buffer.
503 The current window remains selected."
504 (interactive)
505 (display-buffer (Buffer-menu-buffer t) t))
507 (defun Buffer-menu-2-window ()
508 "Select this line's buffer, with previous buffer in second window."
509 (interactive)
510 (let ((buff (Buffer-menu-buffer t))
511 (menu (current-buffer)))
512 (delete-other-windows)
513 (switch-to-buffer (other-buffer))
514 (switch-to-buffer-other-window buff)
515 (bury-buffer menu)))
517 (defun Buffer-menu-toggle-read-only ()
518 "Toggle read-only status of buffer on this line.
519 This behaves like invoking \\[toggle-read-only] in that buffer."
520 (interactive)
521 (let ((read-only
522 (with-current-buffer (Buffer-menu-buffer t)
523 (read-only-mode 'toggle)
524 buffer-read-only)))
525 (tabulated-list-set-col 1 (if read-only "%" " ") t)))
527 (defun Buffer-menu-bury ()
528 "Bury the buffer listed on this line."
529 (interactive)
530 (let ((buffer (tabulated-list-get-id)))
531 (cond ((null buffer))
532 ((buffer-live-p buffer)
533 (bury-buffer buffer)
534 (save-excursion
535 (let ((elt (tabulated-list-delete-entry)))
536 (goto-char (point-max))
537 (apply 'tabulated-list-print-entry elt)))
538 (message "Buffer buried."))
540 (tabulated-list-delete-entry)
541 (message "Buffer is dead; removing from list.")))))
543 (defun Buffer-menu-view ()
544 "View this line's buffer in View mode."
545 (interactive)
546 (view-buffer (Buffer-menu-buffer t)))
548 (defun Buffer-menu-view-other-window ()
549 "View this line's buffer in View mode in another window."
550 (interactive)
551 (view-buffer-other-window (Buffer-menu-buffer t)))
553 ;;; Functions for populating the Buffer Menu.
555 ;;;###autoload
556 (defun list-buffers-noselect (&optional files-only buffer-list)
557 "Create and return a Buffer Menu buffer.
558 This is called by `buffer-menu' and others as a subroutine.
560 If FILES-ONLY is non-nil, show only file-visiting buffers.
561 If BUFFER-LIST is non-nil, it should be a list of buffers; it
562 means list those buffers and no others."
563 (let ((old-buffer (current-buffer))
564 (buffer (get-buffer-create "*Buffer List*")))
565 (with-current-buffer buffer
566 (Buffer-menu-mode)
567 (setq Buffer-menu-files-only
568 (and files-only (>= (prefix-numeric-value files-only) 0)))
569 (list-buffers--refresh buffer-list old-buffer)
570 (tabulated-list-print))
571 buffer))
573 (defun Buffer-menu-mouse-select (event)
574 "Select the buffer whose line you click on."
575 (interactive "e")
576 (select-window (posn-window (event-end event)))
577 (let ((buffer (tabulated-list-get-id (posn-point (event-end event)))))
578 (when (buffer-live-p buffer)
579 (if (and (window-dedicated-p (selected-window))
580 (eq (selected-window) (frame-root-window)))
581 (switch-to-buffer-other-frame buffer)
582 (switch-to-buffer buffer)))))
584 (defun list-buffers--refresh (&optional buffer-list old-buffer)
585 ;; Set up `tabulated-list-format'.
586 (let ((name-width Buffer-menu-name-width)
587 (size-width Buffer-menu-size-width))
588 ;; Handle obsolete variable:
589 (if Buffer-menu-buffer+size-width
590 (setq name-width (- Buffer-menu-buffer+size-width size-width)))
591 (setq tabulated-list-format
592 (vector '("C" 1 t :pad-right 0)
593 '("R" 1 t :pad-right 0)
594 '("M" 1 t)
595 `("Buffer" ,name-width t)
596 `("Size" ,size-width tabulated-list-entry-size->
597 :right-align t)
598 `("Mode" ,Buffer-menu-mode-width t)
599 '("File" 1 t))))
600 (setq tabulated-list-use-header-line Buffer-menu-use-header-line)
601 ;; Collect info for each buffer we're interested in.
602 (let ((buffer-menu-buffer (current-buffer))
603 (show-non-file (not Buffer-menu-files-only))
604 entries)
605 (dolist (buffer (or buffer-list
606 (buffer-list (if Buffer-menu-use-frame-buffer-list
607 (selected-frame)))))
608 (with-current-buffer buffer
609 (let* ((name (buffer-name))
610 (file buffer-file-name))
611 (when (and (buffer-live-p buffer)
612 (or buffer-list
613 (and (not (string= (substring name 0 1) " "))
614 (not (eq buffer buffer-menu-buffer))
615 (or file show-non-file))))
616 (push (list buffer
617 (vector (if (eq buffer old-buffer) "." " ")
618 (if buffer-read-only "%" " ")
619 (if (buffer-modified-p) "*" " ")
620 (Buffer-menu--pretty-name name)
621 (number-to-string (buffer-size))
622 (concat (format-mode-line mode-name nil nil buffer)
623 (if mode-line-process
624 (format-mode-line mode-line-process
625 nil nil buffer)))
626 (Buffer-menu--pretty-file-name file)))
627 entries)))))
628 (setq tabulated-list-entries (nreverse entries)))
629 (tabulated-list-init-header))
631 (defun tabulated-list-entry-size-> (entry1 entry2)
632 (> (string-to-number (aref (cadr entry1) 4))
633 (string-to-number (aref (cadr entry2) 4))))
635 (defun Buffer-menu--pretty-name (name)
636 (propertize name
637 'font-lock-face 'buffer-menu-buffer
638 'mouse-face 'highlight))
640 (defun Buffer-menu--pretty-file-name (file)
641 (cond (file
642 (abbreviate-file-name file))
643 ((and (boundp 'list-buffers-directory)
644 list-buffers-directory)
645 list-buffers-directory)
646 ((eq major-mode 'Info-mode)
647 (Buffer-menu-info-node-description Info-current-file))
648 (t "")))
650 (defun Buffer-menu-info-node-description (file)
651 (cond
652 ((equal file "dir") "*Info Directory*")
653 ((eq file 'apropos) "*Info Apropos*")
654 ((eq file 'history) "*Info History*")
655 ((eq file 'toc) "*Info TOC*")
656 ((not (stringp file)) "") ; Avoid errors
658 (concat "(" (file-name-nondirectory file) ") " Info-current-node))))
660 ;;; buff-menu.el ends here