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.
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.
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.
32 ;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
34 ;; * Clicking or dragging mouse-3 extends the selection as well.
36 ;; * Double-clicking on word constituents selects words.
37 ;; Double-clicking on symbol constituents selects symbols.
38 ;; Double-clicking on quotes or parentheses selects sexps.
39 ;; Double-clicking on whitespace selects whitespace.
40 ;; Triple-clicking selects lines.
41 ;; Quad-clicking selects paragraphs.
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.
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).
51 ;; * Pressing mouse-2 while selecting or extending copies selection
52 ;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
54 ;; * Double-clicking mouse-3 also kills selection.
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.
60 ;; This module requires my thingatpt.el module, which it uses to find the
61 ;; bounds of words, lines, sexps, etc.
63 ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
65 ;;--- Customisation -------------------------------------------------------
67 ;; * You may want to use none or more of following:
69 ;; ;; Enable region highlight
70 ;; (transient-mark-mode 1)
72 ;; ;; But only in the selected window
73 ;; (setq highlight-nonselected-windows nil)
75 ;; ;; Enable pending-delete
76 ;; (delete-selection-mode 1)
78 ;; * You can control the way mouse-sel binds its keys by setting the value
79 ;; of mouse-sel-default-bindings before loading mouse-sel.
81 ;; (a) If mouse-sel-default-bindings = t (the default)
83 ;; Mouse sets and insert selection
84 ;; mouse-1 mouse-select
85 ;; mouse-2 mouse-insert-selection
86 ;; mouse-3 mouse-extend
88 ;; Selection/kill-ring interaction is disabled
89 ;; interprogram-cut-function = nil
90 ;; interprogram-paste-function = nil
92 ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
94 ;; Mouse sets selection, and pastes from kill-ring
95 ;; mouse-1 mouse-select
96 ;; mouse-2 mouse-yank-at-click
97 ;; mouse-3 mouse-extend
99 ;; Selection/kill-ring interaction is retained
100 ;; interprogram-cut-function = x-select-text
101 ;; interprogram-paste-function = x-cut-buffer-or-selection-value
103 ;; What you lose is the ability to select some text in
104 ;; delete-selection-mode and yank over the top of it.
106 ;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
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:
111 ;; (setq mouse-yank-at-point t)
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:
118 ;; (setq mouse-sel-leave-point-near-mouse nil)
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.
123 ;; Disable this behaviour with:
125 ;; (setq mouse-sel-cycle-clicks nil)
127 ;; * The variables mouse-sel-{set,get}-selection-function control how the
128 ;; selection is handled. Under X Windows, these variables default so
129 ;; that the X primary selection is used. Under other windowing systems,
130 ;; alternate functions are used, which simply store the selection value
133 ;; * You can change the selection highlight face by altering the properties
134 ;; of mouse-drag-overlay, eg.
136 ;; (overlay-put mouse-drag-overlay 'face 'bold)
145 ;;=== User Variables ======================================================
147 (defvar mouse-sel-leave-point-near-mouse t
148 "*Leave point near last mouse position.
149 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
150 of the region nearest to where the mouse last was.
151 If nil, point will always be placed at the beginning of the region.")
153 (defvar mouse-sel-cycle-clicks t
154 "*If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks.")
156 (defvar mouse-sel-default-bindings t
157 "Set to nil before loading `mouse-sel' to prevent default mouse bindings.")
159 ;;=== Internal Variables/Constants ========================================
161 (defvar mouse-sel-primary-thing nil
162 "Type of PRIMARY selection in current buffer.")
163 (make-variable-buffer-local 'mouse-sel-primary-thing
)
165 (defvar mouse-sel-secondary-thing nil
166 "Type of SECONDARY selection in current buffer.")
167 (make-variable-buffer-local 'mouse-sel-secondary-thing
)
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
))
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
180 (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
182 where 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.")
187 (defvar mouse-sel-set-selection-function
188 (function (lambda (selection value
)
189 (if (eq selection
'PRIMARY
)
190 (x-select-text value
)
191 (x-set-selection selection value
))))
192 "Function to call to set selection.
193 Called with two arguments:
195 SELECTION, the name of the selection concerned, and
196 VALUE, the text to store.
197 This sets the selection as well as the cut buffer for the older applications.
198 Use (setq mouse-sel-set-selection-function 'x-set-selection) if you don't care
201 (defvar mouse-sel-get-selection-function
202 (function (lambda (selection)
203 (if (eq selection
'PRIMARY
)
204 (x-cut-buffer-or-selection-value)
205 (x-get-selection selection
))))
206 "Function to call to get the selection.
207 Called with one argument:
209 SELECTION: the name of the selection concerned.")
211 ;;=== Support/access functions ============================================
213 (defun mouse-sel-determine-selection-thing (nclicks)
214 "Determine what `thing' `mouse-sel' should operate on.
215 The first argument is NCLICKS, is the number of consecutive
216 mouse clicks at the same position.
218 Double-clicking on word constituents selects words.
219 Double-clicking on symbol constituents selects symbols.
220 Double-clicking on quotes or parentheses selects sexps.
221 Double-clicking on whitespace selects whitespace.
222 Triple-clicking selects lines.
223 Quad-clicking selects paragraphs.
225 Feel free to re-define this function to support your own desired
226 multi-click semantics."
227 (let* ((next-char (char-after (point)))
228 (char-syntax (if next-char
(char-syntax next-char
))))
229 (if mouse-sel-cycle-clicks
230 (setq nclicks
(1+ (%
(1- nclicks
) 4))))
233 ((= nclicks
3) 'line
)
234 ((>= nclicks
4) 'paragraph
)
235 ((memq char-syntax
'(?\
( ?\
) ?
\" ?
')) 'sexp
)
236 ((memq next-char
'(? ?
\t ?
\n)) 'whitespace
)
237 ((eq char-syntax ?_
) 'symbol
)
238 ((eq char-syntax ?w
) 'word
))))
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
)))
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
)))
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
)))
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
))
264 (defun mouse-sel-region-to-primary (orig-window)
265 "Convert region to PRIMARY overlay and deactivate region.
266 Argument ORIG-WINDOW specifies the window the cursor was in when the
267 originating command was issued, and is used to determine whether the
268 region was visible or not."
269 (if transient-mark-mode
270 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY
)))
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)
278 ((eq orig-window
(selected-window))
279 ;; Point was visible, so set overlay at point
280 (move-overlay overlay
(point) (point) (current-buffer)))
282 ;; Nothing was visible, so remove overlay
283 (delete-overlay overlay
)))
284 (setq mark-active nil
))))
286 (defun mouse-sel-primary-to-region (&optional direction
)
287 "Convert PRIMARY overlay to region.
288 Optional argument DIRECTION specifies the mouse drag direction: a value of
289 1 indicates that the mouse was dragged left-to-right, otherwise it was
290 dragged right-to-left."
291 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY
))
292 (start (overlay-start overlay
))
293 (end (overlay-end overlay
)))
296 (if start
(goto-char start
))
298 (if (and mouse-sel-leave-point-near-mouse
(eq direction
1))
301 (push-mark start
'nomsg
'active
))
303 (push-mark end
'nomsg
'active
)))
304 (if transient-mark-mode
(delete-overlay overlay
))))
306 (defmacro mouse-sel-eval-at-event-end
(event &rest forms
)
307 "Evaluate forms at mouse position.
308 Move to the end position of EVENT, execute FORMS, and restore original
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
)))
324 (goto-char (marker-position orig-point-marker
))
325 (move-marker orig-point-marker nil
)
326 (select-window orig-window
)
329 (put 'mouse-sel-eval-at-event-end
'lisp-indent-hook
1)
331 ;;=== Select ==============================================================
333 (defun mouse-select (event)
334 "Set region/selection using the mouse.
336 Click sets point & mark to click position.
337 Dragging extends region/selection.
339 Multi-clicking selects word/lines/paragraphs, as determined by
340 'mouse-sel-determine-selection-thing.
342 Clicking mouse-2 while selecting copies selected text to the kill-ring.
343 Clicking mouse-1 or mouse-3 kills the selected text.
345 This should be bound to a down-mouse event."
349 (setq direction
(mouse-select-internal 'PRIMARY event
))
350 (mouse-sel-primary-to-region direction
))))
352 (defun mouse-select-secondary (event)
353 "Set secondary selection using the mouse.
355 Click sets the start of the secondary selection to click position.
356 Dragging extends the secondary selection.
358 Multi-clicking selects word/lines/paragraphs, as determined by
359 'mouse-sel-determine-selection-thing.
361 Clicking mouse-2 while selecting copies selected text to the kill-ring.
362 Clicking mouse-1 or mouse-3 kills the selected text.
364 This should be bound to a down-mouse event."
366 (mouse-select-internal 'SECONDARY event
))
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
)))
374 (mouse-sel-determine-selection-thing (event-click-count event
)))
375 (let ((object-bounds (bounds-of-thing-at-point
376 (symbol-value thing-symbol
))))
379 (move-overlay overlay
380 (car object-bounds
) (cdr object-bounds
)
382 (move-overlay overlay
(point) (point) (current-buffer)))))
383 (mouse-extend-internal selection
)))
385 ;;=== Extend ==============================================================
387 (defun mouse-extend (event)
388 "Extend region/selection using the mouse."
390 (let ((orig-window (selected-window))
392 (select-window (posn-window (event-end event
)))
395 (mouse-sel-region-to-primary orig-window
)
396 (setq direction
(mouse-extend-internal 'PRIMARY event
)))
397 (mouse-sel-primary-to-region direction
))))
399 (defun mouse-extend-secondary (event)
400 "Extend secondary selection using the mouse."
402 (save-window-excursion
403 (mouse-extend-internal 'SECONDARY event
)))
405 (defun mouse-extend-internal (selection &optional initial-event
)
406 "Extend specified SELECTION using the mouse.
407 Track mouse-motion events, adjusting the SELECTION appropriately.
408 Optional argument INITIAL-EVENT specifies an initial down-mouse event to
411 See 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))))))
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
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
))
435 (set thing-symbol nil
))
439 (if (fboundp 'modify-frame-parameters
)
440 (modify-frame-parameters (selected-frame)
441 '((cursor-type . bar
))))
446 (while (if initial-event
; Use initial event
448 (setq event initial-event
)
449 (setq initial-event nil
))
450 (setq event
(read-event))
452 (memq (car event
) '(mouse-movement switch-frame
))))
454 (let ((selection-thing (symbol-value thing-symbol
))
455 (end (event-end event
)))
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
)))
464 (window-frame posn-w
)
466 (window-frame orig-window
)))) nil
)
468 ;; Different window, same frame
469 ((not (eq (posn-window end
) orig-window
))
470 (let ((end-row (cdr (cdr (mouse-position)))))
472 ((and end-row
(not (bobp)) (< end-row top
))
473 (mouse-scroll-subr orig-window
(- end-row top
)
475 ((and end-row
(not (eobp)) (>= end-row bottom
))
476 (mouse-scroll-subr orig-window
(1+ (- end-row bottom
))
481 ((eq (posn-point end
) 'mode-line
)
482 (mouse-scroll-subr orig-window
1 overlay min
))
484 ;; In original window
485 (t (goto-char (posn-point end
)))
489 ;; Determine direction of drag
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
))
495 ((and (not (eq direction
1)) (>= (point) max
))
498 (if (not selection-thing
) nil
500 ;; If dragging forward, goal is next character
501 (if (and (eq direction
1) (not (eobp))) (forward-char 1))
503 ;; Move to start/end of selected thing
504 (let ((goal (point)))
505 (goto-char (if (eq 1 direction
) min max
))
508 (while (> (* direction
(- goal
(point))) 0)
509 (forward-thing selection-thing direction
))
511 (forward-thing selection-thing
(- direction
))
513 (if (> (* direction
(- goal
(point))) 0)
518 (move-overlay overlay
519 (if (eq 1 direction
) min
(point))
520 (if (eq -
1 direction
) max
(point))
523 ))) ; end track-mouse
525 ;; Finish up after dragging
526 (let ((overlay-start (overlay-start overlay
))
527 (overlay-end (overlay-end overlay
)))
530 (if (not (eq overlay-start overlay-end
))
531 (mouse-sel-set-selection
533 (buffer-substring overlay-start overlay-end
)))
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
)
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
)))))
555 (if (fboundp 'modify-frame-parameters
)
556 (modify-frame-parameters
557 (selected-frame) (list (cons 'cursor-type orig-cursor-type
))))
561 ;;=== Paste ===============================================================
563 (defun mouse-insert-selection (event)
564 "Insert the contents of the PRIMARY selection at mouse click.
565 If `mouse-yank-at-point' is non-nil, insert at point instead."
567 (mouse-insert-selection-internal 'PRIMARY event
))
569 (defun mouse-insert-secondary (event)
570 "Insert the contents of the SECONDARY selection at mouse click.
571 If `mouse-yank-at-point' is non-nil, insert at point instead."
573 (mouse-insert-selection-internal 'SECONDARY event
))
575 (defun mouse-insert-selection-internal (selection event
)
576 "Insert the contents of the named SELECTION at mouse click.
577 If `mouse-yank-at-point' is non-nil, insert at point instead."
578 (or mouse-yank-at-point
579 (mouse-set-point event
))
580 (if mouse-sel-get-selection-function
582 (push-mark (point) 'nomsg
)
583 (insert (or (funcall mouse-sel-get-selection-function selection
) "")))))
585 ;;=== Handle loss of selections ===========================================
587 (defun mouse-sel-lost-selection-hook (selection)
588 "Remove the overlay for a lost selection."
589 (let ((overlay (mouse-sel-selection-overlay selection
)))
590 (delete-overlay overlay
)))
592 (add-hook 'x-lost-selection-hooks
'mouse-sel-lost-selection-hook
)
594 ;;=== Key bindings ========================================================
596 (if (not mouse-sel-default-bindings
) nil
598 (global-unset-key [mouse-1
])
599 (global-unset-key [drag-mouse-1
])
600 (global-unset-key [mouse-3
])
602 (global-set-key [down-mouse-1
] 'mouse-select
)
603 (global-set-key [down-mouse-3
] 'mouse-extend
)
605 (global-unset-key [M-mouse-1
])
606 (global-unset-key [M-drag-mouse-1
])
607 (global-unset-key [M-mouse-3
])
609 (global-set-key [M-down-mouse-1
] 'mouse-select-secondary
)
610 (global-set-key [M-down-mouse-3
] 'mouse-extend-secondary
)
612 (if (eq mouse-sel-default-bindings
'interprogram-cut-paste
) nil
614 (global-set-key [mouse-2
] 'mouse-insert-selection
)
616 (setq interprogram-cut-function nil
617 interprogram-paste-function nil
))
619 (global-set-key [M-mouse-2
] 'mouse-insert-secondary
)
623 ;;=== Bug reporting =======================================================
625 (defconst mouse-sel-maintainer-address
"mikew@gopher.dosli.govt.nz")
627 (defun mouse-sel-submit-bug-report ()
628 "Submit a bug report on mouse-sel.el via mail."
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
644 'mouse-yank-at-point
)))
646 ;; mouse-sel.el ends here.