Add item for something like Adobe Illustrator/Corel Draw.
[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
RS
734;; Momentarily show where the mark is, if highlighting doesn't show it.
735(defun mouse-show-mark ()
98aac09d 736 (if transient-mark-mode
c8f264ae 737 (if window-system
98aac09d
MB
738 (delete-overlay mouse-drag-overlay))
739 (if window-system
740 (let ((inhibit-quit t)
741 (echo-keystrokes 0)
742 event events)
743 (move-overlay mouse-drag-overlay (point) (mark t))
744 (while (progn (setq event (read-event))
745 (setq events (append events (list event)))
746 (and (memq 'down (event-modifiers event))
747 (not (key-binding (apply 'vector events)))
748 (not (mouse-undouble-last-event events)))))
749 (setq unread-command-events
750 (nconc events unread-command-events))
751 (setq quit-flag nil)
752 (delete-overlay mouse-drag-overlay))
753 (save-excursion
754 (goto-char (mark t))
755 (sit-for 1)))))
3f26b32a 756
cc0a8174
JB
757(defun mouse-set-mark (click)
758 "Set mark at the position clicked on with the mouse.
759Display cursor at that position for a second.
760This must be bound to a mouse click."
ec558adc 761 (interactive "e")
f598fb03
RS
762 (mouse-minibuffer-check click)
763 (select-window (posn-window (event-start click)))
764 ;; We don't use save-excursion because that preserves the mark too.
72ea54a4
RS
765 (let ((point-save (point)))
766 (unwind-protect
cc0a8174 767 (progn (mouse-set-point click)
897897e3
RS
768 (push-mark nil t t)
769 (or transient-mark-mode
770 (sit-for 1)))
72ea54a4
RS
771 (goto-char point-save))))
772
cc0a8174
JB
773(defun mouse-kill (click)
774 "Kill the region between point and the mouse click.
775The text is saved in the kill ring, as with \\[kill-region]."
ec558adc 776 (interactive "e")
d65147f6 777 (mouse-minibuffer-check click)
142c7672
KH
778 (let* ((posn (event-start click))
779 (click-posn (posn-point posn)))
780 (select-window (posn-window posn))
bd307392
JB
781 (if (numberp click-posn)
782 (kill-region (min (point) click-posn)
783 (max (point) click-posn)))))
72ea54a4 784
87ef29fd
JB
785(defun mouse-yank-at-click (click arg)
786 "Insert the last stretch of killed text at the position clicked on.
50f58001
RS
787Also move point to one end of the text thus inserted (normally the end).
788Prefix arguments are interpreted as with \\[yank].
789If `mouse-yank-at-point' is non-nil, insert at point
790regardless of where you click."
ec558adc 791 (interactive "e\nP")
33c448cd
RS
792 ;; Give temporary modes such as isearch a chance to turn off.
793 (run-hooks 'mouse-leave-buffer-hook)
50f58001 794 (or mouse-yank-at-point (mouse-set-point click))
d89a4a47 795 (setq this-command 'yank)
a814e9df 796 (setq mouse-selection-click-count 0)
87ef29fd
JB
797 (yank arg))
798
799(defun mouse-kill-ring-save (click)
cc0a8174
JB
800 "Copy the region between point and the mouse click in the kill ring.
801This does not delete the region; it acts like \\[kill-ring-save]."
ec558adc 802 (interactive "e")
3f26b32a 803 (mouse-set-mark-fast click)
6452d8a6
RS
804 (let (this-command last-command)
805 (kill-ring-save (point) (mark t)))
3f26b32a 806 (mouse-show-mark))
72ea54a4 807
dbc4e1c1
JB
808;;; This function used to delete the text between point and the mouse
809;;; whenever it was equal to the front of the kill ring, but some
810;;; people found that confusing.
811
812;;; A list (TEXT START END), describing the text and position of the last
813;;; invocation of mouse-save-then-kill.
814(defvar mouse-save-then-kill-posn nil)
815
26d280b9 816(defun mouse-save-then-kill-delete-region (beg end)
9a974c88
RS
817 ;; We must make our own undo boundaries
818 ;; because they happen automatically only for the current buffer.
819 (undo-boundary)
dd524dbd
RS
820 (if (or (= beg end) (eq buffer-undo-list t))
821 ;; If we have no undo list in this buffer,
822 ;; just delete.
823 (delete-region beg end)
824 ;; Delete, but make the undo-list entry share with the kill ring.
825 ;; First, delete just one char, so in case buffer is being modified
826 ;; for the first time, the undo list records that fact.
2f4b15ef
RS
827 (let (before-change-function after-change-function
828 before-change-functions after-change-functions)
829 (delete-region beg
830 (+ beg (if (> end beg) 1 -1))))
dd524dbd
RS
831 (let ((buffer-undo-list buffer-undo-list))
832 ;; Undo that deletion--but don't change the undo list!
2f4b15ef
RS
833 (let (before-change-function after-change-function
834 before-change-functions after-change-functions)
835 (primitive-undo 1 buffer-undo-list))
dd524dbd
RS
836 ;; Now delete the rest of the specified region,
837 ;; but don't record it.
838 (setq buffer-undo-list t)
9a974c88
RS
839 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
840 (error "Lossage in mouse-save-then-kill-delete-region"))
dd524dbd
RS
841 (delete-region beg end))
842 (let ((tail buffer-undo-list))
843 ;; Search back in buffer-undo-list for the string
844 ;; that came from deleting one character.
845 (while (and tail (not (stringp (car (car tail)))))
846 (setq tail (cdr tail)))
847 ;; Replace it with an entry for the entire deleted text.
848 (and tail
9a974c88
RS
849 (setcar tail (cons (car kill-ring) (min beg end))))))
850 (undo-boundary))
eb6ff46f 851
947da0c4 852(defun mouse-save-then-kill (click)
40a45a9f
RS
853 "Save text to point in kill ring; the second time, kill the text.
854If the text between point and the mouse is the same as what's
855at the front of the kill ring, this deletes the text.
856Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
eb6ff46f
RS
857which prepares for a second click to delete the text.
858
859If you have selected words or lines, this command extends the
860selection through the word or line clicked on. If you do this
861again in a different position, it extends the selection again.
862If you do this twice in the same position, the selection is killed."
947da0c4 863 (interactive "e")
b64548c7
RS
864 (let ((before-scroll point-before-scroll))
865 (mouse-minibuffer-check click)
866 (let ((click-posn (posn-point (event-start click)))
867 ;; Don't let a subsequent kill command append to this one:
868 ;; prevent setting this-command to kill-region.
869 (this-command this-command))
c8c5bd24
RS
870 (if (and (save-excursion
871 (set-buffer (window-buffer (posn-window (event-start click))))
872 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
873 ;; Don't be fooled by a recent click in some other buffer.
874 (eq mouse-selection-click-count-buffer
875 (current-buffer)))))
b64548c7
RS
876 (if (not (and (eq last-command 'mouse-save-then-kill)
877 (equal click-posn
878 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
879 ;; Find both ends of the object selected by this click.
880 (let* ((range
881 (mouse-start-end click-posn click-posn
882 mouse-selection-click-count)))
883 ;; Move whichever end is closer to the click.
884 ;; That's what xterm does, and it seems reasonable.
885 (if (< (abs (- click-posn (mark t)))
886 (abs (- click-posn (point))))
887 (set-mark (car range))
888 (goto-char (nth 1 range)))
889 ;; We have already put the old region in the kill ring.
890 ;; Replace it with the extended region.
891 ;; (It would be annoying to make a separate entry.)
892 (kill-new (buffer-substring (point) (mark t)) t)
e6c2f5d4 893 (mouse-set-region-1)
b64548c7
RS
894 ;; Arrange for a repeated mouse-3 to kill this region.
895 (setq mouse-save-then-kill-posn
896 (list (car kill-ring) (point) click-posn))
897 (mouse-show-mark))
898 ;; If we click this button again without moving it,
899 ;; that time kill.
bcde3748 900 (mouse-save-then-kill-delete-region (mark) (point))
b64548c7 901 (setq mouse-selection-click-count 0)
dd524dbd 902 (setq mouse-save-then-kill-posn nil))
b64548c7
RS
903 (if (and (eq last-command 'mouse-save-then-kill)
904 mouse-save-then-kill-posn
905 (eq (car mouse-save-then-kill-posn) (car kill-ring))
906 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
907 ;; If this is the second time we've called
908 ;; mouse-save-then-kill, delete the text from the buffer.
909 (progn
910 (mouse-save-then-kill-delete-region (point) (mark))
911 ;; After we kill, another click counts as "the first time".
912 (setq mouse-save-then-kill-posn nil))
c8f264ae
RS
913 ;; This is not a repetition.
914 ;; We are adjusting an old selection or creating a new one.
b64548c7
RS
915 (if (or (and (eq last-command 'mouse-save-then-kill)
916 mouse-save-then-kill-posn)
917 (and mark-active transient-mark-mode)
918 (and (memq last-command
919 '(mouse-drag-region mouse-set-region))
920 (or mark-even-if-inactive
921 (not transient-mark-mode))))
922 ;; We have a selection or suitable region, so adjust it.
923 (let* ((posn (event-start click))
924 (new (posn-point posn)))
925 (select-window (posn-window posn))
926 (if (numberp new)
927 (progn
928 ;; Move whichever end of the region is closer to the click.
929 ;; That is what xterm does, and it seems reasonable.
930 (if (< (abs (- new (point))) (abs (- new (mark t))))
931 (goto-char new)
932 (set-mark new))
933 (setq deactivate-mark nil)))
bcde3748
RS
934 (kill-new (buffer-substring (point) (mark t)) t)
935 (mouse-show-mark))
b64548c7
RS
936 ;; Set the mark where point is, then move where clicked.
937 (mouse-set-mark-fast click)
938 (if before-scroll
939 (goto-char before-scroll))
940 (exchange-point-and-mark)
c8f264ae
RS
941 (kill-new (buffer-substring (point) (mark t)))
942 (if window-system
943 (mouse-show-mark)))
e6c2f5d4 944 (mouse-set-region-1)
b64548c7
RS
945 (setq mouse-save-then-kill-posn
946 (list (car kill-ring) (point) click-posn)))))))
e66feb07
RS
947\f
948(global-set-key [M-mouse-1] 'mouse-start-secondary)
949(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
950(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
951(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
9a974c88 952(global-set-key [M-mouse-2] 'mouse-yank-secondary)
e66feb07
RS
953
954;; An overlay which records the current secondary selection
955;; or else is deleted when there is no secondary selection.
956;; May be nil.
957(defvar mouse-secondary-overlay nil)
958
a94c7fcc
RS
959(defvar mouse-secondary-click-count 0)
960
e66feb07
RS
961;; A marker which records the specified first end for a secondary selection.
962;; May be nil.
963(defvar mouse-secondary-start nil)
964
965(defun mouse-start-secondary (click)
966 "Set one end of the secondary selection to the position clicked on.
967Use \\[mouse-secondary-save-then-kill] to set the other end
968and complete the secondary selection."
969 (interactive "e")
d65147f6 970 (mouse-minibuffer-check click)
e66feb07 971 (let ((posn (event-start click)))
230aaa73
RS
972 (save-excursion
973 (set-buffer (window-buffer (posn-window posn)))
974 ;; Cancel any preexisting secondary selection.
975 (if mouse-secondary-overlay
976 (delete-overlay mouse-secondary-overlay))
977 (if (numberp (posn-point posn))
978 (progn
979 (or mouse-secondary-start
980 (setq mouse-secondary-start (make-marker)))
981 (move-marker mouse-secondary-start (posn-point posn)))))))
e66feb07
RS
982
983(defun mouse-set-secondary (click)
984 "Set the secondary selection to the text that the mouse is dragged over.
985This must be bound to a mouse drag event."
986 (interactive "e")
d65147f6 987 (mouse-minibuffer-check click)
e66feb07
RS
988 (let ((posn (event-start click))
989 beg
990 (end (event-end click)))
230aaa73
RS
991 (save-excursion
992 (set-buffer (window-buffer (posn-window posn)))
993 (if (numberp (posn-point posn))
994 (setq beg (posn-point posn)))
995 (if mouse-secondary-overlay
996 (move-overlay mouse-secondary-overlay beg (posn-point end))
997 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
998 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
947da0c4 999
d89a4a47 1000(defun mouse-drag-secondary (start-event)
e66feb07 1001 "Set the secondary selection to the text that the mouse is dragged over.
d89a4a47 1002Highlight the drag area as you move the mouse.
e66feb07
RS
1003This must be bound to a button-down mouse event."
1004 (interactive "e")
d65147f6 1005 (mouse-minibuffer-check start-event)
4c9afcbe
RS
1006 (let* ((echo-keystrokes 0)
1007 (start-posn (event-start start-event))
d89a4a47
RS
1008 (start-point (posn-point start-posn))
1009 (start-window (posn-window start-posn))
1010 (start-frame (window-frame start-window))
1011 (bounds (window-edges start-window))
1012 (top (nth 1 bounds))
1013 (bottom (if (window-minibuffer-p start-window)
1014 (nth 3 bounds)
1015 ;; Don't count the mode line.
1016 (1- (nth 3 bounds))))
1017 (click-count (1- (event-click-count start-event))))
1018 (save-excursion
1019 (set-buffer (window-buffer start-window))
a94c7fcc 1020 (setq mouse-secondary-click-count click-count)
d89a4a47
RS
1021 (or mouse-secondary-overlay
1022 (setq mouse-secondary-overlay
1023 (make-overlay (point) (point))))
26d280b9 1024 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
9a974c88 1025 (if (> (mod click-count 3) 0)
26d280b9
RS
1026 ;; Double or triple press: make an initial selection
1027 ;; of one word or line.
d89a4a47
RS
1028 (let ((range (mouse-start-end start-point start-point click-count)))
1029 (set-marker mouse-secondary-start nil)
1030 (move-overlay mouse-secondary-overlay 1 1
1031 (window-buffer start-window))
1032 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1033 (window-buffer start-window)))
26d280b9 1034 ;; Single-press: cancel any preexisting secondary selection.
d89a4a47
RS
1035 (or mouse-secondary-start
1036 (setq mouse-secondary-start (make-marker)))
1037 (set-marker mouse-secondary-start start-point)
1038 (delete-overlay mouse-secondary-overlay))
1039 (let (event end end-point)
1040 (track-mouse
1041 (while (progn
1042 (setq event (read-event))
1043 (or (mouse-movement-p event)
1044 (eq (car-safe event) 'switch-frame)))
1045
1046 (if (eq (car-safe event) 'switch-frame)
1047 nil
1048 (setq end (event-end event)
1049 end-point (posn-point end))
1050 (cond
d89a4a47
RS
1051 ;; Are we moving within the original window?
1052 ((and (eq (posn-window end) start-window)
1053 (integer-or-marker-p end-point))
d89a4a47
RS
1054 (let ((range (mouse-start-end start-point end-point
1055 click-count)))
0136e1e3
RS
1056 (if (or (/= start-point end-point)
1057 (null (marker-position mouse-secondary-start)))
1058 (progn
1059 (set-marker mouse-secondary-start nil)
1060 (move-overlay mouse-secondary-overlay
1061 (car range) (nth 1 range))))))
3b0aebe9
RS
1062 (t
1063 (let ((mouse-row (cdr (cdr (mouse-position)))))
1064 (cond
1065 ((null mouse-row))
1066 ((< mouse-row top)
e919a622
RS
1067 (mouse-scroll-subr start-window (- mouse-row top)
1068 mouse-secondary-overlay start-point))
d2287ded 1069 ((>= mouse-row bottom)
e919a622 1070 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
3b0aebe9 1071 mouse-secondary-overlay start-point)))))))))
d89a4a47 1072
4e399a53
RS
1073 (if (consp event)
1074;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
1075;;; (eq (posn-window (event-end event)) start-window)
1076;;; (numberp (posn-point (event-end event)))
d89a4a47
RS
1077 (if (marker-position mouse-secondary-start)
1078 (save-window-excursion
1079 (delete-overlay mouse-secondary-overlay)
9a974c88 1080 (x-set-selection 'SECONDARY nil)
d89a4a47
RS
1081 (select-window start-window)
1082 (save-excursion
1083 (goto-char mouse-secondary-start)
1084 (sit-for 1)))
9a974c88
RS
1085 (x-set-selection
1086 'SECONDARY
1087 (buffer-substring (overlay-start mouse-secondary-overlay)
1088 (overlay-end mouse-secondary-overlay)))))))))
e66feb07 1089
9a974c88 1090(defun mouse-yank-secondary (click)
50f58001
RS
1091 "Insert the secondary selection at the position clicked on.
1092Move point to the end of the inserted text.
1093If `mouse-yank-at-point' is non-nil, insert at point
1094regardless of where you click."
9a974c88 1095 (interactive "e")
33c448cd
RS
1096 ;; Give temporary modes such as isearch a chance to turn off.
1097 (run-hooks 'mouse-leave-buffer-hook)
50f58001
RS
1098 (or mouse-yank-at-point (mouse-set-point click))
1099 (insert (x-get-selection 'SECONDARY)))
9a974c88 1100
7e6404f6 1101(defun mouse-kill-secondary ()
9a974c88
RS
1102 "Kill the text in the secondary selection.
1103This is intended more as a keyboard command than as a mouse command
1104but it can work as either one.
1105
1106The current buffer (in case of keyboard use), or the buffer clicked on,
1107must be the one that the secondary selection is in. This requirement
1108is to prevent accidents."
7e6404f6
RS
1109 (interactive)
1110 (let* ((keys (this-command-keys))
1111 (click (elt keys (1- (length keys)))))
1112 (or (eq (overlay-buffer mouse-secondary-overlay)
1113 (if (listp click)
1114 (window-buffer (posn-window (event-start click)))
1115 (current-buffer)))
1116 (error "Select or click on the buffer where the secondary selection is")))
9dfab550
RS
1117 (let (this-command)
1118 (save-excursion
1119 (set-buffer (overlay-buffer mouse-secondary-overlay))
1120 (kill-region (overlay-start mouse-secondary-overlay)
1121 (overlay-end mouse-secondary-overlay))))
e66feb07 1122 (delete-overlay mouse-secondary-overlay)
9dfab550 1123;;; (x-set-selection 'SECONDARY nil)
e66feb07
RS
1124 (setq mouse-secondary-overlay nil))
1125
1126(defun mouse-secondary-save-then-kill (click)
d89a4a47 1127 "Save text to point in kill ring; the second time, kill the text.
7bbe2cc7
RS
1128You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1129If the text between where you did \\[mouse-start-secondary] and where
1130you use this command matches the text at the front of the kill ring,
1131this command deletes the text.
e66feb07 1132Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
7bbe2cc7 1133which prepares for a second click with this command to delete the text.
d89a4a47 1134
7bbe2cc7
RS
1135If you have already made a secondary selection in that buffer,
1136this command extends or retracts the selection to where you click.
1137If you do this again in a different position, it extends or retracts
1138again. If you do this twice in the same position, it kills the selection."
e66feb07 1139 (interactive "e")
d65147f6 1140 (mouse-minibuffer-check click)
d89a4a47
RS
1141 (let ((posn (event-start click))
1142 (click-posn (posn-point (event-start click)))
e66feb07
RS
1143 ;; Don't let a subsequent kill command append to this one:
1144 ;; prevent setting this-command to kill-region.
1145 (this-command this-command))
9a974c88
RS
1146 (or (eq (window-buffer (posn-window posn))
1147 (or (and mouse-secondary-overlay
1148 (overlay-buffer mouse-secondary-overlay))
1149 (if mouse-secondary-start
1150 (marker-buffer mouse-secondary-start))))
1151 (error "Wrong buffer"))
1152 (save-excursion
1153 (set-buffer (window-buffer (posn-window posn)))
a94c7fcc 1154 (if (> (mod mouse-secondary-click-count 3) 0)
9a974c88
RS
1155 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1156 (equal click-posn
1157 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1158 ;; Find both ends of the object selected by this click.
1159 (let* ((range
1160 (mouse-start-end click-posn click-posn
a94c7fcc 1161 mouse-secondary-click-count)))
9a974c88
RS
1162 ;; Move whichever end is closer to the click.
1163 ;; That's what xterm does, and it seems reasonable.
1164 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1165 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1166 (move-overlay mouse-secondary-overlay (car range)
1167 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1168 (move-overlay mouse-secondary-overlay
1169 (overlay-start mouse-secondary-overlay)
1170 (nth 1 range)))
9a974c88
RS
1171 ;; We have already put the old region in the kill ring.
1172 ;; Replace it with the extended region.
1173 ;; (It would be annoying to make a separate entry.)
f69140fd
KH
1174 (kill-new (buffer-substring
1175 (overlay-start mouse-secondary-overlay)
1176 (overlay-end mouse-secondary-overlay)) t)
9a974c88
RS
1177 ;; Arrange for a repeated mouse-3 to kill this region.
1178 (setq mouse-save-then-kill-posn
1179 (list (car kill-ring) (point) click-posn)))
1180 ;; If we click this button again without moving it,
1181 ;; that time kill.
d89a4a47 1182 (progn
9a974c88
RS
1183 (mouse-save-then-kill-delete-region
1184 (overlay-start mouse-secondary-overlay)
1185 (overlay-end mouse-secondary-overlay))
1186 (setq mouse-save-then-kill-posn nil)
a94c7fcc 1187 (setq mouse-secondary-click-count 0)
9a974c88
RS
1188 (delete-overlay mouse-secondary-overlay)))
1189 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1190 mouse-save-then-kill-posn
1191 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1192 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1193 ;; If this is the second time we've called
1194 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1195 (progn
1196 (mouse-save-then-kill-delete-region
1197 (overlay-start mouse-secondary-overlay)
1198 (overlay-end mouse-secondary-overlay))
1199 (setq mouse-save-then-kill-posn nil)
1200 (delete-overlay mouse-secondary-overlay))
1201 (if (overlay-start mouse-secondary-overlay)
1202 ;; We have a selection, so adjust it.
1203 (progn
1204 (if (numberp click-posn)
1205 (progn
1206 ;; Move whichever end of the region is closer to the click.
1207 ;; That is what xterm does, and it seems reasonable.
1208 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1209 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1210 (move-overlay mouse-secondary-overlay click-posn
1211 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1212 (move-overlay mouse-secondary-overlay
1213 (overlay-start mouse-secondary-overlay)
1214 click-posn))
9a974c88 1215 (setq deactivate-mark nil)))
0136e1e3 1216 (if (eq last-command 'mouse-secondary-save-then-kill)
f69140fd
KH
1217 ;; If the front of the kill ring comes from
1218 ;; an immediately previous use of this command,
1219 ;; replace it with the extended region.
1220 ;; (It would be annoying to make a separate entry.)
1221 (kill-new (buffer-substring
0136e1e3 1222 (overlay-start mouse-secondary-overlay)
f69140fd 1223 (overlay-end mouse-secondary-overlay)) t)
0136e1e3
RS
1224 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1225 (overlay-end mouse-secondary-overlay))))
9a974c88
RS
1226 (if mouse-secondary-start
1227 ;; All we have is one end of a selection,
1228 ;; so put the other end here.
1229 (let ((start (+ 0 mouse-secondary-start)))
1230 (kill-ring-save start click-posn)
1231 (if mouse-secondary-overlay
1232 (move-overlay mouse-secondary-overlay start click-posn)
1233 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1234 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1235 (setq mouse-save-then-kill-posn
1236 (list (car kill-ring) (point) click-posn))))
fb28fd5b
RS
1237 (if (overlay-buffer mouse-secondary-overlay)
1238 (x-set-selection 'SECONDARY
9a974c88
RS
1239 (buffer-substring
1240 (overlay-start mouse-secondary-overlay)
1241 (overlay-end mouse-secondary-overlay)))))))
e66feb07 1242\f
f1107960
RS
1243(defvar mouse-menu-buffer-maxlen 20
1244 "*Number of buffers in one pane (submenu) of the buffer menu.
1245If we have lots of buffers, divide them into groups of
1246`mouse-menu-buffer-maxlen' and make a pane (or submenu) for each one.")
1247
8b34e79d 1248(defun mouse-buffer-menu (event)
2d82f7b9
RS
1249 "Pop up a menu of buffers for selection with the mouse.
1250This switches buffers in the window that you clicked on,
1251and selects that window."
ec558adc 1252 (interactive "e")
d65147f6 1253 (mouse-minibuffer-check event)
12dcaa7c
RS
1254 (let* ((buffers
1255 ;; Make an alist of (MENU-ITEM . BUFFER).
1256 (let ((tail (buffer-list))
1257 (maxlen 0)
1258 head)
1259 (while tail
1260 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1261 (setq maxlen
1262 (max maxlen
1263 (length (buffer-name (car tail))))))
1264 (setq tail (cdr tail)))
1265 (setq tail (buffer-list))
1266 (while tail
1267 (let ((elt (car tail)))
bca55f26 1268 (if (/= (aref (buffer-name elt) 0) ?\ )
12dcaa7c
RS
1269 (setq head
1270 (cons
1271 (cons
1272 (format
1273 (format "%%%ds %%s%%s %%s" maxlen)
1274 (buffer-name elt)
1275 (if (buffer-modified-p elt) "*" " ")
1276 (save-excursion
1277 (set-buffer elt)
1278 (if buffer-read-only "%" " "))
1279 (or (buffer-file-name elt)
1280 (save-excursion
1281 (set-buffer elt)
1282 (if list-buffers-directory
1283 (expand-file-name
1284 list-buffers-directory)))
1285 ""))
1286 elt)
1287 head))))
1288 (setq tail (cdr tail)))
a49ea24c
RS
1289 ;; Compensate for the reversal that the above loop does.
1290 (nreverse head)))
12dcaa7c
RS
1291 (menu
1292 ;; If we have lots of buffers, divide them into groups of 20
1293 ;; and make a pane (or submenu) for each one.
f1107960 1294 (if (> (length buffers) (/ (* mouse-menu-buffer-maxlen 3) 2))
a49ea24c 1295 (let ((buffers buffers) sublists next
12dcaa7c
RS
1296 (i 1))
1297 (while buffers
f1107960 1298 ;; Pull off the next mouse-menu-buffer-maxlen buffers
12dcaa7c 1299 ;; and make them the next element of sublist.
f1107960 1300 (setq next (nthcdr mouse-menu-buffer-maxlen buffers))
12dcaa7c 1301 (if next
f1107960
RS
1302 (setcdr (nthcdr (1- mouse-menu-buffer-maxlen) buffers)
1303 nil))
12dcaa7c
RS
1304 (setq sublists (cons (cons (format "Buffers %d" i) buffers)
1305 sublists))
1306 (setq i (1+ i))
1307 (setq buffers next))
1308 (cons "Buffer Menu" (nreverse sublists)))
1309 ;; Few buffers--put them all in one pane.
1310 (list "Buffer Menu" (cons "Select Buffer" buffers)))))
2d82f7b9
RS
1311 (let ((buf (x-popup-menu event menu))
1312 (window (posn-window (event-start event))))
1313 (if buf
1314 (progn
c0bb9f3b 1315 (or (framep window) (select-window window))
2d82f7b9 1316 (switch-to-buffer buf))))))
72ea54a4 1317\f
5ba2dc3f 1318;;; These need to be rewritten for the new scroll bar implementation.
dbc4e1c1
JB
1319
1320;;;!! ;; Commands for the scroll bar.
1321;;;!!
1322;;;!! (defun mouse-scroll-down (click)
1323;;;!! (interactive "@e")
1324;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1325;;;!!
1326;;;!! (defun mouse-scroll-up (click)
1327;;;!! (interactive "@e")
1328;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1329;;;!!
1330;;;!! (defun mouse-scroll-down-full ()
1331;;;!! (interactive "@")
1332;;;!! (scroll-down nil))
1333;;;!!
1334;;;!! (defun mouse-scroll-up-full ()
1335;;;!! (interactive "@")
1336;;;!! (scroll-up nil))
1337;;;!!
1338;;;!! (defun mouse-scroll-move-cursor (click)
1339;;;!! (interactive "@e")
1340;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1341;;;!!
1342;;;!! (defun mouse-scroll-absolute (event)
1343;;;!! (interactive "@e")
1344;;;!! (let* ((pos (car event))
1345;;;!! (position (car pos))
1346;;;!! (length (car (cdr pos))))
1347;;;!! (if (<= length 0) (setq length 1))
1348;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1349;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1350;;;!! position)
1351;;;!! length)
1352;;;!! scale-factor)))
1353;;;!! (goto-char newpos)
1354;;;!! (recenter '(4)))))
1355;;;!!
1356;;;!! (defun mouse-scroll-left (click)
1357;;;!! (interactive "@e")
1358;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1359;;;!!
1360;;;!! (defun mouse-scroll-right (click)
1361;;;!! (interactive "@e")
1362;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1363;;;!!
1364;;;!! (defun mouse-scroll-left-full ()
1365;;;!! (interactive "@")
1366;;;!! (scroll-left nil))
1367;;;!!
1368;;;!! (defun mouse-scroll-right-full ()
1369;;;!! (interactive "@")
1370;;;!! (scroll-right nil))
1371;;;!!
1372;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1373;;;!! (interactive "@e")
1374;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1375;;;!!
1376;;;!! (defun mouse-scroll-absolute-horizontally (event)
1377;;;!! (interactive "@e")
1378;;;!! (let* ((pos (car event))
1379;;;!! (position (car pos))
1380;;;!! (length (car (cdr pos))))
1381;;;!! (set-window-hscroll (selected-window) 33)))
1382;;;!!
1383;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1384;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1385;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1386;;;!!
1387;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1388;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1389;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1390;;;!!
1391;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1392;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1393;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1394;;;!!
1395;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1396;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1397;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1398;;;!!
1399;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1400;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1401;;;!! 'mouse-scroll-absolute-horizontally)
1402;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1403;;;!!
1404;;;!! (global-set-key [horizontal-slider mouse-1]
1405;;;!! 'mouse-scroll-move-cursor-horizontally)
1406;;;!! (global-set-key [horizontal-slider mouse-2]
1407;;;!! 'mouse-scroll-move-cursor-horizontally)
1408;;;!! (global-set-key [horizontal-slider mouse-3]
1409;;;!! 'mouse-scroll-move-cursor-horizontally)
1410;;;!!
1411;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1412;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1413;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1414;;;!!
1415;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1416;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1417;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1418;;;!!
1419;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1420;;;!! 'mouse-split-window-horizontally)
1421;;;!! (global-set-key [mode-line S-mouse-2]
1422;;;!! 'mouse-split-window-horizontally)
1423;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1424;;;!! 'mouse-split-window)
6b2154de 1425\f
dbc4e1c1
JB
1426;;;!! ;;;;
1427;;;!! ;;;; Here are experimental things being tested. Mouse events
1428;;;!! ;;;; are of the form:
1429;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1430;;;!! ;;
1431;;;!! ;;;;
1432;;;!! ;;;; Dynamically track mouse coordinates
1433;;;!! ;;;;
1434;;;!! ;;
1435;;;!! ;;(defun track-mouse (event)
1436;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1437;;;!! ;; (interactive "@e")
1438;;;!! ;; (while mouse-grabbed
1439;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1440;;;!! ;; (abs-x (car pos))
1441;;;!! ;; (abs-y (cdr pos))
1442;;;!! ;; (relative-coordinate (coordinates-in-window-p
1443;;;!! ;; (list (car pos) (cdr pos))
1444;;;!! ;; (selected-window))))
1445;;;!! ;; (if (consp relative-coordinate)
1446;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1447;;;!! ;; (car relative-coordinate)
1448;;;!! ;; (car (cdr relative-coordinate)))
1449;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1450;;;!!
1451;;;!! ;;
1452;;;!! ;; Dynamically put a box around the line indicated by point
1453;;;!! ;;
1454;;;!! ;;
1455;;;!! ;;(require 'backquote)
1456;;;!! ;;
1457;;;!! ;;(defun mouse-select-buffer-line (event)
1458;;;!! ;; (interactive "@e")
1459;;;!! ;; (let ((relative-coordinate
1460;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1461;;;!! ;; (abs-y (car (cdr (car event)))))
1462;;;!! ;; (if (consp relative-coordinate)
1463;;;!! ;; (progn
1464;;;!! ;; (save-excursion
1465;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1466;;;!! ;; (x-draw-rectangle
1467;;;!! ;; (selected-screen)
1468;;;!! ;; abs-y 0
1469;;;!! ;; (save-excursion
1470;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1471;;;!! ;; (end-of-line)
1472;;;!! ;; (push-mark nil t)
1473;;;!! ;; (beginning-of-line)
1474;;;!! ;; (- (region-end) (region-beginning))) 1))
1475;;;!! ;; (sit-for 1)
1476;;;!! ;; (x-erase-rectangle (selected-screen))))))
1477;;;!! ;;
1478;;;!! ;;(defvar last-line-drawn nil)
1479;;;!! ;;(defvar begin-delim "[^ \t]")
1480;;;!! ;;(defvar end-delim "[^ \t]")
1481;;;!! ;;
1482;;;!! ;;(defun mouse-boxing (event)
1483;;;!! ;; (interactive "@e")
1484;;;!! ;; (save-excursion
1485;;;!! ;; (let ((screen (selected-screen)))
1486;;;!! ;; (while (= (x-mouse-events) 0)
1487;;;!! ;; (let* ((pos (read-mouse-position screen))
1488;;;!! ;; (abs-x (car pos))
1489;;;!! ;; (abs-y (cdr pos))
1490;;;!! ;; (relative-coordinate
1491;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1492;;;!! ;; (selected-window)))
1493;;;!! ;; (begin-reg nil)
1494;;;!! ;; (end-reg nil)
1495;;;!! ;; (end-column nil)
1496;;;!! ;; (begin-column nil))
1497;;;!! ;; (if (and (consp relative-coordinate)
1498;;;!! ;; (or (not last-line-drawn)
1499;;;!! ;; (not (= last-line-drawn abs-y))))
1500;;;!! ;; (progn
1501;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1502;;;!! ;; (if (= (following-char) 10)
1503;;;!! ;; ()
1504;;;!! ;; (progn
1505;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1506;;;!! ;; (setq begin-column (1- (current-column)))
1507;;;!! ;; (end-of-line)
1508;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1509;;;!! ;; (setq end-column (1+ (current-column)))
1510;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1511;;;!! ;; (x-draw-rectangle screen
1512;;;!! ;; (setq last-line-drawn abs-y)
1513;;;!! ;; begin-column
1514;;;!! ;; (- end-column begin-column) 1))))))))))
1515;;;!! ;;
1516;;;!! ;;(defun mouse-erase-box ()
1517;;;!! ;; (interactive)
1518;;;!! ;; (if last-line-drawn
1519;;;!! ;; (progn
1520;;;!! ;; (x-erase-rectangle (selected-screen))
1521;;;!! ;; (setq last-line-drawn nil))))
1522;;;!!
1523;;;!! ;;; (defun test-x-rectangle ()
1524;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1525;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1526;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1527;;;!!
1528;;;!! ;;
1529;;;!! ;; Here is how to do double clicking in lisp. About to change.
1530;;;!! ;;
1531;;;!!
1532;;;!! (defvar double-start nil)
1533;;;!! (defconst double-click-interval 300
1534;;;!! "Max ticks between clicks")
1535;;;!!
1536;;;!! (defun double-down (event)
1537;;;!! (interactive "@e")
1538;;;!! (if double-start
1539;;;!! (let ((interval (- (nth 4 event) double-start)))
1540;;;!! (if (< interval double-click-interval)
1541;;;!! (progn
1542;;;!! (backward-up-list 1)
1543;;;!! ;; (message "Interval %d" interval)
1544;;;!! (sleep-for 1)))
1545;;;!! (setq double-start nil))
1546;;;!! (setq double-start (nth 4 event))))
1547;;;!!
1548;;;!! (defun double-up (event)
1549;;;!! (interactive "@e")
1550;;;!! (and double-start
1551;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1552;;;!! (setq double-start nil)))
1553;;;!!
1554;;;!! ;;; (defun x-test-doubleclick ()
1555;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1556;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1557;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1558;;;!!
1559;;;!! ;;
5ba2dc3f 1560;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
dbc4e1c1
JB
1561;;;!! ;;
1562;;;!!
1563;;;!! (defvar scrolled-lines 0)
1564;;;!! (defconst scroll-speed 1)
1565;;;!!
1566;;;!! (defun incr-scroll-down (event)
1567;;;!! (interactive "@e")
1568;;;!! (setq scrolled-lines 0)
1569;;;!! (incremental-scroll scroll-speed))
1570;;;!!
1571;;;!! (defun incr-scroll-up (event)
1572;;;!! (interactive "@e")
1573;;;!! (setq scrolled-lines 0)
1574;;;!! (incremental-scroll (- scroll-speed)))
1575;;;!!
1576;;;!! (defun incremental-scroll (n)
1577;;;!! (while (= (x-mouse-events) 0)
1578;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1579;;;!! (scroll-down n)
1580;;;!! (sit-for 300 t)))
1581;;;!!
1582;;;!! (defun incr-scroll-stop (event)
1583;;;!! (interactive "@e")
1584;;;!! (message "Scrolled %d lines" scrolled-lines)
1585;;;!! (setq scrolled-lines 0)
1586;;;!! (sleep-for 1))
1587;;;!!
1588;;;!! ;;; (defun x-testing-scroll ()
1589;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1590;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1591;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1592;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1593;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1594;;;!!
1595;;;!! ;;
1596;;;!! ;; Some playthings suitable for picture mode? They need work.
1597;;;!! ;;
1598;;;!!
1599;;;!! (defun mouse-kill-rectangle (event)
1600;;;!! "Kill the rectangle between point and the mouse cursor."
1601;;;!! (interactive "@e")
1602;;;!! (let ((point-save (point)))
1603;;;!! (save-excursion
1604;;;!! (mouse-set-point event)
1605;;;!! (push-mark nil t)
1606;;;!! (if (> point-save (point))
1607;;;!! (kill-rectangle (point) point-save)
1608;;;!! (kill-rectangle point-save (point))))))
1609;;;!!
1610;;;!! (defun mouse-open-rectangle (event)
1611;;;!! "Kill the rectangle between point and the mouse cursor."
1612;;;!! (interactive "@e")
1613;;;!! (let ((point-save (point)))
1614;;;!! (save-excursion
1615;;;!! (mouse-set-point event)
1616;;;!! (push-mark nil t)
1617;;;!! (if (> point-save (point))
1618;;;!! (open-rectangle (point) point-save)
1619;;;!! (open-rectangle point-save (point))))))
1620;;;!!
1621;;;!! ;; Must be a better way to do this.
1622;;;!!
1623;;;!! (defun mouse-multiple-insert (n char)
1624;;;!! (while (> n 0)
1625;;;!! (insert char)
1626;;;!! (setq n (1- n))))
1627;;;!!
1628;;;!! ;; What this could do is not finalize until button was released.
1629;;;!!
1630;;;!! (defun mouse-move-text (event)
1631;;;!! "Move text from point to cursor position, inserting spaces."
1632;;;!! (interactive "@e")
1633;;;!! (let* ((relative-coordinate
1634;;;!! (coordinates-in-window-p (car event) (selected-window))))
1635;;;!! (if (consp relative-coordinate)
1636;;;!! (cond ((> (current-column) (car relative-coordinate))
1637;;;!! (delete-char
1638;;;!! (- (car relative-coordinate) (current-column))))
1639;;;!! ((< (current-column) (car relative-coordinate))
1640;;;!! (mouse-multiple-insert
1641;;;!! (- (car relative-coordinate) (current-column)) " "))
1642;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
07a78410 1643\f
f936ae06
RS
1644;; Choose a completion with the mouse.
1645
1646(defun mouse-choose-completion (event)
49e61c42 1647 "Click on an alternative in the `*Completions*' buffer to choose it."
f936ae06 1648 (interactive "e")
33c448cd
RS
1649 ;; Give temporary modes such as isearch a chance to turn off.
1650 (run-hooks 'mouse-leave-buffer-hook)
d89a4a47 1651 (let ((buffer (window-buffer))
02680e9b
RS
1652 choice
1653 base-size)
f936ae06
RS
1654 (save-excursion
1655 (set-buffer (window-buffer (posn-window (event-start event))))
f36f4e9e
RS
1656 (if completion-reference-buffer
1657 (setq buffer completion-reference-buffer))
02680e9b 1658 (setq base-size completion-base-size)
f936ae06
RS
1659 (save-excursion
1660 (goto-char (posn-point (event-start event)))
b6be5d95 1661 (let (beg end)
9a43a594
RS
1662 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1663 (setq end (point) beg (1+ (point))))
1664 (if (null beg)
1665 (error "No completion here"))
1666 (setq beg (previous-single-property-change beg 'mouse-face))
2f5ed2e8
RS
1667 (setq end (or (next-single-property-change end 'mouse-face)
1668 (point-max)))
b6be5d95 1669 (setq choice (buffer-substring beg end)))))
73e2025f
RS
1670 (let ((owindow (selected-window)))
1671 (select-window (posn-window (event-start event)))
874a2cbd
RS
1672 (if (and (one-window-p t 'selected-frame)
1673 (window-dedicated-p (selected-window)))
1674 ;; This is a special buffer's frame
1675 (iconify-frame (selected-frame))
1676 (or (window-dedicated-p (selected-window))
1677 (bury-buffer)))
73e2025f 1678 (select-window owindow))
02680e9b 1679 (choose-completion-string choice buffer base-size)))
f936ae06 1680\f
07a78410
RS
1681;; Font selection.
1682
0eb9fef3
RS
1683(defun font-menu-add-default ()
1684 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1685 (font-alist x-fixed-font-alist)
0d94f5ca 1686 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
0eb9fef3
RS
1687 (if (assoc "Default" elt)
1688 (delete (assoc "Default" elt) elt))
1689 (setcdr elt
3d64d15b 1690 (cons (list "Default"
0eb9fef3
RS
1691 (cdr (assq 'font (frame-parameters (selected-frame)))))
1692 (cdr elt)))))
1693
07a78410
RS
1694(defvar x-fixed-font-alist
1695 '("Font menu"
1696 ("Misc"
19d973e8
RS
1697 ;; For these, we specify the pixel height and width.
1698 ("fixed" "fixed")
1699 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1700 ("6x12"
1701 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1702 ("6x13"
1703 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1704 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1705 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1706 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1707 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1708 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1709 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1710 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
fa21fdec 1711 ("")
f29c3695
RS
1712 ("clean 5x8"
1713 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1714 ("clean 6x8"
1715 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
19d973e8
RS
1716 ("clean 8x8"
1717 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1718 ("clean 8x10"
1719 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1720 ("clean 8x14"
1721 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1722 ("clean 8x16"
1723 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
fa21fdec 1724 ("")
19d973e8 1725 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
07a78410
RS
1726;;; We don't seem to have these; who knows what they are.
1727;;; ("fg-18" "fg-18")
1728;;; ("fg-25" "fg-25")
1729;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1730;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1731;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1732;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1733;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1734 ("Courier"
19d973e8 1735 ;; For these, we specify the point height.
82c048a9
RS
1736 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1737 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1738 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1739 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1740 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1741 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1742 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1743 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1744 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1745 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1746 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1747 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1748 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1749 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1750 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1751 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1752 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1753 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1754 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1755 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1756 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1757 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1758 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1759 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
07a78410
RS
1760 )
1761 "X fonts suitable for use in Emacs.")
1762
1900a92b 1763(defun mouse-set-font (&rest fonts)
07a78410
RS
1764 "Select an emacs font from a list of known good fonts"
1765 (interactive
1766 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
df4de8c6
KH
1767 (if fonts
1768 (let (font)
1769 (while fonts
1770 (condition-case nil
1771 (progn
3fadec1a 1772 (set-default-font (car fonts))
df4de8c6
KH
1773 (setq font (car fonts))
1774 (setq fonts nil))
3fadec1a
RS
1775 (error
1776 (setq fonts (cdr fonts)))))
df4de8c6 1777 (if (null font)
3fadec1a 1778 (error "Font not found")))))
cc0a8174
JB
1779\f
1780;;; Bindings for mouse commands.
1781
fcfc3c63 1782(define-key global-map [down-mouse-1] 'mouse-drag-region)
dbc4e1c1 1783(global-set-key [mouse-1] 'mouse-set-point)
dbc4e1c1 1784(global-set-key [drag-mouse-1] 'mouse-set-region)
fcfc3c63 1785
e37de120
RS
1786;; These are tested for in mouse-drag-region.
1787(global-set-key [double-mouse-1] 'mouse-set-point)
1788(global-set-key [triple-mouse-1] 'mouse-set-point)
1789
dbc4e1c1
JB
1790(global-set-key [mouse-2] 'mouse-yank-at-click)
1791(global-set-key [mouse-3] 'mouse-save-then-kill)
8b34e79d 1792
dbc4e1c1
JB
1793;; By binding these to down-going events, we let the user use the up-going
1794;; event to make the selection, saving a click.
08a1c178
RS
1795(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1796(if (not (eq system-type 'ms-dos))
1797 (global-set-key [S-down-mouse-1] 'mouse-set-font))
eef805d7 1798;; C-down-mouse-2 is bound in facemenu.el.
95132d1c
RS
1799(global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
1800
07a78410 1801
8b34e79d
RS
1802;; Replaced with dragging mouse-1
1803;; (global-set-key [S-mouse-1] 'mouse-set-mark)
947da0c4 1804
3c2dd2c0 1805(global-set-key [mode-line mouse-1] 'mouse-select-window)
544e7e73
RS
1806(global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1807(global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
3c2dd2c0 1808(global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
dbc4e1c1 1809(global-set-key [mode-line mouse-3] 'mouse-delete-window)
3c2dd2c0 1810(global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
9926ab64 1811(global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
b6522df6 1812(global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
08a1c178
RS
1813(global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1814(global-set-key [vertical-line mouse-1] 'mouse-select-window)
49116ac0
JB
1815
1816(provide 'mouse)
1817
6594deb0 1818;;; mouse.el ends here