; doc/emacs/misc.texi (Network Security): Fix typo.
[emacs.git] / lisp / obsolete / mouse-sel.el
blob52e84f2117c1e81ddc637a29b47dee8aa373052f
1 ;;; mouse-sel.el --- multi-click selection support
3 ;; Copyright (C) 1993-1995, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: Mike Williams <mdub@bigfoot.com>
6 ;; Keywords: mouse
7 ;; Obsolete-since: 24.3
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This module provides multi-click mouse support for GNU Emacs versions
27 ;; 19.18 and later. I've tried to make it behave more like standard X
28 ;; clients (eg. xterm) than the default Emacs 19 mouse selection handlers.
29 ;; Basically:
31 ;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
33 ;; * Clicking or dragging mouse-3 extends the selection as well.
35 ;; * Double-clicking on word constituents selects words.
36 ;; Double-clicking on symbol constituents selects symbols.
37 ;; Double-clicking on quotes or parentheses selects sexps.
38 ;; Double-clicking on whitespace selects whitespace.
39 ;; Triple-clicking selects lines.
40 ;; Quad-clicking selects paragraphs.
42 ;; * Selecting sets the region & X primary selection, but does NOT affect
43 ;; the kill-ring. Because the mouse handlers set the primary selection
44 ;; directly, mouse-sel sets the variables interprogram-cut-function
45 ;; and interprogram-paste-function to nil.
47 ;; * Clicking mouse-2 inserts the contents of the primary selection at
48 ;; the mouse position (or point, if mouse-yank-at-point is non-nil).
50 ;; * Pressing mouse-2 while selecting or extending copies selection
51 ;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
53 ;; * Double-clicking mouse-3 also kills selection.
55 ;; * M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
56 ;; & mouse-3, but operate on the X secondary selection rather than the
57 ;; primary selection and region.
59 ;; This module requires my thingatpt.el module, which it uses to find the
60 ;; bounds of words, lines, sexps, etc.
62 ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
64 ;;--- Customization -------------------------------------------------------
66 ;; * You may want to use none or more of following:
68 ;; ;; Enable region highlight
69 ;; (transient-mark-mode 1)
71 ;; ;; But only in the selected window
72 ;; (setq highlight-nonselected-windows nil)
74 ;; ;; Enable pending-delete
75 ;; (delete-selection-mode 1)
77 ;; * You can control the way mouse-sel binds its keys by setting the value
78 ;; of mouse-sel-default-bindings before loading mouse-sel.
80 ;; (a) If mouse-sel-default-bindings = t (the default)
82 ;; Mouse sets and insert selection
83 ;; mouse-1 mouse-select
84 ;; mouse-2 mouse-insert-selection
85 ;; mouse-3 mouse-extend
87 ;; Selection/kill-ring interaction is disabled
88 ;; interprogram-cut-function = nil
89 ;; interprogram-paste-function = nil
91 ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
93 ;; Mouse sets selection, and pastes from kill-ring
94 ;; mouse-1 mouse-select
95 ;; mouse-2 mouse-insert-selection
96 ;; mouse-3 mouse-extend
97 ;; In this mode, mouse-insert-selection just calls mouse-yank-at-click.
99 ;; Selection/kill-ring interaction is retained
100 ;; interprogram-cut-function = gui-select-text
101 ;; interprogram-paste-function = gui-selection-value
103 ;; What you lose is the ability to select some text in
104 ;; delete-selection-mode and yank over the top of it.
106 ;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
108 ;; * By default, mouse-insert-selection (mouse-2) inserts the selection at
109 ;; the mouse position. You can tell it to insert at point instead with:
111 ;; (setq mouse-yank-at-point t)
113 ;; * I like to leave point at the end of the region nearest to where the
114 ;; mouse was, even though this makes region highlighting mis-leading (the
115 ;; cursor makes it look like one extra character is selected). You can
116 ;; disable this behavior with:
118 ;; (setq mouse-sel-leave-point-near-mouse nil)
120 ;; * By default, mouse-select cycles the click count after 4 clicks. That
121 ;; is, clicking mouse-1 five times has the same effect as clicking it
122 ;; once, clicking six times has the same effect as clicking twice, etc.
123 ;; Disable this behavior with:
125 ;; (setq mouse-sel-cycle-clicks nil)
127 ;; * The variables mouse-sel-{set,get}-selection-function control how the
128 ;; selection is handled. Under X Windows, these variables default so
129 ;; that the X primary selection is used. Under other windowing systems,
130 ;; alternate functions are used, which simply store the selection value
131 ;; in a variable.
133 ;;; Code:
135 (require 'mouse)
136 (require 'thingatpt)
138 ;;=== User Variables ======================================================
140 (defgroup mouse-sel nil
141 "Mouse selection enhancement."
142 :group 'mouse)
144 (defcustom mouse-sel-leave-point-near-mouse t
145 "Leave point near last mouse position.
146 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
147 of the region nearest to where the mouse last was.
148 If nil, point will always be placed at the beginning of the region."
149 :type 'boolean
150 :group 'mouse-sel)
152 (defcustom mouse-sel-cycle-clicks t
153 "If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks."
154 :type 'boolean
155 :group 'mouse-sel)
157 (defcustom mouse-sel-default-bindings t
158 "Control mouse bindings."
159 :type '(choice (const :tag "none" nil)
160 (const :tag "cut and paste" interprogram-cut-paste)
161 (other :tag "default bindings" t))
162 :group 'mouse-sel)
164 ;;=== Key bindings ========================================================
166 (defconst mouse-sel-bound-events
167 '(;; Primary selection bindings.
169 ;; Bind keys to `ignore' instead of unsetting them because modes may
170 ;; bind `down-mouse-1', for instance, without binding `mouse-1'.
171 ;; If we unset `mouse-1', this leads to a bitch_at_user when the
172 ;; mouse goes up because no matching binding is found for that.
173 ([mouse-1] . ignore)
174 ([drag-mouse-1] . ignore)
175 ([mouse-3] . ignore)
176 ([down-mouse-1] . mouse-select)
177 ([down-mouse-3] . mouse-extend)
178 ([mouse-2] . mouse-insert-selection)
179 ;; Secondary selection bindings.
180 ([M-mouse-1] . ignore)
181 ([M-drag-mouse-1] . ignore)
182 ([M-mouse-3] . ignore)
183 ([M-down-mouse-1] . mouse-select-secondary)
184 ([M-mouse-2] . mouse-insert-secondary)
185 ([M-down-mouse-3] . mouse-extend-secondary))
186 "An alist of events that `mouse-sel-mode' binds.")
188 ;;=== User Command ========================================================
190 (defvar mouse-sel-original-bindings nil)
192 (defalias 'mouse-sel--ignore #'ignore)
194 ;;;###autoload
195 (define-minor-mode mouse-sel-mode
196 "Toggle Mouse Sel mode.
197 With a prefix argument ARG, enable Mouse Sel mode if ARG is
198 positive, and disable it otherwise. If called from Lisp, enable
199 the mode if ARG is omitted or nil.
201 Mouse Sel mode is a global minor mode. When enabled, mouse
202 selection is enhanced in various ways:
204 - Double-clicking on symbol constituents selects symbols.
205 Double-clicking on quotes or parentheses selects sexps.
206 Double-clicking on whitespace selects whitespace.
207 Triple-clicking selects lines.
208 Quad-clicking selects paragraphs.
210 - Selecting sets the region & X primary selection, but does NOT affect
211 the `kill-ring', nor do the kill-ring functions change the X selection.
212 Because the mouse handlers set the primary selection directly,
213 mouse-sel sets the variables `interprogram-cut-function' and
214 `interprogram-paste-function' to nil.
216 - Clicking mouse-2 inserts the contents of the primary selection at
217 the mouse position (or point, if `mouse-yank-at-point' is non-nil).
219 - mouse-2 while selecting or extending copies selection to the
220 kill ring; mouse-1 or mouse-3 kills it."
221 :global t
222 :group 'mouse-sel
223 (if mouse-sel-mode
224 (progn
225 ;; If mouse-2 has never been done by the user, initialize the
226 ;; `event-kind' property to ensure that `follow-link' clicks
227 ;; are interpreted correctly.
228 (put 'mouse-2 'event-kind 'mouse-click)
229 (add-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
230 (when mouse-sel-default-bindings
231 ;; Save original bindings and replace them with new ones.
232 (setq mouse-sel-original-bindings
233 (mapcar (lambda (binding)
234 (let ((event (car binding)))
235 (prog1 (cons event (lookup-key global-map event))
236 (global-set-key event (cdr binding)))))
237 mouse-sel-bound-events))
238 ;; Update interprogram functions.
239 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
240 (add-function :override interprogram-cut-function
241 #'mouse-sel--ignore)
242 (add-function :override interprogram-paste-function
243 #'mouse-sel--ignore))))
245 ;; Restore original bindings
246 (remove-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
247 (dolist (binding mouse-sel-original-bindings)
248 (global-set-key (car binding) (cdr binding)))
249 ;; Restore the old values of these variables,
250 ;; only if they were actually saved previously.
251 (remove-function interprogram-cut-function #'mouse-sel--ignore)
252 (remove-function interprogram-paste-function #'mouse-sel--ignore)))
254 (make-obsolete 'mouse-sel-mode "use the normal mouse modes" "24.3")
256 ;;=== Internal Variables/Constants ========================================
258 (defvar mouse-sel-primary-thing nil
259 "Type of PRIMARY selection in current buffer.")
260 (make-variable-buffer-local 'mouse-sel-primary-thing)
262 (defvar mouse-sel-secondary-thing nil
263 "Type of SECONDARY selection in current buffer.")
264 (make-variable-buffer-local 'mouse-sel-secondary-thing)
266 ;; Ensure that secondary overlay is defined
267 (unless (overlayp mouse-secondary-overlay)
268 (setq mouse-secondary-overlay (make-overlay 1 1))
269 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
271 (defconst mouse-sel-primary-overlay
272 (let ((ol (make-overlay (point-min) (point-min))))
273 (delete-overlay ol)
274 (overlay-put ol 'face 'region)
276 "An overlay which records the current primary selection.
277 This is used by Mouse Sel mode only.")
279 (defconst mouse-sel-selection-alist
280 '((PRIMARY mouse-sel-primary-overlay mouse-sel-primary-thing)
281 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
282 "Alist associating selections with variables.
283 Each element is of the form:
285 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
287 where SELECTION-NAME = name of selection
288 OVERLAY-SYMBOL = name of variable containing overlay to use
289 SELECTION-THING-SYMBOL = name of variable where the current selection
290 type for this selection should be stored.")
292 (defvar mouse-sel-set-selection-function
293 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
294 'gui-set-selection
295 (lambda (selection value)
296 (if (eq selection 'PRIMARY)
297 (gui-select-text value)
298 (gui-set-selection selection value))))
299 "Function to call to set selection.
300 Called with two arguments:
302 SELECTION, the name of the selection concerned, and
303 VALUE, the text to store.
305 This sets the selection, unless `mouse-sel-default-bindings'
306 is `interprogram-cut-paste'.")
309 (defvar mouse-sel-get-selection-function
310 (lambda (selection)
311 (if (eq selection 'PRIMARY)
312 (or (gui-selection-value)
313 (bound-and-true-p x-last-selected-text-primary)
314 gui--last-selected-text-primary)
315 (gui-get-selection selection)))
316 "Function to call to get the selection.
317 Called with one argument:
319 SELECTION: the name of the selection concerned.")
321 ;;=== Support/access functions ============================================
323 (defun mouse-sel-determine-selection-thing (nclicks)
324 "Determine what `thing' `mouse-sel' should operate on.
325 The first argument is NCLICKS, is the number of consecutive
326 mouse clicks at the same position.
328 Double-clicking on word constituents selects words.
329 Double-clicking on symbol constituents selects symbols.
330 Double-clicking on quotes or parentheses selects sexps.
331 Double-clicking on whitespace selects whitespace.
332 Triple-clicking selects lines.
333 Quad-clicking selects paragraphs.
335 Feel free to re-define this function to support your own desired
336 multi-click semantics."
337 (let* ((next-char (char-after (point)))
338 (char-syntax (if next-char (char-syntax next-char))))
339 (if mouse-sel-cycle-clicks
340 (setq nclicks (1+ (% (1- nclicks) 4))))
341 (cond
342 ((= nclicks 1) nil)
343 ((= nclicks 3) 'line)
344 ((>= nclicks 4) 'paragraph)
345 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
346 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
347 ((eq char-syntax ?_) 'symbol)
348 ((eq char-syntax ?w) 'word))))
350 (defun mouse-sel-set-selection (selection value)
351 "Set the specified SELECTION to VALUE."
352 (if mouse-sel-set-selection-function
353 (funcall mouse-sel-set-selection-function selection value)
354 (put 'mouse-sel-internal-selection selection value)))
356 (defun mouse-sel-get-selection (selection)
357 "Get the value of the specified SELECTION."
358 (if mouse-sel-get-selection-function
359 (funcall mouse-sel-get-selection-function selection)
360 (get 'mouse-sel-internal-selection selection)))
362 (defun mouse-sel-selection-overlay (selection)
363 "Return overlay corresponding to SELECTION."
364 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
365 (or symbol (error "No overlay corresponding to %s selection" selection))
366 (symbol-value symbol)))
368 (defun mouse-sel-selection-thing (selection)
369 "Return overlay corresponding to SELECTION."
370 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
371 (or symbol (error "No symbol corresponding to %s selection" selection))
372 symbol))
374 (defun mouse-sel-region-to-primary (orig-window)
375 "Convert region to PRIMARY overlay and deactivate region.
376 Argument ORIG-WINDOW specifies the window the cursor was in when the
377 originating command was issued, and is used to determine whether the
378 region was visible or not."
379 (if transient-mark-mode
380 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
381 (cond
382 ((and mark-active
383 (or highlight-nonselected-windows
384 (eq orig-window (selected-window))))
385 ;; Region was visible, so convert region to overlay
386 (move-overlay overlay (region-beginning) (region-end)
387 (current-buffer)))
388 ((eq orig-window (selected-window))
389 ;; Point was visible, so set overlay at point
390 (move-overlay overlay (point) (point) (current-buffer)))
392 ;; Nothing was visible, so remove overlay
393 (delete-overlay overlay)))
394 (setq mark-active nil))))
396 (defun mouse-sel-primary-to-region (&optional direction)
397 "Convert PRIMARY overlay to region.
398 Optional argument DIRECTION specifies the mouse drag direction: a value of
399 1 indicates that the mouse was dragged left-to-right, otherwise it was
400 dragged right-to-left."
401 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
402 (start (overlay-start overlay))
403 (end (overlay-end overlay)))
404 (if (eq start end)
405 (progn
406 (if start (goto-char start))
407 (deactivate-mark))
408 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
409 (progn
410 (goto-char end)
411 (push-mark start 'nomsg 'active))
412 (goto-char start)
413 (push-mark end 'nomsg 'active)))
414 (if transient-mark-mode (delete-overlay overlay))))
416 (defmacro mouse-sel-eval-at-event-end (event &rest forms)
417 "Evaluate forms at mouse position.
418 Move to the end position of EVENT, execute FORMS, and restore original
419 point and window."
420 `(let ((posn (event-end ,event)))
421 (if posn (mouse-minibuffer-check ,event))
422 (if (and posn (not (windowp (posn-window posn))))
423 (error "Cursor not in text area of window"))
424 (let (orig-window orig-point-marker)
425 (setq orig-window (selected-window))
426 (if posn (select-window (posn-window posn)))
427 (setq orig-point-marker (point-marker))
428 (if (and posn (numberp (posn-point posn)))
429 (goto-char (posn-point posn)))
430 (unwind-protect
431 (progn
432 ,@forms)
433 (goto-char (marker-position orig-point-marker))
434 (move-marker orig-point-marker nil)
435 (select-window orig-window)))))
437 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
439 ;;=== Select ==============================================================
441 (defun mouse-select (event)
442 "Set region/selection using the mouse.
444 Click sets point & mark to click position.
445 Dragging extends region/selection.
447 Multi-clicking selects word/lines/paragraphs, as determined by
448 'mouse-sel-determine-selection-thing.
450 Clicking mouse-2 while selecting copies selected text to the kill-ring.
451 Clicking mouse-1 or mouse-3 kills the selected text.
453 This should be bound to a down-mouse event."
454 (interactive "@e")
455 (let (select)
456 (unwind-protect
457 (setq select (mouse-select-internal 'PRIMARY event))
458 (if (and select (listp select))
459 (push (cons 'mouse-2 (cdr event)) unread-command-events)
460 (mouse-sel-primary-to-region select)))))
462 (defun mouse-select-secondary (event)
463 "Set secondary selection using the mouse.
465 Click sets the start of the secondary selection to click position.
466 Dragging extends the secondary selection.
468 Multi-clicking selects word/lines/paragraphs, as determined by
469 'mouse-sel-determine-selection-thing.
471 Clicking mouse-2 while selecting copies selected text to the kill-ring.
472 Clicking mouse-1 or mouse-3 kills the selected text.
474 This should be bound to a down-mouse event."
475 (interactive "e")
476 (mouse-select-internal 'SECONDARY event))
478 (defun mouse-select-internal (selection event)
479 "Set SELECTION using the mouse, with EVENT as the initial down-event.
480 Normally, this returns the direction in which the selection was
481 made: a value of 1 indicates that the mouse was dragged
482 left-to-right, otherwise it was dragged right-to-left.
484 However, if `mouse-1-click-follows-link' is non-nil and the
485 subsequent mouse events specify following a link, this returns
486 the final mouse-event. In that case, the selection is not set."
487 (mouse-sel-eval-at-event-end event
488 (let ((thing-symbol (mouse-sel-selection-thing selection))
489 (overlay (mouse-sel-selection-overlay selection)))
490 (set thing-symbol
491 (mouse-sel-determine-selection-thing (event-click-count event)))
492 (let ((object-bounds (bounds-of-thing-at-point
493 (symbol-value thing-symbol))))
494 (if object-bounds
495 (progn
496 (move-overlay overlay
497 (car object-bounds) (cdr object-bounds)
498 (current-buffer)))
499 (move-overlay overlay (point) (point) (current-buffer)))))
500 (catch 'follow-link
501 (mouse-extend-internal selection event t))))
503 ;;=== Extend ==============================================================
505 (defun mouse-extend (event)
506 "Extend region/selection using the mouse."
507 (interactive "e")
508 (let ((orig-window (selected-window))
509 direction)
510 (select-window (posn-window (event-end event)))
511 (unwind-protect
512 (progn
513 (mouse-sel-region-to-primary orig-window)
514 (setq direction (mouse-extend-internal 'PRIMARY event)))
515 (mouse-sel-primary-to-region direction))))
517 (defun mouse-extend-secondary (event)
518 "Extend secondary selection using the mouse."
519 (interactive "e")
520 (save-window-excursion
521 (mouse-extend-internal 'SECONDARY event)))
523 (defun mouse-extend-internal (selection &optional initial-event no-process)
524 "Extend specified SELECTION using the mouse.
525 Track mouse-motion events, adjusting the SELECTION appropriately.
526 Optional argument INITIAL-EVENT specifies an initial down-mouse event.
527 Optional argument NO-PROCESS means not to process the initial
528 event.
530 See documentation for mouse-select-internal for more details."
531 (mouse-sel-eval-at-event-end initial-event
532 (let ((orig-cursor-type
533 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
534 (unwind-protect
536 (let* ((thing-symbol (mouse-sel-selection-thing selection))
537 (overlay (mouse-sel-selection-overlay selection))
538 (orig-window (selected-window))
539 (top (nth 1 (window-edges orig-window)))
540 (bottom (nth 3 (window-edges orig-window)))
541 (mark-active nil) ; inhibit normal region highlight
542 (echo-keystrokes 0) ; don't echo mouse events
543 min max
544 direction
545 event)
547 ;; Get current bounds of overlay
548 (if (eq (overlay-buffer overlay) (current-buffer))
549 (setq min (overlay-start overlay)
550 max (overlay-end overlay))
551 (setq min (point)
552 max min)
553 (set thing-symbol nil))
556 ;; Bar cursor
557 (if (fboundp 'modify-frame-parameters)
558 (modify-frame-parameters (selected-frame)
559 '((cursor-type . bar))))
561 ;; Handle dragging
562 (track-mouse
564 (while (if (and initial-event (not no-process))
565 ;; Use initial event
566 (prog1
567 (setq event initial-event)
568 (setq initial-event nil))
569 (setq event (read-event))
570 (and (consp event)
571 (memq (car event) '(mouse-movement switch-frame))))
573 (let ((selection-thing (symbol-value thing-symbol))
574 (end (event-end event)))
576 (cond
578 ;; Ignore any movement outside the frame
579 ((eq (car-safe event) 'switch-frame) nil)
580 ((and (posn-window end)
581 (not (eq (let ((posn-w (posn-window end)))
582 (if (windowp posn-w)
583 (window-frame posn-w)
584 posn-w))
585 (window-frame orig-window)))) nil)
587 ;; Different window, same frame
588 ((not (eq (posn-window end) orig-window))
589 (let ((end-row (cdr (cdr (mouse-position)))))
590 (cond
591 ((and end-row (not (bobp)) (< end-row top))
592 (mouse-scroll-subr orig-window (- end-row top)
593 overlay max))
594 ((and end-row (not (eobp)) (>= end-row bottom))
595 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
596 overlay min))
599 ;; On the mode line
600 ((eq (posn-point end) 'mode-line)
601 (mouse-scroll-subr orig-window 1 overlay min))
603 ;; In original window
604 (t (goto-char (posn-point end)))
608 ;; Determine direction of drag
609 (cond
610 ((and (not direction) (not (eq min max)))
611 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
612 ((and (not (eq direction -1)) (<= (point) min))
613 (setq direction -1))
614 ((and (not (eq direction 1)) (>= (point) max))
615 (setq direction 1)))
617 (if (not selection-thing) nil
619 ;; If dragging forward, goal is next character
620 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
622 ;; Move to start/end of selected thing
623 (let ((goal (point)))
624 (goto-char (if (eq 1 direction) min max))
625 (condition-case nil
626 (progn
627 (while (> (* direction (- goal (point))) 0)
628 (forward-thing selection-thing direction))
629 (let ((end (point)))
630 (forward-thing selection-thing (- direction))
631 (goto-char
632 (if (> (* direction (- goal (point))) 0)
633 end (point)))))
634 (error))))
636 ;; Move overlay
637 (move-overlay overlay
638 (if (eq 1 direction) min (point))
639 (if (eq -1 direction) max (point))
640 (current-buffer))
642 ))) ; end track-mouse
644 ;; Detect follow-link events
645 (when (mouse-sel-follow-link-p initial-event event)
646 (throw 'follow-link event))
648 ;; Finish up after dragging
649 (let ((overlay-start (overlay-start overlay))
650 (overlay-end (overlay-end overlay)))
652 ;; Set selection
653 (if (not (eq overlay-start overlay-end))
654 (mouse-sel-set-selection
655 selection
656 (buffer-substring overlay-start overlay-end)))
658 ;; Handle copy/kill
659 (let (this-command)
660 (cond
661 ((eq (event-basic-type last-input-event) 'mouse-2)
662 (copy-region-as-kill overlay-start overlay-end)
663 (read-event) (read-event))
664 ((and (memq (event-basic-type last-input-event)
665 '(mouse-1 mouse-3))
666 (memq 'down (event-modifiers last-input-event)))
667 (kill-region overlay-start overlay-end)
668 (move-overlay overlay overlay-start overlay-start)
669 (read-event) (read-event))
670 ((and (eq (event-basic-type last-input-event) 'mouse-3)
671 (memq 'double (event-modifiers last-input-event)))
672 (kill-region overlay-start overlay-end)
673 (move-overlay overlay overlay-start overlay-start)))))
675 direction)
677 ;; Restore cursor
678 (if (fboundp 'modify-frame-parameters)
679 (modify-frame-parameters
680 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
682 ))))
684 (defun mouse-sel-follow-link-p (initial final)
685 "Return t if we should follow a link, given INITIAL and FINAL mouse events.
686 See `mouse-1-click-follows-link' for details. Currently, Mouse
687 Sel mode does not support using a `double' value to follow links
688 using double-clicks."
689 (and initial final mouse-1-click-follows-link
690 (eq (car initial) 'down-mouse-1)
691 (mouse-on-link-p (event-start initial))
692 (= (posn-point (event-start initial))
693 (posn-point (event-end final)))
694 (= (event-click-count initial) 1)
695 (or (not (integerp mouse-1-click-follows-link))
696 (let ((t0 (posn-timestamp (event-start initial)))
697 (t1 (posn-timestamp (event-end final))))
698 (and (integerp t0) (integerp t1)
699 (if (> mouse-1-click-follows-link 0)
700 (<= (- t1 t0) mouse-1-click-follows-link)
701 (< (- t0 t1) mouse-1-click-follows-link)))))))
703 ;;=== Paste ===============================================================
705 (defun mouse-insert-selection (event arg)
706 "Insert the contents of the PRIMARY selection at mouse click.
707 If `mouse-yank-at-point' is non-nil, insert at point instead."
708 (interactive "e\nP")
709 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
710 (mouse-yank-at-click event arg)
711 (mouse-insert-selection-internal 'PRIMARY event)))
713 (defun mouse-insert-secondary (event)
714 "Insert the contents of the SECONDARY selection at mouse click.
715 If `mouse-yank-at-point' is non-nil, insert at point instead."
716 (interactive "e")
717 (mouse-insert-selection-internal 'SECONDARY event))
719 (defun mouse-insert-selection-internal (selection event)
720 "Insert the contents of the named SELECTION at mouse click.
721 If `mouse-yank-at-point' is non-nil, insert at point instead."
722 (unless mouse-yank-at-point
723 (mouse-set-point event))
724 (when mouse-sel-get-selection-function
725 (push-mark (point) 'nomsg)
726 (insert-for-yank
727 (or (funcall mouse-sel-get-selection-function selection) ""))))
729 ;;=== Handle loss of selections ===========================================
731 (defun mouse-sel-lost-selection-hook (selection)
732 "Remove the overlay for a lost selection."
733 (let ((overlay (mouse-sel-selection-overlay selection)))
734 (delete-overlay overlay)))
736 (provide 'mouse-sel)
738 ;;; mouse-sel.el ends here