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