Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; buff-menu.el --- buffer menu main function and support functions. |
2 | ||
5deca1be | 3 | ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 2000 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 | |
b578f267 EN |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
21 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | ;; Boston, MA 02111-1307, USA. | |
2dd96f23 | 23 | |
e41b2db1 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; Edit, delete, or change attributes of all currently active Emacs | |
eb8c3be9 | 27 | ;; buffers from a list summarizing their state. A good way to browse |
e41b2db1 | 28 | ;; any special or scratch buffers you have loaded, since you can't find |
86dbbfc2 ER |
29 | ;; them by filename. The single entry point is `Buffer-menu-mode', |
30 | ;; normally bound to C-x C-b. | |
31 | ||
32 | ;;; Change Log: | |
33 | ||
42a19c2a RS |
34 | ;; Buffer-menu-view: New function |
35 | ;; Buffer-menu-view-other-window: New function | |
36 | ||
86dbbfc2 ER |
37 | ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993 |
38 | ;; | |
86dbbfc2 ER |
39 | ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89 |
40 | ;; | |
41 | ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete | |
42 | ;; current entry and then move to previous one. | |
43 | ;; | |
44 | ;; Based on FSF code dating back to 1985. | |
e41b2db1 | 45 | |
5de9bdab | 46 | ;;; Code: |
10e1dad9 | 47 | |
08c82d4d RS |
48 | ;;;Trying to preserve the old window configuration works well in |
49 | ;;;simple scenarios, when you enter the buffer menu, use it, and exit it. | |
50 | ;;;But it does strange things when you switch back to the buffer list buffer | |
51 | ;;;with C-x b, later on, when the window configuration is different. | |
52 | ;;;The choice seems to be, either restore the window configuration | |
53 | ;;;in all cases, or in no cases. | |
54 | ;;;I decided it was better not to restore the window config at all. -- rms. | |
55 | ||
56 | ;;;But since then, I changed buffer-menu to use the selected window, | |
57 | ;;;so q now once again goes back to the previous window configuration. | |
58 | ||
3ed788ec RS |
59 | ;;;(defvar Buffer-menu-window-config nil |
60 | ;;; "Window configuration saved from entry to `buffer-menu'.") | |
2dd96f23 JB |
61 | |
62 | ; Put buffer *Buffer List* into proper mode right away | |
63 | ; so that from now on even list-buffers is enough to get a buffer menu. | |
64 | ||
e5ea316b KH |
65 | (defvar Buffer-menu-buffer-column nil) |
66 | ||
2dd96f23 JB |
67 | (defvar Buffer-menu-mode-map nil "") |
68 | ||
69 | (if Buffer-menu-mode-map | |
70 | () | |
71 | (setq Buffer-menu-mode-map (make-keymap)) | |
72 | (suppress-keymap Buffer-menu-mode-map t) | |
68e6f094 | 73 | (define-key Buffer-menu-mode-map "q" 'quit-window) |
3ed788ec | 74 | (define-key Buffer-menu-mode-map "v" 'Buffer-menu-select) |
2dd96f23 JB |
75 | (define-key Buffer-menu-mode-map "2" 'Buffer-menu-2-window) |
76 | (define-key Buffer-menu-mode-map "1" 'Buffer-menu-1-window) | |
77 | (define-key Buffer-menu-mode-map "f" 'Buffer-menu-this-window) | |
251680cf | 78 | (define-key Buffer-menu-mode-map "e" 'Buffer-menu-this-window) |
8768da77 | 79 | (define-key Buffer-menu-mode-map "\C-m" 'Buffer-menu-this-window) |
2dd96f23 | 80 | (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window) |
35aaf00c | 81 | (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window) |
2dd96f23 JB |
82 | (define-key Buffer-menu-mode-map "s" 'Buffer-menu-save) |
83 | (define-key Buffer-menu-mode-map "d" 'Buffer-menu-delete) | |
84 | (define-key Buffer-menu-mode-map "k" 'Buffer-menu-delete) | |
85 | (define-key Buffer-menu-mode-map "\C-d" 'Buffer-menu-delete-backwards) | |
86 | (define-key Buffer-menu-mode-map "\C-k" 'Buffer-menu-delete) | |
87 | (define-key Buffer-menu-mode-map "x" 'Buffer-menu-execute) | |
88 | (define-key Buffer-menu-mode-map " " 'next-line) | |
89 | (define-key Buffer-menu-mode-map "n" 'next-line) | |
90 | (define-key Buffer-menu-mode-map "p" 'previous-line) | |
91 | (define-key Buffer-menu-mode-map "\177" 'Buffer-menu-backup-unmark) | |
92 | (define-key Buffer-menu-mode-map "~" 'Buffer-menu-not-modified) | |
93 | (define-key Buffer-menu-mode-map "?" 'describe-mode) | |
94 | (define-key Buffer-menu-mode-map "u" 'Buffer-menu-unmark) | |
95 | (define-key Buffer-menu-mode-map "m" 'Buffer-menu-mark) | |
d91e2b1a ER |
96 | (define-key Buffer-menu-mode-map "t" 'Buffer-menu-visit-tags-table) |
97 | (define-key Buffer-menu-mode-map "%" 'Buffer-menu-toggle-read-only) | |
36570c92 | 98 | (define-key Buffer-menu-mode-map "b" 'Buffer-menu-bury) |
0f88624e | 99 | (define-key Buffer-menu-mode-map "g" 'Buffer-menu-revert) |
42a19c2a | 100 | (define-key Buffer-menu-mode-map "V" 'Buffer-menu-view) |
2cf5fc7e | 101 | (define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-mouse-select) |
d91e2b1a | 102 | ) |
2dd96f23 JB |
103 | |
104 | ;; Buffer Menu mode is suitable only for specially formatted data. | |
105 | (put 'Buffer-menu-mode 'mode-class 'special) | |
106 | ||
107 | (defun Buffer-menu-mode () | |
108 | "Major mode for editing a list of buffers. | |
109 | Each line describes one of the buffers in Emacs. | |
110 | Letters do not insert themselves; instead, they are commands. | |
111 | \\<Buffer-menu-mode-map> | |
2cf5fc7e RS |
112 | \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu. |
113 | \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu. | |
114 | \\[Buffer-menu-other-window] -- select that buffer in another window, | |
115 | so the buffer menu buffer remains visible in its window. | |
42a19c2a RS |
116 | \\[Buffer-menu-view] -- select current line's buffer, but in view-mode. |
117 | \\[Buffer-menu-view-other-window] -- select that buffer in | |
118 | another window, in view-mode. | |
2cf5fc7e RS |
119 | \\[Buffer-menu-switch-other-window] -- make another window display that buffer. |
120 | \\[Buffer-menu-mark] -- mark current line's buffer to be displayed. | |
121 | \\[Buffer-menu-select] -- select current line's buffer. | |
122 | Also show buffers marked with m, in other windows. | |
dc6d9681 | 123 | \\[Buffer-menu-1-window] -- select that buffer in full-frame window. |
2dd96f23 JB |
124 | \\[Buffer-menu-2-window] -- select that buffer in one window, |
125 | together with buffer selected before this one in another window. | |
2dd96f23 JB |
126 | \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer. |
127 | \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer. | |
128 | \\[Buffer-menu-save] -- mark that buffer to be saved, and move down. | |
129 | \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down. | |
130 | \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up. | |
131 | \\[Buffer-menu-execute] -- delete or save marked buffers. | |
132 | \\[Buffer-menu-unmark] -- remove all kinds of marks from current line. | |
86dbbfc2 | 133 | With prefix argument, also move up one line. |
d91e2b1a | 134 | \\[Buffer-menu-backup-unmark] -- back up a line and remove marks. |
0f88624e | 135 | \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line. |
a59faabe RS |
136 | \\[Buffer-menu-revert] -- update the list of buffers. |
137 | \\[Buffer-menu-bury] -- bury the buffer listed on this line." | |
2dd96f23 JB |
138 | (kill-all-local-variables) |
139 | (use-local-map Buffer-menu-mode-map) | |
2dd96f23 JB |
140 | (setq major-mode 'Buffer-menu-mode) |
141 | (setq mode-name "Buffer Menu") | |
710e5da8 RS |
142 | (make-local-variable 'revert-buffer-function) |
143 | (setq revert-buffer-function 'Buffer-menu-revert-function) | |
823316aa KH |
144 | (setq truncate-lines t) |
145 | (setq buffer-read-only t) | |
2dd96f23 | 146 | (run-hooks 'buffer-menu-mode-hook)) |
710e5da8 | 147 | |
0f88624e RS |
148 | (defun Buffer-menu-revert () |
149 | "Update the list of buffers." | |
150 | (interactive) | |
151 | (revert-buffer)) | |
152 | ||
710e5da8 RS |
153 | (defun Buffer-menu-revert-function (ignore1 ignore2) |
154 | (list-buffers)) | |
2dd96f23 | 155 | \f |
2dd96f23 JB |
156 | (defun Buffer-menu-buffer (error-if-non-existent-p) |
157 | "Return buffer described by this line of buffer menu." | |
1b40dbed RS |
158 | (let* ((where (save-excursion |
159 | (beginning-of-line) | |
160 | (+ (point) Buffer-menu-buffer-column))) | |
601ed8a0 GM |
161 | (name (and (not (eobp)) (get-text-property where 'buffer-name))) |
162 | (buf (and (not (eobp)) (get-text-property where 'buffer)))) | |
1b40dbed RS |
163 | (if name |
164 | (or (get-buffer name) | |
601ed8a0 | 165 | (and buf (buffer-name buf) buf) |
1b40dbed RS |
166 | (if error-if-non-existent-p |
167 | (error "No buffer named `%s'" name) | |
168 | nil)) | |
601ed8a0 | 169 | (or (and buf (buffer-name buf) buf) |
1b40dbed RS |
170 | (if error-if-non-existent-p |
171 | (error "No buffer on this line") | |
601ed8a0 | 172 | nil))))) |
2dd96f23 | 173 | \f |
06b1a5ef | 174 | (defun buffer-menu (&optional arg) |
2dd96f23 JB |
175 | "Make a menu of buffers so you can save, delete or select them. |
176 | With argument, show only buffers that are visiting files. | |
177 | Type ? after invocation to get help on commands available. | |
08c82d4d RS |
178 | Type q immediately to make the buffer menu go away." |
179 | (interactive "P") | |
180 | ;;; (setq Buffer-menu-window-config (current-window-configuration)) | |
181 | (switch-to-buffer (list-buffers-noselect arg)) | |
182 | (message | |
183 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) | |
184 | ||
185 | (defun buffer-menu-other-window (&optional arg) | |
186 | "Display a list of buffers in another window. | |
187 | With the buffer list buffer, you can save, delete or select the buffers. | |
188 | With argument, show only buffers that are visiting files. | |
189 | Type ? after invocation to get help on commands available. | |
190 | Type q immediately to make the buffer menu go away." | |
2dd96f23 | 191 | (interactive "P") |
3ed788ec | 192 | ;;; (setq Buffer-menu-window-config (current-window-configuration)) |
08c82d4d | 193 | (switch-to-buffer-other-window (list-buffers-noselect arg)) |
2dd96f23 | 194 | (message |
3ed788ec RS |
195 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) |
196 | ||
2dd96f23 JB |
197 | (defun Buffer-menu-mark () |
198 | "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command." | |
199 | (interactive) | |
200 | (beginning-of-line) | |
201 | (if (looking-at " [-M]") | |
202 | (ding) | |
203 | (let ((buffer-read-only nil)) | |
204 | (delete-char 1) | |
205 | (insert ?>) | |
206 | (forward-line 1)))) | |
207 | ||
86dbbfc2 ER |
208 | (defun Buffer-menu-unmark (&optional backup) |
209 | "Cancel all requested operations on buffer on this line and move down. | |
210 | Optional ARG means move up." | |
211 | (interactive "P") | |
2dd96f23 JB |
212 | (beginning-of-line) |
213 | (if (looking-at " [-M]") | |
214 | (ding) | |
215 | (let* ((buf (Buffer-menu-buffer t)) | |
216 | (mod (buffer-modified-p buf)) | |
217 | (readonly (save-excursion (set-buffer buf) buffer-read-only)) | |
218 | (buffer-read-only nil)) | |
219 | (delete-char 3) | |
220 | (insert (if readonly (if mod " *%" " %") (if mod " * " " "))))) | |
86dbbfc2 | 221 | (forward-line (if backup -1 1))) |
2dd96f23 JB |
222 | |
223 | (defun Buffer-menu-backup-unmark () | |
224 | "Move up and cancel all requested operations on buffer on line above." | |
225 | (interactive) | |
226 | (forward-line -1) | |
227 | (Buffer-menu-unmark) | |
228 | (forward-line -1)) | |
229 | ||
77a43e01 RS |
230 | (defun Buffer-menu-delete (&optional arg) |
231 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command. | |
232 | Prefix arg is how many buffers to delete. | |
233 | Negative arg means delete backwards." | |
234 | (interactive "p") | |
2dd96f23 JB |
235 | (beginning-of-line) |
236 | (if (looking-at " [-M]") ;header lines | |
237 | (ding) | |
238 | (let ((buffer-read-only nil)) | |
77a43e01 RS |
239 | (if (or (null arg) (= arg 0)) |
240 | (setq arg 1)) | |
241 | (while (> arg 0) | |
242 | (delete-char 1) | |
243 | (insert ?D) | |
244 | (forward-line 1) | |
245 | (setq arg (1- arg))) | |
246 | (while (< arg 0) | |
247 | (delete-char 1) | |
248 | (insert ?D) | |
249 | (forward-line -1) | |
250 | (setq arg (1+ arg)))))) | |
251 | ||
252 | (defun Buffer-menu-delete-backwards (&optional arg) | |
2dd96f23 | 253 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command |
77a43e01 RS |
254 | and then move up one line. Prefix arg means move that many lines." |
255 | (interactive "p") | |
256 | (Buffer-menu-delete (- (or arg 1))) | |
257 | (while (looking-at " [-M]") | |
258 | (forward-line 1))) | |
2dd96f23 JB |
259 | |
260 | (defun Buffer-menu-save () | |
261 | "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command." | |
262 | (interactive) | |
263 | (beginning-of-line) | |
2dd96f23 JB |
264 | (if (looking-at " [-M]") ;header lines |
265 | (ding) | |
266 | (let ((buffer-read-only nil)) | |
8cd1ad32 | 267 | (forward-char 1) |
2dd96f23 JB |
268 | (delete-char 1) |
269 | (insert ?S) | |
270 | (forward-line 1)))) | |
271 | ||
a8ce00dc | 272 | (defun Buffer-menu-not-modified (&optional arg) |
2dd96f23 | 273 | "Mark buffer on this line as unmodified (no changes to save)." |
a8ce00dc | 274 | (interactive "P") |
2dd96f23 JB |
275 | (save-excursion |
276 | (set-buffer (Buffer-menu-buffer t)) | |
a8ce00dc | 277 | (set-buffer-modified-p arg)) |
2dd96f23 JB |
278 | (save-excursion |
279 | (beginning-of-line) | |
280 | (forward-char 1) | |
a8ce00dc | 281 | (if (= (char-after (point)) (if arg ? ?*)) |
2dd96f23 JB |
282 | (let ((buffer-read-only nil)) |
283 | (delete-char 1) | |
a8ce00dc | 284 | (insert (if arg ?* ? )))))) |
2dd96f23 JB |
285 | |
286 | (defun Buffer-menu-execute () | |
287 | "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands." | |
288 | (interactive) | |
289 | (save-excursion | |
290 | (goto-char (point-min)) | |
291 | (forward-line 1) | |
292 | (while (re-search-forward "^.S" nil t) | |
293 | (let ((modp nil)) | |
294 | (save-excursion | |
295 | (set-buffer (Buffer-menu-buffer t)) | |
296 | (save-buffer) | |
297 | (setq modp (buffer-modified-p))) | |
298 | (let ((buffer-read-only nil)) | |
299 | (delete-char -1) | |
300 | (insert (if modp ?* ? )))))) | |
301 | (save-excursion | |
302 | (goto-char (point-min)) | |
303 | (forward-line 1) | |
304 | (let ((buff-menu-buffer (current-buffer)) | |
305 | (buffer-read-only nil)) | |
306 | (while (search-forward "\nD" nil t) | |
307 | (forward-char -1) | |
308 | (let ((buf (Buffer-menu-buffer nil))) | |
309 | (or (eq buf nil) | |
310 | (eq buf buff-menu-buffer) | |
601ed8a0 GM |
311 | (save-excursion (kill-buffer buf))) |
312 | (if (and buf (buffer-name buf)) | |
2dd96f23 JB |
313 | (progn (delete-char 1) |
314 | (insert ? )) | |
315 | (delete-region (point) (progn (forward-line 1) (point))) | |
601ed8a0 | 316 | (forward-char -1))))))) |
2dd96f23 JB |
317 | |
318 | (defun Buffer-menu-select () | |
319 | "Select this line's buffer; also display buffers marked with `>'. | |
08c82d4d RS |
320 | You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command. |
321 | This command deletes and replaces all the previously existing windows | |
322 | in the selected frame." | |
2dd96f23 JB |
323 | (interactive) |
324 | (let ((buff (Buffer-menu-buffer t)) | |
10e1dad9 | 325 | (menu (current-buffer)) |
2dd96f23 JB |
326 | (others ()) |
327 | tem) | |
328 | (goto-char (point-min)) | |
329 | (while (search-forward "\n>" nil t) | |
330 | (setq tem (Buffer-menu-buffer t)) | |
331 | (let ((buffer-read-only nil)) | |
332 | (delete-char -1) | |
333 | (insert ?\ )) | |
334 | (or (eq tem buff) (memq tem others) (setq others (cons tem others)))) | |
335 | (setq others (nreverse others) | |
dc6d9681 | 336 | tem (/ (1- (frame-height)) (1+ (length others)))) |
2dd96f23 JB |
337 | (delete-other-windows) |
338 | (switch-to-buffer buff) | |
339 | (or (eq menu buff) | |
340 | (bury-buffer menu)) | |
86dbbfc2 ER |
341 | (if (equal (length others) 0) |
342 | (progn | |
3ed788ec RS |
343 | ;;; ;; Restore previous window configuration before displaying |
344 | ;;; ;; selected buffers. | |
345 | ;;; (if Buffer-menu-window-config | |
346 | ;;; (progn | |
347 | ;;; (set-window-configuration Buffer-menu-window-config) | |
348 | ;;; (setq Buffer-menu-window-config nil))) | |
86dbbfc2 ER |
349 | (switch-to-buffer buff)) |
350 | (while others | |
351 | (split-window nil tem) | |
352 | (other-window 1) | |
353 | (switch-to-buffer (car others)) | |
354 | (setq others (cdr others))) | |
355 | (other-window 1) ;back to the beginning! | |
356 | ))) | |
357 | ||
08c82d4d | 358 | |
2dd96f23 JB |
359 | \f |
360 | (defun Buffer-menu-visit-tags-table () | |
361 | "Visit the tags table in the buffer on this line. See `visit-tags-table'." | |
362 | (interactive) | |
363 | (let ((file (buffer-file-name (Buffer-menu-buffer t)))) | |
364 | (if file | |
365 | (visit-tags-table file) | |
366 | (error "Specified buffer has no file")))) | |
367 | ||
368 | (defun Buffer-menu-1-window () | |
dc6d9681 | 369 | "Select this line's buffer, alone, in full frame." |
2dd96f23 JB |
370 | (interactive) |
371 | (switch-to-buffer (Buffer-menu-buffer t)) | |
372 | (bury-buffer (other-buffer)) | |
373 | (delete-other-windows)) | |
374 | ||
2cf5fc7e RS |
375 | (defun Buffer-menu-mouse-select (event) |
376 | "Select the buffer whose line you click on." | |
377 | (interactive "e") | |
378 | (let (buffer) | |
379 | (save-excursion | |
380 | (set-buffer (window-buffer (posn-window (event-end event)))) | |
381 | (save-excursion | |
382 | (goto-char (posn-point (event-end event))) | |
383 | (setq buffer (Buffer-menu-buffer t)))) | |
384 | (select-window (posn-window (event-end event))) | |
d953b88c RS |
385 | (if (and (window-dedicated-p (selected-window)) |
386 | (eq (selected-window) (frame-root-window))) | |
387 | (switch-to-buffer-other-frame buffer) | |
388 | (switch-to-buffer buffer)))) | |
2cf5fc7e | 389 | |
2dd96f23 JB |
390 | (defun Buffer-menu-this-window () |
391 | "Select this line's buffer in this window." | |
392 | (interactive) | |
393 | (switch-to-buffer (Buffer-menu-buffer t))) | |
394 | ||
395 | (defun Buffer-menu-other-window () | |
396 | "Select this line's buffer in other window, leaving buffer menu visible." | |
397 | (interactive) | |
398 | (switch-to-buffer-other-window (Buffer-menu-buffer t))) | |
399 | ||
35aaf00c RM |
400 | (defun Buffer-menu-switch-other-window () |
401 | "Make the other window select this line's buffer. | |
402 | The current window remains selected." | |
403 | (interactive) | |
404 | (display-buffer (Buffer-menu-buffer t))) | |
405 | ||
2dd96f23 JB |
406 | (defun Buffer-menu-2-window () |
407 | "Select this line's buffer, with previous buffer in second window." | |
408 | (interactive) | |
409 | (let ((buff (Buffer-menu-buffer t)) | |
410 | (menu (current-buffer)) | |
411 | (pop-up-windows t)) | |
86dbbfc2 | 412 | (delete-other-windows) |
2dd96f23 JB |
413 | (switch-to-buffer (other-buffer)) |
414 | (pop-to-buffer buff) | |
415 | (bury-buffer menu))) | |
c0274f38 | 416 | |
d91e2b1a | 417 | (defun Buffer-menu-toggle-read-only () |
f450965c | 418 | "Toggle read-only status of buffer on this line, perhaps via version control." |
d91e2b1a ER |
419 | (interactive) |
420 | (let (char) | |
421 | (save-excursion | |
422 | (set-buffer (Buffer-menu-buffer t)) | |
f450965c | 423 | (vc-toggle-read-only) |
d91e2b1a ER |
424 | (setq char (if buffer-read-only ?% ? ))) |
425 | (save-excursion | |
426 | (beginning-of-line) | |
427 | (forward-char 2) | |
428 | (if (/= (following-char) char) | |
429 | (let (buffer-read-only) | |
430 | (delete-char 1) | |
431 | (insert char)))))) | |
432 | ||
36570c92 RS |
433 | (defun Buffer-menu-bury () |
434 | "Bury the buffer listed on this line." | |
435 | (interactive) | |
a59faabe RS |
436 | (beginning-of-line) |
437 | (if (looking-at " [-M]") ;header lines | |
438 | (ding) | |
439 | (save-excursion | |
440 | (beginning-of-line) | |
441 | (bury-buffer (Buffer-menu-buffer t)) | |
442 | (let ((line (buffer-substring (point) (progn (forward-line 1) (point)))) | |
443 | (buffer-read-only nil)) | |
444 | (delete-region (point) (progn (forward-line -1) (point))) | |
445 | (goto-char (point-max)) | |
446 | (insert line)) | |
447 | (message "Buried buffer moved to the end")))) | |
42a19c2a RS |
448 | |
449 | ||
450 | (defun Buffer-menu-view () | |
451 | "View this line's buffer in View mode." | |
452 | (interactive) | |
453 | (view-buffer (Buffer-menu-buffer t))) | |
454 | ||
455 | ||
456 | (defun Buffer-menu-view-other-window () | |
457 | "View this line's buffer in View mode in another window." | |
458 | (interactive) | |
459 | (view-buffer-other-window (Buffer-menu-buffer t))) | |
0b03ce3a RS |
460 | \f |
461 | ||
462 | (define-key ctl-x-map "\C-b" 'list-buffers) | |
463 | ||
464 | (defun list-buffers (&optional files-only) | |
465 | "Display a list of names of existing buffers. | |
466 | The list is displayed in a buffer named `*Buffer List*'. | |
467 | Note that buffers with names starting with spaces are omitted. | |
468 | Non-null optional arg FILES-ONLY means mention only file buffers. | |
469 | ||
470 | The M column contains a * for buffers that are modified. | |
471 | The R column contains a % for buffers that are read-only." | |
472 | (interactive "P") | |
08c82d4d RS |
473 | (display-buffer (list-buffers-noselect files-only))) |
474 | ||
475 | (defun list-buffers-noselect (&optional files-only) | |
476 | "Create and return a buffer with a list of names of existing buffers. | |
477 | The buffer is named `*Buffer List*'. | |
478 | Note that buffers with names starting with spaces are omitted. | |
479 | Non-null optional arg FILES-ONLY means mention only file buffers. | |
480 | ||
481 | The M column contains a * for buffers that are modified. | |
482 | The R column contains a % for buffers that are read-only." | |
0b03ce3a | 483 | (let ((old-buffer (current-buffer)) |
08c82d4d RS |
484 | (standard-output standard-output) |
485 | desired-point) | |
486 | (save-excursion | |
487 | (set-buffer (get-buffer-create "*Buffer List*")) | |
488 | (setq buffer-read-only nil) | |
489 | (erase-buffer) | |
490 | (setq standard-output (current-buffer)) | |
0b03ce3a RS |
491 | (princ "\ |
492 | MR Buffer Size Mode File | |
493 | -- ------ ---- ---- ---- | |
494 | ") | |
1b40dbed RS |
495 | ;; Record the column where buffer names start. |
496 | (setq Buffer-menu-buffer-column 4) | |
10e1dad9 SS |
497 | (dolist (buffer (buffer-list)) |
498 | (let ((name (buffer-name buffer)) | |
499 | (file (buffer-file-name buffer)) | |
500 | this-buffer-line-start | |
501 | this-buffer-read-only | |
502 | (this-buffer-size (buffer-size buffer)) | |
503 | this-buffer-mode-name | |
504 | this-buffer-directory) | |
505 | (with-current-buffer buffer | |
506 | (setq this-buffer-read-only buffer-read-only | |
507 | this-buffer-mode-name mode-name) | |
508 | (unless file | |
509 | ;; No visited file. Check local value of | |
510 | ;; list-buffers-directory. | |
511 | (when (and (boundp 'list-buffers-directory) | |
512 | list-buffers-directory) | |
513 | (setq this-buffer-directory list-buffers-directory)))) | |
514 | (cond | |
515 | ;; Don't mention internal buffers. | |
516 | ((string= (substring name 0 1) " ")) | |
517 | ;; Maybe don't mention buffers without files. | |
518 | ((and files-only (not file))) | |
519 | ((string= name "*Buffer List*")) | |
520 | ;; Otherwise output info. | |
521 | (t | |
522 | (setq this-buffer-line-start (point)) | |
523 | ;; Identify current buffer. | |
524 | (if (eq buffer old-buffer) | |
525 | (progn | |
526 | (setq desired-point (point)) | |
527 | (princ ".")) | |
528 | (princ " ")) | |
529 | ;; Identify modified buffers. | |
530 | (princ (if (buffer-modified-p buffer) "*" " ")) | |
531 | ;; Handle readonly status. The output buffer is special | |
532 | ;; cased to appear readonly; it is actually made so at a | |
533 | ;; later date. | |
534 | (princ (if (or (eq buffer standard-output) | |
535 | this-buffer-read-only) | |
536 | "% " | |
537 | " ")) | |
538 | (princ name) | |
539 | ;; Put the buffer name into a text property | |
540 | ;; so we don't have to extract it from the text. | |
541 | ;; This way we avoid problems with unusual buffer names. | |
542 | (setq this-buffer-line-start | |
543 | (+ this-buffer-line-start Buffer-menu-buffer-column)) | |
544 | (let ((name-end (point))) | |
545 | (indent-to 17 2) | |
546 | (put-text-property this-buffer-line-start name-end | |
547 | 'buffer-name name) | |
548 | (put-text-property this-buffer-line-start (point) | |
549 | 'buffer buffer) | |
550 | (put-text-property this-buffer-line-start name-end | |
551 | 'mouse-face 'highlight)) | |
552 | (let ((size (format "%8d" this-buffer-size)) | |
553 | (mode this-buffer-mode-name) | |
554 | (excess (- (current-column) 17))) | |
555 | (while (and (> excess 0) (= (aref size 0) ?\ )) | |
556 | (setq size (substring size 1) | |
557 | excess (1- excess))) | |
558 | (princ size) | |
559 | (indent-to 27 1) | |
560 | (princ mode)) | |
561 | (indent-to 40 1) | |
562 | (or file (setq file this-buffer-directory)) | |
563 | (when file | |
564 | (princ (abbreviate-file-name file))) | |
565 | (princ "\n"))))) | |
0b03ce3a | 566 | (Buffer-menu-mode) |
08c82d4d RS |
567 | ;; DESIRED-POINT doesn't have to be set; it is not when the |
568 | ;; current buffer is not displayed for some reason. | |
0b03ce3a | 569 | (and desired-point |
08c82d4d RS |
570 | (goto-char desired-point)) |
571 | (current-buffer)))) | |
0b03ce3a | 572 | |
c0274f38 | 573 | ;;; buff-menu.el ends here |