Mercurial > emacs
annotate lisp/mouse-sel.el @ 5888:0d02ee7ee659
(read_filtered_event): Retry read_char after a buffer change.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Fri, 11 Feb 1994 01:29:24 +0000 |
parents | e1153522d5f1 |
children | 116607f5ce37 |
rev | line source |
---|---|
4934 | 1 ;;; mouse-sel.el --- Multi-click selection support for Emacs 19 |
2 | |
3 ;; Copyright (C) 1993 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz> | |
6 ;; Keywords: mouse | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
7 ;; Version: 2.0 |
4934 | 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 | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
21 ;;; Commentary: =========================================================== |
4934 | 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 ;; | |
43 ;; * Clicking mouse-2 pastes contents of primary selection. | |
44 ;; | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
45 ;; * Pressing mouse-2 while selecting or extending copies selection |
4934 | 46 ;; to the kill ring. Pressing mouse-1 or mouse-3 kills it. |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
47 ;; |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
48 ;; * Double-clicking mouse-3 also kills selection. |
4934 | 49 ;; |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
50 ;; This module requires my thingatpt.el module, which it uses to find the |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
51 ;; bounds of words, lines, sexps, etc. |
4934 | 52 ;; |
53 ;; Thanks to KevinB@bartley.demon.co.uk for his useful input. | |
54 ;; | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
55 ;;--- Customisation ------------------------------------------------------- |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
56 ;; |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
57 ;; * You may want to use none or more of following: |
4934 | 58 ;; |
59 ;; ;; Enable region highlight | |
60 ;; (transient-mark-mode 1) | |
61 ;; | |
62 ;; ;; But only in the selected window | |
63 ;; (setq highlight-nonselected-windows nil) | |
64 ;; | |
65 ;; ;; Enable pending-delete | |
66 ;; (delete-selection-mode 1) | |
67 ;; | |
68 ;; * You can control the way mouse-sel binds it's keys by setting the value | |
69 ;; of mouse-sel-default-bindings before loading mouse-sel. | |
70 ;; | |
71 ;; (a) If mouse-sel-default-bindings = t (the default) | |
72 ;; | |
73 ;; Mouse sets and pastes selection | |
74 ;; mouse-1 mouse-select | |
75 ;; mouse-2 mouse-insert-selection | |
76 ;; mouse-3 mouse-extend | |
77 ;; | |
78 ;; Selection/kill-ring interaction is disabled | |
79 ;; interprogram-cut-function = nil | |
80 ;; interprogram-paste-function = nil | |
81 ;; | |
82 ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste | |
83 ;; | |
84 ;; Mouse sets selection, and pastes from kill-ring | |
85 ;; mouse-1 mouse-select | |
86 ;; mouse-2 mouse-yank-at-click | |
87 ;; mouse-3 mouse-extend | |
88 ;; | |
89 ;; Selection/kill-ring interaction is retained | |
90 ;; interprogram-cut-function = x-select-text | |
91 ;; interprogram-paste-function = x-cut-buffer-or-selection-value | |
92 ;; | |
93 ;; What you lose is the ability to select some text in | |
94 ;; delete-selection-mode and yank over the top of it. | |
95 ;; | |
96 ;; (c) If mouse-sel-default-bindings = nil, no bindings are made. | |
97 ;; | |
98 ;; * I like to leave point at the end of the region nearest to where the | |
99 ;; mouse was, even though this makes region highlighting mis-leading (the | |
100 ;; cursor makes it look like one extra character is selected). You can | |
101 ;; disable this behaviour with: | |
102 ;; | |
103 ;; (setq mouse-sel-leave-point-near-mouse nil) | |
104 ;; | |
105 ;; * Normally, the selection highlight will be removed when the mouse is | |
106 ;; lifted. You can tell mouse-sel to retain the selection highlight | |
107 ;; (useful if you don't use transient-mark-mode) with: | |
108 ;; | |
109 ;; (setq mouse-sel-retain-highlight t) | |
110 ;; | |
111 ;; * By default, mouse-select cycles the click count after 3 clicks. That | |
112 ;; is, clicking mouse-1 four times has the same effect as clicking it | |
113 ;; once, clicking five times has the same effect as clicking twice, etc. | |
114 ;; Disable this behaviour with: | |
115 ;; | |
116 ;; (setq mouse-sel-cycle-clicks nil) | |
117 ;; | |
118 ;; * The variables mouse-sel-{set,get,check}-selection-function control how | |
119 ;; the selection is handled. Under X Windows, these variables default so | |
120 ;; that the X primary selection is used. Under other windowing systems, | |
121 ;; alternate functions are used, which simply store the selection value | |
122 ;; in a variable. | |
123 ;; | |
124 ;;--- Hints --------------------------------------------------------------- | |
125 ;; | |
126 ;; * You can change the selection highlight face by altering the properties | |
127 ;; of mouse-drag-overlay, eg. | |
128 ;; | |
129 ;; (overlay-put mouse-drag-overlay 'face 'bold) | |
130 ;; | |
131 ;; * Pasting from the primary selection under emacs 19.19 is SLOW (there's | |
132 ;; a two second delay). The following code will cause mouse-sel to use | |
133 ;; the cut buffer rather than the primary selection. However, be aware | |
134 ;; that cut buffers are OBSOLETE, and some X applications may not support | |
135 ;; them. | |
136 ;; | |
137 ;; (setq mouse-sel-set-selection-function 'x-select-text | |
138 ;; mouse-sel-get-selection-function 'x-get-cut-buffer) | |
139 ;; | |
140 ;;--- Warnings ------------------------------------------------------------ | |
141 ;; | |
142 ;; * When selecting sexps, the selection extends by sexps at the same | |
143 ;; nesting level. This also means the selection cannot be extended out | |
144 ;; of the enclosing nesting level. This is INTENTIONAL. | |
145 | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
146 ;;; Code: ================================================================= |
4934 | 147 |
148 (provide 'mouse-sel) | |
149 | |
150 (require 'mouse) | |
151 (require 'thingatpt) | |
152 | |
153 ;;=== Version ============================================================= | |
154 | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
155 (defconst mouse-sel-version "2.0" |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
156 "The version number of mouse-sel (as string).") |
4934 | 157 |
158 ;;=== User Variables ====================================================== | |
159 | |
160 (defvar mouse-sel-leave-point-near-mouse t | |
161 "*Leave point near last mouse position. | |
162 If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end | |
163 of the region nearest to where the mouse last was. | |
164 If nil, point will always be placed at the beginning of the region.") | |
165 | |
166 (defvar mouse-sel-retain-highlight nil | |
167 "*Retain highlight on mouse-drag-overlay. | |
168 If non-nil, regions selected using \\[mouse-select] and \\[mouse-extend] will | |
169 remain highlighted. | |
170 If nil, highlighting will be turned off when the mouse is lifted.") | |
171 | |
172 (defvar mouse-sel-cycle-clicks t | |
173 "*If non-nil, \\[mouse-select] cycles the click-counts after 3 clicks. | |
174 Ie. 4 clicks = 1 click, 5 clicks = 2 clicks, etc.") | |
175 | |
176 (defvar mouse-sel-default-bindings t | |
177 "Set to nil before loading mouse-sel to prevent default mouse bindings.") | |
178 | |
179 ;;=== Selection =========================================================== | |
180 | |
181 (defvar mouse-sel-selection-type nil "Type of current selection") | |
182 (make-variable-buffer-local 'mouse-sel-selection-type) | |
183 | |
184 (defvar mouse-sel-selection "" | |
185 "This variable is used to store the selection value when mouse-sel is | |
186 used on windowing systems other than X Windows.") | |
187 | |
188 (defvar mouse-sel-set-selection-function | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
189 (if (fboundp 'x-set-selection) |
4934 | 190 (function (lambda (s) (x-set-selection 'PRIMARY s))) |
191 (function (lambda (s) (setq mouse-sel-selection s)))) | |
192 "Function to call to set selection. | |
193 Called with one argument, the text to select.") | |
194 | |
195 (defvar mouse-sel-get-selection-function | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
196 (if (fboundp 'x-get-selection) |
4934 | 197 'x-get-selection |
198 (function (lambda () mouse-sel-selection))) | |
199 "Function to call to get the selection. | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
200 Called with no argument.") |
4934 | 201 |
202 (defvar mouse-sel-check-selection-function | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
203 (if (fboundp 'x-selection-owner-p) |
4934 | 204 'x-selection-owner-p |
205 nil) | |
206 "Function to check whether emacs still owns the selection. | |
207 Called with no arguments.") | |
208 | |
209 (defun mouse-sel-determine-selection-type (NCLICKS) | |
210 "Determine what `thing' \\[mouse-select] and \\[mouse-extend] should | |
211 select by. The first argument is NCLICKS, is the number of consecutive | |
212 mouse clicks at the same position." | |
213 (let* ((next-char (char-after (point))) | |
214 (char-syntax (if next-char (char-syntax next-char))) | |
215 (nclicks (if mouse-sel-cycle-clicks (1+ (% (1- NCLICKS) 3)) NCLICKS))) | |
216 (cond | |
217 ((= nclicks 1) nil) | |
218 ((>= nclicks 3) 'line) | |
219 ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp) | |
220 ((memq next-char '(? ?\t ?\n)) 'whitespace) | |
221 ((eq char-syntax ?_) 'symbol) | |
222 ((eq char-syntax ?w) 'word)))) | |
223 | |
224 (defun mouse-select (EVENT) | |
225 "Set region/selection using the mouse. | |
226 | |
227 On click, point & mark are set to click position, and mark is disabled. | |
228 Dragging extends region/selection. | |
229 | |
230 Double-clicking on word constituents selects words. | |
231 Double-clicking on symbol constituents selects symbols. | |
232 Double-clicking on quotes or parentheses selects sexps. | |
233 Double-clicking on whitespace selects whitespace. | |
234 Triple-clicking selects lines. | |
235 | |
236 Clicking mouse-2 while selecting copies the region to the kill-ring. | |
237 Clicking mouse-1 or mouse-3 kills the region. | |
238 | |
239 This should be bound to a down-mouse event." | |
240 (interactive "e") | |
241 (mouse-set-point EVENT) | |
242 (setq mouse-sel-selection-type | |
243 (mouse-sel-determine-selection-type (event-click-count EVENT))) | |
244 (let ((object-bounds (bounds-of-thing-at-point mouse-sel-selection-type))) | |
245 (if object-bounds | |
246 (progn | |
247 (setq mark-active t) | |
248 (goto-char (car object-bounds)) | |
249 (set-mark (cdr object-bounds))) | |
250 (deactivate-mark))) | |
251 (mouse-extend)) | |
252 | |
253 (defun mouse-extend (&optional EVENT) | |
254 "Extend region/selection using the mouse. | |
255 | |
256 See documentation for mouse-select for more details. | |
257 | |
258 This should be bound to a down-mouse event." | |
259 (interactive "e") | |
260 (if EVENT (select-window (posn-window (event-end EVENT)))) | |
261 (let* ((min (if mark-active (region-beginning) (point))) | |
262 (max (if mark-active (region-end) (point))) | |
263 (orig-window (selected-window)) | |
264 (orig-window-frame (window-frame orig-window)) | |
265 (top (nth 1 (window-edges orig-window))) | |
266 (bottom (nth 3 (window-edges orig-window))) | |
267 (orig-cursor-type | |
268 (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))) | |
269 direction | |
270 event) | |
271 | |
272 ;; Inhibit normal region highlight | |
273 (setq mark-active nil) | |
274 | |
275 ;; Highlight region (forcing re-highlight) | |
276 (move-overlay mouse-drag-overlay min max (current-buffer)) | |
277 (overlay-put mouse-drag-overlay 'face | |
278 (overlay-get mouse-drag-overlay 'face)) | |
279 | |
280 ;; Bar cursor | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
281 (if (fboundp 'modify-frame-parameters) |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
282 (modify-frame-parameters (selected-frame) '((cursor-type . bar)))) |
4934 | 283 |
284 ;; Handle dragging | |
285 (unwind-protect | |
286 (progn | |
287 (track-mouse | |
288 | |
289 (while (if EVENT ; Use initial event | |
290 (prog1 | |
291 (setq event EVENT) | |
292 (setq EVENT nil)) | |
293 (setq event (read-event)) | |
294 (and (consp event) | |
295 (memq (car event) '(mouse-movement switch-frame)))) | |
296 | |
297 (let ((end (event-end event))) | |
298 | |
299 (cond | |
300 | |
301 ;; Ignore any movement outside the frame | |
302 ((eq (car-safe event) 'switch-frame) nil) | |
303 ((and (posn-window end) | |
304 (not (eq (window-frame (posn-window end)) | |
305 (window-frame orig-window)))) nil) | |
306 | |
307 ;; Different window, same frame | |
308 ((not (eq (posn-window end) orig-window)) | |
309 (let ((end-row (cdr (cdr (mouse-position))))) | |
310 (cond | |
311 ((and end-row (not (bobp)) (< end-row top)) | |
312 (mouse-scroll-subr (- end-row top) | |
313 mouse-drag-overlay max)) | |
314 ((and end-row (not (eobp)) (>= end-row bottom)) | |
315 (mouse-scroll-subr (1+ (- end-row bottom)) | |
316 mouse-drag-overlay min)) | |
317 ))) | |
318 | |
319 ;; On the mode line | |
320 ((eq (posn-point end) 'mode-line) | |
321 (mouse-scroll-subr 1 mouse-drag-overlay min)) | |
322 | |
323 ;; In original window | |
324 (t (goto-char (posn-point end))) | |
325 | |
326 ) | |
327 | |
328 ;; Determine direction of drag | |
329 (cond | |
330 ((and (not direction) (not (eq min max))) | |
331 (setq direction (if (< (point) (/ (+ min max) 2)) -1 1))) | |
332 ((and (not (eq direction -1)) (<= (point) min)) | |
333 (setq direction -1)) | |
334 ((and (not (eq direction 1)) (>= (point) max)) | |
335 (setq direction 1))) | |
336 | |
337 (if (not mouse-sel-selection-type) nil | |
338 | |
339 ;; If dragging forward, goal is next character | |
340 (if (and (eq direction 1) (not (eobp))) (forward-char 1)) | |
341 | |
342 ;; Move to start/end of selected thing | |
343 (let ((goal (point)) | |
344 last) | |
345 (goto-char (if (eq 1 direction) min max)) | |
346 (condition-case nil | |
347 (progn | |
348 (while (> (* direction (- goal (point))) 0) | |
349 (setq last (point)) | |
350 (forward-thing mouse-sel-selection-type | |
351 direction)) | |
352 (let ((end (point))) | |
353 (forward-thing mouse-sel-selection-type | |
354 (- direction)) | |
355 (goto-char | |
356 (if (> (* direction (- goal (point))) 0) | |
357 end last)))) | |
358 (error)))) | |
359 | |
360 ;; Move overlay | |
361 (move-overlay mouse-drag-overlay | |
362 (if (eq 1 direction) min (point)) | |
363 (if (eq -1 direction) max (point)) | |
364 (current-buffer)) | |
365 | |
366 ))) ; end track-mouse | |
367 | |
368 (let ((overlay-start (overlay-start mouse-drag-overlay)) | |
369 (overlay-end (overlay-end mouse-drag-overlay))) | |
370 | |
371 ;; Set region | |
372 (if (eq overlay-start overlay-end) | |
373 (deactivate-mark) | |
374 (if (and mouse-sel-leave-point-near-mouse (eq direction 1)) | |
375 (progn | |
376 (set-mark overlay-start) | |
377 (goto-char overlay-end)) | |
378 (set-mark overlay-end) | |
379 (goto-char overlay-start))) | |
380 | |
381 ;; Set selection | |
382 (if (and mark-active mouse-sel-set-selection-function) | |
383 (funcall mouse-sel-set-selection-function | |
384 (buffer-substring overlay-start overlay-end))) | |
385 | |
386 ;; Handle copy/kill | |
387 (cond | |
388 ((eq (car-safe last-input-event) 'down-mouse-2) | |
389 (copy-region-as-kill overlay-start overlay-end) | |
390 (read-event) (read-event)) | |
391 ((memq (car-safe last-input-event) '(down-mouse-1 down-mouse-3)) | |
392 (kill-region overlay-start overlay-end) | |
393 (deactivate-mark) | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
394 (read-event) (read-event)) |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
395 ((eq (car-safe last-input-event) 'double-mouse-3) |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
396 (kill-region overlay-start overlay-end) |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
397 (deactivate-mark))))) |
4934 | 398 |
399 ;; Restore cursor | |
5750
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
400 (if (fboundp 'modify-frame-parameters) |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
401 (modify-frame-parameters |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
402 (selected-frame) (list (cons 'cursor-type orig-cursor-type)))) |
e1153522d5f1
(mouse-sel-version): Don't base version number on
Richard M. Stallman <rms@gnu.org>
parents:
4934
diff
changeset
|
403 |
4934 | 404 ;; Remove overlay |
405 (or mouse-sel-retain-highlight | |
406 (delete-overlay mouse-drag-overlay))))) | |
407 | |
408 (defun mouse-insert-selection (click) | |
409 "Insert the contents of the selection at mouse click." | |
410 (interactive "e") | |
411 (mouse-set-point click) | |
412 (deactivate-mark) | |
413 (if mouse-sel-get-selection-function | |
414 (insert (or (funcall mouse-sel-get-selection-function) "")))) | |
415 | |
416 (defun mouse-sel-validate-selection () | |
417 "Remove selection highlight if emacs no longer owns the primary selection." | |
418 (or (not mouse-sel-check-selection-function) | |
419 (funcall mouse-sel-check-selection-function) | |
420 (delete-overlay mouse-drag-overlay))) | |
421 | |
422 (add-hook 'pre-command-hook 'mouse-sel-validate-selection) | |
423 | |
424 ;;=== Key bindings ======================================================== | |
425 | |
426 (if (not mouse-sel-default-bindings) nil | |
427 | |
428 (global-unset-key [mouse-1]) | |
429 (global-unset-key [drag-mouse-1]) | |
430 (global-unset-key [mouse-3]) | |
431 | |
432 (global-set-key [down-mouse-1] 'mouse-select) | |
433 (global-set-key [down-mouse-3] 'mouse-extend) | |
434 | |
435 (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) nil | |
436 | |
437 (global-set-key [mouse-2] 'mouse-insert-selection) | |
438 (setq interprogram-cut-function nil | |
439 interprogram-paste-function nil)) | |
440 | |
441 ) | |
442 | |
443 ;; mouse-sel.el ends here. |