Change all the register node names from "RegPos" (etc.) to "Positional Registers...
[emacs.git] / lisp / buff-menu.el
blob95f309e33b93265f551afc72747d0186f05534b0
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))
271 (define-obsolete-variable-alias 'buffer-menu-mode-hook
272 'Buffer-menu-mode-hook "23.1")
274 (defun Buffer-menu-revert-function (_ignore1 _ignore2)
275 (or (eq buffer-undo-list t)
276 (setq buffer-undo-list nil))
277 ;; We can not use save-excursion here. The buffer gets erased.
278 (let ((opoint (point))
279 (eobp (eobp))
280 (ocol (current-column))
281 (oline (progn (move-to-column Buffer-menu-buffer-column)
282 (get-text-property (point) 'buffer)))
283 (prop (point-min))
284 ;; do not make undo records for the reversion.
285 (buffer-undo-list t))
286 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
287 ;; temporarily the current buffer. Make sure that the
288 ;; interactively current buffer is correctly identified with a `.'
289 ;; by `list-buffers-noselect'.
290 (with-current-buffer (window-buffer)
291 (list-buffers-noselect Buffer-menu-files-only Buffer-menu--buffers))
292 (if oline
293 (while (setq prop (next-single-property-change prop 'buffer))
294 (when (eq (get-text-property prop 'buffer) oline)
295 (goto-char prop)
296 (move-to-column ocol)))
297 (goto-char (if eobp (point-max) opoint)))))
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 (revert-buffer))
310 (defun Buffer-menu-buffer (error-if-non-existent-p)
311 "Return buffer described by this line of buffer menu."
312 (let* ((where (+ (line-beginning-position) Buffer-menu-buffer-column))
313 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
314 (buf (and (not (eobp)) (get-text-property where 'buffer))))
315 (if name
316 (or (get-buffer name)
317 (and buf (buffer-name buf) buf)
318 (if error-if-non-existent-p
319 (error "No buffer named `%s'" name)
320 nil))
321 (or (and buf (buffer-name buf) buf)
322 (if error-if-non-existent-p
323 (error "No buffer on this line")
324 nil)))))
326 (defun buffer-menu (&optional arg)
327 "Make a menu of buffers so you can save, delete or select them.
328 With argument, show only buffers that are visiting files.
329 Type ? after invocation to get help on commands available.
330 Type q to remove the buffer menu from the display.
332 The first column shows `>' for a buffer you have
333 marked to be displayed, `D' for one you have marked for
334 deletion, and `.' for the current buffer.
336 The C column has a `.' for the buffer from which you came.
337 The R column has a `%' if the buffer is read-only.
338 The M column has a `*' if it is modified,
339 or `S' if you have marked it for saving.
340 After this come the buffer name, its size in characters,
341 its major mode, and the visited file name (if any)."
342 (interactive "P")
343 ;;; (setq Buffer-menu-window-config (current-window-configuration))
344 (switch-to-buffer (list-buffers-noselect arg))
345 (message
346 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
348 (defun buffer-menu-other-window (&optional arg)
349 "Display a list of buffers in another window.
350 With the buffer list buffer, you can save, delete or select the buffers.
351 With argument, show only buffers that are visiting files.
352 Type ? after invocation to get help on commands available.
353 Type q to remove the buffer menu from the display.
354 For more information, see the function `buffer-menu'."
355 (interactive "P")
356 ;;; (setq Buffer-menu-window-config (current-window-configuration))
357 (switch-to-buffer-other-window (list-buffers-noselect arg))
358 (message
359 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
361 (defun Buffer-menu-no-header ()
362 (beginning-of-line)
363 (if (or Buffer-menu-use-header-line
364 (not (eq (char-after) ?C)))
366 (ding)
367 (forward-line 1)
368 nil))
370 (defun Buffer-menu-mark ()
371 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
372 (interactive)
373 (when (Buffer-menu-no-header)
374 (let ((inhibit-read-only t))
375 (delete-char 1)
376 (insert ?>)
377 (forward-line 1))))
379 (defun Buffer-menu-unmark (&optional backup)
380 "Cancel all requested operations on buffer on this line and move down.
381 Optional prefix arg means move up."
382 (interactive "P")
383 (when (Buffer-menu-no-header)
384 (let* ((buf (Buffer-menu-buffer t))
385 (mod (buffer-modified-p buf))
386 (readonly (with-current-buffer buf buffer-read-only))
387 (inhibit-read-only t))
388 (delete-char 3)
389 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
390 (forward-line (if backup -1 1)))
392 (defun Buffer-menu-backup-unmark ()
393 "Move up and cancel all requested operations on buffer on line above."
394 (interactive)
395 (forward-line -1)
396 (Buffer-menu-unmark)
397 (forward-line -1))
399 (defun Buffer-menu-delete (&optional arg)
400 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
401 Prefix arg is how many buffers to delete.
402 Negative arg means delete backwards."
403 (interactive "p")
404 (when (Buffer-menu-no-header)
405 (let ((inhibit-read-only t))
406 (if (or (null arg) (= arg 0))
407 (setq arg 1))
408 (while (> arg 0)
409 (delete-char 1)
410 (insert ?D)
411 (forward-line 1)
412 (setq arg (1- arg)))
413 (while (and (< arg 0)
414 (Buffer-menu-no-header))
415 (delete-char 1)
416 (insert ?D)
417 (forward-line -1)
418 (setq arg (1+ arg))))))
420 (defun Buffer-menu-delete-backwards (&optional arg)
421 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
422 and then move up one line. Prefix arg means move that many lines."
423 (interactive "p")
424 (Buffer-menu-delete (- (or arg 1))))
426 (defun Buffer-menu-save ()
427 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
428 (interactive)
429 (when (Buffer-menu-no-header)
430 (let ((inhibit-read-only t))
431 (forward-char 2)
432 (delete-char 1)
433 (insert ?S)
434 (forward-line 1))))
436 (defun Buffer-menu-not-modified (&optional arg)
437 "Mark buffer on this line as unmodified (no changes to save)."
438 (interactive "P")
439 (with-current-buffer (Buffer-menu-buffer t)
440 (set-buffer-modified-p arg))
441 (save-excursion
442 (beginning-of-line)
443 (forward-char 2)
444 (if (= (char-after) (if arg ?\s ?*))
445 (let ((inhibit-read-only t))
446 (delete-char 1)
447 (insert (if arg ?* ?\s))))))
449 (defun Buffer-menu-beginning ()
450 (goto-char (point-min))
451 (unless Buffer-menu-use-header-line
452 (forward-line)))
454 (defun Buffer-menu-execute ()
455 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
456 (interactive)
457 (save-excursion
458 (Buffer-menu-beginning)
459 (while (re-search-forward "^..S" nil t)
460 (let ((modp nil))
461 (with-current-buffer (Buffer-menu-buffer t)
462 (save-buffer)
463 (setq modp (buffer-modified-p)))
464 (let ((inhibit-read-only t))
465 (delete-char -1)
466 (insert (if modp ?* ?\s))))))
467 (save-excursion
468 (Buffer-menu-beginning)
469 (let ((buff-menu-buffer (current-buffer))
470 (inhibit-read-only t))
471 (while (re-search-forward "^D" nil t)
472 (forward-char -1)
473 (let ((buf (Buffer-menu-buffer nil)))
474 (or (eq buf nil)
475 (eq buf buff-menu-buffer)
476 (save-excursion (kill-buffer buf)))
477 (if (and buf (buffer-name buf))
478 (progn (delete-char 1)
479 (insert ?\s))
480 (delete-region (point) (progn (forward-line 1) (point)))
481 (unless (bobp)
482 (forward-char -1))))))))
484 (defun Buffer-menu-select ()
485 "Select this line's buffer; also display buffers marked with `>'.
486 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
487 This command deletes and replaces all the previously existing windows
488 in the selected frame."
489 (interactive)
490 (let ((buff (Buffer-menu-buffer t))
491 (menu (current-buffer))
492 (others ())
493 tem)
494 (Buffer-menu-beginning)
495 (while (re-search-forward "^>" nil t)
496 (setq tem (Buffer-menu-buffer t))
497 (let ((inhibit-read-only t))
498 (delete-char -1)
499 (insert ?\s))
500 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
501 (setq others (nreverse others)
502 tem (/ (1- (frame-height)) (1+ (length others))))
503 (delete-other-windows)
504 (switch-to-buffer buff)
505 (or (eq menu buff)
506 (bury-buffer menu))
507 (if (equal (length others) 0)
508 (progn
509 ;;; ;; Restore previous window configuration before displaying
510 ;;; ;; selected buffers.
511 ;;; (if Buffer-menu-window-config
512 ;;; (progn
513 ;;; (set-window-configuration Buffer-menu-window-config)
514 ;;; (setq Buffer-menu-window-config nil)))
515 (switch-to-buffer buff))
516 (while others
517 (split-window nil tem)
518 (other-window 1)
519 (switch-to-buffer (car others))
520 (setq others (cdr others)))
521 (other-window 1) ;back to the beginning!
524 (defun Buffer-menu-marked-buffers ()
525 "Return a list of buffers marked with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
526 (let (buffers)
527 (Buffer-menu-beginning)
528 (while (re-search-forward "^>" nil t)
529 (setq buffers (cons (Buffer-menu-buffer t) buffers)))
530 (nreverse buffers)))
532 (defun Buffer-menu-isearch-buffers ()
533 "Search for a string through all marked buffers using Isearch."
534 (interactive)
535 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
537 (defun Buffer-menu-isearch-buffers-regexp ()
538 "Search for a regexp through all marked buffers using Isearch."
539 (interactive)
540 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
543 (defun Buffer-menu-visit-tags-table ()
544 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
545 (interactive)
546 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
547 (if file
548 (visit-tags-table file)
549 (error "Specified buffer has no file"))))
551 (defun Buffer-menu-1-window ()
552 "Select this line's buffer, alone, in full frame."
553 (interactive)
554 (switch-to-buffer (Buffer-menu-buffer t))
555 (bury-buffer (other-buffer))
556 (delete-other-windows))
558 (defun Buffer-menu-mouse-select (event)
559 "Select the buffer whose line you click on."
560 (interactive "e")
561 (let (buffer)
562 (with-current-buffer (window-buffer (posn-window (event-end event)))
563 (save-excursion
564 (goto-char (posn-point (event-end event)))
565 (setq buffer (Buffer-menu-buffer t))))
566 (select-window (posn-window (event-end event)))
567 (if (and (window-dedicated-p (selected-window))
568 (eq (selected-window) (frame-root-window)))
569 (switch-to-buffer-other-frame buffer)
570 (switch-to-buffer buffer))))
572 (defun Buffer-menu-this-window ()
573 "Select this line's buffer in this window."
574 (interactive)
575 (switch-to-buffer (Buffer-menu-buffer t)))
577 (defun Buffer-menu-other-window ()
578 "Select this line's buffer in other window, leaving buffer menu visible."
579 (interactive)
580 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
582 (defun Buffer-menu-switch-other-window ()
583 "Make the other window select this line's buffer.
584 The current window remains selected."
585 (interactive)
586 (let ((pop-up-windows t)
587 same-window-buffer-names
588 same-window-regexps)
589 (display-buffer (Buffer-menu-buffer 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 (pop-up-windows t)
597 same-window-buffer-names
598 same-window-regexps)
599 (delete-other-windows)
600 (switch-to-buffer (other-buffer))
601 (pop-to-buffer buff)
602 (bury-buffer menu)))
604 (defun Buffer-menu-toggle-read-only ()
605 "Toggle read-only status of buffer on this line, perhaps via version control."
606 (interactive)
607 (let (char)
608 (with-current-buffer (Buffer-menu-buffer t)
609 (toggle-read-only)
610 (setq char (if buffer-read-only ?% ?\s)))
611 (save-excursion
612 (beginning-of-line)
613 (forward-char 1)
614 (if (/= (following-char) char)
615 (let ((inhibit-read-only t))
616 (delete-char 1)
617 (insert char))))))
619 (defun Buffer-menu-bury ()
620 "Bury the buffer listed on this line."
621 (interactive)
622 (when (Buffer-menu-no-header)
623 (save-excursion
624 (beginning-of-line)
625 (bury-buffer (Buffer-menu-buffer t))
626 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
627 (inhibit-read-only t))
628 (delete-region (point) (progn (forward-line -1) (point)))
629 (goto-char (point-max))
630 (insert line))
631 (message "Buried buffer moved to the end"))))
634 (defun Buffer-menu-view ()
635 "View this line's buffer in View mode."
636 (interactive)
637 (view-buffer (Buffer-menu-buffer t)))
640 (defun Buffer-menu-view-other-window ()
641 "View this line's buffer in View mode in another window."
642 (interactive)
643 (view-buffer-other-window (Buffer-menu-buffer t)))
646 ;;;###autoload
647 (define-key ctl-x-map "\C-b" 'list-buffers)
649 ;;;###autoload
650 (defun list-buffers (&optional files-only)
651 "Display a list of names of existing buffers.
652 The list is displayed in a buffer named `*Buffer List*'.
653 Note that buffers with names starting with spaces are omitted.
654 Non-null optional arg FILES-ONLY means mention only file buffers.
656 For more information, see the function `buffer-menu'."
657 (interactive "P")
658 (display-buffer (list-buffers-noselect files-only)))
660 (defconst Buffer-menu-short-ellipsis
661 ;; This file is preloaded, so we can't use char-displayable-p here
662 ;; because we don't know yet what display we're going to connect to.
663 ":" ;; (if (char-displayable-p ?…) "…" ":")
666 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
667 (if (> (+ (string-width name) (string-width size) 2)
668 Buffer-menu-buffer+size-width)
669 (setq name
670 (let ((tail
671 (if (string-match "<[0-9]+>$" name)
672 (match-string 0 name)
673 "")))
674 (concat (truncate-string-to-width
675 name
676 (- Buffer-menu-buffer+size-width
677 (max (string-width size) 3)
678 (string-width tail)
680 Buffer-menu-short-ellipsis
681 tail)))
682 ;; Don't put properties on (buffer-name).
683 (setq name (copy-sequence name)))
684 (add-text-properties 0 (length name) name-props name)
685 (add-text-properties 0 (length size) size-props size)
686 (let ((name+space-width (- Buffer-menu-buffer+size-width
687 (string-width size))))
688 (concat name
689 (propertize (make-string (- name+space-width (string-width name))
690 ?\s)
691 'display `(space :align-to
692 ,(+ Buffer-menu-buffer-column
693 name+space-width)))
694 size)))
696 (defun Buffer-menu-sort (column)
697 "Sort the buffer menu by COLUMN."
698 (interactive "P")
699 (when column
700 (setq column (prefix-numeric-value column))
701 (if (< column 2) (setq column 2))
702 (if (> column 5) (setq column 5)))
703 (setq Buffer-menu-sort-column column)
704 (let ((inhibit-read-only t) l buf m1 m2)
705 (save-excursion
706 (Buffer-menu-beginning)
707 (while (not (eobp))
708 (when (buffer-live-p
709 (setq buf (get-text-property
710 (+ (point)
711 Buffer-menu-buffer-column)
712 'buffer)))
713 (setq m1 (char-after)
714 m1 (if (memq m1 '(?> ?D)) m1)
715 m2 (char-after (+ (point) 2))
716 m2 (if (eq m2 ?S) m2))
717 (if (or m1 m2)
718 (push (list buf m1 m2) l)))
719 (forward-line)))
720 (revert-buffer)
721 (save-excursion
722 (Buffer-menu-beginning)
723 (while (not (eobp))
724 (when (setq buf (assq (get-text-property (+ (point)
725 Buffer-menu-buffer-column)
726 'buffer) l))
727 (setq m1 (cadr buf)
728 m2 (cadr (cdr buf)))
729 (when m1
730 (delete-char 1)
731 (insert m1)
732 (backward-char 1))
733 (when m2
734 (forward-char 2)
735 (delete-char 1)
736 (insert m2)))
737 (forward-line)))))
739 (defun Buffer-menu-sort-by-column (&optional e)
740 "Sort the buffer menu by the column clicked on."
741 (interactive (list last-input-event))
742 (if e (mouse-select-window e))
743 (let* ((pos (event-start e))
744 (obj (posn-object pos))
745 (col (if obj
746 (get-text-property (cdr obj) 'column (car obj))
747 (get-text-property (posn-point pos) 'column))))
748 (Buffer-menu-sort col)))
750 (defvar Buffer-menu-sort-button-map
751 (let ((map (make-sparse-keymap)))
752 ;; This keymap handles both nil and non-nil values for
753 ;; Buffer-menu-use-header-line.
754 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
755 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
756 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
757 (define-key map [follow-link] 'mouse-face)
758 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
759 map)
760 "Local keymap for Buffer menu sort buttons.")
762 (defun Buffer-menu-make-sort-button (name column)
763 (if (equal column Buffer-menu-sort-column) (setq column nil))
764 (propertize name
765 'column column
766 'help-echo (concat
767 (if Buffer-menu-use-header-line
768 "mouse-1, mouse-2: sort by "
769 "mouse-2, RET: sort by ")
770 (if column (downcase name) "visited order"))
771 'mouse-face 'highlight
772 'keymap Buffer-menu-sort-button-map))
774 (defun list-buffers-noselect (&optional files-only buffer-list)
775 "Create and return a buffer with a list of names of existing buffers.
776 The buffer is named `*Buffer List*'.
777 Note that buffers with names starting with spaces are omitted.
778 Non-null optional arg FILES-ONLY means mention only file buffers.
780 If BUFFER-LIST is non-nil, it should be a list of buffers;
781 it means list those buffers and no others.
783 For more information, see the function `buffer-menu'."
784 (let* ((old-buffer (current-buffer))
785 (standard-output standard-output)
786 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
787 (header (concat "CRM "
788 (Buffer-menu-buffer+size
789 (Buffer-menu-make-sort-button "Buffer" 2)
790 (Buffer-menu-make-sort-button "Size" 3))
792 (Buffer-menu-make-sort-button "Mode" 4) mode-end
793 (Buffer-menu-make-sort-button "File" 5) "\n"))
794 list desired-point)
795 (when Buffer-menu-use-header-line
796 (let ((pos 0))
797 ;; Turn whitespace chars in the header into stretch specs so
798 ;; they work regardless of the header-line face.
799 (while (string-match "[ \t\n]+" header pos)
800 (setq pos (match-end 0))
801 (put-text-property (match-beginning 0) pos 'display
802 ;; Assume fixed-size chars in the buffer.
803 (list 'space :align-to pos)
804 header)))
805 ;; Try to better align the one-char headers.
806 (put-text-property 0 3 'face 'fixed-pitch header)
807 ;; Add a "dummy" leading space to align the beginning of the header
808 ;; line with the beginning of the text (rather than with the left
809 ;; scrollbar or the left fringe). --Stef
810 (setq header (concat (propertize " " 'display '(space :align-to 0))
811 header)))
812 (with-current-buffer (get-buffer-create "*Buffer List*")
813 (setq buffer-read-only nil)
814 (erase-buffer)
815 (setq standard-output (current-buffer))
816 (unless Buffer-menu-use-header-line
817 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
818 ;; (i.e. U+002D, HYPHEN-MINUS).
819 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
820 (insert header
821 (apply 'string
822 (mapcar (lambda (c)
823 (if (memq c '(?\n ?\s)) c underline))
824 header)))))
825 ;; Collect info for every buffer we're interested in.
826 (dolist (buffer (or buffer-list
827 (buffer-list
828 (when Buffer-menu-use-frame-buffer-list
829 (selected-frame)))))
830 (with-current-buffer buffer
831 (let ((name (buffer-name))
832 (file buffer-file-name))
833 (unless (and (not buffer-list)
835 ;; Don't mention internal buffers.
836 (and (string= (substring name 0 1) " ") (null file))
837 ;; Maybe don't mention buffers without files.
838 (and files-only (not file))
839 (string= name "*Buffer List*")))
840 ;; Otherwise output info.
841 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
842 (if mode-line-process
843 (format-mode-line mode-line-process
844 nil nil buffer))))
845 (bits (string
846 (if (eq buffer old-buffer) ?. ?\s)
847 ;; Handle readonly status. The output buffer
848 ;; is special cased to appear readonly; it is
849 ;; actually made so at a later date.
850 (if (or (eq buffer standard-output)
851 buffer-read-only)
852 ?% ?\s)
853 ;; Identify modified buffers.
854 (if (buffer-modified-p) ?* ?\s)
855 ;; Space separator.
856 ?\s)))
857 (unless file
858 ;; No visited file. Check local value of
859 ;; list-buffers-directory and, for Info buffers,
860 ;; Info-current-file.
861 (cond ((and (boundp 'list-buffers-directory)
862 list-buffers-directory)
863 (setq file list-buffers-directory))
864 ((eq major-mode 'Info-mode)
865 (setq file Info-current-file)
866 (cond
867 ((equal file "dir")
868 (setq file "*Info Directory*"))
869 ((eq file 'apropos)
870 (setq file "*Info Apropos*"))
871 ((eq file 'history)
872 (setq file "*Info History*"))
873 ((eq file 'toc)
874 (setq file "*Info TOC*"))
875 ((not (stringp file)) ;; avoid errors
876 (setq file nil))
878 (setq file (concat "("
879 (file-name-nondirectory file)
880 ") "
881 Info-current-node)))))))
882 (push (list buffer bits name (buffer-size) mode file)
883 list))))))
884 ;; Preserve the original buffer-list ordering, just in case.
885 (setq list (nreverse list))
886 ;; Place the buffers's info in the output buffer, sorted if necessary.
887 (dolist (buffer
888 (if Buffer-menu-sort-column
889 (sort list
890 (if (eq Buffer-menu-sort-column 3)
891 (lambda (a b)
892 (< (nth Buffer-menu-sort-column a)
893 (nth Buffer-menu-sort-column b)))
894 (lambda (a b)
895 (string< (nth Buffer-menu-sort-column a)
896 (nth Buffer-menu-sort-column b)))))
897 list))
898 (if (eq (car buffer) old-buffer)
899 (setq desired-point (point)))
900 (insert (cadr buffer)
901 ;; Put the buffer name into a text property
902 ;; so we don't have to extract it from the text.
903 ;; This way we avoid problems with unusual buffer names.
904 (let ((name (nth 2 buffer))
905 (size (int-to-string (nth 3 buffer))))
906 (Buffer-menu-buffer+size name size
907 `(buffer-name ,name
908 buffer ,(car buffer)
909 font-lock-face buffer-menu-buffer
910 mouse-face highlight
911 help-echo
912 ,(if (>= (length name)
913 (- Buffer-menu-buffer+size-width
914 (max (length size) 3)
916 name
917 "mouse-2: select this buffer"))))
919 (if (> (string-width (nth 4 buffer)) Buffer-menu-mode-width)
920 (truncate-string-to-width (nth 4 buffer)
921 Buffer-menu-mode-width)
922 (nth 4 buffer)))
923 (when (nth 5 buffer)
924 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
925 Buffer-menu-mode-width 4) 1)
926 (princ (abbreviate-file-name (nth 5 buffer))))
927 (princ "\n"))
928 (Buffer-menu-mode)
929 (when Buffer-menu-use-header-line
930 (setq header-line-format header))
931 ;; DESIRED-POINT doesn't have to be set; it is not when the
932 ;; current buffer is not displayed for some reason.
933 (and desired-point
934 (goto-char desired-point))
935 (setq Buffer-menu-files-only files-only)
936 (setq Buffer-menu--buffers buffer-list)
937 (set-buffer-modified-p nil)
938 (current-buffer))))
940 ;;; buff-menu.el ends here