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