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 (click)
95 "Move point to the position clicked on with the mouse.
96 This must be bound to a mouse click."
98 (let ((posn (event-start click)))
99 (select-window (posn-window posn))
100 (if (numberp (posn-point posn))
101 (goto-char (posn-point posn)))))
103 (defun mouse-set-region (click)
104 "Set the region to the text that the mouse is dragged over.
105 This must be bound to a mouse drag event."
107 (let ((posn (event-start click))
108 (end (event-end click)))
109 (select-window (posn-window posn))
110 (if (numberp (posn-point posn))
111 (goto-char (posn-point posn)))
112 ;; If mark is highlighted, no need to bounce the cursor.
113 (or (and transient-mark-mode
114 (eq (framep (selected-frame)) 'x))
118 (if (numberp (posn-point end))
119 (goto-char (posn-point end)))))
121 (defvar mouse-scroll-delay 0.25
122 "*The pause between scroll steps caused by mouse drags, in seconds.
123 If you drag the mouse beyond the edge of a window, Emacs scrolls the
124 window to bring the text beyond that edge into view, with a delay of
125 this many seconds between scroll steps. Scrolling stops when you move
126 the mouse back into the window, or release the button.
127 This variable's value may be non-integral.
128 Setting this to zero causes Emacs to scroll as fast as it can.")
130 (defun mouse-scroll-subr (jump &optional overlay start)
131 "Scroll the selected window JUMP lines at a time, until new input arrives.
132 If OVERLAY is an overlay, let it stretch from START to the far edge of
133 the newly visible text.
134 Upon exit, point is at the far edge of the newly visible text."
136 (goto-char (window-start))
137 (if (not (zerop (vertical-motion jump)))
139 (set-window-start (selected-window) (point))
142 (goto-char (window-end (selected-window)))
143 ;; window-end doesn't reflect the window's new
144 ;; start position until the next redisplay. Hurrah.
145 (vertical-motion (1- jump)))
146 (goto-char (window-start (selected-window))))
148 (move-overlay overlay start (point)))
150 (sit-for mouse-scroll-delay))))))
153 (defvar mouse-drag-overlay (make-overlay 1 1))
154 (overlay-put mouse-drag-overlay 'face 'region)
156 (defun mouse-drag-region (start-event)
157 "Set the region to the text that the mouse is dragged over.
158 Highlight the drag area as the user moves the mouse.
159 This must be bound to a button-down mouse event."
161 (let* ((start-posn (event-start start-event))
162 (start-point (posn-point start-posn))
163 (start-window (posn-window start-posn))
164 (start-frame (window-frame start-window))
165 (bounds (window-edges start-window))
167 (bottom (if (window-minibuffer-p start-window)
169 ;; Don't count the mode line.
170 (1- (nth 3 bounds)))))
171 (select-window start-window)
172 (goto-char start-point)
173 (move-overlay mouse-drag-overlay
174 start-point start-point
175 (window-buffer start-window))
177 (let (event end end-point)
180 (setq event (read-event))
181 (or (mouse-movement-p event)
182 (eq (car-safe event) 'switch-frame)))
184 (if (eq (car-safe event) 'switch-frame)
186 (setq end (event-end event)
187 end-point (posn-point end))
191 ;; Ignore switch-frame events.
192 ((eq (car-safe event) 'switch-frame))
194 ;; Are we moving within the original window?
195 ((and (eq (posn-window end) start-window)
196 (integer-or-marker-p end-point))
197 (goto-char end-point)
198 (move-overlay mouse-drag-overlay
199 start-point (point)))
201 ;; Are we moving on a different window on the same frame?
202 ((and (windowp (posn-window end))
203 (eq (window-frame (posn-window end)) start-frame))
205 (+ (nth 1 (window-edges (posn-window end)))
206 (cdr (posn-col-row end)))))
210 (- mouse-row top) mouse-drag-overlay start-point))
212 (>= mouse-row bottom))
213 (mouse-scroll-subr (1+ (- mouse-row bottom))
214 mouse-drag-overlay start-point)))))
216 ;; Otherwise, we have no idea where the mouse is.
219 (if (and (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
220 (eq (posn-window (event-end event)) start-window)
221 (numberp (posn-point (event-end event))))
222 (goto-char (posn-point (event-end event))))
223 (if (= (point) start-point)
225 (set-mark start-point))
226 (delete-overlay mouse-drag-overlay))))
228 ;;;! (defun mouse-drag-region (click)
229 ;;;! "Set the region to the text that the mouse is dragged over.
230 ;;;! This must be bound to a button-down mouse event."
231 ;;;! (interactive "e")
232 ;;;! (let ((posn (event-start click))
233 ;;;! done event (mark-active nil))
234 ;;;! (select-window (posn-window posn))
235 ;;;! ;; Set point temporarily, so user sees where it is.
236 ;;;! (if (numberp (posn-point posn))
237 ;;;! (goto-char (posn-point posn)))
238 ;;;! ;; Turn off the old mark when we set up an empty region.
239 ;;;! (setq deactivate-mark t)))
241 ;;;! ;;;Nice hack, but too slow, so not normally in use.
242 ;;;! (defun mouse-drag-region-1 (click)
243 ;;;! "Set the region to the text that the mouse is dragged over.
244 ;;;! This must be bound to a button-down mouse event."
245 ;;;! (interactive "e")
247 ;;;! (let ((posn (event-start click))
248 ;;;! done event omark (mark-active t))
249 ;;;! (select-window (posn-window posn))
250 ;;;! (setq omark (and mark-active (mark)))
251 ;;;! (if (numberp (posn-point posn))
252 ;;;! (goto-char (posn-point posn)))
253 ;;;! ;; Set mark temporarily, so highlighting does what we want.
254 ;;;! (set-marker (mark-marker) (point))
256 ;;;! (while (not done)
257 ;;;! (setq event (read-event))
258 ;;;! (if (eq (car-safe event) 'mouse-movement)
259 ;;;! (goto-char (posn-point (event-start event)))
260 ;;;! ;; Exit when we get the drag event; ignore that event.
261 ;;;! (setq done t))))
262 ;;;! (if (/= (mark) (point))
263 ;;;! (setq newmark (mark)))
264 ;;;! ;; Restore previous mark status.
265 ;;;! (if omark (set-marker (mark-marker) omark)))
266 ;;;! ;; Now, if we dragged, set the mark at the proper place.
268 ;;;! (push-mark newmark t t)
269 ;;;! ;; Turn off the old mark when we set up an empty region.
270 ;;;! (setq deactivate-mark t))))
272 ;; Subroutine: set the mark where CLICK happened,
273 ;; but don't do anything else.
274 (defun mouse-set-mark-fast (click)
275 (let ((posn (event-start click)))
276 (select-window (posn-window posn))
277 (if (numberp (posn-point posn))
278 (push-mark (posn-point posn) t t))))
280 ;; Momentarily show where the mark is, if highlighting doesn't show it.
281 (defun mouse-show-mark ()
282 (or transient-mark-mode
287 (defun mouse-set-mark (click)
288 "Set mark at the position clicked on with the mouse.
289 Display cursor at that position for a second.
290 This must be bound to a mouse click."
292 (let ((point-save (point)))
294 (progn (mouse-set-point click)
296 (or transient-mark-mode
298 (goto-char point-save))))
300 (defun mouse-kill (click)
301 "Kill the region between point and the mouse click.
302 The text is saved in the kill ring, as with \\[kill-region]."
304 (let ((click-posn (posn-point (event-start click))))
305 (if (numberp click-posn)
306 (kill-region (min (point) click-posn)
307 (max (point) click-posn)))))
309 (defun mouse-yank-at-click (click arg)
310 "Insert the last stretch of killed text at the position clicked on.
311 Prefix arguments are interpreted as with \\[yank]."
313 (mouse-set-point click)
316 (defun mouse-kill-ring-save (click)
317 "Copy the region between point and the mouse click in the kill ring.
318 This does not delete the region; it acts like \\[kill-ring-save]."
320 (mouse-set-mark-fast click)
321 (kill-ring-save (point) (mark t))
324 ;;; This function used to delete the text between point and the mouse
325 ;;; whenever it was equal to the front of the kill ring, but some
326 ;;; people found that confusing.
328 ;;; A list (TEXT START END), describing the text and position of the last
329 ;;; invocation of mouse-save-then-kill.
330 (defvar mouse-save-then-kill-posn nil)
332 (defun mouse-save-then-kill (click)
333 "Save text to point in kill ring; the second time, kill the text.
334 If the text between point and the mouse is the same as what's
335 at the front of the kill ring, this deletes the text.
336 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
337 which prepares for a second click to delete the text."
339 (let ((click-posn (posn-point (event-start click)))
340 ;; Don't let a subsequent kill command append to this one:
341 ;; prevent setting this-command to kill-region.
342 (this-command this-command))
343 (if (and (eq last-command 'mouse-save-then-kill)
344 mouse-save-then-kill-posn
345 (eq (car mouse-save-then-kill-posn) (car kill-ring))
346 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
347 ;; If this is the second time we've called
348 ;; mouse-save-then-kill, delete the text from the buffer.
350 (let ((buffer-undo-list t))
351 (delete-region (point) (mark)))
352 ;; Make the undo list by hand so it is shared.
353 (if (not (eq buffer-undo-list t))
354 (setq buffer-undo-list
355 (cons (cons (car kill-ring) (point)) buffer-undo-list))))
356 ;; Otherwise, save this region.
357 (mouse-set-mark-fast click)
358 (kill-ring-save (point) (mark t))
360 (setq mouse-save-then-kill-posn
361 (list (car kill-ring) (point) click-posn)))))
363 (global-set-key [M-mouse-1] 'mouse-start-secondary)
364 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
365 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
366 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
367 (global-set-key [M-mouse-2] 'mouse-kill-secondary)
369 ;; An overlay which records the current secondary selection
370 ;; or else is deleted when there is no secondary selection.
372 (defvar mouse-secondary-overlay nil)
374 ;; A marker which records the specified first end for a secondary selection.
376 (defvar mouse-secondary-start nil)
378 (defun mouse-start-secondary (click)
379 "Set one end of the secondary selection to the position clicked on.
380 Use \\[mouse-secondary-save-then-kill] to set the other end
381 and complete the secondary selection."
383 (let ((posn (event-start click)))
385 (set-buffer (window-buffer (posn-window posn)))
386 ;; Cancel any preexisting secondary selection.
387 (if mouse-secondary-overlay
388 (delete-overlay mouse-secondary-overlay))
389 (if (numberp (posn-point posn))
391 (or mouse-secondary-start
392 (setq mouse-secondary-start (make-marker)))
393 (move-marker mouse-secondary-start (posn-point posn)))))))
395 (defun mouse-set-secondary (click)
396 "Set the secondary selection to the text that the mouse is dragged over.
397 This must be bound to a mouse drag event."
399 (let ((posn (event-start click))
401 (end (event-end click)))
403 (set-buffer (window-buffer (posn-window posn)))
404 (if (numberp (posn-point posn))
405 (setq beg (posn-point posn)))
406 (if mouse-secondary-overlay
407 (move-overlay mouse-secondary-overlay beg (posn-point end))
408 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
409 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
411 (defun mouse-drag-secondary (click)
412 "Set the secondary selection to the text that the mouse is dragged over.
413 This must be bound to a button-down mouse event."
415 (let ((posn (event-start click)))
416 (save-window-excursion
417 (select-window (posn-window posn))
418 ;; Set point temporarily, so user sees where it is.
420 (if (numberp (posn-point posn))
421 (goto-char (posn-point posn)))
422 (setq unread-command-events
423 (cons (read-event) unread-command-events))))))
425 (defun mouse-kill-secondary ()
426 "Kill the text in the secondary selection."
428 (kill-region (overlay-start mouse-secondary-overlay)
429 (overlay-end mouse-secondary-overlay))
430 (delete-overlay mouse-secondary-overlay)
431 (setq mouse-secondary-overlay nil))
433 (defun mouse-secondary-save-then-kill (click)
434 "Save text to secondary start point in kill ring; if twice, kill it.
435 If the text between secondary start point and the mouse is the same as what's
436 at the front of the kill ring, this deletes the text.
437 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
438 which prepares for a second click to delete the text."
440 (let ((click-posn (posn-point (event-start click)))
441 (start (+ 0 mouse-secondary-start))
442 ;; Don't let a subsequent kill command append to this one:
443 ;; prevent setting this-command to kill-region.
444 (this-command this-command))
445 (if (and (eq last-command 'mouse-secondary-save-then-kill)
446 mouse-save-then-kill-posn
447 (eq (car mouse-save-then-kill-posn) (car kill-ring))
448 (equal (cdr mouse-save-then-kill-posn)
449 (list start click-posn)))
450 ;; If this is the second time we've called
451 ;; mouse-save-then-kill, delete the text from the buffer.
453 (let ((buffer-undo-list t))
454 (delete-overlay mouse-secondary-overlay)
455 (delete-region start click-posn))
456 ;; Make the undo list by hand so it is shared.
457 (if (not (eq buffer-undo-list t))
458 (setq buffer-undo-list
459 (cons (cons (car kill-ring) (marker-position start))
461 ;; Otherwise, save this region.
463 (set-buffer (window-buffer (posn-window (event-start click))))
464 (kill-ring-save start click-posn)
465 (if mouse-secondary-overlay
466 (move-overlay mouse-secondary-overlay start click-posn)
467 (setq mouse-secondary-overlay (make-overlay start click-posn)))
468 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
469 (setq mouse-save-then-kill-posn
470 (list (car kill-ring) start click-posn))))))
472 (defun mouse-buffer-menu (event)
473 "Pop up a menu of buffers for selection with the mouse.
474 This switches buffers in the window that you clicked on,
475 and selects that window."
479 (cons "Select Buffer"
480 (let ((tail (buffer-list))
484 (or (eq ?\ (aref (buffer-name (car tail)) 0))
487 (length (buffer-name (car tail))))))
488 (setq tail (cdr tail)))
489 (setq tail (buffer-list))
491 (let ((elt (car tail)))
492 (if (not (string-match "^ "
497 (format "%%%ds %%s%%s %%s"
500 (if (buffer-modified-p elt)
504 (if buffer-read-only "%" " "))
505 (or (buffer-file-name elt) ""))
508 (setq tail (cdr tail)))
510 (let ((buf (x-popup-menu event menu))
511 (window (posn-window (event-start event))))
514 (or (framep window) (select-window window))
515 (switch-to-buffer buf))))))
517 ;;; These need to be rewritten for the new scroll bar implementation.
519 ;;;!! ;; Commands for the scroll bar.
521 ;;;!! (defun mouse-scroll-down (click)
522 ;;;!! (interactive "@e")
523 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
525 ;;;!! (defun mouse-scroll-up (click)
526 ;;;!! (interactive "@e")
527 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
529 ;;;!! (defun mouse-scroll-down-full ()
530 ;;;!! (interactive "@")
531 ;;;!! (scroll-down nil))
533 ;;;!! (defun mouse-scroll-up-full ()
534 ;;;!! (interactive "@")
535 ;;;!! (scroll-up nil))
537 ;;;!! (defun mouse-scroll-move-cursor (click)
538 ;;;!! (interactive "@e")
539 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
541 ;;;!! (defun mouse-scroll-absolute (event)
542 ;;;!! (interactive "@e")
543 ;;;!! (let* ((pos (car event))
544 ;;;!! (position (car pos))
545 ;;;!! (length (car (cdr pos))))
546 ;;;!! (if (<= length 0) (setq length 1))
547 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
548 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
551 ;;;!! scale-factor)))
552 ;;;!! (goto-char newpos)
553 ;;;!! (recenter '(4)))))
555 ;;;!! (defun mouse-scroll-left (click)
556 ;;;!! (interactive "@e")
557 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
559 ;;;!! (defun mouse-scroll-right (click)
560 ;;;!! (interactive "@e")
561 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
563 ;;;!! (defun mouse-scroll-left-full ()
564 ;;;!! (interactive "@")
565 ;;;!! (scroll-left nil))
567 ;;;!! (defun mouse-scroll-right-full ()
568 ;;;!! (interactive "@")
569 ;;;!! (scroll-right nil))
571 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
572 ;;;!! (interactive "@e")
573 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
575 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
576 ;;;!! (interactive "@e")
577 ;;;!! (let* ((pos (car event))
578 ;;;!! (position (car pos))
579 ;;;!! (length (car (cdr pos))))
580 ;;;!! (set-window-hscroll (selected-window) 33)))
582 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
583 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
584 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
586 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
587 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
588 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
590 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
591 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
592 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
594 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
595 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
596 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
598 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
599 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
600 ;;;!! 'mouse-scroll-absolute-horizontally)
601 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
603 ;;;!! (global-set-key [horizontal-slider mouse-1]
604 ;;;!! 'mouse-scroll-move-cursor-horizontally)
605 ;;;!! (global-set-key [horizontal-slider mouse-2]
606 ;;;!! 'mouse-scroll-move-cursor-horizontally)
607 ;;;!! (global-set-key [horizontal-slider mouse-3]
608 ;;;!! 'mouse-scroll-move-cursor-horizontally)
610 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
611 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
612 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
614 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
615 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
616 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
618 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
619 ;;;!! 'mouse-split-window-horizontally)
620 ;;;!! (global-set-key [mode-line S-mouse-2]
621 ;;;!! 'mouse-split-window-horizontally)
622 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
623 ;;;!! 'mouse-split-window)
626 ;;;!! ;;;; Here are experimental things being tested. Mouse events
627 ;;;!! ;;;; are of the form:
628 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
631 ;;;!! ;;;; Dynamically track mouse coordinates
634 ;;;!! ;;(defun track-mouse (event)
635 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
636 ;;;!! ;; (interactive "@e")
637 ;;;!! ;; (while mouse-grabbed
638 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
639 ;;;!! ;; (abs-x (car pos))
640 ;;;!! ;; (abs-y (cdr pos))
641 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
642 ;;;!! ;; (list (car pos) (cdr pos))
643 ;;;!! ;; (selected-window))))
644 ;;;!! ;; (if (consp relative-coordinate)
645 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
646 ;;;!! ;; (car relative-coordinate)
647 ;;;!! ;; (car (cdr relative-coordinate)))
648 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
651 ;;;!! ;; Dynamically put a box around the line indicated by point
654 ;;;!! ;;(require 'backquote)
656 ;;;!! ;;(defun mouse-select-buffer-line (event)
657 ;;;!! ;; (interactive "@e")
658 ;;;!! ;; (let ((relative-coordinate
659 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
660 ;;;!! ;; (abs-y (car (cdr (car event)))))
661 ;;;!! ;; (if (consp relative-coordinate)
663 ;;;!! ;; (save-excursion
664 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
665 ;;;!! ;; (x-draw-rectangle
666 ;;;!! ;; (selected-screen)
668 ;;;!! ;; (save-excursion
669 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
670 ;;;!! ;; (end-of-line)
671 ;;;!! ;; (push-mark nil t)
672 ;;;!! ;; (beginning-of-line)
673 ;;;!! ;; (- (region-end) (region-beginning))) 1))
675 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
677 ;;;!! ;;(defvar last-line-drawn nil)
678 ;;;!! ;;(defvar begin-delim "[^ \t]")
679 ;;;!! ;;(defvar end-delim "[^ \t]")
681 ;;;!! ;;(defun mouse-boxing (event)
682 ;;;!! ;; (interactive "@e")
683 ;;;!! ;; (save-excursion
684 ;;;!! ;; (let ((screen (selected-screen)))
685 ;;;!! ;; (while (= (x-mouse-events) 0)
686 ;;;!! ;; (let* ((pos (read-mouse-position screen))
687 ;;;!! ;; (abs-x (car pos))
688 ;;;!! ;; (abs-y (cdr pos))
689 ;;;!! ;; (relative-coordinate
690 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
691 ;;;!! ;; (selected-window)))
692 ;;;!! ;; (begin-reg nil)
693 ;;;!! ;; (end-reg nil)
694 ;;;!! ;; (end-column nil)
695 ;;;!! ;; (begin-column nil))
696 ;;;!! ;; (if (and (consp relative-coordinate)
697 ;;;!! ;; (or (not last-line-drawn)
698 ;;;!! ;; (not (= last-line-drawn abs-y))))
700 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
701 ;;;!! ;; (if (= (following-char) 10)
704 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
705 ;;;!! ;; (setq begin-column (1- (current-column)))
706 ;;;!! ;; (end-of-line)
707 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
708 ;;;!! ;; (setq end-column (1+ (current-column)))
709 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
710 ;;;!! ;; (x-draw-rectangle screen
711 ;;;!! ;; (setq last-line-drawn abs-y)
712 ;;;!! ;; begin-column
713 ;;;!! ;; (- end-column begin-column) 1))))))))))
715 ;;;!! ;;(defun mouse-erase-box ()
716 ;;;!! ;; (interactive)
717 ;;;!! ;; (if last-line-drawn
719 ;;;!! ;; (x-erase-rectangle (selected-screen))
720 ;;;!! ;; (setq last-line-drawn nil))))
722 ;;;!! ;;; (defun test-x-rectangle ()
723 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
724 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
725 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
728 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
731 ;;;!! (defvar double-start nil)
732 ;;;!! (defconst double-click-interval 300
733 ;;;!! "Max ticks between clicks")
735 ;;;!! (defun double-down (event)
736 ;;;!! (interactive "@e")
737 ;;;!! (if double-start
738 ;;;!! (let ((interval (- (nth 4 event) double-start)))
739 ;;;!! (if (< interval double-click-interval)
741 ;;;!! (backward-up-list 1)
742 ;;;!! ;; (message "Interval %d" interval)
743 ;;;!! (sleep-for 1)))
744 ;;;!! (setq double-start nil))
745 ;;;!! (setq double-start (nth 4 event))))
747 ;;;!! (defun double-up (event)
748 ;;;!! (interactive "@e")
749 ;;;!! (and double-start
750 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
751 ;;;!! (setq double-start nil)))
753 ;;;!! ;;; (defun x-test-doubleclick ()
754 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
755 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
756 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
759 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
762 ;;;!! (defvar scrolled-lines 0)
763 ;;;!! (defconst scroll-speed 1)
765 ;;;!! (defun incr-scroll-down (event)
766 ;;;!! (interactive "@e")
767 ;;;!! (setq scrolled-lines 0)
768 ;;;!! (incremental-scroll scroll-speed))
770 ;;;!! (defun incr-scroll-up (event)
771 ;;;!! (interactive "@e")
772 ;;;!! (setq scrolled-lines 0)
773 ;;;!! (incremental-scroll (- scroll-speed)))
775 ;;;!! (defun incremental-scroll (n)
776 ;;;!! (while (= (x-mouse-events) 0)
777 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
778 ;;;!! (scroll-down n)
779 ;;;!! (sit-for 300 t)))
781 ;;;!! (defun incr-scroll-stop (event)
782 ;;;!! (interactive "@e")
783 ;;;!! (message "Scrolled %d lines" scrolled-lines)
784 ;;;!! (setq scrolled-lines 0)
787 ;;;!! ;;; (defun x-testing-scroll ()
788 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
789 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
790 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
791 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
792 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
795 ;;;!! ;; Some playthings suitable for picture mode? They need work.
798 ;;;!! (defun mouse-kill-rectangle (event)
799 ;;;!! "Kill the rectangle between point and the mouse cursor."
800 ;;;!! (interactive "@e")
801 ;;;!! (let ((point-save (point)))
802 ;;;!! (save-excursion
803 ;;;!! (mouse-set-point event)
804 ;;;!! (push-mark nil t)
805 ;;;!! (if (> point-save (point))
806 ;;;!! (kill-rectangle (point) point-save)
807 ;;;!! (kill-rectangle point-save (point))))))
809 ;;;!! (defun mouse-open-rectangle (event)
810 ;;;!! "Kill the rectangle between point and the mouse cursor."
811 ;;;!! (interactive "@e")
812 ;;;!! (let ((point-save (point)))
813 ;;;!! (save-excursion
814 ;;;!! (mouse-set-point event)
815 ;;;!! (push-mark nil t)
816 ;;;!! (if (> point-save (point))
817 ;;;!! (open-rectangle (point) point-save)
818 ;;;!! (open-rectangle point-save (point))))))
820 ;;;!! ;; Must be a better way to do this.
822 ;;;!! (defun mouse-multiple-insert (n char)
825 ;;;!! (setq n (1- n))))
827 ;;;!! ;; What this could do is not finalize until button was released.
829 ;;;!! (defun mouse-move-text (event)
830 ;;;!! "Move text from point to cursor position, inserting spaces."
831 ;;;!! (interactive "@e")
832 ;;;!! (let* ((relative-coordinate
833 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
834 ;;;!! (if (consp relative-coordinate)
835 ;;;!! (cond ((> (current-column) (car relative-coordinate))
837 ;;;!! (- (car relative-coordinate) (current-column))))
838 ;;;!! ((< (current-column) (car relative-coordinate))
839 ;;;!! (mouse-multiple-insert
840 ;;;!! (- (car relative-coordinate) (current-column)) " "))
841 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
843 ;; Choose a completion with the mouse.
845 (defun mouse-choose-completion (event)
849 (set-buffer (window-buffer (posn-window (event-start event))))
851 (goto-char (posn-point (event-start event)))
852 (skip-chars-backward "^ \t\n")
854 (skip-chars-forward "^ \t\n")
855 (setq choice (buffer-substring beg (point))))))
857 (set-buffer (window-buffer (minibuffer-window)))
858 (goto-char (max (point-min) (- (point-max) (length choice))))
859 (while (and (not (eobp))
860 (let ((tail (buffer-substring (point) (point-max))))
861 (not (string= tail (substring choice 0 (length tail))))))
864 (delete-region (point) (point-max)))))
868 (defvar x-fixed-font-alist
871 ("6x10" "-misc-fixed-medium-r-semicondensed--10-110-75-75-c-60-*-1")
872 ("6x12" "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-*-1")
873 ("6x13" "-misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-*-1")
875 "-b&h-lucidatypewriter-medium-r-normal-sans-0-0-0-0-m-0-*-1")
876 ("7x13" "-misc-fixed-medium-r-normal--13-120-75-75-c-70-*-1")
877 ("7x14" "-misc-fixed-medium-r-normal--14-130-75-75-c-70-*-1")
878 ("9x15" "-misc-fixed-medium-r-normal--15-140-*-*-c-*-*-1")
880 ("clean 8x8" "-schumacher-clean-medium-r-normal--*-80-*-*-c-*-*-1")
881 ("clean 8x14" "-schumacher-clean-medium-r-normal--*-140-*-*-c-*-*-1")
882 ("clean 8x10" "-schumacher-clean-medium-r-normal--*-100-*-*-c-*-*-1")
883 ("clean 8x16" "-schumacher-clean-medium-r-normal--*-160-*-*-c-*-*-1")
885 ("sony 8x16" "-sony-fixed-medium-r-normal--16-120-100-100-c-80-*-1")
891 ;;; We don't seem to have these; who knows what they are.
892 ;;; ("fg-18" "fg-18")
893 ;;; ("fg-25" "fg-25")
894 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
895 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
896 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
897 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
898 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
900 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
901 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
902 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
903 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
904 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
905 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
906 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
907 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
908 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
909 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
910 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
911 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
912 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
913 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
914 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
915 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
916 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
917 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
918 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
919 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
920 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
921 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
922 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
923 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
925 "X fonts suitable for use in Emacs.")
927 (defun mouse-set-font (&optional font)
928 "Select an emacs font from a list of known good fonts"
930 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
932 (progn (modify-frame-parameters (selected-frame)
933 (list (cons 'font font)))
934 ;; Update some standard faces too.
935 (set-face-font 'bold nil (selected-frame))
936 (make-face-bold 'bold (selected-frame) t)
937 (set-face-font 'italic nil (selected-frame))
938 (make-face-italic 'italic (selected-frame) t)
939 (set-face-font 'bold-italic nil (selected-frame))
940 (make-face-bold-italic 'bold-italic (selected-frame) t))))
942 ;;; Bindings for mouse commands.
944 (define-key global-map [down-mouse-1] 'mouse-drag-region)
945 (global-set-key [mouse-1] 'mouse-set-point)
946 (global-set-key [drag-mouse-1] 'mouse-set-region)
948 (global-set-key [mouse-2] 'mouse-yank-at-click)
949 (global-set-key [mouse-3] 'mouse-save-then-kill)
951 ;; By binding these to down-going events, we let the user use the up-going
952 ;; event to make the selection, saving a click.
953 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
954 (global-set-key [C-down-mouse-3] 'mouse-set-font)
956 ;; Replaced with dragging mouse-1
957 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
959 (global-set-key [mode-line mouse-1] 'mouse-delete-other-windows)
960 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
961 (global-set-key [mode-line S-mouse-2] 'mouse-split-window-horizontally)
963 ;; Define the mouse help menu tree.
965 (defvar help-menu-map '(keymap "Help"))
966 (global-set-key [C-down-mouse-2] help-menu-map)
968 (defvar help-apropos-map (make-sparse-keymap "Is there a command that..."))
969 (defvar help-keys-map (make-sparse-keymap "Key Commands <==> Functions"))
970 (defvar help-manual-map (make-sparse-keymap "Manual and tutorial"))
971 (defvar help-misc-map (make-sparse-keymap "Odds and ends"))
972 (defvar help-modes-map (make-sparse-keymap "Modes"))
973 (defvar help-admin-map (make-sparse-keymap "Administrivia"))
975 (define-key help-menu-map [apropos]
976 (cons "@Is there a command that..." help-apropos-map))
977 (define-key help-menu-map [keys]
978 (cons "@Key Commands <==> Functions" help-keys-map))
979 (define-key help-menu-map [manuals]
980 (cons "@Manual and tutorial" help-manual-map))
981 (define-key help-menu-map [misc]
982 (cons "@Odds and ends" help-misc-map))
983 (define-key help-menu-map [modes]
984 (cons "@Modes" help-modes-map))
985 (define-key help-menu-map [admin]
986 (cons "@Administrivia" help-admin-map))
988 (define-key help-apropos-map "c" '("Command Apropos" . command-apropos))
989 (define-key help-apropos-map "a" '("Apropos" . apropos))
991 (define-key help-keys-map "b"
992 '("List all keystroke commands" . describe-bindings))
993 (define-key help-keys-map "c"
994 '("Describe key briefly" . describe-key-briefly))
995 (define-key help-keys-map "k"
996 '("Describe key verbose" . describe-key))
997 (define-key help-keys-map "f"
998 '("Describe Lisp function" . describe-function))
999 (define-key help-keys-map "w"
1000 '("Where is this command" . where-is))
1002 (define-key help-manual-map "i" '("Info system" . info))
1003 (define-key help-manual-map "t"
1004 '("Invoke Emacs tutorial" . help-with-tutorial))
1006 (define-key help-misc-map "l" '("Last 100 Keystrokes" . view-lossage))
1007 (define-key help-misc-map "s" '("Describe syntax table" . describe-syntax))
1009 (define-key help-modes-map "m"
1010 '("Describe current major mode" . describe-mode))
1011 (define-key help-modes-map "b"
1012 '("List all keystroke commands" . describe-bindings))
1014 (define-key help-admin-map "n"
1015 '("view Emacs news" . view-emacs-news))
1016 (define-key help-admin-map "l"
1017 '("View the GNU Emacs license" . describe-copying))
1018 (define-key help-admin-map "d"
1019 '("Describe distribution" . describe-distribution))
1020 (define-key help-admin-map "w"
1021 '("Describe (non)warranty" . describe-no-warranty))
1025 ;;; mouse.el ends here