(comint-output-filter):
[emacs.git] / lisp / mouse.el
blob373d1014a0584d17a9c5cdedf09d6dc712339d78
1 ;;; mouse.el --- window system-independent mouse support
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: hardware, mouse
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This package provides various useful commands (including help
28 ;; system access) through the mouse. All this code assumes that mouse
29 ;; interpretation has been abstracted into Emacs input events.
31 ;; The code is rather X-dependent.
33 ;;; Code:
35 ;;; Utility functions.
37 ;;; Indent track-mouse like progn.
38 (put 'track-mouse 'lisp-indent-function 0)
40 (defcustom mouse-yank-at-point nil
41 "*If non-nil, mouse yank commands yank at point instead of at click."
42 :type 'boolean
43 :group 'mouse)
45 ;; Provide a mode-specific menu on a mouse button.
47 (defun popup-menu (menu &optional position prefix)
48 "Popup the given menu and call the selected option.
49 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
50 `x-popup-menu'.
51 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
52 the current mouse position.
53 PREFIX is the prefix argument (if any) to pass to the command."
54 (let* ((map (cond
55 ((keymapp menu) menu)
56 ((and (listp menu) (keymapp (car menu))) menu)
57 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
58 (filter (when (symbolp map)
59 (plist-get (get map 'menu-pro) :filter))))
60 (if filter (funcall filter (symbol-function map)) map)))))
61 event)
62 ;; The looping behavior was taken from lmenu's popup-menu-popup
63 (while (and map (setq event
64 ;; map could be a prefix key, in which case
65 ;; we need to get its function cell
66 ;; definition.
67 (x-popup-menu position (indirect-function map))))
68 ;; Strangely x-popup-menu returns a list.
69 ;; mouse-major-mode-menu was using a weird:
70 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
71 (let ((cmd
72 (if (and (not (keymapp map)) (listp map))
73 ;; We were given a list of keymaps. Search them all
74 ;; in sequence until a first binding is found.
75 (let ((mouse-click (apply 'vector event))
76 binding)
77 (while (and map (null binding))
78 (setq binding (lookup-key (car map) mouse-click))
79 (if (numberp binding) ; `too long'
80 (setq binding nil))
81 (setq map (cdr map)))
82 binding)
83 ;; We were given a single keymap.
84 (lookup-key map (apply 'vector event)))))
85 (setq map nil)
86 ;; Clear out echoing, which perhaps shows a prefix arg.
87 (message "")
88 (when cmd
89 (if (keymapp cmd)
90 ;; Try again but with the submap.
91 (setq map cmd)
92 (setq prefix-arg prefix)
93 ;; `setup-specified-language-environment', for instance,
94 ;; expects this to be set from a menu keymap.
95 (setq last-command-event (car (last event)))
96 ;; mouse-major-mode-menu was using `command-execute' instead.
97 (call-interactively cmd)))))))
99 (defvar mouse-major-mode-menu-prefix) ; dynamically bound
101 (defun mouse-major-mode-menu (event prefix)
102 "Pop up a mode-specific menu of mouse commands.
103 Default to the Edit menu if the major mode doesn't define a menu."
104 ;; Switch to the window clicked on, because otherwise
105 ;; the mode's commands may not make sense.
106 (interactive "@e\nP")
107 ;; Let the mode update its menus first.
108 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
109 (let* (;; This is where mouse-major-mode-menu-prefix
110 ;; returns the prefix we should use (after menu-bar).
111 ;; It is either nil or (SOME-SYMBOL).
112 (mouse-major-mode-menu-prefix nil)
113 ;; Keymap from which to inherit; may be null.
114 (ancestor (mouse-major-mode-menu-1
115 (and (current-local-map)
116 (local-key-binding [menu-bar]))))
117 ;; Make a keymap in which our last command leads to a menu or
118 ;; default to the edit menu.
119 (newmap (if ancestor
120 (make-sparse-keymap (concat mode-name " Mode"))
121 menu-bar-edit-menu))
122 result)
123 (if ancestor
124 ;; Make our menu inherit from the desired keymap which we want
125 ;; to display as the menu now.
126 (set-keymap-parent newmap ancestor))
127 (popup-menu newmap event prefix)))
130 ;; Compute and cache the equivalent keys in MENU and all its submenus.
131 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
132 ;;; (and (eq (car menu) 'keymap)
133 ;;; (x-popup-menu nil menu))
134 ;;; (while menu
135 ;;; (and (consp (car menu))
136 ;;; (consp (cdr (car menu)))
137 ;;; (let ((tail (cdr (car menu))))
138 ;;; (while (and (consp tail)
139 ;;; (not (eq (car tail) 'keymap)))
140 ;;; (setq tail (cdr tail)))
141 ;;; (if (consp tail)
142 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
143 ;;; (setq menu (cdr menu))))
145 ;; Given a mode's menu bar keymap,
146 ;; if it defines exactly one menu bar menu,
147 ;; return just that menu.
148 ;; Otherwise return a menu for all of them.
149 (defun mouse-major-mode-menu-1 (menubar)
150 (if menubar
151 (let ((tail menubar)
152 submap)
153 (while tail
154 (if (consp (car tail))
155 (if submap
156 (setq submap t)
157 (setq submap (car tail))))
158 (setq tail (cdr tail)))
159 (if (eq submap t)
160 menubar
161 (setq mouse-major-mode-menu-prefix (list (car submap)))
162 (lookup-key menubar (vector (car submap)))))))
164 (defun mouse-popup-menubar (event prefix)
165 "Pops up a menu equiavlent to the menu bar a keyboard EVENT with PREFIX.
166 The contents are the items that would be in the menu bar whether or
167 not it is actually displayed."
168 (interactive "@e \nP")
169 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
170 (let* ((local-menu (and (current-local-map)
171 (lookup-key (current-local-map) [menu-bar])))
172 (global-menu (lookup-key global-map [menu-bar]))
173 ;; If a keymap doesn't have a prompt string (a lazy
174 ;; programmer didn't bother to provide one), create it and
175 ;; insert it into the keymap; each keymap gets its own
176 ;; prompt. This is required for non-toolkit versions to
177 ;; display non-empty menu pane names.
178 (minor-mode-menus
179 (mapcar
180 (function
181 (lambda (menu)
182 (let* ((minor-mode (car menu))
183 (menu (cdr menu))
184 (title-or-map (cadr menu)))
185 (or (stringp title-or-map)
186 (setq menu
187 (cons 'keymap
188 (cons (concat
189 (capitalize (subst-char-in-string
190 ?- ?\ (symbol-name
191 minor-mode)))
192 " Menu")
193 (cdr menu)))))
194 menu)))
195 (minor-mode-key-binding [menu-bar])))
196 (local-title-or-map (and local-menu (cadr local-menu)))
197 (global-title-or-map (cadr global-menu)))
198 (or (null local-menu)
199 (stringp local-title-or-map)
200 (setq local-menu (cons 'keymap
201 (cons (concat mode-name " Mode Menu")
202 (cdr local-menu)))))
203 (or (stringp global-title-or-map)
204 (setq global-menu (cons 'keymap
205 (cons "Global Menu"
206 (cdr global-menu)))))
207 ;; Supplying the list is faster than making a new map.
208 (popup-menu (append (list global-menu)
209 (if local-menu
210 (list local-menu))
211 minor-mode-menus)
212 event prefix)))
214 (defun mouse-popup-menubar-stuff (event prefix)
215 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
216 Use the former if the menu bar is showing, otherwise the latter."
217 (interactive "@e \nP")
218 (if (zerop (assoc-default 'menu-bar-lines (frame-parameters) 'eq 0))
219 (mouse-popup-menubar event prefix)
220 (mouse-major-mode-menu event prefix)))
222 ;; Commands that operate on windows.
224 (defun mouse-minibuffer-check (event)
225 (let ((w (posn-window (event-start event))))
226 (and (window-minibuffer-p w)
227 (not (minibuffer-window-active-p w))
228 (error "Minibuffer window is not active")))
229 ;; Give temporary modes such as isearch a chance to turn off.
230 (run-hooks 'mouse-leave-buffer-hook))
232 (defun mouse-delete-window (click)
233 "Delete the window you click on.
234 If the frame has just one window, bury the current buffer instead.
235 This command must be bound to a mouse click."
236 (interactive "e")
237 (if (one-window-p t)
238 (bury-buffer)
239 (mouse-minibuffer-check click)
240 (delete-window (posn-window (event-start click)))))
242 (defun mouse-select-window (click)
243 "Select the window clicked on; don't move point."
244 (interactive "e")
245 (mouse-minibuffer-check click)
246 (let ((oframe (selected-frame))
247 (frame (window-frame (posn-window (event-start click)))))
248 (select-window (posn-window (event-start click)))
249 (raise-frame frame)
250 (select-frame frame)
251 (or (eq frame oframe)
252 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
254 (defun mouse-tear-off-window (click)
255 "Delete the window clicked on, and create a new frame displaying its buffer."
256 (interactive "e")
257 (mouse-minibuffer-check click)
258 (let* ((window (posn-window (event-start click)))
259 (buf (window-buffer window))
260 (frame (make-frame)))
261 (select-frame frame)
262 (switch-to-buffer buf)
263 (delete-window window)))
265 (defun mouse-delete-other-windows ()
266 "Delete all window except the one you click on."
267 (interactive "@")
268 (delete-other-windows))
270 (defun mouse-split-window-vertically (click)
271 "Select Emacs window mouse is on, then split it vertically in half.
272 The window is split at the line clicked on.
273 This command must be bound to a mouse click."
274 (interactive "@e")
275 (mouse-minibuffer-check click)
276 (let ((start (event-start click)))
277 (select-window (posn-window start))
278 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
279 (first-line window-min-height)
280 (last-line (- (window-height) window-min-height)))
281 (if (< last-line first-line)
282 (error "Window too short to split")
283 (split-window-vertically
284 (min (max new-height first-line) last-line))))))
286 (defun mouse-split-window-horizontally (click)
287 "Select Emacs window mouse is on, then split it horizontally in half.
288 The window is split at the column clicked on.
289 This command must be bound to a mouse click."
290 (interactive "@e")
291 (mouse-minibuffer-check click)
292 (let ((start (event-start click)))
293 (select-window (posn-window start))
294 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
295 (first-col window-min-width)
296 (last-col (- (window-width) window-min-width)))
297 (if (< last-col first-col)
298 (error "Window too narrow to split")
299 (split-window-horizontally
300 (min (max new-width first-col) last-col))))))
302 (defun mouse-drag-mode-line-1 (start-event mode-line-p)
303 "Change the height of a window by dragging on the mode or header line.
304 START-EVENT is the starting mouse-event of the drag action.
305 MODE-LINE-P non-nil means a mode line is dragged."
306 ;; Give temporary modes such as isearch a chance to turn off.
307 (run-hooks 'mouse-leave-buffer-hook)
308 (let ((done nil)
309 (echo-keystrokes 0)
310 (start-event-frame (window-frame (car (car (cdr start-event)))))
311 (start-event-window (car (car (cdr start-event))))
312 (start-nwindows (count-windows t))
313 (old-selected-window (selected-window))
314 should-enlarge-minibuffer
315 event mouse minibuffer y top bot edges wconfig params growth)
316 (setq params (frame-parameters))
317 (setq minibuffer (cdr (assq 'minibuffer params)))
318 (track-mouse
319 (progn
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)
324 ;; if this is the bottommost ordinary window, then to
325 ;; move its modeline the minibuffer must be enlarged.
326 (setq should-enlarge-minibuffer
327 (and minibuffer
328 mode-line-p
329 (not (one-window-p t))
330 (= (nth 1 (window-edges minibuffer))
331 (nth 3 (window-edges)))))
332 ;; loop reading events and sampling the position of
333 ;; the mouse.
334 (while (not done)
335 (setq event (read-event)
336 mouse (mouse-position))
337 ;; do nothing if
338 ;; - there is a switch-frame event.
339 ;; - the mouse isn't in the frame that we started in
340 ;; - the mouse isn't in any Emacs frame
341 ;; drag if
342 ;; - there is a mouse-movement event
343 ;; - there is a scroll-bar-movement event
344 ;; (same as mouse movement for our purposes)
345 ;; quit if
346 ;; - there is a keyboard event or some other unknown event
347 ;; unknown event.
348 (cond ((integerp event)
349 (setq done t))
350 ((eq (car event) 'switch-frame)
351 nil)
352 ((not (memq (car event)
353 '(mouse-movement scroll-bar-movement)))
354 (if (consp event)
355 (setq unread-command-events
356 (cons event unread-command-events)))
357 (setq done t))
358 ((not (eq (car mouse) start-event-frame))
359 nil)
360 ((null (car (cdr mouse)))
361 nil)
363 (setq y (cdr (cdr mouse))
364 edges (window-edges)
365 top (nth 1 edges)
366 bot (nth 3 edges))
368 ;; compute size change needed
369 (cond (mode-line-p
370 ;; Scale back a move that would make the
371 ;; window too short.
372 (when (< (- y top -1) window-min-height)
373 (setq y (+ top window-min-height -1)))
374 (setq growth (- y bot -1)))
376 (when (< (- bot y -1) window-min-height)
377 (setq y (- bot window-min-height -1)))
378 (setq growth (- top y -1))))
379 (setq wconfig (current-window-configuration))
381 ;; Check for an error case.
382 (if (and (/= growth 0)
383 (not minibuffer)
384 (one-window-p t))
385 (error "Attempt to resize sole window"))
387 ;; grow/shrink minibuffer?
388 (if should-enlarge-minibuffer
389 (progn
390 ;; yes. briefly select minibuffer so
391 ;; enlarge-window will affect the
392 ;; correct window.
393 (select-window minibuffer)
394 ;; scale back shrinkage if it would
395 ;; make the minibuffer less than 1
396 ;; line tall.
397 (if (and (> growth 0)
398 (< (- (window-height minibuffer)
399 growth)
401 (setq growth (1- (window-height minibuffer))))
402 (enlarge-window (- growth))
403 (select-window start-event-window))
404 ;; no. grow/shrink the selected window
405 ;; (message "growth = %d" growth)
406 (enlarge-window growth))
408 ;; if this window's growth caused another
409 ;; window to be deleted because it was too
410 ;; short, rescind the change.
412 ;; if size change caused space to be stolen
413 ;; from a window above this one, rescind the
414 ;; change, but only if we didn't grow/shrink
415 ;; the minibuffer. minibuffer size changes
416 ;; can cause all windows to shrink... no way
417 ;; around it.
418 (if (or (/= start-nwindows (count-windows t))
419 (and (not should-enlarge-minibuffer)
420 mode-line-p
421 (/= top (nth 1 (window-edges)))))
422 (set-window-configuration wconfig)))))))))
424 (defun mouse-drag-mode-line (start-event)
425 "Change the height of a window by dragging on the mode line."
426 (interactive "e")
427 (mouse-drag-mode-line-1 start-event t))
429 (defun mouse-drag-header-line (start-event)
430 "Change the height of a window by dragging on the header line."
431 (interactive "e")
432 (mouse-drag-mode-line-1 start-event nil))
435 (defun mouse-drag-vertical-line (start-event)
436 "Change the width of a window by dragging on the vertical line."
437 (interactive "e")
438 ;; Give temporary modes such as isearch a chance to turn off.
439 (run-hooks 'mouse-leave-buffer-hook)
440 (let* ((done nil)
441 (echo-keystrokes 0)
442 (start-event-frame (window-frame (car (car (cdr start-event)))))
443 (start-event-window (car (car (cdr start-event))))
444 (start-nwindows (count-windows t))
445 (old-selected-window (selected-window))
446 event mouse x left right edges wconfig growth
447 (which-side
448 (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
449 'right)))
450 (if (one-window-p t)
451 (error "Attempt to resize sole ordinary window"))
452 (if (eq which-side 'right)
453 (if (= (nth 2 (window-edges start-event-window))
454 (frame-width start-event-frame))
455 (error "Attempt to drag rightmost scrollbar"))
456 (if (= (nth 0 (window-edges start-event-window)) 0)
457 (error "Attempt to drag leftmost scrollbar")))
458 (track-mouse
459 (progn
460 ;; enlarge-window only works on the selected window, so
461 ;; we must select the window where the start event originated.
462 ;; unwind-protect will restore the old selected window later.
463 (select-window start-event-window)
464 ;; loop reading events and sampling the position of
465 ;; the mouse.
466 (while (not done)
467 (setq event (read-event)
468 mouse (mouse-position))
469 ;; do nothing if
470 ;; - there is a switch-frame event.
471 ;; - the mouse isn't in the frame that we started in
472 ;; - the mouse isn't in any Emacs frame
473 ;; drag if
474 ;; - there is a mouse-movement event
475 ;; - there is a scroll-bar-movement event
476 ;; (same as mouse movement for our purposes)
477 ;; quit if
478 ;; - there is a keyboard event or some other unknown event
479 ;; unknown event.
480 (cond ((integerp event)
481 (setq done t))
482 ((eq (car event) 'switch-frame)
483 nil)
484 ((not (memq (car event)
485 '(mouse-movement scroll-bar-movement)))
486 (if (consp event)
487 (setq unread-command-events
488 (cons event unread-command-events)))
489 (setq done t))
490 ((not (eq (car mouse) start-event-frame))
491 nil)
492 ((null (car (cdr mouse)))
493 nil)
495 (save-selected-window
496 ;; If the scroll bar is on the window's left,
497 ;; adjust the window on the left.
498 (unless (eq which-side 'right)
499 (select-window (previous-window)))
500 (setq x (- (car (cdr mouse))
501 (if (eq which-side 'right) 0 2))
502 edges (window-edges)
503 left (nth 0 edges)
504 right (nth 2 edges))
505 ;; scale back a move that would make the
506 ;; window too thin.
507 (if (< (- x left -1) window-min-width)
508 (setq x (+ left window-min-width -1)))
509 ;; compute size change needed
510 (setq growth (- x right -1)
511 wconfig (current-window-configuration))
512 (enlarge-window growth t)
513 ;; if this window's growth caused another
514 ;; window to be deleted because it was too
515 ;; thin, rescind the change.
517 ;; if size change caused space to be stolen
518 ;; from a window to the left of this one,
519 ;; rescind the change.
520 (if (or (/= start-nwindows (count-windows t))
521 (/= left (nth 0 (window-edges))))
522 (set-window-configuration wconfig))))))))))
524 (defun mouse-set-point (event)
525 "Move point to the position clicked on with the mouse.
526 This should be bound to a mouse click event type."
527 (interactive "e")
528 (mouse-minibuffer-check event)
529 ;; Use event-end in case called from mouse-drag-region.
530 ;; If EVENT is a click, event-end and event-start give same value.
531 (let ((posn (event-end event)))
532 (if (not (windowp (posn-window posn)))
533 (error "Cursor not in text area of window"))
534 (select-window (posn-window posn))
535 (if (numberp (posn-point posn))
536 (goto-char (posn-point posn)))))
538 (defvar mouse-last-region-beg nil)
539 (defvar mouse-last-region-end nil)
540 (defvar mouse-last-region-tick nil)
542 (defun mouse-region-match ()
543 "Return non-nil if there's an active region that was set with the mouse."
544 (and (mark t) mark-active
545 (eq mouse-last-region-beg (region-beginning))
546 (eq mouse-last-region-end (region-end))
547 (eq mouse-last-region-tick (buffer-modified-tick))))
549 (defun mouse-set-region (click)
550 "Set the region to the text dragged over, and copy to kill ring.
551 This should be bound to a mouse drag event."
552 (interactive "e")
553 (mouse-minibuffer-check click)
554 (let ((posn (event-start click))
555 (end (event-end click)))
556 (select-window (posn-window posn))
557 (if (numberp (posn-point posn))
558 (goto-char (posn-point posn)))
559 ;; If mark is highlighted, no need to bounce the cursor.
560 ;; On X, we highlight while dragging, thus once again no need to bounce.
561 (or transient-mark-mode
562 (memq (framep (selected-frame)) '(x pc w32))
563 (sit-for 1))
564 (push-mark)
565 (set-mark (point))
566 (if (numberp (posn-point end))
567 (goto-char (posn-point end)))
568 ;; Don't set this-command to kill-region, so that a following
569 ;; C-w will not double the text in the kill ring.
570 ;; Ignore last-command so we don't append to a preceding kill.
571 (let (this-command last-command deactivate-mark)
572 (copy-region-as-kill (mark) (point)))
573 (mouse-set-region-1)))
575 (defun mouse-set-region-1 ()
576 (setq mouse-last-region-beg (region-beginning))
577 (setq mouse-last-region-end (region-end))
578 (setq mouse-last-region-tick (buffer-modified-tick)))
580 (defcustom mouse-scroll-delay 0.25
581 "*The pause between scroll steps caused by mouse drags, in seconds.
582 If you drag the mouse beyond the edge of a window, Emacs scrolls the
583 window to bring the text beyond that edge into view, with a delay of
584 this many seconds between scroll steps. Scrolling stops when you move
585 the mouse back into the window, or release the button.
586 This variable's value may be non-integral.
587 Setting this to zero causes Emacs to scroll as fast as it can."
588 :type 'number
589 :group 'mouse)
591 (defcustom mouse-scroll-min-lines 1
592 "*The minimum number of lines scrolled by dragging mouse out of window.
593 Moving the mouse out the top or bottom edge of the window begins
594 scrolling repeatedly. The number of lines scrolled per repetition
595 is normally equal to the number of lines beyond the window edge that
596 the mouse has moved. However, it always scrolls at least the number
597 of lines specified by this variable."
598 :type 'integer
599 :group 'mouse)
601 (defun mouse-scroll-subr (window jump &optional overlay start)
602 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
603 If OVERLAY is an overlay, let it stretch from START to the far edge of
604 the newly visible text.
605 Upon exit, point is at the far edge of the newly visible text."
606 (cond
607 ((and (> jump 0) (< jump mouse-scroll-min-lines))
608 (setq jump mouse-scroll-min-lines))
609 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
610 (setq jump (- mouse-scroll-min-lines))))
611 (let ((opoint (point)))
612 (while (progn
613 (goto-char (window-start window))
614 (if (not (zerop (vertical-motion jump window)))
615 (progn
616 (set-window-start window (point))
617 (if (natnump jump)
618 (if (window-end window)
619 (progn
620 (goto-char (window-end window))
621 ;; window-end doesn't reflect the window's new
622 ;; start position until the next redisplay.
623 (vertical-motion (1- jump) window))
624 (vertical-motion (- (window-height window) 2)))
625 (goto-char (window-start window)))
626 (if overlay
627 (move-overlay overlay start (point)))
628 ;; Now that we have scrolled WINDOW properly,
629 ;; put point back where it was for the redisplay
630 ;; so that we don't mess up the selected window.
631 (or (eq window (selected-window))
632 (goto-char opoint))
633 (sit-for mouse-scroll-delay)))))
634 (or (eq window (selected-window))
635 (goto-char opoint))))
637 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
638 (defvar mouse-drag-overlay (make-overlay 1 1))
639 (delete-overlay mouse-drag-overlay)
640 (overlay-put mouse-drag-overlay 'face 'region)
642 (defvar mouse-selection-click-count 0)
644 (defvar mouse-selection-click-count-buffer nil)
646 (defun mouse-drag-region (start-event)
647 "Set the region to the text that the mouse is dragged over.
648 Highlight the drag area as you move the mouse.
649 This must be bound to a button-down mouse event.
650 In Transient Mark mode, the highlighting remains as long as the mark
651 remains active. Otherwise, it remains until the next input event."
652 (interactive "e")
653 (mouse-minibuffer-check start-event)
654 (let* ((echo-keystrokes 0)
655 (start-posn (event-start start-event))
656 (start-point (posn-point start-posn))
657 (start-window (posn-window start-posn))
658 (start-frame (window-frame start-window))
659 (start-hscroll (window-hscroll start-window))
660 (bounds (window-edges start-window))
661 (top (nth 1 bounds))
662 (bottom (if (window-minibuffer-p start-window)
663 (nth 3 bounds)
664 ;; Don't count the mode line.
665 (1- (nth 3 bounds))))
666 (click-count (1- (event-click-count start-event))))
667 (setq mouse-selection-click-count click-count)
668 (setq mouse-selection-click-count-buffer (current-buffer))
669 (mouse-set-point start-event)
670 ;; In case the down click is in the middle of some intangible text,
671 ;; use the end of that text, and put it in START-POINT.
672 (if (< (point) start-point)
673 (goto-char start-point))
674 (setq start-point (point))
675 (let ((range (mouse-start-end start-point start-point click-count)))
676 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
677 (window-buffer start-window)))
678 (deactivate-mark)
679 ;; end-of-range is used only in the single-click case.
680 ;; It is the place where the drag has reached so far
681 ;; (but not outside the window where the drag started).
682 (let (event end end-point last-end-point (end-of-range (point)))
683 (track-mouse
684 (while (progn
685 (setq event (read-event))
686 (or (mouse-movement-p event)
687 (eq (car-safe event) 'switch-frame)))
688 (if (eq (car-safe event) 'switch-frame)
690 (setq end (event-end event)
691 end-point (posn-point end))
692 (if (numberp end-point)
693 (setq last-end-point end-point))
695 (cond
696 ;; Are we moving within the original window?
697 ((and (eq (posn-window end) start-window)
698 (integer-or-marker-p end-point))
699 ;; Go to START-POINT first, so that when we move to END-POINT,
700 ;; if it's in the middle of intangible text,
701 ;; point jumps in the direction away from START-POINT.
702 (goto-char start-point)
703 (goto-char end-point)
704 (if (zerop (% click-count 3))
705 (setq end-of-range (point)))
706 (let ((range (mouse-start-end start-point (point) click-count)))
707 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
710 (let ((mouse-row (cdr (cdr (mouse-position)))))
711 (cond
712 ((null mouse-row))
713 ((< mouse-row top)
714 (mouse-scroll-subr start-window (- mouse-row top)
715 mouse-drag-overlay start-point)
716 ;; Without this, point tends to jump back to the starting
717 ;; position where the mouse button was pressed down.
718 (setq end-of-range (overlay-start mouse-drag-overlay)))
719 ((>= mouse-row bottom)
720 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
721 mouse-drag-overlay start-point)
722 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
723 ;; In case we did not get a mouse-motion event
724 ;; for the final move of the mouse before a drag event
725 ;; pretend that we did get one.
726 (when (and (memq 'drag (event-modifiers (car-safe event)))
727 (setq end (event-end event)
728 end-point (posn-point end))
729 (eq (posn-window end) start-window)
730 (integer-or-marker-p end-point))
732 ;; Go to START-POINT first, so that when we move to END-POINT,
733 ;; if it's in the middle of intangible text,
734 ;; point jumps in the direction away from START-POINT.
735 (goto-char start-point)
736 (goto-char end-point)
737 (if (zerop (% click-count 3))
738 (setq end-of-range (point)))
739 (let ((range (mouse-start-end start-point (point) click-count)))
740 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
742 (if (consp event)
743 (let ((fun (key-binding (vector (car event)))))
744 ;; Run the binding of the terminating up-event, if possible.
745 ;; In the case of a multiple click, it gives the wrong results,
746 ;; because it would fail to set up a region.
747 (if (not (= (overlay-start mouse-drag-overlay)
748 (overlay-end mouse-drag-overlay)))
749 (let* ((stop-point
750 (if (numberp (posn-point (event-end event)))
751 (posn-point (event-end event))
752 last-end-point))
753 ;; The end that comes from where we ended the drag.
754 ;; Point goes here.
755 (region-termination
756 (if (and stop-point (< stop-point start-point))
757 (overlay-start mouse-drag-overlay)
758 (overlay-end mouse-drag-overlay)))
759 ;; The end that comes from where we started the drag.
760 ;; Mark goes there.
761 (region-commencement
762 (- (+ (overlay-end mouse-drag-overlay)
763 (overlay-start mouse-drag-overlay))
764 region-termination))
765 last-command this-command)
766 (push-mark region-commencement t t)
767 (goto-char region-termination)
768 ;; Don't let copy-region-as-kill set deactivate-mark.
769 (let (deactivate-mark)
770 (copy-region-as-kill (point) (mark t)))
771 (let ((buffer (current-buffer)))
772 (mouse-show-mark)
773 ;; mouse-show-mark can call read-event,
774 ;; and that means the Emacs server could switch buffers
775 ;; under us. If that happened,
776 ;; avoid trying to use the region.
777 (and (mark t) mark-active
778 (eq buffer (current-buffer))
779 (mouse-set-region-1))))
780 (delete-overlay mouse-drag-overlay)
781 ;; Run the binding of the terminating up-event.
782 (when (and (functionp fun)
783 (= start-hscroll (window-hscroll start-window)))
784 (setq unread-command-events
785 (cons event unread-command-events)))))
786 (delete-overlay mouse-drag-overlay)))))
788 ;; Commands to handle xterm-style multiple clicks.
790 (defun mouse-skip-word (dir)
791 "Skip over word, over whitespace, or over identical punctuation.
792 If DIR is positive skip forward; if negative, skip backward."
793 (let* ((char (following-char))
794 (syntax (char-to-string (char-syntax char))))
795 (cond ((string= syntax "w")
796 ;; Here, we can't use skip-syntax-forward/backward because
797 ;; they don't pay attention to word-separating-categories,
798 ;; and thus they will skip over a true word boundary. So,
799 ;; we simularte the original behaviour by using
800 ;; forward-word.
801 (if (< dir 0)
802 (if (not (looking-at "\\<"))
803 (forward-word -1))
804 (if (or (looking-at "\\<") (not (looking-at "\\>")))
805 (forward-word 1))))
806 ((string= syntax " ")
807 (if (< dir 0)
808 (skip-syntax-backward syntax)
809 (skip-syntax-forward syntax)))
810 ((string= syntax "_")
811 (if (< dir 0)
812 (skip-syntax-backward "w_")
813 (skip-syntax-forward "w_")))
814 ((< dir 0)
815 (while (and (not (bobp)) (= (preceding-char) char))
816 (forward-char -1)))
818 (while (and (not (eobp)) (= (following-char) char))
819 (forward-char 1))))))
821 ;; Return a list of region bounds based on START and END according to MODE.
822 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
823 ;; If MODE is 1 then set point to start of word at (min START END),
824 ;; mark to end of word at (max START END).
825 ;; If MODE is 2 then do the same for lines.
826 (defun mouse-start-end (start end mode)
827 (if (> start end)
828 (let ((temp start))
829 (setq start end
830 end temp)))
831 (setq mode (mod mode 3))
832 (cond ((= mode 0)
833 (list start end))
834 ((and (= mode 1)
835 (= start end)
836 (char-after start)
837 (= (char-syntax (char-after start)) ?\())
838 (list start
839 (save-excursion
840 (goto-char start)
841 (forward-sexp 1)
842 (point))))
843 ((and (= mode 1)
844 (= start end)
845 (char-after start)
846 (= (char-syntax (char-after start)) ?\)))
847 (list (save-excursion
848 (goto-char (1+ start))
849 (backward-sexp 1)
850 (point))
851 (1+ start)))
852 ((and (= mode 1)
853 (= start end)
854 (char-after start)
855 (= (char-syntax (char-after start)) ?\"))
856 (let ((open (or (eq start (point-min))
857 (save-excursion
858 (goto-char (- start 1))
859 (looking-at "\\s(\\|\\s \\|\\s>")))))
860 (if open
861 (list start
862 (save-excursion
863 (condition-case nil
864 (progn
865 (goto-char start)
866 (forward-sexp 1)
867 (point))
868 (error end))))
869 (list (save-excursion
870 (condition-case nil
871 (progn
872 (goto-char (1+ start))
873 (backward-sexp 1)
874 (point))
875 (error end)))
876 (1+ start)))))
877 ((= mode 1)
878 (list (save-excursion
879 (goto-char start)
880 (mouse-skip-word -1)
881 (point))
882 (save-excursion
883 (goto-char end)
884 (mouse-skip-word 1)
885 (point))))
886 ((= mode 2)
887 (list (save-excursion
888 (goto-char start)
889 (beginning-of-line 1)
890 (point))
891 (save-excursion
892 (goto-char end)
893 (forward-line 1)
894 (point))))))
896 ;; Subroutine: set the mark where CLICK happened,
897 ;; but don't do anything else.
898 (defun mouse-set-mark-fast (click)
899 (mouse-minibuffer-check click)
900 (let ((posn (event-start click)))
901 (select-window (posn-window posn))
902 (if (numberp (posn-point posn))
903 (push-mark (posn-point posn) t t))))
905 (defun mouse-undouble-last-event (events)
906 (let* ((index (1- (length events)))
907 (last (nthcdr index events))
908 (event (car last))
909 (basic (event-basic-type event))
910 (old-modifiers (event-modifiers event))
911 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
912 (new
913 (if (consp event)
914 ;; Use reverse, not nreverse, since event-modifiers
915 ;; does not copy the list it returns.
916 (cons (event-convert-list (reverse (cons basic modifiers)))
917 (cdr event))
918 event)))
919 (setcar last new)
920 (if (and (not (equal modifiers old-modifiers))
921 (key-binding (apply 'vector events)))
923 (setcar last event)
924 nil)))
926 ;; Momentarily show where the mark is, if highlighting doesn't show it.
928 (defvar mouse-region-delete-keys '([delete])
929 "List of keys which shall cause the mouse region to be deleted.")
931 (defun mouse-show-mark ()
932 (if transient-mark-mode
933 (delete-overlay mouse-drag-overlay)
934 (let ((inhibit-quit t)
935 (echo-keystrokes 0)
936 event events key ignore
937 x-lost-selection-hooks)
938 (add-hook 'x-lost-selection-hooks
939 (lambda (seltype)
940 (if (eq seltype 'PRIMARY)
941 (progn (setq ignore t)
942 (throw 'mouse-show-mark t)))))
943 (move-overlay mouse-drag-overlay (point) (mark t))
944 (catch 'mouse-show-mark
945 ;; In this loop, execute scroll bar and switch-frame events.
946 ;; Also ignore down-events that are undefined.
947 (while (progn (setq event (read-event))
948 (setq events (append events (list event)))
949 (setq key (apply 'vector events))
950 (or (and (consp event)
951 (eq (car event) 'switch-frame))
952 (and (consp event)
953 (eq (posn-point (event-end event))
954 'vertical-scroll-bar))
955 (and (memq 'down (event-modifiers event))
956 (not (key-binding key))
957 (not (mouse-undouble-last-event events))
958 (not (member key mouse-region-delete-keys)))))
959 (and (consp event)
960 (or (eq (car event) 'switch-frame)
961 (eq (posn-point (event-end event))
962 'vertical-scroll-bar))
963 (let ((keys (vector 'vertical-scroll-bar event)))
964 (and (key-binding keys)
965 (progn
966 (call-interactively (key-binding keys)
967 nil keys)
968 (setq events nil)))))))
969 ;; If we lost the selection, just turn off the highlighting.
970 (if ignore
972 ;; For certain special keys, delete the region.
973 (if (member key mouse-region-delete-keys)
974 (delete-region (overlay-start mouse-drag-overlay)
975 (overlay-end mouse-drag-overlay))
976 ;; Otherwise, unread the key so it gets executed normally.
977 (setq unread-command-events
978 (nconc events unread-command-events))))
979 (setq quit-flag nil)
980 (delete-overlay mouse-drag-overlay))
981 (save-excursion
982 (goto-char (mark t))
983 (sit-for 1))))
985 (defun mouse-set-mark (click)
986 "Set mark at the position clicked on with the mouse.
987 Display cursor at that position for a second.
988 This must be bound to a mouse click."
989 (interactive "e")
990 (mouse-minibuffer-check click)
991 (select-window (posn-window (event-start click)))
992 ;; We don't use save-excursion because that preserves the mark too.
993 (let ((point-save (point)))
994 (unwind-protect
995 (progn (mouse-set-point click)
996 (push-mark nil t t)
997 (or transient-mark-mode
998 (sit-for 1)))
999 (goto-char point-save))))
1001 (defun mouse-kill (click)
1002 "Kill the region between point and the mouse click.
1003 The text is saved in the kill ring, as with \\[kill-region]."
1004 (interactive "e")
1005 (mouse-minibuffer-check click)
1006 (let* ((posn (event-start click))
1007 (click-posn (posn-point posn)))
1008 (select-window (posn-window posn))
1009 (if (numberp click-posn)
1010 (kill-region (min (point) click-posn)
1011 (max (point) click-posn)))))
1013 (defun mouse-yank-at-click (click arg)
1014 "Insert the last stretch of killed text at the position clicked on.
1015 Also move point to one end of the text thus inserted (normally the end),
1016 and set mark at the beginning..
1017 Prefix arguments are interpreted as with \\[yank].
1018 If `mouse-yank-at-point' is non-nil, insert at point
1019 regardless of where you click."
1020 (interactive "e\nP")
1021 ;; Give temporary modes such as isearch a chance to turn off.
1022 (run-hooks 'mouse-leave-buffer-hook)
1023 (or mouse-yank-at-point (mouse-set-point click))
1024 (setq this-command 'yank)
1025 (setq mouse-selection-click-count 0)
1026 (yank arg))
1028 (defun mouse-kill-ring-save (click)
1029 "Copy the region between point and the mouse click in the kill ring.
1030 This does not delete the region; it acts like \\[kill-ring-save]."
1031 (interactive "e")
1032 (mouse-set-mark-fast click)
1033 (let (this-command last-command)
1034 (kill-ring-save (point) (mark t)))
1035 (mouse-show-mark))
1037 ;;; This function used to delete the text between point and the mouse
1038 ;;; whenever it was equal to the front of the kill ring, but some
1039 ;;; people found that confusing.
1041 ;;; A list (TEXT START END), describing the text and position of the last
1042 ;;; invocation of mouse-save-then-kill.
1043 (defvar mouse-save-then-kill-posn nil)
1045 (defun mouse-save-then-kill-delete-region (beg end)
1046 ;; We must make our own undo boundaries
1047 ;; because they happen automatically only for the current buffer.
1048 (undo-boundary)
1049 (if (or (= beg end) (eq buffer-undo-list t))
1050 ;; If we have no undo list in this buffer,
1051 ;; just delete.
1052 (delete-region beg end)
1053 ;; Delete, but make the undo-list entry share with the kill ring.
1054 ;; First, delete just one char, so in case buffer is being modified
1055 ;; for the first time, the undo list records that fact.
1056 (let (before-change-functions after-change-functions)
1057 (delete-region beg
1058 (+ beg (if (> end beg) 1 -1))))
1059 (let ((buffer-undo-list buffer-undo-list))
1060 ;; Undo that deletion--but don't change the undo list!
1061 (let (before-change-functions after-change-functions)
1062 (primitive-undo 1 buffer-undo-list))
1063 ;; Now delete the rest of the specified region,
1064 ;; but don't record it.
1065 (setq buffer-undo-list t)
1066 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1067 (error "Lossage in mouse-save-then-kill-delete-region"))
1068 (delete-region beg end))
1069 (let ((tail buffer-undo-list))
1070 ;; Search back in buffer-undo-list for the string
1071 ;; that came from deleting one character.
1072 (while (and tail (not (stringp (car (car tail)))))
1073 (setq tail (cdr tail)))
1074 ;; Replace it with an entry for the entire deleted text.
1075 (and tail
1076 (setcar tail (cons (car kill-ring) (min beg end))))))
1077 (undo-boundary))
1079 (defun mouse-save-then-kill (click)
1080 "Save text to point in kill ring; the second time, kill the text.
1081 If the text between point and the mouse is the same as what's
1082 at the front of the kill ring, this deletes the text.
1083 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1084 which prepares for a second click to delete the text.
1086 If you have selected words or lines, this command extends the
1087 selection through the word or line clicked on. If you do this
1088 again in a different position, it extends the selection again.
1089 If you do this twice in the same position, the selection is killed."
1090 (interactive "e")
1091 (let ((before-scroll
1092 (with-current-buffer (window-buffer (posn-window (event-start click)))
1093 point-before-scroll)))
1094 (mouse-minibuffer-check click)
1095 (let ((click-posn (posn-point (event-start click)))
1096 ;; Don't let a subsequent kill command append to this one:
1097 ;; prevent setting this-command to kill-region.
1098 (this-command this-command))
1099 (if (and (save-excursion
1100 (set-buffer (window-buffer (posn-window (event-start click))))
1101 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1102 ;; Don't be fooled by a recent click in some other buffer.
1103 (eq mouse-selection-click-count-buffer
1104 (current-buffer)))))
1105 (if (not (and (eq last-command 'mouse-save-then-kill)
1106 (equal click-posn
1107 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1108 ;; Find both ends of the object selected by this click.
1109 (let* ((range
1110 (mouse-start-end click-posn click-posn
1111 mouse-selection-click-count)))
1112 ;; Move whichever end is closer to the click.
1113 ;; That's what xterm does, and it seems reasonable.
1114 (if (< (abs (- click-posn (mark t)))
1115 (abs (- click-posn (point))))
1116 (set-mark (car range))
1117 (goto-char (nth 1 range)))
1118 ;; We have already put the old region in the kill ring.
1119 ;; Replace it with the extended region.
1120 ;; (It would be annoying to make a separate entry.)
1121 (kill-new (buffer-substring (point) (mark t)) t)
1122 (mouse-set-region-1)
1123 ;; Arrange for a repeated mouse-3 to kill this region.
1124 (setq mouse-save-then-kill-posn
1125 (list (car kill-ring) (point) click-posn))
1126 (mouse-show-mark))
1127 ;; If we click this button again without moving it,
1128 ;; that time kill.
1129 (mouse-save-then-kill-delete-region (mark) (point))
1130 (setq mouse-selection-click-count 0)
1131 (setq mouse-save-then-kill-posn nil))
1132 (if (and (eq last-command 'mouse-save-then-kill)
1133 mouse-save-then-kill-posn
1134 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1135 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1136 ;; If this is the second time we've called
1137 ;; mouse-save-then-kill, delete the text from the buffer.
1138 (progn
1139 (mouse-save-then-kill-delete-region (point) (mark))
1140 ;; After we kill, another click counts as "the first time".
1141 (setq mouse-save-then-kill-posn nil))
1142 ;; This is not a repetition.
1143 ;; We are adjusting an old selection or creating a new one.
1144 (if (or (and (eq last-command 'mouse-save-then-kill)
1145 mouse-save-then-kill-posn)
1146 (and mark-active transient-mark-mode)
1147 (and (memq last-command
1148 '(mouse-drag-region mouse-set-region))
1149 (or mark-even-if-inactive
1150 (not transient-mark-mode))))
1151 ;; We have a selection or suitable region, so adjust it.
1152 (let* ((posn (event-start click))
1153 (new (posn-point posn)))
1154 (select-window (posn-window posn))
1155 (if (numberp new)
1156 (progn
1157 ;; Move whichever end of the region is closer to the click.
1158 ;; That is what xterm does, and it seems reasonable.
1159 (if (< (abs (- new (point))) (abs (- new (mark t))))
1160 (goto-char new)
1161 (set-mark new))
1162 (setq deactivate-mark nil)))
1163 (kill-new (buffer-substring (point) (mark t)) t)
1164 (mouse-show-mark))
1165 ;; Set the mark where point is, then move where clicked.
1166 (mouse-set-mark-fast click)
1167 (if before-scroll
1168 (goto-char before-scroll))
1169 (exchange-point-and-mark)
1170 (kill-new (buffer-substring (point) (mark t)))
1171 (mouse-show-mark))
1172 (mouse-set-region-1)
1173 (setq mouse-save-then-kill-posn
1174 (list (car kill-ring) (point) click-posn)))))))
1176 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1177 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1178 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1179 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1180 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1182 ;; An overlay which records the current secondary selection
1183 ;; or else is deleted when there is no secondary selection.
1184 ;; May be nil.
1185 (defvar mouse-secondary-overlay nil)
1187 (defvar mouse-secondary-click-count 0)
1189 ;; A marker which records the specified first end for a secondary selection.
1190 ;; May be nil.
1191 (defvar mouse-secondary-start nil)
1193 (defun mouse-start-secondary (click)
1194 "Set one end of the secondary selection to the position clicked on.
1195 Use \\[mouse-secondary-save-then-kill] to set the other end
1196 and complete the secondary selection."
1197 (interactive "e")
1198 (mouse-minibuffer-check click)
1199 (let ((posn (event-start click)))
1200 (save-excursion
1201 (set-buffer (window-buffer (posn-window posn)))
1202 ;; Cancel any preexisting secondary selection.
1203 (if mouse-secondary-overlay
1204 (delete-overlay mouse-secondary-overlay))
1205 (if (numberp (posn-point posn))
1206 (progn
1207 (or mouse-secondary-start
1208 (setq mouse-secondary-start (make-marker)))
1209 (move-marker mouse-secondary-start (posn-point posn)))))))
1211 (defun mouse-set-secondary (click)
1212 "Set the secondary selection to the text that the mouse is dragged over.
1213 This must be bound to a mouse drag event."
1214 (interactive "e")
1215 (mouse-minibuffer-check click)
1216 (let ((posn (event-start click))
1218 (end (event-end click)))
1219 (save-excursion
1220 (set-buffer (window-buffer (posn-window posn)))
1221 (if (numberp (posn-point posn))
1222 (setq beg (posn-point posn)))
1223 (if mouse-secondary-overlay
1224 (move-overlay mouse-secondary-overlay beg (posn-point end))
1225 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1226 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1228 (defun mouse-drag-secondary (start-event)
1229 "Set the secondary selection to the text that the mouse is dragged over.
1230 Highlight the drag area as you move the mouse.
1231 This must be bound to a button-down mouse event.
1232 The function returns a non-nil value if it creates a secondary selection."
1233 (interactive "e")
1234 (mouse-minibuffer-check start-event)
1235 (let* ((echo-keystrokes 0)
1236 (start-posn (event-start start-event))
1237 (start-point (posn-point start-posn))
1238 (start-window (posn-window start-posn))
1239 (start-frame (window-frame start-window))
1240 (bounds (window-edges start-window))
1241 (top (nth 1 bounds))
1242 (bottom (if (window-minibuffer-p start-window)
1243 (nth 3 bounds)
1244 ;; Don't count the mode line.
1245 (1- (nth 3 bounds))))
1246 (click-count (1- (event-click-count start-event))))
1247 (save-excursion
1248 (set-buffer (window-buffer start-window))
1249 (setq mouse-secondary-click-count click-count)
1250 (or mouse-secondary-overlay
1251 (setq mouse-secondary-overlay
1252 (make-overlay (point) (point))))
1253 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1254 (if (> (mod click-count 3) 0)
1255 ;; Double or triple press: make an initial selection
1256 ;; of one word or line.
1257 (let ((range (mouse-start-end start-point start-point click-count)))
1258 (set-marker mouse-secondary-start nil)
1259 (move-overlay mouse-secondary-overlay 1 1
1260 (window-buffer start-window))
1261 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1262 (window-buffer start-window)))
1263 ;; Single-press: cancel any preexisting secondary selection.
1264 (or mouse-secondary-start
1265 (setq mouse-secondary-start (make-marker)))
1266 (set-marker mouse-secondary-start start-point)
1267 (delete-overlay mouse-secondary-overlay))
1268 (let (event end end-point)
1269 (track-mouse
1270 (while (progn
1271 (setq event (read-event))
1272 (or (mouse-movement-p event)
1273 (eq (car-safe event) 'switch-frame)))
1275 (if (eq (car-safe event) 'switch-frame)
1277 (setq end (event-end event)
1278 end-point (posn-point end))
1279 (cond
1280 ;; Are we moving within the original window?
1281 ((and (eq (posn-window end) start-window)
1282 (integer-or-marker-p end-point))
1283 (let ((range (mouse-start-end start-point end-point
1284 click-count)))
1285 (if (or (/= start-point end-point)
1286 (null (marker-position mouse-secondary-start)))
1287 (progn
1288 (set-marker mouse-secondary-start nil)
1289 (move-overlay mouse-secondary-overlay
1290 (car range) (nth 1 range))))))
1292 (let ((mouse-row (cdr (cdr (mouse-position)))))
1293 (cond
1294 ((null mouse-row))
1295 ((< mouse-row top)
1296 (mouse-scroll-subr start-window (- mouse-row top)
1297 mouse-secondary-overlay start-point))
1298 ((>= mouse-row bottom)
1299 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1300 mouse-secondary-overlay start-point)))))))))
1302 (if (consp event)
1303 (if (marker-position mouse-secondary-start)
1304 (save-window-excursion
1305 (delete-overlay mouse-secondary-overlay)
1306 (x-set-selection 'SECONDARY nil)
1307 (select-window start-window)
1308 (save-excursion
1309 (goto-char mouse-secondary-start)
1310 (sit-for 1)
1311 nil))
1312 (x-set-selection
1313 'SECONDARY
1314 (buffer-substring (overlay-start mouse-secondary-overlay)
1315 (overlay-end mouse-secondary-overlay)))))))))
1317 (defun mouse-yank-secondary (click)
1318 "Insert the secondary selection at the position clicked on.
1319 Move point to the end of the inserted text.
1320 If `mouse-yank-at-point' is non-nil, insert at point
1321 regardless of where you click."
1322 (interactive "e")
1323 ;; Give temporary modes such as isearch a chance to turn off.
1324 (run-hooks 'mouse-leave-buffer-hook)
1325 (or mouse-yank-at-point (mouse-set-point click))
1326 (insert (x-get-selection 'SECONDARY)))
1328 (defun mouse-kill-secondary ()
1329 "Kill the text in the secondary selection.
1330 This is intended more as a keyboard command than as a mouse command
1331 but it can work as either one.
1333 The current buffer (in case of keyboard use), or the buffer clicked on,
1334 must be the one that the secondary selection is in. This requirement
1335 is to prevent accidents."
1336 (interactive)
1337 (let* ((keys (this-command-keys))
1338 (click (elt keys (1- (length keys)))))
1339 (or (eq (overlay-buffer mouse-secondary-overlay)
1340 (if (listp click)
1341 (window-buffer (posn-window (event-start click)))
1342 (current-buffer)))
1343 (error "Select or click on the buffer where the secondary selection is")))
1344 (let (this-command)
1345 (save-excursion
1346 (set-buffer (overlay-buffer mouse-secondary-overlay))
1347 (kill-region (overlay-start mouse-secondary-overlay)
1348 (overlay-end mouse-secondary-overlay))))
1349 (delete-overlay mouse-secondary-overlay)
1350 ;;; (x-set-selection 'SECONDARY nil)
1351 (setq mouse-secondary-overlay nil))
1353 (defun mouse-secondary-save-then-kill (click)
1354 "Save text to point in kill ring; the second time, kill the text.
1355 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1356 If the text between where you did \\[mouse-start-secondary] and where
1357 you use this command matches the text at the front of the kill ring,
1358 this command deletes the text.
1359 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1360 which prepares for a second click with this command to delete the text.
1362 If you have already made a secondary selection in that buffer,
1363 this command extends or retracts the selection to where you click.
1364 If you do this again in a different position, it extends or retracts
1365 again. If you do this twice in the same position, it kills the selection."
1366 (interactive "e")
1367 (mouse-minibuffer-check click)
1368 (let ((posn (event-start click))
1369 (click-posn (posn-point (event-start click)))
1370 ;; Don't let a subsequent kill command append to this one:
1371 ;; prevent setting this-command to kill-region.
1372 (this-command this-command))
1373 (or (eq (window-buffer (posn-window posn))
1374 (or (and mouse-secondary-overlay
1375 (overlay-buffer mouse-secondary-overlay))
1376 (if mouse-secondary-start
1377 (marker-buffer mouse-secondary-start))))
1378 (error "Wrong buffer"))
1379 (save-excursion
1380 (set-buffer (window-buffer (posn-window posn)))
1381 (if (> (mod mouse-secondary-click-count 3) 0)
1382 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1383 (equal click-posn
1384 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1385 ;; Find both ends of the object selected by this click.
1386 (let* ((range
1387 (mouse-start-end click-posn click-posn
1388 mouse-secondary-click-count)))
1389 ;; Move whichever end is closer to the click.
1390 ;; That's what xterm does, and it seems reasonable.
1391 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1392 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1393 (move-overlay mouse-secondary-overlay (car range)
1394 (overlay-end mouse-secondary-overlay))
1395 (move-overlay mouse-secondary-overlay
1396 (overlay-start mouse-secondary-overlay)
1397 (nth 1 range)))
1398 ;; We have already put the old region in the kill ring.
1399 ;; Replace it with the extended region.
1400 ;; (It would be annoying to make a separate entry.)
1401 (kill-new (buffer-substring
1402 (overlay-start mouse-secondary-overlay)
1403 (overlay-end mouse-secondary-overlay)) t)
1404 ;; Arrange for a repeated mouse-3 to kill this region.
1405 (setq mouse-save-then-kill-posn
1406 (list (car kill-ring) (point) click-posn)))
1407 ;; If we click this button again without moving it,
1408 ;; that time kill.
1409 (progn
1410 (mouse-save-then-kill-delete-region
1411 (overlay-start mouse-secondary-overlay)
1412 (overlay-end mouse-secondary-overlay))
1413 (setq mouse-save-then-kill-posn nil)
1414 (setq mouse-secondary-click-count 0)
1415 (delete-overlay mouse-secondary-overlay)))
1416 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1417 mouse-save-then-kill-posn
1418 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1419 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1420 ;; If this is the second time we've called
1421 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1422 (progn
1423 (mouse-save-then-kill-delete-region
1424 (overlay-start mouse-secondary-overlay)
1425 (overlay-end mouse-secondary-overlay))
1426 (setq mouse-save-then-kill-posn nil)
1427 (delete-overlay mouse-secondary-overlay))
1428 (if (overlay-start mouse-secondary-overlay)
1429 ;; We have a selection, so adjust it.
1430 (progn
1431 (if (numberp click-posn)
1432 (progn
1433 ;; Move whichever end of the region is closer to the click.
1434 ;; That is what xterm does, and it seems reasonable.
1435 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1436 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1437 (move-overlay mouse-secondary-overlay click-posn
1438 (overlay-end mouse-secondary-overlay))
1439 (move-overlay mouse-secondary-overlay
1440 (overlay-start mouse-secondary-overlay)
1441 click-posn))
1442 (setq deactivate-mark nil)))
1443 (if (eq last-command 'mouse-secondary-save-then-kill)
1444 ;; If the front of the kill ring comes from
1445 ;; an immediately previous use of this command,
1446 ;; replace it with the extended region.
1447 ;; (It would be annoying to make a separate entry.)
1448 (kill-new (buffer-substring
1449 (overlay-start mouse-secondary-overlay)
1450 (overlay-end mouse-secondary-overlay)) t)
1451 (let (deactivate-mark)
1452 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1453 (overlay-end mouse-secondary-overlay)))))
1454 (if mouse-secondary-start
1455 ;; All we have is one end of a selection,
1456 ;; so put the other end here.
1457 (let ((start (+ 0 mouse-secondary-start)))
1458 (kill-ring-save start click-posn)
1459 (if mouse-secondary-overlay
1460 (move-overlay mouse-secondary-overlay start click-posn)
1461 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1462 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1463 (setq mouse-save-then-kill-posn
1464 (list (car kill-ring) (point) click-posn))))
1465 (if (overlay-buffer mouse-secondary-overlay)
1466 (x-set-selection 'SECONDARY
1467 (buffer-substring
1468 (overlay-start mouse-secondary-overlay)
1469 (overlay-end mouse-secondary-overlay)))))))
1471 (defcustom mouse-buffer-menu-maxlen 20
1472 "*Number of buffers in one pane (submenu) of the buffer menu.
1473 If we have lots of buffers, divide them into groups of
1474 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1475 :type 'integer
1476 :group 'mouse)
1478 (defcustom mouse-buffer-menu-mode-mult 4
1479 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1480 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1481 will split the buffer menu by the major modes (see
1482 `mouse-buffer-menu-mode-groups') or just by menu length.
1483 Set to 1 (or even 0!) if you want to group by major mode always, and to
1484 a large number if you prefer a mixed multitude. The default is 4."
1485 :type 'integer
1486 :group 'mouse
1487 :version "20.3")
1489 (defvar mouse-buffer-menu-mode-groups
1490 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1491 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1492 . "Mail/News")
1493 ("\\<C\\>" . "C")
1494 ("ObjC" . "C")
1495 ("Text" . "Text")
1496 ("Outline" . "Text")
1497 ("Lisp" . "Lisp"))
1498 "How to group various major modes together in \\[mouse-buffer-menu].
1499 Each element has the form (REGEXP . GROUPNAME).
1500 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1502 (defun mouse-buffer-menu (event)
1503 "Pop up a menu of buffers for selection with the mouse.
1504 This switches buffers in the window that you clicked on,
1505 and selects that window."
1506 (interactive "e")
1507 (mouse-minibuffer-check event)
1508 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1509 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1510 (let ((tail buffers))
1511 (while tail
1512 ;; Divide all buffers into buckets for various major modes.
1513 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1514 (with-current-buffer (car tail)
1515 (let* ((adjusted-major-mode major-mode) elt)
1516 (let ((tail mouse-buffer-menu-mode-groups))
1517 (while tail
1518 (if (string-match (car (car tail)) mode-name)
1519 (setq adjusted-major-mode (cdr (car tail))))
1520 (setq tail (cdr tail))))
1521 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1522 (if (null elt)
1523 (setq elt (list adjusted-major-mode
1524 (if (stringp adjusted-major-mode)
1525 adjusted-major-mode
1526 mode-name))
1527 split-by-major-mode (cons elt split-by-major-mode)))
1528 (or (memq (car tail) (cdr (cdr elt)))
1529 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1530 (setq tail (cdr tail))))
1531 ;; Compute the sum of squares of sizes of the major-mode buckets.
1532 (let ((tail split-by-major-mode))
1533 (setq sum-of-squares 0)
1534 (while tail
1535 (setq sum-of-squares
1536 (+ sum-of-squares
1537 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1538 (setq tail (cdr tail))))
1539 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1540 (* (length buffers) (length buffers)))
1541 ;; Subdividing by major modes really helps, so let's do it.
1542 (let (subdivided-menus (buffers-left (length buffers)))
1543 ;; Sort the list to put the most popular major modes first.
1544 (setq split-by-major-mode
1545 (sort split-by-major-mode
1546 (function (lambda (elt1 elt2)
1547 (> (length elt1) (length elt2))))))
1548 ;; Make a separate submenu for each major mode
1549 ;; that has more than one buffer,
1550 ;; unless all the remaining buffers are less than 1/10 of them.
1551 (while (and split-by-major-mode
1552 (and (> (length (car split-by-major-mode)) 3)
1553 (> (* buffers-left 10) (length buffers))))
1554 (setq subdivided-menus
1555 (cons (cons
1556 (nth 1 (car split-by-major-mode))
1557 (mouse-buffer-menu-alist
1558 (cdr (cdr (car split-by-major-mode)))))
1559 subdivided-menus))
1560 (setq buffers-left
1561 (- buffers-left (length (cdr (car split-by-major-mode)))))
1562 (setq split-by-major-mode (cdr split-by-major-mode)))
1563 ;; If any major modes are left over,
1564 ;; make a single submenu for them.
1565 (if split-by-major-mode
1566 (setq subdivided-menus
1567 (cons (cons
1568 "Others"
1569 (mouse-buffer-menu-alist
1570 ;; we don't need split-by-major-mode any
1571 ;; more, so we can ditch it with nconc.
1572 (apply 'nconc (mapcar 'cddr split-by-major-mode))))
1573 subdivided-menus)))
1574 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1575 (progn
1576 (setq alist (mouse-buffer-menu-alist buffers))
1577 (setq menu (cons "Buffer Menu"
1578 (mouse-buffer-menu-split "Select Buffer" alist)))))
1579 (let ((buf (x-popup-menu event menu))
1580 (window (posn-window (event-start event))))
1581 (when buf
1582 (or (framep window) (select-window window))
1583 (switch-to-buffer buf)))))
1585 (defun mouse-buffer-menu-alist (buffers)
1586 (let (tail
1587 (maxlen 0)
1588 head)
1589 (setq buffers
1590 (sort buffers
1591 (function (lambda (elt1 elt2)
1592 (string< (buffer-name elt1) (buffer-name elt2))))))
1593 (setq tail buffers)
1594 (while tail
1595 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1596 (setq maxlen
1597 (max maxlen
1598 (length (buffer-name (car tail))))))
1599 (setq tail (cdr tail)))
1600 (setq tail buffers)
1601 (while tail
1602 (let ((elt (car tail)))
1603 (if (/= (aref (buffer-name elt) 0) ?\ )
1604 (setq head
1605 (cons
1606 (cons
1607 (format
1608 (format "%%%ds %%s%%s %%s" maxlen)
1609 (buffer-name elt)
1610 (if (buffer-modified-p elt) "*" " ")
1611 (save-excursion
1612 (set-buffer elt)
1613 (if buffer-read-only "%" " "))
1614 (or (buffer-file-name elt)
1615 (save-excursion
1616 (set-buffer elt)
1617 (if list-buffers-directory
1618 (expand-file-name
1619 list-buffers-directory)))
1620 ""))
1621 elt)
1622 head))))
1623 (setq tail (cdr tail)))
1624 ;; Compensate for the reversal that the above loop does.
1625 (nreverse head)))
1627 (defun mouse-buffer-menu-split (title alist)
1628 ;; If we have lots of buffers, divide them into groups of 20
1629 ;; and make a pane (or submenu) for each one.
1630 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1631 (let ((alist alist) sublists next
1632 (i 1))
1633 (while alist
1634 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1635 ;; and make them the next element of sublist.
1636 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1637 (if next
1638 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1639 nil))
1640 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1641 sublists))
1642 (setq i (1+ i))
1643 (setq alist next))
1644 (nreverse sublists))
1645 ;; Few buffers--put them all in one pane.
1646 (list (cons title alist))))
1648 ;;; These need to be rewritten for the new scroll bar implementation.
1650 ;;;!! ;; Commands for the scroll bar.
1651 ;;;!!
1652 ;;;!! (defun mouse-scroll-down (click)
1653 ;;;!! (interactive "@e")
1654 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1655 ;;;!!
1656 ;;;!! (defun mouse-scroll-up (click)
1657 ;;;!! (interactive "@e")
1658 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1659 ;;;!!
1660 ;;;!! (defun mouse-scroll-down-full ()
1661 ;;;!! (interactive "@")
1662 ;;;!! (scroll-down nil))
1663 ;;;!!
1664 ;;;!! (defun mouse-scroll-up-full ()
1665 ;;;!! (interactive "@")
1666 ;;;!! (scroll-up nil))
1667 ;;;!!
1668 ;;;!! (defun mouse-scroll-move-cursor (click)
1669 ;;;!! (interactive "@e")
1670 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1671 ;;;!!
1672 ;;;!! (defun mouse-scroll-absolute (event)
1673 ;;;!! (interactive "@e")
1674 ;;;!! (let* ((pos (car event))
1675 ;;;!! (position (car pos))
1676 ;;;!! (length (car (cdr pos))))
1677 ;;;!! (if (<= length 0) (setq length 1))
1678 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1679 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1680 ;;;!! position)
1681 ;;;!! length)
1682 ;;;!! scale-factor)))
1683 ;;;!! (goto-char newpos)
1684 ;;;!! (recenter '(4)))))
1685 ;;;!!
1686 ;;;!! (defun mouse-scroll-left (click)
1687 ;;;!! (interactive "@e")
1688 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1689 ;;;!!
1690 ;;;!! (defun mouse-scroll-right (click)
1691 ;;;!! (interactive "@e")
1692 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1693 ;;;!!
1694 ;;;!! (defun mouse-scroll-left-full ()
1695 ;;;!! (interactive "@")
1696 ;;;!! (scroll-left nil))
1697 ;;;!!
1698 ;;;!! (defun mouse-scroll-right-full ()
1699 ;;;!! (interactive "@")
1700 ;;;!! (scroll-right nil))
1701 ;;;!!
1702 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1703 ;;;!! (interactive "@e")
1704 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1705 ;;;!!
1706 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1707 ;;;!! (interactive "@e")
1708 ;;;!! (let* ((pos (car event))
1709 ;;;!! (position (car pos))
1710 ;;;!! (length (car (cdr pos))))
1711 ;;;!! (set-window-hscroll (selected-window) 33)))
1712 ;;;!!
1713 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1714 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1715 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1716 ;;;!!
1717 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1718 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1719 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1720 ;;;!!
1721 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1722 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1723 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1724 ;;;!!
1725 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1726 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1727 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1728 ;;;!!
1729 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1730 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1731 ;;;!! 'mouse-scroll-absolute-horizontally)
1732 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1733 ;;;!!
1734 ;;;!! (global-set-key [horizontal-slider mouse-1]
1735 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1736 ;;;!! (global-set-key [horizontal-slider mouse-2]
1737 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1738 ;;;!! (global-set-key [horizontal-slider mouse-3]
1739 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1740 ;;;!!
1741 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1742 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1743 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1744 ;;;!!
1745 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1746 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1747 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1748 ;;;!!
1749 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1750 ;;;!! 'mouse-split-window-horizontally)
1751 ;;;!! (global-set-key [mode-line S-mouse-2]
1752 ;;;!! 'mouse-split-window-horizontally)
1753 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1754 ;;;!! 'mouse-split-window)
1756 ;;;!! ;;;;
1757 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1758 ;;;!! ;;;; are of the form:
1759 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1760 ;;;!! ;;
1761 ;;;!! ;;;;
1762 ;;;!! ;;;; Dynamically track mouse coordinates
1763 ;;;!! ;;;;
1764 ;;;!! ;;
1765 ;;;!! ;;(defun track-mouse (event)
1766 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1767 ;;;!! ;; (interactive "@e")
1768 ;;;!! ;; (while mouse-grabbed
1769 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1770 ;;;!! ;; (abs-x (car pos))
1771 ;;;!! ;; (abs-y (cdr pos))
1772 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1773 ;;;!! ;; (list (car pos) (cdr pos))
1774 ;;;!! ;; (selected-window))))
1775 ;;;!! ;; (if (consp relative-coordinate)
1776 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1777 ;;;!! ;; (car relative-coordinate)
1778 ;;;!! ;; (car (cdr relative-coordinate)))
1779 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1780 ;;;!!
1781 ;;;!! ;;
1782 ;;;!! ;; Dynamically put a box around the line indicated by point
1783 ;;;!! ;;
1784 ;;;!! ;;
1785 ;;;!! ;;(require 'backquote)
1786 ;;;!! ;;
1787 ;;;!! ;;(defun mouse-select-buffer-line (event)
1788 ;;;!! ;; (interactive "@e")
1789 ;;;!! ;; (let ((relative-coordinate
1790 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1791 ;;;!! ;; (abs-y (car (cdr (car event)))))
1792 ;;;!! ;; (if (consp relative-coordinate)
1793 ;;;!! ;; (progn
1794 ;;;!! ;; (save-excursion
1795 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1796 ;;;!! ;; (x-draw-rectangle
1797 ;;;!! ;; (selected-screen)
1798 ;;;!! ;; abs-y 0
1799 ;;;!! ;; (save-excursion
1800 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1801 ;;;!! ;; (end-of-line)
1802 ;;;!! ;; (push-mark nil t)
1803 ;;;!! ;; (beginning-of-line)
1804 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1805 ;;;!! ;; (sit-for 1)
1806 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1807 ;;;!! ;;
1808 ;;;!! ;;(defvar last-line-drawn nil)
1809 ;;;!! ;;(defvar begin-delim "[^ \t]")
1810 ;;;!! ;;(defvar end-delim "[^ \t]")
1811 ;;;!! ;;
1812 ;;;!! ;;(defun mouse-boxing (event)
1813 ;;;!! ;; (interactive "@e")
1814 ;;;!! ;; (save-excursion
1815 ;;;!! ;; (let ((screen (selected-screen)))
1816 ;;;!! ;; (while (= (x-mouse-events) 0)
1817 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1818 ;;;!! ;; (abs-x (car pos))
1819 ;;;!! ;; (abs-y (cdr pos))
1820 ;;;!! ;; (relative-coordinate
1821 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1822 ;;;!! ;; (selected-window)))
1823 ;;;!! ;; (begin-reg nil)
1824 ;;;!! ;; (end-reg nil)
1825 ;;;!! ;; (end-column nil)
1826 ;;;!! ;; (begin-column nil))
1827 ;;;!! ;; (if (and (consp relative-coordinate)
1828 ;;;!! ;; (or (not last-line-drawn)
1829 ;;;!! ;; (not (= last-line-drawn abs-y))))
1830 ;;;!! ;; (progn
1831 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1832 ;;;!! ;; (if (= (following-char) 10)
1833 ;;;!! ;; ()
1834 ;;;!! ;; (progn
1835 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1836 ;;;!! ;; (setq begin-column (1- (current-column)))
1837 ;;;!! ;; (end-of-line)
1838 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1839 ;;;!! ;; (setq end-column (1+ (current-column)))
1840 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1841 ;;;!! ;; (x-draw-rectangle screen
1842 ;;;!! ;; (setq last-line-drawn abs-y)
1843 ;;;!! ;; begin-column
1844 ;;;!! ;; (- end-column begin-column) 1))))))))))
1845 ;;;!! ;;
1846 ;;;!! ;;(defun mouse-erase-box ()
1847 ;;;!! ;; (interactive)
1848 ;;;!! ;; (if last-line-drawn
1849 ;;;!! ;; (progn
1850 ;;;!! ;; (x-erase-rectangle (selected-screen))
1851 ;;;!! ;; (setq last-line-drawn nil))))
1852 ;;;!!
1853 ;;;!! ;;; (defun test-x-rectangle ()
1854 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1855 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1856 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1857 ;;;!!
1858 ;;;!! ;;
1859 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1860 ;;;!! ;;
1861 ;;;!!
1862 ;;;!! (defvar double-start nil)
1863 ;;;!! (defconst double-click-interval 300
1864 ;;;!! "Max ticks between clicks")
1865 ;;;!!
1866 ;;;!! (defun double-down (event)
1867 ;;;!! (interactive "@e")
1868 ;;;!! (if double-start
1869 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1870 ;;;!! (if (< interval double-click-interval)
1871 ;;;!! (progn
1872 ;;;!! (backward-up-list 1)
1873 ;;;!! ;; (message "Interval %d" interval)
1874 ;;;!! (sleep-for 1)))
1875 ;;;!! (setq double-start nil))
1876 ;;;!! (setq double-start (nth 4 event))))
1877 ;;;!!
1878 ;;;!! (defun double-up (event)
1879 ;;;!! (interactive "@e")
1880 ;;;!! (and double-start
1881 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1882 ;;;!! (setq double-start nil)))
1883 ;;;!!
1884 ;;;!! ;;; (defun x-test-doubleclick ()
1885 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1886 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1887 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1888 ;;;!!
1889 ;;;!! ;;
1890 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1891 ;;;!! ;;
1892 ;;;!!
1893 ;;;!! (defvar scrolled-lines 0)
1894 ;;;!! (defconst scroll-speed 1)
1895 ;;;!!
1896 ;;;!! (defun incr-scroll-down (event)
1897 ;;;!! (interactive "@e")
1898 ;;;!! (setq scrolled-lines 0)
1899 ;;;!! (incremental-scroll scroll-speed))
1900 ;;;!!
1901 ;;;!! (defun incr-scroll-up (event)
1902 ;;;!! (interactive "@e")
1903 ;;;!! (setq scrolled-lines 0)
1904 ;;;!! (incremental-scroll (- scroll-speed)))
1905 ;;;!!
1906 ;;;!! (defun incremental-scroll (n)
1907 ;;;!! (while (= (x-mouse-events) 0)
1908 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1909 ;;;!! (scroll-down n)
1910 ;;;!! (sit-for 300 t)))
1911 ;;;!!
1912 ;;;!! (defun incr-scroll-stop (event)
1913 ;;;!! (interactive "@e")
1914 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1915 ;;;!! (setq scrolled-lines 0)
1916 ;;;!! (sleep-for 1))
1917 ;;;!!
1918 ;;;!! ;;; (defun x-testing-scroll ()
1919 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1920 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1921 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1922 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1923 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1924 ;;;!!
1925 ;;;!! ;;
1926 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1927 ;;;!! ;;
1928 ;;;!!
1929 ;;;!! (defun mouse-kill-rectangle (event)
1930 ;;;!! "Kill the rectangle between point and the mouse cursor."
1931 ;;;!! (interactive "@e")
1932 ;;;!! (let ((point-save (point)))
1933 ;;;!! (save-excursion
1934 ;;;!! (mouse-set-point event)
1935 ;;;!! (push-mark nil t)
1936 ;;;!! (if (> point-save (point))
1937 ;;;!! (kill-rectangle (point) point-save)
1938 ;;;!! (kill-rectangle point-save (point))))))
1939 ;;;!!
1940 ;;;!! (defun mouse-open-rectangle (event)
1941 ;;;!! "Kill the rectangle between point and the mouse cursor."
1942 ;;;!! (interactive "@e")
1943 ;;;!! (let ((point-save (point)))
1944 ;;;!! (save-excursion
1945 ;;;!! (mouse-set-point event)
1946 ;;;!! (push-mark nil t)
1947 ;;;!! (if (> point-save (point))
1948 ;;;!! (open-rectangle (point) point-save)
1949 ;;;!! (open-rectangle point-save (point))))))
1950 ;;;!!
1951 ;;;!! ;; Must be a better way to do this.
1952 ;;;!!
1953 ;;;!! (defun mouse-multiple-insert (n char)
1954 ;;;!! (while (> n 0)
1955 ;;;!! (insert char)
1956 ;;;!! (setq n (1- n))))
1957 ;;;!!
1958 ;;;!! ;; What this could do is not finalize until button was released.
1959 ;;;!!
1960 ;;;!! (defun mouse-move-text (event)
1961 ;;;!! "Move text from point to cursor position, inserting spaces."
1962 ;;;!! (interactive "@e")
1963 ;;;!! (let* ((relative-coordinate
1964 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1965 ;;;!! (if (consp relative-coordinate)
1966 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1967 ;;;!! (delete-char
1968 ;;;!! (- (car relative-coordinate) (current-column))))
1969 ;;;!! ((< (current-column) (car relative-coordinate))
1970 ;;;!! (mouse-multiple-insert
1971 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1972 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1974 ;; Choose a completion with the mouse.
1976 (defun mouse-choose-completion (event)
1977 "Click on an alternative in the `*Completions*' buffer to choose it."
1978 (interactive "e")
1979 ;; Give temporary modes such as isearch a chance to turn off.
1980 (run-hooks 'mouse-leave-buffer-hook)
1981 (let ((buffer (window-buffer))
1982 choice
1983 base-size)
1984 (save-excursion
1985 (set-buffer (window-buffer (posn-window (event-start event))))
1986 (if completion-reference-buffer
1987 (setq buffer completion-reference-buffer))
1988 (setq base-size completion-base-size)
1989 (save-excursion
1990 (goto-char (posn-point (event-start event)))
1991 (let (beg end)
1992 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1993 (setq end (point) beg (1+ (point))))
1994 (if (null beg)
1995 (error "No completion here"))
1996 (setq beg (previous-single-property-change beg 'mouse-face))
1997 (setq end (or (next-single-property-change end 'mouse-face)
1998 (point-max)))
1999 (setq choice (buffer-substring beg end)))))
2000 (let ((owindow (selected-window)))
2001 (select-window (posn-window (event-start event)))
2002 (if (and (one-window-p t 'selected-frame)
2003 (window-dedicated-p (selected-window)))
2004 ;; This is a special buffer's frame
2005 (iconify-frame (selected-frame))
2006 (or (window-dedicated-p (selected-window))
2007 (bury-buffer)))
2008 (select-window owindow))
2009 (choose-completion-string choice buffer base-size)))
2011 ;; Font selection.
2013 (defun font-menu-add-default ()
2014 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2015 (font-alist x-fixed-font-alist)
2016 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2017 (if (assoc "Default" elt)
2018 (delete (assoc "Default" elt) elt))
2019 (setcdr elt
2020 (cons (list "Default" default)
2021 (cdr elt)))))
2023 (defvar x-fixed-font-alist
2024 '("Font menu"
2025 ("Misc"
2026 ;; For these, we specify the pixel height and width.
2027 ("fixed" "fixed")
2028 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2029 ("6x12"
2030 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2031 ("6x13"
2032 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2033 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2034 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2035 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2036 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2037 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2038 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2039 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2040 ("")
2041 ("clean 5x8"
2042 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2043 ("clean 6x8"
2044 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2045 ("clean 8x8"
2046 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2047 ("clean 8x10"
2048 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2049 ("clean 8x14"
2050 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2051 ("clean 8x16"
2052 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2053 ("")
2054 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2055 ;;; We don't seem to have these; who knows what they are.
2056 ;;; ("fg-18" "fg-18")
2057 ;;; ("fg-25" "fg-25")
2058 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2059 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2060 ("lucidasanstypewriter-bold-24"
2061 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2062 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2063 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2065 ("Courier"
2066 ;; For these, we specify the point height.
2067 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2068 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2069 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2070 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2071 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2072 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2073 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2074 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2075 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2076 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2077 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2078 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2079 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2080 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2081 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2082 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2083 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2084 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2085 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2086 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2087 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2088 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2089 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2090 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2092 "X fonts suitable for use in Emacs.")
2094 (defun mouse-set-font (&rest fonts)
2095 "Select an emacs font from a list of known good fonts and fontsets."
2096 (interactive
2097 (x-popup-menu
2098 last-nonmenu-event
2099 ;; Append list of fontsets currently defined.
2100 (append x-fixed-font-alist (list (generate-fontset-menu)))))
2101 (if fonts
2102 (let (font)
2103 (while fonts
2104 (condition-case nil
2105 (progn
2106 (set-default-font (car fonts))
2107 (setq font (car fonts))
2108 (setq fonts nil))
2109 (error
2110 (setq fonts (cdr fonts)))))
2111 (if (null font)
2112 (error "Font not found")))))
2114 ;;; Bindings for mouse commands.
2116 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2117 (global-set-key [mouse-1] 'mouse-set-point)
2118 (global-set-key [drag-mouse-1] 'mouse-set-region)
2120 ;; These are tested for in mouse-drag-region.
2121 (global-set-key [double-mouse-1] 'mouse-set-point)
2122 (global-set-key [triple-mouse-1] 'mouse-set-point)
2124 (global-set-key [mouse-2] 'mouse-yank-at-click)
2125 (global-set-key [mouse-3] 'mouse-save-then-kill)
2127 ;; By binding these to down-going events, we let the user use the up-going
2128 ;; event to make the selection, saving a click.
2129 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2130 (if (not (eq system-type 'ms-dos))
2131 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2132 ;; C-down-mouse-2 is bound in facemenu.el.
2133 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2136 ;; Replaced with dragging mouse-1
2137 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2139 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2140 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2141 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2142 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2143 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2144 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2145 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2146 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2147 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2148 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2149 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2151 (provide 'mouse)
2153 ;; This file contains the functionality of the old mldrag.el.
2154 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2155 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2156 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2157 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2158 (provide 'mldrag)
2160 ;;; mouse.el ends here