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