1 ;;; viper-mous.el --- mouse support for Viper
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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)
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.
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.
27 (defvar double-click-time
)
28 (defvar mouse-track-multi-click-time
)
29 (defvar viper-search-start-marker
)
30 (defvar viper-local-search-start-marker
)
31 (defvar viper-search-history
)
32 (defvar viper-s-string
)
33 (defvar viper-re-search
)
35 ;; loading happens only in non-interactive compilation
36 ;; in order to spare non-viperized emacs from being viperized
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
))
50 (defgroup viper-mouse nil
51 "Support for Viper special mouse-bound commands"
58 ;; Variable used for catching the switch-frame event.
59 ;; If non-nil, indicates that previous-frame should be the selected
60 ;; one. Used by viper-mouse-click-get-word. Not a user option.
61 (defvar viper-frame-of-focus nil
)
63 ;; Frame that was selected before the switch-frame event.
64 (defconst viper-current-frame-saved
(selected-frame))
66 (defcustom viper-surrounding-word-function
'viper-surrounding-word
67 "*Function that determines what constitutes a word for clicking events.
68 Takes two parameters: a COUNT, indicating how many words to return,
69 and CLICK-COUNT, telling whether this is the first click, a double-click,
74 ;; time interval in millisecond within which successive clicks are
76 (defcustom viper-multiclick-timeout
(if (viper-window-display-p)
78 mouse-track-multi-click-time
81 "*Time interval in millisecond within which successive mouse clicks are
86 ;; current event click count; XEmacs only
87 (defvar viper-current-click-count
0)
88 ;; time stamp of the last click event; XEmacs only
89 (defvar viper-last-click-event-timestamp
0)
91 ;; Local variable used to toggle wraparound search on click.
92 (viper-deflocalvar viper-mouse-click-search-noerror t
)
94 ;; Local variable used to delimit search after wraparound.
95 (viper-deflocalvar viper-mouse-click-search-limit nil
)
97 ;; remembers prefix argument to pass along to commands invoked by second
99 ;; This is needed because in Emacs (not XEmacs), assigning to preix-arg
100 ;; causes Emacs to count the second click as if it was a single click
101 (defvar viper-global-prefix-argument nil
)
104 ;; same keys, but parsed
105 (defvar viper-mouse-up-search-key-parsed nil
)
106 (defvar viper-mouse-down-search-key-parsed nil
)
107 (defvar viper-mouse-up-insert-key-parsed nil
)
108 (defvar viper-mouse-down-insert-key-parsed nil
)
115 (defsubst viper-multiclick-p
()
116 (not (viper-sit-for-short viper-multiclick-timeout t
)))
118 ;; Returns window where click occurs
119 (defun viper-mouse-click-window (click)
120 (let ((win (if viper-xemacs-p
122 (posn-window (event-start click
)))))
123 (if (window-live-p win
)
125 (error "Click was not over a live window"))))
127 ;; Returns window where click occurs
128 (defsubst viper-mouse-click-frame
(click)
129 (window-frame (viper-mouse-click-window click
)))
131 ;; Returns the buffer of the window where click occurs
132 (defsubst viper-mouse-click-window-buffer
(click)
133 (window-buffer (viper-mouse-click-window click
)))
135 ;; Returns the name of the buffer in the window where click occurs
136 (defsubst viper-mouse-click-window-buffer-name
(click)
137 (buffer-name (viper-mouse-click-window-buffer click
)))
139 ;; Returns position of a click
140 (defsubst viper-mouse-click-posn
(click)
143 (posn-point (event-start click
))))
146 (defun viper-surrounding-word (count click-count
)
147 "Returns word surrounding point according to a heuristic.
148 COUNT indicates how many regions to return.
149 If CLICK-COUNT is 1, `word' is a word in Vi sense.
150 If CLICK-COUNT is 2,then `word' is a Word in Vi sense.
151 If the character clicked on is a non-separator and is non-alphanumeric but
152 is adjacent to an alphanumeric symbol, then it is considered alphanumeric
153 for the purpose of this command. If this character has a matching
154 character, such as `\(' is a match for `\)', then the matching character is
155 also considered alphanumeric.
156 For convenience, in Lisp modes, `-' is considered alphanumeric.
158 If CLICK-COUNT is 3 or more, returns the line clicked on with leading and
159 trailing space and tabs removed. In that case, the first argument, COUNT,
161 (let ((modifiers "_")
164 (if (> click-count
2)
167 (viper-skip-all-separators-forward 'within-line
)
170 (setq result
(buffer-substring beg
(point))))
172 (if (and (not (viper-looking-at-alphasep))
173 (or (save-excursion (viper-backward-char-carefully)
174 (viper-looking-at-alpha))
175 (save-excursion (viper-forward-char-carefully)
176 (viper-looking-at-alpha))))
179 (cond ((looking-at "\\\\") "\\\\")
180 ((looking-at "-") "C-C-")
181 ((looking-at "[][]") "][")
182 ((looking-at "[()]") ")(")
183 ((looking-at "[{}]") "{}")
184 ((looking-at "[<>]") "<>")
185 ((looking-at "[`']") "`'")
186 ((looking-at "\\^") "\\^")
187 ((viper-looking-at-separator) "")
188 (t (char-to-string (following-char))))
192 ;; Add `-' to alphanum, if it wasn't added and if we are in Lisp
194 (not (string-match "lisp" (symbol-name major-mode
)))
195 (setq modifiers
(concat modifiers
"C-C-")))
199 (cond ((> click-count
1) (viper-skip-nonseparators 'backward
))
200 ((viper-looking-at-alpha modifiers
)
201 (viper-skip-alpha-backward modifiers
))
202 ((not (viper-looking-at-alphasep modifiers
))
203 (viper-skip-nonalphasep-backward))
204 (t (if (> click-count
1)
205 (viper-skip-nonseparators 'backward
)
206 (viper-skip-alpha-backward modifiers
))))
208 (setq word-beg
(point))
210 (setq skip-flag nil
) ; don't move 1 char forw the first time
212 (if skip-flag
(viper-forward-char-carefully 1))
213 (setq skip-flag t
) ; now always move 1 char forward
214 (if (> click-count
1)
215 (viper-skip-nonseparators 'forward
)
216 (viper-skip-alpha-forward modifiers
))
217 (setq count
(1- count
)))
219 (setq result
(buffer-substring word-beg
(point))))
221 ;; XEmacs doesn't have set-text-properties, but there buffer-substring
222 ;; doesn't return properties together with the string, so it's not needed.
224 (set-text-properties 0 (length result
) nil result
))
229 (defun viper-mouse-click-get-word (click count click-count
)
230 "Returns word surrounding the position of a mouse click.
231 Click may be in another window. Current window and buffer isn't changed.
232 On single or double click, returns the word as determined by
233 `viper-surrounding-word-function'."
235 (let ((click-word "")
236 (click-pos (viper-mouse-click-posn click
))
237 (click-buf (viper-mouse-click-window-buffer click
)))
238 (or (natnump count
) (setq count
1))
239 (or (natnump click-count
) (setq click-count
1))
242 (save-window-excursion
245 (set-buffer click-buf
)
247 (goto-char click-pos
)
249 (funcall viper-surrounding-word-function count click-count
)))
250 (error "Click must be over a window."))
254 (defun viper-mouse-click-insert-word (click arg
)
255 "Insert word clicked or double-clicked on.
256 With prefix argument, N, insert that many words.
257 This command must be bound to a mouse click.
258 The double-click action of the same mouse button must not be bound
259 \(or it must be bound to the same function\).
260 See `viper-surrounding-word' for the definition of a word in this case."
262 (if viper-frame-of-focus
;; to handle clicks in another frame
263 (select-frame viper-frame-of-focus
))
265 (or (not (eq (key-binding viper-mouse-down-insert-key-parsed
)
266 'viper-mouse-catch-frame-switch
))
267 (not (eq (key-binding viper-mouse-up-insert-key-parsed
)
268 'viper-mouse-click-insert-word
))
269 (and viper-xemacs-p
(not (event-over-text-area-p click
)))))
270 () ; do nothing, if binding isn't right or not over text
271 ;; turn arg into a number
272 (cond ((integerp arg
) nil
)
273 ;; prefix arg is a list when one hits C-u then command
274 ((and (listp arg
) (integerp (car arg
)))
275 (setq arg
(car arg
)))
278 (if (not (eq (key-binding viper-mouse-down-insert-key-parsed
)
279 'viper-mouse-catch-frame-switch
))
281 (let (click-count interrupting-event
)
284 ;; This trick checks if there is a pending mouse event if so, we
285 ;; use this latter event and discard the current mouse click If
286 ;; the next pending event is not a mouse event, we execute the
287 ;; current mouse event
289 (setq interrupting-event
(viper-read-event))
290 (viper-mouse-event-p last-input-event
)))
291 (progn ; interrupted wait
292 (setq viper-global-prefix-argument arg
)
293 ;; count this click for XEmacs
294 (viper-event-click-count click
))
295 ;; uninterrupted wait or the interrupting event wasn't a mouse event
296 (setq click-count
(viper-event-click-count click
))
297 (if (> click-count
1)
298 (setq arg viper-global-prefix-argument
299 viper-global-prefix-argument nil
))
300 (insert (viper-mouse-click-get-word click arg click-count
))
301 (if (and interrupting-event
302 (eventp interrupting-event
)
303 (not (viper-mouse-event-p interrupting-event
)))
304 (viper-set-unread-command-events interrupting-event
))
307 ;; Arg is an event. Accepts symbols and numbers, too
308 (defun viper-mouse-event-p (event)
310 (string-match "\\(mouse-\\|frame\\|screen\\|track\\)"
311 (prin1-to-string (viper-event-key event
)))))
313 ;; XEmacs has no double-click events. So, we must simulate.
314 ;; So, we have to simulate event-click-count.
315 (defun viper-event-click-count (click)
317 (viper-event-click-count-xemacs click
)
318 (event-click-count click
)))
320 ;; kind of semaphore for updating viper-current-click-count
321 (defvar viper-counting-clicks-p nil
)
322 (defun viper-event-click-count-xemacs (click)
323 (let ((time-delta (- (event-timestamp click
)
324 viper-last-click-event-timestamp
))
326 (while viper-counting-clicks-p
328 (setq viper-counting-clicks-p t
)
329 (if (> time-delta viper-multiclick-timeout
)
330 (setq viper-current-click-count
0))
332 (setq viper-current-click-count
(1+ viper-current-click-count
)
333 viper-last-click-event-timestamp
(event-timestamp click
))
334 (setq viper-counting-clicks-p nil
)
335 (if (viper-sit-for-short viper-multiclick-timeout t
)
336 viper-current-click-count
341 (defun viper-mouse-click-search-word (click arg
)
342 "Find the word clicked or double-clicked on. Word may be in another window.
343 With prefix argument, N, search for N-th occurrence.
344 This command must be bound to a mouse click. The double-click action of the
345 same button must not be bound \(or it must be bound to the same function\).
346 See `viper-surrounding-word' for the details on what constitutes a word for
349 (if viper-frame-of-focus
;; to handle clicks in another frame
350 (select-frame viper-frame-of-focus
))
352 (or (not (eq (key-binding viper-mouse-down-search-key-parsed
)
353 'viper-mouse-catch-frame-switch
))
354 (not (eq (key-binding viper-mouse-up-search-key-parsed
)
355 'viper-mouse-click-search-word
))
356 (and viper-xemacs-p
(not (event-over-text-area-p click
)))))
357 () ; do nothing, if binding isn't right or not over text
358 (let ((previous-search-string viper-s-string
)
359 click-word click-count
)
363 ;; This trick checks if there is a pending mouse event if so, we use
364 ;; this latter event and discard the current mouse click If the next
365 ;; pending event is not a mouse event, we execute the current mouse
369 (viper-mouse-event-p last-input-event
)))
370 (progn ; interrupted wait
371 (setq viper-global-prefix-argument
(or viper-global-prefix-argument
373 ;; remember command that was before the multiclick
374 this-command last-command
)
375 ;; make sure we counted this event---needed for XEmacs only
376 (viper-event-click-count click
))
377 ;; uninterrupted wait
378 (setq click-count
(viper-event-click-count click
))
379 (setq click-word
(viper-mouse-click-get-word click nil click-count
))
381 (if (> click-count
1)
382 (setq arg viper-global-prefix-argument
383 viper-global-prefix-argument nil
))
384 (setq arg
(or arg
1))
386 (viper-deactivate-mark)
387 (if (or (not (string= click-word viper-s-string
))
388 (not (markerp viper-search-start-marker
))
389 (not (equal (marker-buffer viper-search-start-marker
)
391 (not (eq last-command
'viper-mouse-click-search-word
)))
393 (setq viper-search-start-marker
(point-marker)
394 viper-local-search-start-marker viper-search-start-marker
395 viper-mouse-click-search-noerror t
396 viper-mouse-click-search-limit nil
)
398 ;; make search string known to Viper
399 (setq viper-s-string
(if viper-re-search
400 (regexp-quote click-word
)
402 (if (not (string= viper-s-string
(car viper-search-history
)))
403 (setq viper-search-history
404 (cons viper-s-string viper-search-history
)))
409 (viper-forward-word 1)
412 (if (not (search-forward
413 click-word viper-mouse-click-search-limit
414 viper-mouse-click-search-noerror
))
416 (setq viper-mouse-click-search-noerror nil
)
417 (setq viper-mouse-click-search-limit
420 (markerp viper-local-search-start-marker
)
421 (marker-buffer viper-local-search-start-marker
))
422 (goto-char viper-local-search-start-marker
))
423 (viper-line-pos 'end
)))
425 (goto-char (point-min))
426 (search-forward click-word
427 viper-mouse-click-search-limit nil
)))
428 (goto-char (match-beginning 0))
429 (message "Searching for: %s" viper-s-string
)
430 (if (<= arg
1) ; found the right occurrence of the pattern
432 (viper-adjust-window)
433 (viper-flash-search-pattern)))
436 (if (or (not (string= click-word previous-search-string
))
437 (not (eq last-command
'viper-mouse-click-search-word
)))
438 (message "`%s': String not found in %s"
439 viper-s-string
(buffer-name (current-buffer)))
441 "`%s': Last occurrence in %s. Back to beginning of search"
442 click-word
(buffer-name (current-buffer)))
443 (setq arg
1) ;; to terminate the loop
445 (setq viper-mouse-click-search-noerror t
)
446 (setq viper-mouse-click-search-limit nil
)
447 (if (and (markerp viper-local-search-start-marker
)
448 (marker-buffer viper-local-search-start-marker
))
449 (goto-char viper-local-search-start-marker
))))
453 (defun viper-mouse-catch-frame-switch (event arg
)
454 "Catch the event of switching frame.
455 Usually is bound to a `down-mouse' event to work properly. See sample
456 bindings in the Viper manual."
458 (setq viper-frame-of-focus nil
)
459 ;; pass prefix arg along to viper-mouse-click-search/insert-word
460 (setq prefix-arg arg
)
461 (if (eq last-command
'handle-switch-frame
)
462 (setq viper-frame-of-focus viper-current-frame-saved
))
463 ;; make Emacs forget that it executed viper-mouse-catch-frame-switch
464 (setq this-command last-command
))
466 ;; Called just before switching frames. Saves the old selected frame.
467 ;; Sets last-command to handle-switch-frame (this is done automatically in
469 ;; The semantics of switching frames is different in Emacs and XEmacs.
470 ;; In Emacs, if you select-frame A while mouse is over frame B and then
471 ;; start typing, input goes to frame B, which becomes selected.
472 ;; In XEmacs, input will go to frame A. This may be a bug in one of the
473 ;; Emacsen, but also may be a design decision.
474 ;; Also, in Emacs sending input to frame B generates handle-switch-frame
475 ;; event, while in XEmacs it doesn't.
476 ;; All this accounts for the difference in the behavior of
477 ;; viper-mouse-click-* commands when you click in a frame other than the one
478 ;; that was the last to receive input. In Emacs, focus will be in frame A
479 ;; until you do something other than viper-mouse-click-* command.
480 ;; In XEmacs, you have to manually select frame B (with the mouse click) in
481 ;; order to shift focus to frame B.
482 (defsubst viper-remember-current-frame
(frame)
483 (setq last-command
'handle-switch-frame
484 viper-current-frame-saved
(selected-frame)))
487 ;; The key is of the form (MODIFIER ... BUTTON-NUMBER)
488 ;; Converts into a valid mouse button spec for the appropriate version of
489 ;; Emacs. EVENT-TYPE is either `up' or `down'. Up returns button-up key; down
490 ;; returns button-down key.
491 (defun viper-parse-mouse-key (key-var event-type
)
492 (let ((key (eval key-var
))
493 button-spec meta-spec shift-spec control-spec key-spec
)
500 (if (eq 'up event-type
)
501 "mouse-1" "down-mouse-1")
502 (if (eq 'up event-type
)
503 'button1up
'button1
)))
506 (if (eq 'up event-type
)
507 "mouse-2" "down-mouse-2")
508 (if (eq 'up event-type
)
509 'button2up
'button2
)))
512 (if (eq 'up event-type
)
513 "mouse-3" "down-mouse-3")
514 (if (eq 'up event-type
)
515 'button3up
'button3
)))
517 "%S: invalid button number, %S" key-var key
)))
520 (if viper-emacs-p
"M-" 'meta
)
521 (if viper-emacs-p
"" nil
))
523 (if (memq 'shift key
)
524 (if viper-emacs-p
"S-" 'shift
)
525 (if viper-emacs-p
"" nil
))
527 (if (memq 'control key
)
528 (if viper-emacs-p
"C-" 'control
)
529 (if viper-emacs-p
"" nil
)))
531 (setq key-spec
(if viper-emacs-p
535 control-spec meta-spec shift-spec button-spec
)))
540 control-spec meta-spec shift-spec button-spec
)))))
543 (defun viper-unbind-mouse-search-key ()
544 (if viper-mouse-up-search-key-parsed
545 (global-unset-key viper-mouse-up-search-key-parsed
))
546 (if viper-mouse-down-search-key-parsed
547 (global-unset-key viper-mouse-down-search-key-parsed
))
548 (setq viper-mouse-up-search-key-parsed nil
549 viper-mouse-down-search-key-parsed nil
))
551 (defun viper-unbind-mouse-insert-key ()
552 (if viper-mouse-up-insert-key-parsed
553 (global-unset-key viper-mouse-up-insert-key-parsed
))
554 (if viper-mouse-down-insert-key-parsed
555 (global-unset-key viper-mouse-down-insert-key-parsed
))
556 (setq viper-mouse-up-insert-key-parsed nil
557 viper-mouse-down-insert-key-parsed nil
))
559 ;; If FORCE, bind even if this mouse action is already bound to something else
560 (defun viper-bind-mouse-search-key (&optional force
)
561 (setq viper-mouse-up-search-key-parsed
562 (viper-parse-mouse-key 'viper-mouse-search-key
'up
)
563 viper-mouse-down-search-key-parsed
564 (viper-parse-mouse-key 'viper-mouse-search-key
'down
))
565 (cond ((or (null viper-mouse-up-search-key-parsed
)
566 (null viper-mouse-down-search-key-parsed
))
569 (key-binding viper-mouse-up-search-key-parsed
)
570 (not (eq (key-binding viper-mouse-up-search-key-parsed
)
571 'viper-mouse-click-search-word
)))
573 "%S already bound to a mouse event. Viper mouse-search feature disabled"
574 viper-mouse-up-search-key-parsed
))
576 (key-binding viper-mouse-down-search-key-parsed
)
577 (not (eq (key-binding viper-mouse-down-search-key-parsed
)
578 'viper-mouse-catch-frame-switch
)))
580 "%S already bound to a mouse event. Viper mouse-search feature disabled"
581 viper-mouse-down-search-key-parsed
))
583 (global-set-key viper-mouse-up-search-key-parsed
584 'viper-mouse-click-search-word
)
585 (global-set-key viper-mouse-down-search-key-parsed
586 'viper-mouse-catch-frame-switch
))))
588 ;; If FORCE, bind even if this mouse action is already bound to something else
589 (defun viper-bind-mouse-insert-key (&optional force
)
590 (setq viper-mouse-up-insert-key-parsed
591 (viper-parse-mouse-key 'viper-mouse-insert-key
'up
)
592 viper-mouse-down-insert-key-parsed
593 (viper-parse-mouse-key 'viper-mouse-insert-key
'down
))
594 (cond ((or (null viper-mouse-up-insert-key-parsed
)
595 (null viper-mouse-down-insert-key-parsed
))
598 (key-binding viper-mouse-up-insert-key-parsed
)
599 (not (eq (key-binding viper-mouse-up-insert-key-parsed
)
600 'viper-mouse-click-insert-word
)))
602 "%S already bound to a mouse event. Viper mouse-insert feature disabled"
603 viper-mouse-up-insert-key-parsed
))
605 (key-binding viper-mouse-down-insert-key-parsed
)
606 (not (eq (key-binding viper-mouse-down-insert-key-parsed
)
607 'viper-mouse-catch-frame-switch
)))
609 "%S already bound to a mouse event. Viper mouse-insert feature disabled"
610 viper-mouse-down-insert-key-parsed
))
612 (global-set-key viper-mouse-up-insert-key-parsed
613 'viper-mouse-click-insert-word
)
614 (global-set-key viper-mouse-down-insert-key-parsed
615 'viper-mouse-catch-frame-switch
))))
617 (defun viper-reset-mouse-search-key (symb val
)
618 (viper-unbind-mouse-search-key)
620 (viper-bind-mouse-search-key 'force
))
622 (defun viper-reset-mouse-insert-key (symb val
)
623 (viper-unbind-mouse-insert-key)
625 (viper-bind-mouse-insert-key 'force
))
628 (defcustom viper-mouse-search-key
'(meta shift
1)
629 "*Key used to click-search in Viper.
630 This must be a list that specifies the mouse button and modifiers.
631 The supported modifiers are `meta', `shift', and `control'.
632 For instance, `(meta shift 1)' means that holding the meta and shift
633 keys down and clicking on a word with mouse button 1
634 will search for that word in the buffer that was current before the click.
635 This buffer may be different from the one where the click occurred."
636 :type
'(list (set :inline t
:tag
"Modifiers" :format
"%t: %v"
637 (const :format
"%v " meta
)
638 (const :format
"%v " shift
)
640 (integer :tag
"Button"))
641 :set
'viper-reset-mouse-search-key
644 (defcustom viper-mouse-insert-key
'(meta shift
2)
645 "*Key used to click-insert in Viper.
646 Must be a list that specifies the mouse button and modifiers.
647 The supported modifiers are `meta', `shift', and `control'.
648 For instance, `(meta shift 2)' means that holding the meta and shift keys
649 down, and clicking on a word with mouse button 2, will insert that word
650 at the cursor in the buffer that was current just before the click.
651 This buffer may be different from the one where the click occurred."
652 :type
'(list (set :inline t
:tag
"Modifiers" :format
"%t: %v"
653 (const :format
"%v " meta
)
654 (const :format
"%v " shift
)
656 (integer :tag
"Button"))
657 :set
'viper-reset-mouse-insert-key
663 ;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
667 ;;; viper-mous.el ends here