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 (defvar mouse-selection-click-count nil)
167 (defun mouse-drag-region (start-event)
168 "Set the region to the text that the mouse is dragged over.
169 Highlight the drag area as you move the mouse.
170 This must be bound to a button-down mouse event.
171 In Transient Mark mode, the highlighting remains once you
172 release the mouse button. Otherwise, it does not."
174 (let* ((start-posn (event-start start-event))
175 (start-point (posn-point start-posn))
176 (start-window (posn-window start-posn))
177 (start-frame (window-frame start-window))
178 (bounds (window-edges start-window))
180 (bottom (if (window-minibuffer-p start-window)
182 ;; Don't count the mode line.
183 (1- (nth 3 bounds))))
184 (click-count (1- (event-click-count start-event))))
185 (setq mouse-selection-click-count click-count)
186 (mouse-set-point start-event)
187 (let ((range (mouse-start-end start-point start-point click-count)))
188 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
189 (window-buffer start-window)))
191 (let (event end end-point)
194 (setq event (read-event))
195 (or (mouse-movement-p event)
196 (eq (car-safe event) 'switch-frame)))
198 (if (eq (car-safe event) 'switch-frame)
200 (setq end (event-end event)
201 end-point (posn-point end))
205 ;; Ignore switch-frame events.
206 ((eq (car-safe event) 'switch-frame))
208 ;; Are we moving within the original window?
209 ((and (eq (posn-window end) start-window)
210 (integer-or-marker-p end-point))
211 (goto-char end-point)
212 (let ((range (mouse-start-end start-point (point) click-count)))
213 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
215 ;; Are we moving on a different window on the same frame?
216 ((and (windowp (posn-window end))
217 (eq (window-frame (posn-window end)) start-frame))
219 (+ (nth 1 (window-edges (posn-window end)))
220 (cdr (posn-col-row end)))))
224 (- mouse-row top) mouse-drag-overlay start-point))
226 (>= mouse-row bottom))
227 (mouse-scroll-subr (1+ (- mouse-row bottom))
228 mouse-drag-overlay start-point)))))
231 (let ((mouse-y (cdr (cdr (mouse-position))))
232 (menu-bar-lines (or (cdr (assq 'menu-bar-lines
236 ;; Are we on the menu bar?
237 (and (integerp mouse-y) (< mouse-y menu-bar-lines)
238 (mouse-scroll-subr (- mouse-y menu-bar-lines)
239 mouse-drag-overlay start-point))))))))
241 (if (and (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
242 (eq (posn-window (event-end event)) start-window)
243 (numberp (posn-point (event-end event))))
244 (let ((fun (key-binding (vector (car event)))))
245 (if (memq fun '(mouse-set-region mouse-set-point))
247 (push-mark (overlay-start mouse-drag-overlay) t t)
248 (goto-char (overlay-end mouse-drag-overlay)))
250 (funcall fun event)))))
251 (delete-overlay mouse-drag-overlay))))
253 ;; Commands to handle xterm-style multiple clicks.
255 (defun mouse-skip-word (dir)
256 "Skip over word, over whitespace, or over identical punctuation.
257 If DIR is positive skip forward; if negative, skip backward."
258 (let* ((char (following-char))
259 (syntax (char-to-string (char-syntax char))))
260 (if (or (string= syntax "w") (string= syntax " "))
262 (skip-syntax-backward syntax)
263 (skip-syntax-forward syntax))
265 (while (= (preceding-char) char)
267 (while (= (following-char) char)
268 (forward-char 1))))))
270 ;; Return a list of region bounds based on START and END according to MODE.
271 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
272 ;; If MODE is 1 then set point to start of word at (min START END),
273 ;; mark to end of word at (max START END).
274 ;; If MODE is 2 then do the same for lines.
275 (defun mouse-start-end (start end mode)
284 (= (char-syntax (char-after start)) ?\())
285 (list start (save-excursion (forward-sexp 1) (point))))
288 (= (char-syntax (char-after start)) ?\)))
289 (list (save-excursion
290 (goto-char (1+ start))
294 (list (save-excursion
303 (list (save-excursion
305 (beginning-of-line 1)
312 ;; Subroutine: set the mark where CLICK happened,
313 ;; but don't do anything else.
314 (defun mouse-set-mark-fast (click)
315 (let ((posn (event-start click)))
316 (select-window (posn-window posn))
317 (if (numberp (posn-point posn))
318 (push-mark (posn-point posn) t t))))
320 ;; Momentarily show where the mark is, if highlighting doesn't show it.
321 (defun mouse-show-mark ()
322 (or transient-mark-mode
327 (defun mouse-set-mark (click)
328 "Set mark at the position clicked on with the mouse.
329 Display cursor at that position for a second.
330 This must be bound to a mouse click."
332 (let ((point-save (point)))
334 (progn (mouse-set-point click)
336 (or transient-mark-mode
338 (goto-char point-save))))
340 (defun mouse-kill (click)
341 "Kill the region between point and the mouse click.
342 The text is saved in the kill ring, as with \\[kill-region]."
344 (let ((click-posn (posn-point (event-start click))))
345 (if (numberp click-posn)
346 (kill-region (min (point) click-posn)
347 (max (point) click-posn)))))
349 (defun mouse-yank-at-click (click arg)
350 "Insert the last stretch of killed text at the position clicked on.
351 Prefix arguments are interpreted as with \\[yank]."
353 (mouse-set-point click)
356 (defun mouse-kill-ring-save (click)
357 "Copy the region between point and the mouse click in the kill ring.
358 This does not delete the region; it acts like \\[kill-ring-save]."
360 (mouse-set-mark-fast click)
361 (kill-ring-save (point) (mark t))
364 ;;; This function used to delete the text between point and the mouse
365 ;;; whenever it was equal to the front of the kill ring, but some
366 ;;; people found that confusing.
368 ;;; A list (TEXT START END), describing the text and position of the last
369 ;;; invocation of mouse-save-then-kill.
370 (defvar mouse-save-then-kill-posn nil)
372 (defun mouse-save-then-kill-delete-region ()
373 ;; Delete just one char, so in case buffer is being modified
374 ;; for the first time, the undo list records that fact.
375 (delete-region (point)
376 (+ (point) (if (> (mark) (point)) 1 -1)))
377 ;; Now delete the rest of the specified region,
378 ;; but don't record it.
379 (let ((buffer-undo-list t))
380 (delete-region (point) (mark)))
381 (if (not (eq buffer-undo-list t))
382 (let ((tail buffer-undo-list))
383 ;; Search back in buffer-undo-list for the string
384 ;; that came from the first delete-region.
385 (while (and tail (not (stringp (car (car tail)))))
386 (setq tail (cdr tail)))
387 ;; Replace it with an entry for the entire deleted text.
389 (setcar tail (cons (car kill-ring) (point)))))))
391 (defun mouse-save-then-kill (click)
392 "Save text to point in kill ring; the second time, kill the text.
393 If the text between point and the mouse is the same as what's
394 at the front of the kill ring, this deletes the text.
395 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
396 which prepares for a second click to delete the text.
398 If you have selected words or lines, this command extends the
399 selection through the word or line clicked on. If you do this
400 again in a different position, it extends the selection again.
401 If you do this twice in the same position, the selection is killed."
403 (let ((click-posn (posn-point (event-start click)))
404 ;; Don't let a subsequent kill command append to this one:
405 ;; prevent setting this-command to kill-region.
406 (this-command this-command))
407 (if (> mouse-selection-click-count 0)
408 (if (not (and (eq last-command 'mouse-save-then-kill)
410 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
411 (let* ((obeg (min (point) (mark t)))
412 (oend (max (point) (mark t)))
413 (beg (min obeg click-posn))
414 (end (if (< click-posn obeg) oend click-posn))
416 (mouse-start-end beg end mouse-selection-click-count))
417 (nbeg (if (= beg obeg) obeg (car range)))
418 (nend (if (= end oend) oend (nth 1 range))))
419 (setq mouse-save-then-kill-posn
420 (list (car kill-ring) (point) click-posn))
423 ;; We have already put the old region in the kill ring.
424 ;; Replace it with the extended region.
425 ;; (It would be annoying to make a separate entry.)
426 (setcar kill-ring (buffer-substring (point) (mark t)))
428 ;; If we click this button again without moving it,
430 (mouse-save-then-kill-delete-region))
431 (if (and (eq last-command 'mouse-save-then-kill)
432 mouse-save-then-kill-posn
433 (eq (car mouse-save-then-kill-posn) (car kill-ring))
434 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
435 ;; If this is the second time we've called
436 ;; mouse-save-then-kill, delete the text from the buffer.
437 (mouse-save-then-kill-delete-region)
438 ;; Otherwise, save this region.
439 (mouse-set-mark-fast click)
440 (kill-ring-save (point) (mark t))
442 (setq mouse-save-then-kill-posn
443 (list (car kill-ring) (point) click-posn))))))
445 (global-set-key [M-mouse-1] 'mouse-start-secondary)
446 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
447 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
448 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
449 (global-set-key [M-mouse-2] 'mouse-kill-secondary)
451 ;; An overlay which records the current secondary selection
452 ;; or else is deleted when there is no secondary selection.
454 (defvar mouse-secondary-overlay nil)
456 ;; A marker which records the specified first end for a secondary selection.
458 (defvar mouse-secondary-start nil)
460 (defun mouse-start-secondary (click)
461 "Set one end of the secondary selection to the position clicked on.
462 Use \\[mouse-secondary-save-then-kill] to set the other end
463 and complete the secondary selection."
465 (let ((posn (event-start click)))
467 (set-buffer (window-buffer (posn-window posn)))
468 ;; Cancel any preexisting secondary selection.
469 (if mouse-secondary-overlay
470 (delete-overlay mouse-secondary-overlay))
471 (if (numberp (posn-point posn))
473 (or mouse-secondary-start
474 (setq mouse-secondary-start (make-marker)))
475 (move-marker mouse-secondary-start (posn-point posn)))))))
477 (defun mouse-set-secondary (click)
478 "Set the secondary selection to the text that the mouse is dragged over.
479 This must be bound to a mouse drag event."
481 (let ((posn (event-start click))
483 (end (event-end click)))
485 (set-buffer (window-buffer (posn-window posn)))
486 (if (numberp (posn-point posn))
487 (setq beg (posn-point posn)))
488 (if mouse-secondary-overlay
489 (move-overlay mouse-secondary-overlay beg (posn-point end))
490 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
491 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
493 (defun mouse-drag-secondary (click)
494 "Set the secondary selection to the text that the mouse is dragged over.
495 This must be bound to a button-down mouse event."
497 (let ((posn (event-start click)))
498 (save-window-excursion
499 (select-window (posn-window posn))
500 ;; Set point temporarily, so user sees where it is.
502 (if (numberp (posn-point posn))
503 (goto-char (posn-point posn)))
504 (setq unread-command-events
505 (cons (read-event) unread-command-events))))))
507 (defun mouse-kill-secondary ()
508 "Kill the text in the secondary selection."
510 (kill-region (overlay-start mouse-secondary-overlay)
511 (overlay-end mouse-secondary-overlay))
512 (delete-overlay mouse-secondary-overlay)
513 (setq mouse-secondary-overlay nil))
515 (defun mouse-secondary-save-then-kill (click)
516 "Save text to secondary start point in kill ring; if twice, kill it.
517 If the text between secondary start point and the mouse is the same as what's
518 at the front of the kill ring, this deletes the text.
519 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
520 which prepares for a second click to delete the text."
522 (let ((click-posn (posn-point (event-start click)))
523 (start (+ 0 mouse-secondary-start))
524 ;; Don't let a subsequent kill command append to this one:
525 ;; prevent setting this-command to kill-region.
526 (this-command this-command))
527 (if (and (eq last-command 'mouse-secondary-save-then-kill)
528 mouse-save-then-kill-posn
529 (eq (car mouse-save-then-kill-posn) (car kill-ring))
530 (equal (cdr mouse-save-then-kill-posn)
531 (list start click-posn)))
532 ;; If this is the second time we've called
533 ;; mouse-save-then-kill, delete the text from the buffer.
535 (let ((buffer-undo-list t))
536 (delete-overlay mouse-secondary-overlay)
537 (delete-region start click-posn))
538 ;; Make the undo list by hand so it is shared.
539 (if (not (eq buffer-undo-list t))
540 (setq buffer-undo-list
541 (cons (cons (car kill-ring) (marker-position start))
543 ;; Otherwise, save this region.
545 (set-buffer (window-buffer (posn-window (event-start click))))
546 (kill-ring-save start click-posn)
547 (if mouse-secondary-overlay
548 (move-overlay mouse-secondary-overlay start click-posn)
549 (setq mouse-secondary-overlay (make-overlay start click-posn)))
550 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
551 (setq mouse-save-then-kill-posn
552 (list (car kill-ring) start click-posn))))))
554 (defun mouse-buffer-menu (event)
555 "Pop up a menu of buffers for selection with the mouse.
556 This switches buffers in the window that you clicked on,
557 and selects that window."
561 (cons "Select Buffer"
562 (let ((tail (buffer-list))
566 (or (eq ?\ (aref (buffer-name (car tail)) 0))
569 (length (buffer-name (car tail))))))
570 (setq tail (cdr tail)))
571 (setq tail (buffer-list))
573 (let ((elt (car tail)))
574 (if (not (string-match "^ "
579 (format "%%%ds %%s%%s %%s"
582 (if (buffer-modified-p elt)
586 (if buffer-read-only "%" " "))
587 (or (buffer-file-name elt) ""))
590 (setq tail (cdr tail)))
592 (let ((buf (x-popup-menu event menu))
593 (window (posn-window (event-start event))))
596 (or (framep window) (select-window window))
597 (switch-to-buffer buf))))))
599 ;;; These need to be rewritten for the new scroll bar implementation.
601 ;;;!! ;; Commands for the scroll bar.
603 ;;;!! (defun mouse-scroll-down (click)
604 ;;;!! (interactive "@e")
605 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
607 ;;;!! (defun mouse-scroll-up (click)
608 ;;;!! (interactive "@e")
609 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
611 ;;;!! (defun mouse-scroll-down-full ()
612 ;;;!! (interactive "@")
613 ;;;!! (scroll-down nil))
615 ;;;!! (defun mouse-scroll-up-full ()
616 ;;;!! (interactive "@")
617 ;;;!! (scroll-up nil))
619 ;;;!! (defun mouse-scroll-move-cursor (click)
620 ;;;!! (interactive "@e")
621 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
623 ;;;!! (defun mouse-scroll-absolute (event)
624 ;;;!! (interactive "@e")
625 ;;;!! (let* ((pos (car event))
626 ;;;!! (position (car pos))
627 ;;;!! (length (car (cdr pos))))
628 ;;;!! (if (<= length 0) (setq length 1))
629 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
630 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
633 ;;;!! scale-factor)))
634 ;;;!! (goto-char newpos)
635 ;;;!! (recenter '(4)))))
637 ;;;!! (defun mouse-scroll-left (click)
638 ;;;!! (interactive "@e")
639 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
641 ;;;!! (defun mouse-scroll-right (click)
642 ;;;!! (interactive "@e")
643 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
645 ;;;!! (defun mouse-scroll-left-full ()
646 ;;;!! (interactive "@")
647 ;;;!! (scroll-left nil))
649 ;;;!! (defun mouse-scroll-right-full ()
650 ;;;!! (interactive "@")
651 ;;;!! (scroll-right nil))
653 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
654 ;;;!! (interactive "@e")
655 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
657 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
658 ;;;!! (interactive "@e")
659 ;;;!! (let* ((pos (car event))
660 ;;;!! (position (car pos))
661 ;;;!! (length (car (cdr pos))))
662 ;;;!! (set-window-hscroll (selected-window) 33)))
664 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
665 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
666 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
668 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
669 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
670 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
672 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
673 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
674 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
676 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
677 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
678 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
680 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
681 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
682 ;;;!! 'mouse-scroll-absolute-horizontally)
683 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
685 ;;;!! (global-set-key [horizontal-slider mouse-1]
686 ;;;!! 'mouse-scroll-move-cursor-horizontally)
687 ;;;!! (global-set-key [horizontal-slider mouse-2]
688 ;;;!! 'mouse-scroll-move-cursor-horizontally)
689 ;;;!! (global-set-key [horizontal-slider mouse-3]
690 ;;;!! 'mouse-scroll-move-cursor-horizontally)
692 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
693 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
694 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
696 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
697 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
698 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
700 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
701 ;;;!! 'mouse-split-window-horizontally)
702 ;;;!! (global-set-key [mode-line S-mouse-2]
703 ;;;!! 'mouse-split-window-horizontally)
704 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
705 ;;;!! 'mouse-split-window)
708 ;;;!! ;;;; Here are experimental things being tested. Mouse events
709 ;;;!! ;;;; are of the form:
710 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
713 ;;;!! ;;;; Dynamically track mouse coordinates
716 ;;;!! ;;(defun track-mouse (event)
717 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
718 ;;;!! ;; (interactive "@e")
719 ;;;!! ;; (while mouse-grabbed
720 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
721 ;;;!! ;; (abs-x (car pos))
722 ;;;!! ;; (abs-y (cdr pos))
723 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
724 ;;;!! ;; (list (car pos) (cdr pos))
725 ;;;!! ;; (selected-window))))
726 ;;;!! ;; (if (consp relative-coordinate)
727 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
728 ;;;!! ;; (car relative-coordinate)
729 ;;;!! ;; (car (cdr relative-coordinate)))
730 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
733 ;;;!! ;; Dynamically put a box around the line indicated by point
736 ;;;!! ;;(require 'backquote)
738 ;;;!! ;;(defun mouse-select-buffer-line (event)
739 ;;;!! ;; (interactive "@e")
740 ;;;!! ;; (let ((relative-coordinate
741 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
742 ;;;!! ;; (abs-y (car (cdr (car event)))))
743 ;;;!! ;; (if (consp relative-coordinate)
745 ;;;!! ;; (save-excursion
746 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
747 ;;;!! ;; (x-draw-rectangle
748 ;;;!! ;; (selected-screen)
750 ;;;!! ;; (save-excursion
751 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
752 ;;;!! ;; (end-of-line)
753 ;;;!! ;; (push-mark nil t)
754 ;;;!! ;; (beginning-of-line)
755 ;;;!! ;; (- (region-end) (region-beginning))) 1))
757 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
759 ;;;!! ;;(defvar last-line-drawn nil)
760 ;;;!! ;;(defvar begin-delim "[^ \t]")
761 ;;;!! ;;(defvar end-delim "[^ \t]")
763 ;;;!! ;;(defun mouse-boxing (event)
764 ;;;!! ;; (interactive "@e")
765 ;;;!! ;; (save-excursion
766 ;;;!! ;; (let ((screen (selected-screen)))
767 ;;;!! ;; (while (= (x-mouse-events) 0)
768 ;;;!! ;; (let* ((pos (read-mouse-position screen))
769 ;;;!! ;; (abs-x (car pos))
770 ;;;!! ;; (abs-y (cdr pos))
771 ;;;!! ;; (relative-coordinate
772 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
773 ;;;!! ;; (selected-window)))
774 ;;;!! ;; (begin-reg nil)
775 ;;;!! ;; (end-reg nil)
776 ;;;!! ;; (end-column nil)
777 ;;;!! ;; (begin-column nil))
778 ;;;!! ;; (if (and (consp relative-coordinate)
779 ;;;!! ;; (or (not last-line-drawn)
780 ;;;!! ;; (not (= last-line-drawn abs-y))))
782 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
783 ;;;!! ;; (if (= (following-char) 10)
786 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
787 ;;;!! ;; (setq begin-column (1- (current-column)))
788 ;;;!! ;; (end-of-line)
789 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
790 ;;;!! ;; (setq end-column (1+ (current-column)))
791 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
792 ;;;!! ;; (x-draw-rectangle screen
793 ;;;!! ;; (setq last-line-drawn abs-y)
794 ;;;!! ;; begin-column
795 ;;;!! ;; (- end-column begin-column) 1))))))))))
797 ;;;!! ;;(defun mouse-erase-box ()
798 ;;;!! ;; (interactive)
799 ;;;!! ;; (if last-line-drawn
801 ;;;!! ;; (x-erase-rectangle (selected-screen))
802 ;;;!! ;; (setq last-line-drawn nil))))
804 ;;;!! ;;; (defun test-x-rectangle ()
805 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
806 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
807 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
810 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
813 ;;;!! (defvar double-start nil)
814 ;;;!! (defconst double-click-interval 300
815 ;;;!! "Max ticks between clicks")
817 ;;;!! (defun double-down (event)
818 ;;;!! (interactive "@e")
819 ;;;!! (if double-start
820 ;;;!! (let ((interval (- (nth 4 event) double-start)))
821 ;;;!! (if (< interval double-click-interval)
823 ;;;!! (backward-up-list 1)
824 ;;;!! ;; (message "Interval %d" interval)
825 ;;;!! (sleep-for 1)))
826 ;;;!! (setq double-start nil))
827 ;;;!! (setq double-start (nth 4 event))))
829 ;;;!! (defun double-up (event)
830 ;;;!! (interactive "@e")
831 ;;;!! (and double-start
832 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
833 ;;;!! (setq double-start nil)))
835 ;;;!! ;;; (defun x-test-doubleclick ()
836 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
837 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
838 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
841 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
844 ;;;!! (defvar scrolled-lines 0)
845 ;;;!! (defconst scroll-speed 1)
847 ;;;!! (defun incr-scroll-down (event)
848 ;;;!! (interactive "@e")
849 ;;;!! (setq scrolled-lines 0)
850 ;;;!! (incremental-scroll scroll-speed))
852 ;;;!! (defun incr-scroll-up (event)
853 ;;;!! (interactive "@e")
854 ;;;!! (setq scrolled-lines 0)
855 ;;;!! (incremental-scroll (- scroll-speed)))
857 ;;;!! (defun incremental-scroll (n)
858 ;;;!! (while (= (x-mouse-events) 0)
859 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
860 ;;;!! (scroll-down n)
861 ;;;!! (sit-for 300 t)))
863 ;;;!! (defun incr-scroll-stop (event)
864 ;;;!! (interactive "@e")
865 ;;;!! (message "Scrolled %d lines" scrolled-lines)
866 ;;;!! (setq scrolled-lines 0)
869 ;;;!! ;;; (defun x-testing-scroll ()
870 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
871 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
872 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
873 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
874 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
877 ;;;!! ;; Some playthings suitable for picture mode? They need work.
880 ;;;!! (defun mouse-kill-rectangle (event)
881 ;;;!! "Kill the rectangle between point and the mouse cursor."
882 ;;;!! (interactive "@e")
883 ;;;!! (let ((point-save (point)))
884 ;;;!! (save-excursion
885 ;;;!! (mouse-set-point event)
886 ;;;!! (push-mark nil t)
887 ;;;!! (if (> point-save (point))
888 ;;;!! (kill-rectangle (point) point-save)
889 ;;;!! (kill-rectangle point-save (point))))))
891 ;;;!! (defun mouse-open-rectangle (event)
892 ;;;!! "Kill the rectangle between point and the mouse cursor."
893 ;;;!! (interactive "@e")
894 ;;;!! (let ((point-save (point)))
895 ;;;!! (save-excursion
896 ;;;!! (mouse-set-point event)
897 ;;;!! (push-mark nil t)
898 ;;;!! (if (> point-save (point))
899 ;;;!! (open-rectangle (point) point-save)
900 ;;;!! (open-rectangle point-save (point))))))
902 ;;;!! ;; Must be a better way to do this.
904 ;;;!! (defun mouse-multiple-insert (n char)
907 ;;;!! (setq n (1- n))))
909 ;;;!! ;; What this could do is not finalize until button was released.
911 ;;;!! (defun mouse-move-text (event)
912 ;;;!! "Move text from point to cursor position, inserting spaces."
913 ;;;!! (interactive "@e")
914 ;;;!! (let* ((relative-coordinate
915 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
916 ;;;!! (if (consp relative-coordinate)
917 ;;;!! (cond ((> (current-column) (car relative-coordinate))
919 ;;;!! (- (car relative-coordinate) (current-column))))
920 ;;;!! ((< (current-column) (car relative-coordinate))
921 ;;;!! (mouse-multiple-insert
922 ;;;!! (- (car relative-coordinate) (current-column)) " "))
923 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
925 ;; Choose a completion with the mouse.
927 (defun mouse-choose-completion (event)
928 "Click on an alternative in the `*Completions*' buffer to choose it."
932 (set-buffer (window-buffer (posn-window (event-start event))))
934 (goto-char (posn-point (event-start event)))
935 (skip-chars-backward "^ \t\n")
937 (skip-chars-forward "^ \t\n")
938 (setq choice (buffer-substring beg (point))))))
939 (set-buffer (window-buffer (minibuffer-window)))
940 (goto-char (max (point-min) (- (point-max) (length choice))))
941 (while (and (not (eobp))
942 (let ((tail (buffer-substring (point) (point-max))))
943 (not (string= tail (substring choice 0 (length tail))))))
946 (delete-region (point) (point-max))
947 (minibuffer-complete-and-exit)))
951 (defun font-menu-add-default ()
952 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
953 (font-alist x-fixed-font-alist)
954 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
955 (if (assoc "Default" elt)
956 (delete (assoc "Default" elt) elt))
958 (cons (list "Default"
959 (cdr (assq 'font (frame-parameters (selected-frame)))))
962 (defvar x-fixed-font-alist
965 ("6x10" "-misc-fixed-medium-r-semicondensed--10-110-75-75-c-60-*-1")
966 ("6x12" "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-*-1")
967 ("6x13" "-misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-*-1")
969 "-b&h-lucidatypewriter-medium-r-normal-sans-0-0-0-0-m-0-*-1")
970 ("7x13" "-misc-fixed-medium-r-normal--13-120-75-75-c-70-*-1")
971 ("7x14" "-misc-fixed-medium-r-normal--14-130-75-75-c-70-*-1")
972 ("9x15" "-misc-fixed-medium-r-normal--15-140-*-*-c-*-*-1")
974 ("clean 8x8" "-schumacher-clean-medium-r-normal--*-80-*-*-c-*-*-1")
975 ("clean 8x14" "-schumacher-clean-medium-r-normal--*-140-*-*-c-*-*-1")
976 ("clean 8x10" "-schumacher-clean-medium-r-normal--*-100-*-*-c-*-*-1")
977 ("clean 8x16" "-schumacher-clean-medium-r-normal--*-160-*-*-c-*-*-1")
979 ("sony 8x16" "-sony-fixed-medium-r-normal--16-120-100-100-c-80-*-1")
985 ;;; We don't seem to have these; who knows what they are.
986 ;;; ("fg-18" "fg-18")
987 ;;; ("fg-25" "fg-25")
988 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
989 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
990 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
991 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
992 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
994 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
995 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
996 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
997 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
998 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
999 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1000 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1001 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1002 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1003 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1004 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1005 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1006 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1007 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1008 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1009 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1010 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1011 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1012 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1013 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1014 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1015 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1016 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1017 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
1019 "X fonts suitable for use in Emacs.")
1021 (defun mouse-set-font (&optional font)
1022 "Select an emacs font from a list of known good fonts"
1024 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
1026 (progn (modify-frame-parameters (selected-frame)
1027 (list (cons 'font font)))
1028 ;; Update some standard faces too.
1029 (set-face-font 'bold nil (selected-frame))
1030 (make-face-bold 'bold (selected-frame) t)
1031 (set-face-font 'italic nil (selected-frame))
1032 (make-face-italic 'italic (selected-frame) t)
1033 (set-face-font 'bold-italic nil (selected-frame))
1034 (make-face-bold-italic 'bold-italic (selected-frame) t))))
1036 ;;; Bindings for mouse commands.
1038 (define-key global-map [down-mouse-1] 'mouse-drag-region)
1039 (global-set-key [mouse-1] 'mouse-set-point)
1040 (global-set-key [drag-mouse-1] 'mouse-set-region)
1042 ;; These are tested for in mouse-drag-region.
1043 (global-set-key [double-mouse-1] 'mouse-set-point)
1044 (global-set-key [triple-mouse-1] 'mouse-set-point)
1046 (global-set-key [mouse-2] 'mouse-yank-at-click)
1047 (global-set-key [mouse-3] 'mouse-save-then-kill)
1049 ;; By binding these to down-going events, we let the user use the up-going
1050 ;; event to make the selection, saving a click.
1051 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1052 (global-set-key [C-down-mouse-3] 'mouse-set-font)
1054 ;; Replaced with dragging mouse-1
1055 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
1057 (global-set-key [mode-line mouse-1] 'mouse-delete-other-windows)
1058 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1059 (global-set-key [mode-line S-mouse-2] 'mouse-split-window-horizontally)
1061 ;; Define the mouse help menu tree.
1063 (defvar help-menu-map '(keymap "Help"))
1064 (global-set-key [C-down-mouse-2] help-menu-map)
1066 (defvar help-apropos-map (make-sparse-keymap "Is there a command that..."))
1067 (defvar help-keys-map (make-sparse-keymap "Key Commands <==> Functions"))
1068 (defvar help-manual-map (make-sparse-keymap "Manual and tutorial"))
1069 (defvar help-misc-map (make-sparse-keymap "Odds and ends"))
1070 (defvar help-modes-map (make-sparse-keymap "Modes"))
1071 (defvar help-admin-map (make-sparse-keymap "Administrivia"))
1073 (define-key help-menu-map [apropos]
1074 (cons "@Is there a command that..." help-apropos-map))
1075 (define-key help-menu-map [keys]
1076 (cons "@Key Commands <==> Functions" help-keys-map))
1077 (define-key help-menu-map [manuals]
1078 (cons "@Manual and tutorial" help-manual-map))
1079 (define-key help-menu-map [misc]
1080 (cons "@Odds and ends" help-misc-map))
1081 (define-key help-menu-map [modes]
1082 (cons "@Modes" help-modes-map))
1083 (define-key help-menu-map [admin]
1084 (cons "@Administrivia" help-admin-map))
1086 (define-key help-apropos-map "c" '("Command Apropos" . command-apropos))
1087 (define-key help-apropos-map "a" '("Apropos" . apropos))
1089 (define-key help-keys-map "b"
1090 '("List all keystroke commands" . describe-bindings))
1091 (define-key help-keys-map "c"
1092 '("Describe key briefly" . describe-key-briefly))
1093 (define-key help-keys-map "k"
1094 '("Describe key verbose" . describe-key))
1095 (define-key help-keys-map "f"
1096 '("Describe Lisp function" . describe-function))
1097 (define-key help-keys-map "w"
1098 '("Where is this command" . where-is))
1100 (define-key help-manual-map "i" '("Info system" . info))
1101 (define-key help-manual-map "t"
1102 '("Invoke Emacs tutorial" . help-with-tutorial))
1104 (define-key help-misc-map "l" '("Last 100 Keystrokes" . view-lossage))
1105 (define-key help-misc-map "s" '("Describe syntax table" . describe-syntax))
1107 (define-key help-modes-map "m"
1108 '("Describe current major mode" . describe-mode))
1109 (define-key help-modes-map "b"
1110 '("List all keystroke commands" . describe-bindings))
1112 (define-key help-admin-map "n"
1113 '("View Emacs news" . view-emacs-news))
1114 (define-key help-admin-map "l"
1115 '("View Emacs copying conditions" . describe-copying))
1116 (define-key help-admin-map "d"
1117 '("Describe distribution" . describe-distribution))
1118 (define-key help-admin-map "w"
1119 '("Describe (non)warranty" . describe-no-warranty))
1123 ;;; mouse.el ends here