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