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