(bookmark-locate): ;;;###autoload this alias.
[emacs.git] / lisp / mouse-sel.el
blobd3a63095bbcc96c60d685b16f0cb19e3b0ee77ed
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 ;; 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.
25 ;;; Commentary:
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:
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.
53 ;;
54 ;; * Double-clicking mouse-3 also kills selection.
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.
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)
74 ;;
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)
82 ;;
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
93 ;;
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
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
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
131 ;; in a variable.
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)
138 ;;; Code:
140 (provide 'mouse-sel)
142 (require 'mouse)
143 (require 'thingatpt)
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
178 the form:
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 (if (fboundp 'x-set-selection)
189 'x-set-selection)
190 "Function to call to set selection.
191 Called with two arguments:
193 SELECTION, the name of the selection concerned, and
194 VALUE, the text to store.")
196 (defvar mouse-sel-get-selection-function
197 (if (fboundp 'x-get-selection)
198 'x-get-selection)
199 "Function to call to get the selection.
200 Called with one argument:
202 SELECTION: the name of the selection concerned.")
204 ;;=== Support/access functions ============================================
206 (defun mouse-sel-determine-selection-thing (nclicks)
207 "Determine what `thing' `mouse-sel' should operate on.
208 The first argument is NCLICKS, is the number of consecutive
209 mouse clicks at the same position.
211 Double-clicking on word constituents selects words.
212 Double-clicking on symbol constituents selects symbols.
213 Double-clicking on quotes or parentheses selects sexps.
214 Double-clicking on whitespace selects whitespace.
215 Triple-clicking selects lines.
216 Quad-clicking selects paragraphs.
218 Feel free to re-define this function to support your own desired
219 multi-click semantics."
220 (let* ((next-char (char-after (point)))
221 (char-syntax (if next-char (char-syntax next-char))))
222 (if mouse-sel-cycle-clicks
223 (setq nclicks (1+ (% (1- nclicks) 4))))
224 (cond
225 ((= nclicks 1) nil)
226 ((= nclicks 3) 'line)
227 ((>= nclicks 4) 'paragraph)
228 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
229 ((memq next-char '(? ?\t ?\n)) 'whitespace)
230 ((eq char-syntax ?_) 'symbol)
231 ((eq char-syntax ?w) 'word))))
233 (defun mouse-sel-set-selection (selection value)
234 "Set the specified SELECTION to VALUE."
235 (if mouse-sel-set-selection-function
236 (funcall mouse-sel-set-selection-function selection value)
237 (put 'mouse-sel-internal-selection selection value)))
239 (defun mouse-sel-get-selection (selection)
240 "Get the value of the specified SELECTION."
241 (if mouse-sel-get-selection-function
242 (funcall mouse-sel-get-selection-function selection)
243 (get 'mouse-sel-internal-selection selection)))
245 (defun mouse-sel-selection-overlay (selection)
246 "Return overlay corresponding to SELECTION."
247 (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
248 (or symbol (error "No overlay corresponding to %s selection" selection))
249 (symbol-value symbol)))
251 (defun mouse-sel-selection-thing (selection)
252 "Return overlay corresponding to SELECTION."
253 (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
254 (or symbol (error "No symbol corresponding to %s selection" selection))
255 symbol))
257 (defun mouse-sel-region-to-primary (orig-window)
258 "Convert region to PRIMARY overlay and deactivate region.
259 Argument ORIG-WINDOW specifies the window the cursor was in when the
260 originating command was issued, and is used to determine whether the
261 region was visible or not."
262 (if transient-mark-mode
263 (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
264 (cond
265 ((and mark-active
266 (or highlight-nonselected-windows
267 (eq orig-window (selected-window))))
268 ;; Region was visible, so convert region to overlay
269 (move-overlay overlay (region-beginning) (region-end)
270 (current-buffer)))
271 ((eq orig-window (selected-window))
272 ;; Point was visible, so set overlay at point
273 (move-overlay overlay (point) (point) (current-buffer)))
275 ;; Nothing was visible, so remove overlay
276 (delete-overlay overlay)))
277 (setq mark-active nil))))
279 (defun mouse-sel-primary-to-region (&optional direction)
280 "Convert PRIMARY overlay to region.
281 Optional argument DIRECTION specifies the mouse drag direction: a value of
282 1 indicates that the mouse was dragged left-to-right, otherwise it was
283 dragged right-to-left."
284 (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
285 (start (overlay-start overlay))
286 (end (overlay-end overlay)))
287 (if (eq start end)
288 (progn
289 (if start (goto-char start))
290 (deactivate-mark))
291 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
292 (progn
293 (goto-char end)
294 (push-mark start 'nomsg 'active))
295 (goto-char start)
296 (push-mark end 'nomsg 'active)))
297 (if transient-mark-mode (delete-overlay overlay))))
299 (defmacro mouse-sel-eval-at-event-end (event &rest forms)
300 "Evaluate forms at mouse position.
301 Move to the end position of EVENT, execute FORMS, and restore original
302 point and window."
304 (let ((posn (event-end (, event))))
305 (if posn (mouse-minibuffer-check (, event)))
306 (if (and posn (not (windowp (posn-window posn))))
307 (error "Cursor not in text area of window"))
308 (let (orig-window orig-point-marker)
309 (setq orig-window (selected-window))
310 (if posn (select-window (posn-window posn)))
311 (setq orig-point-marker (point-marker))
312 (if (and posn (numberp (posn-point posn)))
313 (goto-char (posn-point posn)))
314 (unwind-protect
315 (progn
316 (,@ forms))
317 (goto-char (marker-position orig-point-marker))
318 (move-marker orig-point-marker nil)
319 (select-window orig-window)
320 )))))
322 (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
324 ;;=== Select ==============================================================
326 (defun mouse-select (event)
327 "Set region/selection using the mouse.
329 Click sets point & mark to click position.
330 Dragging extends region/selection.
332 Multi-clicking selects word/lines/paragraphs, as determined by
333 'mouse-sel-determine-selection-thing.
335 Clicking mouse-2 while selecting copies selected text to the kill-ring.
336 Clicking mouse-1 or mouse-3 kills the selected text.
338 This should be bound to a down-mouse event."
339 (interactive "@e")
340 (let (direction)
341 (unwind-protect
342 (setq direction (mouse-select-internal 'PRIMARY event))
343 (mouse-sel-primary-to-region direction))))
345 (defun mouse-select-secondary (event)
346 "Set secondary selection using the mouse.
348 Click sets the start of the secondary selection to click position.
349 Dragging extends the secondary selection.
351 Multi-clicking selects word/lines/paragraphs, as determined by
352 'mouse-sel-determine-selection-thing.
354 Clicking mouse-2 while selecting copies selected text to the kill-ring.
355 Clicking mouse-1 or mouse-3 kills the selected text.
357 This should be bound to a down-mouse event."
358 (interactive "e")
359 (mouse-select-internal 'SECONDARY event))
361 (defun mouse-select-internal (selection event)
362 "Set SELECTION using the mouse."
363 (mouse-sel-eval-at-event-end event
364 (let ((thing-symbol (mouse-sel-selection-thing selection))
365 (overlay (mouse-sel-selection-overlay selection)))
366 (set thing-symbol
367 (mouse-sel-determine-selection-thing (event-click-count event)))
368 (let ((object-bounds (bounds-of-thing-at-point
369 (symbol-value thing-symbol))))
370 (if object-bounds
371 (progn
372 (move-overlay overlay
373 (car object-bounds) (cdr object-bounds)
374 (current-buffer)))
375 (move-overlay overlay (point) (point) (current-buffer)))))
376 (mouse-extend-internal selection)))
378 ;;=== Extend ==============================================================
380 (defun mouse-extend (event)
381 "Extend region/selection using the mouse."
382 (interactive "e")
383 (let ((orig-window (selected-window))
384 direction)
385 (select-window (posn-window (event-end event)))
386 (unwind-protect
387 (progn
388 (mouse-sel-region-to-primary orig-window)
389 (setq direction (mouse-extend-internal 'PRIMARY event)))
390 (mouse-sel-primary-to-region direction))))
392 (defun mouse-extend-secondary (event)
393 "Extend secondary selection using the mouse."
394 (interactive "e")
395 (save-window-excursion
396 (mouse-extend-internal 'SECONDARY event)))
398 (defun mouse-extend-internal (selection &optional initial-event)
399 "Extend specified SELECTION using the mouse.
400 Track mouse-motion events, adjusting the SELECTION appropriately.
401 Optional argument INITIAL-EVENT specifies an initial down-mouse event to
402 process.
404 See documentation for mouse-select-internal for more details."
405 (mouse-sel-eval-at-event-end initial-event
406 (let ((orig-cursor-type
407 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
408 (unwind-protect
410 (let* ((thing-symbol (mouse-sel-selection-thing selection))
411 (overlay (mouse-sel-selection-overlay selection))
412 (orig-window (selected-window))
413 (orig-window-frame (window-frame orig-window))
414 (top (nth 1 (window-edges orig-window)))
415 (bottom (nth 3 (window-edges orig-window)))
416 (mark-active nil) ; inhibit normal region highlight
417 (echo-keystrokes 0) ; don't echo mouse events
418 min max
419 direction
420 event)
422 ;; Get current bounds of overlay
423 (if (eq (overlay-buffer overlay) (current-buffer))
424 (setq min (overlay-start overlay)
425 max (overlay-end overlay))
426 (setq min (point)
427 max min)
428 (set thing-symbol nil))
431 ;; Bar cursor
432 (if (fboundp 'modify-frame-parameters)
433 (modify-frame-parameters (selected-frame)
434 '((cursor-type . bar))))
436 ;; Handle dragging
437 (track-mouse
439 (while (if initial-event ; Use initial event
440 (prog1
441 (setq event initial-event)
442 (setq initial-event nil))
443 (setq event (read-event))
444 (and (consp event)
445 (memq (car event) '(mouse-movement switch-frame))))
447 (let ((selection-thing (symbol-value thing-symbol))
448 (end (event-end event)))
450 (cond
452 ;; Ignore any movement outside the frame
453 ((eq (car-safe event) 'switch-frame) nil)
454 ((and (posn-window end)
455 (not (eq (let ((posn-w (posn-window end)))
456 (if (windowp posn-w)
457 (window-frame posn-w)
458 posn-w))
459 (window-frame orig-window)))) nil)
461 ;; Different window, same frame
462 ((not (eq (posn-window end) orig-window))
463 (let ((end-row (cdr (cdr (mouse-position)))))
464 (cond
465 ((and end-row (not (bobp)) (< end-row top))
466 (mouse-scroll-subr orig-window (- end-row top)
467 overlay max))
468 ((and end-row (not (eobp)) (>= end-row bottom))
469 (mouse-scroll-subr orig-window (1+ (- end-row bottom))
470 overlay min))
473 ;; On the mode line
474 ((eq (posn-point end) 'mode-line)
475 (mouse-scroll-subr orig-window 1 overlay min))
477 ;; In original window
478 (t (goto-char (posn-point end)))
482 ;; Determine direction of drag
483 (cond
484 ((and (not direction) (not (eq min max)))
485 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
486 ((and (not (eq direction -1)) (<= (point) min))
487 (setq direction -1))
488 ((and (not (eq direction 1)) (>= (point) max))
489 (setq direction 1)))
491 (if (not selection-thing) nil
493 ;; If dragging forward, goal is next character
494 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
496 ;; Move to start/end of selected thing
497 (let ((goal (point)))
498 (goto-char (if (eq 1 direction) min max))
499 (condition-case nil
500 (progn
501 (while (> (* direction (- goal (point))) 0)
502 (forward-thing selection-thing direction))
503 (let ((end (point)))
504 (forward-thing selection-thing (- direction))
505 (goto-char
506 (if (> (* direction (- goal (point))) 0)
507 end (point)))))
508 (error))))
510 ;; Move overlay
511 (move-overlay overlay
512 (if (eq 1 direction) min (point))
513 (if (eq -1 direction) max (point))
514 (current-buffer))
516 ))) ; end track-mouse
518 ;; Finish up after dragging
519 (let ((overlay-start (overlay-start overlay))
520 (overlay-end (overlay-end overlay)))
522 ;; Set selection
523 (if (not (eq overlay-start overlay-end))
524 (mouse-sel-set-selection
525 selection
526 (buffer-substring overlay-start overlay-end)))
528 ;; Handle copy/kill
529 (let (this-command)
530 (cond
531 ((eq (event-basic-type last-input-event) 'mouse-2)
532 (copy-region-as-kill overlay-start overlay-end)
533 (read-event) (read-event))
534 ((and (memq (event-basic-type last-input-event)
535 '(mouse-1 mouse-3))
536 (memq 'down (event-modifiers last-input-event)))
537 (kill-region overlay-start overlay-end)
538 (move-overlay overlay overlay-start overlay-start)
539 (read-event) (read-event))
540 ((and (eq (event-basic-type last-input-event) 'mouse-3)
541 (memq 'double (event-modifiers last-input-event)))
542 (kill-region overlay-start overlay-end)
543 (move-overlay overlay overlay-start overlay-start)))))
545 direction)
547 ;; Restore cursor
548 (if (fboundp 'modify-frame-parameters)
549 (modify-frame-parameters
550 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
552 ))))
554 ;;=== Paste ===============================================================
556 (defun mouse-insert-selection (event)
557 "Insert the contents of the PRIMARY selection at mouse click.
558 If `mouse-yank-at-point' is non-nil, insert at point instead."
559 (interactive "e")
560 (mouse-insert-selection-internal 'PRIMARY event))
562 (defun mouse-insert-secondary (event)
563 "Insert the contents of the SECONDARY selection at mouse click.
564 If `mouse-yank-at-point' is non-nil, insert at point instead."
565 (interactive "e")
566 (mouse-insert-selection-internal 'SECONDARY event))
568 (defun mouse-insert-selection-internal (selection event)
569 "Insert the contents of the named SELECTION at mouse click.
570 If `mouse-yank-at-point' is non-nil, insert at point instead."
571 (or mouse-yank-at-point
572 (mouse-set-point event))
573 (if mouse-sel-get-selection-function
574 (progn
575 (push-mark (point) 'nomsg)
576 (insert (or (funcall mouse-sel-get-selection-function selection) "")))))
578 ;;=== Handle loss of selections ===========================================
580 (defun mouse-sel-lost-selection-hook (selection)
581 "Remove the overlay for a lost selection."
582 (let ((overlay (mouse-sel-selection-overlay selection)))
583 (delete-overlay overlay)))
585 (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)
587 ;;=== Key bindings ========================================================
589 (if (not mouse-sel-default-bindings) nil
591 (global-unset-key [mouse-1])
592 (global-unset-key [drag-mouse-1])
593 (global-unset-key [mouse-3])
595 (global-set-key [down-mouse-1] 'mouse-select)
596 (global-set-key [down-mouse-3] 'mouse-extend)
598 (global-unset-key [M-mouse-1])
599 (global-unset-key [M-drag-mouse-1])
600 (global-unset-key [M-mouse-3])
602 (global-set-key [M-down-mouse-1] 'mouse-select-secondary)
603 (global-set-key [M-down-mouse-3] 'mouse-extend-secondary)
605 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) nil
607 (global-set-key [mouse-2] 'mouse-insert-selection)
609 (setq interprogram-cut-function nil
610 interprogram-paste-function nil))
612 (global-set-key [M-mouse-2] 'mouse-insert-secondary)
616 ;;=== Bug reporting =======================================================
618 (defconst mouse-sel-maintainer-address "mikew@gopher.dosli.govt.nz")
620 (defun mouse-sel-submit-bug-report ()
621 "Submit a bug report on mouse-sel.el via mail."
622 (interactive)
623 (require 'reporter)
624 (reporter-submit-bug-report
625 mouse-sel-maintainer-address
626 (concat "mouse-sel.el "
627 (or (condition-case nil mouse-sel-version (error))
628 "(distributed with Emacs)"))
629 (list 'transient-mark-mode
630 'delete-selection-mode
631 'mouse-sel-default-bindings
632 'mouse-sel-leave-point-near-mouse
633 'mouse-sel-cycle-clicks
634 'mouse-sel-selection-alist
635 'mouse-sel-set-selection-function
636 'mouse-sel-get-selection-function
637 'mouse-yank-at-point)))
639 ;; mouse-sel.el ends here.