]> code.delx.au - gnu-emacs/blob - lisp/buff-menu.el
Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[gnu-emacs] / lisp / buff-menu.el
1 ;;; buff-menu.el --- buffer menu main function and support functions -*- coding:utf-8 -*-
2
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: convenience
8
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
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) 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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Edit, delete, or change attributes of all currently active Emacs
27 ;; buffers from a list summarizing their state. A good way to browse
28 ;; any special or scratch buffers you have loaded, since you can't find
29 ;; them by filename. The single entry point is `list-buffers',
30 ;; normally bound to C-x C-b.
31
32 ;;; Change Log:
33
34 ;; Buffer-menu-view: New function
35 ;; Buffer-menu-view-other-window: New function
36
37 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
38 ;;
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.
45
46 ;;; Code:
47
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.
55
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.
58
59 ;;(defvar Buffer-menu-window-config nil
60 ;; "Window configuration saved from entry to `buffer-menu'.")
61
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.
64
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
75 (defface buffer-menu-buffer
76 '((t (:weight bold)))
77 "Face used to highlight buffer names in the buffer menu."
78 :group 'Buffer-menu)
79 (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer)
80
81 (defcustom Buffer-menu-buffer+size-width 26
82 "How wide to jointly make the buffer name and size columns."
83 :type 'number
84 :group 'Buffer-menu)
85
86 (defcustom Buffer-menu-mode-width 16
87 "How wide to make the mode name column."
88 :type 'number
89 :group 'Buffer-menu)
90
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
101 ;; This should get updated & resorted when you click on a column heading
102 (defvar Buffer-menu-sort-column nil
103 "Which column to sort the menu on.
104 Use 2 to sort by buffer names, or 5 to sort by file names.
105 A nil value means sort by visited order (the default).")
106
107 (defconst Buffer-menu-buffer-column 4)
108
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 (make-variable-buffer-local 'Buffer-menu-files-only)
115
116 (defvar Buffer-menu--buffers nil
117 "If non-nil, list of buffers shown in the current buffer-menu.
118 This variable determines whether reverting the buffer lists only
119 these buffers. It affects both manual reverting and reverting by
120 Auto Revert Mode.")
121 (make-variable-buffer-local 'Buffer-menu--buffers)
122
123 (defvar Info-current-file) ;; from info.el
124 (defvar Info-current-node) ;; from info.el
125
126 (defvar Buffer-menu-mode-map
127 (let ((map (make-keymap))
128 (menu-map (make-sparse-keymap)))
129 (suppress-keymap map t)
130 (define-key map "v" 'Buffer-menu-select)
131 (define-key map "2" 'Buffer-menu-2-window)
132 (define-key map "1" 'Buffer-menu-1-window)
133 (define-key map "f" 'Buffer-menu-this-window)
134 (define-key map "e" 'Buffer-menu-this-window)
135 (define-key map "\C-m" 'Buffer-menu-this-window)
136 (define-key map "o" 'Buffer-menu-other-window)
137 (define-key map "\C-o" 'Buffer-menu-switch-other-window)
138 (define-key map "s" 'Buffer-menu-save)
139 (define-key map "d" 'Buffer-menu-delete)
140 (define-key map "k" 'Buffer-menu-delete)
141 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
142 (define-key map "\C-k" 'Buffer-menu-delete)
143 (define-key map "x" 'Buffer-menu-execute)
144 (define-key map " " 'next-line)
145 (define-key map "n" 'next-line)
146 (define-key map "p" 'previous-line)
147 (define-key map "\177" 'Buffer-menu-backup-unmark)
148 (define-key map "~" 'Buffer-menu-not-modified)
149 (define-key map "u" 'Buffer-menu-unmark)
150 (define-key map "m" 'Buffer-menu-mark)
151 (define-key map "t" 'Buffer-menu-visit-tags-table)
152 (define-key map "%" 'Buffer-menu-toggle-read-only)
153 (define-key map "b" 'Buffer-menu-bury)
154 (define-key map "V" 'Buffer-menu-view)
155 (define-key map "T" 'Buffer-menu-toggle-files-only)
156 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
157 (define-key map [follow-link] 'mouse-face)
158 (define-key map (kbd "M-s a C-s") 'Buffer-menu-isearch-buffers)
159 (define-key map (kbd "M-s a M-C-s") 'Buffer-menu-isearch-buffers-regexp)
160 (define-key map [menu-bar Buffer-menu-mode] (cons (purecopy "Buffer-Menu") menu-map))
161 (define-key menu-map [quit]
162 `(menu-item ,(purecopy "Quit") quit-window
163 :help ,(purecopy "Remove the buffer menu from the display")))
164 (define-key menu-map [rev]
165 `(menu-item ,(purecopy "Refresh") revert-buffer
166 :help ,(purecopy "Refresh the *Buffer List* buffer contents")))
167 (define-key menu-map [s0] menu-bar-separator)
168 (define-key menu-map [tf]
169 `(menu-item ,(purecopy "Show only file buffers") Buffer-menu-toggle-files-only
170 :button (:toggle . Buffer-menu-files-only)
171 :help ,(purecopy "Toggle whether the current buffer-menu displays only file buffers")))
172 (define-key menu-map [s1] menu-bar-separator)
173 ;; FIXME: The "Select" entries could use better names...
174 (define-key menu-map [sel]
175 `(menu-item ,(purecopy "Select marked") Buffer-menu-select
176 :help ,(purecopy "Select this line's buffer; also display buffers marked with `>'")))
177 (define-key menu-map [bm2]
178 `(menu-item ,(purecopy "Select two") Buffer-menu-2-window
179 :help ,(purecopy "Select this line's buffer, with previous buffer in second window")))
180 (define-key menu-map [bm1]
181 `(menu-item ,(purecopy "Select current") Buffer-menu-1-window
182 :help ,(purecopy "Select this line's buffer, alone, in full frame")))
183 (define-key menu-map [ow]
184 `(menu-item ,(purecopy "Select in other window") Buffer-menu-other-window
185 :help ,(purecopy "Select this line's buffer in other window, leaving buffer menu visible")))
186 (define-key menu-map [tw]
187 `(menu-item ,(purecopy "Select in current window") Buffer-menu-this-window
188 :help ,(purecopy "Select this line's buffer in this window")))
189 (define-key menu-map [s2] menu-bar-separator)
190 (define-key menu-map [is]
191 `(menu-item ,(purecopy "Regexp Isearch marked buffers") Buffer-menu-isearch-buffers-regexp
192 :help ,(purecopy "Search for a regexp through all marked buffers using Isearch")))
193 (define-key menu-map [ir]
194 `(menu-item ,(purecopy "Isearch marked buffers") Buffer-menu-isearch-buffers
195 :help ,(purecopy "Search for a string through all marked buffers using Isearch")))
196 (define-key menu-map [s3] menu-bar-separator)
197 (define-key menu-map [by]
198 `(menu-item ,(purecopy "Bury") Buffer-menu-bury
199 :help ,(purecopy "Bury the buffer listed on this line")))
200 (define-key menu-map [vt]
201 `(menu-item ,(purecopy "Set unmodified") Buffer-menu-not-modified
202 :help ,(purecopy "Mark buffer on this line as unmodified (no changes to save)")))
203 (define-key menu-map [ex]
204 `(menu-item ,(purecopy "Execute") Buffer-menu-execute
205 :help ,(purecopy "Save and/or delete buffers marked with s or k commands")))
206 (define-key menu-map [s4] menu-bar-separator)
207 (define-key menu-map [delb]
208 `(menu-item ,(purecopy "Mark for delete and move backwards") Buffer-menu-delete-backwards
209 :help ,(purecopy "Mark buffer on this line to be deleted by x command and move up one line")))
210 (define-key menu-map [del]
211 `(menu-item ,(purecopy "Mark for delete") Buffer-menu-delete
212 :help ,(purecopy "Mark buffer on this line to be deleted by x command")))
213
214 (define-key menu-map [sv]
215 `(menu-item ,(purecopy "Mark for save") Buffer-menu-save
216 :help ,(purecopy "Mark buffer on this line to be saved by x command")))
217 (define-key menu-map [umk]
218 `(menu-item ,(purecopy "Unmark") Buffer-menu-unmark
219 :help ,(purecopy "Cancel all requested operations on buffer on this line and move down")))
220 (define-key menu-map [mk]
221 `(menu-item ,(purecopy "Mark") Buffer-menu-mark
222 :help ,(purecopy "Mark buffer on this line for being displayed by v command")))
223 map)
224 "Local keymap for `Buffer-menu-mode' buffers.")
225
226 ;; Buffer Menu mode is suitable only for specially formatted data.
227 (put 'Buffer-menu-mode 'mode-class 'special)
228
229 (define-derived-mode Buffer-menu-mode special-mode "Buffer Menu"
230 "Major mode for editing a list of buffers.
231 Each line describes one of the buffers in Emacs.
232 Letters do not insert themselves; instead, they are commands.
233 \\<Buffer-menu-mode-map>
234 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
235 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
236 \\[Buffer-menu-other-window] -- select that buffer in another window,
237 so the buffer menu buffer remains visible in its window.
238 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
239 \\[Buffer-menu-view-other-window] -- select that buffer in
240 another window, in view-mode.
241 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
242 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
243 \\[Buffer-menu-select] -- select current line's buffer.
244 Also show buffers marked with m, in other windows.
245 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
246 \\[Buffer-menu-2-window] -- select that buffer in one window,
247 together with buffer selected before this one in another window.
248 \\[Buffer-menu-isearch-buffers] -- Do incremental search in the marked buffers.
249 \\[Buffer-menu-isearch-buffers-regexp] -- Isearch for regexp in the marked buffers.
250 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
251 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
252 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
253 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
254 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
255 \\[Buffer-menu-execute] -- delete or save marked buffers.
256 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
257 With prefix argument, also move up one line.
258 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
259 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
260 \\[revert-buffer] -- update the list of buffers.
261 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
262 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
263 (set (make-local-variable 'revert-buffer-function)
264 'Buffer-menu-revert-function)
265 (set (make-local-variable 'buffer-stale-function)
266 #'(lambda (&optional noconfirm) 'fast))
267 (setq truncate-lines t)
268 (setq buffer-read-only t))
269
270 (define-obsolete-variable-alias 'buffer-menu-mode-hook
271 'Buffer-menu-mode-hook "23.1")
272
273 (defun Buffer-menu-revert-function (ignore1 ignore2)
274 (or (eq buffer-undo-list t)
275 (setq buffer-undo-list nil))
276 ;; We can not use save-excursion here. The buffer gets erased.
277 (let ((opoint (point))
278 (eobp (eobp))
279 (ocol (current-column))
280 (oline (progn (move-to-column 4)
281 (get-text-property (point) 'buffer)))
282 (prop (point-min))
283 ;; do not make undo records for the reversion.
284 (buffer-undo-list t))
285 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
286 ;; temporarily the current buffer. Make sure that the
287 ;; interactively current buffer is correctly identified with a `.'
288 ;; by `list-buffers-noselect'.
289 (with-current-buffer (window-buffer)
290 (list-buffers-noselect Buffer-menu-files-only Buffer-menu--buffers))
291 (if oline
292 (while (setq prop (next-single-property-change prop 'buffer))
293 (when (eq (get-text-property prop 'buffer) oline)
294 (goto-char prop)
295 (move-to-column ocol)))
296 (goto-char (if eobp (point-max) opoint)))))
297
298 (defun Buffer-menu-toggle-files-only (arg)
299 "Toggle whether the current buffer-menu displays only file buffers.
300 With a positive ARG display only file buffers. With zero or
301 negative ARG, display other buffers as well."
302 (interactive "P")
303 (setq Buffer-menu-files-only
304 (cond ((not arg) (not Buffer-menu-files-only))
305 ((> (prefix-numeric-value arg) 0) t)))
306 (revert-buffer))
307
308 \f
309 (defun Buffer-menu-buffer (error-if-non-existent-p)
310 "Return buffer described by this line of buffer menu."
311 (let* ((where (save-excursion
312 (beginning-of-line)
313 (+ (point) Buffer-menu-buffer-column)))
314 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
315 (buf (and (not (eobp)) (get-text-property where 'buffer))))
316 (if name
317 (or (get-buffer name)
318 (and buf (buffer-name buf) buf)
319 (if error-if-non-existent-p
320 (error "No buffer named `%s'" name)
321 nil))
322 (or (and buf (buffer-name buf) buf)
323 (if error-if-non-existent-p
324 (error "No buffer on this line")
325 nil)))))
326 \f
327 (defun buffer-menu (&optional arg)
328 "Make a menu of buffers so you can save, delete or select them.
329 With argument, show only buffers that are visiting files.
330 Type ? after invocation to get help on commands available.
331 Type q to remove the buffer menu from the display.
332
333 The first column shows `>' for a buffer you have
334 marked to be displayed, `D' for one you have marked for
335 deletion, and `.' for the current buffer.
336
337 The C column has a `.' for the buffer from which you came.
338 The R column has a `%' if the buffer is read-only.
339 The M column has a `*' if it is modified,
340 or `S' if you have marked it for saving.
341 After this come the buffer name, its size in characters,
342 its major mode, and the visited file name (if any)."
343 (interactive "P")
344 ;;; (setq Buffer-menu-window-config (current-window-configuration))
345 (switch-to-buffer (list-buffers-noselect arg))
346 (message
347 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
348
349 (defun buffer-menu-other-window (&optional arg)
350 "Display a list of buffers in another window.
351 With the buffer list buffer, you can save, delete or select the buffers.
352 With argument, show only buffers that are visiting files.
353 Type ? after invocation to get help on commands available.
354 Type q to remove the buffer menu from the display.
355 For more information, see the function `buffer-menu'."
356 (interactive "P")
357 ;;; (setq Buffer-menu-window-config (current-window-configuration))
358 (switch-to-buffer-other-window (list-buffers-noselect arg))
359 (message
360 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
361
362 (defun Buffer-menu-no-header ()
363 (beginning-of-line)
364 (if (or Buffer-menu-use-header-line
365 (not (eq (char-after) ?C)))
366 t
367 (ding)
368 (forward-line 1)
369 nil))
370
371 (defun Buffer-menu-mark ()
372 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
373 (interactive)
374 (when (Buffer-menu-no-header)
375 (let ((inhibit-read-only t))
376 (delete-char 1)
377 (insert ?>)
378 (forward-line 1))))
379
380 (defun Buffer-menu-unmark (&optional backup)
381 "Cancel all requested operations on buffer on this line and move down.
382 Optional prefix arg means move up."
383 (interactive "P")
384 (when (Buffer-menu-no-header)
385 (let* ((buf (Buffer-menu-buffer t))
386 (mod (buffer-modified-p buf))
387 (readonly (with-current-buffer buf buffer-read-only))
388 (inhibit-read-only t))
389 (delete-char 3)
390 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
391 (forward-line (if backup -1 1)))
392
393 (defun Buffer-menu-backup-unmark ()
394 "Move up and cancel all requested operations on buffer on line above."
395 (interactive)
396 (forward-line -1)
397 (Buffer-menu-unmark)
398 (forward-line -1))
399
400 (defun Buffer-menu-delete (&optional arg)
401 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
402 Prefix arg is how many buffers to delete.
403 Negative arg means delete backwards."
404 (interactive "p")
405 (when (Buffer-menu-no-header)
406 (let ((inhibit-read-only t))
407 (if (or (null arg) (= arg 0))
408 (setq arg 1))
409 (while (> arg 0)
410 (delete-char 1)
411 (insert ?D)
412 (forward-line 1)
413 (setq arg (1- arg)))
414 (while (and (< arg 0)
415 (Buffer-menu-no-header))
416 (delete-char 1)
417 (insert ?D)
418 (forward-line -1)
419 (setq arg (1+ arg))))))
420
421 (defun Buffer-menu-delete-backwards (&optional arg)
422 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
423 and then move up one line. Prefix arg means move that many lines."
424 (interactive "p")
425 (Buffer-menu-delete (- (or arg 1))))
426
427 (defun Buffer-menu-save ()
428 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
429 (interactive)
430 (when (Buffer-menu-no-header)
431 (let ((inhibit-read-only t))
432 (forward-char 2)
433 (delete-char 1)
434 (insert ?S)
435 (forward-line 1))))
436
437 (defun Buffer-menu-not-modified (&optional arg)
438 "Mark buffer on this line as unmodified (no changes to save)."
439 (interactive "P")
440 (with-current-buffer (Buffer-menu-buffer t)
441 (set-buffer-modified-p arg))
442 (save-excursion
443 (beginning-of-line)
444 (forward-char 2)
445 (if (= (char-after) (if arg ?\s ?*))
446 (let ((inhibit-read-only t))
447 (delete-char 1)
448 (insert (if arg ?* ?\s))))))
449
450 (defun Buffer-menu-beginning ()
451 (goto-char (point-min))
452 (unless Buffer-menu-use-header-line
453 (forward-line)))
454
455 (defun Buffer-menu-execute ()
456 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
457 (interactive)
458 (save-excursion
459 (Buffer-menu-beginning)
460 (while (re-search-forward "^..S" nil t)
461 (let ((modp nil))
462 (with-current-buffer (Buffer-menu-buffer t)
463 (save-buffer)
464 (setq modp (buffer-modified-p)))
465 (let ((inhibit-read-only t))
466 (delete-char -1)
467 (insert (if modp ?* ?\s))))))
468 (save-excursion
469 (Buffer-menu-beginning)
470 (let ((buff-menu-buffer (current-buffer))
471 (inhibit-read-only t))
472 (while (re-search-forward "^D" nil t)
473 (forward-char -1)
474 (let ((buf (Buffer-menu-buffer nil)))
475 (or (eq buf nil)
476 (eq buf buff-menu-buffer)
477 (save-excursion (kill-buffer buf)))
478 (if (and buf (buffer-name buf))
479 (progn (delete-char 1)
480 (insert ?\s))
481 (delete-region (point) (progn (forward-line 1) (point)))
482 (unless (bobp)
483 (forward-char -1))))))))
484
485 (defun Buffer-menu-select ()
486 "Select this line's buffer; also display buffers marked with `>'.
487 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
488 This command deletes and replaces all the previously existing windows
489 in the selected frame."
490 (interactive)
491 (let ((buff (Buffer-menu-buffer t))
492 (menu (current-buffer))
493 (others ())
494 tem)
495 (Buffer-menu-beginning)
496 (while (re-search-forward "^>" nil t)
497 (setq tem (Buffer-menu-buffer t))
498 (let ((inhibit-read-only t))
499 (delete-char -1)
500 (insert ?\s))
501 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
502 (setq others (nreverse others)
503 tem (/ (1- (frame-height)) (1+ (length others))))
504 (delete-other-windows)
505 (switch-to-buffer buff)
506 (or (eq menu buff)
507 (bury-buffer menu))
508 (if (equal (length others) 0)
509 (progn
510 ;;; ;; Restore previous window configuration before displaying
511 ;;; ;; selected buffers.
512 ;;; (if Buffer-menu-window-config
513 ;;; (progn
514 ;;; (set-window-configuration Buffer-menu-window-config)
515 ;;; (setq Buffer-menu-window-config nil)))
516 (switch-to-buffer buff))
517 (while others
518 (split-window nil tem)
519 (other-window 1)
520 (switch-to-buffer (car others))
521 (setq others (cdr others)))
522 (other-window 1) ;back to the beginning!
523 )))
524
525 (defun Buffer-menu-marked-buffers ()
526 "Return a list of buffers marked with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
527 (let (buffers)
528 (Buffer-menu-beginning)
529 (while (re-search-forward "^>" nil t)
530 (setq buffers (cons (Buffer-menu-buffer t) buffers)))
531 (nreverse buffers)))
532
533 (defun Buffer-menu-isearch-buffers ()
534 "Search for a string through all marked buffers using Isearch."
535 (interactive)
536 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
537
538 (defun Buffer-menu-isearch-buffers-regexp ()
539 "Search for a regexp through all marked buffers using Isearch."
540 (interactive)
541 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
542
543 \f
544 (defun Buffer-menu-visit-tags-table ()
545 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
546 (interactive)
547 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
548 (if file
549 (visit-tags-table file)
550 (error "Specified buffer has no file"))))
551
552 (defun Buffer-menu-1-window ()
553 "Select this line's buffer, alone, in full frame."
554 (interactive)
555 (switch-to-buffer (Buffer-menu-buffer t))
556 (bury-buffer (other-buffer))
557 (delete-other-windows))
558
559 (defun Buffer-menu-mouse-select (event)
560 "Select the buffer whose line you click on."
561 (interactive "e")
562 (let (buffer)
563 (with-current-buffer (window-buffer (posn-window (event-end event)))
564 (save-excursion
565 (goto-char (posn-point (event-end event)))
566 (setq buffer (Buffer-menu-buffer t))))
567 (select-window (posn-window (event-end event)))
568 (if (and (window-dedicated-p (selected-window))
569 (eq (selected-window) (frame-root-window)))
570 (switch-to-buffer-other-frame buffer)
571 (switch-to-buffer buffer))))
572
573 (defun Buffer-menu-this-window ()
574 "Select this line's buffer in this window."
575 (interactive)
576 (switch-to-buffer (Buffer-menu-buffer t)))
577
578 (defun Buffer-menu-other-window ()
579 "Select this line's buffer in other window, leaving buffer menu visible."
580 (interactive)
581 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
582
583 (defun Buffer-menu-switch-other-window ()
584 "Make the other window select this line's buffer.
585 The current window remains selected."
586 (interactive)
587 (let ((pop-up-windows t)
588 same-window-buffer-names
589 same-window-regexps)
590 (display-buffer (Buffer-menu-buffer t))))
591
592 (defun Buffer-menu-2-window ()
593 "Select this line's buffer, with previous buffer in second window."
594 (interactive)
595 (let ((buff (Buffer-menu-buffer t))
596 (menu (current-buffer))
597 (pop-up-windows t)
598 same-window-buffer-names
599 same-window-regexps)
600 (delete-other-windows)
601 (switch-to-buffer (other-buffer))
602 (pop-to-buffer buff)
603 (bury-buffer menu)))
604
605 (defun Buffer-menu-toggle-read-only ()
606 "Toggle read-only status of buffer on this line, perhaps via version control."
607 (interactive)
608 (let (char)
609 (with-current-buffer (Buffer-menu-buffer t)
610 (toggle-read-only)
611 (setq char (if buffer-read-only ?% ?\s)))
612 (save-excursion
613 (beginning-of-line)
614 (forward-char 1)
615 (if (/= (following-char) char)
616 (let ((inhibit-read-only t))
617 (delete-char 1)
618 (insert char))))))
619
620 (defun Buffer-menu-bury ()
621 "Bury the buffer listed on this line."
622 (interactive)
623 (when (Buffer-menu-no-header)
624 (save-excursion
625 (beginning-of-line)
626 (bury-buffer (Buffer-menu-buffer t))
627 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
628 (inhibit-read-only t))
629 (delete-region (point) (progn (forward-line -1) (point)))
630 (goto-char (point-max))
631 (insert line))
632 (message "Buried buffer moved to the end"))))
633
634
635 (defun Buffer-menu-view ()
636 "View this line's buffer in View mode."
637 (interactive)
638 (view-buffer (Buffer-menu-buffer t)))
639
640
641 (defun Buffer-menu-view-other-window ()
642 "View this line's buffer in View mode in another window."
643 (interactive)
644 (view-buffer-other-window (Buffer-menu-buffer t)))
645 \f
646
647 ;;;###autoload
648 (define-key ctl-x-map "\C-b" 'list-buffers)
649
650 ;;;###autoload
651 (defun list-buffers (&optional files-only)
652 "Display a list of names of existing buffers.
653 The list is displayed in a buffer named `*Buffer List*'.
654 Note that buffers with names starting with spaces are omitted.
655 Non-null optional arg FILES-ONLY means mention only file buffers.
656
657 For more information, see the function `buffer-menu'."
658 (interactive "P")
659 (display-buffer (list-buffers-noselect files-only)))
660
661 (defconst Buffer-menu-short-ellipsis
662 ;; This file is preloaded, so we can't use char-displayable-p here
663 ;; because we don't know yet what display we're going to connect to.
664 ":" ;; (if (char-displayable-p ?…) "…" ":")
665 )
666
667 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
668 (if (> (+ (string-width name) (string-width size) 2)
669 Buffer-menu-buffer+size-width)
670 (setq name
671 (let ((tail
672 (if (string-match "<[0-9]+>$" name)
673 (match-string 0 name)
674 "")))
675 (concat (truncate-string-to-width
676 name
677 (- Buffer-menu-buffer+size-width
678 (max (string-width size) 3)
679 (string-width tail)
680 2))
681 Buffer-menu-short-ellipsis
682 tail)))
683 ;; Don't put properties on (buffer-name).
684 (setq name (copy-sequence name)))
685 (add-text-properties 0 (length name) name-props name)
686 (add-text-properties 0 (length size) size-props size)
687 (let ((name+space-width (- Buffer-menu-buffer+size-width
688 (string-width size))))
689 (concat name
690 (propertize (make-string (- name+space-width (string-width name))
691 ?\s)
692 'display `(space :align-to ,(+ 4 name+space-width)))
693 size)))
694
695 (defun Buffer-menu-sort (column)
696 "Sort the buffer menu by COLUMN."
697 (interactive "P")
698 (when column
699 (setq column (prefix-numeric-value column))
700 (if (< column 2) (setq column 2))
701 (if (> column 5) (setq column 5)))
702 (setq Buffer-menu-sort-column column)
703 (let ((inhibit-read-only t) l buf m1 m2)
704 (save-excursion
705 (Buffer-menu-beginning)
706 (while (not (eobp))
707 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
708 (setq m1 (char-after)
709 m1 (if (memq m1 '(?> ?D)) m1)
710 m2 (char-after (+ (point) 2))
711 m2 (if (eq m2 ?S) m2))
712 (if (or m1 m2)
713 (push (list buf m1 m2) l)))
714 (forward-line)))
715 (revert-buffer)
716 (save-excursion
717 (Buffer-menu-beginning)
718 (while (not (eobp))
719 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
720 (setq m1 (cadr buf)
721 m2 (cadr (cdr buf)))
722 (when m1
723 (delete-char 1)
724 (insert m1)
725 (backward-char 1))
726 (when m2
727 (forward-char 2)
728 (delete-char 1)
729 (insert m2)))
730 (forward-line)))))
731
732 (defun Buffer-menu-sort-by-column (&optional e)
733 "Sort the buffer menu by the column clicked on."
734 (interactive (list last-input-event))
735 (if e (mouse-select-window e))
736 (let* ((pos (event-start e))
737 (obj (posn-object pos))
738 (col (if obj
739 (get-text-property (cdr obj) 'column (car obj))
740 (get-text-property (posn-point pos) 'column))))
741 (Buffer-menu-sort col)))
742
743 (defvar Buffer-menu-sort-button-map
744 (let ((map (make-sparse-keymap)))
745 ;; This keymap handles both nil and non-nil values for
746 ;; Buffer-menu-use-header-line.
747 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
748 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
749 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
750 (define-key map [follow-link] 'mouse-face)
751 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
752 map)
753 "Local keymap for Buffer menu sort buttons.")
754
755 (defun Buffer-menu-make-sort-button (name column)
756 (if (equal column Buffer-menu-sort-column) (setq column nil))
757 (propertize name
758 'column column
759 'help-echo (concat
760 (if Buffer-menu-use-header-line
761 "mouse-1, mouse-2: sort by "
762 "mouse-2, RET: sort by ")
763 (if column (downcase name) "visited order"))
764 'mouse-face 'highlight
765 'keymap Buffer-menu-sort-button-map))
766
767 (defun list-buffers-noselect (&optional files-only buffer-list)
768 "Create and return a buffer with a list of names of existing buffers.
769 The buffer is named `*Buffer List*'.
770 Note that buffers with names starting with spaces are omitted.
771 Non-null optional arg FILES-ONLY means mention only file buffers.
772
773 If BUFFER-LIST is non-nil, it should be a list of buffers;
774 it means list those buffers and no others.
775
776 For more information, see the function `buffer-menu'."
777 (let* ((old-buffer (current-buffer))
778 (standard-output standard-output)
779 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
780 (header (concat "CRM "
781 (Buffer-menu-buffer+size
782 (Buffer-menu-make-sort-button "Buffer" 2)
783 (Buffer-menu-make-sort-button "Size" 3))
784 " "
785 (Buffer-menu-make-sort-button "Mode" 4) mode-end
786 (Buffer-menu-make-sort-button "File" 5) "\n"))
787 list desired-point)
788 (when Buffer-menu-use-header-line
789 (let ((pos 0))
790 ;; Turn whitespace chars in the header into stretch specs so
791 ;; they work regardless of the header-line face.
792 (while (string-match "[ \t\n]+" header pos)
793 (setq pos (match-end 0))
794 (put-text-property (match-beginning 0) pos 'display
795 ;; Assume fixed-size chars in the buffer.
796 (list 'space :align-to pos)
797 header)))
798 ;; Try to better align the one-char headers.
799 (put-text-property 0 3 'face 'fixed-pitch header)
800 ;; Add a "dummy" leading space to align the beginning of the header
801 ;; line with the beginning of the text (rather than with the left
802 ;; scrollbar or the left fringe). --Stef
803 (setq header (concat (propertize " " 'display '(space :align-to 0))
804 header)))
805 (with-current-buffer (get-buffer-create "*Buffer List*")
806 (setq buffer-read-only nil)
807 (erase-buffer)
808 (setq standard-output (current-buffer))
809 (unless Buffer-menu-use-header-line
810 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
811 ;; (i.e. U+002D, HYPHEN-MINUS).
812 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
813 (insert header
814 (apply 'string
815 (mapcar (lambda (c)
816 (if (memq c '(?\n ?\s)) c underline))
817 header)))))
818 ;; Collect info for every buffer we're interested in.
819 (dolist (buffer (or buffer-list
820 (buffer-list
821 (when Buffer-menu-use-frame-buffer-list
822 (selected-frame)))))
823 (with-current-buffer buffer
824 (let ((name (buffer-name))
825 (file buffer-file-name))
826 (unless (and (not buffer-list)
827 (or
828 ;; Don't mention internal buffers.
829 (and (string= (substring name 0 1) " ") (null file))
830 ;; Maybe don't mention buffers without files.
831 (and files-only (not file))
832 (string= name "*Buffer List*")))
833 ;; Otherwise output info.
834 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
835 (if mode-line-process
836 (format-mode-line mode-line-process
837 nil nil buffer))))
838 (bits (string
839 (if (eq buffer old-buffer) ?. ?\s)
840 ;; Handle readonly status. The output buffer
841 ;; is special cased to appear readonly; it is
842 ;; actually made so at a later date.
843 (if (or (eq buffer standard-output)
844 buffer-read-only)
845 ?% ?\s)
846 ;; Identify modified buffers.
847 (if (buffer-modified-p) ?* ?\s)
848 ;; Space separator.
849 ?\s)))
850 (unless file
851 ;; No visited file. Check local value of
852 ;; list-buffers-directory and, for Info buffers,
853 ;; Info-current-file.
854 (cond ((and (boundp 'list-buffers-directory)
855 list-buffers-directory)
856 (setq file list-buffers-directory))
857 ((eq major-mode 'Info-mode)
858 (setq file Info-current-file)
859 (cond
860 ((equal file "dir")
861 (setq file "*Info Directory*"))
862 ((eq file 'apropos)
863 (setq file "*Info Apropos*"))
864 ((eq file 'history)
865 (setq file "*Info History*"))
866 ((eq file 'toc)
867 (setq file "*Info TOC*"))
868 ((not (stringp file)) ;; avoid errors
869 (setq file nil))
870 (t
871 (setq file (concat "("
872 (file-name-nondirectory file)
873 ") "
874 Info-current-node)))))))
875 (push (list buffer bits name (buffer-size) mode file)
876 list))))))
877 ;; Preserve the original buffer-list ordering, just in case.
878 (setq list (nreverse list))
879 ;; Place the buffers's info in the output buffer, sorted if necessary.
880 (dolist (buffer
881 (if Buffer-menu-sort-column
882 (sort list
883 (if (eq Buffer-menu-sort-column 3)
884 (lambda (a b)
885 (< (nth Buffer-menu-sort-column a)
886 (nth Buffer-menu-sort-column b)))
887 (lambda (a b)
888 (string< (nth Buffer-menu-sort-column a)
889 (nth Buffer-menu-sort-column b)))))
890 list))
891 (if (eq (car buffer) old-buffer)
892 (setq desired-point (point)))
893 (insert (cadr buffer)
894 ;; Put the buffer name into a text property
895 ;; so we don't have to extract it from the text.
896 ;; This way we avoid problems with unusual buffer names.
897 (let ((name (nth 2 buffer))
898 (size (int-to-string (nth 3 buffer))))
899 (Buffer-menu-buffer+size name size
900 `(buffer-name ,name
901 buffer ,(car buffer)
902 font-lock-face buffer-menu-buffer
903 mouse-face highlight
904 help-echo
905 ,(if (>= (length name)
906 (- Buffer-menu-buffer+size-width
907 (max (length size) 3)
908 2))
909 name
910 "mouse-2: select this buffer"))))
911 " "
912 (if (> (string-width (nth 4 buffer)) Buffer-menu-mode-width)
913 (truncate-string-to-width (nth 4 buffer)
914 Buffer-menu-mode-width)
915 (nth 4 buffer)))
916 (when (nth 5 buffer)
917 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
918 Buffer-menu-mode-width 4) 1)
919 (princ (abbreviate-file-name (nth 5 buffer))))
920 (princ "\n"))
921 (Buffer-menu-mode)
922 (when Buffer-menu-use-header-line
923 (setq header-line-format header))
924 ;; DESIRED-POINT doesn't have to be set; it is not when the
925 ;; current buffer is not displayed for some reason.
926 (and desired-point
927 (goto-char desired-point))
928 (setq Buffer-menu-files-only files-only)
929 (setq Buffer-menu--buffers buffer-list)
930 (set-buffer-modified-p nil)
931 (current-buffer))))
932
933 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
934 ;;; buff-menu.el ends here