Mercurial > emacs
comparison lisp/emulation/viper-mous.el @ 10789:af7c0bb1059f
Initial revision
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Mon, 20 Feb 1995 23:15:10 +0000 |
parents | |
children | d1f620273acb |
comparison
equal
deleted
inserted
replaced
10788:61c7b37349d2 | 10789:af7c0bb1059f |
---|---|
1 ;;; viper-mous.el -- Mouse support for Viper | |
2 | |
3 ;; This file is part of GNU Emacs. | |
4 | |
5 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
6 ;; it under the terms of the GNU General Public License as published by | |
7 ;; the Free Software Foundation; either version 2, or (at your option) | |
8 ;; any later version. | |
9 | |
10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 ;; GNU General Public License for more details. | |
14 | |
15 ;; You should have received a copy of the GNU General Public License | |
16 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
17 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | |
19 | |
20 (require 'viper-util) | |
21 | |
22 | |
23 ;;; Variables | |
24 | |
25 ;; Variable used for catching the switch-frame event. | |
26 ;; If non-nil, indicates that previous-frame should be the selected | |
27 ;; one. Used by vip-mouse-click-get-word. Not a user option. | |
28 (defvar vip-frame-of-focus nil) | |
29 | |
30 ;; Frame that was selected before the switch-frame event. | |
31 (defconst vip-pre-click-frame (vip-selected-frame)) | |
32 | |
33 (defvar vip-surrounding-word-function 'vip-surrounding-word | |
34 "*Function that determines what constitutes a word for clicking events. | |
35 Takes two parameters: a COUNT, indicating how many words to return, | |
36 and CLICK-COUNT, telling whether this is the first click, a double-click, | |
37 or a tripple-click.") | |
38 | |
39 ;; time interval in millisecond within which successive clicks are | |
40 ;; considered related | |
41 (defconst vip-multiclick-timeout (if vip-xemacs-p | |
42 500 | |
43 double-click-time) | |
44 "*Time interval in millisecond within which successive clicks are | |
45 considered related.") | |
46 | |
47 ;; current event click count; XEmacs only | |
48 (defvar vip-current-click-count 0) | |
49 ;; time stamp of the last click event; XEmacs only | |
50 (defvar vip-last-click-event-timestamp 0) | |
51 | |
52 ;; Local variable used to toggle wraparound search on click. | |
53 (vip-deflocalvar vip-mouse-click-search-noerror t) | |
54 | |
55 ;; Local variable used to delimit search after wraparound. | |
56 (vip-deflocalvar vip-mouse-click-search-limit nil) | |
57 | |
58 ;; remembers prefix argument to pass along to commands invoked by second | |
59 ;; click. | |
60 ;; This is needed because in Emacs (not XEmacs), assigning to preix-arg | |
61 ;; causes Emacs to count the second click as if it was a single click | |
62 (defvar vip-global-prefix-argument nil) | |
63 | |
64 | |
65 | |
66 ;;; Code | |
67 | |
68 (defun vip-multiclick-p () | |
69 (not (vip-sit-for-short vip-multiclick-timeout t))) | |
70 | |
71 (defun vip-surrounding-word (count click-count) | |
72 "Returns word surrounding point according to a heuristic. | |
73 COUNT indicates how many regions to return. | |
74 If CLICK-COUNT is 1, `word' is a word in Vi sense. If it is > 1, | |
75 then `word' is a Word in Vi sense. | |
76 If the character clicked on is a non-separator and is non-alphanumeric but | |
77 is adjacent to an alphanumeric symbol, then it is considered alphanumeric | |
78 for the purpose of this command. If this character has a matching | |
79 character, such as `\(' is a match for `\)', then the matching character is | |
80 also considered alphanumeric. | |
81 For convenience, in Lisp modes, `-' is considered alphanumeric." | |
82 (let* ((basic-alpha "_a-zA-Z0-9") ;; it is important for `_' to come first | |
83 (basic-alpha-B "[_a-zA-Z0-9]") | |
84 (basic-nonalphasep-B vip-NONALPHASEP-B) | |
85 (end-modifiers "") | |
86 (start-modifiers "") | |
87 vip-ALPHA vip-ALPHA-B | |
88 vip-NONALPHA vip-NONALPHA-B | |
89 vip-ALPHASEP vip-ALPHASEP-B | |
90 vip-NONALPHASEP vip-NONALPHASEP-B | |
91 skip-flag | |
92 one-char-word-func word-function-forw word-function-back word-beg) | |
93 | |
94 (if (and (looking-at basic-nonalphasep-B) | |
95 (or (save-excursion (vip-backward-char-carefully) | |
96 (looking-at basic-alpha-B)) | |
97 (save-excursion (vip-forward-char-carefully) | |
98 (looking-at basic-alpha-B)))) | |
99 (setq start-modifiers | |
100 (cond ((looking-at "\\\\") "\\\\") | |
101 ((looking-at "-") "") | |
102 ((looking-at "[][]") "][") | |
103 ((looking-at "[()]") ")(") | |
104 ((looking-at "[{}]") "{}") | |
105 ((looking-at "[<>]") "<>") | |
106 ((looking-at "[`']") "`'") | |
107 ((looking-at "\\^") "") | |
108 ((looking-at vip-SEP-B) "") | |
109 (t (char-to-string (following-char)))) | |
110 end-modifiers | |
111 (cond ((looking-at "-") "C-C-") ;; note the C-C trick | |
112 ((looking-at "\\^") "^") | |
113 (t "")))) | |
114 | |
115 ;; Add `-' to alphanum, if it wasn't added and in we are in Lisp | |
116 (or (looking-at "-") | |
117 (not (string-match "lisp" (symbol-name major-mode))) | |
118 (setq end-modifiers (concat end-modifiers "C-C-"))) | |
119 | |
120 (setq vip-ALPHA | |
121 (format "%s%s%s" start-modifiers basic-alpha end-modifiers) | |
122 vip-ALPHA-B | |
123 (format "[%s%s%s]" start-modifiers basic-alpha end-modifiers) | |
124 vip-NONALPHA (concat "^" vip-ALPHA) | |
125 vip-NONALPHA-B (concat "[" vip-NONALPHA "]") | |
126 vip-ALPHASEP (concat vip-ALPHA vip-SEP) | |
127 vip-ALPHASEP-B | |
128 (format "[%s%s%s%s]" | |
129 start-modifiers basic-alpha vip-SEP end-modifiers) | |
130 vip-NONALPHASEP (format "^%s%s" vip-SEP vip-ALPHA) | |
131 vip-NONALPHASEP-B (format "[^%s%s]" vip-SEP vip-ALPHA) | |
132 ) | |
133 | |
134 (if (> click-count 1) | |
135 (setq one-char-word-func 'vip-one-char-Word-p | |
136 word-function-forw 'vip-end-of-Word | |
137 word-function-back 'vip-backward-Word) | |
138 (setq one-char-word-func 'vip-one-char-word-p | |
139 word-function-forw 'vip-end-of-word | |
140 word-function-back 'vip-backward-word)) | |
141 | |
142 (save-excursion | |
143 (cond ((> click-count 1) (skip-chars-backward vip-NONSEP)) | |
144 ((looking-at vip-ALPHA-B) (skip-chars-backward vip-ALPHA)) | |
145 ((looking-at vip-NONALPHASEP-B) | |
146 (skip-chars-backward vip-NONALPHASEP)) | |
147 (t (funcall word-function-back 1))) | |
148 | |
149 (setq word-beg (point)) | |
150 | |
151 (setq skip-flag t) | |
152 (while (> count 0) | |
153 ;; skip-flag and the test for 1-char word takes care of the | |
154 ;; special treatment that vip-end-of-word gives to 1-character | |
155 ;; words. Otherwise, clicking once on such a word will insert two | |
156 ;; words. | |
157 (if (and skip-flag (funcall one-char-word-func)) | |
158 (setq skip-flag (not skip-flag)) | |
159 (funcall word-function-forw 1)) | |
160 (setq count (1- count))) | |
161 | |
162 (vip-forward-char-carefully) | |
163 (buffer-substring word-beg (point))) | |
164 )) | |
165 | |
166 | |
167 (defun vip-mouse-click-get-word (click &optional count click-count) | |
168 "Returns word surrounding the position of a mouse click. | |
169 Click may be in another window. Current window and buffer isn't changed." | |
170 | |
171 (let ((click-word "") | |
172 (click-pos (vip-mouse-click-posn click)) | |
173 (click-buf (vip-mouse-click-window-buffer click))) | |
174 (or (numberp count) (setq count 1)) | |
175 (or (numberp click-count) (setq click-count 1)) | |
176 | |
177 (save-excursion | |
178 (save-window-excursion | |
179 (if click-pos | |
180 (progn | |
181 (set-buffer click-buf) | |
182 | |
183 (goto-char click-pos) | |
184 (setq click-word | |
185 (funcall vip-surrounding-word-function count click-count))) | |
186 (error "Click must be over a window.")) | |
187 click-word)))) | |
188 | |
189 (defun vip-mouse-click-frame (click) | |
190 "Returns window where click occurs." | |
191 (vip-window-frame (vip-mouse-click-window click))) | |
192 | |
193 (defun vip-mouse-click-window (click) | |
194 "Returns window where click occurs." | |
195 (if vip-xemacs-p | |
196 (event-window click) | |
197 (posn-window (event-start click)))) | |
198 | |
199 (defun vip-mouse-click-window-buffer (click) | |
200 "Returns the buffer of the window where click occurs." | |
201 (window-buffer (vip-mouse-click-window click))) | |
202 | |
203 (defun vip-mouse-click-window-buffer-name (click) | |
204 "Returns the name of the buffer in the window where click occurs." | |
205 (buffer-name (vip-mouse-click-window-buffer click))) | |
206 | |
207 (defun vip-mouse-click-posn (click) | |
208 "Returns position of a click." | |
209 (interactive "e") | |
210 (if vip-xemacs-p | |
211 (event-point click) | |
212 (posn-point (event-start click)))) | |
213 | |
214 (defun vip-mouse-click-insert-word (click arg) | |
215 "Insert word clicked or double-clicked on. | |
216 With prefix argument, N, insert that many words. | |
217 This command must be bound to a mouse click. | |
218 The double-click action of the same mouse button must not be bound | |
219 \(or it must be bound to the same function\). | |
220 See `vip-surrounding-word' for the definition of a word in this case." | |
221 (interactive "e\nP") | |
222 (if vip-frame-of-focus ;; to handle clicks in another frame | |
223 (vip-select-frame vip-frame-of-focus)) | |
224 | |
225 ;; turn arg into a number | |
226 (cond ((numberp arg) nil) | |
227 ;; prefix arg is a list when one hits C-u then command | |
228 ((and (listp arg) (numberp (car arg))) | |
229 (setq arg (car arg))) | |
230 (t (setq arg 1))) | |
231 | |
232 (let (click-count interrupting-event) | |
233 (if (and | |
234 (vip-multiclick-p) | |
235 ;; This trick checks if there is a pending mouse event | |
236 ;; if so, we use this latter event and discard the current mouse click | |
237 ;; If the next panding event is not a mouse event, we execute | |
238 ;; the current mouse event | |
239 (progn | |
240 (setq interrupting-event (vip-read-event)) | |
241 (vip-mouse-event-p last-input-event))) | |
242 (progn ;; interrupted wait | |
243 (setq vip-global-prefix-argument arg) | |
244 ;; count this click for XEmacs | |
245 (vip-event-click-count click)) | |
246 ;; uninterrupted wait or the interrupting event wasn't a mouse event | |
247 (setq click-count (vip-event-click-count click)) | |
248 (if (> click-count 1) | |
249 (setq arg vip-global-prefix-argument | |
250 vip-global-prefix-argument nil)) | |
251 (insert (vip-mouse-click-get-word click arg click-count)) | |
252 (if (and interrupting-event | |
253 (eventp interrupting-event) | |
254 (not (vip-mouse-event-p interrupting-event))) | |
255 (vip-set-unread-command-events interrupting-event)) | |
256 ))) | |
257 | |
258 ;; arg is an event. accepts symbols and numbers, too | |
259 (defun vip-mouse-event-p (event) | |
260 (if (eventp event) | |
261 (string-match "\\(mouse-\\|frame\\|screen\\|track\\)" | |
262 (prin1-to-string (vip-event-key event))))) | |
263 | |
264 ;; XEmacs has no double-click events. So, we must simulate. | |
265 ;; So, we have to simulate event-click-count. | |
266 (defun vip-event-click-count (click) | |
267 (if vip-xemacs-p | |
268 (progn | |
269 ;; if more than 1 second | |
270 (if (> (- (event-timestamp click) vip-last-click-event-timestamp) | |
271 vip-multiclick-timeout) | |
272 (setq vip-current-click-count 0)) | |
273 (setq vip-last-click-event-timestamp (event-timestamp click) | |
274 vip-current-click-count (1+ vip-current-click-count))) | |
275 (event-click-count click))) | |
276 | |
277 | |
278 | |
279 (defun vip-mouse-click-search-word (click arg) | |
280 "Find the word clicked or double-clicked on. Word may be in another window. | |
281 With prefix argument, N, search for N-th occurrence. | |
282 This command must be bound to a mouse click. The double-click action of the | |
283 same button must not be bound \(or it must be bound to the same function\). | |
284 See `vip-surrounding-word' for the details on what constitutes a word for | |
285 this command." | |
286 (interactive "e\nP") | |
287 (if vip-frame-of-focus ;; to handle clicks in another frame | |
288 (vip-select-frame vip-frame-of-focus)) | |
289 (let (click-word click-count | |
290 (previous-search-string vip-s-string)) | |
291 | |
292 (if (and | |
293 (vip-multiclick-p) | |
294 ;; This trick checks if there is a pending mouse event | |
295 ;; if so, we use this latter event and discard the current mouse click | |
296 ;; If the next panding event is not a mouse event, we execute | |
297 ;; the current mouse event | |
298 (progn | |
299 (vip-read-event) | |
300 (vip-mouse-event-p last-input-event))) | |
301 (progn ;; interrupted wait | |
302 (setq vip-global-prefix-argument arg) | |
303 ;; remember command that was before the multiclick | |
304 (setq this-command last-command) | |
305 ;; make sure we counted this event---needed for XEmacs only | |
306 (vip-event-click-count click)) | |
307 ;; uninterrupted wait | |
308 (setq click-count (vip-event-click-count click)) | |
309 (setq click-word (vip-mouse-click-get-word click nil click-count)) | |
310 | |
311 (if (> click-count 1) | |
312 (setq arg vip-global-prefix-argument | |
313 vip-global-prefix-argument nil)) | |
314 (setq arg (or arg 1)) | |
315 | |
316 (vip-deactivate-mark) | |
317 (if (or (not (string= click-word vip-s-string)) | |
318 (not (markerp vip-search-start-marker)) | |
319 (not (equal (marker-buffer vip-search-start-marker) | |
320 (current-buffer))) | |
321 (not (eq last-command 'vip-mouse-click-search-word))) | |
322 (progn | |
323 (setq vip-search-start-marker (point-marker) | |
324 vip-local-search-start-marker vip-search-start-marker | |
325 vip-mouse-click-search-noerror t | |
326 vip-mouse-click-search-limit nil) | |
327 | |
328 ;; make search string known to Viper | |
329 (setq vip-s-string (if vip-re-search | |
330 (regexp-quote click-word) | |
331 click-word)) | |
332 (if (not (string= vip-s-string (car vip-search-history))) | |
333 (setq vip-search-history | |
334 (cons vip-s-string vip-search-history))) | |
335 )) | |
336 | |
337 (push-mark nil t) | |
338 (while (> arg 0) | |
339 (vip-forward-word 1) | |
340 (condition-case nil | |
341 (progn | |
342 (if (not (search-forward click-word vip-mouse-click-search-limit | |
343 vip-mouse-click-search-noerror)) | |
344 (progn | |
345 (setq vip-mouse-click-search-noerror nil) | |
346 (setq vip-mouse-click-search-limit | |
347 (save-excursion | |
348 (if (and | |
349 (markerp vip-local-search-start-marker) | |
350 (marker-buffer vip-local-search-start-marker)) | |
351 (goto-char vip-local-search-start-marker)) | |
352 (vip-line-pos 'end))) | |
353 | |
354 (goto-char (point-min)) | |
355 (search-forward click-word | |
356 vip-mouse-click-search-limit nil))) | |
357 (goto-char (match-beginning 0)) | |
358 (message "Searching for: %s" vip-s-string) | |
359 (if (<= arg 1) | |
360 (vip-flash-search-pattern)) | |
361 ) | |
362 (error (beep 1) | |
363 (if (or (not (string= click-word previous-search-string)) | |
364 (not (eq last-command 'vip-mouse-click-search-word))) | |
365 (message "`%s': String not found in %s" | |
366 vip-s-string (buffer-name (current-buffer))) | |
367 (message | |
368 "`%s': Last occurrence in %s. Back to beginning of search" | |
369 click-word (buffer-name (current-buffer))) | |
370 (setq arg 1) ;; to terminate the loop | |
371 (sit-for 2)) | |
372 (setq vip-mouse-click-search-noerror t) | |
373 (setq vip-mouse-click-search-limit nil) | |
374 (if (and (markerp vip-local-search-start-marker) | |
375 (marker-buffer vip-local-search-start-marker)) | |
376 (goto-char vip-local-search-start-marker)))) | |
377 (setq arg (1- arg))) | |
378 ))) | |
379 | |
380 (defun vip-mouse-catch-frame-switch (event arg) | |
381 "Catch the event of switching frame. | |
382 Usually is bound to a 'down-mouse' event to work properly. See sample | |
383 bindings in viper.el and in the Viper manual." | |
384 (interactive "e\nP") | |
385 (setq vip-frame-of-focus nil) | |
386 ;; pass prefix arg along to vip-mouse-click-search/insert-word | |
387 (setq prefix-arg arg) | |
388 (if (eq last-command 'handle-switch-frame) | |
389 (setq vip-frame-of-focus vip-pre-click-frame)) | |
390 ;; make Emacs forget that it executed vip-mouse-catch-frame-switch | |
391 (setq this-command last-command)) | |
392 | |
393 ;; Called just before switching frames. Saves the old selected frame. | |
394 ;; Sets last-command to handle-switch-frame (this is done automatically in | |
395 ;; Emacs. | |
396 ;; The semantics of switching frames is different in Emacs and XEmacs. | |
397 ;; In Emacs, if you select-frame A while mouse is over frame B and then | |
398 ;; start typing, input goes to frame B, which becomes selected. | |
399 ;; In XEmacs, input will go to frame A. This may be a bug in one of the | |
400 ;; Emacsen, but also may be a design decision. | |
401 ;; Also, in Emacs sending input to frame B generates handle-switch-frame | |
402 ;; event, while in XEmacs it doesn't. | |
403 ;; All this accounts for the difference in the behavior of | |
404 ;; vip-mouse-click-* commands when you click in a frame other than the one | |
405 ;; that was the last to receive input. In Emacs, focus will be in frame A | |
406 ;; until you do something other than vip-mouse-click-* command. | |
407 ;; In XEmacs, you have to manually select frame B (with the mouse click) in | |
408 ;; order to shift focus to frame B. | |
409 (defun vip-save-pre-click-frame (frame) | |
410 (setq last-command 'handle-switch-frame) | |
411 (setq vip-pre-click-frame (vip-selected-frame))) | |
412 | |
413 | |
414 (cond (window-system | |
415 (let* ((search-key (if vip-xemacs-p [(meta button1up)] [S-mouse-1])) | |
416 (search-key-catch (if vip-xemacs-p | |
417 [(meta button1)] [S-down-mouse-1])) | |
418 (insert-key (if vip-xemacs-p [(meta button2up)] [S-mouse-2])) | |
419 (insert-key-catch (if vip-xemacs-p | |
420 [(meta button2)] [S-down-mouse-2])) | |
421 (search-key-unbound (and (not (key-binding search-key)) | |
422 (not (key-binding search-key-catch)))) | |
423 (insert-key-unbound (and (not (key-binding insert-key)) | |
424 (not (key-binding insert-key-catch)))) | |
425 ) | |
426 | |
427 (if search-key-unbound | |
428 (global-set-key search-key 'vip-mouse-click-search-word)) | |
429 (if insert-key-unbound | |
430 (global-set-key insert-key 'vip-mouse-click-insert-word)) | |
431 | |
432 ;; The following would be needed if you want to use the above two | |
433 ;; while clicking in another frame. If you only want to use them | |
434 ;; by clicking in another window, not frame, the bindings below | |
435 ;; aren't necessary. | |
436 | |
437 ;; These must be bound to mouse-down event for the same mouse | |
438 ;; buttons as 'vip-mouse-click-search-word and | |
439 ;; 'vip-mouse-click-insert-word | |
440 (if search-key-unbound | |
441 (global-set-key search-key-catch 'vip-mouse-catch-frame-switch)) | |
442 (if insert-key-unbound | |
443 (global-set-key insert-key-catch 'vip-mouse-catch-frame-switch)) | |
444 | |
445 (if vip-xemacs-p | |
446 (add-hook 'mouse-leave-screen-hook | |
447 'vip-save-pre-click-frame) | |
448 (defadvice handle-switch-frame (before vip-frame-advice activate) | |
449 "Remember the selected frame before the switch-frame event." | |
450 (vip-save-pre-click-frame (vip-selected-frame)))) | |
451 ))) | |
452 | |
453 | |
454 | |
455 (provide 'viper-mous) | |
456 | |
457 ;;; viper-mous.el ends here |