(vertical_scroll_bar_type): Add enumerated
[bpt/emacs.git] / lisp / mouse.el
index e73d74d..8da7095 100644 (file)
@@ -1,25 +1,26 @@
-;;; mouse.el --- window system-independent mouse support.
+;;; mouse.el --- window system-independent mouse support
 
-;;; Copyright (C) 1993, 1994 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:
 
   ;; 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
-             (lookup-key (current-local-map) [menu-bar]))))
-    (mouse-major-mode-menu-compute-equiv-keys newmap)
-    (command-execute
-     ;; 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)))
-       (lookup-key newmap (read-key-sequence ""))))))
+  (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)))))
+         (if command
+             (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.
 
@@ -121,8 +130,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."
@@ -278,6 +286,83 @@ This command must be bound to a mouse click."
                              (/= top (nth 1 (window-edges)))))
                     (set-window-configuration wconfig)))))))))
 \f
+(defun mouse-drag-vertical-line (start-event)
+  "Change the width of a window by dragging on the vertical line."
+  (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)
+    (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"))
+    (track-mouse
+      (progn
+       ;; enlarge-window only works on the selected window, so
+       ;; we must select the window where the start event originated.
+       ;; unwind-protect will restore the old selected window later.
+       (select-window start-event-window)
+       ;; loop reading events and sampling the position of
+       ;; the mouse.
+       (while (not done)
+         (setq event (read-event)
+               mouse (mouse-position))
+         ;; do nothing if
+         ;;   - there is a switch-frame event.
+         ;;   - the mouse isn't in the frame that we started in
+         ;;   - the mouse isn't in any Emacs frame
+         ;; drag if
+         ;;   - there is a mouse-movement event
+         ;;   - there is a scroll-bar-movement event
+         ;;     (same as mouse movement for our purposes)
+         ;; quit if
+         ;;   - there is a keyboard event or some other unknown event
+         ;;     unknown event.
+         (cond ((integerp event)
+                (setq done t))
+               ((eq (car event) 'switch-frame)
+                nil)
+               ((not (memq (car event)
+                           '(mouse-movement scroll-bar-movement)))
+                (if (consp event)
+                    (setq unread-command-events
+                          (cons event unread-command-events)))
+                (setq done t))
+               ((not (eq (car mouse) start-event-frame))
+                nil)
+               ((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)))))))))
+\f
 (defun mouse-set-point (event)
   "Move point to the position clicked on with the mouse.
 This should be bound to a mouse click event type."
@@ -292,6 +377,17 @@ This should be bound to a mouse click event type."
     (if (numberp (posn-point posn))
        (goto-char (posn-point posn)))))
 
+(defvar mouse-last-region-beg nil)
+(defvar mouse-last-region-end nil)
+(defvar mouse-last-region-tick nil)
+
+(defun mouse-region-match ()
+  "Return non-nil if there's an active region that was set with the mouse."
+  (and (mark t) mark-active
+       (eq mouse-last-region-beg (region-beginning))
+       (eq mouse-last-region-end (region-end))
+       (eq mouse-last-region-tick (buffer-modified-tick))))
+
 (defun mouse-set-region (click)
   "Set the region to the text dragged over, and copy to kill ring.
 This should be bound to a mouse drag event."
@@ -303,8 +399,9 @@ This should be bound to a mouse drag event."
     (if (numberp (posn-point posn))
        (goto-char (posn-point posn)))
     ;; If mark is highlighted, no need to bounce the cursor.
-    (or (and transient-mark-mode
-            (framep (selected-frame)))
+    ;; On X, we highlight while dragging, thus once again no need to bounce.
+    (or transient-mark-mode
+       (memq (framep (selected-frame)) '(x pc ms-windows))
        (sit-for 1))
     (push-mark)
     (set-mark (point))
@@ -312,8 +409,15 @@ This should be bound to a mouse drag event."
        (goto-char (posn-point end)))
     ;; Don't set this-command to kill-region, so that a following
     ;; C-w will not double the text in the kill ring.
-    (let (this-command)
-      (copy-region-as-kill (mark) (point)))))
+    ;; Ignore last-command so we don't append to a preceding kill.
+    (let (this-command last-command)
+      (copy-region-as-kill (mark) (point)))
+    (mouse-set-region-1)))
+
+(defun mouse-set-region-1 ()
+  (setq mouse-last-region-beg (region-beginning))
+  (setq mouse-last-region-end (region-end))
+  (setq mouse-last-region-tick (buffer-modified-tick)))
 
 (defvar mouse-scroll-delay 0.25
   "*The pause between scroll steps caused by mouse drags, in seconds.
@@ -324,11 +428,24 @@ 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.")
 
+(defvar 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.")
+
 (defun mouse-scroll-subr (window jump &optional overlay start)
   "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
 If OVERLAY is an overlay, let it stretch from START to the far edge of
 the newly visible text.
 Upon exit, point is at the far edge of the newly visible text."
+  (cond
+   ((and (> jump 0) (< jump mouse-scroll-min-lines))
+    (setq jump mouse-scroll-min-lines))
+   ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
+    (setq jump (- mouse-scroll-min-lines))))
   (let ((opoint (point)))
     (while (progn
             (goto-char (window-start window))
@@ -353,20 +470,25 @@ Upon exit, point is at the far edge of the newly visible text."
     (or (eq window (selected-window))
        (goto-char opoint))))
 
+;; Create an overlay and immediately delete it, to get "overlay in no buffer".
 (defvar mouse-drag-overlay (make-overlay 1 1))
+(delete-overlay mouse-drag-overlay)
 (overlay-put mouse-drag-overlay 'face 'region)
 
 (defvar mouse-selection-click-count 0)
 
+(defvar mouse-selection-click-count-buffer nil)
+
 (defun mouse-drag-region (start-event)
   "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))
@@ -378,12 +500,21 @@ release the mouse button.  Otherwise, it does not."
                   (1- (nth 3 bounds))))
         (click-count (1- (event-click-count start-event))))
     (setq mouse-selection-click-count click-count)
+    (setq mouse-selection-click-count-buffer (current-buffer))
     (mouse-set-point start-event)
+    ;; In case the down click is in the middle of some intangible text,
+    ;; use the end of that text, and put it in START-POINT.
+    (if (< (point) start-point)
+       (goto-char start-point))
+    (setq start-point (point))
     (let ((range (mouse-start-end start-point start-point click-count)))
       (move-overlay mouse-drag-overlay (car range) (nth 1 range)
                    (window-buffer start-window)))
     (deactivate-mark)
-    (let (event end end-point)
+    ;; 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 last-end-point (end-of-range (point)))
       (track-mouse
        (while (progn
                 (setq event (read-event))
@@ -393,12 +524,20 @@ release the mouse button.  Otherwise, it does not."
              nil
            (setq end (event-end event)
                  end-point (posn-point end))
+           (if end-point
+               (setq last-end-point end-point))
 
            (cond
             ;; Are we moving within the original window?
             ((and (eq (posn-window end) start-window)
                   (integer-or-marker-p end-point))
+             ;; Go to START-POINT first, so that when we move to END-POINT,
+             ;; if it's in the middle of intangible text,
+             ;; point jumps in the direction away from START-POINT.
+             (goto-char start-point)
              (goto-char end-point)
+             (if (zerop (% click-count 3))
+                 (setq end-of-range (point)))
              (let ((range (mouse-start-end start-point (point) click-count)))
                (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
 
@@ -408,26 +547,63 @@ release the mouse button.  Otherwise, it does not."
                 ((null mouse-row))
                 ((< mouse-row top)
                  (mouse-scroll-subr start-window (- mouse-row top)
-                                    mouse-drag-overlay start-point))
+                                    mouse-drag-overlay start-point)
+                 ;; Without this, point tends to jump back to the starting
+                 ;; position where the mouse button was pressed down.
+                 (setq end-of-range (overlay-start mouse-drag-overlay)))
                 ((>= mouse-row bottom)
                  (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
-                                    mouse-drag-overlay start-point)))))))))
+                                    mouse-drag-overlay start-point)
+                 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
       (if (consp event)
-;;; When we scroll into the mode line or menu bar, or out of the window,
-;;; we get events that don't fit these criteria.
-;;;           (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
-;;;           (eq (posn-window (event-end event)) start-window)
-;;;           (numberp (posn-point (event-end event)))
          (let ((fun (key-binding (vector (car event)))))
-           (if (not (= (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))
-                 (copy-region-as-kill (point) (mark t)))
-             (goto-char (overlay-end mouse-drag-overlay))
-             (setq this-command 'mouse-set-point))))
-      (delete-overlay mouse-drag-overlay))))
+           ;; 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 nil ;; (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)
+                         (overlay-end mouse-drag-overlay)))
+                 (let* ((stop-point (or (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)))
+                        ;; 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)
+                   (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))))
+               (goto-char (overlay-end mouse-drag-overlay))
+               (setq this-command 'mouse-set-point)
+               (delete-overlay mouse-drag-overlay))))
+       (delete-overlay mouse-drag-overlay)))))
 \f
 ;; Commands to handle xterm-style multiple clicks.
 
