Move defvars out of eval-when-compile. Use dolist.
[emacs.git] / lisp / buff-menu.el
blob62ff0c0a01ddf0763245ac0136a7d4a57a465ad1
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 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 "2 for sorting by buffer names. 5 for sorting by file names.
106 nil for default sorting by visited order.")
108 (defconst Buffer-menu-buffer-column 4)
110 (defvar Buffer-menu-mode-map nil
111 "Local keymap for `Buffer-menu-mode' buffers.")
113 (defvar Buffer-menu-files-only nil
114 "Non-nil if the current buffer-menu lists only file buffers.
115 This variable determines whether reverting the buffer lists only
116 file buffers. It affects both manual reverting and reverting by
117 Auto Revert Mode.")
119 (make-variable-buffer-local 'Buffer-menu-files-only)
121 (if Buffer-menu-mode-map
123 (setq Buffer-menu-mode-map (make-keymap))
124 (suppress-keymap Buffer-menu-mode-map t)
125 (define-key Buffer-menu-mode-map "q" 'quit-window)
126 (define-key Buffer-menu-mode-map "v" 'Buffer-menu-select)
127 (define-key Buffer-menu-mode-map "2" 'Buffer-menu-2-window)
128 (define-key Buffer-menu-mode-map "1" 'Buffer-menu-1-window)
129 (define-key Buffer-menu-mode-map "f" 'Buffer-menu-this-window)
130 (define-key Buffer-menu-mode-map "e" 'Buffer-menu-this-window)
131 (define-key Buffer-menu-mode-map "\C-m" 'Buffer-menu-this-window)
132 (define-key Buffer-menu-mode-map "o" 'Buffer-menu-other-window)
133 (define-key Buffer-menu-mode-map "\C-o" 'Buffer-menu-switch-other-window)
134 (define-key Buffer-menu-mode-map "s" 'Buffer-menu-save)
135 (define-key Buffer-menu-mode-map "d" 'Buffer-menu-delete)
136 (define-key Buffer-menu-mode-map "k" 'Buffer-menu-delete)
137 (define-key Buffer-menu-mode-map "\C-d" 'Buffer-menu-delete-backwards)
138 (define-key Buffer-menu-mode-map "\C-k" 'Buffer-menu-delete)
139 (define-key Buffer-menu-mode-map "x" 'Buffer-menu-execute)
140 (define-key Buffer-menu-mode-map " " 'next-line)
141 (define-key Buffer-menu-mode-map "n" 'next-line)
142 (define-key Buffer-menu-mode-map "p" 'previous-line)
143 (define-key Buffer-menu-mode-map "\177" 'Buffer-menu-backup-unmark)
144 (define-key Buffer-menu-mode-map "~" 'Buffer-menu-not-modified)
145 (define-key Buffer-menu-mode-map "?" 'describe-mode)
146 (define-key Buffer-menu-mode-map "u" 'Buffer-menu-unmark)
147 (define-key Buffer-menu-mode-map "m" 'Buffer-menu-mark)
148 (define-key Buffer-menu-mode-map "t" 'Buffer-menu-visit-tags-table)
149 (define-key Buffer-menu-mode-map "%" 'Buffer-menu-toggle-read-only)
150 (define-key Buffer-menu-mode-map "b" 'Buffer-menu-bury)
151 (define-key Buffer-menu-mode-map "g" 'Buffer-menu-revert)
152 (define-key Buffer-menu-mode-map "V" 'Buffer-menu-view)
153 (define-key Buffer-menu-mode-map "T" 'Buffer-menu-toggle-files-only)
154 (define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-mouse-select)
155 (define-key Buffer-menu-mode-map [follow-link] 'mouse-face)
158 ;; Buffer Menu mode is suitable only for specially formatted data.
159 (put 'Buffer-menu-mode 'mode-class 'special)
161 (defun Buffer-menu-mode ()
162 "Major mode for editing a list of buffers.
163 Each line describes one of the buffers in Emacs.
164 Letters do not insert themselves; instead, they are commands.
165 \\<Buffer-menu-mode-map>
166 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
167 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
168 \\[Buffer-menu-other-window] -- select that buffer in another window,
169 so the buffer menu buffer remains visible in its window.
170 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
171 \\[Buffer-menu-view-other-window] -- select that buffer in
172 another window, in view-mode.
173 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
174 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
175 \\[Buffer-menu-select] -- select current line's buffer.
176 Also show buffers marked with m, in other windows.
177 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
178 \\[Buffer-menu-2-window] -- select that buffer in one window,
179 together with buffer selected before this one in another window.
180 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
181 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
182 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
183 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
184 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
185 \\[Buffer-menu-execute] -- delete or save marked buffers.
186 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
187 With prefix argument, also move up one line.
188 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
189 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
190 \\[Buffer-menu-revert] -- update the list of buffers.
191 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
192 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
193 (kill-all-local-variables)
194 (use-local-map Buffer-menu-mode-map)
195 (setq major-mode 'Buffer-menu-mode)
196 (setq mode-name "Buffer Menu")
197 (set (make-local-variable 'revert-buffer-function)
198 'Buffer-menu-revert-function)
199 (set (make-local-variable 'buffer-stale-function)
200 #'(lambda (&optional noconfirm) 'fast))
201 (setq truncate-lines t)
202 (setq buffer-read-only t)
203 (run-mode-hooks 'buffer-menu-mode-hook))
205 ;; This function exists so we can make the doc string of Buffer-menu-mode
206 ;; look nice.
207 (defun Buffer-menu-revert ()
208 "Update the list of buffers."
209 (interactive)
210 (revert-buffer))
212 (defun Buffer-menu-revert-function (ignore1 ignore2)
213 (or (eq buffer-undo-list t)
214 (setq buffer-undo-list nil))
215 ;; We can not use save-excursion here. The buffer gets erased.
216 (let ((opoint (point))
217 (eobp (eobp))
218 (ocol (current-column))
219 (oline (progn (move-to-column 4)
220 (get-text-property (point) 'buffer)))
221 (prop (point-min))
222 ;; do not make undo records for the reversion.
223 (buffer-undo-list t))
224 ;; We can be called by Auto Revert Mode with the "*Buffer Menu*"
225 ;; temporarily the current buffer. Make sure that the
226 ;; interactively current buffer is correctly identified with a `.'
227 ;; by `list-buffers-noselect'.
228 (with-current-buffer (window-buffer)
229 (list-buffers-noselect Buffer-menu-files-only))
230 (if oline
231 (while (setq prop (next-single-property-change prop 'buffer))
232 (when (eq (get-text-property prop 'buffer) oline)
233 (goto-char prop)
234 (move-to-column ocol)))
235 (goto-char (if eobp (point-max) opoint)))))
237 (defun Buffer-menu-toggle-files-only (arg)
238 "Toggle whether the current buffer-menu displays only file buffers.
239 With a positive ARG display only file buffers. With zero or
240 negative ARG, display other buffers as well."
241 (interactive "P")
242 (setq Buffer-menu-files-only
243 (cond ((not arg) (not Buffer-menu-files-only))
244 ((> (prefix-numeric-value arg) 0) t)))
245 (revert-buffer))
248 (defun Buffer-menu-buffer (error-if-non-existent-p)
249 "Return buffer described by this line of buffer menu."
250 (let* ((where (save-excursion
251 (beginning-of-line)
252 (+ (point) Buffer-menu-buffer-column)))
253 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
254 (buf (and (not (eobp)) (get-text-property where 'buffer))))
255 (if name
256 (or (get-buffer name)
257 (and buf (buffer-name buf) buf)
258 (if error-if-non-existent-p
259 (error "No buffer named `%s'" name)
260 nil))
261 (or (and buf (buffer-name buf) buf)
262 (if error-if-non-existent-p
263 (error "No buffer on this line")
264 nil)))))
266 (defun buffer-menu (&optional arg)
267 "Make a menu of buffers so you can save, delete or select them.
268 With argument, show only buffers that are visiting files.
269 Type ? after invocation to get help on commands available.
270 Type q to remove the buffer menu from the display.
272 The first column shows `>' for a buffer you have
273 marked to be displayed, `D' for one you have marked for
274 deletion, and `.' for the current buffer.
276 The C column has a `.' for the buffer from which you came.
277 The R column has a `%' if the buffer is read-only.
278 The M column has a `*' if it is modified,
279 or `S' if you have marked it for saving.
280 After this come the buffer name, its size in characters,
281 its major mode, and the visited file name (if any)."
282 (interactive "P")
283 ;;; (setq Buffer-menu-window-config (current-window-configuration))
284 (switch-to-buffer (list-buffers-noselect arg))
285 (message
286 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
288 (defun buffer-menu-other-window (&optional arg)
289 "Display a list of buffers in another window.
290 With the buffer list buffer, you can save, delete or select the buffers.
291 With argument, show only buffers that are visiting files.
292 Type ? after invocation to get help on commands available.
293 Type q to remove the buffer menu from the display.
294 For more information, see the function `buffer-menu'."
295 (interactive "P")
296 ;;; (setq Buffer-menu-window-config (current-window-configuration))
297 (switch-to-buffer-other-window (list-buffers-noselect arg))
298 (message
299 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
301 (defun Buffer-menu-no-header ()
302 (beginning-of-line)
303 (if (or Buffer-menu-use-header-line
304 (not (eq (char-after) ?C)))
306 (ding)
307 (forward-line 1)
308 nil))
310 (defun Buffer-menu-mark ()
311 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
312 (interactive)
313 (when (Buffer-menu-no-header)
314 (let ((buffer-read-only nil))
315 (delete-char 1)
316 (insert ?>)
317 (forward-line 1))))
319 (defun Buffer-menu-unmark (&optional backup)
320 "Cancel all requested operations on buffer on this line and move down.
321 Optional prefix arg means move up."
322 (interactive "P")
323 (when (Buffer-menu-no-header)
324 (let* ((buf (Buffer-menu-buffer t))
325 (mod (buffer-modified-p buf))
326 (readonly (save-excursion (set-buffer buf) buffer-read-only))
327 (buffer-read-only nil))
328 (delete-char 3)
329 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
330 (forward-line (if backup -1 1)))
332 (defun Buffer-menu-backup-unmark ()
333 "Move up and cancel all requested operations on buffer on line above."
334 (interactive)
335 (forward-line -1)
336 (Buffer-menu-unmark)
337 (forward-line -1))
339 (defun Buffer-menu-delete (&optional arg)
340 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
341 Prefix arg is how many buffers to delete.
342 Negative arg means delete backwards."
343 (interactive "p")
344 (when (Buffer-menu-no-header)
345 (let ((buffer-read-only nil))
346 (if (or (null arg) (= arg 0))
347 (setq arg 1))
348 (while (> arg 0)
349 (delete-char 1)
350 (insert ?D)
351 (forward-line 1)
352 (setq arg (1- arg)))
353 (while (and (< arg 0)
354 (Buffer-menu-no-header))
355 (delete-char 1)
356 (insert ?D)
357 (forward-line -1)
358 (setq arg (1+ arg))))))
360 (defun Buffer-menu-delete-backwards (&optional arg)
361 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
362 and then move up one line. Prefix arg means move that many lines."
363 (interactive "p")
364 (Buffer-menu-delete (- (or arg 1))))
366 (defun Buffer-menu-save ()
367 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
368 (interactive)
369 (when (Buffer-menu-no-header)
370 (let ((buffer-read-only nil))
371 (forward-char 2)
372 (delete-char 1)
373 (insert ?S)
374 (forward-line 1))))
376 (defun Buffer-menu-not-modified (&optional arg)
377 "Mark buffer on this line as unmodified (no changes to save)."
378 (interactive "P")
379 (save-excursion
380 (set-buffer (Buffer-menu-buffer t))
381 (set-buffer-modified-p arg))
382 (save-excursion
383 (beginning-of-line)
384 (forward-char 2)
385 (if (= (char-after) (if arg ?\s ?*))
386 (let ((buffer-read-only nil))
387 (delete-char 1)
388 (insert (if arg ?* ?\s))))))
390 (defun Buffer-menu-beginning ()
391 (goto-char (point-min))
392 (unless Buffer-menu-use-header-line
393 (forward-line)))
395 (defun Buffer-menu-execute ()
396 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
397 (interactive)
398 (save-excursion
399 (Buffer-menu-beginning)
400 (while (re-search-forward "^..S" nil t)
401 (let ((modp nil))
402 (save-excursion
403 (set-buffer (Buffer-menu-buffer t))
404 (save-buffer)
405 (setq modp (buffer-modified-p)))
406 (let ((buffer-read-only nil))
407 (delete-char -1)
408 (insert (if modp ?* ?\s))))))
409 (save-excursion
410 (Buffer-menu-beginning)
411 (let ((buff-menu-buffer (current-buffer))
412 (buffer-read-only nil))
413 (while (re-search-forward "^D" nil t)
414 (forward-char -1)
415 (let ((buf (Buffer-menu-buffer nil)))
416 (or (eq buf nil)
417 (eq buf buff-menu-buffer)
418 (save-excursion (kill-buffer buf)))
419 (if (and buf (buffer-name buf))
420 (progn (delete-char 1)
421 (insert ?\s))
422 (delete-region (point) (progn (forward-line 1) (point)))
423 (unless (bobp)
424 (forward-char -1))))))))
426 (defun Buffer-menu-select ()
427 "Select this line's buffer; also display buffers marked with `>'.
428 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
429 This command deletes and replaces all the previously existing windows
430 in the selected frame."
431 (interactive)
432 (let ((buff (Buffer-menu-buffer t))
433 (menu (current-buffer))
434 (others ())
435 tem)
436 (Buffer-menu-beginning)
437 (while (re-search-forward "^>" nil t)
438 (setq tem (Buffer-menu-buffer t))
439 (let ((buffer-read-only nil))
440 (delete-char -1)
441 (insert ?\s))
442 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
443 (setq others (nreverse others)
444 tem (/ (1- (frame-height)) (1+ (length others))))
445 (delete-other-windows)
446 (switch-to-buffer buff)
447 (or (eq menu buff)
448 (bury-buffer menu))
449 (if (equal (length others) 0)
450 (progn
451 ;;; ;; Restore previous window configuration before displaying
452 ;;; ;; selected buffers.
453 ;;; (if Buffer-menu-window-config
454 ;;; (progn
455 ;;; (set-window-configuration Buffer-menu-window-config)
456 ;;; (setq Buffer-menu-window-config nil)))
457 (switch-to-buffer buff))
458 (while others
459 (split-window nil tem)
460 (other-window 1)
461 (switch-to-buffer (car others))
462 (setq others (cdr others)))
463 (other-window 1) ;back to the beginning!
468 (defun Buffer-menu-visit-tags-table ()
469 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
470 (interactive)
471 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
472 (if file
473 (visit-tags-table file)
474 (error "Specified buffer has no file"))))
476 (defun Buffer-menu-1-window ()
477 "Select this line's buffer, alone, in full frame."
478 (interactive)
479 (switch-to-buffer (Buffer-menu-buffer t))
480 (bury-buffer (other-buffer))
481 (delete-other-windows))
483 (defun Buffer-menu-mouse-select (event)
484 "Select the buffer whose line you click on."
485 (interactive "e")
486 (let (buffer)
487 (save-excursion
488 (set-buffer (window-buffer (posn-window (event-end event))))
489 (save-excursion
490 (goto-char (posn-point (event-end event)))
491 (setq buffer (Buffer-menu-buffer t))))
492 (select-window (posn-window (event-end event)))
493 (if (and (window-dedicated-p (selected-window))
494 (eq (selected-window) (frame-root-window)))
495 (switch-to-buffer-other-frame buffer)
496 (switch-to-buffer buffer))))
498 (defun Buffer-menu-this-window ()
499 "Select this line's buffer in this window."
500 (interactive)
501 (switch-to-buffer (Buffer-menu-buffer t)))
503 (defun Buffer-menu-other-window ()
504 "Select this line's buffer in other window, leaving buffer menu visible."
505 (interactive)
506 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
508 (defun Buffer-menu-switch-other-window ()
509 "Make the other window select this line's buffer.
510 The current window remains selected."
511 (interactive)
512 (let ((pop-up-windows t)
513 same-window-buffer-names
514 same-window-regexps)
515 (display-buffer (Buffer-menu-buffer t))))
517 (defun Buffer-menu-2-window ()
518 "Select this line's buffer, with previous buffer in second window."
519 (interactive)
520 (let ((buff (Buffer-menu-buffer t))
521 (menu (current-buffer))
522 (pop-up-windows t)
523 same-window-buffer-names
524 same-window-regexps)
525 (delete-other-windows)
526 (switch-to-buffer (other-buffer))
527 (pop-to-buffer buff)
528 (bury-buffer menu)))
530 (defun Buffer-menu-toggle-read-only ()
531 "Toggle read-only status of buffer on this line, perhaps via version control."
532 (interactive)
533 (let (char)
534 (save-excursion
535 (set-buffer (Buffer-menu-buffer t))
536 (vc-toggle-read-only)
537 (setq char (if buffer-read-only ?% ?\s)))
538 (save-excursion
539 (beginning-of-line)
540 (forward-char 1)
541 (if (/= (following-char) char)
542 (let (buffer-read-only)
543 (delete-char 1)
544 (insert char))))))
546 (defun Buffer-menu-bury ()
547 "Bury the buffer listed on this line."
548 (interactive)
549 (when (Buffer-menu-no-header)
550 (save-excursion
551 (beginning-of-line)
552 (bury-buffer (Buffer-menu-buffer t))
553 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
554 (buffer-read-only nil))
555 (delete-region (point) (progn (forward-line -1) (point)))
556 (goto-char (point-max))
557 (insert line))
558 (message "Buried buffer moved to the end"))))
561 (defun Buffer-menu-view ()
562 "View this line's buffer in View mode."
563 (interactive)
564 (view-buffer (Buffer-menu-buffer t)))
567 (defun Buffer-menu-view-other-window ()
568 "View this line's buffer in View mode in another window."
569 (interactive)
570 (view-buffer-other-window (Buffer-menu-buffer t)))
573 (define-key ctl-x-map "\C-b" 'list-buffers)
575 (defun list-buffers (&optional files-only)
576 "Display a list of names of existing buffers.
577 The list is displayed in a buffer named `*Buffer List*'.
578 Note that buffers with names starting with spaces are omitted.
579 Non-null optional arg FILES-ONLY means mention only file buffers.
581 For more information, see the function `buffer-menu'."
582 (interactive "P")
583 (display-buffer (list-buffers-noselect files-only)))
585 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
586 (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width)
587 (setq name
588 (if (string-match "<[0-9]+>$" name)
589 (concat (substring name 0
590 (- Buffer-menu-buffer+size-width
591 (max (length size) 3)
592 (match-end 0)
593 (- (match-beginning 0))
595 ":" ; narrow ellipsis
596 (match-string 0 name))
597 (concat (substring name 0
598 (- Buffer-menu-buffer+size-width
599 (max (length size) 3)
601 ":"))) ; narrow ellipsis
602 ;; Don't put properties on (buffer-name).
603 (setq name (copy-sequence name)))
604 (add-text-properties 0 (length name) name-props name)
605 (add-text-properties 0 (length size) size-props size)
606 (concat name
607 (make-string (- Buffer-menu-buffer+size-width
608 (length name)
609 (length size))
610 ?\s)
611 size))
613 (defun Buffer-menu-sort (column)
614 "Sort the buffer menu by COLUMN."
615 (interactive "P")
616 (when column
617 (setq column (prefix-numeric-value column))
618 (if (< column 2) (setq column 2))
619 (if (> column 5) (setq column 5)))
620 (setq Buffer-menu-sort-column column)
621 (let (buffer-read-only l buf m1 m2)
622 (save-excursion
623 (Buffer-menu-beginning)
624 (while (not (eobp))
625 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
626 (setq m1 (char-after)
627 m1 (if (memq m1 '(?> ?D)) m1)
628 m2 (char-after (+ (point) 2))
629 m2 (if (eq m2 ?S) m2))
630 (if (or m1 m2)
631 (push (list buf m1 m2) l)))
632 (forward-line)))
633 (Buffer-menu-revert)
634 (setq buffer-read-only)
635 (save-excursion
636 (Buffer-menu-beginning)
637 (while (not (eobp))
638 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
639 (setq m1 (cadr buf)
640 m2 (cadr (cdr buf)))
641 (when m1
642 (delete-char 1)
643 (insert m1)
644 (backward-char 1))
645 (when m2
646 (forward-char 2)
647 (delete-char 1)
648 (insert m2)))
649 (forward-line)))))
651 (defun Buffer-menu-sort-by-column (&optional e)
652 "Sort the buffer menu by the column clicked on."
653 (interactive (list last-input-event))
654 (if e (mouse-select-window e))
655 (let* ((pos (event-start e))
656 (obj (posn-object pos))
657 (col (if obj
658 (get-text-property (cdr obj) 'column (car obj))
659 (get-text-property (posn-point pos) 'column))))
660 (Buffer-menu-sort col)))
662 (defvar Buffer-menu-sort-button-map
663 (let ((map (make-sparse-keymap)))
664 ;; This keymap handles both nil and non-nil values for
665 ;; Buffer-menu-use-header-line.
666 (define-key map [header-line mouse-1] 'Buffer-menu-sort-by-column)
667 (define-key map [header-line mouse-2] 'Buffer-menu-sort-by-column)
668 (define-key map [mouse-2] 'Buffer-menu-sort-by-column)
669 (define-key map [follow-link] 'mouse-face)
670 (define-key map "\C-m" 'Buffer-menu-sort-by-column)
671 map)
672 "Local keymap for Buffer menu sort buttons.")
674 (defun Buffer-menu-make-sort-button (name column)
675 (if (equal column Buffer-menu-sort-column) (setq column nil))
676 (propertize name
677 'column column
678 'help-echo (concat
679 (if Buffer-menu-use-header-line
680 "mouse-1, mouse-2: sort by "
681 "mouse-2, RET: sort by ")
682 (if column (downcase name) "visited order"))
683 'mouse-face 'highlight
684 'keymap Buffer-menu-sort-button-map))
686 (defun list-buffers-noselect (&optional files-only buffer-list)
687 "Create and return a buffer with a list of names of existing buffers.
688 The buffer is named `*Buffer List*'.
689 Note that buffers with names starting with spaces are omitted.
690 Non-null optional arg FILES-ONLY means mention only file buffers.
692 If BUFFER-LIST is non-nil, it should be a list of buffers;
693 it means list those buffers and no others.
695 For more information, see the function `buffer-menu'."
696 (let* ((old-buffer (current-buffer))
697 (standard-output standard-output)
698 (mode-end (make-string (- Buffer-menu-mode-width 2) ?\s))
699 (header (concat "CRM "
700 (Buffer-menu-buffer+size
701 (Buffer-menu-make-sort-button "Buffer" 2)
702 (Buffer-menu-make-sort-button "Size" 3))
704 (Buffer-menu-make-sort-button "Mode" 4) mode-end
705 (Buffer-menu-make-sort-button "File" 5) "\n"))
706 list desired-point)
707 (when Buffer-menu-use-header-line
708 (let ((pos 0))
709 ;; Turn spaces in the header into stretch specs so they work
710 ;; regardless of the header-line face.
711 (while (string-match "[ \t]+" header pos)
712 (setq pos (match-end 0))
713 (put-text-property (match-beginning 0) pos 'display
714 ;; Assume fixed-size chars in the buffer.
715 (list 'space :align-to pos)
716 header)))
717 ;; Try to better align the one-char headers.
718 (put-text-property 0 3 'face 'fixed-pitch header)
719 ;; Add a "dummy" leading space to align the beginning of the header
720 ;; line with the beginning of the text (rather than with the left
721 ;; scrollbar or the left fringe). –-Stef
722 (setq header (concat (propertize " " 'display '(space :align-to 0))
723 header)))
724 (with-current-buffer (get-buffer-create "*Buffer List*")
725 (setq buffer-read-only nil)
726 (erase-buffer)
727 (setq standard-output (current-buffer))
728 (unless Buffer-menu-use-header-line
729 (let ((underline (if (char-displayable-p ?—) ?— ?-)))
730 (insert header
731 (apply 'string
732 (mapcar (lambda (c)
733 (if (memq c '(?\n ?\s)) c underline))
734 header)))))
735 ;; Collect info for every buffer we're interested in.
736 (dolist (buffer (or buffer-list
737 (buffer-list
738 (when Buffer-menu-use-frame-buffer-list
739 (selected-frame)))))
740 (with-current-buffer buffer
741 (let ((name (buffer-name))
742 (file buffer-file-name))
743 (unless (and (not buffer-list)
745 ;; Don't mention internal buffers.
746 (and (string= (substring name 0 1) " ") (null file))
747 ;; Maybe don't mention buffers without files.
748 (and files-only (not file))
749 (string= name "*Buffer List*")))
750 ;; Otherwise output info.
751 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
752 (if mode-line-process
753 (format-mode-line mode-line-process
754 nil nil buffer))))
755 (bits (string
756 (if (eq buffer old-buffer) ?. ?\s)
757 ;; Handle readonly status. The output buffer
758 ;; is special cased to appear readonly; it is
759 ;; actually made so at a later date.
760 (if (or (eq buffer standard-output)
761 buffer-read-only)
762 ?% ?\s)
763 ;; Identify modified buffers.
764 (if (buffer-modified-p) ?* ?\s)
765 ;; Space separator.
766 ?\s)))
767 (unless file
768 ;; No visited file. Check local value of
769 ;; list-buffers-directory.
770 (when (and (boundp 'list-buffers-directory)
771 list-buffers-directory)
772 (setq file list-buffers-directory)))
773 (push (list buffer bits name (buffer-size) mode file)
774 list))))))
775 ;; Preserve the original buffer-list ordering, just in case.
776 (setq list (nreverse list))
777 ;; Place the buffers's info in the output buffer, sorted if necessary.
778 (dolist (buffer
779 (if Buffer-menu-sort-column
780 (sort list
781 (if (eq Buffer-menu-sort-column 3)
782 (lambda (a b)
783 (< (nth Buffer-menu-sort-column a)
784 (nth Buffer-menu-sort-column b)))
785 (lambda (a b)
786 (string< (nth Buffer-menu-sort-column a)
787 (nth Buffer-menu-sort-column b)))))
788 list))
789 (if (eq (car buffer) old-buffer)
790 (setq desired-point (point)))
791 (insert (cadr buffer)
792 ;; Put the buffer name into a text property
793 ;; so we don't have to extract it from the text.
794 ;; This way we avoid problems with unusual buffer names.
795 (Buffer-menu-buffer+size (nth 2 buffer)
796 (int-to-string (nth 3 buffer))
797 `(buffer-name ,(nth 2 buffer)
798 buffer ,(car buffer)
799 font-lock-face buffer-menu-buffer
800 mouse-face highlight
801 help-echo "mouse-2: select this buffer"))
803 (if (> (length (nth 4 buffer)) Buffer-menu-mode-width)
804 (substring (nth 4 buffer) 0 Buffer-menu-mode-width)
805 (nth 4 buffer)))
806 (when (nth 5 buffer)
807 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
808 Buffer-menu-mode-width 4) 1)
809 (princ (abbreviate-file-name (nth 5 buffer))))
810 (princ "\n"))
811 (Buffer-menu-mode)
812 (when Buffer-menu-use-header-line
813 (setq header-line-format header))
814 ;; DESIRED-POINT doesn't have to be set; it is not when the
815 ;; current buffer is not displayed for some reason.
816 (and desired-point
817 (goto-char desired-point))
818 (setq Buffer-menu-files-only files-only)
819 (set-buffer-modified-p nil)
820 (current-buffer))))
822 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
823 ;;; buff-menu.el ends here