]> code.delx.au - gnu-emacs/blob - lisp/msb.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / msb.el
1 ;;; msb.el --- customizable buffer-selection with multiple menus
2
3 ;; Copyright (C) 1993-1995, 1997-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Lindberg <lars.lindberg@home.se>
6 ;; Maintainer: FSF
7 ;; Created: 8 Oct 1993
8 ;; Lindberg's last update version: 3.34
9 ;; Keywords: mouse buffer menu
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Purpose of this package:
29 ;; 1. Offer a function for letting the user choose buffer,
30 ;; not necessarily for switching to it.
31 ;; 2. Make a better mouse-buffer-menu. This is done as a global
32 ;; minor mode, msb-mode.
33 ;;
34 ;; Customization:
35 ;; Look at the variable `msb-menu-cond' for deciding what menus you
36 ;; want. It's not that hard to customize, despite my not-so-good
37 ;; doc-string. Feel free to send me a better doc-string.
38 ;; There are some constants for you to try here:
39 ;; msb--few-menus
40 ;; msb--very-many-menus (default)
41 ;;
42 ;; Look at the variable `msb-item-handling-function' for customization
43 ;; of the appearance of every menu item. Try for instance setting
44 ;; it to `msb-alon-item-handler'.
45 ;;
46 ;; Look at the variable `msb-item-sort-function' for customization
47 ;; of sorting the menus. Set it to t for instance, which means no
48 ;; sorting - you will get latest used buffer first.
49 ;;
50 ;; Also check out the variable `msb-display-invisible-buffers-p'.
51
52 ;; Known bugs:
53 ;; - Files-by-directory
54 ;; + No possibility to show client/changed buffers separately.
55 ;; + All file buffers only appear in a file sub-menu, they will
56 ;; for instance not appear in the Mail sub-menu.
57
58 ;; Future enhancements:
59
60 ;;; Thanks goes to
61 ;; Mark Brader <msb@sq.com>
62 ;; Jim Berry <m1jhb00@FRB.GOV>
63 ;; Hans Chalupsky <hans@cs.Buffalo.EDU>
64 ;; Larry Rosenberg <ljr@ictv.com>
65 ;; Will Henney <will@astroscu.unam.mx>
66 ;; Jari Aalto <jaalto@tre.tele.nokia.fi>
67 ;; Michael Kifer <kifer@sbkifer.cs.sunysb.edu>
68 ;; Gael Marziou <gael@gnlab030.grenoble.hp.com>
69 ;; Dave Gillespie <daveg@thymus.synaptics.com>
70 ;; Alon Albert <alon@milcse.rtsg.mot.com>
71 ;; Kevin Broadey, <KevinB@bartley.demon.co.uk>
72 ;; Ake Stenhof <ake@cadpoint.se>
73 ;; Richard Stallman <rms@gnu.org>
74 ;; Steve Fisk <fisk@medved.bowdoin.edu>
75
76 ;; This version turned into a global minor mode and subsequently
77 ;; hacked on by Dave Love.
78 ;;; Code:
79
80 (eval-when-compile (require 'cl))
81
82 ;;;
83 ;;; Some example constants to be used for `msb-menu-cond'. See that
84 ;;; variable for more information. Please note that if the condition
85 ;;; returns `multi', then the buffer can appear in several menus.
86 ;;;
87 (defconst msb--few-menus
88 '(((and (boundp 'server-buffer-clients)
89 server-buffer-clients
90 'multi)
91 3030
92 "Clients (%d)")
93 ((and msb-display-invisible-buffers-p
94 (msb-invisible-buffer-p)
95 'multi)
96 3090
97 "Invisible buffers (%d)")
98 ((eq major-mode 'dired-mode)
99 2010
100 "Dired (%d)"
101 msb-dired-item-handler
102 msb-sort-by-directory)
103 ((eq major-mode 'Man-mode)
104 4090
105 "Manuals (%d)")
106 ((eq major-mode 'w3-mode)
107 4020
108 "WWW (%d)")
109 ((or (memq major-mode
110 '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
111 (memq major-mode '(mh-letter-mode mh-show-mode mh-folder-mode))
112 (memq major-mode
113 '(gnus-summary-mode message-mode gnus-group-mode
114 gnus-article-mode score-mode gnus-browse-killed-mode)))
115 4010
116 "Mail (%d)")
117 ((not buffer-file-name)
118 4099
119 "Buffers (%d)")
120 ('no-multi
121 1099
122 "Files (%d)")))
123
124 (defconst msb--very-many-menus
125 '(((and (boundp 'server-buffer-clients)
126 server-buffer-clients
127 'multi)
128 1010
129 "Clients (%d)")
130 ((and (boundp 'vc-mode) vc-mode 'multi)
131 1020
132 "Version Control (%d)")
133 ((and buffer-file-name
134 (buffer-modified-p)
135 'multi)
136 1030
137 "Changed files (%d)")
138 ((and (get-buffer-process (current-buffer))
139 'multi)
140 1040
141 "Processes (%d)")
142 ((and msb-display-invisible-buffers-p
143 (msb-invisible-buffer-p)
144 'multi)
145 1090
146 "Invisible buffers (%d)")
147 ((eq major-mode 'dired-mode)
148 2010
149 "Dired (%d)"
150 ;; Note this different menu-handler
151 msb-dired-item-handler
152 ;; Also note this item-sorter
153 msb-sort-by-directory)
154 ((eq major-mode 'Man-mode)
155 5030
156 "Manuals (%d)")
157 ((eq major-mode 'w3-mode)
158 5020
159 "WWW (%d)")
160 ((or (memq major-mode
161 '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
162 (memq major-mode '(mh-letter-mode mh-show-mode mh-folder-mode))
163 (memq major-mode '(gnus-summary-mode message-mode gnus-group-mode
164 gnus-article-mode score-mode
165 gnus-browse-killed-mode)))
166 5010
167 "Mail (%d)")
168 ;; Catchup for all non-file buffers
169 ((and (not buffer-file-name)
170 'no-multi)
171 5099
172 "Other non-file buffers (%d)")
173 ((and (string-match "/\\.[^/]*$" buffer-file-name)
174 'multi)
175 3090
176 "Hidden Files (%d)")
177 ((memq major-mode '(c-mode c++-mode))
178 3010
179 "C/C++ Files (%d)")
180 ((eq major-mode 'emacs-lisp-mode)
181 3020
182 "Elisp Files (%d)")
183 ((eq major-mode 'latex-mode)
184 3030
185 "LaTeX Files (%d)")
186 ('no-multi
187 3099
188 "Other files (%d)")))
189
190 ;;;
191 ;;; Customizable variables
192 ;;;
193
194 (defgroup msb nil
195 "Customizable buffer-selection with multiple menus."
196 :prefix "msb-"
197 :group 'mouse)
198
199 (defun msb-custom-set (symbol value)
200 "Set the value of custom variables for msb."
201 (set symbol value)
202 (if (and (featurep 'msb) msb-mode)
203 ;; wait until package has been loaded before bothering to update
204 ;; the buffer lists.
205 (msb-menu-bar-update-buffers t)))
206
207 (defcustom msb-menu-cond msb--very-many-menus
208 "List of criteria for splitting the mouse buffer menu.
209 The elements in the list should be of this type:
210 (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).
211
212 When making the split, the buffers are tested one by one against the
213 CONDITION, just like a Lisp cond: When hitting a true condition, the
214 other criteria are *not* tested and the buffer name will appear in the
215 menu with the menu-title corresponding to the true condition.
216
217 If the condition returns the symbol `multi', then the buffer will be
218 added to this menu *and* tested for other menus too. If it returns
219 `no-multi', then the buffer will only be added if it hasn't been added
220 to any other menu.
221
222 During this test, the buffer in question is the current buffer, and
223 the test is surrounded by calls to `save-excursion' and
224 `save-match-data'.
225
226 The categories are sorted by MENU-SORT-KEY. Smaller keys are on top.
227 A value of nil means don't display this menu.
228
229 MENU-TITLE is really a format. If you add %d in it, the %d is
230 replaced with the number of items in that menu.
231
232 ITEM-HANDLING-FN is optional. If it is supplied and is a function,
233 then it is used for displaying the items in that particular buffer
234 menu, otherwise the function pointed out by
235 `msb-item-handling-function' is used.
236
237 ITEM-SORT-FN is also optional.
238 If it is not supplied, the function pointed out by
239 `msb-item-sort-function' is used.
240 If it is nil, then no sort takes place and the buffers are presented
241 in least-recently-used order.
242 If it is t, then no sort takes place and the buffers are presented in
243 most-recently-used order.
244 If it is supplied and non-nil and not t than it is used for sorting
245 the items in that particular buffer menu.
246
247 Note1: There should always be a `catch-all' as last element, in this
248 list. That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
249 Note2: A buffer menu appears only if it has at least one buffer in it.
250 Note3: If you have a CONDITION that can't be evaluated you will get an
251 error every time you do \\[msb]."
252 :type `(choice (const :tag "long" :value ,msb--very-many-menus)
253 (const :tag "short" :value ,msb--few-menus)
254 (sexp :tag "user"))
255 :set 'msb-custom-set
256 :group 'msb)
257
258 (defcustom msb-modes-key 4000
259 "The sort key for files sorted by mode."
260 :type 'integer
261 :set 'msb-custom-set
262 :group 'msb
263 :version "20.3")
264
265 (defcustom msb-separator-diff 100
266 "Non-nil means use separators.
267 The separators will appear between all menus that have a sorting key
268 that differs by this value or more."
269 :type '(choice integer (const nil))
270 :set 'msb-custom-set
271 :group 'msb)
272
273 (defvar msb-files-by-directory-sort-key 0
274 "*The sort key for files sorted by directory.")
275
276 (defcustom msb-max-menu-items 15
277 "The maximum number of items in a menu.
278 If this variable is set to 15 for instance, then the submenu will be
279 split up in minor parts, 15 items each. A value of nil means no limit."
280 :type '(choice integer (const nil))
281 :set 'msb-custom-set
282 :group 'msb)
283
284 (defcustom msb-max-file-menu-items 10
285 "The maximum number of items from different directories.
286
287 When the menu is of type `file by directory', this is the maximum
288 number of buffers that are clumped together from different
289 directories.
290
291 Set this to 1 if you want one menu per directory instead of clumping
292 them together.
293
294 If the value is not a number, then the value 10 is used."
295 :type 'integer
296 :set 'msb-custom-set
297 :group 'msb)
298
299 (defcustom msb-most-recently-used-sort-key -1010
300 "Where should the menu with the most recently used buffers be placed?"
301 :type 'integer
302 :set 'msb-custom-set
303 :group 'msb)
304
305 (defcustom msb-display-most-recently-used 15
306 "How many buffers should be in the most-recently-used menu.
307 No buffers at all if less than 1 or nil (or any non-number)."
308 :type 'integer
309 :set 'msb-custom-set
310 :group 'msb)
311
312 (defcustom msb-most-recently-used-title "Most recently used (%d)"
313 "The title for the most-recently-used menu."
314 :type 'string
315 :set 'msb-custom-set
316 :group 'msb)
317
318 (defvar msb-horizontal-shift-function '(lambda () 0)
319 "*Function that specifies how many pixels to shift the top menu leftwards.")
320
321 (defcustom msb-display-invisible-buffers-p nil
322 "Show invisible buffers or not.
323 Non-nil means that the buffer menu should include buffers that have
324 names that starts with a space character."
325 :type 'boolean
326 :set 'msb-custom-set
327 :group 'msb)
328
329 (defvar msb-item-handling-function 'msb-item-handler
330 "*The appearance of a buffer menu.
331
332 The default function to call for handling the appearance of a menu
333 item. It should take two arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
334 where the latter is the max length of all buffer names.
335
336 The function should return the string to use in the menu.
337
338 When the function is called, BUFFER is the current buffer. This
339 function is called for items in the variable `msb-menu-cond' that have
340 nil as ITEM-HANDLING-FUNCTION. See `msb-menu-cond' for more
341 information.")
342
343 (defcustom msb-item-sort-function 'msb-sort-by-name
344 "The order of items in a buffer menu.
345
346 The default function to call for handling the order of items in a menu
347 item. This function is called like a sort function. The items look
348 like (ITEM-NAME . BUFFER).
349
350 ITEM-NAME is the name of the item that will appear in the menu.
351 BUFFER is the buffer, this is not necessarily the current buffer.
352
353 Set this to nil or t if you don't want any sorting (faster)."
354 :type '(choice (const msb-sort-by-name)
355 (const :tag "Newest first" t)
356 (const :tag "Oldest first" nil))
357 :set 'msb-custom-set
358 :group 'msb)
359
360 (defcustom msb-files-by-directory nil
361 "Non-nil means that files should be sorted by directory.
362 This is instead of the groups in `msb-menu-cond'."
363 :type 'boolean
364 :set 'msb-custom-set
365 :group 'msb)
366
367 (defcustom msb-after-load-hook nil
368 "Hook run after the msb package has been loaded."
369 :type 'hook
370 :set 'msb-custom-set
371 :group 'msb)
372
373 ;;;
374 ;;; Internal variables
375 ;;;
376
377 ;; The last calculated menu.
378 (defvar msb--last-buffer-menu nil)
379
380 ;; If this is non-nil, then it is a string that describes the error.
381 (defvar msb--error nil)
382
383 ;;;
384 ;;; Some example function to be used for `msb-item-handling-function'.
385 ;;;
386 (defun msb-item-handler (buffer &optional maxbuf)
387 "Create one string item, concerning BUFFER, for the buffer menu.
388 The item looks like:
389 *% <buffer-name>
390 The `*' appears only if the buffer is marked as modified.
391 The `%' appears only if the buffer is read-only.
392 Optional second argument MAXBUF is completely ignored."
393 (let ((name (buffer-name))
394 (modified (if (buffer-modified-p) "*" " "))
395 (read-only (if buffer-read-only "%" " ")))
396 (format "%s%s %s" modified read-only name)))
397
398
399 (eval-when-compile (require 'dired))
400
401 ;; `dired' can be called with a list of the form (directory file1 file2 ...)
402 ;; which causes `dired-directory' to be in the same form.
403 (defun msb--dired-directory ()
404 (cond ((stringp dired-directory)
405 (abbreviate-file-name (expand-file-name dired-directory)))
406 ((consp dired-directory)
407 (abbreviate-file-name (expand-file-name (car dired-directory))))
408 (t
409 (error "Unknown type of `dired-directory' in buffer %s"
410 (buffer-name)))))
411
412 (defun msb-dired-item-handler (buffer &optional maxbuf)
413 "Create one string item, concerning a dired BUFFER, for the buffer menu.
414 The item looks like:
415 *% <buffer-name>
416 The `*' appears only if the buffer is marked as modified.
417 The `%' appears only if the buffer is read-only.
418 Optional second argument MAXBUF is completely ignored."
419 (let ((name (msb--dired-directory))
420 (modified (if (buffer-modified-p) "*" " "))
421 (read-only (if buffer-read-only "%" " ")))
422 (format "%s%s %s" modified read-only name)))
423
424 (defun msb-alon-item-handler (buffer maxbuf)
425 "Create one string item for the buffer menu.
426 The item looks like:
427 <buffer-name> *%# <file-name>
428 The `*' appears only if the buffer is marked as modified.
429 The `%' appears only if the buffer is read-only.
430 The `#' appears only version control file (SCCS/RCS)."
431 (format (format "%%%ds %%s%%s%%s %%s" maxbuf)
432 (buffer-name buffer)
433 (if (buffer-modified-p) "*" " ")
434 (if buffer-read-only "%" " ")
435 (if (and (boundp 'vc-mode) vc-mode) "#" " ")
436 (or buffer-file-name "")))
437
438 ;;;
439 ;;; Some example function to be used for `msb-item-sort-function'.
440 ;;;
441 (defun msb-sort-by-name (item1 item2)
442 "Sort the items ITEM1 and ITEM2 by their `buffer-name'.
443 An item looks like (NAME . BUFFER)."
444 (string-lessp (buffer-name (cdr item1))
445 (buffer-name (cdr item2))))
446
447
448 (defun msb-sort-by-directory (item1 item2)
449 "Sort the items ITEM1 and ITEM2 by directory name. Made for dired.
450 An item look like (NAME . BUFFER)."
451 (string-lessp (with-current-buffer (cdr item1)
452 (msb--dired-directory))
453 (with-current-buffer (cdr item2)
454 (msb--dired-directory))))
455
456 ;;;
457 ;;; msb
458 ;;;
459 ;;; This function can be used instead of (mouse-buffer-menu EVENT)
460 ;;; function in "mouse.el".
461 ;;;
462 (defun msb (event)
463 "Pop up several menus of buffers for selection with the mouse.
464 This command switches buffers in the window that you clicked on, and
465 selects that window.
466
467 See the function `mouse-select-buffer' and the variable
468 `msb-menu-cond' for more information about how the menus are split."
469 (interactive "e")
470 (let ((old-window (selected-window))
471 (window (posn-window (event-start event)))
472 early-release)
473 (unless (framep window) (select-window window))
474 ;; This `sit-for' magically makes the menu stay up if the mouse
475 ;; button is released within 0.1 second.
476 (setq early-release (not (sit-for 0.1 t)))
477 (let ((buffer (mouse-select-buffer event)))
478 (if buffer
479 (switch-to-buffer buffer)
480 (select-window old-window)))
481 ;; If the above `sit-for' was interrupted by a mouse-up, avoid
482 ;; generating a drag event.
483 (if (and early-release (memq 'down (event-modifiers last-input-event)))
484 (discard-input)))
485 nil)
486
487 ;;;
488 ;;; Some supportive functions
489 ;;;
490 (defun msb-invisible-buffer-p (&optional buffer)
491 "Return t if optional BUFFER is an \"invisible\" buffer.
492 If the argument is left out or nil, then the current buffer is considered."
493 (and (> (length (buffer-name buffer)) 0)
494 (eq ?\s (aref (buffer-name buffer) 0))))
495
496 (defun msb--strip-dir (dir)
497 "Strip one hierarchy level from the end of DIR."
498 (file-name-directory (directory-file-name dir)))
499
500 ;; Create an alist with all buffers from LIST that lies under the same
501 ;; directory will be in the same item as the directory name.
502 ;; ((DIR1 . (BUFFER-1 BUFFER-2 ...)) (DIR2 . (BUFFER-K BUFFER-K+1...)) ...)
503 (defun msb--init-file-alist (list)
504 (let ((buffer-alist
505 ;; Make alist that looks like
506 ;; ((DIR-1 BUFFER-1) (DIR-2 BUFFER-2) ...)
507 ;; sorted on DIR-x
508 (sort
509 (apply #'nconc
510 (mapcar
511 (lambda (buffer)
512 (let ((file-name (expand-file-name
513 (buffer-file-name buffer))))
514 (when file-name
515 (list (cons (msb--strip-dir file-name) buffer)))))
516 list))
517 (lambda (item1 item2)
518 (string< (car item1) (car item2))))))
519 ;; Now clump buffers together that have the same directory name
520 ;; Make alist that looks like
521 ;; ((DIR1 . (BUFFER-1 BUFFER-2 ...)) (DIR2 . (BUFFER-K)) ...)
522 (let ((dir nil)
523 (buffers nil))
524 (nconc
525 (apply
526 #'nconc
527 (mapcar (lambda (item)
528 (cond
529 ((equal dir (car item))
530 ;; The same dir as earlier:
531 ;; Add to current list of buffers.
532 (push (cdr item) buffers)
533 ;; This item should not be added to list
534 nil)
535 (t
536 ;; New dir
537 (let ((result (and dir (cons dir buffers))))
538 (setq dir (car item))
539 (setq buffers (list (cdr item)))
540 ;; Add the last result the list.
541 (and result (list result))))))
542 buffer-alist))
543 ;; Add the last result to the list
544 (list (cons dir buffers))))))
545
546 (defun msb--format-title (top-found-p dir number-of-items)
547 "Format a suitable title for the menu item."
548 (format (if top-found-p "%s... (%d)" "%s (%d)")
549 (abbreviate-file-name dir) number-of-items))
550
551 ;; Variables for debugging.
552 (defvar msb--choose-file-menu-list)
553 (defvar msb--choose-file-menu-arg-list)
554
555 (defun msb--choose-file-menu (list)
556 "Choose file-menu with respect to directory for every buffer in LIST."
557 (setq msb--choose-file-menu-arg-list list)
558 (let ((buffer-alist (msb--init-file-alist list))
559 (final-list nil)
560 (max-clumped-together (if (numberp msb-max-file-menu-items)
561 msb-max-file-menu-items
562 10))
563 (top-found-p nil)
564 (last-dir nil)
565 first rest dir buffers old-dir)
566 ;; Prepare for looping over all items in buffer-alist
567 (setq first (car buffer-alist)
568 rest (cdr buffer-alist)
569 dir (car first)
570 buffers (cdr first))
571 (setq msb--choose-file-menu-list (copy-sequence rest))
572 ;; This big loop tries to clump buffers together that have a
573 ;; similar name. Remember that buffer-alist is sorted based on the
574 ;; directory name of the buffers' visited files.
575 (while rest
576 (let ((found-p nil)
577 (tmp-rest rest)
578 item)
579 (setq item (car tmp-rest))
580 ;; Clump together the "rest"-buffers that have a dir that is
581 ;; a subdir of the current one.
582 (while (and tmp-rest
583 (<= (length buffers) max-clumped-together)
584 (>= (length (car item)) (length dir))
585 ;; `completion-ignore-case' seems to default to t
586 ;; on the systems with case-insensitive file names.
587 (eq t (compare-strings dir 0 nil
588 (car item) 0 (length dir)
589 completion-ignore-case)))
590 (setq found-p t)
591 (setq buffers (append buffers (cdr item))) ;nconc is faster than append
592 (setq tmp-rest (cdr tmp-rest)
593 item (car tmp-rest)))
594 (cond
595 ((> (length buffers) max-clumped-together)
596 ;; Oh, we failed. Too many buffers clumped together.
597 ;; Just use the original ones for the result.
598 (setq last-dir (car first))
599 (push (cons (msb--format-title top-found-p
600 (car first)
601 (length (cdr first)))
602 (cdr first))
603 final-list)
604 (setq top-found-p nil)
605 (setq first (car rest)
606 rest (cdr rest)
607 dir (car first)
608 buffers (cdr first)))
609 (t
610 ;; The first pass of clumping together worked out, go ahead
611 ;; with this result.
612 (when found-p
613 (setq top-found-p t)
614 (setq first (cons dir buffers)
615 rest tmp-rest))
616 ;; Now see if we can clump more buffers together if we go up
617 ;; one step in the file hierarchy.
618 ;; If dir isn't changed by msb--strip-dir, we are looking
619 ;; at the machine name component of an ange-ftp filename.
620 (setq old-dir dir)
621 (setq dir (msb--strip-dir dir)
622 buffers (cdr first))
623 (if (equal old-dir dir)
624 (setq last-dir dir))
625 (when (and last-dir
626 (or (and (>= (length dir) (length last-dir))
627 (eq t (compare-strings
628 last-dir 0 nil dir 0
629 (length last-dir)
630 completion-ignore-case)))
631 (and (< (length dir) (length last-dir))
632 (eq t (compare-strings
633 dir 0 nil last-dir 0 (length dir)
634 completion-ignore-case)))))
635 ;; We have reached the same place in the file hierarchy as
636 ;; the last result, so we should quit at this point and
637 ;; take what we have as result.
638 (push (cons (msb--format-title top-found-p
639 (car first)
640 (length (cdr first)))
641 (cdr first))
642 final-list)
643 (setq top-found-p nil)
644 (setq first (car rest)
645 rest (cdr rest)
646 dir (car first)
647 buffers (cdr first)))))))
648 ;; Now take care of the last item.
649 (when first
650 (push (cons (msb--format-title top-found-p
651 (car first)
652 (length (cdr first)))
653 (cdr first))
654 final-list))
655 (setq top-found-p nil)
656 (nreverse final-list)))
657
658 (defun msb--create-function-info (menu-cond-elt)
659 "Create a vector from an element MENU-COND-ELT of `msb-menu-cond'.
660 This takes the form:
661 \[BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER]
662 See `msb-menu-cond' for a description of its elements."
663 (let* ((list-symbol (make-symbol "-msb-buffer-list"))
664 (tmp-ih (and (> (length menu-cond-elt) 3)
665 (nth 3 menu-cond-elt)))
666 (item-handler (if (and tmp-ih (fboundp tmp-ih))
667 tmp-ih
668 msb-item-handling-function))
669 (tmp-s (if (> (length menu-cond-elt) 4)
670 (nth 4 menu-cond-elt)
671 msb-item-sort-function))
672 (sorter (if (or (fboundp tmp-s)
673 (null tmp-s)
674 (eq tmp-s t))
675 tmp-s
676 msb-item-sort-function)))
677 (when (< (length menu-cond-elt) 3)
678 (error "Wrong format of msb-menu-cond"))
679 (when (and (> (length menu-cond-elt) 3)
680 (not (fboundp tmp-ih)))
681 (signal 'invalid-function (list tmp-ih)))
682 (when (and (> (length menu-cond-elt) 4)
683 tmp-s
684 (not (fboundp tmp-s))
685 (not (eq tmp-s t)))
686 (signal 'invalid-function (list tmp-s)))
687 (set list-symbol ())
688 (vector list-symbol ;BUFFER-LIST-VARIABLE
689 (nth 0 menu-cond-elt) ;CONDITION
690 (nth 1 menu-cond-elt) ;SORT-KEY
691 (nth 2 menu-cond-elt) ;MENU-TITLE
692 item-handler ;ITEM-HANDLER
693 sorter) ;SORTER
694 ))
695
696 ;; This defsubst is only used in `msb--choose-menu' below. It was
697 ;; pulled out merely to make the code somewhat clearer. The indentation
698 ;; level was too big.
699 (defsubst msb--collect (function-info-vector)
700 (let ((result nil)
701 (multi-flag nil)
702 function-info-list)
703 (setq function-info-list
704 (loop for fi
705 across function-info-vector
706 if (and (setq result
707 (eval (aref fi 1))) ;Test CONDITION
708 (not (and (eq result 'no-multi)
709 multi-flag))
710 (progn (when (eq result 'multi)
711 (setq multi-flag t))
712 t))
713 collect fi
714 until (and result
715 (not (eq result 'multi)))))
716 (when (and (not function-info-list)
717 (not result))
718 (error "No catch-all in msb-menu-cond!"))
719 function-info-list))
720
721 (defun msb--add-to-menu (buffer function-info max-buffer-name-length)
722 "Add BUFFER to the menu depicted by FUNCTION-INFO.
723 All side-effects. Adds an element of form (BUFFER-TITLE . BUFFER)
724 to the buffer-list variable in FUNCTION-INFO."
725 (let ((list-symbol (aref function-info 0))) ;BUFFER-LIST-VARIABLE
726 ;; Here comes the hairy side-effect!
727 (set list-symbol
728 (cons (cons (funcall (aref function-info 4) ;ITEM-HANDLER
729 buffer
730 max-buffer-name-length)
731 buffer)
732 (eval list-symbol)))))
733
734 (defsubst msb--choose-menu (buffer function-info-vector max-buffer-name-length)
735 "Select the appropriate menu for BUFFER."
736 ;; This is all side-effects, folks!
737 ;; This should be optimized.
738 (unless (and (not msb-display-invisible-buffers-p)
739 (msb-invisible-buffer-p buffer))
740 (condition-case nil
741 (with-current-buffer buffer
742 ;; Menu found. Add to this menu
743 (dolist (info (msb--collect function-info-vector))
744 (msb--add-to-menu buffer info max-buffer-name-length)))
745 (error (unless msb--error
746 (setq msb--error
747 (format
748 "In msb-menu-cond, error for buffer `%s'."
749 (buffer-name buffer)))
750 (error "%s" msb--error))))))
751
752 (defun msb--create-sort-item (function-info)
753 "Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the buffer-list is empty."
754 (let ((buffer-list (eval (aref function-info 0))))
755 (when buffer-list
756 (let ((sorter (aref function-info 5)) ;SORTER
757 (sort-key (aref function-info 2))) ;MENU-SORT-KEY
758 (when sort-key
759 (cons sort-key
760 (cons (format (aref function-info 3) ;MENU-TITLE
761 (length buffer-list))
762 (cond
763 ((null sorter)
764 buffer-list)
765 ((eq sorter t)
766 (nreverse buffer-list))
767 (t
768 (sort buffer-list sorter))))))))))
769
770 (defun msb--aggregate-alist (alist same-predicate sort-predicate)
771 "Return ALIST as a sorted, aggregated alist.
772
773 In the result all items with the same car element (according to
774 SAME-PREDICATE) are aggregated together. The alist is first sorted by
775 SORT-PREDICATE.
776
777 Example:
778 \(msb--aggregate-alist
779 '((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
780 (function string=)
781 (lambda (item1 item2)
782 (string< (symbol-name item1) (symbol-name item2))))
783 results in
784 \((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))"
785 (when (not (null alist))
786 (let (same
787 tmp-old-car
788 tmp-same
789 (first-time-p t)
790 old-car)
791 (nconc
792 (apply #'nconc
793 (mapcar
794 (lambda (item)
795 (cond
796 (first-time-p
797 (push (cdr item) same)
798 (setq first-time-p nil)
799 (setq old-car (car item))
800 nil)
801 ((funcall same-predicate (car item) old-car)
802 (push (cdr item) same)
803 nil)
804 (t
805 (setq tmp-same same
806 tmp-old-car old-car)
807 (setq same (list (cdr item))
808 old-car (car item))
809 (list (cons tmp-old-car (nreverse tmp-same))))))
810 (sort alist (lambda (item1 item2)
811 (funcall sort-predicate
812 (car item1) (car item2))))))
813 (list (cons old-car (nreverse same)))))))
814
815
816 (defun msb--mode-menu-cond ()
817 (let ((key msb-modes-key))
818 (mapcar (lambda (item)
819 (incf key)
820 (list `( eq major-mode (quote ,(car item)))
821 key
822 (concat (cdr item) " (%d)")))
823 (sort
824 (let ((mode-list nil))
825 (dolist (buffer (cdr (buffer-list)))
826 (with-current-buffer buffer
827 (when (and (not (msb-invisible-buffer-p))
828 (not (assq major-mode mode-list)))
829 (push (cons major-mode
830 (format-mode-line mode-name nil nil buffer))
831 mode-list))))
832 mode-list)
833 (lambda (item1 item2)
834 (string< (cdr item1) (cdr item2)))))))
835
836 (defun msb--most-recently-used-menu (max-buffer-name-length)
837 "Return a list for the most recently used buffers.
838 It takes the form ((TITLE . BUFFER-LIST)...)."
839 (when (and (numberp msb-display-most-recently-used)
840 (> msb-display-most-recently-used 0))
841 (let* ((buffers (cdr (buffer-list)))
842 (most-recently-used
843 (loop with n = 0
844 for buffer in buffers
845 if (with-current-buffer buffer
846 (and (not (msb-invisible-buffer-p))
847 (not (eq major-mode 'dired-mode))))
848 collect (with-current-buffer buffer
849 (cons (funcall msb-item-handling-function
850 buffer
851 max-buffer-name-length)
852 buffer))
853 and do (incf n)
854 until (>= n msb-display-most-recently-used))))
855 (cons (if (stringp msb-most-recently-used-title)
856 (format msb-most-recently-used-title
857 (length most-recently-used))
858 (signal 'wrong-type-argument (list msb-most-recently-used-title)))
859 most-recently-used))))
860
861 (defun msb--create-buffer-menu-2 ()
862 (let ((max-buffer-name-length 0)
863 file-buffers
864 function-info-vector)
865 ;; Calculate the longest buffer name.
866 (dolist (buffer (buffer-list))
867 (when (or msb-display-invisible-buffers-p
868 (not (msb-invisible-buffer-p)))
869 (setq max-buffer-name-length
870 (max max-buffer-name-length (length (buffer-name buffer))))))
871 ;; Make a list with elements of type
872 ;; (BUFFER-LIST-VARIABLE
873 ;; CONDITION
874 ;; MENU-SORT-KEY
875 ;; MENU-TITLE
876 ;; ITEM-HANDLER
877 ;; SORTER)
878 ;; Uses "function-global" variables:
879 ;; function-info-vector
880 (setq function-info-vector
881 (apply (function vector)
882 (mapcar (function msb--create-function-info)
883 (append msb-menu-cond (msb--mode-menu-cond)))))
884 ;; Split the buffer-list into several lists; one list for each
885 ;; criteria. This is the most critical part with respect to time.
886 (dolist (buffer (buffer-list))
887 (cond ((and msb-files-by-directory
888 (buffer-file-name buffer)
889 ;; exclude ange-ftp buffers
890 ;;(not (string-match "\\/[^/:]+:"
891 ;; (buffer-file-name buffer)))
892 )
893 (push buffer file-buffers))
894 (t
895 (msb--choose-menu buffer
896 function-info-vector
897 max-buffer-name-length))))
898 (when file-buffers
899 (setq file-buffers
900 (mapcar (lambda (buffer-list)
901 (list* msb-files-by-directory-sort-key
902 (car buffer-list)
903 (sort
904 (mapcar (lambda (buffer)
905 (cons (with-current-buffer buffer
906 (funcall
907 msb-item-handling-function
908 buffer
909 max-buffer-name-length))
910 buffer))
911 (cdr buffer-list))
912 (lambda (item1 item2)
913 (string< (car item1) (car item2))))))
914 (msb--choose-file-menu file-buffers))))
915 ;; Now make the menu - a list of (TITLE . BUFFER-LIST)
916 (let* (menu
917 (most-recently-used
918 (msb--most-recently-used-menu max-buffer-name-length))
919 (others (nconc file-buffers
920 (loop for elt
921 across function-info-vector
922 for value = (msb--create-sort-item elt)
923 if value collect value))))
924 (setq menu
925 (mapcar 'cdr ;Remove the SORT-KEY
926 ;; Sort the menus - not the items.
927 (msb--add-separators
928 (sort
929 ;; Get a list of (SORT-KEY TITLE . BUFFER-LIST)
930 ;; Also sorts the items within the menus.
931 (if (cdr most-recently-used)
932 (cons
933 ;; Add most recent used buffers
934 (cons msb-most-recently-used-sort-key
935 most-recently-used)
936 others)
937 others)
938 (lambda (elt1 elt2)
939 (< (car elt1) (car elt2)))))))
940 ;; Now make it a keymap menu
941 (append
942 '(keymap "Select Buffer")
943 (msb--make-keymap-menu menu)
944 (when msb-separator-diff
945 (list (list 'separator "--")))
946 (list (cons 'toggle
947 (cons
948 (if msb-files-by-directory
949 "*Files by type*"
950 "*Files by directory*")
951 'msb--toggle-menu-type)))))))
952
953 (defun msb--create-buffer-menu ()
954 (save-match-data
955 (save-excursion
956 (msb--create-buffer-menu-2))))
957
958 (defun msb--toggle-menu-type ()
959 "Multi-purpose function for selecting a buffer with the mouse."
960 (interactive)
961 (setq msb-files-by-directory (not msb-files-by-directory))
962 ;; This gets a warning, but it is correct,
963 ;; because this file redefines menu-bar-update-buffers.
964 (msb-menu-bar-update-buffers t))
965
966 (defun mouse-select-buffer (event)
967 "Pop up several menus of buffers, for selection with the mouse.
968 Returns the selected buffer or nil if no buffer is selected.
969
970 The way the buffers are split is conveniently handled with the
971 variable `msb-menu-cond'."
972 ;; Popup the menu and return the selected buffer.
973 (when (or msb--error
974 (not msb--last-buffer-menu)
975 (not (fboundp 'frame-or-buffer-changed-p))
976 (frame-or-buffer-changed-p))
977 (setq msb--error nil)
978 (setq msb--last-buffer-menu (msb--create-buffer-menu)))
979 (let ((position event)
980 choice)
981 (when (and (fboundp 'posn-x-y)
982 (fboundp 'posn-window))
983 (let ((posX (car (posn-x-y (event-start event))))
984 (posY (cdr (posn-x-y (event-start event))))
985 (posWind (posn-window (event-start event))))
986 ;; adjust position
987 (setq posX (- posX (funcall msb-horizontal-shift-function))
988 position (list (list posX posY) posWind))))
989 ;; Popup the menu
990 (setq choice (x-popup-menu position msb--last-buffer-menu))
991 (cond
992 ((eq (car choice) 'toggle)
993 ;; Bring up the menu again with type toggled.
994 (msb--toggle-menu-type)
995 (mouse-select-buffer event))
996 ((and (numberp (car choice))
997 (null (cdr choice)))
998 (let ((msb--last-buffer-menu (nthcdr 2 (assq (car choice)
999 msb--last-buffer-menu))))
1000 (mouse-select-buffer event)))
1001 ((while (numberp (car choice))
1002 (setq choice (cdr choice))))
1003 ((and (stringp (car choice))
1004 (null (cdr choice)))
1005 (car choice))
1006 ((null choice)
1007 choice)
1008 (t
1009 (error "Unknown form for buffer: %s" choice)))))
1010
1011 ;; Add separators
1012 (defun msb--add-separators (sorted-list)
1013 (if (or (not msb-separator-diff)
1014 (not (numberp msb-separator-diff)))
1015 sorted-list
1016 (let ((last-key nil))
1017 (apply #'nconc
1018 (mapcar
1019 (lambda (item)
1020 (cond
1021 ((and msb-separator-diff
1022 last-key
1023 (> (- (car item) last-key)
1024 msb-separator-diff))
1025 (setq last-key (car item))
1026 (list (cons last-key 'separator)
1027 item))
1028 (t
1029 (setq last-key (car item))
1030 (list item))))
1031 sorted-list)))))
1032
1033 (defun msb--split-menus-2 (list mcount result)
1034 (cond
1035 ((> (length list) msb-max-menu-items)
1036 (let ((count 0)
1037 sub-name
1038 (tmp-list nil))
1039 (while (< count msb-max-menu-items)
1040 (push (pop list) tmp-list)
1041 (incf count))
1042 (setq tmp-list (nreverse tmp-list))
1043 (setq sub-name (concat (car (car tmp-list)) "..."))
1044 (push (nconc (list mcount sub-name
1045 'keymap sub-name)
1046 tmp-list)
1047 result))
1048 (msb--split-menus-2 list (1+ mcount) result))
1049 ((null result)
1050 list)
1051 (t
1052 (let (sub-name)
1053 (setq sub-name (concat (car (car list)) "..."))
1054 (push (nconc (list mcount sub-name 'keymap sub-name)
1055 list)
1056 result))
1057 (nreverse result))))
1058
1059 (defun msb--split-menus (list)
1060 (if (and (integerp msb-max-menu-items)
1061 (> msb-max-menu-items 0))
1062 (msb--split-menus-2 list 0 nil)
1063 list))
1064
1065 (defun msb--make-keymap-menu (raw-menu)
1066 (let ((end (cons '(nil) 'menu-bar-select-buffer))
1067 (mcount 0))
1068 (mapcar
1069 (lambda (sub-menu)
1070 (cond
1071 ((eq 'separator sub-menu)
1072 (list 'separator "--"))
1073 (t
1074 (let ((buffers (mapcar (lambda (item)
1075 (cons (buffer-name (cdr item))
1076 (cons (car item) end)))
1077 (cdr sub-menu))))
1078 (nconc (list (incf mcount) (car sub-menu)
1079 'keymap (car sub-menu))
1080 (msb--split-menus buffers))))))
1081 raw-menu)))
1082
1083 (defun msb-menu-bar-update-buffers (&optional arg)
1084 "A re-written version of `menu-bar-update-buffers'."
1085 ;; If user discards the Buffers item, play along.
1086 (when (and (lookup-key (current-global-map) [menu-bar buffer])
1087 (or (not (fboundp 'frame-or-buffer-changed-p))
1088 (frame-or-buffer-changed-p)
1089 arg))
1090 (let ((frames (frame-list))
1091 buffers-menu frames-menu)
1092 ;; Make the menu of buffers proper.
1093 (setq msb--last-buffer-menu (msb--create-buffer-menu))
1094 ;; Skip the `keymap' symbol.
1095 (setq buffers-menu (cdr msb--last-buffer-menu))
1096 ;; Make a Frames menu if we have more than one frame.
1097 (when (cdr frames)
1098 (let* ((frame-length (length frames))
1099 (f-title (format "Frames (%d)" frame-length)))
1100 ;; List only the N most recently selected frames
1101 (when (and (integerp msb-max-menu-items)
1102 (> msb-max-menu-items 1)
1103 (> frame-length msb-max-menu-items))
1104 (setcdr (nthcdr msb-max-menu-items frames) nil))
1105 (setq frames-menu
1106 (nconc
1107 (list 'frame f-title '(nil) 'keymap f-title)
1108 (mapcar
1109 (lambda (frame)
1110 (nconc
1111 (list (frame-parameter frame 'name)
1112 (frame-parameter frame 'name)
1113 (cons nil nil))
1114 'menu-bar-select-frame))
1115 frames)))))
1116 (setcdr global-buffers-menu-map
1117 (if (and buffers-menu frames-menu)
1118 ;; Combine Frame and Buffers menus with separator between
1119 (nconc (list "Buffers and Frames" frames-menu
1120 (and msb-separator-diff '(separator "--")))
1121 (cdr buffers-menu))
1122 buffers-menu)))))
1123
1124 ;; Snarf current bindings of `mouse-buffer-menu' (normally
1125 ;; C-down-mouse-1).
1126 (defvar msb-mode-map
1127 (let ((map (make-sparse-keymap "Msb")))
1128 (define-key map [remap mouse-buffer-menu] 'msb)
1129 map))
1130
1131 ;;;###autoload
1132 (define-minor-mode msb-mode
1133 "Toggle Msb mode.
1134 With arg, turn Msb mode on if and only if arg is positive.
1135 This mode overrides the binding(s) of `mouse-buffer-menu' to provide a
1136 different buffer menu using the function `msb'."
1137 :global t :group 'msb
1138 (if msb-mode
1139 (progn
1140 (add-hook 'menu-bar-update-hook 'msb-menu-bar-update-buffers)
1141 (remove-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
1142 (msb-menu-bar-update-buffers t))
1143 (remove-hook 'menu-bar-update-hook 'msb-menu-bar-update-buffers)
1144 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
1145 (menu-bar-update-buffers t)))
1146
1147 (defun msb-unload-function ()
1148 "Unload the Msb library."
1149 (msb-mode -1)
1150 ;; continue standard unloading
1151 nil)
1152
1153 (provide 'msb)
1154 (eval-after-load "msb" '(run-hooks 'msb-after-load-hook 'msb-after-load-hooks))
1155
1156 ;;; msb.el ends here