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