(vc-svn-registered): Catch all errors.
[bpt/emacs.git] / lisp / mouse-sel.el
index 25fdc92..ccd4692 100644 (file)
@@ -1,6 +1,7 @@
 ;;; mouse-sel.el --- multi-click selection support for Emacs 19
 
-;; Copyright (C) 1993,1994,1995,2001,2002 Free Software Foundation, Inc.
+;; Copyright (C) 1993, 1994, 1995, 2001, 2002, 2003, 2004,
+;;   2005 Free Software Foundation, Inc.
 
 ;; Author: Mike Williams <mdub@bigfoot.com>
 ;; Keywords: mouse
@@ -19,8 +20,8 @@
 
 ;; 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.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -93,8 +94,9 @@
 ;;
 ;;       Mouse sets selection, and pastes from kill-ring
 ;;        mouse-1              mouse-select
-;;        mouse-2              mouse-yank-at-click
+;;        mouse-2              mouse-insert-selection
 ;;        mouse-3              mouse-extend
+;;      In this mode, mouse-insert-selection just calls mouse-yank-at-click.
 ;;
 ;;       Selection/kill-ring interaction is retained
 ;;         interprogram-cut-function   = x-select-text
   "Mouse selection enhancement."
   :group 'mouse)
 
-(defcustom mouse-sel-mode nil
-  "Toggle Mouse Sel mode.
-When Mouse Sel mode is enabled, mouse selection is enhanced in various ways.
-Setting this variable directly does not take effect;
-use either \\[customize] or the function `mouse-sel-mode'."
-  :set (lambda (symbol value)
-        (mouse-sel-mode (or value 0)))
-  :initialize 'custom-initialize-default
-  :type 'boolean
-  :group 'mouse-sel
-  :require 'mouse-sel)
-
 (defcustom mouse-sel-leave-point-near-mouse t
   "*Leave point near last mouse position.
 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
@@ -181,10 +171,41 @@ If nil, point will always be placed at the beginning of the region."
                 (other :tag "default bindings" t))
   :group 'mouse-sel)
 
+;;=== Key bindings ========================================================
+
+(defconst mouse-sel-bound-events
+  '(;; Primary selection bindings.
+    ;;
+    ;; Bind keys to `ignore' instead of unsetting them because modes may
+    ;; bind `down-mouse-1', for instance, without binding `mouse-1'.
+    ;; If we unset `mouse-1', this leads to a bitch_at_user when the
+    ;; mouse goes up because no matching binding is found for that.
+    ([mouse-1]         . ignore)
+    ([drag-mouse-1]    . ignore)
+    ([mouse-3]         . ignore)
+    ([down-mouse-1]    . mouse-select)
+    ([down-mouse-3]    . mouse-extend)
+    ([mouse-2]         . mouse-insert-selection)
+    ;; Secondary selection bindings.
+    ([M-mouse-1]       . ignore)
+    ([M-drag-mouse-1]  . ignore)
+    ([M-mouse-3]       . ignore)
+    ([M-down-mouse-1]  . mouse-select-secondary)
+    ([M-mouse-2]       . mouse-insert-secondary)
+    ([M-down-mouse-3]  . mouse-extend-secondary))
+  "An alist of events that `mouse-sel-mode' binds.")
+
 ;;=== User Command ========================================================
 
+(defvar mouse-sel-has-been-enabled nil
+  "Non-nil if Mouse Sel mode has been enabled at least once.")
+
+(defvar mouse-sel-original-bindings nil)
+(defvar mouse-sel-original-interprogram-cut-function nil)
+(defvar mouse-sel-original-interprogram-paste-function nil)
+
 ;;;###autoload
-(defun mouse-sel-mode (&optional arg)
+(define-minor-mode mouse-sel-mode
   "Toggle Mouse Sel mode.
 With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive.
 Returns the new status of Mouse Sel mode (non-nil means on).
@@ -203,10 +224,10 @@ Triple-clicking selects lines.
 Quad-clicking selects paragraphs.
 
 - Selecting sets the region & X primary selection, but does NOT affect
-the kill-ring, nor do the kill-ring function change the X selection.
+the `kill-ring', nor do the kill-ring functions change the X selection.
 Because the mouse handlers set the primary selection directly,
-mouse-sel sets the variables interprogram-cut-function and
-interprogram-paste-function to nil.
+mouse-sel sets the variables `interprogram-cut-function' and
+`interprogram-paste-function' to nil.
 
 - Clicking mouse-2 inserts the contents of the primary selection at
 the mouse position (or point, if `mouse-yank-at-point' is non-nil).
@@ -219,93 +240,40 @@ to the kill ring.  Pressing mouse-1 or mouse-3 kills it.
 - M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
 & mouse-3, but operate on the X secondary selection rather than the
 primary selection and region."
-  (interactive "P")
-  (let ((on-p (if arg
-                 (> (prefix-numeric-value arg) 0)
-               (not mouse-sel-mode))))
-    (if on-p
-       (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
-      (remove-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook))
-    (mouse-sel-bindings on-p)
-    (setq mouse-sel-mode on-p)))
-
-;;=== Key bindings ========================================================
-
-(defconst mouse-sel-bound-events
-  '([down-mouse-1] [mouse-1] [drag-mouse-1]
-    [mouse-2]
-    [down-mouse-3] [mouse-3]
-    [M-mouse-2]
-    [M-down-mouse-1] [M-mouse-1] [M-drag-mouse-1]
-    [M-down-mouse-3] [M-mouse-3])
-  "A list of events that mouse-sel binds.")
-
-(defun mouse-sel-save-original-bindings ()
-  "Save the current bindings for `mouse-sel-bound-events'."
-  (setq mouse-sel-original-bindings nil)
-  (mapc (function 
-         (lambda (event)
-           (setq mouse-sel-original-bindings
-                 (cons (cons event (lookup-key global-map event))
-                       mouse-sel-original-bindings))))
-        mouse-sel-bound-events))
-
-(defun mouse-sel-restore-original-bindings ()
-  "Restore the original bindings for `mouse-sel-bound-events'."
-  (mapc (function 
-         (lambda (binding)
-           (if (cdr binding)
-               (global-set-key (car binding) (cdr binding))
-             (global-unset-key (car binding)))))
-        mouse-sel-original-bindings))
-
-(defun mouse-sel-bindings (bind)
-  (cond 
-
-   ;; Default mouse-sel bindings
-   ((and bind mouse-sel-default-bindings) 
-
-    ;; Save original bindings
-    (mouse-sel-save-original-bindings)
-
-    ;; Primary selection bindings.
-    ;;
-    ;; Bind keys to `ignore' instead of unsetting them because
-    ;; modes may bind `down-mouse-1', for instance, without
-    ;; binding other `up-mouse-1' or `mouse-1'.  If we unset
-    ;; `mouse-1', this leads to a bitch_at_user when the mouse
-    ;; goes up because no matching binding is found for that.
-    (global-set-key [mouse-1]           'ignore)
-    (global-set-key [drag-mouse-1]      'ignore)
-    (global-set-key [mouse-3]           'ignore)
-    (global-set-key [down-mouse-1]     'mouse-select)
-    (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
-      (global-set-key [mouse-2]         'mouse-insert-selection)
-      (setq interprogram-cut-function nil
-            interprogram-paste-function nil))
-    (global-set-key [down-mouse-3]      'mouse-extend)
+  :global t
+  :group 'mouse-sel
+  (if mouse-sel-mode
+      (progn
+       (add-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
+       (when mouse-sel-default-bindings
+         ;; Save original bindings and replace them with new ones.
+         (setq mouse-sel-original-bindings
+               (mapcar (lambda (binding)
+                         (let ((event (car binding)))
+                           (prog1 (cons event (lookup-key global-map event))
+                             (global-set-key event (cdr binding)))))
+                       mouse-sel-bound-events))
+         ;; Update interprogram functions.
+         (setq mouse-sel-original-interprogram-cut-function
+               interprogram-cut-function
+               mouse-sel-original-interprogram-paste-function
+               interprogram-paste-function
+               mouse-sel-has-been-enabled t)
+         (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
+           (setq interprogram-cut-function nil
+                 interprogram-paste-function nil))))
 
-    ;; Secondary selection bindings.
-    (global-set-key [M-mouse-1]         'ignore)
-    (global-set-key [M-drag-mouse-1]    'ignore)
-    (global-set-key [M-mouse-3]         'ignore)
-    (global-set-key [M-down-mouse-1]   'mouse-select-secondary)
-    (global-set-key [M-mouse-2]         'mouse-insert-secondary)
-    (global-set-key [M-down-mouse-3]   'mouse-extend-secondary))
-   
-   ((not bind)
     ;; Restore original bindings
-    (mouse-sel-restore-original-bindings))
-
-   ))
-
-;;=== Command Variable ====================================================
-
-;; This has to come after the function `mouse-sel-mode' and its callee.
-;; An alternative is to put the option `mouse-sel-mode' here and remove its
-;; `:initialize' keyword.
-(when mouse-sel-mode
-  (mouse-sel-mode t))
+    (remove-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
+    (dolist (binding mouse-sel-original-bindings)
+      (global-set-key (car binding) (cdr binding)))
+    ;; Restore the old values of these variables,
+    ;; only if they were actually saved previously.
+    (if mouse-sel-has-been-enabled
+       (setq interprogram-cut-function
+             mouse-sel-original-interprogram-cut-function
+             interprogram-paste-function
+             mouse-sel-original-interprogram-paste-function))))
 
 ;;=== Internal Variables/Constants ========================================
 
@@ -354,7 +322,9 @@ unless `mouse-sel-default-bindings' is `interprogram-cut-paste'.")
 (defvar mouse-sel-get-selection-function
   (lambda (selection)
     (if (eq selection 'PRIMARY)
-       (or (x-cut-buffer-or-selection-value) x-last-selected-text)
+       (or (x-cut-buffer-or-selection-value)
+           (bound-and-true-p x-last-selected-text)
+           (bound-and-true-p x-last-selected-text-primary))
       (x-get-selection selection)))
   "Function to call to get the selection.
 Called with one argument:
@@ -386,7 +356,7 @@ multi-click semantics."
      ((= nclicks 3) 'line)
      ((>= nclicks 4) 'paragraph)
      ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
-     ((memq next-char '(? ?\t ?\n)) 'whitespace)
+     ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
      ((eq char-syntax ?_) 'symbol)
      ((eq char-syntax ?w) 'word))))
 
@@ -495,10 +465,12 @@ Clicking mouse-1 or mouse-3 kills the selected text.
 
 This should be bound to a down-mouse event."
   (interactive "@e")
-  (let (direction)
+  (let (select)
     (unwind-protect
-       (setq direction (mouse-select-internal 'PRIMARY event))
-      (mouse-sel-primary-to-region direction))))
+       (setq select (mouse-select-internal 'PRIMARY event))
+      (if (and select (listp select))
+         (push (cons 'mouse-2 (cdr event)) unread-command-events)
+       (mouse-sel-primary-to-region select)))))
 
 (defun mouse-select-secondary (event)
   "Set secondary selection using the mouse.
@@ -517,7 +489,14 @@ This should be bound to a down-mouse event."
   (mouse-select-internal 'SECONDARY event))
 
 (defun mouse-select-internal (selection event)
-  "Set SELECTION using the mouse."
+  "Set SELECTION using the mouse, with EVENT as the initial down-event.
+Normally, this returns the direction in which the selection was
+made: a value of 1 indicates that the mouse was dragged
+left-to-right, otherwise it was dragged right-to-left.
+
+However, if `mouse-1-click-follows-link' is non-nil and the
+subsequent mouse events specify following a link, this returns
+the final mouse-event.  In that case, the selection is not set."
   (mouse-sel-eval-at-event-end event
     (let ((thing-symbol (mouse-sel-selection-thing selection))
          (overlay (mouse-sel-selection-overlay selection)))
@@ -531,7 +510,8 @@ This should be bound to a down-mouse event."
                            (car object-bounds) (cdr object-bounds)
                            (current-buffer)))
          (move-overlay overlay (point) (point) (current-buffer)))))
-    (mouse-extend-internal selection)))
+    (catch 'follow-link
+      (mouse-extend-internal selection event t))))
 
 ;;=== Extend ==============================================================
 
@@ -553,11 +533,12 @@ This should be bound to a down-mouse event."
   (save-window-excursion
     (mouse-extend-internal 'SECONDARY event)))
 
-(defun mouse-extend-internal (selection &optional initial-event)
+(defun mouse-extend-internal (selection &optional initial-event no-process)
   "Extend specified SELECTION using the mouse.
 Track mouse-motion events, adjusting the SELECTION appropriately.
-Optional argument INITIAL-EVENT specifies an initial down-mouse event to
-process.
+Optional argument INITIAL-EVENT specifies an initial down-mouse event.
+Optional argument NO-PROCESS means not to process the initial
+event.
 
 See documentation for mouse-select-internal for more details."
   (mouse-sel-eval-at-event-end initial-event
@@ -594,7 +575,8 @@ See documentation for mouse-select-internal for more details."
            ;; Handle dragging
            (track-mouse
 
-             (while (if initial-event  ; Use initial event
+             (while (if (and initial-event (not no-process))
+                        ;; Use initial event
                         (prog1
                             (setq event initial-event)
                           (setq initial-event nil))
@@ -673,6 +655,10 @@ See documentation for mouse-select-internal for more details."
 
                  )))                   ; end track-mouse
 
+           ;; Detect follow-link events
+           (when (mouse-sel-follow-link-p initial-event event)
+             (throw 'follow-link event))
+
            ;; Finish up after dragging
            (let ((overlay-start (overlay-start overlay))
                  (overlay-end (overlay-end overlay)))
@@ -709,13 +695,34 @@ See documentation for mouse-select-internal for more details."
 
        ))))
 
+(defun mouse-sel-follow-link-p (initial final)
+  "Return t if we should follow a link, given INITIAL and FINAL mouse events.
+See `mouse-1-click-follows-link' for details.  Currently, Mouse
+Sel mode does not support using a `double' value to follow links
+using double-clicks."
+  (and initial final mouse-1-click-follows-link
+       (eq (car initial) 'down-mouse-1)
+       (mouse-on-link-p        (posn-point (event-start initial)))
+       (= (posn-point (event-start initial))
+         (posn-point (event-end final)))
+       (= (event-click-count initial) 1)
+       (or (not (integerp mouse-1-click-follows-link))
+          (let ((t0 (posn-timestamp (event-start initial)))
+                (t1 (posn-timestamp (event-end final))))
+            (and (integerp t0) (integerp t1)
+                 (if (> mouse-1-click-follows-link 0)
+                     (<= (- t1 t0) mouse-1-click-follows-link)
+                   (< (- t0 t1) mouse-1-click-follows-link)))))))
+
 ;;=== Paste ===============================================================
 
-(defun mouse-insert-selection (event)
+(defun mouse-insert-selection (event arg)
   "Insert the contents of the PRIMARY selection at mouse click.
 If `mouse-yank-at-point' is non-nil, insert at point instead."
-  (interactive "e")
-  (mouse-insert-selection-internal 'PRIMARY event))
+  (interactive "e\nP")
+  (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
+      (mouse-yank-at-click event arg)
+    (mouse-insert-selection-internal 'PRIMARY event)))
 
 (defun mouse-insert-secondary (event)
   "Insert the contents of the SECONDARY selection at mouse click.
@@ -741,4 +748,5 @@ If `mouse-yank-at-point' is non-nil, insert at point instead."
 
 (provide 'mouse-sel)
 
+;; arch-tag: 86e6c73f-deaa-48d3-a24e-c565fda1f7d7
 ;;; mouse-sel.el ends here