(isearch-char-to-string): Use char-to-string.
[bpt/emacs.git] / lisp / mouse-sel.el
CommitLineData
1a2b6c52
RS
1;;; mouse-sel.el --- Multi-click selection support for Emacs 19
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
RS
139
140(provide 'mouse-sel)
141
142(require 'mouse)
143(require 'thingatpt)
1a2b6c52
RS
144
145;;=== User Variables ======================================================
146
147(defvar mouse-sel-leave-point-near-mouse t
148 "*Leave point near last mouse position.
f2506826 149If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
1a2b6c52 150of the region nearest to where the mouse last was.
f2506826 151If nil, point will always be placed at the beginning of the region.")
1a2b6c52
RS
152
153(defvar mouse-sel-cycle-clicks t
f2506826 154 "*If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks.")
1a2b6c52
RS
155
156(defvar mouse-sel-default-bindings t
cd1f32a6 157 "Set to nil before loading `mouse-sel' to prevent default mouse bindings.")
1a2b6c52 158
f2506826
RS
159;;=== Internal Variables/Constants ========================================
160
161(defvar mouse-sel-primary-thing nil
162 "Type of PRIMARY selection in current buffer.")
163(make-variable-buffer-local 'mouse-sel-primary-thing)
164
165(defvar mouse-sel-secondary-thing nil
166 "Type of SECONDARY selection in current buffer.")
167(make-variable-buffer-local 'mouse-sel-secondary-thing)
168
169;; Ensure that secondary overlay is defined
170(if (overlayp mouse-secondary-overlay) nil
171 (setq mouse-secondary-overlay (make-overlay 1 1))
172 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
1a2b6c52 173
f2506826
RS
174(defconst mouse-sel-selection-alist
175 '((PRIMARY mouse-drag-overlay mouse-sel-primary-thing)
176 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
177 "Alist associating selections with variables. Each element is of
178the form:
1a2b6c52 179
f2506826 180 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
1a2b6c52 181
f2506826
RS
182where SELECTION-NAME = name of selection
183 OVERLAY-SYMBOL = name of variable containing overlay to use
184 SELECTION-THING-SYMBOL = name of variable where the current selection
185 type for this selection should be stored.")
186
1a2b6c52 187(defvar mouse-sel-set-selection-function
0a6231f5
RS
188 (function (lambda (selection value)
189 (if (eq selection 'PRIMARY)
190 (x-select-text value)
191 (x-set-selection selection value))))
1a2b6c52 192 "Function to call to set selection.
f2506826
RS
193Called with two arguments:
194
195 SELECTION, the name of the selection concerned, and
0a6231f5
RS
196 VALUE, the text to store.
197This sets the selection as well as the cut buffer for the older applications.
198Use (setq mouse-sel-set-selection-function 'x-set-selection) if you don't care
199for them.")
1a2b6c52
RS
200
201(defvar mouse-sel-get-selection-function
0a6231f5
RS
202 (function (lambda (selection)
203 (if (eq selection 'PRIMARY)
a416bfa8 204 (or (x-cut-buffer-or-selection-value) x-last-selected-text)
0a6231f5 205 (x-get-selection selection))))
1a2b6c52 206 "Function to call to get the selection.
f2506826 207Called with one argument:
1a2b6c52 208
f2506826
RS
209 SELECTION: the name of the selection concerned.")
210
f2506826
RS
211;;=== Support/access functions ============================================
212
213(defun mouse-sel-determine-selection-thing (nclicks)
214 "Determine what `thing' `mouse-sel' should operate on.
215The first argument is NCLICKS, is the number of consecutive
216mouse clicks at the same position.
217
218Double-clicking on word constituents selects words.
219Double-clicking on symbol constituents selects symbols.
220Double-clicking on quotes or parentheses selects sexps.
221Double-clicking on whitespace selects whitespace.
222Triple-clicking selects lines.
223Quad-clicking selects paragraphs.
224
225Feel free to re-define this function to support your own desired
226multi-click semantics."
1a2b6c52 227 (let* ((next-char (char-after (point)))
f2506826
RS
228 (char-syntax (if next-char (char-syntax next-char))))
229 (if mouse-sel-cycle-clicks
230 (setq nclicks (1+ (% (1- nclicks) 4))))
1a2b6c52
RS
231 (cond
232 ((= nclicks 1) nil)
f2506826
RS
233 ((= nclicks 3) 'line)
234 ((>= nclicks 4) 'paragraph)
1a2b6c52
RS
235 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
236 ((memq next-char '(? ?\t ?\n)) 'whitespace)
237 ((eq char-syntax ?_) 'symbol)
238 ((eq char-syntax ?w) 'word))))
239
f2506826
RS
240(defun mouse-sel-set-selection (selection value)
241 "Set the specified SELECTION to VALUE."
242 (if mouse-sel-set-selection-function
243 (funcall mouse-sel-set-selection-function selection value)
244 (put 'mouse-sel-internal-selection selection value)))
245
246(defun mouse-sel-get-selection (selection)
247 "Get the value of the specified SELECTION."
248 (if mouse-sel-get-selection-function
249 (funcall mouse-sel-get-selection-function selection)
250 (get 'mouse-sel-internal-selection selection)))
251
f2506826
RS
252(defun mouse-sel-selection-overlay (selection)
253 "Return overlay corresponding to SELECTION."
254 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
255 (or symbol (error "No overlay corresponding to %s selection" selection))
256 (symbol-value symbol)))
257
258(defun mouse-sel-selection-thing (selection)
259 "Return overlay corresponding to SELECTION."
260 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
261 (or symbol (error "No symbol corresponding to %s selection" selection))
262 symbol))
263
264(defun mouse-sel-region-to-primary (orig-window)
265 "Convert region to PRIMARY overlay and deactivate region.
266Argument ORIG-WINDOW specifies the window the cursor was in when the
267originating command was issued, and is used to determine whether the
268region was visible or not."
269 (if transient-mark-mode
270 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
271 (cond
272 ((and mark-active
273 (or highlight-nonselected-windows
274 (eq orig-window (selected-window))))
275 ;; Region was visible, so convert region to overlay
276 (move-overlay overlay (region-beginning) (region-end)
277 (current-buffer)))
278 ((eq orig-window (selected-window))
279 ;; Point was visible, so set overlay at point
280 (move-overlay overlay (point) (point) (current-buffer)))
281 (t
282 ;; Nothing was visible, so remove overlay
283 (delete-overlay overlay)))
284 (setq mark-active nil))))
285
286(defun mouse-sel-primary-to-region (&optional direction)
287 "Convert PRIMARY overlay to region.
288Optional argument DIRECTION specifies the mouse drag direction: a value of
2891 indicates that the mouse was dragged left-to-right, otherwise it was
290dragged right-to-left."
291 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
292 (start (overlay-start overlay))
293 (end (overlay-end overlay)))
294 (if (eq start end)
295 (progn
296 (if start (goto-char start))
297 (deactivate-mark))
298 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
299 (progn
300 (goto-char end)
301 (push-mark start 'nomsg 'active))
302 (goto-char start)
303 (push-mark end 'nomsg 'active)))
304 (if transient-mark-mode (delete-overlay overlay))))
305
306(defmacro mouse-sel-eval-at-event-end (event &rest forms)
307 "Evaluate forms at mouse position.
308Move to the end position of EVENT, execute FORMS, and restore original
309point and window."
310 (`
311 (let ((posn (event-end (, event))))
312 (if posn (mouse-minibuffer-check (, event)))
313 (if (and posn (not (windowp (posn-window posn))))
314 (error "Cursor not in text area of window"))
315 (let (orig-window orig-point-marker)
316 (setq orig-window (selected-window))
317 (if posn (select-window (posn-window posn)))
318 (setq orig-point-marker (point-marker))
319 (if (and posn (numberp (posn-point posn)))
320 (goto-char (posn-point posn)))
321 (unwind-protect
322 (progn
323 (,@ forms))
324 (goto-char (marker-position orig-point-marker))
325 (move-marker orig-point-marker nil)
326 (select-window orig-window)
327 )))))
328
329(put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
330
331;;=== Select ==============================================================
332
333(defun mouse-select (event)
1a2b6c52
RS
334 "Set region/selection using the mouse.
335
f2506826 336Click sets point & mark to click position.
1a2b6c52
RS
337Dragging extends region/selection.
338
f2506826
RS
339Multi-clicking selects word/lines/paragraphs, as determined by
340'mouse-sel-determine-selection-thing.
1a2b6c52 341
f2506826
RS
342Clicking mouse-2 while selecting copies selected text to the kill-ring.
343Clicking mouse-1 or mouse-3 kills the selected text.
1a2b6c52
RS
344
345This should be bound to a down-mouse event."
f2506826
RS
346 (interactive "@e")
347 (let (direction)
348 (unwind-protect
349 (setq direction (mouse-select-internal 'PRIMARY event))
350 (mouse-sel-primary-to-region direction))))
351
352(defun mouse-select-secondary (event)
353 "Set secondary selection using the mouse.
354
355Click sets the start of the secondary selection to click position.
356Dragging extends the secondary selection.
1a2b6c52 357
f2506826
RS
358Multi-clicking selects word/lines/paragraphs, as determined by
359'mouse-sel-determine-selection-thing.
1a2b6c52 360
f2506826
RS
361Clicking mouse-2 while selecting copies selected text to the kill-ring.
362Clicking mouse-1 or mouse-3 kills the selected text.
1a2b6c52
RS
363
364This should be bound to a down-mouse event."
f2506826
RS
365 (interactive "e")
366 (mouse-select-internal 'SECONDARY event))
367
368(defun mouse-select-internal (selection event)
369 "Set SELECTION using the mouse."
370 (mouse-sel-eval-at-event-end event
371 (let ((thing-symbol (mouse-sel-selection-thing selection))
372 (overlay (mouse-sel-selection-overlay selection)))
373 (set thing-symbol
374 (mouse-sel-determine-selection-thing (event-click-count event)))
375 (let ((object-bounds (bounds-of-thing-at-point
376 (symbol-value thing-symbol))))
377 (if object-bounds
378 (progn
379 (move-overlay overlay
380 (car object-bounds) (cdr object-bounds)
381 (current-buffer)))
382 (move-overlay overlay (point) (point) (current-buffer)))))
383 (mouse-extend-internal selection)))
384
385;;=== Extend ==============================================================
386
387(defun mouse-extend (event)
388 "Extend region/selection using the mouse."
1a2b6c52 389 (interactive "e")
f2506826
RS
390 (let ((orig-window (selected-window))
391 direction)
392 (select-window (posn-window (event-end event)))
1a2b6c52 393 (unwind-protect
f2506826
RS
394 (progn
395 (mouse-sel-region-to-primary orig-window)
396 (setq direction (mouse-extend-internal 'PRIMARY event)))
397 (mouse-sel-primary-to-region direction))))
398
399(defun mouse-extend-secondary (event)
400 "Extend secondary selection using the mouse."
401 (interactive "e")
402 (save-window-excursion
403 (mouse-extend-internal 'SECONDARY event)))
404
405(defun mouse-extend-internal (selection &optional initial-event)
406 "Extend specified SELECTION using the mouse.
407Track mouse-motion events, adjusting the SELECTION appropriately.
408Optional argument INITIAL-EVENT specifies an initial down-mouse event to
409process.
410
411See documentation for mouse-select-internal for more details."
412 (mouse-sel-eval-at-event-end initial-event
413 (let ((orig-cursor-type
414 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
415 (unwind-protect
416
417 (let* ((thing-symbol (mouse-sel-selection-thing selection))
418 (overlay (mouse-sel-selection-overlay selection))
419 (orig-window (selected-window))
420 (orig-window-frame (window-frame orig-window))
421 (top (nth 1 (window-edges orig-window)))
422 (bottom (nth 3 (window-edges orig-window)))
423 (mark-active nil) ; inhibit normal region highlight
424 (echo-keystrokes 0) ; don't echo mouse events
425 min max
426 direction
427 event)
428
429 ;; Get current bounds of overlay
430 (if (eq (overlay-buffer overlay) (current-buffer))
431 (setq min (overlay-start overlay)
432 max (overlay-end overlay))
433 (setq min (point)
434 max min)
435 (set thing-symbol nil))
1a2b6c52 436
f2506826
RS
437
438 ;; Bar cursor
439 (if (fboundp 'modify-frame-parameters)
440 (modify-frame-parameters (selected-frame)
441 '((cursor-type . bar))))
442
443 ;; Handle dragging
444 (track-mouse
445
446 (while (if initial-event ; Use initial event
447 (prog1
448 (setq event initial-event)
449 (setq initial-event nil))
450 (setq event (read-event))
451 (and (consp event)
452 (memq (car event) '(mouse-movement switch-frame))))
453
454 (let ((selection-thing (symbol-value thing-symbol))
455 (end (event-end event)))
456
457 (cond
1a2b6c52 458
f2506826
RS
459 ;; Ignore any movement outside the frame
460 ((eq (car-safe event) 'switch-frame) nil)
461 ((and (posn-window end)
462 (not (eq (let ((posn-w (posn-window end)))
463 (if (windowp posn-w)
464 (window-frame posn-w)
465 posn-w))
466 (window-frame orig-window)))) nil)
1a2b6c52 467
f2506826
RS
468 ;; Different window, same frame
469 ((not (eq (posn-window end) orig-window))
470 (let ((end-row (cdr (cdr (mouse-position)))))
471 (cond
472 ((and end-row (not (bobp)) (< end-row top))
473 (mouse-scroll-subr orig-window (- end-row top)
474 overlay max))
475 ((and end-row (not (eobp)) (>= end-row bottom))
476 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
477 overlay min))
478 )))
479
480 ;; On the mode line
481 ((eq (posn-point end) 'mode-line)
482 (mouse-scroll-subr orig-window 1 overlay min))
483
484 ;; In original window
485 (t (goto-char (posn-point end)))
486
487 )
1a2b6c52 488
f2506826
RS
489 ;; Determine direction of drag
490 (cond
491 ((and (not direction) (not (eq min max)))
492 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
493 ((and (not (eq direction -1)) (<= (point) min))
494 (setq direction -1))
495 ((and (not (eq direction 1)) (>= (point) max))
496 (setq direction 1)))
1a2b6c52 497
f2506826
RS
498 (if (not selection-thing) nil
499
500 ;; If dragging forward, goal is next character
501 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
502
503 ;; Move to start/end of selected thing
67384793 504 (let ((goal (point)))
f2506826
RS
505 (goto-char (if (eq 1 direction) min max))
506 (condition-case nil
507 (progn
508 (while (> (* direction (- goal (point))) 0)
f2506826
RS
509 (forward-thing selection-thing direction))
510 (let ((end (point)))
511 (forward-thing selection-thing (- direction))
512 (goto-char
513 (if (> (* direction (- goal (point))) 0)
67384793 514 end (point)))))
f2506826
RS
515 (error))))
516
517 ;; Move overlay
518 (move-overlay overlay
519 (if (eq 1 direction) min (point))
520 (if (eq -1 direction) max (point))
521 (current-buffer))
522
523 ))) ; end track-mouse
524
525 ;; Finish up after dragging
526 (let ((overlay-start (overlay-start overlay))
527 (overlay-end (overlay-end overlay)))
1a2b6c52 528
f2506826
RS
529 ;; Set selection
530 (if (not (eq overlay-start overlay-end))
531 (mouse-sel-set-selection
532 selection
533 (buffer-substring overlay-start overlay-end)))
1a2b6c52 534
f2506826
RS
535 ;; Handle copy/kill
536 (let (this-command)
537 (cond
538 ((eq (event-basic-type last-input-event) 'mouse-2)
539 (copy-region-as-kill overlay-start overlay-end)
540 (read-event) (read-event))
541 ((and (memq (event-basic-type last-input-event)
542 '(mouse-1 mouse-3))
543 (memq 'down (event-modifiers last-input-event)))
544 (kill-region overlay-start overlay-end)
545 (move-overlay overlay overlay-start overlay-start)
546 (read-event) (read-event))
547 ((and (eq (event-basic-type last-input-event) 'mouse-3)
548 (memq 'double (event-modifiers last-input-event)))
549 (kill-region overlay-start overlay-end)
550 (move-overlay overlay overlay-start overlay-start)))))
551
552 direction)
553
554 ;; Restore cursor
555 (if (fboundp 'modify-frame-parameters)
556 (modify-frame-parameters
557 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
558
559 ))))
560
561;;=== Paste ===============================================================
562
563(defun mouse-insert-selection (event)
564 "Insert the contents of the PRIMARY selection at mouse click.
565If `mouse-yank-at-point' is non-nil, insert at point instead."
566 (interactive "e")
567 (mouse-insert-selection-internal 'PRIMARY event))
568
569(defun mouse-insert-secondary (event)
570 "Insert the contents of the SECONDARY selection at mouse click.
6b4dd332 571If `mouse-yank-at-point' is non-nil, insert at point instead."
1a2b6c52 572 (interactive "e")
f2506826
RS
573 (mouse-insert-selection-internal 'SECONDARY event))
574
575(defun mouse-insert-selection-internal (selection event)
576 "Insert the contents of the named SELECTION at mouse click.
577If `mouse-yank-at-point' is non-nil, insert at point instead."
6b4dd332 578 (or mouse-yank-at-point
f2506826 579 (mouse-set-point event))
1a2b6c52 580 (if mouse-sel-get-selection-function
f2506826
RS
581 (progn
582 (push-mark (point) 'nomsg)
583 (insert (or (funcall mouse-sel-get-selection-function selection) "")))))
584
67384793
RS
585;;=== Handle loss of selections ===========================================
586
587(defun mouse-sel-lost-selection-hook (selection)
588 "Remove the overlay for a lost selection."
7b990caf 589 (let ((overlay (mouse-sel-selection-overlay selection)))
67384793
RS
590 (delete-overlay overlay)))
591
592(add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
1a2b6c52
RS
593
594;;=== Key bindings ========================================================
595
596(if (not mouse-sel-default-bindings) nil
597
598 (global-unset-key [mouse-1])
599 (global-unset-key [drag-mouse-1])
600 (global-unset-key [mouse-3])
601
602 (global-set-key [down-mouse-1] 'mouse-select)
603 (global-set-key [down-mouse-3] 'mouse-extend)
f2506826
RS
604
605 (global-unset-key [M-mouse-1])
606 (global-unset-key [M-drag-mouse-1])
607 (global-unset-key [M-mouse-3])
1a2b6c52 608
f2506826
RS
609 (global-set-key [M-down-mouse-1] 'mouse-select-secondary)
610 (global-set-key [M-down-mouse-3] 'mouse-extend-secondary)
611
1a2b6c52
RS
612 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) nil
613
f2506826
RS
614 (global-set-key [mouse-2] 'mouse-insert-selection)
615
1a2b6c52
RS
616 (setq interprogram-cut-function nil
617 interprogram-paste-function nil))
618
f2506826
RS
619 (global-set-key [M-mouse-2] 'mouse-insert-secondary)
620
1a2b6c52
RS
621 )
622
f2506826
RS
623;;=== Bug reporting =======================================================
624
625(defconst mouse-sel-maintainer-address "mikew@gopher.dosli.govt.nz")
626
627(defun mouse-sel-submit-bug-report ()
628 "Submit a bug report on mouse-sel.el via mail."
629 (interactive)
630 (require 'reporter)
631 (reporter-submit-bug-report
632 mouse-sel-maintainer-address
633 (concat "mouse-sel.el "
634 (or (condition-case nil mouse-sel-version (error))
635 "(distributed with Emacs)"))
636 (list 'transient-mark-mode
637 'delete-selection-mode
638 'mouse-sel-default-bindings
639 'mouse-sel-leave-point-near-mouse
640 'mouse-sel-cycle-clicks
641 'mouse-sel-selection-alist
642 'mouse-sel-set-selection-function
643 'mouse-sel-get-selection-function
f2506826
RS
644 'mouse-yank-at-point)))
645
1a2b6c52 646;; mouse-sel.el ends here.