Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / mouse-sel.el
CommitLineData
e42ebbf2 1;;; mouse-sel.el --- multi-click selection support
1a2b6c52 2
acaf905b 3;; Copyright (C) 1993-1995, 2001-2012 Free Software Foundation, Inc.
1a2b6c52 4
9ae4c925 5;; Author: Mike Williams <mdub@bigfoot.com>
1a2b6c52 6;; Keywords: mouse
1a2b6c52
RS
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
1a2b6c52 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
1a2b6c52
RS
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
b578f267 20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b578f267 22
07b3798c 23;;; Commentary:
b578f267 24
1a2b6c52
RS
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.
f2506826 39;; Quad-clicking selects paragraphs.
1a2b6c52
RS
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;;
f2506826
RS
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).
1a2b6c52 48;;
6a2e3631 49;; * Pressing mouse-2 while selecting or extending copies selection
1a2b6c52 50;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
8a946354 51;;
6a2e3631 52;; * Double-clicking mouse-3 also kills selection.
8a946354 53;;
f2506826
RS
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.
1a2b6c52 57;;
6a2e3631
RS
58;; This module requires my thingatpt.el module, which it uses to find the
59;; bounds of words, lines, sexps, etc.
1a2b6c52
RS
60;;
61;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
62;;
e1dbe924 63;;--- Customization -------------------------------------------------------
6a2e3631
RS
64;;
65;; * You may want to use none or more of following:
1a2b6c52
RS
66;;
67;; ;; Enable region highlight
68;; (transient-mark-mode 1)
69;;
70;; ;; But only in the selected window
71;; (setq highlight-nonselected-windows nil)
8a946354 72;;
1a2b6c52
RS
73;; ;; Enable pending-delete
74;; (delete-selection-mode 1)
75;;
f77e1e4b 76;; * You can control the way mouse-sel binds its keys by setting the value
1a2b6c52
RS
77;; of mouse-sel-default-bindings before loading mouse-sel.
78;;
79;; (a) If mouse-sel-default-bindings = t (the default)
8a946354 80;;
f2506826 81;; Mouse sets and insert selection
1a2b6c52
RS
82;; mouse-1 mouse-select
83;; mouse-2 mouse-insert-selection
f2506826 84;; mouse-3 mouse-extend
1a2b6c52
RS
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
8a946354 91;;
1a2b6c52 92;; Mouse sets selection, and pastes from kill-ring
f2506826 93;; mouse-1 mouse-select
640201f8 94;; mouse-2 mouse-insert-selection
f2506826 95;; mouse-3 mouse-extend
640201f8 96;; In this mode, mouse-insert-selection just calls mouse-yank-at-click.
8a946354 97;;
1a2b6c52
RS
98;; Selection/kill-ring interaction is retained
99;; interprogram-cut-function = x-select-text
6d7cc563 100;; interprogram-paste-function = x-selection-value
8a946354 101;;
1a2b6c52
RS
102;; What you lose is the ability to select some text in
103;; delete-selection-mode and yank over the top of it.
8a946354 104;;
1a2b6c52
RS
105;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
106;;
cd1f32a6
RS
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;;
6b4dd332 110;; (setq mouse-yank-at-point t)
cd1f32a6 111;;
1a2b6c52
RS
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
fffa137c 115;; disable this behavior with:
1a2b6c52
RS
116;;
117;; (setq mouse-sel-leave-point-near-mouse nil)
118;;
f2506826
RS
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.
fffa137c 122;; Disable this behavior with:
1a2b6c52
RS
123;;
124;; (setq mouse-sel-cycle-clicks nil)
125;;
f2506826
RS
126;; * The variables mouse-sel-{set,get}-selection-function control how the
127;; selection is handled. Under X Windows, these variables default so
1a2b6c52
RS
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.
1a2b6c52 131
b578f267 132;;; Code:
1a2b6c52 133
1a2b6c52
RS
134(require 'mouse)
135(require 'thingatpt)
1a2b6c52 136
ad3f1e65
SM
137(eval-when-compile
138 (require 'cl))
139
1a2b6c52
RS
140;;=== User Variables ======================================================
141
ad3f1e65
SM
142(defgroup mouse-sel nil
143 "Mouse selection enhancement."
144 :group 'mouse)
145
ad3f1e65 146(defcustom mouse-sel-leave-point-near-mouse t
9201cc28 147 "Leave point near last mouse position.
f2506826 148If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
1a2b6c52 149of the region nearest to where the mouse last was.
ad3f1e65
SM
150If 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
9201cc28 155 "If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks."
ad3f1e65
SM
156 :type 'boolean
157 :group 'mouse-sel)
158
159(defcustom mouse-sel-default-bindings t
9201cc28 160 "Control mouse bindings."
ad3f1e65 161 :type '(choice (const :tag "none" nil)
21cabb0f
AS
162 (const :tag "cut and paste" interprogram-cut-paste)
163 (other :tag "default bindings" t))
ad3f1e65
SM
164 :group 'mouse-sel)
165
640201f8
SM
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
ad3f1e65
SM
190;;=== User Command ========================================================
191
387f00e6
RS
192(defvar mouse-sel-has-been-enabled nil
193 "Non-nil if Mouse Sel mode has been enabled at least once.")
194
640201f8
SM
195(defvar mouse-sel-original-bindings nil)
196(defvar mouse-sel-original-interprogram-cut-function nil)
caebc99b 197(defvar mouse-sel-original-interprogram-paste-function nil)
640201f8 198
ad3f1e65 199;;;###autoload
640201f8 200(define-minor-mode mouse-sel-mode
ad3f1e65 201 "Toggle Mouse Sel mode.
06e21633
CY
202With a prefix argument ARG, enable Mouse Sel mode if ARG is
203positive, and disable it otherwise. If called from Lisp, enable
204the mode if ARG is omitted or nil.
ad3f1e65 205
06e21633
CY
206Mouse Sel mode is a global minor mode. When enabled, mouse
207selection is enhanced in various ways:
ad3f1e65 208
7a6bda45 209- Double-clicking on symbol constituents selects symbols.
ad3f1e65
SM
210Double-clicking on quotes or parentheses selects sexps.
211Double-clicking on whitespace selects whitespace.
212Triple-clicking selects lines.
213Quad-clicking selects paragraphs.
214
215- Selecting sets the region & X primary selection, but does NOT affect
640201f8 216the `kill-ring', nor do the kill-ring functions change the X selection.
9eebe311 217Because the mouse handlers set the primary selection directly,
640201f8
SM
218mouse-sel sets the variables `interprogram-cut-function' and
219`interprogram-paste-function' to nil.
1a2b6c52 220
ad3f1e65 221- Clicking mouse-2 inserts the contents of the primary selection at
f7542b92 222the mouse position (or point, if `mouse-yank-at-point' is non-nil).
1a2b6c52 223
7a6bda45
CY
224- mouse-2 while selecting or extending copies selection to the
225kill ring; mouse-1 or mouse-3 kills it."
640201f8 226 :global t
3acd2c4f 227 :group 'mouse-sel
640201f8
SM
228 (if mouse-sel-mode
229 (progn
92280c32
CY
230 ;; If mouse-2 has never been done by the user, initialize the
231 ;; `event-kind' property to ensure that `follow-link' clicks
232 ;; are interpreted correctly.
233 (put 'mouse-2 'event-kind 'mouse-click)
a122087b 234 (add-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
640201f8
SM
235 (when mouse-sel-default-bindings
236 ;; Save original bindings and replace them with new ones.
237 (setq mouse-sel-original-bindings
238 (mapcar (lambda (binding)
239 (let ((event (car binding)))
240 (prog1 (cons event (lookup-key global-map event))
241 (global-set-key event (cdr binding)))))
242 mouse-sel-bound-events))
243 ;; Update interprogram functions.
244 (setq mouse-sel-original-interprogram-cut-function
245 interprogram-cut-function
246 mouse-sel-original-interprogram-paste-function
387f00e6
RS
247 interprogram-paste-function
248 mouse-sel-has-been-enabled t)
640201f8
SM
249 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
250 (setq interprogram-cut-function nil
251 interprogram-paste-function nil))))
ad3f1e65 252
4bc65152 253 ;; Restore original bindings
a122087b 254 (remove-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
640201f8
SM
255 (dolist (binding mouse-sel-original-bindings)
256 (global-set-key (car binding) (cdr binding)))
01fce6ab
RS
257 ;; Restore the old values of these variables,
258 ;; only if they were actually saved previously.
387f00e6
RS
259 (if mouse-sel-has-been-enabled
260 (setq interprogram-cut-function
261 mouse-sel-original-interprogram-cut-function
262 interprogram-paste-function
263 mouse-sel-original-interprogram-paste-function))))
1a2b6c52 264
f2506826
RS
265;;=== Internal Variables/Constants ========================================
266
8a946354 267(defvar mouse-sel-primary-thing nil
f2506826
RS
268 "Type of PRIMARY selection in current buffer.")
269(make-variable-buffer-local 'mouse-sel-primary-thing)
270
8a946354 271(defvar mouse-sel-secondary-thing nil
f2506826
RS
272 "Type of SECONDARY selection in current buffer.")
273(make-variable-buffer-local 'mouse-sel-secondary-thing)
274
275;; Ensure that secondary overlay is defined
ad3f1e65 276(unless (overlayp mouse-secondary-overlay)
f2506826
RS
277 (setq mouse-secondary-overlay (make-overlay 1 1))
278 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
1a2b6c52 279
7a6bda45
CY
280(defconst mouse-sel-primary-overlay
281 (let ((ol (make-overlay (point-min) (point-min))))
282 (delete-overlay ol)
460c0fba 283 (overlay-put ol 'face 'region)
7a6bda45
CY
284 ol)
285 "An overlay which records the current primary selection.
286This is used by Mouse Sel mode only.")
287
f2506826 288(defconst mouse-sel-selection-alist
7a6bda45
CY
289 '((PRIMARY mouse-sel-primary-overlay mouse-sel-primary-thing)
290 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
f7542b92
SM
291 "Alist associating selections with variables.
292Each element is of the form:
1a2b6c52 293
f2506826 294 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
1a2b6c52 295
f2506826
RS
296where 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.")
8a946354 300
c8ccffb1 301(declare-function x-select-text "term/common-win" (text))
aa360da1 302
ad3f1e65
SM
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))))
1a2b6c52 310 "Function to call to set selection.
f2506826
RS
311Called with two arguments:
312
313 SELECTION, the name of the selection concerned, and
0a6231f5 314 VALUE, the text to store.
ad3f1e65 315
c8ccffb1 316This sets the selection, unless `mouse-sel-default-bindings'
45240125 317is `interprogram-cut-paste'.")
1a2b6c52 318
6d7cc563 319(declare-function x-selection-value "term/x-win" ())
aa360da1 320
1a2b6c52 321(defvar mouse-sel-get-selection-function
ad3f1e65
SM
322 (lambda (selection)
323 (if (eq selection 'PRIMARY)
6d7cc563 324 (or (x-selection-value)
497cfacf
JPW
325 (bound-and-true-p x-last-selected-text)
326 (bound-and-true-p x-last-selected-text-primary))
ad3f1e65 327 (x-get-selection selection)))
1a2b6c52 328 "Function to call to get the selection.
f2506826 329Called with one argument:
1a2b6c52 330
f2506826
RS
331 SELECTION: the name of the selection concerned.")
332
f2506826
RS
333;;=== Support/access functions ============================================
334
335(defun mouse-sel-determine-selection-thing (nclicks)
336 "Determine what `thing' `mouse-sel' should operate on.
337The first argument is NCLICKS, is the number of consecutive
338mouse clicks at the same position.
339
340Double-clicking on word constituents selects words.
341Double-clicking on symbol constituents selects symbols.
342Double-clicking on quotes or parentheses selects sexps.
343Double-clicking on whitespace selects whitespace.
344Triple-clicking selects lines.
345Quad-clicking selects paragraphs.
346
347Feel free to re-define this function to support your own desired
348multi-click semantics."
1a2b6c52 349 (let* ((next-char (char-after (point)))
f2506826 350 (char-syntax (if next-char (char-syntax next-char))))
8a946354 351 (if mouse-sel-cycle-clicks
f2506826 352 (setq nclicks (1+ (% (1- nclicks) 4))))
1a2b6c52
RS
353 (cond
354 ((= nclicks 1) nil)
f2506826
RS
355 ((= nclicks 3) 'line)
356 ((>= nclicks 4) 'paragraph)
1a2b6c52 357 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
69aac59d 358 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
1a2b6c52
RS
359 ((eq char-syntax ?_) 'symbol)
360 ((eq char-syntax ?w) 'word))))
361
f2506826
RS
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
f2506826
RS
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.
8a946354
SS
388Argument ORIG-WINDOW specifies the window the cursor was in when the
389originating command was issued, and is used to determine whether the
f2506826
RS
390region was visible or not."
391 (if transient-mark-mode
392 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
393 (cond
8a946354
SS
394 ((and mark-active
395 (or highlight-nonselected-windows
f2506826
RS
396 (eq orig-window (selected-window))))
397 ;; Region was visible, so convert region to overlay
8a946354 398 (move-overlay overlay (region-beginning) (region-end)
f2506826
RS
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.
410Optional argument DIRECTION specifies the mouse drag direction: a value of
4111 indicates that the mouse was dragged left-to-right, otherwise it was
412dragged 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.
430Move to the end position of EVENT, execute FORMS, and restore original
431point and window."
8a946354
SS
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)))))
f2506826
RS
448
449(put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
450
451;;=== Select ==============================================================
452
453(defun mouse-select (event)
1a2b6c52
RS
454 "Set region/selection using the mouse.
455
f2506826 456Click sets point & mark to click position.
1a2b6c52
RS
457Dragging extends region/selection.
458
8a946354 459Multi-clicking selects word/lines/paragraphs, as determined by
f2506826 460'mouse-sel-determine-selection-thing.
1a2b6c52 461
f2506826
RS
462Clicking mouse-2 while selecting copies selected text to the kill-ring.
463Clicking mouse-1 or mouse-3 kills the selected text.
1a2b6c52
RS
464
465This should be bound to a down-mouse event."
f2506826 466 (interactive "@e")
3f00c08b 467 (let (select)
f2506826 468 (unwind-protect
3f00c08b
KS
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)))))
f2506826
RS
473
474(defun mouse-select-secondary (event)
ad3f1e65 475 "Set secondary selection using the mouse.
f2506826
RS
476
477Click sets the start of the secondary selection to click position.
478Dragging extends the secondary selection.
1a2b6c52 479
8a946354 480Multi-clicking selects word/lines/paragraphs, as determined by
f2506826 481'mouse-sel-determine-selection-thing.
1a2b6c52 482
f2506826
RS
483Clicking mouse-2 while selecting copies selected text to the kill-ring.
484Clicking mouse-1 or mouse-3 kills the selected text.
1a2b6c52
RS
485
486This should be bound to a down-mouse event."
ad3f1e65 487 (interactive "e")
f2506826
RS
488 (mouse-select-internal 'SECONDARY event))
489
490(defun mouse-select-internal (selection event)
3f00c08b
KS
491 "Set SELECTION using the mouse, with EVENT as the initial down-event.
492Normally, this returns the direction in which the selection was
493made: a value of 1 indicates that the mouse was dragged
494left-to-right, otherwise it was dragged right-to-left.
495
496However, if `mouse-1-click-follows-link' is non-nil and the
497subsequent mouse events specify following a link, this returns
498the final mouse-event. In that case, the selection is not set."
f2506826
RS
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)))))
3f00c08b
KS
512 (catch 'follow-link
513 (mouse-extend-internal selection event t))))
f2506826
RS
514
515;;=== Extend ==============================================================
516
517(defun mouse-extend (event)
518 "Extend region/selection using the mouse."
1a2b6c52 519 (interactive "e")
f2506826
RS
520 (let ((orig-window (selected-window))
521 direction)
522 (select-window (posn-window (event-end event)))
1a2b6c52 523 (unwind-protect
f2506826
RS
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
3f00c08b 535(defun mouse-extend-internal (selection &optional initial-event no-process)
f2506826
RS
536 "Extend specified SELECTION using the mouse.
537Track mouse-motion events, adjusting the SELECTION appropriately.
3f00c08b
KS
538Optional argument INITIAL-EVENT specifies an initial down-mouse event.
539Optional argument NO-PROCESS means not to process the initial
540event.
f2506826
RS
541
542See documentation for mouse-select-internal for more details."
543 (mouse-sel-eval-at-event-end initial-event
8a946354 544 (let ((orig-cursor-type
f2506826
RS
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))
f2506826
RS
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)
558
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))
8a946354 566
f2506826
RS
567
568 ;; Bar cursor
569 (if (fboundp 'modify-frame-parameters)
570 (modify-frame-parameters (selected-frame)
571 '((cursor-type . bar))))
8a946354 572
f2506826
RS
573 ;; Handle dragging
574 (track-mouse
8a946354 575
3f00c08b
KS
576 (while (if (and initial-event (not no-process))
577 ;; Use initial event
f2506826
RS
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))))
8a946354 584
f2506826
RS
585 (let ((selection-thing (symbol-value thing-symbol))
586 (end (event-end event)))
8a946354 587
f2506826 588 (cond
8a946354 589
f2506826
RS
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)
8a946354 598
f2506826
RS
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))
609 )))
8a946354 610
f2506826
RS
611 ;; On the mode line
612 ((eq (posn-point end) 'mode-line)
613 (mouse-scroll-subr orig-window 1 overlay min))
8a946354 614
f2506826
RS
615 ;; In original window
616 (t (goto-char (posn-point end)))
8a946354 617
f2506826 618 )
8a946354 619
f2506826
RS
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)))
8a946354 628
f2506826 629 (if (not selection-thing) nil
8a946354 630
f2506826
RS
631 ;; If dragging forward, goal is next character
632 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
8a946354 633
f2506826 634 ;; Move to start/end of selected thing
67384793 635 (let ((goal (point)))
f2506826
RS
636 (goto-char (if (eq 1 direction) min max))
637 (condition-case nil
638 (progn
639 (while (> (* direction (- goal (point))) 0)
f2506826
RS
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)
67384793 645 end (point)))))
f2506826 646 (error))))
8a946354 647
f2506826
RS
648 ;; Move overlay
649 (move-overlay overlay
650 (if (eq 1 direction) min (point))
651 (if (eq -1 direction) max (point))
652 (current-buffer))
8a946354 653
f2506826
RS
654 ))) ; end track-mouse
655
3f00c08b
KS
656 ;; Detect follow-link events
657 (when (mouse-sel-follow-link-p initial-event event)
658 (throw 'follow-link event))
659
f2506826
RS
660 ;; Finish up after dragging
661 (let ((overlay-start (overlay-start overlay))
662 (overlay-end (overlay-end overlay)))
8a946354 663
f2506826
RS
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)))
8a946354 669
f2506826
RS
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)))))
686
687 direction)
688
689 ;; Restore cursor
690 (if (fboundp 'modify-frame-parameters)
8a946354 691 (modify-frame-parameters
f2506826 692 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
8a946354 693
f2506826
RS
694 ))))
695
3f00c08b
KS
696(defun mouse-sel-follow-link-p (initial final)
697 "Return t if we should follow a link, given INITIAL and FINAL mouse events.
698See `mouse-1-click-follows-link' for details. Currently, Mouse
699Sel mode does not support using a `double' value to follow links
700using double-clicks."
701 (and initial final mouse-1-click-follows-link
702 (eq (car initial) 'down-mouse-1)
91a2acb2 703 (mouse-on-link-p (event-start initial))
3f00c08b
KS
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)))))))
714
f2506826
RS
715;;=== Paste ===============================================================
716
640201f8 717(defun mouse-insert-selection (event arg)
f2506826
RS
718 "Insert the contents of the PRIMARY selection at mouse click.
719If `mouse-yank-at-point' is non-nil, insert at point instead."
640201f8
SM
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)))
f2506826
RS
724
725(defun mouse-insert-secondary (event)
726 "Insert the contents of the SECONDARY selection at mouse click.
6b4dd332 727If `mouse-yank-at-point' is non-nil, insert at point instead."
1a2b6c52 728 (interactive "e")
f2506826
RS
729 (mouse-insert-selection-internal 'SECONDARY event))
730
731(defun mouse-insert-selection-internal (selection event)
732 "Insert the contents of the named SELECTION at mouse click.
733If `mouse-yank-at-point' is non-nil, insert at point instead."
8a946354 734 (unless mouse-yank-at-point
ad3f1e65
SM
735 (mouse-set-point event))
736 (when mouse-sel-get-selection-function
737 (push-mark (point) 'nomsg)
d3886822
EZ
738 (insert-for-yank
739 (or (funcall mouse-sel-get-selection-function selection) ""))))
f2506826 740
67384793
RS
741;;=== Handle loss of selections ===========================================
742
743(defun mouse-sel-lost-selection-hook (selection)
744 "Remove the overlay for a lost selection."
7b990caf 745 (let ((overlay (mouse-sel-selection-overlay selection)))
67384793
RS
746 (delete-overlay overlay)))
747
ad3f1e65 748(provide 'mouse-sel)
f2506826 749
e8af40ee 750;;; mouse-sel.el ends here