]> code.delx.au - gnu-emacs/blobdiff - lisp/mouse.el
(tar-mode): Position point on the name of the first file.
[gnu-emacs] / lisp / mouse.el
index 3531329cdd7fd1017ad8536909b87fd335d1ac31..5d55b3dfd0e9abbb1680184588c2c835b90b239d 100644 (file)
@@ -1,25 +1,26 @@
-;;; mouse.el --- window system-independent mouse support.
+;;; mouse.el --- window system-independent mouse support
 
-;;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
+;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; Keywords: hardware
 
-;;; This file is part of GNU Emacs.
+;; This file is part of GNU Emacs.
 
-;;; GNU Emacs 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, or (at your option)
-;;; any later version.
+;; GNU Emacs 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, or (at your option)
+;; any later version.
 
-;;; GNU Emacs 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.
+;; GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to
-;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
 ;;; Indent track-mouse like progn.
 (put 'track-mouse 'lisp-indent-function 0)
 
-(defvar mouse-yank-at-point nil
-  "*If non-nil, mouse yank commands yank at point instead of at click.")
+(defcustom mouse-yank-at-point nil
+  "*If non-nil, mouse yank commands yank at point instead of at click."
+  :type 'boolean
+  :group 'mouse)
 \f
 ;; Provide a mode-specific menu on a mouse button.
 
-(defun mouse-major-mode-menu (event)
+(defun mouse-major-mode-menu (event prefix)
   "Pop up a mode-specific menu of mouse commands."
   ;; Switch to the window clicked on, because otherwise
   ;; the mode's commands may not make sense.
-  (interactive "@e")
-  (let ((newmap (make-sparse-keymap))
-       (unread-command-events (list event)))
-    ;; Make a keymap in which our last command leads to a menu
-    (define-key newmap (vector (car event))
-      (nconc (make-sparse-keymap "Menu")
-            (mouse-major-mode-menu-1
-             (and (current-local-map)
-                  (lookup-key (current-local-map) [menu-bar])))))
-    (mouse-major-mode-menu-compute-equiv-keys newmap)
-    ;; Make NEWMAP override the usual definition
-    ;; of the mouse button that got us here.
-    ;; Then read the user's menu choice.
-    (let* ((minor-mode-map-alist
-           (cons (cons t newmap) minor-mode-map-alist))
-          ;; read-key-sequence quits if the user aborts the menu.
-          ;; If that happens, do nothing silently.
-          (keyseq (condition-case nil
-                      (read-key-sequence "")
-                    (quit nil)))
-          (command (if keyseq (lookup-key newmap keyseq))))
-      (if command
-         (command-execute command)))))
+  (interactive "@e\nP")
+  ;; Let the mode update its menus first.
+  (run-hooks 'activate-menubar-hook)
+  (let (;; This is where mouse-major-mode-menu-prefix
+       ;; returns the prefix we should use (after menu-bar).
+       ;; It is either nil or (SOME-SYMBOL).
+       (mouse-major-mode-menu-prefix nil)
+       ;; Make a keymap in which our last command leads to a menu
+       (newmap (make-sparse-keymap (concat mode-name " Mode")))
+       result)
+    ;; Make our menu inherit from the desired keymap
+    ;; which we want to display as the menu now.
+    (set-keymap-parent newmap
+                      (mouse-major-mode-menu-1
+                       (and (current-local-map)
+                            (lookup-key (current-local-map) [menu-bar]))))
+    (setq result (x-popup-menu t (list newmap)))
+    (if result
+       (let ((command (key-binding
+                       (apply 'vector (append '(menu-bar)
+                                              mouse-major-mode-menu-prefix
+                                              result)))))
+         ;; Clear out echoing, which perhaps shows a prefix arg.
+         (message "")
+         (if command
+             (progn
+               (setq prefix-arg prefix)
+               (command-execute command)))))))
 
 ;; Compute and cache the equivalent keys in MENU and all its submenus.
-(defun mouse-major-mode-menu-compute-equiv-keys (menu)
-  (and (eq (car menu) 'keymap)
-       (x-popup-menu nil menu))
-  (while menu
-    (and (consp (car menu))
-        (consp (cdr (car menu)))
-        (let ((tail (cdr (car menu))))
-          (while (and (consp tail)
-                      (not (eq (car tail) 'keymap)))
-            (setq tail (cdr tail)))
-          (if (consp tail)
-              (mouse-major-mode-menu-compute-equiv-keys tail))))
-    (setq menu (cdr menu))))
+;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
+;;;  (and (eq (car menu) 'keymap)
+;;;       (x-popup-menu nil menu))
+;;;  (while menu
+;;;    (and (consp (car menu))
+;;;     (consp (cdr (car menu)))
+;;;     (let ((tail (cdr (car menu))))
+;;;       (while (and (consp tail)
+;;;                   (not (eq (car tail) 'keymap)))
+;;;         (setq tail (cdr tail)))
+;;;       (if (consp tail)
+;;;           (mouse-major-mode-menu-compute-equiv-keys tail))))
+;;;    (setq menu (cdr menu))))
 
 ;; Given a mode's menu bar keymap,
 ;; if it defines exactly one menu bar menu,
          (if (consp (car tail))
              (if submap
                  (setq submap t)
-               (setq submap (cdr (car tail)))))
+               (setq submap (car tail))))
          (setq tail (cdr tail)))
-       (if (eq submap t) menubar
-         submap))))
+       (if (eq submap t)
+           menubar
+         (setq mouse-major-mode-menu-prefix (list (car submap)))
+         (cdr (cdr submap))))))
 \f
 ;; Commands that operate on windows.
 
