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