]> code.delx.au - gnu-emacs/blob - lisp/mouse-sel.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / mouse-sel.el
1 ;;; mouse-sel.el --- multi-click selection support
2
3 ;; Copyright (C) 1993-1995, 2001-2011 Free Software Foundation, Inc.
4
5 ;; Author: Mike Williams <mdub@bigfoot.com>
6 ;; Keywords: mouse
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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:
29 ;;
30 ;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
31 ;;
32 ;; * Clicking or dragging mouse-3 extends the selection as well.
33 ;;
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.
40 ;;
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.
45 ;;
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).
48 ;;
49 ;; * Pressing mouse-2 while selecting or extending copies selection
50 ;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
51 ;;
52 ;; * Double-clicking mouse-3 also kills selection.
53 ;;
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.
57 ;;
58 ;; This module requires my thingatpt.el module, which it uses to find the
59 ;; bounds of words, lines, sexps, etc.
60 ;;
61 ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
62 ;;
63 ;;--- Customisation -------------------------------------------------------
64 ;;
65 ;; * You may want to use none or more of following:
66 ;;
67 ;; ;; Enable region highlight
68 ;; (transient-mark-mode 1)
69 ;;
70 ;; ;; But only in the selected window
71 ;; (setq highlight-nonselected-windows nil)
72 ;;
73 ;; ;; Enable pending-delete
74 ;; (delete-selection-mode 1)
75 ;;
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.
78 ;;
79 ;; (a) If mouse-sel-default-bindings = t (the default)
80 ;;
81 ;; Mouse sets and insert selection
82 ;; mouse-1 mouse-select
83 ;; mouse-2 mouse-insert-selection
84 ;; mouse-3 mouse-extend
85 ;;
86 ;; Selection/kill-ring interaction is disabled
87 ;; interprogram-cut-function = nil
88 ;; interprogram-paste-function = nil
89 ;;
90 ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
91 ;;
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.
97 ;;
98 ;; Selection/kill-ring interaction is retained
99 ;; interprogram-cut-function = x-select-text
100 ;; interprogram-paste-function = x-selection-value
101 ;;
102 ;; What you lose is the ability to select some text in
103 ;; delete-selection-mode and yank over the top of it.
104 ;;
105 ;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
106 ;;
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:
109 ;;
110 ;; (setq mouse-yank-at-point t)
111 ;;
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:
116 ;;
117 ;; (setq mouse-sel-leave-point-near-mouse nil)
118 ;;
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:
123 ;;
124 ;; (setq mouse-sel-cycle-clicks nil)
125 ;;
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.
131
132 ;;; Code:
133
134 (require 'mouse)
135 (require 'thingatpt)
136
137 (eval-when-compile
138 (require 'cl))
139
140 ;;=== User Variables ======================================================
141
142 (defgroup mouse-sel nil
143 "Mouse selection enhancement."
144 :group 'mouse)
145
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)
153
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)
158
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)
165
166 ;;=== Key bindings ========================================================
167
168 (defconst mouse-sel-bound-events
169 '(;; Primary selection bindings.
170 ;;
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.")
189
190 ;;=== User Command ========================================================
191
192 (defvar mouse-sel-has-been-enabled nil
193 "Non-nil if Mouse Sel mode has been enabled at least once.")
194
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)
198
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).
204
205 When Mouse Sel mode is enabled, mouse selection is enhanced in various ways:
206
207 - Clicking mouse-1 starts (cancels) selection, dragging extends it.
208
209 - Clicking or dragging mouse-3 extends the selection as well.
210
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.
217
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.
223
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).
226
227 - Pressing mouse-2 while selecting or extending copies selection
228 to the kill ring. Pressing mouse-1 or mouse-3 kills it.
229
230 - Double-clicking mouse-3 also kills selection.
231
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))))
261
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))))
273
274 ;;=== Internal Variables/Constants ========================================
275
276 (defvar mouse-sel-primary-thing nil
277 "Type of PRIMARY selection in current buffer.")
278 (make-variable-buffer-local 'mouse-sel-primary-thing)
279
280 (defvar mouse-sel-secondary-thing nil
281 "Type of SECONDARY selection in current buffer.")
282 (make-variable-buffer-local 'mouse-sel-secondary-thing)
283
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))
288
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:
293
294 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
295
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.")
300
301 (declare-function x-select-text "term/common-win" (text))
302
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:
312
313 SELECTION, the name of the selection concerned, and
314 VALUE, the text to store.
315
316 This sets the selection, unless `mouse-sel-default-bindings'
317 is `interprogram-cut-paste'.")
318
319 (declare-function x-selection-value "term/x-win" ())
320
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:
330
331 SELECTION: the name of the selection concerned.")
332
333 ;;=== Support/access functions ============================================
334
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.
339
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.
346
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))))
361
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)))
367
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)))
373
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)))
379
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))
385
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)))
403 (t
404 ;; Nothing was visible, so remove overlay
405 (delete-overlay overlay)))
406 (setq mark-active nil))))
407
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))))
427
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)))))
448
449 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
450
451 ;;=== Select ==============================================================
452
453 (defun mouse-select (event)
454 "Set region/selection using the mouse.
455
456 Click sets point & mark to click position.
457 Dragging extends region/selection.
458
459 Multi-clicking selects word/lines/paragraphs, as determined by
460 'mouse-sel-determine-selection-thing.
461
462 Clicking mouse-2 while selecting copies selected text to the kill-ring.
463 Clicking mouse-1 or mouse-3 kills the selected text.
464
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)))))
473
474 (defun mouse-select-secondary (event)
475 "Set secondary selection using the mouse.
476
477 Click sets the start of the secondary selection to click position.
478 Dragging extends the secondary selection.
479
480 Multi-clicking selects word/lines/paragraphs, as determined by
481 'mouse-sel-determine-selection-thing.
482
483 Clicking mouse-2 while selecting copies selected text to the kill-ring.
484 Clicking mouse-1 or mouse-3 kills the selected text.
485
486 This should be bound to a down-mouse event."
487 (interactive "e")
488 (mouse-select-internal 'SECONDARY event))
489
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.
495
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))))
514
515 ;;=== Extend ==============================================================
516
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))))
528
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)))
534
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.
541
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
547
548 (let* ((thing-symbol (mouse-sel-selection-thing selection))
549 (overlay (mouse-sel-selection-overlay selection))
550 (orig-window (selected-window))
551 (orig-window-frame (window-frame orig-window))
552 (top (nth 1 (window-edges orig-window)))
553 (bottom (nth 3 (window-edges orig-window)))
554 (mark-active nil) ; inhibit normal region highlight
555 (echo-keystrokes 0) ; don't echo mouse events
556 min max
557 direction
558 event)
559
560 ;; Get current bounds of overlay
561 (if (eq (overlay-buffer overlay) (current-buffer))
562 (setq min (overlay-start overlay)
563 max (overlay-end overlay))
564 (setq min (point)
565 max min)
566 (set thing-symbol nil))
567
568
569 ;; Bar cursor
570 (if (fboundp 'modify-frame-parameters)
571 (modify-frame-parameters (selected-frame)
572 '((cursor-type . bar))))
573
574 ;; Handle dragging
575 (track-mouse
576
577 (while (if (and initial-event (not no-process))
578 ;; Use initial event
579 (prog1
580 (setq event initial-event)
581 (setq initial-event nil))
582 (setq event (read-event))
583 (and (consp event)
584 (memq (car event) '(mouse-movement switch-frame))))
585
586 (let ((selection-thing (symbol-value thing-symbol))
587 (end (event-end event)))
588
589 (cond
590
591 ;; Ignore any movement outside the frame
592 ((eq (car-safe event) 'switch-frame) nil)
593 ((and (posn-window end)
594 (not (eq (let ((posn-w (posn-window end)))
595 (if (windowp posn-w)
596 (window-frame posn-w)
597 posn-w))
598 (window-frame orig-window)))) nil)
599
600 ;; Different window, same frame
601 ((not (eq (posn-window end) orig-window))
602 (let ((end-row (cdr (cdr (mouse-position)))))
603 (cond
604 ((and end-row (not (bobp)) (< end-row top))
605 (mouse-scroll-subr orig-window (- end-row top)
606 overlay max))
607 ((and end-row (not (eobp)) (>= end-row bottom))
608 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
609 overlay min))
610 )))
611
612 ;; On the mode line
613 ((eq (posn-point end) 'mode-line)
614 (mouse-scroll-subr orig-window 1 overlay min))
615
616 ;; In original window
617 (t (goto-char (posn-point end)))
618
619 )
620
621 ;; Determine direction of drag
622 (cond
623 ((and (not direction) (not (eq min max)))
624 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
625 ((and (not (eq direction -1)) (<= (point) min))
626 (setq direction -1))
627 ((and (not (eq direction 1)) (>= (point) max))
628 (setq direction 1)))
629
630 (if (not selection-thing) nil
631
632 ;; If dragging forward, goal is next character
633 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
634
635 ;; Move to start/end of selected thing
636 (let ((goal (point)))
637 (goto-char (if (eq 1 direction) min max))
638 (condition-case nil
639 (progn
640 (while (> (* direction (- goal (point))) 0)
641 (forward-thing selection-thing direction))
642 (let ((end (point)))
643 (forward-thing selection-thing (- direction))
644 (goto-char
645 (if (> (* direction (- goal (point))) 0)
646 end (point)))))
647 (error))))
648
649 ;; Move overlay
650 (move-overlay overlay
651 (if (eq 1 direction) min (point))
652 (if (eq -1 direction) max (point))
653 (current-buffer))
654
655 ))) ; end track-mouse
656
657 ;; Detect follow-link events
658 (when (mouse-sel-follow-link-p initial-event event)
659 (throw 'follow-link event))
660
661 ;; Finish up after dragging
662 (let ((overlay-start (overlay-start overlay))
663 (overlay-end (overlay-end overlay)))
664
665 ;; Set selection
666 (if (not (eq overlay-start overlay-end))
667 (mouse-sel-set-selection
668 selection
669 (buffer-substring overlay-start overlay-end)))
670
671 ;; Handle copy/kill
672 (let (this-command)
673 (cond
674 ((eq (event-basic-type last-input-event) 'mouse-2)
675 (copy-region-as-kill overlay-start overlay-end)
676 (read-event) (read-event))
677 ((and (memq (event-basic-type last-input-event)
678 '(mouse-1 mouse-3))
679 (memq 'down (event-modifiers last-input-event)))
680 (kill-region overlay-start overlay-end)
681 (move-overlay overlay overlay-start overlay-start)
682 (read-event) (read-event))
683 ((and (eq (event-basic-type last-input-event) 'mouse-3)
684 (memq 'double (event-modifiers last-input-event)))
685 (kill-region overlay-start overlay-end)
686 (move-overlay overlay overlay-start overlay-start)))))
687
688 direction)
689
690 ;; Restore cursor
691 (if (fboundp 'modify-frame-parameters)
692 (modify-frame-parameters
693 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
694
695 ))))
696
697 (defun mouse-sel-follow-link-p (initial final)
698 "Return t if we should follow a link, given INITIAL and FINAL mouse events.
699 See `mouse-1-click-follows-link' for details. Currently, Mouse
700 Sel mode does not support using a `double' value to follow links
701 using double-clicks."
702 (and initial final mouse-1-click-follows-link
703 (eq (car initial) 'down-mouse-1)
704 (mouse-on-link-p (event-start initial))
705 (= (posn-point (event-start initial))
706 (posn-point (event-end final)))
707 (= (event-click-count initial) 1)
708 (or (not (integerp mouse-1-click-follows-link))
709 (let ((t0 (posn-timestamp (event-start initial)))
710 (t1 (posn-timestamp (event-end final))))
711 (and (integerp t0) (integerp t1)
712 (if (> mouse-1-click-follows-link 0)
713 (<= (- t1 t0) mouse-1-click-follows-link)
714 (< (- t0 t1) mouse-1-click-follows-link)))))))
715
716 ;;=== Paste ===============================================================
717
718 (defun mouse-insert-selection (event arg)
719 "Insert the contents of the PRIMARY selection at mouse click.
720 If `mouse-yank-at-point' is non-nil, insert at point instead."
721 (interactive "e\nP")
722 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
723 (mouse-yank-at-click event arg)
724 (mouse-insert-selection-internal 'PRIMARY event)))
725
726 (defun mouse-insert-secondary (event)
727 "Insert the contents of the SECONDARY selection at mouse click.
728 If `mouse-yank-at-point' is non-nil, insert at point instead."
729 (interactive "e")
730 (mouse-insert-selection-internal 'SECONDARY event))
731
732 (defun mouse-insert-selection-internal (selection event)
733 "Insert the contents of the named SELECTION at mouse click.
734 If `mouse-yank-at-point' is non-nil, insert at point instead."
735 (unless mouse-yank-at-point
736 (mouse-set-point event))
737 (when mouse-sel-get-selection-function
738 (push-mark (point) 'nomsg)
739 (insert-for-yank
740 (or (funcall mouse-sel-get-selection-function selection) ""))))
741
742 ;;=== Handle loss of selections ===========================================
743
744 (defun mouse-sel-lost-selection-hook (selection)
745 "Remove the overlay for a lost selection."
746 (let ((overlay (mouse-sel-selection-overlay selection)))
747 (delete-overlay overlay)))
748
749 (provide 'mouse-sel)
750
751 ;;; mouse-sel.el ends here