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