(cal-menu-context-mouse-menu): Add some :keys.
[emacs.git] / lisp / buff-menu.el
blob7ebf13c0976ed75f60ba2faa2804056f12fef3ba
1 ;;; buff-menu.el --- buffer menu main function and support functions -*- coding:utf-8 -*-
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: convenience
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
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.
32 ;;; Change Log:
34 ;; Buffer-menu-view: New function
35 ;; Buffer-menu-view-other-window: New function
37 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
39 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
41 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
42 ;; current entry and then move to previous one.
44 ;; Based on FSF code dating back to 1985.
46 ;;; Code:
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.
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.
59 ;;(defvar Buffer-menu-window-config nil
60 ;; "Window configuration saved from entry to `buffer-menu'.")
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.
65 (defgroup Buffer-menu nil
66 "Show a menu of all buffers in a buffer."
67 :group 'tools
68 :group 'convenience)
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)
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)
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)
86 (defcustom Buffer-menu-mode-width 16
87 "*How wide to make the mode name column."
88 :type 'number
89 :group 'Buffer-menu)
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")
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).")
107 (defconst Buffer-menu-buffer-column 4)
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.")
115 (make-variable-buffer-local 'Buffer-menu-files-only)
117 (defvar Info-current-file) ;; from info.el
118 (defvar Info-current-node) ;; from info.el
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 map)
155 "Local keymap for `Buffer-menu-mode' buffers.")
157 ;; Buffer Menu mode is suitable only for specially formatted data.
158 (put 'Buffer-menu-mode 'mode-class 'special)
160 (define-derived-mode Buffer-menu-mode nil "Buffer Menu"
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 (set (make-local-variable 'revert-buffer-function)
193 'Buffer-menu-revert-function)
194 (set (make-local-variable 'buffer-stale-function)
195 #'(lambda (&optional noconfirm) 'fast))
196 (setq truncate-lines t)
197 (setq buffer-read-only t))
199 ;; This function exists so we can make the doc string of Buffer-menu-mode
200 ;; look nice.
201 (defun Buffer-menu-revert ()
202 "Update the list of buffers."
203 (interactive)
204 (revert-buffer))
206 (defun Buffer-menu-revert-function (ignore1 ignore2)
207 (or (eq buffer-undo-list t)
208 (setq buffer-undo-list nil))
209 ;; We can not use save-excursion here. The buffer gets erased.
210 (let ((opoint (point))
211 (eobp (eobp))
212 (ocol (current-column))
213 (oline (progn (move-to-column 4)
214 (get-text-property (point) 'buffer)))
215 (prop (point-min))
216 ;; do not make undo records for the reversion.
217 (buffer-undo-list t))
218 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
219 ;; temporarily the current buffer. Make sure that the
220 ;; interactively current buffer is correctly identified with a `.'
221 ;; by `list-buffers-noselect'.
222 (with-current-buffer (window-buffer)
223 (list-buffers-noselect Buffer-menu-files-only))
224 (if oline
225 (while (setq prop (next-single-property-change prop 'buffer))
226 (when (eq (get-text-property prop 'buffer) oline)
227 (goto-char prop)
228 (move-to-column ocol)))
229 (goto-char (if eobp (point-max) opoint)))))
231 (defun Buffer-menu-toggle-files-only (arg)
232 "Toggle whether the current buffer-menu displays only file buffers.
233 With a positive ARG display only file buffers. With zero or
234 negative ARG, display other buffers as well."
235 (interactive "P")
236 (setq Buffer-menu-files-only
237 (cond ((not arg) (not Buffer-menu-files-only))
238 ((> (prefix-numeric-value arg) 0) t)))
239 (revert-buffer))
242 (defun Buffer-menu-buffer (error-if-non-existent-p)
243 "Return buffer described by this line of buffer menu."
244 (let* ((where (save-excursion
245 (beginning-of-line)
246 (+ (point) Buffer-menu-buffer-column)))
247 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
248 (buf (and (not (eobp)) (get-text-property where 'buffer))))
249 (if name
250 (or (get-buffer name)
251 (and buf (buffer-name buf) buf)
252 (if error-if-non-existent-p
253 (error "No buffer named `%s'" name)
254 nil))
255 (or (and buf (buffer-name buf) buf)
256 (if error-if-non-existent-p
257 (error "No buffer on this line")
258 nil)))))
260 (defun buffer-menu (&optional arg)
261 "Make a menu of buffers so you can save, delete or select them.
262 With argument, show only buffers that are visiting files.
263 Type ? after invocation to get help on commands available.
264 Type q to remove the buffer menu from the display.
266 The first column shows `>' for a buffer you have
267 marked to be displayed, `D' for one you have marked for
268 deletion, and `.' for the current buffer.
270 The C column has a `.' for the buffer from which you came.
271 The R column has a `%' if the buffer is read-only.
272 The M column has a `*' if it is modified,
273 or `S' if you have marked it for saving.
274 After this come the buffer name, its size in characters,
275 its major mode, and the visited file name (if any)."
276 (interactive "P")
277 ;;; (setq Buffer-menu-window-config (current-window-configuration))
278 (switch-to-buffer (list-buffers-noselect arg))
279 (message
280 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
282 (defun buffer-menu-other-window (&optional arg)
283 "Display a list of buffers in another window.
284 With the buffer list buffer, you can save, delete or select the buffers.
285 With argument, show only buffers that are visiting files.
286 Type ? after invocation to get help on commands available.
287 Type q to remove the buffer menu from the display.
288 For more information, see the function `buffer-menu'."
289 (interactive "P")
290 ;;; (setq Buffer-menu-window-config (current-window-configuration))
291 (switch-to-buffer-other-window (list-buffers-noselect arg))
292 (message
293 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
295 (defun Buffer-menu-no-header ()
296 (beginning-of-line)
297 (if (or Buffer-menu-use-header-line
298 (not (eq (char-after) ?C)))
300 (ding)
301 (forward-line 1)
302 nil))
304 (defun Buffer-menu-mark ()
305 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
306 (interactive)
307 (when (Buffer-menu-no-header)
308 (let ((inhibit-read-only t))
309 (delete-char 1)
310 (insert ?>)
311 (forward-line 1))))
313 (defun Buffer-menu-unmark (&optional backup)
314 "Cancel all requested operations on buffer on this line and move down.
315 Optional prefix arg means move up."
316 (interactive "P")
317 (when (Buffer-menu-no-header)
318 (let* ((buf (Buffer-menu-buffer t))
319 (mod (buffer-modified-p buf))
320 (readonly (with-current-buffer buf buffer-read-only))
321 (inhibit-read-only t))
322 (delete-char 3)
323 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
324 (forward-line (if backup -1 1)))
326 (defun Buffer-menu-backup-unmark ()
327 "Move up and cancel all requested operations on buffer on line above."
328 (interactive)
329 (forward-line -1)
330 (Buffer-menu-unmark)
331 (forward-line -1))
333 (defun Buffer-menu-delete (&optional arg)
334 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
335 Prefix arg is how many buffers to delete.
336 Negative arg means delete backwards."
337 (interactive "p")
338 (when (Buffer-menu-no-header)
339 (let ((inhibit-read-only t))
340 (if (or (null arg) (= arg 0))
341 (setq arg 1))
342 (while (> arg 0)
343 (delete-char 1)
344 (insert ?D)
345 (forward-line 1)
346 (setq arg (1- arg)))
347 (while (and (< arg 0)
348 (Buffer-menu-no-header))
349 (delete-char 1)
350 (insert ?D)
351 (forward-line -1)
352 (setq arg (1+ arg))))))
354 (defun Buffer-menu-delete-backwards (&optional arg)
355 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
356 and then move up one line. Prefix arg means move that many lines."
357 (interactive "p")
358 (Buffer-menu-delete (- (or arg 1))))
360 (defun Buffer-menu-save ()
361 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
362 (interactive)
363 (when (Buffer-menu-no-header)
364 (let ((inhibit-read-only t))
365 (forward-char 2)
366 (delete-char 1)
367 (insert ?S)
368 (forward-line 1))))
370 (defun Buffer-menu-not-modified (&optional arg)
371 "Mark buffer on this line as unmodified (no changes to save)."
372 (interactive "P")
373 (with-current-buffer (Buffer-menu-buffer t)
374 (set-buffer-modified-p arg))
375 (save-excursion
376 (beginning-of-line)
377 (forward-char 2)
378 (if (= (char-after) (if arg ?\s ?*))
379 (let ((inhibit-read-only t))
380 (delete-char 1)
381 (insert (if arg ?* ?\s))))))
383 (defun Buffer-menu-beginning ()
384 (goto-char (point-min))
385 (unless Buffer-menu-use-header-line
386 (forward-line)))
388 (defun Buffer-menu-execute ()
389 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
390 (interactive)
391 (save-excursion
392 (Buffer-menu-beginning)
393 (while (re-search-forward "^..S" nil t)
394 (let ((modp nil))
395 (with-current-buffer (Buffer-menu-buffer t)
396 (save-buffer)
397 (setq modp (buffer-modified-p)))
398 (let ((inhibit-read-only t))
399 (delete-char -1)
400 (insert (if modp ?* ?\s))))))
401 (save-excursion
402 (Buffer-menu-beginning)
403 (let ((buff-menu-buffer (current-buffer))
404 (inhibit-read-only t))
405 (while (re-search-forward "^D" nil t)
406 (forward-char -1)
407 (let ((buf (Buffer-menu-buffer nil)))
408 (or (eq buf nil)
409 (eq buf buff-menu-buffer)
410 (save-excursion (kill-buffer buf)))
411 (if (and buf (buffer-name buf))
412 (progn (delete-char 1)
413 (insert ?\s))
414 (delete-region (point) (progn (forward-line 1) (point)))
415 (unless (bobp)
416 (forward-char -1))))))))
418 (defun Buffer-menu-select ()
419 "Select this line's buffer; also display buffers marked with `>'.
420 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
421 This command deletes and replaces all the previously existing windows
422 in the selected frame."
423 (interactive)
424 (let ((buff (Buffer-menu-buffer t))
425 (menu (current-buffer))
426 (others ())
427 tem)
428 (Buffer-menu-beginning)
429 (while (re-search-forward "^>" nil t)
430 (setq tem (Buffer-menu-buffer t))
431 (let ((inhibit-read-only t))
432 (delete-char -1)
433 (insert ?\s))
434 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
435 (setq others (nreverse others)
436 tem (/ (1- (frame-height)) (1+ (length others))))
437 (delete-other-windows)
438 (switch-to-buffer buff)
439 (or (eq menu buff)
440 (bury-buffer menu))
441 (if (equal (length others) 0)
442 (progn
443 ;;; ;; Restore previous window configuration before displaying
444 ;;; ;; selected buffers.
445 ;;; (if Buffer-menu-window-config
446 ;;; (progn
447 ;;; (set-window-configuration Buffer-menu-window-config)
448 ;;; (setq Buffer-menu-window-config nil)))
449 (switch-to-buffer buff))
450 (while others
451 (split-window nil tem)
452 (other-window 1)
453 (switch-to-buffer (car others))
454 (setq others (cdr others)))
455 (other-window 1) ;back to the beginning!
460 (defun Buffer-menu-visit-tags-table ()
461 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
462 (interactive)
463 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
464 (if file
465 (visit-tags-table file)
466 (error "Specified buffer has no file"))))
468 (defun Buffer-menu-1-window ()
469 "Select this line's buffer, alone, in full frame."
470 (interactive)
471 (switch-to-buffer (Buffer-menu-buffer t))
472 (bury-buffer (other-buffer))
473 (delete-other-windows))
475 (defun Buffer-menu-mouse-select (event)
476 "Select the buffer whose line you click on."
477 (interactive "e")
478 (let (buffer)
479 (with-current-buffer (window-buffer (posn-window (event-end event)))
480 (save-excursion
481 (goto-char (posn-point (event-end event)))
482 (setq buffer (Buffer-menu-buffer t))))
483 (select-window (posn-window (event-end event)))
484 (if (and (window-dedicated-p (selected-window))
485 (eq (selected-window) (frame-root-window)))
486 (switch-to-buffer-other-frame buffer)
487 (switch-to-buffer buffer))))
489 (defun Buffer-menu-this-window ()
490 "Select this line's buffer in this window."
491 (interactive)
492 (switch-to-buffer (Buffer-menu-buffer t)))
494 (defun Buffer-menu-other-window ()
495 "Select this line's buffer in other window, leaving buffer menu visible."
496 (interactive)
497 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
499 (defun Buffer-menu-switch-other-window ()
500 "Make the other window select this line's buffer.
501 The current window remains selected."
502 (interactive)
503 (let ((pop-up-windows t)
504 same-window-buffer-names
505 same-window-regexps)
506 (display-buffer (Buffer-menu-buffer t))))
508 (defun Buffer-menu-2-window ()
509 "Select this line's buffer, with previous buffer in second window."
510 (interactive)
511 (let ((buff (Buffer-menu-buffer t))
512 (menu (current-buffer))
513 (pop-up-windows t)
514 same-window-buffer-names
515 same-window-regexps)
516 (delete-other-windows)
517 (switch-to-buffer (other-buffer))
518 (pop-to-buffer buff)
519 (bury-buffer menu)))
521 (defun Buffer-menu-toggle-read-only ()
522 "Toggle read-only status of buffer on this line, perhaps via version control."
523 (interactive)
524 (let (char)
525 (with-current-buffer (Buffer-menu-buffer t)
526 (toggle-read-only)
527 (setq char (if buffer-read-only ?% ?\s)))
528 (save-excursion
529 (beginning-of-line)
530 (forward-char 1)
531 (if (/= (following-char) char)
532 (let ((inhibit-read-only t))
533 (delete-char 1)
534 (insert char))))))
536 (defun Buffer-menu-bury ()
537 "Bury the buffer listed on this line."
538 (interactive)
539 (when (Buffer-menu-no-header)
540 (save-excursion
541 (beginning-of-line)
542 (bury-buffer (Buffer-menu-buffer t))
543 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
544 (inhibit-read-only t))
545 (delete-region (point) (progn (forward-line -1) (point)))
546 (goto-char (point-max))
547 (insert line))
548 (message "Buried buffer moved to the end"))))
551 (defun Buffer-menu-view ()
552 "View this line's buffer in View mode."
553 (interactive)
554 (view-buffer (Buffer-menu-buffer t)))
557 (defun Buffer-menu-view-other-window ()
558 "View this line's buffer in View mode in another window."
559 (interactive)
560 (view-buffer-other-window (Buffer-menu-buffer t)))
563 (define-key ctl-x-map "\C-b" 'list-buffers)
565 (defun list-buffers (&optional files-only)
566 "Display a list of names of existing buffers.
567 The list is displayed in a buffer named `*Buffer List*'.
568 Note that buffers with names starting with spaces are omitted.
569 Non-null optional arg FILES-ONLY means mention only file buffers.
571 For more information, see the function `buffer-menu'."
572 (interactive "P")
573 (display-buffer (list-buffers-noselect files-only)))
575 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
576 (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width)
577 (setq name
578 (if (string-match "<[0-9]+>$" name)
579 (concat (substring name 0
580 (- Buffer-menu-buffer+size-width
581 (max (length size) 3)
582 (match-end 0)
583 (- (match-beginning 0))
585 ":" ; narrow ellipsis
586 (match-string 0 name))
587 (concat (substring name 0
588 (- Buffer-menu-buffer+size-width
589 (max (length size) 3)
591 ":"))) ; narrow ellipsis
592 ;; Don't put properties on (buffer-name).
593 (setq name (copy-sequence name)))
594 (add-text-properties 0 (length name) name-props name)
595 (add-text-properties 0 (length size) size-props size)
596 (concat name
597 (make-string (- Buffer-menu-buffer+size-width
598 (length name)
599 (length size))
600 ?\s)
601 size))
603 (defun Buffer-menu-sort (column)
604 "Sort the buffer menu by COLUMN."
605 (interactive "P")
606 (when column
607 (setq column (prefix-numeric-value column))
608 (if (< column 2) (setq column 2))
609 (if (> column 5) (setq column 5)))
610 (setq Buffer-menu-sort-column column)
611 (let ((inhibit-read-only t) l buf m1 m2)
612 (save-excursion
613 (Buffer-menu-beginning)
614 (while (not (eobp))
615 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
616 (setq m1 (char-after)
617 m1 (if (memq m1 '(?> ?D)) m1)
618 m2 (char-after (+ (point) 2))
619 m2 (if (eq m2 ?S) m2))
620 (if (or m1 m2)
621 (push (list buf m1 m2) l)))
622 (forward-line)))
623 (Buffer-menu-revert)
624 (save-excursion
625 (Buffer-menu-beginning)
626 (while (not (eobp))
627 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
628 (setq m1 (cadr buf)
629 m2 (cadr (cdr buf)))
630 (when m1
631 (delete-char 1)
632 (insert m1)
633 (backward-char 1))
634 (when m2
635 (forward-char 2)
636 (delete-char 1)
637 (insert m2)))
638 (forward-line)))))
640 (defun Buffer-menu-sort-by-column (&optional e)
641 "Sort the buffer menu by the column clicked on."
642 (interactive (list last-input-event))
643 (if e (mouse-select-window e))
644 (let* ((pos (event-start e))
645 (obj (posn-object pos))
646 (col (if obj
647 (get-text-property (cdr obj) 'column (car obj))
648 (get-text-property (posn-point pos) 'column))))
649 (Buffer-menu-sort col)))
651 (defvar Buffer-menu-sort-button-map
652 (let ((map (make-sparse-keymap)))
653 ;; This keymap handles both nil and non-nil values for
654 ;; Buffer-menu-use-header-line.
655 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
656 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
657 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
658 (define-key map [follow-link] 'mouse-face)
659 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
660 map)
661 "Local keymap for Buffer menu sort buttons.")
663 (defun Buffer-menu-make-sort-button (name column)
664 (if (equal column Buffer-menu-sort-column) (setq column nil))
665 (propertize name
666 'column column
667 'help-echo (concat
668 (if Buffer-menu-use-header-line
669 "mouse-1, mouse-2: sort by "
670 "mouse-2, RET: sort by ")
671 (if column (downcase name) "visited order"))
672 'mouse-face 'highlight
673 'keymap Buffer-menu-sort-button-map))
675 (defun list-buffers-noselect (&optional files-only buffer-list)
676 "Create and return a buffer with a list of names of existing buffers.
677 The buffer is named `*Buffer List*'.
678 Note that buffers with names starting with spaces are omitted.
679 Non-null optional arg FILES-ONLY means mention only file buffers.
681 If BUFFER-LIST is non-nil, it should be a list of buffers;
682 it means list those buffers and no others.
684 For more information, see the function `buffer-menu'."
685 (let* ((old-buffer (current-buffer))
686 (standard-output standard-output)
687 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
688 (header (concat "CRM "
689 (Buffer-menu-buffer+size
690 (Buffer-menu-make-sort-button "Buffer" 2)
691 (Buffer-menu-make-sort-button "Size" 3))
693 (Buffer-menu-make-sort-button "Mode" 4) mode-end
694 (Buffer-menu-make-sort-button "File" 5) "\n"))
695 list desired-point)
696 (when Buffer-menu-use-header-line
697 (let ((pos 0))
698 ;; Turn whitespace chars in the header into stretch specs so
699 ;; they work regardless of the header-line face.
700 (while (string-match "[ \t\n]+" header pos)
701 (setq pos (match-end 0))
702 (put-text-property (match-beginning 0) pos 'display
703 ;; Assume fixed-size chars in the buffer.
704 (list 'space :align-to pos)
705 header)))
706 ;; Try to better align the one-char headers.
707 (put-text-property 0 3 'face 'fixed-pitch header)
708 ;; Add a "dummy" leading space to align the beginning of the header
709 ;; line with the beginning of the text (rather than with the left
710 ;; scrollbar or the left fringe). --Stef
711 (setq header (concat (propertize " " 'display '(space :align-to 0))
712 header)))
713 (with-current-buffer (get-buffer-create "*Buffer List*")
714 (setq buffer-read-only nil)
715 (erase-buffer)
716 (setq standard-output (current-buffer))
717 (unless Buffer-menu-use-header-line
718 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
719 ;; (i.e. U+002D, HYPHEN-MINUS).
720 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
721 (insert header
722 (apply 'string
723 (mapcar (lambda (c)
724 (if (memq c '(?\n ?\s)) c underline))
725 header)))))
726 ;; Collect info for every buffer we're interested in.
727 (dolist (buffer (or buffer-list
728 (buffer-list
729 (when Buffer-menu-use-frame-buffer-list
730 (selected-frame)))))
731 (with-current-buffer buffer
732 (let ((name (buffer-name))
733 (file buffer-file-name))
734 (unless (and (not buffer-list)
736 ;; Don't mention internal buffers.
737 (and (string= (substring name 0 1) " ") (null file))
738 ;; Maybe don't mention buffers without files.
739 (and files-only (not file))
740 (string= name "*Buffer List*")))
741 ;; Otherwise output info.
742 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
743 (if mode-line-process
744 (format-mode-line mode-line-process
745 nil nil buffer))))
746 (bits (string
747 (if (eq buffer old-buffer) ?. ?\s)
748 ;; Handle readonly status. The output buffer
749 ;; is special cased to appear readonly; it is
750 ;; actually made so at a later date.
751 (if (or (eq buffer standard-output)
752 buffer-read-only)
753 ?% ?\s)
754 ;; Identify modified buffers.
755 (if (buffer-modified-p) ?* ?\s)
756 ;; Space separator.
757 ?\s)))
758 (unless file
759 ;; No visited file. Check local value of
760 ;; list-buffers-directory and, for Info buffers,
761 ;; Info-current-file.
762 (cond ((and (boundp 'list-buffers-directory)
763 list-buffers-directory)
764 (setq file list-buffers-directory))
765 ((eq major-mode 'Info-mode)
766 (setq file Info-current-file)
767 (cond
768 ((equal file "dir")
769 (setq file "*Info Directory*"))
770 ((eq file 'apropos)
771 (setq file "*Info Apropos*"))
772 ((eq file 'history)
773 (setq file "*Info History*"))
774 ((eq file 'toc)
775 (setq file "*Info TOC*"))
776 ((not (stringp file)) ;; avoid errors
777 (setq file nil))
779 (setq file (concat "("
780 (file-name-nondirectory file)
782 Info-current-node)))))))
783 (push (list buffer bits name (buffer-size) mode file)
784 list))))))
785 ;; Preserve the original buffer-list ordering, just in case.
786 (setq list (nreverse list))
787 ;; Place the buffers's info in the output buffer, sorted if necessary.
788 (dolist (buffer
789 (if Buffer-menu-sort-column
790 (sort list
791 (if (eq Buffer-menu-sort-column 3)
792 (lambda (a b)
793 (< (nth Buffer-menu-sort-column a)
794 (nth Buffer-menu-sort-column b)))
795 (lambda (a b)
796 (string< (nth Buffer-menu-sort-column a)
797 (nth Buffer-menu-sort-column b)))))
798 list))
799 (if (eq (car buffer) old-buffer)
800 (setq desired-point (point)))
801 (insert (cadr buffer)
802 ;; Put the buffer name into a text property
803 ;; so we don't have to extract it from the text.
804 ;; This way we avoid problems with unusual buffer names.
805 (let ((name (nth 2 buffer))
806 (size (int-to-string (nth 3 buffer))))
807 (Buffer-menu-buffer+size name size
808 `(buffer-name ,name
809 buffer ,(car buffer)
810 font-lock-face buffer-menu-buffer
811 mouse-face highlight
812 help-echo
813 ,(if (>= (length name)
814 (- Buffer-menu-buffer+size-width
815 (max (length size) 3)
817 name
818 "mouse-2: select this buffer"))))
820 (if (> (length (nth 4 buffer)) Buffer-menu-mode-width)
821 (substring (nth 4 buffer) 0 Buffer-menu-mode-width)
822 (nth 4 buffer)))
823 (when (nth 5 buffer)
824 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
825 Buffer-menu-mode-width 4) 1)
826 (princ (abbreviate-file-name (nth 5 buffer))))
827 (princ "\n"))
828 (Buffer-menu-mode)
829 (when Buffer-menu-use-header-line
830 (setq header-line-format header))
831 ;; DESIRED-POINT doesn't have to be set; it is not when the
832 ;; current buffer is not displayed for some reason.
833 (and desired-point
834 (goto-char desired-point))
835 (setq Buffer-menu-files-only files-only)
836 (set-buffer-modified-p nil)
837 (current-buffer))))
839 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
840 ;;; buff-menu.el ends here