Commit | Line | Data |
---|---|---|
e5f9458f | 1 | ;;; buff-menu.el --- Interface for viewing and manipulating buffers |
c0274f38 | 2 | |
acaf905b | 3 | ;; Copyright (C) 1985-1987, 1993-1995, 2000-2012 |
e9bffc61 | 4 | ;; Free Software Foundation, Inc. |
2dd96f23 | 5 | |
9750e079 | 6 | ;; Maintainer: FSF |
6d6c3f84 | 7 | ;; Keywords: convenience |
bd78fa1d | 8 | ;; Package: emacs |
9750e079 | 9 | |
2dd96f23 JB |
10 | ;; This file is part of GNU Emacs. |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
2dd96f23 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
2dd96f23 JB |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
2dd96f23 | 24 | |
e41b2db1 ER |
25 | ;;; Commentary: |
26 | ||
e5f9458f CY |
27 | ;; The Buffer Menu is used to view, edit, delete, or change attributes |
28 | ;; of buffers. The entry points are C-x C-b (`list-buffers') and | |
29 | ;; M-x buffer-menu. | |
e41b2db1 | 30 | |
5de9bdab | 31 | ;;; Code: |
10e1dad9 | 32 | |
e5f9458f | 33 | (require 'tabulated-list) |
2dd96f23 | 34 | |
bc7bb432 JB |
35 | (defgroup Buffer-menu nil |
36 | "Show a menu of all buffers in a buffer." | |
37 | :group 'tools | |
38 | :group 'convenience) | |
39 | ||
40 | (defcustom Buffer-menu-use-header-line t | |
e5f9458f | 41 | "If non-nil, use the header line to display Buffer Menu column titles." |
bc7bb432 JB |
42 | :type 'boolean |
43 | :group 'Buffer-menu) | |
44 | ||
d247d0fe | 45 | (defface buffer-menu-buffer |
bc7bb432 | 46 | '((t (:weight bold))) |
e5f9458f | 47 | "Face for buffer names in the Buffer Menu." |
2a8f2d4c | 48 | :group 'Buffer-menu) |
d247d0fe | 49 | (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer) |
bc7bb432 | 50 | |
e5f9458f CY |
51 | (defcustom Buffer-menu-buffer+size-width nil |
52 | "Combined width of buffer name and size columns in Buffer Menu. | |
ac59c2f6 CY |
53 | If nil, use `Buffer-menu-name-width' and `Buffer-menu-size-width'. |
54 | ||
55 | If non-nil, the value of `Buffer-menu-name-width' is overridden; | |
56 | the name column is assigned width `Buffer-menu-buffer+size-width' | |
57 | minus `Buffer-menu-size-width'. This use is deprecated." | |
bc7bb432 | 58 | :type 'number |
e5f9458f CY |
59 | :group 'Buffer-menu |
60 | :version "24.2") | |
61 | ||
62 | (make-obsolete-variable 'Buffer-menu-buffer+size-width | |
63 | "`Buffer-menu-name-width' and `Buffer-menu-size-width'" | |
64 | "24.2") | |
65 | ||
66 | (defcustom Buffer-menu-name-width 19 | |
67 | "Width of buffer size column in the Buffer Menu." | |
68 | :type 'number | |
69 | :group 'Buffer-menu | |
70 | :version "24.2") | |
71 | ||
72 | (defcustom Buffer-menu-size-width 7 | |
73 | "Width of buffer name column in the Buffer Menu." | |
74 | :type 'number | |
75 | :group 'Buffer-menu | |
76 | :version "24.2") | |
bc7bb432 | 77 | |
4f0992b3 | 78 | (defcustom Buffer-menu-mode-width 16 |
e5f9458f | 79 | "Width of mode name column in the Buffer Menu." |
bc7bb432 JB |
80 | :type 'number |
81 | :group 'Buffer-menu) | |
82 | ||
3ce5f932 LT |
83 | (defcustom Buffer-menu-use-frame-buffer-list t |
84 | "If non-nil, the Buffer Menu uses the selected frame's buffer list. | |
85 | Buffers that were never selected in that frame are listed at the end. | |
86 | If the value is nil, the Buffer Menu uses the global buffer list. | |
87 | This variable matters if the Buffer Menu is sorted by visited order, | |
88 | as it is by default." | |
89 | :type 'boolean | |
90 | :group 'Buffer-menu | |
91 | :version "22.1") | |
92 | ||
7b0a86ab | 93 | (defvar Buffer-menu-files-only nil |
e5f9458f CY |
94 | "Non-nil if the current Buffer Menu lists only file buffers. |
95 | This is set by the prefix argument to `buffer-menu' and related | |
96 | commands.") | |
392225ee JB |
97 | (make-variable-buffer-local 'Buffer-menu-files-only) |
98 | ||
e5f9458f CY |
99 | (defvar Info-current-file) ; from info.el |
100 | (defvar Info-current-node) ; from info.el | |
fb5614e8 | 101 | |
392225ee | 102 | (defvar Buffer-menu-mode-map |
e5f9458f | 103 | (let ((map (make-sparse-keymap)) |
6a6baf11 | 104 | (menu-map (make-sparse-keymap))) |
e5f9458f | 105 | (set-keymap-parent map tabulated-list-mode-map) |
392225ee JB |
106 | (define-key map "v" 'Buffer-menu-select) |
107 | (define-key map "2" 'Buffer-menu-2-window) | |
108 | (define-key map "1" 'Buffer-menu-1-window) | |
109 | (define-key map "f" 'Buffer-menu-this-window) | |
110 | (define-key map "e" 'Buffer-menu-this-window) | |
111 | (define-key map "\C-m" 'Buffer-menu-this-window) | |
112 | (define-key map "o" 'Buffer-menu-other-window) | |
113 | (define-key map "\C-o" 'Buffer-menu-switch-other-window) | |
114 | (define-key map "s" 'Buffer-menu-save) | |
115 | (define-key map "d" 'Buffer-menu-delete) | |
116 | (define-key map "k" 'Buffer-menu-delete) | |
392225ee | 117 | (define-key map "\C-k" 'Buffer-menu-delete) |
e5f9458f | 118 | (define-key map "\C-d" 'Buffer-menu-delete-backwards) |
392225ee JB |
119 | (define-key map "x" 'Buffer-menu-execute) |
120 | (define-key map " " 'next-line) | |
392225ee JB |
121 | (define-key map "\177" 'Buffer-menu-backup-unmark) |
122 | (define-key map "~" 'Buffer-menu-not-modified) | |
392225ee JB |
123 | (define-key map "u" 'Buffer-menu-unmark) |
124 | (define-key map "m" 'Buffer-menu-mark) | |
125 | (define-key map "t" 'Buffer-menu-visit-tags-table) | |
126 | (define-key map "%" 'Buffer-menu-toggle-read-only) | |
127 | (define-key map "b" 'Buffer-menu-bury) | |
392225ee JB |
128 | (define-key map "V" 'Buffer-menu-view) |
129 | (define-key map "T" 'Buffer-menu-toggle-files-only) | |
9cda680d JL |
130 | (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers) |
131 | (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp) | |
e5f9458f | 132 | |
0d909786 CY |
133 | (define-key map [mouse-2] 'Buffer-menu-mouse-select) |
134 | (define-key map [follow-link] 'mouse-face) | |
135 | ||
c9753fb4 | 136 | (define-key map [menu-bar Buffer-menu-mode] (cons (purecopy "Buffer-Menu") menu-map)) |
1ec4b7b2 SM |
137 | (bindings--define-key menu-map [quit] |
138 | '(menu-item "Quit" quit-window | |
139 | :help "Remove the buffer menu from the display")) | |
140 | (bindings--define-key menu-map [rev] | |
141 | '(menu-item "Refresh" revert-buffer | |
142 | :help "Refresh the *Buffer List* buffer contents")) | |
143 | (bindings--define-key menu-map [s0] menu-bar-separator) | |
144 | (bindings--define-key menu-map [tf] | |
145 | '(menu-item "Show Only File Buffers" Buffer-menu-toggle-files-only | |
6a6baf11 | 146 | :button (:toggle . Buffer-menu-files-only) |
1ec4b7b2 SM |
147 | :help "Toggle whether the current buffer-menu displays only file buffers")) |
148 | (bindings--define-key menu-map [s1] menu-bar-separator) | |
6a6baf11 | 149 | ;; FIXME: The "Select" entries could use better names... |
1ec4b7b2 SM |
150 | (bindings--define-key menu-map [sel] |
151 | '(menu-item "Select Marked" Buffer-menu-select | |
152 | :help "Select this line's buffer; also display buffers marked with `>'")) | |
153 | (bindings--define-key menu-map [bm2] | |
154 | '(menu-item "Select Two" Buffer-menu-2-window | |
155 | :help "Select this line's buffer, with previous buffer in second window")) | |
156 | (bindings--define-key menu-map [bm1] | |
157 | '(menu-item "Select Current" Buffer-menu-1-window | |
158 | :help "Select this line's buffer, alone, in full frame")) | |
159 | (bindings--define-key menu-map [ow] | |
160 | '(menu-item "Select in Other Window" Buffer-menu-other-window | |
161 | :help "Select this line's buffer in other window, leaving buffer menu visible")) | |
162 | (bindings--define-key menu-map [tw] | |
163 | '(menu-item "Select in Current Window" Buffer-menu-this-window | |
164 | :help "Select this line's buffer in this window")) | |
165 | (bindings--define-key menu-map [s2] menu-bar-separator) | |
166 | (bindings--define-key menu-map [is] | |
167 | '(menu-item "Regexp Isearch Marked Buffers..." Buffer-menu-isearch-buffers-regexp | |
168 | :help "Search for a regexp through all marked buffers using Isearch")) | |
169 | (bindings--define-key menu-map [ir] | |
170 | '(menu-item "Isearch Marked Buffers..." Buffer-menu-isearch-buffers | |
171 | :help "Search for a string through all marked buffers using Isearch")) | |
172 | (bindings--define-key menu-map [s3] menu-bar-separator) | |
173 | (bindings--define-key menu-map [by] | |
174 | '(menu-item "Bury" Buffer-menu-bury | |
175 | :help "Bury the buffer listed on this line")) | |
176 | (bindings--define-key menu-map [vt] | |
177 | '(menu-item "Set Unmodified" Buffer-menu-not-modified | |
178 | :help "Mark buffer on this line as unmodified (no changes to save)")) | |
179 | (bindings--define-key menu-map [ex] | |
180 | '(menu-item "Execute" Buffer-menu-execute | |
181 | :help "Save and/or delete buffers marked with s or k commands")) | |
182 | (bindings--define-key menu-map [s4] menu-bar-separator) | |
183 | (bindings--define-key menu-map [delb] | |
184 | '(menu-item "Mark for Delete and Move Backwards" Buffer-menu-delete-backwards | |
185 | :help "Mark buffer on this line to be deleted by x command and move up one line")) | |
186 | (bindings--define-key menu-map [del] | |
187 | '(menu-item "Mark for Delete" Buffer-menu-delete | |
188 | :help "Mark buffer on this line to be deleted by x command")) | |
189 | ||
190 | (bindings--define-key menu-map [sv] | |
191 | '(menu-item "Mark for Save" Buffer-menu-save | |
192 | :help "Mark buffer on this line to be saved by x command")) | |
193 | (bindings--define-key menu-map [umk] | |
194 | '(menu-item "Unmark" Buffer-menu-unmark | |
195 | :help "Cancel all requested operations on buffer on this line and move down")) | |
196 | (bindings--define-key menu-map [mk] | |
197 | '(menu-item "Mark" Buffer-menu-mark | |
198 | :help "Mark buffer on this line for being displayed by v command")) | |
392225ee JB |
199 | map) |
200 | "Local keymap for `Buffer-menu-mode' buffers.") | |
2dd96f23 | 201 | |
e5bd0a28 SM |
202 | (define-obsolete-variable-alias 'buffer-menu-mode-hook |
203 | 'Buffer-menu-mode-hook "23.1") | |
204 | ||
e5f9458f CY |
205 | (define-derived-mode Buffer-menu-mode tabulated-list-mode "Buffer Menu" |
206 | "Major mode for Buffer Menu buffers. | |
207 | The Buffer Menu is invoked by the commands \\[list-buffers], \\[buffer-menu], and | |
208 | \\[buffer-menu-other-window]. See `buffer-menu' for details." | |
7b0a86ab | 209 | (set (make-local-variable 'buffer-stale-function) |
06b60517 | 210 | (lambda (&optional _noconfirm) 'fast)) |
e5f9458f | 211 | (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t)) |
710e5da8 | 212 | |
e5f9458f CY |
213 | (defun buffer-menu (&optional arg) |
214 | "Switch to the Buffer Menu. | |
215 | By default, all buffers are listed except those whose names start | |
216 | with a space (which are for internal use). With prefix argument | |
217 | ARG, show only buffers that are visiting files. | |
7b0a86ab | 218 | |
e5f9458f CY |
219 | The first column (denoted \"C\") shows \".\" for the buffer from |
220 | which you came. It shows \">\" for buffers you mark to be | |
221 | displayed, and \"D\" for those you mark for deletion. | |
7b0a86ab | 222 | |
e5f9458f CY |
223 | The \"R\" column has a \"%\" if the buffer is read-only. |
224 | The \"M\" column has a \"*\" if it is modified, or \"S\" if you | |
225 | have marked it for saving. | |
226 | ||
227 | After this come the buffer name, its size in characters, its | |
228 | major mode, and the visited file name (if any). | |
229 | ||
230 | ||
231 | In the Buffer Menu, the following commands are defined: | |
232 | \\<Buffer-menu-mode-map> | |
233 | \\[quit-window] Remove the Buffer Menu from the display. | |
234 | \\[Buffer-menu-this-window] Select current line's buffer in place of the buffer menu. | |
235 | \\[Buffer-menu-other-window] Select that buffer in another window, | |
236 | so the Buffer Menu remains visible in its window. | |
237 | \\[Buffer-menu-view] Select current line's buffer, in View mode. | |
238 | \\[Buffer-menu-view-other-window] Select that buffer in | |
239 | another window, in view-mode. | |
240 | \\[Buffer-menu-switch-other-window] Make another window display that buffer. | |
241 | \\[Buffer-menu-mark] Mark current line's buffer to be displayed. | |
242 | \\[Buffer-menu-select] Select current line's buffer. | |
243 | Also show buffers marked with m, in other windows. | |
244 | \\[Buffer-menu-1-window] Select that buffer in full-frame window. | |
245 | \\[Buffer-menu-2-window] Select that buffer in one window, together with the | |
246 | buffer selected before this one in another window. | |
247 | \\[Buffer-menu-isearch-buffers] Incremental search in the marked buffers. | |
248 | \\[Buffer-menu-isearch-buffers-regexp] Isearch for regexp in the marked buffers. | |
249 | \\[Buffer-menu-visit-tags-table] visit-tags-table this buffer. | |
250 | \\[Buffer-menu-not-modified] Clear modified-flag on that buffer. | |
251 | \\[Buffer-menu-save] Mark that buffer to be saved, and move down. | |
252 | \\[Buffer-menu-delete] Mark that buffer to be deleted, and move down. | |
253 | \\[Buffer-menu-delete-backwards] Mark that buffer to be deleted, and move up. | |
254 | \\[Buffer-menu-execute] Delete or save marked buffers. | |
255 | \\[Buffer-menu-unmark] Remove all marks from current line. | |
256 | With prefix argument, also move up one line. | |
257 | \\[Buffer-menu-backup-unmark] Back up a line and remove marks. | |
258 | \\[Buffer-menu-toggle-read-only] Toggle read-only status of buffer on this line. | |
259 | \\[revert-buffer] Update the list of buffers. | |
260 | \\[Buffer-menu-toggle-files-only] Toggle whether the menu displays only file buffers. | |
261 | \\[Buffer-menu-bury] Bury the buffer listed on this line." | |
08c82d4d | 262 | (interactive "P") |
08c82d4d RS |
263 | (switch-to-buffer (list-buffers-noselect arg)) |
264 | (message | |
265 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) | |
266 | ||
267 | (defun buffer-menu-other-window (&optional arg) | |
e5f9458f CY |
268 | "Display the Buffer Menu in another window. |
269 | See `buffer-menu' for a description of the Buffer Menu. | |
270 | ||
271 | By default, all buffers are listed except those whose names start | |
272 | with a space (which are for internal use). With prefix argument | |
273 | ARG, show only buffers that are visiting files." | |
2dd96f23 | 274 | (interactive "P") |
08c82d4d | 275 | (switch-to-buffer-other-window (list-buffers-noselect arg)) |
2dd96f23 | 276 | (message |
3ed788ec RS |
277 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) |
278 | ||
f0809a9d | 279 | ;;;###autoload |
e5f9458f CY |
280 | (defun list-buffers (&optional arg) |
281 | "Display a list of existing buffers. | |
282 | The list is displayed in a buffer named \"*Buffer List*\". | |
283 | See `buffer-menu' for details about the Buffer Menu buffer. | |
284 | ||
285 | By default, all buffers are listed except those whose names start | |
286 | with a space (which are for internal use). With prefix argument | |
287 | ARG, show only buffers that are visiting files." | |
288 | (interactive "P") | |
289 | (display-buffer (list-buffers-noselect arg))) | |
290 | ||
291 | (defun Buffer-menu-toggle-files-only (arg) | |
292 | "Toggle whether the current buffer-menu displays only file buffers. | |
293 | With a positive ARG, display only file buffers. With zero or | |
294 | negative ARG, display other buffers as well." | |
295 | (interactive "P") | |
296 | (setq Buffer-menu-files-only | |
297 | (cond ((not arg) (not Buffer-menu-files-only)) | |
298 | ((> (prefix-numeric-value arg) 0) t))) | |
299 | (message (if Buffer-menu-files-only | |
300 | "Showing only file-visiting buffers." | |
301 | "Showing all non-internal buffers.")) | |
302 | (revert-buffer)) | |
303 | ||
304 | (defalias 'Buffer-menu-sort 'tabulated-list-sort) | |
305 | \f | |
306 | ||
307 | (defun Buffer-menu-buffer (&optional error-if-non-existent-p) | |
308 | "Return the buffer described by the current Buffer Menu line. | |
309 | If there is no buffer here, return nil if ERROR-IF-NON-EXISTENT-P | |
310 | is nil or omitted, and signal an error otherwise." | |
311 | (let ((buffer (tabulated-list-get-id))) | |
312 | (cond ((null buffer) | |
313 | (if error-if-non-existent-p | |
314 | (error "No buffer on this line"))) | |
315 | ((not (buffer-live-p buffer)) | |
316 | (if error-if-non-existent-p | |
317 | (error "This buffer has been killed"))) | |
318 | (t buffer)))) | |
319 | ||
bc7bb432 JB |
320 | (defun Buffer-menu-no-header () |
321 | (beginning-of-line) | |
322 | (if (or Buffer-menu-use-header-line | |
323 | (not (eq (char-after) ?C))) | |
324 | t | |
325 | (ding) | |
326 | (forward-line 1) | |
327 | nil)) | |
328 | ||
e5f9458f CY |
329 | (defun Buffer-menu-beginning () |
330 | (goto-char (point-min)) | |
331 | (unless Buffer-menu-use-header-line | |
332 | (forward-line))) | |
333 | ||
334 | \f | |
335 | ;;; Commands for modifying Buffer Menu entries. | |
336 | ||
2dd96f23 | 337 | (defun Buffer-menu-mark () |
e5f9458f CY |
338 | "Mark the Buffer menu entry at point for later display. |
339 | It will be displayed by the \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command." | |
2dd96f23 | 340 | (interactive) |
e5f9458f CY |
341 | (tabulated-list-set-col 0 ">" t) |
342 | (forward-line)) | |
2dd96f23 | 343 | |
86dbbfc2 ER |
344 | (defun Buffer-menu-unmark (&optional backup) |
345 | "Cancel all requested operations on buffer on this line and move down. | |
bfa13d19 | 346 | Optional prefix arg means move up." |
86dbbfc2 | 347 | (interactive "P") |
e5f9458f | 348 | (tabulated-list-set-col 0 " " t) |
86dbbfc2 | 349 | (forward-line (if backup -1 1))) |
2dd96f23 JB |
350 | |
351 | (defun Buffer-menu-backup-unmark () | |
352 | "Move up and cancel all requested operations on buffer on line above." | |
353 | (interactive) | |
354 | (forward-line -1) | |
e5f9458f | 355 | (tabulated-list-set-col 0 " " t)) |
2dd96f23 | 356 | |
77a43e01 | 357 | (defun Buffer-menu-delete (&optional arg) |
e5f9458f CY |
358 | "Mark the buffer on this Buffer Menu buffer line for deletion. |
359 | A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command | |
360 | will delete it. | |
361 | ||
362 | If prefix argument ARG is non-nil, it specifies the number of | |
363 | buffers to delete; a negative ARG means to delete backwards." | |
77a43e01 | 364 | (interactive "p") |
e5f9458f CY |
365 | (if (or (null arg) (= arg 0)) |
366 | (setq arg 1)) | |
367 | (while (> arg 0) | |
368 | (when (Buffer-menu-buffer) | |
369 | (tabulated-list-set-col 0 "D" t)) | |
370 | (forward-line 1) | |
371 | (setq arg (1- arg))) | |
372 | (while (< arg 0) | |
373 | (when (Buffer-menu-buffer) | |
374 | (tabulated-list-set-col 0 "D" t)) | |
375 | (forward-line -1) | |
376 | (setq arg (1+ arg)))) | |
77a43e01 RS |
377 | |
378 | (defun Buffer-menu-delete-backwards (&optional arg) | |
e5f9458f CY |
379 | "Mark the buffer on this Buffer Menu line for deletion, and move up. |
380 | Prefix ARG means move that many lines." | |
77a43e01 | 381 | (interactive "p") |
bc7bb432 | 382 | (Buffer-menu-delete (- (or arg 1)))) |
2dd96f23 JB |
383 | |
384 | (defun Buffer-menu-save () | |
e5f9458f CY |
385 | "Mark the buffer on this Buffer Menu line for saving. |
386 | A subsequent \\<Buffer-menu-mode-map>`\\[Buffer-menu-execute]' command | |
387 | will save it." | |
2dd96f23 | 388 | (interactive) |
e5f9458f CY |
389 | (when (Buffer-menu-buffer) |
390 | (tabulated-list-set-col 2 "S" t) | |
391 | (forward-line 1))) | |
2dd96f23 | 392 | |
a8ce00dc | 393 | (defun Buffer-menu-not-modified (&optional arg) |
e5f9458f CY |
394 | "Mark the buffer on this line as unmodified (no changes to save). |
395 | If ARG is non-nil (interactively, with a prefix argument), mark | |
396 | it as modified." | |
a8ce00dc | 397 | (interactive "P") |
70ef8857 | 398 | (with-current-buffer (Buffer-menu-buffer t) |
a8ce00dc | 399 | (set-buffer-modified-p arg)) |
e5f9458f | 400 | (tabulated-list-set-col 2 (if arg "*" " ") t)) |
bfb2dda4 | 401 | |
2dd96f23 | 402 | (defun Buffer-menu-execute () |
e5f9458f CY |
403 | "Save and/or delete marked buffers in the Buffer Menu. |
404 | Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-save]' are saved. | |
405 | Buffers marked with \\<Buffer-menu-mode-map>`\\[Buffer-menu-delete]' are deleted." | |
2dd96f23 JB |
406 | (interactive) |
407 | (save-excursion | |
bfb2dda4 | 408 | (Buffer-menu-beginning) |
e5f9458f CY |
409 | (while (not (eobp)) |
410 | (let ((buffer (tabulated-list-get-id)) | |
411 | (entry (tabulated-list-get-entry))) | |
412 | (cond ((null entry) | |
413 | (forward-line 1)) | |
414 | ((not (buffer-live-p buffer)) | |
415 | (tabulated-list-delete-entry)) | |
416 | (t | |
417 | (let ((delete (eq (char-after) ?D))) | |
418 | (when (equal (aref entry 2) "S") | |
419 | (condition-case nil | |
420 | (progn | |
421 | (with-current-buffer buffer | |
422 | (save-buffer)) | |
423 | (tabulated-list-set-col 2 " " t)) | |
424 | (error (warn "Error saving %s" buffer)))) | |
425 | (if delete | |
426 | (unless (eq buffer (current-buffer)) | |
427 | (kill-buffer buffer) | |
428 | (tabulated-list-delete-entry)) | |
429 | (forward-line 1))))))))) | |
2dd96f23 JB |
430 | |
431 | (defun Buffer-menu-select () | |
e5f9458f CY |
432 | "Select this line's buffer; also, display buffers marked with `>'. |
433 | You can mark buffers with the \\<Buffer-menu-mode-map>`\\[Buffer-menu-mark]' command. | |
08c82d4d RS |
434 | This command deletes and replaces all the previously existing windows |
435 | in the selected frame." | |
2dd96f23 | 436 | (interactive) |
e5f9458f CY |
437 | (let* ((this-buffer (Buffer-menu-buffer t)) |
438 | (menu-buffer (current-buffer)) | |
439 | (others (delq this-buffer (Buffer-menu-marked-buffers t))) | |
440 | (height (/ (1- (frame-height)) (1+ (length others))))) | |
2dd96f23 | 441 | (delete-other-windows) |
e5f9458f CY |
442 | (switch-to-buffer this-buffer) |
443 | (unless (eq menu-buffer this-buffer) | |
444 | (bury-buffer menu-buffer)) | |
445 | (dolist (buffer others) | |
446 | (split-window nil height) | |
447 | (other-window 1) | |
448 | (switch-to-buffer buffer)) | |
449 | ;; Back to the beginning! | |
450 | (other-window 1))) | |
451 | ||
452 | (defun Buffer-menu-marked-buffers (&optional unmark) | |
453 | "Return the list of buffers marked with `Buffer-menu-mark'. | |
454 | If UNMARK is non-nil, unmark them." | |
9cda680d JL |
455 | (let (buffers) |
456 | (Buffer-menu-beginning) | |
457 | (while (re-search-forward "^>" nil t) | |
e5f9458f CY |
458 | (let ((buffer (Buffer-menu-buffer))) |
459 | (if (and buffer unmark) | |
460 | (tabulated-list-set-col 0 " " t)) | |
461 | (if (buffer-live-p buffer) | |
462 | (push buffer buffers)))) | |
9cda680d JL |
463 | (nreverse buffers))) |
464 | ||
465 | (defun Buffer-menu-isearch-buffers () | |
466 | "Search for a string through all marked buffers using Isearch." | |
467 | (interactive) | |
468 | (multi-isearch-buffers (Buffer-menu-marked-buffers))) | |
469 | ||
470 | (defun Buffer-menu-isearch-buffers-regexp () | |
471 | "Search for a regexp through all marked buffers using Isearch." | |
472 | (interactive) | |
473 | (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers))) | |
08c82d4d | 474 | |
2dd96f23 JB |
475 | \f |
476 | (defun Buffer-menu-visit-tags-table () | |
477 | "Visit the tags table in the buffer on this line. See `visit-tags-table'." | |
478 | (interactive) | |
479 | (let ((file (buffer-file-name (Buffer-menu-buffer t)))) | |
480 | (if file | |
481 | (visit-tags-table file) | |
482 | (error "Specified buffer has no file")))) | |
483 | ||
484 | (defun Buffer-menu-1-window () | |
dc6d9681 | 485 | "Select this line's buffer, alone, in full frame." |
2dd96f23 JB |
486 | (interactive) |
487 | (switch-to-buffer (Buffer-menu-buffer t)) | |
488 | (bury-buffer (other-buffer)) | |
489 | (delete-other-windows)) | |
490 | ||
491 | (defun Buffer-menu-this-window () | |
492 | "Select this line's buffer in this window." | |
493 | (interactive) | |
494 | (switch-to-buffer (Buffer-menu-buffer t))) | |
495 | ||
496 | (defun Buffer-menu-other-window () | |
497 | "Select this line's buffer in other window, leaving buffer menu visible." | |
498 | (interactive) | |
499 | (switch-to-buffer-other-window (Buffer-menu-buffer t))) | |
500 | ||
35aaf00c RM |
501 | (defun Buffer-menu-switch-other-window () |
502 | "Make the other window select this line's buffer. | |
503 | The current window remains selected." | |
504 | (interactive) | |
3199b96f | 505 | (display-buffer (Buffer-menu-buffer t) t)) |
35aaf00c | 506 | |
2dd96f23 JB |
507 | (defun Buffer-menu-2-window () |
508 | "Select this line's buffer, with previous buffer in second window." | |
509 | (interactive) | |
510 | (let ((buff (Buffer-menu-buffer t)) | |
3199b96f | 511 | (menu (current-buffer))) |
86dbbfc2 | 512 | (delete-other-windows) |
2dd96f23 | 513 | (switch-to-buffer (other-buffer)) |
3199b96f | 514 | (switch-to-buffer-other-window buff) |
2dd96f23 | 515 | (bury-buffer menu))) |
c0274f38 | 516 | |
d91e2b1a | 517 | (defun Buffer-menu-toggle-read-only () |
e5f9458f | 518 | "Toggle read-only status of buffer on this line." |
d91e2b1a | 519 | (interactive) |
e5f9458f | 520 | (let (read-only) |
70ef8857 | 521 | (with-current-buffer (Buffer-menu-buffer t) |
e5f9458f CY |
522 | (with-no-warnings (toggle-read-only)) |
523 | (setq read-only buffer-read-only)) | |
524 | (tabulated-list-set-col 1 (if read-only "%" " ") t))) | |
d91e2b1a | 525 | |
36570c92 RS |
526 | (defun Buffer-menu-bury () |
527 | "Bury the buffer listed on this line." | |
528 | (interactive) | |
e5f9458f CY |
529 | (let ((buffer (tabulated-list-get-id))) |
530 | (cond ((null buffer)) | |
531 | ((buffer-live-p buffer) | |
532 | (bury-buffer buffer) | |
533 | (save-excursion | |
534 | (let ((elt (tabulated-list-delete-entry))) | |
535 | (goto-char (point-max)) | |
536 | (apply 'tabulated-list-print-entry elt))) | |
537 | (message "Buffer buried.")) | |
538 | (t | |
539 | (tabulated-list-delete-entry) | |
540 | (message "Buffer is dead; removing from list."))))) | |
42a19c2a RS |
541 | |
542 | (defun Buffer-menu-view () | |
543 | "View this line's buffer in View mode." | |
544 | (interactive) | |
545 | (view-buffer (Buffer-menu-buffer t))) | |
546 | ||
42a19c2a RS |
547 | (defun Buffer-menu-view-other-window () |
548 | "View this line's buffer in View mode in another window." | |
549 | (interactive) | |
550 | (view-buffer-other-window (Buffer-menu-buffer t))) | |
0b03ce3a | 551 | \f |
e5f9458f | 552 | ;;; Functions for populating the Buffer Menu. |
d5aacb46 | 553 | |
f0809a9d | 554 | ;;;###autoload |
61617913 | 555 | (defun list-buffers-noselect (&optional files-only buffer-list) |
e5f9458f CY |
556 | "Create and return a Buffer Menu buffer. |
557 | This is called by `buffer-menu' and others as a subroutine. | |
558 | ||
559 | If FILES-ONLY is non-nil, show only file-visiting buffers. | |
560 | If BUFFER-LIST is non-nil, it should be a list of buffers; it | |
561 | means list those buffers and no others." | |
562 | (let ((old-buffer (current-buffer)) | |
563 | (buffer (get-buffer-create "*Buffer List*"))) | |
564 | (with-current-buffer buffer | |
0b03ce3a | 565 | (Buffer-menu-mode) |
e5f9458f CY |
566 | (setq Buffer-menu-files-only (and files-only (>= files-only 0))) |
567 | (list-buffers--refresh buffer-list old-buffer) | |
568 | (tabulated-list-print)) | |
569 | buffer)) | |
570 | ||
0d909786 CY |
571 | (defun Buffer-menu-mouse-select (event) |
572 | "Select the buffer whose line you click on." | |
573 | (interactive "e") | |
574 | (select-window (posn-window (event-end event))) | |
575 | (let ((buffer (tabulated-list-get-id (posn-point (event-end event))))) | |
576 | (when (buffer-live-p buffer) | |
577 | (if (and (window-dedicated-p (selected-window)) | |
578 | (eq (selected-window) (frame-root-window))) | |
579 | (switch-to-buffer-other-frame buffer) | |
580 | (switch-to-buffer buffer))))) | |
581 | ||
e5f9458f CY |
582 | (defun list-buffers--refresh (&optional buffer-list old-buffer) |
583 | ;; Set up `tabulated-list-format'. | |
584 | (let ((name-width Buffer-menu-name-width) | |
585 | (size-width Buffer-menu-size-width)) | |
586 | ;; Handle obsolete variable: | |
587 | (if Buffer-menu-buffer+size-width | |
588 | (setq name-width (- Buffer-menu-buffer+size-width size-width))) | |
589 | (setq tabulated-list-format | |
590 | (vector '("C" 1 t :pad-right 0) | |
591 | '("R" 1 t :pad-right 0) | |
592 | '("M" 1 t) | |
593 | `("Buffer" ,name-width t) | |
f0809a9d SM |
594 | `("Size" ,size-width tabulated-list-entry-size-> |
595 | :right-align t) | |
e5f9458f CY |
596 | `("Mode" ,Buffer-menu-mode-width t) |
597 | '("File" 1 t)))) | |
1241b724 | 598 | (setq tabulated-list-use-header-line Buffer-menu-use-header-line) |
e5f9458f CY |
599 | ;; Collect info for each buffer we're interested in. |
600 | (let ((buffer-menu-buffer (current-buffer)) | |
601 | (show-non-file (not Buffer-menu-files-only)) | |
602 | entries) | |
603 | (dolist (buffer (or buffer-list | |
604 | (buffer-list (if Buffer-menu-use-frame-buffer-list | |
605 | (selected-frame))))) | |
606 | (with-current-buffer buffer | |
607 | (let* ((name (buffer-name)) | |
608 | (file buffer-file-name)) | |
609 | (when (and (buffer-live-p buffer) | |
610 | (or buffer-list | |
611 | (and (not (string= (substring name 0 1) " ")) | |
612 | (not (eq buffer buffer-menu-buffer)) | |
613 | (or file show-non-file)))) | |
614 | (push (list buffer | |
615 | (vector (if (eq buffer old-buffer) "." " ") | |
616 | (if buffer-read-only "%" " ") | |
617 | (if (buffer-modified-p) "*" " ") | |
618 | (Buffer-menu--pretty-name name) | |
619 | (number-to-string (buffer-size)) | |
620 | (concat (format-mode-line mode-name nil nil buffer) | |
621 | (if mode-line-process | |
622 | (format-mode-line mode-line-process | |
623 | nil nil buffer))) | |
624 | (Buffer-menu--pretty-file-name file))) | |
625 | entries))))) | |
626 | (setq tabulated-list-entries (nreverse entries))) | |
627 | (tabulated-list-init-header)) | |
628 | ||
629 | (defun tabulated-list-entry-size-> (entry1 entry2) | |
630 | (> (string-to-number (aref (cadr entry1) 4)) | |
631 | (string-to-number (aref (cadr entry2) 4)))) | |
632 | ||
633 | (defun Buffer-menu--pretty-name (name) | |
0d909786 CY |
634 | (propertize name |
635 | 'font-lock-face 'buffer-menu-buffer | |
636 | 'mouse-face 'highlight)) | |
e5f9458f CY |
637 | |
638 | (defun Buffer-menu--pretty-file-name (file) | |
639 | (cond (file | |
640 | (abbreviate-file-name file)) | |
641 | ((and (boundp 'list-buffers-directory) | |
642 | list-buffers-directory) | |
643 | list-buffers-directory) | |
644 | ((eq major-mode 'Info-mode) | |
645 | (Buffer-menu-info-node-description Info-current-file)) | |
646 | (t ""))) | |
647 | ||
648 | (defun Buffer-menu-info-node-description (file) | |
649 | (cond | |
650 | ((equal file "dir") "*Info Directory*") | |
651 | ((eq file 'apropos) "*Info Apropos*") | |
652 | ((eq file 'history) "*Info History*") | |
653 | ((eq file 'toc) "*Info TOC*") | |
654 | ((not (stringp file)) "") ; Avoid errors | |
655 | (t | |
656 | (concat "(" (file-name-nondirectory file) ") " Info-current-node)))) | |
0b03ce3a | 657 | |
c0274f38 | 658 | ;;; buff-menu.el ends here |