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