]> code.delx.au - gnu-emacs/blob - lisp/buff-menu.el
Merge from trunk.
[gnu-emacs] / lisp / buff-menu.el
1 ;;; buff-menu.el --- Interface for viewing and manipulating buffers
2
3 ;; Copyright (C) 1985-1987, 1993-1995, 2000-2013 Free Software
4 ;; Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: convenience
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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.
30
31 ;;; Code:
32
33 (require 'tabulated-list)
34
35 (defgroup Buffer-menu nil
36 "Show a menu of all buffers in a buffer."
37 :group 'tools
38 :group 'convenience)
39
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)
44
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)
50
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'.
54
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")
61
62 (make-obsolete-variable 'Buffer-menu-buffer+size-width
63 "use `Buffer-menu-name-width' and `Buffer-menu-size-width' instead."
64 "24.3")
65
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")
71
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")
77
78 (defcustom Buffer-menu-mode-width 16
79 "Width of mode name column in the Buffer Menu."
80 :type 'number
81 :group 'Buffer-menu)
82
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")
92
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)
98
99 (defvar Info-current-file) ; from info.el
100 (defvar Info-current-node) ; from info.el
101
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
133 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
134 (define-key map [follow-link] 'mouse-face)
135
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"))
189
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.")
201
202 (define-obsolete-variable-alias 'buffer-menu-mode-hook
203 'Buffer-menu-mode-hook "23.1")
204
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],
208 \\[buffer-menu], and \\[buffer-menu-other-window].
209 See `buffer-menu' for a description of its contents.
210
211 In Buffer Menu mode, the following commands are defined:
212 \\<Buffer-menu-mode-map>
213 \\[quit-window] Remove the Buffer Menu from the display.
214 \\[Buffer-menu-this-window] Select current line's buffer in place of the buffer menu.
215 \\[Buffer-menu-other-window] Select that buffer in another window,
216 so the Buffer Menu remains visible in its window.
217 \\[Buffer-menu-view] Select current line's buffer, in View mode.
218 \\[Buffer-menu-view-other-window] Select that buffer in
219 another window, in view-mode.
220 \\[Buffer-menu-switch-other-window] Make another window display that buffer.
221 \\[Buffer-menu-mark] Mark current line's buffer to be displayed.
222 \\[Buffer-menu-select] Select current line's buffer.
223 Also show buffers marked with m, in other windows.
224 \\[Buffer-menu-1-window] Select that buffer in full-frame window.
225 \\[Buffer-menu-2-window] Select that buffer in one window, together with the
226 buffer selected before this one in another window.
227 \\[Buffer-menu-isearch-buffers] Incremental search in the marked buffers.
228 \\[Buffer-menu-isearch-buffers-regexp] Isearch for regexp in the marked buffers.
229 \\[Buffer-menu-visit-tags-table] visit-tags-table this buffer.
230 \\[Buffer-menu-not-modified] Clear modified-flag on that buffer.
231 \\[Buffer-menu-save] Mark that buffer to be saved, and move down.
232 \\[Buffer-menu-delete] Mark that buffer to be deleted, and move down.
233 \\[Buffer-menu-delete-backwards] Mark that buffer to be deleted, and move up.
234 \\[Buffer-menu-execute] Delete or save marked buffers.
235 \\[Buffer-menu-unmark] Remove all marks from current line.
236 With prefix argument, also move up one line.
237 \\[Buffer-menu-backup-unmark] Back up a line and remove marks.
238 \\[Buffer-menu-toggle-read-only] Toggle read-only status of buffer on this line.
239 \\[revert-buffer] Update the list of buffers.
240 \\[Buffer-menu-toggle-files-only] Toggle whether the menu displays only file buffers.
241 \\[Buffer-menu-bury] Bury the buffer listed on this line."
242 (set (make-local-variable 'buffer-stale-function)
243 (lambda (&optional _noconfirm) 'fast))
244 (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t))
245
246 (defun buffer-menu (&optional arg)
247 "Switch to the Buffer Menu.
248 By default, the Buffer Menu lists all buffers except those whose
249 names start with a space (which are for internal use). With
250 prefix argument ARG, show only buffers that are visiting files.
251
252 In the Buffer Menu, the first column (denoted \"C\") shows \".\"
253 for the buffer from which you came, \">\" for buffers you mark to
254 be displayed, and \"D\" for those you mark for deletion.
255
256 The \"R\" column has a \"%\" if the buffer is read-only.
257 The \"M\" column has a \"*\" if it is modified, or \"S\" if you
258 have marked it for saving.
259
260 The remaining columns show the buffer name, the buffer size in
261 characters, its major mode, and the visited file name (if any).
262
263 See `Buffer-menu-mode' for the keybindings available the Buffer
264 Menu."
265 (interactive "P")
266 (switch-to-buffer (list-buffers-noselect arg))
267 (message
268 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
269
270 (defun buffer-menu-other-window (&optional arg)
271 "Display the Buffer Menu in another window.
272 See `buffer-menu' for a description of the Buffer Menu.
273
274 By default, all buffers are listed except those whose names start
275 with a space (which are for internal use). With prefix argument
276 ARG, show only buffers that are visiting files."
277 (interactive "P")
278 (switch-to-buffer-other-window (list-buffers-noselect arg))
279 (message
280 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
281
282 ;;;###autoload
283 (defun list-buffers (&optional arg)
284 "Display a list of existing buffers.
285 The list is displayed in a buffer named \"*Buffer List*\".
286 See `buffer-menu' for a description of the Buffer Menu.
287
288 By default, all buffers are listed except those whose names start
289 with a space (which are for internal use). With prefix argument
290 ARG, show only buffers that are visiting files."
291 (interactive "P")
292 (display-buffer (list-buffers-noselect arg)))
293
294 (defun Buffer-menu-toggle-files-only (arg)
295 "Toggle whether the current buffer-menu displays only file buffers.
296 With a positive ARG, display only file buffers. With zero or
297 negative ARG, display other buffers as well."
298 (interactive "P")
299 (setq Buffer-menu-files-only
300 (cond ((not arg) (not Buffer-menu-files-only))
301 ((> (prefix-numeric-value arg) 0) t)))
302 (message (if Buffer-menu-files-only
303 "Showing only file-visiting buffers."
304 "Showing all non-internal buffers."))
305 (revert-buffer))
306
307 (defalias 'Buffer-menu-sort 'tabulated-list-sort)
308 \f
309
310 (defun Buffer-menu-buffer (&optional error-if-non-existent-p)
311 "Return the buffer described by the current Buffer Menu line.
312 If there is no buffer here, return nil if ERROR-IF-NON-EXISTENT-P
313 is nil or omitted, and signal an error otherwise."
314 (let ((buffer (tabulated-list-get-id)))
315 (cond ((null buffer)
316 (if error-if-non-existent-p
317 (error "No buffer on this line")))
318 ((not (buffer-live-p buffer))
319 (if error-if-non-existent-p
320 (error "This buffer has been killed")))
321 (t buffer))))
322
323 (defun Buffer-menu-no-header ()
324 (beginning-of-line)
325 (if (or Buffer-menu-use-header-line
326 (not (eq (char-after) ?C)))
327 t
328 (ding)
329 (forward-line 1)
330 nil))
331
332 (defun Buffer-menu-beginning ()
333 (goto-char (point-min))
334 (unless Buffer-menu-use-header-line
335 (forward-line)))
336
337 \f
338 ;;; Commands for modifying Buffer Menu entries.
339
340 (defun Buffer-menu-mark ()
341 "Mark the Buffer menu entry at point for later display.
342 It will be displayed by the \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
343 (interactive)
344 (tabulated-list-set-col 0 ">" t)
345 (forward-line))
346
347 (defun Buffer-menu-unmark (&optional backup)
348 "Cancel all requested operations on buffer on this line and move down.
349 Optional prefix arg means move up."
350 (interactive "P")
351 (tabulated-list-set-col 0 " " t)
352 (forward-line (if backup -1 1)))
353
354 (defun Buffer-menu-backup-unmark ()
355 "Move up and cancel all requested operations on buffer on line above."
356 (interactive)
357 (forward-line -1)
358 (tabulated-list-set-col 0 " " t))
359
360 (defun Buffer-menu-delete (&optional arg)
361 "Mark the buffer on this Buffer Menu buffer line for deletion.
362 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
363 will delete it.
364
365 If prefix argument ARG is non-nil, it specifies the number of
366 buffers to delete; a negative ARG means to delete backwards."
367 (interactive "p")
368 (if (or (null arg) (= arg 0))
369 (setq arg 1))
370 (while (> arg 0)
371 (when (Buffer-menu-buffer)
372 (tabulated-list-set-col 0 "D" t))
373 (forward-line 1)
374 (setq arg (1- arg)))
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
381 (defun Buffer-menu-delete-backwards (&optional arg)
382 "Mark the buffer on this Buffer Menu line for deletion, and move up.
383 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]'
384 command will delete the marked buffer. Prefix ARG means move
385 that many lines."
386 (interactive "p")
387 (Buffer-menu-delete (- (or arg 1))))
388
389 (defun Buffer-menu-save ()
390 "Mark the buffer on this Buffer Menu line for saving.
391 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
392 will save it."
393 (interactive)
394 (when (Buffer-menu-buffer)
395 (tabulated-list-set-col 2 "S" t)
396 (forward-line 1)))
397
398 (defun Buffer-menu-not-modified (&optional arg)
399 "Mark the buffer on this line as unmodified (no changes to save).
400 If ARG is non-nil (interactively, with a prefix argument), mark
401 it as modified."
402 (interactive "P")
403 (with-current-buffer (Buffer-menu-buffer t)
404 (set-buffer-modified-p arg))
405 (tabulated-list-set-col 2 (if arg "*" " ") t))
406
407 (defun Buffer-menu-execute ()
408 "Save and/or delete marked buffers in the Buffer Menu.
409 Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-save]' are saved.
410 Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-delete]' are deleted."
411 (interactive)
412 (save-excursion
413 (Buffer-menu-beginning)
414 (while (not (eobp))
415 (let ((buffer (tabulated-list-get-id))
416 (entry (tabulated-list-get-entry)))
417 (cond ((null entry)
418 (forward-line 1))
419 ((not (buffer-live-p buffer))
420 (tabulated-list-delete-entry))
421 (t
422 (let ((delete (eq (char-after) ?D)))
423 (when (equal (aref entry 2) "S")
424 (condition-case nil
425 (progn
426 (with-current-buffer buffer
427 (save-buffer))
428 (tabulated-list-set-col 2 " " t))
429 (error (warn "Error saving %s" buffer))))
430 (if delete
431 (unless (eq buffer (current-buffer))
432 (kill-buffer buffer)
433 (tabulated-list-delete-entry))
434 (forward-line 1)))))))))
435
436 (defun Buffer-menu-select ()
437 "Select this line's buffer; also, display buffers marked with `>'.
438 You can mark buffers with the \\<Buffer-menu-mode-map>`\\[Buffer-menu-mark]' command.
439 This command deletes and replaces all the previously existing windows
440 in the selected frame."
441 (interactive)
442 (let* ((this-buffer (Buffer-menu-buffer t))
443 (menu-buffer (current-buffer))
444 (others (delq this-buffer (Buffer-menu-marked-buffers t)))
445 (height (/ (1- (frame-height)) (1+ (length others)))))
446 (delete-other-windows)
447 (switch-to-buffer this-buffer)
448 (unless (eq menu-buffer this-buffer)
449 (bury-buffer menu-buffer))
450 (dolist (buffer others)
451 (split-window nil height)
452 (other-window 1)
453 (switch-to-buffer buffer))
454 ;; Back to the beginning!
455 (other-window 1)))
456
457 (defun Buffer-menu-marked-buffers (&optional unmark)
458 "Return the list of buffers marked with `Buffer-menu-mark'.
459 If UNMARK is non-nil, unmark them."
460 (let (buffers)
461 (Buffer-menu-beginning)
462 (while (re-search-forward "^>" nil t)
463 (let ((buffer (Buffer-menu-buffer)))
464 (if (and buffer unmark)
465 (tabulated-list-set-col 0 " " t))
466 (if (buffer-live-p buffer)
467 (push buffer buffers))))
468 (nreverse buffers)))
469
470 (defun Buffer-menu-isearch-buffers ()
471 "Search for a string through all marked buffers using Isearch."
472 (interactive)
473 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
474
475 (defun Buffer-menu-isearch-buffers-regexp ()
476 "Search for a regexp through all marked buffers using Isearch."
477 (interactive)
478 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
479
480 \f
481 (defun Buffer-menu-visit-tags-table ()
482 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
483 (interactive)
484 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
485 (if file
486 (visit-tags-table file)
487 (error "Specified buffer has no file"))))
488
489 (defun Buffer-menu-1-window ()
490 "Select this line's buffer, alone, in full frame."
491 (interactive)
492 (switch-to-buffer (Buffer-menu-buffer t))
493 (bury-buffer (other-buffer))
494 (delete-other-windows))
495
496 (defun Buffer-menu-this-window ()
497 "Select this line's buffer in this window."
498 (interactive)
499 (switch-to-buffer (Buffer-menu-buffer t)))
500
501 (defun Buffer-menu-other-window ()
502 "Select this line's buffer in other window, leaving buffer menu visible."
503 (interactive)
504 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
505
506 (defun Buffer-menu-switch-other-window ()
507 "Make the other window select this line's buffer.
508 The current window remains selected."
509 (interactive)
510 (display-buffer (Buffer-menu-buffer t) t))
511
512 (defun Buffer-menu-2-window ()
513 "Select this line's buffer, with previous buffer in second window."
514 (interactive)
515 (let ((buff (Buffer-menu-buffer t))
516 (menu (current-buffer)))
517 (delete-other-windows)
518 (switch-to-buffer (other-buffer))
519 (switch-to-buffer-other-window buff)
520 (bury-buffer menu)))
521
522 (defun Buffer-menu-toggle-read-only ()
523 "Toggle read-only status of buffer on this line.
524 This behaves like invoking \\[toggle-read-only] in that buffer."
525 (interactive)
526 (let ((read-only
527 (with-current-buffer (Buffer-menu-buffer t)
528 (read-only-mode 'toggle)
529 buffer-read-only)))
530 (tabulated-list-set-col 1 (if read-only "%" " ") t)))
531
532 (defun Buffer-menu-bury ()
533 "Bury the buffer listed on this line."
534 (interactive)
535 (let ((buffer (tabulated-list-get-id)))
536 (cond ((null buffer))
537 ((buffer-live-p buffer)
538 (bury-buffer buffer)
539 (save-excursion
540 (let ((elt (tabulated-list-delete-entry)))
541 (goto-char (point-max))
542 (apply 'tabulated-list-print-entry elt)))
543 (message "Buffer buried."))
544 (t
545 (tabulated-list-delete-entry)
546 (message "Buffer is dead; removing from list.")))))
547
548 (defun Buffer-menu-view ()
549 "View this line's buffer in View mode."
550 (interactive)
551 (view-buffer (Buffer-menu-buffer t)))
552
553 (defun Buffer-menu-view-other-window ()
554 "View this line's buffer in View mode in another window."
555 (interactive)
556 (view-buffer-other-window (Buffer-menu-buffer t)))
557 \f
558 ;;; Functions for populating the Buffer Menu.
559
560 ;;;###autoload
561 (defun list-buffers-noselect (&optional files-only buffer-list)
562 "Create and return a Buffer Menu buffer.
563 This is called by `buffer-menu' and others as a subroutine.
564
565 If FILES-ONLY is non-nil, show only file-visiting buffers.
566 If BUFFER-LIST is non-nil, it should be a list of buffers; it
567 means list those buffers and no others."
568 (let ((old-buffer (current-buffer))
569 (buffer (get-buffer-create "*Buffer List*")))
570 (with-current-buffer buffer
571 (Buffer-menu-mode)
572 (setq Buffer-menu-files-only
573 (and files-only (>= (prefix-numeric-value files-only) 0)))
574 (list-buffers--refresh buffer-list old-buffer)
575 (tabulated-list-print))
576 buffer))
577
578 (defun Buffer-menu-mouse-select (event)
579 "Select the buffer whose line you click on."
580 (interactive "e")
581 (select-window (posn-window (event-end event)))
582 (let ((buffer (tabulated-list-get-id (posn-point (event-end event)))))
583 (when (buffer-live-p buffer)
584 (if (and (window-dedicated-p (selected-window))
585 (eq (selected-window) (frame-root-window)))
586 (switch-to-buffer-other-frame buffer)
587 (switch-to-buffer buffer)))))
588
589 (defun list-buffers--refresh (&optional buffer-list old-buffer)
590 ;; Set up `tabulated-list-format'.
591 (let ((name-width Buffer-menu-name-width)
592 (size-width Buffer-menu-size-width))
593 ;; Handle obsolete variable:
594 (if Buffer-menu-buffer+size-width
595 (setq name-width (- Buffer-menu-buffer+size-width size-width)))
596 (setq tabulated-list-format
597 (vector '("C" 1 t :pad-right 0)
598 '("R" 1 t :pad-right 0)
599 '("M" 1 t)
600 `("Buffer" ,name-width t)
601 `("Size" ,size-width tabulated-list-entry-size->
602 :right-align t)
603 `("Mode" ,Buffer-menu-mode-width t)
604 '("File" 1 t))))
605 (setq tabulated-list-use-header-line Buffer-menu-use-header-line)
606 ;; Collect info for each buffer we're interested in.
607 (let ((buffer-menu-buffer (current-buffer))
608 (show-non-file (not Buffer-menu-files-only))
609 entries)
610 (dolist (buffer (or buffer-list
611 (buffer-list (if Buffer-menu-use-frame-buffer-list
612 (selected-frame)))))
613 (with-current-buffer buffer
614 (let* ((name (buffer-name))
615 (file buffer-file-name))
616 (when (and (buffer-live-p buffer)
617 (or buffer-list
618 (and (not (string= (substring name 0 1) " "))
619 (not (eq buffer buffer-menu-buffer))
620 (or file show-non-file))))
621 (push (list buffer
622 (vector (if (eq buffer old-buffer) "." " ")
623 (if buffer-read-only "%" " ")
624 (if (buffer-modified-p) "*" " ")
625 (Buffer-menu--pretty-name name)
626 (number-to-string (buffer-size))
627 (concat (format-mode-line mode-name nil nil buffer)
628 (if mode-line-process
629 (format-mode-line mode-line-process
630 nil nil buffer)))
631 (Buffer-menu--pretty-file-name file)))
632 entries)))))
633 (setq tabulated-list-entries (nreverse entries)))
634 (tabulated-list-init-header))
635
636 (defun tabulated-list-entry-size-> (entry1 entry2)
637 (> (string-to-number (aref (cadr entry1) 4))
638 (string-to-number (aref (cadr entry2) 4))))
639
640 (defun Buffer-menu--pretty-name (name)
641 (propertize name
642 'font-lock-face 'buffer-menu-buffer
643 'mouse-face 'highlight))
644
645 (defun Buffer-menu--pretty-file-name (file)
646 (cond (file
647 (abbreviate-file-name file))
648 ((and (boundp 'list-buffers-directory)
649 list-buffers-directory)
650 list-buffers-directory)
651 ((eq major-mode 'Info-mode)
652 (Buffer-menu-info-node-description Info-current-file))
653 (t "")))
654
655 (defun Buffer-menu-info-node-description (file)
656 (cond
657 ((equal file "dir") "*Info Directory*")
658 ((eq file 'apropos) "*Info Apropos*")
659 ((eq file 'history) "*Info History*")
660 ((eq file 'toc) "*Info TOC*")
661 ((not (stringp file)) "") ; Avoid errors
662 (t
663 (concat "(" (file-name-nondirectory file) ") " Info-current-node))))
664
665 ;;; buff-menu.el ends here