]> code.delx.au - gnu-emacs/blob - lisp/mouse.el
*** empty log message ***
[gnu-emacs] / lisp / mouse.el
1 ;;; mouse.el --- window system-independent mouse support.
2
3 ;;; Copyright (C) 1988, 1992 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: hardware
7
8 ;;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
23
24 \f
25 ;;; Utility functions.
26
27 (defun mouse-movement-p (event)
28 (and (consp event)
29 (eq (car event) 'mouse-movement)))
30
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))
35
36 ;;; Indent track-mouse like progn.
37 (put 'track-mouse 'lisp-indent-function 0)
38
39 \f
40 (defun mouse-delete-window (click)
41 "Delete the window clicked on.
42 This must be bound to a mouse click."
43 (interactive "K")
44 (delete-window (event-window click)))
45
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."
49 (interactive "K")
50 (select-window (event-window click))
51 (delete-other-windows))
52
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."
57 (interactive "K")
58 (select-window (event-window click))
59 (split-window-vertically (1+ (cdr (mouse-coords click)))))
60
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."
64 (interactive "K")
65 (select-window (event-window click))
66 (goto-char (event-point click)))
67
68 (defun mouse-set-mark (click)
69 "Set mark at the position clicked on with the mouse.
70 Display cursor at that position for a second.
71 This must be bound to a mouse click."
72 (interactive "K")
73 (let ((point-save (point)))
74 (unwind-protect
75 (progn (mouse-set-point click)
76 (push-mark nil t)
77 (sit-for 1))
78 (goto-char point-save))))
79
80 (defun mouse-kill (click)
81 "Kill the region between point and the mouse click.
82 The text is saved in the kill ring, as with \\[kill-region]."
83 (interactive "K")
84 (let ((click-posn (event-point click)))
85 (kill-region (min (point) click-posn)
86 (max (point) click-posn))))
87
88 (defun mouse-yank-at-click (click arg)
89 "Insert the last stretch of killed text at the position clicked on.
90 Prefix arguments are interpreted as with \\[yank]."
91 (interactive "K\nP")
92 (mouse-set-point click)
93 (yank arg))
94
95 (defun mouse-kill-ring-save (click)
96 "Copy the region between point and the mouse click in the kill ring.
97 This does not delete the region; it acts like \\[kill-ring-save]."
98 (interactive "K")
99 (mouse-set-mark click)
100 (call-interactively 'kill-ring-save))
101
102
103 \f
104 ;; Commands for the scroll bar.
105
106 (defun mouse-scroll-down (nlines)
107 (interactive "@p")
108 (scroll-down nlines))
109
110 (defun mouse-scroll-up (nlines)
111 (interactive "@p")
112 (scroll-up nlines))
113
114 (defun mouse-scroll-down-full ()
115 (interactive "@")
116 (scroll-down nil))
117
118 (defun mouse-scroll-up-full ()
119 (interactive "@")
120 (scroll-up nil))
121
122 (defun mouse-scroll-move-cursor (nlines)
123 (interactive "@p")
124 (move-to-window-line nlines))
125
126 (defun mouse-scroll-absolute (event)
127 (interactive "@e")
128 (let* ((pos (car event))
129 (position (car pos))
130 (length (car (cdr pos))))
131 (if (<= length 0) (setq length 1))
132 (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
133 (newpos (* (/ (* (/ (buffer-size) scale-factor)
134 position)
135 length)
136 scale-factor)))
137 (goto-char newpos)
138 (recenter '(4)))))
139
140 (defun mouse-scroll-left (ncolumns)
141 (interactive "@p")
142 (scroll-left ncolumns))
143
144 (defun mouse-scroll-right (ncolumns)
145 (interactive "@p")
146 (scroll-right ncolumns))
147
148 (defun mouse-scroll-left-full ()
149 (interactive "@")
150 (scroll-left nil))
151
152 (defun mouse-scroll-right-full ()
153 (interactive "@")
154 (scroll-right nil))
155
156 (defun mouse-scroll-move-cursor-horizontally (ncolumns)
157 (interactive "@p")
158 (move-to-column ncolumns))
159
160 (defun mouse-scroll-absolute-horizontally (event)
161 (interactive "@e")
162 (let* ((pos (car event))
163 (position (car pos))
164 (length (car (cdr pos))))
165 (set-window-hscroll (selected-window) 33)))
166
167 ;; Set up these commands, including the prefix keys for the scroll bar.
168
169 ;;; (fset 'mouse-vertical-scroll-bar-prefix (make-sparse-keymap))
170 ;;; (define-key global-mouse-map mouse-vertical-scroll-bar-prefix
171 ;;; 'mouse-vertical-scroll-bar-prefix)
172 ;;;
173 ;;; (defun mouse-scroll-motion (event)
174 ;;; (interactive "e")
175 ;;; (let ((pos (car (car event)))
176 ;;; (length (car (cdr (car event)))))
177 ;;; (message "[%d %d]" pos length)))
178 ;;;
179 ;;; (let ((map (function mouse-vertical-scroll-bar-prefix)))
180 ;;; (define-key map mouse-button-right 'mouse-scroll-down)
181 ;;; (define-key map mouse-button-left 'mouse-scroll-up)
182 ;;; (define-key map mouse-button-middle 'mouse-scroll-absolute)
183 ;;; (define-key map mouse-motion 'x-horizontal-line))
184 ;;;
185 ;;; ;(fset 'mouse-vertical-slider-prefix (make-sparse-keymap))
186 ;;; ;(define-key global-mouse-map mouse-vertical-slider-prefix
187 ;;; ; 'mouse-vertical-slider-prefix)
188 ;;;
189 ;;; ;(let ((map (function mouse-vertical-slider-prefix)))
190 ;;; ; (define-key map mouse-button-right 'mouse-scroll-move-cursor)
191 ;;; ; (define-key map mouse-button-left 'mouse-scroll-move-cursor)
192 ;;; ; (define-key map mouse-button-middle 'mouse-scroll-move-cursor))
193 ;;;
194 ;;; (fset 'mouse-vertical-thumbup-prefix (make-sparse-keymap))
195 ;;; (define-key global-mouse-map mouse-vertical-thumbup-prefix
196 ;;; 'mouse-vertical-thumbup-prefix)
197 ;;;
198 ;;; (let ((map (function mouse-vertical-thumbup-prefix)))
199 ;;; (define-key map mouse-button-right 'mouse-scroll-down-full)
200 ;;; (define-key map mouse-button-left 'mouse-scroll-down-full)
201 ;;; (define-key map mouse-button-middle 'mouse-scroll-down-full))
202 ;;;
203 ;;; (fset 'mouse-vertical-thumbdown-prefix (make-sparse-keymap))
204 ;;; (define-key global-mouse-map mouse-vertical-thumbdown-prefix
205 ;;; 'mouse-vertical-thumbdown-prefix)
206 ;;;
207 ;;; (let ((map (function mouse-vertical-thumbdown-prefix)))
208 ;;; (define-key map mouse-button-right 'mouse-scroll-up-full)
209 ;;; (define-key map mouse-button-left 'mouse-scroll-up-full)
210 ;;; (define-key map mouse-button-middle 'mouse-scroll-up-full))
211 ;;;
212 ;;; ;; Horizontal bar
213 ;;;
214 ;;; (fset 'mouse-horizontal-scroll-bar-prefix (make-sparse-keymap))
215 ;;; (define-key global-mouse-map mouse-horizontal-scroll-bar-prefix
216 ;;; 'mouse-horizontal-scroll-bar-prefix)
217 ;;;
218 ;;; (let ((map (function mouse-horizontal-scroll-bar-prefix)))
219 ;;; (define-key map mouse-button-right 'mouse-scroll-right)
220 ;;; (define-key map mouse-button-left 'mouse-scroll-left)
221 ;;; (define-key map mouse-button-middle 'mouse-scroll-absolute-horizontally))
222 ;;;
223 ;;; (fset 'mouse-horizontal-thumbleft-prefix (make-sparse-keymap))
224 ;;; (define-key global-mouse-map mouse-horizontal-thumbleft-prefix
225 ;;; 'mouse-horizontal-thumbleft-prefix)
226 ;;;
227 ;;; (let ((map (function mouse-horizontal-thumbleft-prefix)))
228 ;;; (define-key map mouse-button-right 'mouse-scroll-left-full)
229 ;;; (define-key map mouse-button-left 'mouse-scroll-left-full)
230 ;;; (define-key map mouse-button-middle 'mouse-scroll-left-full))
231 ;;;
232 ;;; (fset 'mouse-horizontal-thumbright-prefix (make-sparse-keymap))
233 ;;; (define-key global-mouse-map mouse-horizontal-thumbright-prefix
234 ;;; 'mouse-horizontal-thumbright-prefix)
235 ;;;
236 ;;; (let ((map (function mouse-horizontal-thumbright-prefix)))
237 ;;; (define-key map mouse-button-right 'mouse-scroll-right-full)
238 ;;; (define-key map mouse-button-left 'mouse-scroll-right-full)
239 ;;; (define-key map mouse-button-middle 'mouse-scroll-right-full))
240
241
242 ;;;;
243 ;;;; Here are experimental things being tested. Mouse events
244 ;;;; are of the form:
245 ;;;; ((x y) window screen-part key-sequence timestamp)
246 ;;
247 ;;;;
248 ;;;; Dynamically track mouse coordinates
249 ;;;;
250 ;;
251 ;;(defun track-mouse (event)
252 ;; "Track the coordinates, absolute and relative, of the mouse."
253 ;; (interactive "@e")
254 ;; (while mouse-grabbed
255 ;; (let* ((pos (read-mouse-position (selected-screen)))
256 ;; (abs-x (car pos))
257 ;; (abs-y (cdr pos))
258 ;; (relative-coordinate (coordinates-in-window-p
259 ;; (list (car pos) (cdr pos))
260 ;; (selected-window))))
261 ;; (if (consp relative-coordinate)
262 ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
263 ;; (car relative-coordinate)
264 ;; (car (cdr relative-coordinate)))
265 ;; (message "mouse: [%d %d]" abs-x abs-y)))))
266
267 ;;
268 ;; Dynamically put a box around the line indicated by point
269 ;;
270 ;;
271 ;;(require 'backquote)
272 ;;
273 ;;(defun mouse-select-buffer-line (event)
274 ;; (interactive "@e")
275 ;; (let ((relative-coordinate
276 ;; (coordinates-in-window-p (car event) (selected-window)))
277 ;; (abs-y (car (cdr (car event)))))
278 ;; (if (consp relative-coordinate)
279 ;; (progn
280 ;; (save-excursion
281 ;; (move-to-window-line (car (cdr relative-coordinate)))
282 ;; (x-draw-rectangle
283 ;; (selected-screen)
284 ;; abs-y 0
285 ;; (save-excursion
286 ;; (move-to-window-line (car (cdr relative-coordinate)))
287 ;; (end-of-line)
288 ;; (push-mark nil t)
289 ;; (beginning-of-line)
290 ;; (- (region-end) (region-beginning))) 1))
291 ;; (sit-for 1)
292 ;; (x-erase-rectangle (selected-screen))))))
293 ;;
294 ;;(defvar last-line-drawn nil)
295 ;;(defvar begin-delim "[^ \t]")
296 ;;(defvar end-delim "[^ \t]")
297 ;;
298 ;;(defun mouse-boxing (event)
299 ;; (interactive "@e")
300 ;; (save-excursion
301 ;; (let ((screen (selected-screen)))
302 ;; (while (= (x-mouse-events) 0)
303 ;; (let* ((pos (read-mouse-position screen))
304 ;; (abs-x (car pos))
305 ;; (abs-y (cdr pos))
306 ;; (relative-coordinate
307 ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
308 ;; (selected-window)))
309 ;; (begin-reg nil)
310 ;; (end-reg nil)
311 ;; (end-column nil)
312 ;; (begin-column nil))
313 ;; (if (and (consp relative-coordinate)
314 ;; (or (not last-line-drawn)
315 ;; (not (= last-line-drawn abs-y))))
316 ;; (progn
317 ;; (move-to-window-line (car (cdr relative-coordinate)))
318 ;; (if (= (following-char) 10)
319 ;; ()
320 ;; (progn
321 ;; (setq begin-reg (1- (re-search-forward end-delim)))
322 ;; (setq begin-column (1- (current-column)))
323 ;; (end-of-line)
324 ;; (setq end-reg (1+ (re-search-backward begin-delim)))
325 ;; (setq end-column (1+ (current-column)))
326 ;; (message "%s" (buffer-substring begin-reg end-reg))
327 ;; (x-draw-rectangle screen
328 ;; (setq last-line-drawn abs-y)
329 ;; begin-column
330 ;; (- end-column begin-column) 1))))))))))
331 ;;
332 ;;(defun mouse-erase-box ()
333 ;; (interactive)
334 ;; (if last-line-drawn
335 ;; (progn
336 ;; (x-erase-rectangle (selected-screen))
337 ;; (setq last-line-drawn nil))))
338
339 ;;; (defun test-x-rectangle ()
340 ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
341 ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
342 ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
343
344 ;;
345 ;; Here is how to do double clicking in lisp. About to change.
346 ;;
347
348 (defvar double-start nil)
349 (defconst double-click-interval 300
350 "Max ticks between clicks")
351
352 (defun double-down (event)
353 (interactive "@e")
354 (if double-start
355 (let ((interval (- (nth 4 event) double-start)))
356 (if (< interval double-click-interval)
357 (progn
358 (backward-up-list 1)
359 ;; (message "Interval %d" interval)
360 (sleep-for 1)))
361 (setq double-start nil))
362 (setq double-start (nth 4 event))))
363
364 (defun double-up (event)
365 (interactive "@e")
366 (and double-start
367 (> (- (nth 4 event ) double-start) double-click-interval)
368 (setq double-start nil)))
369
370 ;;; (defun x-test-doubleclick ()
371 ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
372 ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
373 ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
374
375 ;;
376 ;; This scrolls while button is depressed. Use preferable in scrollbar.
377 ;;
378
379 (defvar scrolled-lines 0)
380 (defconst scroll-speed 1)
381
382 (defun incr-scroll-down (event)
383 (interactive "@e")
384 (setq scrolled-lines 0)
385 (incremental-scroll scroll-speed))
386
387 (defun incr-scroll-up (event)
388 (interactive "@e")
389 (setq scrolled-lines 0)
390 (incremental-scroll (- scroll-speed)))
391
392 (defun incremental-scroll (n)
393 (while (= (x-mouse-events) 0)
394 (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
395 (scroll-down n)
396 (sit-for 300 t)))
397
398 (defun incr-scroll-stop (event)
399 (interactive "@e")
400 (message "Scrolled %d lines" scrolled-lines)
401 (setq scrolled-lines 0)
402 (sleep-for 1))
403
404 ;;; (defun x-testing-scroll ()
405 ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
406 ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
407 ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
408 ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
409 ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
410
411 ;;
412 ;; Some playthings suitable for picture mode? They need work.
413 ;;
414
415 (defun mouse-kill-rectangle (event)
416 "Kill the rectangle between point and the mouse cursor."
417 (interactive "@e")
418 (let ((point-save (point)))
419 (save-excursion
420 (mouse-set-point event)
421 (push-mark nil t)
422 (if (> point-save (point))
423 (kill-rectangle (point) point-save)
424 (kill-rectangle point-save (point))))))
425
426 (defun mouse-open-rectangle (event)
427 "Kill the rectangle between point and the mouse cursor."
428 (interactive "@e")
429 (let ((point-save (point)))
430 (save-excursion
431 (mouse-set-point event)
432 (push-mark nil t)
433 (if (> point-save (point))
434 (open-rectangle (point) point-save)
435 (open-rectangle point-save (point))))))
436
437 ;; Must be a better way to do this.
438
439 (defun mouse-multiple-insert (n char)
440 (while (> n 0)
441 (insert char)
442 (setq n (1- n))))
443
444 ;; What this could do is not finalize until button was released.
445
446 (defun mouse-move-text (event)
447 "Move text from point to cursor position, inserting spaces."
448 (interactive "@e")
449 (let* ((relative-coordinate
450 (coordinates-in-window-p (car event) (selected-window))))
451 (if (consp relative-coordinate)
452 (cond ((> (current-column) (car relative-coordinate))
453 (delete-char
454 (- (car relative-coordinate) (current-column))))
455 ((< (current-column) (car relative-coordinate))
456 (mouse-multiple-insert
457 (- (car relative-coordinate) (current-column)) " "))
458 ((= (current-column) (car relative-coordinate)) (ding))))))
459
460 \f
461 ;;; Bindings for mouse commands.
462
463 (global-set-key [mouse-1] 'mouse-set-point)
464 (global-set-key [mouse-2] 'mouse-yank-at-click)
465 (global-set-key [mouse-3] 'mouse-kill-ring-save)
466
467 (global-set-key [S-mouse-1] 'mouse-set-mark)
468
469 (provide 'mouse)
470
471 ;;; mouse.el ends here