Fix previous change.
[emacs.git] / lisp / buff-menu.el
blobf90cf884c1d89f24088bef918b6d5a3e396405a4
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, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Edit, delete, or change attributes of all currently active Emacs
29 ;; buffers from a list summarizing their state. A good way to browse
30 ;; any special or scratch buffers you have loaded, since you can't find
31 ;; them by filename. The single entry point is `list-buffers',
32 ;; normally bound to C-x C-b.
34 ;;; Change Log:
36 ;; Buffer-menu-view: New function
37 ;; Buffer-menu-view-other-window: New function
39 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
41 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
43 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
44 ;; current entry and then move to previous one.
46 ;; Based on FSF code dating back to 1985.
48 ;;; Code:
50 ;;Trying to preserve the old window configuration works well in
51 ;;simple scenarios, when you enter the buffer menu, use it, and exit it.
52 ;;But it does strange things when you switch back to the buffer list buffer
53 ;;with C-x b, later on, when the window configuration is different.
54 ;;The choice seems to be, either restore the window configuration
55 ;;in all cases, or in no cases.
56 ;;I decided it was better not to restore the window config at all. -- rms.
58 ;;But since then, I changed buffer-menu to use the selected window,
59 ;;so q now once again goes back to the previous window configuration.
61 ;;(defvar Buffer-menu-window-config nil
62 ;; "Window configuration saved from entry to `buffer-menu'.")
64 ;; Put buffer *Buffer List* into proper mode right away
65 ;; so that from now on even list-buffers is enough to get a buffer menu.
67 (defgroup Buffer-menu nil
68 "Show a menu of all buffers in a buffer."
69 :group 'tools
70 :group 'convenience)
72 (defcustom Buffer-menu-use-header-line t
73 "*Non-nil means to use an immovable header-line."
74 :type 'boolean
75 :group 'Buffer-menu)
77 (defface buffer-menu-buffer
78 '((t (:weight bold)))
79 "Face used to highlight buffer names in the buffer menu."
80 :group 'Buffer-menu)
81 (put 'Buffer-menu-buffer 'face-alias 'buffer-menu-buffer)
83 (defcustom Buffer-menu-buffer+size-width 26
84 "*How wide to jointly make the buffer name and size columns."
85 :type 'number
86 :group 'Buffer-menu)
88 (defcustom Buffer-menu-mode-width 16
89 "*How wide to make the mode name column."
90 :type 'number
91 :group 'Buffer-menu)
93 (defcustom Buffer-menu-use-frame-buffer-list t
94 "If non-nil, the Buffer Menu uses the selected frame's buffer list.
95 Buffers that were never selected in that frame are listed at the end.
96 If the value is nil, the Buffer Menu uses the global buffer list.
97 This variable matters if the Buffer Menu is sorted by visited order,
98 as it is by default."
99 :type 'boolean
100 :group 'Buffer-menu
101 :version "22.1")
103 ;; This should get updated & resorted when you click on a column heading
104 (defvar Buffer-menu-sort-column nil
105 "Which column to sort the menu on.
106 Use 2 to sort by buffer names, or 5 to sort by file names.
107 A nil value means sort by visited order (the default).")
109 (defconst Buffer-menu-buffer-column 4)
111 (defvar Buffer-menu-files-only nil
112 "Non-nil if the current buffer-menu lists only file buffers.
113 This variable determines whether reverting the buffer lists only
114 file buffers. It affects both manual reverting and reverting by
115 Auto Revert Mode.")
117 (make-variable-buffer-local 'Buffer-menu-files-only)
119 (defvar Info-current-file) ;; from info.el
120 (defvar Info-current-node) ;; from info.el
122 (defvar Buffer-menu-mode-map
123 (let ((map (make-keymap)))
124 (suppress-keymap map t)
125 (define-key map "q" 'quit-window)
126 (define-key map "v" 'Buffer-menu-select)
127 (define-key map "2" 'Buffer-menu-2-window)
128 (define-key map "1" 'Buffer-menu-1-window)
129 (define-key map "f" 'Buffer-menu-this-window)
130 (define-key map "e" 'Buffer-menu-this-window)
131 (define-key map "\C-m" 'Buffer-menu-this-window)
132 (define-key map "o" 'Buffer-menu-other-window)
133 (define-key map "\C-o" 'Buffer-menu-switch-other-window)
134 (define-key map "s" 'Buffer-menu-save)
135 (define-key map "d" 'Buffer-menu-delete)
136 (define-key map "k" 'Buffer-menu-delete)
137 (define-key map "\C-d" 'Buffer-menu-delete-backwards)
138 (define-key map "\C-k" 'Buffer-menu-delete)
139 (define-key map "x" 'Buffer-menu-execute)
140 (define-key map " " 'next-line)
141 (define-key map "n" 'next-line)
142 (define-key map "p" 'previous-line)
143 (define-key map "\177" 'Buffer-menu-backup-unmark)
144 (define-key map "~" 'Buffer-menu-not-modified)
145 (define-key map "?" 'describe-mode)
146 (define-key map "u" 'Buffer-menu-unmark)
147 (define-key map "m" 'Buffer-menu-mark)
148 (define-key map "t" 'Buffer-menu-visit-tags-table)
149 (define-key map "%" 'Buffer-menu-toggle-read-only)
150 (define-key map "b" 'Buffer-menu-bury)
151 (define-key map "g" 'Buffer-menu-revert)
152 (define-key map "V" 'Buffer-menu-view)
153 (define-key map "T" 'Buffer-menu-toggle-files-only)
154 (define-key map [mouse-2] 'Buffer-menu-mouse-select)
155 (define-key map [follow-link] 'mouse-face)
156 map)
157 "Local keymap for `Buffer-menu-mode' buffers.")
159 ;; Buffer Menu mode is suitable only for specially formatted data.
160 (put 'Buffer-menu-mode 'mode-class 'special)
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-visit-tags-table] -- visit-tags-table this buffer.
182 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
183 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
184 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
185 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
186 \\[Buffer-menu-execute] -- delete or save marked buffers.
187 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
188 With prefix argument, also move up one line.
189 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
190 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
191 \\[Buffer-menu-revert] -- update the list of buffers.
192 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
193 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
194 (set (make-local-variable 'revert-buffer-function)
195 'Buffer-menu-revert-function)
196 (set (make-local-variable 'buffer-stale-function)
197 #'(lambda (&optional noconfirm) 'fast))
198 (setq truncate-lines t)
199 (setq buffer-read-only t))
201 ;; This function exists so we can make the doc string of Buffer-menu-mode
202 ;; look nice.
203 (defun Buffer-menu-revert ()
204 "Update the list of buffers."
205 (interactive)
206 (revert-buffer))
208 (defun Buffer-menu-revert-function (ignore1 ignore2)
209 (or (eq buffer-undo-list t)
210 (setq buffer-undo-list nil))
211 ;; We can not use save-excursion here. The buffer gets erased.
212 (let ((opoint (point))
213 (eobp (eobp))
214 (ocol (current-column))
215 (oline (progn (move-to-column 4)
216 (get-text-property (point) 'buffer)))
217 (prop (point-min))
218 ;; do not make undo records for the reversion.
219 (buffer-undo-list t))
220 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
221 ;; temporarily the current buffer. Make sure that the
222 ;; interactively current buffer is correctly identified with a `.'
223 ;; by `list-buffers-noselect'.
224 (with-current-buffer (window-buffer)
225 (list-buffers-noselect Buffer-menu-files-only))
226 (if oline
227 (while (setq prop (next-single-property-change prop 'buffer))
228 (when (eq (get-text-property prop 'buffer) oline)
229 (goto-char prop)
230 (move-to-column ocol)))
231 (goto-char (if eobp (point-max) opoint)))))
233 (defun Buffer-menu-toggle-files-only (arg)
234 "Toggle whether the current buffer-menu displays only file buffers.
235 With a positive ARG display only file buffers. With zero or
236 negative ARG, display other buffers as well."
237 (interactive "P")
238 (setq Buffer-menu-files-only
239 (cond ((not arg) (not Buffer-menu-files-only))
240 ((> (prefix-numeric-value arg) 0) t)))
241 (revert-buffer))
244 (defun Buffer-menu-buffer (error-if-non-existent-p)
245 "Return buffer described by this line of buffer menu."
246 (let* ((where (save-excursion
247 (beginning-of-line)
248 (+ (point) Buffer-menu-buffer-column)))
249 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
250 (buf (and (not (eobp)) (get-text-property where 'buffer))))
251 (if name
252 (or (get-buffer name)
253 (and buf (buffer-name buf) buf)
254 (if error-if-non-existent-p
255 (error "No buffer named `%s'" name)
256 nil))
257 (or (and buf (buffer-name buf) buf)
258 (if error-if-non-existent-p
259 (error "No buffer on this line")
260 nil)))))
262 (defun buffer-menu (&optional arg)
263 "Make a menu of buffers so you can save, delete or select them.
264 With argument, show only buffers that are visiting files.
265 Type ? after invocation to get help on commands available.
266 Type q to remove the buffer menu from the display.
268 The first column shows `>' for a buffer you have
269 marked to be displayed, `D' for one you have marked for
270 deletion, and `.' for the current buffer.
272 The C column has a `.' for the buffer from which you came.
273 The R column has a `%' if the buffer is read-only.
274 The M column has a `*' if it is modified,
275 or `S' if you have marked it for saving.
276 After this come the buffer name, its size in characters,
277 its major mode, and the visited file name (if any)."
278 (interactive "P")
279 ;;; (setq Buffer-menu-window-config (current-window-configuration))
280 (switch-to-buffer (list-buffers-noselect arg))
281 (message
282 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
284 (defun buffer-menu-other-window (&optional arg)
285 "Display a list of buffers in another window.
286 With the buffer list buffer, you can save, delete or select the buffers.
287 With argument, show only buffers that are visiting files.
288 Type ? after invocation to get help on commands available.
289 Type q to remove the buffer menu from the display.
290 For more information, see the function `buffer-menu'."
291 (interactive "P")
292 ;;; (setq Buffer-menu-window-config (current-window-configuration))
293 (switch-to-buffer-other-window (list-buffers-noselect arg))
294 (message
295 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
297 (defun Buffer-menu-no-header ()
298 (beginning-of-line)
299 (if (or Buffer-menu-use-header-line
300 (not (eq (char-after) ?C)))
302 (ding)
303 (forward-line 1)
304 nil))
306 (defun Buffer-menu-mark ()
307 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
308 (interactive)
309 (when (Buffer-menu-no-header)
310 (let ((buffer-read-only nil))
311 (delete-char 1)
312 (insert ?>)
313 (forward-line 1))))
315 (defun Buffer-menu-unmark (&optional backup)
316 "Cancel all requested operations on buffer on this line and move down.
317 Optional prefix arg means move up."
318 (interactive "P")
319 (when (Buffer-menu-no-header)
320 (let* ((buf (Buffer-menu-buffer t))
321 (mod (buffer-modified-p buf))
322 (readonly (save-excursion (set-buffer buf) buffer-read-only))
323 (buffer-read-only nil))
324 (delete-char 3)
325 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
326 (forward-line (if backup -1 1)))
328 (defun Buffer-menu-backup-unmark ()
329 "Move up and cancel all requested operations on buffer on line above."
330 (interactive)
331 (forward-line -1)
332 (Buffer-menu-unmark)
333 (forward-line -1))
335 (defun Buffer-menu-delete (&optional arg)
336 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
337 Prefix arg is how many buffers to delete.
338 Negative arg means delete backwards."
339 (interactive "p")
340 (when (Buffer-menu-no-header)
341 (let ((buffer-read-only nil))
342 (if (or (null arg) (= arg 0))
343 (setq arg 1))
344 (while (> arg 0)
345 (delete-char 1)
346 (insert ?D)
347 (forward-line 1)
348 (setq arg (1- arg)))
349 (while (and (< arg 0)
350 (Buffer-menu-no-header))
351 (delete-char 1)
352 (insert ?D)
353 (forward-line -1)
354 (setq arg (1+ arg))))))
356 (defun Buffer-menu-delete-backwards (&optional arg)
357 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
358 and then move up one line. Prefix arg means move that many lines."
359 (interactive "p")
360 (Buffer-menu-delete (- (or arg 1))))
362 (defun Buffer-menu-save ()
363 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
364 (interactive)
365 (when (Buffer-menu-no-header)
366 (let ((buffer-read-only nil))
367 (forward-char 2)
368 (delete-char 1)
369 (insert ?S)
370 (forward-line 1))))
372 (defun Buffer-menu-not-modified (&optional arg)
373 "Mark buffer on this line as unmodified (no changes to save)."
374 (interactive "P")
375 (save-excursion
376 (set-buffer (Buffer-menu-buffer t))
377 (set-buffer-modified-p arg))
378 (save-excursion
379 (beginning-of-line)
380 (forward-char 2)
381 (if (= (char-after) (if arg ?\s ?*))
382 (let ((buffer-read-only nil))
383 (delete-char 1)
384 (insert (if arg ?* ?\s))))))
386 (defun Buffer-menu-beginning ()
387 (goto-char (point-min))
388 (unless Buffer-menu-use-header-line
389 (forward-line)))
391 (defun Buffer-menu-execute ()
392 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
393 (interactive)
394 (save-excursion
395 (Buffer-menu-beginning)
396 (while (re-search-forward "^..S" nil t)
397 (let ((modp nil))
398 (save-excursion
399 (set-buffer (Buffer-menu-buffer t))
400 (save-buffer)
401 (setq modp (buffer-modified-p)))
402 (let ((buffer-read-only nil))
403 (delete-char -1)
404 (insert (if modp ?* ?\s))))))
405 (save-excursion
406 (Buffer-menu-beginning)
407 (let ((buff-menu-buffer (current-buffer))
408 (buffer-read-only nil))
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))))))))
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 ((buffer-read-only nil))
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!
464 (defun Buffer-menu-visit-tags-table ()
465 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
466 (interactive)
467 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
468 (if file
469 (visit-tags-table file)
470 (error "Specified buffer has no file"))))
472 (defun Buffer-menu-1-window ()
473 "Select this line's buffer, alone, in full frame."
474 (interactive)
475 (switch-to-buffer (Buffer-menu-buffer t))
476 (bury-buffer (other-buffer))
477 (delete-other-windows))
479 (defun Buffer-menu-mouse-select (event)
480 "Select the buffer whose line you click on."
481 (interactive "e")
482 (let (buffer)
483 (save-excursion
484 (set-buffer (window-buffer (posn-window (event-end event))))
485 (save-excursion
486 (goto-char (posn-point (event-end event)))
487 (setq buffer (Buffer-menu-buffer t))))
488 (select-window (posn-window (event-end event)))
489 (if (and (window-dedicated-p (selected-window))
490 (eq (selected-window) (frame-root-window)))
491 (switch-to-buffer-other-frame buffer)
492 (switch-to-buffer buffer))))
494 (defun Buffer-menu-this-window ()
495 "Select this line's buffer in this window."
496 (interactive)
497 (switch-to-buffer (Buffer-menu-buffer t)))
499 (defun Buffer-menu-other-window ()
500 "Select this line's buffer in other window, leaving buffer menu visible."
501 (interactive)
502 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
504 (defun Buffer-menu-switch-other-window ()
505 "Make the other window select this line's buffer.
506 The current window remains selected."
507 (interactive)
508 (let ((pop-up-windows t)
509 same-window-buffer-names
510 same-window-regexps)
511 (display-buffer (Buffer-menu-buffer t))))
513 (defun Buffer-menu-2-window ()
514 "Select this line's buffer, with previous buffer in second window."
515 (interactive)
516 (let ((buff (Buffer-menu-buffer t))
517 (menu (current-buffer))
518 (pop-up-windows t)
519 same-window-buffer-names
520 same-window-regexps)
521 (delete-other-windows)
522 (switch-to-buffer (other-buffer))
523 (pop-to-buffer buff)
524 (bury-buffer menu)))
526 (defun Buffer-menu-toggle-read-only ()
527 "Toggle read-only status of buffer on this line, perhaps via version control."
528 (interactive)
529 (let (char)
530 (save-excursion
531 (set-buffer (Buffer-menu-buffer t))
532 (vc-toggle-read-only)
533 (setq char (if buffer-read-only ?% ?\s)))
534 (save-excursion
535 (beginning-of-line)
536 (forward-char 1)
537 (if (/= (following-char) char)
538 (let (buffer-read-only)
539 (delete-char 1)
540 (insert char))))))
542 (defun Buffer-menu-bury ()
543 "Bury the buffer listed on this line."
544 (interactive)
545 (when (Buffer-menu-no-header)
546 (save-excursion
547 (beginning-of-line)
548 (bury-buffer (Buffer-menu-buffer t))
549 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
550 (buffer-read-only nil))
551 (delete-region (point) (progn (forward-line -1) (point)))
552 (goto-char (point-max))
553 (insert line))
554 (message "Buried buffer moved to the end"))))
557 (defun Buffer-menu-view ()
558 "View this line's buffer in View mode."
559 (interactive)
560 (view-buffer (Buffer-menu-buffer t)))
563 (defun Buffer-menu-view-other-window ()
564 "View this line's buffer in View mode in another window."
565 (interactive)
566 (view-buffer-other-window (Buffer-menu-buffer t)))
569 (define-key ctl-x-map "\C-b" 'list-buffers)
571 (defun list-buffers (&optional files-only)
572 "Display a list of names of existing buffers.
573 The list is displayed in a buffer named `*Buffer List*'.
574 Note that buffers with names starting with spaces are omitted.
575 Non-null optional arg FILES-ONLY means mention only file buffers.
577 For more information, see the function `buffer-menu'."
578 (interactive "P")
579 (display-buffer (list-buffers-noselect files-only)))
581 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
582 (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width)
583 (setq name
584 (if (string-match "<[0-9]+>$" name)
585 (concat (substring name 0
586 (- Buffer-menu-buffer+size-width
587 (max (length size) 3)
588 (match-end 0)
589 (- (match-beginning 0))
591 ":" ; narrow ellipsis
592 (match-string 0 name))
593 (concat (substring name 0
594 (- Buffer-menu-buffer+size-width
595 (max (length size) 3)
597 ":"))) ; narrow ellipsis
598 ;; Don't put properties on (buffer-name).
599 (setq name (copy-sequence name)))
600 (add-text-properties 0 (length name) name-props name)
601 (add-text-properties 0 (length size) size-props size)
602 (concat name
603 (make-string (- Buffer-menu-buffer+size-width
604 (length name)
605 (length size))
606 ?\s)
607 size))
609 (defun Buffer-menu-sort (column)
610 "Sort the buffer menu by COLUMN."
611 (interactive "P")
612 (when column
613 (setq column (prefix-numeric-value column))
614 (if (< column 2) (setq column 2))
615 (if (> column 5) (setq column 5)))
616 (setq Buffer-menu-sort-column column)
617 (let (buffer-read-only l buf m1 m2)
618 (save-excursion
619 (Buffer-menu-beginning)
620 (while (not (eobp))
621 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
622 (setq m1 (char-after)
623 m1 (if (memq m1 '(?> ?D)) m1)
624 m2 (char-after (+ (point) 2))
625 m2 (if (eq m2 ?S) m2))
626 (if (or m1 m2)
627 (push (list buf m1 m2) l)))
628 (forward-line)))
629 (Buffer-menu-revert)
630 (setq buffer-read-only)
631 (save-excursion
632 (Buffer-menu-beginning)
633 (while (not (eobp))
634 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
635 (setq m1 (cadr buf)
636 m2 (cadr (cdr buf)))
637 (when m1
638 (delete-char 1)
639 (insert m1)
640 (backward-char 1))
641 (when m2
642 (forward-char 2)
643 (delete-char 1)
644 (insert m2)))
645 (forward-line)))))
647 (defun Buffer-menu-sort-by-column (&optional e)
648 "Sort the buffer menu by the column clicked on."
649 (interactive (list last-input-event))
650 (if e (mouse-select-window e))
651 (let* ((pos (event-start e))
652 (obj (posn-object pos))
653 (col (if obj
654 (get-text-property (cdr obj) 'column (car obj))
655 (get-text-property (posn-point pos) 'column))))
656 (Buffer-menu-sort col)))
658 (defvar Buffer-menu-sort-button-map
659 (let ((map (make-sparse-keymap)))
660 ;; This keymap handles both nil and non-nil values for
661 ;; Buffer-menu-use-header-line.
662 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
663 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
664 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
665 (define-key map [follow-link] 'mouse-face)
666 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
667 map)
668 "Local keymap for Buffer menu sort buttons.")
670 (defun Buffer-menu-make-sort-button (name column)
671 (if (equal column Buffer-menu-sort-column) (setq column nil))
672 (propertize name
673 'column column
674 'help-echo (concat
675 (if Buffer-menu-use-header-line
676 "mouse-1, mouse-2: sort by "
677 "mouse-2, RET: sort by ")
678 (if column (downcase name) "visited order"))
679 'mouse-face 'highlight
680 'keymap Buffer-menu-sort-button-map))
682 (defun list-buffers-noselect (&optional files-only buffer-list)
683 "Create and return a buffer with a list of names of existing buffers.
684 The buffer is named `*Buffer List*'.
685 Note that buffers with names starting with spaces are omitted.
686 Non-null optional arg FILES-ONLY means mention only file buffers.
688 If BUFFER-LIST is non-nil, it should be a list of buffers;
689 it means list those buffers and no others.
691 For more information, see the function `buffer-menu'."
692 (let* ((old-buffer (current-buffer))
693 (standard-output standard-output)
694 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
695 (header (concat "CRM "
696 (Buffer-menu-buffer+size
697 (Buffer-menu-make-sort-button "Buffer" 2)
698 (Buffer-menu-make-sort-button "Size" 3))
700 (Buffer-menu-make-sort-button "Mode" 4) mode-end
701 (Buffer-menu-make-sort-button "File" 5) "\n"))
702 list desired-point)
703 (when Buffer-menu-use-header-line
704 (let ((pos 0))
705 ;; Turn whitespace chars in the header into stretch specs so
706 ;; they work regardless of the header-line face.
707 (while (string-match "[ \t\n]+" header pos)
708 (setq pos (match-end 0))
709 (put-text-property (match-beginning 0) pos 'display
710 ;; Assume fixed-size chars in the buffer.
711 (list 'space :align-to pos)
712 header)))
713 ;; Try to better align the one-char headers.
714 (put-text-property 0 3 'face 'fixed-pitch header)
715 ;; Add a "dummy" leading space to align the beginning of the header
716 ;; line with the beginning of the text (rather than with the left
717 ;; scrollbar or the left fringe). --Stef
718 (setq header (concat (propertize " " 'display '(space :align-to 0))
719 header)))
720 (with-current-buffer (get-buffer-create "*Buffer List*")
721 (setq buffer-read-only nil)
722 (erase-buffer)
723 (setq standard-output (current-buffer))
724 (unless Buffer-menu-use-header-line
725 ;; Use U+2014 (EM DASH) to underline if possible, else use ASCII
726 ;; (i.e. U+002D, HYPHEN-MINUS).
727 (let ((underline (if (char-displayable-p ?\u2014) ?\u2014 ?-)))
728 (insert header
729 (apply 'string
730 (mapcar (lambda (c)
731 (if (memq c '(?\n ?\s)) c underline))
732 header)))))
733 ;; Collect info for every buffer we're interested in.
734 (dolist (buffer (or buffer-list
735 (buffer-list
736 (when Buffer-menu-use-frame-buffer-list
737 (selected-frame)))))
738 (with-current-buffer buffer
739 (let ((name (buffer-name))
740 (file buffer-file-name))
741 (unless (and (not buffer-list)
743 ;; Don't mention internal buffers.
744 (and (string= (substring name 0 1) " ") (null file))
745 ;; Maybe don't mention buffers without files.
746 (and files-only (not file))
747 (string= name "*Buffer List*")))
748 ;; Otherwise output info.
749 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
750 (if mode-line-process
751 (format-mode-line mode-line-process
752 nil nil buffer))))
753 (bits (string
754 (if (eq buffer old-buffer) ?. ?\s)
755 ;; Handle readonly status. The output buffer
756 ;; is special cased to appear readonly; it is
757 ;; actually made so at a later date.
758 (if (or (eq buffer standard-output)
759 buffer-read-only)
760 ?% ?\s)
761 ;; Identify modified buffers.
762 (if (buffer-modified-p) ?* ?\s)
763 ;; Space separator.
764 ?\s)))
765 (unless file
766 ;; No visited file. Check local value of
767 ;; list-buffers-directory and, for Info buffers,
768 ;; Info-current-file.
769 (cond ((and (boundp 'list-buffers-directory)
770 list-buffers-directory)
771 (setq file list-buffers-directory))
772 ((eq major-mode 'Info-mode)
773 (setq file Info-current-file)
774 (cond
775 ((equal file "dir")
776 (setq file "*Info Directory*"))
777 ((eq file 'apropos)
778 (setq file "*Info Apropos*"))
779 ((eq file 'history)
780 (setq file "*Info History*"))
781 ((eq file 'toc)
782 (setq file "*Info TOC*"))
783 ((not (stringp file)) ;; avoid errors
784 (setq file nil))
786 (setq file (concat "("
787 (file-name-nondirectory file)
789 Info-current-node)))))))
790 (push (list buffer bits name (buffer-size) mode file)
791 list))))))
792 ;; Preserve the original buffer-list ordering, just in case.
793 (setq list (nreverse list))
794 ;; Place the buffers's info in the output buffer, sorted if necessary.
795 (dolist (buffer
796 (if Buffer-menu-sort-column
797 (sort list
798 (if (eq Buffer-menu-sort-column 3)
799 (lambda (a b)
800 (< (nth Buffer-menu-sort-column a)
801 (nth Buffer-menu-sort-column b)))
802 (lambda (a b)
803 (string< (nth Buffer-menu-sort-column a)
804 (nth Buffer-menu-sort-column b)))))
805 list))
806 (if (eq (car buffer) old-buffer)
807 (setq desired-point (point)))
808 (insert (cadr buffer)
809 ;; Put the buffer name into a text property
810 ;; so we don't have to extract it from the text.
811 ;; This way we avoid problems with unusual buffer names.
812 (Buffer-menu-buffer+size (nth 2 buffer)
813 (int-to-string (nth 3 buffer))
814 `(buffer-name ,(nth 2 buffer)
815 buffer ,(car buffer)
816 font-lock-face buffer-menu-buffer
817 mouse-face highlight
818 help-echo "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