1 ;;; mouse.el --- window system-independent mouse support
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001
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 ;; Provide a mode-specific menu on a mouse button.
48 (defun popup-menu (menu &optional position prefix
)
49 "Popup the given menu and call the selected option.
50 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
52 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
53 the current mouse position.
54 PREFIX is the prefix argument (if any) to pass to the command."
57 ((and (listp menu
) (keymapp (car menu
))) menu
)
58 (t (let* ((map (easy-menu-create-menu (car menu
) (cdr menu
)))
59 (filter (when (symbolp map
)
60 (plist-get (get map
'menu-prop
) :filter
))))
61 (if filter
(funcall filter
(symbol-function map
)) map
)))))
64 (let ((mp (mouse-pixel-position)))
65 (setq position
(list (list (cadr mp
) (cddr mp
)) (car mp
)))))
66 ;; The looping behavior was taken from lmenu's popup-menu-popup
67 (while (and map
(setq event
68 ;; map could be a prefix key, in which case
69 ;; we need to get its function cell
71 (x-popup-menu position
(indirect-function map
))))
72 ;; Strangely x-popup-menu returns a list.
73 ;; mouse-major-mode-menu was using a weird:
74 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
76 (if (and (not (keymapp map
)) (listp map
))
77 ;; We were given a list of keymaps. Search them all
78 ;; in sequence until a first binding is found.
79 (let ((mouse-click (apply 'vector event
))
81 (while (and map
(null binding
))
82 (setq binding
(lookup-key (car map
) mouse-click
))
83 (if (numberp binding
) ; `too long'
87 ;; We were given a single keymap.
88 (lookup-key map
(apply 'vector event
))))
89 ;; Clear out echoing, which perhaps shows a prefix arg.
91 ;; Maybe try again but with the submap.
92 (setq map
(if (keymapp cmd
) cmd
)))
94 (setq prefix-arg prefix
)
95 ;; `setup-specified-language-environment', for instance,
96 ;; expects this to be set from a menu keymap.
97 (setq last-command-event
(car (last event
)))
98 ;; mouse-major-mode-menu was using `command-execute' instead.
99 (call-interactively cmd
))))
101 (defvar mouse-major-mode-menu-prefix
) ; dynamically bound
103 (defun mouse-major-mode-menu (event prefix
)
104 "Pop up a mode-specific menu of mouse commands.
105 Default to the Edit menu if the major mode doesn't define a menu."
106 ;; Switch to the window clicked on, because otherwise
107 ;; the mode's commands may not make sense.
108 (interactive "@e\nP")
109 ;; Let the mode update its menus first.
110 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
111 (let* (;; This is where mouse-major-mode-menu-prefix
112 ;; returns the prefix we should use (after menu-bar).
113 ;; It is either nil or (SOME-SYMBOL).
114 (mouse-major-mode-menu-prefix nil
)
115 ;; Keymap from which to inherit; may be null.
116 (ancestor (mouse-major-mode-menu-1
117 (and (current-local-map)
118 (local-key-binding [menu-bar
]))))
119 ;; Make a keymap in which our last command leads to a menu or
120 ;; default to the edit menu.
122 (make-sparse-keymap (concat mode-name
" Mode"))
126 ;; Make our menu inherit from the desired keymap which we want
127 ;; to display as the menu now.
128 (set-keymap-parent newmap ancestor
))
129 (popup-menu newmap event prefix
)))
132 ;; Compute and cache the equivalent keys in MENU and all its submenus.
133 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
134 ;;; (and (eq (car menu) 'keymap)
135 ;;; (x-popup-menu nil menu))
137 ;;; (and (consp (car menu))
138 ;;; (consp (cdr (car menu)))
139 ;;; (let ((tail (cdr (car menu))))
140 ;;; (while (and (consp tail)
141 ;;; (not (eq (car tail) 'keymap)))
142 ;;; (setq tail (cdr tail)))
144 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
145 ;;; (setq menu (cdr menu))))
147 ;; Given a mode's menu bar keymap,
148 ;; if it defines exactly one menu bar menu,
149 ;; return just that menu.
150 ;; Otherwise return a menu for all of them.
151 (defun mouse-major-mode-menu-1 (menubar)
156 (if (consp (car tail
))
159 (setq submap
(car tail
))))
160 (setq tail
(cdr tail
)))
163 (setq mouse-major-mode-menu-prefix
(list (car submap
)))
164 (lookup-key menubar
(vector (car submap
)))))))
166 (defun mouse-popup-menubar (event prefix
)
167 "Pops up a menu equiavlent to the menu bar a keyboard EVENT with PREFIX.
168 The contents are the items that would be in the menu bar whether or
169 not it is actually displayed."
170 (interactive "@e \nP")
171 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
172 (let* ((local-menu (and (current-local-map)
173 (lookup-key (current-local-map) [menu-bar
])))
174 (global-menu (lookup-key global-map
[menu-bar
]))
175 ;; If a keymap doesn't have a prompt string (a lazy
176 ;; programmer didn't bother to provide one), create it and
177 ;; insert it into the keymap; each keymap gets its own
178 ;; prompt. This is required for non-toolkit versions to
179 ;; display non-empty menu pane names.
184 (let* ((minor-mode (car menu
))
186 (title-or-map (cadr menu
)))
187 (or (stringp title-or-map
)
191 (capitalize (subst-char-in-string
197 (minor-mode-key-binding [menu-bar
])))
198 (local-title-or-map (and local-menu
(cadr local-menu
)))
199 (global-title-or-map (cadr global-menu
)))
200 (or (null local-menu
)
201 (stringp local-title-or-map
)
202 (setq local-menu
(cons 'keymap
203 (cons (concat mode-name
" Mode Menu")
205 (or (stringp global-title-or-map
)
206 (setq global-menu
(cons 'keymap
208 (cdr global-menu
)))))
209 ;; Supplying the list is faster than making a new map.
210 (popup-menu (append (list global-menu
)
216 (defun mouse-popup-menubar-stuff (event prefix
)
217 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
218 Use the former if the menu bar is showing, otherwise the latter."
219 (interactive "@e \nP")
220 (if (zerop (assoc-default 'menu-bar-lines
(frame-parameters) 'eq
0))
221 (mouse-popup-menubar event prefix
)
222 (mouse-major-mode-menu event prefix
)))
224 ;; Commands that operate on windows.
226 (defun mouse-minibuffer-check (event)
227 (let ((w (posn-window (event-start event
))))
228 (and (window-minibuffer-p w
)
229 (not (minibuffer-window-active-p w
))
230 (error "Minibuffer window is not active")))
231 ;; Give temporary modes such as isearch a chance to turn off.
232 (run-hooks 'mouse-leave-buffer-hook
))
234 (defun mouse-delete-window (click)
235 "Delete the window you click on.
236 Do nothing if the frame has just one window.
237 This command must be bound to a mouse click."
239 (unless (one-window-p t
)
240 (mouse-minibuffer-check click
)
241 (delete-window (posn-window (event-start click
)))))
243 (defun mouse-select-window (click)
244 "Select the window clicked on; don't move point."
246 (mouse-minibuffer-check click
)
247 (let ((oframe (selected-frame))
248 (frame (window-frame (posn-window (event-start click
)))))
249 (select-window (posn-window (event-start click
)))
252 (or (eq frame oframe
)
253 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
255 (defun mouse-tear-off-window (click)
256 "Delete the window clicked on, and create a new frame displaying its buffer."
258 (mouse-minibuffer-check click
)
259 (let* ((window (posn-window (event-start click
)))
260 (buf (window-buffer window
))
261 (frame (make-frame)))
263 (switch-to-buffer buf
)
264 (delete-window window
)))
266 (defun mouse-delete-other-windows ()
267 "Delete all windows except the one you click on."
269 (delete-other-windows))
271 (defun mouse-split-window-vertically (click)
272 "Select Emacs window mouse is on, then split it vertically in half.
273 The window is split at the line clicked on.
274 This command must be bound to a mouse click."
276 (mouse-minibuffer-check click
)
277 (let ((start (event-start click
)))
278 (select-window (posn-window start
))
279 (let ((new-height (1+ (cdr (posn-col-row (event-end click
)))))
280 (first-line window-min-height
)
281 (last-line (- (window-height) window-min-height
)))
282 (if (< last-line first-line
)
283 (error "Window too short to split")
284 (split-window-vertically
285 (min (max new-height first-line
) last-line
))))))
287 (defun mouse-split-window-horizontally (click)
288 "Select Emacs window mouse is on, then split it horizontally in half.
289 The window is split at the column clicked on.
290 This command must be bound to a mouse click."
292 (mouse-minibuffer-check click
)
293 (let ((start (event-start click
)))
294 (select-window (posn-window start
))
295 (let ((new-width (1+ (car (posn-col-row (event-end click
)))))
296 (first-col window-min-width
)
297 (last-col (- (window-width) window-min-width
)))
298 (if (< last-col first-col
)
299 (error "Window too narrow to split")
300 (split-window-horizontally
301 (min (max new-width first-col
) last-col
))))))
303 (defun mouse-drag-mode-line-1 (start-event mode-line-p
)
304 "Change the height of a window by dragging on the mode or header line.
305 START-EVENT is the starting mouse-event of the drag action.
306 MODE-LINE-P non-nil means a mode line is dragged."
307 ;; Give temporary modes such as isearch a chance to turn off.
308 (run-hooks 'mouse-leave-buffer-hook
)
311 (start (event-start start-event
))
312 (start-event-window (posn-window start
))
313 (start-event-frame (window-frame start-event-window
))
314 (start-nwindows (count-windows t
))
315 (old-selected-window (selected-window))
316 (minibuffer (frame-parameter nil
'minibuffer
))
317 should-enlarge-minibuffer event mouse y top bot edges wconfig growth
)
320 ;; enlarge-window only works on the selected window, so
321 ;; we must select the window where the start event originated.
322 ;; unwind-protect will restore the old selected window later.
323 (select-window start-event-window
)
325 ;; if this is the bottommost ordinary window, then to
326 ;; move its modeline the minibuffer must be enlarged.
327 (setq should-enlarge-minibuffer
330 (not (one-window-p t
))
331 (= (nth 1 (window-edges minibuffer
))
332 (nth 3 (window-edges)))))
334 ;; loop reading events and sampling the position of
337 (setq event
(read-event)
338 mouse
(mouse-position))
341 ;; - there is a switch-frame event.
342 ;; - the mouse isn't in the frame that we started in
343 ;; - the mouse isn't in any Emacs frame
345 ;; - there is a mouse-movement event
346 ;; - there is a scroll-bar-movement event
347 ;; (same as mouse movement for our purposes)
349 ;; - there is a keyboard event or some other unknown event
351 (cond ((integerp event
)
354 ((eq (car event
) 'switch-frame
)
357 ((not (memq (car event
) '(mouse-movement scroll-bar-movement
)))
359 (push event unread-command-events
))
362 ((not (eq (car mouse
) start-event-frame
))
365 ((null (car (cdr mouse
)))
369 (setq y
(cdr (cdr mouse
))
374 ;; compute size change needed
376 ;; Scale back a move that would make the
378 (when (< (- y top -
1) window-min-height
)
379 (setq y
(+ top window-min-height -
1)))
380 (setq growth
(- y bot -
1)))
382 (when (< (- bot y
) window-min-height
)
383 (setq y
(- bot window-min-height
)))
384 ;; The window's top includes the header line!
385 (setq growth
(- top y
))))
386 (setq wconfig
(current-window-configuration))
388 ;; Check for an error case.
389 (when (and (/= growth
0)
392 (error "Attempt to resize sole window"))
394 ;; grow/shrink minibuffer?
395 (if should-enlarge-minibuffer
397 ;; yes. briefly select minibuffer so
398 ;; enlarge-window will affect the
400 (select-window minibuffer
)
401 ;; scale back shrinkage if it would
402 ;; make the minibuffer less than 1
404 (if (and (> growth
0)
405 (< (- (window-height minibuffer
)
408 (setq growth
(1- (window-height minibuffer
))))
409 (enlarge-window (- growth
))
410 (select-window start-event-window
))
411 ;; no. grow/shrink the selected window
412 ;(message "growth = %d" growth)
413 (enlarge-window growth
))
415 ;; if this window's growth caused another
416 ;; window to be deleted because it was too
417 ;; short, rescind the change.
419 ;; if size change caused space to be stolen
420 ;; from a window above this one, rescind the
421 ;; change, but only if we didn't grow/shrink
422 ;; the minibuffer. minibuffer size changes
423 ;; can cause all windows to shrink... no way
425 (when (or (/= start-nwindows
(count-windows t
))
426 (and (not should-enlarge-minibuffer
)
428 (/= top
(nth 1 (window-edges)))))
429 (set-window-configuration wconfig
)))))))))
431 (defun mouse-drag-mode-line (start-event)
432 "Change the height of a window by dragging on the mode line."
434 (mouse-drag-mode-line-1 start-event t
))
436 (defun mouse-drag-header-line (start-event)
437 "Change the height of a window by dragging on the header line.
438 Windows whose header-lines are at the top of the frame cannot be
439 resized by dragging their header-line."
441 ;; Changing the window's size by dragging its header-line when the
442 ;; header-line is at the top of the frame is somewhat strange,
443 ;; because the header-line doesn't move, so don't do it.
444 (let* ((start (event-start start-event
))
445 (window (posn-window start
))
446 (frame (window-frame window
))
447 (first-window (frame-first-window frame
)))
448 (when (or (eq window first-window
)
449 (= (nth 1 (window-edges window
))
450 (nth 1 (window-edges first-window
))))
451 (error "Cannot move header-line at the top of the frame"))
452 (mouse-drag-mode-line-1 start-event nil
)))
455 (defun mouse-drag-vertical-line (start-event)
456 "Change the width of a window by dragging on the vertical line."
458 ;; Give temporary modes such as isearch a chance to turn off.
459 (run-hooks 'mouse-leave-buffer-hook
)
462 (start-event-frame (window-frame (car (car (cdr start-event
)))))
463 (start-event-window (car (car (cdr start-event
))))
464 (start-nwindows (count-windows t
))
465 (old-selected-window (selected-window))
466 event mouse x left right edges wconfig growth
468 (or (cdr (assq 'vertical-scroll-bars
(frame-parameters start-event-frame
)))
471 (error "Attempt to resize sole ordinary window"))
472 (if (eq which-side
'right
)
473 (if (= (nth 2 (window-edges start-event-window
))
474 (frame-width start-event-frame
))
475 (error "Attempt to drag rightmost scrollbar"))
476 (if (= (nth 0 (window-edges start-event-window
)) 0)
477 (error "Attempt to drag leftmost scrollbar")))
480 ;; enlarge-window only works on the selected window, so
481 ;; we must select the window where the start event originated.
482 ;; unwind-protect will restore the old selected window later.
483 (select-window start-event-window
)
484 ;; loop reading events and sampling the position of
487 (setq event
(read-event)
488 mouse
(mouse-position))
490 ;; - there is a switch-frame event.
491 ;; - the mouse isn't in the frame that we started in
492 ;; - the mouse isn't in any Emacs frame
494 ;; - there is a mouse-movement event
495 ;; - there is a scroll-bar-movement event
496 ;; (same as mouse movement for our purposes)
498 ;; - there is a keyboard event or some other unknown event
500 (cond ((integerp event
)
502 ((eq (car event
) 'switch-frame
)
504 ((not (memq (car event
)
505 '(mouse-movement scroll-bar-movement
)))
507 (setq unread-command-events
508 (cons event unread-command-events
)))
510 ((not (eq (car mouse
) start-event-frame
))
512 ((null (car (cdr mouse
)))
515 (save-selected-window
516 ;; If the scroll bar is on the window's left,
517 ;; adjust the window on the left.
518 (unless (eq which-side
'right
)
519 (select-window (previous-window)))
520 (setq x
(- (car (cdr mouse
))
521 (if (eq which-side
'right
) 0 2))
525 ;; scale back a move that would make the
527 (if (< (- x left -
1) window-min-width
)
528 (setq x
(+ left window-min-width -
1)))
529 ;; compute size change needed
530 (setq growth
(- x right -
1)
531 wconfig
(current-window-configuration))
532 (enlarge-window growth t
)
533 ;; if this window's growth caused another
534 ;; window to be deleted because it was too
535 ;; thin, rescind the change.
537 ;; if size change caused space to be stolen
538 ;; from a window to the left of this one,
539 ;; rescind the change.
540 (if (or (/= start-nwindows
(count-windows t
))
541 (/= left
(nth 0 (window-edges))))
542 (set-window-configuration wconfig
))))))))))
544 (defun mouse-set-point (event)
545 "Move point to the position clicked on with the mouse.
546 This should be bound to a mouse click event type."
548 (mouse-minibuffer-check event
)
549 ;; Use event-end in case called from mouse-drag-region.
550 ;; If EVENT is a click, event-end and event-start give same value.
551 (let ((posn (event-end event
)))
552 (if (not (windowp (posn-window posn
)))
553 (error "Cursor not in text area of window"))
554 (select-window (posn-window posn
))
555 (if (numberp (posn-point posn
))
556 (goto-char (posn-point posn
)))))
558 (defvar mouse-last-region-beg nil
)
559 (defvar mouse-last-region-end nil
)
560 (defvar mouse-last-region-tick nil
)
562 (defun mouse-region-match ()
563 "Return non-nil if there's an active region that was set with the mouse."
564 (and (mark t
) mark-active
565 (eq mouse-last-region-beg
(region-beginning))
566 (eq mouse-last-region-end
(region-end))
567 (eq mouse-last-region-tick
(buffer-modified-tick))))
569 (defun mouse-set-region (click)
570 "Set the region to the text dragged over, and copy to kill ring.
571 This should be bound to a mouse drag event."
573 (mouse-minibuffer-check click
)
574 (let ((posn (event-start click
))
575 (end (event-end click
)))
576 (select-window (posn-window posn
))
577 (if (numberp (posn-point posn
))
578 (goto-char (posn-point posn
)))
579 ;; If mark is highlighted, no need to bounce the cursor.
580 ;; On X, we highlight while dragging, thus once again no need to bounce.
581 (or transient-mark-mode
582 (memq (framep (selected-frame)) '(x pc w32
))
586 (if (numberp (posn-point end
))
587 (goto-char (posn-point end
)))
588 ;; Don't set this-command to kill-region, so that a following
589 ;; C-w will not double the text in the kill ring.
590 ;; Ignore last-command so we don't append to a preceding kill.
591 (let (this-command last-command deactivate-mark
)
592 (copy-region-as-kill (mark) (point)))
593 (mouse-set-region-1)))
595 (defun mouse-set-region-1 ()
596 (setq mouse-last-region-beg
(region-beginning))
597 (setq mouse-last-region-end
(region-end))
598 (setq mouse-last-region-tick
(buffer-modified-tick)))
600 (defcustom mouse-scroll-delay
0.25
601 "*The pause between scroll steps caused by mouse drags, in seconds.
602 If you drag the mouse beyond the edge of a window, Emacs scrolls the
603 window to bring the text beyond that edge into view, with a delay of
604 this many seconds between scroll steps. Scrolling stops when you move
605 the mouse back into the window, or release the button.
606 This variable's value may be non-integral.
607 Setting this to zero causes Emacs to scroll as fast as it can."
611 (defcustom mouse-scroll-min-lines
1
612 "*The minimum number of lines scrolled by dragging mouse out of window.
613 Moving the mouse out the top or bottom edge of the window begins
614 scrolling repeatedly. The number of lines scrolled per repetition
615 is normally equal to the number of lines beyond the window edge that
616 the mouse has moved. However, it always scrolls at least the number
617 of lines specified by this variable."
621 (defun mouse-scroll-subr (window jump
&optional overlay start
)
622 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
623 If OVERLAY is an overlay, let it stretch from START to the far edge of
624 the newly visible text.
625 Upon exit, point is at the far edge of the newly visible text."
627 ((and (> jump
0) (< jump mouse-scroll-min-lines
))
628 (setq jump mouse-scroll-min-lines
))
629 ((and (< jump
0) (< (- jump
) mouse-scroll-min-lines
))
630 (setq jump
(- mouse-scroll-min-lines
))))
631 (let ((opoint (point)))
633 (goto-char (window-start window
))
634 (if (not (zerop (vertical-motion jump window
)))
636 (set-window-start window
(point))
638 (if (window-end window
)
640 (goto-char (window-end window
))
641 ;; window-end doesn't reflect the window's new
642 ;; start position until the next redisplay.
643 (vertical-motion (1- jump
) window
))
644 (vertical-motion (- (window-height window
) 2)))
645 (goto-char (window-start window
)))
647 (move-overlay overlay start
(point)))
648 ;; Now that we have scrolled WINDOW properly,
649 ;; put point back where it was for the redisplay
650 ;; so that we don't mess up the selected window.
651 (or (eq window
(selected-window))
653 (sit-for mouse-scroll-delay
)))))
654 (or (eq window
(selected-window))
655 (goto-char opoint
))))
657 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
658 (defvar mouse-drag-overlay
(make-overlay 1 1))
659 (delete-overlay mouse-drag-overlay
)
660 (overlay-put mouse-drag-overlay
'face
'region
)
662 (defvar mouse-selection-click-count
0)
664 (defvar mouse-selection-click-count-buffer nil
)
666 (defun mouse-drag-region (start-event)
667 "Set the region to the text that the mouse is dragged over.
668 Highlight the drag area as you move the mouse.
669 This must be bound to a button-down mouse event.
670 In Transient Mark mode, the highlighting remains as long as the mark
671 remains active. Otherwise, it remains until the next input event."
673 (mouse-minibuffer-check start-event
)
674 (let* ((echo-keystrokes 0)
675 (start-posn (event-start start-event
))
676 (start-point (posn-point start-posn
))
677 (start-window (posn-window start-posn
))
678 (start-frame (window-frame start-window
))
679 (start-hscroll (window-hscroll start-window
))
680 (bounds (window-edges start-window
))
682 (bottom (if (window-minibuffer-p start-window
)
684 ;; Don't count the mode line.
685 (1- (nth 3 bounds
))))
686 (click-count (1- (event-click-count start-event
))))
687 (setq mouse-selection-click-count click-count
)
688 (setq mouse-selection-click-count-buffer
(current-buffer))
689 (mouse-set-point start-event
)
690 ;; In case the down click is in the middle of some intangible text,
691 ;; use the end of that text, and put it in START-POINT.
692 (if (< (point) start-point
)
693 (goto-char start-point
))
694 (setq start-point
(point))
695 (let ((range (mouse-start-end start-point start-point click-count
)))
696 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
)
697 (window-buffer start-window
)))
699 ;; end-of-range is used only in the single-click case.
700 ;; It is the place where the drag has reached so far
701 ;; (but not outside the window where the drag started).
702 (let (event end end-point last-end-point
(end-of-range (point)))
705 (setq event
(read-event))
706 (or (mouse-movement-p event
)
707 (eq (car-safe event
) 'switch-frame
)))
708 (if (eq (car-safe event
) 'switch-frame
)
710 (setq end
(event-end event
)
711 end-point
(posn-point end
))
712 (if (numberp end-point
)
713 (setq last-end-point end-point
))
716 ;; Are we moving within the original window?
717 ((and (eq (posn-window end
) start-window
)
718 (integer-or-marker-p end-point
))
719 ;; Go to START-POINT first, so that when we move to END-POINT,
720 ;; if it's in the middle of intangible text,
721 ;; point jumps in the direction away from START-POINT.
722 (goto-char start-point
)
723 (goto-char end-point
)
724 (if (zerop (% click-count
3))
725 (setq end-of-range
(point)))
726 (let ((range (mouse-start-end start-point
(point) click-count
)))
727 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
))))
730 (let ((mouse-row (cdr (cdr (mouse-position)))))
734 (mouse-scroll-subr start-window
(- mouse-row top
)
735 mouse-drag-overlay start-point
)
736 ;; Without this, point tends to jump back to the starting
737 ;; position where the mouse button was pressed down.
738 (setq end-of-range
(overlay-start mouse-drag-overlay
)))
739 ((>= mouse-row bottom
)
740 (mouse-scroll-subr start-window
(1+ (- mouse-row bottom
))
741 mouse-drag-overlay start-point
)
742 (setq end-of-range
(overlay-end mouse-drag-overlay
))))))))))
743 ;; In case we did not get a mouse-motion event
744 ;; for the final move of the mouse before a drag event
745 ;; pretend that we did get one.
746 (when (and (memq 'drag
(event-modifiers (car-safe event
)))
747 (setq end
(event-end event
)
748 end-point
(posn-point end
))
749 (eq (posn-window end
) start-window
)
750 (integer-or-marker-p end-point
))
752 ;; Go to START-POINT first, so that when we move to END-POINT,
753 ;; if it's in the middle of intangible text,
754 ;; point jumps in the direction away from START-POINT.
755 (goto-char start-point
)
756 (goto-char end-point
)
757 (if (zerop (% click-count
3))
758 (setq end-of-range
(point)))
759 (let ((range (mouse-start-end start-point
(point) click-count
)))
760 (move-overlay mouse-drag-overlay
(car range
) (nth 1 range
))))
763 (let ((fun (key-binding (vector (car event
)))))
764 ;; Run the binding of the terminating up-event, if possible.
765 ;; In the case of a multiple click, it gives the wrong results,
766 ;; because it would fail to set up a region.
767 (if (not (= (overlay-start mouse-drag-overlay
)
768 (overlay-end mouse-drag-overlay
)))
770 (if (numberp (posn-point (event-end event
)))
771 (posn-point (event-end event
))
773 ;; The end that comes from where we ended the drag.
776 (if (and stop-point
(< stop-point start-point
))
777 (overlay-start mouse-drag-overlay
)
778 (overlay-end mouse-drag-overlay
)))
779 ;; The end that comes from where we started the drag.
782 (- (+ (overlay-end mouse-drag-overlay
)
783 (overlay-start mouse-drag-overlay
))
785 last-command this-command
)
786 (push-mark region-commencement t t
)
787 (goto-char region-termination
)
788 ;; Don't let copy-region-as-kill set deactivate-mark.
789 (let (deactivate-mark)
790 (copy-region-as-kill (point) (mark t
)))
791 (let ((buffer (current-buffer)))
793 ;; mouse-show-mark can call read-event,
794 ;; and that means the Emacs server could switch buffers
795 ;; under us. If that happened,
796 ;; avoid trying to use the region.
797 (and (mark t
) mark-active
798 (eq buffer
(current-buffer))
799 (mouse-set-region-1))))
800 (delete-overlay mouse-drag-overlay
)
801 ;; Run the binding of the terminating up-event.
802 (when (and (functionp fun
)
803 (= start-hscroll
(window-hscroll start-window
)))
804 (setq unread-command-events
805 (cons event unread-command-events
)))))
806 (delete-overlay mouse-drag-overlay
)))))
808 ;; Commands to handle xterm-style multiple clicks.
810 (defun mouse-skip-word (dir)
811 "Skip over word, over whitespace, or over identical punctuation.
812 If DIR is positive skip forward; if negative, skip backward."
813 (let* ((char (following-char))
814 (syntax (char-to-string (char-syntax char
))))
815 (cond ((string= syntax
"w")
816 ;; Here, we can't use skip-syntax-forward/backward because
817 ;; they don't pay attention to word-separating-categories,
818 ;; and thus they will skip over a true word boundary. So,
819 ;; we simularte the original behaviour by using
822 (if (not (looking-at "\\<"))
824 (if (or (looking-at "\\<") (not (looking-at "\\>")))
826 ((string= syntax
" ")
828 (skip-syntax-backward syntax
)
829 (skip-syntax-forward syntax
)))
830 ((string= syntax
"_")
832 (skip-syntax-backward "w_")
833 (skip-syntax-forward "w_")))
835 (while (and (not (bobp)) (= (preceding-char) char
))
838 (while (and (not (eobp)) (= (following-char) char
))
839 (forward-char 1))))))
841 ;; Return a list of region bounds based on START and END according to MODE.
842 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
843 ;; If MODE is 1 then set point to start of word at (min START END),
844 ;; mark to end of word at (max START END).
845 ;; If MODE is 2 then do the same for lines.
846 (defun mouse-start-end (start end mode
)
851 (setq mode
(mod mode
3))
857 (= (char-syntax (char-after start
)) ?\
())
866 (= (char-syntax (char-after start
)) ?\
)))
867 (list (save-excursion
868 (goto-char (1+ start
))
875 (= (char-syntax (char-after start
)) ?
\"))
876 (let ((open (or (eq start
(point-min))
878 (goto-char (- start
1))
879 (looking-at "\\s(\\|\\s \\|\\s>")))))
889 (list (save-excursion
892 (goto-char (1+ start
))
898 (list (save-excursion
907 (list (save-excursion
909 (beginning-of-line 1)
916 ;; Subroutine: set the mark where CLICK happened,
917 ;; but don't do anything else.
918 (defun mouse-set-mark-fast (click)
919 (mouse-minibuffer-check click
)
920 (let ((posn (event-start click
)))
921 (select-window (posn-window posn
))
922 (if (numberp (posn-point posn
))
923 (push-mark (posn-point posn
) t t
))))
925 (defun mouse-undouble-last-event (events)
926 (let* ((index (1- (length events
)))
927 (last (nthcdr index events
))
929 (basic (event-basic-type event
))
930 (old-modifiers (event-modifiers event
))
931 (modifiers (delq 'double
(delq 'triple
(copy-sequence old-modifiers
))))
934 ;; Use reverse, not nreverse, since event-modifiers
935 ;; does not copy the list it returns.
936 (cons (event-convert-list (reverse (cons basic modifiers
)))
940 (if (and (not (equal modifiers old-modifiers
))
941 (key-binding (apply 'vector events
)))
946 ;; Momentarily show where the mark is, if highlighting doesn't show it.
948 (defvar mouse-region-delete-keys
'([delete])
949 "List of keys which shall cause the mouse region to be deleted.")
951 (defun mouse-show-mark ()
952 (if transient-mark-mode
953 (delete-overlay mouse-drag-overlay)
954 (let ((inhibit-quit t)
956 event events key ignore
957 x-lost-selection-hooks)
958 (add-hook 'x-lost-selection-hooks
960 (if (eq seltype 'PRIMARY)
961 (progn (setq ignore t)
962 (throw 'mouse-show-mark t)))))
963 (move-overlay mouse-drag-overlay (point) (mark t))
964 (catch 'mouse-show-mark
965 ;; In this loop, execute scroll bar and switch-frame events.
966 ;; Also ignore down-events that are undefined.
967 (while (progn (setq event (read-event))
968 (setq events (append events (list event)))
969 (setq key (apply 'vector events))
970 (or (and (consp event)
971 (eq (car event) 'switch-frame))
973 (eq (posn-point (event-end event))
974 'vertical-scroll-bar))
975 (and (memq 'down (event-modifiers event))
976 (not (key-binding key))
977 (not (mouse-undouble-last-event events))
978 (not (member key mouse-region-delete-keys)))))
980 (or (eq (car event) 'switch-frame)
981 (eq (posn-point (event-end event))
982 'vertical-scroll-bar))
983 (let ((keys (vector 'vertical-scroll-bar event)))
984 (and (key-binding keys)
986 (call-interactively (key-binding keys)
988 (setq events nil)))))))
989 ;; If we lost the selection, just turn off the highlighting.
992 ;; For certain special keys, delete the region.
993 (if (member key mouse-region-delete-keys)
994 (delete-region (overlay-start mouse-drag-overlay)
995 (overlay-end mouse-drag-overlay))
996 ;; Otherwise, unread the key so it gets executed normally.
997 (setq unread-command-events
998 (nconc events unread-command-events))))
1000 (delete-overlay mouse-drag-overlay))
1002 (goto-char (mark t))
1005 (defun mouse-set-mark (click)
1006 "Set mark at the position clicked on with the mouse.
1007 Display cursor at that position for a second.
1008 This must be bound to a mouse click."
1010 (mouse-minibuffer-check click)
1011 (select-window (posn-window (event-start click)))
1012 ;; We don't use save-excursion because that preserves the mark too.
1013 (let ((point-save (point)))
1015 (progn (mouse-set-point click)
1017 (or transient-mark-mode
1019 (goto-char point-save))))
1021 (defun mouse-kill (click)
1022 "Kill the region between point and the mouse click.
1023 The text is saved in the kill ring, as with \\[kill-region]."
1025 (mouse-minibuffer-check click)
1026 (let* ((posn (event-start click))
1027 (click-posn (posn-point posn)))
1028 (select-window (posn-window posn))
1029 (if (numberp click-posn)
1030 (kill-region (min (point) click-posn)
1031 (max (point) click-posn)))))
1033 (defun mouse-yank-at-click (click arg)
1034 "Insert the last stretch of killed text at the position clicked on.
1035 Also move point to one end of the text thus inserted (normally the end),
1036 and set mark at the beginning..
1037 Prefix arguments are interpreted as with \\[yank].
1038 If `mouse-yank-at-point' is non-nil, insert at point
1039 regardless of where you click."
1040 (interactive "e\nP")
1041 ;; Give temporary modes such as isearch a chance to turn off.
1042 (run-hooks 'mouse-leave-buffer-hook)
1043 (or mouse-yank-at-point (mouse-set-point click))
1044 (setq this-command 'yank)
1045 (setq mouse-selection-click-count 0)
1048 (defun mouse-kill-ring-save (click)
1049 "Copy the region between point and the mouse click in the kill ring.
1050 This does not delete the region; it acts like \\[kill-ring-save]."
1052 (mouse-set-mark-fast click)
1053 (let (this-command last-command)
1054 (kill-ring-save (point) (mark t)))
1057 ;;; This function used to delete the text between point and the mouse
1058 ;;; whenever it was equal to the front of the kill ring, but some
1059 ;;; people found that confusing.
1061 ;;; A list (TEXT START END), describing the text and position of the last
1062 ;;; invocation of mouse-save-then-kill.
1063 (defvar mouse-save-then-kill-posn nil)
1065 (defun mouse-save-then-kill-delete-region (beg end)
1066 ;; We must make our own undo boundaries
1067 ;; because they happen automatically only for the current buffer.
1069 (if (or (= beg end) (eq buffer-undo-list t))
1070 ;; If we have no undo list in this buffer,
1072 (delete-region beg end)
1073 ;; Delete, but make the undo-list entry share with the kill ring.
1074 ;; First, delete just one char, so in case buffer is being modified
1075 ;; for the first time, the undo list records that fact.
1076 (let (before-change-functions after-change-functions)
1078 (+ beg (if (> end beg) 1 -1))))
1079 (let ((buffer-undo-list buffer-undo-list))
1080 ;; Undo that deletion--but don't change the undo list!
1081 (let (before-change-functions after-change-functions)
1082 (primitive-undo 1 buffer-undo-list))
1083 ;; Now delete the rest of the specified region,
1084 ;; but don't record it.
1085 (setq buffer-undo-list t)
1086 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1087 (error "Lossage in mouse-save-then-kill-delete-region"))
1088 (delete-region beg end))
1089 (let ((tail buffer-undo-list))
1090 ;; Search back in buffer-undo-list for the string
1091 ;; that came from deleting one character.
1092 (while (and tail (not (stringp (car (car tail)))))
1093 (setq tail (cdr tail)))
1094 ;; Replace it with an entry for the entire deleted text.
1096 (setcar tail (cons (car kill-ring) (min beg end))))))
1099 (defun mouse-save-then-kill (click)
1100 "Save text to point in kill ring; the second time, kill the text.
1101 If the text between point and the mouse is the same as what's
1102 at the front of the kill ring, this deletes the text.
1103 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1104 which prepares for a second click to delete the text.
1106 If you have selected words or lines, this command extends the
1107 selection through the word or line clicked on. If you do this
1108 again in a different position, it extends the selection again.
1109 If you do this twice in the same position, the selection is killed."
1111 (let ((before-scroll
1112 (with-current-buffer (window-buffer (posn-window (event-start click)))
1113 point-before-scroll)))
1114 (mouse-minibuffer-check click)
1115 (let ((click-posn (posn-point (event-start click)))
1116 ;; Don't let a subsequent kill command append to this one:
1117 ;; prevent setting this-command to kill-region.
1118 (this-command this-command))
1119 (if (and (save-excursion
1120 (set-buffer (window-buffer (posn-window (event-start click))))
1121 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1122 ;; Don't be fooled by a recent click in some other buffer.
1123 (eq mouse-selection-click-count-buffer
1124 (current-buffer)))))
1125 (if (not (and (eq last-command 'mouse-save-then-kill)
1127 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1128 ;; Find both ends of the object selected by this click.
1130 (mouse-start-end click-posn click-posn
1131 mouse-selection-click-count)))
1132 ;; Move whichever end is closer to the click.
1133 ;; That's what xterm does, and it seems reasonable.
1134 (if (< (abs (- click-posn (mark t)))
1135 (abs (- click-posn (point))))
1136 (set-mark (car range))
1137 (goto-char (nth 1 range)))
1138 ;; We have already put the old region in the kill ring.
1139 ;; Replace it with the extended region.
1140 ;; (It would be annoying to make a separate entry.)
1141 (kill-new (buffer-substring (point) (mark t)) t)
1142 (mouse-set-region-1)
1143 ;; Arrange for a repeated mouse-3 to kill this region.
1144 (setq mouse-save-then-kill-posn
1145 (list (car kill-ring) (point) click-posn))
1147 ;; If we click this button again without moving it,
1149 (mouse-save-then-kill-delete-region (mark) (point))
1150 (setq mouse-selection-click-count 0)
1151 (setq mouse-save-then-kill-posn nil))
1152 (if (and (eq last-command 'mouse-save-then-kill)
1153 mouse-save-then-kill-posn
1154 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1155 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1156 ;; If this is the second time we've called
1157 ;; mouse-save-then-kill, delete the text from the buffer.
1159 (mouse-save-then-kill-delete-region (point) (mark))
1160 ;; After we kill, another click counts as "the first time".
1161 (setq mouse-save-then-kill-posn nil))
1162 ;; This is not a repetition.
1163 ;; We are adjusting an old selection or creating a new one.
1164 (if (or (and (eq last-command 'mouse-save-then-kill)
1165 mouse-save-then-kill-posn)
1166 (and mark-active transient-mark-mode)
1167 (and (memq last-command
1168 '(mouse-drag-region mouse-set-region))
1169 (or mark-even-if-inactive
1170 (not transient-mark-mode))))
1171 ;; We have a selection or suitable region, so adjust it.
1172 (let* ((posn (event-start click))
1173 (new (posn-point posn)))
1174 (select-window (posn-window posn))
1177 ;; Move whichever end of the region is closer to the click.
1178 ;; That is what xterm does, and it seems reasonable.
1179 (if (<= (abs (- new (point))) (abs (- new (mark t))))
1182 (setq deactivate-mark nil)))
1183 (kill-new (buffer-substring (point) (mark t)) t)
1185 ;; Set the mark where point is, then move where clicked.
1186 (mouse-set-mark-fast click)
1188 (goto-char before-scroll))
1189 (exchange-point-and-mark)
1190 (kill-new (buffer-substring (point) (mark t)))
1192 (mouse-set-region-1)
1193 (setq mouse-save-then-kill-posn
1194 (list (car kill-ring) (point) click-posn)))))))
1196 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1197 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1198 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1199 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1200 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1202 ;; An overlay which records the current secondary selection
1203 ;; or else is deleted when there is no secondary selection.
1205 (defvar mouse-secondary-overlay nil)
1207 (defvar mouse-secondary-click-count 0)
1209 ;; A marker which records the specified first end for a secondary selection.
1211 (defvar mouse-secondary-start nil)
1213 (defun mouse-start-secondary (click)
1214 "Set one end of the secondary selection to the position clicked on.
1215 Use \\[mouse-secondary-save-then-kill] to set the other end
1216 and complete the secondary selection."
1218 (mouse-minibuffer-check click)
1219 (let ((posn (event-start click)))
1221 (set-buffer (window-buffer (posn-window posn)))
1222 ;; Cancel any preexisting secondary selection.
1223 (if mouse-secondary-overlay
1224 (delete-overlay mouse-secondary-overlay))
1225 (if (numberp (posn-point posn))
1227 (or mouse-secondary-start
1228 (setq mouse-secondary-start (make-marker)))
1229 (move-marker mouse-secondary-start (posn-point posn)))))))
1231 (defun mouse-set-secondary (click)
1232 "Set the secondary selection to the text that the mouse is dragged over.
1233 This must be bound to a mouse drag event."
1235 (mouse-minibuffer-check click)
1236 (let ((posn (event-start click))
1238 (end (event-end click)))
1240 (set-buffer (window-buffer (posn-window posn)))
1241 (if (numberp (posn-point posn))
1242 (setq beg (posn-point posn)))
1243 (if mouse-secondary-overlay
1244 (move-overlay mouse-secondary-overlay beg (posn-point end))
1245 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1246 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1248 (defun mouse-drag-secondary (start-event)
1249 "Set the secondary selection to the text that the mouse is dragged over.
1250 Highlight the drag area as you move the mouse.
1251 This must be bound to a button-down mouse event.
1252 The function returns a non-nil value if it creates a secondary selection."
1254 (mouse-minibuffer-check start-event)
1255 (let* ((echo-keystrokes 0)
1256 (start-posn (event-start start-event))
1257 (start-point (posn-point start-posn))
1258 (start-window (posn-window start-posn))
1259 (start-frame (window-frame start-window))
1260 (bounds (window-edges start-window))
1261 (top (nth 1 bounds))
1262 (bottom (if (window-minibuffer-p start-window)
1264 ;; Don't count the mode line.
1265 (1- (nth 3 bounds))))
1266 (click-count (1- (event-click-count start-event))))
1268 (set-buffer (window-buffer start-window))
1269 (setq mouse-secondary-click-count click-count)
1270 (or mouse-secondary-overlay
1271 (setq mouse-secondary-overlay
1272 (make-overlay (point) (point))))
1273 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1274 (if (> (mod click-count 3) 0)
1275 ;; Double or triple press: make an initial selection
1276 ;; of one word or line.
1277 (let ((range (mouse-start-end start-point start-point click-count)))
1278 (set-marker mouse-secondary-start nil)
1279 (move-overlay mouse-secondary-overlay 1 1
1280 (window-buffer start-window))
1281 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1282 (window-buffer start-window)))
1283 ;; Single-press: cancel any preexisting secondary selection.
1284 (or mouse-secondary-start
1285 (setq mouse-secondary-start (make-marker)))
1286 (set-marker mouse-secondary-start start-point)
1287 (delete-overlay mouse-secondary-overlay))
1288 (let (event end end-point)
1291 (setq event (read-event))
1292 (or (mouse-movement-p event)
1293 (eq (car-safe event) 'switch-frame)))
1295 (if (eq (car-safe event) 'switch-frame)
1297 (setq end (event-end event)
1298 end-point (posn-point end))
1300 ;; Are we moving within the original window?
1301 ((and (eq (posn-window end) start-window)
1302 (integer-or-marker-p end-point))
1303 (let ((range (mouse-start-end start-point end-point
1305 (if (or (/= start-point end-point)
1306 (null (marker-position mouse-secondary-start)))
1308 (set-marker mouse-secondary-start nil)
1309 (move-overlay mouse-secondary-overlay
1310 (car range) (nth 1 range))))))
1312 (let ((mouse-row (cdr (cdr (mouse-position)))))
1316 (mouse-scroll-subr start-window (- mouse-row top)
1317 mouse-secondary-overlay start-point))
1318 ((>= mouse-row bottom)
1319 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1320 mouse-secondary-overlay start-point)))))))))
1323 (if (marker-position mouse-secondary-start)
1324 (save-window-excursion
1325 (delete-overlay mouse-secondary-overlay)
1326 (x-set-selection 'SECONDARY nil)
1327 (select-window start-window)
1329 (goto-char mouse-secondary-start)
1334 (buffer-substring (overlay-start mouse-secondary-overlay)
1335 (overlay-end mouse-secondary-overlay)))))))))
1337 (defun mouse-yank-secondary (click)
1338 "Insert the secondary selection at the position clicked on.
1339 Move point to the end of the inserted text.
1340 If `mouse-yank-at-point' is non-nil, insert at point
1341 regardless of where you click."
1343 ;; Give temporary modes such as isearch a chance to turn off.
1344 (run-hooks 'mouse-leave-buffer-hook)
1345 (or mouse-yank-at-point (mouse-set-point click))
1346 (insert (x-get-selection 'SECONDARY)))
1348 (defun mouse-kill-secondary ()
1349 "Kill the text in the secondary selection.
1350 This is intended more as a keyboard command than as a mouse command
1351 but it can work as either one.
1353 The current buffer (in case of keyboard use), or the buffer clicked on,
1354 must be the one that the secondary selection is in. This requirement
1355 is to prevent accidents."
1357 (let* ((keys (this-command-keys))
1358 (click (elt keys (1- (length keys)))))
1359 (or (eq (overlay-buffer mouse-secondary-overlay)
1361 (window-buffer (posn-window (event-start click)))
1363 (error "Select or click on the buffer where the secondary selection is")))
1366 (set-buffer (overlay-buffer mouse-secondary-overlay))
1367 (kill-region (overlay-start mouse-secondary-overlay)
1368 (overlay-end mouse-secondary-overlay))))
1369 (delete-overlay mouse-secondary-overlay)
1370 ;;; (x-set-selection 'SECONDARY nil)
1371 (setq mouse-secondary-overlay nil))
1373 (defun mouse-secondary-save-then-kill (click)
1374 "Save text to point in kill ring; the second time, kill the text.
1375 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1376 If the text between where you did \\[mouse-start-secondary] and where
1377 you use this command matches the text at the front of the kill ring,
1378 this command deletes the text.
1379 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1380 which prepares for a second click with this command to delete the text.
1382 If you have already made a secondary selection in that buffer,
1383 this command extends or retracts the selection to where you click.
1384 If you do this again in a different position, it extends or retracts
1385 again. If you do this twice in the same position, it kills the selection."
1387 (mouse-minibuffer-check click)
1388 (let ((posn (event-start click))
1389 (click-posn (posn-point (event-start click)))
1390 ;; Don't let a subsequent kill command append to this one:
1391 ;; prevent setting this-command to kill-region.
1392 (this-command this-command))
1393 (or (eq (window-buffer (posn-window posn))
1394 (or (and mouse-secondary-overlay
1395 (overlay-buffer mouse-secondary-overlay))
1396 (if mouse-secondary-start
1397 (marker-buffer mouse-secondary-start))))
1398 (error "Wrong buffer"))
1400 (set-buffer (window-buffer (posn-window posn)))
1401 (if (> (mod mouse-secondary-click-count 3) 0)
1402 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1404 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1405 ;; Find both ends of the object selected by this click.
1407 (mouse-start-end click-posn click-posn
1408 mouse-secondary-click-count)))
1409 ;; Move whichever end is closer to the click.
1410 ;; That's what xterm does, and it seems reasonable.
1411 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1412 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1413 (move-overlay mouse-secondary-overlay (car range)
1414 (overlay-end mouse-secondary-overlay))
1415 (move-overlay mouse-secondary-overlay
1416 (overlay-start mouse-secondary-overlay)
1418 ;; We have already put the old region in the kill ring.
1419 ;; Replace it with the extended region.
1420 ;; (It would be annoying to make a separate entry.)
1421 (kill-new (buffer-substring
1422 (overlay-start mouse-secondary-overlay)
1423 (overlay-end mouse-secondary-overlay)) t)
1424 ;; Arrange for a repeated mouse-3 to kill this region.
1425 (setq mouse-save-then-kill-posn
1426 (list (car kill-ring) (point) click-posn)))
1427 ;; If we click this button again without moving it,
1430 (mouse-save-then-kill-delete-region
1431 (overlay-start mouse-secondary-overlay)
1432 (overlay-end mouse-secondary-overlay))
1433 (setq mouse-save-then-kill-posn nil)
1434 (setq mouse-secondary-click-count 0)
1435 (delete-overlay mouse-secondary-overlay)))
1436 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1437 mouse-save-then-kill-posn
1438 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1439 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1440 ;; If this is the second time we've called
1441 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1443 (mouse-save-then-kill-delete-region
1444 (overlay-start mouse-secondary-overlay)
1445 (overlay-end mouse-secondary-overlay))
1446 (setq mouse-save-then-kill-posn nil)
1447 (delete-overlay mouse-secondary-overlay))
1448 (if (overlay-start mouse-secondary-overlay)
1449 ;; We have a selection, so adjust it.
1451 (if (numberp click-posn)
1453 ;; Move whichever end of the region is closer to the click.
1454 ;; That is what xterm does, and it seems reasonable.
1455 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1456 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1457 (move-overlay mouse-secondary-overlay click-posn
1458 (overlay-end mouse-secondary-overlay))
1459 (move-overlay mouse-secondary-overlay
1460 (overlay-start mouse-secondary-overlay)
1462 (setq deactivate-mark nil)))
1463 (if (eq last-command 'mouse-secondary-save-then-kill)
1464 ;; If the front of the kill ring comes from
1465 ;; an immediately previous use of this command,
1466 ;; replace it with the extended region.
1467 ;; (It would be annoying to make a separate entry.)
1468 (kill-new (buffer-substring
1469 (overlay-start mouse-secondary-overlay)
1470 (overlay-end mouse-secondary-overlay)) t)
1471 (let (deactivate-mark)
1472 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1473 (overlay-end mouse-secondary-overlay)))))
1474 (if mouse-secondary-start
1475 ;; All we have is one end of a selection,
1476 ;; so put the other end here.
1477 (let ((start (+ 0 mouse-secondary-start)))
1478 (kill-ring-save start click-posn)
1479 (if mouse-secondary-overlay
1480 (move-overlay mouse-secondary-overlay start click-posn)
1481 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1482 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1483 (setq mouse-save-then-kill-posn
1484 (list (car kill-ring) (point) click-posn))))
1485 (if (overlay-buffer mouse-secondary-overlay)
1486 (x-set-selection 'SECONDARY
1488 (overlay-start mouse-secondary-overlay)
1489 (overlay-end mouse-secondary-overlay)))))))
1491 (defcustom mouse-buffer-menu-maxlen 20
1492 "*Number of buffers in one pane (submenu) of the buffer menu.
1493 If we have lots of buffers, divide them into groups of
1494 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1498 (defcustom mouse-buffer-menu-mode-mult 4
1499 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1500 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1501 will split the buffer menu by the major modes (see
1502 `mouse-buffer-menu-mode-groups') or just by menu length.
1503 Set to 1 (or even 0!) if you want to group by major mode always, and to
1504 a large number if you prefer a mixed multitude. The default is 4."
1509 (defvar mouse-buffer-menu-mode-groups
1510 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1511 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1516 ("Outline" . "Text")
1518 "How to group various major modes together in \\[mouse-buffer-menu].
1519 Each element has the form (REGEXP . GROUPNAME).
1520 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1522 (defun mouse-buffer-menu (event)
1523 "Pop up a menu of buffers for selection with the mouse.
1524 This switches buffers in the window that you clicked on,
1525 and selects that window."
1527 (mouse-minibuffer-check event)
1528 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1529 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1530 (let ((tail buffers))
1532 ;; Divide all buffers into buckets for various major modes.
1533 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1534 (with-current-buffer (car tail)
1535 (let* ((adjusted-major-mode major-mode) elt)
1536 (let ((tail mouse-buffer-menu-mode-groups))
1538 (if (string-match (car (car tail)) mode-name)
1539 (setq adjusted-major-mode (cdr (car tail))))
1540 (setq tail (cdr tail))))
1541 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1543 (setq elt (list adjusted-major-mode
1544 (if (stringp adjusted-major-mode)
1547 split-by-major-mode (cons elt split-by-major-mode)))
1548 (or (memq (car tail) (cdr (cdr elt)))
1549 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1550 (setq tail (cdr tail))))
1551 ;; Compute the sum of squares of sizes of the major-mode buckets.
1552 (let ((tail split-by-major-mode))
1553 (setq sum-of-squares 0)
1555 (setq sum-of-squares
1557 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1558 (setq tail (cdr tail))))
1559 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1560 (* (length buffers) (length buffers)))
1561 ;; Subdividing by major modes really helps, so let's do it.
1562 (let (subdivided-menus (buffers-left (length buffers)))
1563 ;; Sort the list to put the most popular major modes first.
1564 (setq split-by-major-mode
1565 (sort split-by-major-mode
1566 (function (lambda (elt1 elt2)
1567 (> (length elt1) (length elt2))))))
1568 ;; Make a separate submenu for each major mode
1569 ;; that has more than one buffer,
1570 ;; unless all the remaining buffers are less than 1/10 of them.
1571 (while (and split-by-major-mode
1572 (and (> (length (car split-by-major-mode)) 3)
1573 (> (* buffers-left 10) (length buffers))))
1574 (setq subdivided-menus
1576 (nth 1 (car split-by-major-mode))
1577 (mouse-buffer-menu-alist
1578 (cdr (cdr (car split-by-major-mode)))))
1581 (- buffers-left (length (cdr (car split-by-major-mode)))))
1582 (setq split-by-major-mode (cdr split-by-major-mode)))
1583 ;; If any major modes are left over,
1584 ;; make a single submenu for them.
1585 (if split-by-major-mode
1586 (setq subdivided-menus
1589 (mouse-buffer-menu-alist
1590 ;; we don't need split-by-major-mode any
1591 ;; more, so we can ditch it with nconc.
1592 (apply 'nconc (mapcar 'cddr split-by-major-mode))))
1594 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1596 (setq alist (mouse-buffer-menu-alist buffers))
1597 (setq menu (cons "Buffer Menu"
1598 (mouse-buffer-menu-split "Select Buffer" alist)))))
1599 (let ((buf (x-popup-menu event menu))
1600 (window (posn-window (event-start event))))
1602 (or (framep window) (select-window window))
1603 (switch-to-buffer buf)))))
1605 (defun mouse-buffer-menu-alist (buffers)
1611 (function (lambda (elt1 elt2)
1612 (string< (buffer-name elt1) (buffer-name elt2))))))
1615 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1618 (length (buffer-name (car tail))))))
1619 (setq tail (cdr tail)))
1622 (let ((elt (car tail)))
1623 (if (/= (aref (buffer-name elt) 0) ?\ )
1628 (format "%%%ds %%s%%s %%s" maxlen)
1630 (if (buffer-modified-p elt) "*" " ")
1633 (if buffer-read-only "%" " "))
1634 (or (buffer-file-name elt)
1637 (if list-buffers-directory
1639 list-buffers-directory)))
1643 (setq tail (cdr tail)))
1644 ;; Compensate for the reversal that the above loop does.
1647 (defun mouse-buffer-menu-split (title alist)
1648 ;; If we have lots of buffers, divide them into groups of 20
1649 ;; and make a pane (or submenu) for each one.
1650 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1651 (let ((alist alist) sublists next
1654 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1655 ;; and make them the next element of sublist.
1656 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1658 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1660 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1664 (nreverse sublists))
1665 ;; Few buffers--put them all in one pane.
1666 (list (cons title alist))))
1668 ;;; These need to be rewritten for the new scroll bar implementation.
1670 ;;;!! ;; Commands for the scroll bar.
1672 ;;;!! (defun mouse-scroll-down (click)
1673 ;;;!! (interactive "@e")
1674 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1676 ;;;!! (defun mouse-scroll-up (click)
1677 ;;;!! (interactive "@e")
1678 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1680 ;;;!! (defun mouse-scroll-down-full ()
1681 ;;;!! (interactive "@")
1682 ;;;!! (scroll-down nil))
1684 ;;;!! (defun mouse-scroll-up-full ()
1685 ;;;!! (interactive "@")
1686 ;;;!! (scroll-up nil))
1688 ;;;!! (defun mouse-scroll-move-cursor (click)
1689 ;;;!! (interactive "@e")
1690 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1692 ;;;!! (defun mouse-scroll-absolute (event)
1693 ;;;!! (interactive "@e")
1694 ;;;!! (let* ((pos (car event))
1695 ;;;!! (position (car pos))
1696 ;;;!! (length (car (cdr pos))))
1697 ;;;!! (if (<= length 0) (setq length 1))
1698 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1699 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1702 ;;;!! scale-factor)))
1703 ;;;!! (goto-char newpos)
1704 ;;;!! (recenter '(4)))))
1706 ;;;!! (defun mouse-scroll-left (click)
1707 ;;;!! (interactive "@e")
1708 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1710 ;;;!! (defun mouse-scroll-right (click)
1711 ;;;!! (interactive "@e")
1712 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1714 ;;;!! (defun mouse-scroll-left-full ()
1715 ;;;!! (interactive "@")
1716 ;;;!! (scroll-left nil))
1718 ;;;!! (defun mouse-scroll-right-full ()
1719 ;;;!! (interactive "@")
1720 ;;;!! (scroll-right nil))
1722 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1723 ;;;!! (interactive "@e")
1724 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1726 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1727 ;;;!! (interactive "@e")
1728 ;;;!! (let* ((pos (car event))
1729 ;;;!! (position (car pos))
1730 ;;;!! (length (car (cdr pos))))
1731 ;;;!! (set-window-hscroll (selected-window) 33)))
1733 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1734 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1735 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1737 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1738 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1739 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1741 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1742 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1743 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1745 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1746 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1747 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1749 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1750 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1751 ;;;!! 'mouse-scroll-absolute-horizontally)
1752 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1754 ;;;!! (global-set-key [horizontal-slider mouse-1]
1755 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1756 ;;;!! (global-set-key [horizontal-slider mouse-2]
1757 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1758 ;;;!! (global-set-key [horizontal-slider mouse-3]
1759 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1761 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1762 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1763 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1765 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1766 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1767 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1769 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1770 ;;;!! 'mouse-split-window-horizontally)
1771 ;;;!! (global-set-key [mode-line S-mouse-2]
1772 ;;;!! 'mouse-split-window-horizontally)
1773 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1774 ;;;!! 'mouse-split-window)
1777 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1778 ;;;!! ;;;; are of the form:
1779 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1782 ;;;!! ;;;; Dynamically track mouse coordinates
1785 ;;;!! ;;(defun track-mouse (event)
1786 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1787 ;;;!! ;; (interactive "@e")
1788 ;;;!! ;; (while mouse-grabbed
1789 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1790 ;;;!! ;; (abs-x (car pos))
1791 ;;;!! ;; (abs-y (cdr pos))
1792 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1793 ;;;!! ;; (list (car pos) (cdr pos))
1794 ;;;!! ;; (selected-window))))
1795 ;;;!! ;; (if (consp relative-coordinate)
1796 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1797 ;;;!! ;; (car relative-coordinate)
1798 ;;;!! ;; (car (cdr relative-coordinate)))
1799 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1802 ;;;!! ;; Dynamically put a box around the line indicated by point
1805 ;;;!! ;;(require 'backquote)
1807 ;;;!! ;;(defun mouse-select-buffer-line (event)
1808 ;;;!! ;; (interactive "@e")
1809 ;;;!! ;; (let ((relative-coordinate
1810 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1811 ;;;!! ;; (abs-y (car (cdr (car event)))))
1812 ;;;!! ;; (if (consp relative-coordinate)
1814 ;;;!! ;; (save-excursion
1815 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1816 ;;;!! ;; (x-draw-rectangle
1817 ;;;!! ;; (selected-screen)
1819 ;;;!! ;; (save-excursion
1820 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1821 ;;;!! ;; (end-of-line)
1822 ;;;!! ;; (push-mark nil t)
1823 ;;;!! ;; (beginning-of-line)
1824 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1825 ;;;!! ;; (sit-for 1)
1826 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1828 ;;;!! ;;(defvar last-line-drawn nil)
1829 ;;;!! ;;(defvar begin-delim "[^ \t]")
1830 ;;;!! ;;(defvar end-delim "[^ \t]")
1832 ;;;!! ;;(defun mouse-boxing (event)
1833 ;;;!! ;; (interactive "@e")
1834 ;;;!! ;; (save-excursion
1835 ;;;!! ;; (let ((screen (selected-screen)))
1836 ;;;!! ;; (while (= (x-mouse-events) 0)
1837 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1838 ;;;!! ;; (abs-x (car pos))
1839 ;;;!! ;; (abs-y (cdr pos))
1840 ;;;!! ;; (relative-coordinate
1841 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1842 ;;;!! ;; (selected-window)))
1843 ;;;!! ;; (begin-reg nil)
1844 ;;;!! ;; (end-reg nil)
1845 ;;;!! ;; (end-column nil)
1846 ;;;!! ;; (begin-column nil))
1847 ;;;!! ;; (if (and (consp relative-coordinate)
1848 ;;;!! ;; (or (not last-line-drawn)
1849 ;;;!! ;; (not (= last-line-drawn abs-y))))
1851 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1852 ;;;!! ;; (if (= (following-char) 10)
1855 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1856 ;;;!! ;; (setq begin-column (1- (current-column)))
1857 ;;;!! ;; (end-of-line)
1858 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1859 ;;;!! ;; (setq end-column (1+ (current-column)))
1860 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1861 ;;;!! ;; (x-draw-rectangle screen
1862 ;;;!! ;; (setq last-line-drawn abs-y)
1863 ;;;!! ;; begin-column
1864 ;;;!! ;; (- end-column begin-column) 1))))))))))
1866 ;;;!! ;;(defun mouse-erase-box ()
1867 ;;;!! ;; (interactive)
1868 ;;;!! ;; (if last-line-drawn
1870 ;;;!! ;; (x-erase-rectangle (selected-screen))
1871 ;;;!! ;; (setq last-line-drawn nil))))
1873 ;;;!! ;;; (defun test-x-rectangle ()
1874 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1875 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1876 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1879 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1882 ;;;!! (defvar double-start nil)
1883 ;;;!! (defconst double-click-interval 300
1884 ;;;!! "Max ticks between clicks")
1886 ;;;!! (defun double-down (event)
1887 ;;;!! (interactive "@e")
1888 ;;;!! (if double-start
1889 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1890 ;;;!! (if (< interval double-click-interval)
1892 ;;;!! (backward-up-list 1)
1893 ;;;!! ;; (message "Interval %d" interval)
1894 ;;;!! (sleep-for 1)))
1895 ;;;!! (setq double-start nil))
1896 ;;;!! (setq double-start (nth 4 event))))
1898 ;;;!! (defun double-up (event)
1899 ;;;!! (interactive "@e")
1900 ;;;!! (and double-start
1901 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1902 ;;;!! (setq double-start nil)))
1904 ;;;!! ;;; (defun x-test-doubleclick ()
1905 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1906 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1907 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1910 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1913 ;;;!! (defvar scrolled-lines 0)
1914 ;;;!! (defconst scroll-speed 1)
1916 ;;;!! (defun incr-scroll-down (event)
1917 ;;;!! (interactive "@e")
1918 ;;;!! (setq scrolled-lines 0)
1919 ;;;!! (incremental-scroll scroll-speed))
1921 ;;;!! (defun incr-scroll-up (event)
1922 ;;;!! (interactive "@e")
1923 ;;;!! (setq scrolled-lines 0)
1924 ;;;!! (incremental-scroll (- scroll-speed)))
1926 ;;;!! (defun incremental-scroll (n)
1927 ;;;!! (while (= (x-mouse-events) 0)
1928 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1929 ;;;!! (scroll-down n)
1930 ;;;!! (sit-for 300 t)))
1932 ;;;!! (defun incr-scroll-stop (event)
1933 ;;;!! (interactive "@e")
1934 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1935 ;;;!! (setq scrolled-lines 0)
1936 ;;;!! (sleep-for 1))
1938 ;;;!! ;;; (defun x-testing-scroll ()
1939 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1940 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1941 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1942 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1943 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1946 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1949 ;;;!! (defun mouse-kill-rectangle (event)
1950 ;;;!! "Kill the rectangle between point and the mouse cursor."
1951 ;;;!! (interactive "@e")
1952 ;;;!! (let ((point-save (point)))
1953 ;;;!! (save-excursion
1954 ;;;!! (mouse-set-point event)
1955 ;;;!! (push-mark nil t)
1956 ;;;!! (if (> point-save (point))
1957 ;;;!! (kill-rectangle (point) point-save)
1958 ;;;!! (kill-rectangle point-save (point))))))
1960 ;;;!! (defun mouse-open-rectangle (event)
1961 ;;;!! "Kill the rectangle between point and the mouse cursor."
1962 ;;;!! (interactive "@e")
1963 ;;;!! (let ((point-save (point)))
1964 ;;;!! (save-excursion
1965 ;;;!! (mouse-set-point event)
1966 ;;;!! (push-mark nil t)
1967 ;;;!! (if (> point-save (point))
1968 ;;;!! (open-rectangle (point) point-save)
1969 ;;;!! (open-rectangle point-save (point))))))
1971 ;;;!! ;; Must be a better way to do this.
1973 ;;;!! (defun mouse-multiple-insert (n char)
1974 ;;;!! (while (> n 0)
1976 ;;;!! (setq n (1- n))))
1978 ;;;!! ;; What this could do is not finalize until button was released.
1980 ;;;!! (defun mouse-move-text (event)
1981 ;;;!! "Move text from point to cursor position, inserting spaces."
1982 ;;;!! (interactive "@e")
1983 ;;;!! (let* ((relative-coordinate
1984 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1985 ;;;!! (if (consp relative-coordinate)
1986 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1988 ;;;!! (- (car relative-coordinate) (current-column))))
1989 ;;;!! ((< (current-column) (car relative-coordinate))
1990 ;;;!! (mouse-multiple-insert
1991 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1992 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1994 ;; Choose a completion with the mouse.
1996 (defun mouse-choose-completion (event)
1997 "Click on an alternative in the `*Completions*' buffer to choose it."
1999 ;; Give temporary modes such as isearch a chance to turn off.
2000 (run-hooks 'mouse-leave-buffer-hook)
2001 (let ((buffer (window-buffer))
2005 (set-buffer (window-buffer (posn-window (event-start event))))
2006 (if completion-reference-buffer
2007 (setq buffer completion-reference-buffer))
2008 (setq base-size completion-base-size)
2010 (goto-char (posn-point (event-start event)))
2012 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2013 (setq end (point) beg (1+ (point))))
2015 (error "No completion here"))
2016 (setq beg (previous-single-property-change beg 'mouse-face))
2017 (setq end (or (next-single-property-change end 'mouse-face)
2019 (setq choice (buffer-substring beg end)))))
2020 (let ((owindow (selected-window)))
2021 (select-window (posn-window (event-start event)))
2022 (if (and (one-window-p t 'selected-frame)
2023 (window-dedicated-p (selected-window)))
2024 ;; This is a special buffer's frame
2025 (iconify-frame (selected-frame))
2026 (or (window-dedicated-p (selected-window))
2028 (select-window owindow))
2029 (choose-completion-string choice buffer base-size)))
2033 (defun font-menu-add-default ()
2034 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2035 (font-alist x-fixed-font-alist)
2036 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2037 (if (assoc "Default" elt)
2038 (delete (assoc "Default" elt) elt))
2040 (cons (list "Default" default)
2043 (defvar x-fixed-font-alist
2046 ;; For these, we specify the pixel height and width.
2048 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2050 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2052 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2053 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2054 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2055 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2056 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2057 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2058 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2059 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2062 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2064 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2066 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2068 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2070 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2072 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2074 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2075 ;;; We don't seem to have these; who knows what they are.
2076 ;;; ("fg-18" "fg-18")
2077 ;;; ("fg-25" "fg-25")
2078 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2079 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2080 ("lucidasanstypewriter-bold-24"
2081 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2082 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2083 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2086 ;; For these, we specify the point height.
2087 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2088 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2089 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2090 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2091 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2092 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2093 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2094 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2095 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2096 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2097 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2098 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2099 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2100 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2101 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2102 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2103 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2104 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2105 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2106 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2107 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2108 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2109 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2110 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2112 "X fonts suitable for use in Emacs.")
2114 (defun mouse-set-font (&rest fonts)
2115 "Select an emacs font from a list of known good fonts and fontsets."
2119 ;; Append list of fontsets currently defined.
2120 (append x-fixed-font-alist (list (generate-fontset-menu)))))
2126 (set-default-font (car fonts))
2127 (setq font (car fonts))
2130 (setq fonts (cdr fonts)))))
2132 (error "Font not found")))))
2134 ;;; Bindings for mouse commands.
2136 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2137 (global-set-key [mouse-1] 'mouse-set-point)
2138 (global-set-key [drag-mouse-1] 'mouse-set-region)
2140 ;; These are tested for in mouse-drag-region.
2141 (global-set-key [double-mouse-1] 'mouse-set-point)
2142 (global-set-key [triple-mouse-1] 'mouse-set-point)
2144 (global-set-key [mouse-2] 'mouse-yank-at-click)
2145 (global-set-key [mouse-3] 'mouse-save-then-kill)
2147 ;; By binding these to down-going events, we let the user use the up-going
2148 ;; event to make the selection, saving a click.
2149 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2150 (if (not (eq system-type 'ms-dos))
2151 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2152 ;; C-down-mouse-2 is bound in facemenu.el.
2153 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2156 ;; Replaced with dragging mouse-1
2157 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2159 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2160 ;; vertical-line prevents Emacs from signaling an error when the mouse
2161 ;; button is released after dragging these lines, on non-toolkit
2163 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2164 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2165 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2166 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2167 (global-set-key [header-line mouse-1] 'mouse-select-window)
2168 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2169 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2170 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2171 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2172 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2173 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2174 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2178 ;; This file contains the functionality of the old mldrag.el.
2179 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2180 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2181 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2182 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2185 ;;; mouse.el ends here