proto-stream.el (proto-stream-open-tls): Return nil if we don't get any stream.
[emacs.git] / lisp / buff-menu.el
blob21fdada22c2f82b53c04da9ebf43cbfdf90e9dd3
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,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: convenience
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;;; Commentary:
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.
34 ;;; Change Log:
36 ;; Buffer-menu-view: New function
37 ;; Buffer-menu-view-other-window: New function
39 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
41 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
43 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
44 ;; current entry and then move to previous one.
46 ;; Based on FSF code dating back to 1985.
48 ;;; Code:
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.
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.
61 ;;(defvar Buffer-menu-window-config nil
62 ;; "Window configuration saved from entry to `buffer-menu'.")
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.
67 (defgroup Buffer-menu nil
68 "Show a menu of all buffers in a buffer."
69 :group 'tools
70 :group 'convenience)
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)
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)
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)
88 (defcustom Buffer-menu-mode-width 16
89 "How wide to make the mode name column."
90 :type 'number
91 :group 'Buffer-menu)
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")
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).")
109 (defconst Buffer-menu-buffer-column 4)
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.")
117 (make-variable-buffer-local 'Buffer-menu-files-only)
119 (defvar Info-current-file) ;; from info.el
120 (defvar Info-current-node) ;; from info.el
122 (defvar Buffer-menu-mode-map
123 (let ((map (make-keymap))
124 (menu-map (make-sparse-keymap)))
125 (suppress-keymap map t)
126 (define-key map "v" 'Buffer-menu-select)
127 (define-key map "2" 'Buffer-menu-2-window)
128 (define-key map "1" 'Buffer-menu-1-window)
129 (define-key map "f" 'Buffer-menu-this-window)
130 (define-key map "e" 'Buffer-menu-this-window)
131 (define-key map "\C-m" 'Buffer-menu-this-window)
132 (define-key map "o" 'Buffer-menu-other-window)
133 (define-key map "\C-o" 'Buffer-menu-switch-other-window)
134 (define-key map "s" 'Buffer-menu-save)
135 (define-key map "d" 'Buffer-menu-delete)
136 (define-key map "k" 'Buffer-menu-delete)
137 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
138 (define-key map "\C-k" 'Buffer-menu-delete)
139 (define-key map "x" 'Buffer-menu-execute)
140 (define-key map " " 'next-line)
141 (define-key map "n" 'next-line)
142 (define-key map "p" 'previous-line)
143 (define-key map "\177" 'Buffer-menu-backup-unmark)
144 (define-key map "~" 'Buffer-menu-not-modified)
145 (define-key map "u" 'Buffer-menu-unmark)
146 (define-key map "m" 'Buffer-menu-mark)
147 (define-key map "t" 'Buffer-menu-visit-tags-table)
148 (define-key map "%" 'Buffer-menu-toggle-read-only)
149 (define-key map "b" 'Buffer-menu-bury)
150 (define-key map "V" 'Buffer-menu-view)
151 (define-key map "T" 'Buffer-menu-toggle-files-only)
152 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
153 (define-key map [follow-link] 'mouse-face)
154 (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers)
155 (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp)
156 (define-key map [menu-bar Buffer-menu-mode] (cons (purecopy "Buffer-Menu") menu-map))
157 (define-key menu-map [quit]
158 `(menu-item ,(purecopy "Quit") quit-window
159 :help ,(purecopy "Remove the buffer menu from the display")))
160 (define-key menu-map [rev]
161 `(menu-item ,(purecopy "Refresh") revert-buffer
162 :help ,(purecopy "Refresh the *Buffer List* buffer contents")))
163 (define-key menu-map [s0] menu-bar-separator)
164 (define-key menu-map [tf]
165 `(menu-item ,(purecopy "Show only file buffers") Buffer-menu-toggle-files-only
166 :button (:toggle . Buffer-menu-files-only)
167 :help ,(purecopy "Toggle whether the current buffer-menu displays only file buffers")))
168 (define-key menu-map [s1] menu-bar-separator)
169 ;; FIXME: The "Select" entries could use better names...
170 (define-key menu-map [sel]
171 `(menu-item ,(purecopy "Select marked") Buffer-menu-select
172 :help ,(purecopy "Select this line's buffer; also display buffers marked with `>'")))
173 (define-key menu-map [bm2]
174 `(menu-item ,(purecopy "Select two") Buffer-menu-2-window
175 :help ,(purecopy "Select this line's buffer, with previous buffer in second window")))
176 (define-key menu-map [bm1]
177 `(menu-item ,(purecopy "Select current") Buffer-menu-1-window
178 :help ,(purecopy "Select this line's buffer, alone, in full frame")))
179 (define-key menu-map [ow]
180 `(menu-item ,(purecopy "Select in other window") Buffer-menu-other-window
181 :help ,(purecopy "Select this line's buffer in other window, leaving buffer menu visible")))
182 (define-key menu-map [tw]
183 `(menu-item ,(purecopy "Select in current window") Buffer-menu-this-window
184 :help ,(purecopy "Select this line's buffer in this window")))
185 (define-key menu-map [s2] menu-bar-separator)
186 (define-key menu-map [is]
187 `(menu-item ,(purecopy "Regexp Isearch marked buffers") Buffer-menu-isearch-buffers-regexp
188 :help ,(purecopy "Search for a regexp through all marked buffers using Isearch")))
189 (define-key menu-map [ir]
190 `(menu-item ,(purecopy "Isearch marked buffers") Buffer-menu-isearch-buffers
191 :help ,(purecopy "Search for a string through all marked buffers using Isearch")))
192 (define-key menu-map [s3] menu-bar-separator)
193 (define-key menu-map [by]
194 `(menu-item ,(purecopy "Bury") Buffer-menu-bury
195 :help ,(purecopy "Bury the buffer listed on this line")))
196 (define-key menu-map [vt]
197 `(menu-item ,(purecopy "Set unmodified") Buffer-menu-not-modified
198 :help ,(purecopy "Mark buffer on this line as unmodified (no changes to save)")))
199 (define-key menu-map [ex]
200 `(menu-item ,(purecopy "Execute") Buffer-menu-execute
201 :help ,(purecopy "Save and/or delete buffers marked with s or k commands")))
202 (define-key menu-map [s4] menu-bar-separator)
203 (define-key menu-map [delb]
204 `(menu-item ,(purecopy "Mark for delete and move backwards") Buffer-menu-delete-backwards
205 :help ,(purecopy "Mark buffer on this line to be deleted by x command and move up one line")))
206 (define-key menu-map [del]
207 `(menu-item ,(purecopy "Mark for delete") Buffer-menu-delete
208 :help ,(purecopy "Mark buffer on this line to be deleted by x command")))
210 (define-key menu-map [sv]
211 `(menu-item ,(purecopy "Mark for save") Buffer-menu-save
212 :help ,(purecopy "Mark buffer on this line to be saved by x command")))
213 (define-key menu-map [umk]
214 `(menu-item ,(purecopy "Unmark") Buffer-menu-unmark
215 :help ,(purecopy "Cancel all requested operations on buffer on this line and move down")))
216 (define-key menu-map [mk]
217 `(menu-item ,(purecopy "Mark") Buffer-menu-mark
218 :help ,(purecopy "Mark buffer on this line for being displayed by v command")))
219 map)
220 "Local keymap for `Buffer-menu-mode' buffers.")
222 ;; Buffer Menu mode is suitable only for specially formatted data.
223 (put 'Buffer-menu-mode 'mode-class 'special)
225 (define-derived-mode Buffer-menu-mode special-mode "Buffer Menu"
226 "Major mode for editing a list of buffers.
227 Each line describes one of the buffers in Emacs.
228 Letters do not insert themselves; instead, they are commands.
229 \\<Buffer-menu-mode-map>
230 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
231 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
232 \\[Buffer-menu-other-window] -- select that buffer in another window,
233 so the buffer menu buffer remains visible in its window.
234 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
235 \\[Buffer-menu-view-other-window] -- select that buffer in
236 another window, in view-mode.
237 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
238 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
239 \\[Buffer-menu-select] -- select current line's buffer.
240 Also show buffers marked with m, in other windows.
241 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
242 \\[Buffer-menu-2-window] -- select that buffer in one window,
243 together with buffer selected before this one in another window.
244 \\[Buffer-menu-isearch-buffers] -- Do incremental search in the marked buffers.
245 \\[Buffer-menu-isearch-buffers-regexp] -- Isearch for regexp in the marked buffers.
246 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
247 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
248 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
249 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
250 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
251 \\[Buffer-menu-execute] -- delete or save marked buffers.
252 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
253 With prefix argument, also move up one line.
254 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
255 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
256 \\[revert-buffer] -- update the list of buffers.
257 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
258 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
259 (set (make-local-variable 'revert-buffer-function)
260 'Buffer-menu-revert-function)
261 (set (make-local-variable 'buffer-stale-function)
262 #'(lambda (&optional noconfirm) 'fast))
263 (setq truncate-lines t)
264 (setq buffer-read-only t))
266 (define-obsolete-variable-alias 'buffer-menu-mode-hook
267 'Buffer-menu-mode-hook "23.1")
269 (defun Buffer-menu-revert-function (ignore1 ignore2)
270 (or (eq buffer-undo-list t)
271 (setq buffer-undo-list nil))
272 ;; We can not use save-excursion here. The buffer gets erased.
273 (let ((opoint (point))
274 (eobp (eobp))
275 (ocol (current-column))
276 (oline (progn (move-to-column 4)
277 (get-text-property (point) 'buffer)))
278 (prop (point-min))
279 ;; do not make undo records for the reversion.
280 (buffer-undo-list t))
281 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
282 ;; temporarily the current buffer. Make sure that the
283 ;; interactively current buffer is correctly identified with a `.'
284 ;; by `list-buffers-noselect'.
285 (with-current-buffer (window-buffer)
286 (list-buffers-noselect Buffer-menu-files-only))
287 (if oline
288 (while (setq prop (next-single-property-change prop 'buffer))
289 (when (eq (get-text-property prop 'buffer) oline)
290 (goto-char prop)
291 (move-to-column ocol)))
292 (goto-char (if eobp (point-max) opoint)))))
294 (defun Buffer-menu-toggle-files-only (arg)
295 "Toggle whether the current buffer-menu displays only file buffers.
296 With a positive ARG display only file buffers. With zero or
297 negative ARG, display other buffers as well."
298 (interactive "P")
299 (setq Buffer-menu-files-only
300 (cond ((not arg) (not Buffer-menu-files-only))
301 ((> (prefix-numeric-value arg) 0) t)))
302 (revert-buffer))
305 (defun Buffer-menu-buffer (error-if-non-existent-p)
306 "Return buffer described by this line of buffer menu."
307 (let* ((where (+ (line-beginning-position) 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 ;;; buff-menu.el ends here