(posn-set-point): Fix typos: parameter is `position', not `posn'.
[emacs.git] / lisp / mouse-sel.el
blobf9b90fbfc6a815bcb1ebddf42575043ff571ee0a
1 ;;; mouse-sel.el --- multi-click selection support for Emacs 19
3 ;; Copyright (C) 1993,1994,1995,2001,2002 Free Software Foundation, Inc.
5 ;; Author: Mike Williams <mdub@bigfoot.com>
6 ;; Keywords: mouse
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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:
32 ;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
34 ;; * Clicking or dragging mouse-3 extends the selection as well.
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.
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.
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).
51 ;; * Pressing mouse-2 while selecting or extending copies selection
52 ;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
54 ;; * Double-clicking mouse-3 also kills selection.
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.
60 ;; This module requires my thingatpt.el module, which it uses to find the
61 ;; bounds of words, lines, sexps, etc.
63 ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
65 ;;--- Customisation -------------------------------------------------------
67 ;; * You may want to use none or more of following:
69 ;; ;; Enable region highlight
70 ;; (transient-mark-mode 1)
72 ;; ;; But only in the selected window
73 ;; (setq highlight-nonselected-windows nil)
75 ;; ;; Enable pending-delete
76 ;; (delete-selection-mode 1)
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.
81 ;; (a) If mouse-sel-default-bindings = t (the default)
83 ;; Mouse sets and insert selection
84 ;; mouse-1 mouse-select
85 ;; mouse-2 mouse-insert-selection
86 ;; mouse-3 mouse-extend
88 ;; Selection/kill-ring interaction is disabled
89 ;; interprogram-cut-function = nil
90 ;; interprogram-paste-function = nil
92 ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
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.
100 ;; Selection/kill-ring interaction is retained
101 ;; interprogram-cut-function = x-select-text
102 ;; interprogram-paste-function = x-cut-buffer-or-selection-value
104 ;; What you lose is the ability to select some text in
105 ;; delete-selection-mode and yank over the top of it.
107 ;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
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:
112 ;; (setq mouse-yank-at-point t)
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:
119 ;; (setq mouse-sel-leave-point-near-mouse nil)
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:
126 ;; (setq mouse-sel-cycle-clicks nil)
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.
134 ;; * You can change the selection highlight face by altering the properties
135 ;; of mouse-drag-overlay, eg.
137 ;; (overlay-put mouse-drag-overlay 'face 'bold)
139 ;;; Code:
141 (require 'mouse)
142 (require 'thingatpt)
144 (eval-when-compile
145 (require 'cl))
147 ;;=== User Variables ======================================================
149 (defgroup mouse-sel nil
150 "Mouse selection enhancement."
151 :group 'mouse)
153 (defcustom mouse-sel-leave-point-near-mouse t
154 "*Leave point near last mouse position.
155 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
156 of the region nearest to where the mouse last was.
157 If nil, point will always be placed at the beginning of the region."
158 :type 'boolean
159 :group 'mouse-sel)
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)
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)
173 ;;=== Key bindings ========================================================
175 (defconst mouse-sel-bound-events
176 '(;; Primary selection bindings.
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.")
197 ;;=== User Command ========================================================
199 (defvar mouse-sel-has-been-enabled nil
200 "Non-nil if Mouse Sel mode has been enabled at least once.")
202 (defvar mouse-sel-original-bindings nil)
203 (defvar mouse-sel-original-interprogram-cut-function nil)
204 (defvar mouse-sel-original-interprogram-paste-function nil)
206 ;;;###autoload
207 (define-minor-mode mouse-sel-mode
208 "Toggle Mouse Sel mode.
209 With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive.
210 Returns the new status of Mouse Sel mode (non-nil means on).
212 When Mouse Sel mode is enabled, mouse selection is enhanced in various ways:
214 - Clicking mouse-1 starts (cancels) selection, dragging extends it.
216 - Clicking or dragging mouse-3 extends the selection as well.
218 - Double-clicking on word constituents selects words.
219 Double-clicking on symbol constituents selects symbols.
220 Double-clicking on quotes or parentheses selects sexps.
221 Double-clicking on whitespace selects whitespace.
222 Triple-clicking selects lines.
223 Quad-clicking selects paragraphs.
225 - Selecting sets the region & X primary selection, but does NOT affect
226 the `kill-ring', nor do the kill-ring functions change the X selection.
227 Because the mouse handlers set the primary selection directly,
228 mouse-sel sets the variables `interprogram-cut-function' and
229 `interprogram-paste-function' to nil.
231 - Clicking mouse-2 inserts the contents of the primary selection at
232 the mouse position (or point, if `mouse-yank-at-point' is non-nil).
234 - Pressing mouse-2 while selecting or extending copies selection
235 to the kill ring. Pressing mouse-1 or mouse-3 kills it.
237 - Double-clicking mouse-3 also kills selection.
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
241 primary selection and region."
242 :global t
243 (if mouse-sel-mode
244 (progn
245 (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
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
258 interprogram-paste-function
259 mouse-sel-has-been-enabled t)
260 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
261 (setq interprogram-cut-function nil
262 interprogram-paste-function nil))))
264 ;; Restore original bindings
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)))
268 ;; Restore the old values of these variables,
269 ;; only if they were actually saved previously.
270 (if mouse-sel-has-been-enabled
271 (setq interprogram-cut-function
272 mouse-sel-original-interprogram-cut-function
273 interprogram-paste-function
274 mouse-sel-original-interprogram-paste-function))))
276 ;;=== Internal Variables/Constants ========================================
278 (defvar mouse-sel-primary-thing nil
279 "Type of PRIMARY selection in current buffer.")
280 (make-variable-buffer-local 'mouse-sel-primary-thing)
282 (defvar mouse-sel-secondary-thing nil
283 "Type of SECONDARY selection in current buffer.")
284 (make-variable-buffer-local 'mouse-sel-secondary-thing)
286 ;; Ensure that secondary overlay is defined
287 (unless (overlayp mouse-secondary-overlay)
288 (setq mouse-secondary-overlay (make-overlay 1 1))
289 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
291 (defconst mouse-sel-selection-alist
292 '((PRIMARY mouse-drag-overlay mouse-sel-primary-thing)
293 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
294 "Alist associating selections with variables.
295 Each element is of the form:
297 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
299 where SELECTION-NAME = name of selection
300 OVERLAY-SYMBOL = name of variable containing overlay to use
301 SELECTION-THING-SYMBOL = name of variable where the current selection
302 type for this selection should be stored.")
304 (defvar mouse-sel-set-selection-function
305 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
306 'x-set-selection
307 (lambda (selection value)
308 (if (eq selection 'PRIMARY)
309 (x-select-text value)
310 (x-set-selection selection value))))
311 "Function to call to set selection.
312 Called with two arguments:
314 SELECTION, the name of the selection concerned, and
315 VALUE, the text to store.
317 This sets the selection as well as the cut buffer for the older applications,
318 unless `mouse-sel-default-bindings' is `interprogram-cut-paste'.")
320 (defvar mouse-sel-get-selection-function
321 (lambda (selection)
322 (if (eq selection 'PRIMARY)
323 (or (x-cut-buffer-or-selection-value)
324 (bound-and-true-p x-last-selected-text)
325 (bound-and-true-p x-last-selected-text-primary))
326 (x-get-selection selection)))
327 "Function to call to get the selection.
328 Called with one argument:
330 SELECTION: the name of the selection concerned.")
332 ;;=== Support/access functions ============================================
334 (defun mouse-sel-determine-selection-thing (nclicks)
335 "Determine what `thing' `mouse-sel' should operate on.
336 The first argument is NCLICKS, is the number of consecutive
337 mouse clicks at the same position.
339 Double-clicking on word constituents selects words.
340 Double-clicking on symbol constituents selects symbols.
341 Double-clicking on quotes or parentheses selects sexps.
342 Double-clicking on whitespace selects whitespace.
343 Triple-clicking selects lines.
344 Quad-clicking selects paragraphs.
346 Feel free to re-define this function to support your own desired
347 multi-click semantics."
348 (let* ((next-char (char-after (point)))
349 (char-syntax (if next-char (char-syntax next-char))))
350 (if mouse-sel-cycle-clicks
351 (setq nclicks (1+ (% (1- nclicks) 4))))
352 (cond
353 ((= nclicks 1) nil)
354 ((= nclicks 3) 'line)
355 ((>= nclicks 4) 'paragraph)
356 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
357 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
358 ((eq char-syntax ?_) 'symbol)
359 ((eq char-syntax ?w) 'word))))
361 (defun mouse-sel-set-selection (selection value)
362 "Set the specified SELECTION to VALUE."
363 (if mouse-sel-set-selection-function
364 (funcall mouse-sel-set-selection-function selection value)
365 (put 'mouse-sel-internal-selection selection value)))
367 (defun mouse-sel-get-selection (selection)
368 "Get the value of the specified SELECTION."
369 (if mouse-sel-get-selection-function
370 (funcall mouse-sel-get-selection-function selection)
371 (get 'mouse-sel-internal-selection selection)))
373 (defun mouse-sel-selection-overlay (selection)
374 "Return overlay corresponding to SELECTION."
375 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
376 (or symbol (error "No overlay corresponding to %s selection" selection))
377 (symbol-value symbol)))
379 (defun mouse-sel-selection-thing (selection)
380 "Return overlay corresponding to SELECTION."
381 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
382 (or symbol (error "No symbol corresponding to %s selection" selection))
383 symbol))
385 (defun mouse-sel-region-to-primary (orig-window)
386 "Convert region to PRIMARY overlay and deactivate region.
387 Argument ORIG-WINDOW specifies the window the cursor was in when the
388 originating command was issued, and is used to determine whether the
389 region was visible or not."
390 (if transient-mark-mode
391 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
392 (cond
393 ((and mark-active
394 (or highlight-nonselected-windows
395 (eq orig-window (selected-window))))
396 ;; Region was visible, so convert region to overlay
397 (move-overlay overlay (region-beginning) (region-end)
398 (current-buffer)))
399 ((eq orig-window (selected-window))
400 ;; Point was visible, so set overlay at point
401 (move-overlay overlay (point) (point) (current-buffer)))
403 ;; Nothing was visible, so remove overlay
404 (delete-overlay overlay)))
405 (setq mark-active nil))))
407 (defun mouse-sel-primary-to-region (&optional direction)
408 "Convert PRIMARY overlay to region.
409 Optional argument DIRECTION specifies the mouse drag direction: a value of
410 1 indicates that the mouse was dragged left-to-right, otherwise it was
411 dragged right-to-left."
412 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
413 (start (overlay-start overlay))
414 (end (overlay-end overlay)))
415 (if (eq start end)
416 (progn
417 (if start (goto-char start))
418 (deactivate-mark))
419 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
420 (progn
421 (goto-char end)
422 (push-mark start 'nomsg 'active))
423 (goto-char start)
424 (push-mark end 'nomsg 'active)))
425 (if transient-mark-mode (delete-overlay overlay))))
427 (defmacro mouse-sel-eval-at-event-end (event &rest forms)
428 "Evaluate forms at mouse position.
429 Move to the end position of EVENT, execute FORMS, and restore original
430 point and window."
431 `(let ((posn (event-end ,event)))
432 (if posn (mouse-minibuffer-check ,event))
433 (if (and posn (not (windowp (posn-window posn))))
434 (error "Cursor not in text area of window"))
435 (let (orig-window orig-point-marker)
436 (setq orig-window (selected-window))
437 (if posn (select-window (posn-window posn)))
438 (setq orig-point-marker (point-marker))
439 (if (and posn (numberp (posn-point posn)))
440 (goto-char (posn-point posn)))
441 (unwind-protect
442 (progn
443 ,@forms)
444 (goto-char (marker-position orig-point-marker))
445 (move-marker orig-point-marker nil)
446 (select-window orig-window)))))
448 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
450 ;;=== Select ==============================================================
452 (defun mouse-select (event)
453 "Set region/selection using the mouse.
455 Click sets point & mark to click position.
456 Dragging extends region/selection.
458 Multi-clicking selects word/lines/paragraphs, as determined by
459 'mouse-sel-determine-selection-thing.
461 Clicking mouse-2 while selecting copies selected text to the kill-ring.
462 Clicking mouse-1 or mouse-3 kills the selected text.
464 This should be bound to a down-mouse event."
465 (interactive "@e")
466 (let (direction)
467 (unwind-protect
468 (setq direction (mouse-select-internal 'PRIMARY event))
469 (mouse-sel-primary-to-region direction))))
471 (defun mouse-select-secondary (event)
472 "Set secondary selection using the mouse.
474 Click sets the start of the secondary selection to click position.
475 Dragging extends the secondary selection.
477 Multi-clicking selects word/lines/paragraphs, as determined by
478 'mouse-sel-determine-selection-thing.
480 Clicking mouse-2 while selecting copies selected text to the kill-ring.
481 Clicking mouse-1 or mouse-3 kills the selected text.
483 This should be bound to a down-mouse event."
484 (interactive "e")
485 (mouse-select-internal 'SECONDARY event))
487 (defun mouse-select-internal (selection event)
488 "Set SELECTION using the mouse."
489 (mouse-sel-eval-at-event-end event
490 (let ((thing-symbol (mouse-sel-selection-thing selection))
491 (overlay (mouse-sel-selection-overlay selection)))
492 (set thing-symbol
493 (mouse-sel-determine-selection-thing (event-click-count event)))
494 (let ((object-bounds (bounds-of-thing-at-point
495 (symbol-value thing-symbol))))
496 (if object-bounds
497 (progn
498 (move-overlay overlay
499 (car object-bounds) (cdr object-bounds)
500 (current-buffer)))
501 (move-overlay overlay (point) (point) (current-buffer)))))
502 (mouse-extend-internal selection)))
504 ;;=== Extend ==============================================================
506 (defun mouse-extend (event)
507 "Extend region/selection using the mouse."
508 (interactive "e")
509 (let ((orig-window (selected-window))
510 direction)
511 (select-window (posn-window (event-end event)))
512 (unwind-protect
513 (progn
514 (mouse-sel-region-to-primary orig-window)
515 (setq direction (mouse-extend-internal 'PRIMARY event)))
516 (mouse-sel-primary-to-region direction))))
518 (defun mouse-extend-secondary (event)
519 "Extend secondary selection using the mouse."
520 (interactive "e")
521 (save-window-excursion
522 (mouse-extend-internal 'SECONDARY event)))
524 (defun mouse-extend-internal (selection &optional initial-event)
525 "Extend specified SELECTION using the mouse.
526 Track mouse-motion events, adjusting the SELECTION appropriately.
527 Optional argument INITIAL-EVENT specifies an initial down-mouse event to
528 process.
530 See documentation for mouse-select-internal for more details."
531 (mouse-sel-eval-at-event-end initial-event
532 (let ((orig-cursor-type
533 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
534 (unwind-protect
536 (let* ((thing-symbol (mouse-sel-selection-thing selection))
537 (overlay (mouse-sel-selection-overlay selection))
538 (orig-window (selected-window))
539 (orig-window-frame (window-frame orig-window))
540 (top (nth 1 (window-edges orig-window)))
541 (bottom (nth 3 (window-edges orig-window)))
542 (mark-active nil) ; inhibit normal region highlight
543 (echo-keystrokes 0) ; don't echo mouse events
544 min max
545 direction
546 event)
548 ;; Get current bounds of overlay
549 (if (eq (overlay-buffer overlay) (current-buffer))
550 (setq min (overlay-start overlay)
551 max (overlay-end overlay))
552 (setq min (point)
553 max min)
554 (set thing-symbol nil))
557 ;; Bar cursor
558 (if (fboundp 'modify-frame-parameters)
559 (modify-frame-parameters (selected-frame)
560 '((cursor-type . bar))))
562 ;; Handle dragging
563 (track-mouse
565 (while (if initial-event ; Use initial event
566 (prog1
567 (setq event initial-event)
568 (setq initial-event nil))
569 (setq event (read-event))
570 (and (consp event)
571 (memq (car event) '(mouse-movement switch-frame))))
573 (let ((selection-thing (symbol-value thing-symbol))
574 (end (event-end event)))
576 (cond
578 ;; Ignore any movement outside the frame
579 ((eq (car-safe event) 'switch-frame) nil)
580 ((and (posn-window end)
581 (not (eq (let ((posn-w (posn-window end)))
582 (if (windowp posn-w)
583 (window-frame posn-w)
584 posn-w))
585 (window-frame orig-window)))) nil)
587 ;; Different window, same frame
588 ((not (eq (posn-window end) orig-window))
589 (let ((end-row (cdr (cdr (mouse-position)))))
590 (cond
591 ((and end-row (not (bobp)) (< end-row top))
592 (mouse-scroll-subr orig-window (- end-row top)
593 overlay max))
594 ((and end-row (not (eobp)) (>= end-row bottom))
595 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
596 overlay min))
599 ;; On the mode line
600 ((eq (posn-point end) 'mode-line)
601 (mouse-scroll-subr orig-window 1 overlay min))
603 ;; In original window
604 (t (goto-char (posn-point end)))
608 ;; Determine direction of drag
609 (cond
610 ((and (not direction) (not (eq min max)))
611 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
612 ((and (not (eq direction -1)) (<= (point) min))
613 (setq direction -1))
614 ((and (not (eq direction 1)) (>= (point) max))
615 (setq direction 1)))
617 (if (not selection-thing) nil
619 ;; If dragging forward, goal is next character
620 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
622 ;; Move to start/end of selected thing
623 (let ((goal (point)))
624 (goto-char (if (eq 1 direction) min max))
625 (condition-case nil
626 (progn
627 (while (> (* direction (- goal (point))) 0)
628 (forward-thing selection-thing direction))
629 (let ((end (point)))
630 (forward-thing selection-thing (- direction))
631 (goto-char
632 (if (> (* direction (- goal (point))) 0)
633 end (point)))))
634 (error))))
636 ;; Move overlay
637 (move-overlay overlay
638 (if (eq 1 direction) min (point))
639 (if (eq -1 direction) max (point))
640 (current-buffer))
642 ))) ; end track-mouse
644 ;; Finish up after dragging
645 (let ((overlay-start (overlay-start overlay))
646 (overlay-end (overlay-end overlay)))
648 ;; Set selection
649 (if (not (eq overlay-start overlay-end))
650 (mouse-sel-set-selection
651 selection
652 (buffer-substring overlay-start overlay-end)))
654 ;; Handle copy/kill
655 (let (this-command)
656 (cond
657 ((eq (event-basic-type last-input-event) 'mouse-2)
658 (copy-region-as-kill overlay-start overlay-end)
659 (read-event) (read-event))
660 ((and (memq (event-basic-type last-input-event)
661 '(mouse-1 mouse-3))
662 (memq 'down (event-modifiers last-input-event)))
663 (kill-region overlay-start overlay-end)
664 (move-overlay overlay overlay-start overlay-start)
665 (read-event) (read-event))
666 ((and (eq (event-basic-type last-input-event) 'mouse-3)
667 (memq 'double (event-modifiers last-input-event)))
668 (kill-region overlay-start overlay-end)
669 (move-overlay overlay overlay-start overlay-start)))))
671 direction)
673 ;; Restore cursor
674 (if (fboundp 'modify-frame-parameters)
675 (modify-frame-parameters
676 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
678 ))))
680 ;;=== Paste ===============================================================
682 (defun mouse-insert-selection (event arg)
683 "Insert the contents of the PRIMARY selection at mouse click.
684 If `mouse-yank-at-point' is non-nil, insert at point instead."
685 (interactive "e\nP")
686 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
687 (mouse-yank-at-click event arg)
688 (mouse-insert-selection-internal 'PRIMARY event)))
690 (defun mouse-insert-secondary (event)
691 "Insert the contents of the SECONDARY selection at mouse click.
692 If `mouse-yank-at-point' is non-nil, insert at point instead."
693 (interactive "e")
694 (mouse-insert-selection-internal 'SECONDARY event))
696 (defun mouse-insert-selection-internal (selection event)
697 "Insert the contents of the named SELECTION at mouse click.
698 If `mouse-yank-at-point' is non-nil, insert at point instead."
699 (unless mouse-yank-at-point
700 (mouse-set-point event))
701 (when mouse-sel-get-selection-function
702 (push-mark (point) 'nomsg)
703 (insert (or (funcall mouse-sel-get-selection-function selection) ""))))
705 ;;=== Handle loss of selections ===========================================
707 (defun mouse-sel-lost-selection-hook (selection)
708 "Remove the overlay for a lost selection."
709 (let ((overlay (mouse-sel-selection-overlay selection)))
710 (delete-overlay overlay)))
712 (provide 'mouse-sel)
714 ;;; arch-tag: 86e6c73f-deaa-48d3-a24e-c565fda1f7d7
715 ;;; mouse-sel.el ends here