root/trunk/lisp/bs.el

Revision 4220, 55.7 kB (checked in by miyoshi, 8 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; bs.el --- menu for selecting and displaying buffers
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
6 ;; Maintainer: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
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 3, 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; Version: 1.17
29 ;; X-URL: http://www.geekware.de/software/emacs
30 ;;
31 ;; The bs-package contains a main function bs-show for poping up a
32 ;; buffer in a way similar to `list-buffers' and `electric-buffer-list':
33 ;; The new buffer offers a Buffer Selection Menu for manipulating
34 ;; the buffer list and buffers.
35 ;;
36 ;; -----------------------------------------------------------------------
37 ;; | MR Buffer          Size  Mode          File                         |
38 ;; | -- ------          ----  ----          ----                         |
39 ;; |.   bs.el           14690  Emacs-Lisp    /home/sun/sylvester/el/bs.e$|
40 ;; |  % executable.el    9429  Emacs-Lisp    /usr/share/emacs/19.34/lisp$|
41 ;; |  % vc.el          104893  Emacs-Lisp    /usr/share/emacs/19.34/lisp$|
42 ;; |  % test_vc.el        486  Emacs-Lisp    /home/sun/sylvester/el/test$|
43 ;; |  % vc-hooks.el     43605  Emacs-Lisp    /usr/share/emacs/19.34/lisp$|
44 ;; -----------------------------------------------------------------------
45
46 ;;; Quick Installation und Customization:
47
48 ;; Use
49 ;;   M-x bs-show
50 ;; for buffer selection or optional bind a key to main function `bs-show'
51 ;;   (global-set-key "\C-x\C-b" 'bs-show)    ;; or another key
52 ;;
53 ;; For customization use
54 ;; M-x bs-customize
55
56
57 ;;; More Commentary:
58
59 ;; bs-show will generate a new buffer named *buffer-selection*, which shows
60 ;; all buffers or a subset of them, and has possibilities for deleting,
61 ;; saving and selecting buffers. For more details see docstring of
62 ;; function `bs-mode'. A current configuration describes which buffers appear
63 ;; in *buffer-selection*. See docstring of variable `bs-configurations' for
64 ;; more details.
65 ;;
66 ;; The package bs combines the advantages of the Emacs functions
67 ;; `list-buffers' and `electric-buffer-list'.
68 ;;
69 ;; Additional features for Buffer Selection Menu:
70 ;;  - configurable list of buffers (show only files etc.).
71 ;;  - comfortable way to change displayed subset of all buffers.
72 ;;  - show sorted list of buffers.
73 ;;  - cyclic navigation:
74 ;;     - goes to top of buffer list if you are on last line and press down.
75 ;;     - goes to end of buffer list if you are on first line and press up.
76 ;;  - Offer an alternative buffer list by prefix key C-u.
77
78 ;;; Cycling through buffers
79
80 ;; This package offers two functions for buffer cycling. If you want to cycle
81 ;; through buffer list you can use `bs-cycle-next' or `bs-cycle-previous'.
82 ;; Bind these function to a key like
83 ;;   (global-set-key [(f9)]   'bs-cycle-previous)
84 ;;   (global-set-key [(f10)]  'bs-cycle-next)
85 ;;
86 ;; Both functions use a special subset of all buffers for cycling to avoid
87 ;; to go through internal buffers like *Messages*.
88 ;;
89 ;; Cycling through buffers ignores sorting because sorting destroys
90 ;; the logical buffer list. If buffer list is sorted by size you
91 ;; won't be able to cycle to the smallest buffer.
92
93 ;;; Customization:
94
95 ;; There is a customization group called `bs' in group `convenience'.
96 ;; Start customization by M-x bs-customize
97 ;;
98 ;; Buffer list
99 ;; -----------
100 ;; You can define your own configurations by extending variable
101 ;; `bs-configurations' (see docstring for details).
102 ;;
103 ;; `bs-default-configuration' contains the name of default configuration.
104 ;; The default value is "files" which means to show only files.
105 ;;
106 ;; If you always want to see all buffers, customize variable
107 ;; `bs-default-configuration' in customization group `bs'.
108 ;;
109 ;; Configure sorting
110 ;; -----------------
111 ;; You can define functions for sorting the buffer list.
112 ;; When selecting buffers, you can step through available sorting
113 ;; methods with key 'S'.
114 ;; To define a new way of sorting, customize variable `bs-sort-functions'.
115 ;;
116 ;; There are four basic functions for sorting:
117 ;;   by buffer name, by mode, by size, or by filename
118 ;;
119 ;; Configure buffer cycling
120 ;; ------------------------
121 ;; When cycling through buffer list the functions for cycling will use
122 ;; the current configuration of bs to calculate the buffer list.
123 ;; If you want to use a different configuration for cycling you have to set
124 ;; the variable `bs-cycle-configuration-name'. You can customize this variable.
125 ;;
126 ;; For example: If you use the configuration called "files-and-scratch" you
127 ;; can cycle through all file buffers and *scratch* although your current
128 ;; configuration perhaps is "files" which ignores buffer *scratch*.
129
130 ;;; History:
131
132 ;;; Code:
133
134 ;; ----------------------------------------------------------------------
135 ;; Globals for customization
136 ;; ----------------------------------------------------------------------
137
138 (defgroup bs nil
139   "Buffer Selection: Maintaining buffers by buffer menu."
140   :version "21.1"
141   :link '(emacs-commentary-link "bs")
142   :link '(url-link "http://www.geekware.de/software/emacs")
143   :group 'convenience)
144
145 (defgroup bs-appearance nil
146   "Buffer Selection appearance: Appearance of bs buffer menu."
147   :group 'bs)
148
149 (defcustom bs-attributes-list
150   '((""       1   1 left  bs--get-marked-string)
151     ("M"      1   1 left  bs--get-modified-string)
152     ("R"      2   2 left  bs--get-readonly-string)
153     ("Buffer" bs--get-name-length 10 left  bs--get-name)
154     (""       1   1 left  " ")
155     ("Size"   8   8 right bs--get-size-string)
156     (""       1   1 left  " ")
157     ("Mode"   12 12 right bs--get-mode-name)
158     (""       2   2 left  "  ")
159     ("File"   12 12 left  bs--get-file-name)
160     (""       2   2 left  "  "))
161   "*List specifying the layout of a Buffer Selection Menu buffer.
162 Each entry specifies a column and is a list of the form of:
163 \(HEADER MINIMUM-LENGTH MAXIMUM-LENGTH ALIGNMENT FUN-OR-STRING)
164
165 HEADER         : String for header for first line or a function
166                  which calculates column title.
167 MINIMUM-LENGTH : Minimum width of column (number or name of function).
168                  The function must return a positive integer.
169 MAXIMUM-LENGTH : Maximum width of column (number or name of function)
170                  (currently ignored).
171 ALIGNMENT      : Alignment of column (`left', `right', `middle').
172 FUN-OR-STRING  : Name of a function for calculating the value or a
173                  string for a constant value.
174
175 The function gets as parameter the buffer where we have started
176 buffer selection and the list of all buffers to show.  The function must
177 return a string representing the column's value."
178   :group 'bs-appearance
179   :type '(repeat sexp))
180
181 (defun bs--make-header-match-string ()
182   "Return a regexp matching the first line of a Buffer Selection Menu buffer."
183   (let ((res "^\\(")
184         (ele bs-attributes-list))
185     (while ele
186       (setq res (concat res (car (car ele)) " *"))
187       (setq ele (cdr ele)))
188     (concat res "$\\)")))
189
190 ;; Font-Lock-Settings
191 (defvar bs-mode-font-lock-keywords
192   (list ;; header in font-lock-type-face
193    (list (bs--make-header-match-string)
194          '(1 font-lock-type-face append) '(1 'bold append))
195    ;; Buffername embedded by *
196    (list "^\\(.*\\*.*\\*.*\\)$"
197          1
198          ;; problem in XEmacs with font-lock-constant-face
199          (if (facep 'font-lock-constant-face)
200              'font-lock-constant-face
201            'font-lock-comment-face))
202    ;; Dired-Buffers
203    '("^..\\(.*Dired by .*\\)$" 1 font-lock-function-name-face)
204    ;; the star for modified buffers
205    '("^.\\(\\*\\) +[^\\*]"     1 font-lock-comment-face))
206   "Default font lock expressions for Buffer Selection Menu.")
207
208 (defcustom bs-max-window-height 20
209   "*Maximal window height of Buffer Selection Menu."
210   :group 'bs-appearance
211   :type 'integer)
212
213 (defvar bs-dont-show-regexp nil
214   "Regular expression specifying which buffers not to show.
215 A buffer whose name matches this regular expression will not be
216 included in the buffer list.")
217
218 (defvar bs-must-show-regexp nil
219   "Regular expression for specifying buffers which must be shown.
220 A buffer whose name matches this regular expression will be
221 included in the buffer list.
222 Note that this variable is temporary: if the configuration is changed
223 it is reset to nil.  Use `bs-must-always-show-regexp' to specify buffers
224 that must always be shown regardless of the configuration.")
225
226 (defcustom bs-must-always-show-regexp nil
227   "*Regular expression for specifying buffers to show always.
228 A buffer whose name matches this regular expression will
229 be shown regardless of current configuration of Buffer Selection Menu."
230   :group 'bs
231   :type '(choice (const :tag "Nothing at all" nil) regexp))
232
233 (defvar bs-dont-show-function nil
234   "Function for specifying buffers not to show.
235 The function gets one argument - the buffer to test.  The function must
236 return a value different from nil to ignore the buffer in
237 Buffer Selection Menu.")
238
239 (defvar bs-must-show-function nil
240   "Function for specifying buffers which must be shown.
241 The function gets one argument - the buffer to test.")
242
243 (defvar bs-buffer-sort-function nil
244   "Sort function to sort the buffers that appear in Buffer Selection Menu.
245 The function gets two arguments - the buffers to compare.
246 It must return non-nil if the first buffer should sort before the second.")
247
248 (defcustom bs-maximal-buffer-name-column 45
249   "*Maximum column width for buffer names.
250 The column for buffer names has dynamic width.  The width depends on
251 maximal and minimal length of names of buffers to show.  The maximal
252 width is bounded by `bs-maximal-buffer-name-column'.
253 See also `bs-minimal-buffer-name-column'."
254   :group 'bs-appearance
255   :type 'integer)
256
257 (defcustom bs-minimal-buffer-name-column 15
258   "*Minimum column width for buffer names.
259 The column for buffer names has dynamic width.  The width depends on
260 maximal and minimal length of names of buffers to show.  The minimal
261 width is bounded by `bs-minimal-buffer-name-column'.
262 See also `bs-maximal-buffer-name-column'."
263   :group 'bs-appearance
264   :type 'integer)
265
266 (defconst bs-header-lines-length 2
267   "Number of lines for headers in Buffer Selection Menu.")
268
269 (defcustom bs-configurations
270   '(("all" nil nil nil nil nil)
271     ("files" nil nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)
272     ("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file
273      bs-sort-buffer-interns-are-last)
274     ("all-intern-last" nil nil nil nil bs-sort-buffer-interns-are-last))
275   "*List of all configurations you can use in the Buffer Selection Menu.
276 A configuration describes which buffers appear in Buffer Selection Menu
277 and also the order of buffers.  A configuration is a list with
278 six elements.  The first element is a string and describes the configuration.
279 The following five elements represent the values for Buffer Selection Menu
280 configuration variables `bs-must-show-regexp', `bs-must-show-function',
281 `bs-dont-show-regexp', `bs-dont-show-function' and `bs-buffer-sort-function'.
282 By setting these variables you define a configuration."
283   :group 'bs-appearance
284   :type '(repeat sexp))
285
286 (defcustom bs-default-configuration "files"
287   "*Name of default configuration used by the Buffer Selection Menu.
288 \\<bs-mode-map>
289 Will be changed using key \\[bs-select-next-configuration].
290 Must be a string used in `bs-configurations' for naming a configuration."
291   :group 'bs
292   :type 'string)
293
294 (defcustom bs-alternative-configuration "all"
295   "*Name of configuration used when calling `bs-show' with \
296 \\[universal-argument] as prefix key.
297 Must be a string used in `bs-configurations' for naming a configuration."
298   :group 'bs
299   :type  'string)
300
301 (defvar bs-current-configuration bs-default-configuration
302   "Name of current configuration.
303 Must be a string used in `bs-configurations' for naming a configuration.")
304
305 (defcustom bs-cycle-configuration-name nil
306   "*Name of configuration used when cycling through the buffer list.
307 A value of nil means to use current configuration `bs-default-configuration'.
308 Must be a string used in `bs-configurations' for naming a configuration."
309   :group 'bs
310   :type '(choice (const :tag "like current configuration" nil)
311    string))
312
313 (defcustom bs-string-show-always "+"
314   "*String added in column 1 indicating a buffer will always be shown."
315   :group 'bs-appearance
316   :type 'string)
317
318 (defcustom bs-string-show-never "-"
319   "*String added in column 1 indicating a buffer will never be shown."
320   :group 'bs-appearance
321   :type 'string)
322
323 (defcustom bs-string-current "."
324   "*String added in column 1 indicating the current buffer."
325   :group 'bs-appearance
326   :type 'string)
327
328 (defcustom bs-string-current-marked "#"
329   "*String added in column 1 indicating the current buffer when it is marked."
330   :group 'bs-appearance
331   :type 'string)
332
333 (defcustom bs-string-marked ">"
334   "*String added in column 1 indicating a marked buffer."
335   :group 'bs-appearance
336   :type 'string)
337
338 (defcustom bs-string-show-normally  " "
339   "*String added in column 1 indicating an unmarked buffer."
340   :group 'bs-appearance
341   :type 'string)
342
343 (defvar bs--name-entry-length 20
344   "Maximum length of all displayed buffer names.
345 Used internally, only.")
346
347 ;; ----------------------------------------------------------------------
348 ;; Internal globals
349 ;; ----------------------------------------------------------------------
350
351 (defvar bs-buffer-show-mark nil
352   "Flag for the current mode for showing this buffer.
353 A value of nil means buffer will be shown depending on the current
354 configuration.
355 A value of `never' means to never show the buffer.
356 A value of `always' means to show buffer regardless of the configuration.")
357
358 (make-variable-buffer-local 'bs-buffer-show-mark)
359
360 ;; Make face named region (for XEmacs)
361 (unless (facep 'region)
362   (make-face 'region)
363   (set-face-background 'region "gray75"))
364
365 (defun bs--sort-by-name (b1 b2)
366   "Compare buffers B1 and B2 by buffer name."
367   (string< (buffer-name b1)
368            (buffer-name b2)))
369
370 (defun bs--sort-by-filename (b1 b2)
371   "Compare buffers B1 and B2 by file name."
372   (string< (or (buffer-file-name b1) "")
373            (or (buffer-file-name b2) "")))
374
375 (defun bs--sort-by-mode (b1 b2)
376   "Compare buffers B1 and B2 by mode name."
377   (save-excursion
378     (string< (progn (set-buffer b1) (format "%s" mode-name))
379              (progn (set-buffer b2) (format "%s" mode-name)))))
380
381 (defun bs--sort-by-size (b1 b2)
382   "Compare buffers B1 and B2 by buffer size."
383   (save-excursion
384     (< (progn (set-buffer b1) (buffer-size))
385        (progn (set-buffer b2) (buffer-size)))))
386
387 (defcustom bs-sort-functions
388   '(("by name"     bs--sort-by-name     "Buffer" region)
389     ("by size"     bs--sort-by-size     "Size"   region)
390     ("by mode"     bs--sort-by-mode     "Mode"   region)
391     ("by filename" bs--sort-by-filename "File"   region)
392     ("by nothing"  nil                  nil      nil))
393   "*List of all possible sorting aspects for Buffer Selection Menu.
394 You can add a new entry with a call to `bs-define-sort-function'.
395 Each element is a list of four elements (NAME FUNCTION REGEXP-FOR-SORTING FACE).
396 NAME specifies the sort order defined by function FUNCTION.
397 FUNCTION nil means don't sort the buffer list.  Otherwise the function
398 must have two parameters - the buffers to compare.
399 REGEXP-FOR-SORTING is a regular expression which describes the
400 column title to highlight.
401 FACE is a face used to fontify the sorted column title.  A value of nil means
402 don't highlight."
403   :group 'bs
404   :type '(repeat sexp))
405
406 (defun bs-define-sort-function (name fun &optional regexp-for-sorting face)
407   "Define a new function for buffer sorting in Buffer Selection Menu.
408 NAME specifies the sort order defined by function FUN.
409 A value of nil for FUN means don't sort the buffer list.  Otherwise the
410 functions must have two parameters - the buffers to compare.
411 REGEXP-FOR-SORTING is a regular expression which describes the
412 column title to highlight.
413 FACE is a face used to fontify the sorted column title.  A value of nil means
414 don't highlight.
415 The new sort aspect will be inserted into list `bs-sort-functions'."
416   (let ((tupel (assoc name bs-sort-functions)))
417     (if tupel
418         (setcdr tupel (list fun regexp-for-sorting face))
419       (setq bs-sort-functions
420             (cons (list name fun regexp-for-sorting face)
421                   bs-sort-functions)))))
422
423 (defvar bs--current-sort-function nil
424   "Description of the current function for sorting the buffer list.
425 This is an element of `bs-sort-functions'.")
426
427 (defcustom bs-default-sort-name "by nothing"
428   "*Name of default sort behavior.
429 Must be \"by nothing\" or a string used in `bs-sort-functions' for
430 naming a sort behavior.  Default is \"by nothing\" which means no sorting."
431   :group 'bs
432   :type  'string
433   :set (lambda (var-name value)
434          (set var-name value)
435          (setq bs--current-sort-function
436                (assoc value bs-sort-functions))))
437
438 (defvar bs--buffer-coming-from nil
439   "The buffer in which the user started the current Buffer Selection Menu.")
440
441 (defvar bs--show-all nil
442   "Flag whether showing all buffers regardless of current configuration.
443 Non-nil means to show all buffers.  Otherwise show buffers
444 defined by current configuration `bs-current-configuration'.")
445
446 (defvar bs--window-config-coming-from nil
447   "Window configuration before starting Buffer Selection Menu.")
448
449 (defvar bs--intern-show-never "^ \\|\\*buffer-selection\\*"
450   "Regular expression specifying which buffers never to show.
451 A buffer whose name matches this regular expression will never be
452 included in the buffer list.")
453
454 (defvar bs-current-list nil
455   "List of buffers shown in Buffer Selection Menu.
456 Used internally, only.")
457
458 (defvar bs--marked-buffers nil
459   "Currently marked buffers in Buffer Selection Menu.")
460
461 (defvar bs-mode-map
462   (let ((map (make-sparse-keymap)))
463     (define-key map " "       'bs-select)
464     (define-key map "f"       'bs-select)
465     (define-key map "v"       'bs-view)
466     (define-key map "!"       'bs-select-in-one-window)
467     (define-key map [mouse-2] 'bs-mouse-select) ;; for GNU EMACS
468     (define-key map [button2] 'bs-mouse-select) ;; for XEmacs
469     (define-key map "F"       'bs-select-other-frame)
470     (let ((key ?1))
471       (while (<= key ?9)
472         (define-key map (char-to-string key) 'digit-argument)
473         (setq key (1+ key))))
474     (define-key map "-"       'negative-argument)
475     (define-key map "\e-"     'negative-argument)
476     (define-key map "o"       'bs-select-other-window)
477     (define-key map "\C-o"    'bs-tmp-select-other-window)
478     ;; for GNU EMACS
479     (define-key map [mouse-3] 'bs-mouse-select-other-frame)
480     ;; for XEmacs
481     (define-key map [button3] 'bs-mouse-select-other-frame)
482     (define-key map [up]      'bs-up)
483     (define-key map "n"       'bs-down)
484     (define-key map "p"       'bs-up)
485     (define-key map [down]    'bs-down)
486     (define-key map "\C-m"    'bs-select)
487     (define-key map "b"       'bs-bury-buffer)
488     (define-key map "s"       'bs-save)
489     (define-key map "S"       'bs-show-sorted)
490     (define-key map "a"       'bs-toggle-show-all)
491     (define-key map "d"       'bs-delete)
492     (define-key map "\C-d"    'bs-delete-backward)
493     (define-key map "k"       'bs-delete)
494     (define-key map "g"       'bs-refresh)
495     (define-key map "C"       'bs-set-configuration-and-refresh)
496     (define-key map "c"       'bs-select-next-configuration)
497     (define-key map "q"       'bs-kill)
498     ;; (define-key map "z"       'bs-kill)
499     (define-key map "\C-c\C-c" 'bs-kill)
500     (define-key map "\C-g"    'bs-abort)
501     (define-key map "\C-]"    'bs-abort)
502     (define-key map "%"       'bs-toggle-readonly)
503     (define-key map "~"       'bs-clear-modified)
504     (define-key map "M"       'bs-toggle-current-to-show)
505     (define-key map "+"       'bs-set-current-buffer-to-show-always)
506     ;;(define-key map "-"       'bs-set-current-buffer-to-show-never)
507     (define-key map "t"       'bs-visit-tags-table)
508     (define-key map "m"       'bs-mark-current)
509     (define-key map "u"       'bs-unmark-current)
510     (define-key map ">"       'scroll-right)
511     (define-key map "<"       'scroll-left)
512     (define-key map "?"       'bs-help)
513     map)
514   "Keymap of `bs-mode'.")
515
516 ;; ----------------------------------------------------------------------
517 ;; Functions
518 ;; ----------------------------------------------------------------------
519
520 (defun bs-buffer-list (&optional list sort-description)
521   "Return a list of buffers to be shown.
522 LIST is a list of buffers to test for appearance in Buffer Selection Menu.
523 The result list depends on the global variables `bs-dont-show-regexp',
524 `bs-must-show-regexp', `bs-dont-show-function', `bs-must-show-function'
525 and `bs-buffer-sort-function'.
526 If SORT-DESCRIPTION isn't nil the list will be sorted by
527 a special function.  SORT-DESCRIPTION is an element of `bs-sort-functions'."
528   (setq sort-description (or sort-description bs--current-sort-function)
529         list (or list (buffer-list)))
530   (let ((result nil))
531     (while list
532       (let* ((buffername (buffer-name (car list)))
533              (int-show-never (string-match bs--intern-show-never buffername))
534              (ext-show-never (and bs-dont-show-regexp
535                                   (string-match bs-dont-show-regexp
536                                                 buffername)))
537              (extern-must-show (or (and bs-must-always-show-regexp
538                                         (string-match
539                                          bs-must-always-show-regexp
540                                          buffername))
541                                    (and bs-must-show-regexp
542                                         (string-match bs-must-show-regexp
543                                                       buffername))))
544              (extern-show-never-from-fun (and bs-dont-show-function
545                                               (funcall bs-dont-show-function
546                                                        (car list))))
547              (extern-must-show-from-fun (and bs-must-show-function
548                                              (funcall bs-must-show-function
549                                                       (car list))))
550              (show-flag (buffer-local-value 'bs-buffer-show-mark (car list))))
551         (if (or (eq show-flag 'always)
552                 (and (or bs--show-all (not (eq show-flag 'never)))
553                      (not int-show-never)
554                      (or bs--show-all
555                          extern-must-show
556                          extern-must-show-from-fun
557                          (and (not ext-show-never)
558                               (not extern-show-never-from-fun)))))
559             (setq result (cons (car list)
560                                result)))
561         (setq list (cdr list))))
562     (setq result (reverse result))
563     ;; The current buffer which was the start point of bs should be an element
564     ;; of result list, so that we can leave with space and be back in the
565     ;; buffer we started bs-show.
566     (if (and bs--buffer-coming-from
567              (buffer-live-p bs--buffer-coming-from)
568              (not (memq bs--buffer-coming-from result)))
569         (setq result (cons bs--buffer-coming-from result)))
570     ;; sorting
571     (if (and sort-description
572              (nth 1 sort-description))
573         (setq result (sort result (nth 1 sort-description)))
574       ;; else standard sorting
575       (bs-buffer-sort result))))
576
577 (defun bs-buffer-sort (buffer-list)
578   "Sort buffers in BUFFER-LIST according to `bs-buffer-sort-function'."
579   (if bs-buffer-sort-function
580       (sort buffer-list bs-buffer-sort-function)
581     buffer-list))
582
583 (defun bs--redisplay (&optional keep-line-p sort-description)
584   "Redisplay whole Buffer Selection Menu.
585 If KEEP-LINE-P is non-nil the point will stay on current line.
586 SORT-DESCRIPTION is an element of `bs-sort-functions'."
587   (let ((line (1+ (count-lines 1 (point)))))
588     (bs-show-in-buffer (bs-buffer-list nil sort-description))
589     (if keep-line-p
590         (goto-line line))
591     (beginning-of-line)))
592
593 (defun bs--goto-current-buffer ()
594   "Goto line which represents the current buffer;
595 actually the line which begins with character in `bs-string-current' or
596 `bs-string-current-marked'."
597   (let ((regexp (concat "^"
598                         (regexp-quote bs-string-current)
599                         "\\|^"
600                         (regexp-quote bs-string-current-marked)))
601         point)
602     (save-excursion
603       (goto-char (point-min))
604       (if (search-forward-regexp regexp nil t)
605           (setq point (- (point) 1))))
606     (if point
607         (goto-char point))))
608
609 (defun bs--current-config-message ()
610   "Return a string describing the current `bs-mode' configuration."
611   (if bs--show-all
612       "Show all buffers."
613     (format "Show buffer by configuration %S"
614             bs-current-configuration)))
615
616 (defun bs--track-window-changes (frame)
617   "Track window changes to refresh the buffer list.
618 Used from `window-size-change-functions'."
619   (let ((win (get-buffer-window "*buffer-selection*" frame)))
620     (when win
621       (with-selected-window win
622         (bs--set-window-height)))))
623
624 (defun bs--remove-hooks ()
625   "Remove `bs--track-window-changes' and auxiliary hooks."
626   (remove-hook 'window-size-change-functions 'bs--track-window-changes)
627   ;; Remove itself
628   (remove-hook 'kill-buffer-hook 'bs--remove-hooks t)
629   (remove-hook 'change-major-mode-hook 'bs--remove-hooks t))
630
631 (put 'bs-mode 'mode-class 'special)
632
633 (define-derived-mode bs-mode () "Buffer-Selection-Menu"
634   "Major mode for editing a subset of Emacs' buffers.
635 \\<bs-mode-map>
636 Aside from two header lines each line describes one buffer.
637 Move to a line representing the buffer you want to edit and select
638 buffer by \\[bs-select] or SPC.  Abort buffer list with \\[bs-kill].
639 There are many key commands similar to `Buffer-menu-mode' for
640 manipulating the buffer list and buffers.
641 For faster navigation each digit key is a digit argument.
642
643 \\[bs-select] or SPACE -- select current line's buffer and other marked buffers.
644 \\[bs-toggle-show-all]  -- toggle between all buffers and a special subset.
645 \\[bs-select-other-window] -- select current line's buffer in other window.
646 \\[bs-tmp-select-other-window] -- make another window display that buffer and
647     remain in Buffer Selection Menu.
648 \\[bs-mouse-select] -- select current line's buffer and other marked buffers.
649 \\[bs-save] -- save current line's buffer immediately.
650 \\[bs-delete] -- kill current line's buffer immediately.
651 \\[bs-toggle-readonly] -- toggle read-only status of current line's buffer.
652 \\[bs-clear-modified] -- clear modified-flag on that buffer.
653 \\[bs-mark-current] -- mark current line's buffer to be displayed.
654 \\[bs-unmark-current] -- unmark current line's buffer to be displayed.
655 \\[bs-show-sorted] -- display buffer list sorted by next sort aspect.
656 \\[bs-set-configuration-and-refresh] -- ask user for a configuration and \
657 apply selected configuration.
658 \\[bs-select-next-configuration] -- select and apply next \
659 available Buffer Selection Menu configuration.
660 \\[bs-kill] -- leave Buffer Selection Menu without a selection.
661 \\[bs-toggle-current-to-show] -- toggle status of appearance.
662 \\[bs-set-current-buffer-to-show-always] -- mark current line's buffer \
663 to show always.
664 \\[bs-visit-tags-table] -- call `visit-tags-table' on current line's buffer.
665 \\[bs-help] -- display this help text."
666   (make-local-variable 'font-lock-defaults)
667   (make-local-variable 'font-lock-verbose)
668   (make-local-variable 'font-lock-global-modes)
669   (buffer-disable-undo)
670   (setq buffer-read-only t
671         truncate-lines t
672         show-trailing-whitespace nil
673         font-lock-global-modes '(not bs-mode)
674         font-lock-defaults '(bs-mode-font-lock-keywords t)
675         font-lock-verbose nil)
676   (add-hook 'window-size-change-functions 'bs--track-window-changes)
677   (add-hook 'kill-buffer-hook 'bs--remove-hooks nil t)
678   (add-hook 'change-major-mode-hook 'bs--remove-hooks nil t))
679
680 (defun bs--restore-window-config ()
681   "Restore window configuration on the current frame."
682   (when bs--window-config-coming-from
683     (let ((frame (selected-frame)))
684       (unwind-protect
685            (set-window-configuration bs--window-config-coming-from)
686         (select-frame frame)))
687     (setq bs--window-config-coming-from nil)))
688
689 (defun bs-kill ()
690   "Let buffer disappear and reset window configuration."
691   (interactive)
692   (bury-buffer (current-buffer))
693   (bs--restore-window-config))
694
695 (defun bs-abort ()
696   "Ding and leave Buffer Selection Menu without a selection."
697   (interactive)
698   (ding)
699   (bs-kill))
700
701 (defun bs-set-configuration-and-refresh ()
702   "Ask user for a configuration and apply selected configuration.
703 Refresh whole Buffer Selection Menu."
704   (interactive)
705   (call-interactively 'bs-set-configuration)
706   (bs--redisplay t))
707
708 (defun bs-refresh ()
709   "Refresh whole Buffer Selection Menu."
710   (interactive)
711   (bs--redisplay t))
712
713 (defun bs--set-window-height ()
714   "Change the height of the selected window to suit the current buffer list."
715   (unless (one-window-p t)
716     (fit-window-to-buffer (selected-window) bs-max-window-height)))
717
718 (defun bs--current-buffer ()
719   "Return buffer on current line.
720 Raise an error if not on a buffer line."
721   (beginning-of-line)
722   (let ((line (+ (- bs-header-lines-length)
723                  (count-lines 1 (point)))))
724     (if (< line 0)
725         (error "You are on a header row"))
726     (nth line bs-current-list)))
727
728 (defun bs--update-current-line ()
729   "Update the entry on current line for Buffer Selection Menu."
730   (let ((buffer (bs--current-buffer))
731         (inhibit-read-only t))
732     (beginning-of-line)
733     (delete-region (point) (line-end-position))
734     (bs--insert-one-entry buffer)
735     (beginning-of-line)))
736
737 (defun bs-view ()
738   "View current line's buffer in View mode.
739 Leave Buffer Selection Menu."
740   (interactive)
741   (view-buffer (bs--current-buffer)))
742
743 (defun bs-select ()
744   "Select current line's buffer and other marked buffers.
745 If there are no marked buffers the window configuration before starting
746 Buffer Selection Menu will be restored.
747 If there are marked buffers each marked buffer and the current line's buffer
748 will be selected in a window.
749 Leave Buffer Selection Menu."
750   (interactive)
751   (let ((buffer (bs--current-buffer)))
752     (bury-buffer (current-buffer))
753     (bs--restore-window-config)
754     (switch-to-buffer buffer)
755     (if bs--marked-buffers
756         ;; Some marked buffers for selection
757         (let* ((all (delq buffer bs--marked-buffers))
758                (height (/ (1- (frame-height)) (1+ (length all)))))
759           (delete-other-windows)
760           (switch-to-buffer buffer)
761           (while all
762             (split-window nil height)
763             (other-window 1)
764             (switch-to-buffer (car all))
765             (setq all (cdr all)))
766           ;; goto window we have started bs.
767           (other-window 1)))))
768
769 (defun bs-select-other-window ()
770   "Select current line's buffer by `switch-to-buffer-other-window'.
771 The window configuration before starting Buffer Selection Menu will be restored
772 unless there is no other window.  In this case a new window will be created.
773 Leave Buffer Selection Menu."
774   (interactive)
775   (let ((buffer (bs--current-buffer)))
776     (bury-buffer (current-buffer))
777     (bs--restore-window-config)
778     (switch-to-buffer-other-window buffer)))
779
780 (defun bs-tmp-select-other-window ()
781   "Make the other window select this line's buffer.
782 The current window remains selected."
783   (interactive)
784   (let ((buffer (bs--current-buffer)))
785     (display-buffer buffer t)))
786
787 (defun bs-select-other-frame ()
788   "Select current line's buffer in new created frame.
789 Leave Buffer Selection Menu."
790   (interactive)
791   (let ((buffer (bs--current-buffer)))
792     (bury-buffer (current-buffer))
793     (bs--restore-window-config)
794     (switch-to-buffer-other-frame buffer)))
795
796 (defun bs-mouse-select-other-frame (event)
797   "Select selected line's buffer in new created frame.
798 Leave Buffer Selection Menu.
799 EVENT: a mouse click event."
800   (interactive "e")
801   (mouse-set-point event)
802   (bs-select-other-frame))
803
804 (defun bs-mouse-select (event)
805   "Select buffer on mouse click EVENT.
806 Select buffer by `bs-select'."
807   (interactive "e")
808   (mouse-set-point event)
809   (bs-select))
810
811 (defun bs-select-in-one-window ()
812   "Select current line's buffer in one window and delete other windows.
813 Leave Buffer Selection Menu."
814   (interactive)
815   (bs-select)
816   (delete-other-windows))
817
818 (defun bs-bury-buffer ()
819   "Bury buffer on current line."
820   (interactive)
821   (bury-buffer (bs--current-buffer))
822   (bs--redisplay t))
823
824 (defun bs-save ()
825   "Save buffer on current line."
826   (interactive)
827   (let ((buffer (bs--current-buffer)))
828     (save-excursion
829       (set-buffer buffer)
830