1 ;; Mouse support that is independent of window systems.
2 ;; Copyright (C) 1988 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 (defun mouse-select ()
24 "Select the Emacs window the mouse is on."
27 (defun mouse-delete-window ()
28 "Delete the Emacs window the mouse is on."
32 (defun mouse-keep-one-window ()
33 "Select Emacs window mouse is on, then kill all other Emacs windows."
35 (delete-other-windows))
37 (defun mouse-select-and-split ()
38 "Select Emacs window mouse is on, then split it vertically in half."
40 (split-window-vertically nil
))
42 (defun mouse-set-point (event)
43 "Select Emacs window mouse is on, and move point to mouse position."
45 (let ((relative-coordinate
46 (coordinates-in-window-p (car event
) (selected-window))))
47 (if (consp relative-coordinate
)
49 (move-to-window-line (car (cdr relative-coordinate
)))
50 ;; Note that hscroll must get above 1
51 ;; before the text actually starts to move.
52 (move-to-column (+ (car relative-coordinate
) (current-column)
53 (1- (max 1 (window-hscroll (selected-window))))))
56 (defun mouse-eval-last-sexpr (event)
59 (mouse-set-point event
)
60 (eval-last-sexp nil
)))
62 (defun mouse-line-length (event)
63 "Print the length of the line indicated by the pointer."
65 (let ((relative-coordinate
66 (coordinates-in-window-p (car event
) (selected-window))))
67 (if (consp relative-coordinate
)
69 (move-to-window-line (car (cdr relative-coordinate
)))
73 (message "Line length: %d"
74 (- (region-end) (region-beginning)))
77 (defun mouse-set-mark (event)
78 "Select Emacs window mouse is on, and set mark at mouse position.
79 Display cursor at that position for a second."
81 (let ((point-save (point)))
83 (progn (mouse-set-point event
)
86 (goto-char point-save
))))
88 (defun mouse-fill-paragraph (event)
89 "Fill the paragraph at the mouse position."
92 (mouse-set-point event
)
95 (defun mouse-fill-paragraph-with-prefix (event)
96 "Fill the paragraph at the mouse position with specified fill prefix.
97 Click at the end of the fill prefix that you want;
98 The text before the mouse position, on the same line, is used as the prefix."
101 (mouse-set-point event
)
102 (let ((fill-prefix (buffer-substring (save-excursion (beginning-of-line)
107 (defun mouse-scroll (event)
108 "Scroll point to the mouse position."
110 (let ((relative-coordinate
111 (coordinates-in-window-p (car event
) (selected-window))))
112 (if (consp relative-coordinate
)
114 (recenter (car (cdr relative-coordinate
)))
115 (scroll-right (+ (car relative-coordinate
) (current-column)))))))
117 (defun mouse-del-char (event)
118 "Delete the char pointed to by the mouse."
120 (let ((relative-coordinate
121 (coordinates-in-window-p (car event
) (selected-window))))
122 (if (consp relative-coordinate
)
124 (move-to-window-line (car (cdr relative-coordinate
)))
125 (move-to-column (+ (car relative-coordinate
) (current-column)))
126 (delete-char 1 nil
)))))
128 (defun mouse-kill-line (event)
129 "Kill the line pointed to by the mouse."
131 (let ((relative-coordinate
132 (coordinates-in-window-p (car event
) (selected-window))))
133 (if (consp relative-coordinate
)
135 (move-to-window-line (car (cdr relative-coordinate
)))
136 (move-to-column (+ (car relative-coordinate
) (current-column)))
139 (defun narrow-window-to-region (m n
)
140 "Narrow window to region between point and last mark"
144 (if (eq (selected-window) (next-window))
148 (if (eq (selected-window)
149 (if (zerop (minibuffer-depth))
152 (shrink-window (- (- (window-height) (count-lines m n
)) 1))))))
154 (defun mouse-window-to-region (event)
155 "Narrow window to region between cursor and mouse pointer."
157 (let ((point-save (point)))
159 (progn (mouse-set-point event
)
162 (goto-char point-save
)
163 (narrow-window-to-region (region-beginning) (region-end)))))
165 (defun mouse-ignore ()
169 ;; Commands for the scroll bar.
171 (defun mouse-scroll-down (nlines)
173 (scroll-down nlines
))
175 (defun mouse-scroll-up (nlines)
179 (defun mouse-scroll-down-full ()
183 (defun mouse-scroll-up-full ()
187 (defun mouse-scroll-move-cursor (nlines)
189 (move-to-window-line nlines
))
191 (defun mouse-scroll-absolute (event)
193 (let* ((pos (car event
))
195 (length (car (cdr pos
))))
196 (if (<= length
0) (setq length
1))
197 (let* ((scale-factor (max 1 (/ length
(/ 8000000 (buffer-size)))))
198 (newpos (* (/ (* (/ (buffer-size) scale-factor
)
205 (defun mouse-scroll-left (ncolumns)
207 (scroll-left ncolumns
))
209 (defun mouse-scroll-right (ncolumns)
211 (scroll-right ncolumns
))
213 (defun mouse-scroll-left-full ()
217 (defun mouse-scroll-right-full ()
221 (defun mouse-scroll-move-cursor-horizontally (ncolumns)
223 (move-to-column ncolumns
))
225 (defun mouse-scroll-absolute-horizontally (event)
227 (let* ((pos (car event
))
229 (length (car (cdr pos
))))
230 (set-window-hscroll (selected-window) 33)))
232 ;; Set up these commands, including the prefix keys for the scroll bar.
234 (fset 'mouse-vertical-scroll-bar-prefix
(make-sparse-keymap))
235 (define-key global-mouse-map mouse-vertical-scroll-bar-prefix
236 'mouse-vertical-scroll-bar-prefix
)
238 (defun mouse-scroll-motion (event)
240 (let ((pos (car (car event
)))
241 (length (car (cdr (car event
)))))
242 (message "[%d %d]" pos length
)))
244 (let ((map (function mouse-vertical-scroll-bar-prefix
)))
245 (define-key map mouse-button-right
'mouse-scroll-down
)
246 (define-key map mouse-button-left
'mouse-scroll-up
)
247 (define-key map mouse-button-middle
'mouse-scroll-absolute
)
248 (define-key map mouse-motion
'x-horizontal-line
))
250 ;(fset 'mouse-vertical-slider-prefix (make-sparse-keymap))
251 ;(define-key global-mouse-map mouse-vertical-slider-prefix
252 ; 'mouse-vertical-slider-prefix)
254 ;(let ((map (function mouse-vertical-slider-prefix)))
255 ; (define-key map mouse-button-right 'mouse-scroll-move-cursor)
256 ; (define-key map mouse-button-left 'mouse-scroll-move-cursor)
257 ; (define-key map mouse-button-middle 'mouse-scroll-move-cursor))
259 (fset 'mouse-vertical-thumbup-prefix
(make-sparse-keymap))
260 (define-key global-mouse-map mouse-vertical-thumbup-prefix
261 'mouse-vertical-thumbup-prefix
)
263 (let ((map (function mouse-vertical-thumbup-prefix
)))
264 (define-key map mouse-button-right
'mouse-scroll-down-full
)
265 (define-key map mouse-button-left
'mouse-scroll-down-full
)
266 (define-key map mouse-button-middle
'mouse-scroll-down-full
))
268 (fset 'mouse-vertical-thumbdown-prefix
(make-sparse-keymap))
269 (define-key global-mouse-map mouse-vertical-thumbdown-prefix
270 'mouse-vertical-thumbdown-prefix
)
272 (let ((map (function mouse-vertical-thumbdown-prefix
)))
273 (define-key map mouse-button-right
'mouse-scroll-up-full
)
274 (define-key map mouse-button-left
'mouse-scroll-up-full
)
275 (define-key map mouse-button-middle
'mouse-scroll-up-full
))
279 (fset 'mouse-horizontal-scroll-bar-prefix
(make-sparse-keymap))
280 (define-key global-mouse-map mouse-horizontal-scroll-bar-prefix
281 'mouse-horizontal-scroll-bar-prefix
)
283 (let ((map (function mouse-horizontal-scroll-bar-prefix
)))
284 (define-key map mouse-button-right
'mouse-scroll-right
)
285 (define-key map mouse-button-left
'mouse-scroll-left
)
286 (define-key map mouse-button-middle
'mouse-scroll-absolute-horizontally
))
288 (fset 'mouse-horizontal-thumbleft-prefix
(make-sparse-keymap))
289 (define-key global-mouse-map mouse-horizontal-thumbleft-prefix
290 'mouse-horizontal-thumbleft-prefix
)
292 (let ((map (function mouse-horizontal-thumbleft-prefix
)))
293 (define-key map mouse-button-right
'mouse-scroll-left-full
)
294 (define-key map mouse-button-left
'mouse-scroll-left-full
)
295 (define-key map mouse-button-middle
'mouse-scroll-left-full
))
297 (fset 'mouse-horizontal-thumbright-prefix
(make-sparse-keymap))
298 (define-key global-mouse-map mouse-horizontal-thumbright-prefix
299 'mouse-horizontal-thumbright-prefix
)
301 (let ((map (function mouse-horizontal-thumbright-prefix
)))
302 (define-key map mouse-button-right
'mouse-scroll-right-full
)
303 (define-key map mouse-button-left
'mouse-scroll-right-full
)
304 (define-key map mouse-button-middle
'mouse-scroll-right-full
))
308 ;;;; Here are experimental things being tested. Mouse events
309 ;;;; are of the form:
310 ;;;; ((x y) window screen-part key-sequence timestamp)
313 ;;;; Dynamically track mouse coordinates
316 ;;(defun track-mouse (event)
317 ;; "Track the coordinates, absolute and relative, of the mouse."
318 ;; (interactive "@e")
319 ;; (while mouse-grabbed
320 ;; (let* ((pos (read-mouse-position (selected-screen)))
323 ;; (relative-coordinate (coordinates-in-window-p
324 ;; (list (car pos) (cdr pos))
325 ;; (selected-window))))
326 ;; (if (consp relative-coordinate)
327 ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
328 ;; (car relative-coordinate)
329 ;; (car (cdr relative-coordinate)))
330 ;; (message "mouse: [%d %d]" abs-x abs-y)))))
333 ;; Dynamically put a box around the line indicated by point
338 (defun mouse-select-buffer-line (event)
340 (let ((relative-coordinate
341 (coordinates-in-window-p (car event
) (selected-window)))
342 (abs-y (car (cdr (car event
)))))
343 (if (consp relative-coordinate
)
346 (move-to-window-line (car (cdr relative-coordinate
)))
351 (move-to-window-line (car (cdr relative-coordinate
)))
355 (- (region-end) (region-beginning))) 1)
356 (setq the-buffer
(Buffer-menu-buffer t
)))
358 (x-erase-rectangle (selected-screen))))))
360 (defvar last-line-drawn nil
)
361 (defvar begin-delim
"[^ \t]")
362 (defvar end-delim
"[^ \t]")
364 (defun mouse-boxing (event)
367 (let ((screen (selected-screen)))
368 (while (= (x-mouse-events) 0)
369 (let* ((pos (read-mouse-position screen
))
373 (coordinates-in-window-p (` ((, abs-x
) (, abs-y
)))
379 (if (and (consp relative-coordinate
)
380 (or (not last-line-drawn
)
381 (not (= last-line-drawn abs-y
))))
383 (move-to-window-line (car (cdr relative-coordinate
)))
384 (if (= (following-char) 10)
387 (setq begin-reg
(1- (re-search-forward end-delim
)))
388 (setq begin-column
(1- (current-column)))
390 (setq end-reg
(1+ (re-search-backward begin-delim
)))
391 (setq end-column
(1+ (current-column)))
392 (message "%s" (buffer-substring begin-reg end-reg
))
393 (x-draw-rectangle screen
394 (setq last-line-drawn abs-y
)
396 (- end-column begin-column
) 1))))))))))
398 (defun mouse-erase-box ()
402 (x-erase-rectangle (selected-screen))
403 (setq last-line-drawn nil
))))
405 (defun test-x-rectangle ()
406 (use-local-mouse-map (setq rectangle-test-map
(make-sparse-keymap)))
407 (define-key rectangle-test-map mouse-motion-button-left
'mouse-boxing
)
408 (define-key rectangle-test-map mouse-button-left-up
'mouse-erase-box
))
411 ;; Here is how to do double clicking in lisp. About to change.
414 (defvar double-start nil
)
415 (defconst double-click-interval
300
416 "Max ticks between clicks")
418 (defun double-down (event)
421 (let ((interval (- (nth 4 event
) double-start
)))
422 (if (< interval double-click-interval
)
425 ;; (message "Interval %d" interval)
427 (setq double-start nil
))
428 (setq double-start
(nth 4 event
))))
430 (defun double-up (event)
433 (> (- (nth 4 event
) double-start
) double-click-interval
)
434 (setq double-start nil
)))
436 (defun x-test-doubleclick ()
437 (use-local-mouse-map (setq doubleclick-test-map
(make-sparse-keymap)))
438 (define-key doubleclick-test-map mouse-button-left
'double-down
)
439 (define-key doubleclick-test-map mouse-button-left-up
'double-up
))
442 ;; This scrolls while button is depressed. Use preferable in scrollbar.
445 (defvar scrolled-lines
0)
446 (defconst scroll-speed
1)
448 (defun incr-scroll-down (event)
450 (setq scrolled-lines
0)
451 (incremental-scroll scroll-speed
))
453 (defun incr-scroll-up (event)
455 (setq scrolled-lines
0)
456 (incremental-scroll (- scroll-speed
)))
458 (defun incremental-scroll (n)
459 (while (= (x-mouse-events) 0)
460 (setq scrolled-lines
(1+ (* scroll-speed scrolled-lines
)))
464 (defun incr-scroll-stop (event)
466 (message "Scrolled %d lines" scrolled-lines
)
467 (setq scrolled-lines
0)
470 (defun x-testing-scroll ()
471 (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix
)))
472 (define-key scrolling-map mouse-button-left
'incr-scroll-down
)
473 (define-key scrolling-map mouse-button-right
'incr-scroll-up
)
474 (define-key scrolling-map mouse-button-left-up
'incr-scroll-stop
)
475 (define-key scrolling-map mouse-button-right-up
'incr-scroll-stop
)))
478 ;; Some playthings suitable for picture mode? They need work.
481 (defun mouse-kill-rectangle (event)
482 "Kill the rectangle between point and the mouse cursor."
484 (let ((point-save (point)))
486 (mouse-set-point event
)
488 (if (> point-save
(point))
489 (kill-rectangle (point) point-save
)
490 (kill-rectangle point-save
(point))))))
492 (defun mouse-open-rectangle (event)
493 "Kill the rectangle between point and the mouse cursor."
495 (let ((point-save (point)))
497 (mouse-set-point event
)
499 (if (> point-save
(point))
500 (open-rectangle (point) point-save
)
501 (open-rectangle point-save
(point))))))
503 ;; Must be a better way to do this.
505 (defun mouse-multiple-insert (n char
)
510 ;; What this could do is not finalize until button was released.
512 (defun mouse-move-text (event)
513 "Move text from point to cursor position, inserting spaces."
515 (let* ((relative-coordinate
516 (coordinates-in-window-p (car event
) (selected-window))))
517 (if (consp relative-coordinate
)
518 (cond ((> (current-column) (car relative-coordinate
))
520 (- (car relative-coordinate
) (current-column))))
521 ((< (current-column) (car relative-coordinate
))
522 (mouse-multiple-insert
523 (- (car relative-coordinate
) (current-column)) " "))
524 ((= (current-column) (car relative-coordinate
)) (ding))))))