1 ;;; buff-menu.el --- buffer menu main function and support functions.
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ; Put buffer *Buffer List* into proper mode right away
23 ; so that from now on even list-buffers is enough to get a buffer menu.
25 (defvar Buffer-menu-mode-map nil
"")
27 (if Buffer-menu-mode-map
29 (setq Buffer-menu-mode-map
(make-keymap))
30 (suppress-keymap Buffer-menu-mode-map t
)
31 (define-key Buffer-menu-mode-map
"q" 'Buffer-menu-select
)
32 (define-key Buffer-menu-mode-map
"2" 'Buffer-menu-2-window
)
33 (define-key Buffer-menu-mode-map
"1" 'Buffer-menu-1-window
)
34 (define-key Buffer-menu-mode-map
"f" 'Buffer-menu-this-window
)
35 (define-key Buffer-menu-mode-map
"o" 'Buffer-menu-other-window
)
36 (define-key Buffer-menu-mode-map
"\C-o" 'Buffer-menu-switch-other-window
)
37 (define-key Buffer-menu-mode-map
"s" 'Buffer-menu-save
)
38 (define-key Buffer-menu-mode-map
"d" 'Buffer-menu-delete
)
39 (define-key Buffer-menu-mode-map
"k" 'Buffer-menu-delete
)
40 (define-key Buffer-menu-mode-map
"\C-d" 'Buffer-menu-delete-backwards
)
41 (define-key Buffer-menu-mode-map
"\C-k" 'Buffer-menu-delete
)
42 (define-key Buffer-menu-mode-map
"x" 'Buffer-menu-execute
)
43 (define-key Buffer-menu-mode-map
" " 'next-line
)
44 (define-key Buffer-menu-mode-map
"n" 'next-line
)
45 (define-key Buffer-menu-mode-map
"p" 'previous-line
)
46 (define-key Buffer-menu-mode-map
"\177" 'Buffer-menu-backup-unmark
)
47 (define-key Buffer-menu-mode-map
"~" 'Buffer-menu-not-modified
)
48 (define-key Buffer-menu-mode-map
"?" 'describe-mode
)
49 (define-key Buffer-menu-mode-map
"u" 'Buffer-menu-unmark
)
50 (define-key Buffer-menu-mode-map
"m" 'Buffer-menu-mark
)
51 (define-key Buffer-menu-mode-map
"t" 'Buffer-menu-visit-tags-table
))
53 ;; Buffer Menu mode is suitable only for specially formatted data.
54 (put 'Buffer-menu-mode
'mode-class
'special
)
56 (defun Buffer-menu-mode ()
57 "Major mode for editing a list of buffers.
58 Each line describes one of the buffers in Emacs.
59 Letters do not insert themselves; instead, they are commands.
60 \\<Buffer-menu-mode-map>
61 \\[Buffer-menu-mark] -- mark buffer to be displayed.
62 \\[Buffer-menu-select] -- select buffer of line point is on.
63 Also show buffers marked with m in other windows.
64 \\[Buffer-menu-1-window] -- select that buffer in full-screen window.
65 \\[Buffer-menu-2-window] -- select that buffer in one window,
66 together with buffer selected before this one in another window.
67 \\[Buffer-menu-this-window] -- select that buffer in place of the buffer menu buffer.
68 \\[Buffer-menu-other-window] -- select that buffer in another window,
69 so the buffer menu buffer remains visible in its window.
70 \\[Buffer-menu-switch-other-window] -- switch the other window to this buffer.
71 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
72 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
73 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
74 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
75 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
76 \\[Buffer-menu-execute] -- delete or save marked buffers.
77 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
78 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks."
79 (kill-all-local-variables)
80 (use-local-map Buffer-menu-mode-map
)
81 (setq truncate-lines t
)
82 (setq buffer-read-only t
)
83 (setq major-mode
'Buffer-menu-mode
)
84 (setq mode-name
"Buffer Menu")
85 (run-hooks 'buffer-menu-mode-hook
))
87 (defvar Buffer-menu-buffer-column
4)
89 (defun Buffer-menu-buffer (error-if-non-existent-p)
90 "Return buffer described by this line of buffer menu."
93 (forward-char Buffer-menu-buffer-column
)
96 ;; End of buffer name marked by tab or two spaces.
97 (re-search-forward "\t\\| ")
98 (skip-chars-backward " \t")
99 (setq string
(buffer-substring start
(point)))
100 (or (get-buffer string
)
101 (if error-if-non-existent-p
102 (error "No buffer named \"%s\"" string
)
105 (defun buffer-menu (&optional arg
)
106 "Make a menu of buffers so you can save, delete or select them.
107 With argument, show only buffers that are visiting files.
108 Type ? after invocation to get help on commands available.
109 Type q immediately to make the buffer menu go away."
112 (pop-to-buffer "*Buffer List*")
115 "Commands: d, s, x; 1, 2, m, u, q; delete; ~; ? for help."))
117 (defun Buffer-menu-mark ()
118 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
121 (if (looking-at " [-M]")
123 (let ((buffer-read-only nil
))
128 (defun Buffer-menu-unmark ()
129 "Cancel all requested operations on buffer on this line."
132 (if (looking-at " [-M]")
134 (let* ((buf (Buffer-menu-buffer t
))
135 (mod (buffer-modified-p buf
))
136 (readonly (save-excursion (set-buffer buf
) buffer-read-only
))
137 (buffer-read-only nil
))
139 (insert (if readonly
(if mod
" *%" " %") (if mod
" * " " ")))))
142 (defun Buffer-menu-backup-unmark ()
143 "Move up and cancel all requested operations on buffer on line above."
149 (defun Buffer-menu-delete ()
150 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
153 (if (looking-at " [-M]") ;header lines
155 (let ((buffer-read-only nil
))
160 (defun Buffer-menu-delete-backwards ()
161 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
162 and then move up one line"
166 (if (looking-at " [-M]") (forward-line 1)))
168 (defun Buffer-menu-save ()
169 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
173 (if (looking-at " [-M]") ;header lines
175 (let ((buffer-read-only nil
))
180 (defun Buffer-menu-not-modified ()
181 "Mark buffer on this line as unmodified (no changes to save)."
184 (set-buffer (Buffer-menu-buffer t
))
185 (set-buffer-modified-p nil
))
189 (if (looking-at "\\*")
190 (let ((buffer-read-only nil
))
194 (defun Buffer-menu-execute ()
195 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
198 (goto-char (point-min))
200 (while (re-search-forward "^.S" nil t
)
203 (set-buffer (Buffer-menu-buffer t
))
205 (setq modp
(buffer-modified-p)))
206 (let ((buffer-read-only nil
))
208 (insert (if modp ?
* ?
))))))
210 (goto-char (point-min))
212 (let ((buff-menu-buffer (current-buffer))
213 (buffer-read-only nil
))
214 (while (search-forward "\nD" nil t
)
216 (let ((buf (Buffer-menu-buffer nil
)))
218 (eq buf buff-menu-buffer
)
219 (save-excursion (kill-buffer buf
))))
220 (if (Buffer-menu-buffer nil
)
221 (progn (delete-char 1)
223 (delete-region (point) (progn (forward-line 1) (point)))
224 (forward-char -
1))))))
226 (defun Buffer-menu-select ()
227 "Select this line's buffer; also display buffers marked with `>'.
228 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
230 (let ((buff (Buffer-menu-buffer t
))
231 (menu (current-buffer))
234 (goto-char (point-min))
235 (while (search-forward "\n>" nil t
)
236 (setq tem
(Buffer-menu-buffer t
))
237 (let ((buffer-read-only nil
))
240 (or (eq tem buff
) (memq tem others
) (setq others
(cons tem others
))))
241 (setq others
(nreverse others
)
242 tem
(/ (1- (screen-height)) (1+ (length others
))))
243 (delete-other-windows)
244 (switch-to-buffer buff
)
248 (split-window nil tem
)
250 (switch-to-buffer (car others
))
251 (setq others
(cdr others
)))
252 (other-window 1))) ;back to the beginning!
254 (defun Buffer-menu-visit-tags-table ()
255 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
257 (let ((file (buffer-file-name (Buffer-menu-buffer t
))))
259 (visit-tags-table file
)
260 (error "Specified buffer has no file"))))
262 (defun Buffer-menu-1-window ()
263 "Select this line's buffer, alone, in full screen."
265 (switch-to-buffer (Buffer-menu-buffer t
))
266 (bury-buffer (other-buffer))
267 (delete-other-windows))
269 (defun Buffer-menu-this-window ()
270 "Select this line's buffer in this window."
272 (switch-to-buffer (Buffer-menu-buffer t
)))
274 (defun Buffer-menu-other-window ()
275 "Select this line's buffer in other window, leaving buffer menu visible."
277 (switch-to-buffer-other-window (Buffer-menu-buffer t
)))
279 (defun Buffer-menu-switch-other-window ()
280 "Make the other window select this line's buffer.
281 The current window remains selected."
283 (display-buffer (Buffer-menu-buffer t
)))
285 (defun Buffer-menu-2-window ()
286 "Select this line's buffer, with previous buffer in second window."
288 (let ((buff (Buffer-menu-buffer t
))
289 (menu (current-buffer))
291 (switch-to-buffer (other-buffer))
295 ;;; buff-menu.el ends here