]> code.delx.au - gnu-emacs/blob - lisp/buff-menu.el
(Buffer-menu-quit): Function deleted.
[gnu-emacs] / lisp / buff-menu.el
1 ;;; buff-menu.el --- buffer menu main function and support functions.
2
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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 the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; Edit, delete, or change attributes of all currently active Emacs
27 ;; buffers from a list summarizing their state. A good way to browse
28 ;; any special or scratch buffers you have loaded, since you can't find
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 ;;
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.
42
43 ;;; Code:
44
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
56 ;;;(defvar Buffer-menu-window-config nil
57 ;;; "Window configuration saved from entry to `buffer-menu'.")
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
62 (defvar Buffer-menu-buffer-column nil)
63
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)
70 (define-key Buffer-menu-mode-map "q" 'quit-window)
71 (define-key Buffer-menu-mode-map "v" 'Buffer-menu-select)
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)
75 (define-key Buffer-menu-mode-map "e" 'Buffer-menu-this-window)
76 (define-key Buffer-menu-mode-map "\C-m" 'Buffer-menu-this-window)
77 (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window)
78 (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window)
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)
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)
95 (define-key Buffer-menu-mode-map "b" 'Buffer-menu-bury)
96 (define-key Buffer-menu-mode-map "g" 'Buffer-menu-revert)
97 (define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-mouse-select)
98 )
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>
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.
116 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
117 \\[Buffer-menu-2-window] -- select that buffer in one window,
118 together with buffer selected before this one in another window.
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.
126 With prefix argument, also move up one line.
127 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
128 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
129 \\[Buffer-menu-revert] -- update the list of buffers.
130 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
131 (kill-all-local-variables)
132 (use-local-map Buffer-menu-mode-map)
133 (setq major-mode 'Buffer-menu-mode)
134 (setq mode-name "Buffer Menu")
135 (make-local-variable 'revert-buffer-function)
136 (setq revert-buffer-function 'Buffer-menu-revert-function)
137 (setq truncate-lines t)
138 (setq buffer-read-only t)
139 (run-hooks 'buffer-menu-mode-hook))
140
141 (defun Buffer-menu-revert ()
142 "Update the list of buffers."
143 (interactive)
144 (revert-buffer))
145
146 (defun Buffer-menu-revert-function (ignore1 ignore2)
147 (list-buffers))
148 \f
149 (defun Buffer-menu-buffer (error-if-non-existent-p)
150 "Return buffer described by this line of buffer menu."
151 (let* ((where (save-excursion
152 (beginning-of-line)
153 (+ (point) Buffer-menu-buffer-column)))
154 (name (and (not (eobp)) (get-text-property where 'buffer-name))))
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))))
163 \f
164 (defun buffer-menu (&optional arg)
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.
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."
181 (interactive "P")
182 ;;; (setq Buffer-menu-window-config (current-window-configuration))
183 (switch-to-buffer-other-window (list-buffers-noselect arg))
184 (message
185 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
186
187 (defun Buffer-menu-mark ()
188 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
189 (interactive)
190 (beginning-of-line)
191 (if (looking-at " [-M]")
192 (ding)
193 (let ((buffer-read-only nil))
194 (delete-char 1)
195 (insert ?>)
196 (forward-line 1))))
197
198 (defun Buffer-menu-unmark (&optional backup)
199 "Cancel all requested operations on buffer on this line and move down.
200 Optional ARG means move up."
201 (interactive "P")
202 (beginning-of-line)
203 (if (looking-at " [-M]")
204 (ding)
205 (let* ((buf (Buffer-menu-buffer t))
206 (mod (buffer-modified-p buf))
207 (readonly (save-excursion (set-buffer buf) buffer-read-only))
208 (buffer-read-only nil))
209 (delete-char 3)
210 (insert (if readonly (if mod " *%" " %") (if mod " * " " ")))))
211 (forward-line (if backup -1 1)))
212
213 (defun Buffer-menu-backup-unmark ()
214 "Move up and cancel all requested operations on buffer on line above."
215 (interactive)
216 (forward-line -1)
217 (Buffer-menu-unmark)
218 (forward-line -1))
219
220 (defun Buffer-menu-delete (&optional arg)
221 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
222 Prefix arg is how many buffers to delete.
223 Negative arg means delete backwards."
224 (interactive "p")
225 (beginning-of-line)
226 (if (looking-at " [-M]") ;header lines
227 (ding)
228 (let ((buffer-read-only nil))
229 (if (or (null arg) (= arg 0))
230 (setq arg 1))
231 (while (> arg 0)
232 (delete-char 1)
233 (insert ?D)
234 (forward-line 1)
235 (setq arg (1- arg)))
236 (while (< arg 0)
237 (delete-char 1)
238 (insert ?D)
239 (forward-line -1)
240 (setq arg (1+ arg))))))
241
242 (defun Buffer-menu-delete-backwards (&optional arg)
243 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
244 and then move up one line. Prefix arg means move that many lines."
245 (interactive "p")
246 (Buffer-menu-delete (- (or arg 1)))
247 (while (looking-at " [-M]")
248 (forward-line 1)))
249
250 (defun Buffer-menu-save ()
251 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
252 (interactive)
253 (beginning-of-line)
254 (if (looking-at " [-M]") ;header lines
255 (ding)
256 (let ((buffer-read-only nil))
257 (forward-char 1)
258 (delete-char 1)
259 (insert ?S)
260 (forward-line 1))))
261
262 (defun Buffer-menu-not-modified (&optional arg)
263 "Mark buffer on this line as unmodified (no changes to save)."
264 (interactive "P")
265 (save-excursion
266 (set-buffer (Buffer-menu-buffer t))
267 (set-buffer-modified-p arg))
268 (save-excursion
269 (beginning-of-line)
270 (forward-char 1)
271 (if (= (char-after (point)) (if arg ? ?*))
272 (let ((buffer-read-only nil))
273 (delete-char 1)
274 (insert (if arg ?* ? ))))))
275
276 (defun Buffer-menu-execute ()
277 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
278 (interactive)
279 (save-excursion
280 (goto-char (point-min))
281 (forward-line 1)
282 (while (re-search-forward "^.S" nil t)
283 (let ((modp nil))
284 (save-excursion
285 (set-buffer (Buffer-menu-buffer t))
286 (save-buffer)
287 (setq modp (buffer-modified-p)))
288 (let ((buffer-read-only nil))
289 (delete-char -1)
290 (insert (if modp ?* ? ))))))
291 (save-excursion
292 (goto-char (point-min))
293 (forward-line 1)
294 (let ((buff-menu-buffer (current-buffer))
295 (buffer-read-only nil))
296 (while (search-forward "\nD" nil t)
297 (forward-char -1)
298 (let ((buf (Buffer-menu-buffer nil)))
299 (or (eq buf nil)
300 (eq buf buff-menu-buffer)
301 (save-excursion (kill-buffer buf))))
302 (if (Buffer-menu-buffer nil)
303 (progn (delete-char 1)
304 (insert ? ))
305 (delete-region (point) (progn (forward-line 1) (point)))
306 (forward-char -1))))))
307
308 (defun Buffer-menu-select ()
309 "Select this line's buffer; also display buffers marked with `>'.
310 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
311 This command deletes and replaces all the previously existing windows
312 in the selected frame."
313 (interactive)
314 (let ((buff (Buffer-menu-buffer t))
315 (menu (current-buffer))
316 (others ())
317 tem)
318 (goto-char (point-min))
319 (while (search-forward "\n>" nil t)
320 (setq tem (Buffer-menu-buffer t))
321 (let ((buffer-read-only nil))
322 (delete-char -1)
323 (insert ?\ ))
324 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
325 (setq others (nreverse others)
326 tem (/ (1- (frame-height)) (1+ (length others))))
327 (delete-other-windows)
328 (switch-to-buffer buff)
329 (or (eq menu buff)
330 (bury-buffer menu))
331 (if (equal (length others) 0)
332 (progn
333 ;;; ;; Restore previous window configuration before displaying
334 ;;; ;; selected buffers.
335 ;;; (if Buffer-menu-window-config
336 ;;; (progn
337 ;;; (set-window-configuration Buffer-menu-window-config)
338 ;;; (setq Buffer-menu-window-config nil)))
339 (switch-to-buffer buff))
340 (while others
341 (split-window nil tem)
342 (other-window 1)
343 (switch-to-buffer (car others))
344 (setq others (cdr others)))
345 (other-window 1) ;back to the beginning!
346 )))
347
348
349 \f
350 (defun Buffer-menu-visit-tags-table ()
351 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
352 (interactive)
353 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
354 (if file
355 (visit-tags-table file)
356 (error "Specified buffer has no file"))))
357
358 (defun Buffer-menu-1-window ()
359 "Select this line's buffer, alone, in full frame."
360 (interactive)
361 (switch-to-buffer (Buffer-menu-buffer t))
362 (bury-buffer (other-buffer))
363 (delete-other-windows))
364
365 (defun Buffer-menu-mouse-select (event)
366 "Select the buffer whose line you click on."
367 (interactive "e")
368 (let (buffer)
369 (save-excursion
370 (set-buffer (window-buffer (posn-window (event-end event))))
371 (save-excursion
372 (goto-char (posn-point (event-end event)))
373 (setq buffer (Buffer-menu-buffer t))))
374 (select-window (posn-window (event-end event)))
375 (if (and (window-dedicated-p (selected-window))
376 (eq (selected-window) (frame-root-window)))
377 (switch-to-buffer-other-frame buffer)
378 (switch-to-buffer buffer))))
379
380 (defun Buffer-menu-this-window ()
381 "Select this line's buffer in this window."
382 (interactive)
383 (switch-to-buffer (Buffer-menu-buffer t)))
384
385 (defun Buffer-menu-other-window ()
386 "Select this line's buffer in other window, leaving buffer menu visible."
387 (interactive)
388 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
389
390 (defun Buffer-menu-switch-other-window ()
391 "Make the other window select this line's buffer.
392 The current window remains selected."
393 (interactive)
394 (display-buffer (Buffer-menu-buffer t)))
395
396 (defun Buffer-menu-2-window ()
397 "Select this line's buffer, with previous buffer in second window."
398 (interactive)
399 (let ((buff (Buffer-menu-buffer t))
400 (menu (current-buffer))
401 (pop-up-windows t))
402 (delete-other-windows)
403 (switch-to-buffer (other-buffer))
404 (pop-to-buffer buff)
405 (bury-buffer menu)))
406
407 (defun Buffer-menu-toggle-read-only ()
408 "Toggle read-only status of buffer on this line, perhaps via version control."
409 (interactive)
410 (let (char)
411 (save-excursion
412 (set-buffer (Buffer-menu-buffer t))
413 (vc-toggle-read-only)
414 (setq char (if buffer-read-only ?% ? )))
415 (save-excursion
416 (beginning-of-line)
417 (forward-char 2)
418 (if (/= (following-char) char)
419 (let (buffer-read-only)
420 (delete-char 1)
421 (insert char))))))
422
423 (defun Buffer-menu-bury ()
424 "Bury the buffer listed on this line."
425 (interactive)
426 (beginning-of-line)
427 (if (looking-at " [-M]") ;header lines
428 (ding)
429 (save-excursion
430 (beginning-of-line)
431 (bury-buffer (Buffer-menu-buffer t))
432 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
433 (buffer-read-only nil))
434 (delete-region (point) (progn (forward-line -1) (point)))
435 (goto-char (point-max))
436 (insert line))
437 (message "Buried buffer moved to the end"))))
438 \f
439
440 (define-key ctl-x-map "\C-b" 'list-buffers)
441
442 (defun list-buffers (&optional files-only)
443 "Display a list of names of existing buffers.
444 The list is displayed in a buffer named `*Buffer List*'.
445 Note that buffers with names starting with spaces are omitted.
446 Non-null optional arg FILES-ONLY means mention only file buffers.
447
448 The M column contains a * for buffers that are modified.
449 The R column contains a % for buffers that are read-only."
450 (interactive "P")
451 (display-buffer (list-buffers-noselect files-only)))
452
453 (defun list-buffers-noselect (&optional files-only)
454 "Create and return a buffer with a list of names of existing buffers.
455 The buffer is named `*Buffer List*'.
456 Note that buffers with names starting with spaces are omitted.
457 Non-null optional arg FILES-ONLY means mention only file buffers.
458
459 The M column contains a * for buffers that are modified.
460 The R column contains a % for buffers that are read-only."
461 (let ((old-buffer (current-buffer))
462 (standard-output standard-output)
463 desired-point)
464 (save-excursion
465 (set-buffer (get-buffer-create "*Buffer List*"))
466 (setq buffer-read-only nil)
467 (erase-buffer)
468 (setq standard-output (current-buffer))
469 (princ "\
470 MR Buffer Size Mode File
471 -- ------ ---- ---- ----
472 ")
473 ;; Record the column where buffer names start.
474 (setq Buffer-menu-buffer-column 4)
475 (let ((bl (buffer-list)))
476 (while bl
477 (let* ((buffer (car bl))
478 (name (buffer-name buffer))
479 (file (buffer-file-name buffer))
480 this-buffer-line-start
481 this-buffer-read-only
482 this-buffer-size
483 this-buffer-mode-name
484 this-buffer-directory)
485 (save-excursion
486 (set-buffer buffer)
487 (setq this-buffer-read-only buffer-read-only)
488 (setq this-buffer-size (buffer-size))
489 (setq this-buffer-mode-name
490 (if (eq buffer standard-output)
491 "Buffer Menu" mode-name))
492 (or file
493 ;; No visited file. Check local value of
494 ;; list-buffers-directory.
495 (if (and (boundp 'list-buffers-directory)
496 list-buffers-directory)
497 (setq this-buffer-directory list-buffers-directory))))
498 (cond
499 ;; Don't mention internal buffers.
500 ((string= (substring name 0 1) " "))
501 ;; Maybe don't mention buffers without files.
502 ((and files-only (not file)))
503 ;; Otherwise output info.
504 (t
505 (setq this-buffer-line-start (point))
506 ;; Identify current buffer.
507 (if (eq buffer old-buffer)
508 (progn
509 (setq desired-point (point))
510 (princ "."))
511 (princ " "))
512 ;; Identify modified buffers.
513 (princ (if (buffer-modified-p buffer) "*" " "))
514 ;; Handle readonly status. The output buffer is special
515 ;; cased to appear readonly; it is actually made so at a later
516 ;; date.
517 (princ (if (or (eq buffer standard-output)
518 this-buffer-read-only)
519 "% "
520 " "))
521 (princ name)
522 ;; Put the buffer name into a text property
523 ;; so we don't have to extract it from the text.
524 ;; This way we avoid problems with unusual buffer names.
525 (setq this-buffer-line-start
526 (+ this-buffer-line-start Buffer-menu-buffer-column))
527 (let ((name-end (point)))
528 (indent-to 17 2)
529 (put-text-property this-buffer-line-start name-end
530 'buffer-name name)
531 (put-text-property this-buffer-line-start name-end
532 'mouse-face 'highlight))
533 (let (size
534 mode
535 (excess (- (current-column) 17)))
536 (setq size (format "%8d" this-buffer-size))
537 ;; Ack -- if looking at the *Buffer List* buffer,
538 ;; always use "Buffer Menu" mode. Otherwise the
539 ;; first time the buffer is created, the mode will be wrong.
540 (setq mode this-buffer-mode-name)
541 (while (and (> excess 0) (= (aref size 0) ?\ ))
542 (setq size (substring size 1))
543 (setq excess (1- excess)))
544 (princ size)
545 (indent-to 27 1)
546 (princ mode))
547 (indent-to 40 1)
548 (or file (setq file this-buffer-directory))
549 (if file
550 (princ file))
551 (princ "\n"))))
552 (setq bl (cdr bl))))
553 (Buffer-menu-mode)
554 ;; DESIRED-POINT doesn't have to be set; it is not when the
555 ;; current buffer is not displayed for some reason.
556 (and desired-point
557 (goto-char desired-point))
558 (current-buffer))))
559
560 ;;; buff-menu.el ends here