1 ;;; mouse.el --- window system-independent mouse support
3 ;; Copyright (C) 1993, 94, 95, 1999, 2000, 01, 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 ;; Provide a mode-specific menu on a mouse button.
54 (defun popup-menu (menu &optional position prefix
)
55 "Popup the given menu and call the selected option.
56 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
58 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
59 the current mouse position.
60 PREFIX is the prefix argument (if any) to pass to the command."
63 ((and (listp menu
) (keymapp (car menu
))) menu
)
64 (t (let* ((map (easy-menu-create-menu (car menu
) (cdr menu
)))
65 (filter (when (symbolp map
)
66 (plist-get (get map
'menu-prop
) :filter
))))
67 (if filter
(funcall filter
(symbol-function map
)) map
)))))
70 (let ((mp (mouse-pixel-position)))
71 (setq position
(list (list (cadr mp
) (cddr mp
)) (car mp
)))))
72 ;; The looping behavior was taken from lmenu's popup-menu-popup
73 (while (and map
(setq event
74 ;; map could be a prefix key, in which case
75 ;; we need to get its function cell
77 (x-popup-menu position
(indirect-function map
))))
78 ;; Strangely x-popup-menu returns a list.
79 ;; mouse-major-mode-menu was using a weird:
80 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
82 (if (and (not (keymapp map
)) (listp map
))
83 ;; We were given a list of keymaps. Search them all
84 ;; in sequence until a first binding is found.
85 (let ((mouse-click (apply 'vector event
))
87 (while (and map
(null binding
))
88 (setq binding
(lookup-key (car map
) mouse-click
))
89 (if (numberp binding
) ; `too long'
93 ;; We were given a single keymap.
94 (lookup-key map
(apply 'vector event
))))
95 ;; Clear out echoing, which perhaps shows a prefix arg.
97 ;; Maybe try again but with the submap.
98 (setq map
(if (keymapp cmd
) cmd
)))
99 ;; If the user did not cancel by refusing to select,
100 ;; and if the result is a command, run it.
101 (when (and (null map
) (commandp cmd
))
102 (setq prefix-arg prefix
)
103 ;; `setup-specified-language-environment', for instance,
104 ;; expects this to be set from a menu keymap.
105 (setq last-command-event
(car (last event
)))
106 ;; mouse-major-mode-menu was using `command-execute' instead.
107 (call-interactively cmd
))))
109 (defvar mouse-major-mode-menu-prefix
) ; dynamically bound
111 (defun mouse-major-mode-menu (event prefix
)
112 "Pop up a mode-specific menu of mouse commands.
113 Default to the Edit menu if the major mode doesn't define a menu."
114 ;; Switch to the window clicked on, because otherwise
115 ;; the mode's commands may not make sense.
116 (interactive "@e\nP")
117 ;; Let the mode update its menus first.
118 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
119 (let* (;; This is where mouse-major-mode-menu-prefix
120 ;; returns the prefix we should use (after menu-bar).
121 ;; It is either nil or (SOME-SYMBOL).
122 (mouse-major-mode-menu-prefix nil
)
123 ;; Keymap from which to inherit; may be null.
124 (ancestor (mouse-major-mode-menu-1
125 (and (current-local-map)
126 (local-key-binding [menu-bar
]))))
127 ;; Make a keymap in which our last command leads to a menu or
128 ;; default to the edit menu.
130 (make-sparse-keymap (concat mode-name
" Mode"))
134 ;; Make our menu inherit from the desired keymap which we want
135 ;; to display as the menu now.
136 (set-keymap-parent newmap ancestor
))
137 (popup-menu newmap event prefix
)))
140 ;; Compute and cache the equivalent keys in MENU and all its submenus.
141 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
142 ;;; (and (eq (car menu) 'keymap)
143 ;;; (x-popup-menu nil menu))
145 ;;; (and (consp (car menu))
146 ;;; (consp (cdr (car menu)))
147 ;;; (let ((tail (cdr (car menu))))
148 ;;; (while (and (consp tail)
149 ;;; (not (eq (car tail) 'keymap)))
150 ;;; (setq tail (cdr tail)))
152 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
153 ;;; (setq menu (cdr menu))))
155 ;; Given a mode's menu bar keymap,
156 ;; if it defines exactly one menu bar menu,
157 ;; return just that menu.
158 ;; Otherwise return a menu for all of them.
159 (defun mouse-major-mode-menu-1 (menubar)
164 (if (consp (car tail
))
167 (setq submap
(car tail
))))
168 (setq tail
(cdr tail
)))
171 (setq mouse-major-mode-menu-prefix
(list (car submap
)))
172 (lookup-key menubar
(vector (car submap
)))))))
174 (defun mouse-popup-menubar (event prefix
)
175 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
176 The contents are the items that would be in the menu bar whether or
177 not it is actually displayed."
178 (interactive "@e \nP")
179 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
180 (let* ((local-menu (and (current-local-map)
181 (lookup-key (current-local-map) [menu-bar
])))
182 (global-menu (lookup-key global-map
[menu-bar
]))
183 ;; If a keymap doesn't have a prompt string (a lazy
184 ;; programmer didn't bother to provide one), create it and
185 ;; insert it into the keymap; each keymap gets its own
186 ;; prompt. This is required for non-toolkit versions to
187 ;; display non-empty menu pane names.
192 (let* ((minor-mode (car menu
))
194 (title-or-map (cadr menu
)))
195 (or (stringp title-or-map
)
199 (capitalize (subst-char-in-string
205 (minor-mode-key-binding [menu-bar
])))
206 (local-title-or-map (and local-menu
(cadr local-menu
)))
207 (global-title-or-map (cadr global-menu
)))
208 (or (null local-menu
)
209 (stringp local-title-or-map
)
210 (setq local-menu
(cons 'keymap
211 (cons (concat mode-name
" Mode Menu")
213 (or (stringp global-title-or-map
)
214 (setq global-menu
(cons 'keymap
216 (cdr global-menu
)))))
217 ;; Supplying the list is faster than making a new map.
218 (popup-menu (append (list global-menu
)
224 (defun mouse-popup-menubar-stuff (event prefix
)
225 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
226 Use the former if the menu bar is showing, otherwise the latter."
227 (interactive "@e \nP")
228 (if (zerop (assoc-default 'menu-bar-lines
(frame-parameters) 'eq
0))
229 (mouse-popup-menubar event prefix
)
230 (mouse-major-mode-menu event prefix
)))
232 ;; Commands that operate on windows.
234 (defun mouse-minibuffer-check (event)
235 (let ((w (posn-window (event-start event
))))
236 (and (window-minibuffer-p w
)
237 (not (minibuffer-window-active-p w
))
238 (error "Minibuffer window is not active")))
239 ;; Give temporary modes such as isearch a chance to turn off.
240 (run-hooks 'mouse-leave-buffer-hook
))
242 (defun mouse-delete-window (click)
243 "Delete the window you click on.
244 Do nothing if the frame has just one window.
245 This command must be bound to a mouse click."
247 (unless (one-window-p t
)
248 (mouse-minibuffer-check click
)
249 (delete-window (posn-window (event-start click
)))))
251 (defun mouse-select-window (click)
252 "Select the window clicked on; don't move point."
254 (mouse-minibuffer-check click
)
255 (let ((oframe (selected-frame))
256 (frame (window-frame (posn-window (event-start click
)))))
257 (select-window (posn-window (event-start click
)))
260 (or (eq frame oframe
)
261 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
263 (defun mouse-tear-off-window (click)
264 "Delete the window clicked on, and create a new frame displaying its buffer."
266 (mouse-minibuffer-check click
)
267 (let* ((window (posn-window (event-start click
)))
268 (buf (window-buffer window
))
269 (frame (make-frame)))
271 (switch-to-buffer buf
)
272 (delete-window window
)))
274 (defun mouse-delete-other-windows ()
275 "Delete all windows except the one you click on."
277 (delete-other-windows))
279 (defun mouse-split-window-vertically (click)
280 "Select Emacs window mouse is on, then split it vertically in half.
281 The window is split at the line clicked on.
282 This command must be bound to a mouse click."
284 (mouse-minibuffer-check click
)
285 (let ((start (event-start click
)))
286 (select-window (posn-window start
))
287 (let ((new-height (1+ (cdr (posn-col-row (event-end click
)))))
288 (first-line window-min-height
)
289 (last-line (- (window-height) window-min-height
)))
290 (if (< last-line first-line
)
291 (error "Window too short to split")
292 (split-window-vertically
293 (min (max new-height first-line
) last-line
))))))
295 (defun mouse-split-window-horizontally (click)
296 "Select Emacs window mouse is on, then split it horizontally in half.
297 The window is split at the column clicked on.
298 This command must be bound to a mouse click."
300 (mouse-minibuffer-check click
)
301 (let ((start (event-start click
)))
302 (select-window (posn-window start
))
303 (let ((new-width (1+ (car (posn-col-row (event-end click
)))))
304 (first-col window-min-width
)
305 (last-col (- (window-width) window-min-width
)))
306 (if (< last-col first-col
)
307 (error "Window too narrow to split")
308 (split-window-horizontally
309 (min (max new-width first-col
) last-col
))))))
311 (defun mouse-drag-window-above (window)
312 "Return the (or a) window directly above WINDOW.
313 That means one whose bottom edge is at the same height as WINDOW's top edge."
314 (let ((top (nth 1 (window-edges window
)))
315 (start-window window
)
317 (setq window
(previous-window window
0))
318 (while (and (not above-window
) (not (eq window start-window
)))
319 (if (= (+ (window-height window
) (nth 1 (window-edges window
)))
321 (setq above-window window
))
322 (setq window
(previous-window window
)))
325 (defun mouse-drag-move-window-bottom (window growth
)
326 "Move the bottom of WINDOW up or down by GROWTH lines.
327 Move it down if GROWTH is positive, or up if GROWTH is negative.
328 If this would make WINDOW too short,
329 shrink the window or windows above it to make room."
330 (let ((excess (- window-min-height
(+ (window-height window
) growth
))))
331 ;; EXCESS is the number of lines we need to take from windows above.
333 ;; This can recursively shrink windows all the way up.
334 (let ((window-above (mouse-drag-window-above window
)))
336 (mouse-drag-move-window-bottom window-above
(- excess
))))))
337 (save-selected-window
338 (select-window window
)
339 (enlarge-window growth nil
(> growth
0))))
341 (defsubst mouse-drag-move-window-top
(window growth
)
342 "Move the top of WINDOW up or down by GROWTH lines.
343 Move it down if GROWTH is positive, or up if GROWTH is negative.
344 If this would make WINDOW too short, shrink the window or windows
345 above it to make room."
346 ;; Moving the top of WINDOW is actually moving the bottom of the
348 (let ((window-above (mouse-drag-window-above window
)))
350 (mouse-drag-move-window-bottom window-above
(- growth
)))))
352 (defun mouse-drag-mode-line-1 (start-event mode-line-p
)
353 "Change the height of a window by dragging on the mode or header line.
354 START-EVENT is the starting mouse-event of the drag action.
355 MODE-LINE-P non-nil means dragging a mode line; nil means a header line."
356 ;; Give temporary modes such as isearch a chance to turn off.
357 (run-hooks 'mouse-leave-buffer-hook
)
360 (start (event-start start-event
))
361 (start-event-window (posn-window start
))
362 (start-event-frame (window-frame start-event-window
))
363 (start-nwindows (count-windows t
))
364 (old-selected-window (selected-window))
365 (minibuffer (frame-parameter nil
'minibuffer
))
366 should-enlarge-minibuffer event mouse y top bot edges wconfig growth
)
369 ;; enlarge-window only works on the selected window, so
370 ;; we must select the window where the start event originated.
371 ;; unwind-protect will restore the old selected window later.
372 (select-window start-event-window
)
374 ;; if this is the bottommost ordinary window, then to
375 ;; move its modeline the minibuffer must be enlarged.
376 (setq should-enlarge-minibuffer
379 (not (one-window-p t
))
380 (= (nth 1 (window-edges minibuffer
))
381 (nth 3 (window-edges)))))
383 ;; loop reading events and sampling the position of
386 (setq event
(read-event)
387 mouse
(mouse-position))
390 ;; - there is a switch-frame event.
391 ;; - the mouse isn't in the frame that we started in
392 ;; - the mouse isn't in any Emacs frame
394 ;; - there is a mouse-movement event
395 ;; - there is a scroll-bar-movement event
396 ;; (same as mouse movement for our purposes)
398 ;; - there is a keyboard event or some other unknown event
400 (cond ((integerp event
)
403 ((eq (car event
) 'switch-frame
)
406 ((not (memq (car event
) '(mouse-movement scroll-bar-movement
)))
408 (push event unread-command-events
))
411 ((not (eq (car mouse
) start-event-frame
))
414 ((null (car (cdr mouse
)))
418 (setq y
(cdr (cdr mouse
))
423 ;; compute size change needed
425 (setq growth
(- y bot -
1)))
427 (when (< (- bot y
) window-min-height
)
428 (setq y
(- bot window-min-height
)))
429 ;; The window's top includes the header line!
430 (setq growth
(- top y
))))
431 (setq wconfig
(current-window-configuration))
433 ;; Check for an error case.
434 (when (and (/= growth
0)
437 (error "Attempt to resize sole window"))
439 ;; grow/shrink minibuffer?
440 (if should-enlarge-minibuffer
442 ;; yes. briefly select minibuffer so
443 ;; enlarge-window will affect the
445 (select-window minibuffer
)
446 ;; scale back shrinkage if it would
447 ;; make the minibuffer less than 1
449 (if (and (> growth
0)
450 (< (- (window-height minibuffer
)
453 (setq growth
(1- (window-height minibuffer
))))
454 (enlarge-window (- growth
))
455 (select-window start-event-window
))
456 ;; no. grow/shrink the selected window
457 ;(message "growth = %d" growth)
459 (mouse-drag-move-window-bottom start-event-window growth
)
460 (mouse-drag-move-window-top start-event-window growth
)))
462 ;; if this window's growth caused another
463 ;; window to be deleted because it was too
464 ;; short, rescind the change.
466 ;; if size change caused space to be stolen
467 ;; from a window above this one, rescind the
468 ;; change, but only if we didn't grow/shrink
469 ;; the minibuffer. minibuffer size changes
470 ;; can cause all windows to shrink... no way
472 (when (or (/= start-nwindows
(count-windows t
))
473 (and (not should-enlarge-minibuffer
)
476 (/= top
(nth 1 (window-edges)))))
477 (set-window-configuration wconfig
)))))))))
479 (defun mouse-drag-mode-line (start-event)
480 "Change the height of a window by dragging on the mode line."
482 (mouse-drag-mode-line-1 start-event t
))
484 (defun mouse-drag-header-line (start-event)
485 "Change the height of a window by dragging on the header line.
486 Windows whose header-lines are at the top of the frame cannot be
487 resized by dragging their header-line."
489 ;; Changing the window's size by dragging its header-line when the
490 ;; header-line is at the top of the frame is somewhat strange,
491 ;; because the header-line doesn't move, so don't do it.
492 (let* ((start (event-start start-event
))
493 (window (posn-window start
))
494 (frame (window-frame window
))
495 (first-window (frame-first-window frame
)))
496 (when (or (eq window first-window
)
497 (= (nth 1 (window-edges window
))
498 (nth 1 (window-edges first-window
))))
499 (error "Cannot move header-line at the top of the frame"))
500 (mouse-drag-mode-line-1 start-event nil
)))
503 (defun mouse-drag-vertical-line (start-event)
504 "Change the width of a window by dragging on the vertical line."
506 ;; Give temporary modes such as isearch a chance to turn off.
507 (run-hooks 'mouse-leave-buffer-hook
)
510 (start-event-frame (window-frame (car (car (cdr start-event
)))))
511 (start-event-window (car (car (cdr start-event
))))
512 (start-nwindows (count-windows t
))
513 (old-selected-window (selected-window))
514 event mouse x left right edges wconfig growth
516 (or (cdr (assq 'vertical-scroll-bars
(frame-parameters start-event-frame
)))
519 (error "Attempt to resize sole ordinary window"))
520 (if (eq which-side
'right
)
521 (if (= (nth 2 (window-edges start-event-window
))
522 (frame-width start-event-frame
))
523 (error "Attempt to drag rightmost scrollbar"))
524 (if (= (nth 0 (window-edges start-event-window
)) 0)
525 (error "Attempt to drag leftmost scrollbar")))
528 ;; enlarge-window only works on the selected window, so
529 ;; we must select the window where the start event originated.
530 ;; unwind-protect will restore the old selected window later.
531 (select-window start-event-window
)
532 ;; loop reading events and sampling the position of
535 (setq event
(read-event)
536 mouse
(mouse-position))
538 ;; - there is a switch-frame event.
539 ;; - the mouse isn't in the frame that we started in
540 ;; - the mouse isn't in any Emacs frame
542 ;; - there is a mouse-movement event
543 ;; - there is a scroll-bar-movement event
544 ;; (same as mouse movement for our purposes)
546 ;; - there is a keyboard event or some other unknown event
548 (cond ((integerp event
)
550 ((eq (car event
) 'switch-frame
)
552 ((not (memq (car event
)
553 '(mouse-movement scroll-bar-movement
)))
555 (setq unread-command-events
556 (cons event unread-command-events
)))
558 ((not (eq (car mouse
) start-event-frame
))
560 ((null (car (cdr mouse
)))
563 (save-selected-window
564 ;; If the scroll bar is on the window's left,
565 ;; adjust the window on the left.
566 (unless (eq which-side
'right
)
567 (select-window (previous-window)))
568 (setq x
(- (car (cdr mouse
))
569 (if (eq which-side
'right
) 0 2))
573 ;; scale back a move that would make the
575 (if (< (- x left -
1) window-min-width
)
576 (setq x
(+ left window-min-width -
1)))
577 ;; compute size change needed
578 (setq growth
(- x right -
1)
579 wconfig
(current-window-configuration))
580 (enlarge-window growth t
)
581 ;; if this window's growth caused another
582 ;; window to be deleted because it was too
583 ;; thin, rescind the change.
585 ;; if size change caused space to be stolen
586 ;; from a window to the left of this one,
587 ;; rescind the change.
588 (if (or (/= start-nwindows
(count-windows t
))
589 (/= left
(nth 0 (window-edges))))
590 (set-window-configuration wconfig
))))))))))
592 (defun mouse-set-point (event)
593 "Move point to the position clicked on with the mouse.
594 This should be bound to a mouse click event type."
596 (mouse-minibuffer-check event
)
597 ;; Use event-end in case called from mouse-drag-region.
598 ;; If EVENT is a click, event-end and event-start give same value.
599 (posn-set-point (event-end event
)))
601 (defvar mouse-last-region-beg nil
)
602 (defvar mouse-last-region-end nil
)
603 (defvar mouse-last-region-tick nil
)
605 (defun mouse-region-match ()
606 "Return non-nil if there's an active region that was set with the mouse."
607 (and (mark t
) mark-active
608 (eq mouse-last-region-beg
(region-beginning))
609 (eq mouse-last-region-end
(region-end))
610 (eq mouse-last-region-tick
(buffer-modified-tick))))
612 (defun mouse-set-region (click)
613 "Set the region to the text dragged over, and copy to kill ring.
614 This should be bound to a mouse drag event."
616 (mouse-minibuffer-check click
)
617 (let ((posn (event-start click
))
618 (end (event-end click
)))
619 (select-window (posn-window posn
))
620 (if (numberp (posn-point posn
))
621 (goto-char (posn-point posn
)))
622 ;; If mark is highlighted, no need to bounce the cursor.
623 ;; On X, we highlight while dragging, thus once again no need to bounce.
624 (or transient-mark-mode
625 (memq (framep (selected-frame)) '(x pc w32
))
629 (if (numberp (posn-point end
))
630 (goto-char (posn-point end
)))
631 ;; Don't set this-command to kill-region, so that a following
632 ;; C-w will not double the text in the kill ring.
633 ;; Ignore last-command so we don't append to a preceding kill.
634 (when mouse-drag-copy-region
635 (let (this-command last-command deactivate-mark
)
636 (copy-region-as-kill (mark) (point))))
637 (mouse-set-region-1)))
639 (defun mouse-set-region-1 ()
640 ;; Set transient-mark-mode for a little while.
641 (if (memq transient-mark-mode
'(nil identity
))
642 (setq transient-mark-mode
'only
))
643 (setq mouse-last-region-beg
(region-beginning))
644 (setq mouse-last-region-end
(region-end))
645 (setq mouse-last-region-tick
(buffer-modified-tick)))
647 (defcustom mouse-scroll-delay
0.25
648 "*The pause between scroll steps caused by mouse drags, in seconds.
649 If you drag the mouse beyond the edge of a window, Emacs scrolls the
650 window to bring the text beyond that edge into view, with a delay of
651 this many seconds between scroll steps. Scrolling stops when you move
652 the mouse back into the window, or release the button.
653 This variable's value may be non-integral.
654 Setting this to zero causes Emacs to scroll as fast as it can."
658 (defcustom mouse-scroll-min-lines
1
659 "*The minimum number of lines scrolled by dragging mouse out of window.
660 Moving the mouse out the top or bottom edge of the window begins
661 scrolling repeatedly. The number of lines scrolled per repetition
662 is normally equal to the number of lines beyond the window edge that
663 the mouse has moved. However, it always scrolls at least the number
664 of lines specified by this variable."
668 (defun mouse-scroll-subr (window jump
&optional overlay start
)
669 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
670 If OVERLAY is an overlay, let it stretch from START to the far edge of
671 the newly visible text.
672 Upon exit, point is at the far edge of the newly visible text."
674 ((and (> jump
0) (< jump mouse-scroll-min-lines
))
675 (setq jump mouse-scroll-min-lines
))
676 ((and (< jump
0) (< (- jump
) mouse-scroll-min-lines
))
677 (setq jump
(- mouse-scroll-min-lines
))))
678 (let ((opoint (point)))
680 (goto-char (window-start window
))
681 (if (not (zerop (vertical-motion jump window
)))
683 (set-window-start window
(point))
685 (if (window-end window
)
687 (goto-char (window-end window
))
688 ;; window-end doesn't reflect the window's new
689 ;; start position until the next redisplay.
690 (vertical-motion (1- jump
) window
))
691 (vertical-motion (- (window-height window
) 2)))
692 (goto-char (window-start window
)))
694 (move-overlay overlay start
(point)))
695 ;; Now that we have scrolled WINDOW properly,
696 ;; put point back where it was for the redisplay
697 ;; so that we don't mess up the selected window.
698 (or (eq window
(selected-window))
700 (sit-for mouse-scroll-delay
)))))
701 (or (eq window
(selected-window))
702 (goto-char opoint
))))
704 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
705 (defvar mouse-drag-overlay
(make-overlay 1 1))
706 (delete-overlay mouse-drag-overlay
)
707 (overlay-put mouse-drag-overlay
'face
'region
)
709 (defvar mouse-selection-click-count
0)
711 (defvar mouse-selection-click-count-buffer nil
)
713 (defun mouse-drag-region (start-event)
714 "Set the region to the text that the mouse is dragged over.
715 Highlight the drag area as you move the mouse.
716 This must be bound to a button-down mouse event.
717 In Transient Mark mode, the highlighting remains as long as the mark
718 remains active. Otherwise, it remains until the next input event.
720 If the click is in the echo area, display the `*Messages*' buffer."
722 (let ((w (posn-window (event-start start-event
))))
723 (if (not (or (not (window-minibuffer-p w
))
724 (minibuffer-window-active-p w
)))
727 (set-buffer "*Messages*")
728 (goto-char (point-max))
729 (display-buffer (current-buffer)))
730 ;; Give temporary modes such as isearch a chance to turn off.
731 (run-hooks 'mouse-leave-buffer-hook
)
732 (mouse-drag-region-1 start-event
))))
734 (defun mouse-drag-region-1 (start-event)
735 (mouse-minibuffer-check start-event
)
736 (let* ((echo-keystrokes 0)
737 (start-posn (event-start start-event
))
738 (start-point (posn-point start-posn
))
739 (start-window (posn-window start-posn
))
740 (start-window-start (window-start start-window
))
741 (start-frame (window-frame start-window
))
742 (start-hscroll (window-hscroll start-window
))
743 (bounds (window-edges start-window
))
745 (bottom (if (window-minibuffer-p start-window
)
747 ;; Don't count the mode line.
748 (1- (nth 3 bounds
))))
749 (click-count (1- (event-click-count start-event
))))
750 (setq mouse-selection-click-count click-count
)
751 (setq mouse-selection-click-count-buffer
(current-buffer))
752 (mouse-set-point start-event
)
753 ;; In case the down click is in the middle of some intangible text,
754 ;; use the end of that text, and put it in START-POINT.
755 (if (< (point) start-point
)
756 (goto-char start-point
))
757 (setq start-point
(point))
758 (let ((range (mouse-start-end start-point start-point click-count
)))
759 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
)
760 (window-buffer start-window
))
761 (overlay-put mouse-drag-overlay
'window
(selected-window)))
763 ;; end-of-range is used only in the single-click case.
764 ;; It is the place where the drag has reached so far
765 ;; (but not outside the window where the drag started).
766 (let (event end end-point last-end-point
(end-of-range (point)))
769 (setq event
(read-event))
770 (or (mouse-movement-p event
)
771 (eq (car-safe event
) 'switch-frame
)))
772 (if (eq (car-safe event
) 'switch-frame
)
774 (setq end
(event-end event
)
775 end-point
(posn-point end
))
776 (if (numberp end-point
)
777 (setq last-end-point end-point
))
780 ;; Are we moving within the original window?
781 ((and (eq (posn-window end
) start-window
)
782 (integer-or-marker-p end-point
))
783 ;; Go to START-POINT first, so that when we move to END-POINT,
784 ;; if it's in the middle of intangible text,
785 ;; point jumps in the direction away from START-POINT.
786 (goto-char start-point
)
787 (goto-char end-point
)
788 (if (zerop (% click-count
3))
789 (setq end-of-range
(point)))
790 (let ((range (mouse-start-end start-point
(point) click-count
)))
791 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
))))
794 (let ((mouse-row (cdr (cdr (mouse-position)))))
798 (mouse-scroll-subr start-window
(- mouse-row top
)
799 mouse-drag-overlay start-point
)
800 ;; Without this, point tends to jump back to the starting
801 ;; position where the mouse button was pressed down.
802 (setq end-of-range
(overlay-start mouse-drag-overlay
)))
803 ((>= mouse-row bottom
)
804 (mouse-scroll-subr start-window
(1+ (- mouse-row bottom
))
805 mouse-drag-overlay start-point
)
806 (setq end-of-range
(overlay-end mouse-drag-overlay
))))))))))
808 ;; In case we did not get a mouse-motion event
809 ;; for the final move of the mouse before a drag event
810 ;; pretend that we did get one.
811 (when (and (memq 'drag
(event-modifiers (car-safe event
)))
812 (setq end
(event-end event
)
813 end-point
(posn-point end
))
814 (eq (posn-window end
) start-window
)
815 (integer-or-marker-p end-point
))
816 ;; Go to START-POINT first, so that when we move to END-POINT,
817 ;; if it's in the middle of intangible text,
818 ;; point jumps in the direction away from START-POINT.
819 (goto-char start-point
)
820 (goto-char end-point
)
821 (if (zerop (% click-count
3))
822 (setq end-of-range
(point)))
823 (let ((range (mouse-start-end start-point
(point) click-count
)))
824 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
))))
827 (let ((fun (key-binding (vector (car event
)))))
828 ;; Run the binding of the terminating up-event, if possible.
829 ;; In the case of a multiple click, it gives the wrong results,
830 ;; because it would fail to set up a region.
831 (if (not (= (overlay-start mouse-drag-overlay
)
832 (overlay-end mouse-drag-overlay
)))
834 (if (numberp (posn-point (event-end event
)))
835 (posn-point (event-end event
))
837 ;; The end that comes from where we ended the drag.
840 (if (and stop-point
(< stop-point start-point
))
841 (overlay-start mouse-drag-overlay
)
842 (overlay-end mouse-drag-overlay
)))
843 ;; The end that comes from where we started the drag.
846 (- (+ (overlay-end mouse-drag-overlay
)
847 (overlay-start mouse-drag-overlay
))
849 last-command this-command
)
850 (push-mark region-commencement t t
)
851 (goto-char region-termination
)
852 ;; Don't let copy-region-as-kill set deactivate-mark.
853 (when mouse-drag-copy-region
854 (let (deactivate-mark)
855 (copy-region-as-kill (point) (mark t
))))
856 (let ((buffer (current-buffer)))
858 ;; mouse-show-mark can call read-event,
859 ;; and that means the Emacs server could switch buffers
860 ;; under us. If that happened,
861 ;; avoid trying to use the region.
862 (and (mark t
) mark-active
863 (eq buffer
(current-buffer))
864 (mouse-set-region-1))))
865 (delete-overlay mouse-drag-overlay
)
866 ;; Run the binding of the terminating up-event.
867 (when (and (functionp fun
)
868 (= start-hscroll
(window-hscroll start-window
))
869 ;; Don't run the up-event handler if the
870 ;; window start changed in a redisplay after
871 ;; the mouse-set-point for the down-mouse
872 ;; event at the beginning of this function.
873 ;; When the window start has changed, the
874 ;; up-mouse event will contain a different
875 ;; position due to the new window contents,
876 ;; and point is set again.
878 (= (window-start start-window
)
879 start-window-start
)))
880 (setq unread-command-events
881 (cons event unread-command-events
)))))
882 (delete-overlay mouse-drag-overlay
)))))
884 ;; Commands to handle xterm-style multiple clicks.
886 (defun mouse-skip-word (dir)
887 "Skip over word, over whitespace, or over identical punctuation.
888 If DIR is positive skip forward; if negative, skip backward."
889 (let* ((char (following-char))
890 (syntax (char-to-string (char-syntax char
))))
891 (cond ((string= syntax
"w")
892 ;; Here, we can't use skip-syntax-forward/backward because
893 ;; they don't pay attention to word-separating-categories,
894 ;; and thus they will skip over a true word boundary. So,
895 ;; we simularte the original behaviour by using
898 (if (not (looking-at "\\<"))
900 (if (or (looking-at "\\<") (not (looking-at "\\>")))
902 ((string= syntax
" ")
904 (skip-syntax-backward syntax
)
905 (skip-syntax-forward syntax
)))
906 ((string= syntax
"_")
908 (skip-syntax-backward "w_")
909 (skip-syntax-forward "w_")))
911 (while (and (not (bobp)) (= (preceding-char) char
))
914 (while (and (not (eobp)) (= (following-char) char
))
915 (forward-char 1))))))
917 (defun mouse-start-end (start end mode
)
918 "Return a list of region bounds based on START and END according to MODE.
919 If MODE is 0 then set point to (min START END), mark to (max START END).
920 If MODE is 1 then set point to start of word at (min START END),
921 mark to end of word at (max START END).
922 If MODE is 2 then do the same for lines."
927 (setq mode
(mod mode
3))
933 (= (char-syntax (char-after start
)) ?\
())
942 (= (char-syntax (char-after start
)) ?\
)))
943 (list (save-excursion
944 (goto-char (1+ start
))
951 (= (char-syntax (char-after start
)) ?
\"))
952 (let ((open (or (eq start
(point-min))
954 (goto-char (- start
1))
955 (looking-at "\\s(\\|\\s \\|\\s>")))))
965 (list (save-excursion
968 (goto-char (1+ start
))
974 (list (save-excursion
983 (list (save-excursion
985 (beginning-of-line 1)
992 ;; Subroutine: set the mark where CLICK happened,
993 ;; but don't do anything else.
994 (defun mouse-set-mark-fast (click)
995 (mouse-minibuffer-check click
)
996 (let ((posn (event-start click
)))
997 (select-window (posn-window posn
))
998 (if (numberp (posn-point posn
))
999 (push-mark (posn-point posn
) t t
))))
1001 (defun mouse-undouble-last-event (events)
1002 (let* ((index (1- (length events
)))
1003 (last (nthcdr index events
))
1005 (basic (event-basic-type event
))
1006 (old-modifiers (event-modifiers event
))
1007 (modifiers (delq 'double
(delq 'triple
(copy-sequence old-modifiers
))))
1010 ;; Use reverse, not nreverse, since event-modifiers
1011 ;; does not copy the list it returns.
1012 (cons (event-convert-list (reverse (cons basic modifiers
)))
1016 (if (and (not (equal modifiers old-modifiers
))
1017 (key-binding (apply 'vector events
)))
1022 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1024 (defvar mouse-region-delete-keys
'([delete] [deletechar])
1025 "List of keys which shall cause the mouse region to be deleted.")
1027 (defun mouse-show-mark ()
1028 (let ((inhibit-quit t)
1030 event events key ignore
1031 (x-lost-selection-functions
1032 (when (boundp 'x-lost-selection-functions)
1033 (copy-sequence x-lost-selection-functions))))
1034 (add-hook 'x-lost-selection-functions
1036 (when (eq seltype 'PRIMARY)
1038 (throw 'mouse-show-mark t))))
1039 (if transient-mark-mode
1040 (delete-overlay mouse-drag-overlay)
1041 (move-overlay mouse-drag-overlay (point) (mark t)))
1042 (catch 'mouse-show-mark
1043 ;; In this loop, execute scroll bar and switch-frame events.
1044 ;; Also ignore down-events that are undefined.
1045 (while (progn (setq event (read-event))
1046 (setq events (append events (list event)))
1047 (setq key (apply 'vector events))
1048 (or (and (consp event)
1049 (eq (car event) 'switch-frame))
1051 (eq (posn-point (event-end event))
1052 'vertical-scroll-bar))
1053 (and (memq 'down (event-modifiers event))
1054 (not (key-binding key))
1055 (not (mouse-undouble-last-event events))
1056 (not (member key mouse-region-delete-keys)))))
1058 (or (eq (car event) 'switch-frame)
1059 (eq (posn-point (event-end event))
1060 'vertical-scroll-bar))
1061 (let ((keys (vector 'vertical-scroll-bar event)))
1062 (and (key-binding keys)
1064 (call-interactively (key-binding keys)
1066 (setq events nil)))))))
1067 ;; If we lost the selection, just turn off the highlighting.
1069 ;; For certain special keys, delete the region.
1070 (if (member key mouse-region-delete-keys)
1071 (delete-region (mark t) (point))
1072 ;; Otherwise, unread the key so it gets executed normally.
1073 (setq unread-command-events
1074 (nconc events unread-command-events))))
1075 (setq quit-flag nil)
1076 (unless transient-mark-mode
1077 (delete-overlay mouse-drag-overlay))))
1079 (defun mouse-set-mark (click)
1080 "Set mark at the position clicked on with the mouse.
1081 Display cursor at that position for a second.
1082 This must be bound to a mouse click."
1084 (mouse-minibuffer-check click)
1085 (select-window (posn-window (event-start click)))
1086 ;; We don't use save-excursion because that preserves the mark too.
1087 (let ((point-save (point)))
1089 (progn (mouse-set-point click)
1091 (or transient-mark-mode
1093 (goto-char point-save))))
1095 (defun mouse-kill (click)
1096 "Kill the region between point and the mouse click.
1097 The text is saved in the kill ring, as with \\[kill-region]."
1099 (mouse-minibuffer-check click)
1100 (let* ((posn (event-start click))
1101 (click-posn (posn-point posn)))
1102 (select-window (posn-window posn))
1103 (if (numberp click-posn)
1104 (kill-region (min (point) click-posn)
1105 (max (point) click-posn)))))
1107 (defun mouse-yank-at-click (click arg)
1108 "Insert the last stretch of killed text at the position clicked on.
1109 Also move point to one end of the text thus inserted (normally the end),
1110 and set mark at the beginning.
1111 Prefix arguments are interpreted as with \\[yank].
1112 If `mouse-yank-at-point' is non-nil, insert at point
1113 regardless of where you click."
1114 (interactive "e\nP")
1115 ;; Give temporary modes such as isearch a chance to turn off.
1116 (run-hooks 'mouse-leave-buffer-hook)
1117 (or mouse-yank-at-point (mouse-set-point click))
1118 (setq this-command 'yank)
1119 (setq mouse-selection-click-count 0)
1122 (defun mouse-kill-ring-save (click)
1123 "Copy the region between point and the mouse click in the kill ring.
1124 This does not delete the region; it acts like \\[kill-ring-save]."
1126 (mouse-set-mark-fast click)
1127 (let (this-command last-command)
1128 (kill-ring-save (point) (mark t)))
1131 ;;; This function used to delete the text between point and the mouse
1132 ;;; whenever it was equal to the front of the kill ring, but some
1133 ;;; people found that confusing.
1135 ;;; A list (TEXT START END), describing the text and position of the last
1136 ;;; invocation of mouse-save-then-kill.
1137 (defvar mouse-save-then-kill-posn nil)
1139 (defun mouse-save-then-kill-delete-region (beg end)
1140 ;; We must make our own undo boundaries
1141 ;; because they happen automatically only for the current buffer.
1143 (if (or (= beg end) (eq buffer-undo-list t))
1144 ;; If we have no undo list in this buffer,
1146 (delete-region beg end)
1147 ;; Delete, but make the undo-list entry share with the kill ring.
1148 ;; First, delete just one char, so in case buffer is being modified
1149 ;; for the first time, the undo list records that fact.
1150 (let (before-change-functions after-change-functions)
1152 (+ beg (if (> end beg) 1 -1))))
1153 (let ((buffer-undo-list buffer-undo-list))
1154 ;; Undo that deletion--but don't change the undo list!
1155 (let (before-change-functions after-change-functions)
1156 (primitive-undo 1 buffer-undo-list))
1157 ;; Now delete the rest of the specified region,
1158 ;; but don't record it.
1159 (setq buffer-undo-list t)
1160 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1161 (error "Lossage in mouse-save-then-kill-delete-region"))
1162 (delete-region beg end))
1163 (let ((tail buffer-undo-list))
1164 ;; Search back in buffer-undo-list for the string
1165 ;; that came from deleting one character.
1166 (while (and tail (not (stringp (car (car tail)))))
1167 (setq tail (cdr tail)))
1168 ;; Replace it with an entry for the entire deleted text.
1170 (setcar tail (cons (car kill-ring) (min beg end))))))
1173 (defun mouse-save-then-kill (click)
1174 "Save text to point in kill ring; the second time, kill the text.
1175 If the text between point and the mouse is the same as what's
1176 at the front of the kill ring, this deletes the text.
1177 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1178 which prepares for a second click to delete the text.
1180 If you have selected words or lines, this command extends the
1181 selection through the word or line clicked on. If you do this
1182 again in a different position, it extends the selection again.
1183 If you do this twice in the same position, the selection is killed."
1185 (let ((before-scroll
1186 (with-current-buffer (window-buffer (posn-window (event-start click)))
1187 point-before-scroll)))
1188 (mouse-minibuffer-check click)
1189 (let ((click-posn (posn-point (event-start click)))
1190 ;; Don't let a subsequent kill command append to this one:
1191 ;; prevent setting this-command to kill-region.
1192 (this-command this-command))
1193 (if (and (save-excursion
1194 (set-buffer (window-buffer (posn-window (event-start click))))
1195 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1196 ;; Don't be fooled by a recent click in some other buffer.
1197 (eq mouse-selection-click-count-buffer
1198 (current-buffer)))))
1199 (if (not (and (eq last-command 'mouse-save-then-kill)
1201 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1202 ;; Find both ends of the object selected by this click.
1204 (mouse-start-end click-posn click-posn
1205 mouse-selection-click-count)))
1206 ;; Move whichever end is closer to the click.
1207 ;; That's what xterm does, and it seems reasonable.
1208 (if (< (abs (- click-posn (mark t)))
1209 (abs (- click-posn (point))))
1210 (set-mark (car range))
1211 (goto-char (nth 1 range)))
1212 ;; We have already put the old region in the kill ring.
1213 ;; Replace it with the extended region.
1214 ;; (It would be annoying to make a separate entry.)
1215 (kill-new (buffer-substring (point) (mark t)) t)
1216 (mouse-set-region-1)
1217 ;; Arrange for a repeated mouse-3 to kill this region.
1218 (setq mouse-save-then-kill-posn
1219 (list (car kill-ring) (point) click-posn))
1221 ;; If we click this button again without moving it,
1223 (mouse-save-then-kill-delete-region (mark) (point))
1224 (setq mouse-selection-click-count 0)
1225 (setq mouse-save-then-kill-posn nil))
1226 (if (and (eq last-command 'mouse-save-then-kill)
1227 mouse-save-then-kill-posn
1228 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1229 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1230 ;; If this is the second time we've called
1231 ;; mouse-save-then-kill, delete the text from the buffer.
1233 (mouse-save-then-kill-delete-region (point) (mark))
1234 ;; After we kill, another click counts as "the first time".
1235 (setq mouse-save-then-kill-posn nil))
1236 ;; This is not a repetition.
1237 ;; We are adjusting an old selection or creating a new one.
1238 (if (or (and (eq last-command 'mouse-save-then-kill)
1239 mouse-save-then-kill-posn)
1240 (and mark-active transient-mark-mode)
1241 (and (memq last-command
1242 '(mouse-drag-region mouse-set-region))
1243 (or mark-even-if-inactive
1244 (not transient-mark-mode))))
1245 ;; We have a selection or suitable region, so adjust it.
1246 (let* ((posn (event-start click))
1247 (new (posn-point posn)))
1248 (select-window (posn-window posn))
1251 ;; Move whichever end of the region is closer to the click.
1252 ;; That is what xterm does, and it seems reasonable.
1253 (if (<= (abs (- new (point))) (abs (- new (mark t))))
1256 (setq deactivate-mark nil)))
1257 (kill-new (buffer-substring (point) (mark t)) t)
1259 ;; Set the mark where point is, then move where clicked.
1260 (mouse-set-mark-fast click)
1262 (goto-char before-scroll))
1263 (exchange-point-and-mark)
1264 (kill-new (buffer-substring (point) (mark t)))
1266 (mouse-set-region-1)
1267 (setq mouse-save-then-kill-posn
1268 (list (car kill-ring) (point) click-posn)))))))
1270 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1271 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1272 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1273 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1274 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1276 ;; An overlay which records the current secondary selection
1277 ;; or else is deleted when there is no secondary selection.
1279 (defvar mouse-secondary-overlay nil)
1281 (defvar mouse-secondary-click-count 0)
1283 ;; A marker which records the specified first end for a secondary selection.
1285 (defvar mouse-secondary-start nil)
1287 (defun mouse-start-secondary (click)
1288 "Set one end of the secondary selection to the position clicked on.
1289 Use \\[mouse-secondary-save-then-kill] to set the other end
1290 and complete the secondary selection."
1292 (mouse-minibuffer-check click)
1293 (let ((posn (event-start click)))
1295 (set-buffer (window-buffer (posn-window posn)))
1296 ;; Cancel any preexisting secondary selection.
1297 (if mouse-secondary-overlay
1298 (delete-overlay mouse-secondary-overlay))
1299 (if (numberp (posn-point posn))
1301 (or mouse-secondary-start
1302 (setq mouse-secondary-start (make-marker)))
1303 (move-marker mouse-secondary-start (posn-point posn)))))))
1305 (defun mouse-set-secondary (click)
1306 "Set the secondary selection to the text that the mouse is dragged over.
1307 This must be bound to a mouse drag event."
1309 (mouse-minibuffer-check click)
1310 (let ((posn (event-start click))
1312 (end (event-end click)))
1314 (set-buffer (window-buffer (posn-window posn)))
1315 (if (numberp (posn-point posn))
1316 (setq beg (posn-point posn)))
1317 (if mouse-secondary-overlay
1318 (move-overlay mouse-secondary-overlay beg (posn-point end))
1319 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1320 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1322 (defun mouse-drag-secondary (start-event)
1323 "Set the secondary selection to the text that the mouse is dragged over.
1324 Highlight the drag area as you move the mouse.
1325 This must be bound to a button-down mouse event.
1326 The function returns a non-nil value if it creates a secondary selection."
1328 (mouse-minibuffer-check start-event)
1329 (let* ((echo-keystrokes 0)
1330 (start-posn (event-start start-event))
1331 (start-point (posn-point start-posn))
1332 (start-window (posn-window start-posn))
1333 (start-frame (window-frame start-window))
1334 (bounds (window-edges start-window))
1335 (top (nth 1 bounds))
1336 (bottom (if (window-minibuffer-p start-window)
1338 ;; Don't count the mode line.
1339 (1- (nth 3 bounds))))
1340 (click-count (1- (event-click-count start-event))))
1342 (set-buffer (window-buffer start-window))
1343 (setq mouse-secondary-click-count click-count)
1344 (or mouse-secondary-overlay
1345 (setq mouse-secondary-overlay
1346 (make-overlay (point) (point))))
1347 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1348 (if (> (mod click-count 3) 0)
1349 ;; Double or triple press: make an initial selection
1350 ;; of one word or line.
1351 (let ((range (mouse-start-end start-point start-point click-count)))
1352 (set-marker mouse-secondary-start nil)
1353 (move-overlay mouse-secondary-overlay 1 1
1354 (window-buffer start-window))
1355 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1356 (window-buffer start-window)))
1357 ;; Single-press: cancel any preexisting secondary selection.
1358 (or mouse-secondary-start
1359 (setq mouse-secondary-start (make-marker)))
1360 (set-marker mouse-secondary-start start-point)
1361 (delete-overlay mouse-secondary-overlay))
1362 (let (event end end-point)
1365 (setq event (read-event))
1366 (or (mouse-movement-p event)
1367 (eq (car-safe event) 'switch-frame)))
1369 (if (eq (car-safe event) 'switch-frame)
1371 (setq end (event-end event)
1372 end-point (posn-point end))
1374 ;; Are we moving within the original window?
1375 ((and (eq (posn-window end) start-window)
1376 (integer-or-marker-p end-point))
1377 (let ((range (mouse-start-end start-point end-point
1379 (if (or (/= start-point end-point)
1380 (null (marker-position mouse-secondary-start)))
1382 (set-marker mouse-secondary-start nil)
1383 (move-overlay mouse-secondary-overlay
1384 (car range) (nth 1 range))))))
1386 (let ((mouse-row (cdr (cdr (mouse-position)))))
1390 (mouse-scroll-subr start-window (- mouse-row top)
1391 mouse-secondary-overlay start-point))
1392 ((>= mouse-row bottom)
1393 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1394 mouse-secondary-overlay start-point)))))))))
1397 (if (marker-position mouse-secondary-start)
1398 (save-window-excursion
1399 (delete-overlay mouse-secondary-overlay)
1400 (x-set-selection 'SECONDARY nil)
1401 (select-window start-window)
1403 (goto-char mouse-secondary-start)
1408 (buffer-substring (overlay-start mouse-secondary-overlay)
1409 (overlay-end mouse-secondary-overlay)))))))))
1411 (defun mouse-yank-secondary (click)
1412 "Insert the secondary selection at the position clicked on.
1413 Move point to the end of the inserted text.
1414 If `mouse-yank-at-point' is non-nil, insert at point
1415 regardless of where you click."
1417 ;; Give temporary modes such as isearch a chance to turn off.
1418 (run-hooks 'mouse-leave-buffer-hook)
1419 (or mouse-yank-at-point (mouse-set-point click))
1420 (insert (x-get-selection 'SECONDARY)))
1422 (defun mouse-kill-secondary ()
1423 "Kill the text in the secondary selection.
1424 This is intended more as a keyboard command than as a mouse command
1425 but it can work as either one.
1427 The current buffer (in case of keyboard use), or the buffer clicked on,
1428 must be the one that the secondary selection is in. This requirement
1429 is to prevent accidents."
1431 (let* ((keys (this-command-keys))
1432 (click (elt keys (1- (length keys)))))
1433 (or (eq (overlay-buffer mouse-secondary-overlay)
1435 (window-buffer (posn-window (event-start click)))
1437 (error "Select or click on the buffer where the secondary selection is")))
1440 (set-buffer (overlay-buffer mouse-secondary-overlay))
1441 (kill-region (overlay-start mouse-secondary-overlay)
1442 (overlay-end mouse-secondary-overlay))))
1443 (delete-overlay mouse-secondary-overlay)
1444 ;;; (x-set-selection 'SECONDARY nil)
1445 (setq mouse-secondary-overlay nil))
1447 (defun mouse-secondary-save-then-kill (click)
1448 "Save text to point in kill ring; the second time, kill the text.
1449 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1450 If the text between where you did \\[mouse-start-secondary] and where
1451 you use this command matches the text at the front of the kill ring,
1452 this command deletes the text.
1453 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1454 which prepares for a second click with this command to delete the text.
1456 If you have already made a secondary selection in that buffer,
1457 this command extends or retracts the selection to where you click.
1458 If you do this again in a different position, it extends or retracts
1459 again. If you do this twice in the same position, it kills the selection."
1461 (mouse-minibuffer-check click)
1462 (let ((posn (event-start click))
1463 (click-posn (posn-point (event-start click)))
1464 ;; Don't let a subsequent kill command append to this one:
1465 ;; prevent setting this-command to kill-region.
1466 (this-command this-command))
1467 (or (eq (window-buffer (posn-window posn))
1468 (or (and mouse-secondary-overlay
1469 (overlay-buffer mouse-secondary-overlay))
1470 (if mouse-secondary-start
1471 (marker-buffer mouse-secondary-start))))
1472 (error "Wrong buffer"))
1474 (set-buffer (window-buffer (posn-window posn)))
1475 (if (> (mod mouse-secondary-click-count 3) 0)
1476 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1478 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1479 ;; Find both ends of the object selected by this click.
1481 (mouse-start-end click-posn click-posn
1482 mouse-secondary-click-count)))
1483 ;; Move whichever end is closer to the click.
1484 ;; That's what xterm does, and it seems reasonable.
1485 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1486 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1487 (move-overlay mouse-secondary-overlay (car range)
1488 (overlay-end mouse-secondary-overlay))
1489 (move-overlay mouse-secondary-overlay
1490 (overlay-start mouse-secondary-overlay)
1492 ;; We have already put the old region in the kill ring.
1493 ;; Replace it with the extended region.
1494 ;; (It would be annoying to make a separate entry.)
1495 (kill-new (buffer-substring
1496 (overlay-start mouse-secondary-overlay)
1497 (overlay-end mouse-secondary-overlay)) t)
1498 ;; Arrange for a repeated mouse-3 to kill this region.
1499 (setq mouse-save-then-kill-posn
1500 (list (car kill-ring) (point) click-posn)))
1501 ;; If we click this button again without moving it,
1504 (mouse-save-then-kill-delete-region
1505 (overlay-start mouse-secondary-overlay)
1506 (overlay-end mouse-secondary-overlay))
1507 (setq mouse-save-then-kill-posn nil)
1508 (setq mouse-secondary-click-count 0)
1509 (delete-overlay mouse-secondary-overlay)))
1510 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1511 mouse-save-then-kill-posn
1512 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1513 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1514 ;; If this is the second time we've called
1515 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1517 (mouse-save-then-kill-delete-region
1518 (overlay-start mouse-secondary-overlay)
1519 (overlay-end mouse-secondary-overlay))
1520 (setq mouse-save-then-kill-posn nil)
1521 (delete-overlay mouse-secondary-overlay))
1522 (if (overlay-start mouse-secondary-overlay)
1523 ;; We have a selection, so adjust it.
1525 (if (numberp click-posn)
1527 ;; Move whichever end of the region is closer to the click.
1528 ;; That is what xterm does, and it seems reasonable.
1529 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1530 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1531 (move-overlay mouse-secondary-overlay click-posn
1532 (overlay-end mouse-secondary-overlay))
1533 (move-overlay mouse-secondary-overlay
1534 (overlay-start mouse-secondary-overlay)
1536 (setq deactivate-mark nil)))
1537 (if (eq last-command 'mouse-secondary-save-then-kill)
1538 ;; If the front of the kill ring comes from
1539 ;; an immediately previous use of this command,
1540 ;; replace it with the extended region.
1541 ;; (It would be annoying to make a separate entry.)
1542 (kill-new (buffer-substring
1543 (overlay-start mouse-secondary-overlay)
1544 (overlay-end mouse-secondary-overlay)) t)
1545 (let (deactivate-mark)
1546 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1547 (overlay-end mouse-secondary-overlay)))))
1548 (if mouse-secondary-start
1549 ;; All we have is one end of a selection,
1550 ;; so put the other end here.
1551 (let ((start (+ 0 mouse-secondary-start)))
1552 (kill-ring-save start click-posn)
1553 (if mouse-secondary-overlay
1554 (move-overlay mouse-secondary-overlay start click-posn)
1555 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1556 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1557 (setq mouse-save-then-kill-posn
1558 (list (car kill-ring) (point) click-posn))))
1559 (if (overlay-buffer mouse-secondary-overlay)
1560 (x-set-selection 'SECONDARY
1562 (overlay-start mouse-secondary-overlay)
1563 (overlay-end mouse-secondary-overlay)))))))
1565 (defcustom mouse-buffer-menu-maxlen 20
1566 "*Number of buffers in one pane (submenu) of the buffer menu.
1567 If we have lots of buffers, divide them into groups of
1568 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1572 (defcustom mouse-buffer-menu-mode-mult 4
1573 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1574 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1575 will split the buffer menu by the major modes (see
1576 `mouse-buffer-menu-mode-groups') or just by menu length.
1577 Set to 1 (or even 0!) if you want to group by major mode always, and to
1578 a large number if you prefer a mixed multitude. The default is 4."
1583 (defvar mouse-buffer-menu-mode-groups
1584 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1585 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1590 ("Outline" . "Text")
1591 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1592 ("log\\|diff\\|vc\\|cvs" . "Version Control") ; "Change Management"?
1594 "How to group various major modes together in \\[mouse-buffer-menu].
1595 Each element has the form (REGEXP . GROUPNAME).
1596 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1598 (defun mouse-buffer-menu (event)
1599 "Pop up a menu of buffers for selection with the mouse.
1600 This switches buffers in the window that you clicked on,
1601 and selects that window."
1603 (mouse-minibuffer-check event)
1604 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1605 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1606 (let ((tail buffers))
1608 ;; Divide all buffers into buckets for various major modes.
1609 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1610 (with-current-buffer (car tail)
1611 (let* ((adjusted-major-mode major-mode) elt)
1612 (let ((tail mouse-buffer-menu-mode-groups))
1614 (if (string-match (car (car tail)) mode-name)
1615 (setq adjusted-major-mode (cdr (car tail))))
1616 (setq tail (cdr tail))))
1617 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1619 (setq elt (list adjusted-major-mode
1620 (if (stringp adjusted-major-mode)
1623 split-by-major-mode (cons elt split-by-major-mode)))
1624 (or (memq (car tail) (cdr (cdr elt)))
1625 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1626 (setq tail (cdr tail))))
1627 ;; Compute the sum of squares of sizes of the major-mode buckets.
1628 (let ((tail split-by-major-mode))
1629 (setq sum-of-squares 0)
1631 (setq sum-of-squares
1633 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1634 (setq tail (cdr tail))))
1635 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1636 (* (length buffers) (length buffers)))
1637 ;; Subdividing by major modes really helps, so let's do it.
1638 (let (subdivided-menus (buffers-left (length buffers)))
1639 ;; Sort the list to put the most popular major modes first.
1640 (setq split-by-major-mode
1641 (sort split-by-major-mode
1642 (function (lambda (elt1 elt2)
1643 (> (length elt1) (length elt2))))))
1644 ;; Make a separate submenu for each major mode
1645 ;; that has more than one buffer,
1646 ;; unless all the remaining buffers are less than 1/10 of them.
1647 (while (and split-by-major-mode
1648 (and (> (length (car split-by-major-mode)) 3)
1649 (> (* buffers-left 10) (length buffers))))
1650 (let ((this-mode-list (mouse-buffer-menu-alist
1651 (cdr (cdr (car split-by-major-mode))))))
1653 (setq subdivided-menus
1655 (nth 1 (car split-by-major-mode))
1657 subdivided-menus))))
1659 (- buffers-left (length (cdr (car split-by-major-mode)))))
1660 (setq split-by-major-mode (cdr split-by-major-mode)))
1661 ;; If any major modes are left over,
1662 ;; make a single submenu for them.
1663 (if split-by-major-mode
1665 (mouse-buffer-menu-alist
1666 ;; we don't need split-by-major-mode any more,
1667 ;; so we can ditch it with nconc.
1668 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1670 (setq subdivided-menus
1671 (cons (cons "Others" others-list)
1672 subdivided-menus)))))
1673 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1675 (setq alist (mouse-buffer-menu-alist buffers))
1676 (setq menu (cons "Buffer Menu"
1677 (mouse-buffer-menu-split "Select Buffer" alist)))))
1678 (let ((buf (x-popup-menu event menu))
1679 (window (posn-window (event-start event))))
1682 (if (framep window) (frame-selected-window window)
1684 (switch-to-buffer buf)))))
1686 (defun mouse-buffer-menu-alist (buffers)
1692 (function (lambda (elt1 elt2)
1693 (string< (buffer-name elt1) (buffer-name elt2))))))
1696 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1699 (length (buffer-name (car tail))))))
1700 (setq tail (cdr tail)))
1703 (let ((elt (car tail)))
1704 (if (/= (aref (buffer-name elt) 0) ?\ )
1709 (format "%%%ds %%s%%s %%s" maxlen)
1711 (if (buffer-modified-p elt) "*" " ")
1714 (if buffer-read-only "%" " "))
1715 (or (buffer-file-name elt)
1718 (if list-buffers-directory
1720 list-buffers-directory)))
1724 (setq tail (cdr tail)))
1725 ;; Compensate for the reversal that the above loop does.
1728 (defun mouse-buffer-menu-split (title alist)
1729 ;; If we have lots of buffers, divide them into groups of 20
1730 ;; and make a pane (or submenu) for each one.
1731 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1732 (let ((alist alist) sublists next
1735 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1736 ;; and make them the next element of sublist.
1737 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1739 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1741 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1745 (nreverse sublists))
1746 ;; Few buffers--put them all in one pane.
1747 (list (cons title alist))))
1749 ;;; These need to be rewritten for the new scroll bar implementation.
1751 ;;;!! ;; Commands for the scroll bar.
1753 ;;;!! (defun mouse-scroll-down (click)
1754 ;;;!! (interactive "@e")
1755 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1757 ;;;!! (defun mouse-scroll-up (click)
1758 ;;;!! (interactive "@e")
1759 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1761 ;;;!! (defun mouse-scroll-down-full ()
1762 ;;;!! (interactive "@")
1763 ;;;!! (scroll-down nil))
1765 ;;;!! (defun mouse-scroll-up-full ()
1766 ;;;!! (interactive "@")
1767 ;;;!! (scroll-up nil))
1769 ;;;!! (defun mouse-scroll-move-cursor (click)
1770 ;;;!! (interactive "@e")
1771 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1773 ;;;!! (defun mouse-scroll-absolute (event)
1774 ;;;!! (interactive "@e")
1775 ;;;!! (let* ((pos (car event))
1776 ;;;!! (position (car pos))
1777 ;;;!! (length (car (cdr pos))))
1778 ;;;!! (if (<= length 0) (setq length 1))
1779 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1780 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1783 ;;;!! scale-factor)))
1784 ;;;!! (goto-char newpos)
1785 ;;;!! (recenter '(4)))))
1787 ;;;!! (defun mouse-scroll-left (click)
1788 ;;;!! (interactive "@e")
1789 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1791 ;;;!! (defun mouse-scroll-right (click)
1792 ;;;!! (interactive "@e")
1793 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1795 ;;;!! (defun mouse-scroll-left-full ()
1796 ;;;!! (interactive "@")
1797 ;;;!! (scroll-left nil))
1799 ;;;!! (defun mouse-scroll-right-full ()
1800 ;;;!! (interactive "@")
1801 ;;;!! (scroll-right nil))
1803 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1804 ;;;!! (interactive "@e")
1805 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1807 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1808 ;;;!! (interactive "@e")
1809 ;;;!! (let* ((pos (car event))
1810 ;;;!! (position (car pos))
1811 ;;;!! (length (car (cdr pos))))
1812 ;;;!! (set-window-hscroll (selected-window) 33)))
1814 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1815 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1816 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1818 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1819 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1820 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1822 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1823 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1824 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1826 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1827 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1828 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1830 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1831 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1832 ;;;!! 'mouse-scroll-absolute-horizontally)
1833 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1835 ;;;!! (global-set-key [horizontal-slider mouse-1]
1836 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1837 ;;;!! (global-set-key [horizontal-slider mouse-2]
1838 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1839 ;;;!! (global-set-key [horizontal-slider mouse-3]
1840 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1842 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1843 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1844 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1846 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1847 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1848 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1850 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1851 ;;;!! 'mouse-split-window-horizontally)
1852 ;;;!! (global-set-key [mode-line S-mouse-2]
1853 ;;;!! 'mouse-split-window-horizontally)
1854 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1855 ;;;!! 'mouse-split-window)
1858 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1859 ;;;!! ;;;; are of the form:
1860 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1863 ;;;!! ;;;; Dynamically track mouse coordinates
1866 ;;;!! ;;(defun track-mouse (event)
1867 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1868 ;;;!! ;; (interactive "@e")
1869 ;;;!! ;; (while mouse-grabbed
1870 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1871 ;;;!! ;; (abs-x (car pos))
1872 ;;;!! ;; (abs-y (cdr pos))
1873 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1874 ;;;!! ;; (list (car pos) (cdr pos))
1875 ;;;!! ;; (selected-window))))
1876 ;;;!! ;; (if (consp relative-coordinate)
1877 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1878 ;;;!! ;; (car relative-coordinate)
1879 ;;;!! ;; (car (cdr relative-coordinate)))
1880 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1883 ;;;!! ;; Dynamically put a box around the line indicated by point
1886 ;;;!! ;;(require 'backquote)
1888 ;;;!! ;;(defun mouse-select-buffer-line (event)
1889 ;;;!! ;; (interactive "@e")
1890 ;;;!! ;; (let ((relative-coordinate
1891 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1892 ;;;!! ;; (abs-y (car (cdr (car event)))))
1893 ;;;!! ;; (if (consp relative-coordinate)
1895 ;;;!! ;; (save-excursion
1896 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1897 ;;;!! ;; (x-draw-rectangle
1898 ;;;!! ;; (selected-screen)
1900 ;;;!! ;; (save-excursion
1901 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1902 ;;;!! ;; (end-of-line)
1903 ;;;!! ;; (push-mark nil t)
1904 ;;;!! ;; (beginning-of-line)
1905 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1906 ;;;!! ;; (sit-for 1)
1907 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1909 ;;;!! ;;(defvar last-line-drawn nil)
1910 ;;;!! ;;(defvar begin-delim "[^ \t]")
1911 ;;;!! ;;(defvar end-delim "[^ \t]")
1913 ;;;!! ;;(defun mouse-boxing (event)
1914 ;;;!! ;; (interactive "@e")
1915 ;;;!! ;; (save-excursion
1916 ;;;!! ;; (let ((screen (selected-screen)))
1917 ;;;!! ;; (while (= (x-mouse-events) 0)
1918 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1919 ;;;!! ;; (abs-x (car pos))
1920 ;;;!! ;; (abs-y (cdr pos))
1921 ;;;!! ;; (relative-coordinate
1922 ;;;!! ;; (coordinates-in-window-p `(,abs-x ,abs-y)
1923 ;;;!! ;; (selected-window)))
1924 ;;;!! ;; (begin-reg nil)
1925 ;;;!! ;; (end-reg nil)
1926 ;;;!! ;; (end-column nil)
1927 ;;;!! ;; (begin-column nil))
1928 ;;;!! ;; (if (and (consp relative-coordinate)
1929 ;;;!! ;; (or (not last-line-drawn)
1930 ;;;!! ;; (not (= last-line-drawn abs-y))))
1932 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1933 ;;;!! ;; (if (= (following-char) 10)
1936 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1937 ;;;!! ;; (setq begin-column (1- (current-column)))
1938 ;;;!! ;; (end-of-line)
1939 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1940 ;;;!! ;; (setq end-column (1+ (current-column)))
1941 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1942 ;;;!! ;; (x-draw-rectangle screen
1943 ;;;!! ;; (setq last-line-drawn abs-y)
1944 ;;;!! ;; begin-column
1945 ;;;!! ;; (- end-column begin-column) 1))))))))))
1947 ;;;!! ;;(defun mouse-erase-box ()
1948 ;;;!! ;; (interactive)
1949 ;;;!! ;; (if last-line-drawn
1951 ;;;!! ;; (x-erase-rectangle (selected-screen))
1952 ;;;!! ;; (setq last-line-drawn nil))))
1954 ;;;!! ;;; (defun test-x-rectangle ()
1955 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1956 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1957 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1960 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1963 ;;;!! (defvar double-start nil)
1964 ;;;!! (defconst double-click-interval 300
1965 ;;;!! "Max ticks between clicks")
1967 ;;;!! (defun double-down (event)
1968 ;;;!! (interactive "@e")
1969 ;;;!! (if double-start
1970 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1971 ;;;!! (if (< interval double-click-interval)
1973 ;;;!! (backward-up-list 1)
1974 ;;;!! ;; (message "Interval %d" interval)
1975 ;;;!! (sleep-for 1)))
1976 ;;;!! (setq double-start nil))
1977 ;;;!! (setq double-start (nth 4 event))))
1979 ;;;!! (defun double-up (event)
1980 ;;;!! (interactive "@e")
1981 ;;;!! (and double-start
1982 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1983 ;;;!! (setq double-start nil)))
1985 ;;;!! ;;; (defun x-test-doubleclick ()
1986 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1987 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1988 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1991 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1994 ;;;!! (defvar scrolled-lines 0)
1995 ;;;!! (defconst scroll-speed 1)
1997 ;;;!! (defun incr-scroll-down (event)
1998 ;;;!! (interactive "@e")
1999 ;;;!! (setq scrolled-lines 0)
2000 ;;;!! (incremental-scroll scroll-speed))
2002 ;;;!! (defun incr-scroll-up (event)
2003 ;;;!! (interactive "@e")
2004 ;;;!! (setq scrolled-lines 0)
2005 ;;;!! (incremental-scroll (- scroll-speed)))
2007 ;;;!! (defun incremental-scroll (n)
2008 ;;;!! (while (= (x-mouse-events) 0)
2009 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
2010 ;;;!! (scroll-down n)
2011 ;;;!! (sit-for 300 t)))
2013 ;;;!! (defun incr-scroll-stop (event)
2014 ;;;!! (interactive "@e")
2015 ;;;!! (message "Scrolled %d lines" scrolled-lines)
2016 ;;;!! (setq scrolled-lines 0)
2017 ;;;!! (sleep-for 1))
2019 ;;;!! ;;; (defun x-testing-scroll ()
2020 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
2021 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
2022 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
2023 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
2024 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
2027 ;;;!! ;; Some playthings suitable for picture mode? They need work.
2030 ;;;!! (defun mouse-kill-rectangle (event)
2031 ;;;!! "Kill the rectangle between point and the mouse cursor."
2032 ;;;!! (interactive "@e")
2033 ;;;!! (let ((point-save (point)))
2034 ;;;!! (save-excursion
2035 ;;;!! (mouse-set-point event)
2036 ;;;!! (push-mark nil t)
2037 ;;;!! (if (> point-save (point))
2038 ;;;!! (kill-rectangle (point) point-save)
2039 ;;;!! (kill-rectangle point-save (point))))))
2041 ;;;!! (defun mouse-open-rectangle (event)
2042 ;;;!! "Kill the rectangle between point and the mouse cursor."
2043 ;;;!! (interactive "@e")
2044 ;;;!! (let ((point-save (point)))
2045 ;;;!! (save-excursion
2046 ;;;!! (mouse-set-point event)
2047 ;;;!! (push-mark nil t)
2048 ;;;!! (if (> point-save (point))
2049 ;;;!! (open-rectangle (point) point-save)
2050 ;;;!! (open-rectangle point-save (point))))))
2052 ;;;!! ;; Must be a better way to do this.
2054 ;;;!! (defun mouse-multiple-insert (n char)
2055 ;;;!! (while (> n 0)
2057 ;;;!! (setq n (1- n))))
2059 ;;;!! ;; What this could do is not finalize until button was released.
2061 ;;;!! (defun mouse-move-text (event)
2062 ;;;!! "Move text from point to cursor position, inserting spaces."
2063 ;;;!! (interactive "@e")
2064 ;;;!! (let* ((relative-coordinate
2065 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
2066 ;;;!! (if (consp relative-coordinate)
2067 ;;;!! (cond ((> (current-column) (car relative-coordinate))
2069 ;;;!! (- (car relative-coordinate) (current-column))))
2070 ;;;!! ((< (current-column) (car relative-coordinate))
2071 ;;;!! (mouse-multiple-insert
2072 ;;;!! (- (car relative-coordinate) (current-column)) " "))
2073 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
2075 ;; Choose a completion with the mouse.
2077 (defun mouse-choose-completion (event)
2078 "Click on an alternative in the `*Completions*' buffer to choose it."
2080 ;; Give temporary modes such as isearch a chance to turn off.
2081 (run-hooks 'mouse-leave-buffer-hook)
2082 (let ((buffer (window-buffer))
2086 (set-buffer (window-buffer (posn-window (event-start event))))
2087 (if completion-reference-buffer
2088 (setq buffer completion-reference-buffer))
2089 (setq base-size completion-base-size)
2091 (goto-char (posn-point (event-start event)))
2093 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2094 (setq end (point) beg (1+ (point))))
2096 (error "No completion here"))
2097 (setq beg (previous-single-property-change beg 'mouse-face))
2098 (setq end (or (next-single-property-change end 'mouse-face)
2100 (setq choice (buffer-substring beg end)))))
2101 (let ((owindow (selected-window)))
2102 (select-window (posn-window (event-start event)))
2103 (if (and (one-window-p t 'selected-frame)
2104 (window-dedicated-p (selected-window)))
2105 ;; This is a special buffer's frame
2106 (iconify-frame (selected-frame))
2107 (or (window-dedicated-p (selected-window))
2109 (select-window owindow))
2110 (choose-completion-string choice buffer base-size)))
2114 (defun font-menu-add-default ()
2115 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2116 (font-alist x-fixed-font-alist)
2117 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2118 (if (assoc "Default" elt)
2119 (delete (assoc "Default" elt) elt))
2121 (cons (list "Default" default)
2124 (defvar x-fixed-font-alist
2127 ;; For these, we specify the pixel height and width.
2129 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2131 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2133 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2134 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2135 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2136 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2137 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2138 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2139 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2140 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2143 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2145 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2147 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2149 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2151 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2153 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2155 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2156 ;;; We don't seem to have these; who knows what they are.
2157 ;;; ("fg-18" "fg-18")
2158 ;;; ("fg-25" "fg-25")
2159 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2160 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2161 ("lucidasanstypewriter-bold-24"
2162 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2163 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2164 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2167 ;; For these, we specify the point height.
2168 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2169 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2170 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2171 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2172 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2173 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2174 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2175 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2176 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2177 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2178 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2179 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2180 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2181 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2182 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2183 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2184 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2185 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2186 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2187 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2188 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2189 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2190 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2191 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2193 "X fonts suitable for use in Emacs.")
2195 (defun mouse-set-font (&rest fonts)
2196 "Select an emacs font from a list of known good fonts and fontsets."
2198 (progn (unless (display-multi-font-p)
2199 (error "Cannot change fonts on this display"))
2202 ;; Append list of fontsets currently defined.
2203 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2209 (set-default-font (car fonts))
2210 (setq font (car fonts))
2213 (setq fonts (cdr fonts)))))
2215 (error "Font not found")))))
2217 ;;; Bindings for mouse commands.
2219 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2220 (global-set-key [mouse-1] 'mouse-set-point)
2221 (global-set-key [drag-mouse-1] 'mouse-set-region)
2223 ;; These are tested for in mouse-drag-region.
2224 (global-set-key [double-mouse-1] 'mouse-set-point)
2225 (global-set-key [triple-mouse-1] 'mouse-set-point)
2227 ;; Clicking on the fringes causes hscrolling:
2228 (global-set-key [left-fringe mouse-1] 'mouse-set-point)
2229 (global-set-key [right-fringe mouse-1] 'mouse-set-point)
2231 (global-set-key [mouse-2] 'mouse-yank-at-click)
2232 (global-set-key [mouse-3] 'mouse-save-then-kill)
2234 ;; By binding these to down-going events, we let the user use the up-going
2235 ;; event to make the selection, saving a click.
2236 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2237 (if (not (eq system-type 'ms-dos))
2238 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2239 ;; C-down-mouse-2 is bound in facemenu.el.
2240 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2243 ;; Replaced with dragging mouse-1
2244 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2246 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2247 ;; vertical-line prevents Emacs from signaling an error when the mouse
2248 ;; button is released after dragging these lines, on non-toolkit
2250 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2251 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2252 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2253 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2254 (global-set-key [header-line mouse-1] 'mouse-select-window)
2255 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2256 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2257 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2258 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2259 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2260 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2261 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2265 ;; This file contains the functionality of the old mldrag.el.
2266 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2267 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2268 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2269 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2272 ;;; arch-tag: 9a710ce1-914a-4923-9b81-697f7bf82ab3
2273 ;;; mouse.el ends here