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