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