* lisp/obsolete/mouse-sel.el (mouse-sel-mode): Use add/remove-function.
[emacs.git] / lisp / obsolete / mouse-sel.el
blob21765c3d65253b16535d587fc5ed06b97aedd734
1 ;;; mouse-sel.el --- multi-click selection support
3 ;; Copyright (C) 1993-1995, 2001-2014 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 <http://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 = x-select-text
101 ;; interprogram-paste-function = x-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 (eval-when-compile
139 (require 'cl))
141 ;;=== User Variables ======================================================
143 (defgroup mouse-sel nil
144 "Mouse selection enhancement."
145 :group 'mouse)
147 (defcustom mouse-sel-leave-point-near-mouse t
148 "Leave point near last mouse position.
149 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
150 of the region nearest to where the mouse last was.
151 If nil, point will always be placed at the beginning of the region."
152 :type 'boolean
153 :group 'mouse-sel)
155 (defcustom mouse-sel-cycle-clicks t
156 "If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks."
157 :type 'boolean
158 :group 'mouse-sel)
160 (defcustom mouse-sel-default-bindings t
161 "Control mouse bindings."
162 :type '(choice (const :tag "none" nil)
163 (const :tag "cut and paste" interprogram-cut-paste)
164 (other :tag "default bindings" t))
165 :group 'mouse-sel)
167 ;;=== Key bindings ========================================================
169 (defconst mouse-sel-bound-events
170 '(;; Primary selection bindings.
172 ;; Bind keys to `ignore' instead of unsetting them because modes may
173 ;; bind `down-mouse-1', for instance, without binding `mouse-1'.
174 ;; If we unset `mouse-1', this leads to a bitch_at_user when the
175 ;; mouse goes up because no matching binding is found for that.
176 ([mouse-1] . ignore)
177 ([drag-mouse-1] . ignore)
178 ([mouse-3] . ignore)
179 ([down-mouse-1] . mouse-select)
180 ([down-mouse-3] . mouse-extend)
181 ([mouse-2] . mouse-insert-selection)
182 ;; Secondary selection bindings.
183 ([M-mouse-1] . ignore)
184 ([M-drag-mouse-1] . ignore)
185 ([M-mouse-3] . ignore)
186 ([M-down-mouse-1] . mouse-select-secondary)
187 ([M-mouse-2] . mouse-insert-secondary)
188 ([M-down-mouse-3] . mouse-extend-secondary))
189 "An alist of events that `mouse-sel-mode' binds.")
191 ;;=== User Command ========================================================
193 (defvar mouse-sel-original-bindings nil)
195 (defalias 'mouse-sel--ignore #'ignore)
197 ;;;###autoload
198 (define-minor-mode mouse-sel-mode
199 "Toggle Mouse Sel mode.
200 With a prefix argument ARG, enable Mouse Sel mode if ARG is
201 positive, and disable it otherwise. If called from Lisp, enable
202 the mode if ARG is omitted or nil.
204 Mouse Sel mode is a global minor mode. When enabled, mouse
205 selection is enhanced in various ways:
207 - Double-clicking on symbol constituents selects symbols.
208 Double-clicking on quotes or parentheses selects sexps.
209 Double-clicking on whitespace selects whitespace.
210 Triple-clicking selects lines.
211 Quad-clicking selects paragraphs.
213 - Selecting sets the region & X primary selection, but does NOT affect
214 the `kill-ring', nor do the kill-ring functions change the X selection.
215 Because the mouse handlers set the primary selection directly,
216 mouse-sel sets the variables `interprogram-cut-function' and
217 `interprogram-paste-function' to nil.
219 - Clicking mouse-2 inserts the contents of the primary selection at
220 the mouse position (or point, if `mouse-yank-at-point' is non-nil).
222 - mouse-2 while selecting or extending copies selection to the
223 kill ring; mouse-1 or mouse-3 kills it."
224 :global t
225 :group 'mouse-sel
226 (if mouse-sel-mode
227 (progn
228 ;; If mouse-2 has never been done by the user, initialize the
229 ;; `event-kind' property to ensure that `follow-link' clicks
230 ;; are interpreted correctly.
231 (put 'mouse-2 'event-kind 'mouse-click)
232 (add-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
233 (when mouse-sel-default-bindings
234 ;; Save original bindings and replace them with new ones.
235 (setq mouse-sel-original-bindings
236 (mapcar (lambda (binding)
237 (let ((event (car binding)))
238 (prog1 (cons event (lookup-key global-map event))
239 (global-set-key event (cdr binding)))))
240 mouse-sel-bound-events))
241 ;; Update interprogram functions.
242 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
243 (add-function :override interprogram-cut-function
244 #'mouse-sel--ignore)
245 (add-function :override interprogram-paste-function
246 #'mouse-sel--ignore))))
248 ;; Restore original bindings
249 (remove-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
250 (dolist (binding mouse-sel-original-bindings)
251 (global-set-key (car binding) (cdr binding)))
252 ;; Restore the old values of these variables,
253 ;; only if they were actually saved previously.
254 (remove-function interprogram-cut-function #'mouse-sel--ignore)
255 (remove-function interprogram-paste-function #'mouse-sel--ignore)))
257 (make-obsolete 'mouse-sel-mode "use the normal mouse modes" "24.3")
259 ;;=== Internal Variables/Constants ========================================
261 (defvar mouse-sel-primary-thing nil
262 "Type of PRIMARY selection in current buffer.")
263 (make-variable-buffer-local 'mouse-sel-primary-thing)
265 (defvar mouse-sel-secondary-thing nil
266 "Type of SECONDARY selection in current buffer.")
267 (make-variable-buffer-local 'mouse-sel-secondary-thing)
269 ;; Ensure that secondary overlay is defined
270 (unless (overlayp mouse-secondary-overlay)
271 (setq mouse-secondary-overlay (make-overlay 1 1))
272 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
274 (defconst mouse-sel-primary-overlay
275 (let ((ol (make-overlay (point-min) (point-min))))
276 (delete-overlay ol)
277 (overlay-put ol 'face 'region)
279 "An overlay which records the current primary selection.
280 This is used by Mouse Sel mode only.")
282 (defconst mouse-sel-selection-alist
283 '((PRIMARY mouse-sel-primary-overlay mouse-sel-primary-thing)
284 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
285 "Alist associating selections with variables.
286 Each element is of the form:
288 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
290 where SELECTION-NAME = name of selection
291 OVERLAY-SYMBOL = name of variable containing overlay to use
292 SELECTION-THING-SYMBOL = name of variable where the current selection
293 type for this selection should be stored.")
295 (declare-function x-select-text "term/common-win" (text))
297 (defvar mouse-sel-set-selection-function
298 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
299 'x-set-selection
300 (lambda (selection value)
301 (if (eq selection 'PRIMARY)
302 (x-select-text value)
303 (x-set-selection selection value))))
304 "Function to call to set selection.
305 Called with two arguments:
307 SELECTION, the name of the selection concerned, and
308 VALUE, the text to store.
310 This sets the selection, unless `mouse-sel-default-bindings'
311 is `interprogram-cut-paste'.")
313 (declare-function x-selection-value "term/x-win" ())
315 (defvar mouse-sel-get-selection-function
316 (lambda (selection)
317 (if (eq selection 'PRIMARY)
318 (or (x-selection-value)
319 (bound-and-true-p x-last-selected-text)
320 (bound-and-true-p x-last-selected-text-primary))
321 (x-get-selection selection)))
322 "Function to call to get the selection.
323 Called with one argument:
325 SELECTION: the name of the selection concerned.")
327 ;;=== Support/access functions ============================================
329 (defun mouse-sel-determine-selection-thing (nclicks)
330 "Determine what `thing' `mouse-sel' should operate on.
331 The first argument is NCLICKS, is the number of consecutive
332 mouse clicks at the same position.
334 Double-clicking on word constituents selects words.
335 Double-clicking on symbol constituents selects symbols.
336 Double-clicking on quotes or parentheses selects sexps.
337 Double-clicking on whitespace selects whitespace.
338 Triple-clicking selects lines.
339 Quad-clicking selects paragraphs.
341 Feel free to re-define this function to support your own desired
342 multi-click semantics."
343 (let* ((next-char (char-after (point)))
344 (char-syntax (if next-char (char-syntax next-char))))
345 (if mouse-sel-cycle-clicks
346 (setq nclicks (1+ (% (1- nclicks) 4))))
347 (cond
348 ((= nclicks 1) nil)
349 ((= nclicks 3) 'line)
350 ((>= nclicks 4) 'paragraph)
351 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
352 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
353 ((eq char-syntax ?_) 'symbol)
354 ((eq char-syntax ?w) 'word))))
356 (defun mouse-sel-set-selection (selection value)
357 "Set the specified SELECTION to VALUE."
358 (if mouse-sel-set-selection-function
359 (funcall mouse-sel-set-selection-function selection value)
360 (put 'mouse-sel-internal-selection selection value)))
362 (defun mouse-sel-get-selection (selection)
363 "Get the value of the specified SELECTION."
364 (if mouse-sel-get-selection-function
365 (funcall mouse-sel-get-selection-function selection)
366 (get 'mouse-sel-internal-selection selection)))
368 (defun mouse-sel-selection-overlay (selection)
369 "Return overlay corresponding to SELECTION."
370 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
371 (or symbol (error "No overlay corresponding to %s selection" selection))
372 (symbol-value symbol)))
374 (defun mouse-sel-selection-thing (selection)
375 "Return overlay corresponding to SELECTION."
376 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
377 (or symbol (error "No symbol corresponding to %s selection" selection))
378 symbol))
380 (defun mouse-sel-region-to-primary (orig-window)
381 "Convert region to PRIMARY overlay and deactivate region.
382 Argument ORIG-WINDOW specifies the window the cursor was in when the
383 originating command was issued, and is used to determine whether the
384 region was visible or not."
385 (if transient-mark-mode
386 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
387 (cond
388 ((and mark-active
389 (or highlight-nonselected-windows
390 (eq orig-window (selected-window))))
391 ;; Region was visible, so convert region to overlay
392 (move-overlay overlay (region-beginning) (region-end)
393 (current-buffer)))
394 ((eq orig-window (selected-window))
395 ;; Point was visible, so set overlay at point
396 (move-overlay overlay (point) (point) (current-buffer)))
398 ;; Nothing was visible, so remove overlay
399 (delete-overlay overlay)))
400 (setq mark-active nil))))
402 (defun mouse-sel-primary-to-region (&optional direction)
403 "Convert PRIMARY overlay to region.
404 Optional argument DIRECTION specifies the mouse drag direction: a value of
405 1 indicates that the mouse was dragged left-to-right, otherwise it was
406 dragged right-to-left."
407 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
408 (start (overlay-start overlay))
409 (end (overlay-end overlay)))
410 (if (eq start end)
411 (progn
412 (if start (goto-char start))
413 (deactivate-mark))
414 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
415 (progn
416 (goto-char end)
417 (push-mark start 'nomsg 'active))
418 (goto-char start)
419 (push-mark end 'nomsg 'active)))
420 (if transient-mark-mode (delete-overlay overlay))))
422 (defmacro mouse-sel-eval-at-event-end (event &rest forms)
423 "Evaluate forms at mouse position.
424 Move to the end position of EVENT, execute FORMS, and restore original
425 point and window."
426 `(let ((posn (event-end ,event)))
427 (if posn (mouse-minibuffer-check ,event))
428 (if (and posn (not (windowp (posn-window posn))))
429 (error "Cursor not in text area of window"))
430 (let (orig-window orig-point-marker)
431 (setq orig-window (selected-window))
432 (if posn (select-window (posn-window posn)))
433 (setq orig-point-marker (point-marker))
434 (if (and posn (numberp (posn-point posn)))
435 (goto-char (posn-point posn)))
436 (unwind-protect
437 (progn
438 ,@forms)
439 (goto-char (marker-position orig-point-marker))
440 (move-marker orig-point-marker nil)
441 (select-window orig-window)))))
443 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
445 ;;=== Select ==============================================================
447 (defun mouse-select (event)
448 "Set region/selection using the mouse.
450 Click sets point & mark to click position.
451 Dragging extends region/selection.
453 Multi-clicking selects word/lines/paragraphs, as determined by
454 'mouse-sel-determine-selection-thing.
456 Clicking mouse-2 while selecting copies selected text to the kill-ring.
457 Clicking mouse-1 or mouse-3 kills the selected text.
459 This should be bound to a down-mouse event."
460 (interactive "@e")
461 (let (select)
462 (unwind-protect
463 (setq select (mouse-select-internal 'PRIMARY event))
464 (if (and select (listp select))
465 (push (cons 'mouse-2 (cdr event)) unread-command-events)
466 (mouse-sel-primary-to-region select)))))
468 (defun mouse-select-secondary (event)
469 "Set secondary selection using the mouse.
471 Click sets the start of the secondary selection to click position.
472 Dragging extends the secondary selection.
474 Multi-clicking selects word/lines/paragraphs, as determined by
475 'mouse-sel-determine-selection-thing.
477 Clicking mouse-2 while selecting copies selected text to the kill-ring.
478 Clicking mouse-1 or mouse-3 kills the selected text.
480 This should be bound to a down-mouse event."
481 (interactive "e")
482 (mouse-select-internal 'SECONDARY event))
484 (defun mouse-select-internal (selection event)
485 "Set SELECTION using the mouse, with EVENT as the initial down-event.
486 Normally, this returns the direction in which the selection was
487 made: a value of 1 indicates that the mouse was dragged
488 left-to-right, otherwise it was dragged right-to-left.
490 However, if `mouse-1-click-follows-link' is non-nil and the
491 subsequent mouse events specify following a link, this returns
492 the final mouse-event. In that case, the selection is not set."
493 (mouse-sel-eval-at-event-end event
494 (let ((thing-symbol (mouse-sel-selection-thing selection))
495 (overlay (mouse-sel-selection-overlay selection)))
496 (set thing-symbol
497 (mouse-sel-determine-selection-thing (event-click-count event)))
498 (let ((object-bounds (bounds-of-thing-at-point
499 (symbol-value thing-symbol))))
500 (if object-bounds
501 (progn
502 (move-overlay overlay
503 (car object-bounds) (cdr object-bounds)
504 (current-buffer)))
505 (move-overlay overlay (point) (point) (current-buffer)))))
506 (catch 'follow-link
507 (mouse-extend-internal selection event t))))
509 ;;=== Extend ==============================================================
511 (defun mouse-extend (event)
512 "Extend region/selection using the mouse."
513 (interactive "e")
514 (let ((orig-window (selected-window))
515 direction)
516 (select-window (posn-window (event-end event)))
517 (unwind-protect
518 (progn
519 (mouse-sel-region-to-primary orig-window)
520 (setq direction (mouse-extend-internal 'PRIMARY event)))
521 (mouse-sel-primary-to-region direction))))
523 (defun mouse-extend-secondary (event)
524 "Extend secondary selection using the mouse."
525 (interactive "e")
526 (save-window-excursion
527 (mouse-extend-internal 'SECONDARY event)))
529 (defun mouse-extend-internal (selection &optional initial-event no-process)
530 "Extend specified SELECTION using the mouse.
531 Track mouse-motion events, adjusting the SELECTION appropriately.
532 Optional argument INITIAL-EVENT specifies an initial down-mouse event.
533 Optional argument NO-PROCESS means not to process the initial
534 event.
536 See documentation for mouse-select-internal for more details."
537 (mouse-sel-eval-at-event-end initial-event
538 (let ((orig-cursor-type
539 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
540 (unwind-protect
542 (let* ((thing-symbol (mouse-sel-selection-thing selection))
543 (overlay (mouse-sel-selection-overlay selection))
544 (orig-window (selected-window))
545 (top (nth 1 (window-edges orig-window)))
546 (bottom (nth 3 (window-edges orig-window)))
547 (mark-active nil) ; inhibit normal region highlight
548 (echo-keystrokes 0) ; don't echo mouse events
549 min max
550 direction
551 event)
553 ;; Get current bounds of overlay
554 (if (eq (overlay-buffer overlay) (current-buffer))
555 (setq min (overlay-start overlay)
556 max (overlay-end overlay))
557 (setq min (point)
558 max min)
559 (set thing-symbol nil))
562 ;; Bar cursor
563 (if (fboundp 'modify-frame-parameters)
564 (modify-frame-parameters (selected-frame)
565 '((cursor-type . bar))))
567 ;; Handle dragging
568 (track-mouse
570 (while (if (and initial-event (not no-process))
571 ;; Use initial event
572 (prog1
573 (setq event initial-event)
574 (setq initial-event nil))
575 (setq event (read-event))
576 (and (consp event)
577 (memq (car event) '(mouse-movement switch-frame))))
579 (let ((selection-thing (symbol-value thing-symbol))
580 (end (event-end event)))
582 (cond
584 ;; Ignore any movement outside the frame
585 ((eq (car-safe event) 'switch-frame) nil)
586 ((and (posn-window end)
587 (not (eq (let ((posn-w (posn-window end)))
588 (if (windowp posn-w)
589 (window-frame posn-w)
590 posn-w))
591 (window-frame orig-window)))) nil)
593 ;; Different window, same frame
594 ((not (eq (posn-window end) orig-window))
595 (let ((end-row (cdr (cdr (mouse-position)))))
596 (cond
597 ((and end-row (not (bobp)) (< end-row top))
598 (mouse-scroll-subr orig-window (- end-row top)
599 overlay max))
600 ((and end-row (not (eobp)) (>= end-row bottom))
601 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
602 overlay min))
605 ;; On the mode line
606 ((eq (posn-point end) 'mode-line)
607 (mouse-scroll-subr orig-window 1 overlay min))
609 ;; In original window
610 (t (goto-char (posn-point end)))
614 ;; Determine direction of drag
615 (cond
616 ((and (not direction) (not (eq min max)))
617 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
618 ((and (not (eq direction -1)) (<= (point) min))
619 (setq direction -1))
620 ((and (not (eq direction 1)) (>= (point) max))
621 (setq direction 1)))
623 (if (not selection-thing) nil
625 ;; If dragging forward, goal is next character
626 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
628 ;; Move to start/end of selected thing
629 (let ((goal (point)))
630 (goto-char (if (eq 1 direction) min max))
631 (condition-case nil
632 (progn
633 (while (> (* direction (- goal (point))) 0)
634 (forward-thing selection-thing direction))
635 (let ((end (point)))
636 (forward-thing selection-thing (- direction))
637 (goto-char
638 (if (> (* direction (- goal (point))) 0)
639 end (point)))))
640 (error))))
642 ;; Move overlay
643 (move-overlay overlay
644 (if (eq 1 direction) min (point))
645 (if (eq -1 direction) max (point))
646 (current-buffer))
648 ))) ; end track-mouse
650 ;; Detect follow-link events
651 (when (mouse-sel-follow-link-p initial-event event)
652 (throw 'follow-link event))
654 ;; Finish up after dragging
655 (let ((overlay-start (overlay-start overlay))
656 (overlay-end (overlay-end overlay)))
658 ;; Set selection
659 (if (not (eq overlay-start overlay-end))
660 (mouse-sel-set-selection
661 selection
662 (buffer-substring overlay-start overlay-end)))
664 ;; Handle copy/kill
665 (let (this-command)
666 (cond
667 ((eq (event-basic-type last-input-event) 'mouse-2)
668 (copy-region-as-kill overlay-start overlay-end)
669 (read-event) (read-event))
670 ((and (memq (event-basic-type last-input-event)
671 '(mouse-1 mouse-3))
672 (memq 'down (event-modifiers last-input-event)))
673 (kill-region overlay-start overlay-end)
674 (move-overlay overlay overlay-start overlay-start)
675 (read-event) (read-event))
676 ((and (eq (event-basic-type last-input-event) 'mouse-3)
677 (memq 'double (event-modifiers last-input-event)))
678 (kill-region overlay-start overlay-end)
679 (move-overlay overlay overlay-start overlay-start)))))
681 direction)
683 ;; Restore cursor
684 (if (fboundp 'modify-frame-parameters)
685 (modify-frame-parameters
686 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
688 ))))
690 (defun mouse-sel-follow-link-p (initial final)
691 "Return t if we should follow a link, given INITIAL and FINAL mouse events.
692 See `mouse-1-click-follows-link' for details. Currently, Mouse
693 Sel mode does not support using a `double' value to follow links
694 using double-clicks."
695 (and initial final mouse-1-click-follows-link
696 (eq (car initial) 'down-mouse-1)
697 (mouse-on-link-p (event-start initial))
698 (= (posn-point (event-start initial))
699 (posn-point (event-end final)))
700 (= (event-click-count initial) 1)
701 (or (not (integerp mouse-1-click-follows-link))
702 (let ((t0 (posn-timestamp (event-start initial)))
703 (t1 (posn-timestamp (event-end final))))
704 (and (integerp t0) (integerp t1)
705 (if (> mouse-1-click-follows-link 0)
706 (<= (- t1 t0) mouse-1-click-follows-link)
707 (< (- t0 t1) mouse-1-click-follows-link)))))))
709 ;;=== Paste ===============================================================
711 (defun mouse-insert-selection (event arg)
712 "Insert the contents of the PRIMARY selection at mouse click.
713 If `mouse-yank-at-point' is non-nil, insert at point instead."
714 (interactive "e\nP")
715 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
716 (mouse-yank-at-click event arg)
717 (mouse-insert-selection-internal 'PRIMARY event)))
719 (defun mouse-insert-secondary (event)
720 "Insert the contents of the SECONDARY selection at mouse click.
721 If `mouse-yank-at-point' is non-nil, insert at point instead."
722 (interactive "e")
723 (mouse-insert-selection-internal 'SECONDARY event))
725 (defun mouse-insert-selection-internal (selection event)
726 "Insert the contents of the named SELECTION at mouse click.
727 If `mouse-yank-at-point' is non-nil, insert at point instead."
728 (unless mouse-yank-at-point
729 (mouse-set-point event))
730 (when mouse-sel-get-selection-function
731 (push-mark (point) 'nomsg)
732 (insert-for-yank
733 (or (funcall mouse-sel-get-selection-function selection) ""))))
735 ;;=== Handle loss of selections ===========================================
737 (defun mouse-sel-lost-selection-hook (selection)
738 "Remove the overlay for a lost selection."
739 (let ((overlay (mouse-sel-selection-overlay selection)))
740 (delete-overlay overlay)))
742 (provide 'mouse-sel)
744 ;;; mouse-sel.el ends here