@@ -436,15 +612,20 @@ release the mouse button.  Otherwise, it does not."
 If DIR is positive skip forward; if negative, skip backward."
   (let* ((char (following-char))
         (syntax (char-to-string (char-syntax char))))
-    (if (or (string= syntax "w") (string= syntax " "))
-       (if (< dir 0)
-           (skip-syntax-backward syntax)
-         (skip-syntax-forward syntax))
-      (if (< dir 0)
-         (while (and (not (bobp)) (= (preceding-char) char))
-           (forward-char -1))
-       (while (and (not (eobp)) (= (following-char) char))
-         (forward-char 1))))))
+    (cond ((or (string= syntax "w") (string= syntax " "))
+          (if (< dir 0)
+              (skip-syntax-backward syntax)
+            (skip-syntax-forward syntax)))
+         ((string= syntax "_")
+          (if (< dir 0)
+              (skip-syntax-backward "w_")
+            (skip-syntax-forward "w_")))
+         ((< dir 0)
+          (while (and (not (bobp)) (= (preceding-char) char))
+            (forward-char -1)))
+         (t
+          (while (and (not (eobp)) (= (following-char) char))
+            (forward-char 1))))))
 
 ;; Return a list of region bounds based on START and END according to MODE.
 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
@@ -477,6 +658,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 (1+ start)
+                  (save-excursion
+                    (condition-case nil
+                        (progn
+                          (goto-char (1+ start))
+                          (backward-sexp 1)
+                          (point))
+                      (error end)))))))
         ((= mode 1)
         (list (save-excursion
                 (goto-char start)
@@ -505,12 +711,66 @@ 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))
+        (modifiers (delq 'double (delq 'triple (copy-sequence (event-modifiers event)))))
+        (new
+         (if (consp event)
+             (cons (event-convert-list (nreverse (cons basic modifiers)))
+                   (cdr event))
+           event)))
+    (setcar last new)
+    (if (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
+           (while (progn (setq event (read-event))
+                         (setq events (append events (list event)))
+                         (setq key (apply 'vector events))
+                         (and (memq 'down (event-modifiers event))
+                              (not (key-binding key))
+                              (not (member key mouse-region-delete-keys))
+                              (not (mouse-undouble-last-event events))))))
+         ;; 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.
@@ -551,6 +811,7 @@ regardless of where you click."
   (run-hooks 'mouse-leave-buffer-hook)
   (or mouse-yank-at-point (mouse-set-point click))
   (setq this-command 'yank)
+  (setq mouse-selection-click-count 0)
   (yank arg))
 
 (defun mouse-kill-ring-save (click)
@@ -618,73 +879,89 @@ 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")
-  (mouse-minibuffer-check click)
-  (let ((click-posn (posn-point (event-start click)))
-       ;; Don't let a subsequent kill command append to this one:
-       ;; prevent setting this-command to kill-region.
-       (this-command this-command))
-    (if (> (mod mouse-selection-click-count 3) 0)
-       (if (not (and (eq last-command 'mouse-save-then-kill)
-                     (equal click-posn
-                            (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
-           ;; Find both ends of the object selected by this click.
-           (let* ((range
-                   (mouse-start-end click-posn click-posn
-                                    mouse-selection-click-count)))
-             ;; Move whichever end is closer to the click.
-             ;; That's what xterm does, and it seems reasonable.
-             (if (< (abs (- click-posn (mark t)))
-                    (abs (- click-posn (point))))
-                 (set-mark (car range))
-               (goto-char (nth 1 range)))
-             ;; We have already put the old region in the kill ring.
-             ;; Replace it with the extended region.
-             ;; (It would be annoying to make a separate entry.)
-             (kill-new (buffer-substring (point) (mark t)) t)
-             ;; Arrange for a repeated mouse-3 to kill this region.
-             (setq mouse-save-then-kill-posn
-                   (list (car kill-ring) (point) click-posn))
-             (mouse-show-mark))
-         ;; If we click this button again without moving it,
-         ;; that time kill.
-         (mouse-save-then-kill-delete-region (point) (mark))
-         (setq mouse-selection-click-count 0)
-         (setq mouse-save-then-kill-posn nil))
-      (if (and (eq last-command 'mouse-save-then-kill)
-              mouse-save-then-kill-posn
-              (eq (car mouse-save-then-kill-posn) (car kill-ring))
-              (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
-         ;; If this is the second time we've called
-         ;; mouse-save-then-kill, delete the text from the buffer.
-         (progn
-           (mouse-save-then-kill-delete-region (point) (mark))
-           ;; After we kill, another click counts as "the first time".
+  (let ((before-scroll 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:
+         ;; prevent setting this-command to kill-region.
+         (this-command this-command))
+      (if (and (save-excursion
+                (set-buffer (window-buffer (posn-window (event-start click))))
+                (and (mark t) (> (mod mouse-selection-click-count 3) 0)
+                     ;; Don't be fooled by a recent click in some other buffer.
+                     (eq mouse-selection-click-count-buffer 
+                         (current-buffer)))))
+         (if (not (and (eq last-command 'mouse-save-then-kill)
+                       (equal click-posn
+                              (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
+             ;; Find both ends of the object selected by this click.
+             (let* ((range
+                     (mouse-start-end click-posn click-posn
+                                      mouse-selection-click-count)))
+               ;; Move whichever end is closer to the click.
+               ;; That's what xterm does, and it seems reasonable.
+               (if (< (abs (- click-posn (mark t)))
+                      (abs (- click-posn (point))))
+                   (set-mark (car range))
+                 (goto-char (nth 1 range)))
+               ;; We have already put the old region in the kill ring.
+               ;; Replace it with the extended region.
+               ;; (It would be annoying to make a separate entry.)
+               (kill-new (buffer-substring (point) (mark t)) t)
+               (mouse-set-region-1)
+               ;; Arrange for a repeated mouse-3 to kill this region.
+               (setq mouse-save-then-kill-posn
+                     (list (car kill-ring) (point) click-posn))
+               (mouse-show-mark))
+           ;; If we click this button again without moving it,
+           ;; that time kill.
+           (mouse-save-then-kill-delete-region (mark) (point))
+           (setq mouse-selection-click-count 0)
            (setq mouse-save-then-kill-posn nil))
-       (if (or (and (eq last-command 'mouse-save-then-kill)
-                    mouse-save-then-kill-posn)
-               (and mark-active transient-mark-mode)
-               (and (eq last-command 'mouse-drag-region)
-                    (or mark-even-if-inactive
-                        (not transient-mark-mode))))
-           ;; We have a selection or suitable region, so adjust it.
-           (let* ((posn (event-start click))
-                  (new (posn-point posn)))
-             (select-window (posn-window posn))
-             (if (numberp new)
-                 (progn
-                   ;; Move whichever end of the region is closer to the click.
-                   ;; That is what xterm does, and it seems reasonable.
-                   (if (< (abs (- new (point))) (abs (- new (mark t))))
-                       (goto-char new)
-                     (set-mark new))
-                   (setq deactivate-mark nil)))
-             (kill-new (buffer-substring (point) (mark t)) t))
-         ;; We just have point, so set mark here.
-         (mouse-set-mark-fast click)
-         (kill-ring-save (point) (mark t)))
-       (mouse-show-mark)
-       (setq mouse-save-then-kill-posn
-             (list (car kill-ring) (point) click-posn))))))
+       (if (and (eq last-command 'mouse-save-then-kill)
+                mouse-save-then-kill-posn
+                (eq (car mouse-save-then-kill-posn) (car kill-ring))
+                (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
+           ;; If this is the second time we've called
+           ;; mouse-save-then-kill, delete the text from the buffer.
+           (progn
+             (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)
+                 (and (memq last-command
+                            '(mouse-drag-region mouse-set-region))
+                      (or mark-even-if-inactive
+                          (not transient-mark-mode))))
+             ;; We have a selection or suitable region, so adjust it.
+             (let* ((posn (event-start click))
+                    (new (posn-point posn)))
+               (select-window (posn-window posn))
+               (if (numberp new)
+                   (progn
+                     ;; Move whichever end of the region is closer to the click.
+                     ;; That is what xterm does, and it seems reasonable.
+                     (if (< (abs (- new (point))) (abs (- new (mark t))))
+                         (goto-char new)
+                       (set-mark new))
+                     (setq deactivate-mark nil)))
+               (kill-new (buffer-substring (point) (mark t)) t)
+               (mouse-show-mark))
+           ;; Set the mark where point is, then move where clicked.
+           (mouse-set-mark-fast click)
+           (if before-scroll
+               (goto-char before-scroll))
+           (exchange-point-and-mark)
+           (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)))))))
 \f
 (global-set-key [M-mouse-1] 'mouse-start-secondary)
 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
@@ -744,7 +1021,8 @@ Highlight the drag area as you move the mouse.
 This must be bound to a button-down mouse 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))
@@ -854,12 +1132,13 @@ is to prevent accidents."
                (window-buffer (posn-window (event-start click)))
              (current-buffer)))
        (error "Select or click on the buffer where the secondary selection is")))
-  (save-excursion
-    (set-buffer (overlay-buffer mouse-secondary-overlay))
-    (kill-region (overlay-start mouse-secondary-overlay)
-                (overlay-end mouse-secondary-overlay)))
+  (let (this-command)
+    (save-excursion
+      (set-buffer (overlay-buffer mouse-secondary-overlay))
+      (kill-region (overlay-start mouse-secondary-overlay)
+                  (overlay-end mouse-secondary-overlay))))
   (delete-overlay mouse-secondary-overlay)
-  (x-set-selection 'SECONDARY nil)
+;;;  (x-set-selection 'SECONDARY nil)
   (setq mouse-secondary-overlay nil))
 
 (defun mouse-secondary-save-then-kill (click)
@@ -973,51 +1252,80 @@ again.  If you do this twice in the same position, it kills the selection."
                  (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
          (setq mouse-save-then-kill-posn
                (list (car kill-ring) (point) click-posn))))
-      (x-set-selection 'SECONDARY
-                      (if (overlay-buffer mouse-secondary-overlay)
+      (if (overlay-buffer mouse-secondary-overlay)
+         (x-set-selection 'SECONDARY
                           (buffer-substring
                            (overlay-start mouse-secondary-overlay)
                            (overlay-end mouse-secondary-overlay)))))))
 \f
+(defvar mouse-menu-buffer-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-menu-buffer-maxlen' and make a pane (or submenu) for each one.")
+
 (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) ""))
-                                           elt)
-                                          head))))
-                        (setq tail (cdr tail)))
-                      (reverse head))))))
+  (let* ((buffers
+         ;; Make an alist of (MENU-ITEM . BUFFER).
+         (let ((tail (buffer-list))
+               (maxlen 0)
+               head)
+           (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 (buffer-list))
+           (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)))
+        (menu
+         ;; If we have lots of buffers, divide them into groups of 20
+         ;; and make a pane (or submenu) for each one.
+         (if (> (length buffers) (/ (* mouse-menu-buffer-maxlen 3) 2))
+             (let ((buffers buffers) sublists next
+                   (i 1))
+               (while buffers
+                 ;; Pull off the next mouse-menu-buffer-maxlen buffers
+                 ;; and make them the next element of sublist.
+                 (setq next (nthcdr mouse-menu-buffer-maxlen buffers))
+                 (if next
+                     (setcdr (nthcdr (1- mouse-menu-buffer-maxlen) buffers)
+                             nil))
+                 (setq sublists (cons (cons (format "Buffers %d" i) buffers)
+                                      sublists))
+                 (setq i (1+ i))
+                 (setq buffers next))
+               (cons "Buffer Menu" (nreverse sublists)))
+           ;; Few buffers--put them all in one pane.
+           (list "Buffer Menu" (cons "Select Buffer" buffers)))))
     (let ((buf (x-popup-menu event menu))
          (window (posn-window (event-start event))))
       (if buf
@@ -1502,7 +1810,9 @@ and selects that window."
 
 ;; By binding these to down-going events, we let the user use the up-going
 ;; event to make the selection, saving a click.
-(global-set-key [C-down-mouse-1] 'mouse-set-font)
+(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
+(if (not (eq system-type 'ms-dos))
+    (global-set-key [S-down-mouse-1] 'mouse-set-font))
 ;; C-down-mouse-2 is bound in facemenu.el.
 (global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
 
@@ -1518,6 +1828,8 @@ and selects that window."
 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
+(global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
+(global-set-key [vertical-line mouse-1] 'mouse-select-window)
 
 (provide 'mouse)