Merge from emacs-24; up to 2014-07-17T10:18:19Z!dmantipov@yandex.ru
[emacs.git] / lisp / buff-menu.el
blobf1772b3d20c4e18a91103249cbe04ca0acaf1356
1 ;;; buff-menu.el --- Interface for viewing and manipulating buffers
3 ;; Copyright (C) 1985-1987, 1993-1995, 2000-2014 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 (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 (Buffer-menu--unmark)
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 (Buffer-menu--unmark))
365 (defun Buffer-menu--unmark ()
366 (tabulated-list-set-col 0 " " t)
367 (let ((buf (Buffer-menu-buffer)))
368 (when buf
369 (if (buffer-modified-p buf)
370 (tabulated-list-set-col 2 "*" t)
371 (tabulated-list-set-col 2 " " t)))))
373 (defun Buffer-menu-delete (&optional arg)
374 "Mark the buffer on this Buffer Menu buffer line for deletion.
375 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
376 will delete it.
378 If prefix argument ARG is non-nil, it specifies the number of
379 buffers to delete; a negative ARG means to delete backwards."
380 (interactive "p")
381 (if (or (null arg) (= arg 0))
382 (setq arg 1))
383 (while (> arg 0)
384 (when (Buffer-menu-buffer)
385 (tabulated-list-set-col 0 "D" t))
386 (forward-line 1)
387 (setq arg (1- arg)))
388 (while (< arg 0)
389 (when (Buffer-menu-buffer)
390 (tabulated-list-set-col 0 "D" t))
391 (forward-line -1)
392 (setq arg (1+ arg))))
394 (defun Buffer-menu-delete-backwards (&optional arg)
395 "Mark the buffer on this Buffer Menu line for deletion, and move up.
396 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]'
397 command will delete the marked buffer. Prefix ARG means move
398 that many lines."
399 (interactive "p")
400 (Buffer-menu-delete (- (or arg 1))))
402 (defun Buffer-menu-save ()
403 "Mark the buffer on this Buffer Menu line for saving.
404 A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command
405 will save it."
406 (interactive)
407 (when (Buffer-menu-buffer)
408 (tabulated-list-set-col 2 "S" t)
409 (forward-line 1)))
411 (defun Buffer-menu-not-modified (&optional arg)
412 "Mark the buffer on this line as unmodified (no changes to save).
413 If ARG is non-nil (interactively, with a prefix argument), mark
414 it as modified."
415 (interactive "P")
416 (with-current-buffer (Buffer-menu-buffer t)
417 (set-buffer-modified-p arg))
418 (tabulated-list-set-col 2 (if arg "*" " ") t))
420 (defun Buffer-menu-execute ()
421 "Save and/or delete marked buffers in the Buffer Menu.
422 Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-save]' are saved.
423 Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-delete]' are deleted."
424 (interactive)
425 (save-excursion
426 (Buffer-menu-beginning)
427 (while (not (eobp))
428 (let ((buffer (tabulated-list-get-id))
429 (entry (tabulated-list-get-entry)))
430 (cond ((null entry)
431 (forward-line 1))
432 ((not (buffer-live-p buffer))
433 (tabulated-list-delete-entry))
435 (let ((delete (eq (char-after) ?D)))
436 (when (equal (aref entry 2) "S")
437 (condition-case nil
438 (progn
439 (with-current-buffer buffer
440 (save-buffer))
441 (tabulated-list-set-col 2 " " t))
442 (error (warn "Error saving %s" buffer))))
443 (if delete
444 (unless (eq buffer (current-buffer))
445 (kill-buffer buffer)
446 (tabulated-list-delete-entry))
447 (forward-line 1)))))))))
449 (defun Buffer-menu-select ()
450 "Select this line's buffer; also, display buffers marked with `>'.
451 You can mark buffers with the \\<Buffer-menu-mode-map>`\\[Buffer-menu-mark]' command.
452 This command deletes and replaces all the previously existing windows
453 in the selected frame."
454 (interactive)
455 (let* ((this-buffer (Buffer-menu-buffer t))
456 (menu-buffer (current-buffer))
457 (others (delq this-buffer (Buffer-menu-marked-buffers t)))
458 (height (/ (1- (frame-height)) (1+ (length others)))))
459 (delete-other-windows)
460 (switch-to-buffer this-buffer)
461 (unless (eq menu-buffer this-buffer)
462 (bury-buffer menu-buffer))
463 (dolist (buffer others)
464 (split-window nil height)
465 (other-window 1)
466 (switch-to-buffer buffer))
467 ;; Back to the beginning!
468 (other-window 1)))
470 (defun Buffer-menu-marked-buffers (&optional unmark)
471 "Return the list of buffers marked with `Buffer-menu-mark'.
472 If UNMARK is non-nil, unmark them."
473 (let (buffers)
474 (Buffer-menu-beginning)
475 (while (re-search-forward "^>" nil t)
476 (let ((buffer (Buffer-menu-buffer)))
477 (if (and buffer unmark)
478 (tabulated-list-set-col 0 " " t))
479 (if (buffer-live-p buffer)
480 (push buffer buffers))))
481 (nreverse buffers)))
483 (defun Buffer-menu-isearch-buffers ()
484 "Search for a string through all marked buffers using Isearch."
485 (interactive)
486 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
488 (defun Buffer-menu-isearch-buffers-regexp ()
489 "Search for a regexp through all marked buffers using Isearch."
490 (interactive)
491 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
493 (defun Buffer-menu-multi-occur (regexp &optional nlines)
494 "Show all lines in marked buffers containing a match for a regexp."
495 (interactive (occur-read-primary-args))
496 (multi-occur (Buffer-menu-marked-buffers) regexp nlines))
499 (defun Buffer-menu-visit-tags-table ()
500 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
501 (interactive)
502 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
503 (if file
504 (visit-tags-table file)
505 (error "Specified buffer has no file"))))
507 (defun Buffer-menu-1-window ()
508 "Select this line's buffer, alone, in full frame."
509 (interactive)
510 (switch-to-buffer (Buffer-menu-buffer t))
511 (bury-buffer (other-buffer))
512 (delete-other-windows))
514 (defun Buffer-menu-this-window ()
515 "Select this line's buffer in this window."
516 (interactive)
517 (switch-to-buffer (Buffer-menu-buffer t)))
519 (defun Buffer-menu-other-window ()
520 "Select this line's buffer in other window, leaving buffer menu visible."
521 (interactive)
522 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
524 (defun Buffer-menu-switch-other-window ()
525 "Make the other window select this line's buffer.
526 The current window remains selected."
527 (interactive)
528 (display-buffer (Buffer-menu-buffer t) t))
530 (defun Buffer-menu-2-window ()
531 "Select this line's buffer, with previous buffer in second window."
532 (interactive)
533 (let ((buff (Buffer-menu-buffer t))
534 (menu (current-buffer)))
535 (delete-other-windows)
536 (switch-to-buffer (other-buffer))
537 (switch-to-buffer-other-window buff)
538 (bury-buffer menu)))
540 (defun Buffer-menu-toggle-read-only ()
541 "Toggle read-only status of buffer on this line.
542 This behaves like invoking \\[read-only-mode] in that buffer."
543 (interactive)
544 (let ((read-only
545 (with-current-buffer (Buffer-menu-buffer t)
546 (read-only-mode 'toggle)
547 buffer-read-only)))
548 (tabulated-list-set-col 1 (if read-only "%" " ") t)))
550 (defun Buffer-menu-bury ()
551 "Bury the buffer listed on this line."
552 (interactive)
553 (let ((buffer (tabulated-list-get-id)))
554 (cond ((null buffer))
555 ((buffer-live-p buffer)
556 (bury-buffer buffer)
557 (save-excursion
558 (let ((elt (tabulated-list-delete-entry)))
559 (goto-char (point-max))
560 (apply 'tabulated-list-print-entry elt)))
561 (message "Buffer buried."))
563 (tabulated-list-delete-entry)
564 (message "Buffer is dead; removing from list.")))))
566 (defun Buffer-menu-view ()
567 "View this line's buffer in View mode."
568 (interactive)
569 (view-buffer (Buffer-menu-buffer t)))
571 (defun Buffer-menu-view-other-window ()
572 "View this line's buffer in View mode in another window."
573 (interactive)
574 (view-buffer-other-window (Buffer-menu-buffer t)))
576 ;;; Functions for populating the Buffer Menu.
578 ;;;###autoload
579 (defun list-buffers-noselect (&optional files-only buffer-list)
580 "Create and return a Buffer Menu buffer.
581 This is called by `buffer-menu' and others as a subroutine.
583 If FILES-ONLY is non-nil, show only file-visiting buffers.
584 If BUFFER-LIST is non-nil, it should be a list of buffers; it
585 means list those buffers and no others."
586 (let ((old-buffer (current-buffer))
587 (buffer (get-buffer-create "*Buffer List*")))
588 (with-current-buffer buffer
589 (Buffer-menu-mode)
590 (setq Buffer-menu-files-only
591 (and files-only (>= (prefix-numeric-value files-only) 0)))
592 (list-buffers--refresh buffer-list old-buffer)
593 (tabulated-list-print))
594 buffer))
596 (defun Buffer-menu-mouse-select (event)
597 "Select the buffer whose line you click on."
598 (interactive "e")
599 (select-window (posn-window (event-end event)))
600 (let ((buffer (tabulated-list-get-id (posn-point (event-end event)))))
601 (when (buffer-live-p buffer)
602 (if (and (window-dedicated-p)
603 (eq (selected-window) (frame-root-window)))
604 (switch-to-buffer-other-frame buffer)
605 (switch-to-buffer buffer)))))
607 (defun list-buffers--refresh (&optional buffer-list old-buffer)
608 ;; Set up `tabulated-list-format'.
609 (let ((name-width Buffer-menu-name-width)
610 (size-width Buffer-menu-size-width))
611 ;; Handle obsolete variable:
612 (if Buffer-menu-buffer+size-width
613 (setq name-width (- Buffer-menu-buffer+size-width size-width)))
614 (setq tabulated-list-format
615 (vector '("C" 1 t :pad-right 0)
616 '("R" 1 t :pad-right 0)
617 '("M" 1 t)
618 `("Buffer" ,name-width t)
619 `("Size" ,size-width tabulated-list-entry-size->
620 :right-align t)
621 `("Mode" ,Buffer-menu-mode-width t)
622 '("File" 1 t))))
623 (setq tabulated-list-use-header-line Buffer-menu-use-header-line)
624 ;; Collect info for each buffer we're interested in.
625 (let ((buffer-menu-buffer (current-buffer))
626 (show-non-file (not Buffer-menu-files-only))
627 entries)
628 (dolist (buffer (or buffer-list
629 (buffer-list (if Buffer-menu-use-frame-buffer-list
630 (selected-frame)))))
631 (with-current-buffer buffer
632 (let* ((name (buffer-name))
633 (file buffer-file-name))
634 (when (and (buffer-live-p buffer)
635 (or buffer-list
636 (and (not (string= (substring name 0 1) " "))
637 (not (eq buffer buffer-menu-buffer))
638 (or file show-non-file))))
639 (push (list buffer
640 (vector (if (eq buffer old-buffer) "." " ")
641 (if buffer-read-only "%" " ")
642 (if (buffer-modified-p) "*" " ")
643 (Buffer-menu--pretty-name name)
644 (number-to-string (buffer-size))
645 (concat (format-mode-line mode-name nil nil buffer)
646 (if mode-line-process
647 (format-mode-line mode-line-process
648 nil nil buffer)))
649 (Buffer-menu--pretty-file-name file)))
650 entries)))))
651 (setq tabulated-list-entries (nreverse entries)))
652 (tabulated-list-init-header))
654 (defun tabulated-list-entry-size-> (entry1 entry2)
655 (> (string-to-number (aref (cadr entry1) 4))
656 (string-to-number (aref (cadr entry2) 4))))
658 (defun Buffer-menu--pretty-name (name)
659 (propertize name
660 'font-lock-face 'buffer-menu-buffer
661 'mouse-face 'highlight))
663 (defun Buffer-menu--pretty-file-name (file)
664 (cond (file
665 (abbreviate-file-name file))
666 ((and (boundp 'list-buffers-directory)
667 list-buffers-directory)
668 list-buffers-directory)
669 ((eq major-mode 'Info-mode)
670 (Buffer-menu-info-node-description Info-current-file))
671 (t "")))
673 (defun Buffer-menu-info-node-description (file)
674 (cond
675 ((equal file "dir") "*Info Directory*")
676 ((eq file 'apropos) "*Info Apropos*")
677 ((eq file 'history) "*Info History*")
678 ((eq file 'toc) "*Info TOC*")
679 ((not (stringp file)) "") ; Avoid errors
681 (concat "(" (file-name-nondirectory file) ") " Info-current-node))))
683 ;;; buff-menu.el ends here