1 ;;; mouse.el --- window system-independent mouse support.
3 ;;; Copyright (C) 1988 Free Software Foundation, Inc.
5 ;;; This file is part of GNU Emacs.
7 ;;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;;; it under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 1, or (at your option)
10 ;;; any later version.
12 ;;; GNU Emacs is distributed in the hope that it will be useful,
13 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;;; Utility functions.
24 (defun mouse-movement-p (event)
26 (eq (car event
) 'mouse-movement
)))
28 (defun event-window (event) (nth 1 event
))
29 (defun event-point (event) (nth 2 event
))
30 (defun mouse-coords (event) (nth 3 event
))
31 (defun mouse-timestamp (event) (nth 4 event
))
33 ;;; Indent track-mouse like progn.
34 (put 'track-mouse
'lisp-indent-function
0)
37 (defun mouse-delete-window (click)
38 "Delete the window clicked on.
39 This must be bound to a mouse click."
41 (delete-window (event-window click
)))
43 (defun mouse-delete-other-windows (click)
44 "Select Emacs window clicked on, then kill all other Emacs windows.
45 This must be bound to a mouse click."
47 (select-window (event-window click
))
48 (delete-other-windows))
50 (defun mouse-split-window-vertically (click)
51 "Select Emacs window mouse is on, then split it vertically in half.
52 The window is split at the line clicked on.
53 This command must be bound to a mouse click."
55 (select-window (event-window click
))
56 (split-window-vertically (1+ (cdr (mouse-coords click
)))))
58 (defun mouse-set-point (click)
59 "Move point to the position clicked on with the mouse.
60 This must be bound to a mouse click."
62 (select-window (event-window click
))
63 (goto-char (event-point click
)))
65 (defun mouse-set-mark (click)
66 "Set mark at the position clicked on with the mouse.
67 Display cursor at that position for a second.
68 This must be bound to a mouse click."
70 (let ((point-save (point)))
72 (progn (mouse-set-point click
)
75 (goto-char point-save
))))
77 (defun mouse-kill (click)
78 "Kill the region between point and the mouse click.
79 The text is saved in the kill ring, as with \\[kill-region]."
81 (mouse-set-mark click
)
84 (defun mouse-kill-ring-save
85 "Copy the region between point and the mouse click in the kill ring.
86 This does not delete the region; it acts like \\[kill-ring-save]."
88 (mouse-set-mark click
)
93 ;; Commands for the scroll bar.
95 (defun mouse-scroll-down (nlines)
99 (defun mouse-scroll-up (nlines)
103 (defun mouse-scroll-down-full ()
107 (defun mouse-scroll-up-full ()
111 (defun mouse-scroll-move-cursor (nlines)
113 (move-to-window-line nlines
))
115 (defun mouse-scroll-absolute (event)
117 (let* ((pos (car event
))
119 (length (car (cdr pos
))))
120 (if (<= length
0) (setq length
1))
121 (let* ((scale-factor (max 1 (/ length
(/ 8000000 (buffer-size)))))
122 (newpos (* (/ (* (/ (buffer-size) scale-factor
)
129 (defun mouse-scroll-left (ncolumns)
131 (scroll-left ncolumns
))
133 (defun mouse-scroll-right (ncolumns)
135 (scroll-right ncolumns
))
137 (defun mouse-scroll-left-full ()
141 (defun mouse-scroll-right-full ()
145 (defun mouse-scroll-move-cursor-horizontally (ncolumns)
147 (move-to-column ncolumns
))
149 (defun mouse-scroll-absolute-horizontally (event)
151 (let* ((pos (car event
))
153 (length (car (cdr pos
))))
154 (set-window-hscroll (selected-window) 33)))
156 ;; Set up these commands, including the prefix keys for the scroll bar.
158 ;;; (fset 'mouse-vertical-scroll-bar-prefix (make-sparse-keymap))
159 ;;; (define-key global-mouse-map mouse-vertical-scroll-bar-prefix
160 ;;; 'mouse-vertical-scroll-bar-prefix)
162 ;;; (defun mouse-scroll-motion (event)
163 ;;; (interactive "e")
164 ;;; (let ((pos (car (car event)))
165 ;;; (length (car (cdr (car event)))))
166 ;;; (message "[%d %d]" pos length)))
168 ;;; (let ((map (function mouse-vertical-scroll-bar-prefix)))
169 ;;; (define-key map mouse-button-right 'mouse-scroll-down)
170 ;;; (define-key map mouse-button-left 'mouse-scroll-up)
171 ;;; (define-key map mouse-button-middle 'mouse-scroll-absolute)
172 ;;; (define-key map mouse-motion 'x-horizontal-line))
174 ;;; ;(fset 'mouse-vertical-slider-prefix (make-sparse-keymap))
175 ;;; ;(define-key global-mouse-map mouse-vertical-slider-prefix
176 ;;; ; 'mouse-vertical-slider-prefix)
178 ;;; ;(let ((map (function mouse-vertical-slider-prefix)))
179 ;;; ; (define-key map mouse-button-right 'mouse-scroll-move-cursor)
180 ;;; ; (define-key map mouse-button-left 'mouse-scroll-move-cursor)
181 ;;; ; (define-key map mouse-button-middle 'mouse-scroll-move-cursor))
183 ;;; (fset 'mouse-vertical-thumbup-prefix (make-sparse-keymap))
184 ;;; (define-key global-mouse-map mouse-vertical-thumbup-prefix
185 ;;; 'mouse-vertical-thumbup-prefix)
187 ;;; (let ((map (function mouse-vertical-thumbup-prefix)))
188 ;;; (define-key map mouse-button-right 'mouse-scroll-down-full)
189 ;;; (define-key map mouse-button-left 'mouse-scroll-down-full)
190 ;;; (define-key map mouse-button-middle 'mouse-scroll-down-full))
192 ;;; (fset 'mouse-vertical-thumbdown-prefix (make-sparse-keymap))
193 ;;; (define-key global-mouse-map mouse-vertical-thumbdown-prefix
194 ;;; 'mouse-vertical-thumbdown-prefix)
196 ;;; (let ((map (function mouse-vertical-thumbdown-prefix)))
197 ;;; (define-key map mouse-button-right 'mouse-scroll-up-full)
198 ;;; (define-key map mouse-button-left 'mouse-scroll-up-full)
199 ;;; (define-key map mouse-button-middle 'mouse-scroll-up-full))
201 ;;; ;; Horizontal bar
203 ;;; (fset 'mouse-horizontal-scroll-bar-prefix (make-sparse-keymap))
204 ;;; (define-key global-mouse-map mouse-horizontal-scroll-bar-prefix
205 ;;; 'mouse-horizontal-scroll-bar-prefix)
207 ;;; (let ((map (function mouse-horizontal-scroll-bar-prefix)))
208 ;;; (define-key map mouse-button-right 'mouse-scroll-right)
209 ;;; (define-key map mouse-button-left 'mouse-scroll-left)
210 ;;; (define-key map mouse-button-middle 'mouse-scroll-absolute-horizontally))
212 ;;; (fset 'mouse-horizontal-thumbleft-prefix (make-sparse-keymap))
213 ;;; (define-key global-mouse-map mouse-horizontal-thumbleft-prefix
214 ;;; 'mouse-horizontal-thumbleft-prefix)
216 ;;; (let ((map (function mouse-horizontal-thumbleft-prefix)))
217 ;;; (define-key map mouse-button-right 'mouse-scroll-left-full)
218 ;;; (define-key map mouse-button-left 'mouse-scroll-left-full)
219 ;;; (define-key map mouse-button-middle 'mouse-scroll-left-full))
221 ;;; (fset 'mouse-horizontal-thumbright-prefix (make-sparse-keymap))
222 ;;; (define-key global-mouse-map mouse-horizontal-thumbright-prefix
223 ;;; 'mouse-horizontal-thumbright-prefix)
225 ;;; (let ((map (function mouse-horizontal-thumbright-prefix)))
226 ;;; (define-key map mouse-button-right 'mouse-scroll-right-full)
227 ;;; (define-key map mouse-button-left 'mouse-scroll-right-full)
228 ;;; (define-key map mouse-button-middle 'mouse-scroll-right-full))
232 ;;;; Here are experimental things being tested. Mouse events
233 ;;;; are of the form:
234 ;;;; ((x y) window screen-part key-sequence timestamp)
237 ;;;; Dynamically track mouse coordinates
240 ;;(defun track-mouse (event)
241 ;; "Track the coordinates, absolute and relative, of the mouse."
242 ;; (interactive "@e")
243 ;; (while mouse-grabbed
244 ;; (let* ((pos (read-mouse-position (selected-screen)))
247 ;; (relative-coordinate (coordinates-in-window-p
248 ;; (list (car pos) (cdr pos))
249 ;; (selected-window))))
250 ;; (if (consp relative-coordinate)
251 ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
252 ;; (car relative-coordinate)
253 ;; (car (cdr relative-coordinate)))
254 ;; (message "mouse: [%d %d]" abs-x abs-y)))))
257 ;; Dynamically put a box around the line indicated by point
262 (defun mouse-select-buffer-line (event)
264 (let ((relative-coordinate
265 (coordinates-in-window-p (car event
) (selected-window)))
266 (abs-y (car (cdr (car event
)))))
267 (if (consp relative-coordinate
)
270 (move-to-window-line (car (cdr relative-coordinate
)))
275 (move-to-window-line (car (cdr relative-coordinate
)))
279 (- (region-end) (region-beginning))) 1)
280 (setq the-buffer
(Buffer-menu-buffer t
)))
282 (x-erase-rectangle (selected-screen))))))
284 (defvar last-line-drawn nil
)
285 (defvar begin-delim
"[^ \t]")
286 (defvar end-delim
"[^ \t]")
288 (defun mouse-boxing (event)
291 (let ((screen (selected-screen)))
292 (while (= (x-mouse-events) 0)
293 (let* ((pos (read-mouse-position screen
))
297 (coordinates-in-window-p (` ((, abs-x
) (, abs-y
)))
303 (if (and (consp relative-coordinate
)
304 (or (not last-line-drawn
)
305 (not (= last-line-drawn abs-y
))))
307 (move-to-window-line (car (cdr relative-coordinate
)))
308 (if (= (following-char) 10)
311 (setq begin-reg
(1- (re-search-forward end-delim
)))
312 (setq begin-column
(1- (current-column)))
314 (setq end-reg
(1+ (re-search-backward begin-delim
)))
315 (setq end-column
(1+ (current-column)))
316 (message "%s" (buffer-substring begin-reg end-reg
))
317 (x-draw-rectangle screen
318 (setq last-line-drawn abs-y
)
320 (- end-column begin-column
) 1))))))))))
322 (defun mouse-erase-box ()
326 (x-erase-rectangle (selected-screen))
327 (setq last-line-drawn nil
))))
329 ;;; (defun test-x-rectangle ()
330 ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
331 ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
332 ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
335 ;; Here is how to do double clicking in lisp. About to change.
338 (defvar double-start nil
)
339 (defconst double-click-interval
300
340 "Max ticks between clicks")
342 (defun double-down (event)
345 (let ((interval (- (nth 4 event
) double-start
)))
346 (if (< interval double-click-interval
)
349 ;; (message "Interval %d" interval)
351 (setq double-start nil
))
352 (setq double-start
(nth 4 event
))))
354 (defun double-up (event)
357 (> (- (nth 4 event
) double-start
) double-click-interval
)
358 (setq double-start nil
)))
360 ;;; (defun x-test-doubleclick ()
361 ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
362 ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
363 ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
366 ;; This scrolls while button is depressed. Use preferable in scrollbar.
369 (defvar scrolled-lines
0)
370 (defconst scroll-speed
1)
372 (defun incr-scroll-down (event)
374 (setq scrolled-lines
0)
375 (incremental-scroll scroll-speed
))
377 (defun incr-scroll-up (event)
379 (setq scrolled-lines
0)
380 (incremental-scroll (- scroll-speed
)))
382 (defun incremental-scroll (n)
383 (while (= (x-mouse-events) 0)
384 (setq scrolled-lines
(1+ (* scroll-speed scrolled-lines
)))
388 (defun incr-scroll-stop (event)
390 (message "Scrolled %d lines" scrolled-lines
)
391 (setq scrolled-lines
0)
394 ;;; (defun x-testing-scroll ()
395 ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
396 ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
397 ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
398 ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
399 ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
402 ;; Some playthings suitable for picture mode? They need work.
405 (defun mouse-kill-rectangle (event)
406 "Kill the rectangle between point and the mouse cursor."
408 (let ((point-save (point)))
410 (mouse-set-point event
)
412 (if (> point-save
(point))
413 (kill-rectangle (point) point-save
)
414 (kill-rectangle point-save
(point))))))
416 (defun mouse-open-rectangle (event)
417 "Kill the rectangle between point and the mouse cursor."
419 (let ((point-save (point)))
421 (mouse-set-point event
)
423 (if (> point-save
(point))
424 (open-rectangle (point) point-save
)
425 (open-rectangle point-save
(point))))))
427 ;; Must be a better way to do this.
429 (defun mouse-multiple-insert (n char
)
434 ;; What this could do is not finalize until button was released.
436 (defun mouse-move-text (event)
437 "Move text from point to cursor position, inserting spaces."
439 (let* ((relative-coordinate
440 (coordinates-in-window-p (car event
) (selected-window))))
441 (if (consp relative-coordinate
)
442 (cond ((> (current-column) (car relative-coordinate
))
444 (- (car relative-coordinate
) (current-column))))
445 ((< (current-column) (car relative-coordinate
))
446 (mouse-multiple-insert
447 (- (car relative-coordinate
) (current-column)) " "))
448 ((= (current-column) (car relative-coordinate
)) (ding))))))
451 ;;; Bindings for mouse commands.
453 (global-set-key [mouse-1
] 'mouse-set-point
)
454 (global-set-key [S-mouse-1
] 'mouse-set-mark
)
455 (global-set-key [mouse-3
] 'mouse-delete-other-windows
)
459 ;;; mouse.el ends here