*** empty log message ***
[emacs.git] / lisp / buff-menu.el
blob3e509b5198f4d388866c2ec0caeea961bf6d889b
1 ;;; buff-menu.el --- buffer menu main function and support functions.
3 ;; Copyright (C) 1985, 1986, 1987, 1992 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 ;;; Code:
25 ; Put buffer *Buffer List* into proper mode right away
26 ; so that from now on even list-buffers is enough to get a buffer menu.
28 (defvar Buffer-menu-mode-map nil "")
30 (if Buffer-menu-mode-map
32 (setq Buffer-menu-mode-map (make-keymap))
33 (suppress-keymap Buffer-menu-mode-map t)
34 (define-key Buffer-menu-mode-map "q" 'Buffer-menu-select)
35 (define-key Buffer-menu-mode-map "2" 'Buffer-menu-2-window)
36 (define-key Buffer-menu-mode-map "1" 'Buffer-menu-1-window)
37 (define-key Buffer-menu-mode-map "f" 'Buffer-menu-this-window)
38 (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window)
39 (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window)
40 (define-key Buffer-menu-mode-map "s" 'Buffer-menu-save)
41 (define-key Buffer-menu-mode-map "d" 'Buffer-menu-delete)
42 (define-key Buffer-menu-mode-map "k" 'Buffer-menu-delete)
43 (define-key Buffer-menu-mode-map "\C-d" 'Buffer-menu-delete-backwards)
44 (define-key Buffer-menu-mode-map "\C-k" 'Buffer-menu-delete)
45 (define-key Buffer-menu-mode-map "x" 'Buffer-menu-execute)
46 (define-key Buffer-menu-mode-map " " 'next-line)
47 (define-key Buffer-menu-mode-map "n" 'next-line)
48 (define-key Buffer-menu-mode-map "p" 'previous-line)
49 (define-key Buffer-menu-mode-map "\177" 'Buffer-menu-backup-unmark)
50 (define-key Buffer-menu-mode-map "~" 'Buffer-menu-not-modified)
51 (define-key Buffer-menu-mode-map "?" 'describe-mode)
52 (define-key Buffer-menu-mode-map "u" 'Buffer-menu-unmark)
53 (define-key Buffer-menu-mode-map "m" 'Buffer-menu-mark)
54 (define-key Buffer-menu-mode-map "t" 'Buffer-menu-visit-tags-table))
56 ;; Buffer Menu mode is suitable only for specially formatted data.
57 (put 'Buffer-menu-mode 'mode-class 'special)
59 (defun Buffer-menu-mode ()
60 "Major mode for editing a list of buffers.
61 Each line describes one of the buffers in Emacs.
62 Letters do not insert themselves; instead, they are commands.
63 \\<Buffer-menu-mode-map>
64 \\[Buffer-menu-mark] -- mark buffer to be displayed.
65 \\[Buffer-menu-select] -- select buffer of line point is on.
66 Also show buffers marked with m in other windows.
67 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
68 \\[Buffer-menu-2-window] -- select that buffer in one window,
69 together with buffer selected before this one in another window.
70 \\[Buffer-menu-this-window] -- select that buffer in place of the buffer menu buffer.
71 \\[Buffer-menu-other-window] -- select that buffer in another window,
72 so the buffer menu buffer remains visible in its window.
73 \\[Buffer-menu-switch-other-window] -- switch the other window to this buffer.
74 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
75 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
76 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
77 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
78 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
79 \\[Buffer-menu-execute] -- delete or save marked buffers.
80 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
81 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks."
82 (kill-all-local-variables)
83 (use-local-map Buffer-menu-mode-map)
84 (setq truncate-lines t)
85 (setq buffer-read-only t)
86 (setq major-mode 'Buffer-menu-mode)
87 (setq mode-name "Buffer Menu")
88 (run-hooks 'buffer-menu-mode-hook))
90 (defvar Buffer-menu-buffer-column 4)
92 (defun Buffer-menu-buffer (error-if-non-existent-p)
93 "Return buffer described by this line of buffer menu."
94 (save-excursion
95 (beginning-of-line)
96 (forward-char Buffer-menu-buffer-column)
97 (let ((start (point))
98 string)
99 ;; End of buffer name marked by tab or two spaces.
100 (re-search-forward "\t\\| ")
101 (skip-chars-backward " \t")
102 (setq string (buffer-substring start (point)))
103 (or (get-buffer string)
104 (if error-if-non-existent-p
105 (error "No buffer named \"%s\"" string)
106 nil)))))
108 (defun buffer-menu (&optional arg)
109 "Make a menu of buffers so you can save, delete or select them.
110 With argument, show only buffers that are visiting files.
111 Type ? after invocation to get help on commands available.
112 Type q immediately to make the buffer menu go away."
113 (interactive "P")
114 (list-buffers arg)
115 (pop-to-buffer "*Buffer List*")
116 (forward-line 2)
117 (message
118 "Commands: d, s, x; 1, 2, m, u, q; delete; ~; ? for help."))
120 (defun Buffer-menu-mark ()
121 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
122 (interactive)
123 (beginning-of-line)
124 (if (looking-at " [-M]")
125 (ding)
126 (let ((buffer-read-only nil))
127 (delete-char 1)
128 (insert ?>)
129 (forward-line 1))))
131 (defun Buffer-menu-unmark ()
132 "Cancel all requested operations on buffer on this line."
133 (interactive)
134 (beginning-of-line)
135 (if (looking-at " [-M]")
136 (ding)
137 (let* ((buf (Buffer-menu-buffer t))
138 (mod (buffer-modified-p buf))
139 (readonly (save-excursion (set-buffer buf) buffer-read-only))
140 (buffer-read-only nil))
141 (delete-char 3)
142 (insert (if readonly (if mod " *%" " %") (if mod " * " " ")))))
143 (forward-line 1))
145 (defun Buffer-menu-backup-unmark ()
146 "Move up and cancel all requested operations on buffer on line above."
147 (interactive)
148 (forward-line -1)
149 (Buffer-menu-unmark)
150 (forward-line -1))
152 (defun Buffer-menu-delete ()
153 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
154 (interactive)
155 (beginning-of-line)
156 (if (looking-at " [-M]") ;header lines
157 (ding)
158 (let ((buffer-read-only nil))
159 (delete-char 1)
160 (insert ?D)
161 (forward-line 1))))
163 (defun Buffer-menu-delete-backwards ()
164 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
165 and then move up one line"
166 (interactive)
167 (Buffer-menu-delete)
168 (forward-line -2)
169 (if (looking-at " [-M]") (forward-line 1)))
171 (defun Buffer-menu-save ()
172 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
173 (interactive)
174 (beginning-of-line)
175 (forward-char 1)
176 (if (looking-at " [-M]") ;header lines
177 (ding)
178 (let ((buffer-read-only nil))
179 (delete-char 1)
180 (insert ?S)
181 (forward-line 1))))
183 (defun Buffer-menu-not-modified ()
184 "Mark buffer on this line as unmodified (no changes to save)."
185 (interactive)
186 (save-excursion
187 (set-buffer (Buffer-menu-buffer t))
188 (set-buffer-modified-p nil))
189 (save-excursion
190 (beginning-of-line)
191 (forward-char 1)
192 (if (looking-at "\\*")
193 (let ((buffer-read-only nil))
194 (delete-char 1)
195 (insert ? )))))
197 (defun Buffer-menu-execute ()
198 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
199 (interactive)
200 (save-excursion
201 (goto-char (point-min))
202 (forward-line 1)
203 (while (re-search-forward "^.S" nil t)
204 (let ((modp nil))
205 (save-excursion
206 (set-buffer (Buffer-menu-buffer t))
207 (save-buffer)
208 (setq modp (buffer-modified-p)))
209 (let ((buffer-read-only nil))
210 (delete-char -1)
211 (insert (if modp ?* ? ))))))
212 (save-excursion
213 (goto-char (point-min))
214 (forward-line 1)
215 (let ((buff-menu-buffer (current-buffer))
216 (buffer-read-only nil))
217 (while (search-forward "\nD" nil t)
218 (forward-char -1)
219 (let ((buf (Buffer-menu-buffer nil)))
220 (or (eq buf nil)
221 (eq buf buff-menu-buffer)
222 (save-excursion (kill-buffer buf))))
223 (if (Buffer-menu-buffer nil)
224 (progn (delete-char 1)
225 (insert ? ))
226 (delete-region (point) (progn (forward-line 1) (point)))
227 (forward-char -1))))))
229 (defun Buffer-menu-select ()
230 "Select this line's buffer; also display buffers marked with `>'.
231 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
232 (interactive)
233 (let ((buff (Buffer-menu-buffer t))
234 (menu (current-buffer))
235 (others ())
236 tem)
237 (goto-char (point-min))
238 (while (search-forward "\n>" nil t)
239 (setq tem (Buffer-menu-buffer t))
240 (let ((buffer-read-only nil))
241 (delete-char -1)
242 (insert ?\ ))
243 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
244 (setq others (nreverse others)
245 tem (/ (1- (frame-height)) (1+ (length others))))
246 (delete-other-windows)
247 (switch-to-buffer buff)
248 (or (eq menu buff)
249 (bury-buffer menu))
250 (while others
251 (split-window nil tem)
252 (other-window 1)
253 (switch-to-buffer (car others))
254 (setq others (cdr others)))
255 (other-window 1))) ;back to the beginning!
257 (defun Buffer-menu-visit-tags-table ()
258 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
259 (interactive)
260 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
261 (if file
262 (visit-tags-table file)
263 (error "Specified buffer has no file"))))
265 (defun Buffer-menu-1-window ()
266 "Select this line's buffer, alone, in full frame."
267 (interactive)
268 (switch-to-buffer (Buffer-menu-buffer t))
269 (bury-buffer (other-buffer))
270 (delete-other-windows))
272 (defun Buffer-menu-this-window ()
273 "Select this line's buffer in this window."
274 (interactive)
275 (switch-to-buffer (Buffer-menu-buffer t)))
277 (defun Buffer-menu-other-window ()
278 "Select this line's buffer in other window, leaving buffer menu visible."
279 (interactive)
280 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
282 (defun Buffer-menu-switch-other-window ()
283 "Make the other window select this line's buffer.
284 The current window remains selected."
285 (interactive)
286 (display-buffer (Buffer-menu-buffer t)))
288 (defun Buffer-menu-2-window ()
289 "Select this line's buffer, with previous buffer in second window."
290 (interactive)
291 (let ((buff (Buffer-menu-buffer t))
292 (menu (current-buffer))
293 (pop-up-windows t))
294 (switch-to-buffer (other-buffer))
295 (pop-to-buffer buff)
296 (bury-buffer menu)))
298 ;;; buff-menu.el ends here