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