]> code.delx.au - gnu-emacs/blob - lisp/buff-menu.el
Merge from gnus--devo--0
[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, 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 (define-obsolete-variable-alias 'buffer-menu-mode-hook
204 'Buffer-menu-mode-hook "23.1")
205
206 ;; This function exists so we can make the doc string of Buffer-menu-mode
207 ;; look nice.
208 (defun Buffer-menu-revert ()
209 "Update the list of buffers."
210 (interactive)
211 (revert-buffer))
212
213 (defun Buffer-menu-revert-function (ignore1 ignore2)
214 (or (eq buffer-undo-list t)
215 (setq buffer-undo-list nil))
216 ;; We can not use save-excursion here. The buffer gets erased.
217 (let ((opoint (point))
218 (eobp (eobp))
219 (ocol (current-column))
220 (oline (progn (move-to-column 4)
221 (get-text-property (point) 'buffer)))
222 (prop (point-min))
223 ;; do not make undo records for the reversion.
224 (buffer-undo-list t))
225 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
226 ;; temporarily the current buffer. Make sure that the
227 ;; interactively current buffer is correctly identified with a `.'
228 ;; by `list-buffers-noselect'.
229 (with-current-buffer (window-buffer)
230 (list-buffers-noselect Buffer-menu-files-only))
231 (if oline
232 (while (setq prop (next-single-property-change prop 'buffer))
233 (when (eq (get-text-property prop 'buffer) oline)
234 (goto-char prop)
235 (move-to-column ocol)))
236 (goto-char (if eobp (point-max) opoint)))))
237
238 (defun Buffer-menu-toggle-files-only (arg)
239 "Toggle whether the current buffer-menu displays only file buffers.
240 With a positive ARG display only file buffers. With zero or
241 negative ARG, display other buffers as well."
242 (interactive "P")
243 (setq Buffer-menu-files-only
244 (cond ((not arg) (not Buffer-menu-files-only))
245 ((> (prefix-numeric-value arg) 0) t)))
246 (revert-buffer))
247
248 \f
249 (defun Buffer-menu-buffer (error-if-non-existent-p)
250 "Return buffer described by this line of buffer menu."
251 (let* ((where (save-excursion
252 (beginning-of-line)
253 (+ (point) Buffer-menu-buffer-column)))
254 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
255 (buf (and (not (eobp)) (get-text-property where 'buffer))))
256 (if name
257 (or (get-buffer name)
258 (and buf (buffer-name buf) buf)
259 (if error-if-non-existent-p
260 (error "No buffer named `%s'" name)
261 nil))
262 (or (and buf (buffer-name buf) buf)
263 (if error-if-non-existent-p
264 (error "No buffer on this line")
265 nil)))))
266 \f
267 (defun buffer-menu (&optional arg)
268 "Make a menu of buffers so you can save, delete or select them.
269 With argument, show only buffers that are visiting files.
270 Type ? after invocation to get help on commands available.
271 Type q to remove the buffer menu from the display.
272
273 The first column shows `>' for a buffer you have
274 marked to be displayed, `D' for one you have marked for
275 deletion, and `.' for the current buffer.
276
277 The C column has a `.' for the buffer from which you came.
278 The R column has a `%' if the buffer is read-only.
279 The M column has a `*' if it is modified,
280 or `S' if you have marked it for saving.
281 After this come the buffer name, its size in characters,
282 its major mode, and the visited file name (if any)."
283 (interactive "P")
284 ;;; (setq Buffer-menu-window-config (current-window-configuration))
285 (switch-to-buffer (list-buffers-noselect arg))
286 (message
287 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
288
289 (defun buffer-menu-other-window (&optional arg)
290 "Display a list of buffers in another window.
291 With the buffer list buffer, you can save, delete or select the buffers.
292 With argument, show only buffers that are visiting files.
293 Type ? after invocation to get help on commands available.
294 Type q to remove the buffer menu from the display.
295 For more information, see the function `buffer-menu'."
296 (interactive "P")
297 ;;; (setq Buffer-menu-window-config (current-window-configuration))
298 (switch-to-buffer-other-window (list-buffers-noselect arg))
299 (message
300 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
301
302 (defun Buffer-menu-no-header ()
303 (beginning-of-line)
304 (if (or Buffer-menu-use-header-line
305 (not (eq (char-after) ?C)))
306 t
307 (ding)
308 (forward-line 1)
309 nil))
310
311 (defun Buffer-menu-mark ()
312 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
313 (interactive)
314 (when (Buffer-menu-no-header)
315 (let ((inhibit-read-only t))
316 (delete-char 1)
317 (insert ?>)
318 (forward-line 1))))
319
320 (defun Buffer-menu-unmark (&optional backup)
321 "Cancel all requested operations on buffer on this line and move down.
322 Optional prefix arg means move up."
323 (interactive "P")
324 (when (Buffer-menu-no-header)
325 (let* ((buf (Buffer-menu-buffer t))
326 (mod (buffer-modified-p buf))
327 (readonly (with-current-buffer buf buffer-read-only))
328 (inhibit-read-only t))
329 (delete-char 3)
330 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
331 (forward-line (if backup -1 1)))
332
333 (defun Buffer-menu-backup-unmark ()
334 "Move up and cancel all requested operations on buffer on line above."
335 (interactive)
336 (forward-line -1)
337 (Buffer-menu-unmark)
338 (forward-line -1))
339
340 (defun Buffer-menu-delete (&optional arg)
341 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
342 Prefix arg is how many buffers to delete.
343 Negative arg means delete backwards."
344 (interactive "p")
345 (when (Buffer-menu-no-header)
346 (let ((inhibit-read-only t))
347 (if (or (null arg) (= arg 0))
348 (setq arg 1))
349 (while (> arg 0)
350 (delete-char 1)
351 (insert ?D)
352 (forward-line 1)
353 (setq arg (1- arg)))
354 (while (and (< arg 0)
355 (Buffer-menu-no-header))
356 (delete-char 1)
357 (insert ?D)
358 (forward-line -1)
359 (setq arg (1+ arg))))))
360
361 (defun Buffer-menu-delete-backwards (&optional arg)
362 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
363 and then move up one line. Prefix arg means move that many lines."
364 (interactive "p")
365 (Buffer-menu-delete (- (or arg 1))))
366
367 (defun Buffer-menu-save ()
368 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
369 (interactive)
370 (when (Buffer-menu-no-header)
371 (let ((inhibit-read-only t))
372 (forward-char 2)
373 (delete-char 1)
374 (insert ?S)
375 (forward-line 1))))
376
377 (defun Buffer-menu-not-modified (&optional arg)
378 "Mark buffer on this line as unmodified (no changes to save)."
379 (interactive "P")
380 (with-current-buffer (Buffer-menu-buffer t)
381 (set-buffer-modified-p arg))
382 (save-excursion
383 (beginning-of-line)
384 (forward-char 2)
385 (if (= (char-after) (if arg ?\s ?*))
386 (let ((inhibit-read-only t))
387 (delete-char 1)
388 (insert (if arg ?* ?\s))))))
389
390 (defun Buffer-menu-beginning ()
391 (goto-char (point-min))
392 (unless Buffer-menu-use-header-line
393 (forward-line)))
394
395 (defun Buffer-menu-execute ()
396 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
397 (interactive)
398 (save-excursion
399 (Buffer-menu-beginning)
400 (while (re-search-forward "^..S" nil t)
401 (let ((modp nil))
402 (with-current-buffer (Buffer-menu-buffer t)
403 (save-buffer)
404 (setq modp (buffer-modified-p)))
405 (let ((inhibit-read-only t))
406 (delete-char -1)
407 (insert (if modp ?* ?\s))))))
408 (save-excursion
409 (Buffer-menu-beginning)
410 (let ((buff-menu-buffer (current-buffer))
411 (inhibit-read-only t))
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 ((inhibit-read-only t))
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 (defun Buffer-menu-marked-buffers ()
466 "Return a list of buffers marked with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command."
467 (let (buffers)
468 (Buffer-menu-beginning)
469 (while (re-search-forward "^>" nil t)
470 (setq buffers (cons (Buffer-menu-buffer t) buffers)))
471 (nreverse buffers)))
472
473 (defun Buffer-menu-isearch-buffers ()
474 "Search for a string through all marked buffers using Isearch."
475 (interactive)
476 (multi-isearch-buffers (Buffer-menu-marked-buffers)))
477
478 (defun Buffer-menu-isearch-buffers-regexp ()
479 "Search for a regexp through all marked buffers using Isearch."
480 (interactive)
481 (multi-isearch-buffers-regexp (Buffer-menu-marked-buffers)))
482
483 \f
484 (defun Buffer-menu-visit-tags-table ()
485 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
486 (interactive)
487 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
488 (if file
489 (visit-tags-table file)
490 (error "Specified buffer has no file"))))
491
492 (defun Buffer-menu-1-window ()
493 "Select this line's buffer, alone, in full frame."
494 (interactive)
495 (switch-to-buffer (Buffer-menu-buffer t))
496 (bury-buffer (other-buffer))
497 (delete-other-windows))
498
499 (defun Buffer-menu-mouse-select (event)
500 "Select the buffer whose line you click on."
501 (interactive "e")
502 (let (buffer)
503 (with-current-buffer (window-buffer (posn-window (event-end event)))
504 (save-excursion
505 (goto-char (posn-point (event-end event)))
506 (setq buffer (Buffer-menu-buffer t))))
507 (select-window (posn-window (event-end event)))
508 (if (and (window-dedicated-p (selected-window))
509 (eq (selected-window) (frame-root-window)))
510 (switch-to-buffer-other-frame buffer)
511 (switch-to-buffer buffer))))
512
513 (defun Buffer-menu-this-window ()
514 "Select this line's buffer in this window."
515 (interactive)
516 (switch-to-buffer (Buffer-menu-buffer t)))
517
518 (defun Buffer-menu-other-window ()
519 "Select this line's buffer in other window, leaving buffer menu visible."
520 (interactive)
521 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
522
523 (defun Buffer-menu-switch-other-window ()
524 "Make the other window select this line's buffer.
525 The current window remains selected."
526 (interactive)
527 (let ((pop-up-windows t)
528 same-window-buffer-names
529 same-window-regexps)
530 (display-buffer (Buffer-menu-buffer t))))
531
532 (defun Buffer-menu-2-window ()
533 "Select this line's buffer, with previous buffer in second window."
534 (interactive)
535 (let ((buff (Buffer-menu-buffer t))
536 (menu (current-buffer))
537 (pop-up-windows t)
538 same-window-buffer-names
539 same-window-regexps)
540 (delete-other-windows)
541 (switch-to-buffer (other-buffer))
542 (pop-to-buffer buff)
543 (bury-buffer menu)))
544
545 (defun Buffer-menu-toggle-read-only ()
546 "Toggle read-only status of buffer on this line, perhaps via version control."
547 (interactive)
548 (let (char)
549 (with-current-buffer (Buffer-menu-buffer t)
550 (toggle-read-only)
551 (setq char (if buffer-read-only ?% ?\s)))
552 (save-excursion
553 (beginning-of-line)
554 (forward-char 1)
555 (if (/= (following-char) char)
556 (let ((inhibit-read-only t))
557 (delete-char 1)
558 (insert char))))))
559
560 (defun Buffer-menu-bury ()
561 "Bury the buffer listed on this line."
562 (interactive)
563 (when (Buffer-menu-no-header)
564 (save-excursion
565 (beginning-of-line)
566 (bury-buffer (Buffer-menu-buffer t))
567 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
568 (inhibit-read-only t))
569 (delete-region (point) (progn (forward-line -1) (point)))
570 (goto-char (point-max))
571 (insert line))
572 (message "Buried buffer moved to the end"))))
573
574
575 (defun Buffer-menu-view ()
576 "View this line's buffer in View mode."
577 (interactive)
578 (view-buffer (Buffer-menu-buffer t)))
579
580
581 (defun Buffer-menu-view-other-window ()
582 "View this line's buffer in View mode in another window."
583 (interactive)
584 (view-buffer-other-window (Buffer-menu-buffer t)))
585 \f
586
587 (define-key ctl-x-map "\C-b" 'list-buffers)
588
589 (defun list-buffers (&optional files-only)
590 "Display a list of names of existing buffers.
591 The list is displayed in a buffer named `*Buffer List*'.
592 Note that buffers with names starting with spaces are omitted.
593 Non-null optional arg FILES-ONLY means mention only file buffers.
594
595 For more information, see the function `buffer-menu'."
596 (interactive "P")
597 (display-buffer (list-buffers-noselect files-only)))
598
599 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
600 (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width)
601 (setq name
602 (if (string-match "<[0-9]+>$" name)
603 (concat (substring name 0
604 (- Buffer-menu-buffer+size-width
605 (max (length size) 3)
606 (match-end 0)
607 (- (match-beginning 0))
608 2))
609 ":" ; narrow ellipsis
610 (match-string 0 name))
611 (concat (substring name 0
612 (- Buffer-menu-buffer+size-width
613 (max (length size) 3)
614 2))
615 ":"))) ; narrow ellipsis
616 ;; Don't put properties on (buffer-name).
617 (setq name (copy-sequence name)))
618 (add-text-properties 0 (length name) name-props name)
619 (add-text-properties 0 (length size) size-props size)
620 (concat name
621 (make-string (- Buffer-menu-buffer+size-width
622 (length name)
623 (length size))
624 ?\s)
625 size))
626
627 (defun Buffer-menu-sort (column)
628 "Sort the buffer menu by COLUMN."
629 (interactive "P")
630 (when column
631 (setq column (prefix-numeric-value column))
632 (if (< column 2) (setq column 2))
633 (if (> column 5) (setq column 5)))
634 (setq Buffer-menu-sort-column column)
635 (let ((inhibit-read-only t) l buf m1 m2)
636 (save-excursion
637 (Buffer-menu-beginning)
638 (while (not (eobp))
639 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
640 (setq m1 (char-after)
641 m1 (if (memq m1 '(?> ?D)) m1)
642 m2 (char-after (+ (point) 2))
643 m2 (if (eq m2 ?S) m2))
644 (if (or m1 m2)
645 (push (list buf m1 m2) l)))
646 (forward-line)))
647 (Buffer-menu-revert)
648 (save-excursion
649 (Buffer-menu-beginning)
650 (while (not (eobp))
651 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
652 (setq m1 (cadr buf)
653 m2 (cadr (cdr buf)))
654 (when m1
655 (delete-char 1)
656 (insert m1)
657 (backward-char 1))
658 (when m2
659 (forward-char 2)
660 (delete-char 1)
661 (insert m2)))
662 (forward-line)))))
663
664 (defun Buffer-menu-sort-by-column (&optional e)
665 "Sort the buffer menu by the column clicked on."
666 (interactive (list last-input-event))
667 (if e (mouse-select-window e))
668 (let* ((pos (event-start e))
669 (obj (posn-object pos))
670 (col (if obj
671 (get-text-property (cdr obj) 'column (car obj))
672 (get-text-property (posn-point pos) 'column))))
673 (Buffer-menu-sort col)))
674
675 (defvar Buffer-menu-sort-button-map
676 (let ((map (make-sparse-keymap)))
677 ;; This keymap handles both nil and non-nil values for
678 ;; Buffer-menu-use-header-line.
679 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
680 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
681 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
682 (define-key map [follow-link] 'mouse-face)
683 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
684 map)
685 "Local keymap for Buffer menu sort buttons.")
686
687 (defun Buffer-menu-make-sort-button (name column)
688 (if (equal column Buffer-menu-sort-column) (setq column nil))
689 (propertize name
690 'column column
691 'help-echo (concat
692 (if Buffer-menu-use-header-line
693 "mouse-1, mouse-2: sort by "
694 "mouse-2, RET: sort by ")
695 (if column (downcase name) "visited order"))
696 'mouse-face 'highlight
697 'keymap Buffer-menu-sort-button-map))
698
699 (defun list-buffers-noselect (&optional files-only buffer-list)
700 "Create and return a buffer with a list of names of existing buffers.
701 The buffer is named `*Buffer List*'.
702 Note that buffers with names starting with spaces are omitted.
703 Non-null optional arg FILES-ONLY means mention only file buffers.
704
705 If BUFFER-LIST is non-nil, it should be a list of buffers;
706 it means list those buffers and no others.
707
708 For more information, see the function `buffer-menu'."
709 (let* ((old-buffer (current-buffer))
710 (standard-output standard-output)
711 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
712 (header (concat "CRM "
713 (Buffer-menu-buffer+size
714 (Buffer-menu-make-sort-button "Buffer" 2)
715 (Buffer-menu-make-sort-button "Size" 3))
716 " "
717 (Buffer-menu-make-sort-button "Mode" 4) mode-end
718 (Buffer-menu-make-sort-button "File" 5) "\n"))
719 list desired-point)
720 (when Buffer-menu-use-header-line
721 (let ((pos 0))
722 ;; Turn whitespace chars in the header into stretch specs so
723 ;; they work regardless of the header-line face.
724 (while (string-match "[ \t\n]+" header pos)
725 (setq pos (match-end 0))
726 (put-text-property (match-beginning 0) pos 'display
727 ;; Assume fixed-size chars in the buffer.
728 (list 'space :align-to pos)
729 header)))
730 ;; Try to better align the one-char headers.
731 (put-text-property 0 3 'face 'fixed-pitch header)
732 ;; Add a "dummy" leading space to align the beginning of the header
733 ;; line with the beginning of the text (rather than with the left
734 ;; scrollbar or the left fringe). --Stef
735 (setq header (concat (propertize " " 'display '(space :align-to 0))
736 header)))
737 (with-current-buffer (get-buffer-create "*Buffer List*")
738 (setq buffer-read-only nil)
739 (erase-buffer)
740 (setq standard-output (current-buffer))
741 (unless Buffer-menu-use-header-line
742 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
743 ;; (i.e. U+002D, HYPHEN-MINUS).
744 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
745 (insert header
746 (apply 'string
747 (mapcar (lambda (c)
748 (if (memq c '(?\n ?\s)) c underline))
749 header)))))
750 ;; Collect info for every buffer we're interested in.
751 (dolist (buffer (or buffer-list
752 (buffer-list
753 (when Buffer-menu-use-frame-buffer-list
754 (selected-frame)))))
755 (with-current-buffer buffer
756 (let ((name (buffer-name))
757 (file buffer-file-name))
758 (unless (and (not buffer-list)
759 (or
760 ;; Don't mention internal buffers.
761 (and (string= (substring name 0 1) " ") (null file))
762 ;; Maybe don't mention buffers without files.
763 (and files-only (not file))
764 (string= name "*Buffer List*")))
765 ;; Otherwise output info.
766 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
767 (if mode-line-process
768 (format-mode-line mode-line-process
769 nil nil buffer))))
770 (bits (string
771 (if (eq buffer old-buffer) ?. ?\s)
772 ;; Handle readonly status. The output buffer
773 ;; is special cased to appear readonly; it is
774 ;; actually made so at a later date.
775 (if (or (eq buffer standard-output)
776 buffer-read-only)
777 ?% ?\s)
778 ;; Identify modified buffers.
779 (if (buffer-modified-p) ?* ?\s)
780 ;; Space separator.
781 ?\s)))
782 (unless file
783 ;; No visited file. Check local value of
784 ;; list-buffers-directory and, for Info buffers,
785 ;; Info-current-file.
786 (cond ((and (boundp 'list-buffers-directory)
787 list-buffers-directory)
788 (setq file list-buffers-directory))
789 ((eq major-mode 'Info-mode)
790 (setq file Info-current-file)
791 (cond
792 ((equal file "dir")
793 (setq file "*Info Directory*"))
794 ((eq file 'apropos)
795 (setq file "*Info Apropos*"))
796 ((eq file 'history)
797 (setq file "*Info History*"))
798 ((eq file 'toc)
799 (setq file "*Info TOC*"))
800 ((not (stringp file)) ;; avoid errors
801 (setq file nil))
802 (t
803 (setq file (concat "("
804 (file-name-nondirectory file)
805 ") "
806 Info-current-node)))))))
807 (push (list buffer bits name (buffer-size) mode file)
808 list))))))
809 ;; Preserve the original buffer-list ordering, just in case.
810 (setq list (nreverse list))
811 ;; Place the buffers's info in the output buffer, sorted if necessary.
812 (dolist (buffer
813 (if Buffer-menu-sort-column
814 (sort list
815 (if (eq Buffer-menu-sort-column 3)
816 (lambda (a b)
817 (< (nth Buffer-menu-sort-column a)
818 (nth Buffer-menu-sort-column b)))
819 (lambda (a b)
820 (string< (nth Buffer-menu-sort-column a)
821 (nth Buffer-menu-sort-column b)))))
822 list))
823 (if (eq (car buffer) old-buffer)
824 (setq desired-point (point)))
825 (insert (cadr buffer)
826 ;; Put the buffer name into a text property
827 ;; so we don't have to extract it from the text.
828 ;; This way we avoid problems with unusual buffer names.
829 (let ((name (nth 2 buffer))
830 (size (int-to-string (nth 3 buffer))))
831 (Buffer-menu-buffer+size name size
832 `(buffer-name ,name
833 buffer ,(car buffer)
834 font-lock-face buffer-menu-buffer
835 mouse-face highlight
836 help-echo
837 ,(if (>= (length name)
838 (- Buffer-menu-buffer+size-width
839 (max (length size) 3)
840 2))
841 name
842 "mouse-2: select this buffer"))))
843 " "
844 (if (> (length (nth 4 buffer)) Buffer-menu-mode-width)
845 (substring (nth 4 buffer) 0 Buffer-menu-mode-width)
846 (nth 4 buffer)))
847 (when (nth 5 buffer)
848 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
849 Buffer-menu-mode-width 4) 1)
850 (princ (abbreviate-file-name (nth 5 buffer))))
851 (princ "\n"))
852 (Buffer-menu-mode)
853 (when Buffer-menu-use-header-line
854 (setq header-line-format header))
855 ;; DESIRED-POINT doesn't have to be set; it is not when the
856 ;; current buffer is not displayed for some reason.
857 (and desired-point
858 (goto-char desired-point))
859 (setq Buffer-menu-files-only files-only)
860 (set-buffer-modified-p nil)
861 (current-buffer))))
862
863 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
864 ;;; buff-menu.el ends here