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