1 ;;; mouse.el --- window system-independent mouse support
3 ;; Copyright (C) 1993, 94, 95, 1999, 2000, 2001, 2002, 2003, 2004
4 ;; 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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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 350
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 behaviour.
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))
86 ;; Provide a mode-specific menu on a mouse button.
88 (defun popup-menu (menu &optional position prefix)
89 "Popup the given menu and call the selected option.
90 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
92 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
93 the current mouse position.
94 PREFIX is the prefix argument (if any) to pass to the command."
97 ((and (listp menu) (keymapp (car menu))) menu)
98 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
99 (filter (when (symbolp map)
100 (plist-get (get map 'menu-prop) :filter))))
101 (if filter (funcall filter (symbol-function map)) map)))))
104 (let ((mp (mouse-pixel-position)))
105 (setq position (list (list (cadr mp) (cddr mp)) (car mp)))))
106 ;; The looping behavior was taken from lmenu's popup-menu-popup
107 (while (and map (setq event
108 ;; map could be a prefix key, in which case
109 ;; we need to get its function cell
111 (x-popup-menu position (indirect-function map))))
112 ;; Strangely x-popup-menu returns a list.
113 ;; mouse-major-mode-menu was using a weird:
114 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
116 (if (and (not (keymapp map)) (listp map))
117 ;; We were given a list of keymaps. Search them all
118 ;; in sequence until a first binding is found.
119 (let ((mouse-click (apply 'vector event))
121 (while (and map (null binding))
122 (setq binding (lookup-key (car map) mouse-click))
123 (if (numberp binding) ; `too long'
125 (setq map (cdr map)))
127 ;; We were given a single keymap.
128 (lookup-key map (apply 'vector event))))
129 ;; Clear out echoing, which perhaps shows a prefix arg.
131 ;; Maybe try again but with the submap.
132 (setq map (if (keymapp cmd) cmd)))
133 ;; If the user did not cancel by refusing to select,
134 ;; and if the result is a command, run it.
135 (when (and (null map) (commandp cmd))
136 (setq prefix-arg prefix)
137 ;; `setup-specified-language-environment', for instance,
138 ;; expects this to be set from a menu keymap.
139 (setq last-command-event (car (last event)))
140 ;; mouse-major-mode-menu was using `command-execute' instead.
141 (call-interactively cmd))))
143 (defvar mouse-major-mode-menu-prefix) ; dynamically bound
145 (defun mouse-major-mode-menu (event prefix)
146 "Pop up a mode-specific menu of mouse commands.
147 Default to the Edit menu if the major mode doesn't define a menu."
148 ;; Switch to the window clicked on, because otherwise
149 ;; the mode's commands may not make sense.
150 (interactive "@e\nP")
151 ;; Let the mode update its menus first.
152 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
153 (let* (;; This is where mouse-major-mode-menu-prefix
154 ;; returns the prefix we should use (after menu-bar).
155 ;; It is either nil or (SOME-SYMBOL).
156 (mouse-major-mode-menu-prefix nil)
157 ;; Keymap from which to inherit; may be null.
158 (ancestor (mouse-major-mode-menu-1
159 (and (current-local-map)
160 (local-key-binding [menu-bar]))))
161 ;; Make a keymap in which our last command leads to a menu or
162 ;; default to the edit menu.
164 (make-sparse-keymap (concat mode-name " Mode"))
168 ;; Make our menu inherit from the desired keymap which we want
169 ;; to display as the menu now.
170 (set-keymap-parent newmap ancestor))
171 (popup-menu newmap event prefix)))
174 ;; Compute and cache the equivalent keys in MENU and all its submenus.
175 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
176 ;;; (and (eq (car menu) 'keymap)
177 ;;; (x-popup-menu nil menu))
179 ;;; (and (consp (car menu))
180 ;;; (consp (cdr (car menu)))
181 ;;; (let ((tail (cdr (car menu))))
182 ;;; (while (and (consp tail)
183 ;;; (not (eq (car tail) 'keymap)))
184 ;;; (setq tail (cdr tail)))
186 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
187 ;;; (setq menu (cdr menu))))
189 ;; Given a mode's menu bar keymap,
190 ;; if it defines exactly one menu bar menu,
191 ;; return just that menu.
192 ;; Otherwise return a menu for all of them.
193 (defun mouse-major-mode-menu-1 (menubar)
198 (if (consp (car tail))
201 (setq submap (car tail))))
202 (setq tail (cdr tail)))
205 (setq mouse-major-mode-menu-prefix (list (car submap)))
206 (lookup-key menubar (vector (car submap)))))))
208 (defun mouse-popup-menubar (event prefix)
209 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
210 The contents are the items that would be in the menu bar whether or
211 not it is actually displayed."
212 (interactive "@e \nP")
213 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
214 (let* ((local-menu (and (current-local-map)
215 (lookup-key (current-local-map) [menu-bar])))
216 (global-menu (lookup-key global-map [menu-bar]))
217 ;; If a keymap doesn't have a prompt string (a lazy
218 ;; programmer didn't bother to provide one), create it and
219 ;; insert it into the keymap; each keymap gets its own
220 ;; prompt. This is required for non-toolkit versions to
221 ;; display non-empty menu pane names.
226 (let* ((minor-mode (car menu))
228 (title-or-map (cadr menu)))
229 (or (stringp title-or-map)
233 (capitalize (subst-char-in-string
239 (minor-mode-key-binding [menu-bar])))
240 (local-title-or-map (and local-menu (cadr local-menu)))
241 (global-title-or-map (cadr global-menu)))
242 (or (null local-menu)
243 (stringp local-title-or-map)
244 (setq local-menu (cons 'keymap
245 (cons (concat mode-name " Mode Menu")
247 (or (stringp global-title-or-map)
248 (setq global-menu (cons 'keymap
250 (cdr global-menu)))))
251 ;; Supplying the list is faster than making a new map.
252 (popup-menu (append (list global-menu)
258 (defun mouse-popup-menubar-stuff (event prefix)
259 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
260 Use the former if the menu bar is showing, otherwise the latter."
261 (interactive "@e \nP")
262 (if (zerop (assoc-default 'menu-bar-lines (frame-parameters) 'eq 0))
263 (mouse-popup-menubar event prefix)
264 (mouse-major-mode-menu event prefix)))
266 ;; Commands that operate on windows.
268 (defun mouse-minibuffer-check (event)
269 (let ((w (posn-window (event-start event))))
270 (and (window-minibuffer-p w)
271 (not (minibuffer-window-active-p w))
272 (error "Minibuffer window is not active")))
273 ;; Give temporary modes such as isearch a chance to turn off.
274 (run-hooks 'mouse-leave-buffer-hook))
276 (defun mouse-delete-window (click)
277 "Delete the window you click on.
278 Do nothing if the frame has just one window.
279 This command must be bound to a mouse click."
281 (unless (one-window-p t)
282 (mouse-minibuffer-check click)
283 (delete-window (posn-window (event-start click)))))
285 (defun mouse-select-window (click)
286 "Select the window clicked on; don't move point."
288 (mouse-minibuffer-check click)
289 (let ((oframe (selected-frame))
290 (frame (window-frame (posn-window (event-start click)))))
291 (select-window (posn-window (event-start click)))
294 (or (eq frame oframe)
295 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
297 (defun mouse-tear-off-window (click)
298 "Delete the window clicked on, and create a new frame displaying its buffer."
300 (mouse-minibuffer-check click)
301 (let* ((window (posn-window (event-start click)))
302 (buf (window-buffer window))
303 (frame (make-frame)))
305 (switch-to-buffer buf)
306 (delete-window window)))
308 (defun mouse-delete-other-windows ()
309 "Delete all windows except the one you click on."
311 (delete-other-windows))
313 (defun mouse-split-window-vertically (click)
314 "Select Emacs window mouse is on, then split it vertically in half.
315 The window is split at the line clicked on.
316 This command must be bound to a mouse click."
318 (mouse-minibuffer-check click)
319 (let ((start (event-start click)))
320 (select-window (posn-window start))
321 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
322 (first-line window-min-height)
323 (last-line (- (window-height) window-min-height)))
324 (if (< last-line first-line)
325 (error "Window too short to split")
326 (split-window-vertically
327 (min (max new-height first-line) last-line))))))
329 (defun mouse-split-window-horizontally (click)
330 "Select Emacs window mouse is on, then split it horizontally in half.
331 The window is split at the column clicked on.
332 This command must be bound to a mouse click."
334 (mouse-minibuffer-check click)
335 (let ((start (event-start click)))
336 (select-window (posn-window start))
337 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
338 (first-col window-min-width)
339 (last-col (- (window-width) window-min-width)))
340 (if (< last-col first-col)
341 (error "Window too narrow to split")
342 (split-window-horizontally
343 (min (max new-width first-col) last-col))))))
345 (defun mouse-drag-window-above (window)
346 "Return the (or a) window directly above WINDOW.
347 That means one whose bottom edge is at the same height as WINDOW's top edge."
348 (let ((top (nth 1 (window-edges window)))
349 (start-window window)
351 (setq window (previous-window window 0))
352 (while (and (not above-window) (not (eq window start-window)))
353 (if (= (+ (window-height window) (nth 1 (window-edges window)))
355 (setq above-window window))
356 (setq window (previous-window window)))
359 (defun mouse-drag-move-window-bottom (window growth)
360 "Move the bottom of WINDOW up or down by GROWTH lines.
361 Move it down if GROWTH is positive, or up if GROWTH is negative.
362 If this would make WINDOW too short,
363 shrink the window or windows above it to make room."
364 (let ((excess (- window-min-height (+ (window-height window) growth))))
365 ;; EXCESS is the number of lines we need to take from windows above.
367 ;; This can recursively shrink windows all the way up.
368 (let ((window-above (mouse-drag-window-above window)))
370 (mouse-drag-move-window-bottom window-above (- excess))))))
371 (save-selected-window
372 (select-window window)
373 (enlarge-window growth nil (> growth 0))))
375 (defsubst mouse-drag-move-window-top (window growth)
376 "Move the top of WINDOW up or down by GROWTH lines.
377 Move it down if GROWTH is positive, or up if GROWTH is negative.
378 If this would make WINDOW too short, shrink the window or windows
379 above it to make room."
380 ;; Moving the top of WINDOW is actually moving the bottom of the
382 (let ((window-above (mouse-drag-window-above window)))
384 (mouse-drag-move-window-bottom window-above (- growth)))))
386 (defun mouse-drag-mode-line-1 (start-event mode-line-p)
387 "Change the height of a window by dragging on the mode or header line.
388 START-EVENT is the starting mouse-event of the drag action.
389 MODE-LINE-P non-nil means dragging a mode line; nil means a header line."
390 ;; Give temporary modes such as isearch a chance to turn off.
391 (run-hooks 'mouse-leave-buffer-hook)
394 (start (event-start start-event))
395 (start-event-window (posn-window start))
396 (start-event-frame (window-frame start-event-window))
397 (start-nwindows (count-windows t))
398 (old-selected-window (selected-window))
399 (minibuffer (frame-parameter nil 'minibuffer))
400 (mouse-autoselect-window nil)
401 should-enlarge-minibuffer event mouse y top bot edges wconfig growth)
404 ;; enlarge-window only works on the selected window, so
405 ;; we must select the window where the start event originated.
406 ;; unwind-protect will restore the old selected window later.
407 (select-window start-event-window)
409 ;; if this is the bottommost ordinary window, then to
410 ;; move its modeline the minibuffer must be enlarged.
411 (setq should-enlarge-minibuffer
414 (not (one-window-p t))
415 (= (nth 1 (window-edges minibuffer))
416 (nth 3 (window-edges)))))
418 ;; loop reading events and sampling the position of
421 (setq event (read-event)
422 mouse (mouse-position))
425 ;; - there is a switch-frame event.
426 ;; - the mouse isn't in the frame that we started in
427 ;; - the mouse isn't in any Emacs frame
429 ;; - there is a mouse-movement event
430 ;; - there is a scroll-bar-movement event
431 ;; (same as mouse movement for our purposes)
433 ;; - there is a keyboard event or some other unknown event
435 (cond ((integerp event)
438 ((eq (car event) 'switch-frame)
441 ((not (memq (car event) '(mouse-movement scroll-bar-movement)))
443 (push event unread-command-events))
446 ((not (eq (car mouse) start-event-frame))
449 ((null (car (cdr mouse)))
453 (setq y (cdr (cdr mouse))
458 ;; compute size change needed
460 (setq growth (- y bot -1)))
462 (when (< (- bot y) window-min-height)
463 (setq y (- bot window-min-height)))
464 ;; The window's top includes the header line!
465 (setq growth (- top y))))
466 (setq wconfig (current-window-configuration))
468 ;; Check for an error case.
469 (when (and (/= growth 0)
472 (error "Attempt to resize sole window"))
474 ;; grow/shrink minibuffer?
475 (if should-enlarge-minibuffer
477 ;; yes. briefly select minibuffer so
478 ;; enlarge-window will affect the
480 (select-window minibuffer)
481 ;; scale back shrinkage if it would
482 ;; make the minibuffer less than 1
484 (if (and (> growth 0)
485 (< (- (window-height minibuffer)
488 (setq growth (1- (window-height minibuffer))))
489 (enlarge-window (- growth))
490 (select-window start-event-window))
491 ;; no. grow/shrink the selected window
492 ;(message "growth = %d" growth)
494 (mouse-drag-move-window-bottom start-event-window growth)
495 (mouse-drag-move-window-top start-event-window growth)))
497 ;; if this window's growth caused another
498 ;; window to be deleted because it was too
499 ;; short, rescind the change.
501 ;; if size change caused space to be stolen
502 ;; from a window above this one, rescind the
503 ;; change, but only if we didn't grow/shrink
504 ;; the minibuffer. minibuffer size changes
505 ;; can cause all windows to shrink... no way
507 (when (or (/= start-nwindows (count-windows t))
508 (and (not should-enlarge-minibuffer)
511 (/= top (nth 1 (window-edges)))))
512 (set-window-configuration wconfig)))))))))
514 (defun mouse-drag-mode-line (start-event)
515 "Change the height of a window by dragging on the mode line."
517 (mouse-drag-mode-line-1 start-event t))
519 (defun mouse-drag-header-line (start-event)
520 "Change the height of a window by dragging on the header line.
521 Windows whose header-lines are at the top of the frame cannot be
522 resized by dragging their header-line."
524 ;; Changing the window's size by dragging its header-line when the
525 ;; header-line is at the top of the frame is somewhat strange,
526 ;; because the header-line doesn't move, so don't do it.
527 (let* ((start (event-start start-event))
528 (window (posn-window start))
529 (frame (window-frame window))
530 (first-window (frame-first-window frame)))
531 (when (or (eq window first-window)
532 (= (nth 1 (window-edges window))
533 (nth 1 (window-edges first-window))))
534 (error "Cannot move header-line at the top of the frame"))
535 (mouse-drag-mode-line-1 start-event nil)))
538 (defun mouse-drag-vertical-line (start-event)
539 "Change the width of a window by dragging on the vertical line."
541 ;; Give temporary modes such as isearch a chance to turn off.
542 (run-hooks 'mouse-leave-buffer-hook)
545 (start-event-frame (window-frame (car (car (cdr start-event)))))
546 (start-event-window (car (car (cdr start-event))))
547 (start-nwindows (count-windows t))
548 (old-selected-window (selected-window))
549 event mouse x left right edges wconfig growth
551 (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
554 (error "Attempt to resize sole ordinary window"))
555 (if (eq which-side 'right)
556 (if (= (nth 2 (window-edges start-event-window))
557 (frame-width start-event-frame))
558 (error "Attempt to drag rightmost scrollbar"))
559 (if (= (nth 0 (window-edges start-event-window)) 0)
560 (error "Attempt to drag leftmost scrollbar")))
563 ;; enlarge-window only works on the selected window, so
564 ;; we must select the window where the start event originated.
565 ;; unwind-protect will restore the old selected window later.
566 (select-window start-event-window)
567 ;; loop reading events and sampling the position of
570 (setq event (read-event)
571 mouse (mouse-position))
573 ;; - there is a switch-frame event.
574 ;; - the mouse isn't in the frame that we started in
575 ;; - the mouse isn't in any Emacs frame
577 ;; - there is a mouse-movement event
578 ;; - there is a scroll-bar-movement event
579 ;; (same as mouse movement for our purposes)
581 ;; - there is a keyboard event or some other unknown event
583 (cond ((integerp event)
585 ((eq (car event) 'switch-frame)
587 ((not (memq (car event)
588 '(mouse-movement scroll-bar-movement)))
590 (setq unread-command-events
591 (cons event unread-command-events)))
593 ((not (eq (car mouse) start-event-frame))
595 ((null (car (cdr mouse)))
598 (save-selected-window
599 ;; If the scroll bar is on the window's left,
600 ;; adjust the window on the left.
601 (unless (eq which-side 'right)
602 (select-window (previous-window)))
603 (setq x (- (car (cdr mouse))
604 (if (eq which-side 'right) 0 2))
608 ;; scale back a move that would make the
610 (if (< (- x left -1) window-min-width)
611 (setq x (+ left window-min-width -1)))
612 ;; compute size change needed
613 (setq growth (- x right -1)
614 wconfig (current-window-configuration))
615 (enlarge-window growth t)
616 ;; if this window's growth caused another
617 ;; window to be deleted because it was too
618 ;; thin, rescind the change.
620 ;; if size change caused space to be stolen
621 ;; from a window to the left of this one,
622 ;; rescind the change.
623 (if (or (/= start-nwindows (count-windows t))
624 (/= left (nth 0 (window-edges))))
625 (set-window-configuration wconfig))))))))))
627 (defun mouse-set-point (event)
628 "Move point to the position clicked on with the mouse.
629 This should be bound to a mouse click event type."
631 (mouse-minibuffer-check event)
632 ;; Use event-end in case called from mouse-drag-region.
633 ;; If EVENT is a click, event-end and event-start give same value.
634 (posn-set-point (event-end event)))
636 (defvar mouse-last-region-beg nil)
637 (defvar mouse-last-region-end nil)
638 (defvar mouse-last-region-tick nil)
640 (defun mouse-region-match ()
641 "Return non-nil if there's an active region that was set with the mouse."
642 (and (mark t) mark-active
643 (eq mouse-last-region-beg (region-beginning))
644 (eq mouse-last-region-end (region-end))
645 (eq mouse-last-region-tick (buffer-modified-tick))))
647 (defun mouse-set-region (click)
648 "Set the region to the text dragged over, and copy to kill ring.
649 This should be bound to a mouse drag event."
651 (mouse-minibuffer-check click)
652 (let ((posn (event-start click))
653 (end (event-end click)))
654 (select-window (posn-window posn))
655 (if (numberp (posn-point posn))
656 (goto-char (posn-point posn)))
657 ;; If mark is highlighted, no need to bounce the cursor.
658 ;; On X, we highlight while dragging, thus once again no need to bounce.
659 (or transient-mark-mode
660 (memq (framep (selected-frame)) '(x pc w32))
664 (if (numberp (posn-point end))
665 (goto-char (posn-point end)))
666 ;; Don't set this-command to kill-region, so that a following
667 ;; C-w will not double the text in the kill ring.
668 ;; Ignore last-command so we don't append to a preceding kill.
669 (when mouse-drag-copy-region
670 (let (this-command last-command deactivate-mark)
671 (copy-region-as-kill (mark) (point))))
672 (mouse-set-region-1)))
674 (defun mouse-set-region-1 ()
675 ;; Set transient-mark-mode for a little while.
676 (if (memq transient-mark-mode '(nil identity))
677 (setq transient-mark-mode 'only))
678 (setq mouse-last-region-beg (region-beginning))
679 (setq mouse-last-region-end (region-end))
680 (setq mouse-last-region-tick (buffer-modified-tick)))
682 (defcustom mouse-scroll-delay 0.25
683 "*The pause between scroll steps caused by mouse drags, in seconds.
684 If you drag the mouse beyond the edge of a window, Emacs scrolls the
685 window to bring the text beyond that edge into view, with a delay of
686 this many seconds between scroll steps. Scrolling stops when you move
687 the mouse back into the window, or release the button.
688 This variable's value may be non-integral.
689 Setting this to zero causes Emacs to scroll as fast as it can."
693 (defcustom mouse-scroll-min-lines 1
694 "*The minimum number of lines scrolled by dragging mouse out of window.
695 Moving the mouse out the top or bottom edge of the window begins
696 scrolling repeatedly. The number of lines scrolled per repetition
697 is normally equal to the number of lines beyond the window edge that
698 the mouse has moved. However, it always scrolls at least the number
699 of lines specified by this variable."
703 (defun mouse-scroll-subr (window jump &optional overlay start)
704 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
705 If OVERLAY is an overlay, let it stretch from START to the far edge of
706 the newly visible text.
707 Upon exit, point is at the far edge of the newly visible text."
709 ((and (> jump 0) (< jump mouse-scroll-min-lines))
710 (setq jump mouse-scroll-min-lines))
711 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
712 (setq jump (- mouse-scroll-min-lines))))
713 (let ((opoint (point)))
715 (goto-char (window-start window))
716 (if (not (zerop (vertical-motion jump window)))
718 (set-window-start window (point))
720 (if (window-end window)
722 (goto-char (window-end window))
723 ;; window-end doesn't reflect the window's new
724 ;; start position until the next redisplay.
725 (vertical-motion (1- jump) window))
726 (vertical-motion (- (window-height window) 2)))
727 (goto-char (window-start window)))
729 (move-overlay overlay start (point)))
730 ;; Now that we have scrolled WINDOW properly,
731 ;; put point back where it was for the redisplay
732 ;; so that we don't mess up the selected window.
733 (or (eq window (selected-window))
735 (sit-for mouse-scroll-delay)))))
736 (or (eq window (selected-window))
737 (goto-char opoint))))
739 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
740 (defvar mouse-drag-overlay (make-overlay 1 1))
741 (delete-overlay mouse-drag-overlay)
742 (overlay-put mouse-drag-overlay 'face 'region)
744 (defvar mouse-selection-click-count 0)
746 (defvar mouse-selection-click-count-buffer nil)
748 (defun mouse-drag-region (start-event)
749 "Set the region to the text that the mouse is dragged over.
750 Highlight the drag area as you move the mouse.
751 This must be bound to a button-down mouse event.
752 In Transient Mark mode, the highlighting remains as long as the mark
753 remains active. Otherwise, it remains until the next input event.
755 If the click is in the echo area, display the `*Messages*' buffer."
757 (let ((w (posn-window (event-start start-event))))
758 (if (not (or (not (window-minibuffer-p w))
759 (minibuffer-window-active-p w)))
762 (set-buffer "*Messages*")
763 (goto-char (point-max))
764 (display-buffer (current-buffer)))
765 ;; Give temporary modes such as isearch a chance to turn off.
766 (run-hooks 'mouse-leave-buffer-hook)
767 (mouse-drag-region-1 start-event))))
770 (defun mouse-on-link-p (pos)
771 "Return non-nil if POS is on a link in the current buffer.
773 A clickable link is identified by one of the following methods:
775 1) If the character at POS has a non-nil `follow-link' text or
776 overlay property, the value of that property is returned.
778 2) If there is a local key-binding or a keybinding at position
779 POS for the `follow-link' event, the binding of that event
780 determines whether POS is inside a link:
782 - If the binding is `mouse-face', POS is inside a link if there
783 is a non-nil `mouse-face' property at POS. Return t in this case.
785 - If the binding is a function, FUNC, POS is inside a link if
786 the call \(FUNC POS) returns non-nil. Return the return value
789 - Otherwise, return the binding of the `follow-link' binding.
791 The return value is interpreted as follows:
793 - If it is a string, the mouse-1 event is translated into the
794 first character of the string, i.e. the action of the mouse-1
795 click is the local or global binding of that character.
797 - If it is a vector, the mouse-1 event is translated into the
798 first element of that vector, i.e. the action of the mouse-1
799 click is the local or global binding of that event.
801 - Otherwise, the mouse-1 event is translated into a mouse-2 event
802 at the same position."
803 (or (get-char-property pos 'follow-link)
806 (let ((b (key-binding [follow-link] nil t)))
809 (and (get-char-property pos 'mouse-face) t))
814 (defun mouse-drag-region-1 (start-event)
815 (mouse-minibuffer-check start-event)
816 (let* ((echo-keystrokes 0)
817 (start-posn (event-start start-event))
818 (start-point (posn-point start-posn))
819 (start-window (posn-window start-posn))
820 (start-window-start (window-start start-window))
821 (start-frame (window-frame start-window))
822 (start-hscroll (window-hscroll start-window))
823 (bounds (window-edges start-window))
824 (make-cursor-line-fully-visible nil)
826 (bottom (if (window-minibuffer-p start-window)
828 ;; Don't count the mode line.
829 (1- (nth 3 bounds))))
830 on-link remap-double-click
831 (click-count (1- (event-click-count start-event))))
832 (setq mouse-selection-click-count click-count)
833 (setq mouse-selection-click-count-buffer (current-buffer))
834 (mouse-set-point start-event)
835 ;; In case the down click is in the middle of some intangible text,
836 ;; use the end of that text, and put it in START-POINT.
837 (if (< (point) start-point)
838 (goto-char start-point))
839 (setq start-point (point))
840 (setq on-link (and mouse-1-click-follows-link
841 (mouse-on-link-p start-point)))
842 (setq remap-double-click (and on-link
843 (eq mouse-1-click-follows-link 'double)
845 (if remap-double-click ;; Don't expand mouse overlay in links
846 (setq click-count 0))
847 (let ((range (mouse-start-end start-point start-point click-count)))
848 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
849 (window-buffer start-window))
850 (overlay-put mouse-drag-overlay 'window (selected-window)))
852 ;; end-of-range is used only in the single-click case.
853 ;; It is the place where the drag has reached so far
854 ;; (but not outside the window where the drag started).
855 (let (event end end-point last-end-point (end-of-range (point)))
858 (setq event (read-event))
859 (or (mouse-movement-p event)
860 (eq (car-safe event) 'switch-frame)))
861 (if (eq (car-safe event) 'switch-frame)
863 (setq end (event-end event)
864 end-point (posn-point end))
865 (if (numberp end-point)
866 (setq last-end-point end-point))
869 ;; Are we moving within the original window?
870 ((and (eq (posn-window end) start-window)
871 (integer-or-marker-p end-point))
872 ;; Go to START-POINT first, so that when we move to END-POINT,
873 ;; if it's in the middle of intangible text,
874 ;; point jumps in the direction away from START-POINT.
875 (goto-char start-point)
876 (goto-char end-point)
877 (if (zerop (% click-count 3))
878 (setq end-of-range (point)))
879 (let ((range (mouse-start-end start-point (point) click-count)))
880 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
883 (let ((mouse-row (cdr (cdr (mouse-position)))))
887 (mouse-scroll-subr start-window (- mouse-row top)
888 mouse-drag-overlay start-point)
889 ;; Without this, point tends to jump back to the starting
890 ;; position where the mouse button was pressed down.
891 (setq end-of-range (overlay-start mouse-drag-overlay)))
892 ((>= mouse-row bottom)
893 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
894 mouse-drag-overlay start-point)
895 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
897 ;; In case we did not get a mouse-motion event
898 ;; for the final move of the mouse before a drag event
899 ;; pretend that we did get one.
900 (when (and (memq 'drag (event-modifiers (car-safe event)))
901 (setq end (event-end event)
902 end-point (posn-point end))
903 (eq (posn-window end) start-window)
904 (integer-or-marker-p end-point))
905 ;; Go to START-POINT first, so that when we move to END-POINT,
906 ;; if it's in the middle of intangible text,
907 ;; point jumps in the direction away from START-POINT.
908 (goto-char start-point)
909 (goto-char end-point)
910 (if (zerop (% click-count 3))
911 (setq end-of-range (point)))
912 (let ((range (mouse-start-end start-point (point) click-count)))
913 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
916 (let ((fun (key-binding (vector (car event)))))
917 ;; Run the binding of the terminating up-event, if possible.
918 ;; In the case of a multiple click, it gives the wrong results,
919 ;; because it would fail to set up a region.
920 (if (not (= (overlay-start mouse-drag-overlay)
921 (overlay-end mouse-drag-overlay)))
923 (if (numberp (posn-point (event-end event)))
924 (posn-point (event-end event))
926 ;; The end that comes from where we ended the drag.
929 (if (and stop-point (< stop-point start-point))
930 (overlay-start mouse-drag-overlay)
931 (overlay-end mouse-drag-overlay)))
932 ;; The end that comes from where we started the drag.
935 (- (+ (overlay-end mouse-drag-overlay)
936 (overlay-start mouse-drag-overlay))
938 last-command this-command)
939 (push-mark region-commencement t t)
940 (goto-char region-termination)
941 ;; Don't let copy-region-as-kill set deactivate-mark.
942 (when mouse-drag-copy-region
943 (let (deactivate-mark)
944 (copy-region-as-kill (point) (mark t))))
945 (let ((buffer (current-buffer)))
947 ;; mouse-show-mark can call read-event,
948 ;; and that means the Emacs server could switch buffers
949 ;; under us. If that happened,
950 ;; avoid trying to use the region.
951 (and (mark t) mark-active
952 (eq buffer (current-buffer))
953 (mouse-set-region-1))))
954 (delete-overlay mouse-drag-overlay)
955 ;; Run the binding of the terminating up-event.
956 (when (and (functionp fun)
957 (= start-hscroll (window-hscroll start-window))
958 ;; Don't run the up-event handler if the
959 ;; window start changed in a redisplay after
960 ;; the mouse-set-point for the down-mouse
961 ;; event at the beginning of this function.
962 ;; When the window start has changed, the
963 ;; up-mouse event will contain a different
964 ;; position due to the new window contents,
965 ;; and point is set again.
967 (= (window-start start-window)
968 start-window-start)))
972 (or remap-double-click
974 (not (eq mouse-1-click-follows-link 'double))
976 (= (event-click-count event) 1)
977 (not (input-pending-p))
978 (or (not (integerp mouse-1-click-follows-link))
979 (let ((t0 (posn-timestamp (event-start start-event)))
980 (t1 (posn-timestamp (event-end event))))
981 (and (integerp t0) (integerp t1)
982 (if (> mouse-1-click-follows-link 0)
983 (<= (- t1 t0) mouse-1-click-follows-link)
984 (< (- t0 t1) mouse-1-click-follows-link)))))
985 (or (not double-click-time)
986 (sit-for 0 (if (integerp double-click-time)
987 double-click-time 500) t)))))
988 (if (or (vectorp on-link) (stringp on-link))
989 (setq event (aref on-link 0))
990 (setcar event 'mouse-2)))
991 (setq unread-command-events
992 (cons event unread-command-events)))))
993 (delete-overlay mouse-drag-overlay)))))
995 ;; Commands to handle xterm-style multiple clicks.
997 (defun mouse-skip-word (dir)
998 "Skip over word, over whitespace, or over identical punctuation.
999 If DIR is positive skip forward; if negative, skip backward."
1000 (let* ((char (following-char))
1001 (syntax (char-to-string (char-syntax char))))
1002 (cond ((string= syntax "w")
1003 ;; Here, we can't use skip-syntax-forward/backward because
1004 ;; they don't pay attention to word-separating-categories,
1005 ;; and thus they will skip over a true word boundary. So,
1006 ;; we simularte the original behaviour by using
1009 (if (not (looking-at "\\<"))
1011 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1013 ((string= syntax " ")
1015 (skip-syntax-backward syntax)
1016 (skip-syntax-forward syntax)))
1017 ((string= syntax "_")
1019 (skip-syntax-backward "w_")
1020 (skip-syntax-forward "w_")))
1022 (while (and (not (bobp)) (= (preceding-char) char))
1025 (while (and (not (eobp)) (= (following-char) char))
1026 (forward-char 1))))))
1028 (defun mouse-start-end (start end mode)
1029 "Return a list of region bounds based on START and END according to MODE.
1030 If MODE is 0 then set point to (min START END), mark to (max START END).
1031 If MODE is 1 then set point to start of word at (min START END),
1032 mark to end of word at (max START END).
1033 If MODE is 2 then do the same for lines."
1038 (setq mode (mod mode 3))
1044 (= (char-syntax (char-after start)) ?\())
1053 (= (char-syntax (char-after start)) ?\)))
1054 (list (save-excursion
1055 (goto-char (1+ start))
1062 (= (char-syntax (char-after start)) ?\"))
1063 (let ((open (or (eq start (point-min))
1065 (goto-char (- start 1))
1066 (looking-at "\\s(\\|\\s \\|\\s>")))))
1076 (list (save-excursion
1079 (goto-char (1+ start))
1085 (list (save-excursion
1087 (mouse-skip-word -1)
1094 (list (save-excursion
1096 (beginning-of-line 1)
1103 ;; Subroutine: set the mark where CLICK happened,
1104 ;; but don't do anything else.
1105 (defun mouse-set-mark-fast (click)
1106 (mouse-minibuffer-check click)
1107 (let ((posn (event-start click)))
1108 (select-window (posn-window posn))
1109 (if (numberp (posn-point posn))
1110 (push-mark (posn-point posn) t t))))
1112 (defun mouse-undouble-last-event (events)
1113 (let* ((index (1- (length events)))
1114 (last (nthcdr index events))
1116 (basic (event-basic-type event))
1117 (old-modifiers (event-modifiers event))
1118 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1121 ;; Use reverse, not nreverse, since event-modifiers
1122 ;; does not copy the list it returns.
1123 (cons (event-convert-list (reverse (cons basic modifiers)))
1127 (if (and (not (equal modifiers old-modifiers))
1128 (key-binding (apply 'vector events)))
1133 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1135 (defvar mouse-region-delete-keys '([delete] [deletechar])
1136 "List of keys which shall cause the mouse region to be deleted.")
1138 (defun mouse-show-mark ()
1139 (let ((inhibit-quit t)
1141 event events key ignore
1142 (x-lost-selection-functions
1143 (when (boundp 'x-lost-selection-functions)
1144 (copy-sequence x-lost-selection-functions))))
1145 (add-hook 'x-lost-selection-functions
1147 (when (eq seltype 'PRIMARY)
1149 (throw 'mouse-show-mark t))))
1150 (if transient-mark-mode
1151 (delete-overlay mouse-drag-overlay)
1152 (move-overlay mouse-drag-overlay (point) (mark t)))
1153 (catch 'mouse-show-mark
1154 ;; In this loop, execute scroll bar and switch-frame events.
1155 ;; Also ignore down-events that are undefined.
1156 (while (progn (setq event (read-event))
1157 (setq events (append events (list event)))
1158 (setq key (apply 'vector events))
1159 (or (and (consp event)
1160 (eq (car event) 'switch-frame))
1162 (eq (posn-point (event-end event))
1163 'vertical-scroll-bar))
1164 (and (memq 'down (event-modifiers event))
1165 (not (key-binding key))
1166 (not (mouse-undouble-last-event events))
1167 (not (member key mouse-region-delete-keys)))))
1169 (or (eq (car event) 'switch-frame)
1170 (eq (posn-point (event-end event))
1171 'vertical-scroll-bar))
1172 (let ((keys (vector 'vertical-scroll-bar event)))
1173 (and (key-binding keys)
1175 (call-interactively (key-binding keys)
1177 (setq events nil)))))))
1178 ;; If we lost the selection, just turn off the highlighting.
1180 ;; For certain special keys, delete the region.
1181 (if (member key mouse-region-delete-keys)
1182 (delete-region (mark t) (point))
1183 ;; Otherwise, unread the key so it gets executed normally.
1184 (setq unread-command-events
1185 (nconc events unread-command-events))))
1186 (setq quit-flag nil)
1187 (unless transient-mark-mode
1188 (delete-overlay mouse-drag-overlay))))
1190 (defun mouse-set-mark (click)
1191 "Set mark at the position clicked on with the mouse.
1192 Display cursor at that position for a second.
1193 This must be bound to a mouse click."
1195 (mouse-minibuffer-check click)
1196 (select-window (posn-window (event-start click)))
1197 ;; We don't use save-excursion because that preserves the mark too.
1198 (let ((point-save (point)))
1200 (progn (mouse-set-point click)
1202 (or transient-mark-mode
1204 (goto-char point-save))))
1206 (defun mouse-kill (click)
1207 "Kill the region between point and the mouse click.
1208 The text is saved in the kill ring, as with \\[kill-region]."
1210 (mouse-minibuffer-check click)
1211 (let* ((posn (event-start click))
1212 (click-posn (posn-point posn)))
1213 (select-window (posn-window posn))
1214 (if (numberp click-posn)
1215 (kill-region (min (point) click-posn)
1216 (max (point) click-posn)))))
1218 (defun mouse-yank-at-click (click arg)
1219 "Insert the last stretch of killed text at the position clicked on.
1220 Also move point to one end of the text thus inserted (normally the end),
1221 and set mark at the beginning.
1222 Prefix arguments are interpreted as with \\[yank].
1223 If `mouse-yank-at-point' is non-nil, insert at point
1224 regardless of where you click."
1225 (interactive "e\nP")
1226 ;; Give temporary modes such as isearch a chance to turn off.
1227 (run-hooks 'mouse-leave-buffer-hook)
1228 (or mouse-yank-at-point (mouse-set-point click))
1229 (setq this-command 'yank)
1230 (setq mouse-selection-click-count 0)
1233 (defun mouse-kill-ring-save (click)
1234 "Copy the region between point and the mouse click in the kill ring.
1235 This does not delete the region; it acts like \\[kill-ring-save]."
1237 (mouse-set-mark-fast click)
1238 (let (this-command last-command)
1239 (kill-ring-save (point) (mark t)))
1242 ;;; This function used to delete the text between point and the mouse
1243 ;;; whenever it was equal to the front of the kill ring, but some
1244 ;;; people found that confusing.
1246 ;;; A list (TEXT START END), describing the text and position of the last
1247 ;;; invocation of mouse-save-then-kill.
1248 (defvar mouse-save-then-kill-posn nil)
1250 (defun mouse-save-then-kill-delete-region (beg end)
1251 ;; We must make our own undo boundaries
1252 ;; because they happen automatically only for the current buffer.
1254 (if (or (= beg end) (eq buffer-undo-list t))
1255 ;; If we have no undo list in this buffer,
1257 (delete-region beg end)
1258 ;; Delete, but make the undo-list entry share with the kill ring.
1259 ;; First, delete just one char, so in case buffer is being modified
1260 ;; for the first time, the undo list records that fact.
1261 (let (before-change-functions after-change-functions)
1263 (+ beg (if (> end beg) 1 -1))))
1264 (let ((buffer-undo-list buffer-undo-list))
1265 ;; Undo that deletion--but don't change the undo list!
1266 (let (before-change-functions after-change-functions)
1267 (primitive-undo 1 buffer-undo-list))
1268 ;; Now delete the rest of the specified region,
1269 ;; but don't record it.
1270 (setq buffer-undo-list t)
1271 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1272 (error "Lossage in mouse-save-then-kill-delete-region"))
1273 (delete-region beg end))
1274 (let ((tail buffer-undo-list))
1275 ;; Search back in buffer-undo-list for the string
1276 ;; that came from deleting one character.
1277 (while (and tail (not (stringp (car (car tail)))))
1278 (setq tail (cdr tail)))
1279 ;; Replace it with an entry for the entire deleted text.
1281 (setcar tail (cons (car kill-ring) (min beg end))))))
1284 (defun mouse-save-then-kill (click)
1285 "Save text to point in kill ring; the second time, kill the text.
1286 If the text between point and the mouse is the same as what's
1287 at the front of the kill ring, this deletes the text.
1288 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1289 which prepares for a second click to delete the text.
1291 If you have selected words or lines, this command extends the
1292 selection through the word or line clicked on. If you do this
1293 again in a different position, it extends the selection again.
1294 If you do this twice in the same position, the selection is killed."
1296 (let ((before-scroll
1297 (with-current-buffer (window-buffer (posn-window (event-start click)))
1298 point-before-scroll)))
1299 (mouse-minibuffer-check click)
1300 (let ((click-posn (posn-point (event-start click)))
1301 ;; Don't let a subsequent kill command append to this one:
1302 ;; prevent setting this-command to kill-region.
1303 (this-command this-command))
1304 (if (and (save-excursion
1305 (set-buffer (window-buffer (posn-window (event-start click))))
1306 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1307 ;; Don't be fooled by a recent click in some other buffer.
1308 (eq mouse-selection-click-count-buffer
1309 (current-buffer)))))
1310 (if (not (and (eq last-command 'mouse-save-then-kill)
1312 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1313 ;; Find both ends of the object selected by this click.
1315 (mouse-start-end click-posn click-posn
1316 mouse-selection-click-count)))
1317 ;; Move whichever end is closer to the click.
1318 ;; That's what xterm does, and it seems reasonable.
1319 (if (< (abs (- click-posn (mark t)))
1320 (abs (- click-posn (point))))
1321 (set-mark (car range))
1322 (goto-char (nth 1 range)))
1323 ;; We have already put the old region in the kill ring.
1324 ;; Replace it with the extended region.
1325 ;; (It would be annoying to make a separate entry.)
1326 (kill-new (buffer-substring (point) (mark t)) t)
1327 (mouse-set-region-1)
1328 ;; Arrange for a repeated mouse-3 to kill this region.
1329 (setq mouse-save-then-kill-posn
1330 (list (car kill-ring) (point) click-posn))
1332 ;; If we click this button again without moving it,
1334 (mouse-save-then-kill-delete-region (mark) (point))
1335 (setq mouse-selection-click-count 0)
1336 (setq mouse-save-then-kill-posn nil))
1337 (if (and (eq last-command 'mouse-save-then-kill)
1338 mouse-save-then-kill-posn
1339 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1340 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1341 ;; If this is the second time we've called
1342 ;; mouse-save-then-kill, delete the text from the buffer.
1344 (mouse-save-then-kill-delete-region (point) (mark))
1345 ;; After we kill, another click counts as "the first time".
1346 (setq mouse-save-then-kill-posn nil))
1347 ;; This is not a repetition.
1348 ;; We are adjusting an old selection or creating a new one.
1349 (if (or (and (eq last-command 'mouse-save-then-kill)
1350 mouse-save-then-kill-posn)
1351 (and mark-active transient-mark-mode)
1352 (and (memq last-command
1353 '(mouse-drag-region mouse-set-region))
1354 (or mark-even-if-inactive
1355 (not transient-mark-mode))))
1356 ;; We have a selection or suitable region, so adjust it.
1357 (let* ((posn (event-start click))
1358 (new (posn-point posn)))
1359 (select-window (posn-window posn))
1362 ;; Move whichever end of the region is closer to the click.
1363 ;; That is what xterm does, and it seems reasonable.
1364 (if (<= (abs (- new (point))) (abs (- new (mark t))))
1367 (setq deactivate-mark nil)))
1368 (kill-new (buffer-substring (point) (mark t)) t)
1370 ;; Set the mark where point is, then move where clicked.
1371 (mouse-set-mark-fast click)
1373 (goto-char before-scroll))
1374 (exchange-point-and-mark)
1375 (kill-new (buffer-substring (point) (mark t)))
1377 (mouse-set-region-1)
1378 (setq mouse-save-then-kill-posn
1379 (list (car kill-ring) (point) click-posn)))))))
1381 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1382 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1383 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1384 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1385 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1387 ;; An overlay which records the current secondary selection
1388 ;; or else is deleted when there is no secondary selection.
1390 (defvar mouse-secondary-overlay nil)
1392 (defvar mouse-secondary-click-count 0)
1394 ;; A marker which records the specified first end for a secondary selection.
1396 (defvar mouse-secondary-start nil)
1398 (defun mouse-start-secondary (click)
1399 "Set one end of the secondary selection to the position clicked on.
1400 Use \\[mouse-secondary-save-then-kill] to set the other end
1401 and complete the secondary selection."
1403 (mouse-minibuffer-check click)
1404 (let ((posn (event-start click)))
1406 (set-buffer (window-buffer (posn-window posn)))
1407 ;; Cancel any preexisting secondary selection.
1408 (if mouse-secondary-overlay
1409 (delete-overlay mouse-secondary-overlay))
1410 (if (numberp (posn-point posn))
1412 (or mouse-secondary-start
1413 (setq mouse-secondary-start (make-marker)))
1414 (move-marker mouse-secondary-start (posn-point posn)))))))
1416 (defun mouse-set-secondary (click)
1417 "Set the secondary selection to the text that the mouse is dragged over.
1418 This must be bound to a mouse drag event."
1420 (mouse-minibuffer-check click)
1421 (let ((posn (event-start click))
1423 (end (event-end click)))
1425 (set-buffer (window-buffer (posn-window posn)))
1426 (if (numberp (posn-point posn))
1427 (setq beg (posn-point posn)))
1428 (if mouse-secondary-overlay
1429 (move-overlay mouse-secondary-overlay beg (posn-point end))
1430 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1431 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1433 (defun mouse-drag-secondary (start-event)
1434 "Set the secondary selection to the text that the mouse is dragged over.
1435 Highlight the drag area as you move the mouse.
1436 This must be bound to a button-down mouse event.
1437 The function returns a non-nil value if it creates a secondary selection."
1439 (mouse-minibuffer-check start-event)
1440 (let* ((echo-keystrokes 0)
1441 (start-posn (event-start start-event))
1442 (start-point (posn-point start-posn))
1443 (start-window (posn-window start-posn))
1444 (start-frame (window-frame start-window))
1445 (bounds (window-edges start-window))
1446 (top (nth 1 bounds))
1447 (bottom (if (window-minibuffer-p start-window)
1449 ;; Don't count the mode line.
1450 (1- (nth 3 bounds))))
1451 (click-count (1- (event-click-count start-event))))
1453 (set-buffer (window-buffer start-window))
1454 (setq mouse-secondary-click-count click-count)
1455 (or mouse-secondary-overlay
1456 (setq mouse-secondary-overlay
1457 (make-overlay (point) (point))))
1458 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1459 (if (> (mod click-count 3) 0)
1460 ;; Double or triple press: make an initial selection
1461 ;; of one word or line.
1462 (let ((range (mouse-start-end start-point start-point click-count)))
1463 (set-marker mouse-secondary-start nil)
1464 (move-overlay mouse-secondary-overlay 1 1
1465 (window-buffer start-window))
1466 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1467 (window-buffer start-window)))
1468 ;; Single-press: cancel any preexisting secondary selection.
1469 (or mouse-secondary-start
1470 (setq mouse-secondary-start (make-marker)))
1471 (set-marker mouse-secondary-start start-point)
1472 (delete-overlay mouse-secondary-overlay))
1473 (let (event end end-point)
1476 (setq event (read-event))
1477 (or (mouse-movement-p event)
1478 (eq (car-safe event) 'switch-frame)))
1480 (if (eq (car-safe event) 'switch-frame)
1482 (setq end (event-end event)
1483 end-point (posn-point end))
1485 ;; Are we moving within the original window?
1486 ((and (eq (posn-window end) start-window)
1487 (integer-or-marker-p end-point))
1488 (let ((range (mouse-start-end start-point end-point
1490 (if (or (/= start-point end-point)
1491 (null (marker-position mouse-secondary-start)))
1493 (set-marker mouse-secondary-start nil)
1494 (move-overlay mouse-secondary-overlay
1495 (car range) (nth 1 range))))))
1497 (let ((mouse-row (cdr (cdr (mouse-position)))))
1501 (mouse-scroll-subr start-window (- mouse-row top)
1502 mouse-secondary-overlay start-point))
1503 ((>= mouse-row bottom)
1504 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1505 mouse-secondary-overlay start-point)))))))))
1508 (if (marker-position mouse-secondary-start)
1509 (save-window-excursion
1510 (delete-overlay mouse-secondary-overlay)
1511 (x-set-selection 'SECONDARY nil)
1512 (select-window start-window)
1514 (goto-char mouse-secondary-start)
1519 (buffer-substring (overlay-start mouse-secondary-overlay)
1520 (overlay-end mouse-secondary-overlay)))))))))
1522 (defun mouse-yank-secondary (click)
1523 "Insert the secondary selection at the position clicked on.
1524 Move point to the end of the inserted text.
1525 If `mouse-yank-at-point' is non-nil, insert at point
1526 regardless of where you click."
1528 ;; Give temporary modes such as isearch a chance to turn off.
1529 (run-hooks 'mouse-leave-buffer-hook)
1530 (or mouse-yank-at-point (mouse-set-point click))
1531 (insert (x-get-selection 'SECONDARY)))
1533 (defun mouse-kill-secondary ()
1534 "Kill the text in the secondary selection.
1535 This is intended more as a keyboard command than as a mouse command
1536 but it can work as either one.
1538 The current buffer (in case of keyboard use), or the buffer clicked on,
1539 must be the one that the secondary selection is in. This requirement
1540 is to prevent accidents."
1542 (let* ((keys (this-command-keys))
1543 (click (elt keys (1- (length keys)))))
1544 (or (eq (overlay-buffer mouse-secondary-overlay)
1546 (window-buffer (posn-window (event-start click)))
1548 (error "Select or click on the buffer where the secondary selection is")))
1551 (set-buffer (overlay-buffer mouse-secondary-overlay))
1552 (kill-region (overlay-start mouse-secondary-overlay)
1553 (overlay-end mouse-secondary-overlay))))
1554 (delete-overlay mouse-secondary-overlay)
1555 ;;; (x-set-selection 'SECONDARY nil)
1556 (setq mouse-secondary-overlay nil))
1558 (defun mouse-secondary-save-then-kill (click)
1559 "Save text to point in kill ring; the second time, kill the text.
1560 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1561 If the text between where you did \\[mouse-start-secondary] and where
1562 you use this command matches the text at the front of the kill ring,
1563 this command deletes the text.
1564 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1565 which prepares for a second click with this command to delete the text.
1567 If you have already made a secondary selection in that buffer,
1568 this command extends or retracts the selection to where you click.
1569 If you do this again in a different position, it extends or retracts
1570 again. If you do this twice in the same position, it kills the selection."
1572 (mouse-minibuffer-check click)
1573 (let ((posn (event-start click))
1574 (click-posn (posn-point (event-start click)))
1575 ;; Don't let a subsequent kill command append to this one:
1576 ;; prevent setting this-command to kill-region.
1577 (this-command this-command))
1578 (or (eq (window-buffer (posn-window posn))
1579 (or (and mouse-secondary-overlay
1580 (overlay-buffer mouse-secondary-overlay))
1581 (if mouse-secondary-start
1582 (marker-buffer mouse-secondary-start))))
1583 (error "Wrong buffer"))
1585 (set-buffer (window-buffer (posn-window posn)))
1586 (if (> (mod mouse-secondary-click-count 3) 0)
1587 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1589 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1590 ;; Find both ends of the object selected by this click.
1592 (mouse-start-end click-posn click-posn
1593 mouse-secondary-click-count)))
1594 ;; Move whichever end is closer to the click.
1595 ;; That's what xterm does, and it seems reasonable.
1596 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1597 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1598 (move-overlay mouse-secondary-overlay (car range)
1599 (overlay-end mouse-secondary-overlay))
1600 (move-overlay mouse-secondary-overlay
1601 (overlay-start mouse-secondary-overlay)
1603 ;; We have already put the old region in the kill ring.
1604 ;; Replace it with the extended region.
1605 ;; (It would be annoying to make a separate entry.)
1606 (kill-new (buffer-substring
1607 (overlay-start mouse-secondary-overlay)
1608 (overlay-end mouse-secondary-overlay)) t)
1609 ;; Arrange for a repeated mouse-3 to kill this region.
1610 (setq mouse-save-then-kill-posn
1611 (list (car kill-ring) (point) click-posn)))
1612 ;; If we click this button again without moving it,
1615 (mouse-save-then-kill-delete-region
1616 (overlay-start mouse-secondary-overlay)
1617 (overlay-end mouse-secondary-overlay))
1618 (setq mouse-save-then-kill-posn nil)
1619 (setq mouse-secondary-click-count 0)
1620 (delete-overlay mouse-secondary-overlay)))
1621 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1622 mouse-save-then-kill-posn
1623 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1624 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1625 ;; If this is the second time we've called
1626 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1628 (mouse-save-then-kill-delete-region
1629 (overlay-start mouse-secondary-overlay)
1630 (overlay-end mouse-secondary-overlay))
1631 (setq mouse-save-then-kill-posn nil)
1632 (delete-overlay mouse-secondary-overlay))
1633 (if (overlay-start mouse-secondary-overlay)
1634 ;; We have a selection, so adjust it.
1636 (if (numberp click-posn)
1638 ;; Move whichever end of the region is closer to the click.
1639 ;; That is what xterm does, and it seems reasonable.
1640 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1641 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1642 (move-overlay mouse-secondary-overlay click-posn
1643 (overlay-end mouse-secondary-overlay))
1644 (move-overlay mouse-secondary-overlay
1645 (overlay-start mouse-secondary-overlay)
1647 (setq deactivate-mark nil)))
1648 (if (eq last-command 'mouse-secondary-save-then-kill)
1649 ;; If the front of the kill ring comes from
1650 ;; an immediately previous use of this command,
1651 ;; replace it with the extended region.
1652 ;; (It would be annoying to make a separate entry.)
1653 (kill-new (buffer-substring
1654 (overlay-start mouse-secondary-overlay)
1655 (overlay-end mouse-secondary-overlay)) t)
1656 (let (deactivate-mark)
1657 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1658 (overlay-end mouse-secondary-overlay)))))
1659 (if mouse-secondary-start
1660 ;; All we have is one end of a selection,
1661 ;; so put the other end here.
1662 (let ((start (+ 0 mouse-secondary-start)))
1663 (kill-ring-save start click-posn)
1664 (if mouse-secondary-overlay
1665 (move-overlay mouse-secondary-overlay start click-posn)
1666 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1667 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1668 (setq mouse-save-then-kill-posn
1669 (list (car kill-ring) (point) click-posn))))
1670 (if (overlay-buffer mouse-secondary-overlay)
1671 (x-set-selection 'SECONDARY
1673 (overlay-start mouse-secondary-overlay)
1674 (overlay-end mouse-secondary-overlay)))))))
1676 (defcustom mouse-buffer-menu-maxlen 20
1677 "*Number of buffers in one pane (submenu) of the buffer menu.
1678 If we have lots of buffers, divide them into groups of
1679 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1683 (defcustom mouse-buffer-menu-mode-mult 4
1684 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1685 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1686 will split the buffer menu by the major modes (see
1687 `mouse-buffer-menu-mode-groups') or just by menu length.
1688 Set to 1 (or even 0!) if you want to group by major mode always, and to
1689 a large number if you prefer a mixed multitude. The default is 4."
1694 (defvar mouse-buffer-menu-mode-groups
1695 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1696 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1701 ("Outline" . "Text")
1702 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1703 ("log\\|diff\\|vc\\|cvs" . "Version Control") ; "Change Management"?
1705 "How to group various major modes together in \\[mouse-buffer-menu].
1706 Each element has the form (REGEXP . GROUPNAME).
1707 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1709 (defun mouse-buffer-menu (event)
1710 "Pop up a menu of buffers for selection with the mouse.
1711 This switches buffers in the window that you clicked on,
1712 and selects that window."
1714 (mouse-minibuffer-check event)
1715 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1716 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1717 (let ((tail buffers))
1719 ;; Divide all buffers into buckets for various major modes.
1720 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1721 (with-current-buffer (car tail)
1722 (let* ((adjusted-major-mode major-mode) elt)
1723 (let ((tail mouse-buffer-menu-mode-groups))
1725 (if (string-match (car (car tail)) mode-name)
1726 (setq adjusted-major-mode (cdr (car tail))))
1727 (setq tail (cdr tail))))
1728 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1730 (setq elt (list adjusted-major-mode
1731 (if (stringp adjusted-major-mode)
1734 split-by-major-mode (cons elt split-by-major-mode)))
1735 (or (memq (car tail) (cdr (cdr elt)))
1736 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1737 (setq tail (cdr tail))))
1738 ;; Compute the sum of squares of sizes of the major-mode buckets.
1739 (let ((tail split-by-major-mode))
1740 (setq sum-of-squares 0)
1742 (setq sum-of-squares
1744 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1745 (setq tail (cdr tail))))
1746 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1747 (* (length buffers) (length buffers)))
1748 ;; Subdividing by major modes really helps, so let's do it.
1749 (let (subdivided-menus (buffers-left (length buffers)))
1750 ;; Sort the list to put the most popular major modes first.
1751 (setq split-by-major-mode
1752 (sort split-by-major-mode
1753 (function (lambda (elt1 elt2)
1754 (> (length elt1) (length elt2))))))
1755 ;; Make a separate submenu for each major mode
1756 ;; that has more than one buffer,
1757 ;; unless all the remaining buffers are less than 1/10 of them.
1758 (while (and split-by-major-mode
1759 (and (> (length (car split-by-major-mode)) 3)
1760 (> (* buffers-left 10) (length buffers))))
1761 (let ((this-mode-list (mouse-buffer-menu-alist
1762 (cdr (cdr (car split-by-major-mode))))))
1764 (setq subdivided-menus
1766 (nth 1 (car split-by-major-mode))
1768 subdivided-menus))))
1770 (- buffers-left (length (cdr (car split-by-major-mode)))))
1771 (setq split-by-major-mode (cdr split-by-major-mode)))
1772 ;; If any major modes are left over,
1773 ;; make a single submenu for them.
1774 (if split-by-major-mode
1776 (mouse-buffer-menu-alist
1777 ;; we don't need split-by-major-mode any more,
1778 ;; so we can ditch it with nconc.
1779 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1781 (setq subdivided-menus
1782 (cons (cons "Others" others-list)
1783 subdivided-menus)))))
1784 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1786 (setq alist (mouse-buffer-menu-alist buffers))
1787 (setq menu (cons "Buffer Menu"
1788 (mouse-buffer-menu-split "Select Buffer" alist)))))
1789 (let ((buf (x-popup-menu event menu))
1790 (window (posn-window (event-start event))))
1793 (if (framep window) (frame-selected-window window)
1795 (switch-to-buffer buf)))))
1797 (defun mouse-buffer-menu-alist (buffers)
1803 (function (lambda (elt1 elt2)
1804 (string< (buffer-name elt1) (buffer-name elt2))))))
1807 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1810 (length (buffer-name (car tail))))))
1811 (setq tail (cdr tail)))
1814 (let ((elt (car tail)))
1815 (if (/= (aref (buffer-name elt) 0) ?\ )
1820 (format "%%%ds %%s%%s %%s" maxlen)
1822 (if (buffer-modified-p elt) "*" " ")
1825 (if buffer-read-only "%" " "))
1826 (or (buffer-file-name elt)
1829 (if list-buffers-directory
1831 list-buffers-directory)))
1835 (setq tail (cdr tail)))
1836 ;; Compensate for the reversal that the above loop does.
1839 (defun mouse-buffer-menu-split (title alist)
1840 ;; If we have lots of buffers, divide them into groups of 20
1841 ;; and make a pane (or submenu) for each one.
1842 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1843 (let ((alist alist) sublists next
1846 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1847 ;; and make them the next element of sublist.
1848 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1850 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1852 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1856 (nreverse sublists))
1857 ;; Few buffers--put them all in one pane.
1858 (list (cons title alist))))
1860 ;;; These need to be rewritten for the new scroll bar implementation.
1862 ;;;!! ;; Commands for the scroll bar.
1864 ;;;!! (defun mouse-scroll-down (click)
1865 ;;;!! (interactive "@e")
1866 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1868 ;;;!! (defun mouse-scroll-up (click)
1869 ;;;!! (interactive "@e")
1870 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1872 ;;;!! (defun mouse-scroll-down-full ()
1873 ;;;!! (interactive "@")
1874 ;;;!! (scroll-down nil))
1876 ;;;!! (defun mouse-scroll-up-full ()
1877 ;;;!! (interactive "@")
1878 ;;;!! (scroll-up nil))
1880 ;;;!! (defun mouse-scroll-move-cursor (click)
1881 ;;;!! (interactive "@e")
1882 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1884 ;;;!! (defun mouse-scroll-absolute (event)
1885 ;;;!! (interactive "@e")
1886 ;;;!! (let* ((pos (car event))
1887 ;;;!! (position (car pos))
1888 ;;;!! (length (car (cdr pos))))
1889 ;;;!! (if (<= length 0) (setq length 1))
1890 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1891 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1894 ;;;!! scale-factor)))
1895 ;;;!! (goto-char newpos)
1896 ;;;!! (recenter '(4)))))
1898 ;;;!! (defun mouse-scroll-left (click)
1899 ;;;!! (interactive "@e")
1900 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1902 ;;;!! (defun mouse-scroll-right (click)
1903 ;;;!! (interactive "@e")
1904 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1906 ;;;!! (defun mouse-scroll-left-full ()
1907 ;;;!! (interactive "@")
1908 ;;;!! (scroll-left nil))
1910 ;;;!! (defun mouse-scroll-right-full ()
1911 ;;;!! (interactive "@")
1912 ;;;!! (scroll-right nil))
1914 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1915 ;;;!! (interactive "@e")
1916 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1918 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1919 ;;;!! (interactive "@e")
1920 ;;;!! (let* ((pos (car event))
1921 ;;;!! (position (car pos))
1922 ;;;!! (length (car (cdr pos))))
1923 ;;;!! (set-window-hscroll (selected-window) 33)))
1925 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1926 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1927 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1929 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1930 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1931 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1933 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1934 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1935 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1937 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1938 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1939 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1941 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1942 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1943 ;;;!! 'mouse-scroll-absolute-horizontally)
1944 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1946 ;;;!! (global-set-key [horizontal-slider mouse-1]
1947 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1948 ;;;!! (global-set-key [horizontal-slider mouse-2]
1949 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1950 ;;;!! (global-set-key [horizontal-slider mouse-3]
1951 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1953 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1954 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1955 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1957 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1958 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1959 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1961 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1962 ;;;!! 'mouse-split-window-horizontally)
1963 ;;;!! (global-set-key [mode-line S-mouse-2]
1964 ;;;!! 'mouse-split-window-horizontally)
1965 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1966 ;;;!! 'mouse-split-window)
1969 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1970 ;;;!! ;;;; are of the form:
1971 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1974 ;;;!! ;;;; Dynamically track mouse coordinates
1977 ;;;!! ;;(defun track-mouse (event)
1978 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1979 ;;;!! ;; (interactive "@e")
1980 ;;;!! ;; (while mouse-grabbed
1981 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1982 ;;;!! ;; (abs-x (car pos))
1983 ;;;!! ;; (abs-y (cdr pos))
1984 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1985 ;;;!! ;; (list (car pos) (cdr pos))
1986 ;;;!! ;; (selected-window))))
1987 ;;;!! ;; (if (consp relative-coordinate)
1988 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1989 ;;;!! ;; (car relative-coordinate)
1990 ;;;!! ;; (car (cdr relative-coordinate)))
1991 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1994 ;;;!! ;; Dynamically put a box around the line indicated by point
1997 ;;;!! ;;(require 'backquote)
1999 ;;;!! ;;(defun mouse-select-buffer-line (event)
2000 ;;;!! ;; (interactive "@e")
2001 ;;;!! ;; (let ((relative-coordinate
2002 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
2003 ;;;!! ;; (abs-y (car (cdr (car event)))))
2004 ;;;!! ;; (if (consp relative-coordinate)
2006 ;;;!! ;; (save-excursion
2007 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2008 ;;;!! ;; (x-draw-rectangle
2009 ;;;!! ;; (selected-screen)
2011 ;;;!! ;; (save-excursion
2012 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2013 ;;;!! ;; (end-of-line)
2014 ;;;!! ;; (push-mark nil t)
2015 ;;;!! ;; (beginning-of-line)
2016 ;;;!! ;; (- (region-end) (region-beginning))) 1))
2017 ;;;!! ;; (sit-for 1)
2018 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
2020 ;;;!! ;;(defvar last-line-drawn nil)
2021 ;;;!! ;;(defvar begin-delim "[^ \t]")
2022 ;;;!! ;;(defvar end-delim "[^ \t]")
2024 ;;;!! ;;(defun mouse-boxing (event)
2025 ;;;!! ;; (interactive "@e")
2026 ;;;!! ;; (save-excursion
2027 ;;;!! ;; (let ((screen (selected-screen)))
2028 ;;;!! ;; (while (= (x-mouse-events) 0)
2029 ;;;!! ;; (let* ((pos (read-mouse-position screen))
2030 ;;;!! ;; (abs-x (car pos))
2031 ;;;!! ;; (abs-y (cdr pos))
2032 ;;;!! ;; (relative-coordinate
2033 ;;;!! ;; (coordinates-in-window-p `(,abs-x ,abs-y)
2034 ;;;!! ;; (selected-window)))
2035 ;;;!! ;; (begin-reg nil)
2036 ;;;!! ;; (end-reg nil)
2037 ;;;!! ;; (end-column nil)
2038 ;;;!! ;; (begin-column nil))
2039 ;;;!! ;; (if (and (consp relative-coordinate)
2040 ;;;!! ;; (or (not last-line-drawn)
2041 ;;;!! ;; (not (= last-line-drawn abs-y))))
2043 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2044 ;;;!! ;; (if (= (following-char) 10)
2047 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
2048 ;;;!! ;; (setq begin-column (1- (current-column)))
2049 ;;;!! ;; (end-of-line)
2050 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
2051 ;;;!! ;; (setq end-column (1+ (current-column)))
2052 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
2053 ;;;!! ;; (x-draw-rectangle screen
2054 ;;;!! ;; (setq last-line-drawn abs-y)
2055 ;;;!! ;; begin-column
2056 ;;;!! ;; (- end-column begin-column) 1))))))))))
2058 ;;;!! ;;(defun mouse-erase-box ()
2059 ;;;!! ;; (interactive)
2060 ;;;!! ;; (if last-line-drawn
2062 ;;;!! ;; (x-erase-rectangle (selected-screen))
2063 ;;;!! ;; (setq last-line-drawn nil))))
2065 ;;;!! ;;; (defun test-x-rectangle ()
2066 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
2067 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
2068 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
2071 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
2074 ;;;!! (defvar double-start nil)
2075 ;;;!! (defconst double-click-interval 300
2076 ;;;!! "Max ticks between clicks")
2078 ;;;!! (defun double-down (event)
2079 ;;;!! (interactive "@e")
2080 ;;;!! (if double-start
2081 ;;;!! (let ((interval (- (nth 4 event) double-start)))
2082 ;;;!! (if (< interval double-click-interval)
2084 ;;;!! (backward-up-list 1)
2085 ;;;!! ;; (message "Interval %d" interval)
2086 ;;;!! (sleep-for 1)))
2087 ;;;!! (setq double-start nil))
2088 ;;;!! (setq double-start (nth 4 event))))
2090 ;;;!! (defun double-up (event)
2091 ;;;!! (interactive "@e")
2092 ;;;!! (and double-start
2093 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
2094 ;;;!! (setq double-start nil)))
2096 ;;;!! ;;; (defun x-test-doubleclick ()
2097 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
2098 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
2099 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
2102 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
2105 ;;;!! (defvar scrolled-lines 0)
2106 ;;;!! (defconst scroll-speed 1)
2108 ;;;!! (defun incr-scroll-down (event)
2109 ;;;!! (interactive "@e")
2110 ;;;!! (setq scrolled-lines 0)
2111 ;;;!! (incremental-scroll scroll-speed))
2113 ;;;!! (defun incr-scroll-up (event)
2114 ;;;!! (interactive "@e")
2115 ;;;!! (setq scrolled-lines 0)
2116 ;;;!! (incremental-scroll (- scroll-speed)))
2118 ;;;!! (defun incremental-scroll (n)
2119 ;;;!! (while (= (x-mouse-events) 0)
2120 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
2121 ;;;!! (scroll-down n)
2122 ;;;!! (sit-for 300 t)))
2124 ;;;!! (defun incr-scroll-stop (event)
2125 ;;;!! (interactive "@e")
2126 ;;;!! (message "Scrolled %d lines" scrolled-lines)
2127 ;;;!! (setq scrolled-lines 0)
2128 ;;;!! (sleep-for 1))
2130 ;;;!! ;;; (defun x-testing-scroll ()
2131 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
2132 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
2133 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
2134 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
2135 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
2138 ;;;!! ;; Some playthings suitable for picture mode? They need work.
2141 ;;;!! (defun mouse-kill-rectangle (event)
2142 ;;;!! "Kill the rectangle between point and the mouse cursor."
2143 ;;;!! (interactive "@e")
2144 ;;;!! (let ((point-save (point)))
2145 ;;;!! (save-excursion
2146 ;;;!! (mouse-set-point event)
2147 ;;;!! (push-mark nil t)
2148 ;;;!! (if (> point-save (point))
2149 ;;;!! (kill-rectangle (point) point-save)
2150 ;;;!! (kill-rectangle point-save (point))))))
2152 ;;;!! (defun mouse-open-rectangle (event)
2153 ;;;!! "Kill the rectangle between point and the mouse cursor."
2154 ;;;!! (interactive "@e")
2155 ;;;!! (let ((point-save (point)))
2156 ;;;!! (save-excursion
2157 ;;;!! (mouse-set-point event)
2158 ;;;!! (push-mark nil t)
2159 ;;;!! (if (> point-save (point))
2160 ;;;!! (open-rectangle (point) point-save)
2161 ;;;!! (open-rectangle point-save (point))))))
2163 ;;;!! ;; Must be a better way to do this.
2165 ;;;!! (defun mouse-multiple-insert (n char)
2166 ;;;!! (while (> n 0)
2168 ;;;!! (setq n (1- n))))
2170 ;;;!! ;; What this could do is not finalize until button was released.
2172 ;;;!! (defun mouse-move-text (event)
2173 ;;;!! "Move text from point to cursor position, inserting spaces."
2174 ;;;!! (interactive "@e")
2175 ;;;!! (let* ((relative-coordinate
2176 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
2177 ;;;!! (if (consp relative-coordinate)
2178 ;;;!! (cond ((> (current-column) (car relative-coordinate))
2180 ;;;!! (- (car relative-coordinate) (current-column))))
2181 ;;;!! ((< (current-column) (car relative-coordinate))
2182 ;;;!! (mouse-multiple-insert
2183 ;;;!! (- (car relative-coordinate) (current-column)) " "))
2184 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
2186 ;; Choose a completion with the mouse.
2188 (defun mouse-choose-completion (event)
2189 "Click on an alternative in the `*Completions*' buffer to choose it."
2191 ;; Give temporary modes such as isearch a chance to turn off.
2192 (run-hooks 'mouse-leave-buffer-hook)
2193 (let ((buffer (window-buffer))
2197 (set-buffer (window-buffer (posn-window (event-start event))))
2198 (if completion-reference-buffer
2199 (setq buffer completion-reference-buffer))
2200 (setq base-size completion-base-size)
2202 (goto-char (posn-point (event-start event)))
2204 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2205 (setq end (point) beg (1+ (point))))
2207 (error "No completion here"))
2208 (setq beg (previous-single-property-change beg 'mouse-face))
2209 (setq end (or (next-single-property-change end 'mouse-face)
2211 (setq choice (buffer-substring beg end)))))
2212 (let ((owindow (selected-window)))
2213 (select-window (posn-window (event-start event)))
2214 (if (and (one-window-p t 'selected-frame)
2215 (window-dedicated-p (selected-window)))
2216 ;; This is a special buffer's frame
2217 (iconify-frame (selected-frame))
2218 (or (window-dedicated-p (selected-window))
2220 (select-window owindow))
2221 (choose-completion-string choice buffer base-size)))
2225 (defun font-menu-add-default ()
2226 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2227 (font-alist x-fixed-font-alist)
2228 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2229 (if (assoc "Default" elt)
2230 (delete (assoc "Default" elt) elt))
2232 (cons (list "Default" default)
2235 (defvar x-fixed-font-alist
2238 ;; For these, we specify the pixel height and width.
2240 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2242 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2244 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2245 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2246 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2247 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2248 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2249 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2250 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2251 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2254 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2256 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2258 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2260 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2262 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2264 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2266 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2267 ;;; We don't seem to have these; who knows what they are.
2268 ;;; ("fg-18" "fg-18")
2269 ;;; ("fg-25" "fg-25")
2270 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2271 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2272 ("lucidasanstypewriter-bold-24"
2273 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2274 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2275 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2278 ;; For these, we specify the point height.
2279 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2280 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2281 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2282 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2283 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2284 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2285 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2286 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2287 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2288 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2289 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2290 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2291 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2292 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2293 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2294 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2295 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2296 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2297 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2298 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2299 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2300 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2301 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2302 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2304 "X fonts suitable for use in Emacs.")
2306 (defun mouse-set-font (&rest fonts)
2307 "Select an emacs font from a list of known good fonts and fontsets."
2309 (progn (unless (display-multi-font-p)
2310 (error "Cannot change fonts on this display"))
2312 (if (listp last-nonmenu-event)
2314 (list '(0 0) (selected-window)))
2315 ;; Append list of fontsets currently defined.
2316 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2322 (set-default-font (car fonts))
2323 (setq font (car fonts))
2326 (setq fonts (cdr fonts)))))
2328 (error "Font not found")))))
2330 ;;; Bindings for mouse commands.
2332 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2333 (global-set-key [mouse-1] 'mouse-set-point)
2334 (global-set-key [drag-mouse-1] 'mouse-set-region)
2336 ;; These are tested for in mouse-drag-region.
2337 (global-set-key [double-mouse-1] 'mouse-set-point)
2338 (global-set-key [triple-mouse-1] 'mouse-set-point)
2340 ;; Clicking on the fringes causes hscrolling:
2341 (global-set-key [left-fringe mouse-1] 'mouse-set-point)
2342 (global-set-key [right-fringe mouse-1] 'mouse-set-point)
2344 (global-set-key [mouse-2] 'mouse-yank-at-click)
2345 (global-set-key [mouse-3] 'mouse-save-then-kill)
2347 ;; By binding these to down-going events, we let the user use the up-going
2348 ;; event to make the selection, saving a click.
2349 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2350 (if (not (eq system-type 'ms-dos))
2351 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2352 ;; C-down-mouse-2 is bound in facemenu.el.
2353 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2356 ;; Replaced with dragging mouse-1
2357 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2359 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2360 ;; vertical-line prevents Emacs from signaling an error when the mouse
2361 ;; button is released after dragging these lines, on non-toolkit
2363 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2364 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2365 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2366 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2367 (global-set-key [header-line mouse-1] 'mouse-select-window)
2368 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2369 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2370 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2371 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2372 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2373 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2374 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2378 ;; This file contains the functionality of the old mldrag.el.
2379 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2380 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2381 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2382 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2385 ;;; arch-tag: 9a710ce1-914a-4923-9b81-697f7bf82ab3
2386 ;;; mouse.el ends here