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