1 ;;; mouse-sel.el --- Multi-click selection support for Emacs 19
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
22 ;; This module provides multi-click mouse support for GNU Emacs versions
23 ;; 19.18 and later. I've tried to make it behave more like standard X
24 ;; clients (eg. xterm) than the default Emacs 19 mouse selection handlers.
27 ;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
29 ;; * Clicking or dragging mouse-3 extends the selection as well.
31 ;; * Double-clicking on word constituents selects words.
32 ;; Double-clicking on symbol constituents selects symbols.
33 ;; Double-clicking on quotes or parentheses selects sexps.
34 ;; Double-clicking on whitespace selects whitespace.
35 ;; Triple-clicking selects lines.
36 ;; Quad-clicking selects paragraphs.
38 ;; * Selecting sets the region & X primary selection, but does NOT affect
39 ;; the kill-ring. Because the mouse handlers set the primary selection
40 ;; directly, mouse-sel sets the variables interprogram-cut-function
41 ;; and interprogram-paste-function to nil.
43 ;; * Clicking mouse-2 inserts the contents of the primary selection at
44 ;; the mouse position (or point, if mouse-yank-at-point is non-nil).
46 ;; * Pressing mouse-2 while selecting or extending copies selection
47 ;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
49 ;; * Double-clicking mouse-3 also kills selection.
51 ;; * M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
52 ;; & mouse-3, but operate on the X secondary selection rather than the
53 ;; primary selection and region.
55 ;; This module requires my thingatpt.el module, which it uses to find the
56 ;; bounds of words, lines, sexps, etc.
58 ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
60 ;;--- Customisation -------------------------------------------------------
62 ;; * You may want to use none or more of following:
64 ;; ;; Enable region highlight
65 ;; (transient-mark-mode 1)
67 ;; ;; But only in the selected window
68 ;; (setq highlight-nonselected-windows nil)
70 ;; ;; Enable pending-delete
71 ;; (delete-selection-mode 1)
73 ;; * You can control the way mouse-sel binds its keys by setting the value
74 ;; of mouse-sel-default-bindings before loading mouse-sel.
76 ;; (a) If mouse-sel-default-bindings = t (the default)
78 ;; Mouse sets and insert selection
79 ;; mouse-1 mouse-select
80 ;; mouse-2 mouse-insert-selection
81 ;; mouse-3 mouse-extend
83 ;; Selection/kill-ring interaction is disabled
84 ;; interprogram-cut-function = nil
85 ;; interprogram-paste-function = nil
87 ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
89 ;; Mouse sets selection, and pastes from kill-ring
90 ;; mouse-1 mouse-select
91 ;; mouse-2 mouse-yank-at-click
92 ;; mouse-3 mouse-extend
94 ;; Selection/kill-ring interaction is retained
95 ;; interprogram-cut-function = x-select-text
96 ;; interprogram-paste-function = x-cut-buffer-or-selection-value
98 ;; What you lose is the ability to select some text in
99 ;; delete-selection-mode and yank over the top of it.
101 ;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
103 ;; * By default, mouse-insert-selection (mouse-2) inserts the selection at
104 ;; the mouse position. You can tell it to insert at point instead with:
106 ;; (setq mouse-yank-at-point t)
108 ;; * I like to leave point at the end of the region nearest to where the
109 ;; mouse was, even though this makes region highlighting mis-leading (the
110 ;; cursor makes it look like one extra character is selected). You can
111 ;; disable this behaviour with:
113 ;; (setq mouse-sel-leave-point-near-mouse nil)
115 ;; * By default, mouse-select cycles the click count after 4 clicks. That
116 ;; is, clicking mouse-1 five times has the same effect as clicking it
117 ;; once, clicking six times has the same effect as clicking twice, etc.
118 ;; Disable this behaviour with:
120 ;; (setq mouse-sel-cycle-clicks nil)
122 ;; * The variables mouse-sel-{set,get}-selection-function control how the
123 ;; selection is handled. Under X Windows, these variables default so
124 ;; that the X primary selection is used. Under other windowing systems,
125 ;; alternate functions are used, which simply store the selection value
128 ;; * You can change the selection highlight face by altering the properties
129 ;; of mouse-drag-overlay, eg.
131 ;; (overlay-put mouse-drag-overlay 'face 'bold)
133 ;;; Code: =================================================================
141 ;;=== User Variables ======================================================
143 (defvar mouse-sel-leave-point-near-mouse t
144 "*Leave point near last mouse position.
145 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
146 of the region nearest to where the mouse last was.
147 If nil, point will always be placed at the beginning of the region.")
149 (defvar mouse-sel-cycle-clicks t
150 "*If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks.")
152 (defvar mouse-sel-default-bindings t
153 "Set to nil before loading `mouse-sel' to prevent default mouse bindings.")
155 ;;=== Internal Variables/Constants ========================================
157 (defvar mouse-sel-primary-thing nil
158 "Type of PRIMARY selection in current buffer.")
159 (make-variable-buffer-local 'mouse-sel-primary-thing
)
161 (defvar mouse-sel-secondary-thing nil
162 "Type of SECONDARY selection in current buffer.")
163 (make-variable-buffer-local 'mouse-sel-secondary-thing
)
165 ;; Ensure that secondary overlay is defined
166 (if (overlayp mouse-secondary-overlay
) nil
167 (setq mouse-secondary-overlay
(make-overlay 1 1))
168 (overlay-put mouse-secondary-overlay
'face
'secondary-selection
))
170 (defconst mouse-sel-selection-alist
171 '((PRIMARY mouse-drag-overlay mouse-sel-primary-thing
)
172 (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing
))
173 "Alist associating selections with variables. Each element is of
176 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
178 where SELECTION-NAME = name of selection
179 OVERLAY-SYMBOL = name of variable containing overlay to use
180 SELECTION-THING-SYMBOL = name of variable where the current selection
181 type for this selection should be stored.")
183 (defvar mouse-sel-set-selection-function
184 (if (fboundp 'x-set-selection
)
186 "Function to call to set selection.
187 Called with two arguments:
189 SELECTION, the name of the selection concerned, and
190 VALUE, the text to store.")
192 (defvar mouse-sel-get-selection-function
193 (if (fboundp 'x-get-selection
)
195 "Function to call to get the selection.
196 Called with one argument:
198 SELECTION: the name of the selection concerned.")
200 (defvar mouse-sel-selection-owner-p-function
201 (if (fboundp 'x-selection-owner-p
)
202 'x-selection-owner-p
)
203 "Function to check whether Emacs still owns the selection.
204 Called with one argument:
206 SELECTION: the name of the selection concerned.")
208 ;;=== Support/access functions ============================================
210 (defun mouse-sel-determine-selection-thing (nclicks)
211 "Determine what `thing' `mouse-sel' should operate on.
212 The first argument is NCLICKS, is the number of consecutive
213 mouse clicks at the same position.
215 Double-clicking on word constituents selects words.
216 Double-clicking on symbol constituents selects symbols.
217 Double-clicking on quotes or parentheses selects sexps.
218 Double-clicking on whitespace selects whitespace.
219 Triple-clicking selects lines.
220 Quad-clicking selects paragraphs.
222 Feel free to re-define this function to support your own desired
223 multi-click semantics."
224 (let* ((next-char (char-after (point)))
225 (char-syntax (if next-char
(char-syntax next-char
))))
226 (if mouse-sel-cycle-clicks
227 (setq nclicks
(1+ (%
(1- nclicks
) 4))))
230 ((= nclicks
3) 'line
)
231 ((>= nclicks
4) 'paragraph
)
232 ((memq char-syntax
'(?\
( ?\
) ?
\" ?
')) 'sexp
)
233 ((memq next-char
'(? ?
\t ?
\n)) 'whitespace
)
234 ((eq char-syntax ?_
) 'symbol
)
235 ((eq char-syntax ?w
) 'word
))))
237 (defun mouse-sel-set-selection (selection value
)
238 "Set the specified SELECTION to VALUE."
239 (if mouse-sel-set-selection-function
240 (funcall mouse-sel-set-selection-function selection value
)
241 (put 'mouse-sel-internal-selection selection value
)))
243 (defun mouse-sel-get-selection (selection)
244 "Get the value of the specified SELECTION."
245 (if mouse-sel-get-selection-function
246 (funcall mouse-sel-get-selection-function selection
)
247 (get 'mouse-sel-internal-selection selection
)))
249 (defun mouse-sel-selection-owner-p (selection)
250 "Determine whether Emacs owns the specified SELECTION."
251 (if mouse-sel-selection-owner-p-function
252 (funcall mouse-sel-selection-owner-p-function selection
)
255 (defun mouse-sel-selection-overlay (selection)
256 "Return overlay corresponding to SELECTION."
257 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist
))))
258 (or symbol
(error "No overlay corresponding to %s selection" selection
))
259 (symbol-value symbol
)))
261 (defun mouse-sel-selection-thing (selection)
262 "Return overlay corresponding to SELECTION."
263 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist
))))
264 (or symbol
(error "No symbol corresponding to %s selection" selection
))
267 (defun mouse-sel-region-to-primary (orig-window)
268 "Convert region to PRIMARY overlay and deactivate region.
269 Argument ORIG-WINDOW specifies the window the cursor was in when the
270 originating command was issued, and is used to determine whether the
271 region was visible or not."
272 (if transient-mark-mode
273 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY
)))
276 (or highlight-nonselected-windows
277 (eq orig-window
(selected-window))))
278 ;; Region was visible, so convert region to overlay
279 (move-overlay overlay
(region-beginning) (region-end)
281 ((eq orig-window
(selected-window))
282 ;; Point was visible, so set overlay at point
283 (move-overlay overlay
(point) (point) (current-buffer)))
285 ;; Nothing was visible, so remove overlay
286 (delete-overlay overlay
)))
287 (setq mark-active nil
))))
289 (defun mouse-sel-primary-to-region (&optional direction
)
290 "Convert PRIMARY overlay to region.
291 Optional argument DIRECTION specifies the mouse drag direction: a value of
292 1 indicates that the mouse was dragged left-to-right, otherwise it was
293 dragged right-to-left."
294 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY
))
295 (start (overlay-start overlay
))
296 (end (overlay-end overlay
)))
299 (if start
(goto-char start
))
301 (if (and mouse-sel-leave-point-near-mouse
(eq direction
1))
304 (push-mark start
'nomsg
'active
))
306 (push-mark end
'nomsg
'active
)))
307 (if transient-mark-mode
(delete-overlay overlay
))))
309 (defmacro mouse-sel-eval-at-event-end
(event &rest forms
)
310 "Evaluate forms at mouse position.
311 Move to the end position of EVENT, execute FORMS, and restore original
314 (let ((posn (event-end (, event
))))
315 (if posn
(mouse-minibuffer-check (, event
)))
316 (if (and posn
(not (windowp (posn-window posn
))))
317 (error "Cursor not in text area of window"))
318 (let (orig-window orig-point-marker
)
319 (setq orig-window
(selected-window))
320 (if posn
(select-window (posn-window posn
)))
321 (setq orig-point-marker
(point-marker))
322 (if (and posn
(numberp (posn-point posn
)))
323 (goto-char (posn-point posn
)))
327 (goto-char (marker-position orig-point-marker
))
328 (move-marker orig-point-marker nil
)
329 (select-window orig-window
)
332 (put 'mouse-sel-eval-at-event-end
'lisp-indent-hook
1)
334 ;;=== Select ==============================================================
336 (defun mouse-select (event)
337 "Set region/selection using the mouse.
339 Click sets point & mark to click position.
340 Dragging extends region/selection.
342 Multi-clicking selects word/lines/paragraphs, as determined by
343 'mouse-sel-determine-selection-thing.
345 Clicking mouse-2 while selecting copies selected text to the kill-ring.
346 Clicking mouse-1 or mouse-3 kills the selected text.
348 This should be bound to a down-mouse event."
352 (setq direction
(mouse-select-internal 'PRIMARY event
))
353 (mouse-sel-primary-to-region direction
))))
355 (defun mouse-select-secondary (event)
356 "Set secondary selection using the mouse.
358 Click sets the start of the secondary selection to click position.
359 Dragging extends the secondary selection.
361 Multi-clicking selects word/lines/paragraphs, as determined by
362 'mouse-sel-determine-selection-thing.
364 Clicking mouse-2 while selecting copies selected text to the kill-ring.
365 Clicking mouse-1 or mouse-3 kills the selected text.
367 This should be bound to a down-mouse event."
369 (mouse-select-internal 'SECONDARY event
))
371 (defun mouse-select-internal (selection event
)
372 "Set SELECTION using the mouse."
373 (mouse-sel-eval-at-event-end event
374 (let ((thing-symbol (mouse-sel-selection-thing selection
))
375 (overlay (mouse-sel-selection-overlay selection
)))
377 (mouse-sel-determine-selection-thing (event-click-count event
)))
378 (let ((object-bounds (bounds-of-thing-at-point
379 (symbol-value thing-symbol
))))
382 (move-overlay overlay
383 (car object-bounds
) (cdr object-bounds
)
385 (move-overlay overlay
(point) (point) (current-buffer)))))
386 (mouse-extend-internal selection
)))
388 ;;=== Extend ==============================================================
390 (defun mouse-extend (event)
391 "Extend region/selection using the mouse."
393 (let ((orig-window (selected-window))
395 (select-window (posn-window (event-end event
)))
398 (mouse-sel-region-to-primary orig-window
)
399 (setq direction
(mouse-extend-internal 'PRIMARY event
)))
400 (mouse-sel-primary-to-region direction
))))
402 (defun mouse-extend-secondary (event)
403 "Extend secondary selection using the mouse."
405 (save-window-excursion
406 (mouse-extend-internal 'SECONDARY event
)))
408 (defun mouse-extend-internal (selection &optional initial-event
)
409 "Extend specified SELECTION using the mouse.
410 Track mouse-motion events, adjusting the SELECTION appropriately.
411 Optional argument INITIAL-EVENT specifies an initial down-mouse event to
414 See documentation for mouse-select-internal for more details."
415 (mouse-sel-eval-at-event-end initial-event
416 (let ((orig-cursor-type
417 (cdr (assoc 'cursor-type
(frame-parameters (selected-frame))))))
420 (let* ((thing-symbol (mouse-sel-selection-thing selection
))
421 (overlay (mouse-sel-selection-overlay selection
))
422 (orig-window (selected-window))
423 (orig-window-frame (window-frame orig-window
))
424 (top (nth 1 (window-edges orig-window
)))
425 (bottom (nth 3 (window-edges orig-window
)))
426 (mark-active nil
) ; inhibit normal region highlight
427 (echo-keystrokes 0) ; don't echo mouse events
432 ;; Get current bounds of overlay
433 (if (eq (overlay-buffer overlay
) (current-buffer))
434 (setq min
(overlay-start overlay
)
435 max
(overlay-end overlay
))
438 (set thing-symbol nil
))
442 (if (fboundp 'modify-frame-parameters
)
443 (modify-frame-parameters (selected-frame)
444 '((cursor-type . bar
))))
449 (while (if initial-event
; Use initial event
451 (setq event initial-event
)
452 (setq initial-event nil
))
453 (setq event
(read-event))
455 (memq (car event
) '(mouse-movement switch-frame
))))
457 (let ((selection-thing (symbol-value thing-symbol
))
458 (end (event-end event
)))
462 ;; Ignore any movement outside the frame
463 ((eq (car-safe event
) 'switch-frame
) nil
)
464 ((and (posn-window end
)
465 (not (eq (let ((posn-w (posn-window end
)))
467 (window-frame posn-w
)
469 (window-frame orig-window
)))) nil
)
471 ;; Different window, same frame
472 ((not (eq (posn-window end
) orig-window
))
473 (let ((end-row (cdr (cdr (mouse-position)))))
475 ((and end-row
(not (bobp)) (< end-row top
))
476 (mouse-scroll-subr orig-window
(- end-row top
)
478 ((and end-row
(not (eobp)) (>= end-row bottom
))
479 (mouse-scroll-subr orig-window
(1+ (- end-row bottom
))
484 ((eq (posn-point end
) 'mode-line
)
485 (mouse-scroll-subr orig-window
1 overlay min
))
487 ;; In original window
488 (t (goto-char (posn-point end
)))
492 ;; Determine direction of drag
494 ((and (not direction
) (not (eq min max
)))
495 (setq direction
(if (< (point) (/ (+ min max
) 2)) -
1 1)))
496 ((and (not (eq direction -
1)) (<= (point) min
))
498 ((and (not (eq direction
1)) (>= (point) max
))
501 (if (not selection-thing
) nil
503 ;; If dragging forward, goal is next character
504 (if (and (eq direction
1) (not (eobp))) (forward-char 1))
506 ;; Move to start/end of selected thing
509 (goto-char (if (eq 1 direction
) min max
))
512 (while (> (* direction
(- goal
(point))) 0)
514 (forward-thing selection-thing direction
))
516 (forward-thing selection-thing
(- direction
))
518 (if (> (* direction
(- goal
(point))) 0)
523 (move-overlay overlay
524 (if (eq 1 direction
) min
(point))
525 (if (eq -
1 direction
) max
(point))
528 ))) ; end track-mouse
530 ;; Finish up after dragging
531 (let ((overlay-start (overlay-start overlay
))
532 (overlay-end (overlay-end overlay
)))
535 (if (not (eq overlay-start overlay-end
))
536 (mouse-sel-set-selection
538 (buffer-substring overlay-start overlay-end
)))
543 ((eq (event-basic-type last-input-event
) 'mouse-2
)
544 (copy-region-as-kill overlay-start overlay-end
)
545 (read-event) (read-event))
546 ((and (memq (event-basic-type last-input-event
)
548 (memq 'down
(event-modifiers last-input-event
)))
549 (kill-region overlay-start overlay-end
)
550 (move-overlay overlay overlay-start overlay-start
)
551 (read-event) (read-event))
552 ((and (eq (event-basic-type last-input-event
) 'mouse-3
)
553 (memq 'double
(event-modifiers last-input-event
)))
554 (kill-region overlay-start overlay-end
)
555 (move-overlay overlay overlay-start overlay-start
)))))
560 (if (fboundp 'modify-frame-parameters
)
561 (modify-frame-parameters
562 (selected-frame) (list (cons 'cursor-type orig-cursor-type
))))
566 ;;=== Paste ===============================================================
568 (defun mouse-insert-selection (event)
569 "Insert the contents of the PRIMARY selection at mouse click.
570 If `mouse-yank-at-point' is non-nil, insert at point instead."
572 (mouse-insert-selection-internal 'PRIMARY event
))
574 (defun mouse-insert-secondary (event)
575 "Insert the contents of the SECONDARY selection at mouse click.
576 If `mouse-yank-at-point' is non-nil, insert at point instead."
578 (mouse-insert-selection-internal 'SECONDARY event
))
580 (defun mouse-insert-selection-internal (selection event
)
581 "Insert the contents of the named SELECTION at mouse click.
582 If `mouse-yank-at-point' is non-nil, insert at point instead."
583 (or mouse-yank-at-point
584 (mouse-set-point event
))
585 (if mouse-sel-get-selection-function
587 (push-mark (point) 'nomsg
)
588 (insert (or (funcall mouse-sel-get-selection-function selection
) "")))))
590 ;;=== Validate selection ==================================================
592 (defun mouse-sel-validate-selection ()
593 "Validate selections in mouse-sel-selection-alist.
594 For each listed selection, remove the selection overlay if Emacs no longer
596 (let ((owner-p-function mouse-sel-selection-owner-p-function
)
597 (alist mouse-sel-selection-alist
)
601 (setq selection
(car (car alist
))
602 overlay
(symbol-value (nth 1 (car alist
)))
604 (or (funcall owner-p-function selection
)
605 (delete-overlay overlay
))))))
607 (add-hook 'pre-command-hook
'mouse-sel-validate-selection
)
609 ;;=== Key bindings ========================================================
611 (if (not mouse-sel-default-bindings
) nil
613 (global-unset-key [mouse-1
])
614 (global-unset-key [drag-mouse-1
])
615 (global-unset-key [mouse-3
])
617 (global-set-key [down-mouse-1
] 'mouse-select
)
618 (global-set-key [down-mouse-3
] 'mouse-extend
)
620 (global-unset-key [M-mouse-1
])
621 (global-unset-key [M-drag-mouse-1
])
622 (global-unset-key [M-mouse-3
])
624 (global-set-key [M-down-mouse-1
] 'mouse-select-secondary
)
625 (global-set-key [M-down-mouse-3
] 'mouse-extend-secondary
)
627 (if (eq mouse-sel-default-bindings
'interprogram-cut-paste
) nil
629 (global-set-key [mouse-2
] 'mouse-insert-selection
)
631 (setq interprogram-cut-function nil
632 interprogram-paste-function nil
))
634 (global-set-key [M-mouse-2
] 'mouse-insert-secondary
)
638 ;;=== Bug reporting =======================================================
640 (defconst mouse-sel-maintainer-address
"mikew@gopher.dosli.govt.nz")
642 (defun mouse-sel-submit-bug-report ()
643 "Submit a bug report on mouse-sel.el via mail."
646 (reporter-submit-bug-report
647 mouse-sel-maintainer-address
648 (concat "mouse-sel.el "
649 (or (condition-case nil mouse-sel-version
(error))
650 "(distributed with Emacs)"))
651 (list 'transient-mark-mode
652 'delete-selection-mode
653 'mouse-sel-default-bindings
654 'mouse-sel-leave-point-near-mouse
655 'mouse-sel-cycle-clicks
656 'mouse-sel-selection-alist
657 'mouse-sel-set-selection-function
658 'mouse-sel-get-selection-function
659 'mouse-sel-selection-owner-p-function
660 'mouse-yank-at-point
)))
662 ;; mouse-sel.el ends here.