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