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