(isearch-*-char): New arg WANT-BACKSLASH.
[bpt/emacs.git] / lisp / mouse-sel.el
CommitLineData
e8af40ee 1;;; mouse-sel.el --- multi-click selection support for Emacs 19
1a2b6c52 2
4bc65152 3;; Copyright (C) 1993,1994,1995,2001,2002 Free Software Foundation, Inc.
1a2b6c52 4
9ae4c925 5;; Author: Mike Williams <mdub@bigfoot.com>
1a2b6c52 6;; Keywords: mouse
1a2b6c52
RS
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
b578f267
EN
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
07b3798c 25;;; Commentary:
b578f267 26
1a2b6c52
RS
27;; This module provides multi-click mouse support for GNU Emacs versions
28;; 19.18 and later. I've tried to make it behave more like standard X
29;; clients (eg. xterm) than the default Emacs 19 mouse selection handlers.
30;; Basically:
31;;
32;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
33;;
34;; * Clicking or dragging mouse-3 extends the selection as well.
35;;
36;; * Double-clicking on word constituents selects words.
37;; Double-clicking on symbol constituents selects symbols.
38;; Double-clicking on quotes or parentheses selects sexps.
39;; Double-clicking on whitespace selects whitespace.
40;; Triple-clicking selects lines.
f2506826 41;; Quad-clicking selects paragraphs.
1a2b6c52
RS
42;;
43;; * Selecting sets the region & X primary selection, but does NOT affect
44;; the kill-ring. Because the mouse handlers set the primary selection
45;; directly, mouse-sel sets the variables interprogram-cut-function
46;; and interprogram-paste-function to nil.
47;;
f2506826
RS
48;; * Clicking mouse-2 inserts the contents of the primary selection at
49;; the mouse position (or point, if mouse-yank-at-point is non-nil).
1a2b6c52 50;;
6a2e3631 51;; * Pressing mouse-2 while selecting or extending copies selection
1a2b6c52 52;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
8a946354 53;;
6a2e3631 54;; * Double-clicking mouse-3 also kills selection.
8a946354 55;;
f2506826
RS
56;; * M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
57;; & mouse-3, but operate on the X secondary selection rather than the
58;; primary selection and region.
1a2b6c52 59;;
6a2e3631
RS
60;; This module requires my thingatpt.el module, which it uses to find the
61;; bounds of words, lines, sexps, etc.
1a2b6c52
RS
62;;
63;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
64;;
6a2e3631
RS
65;;--- Customisation -------------------------------------------------------
66;;
67;; * You may want to use none or more of following:
1a2b6c52
RS
68;;
69;; ;; Enable region highlight
70;; (transient-mark-mode 1)
71;;
72;; ;; But only in the selected window
73;; (setq highlight-nonselected-windows nil)
8a946354 74;;
1a2b6c52
RS
75;; ;; Enable pending-delete
76;; (delete-selection-mode 1)
77;;
f77e1e4b 78;; * You can control the way mouse-sel binds its keys by setting the value
1a2b6c52
RS
79;; of mouse-sel-default-bindings before loading mouse-sel.
80;;
81;; (a) If mouse-sel-default-bindings = t (the default)
8a946354 82;;
f2506826 83;; Mouse sets and insert selection
1a2b6c52
RS
84;; mouse-1 mouse-select
85;; mouse-2 mouse-insert-selection
f2506826 86;; mouse-3 mouse-extend
1a2b6c52
RS
87;;
88;; Selection/kill-ring interaction is disabled
89;; interprogram-cut-function = nil
90;; interprogram-paste-function = nil
91;;
92;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
8a946354 93;;
1a2b6c52 94;; Mouse sets selection, and pastes from kill-ring
f2506826 95;; mouse-1 mouse-select
640201f8 96;; mouse-2 mouse-insert-selection
f2506826 97;; mouse-3 mouse-extend
640201f8 98;; In this mode, mouse-insert-selection just calls mouse-yank-at-click.
8a946354 99;;
1a2b6c52
RS
100;; Selection/kill-ring interaction is retained
101;; interprogram-cut-function = x-select-text
102;; interprogram-paste-function = x-cut-buffer-or-selection-value
8a946354 103;;
1a2b6c52
RS
104;; What you lose is the ability to select some text in
105;; delete-selection-mode and yank over the top of it.
8a946354 106;;
1a2b6c52
RS
107;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
108;;
cd1f32a6
RS
109;; * By default, mouse-insert-selection (mouse-2) inserts the selection at
110;; the mouse position. You can tell it to insert at point instead with:
111;;
6b4dd332 112;; (setq mouse-yank-at-point t)
cd1f32a6 113;;
1a2b6c52
RS
114;; * I like to leave point at the end of the region nearest to where the
115;; mouse was, even though this makes region highlighting mis-leading (the
116;; cursor makes it look like one extra character is selected). You can
117;; disable this behaviour with:
118;;
119;; (setq mouse-sel-leave-point-near-mouse nil)
120;;
f2506826
RS
121;; * By default, mouse-select cycles the click count after 4 clicks. That
122;; is, clicking mouse-1 five times has the same effect as clicking it
123;; once, clicking six times has the same effect as clicking twice, etc.
1a2b6c52
RS
124;; Disable this behaviour with:
125;;
126;; (setq mouse-sel-cycle-clicks nil)
127;;
f2506826
RS
128;; * The variables mouse-sel-{set,get}-selection-function control how the
129;; selection is handled. Under X Windows, these variables default so
1a2b6c52
RS
130;; that the X primary selection is used. Under other windowing systems,
131;; alternate functions are used, which simply store the selection value
132;; in a variable.
133;;
1a2b6c52
RS
134;; * You can change the selection highlight face by altering the properties
135;; of mouse-drag-overlay, eg.
136;;
137;; (overlay-put mouse-drag-overlay 'face 'bold)
1a2b6c52 138
b578f267 139;;; Code:
1a2b6c52 140
1a2b6c52
RS
141(require 'mouse)
142(require 'thingatpt)
1a2b6c52 143
ad3f1e65
SM
144(eval-when-compile
145 (require 'cl))
146
1a2b6c52
RS
147;;=== User Variables ======================================================
148
ad3f1e65
SM
149(defgroup mouse-sel nil
150 "Mouse selection enhancement."
151 :group 'mouse)
152
ad3f1e65 153(defcustom mouse-sel-leave-point-near-mouse t
1a2b6c52 154 "*Leave point near last mouse position.
f2506826 155If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
1a2b6c52 156of the region nearest to where the mouse last was.
ad3f1e65
SM
157If nil, point will always be placed at the beginning of the region."
158 :type 'boolean
159 :group 'mouse-sel)
160
161(defcustom mouse-sel-cycle-clicks t
162 "*If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks."
163 :type 'boolean
164 :group 'mouse-sel)
165
166(defcustom mouse-sel-default-bindings t
167 "*Control mouse bindings."
168 :type '(choice (const :tag "none" nil)
21cabb0f
AS
169 (const :tag "cut and paste" interprogram-cut-paste)
170 (other :tag "default bindings" t))
ad3f1e65
SM
171 :group 'mouse-sel)
172
640201f8
SM
173;;=== Key bindings ========================================================
174
175(defconst mouse-sel-bound-events
176 '(;; Primary selection bindings.
177 ;;
178 ;; Bind keys to `ignore' instead of unsetting them because modes may
179 ;; bind `down-mouse-1', for instance, without binding `mouse-1'.
180 ;; If we unset `mouse-1', this leads to a bitch_at_user when the
181 ;; mouse goes up because no matching binding is found for that.
182 ([mouse-1] . ignore)
183 ([drag-mouse-1] . ignore)
184 ([mouse-3] . ignore)
185 ([down-mouse-1] . mouse-select)
186 ([down-mouse-3] . mouse-extend)
187 ([mouse-2] . mouse-insert-selection)
188 ;; Secondary selection bindings.
189 ([M-mouse-1] . ignore)
190 ([M-drag-mouse-1] . ignore)
191 ([M-mouse-3] . ignore)
192 ([M-down-mouse-1] . mouse-select-secondary)
193 ([M-mouse-2] . mouse-insert-secondary)
194 ([M-down-mouse-3] . mouse-extend-secondary))
195 "An alist of events that `mouse-sel-mode' binds.")
196
ad3f1e65
SM
197;;=== User Command ========================================================
198
640201f8
SM
199(defvar mouse-sel-original-bindings nil)
200(defvar mouse-sel-original-interprogram-cut-function nil)
caebc99b 201(defvar mouse-sel-original-interprogram-paste-function nil)
640201f8 202
ad3f1e65 203;;;###autoload
640201f8 204(define-minor-mode mouse-sel-mode
ad3f1e65
SM
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
640201f8 223the `kill-ring', nor do the kill-ring functions change the X selection.
9eebe311 224Because the mouse handlers set the primary selection directly,
640201f8
SM
225mouse-sel sets the variables `interprogram-cut-function' and
226`interprogram-paste-function' to nil.
1a2b6c52 227
ad3f1e65 228- Clicking mouse-2 inserts the contents of the primary selection at
f7542b92 229the mouse position (or point, if `mouse-yank-at-point' is non-nil).
1a2b6c52 230
ad3f1e65
SM
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."
640201f8
SM
239 :global t
240 (if mouse-sel-mode
241 (progn
ad3f1e65 242 (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
640201f8
SM
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))))
ad3f1e65 259
4bc65152 260 ;; Restore original bindings
640201f8
SM
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
5a95c143
MW
265 mouse-sel-original-interprogram-cut-function
266 interprogram-paste-function
640201f8 267 mouse-sel-original-interprogram-paste-function)))
1a2b6c52 268
f2506826
RS
269;;=== Internal Variables/Constants ========================================
270
8a946354 271(defvar mouse-sel-primary-thing nil
f2506826
RS
272 "Type of PRIMARY selection in current buffer.")
273(make-variable-buffer-local 'mouse-sel-primary-thing)
274
8a946354 275(defvar mouse-sel-secondary-thing nil
f2506826
RS
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
ad3f1e65 280(unless (overlayp mouse-secondary-overlay)
f2506826
RS
281 (setq mouse-secondary-overlay (make-overlay 1 1))
282 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
1a2b6c52 283
f2506826
RS
284(defconst mouse-sel-selection-alist
285 '((PRIMARY mouse-drag-overlay mouse-sel-primary-thing)
286 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
f7542b92
SM
287 "Alist associating selections with variables.
288Each element is of the form:
1a2b6c52 289
f2506826 290 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
1a2b6c52 291
f2506826
RS
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.")
8a946354 296
ad3f1e65
SM
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))))
1a2b6c52 304 "Function to call to set selection.
f2506826
RS
305Called with two arguments:
306
307 SELECTION, the name of the selection concerned, and
0a6231f5 308 VALUE, the text to store.
ad3f1e65
SM
309
310This sets the selection as well as the cut buffer for the older applications,
311unless `mouse-sel-default-bindings' is `interprogram-cut-paste'.")
1a2b6c52
RS
312
313(defvar mouse-sel-get-selection-function
ad3f1e65
SM
314 (lambda (selection)
315 (if (eq selection 'PRIMARY)
497cfacf
JPW
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))
ad3f1e65 319 (x-get-selection selection)))
1a2b6c52 320 "Function to call to get the selection.
f2506826 321Called with one argument:
1a2b6c52 322
f2506826
RS
323 SELECTION: the name of the selection concerned.")
324
f2506826
RS
325;;=== Support/access functions ============================================
326
327(defun mouse-sel-determine-selection-thing (nclicks)
328 "Determine what `thing' `mouse-sel' should operate on.
329The first argument is NCLICKS, is the number of consecutive
330mouse clicks at the same position.
331
332Double-clicking on word constituents selects words.
333Double-clicking on symbol constituents selects symbols.
334Double-clicking on quotes or parentheses selects sexps.
335Double-clicking on whitespace selects whitespace.
336Triple-clicking selects lines.
337Quad-clicking selects paragraphs.
338
339Feel free to re-define this function to support your own desired
340multi-click semantics."
1a2b6c52 341 (let* ((next-char (char-after (point)))
f2506826 342 (char-syntax (if next-char (char-syntax next-char))))
8a946354 343 (if mouse-sel-cycle-clicks
f2506826 344 (setq nclicks (1+ (% (1- nclicks) 4))))
1a2b6c52
RS
345 (cond
346 ((= nclicks 1) nil)
f2506826
RS
347 ((= nclicks 3) 'line)
348 ((>= nclicks 4) 'paragraph)
1a2b6c52 349 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
69aac59d 350 ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
1a2b6c52
RS
351 ((eq char-syntax ?_) 'symbol)
352 ((eq char-syntax ?w) 'word))))
353
f2506826
RS
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)))
359
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)))
365
f2506826
RS
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)))
371
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))
377
378(defun mouse-sel-region-to-primary (orig-window)
379 "Convert region to PRIMARY overlay and deactivate region.
8a946354
SS
380Argument ORIG-WINDOW specifies the window the cursor was in when the
381originating command was issued, and is used to determine whether the
f2506826
RS
382region was visible or not."
383 (if transient-mark-mode
384 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
385 (cond
8a946354
SS
386 ((and mark-active
387 (or highlight-nonselected-windows
f2506826
RS
388 (eq orig-window (selected-window))))
389 ;; Region was visible, so convert region to overlay
8a946354 390 (move-overlay overlay (region-beginning) (region-end)
f2506826
RS
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)))
395 (t
396 ;; Nothing was visible, so remove overlay
397 (delete-overlay overlay)))
398 (setq mark-active nil))))
399
400(defun mouse-sel-primary-to-region (&optional direction)
401 "Convert PRIMARY overlay to region.
402Optional argument DIRECTION specifies the mouse drag direction: a value of
4031 indicates that the mouse was dragged left-to-right, otherwise it was
404dragged 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))))
419
420(defmacro mouse-sel-eval-at-event-end (event &rest forms)
421 "Evaluate forms at mouse position.
422Move to the end position of EVENT, execute FORMS, and restore original
423point and window."
8a946354
SS
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)))))
f2506826
RS
440
441(put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
442
443;;=== Select ==============================================================
444
445(defun mouse-select (event)
1a2b6c52
RS
446 "Set region/selection using the mouse.
447
f2506826 448Click sets point & mark to click position.
1a2b6c52
RS
449Dragging extends region/selection.
450
8a946354 451Multi-clicking selects word/lines/paragraphs, as determined by
f2506826 452'mouse-sel-determine-selection-thing.
1a2b6c52 453
f2506826
RS
454Clicking mouse-2 while selecting copies selected text to the kill-ring.
455Clicking mouse-1 or mouse-3 kills the selected text.
1a2b6c52
RS
456
457This should be bound to a down-mouse event."
f2506826
RS
458 (interactive "@e")
459 (let (direction)
460 (unwind-protect
461 (setq direction (mouse-select-internal 'PRIMARY event))
462 (mouse-sel-primary-to-region direction))))
463
464(defun mouse-select-secondary (event)
ad3f1e65 465 "Set secondary selection using the mouse.
f2506826
RS
466
467Click sets the start of the secondary selection to click position.
468Dragging extends the secondary selection.
1a2b6c52 469
8a946354 470Multi-clicking selects word/lines/paragraphs, as determined by
f2506826 471'mouse-sel-determine-selection-thing.
1a2b6c52 472
f2506826
RS
473Clicking mouse-2 while selecting copies selected text to the kill-ring.
474Clicking mouse-1 or mouse-3 kills the selected text.
1a2b6c52
RS
475
476This should be bound to a down-mouse event."
ad3f1e65 477 (interactive "e")
f2506826
RS
478 (mouse-select-internal 'SECONDARY event))
479
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)))
496
497;;=== Extend ==============================================================
498
499(defun mouse-extend (event)
500 "Extend region/selection using the mouse."
1a2b6c52 501 (interactive "e")
f2506826
RS
502 (let ((orig-window (selected-window))
503 direction)
504 (select-window (posn-window (event-end event)))
1a2b6c52 505 (unwind-protect
f2506826
RS
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))))
510
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)))
516
517(defun mouse-extend-internal (selection &optional initial-event)
518 "Extend specified SELECTION using the mouse.
519Track mouse-motion events, adjusting the SELECTION appropriately.
8a946354
SS
520Optional argument INITIAL-EVENT specifies an initial down-mouse event to
521process.
f2506826
RS
522
523See documentation for mouse-select-internal for more details."
524 (mouse-sel-eval-at-event-end initial-event
8a946354 525 (let ((orig-cursor-type
f2506826
RS
526 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
527 (unwind-protect
528
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)
540
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))
8a946354 548
f2506826
RS
549
550 ;; Bar cursor
551 (if (fboundp 'modify-frame-parameters)
552 (modify-frame-parameters (selected-frame)
553 '((cursor-type . bar))))
8a946354 554
f2506826
RS
555 ;; Handle dragging
556 (track-mouse
8a946354 557
f2506826
RS
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))))
8a946354 565
f2506826
RS
566 (let ((selection-thing (symbol-value thing-symbol))
567 (end (event-end event)))
8a946354 568
f2506826 569 (cond
8a946354 570
f2506826
RS
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)
8a946354 579
f2506826
RS
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))
590 )))
8a946354 591
f2506826
RS
592 ;; On the mode line
593 ((eq (posn-point end) 'mode-line)
594 (mouse-scroll-subr orig-window 1 overlay min))
8a946354 595
f2506826
RS
596 ;; In original window
597 (t (goto-char (posn-point end)))
8a946354 598
f2506826 599 )
8a946354 600
f2506826
RS
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)))
8a946354 609
f2506826 610 (if (not selection-thing) nil
8a946354 611
f2506826
RS
612 ;; If dragging forward, goal is next character
613 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
8a946354 614
f2506826 615 ;; Move to start/end of selected thing
67384793 616 (let ((goal (point)))
f2506826
RS
617 (goto-char (if (eq 1 direction) min max))
618 (condition-case nil
619 (progn
620 (while (> (* direction (- goal (point))) 0)
f2506826
RS
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)
67384793 626 end (point)))))
f2506826 627 (error))))
8a946354 628
f2506826
RS
629 ;; Move overlay
630 (move-overlay overlay
631 (if (eq 1 direction) min (point))
632 (if (eq -1 direction) max (point))
633 (current-buffer))
8a946354 634
f2506826
RS
635 ))) ; end track-mouse
636
637 ;; Finish up after dragging
638 (let ((overlay-start (overlay-start overlay))
639 (overlay-end (overlay-end overlay)))
8a946354 640
f2506826
RS
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)))
8a946354 646
f2506826
RS
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)))))
663
664 direction)
665
666 ;; Restore cursor
667 (if (fboundp 'modify-frame-parameters)
8a946354 668 (modify-frame-parameters
f2506826 669 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
8a946354 670
f2506826
RS
671 ))))
672
673;;=== Paste ===============================================================
674
640201f8 675(defun mouse-insert-selection (event arg)
f2506826
RS
676 "Insert the contents of the PRIMARY selection at mouse click.
677If `mouse-yank-at-point' is non-nil, insert at point instead."
640201f8
SM
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)))
f2506826
RS
682
683(defun mouse-insert-secondary (event)
684 "Insert the contents of the SECONDARY selection at mouse click.
6b4dd332 685If `mouse-yank-at-point' is non-nil, insert at point instead."
1a2b6c52 686 (interactive "e")
f2506826
RS
687 (mouse-insert-selection-internal 'SECONDARY event))
688
689(defun mouse-insert-selection-internal (selection event)
690 "Insert the contents of the named SELECTION at mouse click.
691If `mouse-yank-at-point' is non-nil, insert at point instead."
8a946354 692 (unless mouse-yank-at-point
ad3f1e65
SM
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) ""))))
f2506826 697
67384793
RS
698;;=== Handle loss of selections ===========================================
699
700(defun mouse-sel-lost-selection-hook (selection)
701 "Remove the overlay for a lost selection."
7b990caf 702 (let ((overlay (mouse-sel-selection-overlay selection)))
67384793
RS
703 (delete-overlay overlay)))
704
ad3f1e65 705(provide 'mouse-sel)
f2506826 706
ab5796a9 707;;; arch-tag: 86e6c73f-deaa-48d3-a24e-c565fda1f7d7
e8af40ee 708;;; mouse-sel.el ends here