Doc fixes. Remove redundant
[emacs.git] / lisp / buff-menu.el
blobea8a4ba914ac4165c93f8149f6a4731f043ca3ad
1 ;;; buff-menu.el --- buffer menu main function and support functions
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: convenience
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
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.
26 ;;; Commentary:
28 ;; Edit, delete, or change attributes of all currently active Emacs
29 ;; buffers from a list summarizing their state. A good way to browse
30 ;; any special or scratch buffers you have loaded, since you can't find
31 ;; them by filename. The single entry point is `list-buffers',
32 ;; normally bound to C-x C-b.
34 ;;; Change Log:
36 ;; Buffer-menu-view: New function
37 ;; Buffer-menu-view-other-window: New function
39 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
41 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
43 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
44 ;; current entry and then move to previous one.
46 ;; Based on FSF code dating back to 1985.
48 ;;; Code:
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.
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.
61 ;;;(defvar Buffer-menu-window-config nil
62 ;;; "Window configuration saved from entry to `buffer-menu'.")
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.
67 (defvar Buffer-menu-buffer-column nil)
69 (defvar Buffer-menu-mode-map nil "")
71 (if Buffer-menu-mode-map
73 (setq Buffer-menu-mode-map (make-keymap))
74 (suppress-keymap Buffer-menu-mode-map t)
75 (define-key Buffer-menu-mode-map "q" 'quit-window)
76 (define-key Buffer-menu-mode-map "v" 'Buffer-menu-select)
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)
80 (define-key Buffer-menu-mode-map "e" 'Buffer-menu-this-window)
81 (define-key Buffer-menu-mode-map "\C-m" 'Buffer-menu-this-window)
82 (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window)
83 (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window)
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)
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)
100 (define-key Buffer-menu-mode-map "b" 'Buffer-menu-bury)
101 (define-key Buffer-menu-mode-map "g" 'Buffer-menu-revert)
102 (define-key Buffer-menu-mode-map "V" 'Buffer-menu-view)
103 (define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-mouse-select)
106 ;; Buffer Menu mode is suitable only for specially formatted data.
107 (put 'Buffer-menu-mode 'mode-class 'special)
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>
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.
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.
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.
125 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
126 \\[Buffer-menu-2-window] -- select that buffer in one window,
127 together with buffer selected before this one in another window.
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.
135 With prefix argument, also move up one line.
136 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
137 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
138 \\[Buffer-menu-revert] -- update the list of buffers.
139 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
140 (kill-all-local-variables)
141 (use-local-map Buffer-menu-mode-map)
142 (setq major-mode 'Buffer-menu-mode)
143 (setq mode-name "Buffer Menu")
144 (make-local-variable 'revert-buffer-function)
145 (setq revert-buffer-function 'Buffer-menu-revert-function)
146 (setq truncate-lines t)
147 (setq buffer-read-only t)
148 (run-hooks 'buffer-menu-mode-hook))
150 (defun Buffer-menu-revert ()
151 "Update the list of buffers."
152 (interactive)
153 (revert-buffer))
155 (defun Buffer-menu-revert-function (ignore1 ignore2)
156 (list-buffers))
158 (defun Buffer-menu-buffer (error-if-non-existent-p)
159 "Return buffer described by this line of buffer menu."
160 (let* ((where (save-excursion
161 (beginning-of-line)
162 (+ (point) Buffer-menu-buffer-column)))
163 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
164 (buf (and (not (eobp)) (get-text-property where 'buffer))))
165 (if name
166 (or (get-buffer name)
167 (and buf (buffer-name buf) buf)
168 (if error-if-non-existent-p
169 (error "No buffer named `%s'" name)
170 nil))
171 (or (and buf (buffer-name buf) buf)
172 (if error-if-non-existent-p
173 (error "No buffer on this line")
174 nil)))))
176 (defun buffer-menu (&optional arg)
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.
180 Type q to remove the buffer menu from the display.
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.
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)."
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."))
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.
202 Type q to remove the buffer menu from the display.
203 For more information, see the function `buffer-menu'."
204 (interactive "P")
205 ;;; (setq Buffer-menu-window-config (current-window-configuration))
206 (switch-to-buffer-other-window (list-buffers-noselect arg))
207 (message
208 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
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))))
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")
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 " * " " ")))))
234 (forward-line (if backup -1 1)))
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))
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")
248 (beginning-of-line)
249 (if (looking-at " [-M]") ;header lines
250 (ding)
251 (let ((buffer-read-only nil))
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))))))
265 (defun Buffer-menu-delete-backwards (&optional arg)
266 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
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)))
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)
277 (if (looking-at " [-M]") ;header lines
278 (ding)
279 (let ((buffer-read-only nil))
280 (forward-char 1)
281 (delete-char 1)
282 (insert ?S)
283 (forward-line 1))))
285 (defun Buffer-menu-not-modified (&optional arg)
286 "Mark buffer on this line as unmodified (no changes to save)."
287 (interactive "P")
288 (save-excursion
289 (set-buffer (Buffer-menu-buffer t))
290 (set-buffer-modified-p arg))
291 (save-excursion
292 (beginning-of-line)
293 (forward-char 1)
294 (if (= (char-after (point)) (if arg ? ?*))
295 (let ((buffer-read-only nil))
296 (delete-char 1)
297 (insert (if arg ?* ? ))))))
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)
324 (save-excursion (kill-buffer buf)))
325 (if (and buf (buffer-name buf))
326 (progn (delete-char 1)
327 (insert ? ))
328 (delete-region (point) (progn (forward-line 1) (point)))
329 (forward-char -1)))))))
331 (defun Buffer-menu-select ()
332 "Select this line's buffer; also display buffers marked with `>'.
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."
336 (interactive)
337 (let ((buff (Buffer-menu-buffer t))
338 (menu (current-buffer))
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)
349 tem (/ (1- (frame-height)) (1+ (length others))))
350 (delete-other-windows)
351 (switch-to-buffer buff)
352 (or (eq menu buff)
353 (bury-buffer menu))
354 (if (equal (length others) 0)
355 (progn
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)))
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!
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"))))
381 (defun Buffer-menu-1-window ()
382 "Select this line's buffer, alone, in full frame."
383 (interactive)
384 (switch-to-buffer (Buffer-menu-buffer t))
385 (bury-buffer (other-buffer))
386 (delete-other-windows))
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)))
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))))
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)))
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)))
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)))
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))
425 (delete-other-windows)
426 (switch-to-buffer (other-buffer))
427 (pop-to-buffer buff)
428 (bury-buffer menu)))
430 (defun Buffer-menu-toggle-read-only ()
431 "Toggle read-only status of buffer on this line, perhaps via version control."
432 (interactive)
433 (let (char)
434 (save-excursion
435 (set-buffer (Buffer-menu-buffer t))
436 (vc-toggle-read-only)
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))))))
446 (defun Buffer-menu-bury ()
447 "Bury the buffer listed on this line."
448 (interactive)
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"))))
463 (defun Buffer-menu-view ()
464 "View this line's buffer in View mode."
465 (interactive)
466 (view-buffer (Buffer-menu-buffer t)))
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)))
475 (define-key ctl-x-map "\C-b" 'list-buffers)
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.
483 For more information, see the function `buffer-menu'."
484 (interactive "P")
485 (display-buffer (list-buffers-noselect files-only)))
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.
493 For more information, see the function `buffer-menu'."
494 (let ((old-buffer (current-buffer))
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))
502 (princ "\
503 MR Buffer Size Mode File
504 -- ------ ---- ---- ----
506 ;; Record the column where buffer names start.
507 (setq Buffer-menu-buffer-column 4)
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.
527 ((and (string= (substring name 0 1) " ") (null file)))
528 ;; Maybe don't mention buffers without files.
529 ((and files-only (not file)))
530 ((string= name "*Buffer List*"))
531 ;; Otherwise output info.
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
562 'mouse-face 'highlight)
563 (put-text-property this-buffer-line-start name-end
564 'help-echo "mouse-2: select this buffer"))
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")))))
579 (Buffer-menu-mode)
580 ;; DESIRED-POINT doesn't have to be set; it is not when the
581 ;; current buffer is not displayed for some reason.
582 (and desired-point
583 (goto-char desired-point))
584 (current-buffer))))
586 ;;; buff-menu.el ends here