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
"o" 'Buffer-menu-other-window
)
65 (define-key Buffer-menu-mode-map
"\C-o" 'Buffer-menu-switch-other-window
)
66 (define-key Buffer-menu-mode-map
"s" 'Buffer-menu-save
)
67 (define-key Buffer-menu-mode-map
"d" 'Buffer-menu-delete
)
68 (define-key Buffer-menu-mode-map
"k" 'Buffer-menu-delete
)
69 (define-key Buffer-menu-mode-map
"\C-d" 'Buffer-menu-delete-backwards
)
70 (define-key Buffer-menu-mode-map
"\C-k" 'Buffer-menu-delete
)
71 (define-key Buffer-menu-mode-map
"x" 'Buffer-menu-execute
)
72 (define-key Buffer-menu-mode-map
" " 'next-line
)
73 (define-key Buffer-menu-mode-map
"n" 'next-line
)
74 (define-key Buffer-menu-mode-map
"p" 'previous-line
)
75 (define-key Buffer-menu-mode-map
"\177" 'Buffer-menu-backup-unmark
)
76 (define-key Buffer-menu-mode-map
"~" 'Buffer-menu-not-modified
)
77 (define-key Buffer-menu-mode-map
"?" 'describe-mode
)
78 (define-key Buffer-menu-mode-map
"u" 'Buffer-menu-unmark
)
79 (define-key Buffer-menu-mode-map
"m" 'Buffer-menu-mark
)
80 (define-key Buffer-menu-mode-map
"t" 'Buffer-menu-visit-tags-table
)
81 (define-key Buffer-menu-mode-map
"%" 'Buffer-menu-toggle-read-only
)
82 (define-key Buffer-menu-mode-map
"g" 'revert-buffer
)
83 (define-key Buffer-menu-mode-map
[mouse-2
] 'Buffer-menu-mouse-select
)
86 ;; Buffer Menu mode is suitable only for specially formatted data.
87 (put 'Buffer-menu-mode
'mode-class
'special
)
89 (defun Buffer-menu-mode ()
90 "Major mode for editing a list of buffers.
91 Each line describes one of the buffers in Emacs.
92 Letters do not insert themselves; instead, they are commands.
93 \\<Buffer-menu-mode-map>
94 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
95 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
96 \\[Buffer-menu-other-window] -- select that buffer in another window,
97 so the buffer menu buffer remains visible in its window.
98 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
99 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
100 \\[Buffer-menu-select] -- select current line's buffer.
101 Also show buffers marked with m, in other windows.
102 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
103 \\[Buffer-menu-2-window] -- select that buffer in one window,
104 together with buffer selected before this one in another window.
105 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
106 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
107 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
108 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
109 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
110 \\[Buffer-menu-execute] -- delete or save marked buffers.
111 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
112 With prefix argument, also move up one line.
113 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
114 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line."
115 (kill-all-local-variables)
116 (use-local-map Buffer-menu-mode-map
)
117 (setq major-mode
'Buffer-menu-mode
)
118 (setq mode-name
"Buffer Menu")
120 (goto-char (point-min))
121 (search-forward "Buffer")
123 (setq Buffer-menu-buffer-column
(current-column))
126 (let ((where (Buffer-menu-buffer-name-position)))
127 (put-text-property (car where
) (cdr where
) 'mouse-face
'highlight
))
129 (make-local-variable 'revert-buffer-function
)
130 (setq revert-buffer-function
'Buffer-menu-revert-function
)
131 (setq truncate-lines t
)
132 (setq buffer-read-only t
)
133 (run-hooks 'buffer-menu-mode-hook
))
135 (defun Buffer-menu-revert-function (ignore1 ignore2
)
138 (defun Buffer-menu-buffer (error-if-non-existent-p)
139 "Return buffer described by this line of buffer menu."
140 (let* ((where (Buffer-menu-buffer-name-position))
141 (string (buffer-substring (car where
) (cdr where
))))
142 (or (get-buffer string
)
143 (if error-if-non-existent-p
144 (error "No buffer named \"%s\"" string
)
147 ;; Find the start and end positions of the buffer name on this line.
148 ;; Returns a cons (START . END).
149 (defun Buffer-menu-buffer-name-position ()
152 (forward-char Buffer-menu-buffer-column
)
153 (let ((start (point)))
154 (re-search-forward "\t\\| ")
155 (skip-chars-backward " \t")
156 (cons start
(point)))))
158 (defun buffer-menu (&optional arg
)
159 "Make a menu of buffers so you can save, delete or select them.
160 With argument, show only buffers that are visiting files.
161 Type ? after invocation to get help on commands available.
162 Type q immediately to make the buffer menu go away and to restore
163 previous window configuration."
165 ;;; (setq Buffer-menu-window-config (current-window-configuration))
167 (let ((newpoint (save-excursion (set-buffer "*Buffer List*")
169 (pop-to-buffer "*Buffer List*")
170 (goto-char newpoint
))
172 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
174 (defun Buffer-menu-quit ()
175 "Quit the buffer menu."
177 (let ((buffer (current-buffer)))
178 ;; Restore previous window configuration before displaying
180 (switch-to-buffer (other-buffer))
181 (bury-buffer buffer
)))
183 (defun Buffer-menu-mark ()
184 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
187 (if (looking-at " [-M]")
189 (let ((buffer-read-only nil
))
194 (defun Buffer-menu-unmark (&optional backup
)
195 "Cancel all requested operations on buffer on this line and move down.
196 Optional ARG means move up."
199 (if (looking-at " [-M]")
201 (let* ((buf (Buffer-menu-buffer t
))
202 (mod (buffer-modified-p buf
))
203 (readonly (save-excursion (set-buffer buf
) buffer-read-only
))
204 (buffer-read-only nil
))
206 (insert (if readonly
(if mod
" *%" " %") (if mod
" * " " ")))))
207 (forward-line (if backup -
1 1)))
209 (defun Buffer-menu-backup-unmark ()
210 "Move up and cancel all requested operations on buffer on line above."
216 (defun Buffer-menu-delete (&optional arg
)
217 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
218 Prefix arg is how many buffers to delete.
219 Negative arg means delete backwards."
222 (if (looking-at " [-M]") ;header lines
224 (let ((buffer-read-only nil
))
225 (if (or (null arg
) (= arg
0))
236 (setq arg
(1+ arg
))))))
238 (defun Buffer-menu-delete-backwards (&optional arg
)
239 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
240 and then move up one line. Prefix arg means move that many lines."
242 (Buffer-menu-delete (- (or arg
1)))
243 (while (looking-at " [-M]")
246 (defun Buffer-menu-save ()
247 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
251 (if (looking-at " [-M]") ;header lines
253 (let ((buffer-read-only nil
))
258 (defun Buffer-menu-not-modified (&optional arg
)
259 "Mark buffer on this line as unmodified (no changes to save)."
262 (set-buffer (Buffer-menu-buffer t
))
263 (set-buffer-modified-p arg
))
267 (if (= (char-after (point)) (if arg ? ?
*))
268 (let ((buffer-read-only nil
))
270 (insert (if arg ?
* ?
))))))
272 (defun Buffer-menu-execute ()
273 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
276 (goto-char (point-min))
278 (while (re-search-forward "^.S" nil t
)
281 (set-buffer (Buffer-menu-buffer t
))
283 (setq modp
(buffer-modified-p)))
284 (let ((buffer-read-only nil
))
286 (insert (if modp ?
* ?
))))))
288 (goto-char (point-min))
290 (let ((buff-menu-buffer (current-buffer))
291 (buffer-read-only nil
))
292 (while (search-forward "\nD" nil t
)
294 (let ((buf (Buffer-menu-buffer nil
)))
296 (eq buf buff-menu-buffer
)
297 (save-excursion (kill-buffer buf
))))
298 (if (Buffer-menu-buffer nil
)
299 (progn (delete-char 1)
301 (delete-region (point) (progn (forward-line 1) (point)))
302 (forward-char -
1))))))
304 (defun Buffer-menu-select ()
305 "Select this line's buffer; also display buffers marked with `>'.
306 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
308 (let ((buff (Buffer-menu-buffer t
))
309 (menu (current-buffer))
312 (goto-char (point-min))
313 (while (search-forward "\n>" nil t
)
314 (setq tem
(Buffer-menu-buffer t
))
315 (let ((buffer-read-only nil
))
318 (or (eq tem buff
) (memq tem others
) (setq others
(cons tem others
))))
319 (setq others
(nreverse others
)
320 tem
(/ (1- (frame-height)) (1+ (length others
))))
321 (delete-other-windows)
322 (switch-to-buffer buff
)
325 (if (equal (length others
) 0)
327 ;;; ;; Restore previous window configuration before displaying
328 ;;; ;; selected buffers.
329 ;;; (if Buffer-menu-window-config
331 ;;; (set-window-configuration Buffer-menu-window-config)
332 ;;; (setq Buffer-menu-window-config nil)))
333 (switch-to-buffer buff
))
335 (split-window nil tem
)
337 (switch-to-buffer (car others
))
338 (setq others
(cdr others
)))
339 (other-window 1) ;back to the beginning!
343 (defun Buffer-menu-visit-tags-table ()
344 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
346 (let ((file (buffer-file-name (Buffer-menu-buffer t
))))
348 (visit-tags-table file
)
349 (error "Specified buffer has no file"))))
351 (defun Buffer-menu-1-window ()
352 "Select this line's buffer, alone, in full frame."
354 (switch-to-buffer (Buffer-menu-buffer t
))
355 (bury-buffer (other-buffer))
356 (delete-other-windows))
358 (defun Buffer-menu-mouse-select (event)
359 "Select the buffer whose line you click on."
363 (set-buffer (window-buffer (posn-window (event-end event
))))
365 (goto-char (posn-point (event-end event
)))
366 (setq buffer
(Buffer-menu-buffer t
))))
367 (select-window (posn-window (event-end event
)))
368 (if (and (window-dedicated-p (selected-window))
369 (eq (selected-window) (frame-root-window)))
370 (switch-to-buffer-other-frame buffer
)
371 (switch-to-buffer buffer
))))
373 (defun Buffer-menu-this-window ()
374 "Select this line's buffer in this window."
376 (switch-to-buffer (Buffer-menu-buffer t
)))
378 (defun Buffer-menu-other-window ()
379 "Select this line's buffer in other window, leaving buffer menu visible."
381 (switch-to-buffer-other-window (Buffer-menu-buffer t
)))
383 (defun Buffer-menu-switch-other-window ()
384 "Make the other window select this line's buffer.
385 The current window remains selected."
387 (display-buffer (Buffer-menu-buffer t
)))
389 (defun Buffer-menu-2-window ()
390 "Select this line's buffer, with previous buffer in second window."
392 (let ((buff (Buffer-menu-buffer t
))
393 (menu (current-buffer))
395 (delete-other-windows)
396 (switch-to-buffer (other-buffer))
400 (defun Buffer-menu-toggle-read-only ()
401 "Toggle read-only status of buffer on this line, perhaps via version control."
405 (set-buffer (Buffer-menu-buffer t
))
406 (vc-toggle-read-only)
407 (setq char
(if buffer-read-only ?% ?
)))
411 (if (/= (following-char) char
)
412 (let (buffer-read-only)
416 ;;; buff-menu.el ends here