1 ;;; mouse.el --- window system-independent mouse support.
3 ;;; Copyright (C) 1988, 1992 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.
25 ;;; Utility functions.
27 (defun mouse-movement-p (event)
29 (eq (car event
) 'mouse-movement
)))
31 (defun event-window (event) (nth 1 event
))
32 (defun event-point (event) (nth 2 event
))
33 (defun mouse-coords (event) (nth 3 event
))
34 (defun mouse-timestamp (event) (nth 4 event
))
36 ;;; Indent track-mouse like progn.
37 (put 'track-mouse
'lisp-indent-function
0)
40 (defun mouse-delete-window (click)
41 "Delete the window clicked on.
42 This must be bound to a mouse click."
44 (delete-window (event-window click
)))
46 (defun mouse-delete-other-windows (click)
47 "Select Emacs window clicked on, then kill all other Emacs windows.
48 This must be bound to a mouse click."
50 (select-window (event-window click
))
51 (delete-other-windows))
53 (defun mouse-split-window-vertically (click)
54 "Select Emacs window mouse is on, then split it vertically in half.
55 The window is split at the line clicked on.
56 This command must be bound to a mouse click."
58 (select-window (event-window click
))
59 (split-window-vertically (1+ (cdr (mouse-coords click
)))))
61 (defun mouse-set-point (click)
62 "Move point to the position clicked on with the mouse.
63 This must be bound to a mouse click."
65 (select-window (event-window click
))
66 (if (numberp (event-point click
))
67 (goto-char (event-point click
))))
69 (defun mouse-set-mark (click)
70 "Set mark at the position clicked on with the mouse.
71 Display cursor at that position for a second.
72 This must be bound to a mouse click."
74 (let ((point-save (point)))
76 (progn (mouse-set-point click
)
79 (goto-char point-save
))))
81 (defun mouse-kill (click)
82 "Kill the region between point and the mouse click.
83 The text is saved in the kill ring, as with \\[kill-region]."
85 (let ((click-posn (event-point click
)))
86 (if (numberp click-posn
)
87 (kill-region (min (point) click-posn
)
88 (max (point) click-posn
)))))
90 (defun mouse-yank-at-click (click arg
)
91 "Insert the last stretch of killed text at the position clicked on.
92 Prefix arguments are interpreted as with \\[yank]."
94 (mouse-set-point click
)
97 (defun mouse-kill-ring-save (click)
98 "Copy the region between point and the mouse click in the kill ring.
99 This does not delete the region; it acts like \\[kill-ring-save]."
101 (mouse-set-mark click
)
102 (call-interactively 'kill-ring-save
))
104 (defun mouse-buffer-menu (event)
105 "Pop up a menu of buffers for selection with the mouse."
109 (cons "Select Buffer"
110 (let ((tail (buffer-list))
113 (let ((elt (car tail
)))
114 (if (not (string-match "^ "
121 (or (buffer-file-name elt
) ""))
124 (setq tail
(cdr tail
)))
126 (switch-to-buffer (or (x-popup-menu event menu
) (current-buffer)))))
128 ;; Commands for the scroll bar.
130 (defun mouse-scroll-down (nlines)
132 (scroll-down nlines
))
134 (defun mouse-scroll-up (nlines)
138 (defun mouse-scroll-down-full ()
142 (defun mouse-scroll-up-full ()
146 (defun mouse-scroll-move-cursor (nlines)
148 (move-to-window-line nlines
))
150 (defun mouse-scroll-absolute (event)
152 (let* ((pos (car event
))
154 (length (car (cdr pos
))))
155 (if (<= length
0) (setq length
1))
156 (let* ((scale-factor (max 1 (/ length
(/ 8000000 (buffer-size)))))
157 (newpos (* (/ (* (/ (buffer-size) scale-factor
)
164 (defun mouse-scroll-left (ncolumns)
166 (scroll-left ncolumns
))
168 (defun mouse-scroll-right (ncolumns)
170 (scroll-right ncolumns
))
172 (defun mouse-scroll-left-full ()
176 (defun mouse-scroll-right-full ()
180 (defun mouse-scroll-move-cursor-horizontally (ncolumns)
182 (move-to-column ncolumns
))
184 (defun mouse-scroll-absolute-horizontally (event)
186 (let* ((pos (car event
))
188 (length (car (cdr pos
))))
189 (set-window-hscroll (selected-window) 33)))
191 (global-set-key [scroll-bar mouse-1
] 'mouse-scroll-up
)
192 (global-set-key [scroll-bar mouse-2
] 'mouse-scroll-absolute
)
193 (global-set-key [scroll-bar mouse-3
] 'mouse-scroll-down
)
195 (global-set-key [vertical-slider mouse-1
] 'mouse-scroll-move-cursor
)
196 (global-set-key [vertical-slider mouse-2
] 'mouse-scroll-move-cursor
)
197 (global-set-key [vertical-slider mouse-3
] 'mouse-scroll-move-cursor
)
199 (global-set-key [thumbup mouse-1
] 'mouse-scroll-up-full
)
200 (global-set-key [thumbup mouse-2
] 'mouse-scroll-up-full
)
201 (global-set-key [thumbup mouse-3
] 'mouse-scroll-up-full
)
203 (global-set-key [thumbdown mouse-1
] 'mouse-scroll-down-full
)
204 (global-set-key [thumbdown mouse-2
] 'mouse-scroll-down-full
)
205 (global-set-key [thumbdown mouse-3
] 'mouse-scroll-down-full
)
207 (global-set-key [horizontal-scroll-bar mouse-1
] 'mouse-scroll-left
)
208 (global-set-key [horizontal-scroll-bar mouse-2
]
209 'mouse-scroll-absolute-horizontally
)
210 (global-set-key [horizontal-scroll-bar mouse-3
] 'mouse-scroll-right
)
212 (global-set-key [horizontal-slider mouse-1
]
213 'mouse-scroll-move-cursor-horizontally
)
214 (global-set-key [horizontal-slider mouse-2
]
215 'mouse-scroll-move-cursor-horizontally
)
216 (global-set-key [horizontal-slider mouse-3
]
217 'mouse-scroll-move-cursor-horizontally
)
219 (global-set-key [thumbleft mouse-1
] 'mouse-scroll-left-full
)
220 (global-set-key [thumbleft mouse-2
] 'mouse-scroll-left-full
)
221 (global-set-key [thumbleft mouse-3
] 'mouse-scroll-left-full
)
223 (global-set-key [thumbright mouse-1
] 'mouse-scroll-right-full
)
224 (global-set-key [thumbright mouse-2
] 'mouse-scroll-right-full
)
225 (global-set-key [thumbright mouse-3
] 'mouse-scroll-right-full
)
228 ;;;; Here are experimental things being tested. Mouse events
229 ;;;; are of the form:
230 ;;;; ((x y) window screen-part key-sequence timestamp)
233 ;;;; Dynamically track mouse coordinates
236 ;;(defun track-mouse (event)
237 ;; "Track the coordinates, absolute and relative, of the mouse."
238 ;; (interactive "@e")
239 ;; (while mouse-grabbed
240 ;; (let* ((pos (read-mouse-position (selected-screen)))
243 ;; (relative-coordinate (coordinates-in-window-p
244 ;; (list (car pos) (cdr pos))
245 ;; (selected-window))))
246 ;; (if (consp relative-coordinate)
247 ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
248 ;; (car relative-coordinate)
249 ;; (car (cdr relative-coordinate)))
250 ;; (message "mouse: [%d %d]" abs-x abs-y)))))
253 ;; Dynamically put a box around the line indicated by point
256 ;;(require 'backquote)
258 ;;(defun mouse-select-buffer-line (event)
259 ;; (interactive "@e")
260 ;; (let ((relative-coordinate
261 ;; (coordinates-in-window-p (car event) (selected-window)))
262 ;; (abs-y (car (cdr (car event)))))
263 ;; (if (consp relative-coordinate)
266 ;; (move-to-window-line (car (cdr relative-coordinate)))
271 ;; (move-to-window-line (car (cdr relative-coordinate)))
274 ;; (beginning-of-line)
275 ;; (- (region-end) (region-beginning))) 1))
277 ;; (x-erase-rectangle (selected-screen))))))
279 ;;(defvar last-line-drawn nil)
280 ;;(defvar begin-delim "[^ \t]")
281 ;;(defvar end-delim "[^ \t]")
283 ;;(defun mouse-boxing (event)
284 ;; (interactive "@e")
286 ;; (let ((screen (selected-screen)))
287 ;; (while (= (x-mouse-events) 0)
288 ;; (let* ((pos (read-mouse-position screen))
291 ;; (relative-coordinate
292 ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
293 ;; (selected-window)))
297 ;; (begin-column nil))
298 ;; (if (and (consp relative-coordinate)
299 ;; (or (not last-line-drawn)
300 ;; (not (= last-line-drawn abs-y))))
302 ;; (move-to-window-line (car (cdr relative-coordinate)))
303 ;; (if (= (following-char) 10)
306 ;; (setq begin-reg (1- (re-search-forward end-delim)))
307 ;; (setq begin-column (1- (current-column)))
309 ;; (setq end-reg (1+ (re-search-backward begin-delim)))
310 ;; (setq end-column (1+ (current-column)))
311 ;; (message "%s" (buffer-substring begin-reg end-reg))
312 ;; (x-draw-rectangle screen
313 ;; (setq last-line-drawn abs-y)
315 ;; (- end-column begin-column) 1))))))))))
317 ;;(defun mouse-erase-box ()
319 ;; (if last-line-drawn
321 ;; (x-erase-rectangle (selected-screen))
322 ;; (setq last-line-drawn nil))))
324 ;;; (defun test-x-rectangle ()
325 ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
326 ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
327 ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
330 ;; Here is how to do double clicking in lisp. About to change.
333 (defvar double-start nil
)
334 (defconst double-click-interval
300
335 "Max ticks between clicks")
337 (defun double-down (event)
340 (let ((interval (- (nth 4 event
) double-start
)))
341 (if (< interval double-click-interval
)
344 ;; (message "Interval %d" interval)
346 (setq double-start nil
))
347 (setq double-start
(nth 4 event
))))
349 (defun double-up (event)
352 (> (- (nth 4 event
) double-start
) double-click-interval
)
353 (setq double-start nil
)))
355 ;;; (defun x-test-doubleclick ()
356 ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
357 ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
358 ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
361 ;; This scrolls while button is depressed. Use preferable in scrollbar.
364 (defvar scrolled-lines
0)
365 (defconst scroll-speed
1)
367 (defun incr-scroll-down (event)
369 (setq scrolled-lines
0)
370 (incremental-scroll scroll-speed
))
372 (defun incr-scroll-up (event)
374 (setq scrolled-lines
0)
375 (incremental-scroll (- scroll-speed
)))
377 (defun incremental-scroll (n)
378 (while (= (x-mouse-events) 0)
379 (setq scrolled-lines
(1+ (* scroll-speed scrolled-lines
)))
383 (defun incr-scroll-stop (event)
385 (message "Scrolled %d lines" scrolled-lines
)
386 (setq scrolled-lines
0)
389 ;;; (defun x-testing-scroll ()
390 ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
391 ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
392 ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
393 ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
394 ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
397 ;; Some playthings suitable for picture mode? They need work.
400 (defun mouse-kill-rectangle (event)
401 "Kill the rectangle between point and the mouse cursor."
403 (let ((point-save (point)))
405 (mouse-set-point event
)
407 (if (> point-save
(point))
408 (kill-rectangle (point) point-save
)
409 (kill-rectangle point-save
(point))))))
411 (defun mouse-open-rectangle (event)
412 "Kill the rectangle between point and the mouse cursor."
414 (let ((point-save (point)))
416 (mouse-set-point event
)
418 (if (> point-save
(point))
419 (open-rectangle (point) point-save
)
420 (open-rectangle point-save
(point))))))
422 ;; Must be a better way to do this.
424 (defun mouse-multiple-insert (n char
)
429 ;; What this could do is not finalize until button was released.
431 (defun mouse-move-text (event)
432 "Move text from point to cursor position, inserting spaces."
434 (let* ((relative-coordinate
435 (coordinates-in-window-p (car event
) (selected-window))))
436 (if (consp relative-coordinate
)
437 (cond ((> (current-column) (car relative-coordinate
))
439 (- (car relative-coordinate
) (current-column))))
440 ((< (current-column) (car relative-coordinate
))
441 (mouse-multiple-insert
442 (- (car relative-coordinate
) (current-column)) " "))
443 ((= (current-column) (car relative-coordinate
)) (ding))))))
446 ;;; Bindings for mouse commands.
448 ;; This won't be needed once the drag and down events
449 ;; are properly implemented.
450 (global-set-key [mouse-1
] 'mouse-set-point
)
452 (global-set-key [down-mouse-1
] 'mouse-set-point
)
453 (global-set-key [drag-mouse-1
] 'mouse-set-mark
)
454 (global-set-key [mouse-2
] 'mouse-yank-at-click
)
455 (global-set-key [mouse-3
] 'mouse-kill-ring-save
)
456 (global-set-key [S-mouse-3
] 'mouse-kill
)
458 (global-set-key [C-mouse-1
] 'mouse-buffer-menu
)
460 ;; Replaced with dragging mouse-1
461 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
463 ;; Define the mouse help menu tree.
465 (defvar help-menu-map
'(keymap "Help"))
466 (global-set-key [C-mouse-2
] help-menu-map
)
468 (defvar help-apropos-map
'(keymap "Is there a command that..."))
469 (defvar help-keys-map
'(keymap "Key Commands <==> Functions"))
470 (defvar help-manual-map
'(keymap "Manual and tutorial"))
471 (defvar help-misc-map
'(keymap "Odds and ends"))
472 (defvar help-modes-map
'(keymap "Modes"))
473 (defvar help-admin-map
'(keymap "Administrivia"))
475 (define-key help-menu-map
[apropos]
476 (cons "@Is there a command that..." help-apropos-map))
477 (define-key help-menu-map [keys]
478 (cons "@Key Commands <==> Functions" help-keys-map))
479 (define-key help-menu-map [manuals]
480 (cons "@Manual and tutorial" help-manual-map))
481 (define-key help-menu-map [misc]
482 (cons "@Odds and ends" help-misc-map))
483 (define-key help-menu-map [modes]
484 (cons "@Modes" help-modes-map))
485 (define-key help-menu-map [admin]
486 (cons "@Administrivia" help-admin-map))
488 (define-key help-apropos-map "c" '("Command Apropos" . command-apropos))
489 (define-key help-apropos-map "a" '("Apropos" . apropos))
491 (define-key help-keys-map "b"
492 '("List all keystroke commands" . describe-bindings))
493 (define-key help-keys-map "c"
494 '("Describe key briefly" . describe-key-briefly))
495 (define-key help-keys-map "k"
496 '("Describe key verbose" . describe-key))
497 (define-key help-keys-map "f"
498 '("Describe Lisp function" . describe-function))
499 (define-key help-keys-map "w"
500 '("Where is this command" . where-is))
502 (define-key help-manual-map "i" '("Info system" . info))
503 (define-key help-manual-map "t"
504 '("Invoke Emacs tutorial" . help-with-tutorial))
506 (define-key help-misc-map "l" '("Last 100 Keystrokes" . view-lossage))
507 (define-key help-misc-map "s" '("Describe syntax table" . describe-syntax))
509 (define-key help-modes-map "m"
510 '("Describe current major mode" . describe-mode))
511 (define-key help-modes-map "b"
512 '("List all keystroke commands" . describe-bindings))
514 (define-key help-admin-map "n"
515 '("view Emacs news" . view-emacs-news))
516 (define-key help-admin-map "l"
517 '("View the GNU Emacs license" . describe-copying))
518 (define-key help-admin-map "d"
519 '("Describe distribution" . describe-distribution))
520 (define-key help-admin-map "w"
521 '("Describe (non)warranty" . describe-no-warranty))
525 ;;; mouse.el ends here