(FORWARD_SIGNAL_TO_MAIN_THREAD): New define.
[emacs.git] / lisp / buff-menu.el
blob9482139339f1bd8b15818fd72a7cbc7557470e4b
1 ;;; buff-menu.el --- buffer menu main function and support functions -*- coding:utf-8 -*-
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: convenience
9 ;; This file is part of GNU Emacs.
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, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Edit, delete, or change attributes of all currently active Emacs
29 ;; buffers from a list summarizing their state. A good way to browse
30 ;; any special or scratch buffers you have loaded, since you can't find
31 ;; them by filename. The single entry point is `list-buffers',
32 ;; normally bound to C-x C-b.
34 ;;; Change Log:
36 ;; Buffer-menu-view: New function
37 ;; Buffer-menu-view-other-window: New function
39 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
41 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
43 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
44 ;; current entry and then move to previous one.
46 ;; Based on FSF code dating back to 1985.
48 ;;; Code:
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.
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.
61 ;;(defvar Buffer-menu-window-config nil
62 ;; "Window configuration saved from entry to `buffer-menu'.")
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.
67 (defgroup Buffer-menu nil
68 "Show a menu of all buffers in a buffer."
69 :group 'tools
70 :group 'convenience)
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)
77 (defface buffer-menu-buffer
78 '((t (:weight bold)))
79 "Face used to highlight buffer names in the buffer menu."
80 :group 'Buffer-menu)
81 (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer)
83 (defcustom Buffer-menu-buffer+size-width 26
84 "*How wide to jointly make the buffer name and size columns."
85 :type 'number
86 :group 'Buffer-menu)
88 (defcustom Buffer-menu-mode-width 16
89 "*How wide to make the mode name column."
90 :type 'number
91 :group 'Buffer-menu)
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")
103 ;; This should get updated & resorted when you click on a column heading
104 (defvar Buffer-menu-sort-column nil
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).")
109 (defconst Buffer-menu-buffer-column 4)
111 (defvar Buffer-menu-mode-map nil
112 "Local keymap for `Buffer-menu-mode' buffers.")
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.")
120 (defvar Info-current-file) ;; from info.el
121 (defvar Info-current-node) ;; from info.el
123 (make-variable-buffer-local 'Buffer-menu-files-only)
125 (if Buffer-menu-mode-map
127 (setq Buffer-menu-mode-map (make-keymap))
128 (suppress-keymap Buffer-menu-mode-map t)
129 (define-key Buffer-menu-mode-map "q" 'quit-window)
130 (define-key Buffer-menu-mode-map "v" 'Buffer-menu-select)
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)
134 (define-key Buffer-menu-mode-map "e" 'Buffer-menu-this-window)
135 (define-key Buffer-menu-mode-map "\C-m" 'Buffer-menu-this-window)
136 (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window)
137 (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window)
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)
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)
154 (define-key Buffer-menu-mode-map "b" 'Buffer-menu-bury)
155 (define-key Buffer-menu-mode-map "g" 'Buffer-menu-revert)
156 (define-key Buffer-menu-mode-map "V" 'Buffer-menu-view)
157 (define-key Buffer-menu-mode-map "T" 'Buffer-menu-toggle-files-only)
158 (define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-mouse-select)
159 (define-key Buffer-menu-mode-map [follow-link] 'mouse-face)
162 ;; Buffer Menu mode is suitable only for specially formatted data.
163 (put 'Buffer-menu-mode 'mode-class 'special)
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>
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.
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.
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.
181 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
182 \\[Buffer-menu-2-window] -- select that buffer in one window,
183 together with buffer selected before this one in another window.
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.
191 With prefix argument, also move up one line.
192 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
193 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
194 \\[Buffer-menu-revert] -- update the list of buffers.
195 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
196 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
197 (kill-all-local-variables)
198 (use-local-map Buffer-menu-mode-map)
199 (setq major-mode 'Buffer-menu-mode)
200 (setq mode-name "Buffer Menu")
201 (set (make-local-variable 'revert-buffer-function)
202 'Buffer-menu-revert-function)
203 (set (make-local-variable 'buffer-stale-function)
204 #'(lambda (&optional noconfirm) 'fast))
205 (setq truncate-lines t)
206 (setq buffer-read-only t)
207 (run-mode-hooks 'buffer-menu-mode-hook))
209 ;; This function exists so we can make the doc string of Buffer-menu-mode
210 ;; look nice.
211 (defun Buffer-menu-revert ()
212 "Update the list of buffers."
213 (interactive)
214 (revert-buffer))
216 (defun Buffer-menu-revert-function (ignore1 ignore2)
217 (or (eq buffer-undo-list t)
218 (setq buffer-undo-list nil))
219 ;; We can not use save-excursion here. The buffer gets erased.
220 (let ((opoint (point))
221 (eobp (eobp))
222 (ocol (current-column))
223 (oline (progn (move-to-column 4)
224 (get-text-property (point) 'buffer)))
225 (prop (point-min))
226 ;; do not make undo records for the reversion.
227 (buffer-undo-list t))
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))
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)))))
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))
252 (defun Buffer-menu-buffer (error-if-non-existent-p)
253 "Return buffer described by this line of buffer menu."
254 (let* ((where (save-excursion
255 (beginning-of-line)
256 (+ (point) Buffer-menu-buffer-column)))
257 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
258 (buf (and (not (eobp)) (get-text-property where 'buffer))))
259 (if name
260 (or (get-buffer name)
261 (and buf (buffer-name buf) buf)
262 (if error-if-non-existent-p
263 (error "No buffer named `%s'" name)
264 nil))
265 (or (and buf (buffer-name buf) buf)
266 (if error-if-non-existent-p
267 (error "No buffer on this line")
268 nil)))))
270 (defun buffer-menu (&optional arg)
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.
274 Type q to remove the buffer menu from the display.
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.
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.
282 The M column has a `*' if it is modified,
283 or `S' if you have marked it for saving.
284 After this come the buffer name, its size in characters,
285 its major mode, and the visited file name (if any)."
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."))
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.
297 Type q to remove the buffer menu from the display.
298 For more information, see the function `buffer-menu'."
299 (interactive "P")
300 ;;; (setq Buffer-menu-window-config (current-window-configuration))
301 (switch-to-buffer-other-window (list-buffers-noselect arg))
302 (message
303 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
305 (defun Buffer-menu-no-header ()
306 (beginning-of-line)
307 (if (or Buffer-menu-use-header-line
308 (not (eq (char-after) ?C)))
310 (ding)
311 (forward-line 1)
312 nil))
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)
317 (when (Buffer-menu-no-header)
318 (let ((buffer-read-only nil))
319 (delete-char 1)
320 (insert ?>)
321 (forward-line 1))))
323 (defun Buffer-menu-unmark (&optional backup)
324 "Cancel all requested operations on buffer on this line and move down.
325 Optional prefix arg means move up."
326 (interactive "P")
327 (when (Buffer-menu-no-header)
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)
333 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
334 (forward-line (if backup -1 1)))
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))
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")
348 (when (Buffer-menu-no-header)
349 (let ((buffer-read-only nil))
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)))
357 (while (and (< arg 0)
358 (Buffer-menu-no-header))
359 (delete-char 1)
360 (insert ?D)
361 (forward-line -1)
362 (setq arg (1+ arg))))))
364 (defun Buffer-menu-delete-backwards (&optional arg)
365 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
366 and then move up one line. Prefix arg means move that many lines."
367 (interactive "p")
368 (Buffer-menu-delete (- (or arg 1))))
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)
373 (when (Buffer-menu-no-header)
374 (let ((buffer-read-only nil))
375 (forward-char 2)
376 (delete-char 1)
377 (insert ?S)
378 (forward-line 1))))
380 (defun Buffer-menu-not-modified (&optional arg)
381 "Mark buffer on this line as unmodified (no changes to save)."
382 (interactive "P")
383 (save-excursion
384 (set-buffer (Buffer-menu-buffer t))
385 (set-buffer-modified-p arg))
386 (save-excursion
387 (beginning-of-line)
388 (forward-char 2)
389 (if (= (char-after) (if arg ?\s ?*))
390 (let ((buffer-read-only nil))
391 (delete-char 1)
392 (insert (if arg ?* ?\s))))))
394 (defun Buffer-menu-beginning ()
395 (goto-char (point-min))
396 (unless Buffer-menu-use-header-line
397 (forward-line)))
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
403 (Buffer-menu-beginning)
404 (while (re-search-forward "^..S" nil t)
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)
412 (insert (if modp ?* ?\s))))))
413 (save-excursion
414 (Buffer-menu-beginning)
415 (let ((buff-menu-buffer (current-buffer))
416 (buffer-read-only nil))
417 (while (re-search-forward "^D" nil t)
418 (forward-char -1)
419 (let ((buf (Buffer-menu-buffer nil)))
420 (or (eq buf nil)
421 (eq buf buff-menu-buffer)
422 (save-excursion (kill-buffer buf)))
423 (if (and buf (buffer-name buf))
424 (progn (delete-char 1)
425 (insert ?\s))
426 (delete-region (point) (progn (forward-line 1) (point)))
427 (unless (bobp)
428 (forward-char -1))))))))
430 (defun Buffer-menu-select ()
431 "Select this line's buffer; also display buffers marked with `>'.
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."
435 (interactive)
436 (let ((buff (Buffer-menu-buffer t))
437 (menu (current-buffer))
438 (others ())
439 tem)
440 (Buffer-menu-beginning)
441 (while (re-search-forward "^>" nil t)
442 (setq tem (Buffer-menu-buffer t))
443 (let ((buffer-read-only nil))
444 (delete-char -1)
445 (insert ?\s))
446 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
447 (setq others (nreverse others)
448 tem (/ (1- (frame-height)) (1+ (length others))))
449 (delete-other-windows)
450 (switch-to-buffer buff)
451 (or (eq menu buff)
452 (bury-buffer menu))
453 (if (equal (length others) 0)
454 (progn
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)))
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!
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"))))
480 (defun Buffer-menu-1-window ()
481 "Select this line's buffer, alone, in full frame."
482 (interactive)
483 (switch-to-buffer (Buffer-menu-buffer t))
484 (bury-buffer (other-buffer))
485 (delete-other-windows))
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)))
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))))
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)))
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)))
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)
516 (let ((pop-up-windows t)
517 same-window-buffer-names
518 same-window-regexps)
519 (display-buffer (Buffer-menu-buffer t))))
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))
526 (pop-up-windows t)
527 same-window-buffer-names
528 same-window-regexps)
529 (delete-other-windows)
530 (switch-to-buffer (other-buffer))
531 (pop-to-buffer buff)
532 (bury-buffer menu)))
534 (defun Buffer-menu-toggle-read-only ()
535 "Toggle read-only status of buffer on this line, perhaps via version control."
536 (interactive)
537 (let (char)
538 (save-excursion
539 (set-buffer (Buffer-menu-buffer t))
540 (vc-toggle-read-only)
541 (setq char (if buffer-read-only ?% ?\s)))
542 (save-excursion
543 (beginning-of-line)
544 (forward-char 1)
545 (if (/= (following-char) char)
546 (let (buffer-read-only)
547 (delete-char 1)
548 (insert char))))))
550 (defun Buffer-menu-bury ()
551 "Bury the buffer listed on this line."
552 (interactive)
553 (when (Buffer-menu-no-header)
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"))))
565 (defun Buffer-menu-view ()
566 "View this line's buffer in View mode."
567 (interactive)
568 (view-buffer (Buffer-menu-buffer t)))
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)))
577 (define-key ctl-x-map "\C-b" 'list-buffers)
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.
585 For more information, see the function `buffer-menu'."
586 (interactive "P")
587 (display-buffer (list-buffers-noselect files-only)))
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))
599 ":" ; narrow ellipsis
600 (match-string 0 name))
601 (concat (substring name 0
602 (- Buffer-menu-buffer+size-width
603 (max (length size) 3)
605 ":"))) ; narrow ellipsis
606 ;; Don't put properties on (buffer-name).
607 (setq name (copy-sequence name)))
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))
614 ?\s)
615 size))
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)
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)))))
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)))
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.")
678 (defun Buffer-menu-make-sort-button (name column)
679 (if (equal column Buffer-menu-sort-column) (setq column nil))
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))
690 (defun list-buffers-noselect (&optional files-only buffer-list)
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.
696 If BUFFER-LIST is non-nil, it should be a list of buffers;
697 it means list those buffers and no others.
699 For more information, see the function `buffer-menu'."
700 (let* ((old-buffer (current-buffer))
701 (standard-output standard-output)
702 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
703 (header (concat "CRM "
704 (Buffer-menu-buffer+size
705 (Buffer-menu-make-sort-button "Buffer" 2)
706 (Buffer-menu-make-sort-button "Size" 3))
708 (Buffer-menu-make-sort-button "Mode" 4) mode-end
709 (Buffer-menu-make-sort-button "File" 5) "\n"))
710 list desired-point)
711 (when Buffer-menu-use-header-line
712 (let ((pos 0))
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)
716 (setq pos (match-end 0))
717 (put-text-property (match-beginning 0) pos 'display
718 ;; Assume fixed-size chars in the buffer.
719 (list 'space :align-to pos)
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
725 ;; scrollbar or the left fringe). --Stef
726 (setq header (concat (propertize " " 'display '(space :align-to 0))
727 header)))
728 (with-current-buffer (get-buffer-create "*Buffer List*")
729 (setq buffer-read-only nil)
730 (erase-buffer)
731 (setq standard-output (current-buffer))
732 (unless Buffer-menu-use-header-line
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 ?-)))
736 (insert header
737 (apply 'string
738 (mapcar (lambda (c)
739 (if (memq c '(?\n ?\s)) c underline))
740 header)))))
741 ;; Collect info for every buffer we're interested in.
742 (dolist (buffer (or buffer-list
743 (buffer-list
744 (when Buffer-menu-use-frame-buffer-list
745 (selected-frame)))))
746 (with-current-buffer buffer
747 (let ((name (buffer-name))
748 (file buffer-file-name))
749 (unless (and (not buffer-list)
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
762 (if (eq buffer old-buffer) ?. ?\s)
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)
768 ?% ?\s)
769 ;; Identify modified buffers.
770 (if (buffer-modified-p) ?* ?\s)
771 ;; Space separator.
772 ?\s)))
773 (unless file
774 ;; No visited file. Check local value of
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
783 ((equal file "dir")
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
792 (setq file nil))
794 (setq file (concat "("
795 (file-name-nondirectory file)
797 Info-current-node)))))))
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))
802 ;; Place the buffers's info in the output buffer, sorted if necessary.
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)
824 font-lock-face buffer-menu-buffer
825 mouse-face highlight
826 help-echo "mouse-2: select this buffer"))
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"))
836 (Buffer-menu-mode)
837 (when Buffer-menu-use-header-line
838 (setq header-line-format header))
839 ;; DESIRED-POINT doesn't have to be set; it is not when the
840 ;; current buffer is not displayed for some reason.
841 (and desired-point
842 (goto-char desired-point))
843 (setq Buffer-menu-files-only files-only)
844 (set-buffer-modified-p nil)
845 (current-buffer))))
847 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
848 ;;; buff-menu.el ends here