Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; buff-menu.el --- buffer menu main function and support functions. |
2 | ||
d733c5ec | 3 | ;; Copyright (C) 1985, 1986, 1987, 1993, 1994 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 | |
eb8c3be9 | 26 | ;; buffers from a list summarizing their state. A good way to browse |
e41b2db1 | 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 | ||
e5ea316b KH |
51 | (defvar Buffer-menu-buffer-column nil) |
52 | ||
2dd96f23 JB |
53 | (defvar Buffer-menu-mode-map nil "") |
54 | ||
55 | (if Buffer-menu-mode-map | |
56 | () | |
57 | (setq Buffer-menu-mode-map (make-keymap)) | |
58 | (suppress-keymap Buffer-menu-mode-map t) | |
3ed788ec RS |
59 | (define-key Buffer-menu-mode-map "q" 'Buffer-menu-quit) |
60 | (define-key Buffer-menu-mode-map "v" 'Buffer-menu-select) | |
2dd96f23 JB |
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) | |
35aaf00c | 65 | (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window) |
2dd96f23 JB |
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) | |
d91e2b1a ER |
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) | |
2cf5fc7e | 82 | (define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-mouse-select) |
d91e2b1a | 83 | ) |
2dd96f23 JB |
84 | |
85 | ;; Buffer Menu mode is suitable only for specially formatted data. | |
86 | (put 'Buffer-menu-mode 'mode-class 'special) | |
87 | ||
88 | (defun Buffer-menu-mode () | |
89 | "Major mode for editing a list of buffers. | |
90 | Each line describes one of the buffers in Emacs. | |
91 | Letters do not insert themselves; instead, they are commands. | |
92 | \\<Buffer-menu-mode-map> | |
2cf5fc7e RS |
93 | \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu. |
94 | \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu. | |
95 | \\[Buffer-menu-other-window] -- select that buffer in another window, | |
96 | so the buffer menu buffer remains visible in its window. | |
97 | \\[Buffer-menu-switch-other-window] -- make another window display that buffer. | |
98 | \\[Buffer-menu-mark] -- mark current line's buffer to be displayed. | |
99 | \\[Buffer-menu-select] -- select current line's buffer. | |
100 | Also show buffers marked with m, in other windows. | |
dc6d9681 | 101 | \\[Buffer-menu-1-window] -- select that buffer in full-frame window. |
2dd96f23 JB |
102 | \\[Buffer-menu-2-window] -- select that buffer in one window, |
103 | together with buffer selected before this one in another window. | |
2dd96f23 JB |
104 | \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer. |
105 | \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer. | |
106 | \\[Buffer-menu-save] -- mark that buffer to be saved, and move down. | |
107 | \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down. | |
108 | \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up. | |
109 | \\[Buffer-menu-execute] -- delete or save marked buffers. | |
110 | \\[Buffer-menu-unmark] -- remove all kinds of marks from current line. | |
86dbbfc2 | 111 | With prefix argument, also move up one line. |
d91e2b1a ER |
112 | \\[Buffer-menu-backup-unmark] -- back up a line and remove marks. |
113 | \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line." | |
2dd96f23 JB |
114 | (kill-all-local-variables) |
115 | (use-local-map Buffer-menu-mode-map) | |
2dd96f23 JB |
116 | (setq major-mode 'Buffer-menu-mode) |
117 | (setq mode-name "Buffer Menu") | |
823316aa KH |
118 | (save-excursion |
119 | (goto-char (point-min)) | |
e5ea316b KH |
120 | (search-forward "Buffer") |
121 | (backward-word 1) | |
122 | (setq Buffer-menu-buffer-column (current-column)) | |
823316aa KH |
123 | (forward-line 2) |
124 | (while (not (eobp)) | |
e5ea316b KH |
125 | (let ((where (Buffer-menu-buffer-name-position))) |
126 | (put-text-property (car where) (cdr where) 'mouse-face 'highlight)) | |
823316aa KH |
127 | (forward-line 1))) |
128 | (setq truncate-lines t) | |
129 | (setq buffer-read-only t) | |
2dd96f23 JB |
130 | (run-hooks 'buffer-menu-mode-hook)) |
131 | \f | |
2dd96f23 JB |
132 | (defun Buffer-menu-buffer (error-if-non-existent-p) |
133 | "Return buffer described by this line of buffer menu." | |
e5ea316b KH |
134 | (let* ((where (Buffer-menu-buffer-name-position)) |
135 | (string (buffer-substring (car where) (cdr where)))) | |
136 | (or (get-buffer string) | |
137 | (if error-if-non-existent-p | |
138 | (error "No buffer named \"%s\"" string) | |
139 | nil)))) | |
140 | ||
141 | ;; Find the start and end positions of the buffer name on this line. | |
142 | ;; Returns a cons (START . END). | |
143 | (defun Buffer-menu-buffer-name-position () | |
2dd96f23 JB |
144 | (save-excursion |
145 | (beginning-of-line) | |
146 | (forward-char Buffer-menu-buffer-column) | |
e5ea316b | 147 | (let ((start (point))) |
2dd96f23 JB |
148 | (re-search-forward "\t\\| ") |
149 | (skip-chars-backward " \t") | |
e5ea316b | 150 | (cons start (point))))) |
2dd96f23 | 151 | \f |
06b1a5ef | 152 | (defun buffer-menu (&optional arg) |
2dd96f23 JB |
153 | "Make a menu of buffers so you can save, delete or select them. |
154 | With argument, show only buffers that are visiting files. | |
155 | Type ? after invocation to get help on commands available. | |
86dbbfc2 ER |
156 | Type q immediately to make the buffer menu go away and to restore |
157 | previous window configuration." | |
2dd96f23 | 158 | (interactive "P") |
3ed788ec | 159 | ;;; (setq Buffer-menu-window-config (current-window-configuration)) |
2dd96f23 JB |
160 | (list-buffers arg) |
161 | (pop-to-buffer "*Buffer List*") | |
162 | (forward-line 2) | |
163 | (message | |
3ed788ec RS |
164 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) |
165 | ||
166 | (defun Buffer-menu-quit () | |
167 | "Quit the buffer menu." | |
168 | (interactive) | |
169 | (let ((buffer (current-buffer))) | |
170 | ;; Restore previous window configuration before displaying | |
171 | ;; selected buffers. | |
172 | (switch-to-buffer (other-buffer)) | |
173 | (bury-buffer buffer))) | |
2dd96f23 JB |
174 | |
175 | (defun Buffer-menu-mark () | |
176 | "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command." | |
177 | (interactive) | |
178 | (beginning-of-line) | |
179 | (if (looking-at " [-M]") | |
180 | (ding) | |
181 | (let ((buffer-read-only nil)) | |
182 | (delete-char 1) | |
183 | (insert ?>) | |
184 | (forward-line 1)))) | |
185 | ||
86dbbfc2 ER |
186 | (defun Buffer-menu-unmark (&optional backup) |
187 | "Cancel all requested operations on buffer on this line and move down. | |
188 | Optional ARG means move up." | |
189 | (interactive "P") | |
2dd96f23 JB |
190 | (beginning-of-line) |
191 | (if (looking-at " [-M]") | |
192 | (ding) | |
193 | (let* ((buf (Buffer-menu-buffer t)) | |
194 | (mod (buffer-modified-p buf)) | |
195 | (readonly (save-excursion (set-buffer buf) buffer-read-only)) | |
196 | (buffer-read-only nil)) | |
197 | (delete-char 3) | |
198 | (insert (if readonly (if mod " *%" " %") (if mod " * " " "))))) | |
86dbbfc2 | 199 | (forward-line (if backup -1 1))) |
2dd96f23 JB |
200 | |
201 | (defun Buffer-menu-backup-unmark () | |
202 | "Move up and cancel all requested operations on buffer on line above." | |
203 | (interactive) | |
204 | (forward-line -1) | |
205 | (Buffer-menu-unmark) | |
206 | (forward-line -1)) | |
207 | ||
208 | (defun Buffer-menu-delete () | |
209 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command." | |
210 | (interactive) | |
211 | (beginning-of-line) | |
212 | (if (looking-at " [-M]") ;header lines | |
213 | (ding) | |
214 | (let ((buffer-read-only nil)) | |
215 | (delete-char 1) | |
216 | (insert ?D) | |
217 | (forward-line 1)))) | |
218 | ||
219 | (defun Buffer-menu-delete-backwards () | |
220 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command | |
221 | and then move up one line" | |
222 | (interactive) | |
223 | (Buffer-menu-delete) | |
224 | (forward-line -2) | |
225 | (if (looking-at " [-M]") (forward-line 1))) | |
226 | ||
227 | (defun Buffer-menu-save () | |
228 | "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command." | |
229 | (interactive) | |
230 | (beginning-of-line) | |
231 | (forward-char 1) | |
232 | (if (looking-at " [-M]") ;header lines | |
233 | (ding) | |
234 | (let ((buffer-read-only nil)) | |
235 | (delete-char 1) | |
236 | (insert ?S) | |
237 | (forward-line 1)))) | |
238 | ||
239 | (defun Buffer-menu-not-modified () | |
240 | "Mark buffer on this line as unmodified (no changes to save)." | |
241 | (interactive) | |
242 | (save-excursion | |
243 | (set-buffer (Buffer-menu-buffer t)) | |
244 | (set-buffer-modified-p nil)) | |
245 | (save-excursion | |
246 | (beginning-of-line) | |
247 | (forward-char 1) | |
248 | (if (looking-at "\\*") | |
249 | (let ((buffer-read-only nil)) | |
250 | (delete-char 1) | |
251 | (insert ? ))))) | |
252 | ||
253 | (defun Buffer-menu-execute () | |
254 | "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands." | |
255 | (interactive) | |
256 | (save-excursion | |
257 | (goto-char (point-min)) | |
258 | (forward-line 1) | |
259 | (while (re-search-forward "^.S" nil t) | |
260 | (let ((modp nil)) | |
261 | (save-excursion | |
262 | (set-buffer (Buffer-menu-buffer t)) | |
263 | (save-buffer) | |
264 | (setq modp (buffer-modified-p))) | |
265 | (let ((buffer-read-only nil)) | |
266 | (delete-char -1) | |
267 | (insert (if modp ?* ? )))))) | |
268 | (save-excursion | |
269 | (goto-char (point-min)) | |
270 | (forward-line 1) | |
271 | (let ((buff-menu-buffer (current-buffer)) | |
272 | (buffer-read-only nil)) | |
273 | (while (search-forward "\nD" nil t) | |
274 | (forward-char -1) | |
275 | (let ((buf (Buffer-menu-buffer nil))) | |
276 | (or (eq buf nil) | |
277 | (eq buf buff-menu-buffer) | |
278 | (save-excursion (kill-buffer buf)))) | |
279 | (if (Buffer-menu-buffer nil) | |
280 | (progn (delete-char 1) | |
281 | (insert ? )) | |
282 | (delete-region (point) (progn (forward-line 1) (point))) | |
283 | (forward-char -1)))))) | |
284 | ||
285 | (defun Buffer-menu-select () | |
286 | "Select this line's buffer; also display buffers marked with `>'. | |
287 | You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command." | |
288 | (interactive) | |
289 | (let ((buff (Buffer-menu-buffer t)) | |
290 | (menu (current-buffer)) | |
291 | (others ()) | |
292 | tem) | |
293 | (goto-char (point-min)) | |
294 | (while (search-forward "\n>" nil t) | |
295 | (setq tem (Buffer-menu-buffer t)) | |
296 | (let ((buffer-read-only nil)) | |
297 | (delete-char -1) | |
298 | (insert ?\ )) | |
299 | (or (eq tem buff) (memq tem others) (setq others (cons tem others)))) | |
300 | (setq others (nreverse others) | |
dc6d9681 | 301 | tem (/ (1- (frame-height)) (1+ (length others)))) |
2dd96f23 JB |
302 | (delete-other-windows) |
303 | (switch-to-buffer buff) | |
304 | (or (eq menu buff) | |
305 | (bury-buffer menu)) | |
86dbbfc2 ER |
306 | (if (equal (length others) 0) |
307 | (progn | |
3ed788ec RS |
308 | ;;; ;; Restore previous window configuration before displaying |
309 | ;;; ;; selected buffers. | |
310 | ;;; (if Buffer-menu-window-config | |
311 | ;;; (progn | |
312 | ;;; (set-window-configuration Buffer-menu-window-config) | |
313 | ;;; (setq Buffer-menu-window-config nil))) | |
86dbbfc2 ER |
314 | (switch-to-buffer buff)) |
315 | (while others | |
316 | (split-window nil tem) | |
317 | (other-window 1) | |
318 | (switch-to-buffer (car others)) | |
319 | (setq others (cdr others))) | |
320 | (other-window 1) ;back to the beginning! | |
321 | ))) | |
322 | ||
2dd96f23 JB |
323 | \f |
324 | (defun Buffer-menu-visit-tags-table () | |
325 | "Visit the tags table in the buffer on this line. See `visit-tags-table'." | |
326 | (interactive) | |
327 | (let ((file (buffer-file-name (Buffer-menu-buffer t)))) | |
328 | (if file | |
329 | (visit-tags-table file) | |
330 | (error "Specified buffer has no file")))) | |
331 | ||
332 | (defun Buffer-menu-1-window () | |
dc6d9681 | 333 | "Select this line's buffer, alone, in full frame." |
2dd96f23 JB |
334 | (interactive) |
335 | (switch-to-buffer (Buffer-menu-buffer t)) | |
336 | (bury-buffer (other-buffer)) | |
337 | (delete-other-windows)) | |
338 | ||
2cf5fc7e RS |
339 | (defun Buffer-menu-mouse-select (event) |
340 | "Select the buffer whose line you click on." | |
341 | (interactive "e") | |
342 | (let (buffer) | |
343 | (save-excursion | |
344 | (set-buffer (window-buffer (posn-window (event-end event)))) | |
345 | (save-excursion | |
346 | (goto-char (posn-point (event-end event))) | |
347 | (setq buffer (Buffer-menu-buffer t)))) | |
348 | (select-window (posn-window (event-end event))) | |
349 | (switch-to-buffer buffer))) | |
350 | ||
2dd96f23 JB |
351 | (defun Buffer-menu-this-window () |
352 | "Select this line's buffer in this window." | |
353 | (interactive) | |
354 | (switch-to-buffer (Buffer-menu-buffer t))) | |
355 | ||
356 | (defun Buffer-menu-other-window () | |
357 | "Select this line's buffer in other window, leaving buffer menu visible." | |
358 | (interactive) | |
359 | (switch-to-buffer-other-window (Buffer-menu-buffer t))) | |
360 | ||
35aaf00c RM |
361 | (defun Buffer-menu-switch-other-window () |
362 | "Make the other window select this line's buffer. | |
363 | The current window remains selected." | |
364 | (interactive) | |
365 | (display-buffer (Buffer-menu-buffer t))) | |
366 | ||
2dd96f23 JB |
367 | (defun Buffer-menu-2-window () |
368 | "Select this line's buffer, with previous buffer in second window." | |
369 | (interactive) | |
370 | (let ((buff (Buffer-menu-buffer t)) | |
371 | (menu (current-buffer)) | |
372 | (pop-up-windows t)) | |
86dbbfc2 | 373 | (delete-other-windows) |
2dd96f23 JB |
374 | (switch-to-buffer (other-buffer)) |
375 | (pop-to-buffer buff) | |
376 | (bury-buffer menu))) | |
c0274f38 | 377 | |
d91e2b1a | 378 | (defun Buffer-menu-toggle-read-only () |
f450965c | 379 | "Toggle read-only status of buffer on this line, perhaps via version control." |
d91e2b1a ER |
380 | (interactive) |
381 | (let (char) | |
382 | (save-excursion | |
383 | (set-buffer (Buffer-menu-buffer t)) | |
f450965c | 384 | (vc-toggle-read-only) |
d91e2b1a ER |
385 | (setq char (if buffer-read-only ?% ? ))) |
386 | (save-excursion | |
387 | (beginning-of-line) | |
388 | (forward-char 2) | |
389 | (if (/= (following-char) char) | |
390 | (let (buffer-read-only) | |
391 | (delete-char 1) | |
392 | (insert char)))))) | |
393 | ||
c0274f38 | 394 | ;;; buff-menu.el ends here |