(mouse-on-link-p): Change functionality and doc
[bpt/emacs.git] / lisp / mouse.el
CommitLineData
be010748 1;;; mouse.el --- window system-independent mouse support
84176303 2
fbd8dc8a 3;; Copyright (C) 1993, 94, 95, 1999, 2000, 2001, 2002, 2003, 2004, 2005
5925bb84 4;; Free Software Foundation, Inc.
eea8d4ef 5
84176303 6;; Maintainer: FSF
de420e82 7;; Keywords: hardware, mouse
84176303 8
be010748 9;; This file is part of GNU Emacs.
72ea54a4 10
be010748
RS
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
72ea54a4 15
be010748
RS
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
72ea54a4 20
be010748 21;; You should have received a copy of the GNU General Public License
4c9afcbe
RS
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
72ea54a4 25
edbd2f74
ER
26;;; Commentary:
27
28;; This package provides various useful commands (including help
29;; system access) through the mouse. All this code assumes that mouse
30;; interpretation has been abstracted into Emacs input events.
31;;
32;; The code is rather X-dependent.
33
aae56ea7
ER
34;;; Code:
35
cc0a8174 36;;; Utility functions.
72ea54a4 37
cc0a8174
JB
38;;; Indent track-mouse like progn.
39(put 'track-mouse 'lisp-indent-function 0)
72ea54a4 40
3b558d41
RS
41(defcustom mouse-yank-at-point nil
42 "*If non-nil, mouse yank commands yank at point instead of at click."
43 :type 'boolean
44 :group 'mouse)
b2dae92a
KS
45
46(defcustom mouse-drag-copy-region t
47 "*If non-nil, mouse drag copies region to kill-ring."
48 :type 'boolean
8413d0d2 49 :version "21.4"
b2dae92a
KS
50 :group 'mouse)
51
65f76581 52(defcustom mouse-1-click-follows-link 350
787e24c3 53 "Non-nil means that clicking Mouse-1 on a link follows the link.
65f76581 54
787e24c3
KS
55With the default setting, an ordinary Mouse-1 click on a link
56performs the same action as Mouse-2 on that link, while a longer
57Mouse-1 click \(hold down the Mouse-1 button for more than 350
58milliseconds) performs the original Mouse-1 binding \(which
65f76581
KS
59typically sets point where you click the mouse).
60
61If value is an integer, the time elapsed between pressing and
62releasing the mouse button determines whether to follow the link
787e24c3 63or perform the normal Mouse-1 action (typically set point).
65f76581
KS
64The absolute numeric value specifices the maximum duration of a
65\"short click\" in milliseconds. A positive value means that a
66short click follows the link, and a longer click performs the
67normal action. A negative value gives the opposite behaviour.
68
69If value is `double', a double click follows the link.
70
787e24c3 71Otherwise, a single Mouse-1 click unconditionally follows the link.
65f76581
KS
72
73Note that dragging the mouse never follows the link.
74
75This feature only works in modes that specifically identify
76clickable text as links, so it may not work with some external
77packages. See `mouse-on-link-p' for details."
78 :version "21.4"
79 :type '(choice (const :tag "Disabled" nil)
80 (const :tag "Double click" double)
81 (number :tag "Single click time limit" :value 350)
82 (other :tag "Single click" t))
83 :group 'mouse)
84
cc0a8174 85\f
95132d1c
RS
86;; Provide a mode-specific menu on a mouse button.
87
21ad0f7b
SM
88(defun popup-menu (menu &optional position prefix)
89 "Popup the given menu and call the selected option.
de420e82
DL
90MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
91`x-popup-menu'.
21ad0f7b
SM
92POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
93 the current mouse position.
94PREFIX is the prefix argument (if any) to pass to the command."
de420e82
DL
95 (let* ((map (cond
96 ((keymapp menu) menu)
97 ((and (listp menu) (keymapp (car menu))) menu)
98 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
72596e2f
SM
99 (filter (when (symbolp map)
100 (plist-get (get map 'menu-prop) :filter))))
101 (if filter (funcall filter (symbol-function map)) map)))))
0dd672a6 102 event cmd)
b52a30d8 103 (unless position
d32af6dd 104 (let ((mp (mouse-pixel-position)))
b52a30d8 105 (setq position (list (list (cadr mp) (cddr mp)) (car mp)))))
21ad0f7b 106 ;; The looping behavior was taken from lmenu's popup-menu-popup
7636d2a3
EZ
107 (while (and map (setq event
108 ;; map could be a prefix key, in which case
109 ;; we need to get its function cell
110 ;; definition.
d32af6dd 111 (x-popup-menu position (indirect-function map))))
21ad0f7b
SM
112 ;; Strangely x-popup-menu returns a list.
113 ;; mouse-major-mode-menu was using a weird:
114 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
0dd672a6
SM
115 (setq cmd
116 (if (and (not (keymapp map)) (listp map))
117 ;; We were given a list of keymaps. Search them all
118 ;; in sequence until a first binding is found.
119 (let ((mouse-click (apply 'vector event))
120 binding)
121 (while (and map (null binding))
122 (setq binding (lookup-key (car map) mouse-click))
123 (if (numberp binding) ; `too long'
124 (setq binding nil))
125 (setq map (cdr map)))
126 binding)
127 ;; We were given a single keymap.
128 (lookup-key map (apply 'vector event))))
129 ;; Clear out echoing, which perhaps shows a prefix arg.
130 (message "")
131 ;; Maybe try again but with the submap.
132 (setq map (if (keymapp cmd) cmd)))
324cd972
RS
133 ;; If the user did not cancel by refusing to select,
134 ;; and if the result is a command, run it.
135 (when (and (null map) (commandp cmd))
0dd672a6
SM
136 (setq prefix-arg prefix)
137 ;; `setup-specified-language-environment', for instance,
138 ;; expects this to be set from a menu keymap.
139 (setq last-command-event (car (last event)))
140 ;; mouse-major-mode-menu was using `command-execute' instead.
65b61266 141 (call-interactively cmd))))
bcd010a0
DL
142
143(defvar mouse-major-mode-menu-prefix) ; dynamically bound
144
6a16c4cb 145(defun mouse-major-mode-menu (event prefix)
b98e5762
DL
146 "Pop up a mode-specific menu of mouse commands.
147Default to the Edit menu if the major mode doesn't define a menu."
95132d1c
RS
148 ;; Switch to the window clicked on, because otherwise
149 ;; the mode's commands may not make sense.
6a16c4cb 150 (interactive "@e\nP")
25f9b4bf 151 ;; Let the mode update its menus first.
179fc9ef 152 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
b98e5762
DL
153 (let* (;; This is where mouse-major-mode-menu-prefix
154 ;; returns the prefix we should use (after menu-bar).
155 ;; It is either nil or (SOME-SYMBOL).
156 (mouse-major-mode-menu-prefix nil)
157 ;; Keymap from which to inherit; may be null.
158 (ancestor (mouse-major-mode-menu-1
159 (and (current-local-map)
25db2767 160 (local-key-binding [menu-bar]))))
b98e5762
DL
161 ;; Make a keymap in which our last command leads to a menu or
162 ;; default to the edit menu.
163 (newmap (if ancestor
164 (make-sparse-keymap (concat mode-name " Mode"))
165 menu-bar-edit-menu))
166 result)
167 (if ancestor
168 ;; Make our menu inherit from the desired keymap which we want
169 ;; to display as the menu now.
170 (set-keymap-parent newmap ancestor))
21ad0f7b
SM
171 (popup-menu newmap event prefix)))
172
95132d1c
RS
173
174;; Compute and cache the equivalent keys in MENU and all its submenus.
d5c847bb
KH
175;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
176;;; (and (eq (car menu) 'keymap)
177;;; (x-popup-menu nil menu))
178;;; (while menu
179;;; (and (consp (car menu))
180;;; (consp (cdr (car menu)))
181;;; (let ((tail (cdr (car menu))))
182;;; (while (and (consp tail)
183;;; (not (eq (car tail) 'keymap)))
184;;; (setq tail (cdr tail)))
185;;; (if (consp tail)
186;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
187;;; (setq menu (cdr menu))))
95132d1c
RS
188
189;; Given a mode's menu bar keymap,
190;; if it defines exactly one menu bar menu,
191;; return just that menu.
192;; Otherwise return a menu for all of them.
193(defun mouse-major-mode-menu-1 (menubar)
194 (if menubar
195 (let ((tail menubar)
196 submap)
197 (while tail
198 (if (consp (car tail))
199 (if submap
200 (setq submap t)
d5c847bb 201 (setq submap (car tail))))
95132d1c 202 (setq tail (cdr tail)))
d5c847bb 203 (if (eq submap t)
d3e458b0 204 menubar
d5c847bb 205 (setq mouse-major-mode-menu-prefix (list (car submap)))
a45423d8 206 (lookup-key menubar (vector (car submap)))))))
de420e82
DL
207
208(defun mouse-popup-menubar (event prefix)
a7df580d 209 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
de420e82
DL
210The contents are the items that would be in the menu bar whether or
211not it is actually displayed."
212 (interactive "@e \nP")
179fc9ef 213 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
7636d2a3
EZ
214 (let* ((local-menu (and (current-local-map)
215 (lookup-key (current-local-map) [menu-bar])))
216 (global-menu (lookup-key global-map [menu-bar]))
59836110
EZ
217 ;; If a keymap doesn't have a prompt string (a lazy
218 ;; programmer didn't bother to provide one), create it and
219 ;; insert it into the keymap; each keymap gets its own
220 ;; prompt. This is required for non-toolkit versions to
221 ;; display non-empty menu pane names.
222 (minor-mode-menus
223 (mapcar
224 (function
225 (lambda (menu)
226 (let* ((minor-mode (car menu))
227 (menu (cdr menu))
228 (title-or-map (cadr menu)))
229 (or (stringp title-or-map)
230 (setq menu
231 (cons 'keymap
232 (cons (concat
233 (capitalize (subst-char-in-string
234 ?- ?\ (symbol-name
235 minor-mode)))
236 " Menu")
237 (cdr menu)))))
238 menu)))
239 (minor-mode-key-binding [menu-bar])))
7636d2a3
EZ
240 (local-title-or-map (and local-menu (cadr local-menu)))
241 (global-title-or-map (cadr global-menu)))
7636d2a3
EZ
242 (or (null local-menu)
243 (stringp local-title-or-map)
244 (setq local-menu (cons 'keymap
245 (cons (concat mode-name " Mode Menu")
246 (cdr local-menu)))))
247 (or (stringp global-title-or-map)
248 (setq global-menu (cons 'keymap
249 (cons "Global Menu"
250 (cdr global-menu)))))
de420e82 251 ;; Supplying the list is faster than making a new map.
25db2767
DL
252 (popup-menu (append (list global-menu)
253 (if local-menu
254 (list local-menu))
255 minor-mode-menus)
7636d2a3 256 event prefix)))
de420e82
DL
257
258(defun mouse-popup-menubar-stuff (event prefix)
259 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
260Use the former if the menu bar is showing, otherwise the latter."
261 (interactive "@e \nP")
262 (if (zerop (assoc-default 'menu-bar-lines (frame-parameters) 'eq 0))
263 (mouse-popup-menubar event prefix)
264 (mouse-major-mode-menu event prefix)))
95132d1c 265\f
544e7e73
RS
266;; Commands that operate on windows.
267
d65147f6
KH
268(defun mouse-minibuffer-check (event)
269 (let ((w (posn-window (event-start event))))
270 (and (window-minibuffer-p w)
271 (not (minibuffer-window-active-p w))
33c448cd
RS
272 (error "Minibuffer window is not active")))
273 ;; Give temporary modes such as isearch a chance to turn off.
274 (run-hooks 'mouse-leave-buffer-hook))
d65147f6 275
cc0a8174 276(defun mouse-delete-window (click)
947da0c4 277 "Delete the window you click on.
6b48d742 278Do nothing if the frame has just one window.
a926a0fa 279This command must be bound to a mouse click."
ec558adc 280 (interactive "e")
6b48d742 281 (unless (one-window-p t)
a926a0fa
RS
282 (mouse-minibuffer-check click)
283 (delete-window (posn-window (event-start click)))))
cc0a8174 284
3c2dd2c0
RS
285(defun mouse-select-window (click)
286 "Select the window clicked on; don't move point."
287 (interactive "e")
d65147f6 288 (mouse-minibuffer-check click)
3c2dd2c0
RS
289 (let ((oframe (selected-frame))
290 (frame (window-frame (posn-window (event-start click)))))
291 (select-window (posn-window (event-start click)))
292 (raise-frame frame)
293 (select-frame frame)
294 (or (eq frame oframe)
e1877477 295 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
3c2dd2c0 296
b0f3a26b
JB
297(defun mouse-tear-off-window (click)
298 "Delete the window clicked on, and create a new frame displaying its buffer."
299 (interactive "e")
d65147f6 300 (mouse-minibuffer-check click)
b0f3a26b
JB
301 (let* ((window (posn-window (event-start click)))
302 (buf (window-buffer window))
01a911e3 303 (frame (make-frame)))
b0f3a26b
JB
304 (select-frame frame)
305 (switch-to-buffer buf)
306 (delete-window window)))
307
b5370f03 308(defun mouse-delete-other-windows ()
5925bb84 309 "Delete all windows except the one you click on."
b5370f03 310 (interactive "@")
cc0a8174 311 (delete-other-windows))
72ea54a4 312
cc0a8174
JB
313(defun mouse-split-window-vertically (click)
314 "Select Emacs window mouse is on, then split it vertically in half.
315The window is split at the line clicked on.
316This command must be bound to a mouse click."
947da0c4 317 (interactive "@e")
d65147f6 318 (mouse-minibuffer-check click)
b5370f03
JB
319 (let ((start (event-start click)))
320 (select-window (posn-window start))
85d6b80b 321 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
5ba2dc3f
JB
322 (first-line window-min-height)
323 (last-line (- (window-height) window-min-height)))
324 (if (< last-line first-line)
0a50b993 325 (error "Window too short to split")
5ba2dc3f
JB
326 (split-window-vertically
327 (min (max new-height first-line) last-line))))))
cc0a8174 328
947da0c4
RS
329(defun mouse-split-window-horizontally (click)
330 "Select Emacs window mouse is on, then split it horizontally in half.
331The window is split at the column clicked on.
332This command must be bound to a mouse click."
333 (interactive "@e")
d65147f6 334 (mouse-minibuffer-check click)
5ba2dc3f
JB
335 (let ((start (event-start click)))
336 (select-window (posn-window start))
337 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
338 (first-col window-min-width)
339 (last-col (- (window-width) window-min-width)))
340 (if (< last-col first-col)
0a50b993 341 (error "Window too narrow to split")
5ba2dc3f
JB
342 (split-window-horizontally
343 (min (max new-width first-col) last-col))))))
947da0c4 344
4a4fa24d
RS
345(defun mouse-drag-window-above (window)
346 "Return the (or a) window directly above WINDOW.
347That means one whose bottom edge is at the same height as WINDOW's top edge."
348 (let ((top (nth 1 (window-edges window)))
349 (start-window window)
350 above-window)
351 (setq window (previous-window window 0))
352 (while (and (not above-window) (not (eq window start-window)))
353 (if (= (+ (window-height window) (nth 1 (window-edges window)))
354 top)
355 (setq above-window window))
356 (setq window (previous-window window)))
357 above-window))
358
359(defun mouse-drag-move-window-bottom (window growth)
360 "Move the bottom of WINDOW up or down by GROWTH lines.
361Move it down if GROWTH is positive, or up if GROWTH is negative.
362If this would make WINDOW too short,
363shrink the window or windows above it to make room."
364 (let ((excess (- window-min-height (+ (window-height window) growth))))
365 ;; EXCESS is the number of lines we need to take from windows above.
366 (if (> excess 0)
367 ;; This can recursively shrink windows all the way up.
368 (let ((window-above (mouse-drag-window-above window)))
369 (if window-above
370 (mouse-drag-move-window-bottom window-above (- excess))))))
371 (save-selected-window
372 (select-window window)
373 (enlarge-window growth nil (> growth 0))))
374
7a892a8b
DP
375(defsubst mouse-drag-move-window-top (window growth)
376 "Move the top of WINDOW up or down by GROWTH lines.
377Move it down if GROWTH is positive, or up if GROWTH is negative.
378If this would make WINDOW too short, shrink the window or windows
379above it to make room."
380 ;; Moving the top of WINDOW is actually moving the bottom of the
381 ;; window above.
382 (let ((window-above (mouse-drag-window-above window)))
383 (and window-above
384 (mouse-drag-move-window-bottom window-above (- growth)))))
385
b0d22e20
GM
386(defun mouse-drag-mode-line-1 (start-event mode-line-p)
387 "Change the height of a window by dragging on the mode or header line.
388START-EVENT is the starting mouse-event of the drag action.
4a4fa24d 389MODE-LINE-P non-nil means dragging a mode line; nil means a header line."
33c448cd
RS
390 ;; Give temporary modes such as isearch a chance to turn off.
391 (run-hooks 'mouse-leave-buffer-hook)
ee0e2bdd
GM
392 (let* ((done nil)
393 (echo-keystrokes 0)
394 (start (event-start start-event))
395 (start-event-window (posn-window start))
396 (start-event-frame (window-frame start-event-window))
397 (start-nwindows (count-windows t))
398 (old-selected-window (selected-window))
399 (minibuffer (frame-parameter nil 'minibuffer))
400 should-enlarge-minibuffer event mouse y top bot edges wconfig growth)
544e7e73
RS
401 (track-mouse
402 (progn
403 ;; enlarge-window only works on the selected window, so
404 ;; we must select the window where the start event originated.
405 ;; unwind-protect will restore the old selected window later.
406 (select-window start-event-window)
7b611de0 407
544e7e73
RS
408 ;; if this is the bottommost ordinary window, then to
409 ;; move its modeline the minibuffer must be enlarged.
410 (setq should-enlarge-minibuffer
411 (and minibuffer
b0d22e20 412 mode-line-p
544e7e73
RS
413 (not (one-window-p t))
414 (= (nth 1 (window-edges minibuffer))
415 (nth 3 (window-edges)))))
7b611de0 416
544e7e73
RS
417 ;; loop reading events and sampling the position of
418 ;; the mouse.
419 (while (not done)
420 (setq event (read-event)
421 mouse (mouse-position))
7b611de0 422
544e7e73
RS
423 ;; do nothing if
424 ;; - there is a switch-frame event.
425 ;; - the mouse isn't in the frame that we started in
426 ;; - the mouse isn't in any Emacs frame
427 ;; drag if
428 ;; - there is a mouse-movement event
429 ;; - there is a scroll-bar-movement event
430 ;; (same as mouse movement for our purposes)
431 ;; quit if
432 ;; - there is a keyboard event or some other unknown event
433 ;; unknown event.
434 (cond ((integerp event)
435 (setq done t))
7b611de0 436
fbd8dc8a 437 ((memq (car event) '(switch-frame select-window))
544e7e73 438 nil)
7b611de0 439
ee0e2bdd
GM
440 ((not (memq (car event) '(mouse-movement scroll-bar-movement)))
441 (when (consp event)
442 (push event unread-command-events))
544e7e73 443 (setq done t))
7b611de0 444
544e7e73
RS
445 ((not (eq (car mouse) start-event-frame))
446 nil)
7b611de0 447
544e7e73
RS
448 ((null (car (cdr mouse)))
449 nil)
7b611de0 450
544e7e73
RS
451 (t
452 (setq y (cdr (cdr mouse))
453 edges (window-edges)
454 top (nth 1 edges)
455 bot (nth 3 edges))
7b611de0 456
544e7e73 457 ;; compute size change needed
b0d22e20 458 (cond (mode-line-p
b0d22e20 459 (setq growth (- y bot -1)))
739e92a7
EZ
460 (t ; header line
461 (when (< (- bot y) window-min-height)
462 (setq y (- bot window-min-height)))
463 ;; The window's top includes the header line!
464 (setq growth (- top y))))
b0d22e20 465 (setq wconfig (current-window-configuration))
7b611de0 466
951e0427 467 ;; Check for an error case.
ee0e2bdd
GM
468 (when (and (/= growth 0)
469 (not minibuffer)
470 (one-window-p t))
471 (error "Attempt to resize sole window"))
7b611de0 472
544e7e73
RS
473 ;; grow/shrink minibuffer?
474 (if should-enlarge-minibuffer
475 (progn
476 ;; yes. briefly select minibuffer so
477 ;; enlarge-window will affect the
478 ;; correct window.
479 (select-window minibuffer)
480 ;; scale back shrinkage if it would
481 ;; make the minibuffer less than 1
482 ;; line tall.
483 (if (and (> growth 0)
484 (< (- (window-height minibuffer)
485 growth)
486 1))
487 (setq growth (1- (window-height minibuffer))))
488 (enlarge-window (- growth))
489 (select-window start-event-window))
490 ;; no. grow/shrink the selected window
ee0e2bdd 491 ;(message "growth = %d" growth)
7a892a8b
DP
492 (if mode-line-p
493 (mouse-drag-move-window-bottom start-event-window growth)
494 (mouse-drag-move-window-top start-event-window growth)))
7b611de0 495
544e7e73
RS
496 ;; if this window's growth caused another
497 ;; window to be deleted because it was too
498 ;; short, rescind the change.
499 ;;
500 ;; if size change caused space to be stolen
501 ;; from a window above this one, rescind the
a45423d8 502 ;; change, but only if we didn't grow/shrink
544e7e73
RS
503 ;; the minibuffer. minibuffer size changes
504 ;; can cause all windows to shrink... no way
505 ;; around it.
ee0e2bdd
GM
506 (when (or (/= start-nwindows (count-windows t))
507 (and (not should-enlarge-minibuffer)
4a4fa24d 508 (> growth 0)
ee0e2bdd
GM
509 mode-line-p
510 (/= top (nth 1 (window-edges)))))
511 (set-window-configuration wconfig)))))))))
b0d22e20
GM
512
513(defun mouse-drag-mode-line (start-event)
514 "Change the height of a window by dragging on the mode line."
515 (interactive "e")
516 (mouse-drag-mode-line-1 start-event t))
517
518(defun mouse-drag-header-line (start-event)
ee0e2bdd
GM
519 "Change the height of a window by dragging on the header line.
520Windows whose header-lines are at the top of the frame cannot be
521resized by dragging their header-line."
b0d22e20 522 (interactive "e")
ee0e2bdd
GM
523 ;; Changing the window's size by dragging its header-line when the
524 ;; header-line is at the top of the frame is somewhat strange,
525 ;; because the header-line doesn't move, so don't do it.
526 (let* ((start (event-start start-event))
527 (window (posn-window start))
528 (frame (window-frame window))
529 (first-window (frame-first-window frame)))
530 (when (or (eq window first-window)
531 (= (nth 1 (window-edges window))
532 (nth 1 (window-edges first-window))))
533 (error "Cannot move header-line at the top of the frame"))
534 (mouse-drag-mode-line-1 start-event nil)))
b0d22e20 535
544e7e73 536\f
08a1c178
RS
537(defun mouse-drag-vertical-line (start-event)
538 "Change the width of a window by dragging on the vertical line."
539 (interactive "e")
540 ;; Give temporary modes such as isearch a chance to turn off.
541 (run-hooks 'mouse-leave-buffer-hook)
5d6a85e0
RS
542 (let* ((done nil)
543 (echo-keystrokes 0)
544 (start-event-frame (window-frame (car (car (cdr start-event)))))
545 (start-event-window (car (car (cdr start-event))))
546 (start-nwindows (count-windows t))
547 (old-selected-window (selected-window))
548 event mouse x left right edges wconfig growth
549 (which-side
550 (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
551 'right)))
08a1c178
RS
552 (if (one-window-p t)
553 (error "Attempt to resize sole ordinary window"))
4e363e7e
KH
554 (if (eq which-side 'right)
555 (if (= (nth 2 (window-edges start-event-window))
556 (frame-width start-event-frame))
557 (error "Attempt to drag rightmost scrollbar"))
558 (if (= (nth 0 (window-edges start-event-window)) 0)
559 (error "Attempt to drag leftmost scrollbar")))
08a1c178
RS
560 (track-mouse
561 (progn
562 ;; enlarge-window only works on the selected window, so
563 ;; we must select the window where the start event originated.
564 ;; unwind-protect will restore the old selected window later.
565 (select-window start-event-window)
566 ;; loop reading events and sampling the position of
567 ;; the mouse.
568 (while (not done)
569 (setq event (read-event)
570 mouse (mouse-position))
571 ;; do nothing if
572 ;; - there is a switch-frame event.
573 ;; - the mouse isn't in the frame that we started in
574 ;; - the mouse isn't in any Emacs frame
575 ;; drag if
576 ;; - there is a mouse-movement event
577 ;; - there is a scroll-bar-movement event
578 ;; (same as mouse movement for our purposes)
579 ;; quit if
580 ;; - there is a keyboard event or some other unknown event
581 ;; unknown event.
582 (cond ((integerp event)
583 (setq done t))
fbd8dc8a 584 ((memq (car event) '(switch-frame select-window))
08a1c178
RS
585 nil)
586 ((not (memq (car event)
587 '(mouse-movement scroll-bar-movement)))
588 (if (consp event)
589 (setq unread-command-events
590 (cons event unread-command-events)))
591 (setq done t))
592 ((not (eq (car mouse) start-event-frame))
593 nil)
594 ((null (car (cdr mouse)))
595 nil)
596 (t
b0ea497f
KH
597 (save-selected-window
598 ;; If the scroll bar is on the window's left,
599 ;; adjust the window on the left.
4e363e7e
KH
600 (unless (eq which-side 'right)
601 (select-window (previous-window)))
b0ea497f 602 (setq x (- (car (cdr mouse))
4e363e7e 603 (if (eq which-side 'right) 0 2))
b0ea497f
KH
604 edges (window-edges)
605 left (nth 0 edges)
606 right (nth 2 edges))
607 ;; scale back a move that would make the
608 ;; window too thin.
609 (if (< (- x left -1) window-min-width)
610 (setq x (+ left window-min-width -1)))
611 ;; compute size change needed
612 (setq growth (- x right -1)
613 wconfig (current-window-configuration))
614 (enlarge-window growth t)
615 ;; if this window's growth caused another
616 ;; window to be deleted because it was too
617 ;; thin, rescind the change.
618 ;;
619 ;; if size change caused space to be stolen
620 ;; from a window to the left of this one,
621 ;; rescind the change.
622 (if (or (/= start-nwindows (count-windows t))
623 (/= left (nth 0 (window-edges))))
624 (set-window-configuration wconfig))))))))))
08a1c178 625\f
2a5fa27b 626(defun mouse-set-point (event)
cc0a8174 627 "Move point to the position clicked on with the mouse.
2a5fa27b 628This should be bound to a mouse click event type."
ec558adc 629 (interactive "e")
d65147f6 630 (mouse-minibuffer-check event)
2a5fa27b
RS
631 ;; Use event-end in case called from mouse-drag-region.
632 ;; If EVENT is a click, event-end and event-start give same value.
541a44d2 633 (posn-set-point (event-end event)))
cc0a8174 634
e6c2f5d4
RS
635(defvar mouse-last-region-beg nil)
636(defvar mouse-last-region-end nil)
637(defvar mouse-last-region-tick nil)
638
639(defun mouse-region-match ()
640 "Return non-nil if there's an active region that was set with the mouse."
641 (and (mark t) mark-active
642 (eq mouse-last-region-beg (region-beginning))
643 (eq mouse-last-region-end (region-end))
644 (eq mouse-last-region-tick (buffer-modified-tick))))
645
652ccd35 646(defun mouse-set-region (click)
e37de120 647 "Set the region to the text dragged over, and copy to kill ring.
2a5fa27b 648This should be bound to a mouse drag event."
652ccd35 649 (interactive "e")
d65147f6 650 (mouse-minibuffer-check click)
652ccd35
RS
651 (let ((posn (event-start click))
652 (end (event-end click)))
653 (select-window (posn-window posn))
654 (if (numberp (posn-point posn))
655 (goto-char (posn-point posn)))
fcfc3c63 656 ;; If mark is highlighted, no need to bounce the cursor.
33a35434
KH
657 ;; On X, we highlight while dragging, thus once again no need to bounce.
658 (or transient-mark-mode
b86b9918 659 (memq (framep (selected-frame)) '(x pc w32))
fcfc3c63 660 (sit-for 1))
652ccd35 661 (push-mark)
1cc8a3f4 662 (set-mark (point))
652ccd35 663 (if (numberp (posn-point end))
e37de120
RS
664 (goto-char (posn-point end)))
665 ;; Don't set this-command to kill-region, so that a following
666 ;; C-w will not double the text in the kill ring.
91a6bc10 667 ;; Ignore last-command so we don't append to a preceding kill.
b2dae92a
KS
668 (when mouse-drag-copy-region
669 (let (this-command last-command deactivate-mark)
670 (copy-region-as-kill (mark) (point))))
e6c2f5d4
RS
671 (mouse-set-region-1)))
672
673(defun mouse-set-region-1 ()
a5809dbd 674 ;; Set transient-mark-mode for a little while.
cc47c660
RS
675 (if (memq transient-mark-mode '(nil identity))
676 (setq transient-mark-mode 'only))
e6c2f5d4
RS
677 (setq mouse-last-region-beg (region-beginning))
678 (setq mouse-last-region-end (region-end))
679 (setq mouse-last-region-tick (buffer-modified-tick)))
652ccd35 680
3b558d41 681(defcustom mouse-scroll-delay 0.25
600c6e3a
JB
682 "*The pause between scroll steps caused by mouse drags, in seconds.
683If you drag the mouse beyond the edge of a window, Emacs scrolls the
684window to bring the text beyond that edge into view, with a delay of
685this many seconds between scroll steps. Scrolling stops when you move
686the mouse back into the window, or release the button.
687This variable's value may be non-integral.
3b558d41
RS
688Setting this to zero causes Emacs to scroll as fast as it can."
689 :type 'number
690 :group 'mouse)
600c6e3a 691
3b558d41 692(defcustom mouse-scroll-min-lines 1
a3d6bb97
RS
693 "*The minimum number of lines scrolled by dragging mouse out of window.
694Moving the mouse out the top or bottom edge of the window begins
695scrolling repeatedly. The number of lines scrolled per repetition
696is normally equal to the number of lines beyond the window edge that
697the mouse has moved. However, it always scrolls at least the number
3b558d41
RS
698of lines specified by this variable."
699 :type 'integer
700 :group 'mouse)
08a1c178 701
e919a622
RS
702(defun mouse-scroll-subr (window jump &optional overlay start)
703 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
600c6e3a
JB
704If OVERLAY is an overlay, let it stretch from START to the far edge of
705the newly visible text.
706Upon exit, point is at the far edge of the newly visible text."
08a1c178
RS
707 (cond
708 ((and (> jump 0) (< jump mouse-scroll-min-lines))
709 (setq jump mouse-scroll-min-lines))
710 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
711 (setq jump (- mouse-scroll-min-lines))))
4e399a53
RS
712 (let ((opoint (point)))
713 (while (progn
714 (goto-char (window-start window))
715 (if (not (zerop (vertical-motion jump window)))
716 (progn
717 (set-window-start window (point))
718 (if (natnump jump)
0320e66f
RS
719 (if (window-end window)
720 (progn
721 (goto-char (window-end window))
722 ;; window-end doesn't reflect the window's new
723 ;; start position until the next redisplay.
724 (vertical-motion (1- jump) window))
725 (vertical-motion (- (window-height window) 2)))
4e399a53
RS
726 (goto-char (window-start window)))
727 (if overlay
728 (move-overlay overlay start (point)))
729 ;; Now that we have scrolled WINDOW properly,
730 ;; put point back where it was for the redisplay
731 ;; so that we don't mess up the selected window.
732 (or (eq window (selected-window))
733 (goto-char opoint))
d2287ded 734 (sit-for mouse-scroll-delay)))))
4e399a53
RS
735 (or (eq window (selected-window))
736 (goto-char opoint))))
fcfc3c63 737
dc269e81 738;; Create an overlay and immediately delete it, to get "overlay in no buffer".
600c6e3a 739(defvar mouse-drag-overlay (make-overlay 1 1))
dc269e81 740(delete-overlay mouse-drag-overlay)
600c6e3a
JB
741(overlay-put mouse-drag-overlay 'face 'region)
742
dd524dbd 743(defvar mouse-selection-click-count 0)
eb6ff46f 744
c8c5bd24
RS
745(defvar mouse-selection-click-count-buffer nil)
746
600c6e3a 747(defun mouse-drag-region (start-event)
bcd5aef1 748 "Set the region to the text that the mouse is dragged over.
78210c95
RS
749Highlight the drag area as you move the mouse.
750This must be bound to a button-down mouse event.
d017deb2 751In Transient Mark mode, the highlighting remains as long as the mark
6454615c
RS
752remains active. Otherwise, it remains until the next input event.
753
754If the click is in the echo area, display the `*Messages*' buffer."
bcd5aef1 755 (interactive "e")
fbd8dc8a
SM
756 (let ((w (posn-window (event-start start-event))))
757 (if (and (window-minibuffer-p w)
758 (not (minibuffer-window-active-p w)))
6454615c
RS
759 (save-excursion
760 (read-event)
761 (set-buffer "*Messages*")
762 (goto-char (point-max))
763 (display-buffer (current-buffer)))
764 ;; Give temporary modes such as isearch a chance to turn off.
765 (run-hooks 'mouse-leave-buffer-hook)
766 (mouse-drag-region-1 start-event))))
767
65f76581
KS
768
769(defun mouse-on-link-p (pos)
770 "Return non-nil if POS is on a link in the current buffer.
771
772A clickable link is identified by one of the following methods:
773
a0ec7b7f
KS
774If the character at POS has a non-nil `follow-link' text or
775overlay property, use the value of that property as action code,
776or if there is a local key-binding or a keybinding at position
777POS for the `follow-link' event, use the binding of that event as
778action code.
65f76581 779
a0ec7b7f 780The action code is used to determine whether POS is inside a link:
65f76581 781
a0ec7b7f 782- If the action code is `mouse-face', POS is inside a link if there
65f76581
KS
783is a non-nil `mouse-face' property at POS. Return t in this case.
784
a0ec7b7f 785- If the action code is a function, FUNC, POS is inside a link if
65f76581
KS
786the call \(FUNC POS) returns non-nil. Return the return value
787from that call.
788
a0ec7b7f 789- Otherwise, return the action code itself.
65f76581
KS
790
791The return value is interpreted as follows:
792
793- If it is a string, the mouse-1 event is translated into the
794first character of the string, i.e. the action of the mouse-1
795click is the local or global binding of that character.
796
797- If it is a vector, the mouse-1 event is translated into the
798first element of that vector, i.e. the action of the mouse-1
799click is the local or global binding of that event.
800
801- Otherwise, the mouse-1 event is translated into a mouse-2 event
802at the same position."
a0ec7b7f
KS
803 (let ((action
804 (or (get-char-property pos 'follow-link)
805 (save-excursion
806 (goto-char pos)
807 (key-binding [follow-link] nil t)))))
808 (cond
809 ((eq action 'mouse-face)
810 (and (get-char-property pos 'mouse-face) t))
811 ((functionp action)
812 (funcall action pos))
813 (t action))))
65f76581 814
6454615c 815(defun mouse-drag-region-1 (start-event)
d65147f6 816 (mouse-minibuffer-check start-event)
4c9afcbe
RS
817 (let* ((echo-keystrokes 0)
818 (start-posn (event-start start-event))
600c6e3a
JB
819 (start-point (posn-point start-posn))
820 (start-window (posn-window start-posn))
02beb936 821 (start-window-start (window-start start-window))
b846d039 822 (start-frame (window-frame start-window))
1853aa6b 823 (start-hscroll (window-hscroll start-window))
600c6e3a 824 (bounds (window-edges start-window))
8413d0d2 825 (make-cursor-line-fully-visible nil)
600c6e3a
JB
826 (top (nth 1 bounds))
827 (bottom (if (window-minibuffer-p start-window)
828 (nth 3 bounds)
829 ;; Don't count the mode line.
e37de120 830 (1- (nth 3 bounds))))
65f76581 831 on-link remap-double-click
e37de120 832 (click-count (1- (event-click-count start-event))))
eb6ff46f 833 (setq mouse-selection-click-count click-count)
c8c5bd24 834 (setq mouse-selection-click-count-buffer (current-buffer))
b80f1928 835 (mouse-set-point start-event)
08a1c178
RS
836 ;; In case the down click is in the middle of some intangible text,
837 ;; use the end of that text, and put it in START-POINT.
838 (if (< (point) start-point)
839 (goto-char start-point))
840 (setq start-point (point))
65f76581
KS
841 (setq on-link (and mouse-1-click-follows-link
842 (mouse-on-link-p start-point)))
843 (setq remap-double-click (and on-link
844 (eq mouse-1-click-follows-link 'double)
845 (= click-count 1)))
846 (if remap-double-click ;; Don't expand mouse overlay in links
847 (setq click-count 0))
e37de120
RS
848 (let ((range (mouse-start-end start-point start-point click-count)))
849 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
2d246c71
RS
850 (window-buffer start-window))
851 (overlay-put mouse-drag-overlay 'window (selected-window)))
f767385c 852 (deactivate-mark)
08a1c178
RS
853 ;; end-of-range is used only in the single-click case.
854 ;; It is the place where the drag has reached so far
855 ;; (but not outside the window where the drag started).
d5c847bb 856 (let (event end end-point last-end-point (end-of-range (point)))
bcd5aef1 857 (track-mouse
600c6e3a 858 (while (progn
b846d039
JB
859 (setq event (read-event))
860 (or (mouse-movement-p event)
fbd8dc8a
SM
861 (memq (car-safe event) '(switch-frame select-window))))
862 (if (memq (car-safe event) '(switch-frame select-window))
b846d039
JB
863 nil
864 (setq end (event-end event)
865 end-point (posn-point end))
3617aa76 866 (if (numberp end-point)
d5c847bb 867 (setq last-end-point end-point))
b846d039
JB
868
869 (cond
b846d039
JB
870 ;; Are we moving within the original window?
871 ((and (eq (posn-window end) start-window)
872 (integer-or-marker-p end-point))
08a1c178
RS
873 ;; Go to START-POINT first, so that when we move to END-POINT,
874 ;; if it's in the middle of intangible text,
875 ;; point jumps in the direction away from START-POINT.
876 (goto-char start-point)
b846d039 877 (goto-char end-point)
08a1c178
RS
878 (if (zerop (% click-count 3))
879 (setq end-of-range (point)))
e37de120
RS
880 (let ((range (mouse-start-end start-point (point) click-count)))
881 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
b846d039 882
50e527bc
KH
883 (t
884 (let ((mouse-row (cdr (cdr (mouse-position)))))
b846d039 885 (cond
50e527bc 886 ((null mouse-row))
b846d039 887 ((< mouse-row top)
e919a622 888 (mouse-scroll-subr start-window (- mouse-row top)
f9278a75
RS
889 mouse-drag-overlay start-point)
890 ;; Without this, point tends to jump back to the starting
891 ;; position where the mouse button was pressed down.
892 (setq end-of-range (overlay-start mouse-drag-overlay)))
d2287ded 893 ((>= mouse-row bottom)
e919a622 894 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
ce8c2809 895 mouse-drag-overlay start-point)
f9278a75 896 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
b2013aad 897
2e3329a5
RS
898 ;; In case we did not get a mouse-motion event
899 ;; for the final move of the mouse before a drag event
900 ;; pretend that we did get one.
901 (when (and (memq 'drag (event-modifiers (car-safe event)))
902 (setq end (event-end event)
903 end-point (posn-point end))
904 (eq (posn-window end) start-window)
905 (integer-or-marker-p end-point))
2e3329a5
RS
906 ;; Go to START-POINT first, so that when we move to END-POINT,
907 ;; if it's in the middle of intangible text,
908 ;; point jumps in the direction away from START-POINT.
909 (goto-char start-point)
910 (goto-char end-point)
911 (if (zerop (% click-count 3))
912 (setq end-of-range (point)))
913 (let ((range (mouse-start-end start-point (point) click-count)))
914 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
915
4e399a53 916 (if (consp event)
e37de120 917 (let ((fun (key-binding (vector (car event)))))
bfbcf12e
RS
918 ;; Run the binding of the terminating up-event, if possible.
919 ;; In the case of a multiple click, it gives the wrong results,
920 ;; because it would fail to set up a region.
bb3aa835
RS
921 (if (not (= (overlay-start mouse-drag-overlay)
922 (overlay-end mouse-drag-overlay)))
923 (let* ((stop-point
924 (if (numberp (posn-point (event-end event)))
925 (posn-point (event-end event))
926 last-end-point))
927 ;; The end that comes from where we ended the drag.
928 ;; Point goes here.
929 (region-termination
930 (if (and stop-point (< stop-point start-point))
931 (overlay-start mouse-drag-overlay)
44dc5252 932 (overlay-end mouse-drag-overlay)))
bb3aa835
RS
933 ;; The end that comes from where we started the drag.
934 ;; Mark goes there.
935 (region-commencement
936 (- (+ (overlay-end mouse-drag-overlay)
937 (overlay-start mouse-drag-overlay))
938 region-termination))
939 last-command this-command)
940 (push-mark region-commencement t t)
941 (goto-char region-termination)
1fa01bcd 942 ;; Don't let copy-region-as-kill set deactivate-mark.
b2dae92a
KS
943 (when mouse-drag-copy-region
944 (let (deactivate-mark)
945 (copy-region-as-kill (point) (mark t))))
bb3aa835
RS
946 (let ((buffer (current-buffer)))
947 (mouse-show-mark)
948 ;; mouse-show-mark can call read-event,
949 ;; and that means the Emacs server could switch buffers
7b611de0 950 ;; under us. If that happened,
bb3aa835
RS
951 ;; avoid trying to use the region.
952 (and (mark t) mark-active
953 (eq buffer (current-buffer))
954 (mouse-set-region-1))))
955 (delete-overlay mouse-drag-overlay)
956 ;; Run the binding of the terminating up-event.
f45aab65 957 (when (and (functionp fun)
02beb936
RS
958 (= start-hscroll (window-hscroll start-window))
959 ;; Don't run the up-event handler if the
960 ;; window start changed in a redisplay after
961 ;; the mouse-set-point for the down-mouse
962 ;; event at the beginning of this function.
963 ;; When the window start has changed, the
964 ;; up-mouse event will contain a different
965 ;; position due to the new window contents,
966 ;; and point is set again.
967 (or end-point
968 (= (window-start start-window)
969 start-window-start)))
65f76581
KS
970 (if (and on-link
971 (not end-point)
972 (consp event)
973 (or remap-double-click
974 (and
975 (not (eq mouse-1-click-follows-link 'double))
976 (= click-count 0)
977 (= (event-click-count event) 1)
978 (not (input-pending-p))
979 (or (not (integerp mouse-1-click-follows-link))
980 (let ((t0 (posn-timestamp (event-start start-event)))
981 (t1 (posn-timestamp (event-end event))))
982 (and (integerp t0) (integerp t1)
983 (if (> mouse-1-click-follows-link 0)
984 (<= (- t1 t0) mouse-1-click-follows-link)
985 (< (- t0 t1) mouse-1-click-follows-link)))))
986 (or (not double-click-time)
987 (sit-for 0 (if (integerp double-click-time)
988 double-click-time 500) t)))))
989 (if (or (vectorp on-link) (stringp on-link))
990 (setq event (aref on-link 0))
991 (setcar event 'mouse-2)))
1853aa6b
GM
992 (setq unread-command-events
993 (cons event unread-command-events)))))
e6c2f5d4 994 (delete-overlay mouse-drag-overlay)))))
e37de120
RS
995\f
996;; Commands to handle xterm-style multiple clicks.
600c6e3a 997
e37de120
RS
998(defun mouse-skip-word (dir)
999 "Skip over word, over whitespace, or over identical punctuation.
1000If DIR is positive skip forward; if negative, skip backward."
1001 (let* ((char (following-char))
1002 (syntax (char-to-string (char-syntax char))))
258e3295
KH
1003 (cond ((string= syntax "w")
1004 ;; Here, we can't use skip-syntax-forward/backward because
1005 ;; they don't pay attention to word-separating-categories,
1006 ;; and thus they will skip over a true word boundary. So,
1007 ;; we simularte the original behaviour by using
1008 ;; forward-word.
1009 (if (< dir 0)
1010 (if (not (looking-at "\\<"))
1011 (forward-word -1))
1012 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1013 (forward-word 1))))
1014 ((string= syntax " ")
08a1c178
RS
1015 (if (< dir 0)
1016 (skip-syntax-backward syntax)
1017 (skip-syntax-forward syntax)))
1018 ((string= syntax "_")
1019 (if (< dir 0)
1020 (skip-syntax-backward "w_")
1021 (skip-syntax-forward "w_")))
1022 ((< dir 0)
1023 (while (and (not (bobp)) (= (preceding-char) char))
1024 (forward-char -1)))
1025 (t
1026 (while (and (not (eobp)) (= (following-char) char))
1027 (forward-char 1))))))
e37de120 1028
eb6ff46f 1029(defun mouse-start-end (start end mode)
a7df580d
DL
1030"Return a list of region bounds based on START and END according to MODE.
1031If MODE is 0 then set point to (min START END), mark to (max START END).
1032If MODE is 1 then set point to start of word at (min START END),
1033mark to end of word at (max START END).
1034If MODE is 2 then do the same for lines."
e37de120
RS
1035 (if (> start end)
1036 (let ((temp start))
1037 (setq start end
1038 end temp)))
9a974c88 1039 (setq mode (mod mode 3))
e37de120
RS
1040 (cond ((= mode 0)
1041 (list start end))
1042 ((and (= mode 1)
1043 (= start end)
1ec71583 1044 (char-after start)
e37de120 1045 (= (char-syntax (char-after start)) ?\())
6f482eec
RS
1046 (list start
1047 (save-excursion
1048 (goto-char start)
1049 (forward-sexp 1)
1050 (point))))
e37de120
RS
1051 ((and (= mode 1)
1052 (= start end)
1ec71583 1053 (char-after start)
e37de120 1054 (= (char-syntax (char-after start)) ?\)))
7b611de0 1055 (list (save-excursion
e37de120 1056 (goto-char (1+ start))
d89a4a47
RS
1057 (backward-sexp 1)
1058 (point))
e37de120 1059 (1+ start)))
53770877
RS
1060 ((and (= mode 1)
1061 (= start end)
1062 (char-after start)
1063 (= (char-syntax (char-after start)) ?\"))
1064 (let ((open (or (eq start (point-min))
1065 (save-excursion
1066 (goto-char (- start 1))
1067 (looking-at "\\s(\\|\\s \\|\\s>")))))
1068 (if open
1069 (list start
1070 (save-excursion
1071 (condition-case nil
7b611de0 1072 (progn
53770877
RS
1073 (goto-char start)
1074 (forward-sexp 1)
1075 (point))
1076 (error end))))
f1017d55 1077 (list (save-excursion
53770877
RS
1078 (condition-case nil
1079 (progn
1080 (goto-char (1+ start))
1081 (backward-sexp 1)
1082 (point))
f1017d55
RS
1083 (error end)))
1084 (1+ start)))))
e37de120
RS
1085 ((= mode 1)
1086 (list (save-excursion
1087 (goto-char start)
1088 (mouse-skip-word -1)
1089 (point))
1090 (save-excursion
1091 (goto-char end)
1092 (mouse-skip-word 1)
1093 (point))))
1094 ((= mode 2)
1095 (list (save-excursion
1096 (goto-char start)
1097 (beginning-of-line 1)
1098 (point))
1099 (save-excursion
1100 (goto-char end)
1101 (forward-line 1)
1102 (point))))))
e66feb07 1103\f
3f26b32a
RS
1104;; Subroutine: set the mark where CLICK happened,
1105;; but don't do anything else.
1106(defun mouse-set-mark-fast (click)
d65147f6 1107 (mouse-minibuffer-check click)
3f26b32a
RS
1108 (let ((posn (event-start click)))
1109 (select-window (posn-window posn))
1110 (if (numberp (posn-point posn))
1111 (push-mark (posn-point posn) t t))))
1112
98aac09d
MB
1113(defun mouse-undouble-last-event (events)
1114 (let* ((index (1- (length events)))
1115 (last (nthcdr index events))
1116 (event (car last))
1117 (basic (event-basic-type event))
397a88f3
RS
1118 (old-modifiers (event-modifiers event))
1119 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
98aac09d
MB
1120 (new
1121 (if (consp event)
7132500b
RS
1122 ;; Use reverse, not nreverse, since event-modifiers
1123 ;; does not copy the list it returns.
3bb846b8 1124 (cons (event-convert-list (reverse (cons basic modifiers)))
98aac09d
MB
1125 (cdr event))
1126 event)))
1127 (setcar last new)
397a88f3
RS
1128 (if (and (not (equal modifiers old-modifiers))
1129 (key-binding (apply 'vector events)))
98aac09d
MB
1130 t
1131 (setcar last event)
1132 nil)))
1133
7b611de0 1134;; Momentarily show where the mark is, if highlighting doesn't show it.
7aeb7370 1135
88f20a3d 1136(defvar mouse-region-delete-keys '([delete] [deletechar])
7aeb7370
RS
1137 "List of keys which shall cause the mouse region to be deleted.")
1138
3f26b32a 1139(defun mouse-show-mark ()
d3d3d650
RS
1140 (let ((inhibit-quit t)
1141 (echo-keystrokes 0)
1142 event events key ignore
b10c429f 1143 (x-lost-selection-functions
ab60bd74
SS
1144 (when (boundp 'x-lost-selection-functions)
1145 (copy-sequence x-lost-selection-functions))))
1146 (add-hook 'x-lost-selection-functions
d3d3d650 1147 (lambda (seltype)
ab60bd74
SS
1148 (when (eq seltype 'PRIMARY)
1149 (setq ignore t)
1150 (throw 'mouse-show-mark t))))
d3d3d650
RS
1151 (if transient-mark-mode
1152 (delete-overlay mouse-drag-overlay)
1153 (move-overlay mouse-drag-overlay (point) (mark t)))
1154 (catch 'mouse-show-mark
1155 ;; In this loop, execute scroll bar and switch-frame events.
fbd8dc8a 1156 ;; Should we similarly handle `select-window' events? --Stef
d3d3d650
RS
1157 ;; Also ignore down-events that are undefined.
1158 (while (progn (setq event (read-event))
1159 (setq events (append events (list event)))
1160 (setq key (apply 'vector events))
1161 (or (and (consp event)
1162 (eq (car event) 'switch-frame))
1163 (and (consp event)
1164 (eq (posn-point (event-end event))
1165 'vertical-scroll-bar))
1166 (and (memq 'down (event-modifiers event))
1167 (not (key-binding key))
1168 (not (mouse-undouble-last-event events))
1169 (not (member key mouse-region-delete-keys)))))
1170 (and (consp event)
1171 (or (eq (car event) 'switch-frame)
1172 (eq (posn-point (event-end event))
1173 'vertical-scroll-bar))
1174 (let ((keys (vector 'vertical-scroll-bar event)))
1175 (and (key-binding keys)
1176 (progn
1177 (call-interactively (key-binding keys)
1178 nil keys)
1179 (setq events nil)))))))
1180 ;; If we lost the selection, just turn off the highlighting.
ab60bd74 1181 (unless ignore
d3d3d650
RS
1182 ;; For certain special keys, delete the region.
1183 (if (member key mouse-region-delete-keys)
9e6856a7 1184 (delete-region (mark t) (point))
d3d3d650
RS
1185 ;; Otherwise, unread the key so it gets executed normally.
1186 (setq unread-command-events
1187 (nconc events unread-command-events))))
1188 (setq quit-flag nil)
1189 (unless transient-mark-mode
f4b60fe6 1190 (delete-overlay mouse-drag-overlay))))
3f26b32a 1191
cc0a8174
JB
1192(defun mouse-set-mark (click)
1193 "Set mark at the position clicked on with the mouse.
1194Display cursor at that position for a second.
1195This must be bound to a mouse click."
ec558adc 1196 (interactive "e")
f598fb03
RS
1197 (mouse-minibuffer-check click)
1198 (select-window (posn-window (event-start click)))
1199 ;; We don't use save-excursion because that preserves the mark too.
72ea54a4
RS
1200 (let ((point-save (point)))
1201 (unwind-protect
cc0a8174 1202 (progn (mouse-set-point click)
897897e3
RS
1203 (push-mark nil t t)
1204 (or transient-mark-mode
1205 (sit-for 1)))
72ea54a4
RS
1206 (goto-char point-save))))
1207
cc0a8174
JB
1208(defun mouse-kill (click)
1209 "Kill the region between point and the mouse click.
1210The text is saved in the kill ring, as with \\[kill-region]."
ec558adc 1211 (interactive "e")
d65147f6 1212 (mouse-minibuffer-check click)
142c7672
KH
1213 (let* ((posn (event-start click))
1214 (click-posn (posn-point posn)))
1215 (select-window (posn-window posn))
bd307392
JB
1216 (if (numberp click-posn)
1217 (kill-region (min (point) click-posn)
1218 (max (point) click-posn)))))
72ea54a4 1219
87ef29fd
JB
1220(defun mouse-yank-at-click (click arg)
1221 "Insert the last stretch of killed text at the position clicked on.
0e57ab65 1222Also move point to one end of the text thus inserted (normally the end),
231c4d1a 1223and set mark at the beginning.
50f58001
RS
1224Prefix arguments are interpreted as with \\[yank].
1225If `mouse-yank-at-point' is non-nil, insert at point
1226regardless of where you click."
a4cabe41 1227 (interactive "e\nP")
33c448cd
RS
1228 ;; Give temporary modes such as isearch a chance to turn off.
1229 (run-hooks 'mouse-leave-buffer-hook)
50f58001 1230 (or mouse-yank-at-point (mouse-set-point click))
d89a4a47 1231 (setq this-command 'yank)
a814e9df 1232 (setq mouse-selection-click-count 0)
87ef29fd
JB
1233 (yank arg))
1234
1235(defun mouse-kill-ring-save (click)
cc0a8174
JB
1236 "Copy the region between point and the mouse click in the kill ring.
1237This does not delete the region; it acts like \\[kill-ring-save]."
ec558adc 1238 (interactive "e")
3f26b32a 1239 (mouse-set-mark-fast click)
6452d8a6
RS
1240 (let (this-command last-command)
1241 (kill-ring-save (point) (mark t)))
3f26b32a 1242 (mouse-show-mark))
72ea54a4 1243
dbc4e1c1
JB
1244;;; This function used to delete the text between point and the mouse
1245;;; whenever it was equal to the front of the kill ring, but some
1246;;; people found that confusing.
1247
1248;;; A list (TEXT START END), describing the text and position of the last
1249;;; invocation of mouse-save-then-kill.
1250(defvar mouse-save-then-kill-posn nil)
1251
26d280b9 1252(defun mouse-save-then-kill-delete-region (beg end)
9a974c88
RS
1253 ;; We must make our own undo boundaries
1254 ;; because they happen automatically only for the current buffer.
1255 (undo-boundary)
dd524dbd
RS
1256 (if (or (= beg end) (eq buffer-undo-list t))
1257 ;; If we have no undo list in this buffer,
1258 ;; just delete.
1259 (delete-region beg end)
1260 ;; Delete, but make the undo-list entry share with the kill ring.
1261 ;; First, delete just one char, so in case buffer is being modified
1262 ;; for the first time, the undo list records that fact.
8faa9707 1263 (let (before-change-functions after-change-functions)
2f4b15ef
RS
1264 (delete-region beg
1265 (+ beg (if (> end beg) 1 -1))))
dd524dbd
RS
1266 (let ((buffer-undo-list buffer-undo-list))
1267 ;; Undo that deletion--but don't change the undo list!
8faa9707 1268 (let (before-change-functions after-change-functions)
2f4b15ef 1269 (primitive-undo 1 buffer-undo-list))
dd524dbd
RS
1270 ;; Now delete the rest of the specified region,
1271 ;; but don't record it.
1272 (setq buffer-undo-list t)
9a974c88
RS
1273 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1274 (error "Lossage in mouse-save-then-kill-delete-region"))
dd524dbd
RS
1275 (delete-region beg end))
1276 (let ((tail buffer-undo-list))
1277 ;; Search back in buffer-undo-list for the string
1278 ;; that came from deleting one character.
1279 (while (and tail (not (stringp (car (car tail)))))
1280 (setq tail (cdr tail)))
1281 ;; Replace it with an entry for the entire deleted text.
1282 (and tail
9a974c88
RS
1283 (setcar tail (cons (car kill-ring) (min beg end))))))
1284 (undo-boundary))
eb6ff46f 1285
947da0c4 1286(defun mouse-save-then-kill (click)
40a45a9f
RS
1287 "Save text to point in kill ring; the second time, kill the text.
1288If the text between point and the mouse is the same as what's
1289at the front of the kill ring, this deletes the text.
1290Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
eb6ff46f
RS
1291which prepares for a second click to delete the text.
1292
1293If you have selected words or lines, this command extends the
1294selection through the word or line clicked on. If you do this
1295again in a different position, it extends the selection again.
7b611de0 1296If you do this twice in the same position, the selection is killed."
947da0c4 1297 (interactive "e")
53016bea
RS
1298 (let ((before-scroll
1299 (with-current-buffer (window-buffer (posn-window (event-start click)))
1300 point-before-scroll)))
b64548c7
RS
1301 (mouse-minibuffer-check click)
1302 (let ((click-posn (posn-point (event-start click)))
1303 ;; Don't let a subsequent kill command append to this one:
1304 ;; prevent setting this-command to kill-region.
1305 (this-command this-command))
c8c5bd24
RS
1306 (if (and (save-excursion
1307 (set-buffer (window-buffer (posn-window (event-start click))))
1308 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1309 ;; Don't be fooled by a recent click in some other buffer.
7b611de0 1310 (eq mouse-selection-click-count-buffer
c8c5bd24 1311 (current-buffer)))))
b64548c7
RS
1312 (if (not (and (eq last-command 'mouse-save-then-kill)
1313 (equal click-posn
1314 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1315 ;; Find both ends of the object selected by this click.
1316 (let* ((range
1317 (mouse-start-end click-posn click-posn
1318 mouse-selection-click-count)))
1319 ;; Move whichever end is closer to the click.
1320 ;; That's what xterm does, and it seems reasonable.
1321 (if (< (abs (- click-posn (mark t)))
1322 (abs (- click-posn (point))))
1323 (set-mark (car range))
1324 (goto-char (nth 1 range)))
1325 ;; We have already put the old region in the kill ring.
1326 ;; Replace it with the extended region.
1327 ;; (It would be annoying to make a separate entry.)
1328 (kill-new (buffer-substring (point) (mark t)) t)
e6c2f5d4 1329 (mouse-set-region-1)
b64548c7
RS
1330 ;; Arrange for a repeated mouse-3 to kill this region.
1331 (setq mouse-save-then-kill-posn
1332 (list (car kill-ring) (point) click-posn))
1333 (mouse-show-mark))
1334 ;; If we click this button again without moving it,
1335 ;; that time kill.
bcde3748 1336 (mouse-save-then-kill-delete-region (mark) (point))
b64548c7 1337 (setq mouse-selection-click-count 0)
dd524dbd 1338 (setq mouse-save-then-kill-posn nil))
b64548c7
RS
1339 (if (and (eq last-command 'mouse-save-then-kill)
1340 mouse-save-then-kill-posn
1341 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1342 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1343 ;; If this is the second time we've called
1344 ;; mouse-save-then-kill, delete the text from the buffer.
1345 (progn
1346 (mouse-save-then-kill-delete-region (point) (mark))
1347 ;; After we kill, another click counts as "the first time".
1348 (setq mouse-save-then-kill-posn nil))
c8f264ae
RS
1349 ;; This is not a repetition.
1350 ;; We are adjusting an old selection or creating a new one.
b64548c7
RS
1351 (if (or (and (eq last-command 'mouse-save-then-kill)
1352 mouse-save-then-kill-posn)
1353 (and mark-active transient-mark-mode)
1354 (and (memq last-command
1355 '(mouse-drag-region mouse-set-region))
1356 (or mark-even-if-inactive
1357 (not transient-mark-mode))))
1358 ;; We have a selection or suitable region, so adjust it.
1359 (let* ((posn (event-start click))
1360 (new (posn-point posn)))
1361 (select-window (posn-window posn))
1362 (if (numberp new)
1363 (progn
1364 ;; Move whichever end of the region is closer to the click.
1365 ;; That is what xterm does, and it seems reasonable.
ed784c53 1366 (if (<= (abs (- new (point))) (abs (- new (mark t))))
b64548c7
RS
1367 (goto-char new)
1368 (set-mark new))
1369 (setq deactivate-mark nil)))
bcde3748
RS
1370 (kill-new (buffer-substring (point) (mark t)) t)
1371 (mouse-show-mark))
b64548c7
RS
1372 ;; Set the mark where point is, then move where clicked.
1373 (mouse-set-mark-fast click)
1374 (if before-scroll
1375 (goto-char before-scroll))
1376 (exchange-point-and-mark)
c8f264ae 1377 (kill-new (buffer-substring (point) (mark t)))
d0d57043 1378 (mouse-show-mark))
e6c2f5d4 1379 (mouse-set-region-1)
b64548c7
RS
1380 (setq mouse-save-then-kill-posn
1381 (list (car kill-ring) (point) click-posn)))))))
e66feb07
RS
1382\f
1383(global-set-key [M-mouse-1] 'mouse-start-secondary)
1384(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1385(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1386(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
9a974c88 1387(global-set-key [M-mouse-2] 'mouse-yank-secondary)
e66feb07
RS
1388
1389;; An overlay which records the current secondary selection
1390;; or else is deleted when there is no secondary selection.
1391;; May be nil.
1392(defvar mouse-secondary-overlay nil)
1393
a94c7fcc
RS
1394(defvar mouse-secondary-click-count 0)
1395
e66feb07
RS
1396;; A marker which records the specified first end for a secondary selection.
1397;; May be nil.
1398(defvar mouse-secondary-start nil)
1399
1400(defun mouse-start-secondary (click)
1401 "Set one end of the secondary selection to the position clicked on.
1402Use \\[mouse-secondary-save-then-kill] to set the other end
1403and complete the secondary selection."
1404 (interactive "e")
d65147f6 1405 (mouse-minibuffer-check click)
e66feb07 1406 (let ((posn (event-start click)))
230aaa73
RS
1407 (save-excursion
1408 (set-buffer (window-buffer (posn-window posn)))
1409 ;; Cancel any preexisting secondary selection.
1410 (if mouse-secondary-overlay
1411 (delete-overlay mouse-secondary-overlay))
1412 (if (numberp (posn-point posn))
1413 (progn
1414 (or mouse-secondary-start
1415 (setq mouse-secondary-start (make-marker)))
1416 (move-marker mouse-secondary-start (posn-point posn)))))))
e66feb07
RS
1417
1418(defun mouse-set-secondary (click)
1419 "Set the secondary selection to the text that the mouse is dragged over.
1420This must be bound to a mouse drag event."
1421 (interactive "e")
d65147f6 1422 (mouse-minibuffer-check click)
e66feb07
RS
1423 (let ((posn (event-start click))
1424 beg
1425 (end (event-end click)))
230aaa73
RS
1426 (save-excursion
1427 (set-buffer (window-buffer (posn-window posn)))
1428 (if (numberp (posn-point posn))
1429 (setq beg (posn-point posn)))
1430 (if mouse-secondary-overlay
1431 (move-overlay mouse-secondary-overlay beg (posn-point end))
1432 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1433 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
947da0c4 1434
d89a4a47 1435(defun mouse-drag-secondary (start-event)
e66feb07 1436 "Set the secondary selection to the text that the mouse is dragged over.
d89a4a47 1437Highlight the drag area as you move the mouse.
efc10c97
RS
1438This must be bound to a button-down mouse event.
1439The function returns a non-nil value if it creates a secondary selection."
e66feb07 1440 (interactive "e")
d65147f6 1441 (mouse-minibuffer-check start-event)
4c9afcbe
RS
1442 (let* ((echo-keystrokes 0)
1443 (start-posn (event-start start-event))
d89a4a47
RS
1444 (start-point (posn-point start-posn))
1445 (start-window (posn-window start-posn))
1446 (start-frame (window-frame start-window))
1447 (bounds (window-edges start-window))
1448 (top (nth 1 bounds))
1449 (bottom (if (window-minibuffer-p start-window)
1450 (nth 3 bounds)
1451 ;; Don't count the mode line.
1452 (1- (nth 3 bounds))))
1453 (click-count (1- (event-click-count start-event))))
1454 (save-excursion
1455 (set-buffer (window-buffer start-window))
a94c7fcc 1456 (setq mouse-secondary-click-count click-count)
d89a4a47
RS
1457 (or mouse-secondary-overlay
1458 (setq mouse-secondary-overlay
1459 (make-overlay (point) (point))))
26d280b9 1460 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
9a974c88 1461 (if (> (mod click-count 3) 0)
26d280b9
RS
1462 ;; Double or triple press: make an initial selection
1463 ;; of one word or line.
d89a4a47
RS
1464 (let ((range (mouse-start-end start-point start-point click-count)))
1465 (set-marker mouse-secondary-start nil)
1466 (move-overlay mouse-secondary-overlay 1 1
1467 (window-buffer start-window))
1468 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1469 (window-buffer start-window)))
26d280b9 1470 ;; Single-press: cancel any preexisting secondary selection.
d89a4a47
RS
1471 (or mouse-secondary-start
1472 (setq mouse-secondary-start (make-marker)))
1473 (set-marker mouse-secondary-start start-point)
1474 (delete-overlay mouse-secondary-overlay))
1475 (let (event end end-point)
1476 (track-mouse
1477 (while (progn
1478 (setq event (read-event))
1479 (or (mouse-movement-p event)
fbd8dc8a 1480 (memq (car-safe event) '(switch-frame select-window))))
d89a4a47 1481
fbd8dc8a 1482 (if (memq (car-safe event) '(switch-frame select-window))
d89a4a47
RS
1483 nil
1484 (setq end (event-end event)
1485 end-point (posn-point end))
1486 (cond
d89a4a47
RS
1487 ;; Are we moving within the original window?
1488 ((and (eq (posn-window end) start-window)
1489 (integer-or-marker-p end-point))
d89a4a47
RS
1490 (let ((range (mouse-start-end start-point end-point
1491 click-count)))
0136e1e3
RS
1492 (if (or (/= start-point end-point)
1493 (null (marker-position mouse-secondary-start)))
1494 (progn
1495 (set-marker mouse-secondary-start nil)
1496 (move-overlay mouse-secondary-overlay
1497 (car range) (nth 1 range))))))
3b0aebe9
RS
1498 (t
1499 (let ((mouse-row (cdr (cdr (mouse-position)))))
1500 (cond
1501 ((null mouse-row))
1502 ((< mouse-row top)
e919a622
RS
1503 (mouse-scroll-subr start-window (- mouse-row top)
1504 mouse-secondary-overlay start-point))
d2287ded 1505 ((>= mouse-row bottom)
e919a622 1506 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
3b0aebe9 1507 mouse-secondary-overlay start-point)))))))))
d89a4a47 1508
4e399a53 1509 (if (consp event)
d89a4a47
RS
1510 (if (marker-position mouse-secondary-start)
1511 (save-window-excursion
1512 (delete-overlay mouse-secondary-overlay)
9a974c88 1513 (x-set-selection 'SECONDARY nil)
d89a4a47
RS
1514 (select-window start-window)
1515 (save-excursion
1516 (goto-char mouse-secondary-start)
efc10c97
RS
1517 (sit-for 1)
1518 nil))
9a974c88
RS
1519 (x-set-selection
1520 'SECONDARY
1521 (buffer-substring (overlay-start mouse-secondary-overlay)
1522 (overlay-end mouse-secondary-overlay)))))))))
e66feb07 1523
9a974c88 1524(defun mouse-yank-secondary (click)
50f58001
RS
1525 "Insert the secondary selection at the position clicked on.
1526Move point to the end of the inserted text.
1527If `mouse-yank-at-point' is non-nil, insert at point
1528regardless of where you click."
a4cabe41 1529 (interactive "e")
33c448cd
RS
1530 ;; Give temporary modes such as isearch a chance to turn off.
1531 (run-hooks 'mouse-leave-buffer-hook)
50f58001
RS
1532 (or mouse-yank-at-point (mouse-set-point click))
1533 (insert (x-get-selection 'SECONDARY)))
9a974c88 1534
7e6404f6 1535(defun mouse-kill-secondary ()
9a974c88
RS
1536 "Kill the text in the secondary selection.
1537This is intended more as a keyboard command than as a mouse command
1538but it can work as either one.
1539
1540The current buffer (in case of keyboard use), or the buffer clicked on,
1541must be the one that the secondary selection is in. This requirement
1542is to prevent accidents."
7e6404f6
RS
1543 (interactive)
1544 (let* ((keys (this-command-keys))
1545 (click (elt keys (1- (length keys)))))
1546 (or (eq (overlay-buffer mouse-secondary-overlay)
1547 (if (listp click)
1548 (window-buffer (posn-window (event-start click)))
1549 (current-buffer)))
1550 (error "Select or click on the buffer where the secondary selection is")))
9dfab550
RS
1551 (let (this-command)
1552 (save-excursion
1553 (set-buffer (overlay-buffer mouse-secondary-overlay))
1554 (kill-region (overlay-start mouse-secondary-overlay)
1555 (overlay-end mouse-secondary-overlay))))
e66feb07 1556 (delete-overlay mouse-secondary-overlay)
9dfab550 1557;;; (x-set-selection 'SECONDARY nil)
e66feb07
RS
1558 (setq mouse-secondary-overlay nil))
1559
1560(defun mouse-secondary-save-then-kill (click)
d89a4a47 1561 "Save text to point in kill ring; the second time, kill the text.
7bbe2cc7
RS
1562You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1563If the text between where you did \\[mouse-start-secondary] and where
1564you use this command matches the text at the front of the kill ring,
1565this command deletes the text.
e66feb07 1566Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
7bbe2cc7 1567which prepares for a second click with this command to delete the text.
d89a4a47 1568
7bbe2cc7
RS
1569If you have already made a secondary selection in that buffer,
1570this command extends or retracts the selection to where you click.
1571If you do this again in a different position, it extends or retracts
1572again. If you do this twice in the same position, it kills the selection."
e66feb07 1573 (interactive "e")
d65147f6 1574 (mouse-minibuffer-check click)
d89a4a47
RS
1575 (let ((posn (event-start click))
1576 (click-posn (posn-point (event-start click)))
e66feb07
RS
1577 ;; Don't let a subsequent kill command append to this one:
1578 ;; prevent setting this-command to kill-region.
1579 (this-command this-command))
9a974c88
RS
1580 (or (eq (window-buffer (posn-window posn))
1581 (or (and mouse-secondary-overlay
1582 (overlay-buffer mouse-secondary-overlay))
1583 (if mouse-secondary-start
1584 (marker-buffer mouse-secondary-start))))
1585 (error "Wrong buffer"))
1586 (save-excursion
1587 (set-buffer (window-buffer (posn-window posn)))
a94c7fcc 1588 (if (> (mod mouse-secondary-click-count 3) 0)
9a974c88
RS
1589 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1590 (equal click-posn
1591 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1592 ;; Find both ends of the object selected by this click.
1593 (let* ((range
1594 (mouse-start-end click-posn click-posn
a94c7fcc 1595 mouse-secondary-click-count)))
9a974c88
RS
1596 ;; Move whichever end is closer to the click.
1597 ;; That's what xterm does, and it seems reasonable.
1598 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1599 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1600 (move-overlay mouse-secondary-overlay (car range)
1601 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1602 (move-overlay mouse-secondary-overlay
1603 (overlay-start mouse-secondary-overlay)
1604 (nth 1 range)))
9a974c88
RS
1605 ;; We have already put the old region in the kill ring.
1606 ;; Replace it with the extended region.
1607 ;; (It would be annoying to make a separate entry.)
f69140fd
KH
1608 (kill-new (buffer-substring
1609 (overlay-start mouse-secondary-overlay)
1610 (overlay-end mouse-secondary-overlay)) t)
9a974c88
RS
1611 ;; Arrange for a repeated mouse-3 to kill this region.
1612 (setq mouse-save-then-kill-posn
1613 (list (car kill-ring) (point) click-posn)))
1614 ;; If we click this button again without moving it,
1615 ;; that time kill.
d89a4a47 1616 (progn
9a974c88
RS
1617 (mouse-save-then-kill-delete-region
1618 (overlay-start mouse-secondary-overlay)
1619 (overlay-end mouse-secondary-overlay))
1620 (setq mouse-save-then-kill-posn nil)
a94c7fcc 1621 (setq mouse-secondary-click-count 0)
9a974c88
RS
1622 (delete-overlay mouse-secondary-overlay)))
1623 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1624 mouse-save-then-kill-posn
1625 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1626 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1627 ;; If this is the second time we've called
1628 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1629 (progn
1630 (mouse-save-then-kill-delete-region
1631 (overlay-start mouse-secondary-overlay)
1632 (overlay-end mouse-secondary-overlay))
1633 (setq mouse-save-then-kill-posn nil)
1634 (delete-overlay mouse-secondary-overlay))
1635 (if (overlay-start mouse-secondary-overlay)
1636 ;; We have a selection, so adjust it.
1637 (progn
1638 (if (numberp click-posn)
1639 (progn
1640 ;; Move whichever end of the region is closer to the click.
1641 ;; That is what xterm does, and it seems reasonable.
1642 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1643 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1644 (move-overlay mouse-secondary-overlay click-posn
1645 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1646 (move-overlay mouse-secondary-overlay
1647 (overlay-start mouse-secondary-overlay)
1648 click-posn))
9a974c88 1649 (setq deactivate-mark nil)))
0136e1e3 1650 (if (eq last-command 'mouse-secondary-save-then-kill)
7b611de0 1651 ;; If the front of the kill ring comes from
f69140fd
KH
1652 ;; an immediately previous use of this command,
1653 ;; replace it with the extended region.
1654 ;; (It would be annoying to make a separate entry.)
1655 (kill-new (buffer-substring
0136e1e3 1656 (overlay-start mouse-secondary-overlay)
f69140fd 1657 (overlay-end mouse-secondary-overlay)) t)
1fa01bcd
RS
1658 (let (deactivate-mark)
1659 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1660 (overlay-end mouse-secondary-overlay)))))
9a974c88
RS
1661 (if mouse-secondary-start
1662 ;; All we have is one end of a selection,
1663 ;; so put the other end here.
1664 (let ((start (+ 0 mouse-secondary-start)))
1665 (kill-ring-save start click-posn)
1666 (if mouse-secondary-overlay
1667 (move-overlay mouse-secondary-overlay start click-posn)
1668 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1669 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1670 (setq mouse-save-then-kill-posn
1671 (list (car kill-ring) (point) click-posn))))
fb28fd5b
RS
1672 (if (overlay-buffer mouse-secondary-overlay)
1673 (x-set-selection 'SECONDARY
9a974c88
RS
1674 (buffer-substring
1675 (overlay-start mouse-secondary-overlay)
1676 (overlay-end mouse-secondary-overlay)))))))
e66feb07 1677\f
0d9be91c 1678(defcustom mouse-buffer-menu-maxlen 20
f1107960
RS
1679 "*Number of buffers in one pane (submenu) of the buffer menu.
1680If we have lots of buffers, divide them into groups of
0d9be91c 1681`mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
3b558d41
RS
1682 :type 'integer
1683 :group 'mouse)
f1107960 1684
3ca87c7b
RS
1685(defcustom mouse-buffer-menu-mode-mult 4
1686 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1687This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1688will split the buffer menu by the major modes (see
1689`mouse-buffer-menu-mode-groups') or just by menu length.
1690Set to 1 (or even 0!) if you want to group by major mode always, and to
1691a large number if you prefer a mixed multitude. The default is 4."
1692 :type 'integer
1693 :group 'mouse
1694 :version "20.3")
1695
284a88a3
RS
1696(defvar mouse-buffer-menu-mode-groups
1697 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1698 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1699 . "Mail/News")
1700 ("\\<C\\>" . "C")
1701 ("ObjC" . "C")
1702 ("Text" . "Text")
1703 ("Outline" . "Text")
c71a58a3
SS
1704 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1705 ("log\\|diff\\|vc\\|cvs" . "Version Control") ; "Change Management"?
284a88a3
RS
1706 ("Lisp" . "Lisp"))
1707 "How to group various major modes together in \\[mouse-buffer-menu].
1708Each element has the form (REGEXP . GROUPNAME).
1709If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1710
8b34e79d 1711(defun mouse-buffer-menu (event)
2d82f7b9
RS
1712 "Pop up a menu of buffers for selection with the mouse.
1713This switches buffers in the window that you clicked on,
1714and selects that window."
ec558adc 1715 (interactive "e")
d65147f6 1716 (mouse-minibuffer-check event)
3ca87c7b 1717 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
284a88a3
RS
1718 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1719 (let ((tail buffers))
1720 (while tail
1721 ;; Divide all buffers into buckets for various major modes.
1722 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1723 (with-current-buffer (car tail)
1724 (let* ((adjusted-major-mode major-mode) elt)
1725 (let ((tail mouse-buffer-menu-mode-groups))
1726 (while tail
1727 (if (string-match (car (car tail)) mode-name)
1728 (setq adjusted-major-mode (cdr (car tail))))
1729 (setq tail (cdr tail))))
1730 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1731 (if (null elt)
1732 (setq elt (list adjusted-major-mode
1733 (if (stringp adjusted-major-mode)
1734 adjusted-major-mode
1735 mode-name))
1736 split-by-major-mode (cons elt split-by-major-mode)))
1737 (or (memq (car tail) (cdr (cdr elt)))
1738 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1739 (setq tail (cdr tail))))
1740 ;; Compute the sum of squares of sizes of the major-mode buckets.
1741 (let ((tail split-by-major-mode))
1742 (setq sum-of-squares 0)
1743 (while tail
1744 (setq sum-of-squares
1745 (+ sum-of-squares
3ca87c7b 1746 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
284a88a3 1747 (setq tail (cdr tail))))
3ca87c7b
RS
1748 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1749 (* (length buffers) (length buffers)))
284a88a3
RS
1750 ;; Subdividing by major modes really helps, so let's do it.
1751 (let (subdivided-menus (buffers-left (length buffers)))
1752 ;; Sort the list to put the most popular major modes first.
1753 (setq split-by-major-mode
1754 (sort split-by-major-mode
1755 (function (lambda (elt1 elt2)
1756 (> (length elt1) (length elt2))))))
1757 ;; Make a separate submenu for each major mode
1758 ;; that has more than one buffer,
1759 ;; unless all the remaining buffers are less than 1/10 of them.
1760 (while (and split-by-major-mode
1761 (and (> (length (car split-by-major-mode)) 3)
1762 (> (* buffers-left 10) (length buffers))))
4b4ea1dc
EZ
1763 (let ((this-mode-list (mouse-buffer-menu-alist
1764 (cdr (cdr (car split-by-major-mode))))))
1765 (and this-mode-list
1766 (setq subdivided-menus
1767 (cons (cons
1768 (nth 1 (car split-by-major-mode))
1769 this-mode-list)
1770 subdivided-menus))))
284a88a3
RS
1771 (setq buffers-left
1772 (- buffers-left (length (cdr (car split-by-major-mode)))))
1773 (setq split-by-major-mode (cdr split-by-major-mode)))
1774 ;; If any major modes are left over,
1775 ;; make a single submenu for them.
1776 (if split-by-major-mode
4b4ea1dc
EZ
1777 (let ((others-list
1778 (mouse-buffer-menu-alist
1779 ;; we don't need split-by-major-mode any more,
1780 ;; so we can ditch it with nconc.
1781 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1782 (and others-list
1783 (setq subdivided-menus
1784 (cons (cons "Others" others-list)
1785 subdivided-menus)))))
3ca87c7b 1786 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
284a88a3
RS
1787 (progn
1788 (setq alist (mouse-buffer-menu-alist buffers))
1789 (setq menu (cons "Buffer Menu"
1790 (mouse-buffer-menu-split "Select Buffer" alist)))))
2d82f7b9
RS
1791 (let ((buf (x-popup-menu event menu))
1792 (window (posn-window (event-start event))))
3ca87c7b 1793 (when buf
f134ad18
RS
1794 (select-window
1795 (if (framep window) (frame-selected-window window)
1796 window))
3ca87c7b 1797 (switch-to-buffer buf)))))
284a88a3
RS
1798
1799(defun mouse-buffer-menu-alist (buffers)
1800 (let (tail
1801 (maxlen 0)
1802 head)
1803 (setq buffers
1804 (sort buffers
1805 (function (lambda (elt1 elt2)
1806 (string< (buffer-name elt1) (buffer-name elt2))))))
1807 (setq tail buffers)
1808 (while tail
1809 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1810 (setq maxlen
1811 (max maxlen
1812 (length (buffer-name (car tail))))))
1813 (setq tail (cdr tail)))
1814 (setq tail buffers)
1815 (while tail
1816 (let ((elt (car tail)))
1817 (if (/= (aref (buffer-name elt) 0) ?\ )
1818 (setq head
1819 (cons
1820 (cons
1821 (format
1822 (format "%%%ds %%s%%s %%s" maxlen)
1823 (buffer-name elt)
1824 (if (buffer-modified-p elt) "*" " ")
1825 (save-excursion
1826 (set-buffer elt)
1827 (if buffer-read-only "%" " "))
7b611de0 1828 (or (buffer-file-name elt)
284a88a3
RS
1829 (save-excursion
1830 (set-buffer elt)
1831 (if list-buffers-directory
1832 (expand-file-name
1833 list-buffers-directory)))
1834 ""))
1835 elt)
1836 head))))
1837 (setq tail (cdr tail)))
1838 ;; Compensate for the reversal that the above loop does.
1839 (nreverse head)))
1840
1841(defun mouse-buffer-menu-split (title alist)
1842 ;; If we have lots of buffers, divide them into groups of 20
1843 ;; and make a pane (or submenu) for each one.
0d9be91c 1844 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
284a88a3
RS
1845 (let ((alist alist) sublists next
1846 (i 1))
1847 (while alist
0d9be91c 1848 ;; Pull off the next mouse-buffer-menu-maxlen buffers
284a88a3 1849 ;; and make them the next element of sublist.
0d9be91c 1850 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
284a88a3 1851 (if next
0d9be91c 1852 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
284a88a3
RS
1853 nil))
1854 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1855 sublists))
1856 (setq i (1+ i))
1857 (setq alist next))
1858 (nreverse sublists))
1859 ;; Few buffers--put them all in one pane.
1860 (list (cons title alist))))
72ea54a4 1861\f
5ba2dc3f 1862;;; These need to be rewritten for the new scroll bar implementation.
dbc4e1c1
JB
1863
1864;;;!! ;; Commands for the scroll bar.
7b611de0 1865;;;!!
dbc4e1c1
JB
1866;;;!! (defun mouse-scroll-down (click)
1867;;;!! (interactive "@e")
1868;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
7b611de0 1869;;;!!
dbc4e1c1
JB
1870;;;!! (defun mouse-scroll-up (click)
1871;;;!! (interactive "@e")
1872;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
7b611de0 1873;;;!!
dbc4e1c1
JB
1874;;;!! (defun mouse-scroll-down-full ()
1875;;;!! (interactive "@")
1876;;;!! (scroll-down nil))
7b611de0 1877;;;!!
dbc4e1c1
JB
1878;;;!! (defun mouse-scroll-up-full ()
1879;;;!! (interactive "@")
1880;;;!! (scroll-up nil))
7b611de0 1881;;;!!
dbc4e1c1
JB
1882;;;!! (defun mouse-scroll-move-cursor (click)
1883;;;!! (interactive "@e")
1884;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
7b611de0 1885;;;!!
dbc4e1c1
JB
1886;;;!! (defun mouse-scroll-absolute (event)
1887;;;!! (interactive "@e")
1888;;;!! (let* ((pos (car event))
1889;;;!! (position (car pos))
1890;;;!! (length (car (cdr pos))))
1891;;;!! (if (<= length 0) (setq length 1))
1892;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1893;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1894;;;!! position)
1895;;;!! length)
1896;;;!! scale-factor)))
1897;;;!! (goto-char newpos)
1898;;;!! (recenter '(4)))))
7b611de0 1899;;;!!
dbc4e1c1
JB
1900;;;!! (defun mouse-scroll-left (click)
1901;;;!! (interactive "@e")
1902;;;!! (scroll-left (1+ (car (mouse-coords click)))))
7b611de0 1903;;;!!
dbc4e1c1
JB
1904;;;!! (defun mouse-scroll-right (click)
1905;;;!! (interactive "@e")
1906;;;!! (scroll-right (1+ (car (mouse-coords click)))))
7b611de0 1907;;;!!
dbc4e1c1
JB
1908;;;!! (defun mouse-scroll-left-full ()
1909;;;!! (interactive "@")
1910;;;!! (scroll-left nil))
7b611de0 1911;;;!!
dbc4e1c1
JB
1912;;;!! (defun mouse-scroll-right-full ()
1913;;;!! (interactive "@")
1914;;;!! (scroll-right nil))
7b611de0 1915;;;!!
dbc4e1c1
JB
1916;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1917;;;!! (interactive "@e")
1918;;;!! (move-to-column (1+ (car (mouse-coords click)))))
7b611de0 1919;;;!!
dbc4e1c1
JB
1920;;;!! (defun mouse-scroll-absolute-horizontally (event)
1921;;;!! (interactive "@e")
1922;;;!! (let* ((pos (car event))
1923;;;!! (position (car pos))
1924;;;!! (length (car (cdr pos))))
1925;;;!! (set-window-hscroll (selected-window) 33)))
7b611de0 1926;;;!!
dbc4e1c1
JB
1927;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1928;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1929;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
7b611de0 1930;;;!!
dbc4e1c1
JB
1931;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1932;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1933;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
7b611de0 1934;;;!!
dbc4e1c1
JB
1935;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1936;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1937;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
7b611de0 1938;;;!!
dbc4e1c1
JB
1939;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1940;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1941;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
7b611de0 1942;;;!!
dbc4e1c1
JB
1943;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1944;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1945;;;!! 'mouse-scroll-absolute-horizontally)
1946;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
7b611de0 1947;;;!!
dbc4e1c1
JB
1948;;;!! (global-set-key [horizontal-slider mouse-1]
1949;;;!! 'mouse-scroll-move-cursor-horizontally)
1950;;;!! (global-set-key [horizontal-slider mouse-2]
1951;;;!! 'mouse-scroll-move-cursor-horizontally)
1952;;;!! (global-set-key [horizontal-slider mouse-3]
1953;;;!! 'mouse-scroll-move-cursor-horizontally)
7b611de0 1954;;;!!
dbc4e1c1
JB
1955;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1956;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1957;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
7b611de0 1958;;;!!
dbc4e1c1
JB
1959;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1960;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1961;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
7b611de0 1962;;;!!
dbc4e1c1
JB
1963;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1964;;;!! 'mouse-split-window-horizontally)
1965;;;!! (global-set-key [mode-line S-mouse-2]
1966;;;!! 'mouse-split-window-horizontally)
1967;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1968;;;!! 'mouse-split-window)
6b2154de 1969\f
dbc4e1c1
JB
1970;;;!! ;;;;
1971;;;!! ;;;; Here are experimental things being tested. Mouse events
1972;;;!! ;;;; are of the form:
1973;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1974;;;!! ;;
1975;;;!! ;;;;
1976;;;!! ;;;; Dynamically track mouse coordinates
1977;;;!! ;;;;
1978;;;!! ;;
1979;;;!! ;;(defun track-mouse (event)
1980;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1981;;;!! ;; (interactive "@e")
1982;;;!! ;; (while mouse-grabbed
1983;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1984;;;!! ;; (abs-x (car pos))
1985;;;!! ;; (abs-y (cdr pos))
1986;;;!! ;; (relative-coordinate (coordinates-in-window-p
1987;;;!! ;; (list (car pos) (cdr pos))
1988;;;!! ;; (selected-window))))
1989;;;!! ;; (if (consp relative-coordinate)
1990;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1991;;;!! ;; (car relative-coordinate)
1992;;;!! ;; (car (cdr relative-coordinate)))
1993;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
7b611de0 1994;;;!!
dbc4e1c1
JB
1995;;;!! ;;
1996;;;!! ;; Dynamically put a box around the line indicated by point
1997;;;!! ;;
1998;;;!! ;;
1999;;;!! ;;(require 'backquote)
2000;;;!! ;;
2001;;;!! ;;(defun mouse-select-buffer-line (event)
2002;;;!! ;; (interactive "@e")
2003;;;!! ;; (let ((relative-coordinate
2004;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
2005;;;!! ;; (abs-y (car (cdr (car event)))))
2006;;;!! ;; (if (consp relative-coordinate)
2007;;;!! ;; (progn
2008;;;!! ;; (save-excursion
2009;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2010;;;!! ;; (x-draw-rectangle
2011;;;!! ;; (selected-screen)
2012;;;!! ;; abs-y 0
2013;;;!! ;; (save-excursion
2014;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2015;;;!! ;; (end-of-line)
2016;;;!! ;; (push-mark nil t)
2017;;;!! ;; (beginning-of-line)
2018;;;!! ;; (- (region-end) (region-beginning))) 1))
2019;;;!! ;; (sit-for 1)
2020;;;!! ;; (x-erase-rectangle (selected-screen))))))
2021;;;!! ;;
2022;;;!! ;;(defvar last-line-drawn nil)
2023;;;!! ;;(defvar begin-delim "[^ \t]")
2024;;;!! ;;(defvar end-delim "[^ \t]")
2025;;;!! ;;
2026;;;!! ;;(defun mouse-boxing (event)
2027;;;!! ;; (interactive "@e")
2028;;;!! ;; (save-excursion
2029;;;!! ;; (let ((screen (selected-screen)))
2030;;;!! ;; (while (= (x-mouse-events) 0)
2031;;;!! ;; (let* ((pos (read-mouse-position screen))
2032;;;!! ;; (abs-x (car pos))
2033;;;!! ;; (abs-y (cdr pos))
2034;;;!! ;; (relative-coordinate
b2013aad 2035;;;!! ;; (coordinates-in-window-p `(,abs-x ,abs-y)
dbc4e1c1
JB
2036;;;!! ;; (selected-window)))
2037;;;!! ;; (begin-reg nil)
2038;;;!! ;; (end-reg nil)
2039;;;!! ;; (end-column nil)
2040;;;!! ;; (begin-column nil))
2041;;;!! ;; (if (and (consp relative-coordinate)
2042;;;!! ;; (or (not last-line-drawn)
2043;;;!! ;; (not (= last-line-drawn abs-y))))
2044;;;!! ;; (progn
2045;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2046;;;!! ;; (if (= (following-char) 10)
2047;;;!! ;; ()
2048;;;!! ;; (progn
2049;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
2050;;;!! ;; (setq begin-column (1- (current-column)))
2051;;;!! ;; (end-of-line)
2052;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
2053;;;!! ;; (setq end-column (1+ (current-column)))
2054;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
2055;;;!! ;; (x-draw-rectangle screen
2056;;;!! ;; (setq last-line-drawn abs-y)
2057;;;!! ;; begin-column
2058;;;!! ;; (- end-column begin-column) 1))))))))))
2059;;;!! ;;
2060;;;!! ;;(defun mouse-erase-box ()
2061;;;!! ;; (interactive)
2062;;;!! ;; (if last-line-drawn
2063;;;!! ;; (progn
2064;;;!! ;; (x-erase-rectangle (selected-screen))
2065;;;!! ;; (setq last-line-drawn nil))))
7b611de0 2066;;;!!
dbc4e1c1
JB
2067;;;!! ;;; (defun test-x-rectangle ()
2068;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
2069;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
2070;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
7b611de0 2071;;;!!
dbc4e1c1
JB
2072;;;!! ;;
2073;;;!! ;; Here is how to do double clicking in lisp. About to change.
2074;;;!! ;;
7b611de0 2075;;;!!
dbc4e1c1
JB
2076;;;!! (defvar double-start nil)
2077;;;!! (defconst double-click-interval 300
2078;;;!! "Max ticks between clicks")
7b611de0 2079;;;!!
dbc4e1c1
JB
2080;;;!! (defun double-down (event)
2081;;;!! (interactive "@e")
2082;;;!! (if double-start
2083;;;!! (let ((interval (- (nth 4 event) double-start)))
2084;;;!! (if (< interval double-click-interval)
2085;;;!! (progn
2086;;;!! (backward-up-list 1)
2087;;;!! ;; (message "Interval %d" interval)
2088;;;!! (sleep-for 1)))
2089;;;!! (setq double-start nil))
2090;;;!! (setq double-start (nth 4 event))))
7b611de0 2091;;;!!
dbc4e1c1
JB
2092;;;!! (defun double-up (event)
2093;;;!! (interactive "@e")
2094;;;!! (and double-start
2095;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
2096;;;!! (setq double-start nil)))
7b611de0 2097;;;!!
dbc4e1c1
JB
2098;;;!! ;;; (defun x-test-doubleclick ()
2099;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
2100;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
2101;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
7b611de0 2102;;;!!
dbc4e1c1 2103;;;!! ;;
5ba2dc3f 2104;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
dbc4e1c1 2105;;;!! ;;
7b611de0 2106;;;!!
dbc4e1c1
JB
2107;;;!! (defvar scrolled-lines 0)
2108;;;!! (defconst scroll-speed 1)
7b611de0 2109;;;!!
dbc4e1c1
JB
2110;;;!! (defun incr-scroll-down (event)
2111;;;!! (interactive "@e")
2112;;;!! (setq scrolled-lines 0)
2113;;;!! (incremental-scroll scroll-speed))
7b611de0 2114;;;!!
dbc4e1c1
JB
2115;;;!! (defun incr-scroll-up (event)
2116;;;!! (interactive "@e")
2117;;;!! (setq scrolled-lines 0)
2118;;;!! (incremental-scroll (- scroll-speed)))
7b611de0 2119;;;!!
dbc4e1c1
JB
2120;;;!! (defun incremental-scroll (n)
2121;;;!! (while (= (x-mouse-events) 0)
2122;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
2123;;;!! (scroll-down n)
2124;;;!! (sit-for 300 t)))
7b611de0 2125;;;!!
dbc4e1c1
JB
2126;;;!! (defun incr-scroll-stop (event)
2127;;;!! (interactive "@e")
2128;;;!! (message "Scrolled %d lines" scrolled-lines)
2129;;;!! (setq scrolled-lines 0)
2130;;;!! (sleep-for 1))
7b611de0 2131;;;!!
dbc4e1c1
JB
2132;;;!! ;;; (defun x-testing-scroll ()
2133;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
2134;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
2135;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
2136;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
2137;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
7b611de0 2138;;;!!
dbc4e1c1
JB
2139;;;!! ;;
2140;;;!! ;; Some playthings suitable for picture mode? They need work.
2141;;;!! ;;
7b611de0 2142;;;!!
dbc4e1c1
JB
2143;;;!! (defun mouse-kill-rectangle (event)
2144;;;!! "Kill the rectangle between point and the mouse cursor."
2145;;;!! (interactive "@e")
2146;;;!! (let ((point-save (point)))
2147;;;!! (save-excursion
2148;;;!! (mouse-set-point event)
2149;;;!! (push-mark nil t)
2150;;;!! (if (> point-save (point))
2151;;;!! (kill-rectangle (point) point-save)
2152;;;!! (kill-rectangle point-save (point))))))
7b611de0 2153;;;!!
dbc4e1c1
JB
2154;;;!! (defun mouse-open-rectangle (event)
2155;;;!! "Kill the rectangle between point and the mouse cursor."
2156;;;!! (interactive "@e")
2157;;;!! (let ((point-save (point)))
2158;;;!! (save-excursion
2159;;;!! (mouse-set-point event)
2160;;;!! (push-mark nil t)
2161;;;!! (if (> point-save (point))
2162;;;!! (open-rectangle (point) point-save)
2163;;;!! (open-rectangle point-save (point))))))
7b611de0 2164;;;!!
dbc4e1c1 2165;;;!! ;; Must be a better way to do this.
7b611de0 2166;;;!!
dbc4e1c1
JB
2167;;;!! (defun mouse-multiple-insert (n char)
2168;;;!! (while (> n 0)
2169;;;!! (insert char)
2170;;;!! (setq n (1- n))))
7b611de0 2171;;;!!
dbc4e1c1 2172;;;!! ;; What this could do is not finalize until button was released.
7b611de0 2173;;;!!
dbc4e1c1
JB
2174;;;!! (defun mouse-move-text (event)
2175;;;!! "Move text from point to cursor position, inserting spaces."
2176;;;!! (interactive "@e")
2177;;;!! (let* ((relative-coordinate
2178;;;!! (coordinates-in-window-p (car event) (selected-window))))
2179;;;!! (if (consp relative-coordinate)
2180;;;!! (cond ((> (current-column) (car relative-coordinate))
2181;;;!! (delete-char
2182;;;!! (- (car relative-coordinate) (current-column))))
2183;;;!! ((< (current-column) (car relative-coordinate))
2184;;;!! (mouse-multiple-insert
2185;;;!! (- (car relative-coordinate) (current-column)) " "))
2186;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
07a78410 2187\f
f936ae06
RS
2188;; Choose a completion with the mouse.
2189
2190(defun mouse-choose-completion (event)
49e61c42 2191 "Click on an alternative in the `*Completions*' buffer to choose it."
f936ae06 2192 (interactive "e")
33c448cd
RS
2193 ;; Give temporary modes such as isearch a chance to turn off.
2194 (run-hooks 'mouse-leave-buffer-hook)
d89a4a47 2195 (let ((buffer (window-buffer))
02680e9b
RS
2196 choice
2197 base-size)
f936ae06
RS
2198 (save-excursion
2199 (set-buffer (window-buffer (posn-window (event-start event))))
f36f4e9e
RS
2200 (if completion-reference-buffer
2201 (setq buffer completion-reference-buffer))
02680e9b 2202 (setq base-size completion-base-size)
f936ae06
RS
2203 (save-excursion
2204 (goto-char (posn-point (event-start event)))
b6be5d95 2205 (let (beg end)
9a43a594
RS
2206 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2207 (setq end (point) beg (1+ (point))))
2208 (if (null beg)
2209 (error "No completion here"))
2210 (setq beg (previous-single-property-change beg 'mouse-face))
2f5ed2e8
RS
2211 (setq end (or (next-single-property-change end 'mouse-face)
2212 (point-max)))
b6be5d95 2213 (setq choice (buffer-substring beg end)))))
73e2025f
RS
2214 (let ((owindow (selected-window)))
2215 (select-window (posn-window (event-start event)))
874a2cbd
RS
2216 (if (and (one-window-p t 'selected-frame)
2217 (window-dedicated-p (selected-window)))
2218 ;; This is a special buffer's frame
2219 (iconify-frame (selected-frame))
2220 (or (window-dedicated-p (selected-window))
2221 (bury-buffer)))
73e2025f 2222 (select-window owindow))
02680e9b 2223 (choose-completion-string choice buffer base-size)))
f936ae06 2224\f
07a78410
RS
2225;; Font selection.
2226
0eb9fef3
RS
2227(defun font-menu-add-default ()
2228 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2229 (font-alist x-fixed-font-alist)
0d94f5ca 2230 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
0eb9fef3
RS
2231 (if (assoc "Default" elt)
2232 (delete (assoc "Default" elt) elt))
2233 (setcdr elt
6b8e486e 2234 (cons (list "Default" default)
0eb9fef3
RS
2235 (cdr elt)))))
2236
07a78410
RS
2237(defvar x-fixed-font-alist
2238 '("Font menu"
2239 ("Misc"
19d973e8
RS
2240 ;; For these, we specify the pixel height and width.
2241 ("fixed" "fixed")
2242 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2243 ("6x12"
2244 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2245 ("6x13"
2246 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2247 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2248 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2249 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2250 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2251 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2252 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2253 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
fa21fdec 2254 ("")
f29c3695
RS
2255 ("clean 5x8"
2256 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2257 ("clean 6x8"
2258 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
19d973e8
RS
2259 ("clean 8x8"
2260 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2261 ("clean 8x10"
2262 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2263 ("clean 8x14"
2264 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2265 ("clean 8x16"
2266 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
fa21fdec 2267 ("")
29a3028d 2268 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
07a78410
RS
2269;;; We don't seem to have these; who knows what they are.
2270;;; ("fg-18" "fg-18")
2271;;; ("fg-25" "fg-25")
29a3028d
DL
2272 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2273 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2274 ("lucidasanstypewriter-bold-24"
2275 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
07a78410
RS
2276;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2277;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
29a3028d 2278 )
07a78410 2279 ("Courier"
19d973e8 2280 ;; For these, we specify the point height.
82c048a9
RS
2281 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2282 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2283 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2284 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2285 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2286 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2287 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2288 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2289 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2290 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2291 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2292 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2293 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2294 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2295 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2296 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2297 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2298 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2299 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2300 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2301 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2302 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2303 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2304 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
07a78410
RS
2305 )
2306 "X fonts suitable for use in Emacs.")
2307
1900a92b 2308(defun mouse-set-font (&rest fonts)
51a02226 2309 "Select an emacs font from a list of known good fonts and fontsets."
07a78410 2310 (interactive
8eb1dc02
RS
2311 (progn (unless (display-multi-font-p)
2312 (error "Cannot change fonts on this display"))
2313 (x-popup-menu
91c51412
LT
2314 (if (listp last-nonmenu-event)
2315 last-nonmenu-event
2316 (list '(0 0) (selected-window)))
8eb1dc02
RS
2317 ;; Append list of fontsets currently defined.
2318 (append x-fixed-font-alist (list (generate-fontset-menu))))))
df4de8c6
KH
2319 (if fonts
2320 (let (font)
2321 (while fonts
2322 (condition-case nil
2323 (progn
3fadec1a 2324 (set-default-font (car fonts))
df4de8c6
KH
2325 (setq font (car fonts))
2326 (setq fonts nil))
3fadec1a
RS
2327 (error
2328 (setq fonts (cdr fonts)))))
df4de8c6 2329 (if (null font)
8eb1dc02 2330 (error "Font not found")))))
cc0a8174
JB
2331\f
2332;;; Bindings for mouse commands.
2333
fcfc3c63 2334(define-key global-map [down-mouse-1] 'mouse-drag-region)
dbc4e1c1 2335(global-set-key [mouse-1] 'mouse-set-point)
dbc4e1c1 2336(global-set-key [drag-mouse-1] 'mouse-set-region)
fcfc3c63 2337
e37de120
RS
2338;; These are tested for in mouse-drag-region.
2339(global-set-key [double-mouse-1] 'mouse-set-point)
2340(global-set-key [triple-mouse-1] 'mouse-set-point)
2341
76693d12
KS
2342;; Clicking on the fringes causes hscrolling:
2343(global-set-key [left-fringe mouse-1] 'mouse-set-point)
2344(global-set-key [right-fringe mouse-1] 'mouse-set-point)
2345
dbc4e1c1
JB
2346(global-set-key [mouse-2] 'mouse-yank-at-click)
2347(global-set-key [mouse-3] 'mouse-save-then-kill)
8b34e79d 2348
dbc4e1c1
JB
2349;; By binding these to down-going events, we let the user use the up-going
2350;; event to make the selection, saving a click.
08a1c178
RS
2351(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2352(if (not (eq system-type 'ms-dos))
2353 (global-set-key [S-down-mouse-1] 'mouse-set-font))
eef805d7 2354;; C-down-mouse-2 is bound in facemenu.el.
de420e82 2355(global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
95132d1c 2356
07a78410 2357
8b34e79d
RS
2358;; Replaced with dragging mouse-1
2359;; (global-set-key [S-mouse-1] 'mouse-set-mark)
947da0c4 2360
06a8c9f8
EZ
2361;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2362;; vertical-line prevents Emacs from signaling an error when the mouse
2363;; button is released after dragging these lines, on non-toolkit
2364;; versions.
3c2dd2c0 2365(global-set-key [mode-line mouse-1] 'mouse-select-window)
544e7e73
RS
2366(global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2367(global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
b0d22e20 2368(global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
06a8c9f8 2369(global-set-key [header-line mouse-1] 'mouse-select-window)
3c2dd2c0 2370(global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
dbc4e1c1 2371(global-set-key [mode-line mouse-3] 'mouse-delete-window)
3c2dd2c0 2372(global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
9926ab64 2373(global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
b6522df6 2374(global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
08a1c178
RS
2375(global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2376(global-set-key [vertical-line mouse-1] 'mouse-select-window)
49116ac0
JB
2377
2378(provide 'mouse)
2379
2fe85227
DL
2380;; This file contains the functionality of the old mldrag.el.
2381(defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2382(defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
342a1e7b
SM
2383(make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2384(make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2fe85227
DL
2385(provide 'mldrag)
2386
ab5796a9 2387;;; arch-tag: 9a710ce1-914a-4923-9b81-697f7bf82ab3
6594deb0 2388;;; mouse.el ends here