Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; buff-menu.el --- buffer menu main function and support functions. |
2 | ||
4fbc6fa6 | 3 | ;; Copyright (C) 1985, 1986, 1987, 1993 Free Software Foundation, Inc. |
2dd96f23 | 4 | |
9750e079 ER |
5 | ;; Maintainer: FSF |
6 | ||
2dd96f23 JB |
7 | ;; This file is part of GNU Emacs. |
8 | ||
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 | |
35aaf00c | 11 | ;; the Free Software Foundation; either version 2, or (at your option) |
2dd96f23 JB |
12 | ;; any later version. |
13 | ||
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. | |
18 | ||
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. | |
22 | ||
e41b2db1 ER |
23 | ;;; Commentary: |
24 | ||
25 | ;; Edit, delete, or change attributes of all currently active Emacs | |
26 | ;; buffers from a list summarizing thir state. A good way to browse | |
27 | ;; any special or scratch buffers you have loaded, since you can't find | |
86dbbfc2 ER |
28 | ;; them by filename. The single entry point is `Buffer-menu-mode', |
29 | ;; normally bound to C-x C-b. | |
30 | ||
31 | ;;; Change Log: | |
32 | ||
33 | ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993 | |
34 | ;; | |
86dbbfc2 ER |
35 | ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89 |
36 | ;; | |
37 | ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete | |
38 | ;; current entry and then move to previous one. | |
39 | ;; | |
40 | ;; Based on FSF code dating back to 1985. | |
e41b2db1 | 41 | |
5de9bdab | 42 | ;;; Code: |
86dbbfc2 | 43 | |
3ed788ec RS |
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'.") | |
2dd96f23 JB |
47 | |
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. | |
50 | ||
51 | (defvar Buffer-menu-mode-map nil "") | |
52 | ||
53 | (if Buffer-menu-mode-map | |
54 | () | |
55 | (setq Buffer-menu-mode-map (make-keymap)) | |
56 | (suppress-keymap Buffer-menu-mode-map t) | |
3ed788ec RS |
57 | (define-key Buffer-menu-mode-map "q" 'Buffer-menu-quit) |
58 | (define-key Buffer-menu-mode-map "v" 'Buffer-menu-select) | |
2dd96f23 JB |
59 | (define-key Buffer-menu-mode-map "2" 'Buffer-menu-2-window) |
60 | (define-key Buffer-menu-mode-map "1" 'Buffer-menu-1-window) | |
61 | (define-key Buffer-menu-mode-map "f" 'Buffer-menu-this-window) | |
62 | (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window) | |
35aaf00c | 63 | (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window) |
2dd96f23 JB |
64 | (define-key Buffer-menu-mode-map "s" 'Buffer-menu-save) |
65 | (define-key Buffer-menu-mode-map "d" 'Buffer-menu-delete) | |
66 | (define-key Buffer-menu-mode-map "k" 'Buffer-menu-delete) | |
67 | (define-key Buffer-menu-mode-map "\C-d" 'Buffer-menu-delete-backwards) | |
68 | (define-key Buffer-menu-mode-map "\C-k" 'Buffer-menu-delete) | |
69 | (define-key Buffer-menu-mode-map "x" 'Buffer-menu-execute) | |
70 | (define-key Buffer-menu-mode-map " " 'next-line) | |
71 | (define-key Buffer-menu-mode-map "n" 'next-line) | |
72 | (define-key Buffer-menu-mode-map "p" 'previous-line) | |
73 | (define-key Buffer-menu-mode-map "\177" 'Buffer-menu-backup-unmark) | |
74 | (define-key Buffer-menu-mode-map "~" 'Buffer-menu-not-modified) | |
75 | (define-key Buffer-menu-mode-map "?" 'describe-mode) | |
76 | (define-key Buffer-menu-mode-map "u" 'Buffer-menu-unmark) | |
77 | (define-key Buffer-menu-mode-map "m" 'Buffer-menu-mark) | |
d91e2b1a ER |
78 | (define-key Buffer-menu-mode-map "t" 'Buffer-menu-visit-tags-table) |
79 | (define-key Buffer-menu-mode-map "%" 'Buffer-menu-toggle-read-only) | |
80 | ) | |
2dd96f23 JB |
81 | |
82 | ;; Buffer Menu mode is suitable only for specially formatted data. | |
83 | (put 'Buffer-menu-mode 'mode-class 'special) | |
84 | ||
85 | (defun Buffer-menu-mode () | |
86 | "Major mode for editing a list of buffers. | |
87 | Each line describes one of the buffers in Emacs. | |
88 | Letters do not insert themselves; instead, they are commands. | |
89 | \\<Buffer-menu-mode-map> | |
90 | \\[Buffer-menu-mark] -- mark buffer to be displayed. | |
91 | \\[Buffer-menu-select] -- select buffer of line point is on. | |
92 | Also show buffers marked with m in other windows. | |
dc6d9681 | 93 | \\[Buffer-menu-1-window] -- select that buffer in full-frame window. |
2dd96f23 JB |
94 | \\[Buffer-menu-2-window] -- select that buffer in one window, |
95 | together with buffer selected before this one in another window. | |
96 | \\[Buffer-menu-this-window] -- select that buffer in place of the buffer menu buffer. | |
97 | \\[Buffer-menu-other-window] -- select that buffer in another window, | |
98 | so the buffer menu buffer remains visible in its window. | |
35aaf00c | 99 | \\[Buffer-menu-switch-other-window] -- switch the other window to this buffer. |
2dd96f23 JB |
100 | \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer. |
101 | \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer. | |
102 | \\[Buffer-menu-save] -- mark that buffer to be saved, and move down. | |
103 | \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down. | |
104 | \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up. | |
105 | \\[Buffer-menu-execute] -- delete or save marked buffers. | |
106 | \\[Buffer-menu-unmark] -- remove all kinds of marks from current line. | |
86dbbfc2 | 107 | With prefix argument, also move up one line. |
d91e2b1a ER |
108 | \\[Buffer-menu-backup-unmark] -- back up a line and remove marks. |
109 | \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line." | |
2dd96f23 JB |
110 | (kill-all-local-variables) |
111 | (use-local-map Buffer-menu-mode-map) | |
112 | (setq truncate-lines t) | |
113 | (setq buffer-read-only t) | |
114 | (setq major-mode 'Buffer-menu-mode) | |
115 | (setq mode-name "Buffer Menu") | |
116 | (run-hooks 'buffer-menu-mode-hook)) | |
117 | \f | |
86dbbfc2 ER |
118 | (defvar Buffer-menu-buffer-column nil) |
119 | ||
120 | (defvar Buffer-menu-size-column nil) | |
2dd96f23 JB |
121 | |
122 | (defun Buffer-menu-buffer (error-if-non-existent-p) | |
123 | "Return buffer described by this line of buffer menu." | |
86dbbfc2 ER |
124 | (if (null Buffer-menu-buffer-column) |
125 | (save-excursion | |
126 | (goto-char (point-min)) | |
127 | (search-forward "Buffer") | |
128 | (backward-word 1) | |
129 | (setq Buffer-menu-buffer-column (current-column)) | |
130 | (search-forward "Size") | |
131 | (backward-word 1) | |
132 | (setq Buffer-menu-size-column (current-column)))) | |
2dd96f23 JB |
133 | (save-excursion |
134 | (beginning-of-line) | |
135 | (forward-char Buffer-menu-buffer-column) | |
136 | (let ((start (point)) | |
137 | string) | |
138 | ;; End of buffer name marked by tab or two spaces. | |
139 | (re-search-forward "\t\\| ") | |
140 | (skip-chars-backward " \t") | |
141 | (setq string (buffer-substring start (point))) | |
142 | (or (get-buffer string) | |
143 | (if error-if-non-existent-p | |
144 | (error "No buffer named \"%s\"" string) | |
145 | nil))))) | |
146 | \f | |
06b1a5ef | 147 | (defun buffer-menu (&optional arg) |
2dd96f23 JB |
148 | "Make a menu of buffers so you can save, delete or select them. |
149 | With argument, show only buffers that are visiting files. | |
150 | Type ? after invocation to get help on commands available. | |
86dbbfc2 ER |
151 | Type q immediately to make the buffer menu go away and to restore |
152 | previous window configuration." | |
2dd96f23 | 153 | (interactive "P") |
3ed788ec | 154 | ;;; (setq Buffer-menu-window-config (current-window-configuration)) |
2dd96f23 JB |
155 | (list-buffers arg) |
156 | (pop-to-buffer "*Buffer List*") | |
157 | (forward-line 2) | |
158 | (message | |
3ed788ec RS |
159 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) |
160 | ||
161 | (defun Buffer-menu-quit () | |
162 | "Quit the buffer menu." | |
163 | (interactive) | |
164 | (let ((buffer (current-buffer))) | |
165 | ;; Restore previous window configuration before displaying | |
166 | ;; selected buffers. | |
167 | (switch-to-buffer (other-buffer)) | |
168 | (bury-buffer buffer))) | |
2dd96f23 JB |
169 | |
170 | (defun Buffer-menu-mark () | |
171 | "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command." | |
172 | (interactive) | |
173 | (beginning-of-line) | |
174 | (if (looking-at " [-M]") | |
175 | (ding) | |
176 | (let ((buffer-read-only nil)) | |
177 | (delete-char 1) | |
178 | (insert ?>) | |
179 | (forward-line 1)))) | |
180 | ||
86dbbfc2 ER |
181 | (defun Buffer-menu-unmark (&optional backup) |
182 | "Cancel all requested operations on buffer on this line and move down. | |
183 | Optional ARG means move up." | |
184 | (interactive "P") | |
2dd96f23 JB |
185 | (beginning-of-line) |
186 | (if (looking-at " [-M]") | |
187 | (ding) | |
188 | (let* ((buf (Buffer-menu-buffer t)) | |
189 | (mod (buffer-modified-p buf)) | |
190 | (readonly (save-excursion (set-buffer buf) buffer-read-only)) | |
191 | (buffer-read-only nil)) | |
192 | (delete-char 3) | |
193 | (insert (if readonly (if mod " *%" " %") (if mod " * " " "))))) | |
86dbbfc2 | 194 | (forward-line (if backup -1 1))) |
2dd96f23 JB |
195 | |
196 | (defun Buffer-menu-backup-unmark () | |
197 | "Move up and cancel all requested operations on buffer on line above." | |
198 | (interactive) | |
199 | (forward-line -1) | |
200 | (Buffer-menu-unmark) | |
201 | (forward-line -1)) | |
202 | ||
203 | (defun Buffer-menu-delete () | |
204 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command." | |
205 | (interactive) | |
206 | (beginning-of-line) | |
207 | (if (looking-at " [-M]") ;header lines | |
208 | (ding) | |
209 | (let ((buffer-read-only nil)) | |
210 | (delete-char 1) | |
211 | (insert ?D) | |
212 | (forward-line 1)))) | |
213 | ||
214 | (defun Buffer-menu-delete-backwards () | |
215 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command | |
216 | and then move up one line" | |
217 | (interactive) | |
218 | (Buffer-menu-delete) | |
219 | (forward-line -2) | |
220 | (if (looking-at " [-M]") (forward-line 1))) | |
221 | ||
222 | (defun Buffer-menu-save () | |
223 | "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command." | |
224 | (interactive) | |
225 | (beginning-of-line) | |
226 | (forward-char 1) | |
227 | (if (looking-at " [-M]") ;header lines | |
228 | (ding) | |
229 | (let ((buffer-read-only nil)) | |
230 | (delete-char 1) | |
231 | (insert ?S) | |
232 | (forward-line 1)))) | |
233 | ||
234 | (defun Buffer-menu-not-modified () | |
235 | "Mark buffer on this line as unmodified (no changes to save)." | |
236 | (interactive) | |
237 | (save-excursion | |
238 | (set-buffer (Buffer-menu-buffer t)) | |
239 | (set-buffer-modified-p nil)) | |
240 | (save-excursion | |
241 | (beginning-of-line) | |
242 | (forward-char 1) | |
243 | (if (looking-at "\\*") | |
244 | (let ((buffer-read-only nil)) | |
245 | (delete-char 1) | |
246 | (insert ? ))))) | |
247 | ||
248 | (defun Buffer-menu-execute () | |
249 | "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands." | |
250 | (interactive) | |
251 | (save-excursion | |
252 | (goto-char (point-min)) | |
253 | (forward-line 1) | |
254 | (while (re-search-forward "^.S" nil t) | |
255 | (let ((modp nil)) | |
256 | (save-excursion | |
257 | (set-buffer (Buffer-menu-buffer t)) | |
258 | (save-buffer) | |
259 | (setq modp (buffer-modified-p))) | |
260 | (let ((buffer-read-only nil)) | |
261 | (delete-char -1) | |
262 | (insert (if modp ?* ? )))))) | |
263 | (save-excursion | |
264 | (goto-char (point-min)) | |
265 | (forward-line 1) | |
266 | (let ((buff-menu-buffer (current-buffer)) | |
267 | (buffer-read-only nil)) | |
268 | (while (search-forward "\nD" nil t) | |
269 | (forward-char -1) | |
270 | (let ((buf (Buffer-menu-buffer nil))) | |
271 | (or (eq buf nil) | |
272 | (eq buf buff-menu-buffer) | |
273 | (save-excursion (kill-buffer buf)))) | |
274 | (if (Buffer-menu-buffer nil) | |
275 | (progn (delete-char 1) | |
276 | (insert ? )) | |
277 | (delete-region (point) (progn (forward-line 1) (point))) | |
278 | (forward-char -1)))))) | |
279 | ||
280 | (defun Buffer-menu-select () | |
281 | "Select this line's buffer; also display buffers marked with `>'. | |
282 | You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command." | |
283 | (interactive) | |
284 | (let ((buff (Buffer-menu-buffer t)) | |
285 | (menu (current-buffer)) | |
286 | (others ()) | |
287 | tem) | |
288 | (goto-char (point-min)) | |
289 | (while (search-forward "\n>" nil t) | |
290 | (setq tem (Buffer-menu-buffer t)) | |
291 | (let ((buffer-read-only nil)) | |
292 | (delete-char -1) | |
293 | (insert ?\ )) | |
294 | (or (eq tem buff) (memq tem others) (setq others (cons tem others)))) | |
295 | (setq others (nreverse others) | |
dc6d9681 | 296 | tem (/ (1- (frame-height)) (1+ (length others)))) |
2dd96f23 JB |
297 | (delete-other-windows) |
298 | (switch-to-buffer buff) | |
299 | (or (eq menu buff) | |
300 | (bury-buffer menu)) | |
86dbbfc2 ER |
301 | (if (equal (length others) 0) |
302 | (progn | |
3ed788ec RS |
303 | ;;; ;; Restore previous window configuration before displaying |
304 | ;;; ;; selected buffers. | |
305 | ;;; (if Buffer-menu-window-config | |
306 | ;;; (progn | |
307 | ;;; (set-window-configuration Buffer-menu-window-config) | |
308 | ;;; (setq Buffer-menu-window-config nil))) | |
86dbbfc2 ER |
309 | (switch-to-buffer buff)) |
310 | (while others | |
311 | (split-window nil tem) | |
312 | (other-window 1) | |
313 | (switch-to-buffer (car others)) | |
314 | (setq others (cdr others))) | |
315 | (other-window 1) ;back to the beginning! | |
316 | ))) | |
317 | ||
2dd96f23 JB |
318 | \f |
319 | (defun Buffer-menu-visit-tags-table () | |
320 | "Visit the tags table in the buffer on this line. See `visit-tags-table'." | |
321 | (interactive) | |
322 | (let ((file (buffer-file-name (Buffer-menu-buffer t)))) | |
323 | (if file | |
324 | (visit-tags-table file) | |
325 | (error "Specified buffer has no file")))) | |
326 | ||
327 | (defun Buffer-menu-1-window () | |
dc6d9681 | 328 | "Select this line's buffer, alone, in full frame." |
2dd96f23 JB |
329 | (interactive) |
330 | (switch-to-buffer (Buffer-menu-buffer t)) | |
331 | (bury-buffer (other-buffer)) | |
332 | (delete-other-windows)) | |
333 | ||
334 | (defun Buffer-menu-this-window () | |
335 | "Select this line's buffer in this window." | |
336 | (interactive) | |
337 | (switch-to-buffer (Buffer-menu-buffer t))) | |
338 | ||
339 | (defun Buffer-menu-other-window () | |
340 | "Select this line's buffer in other window, leaving buffer menu visible." | |
341 | (interactive) | |
342 | (switch-to-buffer-other-window (Buffer-menu-buffer t))) | |
343 | ||
35aaf00c RM |
344 | (defun Buffer-menu-switch-other-window () |
345 | "Make the other window select this line's buffer. | |
346 | The current window remains selected." | |
347 | (interactive) | |
348 | (display-buffer (Buffer-menu-buffer t))) | |
349 | ||
2dd96f23 JB |
350 | (defun Buffer-menu-2-window () |
351 | "Select this line's buffer, with previous buffer in second window." | |
352 | (interactive) | |
353 | (let ((buff (Buffer-menu-buffer t)) | |
354 | (menu (current-buffer)) | |
355 | (pop-up-windows t)) | |
86dbbfc2 | 356 | (delete-other-windows) |
2dd96f23 JB |
357 | (switch-to-buffer (other-buffer)) |
358 | (pop-to-buffer buff) | |
359 | (bury-buffer menu))) | |
c0274f38 | 360 | |
d91e2b1a ER |
361 | (defun Buffer-menu-toggle-read-only () |
362 | "Toggle read-only status of buffer on this line." | |
363 | (interactive) | |
364 | (let (char) | |
365 | (save-excursion | |
366 | (set-buffer (Buffer-menu-buffer t)) | |
367 | (toggle-read-only) | |
368 | (setq char (if buffer-read-only ?% ? ))) | |
369 | (save-excursion | |
370 | (beginning-of-line) | |
371 | (forward-char 2) | |
372 | (if (/= (following-char) char) | |
373 | (let (buffer-read-only) | |
374 | (delete-char 1) | |
375 | (insert char)))))) | |
376 | ||
c0274f38 | 377 | ;;; buff-menu.el ends here |