* alloc.c (garbage_collect): Don't assume stack size fits in int.
[bpt/emacs.git] / lisp / mouse-sel.el
CommitLineData
e42ebbf2 1;;; mouse-sel.el --- multi-click selection support
1a2b6c52 2
73b0cd50 3;; Copyright (C) 1993-1995, 2001-2011 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;;
6a2e3631
RS
63;;--- Customisation -------------------------------------------------------
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
SM
201 "Toggle Mouse Sel mode.
202With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive.
203Returns the new status of Mouse Sel mode (non-nil means on).
204
205When 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.
212Double-clicking on symbol constituents selects symbols.
213Double-clicking on quotes or parentheses selects sexps.
214Double-clicking on whitespace selects whitespace.
215Triple-clicking selects lines.
216Quad-clicking selects paragraphs.
217
218- Selecting sets the region & X primary selection, but does NOT affect
640201f8 219the `kill-ring', nor do the kill-ring functions change the X selection.
9eebe311 220Because the mouse handlers set the primary selection directly,
640201f8
SM
221mouse-sel sets the variables `interprogram-cut-function' and
222`interprogram-paste-function' to nil.
1a2b6c52 223
ad3f1e65 224- Clicking mouse-2 inserts the contents of the primary selection at
f7542b92 225the mouse position (or point, if `mouse-yank-at-point' is non-nil).
1a2b6c52 226
ad3f1e65
SM
227- Pressing mouse-2 while selecting or extending copies selection
228to 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
234primary selection and region."
640201f8 235 :global t
3acd2c4f 236 :group 'mouse-sel
640201f8
SM
237 (if mouse-sel-mode
238 (progn
92280c32
CY
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)
a122087b 243 (add-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
640201f8
SM
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
387f00e6
RS
256 interprogram-paste-function
257 mouse-sel-has-been-enabled t)
640201f8
SM
258 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
259 (setq interprogram-cut-function nil
260 interprogram-paste-function nil))))
ad3f1e65 261
4bc65152 262 ;; Restore original bindings
a122087b 263 (remove-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
640201f8
SM
264 (dolist (binding mouse-sel-original-bindings)
265 (global-set-key (car binding) (cdr binding)))
01fce6ab
RS
266 ;; Restore the old values of these variables,
267 ;; only if they were actually saved previously.
387f00e6
RS
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))))
1a2b6c52 273
f2506826
RS
274;;=== Internal Variables/Constants ========================================
275
8a946354 276(defvar mouse-sel-primary-thing nil
f2506826
RS
277 "Type of PRIMARY selection in current buffer.")
278(make-variable-buffer-local 'mouse-sel-primary-thing)
279
8a946354 280(defvar mouse-sel-secondary-thing nil
f2506826
RS
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
ad3f1e65 285(unless (overlayp mouse-secondary-overlay)
f2506826
RS
286 (setq mouse-secondary-overlay (make-overlay 1 1))
287 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
1a2b6c52 288
f2506826 289(defconst mouse-sel-selection-alist
5592c08f 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))
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))
8a946354 567
f2506826
RS
568
569 ;; Bar cursor
570 (if (fboundp 'modify-frame-parameters)
571 (modify-frame-parameters (selected-frame)
572 '((cursor-type . bar))))
8a946354 573
f2506826
RS
574 ;; Handle dragging
575 (track-mouse
8a946354 576
3f00c08b
KS
577 (while (if (and initial-event (not no-process))
578 ;; Use initial event
f2506826
RS
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))))
8a946354 585
f2506826
RS
586 (let ((selection-thing (symbol-value thing-symbol))
587 (end (event-end event)))
8a946354 588
f2506826 589 (cond
8a946354 590
f2506826
RS
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)
8a946354 599
f2506826
RS
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 )))
8a946354 611
f2506826
RS
612 ;; On the mode line
613 ((eq (posn-point end) 'mode-line)
614 (mouse-scroll-subr orig-window 1 overlay min))
8a946354 615
f2506826
RS
616 ;; In original window
617 (t (goto-char (posn-point end)))
8a946354 618
f2506826 619 )
8a946354 620
f2506826
RS
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)))
8a946354 629
f2506826 630 (if (not selection-thing) nil
8a946354 631
f2506826
RS
632 ;; If dragging forward, goal is next character
633 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
8a946354 634
f2506826 635 ;; Move to start/end of selected thing
67384793 636 (let ((goal (point)))
f2506826
RS
637 (goto-char (if (eq 1 direction) min max))
638 (condition-case nil
639 (progn
640 (while (> (* direction (- goal (point))) 0)
f2506826
RS
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)
67384793 646 end (point)))))
f2506826 647 (error))))
8a946354 648
f2506826
RS
649 ;; Move overlay
650 (move-overlay overlay
651 (if (eq 1 direction) min (point))
652 (if (eq -1 direction) max (point))
653 (current-buffer))
8a946354 654
f2506826
RS
655 ))) ; end track-mouse
656
3f00c08b
KS
657 ;; Detect follow-link events
658 (when (mouse-sel-follow-link-p initial-event event)
659 (throw 'follow-link event))
660
f2506826
RS
661 ;; Finish up after dragging
662 (let ((overlay-start (overlay-start overlay))
663 (overlay-end (overlay-end overlay)))
8a946354 664
f2506826
RS
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)))
8a946354 670
f2506826
RS
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)
8a946354 692 (modify-frame-parameters
f2506826 693 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
8a946354 694
f2506826
RS
695 ))))
696
3f00c08b
KS
697(defun mouse-sel-follow-link-p (initial final)
698 "Return t if we should follow a link, given INITIAL and FINAL mouse events.
699See `mouse-1-click-follows-link' for details. Currently, Mouse
700Sel mode does not support using a `double' value to follow links
701using double-clicks."
702 (and initial final mouse-1-click-follows-link
703 (eq (car initial) 'down-mouse-1)
91a2acb2 704 (mouse-on-link-p (event-start initial))
3f00c08b
KS
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
f2506826
RS
716;;=== Paste ===============================================================
717
640201f8 718(defun mouse-insert-selection (event arg)
f2506826
RS
719 "Insert the contents of the PRIMARY selection at mouse click.
720If `mouse-yank-at-point' is non-nil, insert at point instead."
640201f8
SM
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)))
f2506826
RS
725
726(defun mouse-insert-secondary (event)
727 "Insert the contents of the SECONDARY selection at mouse click.
6b4dd332 728If `mouse-yank-at-point' is non-nil, insert at point instead."
1a2b6c52 729 (interactive "e")
f2506826
RS
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.
734If `mouse-yank-at-point' is non-nil, insert at point instead."
8a946354 735 (unless mouse-yank-at-point
ad3f1e65
SM
736 (mouse-set-point event))
737 (when mouse-sel-get-selection-function
738 (push-mark (point) 'nomsg)
d3886822
EZ
739 (insert-for-yank
740 (or (funcall mouse-sel-get-selection-function selection) ""))))
f2506826 741
67384793
RS
742;;=== Handle loss of selections ===========================================
743
744(defun mouse-sel-lost-selection-hook (selection)
745 "Remove the overlay for a lost selection."
7b990caf 746 (let ((overlay (mouse-sel-selection-overlay selection)))
67384793
RS
747 (delete-overlay overlay)))
748
ad3f1e65 749(provide 'mouse-sel)
f2506826 750
e8af40ee 751;;; mouse-sel.el ends here