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