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