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