]> code.delx.au - gnu-emacs/blob - lisp/mouse.el
* mouse.el (mouse-drag-region): Correctly handle drags which enter
[gnu-emacs] / lisp / mouse.el
1 ;;; mouse.el --- window system-independent mouse support.
2
3 ;;; Copyright (C) 1993 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 ;;; Commentary:
25
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.
29 ;;
30 ;; The code is rather X-dependent.
31
32 ;;; Code:
33
34 ;;; Utility functions.
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 you click on.
42 This must be bound to a mouse click."
43 (interactive "e")
44 (delete-window (posn-window (event-start click))))
45
46 (defun mouse-tear-off-window (click)
47 "Delete the window clicked on, and create a new frame displaying its buffer."
48 (interactive "e")
49 (let* ((window (posn-window (event-start click)))
50 (buf (window-buffer window))
51 (frame (new-frame)))
52 (select-frame frame)
53 (switch-to-buffer buf)
54 (delete-window window)))
55
56 (defun mouse-delete-other-windows ()
57 "Delete all window except the one you click on."
58 (interactive "@")
59 (delete-other-windows))
60
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."
65 (interactive "@e")
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)
70 (1- (window-height)))
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))))))
78
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."
83 (interactive "@e")
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))))))
93
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."
97 (interactive "e")
98 (let ((posn (event-start click)))
99 (select-window (posn-window posn))
100 (if (numberp (posn-point posn))
101 (goto-char (posn-point posn)))))
102
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."
106 (interactive "e")
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))
115 (sit-for 1))
116 (push-mark)
117 (set-mark (point))
118 (if (numberp (posn-point end))
119 (goto-char (posn-point end)))))
120
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.")
129
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."
135 (while (progn
136 (goto-char (window-start))
137 (if (not (zerop (vertical-motion jump)))
138 (progn
139 (set-window-start (selected-window) (point))
140 (if (natnump jump)
141 (progn
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))))
147 (if overlay
148 (move-overlay overlay start (point)))
149 (if (not (eobp))
150 (sit-for mouse-scroll-delay))))))
151 (point))
152
153 (defvar mouse-drag-overlay (make-overlay 1 1))
154 (overlay-put mouse-drag-overlay 'face 'region)
155
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."
160 (interactive "e")
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))
166 (top (nth 1 bounds))
167 (bottom (if (window-minibuffer-p start-window)
168 (nth 3 bounds)
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))
176 (setq mark-active nil)
177 (let (event end end-point)
178 (track-mouse
179 (while (progn
180 (setq event (read-event))
181 (or (mouse-movement-p event)
182 (eq (car-safe event) 'switch-frame)))
183
184 (if (eq (car-safe event) 'switch-frame)
185 nil
186 (setq end (event-end event)
187 end-point (posn-point end))
188
189 (cond
190
191 ;; Ignore switch-frame events.
192 ((eq (car-safe event) 'switch-frame))
193
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)))
200
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))
204 (let ((mouse-row
205 (+ (nth 1 (window-edges (posn-window end)))
206 (cdr (posn-col-row end)))))
207 (cond
208 ((< mouse-row top)
209 (mouse-scroll-subr
210 (- mouse-row top) mouse-drag-overlay start-point))
211 ((and (not (eobp))
212 (>= mouse-row bottom))
213 (mouse-scroll-subr (1+ (- mouse-row bottom))
214 mouse-drag-overlay start-point)))))
215
216 ;; Otherwise, we have no idea where the mouse is.
217 (t)))))
218
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)
224 (setq mark-active nil)
225 (set-mark start-point))
226 (delete-overlay mouse-drag-overlay))))
227
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)))
240 ;;;!
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")
246 ;;;! (let (newmark)
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))
255 ;;;! (track-mouse
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.
267 ;;;! (if newmark
268 ;;;! (push-mark newmark t t)
269 ;;;! ;; Turn off the old mark when we set up an empty region.
270 ;;;! (setq deactivate-mark t))))
271 \f
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))))
279
280 ;; Momentarily show where the mark is, if highlighting doesn't show it.
281 (defun mouse-show-mark ()
282 (or transient-mark-mode
283 (save-excursion
284 (goto-char (mark t))
285 (sit-for 1))))
286
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."
291 (interactive "e")
292 (let ((point-save (point)))
293 (unwind-protect
294 (progn (mouse-set-point click)
295 (push-mark nil t t)
296 (or transient-mark-mode
297 (sit-for 1)))
298 (goto-char point-save))))
299
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]."
303 (interactive "e")
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)))))
308
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]."
312 (interactive "e\nP")
313 (mouse-set-point click)
314 (yank arg))
315
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]."
319 (interactive "e")
320 (mouse-set-mark-fast click)
321 (kill-ring-save (point) (mark t))
322 (mouse-show-mark))
323
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.
327
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)
331
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."
338 (interactive "e")
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.
349 (progn
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))
359 (mouse-show-mark)
360 (setq mouse-save-then-kill-posn
361 (list (car kill-ring) (point) click-posn)))))
362 \f
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)
368
369 ;; An overlay which records the current secondary selection
370 ;; or else is deleted when there is no secondary selection.
371 ;; May be nil.
372 (defvar mouse-secondary-overlay nil)
373
374 ;; A marker which records the specified first end for a secondary selection.
375 ;; May be nil.
376 (defvar mouse-secondary-start nil)
377
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."
382 (interactive "e")
383 (let ((posn (event-start click)))
384 (save-excursion
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))
390 (progn
391 (or mouse-secondary-start
392 (setq mouse-secondary-start (make-marker)))
393 (move-marker mouse-secondary-start (posn-point posn)))))))
394
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."
398 (interactive "e")
399 (let ((posn (event-start click))
400 beg
401 (end (event-end click)))
402 (save-excursion
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))))
410
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."
414 (interactive "e")
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.
419 (save-excursion
420 (if (numberp (posn-point posn))
421 (goto-char (posn-point posn)))
422 (setq unread-command-events
423 (cons (read-event) unread-command-events))))))
424
425 (defun mouse-kill-secondary ()
426 "Kill the text in the secondary selection."
427 (interactive "*")
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))
432
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."
439 (interactive "e")
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.
452 (progn
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) start)
460 buffer-undo-list))))
461 ;; Otherwise, save this region.
462 (save-excursion
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))))))
471 \f
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."
476 (interactive "e")
477 (let ((menu
478 (list "Buffer Menu"
479 (cons "Select Buffer"
480 (let ((tail (buffer-list))
481 (maxbuf 0)
482 head)
483 (while tail
484 (or (eq ?\ (aref (buffer-name (car tail)) 0))
485 (setq maxbuf
486 (max maxbuf
487 (length (buffer-name (car tail))))))
488 (setq tail (cdr tail)))
489 (setq tail (buffer-list))
490 (while tail
491 (let ((elt (car tail)))
492 (if (not (string-match "^ "
493 (buffer-name elt)))
494 (setq head (cons
495 (cons
496 (format
497 (format "%%%ds %%s%%s %%s"
498 maxbuf)
499 (buffer-name elt)
500 (if (buffer-modified-p elt)
501 "*" " ")
502 (save-excursion
503 (set-buffer elt)
504 (if buffer-read-only "%" " "))
505 (or (buffer-file-name elt) ""))
506 elt)
507 head))))
508 (setq tail (cdr tail)))
509 (reverse head))))))
510 (let ((buf (x-popup-menu event menu))
511 (window (posn-window (event-start event))))
512 (if buf
513 (progn
514 (or (framep window) (select-window window))
515 (switch-to-buffer buf))))))
516 \f
517 ;;; These need to be rewritten for the new scroll bar implementation.
518
519 ;;;!! ;; Commands for the scroll bar.
520 ;;;!!
521 ;;;!! (defun mouse-scroll-down (click)
522 ;;;!! (interactive "@e")
523 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
524 ;;;!!
525 ;;;!! (defun mouse-scroll-up (click)
526 ;;;!! (interactive "@e")
527 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
528 ;;;!!
529 ;;;!! (defun mouse-scroll-down-full ()
530 ;;;!! (interactive "@")
531 ;;;!! (scroll-down nil))
532 ;;;!!
533 ;;;!! (defun mouse-scroll-up-full ()
534 ;;;!! (interactive "@")
535 ;;;!! (scroll-up nil))
536 ;;;!!
537 ;;;!! (defun mouse-scroll-move-cursor (click)
538 ;;;!! (interactive "@e")
539 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
540 ;;;!!
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)
549 ;;;!! position)
550 ;;;!! length)
551 ;;;!! scale-factor)))
552 ;;;!! (goto-char newpos)
553 ;;;!! (recenter '(4)))))
554 ;;;!!
555 ;;;!! (defun mouse-scroll-left (click)
556 ;;;!! (interactive "@e")
557 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
558 ;;;!!
559 ;;;!! (defun mouse-scroll-right (click)
560 ;;;!! (interactive "@e")
561 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
562 ;;;!!
563 ;;;!! (defun mouse-scroll-left-full ()
564 ;;;!! (interactive "@")
565 ;;;!! (scroll-left nil))
566 ;;;!!
567 ;;;!! (defun mouse-scroll-right-full ()
568 ;;;!! (interactive "@")
569 ;;;!! (scroll-right nil))
570 ;;;!!
571 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
572 ;;;!! (interactive "@e")
573 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
574 ;;;!!
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)))
581 ;;;!!
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)
585 ;;;!!
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)
589 ;;;!!
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)
593 ;;;!!
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)
597 ;;;!!
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)
602 ;;;!!
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)
609 ;;;!!
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)
613 ;;;!!
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)
617 ;;;!!
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)
624 \f
625 ;;;!! ;;;;
626 ;;;!! ;;;; Here are experimental things being tested. Mouse events
627 ;;;!! ;;;; are of the form:
628 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
629 ;;;!! ;;
630 ;;;!! ;;;;
631 ;;;!! ;;;; Dynamically track mouse coordinates
632 ;;;!! ;;;;
633 ;;;!! ;;
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)))))
649 ;;;!!
650 ;;;!! ;;
651 ;;;!! ;; Dynamically put a box around the line indicated by point
652 ;;;!! ;;
653 ;;;!! ;;
654 ;;;!! ;;(require 'backquote)
655 ;;;!! ;;
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)
662 ;;;!! ;; (progn
663 ;;;!! ;; (save-excursion
664 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
665 ;;;!! ;; (x-draw-rectangle
666 ;;;!! ;; (selected-screen)
667 ;;;!! ;; abs-y 0
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))
674 ;;;!! ;; (sit-for 1)
675 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
676 ;;;!! ;;
677 ;;;!! ;;(defvar last-line-drawn nil)
678 ;;;!! ;;(defvar begin-delim "[^ \t]")
679 ;;;!! ;;(defvar end-delim "[^ \t]")
680 ;;;!! ;;
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))))
699 ;;;!! ;; (progn
700 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
701 ;;;!! ;; (if (= (following-char) 10)
702 ;;;!! ;; ()
703 ;;;!! ;; (progn
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))))))))))
714 ;;;!! ;;
715 ;;;!! ;;(defun mouse-erase-box ()
716 ;;;!! ;; (interactive)
717 ;;;!! ;; (if last-line-drawn
718 ;;;!! ;; (progn
719 ;;;!! ;; (x-erase-rectangle (selected-screen))
720 ;;;!! ;; (setq last-line-drawn nil))))
721 ;;;!!
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))
726 ;;;!!
727 ;;;!! ;;
728 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
729 ;;;!! ;;
730 ;;;!!
731 ;;;!! (defvar double-start nil)
732 ;;;!! (defconst double-click-interval 300
733 ;;;!! "Max ticks between clicks")
734 ;;;!!
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)
740 ;;;!! (progn
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))))
746 ;;;!!
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)))
752 ;;;!!
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))
757 ;;;!!
758 ;;;!! ;;
759 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
760 ;;;!! ;;
761 ;;;!!
762 ;;;!! (defvar scrolled-lines 0)
763 ;;;!! (defconst scroll-speed 1)
764 ;;;!!
765 ;;;!! (defun incr-scroll-down (event)
766 ;;;!! (interactive "@e")
767 ;;;!! (setq scrolled-lines 0)
768 ;;;!! (incremental-scroll scroll-speed))
769 ;;;!!
770 ;;;!! (defun incr-scroll-up (event)
771 ;;;!! (interactive "@e")
772 ;;;!! (setq scrolled-lines 0)
773 ;;;!! (incremental-scroll (- scroll-speed)))
774 ;;;!!
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)))
780 ;;;!!
781 ;;;!! (defun incr-scroll-stop (event)
782 ;;;!! (interactive "@e")
783 ;;;!! (message "Scrolled %d lines" scrolled-lines)
784 ;;;!! (setq scrolled-lines 0)
785 ;;;!! (sleep-for 1))
786 ;;;!!
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)))
793 ;;;!!
794 ;;;!! ;;
795 ;;;!! ;; Some playthings suitable for picture mode? They need work.
796 ;;;!! ;;
797 ;;;!!
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))))))
808 ;;;!!
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))))))
819 ;;;!!
820 ;;;!! ;; Must be a better way to do this.
821 ;;;!!
822 ;;;!! (defun mouse-multiple-insert (n char)
823 ;;;!! (while (> n 0)
824 ;;;!! (insert char)
825 ;;;!! (setq n (1- n))))
826 ;;;!!
827 ;;;!! ;; What this could do is not finalize until button was released.
828 ;;;!!
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))
836 ;;;!! (delete-char
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))))))
842 \f
843 ;; Font selection.
844
845 (defvar x-fixed-font-alist
846 '("Font menu"
847 ("Misc"
848 ("fixed" "fixed")
849 ("6x10" "6x10")
850 ("6x12" "6x12")
851 ("6x13" "6x13")
852 ("7x13" "7x13")
853 ("7x14" "7x14")
854 ("8x13" "8x13")
855 ("8x13 bold" "8x13bold")
856 ("8x16" "8x16")
857 ("9x15" "9x15")
858 ("9x15 bold" "9x15bold")
859 ("10x20" "10x20")
860 ("11x18" "11x18")
861 ("12x24" "12x24"))
862 ;;; We don't seem to have these; who knows what they are.
863 ;;; ("fg-18" "fg-18")
864 ;;; ("fg-25" "fg-25")
865 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
866 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
867 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
868 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
869 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
870 ("Courier"
871 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
872 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
873 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
874 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
875 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
876 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
877 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
878 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
879 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
880 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
881 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
882 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
883 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
884 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
885 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
886 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
887 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
888 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
889 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
890 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
891 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
892 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
893 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
894 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
895 )
896 "X fonts suitable for use in Emacs.")
897
898 (defun mouse-set-font (&optional font)
899 "Select an emacs font from a list of known good fonts"
900 (interactive
901 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
902 (if font
903 (modify-frame-parameters (selected-frame)
904 (list (cons 'font font)))))
905 \f
906 ;;; Bindings for mouse commands.
907
908 (define-key global-map [down-mouse-1] 'mouse-drag-region)
909 (global-set-key [mouse-1] 'mouse-set-point)
910 (global-set-key [drag-mouse-1] 'mouse-set-region)
911
912 (global-set-key [mouse-2] 'mouse-yank-at-click)
913 (global-set-key [mouse-3] 'mouse-save-then-kill)
914
915 ;; By binding these to down-going events, we let the user use the up-going
916 ;; event to make the selection, saving a click.
917 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
918 (global-set-key [C-down-mouse-3] 'mouse-set-font)
919
920 ;; Replaced with dragging mouse-1
921 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
922
923 (global-set-key [mode-line mouse-1] 'mouse-delete-other-windows)
924 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
925 (global-set-key [mode-line S-mouse-2] 'mouse-split-window-horizontally)
926 \f
927 ;; Define the mouse help menu tree.
928
929 (defvar help-menu-map '(keymap "Help"))
930 (global-set-key [C-down-mouse-2] help-menu-map)
931
932 (defvar help-apropos-map (make-sparse-keymap "Is there a command that..."))
933 (defvar help-keys-map (make-sparse-keymap "Key Commands <==> Functions"))
934 (defvar help-manual-map (make-sparse-keymap "Manual and tutorial"))
935 (defvar help-misc-map (make-sparse-keymap "Odds and ends"))
936 (defvar help-modes-map (make-sparse-keymap "Modes"))
937 (defvar help-admin-map (make-sparse-keymap "Administrivia"))
938
939 (define-key help-menu-map [apropos]
940 (cons "@Is there a command that..." help-apropos-map))
941 (define-key help-menu-map [keys]
942 (cons "@Key Commands <==> Functions" help-keys-map))
943 (define-key help-menu-map [manuals]
944 (cons "@Manual and tutorial" help-manual-map))
945 (define-key help-menu-map [misc]
946 (cons "@Odds and ends" help-misc-map))
947 (define-key help-menu-map [modes]
948 (cons "@Modes" help-modes-map))
949 (define-key help-menu-map [admin]
950 (cons "@Administrivia" help-admin-map))
951
952 (define-key help-apropos-map "c" '("Command Apropos" . command-apropos))
953 (define-key help-apropos-map "a" '("Apropos" . apropos))
954
955 (define-key help-keys-map "b"
956 '("List all keystroke commands" . describe-bindings))
957 (define-key help-keys-map "c"
958 '("Describe key briefly" . describe-key-briefly))
959 (define-key help-keys-map "k"
960 '("Describe key verbose" . describe-key))
961 (define-key help-keys-map "f"
962 '("Describe Lisp function" . describe-function))
963 (define-key help-keys-map "w"
964 '("Where is this command" . where-is))
965
966 (define-key help-manual-map "i" '("Info system" . info))
967 (define-key help-manual-map "t"
968 '("Invoke Emacs tutorial" . help-with-tutorial))
969
970 (define-key help-misc-map "l" '("Last 100 Keystrokes" . view-lossage))
971 (define-key help-misc-map "s" '("Describe syntax table" . describe-syntax))
972
973 (define-key help-modes-map "m"
974 '("Describe current major mode" . describe-mode))
975 (define-key help-modes-map "b"
976 '("List all keystroke commands" . describe-bindings))
977
978 (define-key help-admin-map "n"
979 '("view Emacs news" . view-emacs-news))
980 (define-key help-admin-map "l"
981 '("View the GNU Emacs license" . describe-copying))
982 (define-key help-admin-map "d"
983 '("Describe distribution" . describe-distribution))
984 (define-key help-admin-map "w"
985 '("Describe (non)warranty" . describe-no-warranty))
986
987 (provide 'mouse)
988
989 ;;; mouse.el ends here