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