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