Major rewrite to behave more like a minor mode.
[bpt/emacs.git] / lisp / mouse.el
1 ;;; mouse.el --- window system-independent mouse support.
2
3 ;;; Copyright (C) 1993, 1994 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 (defvar mouse-yank-at-point nil
40 "*If non-nil, mouse yank commands yank at point instead of at click.")
41 \f
42 ;; Commands that operate on windows.
43
44 (defun mouse-minibuffer-check (event)
45 (let ((w (posn-window (event-start event))))
46 (and (window-minibuffer-p w)
47 (not (minibuffer-window-active-p w))
48 (error "Minibuffer window is not active"))))
49
50 (defun mouse-delete-window (click)
51 "Delete the window you click on.
52 This must be bound to a mouse click."
53 (interactive "e")
54 (mouse-minibuffer-check click)
55 (delete-window (posn-window (event-start click))))
56
57 (defun mouse-select-window (click)
58 "Select the window clicked on; don't move point."
59 (interactive "e")
60 (mouse-minibuffer-check click)
61 (let ((oframe (selected-frame))
62 (frame (window-frame (posn-window (event-start click)))))
63 (select-window (posn-window (event-start click)))
64 (raise-frame frame)
65 (select-frame frame)
66 (or (eq frame oframe)
67 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
68 (unfocus-frame)))
69
70 (defun mouse-tear-off-window (click)
71 "Delete the window clicked on, and create a new frame displaying its buffer."
72 (interactive "e")
73 (mouse-minibuffer-check click)
74 (let* ((window (posn-window (event-start click)))
75 (buf (window-buffer window))
76 (frame (make-frame)))
77 (select-frame frame)
78 (switch-to-buffer buf)
79 (delete-window window)))
80
81 (defun mouse-delete-other-windows ()
82 "Delete all window except the one you click on."
83 (interactive "@")
84 (delete-other-windows))
85
86 (defun mouse-split-window-vertically (click)
87 "Select Emacs window mouse is on, then split it vertically in half.
88 The window is split at the line clicked on.
89 This command must be bound to a mouse click."
90 (interactive "@e")
91 (mouse-minibuffer-check click)
92 (let ((start (event-start click)))
93 (select-window (posn-window start))
94 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
95 (first-line window-min-height)
96 (last-line (- (window-height) window-min-height)))
97 (if (< last-line first-line)
98 (error "Window too short to split")
99 (split-window-vertically
100 (min (max new-height first-line) last-line))))))
101
102 (defun mouse-split-window-horizontally (click)
103 "Select Emacs window mouse is on, then split it horizontally in half.
104 The window is split at the column clicked on.
105 This command must be bound to a mouse click."
106 (interactive "@e")
107 (mouse-minibuffer-check click)
108 (let ((start (event-start click)))
109 (select-window (posn-window start))
110 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
111 (first-col window-min-width)
112 (last-col (- (window-width) window-min-width)))
113 (if (< last-col first-col)
114 (error "Window too narrow to split")
115 (split-window-horizontally
116 (min (max new-width first-col) last-col))))))
117
118 (defun mouse-drag-mode-line (start-event)
119 "Change the height of a window by dragging on the mode line."
120 (interactive "e")
121 (let ((done nil)
122 (echo-keystrokes 0)
123 (start-event-frame (window-frame (car (car (cdr start-event)))))
124 (start-event-window (car (car (cdr start-event))))
125 (start-nwindows (count-windows t))
126 (old-selected-window (selected-window))
127 should-enlarge-minibuffer
128 event mouse minibuffer y top bot edges wconfig params growth)
129 (setq params (frame-parameters))
130 (if (and (not (setq minibuffer (cdr (assq 'minibuffer params))))
131 (one-window-p t))
132 (error "Attempt to resize sole window"))
133 (track-mouse
134 (progn
135 ;; enlarge-window only works on the selected window, so
136 ;; we must select the window where the start event originated.
137 ;; unwind-protect will restore the old selected window later.
138 (select-window start-event-window)
139 ;; if this is the bottommost ordinary window, then to
140 ;; move its modeline the minibuffer must be enlarged.
141 (setq should-enlarge-minibuffer
142 (and minibuffer
143 (not (one-window-p t))
144 (= (nth 1 (window-edges minibuffer))
145 (nth 3 (window-edges)))))
146 ;; loop reading events and sampling the position of
147 ;; the mouse.
148 (while (not done)
149 (setq event (read-event)
150 mouse (mouse-position))
151 ;; do nothing if
152 ;; - there is a switch-frame event.
153 ;; - the mouse isn't in the frame that we started in
154 ;; - the mouse isn't in any Emacs frame
155 ;; drag if
156 ;; - there is a mouse-movement event
157 ;; - there is a scroll-bar-movement event
158 ;; (same as mouse movement for our purposes)
159 ;; quit if
160 ;; - there is a keyboard event or some other unknown event
161 ;; unknown event.
162 (cond ((integerp event)
163 (setq done t))
164 ((eq (car event) 'switch-frame)
165 nil)
166 ((not (memq (car event)
167 '(mouse-movement scroll-bar-movement)))
168 (if (consp event)
169 (setq unread-command-events
170 (cons event unread-command-events)))
171 (setq done t))
172 ((not (eq (car mouse) start-event-frame))
173 nil)
174 ((null (car (cdr mouse)))
175 nil)
176 (t
177 (setq y (cdr (cdr mouse))
178 edges (window-edges)
179 top (nth 1 edges)
180 bot (nth 3 edges))
181 ;; scale back a move that would make the
182 ;; window too short.
183 (cond ((< (- y top -1) window-min-height)
184 (setq y (+ top window-min-height -1))))
185 ;; compute size change needed
186 (setq growth (- y bot -1)
187 wconfig (current-window-configuration))
188 ;; grow/shrink minibuffer?
189 (if should-enlarge-minibuffer
190 (progn
191 ;; yes. briefly select minibuffer so
192 ;; enlarge-window will affect the
193 ;; correct window.
194 (select-window minibuffer)
195 ;; scale back shrinkage if it would
196 ;; make the minibuffer less than 1
197 ;; line tall.
198 (if (and (> growth 0)
199 (< (- (window-height minibuffer)
200 growth)
201 1))
202 (setq growth (1- (window-height minibuffer))))
203 (enlarge-window (- growth))
204 (select-window start-event-window))
205 ;; no. grow/shrink the selected window
206 (enlarge-window growth))
207 ;; if this window's growth caused another
208 ;; window to be deleted because it was too
209 ;; short, rescind the change.
210 ;;
211 ;; if size change caused space to be stolen
212 ;; from a window above this one, rescind the
213 ;; change, but only if we didn't grow/srhink
214 ;; the minibuffer. minibuffer size changes
215 ;; can cause all windows to shrink... no way
216 ;; around it.
217 (if (or (/= start-nwindows (count-windows t))
218 (and (not should-enlarge-minibuffer)
219 (/= top (nth 1 (window-edges)))))
220 (set-window-configuration wconfig)))))))))
221 \f
222 (defun mouse-set-point (event)
223 "Move point to the position clicked on with the mouse.
224 This should be bound to a mouse click event type."
225 (interactive "e")
226 (mouse-minibuffer-check event)
227 ;; Use event-end in case called from mouse-drag-region.
228 ;; If EVENT is a click, event-end and event-start give same value.
229 (let ((posn (event-end event)))
230 (if (not (windowp (posn-window posn)))
231 (error "Cursor not in text area of window"))
232 (select-window (posn-window posn))
233 (if (numberp (posn-point posn))
234 (goto-char (posn-point posn)))))
235
236 (defun mouse-set-region (click)
237 "Set the region to the text dragged over, and copy to kill ring.
238 This should be bound to a mouse drag event."
239 (interactive "e")
240 (mouse-minibuffer-check click)
241 (let ((posn (event-start click))
242 (end (event-end click)))
243 (select-window (posn-window posn))
244 (if (numberp (posn-point posn))
245 (goto-char (posn-point posn)))
246 ;; If mark is highlighted, no need to bounce the cursor.
247 (or (and transient-mark-mode
248 (eq (framep (selected-frame)) 'x))
249 (sit-for 1))
250 (push-mark)
251 (set-mark (point))
252 (if (numberp (posn-point end))
253 (goto-char (posn-point end)))
254 ;; Don't set this-command to kill-region, so that a following
255 ;; C-w will not double the text in the kill ring.
256 (let (this-command)
257 (copy-region-as-kill (mark) (point)))))
258
259 (defvar mouse-scroll-delay 0.25
260 "*The pause between scroll steps caused by mouse drags, in seconds.
261 If you drag the mouse beyond the edge of a window, Emacs scrolls the
262 window to bring the text beyond that edge into view, with a delay of
263 this many seconds between scroll steps. Scrolling stops when you move
264 the mouse back into the window, or release the button.
265 This variable's value may be non-integral.
266 Setting this to zero causes Emacs to scroll as fast as it can.")
267
268 (defun mouse-scroll-subr (window jump &optional overlay start)
269 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
270 If OVERLAY is an overlay, let it stretch from START to the far edge of
271 the newly visible text.
272 Upon exit, point is at the far edge of the newly visible text."
273 (let ((opoint (point)))
274 (while (progn
275 (goto-char (window-start window))
276 (if (not (zerop (vertical-motion jump window)))
277 (progn
278 (set-window-start window (point))
279 (if (natnump jump)
280 (progn
281 (goto-char (window-end window))
282 ;; window-end doesn't reflect the window's new
283 ;; start position until the next redisplay. Hurrah.
284 (vertical-motion (1- jump) window))
285 (goto-char (window-start window)))
286 (if overlay
287 (move-overlay overlay start (point)))
288 ;; Now that we have scrolled WINDOW properly,
289 ;; put point back where it was for the redisplay
290 ;; so that we don't mess up the selected window.
291 (or (eq window (selected-window))
292 (goto-char opoint))
293 (sit-for mouse-scroll-delay)))))
294 (or (eq window (selected-window))
295 (goto-char opoint))))
296
297 (defvar mouse-drag-overlay (make-overlay 1 1))
298 (overlay-put mouse-drag-overlay 'face 'region)
299
300 (defvar mouse-selection-click-count 0)
301
302 (defun mouse-drag-region (start-event)
303 "Set the region to the text that the mouse is dragged over.
304 Highlight the drag area as you move the mouse.
305 This must be bound to a button-down mouse event.
306 In Transient Mark mode, the highlighting remains once you
307 release the mouse button. Otherwise, it does not."
308 (interactive "e")
309 (mouse-minibuffer-check start-event)
310 (let* ((start-posn (event-start start-event))
311 (start-point (posn-point start-posn))
312 (start-window (posn-window start-posn))
313 (start-frame (window-frame start-window))
314 (bounds (window-edges start-window))
315 (top (nth 1 bounds))
316 (bottom (if (window-minibuffer-p start-window)
317 (nth 3 bounds)
318 ;; Don't count the mode line.
319 (1- (nth 3 bounds))))
320 (click-count (1- (event-click-count start-event))))
321 (setq mouse-selection-click-count click-count)
322 (mouse-set-point start-event)
323 (let ((range (mouse-start-end start-point start-point click-count)))
324 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
325 (window-buffer start-window)))
326 (deactivate-mark)
327 (let (event end end-point)
328 (track-mouse
329 (while (progn
330 (setq event (read-event))
331 (or (mouse-movement-p event)
332 (eq (car-safe event) 'switch-frame)))
333 (if (eq (car-safe event) 'switch-frame)
334 nil
335 (setq end (event-end event)
336 end-point (posn-point end))
337
338 (cond
339 ;; Are we moving within the original window?
340 ((and (eq (posn-window end) start-window)
341 (integer-or-marker-p end-point))
342 (goto-char end-point)
343 (let ((range (mouse-start-end start-point (point) click-count)))
344 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
345
346 (t
347 (let ((mouse-row (cdr (cdr (mouse-position)))))
348 (cond
349 ((null mouse-row))
350 ((< mouse-row top)
351 (mouse-scroll-subr start-window (- mouse-row top)
352 mouse-drag-overlay start-point))
353 ((>= mouse-row bottom)
354 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
355 mouse-drag-overlay start-point)))))))))
356 (if (consp event)
357 ;;; When we scroll into the mode line or menu bar, or out of the window,
358 ;;; we get events that don't fit these criteria.
359 ;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
360 ;;; (eq (posn-window (event-end event)) start-window)
361 ;;; (numberp (posn-point (event-end event)))
362 (let ((fun (key-binding (vector (car event)))))
363 (if (not (= (overlay-start mouse-drag-overlay)
364 (overlay-end mouse-drag-overlay)))
365 (let (last-command this-command)
366 (push-mark (overlay-start mouse-drag-overlay) t t)
367 (goto-char (overlay-end mouse-drag-overlay))
368 (copy-region-as-kill (point) (mark t)))
369 (goto-char (overlay-end mouse-drag-overlay))
370 (setq this-command 'mouse-set-point))))
371 (delete-overlay mouse-drag-overlay))))
372 \f
373 ;; Commands to handle xterm-style multiple clicks.
374
375 (defun mouse-skip-word (dir)
376 "Skip over word, over whitespace, or over identical punctuation.
377 If DIR is positive skip forward; if negative, skip backward."
378 (let* ((char (following-char))
379 (syntax (char-to-string (char-syntax char))))
380 (if (or (string= syntax "w") (string= syntax " "))
381 (if (< dir 0)
382 (skip-syntax-backward syntax)
383 (skip-syntax-forward syntax))
384 (if (< dir 0)
385 (while (and (not (bobp)) (= (preceding-char) char))
386 (forward-char -1))
387 (while (and (not (eobp)) (= (following-char) char))
388 (forward-char 1))))))
389
390 ;; Return a list of region bounds based on START and END according to MODE.
391 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
392 ;; If MODE is 1 then set point to start of word at (min START END),
393 ;; mark to end of word at (max START END).
394 ;; If MODE is 2 then do the same for lines.
395 (defun mouse-start-end (start end mode)
396 (if (> start end)
397 (let ((temp start))
398 (setq start end
399 end temp)))
400 (setq mode (mod mode 3))
401 (cond ((= mode 0)
402 (list start end))
403 ((and (= mode 1)
404 (= start end)
405 (char-after start)
406 (= (char-syntax (char-after start)) ?\())
407 (list start
408 (save-excursion
409 (goto-char start)
410 (forward-sexp 1)
411 (point))))
412 ((and (= mode 1)
413 (= start end)
414 (char-after start)
415 (= (char-syntax (char-after start)) ?\)))
416 (list (save-excursion
417 (goto-char (1+ start))
418 (backward-sexp 1)
419 (point))
420 (1+ start)))
421 ((= mode 1)
422 (list (save-excursion
423 (goto-char start)
424 (mouse-skip-word -1)
425 (point))
426 (save-excursion
427 (goto-char end)
428 (mouse-skip-word 1)
429 (point))))
430 ((= mode 2)
431 (list (save-excursion
432 (goto-char start)
433 (beginning-of-line 1)
434 (point))
435 (save-excursion
436 (goto-char end)
437 (forward-line 1)
438 (point))))))
439 \f
440 ;; Subroutine: set the mark where CLICK happened,
441 ;; but don't do anything else.
442 (defun mouse-set-mark-fast (click)
443 (mouse-minibuffer-check click)
444 (let ((posn (event-start click)))
445 (select-window (posn-window posn))
446 (if (numberp (posn-point posn))
447 (push-mark (posn-point posn) t t))))
448
449 ;; Momentarily show where the mark is, if highlighting doesn't show it.
450 (defun mouse-show-mark ()
451 (or transient-mark-mode
452 (save-excursion
453 (goto-char (mark t))
454 (sit-for 1))))
455
456 (defun mouse-set-mark (click)
457 "Set mark at the position clicked on with the mouse.
458 Display cursor at that position for a second.
459 This must be bound to a mouse click."
460 (interactive "e")
461 (mouse-minibuffer-check click)
462 (select-window (posn-window (event-start click)))
463 ;; We don't use save-excursion because that preserves the mark too.
464 (let ((point-save (point)))
465 (unwind-protect
466 (progn (mouse-set-point click)
467 (push-mark nil t t)
468 (or transient-mark-mode
469 (sit-for 1)))
470 (goto-char point-save))))
471
472 (defun mouse-kill (click)
473 "Kill the region between point and the mouse click.
474 The text is saved in the kill ring, as with \\[kill-region]."
475 (interactive "e")
476 (mouse-minibuffer-check click)
477 (let* ((posn (event-start click))
478 (click-posn (posn-point posn)))
479 (select-window (posn-window posn))
480 (if (numberp click-posn)
481 (kill-region (min (point) click-posn)
482 (max (point) click-posn)))))
483
484 (defun mouse-yank-at-click (click arg)
485 "Insert the last stretch of killed text at the position clicked on.
486 Also move point to one end of the text thus inserted (normally the end).
487 Prefix arguments are interpreted as with \\[yank].
488 If `mouse-yank-at-point' is non-nil, insert at point
489 regardless of where you click."
490 (interactive "e\nP")
491 (or mouse-yank-at-point (mouse-set-point click))
492 (setq this-command 'yank)
493 (yank arg))
494
495 (defun mouse-kill-ring-save (click)
496 "Copy the region between point and the mouse click in the kill ring.
497 This does not delete the region; it acts like \\[kill-ring-save]."
498 (interactive "e")
499 (mouse-set-mark-fast click)
500 (let (this-command last-command)
501 (kill-ring-save (point) (mark t)))
502 (mouse-show-mark))
503
504 ;;; This function used to delete the text between point and the mouse
505 ;;; whenever it was equal to the front of the kill ring, but some
506 ;;; people found that confusing.
507
508 ;;; A list (TEXT START END), describing the text and position of the last
509 ;;; invocation of mouse-save-then-kill.
510 (defvar mouse-save-then-kill-posn nil)
511
512 (defun mouse-save-then-kill-delete-region (beg end)
513 ;; We must make our own undo boundaries
514 ;; because they happen automatically only for the current buffer.
515 (undo-boundary)
516 (if (or (= beg end) (eq buffer-undo-list t))
517 ;; If we have no undo list in this buffer,
518 ;; just delete.
519 (delete-region beg end)
520 ;; Delete, but make the undo-list entry share with the kill ring.
521 ;; First, delete just one char, so in case buffer is being modified
522 ;; for the first time, the undo list records that fact.
523 (let (before-change-function after-change-function
524 before-change-functions after-change-functions)
525 (delete-region beg
526 (+ beg (if (> end beg) 1 -1))))
527 (let ((buffer-undo-list buffer-undo-list))
528 ;; Undo that deletion--but don't change the undo list!
529 (let (before-change-function after-change-function
530 before-change-functions after-change-functions)
531 (primitive-undo 1 buffer-undo-list))
532 ;; Now delete the rest of the specified region,
533 ;; but don't record it.
534 (setq buffer-undo-list t)
535 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
536 (error "Lossage in mouse-save-then-kill-delete-region"))
537 (delete-region beg end))
538 (let ((tail buffer-undo-list))
539 ;; Search back in buffer-undo-list for the string
540 ;; that came from deleting one character.
541 (while (and tail (not (stringp (car (car tail)))))
542 (setq tail (cdr tail)))
543 ;; Replace it with an entry for the entire deleted text.
544 (and tail
545 (setcar tail (cons (car kill-ring) (min beg end))))))
546 (undo-boundary))
547
548 (defun mouse-save-then-kill (click)
549 "Save text to point in kill ring; the second time, kill the text.
550 If the text between point and the mouse is the same as what's
551 at the front of the kill ring, this deletes the text.
552 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
553 which prepares for a second click to delete the text.
554
555 If you have selected words or lines, this command extends the
556 selection through the word or line clicked on. If you do this
557 again in a different position, it extends the selection again.
558 If you do this twice in the same position, the selection is killed."
559 (interactive "e")
560 (mouse-minibuffer-check click)
561 (let ((click-posn (posn-point (event-start click)))
562 ;; Don't let a subsequent kill command append to this one:
563 ;; prevent setting this-command to kill-region.
564 (this-command this-command))
565 (if (> (mod mouse-selection-click-count 3) 0)
566 (if (not (and (eq last-command 'mouse-save-then-kill)
567 (equal click-posn
568 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
569 ;; Find both ends of the object selected by this click.
570 (let* ((range
571 (mouse-start-end click-posn click-posn
572 mouse-selection-click-count)))
573 ;; Move whichever end is closer to the click.
574 ;; That's what xterm does, and it seems reasonable.
575 (if (< (abs (- click-posn (mark t)))
576 (abs (- click-posn (point))))
577 (set-mark (car range))
578 (goto-char (nth 1 range)))
579 ;; We have already put the old region in the kill ring.
580 ;; Replace it with the extended region.
581 ;; (It would be annoying to make a separate entry.)
582 (kill-new (buffer-substring (point) (mark t)) t)
583 ;; Arrange for a repeated mouse-3 to kill this region.
584 (setq mouse-save-then-kill-posn
585 (list (car kill-ring) (point) click-posn))
586 (mouse-show-mark))
587 ;; If we click this button again without moving it,
588 ;; that time kill.
589 (mouse-save-then-kill-delete-region (point) (mark))
590 (setq mouse-selection-click-count 0)
591 (setq mouse-save-then-kill-posn nil))
592 (if (and (eq last-command 'mouse-save-then-kill)
593 mouse-save-then-kill-posn
594 (eq (car mouse-save-then-kill-posn) (car kill-ring))
595 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
596 ;; If this is the second time we've called
597 ;; mouse-save-then-kill, delete the text from the buffer.
598 (progn
599 (mouse-save-then-kill-delete-region (point) (mark))
600 ;; After we kill, another click counts as "the first time".
601 (setq mouse-save-then-kill-posn nil))
602 (if (or (and (eq last-command 'mouse-save-then-kill)
603 mouse-save-then-kill-posn)
604 (and mark-active transient-mark-mode)
605 (and (eq last-command 'mouse-drag-region)
606 (or mark-even-if-inactive
607 (not transient-mark-mode))))
608 ;; We have a selection or suitable region, so adjust it.
609 (let* ((posn (event-start click))
610 (new (posn-point posn)))
611 (select-window (posn-window posn))
612 (if (numberp new)
613 (progn
614 ;; Move whichever end of the region is closer to the click.
615 ;; That is what xterm does, and it seems reasonable.
616 (if (< (abs (- new (point))) (abs (- new (mark t))))
617 (goto-char new)
618 (set-mark new))
619 (setq deactivate-mark nil)))
620 (kill-new (buffer-substring (point) (mark t)) t))
621 ;; We just have point, so set mark here.
622 (mouse-set-mark-fast click)
623 (kill-ring-save (point) (mark t))
624 (mouse-show-mark))
625 (setq mouse-save-then-kill-posn
626 (list (car kill-ring) (point) click-posn))))))
627 \f
628 (global-set-key [M-mouse-1] 'mouse-start-secondary)
629 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
630 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
631 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
632 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
633
634 ;; An overlay which records the current secondary selection
635 ;; or else is deleted when there is no secondary selection.
636 ;; May be nil.
637 (defvar mouse-secondary-overlay nil)
638
639 ;; A marker which records the specified first end for a secondary selection.
640 ;; May be nil.
641 (defvar mouse-secondary-start nil)
642
643 (defun mouse-start-secondary (click)
644 "Set one end of the secondary selection to the position clicked on.
645 Use \\[mouse-secondary-save-then-kill] to set the other end
646 and complete the secondary selection."
647 (interactive "e")
648 (mouse-minibuffer-check click)
649 (let ((posn (event-start click)))
650 (save-excursion
651 (set-buffer (window-buffer (posn-window posn)))
652 ;; Cancel any preexisting secondary selection.
653 (if mouse-secondary-overlay
654 (delete-overlay mouse-secondary-overlay))
655 (if (numberp (posn-point posn))
656 (progn
657 (or mouse-secondary-start
658 (setq mouse-secondary-start (make-marker)))
659 (move-marker mouse-secondary-start (posn-point posn)))))))
660
661 (defun mouse-set-secondary (click)
662 "Set the secondary selection to the text that the mouse is dragged over.
663 This must be bound to a mouse drag event."
664 (interactive "e")
665 (mouse-minibuffer-check click)
666 (let ((posn (event-start click))
667 beg
668 (end (event-end click)))
669 (save-excursion
670 (set-buffer (window-buffer (posn-window posn)))
671 (if (numberp (posn-point posn))
672 (setq beg (posn-point posn)))
673 (if mouse-secondary-overlay
674 (move-overlay mouse-secondary-overlay beg (posn-point end))
675 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
676 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
677
678 (defun mouse-drag-secondary (start-event)
679 "Set the secondary selection to the text that the mouse is dragged over.
680 Highlight the drag area as you move the mouse.
681 This must be bound to a button-down mouse event."
682 (interactive "e")
683 (mouse-minibuffer-check start-event)
684 (let* ((start-posn (event-start start-event))
685 (start-point (posn-point start-posn))
686 (start-window (posn-window start-posn))
687 (start-frame (window-frame start-window))
688 (bounds (window-edges start-window))
689 (top (nth 1 bounds))
690 (bottom (if (window-minibuffer-p start-window)
691 (nth 3 bounds)
692 ;; Don't count the mode line.
693 (1- (nth 3 bounds))))
694 (click-count (1- (event-click-count start-event))))
695 (save-excursion
696 (set-buffer (window-buffer start-window))
697 (setq mouse-selection-click-count click-count)
698 (or mouse-secondary-overlay
699 (setq mouse-secondary-overlay
700 (make-overlay (point) (point))))
701 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
702 (if (> (mod click-count 3) 0)
703 ;; Double or triple press: make an initial selection
704 ;; of one word or line.
705 (let ((range (mouse-start-end start-point start-point click-count)))
706 (set-marker mouse-secondary-start nil)
707 (move-overlay mouse-secondary-overlay 1 1
708 (window-buffer start-window))
709 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
710 (window-buffer start-window)))
711 ;; Single-press: cancel any preexisting secondary selection.
712 (or mouse-secondary-start
713 (setq mouse-secondary-start (make-marker)))
714 (set-marker mouse-secondary-start start-point)
715 (delete-overlay mouse-secondary-overlay))
716 (let (event end end-point)
717 (track-mouse
718 (while (progn
719 (setq event (read-event))
720 (or (mouse-movement-p event)
721 (eq (car-safe event) 'switch-frame)))
722
723 (if (eq (car-safe event) 'switch-frame)
724 nil
725 (setq end (event-end event)
726 end-point (posn-point end))
727 (cond
728 ;; Are we moving within the original window?
729 ((and (eq (posn-window end) start-window)
730 (integer-or-marker-p end-point))
731 (let ((range (mouse-start-end start-point end-point
732 click-count)))
733 (if (or (/= start-point end-point)
734 (null (marker-position mouse-secondary-start)))
735 (progn
736 (set-marker mouse-secondary-start nil)
737 (move-overlay mouse-secondary-overlay
738 (car range) (nth 1 range))))))
739 (t
740 (let ((mouse-row (cdr (cdr (mouse-position)))))
741 (cond
742 ((null mouse-row))
743 ((< mouse-row top)
744 (mouse-scroll-subr start-window (- mouse-row top)
745 mouse-secondary-overlay start-point))
746 ((>= mouse-row bottom)
747 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
748 mouse-secondary-overlay start-point)))))))))
749
750 (if (consp event)
751 ;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
752 ;;; (eq (posn-window (event-end event)) start-window)
753 ;;; (numberp (posn-point (event-end event)))
754 (if (marker-position mouse-secondary-start)
755 (save-window-excursion
756 (delete-overlay mouse-secondary-overlay)
757 (x-set-selection 'SECONDARY nil)
758 (select-window start-window)
759 (save-excursion
760 (goto-char mouse-secondary-start)
761 (sit-for 1)))
762 (x-set-selection
763 'SECONDARY
764 (buffer-substring (overlay-start mouse-secondary-overlay)
765 (overlay-end mouse-secondary-overlay)))))))))
766
767 (defun mouse-yank-secondary (click)
768 "Insert the secondary selection at the position clicked on.
769 Move point to the end of the inserted text.
770 If `mouse-yank-at-point' is non-nil, insert at point
771 regardless of where you click."
772 (interactive "e")
773 (or mouse-yank-at-point (mouse-set-point click))
774 (insert (x-get-selection 'SECONDARY)))
775
776 (defun mouse-kill-secondary ()
777 "Kill the text in the secondary selection.
778 This is intended more as a keyboard command than as a mouse command
779 but it can work as either one.
780
781 The current buffer (in case of keyboard use), or the buffer clicked on,
782 must be the one that the secondary selection is in. This requirement
783 is to prevent accidents."
784 (interactive)
785 (let* ((keys (this-command-keys))
786 (click (elt keys (1- (length keys)))))
787 (or (eq (overlay-buffer mouse-secondary-overlay)
788 (if (listp click)
789 (window-buffer (posn-window (event-start click)))
790 (current-buffer)))
791 (error "Select or click on the buffer where the secondary selection is")))
792 (save-excursion
793 (set-buffer (overlay-buffer mouse-secondary-overlay))
794 (kill-region (overlay-start mouse-secondary-overlay)
795 (overlay-end mouse-secondary-overlay)))
796 (delete-overlay mouse-secondary-overlay)
797 (x-set-selection 'SECONDARY nil)
798 (setq mouse-secondary-overlay nil))
799
800 (defun mouse-secondary-save-then-kill (click)
801 "Save text to point in kill ring; the second time, kill the text.
802 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
803 If the text between where you did \\[mouse-start-secondary] and where
804 you use this command matches the text at the front of the kill ring,
805 this command deletes the text.
806 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
807 which prepares for a second click with this command to delete the text.
808
809 If you have already made a secondary selection in that buffer,
810 this command extends or retracts the selection to where you click.
811 If you do this again in a different position, it extends or retracts
812 again. If you do this twice in the same position, it kills the selection."
813 (interactive "e")
814 (mouse-minibuffer-check click)
815 (let ((posn (event-start click))
816 (click-posn (posn-point (event-start click)))
817 ;; Don't let a subsequent kill command append to this one:
818 ;; prevent setting this-command to kill-region.
819 (this-command this-command))
820 (or (eq (window-buffer (posn-window posn))
821 (or (and mouse-secondary-overlay
822 (overlay-buffer mouse-secondary-overlay))
823 (if mouse-secondary-start
824 (marker-buffer mouse-secondary-start))))
825 (error "Wrong buffer"))
826 (save-excursion
827 (set-buffer (window-buffer (posn-window posn)))
828 (if (> (mod mouse-selection-click-count 3) 0)
829 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
830 (equal click-posn
831 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
832 ;; Find both ends of the object selected by this click.
833 (let* ((range
834 (mouse-start-end click-posn click-posn
835 mouse-selection-click-count)))
836 ;; Move whichever end is closer to the click.
837 ;; That's what xterm does, and it seems reasonable.
838 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
839 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
840 (move-overlay mouse-secondary-overlay (car range)
841 (overlay-end mouse-secondary-overlay))
842 (move-overlay mouse-secondary-overlay
843 (overlay-start mouse-secondary-overlay)
844 (nth 1 range)))
845 ;; We have already put the old region in the kill ring.
846 ;; Replace it with the extended region.
847 ;; (It would be annoying to make a separate entry.)
848 (kill-new (buffer-substring
849 (overlay-start mouse-secondary-overlay)
850 (overlay-end mouse-secondary-overlay)) t)
851 ;; Arrange for a repeated mouse-3 to kill this region.
852 (setq mouse-save-then-kill-posn
853 (list (car kill-ring) (point) click-posn)))
854 ;; If we click this button again without moving it,
855 ;; that time kill.
856 (progn
857 (mouse-save-then-kill-delete-region
858 (overlay-start mouse-secondary-overlay)
859 (overlay-end mouse-secondary-overlay))
860 (setq mouse-save-then-kill-posn nil)
861 (setq mouse-selection-click-count 0)
862 (delete-overlay mouse-secondary-overlay)))
863 (if (and (eq last-command 'mouse-secondary-save-then-kill)
864 mouse-save-then-kill-posn
865 (eq (car mouse-save-then-kill-posn) (car kill-ring))
866 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
867 ;; If this is the second time we've called
868 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
869 (progn
870 (mouse-save-then-kill-delete-region
871 (overlay-start mouse-secondary-overlay)
872 (overlay-end mouse-secondary-overlay))
873 (setq mouse-save-then-kill-posn nil)
874 (delete-overlay mouse-secondary-overlay))
875 (if (overlay-start mouse-secondary-overlay)
876 ;; We have a selection, so adjust it.
877 (progn
878 (if (numberp click-posn)
879 (progn
880 ;; Move whichever end of the region is closer to the click.
881 ;; That is what xterm does, and it seems reasonable.
882 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
883 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
884 (move-overlay mouse-secondary-overlay click-posn
885 (overlay-end mouse-secondary-overlay))
886 (move-overlay mouse-secondary-overlay
887 (overlay-start mouse-secondary-overlay)
888 click-posn))
889 (setq deactivate-mark nil)))
890 (if (eq last-command 'mouse-secondary-save-then-kill)
891 ;; If the front of the kill ring comes from
892 ;; an immediately previous use of this command,
893 ;; replace it with the extended region.
894 ;; (It would be annoying to make a separate entry.)
895 (kill-new (buffer-substring
896 (overlay-start mouse-secondary-overlay)
897 (overlay-end mouse-secondary-overlay)) t)
898 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
899 (overlay-end mouse-secondary-overlay))))
900 (if mouse-secondary-start
901 ;; All we have is one end of a selection,
902 ;; so put the other end here.
903 (let ((start (+ 0 mouse-secondary-start)))
904 (kill-ring-save start click-posn)
905 (if mouse-secondary-overlay
906 (move-overlay mouse-secondary-overlay start click-posn)
907 (setq mouse-secondary-overlay (make-overlay start click-posn)))
908 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
909 (setq mouse-save-then-kill-posn
910 (list (car kill-ring) (point) click-posn))))
911 (x-set-selection 'SECONDARY
912 (if (overlay-buffer mouse-secondary-overlay)
913 (buffer-substring
914 (overlay-start mouse-secondary-overlay)
915 (overlay-end mouse-secondary-overlay)))))))
916 \f
917 (defun mouse-buffer-menu (event)
918 "Pop up a menu of buffers for selection with the mouse.
919 This switches buffers in the window that you clicked on,
920 and selects that window."
921 (interactive "e")
922 (mouse-minibuffer-check event)
923 (let ((menu
924 (list "Buffer Menu"
925 (cons "Select Buffer"
926 (let ((tail (buffer-list))
927 (maxbuf 0)
928 head)
929 (while tail
930 (or (eq ?\ (aref (buffer-name (car tail)) 0))
931 (setq maxbuf
932 (max maxbuf
933 (length (buffer-name (car tail))))))
934 (setq tail (cdr tail)))
935 (setq tail (buffer-list))
936 (while tail
937 (let ((elt (car tail)))
938 (if (not (string-match "^ "
939 (buffer-name elt)))
940 (setq head (cons
941 (cons
942 (format
943 (format "%%%ds %%s%%s %%s"
944 maxbuf)
945 (buffer-name elt)
946 (if (buffer-modified-p elt)
947 "*" " ")
948 (save-excursion
949 (set-buffer elt)
950 (if buffer-read-only "%" " "))
951 (or (buffer-file-name elt) ""))
952 elt)
953 head))))
954 (setq tail (cdr tail)))
955 (reverse head))))))
956 (let ((buf (x-popup-menu event menu))
957 (window (posn-window (event-start event))))
958 (if buf
959 (progn
960 (or (framep window) (select-window window))
961 (switch-to-buffer buf))))))
962 \f
963 ;;; These need to be rewritten for the new scroll bar implementation.
964
965 ;;;!! ;; Commands for the scroll bar.
966 ;;;!!
967 ;;;!! (defun mouse-scroll-down (click)
968 ;;;!! (interactive "@e")
969 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
970 ;;;!!
971 ;;;!! (defun mouse-scroll-up (click)
972 ;;;!! (interactive "@e")
973 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
974 ;;;!!
975 ;;;!! (defun mouse-scroll-down-full ()
976 ;;;!! (interactive "@")
977 ;;;!! (scroll-down nil))
978 ;;;!!
979 ;;;!! (defun mouse-scroll-up-full ()
980 ;;;!! (interactive "@")
981 ;;;!! (scroll-up nil))
982 ;;;!!
983 ;;;!! (defun mouse-scroll-move-cursor (click)
984 ;;;!! (interactive "@e")
985 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
986 ;;;!!
987 ;;;!! (defun mouse-scroll-absolute (event)
988 ;;;!! (interactive "@e")
989 ;;;!! (let* ((pos (car event))
990 ;;;!! (position (car pos))
991 ;;;!! (length (car (cdr pos))))
992 ;;;!! (if (<= length 0) (setq length 1))
993 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
994 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
995 ;;;!! position)
996 ;;;!! length)
997 ;;;!! scale-factor)))
998 ;;;!! (goto-char newpos)
999 ;;;!! (recenter '(4)))))
1000 ;;;!!
1001 ;;;!! (defun mouse-scroll-left (click)
1002 ;;;!! (interactive "@e")
1003 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1004 ;;;!!
1005 ;;;!! (defun mouse-scroll-right (click)
1006 ;;;!! (interactive "@e")
1007 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1008 ;;;!!
1009 ;;;!! (defun mouse-scroll-left-full ()
1010 ;;;!! (interactive "@")
1011 ;;;!! (scroll-left nil))
1012 ;;;!!
1013 ;;;!! (defun mouse-scroll-right-full ()
1014 ;;;!! (interactive "@")
1015 ;;;!! (scroll-right nil))
1016 ;;;!!
1017 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1018 ;;;!! (interactive "@e")
1019 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1020 ;;;!!
1021 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1022 ;;;!! (interactive "@e")
1023 ;;;!! (let* ((pos (car event))
1024 ;;;!! (position (car pos))
1025 ;;;!! (length (car (cdr pos))))
1026 ;;;!! (set-window-hscroll (selected-window) 33)))
1027 ;;;!!
1028 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1029 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1030 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1031 ;;;!!
1032 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1033 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1034 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1035 ;;;!!
1036 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1037 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1038 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1039 ;;;!!
1040 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1041 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1042 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1043 ;;;!!
1044 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1045 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1046 ;;;!! 'mouse-scroll-absolute-horizontally)
1047 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1048 ;;;!!
1049 ;;;!! (global-set-key [horizontal-slider mouse-1]
1050 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1051 ;;;!! (global-set-key [horizontal-slider mouse-2]
1052 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1053 ;;;!! (global-set-key [horizontal-slider mouse-3]
1054 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1055 ;;;!!
1056 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1057 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1058 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1059 ;;;!!
1060 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1061 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1062 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1063 ;;;!!
1064 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1065 ;;;!! 'mouse-split-window-horizontally)
1066 ;;;!! (global-set-key [mode-line S-mouse-2]
1067 ;;;!! 'mouse-split-window-horizontally)
1068 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1069 ;;;!! 'mouse-split-window)
1070 \f
1071 ;;;!! ;;;;
1072 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1073 ;;;!! ;;;; are of the form:
1074 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1075 ;;;!! ;;
1076 ;;;!! ;;;;
1077 ;;;!! ;;;; Dynamically track mouse coordinates
1078 ;;;!! ;;;;
1079 ;;;!! ;;
1080 ;;;!! ;;(defun track-mouse (event)
1081 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1082 ;;;!! ;; (interactive "@e")
1083 ;;;!! ;; (while mouse-grabbed
1084 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1085 ;;;!! ;; (abs-x (car pos))
1086 ;;;!! ;; (abs-y (cdr pos))
1087 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1088 ;;;!! ;; (list (car pos) (cdr pos))
1089 ;;;!! ;; (selected-window))))
1090 ;;;!! ;; (if (consp relative-coordinate)
1091 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1092 ;;;!! ;; (car relative-coordinate)
1093 ;;;!! ;; (car (cdr relative-coordinate)))
1094 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1095 ;;;!!
1096 ;;;!! ;;
1097 ;;;!! ;; Dynamically put a box around the line indicated by point
1098 ;;;!! ;;
1099 ;;;!! ;;
1100 ;;;!! ;;(require 'backquote)
1101 ;;;!! ;;
1102 ;;;!! ;;(defun mouse-select-buffer-line (event)
1103 ;;;!! ;; (interactive "@e")
1104 ;;;!! ;; (let ((relative-coordinate
1105 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1106 ;;;!! ;; (abs-y (car (cdr (car event)))))
1107 ;;;!! ;; (if (consp relative-coordinate)
1108 ;;;!! ;; (progn
1109 ;;;!! ;; (save-excursion
1110 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1111 ;;;!! ;; (x-draw-rectangle
1112 ;;;!! ;; (selected-screen)
1113 ;;;!! ;; abs-y 0
1114 ;;;!! ;; (save-excursion
1115 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1116 ;;;!! ;; (end-of-line)
1117 ;;;!! ;; (push-mark nil t)
1118 ;;;!! ;; (beginning-of-line)
1119 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1120 ;;;!! ;; (sit-for 1)
1121 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1122 ;;;!! ;;
1123 ;;;!! ;;(defvar last-line-drawn nil)
1124 ;;;!! ;;(defvar begin-delim "[^ \t]")
1125 ;;;!! ;;(defvar end-delim "[^ \t]")
1126 ;;;!! ;;
1127 ;;;!! ;;(defun mouse-boxing (event)
1128 ;;;!! ;; (interactive "@e")
1129 ;;;!! ;; (save-excursion
1130 ;;;!! ;; (let ((screen (selected-screen)))
1131 ;;;!! ;; (while (= (x-mouse-events) 0)
1132 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1133 ;;;!! ;; (abs-x (car pos))
1134 ;;;!! ;; (abs-y (cdr pos))
1135 ;;;!! ;; (relative-coordinate
1136 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1137 ;;;!! ;; (selected-window)))
1138 ;;;!! ;; (begin-reg nil)
1139 ;;;!! ;; (end-reg nil)
1140 ;;;!! ;; (end-column nil)
1141 ;;;!! ;; (begin-column nil))
1142 ;;;!! ;; (if (and (consp relative-coordinate)
1143 ;;;!! ;; (or (not last-line-drawn)
1144 ;;;!! ;; (not (= last-line-drawn abs-y))))
1145 ;;;!! ;; (progn
1146 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1147 ;;;!! ;; (if (= (following-char) 10)
1148 ;;;!! ;; ()
1149 ;;;!! ;; (progn
1150 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1151 ;;;!! ;; (setq begin-column (1- (current-column)))
1152 ;;;!! ;; (end-of-line)
1153 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1154 ;;;!! ;; (setq end-column (1+ (current-column)))
1155 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1156 ;;;!! ;; (x-draw-rectangle screen
1157 ;;;!! ;; (setq last-line-drawn abs-y)
1158 ;;;!! ;; begin-column
1159 ;;;!! ;; (- end-column begin-column) 1))))))))))
1160 ;;;!! ;;
1161 ;;;!! ;;(defun mouse-erase-box ()
1162 ;;;!! ;; (interactive)
1163 ;;;!! ;; (if last-line-drawn
1164 ;;;!! ;; (progn
1165 ;;;!! ;; (x-erase-rectangle (selected-screen))
1166 ;;;!! ;; (setq last-line-drawn nil))))
1167 ;;;!!
1168 ;;;!! ;;; (defun test-x-rectangle ()
1169 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1170 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1171 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1172 ;;;!!
1173 ;;;!! ;;
1174 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1175 ;;;!! ;;
1176 ;;;!!
1177 ;;;!! (defvar double-start nil)
1178 ;;;!! (defconst double-click-interval 300
1179 ;;;!! "Max ticks between clicks")
1180 ;;;!!
1181 ;;;!! (defun double-down (event)
1182 ;;;!! (interactive "@e")
1183 ;;;!! (if double-start
1184 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1185 ;;;!! (if (< interval double-click-interval)
1186 ;;;!! (progn
1187 ;;;!! (backward-up-list 1)
1188 ;;;!! ;; (message "Interval %d" interval)
1189 ;;;!! (sleep-for 1)))
1190 ;;;!! (setq double-start nil))
1191 ;;;!! (setq double-start (nth 4 event))))
1192 ;;;!!
1193 ;;;!! (defun double-up (event)
1194 ;;;!! (interactive "@e")
1195 ;;;!! (and double-start
1196 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1197 ;;;!! (setq double-start nil)))
1198 ;;;!!
1199 ;;;!! ;;; (defun x-test-doubleclick ()
1200 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1201 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1202 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1203 ;;;!!
1204 ;;;!! ;;
1205 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1206 ;;;!! ;;
1207 ;;;!!
1208 ;;;!! (defvar scrolled-lines 0)
1209 ;;;!! (defconst scroll-speed 1)
1210 ;;;!!
1211 ;;;!! (defun incr-scroll-down (event)
1212 ;;;!! (interactive "@e")
1213 ;;;!! (setq scrolled-lines 0)
1214 ;;;!! (incremental-scroll scroll-speed))
1215 ;;;!!
1216 ;;;!! (defun incr-scroll-up (event)
1217 ;;;!! (interactive "@e")
1218 ;;;!! (setq scrolled-lines 0)
1219 ;;;!! (incremental-scroll (- scroll-speed)))
1220 ;;;!!
1221 ;;;!! (defun incremental-scroll (n)
1222 ;;;!! (while (= (x-mouse-events) 0)
1223 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1224 ;;;!! (scroll-down n)
1225 ;;;!! (sit-for 300 t)))
1226 ;;;!!
1227 ;;;!! (defun incr-scroll-stop (event)
1228 ;;;!! (interactive "@e")
1229 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1230 ;;;!! (setq scrolled-lines 0)
1231 ;;;!! (sleep-for 1))
1232 ;;;!!
1233 ;;;!! ;;; (defun x-testing-scroll ()
1234 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1235 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1236 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1237 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1238 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1239 ;;;!!
1240 ;;;!! ;;
1241 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1242 ;;;!! ;;
1243 ;;;!!
1244 ;;;!! (defun mouse-kill-rectangle (event)
1245 ;;;!! "Kill the rectangle between point and the mouse cursor."
1246 ;;;!! (interactive "@e")
1247 ;;;!! (let ((point-save (point)))
1248 ;;;!! (save-excursion
1249 ;;;!! (mouse-set-point event)
1250 ;;;!! (push-mark nil t)
1251 ;;;!! (if (> point-save (point))
1252 ;;;!! (kill-rectangle (point) point-save)
1253 ;;;!! (kill-rectangle point-save (point))))))
1254 ;;;!!
1255 ;;;!! (defun mouse-open-rectangle (event)
1256 ;;;!! "Kill the rectangle between point and the mouse cursor."
1257 ;;;!! (interactive "@e")
1258 ;;;!! (let ((point-save (point)))
1259 ;;;!! (save-excursion
1260 ;;;!! (mouse-set-point event)
1261 ;;;!! (push-mark nil t)
1262 ;;;!! (if (> point-save (point))
1263 ;;;!! (open-rectangle (point) point-save)
1264 ;;;!! (open-rectangle point-save (point))))))
1265 ;;;!!
1266 ;;;!! ;; Must be a better way to do this.
1267 ;;;!!
1268 ;;;!! (defun mouse-multiple-insert (n char)
1269 ;;;!! (while (> n 0)
1270 ;;;!! (insert char)
1271 ;;;!! (setq n (1- n))))
1272 ;;;!!
1273 ;;;!! ;; What this could do is not finalize until button was released.
1274 ;;;!!
1275 ;;;!! (defun mouse-move-text (event)
1276 ;;;!! "Move text from point to cursor position, inserting spaces."
1277 ;;;!! (interactive "@e")
1278 ;;;!! (let* ((relative-coordinate
1279 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1280 ;;;!! (if (consp relative-coordinate)
1281 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1282 ;;;!! (delete-char
1283 ;;;!! (- (car relative-coordinate) (current-column))))
1284 ;;;!! ((< (current-column) (car relative-coordinate))
1285 ;;;!! (mouse-multiple-insert
1286 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1287 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1288 \f
1289 ;; Choose a completion with the mouse.
1290
1291 (defun mouse-choose-completion (event)
1292 "Click on an alternative in the `*Completions*' buffer to choose it."
1293 (interactive "e")
1294 (let ((buffer (window-buffer))
1295 choice
1296 base-size)
1297 (save-excursion
1298 (set-buffer (window-buffer (posn-window (event-start event))))
1299 (if completion-reference-buffer
1300 (setq buffer completion-reference-buffer))
1301 (setq base-size completion-base-size)
1302 (save-excursion
1303 (goto-char (posn-point (event-start event)))
1304 (let (beg end)
1305 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1306 (setq end (point) beg (1+ (point))))
1307 (if (null beg)
1308 (error "No completion here"))
1309 (setq beg (previous-single-property-change beg 'mouse-face))
1310 (setq end (or (next-single-property-change end 'mouse-face)
1311 (point-max)))
1312 (setq choice (buffer-substring beg end)))))
1313 (let ((owindow (selected-window)))
1314 (select-window (posn-window (event-start event)))
1315 (if (and (one-window-p t 'selected-frame)
1316 (window-dedicated-p (selected-window)))
1317 ;; This is a special buffer's frame
1318 (iconify-frame (selected-frame))
1319 (or (window-dedicated-p (selected-window))
1320 (bury-buffer)))
1321 (select-window owindow))
1322 (choose-completion-string choice buffer base-size)))
1323 \f
1324 ;; Font selection.
1325
1326 (defun font-menu-add-default ()
1327 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1328 (font-alist x-fixed-font-alist)
1329 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1330 (if (assoc "Default" elt)
1331 (delete (assoc "Default" elt) elt))
1332 (setcdr elt
1333 (cons (list "Default"
1334 (cdr (assq 'font (frame-parameters (selected-frame)))))
1335 (cdr elt)))))
1336
1337 (defvar x-fixed-font-alist
1338 '("Font menu"
1339 ("Misc"
1340 ;; For these, we specify the pixel height and width.
1341 ("fixed" "fixed")
1342 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1343 ("6x12"
1344 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1345 ("6x13"
1346 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1347 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1348 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1349 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1350 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1351 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1352 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1353 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1354 ("")
1355 ("clean 5x8"
1356 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1357 ("clean 6x8"
1358 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1359 ("clean 8x8"
1360 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1361 ("clean 8x10"
1362 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1363 ("clean 8x14"
1364 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1365 ("clean 8x16"
1366 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1367 ("")
1368 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
1369 ;;; We don't seem to have these; who knows what they are.
1370 ;;; ("fg-18" "fg-18")
1371 ;;; ("fg-25" "fg-25")
1372 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1373 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1374 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1375 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1376 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1377 ("Courier"
1378 ;; For these, we specify the point height.
1379 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1380 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1381 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1382 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1383 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1384 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1385 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1386 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1387 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1388 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1389 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1390 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1391 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1392 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1393 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1394 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1395 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1396 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1397 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1398 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1399 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1400 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1401 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1402 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
1403 )
1404 "X fonts suitable for use in Emacs.")
1405
1406 (defun mouse-set-font (&rest fonts)
1407 "Select an emacs font from a list of known good fonts"
1408 (interactive
1409 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
1410 (if fonts
1411 (let (font)
1412 (while fonts
1413 (condition-case nil
1414 (progn
1415 (set-default-font (car fonts))
1416 (setq font (car fonts))
1417 (setq fonts nil))
1418 (error
1419 (setq fonts (cdr fonts)))))
1420 (if (null font)
1421 (error "Font not found")))))
1422 \f
1423 ;;; Bindings for mouse commands.
1424
1425 (define-key global-map [down-mouse-1] 'mouse-drag-region)
1426 (global-set-key [mouse-1] 'mouse-set-point)
1427 (global-set-key [drag-mouse-1] 'mouse-set-region)
1428
1429 ;; These are tested for in mouse-drag-region.
1430 (global-set-key [double-mouse-1] 'mouse-set-point)
1431 (global-set-key [triple-mouse-1] 'mouse-set-point)
1432
1433 (global-set-key [mouse-2] 'mouse-yank-at-click)
1434 (global-set-key [mouse-3] 'mouse-save-then-kill)
1435
1436 ;; By binding these to down-going events, we let the user use the up-going
1437 ;; event to make the selection, saving a click.
1438 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1439 (global-set-key [C-down-mouse-3] 'mouse-set-font)
1440
1441 ;; Replaced with dragging mouse-1
1442 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
1443
1444 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1445 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1446 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1447 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1448 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1449 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1450 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1451 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1452
1453 (provide 'mouse)
1454
1455 ;;; mouse.el ends here