* admin/gitmerge.el (gitmerge-skip-regexp): Be stricter in batch mode.
[emacs.git] / lisp / bs.el
blob07d23e465e363548f6fd188d75a34399795548e5
1 ;;; bs.el --- menu for selecting and displaying buffers -*- lexical-binding: t -*-
3 ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
4 ;; Author: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
5 ;; Maintainer: emacs-devel@gnu.org
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 <https://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 "U" 'bs-unmark-all)
495 (define-key map "\177" 'bs-unmark-previous)
496 (define-key map ">" 'scroll-right)
497 (define-key map "<" 'scroll-left)
498 (define-key map "?" 'bs-help)
499 map)
500 "Keymap of `bs-mode'.")
502 ;; ----------------------------------------------------------------------
503 ;; Functions
504 ;; ----------------------------------------------------------------------
506 (defun bs-buffer-list (&optional list sort-description)
507 "Return a list of buffers to be shown.
508 LIST is a list of buffers to test for appearance in Buffer Selection Menu.
509 The result list depends on the global variables `bs-dont-show-regexp',
510 `bs-must-show-regexp', `bs-dont-show-function', `bs-must-show-function'
511 and `bs-buffer-sort-function'.
512 If SORT-DESCRIPTION isn't nil the list will be sorted by
513 a special function. SORT-DESCRIPTION is an element of `bs-sort-functions'."
514 (setq sort-description (or sort-description bs--current-sort-function)
515 list (or list (buffer-list)))
516 (let ((result nil))
517 (dolist (buf list)
518 (let* ((buffername (buffer-name buf))
519 (int-show-never (string-match-p bs--intern-show-never buffername))
520 (ext-show-never (and bs-dont-show-regexp
521 (string-match-p bs-dont-show-regexp
522 buffername)))
523 (extern-must-show (or (and bs-must-always-show-regexp
524 (string-match-p
525 bs-must-always-show-regexp
526 buffername))
527 (and bs-must-show-regexp
528 (string-match-p bs-must-show-regexp
529 buffername))))
530 (extern-show-never-from-fun (and bs-dont-show-function
531 (funcall bs-dont-show-function
532 buf)))
533 (extern-must-show-from-fun (and bs-must-show-function
534 (funcall bs-must-show-function
535 buf)))
536 (show-flag (buffer-local-value 'bs-buffer-show-mark buf)))
537 (when (or (eq show-flag 'always)
538 (and (or bs--show-all (not (eq show-flag 'never)))
539 (not int-show-never)
540 (or bs--show-all
541 extern-must-show
542 extern-must-show-from-fun
543 (and (not ext-show-never)
544 (not extern-show-never-from-fun)))))
545 (setq result (cons buf result)))))
546 (setq result (reverse result))
547 ;; The current buffer which was the start point of bs should be an element
548 ;; of result list, so that we can leave with space and be back in the
549 ;; buffer we started bs-show.
550 (when (and bs--buffer-coming-from
551 (buffer-live-p bs--buffer-coming-from)
552 (not (memq bs--buffer-coming-from result)))
553 (setq result (cons bs--buffer-coming-from result)))
554 ;; sorting
555 (if (and sort-description
556 (nth 1 sort-description))
557 (setq result (sort result (nth 1 sort-description)))
558 ;; else standard sorting
559 (bs-buffer-sort result))))
561 (defun bs-buffer-sort (buffer-list)
562 "Sort buffers in BUFFER-LIST according to `bs-buffer-sort-function'."
563 (if bs-buffer-sort-function
564 (sort buffer-list bs-buffer-sort-function)
565 buffer-list))
567 (defun bs--redisplay (&optional keep-line-p sort-description)
568 "Redisplay whole Buffer Selection Menu.
569 If KEEP-LINE-P is non-nil the point will stay on current line.
570 SORT-DESCRIPTION is an element of `bs-sort-functions'."
571 (let ((line (count-lines 1 (point))))
572 (bs-show-in-buffer (bs-buffer-list nil sort-description))
573 (when keep-line-p
574 (goto-char (point-min))
575 (forward-line line))
576 (beginning-of-line)))
578 (defun bs--goto-current-buffer ()
579 "Goto line which represents the current buffer;
580 actually the line which begins with character in `bs-string-current' or
581 `bs-string-current-marked'."
582 (let ((regexp (concat "^"
583 (regexp-quote bs-string-current)
584 "\\|^"
585 (regexp-quote bs-string-current-marked)))
586 point)
587 (save-excursion
588 (goto-char (point-min))
589 (when (search-forward-regexp regexp nil t)
590 (setq point (1- (point)))))
591 (when point
592 (goto-char point))))
594 (defun bs--current-config-message ()
595 "Return a string describing the current `bs-mode' configuration."
596 (if bs--show-all
597 "Show all buffers."
598 (format "Show buffer by configuration %S"
599 bs-current-configuration)))
601 (defun bs--track-window-changes (frame)
602 "Track window changes to refresh the buffer list.
603 Used from `window-size-change-functions'."
604 (let ((win (get-buffer-window "*buffer-selection*" frame)))
605 (when win
606 (with-selected-window win
607 (bs--set-window-height)))))
609 (defun bs--remove-hooks ()
610 "Remove `bs--track-window-changes' and auxiliary hooks."
611 (remove-hook 'window-size-change-functions 'bs--track-window-changes)
612 ;; Remove itself
613 (remove-hook 'kill-buffer-hook 'bs--remove-hooks t)
614 (remove-hook 'change-major-mode-hook 'bs--remove-hooks t))
616 (put 'bs-mode 'mode-class 'special)
618 (define-derived-mode bs-mode nil "Buffer-Selection-Menu"
619 "Major mode for editing a subset of Emacs's buffers.
620 \\<bs-mode-map>
621 Aside from two header lines each line describes one buffer.
622 Move to a line representing the buffer you want to edit and select
623 buffer by \\[bs-select] or SPC. Abort buffer list with \\[bs-kill].
624 There are many key commands similar to `Buffer-menu-mode' for
625 manipulating the buffer list and buffers.
626 For faster navigation each digit key is a digit argument.
628 \\[bs-select] or SPACE -- select current line's buffer and other marked buffers.
629 \\[bs-toggle-show-all] -- toggle between all buffers and a special subset.
630 \\[bs-select-other-window] -- select current line's buffer in other window.
631 \\[bs-tmp-select-other-window] -- make another window display that buffer and
632 remain in Buffer Selection Menu.
633 \\[bs-mouse-select] -- select current line's buffer and other marked buffers.
634 \\[bs-save] -- save current line's buffer immediately.
635 \\[bs-delete] -- kill current line's buffer immediately.
636 \\[bs-toggle-readonly] -- toggle read-only status of current line's buffer.
637 \\[bs-clear-modified] -- clear modified-flag on that buffer.
638 \\[bs-mark-current] -- mark current line's buffer to be displayed.
639 \\[bs-unmark-current] -- unmark current line's buffer to be displayed.
640 \\[bs-unmark-all] -- unmark all buffer lines.
641 \\[bs-unmark-previous] -- unmark previous line's buffer to be displayed.
642 \\[bs-show-sorted] -- display buffer list sorted by next sort aspect.
643 \\[bs-set-configuration-and-refresh] -- ask user for a configuration and \
644 apply selected configuration.
645 \\[bs-select-next-configuration] -- select and apply next \
646 available Buffer Selection Menu configuration.
647 \\[bs-kill] -- leave Buffer Selection Menu without a selection.
648 \\[bs-toggle-current-to-show] -- toggle status of appearance.
649 \\[bs-set-current-buffer-to-show-always] -- mark current line's buffer \
650 to show always.
651 \\[bs-visit-tags-table] -- call `visit-tags-table' on current line's buffer.
652 \\[bs-help] -- display this help text."
653 (buffer-disable-undo)
654 (setq buffer-read-only t
655 truncate-lines t
656 show-trailing-whitespace nil)
657 (setq-local font-lock-defaults '(bs-mode-font-lock-keywords t))
658 (setq-local font-lock-verbose nil)
659 (setq-local font-lock-global-modes '(not bs-mode))
660 (setq-local revert-buffer-function 'bs-refresh)
661 (add-hook 'window-size-change-functions 'bs--track-window-changes)
662 (add-hook 'kill-buffer-hook 'bs--remove-hooks nil t)
663 (add-hook 'change-major-mode-hook 'bs--remove-hooks nil t))
665 (defun bs--restore-window-config ()
666 "Restore window configuration on the current frame."
667 (when bs--window-config-coming-from
668 (let ((frame (selected-frame)))
669 (unwind-protect
670 (set-window-configuration bs--window-config-coming-from)
671 (select-frame frame)))
672 (setq bs--window-config-coming-from nil)))
674 (defun bs-kill ()
675 "Let buffer disappear and reset window configuration."
676 (interactive)
677 (bury-buffer (current-buffer))
678 (bs--restore-window-config))
680 (defun bs-abort ()
681 "Ding and leave Buffer Selection Menu without a selection."
682 (interactive)
683 (ding)
684 (bs-kill))
686 (defun bs-set-configuration-and-refresh ()
687 "Ask user for a configuration and apply selected configuration.
688 Refresh whole Buffer Selection Menu."
689 (interactive)
690 (call-interactively 'bs-set-configuration)
691 (bs--redisplay t))
693 (defun bs-refresh (&rest _ignored)
694 "Refresh whole Buffer Selection Menu.
695 Arguments are IGNORED (for `revert-buffer')."
696 (interactive)
697 (bs--redisplay t))
699 (defun bs--set-window-height ()
700 "Change the height of the selected window to suit the current buffer list."
701 (unless (one-window-p t)
702 (fit-window-to-buffer (selected-window) bs-max-window-height)))
704 (defun bs--current-buffer ()
705 "Return buffer on current line.
706 Raise an error if not on a buffer line."
707 (beginning-of-line)
708 (let ((line (+ (- bs-header-lines-length)
709 (count-lines 1 (point)))))
710 (when (< line 0)
711 (error "You are on a header row"))
712 (nth line bs-current-list)))
714 (defun bs--update-current-line ()
715 "Update the entry on current line for Buffer Selection Menu."
716 (let ((buffer (bs--current-buffer))
717 (inhibit-read-only t))
718 (beginning-of-line)
719 (delete-region (point) (line-end-position))
720 (bs--insert-one-entry buffer)
721 (beginning-of-line)))
723 (defun bs-view ()
724 "View current line's buffer in View mode.
725 Leave Buffer Selection Menu."
726 (interactive)
727 (view-buffer (bs--current-buffer)))
729 (defun bs-select ()
730 "Select current line's buffer and other marked buffers.
731 If there are no marked buffers the window configuration before starting
732 Buffer Selection Menu will be restored.
733 If there are marked buffers each marked buffer and the current line's buffer
734 will be selected in a window.
735 Leave Buffer Selection Menu."
736 (interactive)
737 (let ((buffer (bs--current-buffer)))
738 (bury-buffer (current-buffer))
739 (bs--restore-window-config)
740 (switch-to-buffer buffer)
741 (when bs--marked-buffers
742 ;; Some marked buffers for selection
743 (let* ((all (delq buffer bs--marked-buffers))
744 (height (/ (1- (frame-height)) (1+ (length all)))))
745 (delete-other-windows)
746 (switch-to-buffer buffer)
747 (dolist (buf all)
748 (split-window nil height)
749 (other-window 1)
750 (switch-to-buffer buf))
751 ;; goto window we have started bs.
752 (other-window 1)))))
754 (defun bs-select-other-window ()
755 "Select current line's buffer by `switch-to-buffer-other-window'.
756 The window configuration before starting Buffer Selection Menu will be restored
757 unless there is no other window. In this case a new window will be created.
758 Leave Buffer Selection Menu."
759 (interactive)
760 (let ((buffer (bs--current-buffer)))
761 (bury-buffer (current-buffer))
762 (bs--restore-window-config)
763 (switch-to-buffer-other-window buffer)))
765 (defun bs-tmp-select-other-window ()
766 "Make the other window select this line's buffer.
767 The current window remains selected."
768 (interactive)
769 (let ((buffer (bs--current-buffer)))
770 (display-buffer buffer t)))
772 (defun bs-select-other-frame ()
773 "Select current line's buffer in new created frame.
774 Leave Buffer Selection Menu."
775 (interactive)
776 (let ((buffer (bs--current-buffer)))
777 (bury-buffer (current-buffer))
778 (bs--restore-window-config)
779 (switch-to-buffer-other-frame buffer)))
781 (defun bs-mouse-select-other-frame (event)
782 "Select selected line's buffer in new created frame.
783 Leave Buffer Selection Menu.
784 EVENT: a mouse click event."
785 (interactive "e")
786 (mouse-set-point event)
787 (bs-select-other-frame))
789 (defun bs-mouse-select (event)
790 "Select buffer on mouse click EVENT.
791 Select buffer by `bs-select'."
792 (interactive "e")
793 (mouse-set-point event)
794 (bs-select))
796 (defun bs-select-in-one-window ()
797 "Select current line's buffer in one window and delete other windows.
798 Leave Buffer Selection Menu."
799 (interactive)
800 (bs-select)
801 (delete-other-windows))
803 (defun bs-bury-buffer ()
804 "Bury buffer on current line."
805 (interactive)
806 (bury-buffer (bs--current-buffer))
807 (bs--redisplay t))
809 (defun bs-save ()
810 "Save buffer on current line."
811 (interactive)
812 (with-current-buffer (bs--current-buffer)
813 (save-buffer))
814 (bs--update-current-line))
816 (defun bs-visit-tags-table ()
817 "Visit the tags table in the buffer on this line.
818 See `visit-tags-table'."
819 (interactive)
820 (let ((file (buffer-file-name (bs--current-buffer))))
821 (if file
822 (visit-tags-table file)
823 (error "Specified buffer has no file"))))
825 (defun bs-toggle-current-to-show ()
826 "Toggle status of showing flag for buffer in current line."
827 (interactive)
828 (let ((res
829 (with-current-buffer (bs--current-buffer)
830 (setq bs-buffer-show-mark (pcase bs-buffer-show-mark
831 (`nil 'never)
832 (`never 'always)
833 (_ nil))))))
834 (bs--update-current-line)
835 (bs--set-window-height)
836 (bs--show-config-message res)))
838 (defun bs-set-current-buffer-to-show-always (&optional not-to-show-p)
839 "Toggle status of buffer on line to `always shown'.
840 NOT-TO-SHOW-P: prefix argument.
841 With no prefix argument the buffer on current line is marked to show
842 always. Otherwise it is marked to show never."
843 (interactive "P")
844 (if not-to-show-p
845 (bs-set-current-buffer-to-show-never)
846 (bs--set-toggle-to-show (bs--current-buffer) 'always)))
848 (defun bs-set-current-buffer-to-show-never ()
849 "Toggle status of buffer on line to `never shown'."
850 (interactive)
851 (bs--set-toggle-to-show (bs--current-buffer) 'never))
853 (defun bs--set-toggle-to-show (buffer what)
854 "Set value `bs-buffer-show-mark' of buffer BUFFER to WHAT.
855 Redisplay current line and display a message describing
856 the status of buffer on current line."
857 (with-current-buffer buffer (setq bs-buffer-show-mark what))
858 (bs--update-current-line)
859 (bs--set-window-height)
860 (bs--show-config-message what))
862 (defun bs--mark-unmark (count fun)
863 "Call FUN on COUNT consecutive buffers of *buffer-selection*."
864 (let ((dir (if (> count 0) 1 -1)))
865 (dotimes (_i (abs count))
866 (let ((buffer (bs--current-buffer)))
867 (when buffer (funcall fun buffer))
868 (bs--update-current-line)
869 (bs-down dir)))))
871 (defun bs-mark-current (count)
872 "Mark buffers.
873 COUNT is the number of buffers to mark.
874 Move point vertically down COUNT lines."
875 (interactive "p")
876 (bs--mark-unmark count
877 (lambda (buf)
878 (add-to-list 'bs--marked-buffers buf))))
880 (defun bs-unmark-current (count)
881 "Unmark buffers.
882 COUNT is the number of buffers to unmark.
883 Move point vertically down COUNT lines."
884 (interactive "p")
885 (bs--mark-unmark count
886 (lambda (buf)
887 (setq bs--marked-buffers (delq buf bs--marked-buffers)))))
889 (defun bs-unmark-previous (count)
890 "Unmark previous COUNT buffers.
891 Move point vertically up COUNT lines.
892 When called interactively a numeric prefix argument sets COUNT."
893 (interactive "p")
894 (forward-line (- count))
895 (save-excursion (bs-unmark-current count)))
897 (defun bs-unmark-all ()
898 "Unmark all buffers."
899 (interactive)
900 (let ((marked (string-to-char bs-string-marked))
901 (current (string-to-char bs-string-current))
902 (marked-cur (string-to-char bs-string-current-marked))
903 (unmarked (string-to-char bs-string-show-normally))
904 (inhibit-read-only t))
905 (save-excursion
906 (goto-char (point-min))
907 (forward-line 2)
908 (while (not (eobp))
909 (if (eq (char-after) marked)
910 (subst-char-in-region (point) (1+ (point)) marked unmarked)
911 (when (eq (char-after) marked-cur)
912 (subst-char-in-region (point) (1+ (point)) marked-cur current)))
913 (forward-line 1))
914 (setq bs--marked-buffers nil))))
916 (defun bs--show-config-message (what)
917 "Show message indicating the new showing status WHAT.
918 WHAT is a value of nil, `never', or `always'."
919 (bs-message-without-log (cond ((null what)
920 "Buffer will be shown normally.")
921 ((eq what 'never)
922 "Mark buffer to never be shown.")
923 (t "Mark buffer to show always."))))
925 (defun bs-delete ()
926 "Kill buffer on current line."
927 (interactive)
928 (let ((current (bs--current-buffer))
929 (inhibit-read-only t))
930 (unless (kill-buffer current)
931 (error "Buffer was not deleted"))
932 (setq bs-current-list (delq current bs-current-list))
933 (beginning-of-line)
934 (delete-region (point) (save-excursion
935 (end-of-line)
936 (if (eobp) (point) (1+ (point)))))
937 (when (eobp)
938 (backward-delete-char 1)
939 (beginning-of-line)
940 (recenter -1))
941 (bs--set-window-height)))
943 (defun bs-delete-backward ()
944 "Like `bs-delete' but go to buffer in front of current."
945 (interactive)
946 (let ((on-last-line-p (save-excursion (end-of-line) (eobp))))
947 (bs-delete)
948 (unless on-last-line-p
949 (bs-up 1))))
951 (defun bs-show-sorted ()
952 "Show buffer list sorted by next sort aspect."
953 (interactive)
954 (setq bs--current-sort-function
955 (bs-next-config-aux (car bs--current-sort-function)
956 bs-sort-functions))
957 (bs--redisplay)
958 (bs--goto-current-buffer)
959 (bs-message-without-log "Sorted %s" (car bs--current-sort-function)))
961 (defun bs-apply-sort-faces (&optional sort-description)
962 "Set text properties for the sort described by SORT-DESCRIPTION.
963 SORT-DESCRIPTION is an element of `bs-sort-functions'.
964 Default is `bs--current-sort-function'."
965 (let ((sort-description (or sort-description
966 bs--current-sort-function)))
967 (save-excursion
968 (goto-char (point-min))
969 (when (and (nth 2 sort-description)
970 (search-forward-regexp (nth 2 sort-description) nil t))
971 (let ((inhibit-read-only t))
972 (put-text-property (match-beginning 0)
973 (match-end 0)
974 'face
975 (or (nth 3 sort-description)
976 'region)))))))
978 (defun bs-toggle-show-all ()
979 "Toggle show all buffers / show buffers with current configuration."
980 (interactive)
981 (setq bs--show-all (not bs--show-all))
982 (bs--redisplay)
983 (bs--goto-current-buffer)
984 (bs-message-without-log "%s" (bs--current-config-message)))
986 (defun bs-toggle-readonly ()
987 "Toggle read-only status for buffer on current line.
988 Uses function `read-only-mode'."
989 (interactive)
990 (with-current-buffer (bs--current-buffer)
991 (read-only-mode 'toggle))
992 (bs--update-current-line))
994 (defun bs-clear-modified ()
995 "Set modified flag for buffer on current line to nil."
996 (interactive)
997 (with-current-buffer (bs--current-buffer)
998 (set-buffer-modified-p nil))
999 (bs--update-current-line))
1001 (defun bs--nth-wrapper (count fun &rest args)
1002 "Call COUNT times function FUN with arguments ARGS."
1003 (dotimes (_i (or count 1))
1004 (apply fun args)))
1006 (defun bs-up (arg)
1007 "Move point vertically up ARG lines in Buffer Selection Menu."
1008 (interactive "p")
1009 (if (and arg (numberp arg) (< arg 0))
1010 (bs--nth-wrapper (- arg) 'bs--down)
1011 (bs--nth-wrapper arg 'bs--up)))
1013 (defun bs--up ()
1014 "Move point vertically up one line.
1015 If on top of buffer list go to last line."
1016 (if (> (count-lines 1 (point)) bs-header-lines-length)
1017 (forward-line -1)
1018 (goto-char (point-max))
1019 (beginning-of-line)
1020 (recenter -1)))
1022 (defun bs-down (arg)
1023 "Move point vertically down ARG lines in Buffer Selection Menu."
1024 (interactive "p")
1025 (if (and arg (numberp arg) (< arg 0))
1026 (bs--nth-wrapper (- arg) 'bs--up)
1027 (bs--nth-wrapper arg 'bs--down)))
1029 (defun bs--down ()
1030 "Move point vertically down one line.
1031 If at end of buffer list go to first line."
1032 (if (eq (line-end-position) (point-max))
1033 (progn
1034 (goto-char (point-min))
1035 (forward-line bs-header-lines-length))
1036 (forward-line 1)))
1038 (defun bs-visits-non-file (buffer)
1039 "Return whether BUFFER visits no file.
1040 A value of t means BUFFER belongs to no file.
1041 A value of nil means BUFFER belongs to a file."
1042 (not (buffer-file-name buffer)))
1044 (defun bs-sort-buffer-interns-are-last (_b1 b2)
1045 "Function for sorting internal buffers at the end of all buffers."
1046 (string-match-p "^\\*" (buffer-name b2)))
1048 ;; ----------------------------------------------------------------------
1049 ;; Configurations:
1050 ;; ----------------------------------------------------------------------
1052 (defun bs-config-clear ()
1053 "Reset all variables which specify a configuration.
1054 These variables are `bs-dont-show-regexp', `bs-must-show-regexp',
1055 `bs-dont-show-function', `bs-must-show-function' and
1056 `bs-buffer-sort-function'."
1057 (setq bs-dont-show-regexp nil
1058 bs-must-show-regexp nil
1059 bs-dont-show-function nil
1060 bs-must-show-function nil
1061 bs-buffer-sort-function nil))
1063 (defun bs-config--only-files ()
1064 "Define a configuration for showing only buffers visiting a file."
1065 (bs-config-clear)
1066 (setq ;; I want to see *-buffers at the end
1067 bs-buffer-sort-function 'bs-sort-buffer-interns-are-last
1068 ;; Don't show files who don't belong to a file
1069 bs-dont-show-function 'bs-visits-non-file))
1071 (defun bs-config--files-and-scratch ()
1072 "Define a configuration for showing buffer *scratch* and file buffers."
1073 (bs-config-clear)
1074 (setq ;; I want to see *-buffers at the end
1075 bs-buffer-sort-function 'bs-sort-buffer-interns-are-last
1076 ;; Don't show files who don't belong to a file
1077 bs-dont-show-function 'bs-visits-non-file
1078 ;; Show *scratch* buffer.
1079 bs-must-show-regexp "^\\*scratch\\*$"))
1081 (defun bs-config--all ()
1082 "Define a configuration for showing all buffers.
1083 Reset all according variables by `bs-config-clear'."
1084 (bs-config-clear))
1086 (defun bs-config--all-intern-last ()
1087 "Define a configuration for showing all buffers.
1088 Internal buffers appear at end of all buffers."
1089 (bs-config-clear)
1090 ;; I want to see *-buffers at the end
1091 (setq bs-buffer-sort-function 'bs-sort-buffer-interns-are-last))
1093 (defun bs-set-configuration (name)
1094 "Set configuration to the one saved under string NAME in `bs-configurations'.
1095 When called interactively ask user for a configuration and apply selected
1096 configuration."
1097 (interactive (list (completing-read "Use configuration: "
1098 bs-configurations
1100 t)))
1101 (let ((list (assoc name bs-configurations)))
1102 (if list
1103 (if (listp list)
1104 (setq bs-current-configuration name
1105 bs-must-show-regexp (nth 1 list)
1106 bs-must-show-function (nth 2 list)
1107 bs-dont-show-regexp (nth 3 list)
1108 bs-dont-show-function (nth 4 list)
1109 bs-buffer-sort-function (nth 5 list))
1110 ;; for backward compatibility
1111 (funcall (cdr list)))
1112 ;; else
1113 (ding)
1114 (bs-message-without-log "No bs-configuration named %S." name))))
1116 (defun bs-help ()
1117 "Help for `bs-show'."
1118 (interactive)
1119 (describe-function 'bs-mode))
1121 (defun bs-next-config-aux (start-name list)
1122 "Get the next assoc after START-NAME in list LIST.
1123 Will return the first if START-NAME is at end."
1124 (let ((assocs list)
1125 (length (length list))
1126 pos)
1127 (while (and assocs (not pos))
1128 (when (string= (car (car assocs)) start-name)
1129 (setq pos (- length (length assocs))))
1130 (setq assocs (cdr assocs)))
1131 (setq pos (1+ pos))
1132 (if (eq pos length)
1133 (car list)
1134 (nth pos list))))
1136 (defun bs-next-config (name)
1137 "Return next configuration with respect to configuration with name NAME."
1138 (bs-next-config-aux name bs-configurations))
1140 (defun bs-select-next-configuration (&optional start-name)
1141 "Apply next configuration START-NAME and refresh buffer list.
1142 If START-NAME is nil the current configuration `bs-current-configuration'
1143 will be used."
1144 (interactive)
1145 (let ((config (bs-next-config (or start-name bs-current-configuration))))
1146 (bs-set-configuration (car config))
1147 (setq bs-default-configuration bs-current-configuration)
1148 (bs--redisplay t)
1149 (bs--set-window-height)
1150 (bs-message-without-log "Selected configuration: %s" (car config))))
1152 (defun bs-show-in-buffer (list)
1153 "Display buffer list LIST in buffer *buffer-selection*.
1154 Select buffer *buffer-selection* and display buffers according to current
1155 configuration `bs-current-configuration'. Set window height, fontify buffer
1156 and move point to current buffer."
1157 (setq bs-current-list list)
1158 (switch-to-buffer (get-buffer-create "*buffer-selection*"))
1159 (bs-mode)
1160 (let* ((inhibit-read-only t)
1161 (map-fun (lambda (entry)
1162 (length (buffer-name entry))))
1163 (max-length-of-names (apply 'max
1164 (cons 0 (mapcar map-fun list))))
1165 (name-entry-length (min bs-maximal-buffer-name-column
1166 (max bs-minimal-buffer-name-column
1167 max-length-of-names))))
1168 (erase-buffer)
1169 (setq bs--name-entry-length name-entry-length)
1170 (bs--show-header)
1171 (dolist (buffer list)
1172 (bs--insert-one-entry buffer)
1173 (insert "\n"))
1174 (delete-char -1)
1175 (bs--set-window-height)
1176 (bs--goto-current-buffer)
1177 (font-lock-ensure)
1178 (bs-apply-sort-faces)
1179 (set-buffer-modified-p nil)))
1181 (defun bs-next-buffer (&optional buffer-list sorting-p)
1182 "Return next buffer and buffer list for buffer cycling in BUFFER-LIST.
1183 Ignore sorting when SORTING-P is nil.
1184 If BUFFER-LIST is nil the result of `bs-buffer-list' will be used as
1185 buffer list. The result is a cons of normally the second element of
1186 BUFFER-LIST and the buffer list used for buffer cycling."
1187 (let* ((bs--current-sort-function (if sorting-p
1188 bs--current-sort-function))
1189 (bs-buffer-list (or buffer-list (bs-buffer-list))))
1190 (cons (or (car (cdr bs-buffer-list))
1191 (car bs-buffer-list)
1192 (current-buffer))
1193 bs-buffer-list)))
1195 (defun bs-previous-buffer (&optional buffer-list sorting-p)
1196 "Return previous buffer and buffer list for buffer cycling in BUFFER-LIST.
1197 Ignore sorting when SORTING-P is nil.
1198 If BUFFER-LIST is nil the result of `bs-buffer-list' will be used as
1199 buffer list. The result is a cons of last element of BUFFER-LIST and the
1200 buffer list used for buffer cycling."
1201 (let* ((bs--current-sort-function (if sorting-p
1202 bs--current-sort-function))
1203 (bs-buffer-list (or buffer-list (bs-buffer-list))))
1204 (cons (or (car (last bs-buffer-list))
1205 (current-buffer))
1206 bs-buffer-list)))
1208 (defun bs-message-without-log (&rest args)
1209 "Like `message' but don't log it on the message log.
1210 All arguments ARGS are transferred to function `message'."
1211 (let ((message-log-max nil))
1212 (apply 'message args)))
1214 (defvar bs--cycle-list nil
1215 "Current buffer list used for cycling.")
1217 ;;;###autoload
1218 (defun bs-cycle-next ()
1219 "Select next buffer defined by buffer cycling.
1220 The buffers taking part in buffer cycling are defined
1221 by buffer configuration `bs-cycle-configuration-name'."
1222 (interactive)
1223 (let ((bs--buffer-coming-from (current-buffer))
1224 (bs-dont-show-regexp bs-dont-show-regexp)
1225 (bs-must-show-regexp bs-must-show-regexp)
1226 (bs-dont-show-function bs-dont-show-function)
1227 (bs-must-show-function bs-must-show-function)
1228 (bs--show-all nil))
1229 (bs-set-configuration (or bs-cycle-configuration-name bs-default-configuration))
1230 (let ((bs-buffer-sort-function nil)
1231 (bs--current-sort-function nil))
1232 (let* ((tupel (bs-next-buffer (if (or (eq last-command
1233 'bs-cycle-next)
1234 (eq last-command
1235 'bs-cycle-previous))
1236 bs--cycle-list)))
1237 (next (car tupel))
1238 (cycle-list (cdr tupel)))
1239 ;; We don't want the frame iconified if the only window in the frame
1240 ;; happens to be dedicated.
1241 (bury-buffer (current-buffer))
1242 (switch-to-buffer next nil t)
1243 (setq bs--cycle-list (append (cdr cycle-list)
1244 (list (car cycle-list))))
1245 (bs-message-without-log "Next buffers: %s"
1246 (or (cdr bs--cycle-list)
1247 "this buffer"))))))
1249 ;;;###autoload
1250 (defun bs-cycle-previous ()
1251 "Select previous buffer defined by buffer cycling.
1252 The buffers taking part in buffer cycling are defined
1253 by buffer configuration `bs-cycle-configuration-name'."
1254 (interactive)
1255 (let ((bs--buffer-coming-from (current-buffer))
1256 (bs-dont-show-regexp bs-dont-show-regexp)
1257 (bs-must-show-regexp bs-must-show-regexp)
1258 (bs-dont-show-function bs-dont-show-function)
1259 (bs-must-show-function bs-must-show-function)
1260 (bs--show-all nil))
1261 (bs-set-configuration (or bs-cycle-configuration-name bs-default-configuration))
1262 (let ((bs-buffer-sort-function nil)
1263 (bs--current-sort-function nil))
1264 (let* ((tupel (bs-previous-buffer (if (or (eq last-command
1265 'bs-cycle-next)
1266 (eq last-command
1267 'bs-cycle-previous))
1268 bs--cycle-list)))
1269 (prev-buffer (car tupel))
1270 (cycle-list (cdr tupel)))
1271 (switch-to-buffer prev-buffer nil t)
1272 (setq bs--cycle-list (append (last cycle-list)
1273 (reverse (cdr (reverse cycle-list)))))
1274 (bs-message-without-log "Previous buffers: %s"
1275 (or (reverse (cdr bs--cycle-list))
1276 "this buffer"))))))
1278 (defun bs--get-value (fun &optional args)
1279 "Apply function FUN with arguments ARGS.
1280 Return result of evaluation. Will return FUN if FUN is a number
1281 or a string."
1282 (cond ((numberp fun)
1283 fun)
1284 ((stringp fun)
1285 fun)
1286 (t (apply fun args))))
1288 (defun bs--get-marked-string (start-buffer _all-buffers)
1289 "Return a string which describes whether current buffer is marked.
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 The result string is one of `bs-string-current', `bs-string-current-marked',
1293 `bs-string-marked', `bs-string-show-normally', `bs-string-show-never', or
1294 `bs-string-show-always'."
1295 (cond ;; current buffer is the buffer we started buffer selection.
1296 ((eq (current-buffer) start-buffer)
1297 (if (memq (current-buffer) bs--marked-buffers)
1298 bs-string-current-marked ; buffer is marked
1299 bs-string-current))
1300 ;; current buffer is marked
1301 ((memq (current-buffer) bs--marked-buffers)
1302 bs-string-marked)
1303 ;; current buffer hasn't a special mark.
1304 ((null bs-buffer-show-mark)
1305 bs-string-show-normally)
1306 ;; current buffer has a mark not to show itself.
1307 ((eq bs-buffer-show-mark 'never)
1308 bs-string-show-never)
1309 ;; otherwise current buffer is marked to show always.
1311 bs-string-show-always)))
1313 (defun bs--get-modified-string (_start-buffer _all-buffers)
1314 "Return a string which describes whether current buffer is modified.
1315 START-BUFFER is the buffer where we started buffer selection.
1316 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
1317 (if (buffer-modified-p) "*" " "))
1319 (defun bs--get-readonly-string (_start-buffer _all-buffers)
1320 "Return a string which describes whether current buffer is read only.
1321 START-BUFFER is the buffer where we started buffer selection.
1322 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
1323 (if buffer-read-only "%" " "))
1325 (defun bs--get-size-string (_start-buffer _all-buffers)
1326 "Return a string which describes the size of current buffer.
1327 START-BUFFER is the buffer where we started buffer selection.
1328 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
1329 (int-to-string (buffer-size)))
1331 (defun bs--get-name (_start-buffer _all-buffers)
1332 "Return name of current buffer for Buffer Selection Menu.
1333 The name of current buffer gets additional text properties
1334 for mouse highlighting.
1335 START-BUFFER is the buffer where we started buffer selection.
1336 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
1337 (propertize (buffer-name)
1338 'help-echo "mouse-2: select this buffer, mouse-3: select in other frame"
1339 'mouse-face 'highlight))
1341 (defun bs--get-mode-name (start-buffer _all-buffers)
1342 "Return the name of mode of current buffer for Buffer Selection Menu.
1343 START-BUFFER is the buffer where we started buffer selection.
1344 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
1345 (format-mode-line mode-name nil nil start-buffer))
1347 (defun bs--get-file-name (_start-buffer _all-buffers)
1348 "Return string for column `File' in Buffer Selection Menu.
1349 This is the variable `buffer-file-name' of current buffer.
1350 If not visiting a file, `list-buffers-directory' is returned instead.
1351 START-BUFFER is the buffer where we started buffer selection.
1352 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
1353 (propertize (or buffer-file-name
1354 (bound-and-true-p list-buffers-directory)
1356 'mouse-face 'highlight
1357 'help-echo "mouse-2: select this buffer, mouse-3: select in other frame"))
1359 (defun bs--insert-one-entry (buffer)
1360 "Generate one entry for buffer BUFFER in Buffer Selection Menu.
1361 It goes over all columns described in `bs-attributes-list'
1362 and evaluates corresponding string. Inserts string in current buffer;
1363 normally *buffer-selection*."
1364 (let ((string "")
1365 (to-much 0)
1366 (apply-args (append (list bs--buffer-coming-from bs-current-list))))
1367 (with-current-buffer buffer
1368 (dolist (column bs-attributes-list)
1369 (let* ((min (bs--get-value (nth 1 column)))
1370 (new-string (bs--format-aux (bs--get-value (nth 4 column) ; fun
1371 apply-args)
1372 (nth 3 column) ; align
1373 (- min to-much)))
1374 (len (length new-string)))
1375 (setq string (concat string new-string))
1376 (when (> len min)
1377 (setq to-much (- len min))))))
1378 (insert string)))
1380 (defun bs--format-aux (string align len)
1381 "Pad STRING to length LEN with alignment ALIGN.
1382 ALIGN is one of the symbols `left', `middle', or `right'."
1383 (let* ((width (length string))
1384 (len (max len width)))
1385 (format (format "%%%s%ds" (if (eq align 'right) "" "-") len)
1386 (if (eq align 'middle)
1387 (concat (make-string (/ (- len width) 2) ?\s) string)
1388 string))))
1390 (defun bs--show-header ()
1391 "Insert header for Buffer Selection Menu in current buffer."
1392 (insert (bs--create-header-line #'identity)
1393 "\n"
1394 (bs--create-header-line (lambda (title)
1395 (make-string (length title) ?-)))
1396 "\n"))
1398 (defun bs--get-name-length ()
1399 "Return value of `bs--name-entry-length'."
1400 bs--name-entry-length)
1402 (defun bs--create-header-line (col)
1403 "Generate a line for the header.
1404 COL is called for each column in `bs-attributes-list' as a
1405 function of one argument, the string heading for the column."
1406 (mapconcat (lambda (column)
1407 (bs--format-aux (funcall col (bs--get-value (car column)))
1408 (nth 3 column) ; align
1409 (bs--get-value (nth 1 column))))
1410 bs-attributes-list
1411 ""))
1413 (defun bs--show-with-configuration (name &optional arg)
1414 "Display buffer list of configuration with name NAME.
1415 Set configuration NAME and determine window for Buffer Selection Menu.
1416 Unless current buffer is buffer *buffer-selection* we have to save
1417 the buffer we started Buffer Selection Menu and the current window
1418 configuration to restore buffer and window configuration after a
1419 selection. If there is already a window displaying *buffer-selection*
1420 select this window for Buffer Selection Menu. Otherwise open a new
1421 window.
1422 The optional argument ARG is the prefix argument when calling a function
1423 for buffer selection."
1424 (bs-set-configuration name)
1425 (let ((bs--show-all (or bs--show-all arg)))
1426 (unless (string= "*buffer-selection*" (buffer-name))
1427 ;; Only when not in buffer *buffer-selection*
1428 ;; we have to set the buffer we started the command
1429 (setq bs--buffer-coming-from (current-buffer)))
1430 (let ((liste (bs-buffer-list))
1431 (active-window (get-window-with-predicate
1432 (lambda (w)
1433 (string= (buffer-name (window-buffer w))
1434 "*buffer-selection*"))
1435 nil (selected-frame))))
1436 (if active-window
1437 (select-window active-window)
1438 (bs--restore-window-config)
1439 (setq bs--window-config-coming-from (current-window-configuration))
1440 (when (> (window-height) 7)
1441 ;; Errors would mess with the window configuration (bug#10882).
1442 (ignore-errors (select-window (split-window-below)))))
1443 (bs-show-in-buffer liste)
1444 (bs-message-without-log "%s" (bs--current-config-message)))))
1446 (defun bs--configuration-name-for-prefix-arg (prefix)
1447 "Convert prefix argument PREFIX to a name of a buffer configuration.
1448 If PREFIX is nil return `bs-default-configuration'.
1449 If PREFIX is an integer return PREFIX element of `bs-configurations'.
1450 Otherwise return `bs-alternative-configuration'."
1451 (cond ;; usually activation
1452 ((null prefix)
1453 bs-default-configuration)
1454 ;; call with integer as prefix argument
1455 ((integerp prefix)
1456 (if (and (< 0 prefix) (<= prefix (length bs-configurations)))
1457 (car (nth (1- prefix) bs-configurations))
1458 bs-default-configuration))
1459 ;; call by prefix argument C-u
1460 (t bs-alternative-configuration)))
1462 ;; ----------------------------------------------------------------------
1463 ;; Main function bs-customize and bs-show
1464 ;; ----------------------------------------------------------------------
1466 ;;;###autoload
1467 (defun bs-customize ()
1468 "Customization of group bs for Buffer Selection Menu."
1469 (interactive)
1470 (customize-group "bs"))
1472 ;;;###autoload
1473 (defun bs-show (arg)
1474 "Make a menu of buffers so you can manipulate buffers or the buffer list.
1475 \\<bs-mode-map>
1476 There are many key commands similar to `Buffer-menu-mode' for
1477 manipulating the buffer list and the buffers themselves.
1478 User can move with [up] or [down], select a buffer
1479 by \\[bs-select] or [SPC]\n
1480 Type \\[bs-kill] to leave Buffer Selection Menu without a selection.
1481 Type \\[bs-help] after invocation to get help on commands available.
1482 With prefix argument ARG show a different buffer list. Function
1483 `bs--configuration-name-for-prefix-arg' determine accordingly
1484 name of buffer configuration."
1485 (interactive "P")
1486 (setq bs--marked-buffers nil)
1487 (bs--show-with-configuration (bs--configuration-name-for-prefix-arg arg)))
1489 ;; ----------------------------------------------------------------------
1490 ;; Cleanup
1491 ;; ----------------------------------------------------------------------
1493 (defun bs-unload-function ()
1494 "Unload the Buffer Selection library."
1495 (let ((bs-buf (get-buffer "*buffer-selection*")))
1496 (when bs-buf
1497 (with-current-buffer bs-buf
1498 (when (eq major-mode 'bs-mode)
1499 (bs-kill)
1500 (kill-buffer bs-buf)))))
1501 ;; continue standard unloading
1502 nil)
1504 ;; Now provide feature bs
1505 (provide 'bs)
1507 ;;; bs.el ends here