Commit | Line | Data |
---|---|---|
b9a5a6af | 1 | ;;; msb.el --- Customizable buffer-selection with multiple menus. |
b578f267 | 2 | |
a4a49c21 | 3 | ;; Copyright (C) 1993, 94, 95, 97, 98, 99, 2000 Free Software Foundation, Inc. |
b578f267 | 4 | |
0eb3b336 | 5 | ;; Author: Lars Lindberg <Lars.G.Lindberg@capgemini.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 | ||
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 |
b578f267 EN |
15 | ;; the Free Software Foundation; either version 2, or (at your option) |
16 | ;; any later version. | |
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 |
b578f267 EN |
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. | |
b9a5a6af | 27 | |
b9a5a6af | 28 | ;;; Commentary: |
b578f267 | 29 | |
b9a5a6af RS |
30 | ;; Purpose of this package: |
31 | ;; 1. Offer a function for letting the user choose buffer, | |
32 | ;; not necessarily for switching to it. | |
aade135d DL |
33 | ;; 2. Make a better mouse-buffer-menu. This is done as a global |
34 | ;; minor mode, msb-mode. | |
b9a5a6af RS |
35 | ;; |
36 | ;; Customization: | |
2e6286be RS |
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. | |
b9a5a6af RS |
40 | ;; There are some constants for you to try here: |
41 | ;; msb--few-menus | |
42 | ;; msb--very-many-menus (default) | |
3cfa0ee9 | 43 | ;; |
2e6286be RS |
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'. | |
0eb3b336 | 47 | ;; |
2e6286be RS |
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 | |
b9a5a6af RS |
50 | ;; sorting - you will get latest used buffer first. |
51 | ;; | |
2e6286be | 52 | ;; Also check out the variable `msb-display-invisible-buffers-p'. |
b9a5a6af RS |
53 | |
54 | ;; Known bugs: | |
4aa4849b | 55 | ;; - Files-by-directory |
fd46fd17 | 56 | ;; + No possibility to show client/changed buffers separately. |
3cfa0ee9 | 57 | ;; + All file buffers only appear in a file sub-menu, they will |
fd46fd17 RS |
58 | ;; for instance not appear in the Mail sub-menu. |
59 | ||
b9a5a6af | 60 | ;; Future enhancements: |
b9a5a6af | 61 | |
b9a5a6af | 62 | ;;; Thanks goes to |
fd46fd17 RS |
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> | |
5762abec | 75 | ;; Richard Stallman <rms@gnu.org> |
fd46fd17 | 76 | ;; Steve Fisk <fisk@medved.bowdoin.edu> |
b9a5a6af | 77 | |
492bd758 DL |
78 | ;; This version turned into a global minor mode and subsequently |
79 | ;; hacked on by Dave Love. | |
b9a5a6af RS |
80 | ;;; Code: |
81 | ||
492bd758 | 82 | (eval-when-compile (require 'cl)) |
b9a5a6af RS |
83 | |
84 | ;;; | |
2e6286be RS |
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. | |
b9a5a6af RS |
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)") | |
a4a49c21 DL |
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))) | |
b9a5a6af RS |
117 | 4010 |
118 | "Mail (%d)") | |
119 | ((not buffer-file-name) | |
120 | 4099 | |
121 | "Buffers (%d)") | |
122 | ('no-multi | |
123 | 1099 | |
124 | "Files (%d)"))) | |
125 | ||
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 | |
0eb3b336 | 148 | "Invisible buffers (%d)") |
b9a5a6af RS |
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) | |
3cfa0ee9 | 157 | 5030 |
b9a5a6af RS |
158 | "Manuals (%d)") |
159 | ((eq major-mode 'w3-mode) | |
3cfa0ee9 | 160 | 5020 |
b9a5a6af | 161 | "WWW (%d)") |
a4a49c21 DL |
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 | |
b9a5a6af | 167 | gnus-browse-killed-mode))) |
3cfa0ee9 | 168 | 5010 |
b9a5a6af RS |
169 | "Mail (%d)") |
170 | ;; Catchup for all non-file buffers | |
171 | ((and (not buffer-file-name) | |
172 | 'no-multi) | |
3cfa0ee9 | 173 | 5099 |
b9a5a6af RS |
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)"))) | |
191 | ||
192 | ;; msb--many-menus is obsolete | |
193 | (defvar msb--many-menus msb--very-many-menus) | |
194 | ||
195 | ;;; | |
196 | ;;; Customizable variables | |
197 | ;;; | |
198 | ||
3cfa0ee9 SE |
199 | (defgroup msb nil |
200 | "Customizable buffer-selection with multiple menus." | |
201 | :prefix "msb-" | |
202 | :group 'mouse) | |
203 | ||
204 | (defun msb-custom-set (symbol value) | |
205 | "Set the value of custom variables for msb." | |
206 | (set symbol value) | |
eed30659 | 207 | (if (and (featurep 'msb) msb-mode) |
3cfa0ee9 SE |
208 | ;; wait until package has been loaded before bothering to update |
209 | ;; the buffer lists. | |
eed30659 | 210 | (msb-menu-bar-update-buffers t))) |
3cfa0ee9 SE |
211 | |
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). | |
216 | ||
217 | When making the split, the buffers are tested one by one against the | |
aade135d | 218 | CONDITION, just like a Lisp cond: When hitting a true condition, the |
3cfa0ee9 SE |
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. | |
221 | ||
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. | |
226 | ||
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'. | |
230 | ||
231 | The categories are sorted by MENU-SORT-KEY. Smaller keys are on top. | |
232 | nil means don't display this menu. | |
233 | ||
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. | |
236 | ||
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. | |
241 | ||
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. | |
251 | ||
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) | |
25bb0401 GM |
258 | (const :tag "short" :value ,msb--few-menus) |
259 | (sexp :tag "user")) | |
3cfa0ee9 SE |
260 | :set 'msb-custom-set |
261 | :group 'msb) | |
262 | ||
263 | (defcustom msb-modes-key 4000 | |
264 | "The sort key for files sorted by mode." | |
265 | :type 'integer | |
266 | :set 'msb-custom-set | |
f9e9ac1d DN |
267 | :group 'msb |
268 | :version "20.3") | |
3cfa0ee9 SE |
269 | |
270 | (defcustom msb-separator-diff 100 | |
b9a5a6af | 271 | "*Non-nil means use separators. |
0eb3b336 | 272 | The separators will appear between all menus that have a sorting key |
3cfa0ee9 SE |
273 | that differs by this value or more." |
274 | :type '(choice integer (const nil)) | |
275 | :set 'msb-custom-set | |
276 | :group 'msb) | |
b9a5a6af RS |
277 | |
278 | (defvar msb-files-by-directory-sort-key 0 | |
0eb3b336 | 279 | "*The sort key for files sorted by directory.") |
b9a5a6af | 280 | |
3cfa0ee9 | 281 | (defcustom msb-max-menu-items 15 |
b9a5a6af | 282 | "*The maximum number of items in a menu. |
0eb3b336 | 283 | If this variable is set to 15 for instance, then the submenu will be |
3cfa0ee9 SE |
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) | |
b9a5a6af | 288 | |
3cfa0ee9 | 289 | (defcustom msb-max-file-menu-items 10 |
b9a5a6af RS |
290 | "*The maximum number of items from different directories. |
291 | ||
2e6286be | 292 | When the menu is of type `file by directory', this is the maximum |
6331da4b | 293 | number of buffers that are clumped together from different |
b9a5a6af RS |
294 | directories. |
295 | ||
4aa4849b RS |
296 | Set this to 1 if you want one menu per directory instead of clumping |
297 | them together. | |
298 | ||
3cfa0ee9 SE |
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) | |
b9a5a6af | 303 | |
3cfa0ee9 SE |
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) | |
b9a5a6af | 309 | |
3cfa0ee9 | 310 | (defcustom msb-display-most-recently-used 15 |
b9a5a6af | 311 | "*How many buffers should be in the most-recently-used menu. |
3cfa0ee9 SE |
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) | |
316 | ||
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) | |
0eb3b336 | 322 | |
b9a5a6af | 323 | (defvar msb-horizontal-shift-function '(lambda () 0) |
0eb3b336 | 324 | "*Function that specifies how many pixels to shift the top menu leftwards.") |
b9a5a6af | 325 | |
3cfa0ee9 | 326 | (defcustom msb-display-invisible-buffers-p nil |
b9a5a6af RS |
327 | "*Show invisible buffers or not. |
328 | Non-nil means that the buffer menu should include buffers that have | |
3cfa0ee9 SE |
329 | names that starts with a space character." |
330 | :type 'boolean | |
331 | :set 'msb-custom-set | |
332 | :group 'msb) | |
b9a5a6af RS |
333 | |
334 | (defvar msb-item-handling-function 'msb-item-handler | |
335 | "*The appearance of a buffer menu. | |
336 | ||
337 | The default function to call for handling the appearance of a menu | |
2e6286be | 338 | item. It should take to arguments, BUFFER and MAX-BUFFER-NAME-LENGTH, |
b9a5a6af | 339 | where the latter is the max length of all buffer names. |
4aa4849b RS |
340 | |
341 | The function should return the string to use in the menu. | |
342 | ||
0eb3b336 RS |
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 | |
b9a5a6af RS |
346 | information.") |
347 | ||
3cfa0ee9 | 348 | (defcustom msb-item-sort-function 'msb-sort-by-name |
b9a5a6af | 349 | "*The order of items in a buffer menu. |
0eb3b336 | 350 | |
b9a5a6af | 351 | The default function to call for handling the order of items in a menu |
0eb3b336 RS |
352 | item. This function is called like a sort function. The items look |
353 | like (ITEM-NAME . BUFFER). | |
354 | ||
b9a5a6af RS |
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. | |
357 | ||
3cfa0ee9 SE |
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 | |
a4a49c21 | 363 | :group 'msb) |
3cfa0ee9 SE |
364 | |
365 | (defcustom msb-files-by-directory nil | |
aade135d DL |
366 | "*Non-nil means that files should be sorted by directory. |
367 | This is instead of the groups in `msb-menu-cond'." | |
3cfa0ee9 SE |
368 | :type 'boolean |
369 | :set 'msb-custom-set | |
370 | :group 'msb) | |
b9a5a6af | 371 | |
3cfa0ee9 SE |
372 | (defcustom msb-after-load-hooks nil |
373 | "Hooks to be run after the msb package has been loaded." | |
374 | :type 'hook | |
375 | :set 'msb-custom-set | |
376 | :group 'msb) | |
b9a5a6af RS |
377 | |
378 | ;;; | |
379 | ;;; Internal variables | |
380 | ;;; | |
381 | ||
382 | ;; The last calculated menu. | |
383 | (defvar msb--last-buffer-menu nil) | |
384 | ||
385 | ;; If this is non-nil, then it is a string that describes the error. | |
386 | (defvar msb--error nil) | |
387 | ||
388 | ;;; | |
4aa4849b | 389 | ;;; Some example function to be used for `msb-item-handling-function'. |
b9a5a6af RS |
390 | ;;; |
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> | |
2e6286be RS |
395 | The `*' appears only if the buffer is marked as modified. |
396 | The `%' appears only if the buffer is read-only. | |
b9a5a6af RS |
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))) | |
402 | ||
403 | ||
404 | (eval-when-compile (require 'dired)) | |
405 | ||
2e6286be RS |
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. | |
b9a5a6af RS |
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)))) | |
413 | (t | |
2e6286be | 414 | (error "Unknown type of `dired-directory' in buffer %s" |
b9a5a6af RS |
415 | (buffer-name))))) |
416 | ||
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> | |
2e6286be RS |
421 | The `*' appears only if the buffer is marked as modified. |
422 | The `%' appears only if the buffer is read-only. | |
b9a5a6af RS |
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))) | |
428 | ||
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> | |
2e6286be RS |
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)." | |
b9a5a6af RS |
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 ""))) | |
442 | ||
443 | ;;; | |
4aa4849b | 444 | ;;; Some example function to be used for `msb-item-sort-function'. |
b9a5a6af RS |
445 | ;;; |
446 | (defun msb-sort-by-name (item1 item2) | |
aade135d DL |
447 | "Sort the items ITEM1 and ITEM2 by their `buffer-name'. |
448 | An item looks like (NAME . BUFFER)." | |
b9a5a6af RS |
449 | (string-lessp (buffer-name (cdr item1)) |
450 | (buffer-name (cdr item2)))) | |
451 | ||
452 | ||
453 | (defun msb-sort-by-directory (item1 item2) | |
aade135d | 454 | "Sort the items ITEM1 and ITEM2 by directory name. Made for dired. |
b9a5a6af | 455 | An item look like (NAME . BUFFER)." |
0eb3b336 RS |
456 | (string-lessp (save-excursion (set-buffer (cdr item1)) |
457 | (msb--dired-directory)) | |
458 | (save-excursion (set-buffer (cdr item2)) | |
459 | (msb--dired-directory)))) | |
b9a5a6af RS |
460 | |
461 | ;;; | |
462 | ;;; msb | |
463 | ;;; | |
464 | ;;; This function can be used instead of (mouse-buffer-menu EVENT) | |
465 | ;;; function in "mouse.el". | |
0eb3b336 | 466 | ;;; |
b9a5a6af RS |
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. | |
471 | ||
2e6286be RS |
472 | See the function `mouse-select-buffer' and the variable |
473 | `msb-menu-cond' for more information about how the menus are split." | |
b9a5a6af | 474 | (interactive "e") |
fd46fd17 | 475 | (let ((old-window (selected-window)) |
b9a5a6af | 476 | (window (posn-window (event-start event)))) |
fd46fd17 RS |
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)))) | |
b9a5a6af RS |
482 | nil) |
483 | ||
484 | ;;; | |
485 | ;;; Some supportive functions | |
486 | ;;; | |
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)))) | |
492 | ||
7612d61a | 493 | (defun msb--strip-dir (dir) |
eed30659 | 494 | "Strip one hierarchy level from the end of DIR." |
862aacbf | 495 | (file-name-directory (directory-file-name dir))) |
b9a5a6af RS |
496 | |
497 | ;; Create an alist with all buffers from LIST that lies under the same | |
0eb3b336 | 498 | ;; directory will be in the same item as the directory string. |
9153492c | 499 | ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K BUFFER-K+1...)) ...) |
b9a5a6af RS |
500 | (defun msb--init-file-alist (list) |
501 | (let ((buffer-alist | |
0eb3b336 RS |
502 | ;; Make alist that looks like |
503 | ;; ((PATH-1 BUFFER-1) (PATH-2 BUFFER-2) ...) | |
504 | ;; sorted on PATH-x | |
a4a49c21 DL |
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)))))) | |
c549c1bf | 516 | ;; Now clump buffers together that have the same path |
b9a5a6af | 517 | ;; Make alist that looks like |
2e6286be | 518 | ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K)) ...) |
b9a5a6af | 519 | (let ((path nil) |
0eb3b336 RS |
520 | (buffers nil)) |
521 | (nconc | |
a4a49c21 DL |
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) | |
532 | (t | |
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)) | |
0eb3b336 | 540 | ;; Add the last result to the list |
b9a5a6af RS |
541 | (list (cons path buffers)))))) |
542 | ||
0eb3b336 | 543 | (defun msb--format-title (top-found-p path number-of-items) |
eed30659 | 544 | "Format a suitable title for the menu item." |
492bd758 DL |
545 | (format (if top-found-p "%s... (%d)" "%s (%d)") |
546 | (abbreviate-file-name path) number-of-items)) | |
0eb3b336 | 547 | |
c549c1bf RS |
548 | ;; Variables for debugging. |
549 | (defvar msb--choose-file-menu-list) | |
550 | (defvar msb--choose-file-menu-arg-list) | |
0eb3b336 | 551 | |
b9a5a6af | 552 | (defun msb--choose-file-menu (list) |
eed30659 | 553 | "Choose file-menu with respect to directory for every buffer in LIST." |
c549c1bf | 554 | (setq msb--choose-file-menu-arg-list list) |
b9a5a6af RS |
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) | |
3cfa0ee9 | 562 | first rest path buffers old-path) |
0eb3b336 RS |
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)) | |
a4a49c21 | 568 | (setq msb--choose-file-menu-list (copy-sequence rest)) |
0eb3b336 RS |
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. | |
b9a5a6af RS |
572 | (while rest |
573 | (let ((found-p nil) | |
574 | (tmp-rest rest) | |
0eb3b336 | 575 | result |
b9a5a6af RS |
576 | new-path item) |
577 | (setq item (car tmp-rest)) | |
0eb3b336 RS |
578 | ;; Clump together the "rest"-buffers that have a path that is |
579 | ;; a subpath of the current one. | |
b9a5a6af RS |
580 | (while (and tmp-rest |
581 | (<= (length buffers) max-clumped-together) | |
582 | (>= (length (car item)) (length path)) | |
b9b37d2b DL |
583 | ;; `completion-ignore-case' seems to default to t |
584 | ;; on the systems with case-insensitive file names. | |
c88e53b7 | 585 | (eq t (compare-strings path 0 nil |
b9b37d2b DL |
586 | (car item) 0 (length path) |
587 | completion-ignore-case))) | |
b9a5a6af | 588 | (setq found-p t) |
0eb3b336 RS |
589 | (setq buffers (append buffers (cdr item))) ;nconc is faster than append |
590 | (setq tmp-rest (cdr tmp-rest) | |
591 | item (car tmp-rest))) | |
b9a5a6af RS |
592 | (cond |
593 | ((> (length buffers) max-clumped-together) | |
0eb3b336 RS |
594 | ;; Oh, we failed. Too many buffers clumped together. |
595 | ;; Just use the original ones for the result. | |
b9a5a6af | 596 | (setq last-path (car first)) |
0eb3b336 RS |
597 | (push (cons (msb--format-title top-found-p |
598 | (car first) | |
599 | (length (cdr first))) | |
600 | (cdr first)) | |
601 | final-list) | |
4aa4849b | 602 | (setq top-found-p nil) |
b9a5a6af | 603 | (setq first (car rest) |
0eb3b336 RS |
604 | rest (cdr rest) |
605 | path (car first) | |
b9a5a6af RS |
606 | buffers (cdr first))) |
607 | (t | |
0eb3b336 RS |
608 | ;; The first pass of clumping together worked out, go ahead |
609 | ;; with this result. | |
b9a5a6af RS |
610 | (when found-p |
611 | (setq top-found-p t) | |
612 | (setq first (cons path buffers) | |
613 | rest tmp-rest)) | |
0eb3b336 RS |
614 | ;; Now see if we can clump more buffers together if we go up |
615 | ;; one step in the file hierarchy. | |
3cfa0ee9 SE |
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) | |
7612d61a | 619 | (setq path (msb--strip-dir path) |
b9a5a6af | 620 | buffers (cdr first)) |
3cfa0ee9 SE |
621 | (if (equal old-path path) |
622 | (setq last-path path)) | |
b9a5a6af RS |
623 | (when (and last-path |
624 | (or (and (>= (length path) (length last-path)) | |
b9b37d2b DL |
625 | (eq t (compare-strings |
626 | last-path 0 nil path 0 | |
627 | (length last-path) | |
628 | completion-ignore-case))) | |
b9a5a6af | 629 | (and (< (length path) (length last-path)) |
b9b37d2b DL |
630 | (eq t (compare-strings |
631 | path 0 nil last-path 0 (length path) | |
632 | completion-ignore-case))))) | |
0eb3b336 RS |
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) | |
4aa4849b | 641 | (setq top-found-p nil) |
b9a5a6af | 642 | (setq first (car rest) |
0eb3b336 RS |
643 | rest (cdr rest) |
644 | path (car first) | |
645 | buffers (cdr first))))))) | |
646 | ;; Now take care of the last item. | |
3cfa0ee9 SE |
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)) | |
4aa4849b | 653 | (setq top-found-p nil) |
b9a5a6af RS |
654 | (nreverse final-list))) |
655 | ||
b9a5a6af | 656 | (defun msb--create-function-info (menu-cond-elt) |
eed30659 DL |
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." | |
b9a5a6af RS |
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) | |
2e6286be | 672 | (eq tmp-s t)) |
a4a49c21 | 673 | tmp-s |
b9a5a6af RS |
674 | msb-item-sort-function))) |
675 | (when (< (length menu-cond-elt) 3) | |
aade135d | 676 | (error "Wrong format of msb-menu-cond")) |
b9a5a6af RS |
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)) | |
2e6286be | 683 | (not (eq tmp-s t))) |
b9a5a6af | 684 | (signal 'invalid-function (list tmp-s))) |
2e6286be | 685 | (set list-symbol ()) |
b9a5a6af RS |
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 | |
692 | )) | |
693 | ||
694 | ;; This defsubst is only used in `msb--choose-menu' below. It was | |
3cfa0ee9 | 695 | ;; pulled out merely to make the code somewhat clearer. The indentation |
b9a5a6af RS |
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)) | |
4aa4849b | 710 | t)) |
b9a5a6af RS |
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)) | |
718 | ||
b9a5a6af | 719 | (defun msb--add-to-menu (buffer function-info max-buffer-name-length) |
eed30659 DL |
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." | |
b9a5a6af RS |
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))))) | |
0eb3b336 | 731 | |
b9a5a6af | 732 | (defsubst msb--choose-menu (buffer function-info-vector max-buffer-name-length) |
eed30659 DL |
733 | "Select the appropriate menu for BUFFER." |
734 | ;; This is all side-effects, folks! | |
735 | ;; This should be optimized. | |
b9a5a6af RS |
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) | |
2e6286be | 741 | ;; Menu found. Add to this menu |
b2eb3813 GM |
742 | (dolist (info (msb--collect function-info-vector)) |
743 | (msb--add-to-menu buffer info max-buffer-name-length))) | |
b9a5a6af RS |
744 | (error (unless msb--error |
745 | (setq msb--error | |
746 | (format | |
2e6286be | 747 | "In msb-menu-cond, error for buffer `%s'." |
b9a5a6af | 748 | (buffer-name buffer))) |
76e4c0ba | 749 | (error "%s" msb--error)))))) |
b9a5a6af | 750 | |
b9a5a6af | 751 | (defun msb--create-sort-item (function-info) |
eed30659 | 752 | "Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the buffer-list is empty." |
b9a5a6af RS |
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 | |
0eb3b336 | 758 | (cons sort-key |
b9a5a6af RS |
759 | (cons (format (aref function-info 3) ;MENU-TITLE |
760 | (length buffer-list)) | |
761 | (cond | |
762 | ((null sorter) | |
763 | buffer-list) | |
2e6286be | 764 | ((eq sorter t) |
b9a5a6af RS |
765 | (nreverse buffer-list)) |
766 | (t | |
767 | (sort buffer-list sorter)))))))))) | |
768 | ||
3cfa0ee9 | 769 | (defun msb--aggregate-alist (alist same-predicate sort-predicate) |
eed30659 DL |
770 | "Return ALIST as a sorted, aggregated alist. |
771 | ||
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. | |
775 | ||
776 | Example: | |
b2eb3813 | 777 | \(msb--aggregate-alist |
eed30659 DL |
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 | |
b2eb3813 | 783 | \((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))" |
3cfa0ee9 SE |
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 | |
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) | |
a4a49c21 | 811 | (funcall sort-predicate (car item1) (car item2)))))) |
3cfa0ee9 SE |
812 | (list (cons old-car (nreverse same))))))) |
813 | ||
814 | ||
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))) | |
820 | key | |
821 | (concat (cdr item) " (%d)"))) | |
aade135d | 822 | (sort |
3cfa0ee9 | 823 | (let ((mode-list nil)) |
b2eb3813 GM |
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)))) | |
3cfa0ee9 SE |
831 | mode-list) |
832 | (lambda (item1 item2) | |
833 | (string< (cdr item1) (cdr item2))))))) | |
834 | ||
b9a5a6af | 835 | (defun msb--most-recently-used-menu (max-buffer-name-length) |
eed30659 DL |
836 | "Return a list for the most recently used buffers. |
837 | It takes the form ((TITLE . BUFFER-LIST)...)." | |
4aa4849b RS |
838 | (when (and (numberp msb-display-most-recently-used) |
839 | (> msb-display-most-recently-used 0)) | |
fd46fd17 RS |
840 | (let* ((buffers (cdr (buffer-list))) |
841 | (most-recently-used | |
b9a5a6af | 842 | (loop with n = 0 |
fd46fd17 | 843 | for buffer in buffers |
b9a5a6af RS |
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) | |
4aa4849b | 855 | until (>= n msb-display-most-recently-used)))) |
b9a5a6af RS |
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)))) | |
861 | ||
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. | |
b2eb3813 GM |
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)))))) | |
b9a5a6af RS |
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) | |
3cfa0ee9 | 884 | (append msb-menu-cond (msb--mode-menu-cond))))) |
b9a5a6af | 885 | ;; Split the buffer-list into several lists; one list for each |
2e6286be | 886 | ;; criteria. This is the most critical part with respect to time. |
b2eb3813 GM |
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))) | |
893 | ) | |
894 | (push buffer file-buffers)) | |
895 | (t | |
896 | (msb--choose-menu buffer | |
897 | function-info-vector | |
898 | max-buffer-name-length)))) | |
b9a5a6af RS |
899 | (when file-buffers |
900 | (setq file-buffers | |
3cfa0ee9 SE |
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)))))))) | |
b9a5a6af RS |
917 | (msb--choose-file-menu file-buffers)))) |
918 | ;; Now make the menu - a list of (TITLE . BUFFER-LIST) | |
4aa4849b | 919 | (let* (menu |
b9a5a6af RS |
920 | (most-recently-used |
921 | (msb--most-recently-used-menu max-buffer-name-length)) | |
0eb3b336 | 922 | (others (nconc file-buffers |
b9a5a6af | 923 | (loop for elt |
4aa4849b RS |
924 | across function-info-vector |
925 | for value = (msb--create-sort-item elt) | |
926 | if value collect value)))) | |
b9a5a6af RS |
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) | |
3cfa0ee9 SE |
941 | (lambda (elt1 elt2) |
942 | (< (car elt1) (car elt2))))))) | |
b9a5a6af RS |
943 | ;; Now make it a keymap menu |
944 | (append | |
945 | '(keymap "Select Buffer") | |
946 | (msb--make-keymap-menu menu) | |
947 | (when msb-separator-diff | |
0eb3b336 RS |
948 | (list (list 'separator "--"))) |
949 | (list (cons 'toggle | |
b9a5a6af RS |
950 | (cons |
951 | (if msb-files-by-directory | |
a4a49c21 DL |
952 | "*Files by type*" |
953 | "*Files by directory*") | |
954 | 'msb--toggle-menu-type))))))) | |
b9a5a6af RS |
955 | |
956 | (defun msb--create-buffer-menu () | |
957 | (save-match-data | |
958 | (save-excursion | |
959 | (msb--create-buffer-menu-2)))) | |
960 | ||
b9a5a6af | 961 | (defun msb--toggle-menu-type () |
eed30659 | 962 | "Multi purpose function for selecting a buffer with the mouse." |
b9a5a6af RS |
963 | (interactive) |
964 | (setq msb-files-by-directory (not msb-files-by-directory)) | |
c549c1bf RS |
965 | ;; This gets a warning, but it is correct, |
966 | ;; because this file redefines menu-bar-update-buffers. | |
eed30659 | 967 | (msb-menu-bar-update-buffers t)) |
b9a5a6af RS |
968 | |
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. | |
972 | ||
4aa4849b | 973 | The way the buffers are split is conveniently handled with the |
2e6286be | 974 | variable `msb-menu-cond'." |
b9a5a6af RS |
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))) | |
4aa4849b RS |
982 | (let ((position event) |
983 | choice) | |
b9a5a6af RS |
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)))) | |
4aa4849b | 988 | (posWind (posn-window (event-start event)))) |
b9a5a6af RS |
989 | ;; adjust position |
990 | (setq posX (- posX (funcall msb-horizontal-shift-function)) | |
991 | position (list (list posX posY) posWind)))) | |
1cc9a99e | 992 | ;; This `sit-for' magically makes the menu stay up if the mouse |
6331da4b | 993 | ;; button is released within 0.1 second. |
1cc9a99e RS |
994 | (sit-for 0 100) |
995 | ;; Popup the menu | |
4aa4849b | 996 | (setq choice (x-popup-menu position msb--last-buffer-menu)) |
b9a5a6af | 997 | (cond |
4aa4849b RS |
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))) | |
a4a49c21 DL |
1004 | (let ((msb--last-buffer-menu (nthcdr 3 (assq (car choice) |
1005 | msb--last-buffer-menu)))) | |
b9a5a6af | 1006 | (mouse-select-buffer event))) |
4aa4849b RS |
1007 | ((while (numberp (car choice)) |
1008 | (setq choice (cdr choice)))) | |
1009 | ((and (stringp (car choice)) | |
1010 | (null (cdr choice))) | |
1011 | (car choice)) | |
dc3247b3 RS |
1012 | ((null choice) |
1013 | choice) | |
4aa4849b RS |
1014 | (t |
1015 | (error "Unknown form for buffer: %s" choice))))) | |
3cfa0ee9 | 1016 | |
b9a5a6af RS |
1017 | ;; Add separators |
1018 | (defun msb--add-separators (sorted-list) | |
a4a49c21 DL |
1019 | (if (or (not msb-separator-diff) |
1020 | (not (numberp msb-separator-diff))) | |
1021 | sorted-list | |
b9a5a6af | 1022 | (let ((last-key nil)) |
a4a49c21 DL |
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)) | |
1034 | (t | |
1035 | (setq last-key (car item)) | |
1036 | (list item)))) | |
1037 | sorted-list))))) | |
b9a5a6af | 1038 | |
4aa4849b RS |
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)) | |
a4a49c21 DL |
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)) | |
4aa4849b RS |
1054 | (msb--split-menus-2 list (1+ mcount) result)) |
1055 | ((null result) | |
1056 | list) | |
1057 | (t | |
1058 | (let (sub-name) | |
1059 | (setq sub-name (concat (car (car list)) "...")) | |
a4a49c21 DL |
1060 | (push (nconc (list mcount sub-name 'keymap sub-name) |
1061 | list) | |
1062 | result)) | |
4aa4849b | 1063 | (nreverse result)))) |
4aa4849b | 1064 | |
3cfa0ee9 SE |
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)) | |
4aa4849b | 1070 | |
b9a5a6af RS |
1071 | (defun msb--make-keymap-menu (raw-menu) |
1072 | (let ((end (cons '(nil) 'menu-bar-select-buffer)) | |
1073 | (mcount 0)) | |
1074 | (mapcar | |
3cfa0ee9 | 1075 | (lambda (sub-menu) |
aade135d | 1076 | (cond |
3cfa0ee9 SE |
1077 | ((eq 'separator sub-menu) |
1078 | (list 'separator "--")) | |
1079 | (t | |
a4a49c21 DL |
1080 | (let ((buffers (mapcar (lambda (item) |
1081 | (cons (buffer-name (cdr item)) | |
1082 | (cons (car item) end))) | |
3cfa0ee9 SE |
1083 | (cdr sub-menu)))) |
1084 | (nconc (list (incf mcount) (car sub-menu) | |
1085 | 'keymap (car sub-menu)) | |
1086 | (msb--split-menus buffers)))))) | |
b9a5a6af RS |
1087 | raw-menu))) |
1088 | ||
eed30659 DL |
1089 | (defun msb-menu-bar-update-buffers (&optional arg) |
1090 | "A re-written version of `menu-bar-update-buffers'." | |
b9a5a6af RS |
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)) | |
fd46fd17 | 1096 | (let ((frames (frame-list)) |
b9a5a6af | 1097 | buffers-menu frames-menu) |
b9a5a6af RS |
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. | |
fd46fd17 RS |
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)) | |
b9a5a6af | 1110 | (setq frames-menu |
fd46fd17 RS |
1111 | (nconc |
1112 | (list 'frame f-title '(nil) 'keymap f-title) | |
1113 | (mapcar | |
3cfa0ee9 SE |
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)) | |
fd46fd17 | 1121 | frames))))) |
b9a5a6af RS |
1122 | (define-key (current-global-map) [menu-bar buffer] |
1123 | (cons "Buffers" | |
1124 | (if (and buffers-menu frames-menu) | |
fd46fd17 RS |
1125 | ;; Combine Frame and Buffers menus with separator between |
1126 | (nconc (list 'keymap "Buffers and Frames" frames-menu | |
0eb3b336 | 1127 | (and msb-separator-diff '(separator "--"))) |
fd46fd17 RS |
1128 | (cddr buffers-menu)) |
1129 | (or buffers-menu 'undefined))))))) | |
b9a5a6af | 1130 | |
aade135d DL |
1131 | ;; Snarf current bindings of `mouse-buffer-menu' (normally |
1132 | ;; C-down-mouse-1). | |
1133 | (defvar msb-mode-map | |
a4a49c21 | 1134 | (let ((map (make-sparse-keymap "Msb"))) |
8b618804 | 1135 | (substitute-key-definition 'mouse-buffer-menu 'msb map global-map) |
aade135d DL |
1136 | map)) |
1137 | ||
1138 | ;;;###autoload | |
3bdb5fb8 | 1139 | (define-minor-mode msb-mode |
aade135d DL |
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'." | |
8b618804 DL |
1144 | nil nil msb-mode-map |
1145 | :global t | |
aade135d | 1146 | (if msb-mode |
eed30659 DL |
1147 | (progn |
1148 | (add-hook 'menu-bar-update-hook 'msb-menu-bar-update-buffers) | |
1149 | (remove-hook 'menu-bar-update-hook 'menu-bar-update-buffers)) | |
1150 | (remove-hook 'menu-bar-update-hook 'msb-menu-bar-update-buffers) | |
1151 | (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)) | |
1152 | (run-hooks 'menu-bar-update-hook)) | |
aade135d | 1153 | |
a4a49c21 DL |
1154 | (defun msb-unload-hook () |
1155 | (msb-mode 0)) | |
1156 | ||
b9a5a6af RS |
1157 | (provide 'msb) |
1158 | (eval-after-load 'msb (run-hooks 'msb-after-load-hooks)) | |
be17d374 | 1159 | |
b9a5a6af | 1160 | ;;; msb.el ends here |