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