*** empty log message ***
[bpt/emacs.git] / lisp / mouse-sel.el
... / ...
CommitLineData
1;;; mouse-sel.el --- multi-click selection support for Emacs 19
2
3;; Copyright (C) 1993,1994,1995,2001,2002 Free Software Foundation, Inc.
4
5;; Author: Mike Williams <mdub@bigfoot.com>
6;; Keywords: mouse
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 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
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
25;;; Commentary:
26
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.
41;; Quad-clicking selects paragraphs.
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;;
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).
50;;
51;; * Pressing mouse-2 while selecting or extending copies selection
52;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
53;;
54;; * Double-clicking mouse-3 also kills selection.
55;;
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.
59;;
60;; This module requires my thingatpt.el module, which it uses to find the
61;; bounds of words, lines, sexps, etc.
62;;
63;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
64;;
65;;--- Customisation -------------------------------------------------------
66;;
67;; * You may want to use none or more of following:
68;;
69;; ;; Enable region highlight
70;; (transient-mark-mode 1)
71;;
72;; ;; But only in the selected window
73;; (setq highlight-nonselected-windows nil)
74;;
75;; ;; Enable pending-delete
76;; (delete-selection-mode 1)
77;;
78;; * You can control the way mouse-sel binds its keys by setting the value
79;; of mouse-sel-default-bindings before loading mouse-sel.
80;;
81;; (a) If mouse-sel-default-bindings = t (the default)
82;;
83;; Mouse sets and insert selection
84;; mouse-1 mouse-select
85;; mouse-2 mouse-insert-selection
86;; mouse-3 mouse-extend
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
93;;
94;; Mouse sets selection, and pastes from kill-ring
95;; mouse-1 mouse-select
96;; mouse-2 mouse-insert-selection
97;; mouse-3 mouse-extend
98;; In this mode, mouse-insert-selection just calls mouse-yank-at-click.
99;;
100;; Selection/kill-ring interaction is retained
101;; interprogram-cut-function = x-select-text
102;; interprogram-paste-function = x-cut-buffer-or-selection-value
103;;
104;; What you lose is the ability to select some text in
105;; delete-selection-mode and yank over the top of it.
106;;
107;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
108;;
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;;
112;; (setq mouse-yank-at-point t)
113;;
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;;
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.
124;; Disable this behaviour with:
125;;
126;; (setq mouse-sel-cycle-clicks nil)
127;;
128;; * The variables mouse-sel-{set,get}-selection-function control how the
129;; selection is handled. Under X Windows, these variables default so
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;;
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)
138
139;;; Code:
140
141(require 'mouse)
142(require 'thingatpt)
143
144(eval-when-compile
145 (require 'cl))
146
147;;=== User Variables ======================================================
148
149(defgroup mouse-sel nil
150 "Mouse selection enhancement."
151 :group 'mouse)
152
153(defcustom mouse-sel-leave-point-near-mouse t
154 "*Leave point near last mouse position.
155If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
156of the region nearest to where the mouse last was.
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)
169 (const :tag "cut and paste" interprogram-cut-paste)
170 (other :tag "default bindings" t))
171 :group 'mouse-sel)
172
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
197;;=== User Command ========================================================
198
199(defvar mouse-sel-original-bindings nil)
200(defvar mouse-sel-original-interprogram-cut-function nil)
201(defvar mouse-sel-original-interprogram-paste-function nil)
202
203;;;###autoload
204(define-minor-mode mouse-sel-mode
205 "Toggle Mouse Sel mode.
206With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive.
207Returns the new status of Mouse Sel mode (non-nil means on).
208
209When Mouse Sel mode is enabled, mouse selection is enhanced in various ways:
210
211- Clicking mouse-1 starts (cancels) selection, dragging extends it.
212
213- Clicking or dragging mouse-3 extends the selection as well.
214
215- Double-clicking on word constituents selects words.
216Double-clicking on symbol constituents selects symbols.
217Double-clicking on quotes or parentheses selects sexps.
218Double-clicking on whitespace selects whitespace.
219Triple-clicking selects lines.
220Quad-clicking selects paragraphs.
221
222- Selecting sets the region & X primary selection, but does NOT affect
223the `kill-ring', nor do the kill-ring functions change the X selection.
224Because the mouse handlers set the primary selection directly,
225mouse-sel sets the variables `interprogram-cut-function' and
226`interprogram-paste-function' to nil.
227
228- Clicking mouse-2 inserts the contents of the primary selection at
229the mouse position (or point, if `mouse-yank-at-point' is non-nil).
230
231- Pressing mouse-2 while selecting or extending copies selection
232to the kill ring. Pressing mouse-1 or mouse-3 kills it.
233
234- Double-clicking mouse-3 also kills selection.
235
236- M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
237& mouse-3, but operate on the X secondary selection rather than the
238primary selection and region."
239 :global t
240 (if mouse-sel-mode
241 (progn
242 (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
243 (when mouse-sel-default-bindings
244 ;; Save original bindings and replace them with new ones.
245 (setq mouse-sel-original-bindings
246 (mapcar (lambda (binding)
247 (let ((event (car binding)))
248 (prog1 (cons event (lookup-key global-map event))
249 (global-set-key event (cdr binding)))))
250 mouse-sel-bound-events))
251 ;; Update interprogram functions.
252 (setq mouse-sel-original-interprogram-cut-function
253 interprogram-cut-function
254 mouse-sel-original-interprogram-paste-function
255 interprogram-paste-function)
256 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
257 (setq interprogram-cut-function nil
258 interprogram-paste-function nil))))
259
260 ;; Restore original bindings
261 (remove-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
262 (dolist (binding mouse-sel-original-bindings)
263 (global-set-key (car binding) (cdr binding)))
264 (setq interprogram-cut-function
265 mouse-sel-original-interprogram-cut-function
266 interprogram-paste-function
267 mouse-sel-original-interprogram-paste-function)))
268
269;;=== Internal Variables/Constants ========================================
270
271(defvar mouse-sel-primary-thing nil
272 "Type of PRIMARY selection in current buffer.")
273(make-variable-buffer-local 'mouse-sel-primary-thing)
274
275(defvar mouse-sel-secondary-thing nil
276 "Type of SECONDARY selection in current buffer.")
277(make-variable-buffer-local 'mouse-sel-secondary-thing)
278
279;; Ensure that secondary overlay is defined
280(unless (overlayp mouse-secondary-overlay)
281 (setq mouse-secondary-overlay (make-overlay 1 1))
282 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
283
284(defconst mouse-sel-selection-alist
285 '((PRIMARY mouse-drag-overlay mouse-sel-primary-thing)
286 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
287 "Alist associating selections with variables.
288Each element is of the form:
289
290 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
291
292where SELECTION-NAME = name of selection
293 OVERLAY-SYMBOL = name of variable containing overlay to use
294 SELECTION-THING-SYMBOL = name of variable where the current selection
295 type for this selection should be stored.")
296
297(defvar mouse-sel-set-selection-function
298 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
299 'x-set-selection
300 (lambda (selection value)
301 (if (eq selection 'PRIMARY)
302 (x-select-text value)
303 (x-set-selection selection value))))
304 "Function to call to set selection.
305Called with two arguments:
306
307 SELECTION, the name of the selection concerned, and
308 VALUE, the text to store.
309
310This sets the selection as well as the cut buffer for the older applications,
311unless `mouse-sel-default-bindings' is `interprogram-cut-paste'.")
312
313(defvar mouse-sel-get-selection-function
314 (lambda (selection)
315 (if (eq selection 'PRIMARY)
316 (or (x-cut-buffer-or-selection-value) x-last-selected-text)
317 (x-get-selection selection)))
318 "Function to call to get the selection.
319Called with one argument:
320
321 SELECTION: the name of the selection concerned.")
322
323;;=== Support/access functions ============================================
324
325(defun mouse-sel-determine-selection-thing (nclicks)
326 "Determine what `thing' `mouse-sel' should operate on.
327The first argument is NCLICKS, is the number of consecutive
328mouse clicks at the same position.
329
330Double-clicking on word constituents selects words.
331Double-clicking on symbol constituents selects symbols.
332Double-clicking on quotes or parentheses selects sexps.
333Double-clicking on whitespace selects whitespace.
334Triple-clicking selects lines.
335Quad-clicking selects paragraphs.
336
337Feel free to re-define this function to support your own desired
338multi-click semantics."
339 (let* ((next-char (char-after (point)))
340 (char-syntax (if next-char (char-syntax next-char))))
341 (if mouse-sel-cycle-clicks
342 (setq nclicks (1+ (% (1- nclicks) 4))))
343 (cond
344 ((= nclicks 1) nil)
345 ((= nclicks 3) 'line)
346 ((>= nclicks 4) 'paragraph)
347 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
348 ((memq next-char '(? ?\t ?\n)) 'whitespace)
349 ((eq char-syntax ?_) 'symbol)
350 ((eq char-syntax ?w) 'word))))
351
352(defun mouse-sel-set-selection (selection value)
353 "Set the specified SELECTION to VALUE."
354 (if mouse-sel-set-selection-function
355 (funcall mouse-sel-set-selection-function selection value)
356 (put 'mouse-sel-internal-selection selection value)))
357
358(defun mouse-sel-get-selection (selection)
359 "Get the value of the specified SELECTION."
360 (if mouse-sel-get-selection-function
361 (funcall mouse-sel-get-selection-function selection)
362 (get 'mouse-sel-internal-selection selection)))
363
364(defun mouse-sel-selection-overlay (selection)
365 "Return overlay corresponding to SELECTION."
366 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
367 (or symbol (error "No overlay corresponding to %s selection" selection))
368 (symbol-value symbol)))
369
370(defun mouse-sel-selection-thing (selection)
371 "Return overlay corresponding to SELECTION."
372 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
373 (or symbol (error "No symbol corresponding to %s selection" selection))
374 symbol))
375
376(defun mouse-sel-region-to-primary (orig-window)
377 "Convert region to PRIMARY overlay and deactivate region.
378Argument ORIG-WINDOW specifies the window the cursor was in when the
379originating command was issued, and is used to determine whether the
380region was visible or not."
381 (if transient-mark-mode
382 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
383 (cond
384 ((and mark-active
385 (or highlight-nonselected-windows
386 (eq orig-window (selected-window))))
387 ;; Region was visible, so convert region to overlay
388 (move-overlay overlay (region-beginning) (region-end)
389 (current-buffer)))
390 ((eq orig-window (selected-window))
391 ;; Point was visible, so set overlay at point
392 (move-overlay overlay (point) (point) (current-buffer)))
393 (t
394 ;; Nothing was visible, so remove overlay
395 (delete-overlay overlay)))
396 (setq mark-active nil))))
397
398(defun mouse-sel-primary-to-region (&optional direction)
399 "Convert PRIMARY overlay to region.
400Optional argument DIRECTION specifies the mouse drag direction: a value of
4011 indicates that the mouse was dragged left-to-right, otherwise it was
402dragged right-to-left."
403 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
404 (start (overlay-start overlay))
405 (end (overlay-end overlay)))
406 (if (eq start end)
407 (progn
408 (if start (goto-char start))
409 (deactivate-mark))
410 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
411 (progn
412 (goto-char end)
413 (push-mark start 'nomsg 'active))
414 (goto-char start)
415 (push-mark end 'nomsg 'active)))
416 (if transient-mark-mode (delete-overlay overlay))))
417
418(defmacro mouse-sel-eval-at-event-end (event &rest forms)
419 "Evaluate forms at mouse position.
420Move to the end position of EVENT, execute FORMS, and restore original
421point and window."
422 `(let ((posn (event-end ,event)))
423 (if posn (mouse-minibuffer-check ,event))
424 (if (and posn (not (windowp (posn-window posn))))
425 (error "Cursor not in text area of window"))
426 (let (orig-window orig-point-marker)
427 (setq orig-window (selected-window))
428 (if posn (select-window (posn-window posn)))
429 (setq orig-point-marker (point-marker))
430 (if (and posn (numberp (posn-point posn)))
431 (goto-char (posn-point posn)))
432 (unwind-protect
433 (progn
434 ,@forms)
435 (goto-char (marker-position orig-point-marker))
436 (move-marker orig-point-marker nil)
437 (select-window orig-window)))))
438
439(put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
440
441;;=== Select ==============================================================
442
443(defun mouse-select (event)
444 "Set region/selection using the mouse.
445
446Click sets point & mark to click position.
447Dragging extends region/selection.
448
449Multi-clicking selects word/lines/paragraphs, as determined by
450'mouse-sel-determine-selection-thing.
451
452Clicking mouse-2 while selecting copies selected text to the kill-ring.
453Clicking mouse-1 or mouse-3 kills the selected text.
454
455This should be bound to a down-mouse event."
456 (interactive "@e")
457 (let (direction)
458 (unwind-protect
459 (setq direction (mouse-select-internal 'PRIMARY event))
460 (mouse-sel-primary-to-region direction))))
461
462(defun mouse-select-secondary (event)
463 "Set secondary selection using the mouse.
464
465Click sets the start of the secondary selection to click position.
466Dragging extends the secondary selection.
467
468Multi-clicking selects word/lines/paragraphs, as determined by
469'mouse-sel-determine-selection-thing.
470
471Clicking mouse-2 while selecting copies selected text to the kill-ring.
472Clicking mouse-1 or mouse-3 kills the selected text.
473
474This should be bound to a down-mouse event."
475 (interactive "e")
476 (mouse-select-internal 'SECONDARY event))
477
478(defun mouse-select-internal (selection event)
479 "Set SELECTION using the mouse."
480 (mouse-sel-eval-at-event-end event
481 (let ((thing-symbol (mouse-sel-selection-thing selection))
482 (overlay (mouse-sel-selection-overlay selection)))
483 (set thing-symbol
484 (mouse-sel-determine-selection-thing (event-click-count event)))
485 (let ((object-bounds (bounds-of-thing-at-point
486 (symbol-value thing-symbol))))
487 (if object-bounds
488 (progn
489 (move-overlay overlay
490 (car object-bounds) (cdr object-bounds)
491 (current-buffer)))
492 (move-overlay overlay (point) (point) (current-buffer)))))
493 (mouse-extend-internal selection)))
494
495;;=== Extend ==============================================================
496
497(defun mouse-extend (event)
498 "Extend region/selection using the mouse."
499 (interactive "e")
500 (let ((orig-window (selected-window))
501 direction)
502 (select-window (posn-window (event-end event)))
503 (unwind-protect
504 (progn
505 (mouse-sel-region-to-primary orig-window)
506 (setq direction (mouse-extend-internal 'PRIMARY event)))
507 (mouse-sel-primary-to-region direction))))
508
509(defun mouse-extend-secondary (event)
510 "Extend secondary selection using the mouse."
511 (interactive "e")
512 (save-window-excursion
513 (mouse-extend-internal 'SECONDARY event)))
514
515(defun mouse-extend-internal (selection &optional initial-event)
516 "Extend specified SELECTION using the mouse.
517Track mouse-motion events, adjusting the SELECTION appropriately.
518Optional argument INITIAL-EVENT specifies an initial down-mouse event to
519process.
520
521See documentation for mouse-select-internal for more details."
522 (mouse-sel-eval-at-event-end initial-event
523 (let ((orig-cursor-type
524 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
525 (unwind-protect
526
527 (let* ((thing-symbol (mouse-sel-selection-thing selection))
528 (overlay (mouse-sel-selection-overlay selection))
529 (orig-window (selected-window))
530 (orig-window-frame (window-frame orig-window))
531 (top (nth 1 (window-edges orig-window)))
532 (bottom (nth 3 (window-edges orig-window)))
533 (mark-active nil) ; inhibit normal region highlight
534 (echo-keystrokes 0) ; don't echo mouse events
535 min max
536 direction
537 event)
538
539 ;; Get current bounds of overlay
540 (if (eq (overlay-buffer overlay) (current-buffer))
541 (setq min (overlay-start overlay)
542 max (overlay-end overlay))
543 (setq min (point)
544 max min)
545 (set thing-symbol nil))
546
547
548 ;; Bar cursor
549 (if (fboundp 'modify-frame-parameters)
550 (modify-frame-parameters (selected-frame)
551 '((cursor-type . bar))))
552
553 ;; Handle dragging
554 (track-mouse
555
556 (while (if initial-event ; Use initial event
557 (prog1
558 (setq event initial-event)
559 (setq initial-event nil))
560 (setq event (read-event))
561 (and (consp event)
562 (memq (car event) '(mouse-movement switch-frame))))
563
564 (let ((selection-thing (symbol-value thing-symbol))
565 (end (event-end event)))
566
567 (cond
568
569 ;; Ignore any movement outside the frame
570 ((eq (car-safe event) 'switch-frame) nil)
571 ((and (posn-window end)
572 (not (eq (let ((posn-w (posn-window end)))
573 (if (windowp posn-w)
574 (window-frame posn-w)
575 posn-w))
576 (window-frame orig-window)))) nil)
577
578 ;; Different window, same frame
579 ((not (eq (posn-window end) orig-window))
580 (let ((end-row (cdr (cdr (mouse-position)))))
581 (cond
582 ((and end-row (not (bobp)) (< end-row top))
583 (mouse-scroll-subr orig-window (- end-row top)
584 overlay max))
585 ((and end-row (not (eobp)) (>= end-row bottom))
586 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
587 overlay min))
588 )))
589
590 ;; On the mode line
591 ((eq (posn-point end) 'mode-line)
592 (mouse-scroll-subr orig-window 1 overlay min))
593
594 ;; In original window
595 (t (goto-char (posn-point end)))
596
597 )
598
599 ;; Determine direction of drag
600 (cond
601 ((and (not direction) (not (eq min max)))
602 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
603 ((and (not (eq direction -1)) (<= (point) min))
604 (setq direction -1))
605 ((and (not (eq direction 1)) (>= (point) max))
606 (setq direction 1)))
607
608 (if (not selection-thing) nil
609
610 ;; If dragging forward, goal is next character
611 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
612
613 ;; Move to start/end of selected thing
614 (let ((goal (point)))
615 (goto-char (if (eq 1 direction) min max))
616 (condition-case nil
617 (progn
618 (while (> (* direction (- goal (point))) 0)
619 (forward-thing selection-thing direction))
620 (let ((end (point)))
621 (forward-thing selection-thing (- direction))
622 (goto-char
623 (if (> (* direction (- goal (point))) 0)
624 end (point)))))
625 (error))))
626
627 ;; Move overlay
628 (move-overlay overlay
629 (if (eq 1 direction) min (point))
630 (if (eq -1 direction) max (point))
631 (current-buffer))
632
633 ))) ; end track-mouse
634
635 ;; Finish up after dragging
636 (let ((overlay-start (overlay-start overlay))
637 (overlay-end (overlay-end overlay)))
638
639 ;; Set selection
640 (if (not (eq overlay-start overlay-end))
641 (mouse-sel-set-selection
642 selection
643 (buffer-substring overlay-start overlay-end)))
644
645 ;; Handle copy/kill
646 (let (this-command)
647 (cond
648 ((eq (event-basic-type last-input-event) 'mouse-2)
649 (copy-region-as-kill overlay-start overlay-end)
650 (read-event) (read-event))
651 ((and (memq (event-basic-type last-input-event)
652 '(mouse-1 mouse-3))
653 (memq 'down (event-modifiers last-input-event)))
654 (kill-region overlay-start overlay-end)
655 (move-overlay overlay overlay-start overlay-start)
656 (read-event) (read-event))
657 ((and (eq (event-basic-type last-input-event) 'mouse-3)
658 (memq 'double (event-modifiers last-input-event)))
659 (kill-region overlay-start overlay-end)
660 (move-overlay overlay overlay-start overlay-start)))))
661
662 direction)
663
664 ;; Restore cursor
665 (if (fboundp 'modify-frame-parameters)
666 (modify-frame-parameters
667 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
668
669 ))))
670
671;;=== Paste ===============================================================
672
673(defun mouse-insert-selection (event arg)
674 "Insert the contents of the PRIMARY selection at mouse click.
675If `mouse-yank-at-point' is non-nil, insert at point instead."
676 (interactive "e\nP")
677 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
678 (mouse-yank-at-click event arg)
679 (mouse-insert-selection-internal 'PRIMARY event)))
680
681(defun mouse-insert-secondary (event)
682 "Insert the contents of the SECONDARY selection at mouse click.
683If `mouse-yank-at-point' is non-nil, insert at point instead."
684 (interactive "e")
685 (mouse-insert-selection-internal 'SECONDARY event))
686
687(defun mouse-insert-selection-internal (selection event)
688 "Insert the contents of the named SELECTION at mouse click.
689If `mouse-yank-at-point' is non-nil, insert at point instead."
690 (unless mouse-yank-at-point
691 (mouse-set-point event))
692 (when mouse-sel-get-selection-function
693 (push-mark (point) 'nomsg)
694 (insert (or (funcall mouse-sel-get-selection-function selection) ""))))
695
696;;=== Handle loss of selections ===========================================
697
698(defun mouse-sel-lost-selection-hook (selection)
699 "Remove the overlay for a lost selection."
700 (let ((overlay (mouse-sel-selection-overlay selection)))
701 (delete-overlay overlay)))
702
703(provide 'mouse-sel)
704
705;;; mouse-sel.el ends here