Commit | Line | Data |
---|---|---|
5e684428 | 1 | ;;; buff-menu.el --- buffer menu main function and support functions -*- coding:utf-8 -*- |
c0274f38 | 2 | |
f4872033 | 3 | ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 2000, 2001, 2002, 2003, |
409cc4a3 | 4 | ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
2dd96f23 | 5 | |
9750e079 | 6 | ;; Maintainer: FSF |
6d6c3f84 | 7 | ;; Keywords: convenience |
9750e079 | 8 | |
2dd96f23 JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
2dd96f23 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
2dd96f23 JB |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
2dd96f23 | 23 | |
e41b2db1 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; Edit, delete, or change attributes of all currently active Emacs | |
eb8c3be9 | 27 | ;; buffers from a list summarizing their state. A good way to browse |
e41b2db1 | 28 | ;; any special or scratch buffers you have loaded, since you can't find |
6d6c3f84 | 29 | ;; them by filename. The single entry point is `list-buffers', |
86dbbfc2 ER |
30 | ;; normally bound to C-x C-b. |
31 | ||
32 | ;;; Change Log: | |
33 | ||
42a19c2a RS |
34 | ;; Buffer-menu-view: New function |
35 | ;; Buffer-menu-view-other-window: New function | |
36 | ||
86dbbfc2 ER |
37 | ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993 |
38 | ;; | |
86dbbfc2 ER |
39 | ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89 |
40 | ;; | |
41 | ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete | |
42 | ;; current entry and then move to previous one. | |
43 | ;; | |
44 | ;; Based on FSF code dating back to 1985. | |
e41b2db1 | 45 | |
5de9bdab | 46 | ;;; Code: |
10e1dad9 | 47 | |
d5aacb46 SM |
48 | ;;Trying to preserve the old window configuration works well in |
49 | ;;simple scenarios, when you enter the buffer menu, use it, and exit it. | |
50 | ;;But it does strange things when you switch back to the buffer list buffer | |
51 | ;;with C-x b, later on, when the window configuration is different. | |
52 | ;;The choice seems to be, either restore the window configuration | |
53 | ;;in all cases, or in no cases. | |
54 | ;;I decided it was better not to restore the window config at all. -- rms. | |
08c82d4d | 55 | |
d5aacb46 SM |
56 | ;;But since then, I changed buffer-menu to use the selected window, |
57 | ;;so q now once again goes back to the previous window configuration. | |
08c82d4d | 58 | |
d5aacb46 SM |
59 | ;;(defvar Buffer-menu-window-config nil |
60 | ;; "Window configuration saved from entry to `buffer-menu'.") | |
2dd96f23 | 61 | |
d5aacb46 SM |
62 | ;; Put buffer *Buffer List* into proper mode right away |
63 | ;; so that from now on even list-buffers is enough to get a buffer menu. | |
2dd96f23 | 64 | |
bc7bb432 JB |
65 | (defgroup Buffer-menu nil |
66 | "Show a menu of all buffers in a buffer." | |
67 | :group 'tools | |
68 | :group 'convenience) | |
69 | ||
70 | (defcustom Buffer-menu-use-header-line t | |
71 | "*Non-nil means to use an immovable header-line." | |
72 | :type 'boolean | |
73 | :group 'Buffer-menu) | |
74 | ||
d247d0fe | 75 | (defface buffer-menu-buffer |
bc7bb432 | 76 | '((t (:weight bold))) |
d247d0fe | 77 | "Face used to highlight buffer names in the buffer menu." |
2a8f2d4c | 78 | :group 'Buffer-menu) |
d247d0fe | 79 | (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer) |
bc7bb432 | 80 | |
4f0992b3 | 81 | (defcustom Buffer-menu-buffer+size-width 26 |
bc7bb432 JB |
82 | "*How wide to jointly make the buffer name and size columns." |
83 | :type 'number | |
84 | :group 'Buffer-menu) | |
85 | ||
4f0992b3 | 86 | (defcustom Buffer-menu-mode-width 16 |
bc7bb432 JB |
87 | "*How wide to make the mode name column." |
88 | :type 'number | |
89 | :group 'Buffer-menu) | |
90 | ||
3ce5f932 LT |
91 | (defcustom Buffer-menu-use-frame-buffer-list t |
92 | "If non-nil, the Buffer Menu uses the selected frame's buffer list. | |
93 | Buffers that were never selected in that frame are listed at the end. | |
94 | If the value is nil, the Buffer Menu uses the global buffer list. | |
95 | This variable matters if the Buffer Menu is sorted by visited order, | |
96 | as it is by default." | |
97 | :type 'boolean | |
98 | :group 'Buffer-menu | |
99 | :version "22.1") | |
100 | ||
d5aacb46 | 101 | ;; This should get updated & resorted when you click on a column heading |
bc7bb432 | 102 | (defvar Buffer-menu-sort-column nil |
84bbd894 RS |
103 | "Which column to sort the menu on. |
104 | Use 2 to sort by buffer names, or 5 to sort by file names. | |
392225ee | 105 | A nil value means sort by visited order (the default).") |
bc7bb432 JB |
106 | |
107 | (defconst Buffer-menu-buffer-column 4) | |
e5ea316b | 108 | |
7b0a86ab LT |
109 | (defvar Buffer-menu-files-only nil |
110 | "Non-nil if the current buffer-menu lists only file buffers. | |
111 | This variable determines whether reverting the buffer lists only | |
112 | file buffers. It affects both manual reverting and reverting by | |
113 | Auto Revert Mode.") | |
114 | ||
392225ee JB |
115 | (make-variable-buffer-local 'Buffer-menu-files-only) |
116 | ||
fb5614e8 | 117 | (defvar Info-current-file) ;; from info.el |
b653cee4 | 118 | (defvar Info-current-node) ;; from info.el |
fb5614e8 | 119 | |
392225ee JB |
120 | (defvar Buffer-menu-mode-map |
121 | (let ((map (make-keymap))) | |
122 | (suppress-keymap map t) | |
123 | (define-key map "q" 'quit-window) | |
124 | (define-key map "v" 'Buffer-menu-select) | |
125 | (define-key map "2" 'Buffer-menu-2-window) | |
126 | (define-key map "1" 'Buffer-menu-1-window) | |
127 | (define-key map "f" 'Buffer-menu-this-window) | |
128 | (define-key map "e" 'Buffer-menu-this-window) | |
129 | (define-key map "\C-m" 'Buffer-menu-this-window) | |
130 | (define-key map "o" 'Buffer-menu-other-window) | |
131 | (define-key map "\C-o" 'Buffer-menu-switch-other-window) | |
132 | (define-key map "s" 'Buffer-menu-save) | |
133 | (define-key map "d" 'Buffer-menu-delete) | |
134 | (define-key map "k" 'Buffer-menu-delete) | |
135 | (define-key map "\C-d" 'Buffer-menu-delete-backwards) | |
136 | (define-key map "\C-k" 'Buffer-menu-delete) | |
137 | (define-key map "x" 'Buffer-menu-execute) | |
138 | (define-key map " " 'next-line) | |
139 | (define-key map "n" 'next-line) | |
140 | (define-key map "p" 'previous-line) | |
141 | (define-key map "\177" 'Buffer-menu-backup-unmark) | |
142 | (define-key map "~" 'Buffer-menu-not-modified) | |
143 | (define-key map "?" 'describe-mode) | |
144 | (define-key map "u" 'Buffer-menu-unmark) | |
145 | (define-key map "m" 'Buffer-menu-mark) | |
146 | (define-key map "t" 'Buffer-menu-visit-tags-table) | |
147 | (define-key map "%" 'Buffer-menu-toggle-read-only) | |
148 | (define-key map "b" 'Buffer-menu-bury) | |
149 | (define-key map "g" 'Buffer-menu-revert) | |
150 | (define-key map "V" 'Buffer-menu-view) | |
151 | (define-key map "T" 'Buffer-menu-toggle-files-only) | |
152 | (define-key map [mouse-2] 'Buffer-menu-mouse-select) | |
153 | (define-key map [follow-link] 'mouse-face) | |
9cda680d JL |
154 | (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers) |
155 | (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp) | |
392225ee JB |
156 | map) |
157 | "Local keymap for `Buffer-menu-mode' buffers.") | |
2dd96f23 JB |
158 | |
159 | ;; Buffer Menu mode is suitable only for specially formatted data. | |
160 | (put 'Buffer-menu-mode 'mode-class 'special) | |
161 | ||
392225ee | 162 | (define-derived-mode Buffer-menu-mode nil "Buffer Menu" |
2dd96f23 JB |
163 | "Major mode for editing a list of buffers. |
164 | Each line describes one of the buffers in Emacs. | |
165 | Letters do not insert themselves; instead, they are commands. | |
166 | \\<Buffer-menu-mode-map> | |
2cf5fc7e RS |
167 | \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu. |
168 | \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu. | |
169 | \\[Buffer-menu-other-window] -- select that buffer in another window, | |
170 | so the buffer menu buffer remains visible in its window. | |
42a19c2a RS |
171 | \\[Buffer-menu-view] -- select current line's buffer, but in view-mode. |
172 | \\[Buffer-menu-view-other-window] -- select that buffer in | |
173 | another window, in view-mode. | |
2cf5fc7e RS |
174 | \\[Buffer-menu-switch-other-window] -- make another window display that buffer. |
175 | \\[Buffer-menu-mark] -- mark current line's buffer to be displayed. | |
176 | \\[Buffer-menu-select] -- select current line's buffer. | |
177 | Also show buffers marked with m, in other windows. | |
dc6d9681 | 178 | \\[Buffer-menu-1-window] -- select that buffer in full-frame window. |
2dd96f23 JB |
179 | \\[Buffer-menu-2-window] -- select that buffer in one window, |
180 | together with buffer selected before this one in another window. | |
1fbb8c2e JL |
181 | \\[Buffer-menu-isearch-buffers] -- Do incremental search in the marked buffers. |
182 | \\[Buffer-menu-isearch-buffers-regexp] -- Isearch for regexp in the marked buffers. | |
2dd96f23 JB |
183 | \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer. |
184 | \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer. | |
185 | \\[Buffer-menu-save] -- mark that buffer to be saved, and move down. | |
186 | \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down. | |
187 | \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up. | |
188 | \\[Buffer-menu-execute] -- delete or save marked buffers. | |
189 | \\[Buffer-menu-unmark] -- remove all kinds of marks from current line. | |
86dbbfc2 | 190 | With prefix argument, also move up one line. |
d91e2b1a | 191 | \\[Buffer-menu-backup-unmark] -- back up a line and remove marks. |
0f88624e | 192 | \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line. |
a59faabe | 193 | \\[Buffer-menu-revert] -- update the list of buffers. |
7b0a86ab | 194 | \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers. |
a59faabe | 195 | \\[Buffer-menu-bury] -- bury the buffer listed on this line." |
7b0a86ab LT |
196 | (set (make-local-variable 'revert-buffer-function) |
197 | 'Buffer-menu-revert-function) | |
198 | (set (make-local-variable 'buffer-stale-function) | |
012ad4a3 | 199 | #'(lambda (&optional noconfirm) 'fast)) |
823316aa | 200 | (setq truncate-lines t) |
392225ee | 201 | (setq buffer-read-only t)) |
710e5da8 | 202 | |
5f11b513 RS |
203 | ;; This function exists so we can make the doc string of Buffer-menu-mode |
204 | ;; look nice. | |
0f88624e RS |
205 | (defun Buffer-menu-revert () |
206 | "Update the list of buffers." | |
207 | (interactive) | |
208 | (revert-buffer)) | |
209 | ||
710e5da8 | 210 | (defun Buffer-menu-revert-function (ignore1 ignore2) |
fda73b45 LT |
211 | (or (eq buffer-undo-list t) |
212 | (setq buffer-undo-list nil)) | |
7b0a86ab | 213 | ;; We can not use save-excursion here. The buffer gets erased. |
3f9c9ff7 LT |
214 | (let ((opoint (point)) |
215 | (eobp (eobp)) | |
216 | (ocol (current-column)) | |
bfb2dda4 DP |
217 | (oline (progn (move-to-column 4) |
218 | (get-text-property (point) 'buffer))) | |
fda73b45 LT |
219 | (prop (point-min)) |
220 | ;; do not make undo records for the reversion. | |
221 | (buffer-undo-list t)) | |
63de2160 LT |
222 | ;; We can be called by Auto Revert Mode with the "*Buffer Menu*" |
223 | ;; temporarily the current buffer. Make sure that the | |
224 | ;; interactively current buffer is correctly identified with a `.' | |
225 | ;; by `list-buffers-noselect'. | |
226 | (with-current-buffer (window-buffer) | |
227 | (list-buffers-noselect Buffer-menu-files-only)) | |
3f9c9ff7 LT |
228 | (if oline |
229 | (while (setq prop (next-single-property-change prop 'buffer)) | |
230 | (when (eq (get-text-property prop 'buffer) oline) | |
231 | (goto-char prop) | |
232 | (move-to-column ocol))) | |
233 | (goto-char (if eobp (point-max) opoint))))) | |
7b0a86ab LT |
234 | |
235 | (defun Buffer-menu-toggle-files-only (arg) | |
236 | "Toggle whether the current buffer-menu displays only file buffers. | |
237 | With a positive ARG display only file buffers. With zero or | |
238 | negative ARG, display other buffers as well." | |
239 | (interactive "P") | |
240 | (setq Buffer-menu-files-only | |
241 | (cond ((not arg) (not Buffer-menu-files-only)) | |
242 | ((> (prefix-numeric-value arg) 0) t))) | |
243 | (revert-buffer)) | |
244 | ||
2dd96f23 | 245 | \f |
2dd96f23 JB |
246 | (defun Buffer-menu-buffer (error-if-non-existent-p) |
247 | "Return buffer described by this line of buffer menu." | |
1b40dbed RS |
248 | (let* ((where (save-excursion |
249 | (beginning-of-line) | |
250 | (+ (point) Buffer-menu-buffer-column))) | |
601ed8a0 GM |
251 | (name (and (not (eobp)) (get-text-property where 'buffer-name))) |
252 | (buf (and (not (eobp)) (get-text-property where 'buffer)))) | |
1b40dbed RS |
253 | (if name |
254 | (or (get-buffer name) | |
601ed8a0 | 255 | (and buf (buffer-name buf) buf) |
1b40dbed RS |
256 | (if error-if-non-existent-p |
257 | (error "No buffer named `%s'" name) | |
258 | nil)) | |
601ed8a0 | 259 | (or (and buf (buffer-name buf) buf) |
463a7342 JPW |
260 | (if error-if-non-existent-p |
261 | (error "No buffer on this line") | |
601ed8a0 | 262 | nil))))) |
2dd96f23 | 263 | \f |
06b1a5ef | 264 | (defun buffer-menu (&optional arg) |
2dd96f23 JB |
265 | "Make a menu of buffers so you can save, delete or select them. |
266 | With argument, show only buffers that are visiting files. | |
267 | Type ? after invocation to get help on commands available. | |
6a0d92d3 RS |
268 | Type q to remove the buffer menu from the display. |
269 | ||
270 | The first column shows `>' for a buffer you have | |
271 | marked to be displayed, `D' for one you have marked for | |
272 | deletion, and `.' for the current buffer. | |
273 | ||
bc7bb432 JB |
274 | The C column has a `.' for the buffer from which you came. |
275 | The R column has a `%' if the buffer is read-only. | |
6a0d92d3 RS |
276 | The M column has a `*' if it is modified, |
277 | or `S' if you have marked it for saving. | |
6a0d92d3 RS |
278 | After this come the buffer name, its size in characters, |
279 | its major mode, and the visited file name (if any)." | |
08c82d4d RS |
280 | (interactive "P") |
281 | ;;; (setq Buffer-menu-window-config (current-window-configuration)) | |
282 | (switch-to-buffer (list-buffers-noselect arg)) | |
283 | (message | |
284 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) | |
285 | ||
286 | (defun buffer-menu-other-window (&optional arg) | |
287 | "Display a list of buffers in another window. | |
288 | With the buffer list buffer, you can save, delete or select the buffers. | |
289 | With argument, show only buffers that are visiting files. | |
290 | Type ? after invocation to get help on commands available. | |
6a0d92d3 RS |
291 | Type q to remove the buffer menu from the display. |
292 | For more information, see the function `buffer-menu'." | |
2dd96f23 | 293 | (interactive "P") |
3ed788ec | 294 | ;;; (setq Buffer-menu-window-config (current-window-configuration)) |
08c82d4d | 295 | (switch-to-buffer-other-window (list-buffers-noselect arg)) |
2dd96f23 | 296 | (message |
3ed788ec RS |
297 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) |
298 | ||
bc7bb432 JB |
299 | (defun Buffer-menu-no-header () |
300 | (beginning-of-line) | |
301 | (if (or Buffer-menu-use-header-line | |
302 | (not (eq (char-after) ?C))) | |
303 | t | |
304 | (ding) | |
305 | (forward-line 1) | |
306 | nil)) | |
307 | ||
2dd96f23 JB |
308 | (defun Buffer-menu-mark () |
309 | "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command." | |
310 | (interactive) | |
bc7bb432 | 311 | (when (Buffer-menu-no-header) |
70ef8857 | 312 | (let ((inhibit-read-only t)) |
2dd96f23 JB |
313 | (delete-char 1) |
314 | (insert ?>) | |
315 | (forward-line 1)))) | |
316 | ||
86dbbfc2 ER |
317 | (defun Buffer-menu-unmark (&optional backup) |
318 | "Cancel all requested operations on buffer on this line and move down. | |
bfa13d19 | 319 | Optional prefix arg means move up." |
86dbbfc2 | 320 | (interactive "P") |
bc7bb432 | 321 | (when (Buffer-menu-no-header) |
2dd96f23 JB |
322 | (let* ((buf (Buffer-menu-buffer t)) |
323 | (mod (buffer-modified-p buf)) | |
70ef8857 SM |
324 | (readonly (with-current-buffer buf buffer-read-only)) |
325 | (inhibit-read-only t)) | |
2dd96f23 | 326 | (delete-char 3) |
bc7bb432 | 327 | (insert (if readonly (if mod " %*" " % ") (if mod " *" " "))))) |
86dbbfc2 | 328 | (forward-line (if backup -1 1))) |
2dd96f23 JB |
329 | |
330 | (defun Buffer-menu-backup-unmark () | |
331 | "Move up and cancel all requested operations on buffer on line above." | |
332 | (interactive) | |
333 | (forward-line -1) | |
334 | (Buffer-menu-unmark) | |
335 | (forward-line -1)) | |
336 | ||
77a43e01 RS |
337 | (defun Buffer-menu-delete (&optional arg) |
338 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command. | |
339 | Prefix arg is how many buffers to delete. | |
340 | Negative arg means delete backwards." | |
341 | (interactive "p") | |
bc7bb432 | 342 | (when (Buffer-menu-no-header) |
70ef8857 | 343 | (let ((inhibit-read-only t)) |
77a43e01 RS |
344 | (if (or (null arg) (= arg 0)) |
345 | (setq arg 1)) | |
346 | (while (> arg 0) | |
347 | (delete-char 1) | |
348 | (insert ?D) | |
349 | (forward-line 1) | |
350 | (setq arg (1- arg))) | |
bc7bb432 JB |
351 | (while (and (< arg 0) |
352 | (Buffer-menu-no-header)) | |
77a43e01 RS |
353 | (delete-char 1) |
354 | (insert ?D) | |
355 | (forward-line -1) | |
356 | (setq arg (1+ arg)))))) | |
357 | ||
358 | (defun Buffer-menu-delete-backwards (&optional arg) | |
2dd96f23 | 359 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command |
77a43e01 RS |
360 | and then move up one line. Prefix arg means move that many lines." |
361 | (interactive "p") | |
bc7bb432 | 362 | (Buffer-menu-delete (- (or arg 1)))) |
2dd96f23 JB |
363 | |
364 | (defun Buffer-menu-save () | |
365 | "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command." | |
366 | (interactive) | |
bc7bb432 | 367 | (when (Buffer-menu-no-header) |
70ef8857 | 368 | (let ((inhibit-read-only t)) |
bc7bb432 | 369 | (forward-char 2) |
2dd96f23 JB |
370 | (delete-char 1) |
371 | (insert ?S) | |
372 | (forward-line 1)))) | |
373 | ||
a8ce00dc | 374 | (defun Buffer-menu-not-modified (&optional arg) |
2dd96f23 | 375 | "Mark buffer on this line as unmodified (no changes to save)." |
a8ce00dc | 376 | (interactive "P") |
70ef8857 | 377 | (with-current-buffer (Buffer-menu-buffer t) |
a8ce00dc | 378 | (set-buffer-modified-p arg)) |
2dd96f23 JB |
379 | (save-excursion |
380 | (beginning-of-line) | |
bc7bb432 | 381 | (forward-char 2) |
bfa13d19 | 382 | (if (= (char-after) (if arg ?\s ?*)) |
70ef8857 | 383 | (let ((inhibit-read-only t)) |
2dd96f23 | 384 | (delete-char 1) |
bfa13d19 | 385 | (insert (if arg ?* ?\s)))))) |
2dd96f23 | 386 | |
bfb2dda4 DP |
387 | (defun Buffer-menu-beginning () |
388 | (goto-char (point-min)) | |
389 | (unless Buffer-menu-use-header-line | |
390 | (forward-line))) | |
391 | ||
2dd96f23 JB |
392 | (defun Buffer-menu-execute () |
393 | "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands." | |
394 | (interactive) | |
395 | (save-excursion | |
bfb2dda4 | 396 | (Buffer-menu-beginning) |
bc7bb432 | 397 | (while (re-search-forward "^..S" nil t) |
2dd96f23 | 398 | (let ((modp nil)) |
70ef8857 | 399 | (with-current-buffer (Buffer-menu-buffer t) |
2dd96f23 JB |
400 | (save-buffer) |
401 | (setq modp (buffer-modified-p))) | |
70ef8857 | 402 | (let ((inhibit-read-only t)) |
2dd96f23 | 403 | (delete-char -1) |
bfa13d19 | 404 | (insert (if modp ?* ?\s)))))) |
2dd96f23 | 405 | (save-excursion |
bfb2dda4 | 406 | (Buffer-menu-beginning) |
2dd96f23 | 407 | (let ((buff-menu-buffer (current-buffer)) |
70ef8857 | 408 | (inhibit-read-only t)) |
4f1ab860 | 409 | (while (re-search-forward "^D" nil t) |
2dd96f23 JB |
410 | (forward-char -1) |
411 | (let ((buf (Buffer-menu-buffer nil))) | |
412 | (or (eq buf nil) | |
413 | (eq buf buff-menu-buffer) | |
601ed8a0 GM |
414 | (save-excursion (kill-buffer buf))) |
415 | (if (and buf (buffer-name buf)) | |
2dd96f23 | 416 | (progn (delete-char 1) |
bfa13d19 | 417 | (insert ?\s)) |
2dd96f23 | 418 | (delete-region (point) (progn (forward-line 1) (point))) |
4f1ab860 DL |
419 | (unless (bobp) |
420 | (forward-char -1)))))))) | |
2dd96f23 JB |
421 | |
422 | (defun Buffer-menu-select () | |
423 | "Select this line's buffer; also display buffers marked with `>'. | |
08c82d4d RS |
424 | You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command. |
425 | This command deletes and replaces all the previously existing windows | |
426 | in the selected frame." | |
2dd96f23 JB |
427 | (interactive) |
428 | (let ((buff (Buffer-menu-buffer t)) | |
10e1dad9 | 429 | (menu (current-buffer)) |
2dd96f23 JB |
430 | (others ()) |
431 | tem) | |
bfb2dda4 | 432 | (Buffer-menu-beginning) |
a9fb4690 | 433 | (while (re-search-forward "^>" nil t) |
2dd96f23 | 434 | (setq tem (Buffer-menu-buffer t)) |
70ef8857 | 435 | (let ((inhibit-read-only t)) |
2dd96f23 | 436 | (delete-char -1) |
bfa13d19 | 437 | (insert ?\s)) |
2dd96f23 JB |
438 | (or (eq tem buff) (memq tem others) (setq others (cons tem others)))) |
439 | (setq others (nreverse others) | |
dc6d9681 | 440 | tem (/ (1- (frame-height)) (1+ (length others)))) |
2dd96f23 JB |
441 | (delete-other-windows) |
442 | (switch-to-buffer buff) | |
443 | (or (eq menu buff) | |
444 | (bury-buffer menu)) | |
86dbbfc2 ER |
445 | (if (equal (length others) 0) |
446 | (progn | |
3ed788ec RS |
447 | ;;; ;; Restore previous window configuration before displaying |
448 | ;;; ;; selected buffers. | |
449 | ;;; (if Buffer-menu-window-config | |
450 | ;;; (progn | |
451 | ;;; (set-window-configuration Buffer-menu-window-config) | |
452 | ;;; (setq Buffer-menu-window-config nil))) | |
86dbbfc2 ER |
453 | (switch-to-buffer buff)) |
454 | (while others | |
455 | (split-window nil tem) | |
456 | (other-window 1) | |
457 | (switch-to-buffer (car others)) | |
458 | (setq others (cdr others))) | |
459 | (other-window 1) ;back to the beginning! | |
460 | ))) | |
461 | ||
9cda680d JL |
462 | (defun Buffer-menu-marked-buffers () |
463 | "Return a list of buffers marked with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command." | |
464 | (let (buffers) | |
465 | (Buffer-menu-beginning) | |
466 | (while (re-search-forward "^>" nil t) | |
467 | (setq buffers (cons (Buffer-menu-buffer t) buffers))) | |
468 | (nreverse buffers))) | |
469 | ||
470 | (defun Buffer-menu-isearch-buffers () | |
471 | "Search for a string through all marked buffers using Isearch." | |
472 | (interactive) | |
473 | (multi-isearch-buffers (Buffer-menu-marked-buffers))) | |
474 | ||
475 | (defun Buffer-menu-isearch-buffers-regexp () | |
476 | "Search for a regexp through all marked buffers using Isearch." | |
477 | (interactive) | |
478 | (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers))) | |
08c82d4d | 479 | |
2dd96f23 JB |
480 | \f |
481 | (defun Buffer-menu-visit-tags-table () | |
482 | "Visit the tags table in the buffer on this line. See `visit-tags-table'." | |
483 | (interactive) | |
484 | (let ((file (buffer-file-name (Buffer-menu-buffer t)))) | |
485 | (if file | |
486 | (visit-tags-table file) | |
487 | (error "Specified buffer has no file")))) | |
488 | ||
489 | (defun Buffer-menu-1-window () | |
dc6d9681 | 490 | "Select this line's buffer, alone, in full frame." |
2dd96f23 JB |
491 | (interactive) |
492 | (switch-to-buffer (Buffer-menu-buffer t)) | |
493 | (bury-buffer (other-buffer)) | |
494 | (delete-other-windows)) | |
495 | ||
2cf5fc7e RS |
496 | (defun Buffer-menu-mouse-select (event) |
497 | "Select the buffer whose line you click on." | |
498 | (interactive "e") | |
499 | (let (buffer) | |
70ef8857 | 500 | (with-current-buffer (window-buffer (posn-window (event-end event))) |
2cf5fc7e RS |
501 | (save-excursion |
502 | (goto-char (posn-point (event-end event))) | |
503 | (setq buffer (Buffer-menu-buffer t)))) | |
504 | (select-window (posn-window (event-end event))) | |
d953b88c RS |
505 | (if (and (window-dedicated-p (selected-window)) |
506 | (eq (selected-window) (frame-root-window))) | |
507 | (switch-to-buffer-other-frame buffer) | |
508 | (switch-to-buffer buffer)))) | |
2cf5fc7e | 509 | |
2dd96f23 JB |
510 | (defun Buffer-menu-this-window () |
511 | "Select this line's buffer in this window." | |
512 | (interactive) | |
513 | (switch-to-buffer (Buffer-menu-buffer t))) | |
514 | ||
515 | (defun Buffer-menu-other-window () | |
516 | "Select this line's buffer in other window, leaving buffer menu visible." | |
517 | (interactive) | |
518 | (switch-to-buffer-other-window (Buffer-menu-buffer t))) | |
519 | ||
35aaf00c RM |
520 | (defun Buffer-menu-switch-other-window () |
521 | "Make the other window select this line's buffer. | |
522 | The current window remains selected." | |
523 | (interactive) | |
a0e4e275 JL |
524 | (let ((pop-up-windows t) |
525 | same-window-buffer-names | |
526 | same-window-regexps) | |
527 | (display-buffer (Buffer-menu-buffer t)))) | |
35aaf00c | 528 | |
2dd96f23 JB |
529 | (defun Buffer-menu-2-window () |
530 | "Select this line's buffer, with previous buffer in second window." | |
531 | (interactive) | |
532 | (let ((buff (Buffer-menu-buffer t)) | |
533 | (menu (current-buffer)) | |
a0e4e275 JL |
534 | (pop-up-windows t) |
535 | same-window-buffer-names | |
536 | same-window-regexps) | |
86dbbfc2 | 537 | (delete-other-windows) |
2dd96f23 JB |
538 | (switch-to-buffer (other-buffer)) |
539 | (pop-to-buffer buff) | |
540 | (bury-buffer menu))) | |
c0274f38 | 541 | |
d91e2b1a | 542 | (defun Buffer-menu-toggle-read-only () |
f450965c | 543 | "Toggle read-only status of buffer on this line, perhaps via version control." |
d91e2b1a ER |
544 | (interactive) |
545 | (let (char) | |
70ef8857 SM |
546 | (with-current-buffer (Buffer-menu-buffer t) |
547 | (toggle-read-only) | |
bfa13d19 | 548 | (setq char (if buffer-read-only ?% ?\s))) |
d91e2b1a ER |
549 | (save-excursion |
550 | (beginning-of-line) | |
bc7bb432 | 551 | (forward-char 1) |
d91e2b1a | 552 | (if (/= (following-char) char) |
70ef8857 | 553 | (let ((inhibit-read-only t)) |
d91e2b1a ER |
554 | (delete-char 1) |
555 | (insert char)))))) | |
556 | ||
36570c92 RS |
557 | (defun Buffer-menu-bury () |
558 | "Bury the buffer listed on this line." | |
559 | (interactive) | |
bc7bb432 | 560 | (when (Buffer-menu-no-header) |
a59faabe RS |
561 | (save-excursion |
562 | (beginning-of-line) | |
563 | (bury-buffer (Buffer-menu-buffer t)) | |
564 | (let ((line (buffer-substring (point) (progn (forward-line 1) (point)))) | |
70ef8857 | 565 | (inhibit-read-only t)) |
a59faabe RS |
566 | (delete-region (point) (progn (forward-line -1) (point))) |
567 | (goto-char (point-max)) | |
568 | (insert line)) | |
569 | (message "Buried buffer moved to the end")))) | |
42a19c2a RS |
570 | |
571 | ||
572 | (defun Buffer-menu-view () | |
573 | "View this line's buffer in View mode." | |
574 | (interactive) | |
575 | (view-buffer (Buffer-menu-buffer t))) | |
576 | ||
577 | ||
578 | (defun Buffer-menu-view-other-window () | |
579 | "View this line's buffer in View mode in another window." | |
580 | (interactive) | |
581 | (view-buffer-other-window (Buffer-menu-buffer t))) | |
0b03ce3a RS |
582 | \f |
583 | ||
584 | (define-key ctl-x-map "\C-b" 'list-buffers) | |
585 | ||
586 | (defun list-buffers (&optional files-only) | |
587 | "Display a list of names of existing buffers. | |
588 | The list is displayed in a buffer named `*Buffer List*'. | |
589 | Note that buffers with names starting with spaces are omitted. | |
590 | Non-null optional arg FILES-ONLY means mention only file buffers. | |
591 | ||
6a0d92d3 | 592 | For more information, see the function `buffer-menu'." |
0b03ce3a | 593 | (interactive "P") |
08c82d4d RS |
594 | (display-buffer (list-buffers-noselect files-only))) |
595 | ||
bc7bb432 JB |
596 | (defun Buffer-menu-buffer+size (name size &optional name-props size-props) |
597 | (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width) | |
598 | (setq name | |
599 | (if (string-match "<[0-9]+>$" name) | |
600 | (concat (substring name 0 | |
601 | (- Buffer-menu-buffer+size-width | |
602 | (max (length size) 3) | |
603 | (match-end 0) | |
604 | (- (match-beginning 0)) | |
605 | 2)) | |
606 | ":" ; narrow ellipsis | |
607 | (match-string 0 name)) | |
608 | (concat (substring name 0 | |
609 | (- Buffer-menu-buffer+size-width | |
610 | (max (length size) 3) | |
611 | 2)) | |
ee5861c8 AS |
612 | ":"))) ; narrow ellipsis |
613 | ;; Don't put properties on (buffer-name). | |
614 | (setq name (copy-sequence name))) | |
bc7bb432 JB |
615 | (add-text-properties 0 (length name) name-props name) |
616 | (add-text-properties 0 (length size) size-props size) | |
617 | (concat name | |
618 | (make-string (- Buffer-menu-buffer+size-width | |
619 | (length name) | |
620 | (length size)) | |
bfa13d19 | 621 | ?\s) |
bc7bb432 JB |
622 | size)) |
623 | ||
d5aacb46 SM |
624 | (defun Buffer-menu-sort (column) |
625 | "Sort the buffer menu by COLUMN." | |
626 | (interactive "P") | |
627 | (when column | |
628 | (setq column (prefix-numeric-value column)) | |
629 | (if (< column 2) (setq column 2)) | |
630 | (if (> column 5) (setq column 5))) | |
631 | (setq Buffer-menu-sort-column column) | |
70ef8857 | 632 | (let ((inhibit-read-only t) l buf m1 m2) |
bfb2dda4 DP |
633 | (save-excursion |
634 | (Buffer-menu-beginning) | |
635 | (while (not (eobp)) | |
636 | (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer))) | |
637 | (setq m1 (char-after) | |
638 | m1 (if (memq m1 '(?> ?D)) m1) | |
639 | m2 (char-after (+ (point) 2)) | |
640 | m2 (if (eq m2 ?S) m2)) | |
641 | (if (or m1 m2) | |
642 | (push (list buf m1 m2) l))) | |
643 | (forward-line))) | |
644 | (Buffer-menu-revert) | |
bfb2dda4 DP |
645 | (save-excursion |
646 | (Buffer-menu-beginning) | |
647 | (while (not (eobp)) | |
648 | (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l)) | |
649 | (setq m1 (cadr buf) | |
650 | m2 (cadr (cdr buf))) | |
651 | (when m1 | |
652 | (delete-char 1) | |
653 | (insert m1) | |
654 | (backward-char 1)) | |
655 | (when m2 | |
656 | (forward-char 2) | |
657 | (delete-char 1) | |
658 | (insert m2))) | |
659 | (forward-line))))) | |
d5aacb46 | 660 | |
104fc809 CY |
661 | (defun Buffer-menu-sort-by-column (&optional e) |
662 | "Sort the buffer menu by the column clicked on." | |
663 | (interactive (list last-input-event)) | |
664 | (if e (mouse-select-window e)) | |
665 | (let* ((pos (event-start e)) | |
666 | (obj (posn-object pos)) | |
667 | (col (if obj | |
668 | (get-text-property (cdr obj) 'column (car obj)) | |
669 | (get-text-property (posn-point pos) 'column)))) | |
670 | (Buffer-menu-sort col))) | |
671 | ||
672 | (defvar Buffer-menu-sort-button-map | |
673 | (let ((map (make-sparse-keymap))) | |
674 | ;; This keymap handles both nil and non-nil values for | |
675 | ;; Buffer-menu-use-header-line. | |
676 | (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column) | |
677 | (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column) | |
678 | (define-key map [mouse-2] 'Buffer-menu-sort-by-column) | |
679 | (define-key map [follow-link] 'mouse-face) | |
680 | (define-key map "\C-m" 'Buffer-menu-sort-by-column) | |
681 | map) | |
682 | "Local keymap for Buffer menu sort buttons.") | |
683 | ||
d5aacb46 SM |
684 | (defun Buffer-menu-make-sort-button (name column) |
685 | (if (equal column Buffer-menu-sort-column) (setq column nil)) | |
104fc809 CY |
686 | (propertize name |
687 | 'column column | |
688 | 'help-echo (concat | |
689 | (if Buffer-menu-use-header-line | |
690 | "mouse-1, mouse-2: sort by " | |
691 | "mouse-2, RET: sort by ") | |
692 | (if column (downcase name) "visited order")) | |
693 | 'mouse-face 'highlight | |
694 | 'keymap Buffer-menu-sort-button-map)) | |
d5aacb46 | 695 | |
61617913 | 696 | (defun list-buffers-noselect (&optional files-only buffer-list) |
08c82d4d RS |
697 | "Create and return a buffer with a list of names of existing buffers. |
698 | The buffer is named `*Buffer List*'. | |
699 | Note that buffers with names starting with spaces are omitted. | |
700 | Non-null optional arg FILES-ONLY means mention only file buffers. | |
701 | ||
61617913 RS |
702 | If BUFFER-LIST is non-nil, it should be a list of buffers; |
703 | it means list those buffers and no others. | |
704 | ||
6a0d92d3 | 705 | For more information, see the function `buffer-menu'." |
bc7bb432 JB |
706 | (let* ((old-buffer (current-buffer)) |
707 | (standard-output standard-output) | |
bfa13d19 | 708 | (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s)) |
5e684428 | 709 | (header (concat "CRM " |
d5aacb46 SM |
710 | (Buffer-menu-buffer+size |
711 | (Buffer-menu-make-sort-button "Buffer" 2) | |
712 | (Buffer-menu-make-sort-button "Size" 3)) | |
713 | " " | |
714 | (Buffer-menu-make-sort-button "Mode" 4) mode-end | |
715 | (Buffer-menu-make-sort-button "File" 5) "\n")) | |
f4872033 | 716 | list desired-point) |
748a336b | 717 | (when Buffer-menu-use-header-line |
d5aacb46 | 718 | (let ((pos 0)) |
fff861a2 JB |
719 | ;; Turn whitespace chars in the header into stretch specs so |
720 | ;; they work regardless of the header-line face. | |
721 | (while (string-match "[ \t\n]+" header pos) | |
748a336b SM |
722 | (setq pos (match-end 0)) |
723 | (put-text-property (match-beginning 0) pos 'display | |
5e684428 | 724 | ;; Assume fixed-size chars in the buffer. |
6d3bce2b | 725 | (list 'space :align-to pos) |
5e684428 SM |
726 | header))) |
727 | ;; Try to better align the one-char headers. | |
728 | (put-text-property 0 3 'face 'fixed-pitch header) | |
729 | ;; Add a "dummy" leading space to align the beginning of the header | |
730 | ;; line with the beginning of the text (rather than with the left | |
c822571a | 731 | ;; scrollbar or the left fringe). --Stef |
5e684428 | 732 | (setq header (concat (propertize " " 'display '(space :align-to 0)) |
a6a2fd5e | 733 | header))) |
d5aacb46 | 734 | (with-current-buffer (get-buffer-create "*Buffer List*") |
08c82d4d RS |
735 | (setq buffer-read-only nil) |
736 | (erase-buffer) | |
737 | (setq standard-output (current-buffer)) | |
bc7bb432 | 738 | (unless Buffer-menu-use-header-line |
c822571a SM |
739 | ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII |
740 | ;; (i.e. U+002D, HYPHEN-MINUS). | |
741 | (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-))) | |
5e684428 SM |
742 | (insert header |
743 | (apply 'string | |
744 | (mapcar (lambda (c) | |
bfa13d19 | 745 | (if (memq c '(?\n ?\s)) c underline)) |
5e684428 | 746 | header))))) |
a6a2fd5e | 747 | ;; Collect info for every buffer we're interested in. |
3ce5f932 LT |
748 | (dolist (buffer (or buffer-list |
749 | (buffer-list | |
750 | (when Buffer-menu-use-frame-buffer-list | |
751 | (selected-frame))))) | |
a6a2fd5e JL |
752 | (with-current-buffer buffer |
753 | (let ((name (buffer-name)) | |
754 | (file buffer-file-name)) | |
755 | (unless (and (not buffer-list) | |
756 | (or | |
757 | ;; Don't mention internal buffers. | |
758 | (and (string= (substring name 0 1) " ") (null file)) | |
759 | ;; Maybe don't mention buffers without files. | |
760 | (and files-only (not file)) | |
761 | (string= name "*Buffer List*"))) | |
762 | ;; Otherwise output info. | |
763 | (let ((mode (concat (format-mode-line mode-name nil nil buffer) | |
764 | (if mode-line-process | |
765 | (format-mode-line mode-line-process | |
766 | nil nil buffer)))) | |
767 | (bits (string | |
bfa13d19 | 768 | (if (eq buffer old-buffer) ?. ?\s) |
a6a2fd5e JL |
769 | ;; Handle readonly status. The output buffer |
770 | ;; is special cased to appear readonly; it is | |
771 | ;; actually made so at a later date. | |
772 | (if (or (eq buffer standard-output) | |
773 | buffer-read-only) | |
bfa13d19 | 774 | ?% ?\s) |
a6a2fd5e | 775 | ;; Identify modified buffers. |
bfa13d19 | 776 | (if (buffer-modified-p) ?* ?\s) |
a6a2fd5e | 777 | ;; Space separator. |
bfa13d19 | 778 | ?\s))) |
a6a2fd5e JL |
779 | (unless file |
780 | ;; No visited file. Check local value of | |
fb5614e8 EZ |
781 | ;; list-buffers-directory and, for Info buffers, |
782 | ;; Info-current-file. | |
783 | (cond ((and (boundp 'list-buffers-directory) | |
784 | list-buffers-directory) | |
785 | (setq file list-buffers-directory)) | |
786 | ((eq major-mode 'Info-mode) | |
787 | (setq file Info-current-file) | |
788 | (cond | |
26f544d3 | 789 | ((equal file "dir") |
fb5614e8 EZ |
790 | (setq file "*Info Directory*")) |
791 | ((eq file 'apropos) | |
792 | (setq file "*Info Apropos*")) | |
793 | ((eq file 'history) | |
794 | (setq file "*Info History*")) | |
795 | ((eq file 'toc) | |
796 | (setq file "*Info TOC*")) | |
797 | ((not (stringp file)) ;; avoid errors | |
b653cee4 EZ |
798 | (setq file nil)) |
799 | (t | |
800 | (setq file (concat "(" | |
801 | (file-name-nondirectory file) | |
9cda680d | 802 | ") " |
b653cee4 | 803 | Info-current-node))))))) |
a6a2fd5e JL |
804 | (push (list buffer bits name (buffer-size) mode file) |
805 | list)))))) | |
806 | ;; Preserve the original buffer-list ordering, just in case. | |
807 | (setq list (nreverse list)) | |
f4872033 | 808 | ;; Place the buffers's info in the output buffer, sorted if necessary. |
bc7bb432 JB |
809 | (dolist (buffer |
810 | (if Buffer-menu-sort-column | |
811 | (sort list | |
812 | (if (eq Buffer-menu-sort-column 3) | |
813 | (lambda (a b) | |
814 | (< (nth Buffer-menu-sort-column a) | |
815 | (nth Buffer-menu-sort-column b))) | |
816 | (lambda (a b) | |
817 | (string< (nth Buffer-menu-sort-column a) | |
818 | (nth Buffer-menu-sort-column b))))) | |
819 | list)) | |
820 | (if (eq (car buffer) old-buffer) | |
821 | (setq desired-point (point))) | |
822 | (insert (cadr buffer) | |
823 | ;; Put the buffer name into a text property | |
824 | ;; so we don't have to extract it from the text. | |
825 | ;; This way we avoid problems with unusual buffer names. | |
392640c4 NR |
826 | (let ((name (nth 2 buffer)) |
827 | (size (int-to-string (nth 3 buffer)))) | |
828 | (Buffer-menu-buffer+size name size | |
829 | `(buffer-name ,name | |
830 | buffer ,(car buffer) | |
831 | font-lock-face buffer-menu-buffer | |
832 | mouse-face highlight | |
833 | help-echo | |
834 | ,(if (>= (length name) | |
835 | (- Buffer-menu-buffer+size-width | |
836 | (max (length size) 3) | |
837 | 2)) | |
838 | name | |
839 | "mouse-2: select this buffer")))) | |
840 | " " | |
bc7bb432 JB |
841 | (if (> (length (nth 4 buffer)) Buffer-menu-mode-width) |
842 | (substring (nth 4 buffer) 0 Buffer-menu-mode-width) | |
843 | (nth 4 buffer))) | |
844 | (when (nth 5 buffer) | |
845 | (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width | |
846 | Buffer-menu-mode-width 4) 1) | |
847 | (princ (abbreviate-file-name (nth 5 buffer)))) | |
848 | (princ "\n")) | |
0b03ce3a | 849 | (Buffer-menu-mode) |
bc7bb432 | 850 | (when Buffer-menu-use-header-line |
748a336b | 851 | (setq header-line-format header)) |
08c82d4d RS |
852 | ;; DESIRED-POINT doesn't have to be set; it is not when the |
853 | ;; current buffer is not displayed for some reason. | |
0b03ce3a | 854 | (and desired-point |
08c82d4d | 855 | (goto-char desired-point)) |
7b0a86ab LT |
856 | (setq Buffer-menu-files-only files-only) |
857 | (set-buffer-modified-p nil) | |
08c82d4d | 858 | (current-buffer)))) |
0b03ce3a | 859 | |
f4872033 | 860 | ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6 |
c0274f38 | 861 | ;;; buff-menu.el ends here |