Commit | Line | Data |
---|---|---|
2fbc1934 | 1 | ;;; bs.el --- menu for selecting and displaying buffers -*- lexical-binding: t -*- |
6448a6b3 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1998-2014 Free Software Foundation, Inc. |
6448a6b3 GM |
4 | ;; Author: Olaf Sylvester <Olaf.Sylvester@netsurf.de> |
5 | ;; Maintainer: Olaf Sylvester <Olaf.Sylvester@netsurf.de> | |
6 | ;; Keywords: convenience | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
6448a6b3 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
6448a6b3 GM |
14 | |
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. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
6448a6b3 GM |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; Version: 1.17 | |
91b69101 | 26 | ;; X-URL: http://www.geekware.de/software/emacs |
6448a6b3 | 27 | ;; |
0b381c7e | 28 | ;; The bs-package contains a main function bs-show for popping up a |
6448a6b3 GM |
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. | |
32 | ;; | |
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 | ;; ----------------------------------------------------------------------- | |
42 | ||
135377f1 | 43 | ;;; Quick Installation and Customization: |
6448a6b3 | 44 | |
aca6915a | 45 | ;; To display the bs menu, do |
6448a6b3 | 46 | ;; M-x bs-show |
aca6915a CY |
47 | ;; To customize its behavior, do |
48 | ;; M-x bs-customize | |
6448a6b3 GM |
49 | |
50 | ;;; More Commentary: | |
51 | ||
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. | |
58 | ;; | |
59 | ;; The package bs combines the advantages of the Emacs functions | |
60 | ;; `list-buffers' and `electric-buffer-list'. | |
61 | ;; | |
880ed9f1 | 62 | ;; Additional features for Buffer Selection Menu: |
6448a6b3 GM |
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. | |
70 | ||
71 | ;;; Cycling through buffers | |
72 | ||
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) | |
78 | ;; | |
79 | ;; Both functions use a special subset of all buffers for cycling to avoid | |
80 | ;; to go through internal buffers like *Messages*. | |
81 | ;; | |
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. | |
85 | ||
86 | ;;; Customization: | |
87 | ||
88 | ;; There is a customization group called `bs' in group `convenience'. | |
89 | ;; Start customization by M-x bs-customize | |
90 | ;; | |
91 | ;; Buffer list | |
92 | ;; ----------- | |
93 | ;; You can define your own configurations by extending variable | |
94 | ;; `bs-configurations' (see docstring for details). | |
95 | ;; | |
96 | ;; `bs-default-configuration' contains the name of default configuration. | |
97 | ;; The default value is "files" which means to show only files. | |
98 | ;; | |
99 | ;; If you always want to see all buffers, customize variable | |
100 | ;; `bs-default-configuration' in customization group `bs'. | |
101 | ;; | |
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'. | |
108 | ;; | |
109 | ;; There are four basic functions for sorting: | |
110 | ;; by buffer name, by mode, by size, or by filename | |
111 | ;; | |
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. | |
118 | ;; | |
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*. | |
122 | ||
123 | ;;; History: | |
124 | ||
125 | ;;; Code: | |
126 | ||
127 | ;; ---------------------------------------------------------------------- | |
128 | ;; Globals for customization | |
129 | ;; ---------------------------------------------------------------------- | |
130 | ||
131 | (defgroup bs nil | |
132 | "Buffer Selection: Maintaining buffers by buffer menu." | |
00a9f6a5 | 133 | :version "21.1" |
dc7904f5 | 134 | :link '(emacs-commentary-link "bs") |
91b69101 | 135 | :link '(url-link "http://www.geekware.de/software/emacs") |
6448a6b3 GM |
136 | :group 'convenience) |
137 | ||
880ed9f1 GM |
138 | (defgroup bs-appearance nil |
139 | "Buffer Selection appearance: Appearance of bs buffer menu." | |
6448a6b3 GM |
140 | :group 'bs) |
141 | ||
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 " ")) | |
e749f576 | 154 | "List specifying the layout of a Buffer Selection Menu buffer. |
6448a6b3 | 155 | Each entry specifies a column and is a list of the form of: |
c0cb1027 | 156 | \(HEADER MINIMUM-LENGTH MAXIMUM-LENGTH ALIGNMENT FUN-OR-STRING) |
91b69101 JB |
157 | |
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. | |
167 | ||
880ed9f1 | 168 | The function gets as parameter the buffer where we have started |
6448a6b3 | 169 | buffer selection and the list of all buffers to show. The function must |
880ed9f1 GM |
170 | return a string representing the column's value." |
171 | :group 'bs-appearance | |
6448a6b3 GM |
172 | :type '(repeat sexp)) |
173 | ||
6448a6b3 GM |
174 | (defun bs--make-header-match-string () |
175 | "Return a regexp matching the first line of a Buffer Selection Menu buffer." | |
e749f576 | 176 | (concat "^\\(" (mapconcat #'car bs-attributes-list " *") " *$\\)")) |
6448a6b3 | 177 | |
d8754ce5 | 178 | ;; Font-Lock-Settings |
6448a6b3 | 179 | (defvar bs-mode-font-lock-keywords |
880ed9f1 | 180 | (list ;; header in font-lock-type-face |
dc7904f5 DL |
181 | (list (bs--make-header-match-string) |
182 | '(1 font-lock-type-face append) '(1 'bold append)) | |
183 | ;; Buffername embedded by * | |
184 | (list "^\\(.*\\*.*\\*.*\\)$" | |
a5b09d5a DL |
185 | 1 |
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)) | |
dc7904f5 | 190 | ;; Dired-Buffers |
29115ca9 | 191 | '("^..\\(.*Dired .*\\)$" 1 font-lock-function-name-face) |
dc7904f5 DL |
192 | ;; the star for modified buffers |
193 | '("^.\\(\\*\\) +[^\\*]" 1 font-lock-comment-face)) | |
6448a6b3 GM |
194 | "Default font lock expressions for Buffer Selection Menu.") |
195 | ||
196 | (defcustom bs-max-window-height 20 | |
e749f576 | 197 | "Maximal window height of Buffer Selection Menu." |
880ed9f1 | 198 | :group 'bs-appearance |
6448a6b3 GM |
199 | :type 'integer) |
200 | ||
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.") | |
205 | ||
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.") | |
213 | ||
214 | (defcustom bs-must-always-show-regexp nil | |
e749f576 | 215 | "Regular expression for specifying buffers to show always. |
6448a6b3 GM |
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)) | |
220 | ||
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.") | |
226 | ||
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.") | |
230 | ||
231 | (defvar bs-buffer-sort-function nil | |
232 | "Sort function to sort the buffers that appear in Buffer Selection Menu. | |
b75ccc22 JB |
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.") | |
6448a6b3 GM |
235 | |
236 | (defcustom bs-maximal-buffer-name-column 45 | |
e749f576 | 237 | "Maximum column width for buffer names. |
6448a6b3 GM |
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'." | |
880ed9f1 | 242 | :group 'bs-appearance |
6448a6b3 GM |
243 | :type 'integer) |
244 | ||
245 | (defcustom bs-minimal-buffer-name-column 15 | |
e749f576 | 246 | "Minimum column width for buffer names. |
6448a6b3 GM |
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'." | |
880ed9f1 | 251 | :group 'bs-appearance |
6448a6b3 GM |
252 | :type 'integer) |
253 | ||
254 | (defconst bs-header-lines-length 2 | |
255 | "Number of lines for headers in Buffer Selection Menu.") | |
256 | ||
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) | |
880ed9f1 | 260 | ("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file |
6448a6b3 GM |
261 | bs-sort-buffer-interns-are-last) |
262 | ("all-intern-last" nil nil nil nil bs-sort-buffer-interns-are-last)) | |
e749f576 | 263 | "List of all configurations you can use in the Buffer Selection Menu. |
6448a6b3 | 264 | A configuration describes which buffers appear in Buffer Selection Menu |
880ed9f1 | 265 | and also the order of buffers. A configuration is a list with |
6448a6b3 GM |
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 | |
880ed9f1 GM |
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'. | |
6448a6b3 | 270 | By setting these variables you define a configuration." |
880ed9f1 | 271 | :group 'bs-appearance |
6448a6b3 GM |
272 | :type '(repeat sexp)) |
273 | ||
274 | (defcustom bs-default-configuration "files" | |
e749f576 | 275 | "Name of default configuration used by the Buffer Selection Menu. |
6448a6b3 GM |
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) | |
281 | ||
282 | (defcustom bs-alternative-configuration "all" | |
e749f576 | 283 | "Name of configuration used when calling `bs-show' with \ |
6448a6b3 GM |
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) | |
288 | ||
289 | (defvar bs-current-configuration bs-default-configuration | |
290 | "Name of current configuration. | |
880ed9f1 | 291 | Must be a string used in `bs-configurations' for naming a configuration.") |
6448a6b3 GM |
292 | |
293 | (defcustom bs-cycle-configuration-name nil | |
e749f576 | 294 | "Name of configuration used when cycling through the buffer list. |
6448a6b3 GM |
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)) | |
300 | ||
301 | (defcustom bs-string-show-always "+" | |
e749f576 | 302 | "String added in column 1 indicating a buffer will always be shown." |
880ed9f1 | 303 | :group 'bs-appearance |
6448a6b3 GM |
304 | :type 'string) |
305 | ||
306 | (defcustom bs-string-show-never "-" | |
e749f576 | 307 | "String added in column 1 indicating a buffer will never be shown." |
880ed9f1 | 308 | :group 'bs-appearance |
6448a6b3 GM |
309 | :type 'string) |
310 | ||
311 | (defcustom bs-string-current "." | |
e749f576 | 312 | "String added in column 1 indicating the current buffer." |
880ed9f1 | 313 | :group 'bs-appearance |
6448a6b3 GM |
314 | :type 'string) |
315 | ||
316 | (defcustom bs-string-current-marked "#" | |
e749f576 | 317 | "String added in column 1 indicating the current buffer when it is marked." |
880ed9f1 | 318 | :group 'bs-appearance |
6448a6b3 GM |
319 | :type 'string) |
320 | ||
321 | (defcustom bs-string-marked ">" | |
e749f576 | 322 | "String added in column 1 indicating a marked buffer." |
880ed9f1 | 323 | :group 'bs-appearance |
6448a6b3 GM |
324 | :type 'string) |
325 | ||
326 | (defcustom bs-string-show-normally " " | |
e749f576 | 327 | "String added in column 1 indicating an unmarked buffer." |
880ed9f1 | 328 | :group 'bs-appearance |
6448a6b3 GM |
329 | :type 'string) |
330 | ||
331 | (defvar bs--name-entry-length 20 | |
332 | "Maximum length of all displayed buffer names. | |
333 | Used internally, only.") | |
334 | ||
335 | ;; ---------------------------------------------------------------------- | |
880ed9f1 | 336 | ;; Internal globals |
6448a6b3 GM |
337 | ;; ---------------------------------------------------------------------- |
338 | ||
388573ee | 339 | (defvar-local bs-buffer-show-mark nil |
6448a6b3 | 340 | "Flag for the current mode for showing this buffer. |
880ed9f1 GM |
341 | A value of nil means buffer will be shown depending on the current |
342 | configuration. | |
6448a6b3 GM |
343 | A value of `never' means to never show the buffer. |
344 | A value of `always' means to show buffer regardless of the configuration.") | |
345 | ||
6448a6b3 GM |
346 | ;; Make face named region (for XEmacs) |
347 | (unless (facep 'region) | |
348 | (make-face 'region) | |
349 | (set-face-background 'region "gray75")) | |
350 | ||
6448a6b3 GM |
351 | (defun bs--sort-by-name (b1 b2) |
352 | "Compare buffers B1 and B2 by buffer name." | |
353 | (string< (buffer-name b1) | |
dc7904f5 | 354 | (buffer-name b2))) |
6448a6b3 GM |
355 | |
356 | (defun bs--sort-by-filename (b1 b2) | |
357 | "Compare buffers B1 and B2 by file name." | |
358 | (string< (or (buffer-file-name b1) "") | |
dc7904f5 | 359 | (or (buffer-file-name b2) ""))) |
6448a6b3 GM |
360 | |
361 | (defun bs--sort-by-mode (b1 b2) | |
362 | "Compare buffers B1 and B2 by mode name." | |
48d33090 SM |
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))))) | |
6448a6b3 GM |
366 | |
367 | (defun bs--sort-by-size (b1 b2) | |
368 | "Compare buffers B1 and B2 by buffer size." | |
70ef8857 | 369 | (< (buffer-size b1) (buffer-size b2))) |
6448a6b3 GM |
370 | |
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)) | |
e749f576 | 377 | "List of all possible sorting aspects for Buffer Selection Menu. |
6448a6b3 | 378 | You can add a new entry with a call to `bs-define-sort-function'. |
b75ccc22 | 379 | Each element is a list of four elements (NAME FUNCTION REGEXP-FOR-SORTING FACE). |
6448a6b3 | 380 | NAME specifies the sort order defined by function FUNCTION. |
b75ccc22 | 381 | FUNCTION nil means don't sort the buffer list. Otherwise the function |
6448a6b3 GM |
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)) | |
389 | ||
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 | |
dc7904f5 | 402 | (setcdr tupel (list fun regexp-for-sorting face)) |
6448a6b3 | 403 | (setq bs-sort-functions |
dc7904f5 DL |
404 | (cons (list name fun regexp-for-sorting face) |
405 | bs-sort-functions))))) | |
6448a6b3 GM |
406 | |
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'.") | |
410 | ||
411 | (defcustom bs-default-sort-name "by nothing" | |
e749f576 | 412 | "Name of default sort behavior. |
6448a6b3 GM |
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) | |
dc7904f5 DL |
418 | (set var-name value) |
419 | (setq bs--current-sort-function | |
420 | (assoc value bs-sort-functions)))) | |
6448a6b3 GM |
421 | |
422 | (defvar bs--buffer-coming-from nil | |
423 | "The buffer in which the user started the current Buffer Selection Menu.") | |
424 | ||
425 | (defvar bs--show-all nil | |
426 | "Flag whether showing all buffers regardless of current configuration. | |
aaf08d6b | 427 | Non-nil means to show all buffers. Otherwise show buffers |
6448a6b3 GM |
428 | defined by current configuration `bs-current-configuration'.") |
429 | ||
430 | (defvar bs--window-config-coming-from nil | |
431 | "Window configuration before starting Buffer Selection Menu.") | |
432 | ||
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.") | |
437 | ||
438 | (defvar bs-current-list nil | |
439 | "List of buffers shown in Buffer Selection Menu. | |
440 | Used internally, only.") | |
441 | ||
442 | (defvar bs--marked-buffers nil | |
443 | "Currently marked buffers in Buffer Selection Menu.") | |
444 | ||
e6e76838 AS |
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) | |
6448a6b3 GM |
498 | "Keymap of `bs-mode'.") |
499 | ||
6448a6b3 GM |
500 | ;; ---------------------------------------------------------------------- |
501 | ;; Functions | |
502 | ;; ---------------------------------------------------------------------- | |
503 | ||
504 | (defun bs-buffer-list (&optional list sort-description) | |
505 | "Return a list of buffers to be shown. | |
880ed9f1 | 506 | LIST is a list of buffers to test for appearance in Buffer Selection Menu. |
6448a6b3 GM |
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) | |
dc7904f5 | 513 | list (or list (buffer-list))) |
6448a6b3 | 514 | (let ((result nil)) |
e749f576 JB |
515 | (dolist (buf list) |
516 | (let* ((buffername (buffer-name buf)) | |
517 | (int-show-never (string-match-p bs--intern-show-never buffername)) | |
dc7904f5 | 518 | (ext-show-never (and bs-dont-show-regexp |
e749f576 JB |
519 | (string-match-p bs-dont-show-regexp |
520 | buffername))) | |
dc7904f5 | 521 | (extern-must-show (or (and bs-must-always-show-regexp |
e749f576 | 522 | (string-match-p |
dc7904f5 DL |
523 | bs-must-always-show-regexp |
524 | buffername)) | |
525 | (and bs-must-show-regexp | |
e749f576 JB |
526 | (string-match-p bs-must-show-regexp |
527 | buffername)))) | |
dc7904f5 DL |
528 | (extern-show-never-from-fun (and bs-dont-show-function |
529 | (funcall bs-dont-show-function | |
e749f576 | 530 | buf))) |
dc7904f5 DL |
531 | (extern-must-show-from-fun (and bs-must-show-function |
532 | (funcall bs-must-show-function | |
e749f576 JB |
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))))) | |
6448a6b3 GM |
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. | |
e749f576 JB |
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))) | |
6448a6b3 GM |
552 | ;; sorting |
553 | (if (and sort-description | |
dc7904f5 DL |
554 | (nth 1 sort-description)) |
555 | (setq result (sort result (nth 1 sort-description))) | |
6448a6b3 GM |
556 | ;; else standard sorting |
557 | (bs-buffer-sort result)))) | |
558 | ||
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)) | |
564 | ||
565 | (defun bs--redisplay (&optional keep-line-p sort-description) | |
566 | "Redisplay whole Buffer Selection Menu. | |
aaf08d6b | 567 | If KEEP-LINE-P is non-nil the point will stay on current line. |
ce3ba12c | 568 | SORT-DESCRIPTION is an element of `bs-sort-functions'." |
e6ce8c42 | 569 | (let ((line (count-lines 1 (point)))) |
6448a6b3 | 570 | (bs-show-in-buffer (bs-buffer-list nil sort-description)) |
e749f576 | 571 | (when keep-line-p |
e6ce8c42 GM |
572 | (goto-char (point-min)) |
573 | (forward-line line)) | |
6448a6b3 GM |
574 | (beginning-of-line))) |
575 | ||
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'." | |
dc7904f5 DL |
580 | (let ((regexp (concat "^" |
581 | (regexp-quote bs-string-current) | |
582 | "\\|^" | |
583 | (regexp-quote bs-string-current-marked))) | |
584 | point) | |
6448a6b3 GM |
585 | (save-excursion |
586 | (goto-char (point-min)) | |
e749f576 JB |
587 | (when (search-forward-regexp regexp nil t) |
588 | (setq point (1- (point))))) | |
589 | (when point | |
590 | (goto-char point)))) | |
6448a6b3 GM |
591 | |
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" | |
dc7904f5 | 597 | bs-current-configuration))) |
6448a6b3 | 598 | |
e749f576 JB |
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 | |
e749f576 JB |
605 | (bs--set-window-height))))) |
606 | ||
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)) | |
613 | ||
5c742460 JB |
614 | (put 'bs-mode 'mode-class 'special) |
615 | ||
e749f576 | 616 | (define-derived-mode bs-mode nil "Buffer-Selection-Menu" |
44e97401 | 617 | "Major mode for editing a subset of Emacs's buffers. |
6448a6b3 GM |
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 | |
462db93c | 621 | buffer by \\[bs-select] or SPC. Abort buffer list with \\[bs-kill]. |
6448a6b3 GM |
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. | |
625 | ||
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. | |
e148f726 TTN |
632 | \\[bs-save] -- save current line's buffer immediately. |
633 | \\[bs-delete] -- kill current line's buffer immediately. | |
6448a6b3 GM |
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. | |
880ed9f1 | 644 | \\[bs-toggle-current-to-show] -- toggle status of appearance. |
6448a6b3 GM |
645 | \\[bs-set-current-buffer-to-show-always] -- mark current line's buffer \ |
646 | to show always. | |
880ed9f1 | 647 | \\[bs-visit-tags-table] -- call `visit-tags-table' on current line's buffer. |
6448a6b3 | 648 | \\[bs-help] -- display this help text." |
310a26be | 649 | (buffer-disable-undo) |
e749f576 | 650 | (setq buffer-read-only t |
dc7904f5 | 651 | truncate-lines t |
388573ee JB |
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) | |
e749f576 JB |
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)) | |
6448a6b3 | 660 | |
ce3ba12c JB |
661 | (defun bs--restore-window-config () |
662 | "Restore window configuration on the current frame." | |
663 | (when bs--window-config-coming-from | |
e749f576 JB |
664 | (let ((frame (selected-frame))) |
665 | (unwind-protect | |
666 | (set-window-configuration bs--window-config-coming-from) | |
667 | (select-frame frame))) | |
ce3ba12c JB |
668 | (setq bs--window-config-coming-from nil))) |
669 | ||
6448a6b3 | 670 | (defun bs-kill () |
6af00b67 | 671 | "Let buffer disappear and reset window configuration." |
6448a6b3 GM |
672 | (interactive) |
673 | (bury-buffer (current-buffer)) | |
ce3ba12c | 674 | (bs--restore-window-config)) |
6448a6b3 GM |
675 | |
676 | (defun bs-abort () | |
677 | "Ding and leave Buffer Selection Menu without a selection." | |
dc7904f5 | 678 | (interactive) |
6448a6b3 GM |
679 | (ding) |
680 | (bs-kill)) | |
681 | ||
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)) | |
688 | ||
2fbc1934 | 689 | (defun bs-refresh (&rest _ignored) |
fa72d075 JB |
690 | "Refresh whole Buffer Selection Menu. |
691 | Arguments are IGNORED (for `revert-buffer')." | |
6448a6b3 GM |
692 | (interactive) |
693 | (bs--redisplay t)) | |
694 | ||
6448a6b3 GM |
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) | |
ca6b8248 | 698 | (fit-window-to-buffer (selected-window) bs-max-window-height))) |
6448a6b3 GM |
699 | |
700 | (defun bs--current-buffer () | |
701 | "Return buffer on current line. | |
880ed9f1 | 702 | Raise an error if not on a buffer line." |
6448a6b3 GM |
703 | (beginning-of-line) |
704 | (let ((line (+ (- bs-header-lines-length) | |
dc7904f5 | 705 | (count-lines 1 (point))))) |
e749f576 JB |
706 | (when (< line 0) |
707 | (error "You are on a header row")) | |
6448a6b3 GM |
708 | (nth line bs-current-list))) |
709 | ||
710 | (defun bs--update-current-line () | |
711 | "Update the entry on current line for Buffer Selection Menu." | |
712 | (let ((buffer (bs--current-buffer)) | |
dc7904f5 | 713 | (inhibit-read-only t)) |
6448a6b3 GM |
714 | (beginning-of-line) |
715 | (delete-region (point) (line-end-position)) | |
716 | (bs--insert-one-entry buffer) | |
717 | (beginning-of-line))) | |
718 | ||
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))) | |
724 | ||
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 | |
de302d45 | 728 | Buffer Selection Menu will be restored. |
6448a6b3 GM |
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)) | |
ce3ba12c | 735 | (bs--restore-window-config) |
6448a6b3 | 736 | (switch-to-buffer buffer) |
e749f576 JB |
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))))) | |
6448a6b3 GM |
749 | |
750 | (defun bs-select-other-window () | |
751 | "Select current line's buffer by `switch-to-buffer-other-window'. | |
de302d45 | 752 | The window configuration before starting Buffer Selection Menu will be restored |
6448a6b3 GM |
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)) | |
ce3ba12c | 758 | (bs--restore-window-config) |
6448a6b3 GM |
759 | (switch-to-buffer-other-window buffer))) |
760 | ||
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))) | |
767 | ||
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)) | |
ce3ba12c | 774 | (bs--restore-window-config) |
6448a6b3 GM |
775 | (switch-to-buffer-other-frame buffer))) |
776 | ||
777 | (defun bs-mouse-select-other-frame (event) | |
778 | "Select selected line's buffer in new created frame. | |
779 | Leave Buffer Selection Menu. | |
b75ccc22 | 780 | EVENT: a mouse click event." |
6448a6b3 GM |
781 | (interactive "e") |
782 | (mouse-set-point event) | |
783 | (bs-select-other-frame)) | |
784 | ||
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)) | |
791 | ||
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)) | |
798 | ||
799 | (defun bs-bury-buffer () | |
800 | "Bury buffer on current line." | |
801 | (interactive) | |
802 | (bury-buffer (bs--current-buffer)) | |
803 | (bs--redisplay t)) | |
804 | ||
805 | (defun bs-save () | |
806 | "Save buffer on current line." | |
807 | (interactive) | |
70ef8857 | 808 | (with-current-buffer (bs--current-buffer) |
3af36712 SM |
809 | (save-buffer)) |
810 | (bs--update-current-line)) | |
6448a6b3 GM |
811 | |
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 | |
dc7904f5 | 818 | (visit-tags-table file) |
6448a6b3 GM |
819 | (error "Specified buffer has no file")))) |
820 | ||
821 | (defun bs-toggle-current-to-show () | |
822 | "Toggle status of showing flag for buffer in current line." | |
823 | (interactive) | |
70ef8857 SM |
824 | (let ((res |
825 | (with-current-buffer (bs--current-buffer) | |
f58e0fd5 SM |
826 | (setq bs-buffer-show-mark (pcase bs-buffer-show-mark |
827 | (`nil 'never) | |
828 | (`never 'always) | |
829 | (_ nil)))))) | |
6448a6b3 GM |
830 | (bs--update-current-line) |
831 | (bs--set-window-height) | |
832 | (bs--show-config-message res))) | |
833 | ||
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))) | |
843 | ||
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)) | |
848 | ||
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." | |
e64dbd8b | 853 | (with-current-buffer buffer (setq bs-buffer-show-mark what)) |
6448a6b3 GM |
854 | (bs--update-current-line) |
855 | (bs--set-window-height) | |
856 | (bs--show-config-message what)) | |
857 | ||
3d5aef76 JB |
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))) | |
b3e945d3 | 861 | (dotimes (_i (abs count)) |
3d5aef76 JB |
862 | (let ((buffer (bs--current-buffer))) |
863 | (when buffer (funcall fun buffer)) | |
864 | (bs--update-current-line) | |
865 | (bs-down dir))))) | |
866 | ||
6448a6b3 GM |
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") | |
3d5aef76 JB |
872 | (bs--mark-unmark count |
873 | (lambda (buf) | |
874 | (add-to-list 'bs--marked-buffers buf)))) | |
6448a6b3 GM |
875 | |
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") | |
3d5aef76 JB |
881 | (bs--mark-unmark count |
882 | (lambda (buf) | |
883 | (setq bs--marked-buffers (delq buf bs--marked-buffers))))) | |
6448a6b3 GM |
884 | |
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) | |
dc7904f5 DL |
889 | "Buffer will be shown normally.") |
890 | ((eq what 'never) | |
891 | "Mark buffer to never be shown.") | |
892 | (t "Mark buffer to show always.")))) | |
6448a6b3 GM |
893 | |
894 | (defun bs-delete () | |
895 | "Kill buffer on current line." | |
896 | (interactive) | |
897 | (let ((current (bs--current-buffer)) | |
dc7904f5 | 898 | (inhibit-read-only t)) |
706e6a52 EZ |
899 | (unless (kill-buffer current) |
900 | (error "Buffer was not deleted")) | |
6448a6b3 | 901 | (setq bs-current-list (delq current bs-current-list)) |
6448a6b3 GM |
902 | (beginning-of-line) |
903 | (delete-region (point) (save-excursion | |
dc7904f5 DL |
904 | (end-of-line) |
905 | (if (eobp) (point) (1+ (point))))) | |
e749f576 JB |
906 | (when (eobp) |
907 | (backward-delete-char 1) | |
908 | (beginning-of-line) | |
909 | (recenter -1)) | |
6448a6b3 GM |
910 | (bs--set-window-height))) |
911 | ||
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 | |
dc7904f5 | 918 | (bs-up 1)))) |
6448a6b3 GM |
919 | |
920 | (defun bs-show-sorted () | |
5a25a895 | 921 | "Show buffer list sorted by next sort aspect." |
6448a6b3 GM |
922 | (interactive) |
923 | (setq bs--current-sort-function | |
dc7904f5 DL |
924 | (bs-next-config-aux (car bs--current-sort-function) |
925 | bs-sort-functions)) | |
6448a6b3 GM |
926 | (bs--redisplay) |
927 | (bs--goto-current-buffer) | |
928 | (bs-message-without-log "Sorted %s" (car bs--current-sort-function))) | |
929 | ||
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 | |
dc7904f5 | 935 | bs--current-sort-function))) |
6448a6b3 GM |
936 | (save-excursion |
937 | (goto-char (point-min)) | |
e749f576 JB |
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))))))) | |
6448a6b3 GM |
946 | |
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))) | |
954 | ||
955 | (defun bs-toggle-readonly () | |
956 | "Toggle read-only status for buffer on current line. | |
70ef8857 | 957 | Uses function `toggle-read-only'." |
6448a6b3 | 958 | (interactive) |
70ef8857 | 959 | (with-current-buffer (bs--current-buffer) |
9a930676 | 960 | (read-only-mode 'toggle)) |
70ef8857 | 961 | (bs--update-current-line)) |
6448a6b3 GM |
962 | |
963 | (defun bs-clear-modified () | |
964 | "Set modified flag for buffer on current line to nil." | |
965 | (interactive) | |
70ef8857 SM |
966 | (with-current-buffer (bs--current-buffer) |
967 | (set-buffer-modified-p nil)) | |
6448a6b3 GM |
968 | (bs--update-current-line)) |
969 | ||
970 | (defun bs--nth-wrapper (count fun &rest args) | |
971 | "Call COUNT times function FUN with arguments ARGS." | |
b3e945d3 | 972 | (dotimes (_i (or count 1)) |
e749f576 | 973 | (apply fun args))) |
6448a6b3 GM |
974 | |
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))) | |
981 | ||
982 | (defun bs--up () | |
983 | "Move cursor vertically up one line. | |
984 | If on top of buffer list go to last line." | |
ca6b8248 JB |
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))) | |
6448a6b3 GM |
990 | |
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))) | |
997 | ||
998 | (defun bs--down () | |
999 | "Move cursor vertically down one line. | |
1000 | If at end of buffer list go to first line." | |
ca6b8248 | 1001 | (if (eq (line-end-position) (point-max)) |
936934f5 | 1002 | (progn |
e600eb79 | 1003 | (goto-char (point-min)) |
936934f5 | 1004 | (forward-line bs-header-lines-length)) |
ca6b8248 | 1005 | (forward-line 1))) |
6448a6b3 GM |
1006 | |
1007 | (defun bs-visits-non-file (buffer) | |
b75ccc22 | 1008 | "Return whether BUFFER visits no file. |
6448a6b3 GM |
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))) | |
1012 | ||
2fbc1934 | 1013 | (defun bs-sort-buffer-interns-are-last (_b1 b2) |
b75ccc22 | 1014 | "Function for sorting internal buffers at the end of all buffers." |
e749f576 | 1015 | (string-match-p "^\\*" (buffer-name b2))) |
6448a6b3 GM |
1016 | |
1017 | ;; ---------------------------------------------------------------------- | |
1018 | ;; Configurations: | |
1019 | ;; ---------------------------------------------------------------------- | |
1020 | ||
1021 | (defun bs-config-clear () | |
e0d0133a | 1022 | "Reset all variables which specify a configuration. |
6448a6b3 GM |
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 | |
dc7904f5 DL |
1027 | bs-must-show-regexp nil |
1028 | bs-dont-show-function nil | |
1029 | bs-must-show-function nil | |
1030 | bs-buffer-sort-function nil)) | |
6448a6b3 GM |
1031 | |
1032 | (defun bs-config--only-files () | |
1033 | "Define a configuration for showing only buffers visiting a file." | |
1034 | (bs-config-clear) | |
880ed9f1 | 1035 | (setq ;; I want to see *-buffers at the end |
dc7904f5 DL |
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)) | |
6448a6b3 GM |
1039 | |
1040 | (defun bs-config--files-and-scratch () | |
1041 | "Define a configuration for showing buffer *scratch* and file buffers." | |
1042 | (bs-config-clear) | |
880ed9f1 | 1043 | (setq ;; I want to see *-buffers at the end |
dc7904f5 DL |
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. | |
880ed9f1 | 1048 | bs-must-show-regexp "^\\*scratch\\*$")) |
6448a6b3 GM |
1049 | |
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)) | |
1054 | ||
1055 | (defun bs-config--all-intern-last () | |
1056 | "Define a configuration for showing all buffers. | |
880ed9f1 | 1057 | Internal buffers appear at end of all buffers." |
6448a6b3 GM |
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)) | |
1061 | ||
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: " | |
dc7904f5 DL |
1067 | bs-configurations |
1068 | nil | |
1069 | t))) | |
6448a6b3 GM |
1070 | (let ((list (assoc name bs-configurations))) |
1071 | (if list | |
dc7904f5 DL |
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)) | |
135377f1 | 1079 | ;; for backward compatibility |
dc7904f5 | 1080 | (funcall (cdr list))) |
6448a6b3 GM |
1081 | ;; else |
1082 | (ding) | |
1083 | (bs-message-without-log "No bs-configuration named %S." name)))) | |
1084 | ||
1085 | (defun bs-help () | |
1086 | "Help for `bs-show'." | |
1087 | (interactive) | |
1088 | (describe-function 'bs-mode)) | |
1089 | ||
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) | |
dc7904f5 DL |
1094 | (length (length list)) |
1095 | pos) | |
6448a6b3 | 1096 | (while (and assocs (not pos)) |
e749f576 JB |
1097 | (when (string= (car (car assocs)) start-name) |
1098 | (setq pos (- length (length assocs)))) | |
6448a6b3 GM |
1099 | (setq assocs (cdr assocs))) |
1100 | (setq pos (1+ pos)) | |
1101 | (if (eq pos length) | |
dc7904f5 | 1102 | (car list) |
6448a6b3 GM |
1103 | (nth pos list)))) |
1104 | ||
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)) | |
1108 | ||
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) | |
880ed9f1 | 1119 | (bs-message-without-log "Selected configuration: %s" (car config)))) |
6448a6b3 GM |
1120 | |
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) | |
dc7904f5 DL |
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)))) | |
6448a6b3 GM |
1137 | (erase-buffer) |
1138 | (setq bs--name-entry-length name-entry-length) | |
1139 | (bs--show-header) | |
e749f576 JB |
1140 | (dolist (buffer list) |
1141 | (bs--insert-one-entry buffer) | |
1142 | (insert "\n")) | |
d355a0b7 | 1143 | (delete-char -1) |
6448a6b3 GM |
1144 | (bs--set-window-height) |
1145 | (bs--goto-current-buffer) | |
1146 | (font-lock-fontify-buffer) | |
310a26be JB |
1147 | (bs-apply-sort-faces) |
1148 | (set-buffer-modified-p nil))) | |
6448a6b3 GM |
1149 | |
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 | |
dc7904f5 DL |
1157 | bs--current-sort-function)) |
1158 | (bs-buffer-list (or buffer-list (bs-buffer-list)))) | |
6448a6b3 | 1159 | (cons (or (car (cdr bs-buffer-list)) |
dc7904f5 DL |
1160 | (car bs-buffer-list) |
1161 | (current-buffer)) | |
1162 | bs-buffer-list))) | |
6448a6b3 GM |
1163 | |
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 | |
dc7904f5 DL |
1171 | bs--current-sort-function)) |
1172 | (bs-buffer-list (or buffer-list (bs-buffer-list)))) | |
6448a6b3 | 1173 | (cons (or (car (last bs-buffer-list)) |
dc7904f5 DL |
1174 | (current-buffer)) |
1175 | bs-buffer-list))) | |
6448a6b3 GM |
1176 | |
1177 | (defun bs-message-without-log (&rest args) | |
1178 | "Like `message' but don't log it on the message log. | |
a1d5a11b | 1179 | All arguments ARGS are transferred to function `message'." |
6448a6b3 GM |
1180 | (let ((message-log-max nil)) |
1181 | (apply 'message args))) | |
1182 | ||
1183 | (defvar bs--cycle-list nil | |
880ed9f1 | 1184 | "Current buffer list used for cycling.") |
6448a6b3 GM |
1185 | |
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)) | |
dc7904f5 DL |
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) | |
038e04cb GM |
1197 | (bs--show-all nil)) |
1198 | (bs-set-configuration (or bs-cycle-configuration-name bs-default-configuration)) | |
6448a6b3 | 1199 | (let ((bs-buffer-sort-function nil) |
dc7904f5 | 1200 | (bs--current-sort-function nil)) |
6448a6b3 | 1201 | (let* ((tupel (bs-next-buffer (if (or (eq last-command |
dc7904f5 DL |
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))) | |
c9d29fb8 SM |
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)) | |
8bdfa064 | 1211 | (switch-to-buffer next nil t) |
dc7904f5 DL |
1212 | (setq bs--cycle-list (append (cdr cycle-list) |
1213 | (list (car cycle-list)))) | |
dc7904f5 DL |
1214 | (bs-message-without-log "Next buffers: %s" |
1215 | (or (cdr bs--cycle-list) | |
1216 | "this buffer")))))) | |
6448a6b3 | 1217 | |
6448a6b3 GM |
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)) | |
dc7904f5 DL |
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) | |
038e04cb GM |
1229 | (bs--show-all nil)) |
1230 | (bs-set-configuration (or bs-cycle-configuration-name bs-default-configuration)) | |
6448a6b3 | 1231 | (let ((bs-buffer-sort-function nil) |
dc7904f5 | 1232 | (bs--current-sort-function nil)) |
6448a6b3 | 1233 | (let* ((tupel (bs-previous-buffer (if (or (eq last-command |
dc7904f5 DL |
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))) | |
8bdfa064 | 1240 | (switch-to-buffer prev-buffer nil t) |
dc7904f5 DL |
1241 | (setq bs--cycle-list (append (last cycle-list) |
1242 | (reverse (cdr (reverse cycle-list))))) | |
dc7904f5 DL |
1243 | (bs-message-without-log "Previous buffers: %s" |
1244 | (or (reverse (cdr bs--cycle-list)) | |
1245 | "this buffer")))))) | |
6448a6b3 GM |
1246 | |
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) | |
dc7904f5 DL |
1252 | fun) |
1253 | ((stringp fun) | |
1254 | fun) | |
1255 | (t (apply fun args)))) | |
6448a6b3 | 1256 | |
2fbc1934 | 1257 | (defun bs--get-marked-string (start-buffer _all-buffers) |
6448a6b3 GM |
1258 | "Return a string which describes whether current buffer is marked. |
1259 | START-BUFFER is the buffer where we started buffer selection. | |
462db93c | 1260 | ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu. |
6448a6b3 GM |
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'." | |
880ed9f1 | 1264 | (cond ;; current buffer is the buffer we started buffer selection. |
dc7904f5 DL |
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. | |
1279 | (t | |
1280 | bs-string-show-always))) | |
6448a6b3 | 1281 | |
2fbc1934 | 1282 | (defun bs--get-modified-string (_start-buffer _all-buffers) |
6448a6b3 GM |
1283 | "Return a string which describes whether current buffer is modified. |
1284 | START-BUFFER is the buffer where we started buffer selection. | |
462db93c | 1285 | ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu." |
6448a6b3 GM |
1286 | (if (buffer-modified-p) "*" " ")) |
1287 | ||
2fbc1934 | 1288 | (defun bs--get-readonly-string (_start-buffer _all-buffers) |
6448a6b3 GM |
1289 | "Return a string which describes whether current buffer is read only. |
1290 | START-BUFFER is the buffer where we started buffer selection. | |
462db93c | 1291 | ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu." |
6448a6b3 GM |
1292 | (if buffer-read-only "%" " ")) |
1293 | ||
2fbc1934 | 1294 | (defun bs--get-size-string (_start-buffer _all-buffers) |
6448a6b3 GM |
1295 | "Return a string which describes the size of current buffer. |
1296 | START-BUFFER is the buffer where we started buffer selection. | |
462db93c | 1297 | ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu." |
6448a6b3 GM |
1298 | (int-to-string (buffer-size))) |
1299 | ||
2fbc1934 | 1300 | (defun bs--get-name (_start-buffer _all-buffers) |
6448a6b3 GM |
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. | |
462db93c | 1305 | ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu." |
635ed242 JB |
1306 | (propertize (buffer-name) |
1307 | 'help-echo "mouse-2: select this buffer, mouse-3: select in other frame" | |
1308 | 'mouse-face 'highlight)) | |
6448a6b3 | 1309 | |
2fbc1934 | 1310 | (defun bs--get-mode-name (start-buffer _all-buffers) |
6448a6b3 GM |
1311 | "Return the name of mode of current buffer for Buffer Selection Menu. |
1312 | START-BUFFER is the buffer where we started buffer selection. | |
462db93c | 1313 | ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu." |
c2699583 | 1314 | (format-mode-line mode-name nil nil start-buffer)) |
6448a6b3 | 1315 | |
2fbc1934 | 1316 | (defun bs--get-file-name (_start-buffer _all-buffers) |
6448a6b3 GM |
1317 | "Return string for column 'File' in Buffer Selection Menu. |
1318 | This is the variable `buffer-file-name' of current buffer. | |
027b1942 | 1319 | If not visiting a file, `list-buffers-directory' is returned instead. |
6448a6b3 | 1320 | START-BUFFER is the buffer where we started buffer selection. |
b75ccc22 | 1321 | ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu." |
027b1942 JB |
1322 | (propertize (or buffer-file-name |
1323 | (bound-and-true-p list-buffers-directory) | |
1324 | "") | |
310a26be JB |
1325 | 'mouse-face 'highlight |
1326 | 'help-echo "mouse-2: select this buffer, mouse-3: select in other frame")) | |
6448a6b3 | 1327 | |
6448a6b3 GM |
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 "") | |
dc7904f5 | 1334 | (to-much 0) |
6448a6b3 | 1335 | (apply-args (append (list bs--buffer-coming-from bs-current-list)))) |
70ef8857 | 1336 | (with-current-buffer buffer |
e749f576 JB |
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))) | |
dc7904f5 | 1344 | (setq string (concat string new-string)) |
e749f576 JB |
1345 | (when (> len min) |
1346 | (setq to-much (- len min)))))) | |
1347 | (insert string))) | |
6448a6b3 GM |
1348 | |
1349 | (defun bs--format-aux (string align len) | |
462db93c | 1350 | "Pad STRING to length LEN with alignment ALIGN. |
6448a6b3 | 1351 | ALIGN is one of the symbols `left', `middle', or `right'." |
635ed242 JB |
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)))) | |
6448a6b3 GM |
1358 | |
1359 | (defun bs--show-header () | |
1360 | "Insert header for Buffer Selection Menu in current buffer." | |
e749f576 JB |
1361 | (insert (bs--create-header-line #'identity) |
1362 | "\n" | |
1363 | (bs--create-header-line (lambda (title) | |
1364 | (make-string (length title) ?-))) | |
1365 | "\n")) | |
6448a6b3 GM |
1366 | |
1367 | (defun bs--get-name-length () | |
1368 | "Return value of `bs--name-entry-length'." | |
1369 | bs--name-entry-length) | |
1370 | ||
e749f576 JB |
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 | "")) | |
6448a6b3 GM |
1381 | |
1382 | (defun bs--show-with-configuration (name &optional arg) | |
880ed9f1 | 1383 | "Display buffer list of configuration with name NAME. |
6448a6b3 GM |
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))) | |
dc7904f5 | 1395 | (unless (string= "*buffer-selection*" (buffer-name)) |
6448a6b3 GM |
1396 | ;; Only when not in buffer *buffer-selection* |
1397 | ;; we have to set the buffer we started the command | |
ce3ba12c | 1398 | (setq bs--buffer-coming-from (current-buffer))) |
dc7904f5 | 1399 | (let ((liste (bs-buffer-list)) |
edf6fbc5 | 1400 | (active-window (get-window-with-predicate |
e749f576 JB |
1401 | (lambda (w) |
1402 | (string= (buffer-name (window-buffer w)) | |
1403 | "*buffer-selection*")) | |
1404 | nil (selected-frame)))) | |
dc7904f5 DL |
1405 | (if active-window |
1406 | (select-window active-window) | |
e749f576 JB |
1407 | (bs--restore-window-config) |
1408 | (setq bs--window-config-coming-from (current-window-configuration)) | |
290d5b58 | 1409 | (when (> (window-height) 7) |
ea8fb88d JB |
1410 | ;; Errors would mess with the window configuration (bug#10882). |
1411 | (ignore-errors (select-window (split-window-below))))) | |
dc7904f5 DL |
1412 | (bs-show-in-buffer liste) |
1413 | (bs-message-without-log "%s" (bs--current-config-message))))) | |
6448a6b3 | 1414 | |
2fbc1934 JB |
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'. | |
6448a6b3 | 1419 | Otherwise return `bs-alternative-configuration'." |
880ed9f1 | 1420 | (cond ;; usually activation |
2fbc1934 | 1421 | ((null prefix) |
dc7904f5 DL |
1422 | bs-default-configuration) |
1423 | ;; call with integer as prefix argument | |
2fbc1934 JB |
1424 | ((integerp prefix) |
1425 | (if (and (< 0 prefix) (<= prefix (length bs-configurations))) | |
1426 | (car (nth (1- prefix) bs-configurations)) | |
dc7904f5 DL |
1427 | bs-default-configuration)) |
1428 | ;; call by prefix argument C-u | |
1429 | (t bs-alternative-configuration))) | |
6448a6b3 GM |
1430 | |
1431 | ;; ---------------------------------------------------------------------- | |
1432 | ;; Main function bs-customize and bs-show | |
1433 | ;; ---------------------------------------------------------------------- | |
1434 | ||
1435 | ;;;###autoload | |
1436 | (defun bs-customize () | |
1437 | "Customization of group bs for Buffer Selection Menu." | |
1438 | (interactive) | |
1439 | (customize-group "bs")) | |
1440 | ||
1441 | ;;;###autoload | |
1442 | (defun bs-show (arg) | |
dc7904f5 | 1443 | "Make a menu of buffers so you can manipulate buffers or the buffer list. |
6448a6b3 GM |
1444 | \\<bs-mode-map> |
1445 | There are many key commands similar to `Buffer-menu-mode' for | |
b75ccc22 | 1446 | manipulating the buffer list and the buffers themselves. |
6448a6b3 GM |
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))) | |
1457 | ||
c4384ef1 JB |
1458 | ;; ---------------------------------------------------------------------- |
1459 | ;; Cleanup | |
1460 | ;; ---------------------------------------------------------------------- | |
1461 | ||
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) | |
1472 | ||
d8754ce5 | 1473 | ;; Now provide feature bs |
6448a6b3 GM |
1474 | (provide 'bs) |
1475 | ||
1476 | ;;; bs.el ends here |