1 ;;; buff-menu.el --- buffer menu main function and support functions.
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Author: Bob Weiner <rsw@cs.brown.edu>
7 ;; Adapted-by: Eric S. Raymond <esr@snark.thyrsus.com>
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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; Edit, delete, or change attributes of all currently active Emacs
28 ;; buffers from a list summarizing thir state. A good way to browse
29 ;; any special or scratch buffers you have loaded, since you can't find
30 ;; them by filename. The single entry point is `Buffer-menu-mode',
31 ;; normally bound to C-x C-b.
35 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
37 ;; Modified by Bob Weiner, Motorola, Inc., 3/1/89
39 ;; Save window configuration when 'buffer-menu' is called so that
40 ;; previous window configuration is restored. prior to selecting
42 ;; Made 'Buffer-menu-select' also perform a 'Buffer-menu-execute'.
44 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
46 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
47 ;; current entry and then move to previous one.
49 ;; Based on FSF code dating back to 1985.
53 (defvar *buff-window-config
* nil
54 "Stores window configuration upon entry of 'buffer-menu'. Used to
55 restore window configuration when only one buffer is selected.")
57 ; Put buffer *Buffer List* into proper mode right away
58 ; so that from now on even list-buffers is enough to get a buffer menu.
60 (defvar Buffer-menu-mode-map nil
"")
62 (if Buffer-menu-mode-map
64 (setq Buffer-menu-mode-map
(make-keymap))
65 (suppress-keymap Buffer-menu-mode-map t
)
66 (define-key Buffer-menu-mode-map
"q" 'Buffer-menu-select
)
67 (define-key Buffer-menu-mode-map
"2" 'Buffer-menu-2-window
)
68 (define-key Buffer-menu-mode-map
"1" 'Buffer-menu-1-window
)
69 (define-key Buffer-menu-mode-map
"f" 'Buffer-menu-this-window
)
70 (define-key Buffer-menu-mode-map
"o" 'Buffer-menu-other-window
)
71 (define-key Buffer-menu-mode-map
"\C-o" 'Buffer-menu-switch-other-window
)
72 (define-key Buffer-menu-mode-map
"s" 'Buffer-menu-save
)
73 (define-key Buffer-menu-mode-map
"d" 'Buffer-menu-delete
)
74 (define-key Buffer-menu-mode-map
"k" 'Buffer-menu-delete
)
75 (define-key Buffer-menu-mode-map
"\C-d" 'Buffer-menu-delete-backwards
)
76 (define-key Buffer-menu-mode-map
"\C-k" 'Buffer-menu-delete
)
77 (define-key Buffer-menu-mode-map
"x" 'Buffer-menu-execute
)
78 (define-key Buffer-menu-mode-map
" " 'next-line
)
79 (define-key Buffer-menu-mode-map
"n" 'next-line
)
80 (define-key Buffer-menu-mode-map
"p" 'previous-line
)
81 (define-key Buffer-menu-mode-map
"\177" 'Buffer-menu-backup-unmark
)
82 (define-key Buffer-menu-mode-map
"~" 'Buffer-menu-not-modified
)
83 (define-key Buffer-menu-mode-map
"?" 'describe-mode
)
84 (define-key Buffer-menu-mode-map
"u" 'Buffer-menu-unmark
)
85 (define-key Buffer-menu-mode-map
"m" 'Buffer-menu-mark
)
86 (define-key Buffer-menu-mode-map
"t" 'Buffer-menu-visit-tags-table
)
87 (define-key Buffer-menu-mode-map
"%" 'Buffer-menu-toggle-read-only
)
90 ;; Buffer Menu mode is suitable only for specially formatted data.
91 (put 'Buffer-menu-mode
'mode-class
'special
)
93 (defun Buffer-menu-mode ()
94 "Major mode for editing a list of buffers.
95 Each line describes one of the buffers in Emacs.
96 Letters do not insert themselves; instead, they are commands.
97 \\<Buffer-menu-mode-map>
98 \\[Buffer-menu-mark] -- mark buffer to be displayed.
99 \\[Buffer-menu-select] -- select buffer of line point is on.
100 Also show buffers marked with m in other windows.
101 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
102 \\[Buffer-menu-2-window] -- select that buffer in one window,
103 together with buffer selected before this one in another window.
104 \\[Buffer-menu-this-window] -- select that buffer in place of the buffer menu buffer.
105 \\[Buffer-menu-other-window] -- select that buffer in another window,
106 so the buffer menu buffer remains visible in its window.
107 \\[Buffer-menu-switch-other-window] -- switch the other window to this buffer.
108 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
109 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
110 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
111 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
112 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
113 \\[Buffer-menu-execute] -- delete or save marked buffers.
114 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
115 With prefix argument, also move up one line.
116 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
117 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line."
118 (kill-all-local-variables)
119 (use-local-map Buffer-menu-mode-map
)
120 (setq truncate-lines t
)
121 (setq buffer-read-only t
)
122 (setq major-mode
'Buffer-menu-mode
)
123 (setq mode-name
"Buffer Menu")
124 (run-hooks 'buffer-menu-mode-hook
))
126 (defvar Buffer-menu-buffer-column nil
)
128 (defvar Buffer-menu-size-column nil
)
130 (defun Buffer-menu-buffer (error-if-non-existent-p)
131 "Return buffer described by this line of buffer menu."
132 (if (null Buffer-menu-buffer-column
)
134 (goto-char (point-min))
135 (search-forward "Buffer")
137 (setq Buffer-menu-buffer-column
(current-column))
138 (search-forward "Size")
140 (setq Buffer-menu-size-column
(current-column))))
143 (forward-char Buffer-menu-buffer-column
)
144 (let ((start (point))
146 ;; End of buffer name marked by tab or two spaces.
147 (re-search-forward "\t\\| ")
148 (skip-chars-backward " \t")
149 (setq string
(buffer-substring start
(point)))
150 (or (get-buffer string
)
151 (if error-if-non-existent-p
152 (error "No buffer named \"%s\"" string
)
155 (defun buffer-menu (&optional arg
)
156 "Make a menu of buffers so you can save, delete or select them.
157 With argument, show only buffers that are visiting files.
158 Type ? after invocation to get help on commands available.
159 Type q immediately to make the buffer menu go away and to restore
160 previous window configuration."
162 (setq *buff-window-config
* (current-window-configuration))
164 (pop-to-buffer "*Buffer List*")
167 "Commands: d, s, x; 1, 2, m, u; delete; ~; q to quit; ? for help."))
169 (defun Buffer-menu-mark ()
170 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
173 (if (looking-at " [-M]")
175 (let ((buffer-read-only nil
))
180 (defun Buffer-menu-unmark (&optional backup
)
181 "Cancel all requested operations on buffer on this line and move down.
182 Optional ARG means move up."
185 (if (looking-at " [-M]")
187 (let* ((buf (Buffer-menu-buffer t
))
188 (mod (buffer-modified-p buf
))
189 (readonly (save-excursion (set-buffer buf
) buffer-read-only
))
190 (buffer-read-only nil
))
192 (insert (if readonly
(if mod
" *%" " %") (if mod
" * " " ")))))
193 (forward-line (if backup -
1 1)))
195 (defun Buffer-menu-backup-unmark ()
196 "Move up and cancel all requested operations on buffer on line above."
202 (defun Buffer-menu-delete ()
203 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
206 (if (looking-at " [-M]") ;header lines
208 (let ((buffer-read-only nil
))
213 (defun Buffer-menu-delete-backwards ()
214 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
215 and then move up one line"
219 (if (looking-at " [-M]") (forward-line 1)))
221 (defun Buffer-menu-save ()
222 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
226 (if (looking-at " [-M]") ;header lines
228 (let ((buffer-read-only nil
))
233 (defun Buffer-menu-not-modified ()
234 "Mark buffer on this line as unmodified (no changes to save)."
237 (set-buffer (Buffer-menu-buffer t
))
238 (set-buffer-modified-p nil
))
242 (if (looking-at "\\*")
243 (let ((buffer-read-only nil
))
247 (defun Buffer-menu-execute ()
248 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
251 (goto-char (point-min))
253 (while (re-search-forward "^.S" nil t
)
256 (set-buffer (Buffer-menu-buffer t
))
258 (setq modp
(buffer-modified-p)))
259 (let ((buffer-read-only nil
))
261 (insert (if modp ?
* ?
))))))
263 (goto-char (point-min))
265 (let ((buff-menu-buffer (current-buffer))
266 (buffer-read-only nil
))
267 (while (search-forward "\nD" nil t
)
269 (let ((buf (Buffer-menu-buffer nil
)))
271 (eq buf buff-menu-buffer
)
272 (save-excursion (kill-buffer buf
))))
273 (if (Buffer-menu-buffer nil
)
274 (progn (delete-char 1)
276 (delete-region (point) (progn (forward-line 1) (point)))
277 (forward-char -
1))))))
279 (defun Buffer-menu-select ()
280 "Select this line's buffer; also display buffers marked with `>'.
281 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
283 (let ((buff (Buffer-menu-buffer t
))
284 (menu (current-buffer))
287 (goto-char (point-min))
288 (while (search-forward "\n>" nil t
)
289 (setq tem
(Buffer-menu-buffer t
))
290 (let ((buffer-read-only nil
))
293 (or (eq tem buff
) (memq tem others
) (setq others
(cons tem others
))))
294 (setq others
(nreverse others
)
295 tem
(/ (1- (frame-height)) (1+ (length others
))))
296 (Buffer-menu-execute)
297 (delete-other-windows)
298 (switch-to-buffer buff
)
301 (if (equal (length others
) 0)
303 ;; Restore previous window configuration before displaying
305 (if *buff-window-config
*
307 (set-window-configuration *buff-window-config
*)
308 (setq *buff-window-config
* nil
)))
309 (switch-to-buffer buff
))
311 (split-window nil tem
)
313 (switch-to-buffer (car others
))
314 (setq others
(cdr others
)))
315 (other-window 1) ;back to the beginning!
319 (defun Buffer-menu-visit-tags-table ()
320 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
322 (let ((file (buffer-file-name (Buffer-menu-buffer t
))))
324 (visit-tags-table file
)
325 (error "Specified buffer has no file"))))
327 (defun Buffer-menu-1-window ()
328 "Select this line's buffer, alone, in full frame."
330 (switch-to-buffer (Buffer-menu-buffer t
))
331 (bury-buffer (other-buffer))
332 (delete-other-windows))
334 (defun Buffer-menu-this-window ()
335 "Select this line's buffer in this window."
337 (switch-to-buffer (Buffer-menu-buffer t
)))
339 (defun Buffer-menu-other-window ()
340 "Select this line's buffer in other window, leaving buffer menu visible."
342 (switch-to-buffer-other-window (Buffer-menu-buffer t
)))
344 (defun Buffer-menu-switch-other-window ()
345 "Make the other window select this line's buffer.
346 The current window remains selected."
348 (display-buffer (Buffer-menu-buffer t
)))
350 (defun Buffer-menu-2-window ()
351 "Select this line's buffer, with previous buffer in second window."
353 (let ((buff (Buffer-menu-buffer t
))
354 (menu (current-buffer))
356 (delete-other-windows)
357 (switch-to-buffer (other-buffer))
361 (defun Buffer-menu-toggle-read-only ()
362 "Toggle read-only status of buffer on this line."
366 (set-buffer (Buffer-menu-buffer t
))
368 (setq char
(if buffer-read-only ?% ?
)))
372 (if (/= (following-char) char
)
373 (let (buffer-read-only)
377 ;;; buff-menu.el ends here