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