(etags_getcwd): Use getcwd if available even if MSDOS.
[bpt/emacs.git] / lisp / mouse.el
CommitLineData
be010748 1;;; mouse.el --- window system-independent mouse support
84176303 2
be010748 3;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
eea8d4ef 4
84176303 5;; Maintainer: FSF
84176303
ER
6;; Keywords: hardware
7
be010748 8;; This file is part of GNU Emacs.
72ea54a4 9
be010748
RS
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
72ea54a4 14
be010748
RS
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
be010748 20;; You should have received a copy of the GNU General Public License
4c9afcbe
RS
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
72ea54a4 24
edbd2f74
ER
25;;; Commentary:
26
27;; This package provides various useful commands (including help
28;; system access) through the mouse. All this code assumes that mouse
29;; interpretation has been abstracted into Emacs input events.
30;;
31;; The code is rather X-dependent.
32
aae56ea7
ER
33;;; Code:
34
cc0a8174 35;;; Utility functions.
72ea54a4 36
cc0a8174
JB
37;;; Indent track-mouse like progn.
38(put 'track-mouse 'lisp-indent-function 0)
72ea54a4 39
50f58001
RS
40(defvar mouse-yank-at-point nil
41 "*If non-nil, mouse yank commands yank at point instead of at click.")
cc0a8174 42\f
95132d1c
RS
43;; Provide a mode-specific menu on a mouse button.
44
45(defun mouse-major-mode-menu (event)
46 "Pop up a mode-specific menu of mouse commands."
47 ;; Switch to the window clicked on, because otherwise
48 ;; the mode's commands may not make sense.
49 (interactive "@e")
d5c847bb
KH
50 (let (;; This is where mouse-major-mode-menu-prefix
51 ;; returns the prefix we should use (after menu-bar).
52 ;; It is either nil or (SOME-SYMBOL).
53 (mouse-major-mode-menu-prefix nil)
54 ;; Make a keymap in which our last command leads to a menu
55 (newmap (make-sparse-keymap (concat mode-name " Mode")))
56 result)
57 ;; Make our menu inherit from the desired keymap
58 ;; which we want to display as the menu now.
59 (set-keymap-parent newmap
60 (mouse-major-mode-menu-1
61 (and (current-local-map)
62 (lookup-key (current-local-map) [menu-bar]))))
63 (setq result (x-popup-menu t (list newmap)))
64 (if result
65 (let ((command (key-binding
66 (apply 'vector (append '(menu-bar)
67 mouse-major-mode-menu-prefix
68 result)))))
69 (if command
70 (command-execute command))))))
95132d1c
RS
71
72;; Compute and cache the equivalent keys in MENU and all its submenus.
d5c847bb
KH
73;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
74;;; (and (eq (car menu) 'keymap)
75;;; (x-popup-menu nil menu))
76;;; (while menu
77;;; (and (consp (car menu))
78;;; (consp (cdr (car menu)))
79;;; (let ((tail (cdr (car menu))))
80;;; (while (and (consp tail)
81;;; (not (eq (car tail) 'keymap)))
82;;; (setq tail (cdr tail)))
83;;; (if (consp tail)
84;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
85;;; (setq menu (cdr menu))))
95132d1c
RS
86
87;; Given a mode's menu bar keymap,
88;; if it defines exactly one menu bar menu,
89;; return just that menu.
90;; Otherwise return a menu for all of them.
91(defun mouse-major-mode-menu-1 (menubar)
92 (if menubar
93 (let ((tail menubar)
94 submap)
95 (while tail
96 (if (consp (car tail))
97 (if submap
98 (setq submap t)
d5c847bb 99 (setq submap (car tail))))
95132d1c 100 (setq tail (cdr tail)))
d5c847bb 101 (if (eq submap t)
d3e458b0 102 menubar
d5c847bb
KH
103 (setq mouse-major-mode-menu-prefix (list (car submap)))
104 (cdr (cdr submap))))))
95132d1c 105\f
544e7e73
RS
106;; Commands that operate on windows.
107
d65147f6
KH
108(defun mouse-minibuffer-check (event)
109 (let ((w (posn-window (event-start event))))
110 (and (window-minibuffer-p w)
111 (not (minibuffer-window-active-p w))
33c448cd
RS
112 (error "Minibuffer window is not active")))
113 ;; Give temporary modes such as isearch a chance to turn off.
114 (run-hooks 'mouse-leave-buffer-hook))
d65147f6 115
cc0a8174 116(defun mouse-delete-window (click)
947da0c4 117 "Delete the window you click on.
cc0a8174 118This must be bound to a mouse click."
ec558adc 119 (interactive "e")
d65147f6 120 (mouse-minibuffer-check click)
b5370f03 121 (delete-window (posn-window (event-start click))))
cc0a8174 122
3c2dd2c0
RS
123(defun mouse-select-window (click)
124 "Select the window clicked on; don't move point."
125 (interactive "e")
d65147f6 126 (mouse-minibuffer-check click)
3c2dd2c0
RS
127 (let ((oframe (selected-frame))
128 (frame (window-frame (posn-window (event-start click)))))
129 (select-window (posn-window (event-start click)))
130 (raise-frame frame)
131 (select-frame frame)
132 (or (eq frame oframe)
133 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
134 (unfocus-frame)))
135
b0f3a26b
JB
136(defun mouse-tear-off-window (click)
137 "Delete the window clicked on, and create a new frame displaying its buffer."
138 (interactive "e")
d65147f6 139 (mouse-minibuffer-check click)
b0f3a26b
JB
140 (let* ((window (posn-window (event-start click)))
141 (buf (window-buffer window))
01a911e3 142 (frame (make-frame)))
b0f3a26b
JB
143 (select-frame frame)
144 (switch-to-buffer buf)
145 (delete-window window)))
146
b5370f03 147(defun mouse-delete-other-windows ()
947da0c4 148 "Delete all window except the one you click on."
b5370f03 149 (interactive "@")
cc0a8174 150 (delete-other-windows))
72ea54a4 151
cc0a8174
JB
152(defun mouse-split-window-vertically (click)
153 "Select Emacs window mouse is on, then split it vertically in half.
154The window is split at the line clicked on.
155This command must be bound to a mouse click."
947da0c4 156 (interactive "@e")
d65147f6 157 (mouse-minibuffer-check click)
b5370f03
JB
158 (let ((start (event-start click)))
159 (select-window (posn-window start))
85d6b80b 160 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
5ba2dc3f
JB
161 (first-line window-min-height)
162 (last-line (- (window-height) window-min-height)))
163 (if (< last-line first-line)
0a50b993 164 (error "Window too short to split")
5ba2dc3f
JB
165 (split-window-vertically
166 (min (max new-height first-line) last-line))))))
cc0a8174 167
947da0c4
RS
168(defun mouse-split-window-horizontally (click)
169 "Select Emacs window mouse is on, then split it horizontally in half.
170The window is split at the column clicked on.
171This command must be bound to a mouse click."
172 (interactive "@e")
d65147f6 173 (mouse-minibuffer-check click)
5ba2dc3f
JB
174 (let ((start (event-start click)))
175 (select-window (posn-window start))
176 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
177 (first-col window-min-width)
178 (last-col (- (window-width) window-min-width)))
179 (if (< last-col first-col)
0a50b993 180 (error "Window too narrow to split")
5ba2dc3f
JB
181 (split-window-horizontally
182 (min (max new-width first-col) last-col))))))
947da0c4 183
544e7e73
RS
184(defun mouse-drag-mode-line (start-event)
185 "Change the height of a window by dragging on the mode line."
186 (interactive "e")
33c448cd
RS
187 ;; Give temporary modes such as isearch a chance to turn off.
188 (run-hooks 'mouse-leave-buffer-hook)
544e7e73
RS
189 (let ((done nil)
190 (echo-keystrokes 0)
191 (start-event-frame (window-frame (car (car (cdr start-event)))))
192 (start-event-window (car (car (cdr start-event))))
193 (start-nwindows (count-windows t))
194 (old-selected-window (selected-window))
195 should-enlarge-minibuffer
196 event mouse minibuffer y top bot edges wconfig params growth)
197 (setq params (frame-parameters))
198 (if (and (not (setq minibuffer (cdr (assq 'minibuffer params))))
199 (one-window-p t))
200 (error "Attempt to resize sole window"))
201 (track-mouse
202 (progn
203 ;; enlarge-window only works on the selected window, so
204 ;; we must select the window where the start event originated.
205 ;; unwind-protect will restore the old selected window later.
206 (select-window start-event-window)
207 ;; if this is the bottommost ordinary window, then to
208 ;; move its modeline the minibuffer must be enlarged.
209 (setq should-enlarge-minibuffer
210 (and minibuffer
211 (not (one-window-p t))
212 (= (nth 1 (window-edges minibuffer))
213 (nth 3 (window-edges)))))
214 ;; loop reading events and sampling the position of
215 ;; the mouse.
216 (while (not done)
217 (setq event (read-event)
218 mouse (mouse-position))
219 ;; do nothing if
220 ;; - there is a switch-frame event.
221 ;; - the mouse isn't in the frame that we started in
222 ;; - the mouse isn't in any Emacs frame
223 ;; drag if
224 ;; - there is a mouse-movement event
225 ;; - there is a scroll-bar-movement event
226 ;; (same as mouse movement for our purposes)
227 ;; quit if
228 ;; - there is a keyboard event or some other unknown event
229 ;; unknown event.
230 (cond ((integerp event)
231 (setq done t))
232 ((eq (car event) 'switch-frame)
233 nil)
234 ((not (memq (car event)
235 '(mouse-movement scroll-bar-movement)))
236 (if (consp event)
237 (setq unread-command-events
238 (cons event unread-command-events)))
239 (setq done t))
240 ((not (eq (car mouse) start-event-frame))
241 nil)
242 ((null (car (cdr mouse)))
243 nil)
244 (t
245 (setq y (cdr (cdr mouse))
246 edges (window-edges)
247 top (nth 1 edges)
248 bot (nth 3 edges))
249 ;; scale back a move that would make the
250 ;; window too short.
251 (cond ((< (- y top -1) window-min-height)
252 (setq y (+ top window-min-height -1))))
253 ;; compute size change needed
254 (setq growth (- y bot -1)
255 wconfig (current-window-configuration))
256 ;; grow/shrink minibuffer?
257 (if should-enlarge-minibuffer
258 (progn
259 ;; yes. briefly select minibuffer so
260 ;; enlarge-window will affect the
261 ;; correct window.
262 (select-window minibuffer)
263 ;; scale back shrinkage if it would
264 ;; make the minibuffer less than 1
265 ;; line tall.
266 (if (and (> growth 0)
267 (< (- (window-height minibuffer)
268 growth)
269 1))
270 (setq growth (1- (window-height minibuffer))))
271 (enlarge-window (- growth))
272 (select-window start-event-window))
273 ;; no. grow/shrink the selected window
274 (enlarge-window growth))
275 ;; if this window's growth caused another
276 ;; window to be deleted because it was too
277 ;; short, rescind the change.
278 ;;
279 ;; if size change caused space to be stolen
280 ;; from a window above this one, rescind the
281 ;; change, but only if we didn't grow/srhink
282 ;; the minibuffer. minibuffer size changes
283 ;; can cause all windows to shrink... no way
284 ;; around it.
285 (if (or (/= start-nwindows (count-windows t))
286 (and (not should-enlarge-minibuffer)
287 (/= top (nth 1 (window-edges)))))
288 (set-window-configuration wconfig)))))))))
289\f
08a1c178
RS
290(defun mouse-drag-vertical-line (start-event)
291 "Change the width of a window by dragging on the vertical line."
292 (interactive "e")
293 ;; Give temporary modes such as isearch a chance to turn off.
294 (run-hooks 'mouse-leave-buffer-hook)
295 (let ((done nil)
296 (echo-keystrokes 0)
297 (start-event-frame (window-frame (car (car (cdr start-event)))))
298 (start-event-window (car (car (cdr start-event))))
299 (start-nwindows (count-windows t))
300 (old-selected-window (selected-window))
301 event mouse x left right edges wconfig growth)
302 (if (one-window-p t)
303 (error "Attempt to resize sole ordinary window"))
304 (if (= (nth 2 (window-edges start-event-window))
305 (frame-width start-event-frame))
306 (error "Attempt to drag rightmost scrollbar"))
307 (track-mouse
308 (progn
309 ;; enlarge-window only works on the selected window, so
310 ;; we must select the window where the start event originated.
311 ;; unwind-protect will restore the old selected window later.
312 (select-window start-event-window)
313 ;; loop reading events and sampling the position of
314 ;; the mouse.
315 (while (not done)
316 (setq event (read-event)
317 mouse (mouse-position))
318 ;; do nothing if
319 ;; - there is a switch-frame event.
320 ;; - the mouse isn't in the frame that we started in
321 ;; - the mouse isn't in any Emacs frame
322 ;; drag if
323 ;; - there is a mouse-movement event
324 ;; - there is a scroll-bar-movement event
325 ;; (same as mouse movement for our purposes)
326 ;; quit if
327 ;; - there is a keyboard event or some other unknown event
328 ;; unknown event.
329 (cond ((integerp event)
330 (setq done t))
331 ((eq (car event) 'switch-frame)
332 nil)
333 ((not (memq (car event)
334 '(mouse-movement scroll-bar-movement)))
335 (if (consp event)
336 (setq unread-command-events
337 (cons event unread-command-events)))
338 (setq done t))
339 ((not (eq (car mouse) start-event-frame))
340 nil)
341 ((null (car (cdr mouse)))
342 nil)
343 (t
344 (setq x (car (cdr mouse))
345 edges (window-edges)
346 left (nth 0 edges)
347 right (nth 2 edges))
348 ;; scale back a move that would make the
349 ;; window too thin.
350 (cond ((< (- x left -1) window-min-width)
351 (setq x (+ left window-min-width -1))))
352 ;; compute size change needed
353 (setq growth (- x right -1)
354 wconfig (current-window-configuration))
355 (enlarge-window growth t)
356 ;; if this window's growth caused another
357 ;; window to be deleted because it was too
358 ;; thin, rescind the change.
359 ;;
360 ;; if size change caused space to be stolen
361 ;; from a window to the left of this one,
362 ;; rescind the change.
363 (if (or (/= start-nwindows (count-windows t))
364 (/= left (nth 0 (window-edges))))
365 (set-window-configuration wconfig)))))))))
366\f
2a5fa27b 367(defun mouse-set-point (event)
cc0a8174 368 "Move point to the position clicked on with the mouse.
2a5fa27b 369This should be bound to a mouse click event type."
ec558adc 370 (interactive "e")
d65147f6 371 (mouse-minibuffer-check event)
2a5fa27b
RS
372 ;; Use event-end in case called from mouse-drag-region.
373 ;; If EVENT is a click, event-end and event-start give same value.
374 (let ((posn (event-end event)))
0a50b993
RS
375 (if (not (windowp (posn-window posn)))
376 (error "Cursor not in text area of window"))
b5370f03
JB
377 (select-window (posn-window posn))
378 (if (numberp (posn-point posn))
379 (goto-char (posn-point posn)))))
cc0a8174 380
e6c2f5d4
RS
381(defvar mouse-last-region-beg nil)
382(defvar mouse-last-region-end nil)
383(defvar mouse-last-region-tick nil)
384
385(defun mouse-region-match ()
386 "Return non-nil if there's an active region that was set with the mouse."
387 (and (mark t) mark-active
388 (eq mouse-last-region-beg (region-beginning))
389 (eq mouse-last-region-end (region-end))
390 (eq mouse-last-region-tick (buffer-modified-tick))))
391
652ccd35 392(defun mouse-set-region (click)
e37de120 393 "Set the region to the text dragged over, and copy to kill ring.
2a5fa27b 394This should be bound to a mouse drag event."
652ccd35 395 (interactive "e")
d65147f6 396 (mouse-minibuffer-check click)
652ccd35
RS
397 (let ((posn (event-start click))
398 (end (event-end click)))
399 (select-window (posn-window posn))
400 (if (numberp (posn-point posn))
401 (goto-char (posn-point posn)))
fcfc3c63 402 ;; If mark is highlighted, no need to bounce the cursor.
33a35434
KH
403 ;; On X, we highlight while dragging, thus once again no need to bounce.
404 (or transient-mark-mode
405 (eq (framep (selected-frame)) 'x)
f9278a75 406 (eq (framep (selected-frame)) 'pc)
f93f3cf5 407 (eq (framep (selected-frame)) 'win32)
fcfc3c63 408 (sit-for 1))
652ccd35 409 (push-mark)
1cc8a3f4 410 (set-mark (point))
652ccd35 411 (if (numberp (posn-point end))
e37de120
RS
412 (goto-char (posn-point end)))
413 ;; Don't set this-command to kill-region, so that a following
414 ;; C-w will not double the text in the kill ring.
91a6bc10
RS
415 ;; Ignore last-command so we don't append to a preceding kill.
416 (let (this-command last-command)
e6c2f5d4
RS
417 (copy-region-as-kill (mark) (point)))
418 (mouse-set-region-1)))
419
420(defun mouse-set-region-1 ()
421 (setq mouse-last-region-beg (region-beginning))
422 (setq mouse-last-region-end (region-end))
423 (setq mouse-last-region-tick (buffer-modified-tick)))
652ccd35 424
600c6e3a
JB
425(defvar mouse-scroll-delay 0.25
426 "*The pause between scroll steps caused by mouse drags, in seconds.
427If you drag the mouse beyond the edge of a window, Emacs scrolls the
428window to bring the text beyond that edge into view, with a delay of
429this many seconds between scroll steps. Scrolling stops when you move
430the mouse back into the window, or release the button.
431This variable's value may be non-integral.
432Setting this to zero causes Emacs to scroll as fast as it can.")
433
08a1c178 434(defvar mouse-scroll-min-lines 1
a3d6bb97
RS
435 "*The minimum number of lines scrolled by dragging mouse out of window.
436Moving the mouse out the top or bottom edge of the window begins
437scrolling repeatedly. The number of lines scrolled per repetition
438is normally equal to the number of lines beyond the window edge that
439the mouse has moved. However, it always scrolls at least the number
440of lines specified by this variable.")
08a1c178 441
e919a622
RS
442(defun mouse-scroll-subr (window jump &optional overlay start)
443 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
600c6e3a
JB
444If OVERLAY is an overlay, let it stretch from START to the far edge of
445the newly visible text.
446Upon exit, point is at the far edge of the newly visible text."
08a1c178
RS
447 (cond
448 ((and (> jump 0) (< jump mouse-scroll-min-lines))
449 (setq jump mouse-scroll-min-lines))
450 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
451 (setq jump (- mouse-scroll-min-lines))))
4e399a53
RS
452 (let ((opoint (point)))
453 (while (progn
454 (goto-char (window-start window))
455 (if (not (zerop (vertical-motion jump window)))
456 (progn
457 (set-window-start window (point))
458 (if (natnump jump)
459 (progn
460 (goto-char (window-end window))
461 ;; window-end doesn't reflect the window's new
462 ;; start position until the next redisplay. Hurrah.
463 (vertical-motion (1- jump) window))
464 (goto-char (window-start window)))
465 (if overlay
466 (move-overlay overlay start (point)))
467 ;; Now that we have scrolled WINDOW properly,
468 ;; put point back where it was for the redisplay
469 ;; so that we don't mess up the selected window.
470 (or (eq window (selected-window))
471 (goto-char opoint))
d2287ded 472 (sit-for mouse-scroll-delay)))))
4e399a53
RS
473 (or (eq window (selected-window))
474 (goto-char opoint))))
fcfc3c63 475
dc269e81 476;; Create an overlay and immediately delete it, to get "overlay in no buffer".
600c6e3a 477(defvar mouse-drag-overlay (make-overlay 1 1))
dc269e81 478(delete-overlay mouse-drag-overlay)
600c6e3a
JB
479(overlay-put mouse-drag-overlay 'face 'region)
480
dd524dbd 481(defvar mouse-selection-click-count 0)
eb6ff46f 482
c8c5bd24
RS
483(defvar mouse-selection-click-count-buffer nil)
484
600c6e3a 485(defun mouse-drag-region (start-event)
bcd5aef1 486 "Set the region to the text that the mouse is dragged over.
78210c95
RS
487Highlight the drag area as you move the mouse.
488This must be bound to a button-down mouse event.
d017deb2
RS
489In Transient Mark mode, the highlighting remains as long as the mark
490remains active. Otherwise, it remains until the next input event."
bcd5aef1 491 (interactive "e")
d65147f6 492 (mouse-minibuffer-check start-event)
4c9afcbe
RS
493 (let* ((echo-keystrokes 0)
494 (start-posn (event-start start-event))
600c6e3a
JB
495 (start-point (posn-point start-posn))
496 (start-window (posn-window start-posn))
b846d039 497 (start-frame (window-frame start-window))
600c6e3a
JB
498 (bounds (window-edges start-window))
499 (top (nth 1 bounds))
500 (bottom (if (window-minibuffer-p start-window)
501 (nth 3 bounds)
502 ;; Don't count the mode line.
e37de120
RS
503 (1- (nth 3 bounds))))
504 (click-count (1- (event-click-count start-event))))
eb6ff46f 505 (setq mouse-selection-click-count click-count)
c8c5bd24 506 (setq mouse-selection-click-count-buffer (current-buffer))
b80f1928 507 (mouse-set-point start-event)
08a1c178
RS
508 ;; In case the down click is in the middle of some intangible text,
509 ;; use the end of that text, and put it in START-POINT.
510 (if (< (point) start-point)
511 (goto-char start-point))
512 (setq start-point (point))
e37de120
RS
513 (let ((range (mouse-start-end start-point start-point click-count)))
514 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
515 (window-buffer start-window)))
f767385c 516 (deactivate-mark)
08a1c178
RS
517 ;; end-of-range is used only in the single-click case.
518 ;; It is the place where the drag has reached so far
519 ;; (but not outside the window where the drag started).
d5c847bb 520 (let (event end end-point last-end-point (end-of-range (point)))
bcd5aef1 521 (track-mouse
600c6e3a 522 (while (progn
b846d039
JB
523 (setq event (read-event))
524 (or (mouse-movement-p event)
525 (eq (car-safe event) 'switch-frame)))
b846d039
JB
526 (if (eq (car-safe event) 'switch-frame)
527 nil
528 (setq end (event-end event)
529 end-point (posn-point end))
d5c847bb
KH
530 (if end-point
531 (setq last-end-point end-point))
b846d039
JB
532
533 (cond
b846d039
JB
534 ;; Are we moving within the original window?
535 ((and (eq (posn-window end) start-window)
536 (integer-or-marker-p end-point))
08a1c178
RS
537 ;; Go to START-POINT first, so that when we move to END-POINT,
538 ;; if it's in the middle of intangible text,
539 ;; point jumps in the direction away from START-POINT.
540 (goto-char start-point)
b846d039 541 (goto-char end-point)
08a1c178
RS
542 (if (zerop (% click-count 3))
543 (setq end-of-range (point)))
e37de120
RS
544 (let ((range (mouse-start-end start-point (point) click-count)))
545 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
b846d039 546
50e527bc
KH
547 (t
548 (let ((mouse-row (cdr (cdr (mouse-position)))))
b846d039 549 (cond
50e527bc 550 ((null mouse-row))
b846d039 551 ((< mouse-row top)
e919a622 552 (mouse-scroll-subr start-window (- mouse-row top)
f9278a75
RS
553 mouse-drag-overlay start-point)
554 ;; Without this, point tends to jump back to the starting
555 ;; position where the mouse button was pressed down.
556 (setq end-of-range (overlay-start mouse-drag-overlay)))
d2287ded 557 ((>= mouse-row bottom)
e919a622 558 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
f9278a75
RS
559 mouse-drag-overlay start-point)
560 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
4e399a53 561 (if (consp event)
e37de120 562 (let ((fun (key-binding (vector (car event)))))
bfbcf12e
RS
563 ;; Run the binding of the terminating up-event, if possible.
564 ;; In the case of a multiple click, it gives the wrong results,
565 ;; because it would fail to set up a region.
917baba0 566 (if nil ;; (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
c2ef83b4 567 ;; In this case, we can just let the up-event execute normally.
08a1c178
RS
568 (let ((end (event-end event)))
569 ;; Set the position in the event before we replay it,
570 ;; because otherwise it may have a position in the wrong
571 ;; buffer.
572 (setcar (cdr end) end-of-range)
e6c2f5d4
RS
573 ;; Delete the overlay before calling the function,
574 ;; because delete-overlay increases buffer-modified-tick.
575 (delete-overlay mouse-drag-overlay)
c2ef83b4
RS
576 (setq unread-command-events
577 (cons event unread-command-events)))
44dc5252
RS
578 (if (not (= (overlay-start mouse-drag-overlay)
579 (overlay-end mouse-drag-overlay)))
d5c847bb 580 (let* ((stop-point (or (posn-point (event-end event)) last-end-point))
6ce29929
RS
581 ;; The end that comes from where we ended the drag.
582 ;; Point goes here.
583 (region-termination
d5c847bb 584 (if (and stop-point (< stop-point start-point))
6ce29929
RS
585 (overlay-start mouse-drag-overlay)
586 (overlay-end mouse-drag-overlay)))
587 ;; The end that comes from where we started the drag.
588 ;; Mark goes there.
589 (region-commencement
590 (- (+ (overlay-end mouse-drag-overlay)
591 (overlay-start mouse-drag-overlay))
592 region-termination))
593 last-command this-command)
594 (push-mark region-commencement t t)
595 (goto-char region-termination)
f42be8fd 596 (copy-region-as-kill (point) (mark t))
1c9df23f
RS
597 (let ((buffer (current-buffer)))
598 (mouse-show-mark)
599 ;; mouse-show-mark can call read-event,
600 ;; and that means the Emacs server could switch buffers
601 ;; under us. If that happened,
602 ;; avoid trying to use the region.
603 (and (mark t) mark-active
604 (eq buffer (current-buffer))
605 (mouse-set-region-1))))
44dc5252 606 (goto-char (overlay-end mouse-drag-overlay))
e6c2f5d4
RS
607 (setq this-command 'mouse-set-point)
608 (delete-overlay mouse-drag-overlay))))
609 (delete-overlay mouse-drag-overlay)))))
e37de120
RS
610\f
611;; Commands to handle xterm-style multiple clicks.
600c6e3a 612
e37de120
RS
613(defun mouse-skip-word (dir)
614 "Skip over word, over whitespace, or over identical punctuation.
615If DIR is positive skip forward; if negative, skip backward."
616 (let* ((char (following-char))
617 (syntax (char-to-string (char-syntax char))))
08a1c178
RS
618 (cond ((or (string= syntax "w") (string= syntax " "))
619 (if (< dir 0)
620 (skip-syntax-backward syntax)
621 (skip-syntax-forward syntax)))
622 ((string= syntax "_")
623 (if (< dir 0)
624 (skip-syntax-backward "w_")
625 (skip-syntax-forward "w_")))
626 ((< dir 0)
627 (while (and (not (bobp)) (= (preceding-char) char))
628 (forward-char -1)))
629 (t
630 (while (and (not (eobp)) (= (following-char) char))
631 (forward-char 1))))))
e37de120
RS
632
633;; Return a list of region bounds based on START and END according to MODE.
634;; If MODE is 0 then set point to (min START END), mark to (max START END).
635;; If MODE is 1 then set point to start of word at (min START END),
636;; mark to end of word at (max START END).
637;; If MODE is 2 then do the same for lines.
eb6ff46f 638(defun mouse-start-end (start end mode)
e37de120
RS
639 (if (> start end)
640 (let ((temp start))
641 (setq start end
642 end temp)))
9a974c88 643 (setq mode (mod mode 3))
e37de120
RS
644 (cond ((= mode 0)
645 (list start end))
646 ((and (= mode 1)
647 (= start end)
1ec71583 648 (char-after start)
e37de120 649 (= (char-syntax (char-after start)) ?\())
6f482eec
RS
650 (list start
651 (save-excursion
652 (goto-char start)
653 (forward-sexp 1)
654 (point))))
e37de120
RS
655 ((and (= mode 1)
656 (= start end)
1ec71583 657 (char-after start)
e37de120
RS
658 (= (char-syntax (char-after start)) ?\)))
659 (list (save-excursion
660 (goto-char (1+ start))
d89a4a47
RS
661 (backward-sexp 1)
662 (point))
e37de120 663 (1+ start)))
53770877
RS
664 ((and (= mode 1)
665 (= start end)
666 (char-after start)
667 (= (char-syntax (char-after start)) ?\"))
668 (let ((open (or (eq start (point-min))
669 (save-excursion
670 (goto-char (- start 1))
671 (looking-at "\\s(\\|\\s \\|\\s>")))))
672 (if open
673 (list start
674 (save-excursion
675 (condition-case nil
676 (progn
677 (goto-char start)
678 (forward-sexp 1)
679 (point))
680 (error end))))
681 (list (1+ start)
682 (save-excursion
683 (condition-case nil
684 (progn
685 (goto-char (1+ start))
686 (backward-sexp 1)
687 (point))
688 (error end)))))))
e37de120
RS
689 ((= mode 1)
690 (list (save-excursion
691 (goto-char start)
692 (mouse-skip-word -1)
693 (point))
694 (save-excursion
695 (goto-char end)
696 (mouse-skip-word 1)
697 (point))))
698 ((= mode 2)
699 (list (save-excursion
700 (goto-char start)
701 (beginning-of-line 1)
702 (point))
703 (save-excursion
704 (goto-char end)
705 (forward-line 1)
706 (point))))))
e66feb07 707\f
3f26b32a
RS
708;; Subroutine: set the mark where CLICK happened,
709;; but don't do anything else.
710(defun mouse-set-mark-fast (click)
d65147f6 711 (mouse-minibuffer-check click)
3f26b32a
RS
712 (let ((posn (event-start click)))
713 (select-window (posn-window posn))
714 (if (numberp (posn-point posn))
715 (push-mark (posn-point posn) t t))))
716
98aac09d
MB
717(defun mouse-undouble-last-event (events)
718 (let* ((index (1- (length events)))
719 (last (nthcdr index events))
720 (event (car last))
721 (basic (event-basic-type event))
722 (modifiers (delq 'double (delq 'triple (copy-sequence (event-modifiers event)))))
723 (new
724 (if (consp event)
725 (cons (event-convert-list (nreverse (cons basic modifiers)))
726 (cdr event))
727 event)))
728 (setcar last new)
729 (if (key-binding (apply 'vector events))
730 t
731 (setcar last event)
732 nil)))
733
3f26b32a 734;; Momentarily show where the mark is, if highlighting doesn't show it.
7aeb7370
RS
735
736(defvar mouse-region-delete-keys '([delete])
737 "List of keys which shall cause the mouse region to be deleted.")
738
3f26b32a 739(defun mouse-show-mark ()
98aac09d 740 (if transient-mark-mode
c8f264ae 741 (if window-system
98aac09d
MB
742 (delete-overlay mouse-drag-overlay))
743 (if window-system
744 (let ((inhibit-quit t)
745 (echo-keystrokes 0)
33d79bad
RS
746 event events key ignore
747 x-lost-selection-hooks)
748 (add-hook 'x-lost-selection-hooks
749 '(lambda (seltype)
750 (if (eq seltype 'PRIMARY)
751 (progn (setq ignore t)
752 (throw 'mouse-show-mark t)))))
98aac09d 753 (move-overlay mouse-drag-overlay (point) (mark t))
33d79bad
RS
754 (catch 'mouse-show-mark
755 (while (progn (setq event (read-event))
756 (setq events (append events (list event)))
757 (setq key (apply 'vector events))
758 (and (memq 'down (event-modifiers event))
759 (not (key-binding key))
760 (not (member key mouse-region-delete-keys))
761 (not (mouse-undouble-last-event events))))))
762 ;; If we lost the selection, just turn off the highlighting.
763 (if ignore
764 nil
765 ;; For certain special keys, delete the region.
766 (if (member key mouse-region-delete-keys)
767 (delete-region (overlay-start mouse-drag-overlay)
768 (overlay-end mouse-drag-overlay))
769 ;; Otherwise, unread the key so it gets executed normally.
770 (setq unread-command-events
771 (nconc events unread-command-events))))
98aac09d
MB
772 (setq quit-flag nil)
773 (delete-overlay mouse-drag-overlay))
774 (save-excursion
775 (goto-char (mark t))
776 (sit-for 1)))))
3f26b32a 777
cc0a8174
JB
778(defun mouse-set-mark (click)
779 "Set mark at the position clicked on with the mouse.
780Display cursor at that position for a second.
781This must be bound to a mouse click."
ec558adc 782 (interactive "e")
f598fb03
RS
783 (mouse-minibuffer-check click)
784 (select-window (posn-window (event-start click)))
785 ;; We don't use save-excursion because that preserves the mark too.
72ea54a4
RS
786 (let ((point-save (point)))
787 (unwind-protect
cc0a8174 788 (progn (mouse-set-point click)
897897e3
RS
789 (push-mark nil t t)
790 (or transient-mark-mode
791 (sit-for 1)))
72ea54a4
RS
792 (goto-char point-save))))
793
cc0a8174
JB
794(defun mouse-kill (click)
795 "Kill the region between point and the mouse click.
796The text is saved in the kill ring, as with \\[kill-region]."
ec558adc 797 (interactive "e")
d65147f6 798 (mouse-minibuffer-check click)
142c7672
KH
799 (let* ((posn (event-start click))
800 (click-posn (posn-point posn)))
801 (select-window (posn-window posn))
bd307392
JB
802 (if (numberp click-posn)
803 (kill-region (min (point) click-posn)
804 (max (point) click-posn)))))
72ea54a4 805
87ef29fd
JB
806(defun mouse-yank-at-click (click arg)
807 "Insert the last stretch of killed text at the position clicked on.
50f58001
RS
808Also move point to one end of the text thus inserted (normally the end).
809Prefix arguments are interpreted as with \\[yank].
810If `mouse-yank-at-point' is non-nil, insert at point
811regardless of where you click."
ec558adc 812 (interactive "e\nP")
33c448cd
RS
813 ;; Give temporary modes such as isearch a chance to turn off.
814 (run-hooks 'mouse-leave-buffer-hook)
50f58001 815 (or mouse-yank-at-point (mouse-set-point click))
d89a4a47 816 (setq this-command 'yank)
a814e9df 817 (setq mouse-selection-click-count 0)
87ef29fd
JB
818 (yank arg))
819
820(defun mouse-kill-ring-save (click)
cc0a8174
JB
821 "Copy the region between point and the mouse click in the kill ring.
822This does not delete the region; it acts like \\[kill-ring-save]."
ec558adc 823 (interactive "e")
3f26b32a 824 (mouse-set-mark-fast click)
6452d8a6
RS
825 (let (this-command last-command)
826 (kill-ring-save (point) (mark t)))
3f26b32a 827 (mouse-show-mark))
72ea54a4 828
dbc4e1c1
JB
829;;; This function used to delete the text between point and the mouse
830;;; whenever it was equal to the front of the kill ring, but some
831;;; people found that confusing.
832
833;;; A list (TEXT START END), describing the text and position of the last
834;;; invocation of mouse-save-then-kill.
835(defvar mouse-save-then-kill-posn nil)
836
26d280b9 837(defun mouse-save-then-kill-delete-region (beg end)
9a974c88
RS
838 ;; We must make our own undo boundaries
839 ;; because they happen automatically only for the current buffer.
840 (undo-boundary)
dd524dbd
RS
841 (if (or (= beg end) (eq buffer-undo-list t))
842 ;; If we have no undo list in this buffer,
843 ;; just delete.
844 (delete-region beg end)
845 ;; Delete, but make the undo-list entry share with the kill ring.
846 ;; First, delete just one char, so in case buffer is being modified
847 ;; for the first time, the undo list records that fact.
2f4b15ef
RS
848 (let (before-change-function after-change-function
849 before-change-functions after-change-functions)
850 (delete-region beg
851 (+ beg (if (> end beg) 1 -1))))
dd524dbd
RS
852 (let ((buffer-undo-list buffer-undo-list))
853 ;; Undo that deletion--but don't change the undo list!
2f4b15ef
RS
854 (let (before-change-function after-change-function
855 before-change-functions after-change-functions)
856 (primitive-undo 1 buffer-undo-list))
dd524dbd
RS
857 ;; Now delete the rest of the specified region,
858 ;; but don't record it.
859 (setq buffer-undo-list t)
9a974c88
RS
860 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
861 (error "Lossage in mouse-save-then-kill-delete-region"))
dd524dbd
RS
862 (delete-region beg end))
863 (let ((tail buffer-undo-list))
864 ;; Search back in buffer-undo-list for the string
865 ;; that came from deleting one character.
866 (while (and tail (not (stringp (car (car tail)))))
867 (setq tail (cdr tail)))
868 ;; Replace it with an entry for the entire deleted text.
869 (and tail
9a974c88
RS
870 (setcar tail (cons (car kill-ring) (min beg end))))))
871 (undo-boundary))
eb6ff46f 872
947da0c4 873(defun mouse-save-then-kill (click)
40a45a9f
RS
874 "Save text to point in kill ring; the second time, kill the text.
875If the text between point and the mouse is the same as what's
876at the front of the kill ring, this deletes the text.
877Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
eb6ff46f
RS
878which prepares for a second click to delete the text.
879
880If you have selected words or lines, this command extends the
881selection through the word or line clicked on. If you do this
882again in a different position, it extends the selection again.
883If you do this twice in the same position, the selection is killed."
947da0c4 884 (interactive "e")
b64548c7
RS
885 (let ((before-scroll point-before-scroll))
886 (mouse-minibuffer-check click)
887 (let ((click-posn (posn-point (event-start click)))
888 ;; Don't let a subsequent kill command append to this one:
889 ;; prevent setting this-command to kill-region.
890 (this-command this-command))
c8c5bd24
RS
891 (if (and (save-excursion
892 (set-buffer (window-buffer (posn-window (event-start click))))
893 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
894 ;; Don't be fooled by a recent click in some other buffer.
895 (eq mouse-selection-click-count-buffer
896 (current-buffer)))))
b64548c7
RS
897 (if (not (and (eq last-command 'mouse-save-then-kill)
898 (equal click-posn
899 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
900 ;; Find both ends of the object selected by this click.
901 (let* ((range
902 (mouse-start-end click-posn click-posn
903 mouse-selection-click-count)))
904 ;; Move whichever end is closer to the click.
905 ;; That's what xterm does, and it seems reasonable.
906 (if (< (abs (- click-posn (mark t)))
907 (abs (- click-posn (point))))
908 (set-mark (car range))
909 (goto-char (nth 1 range)))
910 ;; We have already put the old region in the kill ring.
911 ;; Replace it with the extended region.
912 ;; (It would be annoying to make a separate entry.)
913 (kill-new (buffer-substring (point) (mark t)) t)
e6c2f5d4 914 (mouse-set-region-1)
b64548c7
RS
915 ;; Arrange for a repeated mouse-3 to kill this region.
916 (setq mouse-save-then-kill-posn
917 (list (car kill-ring) (point) click-posn))
918 (mouse-show-mark))
919 ;; If we click this button again without moving it,
920 ;; that time kill.
bcde3748 921 (mouse-save-then-kill-delete-region (mark) (point))
b64548c7 922 (setq mouse-selection-click-count 0)
dd524dbd 923 (setq mouse-save-then-kill-posn nil))
b64548c7
RS
924 (if (and (eq last-command 'mouse-save-then-kill)
925 mouse-save-then-kill-posn
926 (eq (car mouse-save-then-kill-posn) (car kill-ring))
927 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
928 ;; If this is the second time we've called
929 ;; mouse-save-then-kill, delete the text from the buffer.
930 (progn
931 (mouse-save-then-kill-delete-region (point) (mark))
932 ;; After we kill, another click counts as "the first time".
933 (setq mouse-save-then-kill-posn nil))
c8f264ae
RS
934 ;; This is not a repetition.
935 ;; We are adjusting an old selection or creating a new one.
b64548c7
RS
936 (if (or (and (eq last-command 'mouse-save-then-kill)
937 mouse-save-then-kill-posn)
938 (and mark-active transient-mark-mode)
939 (and (memq last-command
940 '(mouse-drag-region mouse-set-region))
941 (or mark-even-if-inactive
942 (not transient-mark-mode))))
943 ;; We have a selection or suitable region, so adjust it.
944 (let* ((posn (event-start click))
945 (new (posn-point posn)))
946 (select-window (posn-window posn))
947 (if (numberp new)
948 (progn
949 ;; Move whichever end of the region is closer to the click.
950 ;; That is what xterm does, and it seems reasonable.
951 (if (< (abs (- new (point))) (abs (- new (mark t))))
952 (goto-char new)
953 (set-mark new))
954 (setq deactivate-mark nil)))
bcde3748
RS
955 (kill-new (buffer-substring (point) (mark t)) t)
956 (mouse-show-mark))
b64548c7
RS
957 ;; Set the mark where point is, then move where clicked.
958 (mouse-set-mark-fast click)
959 (if before-scroll
960 (goto-char before-scroll))
961 (exchange-point-and-mark)
c8f264ae
RS
962 (kill-new (buffer-substring (point) (mark t)))
963 (if window-system
964 (mouse-show-mark)))
e6c2f5d4 965 (mouse-set-region-1)
b64548c7
RS
966 (setq mouse-save-then-kill-posn
967 (list (car kill-ring) (point) click-posn)))))))
e66feb07
RS
968\f
969(global-set-key [M-mouse-1] 'mouse-start-secondary)
970(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
971(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
972(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
9a974c88 973(global-set-key [M-mouse-2] 'mouse-yank-secondary)
e66feb07
RS
974
975;; An overlay which records the current secondary selection
976;; or else is deleted when there is no secondary selection.
977;; May be nil.
978(defvar mouse-secondary-overlay nil)
979
a94c7fcc
RS
980(defvar mouse-secondary-click-count 0)
981
e66feb07
RS
982;; A marker which records the specified first end for a secondary selection.
983;; May be nil.
984(defvar mouse-secondary-start nil)
985
986(defun mouse-start-secondary (click)
987 "Set one end of the secondary selection to the position clicked on.
988Use \\[mouse-secondary-save-then-kill] to set the other end
989and complete the secondary selection."
990 (interactive "e")
d65147f6 991 (mouse-minibuffer-check click)
e66feb07 992 (let ((posn (event-start click)))
230aaa73
RS
993 (save-excursion
994 (set-buffer (window-buffer (posn-window posn)))
995 ;; Cancel any preexisting secondary selection.
996 (if mouse-secondary-overlay
997 (delete-overlay mouse-secondary-overlay))
998 (if (numberp (posn-point posn))
999 (progn
1000 (or mouse-secondary-start
1001 (setq mouse-secondary-start (make-marker)))
1002 (move-marker mouse-secondary-start (posn-point posn)))))))
e66feb07
RS
1003
1004(defun mouse-set-secondary (click)
1005 "Set the secondary selection to the text that the mouse is dragged over.
1006This must be bound to a mouse drag event."
1007 (interactive "e")
d65147f6 1008 (mouse-minibuffer-check click)
e66feb07
RS
1009 (let ((posn (event-start click))
1010 beg
1011 (end (event-end click)))
230aaa73
RS
1012 (save-excursion
1013 (set-buffer (window-buffer (posn-window posn)))
1014 (if (numberp (posn-point posn))
1015 (setq beg (posn-point posn)))
1016 (if mouse-secondary-overlay
1017 (move-overlay mouse-secondary-overlay beg (posn-point end))
1018 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1019 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
947da0c4 1020
d89a4a47 1021(defun mouse-drag-secondary (start-event)
e66feb07 1022 "Set the secondary selection to the text that the mouse is dragged over.
d89a4a47 1023Highlight the drag area as you move the mouse.
e66feb07
RS
1024This must be bound to a button-down mouse event."
1025 (interactive "e")
d65147f6 1026 (mouse-minibuffer-check start-event)
4c9afcbe
RS
1027 (let* ((echo-keystrokes 0)
1028 (start-posn (event-start start-event))
d89a4a47
RS
1029 (start-point (posn-point start-posn))
1030 (start-window (posn-window start-posn))
1031 (start-frame (window-frame start-window))
1032 (bounds (window-edges start-window))
1033 (top (nth 1 bounds))
1034 (bottom (if (window-minibuffer-p start-window)
1035 (nth 3 bounds)
1036 ;; Don't count the mode line.
1037 (1- (nth 3 bounds))))
1038 (click-count (1- (event-click-count start-event))))
1039 (save-excursion
1040 (set-buffer (window-buffer start-window))
a94c7fcc 1041 (setq mouse-secondary-click-count click-count)
d89a4a47
RS
1042 (or mouse-secondary-overlay
1043 (setq mouse-secondary-overlay
1044 (make-overlay (point) (point))))
26d280b9 1045 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
9a974c88 1046 (if (> (mod click-count 3) 0)
26d280b9
RS
1047 ;; Double or triple press: make an initial selection
1048 ;; of one word or line.
d89a4a47
RS
1049 (let ((range (mouse-start-end start-point start-point click-count)))
1050 (set-marker mouse-secondary-start nil)
1051 (move-overlay mouse-secondary-overlay 1 1
1052 (window-buffer start-window))
1053 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1054 (window-buffer start-window)))
26d280b9 1055 ;; Single-press: cancel any preexisting secondary selection.
d89a4a47
RS
1056 (or mouse-secondary-start
1057 (setq mouse-secondary-start (make-marker)))
1058 (set-marker mouse-secondary-start start-point)
1059 (delete-overlay mouse-secondary-overlay))
1060 (let (event end end-point)
1061 (track-mouse
1062 (while (progn
1063 (setq event (read-event))
1064 (or (mouse-movement-p event)
1065 (eq (car-safe event) 'switch-frame)))
1066
1067 (if (eq (car-safe event) 'switch-frame)
1068 nil
1069 (setq end (event-end event)
1070 end-point (posn-point end))
1071 (cond
d89a4a47
RS
1072 ;; Are we moving within the original window?
1073 ((and (eq (posn-window end) start-window)
1074 (integer-or-marker-p end-point))
d89a4a47
RS
1075 (let ((range (mouse-start-end start-point end-point
1076 click-count)))
0136e1e3
RS
1077 (if (or (/= start-point end-point)
1078 (null (marker-position mouse-secondary-start)))
1079 (progn
1080 (set-marker mouse-secondary-start nil)
1081 (move-overlay mouse-secondary-overlay
1082 (car range) (nth 1 range))))))
3b0aebe9
RS
1083 (t
1084 (let ((mouse-row (cdr (cdr (mouse-position)))))
1085 (cond
1086 ((null mouse-row))
1087 ((< mouse-row top)
e919a622
RS
1088 (mouse-scroll-subr start-window (- mouse-row top)
1089 mouse-secondary-overlay start-point))
d2287ded 1090 ((>= mouse-row bottom)
e919a622 1091 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
3b0aebe9 1092 mouse-secondary-overlay start-point)))))))))
d89a4a47 1093
4e399a53
RS
1094 (if (consp event)
1095;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
1096;;; (eq (posn-window (event-end event)) start-window)
1097;;; (numberp (posn-point (event-end event)))
d89a4a47
RS
1098 (if (marker-position mouse-secondary-start)
1099 (save-window-excursion
1100 (delete-overlay mouse-secondary-overlay)
9a974c88 1101 (x-set-selection 'SECONDARY nil)
d89a4a47
RS
1102 (select-window start-window)
1103 (save-excursion
1104 (goto-char mouse-secondary-start)
1105 (sit-for 1)))
9a974c88
RS
1106 (x-set-selection
1107 'SECONDARY
1108 (buffer-substring (overlay-start mouse-secondary-overlay)
1109 (overlay-end mouse-secondary-overlay)))))))))
e66feb07 1110
9a974c88 1111(defun mouse-yank-secondary (click)
50f58001
RS
1112 "Insert the secondary selection at the position clicked on.
1113Move point to the end of the inserted text.
1114If `mouse-yank-at-point' is non-nil, insert at point
1115regardless of where you click."
9a974c88 1116 (interactive "e")
33c448cd
RS
1117 ;; Give temporary modes such as isearch a chance to turn off.
1118 (run-hooks 'mouse-leave-buffer-hook)
50f58001
RS
1119 (or mouse-yank-at-point (mouse-set-point click))
1120 (insert (x-get-selection 'SECONDARY)))
9a974c88 1121
7e6404f6 1122(defun mouse-kill-secondary ()
9a974c88
RS
1123 "Kill the text in the secondary selection.
1124This is intended more as a keyboard command than as a mouse command
1125but it can work as either one.
1126
1127The current buffer (in case of keyboard use), or the buffer clicked on,
1128must be the one that the secondary selection is in. This requirement
1129is to prevent accidents."
7e6404f6
RS
1130 (interactive)
1131 (let* ((keys (this-command-keys))
1132 (click (elt keys (1- (length keys)))))
1133 (or (eq (overlay-buffer mouse-secondary-overlay)
1134 (if (listp click)
1135 (window-buffer (posn-window (event-start click)))
1136 (current-buffer)))
1137 (error "Select or click on the buffer where the secondary selection is")))
9dfab550
RS
1138 (let (this-command)
1139 (save-excursion
1140 (set-buffer (overlay-buffer mouse-secondary-overlay))
1141 (kill-region (overlay-start mouse-secondary-overlay)
1142 (overlay-end mouse-secondary-overlay))))
e66feb07 1143 (delete-overlay mouse-secondary-overlay)
9dfab550 1144;;; (x-set-selection 'SECONDARY nil)
e66feb07
RS
1145 (setq mouse-secondary-overlay nil))
1146
1147(defun mouse-secondary-save-then-kill (click)
d89a4a47 1148 "Save text to point in kill ring; the second time, kill the text.
7bbe2cc7
RS
1149You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1150If the text between where you did \\[mouse-start-secondary] and where
1151you use this command matches the text at the front of the kill ring,
1152this command deletes the text.
e66feb07 1153Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
7bbe2cc7 1154which prepares for a second click with this command to delete the text.
d89a4a47 1155
7bbe2cc7
RS
1156If you have already made a secondary selection in that buffer,
1157this command extends or retracts the selection to where you click.
1158If you do this again in a different position, it extends or retracts
1159again. If you do this twice in the same position, it kills the selection."
e66feb07 1160 (interactive "e")
d65147f6 1161 (mouse-minibuffer-check click)
d89a4a47
RS
1162 (let ((posn (event-start click))
1163 (click-posn (posn-point (event-start click)))
e66feb07
RS
1164 ;; Don't let a subsequent kill command append to this one:
1165 ;; prevent setting this-command to kill-region.
1166 (this-command this-command))
9a974c88
RS
1167 (or (eq (window-buffer (posn-window posn))
1168 (or (and mouse-secondary-overlay
1169 (overlay-buffer mouse-secondary-overlay))
1170 (if mouse-secondary-start
1171 (marker-buffer mouse-secondary-start))))
1172 (error "Wrong buffer"))
1173 (save-excursion
1174 (set-buffer (window-buffer (posn-window posn)))
a94c7fcc 1175 (if (> (mod mouse-secondary-click-count 3) 0)
9a974c88
RS
1176 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1177 (equal click-posn
1178 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1179 ;; Find both ends of the object selected by this click.
1180 (let* ((range
1181 (mouse-start-end click-posn click-posn
a94c7fcc 1182 mouse-secondary-click-count)))
9a974c88
RS
1183 ;; Move whichever end is closer to the click.
1184 ;; That's what xterm does, and it seems reasonable.
1185 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1186 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1187 (move-overlay mouse-secondary-overlay (car range)
1188 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1189 (move-overlay mouse-secondary-overlay
1190 (overlay-start mouse-secondary-overlay)
1191 (nth 1 range)))
9a974c88
RS
1192 ;; We have already put the old region in the kill ring.
1193 ;; Replace it with the extended region.
1194 ;; (It would be annoying to make a separate entry.)
f69140fd
KH
1195 (kill-new (buffer-substring
1196 (overlay-start mouse-secondary-overlay)
1197 (overlay-end mouse-secondary-overlay)) t)
9a974c88
RS
1198 ;; Arrange for a repeated mouse-3 to kill this region.
1199 (setq mouse-save-then-kill-posn
1200 (list (car kill-ring) (point) click-posn)))
1201 ;; If we click this button again without moving it,
1202 ;; that time kill.
d89a4a47 1203 (progn
9a974c88
RS
1204 (mouse-save-then-kill-delete-region
1205 (overlay-start mouse-secondary-overlay)
1206 (overlay-end mouse-secondary-overlay))
1207 (setq mouse-save-then-kill-posn nil)
a94c7fcc 1208 (setq mouse-secondary-click-count 0)
9a974c88
RS
1209 (delete-overlay mouse-secondary-overlay)))
1210 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1211 mouse-save-then-kill-posn
1212 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1213 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1214 ;; If this is the second time we've called
1215 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1216 (progn
1217 (mouse-save-then-kill-delete-region
1218 (overlay-start mouse-secondary-overlay)
1219 (overlay-end mouse-secondary-overlay))
1220 (setq mouse-save-then-kill-posn nil)
1221 (delete-overlay mouse-secondary-overlay))
1222 (if (overlay-start mouse-secondary-overlay)
1223 ;; We have a selection, so adjust it.
1224 (progn
1225 (if (numberp click-posn)
1226 (progn
1227 ;; Move whichever end of the region is closer to the click.
1228 ;; That is what xterm does, and it seems reasonable.
1229 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1230 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1231 (move-overlay mouse-secondary-overlay click-posn
1232 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1233 (move-overlay mouse-secondary-overlay
1234 (overlay-start mouse-secondary-overlay)
1235 click-posn))
9a974c88 1236 (setq deactivate-mark nil)))
0136e1e3 1237 (if (eq last-command 'mouse-secondary-save-then-kill)
f69140fd
KH
1238 ;; If the front of the kill ring comes from
1239 ;; an immediately previous use of this command,
1240 ;; replace it with the extended region.
1241 ;; (It would be annoying to make a separate entry.)
1242 (kill-new (buffer-substring
0136e1e3 1243 (overlay-start mouse-secondary-overlay)
f69140fd 1244 (overlay-end mouse-secondary-overlay)) t)
0136e1e3
RS
1245 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1246 (overlay-end mouse-secondary-overlay))))
9a974c88
RS
1247 (if mouse-secondary-start
1248 ;; All we have is one end of a selection,
1249 ;; so put the other end here.
1250 (let ((start (+ 0 mouse-secondary-start)))
1251 (kill-ring-save start click-posn)
1252 (if mouse-secondary-overlay
1253 (move-overlay mouse-secondary-overlay start click-posn)
1254 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1255 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1256 (setq mouse-save-then-kill-posn
1257 (list (car kill-ring) (point) click-posn))))
fb28fd5b
RS
1258 (if (overlay-buffer mouse-secondary-overlay)
1259 (x-set-selection 'SECONDARY
9a974c88
RS
1260 (buffer-substring
1261 (overlay-start mouse-secondary-overlay)
1262 (overlay-end mouse-secondary-overlay)))))))
e66feb07 1263\f
f1107960
RS
1264(defvar mouse-menu-buffer-maxlen 20
1265 "*Number of buffers in one pane (submenu) of the buffer menu.
1266If we have lots of buffers, divide them into groups of
1267`mouse-menu-buffer-maxlen' and make a pane (or submenu) for each one.")
1268
8b34e79d 1269(defun mouse-buffer-menu (event)
2d82f7b9
RS
1270 "Pop up a menu of buffers for selection with the mouse.
1271This switches buffers in the window that you clicked on,
1272and selects that window."
ec558adc 1273 (interactive "e")
d65147f6 1274 (mouse-minibuffer-check event)
12dcaa7c
RS
1275 (let* ((buffers
1276 ;; Make an alist of (MENU-ITEM . BUFFER).
1277 (let ((tail (buffer-list))
1278 (maxlen 0)
1279 head)
1280 (while tail
1281 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1282 (setq maxlen
1283 (max maxlen
1284 (length (buffer-name (car tail))))))
1285 (setq tail (cdr tail)))
1286 (setq tail (buffer-list))
1287 (while tail
1288 (let ((elt (car tail)))
bca55f26 1289 (if (/= (aref (buffer-name elt) 0) ?\ )
12dcaa7c
RS
1290 (setq head
1291 (cons
1292 (cons
1293 (format
1294 (format "%%%ds %%s%%s %%s" maxlen)
1295 (buffer-name elt)
1296 (if (buffer-modified-p elt) "*" " ")
1297 (save-excursion
1298 (set-buffer elt)
1299 (if buffer-read-only "%" " "))
1300 (or (buffer-file-name elt)
1301 (save-excursion
1302 (set-buffer elt)
1303 (if list-buffers-directory
1304 (expand-file-name
1305 list-buffers-directory)))
1306 ""))
1307 elt)
1308 head))))
1309 (setq tail (cdr tail)))
a49ea24c
RS
1310 ;; Compensate for the reversal that the above loop does.
1311 (nreverse head)))
12dcaa7c
RS
1312 (menu
1313 ;; If we have lots of buffers, divide them into groups of 20
1314 ;; and make a pane (or submenu) for each one.
f1107960 1315 (if (> (length buffers) (/ (* mouse-menu-buffer-maxlen 3) 2))
a49ea24c 1316 (let ((buffers buffers) sublists next
12dcaa7c
RS
1317 (i 1))
1318 (while buffers
f1107960 1319 ;; Pull off the next mouse-menu-buffer-maxlen buffers
12dcaa7c 1320 ;; and make them the next element of sublist.
f1107960 1321 (setq next (nthcdr mouse-menu-buffer-maxlen buffers))
12dcaa7c 1322 (if next
f1107960
RS
1323 (setcdr (nthcdr (1- mouse-menu-buffer-maxlen) buffers)
1324 nil))
12dcaa7c
RS
1325 (setq sublists (cons (cons (format "Buffers %d" i) buffers)
1326 sublists))
1327 (setq i (1+ i))
1328 (setq buffers next))
1329 (cons "Buffer Menu" (nreverse sublists)))
1330 ;; Few buffers--put them all in one pane.
1331 (list "Buffer Menu" (cons "Select Buffer" buffers)))))
2d82f7b9
RS
1332 (let ((buf (x-popup-menu event menu))
1333 (window (posn-window (event-start event))))
1334 (if buf
1335 (progn
c0bb9f3b 1336 (or (framep window) (select-window window))
2d82f7b9 1337 (switch-to-buffer buf))))))
72ea54a4 1338\f
5ba2dc3f 1339;;; These need to be rewritten for the new scroll bar implementation.
dbc4e1c1
JB
1340
1341;;;!! ;; Commands for the scroll bar.
1342;;;!!
1343;;;!! (defun mouse-scroll-down (click)
1344;;;!! (interactive "@e")
1345;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1346;;;!!
1347;;;!! (defun mouse-scroll-up (click)
1348;;;!! (interactive "@e")
1349;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1350;;;!!
1351;;;!! (defun mouse-scroll-down-full ()
1352;;;!! (interactive "@")
1353;;;!! (scroll-down nil))
1354;;;!!
1355;;;!! (defun mouse-scroll-up-full ()
1356;;;!! (interactive "@")
1357;;;!! (scroll-up nil))
1358;;;!!
1359;;;!! (defun mouse-scroll-move-cursor (click)
1360;;;!! (interactive "@e")
1361;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1362;;;!!
1363;;;!! (defun mouse-scroll-absolute (event)
1364;;;!! (interactive "@e")
1365;;;!! (let* ((pos (car event))
1366;;;!! (position (car pos))
1367;;;!! (length (car (cdr pos))))
1368;;;!! (if (<= length 0) (setq length 1))
1369;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1370;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1371;;;!! position)
1372;;;!! length)
1373;;;!! scale-factor)))
1374;;;!! (goto-char newpos)
1375;;;!! (recenter '(4)))))
1376;;;!!
1377;;;!! (defun mouse-scroll-left (click)
1378;;;!! (interactive "@e")
1379;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1380;;;!!
1381;;;!! (defun mouse-scroll-right (click)
1382;;;!! (interactive "@e")
1383;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1384;;;!!
1385;;;!! (defun mouse-scroll-left-full ()
1386;;;!! (interactive "@")
1387;;;!! (scroll-left nil))
1388;;;!!
1389;;;!! (defun mouse-scroll-right-full ()
1390;;;!! (interactive "@")
1391;;;!! (scroll-right nil))
1392;;;!!
1393;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1394;;;!! (interactive "@e")
1395;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1396;;;!!
1397;;;!! (defun mouse-scroll-absolute-horizontally (event)
1398;;;!! (interactive "@e")
1399;;;!! (let* ((pos (car event))
1400;;;!! (position (car pos))
1401;;;!! (length (car (cdr pos))))
1402;;;!! (set-window-hscroll (selected-window) 33)))
1403;;;!!
1404;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1405;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1406;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1407;;;!!
1408;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1409;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1410;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1411;;;!!
1412;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1413;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1414;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1415;;;!!
1416;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1417;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1418;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1419;;;!!
1420;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1421;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1422;;;!! 'mouse-scroll-absolute-horizontally)
1423;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1424;;;!!
1425;;;!! (global-set-key [horizontal-slider mouse-1]
1426;;;!! 'mouse-scroll-move-cursor-horizontally)
1427;;;!! (global-set-key [horizontal-slider mouse-2]
1428;;;!! 'mouse-scroll-move-cursor-horizontally)
1429;;;!! (global-set-key [horizontal-slider mouse-3]
1430;;;!! 'mouse-scroll-move-cursor-horizontally)
1431;;;!!
1432;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1433;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1434;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1435;;;!!
1436;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1437;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1438;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1439;;;!!
1440;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1441;;;!! 'mouse-split-window-horizontally)
1442;;;!! (global-set-key [mode-line S-mouse-2]
1443;;;!! 'mouse-split-window-horizontally)
1444;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1445;;;!! 'mouse-split-window)
6b2154de 1446\f
dbc4e1c1
JB
1447;;;!! ;;;;
1448;;;!! ;;;; Here are experimental things being tested. Mouse events
1449;;;!! ;;;; are of the form:
1450;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1451;;;!! ;;
1452;;;!! ;;;;
1453;;;!! ;;;; Dynamically track mouse coordinates
1454;;;!! ;;;;
1455;;;!! ;;
1456;;;!! ;;(defun track-mouse (event)
1457;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1458;;;!! ;; (interactive "@e")
1459;;;!! ;; (while mouse-grabbed
1460;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1461;;;!! ;; (abs-x (car pos))
1462;;;!! ;; (abs-y (cdr pos))
1463;;;!! ;; (relative-coordinate (coordinates-in-window-p
1464;;;!! ;; (list (car pos) (cdr pos))
1465;;;!! ;; (selected-window))))
1466;;;!! ;; (if (consp relative-coordinate)
1467;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1468;;;!! ;; (car relative-coordinate)
1469;;;!! ;; (car (cdr relative-coordinate)))
1470;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1471;;;!!
1472;;;!! ;;
1473;;;!! ;; Dynamically put a box around the line indicated by point
1474;;;!! ;;
1475;;;!! ;;
1476;;;!! ;;(require 'backquote)
1477;;;!! ;;
1478;;;!! ;;(defun mouse-select-buffer-line (event)
1479;;;!! ;; (interactive "@e")
1480;;;!! ;; (let ((relative-coordinate
1481;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1482;;;!! ;; (abs-y (car (cdr (car event)))))
1483;;;!! ;; (if (consp relative-coordinate)
1484;;;!! ;; (progn
1485;;;!! ;; (save-excursion
1486;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1487;;;!! ;; (x-draw-rectangle
1488;;;!! ;; (selected-screen)
1489;;;!! ;; abs-y 0
1490;;;!! ;; (save-excursion
1491;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1492;;;!! ;; (end-of-line)
1493;;;!! ;; (push-mark nil t)
1494;;;!! ;; (beginning-of-line)
1495;;;!! ;; (- (region-end) (region-beginning))) 1))
1496;;;!! ;; (sit-for 1)
1497;;;!! ;; (x-erase-rectangle (selected-screen))))))
1498;;;!! ;;
1499;;;!! ;;(defvar last-line-drawn nil)
1500;;;!! ;;(defvar begin-delim "[^ \t]")
1501;;;!! ;;(defvar end-delim "[^ \t]")
1502;;;!! ;;
1503;;;!! ;;(defun mouse-boxing (event)
1504;;;!! ;; (interactive "@e")
1505;;;!! ;; (save-excursion
1506;;;!! ;; (let ((screen (selected-screen)))
1507;;;!! ;; (while (= (x-mouse-events) 0)
1508;;;!! ;; (let* ((pos (read-mouse-position screen))
1509;;;!! ;; (abs-x (car pos))
1510;;;!! ;; (abs-y (cdr pos))
1511;;;!! ;; (relative-coordinate
1512;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1513;;;!! ;; (selected-window)))
1514;;;!! ;; (begin-reg nil)
1515;;;!! ;; (end-reg nil)
1516;;;!! ;; (end-column nil)
1517;;;!! ;; (begin-column nil))
1518;;;!! ;; (if (and (consp relative-coordinate)
1519;;;!! ;; (or (not last-line-drawn)
1520;;;!! ;; (not (= last-line-drawn abs-y))))
1521;;;!! ;; (progn
1522;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1523;;;!! ;; (if (= (following-char) 10)
1524;;;!! ;; ()
1525;;;!! ;; (progn
1526;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1527;;;!! ;; (setq begin-column (1- (current-column)))
1528;;;!! ;; (end-of-line)
1529;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1530;;;!! ;; (setq end-column (1+ (current-column)))
1531;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1532;;;!! ;; (x-draw-rectangle screen
1533;;;!! ;; (setq last-line-drawn abs-y)
1534;;;!! ;; begin-column
1535;;;!! ;; (- end-column begin-column) 1))))))))))
1536;;;!! ;;
1537;;;!! ;;(defun mouse-erase-box ()
1538;;;!! ;; (interactive)
1539;;;!! ;; (if last-line-drawn
1540;;;!! ;; (progn
1541;;;!! ;; (x-erase-rectangle (selected-screen))
1542;;;!! ;; (setq last-line-drawn nil))))
1543;;;!!
1544;;;!! ;;; (defun test-x-rectangle ()
1545;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1546;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1547;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1548;;;!!
1549;;;!! ;;
1550;;;!! ;; Here is how to do double clicking in lisp. About to change.
1551;;;!! ;;
1552;;;!!
1553;;;!! (defvar double-start nil)
1554;;;!! (defconst double-click-interval 300
1555;;;!! "Max ticks between clicks")
1556;;;!!
1557;;;!! (defun double-down (event)
1558;;;!! (interactive "@e")
1559;;;!! (if double-start
1560;;;!! (let ((interval (- (nth 4 event) double-start)))
1561;;;!! (if (< interval double-click-interval)
1562;;;!! (progn
1563;;;!! (backward-up-list 1)
1564;;;!! ;; (message "Interval %d" interval)
1565;;;!! (sleep-for 1)))
1566;;;!! (setq double-start nil))
1567;;;!! (setq double-start (nth 4 event))))
1568;;;!!
1569;;;!! (defun double-up (event)
1570;;;!! (interactive "@e")
1571;;;!! (and double-start
1572;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1573;;;!! (setq double-start nil)))
1574;;;!!
1575;;;!! ;;; (defun x-test-doubleclick ()
1576;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1577;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1578;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1579;;;!!
1580;;;!! ;;
5ba2dc3f 1581;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
dbc4e1c1
JB
1582;;;!! ;;
1583;;;!!
1584;;;!! (defvar scrolled-lines 0)
1585;;;!! (defconst scroll-speed 1)
1586;;;!!
1587;;;!! (defun incr-scroll-down (event)
1588;;;!! (interactive "@e")
1589;;;!! (setq scrolled-lines 0)
1590;;;!! (incremental-scroll scroll-speed))
1591;;;!!
1592;;;!! (defun incr-scroll-up (event)
1593;;;!! (interactive "@e")
1594;;;!! (setq scrolled-lines 0)
1595;;;!! (incremental-scroll (- scroll-speed)))
1596;;;!!
1597;;;!! (defun incremental-scroll (n)
1598;;;!! (while (= (x-mouse-events) 0)
1599;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1600;;;!! (scroll-down n)
1601;;;!! (sit-for 300 t)))
1602;;;!!
1603;;;!! (defun incr-scroll-stop (event)
1604;;;!! (interactive "@e")
1605;;;!! (message "Scrolled %d lines" scrolled-lines)
1606;;;!! (setq scrolled-lines 0)
1607;;;!! (sleep-for 1))
1608;;;!!
1609;;;!! ;;; (defun x-testing-scroll ()
1610;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1611;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1612;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1613;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1614;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1615;;;!!
1616;;;!! ;;
1617;;;!! ;; Some playthings suitable for picture mode? They need work.
1618;;;!! ;;
1619;;;!!
1620;;;!! (defun mouse-kill-rectangle (event)
1621;;;!! "Kill the rectangle between point and the mouse cursor."
1622;;;!! (interactive "@e")
1623;;;!! (let ((point-save (point)))
1624;;;!! (save-excursion
1625;;;!! (mouse-set-point event)
1626;;;!! (push-mark nil t)
1627;;;!! (if (> point-save (point))
1628;;;!! (kill-rectangle (point) point-save)
1629;;;!! (kill-rectangle point-save (point))))))
1630;;;!!
1631;;;!! (defun mouse-open-rectangle (event)
1632;;;!! "Kill the rectangle between point and the mouse cursor."
1633;;;!! (interactive "@e")
1634;;;!! (let ((point-save (point)))
1635;;;!! (save-excursion
1636;;;!! (mouse-set-point event)
1637;;;!! (push-mark nil t)
1638;;;!! (if (> point-save (point))
1639;;;!! (open-rectangle (point) point-save)
1640;;;!! (open-rectangle point-save (point))))))
1641;;;!!
1642;;;!! ;; Must be a better way to do this.
1643;;;!!
1644;;;!! (defun mouse-multiple-insert (n char)
1645;;;!! (while (> n 0)
1646;;;!! (insert char)
1647;;;!! (setq n (1- n))))
1648;;;!!
1649;;;!! ;; What this could do is not finalize until button was released.
1650;;;!!
1651;;;!! (defun mouse-move-text (event)
1652;;;!! "Move text from point to cursor position, inserting spaces."
1653;;;!! (interactive "@e")
1654;;;!! (let* ((relative-coordinate
1655;;;!! (coordinates-in-window-p (car event) (selected-window))))
1656;;;!! (if (consp relative-coordinate)
1657;;;!! (cond ((> (current-column) (car relative-coordinate))
1658;;;!! (delete-char
1659;;;!! (- (car relative-coordinate) (current-column))))
1660;;;!! ((< (current-column) (car relative-coordinate))
1661;;;!! (mouse-multiple-insert
1662;;;!! (- (car relative-coordinate) (current-column)) " "))
1663;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
07a78410 1664\f
f936ae06
RS
1665;; Choose a completion with the mouse.
1666
1667(defun mouse-choose-completion (event)
49e61c42 1668 "Click on an alternative in the `*Completions*' buffer to choose it."
f936ae06 1669 (interactive "e")
33c448cd
RS
1670 ;; Give temporary modes such as isearch a chance to turn off.
1671 (run-hooks 'mouse-leave-buffer-hook)
d89a4a47 1672 (let ((buffer (window-buffer))
02680e9b
RS
1673 choice
1674 base-size)
f936ae06
RS
1675 (save-excursion
1676 (set-buffer (window-buffer (posn-window (event-start event))))
f36f4e9e
RS
1677 (if completion-reference-buffer
1678 (setq buffer completion-reference-buffer))
02680e9b 1679 (setq base-size completion-base-size)
f936ae06
RS
1680 (save-excursion
1681 (goto-char (posn-point (event-start event)))
b6be5d95 1682 (let (beg end)
9a43a594
RS
1683 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1684 (setq end (point) beg (1+ (point))))
1685 (if (null beg)
1686 (error "No completion here"))
1687 (setq beg (previous-single-property-change beg 'mouse-face))
2f5ed2e8
RS
1688 (setq end (or (next-single-property-change end 'mouse-face)
1689 (point-max)))
b6be5d95 1690 (setq choice (buffer-substring beg end)))))
73e2025f
RS
1691 (let ((owindow (selected-window)))
1692 (select-window (posn-window (event-start event)))
874a2cbd
RS
1693 (if (and (one-window-p t 'selected-frame)
1694 (window-dedicated-p (selected-window)))
1695 ;; This is a special buffer's frame
1696 (iconify-frame (selected-frame))
1697 (or (window-dedicated-p (selected-window))
1698 (bury-buffer)))
73e2025f 1699 (select-window owindow))
02680e9b 1700 (choose-completion-string choice buffer base-size)))
f936ae06 1701\f
07a78410
RS
1702;; Font selection.
1703
0eb9fef3
RS
1704(defun font-menu-add-default ()
1705 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1706 (font-alist x-fixed-font-alist)
0d94f5ca 1707 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
0eb9fef3
RS
1708 (if (assoc "Default" elt)
1709 (delete (assoc "Default" elt) elt))
1710 (setcdr elt
3d64d15b 1711 (cons (list "Default"
0eb9fef3
RS
1712 (cdr (assq 'font (frame-parameters (selected-frame)))))
1713 (cdr elt)))))
1714
07a78410
RS
1715(defvar x-fixed-font-alist
1716 '("Font menu"
1717 ("Misc"
19d973e8
RS
1718 ;; For these, we specify the pixel height and width.
1719 ("fixed" "fixed")
1720 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1721 ("6x12"
1722 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1723 ("6x13"
1724 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1725 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1726 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1727 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1728 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1729 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1730 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1731 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
fa21fdec 1732 ("")
f29c3695
RS
1733 ("clean 5x8"
1734 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1735 ("clean 6x8"
1736 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
19d973e8
RS
1737 ("clean 8x8"
1738 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1739 ("clean 8x10"
1740 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1741 ("clean 8x14"
1742 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1743 ("clean 8x16"
1744 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
fa21fdec 1745 ("")
19d973e8 1746 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
07a78410
RS
1747;;; We don't seem to have these; who knows what they are.
1748;;; ("fg-18" "fg-18")
1749;;; ("fg-25" "fg-25")
1750;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1751;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1752;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1753;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1754;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1755 ("Courier"
19d973e8 1756 ;; For these, we specify the point height.
82c048a9
RS
1757 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1758 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1759 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1760 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1761 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1762 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1763 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1764 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1765 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1766 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1767 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1768 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1769 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1770 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1771 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1772 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1773 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1774 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1775 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1776 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1777 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1778 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1779 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1780 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
07a78410
RS
1781 )
1782 "X fonts suitable for use in Emacs.")
1783
1900a92b 1784(defun mouse-set-font (&rest fonts)
07a78410
RS
1785 "Select an emacs font from a list of known good fonts"
1786 (interactive
1787 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
df4de8c6
KH
1788 (if fonts
1789 (let (font)
1790 (while fonts
1791 (condition-case nil
1792 (progn
3fadec1a 1793 (set-default-font (car fonts))
df4de8c6
KH
1794 (setq font (car fonts))
1795 (setq fonts nil))
3fadec1a
RS
1796 (error
1797 (setq fonts (cdr fonts)))))
df4de8c6 1798 (if (null font)
3fadec1a 1799 (error "Font not found")))))
cc0a8174
JB
1800\f
1801;;; Bindings for mouse commands.
1802
fcfc3c63 1803(define-key global-map [down-mouse-1] 'mouse-drag-region)
dbc4e1c1 1804(global-set-key [mouse-1] 'mouse-set-point)
dbc4e1c1 1805(global-set-key [drag-mouse-1] 'mouse-set-region)
fcfc3c63 1806
e37de120
RS
1807;; These are tested for in mouse-drag-region.
1808(global-set-key [double-mouse-1] 'mouse-set-point)
1809(global-set-key [triple-mouse-1] 'mouse-set-point)
1810
dbc4e1c1
JB
1811(global-set-key [mouse-2] 'mouse-yank-at-click)
1812(global-set-key [mouse-3] 'mouse-save-then-kill)
8b34e79d 1813
dbc4e1c1
JB
1814;; By binding these to down-going events, we let the user use the up-going
1815;; event to make the selection, saving a click.
08a1c178
RS
1816(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1817(if (not (eq system-type 'ms-dos))
1818 (global-set-key [S-down-mouse-1] 'mouse-set-font))
eef805d7 1819;; C-down-mouse-2 is bound in facemenu.el.
95132d1c
RS
1820(global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
1821
07a78410 1822
8b34e79d
RS
1823;; Replaced with dragging mouse-1
1824;; (global-set-key [S-mouse-1] 'mouse-set-mark)
947da0c4 1825
3c2dd2c0 1826(global-set-key [mode-line mouse-1] 'mouse-select-window)
544e7e73
RS
1827(global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1828(global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
3c2dd2c0 1829(global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
dbc4e1c1 1830(global-set-key [mode-line mouse-3] 'mouse-delete-window)
3c2dd2c0 1831(global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
9926ab64 1832(global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
b6522df6 1833(global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
08a1c178
RS
1834(global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1835(global-set-key [vertical-line mouse-1] 'mouse-select-window)
49116ac0
JB
1836
1837(provide 'mouse)
1838
6594deb0 1839;;; mouse.el ends here