1 ;;; mouse.el --- window system-independent mouse support
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
7 ;; Keywords: hardware, mouse
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This package provides various useful commands (including help
29 ;; system access) through the mouse. All this code assumes that mouse
30 ;; interpretation has been abstracted into Emacs input events.
32 ;; The code is rather X-dependent.
36 ;;; Utility functions.
38 ;;; Indent track-mouse like progn.
39 (put 'track-mouse
'lisp-indent-function
0)
41 (defcustom mouse-yank-at-point nil
42 "*If non-nil, mouse yank commands yank at point instead of at click."
46 (defcustom mouse-drag-copy-region t
47 "*If non-nil, mouse drag copies region to kill-ring."
52 (defcustom mouse-1-click-follows-link
450
53 "Non-nil means that clicking Mouse-1 on a link follows the link.
55 With the default setting, an ordinary Mouse-1 click on a link
56 performs the same action as Mouse-2 on that link, while a longer
57 Mouse-1 click \(hold down the Mouse-1 button for more than 350
58 milliseconds) performs the original Mouse-1 binding \(which
59 typically sets point where you click the mouse).
61 If value is an integer, the time elapsed between pressing and
62 releasing the mouse button determines whether to follow the link
63 or perform the normal Mouse-1 action (typically set point).
64 The absolute numeric value specifices the maximum duration of a
65 \"short click\" in milliseconds. A positive value means that a
66 short click follows the link, and a longer click performs the
67 normal action. A negative value gives the opposite behavior.
69 If value is `double', a double click follows the link.
71 Otherwise, a single Mouse-1 click unconditionally follows the link.
73 Note that dragging the mouse never follows the link.
75 This feature only works in modes that specifically identify
76 clickable text as links, so it may not work with some external
77 packages. See `mouse-on-link-p' for details."
79 :type
'(choice (const :tag
"Disabled" nil
)
80 (const :tag
"Double click" double
)
81 (number :tag
"Single click time limit" :value
350)
82 (other :tag
"Single click" t
))
85 (defcustom mouse-1-click-in-non-selected-windows t
86 "*If non-nil, a Mouse-1 click also follows links in non-selected windows.
88 If nil, a Mouse-1 click on a link in a non-selected window performs
89 the normal mouse-1 binding, typically selects the window and sets
90 point at the click position."
97 ;; Provide a mode-specific menu on a mouse button.
99 (defun popup-menu (menu &optional position prefix
)
100 "Popup the given menu and call the selected option.
101 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
103 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
104 the current mouse position.
105 PREFIX is the prefix argument (if any) to pass to the command."
107 ((keymapp menu
) menu
)
108 ((and (listp menu
) (keymapp (car menu
))) menu
)
109 (t (let* ((map (easy-menu-create-menu (car menu
) (cdr menu
)))
110 (filter (when (symbolp map
)
111 (plist-get (get map
'menu-prop
) :filter
))))
112 (if filter
(funcall filter
(symbol-function map
)) map
)))))
115 (let ((mp (mouse-pixel-position)))
116 (setq position
(list (list (cadr mp
) (cddr mp
)) (car mp
)))))
117 ;; The looping behavior was taken from lmenu's popup-menu-popup
118 (while (and map
(setq event
119 ;; map could be a prefix key, in which case
120 ;; we need to get its function cell
122 (x-popup-menu position
(indirect-function map
))))
123 ;; Strangely x-popup-menu returns a list.
124 ;; mouse-major-mode-menu was using a weird:
125 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
127 (if (and (not (keymapp map
)) (listp map
))
128 ;; We were given a list of keymaps. Search them all
129 ;; in sequence until a first binding is found.
130 (let ((mouse-click (apply 'vector event
))
132 (while (and map
(null binding
))
133 (setq binding
(lookup-key (car map
) mouse-click
))
134 (if (numberp binding
) ; `too long'
136 (setq map
(cdr map
)))
138 ;; We were given a single keymap.
139 (lookup-key map
(apply 'vector event
))))
140 ;; Clear out echoing, which perhaps shows a prefix arg.
142 ;; Maybe try again but with the submap.
143 (setq map
(if (keymapp cmd
) cmd
)))
144 ;; If the user did not cancel by refusing to select,
145 ;; and if the result is a command, run it.
146 (when (and (null map
) (commandp cmd
))
147 (setq prefix-arg prefix
)
148 ;; `setup-specified-language-environment', for instance,
149 ;; expects this to be set from a menu keymap.
150 (setq last-command-event
(car (last event
)))
151 ;; mouse-major-mode-menu was using `command-execute' instead.
152 (call-interactively cmd
))))
154 (defvar mouse-major-mode-menu-prefix
) ; dynamically bound
156 (defun mouse-major-mode-menu (event prefix
)
157 "Pop up a mode-specific menu of mouse commands.
158 Default to the Edit menu if the major mode doesn't define a menu."
159 ;; Switch to the window clicked on, because otherwise
160 ;; the mode's commands may not make sense.
161 (interactive "@e\nP")
162 ;; Let the mode update its menus first.
163 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
164 (let* (;; This is where mouse-major-mode-menu-prefix
165 ;; returns the prefix we should use (after menu-bar).
166 ;; It is either nil or (SOME-SYMBOL).
167 (mouse-major-mode-menu-prefix nil
)
168 ;; Keymap from which to inherit; may be null.
169 (ancestor (mouse-major-mode-menu-1
170 (and (current-local-map)
171 (local-key-binding [menu-bar
]))))
172 ;; Make a keymap in which our last command leads to a menu or
173 ;; default to the edit menu.
175 (make-sparse-keymap (concat mode-name
" Mode"))
179 ;; Make our menu inherit from the desired keymap which we want
180 ;; to display as the menu now.
181 (set-keymap-parent newmap ancestor
))
182 (popup-menu newmap event prefix
)))
185 ;; Compute and cache the equivalent keys in MENU and all its submenus.
186 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
187 ;;; (and (eq (car menu) 'keymap)
188 ;;; (x-popup-menu nil menu))
190 ;;; (and (consp (car menu))
191 ;;; (consp (cdr (car menu)))
192 ;;; (let ((tail (cdr (car menu))))
193 ;;; (while (and (consp tail)
194 ;;; (not (eq (car tail) 'keymap)))
195 ;;; (setq tail (cdr tail)))
197 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
198 ;;; (setq menu (cdr menu))))
200 ;; Given a mode's menu bar keymap,
201 ;; if it defines exactly one menu bar menu,
202 ;; return just that menu.
203 ;; Otherwise return a menu for all of them.
204 (defun mouse-major-mode-menu-1 (menubar)
209 (if (consp (car tail
))
212 (setq submap
(car tail
))))
213 (setq tail
(cdr tail
)))
216 (setq mouse-major-mode-menu-prefix
(list (car submap
)))
217 (lookup-key menubar
(vector (car submap
)))))))
219 (defun mouse-popup-menubar (event prefix
)
220 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
221 The contents are the items that would be in the menu bar whether or
222 not it is actually displayed."
223 (interactive "@e \nP")
224 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
225 (let* ((local-menu (and (current-local-map)
226 (lookup-key (current-local-map) [menu-bar
])))
227 (global-menu (lookup-key global-map
[menu-bar
]))
228 ;; If a keymap doesn't have a prompt string (a lazy
229 ;; programmer didn't bother to provide one), create it and
230 ;; insert it into the keymap; each keymap gets its own
231 ;; prompt. This is required for non-toolkit versions to
232 ;; display non-empty menu pane names.
237 (let* ((minor-mode (car menu
))
239 (title-or-map (cadr menu
)))
240 (or (stringp title-or-map
)
244 (capitalize (subst-char-in-string
250 (minor-mode-key-binding [menu-bar
])))
251 (local-title-or-map (and local-menu
(cadr local-menu
)))
252 (global-title-or-map (cadr global-menu
)))
253 (or (null local-menu
)
254 (stringp local-title-or-map
)
255 (setq local-menu
(cons 'keymap
256 (cons (concat mode-name
" Mode Menu")
258 (or (stringp global-title-or-map
)
259 (setq global-menu
(cons 'keymap
261 (cdr global-menu
)))))
262 ;; Supplying the list is faster than making a new map.
263 (popup-menu (append (list global-menu
)
269 (defun mouse-popup-menubar-stuff (event prefix
)
270 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
271 Use the former if the menu bar is showing, otherwise the latter."
272 (interactive "@e \nP")
273 (if (zerop (assoc-default 'menu-bar-lines
(frame-parameters) 'eq
0))
274 (mouse-popup-menubar event prefix
)
275 (mouse-major-mode-menu event prefix
)))
277 ;; Commands that operate on windows.
279 (defun mouse-minibuffer-check (event)
280 (let ((w (posn-window (event-start event
))))
281 (and (window-minibuffer-p w
)
282 (not (minibuffer-window-active-p w
))
283 (error "Minibuffer window is not active")))
284 ;; Give temporary modes such as isearch a chance to turn off.
285 (run-hooks 'mouse-leave-buffer-hook
))
287 (defun mouse-delete-window (click)
288 "Delete the window you click on.
289 Do nothing if the frame has just one window.
290 This command must be bound to a mouse click."
292 (unless (one-window-p t
)
293 (mouse-minibuffer-check click
)
294 (delete-window (posn-window (event-start click
)))))
296 (defun mouse-select-window (click)
297 "Select the window clicked on; don't move point."
299 (mouse-minibuffer-check click
)
300 (let ((oframe (selected-frame))
301 (frame (window-frame (posn-window (event-start click
)))))
302 (select-window (posn-window (event-start click
)))
305 (or (eq frame oframe
)
306 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
308 (defun mouse-tear-off-window (click)
309 "Delete the window clicked on, and create a new frame displaying its buffer."
311 (mouse-minibuffer-check click
)
312 (let* ((window (posn-window (event-start click
)))
313 (buf (window-buffer window
))
314 (frame (make-frame)))
316 (switch-to-buffer buf
)
317 (delete-window window
)))
319 (defun mouse-delete-other-windows ()
320 "Delete all windows except the one you click on."
322 (delete-other-windows))
324 (defun mouse-split-window-vertically (click)
325 "Select Emacs window mouse is on, then split it vertically in half.
326 The window is split at the line clicked on.
327 This command must be bound to a mouse click."
329 (mouse-minibuffer-check click
)
330 (let ((start (event-start click
)))
331 (select-window (posn-window start
))
332 (let ((new-height (1+ (cdr (posn-col-row (event-end click
)))))
333 (first-line window-min-height
)
334 (last-line (- (window-height) window-min-height
)))
335 (if (< last-line first-line
)
336 (error "Window too short to split")
337 (split-window-vertically
338 (min (max new-height first-line
) last-line
))))))
340 (defun mouse-split-window-horizontally (click)
341 "Select Emacs window mouse is on, then split it horizontally in half.
342 The window is split at the column clicked on.
343 This command must be bound to a mouse click."
345 (mouse-minibuffer-check click
)
346 (let ((start (event-start click
)))
347 (select-window (posn-window start
))
348 (let ((new-width (1+ (car (posn-col-row (event-end click
)))))
349 (first-col window-min-width
)
350 (last-col (- (window-width) window-min-width
)))
351 (if (< last-col first-col
)
352 (error "Window too narrow to split")
353 (split-window-horizontally
354 (min (max new-width first-col
) last-col
))))))
356 (defun mouse-drag-window-above (window)
357 "Return the (or a) window directly above WINDOW.
358 That means one whose bottom edge is at the same height as WINDOW's top edge."
359 (let ((top (nth 1 (window-edges window
)))
360 (start-window window
)
362 (setq window
(previous-window window
0))
363 (while (and (not above-window
) (not (eq window start-window
)))
364 (if (= (+ (window-height window
) (nth 1 (window-edges window
)))
366 (setq above-window window
))
367 (setq window
(previous-window window
)))
370 (defun mouse-drag-move-window-bottom (window growth
)
371 "Move the bottom of WINDOW up or down by GROWTH lines.
372 Move it down if GROWTH is positive, or up if GROWTH is negative.
373 If this would make WINDOW too short,
374 shrink the window or windows above it to make room."
375 (let ((excess (- window-min-height
(+ (window-height window
) growth
))))
376 ;; EXCESS is the number of lines we need to take from windows above.
378 ;; This can recursively shrink windows all the way up.
379 (let ((window-above (mouse-drag-window-above window
)))
381 (mouse-drag-move-window-bottom window-above
(- excess
))))))
382 (save-selected-window
383 (select-window window
)
384 (enlarge-window growth nil
(> growth
0))))
386 (defsubst mouse-drag-move-window-top
(window growth
)
387 "Move the top of WINDOW up or down by GROWTH lines.
388 Move it down if GROWTH is positive, or up if GROWTH is negative.
389 If this would make WINDOW too short, shrink the window or windows
390 above it to make room."
391 ;; Moving the top of WINDOW is actually moving the bottom of the
393 (let ((window-above (mouse-drag-window-above window
)))
395 (mouse-drag-move-window-bottom window-above
(- growth
)))))
397 (defun mouse-drag-mode-line-1 (start-event mode-line-p
)
398 "Change the height of a window by dragging on the mode or header line.
399 START-EVENT is the starting mouse-event of the drag action.
400 MODE-LINE-P non-nil means dragging a mode line; nil means a header line."
401 ;; Give temporary modes such as isearch a chance to turn off.
402 (run-hooks 'mouse-leave-buffer-hook
)
405 (start (event-start start-event
))
406 (start-event-window (posn-window start
))
407 (start-event-frame (window-frame start-event-window
))
408 (start-nwindows (count-windows t
))
409 (old-selected-window (selected-window))
410 (minibuffer (frame-parameter nil
'minibuffer
))
411 should-enlarge-minibuffer event mouse y top bot edges wconfig growth
)
414 ;; enlarge-window only works on the selected window, so
415 ;; we must select the window where the start event originated.
416 ;; unwind-protect will restore the old selected window later.
417 (select-window start-event-window
)
419 ;; if this is the bottommost ordinary window, then to
420 ;; move its modeline the minibuffer must be enlarged.
421 (setq should-enlarge-minibuffer
424 (not (one-window-p t
))
425 (= (nth 1 (window-edges minibuffer
))
426 (nth 3 (window-edges)))))
428 ;; loop reading events and sampling the position of
431 (setq event
(read-event)
432 mouse
(mouse-position))
435 ;; - there is a switch-frame event.
436 ;; - the mouse isn't in the frame that we started in
437 ;; - the mouse isn't in any Emacs frame
439 ;; - there is a mouse-movement event
440 ;; - there is a scroll-bar-movement event
441 ;; (same as mouse movement for our purposes)
443 ;; - there is a keyboard event or some other unknown event
445 (cond ((integerp event
)
448 ((memq (car event
) '(switch-frame select-window
))
451 ((not (memq (car event
) '(mouse-movement scroll-bar-movement
)))
453 (push event unread-command-events
))
456 ((not (eq (car mouse
) start-event-frame
))
459 ((null (car (cdr mouse
)))
463 (setq y
(cdr (cdr mouse
))
468 ;; compute size change needed
470 (setq growth
(- y bot -
1)))
472 (when (< (- bot y
) window-min-height
)
473 (setq y
(- bot window-min-height
)))
474 ;; The window's top includes the header line!
475 (setq growth
(- top y
))))
476 (setq wconfig
(current-window-configuration))
478 ;; Check for an error case.
479 (when (and (/= growth
0)
482 (error "Attempt to resize sole window"))
484 ;; grow/shrink minibuffer?
485 (if should-enlarge-minibuffer
487 ;; yes. briefly select minibuffer so
488 ;; enlarge-window will affect the
490 (select-window minibuffer
)
491 ;; scale back shrinkage if it would
492 ;; make the minibuffer less than 1
494 (if (and (> growth
0)
495 (< (- (window-height minibuffer
)
498 (setq growth
(1- (window-height minibuffer
))))
499 (enlarge-window (- growth
))
500 (select-window start-event-window
))
501 ;; no. grow/shrink the selected window
502 ;(message "growth = %d" growth)
504 (mouse-drag-move-window-bottom start-event-window growth
)
505 (mouse-drag-move-window-top start-event-window growth
)))
507 ;; if this window's growth caused another
508 ;; window to be deleted because it was too
509 ;; short, rescind the change.
511 ;; if size change caused space to be stolen
512 ;; from a window above this one, rescind the
513 ;; change, but only if we didn't grow/shrink
514 ;; the minibuffer. minibuffer size changes
515 ;; can cause all windows to shrink... no way
517 (when (or (/= start-nwindows
(count-windows t
))
518 (and (not should-enlarge-minibuffer
)
521 (/= top
(nth 1 (window-edges)))))
522 (set-window-configuration wconfig
)))))))))
524 (defun mouse-drag-mode-line (start-event)
525 "Change the height of a window by dragging on the mode line."
527 (mouse-drag-mode-line-1 start-event t
))
529 (defun mouse-drag-header-line (start-event)
530 "Change the height of a window by dragging on the header line.
531 Windows whose header-lines are at the top of the frame cannot be
532 resized by dragging their header-line."
534 ;; Changing the window's size by dragging its header-line when the
535 ;; header-line is at the top of the frame is somewhat strange,
536 ;; because the header-line doesn't move, so don't do it.
537 (let* ((start (event-start start-event
))
538 (window (posn-window start
))
539 (frame (window-frame window
))
540 (first-window (frame-first-window frame
)))
541 (unless (or (eq window first-window
)
542 (= (nth 1 (window-edges window
))
543 (nth 1 (window-edges first-window
))))
544 (mouse-drag-mode-line-1 start-event nil
))))
547 (defun mouse-drag-vertical-line (start-event)
548 "Change the width of a window by dragging on the vertical line."
550 ;; Give temporary modes such as isearch a chance to turn off.
551 (run-hooks 'mouse-leave-buffer-hook
)
554 (start-event-frame (window-frame (car (car (cdr start-event
)))))
555 (start-event-window (car (car (cdr start-event
))))
556 (start-nwindows (count-windows t
))
557 (old-selected-window (selected-window))
558 event mouse x left right edges wconfig growth
560 (or (cdr (assq 'vertical-scroll-bars
(frame-parameters start-event-frame
)))
563 (error "Attempt to resize sole ordinary window"))
564 (if (eq which-side
'right
)
565 (if (= (nth 2 (window-edges start-event-window
))
566 (frame-width start-event-frame
))
567 (error "Attempt to drag rightmost scrollbar"))
568 (if (= (nth 0 (window-edges start-event-window
)) 0)
569 (error "Attempt to drag leftmost scrollbar")))
572 ;; enlarge-window only works on the selected window, so
573 ;; we must select the window where the start event originated.
574 ;; unwind-protect will restore the old selected window later.
575 (select-window start-event-window
)
576 ;; loop reading events and sampling the position of
579 (setq event
(read-event)
580 mouse
(mouse-position))
582 ;; - there is a switch-frame event.
583 ;; - the mouse isn't in the frame that we started in
584 ;; - the mouse isn't in any Emacs frame
586 ;; - there is a mouse-movement event
587 ;; - there is a scroll-bar-movement event
588 ;; (same as mouse movement for our purposes)
590 ;; - there is a keyboard event or some other unknown event
592 (cond ((integerp event
)
594 ((memq (car event
) '(switch-frame select-window
))
596 ((not (memq (car event
)
597 '(mouse-movement scroll-bar-movement
)))
599 (setq unread-command-events
600 (cons event unread-command-events
)))
602 ((not (eq (car mouse
) start-event-frame
))
604 ((null (car (cdr mouse
)))
607 (save-selected-window
608 ;; If the scroll bar is on the window's left,
609 ;; adjust the window on the left.
610 (unless (eq which-side
'right
)
611 (select-window (previous-window)))
612 (setq x
(- (car (cdr mouse
))
613 (if (eq which-side
'right
) 0 2))
617 ;; scale back a move that would make the
619 (if (< (- x left -
1) window-min-width
)
620 (setq x
(+ left window-min-width -
1)))
621 ;; compute size change needed
622 (setq growth
(- x right -
1)
623 wconfig
(current-window-configuration))
624 (enlarge-window growth t
)
625 ;; if this window's growth caused another
626 ;; window to be deleted because it was too
627 ;; thin, rescind the change.
629 ;; if size change caused space to be stolen
630 ;; from a window to the left of this one,
631 ;; rescind the change.
632 (if (or (/= start-nwindows
(count-windows t
))
633 (/= left
(nth 0 (window-edges))))
634 (set-window-configuration wconfig
))))))))))
636 (defun mouse-set-point (event)
637 "Move point to the position clicked on with the mouse.
638 This should be bound to a mouse click event type."
640 (mouse-minibuffer-check event
)
641 ;; Use event-end in case called from mouse-drag-region.
642 ;; If EVENT is a click, event-end and event-start give same value.
643 (posn-set-point (event-end event
)))
645 (defvar mouse-last-region-beg nil
)
646 (defvar mouse-last-region-end nil
)
647 (defvar mouse-last-region-tick nil
)
649 (defun mouse-region-match ()
650 "Return non-nil if there's an active region that was set with the mouse."
651 (and (mark t
) mark-active
652 (eq mouse-last-region-beg
(region-beginning))
653 (eq mouse-last-region-end
(region-end))
654 (eq mouse-last-region-tick
(buffer-modified-tick))))
656 (defun mouse-set-region (click)
657 "Set the region to the text dragged over, and copy to kill ring.
658 This should be bound to a mouse drag event."
660 (mouse-minibuffer-check click
)
661 (let ((posn (event-start click
))
662 (end (event-end click
)))
663 (select-window (posn-window posn
))
664 (if (numberp (posn-point posn
))
665 (goto-char (posn-point posn
)))
666 ;; If mark is highlighted, no need to bounce the cursor.
667 ;; On X, we highlight while dragging, thus once again no need to bounce.
668 (or transient-mark-mode
669 (memq (framep (selected-frame)) '(x pc w32
))
673 (if (numberp (posn-point end
))
674 (goto-char (posn-point end
)))
675 ;; Don't set this-command to kill-region, so that a following
676 ;; C-w will not double the text in the kill ring.
677 ;; Ignore last-command so we don't append to a preceding kill.
678 (when mouse-drag-copy-region
679 (let (this-command last-command deactivate-mark
)
680 (copy-region-as-kill (mark) (point))))
681 (mouse-set-region-1)))
683 (defun mouse-set-region-1 ()
684 ;; Set transient-mark-mode for a little while.
685 (if (memq transient-mark-mode
'(nil identity
))
686 (setq transient-mark-mode
'only
))
687 (setq mouse-last-region-beg
(region-beginning))
688 (setq mouse-last-region-end
(region-end))
689 (setq mouse-last-region-tick
(buffer-modified-tick)))
691 (defcustom mouse-scroll-delay
0.25
692 "*The pause between scroll steps caused by mouse drags, in seconds.
693 If you drag the mouse beyond the edge of a window, Emacs scrolls the
694 window to bring the text beyond that edge into view, with a delay of
695 this many seconds between scroll steps. Scrolling stops when you move
696 the mouse back into the window, or release the button.
697 This variable's value may be non-integral.
698 Setting this to zero causes Emacs to scroll as fast as it can."
702 (defcustom mouse-scroll-min-lines
1
703 "*The minimum number of lines scrolled by dragging mouse out of window.
704 Moving the mouse out the top or bottom edge of the window begins
705 scrolling repeatedly. The number of lines scrolled per repetition
706 is normally equal to the number of lines beyond the window edge that
707 the mouse has moved. However, it always scrolls at least the number
708 of lines specified by this variable."
712 (defun mouse-scroll-subr (window jump
&optional overlay start
)
713 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
714 If OVERLAY is an overlay, let it stretch from START to the far edge of
715 the newly visible text.
716 Upon exit, point is at the far edge of the newly visible text."
718 ((and (> jump
0) (< jump mouse-scroll-min-lines
))
719 (setq jump mouse-scroll-min-lines
))
720 ((and (< jump
0) (< (- jump
) mouse-scroll-min-lines
))
721 (setq jump
(- mouse-scroll-min-lines
))))
722 (let ((opoint (point)))
724 (goto-char (window-start window
))
725 (if (not (zerop (vertical-motion jump window
)))
727 (set-window-start window
(point))
729 (if (window-end window
)
731 (goto-char (window-end window
))
732 ;; window-end doesn't reflect the window's new
733 ;; start position until the next redisplay.
734 (vertical-motion (1- jump
) window
))
735 (vertical-motion (- (window-height window
) 2)))
736 (goto-char (window-start window
)))
738 (move-overlay overlay start
(point)))
739 ;; Now that we have scrolled WINDOW properly,
740 ;; put point back where it was for the redisplay
741 ;; so that we don't mess up the selected window.
742 (or (eq window
(selected-window))
744 (sit-for mouse-scroll-delay
)))))
745 (or (eq window
(selected-window))
746 (goto-char opoint
))))
748 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
749 (defvar mouse-drag-overlay
(make-overlay 1 1))
750 (delete-overlay mouse-drag-overlay
)
751 (overlay-put mouse-drag-overlay
'face
'region
)
753 (defvar mouse-selection-click-count
0)
755 (defvar mouse-selection-click-count-buffer nil
)
757 (defun mouse-drag-region (start-event)
758 "Set the region to the text that the mouse is dragged over.
759 Highlight the drag area as you move the mouse.
760 This must be bound to a button-down mouse event.
761 In Transient Mark mode, the highlighting remains as long as the mark
762 remains active. Otherwise, it remains until the next input event.
764 If the click is in the echo area, display the `*Messages*' buffer."
766 (let ((w (posn-window (event-start start-event
))))
767 (if (and (window-minibuffer-p w
)
768 (not (minibuffer-window-active-p w
)))
770 ;; Swallow the up-event.
772 (set-buffer "*Messages*")
773 (goto-char (point-max))
774 (display-buffer (current-buffer)))
775 ;; Give temporary modes such as isearch a chance to turn off.
776 (run-hooks 'mouse-leave-buffer-hook
)
777 (mouse-drag-region-1 start-event
))))
780 (defun mouse-on-link-p (pos)
781 "Return non-nil if POS is on a link in the current buffer.
782 POS must be a buffer position in the current buffer or an mouse
783 event location in the selected window, see `event-start'.
784 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
785 POS may be a mouse event location in any window.
787 A clickable link is identified by one of the following methods:
789 - If the character at POS has a non-nil `follow-link' text or
790 overlay property, the value of that property determines what to do.
792 - If there is a local key-binding or a keybinding at position POS
793 for the `follow-link' event, the binding of that event determines
796 The resulting value determine whether POS is inside a link:
798 - If the value is `mouse-face', POS is inside a link if there
799 is a non-nil `mouse-face' property at POS. Return t in this case.
801 - If the value is a function, FUNC, POS is inside a link if
802 the call \(FUNC POS) returns non-nil. Return the return value
803 from that call. Arg is \(posn-point POS) if POS is a mouse event,
805 - Otherwise, return the value itself.
807 The return value is interpreted as follows:
809 - If it is a string, the mouse-1 event is translated into the
810 first character of the string, i.e. the action of the mouse-1
811 click is the local or global binding of that character.
813 - If it is a vector, the mouse-1 event is translated into the
814 first element of that vector, i.e. the action of the mouse-1
815 click is the local or global binding of that event.
817 - Otherwise, the mouse-1 event is translated into a mouse-2 event
818 at the same position."
819 (let ((w (and (consp pos
) (posn-window pos
))))
821 (setq pos
(and (or mouse-1-click-in-non-selected-windows
822 (eq (selected-window) w
))
825 (with-current-buffer (window-buffer w
)
827 (or (get-char-property pos
'follow-link
)
830 (key-binding [follow-link
] nil t
)))))
832 ((eq action
'mouse-face
)
833 (and (get-char-property pos
'mouse-face
) t
))
835 (funcall action pos
))
838 (defun mouse-fixup-help-message (msg)
839 "Fix help message MSG for `mouse-1-click-follows-link'."
841 (if (and mouse-1-click-follows-link
844 (string-match "^mouse-2" msg
))
845 (setq mp
(mouse-pixel-position))
846 (consp (setq pos
(cdr mp
)))
847 (car pos
) (>= (car pos
) 0)
848 (cdr pos
) (>= (cdr pos
) 0)
849 (setq pos
(posn-at-x-y (car pos
) (cdr pos
) (car mp
)))
850 (windowp (posn-window pos
)))
851 (with-current-buffer (window-buffer (posn-window pos
))
852 (if (mouse-on-link-p pos
)
855 ((eq mouse-1-click-follows-link
'double
) "double-")
856 ((and (integerp mouse-1-click-follows-link
)
857 (< mouse-1-click-follows-link
0)) "Long ")
859 "mouse-1" (substring msg
7)))))))
862 (defun mouse-drag-region-1 (start-event)
863 (mouse-minibuffer-check start-event
)
864 (let* ((echo-keystrokes 0)
865 (start-posn (event-start start-event
))
866 (start-point (posn-point start-posn
))
867 (start-window (posn-window start-posn
))
868 (start-window-start (window-start start-window
))
869 (start-frame (window-frame start-window
))
870 (start-hscroll (window-hscroll start-window
))
871 (bounds (window-edges start-window
))
872 (make-cursor-line-fully-visible nil
)
874 (bottom (if (window-minibuffer-p start-window
)
876 ;; Don't count the mode line.
877 (1- (nth 3 bounds
))))
878 (on-link (and mouse-1-click-follows-link
879 (or mouse-1-click-in-non-selected-windows
880 (eq start-window
(selected-window)))))
882 (click-count (1- (event-click-count start-event
))))
883 (setq mouse-selection-click-count click-count
)
884 (setq mouse-selection-click-count-buffer
(current-buffer))
885 (mouse-set-point start-event
)
886 ;; In case the down click is in the middle of some intangible text,
887 ;; use the end of that text, and put it in START-POINT.
888 (if (< (point) start-point
)
889 (goto-char start-point
))
890 (setq start-point
(point))
891 (setq on-link
(and on-link
892 (mouse-on-link-p start-point
)))
893 (setq remap-double-click
(and on-link
894 (eq mouse-1-click-follows-link
'double
)
896 (if remap-double-click
;; Don't expand mouse overlay in links
897 (setq click-count
0))
898 (let ((range (mouse-start-end start-point start-point click-count
)))
899 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
)
900 (window-buffer start-window
))
901 (overlay-put mouse-drag-overlay
'window
(selected-window)))
903 ;; end-of-range is used only in the single-click case.
904 ;; It is the place where the drag has reached so far
905 ;; (but not outside the window where the drag started).
906 (let (event end end-point last-end-point
(end-of-range (point)))
909 (setq event
(read-event))
910 (or (mouse-movement-p event
)
911 (memq (car-safe event
) '(switch-frame select-window
))))
912 (if (memq (car-safe event
) '(switch-frame select-window
))
914 (setq end
(event-end event
)
915 end-point
(posn-point end
))
916 (if (numberp end-point
)
917 (setq last-end-point end-point
))
920 ;; Are we moving within the original window?
921 ((and (eq (posn-window end
) start-window
)
922 (integer-or-marker-p end-point
))
923 ;; Go to START-POINT first, so that when we move to END-POINT,
924 ;; if it's in the middle of intangible text,
925 ;; point jumps in the direction away from START-POINT.
926 (goto-char start-point
)
927 (goto-char end-point
)
928 (if (zerop (% click-count
3))
929 (setq end-of-range
(point)))
930 (let ((range (mouse-start-end start-point
(point) click-count
)))
931 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
))))
934 (let ((mouse-row (cdr (cdr (mouse-position)))))
938 (mouse-scroll-subr start-window
(- mouse-row top
)
939 mouse-drag-overlay start-point
)
940 ;; Without this, point tends to jump back to the starting
941 ;; position where the mouse button was pressed down.
942 (setq end-of-range
(overlay-start mouse-drag-overlay
)))
943 ((>= mouse-row bottom
)
944 (mouse-scroll-subr start-window
(1+ (- mouse-row bottom
))
945 mouse-drag-overlay start-point
)
946 (setq end-of-range
(overlay-end mouse-drag-overlay
))))))))))
948 ;; In case we did not get a mouse-motion event
949 ;; for the final move of the mouse before a drag event
950 ;; pretend that we did get one.
951 (when (and (memq 'drag
(event-modifiers (car-safe event
)))
952 (setq end
(event-end event
)
953 end-point
(posn-point end
))
954 (eq (posn-window end
) start-window
)
955 (integer-or-marker-p end-point
))
956 ;; Go to START-POINT first, so that when we move to END-POINT,
957 ;; if it's in the middle of intangible text,
958 ;; point jumps in the direction away from START-POINT.
959 (goto-char start-point
)
960 (goto-char end-point
)
961 (if (zerop (% click-count
3))
962 (setq end-of-range
(point)))
963 (let ((range (mouse-start-end start-point
(point) click-count
)))
964 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
))))
967 (let ((fun (key-binding (vector (car event
)))))
968 ;; Run the binding of the terminating up-event, if possible.
969 ;; In the case of a multiple click, it gives the wrong results,
970 ;; because it would fail to set up a region.
971 (if (not (= (overlay-start mouse-drag-overlay
)
972 (overlay-end mouse-drag-overlay
)))
974 (if (numberp (posn-point (event-end event
)))
975 (posn-point (event-end event
))
977 ;; The end that comes from where we ended the drag.
980 (if (and stop-point
(< stop-point start-point
))
981 (overlay-start mouse-drag-overlay
)
982 (overlay-end mouse-drag-overlay
)))
983 ;; The end that comes from where we started the drag.
986 (- (+ (overlay-end mouse-drag-overlay
)
987 (overlay-start mouse-drag-overlay
))
989 last-command this-command
)
990 (push-mark region-commencement t t
)
991 (goto-char region-termination
)
992 ;; Don't let copy-region-as-kill set deactivate-mark.
993 (when mouse-drag-copy-region
994 (let (deactivate-mark)
995 (copy-region-as-kill (point) (mark t
))))
996 (let ((buffer (current-buffer)))
998 ;; mouse-show-mark can call read-event,
999 ;; and that means the Emacs server could switch buffers
1000 ;; under us. If that happened,
1001 ;; avoid trying to use the region.
1002 (and (mark t
) mark-active
1003 (eq buffer
(current-buffer))
1004 (mouse-set-region-1))))
1005 (delete-overlay mouse-drag-overlay
)
1006 ;; Run the binding of the terminating up-event.
1007 (when (and (functionp fun
)
1008 (= start-hscroll
(window-hscroll start-window
))
1009 ;; Don't run the up-event handler if the
1010 ;; window start changed in a redisplay after
1011 ;; the mouse-set-point for the down-mouse
1012 ;; event at the beginning of this function.
1013 ;; When the window start has changed, the
1014 ;; up-mouse event will contain a different
1015 ;; position due to the new window contents,
1016 ;; and point is set again.
1018 (= (window-start start-window
)
1019 start-window-start
)))
1021 (or (not end-point
) (= end-point start-point
))
1023 (or remap-double-click
1025 (not (eq mouse-1-click-follows-link
'double
))
1027 (= (event-click-count event
) 1)
1028 (not (input-pending-p))
1029 (or (not (integerp mouse-1-click-follows-link
))
1030 (let ((t0 (posn-timestamp (event-start start-event
)))
1031 (t1 (posn-timestamp (event-end event
))))
1032 (and (integerp t0
) (integerp t1
)
1033 (if (> mouse-1-click-follows-link
0)
1034 (<= (- t1 t0
) mouse-1-click-follows-link
)
1035 (< (- t0 t1
) mouse-1-click-follows-link
)))))
1036 (or (not double-click-time
)
1037 (sit-for 0 (if (integerp double-click-time
)
1038 double-click-time
500) t
)))))
1039 (if (or (vectorp on-link
) (stringp on-link
))
1040 (setq event
(aref on-link
0))
1041 (setcar event
'mouse-2
)))
1042 (setq unread-command-events
1043 (cons event unread-command-events
)))))
1044 (delete-overlay mouse-drag-overlay
)))))
1046 ;; Commands to handle xterm-style multiple clicks.
1048 (defun mouse-skip-word (dir)
1049 "Skip over word, over whitespace, or over identical punctuation.
1050 If DIR is positive skip forward; if negative, skip backward."
1051 (let* ((char (following-char))
1052 (syntax (char-to-string (char-syntax char
))))
1053 (cond ((string= syntax
"w")
1054 ;; Here, we can't use skip-syntax-forward/backward because
1055 ;; they don't pay attention to word-separating-categories,
1056 ;; and thus they will skip over a true word boundary. So,
1057 ;; we simularte the original behaviour by using
1060 (if (not (looking-at "\\<"))
1062 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1064 ((string= syntax
" ")
1066 (skip-syntax-backward syntax
)
1067 (skip-syntax-forward syntax
)))
1068 ((string= syntax
"_")
1070 (skip-syntax-backward "w_")
1071 (skip-syntax-forward "w_")))
1073 (while (and (not (bobp)) (= (preceding-char) char
))
1076 (while (and (not (eobp)) (= (following-char) char
))
1077 (forward-char 1))))))
1079 (defun mouse-start-end (start end mode
)
1080 "Return a list of region bounds based on START and END according to MODE.
1081 If MODE is 0 then set point to (min START END), mark to (max START END).
1082 If MODE is 1 then set point to start of word at (min START END),
1083 mark to end of word at (max START END).
1084 If MODE is 2 then do the same for lines."
1089 (setq mode
(mod mode
3))
1095 (= (char-syntax (char-after start
)) ?\
())
1104 (= (char-syntax (char-after start
)) ?\
)))
1105 (list (save-excursion
1106 (goto-char (1+ start
))
1113 (= (char-syntax (char-after start
)) ?
\"))
1114 (let ((open (or (eq start
(point-min))
1116 (goto-char (- start
1))
1117 (looking-at "\\s(\\|\\s \\|\\s>")))))
1127 (list (save-excursion
1130 (goto-char (1+ start
))
1136 (list (save-excursion
1138 (mouse-skip-word -
1)
1145 (list (save-excursion
1147 (beginning-of-line 1)
1154 ;; Subroutine: set the mark where CLICK happened,
1155 ;; but don't do anything else.
1156 (defun mouse-set-mark-fast (click)
1157 (mouse-minibuffer-check click
)
1158 (let ((posn (event-start click
)))
1159 (select-window (posn-window posn
))
1160 (if (numberp (posn-point posn
))
1161 (push-mark (posn-point posn
) t t
))))
1163 (defun mouse-undouble-last-event (events)
1164 (let* ((index (1- (length events
)))
1165 (last (nthcdr index events
))
1167 (basic (event-basic-type event
))
1168 (old-modifiers (event-modifiers event
))
1169 (modifiers (delq 'double
(delq 'triple
(copy-sequence old-modifiers
))))
1172 ;; Use reverse, not nreverse, since event-modifiers
1173 ;; does not copy the list it returns.
1174 (cons (event-convert-list (reverse (cons basic modifiers
)))
1178 (if (and (not (equal modifiers old-modifiers
))
1179 (key-binding (apply 'vector events
)))
1184 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1186 (defvar mouse-region-delete-keys
'([delete] [deletechar])
1187 "List of keys which shall cause the mouse region to be deleted.")
1189 (defun mouse-show-mark ()
1190 (let ((inhibit-quit t)
1192 event events key ignore
1193 (x-lost-selection-functions
1194 (when (boundp 'x-lost-selection-functions)
1195 (copy-sequence x-lost-selection-functions))))
1196 (add-hook 'x-lost-selection-functions
1198 (when (eq seltype 'PRIMARY)
1200 (throw 'mouse-show-mark t))))
1201 (if transient-mark-mode
1202 (delete-overlay mouse-drag-overlay)
1203 (move-overlay mouse-drag-overlay (point) (mark t)))
1204 (catch 'mouse-show-mark
1205 ;; In this loop, execute scroll bar and switch-frame events.
1206 ;; Should we similarly handle `select-window' events? --Stef
1207 ;; Also ignore down-events that are undefined.
1208 (while (progn (setq event (read-event))
1209 (setq events (append events (list event)))
1210 (setq key (apply 'vector events))
1211 (or (and (consp event)
1212 (eq (car event) 'switch-frame))
1214 (eq (posn-point (event-end event))
1215 'vertical-scroll-bar))
1216 (and (memq 'down (event-modifiers event))
1217 (not (key-binding key))
1218 (not (mouse-undouble-last-event events))
1219 (not (member key mouse-region-delete-keys)))))
1221 (or (eq (car event) 'switch-frame)
1222 (eq (posn-point (event-end event))
1223 'vertical-scroll-bar))
1224 (let ((keys (vector 'vertical-scroll-bar event)))
1225 (and (key-binding keys)
1227 (call-interactively (key-binding keys)
1229 (setq events nil)))))))
1230 ;; If we lost the selection, just turn off the highlighting.
1232 ;; For certain special keys, delete the region.
1233 (if (member key mouse-region-delete-keys)
1234 (delete-region (mark t) (point))
1235 ;; Otherwise, unread the key so it gets executed normally.
1236 (setq unread-command-events
1237 (nconc events unread-command-events))))
1238 (setq quit-flag nil)
1239 (unless transient-mark-mode
1240 (delete-overlay mouse-drag-overlay))))
1242 (defun mouse-set-mark (click)
1243 "Set mark at the position clicked on with the mouse.
1244 Display cursor at that position for a second.
1245 This must be bound to a mouse click."
1247 (mouse-minibuffer-check click)
1248 (select-window (posn-window (event-start click)))
1249 ;; We don't use save-excursion because that preserves the mark too.
1250 (let ((point-save (point)))
1252 (progn (mouse-set-point click)
1254 (or transient-mark-mode
1256 (goto-char point-save))))
1258 (defun mouse-kill (click)
1259 "Kill the region between point and the mouse click.
1260 The text is saved in the kill ring, as with \\[kill-region]."
1262 (mouse-minibuffer-check click)
1263 (let* ((posn (event-start click))
1264 (click-posn (posn-point posn)))
1265 (select-window (posn-window posn))
1266 (if (numberp click-posn)
1267 (kill-region (min (point) click-posn)
1268 (max (point) click-posn)))))
1270 (defun mouse-yank-at-click (click arg)
1271 "Insert the last stretch of killed text at the position clicked on.
1272 Also move point to one end of the text thus inserted (normally the end),
1273 and set mark at the beginning.
1274 Prefix arguments are interpreted as with \\[yank].
1275 If `mouse-yank-at-point' is non-nil, insert at point
1276 regardless of where you click."
1277 (interactive "e\nP")
1278 ;; Give temporary modes such as isearch a chance to turn off.
1279 (run-hooks 'mouse-leave-buffer-hook)
1280 (or mouse-yank-at-point (mouse-set-point click))
1281 (setq this-command 'yank)
1282 (setq mouse-selection-click-count 0)
1285 (defun mouse-kill-ring-save (click)
1286 "Copy the region between point and the mouse click in the kill ring.
1287 This does not delete the region; it acts like \\[kill-ring-save]."
1289 (mouse-set-mark-fast click)
1290 (let (this-command last-command)
1291 (kill-ring-save (point) (mark t)))
1294 ;;; This function used to delete the text between point and the mouse
1295 ;;; whenever it was equal to the front of the kill ring, but some
1296 ;;; people found that confusing.
1298 ;;; A list (TEXT START END), describing the text and position of the last
1299 ;;; invocation of mouse-save-then-kill.
1300 (defvar mouse-save-then-kill-posn nil)
1302 (defun mouse-save-then-kill-delete-region (beg end)
1303 ;; We must make our own undo boundaries
1304 ;; because they happen automatically only for the current buffer.
1306 (if (or (= beg end) (eq buffer-undo-list t))
1307 ;; If we have no undo list in this buffer,
1309 (delete-region beg end)
1310 ;; Delete, but make the undo-list entry share with the kill ring.
1311 ;; First, delete just one char, so in case buffer is being modified
1312 ;; for the first time, the undo list records that fact.
1313 (let (before-change-functions after-change-functions)
1315 (+ beg (if (> end beg) 1 -1))))
1316 (let ((buffer-undo-list buffer-undo-list))
1317 ;; Undo that deletion--but don't change the undo list!
1318 (let (before-change-functions after-change-functions)
1319 (primitive-undo 1 buffer-undo-list))
1320 ;; Now delete the rest of the specified region,
1321 ;; but don't record it.
1322 (setq buffer-undo-list t)
1323 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1324 (error "Lossage in mouse-save-then-kill-delete-region"))
1325 (delete-region beg end))
1326 (let ((tail buffer-undo-list))
1327 ;; Search back in buffer-undo-list for the string
1328 ;; that came from deleting one character.
1329 (while (and tail (not (stringp (car (car tail)))))
1330 (setq tail (cdr tail)))
1331 ;; Replace it with an entry for the entire deleted text.
1333 (setcar tail (cons (car kill-ring) (min beg end))))))
1336 (defun mouse-save-then-kill (click)
1337 "Save text to point in kill ring; the second time, kill the text.
1338 If the text between point and the mouse is the same as what's
1339 at the front of the kill ring, this deletes the text.
1340 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1341 which prepares for a second click to delete the text.
1343 If you have selected words or lines, this command extends the
1344 selection through the word or line clicked on. If you do this
1345 again in a different position, it extends the selection again.
1346 If you do this twice in the same position, the selection is killed."
1348 (let ((before-scroll
1349 (with-current-buffer (window-buffer (posn-window (event-start click)))
1350 point-before-scroll)))
1351 (mouse-minibuffer-check click)
1352 (let ((click-posn (posn-point (event-start click)))
1353 ;; Don't let a subsequent kill command append to this one:
1354 ;; prevent setting this-command to kill-region.
1355 (this-command this-command))
1356 (if (and (save-excursion
1357 (set-buffer (window-buffer (posn-window (event-start click))))
1358 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1359 ;; Don't be fooled by a recent click in some other buffer.
1360 (eq mouse-selection-click-count-buffer
1361 (current-buffer)))))
1362 (if (not (and (eq last-command 'mouse-save-then-kill)
1364 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1365 ;; Find both ends of the object selected by this click.
1367 (mouse-start-end click-posn click-posn
1368 mouse-selection-click-count)))
1369 ;; Move whichever end is closer to the click.
1370 ;; That's what xterm does, and it seems reasonable.
1371 (if (< (abs (- click-posn (mark t)))
1372 (abs (- click-posn (point))))
1373 (set-mark (car range))
1374 (goto-char (nth 1 range)))
1375 ;; We have already put the old region in the kill ring.
1376 ;; Replace it with the extended region.
1377 ;; (It would be annoying to make a separate entry.)
1378 (kill-new (buffer-substring (point) (mark t)) t)
1379 (mouse-set-region-1)
1380 ;; Arrange for a repeated mouse-3 to kill this region.
1381 (setq mouse-save-then-kill-posn
1382 (list (car kill-ring) (point) click-posn))
1384 ;; If we click this button again without moving it,
1386 (mouse-save-then-kill-delete-region (mark) (point))
1387 (setq mouse-selection-click-count 0)
1388 (setq mouse-save-then-kill-posn nil))
1389 (if (and (eq last-command 'mouse-save-then-kill)
1390 mouse-save-then-kill-posn
1391 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1392 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1393 ;; If this is the second time we've called
1394 ;; mouse-save-then-kill, delete the text from the buffer.
1396 (mouse-save-then-kill-delete-region (point) (mark))
1397 ;; After we kill, another click counts as "the first time".
1398 (setq mouse-save-then-kill-posn nil))
1399 ;; This is not a repetition.
1400 ;; We are adjusting an old selection or creating a new one.
1401 (if (or (and (eq last-command 'mouse-save-then-kill)
1402 mouse-save-then-kill-posn)
1403 (and mark-active transient-mark-mode)
1404 (and (memq last-command
1405 '(mouse-drag-region mouse-set-region))
1406 (or mark-even-if-inactive
1407 (not transient-mark-mode))))
1408 ;; We have a selection or suitable region, so adjust it.
1409 (let* ((posn (event-start click))
1410 (new (posn-point posn)))
1411 (select-window (posn-window posn))
1414 ;; Move whichever end of the region is closer to the click.
1415 ;; That is what xterm does, and it seems reasonable.
1416 (if (<= (abs (- new (point))) (abs (- new (mark t))))
1419 (setq deactivate-mark nil)))
1420 (kill-new (buffer-substring (point) (mark t)) t)
1422 ;; Set the mark where point is, then move where clicked.
1423 (mouse-set-mark-fast click)
1425 (goto-char before-scroll))
1426 (exchange-point-and-mark)
1427 (kill-new (buffer-substring (point) (mark t)))
1429 (mouse-set-region-1)
1430 (setq mouse-save-then-kill-posn
1431 (list (car kill-ring) (point) click-posn)))))))
1433 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1434 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1435 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1436 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1437 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1439 ;; An overlay which records the current secondary selection
1440 ;; or else is deleted when there is no secondary selection.
1442 (defvar mouse-secondary-overlay nil)
1444 (defvar mouse-secondary-click-count 0)
1446 ;; A marker which records the specified first end for a secondary selection.
1448 (defvar mouse-secondary-start nil)
1450 (defun mouse-start-secondary (click)
1451 "Set one end of the secondary selection to the position clicked on.
1452 Use \\[mouse-secondary-save-then-kill] to set the other end
1453 and complete the secondary selection."
1455 (mouse-minibuffer-check click)
1456 (let ((posn (event-start click)))
1458 (set-buffer (window-buffer (posn-window posn)))
1459 ;; Cancel any preexisting secondary selection.
1460 (if mouse-secondary-overlay
1461 (delete-overlay mouse-secondary-overlay))
1462 (if (numberp (posn-point posn))
1464 (or mouse-secondary-start
1465 (setq mouse-secondary-start (make-marker)))
1466 (move-marker mouse-secondary-start (posn-point posn)))))))
1468 (defun mouse-set-secondary (click)
1469 "Set the secondary selection to the text that the mouse is dragged over.
1470 This must be bound to a mouse drag event."
1472 (mouse-minibuffer-check click)
1473 (let ((posn (event-start click))
1475 (end (event-end click)))
1477 (set-buffer (window-buffer (posn-window posn)))
1478 (if (numberp (posn-point posn))
1479 (setq beg (posn-point posn)))
1480 (if mouse-secondary-overlay
1481 (move-overlay mouse-secondary-overlay beg (posn-point end))
1482 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1483 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1485 (defun mouse-drag-secondary (start-event)
1486 "Set the secondary selection to the text that the mouse is dragged over.
1487 Highlight the drag area as you move the mouse.
1488 This must be bound to a button-down mouse event.
1489 The function returns a non-nil value if it creates a secondary selection."
1491 (mouse-minibuffer-check start-event)
1492 (let* ((echo-keystrokes 0)
1493 (start-posn (event-start start-event))
1494 (start-point (posn-point start-posn))
1495 (start-window (posn-window start-posn))
1496 (start-frame (window-frame start-window))
1497 (bounds (window-edges start-window))
1498 (top (nth 1 bounds))
1499 (bottom (if (window-minibuffer-p start-window)
1501 ;; Don't count the mode line.
1502 (1- (nth 3 bounds))))
1503 (click-count (1- (event-click-count start-event))))
1505 (set-buffer (window-buffer start-window))
1506 (setq mouse-secondary-click-count click-count)
1507 (or mouse-secondary-overlay
1508 (setq mouse-secondary-overlay
1509 (make-overlay (point) (point))))
1510 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1511 (if (> (mod click-count 3) 0)
1512 ;; Double or triple press: make an initial selection
1513 ;; of one word or line.
1514 (let ((range (mouse-start-end start-point start-point click-count)))
1515 (set-marker mouse-secondary-start nil)
1516 (move-overlay mouse-secondary-overlay 1 1
1517 (window-buffer start-window))
1518 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1519 (window-buffer start-window)))
1520 ;; Single-press: cancel any preexisting secondary selection.
1521 (or mouse-secondary-start
1522 (setq mouse-secondary-start (make-marker)))
1523 (set-marker mouse-secondary-start start-point)
1524 (delete-overlay mouse-secondary-overlay))
1525 (let (event end end-point)
1528 (setq event (read-event))
1529 (or (mouse-movement-p event)
1530 (memq (car-safe event) '(switch-frame select-window))))
1532 (if (memq (car-safe event) '(switch-frame select-window))
1534 (setq end (event-end event)
1535 end-point (posn-point end))
1537 ;; Are we moving within the original window?
1538 ((and (eq (posn-window end) start-window)
1539 (integer-or-marker-p end-point))
1540 (let ((range (mouse-start-end start-point end-point
1542 (if (or (/= start-point end-point)
1543 (null (marker-position mouse-secondary-start)))
1545 (set-marker mouse-secondary-start nil)
1546 (move-overlay mouse-secondary-overlay
1547 (car range) (nth 1 range))))))
1549 (let ((mouse-row (cdr (cdr (mouse-position)))))
1553 (mouse-scroll-subr start-window (- mouse-row top)
1554 mouse-secondary-overlay start-point))
1555 ((>= mouse-row bottom)
1556 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1557 mouse-secondary-overlay start-point)))))))))
1560 (if (marker-position mouse-secondary-start)
1561 (save-window-excursion
1562 (delete-overlay mouse-secondary-overlay)
1563 (x-set-selection 'SECONDARY nil)
1564 (select-window start-window)
1566 (goto-char mouse-secondary-start)
1571 (buffer-substring (overlay-start mouse-secondary-overlay)
1572 (overlay-end mouse-secondary-overlay)))))))))
1574 (defun mouse-yank-secondary (click)
1575 "Insert the secondary selection at the position clicked on.
1576 Move point to the end of the inserted text.
1577 If `mouse-yank-at-point' is non-nil, insert at point
1578 regardless of where you click."
1580 ;; Give temporary modes such as isearch a chance to turn off.
1581 (run-hooks 'mouse-leave-buffer-hook)
1582 (or mouse-yank-at-point (mouse-set-point click))
1583 (insert (x-get-selection 'SECONDARY)))
1585 (defun mouse-kill-secondary ()
1586 "Kill the text in the secondary selection.
1587 This is intended more as a keyboard command than as a mouse command
1588 but it can work as either one.
1590 The current buffer (in case of keyboard use), or the buffer clicked on,
1591 must be the one that the secondary selection is in. This requirement
1592 is to prevent accidents."
1594 (let* ((keys (this-command-keys))
1595 (click (elt keys (1- (length keys)))))
1596 (or (eq (overlay-buffer mouse-secondary-overlay)
1598 (window-buffer (posn-window (event-start click)))
1600 (error "Select or click on the buffer where the secondary selection is")))
1603 (set-buffer (overlay-buffer mouse-secondary-overlay))
1604 (kill-region (overlay-start mouse-secondary-overlay)
1605 (overlay-end mouse-secondary-overlay))))
1606 (delete-overlay mouse-secondary-overlay)
1607 ;;; (x-set-selection 'SECONDARY nil)
1608 (setq mouse-secondary-overlay nil))
1610 (defun mouse-secondary-save-then-kill (click)
1611 "Save text to point in kill ring; the second time, kill the text.
1612 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1613 If the text between where you did \\[mouse-start-secondary] and where
1614 you use this command matches the text at the front of the kill ring,
1615 this command deletes the text.
1616 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1617 which prepares for a second click with this command to delete the text.
1619 If you have already made a secondary selection in that buffer,
1620 this command extends or retracts the selection to where you click.
1621 If you do this again in a different position, it extends or retracts
1622 again. If you do this twice in the same position, it kills the selection."
1624 (mouse-minibuffer-check click)
1625 (let ((posn (event-start click))
1626 (click-posn (posn-point (event-start click)))
1627 ;; Don't let a subsequent kill command append to this one:
1628 ;; prevent setting this-command to kill-region.
1629 (this-command this-command))
1630 (or (eq (window-buffer (posn-window posn))
1631 (or (and mouse-secondary-overlay
1632 (overlay-buffer mouse-secondary-overlay))
1633 (if mouse-secondary-start
1634 (marker-buffer mouse-secondary-start))))
1635 (error "Wrong buffer"))
1637 (set-buffer (window-buffer (posn-window posn)))
1638 (if (> (mod mouse-secondary-click-count 3) 0)
1639 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1641 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1642 ;; Find both ends of the object selected by this click.
1644 (mouse-start-end click-posn click-posn
1645 mouse-secondary-click-count)))
1646 ;; Move whichever end is closer to the click.
1647 ;; That's what xterm does, and it seems reasonable.
1648 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1649 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1650 (move-overlay mouse-secondary-overlay (car range)
1651 (overlay-end mouse-secondary-overlay))
1652 (move-overlay mouse-secondary-overlay
1653 (overlay-start mouse-secondary-overlay)
1655 ;; We have already put the old region in the kill ring.
1656 ;; Replace it with the extended region.
1657 ;; (It would be annoying to make a separate entry.)
1658 (kill-new (buffer-substring
1659 (overlay-start mouse-secondary-overlay)
1660 (overlay-end mouse-secondary-overlay)) t)
1661 ;; Arrange for a repeated mouse-3 to kill this region.
1662 (setq mouse-save-then-kill-posn
1663 (list (car kill-ring) (point) click-posn)))
1664 ;; If we click this button again without moving it,
1667 (mouse-save-then-kill-delete-region
1668 (overlay-start mouse-secondary-overlay)
1669 (overlay-end mouse-secondary-overlay))
1670 (setq mouse-save-then-kill-posn nil)
1671 (setq mouse-secondary-click-count 0)
1672 (delete-overlay mouse-secondary-overlay)))
1673 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1674 mouse-save-then-kill-posn
1675 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1676 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1677 ;; If this is the second time we've called
1678 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1680 (mouse-save-then-kill-delete-region
1681 (overlay-start mouse-secondary-overlay)
1682 (overlay-end mouse-secondary-overlay))
1683 (setq mouse-save-then-kill-posn nil)
1684 (delete-overlay mouse-secondary-overlay))
1685 (if (overlay-start mouse-secondary-overlay)
1686 ;; We have a selection, so adjust it.
1688 (if (numberp click-posn)
1690 ;; Move whichever end of the region is closer to the click.
1691 ;; That is what xterm does, and it seems reasonable.
1692 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1693 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1694 (move-overlay mouse-secondary-overlay click-posn
1695 (overlay-end mouse-secondary-overlay))
1696 (move-overlay mouse-secondary-overlay
1697 (overlay-start mouse-secondary-overlay)
1699 (setq deactivate-mark nil)))
1700 (if (eq last-command 'mouse-secondary-save-then-kill)
1701 ;; If the front of the kill ring comes from
1702 ;; an immediately previous use of this command,
1703 ;; replace it with the extended region.
1704 ;; (It would be annoying to make a separate entry.)
1705 (kill-new (buffer-substring
1706 (overlay-start mouse-secondary-overlay)
1707 (overlay-end mouse-secondary-overlay)) t)
1708 (let (deactivate-mark)
1709 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1710 (overlay-end mouse-secondary-overlay)))))
1711 (if mouse-secondary-start
1712 ;; All we have is one end of a selection,
1713 ;; so put the other end here.
1714 (let ((start (+ 0 mouse-secondary-start)))
1715 (kill-ring-save start click-posn)
1716 (if mouse-secondary-overlay
1717 (move-overlay mouse-secondary-overlay start click-posn)
1718 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1719 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1720 (setq mouse-save-then-kill-posn
1721 (list (car kill-ring) (point) click-posn))))
1722 (if (overlay-buffer mouse-secondary-overlay)
1723 (x-set-selection 'SECONDARY
1725 (overlay-start mouse-secondary-overlay)
1726 (overlay-end mouse-secondary-overlay)))))))
1728 (defcustom mouse-buffer-menu-maxlen 20
1729 "*Number of buffers in one pane (submenu) of the buffer menu.
1730 If we have lots of buffers, divide them into groups of
1731 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1735 (defcustom mouse-buffer-menu-mode-mult 4
1736 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1737 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1738 will split the buffer menu by the major modes (see
1739 `mouse-buffer-menu-mode-groups') or just by menu length.
1740 Set to 1 (or even 0!) if you want to group by major mode always, and to
1741 a large number if you prefer a mixed multitude. The default is 4."
1746 (defvar mouse-buffer-menu-mode-groups
1747 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1748 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1753 ("Outline" . "Text")
1754 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1755 ("log\\|diff\\|vc\\|cvs" . "Version Control") ; "Change Management"?
1757 "How to group various major modes together in \\[mouse-buffer-menu].
1758 Each element has the form (REGEXP . GROUPNAME).
1759 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1761 (defun mouse-buffer-menu (event)
1762 "Pop up a menu of buffers for selection with the mouse.
1763 This switches buffers in the window that you clicked on,
1764 and selects that window."
1766 (mouse-minibuffer-check event)
1767 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1768 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1769 (let ((tail buffers))
1771 ;; Divide all buffers into buckets for various major modes.
1772 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1773 (with-current-buffer (car tail)
1774 (let* ((adjusted-major-mode major-mode) elt)
1775 (let ((tail mouse-buffer-menu-mode-groups))
1777 (if (string-match (car (car tail)) mode-name)
1778 (setq adjusted-major-mode (cdr (car tail))))
1779 (setq tail (cdr tail))))
1780 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1782 (setq elt (list adjusted-major-mode
1783 (if (stringp adjusted-major-mode)
1786 split-by-major-mode (cons elt split-by-major-mode)))
1787 (or (memq (car tail) (cdr (cdr elt)))
1788 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1789 (setq tail (cdr tail))))
1790 ;; Compute the sum of squares of sizes of the major-mode buckets.
1791 (let ((tail split-by-major-mode))
1792 (setq sum-of-squares 0)
1794 (setq sum-of-squares
1796 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1797 (setq tail (cdr tail))))
1798 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1799 (* (length buffers) (length buffers)))
1800 ;; Subdividing by major modes really helps, so let's do it.
1801 (let (subdivided-menus (buffers-left (length buffers)))
1802 ;; Sort the list to put the most popular major modes first.
1803 (setq split-by-major-mode
1804 (sort split-by-major-mode
1805 (function (lambda (elt1 elt2)
1806 (> (length elt1) (length elt2))))))
1807 ;; Make a separate submenu for each major mode
1808 ;; that has more than one buffer,
1809 ;; unless all the remaining buffers are less than 1/10 of them.
1810 (while (and split-by-major-mode
1811 (and (> (length (car split-by-major-mode)) 3)
1812 (> (* buffers-left 10) (length buffers))))
1813 (let ((this-mode-list (mouse-buffer-menu-alist
1814 (cdr (cdr (car split-by-major-mode))))))
1816 (setq subdivided-menus
1818 (nth 1 (car split-by-major-mode))
1820 subdivided-menus))))
1822 (- buffers-left (length (cdr (car split-by-major-mode)))))
1823 (setq split-by-major-mode (cdr split-by-major-mode)))
1824 ;; If any major modes are left over,
1825 ;; make a single submenu for them.
1826 (if split-by-major-mode
1828 (mouse-buffer-menu-alist
1829 ;; we don't need split-by-major-mode any more,
1830 ;; so we can ditch it with nconc.
1831 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1833 (setq subdivided-menus
1834 (cons (cons "Others" others-list)
1835 subdivided-menus)))))
1836 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1838 (setq alist (mouse-buffer-menu-alist buffers))
1839 (setq menu (cons "Buffer Menu"
1840 (mouse-buffer-menu-split "Select Buffer" alist)))))
1841 (let ((buf (x-popup-menu event menu))
1842 (window (posn-window (event-start event))))
1845 (if (framep window) (frame-selected-window window)
1847 (switch-to-buffer buf)))))
1849 (defun mouse-buffer-menu-alist (buffers)
1855 (function (lambda (elt1 elt2)
1856 (string< (buffer-name elt1) (buffer-name elt2))))))
1859 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1862 (length (buffer-name (car tail))))))
1863 (setq tail (cdr tail)))
1866 (let ((elt (car tail)))
1867 (if (/= (aref (buffer-name elt) 0) ?\ )
1872 (format "%%%ds %%s%%s %%s" maxlen)
1874 (if (buffer-modified-p elt) "*" " ")
1877 (if buffer-read-only "%" " "))
1878 (or (buffer-file-name elt)
1881 (if list-buffers-directory
1883 list-buffers-directory)))
1887 (setq tail (cdr tail)))
1888 ;; Compensate for the reversal that the above loop does.
1891 (defun mouse-buffer-menu-split (title alist)
1892 ;; If we have lots of buffers, divide them into groups of 20
1893 ;; and make a pane (or submenu) for each one.
1894 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1895 (let ((alist alist) sublists next
1898 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1899 ;; and make them the next element of sublist.
1900 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1902 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1904 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1908 (nreverse sublists))
1909 ;; Few buffers--put them all in one pane.
1910 (list (cons title alist))))
1912 ;;; These need to be rewritten for the new scroll bar implementation.
1914 ;;;!! ;; Commands for the scroll bar.
1916 ;;;!! (defun mouse-scroll-down (click)
1917 ;;;!! (interactive "@e")
1918 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1920 ;;;!! (defun mouse-scroll-up (click)
1921 ;;;!! (interactive "@e")
1922 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1924 ;;;!! (defun mouse-scroll-down-full ()
1925 ;;;!! (interactive "@")
1926 ;;;!! (scroll-down nil))
1928 ;;;!! (defun mouse-scroll-up-full ()
1929 ;;;!! (interactive "@")
1930 ;;;!! (scroll-up nil))
1932 ;;;!! (defun mouse-scroll-move-cursor (click)
1933 ;;;!! (interactive "@e")
1934 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1936 ;;;!! (defun mouse-scroll-absolute (event)
1937 ;;;!! (interactive "@e")
1938 ;;;!! (let* ((pos (car event))
1939 ;;;!! (position (car pos))
1940 ;;;!! (length (car (cdr pos))))
1941 ;;;!! (if (<= length 0) (setq length 1))
1942 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1943 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1946 ;;;!! scale-factor)))
1947 ;;;!! (goto-char newpos)
1948 ;;;!! (recenter '(4)))))
1950 ;;;!! (defun mouse-scroll-left (click)
1951 ;;;!! (interactive "@e")
1952 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1954 ;;;!! (defun mouse-scroll-right (click)
1955 ;;;!! (interactive "@e")
1956 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1958 ;;;!! (defun mouse-scroll-left-full ()
1959 ;;;!! (interactive "@")
1960 ;;;!! (scroll-left nil))
1962 ;;;!! (defun mouse-scroll-right-full ()
1963 ;;;!! (interactive "@")
1964 ;;;!! (scroll-right nil))
1966 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1967 ;;;!! (interactive "@e")
1968 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1970 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1971 ;;;!! (interactive "@e")
1972 ;;;!! (let* ((pos (car event))
1973 ;;;!! (position (car pos))
1974 ;;;!! (length (car (cdr pos))))
1975 ;;;!! (set-window-hscroll (selected-window) 33)))
1977 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1978 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1979 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1981 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1982 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1983 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1985 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1986 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1987 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1989 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1990 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1991 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1993 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1994 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1995 ;;;!! 'mouse-scroll-absolute-horizontally)
1996 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1998 ;;;!! (global-set-key [horizontal-slider mouse-1]
1999 ;;;!! 'mouse-scroll-move-cursor-horizontally)
2000 ;;;!! (global-set-key [horizontal-slider mouse-2]
2001 ;;;!! 'mouse-scroll-move-cursor-horizontally)
2002 ;;;!! (global-set-key [horizontal-slider mouse-3]
2003 ;;;!! 'mouse-scroll-move-cursor-horizontally)
2005 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
2006 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
2007 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
2009 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
2010 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
2011 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
2013 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
2014 ;;;!! 'mouse-split-window-horizontally)
2015 ;;;!! (global-set-key [mode-line S-mouse-2]
2016 ;;;!! 'mouse-split-window-horizontally)
2017 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
2018 ;;;!! 'mouse-split-window)
2021 ;;;!! ;;;; Here are experimental things being tested. Mouse events
2022 ;;;!! ;;;; are of the form:
2023 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
2026 ;;;!! ;;;; Dynamically track mouse coordinates
2029 ;;;!! ;;(defun track-mouse (event)
2030 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
2031 ;;;!! ;; (interactive "@e")
2032 ;;;!! ;; (while mouse-grabbed
2033 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
2034 ;;;!! ;; (abs-x (car pos))
2035 ;;;!! ;; (abs-y (cdr pos))
2036 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
2037 ;;;!! ;; (list (car pos) (cdr pos))
2038 ;;;!! ;; (selected-window))))
2039 ;;;!! ;; (if (consp relative-coordinate)
2040 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
2041 ;;;!! ;; (car relative-coordinate)
2042 ;;;!! ;; (car (cdr relative-coordinate)))
2043 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
2046 ;;;!! ;; Dynamically put a box around the line indicated by point
2049 ;;;!! ;;(require 'backquote)
2051 ;;;!! ;;(defun mouse-select-buffer-line (event)
2052 ;;;!! ;; (interactive "@e")
2053 ;;;!! ;; (let ((relative-coordinate
2054 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
2055 ;;;!! ;; (abs-y (car (cdr (car event)))))
2056 ;;;!! ;; (if (consp relative-coordinate)
2058 ;;;!! ;; (save-excursion
2059 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2060 ;;;!! ;; (x-draw-rectangle
2061 ;;;!! ;; (selected-screen)
2063 ;;;!! ;; (save-excursion
2064 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2065 ;;;!! ;; (end-of-line)
2066 ;;;!! ;; (push-mark nil t)
2067 ;;;!! ;; (beginning-of-line)
2068 ;;;!! ;; (- (region-end) (region-beginning))) 1))
2069 ;;;!! ;; (sit-for 1)
2070 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
2072 ;;;!! ;;(defvar last-line-drawn nil)
2073 ;;;!! ;;(defvar begin-delim "[^ \t]")
2074 ;;;!! ;;(defvar end-delim "[^ \t]")
2076 ;;;!! ;;(defun mouse-boxing (event)
2077 ;;;!! ;; (interactive "@e")
2078 ;;;!! ;; (save-excursion
2079 ;;;!! ;; (let ((screen (selected-screen)))
2080 ;;;!! ;; (while (= (x-mouse-events) 0)
2081 ;;;!! ;; (let* ((pos (read-mouse-position screen))
2082 ;;;!! ;; (abs-x (car pos))
2083 ;;;!! ;; (abs-y (cdr pos))
2084 ;;;!! ;; (relative-coordinate
2085 ;;;!! ;; (coordinates-in-window-p `(,abs-x ,abs-y)
2086 ;;;!! ;; (selected-window)))
2087 ;;;!! ;; (begin-reg nil)
2088 ;;;!! ;; (end-reg nil)
2089 ;;;!! ;; (end-column nil)
2090 ;;;!! ;; (begin-column nil))
2091 ;;;!! ;; (if (and (consp relative-coordinate)
2092 ;;;!! ;; (or (not last-line-drawn)
2093 ;;;!! ;; (not (= last-line-drawn abs-y))))
2095 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2096 ;;;!! ;; (if (= (following-char) 10)
2099 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
2100 ;;;!! ;; (setq begin-column (1- (current-column)))
2101 ;;;!! ;; (end-of-line)
2102 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
2103 ;;;!! ;; (setq end-column (1+ (current-column)))
2104 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
2105 ;;;!! ;; (x-draw-rectangle screen
2106 ;;;!! ;; (setq last-line-drawn abs-y)
2107 ;;;!! ;; begin-column
2108 ;;;!! ;; (- end-column begin-column) 1))))))))))
2110 ;;;!! ;;(defun mouse-erase-box ()
2111 ;;;!! ;; (interactive)
2112 ;;;!! ;; (if last-line-drawn
2114 ;;;!! ;; (x-erase-rectangle (selected-screen))
2115 ;;;!! ;; (setq last-line-drawn nil))))
2117 ;;;!! ;;; (defun test-x-rectangle ()
2118 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
2119 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
2120 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
2123 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
2126 ;;;!! (defvar double-start nil)
2127 ;;;!! (defconst double-click-interval 300
2128 ;;;!! "Max ticks between clicks")
2130 ;;;!! (defun double-down (event)
2131 ;;;!! (interactive "@e")
2132 ;;;!! (if double-start
2133 ;;;!! (let ((interval (- (nth 4 event) double-start)))
2134 ;;;!! (if (< interval double-click-interval)
2136 ;;;!! (backward-up-list 1)
2137 ;;;!! ;; (message "Interval %d" interval)
2138 ;;;!! (sleep-for 1)))
2139 ;;;!! (setq double-start nil))
2140 ;;;!! (setq double-start (nth 4 event))))
2142 ;;;!! (defun double-up (event)
2143 ;;;!! (interactive "@e")
2144 ;;;!! (and double-start
2145 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
2146 ;;;!! (setq double-start nil)))
2148 ;;;!! ;;; (defun x-test-doubleclick ()
2149 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
2150 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
2151 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
2154 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
2157 ;;;!! (defvar scrolled-lines 0)
2158 ;;;!! (defconst scroll-speed 1)
2160 ;;;!! (defun incr-scroll-down (event)
2161 ;;;!! (interactive "@e")
2162 ;;;!! (setq scrolled-lines 0)
2163 ;;;!! (incremental-scroll scroll-speed))
2165 ;;;!! (defun incr-scroll-up (event)
2166 ;;;!! (interactive "@e")
2167 ;;;!! (setq scrolled-lines 0)
2168 ;;;!! (incremental-scroll (- scroll-speed)))
2170 ;;;!! (defun incremental-scroll (n)
2171 ;;;!! (while (= (x-mouse-events) 0)
2172 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
2173 ;;;!! (scroll-down n)
2174 ;;;!! (sit-for 300 t)))
2176 ;;;!! (defun incr-scroll-stop (event)
2177 ;;;!! (interactive "@e")
2178 ;;;!! (message "Scrolled %d lines" scrolled-lines)
2179 ;;;!! (setq scrolled-lines 0)
2180 ;;;!! (sleep-for 1))
2182 ;;;!! ;;; (defun x-testing-scroll ()
2183 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
2184 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
2185 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
2186 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
2187 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
2190 ;;;!! ;; Some playthings suitable for picture mode? They need work.
2193 ;;;!! (defun mouse-kill-rectangle (event)
2194 ;;;!! "Kill the rectangle between point and the mouse cursor."
2195 ;;;!! (interactive "@e")
2196 ;;;!! (let ((point-save (point)))
2197 ;;;!! (save-excursion
2198 ;;;!! (mouse-set-point event)
2199 ;;;!! (push-mark nil t)
2200 ;;;!! (if (> point-save (point))
2201 ;;;!! (kill-rectangle (point) point-save)
2202 ;;;!! (kill-rectangle point-save (point))))))
2204 ;;;!! (defun mouse-open-rectangle (event)
2205 ;;;!! "Kill the rectangle between point and the mouse cursor."
2206 ;;;!! (interactive "@e")
2207 ;;;!! (let ((point-save (point)))
2208 ;;;!! (save-excursion
2209 ;;;!! (mouse-set-point event)
2210 ;;;!! (push-mark nil t)
2211 ;;;!! (if (> point-save (point))
2212 ;;;!! (open-rectangle (point) point-save)
2213 ;;;!! (open-rectangle point-save (point))))))
2215 ;;;!! ;; Must be a better way to do this.
2217 ;;;!! (defun mouse-multiple-insert (n char)
2218 ;;;!! (while (> n 0)
2220 ;;;!! (setq n (1- n))))
2222 ;;;!! ;; What this could do is not finalize until button was released.
2224 ;;;!! (defun mouse-move-text (event)
2225 ;;;!! "Move text from point to cursor position, inserting spaces."
2226 ;;;!! (interactive "@e")
2227 ;;;!! (let* ((relative-coordinate
2228 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
2229 ;;;!! (if (consp relative-coordinate)
2230 ;;;!! (cond ((> (current-column) (car relative-coordinate))
2232 ;;;!! (- (car relative-coordinate) (current-column))))
2233 ;;;!! ((< (current-column) (car relative-coordinate))
2234 ;;;!! (mouse-multiple-insert
2235 ;;;!! (- (car relative-coordinate) (current-column)) " "))
2236 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
2238 ;; Choose a completion with the mouse.
2240 (defun mouse-choose-completion (event)
2241 "Click on an alternative in the `*Completions*' buffer to choose it."
2243 ;; Give temporary modes such as isearch a chance to turn off.
2244 (run-hooks 'mouse-leave-buffer-hook)
2245 (let ((buffer (window-buffer))
2249 (set-buffer (window-buffer (posn-window (event-start event))))
2250 (if completion-reference-buffer
2251 (setq buffer completion-reference-buffer))
2252 (setq base-size completion-base-size)
2254 (goto-char (posn-point (event-start event)))
2256 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2257 (setq end (point) beg (1+ (point))))
2259 (error "No completion here"))
2260 (setq beg (previous-single-property-change beg 'mouse-face))
2261 (setq end (or (next-single-property-change end 'mouse-face)
2263 (setq choice (buffer-substring beg end)))))
2264 (let ((owindow (selected-window)))
2265 (select-window (posn-window (event-start event)))
2266 (if (and (one-window-p t 'selected-frame)
2267 (window-dedicated-p (selected-window)))
2268 ;; This is a special buffer's frame
2269 (iconify-frame (selected-frame))
2270 (or (window-dedicated-p (selected-window))
2272 (select-window owindow))
2273 (choose-completion-string choice buffer base-size)))
2277 (defun font-menu-add-default ()
2278 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2279 (font-alist x-fixed-font-alist)
2280 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2281 (if (assoc "Default" elt)
2282 (delete (assoc "Default" elt) elt))
2284 (cons (list "Default" default)
2287 (defvar x-fixed-font-alist
2290 ;; For these, we specify the pixel height and width.
2292 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2294 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2296 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2297 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2298 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2299 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2300 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2301 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2302 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2303 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2306 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2308 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2310 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2312 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2314 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2316 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2318 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2319 ;;; We don't seem to have these; who knows what they are.
2320 ;;; ("fg-18" "fg-18")
2321 ;;; ("fg-25" "fg-25")
2322 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2323 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2324 ("lucidasanstypewriter-bold-24"
2325 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2326 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2327 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2330 ;; For these, we specify the point height.
2331 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2332 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2333 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2334 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2335 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2336 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2337 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2338 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2339 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2340 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2341 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2342 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2343 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2344 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2345 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2346 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2347 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2348 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2349 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2350 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2351 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2352 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2353 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2354 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2356 "X fonts suitable for use in Emacs.")
2358 (defun mouse-set-font (&rest fonts)
2359 "Select an emacs font from a list of known good fonts and fontsets."
2361 (progn (unless (display-multi-font-p)
2362 (error "Cannot change fonts on this display"))
2364 (if (listp last-nonmenu-event)
2366 (list '(0 0) (selected-window)))
2367 ;; Append list of fontsets currently defined.
2368 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2374 (set-default-font (car fonts))
2375 (setq font (car fonts))
2378 (setq fonts (cdr fonts)))))
2380 (error "Font not found")))))
2382 ;;; Bindings for mouse commands.
2384 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2385 (global-set-key [mouse-1] 'mouse-set-point)
2386 (global-set-key [drag-mouse-1] 'mouse-set-region)
2388 ;; These are tested for in mouse-drag-region.
2389 (global-set-key [double-mouse-1] 'mouse-set-point)
2390 (global-set-key [triple-mouse-1] 'mouse-set-point)
2392 ;; Clicking on the fringes causes hscrolling:
2393 (global-set-key [left-fringe mouse-1] 'mouse-set-point)
2394 (global-set-key [right-fringe mouse-1] 'mouse-set-point)
2396 (global-set-key [mouse-2] 'mouse-yank-at-click)
2397 (global-set-key [mouse-3] 'mouse-save-then-kill)
2399 ;; By binding these to down-going events, we let the user use the up-going
2400 ;; event to make the selection, saving a click.
2401 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2402 (if (not (eq system-type 'ms-dos))
2403 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2404 ;; C-down-mouse-2 is bound in facemenu.el.
2405 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2408 ;; Replaced with dragging mouse-1
2409 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2411 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2412 ;; vertical-line prevents Emacs from signaling an error when the mouse
2413 ;; button is released after dragging these lines, on non-toolkit
2415 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2416 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2417 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2418 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2419 (global-set-key [header-line mouse-1] 'mouse-select-window)
2420 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2421 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2422 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2423 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2424 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2425 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2426 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2430 ;; This file contains the functionality of the old mldrag.el.
2431 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2432 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2433 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2434 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2437 ;;; arch-tag: 9a710ce1-914a-4923-9b81-697f7bf82ab3
2438 ;;; mouse.el ends here