File name fix, for AUTHORS.
[emacs.git] / lisp / buff-menu.el
blob3128ddcf1c79e790b2dc2a3919ab3bb733baf211
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 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 (suppress-keymap map t)
123 (define-key map "q" 'quit-window)
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 "?" 'describe-mode)
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 "g" 'Buffer-menu-revert)
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 map)
157 "Local keymap for `Buffer-menu-mode' buffers.")
159 ;; Buffer Menu mode is suitable only for specially formatted data.
160 (put 'Buffer-menu-mode 'mode-class 'special)
162 (define-derived-mode Buffer-menu-mode nil "Buffer Menu"
163 "Major mode for editing a list of buffers.
164 Each line describes one of the buffers in Emacs.
165 Letters do not insert themselves; instead, they are commands.
166 \\<Buffer-menu-mode-map>
167 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
168 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
169 \\[Buffer-menu-other-window] -- select that buffer in another window,
170 so the buffer menu buffer remains visible in its window.
171 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
172 \\[Buffer-menu-view-other-window] -- select that buffer in
173 another window, in view-mode.
174 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
175 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
176 \\[Buffer-menu-select] -- select current line's buffer.
177 Also show buffers marked with m, in other windows.
178 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
179 \\[Buffer-menu-2-window] -- select that buffer in one window,
180 together with buffer selected before this one in another window.
181 \\[Buffer-menu-isearch-buffers] -- Do incremental search in the marked buffers.
182 \\[Buffer-menu-isearch-buffers-regexp] -- Isearch for regexp in the marked buffers.
183 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
184 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
185 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
186 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
187 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
188 \\[Buffer-menu-execute] -- delete or save marked buffers.
189 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
190 With prefix argument, also move up one line.
191 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
192 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
193 \\[Buffer-menu-revert] -- update the list of buffers.
194 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
195 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
196 (set (make-local-variable 'revert-buffer-function)
197 'Buffer-menu-revert-function)
198 (set (make-local-variable 'buffer-stale-function)
199 #'(lambda (&optional noconfirm) 'fast))
200 (setq truncate-lines t)
201 (setq buffer-read-only t))
203 (define-obsolete-variable-alias 'buffer-menu-mode-hook
204 'Buffer-menu-mode-hook "23.1")
206 ;; This function exists so we can make the doc string of Buffer-menu-mode
207 ;; look nice.
208 (defun Buffer-menu-revert ()
209 "Update the list of buffers."
210 (interactive)
211 (revert-buffer))
213 (defun Buffer-menu-revert-function (ignore1 ignore2)
214 (or (eq buffer-undo-list t)
215 (setq buffer-undo-list nil))
216 ;; We can not use save-excursion here. The buffer gets erased.
217 (let ((opoint (point))
218 (eobp (eobp))
219 (ocol (current-column))
220 (oline (progn (move-to-column 4)
221 (get-text-property (point) 'buffer)))
222 (prop (point-min))
223 ;; do not make undo records for the reversion.
224 (buffer-undo-list t))
225 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
226 ;; temporarily the current buffer. Make sure that the
227 ;; interactively current buffer is correctly identified with a `.'
228 ;; by `list-buffers-noselect'.
229 (with-current-buffer (window-buffer)
230 (list-buffers-noselect Buffer-menu-files-only))
231 (if oline
232 (while (setq prop (next-single-property-change prop 'buffer))
233 (when (eq (get-text-property prop 'buffer) oline)
234 (goto-char prop)
235 (move-to-column ocol)))
236 (goto-char (if eobp (point-max) opoint)))))
238 (defun Buffer-menu-toggle-files-only (arg)
239 "Toggle whether the current buffer-menu displays only file buffers.
240 With a positive ARG display only file buffers. With zero or
241 negative ARG, display other buffers as well."
242 (interactive "P")
243 (setq Buffer-menu-files-only
244 (cond ((not arg) (not Buffer-menu-files-only))
245 ((> (prefix-numeric-value arg) 0) t)))
246 (revert-buffer))
249 (defun Buffer-menu-buffer (error-if-non-existent-p)
250 "Return buffer described by this line of buffer menu."
251 (let* ((where (save-excursion
252 (beginning-of-line)
253 (+ (point) Buffer-menu-buffer-column)))
254 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
255 (buf (and (not (eobp)) (get-text-property where 'buffer))))
256 (if name
257 (or (get-buffer name)
258 (and buf (buffer-name buf) buf)
259 (if error-if-non-existent-p
260 (error "No buffer named `%s'" name)
261 nil))
262 (or (and buf (buffer-name buf) buf)
263 (if error-if-non-existent-p
264 (error "No buffer on this line")
265 nil)))))
267 (defun buffer-menu (&optional arg)
268 "Make a menu of buffers so you can save, delete or select them.
269 With argument, show only buffers that are visiting files.
270 Type ? after invocation to get help on commands available.
271 Type q to remove the buffer menu from the display.
273 The first column shows `>' for a buffer you have
274 marked to be displayed, `D' for one you have marked for
275 deletion, and `.' for the current buffer.
277 The C column has a `.' for the buffer from which you came.
278 The R column has a `%' if the buffer is read-only.
279 The M column has a `*' if it is modified,
280 or `S' if you have marked it for saving.
281 After this come the buffer name, its size in characters,
282 its major mode, and the visited file name (if any)."
283 (interactive "P")
284 ;;; (setq Buffer-menu-window-config (current-window-configuration))
285 (switch-to-buffer (list-buffers-noselect arg))
286 (message
287 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
289 (defun buffer-menu-other-window (&optional arg)
290 "Display a list of buffers in another window.
291 With the buffer list buffer, you can save, delete or select the buffers.
292 With argument, show only buffers that are visiting files.
293 Type ? after invocation to get help on commands available.
294 Type q to remove the buffer menu from the display.
295 For more information, see the function `buffer-menu'."
296 (interactive "P")
297 ;;; (setq Buffer-menu-window-config (current-window-configuration))
298 (switch-to-buffer-other-window (list-buffers-noselect arg))
299 (message
300 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
302 (defun Buffer-menu-no-header ()
303 (beginning-of-line)
304 (if (or Buffer-menu-use-header-line
305 (not (eq (char-after) ?C)))
307 (ding)
308 (forward-line 1)
309 nil))
311 (defun Buffer-menu-mark ()
312 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
313 (interactive)
314 (when (Buffer-menu-no-header)
315 (let ((inhibit-read-only t))
316 (delete-char 1)
317 (insert ?>)
318 (forward-line 1))))
320 (defun Buffer-menu-unmark (&optional backup)
321 "Cancel all requested operations on buffer on this line and move down.
322 Optional prefix arg means move up."
323 (interactive "P")
324 (when (Buffer-menu-no-header)
325 (let* ((buf (Buffer-menu-buffer t))
326 (mod (buffer-modified-p buf))
327 (readonly (with-current-buffer buf buffer-read-only))
328 (inhibit-read-only t))
329 (delete-char 3)
330 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
331 (forward-line (if backup -1 1)))
333 (defun Buffer-menu-backup-unmark ()
334 "Move up and cancel all requested operations on buffer on line above."
335 (interactive)
336 (forward-line -1)
337 (Buffer-menu-unmark)
338 (forward-line -1))
340 (defun Buffer-menu-delete (&optional arg)
341 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
342 Prefix arg is how many buffers to delete.
343 Negative arg means delete backwards."
344 (interactive "p")
345 (when (Buffer-menu-no-header)
346 (let ((inhibit-read-only t))
347 (if (or (null arg) (= arg 0))
348 (setq arg 1))
349 (while (> arg 0)
350 (delete-char 1)
351 (insert ?D)
352 (forward-line 1)
353 (setq arg (1- arg)))
354 (while (and (< arg 0)
355 (Buffer-menu-no-header))
356 (delete-char 1)
357 (insert ?D)
358 (forward-line -1)
359 (setq arg (1+ arg))))))
361 (defun Buffer-menu-delete-backwards (&optional arg)
362 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
363 and then move up one line. Prefix arg means move that many lines."
364 (interactive "p")
365 (Buffer-menu-delete (- (or arg 1))))
367 (defun Buffer-menu-save ()
368 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
369 (interactive)
370 (when (Buffer-menu-no-header)
371 (let ((inhibit-read-only t))
372 (forward-char 2)
373 (delete-char 1)
374 (insert ?S)
375 (forward-line 1))))
377 (defun Buffer-menu-not-modified (&optional arg)
378 "Mark buffer on this line as unmodified (no changes to save)."
379 (interactive "P")
380 (with-current-buffer (Buffer-menu-buffer t)
381 (set-buffer-modified-p arg))
382 (save-excursion
383 (beginning-of-line)
384 (forward-char 2)
385 (if (= (char-after) (if arg ?\s ?*))
386 (let ((inhibit-read-only t))
387 (delete-char 1)
388 (insert (if arg ?* ?\s))))))
390 (defun Buffer-menu-beginning ()
391 (goto-char (point-min))
392 (unless Buffer-menu-use-header-line
393 (forward-line)))
395 (defun Buffer-menu-execute ()
396 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
397 (interactive)
398 (save-excursion
399 (Buffer-menu-beginning)
400 (while (re-search-forward "^..S" nil t)
401 (let ((modp nil))
402 (with-current-buffer (Buffer-menu-buffer t)
403 (save-buffer)
404 (setq modp (buffer-modified-p)))
405 (let ((inhibit-read-only t))
406 (delete-char -1)
407 (insert (if modp ?* ?\s))))))
408 (save-excursion
409 (Buffer-menu-beginning)
410 (let ((buff-menu-buffer (current-buffer))
411 (inhibit-read-only t))
412 (while (re-search-forward "^D" nil t)
413 (forward-char -1)
414 (let ((buf (Buffer-menu-buffer nil)))
415 (or (eq buf nil)
416 (eq buf buff-menu-buffer)
417 (save-excursion (kill-buffer buf)))
418 (if (and buf (buffer-name buf))
419 (progn (delete-char 1)
420 (insert ?\s))
421 (delete-region (point) (progn (forward-line 1) (point)))
422 (unless (bobp)
423 (forward-char -1))))))))
425 (defun Buffer-menu-select ()
426 "Select this line's buffer; also display buffers marked with `>'.
427 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
428 This command deletes and replaces all the previously existing windows
429 in the selected frame."
430 (interactive)
431 (let ((buff (Buffer-menu-buffer t))
432 (menu (current-buffer))
433 (others ())
434 tem)
435 (Buffer-menu-beginning)
436 (while (re-search-forward "^>" nil t)
437 (setq tem (Buffer-menu-buffer t))
438 (let ((inhibit-read-only t))
439 (delete-char -1)
440 (insert ?\s))
441 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
442 (setq others (nreverse others)
443 tem (/ (1- (frame-height)) (1+ (length others))))
444 (delete-other-windows)
445 (switch-to-buffer buff)
446 (or (eq menu buff)
447 (bury-buffer menu))
448 (if (equal (length others) 0)
449 (progn
450 ;;; ;; Restore previous window configuration before displaying
451 ;;; ;; selected buffers.
452 ;;; (if Buffer-menu-window-config
453 ;;; (progn
454 ;;; (set-window-configuration Buffer-menu-window-config)
455 ;;; (setq Buffer-menu-window-config nil)))
456 (switch-to-buffer buff))
457 (while others
458 (split-window nil tem)
459 (other-window 1)
460 (switch-to-buffer (car others))
461 (setq others (cdr others)))
462 (other-window 1) ;back to the beginning!
465 (defun Buffer-menu-marked-buffers ()
466 "Return a list of buffers marked with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
467 (let (buffers)
468 (Buffer-menu-beginning)
469 (while (re-search-forward "^>" nil t)
470 (setq buffers (cons (Buffer-menu-buffer t) buffers)))
471 (nreverse buffers)))
473 (defun Buffer-menu-isearch-buffers ()
474 "Search for a string through all marked buffers using Isearch."
475 (interactive)
476 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
478 (defun Buffer-menu-isearch-buffers-regexp ()
479 "Search for a regexp through all marked buffers using Isearch."
480 (interactive)
481 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
484 (defun Buffer-menu-visit-tags-table ()
485 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
486 (interactive)
487 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
488 (if file
489 (visit-tags-table file)
490 (error "Specified buffer has no file"))))
492 (defun Buffer-menu-1-window ()
493 "Select this line's buffer, alone, in full frame."
494 (interactive)
495 (switch-to-buffer (Buffer-menu-buffer t))
496 (bury-buffer (other-buffer))
497 (delete-other-windows))
499 (defun Buffer-menu-mouse-select (event)
500 "Select the buffer whose line you click on."
501 (interactive "e")
502 (let (buffer)
503 (with-current-buffer (window-buffer (posn-window (event-end event)))
504 (save-excursion
505 (goto-char (posn-point (event-end event)))
506 (setq buffer (Buffer-menu-buffer t))))
507 (select-window (posn-window (event-end event)))
508 (if (and (window-dedicated-p (selected-window))
509 (eq (selected-window) (frame-root-window)))
510 (switch-to-buffer-other-frame buffer)
511 (switch-to-buffer buffer))))
513 (defun Buffer-menu-this-window ()
514 "Select this line's buffer in this window."
515 (interactive)
516 (switch-to-buffer (Buffer-menu-buffer t)))
518 (defun Buffer-menu-other-window ()
519 "Select this line's buffer in other window, leaving buffer menu visible."
520 (interactive)
521 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
523 (defun Buffer-menu-switch-other-window ()
524 "Make the other window select this line's buffer.
525 The current window remains selected."
526 (interactive)
527 (let ((pop-up-windows t)
528 same-window-buffer-names
529 same-window-regexps)
530 (display-buffer (Buffer-menu-buffer t))))
532 (defun Buffer-menu-2-window ()
533 "Select this line's buffer, with previous buffer in second window."
534 (interactive)
535 (let ((buff (Buffer-menu-buffer t))
536 (menu (current-buffer))
537 (pop-up-windows t)
538 same-window-buffer-names
539 same-window-regexps)
540 (delete-other-windows)
541 (switch-to-buffer (other-buffer))
542 (pop-to-buffer buff)
543 (bury-buffer menu)))
545 (defun Buffer-menu-toggle-read-only ()
546 "Toggle read-only status of buffer on this line, perhaps via version control."
547 (interactive)
548 (let (char)
549 (with-current-buffer (Buffer-menu-buffer t)
550 (toggle-read-only)
551 (setq char (if buffer-read-only ?% ?\s)))
552 (save-excursion
553 (beginning-of-line)
554 (forward-char 1)
555 (if (/= (following-char) char)
556 (let ((inhibit-read-only t))
557 (delete-char 1)
558 (insert char))))))
560 (defun Buffer-menu-bury ()
561 "Bury the buffer listed on this line."
562 (interactive)
563 (when (Buffer-menu-no-header)
564 (save-excursion
565 (beginning-of-line)
566 (bury-buffer (Buffer-menu-buffer t))
567 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
568 (inhibit-read-only t))
569 (delete-region (point) (progn (forward-line -1) (point)))
570 (goto-char (point-max))
571 (insert line))
572 (message "Buried buffer moved to the end"))))
575 (defun Buffer-menu-view ()
576 "View this line's buffer in View mode."
577 (interactive)
578 (view-buffer (Buffer-menu-buffer t)))
581 (defun Buffer-menu-view-other-window ()
582 "View this line's buffer in View mode in another window."
583 (interactive)
584 (view-buffer-other-window (Buffer-menu-buffer t)))
587 (define-key ctl-x-map "\C-b" 'list-buffers)
589 (defun list-buffers (&optional files-only)
590 "Display a list of names of existing buffers.
591 The list is displayed in a buffer named `*Buffer List*'.
592 Note that buffers with names starting with spaces are omitted.
593 Non-null optional arg FILES-ONLY means mention only file buffers.
595 For more information, see the function `buffer-menu'."
596 (interactive "P")
597 (display-buffer (list-buffers-noselect files-only)))
599 (defconst Buffer-menu-short-ellipsis
600 ;; This file is preloaded, so we can't use char-displayable-p here
601 ;; because we don't know yet what display we're going to connect to.
602 ":" ;; (if (char-displayable-p ?…) "…" ":")
605 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
606 (if (> (+ (string-width name) (string-width size) 2)
607 Buffer-menu-buffer+size-width)
608 (setq name
609 (let ((tail
610 (if (string-match "<[0-9]+>$" name)
611 (match-string 0 name)
612 "")))
613 (concat (truncate-string-to-width
614 name
615 (- Buffer-menu-buffer+size-width
616 (max (string-width size) 3)
617 (string-width tail)
619 Buffer-menu-short-ellipsis
620 tail)))
621 ;; Don't put properties on (buffer-name).
622 (setq name (copy-sequence name)))
623 (add-text-properties 0 (length name) name-props name)
624 (add-text-properties 0 (length size) size-props size)
625 (concat name
626 (make-string (- Buffer-menu-buffer+size-width
627 (string-width name)
628 (string-width size))
629 ?\s)
630 size))
632 (defun Buffer-menu-sort (column)
633 "Sort the buffer menu by COLUMN."
634 (interactive "P")
635 (when column
636 (setq column (prefix-numeric-value column))
637 (if (< column 2) (setq column 2))
638 (if (> column 5) (setq column 5)))
639 (setq Buffer-menu-sort-column column)
640 (let ((inhibit-read-only t) l buf m1 m2)
641 (save-excursion
642 (Buffer-menu-beginning)
643 (while (not (eobp))
644 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
645 (setq m1 (char-after)
646 m1 (if (memq m1 '(?> ?D)) m1)
647 m2 (char-after (+ (point) 2))
648 m2 (if (eq m2 ?S) m2))
649 (if (or m1 m2)
650 (push (list buf m1 m2) l)))
651 (forward-line)))
652 (Buffer-menu-revert)
653 (save-excursion
654 (Buffer-menu-beginning)
655 (while (not (eobp))
656 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
657 (setq m1 (cadr buf)
658 m2 (cadr (cdr buf)))
659 (when m1
660 (delete-char 1)
661 (insert m1)
662 (backward-char 1))
663 (when m2
664 (forward-char 2)
665 (delete-char 1)
666 (insert m2)))
667 (forward-line)))))
669 (defun Buffer-menu-sort-by-column (&optional e)
670 "Sort the buffer menu by the column clicked on."
671 (interactive (list last-input-event))
672 (if e (mouse-select-window e))
673 (let* ((pos (event-start e))
674 (obj (posn-object pos))
675 (col (if obj
676 (get-text-property (cdr obj) 'column (car obj))
677 (get-text-property (posn-point pos) 'column))))
678 (Buffer-menu-sort col)))
680 (defvar Buffer-menu-sort-button-map
681 (let ((map (make-sparse-keymap)))
682 ;; This keymap handles both nil and non-nil values for
683 ;; Buffer-menu-use-header-line.
684 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
685 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
686 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
687 (define-key map [follow-link] 'mouse-face)
688 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
689 map)
690 "Local keymap for Buffer menu sort buttons.")
692 (defun Buffer-menu-make-sort-button (name column)
693 (if (equal column Buffer-menu-sort-column) (setq column nil))
694 (propertize name
695 'column column
696 'help-echo (concat
697 (if Buffer-menu-use-header-line
698 "mouse-1, mouse-2: sort by "
699 "mouse-2, RET: sort by ")
700 (if column (downcase name) "visited order"))
701 'mouse-face 'highlight
702 'keymap Buffer-menu-sort-button-map))
704 (defun list-buffers-noselect (&optional files-only buffer-list)
705 "Create and return a buffer with a list of names of existing buffers.
706 The buffer is named `*Buffer List*'.
707 Note that buffers with names starting with spaces are omitted.
708 Non-null optional arg FILES-ONLY means mention only file buffers.
710 If BUFFER-LIST is non-nil, it should be a list of buffers;
711 it means list those buffers and no others.
713 For more information, see the function `buffer-menu'."
714 (let* ((old-buffer (current-buffer))
715 (standard-output standard-output)
716 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
717 (header (concat "CRM "
718 (Buffer-menu-buffer+size
719 (Buffer-menu-make-sort-button "Buffer" 2)
720 (Buffer-menu-make-sort-button "Size" 3))
722 (Buffer-menu-make-sort-button "Mode" 4) mode-end
723 (Buffer-menu-make-sort-button "File" 5) "\n"))
724 list desired-point)
725 (when Buffer-menu-use-header-line
726 (let ((pos 0))
727 ;; Turn whitespace chars in the header into stretch specs so
728 ;; they work regardless of the header-line face.
729 (while (string-match "[ \t\n]+" header pos)
730 (setq pos (match-end 0))
731 (put-text-property (match-beginning 0) pos 'display
732 ;; Assume fixed-size chars in the buffer.
733 (list 'space :align-to pos)
734 header)))
735 ;; Try to better align the one-char headers.
736 (put-text-property 0 3 'face 'fixed-pitch header)
737 ;; Add a "dummy" leading space to align the beginning of the header
738 ;; line with the beginning of the text (rather than with the left
739 ;; scrollbar or the left fringe). --Stef
740 (setq header (concat (propertize " " 'display '(space :align-to 0))
741 header)))
742 (with-current-buffer (get-buffer-create "*Buffer List*")
743 (setq buffer-read-only nil)
744 (erase-buffer)
745 (setq standard-output (current-buffer))
746 (unless Buffer-menu-use-header-line
747 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
748 ;; (i.e. U+002D, HYPHEN-MINUS).
749 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
750 (insert header
751 (apply 'string
752 (mapcar (lambda (c)
753 (if (memq c '(?\n ?\s)) c underline))
754 header)))))
755 ;; Collect info for every buffer we're interested in.
756 (dolist (buffer (or buffer-list
757 (buffer-list
758 (when Buffer-menu-use-frame-buffer-list
759 (selected-frame)))))
760 (with-current-buffer buffer
761 (let ((name (buffer-name))
762 (file buffer-file-name))
763 (unless (and (not buffer-list)
765 ;; Don't mention internal buffers.
766 (and (string= (substring name 0 1) " ") (null file))
767 ;; Maybe don't mention buffers without files.
768 (and files-only (not file))
769 (string= name "*Buffer List*")))
770 ;; Otherwise output info.
771 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
772 (if mode-line-process
773 (format-mode-line mode-line-process
774 nil nil buffer))))
775 (bits (string
776 (if (eq buffer old-buffer) ?. ?\s)
777 ;; Handle readonly status. The output buffer
778 ;; is special cased to appear readonly; it is
779 ;; actually made so at a later date.
780 (if (or (eq buffer standard-output)
781 buffer-read-only)
782 ?% ?\s)
783 ;; Identify modified buffers.
784 (if (buffer-modified-p) ?* ?\s)
785 ;; Space separator.
786 ?\s)))
787 (unless file
788 ;; No visited file. Check local value of
789 ;; list-buffers-directory and, for Info buffers,
790 ;; Info-current-file.
791 (cond ((and (boundp 'list-buffers-directory)
792 list-buffers-directory)
793 (setq file list-buffers-directory))
794 ((eq major-mode 'Info-mode)
795 (setq file Info-current-file)
796 (cond
797 ((equal file "dir")
798 (setq file "*Info Directory*"))
799 ((eq file 'apropos)
800 (setq file "*Info Apropos*"))
801 ((eq file 'history)
802 (setq file "*Info History*"))
803 ((eq file 'toc)
804 (setq file "*Info TOC*"))
805 ((not (stringp file)) ;; avoid errors
806 (setq file nil))
808 (setq file (concat "("
809 (file-name-nondirectory file)
810 ") "
811 Info-current-node)))))))
812 (push (list buffer bits name (buffer-size) mode file)
813 list))))))
814 ;; Preserve the original buffer-list ordering, just in case.
815 (setq list (nreverse list))
816 ;; Place the buffers's info in the output buffer, sorted if necessary.
817 (dolist (buffer
818 (if Buffer-menu-sort-column
819 (sort list
820 (if (eq Buffer-menu-sort-column 3)
821 (lambda (a b)
822 (< (nth Buffer-menu-sort-column a)
823 (nth Buffer-menu-sort-column b)))
824 (lambda (a b)
825 (string< (nth Buffer-menu-sort-column a)
826 (nth Buffer-menu-sort-column b)))))
827 list))
828 (if (eq (car buffer) old-buffer)
829 (setq desired-point (point)))
830 (insert (cadr buffer)
831 ;; Put the buffer name into a text property
832 ;; so we don't have to extract it from the text.
833 ;; This way we avoid problems with unusual buffer names.
834 (let ((name (nth 2 buffer))
835 (size (int-to-string (nth 3 buffer))))
836 (Buffer-menu-buffer+size name size
837 `(buffer-name ,name
838 buffer ,(car buffer)
839 font-lock-face buffer-menu-buffer
840 mouse-face highlight
841 help-echo
842 ,(if (>= (length name)
843 (- Buffer-menu-buffer+size-width
844 (max (length size) 3)
846 name
847 "mouse-2: select this buffer"))))
849 (if (> (string-width (nth 4 buffer)) Buffer-menu-mode-width)
850 (truncate-string-to-width (nth 4 buffer)
851 Buffer-menu-mode-width)
852 (nth 4 buffer)))
853 (when (nth 5 buffer)
854 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
855 Buffer-menu-mode-width 4) 1)
856 (princ (abbreviate-file-name (nth 5 buffer))))
857 (princ "\n"))
858 (Buffer-menu-mode)
859 (when Buffer-menu-use-header-line
860 (setq header-line-format header))
861 ;; DESIRED-POINT doesn't have to be set; it is not when the
862 ;; current buffer is not displayed for some reason.
863 (and desired-point
864 (goto-char desired-point))
865 (setq Buffer-menu-files-only files-only)
866 (set-buffer-modified-p nil)
867 (current-buffer))))
869 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
870 ;;; buff-menu.el ends here