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