Merge from trunk
[emacs.git] / lisp / buff-menu.el
blob9072bc400cd85039810d5bdc5358411aeb15eac3
1 ;;; buff-menu.el --- buffer menu main function and support functions -*- coding:utf-8 -*-
3 ;; Copyright (C) 1985-1987, 1993-1995, 2000-2011
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 ;; Edit, delete, or change attributes of all currently active Emacs
28 ;; buffers from a list summarizing their state. A good way to browse
29 ;; any special or scratch buffers you have loaded, since you can't find
30 ;; them by filename. The single entry point is `list-buffers',
31 ;; normally bound to C-x C-b.
33 ;;; Change Log:
35 ;; Buffer-menu-view: New function
36 ;; Buffer-menu-view-other-window: New function
38 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
40 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
42 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
43 ;; current entry and then move to previous one.
45 ;; Based on FSF code dating back to 1985.
47 ;;; Code:
49 ;; Put buffer *Buffer List* into proper mode right away
50 ;; so that from now on even list-buffers is enough to get a buffer menu.
52 (defgroup Buffer-menu nil
53 "Show a menu of all buffers in a buffer."
54 :group 'tools
55 :group 'convenience)
57 (defcustom Buffer-menu-use-header-line t
58 "Non-nil means to use an immovable header-line."
59 :type 'boolean
60 :group 'Buffer-menu)
62 (defface buffer-menu-buffer
63 '((t (:weight bold)))
64 "Face used to highlight buffer names in the buffer menu."
65 :group 'Buffer-menu)
66 (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer)
68 (defcustom Buffer-menu-buffer+size-width 26
69 "How wide to jointly make the buffer name and size columns."
70 :type 'number
71 :group 'Buffer-menu)
73 (defcustom Buffer-menu-mode-width 16
74 "How wide to make the mode name column."
75 :type 'number
76 :group 'Buffer-menu)
78 (defcustom Buffer-menu-use-frame-buffer-list t
79 "If non-nil, the Buffer Menu uses the selected frame's buffer list.
80 Buffers that were never selected in that frame are listed at the end.
81 If the value is nil, the Buffer Menu uses the global buffer list.
82 This variable matters if the Buffer Menu is sorted by visited order,
83 as it is by default."
84 :type 'boolean
85 :group 'Buffer-menu
86 :version "22.1")
88 ;; This should get updated & resorted when you click on a column heading
89 (defvar Buffer-menu-sort-column nil
90 "Which column to sort the menu on.
91 Use 2 to sort by buffer names, or 5 to sort by file names.
92 A nil value means sort by visited order (the default).")
94 (defconst Buffer-menu-buffer-column 4)
96 (defvar Buffer-menu-files-only nil
97 "Non-nil if the current buffer-menu lists only file buffers.
98 This variable determines whether reverting the buffer lists only
99 file buffers. It affects both manual reverting and reverting by
100 Auto Revert Mode.")
101 (make-variable-buffer-local 'Buffer-menu-files-only)
103 (defvar Buffer-menu--buffers nil
104 "If non-nil, list of buffers shown in the current buffer-menu.
105 This variable determines whether reverting the buffer lists only
106 these buffers. It affects both manual reverting and reverting by
107 Auto Revert Mode.")
108 (make-variable-buffer-local 'Buffer-menu--buffers)
110 (defvar Info-current-file) ;; from info.el
111 (defvar Info-current-node) ;; from info.el
113 (defvar Buffer-menu-mode-map
114 (let ((map (make-keymap))
115 (menu-map (make-sparse-keymap)))
116 (suppress-keymap map t)
117 (define-key map "v" 'Buffer-menu-select)
118 (define-key map "2" 'Buffer-menu-2-window)
119 (define-key map "1" 'Buffer-menu-1-window)
120 (define-key map "f" 'Buffer-menu-this-window)
121 (define-key map "e" 'Buffer-menu-this-window)
122 (define-key map "\C-m" 'Buffer-menu-this-window)
123 (define-key map "o" 'Buffer-menu-other-window)
124 (define-key map "\C-o" 'Buffer-menu-switch-other-window)
125 (define-key map "s" 'Buffer-menu-save)
126 (define-key map "d" 'Buffer-menu-delete)
127 (define-key map "k" 'Buffer-menu-delete)
128 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
129 (define-key map "\C-k" 'Buffer-menu-delete)
130 (define-key map "x" 'Buffer-menu-execute)
131 (define-key map " " 'next-line)
132 (define-key map "n" 'next-line)
133 (define-key map "p" 'previous-line)
134 (define-key map "\177" 'Buffer-menu-backup-unmark)
135 (define-key map "~" 'Buffer-menu-not-modified)
136 (define-key map "u" 'Buffer-menu-unmark)
137 (define-key map "m" 'Buffer-menu-mark)
138 (define-key map "t" 'Buffer-menu-visit-tags-table)
139 (define-key map "%" 'Buffer-menu-toggle-read-only)
140 (define-key map "b" 'Buffer-menu-bury)
141 (define-key map "V" 'Buffer-menu-view)
142 (define-key map "T" 'Buffer-menu-toggle-files-only)
143 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
144 (define-key map [follow-link] 'mouse-face)
145 (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers)
146 (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp)
147 (define-key map [menu-bar Buffer-menu-mode] (cons (purecopy "Buffer-Menu") menu-map))
148 (define-key menu-map [quit]
149 `(menu-item ,(purecopy "Quit") quit-restore-window
150 :help ,(purecopy "Remove the buffer menu from the display")))
151 (define-key menu-map [rev]
152 `(menu-item ,(purecopy "Refresh") revert-buffer
153 :help ,(purecopy "Refresh the *Buffer List* buffer contents")))
154 (define-key menu-map [s0] menu-bar-separator)
155 (define-key menu-map [tf]
156 `(menu-item ,(purecopy "Show only file buffers") Buffer-menu-toggle-files-only
157 :button (:toggle . Buffer-menu-files-only)
158 :help ,(purecopy "Toggle whether the current buffer-menu displays only file buffers")))
159 (define-key menu-map [s1] menu-bar-separator)
160 ;; FIXME: The "Select" entries could use better names...
161 (define-key menu-map [sel]
162 `(menu-item ,(purecopy "Select marked") Buffer-menu-select
163 :help ,(purecopy "Select this line's buffer; also display buffers marked with `>'")))
164 (define-key menu-map [bm2]
165 `(menu-item ,(purecopy "Select two") Buffer-menu-2-window
166 :help ,(purecopy "Select this line's buffer, with previous buffer in second window")))
167 (define-key menu-map [bm1]
168 `(menu-item ,(purecopy "Select current") Buffer-menu-1-window
169 :help ,(purecopy "Select this line's buffer, alone, in full frame")))
170 (define-key menu-map [ow]
171 `(menu-item ,(purecopy "Select in other window") Buffer-menu-other-window
172 :help ,(purecopy "Select this line's buffer in other window, leaving buffer menu visible")))
173 (define-key menu-map [tw]
174 `(menu-item ,(purecopy "Select in current window") Buffer-menu-this-window
175 :help ,(purecopy "Select this line's buffer in this window")))
176 (define-key menu-map [s2] menu-bar-separator)
177 (define-key menu-map [is]
178 `(menu-item ,(purecopy "Regexp Isearch marked buffers") Buffer-menu-isearch-buffers-regexp
179 :help ,(purecopy "Search for a regexp through all marked buffers using Isearch")))
180 (define-key menu-map [ir]
181 `(menu-item ,(purecopy "Isearch marked buffers") Buffer-menu-isearch-buffers
182 :help ,(purecopy "Search for a string through all marked buffers using Isearch")))
183 (define-key menu-map [s3] menu-bar-separator)
184 (define-key menu-map [by]
185 `(menu-item ,(purecopy "Bury") Buffer-menu-bury
186 :help ,(purecopy "Bury the buffer listed on this line")))
187 (define-key menu-map [vt]
188 `(menu-item ,(purecopy "Set unmodified") Buffer-menu-not-modified
189 :help ,(purecopy "Mark buffer on this line as unmodified (no changes to save)")))
190 (define-key menu-map [ex]
191 `(menu-item ,(purecopy "Execute") Buffer-menu-execute
192 :help ,(purecopy "Save and/or delete buffers marked with s or k commands")))
193 (define-key menu-map [s4] menu-bar-separator)
194 (define-key menu-map [delb]
195 `(menu-item ,(purecopy "Mark for delete and move backwards") Buffer-menu-delete-backwards
196 :help ,(purecopy "Mark buffer on this line to be deleted by x command and move up one line")))
197 (define-key menu-map [del]
198 `(menu-item ,(purecopy "Mark for delete") Buffer-menu-delete
199 :help ,(purecopy "Mark buffer on this line to be deleted by x command")))
201 (define-key menu-map [sv]
202 `(menu-item ,(purecopy "Mark for save") Buffer-menu-save
203 :help ,(purecopy "Mark buffer on this line to be saved by x command")))
204 (define-key menu-map [umk]
205 `(menu-item ,(purecopy "Unmark") Buffer-menu-unmark
206 :help ,(purecopy "Cancel all requested operations on buffer on this line and move down")))
207 (define-key menu-map [mk]
208 `(menu-item ,(purecopy "Mark") Buffer-menu-mark
209 :help ,(purecopy "Mark buffer on this line for being displayed by v command")))
210 map)
211 "Local keymap for `Buffer-menu-mode' buffers.")
213 ;; Buffer Menu mode is suitable only for specially formatted data.
214 (put 'Buffer-menu-mode 'mode-class 'special)
216 (define-derived-mode Buffer-menu-mode special-mode "Buffer Menu"
217 "Major mode for editing a list of buffers.
218 Each line describes one of the buffers in Emacs.
219 Letters do not insert themselves; instead, they are commands.
220 \\<Buffer-menu-mode-map>
221 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
222 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
223 \\[Buffer-menu-other-window] -- select that buffer in another window,
224 so the buffer menu buffer remains visible in its window.
225 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
226 \\[Buffer-menu-view-other-window] -- select that buffer in
227 another window, in view-mode.
228 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
229 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
230 \\[Buffer-menu-select] -- select current line's buffer.
231 Also show buffers marked with m, in other windows.
232 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
233 \\[Buffer-menu-2-window] -- select that buffer in one window,
234 together with buffer selected before this one in another window.
235 \\[Buffer-menu-isearch-buffers] -- Do incremental search in the marked buffers.
236 \\[Buffer-menu-isearch-buffers-regexp] -- Isearch for regexp in the marked buffers.
237 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
238 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
239 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
240 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
241 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
242 \\[Buffer-menu-execute] -- delete or save marked buffers.
243 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
244 With prefix argument, also move up one line.
245 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
246 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
247 \\[revert-buffer] -- update the list of buffers.
248 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
249 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
250 (set (make-local-variable 'revert-buffer-function)
251 'Buffer-menu-revert-function)
252 (set (make-local-variable 'buffer-stale-function)
253 (lambda (&optional _noconfirm) 'fast))
254 (setq truncate-lines t)
255 (setq buffer-read-only t))
257 (define-obsolete-variable-alias 'buffer-menu-mode-hook
258 'Buffer-menu-mode-hook "23.1")
260 (defun Buffer-menu-revert-function (_ignore1 _ignore2)
261 (or (eq buffer-undo-list t)
262 (setq buffer-undo-list nil))
263 ;; We can not use save-excursion here. The buffer gets erased.
264 (let ((opoint (point))
265 (eobp (eobp))
266 (ocol (current-column))
267 (oline (progn (move-to-column Buffer-menu-buffer-column)
268 (get-text-property (point) 'buffer)))
269 (prop (point-min))
270 ;; do not make undo records for the reversion.
271 (buffer-undo-list t))
272 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
273 ;; temporarily the current buffer. Make sure that the
274 ;; interactively current buffer is correctly identified with a `.'
275 ;; by `list-buffers-noselect'.
276 (with-current-buffer (window-buffer)
277 (list-buffers-noselect Buffer-menu-files-only Buffer-menu--buffers))
278 (if oline
279 (while (setq prop (next-single-property-change prop 'buffer))
280 (when (eq (get-text-property prop 'buffer) oline)
281 (goto-char prop)
282 (move-to-column ocol)))
283 (goto-char (if eobp (point-max) opoint)))))
285 (defun Buffer-menu-toggle-files-only (arg)
286 "Toggle whether the current buffer-menu displays only file buffers.
287 With a positive ARG display only file buffers. With zero or
288 negative ARG, display other buffers as well."
289 (interactive "P")
290 (setq Buffer-menu-files-only
291 (cond ((not arg) (not Buffer-menu-files-only))
292 ((> (prefix-numeric-value arg) 0) t)))
293 (revert-buffer))
296 (defun Buffer-menu-buffer (error-if-non-existent-p)
297 "Return buffer described by this line of buffer menu."
298 (let* ((where (+ (line-beginning-position) Buffer-menu-buffer-column))
299 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
300 (buf (and (not (eobp)) (get-text-property where 'buffer))))
301 (if name
302 (or (get-buffer name)
303 (and buf (buffer-name buf) buf)
304 (if error-if-non-existent-p
305 (error "No buffer named `%s'" name)
306 nil))
307 (or (and buf (buffer-name buf) buf)
308 (if error-if-non-existent-p
309 (error "No buffer on this line")
310 nil)))))
312 (defun buffer-menu (&optional arg)
313 "Make a menu of buffers so you can save, delete or select them.
314 With argument, show only buffers that are visiting files.
315 Type ? after invocation to get help on commands available.
316 Type q to remove the buffer menu from the display.
318 The first column shows `>' for a buffer you have
319 marked to be displayed, `D' for one you have marked for
320 deletion, and `.' for the current buffer.
322 The C column has a `.' for the buffer from which you came.
323 The R column has a `%' if the buffer is read-only.
324 The M column has a `*' if it is modified,
325 or `S' if you have marked it for saving.
326 After this come the buffer name, its size in characters,
327 its major mode, and the visited file name (if any)."
328 (interactive "P")
329 (pop-to-buffer-same-window
330 (list-buffers-noselect arg) nil 'buffer-menu)
331 (message
332 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
334 (defun buffer-menu-other-window (&optional arg)
335 "Display a list of buffers in another window.
336 With the buffer list buffer, you can save, delete or select the buffers.
337 With argument, show only buffers that are visiting files.
338 Type ? after invocation to get help on commands available.
339 Type q to remove the buffer menu from the display.
340 For more information, see the function `buffer-menu'."
341 (interactive "P")
342 (pop-to-buffer-other-window
343 (list-buffers-noselect arg) nil 'buffer-menu-other-window)
344 (message
345 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
347 (defun Buffer-menu-no-header ()
348 (beginning-of-line)
349 (if (or Buffer-menu-use-header-line
350 (not (eq (char-after) ?C)))
352 (ding)
353 (forward-line 1)
354 nil))
356 (defun Buffer-menu-mark ()
357 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
358 (interactive)
359 (when (Buffer-menu-no-header)
360 (let ((inhibit-read-only t))
361 (delete-char 1)
362 (insert ?>)
363 (forward-line 1))))
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 (when (Buffer-menu-no-header)
370 (let* ((buf (Buffer-menu-buffer t))
371 (mod (buffer-modified-p buf))
372 (readonly (with-current-buffer buf buffer-read-only))
373 (inhibit-read-only t))
374 (delete-char 3)
375 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
376 (forward-line (if backup -1 1)))
378 (defun Buffer-menu-backup-unmark ()
379 "Move up and cancel all requested operations on buffer on line above."
380 (interactive)
381 (forward-line -1)
382 (Buffer-menu-unmark)
383 (forward-line -1))
385 (defun Buffer-menu-delete (&optional arg)
386 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
387 Prefix arg is how many buffers to delete.
388 Negative arg means delete backwards."
389 (interactive "p")
390 (when (Buffer-menu-no-header)
391 (let ((inhibit-read-only t))
392 (if (or (null arg) (= arg 0))
393 (setq arg 1))
394 (while (> arg 0)
395 (delete-char 1)
396 (insert ?D)
397 (forward-line 1)
398 (setq arg (1- arg)))
399 (while (and (< arg 0)
400 (Buffer-menu-no-header))
401 (delete-char 1)
402 (insert ?D)
403 (forward-line -1)
404 (setq arg (1+ arg))))))
406 (defun Buffer-menu-delete-backwards (&optional arg)
407 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
408 and then move up one line. Prefix arg means move that many lines."
409 (interactive "p")
410 (Buffer-menu-delete (- (or arg 1))))
412 (defun Buffer-menu-save ()
413 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
414 (interactive)
415 (when (Buffer-menu-no-header)
416 (let ((inhibit-read-only t))
417 (forward-char 2)
418 (delete-char 1)
419 (insert ?S)
420 (forward-line 1))))
422 (defun Buffer-menu-not-modified (&optional arg)
423 "Mark buffer on this line as unmodified (no changes to save)."
424 (interactive "P")
425 (with-current-buffer (Buffer-menu-buffer t)
426 (set-buffer-modified-p arg))
427 (save-excursion
428 (beginning-of-line)
429 (forward-char 2)
430 (if (= (char-after) (if arg ?\s ?*))
431 (let ((inhibit-read-only t))
432 (delete-char 1)
433 (insert (if arg ?* ?\s))))))
435 (defun Buffer-menu-beginning ()
436 (goto-char (point-min))
437 (unless Buffer-menu-use-header-line
438 (forward-line)))
440 (defun Buffer-menu-execute ()
441 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
442 (interactive)
443 (save-excursion
444 (Buffer-menu-beginning)
445 (while (re-search-forward "^..S" nil t)
446 (let ((modp nil))
447 (with-current-buffer (Buffer-menu-buffer t)
448 (save-buffer)
449 (setq modp (buffer-modified-p)))
450 (let ((inhibit-read-only t))
451 (delete-char -1)
452 (insert (if modp ?* ?\s))))))
453 (save-excursion
454 (Buffer-menu-beginning)
455 (let ((buff-menu-buffer (current-buffer))
456 (inhibit-read-only t))
457 (while (re-search-forward "^D" nil t)
458 (forward-char -1)
459 (let ((buf (Buffer-menu-buffer nil)))
460 (or (eq buf nil)
461 (eq buf buff-menu-buffer)
462 (save-excursion (kill-buffer buf)))
463 (if (and buf (buffer-name buf))
464 (progn (delete-char 1)
465 (insert ?\s))
466 (delete-region (point) (progn (forward-line 1) (point)))
467 (unless (bobp)
468 (forward-char -1))))))))
470 (defun Buffer-menu-select ()
471 "Select this line's buffer; also display buffers marked with `>'.
472 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
473 This command deletes and replaces all the previously existing windows
474 in the selected frame."
475 (interactive)
476 (let ((buff (Buffer-menu-buffer t))
477 (menu (current-buffer))
478 others tem buff-window)
479 (Buffer-menu-beginning)
480 (while (re-search-forward "^>" nil t)
481 (setq tem (Buffer-menu-buffer t))
482 (let ((inhibit-read-only t))
483 (delete-char -1)
484 (insert ?\s))
485 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
486 (setq others (nreverse others))
487 ;; Now start displaying the buffers.
488 (delete-other-windows)
489 (setq buff-window (display-buffer-same-window
490 buff 'Buffer-menu-select-buff))
491 (unless (eq menu buff)
492 (bury-buffer menu))
493 (when others
494 (let ((window-splits t)
495 (window buff-window)
496 other)
497 (while (and window others)
498 (setq other (car others))
499 ;; Try to always split the last window, so we get them in the
500 ;; appropriate order. This will fail when the frame is full,
501 ;; which means that the user has marked too many buffers.
502 (setq window (display-buffer
503 other `((pop-up-window (,window . below)))
504 'Buffer-menu-select-other))
505 (setq others (cdr others)))))))
507 (defun Buffer-menu-marked-buffers ()
508 "Return a list of buffers marked with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
509 (let (buffers)
510 (Buffer-menu-beginning)
511 (while (re-search-forward "^>" nil t)
512 (setq buffers (cons (Buffer-menu-buffer t) buffers)))
513 (nreverse buffers)))
515 (defun Buffer-menu-isearch-buffers ()
516 "Search for a string through all marked buffers using Isearch."
517 (interactive)
518 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
520 (defun Buffer-menu-isearch-buffers-regexp ()
521 "Search for a regexp through all marked buffers using Isearch."
522 (interactive)
523 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
526 (defun Buffer-menu-visit-tags-table ()
527 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
528 (interactive)
529 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
530 (if file
531 (visit-tags-table file)
532 (error "Specified buffer has no file"))))
534 (defun Buffer-menu-1-window ()
535 "Select this line's buffer, alone, in full frame."
536 (interactive)
537 (let ((menu (current-buffer)))
538 (pop-to-buffer-same-window
539 (Buffer-menu-buffer t) nil 'buffer-menu-1-window)
540 (bury-buffer menu)
541 (delete-other-windows)))
543 (defun Buffer-menu-mouse-select (event)
544 "Select the buffer whose line you click on."
545 (interactive "e")
546 (let (buffer)
547 (with-current-buffer (window-buffer (posn-window (event-end event)))
548 (save-excursion
549 (goto-char (posn-point (event-end event)))
550 (setq buffer (Buffer-menu-buffer t))))
551 (select-window (posn-window (event-end event)))
552 (pop-to-buffer-same-window buffer nil 'Buffer-menu-mouse-select)))
554 (defun Buffer-menu-this-window ()
555 "Select this line's buffer in this window."
556 (interactive)
557 (pop-to-buffer-same-window
558 (Buffer-menu-buffer t) nil 'Buffer-menu-this-window))
560 (defun Buffer-menu-other-window ()
561 "Select this line's buffer in other window, leaving buffer menu visible."
562 (interactive)
563 (pop-to-buffer-other-window
564 (Buffer-menu-buffer t) nil 'Buffer-menu-other-window))
566 ;; Name and doc-string of this function are confusing.
567 (defun Buffer-menu-switch-other-window ()
568 "Make the other window select this line's buffer.
569 The current window remains selected."
570 (interactive)
571 (display-buffer-same-frame-other-window
572 ;; Maybe we should insist on reusing another window here.
573 (Buffer-menu-buffer t) 'Buffer-menu-switch-other-window))
575 (defun Buffer-menu-2-window ()
576 "Select this line's buffer, with previous buffer in second window."
577 (interactive)
578 (let ((buff (Buffer-menu-buffer t))
579 (menu (current-buffer)))
580 (delete-other-windows)
581 ;; It's very difficult to understand where we want to display what.
582 (display-buffer-same-window
583 (other-buffer) 'Buffer-menu-2-window-first)
584 (pop-to-buffer-same-frame-other-window
585 buff 'Buffer-menu-2-window-second)
586 (bury-buffer menu)))
588 (defun Buffer-menu-toggle-read-only ()
589 "Toggle read-only status of buffer on this line, perhaps via version control."
590 (interactive)
591 (let (char)
592 (with-current-buffer (Buffer-menu-buffer t)
593 (toggle-read-only)
594 (setq char (if buffer-read-only ?% ?\s)))
595 (save-excursion
596 (beginning-of-line)
597 (forward-char 1)
598 (if (/= (following-char) char)
599 (let ((inhibit-read-only t))
600 (delete-char 1)
601 (insert char))))))
603 (defun Buffer-menu-bury ()
604 "Bury the buffer listed on this line."
605 (interactive)
606 (when (Buffer-menu-no-header)
607 (save-excursion
608 (beginning-of-line)
609 (bury-buffer (Buffer-menu-buffer t))
610 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
611 (inhibit-read-only t))
612 (delete-region (point) (progn (forward-line -1) (point)))
613 (goto-char (point-max))
614 (insert line))
615 (message "Buried buffer moved to the end"))))
618 (defun Buffer-menu-view ()
619 "View this line's buffer in View mode."
620 (interactive)
621 (view-buffer (Buffer-menu-buffer t)))
624 (defun Buffer-menu-view-other-window ()
625 "View this line's buffer in View mode in another window."
626 (interactive)
627 (view-buffer-other-window (Buffer-menu-buffer t)))
630 ;;;###autoload
631 (define-key ctl-x-map "\C-b" 'list-buffers)
633 ;;;###autoload
634 (defun list-buffers (&optional files-only)
635 "Display a list of names of existing buffers.
636 The list is displayed in a buffer named `*Buffer List*'.
637 Note that buffers with names starting with spaces are omitted.
638 Non-null optional arg FILES-ONLY means mention only file buffers.
640 For more information, see the function `buffer-menu'."
641 (interactive "P")
642 (display-buffer (list-buffers-noselect files-only)))
644 (defconst Buffer-menu-short-ellipsis
645 ;; This file is preloaded, so we can't use char-displayable-p here
646 ;; because we don't know yet what display we're going to connect to.
647 ":" ;; (if (char-displayable-p ?…) "…" ":")
650 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
651 (if (> (+ (string-width name) (string-width size) 2)
652 Buffer-menu-buffer+size-width)
653 (setq name
654 (let ((tail
655 (if (string-match "<[0-9]+>$" name)
656 (match-string 0 name)
657 "")))
658 (concat (truncate-string-to-width
659 name
660 (- Buffer-menu-buffer+size-width
661 (max (string-width size) 3)
662 (string-width tail)
664 Buffer-menu-short-ellipsis
665 tail)))
666 ;; Don't put properties on (buffer-name).
667 (setq name (copy-sequence name)))
668 (add-text-properties 0 (length name) name-props name)
669 (add-text-properties 0 (length size) size-props size)
670 (let ((name+space-width (- Buffer-menu-buffer+size-width
671 (string-width size))))
672 (concat name
673 (propertize (make-string (- name+space-width (string-width name))
674 ?\s)
675 'display `(space :align-to
676 ,(+ Buffer-menu-buffer-column
677 name+space-width)))
678 size)))
680 (defun Buffer-menu-sort (column)
681 "Sort the buffer menu by COLUMN."
682 (interactive "P")
683 (when column
684 (setq column (prefix-numeric-value column))
685 (if (< column 2) (setq column 2))
686 (if (> column 5) (setq column 5)))
687 (setq Buffer-menu-sort-column column)
688 (let ((inhibit-read-only t) l buf m1 m2)
689 (save-excursion
690 (Buffer-menu-beginning)
691 (while (not (eobp))
692 (when (buffer-live-p
693 (setq buf (get-text-property
694 (+ (point)
695 Buffer-menu-buffer-column)
696 'buffer)))
697 (setq m1 (char-after)
698 m1 (if (memq m1 '(?> ?D)) m1)
699 m2 (char-after (+ (point) 2))
700 m2 (if (eq m2 ?S) m2))
701 (if (or m1 m2)
702 (push (list buf m1 m2) l)))
703 (forward-line)))
704 (revert-buffer)
705 (save-excursion
706 (Buffer-menu-beginning)
707 (while (not (eobp))
708 (when (setq buf (assq (get-text-property (+ (point)
709 Buffer-menu-buffer-column)
710 'buffer) l))
711 (setq m1 (cadr buf)
712 m2 (cadr (cdr buf)))
713 (when m1
714 (delete-char 1)
715 (insert m1)
716 (backward-char 1))
717 (when m2
718 (forward-char 2)
719 (delete-char 1)
720 (insert m2)))
721 (forward-line)))))
723 (defun Buffer-menu-sort-by-column (&optional e)
724 "Sort the buffer menu by the column clicked on."
725 (interactive (list last-input-event))
726 (if e (mouse-select-window e))
727 (let* ((pos (event-start e))
728 (obj (posn-object pos))
729 (col (if obj
730 (get-text-property (cdr obj) 'column (car obj))
731 (get-text-property (posn-point pos) 'column))))
732 (Buffer-menu-sort col)))
734 (defvar Buffer-menu-sort-button-map
735 (let ((map (make-sparse-keymap)))
736 ;; This keymap handles both nil and non-nil values for
737 ;; Buffer-menu-use-header-line.
738 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
739 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
740 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
741 (define-key map [follow-link] 'mouse-face)
742 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
743 map)
744 "Local keymap for Buffer menu sort buttons.")
746 (defun Buffer-menu-make-sort-button (name column)
747 (if (equal column Buffer-menu-sort-column) (setq column nil))
748 (propertize name
749 'column column
750 'help-echo (concat
751 (if Buffer-menu-use-header-line
752 "mouse-1, mouse-2: sort by "
753 "mouse-2, RET: sort by ")
754 (if column (downcase name) "visited order"))
755 'mouse-face 'highlight
756 'keymap Buffer-menu-sort-button-map))
758 (defun list-buffers-noselect (&optional files-only buffer-list)
759 "Create and return a buffer with a list of names of existing buffers.
760 The buffer is named `*Buffer List*'.
761 Note that buffers with names starting with spaces are omitted.
762 Non-null optional arg FILES-ONLY means mention only file buffers.
764 If BUFFER-LIST is non-nil, it should be a list of buffers;
765 it means list those buffers and no others.
767 For more information, see the function `buffer-menu'."
768 (let* ((old-buffer (current-buffer))
769 (standard-output standard-output)
770 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
771 (header (concat "CRM "
772 (Buffer-menu-buffer+size
773 (Buffer-menu-make-sort-button "Buffer" 2)
774 (Buffer-menu-make-sort-button "Size" 3))
776 (Buffer-menu-make-sort-button "Mode" 4) mode-end
777 (Buffer-menu-make-sort-button "File" 5) "\n"))
778 list desired-point)
779 (when Buffer-menu-use-header-line
780 (let ((pos 0))
781 ;; Turn whitespace chars in the header into stretch specs so
782 ;; they work regardless of the header-line face.
783 (while (string-match "[ \t\n]+" header pos)
784 (setq pos (match-end 0))
785 (put-text-property (match-beginning 0) pos 'display
786 ;; Assume fixed-size chars in the buffer.
787 (list 'space :align-to pos)
788 header)))
789 ;; Try to better align the one-char headers.
790 (put-text-property 0 3 'face 'fixed-pitch header)
791 ;; Add a "dummy" leading space to align the beginning of the header
792 ;; line with the beginning of the text (rather than with the left
793 ;; scrollbar or the left fringe). --Stef
794 (setq header (concat (propertize " " 'display '(space :align-to 0))
795 header)))
796 (with-current-buffer (get-buffer-create "*Buffer List*")
797 (setq buffer-read-only nil)
798 (erase-buffer)
799 (setq standard-output (current-buffer))
800 (unless Buffer-menu-use-header-line
801 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
802 ;; (i.e. U+002D, HYPHEN-MINUS).
803 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
804 (insert header
805 (apply 'string
806 (mapcar (lambda (c)
807 (if (memq c '(?\n ?\s)) c underline))
808 header)))))
809 ;; Collect info for every buffer we're interested in.
810 (dolist (buffer (or buffer-list
811 (buffer-list
812 (when Buffer-menu-use-frame-buffer-list
813 (selected-frame)))))
814 (with-current-buffer buffer
815 (let ((name (buffer-name))
816 (file buffer-file-name))
817 (unless (and (not buffer-list)
819 ;; Don't mention internal buffers.
820 (and (string= (substring name 0 1) " ") (null file))
821 ;; Maybe don't mention buffers without files.
822 (and files-only (not file))
823 (string= name "*Buffer List*")))
824 ;; Otherwise output info.
825 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
826 (if mode-line-process
827 (format-mode-line mode-line-process
828 nil nil buffer))))
829 (bits (string
830 (if (eq buffer old-buffer) ?. ?\s)
831 ;; Handle readonly status. The output buffer
832 ;; is special cased to appear readonly; it is
833 ;; actually made so at a later date.
834 (if (or (eq buffer standard-output)
835 buffer-read-only)
836 ?% ?\s)
837 ;; Identify modified buffers.
838 (if (buffer-modified-p) ?* ?\s)
839 ;; Space separator.
840 ?\s)))
841 (unless file
842 ;; No visited file. Check local value of
843 ;; list-buffers-directory and, for Info buffers,
844 ;; Info-current-file.
845 (cond ((and (boundp 'list-buffers-directory)
846 list-buffers-directory)
847 (setq file list-buffers-directory))
848 ((eq major-mode 'Info-mode)
849 (setq file Info-current-file)
850 (cond
851 ((equal file "dir")
852 (setq file "*Info Directory*"))
853 ((eq file 'apropos)
854 (setq file "*Info Apropos*"))
855 ((eq file 'history)
856 (setq file "*Info History*"))
857 ((eq file 'toc)
858 (setq file "*Info TOC*"))
859 ((not (stringp file)) ;; avoid errors
860 (setq file nil))
862 (setq file (concat "("
863 (file-name-nondirectory file)
864 ") "
865 Info-current-node)))))))
866 (push (list buffer bits name (buffer-size) mode file)
867 list))))))
868 ;; Preserve the original buffer-list ordering, just in case.
869 (setq list (nreverse list))
870 ;; Place the buffers's info in the output buffer, sorted if necessary.
871 (dolist (buffer
872 (if Buffer-menu-sort-column
873 (sort list
874 (if (eq Buffer-menu-sort-column 3)
875 (lambda (a b)
876 (< (nth Buffer-menu-sort-column a)
877 (nth Buffer-menu-sort-column b)))
878 (lambda (a b)
879 (string< (nth Buffer-menu-sort-column a)
880 (nth Buffer-menu-sort-column b)))))
881 list))
882 (if (eq (car buffer) old-buffer)
883 (setq desired-point (point)))
884 (insert (cadr buffer)
885 ;; Put the buffer name into a text property
886 ;; so we don't have to extract it from the text.
887 ;; This way we avoid problems with unusual buffer names.
888 (let ((name (nth 2 buffer))
889 (size (int-to-string (nth 3 buffer))))
890 (Buffer-menu-buffer+size name size
891 `(buffer-name ,name
892 buffer ,(car buffer)
893 font-lock-face buffer-menu-buffer
894 mouse-face highlight
895 help-echo
896 ,(if (>= (length name)
897 (- Buffer-menu-buffer+size-width
898 (max (length size) 3)
900 name
901 "mouse-2: select this buffer"))))
903 (if (> (string-width (nth 4 buffer)) Buffer-menu-mode-width)
904 (truncate-string-to-width (nth 4 buffer)
905 Buffer-menu-mode-width)
906 (nth 4 buffer)))
907 (when (nth 5 buffer)
908 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
909 Buffer-menu-mode-width 4) 1)
910 (princ (abbreviate-file-name (nth 5 buffer))))
911 (princ "\n"))
912 (Buffer-menu-mode)
913 (when Buffer-menu-use-header-line
914 (setq header-line-format header))
915 ;; DESIRED-POINT doesn't have to be set; it is not when the
916 ;; current buffer is not displayed for some reason.
917 (and desired-point
918 (goto-char desired-point))
919 (setq Buffer-menu-files-only files-only)
920 (setq Buffer-menu--buffers buffer-list)
921 (set-buffer-modified-p nil)
922 (current-buffer))))
924 ;;; buff-menu.el ends here