]> code.delx.au - gnu-emacs/blob - lisp/buff-menu.el
*** empty log message ***
[gnu-emacs] / lisp / buff-menu.el
1 ;;; buff-menu.el --- buffer menu main function and support functions -*- coding:utf-8 -*-
2
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
4 ;; 2004 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: convenience
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
33
34 ;;; Change Log:
35
36 ;; Buffer-menu-view: New function
37 ;; Buffer-menu-view-other-window: New function
38
39 ;; Merged by esr with recent mods to Emacs 19 buff-menu, 23 Mar 1993
40 ;;
41 ;; Modified by Bob Weiner, Motorola, Inc., 4/14/89
42 ;;
43 ;; Added optional backup argument to 'Buffer-menu-unmark' to make it undelete
44 ;; current entry and then move to previous one.
45 ;;
46 ;; Based on FSF code dating back to 1985.
47
48 ;;; Code:
49
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.
57
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.
60
61 ;;(defvar Buffer-menu-window-config nil
62 ;; "Window configuration saved from entry to `buffer-menu'.")
63
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.
66
67 (defgroup Buffer-menu nil
68 "Show a menu of all buffers in a buffer."
69 :group 'tools
70 :group 'convenience)
71
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)
76
77 (defface Buffer-menu-buffer-face
78 '((t (:weight bold)))
79 "Face used to highlight buffer name."
80 :group 'font-lock-highlighting-faces)
81
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)
86
87 (defcustom Buffer-menu-mode-width 16
88 "*How wide to make the mode name column."
89 :type 'number
90 :group 'Buffer-menu)
91
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.")
96
97 (defconst Buffer-menu-buffer-column 4)
98
99 (defvar Buffer-menu-mode-map nil
100 "Local keymap for `Buffer-menu-mode' buffers.")
101
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.")
107
108 (make-variable-buffer-local 'Buffer-menu-files-only)
109
110 (if Buffer-menu-mode-map
111 ()
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 )
145
146 ;; Buffer Menu mode is suitable only for specially formatted data.
147 (put 'Buffer-menu-mode 'mode-class 'special)
148
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))
192
193 ;; This function exists so we can make the doc string of Buffer-menu-mode
194 ;; look nice.
195 (defun Buffer-menu-revert ()
196 "Update the list of buffers."
197 (interactive)
198 (revert-buffer))
199
200 (defun Buffer-menu-revert-function (ignore1 ignore2)
201 (or (eq buffer-undo-list t)
202 (setq buffer-undo-list nil))
203 ;; We can not use save-excursion here. The buffer gets erased.
204 (let ((ocol (current-column))
205 (oline (progn (move-to-column 4)
206 (get-text-property (point) 'buffer)))
207 (prop (point-min))
208 ;; do not make undo records for the reversion.
209 (buffer-undo-list t))
210 (list-buffers-noselect Buffer-menu-files-only)
211 (while (setq prop (next-single-property-change prop 'buffer))
212 (when (eq (get-text-property prop 'buffer) oline)
213 (goto-char prop)
214 (move-to-column ocol)))))
215
216 (defun Buffer-menu-toggle-files-only (arg)
217 "Toggle whether the current buffer-menu displays only file buffers.
218 With a positive ARG display only file buffers. With zero or
219 negative ARG, display other buffers as well."
220 (interactive "P")
221 (setq Buffer-menu-files-only
222 (cond ((not arg) (not Buffer-menu-files-only))
223 ((> (prefix-numeric-value arg) 0) t)))
224 (revert-buffer))
225
226 \f
227 (defun Buffer-menu-buffer (error-if-non-existent-p)
228 "Return buffer described by this line of buffer menu."
229 (let* ((where (save-excursion
230 (beginning-of-line)
231 (+ (point) Buffer-menu-buffer-column)))
232 (name (and (not (eobp)) (get-text-property where 'buffer-name)))
233 (buf (and (not (eobp)) (get-text-property where 'buffer))))
234 (if name
235 (or (get-buffer name)
236 (and buf (buffer-name buf) buf)
237 (if error-if-non-existent-p
238 (error "No buffer named `%s'" name)
239 nil))
240 (or (and buf (buffer-name buf) buf)
241 (if error-if-non-existent-p
242 (error "No buffer on this line")
243 nil)))))
244 \f
245 (defun buffer-menu (&optional arg)
246 "Make a menu of buffers so you can save, delete or select them.
247 With argument, show only buffers that are visiting files.
248 Type ? after invocation to get help on commands available.
249 Type q to remove the buffer menu from the display.
250
251 The first column shows `>' for a buffer you have
252 marked to be displayed, `D' for one you have marked for
253 deletion, and `.' for the current buffer.
254
255 The C column has a `.' for the buffer from which you came.
256 The R column has a `%' if the buffer is read-only.
257 The M column has a `*' if it is modified,
258 or `S' if you have marked it for saving.
259 After this come the buffer name, its size in characters,
260 its major mode, and the visited file name (if any)."
261 (interactive "P")
262 ;;; (setq Buffer-menu-window-config (current-window-configuration))
263 (switch-to-buffer (list-buffers-noselect arg))
264 (message
265 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
266
267 (defun buffer-menu-other-window (&optional arg)
268 "Display a list of buffers in another window.
269 With the buffer list buffer, you can save, delete or select the buffers.
270 With argument, show only buffers that are visiting files.
271 Type ? after invocation to get help on commands available.
272 Type q to remove the buffer menu from the display.
273 For more information, see the function `buffer-menu'."
274 (interactive "P")
275 ;;; (setq Buffer-menu-window-config (current-window-configuration))
276 (switch-to-buffer-other-window (list-buffers-noselect arg))
277 (message
278 "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))
279
280 (defun Buffer-menu-no-header ()
281 (beginning-of-line)
282 (if (or Buffer-menu-use-header-line
283 (not (eq (char-after) ?C)))
284 t
285 (ding)
286 (forward-line 1)
287 nil))
288
289 (defun Buffer-menu-mark ()
290 "Mark buffer on this line for being displayed by \\<Buffer-menu-mode-map>\\[Buffer-menu-select] command."
291 (interactive)
292 (when (Buffer-menu-no-header)
293 (let ((buffer-read-only nil))
294 (delete-char 1)
295 (insert ?>)
296 (forward-line 1))))
297
298 (defun Buffer-menu-unmark (&optional backup)
299 "Cancel all requested operations on buffer on this line and move down.
300 Optional ARG means move up."
301 (interactive "P")
302 (when (Buffer-menu-no-header)
303 (let* ((buf (Buffer-menu-buffer t))
304 (mod (buffer-modified-p buf))
305 (readonly (save-excursion (set-buffer buf) buffer-read-only))
306 (buffer-read-only nil))
307 (delete-char 3)
308 (insert (if readonly (if mod " %*" " % ") (if mod " *" " ")))))
309 (forward-line (if backup -1 1)))
310
311 (defun Buffer-menu-backup-unmark ()
312 "Move up and cancel all requested operations on buffer on line above."
313 (interactive)
314 (forward-line -1)
315 (Buffer-menu-unmark)
316 (forward-line -1))
317
318 (defun Buffer-menu-delete (&optional arg)
319 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command.
320 Prefix arg is how many buffers to delete.
321 Negative arg means delete backwards."
322 (interactive "p")
323 (when (Buffer-menu-no-header)
324 (let ((buffer-read-only nil))
325 (if (or (null arg) (= arg 0))
326 (setq arg 1))
327 (while (> arg 0)
328 (delete-char 1)
329 (insert ?D)
330 (forward-line 1)
331 (setq arg (1- arg)))
332 (while (and (< arg 0)
333 (Buffer-menu-no-header))
334 (delete-char 1)
335 (insert ?D)
336 (forward-line -1)
337 (setq arg (1+ arg))))))
338
339 (defun Buffer-menu-delete-backwards (&optional arg)
340 "Mark buffer on this line to be deleted by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command
341 and then move up one line. Prefix arg means move that many lines."
342 (interactive "p")
343 (Buffer-menu-delete (- (or arg 1))))
344
345 (defun Buffer-menu-save ()
346 "Mark buffer on this line to be saved by \\<Buffer-menu-mode-map>\\[Buffer-menu-execute] command."
347 (interactive)
348 (when (Buffer-menu-no-header)
349 (let ((buffer-read-only nil))
350 (forward-char 2)
351 (delete-char 1)
352 (insert ?S)
353 (forward-line 1))))
354
355 (defun Buffer-menu-not-modified (&optional arg)
356 "Mark buffer on this line as unmodified (no changes to save)."
357 (interactive "P")
358 (save-excursion
359 (set-buffer (Buffer-menu-buffer t))
360 (set-buffer-modified-p arg))
361 (save-excursion
362 (beginning-of-line)
363 (forward-char 2)
364 (if (= (char-after) (if arg ? ?*))
365 (let ((buffer-read-only nil))
366 (delete-char 1)
367 (insert (if arg ?* ? ))))))
368
369 (defun Buffer-menu-beginning ()
370 (goto-char (point-min))
371 (unless Buffer-menu-use-header-line
372 (forward-line)))
373
374 (defun Buffer-menu-execute ()
375 "Save and/or delete buffers marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-save] or \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands."
376 (interactive)
377 (save-excursion
378 (Buffer-menu-beginning)
379 (while (re-search-forward "^..S" nil t)
380 (let ((modp nil))
381 (save-excursion
382 (set-buffer (Buffer-menu-buffer t))
383 (save-buffer)
384 (setq modp (buffer-modified-p)))
385 (let ((buffer-read-only nil))
386 (delete-char -1)
387 (insert (if modp ?* ? ))))))
388 (save-excursion
389 (Buffer-menu-beginning)
390 (let ((buff-menu-buffer (current-buffer))
391 (buffer-read-only nil))
392 (while (re-search-forward "^D" nil t)
393 (forward-char -1)
394 (let ((buf (Buffer-menu-buffer nil)))
395 (or (eq buf nil)
396 (eq buf buff-menu-buffer)
397 (save-excursion (kill-buffer buf)))
398 (if (and buf (buffer-name buf))
399 (progn (delete-char 1)
400 (insert ? ))
401 (delete-region (point) (progn (forward-line 1) (point)))
402 (unless (bobp)
403 (forward-char -1))))))))
404
405 (defun Buffer-menu-select ()
406 "Select this line's buffer; also display buffers marked with `>'.
407 You can mark buffers with the \\<Buffer-menu-mode-map>\\[Buffer-menu-mark] command.
408 This command deletes and replaces all the previously existing windows
409 in the selected frame."
410 (interactive)
411 (let ((buff (Buffer-menu-buffer t))
412 (menu (current-buffer))
413 (others ())
414 tem)
415 (Buffer-menu-beginning)
416 (while (re-search-forward "^>" nil t)
417 (setq tem (Buffer-menu-buffer t))
418 (let ((buffer-read-only nil))
419 (delete-char -1)
420 (insert ?\ ))
421 (or (eq tem buff) (memq tem others) (setq others (cons tem others))))
422 (setq others (nreverse others)
423 tem (/ (1- (frame-height)) (1+ (length others))))
424 (delete-other-windows)
425 (switch-to-buffer buff)
426 (or (eq menu buff)
427 (bury-buffer menu))
428 (if (equal (length others) 0)
429 (progn
430 ;;; ;; Restore previous window configuration before displaying
431 ;;; ;; selected buffers.
432 ;;; (if Buffer-menu-window-config
433 ;;; (progn
434 ;;; (set-window-configuration Buffer-menu-window-config)
435 ;;; (setq Buffer-menu-window-config nil)))
436 (switch-to-buffer buff))
437 (while others
438 (split-window nil tem)
439 (other-window 1)
440 (switch-to-buffer (car others))
441 (setq others (cdr others)))
442 (other-window 1) ;back to the beginning!
443 )))
444
445
446 \f
447 (defun Buffer-menu-visit-tags-table ()
448 "Visit the tags table in the buffer on this line. See `visit-tags-table'."
449 (interactive)
450 (let ((file (buffer-file-name (Buffer-menu-buffer t))))
451 (if file
452 (visit-tags-table file)
453 (error "Specified buffer has no file"))))
454
455 (defun Buffer-menu-1-window ()
456 "Select this line's buffer, alone, in full frame."
457 (interactive)
458 (switch-to-buffer (Buffer-menu-buffer t))
459 (bury-buffer (other-buffer))
460 (delete-other-windows))
461
462 (defun Buffer-menu-mouse-select (event)
463 "Select the buffer whose line you click on."
464 (interactive "e")
465 (let (buffer)
466 (save-excursion
467 (set-buffer (window-buffer (posn-window (event-end event))))
468 (save-excursion
469 (goto-char (posn-point (event-end event)))
470 (setq buffer (Buffer-menu-buffer t))))
471 (select-window (posn-window (event-end event)))
472 (if (and (window-dedicated-p (selected-window))
473 (eq (selected-window) (frame-root-window)))
474 (switch-to-buffer-other-frame buffer)
475 (switch-to-buffer buffer))))
476
477 (defun Buffer-menu-this-window ()
478 "Select this line's buffer in this window."
479 (interactive)
480 (switch-to-buffer (Buffer-menu-buffer t)))
481
482 (defun Buffer-menu-other-window ()
483 "Select this line's buffer in other window, leaving buffer menu visible."
484 (interactive)
485 (switch-to-buffer-other-window (Buffer-menu-buffer t)))
486
487 (defun Buffer-menu-switch-other-window ()
488 "Make the other window select this line's buffer.
489 The current window remains selected."
490 (interactive)
491 (let ((pop-up-windows t)
492 same-window-buffer-names
493 same-window-regexps)
494 (display-buffer (Buffer-menu-buffer t))))
495
496 (defun Buffer-menu-2-window ()
497 "Select this line's buffer, with previous buffer in second window."
498 (interactive)
499 (let ((buff (Buffer-menu-buffer t))
500 (menu (current-buffer))
501 (pop-up-windows t)
502 same-window-buffer-names
503 same-window-regexps)
504 (delete-other-windows)
505 (switch-to-buffer (other-buffer))
506 (pop-to-buffer buff)
507 (bury-buffer menu)))
508
509 (defun Buffer-menu-toggle-read-only ()
510 "Toggle read-only status of buffer on this line, perhaps via version control."
511 (interactive)
512 (let (char)
513 (save-excursion
514 (set-buffer (Buffer-menu-buffer t))
515 (vc-toggle-read-only)
516 (setq char (if buffer-read-only ?% ? )))
517 (save-excursion
518 (beginning-of-line)
519 (forward-char 1)
520 (if (/= (following-char) char)
521 (let (buffer-read-only)
522 (delete-char 1)
523 (insert char))))))
524
525 (defun Buffer-menu-bury ()
526 "Bury the buffer listed on this line."
527 (interactive)
528 (when (Buffer-menu-no-header)
529 (save-excursion
530 (beginning-of-line)
531 (bury-buffer (Buffer-menu-buffer t))
532 (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
533 (buffer-read-only nil))
534 (delete-region (point) (progn (forward-line -1) (point)))
535 (goto-char (point-max))
536 (insert line))
537 (message "Buried buffer moved to the end"))))
538
539
540 (defun Buffer-menu-view ()
541 "View this line's buffer in View mode."
542 (interactive)
543 (view-buffer (Buffer-menu-buffer t)))
544
545
546 (defun Buffer-menu-view-other-window ()
547 "View this line's buffer in View mode in another window."
548 (interactive)
549 (view-buffer-other-window (Buffer-menu-buffer t)))
550 \f
551
552 (define-key ctl-x-map "\C-b" 'list-buffers)
553
554 (defun list-buffers (&optional files-only)
555 "Display a list of names of existing buffers.
556 The list is displayed in a buffer named `*Buffer List*'.
557 Note that buffers with names starting with spaces are omitted.
558 Non-null optional arg FILES-ONLY means mention only file buffers.
559
560 For more information, see the function `buffer-menu'."
561 (interactive "P")
562 (display-buffer (list-buffers-noselect files-only)))
563
564 (defun Buffer-menu-buffer+size (name size &optional name-props size-props)
565 (if (> (+ (length name) (length size) 2) Buffer-menu-buffer+size-width)
566 (setq name
567 (if (string-match "<[0-9]+>$" name)
568 (concat (substring name 0
569 (- Buffer-menu-buffer+size-width
570 (max (length size) 3)
571 (match-end 0)
572 (- (match-beginning 0))
573 2))
574 ":" ; narrow ellipsis
575 (match-string 0 name))
576 (concat (substring name 0
577 (- Buffer-menu-buffer+size-width
578 (max (length size) 3)
579 2))
580 ":"))) ; narrow ellipsis
581 ;; Don't put properties on (buffer-name).
582 (setq name (copy-sequence name)))
583 (add-text-properties 0 (length name) name-props name)
584 (add-text-properties 0 (length size) size-props size)
585 (concat name
586 (make-string (- Buffer-menu-buffer+size-width
587 (length name)
588 (length size))
589 ? )
590 size))
591
592 (defun Buffer-menu-sort (column)
593 "Sort the buffer menu by COLUMN."
594 (interactive "P")
595 (when column
596 (setq column (prefix-numeric-value column))
597 (if (< column 2) (setq column 2))
598 (if (> column 5) (setq column 5)))
599 (setq Buffer-menu-sort-column column)
600 (let (buffer-read-only l buf m1 m2)
601 (save-excursion
602 (Buffer-menu-beginning)
603 (while (not (eobp))
604 (when (buffer-live-p (setq buf (get-text-property (+ (point) 4) 'buffer)))
605 (setq m1 (char-after)
606 m1 (if (memq m1 '(?> ?D)) m1)
607 m2 (char-after (+ (point) 2))
608 m2 (if (eq m2 ?S) m2))
609 (if (or m1 m2)
610 (push (list buf m1 m2) l)))
611 (forward-line)))
612 (Buffer-menu-revert)
613 (setq buffer-read-only)
614 (save-excursion
615 (Buffer-menu-beginning)
616 (while (not (eobp))
617 (when (setq buf (assq (get-text-property (+ (point) 4) 'buffer) l))
618 (setq m1 (cadr buf)
619 m2 (cadr (cdr buf)))
620 (when m1
621 (delete-char 1)
622 (insert m1)
623 (backward-char 1))
624 (when m2
625 (forward-char 2)
626 (delete-char 1)
627 (insert m2)))
628 (forward-line)))))
629
630 (defun Buffer-menu-make-sort-button (name column)
631 (if (equal column Buffer-menu-sort-column) (setq column nil))
632 (propertize name
633 'help-echo (if column
634 (concat "mouse-2: sort by " (downcase name))
635 "mouse-2: sort by visited order")
636 'mouse-face 'highlight
637 'keymap (let ((map (make-sparse-keymap)))
638 (define-key map [header-line mouse-2]
639 `(lambda (e)
640 (interactive "e")
641 (save-window-excursion
642 (if e (mouse-select-window e))
643 (Buffer-menu-sort ,column))))
644 map)))
645
646 (defun list-buffers-noselect (&optional files-only buffer-list)
647 "Create and return a buffer with a list of names of existing buffers.
648 The buffer is named `*Buffer List*'.
649 Note that buffers with names starting with spaces are omitted.
650 Non-null optional arg FILES-ONLY means mention only file buffers.
651
652 If BUFFER-LIST is non-nil, it should be a list of buffers;
653 it means list those buffers and no others.
654
655 For more information, see the function `buffer-menu'."
656 (let* ((old-buffer (current-buffer))
657 (standard-output standard-output)
658 (mode-end (make-string (- Buffer-menu-mode-width 2) ? ))
659 (header (concat "CRM "
660 (Buffer-menu-buffer+size
661 (Buffer-menu-make-sort-button "Buffer" 2)
662 (Buffer-menu-make-sort-button "Size" 3))
663 " "
664 (Buffer-menu-make-sort-button "Mode" 4) mode-end
665 (Buffer-menu-make-sort-button "File" 5) "\n"))
666 list desired-point)
667 (when Buffer-menu-use-header-line
668 (let ((pos 0))
669 ;; Turn spaces in the header into stretch specs so they work
670 ;; regardless of the header-line face.
671 (while (string-match "[ \t]+" header pos)
672 (setq pos (match-end 0))
673 (put-text-property (match-beginning 0) pos 'display
674 ;; Assume fixed-size chars in the buffer.
675 (list 'space :align-to pos)
676 header)))
677 ;; Try to better align the one-char headers.
678 (put-text-property 0 3 'face 'fixed-pitch header)
679 ;; Add a "dummy" leading space to align the beginning of the header
680 ;; line with the beginning of the text (rather than with the left
681 ;; scrollbar or the left fringe). –-Stef
682 (setq header (concat (propertize " " 'display '(space :align-to 0))
683 header)))
684 (with-current-buffer (get-buffer-create "*Buffer List*")
685 (setq buffer-read-only nil)
686 (erase-buffer)
687 (setq standard-output (current-buffer))
688 (unless Buffer-menu-use-header-line
689 (let ((underline (if (char-displayable-p ?—) ?— ?-)))
690 (insert header
691 (apply 'string
692 (mapcar (lambda (c)
693 (if (memq c '(?\n ?\ )) c underline))
694 header)))))
695 ;; Collect info for every buffer we're interested in.
696 (dolist (buffer (or buffer-list (buffer-list)))
697 (with-current-buffer buffer
698 (let ((name (buffer-name))
699 (file buffer-file-name))
700 (unless (and (not buffer-list)
701 (or
702 ;; Don't mention internal buffers.
703 (and (string= (substring name 0 1) " ") (null file))
704 ;; Maybe don't mention buffers without files.
705 (and files-only (not file))
706 (string= name "*Buffer List*")))
707 ;; Otherwise output info.
708 (let ((mode (concat (format-mode-line mode-name nil nil buffer)
709 (if mode-line-process
710 (format-mode-line mode-line-process
711 nil nil buffer))))
712 (bits (string
713 (if (eq buffer old-buffer) ?. ?\ )
714 ;; Handle readonly status. The output buffer
715 ;; is special cased to appear readonly; it is
716 ;; actually made so at a later date.
717 (if (or (eq buffer standard-output)
718 buffer-read-only)
719 ?% ?\ )
720 ;; Identify modified buffers.
721 (if (buffer-modified-p) ?* ?\ )
722 ;; Space separator.
723 ?\ )))
724 (unless file
725 ;; No visited file. Check local value of
726 ;; list-buffers-directory.
727 (when (and (boundp 'list-buffers-directory)
728 list-buffers-directory)
729 (setq file list-buffers-directory)))
730 (push (list buffer bits name (buffer-size) mode file)
731 list))))))
732 ;; Preserve the original buffer-list ordering, just in case.
733 (setq list (nreverse list))
734 ;; Place the buffers's info in the output buffer, sorted if necessary.
735 (dolist (buffer
736 (if Buffer-menu-sort-column
737 (sort list
738 (if (eq Buffer-menu-sort-column 3)
739 (lambda (a b)
740 (< (nth Buffer-menu-sort-column a)
741 (nth Buffer-menu-sort-column b)))
742 (lambda (a b)
743 (string< (nth Buffer-menu-sort-column a)
744 (nth Buffer-menu-sort-column b)))))
745 list))
746 (if (eq (car buffer) old-buffer)
747 (setq desired-point (point)))
748 (insert (cadr buffer)
749 ;; Put the buffer name into a text property
750 ;; so we don't have to extract it from the text.
751 ;; This way we avoid problems with unusual buffer names.
752 (Buffer-menu-buffer+size (nth 2 buffer)
753 (int-to-string (nth 3 buffer))
754 `(buffer-name ,(nth 2 buffer)
755 buffer ,(car buffer)
756 face Buffer-menu-buffer-face
757 mouse-face highlight
758 help-echo "mouse-2: select this buffer"))
759 " "
760 (if (> (length (nth 4 buffer)) Buffer-menu-mode-width)
761 (substring (nth 4 buffer) 0 Buffer-menu-mode-width)
762 (nth 4 buffer)))
763 (when (nth 5 buffer)
764 (indent-to (+ Buffer-menu-buffer-column Buffer-menu-buffer+size-width
765 Buffer-menu-mode-width 4) 1)
766 (princ (abbreviate-file-name (nth 5 buffer))))
767 (princ "\n"))
768 (Buffer-menu-mode)
769 (when Buffer-menu-use-header-line
770 (setq header-line-format header))
771 ;; DESIRED-POINT doesn't have to be set; it is not when the
772 ;; current buffer is not displayed for some reason.
773 (and desired-point
774 (goto-char desired-point))
775 (setq Buffer-menu-files-only files-only)
776 (set-buffer-modified-p nil)
777 (current-buffer))))
778
779 ;; arch-tag: e7dfcfc9-6cb2-46e4-bf55-8ef1936d83c6
780 ;;; buff-menu.el ends here