lisp/startup.el (argi): Declare as global variable (bug#9275).
[emacs.git] / lisp / mouse-sel.el
blob50d221b6fa0165ebf1d7b48ac5cd5fbae455a4ca
1 ;;; mouse-sel.el --- multi-click selection support
3 ;; Copyright (C) 1993-1995, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Mike Williams <mdub@bigfoot.com>
6 ;; Keywords: mouse
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This module provides multi-click mouse support for GNU Emacs versions
26 ;; 19.18 and later. I've tried to make it behave more like standard X
27 ;; clients (eg. xterm) than the default Emacs 19 mouse selection handlers.
28 ;; Basically:
30 ;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
32 ;; * Clicking or dragging mouse-3 extends the selection as well.
34 ;; * Double-clicking on word constituents selects words.
35 ;; Double-clicking on symbol constituents selects symbols.
36 ;; Double-clicking on quotes or parentheses selects sexps.
37 ;; Double-clicking on whitespace selects whitespace.
38 ;; Triple-clicking selects lines.
39 ;; Quad-clicking selects paragraphs.
41 ;; * Selecting sets the region & X primary selection, but does NOT affect
42 ;; the kill-ring. Because the mouse handlers set the primary selection
43 ;; directly, mouse-sel sets the variables interprogram-cut-function
44 ;; and interprogram-paste-function to nil.
46 ;; * Clicking mouse-2 inserts the contents of the primary selection at
47 ;; the mouse position (or point, if mouse-yank-at-point is non-nil).
49 ;; * Pressing mouse-2 while selecting or extending copies selection
50 ;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
52 ;; * Double-clicking mouse-3 also kills selection.
54 ;; * M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
55 ;; & mouse-3, but operate on the X secondary selection rather than the
56 ;; primary selection and region.
58 ;; This module requires my thingatpt.el module, which it uses to find the
59 ;; bounds of words, lines, sexps, etc.
61 ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
63 ;;--- Customisation -------------------------------------------------------
65 ;; * You may want to use none or more of following:
67 ;; ;; Enable region highlight
68 ;; (transient-mark-mode 1)
70 ;; ;; But only in the selected window
71 ;; (setq highlight-nonselected-windows nil)
73 ;; ;; Enable pending-delete
74 ;; (delete-selection-mode 1)
76 ;; * You can control the way mouse-sel binds its keys by setting the value
77 ;; of mouse-sel-default-bindings before loading mouse-sel.
79 ;; (a) If mouse-sel-default-bindings = t (the default)
81 ;; Mouse sets and insert selection
82 ;; mouse-1 mouse-select
83 ;; mouse-2 mouse-insert-selection
84 ;; mouse-3 mouse-extend
86 ;; Selection/kill-ring interaction is disabled
87 ;; interprogram-cut-function = nil
88 ;; interprogram-paste-function = nil
90 ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
92 ;; Mouse sets selection, and pastes from kill-ring
93 ;; mouse-1 mouse-select
94 ;; mouse-2 mouse-insert-selection
95 ;; mouse-3 mouse-extend
96 ;; In this mode, mouse-insert-selection just calls mouse-yank-at-click.
98 ;; Selection/kill-ring interaction is retained
99 ;; interprogram-cut-function = x-select-text
100 ;; interprogram-paste-function = x-selection-value
102 ;; What you lose is the ability to select some text in
103 ;; delete-selection-mode and yank over the top of it.
105 ;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
107 ;; * By default, mouse-insert-selection (mouse-2) inserts the selection at
108 ;; the mouse position. You can tell it to insert at point instead with:
110 ;; (setq mouse-yank-at-point t)
112 ;; * I like to leave point at the end of the region nearest to where the
113 ;; mouse was, even though this makes region highlighting mis-leading (the
114 ;; cursor makes it look like one extra character is selected). You can
115 ;; disable this behavior with:
117 ;; (setq mouse-sel-leave-point-near-mouse nil)
119 ;; * By default, mouse-select cycles the click count after 4 clicks. That
120 ;; is, clicking mouse-1 five times has the same effect as clicking it
121 ;; once, clicking six times has the same effect as clicking twice, etc.
122 ;; Disable this behavior with:
124 ;; (setq mouse-sel-cycle-clicks nil)
126 ;; * The variables mouse-sel-{set,get}-selection-function control how the
127 ;; selection is handled. Under X Windows, these variables default so
128 ;; that the X primary selection is used. Under other windowing systems,
129 ;; alternate functions are used, which simply store the selection value
130 ;; in a variable.
132 ;;; Code:
134 (require 'mouse)
135 (require 'thingatpt)
137 (eval-when-compile
138 (require 'cl))
140 ;;=== User Variables ======================================================
142 (defgroup mouse-sel nil
143 "Mouse selection enhancement."
144 :group 'mouse)
146 (defcustom mouse-sel-leave-point-near-mouse t
147 "Leave point near last mouse position.
148 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
149 of the region nearest to where the mouse last was.
150 If nil, point will always be placed at the beginning of the region."
151 :type 'boolean
152 :group 'mouse-sel)
154 (defcustom mouse-sel-cycle-clicks t
155 "If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks."
156 :type 'boolean
157 :group 'mouse-sel)
159 (defcustom mouse-sel-default-bindings t
160 "Control mouse bindings."
161 :type '(choice (const :tag "none" nil)
162 (const :tag "cut and paste" interprogram-cut-paste)
163 (other :tag "default bindings" t))
164 :group 'mouse-sel)
166 ;;=== Key bindings ========================================================
168 (defconst mouse-sel-bound-events
169 '(;; Primary selection bindings.
171 ;; Bind keys to `ignore' instead of unsetting them because modes may
172 ;; bind `down-mouse-1', for instance, without binding `mouse-1'.
173 ;; If we unset `mouse-1', this leads to a bitch_at_user when the
174 ;; mouse goes up because no matching binding is found for that.
175 ([mouse-1] . ignore)
176 ([drag-mouse-1] . ignore)
177 ([mouse-3] . ignore)
178 ([down-mouse-1] . mouse-select)
179 ([down-mouse-3] . mouse-extend)
180 ([mouse-2] . mouse-insert-selection)
181 ;; Secondary selection bindings.
182 ([M-mouse-1] . ignore)
183 ([M-drag-mouse-1] . ignore)
184 ([M-mouse-3] . ignore)
185 ([M-down-mouse-1] . mouse-select-secondary)
186 ([M-mouse-2] . mouse-insert-secondary)
187 ([M-down-mouse-3] . mouse-extend-secondary))
188 "An alist of events that `mouse-sel-mode' binds.")
190 ;;=== User Command ========================================================
192 (defvar mouse-sel-has-been-enabled nil
193 "Non-nil if Mouse Sel mode has been enabled at least once.")
195 (defvar mouse-sel-original-bindings nil)
196 (defvar mouse-sel-original-interprogram-cut-function nil)
197 (defvar mouse-sel-original-interprogram-paste-function nil)
199 ;;;###autoload
200 (define-minor-mode mouse-sel-mode
201 "Toggle Mouse Sel mode.
202 With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive.
203 Returns the new status of Mouse Sel mode (non-nil means on).
205 When Mouse Sel mode is enabled, mouse selection is enhanced in
206 various ways:
208 - Double-clicking on symbol constituents selects symbols.
209 Double-clicking on quotes or parentheses selects sexps.
210 Double-clicking on whitespace selects whitespace.
211 Triple-clicking selects lines.
212 Quad-clicking selects paragraphs.
214 - Selecting sets the region & X primary selection, but does NOT affect
215 the `kill-ring', nor do the kill-ring functions change the X selection.
216 Because the mouse handlers set the primary selection directly,
217 mouse-sel sets the variables `interprogram-cut-function' and
218 `interprogram-paste-function' to nil.
220 - Clicking mouse-2 inserts the contents of the primary selection at
221 the mouse position (or point, if `mouse-yank-at-point' is non-nil).
223 - mouse-2 while selecting or extending copies selection to the
224 kill ring; mouse-1 or mouse-3 kills it."
225 :global t
226 :group 'mouse-sel
227 (if mouse-sel-mode
228 (progn
229 ;; If mouse-2 has never been done by the user, initialize the
230 ;; `event-kind' property to ensure that `follow-link' clicks
231 ;; are interpreted correctly.
232 (put 'mouse-2 'event-kind 'mouse-click)
233 (add-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
234 (when mouse-sel-default-bindings
235 ;; Save original bindings and replace them with new ones.
236 (setq mouse-sel-original-bindings
237 (mapcar (lambda (binding)
238 (let ((event (car binding)))
239 (prog1 (cons event (lookup-key global-map event))
240 (global-set-key event (cdr binding)))))
241 mouse-sel-bound-events))
242 ;; Update interprogram functions.
243 (setq mouse-sel-original-interprogram-cut-function
244 interprogram-cut-function
245 mouse-sel-original-interprogram-paste-function
246 interprogram-paste-function
247 mouse-sel-has-been-enabled t)
248 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
249 (setq interprogram-cut-function nil
250 interprogram-paste-function nil))))
252 ;; Restore original bindings
253 (remove-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
254 (dolist (binding mouse-sel-original-bindings)
255 (global-set-key (car binding) (cdr binding)))
256 ;; Restore the old values of these variables,
257 ;; only if they were actually saved previously.
258 (if mouse-sel-has-been-enabled
259 (setq interprogram-cut-function
260 mouse-sel-original-interprogram-cut-function
261 interprogram-paste-function
262 mouse-sel-original-interprogram-paste-function))))
264 ;;=== Internal Variables/Constants ========================================
266 (defvar mouse-sel-primary-thing nil
267 "Type of PRIMARY selection in current buffer.")
268 (make-variable-buffer-local 'mouse-sel-primary-thing)
270 (defvar mouse-sel-secondary-thing nil
271 "Type of SECONDARY selection in current buffer.")
272 (make-variable-buffer-local 'mouse-sel-secondary-thing)
274 ;; Ensure that secondary overlay is defined
275 (unless (overlayp mouse-secondary-overlay)
276 (setq mouse-secondary-overlay (make-overlay 1 1))
277 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
279 (defconst mouse-sel-primary-overlay
280 (let ((ol (make-overlay (point-min) (point-min))))
281 (delete-overlay ol)
282 (overlay-put ol 'face 'region)
284 "An overlay which records the current primary selection.
285 This is used by Mouse Sel mode only.")
287 (defconst mouse-sel-selection-alist
288 '((PRIMARY mouse-sel-primary-overlay mouse-sel-primary-thing)
289 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
290 "Alist associating selections with variables.
291 Each element is of the form:
293 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
295 where SELECTION-NAME = name of selection
296 OVERLAY-SYMBOL = name of variable containing overlay to use
297 SELECTION-THING-SYMBOL = name of variable where the current selection
298 type for this selection should be stored.")
300 (declare-function x-select-text "term/common-win" (text))
302 (defvar mouse-sel-set-selection-function
303 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
304 'x-set-selection
305 (lambda (selection value)
306 (if (eq selection 'PRIMARY)
307 (x-select-text value)
308 (x-set-selection selection value))))
309 "Function to call to set selection.
310 Called with two arguments:
312 SELECTION, the name of the selection concerned, and
313 VALUE, the text to store.
315 This sets the selection, unless `mouse-sel-default-bindings'
316 is `interprogram-cut-paste'.")
318 (declare-function x-selection-value "term/x-win" ())
320 (defvar mouse-sel-get-selection-function
321 (lambda (selection)
322 (if (eq selection 'PRIMARY)
323 (or (x-selection-value)
324 (bound-and-true-p x-last-selected-text)
325 (bound-and-true-p x-last-selected-text-primary))
326 (x-get-selection selection)))
327 "Function to call to get the selection.
328 Called with one argument:
330 SELECTION: the name of the selection concerned.")
332 ;;=== Support/access functions ============================================
334 (defun mouse-sel-determine-selection-thing (nclicks)
335 "Determine what `thing' `mouse-sel' should operate on.
336 The first argument is NCLICKS, is the number of consecutive
337 mouse clicks at the same position.
339 Double-clicking on word constituents selects words.
340 Double-clicking on symbol constituents selects symbols.
341 Double-clicking on quotes or parentheses selects sexps.
342 Double-clicking on whitespace selects whitespace.
343 Triple-clicking selects lines.
344 Quad-clicking selects paragraphs.
346 Feel free to re-define this function to support your own desired
347 multi-click semantics."
348 (let* ((next-char (char-after (point)))
349 (char-syntax (if next-char (char-syntax next-char))))
350 (if mouse-sel-cycle-clicks
351 (setq nclicks (1+ (% (1- nclicks) 4))))
352 (cond
353 ((= nclicks 1) nil)
354 ((= nclicks 3) 'line)
355 ((>= nclicks 4) 'paragraph)
356 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
357 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
358 ((eq char-syntax ?_) 'symbol)
359 ((eq char-syntax ?w) 'word))))
361 (defun mouse-sel-set-selection (selection value)
362 "Set the specified SELECTION to VALUE."
363 (if mouse-sel-set-selection-function
364 (funcall mouse-sel-set-selection-function selection value)
365 (put 'mouse-sel-internal-selection selection value)))
367 (defun mouse-sel-get-selection (selection)
368 "Get the value of the specified SELECTION."
369 (if mouse-sel-get-selection-function
370 (funcall mouse-sel-get-selection-function selection)
371 (get 'mouse-sel-internal-selection selection)))
373 (defun mouse-sel-selection-overlay (selection)
374 "Return overlay corresponding to SELECTION."
375 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
376 (or symbol (error "No overlay corresponding to %s selection" selection))
377 (symbol-value symbol)))
379 (defun mouse-sel-selection-thing (selection)
380 "Return overlay corresponding to SELECTION."
381 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
382 (or symbol (error "No symbol corresponding to %s selection" selection))
383 symbol))
385 (defun mouse-sel-region-to-primary (orig-window)
386 "Convert region to PRIMARY overlay and deactivate region.
387 Argument ORIG-WINDOW specifies the window the cursor was in when the
388 originating command was issued, and is used to determine whether the
389 region was visible or not."
390 (if transient-mark-mode
391 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
392 (cond
393 ((and mark-active
394 (or highlight-nonselected-windows
395 (eq orig-window (selected-window))))
396 ;; Region was visible, so convert region to overlay
397 (move-overlay overlay (region-beginning) (region-end)
398 (current-buffer)))
399 ((eq orig-window (selected-window))
400 ;; Point was visible, so set overlay at point
401 (move-overlay overlay (point) (point) (current-buffer)))
403 ;; Nothing was visible, so remove overlay
404 (delete-overlay overlay)))
405 (setq mark-active nil))))
407 (defun mouse-sel-primary-to-region (&optional direction)
408 "Convert PRIMARY overlay to region.
409 Optional argument DIRECTION specifies the mouse drag direction: a value of
410 1 indicates that the mouse was dragged left-to-right, otherwise it was
411 dragged right-to-left."
412 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
413 (start (overlay-start overlay))
414 (end (overlay-end overlay)))
415 (if (eq start end)
416 (progn
417 (if start (goto-char start))
418 (deactivate-mark))
419 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
420 (progn
421 (goto-char end)
422 (push-mark start 'nomsg 'active))
423 (goto-char start)
424 (push-mark end 'nomsg 'active)))
425 (if transient-mark-mode (delete-overlay overlay))))
427 (defmacro mouse-sel-eval-at-event-end (event &rest forms)
428 "Evaluate forms at mouse position.
429 Move to the end position of EVENT, execute FORMS, and restore original
430 point and window."
431 `(let ((posn (event-end ,event)))
432 (if posn (mouse-minibuffer-check ,event))
433 (if (and posn (not (windowp (posn-window posn))))
434 (error "Cursor not in text area of window"))
435 (let (orig-window orig-point-marker)
436 (setq orig-window (selected-window))
437 (if posn (select-window (posn-window posn)))
438 (setq orig-point-marker (point-marker))
439 (if (and posn (numberp (posn-point posn)))
440 (goto-char (posn-point posn)))
441 (unwind-protect
442 (progn
443 ,@forms)
444 (goto-char (marker-position orig-point-marker))
445 (move-marker orig-point-marker nil)
446 (select-window orig-window)))))
448 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
450 ;;=== Select ==============================================================
452 (defun mouse-select (event)
453 "Set region/selection using the mouse.
455 Click sets point & mark to click position.
456 Dragging extends region/selection.
458 Multi-clicking selects word/lines/paragraphs, as determined by
459 'mouse-sel-determine-selection-thing.
461 Clicking mouse-2 while selecting copies selected text to the kill-ring.
462 Clicking mouse-1 or mouse-3 kills the selected text.
464 This should be bound to a down-mouse event."
465 (interactive "@e")
466 (let (select)
467 (unwind-protect
468 (setq select (mouse-select-internal 'PRIMARY event))
469 (if (and select (listp select))
470 (push (cons 'mouse-2 (cdr event)) unread-command-events)
471 (mouse-sel-primary-to-region select)))))
473 (defun mouse-select-secondary (event)
474 "Set secondary selection using the mouse.
476 Click sets the start of the secondary selection to click position.
477 Dragging extends the secondary selection.
479 Multi-clicking selects word/lines/paragraphs, as determined by
480 'mouse-sel-determine-selection-thing.
482 Clicking mouse-2 while selecting copies selected text to the kill-ring.
483 Clicking mouse-1 or mouse-3 kills the selected text.
485 This should be bound to a down-mouse event."
486 (interactive "e")
487 (mouse-select-internal 'SECONDARY event))
489 (defun mouse-select-internal (selection event)
490 "Set SELECTION using the mouse, with EVENT as the initial down-event.
491 Normally, this returns the direction in which the selection was
492 made: a value of 1 indicates that the mouse was dragged
493 left-to-right, otherwise it was dragged right-to-left.
495 However, if `mouse-1-click-follows-link' is non-nil and the
496 subsequent mouse events specify following a link, this returns
497 the final mouse-event. In that case, the selection is not set."
498 (mouse-sel-eval-at-event-end event
499 (let ((thing-symbol (mouse-sel-selection-thing selection))
500 (overlay (mouse-sel-selection-overlay selection)))
501 (set thing-symbol
502 (mouse-sel-determine-selection-thing (event-click-count event)))
503 (let ((object-bounds (bounds-of-thing-at-point
504 (symbol-value thing-symbol))))
505 (if object-bounds
506 (progn
507 (move-overlay overlay
508 (car object-bounds) (cdr object-bounds)
509 (current-buffer)))
510 (move-overlay overlay (point) (point) (current-buffer)))))
511 (catch 'follow-link
512 (mouse-extend-internal selection event t))))
514 ;;=== Extend ==============================================================
516 (defun mouse-extend (event)
517 "Extend region/selection using the mouse."
518 (interactive "e")
519 (let ((orig-window (selected-window))
520 direction)
521 (select-window (posn-window (event-end event)))
522 (unwind-protect
523 (progn
524 (mouse-sel-region-to-primary orig-window)
525 (setq direction (mouse-extend-internal 'PRIMARY event)))
526 (mouse-sel-primary-to-region direction))))
528 (defun mouse-extend-secondary (event)
529 "Extend secondary selection using the mouse."
530 (interactive "e")
531 (save-window-excursion
532 (mouse-extend-internal 'SECONDARY event)))
534 (defun mouse-extend-internal (selection &optional initial-event no-process)
535 "Extend specified SELECTION using the mouse.
536 Track mouse-motion events, adjusting the SELECTION appropriately.
537 Optional argument INITIAL-EVENT specifies an initial down-mouse event.
538 Optional argument NO-PROCESS means not to process the initial
539 event.
541 See documentation for mouse-select-internal for more details."
542 (mouse-sel-eval-at-event-end initial-event
543 (let ((orig-cursor-type
544 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
545 (unwind-protect
547 (let* ((thing-symbol (mouse-sel-selection-thing selection))
548 (overlay (mouse-sel-selection-overlay selection))
549 (orig-window (selected-window))
550 (top (nth 1 (window-edges orig-window)))
551 (bottom (nth 3 (window-edges orig-window)))
552 (mark-active nil) ; inhibit normal region highlight
553 (echo-keystrokes 0) ; don't echo mouse events
554 min max
555 direction
556 event)
558 ;; Get current bounds of overlay
559 (if (eq (overlay-buffer overlay) (current-buffer))
560 (setq min (overlay-start overlay)
561 max (overlay-end overlay))
562 (setq min (point)
563 max min)
564 (set thing-symbol nil))
567 ;; Bar cursor
568 (if (fboundp 'modify-frame-parameters)
569 (modify-frame-parameters (selected-frame)
570 '((cursor-type . bar))))
572 ;; Handle dragging
573 (track-mouse
575 (while (if (and initial-event (not no-process))
576 ;; Use initial event
577 (prog1
578 (setq event initial-event)
579 (setq initial-event nil))
580 (setq event (read-event))
581 (and (consp event)
582 (memq (car event) '(mouse-movement switch-frame))))
584 (let ((selection-thing (symbol-value thing-symbol))
585 (end (event-end event)))
587 (cond
589 ;; Ignore any movement outside the frame
590 ((eq (car-safe event) 'switch-frame) nil)
591 ((and (posn-window end)
592 (not (eq (let ((posn-w (posn-window end)))
593 (if (windowp posn-w)
594 (window-frame posn-w)
595 posn-w))
596 (window-frame orig-window)))) nil)
598 ;; Different window, same frame
599 ((not (eq (posn-window end) orig-window))
600 (let ((end-row (cdr (cdr (mouse-position)))))
601 (cond
602 ((and end-row (not (bobp)) (< end-row top))
603 (mouse-scroll-subr orig-window (- end-row top)
604 overlay max))
605 ((and end-row (not (eobp)) (>= end-row bottom))
606 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
607 overlay min))
610 ;; On the mode line
611 ((eq (posn-point end) 'mode-line)
612 (mouse-scroll-subr orig-window 1 overlay min))
614 ;; In original window
615 (t (goto-char (posn-point end)))
619 ;; Determine direction of drag
620 (cond
621 ((and (not direction) (not (eq min max)))
622 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
623 ((and (not (eq direction -1)) (<= (point) min))
624 (setq direction -1))
625 ((and (not (eq direction 1)) (>= (point) max))
626 (setq direction 1)))
628 (if (not selection-thing) nil
630 ;; If dragging forward, goal is next character
631 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
633 ;; Move to start/end of selected thing
634 (let ((goal (point)))
635 (goto-char (if (eq 1 direction) min max))
636 (condition-case nil
637 (progn
638 (while (> (* direction (- goal (point))) 0)
639 (forward-thing selection-thing direction))
640 (let ((end (point)))
641 (forward-thing selection-thing (- direction))
642 (goto-char
643 (if (> (* direction (- goal (point))) 0)
644 end (point)))))
645 (error))))
647 ;; Move overlay
648 (move-overlay overlay
649 (if (eq 1 direction) min (point))
650 (if (eq -1 direction) max (point))
651 (current-buffer))
653 ))) ; end track-mouse
655 ;; Detect follow-link events
656 (when (mouse-sel-follow-link-p initial-event event)
657 (throw 'follow-link event))
659 ;; Finish up after dragging
660 (let ((overlay-start (overlay-start overlay))
661 (overlay-end (overlay-end overlay)))
663 ;; Set selection
664 (if (not (eq overlay-start overlay-end))
665 (mouse-sel-set-selection
666 selection
667 (buffer-substring overlay-start overlay-end)))
669 ;; Handle copy/kill
670 (let (this-command)
671 (cond
672 ((eq (event-basic-type last-input-event) 'mouse-2)
673 (copy-region-as-kill overlay-start overlay-end)
674 (read-event) (read-event))
675 ((and (memq (event-basic-type last-input-event)
676 '(mouse-1 mouse-3))
677 (memq 'down (event-modifiers last-input-event)))
678 (kill-region overlay-start overlay-end)
679 (move-overlay overlay overlay-start overlay-start)
680 (read-event) (read-event))
681 ((and (eq (event-basic-type last-input-event) 'mouse-3)
682 (memq 'double (event-modifiers last-input-event)))
683 (kill-region overlay-start overlay-end)
684 (move-overlay overlay overlay-start overlay-start)))))
686 direction)
688 ;; Restore cursor
689 (if (fboundp 'modify-frame-parameters)
690 (modify-frame-parameters
691 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
693 ))))
695 (defun mouse-sel-follow-link-p (initial final)
696 "Return t if we should follow a link, given INITIAL and FINAL mouse events.
697 See `mouse-1-click-follows-link' for details. Currently, Mouse
698 Sel mode does not support using a `double' value to follow links
699 using double-clicks."
700 (and initial final mouse-1-click-follows-link
701 (eq (car initial) 'down-mouse-1)
702 (mouse-on-link-p (event-start initial))
703 (= (posn-point (event-start initial))
704 (posn-point (event-end final)))
705 (= (event-click-count initial) 1)
706 (or (not (integerp mouse-1-click-follows-link))
707 (let ((t0 (posn-timestamp (event-start initial)))
708 (t1 (posn-timestamp (event-end final))))
709 (and (integerp t0) (integerp t1)
710 (if (> mouse-1-click-follows-link 0)
711 (<= (- t1 t0) mouse-1-click-follows-link)
712 (< (- t0 t1) mouse-1-click-follows-link)))))))
714 ;;=== Paste ===============================================================
716 (defun mouse-insert-selection (event arg)
717 "Insert the contents of the PRIMARY selection at mouse click.
718 If `mouse-yank-at-point' is non-nil, insert at point instead."
719 (interactive "e\nP")
720 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
721 (mouse-yank-at-click event arg)
722 (mouse-insert-selection-internal 'PRIMARY event)))
724 (defun mouse-insert-secondary (event)
725 "Insert the contents of the SECONDARY selection at mouse click.
726 If `mouse-yank-at-point' is non-nil, insert at point instead."
727 (interactive "e")
728 (mouse-insert-selection-internal 'SECONDARY event))
730 (defun mouse-insert-selection-internal (selection event)
731 "Insert the contents of the named SELECTION at mouse click.
732 If `mouse-yank-at-point' is non-nil, insert at point instead."
733 (unless mouse-yank-at-point
734 (mouse-set-point event))
735 (when mouse-sel-get-selection-function
736 (push-mark (point) 'nomsg)
737 (insert-for-yank
738 (or (funcall mouse-sel-get-selection-function selection) ""))))
740 ;;=== Handle loss of selections ===========================================
742 (defun mouse-sel-lost-selection-hook (selection)
743 "Remove the overlay for a lost selection."
744 (let ((overlay (mouse-sel-selection-overlay selection)))
745 (delete-overlay overlay)))
747 (provide 'mouse-sel)
749 ;;; mouse-sel.el ends here