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