Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / bs.el
CommitLineData
6448a6b3
GM
1;;; bs.el --- menu for selecting and displaying buffers
2
e6ce8c42 3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
49f70d46 4;; 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
6448a6b3
GM
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
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
6448a6b3 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
6448a6b3
GM
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6448a6b3
GM
23
24;;; Commentary:
25
26;; Version: 1.17
91b69101 27;; X-URL: http://www.geekware.de/software/emacs
6448a6b3
GM
28;;
29;; The bs-package contains a main function bs-show for poping up a
30;; buffer in a way similar to `list-buffers' and `electric-buffer-list':
31;; The new buffer offers a Buffer Selection Menu for manipulating
32;; the buffer list and buffers.
33;;
34;; -----------------------------------------------------------------------
35;; | MR Buffer Size Mode File |
36;; | -- ------ ---- ---- ---- |
37;; |. bs.el 14690 Emacs-Lisp /home/sun/sylvester/el/bs.e$|
38;; | % executable.el 9429 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
39;; | % vc.el 104893 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
40;; | % test_vc.el 486 Emacs-Lisp /home/sun/sylvester/el/test$|
41;; | % vc-hooks.el 43605 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
42;; -----------------------------------------------------------------------
43
44;;; Quick Installation und Customization:
45
46;; Use
47;; M-x bs-show
48;; for buffer selection or optional bind a key to main function `bs-show'
49;; (global-set-key "\C-x\C-b" 'bs-show) ;; or another key
50;;
51;; For customization use
52;; M-x bs-customize
53
54
55;;; More Commentary:
56
57;; bs-show will generate a new buffer named *buffer-selection*, which shows
58;; all buffers or a subset of them, and has possibilities for deleting,
59;; saving and selecting buffers. For more details see docstring of
60;; function `bs-mode'. A current configuration describes which buffers appear
61;; in *buffer-selection*. See docstring of variable `bs-configurations' for
62;; more details.
63;;
64;; The package bs combines the advantages of the Emacs functions
65;; `list-buffers' and `electric-buffer-list'.
66;;
880ed9f1 67;; Additional features for Buffer Selection Menu:
6448a6b3
GM
68;; - configurable list of buffers (show only files etc.).
69;; - comfortable way to change displayed subset of all buffers.
70;; - show sorted list of buffers.
71;; - cyclic navigation:
72;; - goes to top of buffer list if you are on last line and press down.
73;; - goes to end of buffer list if you are on first line and press up.
74;; - Offer an alternative buffer list by prefix key C-u.
75
76;;; Cycling through buffers
77
78;; This package offers two functions for buffer cycling. If you want to cycle
79;; through buffer list you can use `bs-cycle-next' or `bs-cycle-previous'.
80;; Bind these function to a key like
81;; (global-set-key [(f9)] 'bs-cycle-previous)
82;; (global-set-key [(f10)] 'bs-cycle-next)
83;;
84;; Both functions use a special subset of all buffers for cycling to avoid
85;; to go through internal buffers like *Messages*.
86;;
87;; Cycling through buffers ignores sorting because sorting destroys
88;; the logical buffer list. If buffer list is sorted by size you
89;; won't be able to cycle to the smallest buffer.
90
91;;; Customization:
92
93;; There is a customization group called `bs' in group `convenience'.
94;; Start customization by M-x bs-customize
95;;
96;; Buffer list
97;; -----------
98;; You can define your own configurations by extending variable
99;; `bs-configurations' (see docstring for details).
100;;
101;; `bs-default-configuration' contains the name of default configuration.
102;; The default value is "files" which means to show only files.
103;;
104;; If you always want to see all buffers, customize variable
105;; `bs-default-configuration' in customization group `bs'.
106;;
107;; Configure sorting
108;; -----------------
109;; You can define functions for sorting the buffer list.
110;; When selecting buffers, you can step through available sorting
111;; methods with key 'S'.
112;; To define a new way of sorting, customize variable `bs-sort-functions'.
113;;
114;; There are four basic functions for sorting:
115;; by buffer name, by mode, by size, or by filename
116;;
117;; Configure buffer cycling
118;; ------------------------
119;; When cycling through buffer list the functions for cycling will use
120;; the current configuration of bs to calculate the buffer list.
121;; If you want to use a different configuration for cycling you have to set
122;; the variable `bs-cycle-configuration-name'. You can customize this variable.
123;;
124;; For example: If you use the configuration called "files-and-scratch" you
125;; can cycle through all file buffers and *scratch* although your current
126;; configuration perhaps is "files" which ignores buffer *scratch*.
127
128;;; History:
129
130;;; Code:
131
70ef8857
SM
132(eval-when-compile (require 'cl))
133
6448a6b3
GM
134;; ----------------------------------------------------------------------
135;; Globals for customization
136;; ----------------------------------------------------------------------
137
138(defgroup bs nil
139 "Buffer Selection: Maintaining buffers by buffer menu."
00a9f6a5 140 :version "21.1"
dc7904f5 141 :link '(emacs-commentary-link "bs")
91b69101 142 :link '(url-link "http://www.geekware.de/software/emacs")
6448a6b3
GM
143 :group 'convenience)
144
880ed9f1
GM
145(defgroup bs-appearance nil
146 "Buffer Selection appearance: Appearance of bs buffer menu."
6448a6b3
GM
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 " "))
e749f576 161 "List specifying the layout of a Buffer Selection Menu buffer.
6448a6b3 162Each entry specifies a column and is a list of the form of:
c0cb1027 163\(HEADER MINIMUM-LENGTH MAXIMUM-LENGTH ALIGNMENT FUN-OR-STRING)
91b69101
JB
164
165HEADER : String for header for first line or a function
166 which calculates column title.
167MINIMUM-LENGTH : Minimum width of column (number or name of function).
168 The function must return a positive integer.
169MAXIMUM-LENGTH : Maximum width of column (number or name of function)
170 (currently ignored).
171ALIGNMENT : Alignment of column (`left', `right', `middle').
172FUN-OR-STRING : Name of a function for calculating the value or a
173 string for a constant value.
174
880ed9f1 175The function gets as parameter the buffer where we have started
6448a6b3 176buffer selection and the list of all buffers to show. The function must
880ed9f1
GM
177return a string representing the column's value."
178 :group 'bs-appearance
6448a6b3
GM
179 :type '(repeat sexp))
180
6448a6b3
GM
181(defun bs--make-header-match-string ()
182 "Return a regexp matching the first line of a Buffer Selection Menu buffer."
e749f576 183 (concat "^\\(" (mapconcat #'car bs-attributes-list " *") " *$\\)"))
6448a6b3 184
d8754ce5 185;; Font-Lock-Settings
6448a6b3 186(defvar bs-mode-font-lock-keywords
880ed9f1 187 (list ;; header in font-lock-type-face
dc7904f5
DL
188 (list (bs--make-header-match-string)
189 '(1 font-lock-type-face append) '(1 'bold append))
190 ;; Buffername embedded by *
191 (list "^\\(.*\\*.*\\*.*\\)$"
a5b09d5a
DL
192 1
193 ;; problem in XEmacs with font-lock-constant-face
194 (if (facep 'font-lock-constant-face)
195 'font-lock-constant-face
196 'font-lock-comment-face))
dc7904f5 197 ;; Dired-Buffers
29115ca9 198 '("^..\\(.*Dired .*\\)$" 1 font-lock-function-name-face)
dc7904f5
DL
199 ;; the star for modified buffers
200 '("^.\\(\\*\\) +[^\\*]" 1 font-lock-comment-face))
6448a6b3
GM
201 "Default font lock expressions for Buffer Selection Menu.")
202
203(defcustom bs-max-window-height 20
e749f576 204 "Maximal window height of Buffer Selection Menu."
880ed9f1 205 :group 'bs-appearance
6448a6b3
GM
206 :type 'integer)
207
208(defvar bs-dont-show-regexp nil
209 "Regular expression specifying which buffers not to show.
210A buffer whose name matches this regular expression will not be
211included in the buffer list.")
212
213(defvar bs-must-show-regexp nil
214 "Regular expression for specifying buffers which must be shown.
215A buffer whose name matches this regular expression will be
216included in the buffer list.
217Note that this variable is temporary: if the configuration is changed
218it is reset to nil. Use `bs-must-always-show-regexp' to specify buffers
219that must always be shown regardless of the configuration.")
220
221(defcustom bs-must-always-show-regexp nil
e749f576 222 "Regular expression for specifying buffers to show always.
6448a6b3
GM
223A buffer whose name matches this regular expression will
224be shown regardless of current configuration of Buffer Selection Menu."
225 :group 'bs
226 :type '(choice (const :tag "Nothing at all" nil) regexp))
227
228(defvar bs-dont-show-function nil
229 "Function for specifying buffers not to show.
230The function gets one argument - the buffer to test. The function must
231return a value different from nil to ignore the buffer in
232Buffer Selection Menu.")
233
234(defvar bs-must-show-function nil
235 "Function for specifying buffers which must be shown.
236The function gets one argument - the buffer to test.")
237
238(defvar bs-buffer-sort-function nil
239 "Sort function to sort the buffers that appear in Buffer Selection Menu.
b75ccc22
JB
240The function gets two arguments - the buffers to compare.
241It must return non-nil if the first buffer should sort before the second.")
6448a6b3
GM
242
243(defcustom bs-maximal-buffer-name-column 45
e749f576 244 "Maximum column width for buffer names.
6448a6b3
GM
245The column for buffer names has dynamic width. The width depends on
246maximal and minimal length of names of buffers to show. The maximal
247width is bounded by `bs-maximal-buffer-name-column'.
248See also `bs-minimal-buffer-name-column'."
880ed9f1 249 :group 'bs-appearance
6448a6b3
GM
250 :type 'integer)
251
252(defcustom bs-minimal-buffer-name-column 15
e749f576 253 "Minimum column width for buffer names.
6448a6b3
GM
254The column for buffer names has dynamic width. The width depends on
255maximal and minimal length of names of buffers to show. The minimal
256width is bounded by `bs-minimal-buffer-name-column'.
257See also `bs-maximal-buffer-name-column'."
880ed9f1 258 :group 'bs-appearance
6448a6b3
GM
259 :type 'integer)
260
261(defconst bs-header-lines-length 2
262 "Number of lines for headers in Buffer Selection Menu.")
263
264(defcustom bs-configurations
265 '(("all" nil nil nil nil nil)
266 ("files" nil nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)
880ed9f1 267 ("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file
6448a6b3
GM
268 bs-sort-buffer-interns-are-last)
269 ("all-intern-last" nil nil nil nil bs-sort-buffer-interns-are-last))
e749f576 270 "List of all configurations you can use in the Buffer Selection Menu.
6448a6b3 271A configuration describes which buffers appear in Buffer Selection Menu
880ed9f1 272and also the order of buffers. A configuration is a list with
6448a6b3
GM
273six elements. The first element is a string and describes the configuration.
274The following five elements represent the values for Buffer Selection Menu
880ed9f1
GM
275configuration variables `bs-must-show-regexp', `bs-must-show-function',
276`bs-dont-show-regexp', `bs-dont-show-function' and `bs-buffer-sort-function'.
6448a6b3 277By setting these variables you define a configuration."
880ed9f1 278 :group 'bs-appearance
6448a6b3
GM
279 :type '(repeat sexp))
280
281(defcustom bs-default-configuration "files"
e749f576 282 "Name of default configuration used by the Buffer Selection Menu.
6448a6b3
GM
283\\<bs-mode-map>
284Will be changed using key \\[bs-select-next-configuration].
285Must be a string used in `bs-configurations' for naming a configuration."
286 :group 'bs
287 :type 'string)
288
289(defcustom bs-alternative-configuration "all"
e749f576 290 "Name of configuration used when calling `bs-show' with \
6448a6b3
GM
291\\[universal-argument] as prefix key.
292Must be a string used in `bs-configurations' for naming a configuration."
293 :group 'bs
294 :type 'string)
295
296(defvar bs-current-configuration bs-default-configuration
297 "Name of current configuration.
880ed9f1 298Must be a string used in `bs-configurations' for naming a configuration.")
6448a6b3
GM
299
300(defcustom bs-cycle-configuration-name nil
e749f576 301 "Name of configuration used when cycling through the buffer list.
6448a6b3
GM
302A value of nil means to use current configuration `bs-default-configuration'.
303Must be a string used in `bs-configurations' for naming a configuration."
304 :group 'bs
305 :type '(choice (const :tag "like current configuration" nil)
306 string))
307
308(defcustom bs-string-show-always "+"
e749f576 309 "String added in column 1 indicating a buffer will always be shown."
880ed9f1 310 :group 'bs-appearance
6448a6b3
GM
311 :type 'string)
312
313(defcustom bs-string-show-never "-"
e749f576 314 "String added in column 1 indicating a buffer will never be shown."
880ed9f1 315 :group 'bs-appearance
6448a6b3
GM
316 :type 'string)
317
318(defcustom bs-string-current "."
e749f576 319 "String added in column 1 indicating the current buffer."
880ed9f1 320 :group 'bs-appearance
6448a6b3
GM
321 :type 'string)
322
323(defcustom bs-string-current-marked "#"
e749f576 324 "String added in column 1 indicating the current buffer when it is marked."
880ed9f1 325 :group 'bs-appearance
6448a6b3
GM
326 :type 'string)
327
328(defcustom bs-string-marked ">"
e749f576 329 "String added in column 1 indicating a marked buffer."
880ed9f1 330 :group 'bs-appearance
6448a6b3
GM
331 :type 'string)
332
333(defcustom bs-string-show-normally " "
e749f576 334 "String added in column 1 indicating an unmarked buffer."
880ed9f1 335 :group 'bs-appearance
6448a6b3
GM
336 :type 'string)
337
338(defvar bs--name-entry-length 20
339 "Maximum length of all displayed buffer names.
340Used internally, only.")
341
342;; ----------------------------------------------------------------------
880ed9f1 343;; Internal globals
6448a6b3
GM
344;; ----------------------------------------------------------------------
345
346(defvar bs-buffer-show-mark nil
347 "Flag for the current mode for showing this buffer.
880ed9f1
GM
348A value of nil means buffer will be shown depending on the current
349configuration.
6448a6b3
GM
350A value of `never' means to never show the buffer.
351A value of `always' means to show buffer regardless of the configuration.")
352
353(make-variable-buffer-local 'bs-buffer-show-mark)
354
355;; Make face named region (for XEmacs)
356(unless (facep 'region)
357 (make-face 'region)
358 (set-face-background 'region "gray75"))
359
6448a6b3
GM
360(defun bs--sort-by-name (b1 b2)
361 "Compare buffers B1 and B2 by buffer name."
362 (string< (buffer-name b1)
dc7904f5 363 (buffer-name b2)))
6448a6b3
GM
364
365(defun bs--sort-by-filename (b1 b2)
366 "Compare buffers B1 and B2 by file name."
367 (string< (or (buffer-file-name b1) "")
dc7904f5 368 (or (buffer-file-name b2) "")))
6448a6b3
GM
369
370(defun bs--sort-by-mode (b1 b2)
371 "Compare buffers B1 and B2 by mode name."
48d33090
SM
372 (save-current-buffer
373 (string< (progn (set-buffer b1) (format-mode-line mode-name nil nil b1))
374 (progn (set-buffer b2) (format-mode-line mode-name nil nil b2)))))
6448a6b3
GM
375
376(defun bs--sort-by-size (b1 b2)
377 "Compare buffers B1 and B2 by buffer size."
70ef8857 378 (< (buffer-size b1) (buffer-size b2)))
6448a6b3
GM
379
380(defcustom bs-sort-functions
381 '(("by name" bs--sort-by-name "Buffer" region)
382 ("by size" bs--sort-by-size "Size" region)
383 ("by mode" bs--sort-by-mode "Mode" region)
384 ("by filename" bs--sort-by-filename "File" region)
385 ("by nothing" nil nil nil))
e749f576 386 "List of all possible sorting aspects for Buffer Selection Menu.
6448a6b3 387You can add a new entry with a call to `bs-define-sort-function'.
b75ccc22 388Each element is a list of four elements (NAME FUNCTION REGEXP-FOR-SORTING FACE).
6448a6b3 389NAME specifies the sort order defined by function FUNCTION.
b75ccc22 390FUNCTION nil means don't sort the buffer list. Otherwise the function
6448a6b3
GM
391must have two parameters - the buffers to compare.
392REGEXP-FOR-SORTING is a regular expression which describes the
393column title to highlight.
394FACE is a face used to fontify the sorted column title. A value of nil means
395don't highlight."
396 :group 'bs
397 :type '(repeat sexp))
398
399(defun bs-define-sort-function (name fun &optional regexp-for-sorting face)
400 "Define a new function for buffer sorting in Buffer Selection Menu.
401NAME specifies the sort order defined by function FUN.
402A value of nil for FUN means don't sort the buffer list. Otherwise the
403functions must have two parameters - the buffers to compare.
404REGEXP-FOR-SORTING is a regular expression which describes the
405column title to highlight.
406FACE is a face used to fontify the sorted column title. A value of nil means
407don't highlight.
408The new sort aspect will be inserted into list `bs-sort-functions'."
409 (let ((tupel (assoc name bs-sort-functions)))
410 (if tupel
dc7904f5 411 (setcdr tupel (list fun regexp-for-sorting face))
6448a6b3 412 (setq bs-sort-functions
dc7904f5
DL
413 (cons (list name fun regexp-for-sorting face)
414 bs-sort-functions)))))
6448a6b3
GM
415
416(defvar bs--current-sort-function nil
417 "Description of the current function for sorting the buffer list.
418This is an element of `bs-sort-functions'.")
419
420(defcustom bs-default-sort-name "by nothing"
e749f576 421 "Name of default sort behavior.
6448a6b3
GM
422Must be \"by nothing\" or a string used in `bs-sort-functions' for
423naming a sort behavior. Default is \"by nothing\" which means no sorting."
424 :group 'bs
425 :type 'string
426 :set (lambda (var-name value)
dc7904f5
DL
427 (set var-name value)
428 (setq bs--current-sort-function
429 (assoc value bs-sort-functions))))
6448a6b3
GM
430
431(defvar bs--buffer-coming-from nil
432 "The buffer in which the user started the current Buffer Selection Menu.")
433
434(defvar bs--show-all nil
435 "Flag whether showing all buffers regardless of current configuration.
aaf08d6b 436Non-nil means to show all buffers. Otherwise show buffers
6448a6b3
GM
437defined by current configuration `bs-current-configuration'.")
438
439(defvar bs--window-config-coming-from nil
440 "Window configuration before starting Buffer Selection Menu.")
441
442(defvar bs--intern-show-never "^ \\|\\*buffer-selection\\*"
443 "Regular expression specifying which buffers never to show.
444A buffer whose name matches this regular expression will never be
445included in the buffer list.")
446
447(defvar bs-current-list nil
448 "List of buffers shown in Buffer Selection Menu.
449Used internally, only.")
450
451(defvar bs--marked-buffers nil
452 "Currently marked buffers in Buffer Selection Menu.")
453
e6e76838
AS
454(defvar bs-mode-map
455 (let ((map (make-sparse-keymap)))
456 (define-key map " " 'bs-select)
457 (define-key map "f" 'bs-select)
458 (define-key map "v" 'bs-view)
459 (define-key map "!" 'bs-select-in-one-window)
460 (define-key map [mouse-2] 'bs-mouse-select) ;; for GNU EMACS
461 (define-key map [button2] 'bs-mouse-select) ;; for XEmacs
462 (define-key map "F" 'bs-select-other-frame)
463 (let ((key ?1))
464 (while (<= key ?9)
465 (define-key map (char-to-string key) 'digit-argument)
466 (setq key (1+ key))))
467 (define-key map "-" 'negative-argument)
468 (define-key map "\e-" 'negative-argument)
469 (define-key map "o" 'bs-select-other-window)
470 (define-key map "\C-o" 'bs-tmp-select-other-window)
471 ;; for GNU EMACS
472 (define-key map [mouse-3] 'bs-mouse-select-other-frame)
473 ;; for XEmacs
474 (define-key map [button3] 'bs-mouse-select-other-frame)
475 (define-key map [up] 'bs-up)
476 (define-key map "n" 'bs-down)
477 (define-key map "p" 'bs-up)
478 (define-key map [down] 'bs-down)
479 (define-key map "\C-m" 'bs-select)
480 (define-key map "b" 'bs-bury-buffer)
481 (define-key map "s" 'bs-save)
482 (define-key map "S" 'bs-show-sorted)
483 (define-key map "a" 'bs-toggle-show-all)
484 (define-key map "d" 'bs-delete)
485 (define-key map "\C-d" 'bs-delete-backward)
486 (define-key map "k" 'bs-delete)
487 (define-key map "g" 'bs-refresh)
488 (define-key map "C" 'bs-set-configuration-and-refresh)
489 (define-key map "c" 'bs-select-next-configuration)
490 (define-key map "q" 'bs-kill)
491 ;; (define-key map "z" 'bs-kill)
492 (define-key map "\C-c\C-c" 'bs-kill)
493 (define-key map "\C-g" 'bs-abort)
494 (define-key map "\C-]" 'bs-abort)
495 (define-key map "%" 'bs-toggle-readonly)
496 (define-key map "~" 'bs-clear-modified)
497 (define-key map "M" 'bs-toggle-current-to-show)
498 (define-key map "+" 'bs-set-current-buffer-to-show-always)
499 ;;(define-key map "-" 'bs-set-current-buffer-to-show-never)
500 (define-key map "t" 'bs-visit-tags-table)
501 (define-key map "m" 'bs-mark-current)
502 (define-key map "u" 'bs-unmark-current)
503 (define-key map ">" 'scroll-right)
504 (define-key map "<" 'scroll-left)
505 (define-key map "?" 'bs-help)
506 map)
6448a6b3
GM
507 "Keymap of `bs-mode'.")
508
6448a6b3
GM
509;; ----------------------------------------------------------------------
510;; Functions
511;; ----------------------------------------------------------------------
512
513(defun bs-buffer-list (&optional list sort-description)
514 "Return a list of buffers to be shown.
880ed9f1 515LIST is a list of buffers to test for appearance in Buffer Selection Menu.
6448a6b3
GM
516The result list depends on the global variables `bs-dont-show-regexp',
517`bs-must-show-regexp', `bs-dont-show-function', `bs-must-show-function'
518and `bs-buffer-sort-function'.
519If SORT-DESCRIPTION isn't nil the list will be sorted by
520a special function. SORT-DESCRIPTION is an element of `bs-sort-functions'."
521 (setq sort-description (or sort-description bs--current-sort-function)
dc7904f5 522 list (or list (buffer-list)))
6448a6b3 523 (let ((result nil))
e749f576
JB
524 (dolist (buf list)
525 (let* ((buffername (buffer-name buf))
526 (int-show-never (string-match-p bs--intern-show-never buffername))
dc7904f5 527 (ext-show-never (and bs-dont-show-regexp
e749f576
JB
528 (string-match-p bs-dont-show-regexp
529 buffername)))
dc7904f5 530 (extern-must-show (or (and bs-must-always-show-regexp
e749f576 531 (string-match-p
dc7904f5
DL
532 bs-must-always-show-regexp
533 buffername))
534 (and bs-must-show-regexp
e749f576
JB
535 (string-match-p bs-must-show-regexp
536 buffername))))
dc7904f5
DL
537 (extern-show-never-from-fun (and bs-dont-show-function
538 (funcall bs-dont-show-function
e749f576 539 buf)))
dc7904f5
DL
540 (extern-must-show-from-fun (and bs-must-show-function
541 (funcall bs-must-show-function
e749f576
JB
542 buf)))
543 (show-flag (buffer-local-value 'bs-buffer-show-mark buf)))
544 (when (or (eq show-flag 'always)
545 (and (or bs--show-all (not (eq show-flag 'never)))
546 (not int-show-never)
547 (or bs--show-all
548 extern-must-show
549 extern-must-show-from-fun
550 (and (not ext-show-never)
551 (not extern-show-never-from-fun)))))
552 (setq result (cons buf result)))))
6448a6b3
GM
553 (setq result (reverse result))
554 ;; The current buffer which was the start point of bs should be an element
555 ;; of result list, so that we can leave with space and be back in the
556 ;; buffer we started bs-show.
e749f576
JB
557 (when (and bs--buffer-coming-from
558 (buffer-live-p bs--buffer-coming-from)
559 (not (memq bs--buffer-coming-from result)))
560 (setq result (cons bs--buffer-coming-from result)))
6448a6b3
GM
561 ;; sorting
562 (if (and sort-description
dc7904f5
DL
563 (nth 1 sort-description))
564 (setq result (sort result (nth 1 sort-description)))
6448a6b3
GM
565 ;; else standard sorting
566 (bs-buffer-sort result))))
567
568(defun bs-buffer-sort (buffer-list)
569 "Sort buffers in BUFFER-LIST according to `bs-buffer-sort-function'."
570 (if bs-buffer-sort-function
571 (sort buffer-list bs-buffer-sort-function)
572 buffer-list))
573
574(defun bs--redisplay (&optional keep-line-p sort-description)
575 "Redisplay whole Buffer Selection Menu.
aaf08d6b 576If KEEP-LINE-P is non-nil the point will stay on current line.
ce3ba12c 577SORT-DESCRIPTION is an element of `bs-sort-functions'."
e6ce8c42 578 (let ((line (count-lines 1 (point))))
6448a6b3 579 (bs-show-in-buffer (bs-buffer-list nil sort-description))
e749f576 580 (when keep-line-p
e6ce8c42
GM
581 (goto-char (point-min))
582 (forward-line line))
6448a6b3
GM
583 (beginning-of-line)))
584
585(defun bs--goto-current-buffer ()
586 "Goto line which represents the current buffer;
587actually the line which begins with character in `bs-string-current' or
588`bs-string-current-marked'."
dc7904f5
DL
589 (let ((regexp (concat "^"
590 (regexp-quote bs-string-current)
591 "\\|^"
592 (regexp-quote bs-string-current-marked)))
593 point)
6448a6b3
GM
594 (save-excursion
595 (goto-char (point-min))
e749f576
JB
596 (when (search-forward-regexp regexp nil t)
597 (setq point (1- (point)))))
598 (when point
599 (goto-char point))))
6448a6b3
GM
600
601(defun bs--current-config-message ()
602 "Return a string describing the current `bs-mode' configuration."
603 (if bs--show-all
604 "Show all buffers."
605 (format "Show buffer by configuration %S"
dc7904f5 606 bs-current-configuration)))
6448a6b3 607
e749f576
JB
608(defun bs--track-window-changes (frame)
609 "Track window changes to refresh the buffer list.
610Used from `window-size-change-functions'."
611 (let ((win (get-buffer-window "*buffer-selection*" frame)))
612 (when win
613 (with-selected-window win
e749f576
JB
614 (bs--set-window-height)))))
615
616(defun bs--remove-hooks ()
617 "Remove `bs--track-window-changes' and auxiliary hooks."
618 (remove-hook 'window-size-change-functions 'bs--track-window-changes)
619 ;; Remove itself
620 (remove-hook 'kill-buffer-hook 'bs--remove-hooks t)
621 (remove-hook 'change-major-mode-hook 'bs--remove-hooks t))
622
5c742460
JB
623(put 'bs-mode 'mode-class 'special)
624
e749f576 625(define-derived-mode bs-mode nil "Buffer-Selection-Menu"
6448a6b3
GM
626 "Major mode for editing a subset of Emacs' buffers.
627\\<bs-mode-map>
628Aside from two header lines each line describes one buffer.
629Move to a line representing the buffer you want to edit and select
462db93c 630buffer by \\[bs-select] or SPC. Abort buffer list with \\[bs-kill].
6448a6b3
GM
631There are many key commands similar to `Buffer-menu-mode' for
632manipulating the buffer list and buffers.
633For faster navigation each digit key is a digit argument.
634
635\\[bs-select] or SPACE -- select current line's buffer and other marked buffers.
636\\[bs-toggle-show-all] -- toggle between all buffers and a special subset.
637\\[bs-select-other-window] -- select current line's buffer in other window.
638\\[bs-tmp-select-other-window] -- make another window display that buffer and
639 remain in Buffer Selection Menu.
640\\[bs-mouse-select] -- select current line's buffer and other marked buffers.
e148f726
TTN
641\\[bs-save] -- save current line's buffer immediately.
642\\[bs-delete] -- kill current line's buffer immediately.
6448a6b3
GM
643\\[bs-toggle-readonly] -- toggle read-only status of current line's buffer.
644\\[bs-clear-modified] -- clear modified-flag on that buffer.
645\\[bs-mark-current] -- mark current line's buffer to be displayed.
646\\[bs-unmark-current] -- unmark current line's buffer to be displayed.
647\\[bs-show-sorted] -- display buffer list sorted by next sort aspect.
648\\[bs-set-configuration-and-refresh] -- ask user for a configuration and \
649apply selected configuration.
650\\[bs-select-next-configuration] -- select and apply next \
651available Buffer Selection Menu configuration.
652\\[bs-kill] -- leave Buffer Selection Menu without a selection.
880ed9f1 653\\[bs-toggle-current-to-show] -- toggle status of appearance.
6448a6b3
GM
654\\[bs-set-current-buffer-to-show-always] -- mark current line's buffer \
655to show always.
880ed9f1 656\\[bs-visit-tags-table] -- call `visit-tags-table' on current line's buffer.
6448a6b3 657\\[bs-help] -- display this help text."
6448a6b3
GM
658 (make-local-variable 'font-lock-defaults)
659 (make-local-variable 'font-lock-verbose)
5a25a895 660 (make-local-variable 'font-lock-global-modes)
310a26be 661 (buffer-disable-undo)
e749f576 662 (setq buffer-read-only t
dc7904f5 663 truncate-lines t
e0d0133a 664 show-trailing-whitespace nil
5a25a895 665 font-lock-global-modes '(not bs-mode)
dc7904f5 666 font-lock-defaults '(bs-mode-font-lock-keywords t)
1e7a80f2
JB
667 font-lock-verbose nil)
668 (set (make-local-variable 'revert-buffer-function) 'bs-refresh)
e749f576
JB
669 (add-hook 'window-size-change-functions 'bs--track-window-changes)
670 (add-hook 'kill-buffer-hook 'bs--remove-hooks nil t)
671 (add-hook 'change-major-mode-hook 'bs--remove-hooks nil t))
6448a6b3 672
ce3ba12c
JB
673(defun bs--restore-window-config ()
674 "Restore window configuration on the current frame."
675 (when bs--window-config-coming-from
e749f576
JB
676 (let ((frame (selected-frame)))
677 (unwind-protect
678 (set-window-configuration bs--window-config-coming-from)
679 (select-frame frame)))
ce3ba12c
JB
680 (setq bs--window-config-coming-from nil)))
681
6448a6b3 682(defun bs-kill ()
6af00b67 683 "Let buffer disappear and reset window configuration."
6448a6b3
GM
684 (interactive)
685 (bury-buffer (current-buffer))
ce3ba12c 686 (bs--restore-window-config))
6448a6b3
GM
687
688(defun bs-abort ()
689 "Ding and leave Buffer Selection Menu without a selection."
dc7904f5 690 (interactive)
6448a6b3
GM
691 (ding)
692 (bs-kill))
693
694(defun bs-set-configuration-and-refresh ()
695 "Ask user for a configuration and apply selected configuration.
696Refresh whole Buffer Selection Menu."
697 (interactive)
698 (call-interactively 'bs-set-configuration)
699 (bs--redisplay t))
700
fa72d075
JB
701(defun bs-refresh (&rest ignored)
702 "Refresh whole Buffer Selection Menu.
703Arguments are IGNORED (for `revert-buffer')."
6448a6b3
GM
704 (interactive)
705 (bs--redisplay t))
706
6448a6b3
GM
707(defun bs--set-window-height ()
708 "Change the height of the selected window to suit the current buffer list."
709 (unless (one-window-p t)
ca6b8248 710 (fit-window-to-buffer (selected-window) bs-max-window-height)))
6448a6b3
GM
711
712(defun bs--current-buffer ()
713 "Return buffer on current line.
880ed9f1 714Raise an error if not on a buffer line."
6448a6b3
GM
715 (beginning-of-line)
716 (let ((line (+ (- bs-header-lines-length)
dc7904f5 717 (count-lines 1 (point)))))
e749f576
JB
718 (when (< line 0)
719 (error "You are on a header row"))
6448a6b3
GM
720 (nth line bs-current-list)))
721
722(defun bs--update-current-line ()
723 "Update the entry on current line for Buffer Selection Menu."
724 (let ((buffer (bs--current-buffer))
dc7904f5 725 (inhibit-read-only t))
6448a6b3
GM
726 (beginning-of-line)
727 (delete-region (point) (line-end-position))
728 (bs--insert-one-entry buffer)
729 (beginning-of-line)))
730
731(defun bs-view ()
732 "View current line's buffer in View mode.
733Leave Buffer Selection Menu."
734 (interactive)
735 (view-buffer (bs--current-buffer)))
736
737(defun bs-select ()
738 "Select current line's buffer and other marked buffers.
739If there are no marked buffers the window configuration before starting
de302d45 740Buffer Selection Menu will be restored.
6448a6b3
GM
741If there are marked buffers each marked buffer and the current line's buffer
742will be selected in a window.
743Leave Buffer Selection Menu."
744 (interactive)
745 (let ((buffer (bs--current-buffer)))
746 (bury-buffer (current-buffer))
ce3ba12c 747 (bs--restore-window-config)
6448a6b3 748 (switch-to-buffer buffer)
e749f576
JB
749 (when bs--marked-buffers
750 ;; Some marked buffers for selection
751 (let* ((all (delq buffer bs--marked-buffers))
752 (height (/ (1- (frame-height)) (1+ (length all)))))
753 (delete-other-windows)
754 (switch-to-buffer buffer)
755 (dolist (buf all)
756 (split-window nil height)
757 (other-window 1)
758 (switch-to-buffer buf))
759 ;; goto window we have started bs.
760 (other-window 1)))))
6448a6b3
GM
761
762(defun bs-select-other-window ()
763 "Select current line's buffer by `switch-to-buffer-other-window'.
de302d45 764The window configuration before starting Buffer Selection Menu will be restored
6448a6b3
GM
765unless there is no other window. In this case a new window will be created.
766Leave Buffer Selection Menu."
767 (interactive)
768 (let ((buffer (bs--current-buffer)))
769 (bury-buffer (current-buffer))
ce3ba12c 770 (bs--restore-window-config)
6448a6b3
GM
771 (switch-to-buffer-other-window buffer)))
772
773(defun bs-tmp-select-other-window ()
774 "Make the other window select this line's buffer.
775The current window remains selected."
776 (interactive)
777 (let ((buffer (bs--current-buffer)))
778 (display-buffer buffer t)))
779
780(defun bs-select-other-frame ()
781 "Select current line's buffer in new created frame.
782Leave Buffer Selection Menu."
783 (interactive)
784 (let ((buffer (bs--current-buffer)))
785 (bury-buffer (current-buffer))
ce3ba12c 786 (bs--restore-window-config)
6448a6b3
GM
787 (switch-to-buffer-other-frame buffer)))
788
789(defun bs-mouse-select-other-frame (event)
790 "Select selected line's buffer in new created frame.
791Leave Buffer Selection Menu.
b75ccc22 792EVENT: a mouse click event."
6448a6b3
GM
793 (interactive "e")
794 (mouse-set-point event)
795 (bs-select-other-frame))
796
797(defun bs-mouse-select (event)
798 "Select buffer on mouse click EVENT.
799Select buffer by `bs-select'."
800 (interactive "e")
801 (mouse-set-point event)
802 (bs-select))
803
804(defun bs-select-in-one-window ()
805 "Select current line's buffer in one window and delete other windows.
806Leave Buffer Selection Menu."
807 (interactive)
808 (bs-select)
809 (delete-other-windows))
810
811(defun bs-bury-buffer ()
812 "Bury buffer on current line."
813 (interactive)
814 (bury-buffer (bs--current-buffer))
815 (bs--redisplay t))
816
817(defun bs-save ()
818 "Save buffer on current line."
819 (interactive)
70ef8857 820 (with-current-buffer (bs--current-buffer)
3af36712
SM
821 (save-buffer))
822 (bs--update-current-line))
6448a6b3
GM
823
824(defun bs-visit-tags-table ()
825 "Visit the tags table in the buffer on this line.
826See `visit-tags-table'."
827 (interactive)
828 (let ((file (buffer-file-name (bs--current-buffer))))
829 (if file
dc7904f5 830 (visit-tags-table file)
6448a6b3
GM
831 (error "Specified buffer has no file"))))
832
833(defun bs-toggle-current-to-show ()
834 "Toggle status of showing flag for buffer in current line."
835 (interactive)
70ef8857
SM
836 (let ((res
837 (with-current-buffer (bs--current-buffer)
838 (setq bs-buffer-show-mark (case bs-buffer-show-mark
839 ((nil) 'never)
840 ((never) 'always)
841 (t nil))))))
6448a6b3
GM
842 (bs--update-current-line)
843 (bs--set-window-height)
844 (bs--show-config-message res)))
845
846(defun bs-set-current-buffer-to-show-always (&optional not-to-show-p)
847 "Toggle status of buffer on line to `always shown'.
848NOT-TO-SHOW-P: prefix argument.
849With no prefix argument the buffer on current line is marked to show
850always. Otherwise it is marked to show never."
851 (interactive "P")
852 (if not-to-show-p
853 (bs-set-current-buffer-to-show-never)
854 (bs--set-toggle-to-show (bs--current-buffer) 'always)))
855
856(defun bs-set-current-buffer-to-show-never ()
857 "Toggle status of buffer on line to `never shown'."
858 (interactive)
859 (bs--set-toggle-to-show (bs--current-buffer) 'never))
860
861(defun bs--set-toggle-to-show (buffer what)
862 "Set value `bs-buffer-show-mark' of buffer BUFFER to WHAT.
863Redisplay current line and display a message describing
864the status of buffer on current line."
e64dbd8b 865 (with-current-buffer buffer (setq bs-buffer-show-mark what))
6448a6b3
GM
866 (bs--update-current-line)
867 (bs--set-window-height)
868 (bs--show-config-message what))
869
3d5aef76
JB
870(defun bs--mark-unmark (count fun)
871 "Call FUN on COUNT consecutive buffers of *buffer-selection*."
872 (let ((dir (if (> count 0) 1 -1)))
873 (dotimes (i (abs count))
874 (let ((buffer (bs--current-buffer)))
875 (when buffer (funcall fun buffer))
876 (bs--update-current-line)
877 (bs-down dir)))))
878
6448a6b3
GM
879(defun bs-mark-current (count)
880 "Mark buffers.
881COUNT is the number of buffers to mark.
882Move cursor vertically down COUNT lines."
883 (interactive "p")
3d5aef76
JB
884 (bs--mark-unmark count
885 (lambda (buf)
886 (add-to-list 'bs--marked-buffers buf))))
6448a6b3
GM
887
888(defun bs-unmark-current (count)
889 "Unmark buffers.
890COUNT is the number of buffers to unmark.
891Move cursor vertically down COUNT lines."
892 (interactive "p")
3d5aef76
JB
893 (bs--mark-unmark count
894 (lambda (buf)
895 (setq bs--marked-buffers (delq buf bs--marked-buffers)))))
6448a6b3
GM
896
897(defun bs--show-config-message (what)
898 "Show message indicating the new showing status WHAT.
899WHAT is a value of nil, `never', or `always'."
900 (bs-message-without-log (cond ((null what)
dc7904f5
DL
901 "Buffer will be shown normally.")
902 ((eq what 'never)
903 "Mark buffer to never be shown.")
904 (t "Mark buffer to show always."))))
6448a6b3
GM
905
906(defun bs-delete ()
907 "Kill buffer on current line."
908 (interactive)
909 (let ((current (bs--current-buffer))
dc7904f5 910 (inhibit-read-only t))
706e6a52
EZ
911 (unless (kill-buffer current)
912 (error "Buffer was not deleted"))
6448a6b3 913 (setq bs-current-list (delq current bs-current-list))
6448a6b3
GM
914 (beginning-of-line)
915 (delete-region (point) (save-excursion
dc7904f5
DL
916 (end-of-line)
917 (if (eobp) (point) (1+ (point)))))
e749f576
JB
918 (when (eobp)
919 (backward-delete-char 1)
920 (beginning-of-line)
921 (recenter -1))
6448a6b3
GM
922 (bs--set-window-height)))
923
924(defun bs-delete-backward ()
925 "Like `bs-delete' but go to buffer in front of current."
926 (interactive)
927 (let ((on-last-line-p (save-excursion (end-of-line) (eobp))))
928 (bs-delete)
929 (unless on-last-line-p
dc7904f5 930 (bs-up 1))))
6448a6b3
GM
931
932(defun bs-show-sorted ()
5a25a895 933 "Show buffer list sorted by next sort aspect."
6448a6b3
GM
934 (interactive)
935 (setq bs--current-sort-function
dc7904f5
DL
936 (bs-next-config-aux (car bs--current-sort-function)
937 bs-sort-functions))
6448a6b3
GM
938 (bs--redisplay)
939 (bs--goto-current-buffer)
940 (bs-message-without-log "Sorted %s" (car bs--current-sort-function)))
941
942(defun bs-apply-sort-faces (&optional sort-description)
943 "Set text properties for the sort described by SORT-DESCRIPTION.
944SORT-DESCRIPTION is an element of `bs-sort-functions'.
945Default is `bs--current-sort-function'."
946 (let ((sort-description (or sort-description
dc7904f5 947 bs--current-sort-function)))
6448a6b3
GM
948 (save-excursion
949 (goto-char (point-min))
e749f576
JB
950 (when (and (nth 2 sort-description)
951 (search-forward-regexp (nth 2 sort-description) nil t))
952 (let ((inhibit-read-only t))
953 (put-text-property (match-beginning 0)
954 (match-end 0)
955 'face
956 (or (nth 3 sort-description)
957 'region)))))))
6448a6b3
GM
958
959(defun bs-toggle-show-all ()
960 "Toggle show all buffers / show buffers with current configuration."
961 (interactive)
962 (setq bs--show-all (not bs--show-all))
963 (bs--redisplay)
964 (bs--goto-current-buffer)
965 (bs-message-without-log "%s" (bs--current-config-message)))
966
967(defun bs-toggle-readonly ()
968 "Toggle read-only status for buffer on current line.
70ef8857 969Uses function `toggle-read-only'."
6448a6b3 970 (interactive)
70ef8857
SM
971 (with-current-buffer (bs--current-buffer)
972 (toggle-read-only))
973 (bs--update-current-line))
6448a6b3
GM
974
975(defun bs-clear-modified ()
976 "Set modified flag for buffer on current line to nil."
977 (interactive)
70ef8857
SM
978 (with-current-buffer (bs--current-buffer)
979 (set-buffer-modified-p nil))
6448a6b3
GM
980 (bs--update-current-line))
981
982(defun bs--nth-wrapper (count fun &rest args)
983 "Call COUNT times function FUN with arguments ARGS."
e749f576
JB
984 (dotimes (i (or count 1))
985 (apply fun args)))
6448a6b3
GM
986
987(defun bs-up (arg)
988 "Move cursor vertically up ARG lines in Buffer Selection Menu."
989 (interactive "p")
990 (if (and arg (numberp arg) (< arg 0))
991 (bs--nth-wrapper (- arg) 'bs--down)
992 (bs--nth-wrapper arg 'bs--up)))
993
994(defun bs--up ()
995 "Move cursor vertically up one line.
996If on top of buffer list go to last line."
ca6b8248
JB
997 (if (> (count-lines 1 (point)) bs-header-lines-length)
998 (forward-line -1)
999 (goto-char (point-max))
1000 (beginning-of-line)
1001 (recenter -1)))
6448a6b3
GM
1002
1003(defun bs-down (arg)
1004 "Move cursor vertically down ARG lines in Buffer Selection Menu."
1005 (interactive "p")
1006 (if (and arg (numberp arg) (< arg 0))
1007 (bs--nth-wrapper (- arg) 'bs--up)
1008 (bs--nth-wrapper arg 'bs--down)))
1009
1010(defun bs--down ()
1011 "Move cursor vertically down one line.
1012If at end of buffer list go to first line."
ca6b8248 1013 (if (eq (line-end-position) (point-max))
936934f5 1014 (progn
e600eb79 1015 (goto-char (point-min))
936934f5 1016 (forward-line bs-header-lines-length))
ca6b8248 1017 (forward-line 1)))
6448a6b3
GM
1018
1019(defun bs-visits-non-file (buffer)
b75ccc22 1020 "Return whether BUFFER visits no file.
6448a6b3
GM
1021A value of t means BUFFER belongs to no file.
1022A value of nil means BUFFER belongs to a file."
1023 (not (buffer-file-name buffer)))
1024
1025(defun bs-sort-buffer-interns-are-last (b1 b2)
b75ccc22 1026 "Function for sorting internal buffers at the end of all buffers."
e749f576 1027 (string-match-p "^\\*" (buffer-name b2)))
6448a6b3
GM
1028
1029;; ----------------------------------------------------------------------
1030;; Configurations:
1031;; ----------------------------------------------------------------------
1032
1033(defun bs-config-clear ()
e0d0133a 1034 "Reset all variables which specify a configuration.
6448a6b3
GM
1035These variables are `bs-dont-show-regexp', `bs-must-show-regexp',
1036`bs-dont-show-function', `bs-must-show-function' and
1037`bs-buffer-sort-function'."
1038 (setq bs-dont-show-regexp nil
dc7904f5
DL
1039 bs-must-show-regexp nil
1040 bs-dont-show-function nil
1041 bs-must-show-function nil
1042 bs-buffer-sort-function nil))
6448a6b3
GM
1043
1044(defun bs-config--only-files ()
1045 "Define a configuration for showing only buffers visiting a file."
1046 (bs-config-clear)
880ed9f1 1047 (setq ;; I want to see *-buffers at the end
dc7904f5
DL
1048 bs-buffer-sort-function 'bs-sort-buffer-interns-are-last
1049 ;; Don't show files who don't belong to a file
1050 bs-dont-show-function 'bs-visits-non-file))
6448a6b3
GM
1051
1052(defun bs-config--files-and-scratch ()
1053 "Define a configuration for showing buffer *scratch* and file buffers."
1054 (bs-config-clear)
880ed9f1 1055 (setq ;; I want to see *-buffers at the end
dc7904f5
DL
1056 bs-buffer-sort-function 'bs-sort-buffer-interns-are-last
1057 ;; Don't show files who don't belong to a file
1058 bs-dont-show-function 'bs-visits-non-file
1059 ;; Show *scratch* buffer.
880ed9f1 1060 bs-must-show-regexp "^\\*scratch\\*$"))
6448a6b3
GM
1061
1062(defun bs-config--all ()
1063 "Define a configuration for showing all buffers.
1064Reset all according variables by `bs-config-clear'."
1065 (bs-config-clear))
1066
1067(defun bs-config--all-intern-last ()
1068 "Define a configuration for showing all buffers.
880ed9f1 1069Internal buffers appear at end of all buffers."
6448a6b3
GM
1070 (bs-config-clear)
1071 ;; I want to see *-buffers at the end
1072 (setq bs-buffer-sort-function 'bs-sort-buffer-interns-are-last))
1073
1074(defun bs-set-configuration (name)
1075 "Set configuration to the one saved under string NAME in `bs-configurations'.
1076When called interactively ask user for a configuration and apply selected
1077configuration."
1078 (interactive (list (completing-read "Use configuration: "
dc7904f5
DL
1079 bs-configurations
1080 nil
1081 t)))
6448a6b3
GM
1082 (let ((list (assoc name bs-configurations)))
1083 (if list
dc7904f5
DL
1084 (if (listp list)
1085 (setq bs-current-configuration name
1086 bs-must-show-regexp (nth 1 list)
1087 bs-must-show-function (nth 2 list)
1088 bs-dont-show-regexp (nth 3 list)
1089 bs-dont-show-function (nth 4 list)
1090 bs-buffer-sort-function (nth 5 list))
1091 ;; for backward compability
1092 (funcall (cdr list)))
6448a6b3
GM
1093 ;; else
1094 (ding)
1095 (bs-message-without-log "No bs-configuration named %S." name))))
1096
1097(defun bs-help ()
1098 "Help for `bs-show'."
1099 (interactive)
1100 (describe-function 'bs-mode))
1101
1102(defun bs-next-config-aux (start-name list)
1103 "Get the next assoc after START-NAME in list LIST.
1104Will return the first if START-NAME is at end."
1105 (let ((assocs list)
dc7904f5
DL
1106 (length (length list))
1107 pos)
6448a6b3 1108 (while (and assocs (not pos))
e749f576
JB
1109 (when (string= (car (car assocs)) start-name)
1110 (setq pos (- length (length assocs))))
6448a6b3
GM
1111 (setq assocs (cdr assocs)))
1112 (setq pos (1+ pos))
1113 (if (eq pos length)
dc7904f5 1114 (car list)
6448a6b3
GM
1115 (nth pos list))))
1116
1117(defun bs-next-config (name)
1118 "Return next configuration with respect to configuration with name NAME."
1119 (bs-next-config-aux name bs-configurations))
1120
1121(defun bs-select-next-configuration (&optional start-name)
1122 "Apply next configuration START-NAME and refresh buffer list.
1123If START-NAME is nil the current configuration `bs-current-configuration'
1124will be used."
1125 (interactive)
1126 (let ((config (bs-next-config (or start-name bs-current-configuration))))
1127 (bs-set-configuration (car config))
1128 (setq bs-default-configuration bs-current-configuration)
1129 (bs--redisplay t)
1130 (bs--set-window-height)
880ed9f1 1131 (bs-message-without-log "Selected configuration: %s" (car config))))
6448a6b3
GM
1132
1133(defun bs-show-in-buffer (list)
1134 "Display buffer list LIST in buffer *buffer-selection*.
1135Select buffer *buffer-selection* and display buffers according to current
1136configuration `bs-current-configuration'. Set window height, fontify buffer
1137and move point to current buffer."
1138 (setq bs-current-list list)
1139 (switch-to-buffer (get-buffer-create "*buffer-selection*"))
1140 (bs-mode)
1141 (let* ((inhibit-read-only t)
dc7904f5
DL
1142 (map-fun (lambda (entry)
1143 (length (buffer-name entry))))
1144 (max-length-of-names (apply 'max
1145 (cons 0 (mapcar map-fun list))))
1146 (name-entry-length (min bs-maximal-buffer-name-column
1147 (max bs-minimal-buffer-name-column
1148 max-length-of-names))))
6448a6b3
GM
1149 (erase-buffer)
1150 (setq bs--name-entry-length name-entry-length)
1151 (bs--show-header)
e749f576
JB
1152 (dolist (buffer list)
1153 (bs--insert-one-entry buffer)
1154 (insert "\n"))
6448a6b3
GM
1155 (delete-backward-char 1)
1156 (bs--set-window-height)
1157 (bs--goto-current-buffer)
1158 (font-lock-fontify-buffer)
310a26be
JB
1159 (bs-apply-sort-faces)
1160 (set-buffer-modified-p nil)))
6448a6b3
GM
1161
1162(defun bs-next-buffer (&optional buffer-list sorting-p)
1163 "Return next buffer and buffer list for buffer cycling in BUFFER-LIST.
1164Ignore sorting when SORTING-P is nil.
1165If BUFFER-LIST is nil the result of `bs-buffer-list' will be used as
1166buffer list. The result is a cons of normally the second element of
1167BUFFER-LIST and the buffer list used for buffer cycling."
1168 (let* ((bs--current-sort-function (if sorting-p
dc7904f5
DL
1169 bs--current-sort-function))
1170 (bs-buffer-list (or buffer-list (bs-buffer-list))))
6448a6b3 1171 (cons (or (car (cdr bs-buffer-list))
dc7904f5
DL
1172 (car bs-buffer-list)
1173 (current-buffer))
1174 bs-buffer-list)))
6448a6b3
GM
1175
1176(defun bs-previous-buffer (&optional buffer-list sorting-p)
1177 "Return previous buffer and buffer list for buffer cycling in BUFFER-LIST.
1178Ignore sorting when SORTING-P is nil.
1179If BUFFER-LIST is nil the result of `bs-buffer-list' will be used as
1180buffer list. The result is a cons of last element of BUFFER-LIST and the
1181buffer list used for buffer cycling."
1182 (let* ((bs--current-sort-function (if sorting-p
dc7904f5
DL
1183 bs--current-sort-function))
1184 (bs-buffer-list (or buffer-list (bs-buffer-list))))
6448a6b3 1185 (cons (or (car (last bs-buffer-list))
dc7904f5
DL
1186 (current-buffer))
1187 bs-buffer-list)))
6448a6b3
GM
1188
1189(defun bs-message-without-log (&rest args)
1190 "Like `message' but don't log it on the message log.
a1d5a11b 1191All arguments ARGS are transferred to function `message'."
6448a6b3
GM
1192 (let ((message-log-max nil))
1193 (apply 'message args)))
1194
1195(defvar bs--cycle-list nil
880ed9f1 1196 "Current buffer list used for cycling.")
6448a6b3
GM
1197
1198;;;###autoload
1199(defun bs-cycle-next ()
1200 "Select next buffer defined by buffer cycling.
1201The buffers taking part in buffer cycling are defined
1202by buffer configuration `bs-cycle-configuration-name'."
1203 (interactive)
1204 (let ((bs--buffer-coming-from (current-buffer))
dc7904f5
DL
1205 (bs-dont-show-regexp bs-dont-show-regexp)
1206 (bs-must-show-regexp bs-must-show-regexp)
1207 (bs-dont-show-function bs-dont-show-function)
1208 (bs-must-show-function bs-must-show-function)
038e04cb
GM
1209 (bs--show-all nil))
1210 (bs-set-configuration (or bs-cycle-configuration-name bs-default-configuration))
6448a6b3 1211 (let ((bs-buffer-sort-function nil)
dc7904f5 1212 (bs--current-sort-function nil))
6448a6b3 1213 (let* ((tupel (bs-next-buffer (if (or (eq last-command
dc7904f5
DL
1214 'bs-cycle-next)
1215 (eq last-command
1216 'bs-cycle-previous))
1217 bs--cycle-list)))
1218 (next (car tupel))
1219 (cycle-list (cdr tupel)))
b49bf494
CY
1220 (unless (window-dedicated-p (selected-window))
1221 ;; We don't want the frame iconified if the only window in the frame
1222 ;; happens to be dedicated; let's get the error from switch-to-buffer
1223 (bury-buffer))
1224 (switch-to-buffer next)
dc7904f5
DL
1225 (setq bs--cycle-list (append (cdr cycle-list)
1226 (list (car cycle-list))))
dc7904f5
DL
1227 (bs-message-without-log "Next buffers: %s"
1228 (or (cdr bs--cycle-list)
1229 "this buffer"))))))
6448a6b3 1230
6448a6b3
GM
1231;;;###autoload
1232(defun bs-cycle-previous ()
1233 "Select previous buffer defined by buffer cycling.
1234The buffers taking part in buffer cycling are defined
1235by buffer configuration `bs-cycle-configuration-name'."
1236 (interactive)
1237 (let ((bs--buffer-coming-from (current-buffer))
dc7904f5
DL
1238 (bs-dont-show-regexp bs-dont-show-regexp)
1239 (bs-must-show-regexp bs-must-show-regexp)
1240 (bs-dont-show-function bs-dont-show-function)
1241 (bs-must-show-function bs-must-show-function)
038e04cb
GM
1242 (bs--show-all nil))
1243 (bs-set-configuration (or bs-cycle-configuration-name bs-default-configuration))
6448a6b3 1244 (let ((bs-buffer-sort-function nil)
dc7904f5 1245 (bs--current-sort-function nil))
6448a6b3 1246 (let* ((tupel (bs-previous-buffer (if (or (eq last-command
dc7904f5
DL
1247 'bs-cycle-next)
1248 (eq last-command
1249 'bs-cycle-previous))
1250 bs--cycle-list)))
1251 (prev-buffer (car tupel))
1252 (cycle-list (cdr tupel)))
b49bf494 1253 (switch-to-buffer prev-buffer)
dc7904f5
DL
1254 (setq bs--cycle-list (append (last cycle-list)
1255 (reverse (cdr (reverse cycle-list)))))
dc7904f5
DL
1256 (bs-message-without-log "Previous buffers: %s"
1257 (or (reverse (cdr bs--cycle-list))
1258 "this buffer"))))))
6448a6b3
GM
1259
1260(defun bs--get-value (fun &optional args)
1261 "Apply function FUN with arguments ARGS.
1262Return result of evaluation. Will return FUN if FUN is a number
1263or a string."
1264 (cond ((numberp fun)
dc7904f5
DL
1265 fun)
1266 ((stringp fun)
1267 fun)
1268 (t (apply fun args))))
6448a6b3
GM
1269
1270(defun bs--get-marked-string (start-buffer all-buffers)
1271 "Return a string which describes whether current buffer is marked.
1272START-BUFFER is the buffer where we started buffer selection.
462db93c 1273ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu.
6448a6b3
GM
1274The result string is one of `bs-string-current', `bs-string-current-marked',
1275`bs-string-marked', `bs-string-show-normally', `bs-string-show-never', or
1276`bs-string-show-always'."
880ed9f1 1277 (cond ;; current buffer is the buffer we started buffer selection.
dc7904f5
DL
1278 ((eq (current-buffer) start-buffer)
1279 (if (memq (current-buffer) bs--marked-buffers)
1280 bs-string-current-marked ; buffer is marked
1281 bs-string-current))
1282 ;; current buffer is marked
1283 ((memq (current-buffer) bs--marked-buffers)
1284 bs-string-marked)
1285 ;; current buffer hasn't a special mark.
1286 ((null bs-buffer-show-mark)
1287 bs-string-show-normally)
1288 ;; current buffer has a mark not to show itself.
1289 ((eq bs-buffer-show-mark 'never)
1290 bs-string-show-never)
1291 ;; otherwise current buffer is marked to show always.
1292 (t
1293 bs-string-show-always)))
6448a6b3
GM
1294
1295(defun bs--get-modified-string (start-buffer all-buffers)
1296 "Return a string which describes whether current buffer is modified.
1297START-BUFFER is the buffer where we started buffer selection.
462db93c 1298ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
6448a6b3
GM
1299 (if (buffer-modified-p) "*" " "))
1300
1301(defun bs--get-readonly-string (start-buffer all-buffers)
1302 "Return a string which describes whether current buffer is read only.
1303START-BUFFER is the buffer where we started buffer selection.
462db93c 1304ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
6448a6b3
GM
1305 (if buffer-read-only "%" " "))
1306
1307(defun bs--get-size-string (start-buffer all-buffers)
1308 "Return a string which describes the size of current buffer.
1309START-BUFFER is the buffer where we started buffer selection.
462db93c 1310ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
6448a6b3
GM
1311 (int-to-string (buffer-size)))
1312
1313(defun bs--get-name (start-buffer all-buffers)
1314 "Return name of current buffer for Buffer Selection Menu.
1315The name of current buffer gets additional text properties
1316for mouse highlighting.
1317START-BUFFER is the buffer where we started buffer selection.
462db93c 1318ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
635ed242
JB
1319 (propertize (buffer-name)
1320 'help-echo "mouse-2: select this buffer, mouse-3: select in other frame"
1321 'mouse-face 'highlight))
6448a6b3 1322
6448a6b3
GM
1323(defun bs--get-mode-name (start-buffer all-buffers)
1324 "Return the name of mode of current buffer for Buffer Selection Menu.
1325START-BUFFER is the buffer where we started buffer selection.
462db93c 1326ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
c2699583 1327 (format-mode-line mode-name nil nil start-buffer))
6448a6b3
GM
1328
1329(defun bs--get-file-name (start-buffer all-buffers)
1330 "Return string for column 'File' in Buffer Selection Menu.
1331This is the variable `buffer-file-name' of current buffer.
027b1942 1332If not visiting a file, `list-buffers-directory' is returned instead.
6448a6b3 1333START-BUFFER is the buffer where we started buffer selection.
b75ccc22 1334ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
027b1942
JB
1335 (propertize (or buffer-file-name
1336 (bound-and-true-p list-buffers-directory)
1337 "")
310a26be
JB
1338 'mouse-face 'highlight
1339 'help-echo "mouse-2: select this buffer, mouse-3: select in other frame"))
6448a6b3 1340
6448a6b3
GM
1341(defun bs--insert-one-entry (buffer)
1342 "Generate one entry for buffer BUFFER in Buffer Selection Menu.
1343It goes over all columns described in `bs-attributes-list'
1344and evaluates corresponding string. Inserts string in current buffer;
1345normally *buffer-selection*."
1346 (let ((string "")
dc7904f5 1347 (to-much 0)
6448a6b3 1348 (apply-args (append (list bs--buffer-coming-from bs-current-list))))
70ef8857 1349 (with-current-buffer buffer
e749f576
JB
1350 (dolist (column bs-attributes-list)
1351 (let* ((min (bs--get-value (nth 1 column)))
1352 (new-string (bs--format-aux (bs--get-value (nth 4 column) ; fun
1353 apply-args)
1354 (nth 3 column) ; align
1355 (- min to-much)))
1356 (len (length new-string)))
dc7904f5 1357 (setq string (concat string new-string))
e749f576
JB
1358 (when (> len min)
1359 (setq to-much (- len min))))))
1360 (insert string)))
6448a6b3
GM
1361
1362(defun bs--format-aux (string align len)
462db93c 1363 "Pad STRING to length LEN with alignment ALIGN.
6448a6b3 1364ALIGN is one of the symbols `left', `middle', or `right'."
635ed242
JB
1365 (let* ((width (length string))
1366 (len (max len width)))
1367 (format (format "%%%s%ds" (if (eq align 'right) "" "-") len)
1368 (if (eq align 'middle)
1369 (concat (make-string (/ (- len width) 2) ?\s) string)
1370 string))))
6448a6b3
GM
1371
1372(defun bs--show-header ()
1373 "Insert header for Buffer Selection Menu in current buffer."
e749f576
JB
1374 (insert (bs--create-header-line #'identity)
1375 "\n"
1376 (bs--create-header-line (lambda (title)
1377 (make-string (length title) ?-)))
1378 "\n"))
6448a6b3
GM
1379
1380(defun bs--get-name-length ()
1381 "Return value of `bs--name-entry-length'."
1382 bs--name-entry-length)
1383
e749f576
JB
1384(defun bs--create-header-line (col)
1385 "Generate a line for the header.
1386COL is called for each column in `bs-attributes-list' as a
1387function of one argument, the string heading for the column."
1388 (mapconcat (lambda (column)
1389 (bs--format-aux (funcall col (bs--get-value (car column)))
1390 (nth 3 column) ; align
1391 (bs--get-value (nth 1 column))))
1392 bs-attributes-list
1393 ""))
6448a6b3
GM
1394
1395(defun bs--show-with-configuration (name &optional arg)
880ed9f1 1396 "Display buffer list of configuration with name NAME.
6448a6b3
GM
1397Set configuration NAME and determine window for Buffer Selection Menu.
1398Unless current buffer is buffer *buffer-selection* we have to save
1399the buffer we started Buffer Selection Menu and the current window
1400configuration to restore buffer and window configuration after a
1401selection. If there is already a window displaying *buffer-selection*
1402select this window for Buffer Selection Menu. Otherwise open a new
1403window.
1404The optional argument ARG is the prefix argument when calling a function
1405for buffer selection."
1406 (bs-set-configuration name)
1407 (let ((bs--show-all (or bs--show-all arg)))
dc7904f5 1408 (unless (string= "*buffer-selection*" (buffer-name))
6448a6b3
GM
1409 ;; Only when not in buffer *buffer-selection*
1410 ;; we have to set the buffer we started the command
ce3ba12c 1411 (setq bs--buffer-coming-from (current-buffer)))
dc7904f5 1412 (let ((liste (bs-buffer-list))
edf6fbc5 1413 (active-window (get-window-with-predicate
e749f576
JB
1414 (lambda (w)
1415 (string= (buffer-name (window-buffer w))
1416 "*buffer-selection*"))
1417 nil (selected-frame))))
dc7904f5
DL
1418 (if active-window
1419 (select-window active-window)
e749f576
JB
1420 (bs--restore-window-config)
1421 (setq bs--window-config-coming-from (current-window-configuration))
ce3ba12c
JB
1422 (when (> (window-height (selected-window)) 7)
1423 (split-window-vertically)
1424 (other-window 1)))
dc7904f5
DL
1425 (bs-show-in-buffer liste)
1426 (bs-message-without-log "%s" (bs--current-config-message)))))
6448a6b3
GM
1427
1428(defun bs--configuration-name-for-prefix-arg (prefix-arg)
1429 "Convert prefix argument PREFIX-ARG to a name of a buffer configuration.
1430If PREFIX-ARG is nil return `bs-default-configuration'.
1431If PREFIX-ARG is an integer return PREFIX-ARG element of `bs-configurations'.
1432Otherwise return `bs-alternative-configuration'."
880ed9f1 1433 (cond ;; usually activation
dc7904f5
DL
1434 ((null prefix-arg)
1435 bs-default-configuration)
1436 ;; call with integer as prefix argument
1437 ((integerp prefix-arg)
1438 (if (and (< 0 prefix-arg) (<= prefix-arg (length bs-configurations)))
1439 (car (nth (1- prefix-arg) bs-configurations))
1440 bs-default-configuration))
1441 ;; call by prefix argument C-u
1442 (t bs-alternative-configuration)))
6448a6b3
GM
1443
1444;; ----------------------------------------------------------------------
1445;; Main function bs-customize and bs-show
1446;; ----------------------------------------------------------------------
1447
1448;;;###autoload
1449(defun bs-customize ()
1450 "Customization of group bs for Buffer Selection Menu."
1451 (interactive)
1452 (customize-group "bs"))
1453
1454;;;###autoload
1455(defun bs-show (arg)
dc7904f5 1456 "Make a menu of buffers so you can manipulate buffers or the buffer list.
6448a6b3
GM
1457\\<bs-mode-map>
1458There are many key commands similar to `Buffer-menu-mode' for
b75ccc22 1459manipulating the buffer list and the buffers themselves.
6448a6b3
GM
1460User can move with [up] or [down], select a buffer
1461by \\[bs-select] or [SPC]\n
1462Type \\[bs-kill] to leave Buffer Selection Menu without a selection.
1463Type \\[bs-help] after invocation to get help on commands available.
1464With prefix argument ARG show a different buffer list. Function
1465`bs--configuration-name-for-prefix-arg' determine accordingly
1466name of buffer configuration."
1467 (interactive "P")
1468 (setq bs--marked-buffers nil)
1469 (bs--show-with-configuration (bs--configuration-name-for-prefix-arg arg)))
1470
c4384ef1
JB
1471;; ----------------------------------------------------------------------
1472;; Cleanup
1473;; ----------------------------------------------------------------------
1474
1475(defun bs-unload-function ()
1476 "Unload the Buffer Selection library."
1477 (let ((bs-buf (get-buffer "*buffer-selection*")))
1478 (when bs-buf
1479 (with-current-buffer bs-buf
1480 (when (eq major-mode 'bs-mode)
1481 (bs-kill)
1482 (kill-buffer bs-buf)))))
1483 ;; continue standard unloading
1484 nil)
1485
d8754ce5 1486;; Now provide feature bs
6448a6b3
GM
1487(provide 'bs)
1488
48d33090 1489;; arch-tag: c0d9ab34-bf06-4368-ae9d-af88878e6802
6448a6b3 1490;;; bs.el ends here