1 ;;; buff-menu.el --- buffer menu main function and support functions.
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; Edit, delete, or change attributes of all currently active Emacs
26 ;; buffers from a list summarizing their state. A good way to browse
27 ;; any special or scratch buffers you have loaded, since you can't find
28 ;; them by filename. The single entry point is `Buffer-menu-mode',
29 ;; normally bound to C-x C-b.
33 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
35 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
37 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
38 ;; current entry and then move to previous one.
40 ;; Based on FSF code dating back to 1985.
44 ;;;Not needed, now that q is now just quit and Buffer-menu-select is v.
45 ;;;(defvar Buffer-menu-window-config nil
46 ;;; "Window configuration saved from entry to `buffer-menu'.")
48 ; Put buffer *Buffer List* into proper mode right away
49 ; so that from now on even list-buffers is enough to get a buffer menu.
51 (defvar Buffer-menu-buffer-column nil
)
53 (defvar Buffer-menu-mode-map nil
"")
55 (if Buffer-menu-mode-map
57 (setq Buffer-menu-mode-map
(make-keymap))
58 (suppress-keymap Buffer-menu-mode-map t
)
59 (define-key Buffer-menu-mode-map
"q" 'Buffer-menu-quit
)
60 (define-key Buffer-menu-mode-map
"v" 'Buffer-menu-select
)
61 (define-key Buffer-menu-mode-map
"2" 'Buffer-menu-2-window
)
62 (define-key Buffer-menu-mode-map
"1" 'Buffer-menu-1-window
)
63 (define-key Buffer-menu-mode-map
"f" 'Buffer-menu-this-window
)
64 (define-key Buffer-menu-mode-map
"\C-m" 'Buffer-menu-this-window
)
65 (define-key Buffer-menu-mode-map
"o" 'Buffer-menu-other-window
)
66 (define-key Buffer-menu-mode-map
"\C-o" 'Buffer-menu-switch-other-window
)
67 (define-key Buffer-menu-mode-map
"s" 'Buffer-menu-save
)
68 (define-key Buffer-menu-mode-map
"d" 'Buffer-menu-delete
)
69 (define-key Buffer-menu-mode-map
"k" 'Buffer-menu-delete
)
70 (define-key Buffer-menu-mode-map
"\C-d" 'Buffer-menu-delete-backwards
)
71 (define-key Buffer-menu-mode-map
"\C-k" 'Buffer-menu-delete
)
72 (define-key Buffer-menu-mode-map
"x" 'Buffer-menu-execute
)
73 (define-key Buffer-menu-mode-map
" " 'next-line
)
74 (define-key Buffer-menu-mode-map
"n" 'next-line
)
75 (define-key Buffer-menu-mode-map
"p" 'previous-line
)
76 (define-key Buffer-menu-mode-map
"\177" 'Buffer-menu-backup-unmark
)
77 (define-key Buffer-menu-mode-map
"~" 'Buffer-menu-not-modified
)
78 (define-key Buffer-menu-mode-map
"?" 'describe-mode
)
79 (define-key Buffer-menu-mode-map
"u" 'Buffer-menu-unmark
)
80 (define-key Buffer-menu-mode-map
"m" 'Buffer-menu-mark
)
81 (define-key Buffer-menu-mode-map
"t" 'Buffer-menu-visit-tags-table
)
82 (define-key Buffer-menu-mode-map
"%" 'Buffer-menu-toggle-read-only
)
83 (define-key Buffer-menu-mode-map
"g" 'revert-buffer
)
84 (define-key Buffer-menu-mode-map
[mouse-2
] 'Buffer-menu-mouse-select
)
87 ;; Buffer Menu mode is suitable only for specially formatted data.
88 (put 'Buffer-menu-mode
'mode-class
'special
)
90 (defun Buffer-menu-mode ()
91 "Major mode for editing a list of buffers.
92 Each line describes one of the buffers in Emacs.
93 Letters do not insert themselves; instead, they are commands.
94 \\<Buffer-menu-mode-map>
95 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
96 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
97 \\[Buffer-menu-other-window] -- select that buffer in another window,
98 so the buffer menu buffer remains visible in its window.
99 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
100 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
101 \\[Buffer-menu-select] -- select current line's buffer.
102 Also show buffers marked with m, in other windows.
103 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
104 \\[Buffer-menu-2-window] -- select that buffer in one window,
105 together with buffer selected before this one in another window.
106 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
107 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
108 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
109 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
110 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
111 \\[Buffer-menu-execute] -- delete or save marked buffers.
112 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
113 With prefix argument, also move up one line.
114 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
115 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line."
116 (kill-all-local-variables)
117 (use-local-map Buffer-menu-mode-map
)
118 (setq major-mode
'Buffer-menu-mode
)
119 (setq mode-name
"Buffer Menu")
121 (goto-char (point-min))
122 (search-forward "Buffer")
124 (setq Buffer-menu-buffer-column
(current-column))
127 (let ((where (Buffer-menu-buffer-name-position)))
128 (put-text-property (car where
) (cdr where
) 'mouse-face
'highlight
))
130 (make-local-variable 'revert-buffer-function
)
131 (setq revert-buffer-function
'Buffer-menu-revert-function
)
132 (setq truncate-lines t
)
133 (setq buffer-read-only t
)
134 (run-hooks 'buffer-menu-mode-hook
))
136 (defun Buffer-menu-revert-function (ignore1 ignore2
)
139 (defun Buffer-menu-buffer (error-if-non-existent-p)
140 "Return buffer described by this line of buffer menu."
141 (let* ((where (Buffer-menu-buffer-name-position))
142 (string (buffer-substring (car where
) (cdr where
))))
143 (set-text-properties 0 (length string
) nil string
)
144 (or (get-buffer string
)
145 (if error-if-non-existent-p
146 (error "No buffer named \"%s\"" string
)
149 ;; Find the start and end positions of the buffer name on this line.
150 ;; Returns a cons (START . END).
151 (defun Buffer-menu-buffer-name-position ()
154 (forward-char Buffer-menu-buffer-column
)
155 (let ((start (point)))
156 (re-search-forward "\t\\| ")
157 (skip-chars-backward " \t")
158 (cons start
(point)))))
160 (defun buffer-menu (&optional arg
)
161 "Make a menu of buffers so you can save, delete or select them.
162 With argument, show only buffers that are visiting files.
163 Type ? after invocation to get help on commands available.
164 Type q immediately to make the buffer menu go away and to restore
165 previous window configuration."
167 ;;; (setq Buffer-menu-window-config (current-window-configuration))
169 (let ((newpoint (save-excursion (set-buffer "*Buffer List*")
171 (pop-to-buffer "*Buffer List*")
172 (goto-char newpoint
))
174 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
176 (defun Buffer-menu-quit ()
177 "Quit the buffer menu."
179 (let ((buffer (current-buffer)))
180 ;; Restore previous window configuration before displaying
182 (switch-to-buffer (other-buffer))
183 (bury-buffer buffer
)))
185 (defun Buffer-menu-mark ()
186 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
189 (if (looking-at " [-M]")
191 (let ((buffer-read-only nil
))
196 (defun Buffer-menu-unmark (&optional backup
)
197 "Cancel all requested operations on buffer on this line and move down.
198 Optional ARG means move up."
201 (if (looking-at " [-M]")
203 (let* ((buf (Buffer-menu-buffer t
))
204 (mod (buffer-modified-p buf
))
205 (readonly (save-excursion (set-buffer buf
) buffer-read-only
))
206 (buffer-read-only nil
))
208 (insert (if readonly
(if mod
" *%" " %") (if mod
" * " " ")))))
209 (forward-line (if backup -
1 1)))
211 (defun Buffer-menu-backup-unmark ()
212 "Move up and cancel all requested operations on buffer on line above."
218 (defun Buffer-menu-delete (&optional arg
)
219 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
220 Prefix arg is how many buffers to delete.
221 Negative arg means delete backwards."
224 (if (looking-at " [-M]") ;header lines
226 (let ((buffer-read-only nil
))
227 (if (or (null arg
) (= arg
0))
238 (setq arg
(1+ arg
))))))
240 (defun Buffer-menu-delete-backwards (&optional arg
)
241 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
242 and then move up one line. Prefix arg means move that many lines."
244 (Buffer-menu-delete (- (or arg
1)))
245 (while (looking-at " [-M]")
248 (defun Buffer-menu-save ()
249 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
253 (if (looking-at " [-M]") ;header lines
255 (let ((buffer-read-only nil
))
260 (defun Buffer-menu-not-modified (&optional arg
)
261 "Mark buffer on this line as unmodified (no changes to save)."
264 (set-buffer (Buffer-menu-buffer t
))
265 (set-buffer-modified-p arg
))
269 (if (= (char-after (point)) (if arg ? ?
*))
270 (let ((buffer-read-only nil
))
272 (insert (if arg ?
* ?
))))))
274 (defun Buffer-menu-execute ()
275 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
278 (goto-char (point-min))
280 (while (re-search-forward "^.S" nil t
)
283 (set-buffer (Buffer-menu-buffer t
))
285 (setq modp
(buffer-modified-p)))
286 (let ((buffer-read-only nil
))
288 (insert (if modp ?
* ?
))))))
290 (goto-char (point-min))
292 (let ((buff-menu-buffer (current-buffer))
293 (buffer-read-only nil
))
294 (while (search-forward "\nD" nil t
)
296 (let ((buf (Buffer-menu-buffer nil
)))
298 (eq buf buff-menu-buffer
)
299 (save-excursion (kill-buffer buf
))))
300 (if (Buffer-menu-buffer nil
)
301 (progn (delete-char 1)
303 (delete-region (point) (progn (forward-line 1) (point)))
304 (forward-char -
1))))))
306 (defun Buffer-menu-select ()
307 "Select this line's buffer; also display buffers marked with `>'.
308 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
310 (let ((buff (Buffer-menu-buffer t
))
311 (menu (current-buffer))
314 (goto-char (point-min))
315 (while (search-forward "\n>" nil t
)
316 (setq tem
(Buffer-menu-buffer t
))
317 (let ((buffer-read-only nil
))
320 (or (eq tem buff
) (memq tem others
) (setq others
(cons tem others
))))
321 (setq others
(nreverse others
)
322 tem
(/ (1- (frame-height)) (1+ (length others
))))
323 (delete-other-windows)
324 (switch-to-buffer buff
)
327 (if (equal (length others
) 0)
329 ;;; ;; Restore previous window configuration before displaying
330 ;;; ;; selected buffers.
331 ;;; (if Buffer-menu-window-config
333 ;;; (set-window-configuration Buffer-menu-window-config)
334 ;;; (setq Buffer-menu-window-config nil)))
335 (switch-to-buffer buff
))
337 (split-window nil tem
)
339 (switch-to-buffer (car others
))
340 (setq others
(cdr others
)))
341 (other-window 1) ;back to the beginning!
345 (defun Buffer-menu-visit-tags-table ()
346 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
348 (let ((file (buffer-file-name (Buffer-menu-buffer t
))))
350 (visit-tags-table file
)
351 (error "Specified buffer has no file"))))
353 (defun Buffer-menu-1-window ()
354 "Select this line's buffer, alone, in full frame."
356 (switch-to-buffer (Buffer-menu-buffer t
))
357 (bury-buffer (other-buffer))
358 (delete-other-windows))
360 (defun Buffer-menu-mouse-select (event)
361 "Select the buffer whose line you click on."
365 (set-buffer (window-buffer (posn-window (event-end event
))))
367 (goto-char (posn-point (event-end event
)))
368 (setq buffer
(Buffer-menu-buffer t
))))
369 (select-window (posn-window (event-end event
)))
370 (if (and (window-dedicated-p (selected-window))
371 (eq (selected-window) (frame-root-window)))
372 (switch-to-buffer-other-frame buffer
)
373 (switch-to-buffer buffer
))))
375 (defun Buffer-menu-this-window ()
376 "Select this line's buffer in this window."
378 (switch-to-buffer (Buffer-menu-buffer t
)))
380 (defun Buffer-menu-other-window ()
381 "Select this line's buffer in other window, leaving buffer menu visible."
383 (switch-to-buffer-other-window (Buffer-menu-buffer t
)))
385 (defun Buffer-menu-switch-other-window ()
386 "Make the other window select this line's buffer.
387 The current window remains selected."
389 (display-buffer (Buffer-menu-buffer t
)))
391 (defun Buffer-menu-2-window ()
392 "Select this line's buffer, with previous buffer in second window."
394 (let ((buff (Buffer-menu-buffer t
))
395 (menu (current-buffer))
397 (delete-other-windows)
398 (switch-to-buffer (other-buffer))
402 (defun Buffer-menu-toggle-read-only ()
403 "Toggle read-only status of buffer on this line, perhaps via version control."
407 (set-buffer (Buffer-menu-buffer t
))
408 (vc-toggle-read-only)
409 (setq char
(if buffer-read-only ?% ?
)))
413 (if (/= (following-char) char
)
414 (let (buffer-read-only)
420 (define-key ctl-x-map
"\C-b" 'list-buffers
)
422 (defun list-buffers (&optional files-only
)
423 "Display a list of names of existing buffers.
424 The list is displayed in a buffer named `*Buffer List*'.
425 Note that buffers with names starting with spaces are omitted.
426 Non-null optional arg FILES-ONLY means mention only file buffers.
428 The M column contains a * for buffers that are modified.
429 The R column contains a % for buffers that are read-only."
431 (let ((old-buffer (current-buffer))
432 (blist-buffer (get-buffer-create "*Buffer List*"))
434 (with-output-to-temp-buffer "*Buffer List*"
435 (set-buffer standard-output
)
437 MR Buffer Size Mode File
438 -- ------ ---- ---- ----
440 (let ((bl (buffer-list)))
442 (let* ((buffer (car bl
))
443 (name (buffer-name buffer
))
444 (file (buffer-file-name buffer
)))
446 ;; Don't mention internal buffers.
447 ((string= (substring name
0 1) " "))
448 ;; Maybe don't mention buffers without files.
449 ((and files-only
(not file
)))
450 ;; Otherwise output info.
452 ;; Identify current buffer.
453 (if (eq buffer old-buffer
)
455 (setq desired-point
(point))
458 ;; Identify modified buffers.
459 (princ (if (buffer-modified-p buffer
) "*" " "))
460 ;; Handle readonly status. The output buffer is special
461 ;; cased to be readonly; it is actually made so at a later
463 (princ (if (or (eq buffer standard-output
) buffer-read-only
)
470 (excess (- (current-column) 17)))
473 (setq size
(format "%8d" (buffer-size)))
474 ;; Ack -- if looking at the *Buffer List* buffer,
475 ;; always use "Buffer Menu" mode. Otherwise the
476 ;; first time the buffer is created, the mode will
478 (setq mode
(if (eq buffer standard-output
)
481 (while (and (> excess
0) (= (aref size
0) ?\
))
482 (setq size
(substring size
1))
483 (setq excess
(1- excess
))))
489 ;; No visited file. Check local value of
490 ;; list-buffers-directory.
493 (if (and (boundp 'list-buffers-directory
)
494 list-buffers-directory
)
495 (setq file list-buffers-directory
))))
499 (setq bl
(cdr bl
)))))
500 ;; DESIRED-POINT doesn't have to be set; it is not when the
501 ;; current buffer is not displayed for some reason.
503 (set-buffer blist-buffer
)
506 (goto-char desired-point
)))))
508 ;;; buff-menu.el ends here