1 ;;; mouse.el --- window system-independent mouse support.
3 ;;; Copyright (C) 1993 Free Software Foundation, Inc.
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
22 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This package provides various useful commands (including help
27 ;; system access) through the mouse. All this code assumes that mouse
28 ;; interpretation has been abstracted into Emacs input events.
30 ;; The code is rather X-dependent.
34 ;;; Utility functions.
36 ;;; Indent track-mouse like progn.
37 (put 'track-mouse 'lisp-indent-function 0)
40 (defun mouse-delete-window (click)
41 "Delete the window you click on.
42 This must be bound to a mouse click."
44 (delete-window (posn-window (event-start click))))
46 (defun mouse-tear-off-window (click)
47 "Delete the window clicked on, and create a new frame displaying its buffer."
49 (let* ((window (posn-window (event-start click)))
50 (buf (window-buffer window))
53 (switch-to-buffer buf)
54 (delete-window window)))
56 (defun mouse-delete-other-windows ()
57 "Delete all window except the one you click on."
59 (delete-other-windows))
61 (defun mouse-split-window-vertically (click)
62 "Select Emacs window mouse is on, then split it vertically in half.
63 The window is split at the line clicked on.
64 This command must be bound to a mouse click."
66 (let ((start (event-start click)))
67 (select-window (posn-window start))
68 (let ((new-height (if (eq (posn-point start) 'vertical-scroll-bar)
69 (scroll-bar-scale (posn-col-row start)
71 (1+ (cdr (posn-col-row (event-end click))))))
72 (first-line window-min-height)
73 (last-line (- (window-height) window-min-height)))
74 (if (< last-line first-line)
75 (error "window too short to split")
76 (split-window-vertically
77 (min (max new-height first-line) last-line))))))
79 (defun mouse-split-window-horizontally (click)
80 "Select Emacs window mouse is on, then split it horizontally in half.
81 The window is split at the column clicked on.
82 This command must be bound to a mouse click."
84 (let ((start (event-start click)))
85 (select-window (posn-window start))
86 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
87 (first-col window-min-width)
88 (last-col (- (window-width) window-min-width)))
89 (if (< last-col first-col)
90 (error "window too narrow to split")
91 (split-window-horizontally
92 (min (max new-width first-col) last-col))))))
94 (defun mouse-set-point (event)
95 "Move point to the position clicked on with the mouse.
96 This should be bound to a mouse click event type."
98 ;; Use event-end in case called from mouse-drag-region.
99 ;; If EVENT is a click, event-end and event-start give same value.
100 (let ((posn (event-end event)))
101 (and (window-minibuffer-p (posn-window posn))
102 (not (minibuffer-window-active-p (posn-window posn)))
103 (error "Minibuffer window is not active"))
104 (select-window (posn-window posn))
105 (if (numberp (posn-point posn))
106 (goto-char (posn-point posn)))))
108 (defun mouse-set-region (click)
109 "Set the region to the text dragged over, and copy to kill ring.
110 This should be bound to a mouse drag event."
112 (let ((posn (event-start click))
113 (end (event-end click)))
114 (select-window (posn-window posn))
115 (if (numberp (posn-point posn))
116 (goto-char (posn-point posn)))
117 ;; If mark is highlighted, no need to bounce the cursor.
118 (or (and transient-mark-mode
119 (eq (framep (selected-frame)) 'x))
123 (if (numberp (posn-point end))
124 (goto-char (posn-point end)))
125 ;; Don't set this-command to kill-region, so that a following
126 ;; C-w will not double the text in the kill ring.
128 (copy-region-as-kill (mark) (point)))))
130 (defvar mouse-scroll-delay 0.25
131 "*The pause between scroll steps caused by mouse drags, in seconds.
132 If you drag the mouse beyond the edge of a window, Emacs scrolls the
133 window to bring the text beyond that edge into view, with a delay of
134 this many seconds between scroll steps. Scrolling stops when you move
135 the mouse back into the window, or release the button.
136 This variable's value may be non-integral.
137 Setting this to zero causes Emacs to scroll as fast as it can.")
139 (defun mouse-scroll-subr (jump &optional overlay start)
140 "Scroll the selected window JUMP lines at a time, until new input arrives.
141 If OVERLAY is an overlay, let it stretch from START to the far edge of
142 the newly visible text.
143 Upon exit, point is at the far edge of the newly visible text."
145 (goto-char (window-start))
146 (if (not (zerop (vertical-motion jump)))
148 (set-window-start (selected-window) (point))
151 (goto-char (window-end (selected-window)))
152 ;; window-end doesn't reflect the window's new
153 ;; start position until the next redisplay. Hurrah.
154 (vertical-motion (1- jump)))
155 (goto-char (window-start (selected-window))))
157 (move-overlay overlay start (point)))
159 (sit-for mouse-scroll-delay))))))
162 (defvar mouse-drag-overlay (make-overlay 1 1))
163 (overlay-put mouse-drag-overlay 'face 'region)
165 (defun mouse-drag-region (start-event)
166 "Set the region to the text that the mouse is dragged over.
167 Highlight the drag area as you move the mouse.
168 This must be bound to a button-down mouse event.
169 In Transient Mark mode, the highlighting remains once you
170 release the mouse button. Otherwise, it does not."
172 (let* ((start-posn (event-start start-event))
173 (start-point (posn-point start-posn))
174 (start-window (posn-window start-posn))
175 (start-frame (window-frame start-window))
176 (bounds (window-edges start-window))
178 (bottom (if (window-minibuffer-p start-window)
180 ;; Don't count the mode line.
181 (1- (nth 3 bounds))))
182 (click-count (1- (event-click-count start-event))))
183 (mouse-set-point start-event)
184 (let ((range (mouse-start-end start-point start-point click-count)))
185 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
186 (window-buffer start-window)))
188 (let (event end end-point)
191 (setq event (read-event))
192 (or (mouse-movement-p event)
193 (eq (car-safe event) 'switch-frame)))
195 (if (eq (car-safe event) 'switch-frame)
197 (setq end (event-end event)
198 end-point (posn-point end))
202 ;; Ignore switch-frame events.
203 ((eq (car-safe event) 'switch-frame))
205 ;; Are we moving within the original window?
206 ((and (eq (posn-window end) start-window)
207 (integer-or-marker-p end-point))
208 (goto-char end-point)
209 (let ((range (mouse-start-end start-point (point) click-count)))
210 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
212 ;; Are we moving on a different window on the same frame?
213 ((and (windowp (posn-window end))
214 (eq (window-frame (posn-window end)) start-frame))
216 (+ (nth 1 (window-edges (posn-window end)))
217 (cdr (posn-col-row end)))))
221 (- mouse-row top) mouse-drag-overlay start-point))
223 (>= mouse-row bottom))
224 (mouse-scroll-subr (1+ (- mouse-row bottom))
225 mouse-drag-overlay start-point)))))
228 (let ((mouse-y (cdr (cdr (mouse-position))))
229 (menu-bar-lines (or (cdr (assq 'menu-bar-lines
233 ;; Are we on the menu bar?
234 (and (integerp mouse-y) (< mouse-y menu-bar-lines)
235 (mouse-scroll-subr (- mouse-y menu-bar-lines)
236 mouse-drag-overlay start-point))))))))
238 (if (and (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
239 (eq (posn-window (event-end event)) start-window)
240 (numberp (posn-point (event-end event))))
241 (let ((fun (key-binding (vector (car event)))))
242 (if (memq fun '(mouse-set-region mouse-set-point))
244 (push-mark (overlay-start mouse-drag-overlay) t t)
245 (goto-char (overlay-end mouse-drag-overlay)))
247 (funcall fun event)))))
248 (delete-overlay mouse-drag-overlay))))
250 ;; Commands to handle xterm-style multiple clicks.
252 (defun mouse-skip-word (dir)
253 "Skip over word, over whitespace, or over identical punctuation.
254 If DIR is positive skip forward; if negative, skip backward."
255 (let* ((char (following-char))
256 (syntax (char-to-string (char-syntax char))))
257 (if (or (string= syntax "w") (string= syntax " "))
259 (skip-syntax-backward syntax)
260 (skip-syntax-forward syntax))
262 (while (= (preceding-char) char)
264 (while (= (following-char) char)
265 (forward-char 1))))))
267 ;; Return a list of region bounds based on START and END according to MODE.
268 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
269 ;; If MODE is 1 then set point to start of word at (min START END),
270 ;; mark to end of word at (max START END).
271 ;; If MODE is 2 then do the same for lines.
272 ;; Optional KEEP-END if non-nil means do not change end.
273 (defun mouse-start-end (start end mode &optional keep-end)
282 (= (char-syntax (char-after start)) ?\())
283 (list start (save-excursion (forward-sexp 1) (point))))
286 (= (char-syntax (char-after start)) ?\)))
287 (list (save-excursion
288 (goto-char (1+ start))
292 (list (save-excursion
301 (list (save-excursion
303 (beginning-of-line 1)
310 ;; Subroutine: set the mark where CLICK happened,
311 ;; but don't do anything else.
312 (defun mouse-set-mark-fast (click)
313 (let ((posn (event-start click)))
314 (select-window (posn-window posn))
315 (if (numberp (posn-point posn))
316 (push-mark (posn-point posn) t t))))
318 ;; Momentarily show where the mark is, if highlighting doesn't show it.
319 (defun mouse-show-mark ()
320 (or transient-mark-mode
325 (defun mouse-set-mark (click)
326 "Set mark at the position clicked on with the mouse.
327 Display cursor at that position for a second.
328 This must be bound to a mouse click."
330 (let ((point-save (point)))
332 (progn (mouse-set-point click)
334 (or transient-mark-mode
336 (goto-char point-save))))
338 (defun mouse-kill (click)
339 "Kill the region between point and the mouse click.
340 The text is saved in the kill ring, as with \\[kill-region]."
342 (let ((click-posn (posn-point (event-start click))))
343 (if (numberp click-posn)
344 (kill-region (min (point) click-posn)
345 (max (point) click-posn)))))
347 (defun mouse-yank-at-click (click arg)
348 "Insert the last stretch of killed text at the position clicked on.
349 Prefix arguments are interpreted as with \\[yank]."
351 (mouse-set-point click)
354 (defun mouse-kill-ring-save (click)
355 "Copy the region between point and the mouse click in the kill ring.
356 This does not delete the region; it acts like \\[kill-ring-save]."
358 (mouse-set-mark-fast click)
359 (kill-ring-save (point) (mark t))
362 ;;; This function used to delete the text between point and the mouse
363 ;;; whenever it was equal to the front of the kill ring, but some
364 ;;; people found that confusing.
366 ;;; A list (TEXT START END), describing the text and position of the last
367 ;;; invocation of mouse-save-then-kill.
368 (defvar mouse-save-then-kill-posn nil)
370 (defun mouse-save-then-kill (click)
371 "Save text to point in kill ring; the second time, kill the text.
372 If the text between point and the mouse is the same as what's
373 at the front of the kill ring, this deletes the text.
374 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
375 which prepares for a second click to delete the text."
377 (let ((click-posn (posn-point (event-start click)))
378 ;; Don't let a subsequent kill command append to this one:
379 ;; prevent setting this-command to kill-region.
380 (this-command this-command))
381 (if (and (eq last-command 'mouse-save-then-kill)
382 mouse-save-then-kill-posn
383 (eq (car mouse-save-then-kill-posn) (car kill-ring))
384 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
385 ;; If this is the second time we've called
386 ;; mouse-save-then-kill, delete the text from the buffer.
388 ;; Delete just one char, so in case buffer is being modified
389 ;; for the first time, the undo list records that fact.
390 (delete-region (point)
391 (+ (point) (if (> (mark) (point)) 1 -1)))
392 ;; Now delete the rest of the specified region,
393 ;; but don't record it.
394 (let ((buffer-undo-list t))
395 (delete-region (point) (mark)))
396 (if (not (eq buffer-undo-list t))
397 (let ((tail buffer-undo-list))
398 ;; Search back in buffer-undo-list for the string
399 ;; that came from the first delete-region.
400 (while (and tail (not (stringp (car (car tail)))))
401 (setq tail (cdr tail)))
402 ;; Replace it with an entry for the entire deleted text.
404 (setcar tail (cons (car kill-ring) (point)))))))
405 ;; Otherwise, save this region.
406 (mouse-set-mark-fast click)
407 (kill-ring-save (point) (mark t))
409 (setq mouse-save-then-kill-posn
410 (list (car kill-ring) (point) click-posn)))))
412 (global-set-key [M-mouse-1] 'mouse-start-secondary)
413 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
414 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
415 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
416 (global-set-key [M-mouse-2] 'mouse-kill-secondary)
418 ;; An overlay which records the current secondary selection
419 ;; or else is deleted when there is no secondary selection.
421 (defvar mouse-secondary-overlay nil)
423 ;; A marker which records the specified first end for a secondary selection.
425 (defvar mouse-secondary-start nil)
427 (defun mouse-start-secondary (click)
428 "Set one end of the secondary selection to the position clicked on.
429 Use \\[mouse-secondary-save-then-kill] to set the other end
430 and complete the secondary selection."
432 (let ((posn (event-start click)))
434 (set-buffer (window-buffer (posn-window posn)))
435 ;; Cancel any preexisting secondary selection.
436 (if mouse-secondary-overlay
437 (delete-overlay mouse-secondary-overlay))
438 (if (numberp (posn-point posn))
440 (or mouse-secondary-start
441 (setq mouse-secondary-start (make-marker)))
442 (move-marker mouse-secondary-start (posn-point posn)))))))
444 (defun mouse-set-secondary (click)
445 "Set the secondary selection to the text that the mouse is dragged over.
446 This must be bound to a mouse drag event."
448 (let ((posn (event-start click))
450 (end (event-end click)))
452 (set-buffer (window-buffer (posn-window posn)))
453 (if (numberp (posn-point posn))
454 (setq beg (posn-point posn)))
455 (if mouse-secondary-overlay
456 (move-overlay mouse-secondary-overlay beg (posn-point end))
457 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
458 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
460 (defun mouse-drag-secondary (click)
461 "Set the secondary selection to the text that the mouse is dragged over.
462 This must be bound to a button-down mouse event."
464 (let ((posn (event-start click)))
465 (save-window-excursion
466 (select-window (posn-window posn))
467 ;; Set point temporarily, so user sees where it is.
469 (if (numberp (posn-point posn))
470 (goto-char (posn-point posn)))
471 (setq unread-command-events
472 (cons (read-event) unread-command-events))))))
474 (defun mouse-kill-secondary ()
475 "Kill the text in the secondary selection."
477 (kill-region (overlay-start mouse-secondary-overlay)
478 (overlay-end mouse-secondary-overlay))
479 (delete-overlay mouse-secondary-overlay)
480 (setq mouse-secondary-overlay nil))
482 (defun mouse-secondary-save-then-kill (click)
483 "Save text to secondary start point in kill ring; if twice, kill it.
484 If the text between secondary start point and the mouse is the same as what's
485 at the front of the kill ring, this deletes the text.
486 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
487 which prepares for a second click to delete the text."
489 (let ((click-posn (posn-point (event-start click)))
490 (start (+ 0 mouse-secondary-start))
491 ;; Don't let a subsequent kill command append to this one:
492 ;; prevent setting this-command to kill-region.
493 (this-command this-command))
494 (if (and (eq last-command 'mouse-secondary-save-then-kill)
495 mouse-save-then-kill-posn
496 (eq (car mouse-save-then-kill-posn) (car kill-ring))
497 (equal (cdr mouse-save-then-kill-posn)
498 (list start click-posn)))
499 ;; If this is the second time we've called
500 ;; mouse-save-then-kill, delete the text from the buffer.
502 (let ((buffer-undo-list t))
503 (delete-overlay mouse-secondary-overlay)
504 (delete-region start click-posn))
505 ;; Make the undo list by hand so it is shared.
506 (if (not (eq buffer-undo-list t))
507 (setq buffer-undo-list
508 (cons (cons (car kill-ring) (marker-position start))
510 ;; Otherwise, save this region.
512 (set-buffer (window-buffer (posn-window (event-start click))))
513 (kill-ring-save start click-posn)
514 (if mouse-secondary-overlay
515 (move-overlay mouse-secondary-overlay start click-posn)
516 (setq mouse-secondary-overlay (make-overlay start click-posn)))
517 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
518 (setq mouse-save-then-kill-posn
519 (list (car kill-ring) start click-posn))))))
521 (defun mouse-buffer-menu (event)
522 "Pop up a menu of buffers for selection with the mouse.
523 This switches buffers in the window that you clicked on,
524 and selects that window."
528 (cons "Select Buffer"
529 (let ((tail (buffer-list))
533 (or (eq ?\ (aref (buffer-name (car tail)) 0))
536 (length (buffer-name (car tail))))))
537 (setq tail (cdr tail)))
538 (setq tail (buffer-list))
540 (let ((elt (car tail)))
541 (if (not (string-match "^ "
546 (format "%%%ds %%s%%s %%s"
549 (if (buffer-modified-p elt)
553 (if buffer-read-only "%" " "))
554 (or (buffer-file-name elt) ""))
557 (setq tail (cdr tail)))
559 (let ((buf (x-popup-menu event menu))
560 (window (posn-window (event-start event))))
563 (or (framep window) (select-window window))
564 (switch-to-buffer buf))))))
566 ;;; These need to be rewritten for the new scroll bar implementation.
568 ;;;!! ;; Commands for the scroll bar.
570 ;;;!! (defun mouse-scroll-down (click)
571 ;;;!! (interactive "@e")
572 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
574 ;;;!! (defun mouse-scroll-up (click)
575 ;;;!! (interactive "@e")
576 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
578 ;;;!! (defun mouse-scroll-down-full ()
579 ;;;!! (interactive "@")
580 ;;;!! (scroll-down nil))
582 ;;;!! (defun mouse-scroll-up-full ()
583 ;;;!! (interactive "@")
584 ;;;!! (scroll-up nil))
586 ;;;!! (defun mouse-scroll-move-cursor (click)
587 ;;;!! (interactive "@e")
588 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
590 ;;;!! (defun mouse-scroll-absolute (event)
591 ;;;!! (interactive "@e")
592 ;;;!! (let* ((pos (car event))
593 ;;;!! (position (car pos))
594 ;;;!! (length (car (cdr pos))))
595 ;;;!! (if (<= length 0) (setq length 1))
596 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
597 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
600 ;;;!! scale-factor)))
601 ;;;!! (goto-char newpos)
602 ;;;!! (recenter '(4)))))
604 ;;;!! (defun mouse-scroll-left (click)
605 ;;;!! (interactive "@e")
606 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
608 ;;;!! (defun mouse-scroll-right (click)
609 ;;;!! (interactive "@e")
610 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
612 ;;;!! (defun mouse-scroll-left-full ()
613 ;;;!! (interactive "@")
614 ;;;!! (scroll-left nil))
616 ;;;!! (defun mouse-scroll-right-full ()
617 ;;;!! (interactive "@")
618 ;;;!! (scroll-right nil))
620 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
621 ;;;!! (interactive "@e")
622 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
624 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
625 ;;;!! (interactive "@e")
626 ;;;!! (let* ((pos (car event))
627 ;;;!! (position (car pos))
628 ;;;!! (length (car (cdr pos))))
629 ;;;!! (set-window-hscroll (selected-window) 33)))
631 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
632 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
633 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
635 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
636 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
637 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
639 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
640 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
641 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
643 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
644 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
645 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
647 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
648 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
649 ;;;!! 'mouse-scroll-absolute-horizontally)
650 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
652 ;;;!! (global-set-key [horizontal-slider mouse-1]
653 ;;;!! 'mouse-scroll-move-cursor-horizontally)
654 ;;;!! (global-set-key [horizontal-slider mouse-2]
655 ;;;!! 'mouse-scroll-move-cursor-horizontally)
656 ;;;!! (global-set-key [horizontal-slider mouse-3]
657 ;;;!! 'mouse-scroll-move-cursor-horizontally)
659 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
660 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
661 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
663 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
664 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
665 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
667 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
668 ;;;!! 'mouse-split-window-horizontally)
669 ;;;!! (global-set-key [mode-line S-mouse-2]
670 ;;;!! 'mouse-split-window-horizontally)
671 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
672 ;;;!! 'mouse-split-window)
675 ;;;!! ;;;; Here are experimental things being tested. Mouse events
676 ;;;!! ;;;; are of the form:
677 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
680 ;;;!! ;;;; Dynamically track mouse coordinates
683 ;;;!! ;;(defun track-mouse (event)
684 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
685 ;;;!! ;; (interactive "@e")
686 ;;;!! ;; (while mouse-grabbed
687 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
688 ;;;!! ;; (abs-x (car pos))
689 ;;;!! ;; (abs-y (cdr pos))
690 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
691 ;;;!! ;; (list (car pos) (cdr pos))
692 ;;;!! ;; (selected-window))))
693 ;;;!! ;; (if (consp relative-coordinate)
694 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
695 ;;;!! ;; (car relative-coordinate)
696 ;;;!! ;; (car (cdr relative-coordinate)))
697 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
700 ;;;!! ;; Dynamically put a box around the line indicated by point
703 ;;;!! ;;(require 'backquote)
705 ;;;!! ;;(defun mouse-select-buffer-line (event)
706 ;;;!! ;; (interactive "@e")
707 ;;;!! ;; (let ((relative-coordinate
708 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
709 ;;;!! ;; (abs-y (car (cdr (car event)))))
710 ;;;!! ;; (if (consp relative-coordinate)
712 ;;;!! ;; (save-excursion
713 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
714 ;;;!! ;; (x-draw-rectangle
715 ;;;!! ;; (selected-screen)
717 ;;;!! ;; (save-excursion
718 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
719 ;;;!! ;; (end-of-line)
720 ;;;!! ;; (push-mark nil t)
721 ;;;!! ;; (beginning-of-line)
722 ;;;!! ;; (- (region-end) (region-beginning))) 1))
724 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
726 ;;;!! ;;(defvar last-line-drawn nil)
727 ;;;!! ;;(defvar begin-delim "[^ \t]")
728 ;;;!! ;;(defvar end-delim "[^ \t]")
730 ;;;!! ;;(defun mouse-boxing (event)
731 ;;;!! ;; (interactive "@e")
732 ;;;!! ;; (save-excursion
733 ;;;!! ;; (let ((screen (selected-screen)))
734 ;;;!! ;; (while (= (x-mouse-events) 0)
735 ;;;!! ;; (let* ((pos (read-mouse-position screen))
736 ;;;!! ;; (abs-x (car pos))
737 ;;;!! ;; (abs-y (cdr pos))
738 ;;;!! ;; (relative-coordinate
739 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
740 ;;;!! ;; (selected-window)))
741 ;;;!! ;; (begin-reg nil)
742 ;;;!! ;; (end-reg nil)
743 ;;;!! ;; (end-column nil)
744 ;;;!! ;; (begin-column nil))
745 ;;;!! ;; (if (and (consp relative-coordinate)
746 ;;;!! ;; (or (not last-line-drawn)
747 ;;;!! ;; (not (= last-line-drawn abs-y))))
749 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
750 ;;;!! ;; (if (= (following-char) 10)
753 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
754 ;;;!! ;; (setq begin-column (1- (current-column)))
755 ;;;!! ;; (end-of-line)
756 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
757 ;;;!! ;; (setq end-column (1+ (current-column)))
758 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
759 ;;;!! ;; (x-draw-rectangle screen
760 ;;;!! ;; (setq last-line-drawn abs-y)
761 ;;;!! ;; begin-column
762 ;;;!! ;; (- end-column begin-column) 1))))))))))
764 ;;;!! ;;(defun mouse-erase-box ()
765 ;;;!! ;; (interactive)
766 ;;;!! ;; (if last-line-drawn
768 ;;;!! ;; (x-erase-rectangle (selected-screen))
769 ;;;!! ;; (setq last-line-drawn nil))))
771 ;;;!! ;;; (defun test-x-rectangle ()
772 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
773 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
774 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
777 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
780 ;;;!! (defvar double-start nil)
781 ;;;!! (defconst double-click-interval 300
782 ;;;!! "Max ticks between clicks")
784 ;;;!! (defun double-down (event)
785 ;;;!! (interactive "@e")
786 ;;;!! (if double-start
787 ;;;!! (let ((interval (- (nth 4 event) double-start)))
788 ;;;!! (if (< interval double-click-interval)
790 ;;;!! (backward-up-list 1)
791 ;;;!! ;; (message "Interval %d" interval)
792 ;;;!! (sleep-for 1)))
793 ;;;!! (setq double-start nil))
794 ;;;!! (setq double-start (nth 4 event))))
796 ;;;!! (defun double-up (event)
797 ;;;!! (interactive "@e")
798 ;;;!! (and double-start
799 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
800 ;;;!! (setq double-start nil)))
802 ;;;!! ;;; (defun x-test-doubleclick ()
803 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
804 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
805 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
808 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
811 ;;;!! (defvar scrolled-lines 0)
812 ;;;!! (defconst scroll-speed 1)
814 ;;;!! (defun incr-scroll-down (event)
815 ;;;!! (interactive "@e")
816 ;;;!! (setq scrolled-lines 0)
817 ;;;!! (incremental-scroll scroll-speed))
819 ;;;!! (defun incr-scroll-up (event)
820 ;;;!! (interactive "@e")
821 ;;;!! (setq scrolled-lines 0)
822 ;;;!! (incremental-scroll (- scroll-speed)))
824 ;;;!! (defun incremental-scroll (n)
825 ;;;!! (while (= (x-mouse-events) 0)
826 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
827 ;;;!! (scroll-down n)
828 ;;;!! (sit-for 300 t)))
830 ;;;!! (defun incr-scroll-stop (event)
831 ;;;!! (interactive "@e")
832 ;;;!! (message "Scrolled %d lines" scrolled-lines)
833 ;;;!! (setq scrolled-lines 0)
836 ;;;!! ;;; (defun x-testing-scroll ()
837 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
838 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
839 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
840 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
841 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
844 ;;;!! ;; Some playthings suitable for picture mode? They need work.
847 ;;;!! (defun mouse-kill-rectangle (event)
848 ;;;!! "Kill the rectangle between point and the mouse cursor."
849 ;;;!! (interactive "@e")
850 ;;;!! (let ((point-save (point)))
851 ;;;!! (save-excursion
852 ;;;!! (mouse-set-point event)
853 ;;;!! (push-mark nil t)
854 ;;;!! (if (> point-save (point))
855 ;;;!! (kill-rectangle (point) point-save)
856 ;;;!! (kill-rectangle point-save (point))))))
858 ;;;!! (defun mouse-open-rectangle (event)
859 ;;;!! "Kill the rectangle between point and the mouse cursor."
860 ;;;!! (interactive "@e")
861 ;;;!! (let ((point-save (point)))
862 ;;;!! (save-excursion
863 ;;;!! (mouse-set-point event)
864 ;;;!! (push-mark nil t)
865 ;;;!! (if (> point-save (point))
866 ;;;!! (open-rectangle (point) point-save)
867 ;;;!! (open-rectangle point-save (point))))))
869 ;;;!! ;; Must be a better way to do this.
871 ;;;!! (defun mouse-multiple-insert (n char)
874 ;;;!! (setq n (1- n))))
876 ;;;!! ;; What this could do is not finalize until button was released.
878 ;;;!! (defun mouse-move-text (event)
879 ;;;!! "Move text from point to cursor position, inserting spaces."
880 ;;;!! (interactive "@e")
881 ;;;!! (let* ((relative-coordinate
882 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
883 ;;;!! (if (consp relative-coordinate)
884 ;;;!! (cond ((> (current-column) (car relative-coordinate))
886 ;;;!! (- (car relative-coordinate) (current-column))))
887 ;;;!! ((< (current-column) (car relative-coordinate))
888 ;;;!! (mouse-multiple-insert
889 ;;;!! (- (car relative-coordinate) (current-column)) " "))
890 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
892 ;; Choose a completion with the mouse.
894 (defun mouse-choose-completion (event)
895 "Click on an alternative in the `*Completions*' buffer to choose it."
899 (set-buffer (window-buffer (posn-window (event-start event))))
901 (goto-char (posn-point (event-start event)))
902 (skip-chars-backward "^ \t\n")
904 (skip-chars-forward "^ \t\n")
905 (setq choice (buffer-substring beg (point))))))
906 (set-buffer (window-buffer (minibuffer-window)))
907 (goto-char (max (point-min) (- (point-max) (length choice))))
908 (while (and (not (eobp))
909 (let ((tail (buffer-substring (point) (point-max))))
910 (not (string= tail (substring choice 0 (length tail))))))
913 (delete-region (point) (point-max))
914 (minibuffer-complete-and-exit)))
918 (defun font-menu-add-default ()
919 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
920 (font-alist x-fixed-font-alist)
921 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
922 (if (assoc "Default" elt)
923 (delete (assoc "Default" elt) elt))
925 (cons (list "Default"
926 (cdr (assq 'font (frame-parameters (selected-frame)))))
929 (defvar x-fixed-font-alist
932 ("6x10" "-misc-fixed-medium-r-semicondensed--10-110-75-75-c-60-*-1")
933 ("6x12" "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-*-1")
934 ("6x13" "-misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-*-1")
936 "-b&h-lucidatypewriter-medium-r-normal-sans-0-0-0-0-m-0-*-1")
937 ("7x13" "-misc-fixed-medium-r-normal--13-120-75-75-c-70-*-1")
938 ("7x14" "-misc-fixed-medium-r-normal--14-130-75-75-c-70-*-1")
939 ("9x15" "-misc-fixed-medium-r-normal--15-140-*-*-c-*-*-1")
941 ("clean 8x8" "-schumacher-clean-medium-r-normal--*-80-*-*-c-*-*-1")
942 ("clean 8x14" "-schumacher-clean-medium-r-normal--*-140-*-*-c-*-*-1")
943 ("clean 8x10" "-schumacher-clean-medium-r-normal--*-100-*-*-c-*-*-1")
944 ("clean 8x16" "-schumacher-clean-medium-r-normal--*-160-*-*-c-*-*-1")
946 ("sony 8x16" "-sony-fixed-medium-r-normal--16-120-100-100-c-80-*-1")
952 ;;; We don't seem to have these; who knows what they are.
953 ;;; ("fg-18" "fg-18")
954 ;;; ("fg-25" "fg-25")
955 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
956 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
957 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
958 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
959 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
961 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
962 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
963 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
964 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
965 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
966 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
967 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
968 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
969 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
970 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
971 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
972 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
973 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
974 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
975 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
976 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
977 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
978 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
979 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
980 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
981 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
982 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
983 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
984 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
986 "X fonts suitable for use in Emacs.")
988 (defun mouse-set-font (&optional font)
989 "Select an emacs font from a list of known good fonts"
991 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
993 (progn (modify-frame-parameters (selected-frame)
994 (list (cons 'font font)))
995 ;; Update some standard faces too.
996 (set-face-font 'bold nil (selected-frame))
997 (make-face-bold 'bold (selected-frame) t)
998 (set-face-font 'italic nil (selected-frame))
999 (make-face-italic 'italic (selected-frame) t)
1000 (set-face-font 'bold-italic nil (selected-frame))
1001 (make-face-bold-italic 'bold-italic (selected-frame) t))))
1003 ;;; Bindings for mouse commands.
1005 (define-key global-map [down-mouse-1] 'mouse-drag-region)
1006 (global-set-key [mouse-1] 'mouse-set-point)
1007 (global-set-key [drag-mouse-1] 'mouse-set-region)
1009 ;; These are tested for in mouse-drag-region.
1010 (global-set-key [double-mouse-1] 'mouse-set-point)
1011 (global-set-key [triple-mouse-1] 'mouse-set-point)
1013 (global-set-key [mouse-2] 'mouse-yank-at-click)
1014 (global-set-key [mouse-3] 'mouse-save-then-kill)
1016 ;; By binding these to down-going events, we let the user use the up-going
1017 ;; event to make the selection, saving a click.
1018 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1019 (global-set-key [C-down-mouse-3] 'mouse-set-font)
1021 ;; Replaced with dragging mouse-1
1022 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
1024 (global-set-key [mode-line mouse-1] 'mouse-delete-other-windows)
1025 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1026 (global-set-key [mode-line S-mouse-2] 'mouse-split-window-horizontally)
1028 ;; Define the mouse help menu tree.
1030 (defvar help-menu-map '(keymap "Help"))
1031 (global-set-key [C-down-mouse-2] help-menu-map)
1033 (defvar help-apropos-map (make-sparse-keymap "Is there a command that..."))
1034 (defvar help-keys-map (make-sparse-keymap "Key Commands <==> Functions"))
1035 (defvar help-manual-map (make-sparse-keymap "Manual and tutorial"))
1036 (defvar help-misc-map (make-sparse-keymap "Odds and ends"))
1037 (defvar help-modes-map (make-sparse-keymap "Modes"))
1038 (defvar help-admin-map (make-sparse-keymap "Administrivia"))
1040 (define-key help-menu-map [apropos]
1041 (cons "@Is there a command that..." help-apropos-map))
1042 (define-key help-menu-map [keys]
1043 (cons "@Key Commands <==> Functions" help-keys-map))
1044 (define-key help-menu-map [manuals]
1045 (cons "@Manual and tutorial" help-manual-map))
1046 (define-key help-menu-map [misc]
1047 (cons "@Odds and ends" help-misc-map))
1048 (define-key help-menu-map [modes]
1049 (cons "@Modes" help-modes-map))
1050 (define-key help-menu-map [admin]
1051 (cons "@Administrivia" help-admin-map))
1053 (define-key help-apropos-map "c" '("Command Apropos" . command-apropos))
1054 (define-key help-apropos-map "a" '("Apropos" . apropos))
1056 (define-key help-keys-map "b"
1057 '("List all keystroke commands" . describe-bindings))
1058 (define-key help-keys-map "c"
1059 '("Describe key briefly" . describe-key-briefly))
1060 (define-key help-keys-map "k"
1061 '("Describe key verbose" . describe-key))
1062 (define-key help-keys-map "f"
1063 '("Describe Lisp function" . describe-function))
1064 (define-key help-keys-map "w"
1065 '("Where is this command" . where-is))
1067 (define-key help-manual-map "i" '("Info system" . info))
1068 (define-key help-manual-map "t"
1069 '("Invoke Emacs tutorial" . help-with-tutorial))
1071 (define-key help-misc-map "l" '("Last 100 Keystrokes" . view-lossage))
1072 (define-key help-misc-map "s" '("Describe syntax table" . describe-syntax))
1074 (define-key help-modes-map "m"
1075 '("Describe current major mode" . describe-mode))
1076 (define-key help-modes-map "b"
1077 '("List all keystroke commands" . describe-bindings))
1079 (define-key help-admin-map "n"
1080 '("View Emacs news" . view-emacs-news))
1081 (define-key help-admin-map "l"
1082 '("View Emacs copying conditions" . describe-copying))
1083 (define-key help-admin-map "d"
1084 '("Describe distribution" . describe-distribution))
1085 (define-key help-admin-map "w"
1086 '("Describe (non)warranty" . describe-no-warranty))
1090 ;;; mouse.el ends here