(undigestify-rmail-message): Better error messages.
[bpt/emacs.git] / lisp / mouse.el
index 9e4c482..351d307 100644 (file)
@@ -1,6 +1,6 @@
 ;;; 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
     (define-key newmap (vector (car event))
       (nconc (make-sparse-keymap "Menu")
             (mouse-major-mode-menu-1
-             (lookup-key (current-local-map) [menu-bar]))))
+             (and (current-local-map)
+                  (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 ""))))))
+    ;; 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)))))
 
 ;; Compute and cache the equivalent keys in MENU and all its submenus.
 (defun mouse-major-mode-menu-compute-equiv-keys (menu)
@@ -278,6 +285,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."
@@ -314,8 +398,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
+       (eq (framep (selected-frame)) 'x)
        (sit-for 1))
     (push-mark)
     (set-mark (point))
@@ -323,7 +408,8 @@ 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)
+    ;; 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)))
 
@@ -341,11 +427,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))
@@ -370,11 +469,15 @@ 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.
@@ -395,12 +498,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 (end-of-range (point)))
       (track-mouse
        (while (progn
                 (setq event (read-event))
@@ -415,7 +527,13 @@ release the mouse button.  Otherwise, it does not."
             ;; 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))))
 
@@ -435,12 +553,17 @@ release the mouse button.  Otherwise, it does not."
            ;; 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))
-               (progn
-                 (setq this-command 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)
-                 (funcall fun event))
+                 (setq unread-command-events
+                       (cons event unread-command-events)))
              (if (not (= (overlay-start mouse-drag-overlay)
                          (overlay-end mouse-drag-overlay)))
                  (let (last-command this-command)
@@ -461,15 +584,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).
@@ -576,6 +704,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)
@@ -649,7 +778,12 @@ If you do this twice in the same position, the selection is killed."
          ;; Don't let a subsequent kill command append to this one:
          ;; prevent setting this-command to kill-region.
          (this-command this-command))
-      (if (and (mark t) (> (mod mouse-selection-click-count 3) 0))
+      (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))))))
@@ -674,7 +808,7 @@ If you do this twice in the same position, the selection is killed."
                (mouse-show-mark))
            ;; If we click this button again without moving it,
            ;; that time kill.
-           (mouse-save-then-kill-delete-region (point) (mark))
+           (mouse-save-then-kill-delete-region (mark) (point))
            (setq mouse-selection-click-count 0)
            (setq mouse-save-then-kill-posn nil))
        (if (and (eq last-command 'mouse-save-then-kill)
@@ -706,14 +840,14 @@ If you do this twice in the same position, the selection is killed."
                          (goto-char new)
                        (set-mark new))
                      (setq deactivate-mark nil)))
-               (kill-new (buffer-substring (point) (mark t)) t))
+               (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-ring-save (point) (mark t)))
-         (mouse-show-mark)
+           (kill-new (buffer-substring (point) (mark t))))
          (mouse-set-region-1)
          (setq mouse-save-then-kill-posn
                (list (car kill-ring) (point) click-posn)))))))
@@ -886,12 +1020,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)
@@ -1539,7 +1674,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)
 
@@ -1555,6 +1692,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)