(mouse-show-mark): Adjust to new name and don't assume
[bpt/emacs.git] / lisp / mouse-sel.el
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.
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)
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-has-been-enabled nil
200 "Non-nil if Mouse Sel mode has been enabled at least once.")
201
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)
205
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).
211
212 When 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.
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.
224
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.
230
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).
233
234 - Pressing mouse-2 while selecting or extending copies selection
235 to 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
241 primary selection and region."
242 :global t
243 :group 'mouse-sel
244 (if mouse-sel-mode
245 (progn
246 (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
247 (when mouse-sel-default-bindings
248 ;; Save original bindings and replace them with new ones.
249 (setq mouse-sel-original-bindings
250 (mapcar (lambda (binding)
251 (let ((event (car binding)))
252 (prog1 (cons event (lookup-key global-map event))
253 (global-set-key event (cdr binding)))))
254 mouse-sel-bound-events))
255 ;; Update interprogram functions.
256 (setq mouse-sel-original-interprogram-cut-function
257 interprogram-cut-function
258 mouse-sel-original-interprogram-paste-function
259 interprogram-paste-function
260 mouse-sel-has-been-enabled t)
261 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
262 (setq interprogram-cut-function nil
263 interprogram-paste-function nil))))
264
265 ;; Restore original bindings
266 (remove-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
267 (dolist (binding mouse-sel-original-bindings)
268 (global-set-key (car binding) (cdr binding)))
269 ;; Restore the old values of these variables,
270 ;; only if they were actually saved previously.
271 (if mouse-sel-has-been-enabled
272 (setq interprogram-cut-function
273 mouse-sel-original-interprogram-cut-function
274 interprogram-paste-function
275 mouse-sel-original-interprogram-paste-function))))
276
277 ;;=== Internal Variables/Constants ========================================
278
279 (defvar mouse-sel-primary-thing nil
280 "Type of PRIMARY selection in current buffer.")
281 (make-variable-buffer-local 'mouse-sel-primary-thing)
282
283 (defvar mouse-sel-secondary-thing nil
284 "Type of SECONDARY selection in current buffer.")
285 (make-variable-buffer-local 'mouse-sel-secondary-thing)
286
287 ;; Ensure that secondary overlay is defined
288 (unless (overlayp mouse-secondary-overlay)
289 (setq mouse-secondary-overlay (make-overlay 1 1))
290 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
291
292 (defconst mouse-sel-selection-alist
293 '((PRIMARY mouse-drag-overlay mouse-sel-primary-thing)
294 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
295 "Alist associating selections with variables.
296 Each element is of the form:
297
298 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
299
300 where SELECTION-NAME = name of selection
301 OVERLAY-SYMBOL = name of variable containing overlay to use
302 SELECTION-THING-SYMBOL = name of variable where the current selection
303 type for this selection should be stored.")
304
305 (defvar mouse-sel-set-selection-function
306 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
307 'x-set-selection
308 (lambda (selection value)
309 (if (eq selection 'PRIMARY)
310 (x-select-text value)
311 (x-set-selection selection value))))
312 "Function to call to set selection.
313 Called with two arguments:
314
315 SELECTION, the name of the selection concerned, and
316 VALUE, the text to store.
317
318 This sets the selection as well as the cut buffer for the older applications,
319 unless `mouse-sel-default-bindings' is `interprogram-cut-paste'.")
320
321 (defvar mouse-sel-get-selection-function
322 (lambda (selection)
323 (if (eq selection 'PRIMARY)
324 (or (x-cut-buffer-or-selection-value)
325 (bound-and-true-p x-last-selected-text)
326 (bound-and-true-p x-last-selected-text-primary))
327 (x-get-selection selection)))
328 "Function to call to get the selection.
329 Called with one argument:
330
331 SELECTION: the name of the selection concerned.")
332
333 ;;=== Support/access functions ============================================
334
335 (defun mouse-sel-determine-selection-thing (nclicks)
336 "Determine what `thing' `mouse-sel' should operate on.
337 The first argument is NCLICKS, is the number of consecutive
338 mouse clicks at the same position.
339
340 Double-clicking on word constituents selects words.
341 Double-clicking on symbol constituents selects symbols.
342 Double-clicking on quotes or parentheses selects sexps.
343 Double-clicking on whitespace selects whitespace.
344 Triple-clicking selects lines.
345 Quad-clicking selects paragraphs.
346
347 Feel free to re-define this function to support your own desired
348 multi-click semantics."
349 (let* ((next-char (char-after (point)))
350 (char-syntax (if next-char (char-syntax next-char))))
351 (if mouse-sel-cycle-clicks
352 (setq nclicks (1+ (% (1- nclicks) 4))))
353 (cond
354 ((= nclicks 1) nil)
355 ((= nclicks 3) 'line)
356 ((>= nclicks 4) 'paragraph)
357 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
358 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
359 ((eq char-syntax ?_) 'symbol)
360 ((eq char-syntax ?w) 'word))))
361
362 (defun mouse-sel-set-selection (selection value)
363 "Set the specified SELECTION to VALUE."
364 (if mouse-sel-set-selection-function
365 (funcall mouse-sel-set-selection-function selection value)
366 (put 'mouse-sel-internal-selection selection value)))
367
368 (defun mouse-sel-get-selection (selection)
369 "Get the value of the specified SELECTION."
370 (if mouse-sel-get-selection-function
371 (funcall mouse-sel-get-selection-function selection)
372 (get 'mouse-sel-internal-selection selection)))
373
374 (defun mouse-sel-selection-overlay (selection)
375 "Return overlay corresponding to SELECTION."
376 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
377 (or symbol (error "No overlay corresponding to %s selection" selection))
378 (symbol-value symbol)))
379
380 (defun mouse-sel-selection-thing (selection)
381 "Return overlay corresponding to SELECTION."
382 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
383 (or symbol (error "No symbol corresponding to %s selection" selection))
384 symbol))
385
386 (defun mouse-sel-region-to-primary (orig-window)
387 "Convert region to PRIMARY overlay and deactivate region.
388 Argument ORIG-WINDOW specifies the window the cursor was in when the
389 originating command was issued, and is used to determine whether the
390 region was visible or not."
391 (if transient-mark-mode
392 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
393 (cond
394 ((and mark-active
395 (or highlight-nonselected-windows
396 (eq orig-window (selected-window))))
397 ;; Region was visible, so convert region to overlay
398 (move-overlay overlay (region-beginning) (region-end)
399 (current-buffer)))
400 ((eq orig-window (selected-window))
401 ;; Point was visible, so set overlay at point
402 (move-overlay overlay (point) (point) (current-buffer)))
403 (t
404 ;; Nothing was visible, so remove overlay
405 (delete-overlay overlay)))
406 (setq mark-active nil))))
407
408 (defun mouse-sel-primary-to-region (&optional direction)
409 "Convert PRIMARY overlay to region.
410 Optional argument DIRECTION specifies the mouse drag direction: a value of
411 1 indicates that the mouse was dragged left-to-right, otherwise it was
412 dragged right-to-left."
413 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
414 (start (overlay-start overlay))
415 (end (overlay-end overlay)))
416 (if (eq start end)
417 (progn
418 (if start (goto-char start))
419 (deactivate-mark))
420 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
421 (progn
422 (goto-char end)
423 (push-mark start 'nomsg 'active))
424 (goto-char start)
425 (push-mark end 'nomsg 'active)))
426 (if transient-mark-mode (delete-overlay overlay))))
427
428 (defmacro mouse-sel-eval-at-event-end (event &rest forms)
429 "Evaluate forms at mouse position.
430 Move to the end position of EVENT, execute FORMS, and restore original
431 point and window."
432 `(let ((posn (event-end ,event)))
433 (if posn (mouse-minibuffer-check ,event))
434 (if (and posn (not (windowp (posn-window posn))))
435 (error "Cursor not in text area of window"))
436 (let (orig-window orig-point-marker)
437 (setq orig-window (selected-window))
438 (if posn (select-window (posn-window posn)))
439 (setq orig-point-marker (point-marker))
440 (if (and posn (numberp (posn-point posn)))
441 (goto-char (posn-point posn)))
442 (unwind-protect
443 (progn
444 ,@forms)
445 (goto-char (marker-position orig-point-marker))
446 (move-marker orig-point-marker nil)
447 (select-window orig-window)))))
448
449 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
450
451 ;;=== Select ==============================================================
452
453 (defun mouse-select (event)
454 "Set region/selection using the mouse.
455
456 Click sets point & mark to click position.
457 Dragging extends region/selection.
458
459 Multi-clicking selects word/lines/paragraphs, as determined by
460 'mouse-sel-determine-selection-thing.
461
462 Clicking mouse-2 while selecting copies selected text to the kill-ring.
463 Clicking mouse-1 or mouse-3 kills the selected text.
464
465 This should be bound to a down-mouse event."
466 (interactive "@e")
467 (let (direction)
468 (unwind-protect
469 (setq direction (mouse-select-internal 'PRIMARY event))
470 (mouse-sel-primary-to-region direction))))
471
472 (defun mouse-select-secondary (event)
473 "Set secondary selection using the mouse.
474
475 Click sets the start of the secondary selection to click position.
476 Dragging extends the secondary selection.
477
478 Multi-clicking selects word/lines/paragraphs, as determined by
479 'mouse-sel-determine-selection-thing.
480
481 Clicking mouse-2 while selecting copies selected text to the kill-ring.
482 Clicking mouse-1 or mouse-3 kills the selected text.
483
484 This should be bound to a down-mouse event."
485 (interactive "e")
486 (mouse-select-internal 'SECONDARY event))
487
488 (defun mouse-select-internal (selection event)
489 "Set SELECTION using the mouse."
490 (mouse-sel-eval-at-event-end event
491 (let ((thing-symbol (mouse-sel-selection-thing selection))
492 (overlay (mouse-sel-selection-overlay selection)))
493 (set thing-symbol
494 (mouse-sel-determine-selection-thing (event-click-count event)))
495 (let ((object-bounds (bounds-of-thing-at-point
496 (symbol-value thing-symbol))))
497 (if object-bounds
498 (progn
499 (move-overlay overlay
500 (car object-bounds) (cdr object-bounds)
501 (current-buffer)))
502 (move-overlay overlay (point) (point) (current-buffer)))))
503 (mouse-extend-internal selection)))
504
505 ;;=== Extend ==============================================================
506
507 (defun mouse-extend (event)
508 "Extend region/selection using the mouse."
509 (interactive "e")
510 (let ((orig-window (selected-window))
511 direction)
512 (select-window (posn-window (event-end event)))
513 (unwind-protect
514 (progn
515 (mouse-sel-region-to-primary orig-window)
516 (setq direction (mouse-extend-internal 'PRIMARY event)))
517 (mouse-sel-primary-to-region direction))))
518
519 (defun mouse-extend-secondary (event)
520 "Extend secondary selection using the mouse."
521 (interactive "e")
522 (save-window-excursion
523 (mouse-extend-internal 'SECONDARY event)))
524
525 (defun mouse-extend-internal (selection &optional initial-event)
526 "Extend specified SELECTION using the mouse.
527 Track mouse-motion events, adjusting the SELECTION appropriately.
528 Optional argument INITIAL-EVENT specifies an initial down-mouse event to
529 process.
530
531 See documentation for mouse-select-internal for more details."
532 (mouse-sel-eval-at-event-end initial-event
533 (let ((orig-cursor-type
534 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
535 (unwind-protect
536
537 (let* ((thing-symbol (mouse-sel-selection-thing selection))
538 (overlay (mouse-sel-selection-overlay selection))
539 (orig-window (selected-window))
540 (orig-window-frame (window-frame orig-window))
541 (top (nth 1 (window-edges orig-window)))
542 (bottom (nth 3 (window-edges orig-window)))
543 (mark-active nil) ; inhibit normal region highlight
544 (echo-keystrokes 0) ; don't echo mouse events
545 min max
546 direction
547 event)
548
549 ;; Get current bounds of overlay
550 (if (eq (overlay-buffer overlay) (current-buffer))
551 (setq min (overlay-start overlay)
552 max (overlay-end overlay))
553 (setq min (point)
554 max min)
555 (set thing-symbol nil))
556
557
558 ;; Bar cursor
559 (if (fboundp 'modify-frame-parameters)
560 (modify-frame-parameters (selected-frame)
561 '((cursor-type . bar))))
562
563 ;; Handle dragging
564 (track-mouse
565
566 (while (if initial-event ; Use initial event
567 (prog1
568 (setq event initial-event)
569 (setq initial-event nil))
570 (setq event (read-event))
571 (and (consp event)
572 (memq (car event) '(mouse-movement switch-frame))))
573
574 (let ((selection-thing (symbol-value thing-symbol))
575 (end (event-end event)))
576
577 (cond
578
579 ;; Ignore any movement outside the frame
580 ((eq (car-safe event) 'switch-frame) nil)
581 ((and (posn-window end)
582 (not (eq (let ((posn-w (posn-window end)))
583 (if (windowp posn-w)
584 (window-frame posn-w)
585 posn-w))
586 (window-frame orig-window)))) nil)
587
588 ;; Different window, same frame
589 ((not (eq (posn-window end) orig-window))
590 (let ((end-row (cdr (cdr (mouse-position)))))
591 (cond
592 ((and end-row (not (bobp)) (< end-row top))
593 (mouse-scroll-subr orig-window (- end-row top)
594 overlay max))
595 ((and end-row (not (eobp)) (>= end-row bottom))
596 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
597 overlay min))
598 )))
599
600 ;; On the mode line
601 ((eq (posn-point end) 'mode-line)
602 (mouse-scroll-subr orig-window 1 overlay min))
603
604 ;; In original window
605 (t (goto-char (posn-point end)))
606
607 )
608
609 ;; Determine direction of drag
610 (cond
611 ((and (not direction) (not (eq min max)))
612 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
613 ((and (not (eq direction -1)) (<= (point) min))
614 (setq direction -1))
615 ((and (not (eq direction 1)) (>= (point) max))
616 (setq direction 1)))
617
618 (if (not selection-thing) nil
619
620 ;; If dragging forward, goal is next character
621 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
622
623 ;; Move to start/end of selected thing
624 (let ((goal (point)))
625 (goto-char (if (eq 1 direction) min max))
626 (condition-case nil
627 (progn
628 (while (> (* direction (- goal (point))) 0)
629 (forward-thing selection-thing direction))
630 (let ((end (point)))
631 (forward-thing selection-thing (- direction))
632 (goto-char
633 (if (> (* direction (- goal (point))) 0)
634 end (point)))))
635 (error))))
636
637 ;; Move overlay
638 (move-overlay overlay
639 (if (eq 1 direction) min (point))
640 (if (eq -1 direction) max (point))
641 (current-buffer))
642
643 ))) ; end track-mouse
644
645 ;; Finish up after dragging
646 (let ((overlay-start (overlay-start overlay))
647 (overlay-end (overlay-end overlay)))
648
649 ;; Set selection
650 (if (not (eq overlay-start overlay-end))
651 (mouse-sel-set-selection
652 selection
653 (buffer-substring overlay-start overlay-end)))
654
655 ;; Handle copy/kill
656 (let (this-command)
657 (cond
658 ((eq (event-basic-type last-input-event) 'mouse-2)
659 (copy-region-as-kill overlay-start overlay-end)
660 (read-event) (read-event))
661 ((and (memq (event-basic-type last-input-event)
662 '(mouse-1 mouse-3))
663 (memq 'down (event-modifiers last-input-event)))
664 (kill-region overlay-start overlay-end)
665 (move-overlay overlay overlay-start overlay-start)
666 (read-event) (read-event))
667 ((and (eq (event-basic-type last-input-event) 'mouse-3)
668 (memq 'double (event-modifiers last-input-event)))
669 (kill-region overlay-start overlay-end)
670 (move-overlay overlay overlay-start overlay-start)))))
671
672 direction)
673
674 ;; Restore cursor
675 (if (fboundp 'modify-frame-parameters)
676 (modify-frame-parameters
677 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
678
679 ))))
680
681 ;;=== Paste ===============================================================
682
683 (defun mouse-insert-selection (event arg)
684 "Insert the contents of the PRIMARY selection at mouse click.
685 If `mouse-yank-at-point' is non-nil, insert at point instead."
686 (interactive "e\nP")
687 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
688 (mouse-yank-at-click event arg)
689 (mouse-insert-selection-internal 'PRIMARY event)))
690
691 (defun mouse-insert-secondary (event)
692 "Insert the contents of the SECONDARY selection at mouse click.
693 If `mouse-yank-at-point' is non-nil, insert at point instead."
694 (interactive "e")
695 (mouse-insert-selection-internal 'SECONDARY event))
696
697 (defun mouse-insert-selection-internal (selection event)
698 "Insert the contents of the named SELECTION at mouse click.
699 If `mouse-yank-at-point' is non-nil, insert at point instead."
700 (unless mouse-yank-at-point
701 (mouse-set-point event))
702 (when mouse-sel-get-selection-function
703 (push-mark (point) 'nomsg)
704 (insert (or (funcall mouse-sel-get-selection-function selection) ""))))
705
706 ;;=== Handle loss of selections ===========================================
707
708 (defun mouse-sel-lost-selection-hook (selection)
709 "Remove the overlay for a lost selection."
710 (let ((overlay (mouse-sel-selection-overlay selection)))
711 (delete-overlay overlay)))
712
713 (provide 'mouse-sel)
714
715 ;;; arch-tag: 86e6c73f-deaa-48d3-a24e-c565fda1f7d7
716 ;;; mouse-sel.el ends here