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