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 Free Software Foundation, Inc.
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 2, or (at your option)
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
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.
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.
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."
72 (defcustom Buffer-menu-use-header-line t
73 "*Non-nil means to use an immovable header-line."
77 (defface Buffer-menu-buffer
79 "Face used to highlight buffer name."
81 :group
'font-lock-highlighting-faces
)
82 ;; backward-compatibility alias
83 (put 'Buffer-menu-buffer-face
'face-alias
'Buffer-menu-buffer
)
85 (defcustom Buffer-menu-buffer
+size-width
26
86 "*How wide to jointly make the buffer name and size columns."
90 (defcustom Buffer-menu-mode-width
16
91 "*How wide to make the mode name column."
95 ;; This should get updated & resorted when you click on a column heading
96 (defvar Buffer-menu-sort-column nil
97 "*2 for sorting by buffer names. 5 for sorting by file names.
98 nil for default sorting by visited order.")
100 (defconst Buffer-menu-buffer-column
4)
102 (defvar Buffer-menu-mode-map nil
103 "Local keymap for `Buffer-menu-mode' buffers.")
105 (defvar Buffer-menu-files-only nil
106 "Non-nil if the current buffer-menu lists only file buffers.
107 This variable determines whether reverting the buffer lists only
108 file buffers. It affects both manual reverting and reverting by
111 (make-variable-buffer-local 'Buffer-menu-files-only
)
113 (if Buffer-menu-mode-map
115 (setq Buffer-menu-mode-map
(make-keymap))
116 (suppress-keymap Buffer-menu-mode-map t
)
117 (define-key Buffer-menu-mode-map
"q" 'quit-window
)
118 (define-key Buffer-menu-mode-map
"v" 'Buffer-menu-select
)
119 (define-key Buffer-menu-mode-map
"2" 'Buffer-menu-2-window
)
120 (define-key Buffer-menu-mode-map
"1" 'Buffer-menu-1-window
)
121 (define-key Buffer-menu-mode-map
"f" 'Buffer-menu-this-window
)
122 (define-key Buffer-menu-mode-map
"e" 'Buffer-menu-this-window
)
123 (define-key Buffer-menu-mode-map
"\C-m" 'Buffer-menu-this-window
)
124 (define-key Buffer-menu-mode-map
"o" 'Buffer-menu-other-window
)
125 (define-key Buffer-menu-mode-map
"\C-o" 'Buffer-menu-switch-other-window
)
126 (define-key Buffer-menu-mode-map
"s" 'Buffer-menu-save
)
127 (define-key Buffer-menu-mode-map
"d" 'Buffer-menu-delete
)
128 (define-key Buffer-menu-mode-map
"k" 'Buffer-menu-delete
)
129 (define-key Buffer-menu-mode-map
"\C-d" 'Buffer-menu-delete-backwards
)
130 (define-key Buffer-menu-mode-map
"\C-k" 'Buffer-menu-delete
)
131 (define-key Buffer-menu-mode-map
"x" 'Buffer-menu-execute
)
132 (define-key Buffer-menu-mode-map
" " 'next-line
)
133 (define-key Buffer-menu-mode-map
"n" 'next-line
)
134 (define-key Buffer-menu-mode-map
"p" 'previous-line
)
135 (define-key Buffer-menu-mode-map
"\177" 'Buffer-menu-backup-unmark
)
136 (define-key Buffer-menu-mode-map
"~" 'Buffer-menu-not-modified
)
137 (define-key Buffer-menu-mode-map
"?" 'describe-mode
)
138 (define-key Buffer-menu-mode-map
"u" 'Buffer-menu-unmark
)
139 (define-key Buffer-menu-mode-map
"m" 'Buffer-menu-mark
)
140 (define-key Buffer-menu-mode-map
"t" 'Buffer-menu-visit-tags-table
)
141 (define-key Buffer-menu-mode-map
"%" 'Buffer-menu-toggle-read-only
)
142 (define-key Buffer-menu-mode-map
"b" 'Buffer-menu-bury
)
143 (define-key Buffer-menu-mode-map
"g" 'Buffer-menu-revert
)
144 (define-key Buffer-menu-mode-map
"V" 'Buffer-menu-view
)
145 (define-key Buffer-menu-mode-map
"T" 'Buffer-menu-toggle-files-only
)
146 (define-key Buffer-menu-mode-map
[mouse-2
] 'Buffer-menu-mouse-select
)
147 (define-key Buffer-menu-mode-map
[follow-link
] 'mouse-face
)
150 ;; Buffer Menu mode is suitable only for specially formatted data.
151 (put 'Buffer-menu-mode
'mode-class
'special
)
153 (defun Buffer-menu-mode ()
154 "Major mode for editing a list of buffers.
155 Each line describes one of the buffers in Emacs.
156 Letters do not insert themselves; instead, they are commands.
157 \\<Buffer-menu-mode-map>
158 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
159 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
160 \\[Buffer-menu-other-window] -- select that buffer in another window,
161 so the buffer menu buffer remains visible in its window.
162 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
163 \\[Buffer-menu-view-other-window] -- select that buffer in
164 another window, in view-mode.
165 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
166 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
167 \\[Buffer-menu-select] -- select current line's buffer.
168 Also show buffers marked with m, in other windows.
169 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
170 \\[Buffer-menu-2-window] -- select that buffer in one window,
171 together with buffer selected before this one in another window.
172 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
173 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
174 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
175 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
176 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
177 \\[Buffer-menu-execute] -- delete or save marked buffers.
178 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
179 With prefix argument, also move up one line.
180 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
181 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
182 \\[Buffer-menu-revert] -- update the list of buffers.
183 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
184 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
185 (kill-all-local-variables)
186 (use-local-map Buffer-menu-mode-map
)
187 (setq major-mode
'Buffer-menu-mode
)
188 (setq mode-name
"Buffer Menu")
189 (set (make-local-variable 'revert-buffer-function
)
190 'Buffer-menu-revert-function
)
191 (set (make-local-variable 'buffer-stale-function
)
192 #'(lambda (&optional noconfirm
) 'fast
))
193 (setq truncate-lines t
)
194 (setq buffer-read-only t
)
195 (run-mode-hooks 'buffer-menu-mode-hook
))
197 ;; This function exists so we can make the doc string of Buffer-menu-mode
199 (defun Buffer-menu-revert ()
200 "Update the list of buffers."
204 (defun Buffer-menu-revert-function (ignore1 ignore2
)
205 (or (eq buffer-undo-list t
)
206 (setq buffer-undo-list nil
))
207 ;; We can not use save-excursion here. The buffer gets erased.
208 (let ((opoint (point))
210 (ocol (current-column))
211 (oline (progn (move-to-column 4)
212 (get-text-property (point) 'buffer
)))
214 ;; do not make undo records for the reversion.
215 (buffer-undo-list t
))
216 (list-buffers-noselect Buffer-menu-files-only
)
218 (while (setq prop
(next-single-property-change prop
'buffer
))
219 (when (eq (get-text-property prop
'buffer
) oline
)
221 (move-to-column ocol
)))
222 (goto-char (if eobp
(point-max) opoint
)))))
224 (defun Buffer-menu-toggle-files-only (arg)
225 "Toggle whether the current buffer-menu displays only file buffers.
226 With a positive ARG display only file buffers. With zero or
227 negative ARG, display other buffers as well."
229 (setq Buffer-menu-files-only
230 (cond ((not arg
) (not Buffer-menu-files-only
))
231 ((> (prefix-numeric-value arg
) 0) t
)))
235 (defun Buffer-menu-buffer (error-if-non-existent-p)
236 "Return buffer described by this line of buffer menu."
237 (let* ((where (save-excursion
239 (+ (point) Buffer-menu-buffer-column
)))
240 (name (and (not (eobp)) (get-text-property where
'buffer-name
)))
241 (buf (and (not (eobp)) (get-text-property where
'buffer
))))
243 (or (get-buffer name
)
244 (and buf
(buffer-name buf
) buf
)
245 (if error-if-non-existent-p
246 (error "No buffer named `%s'" name
)
248 (or (and buf
(buffer-name buf
) buf
)
249 (if error-if-non-existent-p
250 (error "No buffer on this line")
253 (defun buffer-menu (&optional arg
)
254 "Make a menu of buffers so you can save, delete or select them.
255 With argument, show only buffers that are visiting files.
256 Type ? after invocation to get help on commands available.
257 Type q to remove the buffer menu from the display.
259 The first column shows `>' for a buffer you have
260 marked to be displayed, `D' for one you have marked for
261 deletion, and `.' for the current buffer.
263 The C column has a `.' for the buffer from which you came.
264 The R column has a `%' if the buffer is read-only.
265 The M column has a `*' if it is modified,
266 or `S' if you have marked it for saving.
267 After this come the buffer name, its size in characters,
268 its major mode, and the visited file name (if any)."
270 ;;; (setq Buffer-menu-window-config (current-window-configuration))
271 (switch-to-buffer (list-buffers-noselect arg
))
273 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
275 (defun buffer-menu-other-window (&optional arg
)
276 "Display a list of buffers in another window.
277 With the buffer list buffer, you can save, delete or select the buffers.
278 With argument, show only buffers that are visiting files.
279 Type ? after invocation to get help on commands available.
280 Type q to remove the buffer menu from the display.
281 For more information, see the function `buffer-menu'."
283 ;;; (setq Buffer-menu-window-config (current-window-configuration))
284 (switch-to-buffer-other-window (list-buffers-noselect arg
))
286 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
288 (defun Buffer-menu-no-header ()
290 (if (or Buffer-menu-use-header-line
291 (not (eq (char-after) ?C
)))
297 (defun Buffer-menu-mark ()
298 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
300 (when (Buffer-menu-no-header)
301 (let ((buffer-read-only nil
))
306 (defun Buffer-menu-unmark (&optional backup
)
307 "Cancel all requested operations on buffer on this line and move down.
308 Optional ARG means move up."
310 (when (Buffer-menu-no-header)
311 (let* ((buf (Buffer-menu-buffer t
))
312 (mod (buffer-modified-p buf
))
313 (readonly (save-excursion (set-buffer buf
) buffer-read-only
))
314 (buffer-read-only nil
))
316 (insert (if readonly
(if mod
" %*" " % ") (if mod
" *" " ")))))
317 (forward-line (if backup -
1 1)))
319 (defun Buffer-menu-backup-unmark ()
320 "Move up and cancel all requested operations on buffer on line above."
326 (defun Buffer-menu-delete (&optional arg
)
327 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
328 Prefix arg is how many buffers to delete.
329 Negative arg means delete backwards."
331 (when (Buffer-menu-no-header)
332 (let ((buffer-read-only nil
))
333 (if (or (null arg
) (= arg
0))
340 (while (and (< arg
0)
341 (Buffer-menu-no-header))
345 (setq arg
(1+ arg
))))))
347 (defun Buffer-menu-delete-backwards (&optional arg
)
348 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
349 and then move up one line. Prefix arg means move that many lines."
351 (Buffer-menu-delete (- (or arg
1))))
353 (defun Buffer-menu-save ()
354 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
356 (when (Buffer-menu-no-header)
357 (let ((buffer-read-only nil
))
363 (defun Buffer-menu-not-modified (&optional arg
)
364 "Mark buffer on this line as unmodified (no changes to save)."
367 (set-buffer (Buffer-menu-buffer t
))
368 (set-buffer-modified-p arg
))
372 (if (= (char-after) (if arg ? ?
*))
373 (let ((buffer-read-only nil
))
375 (insert (if arg ?
* ?
))))))
377 (defun Buffer-menu-beginning ()
378 (goto-char (point-min))
379 (unless Buffer-menu-use-header-line
382 (defun Buffer-menu-execute ()
383 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
386 (Buffer-menu-beginning)
387 (while (re-search-forward "^..S" nil t
)
390 (set-buffer (Buffer-menu-buffer t
))
392 (setq modp
(buffer-modified-p)))
393 (let ((buffer-read-only nil
))
395 (insert (if modp ?
* ?
))))))
397 (Buffer-menu-beginning)
398 (let ((buff-menu-buffer (current-buffer))
399 (buffer-read-only nil
))
400 (while (re-search-forward "^D" nil t
)
402 (let ((buf (Buffer-menu-buffer nil
)))
404 (eq buf buff-menu-buffer
)
405 (save-excursion (kill-buffer buf
)))
406 (if (and buf
(buffer-name buf
))
407 (progn (delete-char 1)
409 (delete-region (point) (progn (forward-line 1) (point)))
411 (forward-char -
1))))))))
413 (defun Buffer-menu-select ()
414 "Select this line's buffer; also display buffers marked with `>'.
415 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
416 This command deletes and replaces all the previously existing windows
417 in the selected frame."
419 (let ((buff (Buffer-menu-buffer t
))
420 (menu (current-buffer))
423 (Buffer-menu-beginning)
424 (while (re-search-forward "^>" nil t
)
425 (setq tem
(Buffer-menu-buffer t
))
426 (let ((buffer-read-only nil
))
429 (or (eq tem buff
) (memq tem others
) (setq others
(cons tem others
))))
430 (setq others
(nreverse others
)
431 tem
(/ (1- (frame-height)) (1+ (length others
))))
432 (delete-other-windows)
433 (switch-to-buffer buff
)
436 (if (equal (length others
) 0)
438 ;;; ;; Restore previous window configuration before displaying
439 ;;; ;; selected buffers.
440 ;;; (if Buffer-menu-window-config
442 ;;; (set-window-configuration Buffer-menu-window-config)
443 ;;; (setq Buffer-menu-window-config nil)))
444 (switch-to-buffer buff
))
446 (split-window nil tem
)
448 (switch-to-buffer (car others
))
449 (setq others
(cdr others
)))
450 (other-window 1) ;back to the beginning!
455 (defun Buffer-menu-visit-tags-table ()
456 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
458 (let ((file (buffer-file-name (Buffer-menu-buffer t
))))
460 (visit-tags-table file
)
461 (error "Specified buffer has no file"))))
463 (defun Buffer-menu-1-window ()
464 "Select this line's buffer, alone, in full frame."
466 (switch-to-buffer (Buffer-menu-buffer t
))
467 (bury-buffer (other-buffer))
468 (delete-other-windows))
470 (defun Buffer-menu-mouse-select (event)
471 "Select the buffer whose line you click on."
475 (set-buffer (window-buffer (posn-window (event-end event
))))
477 (goto-char (posn-point (event-end event
)))
478 (setq buffer
(Buffer-menu-buffer t
))))
479 (select-window (posn-window (event-end event
)))
480 (if (and (window-dedicated-p (selected-window))
481 (eq (selected-window) (frame-root-window)))
482 (switch-to-buffer-other-frame buffer
)
483 (switch-to-buffer buffer
))))
485 (defun Buffer-menu-this-window ()
486 "Select this line's buffer in this window."
488 (switch-to-buffer (Buffer-menu-buffer t
)))
490 (defun Buffer-menu-other-window ()
491 "Select this line's buffer in other window, leaving buffer menu visible."
493 (switch-to-buffer-other-window (Buffer-menu-buffer t
)))
495 (defun Buffer-menu-switch-other-window ()
496 "Make the other window select this line's buffer.
497 The current window remains selected."
499 (let ((pop-up-windows t
)
500 same-window-buffer-names
502 (display-buffer (Buffer-menu-buffer t
))))
504 (defun Buffer-menu-2-window ()
505 "Select this line's buffer, with previous buffer in second window."
507 (let ((buff (Buffer-menu-buffer t
))
508 (menu (current-buffer))
510 same-window-buffer-names
512 (delete-other-windows)
513 (switch-to-buffer (other-buffer))
517 (defun Buffer-menu-toggle-read-only ()
518 "Toggle read-only status of buffer on this line, perhaps via version control."
522 (set-buffer (Buffer-menu-buffer t
))
523 (vc-toggle-read-only)
524 (setq char
(if buffer-read-only ?% ?
)))
528 (if (/= (following-char) char
)
529 (let (buffer-read-only)
533 (defun Buffer-menu-bury ()
534 "Bury the buffer listed on this line."
536 (when (Buffer-menu-no-header)
539 (bury-buffer (Buffer-menu-buffer t
))
540 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
541 (buffer-read-only nil
))
542 (delete-region (point) (progn (forward-line -
1) (point)))
543 (goto-char (point-max))
545 (message "Buried buffer moved to the end"))))
548 (defun Buffer-menu-view ()
549 "View this line's buffer in View mode."
551 (view-buffer (Buffer-menu-buffer t
)))
554 (defun Buffer-menu-view-other-window ()
555 "View this line's buffer in View mode in another window."
557 (view-buffer-other-window (Buffer-menu-buffer t
)))
560 (define-key ctl-x-map
"\C-b" 'list-buffers
)
562 (defun list-buffers (&optional files-only
)
563 "Display a list of names of existing buffers.
564 The list is displayed in a buffer named `*Buffer List*'.
565 Note that buffers with names starting with spaces are omitted.
566 Non-null optional arg FILES-ONLY means mention only file buffers.
568 For more information, see the function `buffer-menu'."
570 (display-buffer (list-buffers-noselect files-only
)))
572 (defun Buffer-menu-buffer+size
(name size
&optional name-props size-props
)
573 (if (> (+ (length name
) (length size
) 2) Buffer-menu-buffer
+size-width
)
575 (if (string-match "<[0-9]+>$" name
)
576 (concat (substring name
0
577 (- Buffer-menu-buffer
+size-width
578 (max (length size
) 3)
580 (- (match-beginning 0))
582 ":" ; narrow ellipsis
583 (match-string 0 name
))
584 (concat (substring name
0
585 (- Buffer-menu-buffer
+size-width
586 (max (length size
) 3)
588 ":"))) ; narrow ellipsis
589 ;; Don't put properties on (buffer-name).
590 (setq name
(copy-sequence name
)))
591 (add-text-properties 0 (length name
) name-props name
)
592 (add-text-properties 0 (length size
) size-props size
)
594 (make-string (- Buffer-menu-buffer
+size-width
600 (defun Buffer-menu-sort (column)
601 "Sort the buffer menu by COLUMN."
604 (setq column
(prefix-numeric-value column
))
605 (if (< column
2) (setq column
2))
606 (if (> column
5) (setq column
5)))
607 (setq Buffer-menu-sort-column column
)
608 (let (buffer-read-only l buf m1 m2
)
610 (Buffer-menu-beginning)
612 (when (buffer-live-p (setq buf
(get-text-property (+ (point) 4) 'buffer
)))
613 (setq m1
(char-after)
614 m1
(if (memq m1
'(?
> ?D
)) m1
)
615 m2
(char-after (+ (point) 2))
616 m2
(if (eq m2 ?S
) m2
))
618 (push (list buf m1 m2
) l
)))
621 (setq buffer-read-only
)
623 (Buffer-menu-beginning)
625 (when (setq buf
(assq (get-text-property (+ (point) 4) 'buffer
) l
))
638 (defun Buffer-menu-make-sort-button (name column
)
639 (if (equal column Buffer-menu-sort-column
) (setq column nil
))
641 'help-echo
(if column
642 (if Buffer-menu-use-header-line
643 (concat "mouse-2: sort by " (downcase name
))
644 (concat "mouse-2, RET: sort by "
646 (if Buffer-menu-use-header-line
647 "mouse-2: sort by visited order"
648 "mouse-2, RET: sort by visited order"))
649 'mouse-face
'highlight
650 'keymap
(let ((map (make-sparse-keymap)))
651 (if Buffer-menu-use-header-line
652 (define-key map
[header-line mouse-2
]
655 (save-window-excursion
656 (if e
(mouse-select-window e
))
657 (Buffer-menu-sort ,column
))))
658 (define-key map
[mouse-2
]
661 (if e
(mouse-select-window e
))
662 (Buffer-menu-sort ,column
)))
663 (define-key map
"\C-m"
664 `(lambda () (interactive)
665 (Buffer-menu-sort ,column
))))
668 (defun list-buffers-noselect (&optional files-only buffer-list
)
669 "Create and return a buffer with a list of names of existing buffers.
670 The buffer is named `*Buffer List*'.
671 Note that buffers with names starting with spaces are omitted.
672 Non-null optional arg FILES-ONLY means mention only file buffers.
674 If BUFFER-LIST is non-nil, it should be a list of buffers;
675 it means list those buffers and no others.
677 For more information, see the function `buffer-menu'."
678 (let* ((old-buffer (current-buffer))
679 (standard-output standard-output
)
680 (mode-end (make-string (- Buffer-menu-mode-width
2) ?
))
681 (header (concat "CRM "
682 (Buffer-menu-buffer+size
683 (Buffer-menu-make-sort-button "Buffer" 2)
684 (Buffer-menu-make-sort-button "Size" 3))
686 (Buffer-menu-make-sort-button "Mode" 4) mode-end
687 (Buffer-menu-make-sort-button "File" 5) "\n"))
689 (when Buffer-menu-use-header-line
691 ;; Turn spaces in the header into stretch specs so they work
692 ;; regardless of the header-line face.
693 (while (string-match "[ \t]+" header pos
)
694 (setq pos
(match-end 0))
695 (put-text-property (match-beginning 0) pos
'display
696 ;; Assume fixed-size chars in the buffer.
697 (list 'space
:align-to pos
)
699 ;; Try to better align the one-char headers.
700 (put-text-property 0 3 'face
'fixed-pitch header
)
701 ;; Add a "dummy" leading space to align the beginning of the header
702 ;; line with the beginning of the text (rather than with the left
703 ;; scrollbar or the left fringe). –-Stef
704 (setq header
(concat (propertize " " 'display
'(space :align-to
0))
706 (with-current-buffer (get-buffer-create "*Buffer List*")
707 (setq buffer-read-only nil
)
709 (setq standard-output
(current-buffer))
710 (unless Buffer-menu-use-header-line
711 (let ((underline (if (char-displayable-p ?—
) ?— ?-
)))
715 (if (memq c
'(?
\n ?\
)) c underline
))
717 ;; Collect info for every buffer we're interested in.
718 (dolist (buffer (or buffer-list
(buffer-list)))
719 (with-current-buffer buffer
720 (let ((name (buffer-name))
721 (file buffer-file-name
))
722 (unless (and (not buffer-list
)
724 ;; Don't mention internal buffers.
725 (and (string= (substring name
0 1) " ") (null file
))
726 ;; Maybe don't mention buffers without files.
727 (and files-only
(not file
))
728 (string= name
"*Buffer List*")))
729 ;; Otherwise output info.
730 (let ((mode (concat (format-mode-line mode-name nil nil buffer
)
731 (if mode-line-process
732 (format-mode-line mode-line-process
735 (if (eq buffer old-buffer
) ?. ?\
)
736 ;; Handle readonly status. The output buffer
737 ;; is special cased to appear readonly; it is
738 ;; actually made so at a later date.
739 (if (or (eq buffer standard-output
)
742 ;; Identify modified buffers.
743 (if (buffer-modified-p) ?
* ?\
)
747 ;; No visited file. Check local value of
748 ;; list-buffers-directory.
749 (when (and (boundp 'list-buffers-directory
)
750 list-buffers-directory
)
751 (setq file list-buffers-directory
)))
752 (push (list buffer bits name
(buffer-size) mode file
)
754 ;; Preserve the original buffer-list ordering, just in case.
755 (setq list
(nreverse list
))
756 ;; Place the buffers's info in the output buffer, sorted if necessary.
758 (if Buffer-menu-sort-column
760 (if (eq Buffer-menu-sort-column
3)
762 (< (nth Buffer-menu-sort-column a
)
763 (nth Buffer-menu-sort-column b
)))
765 (string< (nth Buffer-menu-sort-column a
)
766 (nth Buffer-menu-sort-column b
)))))
768 (if (eq (car buffer
) old-buffer
)
769 (setq desired-point
(point)))
770 (insert (cadr buffer
)
771 ;; Put the buffer name into a text property
772 ;; so we don't have to extract it from the text.
773 ;; This way we avoid problems with unusual buffer names.
774 (Buffer-menu-buffer+size
(nth 2 buffer
)
775 (int-to-string (nth 3 buffer
))
776 `(buffer-name ,(nth 2 buffer
)
778 font-lock-face Buffer-menu-buffer
780 help-echo
"mouse-2: select this buffer"))
782 (if (> (length (nth 4 buffer
)) Buffer-menu-mode-width
)
783 (substring (nth 4 buffer
) 0 Buffer-menu-mode-width
)
786 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer
+size-width
787 Buffer-menu-mode-width
4) 1)
788 (princ (abbreviate-file-name (nth 5 buffer
))))
791 (when Buffer-menu-use-header-line
792 (setq header-line-format header
))
793 ;; DESIRED-POINT doesn't have to be set; it is not when the
794 ;; current buffer is not displayed for some reason.
796 (goto-char desired-point
))
797 (setq Buffer-menu-files-only files-only
)
798 (set-buffer-modified-p nil
)
801 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
802 ;;; buff-menu.el ends here