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