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