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