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