(list-buffers-noselect): Eliminate space at the start of HEADER.
[emacs.git] / lisp / buff-menu.el
blob581f505ee9818f23f2fdfcdf29d93994594282a0
1 ;;; buff-menu.el --- buffer menu main function and support functions
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
4 ;; 2004 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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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-face
78 '((t (:weight bold)))
79 "Face used to highlight buffer name."
80 :group 'font-lock-highlighting-faces)
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)
146 ;; Buffer Menu mode is suitable only for specially formatted data.
147 (put 'Buffer-menu-mode 'mode-class 'special)
149 (defun Buffer-menu-mode ()
150 "Major mode for editing a list of buffers.
151 Each line describes one of the buffers in Emacs.
152 Letters do not insert themselves; instead, they are commands.
153 \\<Buffer-menu-mode-map>
154 \\[Buffer-menu-mouse-select] -- select buffer you click on, in place of the buffer menu.
155 \\[Buffer-menu-this-window] -- select current line's buffer in place of the buffer menu.
156 \\[Buffer-menu-other-window] -- select that buffer in another window,
157 so the buffer menu buffer remains visible in its window.
158 \\[Buffer-menu-view] -- select current line's buffer, but in view-mode.
159 \\[Buffer-menu-view-other-window] -- select that buffer in
160 another window, in view-mode.
161 \\[Buffer-menu-switch-other-window] -- make another window display that buffer.
162 \\[Buffer-menu-mark] -- mark current line's buffer to be displayed.
163 \\[Buffer-menu-select] -- select current line's buffer.
164 Also show buffers marked with m, in other windows.
165 \\[Buffer-menu-1-window] -- select that buffer in full-frame window.
166 \\[Buffer-menu-2-window] -- select that buffer in one window,
167 together with buffer selected before this one in another window.
168 \\[Buffer-menu-visit-tags-table] -- visit-tags-table this buffer.
169 \\[Buffer-menu-not-modified] -- clear modified-flag on that buffer.
170 \\[Buffer-menu-save] -- mark that buffer to be saved, and move down.
171 \\[Buffer-menu-delete] -- mark that buffer to be deleted, and move down.
172 \\[Buffer-menu-delete-backwards] -- mark that buffer to be deleted, and move up.
173 \\[Buffer-menu-execute] -- delete or save marked buffers.
174 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
175 With prefix argument, also move up one line.
176 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
177 \\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
178 \\[Buffer-menu-revert] -- update the list of buffers.
179 \\[Buffer-menu-toggle-files-only] -- toggle whether the menu displays only file buffers.
180 \\[Buffer-menu-bury] -- bury the buffer listed on this line."
181 (kill-all-local-variables)
182 (use-local-map Buffer-menu-mode-map)
183 (setq major-mode 'Buffer-menu-mode)
184 (setq mode-name "Buffer Menu")
185 (set (make-local-variable 'revert-buffer-function)
186 'Buffer-menu-revert-function)
187 (set (make-local-variable 'buffer-stale-function)
188 #'(lambda (&optional noconfirm) 'fast))
189 (setq truncate-lines t)
190 (setq buffer-read-only t)
191 (run-hooks 'buffer-menu-mode-hook))
193 (defun Buffer-menu-revert ()
194 "Update the list of buffers."
195 (interactive)
196 (revert-buffer))
198 (defun Buffer-menu-revert-function (ignore1 ignore2)
199 ;; We can not use save-excursion here. The buffer gets erased.
200 (let ((ocol (current-column))
201 (oline (progn (move-to-column 4)
202 (get-text-property (point) 'buffer)))
203 (prop (point-min)))
204 (list-buffers-noselect Buffer-menu-files-only)
205 (while (setq prop (next-single-property-change prop 'buffer))
206 (when (eq (get-text-property prop 'buffer) oline)
207 (goto-char prop)
208 (move-to-column ocol)))))
210 (defun Buffer-menu-toggle-files-only (arg)
211 "Toggle whether the current buffer-menu displays only file buffers.
212 With a positive ARG display only file buffers. With zero or
213 negative ARG, display other buffers as well."
214 (interactive "P")
215 (setq Buffer-menu-files-only
216 (cond ((not arg) (not Buffer-menu-files-only))
217 ((> (prefix-numeric-value arg) 0) t)))
218 (revert-buffer))
221 (defun Buffer-menu-buffer (error-if-non-existent-p)
222 "Return buffer described by this line of buffer menu."
223 (let* ((where (save-excursion
224 (beginning-of-line)
225 (+ (point) Buffer-menu-buffer-column)))
226 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
227 (buf (and (not (eobp)) (get-text-property where 'buffer))))
228 (if name
229 (or (get-buffer name)
230 (and buf (buffer-name buf) buf)
231 (if error-if-non-existent-p
232 (error "No buffer named `%s'" name)
233 nil))
234 (or (and buf (buffer-name buf) buf)
235 (if error-if-non-existent-p
236 (error "No buffer on this line")
237 nil)))))
239 (defun buffer-menu (&optional arg)
240 "Make a menu of buffers so you can save, delete or select them.
241 With argument, show only buffers that are visiting files.
242 Type ? after invocation to get help on commands available.
243 Type q to remove the buffer menu from the display.
245 The first column shows `>' for a buffer you have
246 marked to be displayed, `D' for one you have marked for
247 deletion, and `.' for the current buffer.
249 The C column has a `.' for the buffer from which you came.
250 The R column has a `%' if the buffer is read-only.
251 The M column has a `*' if it is modified,
252 or `S' if you have marked it for saving.
253 After this come the buffer name, its size in characters,
254 its major mode, and the visited file name (if any)."
255 (interactive "P")
256 ;;; (setq Buffer-menu-window-config (current-window-configuration))
257 (switch-to-buffer (list-buffers-noselect arg))
258 (message
259 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
261 (defun buffer-menu-other-window (&optional arg)
262 "Display a list of buffers in another window.
263 With the buffer list buffer, you can save, delete or select the buffers.
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.
267 For more information, see the function `buffer-menu'."
268 (interactive "P")
269 ;;; (setq Buffer-menu-window-config (current-window-configuration))
270 (switch-to-buffer-other-window (list-buffers-noselect arg))
271 (message
272 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
274 (defun Buffer-menu-no-header ()
275 (beginning-of-line)
276 (if (or Buffer-menu-use-header-line
277 (not (eq (char-after) ?C)))
279 (ding)
280 (forward-line 1)
281 nil))
283 (defun Buffer-menu-mark ()
284 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
285 (interactive)
286 (when (Buffer-menu-no-header)
287 (let ((buffer-read-only nil))
288 (delete-char 1)
289 (insert ?>)
290 (forward-line 1))))
292 (defun Buffer-menu-unmark (&optional backup)
293 "Cancel all requested operations on buffer on this line and move down.
294 Optional ARG means move up."
295 (interactive "P")
296 (when (Buffer-menu-no-header)
297 (let* ((buf (Buffer-menu-buffer t))
298 (mod (buffer-modified-p buf))
299 (readonly (save-excursion (set-buffer buf) buffer-read-only))
300 (buffer-read-only nil))
301 (delete-char 3)
302 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
303 (forward-line (if backup -1 1)))
305 (defun Buffer-menu-backup-unmark ()
306 "Move up and cancel all requested operations on buffer on line above."
307 (interactive)
308 (forward-line -1)
309 (Buffer-menu-unmark)
310 (forward-line -1))
312 (defun Buffer-menu-delete (&optional arg)
313 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
314 Prefix arg is how many buffers to delete.
315 Negative arg means delete backwards."
316 (interactive "p")
317 (when (Buffer-menu-no-header)
318 (let ((buffer-read-only nil))
319 (if (or (null arg) (= arg 0))
320 (setq arg 1))
321 (while (> arg 0)
322 (delete-char 1)
323 (insert ?D)
324 (forward-line 1)
325 (setq arg (1- arg)))
326 (while (and (< arg 0)
327 (Buffer-menu-no-header))
328 (delete-char 1)
329 (insert ?D)
330 (forward-line -1)
331 (setq arg (1+ arg))))))
333 (defun Buffer-menu-delete-backwards (&optional arg)
334 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
335 and then move up one line. Prefix arg means move that many lines."
336 (interactive "p")
337 (Buffer-menu-delete (- (or arg 1))))
339 (defun Buffer-menu-save ()
340 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
341 (interactive)
342 (when (Buffer-menu-no-header)
343 (let ((buffer-read-only nil))
344 (forward-char 2)
345 (delete-char 1)
346 (insert ?S)
347 (forward-line 1))))
349 (defun Buffer-menu-not-modified (&optional arg)
350 "Mark buffer on this line as unmodified (no changes to save)."
351 (interactive "P")
352 (save-excursion
353 (set-buffer (Buffer-menu-buffer t))
354 (set-buffer-modified-p arg))
355 (save-excursion
356 (beginning-of-line)
357 (forward-char 2)
358 (if (= (char-after) (if arg ? ?*))
359 (let ((buffer-read-only nil))
360 (delete-char 1)
361 (insert (if arg ?* ? ))))))
363 (defun Buffer-menu-beginning ()
364 (goto-char (point-min))
365 (unless Buffer-menu-use-header-line
366 (forward-line)))
368 (defun Buffer-menu-execute ()
369 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
370 (interactive)
371 (save-excursion
372 (Buffer-menu-beginning)
373 (while (re-search-forward "^..S" nil t)
374 (let ((modp nil))
375 (save-excursion
376 (set-buffer (Buffer-menu-buffer t))
377 (save-buffer)
378 (setq modp (buffer-modified-p)))
379 (let ((buffer-read-only nil))
380 (delete-char -1)
381 (insert (if modp ?* ? ))))))
382 (save-excursion
383 (Buffer-menu-beginning)
384 (let ((buff-menu-buffer (current-buffer))
385 (buffer-read-only nil))
386 (while (re-search-forward "^D" nil t)
387 (forward-char -1)
388 (let ((buf (Buffer-menu-buffer nil)))
389 (or (eq buf nil)
390 (eq buf buff-menu-buffer)
391 (save-excursion (kill-buffer buf)))
392 (if (and buf (buffer-name buf))
393 (progn (delete-char 1)
394 (insert ? ))
395 (delete-region (point) (progn (forward-line 1) (point)))
396 (unless (bobp)
397 (forward-char -1))))))))
399 (defun Buffer-menu-select ()
400 "Select this line's buffer; also display buffers marked with `>'.
401 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
402 This command deletes and replaces all the previously existing windows
403 in the selected frame."
404 (interactive)
405 (let ((buff (Buffer-menu-buffer t))
406 (menu (current-buffer))
407 (others ())
408 tem)
409 (Buffer-menu-beginning)
410 (while (re-search-forward "^>" nil t)
411 (setq tem (Buffer-menu-buffer t))
412 (let ((buffer-read-only nil))
413 (delete-char -1)
414 (insert ?\ ))
415 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
416 (setq others (nreverse others)
417 tem (/ (1- (frame-height)) (1+ (length others))))
418 (delete-other-windows)
419 (switch-to-buffer buff)
420 (or (eq menu buff)
421 (bury-buffer menu))
422 (if (equal (length others) 0)
423 (progn
424 ;;; ;; Restore previous window configuration before displaying
425 ;;; ;; selected buffers.
426 ;;; (if Buffer-menu-window-config
427 ;;; (progn
428 ;;; (set-window-configuration Buffer-menu-window-config)
429 ;;; (setq Buffer-menu-window-config nil)))
430 (switch-to-buffer buff))
431 (while others
432 (split-window nil tem)
433 (other-window 1)
434 (switch-to-buffer (car others))
435 (setq others (cdr others)))
436 (other-window 1) ;back to the beginning!
441 (defun Buffer-menu-visit-tags-table ()
442 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
443 (interactive)
444 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
445 (if file
446 (visit-tags-table file)
447 (error "Specified buffer has no file"))))
449 (defun Buffer-menu-1-window ()
450 "Select this line's buffer, alone, in full frame."
451 (interactive)
452 (switch-to-buffer (Buffer-menu-buffer t))
453 (bury-buffer (other-buffer))
454 (delete-other-windows))
456 (defun Buffer-menu-mouse-select (event)
457 "Select the buffer whose line you click on."
458 (interactive "e")
459 (let (buffer)
460 (save-excursion
461 (set-buffer (window-buffer (posn-window (event-end event))))
462 (save-excursion
463 (goto-char (posn-point (event-end event)))
464 (setq buffer (Buffer-menu-buffer t))))
465 (select-window (posn-window (event-end event)))
466 (if (and (window-dedicated-p (selected-window))
467 (eq (selected-window) (frame-root-window)))
468 (switch-to-buffer-other-frame buffer)
469 (switch-to-buffer buffer))))
471 (defun Buffer-menu-this-window ()
472 "Select this line's buffer in this window."
473 (interactive)
474 (switch-to-buffer (Buffer-menu-buffer t)))
476 (defun Buffer-menu-other-window ()
477 "Select this line's buffer in other window, leaving buffer menu visible."
478 (interactive)
479 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
481 (defun Buffer-menu-switch-other-window ()
482 "Make the other window select this line's buffer.
483 The current window remains selected."
484 (interactive)
485 (display-buffer (Buffer-menu-buffer t)))
487 (defun Buffer-menu-2-window ()
488 "Select this line's buffer, with previous buffer in second window."
489 (interactive)
490 (let ((buff (Buffer-menu-buffer t))
491 (menu (current-buffer))
492 (pop-up-windows t))
493 (delete-other-windows)
494 (switch-to-buffer (other-buffer))
495 (pop-to-buffer buff)
496 (bury-buffer menu)))
498 (defun Buffer-menu-toggle-read-only ()
499 "Toggle read-only status of buffer on this line, perhaps via version control."
500 (interactive)
501 (let (char)
502 (save-excursion
503 (set-buffer (Buffer-menu-buffer t))
504 (vc-toggle-read-only)
505 (setq char (if buffer-read-only ?% ? )))
506 (save-excursion
507 (beginning-of-line)
508 (forward-char 1)
509 (if (/= (following-char) char)
510 (let (buffer-read-only)
511 (delete-char 1)
512 (insert char))))))
514 (defun Buffer-menu-bury ()
515 "Bury the buffer listed on this line."
516 (interactive)
517 (when (Buffer-menu-no-header)
518 (save-excursion
519 (beginning-of-line)
520 (bury-buffer (Buffer-menu-buffer t))
521 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
522 (buffer-read-only nil))
523 (delete-region (point) (progn (forward-line -1) (point)))
524 (goto-char (point-max))
525 (insert line))
526 (message "Buried buffer moved to the end"))))
529 (defun Buffer-menu-view ()
530 "View this line's buffer in View mode."
531 (interactive)
532 (view-buffer (Buffer-menu-buffer t)))
535 (defun Buffer-menu-view-other-window ()
536 "View this line's buffer in View mode in another window."
537 (interactive)
538 (view-buffer-other-window (Buffer-menu-buffer t)))
541 (define-key ctl-x-map "\C-b" 'list-buffers)
543 (defun list-buffers (&optional files-only)
544 "Display a list of names of existing buffers.
545 The list is displayed in a buffer named `*Buffer List*'.
546 Note that buffers with names starting with spaces are omitted.
547 Non-null optional arg FILES-ONLY means mention only file buffers.
549 For more information, see the function `buffer-menu'."
550 (interactive "P")
551 (display-buffer (list-buffers-noselect files-only)))
553 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
554 (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width)
555 (setq name
556 (if (string-match "<[0-9]+>$" name)
557 (concat (substring name 0
558 (- Buffer-menu-buffer+size-width
559 (max (length size) 3)
560 (match-end 0)
561 (- (match-beginning 0))
563 ":" ; narrow ellipsis
564 (match-string 0 name))
565 (concat (substring name 0
566 (- Buffer-menu-buffer+size-width
567 (max (length size) 3)
569 ":"))) ; narrow ellipsis
570 ;; Don't put properties on (buffer-name).
571 (setq name (copy-sequence name)))
572 (add-text-properties 0 (length name) name-props name)
573 (add-text-properties 0 (length size) size-props size)
574 (concat name
575 (make-string (- Buffer-menu-buffer+size-width
576 (length name)
577 (length size))
579 size))
581 (defun Buffer-menu-sort (column)
582 "Sort the buffer menu by COLUMN."
583 (interactive "P")
584 (when column
585 (setq column (prefix-numeric-value column))
586 (if (< column 2) (setq column 2))
587 (if (> column 5) (setq column 5)))
588 (setq Buffer-menu-sort-column column)
589 (let (buffer-read-only l buf m1 m2)
590 (save-excursion
591 (Buffer-menu-beginning)
592 (while (not (eobp))
593 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
594 (setq m1 (char-after)
595 m1 (if (memq m1 '(?> ?D)) m1)
596 m2 (char-after (+ (point) 2))
597 m2 (if (eq m2 ?S) m2))
598 (if (or m1 m2)
599 (push (list buf m1 m2) l)))
600 (forward-line)))
601 (Buffer-menu-revert)
602 (setq buffer-read-only)
603 (save-excursion
604 (Buffer-menu-beginning)
605 (while (not (eobp))
606 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
607 (setq m1 (cadr buf)
608 m2 (cadr (cdr buf)))
609 (when m1
610 (delete-char 1)
611 (insert m1)
612 (backward-char 1))
613 (when m2
614 (forward-char 2)
615 (delete-char 1)
616 (insert m2)))
617 (forward-line)))))
619 (defun Buffer-menu-make-sort-button (name column)
620 (if (equal column Buffer-menu-sort-column) (setq column nil))
621 (propertize name
622 'help-echo (if column
623 (concat "mouse-2: sort by " (downcase name))
624 "mouse-2: sort by visited order")
625 'mouse-face 'highlight
626 'keymap (let ((map (make-sparse-keymap)))
627 (define-key map [header-line mouse-2]
628 `(lambda (e)
629 (interactive "e")
630 (save-window-excursion
631 (if e (mouse-select-window e))
632 (Buffer-menu-sort ,column))))
633 map)))
635 (defun list-buffers-noselect (&optional files-only buffer-list)
636 "Create and return a buffer with a list of names of existing buffers.
637 The buffer is named `*Buffer List*'.
638 Note that buffers with names starting with spaces are omitted.
639 Non-null optional arg FILES-ONLY means mention only file buffers.
641 If BUFFER-LIST is non-nil, it should be a list of buffers;
642 it means list those buffers and no others.
644 For more information, see the function `buffer-menu'."
645 (let* ((old-buffer (current-buffer))
646 (standard-output standard-output)
647 (mode-end (make-string (- Buffer-menu-mode-width 2) ? ))
648 (header (concat (propertize "CRM " 'face 'fixed-pitch)
649 (Buffer-menu-buffer+size
650 (Buffer-menu-make-sort-button "Buffer" 2)
651 (Buffer-menu-make-sort-button "Size" 3))
653 (Buffer-menu-make-sort-button "Mode" 4) mode-end
654 (Buffer-menu-make-sort-button "File" 5) "\n"))
655 list desired-point)
656 (when Buffer-menu-use-header-line
657 (let ((pos 0))
658 ;; Turn spaces in the header into stretch specs so they work
659 ;; regardless of the header-line face.
660 (while (string-match "[ \t]+" header pos)
661 (setq pos (match-end 0))
662 (put-text-property (match-beginning 0) pos 'display
663 ;; Assume fixed-size chars
664 (list 'space :align-to pos)
665 header))))
666 (with-current-buffer (get-buffer-create "*Buffer List*")
667 (setq buffer-read-only nil)
668 (erase-buffer)
669 (setq standard-output (current-buffer))
670 (unless Buffer-menu-use-header-line
671 (insert header (propertize "---" 'face 'fixed-pitch) " ")
672 (insert (Buffer-menu-buffer+size "------" "----"))
673 (insert " ----" mode-end "----\n"))
674 (if buffer-list
675 (setq list buffer-list)
676 ;; Collect info for every buffer we're interested in.
677 (dolist (buffer (buffer-list))
678 (with-current-buffer buffer
679 (let ((name (buffer-name))
680 (file buffer-file-name))
681 (cond
682 ;; Don't mention internal buffers.
683 ((and (string= (substring name 0 1) " ") (null file)))
684 ;; Maybe don't mention buffers without files.
685 ((and files-only (not file)))
686 ((string= name "*Buffer List*"))
687 ;; Otherwise output info.
689 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
690 (if mode-line-process
691 (format-mode-line mode-line-process
692 nil nil buffer))))
693 (bits (string
694 (if (eq buffer old-buffer) ?. ?\ )
695 ;; Handle readonly status. The output buffer
696 ;; is special cased to appear readonly; it is
697 ;; actually made so at a later date.
698 (if (or (eq buffer standard-output)
699 buffer-read-only)
700 ?% ?\ )
701 ;; Identify modified buffers.
702 (if (buffer-modified-p) ?* ?\ )
703 ;; Space separator.
704 ?\ )))
705 (unless file
706 ;; No visited file. Check local value of
707 ;; list-buffers-directory.
708 (when (and (boundp 'list-buffers-directory)
709 list-buffers-directory)
710 (setq file list-buffers-directory)))
711 (push (list buffer bits name (buffer-size) mode file)
712 list)))))))
713 ;; Preserve the original buffer-list ordering, just in case.
714 (setq list (nreverse list)))
715 ;; Place the buffers's info in the output buffer, sorted if necessary.
716 (dolist (buffer
717 (if Buffer-menu-sort-column
718 (sort list
719 (if (eq Buffer-menu-sort-column 3)
720 (lambda (a b)
721 (< (nth Buffer-menu-sort-column a)
722 (nth Buffer-menu-sort-column b)))
723 (lambda (a b)
724 (string< (nth Buffer-menu-sort-column a)
725 (nth Buffer-menu-sort-column b)))))
726 list))
727 (if (eq (car buffer) old-buffer)
728 (setq desired-point (point)))
729 (insert (cadr buffer)
730 ;; Put the buffer name into a text property
731 ;; so we don't have to extract it from the text.
732 ;; This way we avoid problems with unusual buffer names.
733 (Buffer-menu-buffer+size (nth 2 buffer)
734 (int-to-string (nth 3 buffer))
735 `(buffer-name ,(nth 2 buffer)
736 buffer ,(car buffer)
737 face Buffer-menu-buffer-face
738 mouse-face highlight
739 help-echo "mouse-2: select this buffer"))
741 (if (> (length (nth 4 buffer)) Buffer-menu-mode-width)
742 (substring (nth 4 buffer) 0 Buffer-menu-mode-width)
743 (nth 4 buffer)))
744 (when (nth 5 buffer)
745 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
746 Buffer-menu-mode-width 4) 1)
747 (princ (abbreviate-file-name (nth 5 buffer))))
748 (princ "\n"))
749 (Buffer-menu-mode)
750 (when Buffer-menu-use-header-line
751 (setq header-line-format header))
752 ;; DESIRED-POINT doesn't have to be set; it is not when the
753 ;; current buffer is not displayed for some reason.
754 (and desired-point
755 (goto-char desired-point))
756 (setq Buffer-menu-files-only files-only)
757 (set-buffer-modified-p nil)
758 (current-buffer))))
760 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
761 ;;; buff-menu.el ends here