194
|
1 ;; Buffer menu main function and support functions.
|
|
2 ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; This file is part of GNU Emacs.
|
|
5
|
|
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 ;; it under the terms of the GNU General Public License as published by
|
|
8 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
9 ;; any later version.
|
|
10
|
|
11 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 ;; GNU General Public License for more details.
|
|
15
|
|
16 ;; You should have received a copy of the GNU General Public License
|
|
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20
|
|
21 ; Put buffer *Buffer List* into proper mode right away
|
|
22 ; so that from now on even list-buffers is enough to get a buffer menu.
|
|
23
|
|
24 (defvar Buffer-menu-mode-map nil "")
|
|
25
|
|
26 (if Buffer-menu-mode-map
|
|
27 ()
|
|
28 (setq Buffer-menu-mode-map (make-keymap))
|
|
29 (suppress-keymap Buffer-menu-mode-map t)
|
|
30 (define-key Buffer-menu-mode-map "q" 'Buffer-menu-select)
|
|
31 (define-key Buffer-menu-mode-map "2" 'Buffer-menu-2-window)
|
|
32 (define-key Buffer-menu-mode-map "1" 'Buffer-menu-1-window)
|
|
33 (define-key Buffer-menu-mode-map "f" 'Buffer-menu-this-window)
|
|
34 (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window)
|
|
35 (define-key Buffer-menu-mode-map "s" 'Buffer-menu-save)
|
|
36 (define-key Buffer-menu-mode-map "d" 'Buffer-menu-delete)
|
|
37 (define-key Buffer-menu-mode-map "k" 'Buffer-menu-delete)
|
|
38 (define-key Buffer-menu-mode-map "\C-d" 'Buffer-menu-delete-backwards)
|
|
39 (define-key Buffer-menu-mode-map "\C-k" 'Buffer-menu-delete)
|
|
40 (define-key Buffer-menu-mode-map "x" 'Buffer-menu-execute)
|
|
41 (define-key Buffer-menu-mode-map " " 'next-line)
|
|
42 (define-key Buffer-menu-mode-map "n" 'next-line)
|
|
43 (define-key Buffer-menu-mode-map "p" 'previous-line)
|
|
44 (define-key Buffer-menu-mode-map "\177" 'Buffer-menu-backup-unmark)
|
|
45 (define-key Buffer-menu-mode-map "~" 'Buffer-menu-not-modified)
|
|
46 (define-key Buffer-menu-mode-map "?" 'describe-mode)
|
|
47 (define-key Buffer-menu-mode-map "u" 'Buffer-menu-unmark)
|
|
48 (define-key Buffer-menu-mode-map "m" 'Buffer-menu-mark)
|
|
49 (define-key Buffer-menu-mode-map "t" 'Buffer-menu-visit-tags-table))
|
|
50
|
|
51 ;; Buffer Menu mode is suitable only for specially formatted data.
|
|
52 (put 'Buffer-menu-mode 'mode-class 'special)
|
|
53
|
|
54 (defun Buffer-menu-mode ()
|
|
55 "Major mode for editing a list of buffers.
|
|
56 Each line describes one of the buffers in Emacs.
|
|
57 Letters do not insert themselves; instead, they are commands.
|
|
58 \\<Buffer-menu-mode-map>
|
|
59 \\[Buffer-menu-mark] -- mark buffer to be displayed.
|
|
60 \\[Buffer-menu-select] -- select buffer of line point is on.
|
|
61 Also show buffers marked with m in other windows.
|
|
62 \\[Buffer-menu-1-window] -- select that buffer in full-screen window.
|
|
63 \\[Buffer-menu-2-window] -- select that buffer in one window,
|
|
64 together with buffer selected before this one in another window.
|
|
65 \\[Buffer-menu-this-window] -- select that buffer in place of the buffer menu buffer.
|
|
66 \\[Buffer-menu-other-window] -- select that buffer in another window,
|
|
67 so the buffer menu buffer remains visible in its window.
|
|
68 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
|
|
69 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
|
|
70 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
|
|
71 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
|
|
72 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
|
|
73 \\[Buffer-menu-execute] -- delete or save marked buffers.
|
|
74 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
|
|
75 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks."
|
|
76 (kill-all-local-variables)
|
|
77 (use-local-map Buffer-menu-mode-map)
|
|
78 (setq truncate-lines t)
|
|
79 (setq buffer-read-only t)
|
|
80 (setq major-mode 'Buffer-menu-mode)
|
|
81 (setq mode-name "Buffer Menu")
|
|
82 (run-hooks 'buffer-menu-mode-hook))
|
|
83
|
|
84 (defvar Buffer-menu-buffer-column 4)
|
|
85
|
|
86 (defun Buffer-menu-buffer (error-if-non-existent-p)
|
|
87 "Return buffer described by this line of buffer menu."
|
|
88 (save-excursion
|
|
89 (beginning-of-line)
|
|
90 (forward-char Buffer-menu-buffer-column)
|
|
91 (let ((start (point))
|
|
92 string)
|
|
93 ;; End of buffer name marked by tab or two spaces.
|
|
94 (re-search-forward "\t\\| ")
|
|
95 (skip-chars-backward " \t")
|
|
96 (setq string (buffer-substring start (point)))
|
|
97 (or (get-buffer string)
|
|
98 (if error-if-non-existent-p
|
|
99 (error "No buffer named \"%s\"" string)
|
|
100 nil)))))
|
|
101
|
|
102 (defun buffer-menu (arg)
|
|
103 "Make a menu of buffers so you can save, delete or select them.
|
|
104 With argument, show only buffers that are visiting files.
|
|
105 Type ? after invocation to get help on commands available.
|
|
106 Type q immediately to make the buffer menu go away."
|
|
107 (interactive "P")
|
|
108 (list-buffers arg)
|
|
109 (pop-to-buffer "*Buffer List*")
|
|
110 (forward-line 2)
|
|
111 (message
|
|
112 "Commands: d, s, x; 1, 2, m, u, q; delete; ~; ? for help."))
|
|
113
|
|
114 (defun Buffer-menu-mark ()
|
|
115 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
|
|
116 (interactive)
|
|
117 (beginning-of-line)
|
|
118 (if (looking-at " [-M]")
|
|
119 (ding)
|
|
120 (let ((buffer-read-only nil))
|
|
121 (delete-char 1)
|
|
122 (insert ?>)
|
|
123 (forward-line 1))))
|
|
124
|
|
125 (defun Buffer-menu-unmark ()
|
|
126 "Cancel all requested operations on buffer on this line."
|
|
127 (interactive)
|
|
128 (beginning-of-line)
|
|
129 (if (looking-at " [-M]")
|
|
130 (ding)
|
|
131 (let* ((buf (Buffer-menu-buffer t))
|
|
132 (mod (buffer-modified-p buf))
|
|
133 (readonly (save-excursion (set-buffer buf) buffer-read-only))
|
|
134 (buffer-read-only nil))
|
|
135 (delete-char 3)
|
|
136 (insert (if readonly (if mod " *%" " %") (if mod " * " " ")))))
|
|
137 (forward-line 1))
|
|
138
|
|
139 (defun Buffer-menu-backup-unmark ()
|
|
140 "Move up and cancel all requested operations on buffer on line above."
|
|
141 (interactive)
|
|
142 (forward-line -1)
|
|
143 (Buffer-menu-unmark)
|
|
144 (forward-line -1))
|
|
145
|
|
146 (defun Buffer-menu-delete ()
|
|
147 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
|
|
148 (interactive)
|
|
149 (beginning-of-line)
|
|
150 (if (looking-at " [-M]") ;header lines
|
|
151 (ding)
|
|
152 (let ((buffer-read-only nil))
|
|
153 (delete-char 1)
|
|
154 (insert ?D)
|
|
155 (forward-line 1))))
|
|
156
|
|
157 (defun Buffer-menu-delete-backwards ()
|
|
158 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
|
|
159 and then move up one line"
|
|
160 (interactive)
|
|
161 (Buffer-menu-delete)
|
|
162 (forward-line -2)
|
|
163 (if (looking-at " [-M]") (forward-line 1)))
|
|
164
|
|
165 (defun Buffer-menu-save ()
|
|
166 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
|
|
167 (interactive)
|
|
168 (beginning-of-line)
|
|
169 (forward-char 1)
|
|
170 (if (looking-at " [-M]") ;header lines
|
|
171 (ding)
|
|
172 (let ((buffer-read-only nil))
|
|
173 (delete-char 1)
|
|
174 (insert ?S)
|
|
175 (forward-line 1))))
|
|
176
|
|
177 (defun Buffer-menu-not-modified ()
|
|
178 "Mark buffer on this line as unmodified (no changes to save)."
|
|
179 (interactive)
|
|
180 (save-excursion
|
|
181 (set-buffer (Buffer-menu-buffer t))
|
|
182 (set-buffer-modified-p nil))
|
|
183 (save-excursion
|
|
184 (beginning-of-line)
|
|
185 (forward-char 1)
|
|
186 (if (looking-at "\\*")
|
|
187 (let ((buffer-read-only nil))
|
|
188 (delete-char 1)
|
|
189 (insert ? )))))
|
|
190
|
|
191 (defun Buffer-menu-execute ()
|
|
192 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
|
|
193 (interactive)
|
|
194 (save-excursion
|
|
195 (goto-char (point-min))
|
|
196 (forward-line 1)
|
|
197 (while (re-search-forward "^.S" nil t)
|
|
198 (let ((modp nil))
|
|
199 (save-excursion
|
|
200 (set-buffer (Buffer-menu-buffer t))
|
|
201 (save-buffer)
|
|
202 (setq modp (buffer-modified-p)))
|
|
203 (let ((buffer-read-only nil))
|
|
204 (delete-char -1)
|
|
205 (insert (if modp ?* ? ))))))
|
|
206 (save-excursion
|
|
207 (goto-char (point-min))
|
|
208 (forward-line 1)
|
|
209 (let ((buff-menu-buffer (current-buffer))
|
|
210 (buffer-read-only nil))
|
|
211 (while (search-forward "\nD" nil t)
|
|
212 (forward-char -1)
|
|
213 (let ((buf (Buffer-menu-buffer nil)))
|
|
214 (or (eq buf nil)
|
|
215 (eq buf buff-menu-buffer)
|
|
216 (save-excursion (kill-buffer buf))))
|
|
217 (if (Buffer-menu-buffer nil)
|
|
218 (progn (delete-char 1)
|
|
219 (insert ? ))
|
|
220 (delete-region (point) (progn (forward-line 1) (point)))
|
|
221 (forward-char -1))))))
|
|
222
|
|
223 (defun Buffer-menu-select ()
|
|
224 "Select this line's buffer; also display buffers marked with `>'.
|
|
225 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
|
|
226 (interactive)
|
|
227 (let ((buff (Buffer-menu-buffer t))
|
|
228 (menu (current-buffer))
|
|
229 (others ())
|
|
230 tem)
|
|
231 (goto-char (point-min))
|
|
232 (while (search-forward "\n>" nil t)
|
|
233 (setq tem (Buffer-menu-buffer t))
|
|
234 (let ((buffer-read-only nil))
|
|
235 (delete-char -1)
|
|
236 (insert ?\ ))
|
|
237 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
|
|
238 (setq others (nreverse others)
|
|
239 tem (/ (1- (screen-height)) (1+ (length others))))
|
|
240 (delete-other-windows)
|
|
241 (switch-to-buffer buff)
|
|
242 (or (eq menu buff)
|
|
243 (bury-buffer menu))
|
|
244 (while others
|
|
245 (split-window nil tem)
|
|
246 (other-window 1)
|
|
247 (switch-to-buffer (car others))
|
|
248 (setq others (cdr others)))
|
|
249 (other-window 1))) ;back to the beginning!
|
|
250
|
|
251 (defun Buffer-menu-visit-tags-table ()
|
|
252 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
|
|
253 (interactive)
|
|
254 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
|
|
255 (if file
|
|
256 (visit-tags-table file)
|
|
257 (error "Specified buffer has no file"))))
|
|
258
|
|
259 (defun Buffer-menu-1-window ()
|
|
260 "Select this line's buffer, alone, in full screen."
|
|
261 (interactive)
|
|
262 (switch-to-buffer (Buffer-menu-buffer t))
|
|
263 (bury-buffer (other-buffer))
|
|
264 (delete-other-windows))
|
|
265
|
|
266 (defun Buffer-menu-this-window ()
|
|
267 "Select this line's buffer in this window."
|
|
268 (interactive)
|
|
269 (switch-to-buffer (Buffer-menu-buffer t)))
|
|
270
|
|
271 (defun Buffer-menu-other-window ()
|
|
272 "Select this line's buffer in other window, leaving buffer menu visible."
|
|
273 (interactive)
|
|
274 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
|
|
275
|
|
276 (defun Buffer-menu-2-window ()
|
|
277 "Select this line's buffer, with previous buffer in second window."
|
|
278 (interactive)
|
|
279 (let ((buff (Buffer-menu-buffer t))
|
|
280 (menu (current-buffer))
|
|
281 (pop-up-windows t))
|
|
282 (switch-to-buffer (other-buffer))
|
|
283 (pop-to-buffer buff)
|
|
284 (bury-buffer menu)))
|