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