(summarize-tar-header-block): Add mouse-face properties.
[emacs.git] / lisp / mouse-sel.el
blobf079c62f9789cc6a1fe69a849dbbad8ed21a6e40
1 ;;; mouse-sel.el --- Multi-click selection support for Emacs 19
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
6 ;; Keywords: mouse
7 ;; Version: 2.1
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;;; Commentary: ===========================================================
23 ;; This module provides multi-click mouse support for GNU Emacs versions
24 ;; 19.18 and later. I've tried to make it behave more like standard X
25 ;; clients (eg. xterm) than the default Emacs 19 mouse selection handlers.
26 ;; Basically:
28 ;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
30 ;; * Clicking or dragging mouse-3 extends the selection as well.
32 ;; * Double-clicking on word constituents selects words.
33 ;; Double-clicking on symbol constituents selects symbols.
34 ;; Double-clicking on quotes or parentheses selects sexps.
35 ;; Double-clicking on whitespace selects whitespace.
36 ;; Triple-clicking selects lines.
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 pastes contents of primary selection at the mouse
44 ;; position.
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.
51 ;; This module requires my thingatpt.el module, which it uses to find the
52 ;; bounds of words, lines, sexps, etc.
54 ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
56 ;;--- Customisation -------------------------------------------------------
58 ;; * You may want to use none or more of following:
60 ;; ;; Enable region highlight
61 ;; (transient-mark-mode 1)
63 ;; ;; But only in the selected window
64 ;; (setq highlight-nonselected-windows nil)
65 ;;
66 ;; ;; Enable pending-delete
67 ;; (delete-selection-mode 1)
69 ;; * You can control the way mouse-sel binds it's keys by setting the value
70 ;; of mouse-sel-default-bindings before loading mouse-sel.
72 ;; (a) If mouse-sel-default-bindings = t (the default)
73 ;;
74 ;; Mouse sets and pastes selection
75 ;; mouse-1 mouse-select
76 ;; mouse-2 mouse-insert-selection
77 ;; mouse-3 mouse-extend
79 ;; Selection/kill-ring interaction is disabled
80 ;; interprogram-cut-function = nil
81 ;; interprogram-paste-function = nil
83 ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
84 ;;
85 ;; Mouse sets selection, and pastes from kill-ring
86 ;; mouse-1 mouse-select
87 ;; mouse-2 mouse-yank-at-click
88 ;; mouse-3 mouse-extend
89 ;;
90 ;; Selection/kill-ring interaction is retained
91 ;; interprogram-cut-function = x-select-text
92 ;; interprogram-paste-function = x-cut-buffer-or-selection-value
93 ;;
94 ;; What you lose is the ability to select some text in
95 ;; delete-selection-mode and yank over the top of it.
96 ;;
97 ;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
99 ;; * By default, mouse-insert-selection (mouse-2) inserts the selection at
100 ;; the mouse position. You can tell it to insert at point instead with:
102 ;; (setq mouse-sel-insert-at-point t)
104 ;; * I like to leave point at the end of the region nearest to where the
105 ;; mouse was, even though this makes region highlighting mis-leading (the
106 ;; cursor makes it look like one extra character is selected). You can
107 ;; disable this behaviour with:
109 ;; (setq mouse-sel-leave-point-near-mouse nil)
111 ;; * Normally, the selection highlight will be removed when the mouse is
112 ;; lifted. You can tell mouse-sel to retain the selection highlight
113 ;; (useful if you don't use transient-mark-mode) with:
115 ;; (setq mouse-sel-retain-highlight t)
117 ;; * By default, mouse-select cycles the click count after 3 clicks. That
118 ;; is, clicking mouse-1 four times has the same effect as clicking it
119 ;; once, clicking five times has the same effect as clicking twice, etc.
120 ;; Disable this behaviour with:
122 ;; (setq mouse-sel-cycle-clicks nil)
124 ;; * The variables mouse-sel-{set,get,check}-selection-function control how
125 ;; the selection is handled. Under X Windows, these variables default so
126 ;; that the X primary selection is used. Under other windowing systems,
127 ;; alternate functions are used, which simply store the selection value
128 ;; in a variable.
130 ;;--- Hints ---------------------------------------------------------------
132 ;; * You can change the selection highlight face by altering the properties
133 ;; of mouse-drag-overlay, eg.
135 ;; (overlay-put mouse-drag-overlay 'face 'bold)
137 ;; * Pasting from the primary selection under emacs 19.19 is SLOW (there's
138 ;; a two second delay). The following code will cause mouse-sel to use
139 ;; the cut buffer rather than the primary selection. However, be aware
140 ;; that cut buffers are OBSOLETE, and some X applications may not support
141 ;; them.
143 ;; (setq mouse-sel-set-selection-function 'x-select-text
144 ;; mouse-sel-get-selection-function 'x-get-cut-buffer)
146 ;;--- Warnings ------------------------------------------------------------
148 ;; * When selecting sexps, the selection extends by sexps at the same
149 ;; nesting level. This also means the selection cannot be extended out
150 ;; of the enclosing nesting level. This is INTENTIONAL.
152 ;;; Code: =================================================================
154 (provide 'mouse-sel)
156 (require 'mouse)
157 (require 'thingatpt)
159 ;;=== Version =============================================================
161 (defconst mouse-sel-version "2.1"
162 "The version number of mouse-sel (as string).")
164 ;;=== User Variables ======================================================
166 (defvar mouse-sel-leave-point-near-mouse t
167 "*Leave point near last mouse position.
168 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
169 of the region nearest to where the mouse last was.
170 If nil, point will always be placed at the beginning of the region.")
172 (defvar mouse-sel-retain-highlight nil
173 "*Retain highlight on mouse-drag-overlay.
174 If non-nil, regions selected using \\[mouse-select] and \\[mouse-extend] will
175 remain highlighted.
176 If nil, highlighting will be turned off when the mouse is lifted.")
178 (defvar mouse-sel-cycle-clicks t
179 "*If non-nil, \\[mouse-select] cycles the click-counts after 3 clicks.
180 Ie. 4 clicks = 1 click, 5 clicks = 2 clicks, etc.")
182 (defvar mouse-sel-insert-at-point nil
183 "*If non-nil, \\[mouse-insert-selection] inserts at point.
184 Normally, \\[mouse-insert-selection] inserts at the mouse position.")
186 (defvar mouse-sel-default-bindings t
187 "Set to nil before loading `mouse-sel' to prevent default mouse bindings.")
189 ;;=== Selection ===========================================================
191 (defvar mouse-sel-selection-type nil "Type of current selection")
192 (make-variable-buffer-local 'mouse-sel-selection-type)
194 (defvar mouse-sel-selection ""
195 "Store the selection value when using a window systems other than X.")
197 (defvar mouse-sel-set-selection-function
198 (if (fboundp 'x-set-selection)
199 (function (lambda (s) (x-set-selection 'PRIMARY s)))
200 (function (lambda (s) (setq mouse-sel-selection s))))
201 "Function to call to set selection.
202 Called with one argument, the text to select.")
204 (defvar mouse-sel-get-selection-function
205 (if (fboundp 'x-get-selection)
206 'x-get-selection
207 (function (lambda () mouse-sel-selection)))
208 "Function to call to get the selection.
209 Called with no argument.")
211 (defvar mouse-sel-check-selection-function
212 (if (fboundp 'x-selection-owner-p)
213 'x-selection-owner-p
214 nil)
215 "Function to check whether emacs still owns the selection.
216 Called with no arguments.")
218 (defun mouse-sel-determine-selection-type (NCLICKS)
219 "Determine what `thing' `mouse-sel' should operate on.
220 The first argument is NCLICKS, is the number of consecutive
221 mouse clicks at the same position."
222 (let* ((next-char (char-after (point)))
223 (char-syntax (if next-char (char-syntax next-char)))
224 (nclicks (if mouse-sel-cycle-clicks (1+ (% (1- NCLICKS) 3)) NCLICKS)))
225 (cond
226 ((= nclicks 1) nil)
227 ((>= nclicks 3) 'line)
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-select (EVENT)
234 "Set region/selection using the mouse.
236 On click, point & mark are set to click position, and mark is disabled.
237 Dragging extends region/selection.
239 Double-clicking on word constituents selects words.
240 Double-clicking on symbol constituents selects symbols.
241 Double-clicking on quotes or parentheses selects sexps.
242 Double-clicking on whitespace selects whitespace.
243 Triple-clicking selects lines.
245 Clicking mouse-2 while selecting copies the region to the kill-ring.
246 Clicking mouse-1 or mouse-3 kills the region.
248 This should be bound to a down-mouse event."
249 (interactive "e")
250 (mouse-set-point EVENT)
251 (setq mouse-sel-selection-type
252 (mouse-sel-determine-selection-type (event-click-count EVENT)))
253 (let ((object-bounds (bounds-of-thing-at-point mouse-sel-selection-type)))
254 (if object-bounds
255 (progn
256 (setq mark-active t)
257 (goto-char (car object-bounds))
258 (set-mark (cdr object-bounds)))
259 (deactivate-mark)))
260 (mouse-extend))
262 (defun mouse-extend (&optional EVENT)
263 "Extend region/selection using the mouse.
265 See documentation for mouse-select for more details.
267 This should be bound to a down-mouse event."
268 (interactive "e")
269 (if EVENT (select-window (posn-window (event-end EVENT))))
270 (let* ((min (if mark-active (region-beginning) (point)))
271 (max (if mark-active (region-end) (point)))
272 (orig-window (selected-window))
273 (orig-window-frame (window-frame orig-window))
274 (top (nth 1 (window-edges orig-window)))
275 (bottom (nth 3 (window-edges orig-window)))
276 (orig-cursor-type
277 (cdr (assoc 'cursor-type (frame-parameters (selected-frame)))))
278 direction
279 event)
281 ;; Inhibit normal region highlight
282 (setq mark-active nil)
284 ;; Highlight region (forcing re-highlight)
285 (move-overlay mouse-drag-overlay min max (current-buffer))
286 (overlay-put mouse-drag-overlay 'face
287 (overlay-get mouse-drag-overlay 'face))
289 ;; Bar cursor
290 (if (fboundp 'modify-frame-parameters)
291 (modify-frame-parameters (selected-frame) '((cursor-type . bar))))
293 ;; Handle dragging
294 (unwind-protect
295 (progn
296 (track-mouse
298 (while (if EVENT ; Use initial event
299 (prog1
300 (setq event EVENT)
301 (setq EVENT nil))
302 (setq event (read-event))
303 (and (consp event)
304 (memq (car event) '(mouse-movement switch-frame))))
306 (let ((end (event-end event)))
308 (cond
310 ;; Ignore any movement outside the frame
311 ((eq (car-safe event) 'switch-frame) nil)
312 ((and (posn-window end)
313 (not (eq (window-frame (posn-window end))
314 (window-frame orig-window)))) nil)
316 ;; Different window, same frame
317 ((not (eq (posn-window end) orig-window))
318 (let ((end-row (cdr (cdr (mouse-position)))))
319 (cond
320 ((and end-row (not (bobp)) (< end-row top))
321 (mouse-scroll-subr (- end-row top)
322 mouse-drag-overlay max))
323 ((and end-row (not (eobp)) (>= end-row bottom))
324 (mouse-scroll-subr (1+ (- end-row bottom))
325 mouse-drag-overlay min))
328 ;; On the mode line
329 ((eq (posn-point end) 'mode-line)
330 (mouse-scroll-subr 1 mouse-drag-overlay min))
332 ;; In original window
333 (t (goto-char (posn-point end)))
337 ;; Determine direction of drag
338 (cond
339 ((and (not direction) (not (eq min max)))
340 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
341 ((and (not (eq direction -1)) (<= (point) min))
342 (setq direction -1))
343 ((and (not (eq direction 1)) (>= (point) max))
344 (setq direction 1)))
346 (if (not mouse-sel-selection-type) nil
348 ;; If dragging forward, goal is next character
349 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
351 ;; Move to start/end of selected thing
352 (let ((goal (point))
353 last)
354 (goto-char (if (eq 1 direction) min max))
355 (condition-case nil
356 (progn
357 (while (> (* direction (- goal (point))) 0)
358 (setq last (point))
359 (forward-thing mouse-sel-selection-type
360 direction))
361 (let ((end (point)))
362 (forward-thing mouse-sel-selection-type
363 (- direction))
364 (goto-char
365 (if (> (* direction (- goal (point))) 0)
366 end last))))
367 (error))))
369 ;; Move overlay
370 (move-overlay mouse-drag-overlay
371 (if (eq 1 direction) min (point))
372 (if (eq -1 direction) max (point))
373 (current-buffer))
375 ))) ; end track-mouse
377 (let ((overlay-start (overlay-start mouse-drag-overlay))
378 (overlay-end (overlay-end mouse-drag-overlay)))
380 ;; Set region
381 (if (eq overlay-start overlay-end)
382 (deactivate-mark)
383 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
384 (progn
385 (set-mark overlay-start)
386 (goto-char overlay-end))
387 (set-mark overlay-end)
388 (goto-char overlay-start)))
390 ;; Set selection
391 (if (and mark-active mouse-sel-set-selection-function)
392 (funcall mouse-sel-set-selection-function
393 (buffer-substring overlay-start overlay-end)))
395 ;; Handle copy/kill
396 (cond
397 ((eq (car-safe last-input-event) 'down-mouse-2)
398 (copy-region-as-kill overlay-start overlay-end)
399 (read-event) (read-event))
400 ((memq (car-safe last-input-event) '(down-mouse-1 down-mouse-3))
401 (kill-region overlay-start overlay-end)
402 (deactivate-mark)
403 (read-event) (read-event))
404 ((eq (car-safe last-input-event) 'double-mouse-3)
405 (kill-region overlay-start overlay-end)
406 (deactivate-mark)))))
408 ;; Restore cursor
409 (if (fboundp 'modify-frame-parameters)
410 (modify-frame-parameters
411 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
413 ;; Remove overlay
414 (or mouse-sel-retain-highlight
415 (delete-overlay mouse-drag-overlay)))))
417 (defun mouse-insert-selection (click)
418 "Insert the contents of the selection at mouse click.
419 If `mouse-sel-insert-at-point' is non-nil, insert at point instead."
420 (interactive "e")
421 (or mouse-sel-insert-at-point
422 (mouse-set-point click))
423 (deactivate-mark)
424 (if mouse-sel-get-selection-function
425 (insert (or (funcall mouse-sel-get-selection-function) ""))))
427 (defun mouse-sel-validate-selection ()
428 "Remove selection highlight if emacs no longer owns the primary selection."
429 (or (not mouse-sel-check-selection-function)
430 (funcall mouse-sel-check-selection-function)
431 (delete-overlay mouse-drag-overlay)))
433 (add-hook 'pre-command-hook 'mouse-sel-validate-selection)
435 ;;=== Key bindings ========================================================
437 (if (not mouse-sel-default-bindings) nil
439 (global-unset-key [mouse-1])
440 (global-unset-key [drag-mouse-1])
441 (global-unset-key [mouse-3])
443 (global-set-key [down-mouse-1] 'mouse-select)
444 (global-set-key [down-mouse-3] 'mouse-extend)
446 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) nil
448 (global-set-key [mouse-2] 'mouse-insert-selection)
449 (setq interprogram-cut-function nil
450 interprogram-paste-function nil))
454 ;; mouse-sel.el ends here.