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