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