Merge: fix two m4/gnulib-*.m4 file names that clashed under MS-DOS
[bpt/emacs.git] / lisp / msb.el
CommitLineData
55535639 1;;; msb.el --- customizable buffer-selection with multiple menus
b578f267 2
73b0cd50 3;; Copyright (C) 1993-1995, 1997-2011 Free Software Foundation, Inc.
b578f267 4
17df99ea 5;; Author: Lars Lindberg <lars.lindberg@home.se>
eed30659 6;; Maintainer: FSF
b9a5a6af 7;; Created: 8 Oct 1993
3cfa0ee9 8;; Lindberg's last update version: 3.34
0eb3b336 9;; Keywords: mouse buffer menu
b578f267
EN
10
11;; This file is part of GNU Emacs.
12
eb3fa2cf 13;; GNU Emacs is free software: you can redistribute it and/or modify
b9a5a6af 14;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
b578f267
EN
17
18;; GNU Emacs is distributed in the hope that it will be useful,
b9a5a6af
RS
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.
b578f267 22
b9a5a6af 23;; You should have received a copy of the GNU General Public License
eb3fa2cf 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b9a5a6af 25
b9a5a6af 26;;; Commentary:
b578f267 27
b9a5a6af
RS
28;; Purpose of this package:
29;; 1. Offer a function for letting the user choose buffer,
30;; not necessarily for switching to it.
aade135d
DL
31;; 2. Make a better mouse-buffer-menu. This is done as a global
32;; minor mode, msb-mode.
b9a5a6af
RS
33;;
34;; Customization:
2e6286be
RS
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.
b9a5a6af
RS
38;; There are some constants for you to try here:
39;; msb--few-menus
40;; msb--very-many-menus (default)
f1180544 41;;
2e6286be
RS
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'.
f1180544 45;;
2e6286be
RS
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
b9a5a6af
RS
48;; sorting - you will get latest used buffer first.
49;;
2e6286be 50;; Also check out the variable `msb-display-invisible-buffers-p'.
b9a5a6af
RS
51
52;; Known bugs:
4aa4849b 53;; - Files-by-directory
fd46fd17 54;; + No possibility to show client/changed buffers separately.
3cfa0ee9 55;; + All file buffers only appear in a file sub-menu, they will
fd46fd17
RS
56;; for instance not appear in the Mail sub-menu.
57
b9a5a6af 58;; Future enhancements:
b9a5a6af 59
b9a5a6af 60;;; Thanks goes to
fd46fd17
RS
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>
5762abec 73;; Richard Stallman <rms@gnu.org>
fd46fd17 74;; Steve Fisk <fisk@medved.bowdoin.edu>
b9a5a6af 75
492bd758
DL
76;; This version turned into a global minor mode and subsequently
77;; hacked on by Dave Love.
b9a5a6af
RS
78;;; Code:
79
492bd758 80(eval-when-compile (require 'cl))
b9a5a6af
RS
81
82;;;
2e6286be
RS
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.
b9a5a6af
RS
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)")
a4a49c21
DL
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)))
b9a5a6af
RS
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
0eb3b336 146 "Invisible buffers (%d)")
b9a5a6af
RS
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)
3cfa0ee9 155 5030
b9a5a6af
RS
156 "Manuals (%d)")
157 ((eq major-mode 'w3-mode)
3cfa0ee9 158 5020
b9a5a6af 159 "WWW (%d)")
a4a49c21
DL
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
b9a5a6af 165 gnus-browse-killed-mode)))
3cfa0ee9 166 5010
b9a5a6af
RS
167 "Mail (%d)")
168 ;; Catchup for all non-file buffers
169 ((and (not buffer-file-name)
170 'no-multi)
3cfa0ee9 171 5099
b9a5a6af
RS
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
8e2c8d3e 185 "LaTeX Files (%d)")
b9a5a6af
RS
186 ('no-multi
187 3099
188 "Other files (%d)")))
189
b9a5a6af
RS
190;;;
191;;; Customizable variables
192;;;
193
3cfa0ee9
SE
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)
eed30659 202 (if (and (featurep 'msb) msb-mode)
3cfa0ee9
SE
203 ;; wait until package has been loaded before bothering to update
204 ;; the buffer lists.
eed30659 205 (msb-menu-bar-update-buffers t)))
3cfa0ee9
SE
206
207(defcustom msb-menu-cond msb--very-many-menus
9201cc28 208 "List of criteria for splitting the mouse buffer menu.
3cfa0ee9
SE
209The elements in the list should be of this type:
210 (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).
211
212When making the split, the buffers are tested one by one against the
aade135d 213CONDITION, just like a Lisp cond: When hitting a true condition, the
3cfa0ee9
SE
214other criteria are *not* tested and the buffer name will appear in the
215menu with the menu-title corresponding to the true condition.
216
217If the condition returns the symbol `multi', then the buffer will be
218added 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
220to any other menu.
221
222During this test, the buffer in question is the current buffer, and
223the test is surrounded by calls to `save-excursion' and
224`save-match-data'.
225
226The categories are sorted by MENU-SORT-KEY. Smaller keys are on top.
c11bf648 227A value of nil means don't display this menu.
3cfa0ee9
SE
228
229MENU-TITLE is really a format. If you add %d in it, the %d is
230replaced with the number of items in that menu.
231
20ba690c
JB
232ITEM-HANDLING-FN is optional. If it is supplied and is a function,
233then it is used for displaying the items in that particular buffer
3cfa0ee9
SE
234menu, otherwise the function pointed out by
235`msb-item-handling-function' is used.
236
20ba690c 237ITEM-SORT-FN is also optional.
3cfa0ee9
SE
238If it is not supplied, the function pointed out by
239`msb-item-sort-function' is used.
240If it is nil, then no sort takes place and the buffers are presented
241in least-recently-used order.
242If it is t, then no sort takes place and the buffers are presented in
243most-recently-used order.
244If it is supplied and non-nil and not t than it is used for sorting
245the items in that particular buffer menu.
246
247Note1: There should always be a `catch-all' as last element, in this
248list. That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
249Note2: A buffer menu appears only if it has at least one buffer in it.
250Note3: If you have a CONDITION that can't be evaluated you will get an
251error every time you do \\[msb]."
252 :type `(choice (const :tag "long" :value ,msb--very-many-menus)
25bb0401
GM
253 (const :tag "short" :value ,msb--few-menus)
254 (sexp :tag "user"))
3cfa0ee9
SE
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
f9e9ac1d
DN
262 :group 'msb
263 :version "20.3")
3cfa0ee9
SE
264
265(defcustom msb-separator-diff 100
9201cc28 266 "Non-nil means use separators.
0eb3b336 267The separators will appear between all menus that have a sorting key
3cfa0ee9
SE
268that differs by this value or more."
269 :type '(choice integer (const nil))
270 :set 'msb-custom-set
271 :group 'msb)
b9a5a6af
RS
272
273(defvar msb-files-by-directory-sort-key 0
0eb3b336 274 "*The sort key for files sorted by directory.")
b9a5a6af 275
3cfa0ee9 276(defcustom msb-max-menu-items 15
9201cc28 277 "The maximum number of items in a menu.
0eb3b336 278If this variable is set to 15 for instance, then the submenu will be
20ba690c 279split up in minor parts, 15 items each. A value of nil means no limit."
3cfa0ee9
SE
280 :type '(choice integer (const nil))
281 :set 'msb-custom-set
282 :group 'msb)
b9a5a6af 283
3cfa0ee9 284(defcustom msb-max-file-menu-items 10
9201cc28 285 "The maximum number of items from different directories.
b9a5a6af 286
2e6286be 287When the menu is of type `file by directory', this is the maximum
6331da4b 288number of buffers that are clumped together from different
b9a5a6af
RS
289directories.
290
4aa4849b
RS
291Set this to 1 if you want one menu per directory instead of clumping
292them together.
293
3cfa0ee9
SE
294If the value is not a number, then the value 10 is used."
295 :type 'integer
296 :set 'msb-custom-set
297 :group 'msb)
b9a5a6af 298
3cfa0ee9 299(defcustom msb-most-recently-used-sort-key -1010
9201cc28 300 "Where should the menu with the most recently used buffers be placed?"
3cfa0ee9
SE
301 :type 'integer
302 :set 'msb-custom-set
303 :group 'msb)
b9a5a6af 304
3cfa0ee9 305(defcustom msb-display-most-recently-used 15
9201cc28 306 "How many buffers should be in the most-recently-used menu.
3cfa0ee9
SE
307No 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)"
9201cc28 313 "The title for the most-recently-used menu."
3cfa0ee9
SE
314 :type 'string
315 :set 'msb-custom-set
316 :group 'msb)
f1180544 317
b9a5a6af 318(defvar msb-horizontal-shift-function '(lambda () 0)
0eb3b336 319 "*Function that specifies how many pixels to shift the top menu leftwards.")
b9a5a6af 320
3cfa0ee9 321(defcustom msb-display-invisible-buffers-p nil
9201cc28 322 "Show invisible buffers or not.
b9a5a6af 323Non-nil means that the buffer menu should include buffers that have
3cfa0ee9
SE
324names that starts with a space character."
325 :type 'boolean
326 :set 'msb-custom-set
327 :group 'msb)
b9a5a6af
RS
328
329(defvar msb-item-handling-function 'msb-item-handler
330 "*The appearance of a buffer menu.
331
332The default function to call for handling the appearance of a menu
20ba690c 333item. It should take two arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
b9a5a6af 334where the latter is the max length of all buffer names.
4aa4849b
RS
335
336The function should return the string to use in the menu.
337
0eb3b336
RS
338When the function is called, BUFFER is the current buffer. This
339function is called for items in the variable `msb-menu-cond' that have
340nil as ITEM-HANDLING-FUNCTION. See `msb-menu-cond' for more
b9a5a6af
RS
341information.")
342
3cfa0ee9 343(defcustom msb-item-sort-function 'msb-sort-by-name
9201cc28 344 "The order of items in a buffer menu.
0eb3b336 345
b9a5a6af 346The default function to call for handling the order of items in a menu
0eb3b336
RS
347item. This function is called like a sort function. The items look
348like (ITEM-NAME . BUFFER).
349
b9a5a6af
RS
350ITEM-NAME is the name of the item that will appear in the menu.
351BUFFER is the buffer, this is not necessarily the current buffer.
352
3cfa0ee9
SE
353Set 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
a4a49c21 358 :group 'msb)
f1180544 359
3cfa0ee9 360(defcustom msb-files-by-directory nil
9201cc28 361 "Non-nil means that files should be sorted by directory.
aade135d 362This is instead of the groups in `msb-menu-cond'."
3cfa0ee9
SE
363 :type 'boolean
364 :set 'msb-custom-set
365 :group 'msb)
b9a5a6af 366
3392cf05
SM
367(defcustom msb-after-load-hook nil
368 "Hook run after the msb package has been loaded."
3cfa0ee9
SE
369 :type 'hook
370 :set 'msb-custom-set
371 :group 'msb)
b9a5a6af
RS
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;;;
4aa4849b 384;;; Some example function to be used for `msb-item-handling-function'.
b9a5a6af
RS
385;;;
386(defun msb-item-handler (buffer &optional maxbuf)
387 "Create one string item, concerning BUFFER, for the buffer menu.
388The item looks like:
389*% <buffer-name>
2e6286be
RS
390The `*' appears only if the buffer is marked as modified.
391The `%' appears only if the buffer is read-only.
b9a5a6af
RS
392Optional 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
2e6286be
RS
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.
b9a5a6af
RS
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
2e6286be 409 (error "Unknown type of `dired-directory' in buffer %s"
b9a5a6af
RS
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.
414The item looks like:
415*% <buffer-name>
2e6286be
RS
416The `*' appears only if the buffer is marked as modified.
417The `%' appears only if the buffer is read-only.
b9a5a6af
RS
418Optional 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.
426The item looks like:
427<buffer-name> *%# <file-name>
2e6286be
RS
428The `*' appears only if the buffer is marked as modified.
429The `%' appears only if the buffer is read-only.
430The `#' appears only version control file (SCCS/RCS)."
b9a5a6af
RS
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;;;
4aa4849b 439;;; Some example function to be used for `msb-item-sort-function'.
b9a5a6af
RS
440;;;
441(defun msb-sort-by-name (item1 item2)
aade135d
DL
442 "Sort the items ITEM1 and ITEM2 by their `buffer-name'.
443An item looks like (NAME . BUFFER)."
b9a5a6af
RS
444 (string-lessp (buffer-name (cdr item1))
445 (buffer-name (cdr item2))))
446
447
448(defun msb-sort-by-directory (item1 item2)
aade135d 449 "Sort the items ITEM1 and ITEM2 by directory name. Made for dired.
b9a5a6af 450An item look like (NAME . BUFFER)."
7ccc8f70
SM
451 (string-lessp (with-current-buffer (cdr item1)
452 (msb--dired-directory))
453 (with-current-buffer (cdr item2)
454 (msb--dired-directory))))
b9a5a6af
RS
455
456;;;
457;;; msb
458;;;
459;;; This function can be used instead of (mouse-buffer-menu EVENT)
460;;; function in "mouse.el".
0eb3b336 461;;;
b9a5a6af
RS
462(defun msb (event)
463 "Pop up several menus of buffers for selection with the mouse.
464This command switches buffers in the window that you clicked on, and
465selects that window.
466
2e6286be
RS
467See the function `mouse-select-buffer' and the variable
468`msb-menu-cond' for more information about how the menus are split."
b9a5a6af 469 (interactive "e")
fd46fd17 470 (let ((old-window (selected-window))
809b6e98
CY
471 (window (posn-window (event-start event)))
472 early-release)
fd46fd17 473 (unless (framep window) (select-window window))
809b6e98
CY
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)))
fd46fd17
RS
477 (let ((buffer (mouse-select-buffer event)))
478 (if buffer
479 (switch-to-buffer buffer)
809b6e98
CY
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)))
b9a5a6af
RS
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.
492If the argument is left out or nil, then the current buffer is considered."
493 (and (> (length (buffer-name buffer)) 0)
e665a469 494 (eq ?\s (aref (buffer-name buffer) 0))))
b9a5a6af 495
7612d61a 496(defun msb--strip-dir (dir)
eed30659 497 "Strip one hierarchy level from the end of DIR."
862aacbf 498 (file-name-directory (directory-file-name dir)))
b9a5a6af
RS
499
500;; Create an alist with all buffers from LIST that lies under the same
965440e6
KS
501;; directory will be in the same item as the directory name.
502;; ((DIR1 . (BUFFER-1 BUFFER-2 ...)) (DIR2 . (BUFFER-K BUFFER-K+1...)) ...)
b9a5a6af
RS
503(defun msb--init-file-alist (list)
504 (let ((buffer-alist
0eb3b336 505 ;; Make alist that looks like
965440e6
KS
506 ;; ((DIR-1 BUFFER-1) (DIR-2 BUFFER-2) ...)
507 ;; sorted on DIR-x
a4a49c21
DL
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))))))
965440e6 519 ;; Now clump buffers together that have the same directory name
b9a5a6af 520 ;; Make alist that looks like
965440e6
KS
521 ;; ((DIR1 . (BUFFER-1 BUFFER-2 ...)) (DIR2 . (BUFFER-K)) ...)
522 (let ((dir nil)
0eb3b336
RS
523 (buffers nil))
524 (nconc
a4a49c21
DL
525 (apply
526 #'nconc
527 (mapcar (lambda (item)
528 (cond
965440e6
KS
529 ((equal dir (car item))
530 ;; The same dir as earlier:
531 ;; Add to current list of buffers.
a4a49c21
DL
532 (push (cdr item) buffers)
533 ;; This item should not be added to list
534 nil)
535 (t
965440e6
KS
536 ;; New dir
537 (let ((result (and dir (cons dir buffers))))
538 (setq dir (car item))
a4a49c21
DL
539 (setq buffers (list (cdr item)))
540 ;; Add the last result the list.
541 (and result (list result))))))
542 buffer-alist))
0eb3b336 543 ;; Add the last result to the list
965440e6 544 (list (cons dir buffers))))))
b9a5a6af 545
965440e6 546(defun msb--format-title (top-found-p dir number-of-items)
eed30659 547 "Format a suitable title for the menu item."
492bd758 548 (format (if top-found-p "%s... (%d)" "%s (%d)")
965440e6 549 (abbreviate-file-name dir) number-of-items))
0eb3b336 550
c549c1bf
RS
551;; Variables for debugging.
552(defvar msb--choose-file-menu-list)
553(defvar msb--choose-file-menu-arg-list)
0eb3b336 554
b9a5a6af 555(defun msb--choose-file-menu (list)
eed30659 556 "Choose file-menu with respect to directory for every buffer in LIST."
c549c1bf 557 (setq msb--choose-file-menu-arg-list list)
b9a5a6af
RS
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)
965440e6
KS
564 (last-dir nil)
565 first rest dir buffers old-dir)
0eb3b336
RS
566 ;; Prepare for looping over all items in buffer-alist
567 (setq first (car buffer-alist)
568 rest (cdr buffer-alist)
965440e6 569 dir (car first)
0eb3b336 570 buffers (cdr first))
a4a49c21 571 (setq msb--choose-file-menu-list (copy-sequence rest))
0eb3b336
RS
572 ;; This big loop tries to clump buffers together that have a
573 ;; similar name. Remember that buffer-alist is sorted based on the
965440e6 574 ;; directory name of the buffers' visited files.
b9a5a6af
RS
575 (while rest
576 (let ((found-p nil)
577 (tmp-rest rest)
7ccc8f70 578 item)
b9a5a6af 579 (setq item (car tmp-rest))
965440e6
KS
580 ;; Clump together the "rest"-buffers that have a dir that is
581 ;; a subdir of the current one.
b9a5a6af
RS
582 (while (and tmp-rest
583 (<= (length buffers) max-clumped-together)
965440e6 584 (>= (length (car item)) (length dir))
b9b37d2b
DL
585 ;; `completion-ignore-case' seems to default to t
586 ;; on the systems with case-insensitive file names.
965440e6
KS
587 (eq t (compare-strings dir 0 nil
588 (car item) 0 (length dir)
b9b37d2b 589 completion-ignore-case)))
b9a5a6af 590 (setq found-p t)
0eb3b336
RS
591 (setq buffers (append buffers (cdr item))) ;nconc is faster than append
592 (setq tmp-rest (cdr tmp-rest)
593 item (car tmp-rest)))
b9a5a6af
RS
594 (cond
595 ((> (length buffers) max-clumped-together)
0eb3b336
RS
596 ;; Oh, we failed. Too many buffers clumped together.
597 ;; Just use the original ones for the result.
965440e6 598 (setq last-dir (car first))
0eb3b336
RS
599 (push (cons (msb--format-title top-found-p
600 (car first)
601 (length (cdr first)))
602 (cdr first))
603 final-list)
4aa4849b 604 (setq top-found-p nil)
b9a5a6af 605 (setq first (car rest)
0eb3b336 606 rest (cdr rest)
965440e6 607 dir (car first)
b9a5a6af
RS
608 buffers (cdr first)))
609 (t
0eb3b336
RS
610 ;; The first pass of clumping together worked out, go ahead
611 ;; with this result.
b9a5a6af
RS
612 (when found-p
613 (setq top-found-p t)
965440e6 614 (setq first (cons dir buffers)
b9a5a6af 615 rest tmp-rest))
0eb3b336
RS
616 ;; Now see if we can clump more buffers together if we go up
617 ;; one step in the file hierarchy.
965440e6 618 ;; If dir isn't changed by msb--strip-dir, we are looking
3cfa0ee9 619 ;; at the machine name component of an ange-ftp filename.
965440e6
KS
620 (setq old-dir dir)
621 (setq dir (msb--strip-dir dir)
b9a5a6af 622 buffers (cdr first))
965440e6
KS
623 (if (equal old-dir dir)
624 (setq last-dir dir))
625 (when (and last-dir
626 (or (and (>= (length dir) (length last-dir))
b9b37d2b 627 (eq t (compare-strings
965440e6
KS
628 last-dir 0 nil dir 0
629 (length last-dir)
b9b37d2b 630 completion-ignore-case)))
965440e6 631 (and (< (length dir) (length last-dir))
b9b37d2b 632 (eq t (compare-strings
965440e6 633 dir 0 nil last-dir 0 (length dir)
b9b37d2b 634 completion-ignore-case)))))
0eb3b336
RS
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)
4aa4849b 643 (setq top-found-p nil)
b9a5a6af 644 (setq first (car rest)
0eb3b336 645 rest (cdr rest)
965440e6 646 dir (car first)
0eb3b336
RS
647 buffers (cdr first)))))))
648 ;; Now take care of the last item.
3cfa0ee9
SE
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))
4aa4849b 655 (setq top-found-p nil)
b9a5a6af
RS
656 (nreverse final-list)))
657
b9a5a6af 658(defun msb--create-function-info (menu-cond-elt)
eed30659
DL
659 "Create a vector from an element MENU-COND-ELT of `msb-menu-cond'.
660This takes the form:
20ba690c 661\[BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER]
eed30659 662See `msb-menu-cond' for a description of its elements."
b9a5a6af
RS
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)
2e6286be 674 (eq tmp-s t))
a4a49c21 675 tmp-s
b9a5a6af
RS
676 msb-item-sort-function)))
677 (when (< (length menu-cond-elt) 3)
aade135d 678 (error "Wrong format of msb-menu-cond"))
b9a5a6af
RS
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))
2e6286be 685 (not (eq tmp-s t)))
b9a5a6af 686 (signal 'invalid-function (list tmp-s)))
2e6286be 687 (set list-symbol ())
b9a5a6af
RS
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
3cfa0ee9 697;; pulled out merely to make the code somewhat clearer. The indentation
b9a5a6af
RS
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))
4aa4849b 712 t))
b9a5a6af
RS
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
b9a5a6af 721(defun msb--add-to-menu (buffer function-info max-buffer-name-length)
eed30659
DL
722 "Add BUFFER to the menu depicted by FUNCTION-INFO.
723All side-effects. Adds an element of form (BUFFER-TITLE . BUFFER)
20ba690c 724to the buffer-list variable in FUNCTION-INFO."
b9a5a6af
RS
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)))))
f1180544 733
b9a5a6af 734(defsubst msb--choose-menu (buffer function-info-vector max-buffer-name-length)
eed30659
DL
735 "Select the appropriate menu for BUFFER."
736 ;; This is all side-effects, folks!
737 ;; This should be optimized.
b9a5a6af
RS
738 (unless (and (not msb-display-invisible-buffers-p)
739 (msb-invisible-buffer-p buffer))
740 (condition-case nil
7ccc8f70 741 (with-current-buffer buffer
2e6286be 742 ;; Menu found. Add to this menu
b2eb3813
GM
743 (dolist (info (msb--collect function-info-vector))
744 (msb--add-to-menu buffer info max-buffer-name-length)))
b9a5a6af
RS
745 (error (unless msb--error
746 (setq msb--error
747 (format
2e6286be 748 "In msb-menu-cond, error for buffer `%s'."
b9a5a6af 749 (buffer-name buffer)))
76e4c0ba 750 (error "%s" msb--error))))))
b9a5a6af 751
b9a5a6af 752(defun msb--create-sort-item (function-info)
eed30659 753 "Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the buffer-list is empty."
b9a5a6af
RS
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
0eb3b336 759 (cons sort-key
b9a5a6af
RS
760 (cons (format (aref function-info 3) ;MENU-TITLE
761 (length buffer-list))
762 (cond
763 ((null sorter)
764 buffer-list)
2e6286be 765 ((eq sorter t)
b9a5a6af
RS
766 (nreverse buffer-list))
767 (t
768 (sort buffer-list sorter))))))))))
769
3cfa0ee9 770(defun msb--aggregate-alist (alist same-predicate sort-predicate)
eed30659
DL
771 "Return ALIST as a sorted, aggregated alist.
772
773In the result all items with the same car element (according to
774SAME-PREDICATE) are aggregated together. The alist is first sorted by
775SORT-PREDICATE.
776
777Example:
b2eb3813 778\(msb--aggregate-alist
eed30659
DL
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))))
783results in
b2eb3813 784\((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))"
3cfa0ee9 785 (when (not (null alist))
7ccc8f70 786 (let (same
3cfa0ee9
SE
787 tmp-old-car
788 tmp-same
789 (first-time-p t)
790 old-car)
791 (nconc
a4a49c21
DL
792 (apply #'nconc
793 (mapcar
794 (lambda (item)
3cfa0ee9
SE
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)
7ccc8f70
SM
811 (funcall sort-predicate
812 (car item1) (car item2))))))
3cfa0ee9
SE
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)")))
aade135d 823 (sort
3cfa0ee9 824 (let ((mode-list nil))
b2eb3813 825 (dolist (buffer (cdr (buffer-list)))
7ccc8f70 826 (with-current-buffer buffer
b2eb3813
GM
827 (when (and (not (msb-invisible-buffer-p))
828 (not (assq major-mode mode-list)))
48d33090
SM
829 (push (cons major-mode
830 (format-mode-line mode-name nil nil buffer))
b2eb3813 831 mode-list))))
3cfa0ee9
SE
832 mode-list)
833 (lambda (item1 item2)
834 (string< (cdr item1) (cdr item2)))))))
835
b9a5a6af 836(defun msb--most-recently-used-menu (max-buffer-name-length)
eed30659
DL
837 "Return a list for the most recently used buffers.
838It takes the form ((TITLE . BUFFER-LIST)...)."
4aa4849b
RS
839 (when (and (numberp msb-display-most-recently-used)
840 (> msb-display-most-recently-used 0))
fd46fd17
RS
841 (let* ((buffers (cdr (buffer-list)))
842 (most-recently-used
b9a5a6af 843 (loop with n = 0
fd46fd17 844 for buffer in buffers
7ccc8f70 845 if (with-current-buffer buffer
b9a5a6af
RS
846 (and (not (msb-invisible-buffer-p))
847 (not (eq major-mode 'dired-mode))))
7ccc8f70 848 collect (with-current-buffer buffer
b9a5a6af
RS
849 (cons (funcall msb-item-handling-function
850 buffer
851 max-buffer-name-length)
852 buffer))
853 and do (incf n)
4aa4849b 854 until (>= n msb-display-most-recently-used))))
b9a5a6af
RS
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.
b2eb3813
GM
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))))))
b9a5a6af
RS
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)
3cfa0ee9 883 (append msb-menu-cond (msb--mode-menu-cond)))))
b9a5a6af 884 ;; Split the buffer-list into several lists; one list for each
2e6286be 885 ;; criteria. This is the most critical part with respect to time.
b2eb3813
GM
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))))
b9a5a6af
RS
898 (when file-buffers
899 (setq file-buffers
3cfa0ee9 900 (mapcar (lambda (buffer-list)
7ccc8f70
SM
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))))
b9a5a6af 915 ;; Now make the menu - a list of (TITLE . BUFFER-LIST)
4aa4849b 916 (let* (menu
b9a5a6af
RS
917 (most-recently-used
918 (msb--most-recently-used-menu max-buffer-name-length))
0eb3b336 919 (others (nconc file-buffers
b9a5a6af 920 (loop for elt
4aa4849b
RS
921 across function-info-vector
922 for value = (msb--create-sort-item elt)
923 if value collect value))))
b9a5a6af
RS
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)
3cfa0ee9
SE
938 (lambda (elt1 elt2)
939 (< (car elt1) (car elt2)))))))
b9a5a6af
RS
940 ;; Now make it a keymap menu
941 (append
942 '(keymap "Select Buffer")
943 (msb--make-keymap-menu menu)
944 (when msb-separator-diff
0eb3b336
RS
945 (list (list 'separator "--")))
946 (list (cons 'toggle
b9a5a6af
RS
947 (cons
948 (if msb-files-by-directory
a4a49c21
DL
949 "*Files by type*"
950 "*Files by directory*")
951 'msb--toggle-menu-type)))))))
b9a5a6af 952
0b704e15 953(defun msb--create-buffer-menu ()
b9a5a6af
RS
954 (save-match-data
955 (save-excursion
956 (msb--create-buffer-menu-2))))
957
b9a5a6af 958(defun msb--toggle-menu-type ()
20ba690c 959 "Multi-purpose function for selecting a buffer with the mouse."
b9a5a6af
RS
960 (interactive)
961 (setq msb-files-by-directory (not msb-files-by-directory))
c549c1bf
RS
962 ;; This gets a warning, but it is correct,
963 ;; because this file redefines menu-bar-update-buffers.
eed30659 964 (msb-menu-bar-update-buffers t))
b9a5a6af
RS
965
966(defun mouse-select-buffer (event)
967 "Pop up several menus of buffers, for selection with the mouse.
968Returns the selected buffer or nil if no buffer is selected.
969
4aa4849b 970The way the buffers are split is conveniently handled with the
2e6286be 971variable `msb-menu-cond'."
b9a5a6af
RS
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)))
4aa4849b
RS
979 (let ((position event)
980 choice)
b9a5a6af
RS
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))))
4aa4849b 985 (posWind (posn-window (event-start event))))
b9a5a6af
RS
986 ;; adjust position
987 (setq posX (- posX (funcall msb-horizontal-shift-function))
988 position (list (list posX posY) posWind))))
1cc9a99e 989 ;; Popup the menu
4aa4849b 990 (setq choice (x-popup-menu position msb--last-buffer-menu))
b9a5a6af 991 (cond
4aa4849b
RS
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)))
122e29de 998 (let ((msb--last-buffer-menu (nthcdr 2 (assq (car choice)
a4a49c21 999 msb--last-buffer-menu))))
b9a5a6af 1000 (mouse-select-buffer event)))
4aa4849b
RS
1001 ((while (numberp (car choice))
1002 (setq choice (cdr choice))))
1003 ((and (stringp (car choice))
1004 (null (cdr choice)))
1005 (car choice))
dc3247b3
RS
1006 ((null choice)
1007 choice)
4aa4849b
RS
1008 (t
1009 (error "Unknown form for buffer: %s" choice)))))
3cfa0ee9 1010
b9a5a6af
RS
1011;; Add separators
1012(defun msb--add-separators (sorted-list)
a4a49c21
DL
1013 (if (or (not msb-separator-diff)
1014 (not (numberp msb-separator-diff)))
1015 sorted-list
b9a5a6af 1016 (let ((last-key nil))
a4a49c21
DL
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)))))
b9a5a6af 1032
4aa4849b
RS
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))
a4a49c21
DL
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))
4aa4849b
RS
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)) "..."))
a4a49c21
DL
1054 (push (nconc (list mcount sub-name 'keymap sub-name)
1055 list)
1056 result))
4aa4849b 1057 (nreverse result))))
4aa4849b 1058
3cfa0ee9
SE
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))
4aa4849b 1064
b9a5a6af
RS
1065(defun msb--make-keymap-menu (raw-menu)
1066 (let ((end (cons '(nil) 'menu-bar-select-buffer))
1067 (mcount 0))
1068 (mapcar
3cfa0ee9 1069 (lambda (sub-menu)
aade135d 1070 (cond
3cfa0ee9
SE
1071 ((eq 'separator sub-menu)
1072 (list 'separator "--"))
1073 (t
a4a49c21
DL
1074 (let ((buffers (mapcar (lambda (item)
1075 (cons (buffer-name (cdr item))
1076 (cons (car item) end)))
3cfa0ee9
SE
1077 (cdr sub-menu))))
1078 (nconc (list (incf mcount) (car sub-menu)
1079 'keymap (car sub-menu))
1080 (msb--split-menus buffers))))))
b9a5a6af
RS
1081 raw-menu)))
1082
eed30659
DL
1083(defun msb-menu-bar-update-buffers (&optional arg)
1084 "A re-written version of `menu-bar-update-buffers'."
b9a5a6af
RS
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))
fd46fd17 1090 (let ((frames (frame-list))
b9a5a6af 1091 buffers-menu frames-menu)
b9a5a6af
RS
1092 ;; Make the menu of buffers proper.
1093 (setq msb--last-buffer-menu (msb--create-buffer-menu))
7ccc8f70
SM
1094 ;; Skip the `keymap' symbol.
1095 (setq buffers-menu (cdr msb--last-buffer-menu))
b9a5a6af 1096 ;; Make a Frames menu if we have more than one frame.
fd46fd17
RS
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)
0b704e15 1102 (> msb-max-menu-items 1)
fd46fd17
RS
1103 (> frame-length msb-max-menu-items))
1104 (setcdr (nthcdr msb-max-menu-items frames) nil))
b9a5a6af 1105 (setq frames-menu
fd46fd17
RS
1106 (nconc
1107 (list 'frame f-title '(nil) 'keymap f-title)
1108 (mapcar
3cfa0ee9
SE
1109 (lambda (frame)
1110 (nconc
10df5051
RS
1111 (list (frame-parameter frame 'name)
1112 (frame-parameter frame 'name)
3cfa0ee9
SE
1113 (cons nil nil))
1114 'menu-bar-select-frame))
fd46fd17 1115 frames)))))
7ccc8f70 1116 (setcdr global-buffers-menu-map
b9a5a6af 1117 (if (and buffers-menu frames-menu)
fd46fd17 1118 ;; Combine Frame and Buffers menus with separator between
7ccc8f70 1119 (nconc (list "Buffers and Frames" frames-menu
0eb3b336 1120 (and msb-separator-diff '(separator "--")))
7ccc8f70
SM
1121 (cdr buffers-menu))
1122 buffers-menu)))))
b9a5a6af 1123
aade135d
DL
1124;; Snarf current bindings of `mouse-buffer-menu' (normally
1125;; C-down-mouse-1).
1126(defvar msb-mode-map
a4a49c21 1127 (let ((map (make-sparse-keymap "Msb")))
9103eeef 1128 (define-key map [remap mouse-buffer-menu] 'msb)
aade135d
DL
1129 map))
1130
1131;;;###autoload
3bdb5fb8 1132(define-minor-mode msb-mode
aade135d
DL
1133 "Toggle Msb mode.
1134With arg, turn Msb mode on if and only if arg is positive.
1135This mode overrides the binding(s) of `mouse-buffer-menu' to provide a
1136different buffer menu using the function `msb'."
329ffac0 1137 :global t :group 'msb
aade135d 1138 (if msb-mode
eed30659
DL
1139 (progn
1140 (add-hook 'menu-bar-update-hook 'msb-menu-bar-update-buffers)
0f6d89c4
GM
1141 (remove-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
1142 (msb-menu-bar-update-buffers t))
eed30659 1143 (remove-hook 'menu-bar-update-hook 'msb-menu-bar-update-buffers)
0f6d89c4
GM
1144 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
1145 (menu-bar-update-buffers t)))
aade135d 1146
0b704e15
JB
1147(defun msb-unload-function ()
1148 "Unload the Msb library."
1149 (msb-mode -1)
1150 ;; continue standard unloading
1151 nil)
a4a49c21 1152
b9a5a6af 1153(provide 'msb)
00b1e7a1 1154(eval-after-load "msb" '(run-hooks 'msb-after-load-hook 'msb-after-load-hooks))
be17d374 1155
b9a5a6af 1156;;; msb.el ends here