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 | ||
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. | |
392225ee | 107 | A nil value means sort by visited order (the default).") |
bc7bb432 JB |
108 | |
109 | (defconst Buffer-menu-buffer-column 4) | |
e5ea316b | 110 | |
7b0a86ab LT |
111 | (defvar Buffer-menu-files-only nil |
112 | "Non-nil if the current buffer-menu lists only file buffers. | |
113 | This variable determines whether reverting the buffer lists only | |
114 | file buffers. It affects both manual reverting and reverting by | |
115 | Auto Revert Mode.") | |
116 | ||
392225ee JB |
117 | (make-variable-buffer-local 'Buffer-menu-files-only) |
118 | ||
fb5614e8 | 119 | (defvar Info-current-file) ;; from info.el |
b653cee4 | 120 | (defvar Info-current-node) ;; from info.el |
fb5614e8 | 121 | |
392225ee JB |
122 | (defvar Buffer-menu-mode-map |
123 | (let ((map (make-keymap))) | |
124 | (suppress-keymap map t) | |
125 | (define-key map "q" 'quit-window) | |
126 | (define-key map "v" 'Buffer-menu-select) | |
127 | (define-key map "2" 'Buffer-menu-2-window) | |
128 | (define-key map "1" 'Buffer-menu-1-window) | |
129 | (define-key map "f" 'Buffer-menu-this-window) | |
130 | (define-key map "e" 'Buffer-menu-this-window) | |
131 | (define-key map "\C-m" 'Buffer-menu-this-window) | |
132 | (define-key map "o" 'Buffer-menu-other-window) | |
133 | (define-key map "\C-o" 'Buffer-menu-switch-other-window) | |
134 | (define-key map "s" 'Buffer-menu-save) | |
135 | (define-key map "d" 'Buffer-menu-delete) | |
136 | (define-key map "k" 'Buffer-menu-delete) | |
137 | (define-key map "\C-d" 'Buffer-menu-delete-backwards) | |
138 | (define-key map "\C-k" 'Buffer-menu-delete) | |
139 | (define-key map "x" 'Buffer-menu-execute) | |
140 | (define-key map " " 'next-line) | |
141 | (define-key map "n" 'next-line) | |
142 | (define-key map "p" 'previous-line) | |
143 | (define-key map "\177" 'Buffer-menu-backup-unmark) | |
144 | (define-key map "~" 'Buffer-menu-not-modified) | |
145 | (define-key map "?" 'describe-mode) | |
146 | (define-key map "u" 'Buffer-menu-unmark) | |
147 | (define-key map "m" 'Buffer-menu-mark) | |
148 | (define-key map "t" 'Buffer-menu-visit-tags-table) | |
149 | (define-key map "%" 'Buffer-menu-toggle-read-only) | |
150 | (define-key map "b" 'Buffer-menu-bury) | |
151 | (define-key map "g" 'Buffer-menu-revert) | |
152 | (define-key map "V" 'Buffer-menu-view) | |
153 | (define-key map "T" 'Buffer-menu-toggle-files-only) | |
154 | (define-key map [mouse-2] 'Buffer-menu-mouse-select) | |
155 | (define-key map [follow-link] 'mouse-face) | |
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. | |
2dd96f23 JB |
181 | \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer. |
182 | \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer. | |
183 | \\[Buffer-menu-save] -- mark that buffer to be saved, and move down. | |
184 | \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down. | |
185 | \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up. | |
186 | \\[Buffer-menu-execute] -- delete or save marked buffers. | |
187 | \\[Buffer-menu-unmark] -- remove all kinds of marks from current line. | |
86dbbfc2 | 188 | With prefix argument, also move up one line. |
d91e2b1a | 189 | \\[Buffer-menu-backup-unmark] -- back up a line and remove marks. |
0f88624e | 190 | \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line. |
a59faabe | 191 | \\[Buffer-menu-revert] -- update the list of buffers. |
7b0a86ab | 192 | \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers. |
a59faabe | 193 | \\[Buffer-menu-bury] -- bury the buffer listed on this line." |
7b0a86ab LT |
194 | (set (make-local-variable 'revert-buffer-function) |
195 | 'Buffer-menu-revert-function) | |
196 | (set (make-local-variable 'buffer-stale-function) | |
012ad4a3 | 197 | #'(lambda (&optional noconfirm) 'fast)) |
823316aa | 198 | (setq truncate-lines t) |
392225ee | 199 | (setq buffer-read-only t)) |
710e5da8 | 200 | |
5f11b513 RS |
201 | ;; This function exists so we can make the doc string of Buffer-menu-mode |
202 | ;; look nice. | |
0f88624e RS |
203 | (defun Buffer-menu-revert () |
204 | "Update the list of buffers." | |
205 | (interactive) | |
206 | (revert-buffer)) | |
207 | ||
710e5da8 | 208 | (defun Buffer-menu-revert-function (ignore1 ignore2) |
fda73b45 LT |
209 | (or (eq buffer-undo-list t) |
210 | (setq buffer-undo-list nil)) | |
7b0a86ab | 211 | ;; We can not use save-excursion here. The buffer gets erased. |
3f9c9ff7 LT |
212 | (let ((opoint (point)) |
213 | (eobp (eobp)) | |
214 | (ocol (current-column)) | |
bfb2dda4 DP |
215 | (oline (progn (move-to-column 4) |
216 | (get-text-property (point) 'buffer))) | |
fda73b45 LT |
217 | (prop (point-min)) |
218 | ;; do not make undo records for the reversion. | |
219 | (buffer-undo-list t)) | |
63de2160 LT |
220 | ;; We can be called by Auto Revert Mode with the "*Buffer Menu*" |
221 | ;; temporarily the current buffer. Make sure that the | |
222 | ;; interactively current buffer is correctly identified with a `.' | |
223 | ;; by `list-buffers-noselect'. | |
224 | (with-current-buffer (window-buffer) | |
225 | (list-buffers-noselect Buffer-menu-files-only)) | |
3f9c9ff7 LT |
226 | (if oline |
227 | (while (setq prop (next-single-property-change prop 'buffer)) | |
228 | (when (eq (get-text-property prop 'buffer) oline) | |
229 | (goto-char prop) | |
230 | (move-to-column ocol))) | |
231 | (goto-char (if eobp (point-max) opoint))))) | |
7b0a86ab LT |
232 | |
233 | (defun Buffer-menu-toggle-files-only (arg) | |
234 | "Toggle whether the current buffer-menu displays only file buffers. | |
235 | With a positive ARG display only file buffers. With zero or | |
236 | negative ARG, display other buffers as well." | |
237 | (interactive "P") | |
238 | (setq Buffer-menu-files-only | |
239 | (cond ((not arg) (not Buffer-menu-files-only)) | |
240 | ((> (prefix-numeric-value arg) 0) t))) | |
241 | (revert-buffer)) | |
242 | ||
2dd96f23 | 243 | \f |
2dd96f23 JB |
244 | (defun Buffer-menu-buffer (error-if-non-existent-p) |
245 | "Return buffer described by this line of buffer menu." | |
1b40dbed RS |
246 | (let* ((where (save-excursion |
247 | (beginning-of-line) | |
248 | (+ (point) Buffer-menu-buffer-column))) | |
601ed8a0 GM |
249 | (name (and (not (eobp)) (get-text-property where 'buffer-name))) |
250 | (buf (and (not (eobp)) (get-text-property where 'buffer)))) | |
1b40dbed RS |
251 | (if name |
252 | (or (get-buffer name) | |
601ed8a0 | 253 | (and buf (buffer-name buf) buf) |
1b40dbed RS |
254 | (if error-if-non-existent-p |
255 | (error "No buffer named `%s'" name) | |
256 | nil)) | |
601ed8a0 | 257 | (or (and buf (buffer-name buf) buf) |
463a7342 JPW |
258 | (if error-if-non-existent-p |
259 | (error "No buffer on this line") | |
601ed8a0 | 260 | nil))))) |
2dd96f23 | 261 | \f |
06b1a5ef | 262 | (defun buffer-menu (&optional arg) |
2dd96f23 JB |
263 | "Make a menu of buffers so you can save, delete or select them. |
264 | With argument, show only buffers that are visiting files. | |
265 | Type ? after invocation to get help on commands available. | |
6a0d92d3 RS |
266 | Type q to remove the buffer menu from the display. |
267 | ||
268 | The first column shows `>' for a buffer you have | |
269 | marked to be displayed, `D' for one you have marked for | |
270 | deletion, and `.' for the current buffer. | |
271 | ||
bc7bb432 JB |
272 | The C column has a `.' for the buffer from which you came. |
273 | The R column has a `%' if the buffer is read-only. | |
6a0d92d3 RS |
274 | The M column has a `*' if it is modified, |
275 | or `S' if you have marked it for saving. | |
6a0d92d3 RS |
276 | After this come the buffer name, its size in characters, |
277 | its major mode, and the visited file name (if any)." | |
08c82d4d RS |
278 | (interactive "P") |
279 | ;;; (setq Buffer-menu-window-config (current-window-configuration)) | |
280 | (switch-to-buffer (list-buffers-noselect arg)) | |
281 | (message | |
282 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) | |
283 | ||
284 | (defun buffer-menu-other-window (&optional arg) | |
285 | "Display a list of buffers in another window. | |
286 | With the buffer list buffer, you can save, delete or select the buffers. | |
287 | With argument, show only buffers that are visiting files. | |
288 | Type ? after invocation to get help on commands available. | |
6a0d92d3 RS |
289 | Type q to remove the buffer menu from the display. |
290 | For more information, see the function `buffer-menu'." | |
2dd96f23 | 291 | (interactive "P") |
3ed788ec | 292 | ;;; (setq Buffer-menu-window-config (current-window-configuration)) |
08c82d4d | 293 | (switch-to-buffer-other-window (list-buffers-noselect arg)) |
2dd96f23 | 294 | (message |
3ed788ec RS |
295 | "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help.")) |
296 | ||
bc7bb432 JB |
297 | (defun Buffer-menu-no-header () |
298 | (beginning-of-line) | |
299 | (if (or Buffer-menu-use-header-line | |
300 | (not (eq (char-after) ?C))) | |
301 | t | |
302 | (ding) | |
303 | (forward-line 1) | |
304 | nil)) | |
305 | ||
2dd96f23 JB |
306 | (defun Buffer-menu-mark () |
307 | "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command." | |
308 | (interactive) | |
bc7bb432 | 309 | (when (Buffer-menu-no-header) |
2dd96f23 JB |
310 | (let ((buffer-read-only nil)) |
311 | (delete-char 1) | |
312 | (insert ?>) | |
313 | (forward-line 1)))) | |
314 | ||
86dbbfc2 ER |
315 | (defun Buffer-menu-unmark (&optional backup) |
316 | "Cancel all requested operations on buffer on this line and move down. | |
bfa13d19 | 317 | Optional prefix arg means move up." |
86dbbfc2 | 318 | (interactive "P") |
bc7bb432 | 319 | (when (Buffer-menu-no-header) |
2dd96f23 JB |
320 | (let* ((buf (Buffer-menu-buffer t)) |
321 | (mod (buffer-modified-p buf)) | |
322 | (readonly (save-excursion (set-buffer buf) buffer-read-only)) | |
323 | (buffer-read-only nil)) | |
324 | (delete-char 3) | |
bc7bb432 | 325 | (insert (if readonly (if mod " %*" " % ") (if mod " *" " "))))) |
86dbbfc2 | 326 | (forward-line (if backup -1 1))) |
2dd96f23 JB |
327 | |
328 | (defun Buffer-menu-backup-unmark () | |
329 | "Move up and cancel all requested operations on buffer on line above." | |
330 | (interactive) | |
331 | (forward-line -1) | |
332 | (Buffer-menu-unmark) | |
333 | (forward-line -1)) | |
334 | ||
77a43e01 RS |
335 | (defun Buffer-menu-delete (&optional arg) |
336 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command. | |
337 | Prefix arg is how many buffers to delete. | |
338 | Negative arg means delete backwards." | |
339 | (interactive "p") | |
bc7bb432 | 340 | (when (Buffer-menu-no-header) |
2dd96f23 | 341 | (let ((buffer-read-only nil)) |
77a43e01 RS |
342 | (if (or (null arg) (= arg 0)) |
343 | (setq arg 1)) | |
344 | (while (> arg 0) | |
345 | (delete-char 1) | |
346 | (insert ?D) | |
347 | (forward-line 1) | |
348 | (setq arg (1- arg))) | |
bc7bb432 JB |
349 | (while (and (< arg 0) |
350 | (Buffer-menu-no-header)) | |
77a43e01 RS |
351 | (delete-char 1) |
352 | (insert ?D) | |
353 | (forward-line -1) | |
354 | (setq arg (1+ arg)))))) | |
355 | ||
356 | (defun Buffer-menu-delete-backwards (&optional arg) | |
2dd96f23 | 357 | "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command |
77a43e01 RS |
358 | and then move up one line. Prefix arg means move that many lines." |
359 | (interactive "p") | |
bc7bb432 | 360 | (Buffer-menu-delete (- (or arg 1)))) |
2dd96f23 JB |
361 | |
362 | (defun Buffer-menu-save () | |
363 | "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command." | |
364 | (interactive) | |
bc7bb432 | 365 | (when (Buffer-menu-no-header) |
2dd96f23 | 366 | (let ((buffer-read-only nil)) |
bc7bb432 | 367 | (forward-char 2) |
2dd96f23 JB |
368 | (delete-char 1) |
369 | (insert ?S) | |
370 | (forward-line 1)))) | |
371 | ||
a8ce00dc | 372 | (defun Buffer-menu-not-modified (&optional arg) |
2dd96f23 | 373 | "Mark buffer on this line as unmodified (no changes to save)." |
a8ce00dc | 374 | (interactive "P") |
2dd96f23 JB |
375 | (save-excursion |
376 | (set-buffer (Buffer-menu-buffer t)) | |
a8ce00dc | 377 | (set-buffer-modified-p arg)) |
2dd96f23 JB |
378 | (save-excursion |
379 | (beginning-of-line) | |
bc7bb432 | 380 | (forward-char 2) |
bfa13d19 | 381 | (if (= (char-after) (if arg ?\s ?*)) |
2dd96f23 JB |
382 | (let ((buffer-read-only nil)) |
383 | (delete-char 1) | |
bfa13d19 | 384 | (insert (if arg ?* ?\s)))))) |
2dd96f23 | 385 | |
bfb2dda4 DP |
386 | (defun Buffer-menu-beginning () |
387 | (goto-char (point-min)) | |
388 | (unless Buffer-menu-use-header-line | |
389 | (forward-line))) | |
390 | ||
2dd96f23 JB |
391 | (defun Buffer-menu-execute () |
392 | "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands." | |
393 | (interactive) | |
394 | (save-excursion | |
bfb2dda4 | 395 | (Buffer-menu-beginning) |
bc7bb432 | 396 | (while (re-search-forward "^..S" nil t) |
2dd96f23 JB |
397 | (let ((modp nil)) |
398 | (save-excursion | |
399 | (set-buffer (Buffer-menu-buffer t)) | |
400 | (save-buffer) | |
401 | (setq modp (buffer-modified-p))) | |
402 | (let ((buffer-read-only nil)) | |
403 | (delete-char -1) | |
bfa13d19 | 404 | (insert (if modp ?* ?\s)))))) |
2dd96f23 | 405 | (save-excursion |
bfb2dda4 | 406 | (Buffer-menu-beginning) |
2dd96f23 JB |
407 | (let ((buff-menu-buffer (current-buffer)) |
408 | (buffer-read-only nil)) | |
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 JB |
434 | (setq tem (Buffer-menu-buffer t)) |
435 | (let ((buffer-read-only nil)) | |
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 | ||
08c82d4d | 462 | |
2dd96f23 JB |
463 | \f |
464 | (defun Buffer-menu-visit-tags-table () | |
465 | "Visit the tags table in the buffer on this line. See `visit-tags-table'." | |
466 | (interactive) | |
467 | (let ((file (buffer-file-name (Buffer-menu-buffer t)))) | |
468 | (if file | |
469 | (visit-tags-table file) | |
470 | (error "Specified buffer has no file")))) | |
471 | ||
472 | (defun Buffer-menu-1-window () | |
dc6d9681 | 473 | "Select this line's buffer, alone, in full frame." |
2dd96f23 JB |
474 | (interactive) |
475 | (switch-to-buffer (Buffer-menu-buffer t)) | |
476 | (bury-buffer (other-buffer)) | |
477 | (delete-other-windows)) | |
478 | ||
2cf5fc7e RS |
479 | (defun Buffer-menu-mouse-select (event) |
480 | "Select the buffer whose line you click on." | |
481 | (interactive "e") | |
482 | (let (buffer) | |
483 | (save-excursion | |
484 | (set-buffer (window-buffer (posn-window (event-end event)))) | |
485 | (save-excursion | |
486 | (goto-char (posn-point (event-end event))) | |
487 | (setq buffer (Buffer-menu-buffer t)))) | |
488 | (select-window (posn-window (event-end event))) | |
d953b88c RS |
489 | (if (and (window-dedicated-p (selected-window)) |
490 | (eq (selected-window) (frame-root-window))) | |
491 | (switch-to-buffer-other-frame buffer) | |
492 | (switch-to-buffer buffer)))) | |
2cf5fc7e | 493 | |
2dd96f23 JB |
494 | (defun Buffer-menu-this-window () |
495 | "Select this line's buffer in this window." | |
496 | (interactive) | |
497 | (switch-to-buffer (Buffer-menu-buffer t))) | |
498 | ||
499 | (defun Buffer-menu-other-window () | |
500 | "Select this line's buffer in other window, leaving buffer menu visible." | |
501 | (interactive) | |
502 | (switch-to-buffer-other-window (Buffer-menu-buffer t))) | |
503 | ||
35aaf00c RM |
504 | (defun Buffer-menu-switch-other-window () |
505 | "Make the other window select this line's buffer. | |
506 | The current window remains selected." | |
507 | (interactive) | |
a0e4e275 JL |
508 | (let ((pop-up-windows t) |
509 | same-window-buffer-names | |
510 | same-window-regexps) | |
511 | (display-buffer (Buffer-menu-buffer t)))) | |
35aaf00c | 512 | |
2dd96f23 JB |
513 | (defun Buffer-menu-2-window () |
514 | "Select this line's buffer, with previous buffer in second window." | |
515 | (interactive) | |
516 | (let ((buff (Buffer-menu-buffer t)) | |
517 | (menu (current-buffer)) | |
a0e4e275 JL |
518 | (pop-up-windows t) |
519 | same-window-buffer-names | |
520 | same-window-regexps) | |
86dbbfc2 | 521 | (delete-other-windows) |
2dd96f23 JB |
522 | (switch-to-buffer (other-buffer)) |
523 | (pop-to-buffer buff) | |
524 | (bury-buffer menu))) | |
c0274f38 | 525 | |
d91e2b1a | 526 | (defun Buffer-menu-toggle-read-only () |
f450965c | 527 | "Toggle read-only status of buffer on this line, perhaps via version control." |
d91e2b1a ER |
528 | (interactive) |
529 | (let (char) | |
530 | (save-excursion | |
531 | (set-buffer (Buffer-menu-buffer t)) | |
f450965c | 532 | (vc-toggle-read-only) |
bfa13d19 | 533 | (setq char (if buffer-read-only ?% ?\s))) |
d91e2b1a ER |
534 | (save-excursion |
535 | (beginning-of-line) | |
bc7bb432 | 536 | (forward-char 1) |
d91e2b1a ER |
537 | (if (/= (following-char) char) |
538 | (let (buffer-read-only) | |
539 | (delete-char 1) | |
540 | (insert char)))))) | |
541 | ||
36570c92 RS |
542 | (defun Buffer-menu-bury () |
543 | "Bury the buffer listed on this line." | |
544 | (interactive) | |
bc7bb432 | 545 | (when (Buffer-menu-no-header) |
a59faabe RS |
546 | (save-excursion |
547 | (beginning-of-line) | |
548 | (bury-buffer (Buffer-menu-buffer t)) | |
549 | (let ((line (buffer-substring (point) (progn (forward-line 1) (point)))) | |
550 | (buffer-read-only nil)) | |
551 | (delete-region (point) (progn (forward-line -1) (point))) | |
552 | (goto-char (point-max)) | |
553 | (insert line)) | |
554 | (message "Buried buffer moved to the end")))) | |
42a19c2a RS |
555 | |
556 | ||
557 | (defun Buffer-menu-view () | |
558 | "View this line's buffer in View mode." | |
559 | (interactive) | |
560 | (view-buffer (Buffer-menu-buffer t))) | |
561 | ||
562 | ||
563 | (defun Buffer-menu-view-other-window () | |
564 | "View this line's buffer in View mode in another window." | |
565 | (interactive) | |
566 | (view-buffer-other-window (Buffer-menu-buffer t))) | |
0b03ce3a RS |
567 | \f |
568 | ||
569 | (define-key ctl-x-map "\C-b" 'list-buffers) | |
570 | ||
571 | (defun list-buffers (&optional files-only) | |
572 | "Display a list of names of existing buffers. | |
573 | The list is displayed in a buffer named `*Buffer List*'. | |
574 | Note that buffers with names starting with spaces are omitted. | |
575 | Non-null optional arg FILES-ONLY means mention only file buffers. | |
576 | ||
6a0d92d3 | 577 | For more information, see the function `buffer-menu'." |
0b03ce3a | 578 | (interactive "P") |
08c82d4d RS |
579 | (display-buffer (list-buffers-noselect files-only))) |
580 | ||
bc7bb432 JB |
581 | (defun Buffer-menu-buffer+size (name size &optional name-props size-props) |
582 | (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width) | |
583 | (setq name | |
584 | (if (string-match "<[0-9]+>$" name) | |
585 | (concat (substring name 0 | |
586 | (- Buffer-menu-buffer+size-width | |
587 | (max (length size) 3) | |
588 | (match-end 0) | |
589 | (- (match-beginning 0)) | |
590 | 2)) | |
591 | ":" ; narrow ellipsis | |
592 | (match-string 0 name)) | |
593 | (concat (substring name 0 | |
594 | (- Buffer-menu-buffer+size-width | |
595 | (max (length size) 3) | |
596 | 2)) | |
ee5861c8 AS |
597 | ":"))) ; narrow ellipsis |
598 | ;; Don't put properties on (buffer-name). | |
599 | (setq name (copy-sequence name))) | |
bc7bb432 JB |
600 | (add-text-properties 0 (length name) name-props name) |
601 | (add-text-properties 0 (length size) size-props size) | |
602 | (concat name | |
603 | (make-string (- Buffer-menu-buffer+size-width | |
604 | (length name) | |
605 | (length size)) | |
bfa13d19 | 606 | ?\s) |
bc7bb432 JB |
607 | size)) |
608 | ||
d5aacb46 SM |
609 | (defun Buffer-menu-sort (column) |
610 | "Sort the buffer menu by COLUMN." | |
611 | (interactive "P") | |
612 | (when column | |
613 | (setq column (prefix-numeric-value column)) | |
614 | (if (< column 2) (setq column 2)) | |
615 | (if (> column 5) (setq column 5))) | |
616 | (setq Buffer-menu-sort-column column) | |
bfb2dda4 DP |
617 | (let (buffer-read-only l buf m1 m2) |
618 | (save-excursion | |
619 | (Buffer-menu-beginning) | |
620 | (while (not (eobp)) | |
621 | (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer))) | |
622 | (setq m1 (char-after) | |
623 | m1 (if (memq m1 '(?> ?D)) m1) | |
624 | m2 (char-after (+ (point) 2)) | |
625 | m2 (if (eq m2 ?S) m2)) | |
626 | (if (or m1 m2) | |
627 | (push (list buf m1 m2) l))) | |
628 | (forward-line))) | |
629 | (Buffer-menu-revert) | |
630 | (setq buffer-read-only) | |
631 | (save-excursion | |
632 | (Buffer-menu-beginning) | |
633 | (while (not (eobp)) | |
634 | (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l)) | |
635 | (setq m1 (cadr buf) | |
636 | m2 (cadr (cdr buf))) | |
637 | (when m1 | |
638 | (delete-char 1) | |
639 | (insert m1) | |
640 | (backward-char 1)) | |
641 | (when m2 | |
642 | (forward-char 2) | |
643 | (delete-char 1) | |
644 | (insert m2))) | |
645 | (forward-line))))) | |
d5aacb46 | 646 | |
104fc809 CY |
647 | (defun Buffer-menu-sort-by-column (&optional e) |
648 | "Sort the buffer menu by the column clicked on." | |
649 | (interactive (list last-input-event)) | |
650 | (if e (mouse-select-window e)) | |
651 | (let* ((pos (event-start e)) | |
652 | (obj (posn-object pos)) | |
653 | (col (if obj | |
654 | (get-text-property (cdr obj) 'column (car obj)) | |
655 | (get-text-property (posn-point pos) 'column)))) | |
656 | (Buffer-menu-sort col))) | |
657 | ||
658 | (defvar Buffer-menu-sort-button-map | |
659 | (let ((map (make-sparse-keymap))) | |
660 | ;; This keymap handles both nil and non-nil values for | |
661 | ;; Buffer-menu-use-header-line. | |
662 | (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column) | |
663 | (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column) | |
664 | (define-key map [mouse-2] 'Buffer-menu-sort-by-column) | |
665 | (define-key map [follow-link] 'mouse-face) | |
666 | (define-key map "\C-m" 'Buffer-menu-sort-by-column) | |
667 | map) | |
668 | "Local keymap for Buffer menu sort buttons.") | |
669 | ||
d5aacb46 SM |
670 | (defun Buffer-menu-make-sort-button (name column) |
671 | (if (equal column Buffer-menu-sort-column) (setq column nil)) | |
104fc809 CY |
672 | (propertize name |
673 | 'column column | |
674 | 'help-echo (concat | |
675 | (if Buffer-menu-use-header-line | |
676 | "mouse-1, mouse-2: sort by " | |
677 | "mouse-2, RET: sort by ") | |
678 | (if column (downcase name) "visited order")) | |
679 | 'mouse-face 'highlight | |
680 | 'keymap Buffer-menu-sort-button-map)) | |
d5aacb46 | 681 | |
61617913 | 682 | (defun list-buffers-noselect (&optional files-only buffer-list) |
08c82d4d RS |
683 | "Create and return a buffer with a list of names of existing buffers. |
684 | The buffer is named `*Buffer List*'. | |
685 | Note that buffers with names starting with spaces are omitted. | |
686 | Non-null optional arg FILES-ONLY means mention only file buffers. | |
687 | ||
61617913 RS |
688 | If BUFFER-LIST is non-nil, it should be a list of buffers; |
689 | it means list those buffers and no others. | |
690 | ||
6a0d92d3 | 691 | For more information, see the function `buffer-menu'." |
bc7bb432 JB |
692 | (let* ((old-buffer (current-buffer)) |
693 | (standard-output standard-output) | |
bfa13d19 | 694 | (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s)) |
5e684428 | 695 | (header (concat "CRM " |
d5aacb46 SM |
696 | (Buffer-menu-buffer+size |
697 | (Buffer-menu-make-sort-button "Buffer" 2) | |
698 | (Buffer-menu-make-sort-button "Size" 3)) | |
699 | " " | |
700 | (Buffer-menu-make-sort-button "Mode" 4) mode-end | |
701 | (Buffer-menu-make-sort-button "File" 5) "\n")) | |
f4872033 | 702 | list desired-point) |
748a336b | 703 | (when Buffer-menu-use-header-line |
d5aacb46 | 704 | (let ((pos 0)) |
fff861a2 JB |
705 | ;; Turn whitespace chars in the header into stretch specs so |
706 | ;; they work regardless of the header-line face. | |
707 | (while (string-match "[ \t\n]+" header pos) | |
748a336b SM |
708 | (setq pos (match-end 0)) |
709 | (put-text-property (match-beginning 0) pos 'display | |
5e684428 | 710 | ;; Assume fixed-size chars in the buffer. |
6d3bce2b | 711 | (list 'space :align-to pos) |
5e684428 SM |
712 | header))) |
713 | ;; Try to better align the one-char headers. | |
714 | (put-text-property 0 3 'face 'fixed-pitch header) | |
715 | ;; Add a "dummy" leading space to align the beginning of the header | |
716 | ;; line with the beginning of the text (rather than with the left | |
c822571a | 717 | ;; scrollbar or the left fringe). --Stef |
5e684428 | 718 | (setq header (concat (propertize " " 'display '(space :align-to 0)) |
a6a2fd5e | 719 | header))) |
d5aacb46 | 720 | (with-current-buffer (get-buffer-create "*Buffer List*") |
08c82d4d RS |
721 | (setq buffer-read-only nil) |
722 | (erase-buffer) | |
723 | (setq standard-output (current-buffer)) | |
bc7bb432 | 724 | (unless Buffer-menu-use-header-line |
c822571a SM |
725 | ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII |
726 | ;; (i.e. U+002D, HYPHEN-MINUS). | |
727 | (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-))) | |
5e684428 SM |
728 | (insert header |
729 | (apply 'string | |
730 | (mapcar (lambda (c) | |
bfa13d19 | 731 | (if (memq c '(?\n ?\s)) c underline)) |
5e684428 | 732 | header))))) |
a6a2fd5e | 733 | ;; Collect info for every buffer we're interested in. |
3ce5f932 LT |
734 | (dolist (buffer (or buffer-list |
735 | (buffer-list | |
736 | (when Buffer-menu-use-frame-buffer-list | |
737 | (selected-frame))))) | |
a6a2fd5e JL |
738 | (with-current-buffer buffer |
739 | (let ((name (buffer-name)) | |
740 | (file buffer-file-name)) | |
741 | (unless (and (not buffer-list) | |
742 | (or | |
743 | ;; Don't mention internal buffers. | |
744 | (and (string= (substring name 0 1) " ") (null file)) | |
745 | ;; Maybe don't mention buffers without files. | |
746 | (and files-only (not file)) | |
747 | (string= name "*Buffer List*"))) | |
748 | ;; Otherwise output info. | |
749 | (let ((mode (concat (format-mode-line mode-name nil nil buffer) | |
750 | (if mode-line-process | |
751 | (format-mode-line mode-line-process | |
752 | nil nil buffer)))) | |
753 | (bits (string | |
bfa13d19 | 754 | (if (eq buffer old-buffer) ?. ?\s) |
a6a2fd5e JL |
755 | ;; Handle readonly status. The output buffer |
756 | ;; is special cased to appear readonly; it is | |
757 | ;; actually made so at a later date. | |
758 | (if (or (eq buffer standard-output) | |
759 | buffer-read-only) | |
bfa13d19 | 760 | ?% ?\s) |
a6a2fd5e | 761 | ;; Identify modified buffers. |
bfa13d19 | 762 | (if (buffer-modified-p) ?* ?\s) |
a6a2fd5e | 763 | ;; Space separator. |
bfa13d19 | 764 | ?\s))) |
a6a2fd5e JL |
765 | (unless file |
766 | ;; No visited file. Check local value of | |
fb5614e8 EZ |
767 | ;; list-buffers-directory and, for Info buffers, |
768 | ;; Info-current-file. | |
769 | (cond ((and (boundp 'list-buffers-directory) | |
770 | list-buffers-directory) | |
771 | (setq file list-buffers-directory)) | |
772 | ((eq major-mode 'Info-mode) | |
773 | (setq file Info-current-file) | |
774 | (cond | |
26f544d3 | 775 | ((equal file "dir") |
fb5614e8 EZ |
776 | (setq file "*Info Directory*")) |
777 | ((eq file 'apropos) | |
778 | (setq file "*Info Apropos*")) | |
779 | ((eq file 'history) | |
780 | (setq file "*Info History*")) | |
781 | ((eq file 'toc) | |
782 | (setq file "*Info TOC*")) | |
783 | ((not (stringp file)) ;; avoid errors | |
b653cee4 EZ |
784 | (setq file nil)) |
785 | (t | |
786 | (setq file (concat "(" | |
787 | (file-name-nondirectory file) | |
788 | ")" | |
789 | Info-current-node))))))) | |
a6a2fd5e JL |
790 | (push (list buffer bits name (buffer-size) mode file) |
791 | list)))))) | |
792 | ;; Preserve the original buffer-list ordering, just in case. | |
793 | (setq list (nreverse list)) | |
f4872033 | 794 | ;; Place the buffers's info in the output buffer, sorted if necessary. |
bc7bb432 JB |
795 | (dolist (buffer |
796 | (if Buffer-menu-sort-column | |
797 | (sort list | |
798 | (if (eq Buffer-menu-sort-column 3) | |
799 | (lambda (a b) | |
800 | (< (nth Buffer-menu-sort-column a) | |
801 | (nth Buffer-menu-sort-column b))) | |
802 | (lambda (a b) | |
803 | (string< (nth Buffer-menu-sort-column a) | |
804 | (nth Buffer-menu-sort-column b))))) | |
805 | list)) | |
806 | (if (eq (car buffer) old-buffer) | |
807 | (setq desired-point (point))) | |
808 | (insert (cadr buffer) | |
809 | ;; Put the buffer name into a text property | |
810 | ;; so we don't have to extract it from the text. | |
811 | ;; This way we avoid problems with unusual buffer names. | |
392640c4 NR |
812 | (let ((name (nth 2 buffer)) |
813 | (size (int-to-string (nth 3 buffer)))) | |
814 | (Buffer-menu-buffer+size name size | |
815 | `(buffer-name ,name | |
816 | buffer ,(car buffer) | |
817 | font-lock-face buffer-menu-buffer | |
818 | mouse-face highlight | |
819 | help-echo | |
820 | ,(if (>= (length name) | |
821 | (- Buffer-menu-buffer+size-width | |
822 | (max (length size) 3) | |
823 | 2)) | |
824 | name | |
825 | "mouse-2: select this buffer")))) | |
826 | " " | |
bc7bb432 JB |
827 | (if (> (length (nth 4 buffer)) Buffer-menu-mode-width) |
828 | (substring (nth 4 buffer) 0 Buffer-menu-mode-width) | |
829 | (nth 4 buffer))) | |
830 | (when (nth 5 buffer) | |
831 | (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width | |
832 | Buffer-menu-mode-width 4) 1) | |
833 | (princ (abbreviate-file-name (nth 5 buffer)))) | |
834 | (princ "\n")) | |
0b03ce3a | 835 | (Buffer-menu-mode) |
bc7bb432 | 836 | (when Buffer-menu-use-header-line |
748a336b | 837 | (setq header-line-format header)) |
08c82d4d RS |
838 | ;; DESIRED-POINT doesn't have to be set; it is not when the |
839 | ;; current buffer is not displayed for some reason. | |
0b03ce3a | 840 | (and desired-point |
08c82d4d | 841 | (goto-char desired-point)) |
7b0a86ab LT |
842 | (setq Buffer-menu-files-only files-only) |
843 | (set-buffer-modified-p nil) | |
08c82d4d | 844 | (current-buffer)))) |
0b03ce3a | 845 | |
f4872033 | 846 | ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6 |
c0274f38 | 847 | ;;; buff-menu.el ends here |