@@ -128,8 +138,7 @@ This must be bound to a mouse click."
     (raise-frame frame)
     (select-frame frame)
     (or (eq frame oframe)
-       (set-mouse-position (selected-frame) (1- (frame-width)) 0))
-    (unfocus-frame)))
+       (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
 
 (defun mouse-tear-off-window (click)
   "Delete the window clicked on, and create a new frame displaying its buffer."
@@ -193,9 +202,7 @@ This command must be bound to a mouse click."
        should-enlarge-minibuffer
        event mouse minibuffer y top bot edges wconfig params growth)
     (setq params (frame-parameters))
-    (if (and (not (setq minibuffer (cdr (assq 'minibuffer params))))
-            (one-window-p t))
-       (error "Attempt to resize sole window"))
+    (setq minibuffer (cdr (assq 'minibuffer params)))
     (track-mouse
       (progn
        ;; enlarge-window only works on the selected window, so
@@ -251,6 +258,11 @@ This command must be bound to a mouse click."
                 ;; compute size change needed
                 (setq growth (- y bot -1)
                       wconfig (current-window-configuration))
+                ;; Check for an error case.
+                (if (and (/= growth 0)
+                         (not minibuffer)
+                         (one-window-p t))
+                    (error "Attempt to resize sole window"))
                 ;; grow/shrink minibuffer?
                 (if should-enlarge-minibuffer
                     (progn
@@ -290,18 +302,24 @@ This command must be bound to a mouse click."
   (interactive "e")
   ;; Give temporary modes such as isearch a chance to turn off.
   (run-hooks 'mouse-leave-buffer-hook)
-  (let ((done nil)
-       (echo-keystrokes 0)
-       (start-event-frame (window-frame (car (car (cdr start-event)))))
-       (start-event-window (car (car (cdr start-event))))
-       (start-nwindows (count-windows t))
-       (old-selected-window (selected-window))
-       event mouse x left right edges wconfig growth)
+  (let* ((done nil)
+        (echo-keystrokes 0)
+        (start-event-frame (window-frame (car (car (cdr start-event)))))
+        (start-event-window (car (car (cdr start-event))))
+        (start-nwindows (count-windows t))
+        (old-selected-window (selected-window))
+        event mouse x left right edges wconfig growth
+        (which-side
+         (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
+             'right)))
     (if (one-window-p t)
        (error "Attempt to resize sole ordinary window"))
-    (if (= (nth 2 (window-edges start-event-window))
-          (frame-width start-event-frame))
-       (error "Attempt to drag rightmost scrollbar"))
+    (if (eq which-side 'left)
+       (if (= (nth 0 (window-edges start-event-window)) 0)
+           (error "Attempt to drag leftmost scrollbar"))
+      (if (= (nth 2 (window-edges start-event-window))
+            (frame-width start-event-frame))
+         (error "Attempt to drag rightmost scrollbar")))
     (track-mouse
       (progn
        ;; enlarge-window only works on the selected window, so
@@ -339,28 +357,34 @@ This command must be bound to a mouse click."
                ((null (car (cdr mouse)))
                 nil)
                (t
-                (setq x (car (cdr mouse))
-                      edges (window-edges)
-                      left (nth 0 edges)
-                      right (nth 2 edges))
-                ;; scale back a move that would make the
-                ;; window too thin.
-                (cond ((< (- x left -1) window-min-width)
-                       (setq x (+ left window-min-width -1))))
-                ;; compute size change needed
-                (setq growth (- x right -1)
-                      wconfig (current-window-configuration))
-                (enlarge-window growth t)
-                ;; if this window's growth caused another
-                ;; window to be deleted because it was too
-                ;; thin, rescind the change.
-                ;;
-                ;; if size change caused space to be stolen
-                ;; from a window to the left of this one,
-                ;; rescind the change.
-                (if (or (/= start-nwindows (count-windows t))
-                        (/= left (nth 0 (window-edges))))
-                    (set-window-configuration wconfig)))))))))
+                (save-selected-window
+                  ;; If the scroll bar is on the window's left,
+                  ;; adjust the window on the left.
+                  (if (eq which-side 'left)
+                      (select-window (previous-window)))
+                  (setq x (- (car (cdr mouse))
+                             (if (eq which-side 'left) 2 0))
+                        edges (window-edges)
+                        left (nth 0 edges)
+                        right (nth 2 edges))
+                  ;; scale back a move that would make the
+                  ;; window too thin.
+                  (if (< (- x left -1) window-min-width)
+                      (setq x (+ left window-min-width -1)))
+                  ;; compute size change needed
+                  (setq growth (- x right -1)
+                        wconfig (current-window-configuration))
+                  (enlarge-window growth t)
+                  ;; if this window's growth caused another
+                  ;; window to be deleted because it was too
+                  ;; thin, rescind the change.
+                  ;;
+                  ;; if size change caused space to be stolen
+                  ;; from a window to the left of this one,
+                  ;; rescind the change.
+                  (if (or (/= start-nwindows (count-windows t))
+                          (/= left (nth 0 (window-edges))))
+                      (set-window-configuration wconfig))))))))))
 \f
 (defun mouse-set-point (event)
   "Move point to the position clicked on with the mouse.
@@ -400,8 +424,7 @@ This should be bound to a mouse drag event."
     ;; If mark is highlighted, no need to bounce the cursor.
     ;; On X, we highlight while dragging, thus once again no need to bounce.
     (or transient-mark-mode
-       (eq (framep (selected-frame)) 'x)
-       (eq (framep (selected-frame)) 'pc)
+       (memq (framep (selected-frame)) '(x pc w32))
        (sit-for 1))
     (push-mark)
     (set-mark (point))
@@ -410,7 +433,7 @@ This should be bound to a mouse drag event."
     ;; Don't set this-command to kill-region, so that a following
     ;; C-w will not double the text in the kill ring.
     ;; Ignore last-command so we don't append to a preceding kill.
-    (let (this-command last-command)
+    (let (this-command last-command deactivate-mark)
       (copy-region-as-kill (mark) (point)))
     (mouse-set-region-1)))
 
@@ -419,22 +442,26 @@ This should be bound to a mouse drag event."
   (setq mouse-last-region-end (region-end))
   (setq mouse-last-region-tick (buffer-modified-tick)))
 
-(defvar mouse-scroll-delay 0.25
+(defcustom mouse-scroll-delay 0.25
   "*The pause between scroll steps caused by mouse drags, in seconds.
 If you drag the mouse beyond the edge of a window, Emacs scrolls the
 window to bring the text beyond that edge into view, with a delay of
 this many seconds between scroll steps.  Scrolling stops when you move
 the mouse back into the window, or release the button.
 This variable's value may be non-integral.
-Setting this to zero causes Emacs to scroll as fast as it can.")
+Setting this to zero causes Emacs to scroll as fast as it can."
+  :type 'number
+  :group 'mouse)
 
-(defvar mouse-scroll-min-lines 1
+(defcustom mouse-scroll-min-lines 1
   "*The minimum number of lines scrolled by dragging mouse out of window.
 Moving the mouse out the top or bottom edge of the window begins
 scrolling repeatedly.  The number of lines scrolled per repetition
 is normally equal to the number of lines beyond the window edge that
 the mouse has moved.  However, it always scrolls at least the number
-of lines specified by this variable.")
+of lines specified by this variable."
+  :type 'integer
+  :group 'mouse)
 
 (defun mouse-scroll-subr (window jump &optional overlay start)
   "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
@@ -453,11 +480,13 @@ Upon exit, point is at the far edge of the newly visible text."
                 (progn
                   (set-window-start window (point))
                   (if (natnump jump)
-                      (progn
-                        (goto-char (window-end window))
-                        ;; window-end doesn't reflect the window's new
-                        ;; start position until the next redisplay.  Hurrah.
-                        (vertical-motion (1- jump) window))
+                      (if (window-end window)
+                          (progn
+                            (goto-char (window-end window))
+                            ;; window-end doesn't reflect the window's new
+                            ;; start position until the next redisplay.
+                            (vertical-motion (1- jump) window))
+                        (vertical-motion (- (window-height window) 2)))
                     (goto-char (window-start window)))
                   (if overlay
                       (move-overlay overlay start (point)))
@@ -483,11 +512,12 @@ Upon exit, point is at the far edge of the newly visible text."
   "Set the region to the text that the mouse is dragged over.
 Highlight the drag area as you move the mouse.
 This must be bound to a button-down mouse event.
-In Transient Mark mode, the highlighting remains once you
-release the mouse button.  Otherwise, it does not."
+In Transient Mark mode, the highlighting remains as long as the mark
+remains active.  Otherwise, it remains until the next input event."
   (interactive "e")
   (mouse-minibuffer-check start-event)
-  (let* ((start-posn (event-start start-event))
+  (let* ((echo-keystrokes 0)
+        (start-posn (event-start start-event))
         (start-point (posn-point start-posn))
         (start-window (posn-window start-posn))
         (start-frame (window-frame start-window))
@@ -513,7 +543,7 @@ release the mouse button.  Otherwise, it does not."
     ;; end-of-range is used only in the single-click case.
     ;; It is the place where the drag has reached so far
     ;; (but not outside the window where the drag started).
-    (let (event end end-point (end-of-range (point)))
+    (let (event end end-point last-end-point (end-of-range (point)))
       (track-mouse
        (while (progn
                 (setq event (read-event))
@@ -523,6 +553,8 @@ release the mouse button.  Otherwise, it does not."
              nil
            (setq end (event-end event)
                  end-point (posn-point end))
+           (if (numberp end-point)
+               (setq last-end-point end-point))
 
            (cond
             ;; Are we moving within the original window?
@@ -557,29 +589,44 @@ release the mouse button.  Otherwise, it does not."
            ;; Run the binding of the terminating up-event, if possible.
            ;; In the case of a multiple click, it gives the wrong results,
            ;; because it would fail to set up a region.
-           (if (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
-               ;; In this case, we can just let the up-event execute normally.
-               (let ((end (event-end event)))
-                 ;; Set the position in the event before we replay it,
-                 ;; because otherwise it may have a position in the wrong
-                 ;; buffer.
-                 (setcar (cdr end) end-of-range)
-                 ;; Delete the overlay before calling the function,
-                 ;; because delete-overlay increases buffer-modified-tick.
-                 (delete-overlay mouse-drag-overlay)
-                 (setq unread-command-events
-                       (cons event unread-command-events)))
-             (if (not (= (overlay-start mouse-drag-overlay)
+           (if (not (= (overlay-start mouse-drag-overlay)
+                       (overlay-end mouse-drag-overlay)))
+               (let* ((stop-point
+                       (if (numberp (posn-point (event-end event)))
+                           (posn-point (event-end event))
+                         last-end-point))
+                      ;; The end that comes from where we ended the drag.
+                      ;; Point goes here.
+                      (region-termination
+                       (if (and stop-point (< stop-point start-point))
+                           (overlay-start mouse-drag-overlay)
                          (overlay-end mouse-drag-overlay)))
-                 (let (last-command this-command)
-                   (push-mark (overlay-start mouse-drag-overlay) t t)
-                   (goto-char (overlay-end mouse-drag-overlay))
-                   (delete-overlay mouse-drag-overlay)
-                   (copy-region-as-kill (point) (mark t))
-                   (mouse-set-region-1))
-               (goto-char (overlay-end mouse-drag-overlay))
-               (setq this-command 'mouse-set-point)
-               (delete-overlay mouse-drag-overlay))))
+                      ;; The end that comes from where we started the drag.
+                      ;; Mark goes there.
+                      (region-commencement
+                       (- (+ (overlay-end mouse-drag-overlay)
+                             (overlay-start mouse-drag-overlay))
+                          region-termination))
+                      last-command this-command)
+                 (push-mark region-commencement t t)
+                 (goto-char region-termination)
+                 ;; Don't let copy-region-as-kill set deactivate-mark.
+                 (let (deactivate-mark)
+                   (copy-region-as-kill (point) (mark t)))
+                 (let ((buffer (current-buffer)))
+                   (mouse-show-mark)
+                   ;; mouse-show-mark can call read-event,
+                   ;; and that means the Emacs server could switch buffers
+                   ;; under us.  If that happened, 
+                   ;; avoid trying to use the region.
+                   (and (mark t) mark-active
+                        (eq buffer (current-buffer))
+                        (mouse-set-region-1))))
+             (delete-overlay mouse-drag-overlay)
+             ;; Run the binding of the terminating up-event.
+             (if (fboundp fun)
+                 (setq unread-command-events
+                       (cons event unread-command-events)))))
        (delete-overlay mouse-drag-overlay)))))
 \f
 ;; Commands to handle xterm-style multiple clicks.
@@ -635,6 +682,31 @@ If DIR is positive skip forward; if negative, skip backward."
                 (backward-sexp 1)
                 (point))
               (1+ start)))
+       ((and (= mode 1)
+              (= start end)
+             (char-after start)
+              (= (char-syntax (char-after start)) ?\"))
+        (let ((open (or (eq start (point-min))
+                        (save-excursion
+                          (goto-char (- start 1))
+                          (looking-at "\\s(\\|\\s \\|\\s>")))))
+          (if open
+              (list start
+                    (save-excursion
+                      (condition-case nil
+                          (progn 
+                            (goto-char start)
+                            (forward-sexp 1)
+                            (point))
+                        (error end))))
+            (list (save-excursion
+                    (condition-case nil
+                        (progn
+                          (goto-char (1+ start))
+                          (backward-sexp 1)
+                          (point))
+                      (error end)))
+                  (1+ start)))))
         ((= mode 1)
         (list (save-excursion
                 (goto-char start)
@@ -663,12 +735,87 @@ If DIR is positive skip forward; if negative, skip backward."
     (if (numberp (posn-point posn))
        (push-mark (posn-point posn) t t))))
 
+(defun mouse-undouble-last-event (events)
+  (let* ((index (1- (length events)))
+        (last (nthcdr index events))
+        (event (car last))
+        (basic (event-basic-type event))
+        (old-modifiers (event-modifiers event))
+        (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
+        (new
+         (if (consp event)
+             ;; Use reverse, not nreverse, since event-modifiers
+             ;; does not copy the list it returns.
+             (cons (event-convert-list (reverse (cons basic modifiers)))
+                   (cdr event))
+           event)))
+    (setcar last new)
+    (if (and (not (equal modifiers old-modifiers))
+            (key-binding (apply 'vector events)))
+       t
+      (setcar last event)
+      nil)))
+
 ;; Momentarily show where the mark is, if highlighting doesn't show it. 
+
+(defvar mouse-region-delete-keys '([delete])
+  "List of keys which shall cause the mouse region to be deleted.")
+
 (defun mouse-show-mark ()
-  (or transient-mark-mode
+  (if transient-mark-mode
+      (if window-system
+         (delete-overlay mouse-drag-overlay))
+    (if window-system
+       (let ((inhibit-quit t)
+             (echo-keystrokes 0)
+             event events key ignore
+             x-lost-selection-hooks)
+         (add-hook 'x-lost-selection-hooks
+                   '(lambda (seltype)
+                      (if (eq seltype 'PRIMARY)
+                          (progn (setq ignore t)
+                                 (throw 'mouse-show-mark t)))))
+         (move-overlay mouse-drag-overlay (point) (mark t))
+         (catch 'mouse-show-mark
+           ;; In this loop, execute scroll bar and switch-frame events.
+           ;; Also ignore down-events that are undefined.
+           (while (progn (setq event (read-event))
+                         (setq events (append events (list event)))
+                         (setq key (apply 'vector events))
+                         (or (and (consp event)
+                                  (eq (car event) 'switch-frame))
+                             (and (consp event)
+                                  (eq (posn-point (event-end event))
+                                      'vertical-scroll-bar))
+                             (and (memq 'down (event-modifiers event))
+                                  (not (key-binding key))
+                                  (not (mouse-undouble-last-event events))
+                                  (not (member key mouse-region-delete-keys)))))
+             (and (consp event)
+                  (or (eq (car event) 'switch-frame)
+                      (eq (posn-point (event-end event))
+                          'vertical-scroll-bar))
+                  (let ((keys (vector 'vertical-scroll-bar event)))
+                    (and (key-binding keys)
+                         (progn
+                           (call-interactively (key-binding keys)
+                                               nil keys)
+                           (setq events nil)))))))
+         ;; If we lost the selection, just turn off the highlighting.
+         (if ignore
+             nil
+           ;; For certain special keys, delete the region.
+           (if (member key mouse-region-delete-keys)
+               (delete-region (overlay-start mouse-drag-overlay)
+                              (overlay-end mouse-drag-overlay))
+             ;; Otherwise, unread the key so it gets executed normally.
+             (setq unread-command-events
+                   (nconc events unread-command-events))))
+         (setq quit-flag nil)
+         (delete-overlay mouse-drag-overlay))
       (save-excursion
-       (goto-char (mark t))
-       (sit-for 1))))
+       (goto-char (mark t))
+       (sit-for 1)))))
 
 (defun mouse-set-mark (click)
   "Set mark at the position clicked on with the mouse.
@@ -777,7 +924,9 @@ selection through the word or line clicked on.  If you do this
 again in a different position, it extends the selection again.
 If you do this twice in the same position, the selection is killed." 
   (interactive "e")
-  (let ((before-scroll point-before-scroll))
+  (let ((before-scroll
+        (with-current-buffer (window-buffer (posn-window (event-start click)))
+          point-before-scroll)))
     (mouse-minibuffer-check click)
     (let ((click-posn (posn-point (event-start click)))
          ;; Don't let a subsequent kill command append to this one:
@@ -826,6 +975,8 @@ If you do this twice in the same position, the selection is killed."
              (mouse-save-then-kill-delete-region (point) (mark))
              ;; After we kill, another click counts as "the first time".
              (setq mouse-save-then-kill-posn nil))
+         ;; This is not a repetition.
+         ;; We are adjusting an old selection or creating a new one.
          (if (or (and (eq last-command 'mouse-save-then-kill)
                       mouse-save-then-kill-posn)
                  (and mark-active transient-mark-mode)
@@ -852,7 +1003,9 @@ If you do this twice in the same position, the selection is killed."
            (if before-scroll
                (goto-char before-scroll))
            (exchange-point-and-mark)
-           (kill-new (buffer-substring (point) (mark t))))
+           (kill-new (buffer-substring (point) (mark t)))
+           (if window-system
+               (mouse-show-mark)))
          (mouse-set-region-1)
          (setq mouse-save-then-kill-posn
                (list (car kill-ring) (point) click-posn)))))))
@@ -912,10 +1065,12 @@ This must be bound to a mouse drag event."
 (defun mouse-drag-secondary (start-event)
   "Set the secondary selection to the text that the mouse is dragged over.
 Highlight the drag area as you move the mouse.
-This must be bound to a button-down mouse event."
+This must be bound to a button-down mouse event.
+The function returns a non-nil value if it creates a secondary selection."
   (interactive "e")
   (mouse-minibuffer-check start-event)
-  (let* ((start-posn (event-start start-event))
+  (let* ((echo-keystrokes 0)
+        (start-posn (event-start start-event))
         (start-point (posn-point start-posn))
         (start-window (posn-window start-posn))
         (start-frame (window-frame start-window))
@@ -982,9 +1137,6 @@ This must be bound to a button-down mouse event."
                                        mouse-secondary-overlay start-point)))))))))
 
        (if (consp event)
-;;;             (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
-;;;             (eq (posn-window (event-end event)) start-window)
-;;;             (numberp (posn-point (event-end event)))
            (if (marker-position mouse-secondary-start)
                (save-window-excursion
                  (delete-overlay mouse-secondary-overlay)
@@ -992,7 +1144,8 @@ This must be bound to a button-down mouse event."
                  (select-window start-window)
                  (save-excursion
                    (goto-char mouse-secondary-start)
-                   (sit-for 1)))
+                   (sit-for 1)
+                   nil))
              (x-set-selection
               'SECONDARY
               (buffer-substring (overlay-start mouse-secondary-overlay)
@@ -1132,8 +1285,9 @@ again.  If you do this twice in the same position, it kills the selection."
                    (kill-new (buffer-substring
                               (overlay-start mouse-secondary-overlay)
                               (overlay-end mouse-secondary-overlay)) t)
-                 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
-                                      (overlay-end mouse-secondary-overlay))))
+                 (let (deactivate-mark)
+                   (copy-region-as-kill (overlay-start mouse-secondary-overlay)
+                                        (overlay-end mouse-secondary-overlay)))))
            (if mouse-secondary-start
                ;; All we have is one end of a selection,
                ;; so put the other end here.
@@ -1151,56 +1305,182 @@ again.  If you do this twice in the same position, it kills the selection."
                            (overlay-start mouse-secondary-overlay)
                            (overlay-end mouse-secondary-overlay)))))))
 \f
+(defcustom mouse-buffer-menu-maxlen 20
+  "*Number of buffers in one pane (submenu) of the buffer menu.
+If we have lots of buffers, divide them into groups of
+`mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
+  :type 'integer
+  :group 'mouse)
+
+(defcustom mouse-buffer-menu-mode-mult 4
+  "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
+This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
+will split the buffer menu by the major modes (see
+`mouse-buffer-menu-mode-groups') or just by menu length.
+Set to 1 (or even 0!) if you want to group by major mode always, and to
+a large number if you prefer a mixed multitude.  The default is 4."
+  :type 'integer
+  :group 'mouse
+  :version "20.3")
+
+(defvar mouse-buffer-menu-mode-groups
+  '(("Info\\|Help\\|Apropos\\|Man" . "Help")
+    ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
+     . "Mail/News")
+    ("\\<C\\>" . "C")
+    ("ObjC" . "C")
+    ("Text" . "Text")
+    ("Outline" . "Text")
+    ("Lisp" . "Lisp"))
+  "How to group various major modes together in \\[mouse-buffer-menu].
+Each element has the form (REGEXP . GROUPNAME).
+If the major mode's name string matches REGEXP, use GROUPNAME instead.")
+
 (defun mouse-buffer-menu (event)
   "Pop up a menu of buffers for selection with the mouse.
 This switches buffers in the window that you clicked on,
 and selects that window."
   (interactive "e")
   (mouse-minibuffer-check event)
-  (let ((menu
-        (list "Buffer Menu"
-              (cons "Select Buffer"
-                    (let ((tail (buffer-list))
-                          (maxbuf 0)
-                          head)
-                      (while tail
-                        (or (eq ?\ (aref (buffer-name (car tail)) 0))
-                            (setq maxbuf
-                                  (max maxbuf
-                                       (length (buffer-name (car tail))))))
-                        (setq tail (cdr tail)))
-                      (setq tail (buffer-list))
-                      (while tail
-                        (let ((elt (car tail)))
-                          (if (not (string-match "^ "
-                                                 (buffer-name elt)))
-                              (setq head
-                               (cons
-                                (cons
-                                 (format
-                                  (format "%%%ds  %%s%%s  %%s" maxbuf)
-                                  (buffer-name elt)
-                                  (if (buffer-modified-p elt) "*" " ")
-                                  (save-excursion
-                                    (set-buffer elt)
-                                    (if buffer-read-only "%" " "))
-                                  (or (buffer-file-name elt) 
-                                      (save-excursion
-                                        (set-buffer elt)
-                                        (if list-buffers-directory
-                                            (expand-file-name
-                                             list-buffers-directory)))
-                                      ""))
-                                 elt)
-                                head))))
-                        (setq tail (cdr tail)))
-                      (reverse head))))))
+  (let ((buffers (buffer-list))  alist menu split-by-major-mode sum-of-squares)
+    ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
+    (let ((tail buffers))
+      (while tail
+       ;; Divide all buffers into buckets for various major modes.
+       ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
+       (with-current-buffer (car tail)
+         (let* ((adjusted-major-mode major-mode) elt)
+           (let ((tail mouse-buffer-menu-mode-groups))
+             (while tail
+               (if (string-match (car (car tail)) mode-name)
+                   (setq adjusted-major-mode (cdr (car tail))))
+               (setq tail (cdr tail))))
+           (setq elt (assoc adjusted-major-mode split-by-major-mode))
+           (if (null elt)
+               (setq elt (list adjusted-major-mode
+                               (if (stringp adjusted-major-mode)
+                                   adjusted-major-mode
+                                 mode-name))
+                     split-by-major-mode (cons elt split-by-major-mode)))
+           (or (memq (car tail) (cdr (cdr elt)))
+               (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
+       (setq tail (cdr tail))))
+    ;; Compute the sum of squares of sizes of the major-mode buckets.
+    (let ((tail split-by-major-mode))
+      (setq sum-of-squares 0)
+      (while tail
+       (setq sum-of-squares
+             (+ sum-of-squares
+                (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
+       (setq tail (cdr tail))))
+    (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
+          (* (length buffers) (length buffers)))
+       ;; Subdividing by major modes really helps, so let's do it.
+       (let (subdivided-menus (buffers-left (length buffers)))
+         ;; Sort the list to put the most popular major modes first.
+         (setq split-by-major-mode
+               (sort split-by-major-mode
+                     (function (lambda (elt1 elt2)
+                                 (> (length elt1) (length elt2))))))
+         ;; Make a separate submenu for each major mode
+         ;; that has more than one buffer,
+         ;; unless all the remaining buffers are less than 1/10 of them.
+         (while (and split-by-major-mode
+                     (and (> (length (car split-by-major-mode)) 3)
+                          (> (* buffers-left 10) (length buffers))))
+           (setq subdivided-menus
+                 (cons (cons
+                        (nth 1 (car split-by-major-mode))
+                        (mouse-buffer-menu-alist
+                         (cdr (cdr (car split-by-major-mode)))))
+                       subdivided-menus))
+           (setq buffers-left
+                 (- buffers-left (length (cdr (car split-by-major-mode)))))
+           (setq split-by-major-mode (cdr split-by-major-mode)))
+         ;; If any major modes are left over,
+         ;; make a single submenu for them.
+         (if split-by-major-mode
+             (setq subdivided-menus
+                   (cons (cons
+                          "Others"
+                          (mouse-buffer-menu-alist
+                           ;; we don't need split-by-major-mode any
+                           ;; more, so we can ditch it with nconc.
+                           (apply 'nconc (mapcar 'cddr split-by-major-mode))))
+                         subdivided-menus)))
+         (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
+      (progn
+       (setq alist (mouse-buffer-menu-alist buffers))
+       (setq menu (cons "Buffer Menu"
+                        (mouse-buffer-menu-split "Select Buffer" alist)))))
     (let ((buf (x-popup-menu event menu))
          (window (posn-window (event-start event))))
-      (if buf
-         (progn
+      (when buf
            (or (framep window) (select-window window))
-           (switch-to-buffer buf))))))
+       (switch-to-buffer buf)))))
+
+(defun mouse-buffer-menu-alist (buffers)
+  (let (tail
+       (maxlen 0)
+       head)
+    (setq buffers
+         (sort buffers
+               (function (lambda (elt1 elt2)
+                           (string< (buffer-name elt1) (buffer-name elt2))))))
+    (setq tail buffers)
+    (while tail
+      (or (eq ?\ (aref (buffer-name (car tail)) 0))
+         (setq maxlen
+               (max maxlen
+                    (length (buffer-name (car tail))))))
+      (setq tail (cdr tail)))
+    (setq tail buffers)
+    (while tail
+      (let ((elt (car tail)))
+       (if (/= (aref (buffer-name elt) 0) ?\ )
+           (setq head
+                 (cons
+                  (cons
+                   (format
+                    (format "%%%ds  %%s%%s  %%s" maxlen)
+                    (buffer-name elt)
+                    (if (buffer-modified-p elt) "*" " ")
+                    (save-excursion
+                      (set-buffer elt)
+                      (if buffer-read-only "%" " "))
+                    (or (buffer-file-name elt) 
+                        (save-excursion
+                          (set-buffer elt)
+                          (if list-buffers-directory
+                              (expand-file-name
+                               list-buffers-directory)))
+                        ""))
+                   elt)
+                  head))))
+      (setq tail (cdr tail)))
+    ;; Compensate for the reversal that the above loop does.
+    (nreverse head)))
+
+(defun mouse-buffer-menu-split (title alist)
+  ;; If we have lots of buffers, divide them into groups of 20
+  ;; and make a pane (or submenu) for each one.
+  (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
+      (let ((alist alist) sublists next
+           (i 1))
+       (while alist
+         ;; Pull off the next mouse-buffer-menu-maxlen buffers
+         ;; and make them the next element of sublist.
+         (setq next (nthcdr mouse-buffer-menu-maxlen alist))
+         (if next
+             (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
+                     nil))
+         (setq sublists (cons (cons (format "Buffers %d" i) alist)
+                              sublists))
+         (setq i (1+ i))
+         (setq alist next))
+       (nreverse sublists))
+    ;; Few buffers--put them all in one pane.
+    (list (cons title alist))))
 \f
 ;;; These need to be rewritten for the new scroll bar implementation.
 
@@ -1648,9 +1928,12 @@ and selects that window."
   "X fonts suitable for use in Emacs.")
 
 (defun mouse-set-font (&rest fonts)
-  "Select an emacs font from a list of known good fonts"
+  "Select an emacs font from a list of known good fonts and fontsets."
   (interactive
-   (x-popup-menu last-nonmenu-event x-fixed-font-alist))
+   (x-popup-menu
+    last-nonmenu-event
+    ;; Append list of fontsets currently defined.
+    (append x-fixed-font-alist (list (generate-fontset-menu)))))
   (if fonts
       (let (font)
        (while fonts