(vc-do-command): Autoload.
[bpt/emacs.git] / lisp / mouse-sel.el
CommitLineData
e8af40ee 1;;; mouse-sel.el --- multi-click selection support for Emacs 19
1a2b6c52 2
f2506826 3;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
1a2b6c52
RS
4
5;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
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.
6a2e3631
RS
53;;
54;; * Double-clicking mouse-3 also kills selection.
f2506826
RS
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.
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)
74;;
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)
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
93;;
94;; Mouse sets selection, and pastes from kill-ring
f2506826
RS
95;; mouse-1 mouse-select
96;; mouse-2 mouse-yank-at-click
97;; mouse-3 mouse-extend
1a2b6c52
RS
98;;
99;; Selection/kill-ring interaction is retained
100;; interprogram-cut-function = x-select-text
101;; interprogram-paste-function = x-cut-buffer-or-selection-value
102;;
103;; What you lose is the ability to select some text in
104;; delete-selection-mode and yank over the top of it.
105;;
106;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
107;;
cd1f32a6
RS
108;; * By default, mouse-insert-selection (mouse-2) inserts the selection at
109;; the mouse position. You can tell it to insert at point instead with:
110;;
6b4dd332 111;; (setq mouse-yank-at-point t)
cd1f32a6 112;;
1a2b6c52
RS
113;; * I like to leave point at the end of the region nearest to where the
114;; mouse was, even though this makes region highlighting mis-leading (the
115;; cursor makes it look like one extra character is selected). You can
116;; disable this behaviour with:
117;;
118;; (setq mouse-sel-leave-point-near-mouse nil)
119;;
f2506826
RS
120;; * By default, mouse-select cycles the click count after 4 clicks. That
121;; is, clicking mouse-1 five times has the same effect as clicking it
122;; once, clicking six times has the same effect as clicking twice, etc.
1a2b6c52
RS
123;; Disable this behaviour with:
124;;
125;; (setq mouse-sel-cycle-clicks nil)
126;;
f2506826
RS
127;; * The variables mouse-sel-{set,get}-selection-function control how the
128;; selection is handled. Under X Windows, these variables default so
1a2b6c52
RS
129;; that the X primary selection is used. Under other windowing systems,
130;; alternate functions are used, which simply store the selection value
131;; in a variable.
132;;
1a2b6c52
RS
133;; * You can change the selection highlight face by altering the properties
134;; of mouse-drag-overlay, eg.
135;;
136;; (overlay-put mouse-drag-overlay 'face 'bold)
1a2b6c52 137
b578f267 138;;; Code:
1a2b6c52 139
1a2b6c52
RS
140(require 'mouse)
141(require 'thingatpt)
1a2b6c52 142
ad3f1e65
SM
143(eval-when-compile
144 (require 'cl))
145
1a2b6c52
RS
146;;=== User Variables ======================================================
147
ad3f1e65
SM
148(defgroup mouse-sel nil
149 "Mouse selection enhancement."
150 :group 'mouse)
151
152(defcustom mouse-sel-mode nil
153 "Toggle Mouse Sel mode.
154When Mouse Sel mode is enabled, mouse selection is enhanced in various ways.
8213ac4b
DL
155Setting this variable directly does not take effect;
156use either \\[customize] or the function `mouse-sel-mode'."
ad3f1e65
SM
157 :set (lambda (symbol value)
158 (mouse-sel-mode (or value 0)))
159 :initialize 'custom-initialize-default
160 :type 'boolean
161 :group 'mouse-sel
162 :require 'mouse-sel)
163
164(defcustom mouse-sel-leave-point-near-mouse t
1a2b6c52 165 "*Leave point near last mouse position.
f2506826 166If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
1a2b6c52 167of the region nearest to where the mouse last was.
ad3f1e65
SM
168If nil, point will always be placed at the beginning of the region."
169 :type 'boolean
170 :group 'mouse-sel)
171
172(defcustom mouse-sel-cycle-clicks t
173 "*If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks."
174 :type 'boolean
175 :group 'mouse-sel)
176
177(defcustom mouse-sel-default-bindings t
178 "*Control mouse bindings."
179 :type '(choice (const :tag "none" nil)
21cabb0f
AS
180 (const :tag "cut and paste" interprogram-cut-paste)
181 (other :tag "default bindings" t))
ad3f1e65
SM
182 :group 'mouse-sel)
183
184;;=== User Command ========================================================
185
186;;;###autoload
187(defun mouse-sel-mode (&optional arg)
188 "Toggle Mouse Sel mode.
189With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive.
190Returns the new status of Mouse Sel mode (non-nil means on).
191
192When Mouse Sel mode is enabled, mouse selection is enhanced in various ways:
193
194- Clicking mouse-1 starts (cancels) selection, dragging extends it.
195
196- Clicking or dragging mouse-3 extends the selection as well.
197
198- Double-clicking on word constituents selects words.
199Double-clicking on symbol constituents selects symbols.
200Double-clicking on quotes or parentheses selects sexps.
201Double-clicking on whitespace selects whitespace.
202Triple-clicking selects lines.
203Quad-clicking selects paragraphs.
204
205- Selecting sets the region & X primary selection, but does NOT affect
9eebe311
GM
206the kill-ring, nor do the kill-ring function change the X selection.
207Because the mouse handlers set the primary selection directly,
208mouse-sel sets the variables interprogram-cut-function and
209interprogram-paste-function to nil.
1a2b6c52 210
ad3f1e65
SM
211- Clicking mouse-2 inserts the contents of the primary selection at
212the mouse position (or point, if mouse-yank-at-point is non-nil).
1a2b6c52 213
ad3f1e65
SM
214- Pressing mouse-2 while selecting or extending copies selection
215to the kill ring. Pressing mouse-1 or mouse-3 kills it.
216
217- Double-clicking mouse-3 also kills selection.
218
219- M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
220& mouse-3, but operate on the X secondary selection rather than the
221primary selection and region."
222 (interactive "P")
223 (let ((on-p (if arg
224 (> (prefix-numeric-value arg) 0)
225 (not mouse-sel-mode))))
226 (if on-p
227 (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
228 (remove-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook))
229 (mouse-sel-bindings on-p)
230 (setq mouse-sel-mode on-p)))
231
232;;=== Key bindings ========================================================
233
234(defun mouse-sel-bindings (bind)
235 (cond ((not bind)
236 ;; These bindings are taken from mouse.el, i.e., they are the default
237 ;; bindings. It would be better to restore the previous bindings.
238 ;; Primary selection bindings.
239 (global-set-key [mouse-1] 'mouse-set-point)
240 (global-set-key [mouse-2] 'mouse-yank-at-click)
241 (global-set-key [mouse-3] 'mouse-save-then-kill)
242 (global-set-key [down-mouse-1] 'mouse-drag-region)
243 (global-set-key [drag-mouse-1] 'mouse-set-region)
244 (global-set-key [double-mouse-1] 'mouse-set-point)
245 (global-set-key [triple-mouse-1] 'mouse-set-point)
246 ;; Secondary selection bindings.
247 (global-set-key [M-mouse-1] 'mouse-start-secondary)
248 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
249 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
250 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
251 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary))
252 (mouse-sel-default-bindings
253 ;;
254 ;; Primary selection bindings.
255 (global-unset-key [mouse-1])
256 (global-unset-key [drag-mouse-1])
257 (global-unset-key [mouse-3])
258 (global-set-key [down-mouse-1] 'mouse-select)
259 (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
260 (global-set-key [mouse-2] 'mouse-insert-selection)
261 (setq interprogram-cut-function nil
262 interprogram-paste-function nil))
263 (global-set-key [down-mouse-3] 'mouse-extend)
264 ;;
265 ;; Secondary selection bindings.
266 (global-unset-key [M-mouse-1])
267 (global-unset-key [M-drag-mouse-1])
268 (global-unset-key [M-mouse-3])
269 (global-set-key [M-down-mouse-1] 'mouse-select-secondary)
270 (global-set-key [M-mouse-2] 'mouse-insert-secondary)
271 (global-set-key [M-down-mouse-3] 'mouse-extend-secondary))))
272
273;;=== Command Variable ====================================================
274
275;; This has to come after the function `mouse-sel-mode' and its callee.
276;; An alternative is to put the option `mouse-sel-mode' here and remove its
277;; `:initialize' keyword.
278(when mouse-sel-mode
279 (mouse-sel-mode t))
1a2b6c52 280
f2506826
RS
281;;=== Internal Variables/Constants ========================================
282
283(defvar mouse-sel-primary-thing nil
284 "Type of PRIMARY selection in current buffer.")
285(make-variable-buffer-local 'mouse-sel-primary-thing)
286
287(defvar mouse-sel-secondary-thing nil
288 "Type of SECONDARY selection in current buffer.")
289(make-variable-buffer-local 'mouse-sel-secondary-thing)
290
291;; Ensure that secondary overlay is defined
ad3f1e65 292(unless (overlayp mouse-secondary-overlay)
f2506826
RS
293 (setq mouse-secondary-overlay (make-overlay 1 1))
294 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
1a2b6c52 295
f2506826
RS
296(defconst mouse-sel-selection-alist
297 '((PRIMARY mouse-drag-overlay mouse-sel-primary-thing)
298 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
299 "Alist associating selections with variables. Each element is of
300the form:
1a2b6c52 301
f2506826 302 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
1a2b6c52 303
f2506826
RS
304where SELECTION-NAME = name of selection
305 OVERLAY-SYMBOL = name of variable containing overlay to use
306 SELECTION-THING-SYMBOL = name of variable where the current selection
307 type for this selection should be stored.")
308
ad3f1e65
SM
309(defvar mouse-sel-set-selection-function
310 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
311 'x-set-selection
312 (lambda (selection value)
313 (if (eq selection 'PRIMARY)
314 (x-select-text value)
315 (x-set-selection selection value))))
1a2b6c52 316 "Function to call to set selection.
f2506826
RS
317Called with two arguments:
318
319 SELECTION, the name of the selection concerned, and
0a6231f5 320 VALUE, the text to store.
ad3f1e65
SM
321
322This sets the selection as well as the cut buffer for the older applications,
323unless `mouse-sel-default-bindings' is `interprogram-cut-paste'.")
1a2b6c52
RS
324
325(defvar mouse-sel-get-selection-function
ad3f1e65
SM
326 (lambda (selection)
327 (if (eq selection 'PRIMARY)
328 (or (x-cut-buffer-or-selection-value) x-last-selected-text)
329 (x-get-selection selection)))
1a2b6c52 330 "Function to call to get the selection.
f2506826 331Called with one argument:
1a2b6c52 332
f2506826
RS
333 SELECTION: the name of the selection concerned.")
334
f2506826
RS
335;;=== Support/access functions ============================================
336
337(defun mouse-sel-determine-selection-thing (nclicks)
338 "Determine what `thing' `mouse-sel' should operate on.
339The first argument is NCLICKS, is the number of consecutive
340mouse clicks at the same position.
341
342Double-clicking on word constituents selects words.
343Double-clicking on symbol constituents selects symbols.
344Double-clicking on quotes or parentheses selects sexps.
345Double-clicking on whitespace selects whitespace.
346Triple-clicking selects lines.
347Quad-clicking selects paragraphs.
348
349Feel free to re-define this function to support your own desired
350multi-click semantics."
1a2b6c52 351 (let* ((next-char (char-after (point)))
f2506826
RS
352 (char-syntax (if next-char (char-syntax next-char))))
353 (if mouse-sel-cycle-clicks
354 (setq nclicks (1+ (% (1- nclicks) 4))))
1a2b6c52
RS
355 (cond
356 ((= nclicks 1) nil)
f2506826
RS
357 ((= nclicks 3) 'line)
358 ((>= nclicks 4) 'paragraph)
1a2b6c52
RS
359 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
360 ((memq next-char '(? ?\t ?\n)) 'whitespace)
361 ((eq char-syntax ?_) 'symbol)
362 ((eq char-syntax ?w) 'word))))
363
f2506826
RS
364(defun mouse-sel-set-selection (selection value)
365 "Set the specified SELECTION to VALUE."
366 (if mouse-sel-set-selection-function
367 (funcall mouse-sel-set-selection-function selection value)
368 (put 'mouse-sel-internal-selection selection value)))
369
370(defun mouse-sel-get-selection (selection)
371 "Get the value of the specified SELECTION."
372 (if mouse-sel-get-selection-function
373 (funcall mouse-sel-get-selection-function selection)
374 (get 'mouse-sel-internal-selection selection)))
375
f2506826
RS
376(defun mouse-sel-selection-overlay (selection)
377 "Return overlay corresponding to SELECTION."
378 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
379 (or symbol (error "No overlay corresponding to %s selection" selection))
380 (symbol-value symbol)))
381
382(defun mouse-sel-selection-thing (selection)
383 "Return overlay corresponding to SELECTION."
384 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
385 (or symbol (error "No symbol corresponding to %s selection" selection))
386 symbol))
387
388(defun mouse-sel-region-to-primary (orig-window)
389 "Convert region to PRIMARY overlay and deactivate region.
390Argument ORIG-WINDOW specifies the window the cursor was in when the
391originating command was issued, and is used to determine whether the
392region was visible or not."
393 (if transient-mark-mode
394 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
395 (cond
396 ((and mark-active
397 (or highlight-nonselected-windows
398 (eq orig-window (selected-window))))
399 ;; Region was visible, so convert region to overlay
400 (move-overlay overlay (region-beginning) (region-end)
401 (current-buffer)))
402 ((eq orig-window (selected-window))
403 ;; Point was visible, so set overlay at point
404 (move-overlay overlay (point) (point) (current-buffer)))
405 (t
406 ;; Nothing was visible, so remove overlay
407 (delete-overlay overlay)))
408 (setq mark-active nil))))
409
410(defun mouse-sel-primary-to-region (&optional direction)
411 "Convert PRIMARY overlay to region.
412Optional argument DIRECTION specifies the mouse drag direction: a value of
4131 indicates that the mouse was dragged left-to-right, otherwise it was
414dragged right-to-left."
415 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
416 (start (overlay-start overlay))
417 (end (overlay-end overlay)))
418 (if (eq start end)
419 (progn
420 (if start (goto-char start))
421 (deactivate-mark))
422 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
423 (progn
424 (goto-char end)
425 (push-mark start 'nomsg 'active))
426 (goto-char start)
427 (push-mark end 'nomsg 'active)))
428 (if transient-mark-mode (delete-overlay overlay))))
429
430(defmacro mouse-sel-eval-at-event-end (event &rest forms)
431 "Evaluate forms at mouse position.
432Move to the end position of EVENT, execute FORMS, and restore original
433point and window."
434 (`
435 (let ((posn (event-end (, event))))
436 (if posn (mouse-minibuffer-check (, event)))
437 (if (and posn (not (windowp (posn-window posn))))
438 (error "Cursor not in text area of window"))
439 (let (orig-window orig-point-marker)
440 (setq orig-window (selected-window))
441 (if posn (select-window (posn-window posn)))
442 (setq orig-point-marker (point-marker))
443 (if (and posn (numberp (posn-point posn)))
444 (goto-char (posn-point posn)))
445 (unwind-protect
446 (progn
447 (,@ forms))
448 (goto-char (marker-position orig-point-marker))
449 (move-marker orig-point-marker nil)
450 (select-window orig-window)
451 )))))
452
453(put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
454
455;;=== Select ==============================================================
456
457(defun mouse-select (event)
1a2b6c52
RS
458 "Set region/selection using the mouse.
459
f2506826 460Click sets point & mark to click position.
1a2b6c52
RS
461Dragging extends region/selection.
462
f2506826
RS
463Multi-clicking selects word/lines/paragraphs, as determined by
464'mouse-sel-determine-selection-thing.
1a2b6c52 465
f2506826
RS
466Clicking mouse-2 while selecting copies selected text to the kill-ring.
467Clicking mouse-1 or mouse-3 kills the selected text.
1a2b6c52
RS
468
469This should be bound to a down-mouse event."
f2506826
RS
470 (interactive "@e")
471 (let (direction)
472 (unwind-protect
473 (setq direction (mouse-select-internal 'PRIMARY event))
474 (mouse-sel-primary-to-region direction))))
475
476(defun mouse-select-secondary (event)
ad3f1e65 477 "Set secondary selection using the mouse.
f2506826
RS
478
479Click sets the start of the secondary selection to click position.
480Dragging extends the secondary selection.
1a2b6c52 481
f2506826
RS
482Multi-clicking selects word/lines/paragraphs, as determined by
483'mouse-sel-determine-selection-thing.
1a2b6c52 484
f2506826
RS
485Clicking mouse-2 while selecting copies selected text to the kill-ring.
486Clicking mouse-1 or mouse-3 kills the selected text.
1a2b6c52
RS
487
488This should be bound to a down-mouse event."
ad3f1e65 489 (interactive "e")
f2506826
RS
490 (mouse-select-internal 'SECONDARY event))
491
492(defun mouse-select-internal (selection event)
493 "Set SELECTION using the mouse."
494 (mouse-sel-eval-at-event-end event
495 (let ((thing-symbol (mouse-sel-selection-thing selection))
496 (overlay (mouse-sel-selection-overlay selection)))
497 (set thing-symbol
498 (mouse-sel-determine-selection-thing (event-click-count event)))
499 (let ((object-bounds (bounds-of-thing-at-point
500 (symbol-value thing-symbol))))
501 (if object-bounds
502 (progn
503 (move-overlay overlay
504 (car object-bounds) (cdr object-bounds)
505 (current-buffer)))
506 (move-overlay overlay (point) (point) (current-buffer)))))
507 (mouse-extend-internal selection)))
508
509;;=== Extend ==============================================================
510
511(defun mouse-extend (event)
512 "Extend region/selection using the mouse."
1a2b6c52 513 (interactive "e")
f2506826
RS
514 (let ((orig-window (selected-window))
515 direction)
516 (select-window (posn-window (event-end event)))
1a2b6c52 517 (unwind-protect
f2506826
RS
518 (progn
519 (mouse-sel-region-to-primary orig-window)
520 (setq direction (mouse-extend-internal 'PRIMARY event)))
521 (mouse-sel-primary-to-region direction))))
522
523(defun mouse-extend-secondary (event)
524 "Extend secondary selection using the mouse."
525 (interactive "e")
526 (save-window-excursion
527 (mouse-extend-internal 'SECONDARY event)))
528
529(defun mouse-extend-internal (selection &optional initial-event)
530 "Extend specified SELECTION using the mouse.
531Track mouse-motion events, adjusting the SELECTION appropriately.
532Optional argument INITIAL-EVENT specifies an initial down-mouse event to
533process.
534
535See documentation for mouse-select-internal for more details."
536 (mouse-sel-eval-at-event-end initial-event
537 (let ((orig-cursor-type
538 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
539 (unwind-protect
540
541 (let* ((thing-symbol (mouse-sel-selection-thing selection))
542 (overlay (mouse-sel-selection-overlay selection))
543 (orig-window (selected-window))
544 (orig-window-frame (window-frame orig-window))
545 (top (nth 1 (window-edges orig-window)))
546 (bottom (nth 3 (window-edges orig-window)))
547 (mark-active nil) ; inhibit normal region highlight
548 (echo-keystrokes 0) ; don't echo mouse events
549 min max
550 direction
551 event)
552
553 ;; Get current bounds of overlay
554 (if (eq (overlay-buffer overlay) (current-buffer))
555 (setq min (overlay-start overlay)
556 max (overlay-end overlay))
557 (setq min (point)
558 max min)
559 (set thing-symbol nil))
1a2b6c52 560
f2506826
RS
561
562 ;; Bar cursor
563 (if (fboundp 'modify-frame-parameters)
564 (modify-frame-parameters (selected-frame)
565 '((cursor-type . bar))))
566
567 ;; Handle dragging
568 (track-mouse
569
570 (while (if initial-event ; Use initial event
571 (prog1
572 (setq event initial-event)
573 (setq initial-event nil))
574 (setq event (read-event))
575 (and (consp event)
576 (memq (car event) '(mouse-movement switch-frame))))
577
578 (let ((selection-thing (symbol-value thing-symbol))
579 (end (event-end event)))
580
581 (cond
1a2b6c52 582
f2506826
RS
583 ;; Ignore any movement outside the frame
584 ((eq (car-safe event) 'switch-frame) nil)
585 ((and (posn-window end)
586 (not (eq (let ((posn-w (posn-window end)))
587 (if (windowp posn-w)
588 (window-frame posn-w)
589 posn-w))
590 (window-frame orig-window)))) nil)
1a2b6c52 591
f2506826
RS
592 ;; Different window, same frame
593 ((not (eq (posn-window end) orig-window))
594 (let ((end-row (cdr (cdr (mouse-position)))))
595 (cond
596 ((and end-row (not (bobp)) (< end-row top))
597 (mouse-scroll-subr orig-window (- end-row top)
598 overlay max))
599 ((and end-row (not (eobp)) (>= end-row bottom))
600 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
601 overlay min))
602 )))
603
604 ;; On the mode line
605 ((eq (posn-point end) 'mode-line)
606 (mouse-scroll-subr orig-window 1 overlay min))
607
608 ;; In original window
609 (t (goto-char (posn-point end)))
610
611 )
1a2b6c52 612
f2506826
RS
613 ;; Determine direction of drag
614 (cond
615 ((and (not direction) (not (eq min max)))
616 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
617 ((and (not (eq direction -1)) (<= (point) min))
618 (setq direction -1))
619 ((and (not (eq direction 1)) (>= (point) max))
620 (setq direction 1)))
1a2b6c52 621
f2506826
RS
622 (if (not selection-thing) nil
623
624 ;; If dragging forward, goal is next character
625 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
626
627 ;; Move to start/end of selected thing
67384793 628 (let ((goal (point)))
f2506826
RS
629 (goto-char (if (eq 1 direction) min max))
630 (condition-case nil
631 (progn
632 (while (> (* direction (- goal (point))) 0)
f2506826
RS
633 (forward-thing selection-thing direction))
634 (let ((end (point)))
635 (forward-thing selection-thing (- direction))
636 (goto-char
637 (if (> (* direction (- goal (point))) 0)
67384793 638 end (point)))))
f2506826
RS
639 (error))))
640
641 ;; Move overlay
642 (move-overlay overlay
643 (if (eq 1 direction) min (point))
644 (if (eq -1 direction) max (point))
645 (current-buffer))
646
647 ))) ; end track-mouse
648
649 ;; Finish up after dragging
650 (let ((overlay-start (overlay-start overlay))
651 (overlay-end (overlay-end overlay)))
1a2b6c52 652
f2506826
RS
653 ;; Set selection
654 (if (not (eq overlay-start overlay-end))
655 (mouse-sel-set-selection
656 selection
657 (buffer-substring overlay-start overlay-end)))
1a2b6c52 658
f2506826
RS
659 ;; Handle copy/kill
660 (let (this-command)
661 (cond
662 ((eq (event-basic-type last-input-event) 'mouse-2)
663 (copy-region-as-kill overlay-start overlay-end)
664 (read-event) (read-event))
665 ((and (memq (event-basic-type last-input-event)
666 '(mouse-1 mouse-3))
667 (memq 'down (event-modifiers last-input-event)))
668 (kill-region overlay-start overlay-end)
669 (move-overlay overlay overlay-start overlay-start)
670 (read-event) (read-event))
671 ((and (eq (event-basic-type last-input-event) 'mouse-3)
672 (memq 'double (event-modifiers last-input-event)))
673 (kill-region overlay-start overlay-end)
674 (move-overlay overlay overlay-start overlay-start)))))
675
676 direction)
677
678 ;; Restore cursor
679 (if (fboundp 'modify-frame-parameters)
680 (modify-frame-parameters
681 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
682
683 ))))
684
685;;=== Paste ===============================================================
686
687(defun mouse-insert-selection (event)
688 "Insert the contents of the PRIMARY selection at mouse click.
689If `mouse-yank-at-point' is non-nil, insert at point instead."
690 (interactive "e")
691 (mouse-insert-selection-internal 'PRIMARY event))
692
693(defun mouse-insert-secondary (event)
694 "Insert the contents of the SECONDARY selection at mouse click.
6b4dd332 695If `mouse-yank-at-point' is non-nil, insert at point instead."
1a2b6c52 696 (interactive "e")
f2506826
RS
697 (mouse-insert-selection-internal 'SECONDARY event))
698
699(defun mouse-insert-selection-internal (selection event)
700 "Insert the contents of the named SELECTION at mouse click.
701If `mouse-yank-at-point' is non-nil, insert at point instead."
ad3f1e65
SM
702 (unless mouse-yank-at-point
703 (mouse-set-point event))
704 (when mouse-sel-get-selection-function
705 (push-mark (point) 'nomsg)
706 (insert (or (funcall mouse-sel-get-selection-function selection) ""))))
f2506826 707
67384793
RS
708;;=== Handle loss of selections ===========================================
709
710(defun mouse-sel-lost-selection-hook (selection)
711 "Remove the overlay for a lost selection."
7b990caf 712 (let ((overlay (mouse-sel-selection-overlay selection)))
67384793
RS
713 (delete-overlay overlay)))
714
f2506826
RS
715;;=== Bug reporting =======================================================
716
ad3f1e65
SM
717;(defconst mouse-sel-maintainer-address "mikew@gopher.dosli.govt.nz")
718
719;(defun mouse-sel-submit-bug-report ()
720; "Submit a bug report on mouse-sel.el via mail."
721; (interactive)
722; (require 'reporter)
723; (reporter-submit-bug-report
724; mouse-sel-maintainer-address
725; (concat "mouse-sel.el "
726; (or (condition-case nil mouse-sel-version (error))
727; "(distributed with Emacs)"))
728; (list 'transient-mark-mode
729; 'delete-selection-mode
730; 'mouse-sel-default-bindings
731; 'mouse-sel-leave-point-near-mouse
732; 'mouse-sel-cycle-clicks
733; 'mouse-sel-selection-alist
734; 'mouse-sel-set-selection-function
735; 'mouse-sel-get-selection-function
736; 'mouse-yank-at-point)))
737
738(provide 'mouse-sel)
f2506826 739
e8af40ee 740;;; mouse-sel.el ends here