msb.el 31.7 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; msb.el --- Customizable buffer-selection with multiple menus.
Karl Heuer's avatar
Karl Heuer committed
2
;; Copyright (C) 1993, 1994, 1995 Lars Lindberg <Lars.Lindberg@sypro.cap.se>
Richard M. Stallman's avatar
Richard M. Stallman committed
3 4 5
;;
;; Author: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
;; Created: 8 Oct 1993
6
;; Lindberg's last update version: 3.31
Richard M. Stallman's avatar
Richard M. Stallman committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
;; Keywords: mouse buffer menu 
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:
;;
;; Purpose of this package:
;;   1. Offer a function for letting the user choose buffer,
;;      not necessarily for switching to it.
;;   2. Make a better mouse-buffer-menu.
;;
;; Installation:
31 32 33 34 35 36 37 38

;;   1. Byte compile msb first.  It uses things in the cl package that
;;      are slow if not compiled, but blazingly fast when compiled.  I
;;      have also had one report that said that msb malfunctioned when
;;      not compiled.
;;   2. (require 'msb)
;;      Note! You now use msb instead of mouse-buffer-menu.
;;   3. Now try the menu bar Buffers menu.
Richard M. Stallman's avatar
Richard M. Stallman committed
39 40
;;
;; Customization:
41 42 43
;;   Look at the variable `msb-menu-cond' for deciding what menus you
;;   want.  It's not that hard to customize, despite my not-so-good
;;   doc-string.  Feel free to send me a better doc-string.
Richard M. Stallman's avatar
Richard M. Stallman committed
44 45 46 47
;;   There are some constants for you to try here:
;;   msb--few-menus
;;   msb--very-many-menus (default)
;;   
48 49 50
;;   Look at the variable `msb-item-handling-function' for customization
;;   of the appearance of every menu item.  Try for instance setting
;;   it to `msb-alon-item-handler'.
Richard M. Stallman's avatar
Richard M. Stallman committed
51
;;   
52 53
;;   Look at the variable `msb-item-sort-function' for customization
;;   of sorting the menus.  Set it to t for instance, which means no
Richard M. Stallman's avatar
Richard M. Stallman committed
54 55
;;   sorting - you will get latest used buffer first.
;;
56
;;   Also check out the variable `msb-display-invisible-buffers-p'.
Richard M. Stallman's avatar
Richard M. Stallman committed
57 58

;; Known bugs:
59
;; - Files-by-directory
60 61 62 63
;;   + No possibility to show client/changed buffers separately.
;;   + All file buffers only appear in in a file sub-menu, they will
;;     for instance not appear in the Mail sub-menu.

Richard M. Stallman's avatar
Richard M. Stallman committed
64 65 66
;; Future enhancements:

;;; Thanks goes to
67 68 69 70 71 72 73 74 75 76 77 78 79 80
;;  Mark Brader <msb@sq.com>
;;  Jim Berry <m1jhb00@FRB.GOV>
;;  Hans Chalupsky <hans@cs.Buffalo.EDU>
;;  Larry Rosenberg <ljr@ictv.com>
;;  Will Henney <will@astroscu.unam.mx>
;;  Jari Aalto <jaalto@tre.tele.nokia.fi>
;;  Michael Kifer <kifer@sbkifer.cs.sunysb.edu>
;;  Gael Marziou <gael@gnlab030.grenoble.hp.com>
;;  Dave Gillespie <daveg@thymus.synaptics.com>
;;  Alon Albert <alon@milcse.rtsg.mot.com>
;;  Kevin Broadey, <KevinB@bartley.demon.co.uk>
;;  Ake Stenhof <ake@cadpoint.se>
;;  Richard Stallman <rms@gnu.ai.mit.edu>
;;  Steve Fisk <fisk@medved.bowdoin.edu>
Richard M. Stallman's avatar
Richard M. Stallman committed
81 82 83 84 85 86

;;; Code:

(require 'cl)

;;;
87 88 89
;;; Some example constants to be used for `msb-menu-cond'.  See that
;;; variable for more information.  Please note that if the condition
;;; returns `multi', then the buffer can appear in several menus.
Richard M. Stallman's avatar
Richard M. Stallman committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
;;;
(defconst msb--few-menus
  '(((and (boundp 'server-buffer-clients)
	  server-buffer-clients
	  'multi)
     3030
     "Clients (%d)")
    ((and msb-display-invisible-buffers-p
	  (msb-invisible-buffer-p)
	  'multi)
     3090
     "Invisible buffers (%d)")
    ((eq major-mode 'dired-mode)
     2010
     "Dired (%d)"
     msb-dired-item-handler
     msb-sort-by-directory)
    ((eq major-mode 'Man-mode)
     4090
     "Manuals (%d)")
    ((eq major-mode 'w3-mode)
     4020
     "WWW (%d)")
113
    ((or (memq major-mode '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	 (memq major-mode '(mh-letter-mode
			    mh-show-mode
			    mh-folder-mode))	 
	 (memq major-mode '(gnus-summary-mode
			    news-reply-mode
			    gnus-group-mode
			    gnus-article-mode
			    gnus-kill-file-mode
			    gnus-browse-killed-mode)))
     4010
     "Mail (%d)")
    ((not buffer-file-name)
     4099
     "Buffers (%d)")
    ('no-multi
     1099
     "Files (%d)")))

(defconst msb--very-many-menus
  '(((and (boundp 'server-buffer-clients)
	  server-buffer-clients
	  'multi)
     1010
     "Clients (%d)")
    ((and (boundp 'vc-mode) vc-mode 'multi)
     1020
     "Version Control (%d)")
    ((and buffer-file-name
	  (buffer-modified-p)
	  'multi)
     1030
     "Changed files (%d)")
    ((and (get-buffer-process (current-buffer))
	  'multi)
     1040
     "Processes (%d)")
    ((and msb-display-invisible-buffers-p
	  (msb-invisible-buffer-p)
	  'multi)
     1090
     "Invisible buffers (%d)") 
    ((eq major-mode 'dired-mode)
     2010
     "Dired (%d)"
     ;; Note this different menu-handler
     msb-dired-item-handler
     ;; Also note this item-sorter
     msb-sort-by-directory)
    ((eq major-mode 'Man-mode)
     4030
     "Manuals (%d)")
    ((eq major-mode 'w3-mode)
     4020
     "WWW (%d)")
168
    ((or (memq major-mode '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
Richard M. Stallman's avatar
Richard M. Stallman committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	 (memq major-mode '(mh-letter-mode
			    mh-show-mode
			    mh-folder-mode))	 
	 (memq major-mode '(gnus-summary-mode
			    news-reply-mode
			    gnus-group-mode
			    gnus-article-mode
			    gnus-kill-file-mode
			    gnus-browse-killed-mode)))
     4010
     "Mail (%d)")
    ;; Catchup for all non-file buffers
    ((and (not buffer-file-name)
	  'no-multi)
     4099
     "Other non-file buffers (%d)")
    ((and (string-match "/\\.[^/]*$" buffer-file-name)
	  'multi)
     3090
     "Hidden Files (%d)")
    ((memq major-mode '(c-mode c++-mode))
     3010
     "C/C++ Files (%d)")
    ((eq major-mode 'emacs-lisp-mode)
     3020
     "Elisp Files (%d)")
    ((eq major-mode 'latex-mode)
     3030
     "LaTex Files (%d)")
    ('no-multi
     3099
     "Other files (%d)")))

;; msb--many-menus is obsolete
(defvar msb--many-menus msb--very-many-menus)

;;;
;;; Customizable variables
;;;

(defvar msb-separator-diff 100
  "*Non-nil means use separators.
The separators will appear between all menus that have a sorting key that differs by this value or more.")

(defvar msb-files-by-directory-sort-key 0
  "*The sort key for files sorted by directory")

216
(defvar msb-max-menu-items 15
Richard M. Stallman's avatar
Richard M. Stallman committed
217
  "*The maximum number of items in a menu.
218
If this variable is set to 15 for instance, then the submenu will be split up in minor parts, 15 items each.
Richard M. Stallman's avatar
Richard M. Stallman committed
219 220 221 222 223
Nil means no limit.")

(defvar msb-max-file-menu-items 10
  "*The maximum number of items from different directories.

224
When the menu is of type `file by directory', this is the maximum
Richard M. Stallman's avatar
Richard M. Stallman committed
225 226 227
number of buffers that are clumped togehter from different
directories.

228 229 230
Set this to 1 if you want one menu per directory instead of clumping
them together.

Richard M. Stallman's avatar
Richard M. Stallman committed
231 232 233 234 235
If the value is not a number, then the value 10 is used.")

(defvar msb-most-recently-used-sort-key -1010
  "*Where should the menu with the most recently used buffers be placed?")

236
(defvar msb-display-most-recently-used 15
Richard M. Stallman's avatar
Richard M. Stallman committed
237
  "*How many buffers should be in the most-recently-used menu.
238
 No buffers at all if less than 1 or nil (or any non-number).")
Richard M. Stallman's avatar
Richard M. Stallman committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

(defvar msb-most-recently-used-title "Most recently used (%d)"
  "*The title for the most-recently-used menu.")
  
(defvar msb-horizontal-shift-function '(lambda () 0)
  "*Function that specifies a number of pixels by which the top menu should
be shifted leftwards.")

(defvar msb-display-invisible-buffers-p nil
  "*Show invisible buffers or not.
Non-nil means that the buffer menu should include buffers that have
names that starts with a space character.")

(defvar msb-item-handling-function 'msb-item-handler
  "*The appearance of a buffer menu.

The default function to call for handling the appearance of a menu
256
item.  It should take to arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
Richard M. Stallman's avatar
Richard M. Stallman committed
257
where the latter is the max length of all buffer names.
258 259 260

The function should return the string to use in the menu.

Richard M. Stallman's avatar
Richard M. Stallman committed
261
When the function is called, BUFFER is the current buffer.
262 263
This function is called for items in the variable `msb-menu-cond' that
have nil as ITEM-HANDLING-FUNCTION.  See `msb-menu-cond' for more
Richard M. Stallman's avatar
Richard M. Stallman committed
264 265 266 267 268
information.")

(defvar msb-item-sort-function 'msb-sort-by-name
  "*The order of items in a buffer menu.
The default function to call for handling the order of items in a menu
269
item.  This function is called like a sort function.  The items
Richard M. Stallman's avatar
Richard M. Stallman committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
look like (ITEM-NAME . BUFFER).
ITEM-NAME is the name of the item that will appear in the menu.
BUFFER is the buffer, this is not necessarily the current buffer.

Set this to nil or t if you don't want any sorting (faster).")

(defvar msb-files-by-directory nil
  "*Non-nil means that files should be sorted by directory instead of
the groups in msb-menu-cond.")

(defvar msb-menu-cond msb--very-many-menus
  "*List of criterias for splitting the mouse buffer menu.
The elements in the list should be of this type:
 (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).

When making the split, the buffers are tested one by one against the
CONDITION, just like a lisp cond: When hitting a true condition, the
other criterias are *not* tested and the buffer name will appear in
the menu with the menu-title corresponding to the true condition.

290
If the condition returns the symbol `multi', then the buffer will be
Richard M. Stallman's avatar
Richard M. Stallman committed
291
added to this menu *and* tested for other menus too.  If it returns
292
`no-multi', then the buffer will only be added if it hasn't been added
Richard M. Stallman's avatar
Richard M. Stallman committed
293 294 295 296
to any other menu.

During this test, the buffer in question is the current buffer, and
the test is surrounded by calls to `save-excursion' and
297
`save-match-data'.
Richard M. Stallman's avatar
Richard M. Stallman committed
298

299 300
The categories are sorted by MENU-SORT-KEY.  Smaller keys are on
top.  nil means don't display this menu.
Richard M. Stallman's avatar
Richard M. Stallman committed
301

302
MENU-TITLE is really a format.  If you add %d in it, the %d is replaced
Richard M. Stallman's avatar
Richard M. Stallman committed
303 304
with the number of items in that menu.

305
ITEM-HANDLING-FN, is optional.  If it is supplied and is a
Richard M. Stallman's avatar
Richard M. Stallman committed
306 307
function, than it is used for displaying the items in that particular
buffer menu, otherwise the function pointed out by
308
`msb-item-handling-function' is used.
Richard M. Stallman's avatar
Richard M. Stallman committed
309 310 311

ITEM-SORT-FN, is also optional.
If it is not supplied, the function pointed out by
312
`msb-item-sort-function' is used.
Richard M. Stallman's avatar
Richard M. Stallman committed
313 314 315 316 317 318 319
If it is nil, then no sort takes place and the buffers are presented
in least-recently-used order.
If it is t, then no sort takes place and the buffers are presented in
most-recently-used order.
If it is supplied and non-nil and not t than it is used for sorting
the items in that particular buffer menu.

320 321
Note1: There should always be a `catch-all' as last element,
in this list.  That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
Richard M. Stallman's avatar
Richard M. Stallman committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
Note2: A buffer menu appears only if it has at least one buffer in it.
Note3: If you have a CONDITION that can't be evaluated you will get an
error every time you do \\[msb].")

(defvar msb-after-load-hooks nil
  "Hooks to be run after the msb package has been loaded.")

;;;
;;; Internal variables
;;;

;; The last calculated menu.
(defvar msb--last-buffer-menu nil)

;; If this is non-nil, then it is a string that describes the error.
(defvar msb--error nil)

;;;
340
;;; Some example function to be used for `msb-item-handling-function'.
Richard M. Stallman's avatar
Richard M. Stallman committed
341 342 343 344 345
;;;
(defun msb-item-handler (buffer &optional maxbuf)
  "Create one string item, concerning BUFFER, for the buffer menu.
The item looks like:
*% <buffer-name>
346 347
The `*' appears only if the buffer is marked as modified.
The `%' appears only if the buffer is read-only.
Richard M. Stallman's avatar
Richard M. Stallman committed
348 349 350 351 352 353 354 355 356
Optional second argument MAXBUF is completely ignored."
  (let ((name (buffer-name))
	(modified (if (buffer-modified-p) "*" " "))
	(read-only (if buffer-read-only "%" " ")))
    (format "%s%s %s" modified read-only name)))


(eval-when-compile (require 'dired))

357 358
;; `dired' can be called with a list of the form (directory file1 file2 ...)
;; which causes `dired-directory' to be in the same form.
Richard M. Stallman's avatar
Richard M. Stallman committed
359 360 361 362 363 364
(defun msb--dired-directory ()
  (cond ((stringp dired-directory)
	 (abbreviate-file-name (expand-file-name dired-directory)))
	((consp dired-directory)
	 (abbreviate-file-name (expand-file-name (car dired-directory))))
	(t
365
	 (error "Unknown type of `dired-directory' in buffer %s"
Richard M. Stallman's avatar
Richard M. Stallman committed
366 367 368 369 370 371
		(buffer-name)))))

(defun msb-dired-item-handler (buffer &optional maxbuf)
  "Create one string item, concerning a dired BUFFER, for the buffer menu.
The item looks like:
*% <buffer-name>
372 373
The `*' appears only if the buffer is marked as modified.
The `%' appears only if the buffer is read-only.
Richard M. Stallman's avatar
Richard M. Stallman committed
374 375 376 377 378 379 380 381 382 383
Optional second argument MAXBUF is completely ignored."
  (let ((name (msb--dired-directory))
	(modified (if (buffer-modified-p) "*" " "))
	(read-only (if buffer-read-only "%" " ")))
    (format "%s%s %s" modified read-only name)))

(defun msb-alon-item-handler (buffer maxbuf)
  "Create one string item for the buffer menu.
The item looks like:
<buffer-name> *%# <file-name>
384 385 386
The `*' appears only if the buffer is marked as modified.
The `%' appears only if the buffer is read-only.
The `#' appears only version control file (SCCS/RCS)."
Richard M. Stallman's avatar
Richard M. Stallman committed
387 388 389 390 391 392 393 394
  (format (format "%%%ds  %%s%%s%%s  %%s" maxbuf)
          (buffer-name buffer)
          (if (buffer-modified-p) "*" " ")
          (if buffer-read-only "%" " ")
          (if (and (boundp 'vc-mode) vc-mode) "#" " ")
          (or buffer-file-name "")))

;;;
395
;;; Some example function to be used for `msb-item-sort-function'.
Richard M. Stallman's avatar
Richard M. Stallman committed
396 397 398 399 400 401 402 403 404
;;;
(defun msb-sort-by-name (item1 item2)
  "Sorts the items depending on their buffer-name
An item look like (NAME . BUFFER)."
  (string-lessp (buffer-name (cdr item1))
		(buffer-name (cdr item2))))


(defun msb-sort-by-directory (item1 item2)
405
  "Sorts the items depending on their directory.  Made for dired.
Richard M. Stallman's avatar
Richard M. Stallman committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
An item look like (NAME . BUFFER)."
  (string-lessp (save-excursion (set-buffer (cdr item1)) (msb--dired-directory))
		(save-excursion (set-buffer (cdr item2)) (msb--dired-directory))))

;;;
;;; msb
;;;
;;; This function can be used instead of (mouse-buffer-menu EVENT)
;;; function in "mouse.el".
;;; 
(defun msb (event)
  "Pop up several menus of buffers for selection with the mouse.
This command switches buffers in the window that you clicked on, and
selects that window.

421 422
See the function `mouse-select-buffer' and the variable
`msb-menu-cond' for more information about how the menus are split."
Richard M. Stallman's avatar
Richard M. Stallman committed
423
  (interactive "e")
424
  (let ((old-window (selected-window))
Richard M. Stallman's avatar
Richard M. Stallman committed
425
	(window (posn-window (event-start event))))
426 427 428 429 430
    (unless (framep window) (select-window window))
    (let ((buffer (mouse-select-buffer event)))
      (if buffer
	  (switch-to-buffer buffer)
	(select-window old-window))))
Richard M. Stallman's avatar
Richard M. Stallman committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
  nil)

;;;
;;; Some supportive functions
;;;
(defun msb-invisible-buffer-p (&optional buffer)
  "Return t if optional BUFFER is an \"invisible\" buffer.
If the argument is left out or nil, then the current buffer is considered."
  (and (> (length (buffer-name buffer)) 0)
       (eq ?\ (aref (buffer-name buffer) 0))))

;; Strip one hierarcy level from the end of PATH.
(defun msb--strip-path (path)
  (save-match-data
    (if (string-match "\\(.+\\)/[^/]+$" path)
	(substring path (match-beginning 1) (match-end 1))
      "/")))

;; Create an alist with all buffers from LIST that lies under the same
;; directory will be in the same item as the directory string as
451
;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K BUFFER-K+1...)) ...)
Richard M. Stallman's avatar
Richard M. Stallman committed
452 453 454 455 456 457 458 459 460 461 462 463
(defun msb--init-file-alist (list)
  (let ((buffer-alist
	 (sort (mapcan
		(function
		 (lambda (buffer)
		   (let ((file-name (buffer-file-name buffer)))
		     (when file-name
		       (list (cons (msb--strip-path file-name) buffer))))))
		list)
	       (function (lambda (item1 item2)
			   (string< (car item1) (car item2)))))))
    ;; Make alist that looks like
464
    ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K)) ...)
Richard M. Stallman's avatar
Richard M. Stallman committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    (let ((path nil)
	  (buffers nil)
	  (result nil))
      (append
       (mapcan (function
	       (lambda (item)
		 (cond
		  ((and path
			(string= path (car item)))
		   (push (cdr item) buffers)
		   nil)
		  (t
		   (when path
		     (setq result (cons path buffers)))
		   (setq path (car item))
		   (setq buffers (list (cdr item)))
		   (and result (list result))))))
	      buffer-alist)
       (list (cons path buffers))))))

;; Choose file-menu with respect to directory for every buffer in LIST.
(defun msb--choose-file-menu (list)
  (let ((buffer-alist (msb--init-file-alist list))
	(final-list nil)
	(max-clumped-together (if (numberp msb-max-file-menu-items)
				  msb-max-file-menu-items
				10))
	(top-found-p nil)
	(last-path nil)
	first rest path buffers)
    (setq first (car buffer-alist))
    (setq rest (cdr buffer-alist))
    (setq path (car first))
    (setq buffers (cdr first))
    (while rest
      (let ((found-p nil)
	    (tmp-rest rest)
	    new-path item)
	(setq item (car tmp-rest))
	(while (and tmp-rest
		    (<= (length buffers) max-clumped-together)
		    (>= (length (car item)) (length path))
		    (string= path (substring (car item) 0 (length path))))
	  (setq found-p t)
	  (setq buffers (append buffers (cdr item)))
	  (setq tmp-rest (cdr tmp-rest))
	  (setq item (car tmp-rest)))
	(cond
	 ((> (length buffers) max-clumped-together)
	  (setq last-path (car first))
515 516 517 518 519 520 521 522
	  (setq first
		(cons (format (if top-found-p
				  "%s/... (%d)"
				"%s (%d)")
			      (car first)
			      (length (cdr first)))
		      (cdr first)))
	  (setq top-found-p nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	  (push first final-list)
	  (setq first (car rest)
		rest (cdr rest))
	  (setq path (car first)
		buffers (cdr first)))
	 (t
	  (when found-p
	    (setq top-found-p t)
	    (setq first (cons path buffers)
		  rest tmp-rest))
	  (setq path (msb--strip-path path)
		buffers (cdr first))
	  (when (and last-path
		     (or (and (>= (length path) (length last-path))
			      (string= last-path
				       (substring path 0 (length last-path))))
			 (and (< (length path) (length last-path))
			      (string= path
				       (substring last-path 0 (length path))))))
			 
543 544 545 546 547 548 549 550
	    (setq first
		  (cons (format (if top-found-p
				    "%s/... (%d)"
				  "%s (%d)")
				(car first)
				(length (cdr first)))
			(cdr first)))
	    (setq top-found-p nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
551 552 553 554 555
	    (push first final-list)
	    (setq first (car rest)
		  rest (cdr rest))
	    (setq path (car first)
		buffers (cdr first)))))))
556 557 558 559 560 561 562 563
    (setq first
	  (cons (format (if top-found-p
			    "%s/... (%d)"
			  "%s (%d)")
			(car first)
			(length (cdr first)))
		(cdr first)))
    (setq top-found-p nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
564 565 566 567 568
    (push first final-list)
    (nreverse final-list)))

;; Create a vector as:
;; [BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER)
569 570
;; from an element in `msb-menu-cond'.  See that variable for a
;; description of its elements.
Richard M. Stallman's avatar
Richard M. Stallman committed
571 572 573 574 575 576 577 578 579 580 581 582
(defun msb--create-function-info (menu-cond-elt)
  (let* ((list-symbol (make-symbol "-msb-buffer-list"))
	 (tmp-ih (and (> (length menu-cond-elt) 3)
		      (nth 3 menu-cond-elt)))
	 (item-handler (if (and tmp-ih (fboundp tmp-ih))
			   tmp-ih
			 msb-item-handling-function))
	 (tmp-s (if (> (length menu-cond-elt) 4)
		    (nth 4 menu-cond-elt)
		  msb-item-sort-function))
	 (sorter (if (or (fboundp tmp-s)
			 (null tmp-s)
583
			 (eq tmp-s t))
Richard M. Stallman's avatar
Richard M. Stallman committed
584 585 586 587 588 589 590 591 592 593
		    tmp-s
		   msb-item-sort-function)))
    (when (< (length menu-cond-elt) 3)
      (error "Wrong format of msb-menu-cond."))
    (when (and (> (length menu-cond-elt) 3)
	       (not (fboundp tmp-ih)))
      (signal 'invalid-function (list tmp-ih)))
    (when (and (> (length menu-cond-elt) 4)
	       tmp-s
	       (not (fboundp tmp-s))
594
	       (not (eq tmp-s t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
595
      (signal 'invalid-function (list tmp-s)))
596
    (set list-symbol ())
Richard M. Stallman's avatar
Richard M. Stallman committed
597 598 599 600 601 602 603 604 605
    (vector list-symbol			;BUFFER-LIST-VARIABLE
	    (nth 0 menu-cond-elt)	;CONDITION
	    (nth 1 menu-cond-elt)	;SORT-KEY
	    (nth 2 menu-cond-elt)	;MENU-TITLE
	    item-handler		;ITEM-HANDLER
	    sorter)			;SORTER
    ))

;; This defsubst is only used in `msb--choose-menu' below.  It was
606
;; pulled out merely to make the code somewhat clearer.  The indention
Richard M. Stallman's avatar
Richard M. Stallman committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620
;; level was too big.
(defsubst msb--collect (function-info-vector)
  (let ((result nil)
	(multi-flag nil)
	function-info-list)
    (setq function-info-list
	  (loop for fi
		across function-info-vector
		if (and (setq result
			      (eval (aref fi 1))) ;Test CONDITION
			(not (and (eq result 'no-multi)
				  multi-flag))
			(progn (when (eq result 'multi)
				 (setq multi-flag t))
621
			       t))
Richard M. Stallman's avatar
Richard M. Stallman committed
622 623 624 625 626 627 628 629 630
		collect fi
		until (and result
			   (not (eq result 'multi)))))
    (when (and (not function-info-list)
	       (not result))
      (error "No catch-all in msb-menu-cond!"))
    function-info-list))

;; Adds BUFFER to the menu depicted by FUNCTION-INFO
631
;; All side-effects.  Adds an element of form (BUFFER-TITLE . BUFFER)
Richard M. Stallman's avatar
Richard M. Stallman committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
;; to the buffer-list variable in function-info.
(defun msb--add-to-menu (buffer function-info max-buffer-name-length)
  (let ((list-symbol (aref function-info 0))) ;BUFFER-LIST-VARIABLE
    ;; Here comes the hairy side-effect!
    (set list-symbol
	 (cons (cons (funcall (aref function-info 4) ;ITEM-HANDLER
			      buffer
			      max-buffer-name-length)
		     buffer)
	       (eval list-symbol)))))
  
;; Selects the appropriate menu for BUFFER.
;; This is all side-effects, folks!
;; This should be optimized.
(defsubst msb--choose-menu (buffer function-info-vector max-buffer-name-length)
  (unless (and (not msb-display-invisible-buffers-p)
	       (msb-invisible-buffer-p buffer))
    (condition-case nil
	(save-excursion
	  (set-buffer buffer)
652
	  ;; Menu found.  Add to this menu
Richard M. Stallman's avatar
Richard M. Stallman committed
653 654 655 656 657 658 659
	  (mapc (function
		 (lambda (function-info)
		   (msb--add-to-menu buffer function-info max-buffer-name-length)))
		(msb--collect function-info-vector)))
      (error (unless msb--error
	       (setq msb--error
		     (format
660
		      "In msb-menu-cond, error for buffer `%s'."
Richard M. Stallman's avatar
Richard M. Stallman committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
		      (buffer-name buffer)))
	       (error msb--error))))))

;; Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the
;; buffer-list is empty.
(defun msb--create-sort-item (function-info)
  (let ((buffer-list (eval (aref function-info 0))))
    (when buffer-list
      (let ((sorter (aref function-info 5)) ;SORTER
	    (sort-key (aref function-info 2))) ;MENU-SORT-KEY
	(when sort-key
	  (cons sort-key	
		(cons (format (aref function-info 3) ;MENU-TITLE
			      (length buffer-list))
		      (cond
		       ((null sorter)
			buffer-list)
678
		       ((eq sorter t)
Richard M. Stallman's avatar
Richard M. Stallman committed
679 680 681 682 683 684 685
			(nreverse buffer-list))
		       (t
			(sort buffer-list sorter))))))))))

;; Returns a list on the form ((TITLE . BUFFER-LIST)) for
;; the most recently used buffers.
(defun msb--most-recently-used-menu (max-buffer-name-length)
686 687
  (when (and (numberp msb-display-most-recently-used)
 	     (> msb-display-most-recently-used 0))
688 689
    (let* ((buffers (cdr (buffer-list)))
	   (most-recently-used
Richard M. Stallman's avatar
Richard M. Stallman committed
690
	    (loop with n = 0
691
		  for buffer in buffers
Richard M. Stallman's avatar
Richard M. Stallman committed
692 693 694 695 696 697 698 699 700 701 702
		  if (save-excursion
		       (set-buffer buffer)
		       (and (not (msb-invisible-buffer-p))
			    (not (eq major-mode 'dired-mode))))
		  collect (save-excursion
			    (set-buffer buffer)
			    (cons (funcall msb-item-handling-function
					   buffer
					   max-buffer-name-length)
				  buffer))
		  and do (incf n)
703
		  until (>= n msb-display-most-recently-used))))
Richard M. Stallman's avatar
Richard M. Stallman committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
      (cons (if (stringp msb-most-recently-used-title)
		(format msb-most-recently-used-title
			(length most-recently-used))
	      (signal 'wrong-type-argument (list msb-most-recently-used-title)))
	    most-recently-used))))

(defun msb--create-buffer-menu-2 ()
  (let ((max-buffer-name-length 0)
	file-buffers
	function-info-vector)
    ;; Calculate the longest buffer name.
    (mapc
     (function
      (lambda (buffer)
	(if (or msb-display-invisible-buffers-p
		(not (msb-invisible-buffer-p)))
	    (setq max-buffer-name-length
		  (max max-buffer-name-length
		       (length (buffer-name buffer)))))))
     (buffer-list))
    ;; Make a list with elements of type
    ;; (BUFFER-LIST-VARIABLE
    ;;  CONDITION
    ;;  MENU-SORT-KEY
    ;;  MENU-TITLE
    ;;  ITEM-HANDLER
    ;;  SORTER)
    ;; Uses "function-global" variables:
    ;; function-info-vector
    (setq function-info-vector
	  (apply (function vector)
		 (mapcar (function msb--create-function-info)
			 msb-menu-cond)))
    ;; Split the buffer-list into several lists; one list for each
738
    ;; criteria.  This is the most critical part with respect to time.
Richard M. Stallman's avatar
Richard M. Stallman committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    (mapc (function (lambda (buffer)
		      (cond ((and msb-files-by-directory
				  (buffer-file-name buffer))
			     (push buffer file-buffers))
			    (t
			     (msb--choose-menu buffer
					       function-info-vector
					       max-buffer-name-length)))))
	  (buffer-list))
    (when file-buffers
      (setq file-buffers
	    (mapcar (function
		     (lambda (buffer-list)
		       (cons msb-files-by-directory-sort-key
			     (cons (car buffer-list)
				   (sort
				    (mapcar (function
					     (lambda (buffer)
757 758 759 760 761
					       (cons (save-excursion
						       (set-buffer buffer)
						       (funcall msb-item-handling-function
							      buffer
							      max-buffer-name-length))
Richard M. Stallman's avatar
Richard M. Stallman committed
762 763 764 765 766 767 768
						     buffer)))
					    (cdr buffer-list))
				    (function
				     (lambda (item1 item2)
				       (string< (car item1) (car item2)))))))))
		     (msb--choose-file-menu file-buffers))))
    ;; Now make the menu - a list of (TITLE . BUFFER-LIST)
769
    (let* (menu
Richard M. Stallman's avatar
Richard M. Stallman committed
770 771 772 773
	   (most-recently-used
	    (msb--most-recently-used-menu max-buffer-name-length))
	   (others (append file-buffers
			   (loop for elt
774 775 776
				 across function-info-vector
				 for value = (msb--create-sort-item elt)
				 if value collect value))))
Richard M. Stallman's avatar
Richard M. Stallman committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
      (setq menu
	    (mapcar 'cdr		;Remove the SORT-KEY
		    ;; Sort the menus - not the items.
		    (msb--add-separators
		    (sort
		     ;; Get a list of (SORT-KEY TITLE . BUFFER-LIST)
		     ;; Also sorts the items within the menus.
		     (if (cdr most-recently-used)
			 (cons
			  ;; Add most recent used buffers
			  (cons msb-most-recently-used-sort-key
				most-recently-used)
			  others)
		       others)
		     (function (lambda (elt1 elt2)
				 (< (car elt1) (car elt2))))))))
      ;; Now make it a keymap menu
      (append
       '(keymap "Select Buffer")
       (msb--make-keymap-menu menu)
       (when msb-separator-diff
	 (list (list 'separator "---")))
       (list (cons 'toggle 
		   (cons
		   (if msb-files-by-directory
		       "*Files by type*"
		     "*Files by directory*")
		   'msb--toggle-menu-type)))))))

(defun msb--create-buffer-menu  ()
  (save-match-data
    (save-excursion
      (msb--create-buffer-menu-2))))

;;;
;;; Multi purpose function for selecting a buffer with the mouse.
;;; 
(defun msb--toggle-menu-type ()
  (interactive)
  (setq msb-files-by-directory (not msb-files-by-directory))
817
  (menu-bar-update-buffers))
Richard M. Stallman's avatar
Richard M. Stallman committed
818 819 820 821 822

(defun mouse-select-buffer (event)
  "Pop up several menus of buffers, for selection with the mouse.
Returns the selected buffer or nil if no buffer is selected.

823
The way the buffers are split is conveniently handled with the
824
variable `msb-menu-cond'."
Richard M. Stallman's avatar
Richard M. Stallman committed
825 826 827 828 829 830 831
  ;; Popup the menu and return the selected buffer.
  (when (or msb--error
	    (not msb--last-buffer-menu)
	    (not (fboundp 'frame-or-buffer-changed-p))
	    (frame-or-buffer-changed-p))
    (setq msb--error nil)
    (setq msb--last-buffer-menu (msb--create-buffer-menu)))
832 833
  (let ((position event)
	choice)
Richard M. Stallman's avatar
Richard M. Stallman committed
834 835 836 837
    (when (and (fboundp 'posn-x-y)
	       (fboundp 'posn-window))
      (let ((posX (car (posn-x-y (event-start event))))
	    (posY (cdr (posn-x-y (event-start event))))
838
	    (posWind (posn-window (event-start event))))
Richard M. Stallman's avatar
Richard M. Stallman committed
839 840 841
	;; adjust position
	(setq posX (- posX (funcall msb-horizontal-shift-function))
	      position (list (list posX posY) posWind))))
842 843 844 845
    ;; This `sit-for' magically makes the menu stay up if the mouse
    ;; button is released withing 0.1 second.
    (sit-for 0 100)
    ;; Popup the menu
846
    (setq choice (x-popup-menu position msb--last-buffer-menu))
Richard M. Stallman's avatar
Richard M. Stallman committed
847
    (cond
848 849 850 851 852 853 854
     ((eq (car choice) 'toggle)
      ;; Bring up the menu again with type toggled.
      (msb--toggle-menu-type)
      (mouse-select-buffer event))
     ((and (numberp (car choice))
	   (null (cdr choice)))
      (let ((msb--last-buffer-menu (nthcdr 3 (assq (car choice) msb--last-buffer-menu))))
Richard M. Stallman's avatar
Richard M. Stallman committed
855
	(mouse-select-buffer event)))
856 857 858 859 860
     ((while (numberp (car choice))
	(setq choice (cdr choice))))
     ((and (stringp (car choice))
	   (null (cdr choice)))
      (car choice))
861 862
     ((null choice)
      choice)
863 864
     (t
      (error "Unknown form for buffer: %s" choice)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
		    
;; Add separators
(defun msb--add-separators (sorted-list)
  (cond
   ((or (not msb-separator-diff)
	(not (numberp msb-separator-diff)))
    sorted-list)
   (t
    (let ((last-key nil))
      (mapcan
       (function
	(lambda (item)
	  (cond
	   ((and msb-separator-diff
		 last-key 
		 (> (- (car item) last-key)
		    msb-separator-diff))
	    (setq last-key (car item))
	    (list (cons last-key 'separator)
		  item))
	   (t
	    (setq last-key (car item))
	    (list item)))))
       sorted-list)))))

890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
(defun msb--split-menus-2 (list mcount result)
  (cond
   ((> (length list) msb-max-menu-items)
    (let ((count 0)
	  sub-name
	  (tmp-list nil))
      (while (< count msb-max-menu-items)
	(push (pop list) tmp-list)
	(incf count))
    (setq tmp-list (nreverse tmp-list))
    (setq sub-name (concat (car (car tmp-list)) "..."))
    (push (append (list mcount sub-name
			'keymap sub-name)
		  tmp-list)
	  result))
    (msb--split-menus-2 list (1+ mcount) result))
   ((null result)
    list)
   (t
    (let (sub-name)
      (setq sub-name (concat (car (car list)) "..."))
      (push (append (list mcount sub-name
			'keymap sub-name)
		  list)
	  result))
    (nreverse result))))
    
(defun msb--split-menus (list)
 (msb--split-menus-2 list 0 nil))


Richard M. Stallman's avatar
Richard M. Stallman committed
921 922 923 924 925 926 927 928 929 930
(defun msb--make-keymap-menu (raw-menu)
  (let ((end (cons '(nil) 'menu-bar-select-buffer))
	(mcount 0))
    (mapcar
     (function
      (lambda (sub-menu)
	(cond 
	 ((eq 'separator sub-menu)
	  (list 'separator "---"))
	 (t
931 932 933 934 935 936 937 938 939 940
	  (let ((buffers (mapcar (function
				  (lambda (item)
				    (let ((string (car item))
					  (buffer (cdr item)))
				      (cons (buffer-name buffer)
					    (cons string end)))))
				 (cdr sub-menu))))
	    (append (list (incf mcount) (car sub-menu)
			  'keymap (car sub-menu))
		    (msb--split-menus buffers)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
941 942 943 944 945 946 947 948
     raw-menu)))

(defun menu-bar-update-buffers (&optional arg)
  ;; If user discards the Buffers item, play along.
  (when (and (lookup-key (current-global-map) [menu-bar buffer])
	     (or (not (fboundp 'frame-or-buffer-changed-p))
		 (frame-or-buffer-changed-p)
		 arg))
949
    (let ((frames (frame-list))
Richard M. Stallman's avatar
Richard M. Stallman committed
950 951 952 953 954
	  buffers-menu frames-menu)
      ;; Make the menu of buffers proper.
      (setq msb--last-buffer-menu (msb--create-buffer-menu))
      (setq buffers-menu msb--last-buffer-menu)
      ;; Make a Frames menu if we have more than one frame.
955 956 957 958 959 960 961 962
      (when (cdr frames)
	(let* ((frame-length (length frames))
	       (f-title  (format "Frames (%d)" frame-length)))
	  ;; List only the N most recently selected frames
	  (when (and (integerp msb-max-menu-items)
		     (>  msb-max-menu-items 1)
		     (> frame-length msb-max-menu-items))
	    (setcdr (nthcdr msb-max-menu-items frames) nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
963
	  (setq frames-menu
964 965 966 967 968 969 970 971 972 973 974 975
		(nconc
		 (list 'frame f-title '(nil) 'keymap f-title)
		 (mapcar
		  (function
		   (lambda (frame)
		     (nconc
		      (list frame
			    (cdr (assq 'name
				       (frame-parameters frame)))
			    (cons nil nil))
		      'menu-bar-select-frame)))
		  frames)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
976 977 978
      (define-key (current-global-map) [menu-bar buffer]
	(cons "Buffers"
	      (if (and buffers-menu frames-menu)
979 980 981 982 983
		  ;; Combine Frame and Buffers menus with separator between
		  (nconc (list 'keymap "Buffers and Frames" frames-menu
			       (and msb-separator-diff '(separator "---")))
			 (cddr buffers-menu))
		(or buffers-menu 'undefined)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003

(when (and (boundp 'menu-bar-update-hook)
	   (not (fboundp 'frame-or-buffer-changed-p)))
  (defvar msb--buffer-count 0)
  (defun frame-or-buffer-changed-p ()
    (let ((count (length (buffer-list))))
      (when (/= count msb--buffer-count)
        (setq msb--buffer-count count)
        t))))

(unless (or (not (boundp 'menu-bar-update-hook))
	    (memq 'menu-bar-update-buffers menu-bar-update-hook))
    (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers))

(and (fboundp 'mouse-buffer-menu)
     (substitute-key-definition 'mouse-buffer-menu 'msb (current-global-map)))

(provide 'msb)
(eval-after-load 'msb (run-hooks 'msb-after-load-hooks))
;;; msb.el ends here