(Fdefine_fringe_bitmap): Fix typo in docstring.
[emacs.git] / lisp / mouse-sel.el
blob92e837657f698aba4b267ec340f1e868e7a4e9fc
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-original-bindings nil)
200 (defvar mouse-sel-original-interprogram-cut-function nil)
201 (defvar mouse-sel-original-interprogram-paste-function nil)
203 ;;;###autoload
204 (define-minor-mode mouse-sel-mode
205 "Toggle Mouse Sel mode.
206 With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive.
207 Returns the new status of Mouse Sel mode (non-nil means on).
209 When Mouse Sel mode is enabled, mouse selection is enhanced in various ways:
211 - Clicking mouse-1 starts (cancels) selection, dragging extends it.
213 - Clicking or dragging mouse-3 extends the selection as well.
215 - Double-clicking on word constituents selects words.
216 Double-clicking on symbol constituents selects symbols.
217 Double-clicking on quotes or parentheses selects sexps.
218 Double-clicking on whitespace selects whitespace.
219 Triple-clicking selects lines.
220 Quad-clicking selects paragraphs.
222 - Selecting sets the region & X primary selection, but does NOT affect
223 the `kill-ring', nor do the kill-ring functions change the X selection.
224 Because the mouse handlers set the primary selection directly,
225 mouse-sel sets the variables `interprogram-cut-function' and
226 `interprogram-paste-function' to nil.
228 - Clicking mouse-2 inserts the contents of the primary selection at
229 the mouse position (or point, if `mouse-yank-at-point' is non-nil).
231 - Pressing mouse-2 while selecting or extending copies selection
232 to the kill ring. Pressing mouse-1 or mouse-3 kills it.
234 - Double-clicking mouse-3 also kills selection.
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
238 primary 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))))
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)))
269 ;;=== Internal Variables/Constants ========================================
271 (defvar mouse-sel-primary-thing nil
272 "Type of PRIMARY selection in current buffer.")
273 (make-variable-buffer-local 'mouse-sel-primary-thing)
275 (defvar mouse-sel-secondary-thing nil
276 "Type of SECONDARY selection in current buffer.")
277 (make-variable-buffer-local 'mouse-sel-secondary-thing)
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))
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.
288 Each element is of the form:
290 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
292 where 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.")
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.
305 Called with two arguments:
307 SELECTION, the name of the selection concerned, and
308 VALUE, the text to store.
310 This sets the selection as well as the cut buffer for the older applications,
311 unless `mouse-sel-default-bindings' is `interprogram-cut-paste'.")
313 (defvar mouse-sel-get-selection-function
314 (lambda (selection)
315 (if (eq selection 'PRIMARY)
316 (or (x-cut-buffer-or-selection-value)
317 (bound-and-true-p x-last-selected-text)
318 (bound-and-true-p x-last-selected-text-primary))
319 (x-get-selection selection)))
320 "Function to call to get the selection.
321 Called with one argument:
323 SELECTION: the name of the selection concerned.")
325 ;;=== Support/access functions ============================================
327 (defun mouse-sel-determine-selection-thing (nclicks)
328 "Determine what `thing' `mouse-sel' should operate on.
329 The first argument is NCLICKS, is the number of consecutive
330 mouse clicks at the same position.
332 Double-clicking on word constituents selects words.
333 Double-clicking on symbol constituents selects symbols.
334 Double-clicking on quotes or parentheses selects sexps.
335 Double-clicking on whitespace selects whitespace.
336 Triple-clicking selects lines.
337 Quad-clicking selects paragraphs.
339 Feel free to re-define this function to support your own desired
340 multi-click semantics."
341 (let* ((next-char (char-after (point)))
342 (char-syntax (if next-char (char-syntax next-char))))
343 (if mouse-sel-cycle-clicks
344 (setq nclicks (1+ (% (1- nclicks) 4))))
345 (cond
346 ((= nclicks 1) nil)
347 ((= nclicks 3) 'line)
348 ((>= nclicks 4) 'paragraph)
349 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
350 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
351 ((eq char-syntax ?_) 'symbol)
352 ((eq char-syntax ?w) 'word))))
354 (defun mouse-sel-set-selection (selection value)
355 "Set the specified SELECTION to VALUE."
356 (if mouse-sel-set-selection-function
357 (funcall mouse-sel-set-selection-function selection value)
358 (put 'mouse-sel-internal-selection selection value)))
360 (defun mouse-sel-get-selection (selection)
361 "Get the value of the specified SELECTION."
362 (if mouse-sel-get-selection-function
363 (funcall mouse-sel-get-selection-function selection)
364 (get 'mouse-sel-internal-selection selection)))
366 (defun mouse-sel-selection-overlay (selection)
367 "Return overlay corresponding to SELECTION."
368 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
369 (or symbol (error "No overlay corresponding to %s selection" selection))
370 (symbol-value symbol)))
372 (defun mouse-sel-selection-thing (selection)
373 "Return overlay corresponding to SELECTION."
374 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
375 (or symbol (error "No symbol corresponding to %s selection" selection))
376 symbol))
378 (defun mouse-sel-region-to-primary (orig-window)
379 "Convert region to PRIMARY overlay and deactivate region.
380 Argument ORIG-WINDOW specifies the window the cursor was in when the
381 originating command was issued, and is used to determine whether the
382 region was visible or not."
383 (if transient-mark-mode
384 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
385 (cond
386 ((and mark-active
387 (or highlight-nonselected-windows
388 (eq orig-window (selected-window))))
389 ;; Region was visible, so convert region to overlay
390 (move-overlay overlay (region-beginning) (region-end)
391 (current-buffer)))
392 ((eq orig-window (selected-window))
393 ;; Point was visible, so set overlay at point
394 (move-overlay overlay (point) (point) (current-buffer)))
396 ;; Nothing was visible, so remove overlay
397 (delete-overlay overlay)))
398 (setq mark-active nil))))
400 (defun mouse-sel-primary-to-region (&optional direction)
401 "Convert PRIMARY overlay to region.
402 Optional argument DIRECTION specifies the mouse drag direction: a value of
403 1 indicates that the mouse was dragged left-to-right, otherwise it was
404 dragged right-to-left."
405 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
406 (start (overlay-start overlay))
407 (end (overlay-end overlay)))
408 (if (eq start end)
409 (progn
410 (if start (goto-char start))
411 (deactivate-mark))
412 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
413 (progn
414 (goto-char end)
415 (push-mark start 'nomsg 'active))
416 (goto-char start)
417 (push-mark end 'nomsg 'active)))
418 (if transient-mark-mode (delete-overlay overlay))))
420 (defmacro mouse-sel-eval-at-event-end (event &rest forms)
421 "Evaluate forms at mouse position.
422 Move to the end position of EVENT, execute FORMS, and restore original
423 point and window."
424 `(let ((posn (event-end ,event)))
425 (if posn (mouse-minibuffer-check ,event))
426 (if (and posn (not (windowp (posn-window posn))))
427 (error "Cursor not in text area of window"))
428 (let (orig-window orig-point-marker)
429 (setq orig-window (selected-window))
430 (if posn (select-window (posn-window posn)))
431 (setq orig-point-marker (point-marker))
432 (if (and posn (numberp (posn-point posn)))
433 (goto-char (posn-point posn)))
434 (unwind-protect
435 (progn
436 ,@forms)
437 (goto-char (marker-position orig-point-marker))
438 (move-marker orig-point-marker nil)
439 (select-window orig-window)))))
441 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
443 ;;=== Select ==============================================================
445 (defun mouse-select (event)
446 "Set region/selection using the mouse.
448 Click sets point & mark to click position.
449 Dragging extends region/selection.
451 Multi-clicking selects word/lines/paragraphs, as determined by
452 'mouse-sel-determine-selection-thing.
454 Clicking mouse-2 while selecting copies selected text to the kill-ring.
455 Clicking mouse-1 or mouse-3 kills the selected text.
457 This should be bound to a down-mouse event."
458 (interactive "@e")
459 (let (direction)
460 (unwind-protect
461 (setq direction (mouse-select-internal 'PRIMARY event))
462 (mouse-sel-primary-to-region direction))))
464 (defun mouse-select-secondary (event)
465 "Set secondary selection using the mouse.
467 Click sets the start of the secondary selection to click position.
468 Dragging extends the secondary selection.
470 Multi-clicking selects word/lines/paragraphs, as determined by
471 'mouse-sel-determine-selection-thing.
473 Clicking mouse-2 while selecting copies selected text to the kill-ring.
474 Clicking mouse-1 or mouse-3 kills the selected text.
476 This should be bound to a down-mouse event."
477 (interactive "e")
478 (mouse-select-internal 'SECONDARY event))
480 (defun mouse-select-internal (selection event)
481 "Set SELECTION using the mouse."
482 (mouse-sel-eval-at-event-end event
483 (let ((thing-symbol (mouse-sel-selection-thing selection))
484 (overlay (mouse-sel-selection-overlay selection)))
485 (set thing-symbol
486 (mouse-sel-determine-selection-thing (event-click-count event)))
487 (let ((object-bounds (bounds-of-thing-at-point
488 (symbol-value thing-symbol))))
489 (if object-bounds
490 (progn
491 (move-overlay overlay
492 (car object-bounds) (cdr object-bounds)
493 (current-buffer)))
494 (move-overlay overlay (point) (point) (current-buffer)))))
495 (mouse-extend-internal selection)))
497 ;;=== Extend ==============================================================
499 (defun mouse-extend (event)
500 "Extend region/selection using the mouse."
501 (interactive "e")
502 (let ((orig-window (selected-window))
503 direction)
504 (select-window (posn-window (event-end event)))
505 (unwind-protect
506 (progn
507 (mouse-sel-region-to-primary orig-window)
508 (setq direction (mouse-extend-internal 'PRIMARY event)))
509 (mouse-sel-primary-to-region direction))))
511 (defun mouse-extend-secondary (event)
512 "Extend secondary selection using the mouse."
513 (interactive "e")
514 (save-window-excursion
515 (mouse-extend-internal 'SECONDARY event)))
517 (defun mouse-extend-internal (selection &optional initial-event)
518 "Extend specified SELECTION using the mouse.
519 Track mouse-motion events, adjusting the SELECTION appropriately.
520 Optional argument INITIAL-EVENT specifies an initial down-mouse event to
521 process.
523 See documentation for mouse-select-internal for more details."
524 (mouse-sel-eval-at-event-end initial-event
525 (let ((orig-cursor-type
526 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
527 (unwind-protect
529 (let* ((thing-symbol (mouse-sel-selection-thing selection))
530 (overlay (mouse-sel-selection-overlay selection))
531 (orig-window (selected-window))
532 (orig-window-frame (window-frame orig-window))
533 (top (nth 1 (window-edges orig-window)))
534 (bottom (nth 3 (window-edges orig-window)))
535 (mark-active nil) ; inhibit normal region highlight
536 (echo-keystrokes 0) ; don't echo mouse events
537 min max
538 direction
539 event)
541 ;; Get current bounds of overlay
542 (if (eq (overlay-buffer overlay) (current-buffer))
543 (setq min (overlay-start overlay)
544 max (overlay-end overlay))
545 (setq min (point)
546 max min)
547 (set thing-symbol nil))
550 ;; Bar cursor
551 (if (fboundp 'modify-frame-parameters)
552 (modify-frame-parameters (selected-frame)
553 '((cursor-type . bar))))
555 ;; Handle dragging
556 (track-mouse
558 (while (if initial-event ; Use initial event
559 (prog1
560 (setq event initial-event)
561 (setq initial-event nil))
562 (setq event (read-event))
563 (and (consp event)
564 (memq (car event) '(mouse-movement switch-frame))))
566 (let ((selection-thing (symbol-value thing-symbol))
567 (end (event-end event)))
569 (cond
571 ;; Ignore any movement outside the frame
572 ((eq (car-safe event) 'switch-frame) nil)
573 ((and (posn-window end)
574 (not (eq (let ((posn-w (posn-window end)))
575 (if (windowp posn-w)
576 (window-frame posn-w)
577 posn-w))
578 (window-frame orig-window)))) nil)
580 ;; Different window, same frame
581 ((not (eq (posn-window end) orig-window))
582 (let ((end-row (cdr (cdr (mouse-position)))))
583 (cond
584 ((and end-row (not (bobp)) (< end-row top))
585 (mouse-scroll-subr orig-window (- end-row top)
586 overlay max))
587 ((and end-row (not (eobp)) (>= end-row bottom))
588 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
589 overlay min))
592 ;; On the mode line
593 ((eq (posn-point end) 'mode-line)
594 (mouse-scroll-subr orig-window 1 overlay min))
596 ;; In original window
597 (t (goto-char (posn-point end)))
601 ;; Determine direction of drag
602 (cond
603 ((and (not direction) (not (eq min max)))
604 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
605 ((and (not (eq direction -1)) (<= (point) min))
606 (setq direction -1))
607 ((and (not (eq direction 1)) (>= (point) max))
608 (setq direction 1)))
610 (if (not selection-thing) nil
612 ;; If dragging forward, goal is next character
613 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
615 ;; Move to start/end of selected thing
616 (let ((goal (point)))
617 (goto-char (if (eq 1 direction) min max))
618 (condition-case nil
619 (progn
620 (while (> (* direction (- goal (point))) 0)
621 (forward-thing selection-thing direction))
622 (let ((end (point)))
623 (forward-thing selection-thing (- direction))
624 (goto-char
625 (if (> (* direction (- goal (point))) 0)
626 end (point)))))
627 (error))))
629 ;; Move overlay
630 (move-overlay overlay
631 (if (eq 1 direction) min (point))
632 (if (eq -1 direction) max (point))
633 (current-buffer))
635 ))) ; end track-mouse
637 ;; Finish up after dragging
638 (let ((overlay-start (overlay-start overlay))
639 (overlay-end (overlay-end overlay)))
641 ;; Set selection
642 (if (not (eq overlay-start overlay-end))
643 (mouse-sel-set-selection
644 selection
645 (buffer-substring overlay-start overlay-end)))
647 ;; Handle copy/kill
648 (let (this-command)
649 (cond
650 ((eq (event-basic-type last-input-event) 'mouse-2)
651 (copy-region-as-kill overlay-start overlay-end)
652 (read-event) (read-event))
653 ((and (memq (event-basic-type last-input-event)
654 '(mouse-1 mouse-3))
655 (memq 'down (event-modifiers last-input-event)))
656 (kill-region overlay-start overlay-end)
657 (move-overlay overlay overlay-start overlay-start)
658 (read-event) (read-event))
659 ((and (eq (event-basic-type last-input-event) 'mouse-3)
660 (memq 'double (event-modifiers last-input-event)))
661 (kill-region overlay-start overlay-end)
662 (move-overlay overlay overlay-start overlay-start)))))
664 direction)
666 ;; Restore cursor
667 (if (fboundp 'modify-frame-parameters)
668 (modify-frame-parameters
669 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
671 ))))
673 ;;=== Paste ===============================================================
675 (defun mouse-insert-selection (event arg)
676 "Insert the contents of the PRIMARY selection at mouse click.
677 If `mouse-yank-at-point' is non-nil, insert at point instead."
678 (interactive "e\nP")
679 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
680 (mouse-yank-at-click event arg)
681 (mouse-insert-selection-internal 'PRIMARY event)))
683 (defun mouse-insert-secondary (event)
684 "Insert the contents of the SECONDARY selection at mouse click.
685 If `mouse-yank-at-point' is non-nil, insert at point instead."
686 (interactive "e")
687 (mouse-insert-selection-internal 'SECONDARY event))
689 (defun mouse-insert-selection-internal (selection event)
690 "Insert the contents of the named SELECTION at mouse click.
691 If `mouse-yank-at-point' is non-nil, insert at point instead."
692 (unless mouse-yank-at-point
693 (mouse-set-point event))
694 (when mouse-sel-get-selection-function
695 (push-mark (point) 'nomsg)
696 (insert (or (funcall mouse-sel-get-selection-function selection) ""))))
698 ;;=== Handle loss of selections ===========================================
700 (defun mouse-sel-lost-selection-hook (selection)
701 "Remove the overlay for a lost selection."
702 (let ((overlay (mouse-sel-selection-overlay selection)))
703 (delete-overlay overlay)))
705 (provide 'mouse-sel)
707 ;;; arch-tag: 86e6c73f-deaa-48d3-a24e-c565fda1f7d7
708 ;;; mouse-sel.el ends here