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