1 ;;; buff-menu.el --- buffer menu main function and support functions.
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
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 `Buffer-menu-mode',
30 ;; normally bound to C-x C-b.
34 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
36 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
38 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
39 ;; current entry and then move to previous one.
41 ;; Based on FSF code dating back to 1985.
45 ;;;Trying to preserve the old window configuration works well in
46 ;;;simple scenarios, when you enter the buffer menu, use it, and exit it.
47 ;;;But it does strange things when you switch back to the buffer list buffer
48 ;;;with C-x b, later on, when the window configuration is different.
49 ;;;The choice seems to be, either restore the window configuration
50 ;;;in all cases, or in no cases.
51 ;;;I decided it was better not to restore the window config at all. -- rms.
53 ;;;But since then, I changed buffer-menu to use the selected window,
54 ;;;so q now once again goes back to the previous window configuration.
56 ;;;(defvar Buffer-menu-window-config nil
57 ;;; "Window configuration saved from entry to `buffer-menu'.")
59 ; Put buffer *Buffer List* into proper mode right away
60 ; so that from now on even list-buffers is enough to get a buffer menu.
62 (defvar Buffer-menu-buffer-column nil
)
64 (defvar Buffer-menu-mode-map nil
"")
66 (if Buffer-menu-mode-map
68 (setq Buffer-menu-mode-map
(make-keymap))
69 (suppress-keymap Buffer-menu-mode-map t
)
70 (define-key Buffer-menu-mode-map
"q" 'Buffer-menu-quit
)
71 (define-key Buffer-menu-mode-map
"v" 'Buffer-menu-select
)
72 (define-key Buffer-menu-mode-map
"2" 'Buffer-menu-2-window
)
73 (define-key Buffer-menu-mode-map
"1" 'Buffer-menu-1-window
)
74 (define-key Buffer-menu-mode-map
"f" 'Buffer-menu-this-window
)
75 (define-key Buffer-menu-mode-map
"\C-m" 'Buffer-menu-this-window
)
76 (define-key Buffer-menu-mode-map
"o" 'Buffer-menu-other-window
)
77 (define-key Buffer-menu-mode-map
"\C-o" 'Buffer-menu-switch-other-window
)
78 (define-key Buffer-menu-mode-map
"s" 'Buffer-menu-save
)
79 (define-key Buffer-menu-mode-map
"d" 'Buffer-menu-delete
)
80 (define-key Buffer-menu-mode-map
"k" 'Buffer-menu-delete
)
81 (define-key Buffer-menu-mode-map
"\C-d" 'Buffer-menu-delete-backwards
)
82 (define-key Buffer-menu-mode-map
"\C-k" 'Buffer-menu-delete
)
83 (define-key Buffer-menu-mode-map
"x" 'Buffer-menu-execute
)
84 (define-key Buffer-menu-mode-map
" " 'next-line
)
85 (define-key Buffer-menu-mode-map
"n" 'next-line
)
86 (define-key Buffer-menu-mode-map
"p" 'previous-line
)
87 (define-key Buffer-menu-mode-map
"\177" 'Buffer-menu-backup-unmark
)
88 (define-key Buffer-menu-mode-map
"~" 'Buffer-menu-not-modified
)
89 (define-key Buffer-menu-mode-map
"?" 'describe-mode
)
90 (define-key Buffer-menu-mode-map
"u" 'Buffer-menu-unmark
)
91 (define-key Buffer-menu-mode-map
"m" 'Buffer-menu-mark
)
92 (define-key Buffer-menu-mode-map
"t" 'Buffer-menu-visit-tags-table
)
93 (define-key Buffer-menu-mode-map
"%" 'Buffer-menu-toggle-read-only
)
94 (define-key Buffer-menu-mode-map
"b" 'Buffer-menu-bury
)
95 (define-key Buffer-menu-mode-map
"g" 'Buffer-menu-revert
)
96 (define-key Buffer-menu-mode-map
[mouse-2
] 'Buffer-menu-mouse-select
)
99 ;; Buffer Menu mode is suitable only for specially formatted data.
100 (put 'Buffer-menu-mode
'mode-class
'special
)
102 (defun Buffer-menu-mode ()
103 "Major mode for editing a list of buffers.
104 Each line describes one of the buffers in Emacs.
105 Letters do not insert themselves; instead, they are commands.
106 \\<Buffer-menu-mode-map>
107 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
108 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
109 \\[Buffer-menu-other-window] -- select that buffer in another window,
110 so the buffer menu buffer remains visible in its window.
111 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
112 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
113 \\[Buffer-menu-select] -- select current line's buffer.
114 Also show buffers marked with m, in other windows.
115 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
116 \\[Buffer-menu-2-window] -- select that buffer in one window,
117 together with buffer selected before this one in another window.
118 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
119 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
120 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
121 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
122 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
123 \\[Buffer-menu-execute] -- delete or save marked buffers.
124 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
125 With prefix argument, also move up one line.
126 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
127 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
128 \\[Buffer-menu-revert] -- update the list of buffers.
129 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
130 (kill-all-local-variables)
131 (use-local-map Buffer-menu-mode-map
)
132 (setq major-mode
'Buffer-menu-mode
)
133 (setq mode-name
"Buffer Menu")
134 (make-local-variable 'revert-buffer-function
)
135 (setq revert-buffer-function
'Buffer-menu-revert-function
)
136 (setq truncate-lines t
)
137 (setq buffer-read-only t
)
138 (run-hooks 'buffer-menu-mode-hook
))
140 (defun Buffer-menu-revert ()
141 "Update the list of buffers."
145 (defun Buffer-menu-revert-function (ignore1 ignore2
)
148 (defun Buffer-menu-buffer (error-if-non-existent-p)
149 "Return buffer described by this line of buffer menu."
150 (let* ((where (save-excursion
152 (+ (point) Buffer-menu-buffer-column
)))
153 (name (and (not (eobp)) (get-text-property where
'buffer-name
))))
155 (or (get-buffer name
)
156 (if error-if-non-existent-p
157 (error "No buffer named `%s'" name
)
159 (if error-if-non-existent-p
160 (error "No buffer on this line")
163 (defun buffer-menu (&optional arg
)
164 "Make a menu of buffers so you can save, delete or select them.
165 With argument, show only buffers that are visiting files.
166 Type ? after invocation to get help on commands available.
167 Type q immediately to make the buffer menu go away."
169 ;;; (setq Buffer-menu-window-config (current-window-configuration))
170 (switch-to-buffer (list-buffers-noselect arg
))
172 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
174 (defun buffer-menu-other-window (&optional arg
)
175 "Display a list of buffers in another window.
176 With the buffer list buffer, you can save, delete or select the buffers.
177 With argument, show only buffers that are visiting files.
178 Type ? after invocation to get help on commands available.
179 Type q immediately to make the buffer menu go away."
181 ;;; (setq Buffer-menu-window-config (current-window-configuration))
182 (switch-to-buffer-other-window (list-buffers-noselect arg
))
184 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
186 (defun Buffer-menu-quit ()
187 "Quit the buffer menu."
189 (let ((buffer (current-buffer)))
190 ;; Switch away from the buffer menu and bury it.
191 (switch-to-buffer (other-buffer))
192 (bury-buffer buffer
)))
194 (defun Buffer-menu-mark ()
195 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
198 (if (looking-at " [-M]")
200 (let ((buffer-read-only nil
))
205 (defun Buffer-menu-unmark (&optional backup
)
206 "Cancel all requested operations on buffer on this line and move down.
207 Optional ARG means move up."
210 (if (looking-at " [-M]")
212 (let* ((buf (Buffer-menu-buffer t
))
213 (mod (buffer-modified-p buf
))
214 (readonly (save-excursion (set-buffer buf
) buffer-read-only
))
215 (buffer-read-only nil
))
217 (insert (if readonly
(if mod
" *%" " %") (if mod
" * " " ")))))
218 (forward-line (if backup -
1 1)))
220 (defun Buffer-menu-backup-unmark ()
221 "Move up and cancel all requested operations on buffer on line above."
227 (defun Buffer-menu-delete (&optional arg
)
228 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
229 Prefix arg is how many buffers to delete.
230 Negative arg means delete backwards."
233 (if (looking-at " [-M]") ;header lines
235 (let ((buffer-read-only nil
))
236 (if (or (null arg
) (= arg
0))
247 (setq arg
(1+ arg
))))))
249 (defun Buffer-menu-delete-backwards (&optional arg
)
250 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
251 and then move up one line. Prefix arg means move that many lines."
253 (Buffer-menu-delete (- (or arg
1)))
254 (while (looking-at " [-M]")
257 (defun Buffer-menu-save ()
258 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
261 (if (looking-at " [-M]") ;header lines
263 (let ((buffer-read-only nil
))
269 (defun Buffer-menu-not-modified (&optional arg
)
270 "Mark buffer on this line as unmodified (no changes to save)."
273 (set-buffer (Buffer-menu-buffer t
))
274 (set-buffer-modified-p arg
))
278 (if (= (char-after (point)) (if arg ? ?
*))
279 (let ((buffer-read-only nil
))
281 (insert (if arg ?
* ?
))))))
283 (defun Buffer-menu-execute ()
284 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
287 (goto-char (point-min))
289 (while (re-search-forward "^.S" nil t
)
292 (set-buffer (Buffer-menu-buffer t
))
294 (setq modp
(buffer-modified-p)))
295 (let ((buffer-read-only nil
))
297 (insert (if modp ?
* ?
))))))
299 (goto-char (point-min))
301 (let ((buff-menu-buffer (current-buffer))
302 (buffer-read-only nil
))
303 (while (search-forward "\nD" nil t
)
305 (let ((buf (Buffer-menu-buffer nil
)))
307 (eq buf buff-menu-buffer
)
308 (save-excursion (kill-buffer buf
))))
309 (if (Buffer-menu-buffer nil
)
310 (progn (delete-char 1)
312 (delete-region (point) (progn (forward-line 1) (point)))
313 (forward-char -
1))))))
315 (defun Buffer-menu-select ()
316 "Select this line's buffer; also display buffers marked with `>'.
317 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
318 This command deletes and replaces all the previously existing windows
319 in the selected frame."
321 (let ((buff (Buffer-menu-buffer t
))
322 (menu (current-buffer))
325 (goto-char (point-min))
326 (while (search-forward "\n>" nil t
)
327 (setq tem
(Buffer-menu-buffer t
))
328 (let ((buffer-read-only nil
))
331 (or (eq tem buff
) (memq tem others
) (setq others
(cons tem others
))))
332 (setq others
(nreverse others
)
333 tem
(/ (1- (frame-height)) (1+ (length others
))))
334 (delete-other-windows)
335 (switch-to-buffer buff
)
338 (if (equal (length others
) 0)
340 ;;; ;; Restore previous window configuration before displaying
341 ;;; ;; selected buffers.
342 ;;; (if Buffer-menu-window-config
344 ;;; (set-window-configuration Buffer-menu-window-config)
345 ;;; (setq Buffer-menu-window-config nil)))
346 (switch-to-buffer buff
))
348 (split-window nil tem
)
350 (switch-to-buffer (car others
))
351 (setq others
(cdr others
)))
352 (other-window 1) ;back to the beginning!
357 (defun Buffer-menu-visit-tags-table ()
358 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
360 (let ((file (buffer-file-name (Buffer-menu-buffer t
))))
362 (visit-tags-table file
)
363 (error "Specified buffer has no file"))))
365 (defun Buffer-menu-1-window ()
366 "Select this line's buffer, alone, in full frame."
368 (switch-to-buffer (Buffer-menu-buffer t
))
369 (bury-buffer (other-buffer))
370 (delete-other-windows))
372 (defun Buffer-menu-mouse-select (event)
373 "Select the buffer whose line you click on."
377 (set-buffer (window-buffer (posn-window (event-end event
))))
379 (goto-char (posn-point (event-end event
)))
380 (setq buffer
(Buffer-menu-buffer t
))))
381 (select-window (posn-window (event-end event
)))
382 (if (and (window-dedicated-p (selected-window))
383 (eq (selected-window) (frame-root-window)))
384 (switch-to-buffer-other-frame buffer
)
385 (switch-to-buffer buffer
))))
387 (defun Buffer-menu-this-window ()
388 "Select this line's buffer in this window."
390 (switch-to-buffer (Buffer-menu-buffer t
)))
392 (defun Buffer-menu-other-window ()
393 "Select this line's buffer in other window, leaving buffer menu visible."
395 (switch-to-buffer-other-window (Buffer-menu-buffer t
)))
397 (defun Buffer-menu-switch-other-window ()
398 "Make the other window select this line's buffer.
399 The current window remains selected."
401 (display-buffer (Buffer-menu-buffer t
)))
403 (defun Buffer-menu-2-window ()
404 "Select this line's buffer, with previous buffer in second window."
406 (let ((buff (Buffer-menu-buffer t
))
407 (menu (current-buffer))
409 (delete-other-windows)
410 (switch-to-buffer (other-buffer))
414 (defun Buffer-menu-toggle-read-only ()
415 "Toggle read-only status of buffer on this line, perhaps via version control."
419 (set-buffer (Buffer-menu-buffer t
))
420 (vc-toggle-read-only)
421 (setq char
(if buffer-read-only ?% ?
)))
425 (if (/= (following-char) char
)
426 (let (buffer-read-only)
430 (defun Buffer-menu-bury ()
431 "Bury the buffer listed on this line."
434 (if (looking-at " [-M]") ;header lines
438 (bury-buffer (Buffer-menu-buffer t
))
439 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
440 (buffer-read-only nil
))
441 (delete-region (point) (progn (forward-line -
1) (point)))
442 (goto-char (point-max))
444 (message "Buried buffer moved to the end"))))
447 (define-key ctl-x-map
"\C-b" 'list-buffers
)
449 (defun list-buffers (&optional files-only
)
450 "Display a list of names of existing buffers.
451 The list is displayed in a buffer named `*Buffer List*'.
452 Note that buffers with names starting with spaces are omitted.
453 Non-null optional arg FILES-ONLY means mention only file buffers.
455 The M column contains a * for buffers that are modified.
456 The R column contains a % for buffers that are read-only."
458 (display-buffer (list-buffers-noselect files-only
)))
460 (defun list-buffers-noselect (&optional files-only
)
461 "Create and return a buffer with a list of names of existing buffers.
462 The buffer is named `*Buffer List*'.
463 Note that buffers with names starting with spaces are omitted.
464 Non-null optional arg FILES-ONLY means mention only file buffers.
466 The M column contains a * for buffers that are modified.
467 The R column contains a % for buffers that are read-only."
468 (let ((old-buffer (current-buffer))
469 (standard-output standard-output
)
472 (set-buffer (get-buffer-create "*Buffer List*"))
473 (setq buffer-read-only nil
)
475 (setq standard-output
(current-buffer))
477 MR Buffer Size Mode File
478 -- ------ ---- ---- ----
480 ;; Record the column where buffer names start.
481 (setq Buffer-menu-buffer-column
4)
482 (let ((bl (buffer-list)))
484 (let* ((buffer (car bl
))
485 (name (buffer-name buffer
))
486 (file (buffer-file-name buffer
))
487 this-buffer-line-start
488 this-buffer-read-only
490 this-buffer-mode-name
491 this-buffer-directory
)
494 (setq this-buffer-read-only buffer-read-only
)
495 (setq this-buffer-size
(buffer-size))
496 (setq this-buffer-mode-name
497 (if (eq buffer standard-output
)
498 "Buffer Menu" mode-name
))
500 ;; No visited file. Check local value of
501 ;; list-buffers-directory.
502 (if (and (boundp 'list-buffers-directory
)
503 list-buffers-directory
)
504 (setq this-buffer-directory list-buffers-directory
))))
506 ;; Don't mention internal buffers.
507 ((string= (substring name
0 1) " "))
508 ;; Maybe don't mention buffers without files.
509 ((and files-only
(not file
)))
510 ;; Otherwise output info.
512 (setq this-buffer-line-start
(point))
513 ;; Identify current buffer.
514 (if (eq buffer old-buffer
)
516 (setq desired-point
(point))
519 ;; Identify modified buffers.
520 (princ (if (buffer-modified-p buffer
) "*" " "))
521 ;; Handle readonly status. The output buffer is special
522 ;; cased to appear readonly; it is actually made so at a later
524 (princ (if (or (eq buffer standard-output
)
525 this-buffer-read-only
)
529 ;; Put the buffer name into a text property
530 ;; so we don't have to extract it from the text.
531 ;; This way we avoid problems with unusual buffer names.
532 (setq this-buffer-line-start
533 (+ this-buffer-line-start Buffer-menu-buffer-column
))
534 (let ((name-end (point)))
536 (put-text-property this-buffer-line-start name-end
538 (put-text-property this-buffer-line-start name-end
539 'mouse-face
'highlight
))
542 (excess (- (current-column) 17)))
543 (setq size
(format "%8d" this-buffer-size
))
544 ;; Ack -- if looking at the *Buffer List* buffer,
545 ;; always use "Buffer Menu" mode. Otherwise the
546 ;; first time the buffer is created, the mode will be wrong.
547 (setq mode this-buffer-mode-name
)
548 (while (and (> excess
0) (= (aref size
0) ?\
))
549 (setq size
(substring size
1))
550 (setq excess
(1- excess
)))
555 (or file
(setq file this-buffer-directory
))
561 ;; DESIRED-POINT doesn't have to be set; it is not when the
562 ;; current buffer is not displayed for some reason.
564 (goto-char desired-point
))
567 ;;; buff-menu.el ends here