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