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