Remove unnecessary casts.
[emacs.git] / lisp / mouse-sel.el
blob1f601377ad45829d01ce575cc9368b5d9106b95a
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 various ways:
207 - Clicking mouse-1 starts (cancels) selection, dragging extends it.
209 - Clicking or dragging mouse-3 extends the selection as well.
211 - Double-clicking on word constituents selects words.
212 Double-clicking on symbol constituents selects symbols.
213 Double-clicking on quotes or parentheses selects sexps.
214 Double-clicking on whitespace selects whitespace.
215 Triple-clicking selects lines.
216 Quad-clicking selects paragraphs.
218 - Selecting sets the region & X primary selection, but does NOT affect
219 the `kill-ring', nor do the kill-ring functions change the X selection.
220 Because the mouse handlers set the primary selection directly,
221 mouse-sel sets the variables `interprogram-cut-function' and
222 `interprogram-paste-function' to nil.
224 - Clicking mouse-2 inserts the contents of the primary selection at
225 the mouse position (or point, if `mouse-yank-at-point' is non-nil).
227 - Pressing mouse-2 while selecting or extending copies selection
228 to the kill ring. Pressing mouse-1 or mouse-3 kills it.
230 - Double-clicking mouse-3 also kills selection.
232 - M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
233 & mouse-3, but operate on the X secondary selection rather than the
234 primary selection and region."
235 :global t
236 :group 'mouse-sel
237 (if mouse-sel-mode
238 (progn
239 ;; If mouse-2 has never been done by the user, initialize the
240 ;; `event-kind' property to ensure that `follow-link' clicks
241 ;; are interpreted correctly.
242 (put 'mouse-2 'event-kind 'mouse-click)
243 (add-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
244 (when mouse-sel-default-bindings
245 ;; Save original bindings and replace them with new ones.
246 (setq mouse-sel-original-bindings
247 (mapcar (lambda (binding)
248 (let ((event (car binding)))
249 (prog1 (cons event (lookup-key global-map event))
250 (global-set-key event (cdr binding)))))
251 mouse-sel-bound-events))
252 ;; Update interprogram functions.
253 (setq mouse-sel-original-interprogram-cut-function
254 interprogram-cut-function
255 mouse-sel-original-interprogram-paste-function
256 interprogram-paste-function
257 mouse-sel-has-been-enabled t)
258 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
259 (setq interprogram-cut-function nil
260 interprogram-paste-function nil))))
262 ;; Restore original bindings
263 (remove-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
264 (dolist (binding mouse-sel-original-bindings)
265 (global-set-key (car binding) (cdr binding)))
266 ;; Restore the old values of these variables,
267 ;; only if they were actually saved previously.
268 (if mouse-sel-has-been-enabled
269 (setq interprogram-cut-function
270 mouse-sel-original-interprogram-cut-function
271 interprogram-paste-function
272 mouse-sel-original-interprogram-paste-function))))
274 ;;=== Internal Variables/Constants ========================================
276 (defvar mouse-sel-primary-thing nil
277 "Type of PRIMARY selection in current buffer.")
278 (make-variable-buffer-local 'mouse-sel-primary-thing)
280 (defvar mouse-sel-secondary-thing nil
281 "Type of SECONDARY selection in current buffer.")
282 (make-variable-buffer-local 'mouse-sel-secondary-thing)
284 ;; Ensure that secondary overlay is defined
285 (unless (overlayp mouse-secondary-overlay)
286 (setq mouse-secondary-overlay (make-overlay 1 1))
287 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
289 (defconst mouse-sel-selection-alist
290 '((SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
291 "Alist associating selections with variables.
292 Each element is of the form:
294 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
296 where SELECTION-NAME = name of selection
297 OVERLAY-SYMBOL = name of variable containing overlay to use
298 SELECTION-THING-SYMBOL = name of variable where the current selection
299 type for this selection should be stored.")
301 (declare-function x-select-text "term/common-win" (text))
303 (defvar mouse-sel-set-selection-function
304 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
305 'x-set-selection
306 (lambda (selection value)
307 (if (eq selection 'PRIMARY)
308 (x-select-text value)
309 (x-set-selection selection value))))
310 "Function to call to set selection.
311 Called with two arguments:
313 SELECTION, the name of the selection concerned, and
314 VALUE, the text to store.
316 This sets the selection, unless `mouse-sel-default-bindings'
317 is `interprogram-cut-paste'.")
319 (declare-function x-selection-value "term/x-win" ())
321 (defvar mouse-sel-get-selection-function
322 (lambda (selection)
323 (if (eq selection 'PRIMARY)
324 (or (x-selection-value)
325 (bound-and-true-p x-last-selected-text)
326 (bound-and-true-p x-last-selected-text-primary))
327 (x-get-selection selection)))
328 "Function to call to get the selection.
329 Called with one argument:
331 SELECTION: the name of the selection concerned.")
333 ;;=== Support/access functions ============================================
335 (defun mouse-sel-determine-selection-thing (nclicks)
336 "Determine what `thing' `mouse-sel' should operate on.
337 The first argument is NCLICKS, is the number of consecutive
338 mouse clicks at the same position.
340 Double-clicking on word constituents selects words.
341 Double-clicking on symbol constituents selects symbols.
342 Double-clicking on quotes or parentheses selects sexps.
343 Double-clicking on whitespace selects whitespace.
344 Triple-clicking selects lines.
345 Quad-clicking selects paragraphs.
347 Feel free to re-define this function to support your own desired
348 multi-click semantics."
349 (let* ((next-char (char-after (point)))
350 (char-syntax (if next-char (char-syntax next-char))))
351 (if mouse-sel-cycle-clicks
352 (setq nclicks (1+ (% (1- nclicks) 4))))
353 (cond
354 ((= nclicks 1) nil)
355 ((= nclicks 3) 'line)
356 ((>= nclicks 4) 'paragraph)
357 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
358 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
359 ((eq char-syntax ?_) 'symbol)
360 ((eq char-syntax ?w) 'word))))
362 (defun mouse-sel-set-selection (selection value)
363 "Set the specified SELECTION to VALUE."
364 (if mouse-sel-set-selection-function
365 (funcall mouse-sel-set-selection-function selection value)
366 (put 'mouse-sel-internal-selection selection value)))
368 (defun mouse-sel-get-selection (selection)
369 "Get the value of the specified SELECTION."
370 (if mouse-sel-get-selection-function
371 (funcall mouse-sel-get-selection-function selection)
372 (get 'mouse-sel-internal-selection selection)))
374 (defun mouse-sel-selection-overlay (selection)
375 "Return overlay corresponding to SELECTION."
376 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
377 (or symbol (error "No overlay corresponding to %s selection" selection))
378 (symbol-value symbol)))
380 (defun mouse-sel-selection-thing (selection)
381 "Return overlay corresponding to SELECTION."
382 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
383 (or symbol (error "No symbol corresponding to %s selection" selection))
384 symbol))
386 (defun mouse-sel-region-to-primary (orig-window)
387 "Convert region to PRIMARY overlay and deactivate region.
388 Argument ORIG-WINDOW specifies the window the cursor was in when the
389 originating command was issued, and is used to determine whether the
390 region was visible or not."
391 (if transient-mark-mode
392 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
393 (cond
394 ((and mark-active
395 (or highlight-nonselected-windows
396 (eq orig-window (selected-window))))
397 ;; Region was visible, so convert region to overlay
398 (move-overlay overlay (region-beginning) (region-end)
399 (current-buffer)))
400 ((eq orig-window (selected-window))
401 ;; Point was visible, so set overlay at point
402 (move-overlay overlay (point) (point) (current-buffer)))
404 ;; Nothing was visible, so remove overlay
405 (delete-overlay overlay)))
406 (setq mark-active nil))))
408 (defun mouse-sel-primary-to-region (&optional direction)
409 "Convert PRIMARY overlay to region.
410 Optional argument DIRECTION specifies the mouse drag direction: a value of
411 1 indicates that the mouse was dragged left-to-right, otherwise it was
412 dragged right-to-left."
413 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
414 (start (overlay-start overlay))
415 (end (overlay-end overlay)))
416 (if (eq start end)
417 (progn
418 (if start (goto-char start))
419 (deactivate-mark))
420 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
421 (progn
422 (goto-char end)
423 (push-mark start 'nomsg 'active))
424 (goto-char start)
425 (push-mark end 'nomsg 'active)))
426 (if transient-mark-mode (delete-overlay overlay))))
428 (defmacro mouse-sel-eval-at-event-end (event &rest forms)
429 "Evaluate forms at mouse position.
430 Move to the end position of EVENT, execute FORMS, and restore original
431 point and window."
432 `(let ((posn (event-end ,event)))
433 (if posn (mouse-minibuffer-check ,event))
434 (if (and posn (not (windowp (posn-window posn))))
435 (error "Cursor not in text area of window"))
436 (let (orig-window orig-point-marker)
437 (setq orig-window (selected-window))
438 (if posn (select-window (posn-window posn)))
439 (setq orig-point-marker (point-marker))
440 (if (and posn (numberp (posn-point posn)))
441 (goto-char (posn-point posn)))
442 (unwind-protect
443 (progn
444 ,@forms)
445 (goto-char (marker-position orig-point-marker))
446 (move-marker orig-point-marker nil)
447 (select-window orig-window)))))
449 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
451 ;;=== Select ==============================================================
453 (defun mouse-select (event)
454 "Set region/selection using the mouse.
456 Click sets point & mark to click position.
457 Dragging extends region/selection.
459 Multi-clicking selects word/lines/paragraphs, as determined by
460 'mouse-sel-determine-selection-thing.
462 Clicking mouse-2 while selecting copies selected text to the kill-ring.
463 Clicking mouse-1 or mouse-3 kills the selected text.
465 This should be bound to a down-mouse event."
466 (interactive "@e")
467 (let (select)
468 (unwind-protect
469 (setq select (mouse-select-internal 'PRIMARY event))
470 (if (and select (listp select))
471 (push (cons 'mouse-2 (cdr event)) unread-command-events)
472 (mouse-sel-primary-to-region select)))))
474 (defun mouse-select-secondary (event)
475 "Set secondary selection using the mouse.
477 Click sets the start of the secondary selection to click position.
478 Dragging extends the secondary selection.
480 Multi-clicking selects word/lines/paragraphs, as determined by
481 'mouse-sel-determine-selection-thing.
483 Clicking mouse-2 while selecting copies selected text to the kill-ring.
484 Clicking mouse-1 or mouse-3 kills the selected text.
486 This should be bound to a down-mouse event."
487 (interactive "e")
488 (mouse-select-internal 'SECONDARY event))
490 (defun mouse-select-internal (selection event)
491 "Set SELECTION using the mouse, with EVENT as the initial down-event.
492 Normally, this returns the direction in which the selection was
493 made: a value of 1 indicates that the mouse was dragged
494 left-to-right, otherwise it was dragged right-to-left.
496 However, if `mouse-1-click-follows-link' is non-nil and the
497 subsequent mouse events specify following a link, this returns
498 the final mouse-event. In that case, the selection is not set."
499 (mouse-sel-eval-at-event-end event
500 (let ((thing-symbol (mouse-sel-selection-thing selection))
501 (overlay (mouse-sel-selection-overlay selection)))
502 (set thing-symbol
503 (mouse-sel-determine-selection-thing (event-click-count event)))
504 (let ((object-bounds (bounds-of-thing-at-point
505 (symbol-value thing-symbol))))
506 (if object-bounds
507 (progn
508 (move-overlay overlay
509 (car object-bounds) (cdr object-bounds)
510 (current-buffer)))
511 (move-overlay overlay (point) (point) (current-buffer)))))
512 (catch 'follow-link
513 (mouse-extend-internal selection event t))))
515 ;;=== Extend ==============================================================
517 (defun mouse-extend (event)
518 "Extend region/selection using the mouse."
519 (interactive "e")
520 (let ((orig-window (selected-window))
521 direction)
522 (select-window (posn-window (event-end event)))
523 (unwind-protect
524 (progn
525 (mouse-sel-region-to-primary orig-window)
526 (setq direction (mouse-extend-internal 'PRIMARY event)))
527 (mouse-sel-primary-to-region direction))))
529 (defun mouse-extend-secondary (event)
530 "Extend secondary selection using the mouse."
531 (interactive "e")
532 (save-window-excursion
533 (mouse-extend-internal 'SECONDARY event)))
535 (defun mouse-extend-internal (selection &optional initial-event no-process)
536 "Extend specified SELECTION using the mouse.
537 Track mouse-motion events, adjusting the SELECTION appropriately.
538 Optional argument INITIAL-EVENT specifies an initial down-mouse event.
539 Optional argument NO-PROCESS means not to process the initial
540 event.
542 See documentation for mouse-select-internal for more details."
543 (mouse-sel-eval-at-event-end initial-event
544 (let ((orig-cursor-type
545 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
546 (unwind-protect
548 (let* ((thing-symbol (mouse-sel-selection-thing selection))
549 (overlay (mouse-sel-selection-overlay selection))
550 (orig-window (selected-window))
551 (top (nth 1 (window-edges orig-window)))
552 (bottom (nth 3 (window-edges orig-window)))
553 (mark-active nil) ; inhibit normal region highlight
554 (echo-keystrokes 0) ; don't echo mouse events
555 min max
556 direction
557 event)
559 ;; Get current bounds of overlay
560 (if (eq (overlay-buffer overlay) (current-buffer))
561 (setq min (overlay-start overlay)
562 max (overlay-end overlay))
563 (setq min (point)
564 max min)
565 (set thing-symbol nil))
568 ;; Bar cursor
569 (if (fboundp 'modify-frame-parameters)
570 (modify-frame-parameters (selected-frame)
571 '((cursor-type . bar))))
573 ;; Handle dragging
574 (track-mouse
576 (while (if (and initial-event (not no-process))
577 ;; Use initial event
578 (prog1
579 (setq event initial-event)
580 (setq initial-event nil))
581 (setq event (read-event))
582 (and (consp event)
583 (memq (car event) '(mouse-movement switch-frame))))
585 (let ((selection-thing (symbol-value thing-symbol))
586 (end (event-end event)))
588 (cond
590 ;; Ignore any movement outside the frame
591 ((eq (car-safe event) 'switch-frame) nil)
592 ((and (posn-window end)
593 (not (eq (let ((posn-w (posn-window end)))
594 (if (windowp posn-w)
595 (window-frame posn-w)
596 posn-w))
597 (window-frame orig-window)))) nil)
599 ;; Different window, same frame
600 ((not (eq (posn-window end) orig-window))
601 (let ((end-row (cdr (cdr (mouse-position)))))
602 (cond
603 ((and end-row (not (bobp)) (< end-row top))
604 (mouse-scroll-subr orig-window (- end-row top)
605 overlay max))
606 ((and end-row (not (eobp)) (>= end-row bottom))
607 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
608 overlay min))
611 ;; On the mode line
612 ((eq (posn-point end) 'mode-line)
613 (mouse-scroll-subr orig-window 1 overlay min))
615 ;; In original window
616 (t (goto-char (posn-point end)))
620 ;; Determine direction of drag
621 (cond
622 ((and (not direction) (not (eq min max)))
623 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
624 ((and (not (eq direction -1)) (<= (point) min))
625 (setq direction -1))
626 ((and (not (eq direction 1)) (>= (point) max))
627 (setq direction 1)))
629 (if (not selection-thing) nil
631 ;; If dragging forward, goal is next character
632 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
634 ;; Move to start/end of selected thing
635 (let ((goal (point)))
636 (goto-char (if (eq 1 direction) min max))
637 (condition-case nil
638 (progn
639 (while (> (* direction (- goal (point))) 0)
640 (forward-thing selection-thing direction))
641 (let ((end (point)))
642 (forward-thing selection-thing (- direction))
643 (goto-char
644 (if (> (* direction (- goal (point))) 0)
645 end (point)))))
646 (error))))
648 ;; Move overlay
649 (move-overlay overlay
650 (if (eq 1 direction) min (point))
651 (if (eq -1 direction) max (point))
652 (current-buffer))
654 ))) ; end track-mouse
656 ;; Detect follow-link events
657 (when (mouse-sel-follow-link-p initial-event event)
658 (throw 'follow-link event))
660 ;; Finish up after dragging
661 (let ((overlay-start (overlay-start overlay))
662 (overlay-end (overlay-end overlay)))
664 ;; Set selection
665 (if (not (eq overlay-start overlay-end))
666 (mouse-sel-set-selection
667 selection
668 (buffer-substring overlay-start overlay-end)))
670 ;; Handle copy/kill
671 (let (this-command)
672 (cond
673 ((eq (event-basic-type last-input-event) 'mouse-2)
674 (copy-region-as-kill overlay-start overlay-end)
675 (read-event) (read-event))
676 ((and (memq (event-basic-type last-input-event)
677 '(mouse-1 mouse-3))
678 (memq 'down (event-modifiers last-input-event)))
679 (kill-region overlay-start overlay-end)
680 (move-overlay overlay overlay-start overlay-start)
681 (read-event) (read-event))
682 ((and (eq (event-basic-type last-input-event) 'mouse-3)
683 (memq 'double (event-modifiers last-input-event)))
684 (kill-region overlay-start overlay-end)
685 (move-overlay overlay overlay-start overlay-start)))))
687 direction)
689 ;; Restore cursor
690 (if (fboundp 'modify-frame-parameters)
691 (modify-frame-parameters
692 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
694 ))))
696 (defun mouse-sel-follow-link-p (initial final)
697 "Return t if we should follow a link, given INITIAL and FINAL mouse events.
698 See `mouse-1-click-follows-link' for details. Currently, Mouse
699 Sel mode does not support using a `double' value to follow links
700 using double-clicks."
701 (and initial final mouse-1-click-follows-link
702 (eq (car initial) 'down-mouse-1)
703 (mouse-on-link-p (event-start initial))
704 (= (posn-point (event-start initial))
705 (posn-point (event-end final)))
706 (= (event-click-count initial) 1)
707 (or (not (integerp mouse-1-click-follows-link))
708 (let ((t0 (posn-timestamp (event-start initial)))
709 (t1 (posn-timestamp (event-end final))))
710 (and (integerp t0) (integerp t1)
711 (if (> mouse-1-click-follows-link 0)
712 (<= (- t1 t0) mouse-1-click-follows-link)
713 (< (- t0 t1) mouse-1-click-follows-link)))))))
715 ;;=== Paste ===============================================================
717 (defun mouse-insert-selection (event arg)
718 "Insert the contents of the PRIMARY selection at mouse click.
719 If `mouse-yank-at-point' is non-nil, insert at point instead."
720 (interactive "e\nP")
721 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
722 (mouse-yank-at-click event arg)
723 (mouse-insert-selection-internal 'PRIMARY event)))
725 (defun mouse-insert-secondary (event)
726 "Insert the contents of the SECONDARY selection at mouse click.
727 If `mouse-yank-at-point' is non-nil, insert at point instead."
728 (interactive "e")
729 (mouse-insert-selection-internal 'SECONDARY event))
731 (defun mouse-insert-selection-internal (selection event)
732 "Insert the contents of the named SELECTION at mouse click.
733 If `mouse-yank-at-point' is non-nil, insert at point instead."
734 (unless mouse-yank-at-point
735 (mouse-set-point event))
736 (when mouse-sel-get-selection-function
737 (push-mark (point) 'nomsg)
738 (insert-for-yank
739 (or (funcall mouse-sel-get-selection-function selection) ""))))
741 ;;=== Handle loss of selections ===========================================
743 (defun mouse-sel-lost-selection-hook (selection)
744 "Remove the overlay for a lost selection."
745 (let ((overlay (mouse-sel-selection-overlay selection)))
746 (delete-overlay overlay)))
748 (provide 'mouse-sel)
750 ;;; mouse-sel.el ends here