Fix the way the sexagisimal names are calculated.
[bpt/emacs.git] / lisp / mouse.el
CommitLineData
6594deb0 1;;; mouse.el --- window system-independent mouse support.
84176303 2
f8c25f1b 3;;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
eea8d4ef 4
84176303 5;; Maintainer: FSF
84176303
ER
6;; Keywords: hardware
7
cc0a8174 8;;; This file is part of GNU Emacs.
72ea54a4 9
cc0a8174
JB
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
87ef29fd 12;;; the Free Software Foundation; either version 2, or (at your option)
cc0a8174 13;;; any later version.
72ea54a4 14
cc0a8174
JB
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
cc0a8174
JB
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)
fcfc3c63 404 (sit-for 1))
652ccd35 405 (push-mark)
1cc8a3f4 406 (set-mark (point))
652ccd35 407 (if (numberp (posn-point end))
e37de120
RS
408 (goto-char (posn-point end)))
409 ;; Don't set this-command to kill-region, so that a following
410 ;; C-w will not double the text in the kill ring.
91a6bc10
RS
411 ;; Ignore last-command so we don't append to a preceding kill.
412 (let (this-command last-command)
e6c2f5d4
RS
413 (copy-region-as-kill (mark) (point)))
414 (mouse-set-region-1)))
415
416(defun mouse-set-region-1 ()
417 (setq mouse-last-region-beg (region-beginning))
418 (setq mouse-last-region-end (region-end))
419 (setq mouse-last-region-tick (buffer-modified-tick)))
652ccd35 420
600c6e3a
JB
421(defvar mouse-scroll-delay 0.25
422 "*The pause between scroll steps caused by mouse drags, in seconds.
423If you drag the mouse beyond the edge of a window, Emacs scrolls the
424window to bring the text beyond that edge into view, with a delay of
425this many seconds between scroll steps. Scrolling stops when you move
426the mouse back into the window, or release the button.
427This variable's value may be non-integral.
428Setting this to zero causes Emacs to scroll as fast as it can.")
429
08a1c178 430(defvar mouse-scroll-min-lines 1
a3d6bb97
RS
431 "*The minimum number of lines scrolled by dragging mouse out of window.
432Moving the mouse out the top or bottom edge of the window begins
433scrolling repeatedly. The number of lines scrolled per repetition
434is normally equal to the number of lines beyond the window edge that
435the mouse has moved. However, it always scrolls at least the number
436of lines specified by this variable.")
08a1c178 437
e919a622
RS
438(defun mouse-scroll-subr (window jump &optional overlay start)
439 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
600c6e3a
JB
440If OVERLAY is an overlay, let it stretch from START to the far edge of
441the newly visible text.
442Upon exit, point is at the far edge of the newly visible text."
08a1c178
RS
443 (cond
444 ((and (> jump 0) (< jump mouse-scroll-min-lines))
445 (setq jump mouse-scroll-min-lines))
446 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
447 (setq jump (- mouse-scroll-min-lines))))
4e399a53
RS
448 (let ((opoint (point)))
449 (while (progn
450 (goto-char (window-start window))
451 (if (not (zerop (vertical-motion jump window)))
452 (progn
453 (set-window-start window (point))
454 (if (natnump jump)
455 (progn
456 (goto-char (window-end window))
457 ;; window-end doesn't reflect the window's new
458 ;; start position until the next redisplay. Hurrah.
459 (vertical-motion (1- jump) window))
460 (goto-char (window-start window)))
461 (if overlay
462 (move-overlay overlay start (point)))
463 ;; Now that we have scrolled WINDOW properly,
464 ;; put point back where it was for the redisplay
465 ;; so that we don't mess up the selected window.
466 (or (eq window (selected-window))
467 (goto-char opoint))
d2287ded 468 (sit-for mouse-scroll-delay)))))
4e399a53
RS
469 (or (eq window (selected-window))
470 (goto-char opoint))))
fcfc3c63 471
dc269e81 472;; Create an overlay and immediately delete it, to get "overlay in no buffer".
600c6e3a 473(defvar mouse-drag-overlay (make-overlay 1 1))
dc269e81 474(delete-overlay mouse-drag-overlay)
600c6e3a
JB
475(overlay-put mouse-drag-overlay 'face 'region)
476
dd524dbd 477(defvar mouse-selection-click-count 0)
eb6ff46f 478
c8c5bd24
RS
479(defvar mouse-selection-click-count-buffer nil)
480
600c6e3a 481(defun mouse-drag-region (start-event)
bcd5aef1 482 "Set the region to the text that the mouse is dragged over.
78210c95
RS
483Highlight the drag area as you move the mouse.
484This must be bound to a button-down mouse event.
485In Transient Mark mode, the highlighting remains once you
486release the mouse button. Otherwise, it does not."
bcd5aef1 487 (interactive "e")
d65147f6 488 (mouse-minibuffer-check start-event)
600c6e3a
JB
489 (let* ((start-posn (event-start start-event))
490 (start-point (posn-point start-posn))
491 (start-window (posn-window start-posn))
b846d039 492 (start-frame (window-frame start-window))
600c6e3a
JB
493 (bounds (window-edges start-window))
494 (top (nth 1 bounds))
495 (bottom (if (window-minibuffer-p start-window)
496 (nth 3 bounds)
497 ;; Don't count the mode line.
e37de120
RS
498 (1- (nth 3 bounds))))
499 (click-count (1- (event-click-count start-event))))
eb6ff46f 500 (setq mouse-selection-click-count click-count)
c8c5bd24 501 (setq mouse-selection-click-count-buffer (current-buffer))
b80f1928 502 (mouse-set-point start-event)
08a1c178
RS
503 ;; In case the down click is in the middle of some intangible text,
504 ;; use the end of that text, and put it in START-POINT.
505 (if (< (point) start-point)
506 (goto-char start-point))
507 (setq start-point (point))
e37de120
RS
508 (let ((range (mouse-start-end start-point start-point click-count)))
509 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
510 (window-buffer start-window)))
f767385c 511 (deactivate-mark)
08a1c178
RS
512 ;; end-of-range is used only in the single-click case.
513 ;; It is the place where the drag has reached so far
514 ;; (but not outside the window where the drag started).
515 (let (event end end-point (end-of-range (point)))
bcd5aef1 516 (track-mouse
600c6e3a 517 (while (progn
b846d039
JB
518 (setq event (read-event))
519 (or (mouse-movement-p event)
520 (eq (car-safe event) 'switch-frame)))
b846d039
JB
521 (if (eq (car-safe event) 'switch-frame)
522 nil
523 (setq end (event-end event)
524 end-point (posn-point end))
525
526 (cond
b846d039
JB
527 ;; Are we moving within the original window?
528 ((and (eq (posn-window end) start-window)
529 (integer-or-marker-p end-point))
08a1c178
RS
530 ;; Go to START-POINT first, so that when we move to END-POINT,
531 ;; if it's in the middle of intangible text,
532 ;; point jumps in the direction away from START-POINT.
533 (goto-char start-point)
b846d039 534 (goto-char end-point)
08a1c178
RS
535 (if (zerop (% click-count 3))
536 (setq end-of-range (point)))
e37de120
RS
537 (let ((range (mouse-start-end start-point (point) click-count)))
538 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
b846d039 539
50e527bc
KH
540 (t
541 (let ((mouse-row (cdr (cdr (mouse-position)))))
b846d039 542 (cond
50e527bc 543 ((null mouse-row))
b846d039 544 ((< mouse-row top)
e919a622
RS
545 (mouse-scroll-subr start-window (- mouse-row top)
546 mouse-drag-overlay start-point))
d2287ded 547 ((>= mouse-row bottom)
e919a622 548 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
50e527bc 549 mouse-drag-overlay start-point)))))))))
4e399a53 550 (if (consp event)
e37de120 551 (let ((fun (key-binding (vector (car event)))))
bfbcf12e
RS
552 ;; Run the binding of the terminating up-event, if possible.
553 ;; In the case of a multiple click, it gives the wrong results,
554 ;; because it would fail to set up a region.
555 (if (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
c2ef83b4 556 ;; In this case, we can just let the up-event execute normally.
08a1c178
RS
557 (let ((end (event-end event)))
558 ;; Set the position in the event before we replay it,
559 ;; because otherwise it may have a position in the wrong
560 ;; buffer.
561 (setcar (cdr end) end-of-range)
e6c2f5d4
RS
562 ;; Delete the overlay before calling the function,
563 ;; because delete-overlay increases buffer-modified-tick.
564 (delete-overlay mouse-drag-overlay)
c2ef83b4
RS
565 (setq unread-command-events
566 (cons event unread-command-events)))
44dc5252
RS
567 (if (not (= (overlay-start mouse-drag-overlay)
568 (overlay-end mouse-drag-overlay)))
569 (let (last-command this-command)
570 (push-mark (overlay-start mouse-drag-overlay) t t)
571 (goto-char (overlay-end mouse-drag-overlay))
e6c2f5d4
RS
572 (delete-overlay mouse-drag-overlay)
573 (copy-region-as-kill (point) (mark t))
574 (mouse-set-region-1))
44dc5252 575 (goto-char (overlay-end mouse-drag-overlay))
e6c2f5d4
RS
576 (setq this-command 'mouse-set-point)
577 (delete-overlay mouse-drag-overlay))))
578 (delete-overlay mouse-drag-overlay)))))
e37de120
RS
579\f
580;; Commands to handle xterm-style multiple clicks.
600c6e3a 581
e37de120
RS
582(defun mouse-skip-word (dir)
583 "Skip over word, over whitespace, or over identical punctuation.
584If DIR is positive skip forward; if negative, skip backward."
585 (let* ((char (following-char))
586 (syntax (char-to-string (char-syntax char))))
08a1c178
RS
587 (cond ((or (string= syntax "w") (string= syntax " "))
588 (if (< dir 0)
589 (skip-syntax-backward syntax)
590 (skip-syntax-forward syntax)))
591 ((string= syntax "_")
592 (if (< dir 0)
593 (skip-syntax-backward "w_")
594 (skip-syntax-forward "w_")))
595 ((< dir 0)
596 (while (and (not (bobp)) (= (preceding-char) char))
597 (forward-char -1)))
598 (t
599 (while (and (not (eobp)) (= (following-char) char))
600 (forward-char 1))))))
e37de120
RS
601
602;; Return a list of region bounds based on START and END according to MODE.
603;; If MODE is 0 then set point to (min START END), mark to (max START END).
604;; If MODE is 1 then set point to start of word at (min START END),
605;; mark to end of word at (max START END).
606;; If MODE is 2 then do the same for lines.
eb6ff46f 607(defun mouse-start-end (start end mode)
e37de120
RS
608 (if (> start end)
609 (let ((temp start))
610 (setq start end
611 end temp)))
9a974c88 612 (setq mode (mod mode 3))
e37de120
RS
613 (cond ((= mode 0)
614 (list start end))
615 ((and (= mode 1)
616 (= start end)
1ec71583 617 (char-after start)
e37de120 618 (= (char-syntax (char-after start)) ?\())
6f482eec
RS
619 (list start
620 (save-excursion
621 (goto-char start)
622 (forward-sexp 1)
623 (point))))
e37de120
RS
624 ((and (= mode 1)
625 (= start end)
1ec71583 626 (char-after start)
e37de120
RS
627 (= (char-syntax (char-after start)) ?\)))
628 (list (save-excursion
629 (goto-char (1+ start))
d89a4a47
RS
630 (backward-sexp 1)
631 (point))
e37de120
RS
632 (1+ start)))
633 ((= mode 1)
634 (list (save-excursion
635 (goto-char start)
636 (mouse-skip-word -1)
637 (point))
638 (save-excursion
639 (goto-char end)
640 (mouse-skip-word 1)
641 (point))))
642 ((= mode 2)
643 (list (save-excursion
644 (goto-char start)
645 (beginning-of-line 1)
646 (point))
647 (save-excursion
648 (goto-char end)
649 (forward-line 1)
650 (point))))))
e66feb07 651\f
3f26b32a
RS
652;; Subroutine: set the mark where CLICK happened,
653;; but don't do anything else.
654(defun mouse-set-mark-fast (click)
d65147f6 655 (mouse-minibuffer-check click)
3f26b32a
RS
656 (let ((posn (event-start click)))
657 (select-window (posn-window posn))
658 (if (numberp (posn-point posn))
659 (push-mark (posn-point posn) t t))))
660
661;; Momentarily show where the mark is, if highlighting doesn't show it.
662(defun mouse-show-mark ()
663 (or transient-mark-mode
664 (save-excursion
665 (goto-char (mark t))
666 (sit-for 1))))
667
cc0a8174
JB
668(defun mouse-set-mark (click)
669 "Set mark at the position clicked on with the mouse.
670Display cursor at that position for a second.
671This must be bound to a mouse click."
ec558adc 672 (interactive "e")
f598fb03
RS
673 (mouse-minibuffer-check click)
674 (select-window (posn-window (event-start click)))
675 ;; We don't use save-excursion because that preserves the mark too.
72ea54a4
RS
676 (let ((point-save (point)))
677 (unwind-protect
cc0a8174 678 (progn (mouse-set-point click)
897897e3
RS
679 (push-mark nil t t)
680 (or transient-mark-mode
681 (sit-for 1)))
72ea54a4
RS
682 (goto-char point-save))))
683
cc0a8174
JB
684(defun mouse-kill (click)
685 "Kill the region between point and the mouse click.
686The text is saved in the kill ring, as with \\[kill-region]."
ec558adc 687 (interactive "e")
d65147f6 688 (mouse-minibuffer-check click)
142c7672
KH
689 (let* ((posn (event-start click))
690 (click-posn (posn-point posn)))
691 (select-window (posn-window posn))
bd307392
JB
692 (if (numberp click-posn)
693 (kill-region (min (point) click-posn)
694 (max (point) click-posn)))))
72ea54a4 695
87ef29fd
JB
696(defun mouse-yank-at-click (click arg)
697 "Insert the last stretch of killed text at the position clicked on.
50f58001
RS
698Also move point to one end of the text thus inserted (normally the end).
699Prefix arguments are interpreted as with \\[yank].
700If `mouse-yank-at-point' is non-nil, insert at point
701regardless of where you click."
ec558adc 702 (interactive "e\nP")
33c448cd
RS
703 ;; Give temporary modes such as isearch a chance to turn off.
704 (run-hooks 'mouse-leave-buffer-hook)
50f58001 705 (or mouse-yank-at-point (mouse-set-point click))
d89a4a47 706 (setq this-command 'yank)
a814e9df 707 (setq mouse-selection-click-count 0)
87ef29fd
JB
708 (yank arg))
709
710(defun mouse-kill-ring-save (click)
cc0a8174
JB
711 "Copy the region between point and the mouse click in the kill ring.
712This does not delete the region; it acts like \\[kill-ring-save]."
ec558adc 713 (interactive "e")
3f26b32a 714 (mouse-set-mark-fast click)
6452d8a6
RS
715 (let (this-command last-command)
716 (kill-ring-save (point) (mark t)))
3f26b32a 717 (mouse-show-mark))
72ea54a4 718
dbc4e1c1
JB
719;;; This function used to delete the text between point and the mouse
720;;; whenever it was equal to the front of the kill ring, but some
721;;; people found that confusing.
722
723;;; A list (TEXT START END), describing the text and position of the last
724;;; invocation of mouse-save-then-kill.
725(defvar mouse-save-then-kill-posn nil)
726
26d280b9 727(defun mouse-save-then-kill-delete-region (beg end)
9a974c88
RS
728 ;; We must make our own undo boundaries
729 ;; because they happen automatically only for the current buffer.
730 (undo-boundary)
dd524dbd
RS
731 (if (or (= beg end) (eq buffer-undo-list t))
732 ;; If we have no undo list in this buffer,
733 ;; just delete.
734 (delete-region beg end)
735 ;; Delete, but make the undo-list entry share with the kill ring.
736 ;; First, delete just one char, so in case buffer is being modified
737 ;; for the first time, the undo list records that fact.
2f4b15ef
RS
738 (let (before-change-function after-change-function
739 before-change-functions after-change-functions)
740 (delete-region beg
741 (+ beg (if (> end beg) 1 -1))))
dd524dbd
RS
742 (let ((buffer-undo-list buffer-undo-list))
743 ;; Undo that deletion--but don't change the undo list!
2f4b15ef
RS
744 (let (before-change-function after-change-function
745 before-change-functions after-change-functions)
746 (primitive-undo 1 buffer-undo-list))
dd524dbd
RS
747 ;; Now delete the rest of the specified region,
748 ;; but don't record it.
749 (setq buffer-undo-list t)
9a974c88
RS
750 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
751 (error "Lossage in mouse-save-then-kill-delete-region"))
dd524dbd
RS
752 (delete-region beg end))
753 (let ((tail buffer-undo-list))
754 ;; Search back in buffer-undo-list for the string
755 ;; that came from deleting one character.
756 (while (and tail (not (stringp (car (car tail)))))
757 (setq tail (cdr tail)))
758 ;; Replace it with an entry for the entire deleted text.
759 (and tail
9a974c88
RS
760 (setcar tail (cons (car kill-ring) (min beg end))))))
761 (undo-boundary))
eb6ff46f 762
947da0c4 763(defun mouse-save-then-kill (click)
40a45a9f
RS
764 "Save text to point in kill ring; the second time, kill the text.
765If the text between point and the mouse is the same as what's
766at the front of the kill ring, this deletes the text.
767Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
eb6ff46f
RS
768which prepares for a second click to delete the text.
769
770If you have selected words or lines, this command extends the
771selection through the word or line clicked on. If you do this
772again in a different position, it extends the selection again.
773If you do this twice in the same position, the selection is killed."
947da0c4 774 (interactive "e")
b64548c7
RS
775 (let ((before-scroll point-before-scroll))
776 (mouse-minibuffer-check click)
777 (let ((click-posn (posn-point (event-start click)))
778 ;; Don't let a subsequent kill command append to this one:
779 ;; prevent setting this-command to kill-region.
780 (this-command this-command))
c8c5bd24
RS
781 (if (and (save-excursion
782 (set-buffer (window-buffer (posn-window (event-start click))))
783 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
784 ;; Don't be fooled by a recent click in some other buffer.
785 (eq mouse-selection-click-count-buffer
786 (current-buffer)))))
b64548c7
RS
787 (if (not (and (eq last-command 'mouse-save-then-kill)
788 (equal click-posn
789 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
790 ;; Find both ends of the object selected by this click.
791 (let* ((range
792 (mouse-start-end click-posn click-posn
793 mouse-selection-click-count)))
794 ;; Move whichever end is closer to the click.
795 ;; That's what xterm does, and it seems reasonable.
796 (if (< (abs (- click-posn (mark t)))
797 (abs (- click-posn (point))))
798 (set-mark (car range))
799 (goto-char (nth 1 range)))
800 ;; We have already put the old region in the kill ring.
801 ;; Replace it with the extended region.
802 ;; (It would be annoying to make a separate entry.)
803 (kill-new (buffer-substring (point) (mark t)) t)
e6c2f5d4 804 (mouse-set-region-1)
b64548c7
RS
805 ;; Arrange for a repeated mouse-3 to kill this region.
806 (setq mouse-save-then-kill-posn
807 (list (car kill-ring) (point) click-posn))
808 (mouse-show-mark))
809 ;; If we click this button again without moving it,
810 ;; that time kill.
bcde3748 811 (mouse-save-then-kill-delete-region (mark) (point))
b64548c7 812 (setq mouse-selection-click-count 0)
dd524dbd 813 (setq mouse-save-then-kill-posn nil))
b64548c7
RS
814 (if (and (eq last-command 'mouse-save-then-kill)
815 mouse-save-then-kill-posn
816 (eq (car mouse-save-then-kill-posn) (car kill-ring))
817 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
818 ;; If this is the second time we've called
819 ;; mouse-save-then-kill, delete the text from the buffer.
820 (progn
821 (mouse-save-then-kill-delete-region (point) (mark))
822 ;; After we kill, another click counts as "the first time".
823 (setq mouse-save-then-kill-posn nil))
824 (if (or (and (eq last-command 'mouse-save-then-kill)
825 mouse-save-then-kill-posn)
826 (and mark-active transient-mark-mode)
827 (and (memq last-command
828 '(mouse-drag-region mouse-set-region))
829 (or mark-even-if-inactive
830 (not transient-mark-mode))))
831 ;; We have a selection or suitable region, so adjust it.
832 (let* ((posn (event-start click))
833 (new (posn-point posn)))
834 (select-window (posn-window posn))
835 (if (numberp new)
836 (progn
837 ;; Move whichever end of the region is closer to the click.
838 ;; That is what xterm does, and it seems reasonable.
839 (if (< (abs (- new (point))) (abs (- new (mark t))))
840 (goto-char new)
841 (set-mark new))
842 (setq deactivate-mark nil)))
bcde3748
RS
843 (kill-new (buffer-substring (point) (mark t)) t)
844 (mouse-show-mark))
b64548c7
RS
845 ;; Set the mark where point is, then move where clicked.
846 (mouse-set-mark-fast click)
847 (if before-scroll
848 (goto-char before-scroll))
849 (exchange-point-and-mark)
eb9780b0 850 (kill-new (buffer-substring (point) (mark t))))
e6c2f5d4 851 (mouse-set-region-1)
b64548c7
RS
852 (setq mouse-save-then-kill-posn
853 (list (car kill-ring) (point) click-posn)))))))
e66feb07
RS
854\f
855(global-set-key [M-mouse-1] 'mouse-start-secondary)
856(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
857(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
858(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
9a974c88 859(global-set-key [M-mouse-2] 'mouse-yank-secondary)
e66feb07
RS
860
861;; An overlay which records the current secondary selection
862;; or else is deleted when there is no secondary selection.
863;; May be nil.
864(defvar mouse-secondary-overlay nil)
865
a94c7fcc
RS
866(defvar mouse-secondary-click-count 0)
867
e66feb07
RS
868;; A marker which records the specified first end for a secondary selection.
869;; May be nil.
870(defvar mouse-secondary-start nil)
871
872(defun mouse-start-secondary (click)
873 "Set one end of the secondary selection to the position clicked on.
874Use \\[mouse-secondary-save-then-kill] to set the other end
875and complete the secondary selection."
876 (interactive "e")
d65147f6 877 (mouse-minibuffer-check click)
e66feb07 878 (let ((posn (event-start click)))
230aaa73
RS
879 (save-excursion
880 (set-buffer (window-buffer (posn-window posn)))
881 ;; Cancel any preexisting secondary selection.
882 (if mouse-secondary-overlay
883 (delete-overlay mouse-secondary-overlay))
884 (if (numberp (posn-point posn))
885 (progn
886 (or mouse-secondary-start
887 (setq mouse-secondary-start (make-marker)))
888 (move-marker mouse-secondary-start (posn-point posn)))))))
e66feb07
RS
889
890(defun mouse-set-secondary (click)
891 "Set the secondary selection to the text that the mouse is dragged over.
892This must be bound to a mouse drag event."
893 (interactive "e")
d65147f6 894 (mouse-minibuffer-check click)
e66feb07
RS
895 (let ((posn (event-start click))
896 beg
897 (end (event-end click)))
230aaa73
RS
898 (save-excursion
899 (set-buffer (window-buffer (posn-window posn)))
900 (if (numberp (posn-point posn))
901 (setq beg (posn-point posn)))
902 (if mouse-secondary-overlay
903 (move-overlay mouse-secondary-overlay beg (posn-point end))
904 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
905 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
947da0c4 906
d89a4a47 907(defun mouse-drag-secondary (start-event)
e66feb07 908 "Set the secondary selection to the text that the mouse is dragged over.
d89a4a47 909Highlight the drag area as you move the mouse.
e66feb07
RS
910This must be bound to a button-down mouse event."
911 (interactive "e")
d65147f6 912 (mouse-minibuffer-check start-event)
d89a4a47
RS
913 (let* ((start-posn (event-start start-event))
914 (start-point (posn-point start-posn))
915 (start-window (posn-window start-posn))
916 (start-frame (window-frame start-window))
917 (bounds (window-edges start-window))
918 (top (nth 1 bounds))
919 (bottom (if (window-minibuffer-p start-window)
920 (nth 3 bounds)
921 ;; Don't count the mode line.
922 (1- (nth 3 bounds))))
923 (click-count (1- (event-click-count start-event))))
924 (save-excursion
925 (set-buffer (window-buffer start-window))
a94c7fcc 926 (setq mouse-secondary-click-count click-count)
d89a4a47
RS
927 (or mouse-secondary-overlay
928 (setq mouse-secondary-overlay
929 (make-overlay (point) (point))))
26d280b9 930 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
9a974c88 931 (if (> (mod click-count 3) 0)
26d280b9
RS
932 ;; Double or triple press: make an initial selection
933 ;; of one word or line.
d89a4a47
RS
934 (let ((range (mouse-start-end start-point start-point click-count)))
935 (set-marker mouse-secondary-start nil)
936 (move-overlay mouse-secondary-overlay 1 1
937 (window-buffer start-window))
938 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
939 (window-buffer start-window)))
26d280b9 940 ;; Single-press: cancel any preexisting secondary selection.
d89a4a47
RS
941 (or mouse-secondary-start
942 (setq mouse-secondary-start (make-marker)))
943 (set-marker mouse-secondary-start start-point)
944 (delete-overlay mouse-secondary-overlay))
945 (let (event end end-point)
946 (track-mouse
947 (while (progn
948 (setq event (read-event))
949 (or (mouse-movement-p event)
950 (eq (car-safe event) 'switch-frame)))
951
952 (if (eq (car-safe event) 'switch-frame)
953 nil
954 (setq end (event-end event)
955 end-point (posn-point end))
956 (cond
d89a4a47
RS
957 ;; Are we moving within the original window?
958 ((and (eq (posn-window end) start-window)
959 (integer-or-marker-p end-point))
d89a4a47
RS
960 (let ((range (mouse-start-end start-point end-point
961 click-count)))
0136e1e3
RS
962 (if (or (/= start-point end-point)
963 (null (marker-position mouse-secondary-start)))
964 (progn
965 (set-marker mouse-secondary-start nil)
966 (move-overlay mouse-secondary-overlay
967 (car range) (nth 1 range))))))
3b0aebe9
RS
968 (t
969 (let ((mouse-row (cdr (cdr (mouse-position)))))
970 (cond
971 ((null mouse-row))
972 ((< mouse-row top)
e919a622
RS
973 (mouse-scroll-subr start-window (- mouse-row top)
974 mouse-secondary-overlay start-point))
d2287ded 975 ((>= mouse-row bottom)
e919a622 976 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
3b0aebe9 977 mouse-secondary-overlay start-point)))))))))
d89a4a47 978
4e399a53
RS
979 (if (consp event)
980;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
981;;; (eq (posn-window (event-end event)) start-window)
982;;; (numberp (posn-point (event-end event)))
d89a4a47
RS
983 (if (marker-position mouse-secondary-start)
984 (save-window-excursion
985 (delete-overlay mouse-secondary-overlay)
9a974c88 986 (x-set-selection 'SECONDARY nil)
d89a4a47
RS
987 (select-window start-window)
988 (save-excursion
989 (goto-char mouse-secondary-start)
990 (sit-for 1)))
9a974c88
RS
991 (x-set-selection
992 'SECONDARY
993 (buffer-substring (overlay-start mouse-secondary-overlay)
994 (overlay-end mouse-secondary-overlay)))))))))
e66feb07 995
9a974c88 996(defun mouse-yank-secondary (click)
50f58001
RS
997 "Insert the secondary selection at the position clicked on.
998Move point to the end of the inserted text.
999If `mouse-yank-at-point' is non-nil, insert at point
1000regardless of where you click."
9a974c88 1001 (interactive "e")
33c448cd
RS
1002 ;; Give temporary modes such as isearch a chance to turn off.
1003 (run-hooks 'mouse-leave-buffer-hook)
50f58001
RS
1004 (or mouse-yank-at-point (mouse-set-point click))
1005 (insert (x-get-selection 'SECONDARY)))
9a974c88 1006
7e6404f6 1007(defun mouse-kill-secondary ()
9a974c88
RS
1008 "Kill the text in the secondary selection.
1009This is intended more as a keyboard command than as a mouse command
1010but it can work as either one.
1011
1012The current buffer (in case of keyboard use), or the buffer clicked on,
1013must be the one that the secondary selection is in. This requirement
1014is to prevent accidents."
7e6404f6
RS
1015 (interactive)
1016 (let* ((keys (this-command-keys))
1017 (click (elt keys (1- (length keys)))))
1018 (or (eq (overlay-buffer mouse-secondary-overlay)
1019 (if (listp click)
1020 (window-buffer (posn-window (event-start click)))
1021 (current-buffer)))
1022 (error "Select or click on the buffer where the secondary selection is")))
9dfab550
RS
1023 (let (this-command)
1024 (save-excursion
1025 (set-buffer (overlay-buffer mouse-secondary-overlay))
1026 (kill-region (overlay-start mouse-secondary-overlay)
1027 (overlay-end mouse-secondary-overlay))))
e66feb07 1028 (delete-overlay mouse-secondary-overlay)
9dfab550 1029;;; (x-set-selection 'SECONDARY nil)
e66feb07
RS
1030 (setq mouse-secondary-overlay nil))
1031
1032(defun mouse-secondary-save-then-kill (click)
d89a4a47 1033 "Save text to point in kill ring; the second time, kill the text.
7bbe2cc7
RS
1034You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1035If the text between where you did \\[mouse-start-secondary] and where
1036you use this command matches the text at the front of the kill ring,
1037this command deletes the text.
e66feb07 1038Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
7bbe2cc7 1039which prepares for a second click with this command to delete the text.
d89a4a47 1040
7bbe2cc7
RS
1041If you have already made a secondary selection in that buffer,
1042this command extends or retracts the selection to where you click.
1043If you do this again in a different position, it extends or retracts
1044again. If you do this twice in the same position, it kills the selection."
e66feb07 1045 (interactive "e")
d65147f6 1046 (mouse-minibuffer-check click)
d89a4a47
RS
1047 (let ((posn (event-start click))
1048 (click-posn (posn-point (event-start click)))
e66feb07
RS
1049 ;; Don't let a subsequent kill command append to this one:
1050 ;; prevent setting this-command to kill-region.
1051 (this-command this-command))
9a974c88
RS
1052 (or (eq (window-buffer (posn-window posn))
1053 (or (and mouse-secondary-overlay
1054 (overlay-buffer mouse-secondary-overlay))
1055 (if mouse-secondary-start
1056 (marker-buffer mouse-secondary-start))))
1057 (error "Wrong buffer"))
1058 (save-excursion
1059 (set-buffer (window-buffer (posn-window posn)))
a94c7fcc 1060 (if (> (mod mouse-secondary-click-count 3) 0)
9a974c88
RS
1061 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1062 (equal click-posn
1063 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1064 ;; Find both ends of the object selected by this click.
1065 (let* ((range
1066 (mouse-start-end click-posn click-posn
a94c7fcc 1067 mouse-secondary-click-count)))
9a974c88
RS
1068 ;; Move whichever end is closer to the click.
1069 ;; That's what xterm does, and it seems reasonable.
1070 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1071 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1072 (move-overlay mouse-secondary-overlay (car range)
1073 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1074 (move-overlay mouse-secondary-overlay
1075 (overlay-start mouse-secondary-overlay)
1076 (nth 1 range)))
9a974c88
RS
1077 ;; We have already put the old region in the kill ring.
1078 ;; Replace it with the extended region.
1079 ;; (It would be annoying to make a separate entry.)
f69140fd
KH
1080 (kill-new (buffer-substring
1081 (overlay-start mouse-secondary-overlay)
1082 (overlay-end mouse-secondary-overlay)) t)
9a974c88
RS
1083 ;; Arrange for a repeated mouse-3 to kill this region.
1084 (setq mouse-save-then-kill-posn
1085 (list (car kill-ring) (point) click-posn)))
1086 ;; If we click this button again without moving it,
1087 ;; that time kill.
d89a4a47 1088 (progn
9a974c88
RS
1089 (mouse-save-then-kill-delete-region
1090 (overlay-start mouse-secondary-overlay)
1091 (overlay-end mouse-secondary-overlay))
1092 (setq mouse-save-then-kill-posn nil)
a94c7fcc 1093 (setq mouse-secondary-click-count 0)
9a974c88
RS
1094 (delete-overlay mouse-secondary-overlay)))
1095 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1096 mouse-save-then-kill-posn
1097 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1098 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1099 ;; If this is the second time we've called
1100 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1101 (progn
1102 (mouse-save-then-kill-delete-region
1103 (overlay-start mouse-secondary-overlay)
1104 (overlay-end mouse-secondary-overlay))
1105 (setq mouse-save-then-kill-posn nil)
1106 (delete-overlay mouse-secondary-overlay))
1107 (if (overlay-start mouse-secondary-overlay)
1108 ;; We have a selection, so adjust it.
1109 (progn
1110 (if (numberp click-posn)
1111 (progn
1112 ;; Move whichever end of the region is closer to the click.
1113 ;; That is what xterm does, and it seems reasonable.
1114 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1115 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1116 (move-overlay mouse-secondary-overlay click-posn
1117 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1118 (move-overlay mouse-secondary-overlay
1119 (overlay-start mouse-secondary-overlay)
1120 click-posn))
9a974c88 1121 (setq deactivate-mark nil)))
0136e1e3 1122 (if (eq last-command 'mouse-secondary-save-then-kill)
f69140fd
KH
1123 ;; If the front of the kill ring comes from
1124 ;; an immediately previous use of this command,
1125 ;; replace it with the extended region.
1126 ;; (It would be annoying to make a separate entry.)
1127 (kill-new (buffer-substring
0136e1e3 1128 (overlay-start mouse-secondary-overlay)
f69140fd 1129 (overlay-end mouse-secondary-overlay)) t)
0136e1e3
RS
1130 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1131 (overlay-end mouse-secondary-overlay))))
9a974c88
RS
1132 (if mouse-secondary-start
1133 ;; All we have is one end of a selection,
1134 ;; so put the other end here.
1135 (let ((start (+ 0 mouse-secondary-start)))
1136 (kill-ring-save start click-posn)
1137 (if mouse-secondary-overlay
1138 (move-overlay mouse-secondary-overlay start click-posn)
1139 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1140 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1141 (setq mouse-save-then-kill-posn
1142 (list (car kill-ring) (point) click-posn))))
fb28fd5b
RS
1143 (if (overlay-buffer mouse-secondary-overlay)
1144 (x-set-selection 'SECONDARY
9a974c88
RS
1145 (buffer-substring
1146 (overlay-start mouse-secondary-overlay)
1147 (overlay-end mouse-secondary-overlay)))))))
e66feb07 1148\f
8b34e79d 1149(defun mouse-buffer-menu (event)
2d82f7b9
RS
1150 "Pop up a menu of buffers for selection with the mouse.
1151This switches buffers in the window that you clicked on,
1152and selects that window."
ec558adc 1153 (interactive "e")
d65147f6 1154 (mouse-minibuffer-check event)
8b34e79d
RS
1155 (let ((menu
1156 (list "Buffer Menu"
1157 (cons "Select Buffer"
1158 (let ((tail (buffer-list))
af2a85fe 1159 (maxbuf 0)
8b34e79d 1160 head)
af2a85fe
RS
1161 (while tail
1162 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1163 (setq maxbuf
1164 (max maxbuf
1165 (length (buffer-name (car tail))))))
1166 (setq tail (cdr tail)))
1167 (setq tail (buffer-list))
8b34e79d
RS
1168 (while tail
1169 (let ((elt (car tail)))
1170 (if (not (string-match "^ "
1171 (buffer-name elt)))
95e5bde9
SM
1172 (setq head
1173 (cons
1174 (cons
1175 (format
1176 (format "%%%ds %%s%%s %%s" maxbuf)
1177 (buffer-name elt)
1178 (if (buffer-modified-p elt) "*" " ")
1179 (save-excursion
1180 (set-buffer elt)
1181 (if buffer-read-only "%" " "))
1182 (or (buffer-file-name elt)
1183 (save-excursion
1184 (set-buffer elt)
1185 (if list-buffers-directory
1186 (expand-file-name
1187 list-buffers-directory)))
1188 ""))
1189 elt)
1190 head))))
8b34e79d
RS
1191 (setq tail (cdr tail)))
1192 (reverse head))))))
2d82f7b9
RS
1193 (let ((buf (x-popup-menu event menu))
1194 (window (posn-window (event-start event))))
1195 (if buf
1196 (progn
c0bb9f3b 1197 (or (framep window) (select-window window))
2d82f7b9 1198 (switch-to-buffer buf))))))
72ea54a4 1199\f
5ba2dc3f 1200;;; These need to be rewritten for the new scroll bar implementation.
dbc4e1c1
JB
1201
1202;;;!! ;; Commands for the scroll bar.
1203;;;!!
1204;;;!! (defun mouse-scroll-down (click)
1205;;;!! (interactive "@e")
1206;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1207;;;!!
1208;;;!! (defun mouse-scroll-up (click)
1209;;;!! (interactive "@e")
1210;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1211;;;!!
1212;;;!! (defun mouse-scroll-down-full ()
1213;;;!! (interactive "@")
1214;;;!! (scroll-down nil))
1215;;;!!
1216;;;!! (defun mouse-scroll-up-full ()
1217;;;!! (interactive "@")
1218;;;!! (scroll-up nil))
1219;;;!!
1220;;;!! (defun mouse-scroll-move-cursor (click)
1221;;;!! (interactive "@e")
1222;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1223;;;!!
1224;;;!! (defun mouse-scroll-absolute (event)
1225;;;!! (interactive "@e")
1226;;;!! (let* ((pos (car event))
1227;;;!! (position (car pos))
1228;;;!! (length (car (cdr pos))))
1229;;;!! (if (<= length 0) (setq length 1))
1230;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1231;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1232;;;!! position)
1233;;;!! length)
1234;;;!! scale-factor)))
1235;;;!! (goto-char newpos)
1236;;;!! (recenter '(4)))))
1237;;;!!
1238;;;!! (defun mouse-scroll-left (click)
1239;;;!! (interactive "@e")
1240;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1241;;;!!
1242;;;!! (defun mouse-scroll-right (click)
1243;;;!! (interactive "@e")
1244;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1245;;;!!
1246;;;!! (defun mouse-scroll-left-full ()
1247;;;!! (interactive "@")
1248;;;!! (scroll-left nil))
1249;;;!!
1250;;;!! (defun mouse-scroll-right-full ()
1251;;;!! (interactive "@")
1252;;;!! (scroll-right nil))
1253;;;!!
1254;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1255;;;!! (interactive "@e")
1256;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1257;;;!!
1258;;;!! (defun mouse-scroll-absolute-horizontally (event)
1259;;;!! (interactive "@e")
1260;;;!! (let* ((pos (car event))
1261;;;!! (position (car pos))
1262;;;!! (length (car (cdr pos))))
1263;;;!! (set-window-hscroll (selected-window) 33)))
1264;;;!!
1265;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1266;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1267;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1268;;;!!
1269;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1270;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1271;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1272;;;!!
1273;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1274;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1275;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1276;;;!!
1277;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1278;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1279;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1280;;;!!
1281;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1282;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1283;;;!! 'mouse-scroll-absolute-horizontally)
1284;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1285;;;!!
1286;;;!! (global-set-key [horizontal-slider mouse-1]
1287;;;!! 'mouse-scroll-move-cursor-horizontally)
1288;;;!! (global-set-key [horizontal-slider mouse-2]
1289;;;!! 'mouse-scroll-move-cursor-horizontally)
1290;;;!! (global-set-key [horizontal-slider mouse-3]
1291;;;!! 'mouse-scroll-move-cursor-horizontally)
1292;;;!!
1293;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1294;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1295;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1296;;;!!
1297;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1298;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1299;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1300;;;!!
1301;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1302;;;!! 'mouse-split-window-horizontally)
1303;;;!! (global-set-key [mode-line S-mouse-2]
1304;;;!! 'mouse-split-window-horizontally)
1305;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1306;;;!! 'mouse-split-window)
6b2154de 1307\f
dbc4e1c1
JB
1308;;;!! ;;;;
1309;;;!! ;;;; Here are experimental things being tested. Mouse events
1310;;;!! ;;;; are of the form:
1311;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1312;;;!! ;;
1313;;;!! ;;;;
1314;;;!! ;;;; Dynamically track mouse coordinates
1315;;;!! ;;;;
1316;;;!! ;;
1317;;;!! ;;(defun track-mouse (event)
1318;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1319;;;!! ;; (interactive "@e")
1320;;;!! ;; (while mouse-grabbed
1321;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1322;;;!! ;; (abs-x (car pos))
1323;;;!! ;; (abs-y (cdr pos))
1324;;;!! ;; (relative-coordinate (coordinates-in-window-p
1325;;;!! ;; (list (car pos) (cdr pos))
1326;;;!! ;; (selected-window))))
1327;;;!! ;; (if (consp relative-coordinate)
1328;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1329;;;!! ;; (car relative-coordinate)
1330;;;!! ;; (car (cdr relative-coordinate)))
1331;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1332;;;!!
1333;;;!! ;;
1334;;;!! ;; Dynamically put a box around the line indicated by point
1335;;;!! ;;
1336;;;!! ;;
1337;;;!! ;;(require 'backquote)
1338;;;!! ;;
1339;;;!! ;;(defun mouse-select-buffer-line (event)
1340;;;!! ;; (interactive "@e")
1341;;;!! ;; (let ((relative-coordinate
1342;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1343;;;!! ;; (abs-y (car (cdr (car event)))))
1344;;;!! ;; (if (consp relative-coordinate)
1345;;;!! ;; (progn
1346;;;!! ;; (save-excursion
1347;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1348;;;!! ;; (x-draw-rectangle
1349;;;!! ;; (selected-screen)
1350;;;!! ;; abs-y 0
1351;;;!! ;; (save-excursion
1352;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1353;;;!! ;; (end-of-line)
1354;;;!! ;; (push-mark nil t)
1355;;;!! ;; (beginning-of-line)
1356;;;!! ;; (- (region-end) (region-beginning))) 1))
1357;;;!! ;; (sit-for 1)
1358;;;!! ;; (x-erase-rectangle (selected-screen))))))
1359;;;!! ;;
1360;;;!! ;;(defvar last-line-drawn nil)
1361;;;!! ;;(defvar begin-delim "[^ \t]")
1362;;;!! ;;(defvar end-delim "[^ \t]")
1363;;;!! ;;
1364;;;!! ;;(defun mouse-boxing (event)
1365;;;!! ;; (interactive "@e")
1366;;;!! ;; (save-excursion
1367;;;!! ;; (let ((screen (selected-screen)))
1368;;;!! ;; (while (= (x-mouse-events) 0)
1369;;;!! ;; (let* ((pos (read-mouse-position screen))
1370;;;!! ;; (abs-x (car pos))
1371;;;!! ;; (abs-y (cdr pos))
1372;;;!! ;; (relative-coordinate
1373;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1374;;;!! ;; (selected-window)))
1375;;;!! ;; (begin-reg nil)
1376;;;!! ;; (end-reg nil)
1377;;;!! ;; (end-column nil)
1378;;;!! ;; (begin-column nil))
1379;;;!! ;; (if (and (consp relative-coordinate)
1380;;;!! ;; (or (not last-line-drawn)
1381;;;!! ;; (not (= last-line-drawn abs-y))))
1382;;;!! ;; (progn
1383;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1384;;;!! ;; (if (= (following-char) 10)
1385;;;!! ;; ()
1386;;;!! ;; (progn
1387;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1388;;;!! ;; (setq begin-column (1- (current-column)))
1389;;;!! ;; (end-of-line)
1390;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1391;;;!! ;; (setq end-column (1+ (current-column)))
1392;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1393;;;!! ;; (x-draw-rectangle screen
1394;;;!! ;; (setq last-line-drawn abs-y)
1395;;;!! ;; begin-column
1396;;;!! ;; (- end-column begin-column) 1))))))))))
1397;;;!! ;;
1398;;;!! ;;(defun mouse-erase-box ()
1399;;;!! ;; (interactive)
1400;;;!! ;; (if last-line-drawn
1401;;;!! ;; (progn
1402;;;!! ;; (x-erase-rectangle (selected-screen))
1403;;;!! ;; (setq last-line-drawn nil))))
1404;;;!!
1405;;;!! ;;; (defun test-x-rectangle ()
1406;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1407;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1408;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1409;;;!!
1410;;;!! ;;
1411;;;!! ;; Here is how to do double clicking in lisp. About to change.
1412;;;!! ;;
1413;;;!!
1414;;;!! (defvar double-start nil)
1415;;;!! (defconst double-click-interval 300
1416;;;!! "Max ticks between clicks")
1417;;;!!
1418;;;!! (defun double-down (event)
1419;;;!! (interactive "@e")
1420;;;!! (if double-start
1421;;;!! (let ((interval (- (nth 4 event) double-start)))
1422;;;!! (if (< interval double-click-interval)
1423;;;!! (progn
1424;;;!! (backward-up-list 1)
1425;;;!! ;; (message "Interval %d" interval)
1426;;;!! (sleep-for 1)))
1427;;;!! (setq double-start nil))
1428;;;!! (setq double-start (nth 4 event))))
1429;;;!!
1430;;;!! (defun double-up (event)
1431;;;!! (interactive "@e")
1432;;;!! (and double-start
1433;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1434;;;!! (setq double-start nil)))
1435;;;!!
1436;;;!! ;;; (defun x-test-doubleclick ()
1437;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1438;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1439;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1440;;;!!
1441;;;!! ;;
5ba2dc3f 1442;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
dbc4e1c1
JB
1443;;;!! ;;
1444;;;!!
1445;;;!! (defvar scrolled-lines 0)
1446;;;!! (defconst scroll-speed 1)
1447;;;!!
1448;;;!! (defun incr-scroll-down (event)
1449;;;!! (interactive "@e")
1450;;;!! (setq scrolled-lines 0)
1451;;;!! (incremental-scroll scroll-speed))
1452;;;!!
1453;;;!! (defun incr-scroll-up (event)
1454;;;!! (interactive "@e")
1455;;;!! (setq scrolled-lines 0)
1456;;;!! (incremental-scroll (- scroll-speed)))
1457;;;!!
1458;;;!! (defun incremental-scroll (n)
1459;;;!! (while (= (x-mouse-events) 0)
1460;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1461;;;!! (scroll-down n)
1462;;;!! (sit-for 300 t)))
1463;;;!!
1464;;;!! (defun incr-scroll-stop (event)
1465;;;!! (interactive "@e")
1466;;;!! (message "Scrolled %d lines" scrolled-lines)
1467;;;!! (setq scrolled-lines 0)
1468;;;!! (sleep-for 1))
1469;;;!!
1470;;;!! ;;; (defun x-testing-scroll ()
1471;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1472;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1473;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1474;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1475;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1476;;;!!
1477;;;!! ;;
1478;;;!! ;; Some playthings suitable for picture mode? They need work.
1479;;;!! ;;
1480;;;!!
1481;;;!! (defun mouse-kill-rectangle (event)
1482;;;!! "Kill the rectangle between point and the mouse cursor."
1483;;;!! (interactive "@e")
1484;;;!! (let ((point-save (point)))
1485;;;!! (save-excursion
1486;;;!! (mouse-set-point event)
1487;;;!! (push-mark nil t)
1488;;;!! (if (> point-save (point))
1489;;;!! (kill-rectangle (point) point-save)
1490;;;!! (kill-rectangle point-save (point))))))
1491;;;!!
1492;;;!! (defun mouse-open-rectangle (event)
1493;;;!! "Kill the rectangle between point and the mouse cursor."
1494;;;!! (interactive "@e")
1495;;;!! (let ((point-save (point)))
1496;;;!! (save-excursion
1497;;;!! (mouse-set-point event)
1498;;;!! (push-mark nil t)
1499;;;!! (if (> point-save (point))
1500;;;!! (open-rectangle (point) point-save)
1501;;;!! (open-rectangle point-save (point))))))
1502;;;!!
1503;;;!! ;; Must be a better way to do this.
1504;;;!!
1505;;;!! (defun mouse-multiple-insert (n char)
1506;;;!! (while (> n 0)
1507;;;!! (insert char)
1508;;;!! (setq n (1- n))))
1509;;;!!
1510;;;!! ;; What this could do is not finalize until button was released.
1511;;;!!
1512;;;!! (defun mouse-move-text (event)
1513;;;!! "Move text from point to cursor position, inserting spaces."
1514;;;!! (interactive "@e")
1515;;;!! (let* ((relative-coordinate
1516;;;!! (coordinates-in-window-p (car event) (selected-window))))
1517;;;!! (if (consp relative-coordinate)
1518;;;!! (cond ((> (current-column) (car relative-coordinate))
1519;;;!! (delete-char
1520;;;!! (- (car relative-coordinate) (current-column))))
1521;;;!! ((< (current-column) (car relative-coordinate))
1522;;;!! (mouse-multiple-insert
1523;;;!! (- (car relative-coordinate) (current-column)) " "))
1524;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
07a78410 1525\f
f936ae06
RS
1526;; Choose a completion with the mouse.
1527
1528(defun mouse-choose-completion (event)
49e61c42 1529 "Click on an alternative in the `*Completions*' buffer to choose it."
f936ae06 1530 (interactive "e")
33c448cd
RS
1531 ;; Give temporary modes such as isearch a chance to turn off.
1532 (run-hooks 'mouse-leave-buffer-hook)
d89a4a47 1533 (let ((buffer (window-buffer))
02680e9b
RS
1534 choice
1535 base-size)
f936ae06
RS
1536 (save-excursion
1537 (set-buffer (window-buffer (posn-window (event-start event))))
f36f4e9e
RS
1538 (if completion-reference-buffer
1539 (setq buffer completion-reference-buffer))
02680e9b 1540 (setq base-size completion-base-size)
f936ae06
RS
1541 (save-excursion
1542 (goto-char (posn-point (event-start event)))
b6be5d95 1543 (let (beg end)
9a43a594
RS
1544 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1545 (setq end (point) beg (1+ (point))))
1546 (if (null beg)
1547 (error "No completion here"))
1548 (setq beg (previous-single-property-change beg 'mouse-face))
2f5ed2e8
RS
1549 (setq end (or (next-single-property-change end 'mouse-face)
1550 (point-max)))
b6be5d95 1551 (setq choice (buffer-substring beg end)))))
73e2025f
RS
1552 (let ((owindow (selected-window)))
1553 (select-window (posn-window (event-start event)))
874a2cbd
RS
1554 (if (and (one-window-p t 'selected-frame)
1555 (window-dedicated-p (selected-window)))
1556 ;; This is a special buffer's frame
1557 (iconify-frame (selected-frame))
1558 (or (window-dedicated-p (selected-window))
1559 (bury-buffer)))
73e2025f 1560 (select-window owindow))
02680e9b 1561 (choose-completion-string choice buffer base-size)))
f936ae06 1562\f
07a78410
RS
1563;; Font selection.
1564
0eb9fef3
RS
1565(defun font-menu-add-default ()
1566 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1567 (font-alist x-fixed-font-alist)
0d94f5ca 1568 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
0eb9fef3
RS
1569 (if (assoc "Default" elt)
1570 (delete (assoc "Default" elt) elt))
1571 (setcdr elt
3d64d15b 1572 (cons (list "Default"
0eb9fef3
RS
1573 (cdr (assq 'font (frame-parameters (selected-frame)))))
1574 (cdr elt)))))
1575
07a78410
RS
1576(defvar x-fixed-font-alist
1577 '("Font menu"
1578 ("Misc"
19d973e8
RS
1579 ;; For these, we specify the pixel height and width.
1580 ("fixed" "fixed")
1581 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1582 ("6x12"
1583 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1584 ("6x13"
1585 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1586 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1587 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1588 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1589 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1590 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1591 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1592 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
fa21fdec 1593 ("")
f29c3695
RS
1594 ("clean 5x8"
1595 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1596 ("clean 6x8"
1597 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
19d973e8
RS
1598 ("clean 8x8"
1599 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1600 ("clean 8x10"
1601 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1602 ("clean 8x14"
1603 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1604 ("clean 8x16"
1605 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
fa21fdec 1606 ("")
19d973e8 1607 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
07a78410
RS
1608;;; We don't seem to have these; who knows what they are.
1609;;; ("fg-18" "fg-18")
1610;;; ("fg-25" "fg-25")
1611;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1612;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1613;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1614;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1615;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1616 ("Courier"
19d973e8 1617 ;; For these, we specify the point height.
82c048a9
RS
1618 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1619 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1620 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1621 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1622 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1623 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1624 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1625 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1626 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1627 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1628 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1629 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1630 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1631 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1632 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1633 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1634 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1635 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1636 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1637 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1638 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1639 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1640 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1641 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
07a78410
RS
1642 )
1643 "X fonts suitable for use in Emacs.")
1644
1900a92b 1645(defun mouse-set-font (&rest fonts)
07a78410
RS
1646 "Select an emacs font from a list of known good fonts"
1647 (interactive
1648 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
df4de8c6
KH
1649 (if fonts
1650 (let (font)
1651 (while fonts
1652 (condition-case nil
1653 (progn
3fadec1a 1654 (set-default-font (car fonts))
df4de8c6
KH
1655 (setq font (car fonts))
1656 (setq fonts nil))
3fadec1a
RS
1657 (error
1658 (setq fonts (cdr fonts)))))
df4de8c6 1659 (if (null font)
3fadec1a 1660 (error "Font not found")))))
cc0a8174
JB
1661\f
1662;;; Bindings for mouse commands.
1663
fcfc3c63 1664(define-key global-map [down-mouse-1] 'mouse-drag-region)
dbc4e1c1 1665(global-set-key [mouse-1] 'mouse-set-point)
dbc4e1c1 1666(global-set-key [drag-mouse-1] 'mouse-set-region)
fcfc3c63 1667
e37de120
RS
1668;; These are tested for in mouse-drag-region.
1669(global-set-key [double-mouse-1] 'mouse-set-point)
1670(global-set-key [triple-mouse-1] 'mouse-set-point)
1671
dbc4e1c1
JB
1672(global-set-key [mouse-2] 'mouse-yank-at-click)
1673(global-set-key [mouse-3] 'mouse-save-then-kill)
8b34e79d 1674
dbc4e1c1
JB
1675;; By binding these to down-going events, we let the user use the up-going
1676;; event to make the selection, saving a click.
08a1c178
RS
1677(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1678(if (not (eq system-type 'ms-dos))
1679 (global-set-key [S-down-mouse-1] 'mouse-set-font))
eef805d7 1680;; C-down-mouse-2 is bound in facemenu.el.
95132d1c
RS
1681(global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
1682
07a78410 1683
8b34e79d
RS
1684;; Replaced with dragging mouse-1
1685;; (global-set-key [S-mouse-1] 'mouse-set-mark)
947da0c4 1686
3c2dd2c0 1687(global-set-key [mode-line mouse-1] 'mouse-select-window)
544e7e73
RS
1688(global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1689(global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
3c2dd2c0 1690(global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
dbc4e1c1 1691(global-set-key [mode-line mouse-3] 'mouse-delete-window)
3c2dd2c0 1692(global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
9926ab64 1693(global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
b6522df6 1694(global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
08a1c178
RS
1695(global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1696(global-set-key [vertical-line mouse-1] 'mouse-select-window)
49116ac0
JB
1697
1698(provide 'mouse)
1699
6594deb0 1700;;; mouse.el ends here