1 ;;; buff-menu.el --- buffer menu main function and support functions
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 2000, 2001, 2002, 2003
4 ;; Free Software Foundation, Inc.
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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
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.
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.
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."
72 (defcustom Buffer-menu-use-header-line t
73 "*Non-nil means to use an immovable header-line."
77 (defface Buffer-menu-buffer-face
79 "Face used to highlight buffer name."
80 :group
'font-lock-highlighting-faces
)
82 (defcustom Buffer-menu-buffer
+size-width
26
83 "*How wide to jointly make the buffer name and size columns."
87 (defcustom Buffer-menu-mode-width
16
88 "*How wide to make the mode name column."
92 ; This should get updated & resorted when you click on a column heading
93 (defvar Buffer-menu-sort-column nil
94 "*2 for sorting by buffer names. 5 for sorting by file names.
95 nil for default sorting by visited order.")
97 (defconst Buffer-menu-buffer-column
4)
99 (defvar Buffer-menu-mode-map nil
"")
101 (if Buffer-menu-mode-map
103 (setq Buffer-menu-mode-map
(make-keymap))
104 (suppress-keymap Buffer-menu-mode-map t
)
105 (define-key Buffer-menu-mode-map
"q" 'quit-window
)
106 (define-key Buffer-menu-mode-map
"v" 'Buffer-menu-select
)
107 (define-key Buffer-menu-mode-map
"2" 'Buffer-menu-2-window
)
108 (define-key Buffer-menu-mode-map
"1" 'Buffer-menu-1-window
)
109 (define-key Buffer-menu-mode-map
"f" 'Buffer-menu-this-window
)
110 (define-key Buffer-menu-mode-map
"e" 'Buffer-menu-this-window
)
111 (define-key Buffer-menu-mode-map
"\C-m" 'Buffer-menu-this-window
)
112 (define-key Buffer-menu-mode-map
"o" 'Buffer-menu-other-window
)
113 (define-key Buffer-menu-mode-map
"\C-o" 'Buffer-menu-switch-other-window
)
114 (define-key Buffer-menu-mode-map
"s" 'Buffer-menu-save
)
115 (define-key Buffer-menu-mode-map
"d" 'Buffer-menu-delete
)
116 (define-key Buffer-menu-mode-map
"k" 'Buffer-menu-delete
)
117 (define-key Buffer-menu-mode-map
"\C-d" 'Buffer-menu-delete-backwards
)
118 (define-key Buffer-menu-mode-map
"\C-k" 'Buffer-menu-delete
)
119 (define-key Buffer-menu-mode-map
"x" 'Buffer-menu-execute
)
120 (define-key Buffer-menu-mode-map
" " 'next-line
)
121 (define-key Buffer-menu-mode-map
"n" 'next-line
)
122 (define-key Buffer-menu-mode-map
"p" 'previous-line
)
123 (define-key Buffer-menu-mode-map
"\177" 'Buffer-menu-backup-unmark
)
124 (define-key Buffer-menu-mode-map
"~" 'Buffer-menu-not-modified
)
125 (define-key Buffer-menu-mode-map
"?" 'describe-mode
)
126 (define-key Buffer-menu-mode-map
"u" 'Buffer-menu-unmark
)
127 (define-key Buffer-menu-mode-map
"m" 'Buffer-menu-mark
)
128 (define-key Buffer-menu-mode-map
"t" 'Buffer-menu-visit-tags-table
)
129 (define-key Buffer-menu-mode-map
"%" 'Buffer-menu-toggle-read-only
)
130 (define-key Buffer-menu-mode-map
"b" 'Buffer-menu-bury
)
131 (define-key Buffer-menu-mode-map
"g" 'Buffer-menu-revert
)
132 (define-key Buffer-menu-mode-map
"V" 'Buffer-menu-view
)
133 (define-key Buffer-menu-mode-map
[mouse-2
] 'Buffer-menu-mouse-select
)
136 ;; Buffer Menu mode is suitable only for specially formatted data.
137 (put 'Buffer-menu-mode
'mode-class
'special
)
139 (defun Buffer-menu-mode ()
140 "Major mode for editing a list of buffers.
141 Each line describes one of the buffers in Emacs.
142 Letters do not insert themselves; instead, they are commands.
143 \\<Buffer-menu-mode-map>
144 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
145 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
146 \\[Buffer-menu-other-window] -- select that buffer in another window,
147 so the buffer menu buffer remains visible in its window.
148 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
149 \\[Buffer-menu-view-other-window] -- select that buffer in
150 another window, in view-mode.
151 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
152 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
153 \\[Buffer-menu-select] -- select current line's buffer.
154 Also show buffers marked with m, in other windows.
155 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
156 \\[Buffer-menu-2-window] -- select that buffer in one window,
157 together with buffer selected before this one in another window.
158 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
159 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
160 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
161 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
162 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
163 \\[Buffer-menu-execute] -- delete or save marked buffers.
164 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
165 With prefix argument, also move up one line.
166 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
167 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
168 \\[Buffer-menu-revert] -- update the list of buffers.
169 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
170 (kill-all-local-variables)
171 (use-local-map Buffer-menu-mode-map
)
172 (setq major-mode
'Buffer-menu-mode
)
173 (setq mode-name
"Buffer Menu")
174 (make-local-variable 'revert-buffer-function
)
175 (setq revert-buffer-function
'Buffer-menu-revert-function
)
176 (setq truncate-lines t
)
177 (setq buffer-read-only t
)
178 (run-hooks 'buffer-menu-mode-hook
))
180 (defun Buffer-menu-revert ()
181 "Update the list of buffers."
185 (defun Buffer-menu-revert-function (ignore1 ignore2
)
188 (defun Buffer-menu-buffer (error-if-non-existent-p)
189 "Return buffer described by this line of buffer menu."
190 (let* ((where (save-excursion
192 (+ (point) Buffer-menu-buffer-column
)))
193 (name (and (not (eobp)) (get-text-property where
'buffer-name
)))
194 (buf (and (not (eobp)) (get-text-property where
'buffer
))))
196 (or (get-buffer name
)
197 (and buf
(buffer-name buf
) buf
)
198 (if error-if-non-existent-p
199 (error "No buffer named `%s'" name
)
201 (or (and buf
(buffer-name buf
) buf
)
202 (if error-if-non-existent-p
203 (error "No buffer on this line")
206 (defun buffer-menu (&optional arg
)
207 "Make a menu of buffers so you can save, delete or select them.
208 With argument, show only buffers that are visiting files.
209 Type ? after invocation to get help on commands available.
210 Type q to remove the buffer menu from the display.
212 The first column shows `>' for a buffer you have
213 marked to be displayed, `D' for one you have marked for
214 deletion, and `.' for the current buffer.
216 The C column has a `.' for the buffer from which you came.
217 The R column has a `%' if the buffer is read-only.
218 The M column has a `*' if it is modified,
219 or `S' if you have marked it for saving.
220 After this come the buffer name, its size in characters,
221 its major mode, and the visited file name (if any)."
223 ;;; (setq Buffer-menu-window-config (current-window-configuration))
224 (switch-to-buffer (list-buffers-noselect arg
))
226 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
228 (defun buffer-menu-other-window (&optional arg
)
229 "Display a list of buffers in another window.
230 With the buffer list buffer, you can save, delete or select the buffers.
231 With argument, show only buffers that are visiting files.
232 Type ? after invocation to get help on commands available.
233 Type q to remove the buffer menu from the display.
234 For more information, see the function `buffer-menu'."
236 ;;; (setq Buffer-menu-window-config (current-window-configuration))
237 (switch-to-buffer-other-window (list-buffers-noselect arg
))
239 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
241 (defun Buffer-menu-no-header ()
243 (if (or Buffer-menu-use-header-line
244 (not (eq (char-after) ?C
)))
250 (defun Buffer-menu-mark ()
251 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
253 (when (Buffer-menu-no-header)
254 (let ((buffer-read-only nil
))
259 (defun Buffer-menu-unmark (&optional backup
)
260 "Cancel all requested operations on buffer on this line and move down.
261 Optional ARG means move up."
263 (when (Buffer-menu-no-header)
264 (let* ((buf (Buffer-menu-buffer t
))
265 (mod (buffer-modified-p buf
))
266 (readonly (save-excursion (set-buffer buf
) buffer-read-only
))
267 (buffer-read-only nil
))
269 (insert (if readonly
(if mod
" %*" " % ") (if mod
" *" " ")))))
270 (forward-line (if backup -
1 1)))
272 (defun Buffer-menu-backup-unmark ()
273 "Move up and cancel all requested operations on buffer on line above."
279 (defun Buffer-menu-delete (&optional arg
)
280 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
281 Prefix arg is how many buffers to delete.
282 Negative arg means delete backwards."
284 (when (Buffer-menu-no-header)
285 (let ((buffer-read-only nil
))
286 (if (or (null arg
) (= arg
0))
293 (while (and (< arg
0)
294 (Buffer-menu-no-header))
298 (setq arg
(1+ arg
))))))
300 (defun Buffer-menu-delete-backwards (&optional arg
)
301 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
302 and then move up one line. Prefix arg means move that many lines."
304 (Buffer-menu-delete (- (or arg
1))))
306 (defun Buffer-menu-save ()
307 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
309 (when (Buffer-menu-no-header)
310 (let ((buffer-read-only nil
))
316 (defun Buffer-menu-not-modified (&optional arg
)
317 "Mark buffer on this line as unmodified (no changes to save)."
320 (set-buffer (Buffer-menu-buffer t
))
321 (set-buffer-modified-p arg
))
325 (if (= (char-after) (if arg ? ?
*))
326 (let ((buffer-read-only nil
))
328 (insert (if arg ?
* ?
))))))
330 (defun Buffer-menu-execute ()
331 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
334 (goto-char (point-min))
335 (unless Buffer-menu-use-header-line
337 (while (re-search-forward "^..S" nil t
)
340 (set-buffer (Buffer-menu-buffer t
))
342 (setq modp
(buffer-modified-p)))
343 (let ((buffer-read-only nil
))
345 (insert (if modp ?
* ?
))))))
347 (goto-char (point-min))
348 (unless Buffer-menu-use-header-line
350 (let ((buff-menu-buffer (current-buffer))
351 (buffer-read-only nil
))
352 (while (re-search-forward "^D" nil t
)
354 (let ((buf (Buffer-menu-buffer nil
)))
356 (eq buf buff-menu-buffer
)
357 (save-excursion (kill-buffer buf
)))
358 (if (and buf
(buffer-name buf
))
359 (progn (delete-char 1)
361 (delete-region (point) (progn (forward-line 1) (point)))
363 (forward-char -
1))))))))
365 (defun Buffer-menu-select ()
366 "Select this line's buffer; also display buffers marked with `>'.
367 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
368 This command deletes and replaces all the previously existing windows
369 in the selected frame."
371 (let ((buff (Buffer-menu-buffer t
))
372 (menu (current-buffer))
375 (goto-char (point-min))
376 (unless Buffer-menu-use-header-line
378 (while (re-search-forward "^>" nil t
)
379 (setq tem
(Buffer-menu-buffer t
))
380 (let ((buffer-read-only nil
))
383 (or (eq tem buff
) (memq tem others
) (setq others
(cons tem others
))))
384 (setq others
(nreverse others
)
385 tem
(/ (1- (frame-height)) (1+ (length others
))))
386 (delete-other-windows)
387 (switch-to-buffer buff
)
390 (if (equal (length others
) 0)
392 ;;; ;; Restore previous window configuration before displaying
393 ;;; ;; selected buffers.
394 ;;; (if Buffer-menu-window-config
396 ;;; (set-window-configuration Buffer-menu-window-config)
397 ;;; (setq Buffer-menu-window-config nil)))
398 (switch-to-buffer buff
))
400 (split-window nil tem
)
402 (switch-to-buffer (car others
))
403 (setq others
(cdr others
)))
404 (other-window 1) ;back to the beginning!
409 (defun Buffer-menu-visit-tags-table ()
410 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
412 (let ((file (buffer-file-name (Buffer-menu-buffer t
))))
414 (visit-tags-table file
)
415 (error "Specified buffer has no file"))))
417 (defun Buffer-menu-1-window ()
418 "Select this line's buffer, alone, in full frame."
420 (switch-to-buffer (Buffer-menu-buffer t
))
421 (bury-buffer (other-buffer))
422 (delete-other-windows))
424 (defun Buffer-menu-mouse-select (event)
425 "Select the buffer whose line you click on."
429 (set-buffer (window-buffer (posn-window (event-end event
))))
431 (goto-char (posn-point (event-end event
)))
432 (setq buffer
(Buffer-menu-buffer t
))))
433 (select-window (posn-window (event-end event
)))
434 (if (and (window-dedicated-p (selected-window))
435 (eq (selected-window) (frame-root-window)))
436 (switch-to-buffer-other-frame buffer
)
437 (switch-to-buffer buffer
))))
439 (defun Buffer-menu-this-window ()
440 "Select this line's buffer in this window."
442 (switch-to-buffer (Buffer-menu-buffer t
)))
444 (defun Buffer-menu-other-window ()
445 "Select this line's buffer in other window, leaving buffer menu visible."
447 (switch-to-buffer-other-window (Buffer-menu-buffer t
)))
449 (defun Buffer-menu-switch-other-window ()
450 "Make the other window select this line's buffer.
451 The current window remains selected."
453 (display-buffer (Buffer-menu-buffer t
)))
455 (defun Buffer-menu-2-window ()
456 "Select this line's buffer, with previous buffer in second window."
458 (let ((buff (Buffer-menu-buffer t
))
459 (menu (current-buffer))
461 (delete-other-windows)
462 (switch-to-buffer (other-buffer))
466 (defun Buffer-menu-toggle-read-only ()
467 "Toggle read-only status of buffer on this line, perhaps via version control."
471 (set-buffer (Buffer-menu-buffer t
))
472 (vc-toggle-read-only)
473 (setq char
(if buffer-read-only ?% ?
)))
477 (if (/= (following-char) char
)
478 (let (buffer-read-only)
482 (defun Buffer-menu-bury ()
483 "Bury the buffer listed on this line."
485 (when (Buffer-menu-no-header)
488 (bury-buffer (Buffer-menu-buffer t
))
489 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
490 (buffer-read-only nil
))
491 (delete-region (point) (progn (forward-line -
1) (point)))
492 (goto-char (point-max))
494 (message "Buried buffer moved to the end"))))
497 (defun Buffer-menu-view ()
498 "View this line's buffer in View mode."
500 (view-buffer (Buffer-menu-buffer t
)))
503 (defun Buffer-menu-view-other-window ()
504 "View this line's buffer in View mode in another window."
506 (view-buffer-other-window (Buffer-menu-buffer t
)))
509 (define-key ctl-x-map
"\C-b" 'list-buffers
)
511 (defun list-buffers (&optional files-only
)
512 "Display a list of names of existing buffers.
513 The list is displayed in a buffer named `*Buffer List*'.
514 Note that buffers with names starting with spaces are omitted.
515 Non-null optional arg FILES-ONLY means mention only file buffers.
517 For more information, see the function `buffer-menu'."
519 (display-buffer (list-buffers-noselect files-only
)))
521 (defun Buffer-menu-buffer+size
(name size
&optional name-props size-props
)
522 (if (> (+ (length name
) (length size
) 2) Buffer-menu-buffer
+size-width
)
524 (if (string-match "<[0-9]+>$" name
)
525 (concat (substring name
0
526 (- Buffer-menu-buffer
+size-width
527 (max (length size
) 3)
529 (- (match-beginning 0))
531 ":" ; narrow ellipsis
532 (match-string 0 name
))
533 (concat (substring name
0
534 (- Buffer-menu-buffer
+size-width
535 (max (length size
) 3)
537 ":"))) ; narrow ellipsis
538 ;; Don't put properties on (buffer-name).
539 (setq name
(copy-sequence name
)))
540 (add-text-properties 0 (length name
) name-props name
)
541 (add-text-properties 0 (length size
) size-props size
)
543 (make-string (- Buffer-menu-buffer
+size-width
549 (defun list-buffers-noselect (&optional files-only
)
550 "Create and return a buffer with a list of names of existing buffers.
551 The buffer is named `*Buffer List*'.
552 Note that buffers with names starting with spaces are omitted.
553 Non-null optional arg FILES-ONLY means mention only file buffers.
555 For more information, see the function `buffer-menu'."
556 (let* ((old-buffer (current-buffer))
557 (standard-output standard-output
)
558 (mode-end (make-string (- Buffer-menu-mode-width
2) ?
))
559 ;; Fixme: This is wrong for left-hand scroll-bars.
560 (header (concat "CRM " (Buffer-menu-buffer+size
"Buffer" "Size")
561 " Mode" mode-end
"File\n"))
562 list desired-point name file mode
)
564 (set-buffer (get-buffer-create "*Buffer List*"))
565 (setq buffer-read-only nil
)
567 (setq standard-output
(current-buffer))
568 (unless Buffer-menu-use-header-line
569 (insert header
"--- ------")
570 (indent-to Buffer-menu-buffer
+size-width
)
571 (insert "---- ----" mode-end
"----\n")
572 (put-text-property 1 (point) 'intangible t
))
577 (with-current-buffer buffer
578 (setq name
(buffer-name)
579 file
(buffer-file-name))
581 ;; Don't mention internal buffers.
582 ((and (string= (substring name
0 1) " ") (null file
)))
583 ;; Maybe don't mention buffers without files.
584 ((and files-only
(not file
)))
585 ((string= name
"*Buffer List*"))
586 ;; Otherwise output info.
589 ;; No visited file. Check local value of
590 ;; list-buffers-directory.
591 (when (and (boundp 'list-buffers-directory
)
592 list-buffers-directory
)
593 (setq file list-buffers-directory
)))
596 (if (eq buffer old-buffer
) ?. ?
)
597 ;; Handle readonly status. The output buffer is special
598 ;; cased to appear readonly; it is actually made so at a
600 (if (or (eq buffer standard-output
)
603 ;; Identify modified buffers.
604 (if (buffer-modified-p) ?
* ?
))
605 name
(buffer-size) mode-name file
)))))
608 (if Buffer-menu-sort-column
610 (if (eq Buffer-menu-sort-column
3)
612 (< (nth Buffer-menu-sort-column a
)
613 (nth Buffer-menu-sort-column b
)))
615 (string< (nth Buffer-menu-sort-column a
)
616 (nth Buffer-menu-sort-column b
)))))
618 (if (eq (car buffer
) old-buffer
)
619 (setq desired-point
(point)))
620 (insert (cadr buffer
)
621 ;; Put the buffer name into a text property
622 ;; so we don't have to extract it from the text.
623 ;; This way we avoid problems with unusual buffer names.
624 (Buffer-menu-buffer+size
(nth 2 buffer
)
625 (int-to-string (nth 3 buffer
))
626 `(buffer-name ,(nth 2 buffer
)
628 face Buffer-menu-buffer-face
630 help-echo
"mouse-2: select this buffer"))
632 (if (> (length (nth 4 buffer
)) Buffer-menu-mode-width
)
633 (substring (nth 4 buffer
) 0 Buffer-menu-mode-width
)
636 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer
+size-width
637 Buffer-menu-mode-width
4) 1)
638 (princ (abbreviate-file-name (nth 5 buffer
))))
641 (when Buffer-menu-use-header-line
642 (set (make-local-variable 'Buffer-menu-header-line
)
644 (setq header-line-format
'Buffer-menu-header-line
))
645 ;; DESIRED-POINT doesn't have to be set; it is not when the
646 ;; current buffer is not displayed for some reason.
648 (goto-char desired-point
))
651 ;;; buff-menu.el ends here