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