(hack-one-local-variable): Check properly for `defun.
[bpt/emacs.git] / lisp / mouse-sel.el
CommitLineData
1a2b6c52
RS
1;;; mouse-sel.el --- Multi-click selection support for Emacs 19
2
d733c5ec 3;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
1a2b6c52
RS
4
5;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
6;; Keywords: mouse
cd1f32a6 7;; Version: 2.1
1a2b6c52
RS
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
6a2e3631 21;;; Commentary: ===========================================================
1a2b6c52
RS
22;;
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:
27;;
28;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
29;;
30;; * Clicking or dragging mouse-3 extends the selection as well.
31;;
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.
37;;
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.
42;;
cd1f32a6
RS
43;; * Clicking mouse-2 pastes contents of primary selection at the mouse
44;; position.
1a2b6c52 45;;
6a2e3631 46;; * Pressing mouse-2 while selecting or extending copies selection
1a2b6c52 47;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
6a2e3631
RS
48;;
49;; * Double-clicking mouse-3 also kills selection.
1a2b6c52 50;;
6a2e3631
RS
51;; This module requires my thingatpt.el module, which it uses to find the
52;; bounds of words, lines, sexps, etc.
1a2b6c52
RS
53;;
54;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
55;;
6a2e3631
RS
56;;--- Customisation -------------------------------------------------------
57;;
58;; * You may want to use none or more of following:
1a2b6c52
RS
59;;
60;; ;; Enable region highlight
61;; (transient-mark-mode 1)
62;;
63;; ;; But only in the selected window
64;; (setq highlight-nonselected-windows nil)
65;;
66;; ;; Enable pending-delete
67;; (delete-selection-mode 1)
68;;
1a2b6c52
RS
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.
71;;
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
78;;
79;; Selection/kill-ring interaction is disabled
80;; interprogram-cut-function = nil
81;; interprogram-paste-function = nil
82;;
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.
98;;
cd1f32a6
RS
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:
101;;
102;; (setq mouse-sel-insert-at-point t)
103;;
1a2b6c52
RS
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:
108;;
109;; (setq mouse-sel-leave-point-near-mouse nil)
110;;
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:
114;;
115;; (setq mouse-sel-retain-highlight t)
116;;
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:
121;;
122;; (setq mouse-sel-cycle-clicks nil)
123;;
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.
129;;
130;;--- Hints ---------------------------------------------------------------
131;;
132;; * You can change the selection highlight face by altering the properties
133;; of mouse-drag-overlay, eg.
134;;
135;; (overlay-put mouse-drag-overlay 'face 'bold)
136;;
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.
142;;
143;; (setq mouse-sel-set-selection-function 'x-select-text
144;; mouse-sel-get-selection-function 'x-get-cut-buffer)
145;;
146;;--- Warnings ------------------------------------------------------------
147;;
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.
151
6a2e3631 152;;; Code: =================================================================
1a2b6c52
RS
153
154(provide 'mouse-sel)
155
156(require 'mouse)
157(require 'thingatpt)
158
159;;=== Version =============================================================
160
cd1f32a6 161(defconst mouse-sel-version "2.1"
6a2e3631 162 "The version number of mouse-sel (as string).")
1a2b6c52
RS
163
164;;=== User Variables ======================================================
165
166(defvar mouse-sel-leave-point-near-mouse t
167 "*Leave point near last mouse position.
168If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
169of the region nearest to where the mouse last was.
170If nil, point will always be placed at the beginning of the region.")
171
172(defvar mouse-sel-retain-highlight nil
173 "*Retain highlight on mouse-drag-overlay.
174If non-nil, regions selected using \\[mouse-select] and \\[mouse-extend] will
175remain highlighted.
176If nil, highlighting will be turned off when the mouse is lifted.")
177
178(defvar mouse-sel-cycle-clicks t
179 "*If non-nil, \\[mouse-select] cycles the click-counts after 3 clicks.
180Ie. 4 clicks = 1 click, 5 clicks = 2 clicks, etc.")
181
cd1f32a6
RS
182(defvar mouse-sel-insert-at-point nil
183 "*If non-nil, \\[mouse-insert-selection] inserts at point.
184Normally, \\[mouse-insert-selection] inserts at the mouse position.")
185
1a2b6c52 186(defvar mouse-sel-default-bindings t
cd1f32a6 187 "Set to nil before loading `mouse-sel' to prevent default mouse bindings.")
1a2b6c52
RS
188
189;;=== Selection ===========================================================
190
191(defvar mouse-sel-selection-type nil "Type of current selection")
192(make-variable-buffer-local 'mouse-sel-selection-type)
193
194(defvar mouse-sel-selection ""
cd1f32a6 195 "Store the selection value when using a window systems other than X.")
1a2b6c52
RS
196
197(defvar mouse-sel-set-selection-function
6a2e3631 198 (if (fboundp 'x-set-selection)
1a2b6c52
RS
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.
202Called with one argument, the text to select.")
203
204(defvar mouse-sel-get-selection-function
6a2e3631 205 (if (fboundp 'x-get-selection)
1a2b6c52
RS
206 'x-get-selection
207 (function (lambda () mouse-sel-selection)))
208 "Function to call to get the selection.
6a2e3631 209Called with no argument.")
1a2b6c52
RS
210
211(defvar mouse-sel-check-selection-function
6a2e3631 212 (if (fboundp 'x-selection-owner-p)
1a2b6c52
RS
213 'x-selection-owner-p
214 nil)
215 "Function to check whether emacs still owns the selection.
216Called with no arguments.")
217
218(defun mouse-sel-determine-selection-type (NCLICKS)
cd1f32a6
RS
219 "Determine what `thing' `mouse-sel' should operate on.
220The first argument is NCLICKS, is the number of consecutive
1a2b6c52
RS
221mouse 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))))
232
233(defun mouse-select (EVENT)
234 "Set region/selection using the mouse.
235
236On click, point & mark are set to click position, and mark is disabled.
237Dragging extends region/selection.
238
239Double-clicking on word constituents selects words.
240Double-clicking on symbol constituents selects symbols.
241Double-clicking on quotes or parentheses selects sexps.
242Double-clicking on whitespace selects whitespace.
243Triple-clicking selects lines.
244
245Clicking mouse-2 while selecting copies the region to the kill-ring.
246Clicking mouse-1 or mouse-3 kills the region.
247
248This 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))
261
262(defun mouse-extend (&optional EVENT)
263 "Extend region/selection using the mouse.
264
265See documentation for mouse-select for more details.
266
267This 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)
280
281 ;; Inhibit normal region highlight
282 (setq mark-active nil)
283
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))
288
289 ;; Bar cursor
6a2e3631
RS
290 (if (fboundp 'modify-frame-parameters)
291 (modify-frame-parameters (selected-frame) '((cursor-type . bar))))
1a2b6c52
RS
292
293 ;; Handle dragging
294 (unwind-protect
295 (progn
296 (track-mouse
297
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))))
305
306 (let ((end (event-end event)))
307
308 (cond
309
310 ;; Ignore any movement outside the frame
311 ((eq (car-safe event) 'switch-frame) nil)
312 ((and (posn-window end)
aa718a1f
RS
313 (not (eq (let ((posn-w (posn-window end)))
314 (if (windowp posn-w)
315 (window-frame posn-w)
316 posn-w))
1a2b6c52
RS
317 (window-frame orig-window)))) nil)
318
319 ;; Different window, same frame
320 ((not (eq (posn-window end) orig-window))
321 (let ((end-row (cdr (cdr (mouse-position)))))
322 (cond
323 ((and end-row (not (bobp)) (< end-row top))
324 (mouse-scroll-subr (- end-row top)
325 mouse-drag-overlay max))
326 ((and end-row (not (eobp)) (>= end-row bottom))
327 (mouse-scroll-subr (1+ (- end-row bottom))
328 mouse-drag-overlay min))
329 )))
330
331 ;; On the mode line
332 ((eq (posn-point end) 'mode-line)
333 (mouse-scroll-subr 1 mouse-drag-overlay min))
334
335 ;; In original window
336 (t (goto-char (posn-point end)))
337
338 )
339
340 ;; Determine direction of drag
341 (cond
342 ((and (not direction) (not (eq min max)))
343 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
344 ((and (not (eq direction -1)) (<= (point) min))
345 (setq direction -1))
346 ((and (not (eq direction 1)) (>= (point) max))
347 (setq direction 1)))
348
349 (if (not mouse-sel-selection-type) nil
350
351 ;; If dragging forward, goal is next character
352 (if (and (eq direction 1) (not (eobp))) (forward-char 1))
353
354 ;; Move to start/end of selected thing
355 (let ((goal (point))
356 last)
357 (goto-char (if (eq 1 direction) min max))
358 (condition-case nil
359 (progn
360 (while (> (* direction (- goal (point))) 0)
361 (setq last (point))
362 (forward-thing mouse-sel-selection-type
363 direction))
364 (let ((end (point)))
365 (forward-thing mouse-sel-selection-type
366 (- direction))
367 (goto-char
368 (if (> (* direction (- goal (point))) 0)
369 end last))))
370 (error))))
371
372 ;; Move overlay
373 (move-overlay mouse-drag-overlay
374 (if (eq 1 direction) min (point))
375 (if (eq -1 direction) max (point))
376 (current-buffer))
377
378 ))) ; end track-mouse
379
380 (let ((overlay-start (overlay-start mouse-drag-overlay))
381 (overlay-end (overlay-end mouse-drag-overlay)))
382
383 ;; Set region
384 (if (eq overlay-start overlay-end)
385 (deactivate-mark)
386 (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
387 (progn
388 (set-mark overlay-start)
389 (goto-char overlay-end))
390 (set-mark overlay-end)
391 (goto-char overlay-start)))
392
393 ;; Set selection
394 (if (and mark-active mouse-sel-set-selection-function)
395 (funcall mouse-sel-set-selection-function
396 (buffer-substring overlay-start overlay-end)))
397
398 ;; Handle copy/kill
399 (cond
400 ((eq (car-safe last-input-event) 'down-mouse-2)
401 (copy-region-as-kill overlay-start overlay-end)
402 (read-event) (read-event))
403 ((memq (car-safe last-input-event) '(down-mouse-1 down-mouse-3))
404 (kill-region overlay-start overlay-end)
405 (deactivate-mark)
6a2e3631
RS
406 (read-event) (read-event))
407 ((eq (car-safe last-input-event) 'double-mouse-3)
408 (kill-region overlay-start overlay-end)
409 (deactivate-mark)))))
1a2b6c52
RS
410
411 ;; Restore cursor
6a2e3631
RS
412 (if (fboundp 'modify-frame-parameters)
413 (modify-frame-parameters
414 (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
415
1a2b6c52
RS
416 ;; Remove overlay
417 (or mouse-sel-retain-highlight
418 (delete-overlay mouse-drag-overlay)))))
419
420(defun mouse-insert-selection (click)
cd1f32a6
RS
421 "Insert the contents of the selection at mouse click.
422If `mouse-sel-insert-at-point' is non-nil, insert at point instead."
1a2b6c52 423 (interactive "e")
cd1f32a6
RS
424 (or mouse-sel-insert-at-point
425 (mouse-set-point click))
1a2b6c52
RS
426 (deactivate-mark)
427 (if mouse-sel-get-selection-function
428 (insert (or (funcall mouse-sel-get-selection-function) ""))))
429
430(defun mouse-sel-validate-selection ()
431 "Remove selection highlight if emacs no longer owns the primary selection."
432 (or (not mouse-sel-check-selection-function)
433 (funcall mouse-sel-check-selection-function)
434 (delete-overlay mouse-drag-overlay)))
435
436(add-hook 'pre-command-hook 'mouse-sel-validate-selection)
437
438;;=== Key bindings ========================================================
439
440(if (not mouse-sel-default-bindings) nil
441
442 (global-unset-key [mouse-1])
443 (global-unset-key [drag-mouse-1])
444 (global-unset-key [mouse-3])
445
446 (global-set-key [down-mouse-1] 'mouse-select)
447 (global-set-key [down-mouse-3] 'mouse-extend)
448
449 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) nil
450
451 (global-set-key [mouse-2] 'mouse-insert-selection)
452 (setq interprogram-cut-function nil
453 interprogram-paste-function nil))
454
455 )
456
457;; mouse-sel.el ends here.