Implement cygw32
[bpt/emacs.git] / lisp / mouse.el
CommitLineData
e1894109 1;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
84176303 2
41f03f4d 3;; Copyright (C) 1993-1995, 1999-2012 Free Software Foundation, Inc.
eea8d4ef 4
84176303 5;; Maintainer: FSF
de420e82 6;; Keywords: hardware, mouse
bd78fa1d 7;; Package: emacs
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
93d68d4f
DDLHG
45 "If non-nil, copy to kill-ring upon mouse adjustments of the region.
46
47This affects `mouse-save-then-kill' (\\[mouse-save-then-kill]) in
48addition to mouse drags."
b2dae92a 49 :type 'boolean
2490cbbc 50 :version "24.1"
b2dae92a
KS
51 :group 'mouse)
52
5dbda518 53(defcustom mouse-1-click-follows-link 450
787e24c3 54 "Non-nil means that clicking Mouse-1 on a link follows the link.
65f76581 55
787e24c3
KS
56With the default setting, an ordinary Mouse-1 click on a link
57performs the same action as Mouse-2 on that link, while a longer
97b1270c 58Mouse-1 click \(hold down the Mouse-1 button for more than 450
787e24c3 59milliseconds) performs the original Mouse-1 binding \(which
65f76581
KS
60typically sets point where you click the mouse).
61
62If value is an integer, the time elapsed between pressing and
63releasing the mouse button determines whether to follow the link
787e24c3 64or perform the normal Mouse-1 action (typically set point).
22bcf204 65The absolute numeric value specifies the maximum duration of a
65f76581
KS
66\"short click\" in milliseconds. A positive value means that a
67short click follows the link, and a longer click performs the
1d68acd3 68normal action. A negative value gives the opposite behavior.
65f76581
KS
69
70If value is `double', a double click follows the link.
71
787e24c3 72Otherwise, a single Mouse-1 click unconditionally follows the link.
65f76581
KS
73
74Note that dragging the mouse never follows the link.
75
76This feature only works in modes that specifically identify
77clickable text as links, so it may not work with some external
78packages. See `mouse-on-link-p' for details."
bf247b6e 79 :version "22.1"
65f76581
KS
80 :type '(choice (const :tag "Disabled" nil)
81 (const :tag "Double click" double)
97b1270c 82 (number :tag "Single click time limit" :value 450)
65f76581
KS
83 (other :tag "Single click" t))
84 :group 'mouse)
85
185a53bb 86(defcustom mouse-1-click-in-non-selected-windows t
9201cc28 87 "If non-nil, a Mouse-1 click also follows links in non-selected windows.
185a53bb
KS
88
89If nil, a Mouse-1 click on a link in a non-selected window performs
90the normal mouse-1 binding, typically selects the window and sets
91point at the click position."
92 :type 'boolean
93 :version "22.1"
94 :group 'mouse)
95
96
cc0a8174 97\f
95132d1c
RS
98;; Provide a mode-specific menu on a mouse button.
99
21ad0f7b
SM
100(defun popup-menu (menu &optional position prefix)
101 "Popup the given menu and call the selected option.
de420e82
DL
102MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
103`x-popup-menu'.
c69f56a2
MY
104The menu is shown at the place where POSITION specifies. About
105the form of POSITION, see `popup-menu-normalize-position'.
21ad0f7b 106PREFIX is the prefix argument (if any) to pass to the command."
de420e82
DL
107 (let* ((map (cond
108 ((keymapp menu) menu)
109 ((and (listp menu) (keymapp (car menu))) menu)
110 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
72596e2f
SM
111 (filter (when (symbolp map)
112 (plist-get (get map 'menu-prop) :filter))))
113 (if filter (funcall filter (symbol-function map)) map)))))
c69f56a2
MY
114 event cmd
115 (position (popup-menu-normalize-position position)))
21ad0f7b 116 ;; The looping behavior was taken from lmenu's popup-menu-popup
7636d2a3
EZ
117 (while (and map (setq event
118 ;; map could be a prefix key, in which case
119 ;; we need to get its function cell
120 ;; definition.
d32af6dd 121 (x-popup-menu position (indirect-function map))))
21ad0f7b
SM
122 ;; Strangely x-popup-menu returns a list.
123 ;; mouse-major-mode-menu was using a weird:
124 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
0dd672a6
SM
125 (setq cmd
126 (if (and (not (keymapp map)) (listp map))
127 ;; We were given a list of keymaps. Search them all
128 ;; in sequence until a first binding is found.
129 (let ((mouse-click (apply 'vector event))
130 binding)
131 (while (and map (null binding))
132 (setq binding (lookup-key (car map) mouse-click))
c8e5a42c 133 (if (numberp binding) ; `too long'
0dd672a6
SM
134 (setq binding nil))
135 (setq map (cdr map)))
136 binding)
137 ;; We were given a single keymap.
138 (lookup-key map (apply 'vector event))))
139 ;; Clear out echoing, which perhaps shows a prefix arg.
140 (message "")
141 ;; Maybe try again but with the submap.
142 (setq map (if (keymapp cmd) cmd)))
324cd972
RS
143 ;; If the user did not cancel by refusing to select,
144 ;; and if the result is a command, run it.
145 (when (and (null map) (commandp cmd))
0dd672a6
SM
146 (setq prefix-arg prefix)
147 ;; `setup-specified-language-environment', for instance,
148 ;; expects this to be set from a menu keymap.
149 (setq last-command-event (car (last event)))
150 ;; mouse-major-mode-menu was using `command-execute' instead.
65b61266 151 (call-interactively cmd))))
bcd010a0 152
c69f56a2 153(defun popup-menu-normalize-position (position)
e1894109
SM
154 "Convert the POSITION to the form which `popup-menu' expects internally.
155POSITION can an event, a posn- value, a value having
156form ((XOFFSET YOFFSET) WINDOW), or nil.
157If nil, the current mouse position is used."
c69f56a2
MY
158 (pcase position
159 ;; nil -> mouse cursor position
c69f56a2
MY
160 (`nil
161 (let ((mp (mouse-pixel-position)))
162 (list (list (cadr mp) (cddr mp)) (car mp))))
e1894109
SM
163 ;; Value returned from `event-end' or `posn-at-point'.
164 ((pred posnp)
c69f56a2
MY
165 (let ((xy (posn-x-y position)))
166 (list (list (car xy) (cdr xy))
167 (posn-window position))))
e1894109 168 ;; Event.
c69f56a2
MY
169 ((pred eventp)
170 (popup-menu-normalize-position (event-end position)))
e1894109 171 (t position)))
c69f56a2 172
7e4e9c66 173(defun minor-mode-menu-from-indicator (indicator)
fb1a03c9
DN
174 "Show menu for minor mode specified by INDICATOR.
175Interactively, INDICATOR is read using completion.
176If there is no menu defined for the minor mode, then create one with
177items `Turn Off' and `Help'."
178 (interactive
5e38cdab 179 (list (completing-read
fb1a03c9
DN
180 "Minor mode indicator: "
181 (describe-minor-mode-completion-table-for-indicator))))
97b952b7
SM
182 (let* ((minor-mode (lookup-minor-mode-from-indicator indicator))
183 (mm-fun (or (get minor-mode :minor-mode-function) minor-mode)))
fb1a03c9
DN
184 (unless minor-mode (error "Cannot find minor mode for `%s'" indicator))
185 (let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist)))
186 (menu (and (keymapp map) (lookup-key map [menu-bar]))))
ad0d18af
SM
187 (setq menu
188 (if menu
189 (mouse-menu-non-singleton menu)
fb1a03c9 190 `(keymap
ad0d18af 191 ,indicator
97b952b7 192 (turn-off menu-item "Turn Off minor mode" ,mm-fun)
ad0d18af 193 (help menu-item "Help for minor mode"
5e38cdab 194 (lambda () (interactive)
97b952b7 195 (describe-function ',mm-fun))))))
fb1a03c9 196 (popup-menu menu))))
7e4e9c66
GM
197
198(defun mouse-minor-mode-menu (event)
199 "Show minor-mode menu for EVENT on minor modes area of the mode line."
200 (interactive "@e")
201 (let ((indicator (car (nth 4 (car (cdr event))))))
202 (minor-mode-menu-from-indicator indicator)))
203
0b2b62ff 204(defun mouse-menu-major-mode-map ()
d5e6e0b6 205 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
ad0d18af
SM
206 (let* (;; Keymap from which to inherit; may be null.
207 (ancestor (mouse-menu-non-singleton
b98e5762 208 (and (current-local-map)
25db2767 209 (local-key-binding [menu-bar]))))
b98e5762
DL
210 ;; Make a keymap in which our last command leads to a menu or
211 ;; default to the edit menu.
212 (newmap (if ancestor
48d33090
SM
213 (make-sparse-keymap (concat (format-mode-line mode-name)
214 " Mode"))
4d6769e1 215 menu-bar-edit-menu)))
b98e5762 216 (if ancestor
00f7c5ed 217 (set-keymap-parent newmap ancestor))
0b2b62ff 218 newmap))
95132d1c 219
ad0d18af 220(defun mouse-menu-non-singleton (menubar)
d1f14baa
CY
221 "Return menu keybar MENUBAR, or a lone submenu inside it.
222If MENUBAR defines exactly one submenu, return just that submenu.
223Otherwise, return MENUBAR."
95132d1c 224 (if menubar
ad0d18af
SM
225 (let (submap)
226 (map-keymap
227 (lambda (k v) (setq submap (if submap t (cons k v))))
00f7c5ed 228 (keymap-canonicalize menubar))
ad0d18af
SM
229 (if (eq submap t)
230 menubar
231 (lookup-key menubar (vector (car submap)))))))
de420e82 232
0b2b62ff
SM
233(defun mouse-menu-bar-map ()
234 "Return a keymap equivalent to the menu bar.
de420e82
DL
235The contents are the items that would be in the menu bar whether or
236not it is actually displayed."
d5e6e0b6 237 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
7636d2a3
EZ
238 (let* ((local-menu (and (current-local-map)
239 (lookup-key (current-local-map) [menu-bar])))
240 (global-menu (lookup-key global-map [menu-bar]))
59836110
EZ
241 ;; If a keymap doesn't have a prompt string (a lazy
242 ;; programmer didn't bother to provide one), create it and
243 ;; insert it into the keymap; each keymap gets its own
244 ;; prompt. This is required for non-toolkit versions to
245 ;; display non-empty menu pane names.
246 (minor-mode-menus
247 (mapcar
00f7c5ed
SM
248 (lambda (menu)
249 (let* ((minor-mode (car menu))
250 (menu (cdr menu))
251 (title-or-map (cadr menu)))
252 (or (stringp title-or-map)
253 (setq menu
254 (cons 'keymap
255 (cons (concat
256 (capitalize (subst-char-in-string
257 ?- ?\s (symbol-name
258 minor-mode)))
259 " Menu")
260 (cdr menu)))))
261 menu))
59836110 262 (minor-mode-key-binding [menu-bar])))
7636d2a3
EZ
263 (local-title-or-map (and local-menu (cadr local-menu)))
264 (global-title-or-map (cadr global-menu)))
7636d2a3
EZ
265 (or (null local-menu)
266 (stringp local-title-or-map)
267 (setq local-menu (cons 'keymap
b38f5e6f 268 (cons (concat (format-mode-line mode-name)
48d33090 269 " Mode Menu")
7636d2a3
EZ
270 (cdr local-menu)))))
271 (or (stringp global-title-or-map)
272 (setq global-menu (cons 'keymap
273 (cons "Global Menu"
274 (cdr global-menu)))))
de420e82 275 ;; Supplying the list is faster than making a new map.
0b2b62ff
SM
276 ;; FIXME: We have a problem here: we have to use the global/local/minor
277 ;; so they're displayed in the expected order, but later on in the command
278 ;; loop, they're actually looked up in the opposite order.
279 (apply 'append
280 global-menu
281 local-menu
282 minor-mode-menus)))
283
284(defun mouse-major-mode-menu (event &optional prefix)
285 "Pop up a mode-specific menu of mouse commands.
286Default to the Edit menu if the major mode doesn't define a menu."
287 (interactive "@e\nP")
288 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
289 (popup-menu (mouse-menu-major-mode-map) event prefix))
5e38cdab 290(make-obsolete 'mouse-major-mode-menu 'mouse-menu-major-mode-map "23.1")
0b2b62ff
SM
291
292(defun mouse-popup-menubar (event prefix)
293 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
294The contents are the items that would be in the menu bar whether or
295not it is actually displayed."
296 (interactive "@e \nP")
297 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
fd6fa53f 298 (popup-menu (mouse-menu-bar-map) (unless (integerp event) event) prefix))
5e38cdab 299(make-obsolete 'mouse-popup-menubar 'mouse-menu-bar-map "23.1")
de420e82
DL
300
301(defun mouse-popup-menubar-stuff (event prefix)
302 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
303Use the former if the menu bar is showing, otherwise the latter."
0b2b62ff
SM
304 (interactive "@e\nP")
305 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
306 (popup-menu
307 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
308 (mouse-menu-bar-map)
309 (mouse-menu-major-mode-map))
310 event prefix))
5e38cdab 311(make-obsolete 'mouse-popup-menubar-stuff nil "23.1")
95132d1c 312\f
544e7e73
RS
313;; Commands that operate on windows.
314
d65147f6
KH
315(defun mouse-minibuffer-check (event)
316 (let ((w (posn-window (event-start event))))
317 (and (window-minibuffer-p w)
318 (not (minibuffer-window-active-p w))
71873e2b 319 (user-error "Minibuffer window is not active")))
33c448cd
RS
320 ;; Give temporary modes such as isearch a chance to turn off.
321 (run-hooks 'mouse-leave-buffer-hook))
d65147f6 322
cc0a8174 323(defun mouse-delete-window (click)
947da0c4 324 "Delete the window you click on.
6b48d742 325Do nothing if the frame has just one window.
a926a0fa 326This command must be bound to a mouse click."
ec558adc 327 (interactive "e")
6b48d742 328 (unless (one-window-p t)
a926a0fa
RS
329 (mouse-minibuffer-check click)
330 (delete-window (posn-window (event-start click)))))
cc0a8174 331
3c2dd2c0
RS
332(defun mouse-select-window (click)
333 "Select the window clicked on; don't move point."
334 (interactive "e")
d65147f6 335 (mouse-minibuffer-check click)
3c2dd2c0
RS
336 (let ((oframe (selected-frame))
337 (frame (window-frame (posn-window (event-start click)))))
338 (select-window (posn-window (event-start click)))
339 (raise-frame frame)
340 (select-frame frame)
341 (or (eq frame oframe)
e1877477 342 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
3c2dd2c0 343
b0f3a26b
JB
344(defun mouse-tear-off-window (click)
345 "Delete the window clicked on, and create a new frame displaying its buffer."
346 (interactive "e")
d65147f6 347 (mouse-minibuffer-check click)
b0f3a26b
JB
348 (let* ((window (posn-window (event-start click)))
349 (buf (window-buffer window))
01a911e3 350 (frame (make-frame)))
b0f3a26b
JB
351 (select-frame frame)
352 (switch-to-buffer buf)
353 (delete-window window)))
354
b5370f03 355(defun mouse-delete-other-windows ()
5925bb84 356 "Delete all windows except the one you click on."
b5370f03 357 (interactive "@")
cc0a8174 358 (delete-other-windows))
72ea54a4 359
cc0a8174
JB
360(defun mouse-split-window-vertically (click)
361 "Select Emacs window mouse is on, then split it vertically in half.
362The window is split at the line clicked on.
363This command must be bound to a mouse click."
947da0c4 364 (interactive "@e")
d65147f6 365 (mouse-minibuffer-check click)
b5370f03
JB
366 (let ((start (event-start click)))
367 (select-window (posn-window start))
85d6b80b 368 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
5ba2dc3f
JB
369 (first-line window-min-height)
370 (last-line (- (window-height) window-min-height)))
371 (if (< last-line first-line)
0a50b993 372 (error "Window too short to split")
5ba2dc3f
JB
373 (split-window-vertically
374 (min (max new-height first-line) last-line))))))
cc0a8174 375
947da0c4
RS
376(defun mouse-split-window-horizontally (click)
377 "Select Emacs window mouse is on, then split it horizontally in half.
378The window is split at the column clicked on.
379This command must be bound to a mouse click."
380 (interactive "@e")
d65147f6 381 (mouse-minibuffer-check click)
5ba2dc3f
JB
382 (let ((start (event-start click)))
383 (select-window (posn-window start))
384 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
385 (first-col window-min-width)
386 (last-col (- (window-width) window-min-width)))
387 (if (< last-col first-col)
0a50b993 388 (error "Window too narrow to split")
5ba2dc3f
JB
389 (split-window-horizontally
390 (min (max new-width first-col) last-col))))))
947da0c4 391
e07b9a6d
MR
392;; `mouse-drag-line' is now the common routine for handling all line
393;; dragging events combining the earlier `mouse-drag-mode-line-1' and
394;; `mouse-drag-vertical-line'. It should improve the behavior of line
395;; dragging wrt Emacs 23 as follows:
396
397;; (1) Gratuitous error messages and restrictions have been (hopefully)
398;; removed. (The help-echo that dragging the mode-line can resize a
399;; one-window-frame's window will still show through via bindings.el.)
400
401;; (2) No gratuitous selection of other windows should happen. (This
402;; has not been completely fixed for mouse-autoselected windows yet.)
403
404;; (3) Mouse clicks below a scroll-bar should pass through via unread
405;; command events.
406
407;; Note that `window-in-direction' replaces `mouse-drag-window-above'
408;; and `mouse-drag-vertical-line-rightward-window' with Emacs 24.1.
01ac65bd 409
e07b9a6d 410(defun mouse-drag-line (start-event line)
01ac65bd 411 "Drag a mode line, header line, or vertical line with the mouse.
e07b9a6d 412START-EVENT is the starting mouse-event of the drag action. LINE
01ac65bd 413must be one of the symbols `header', `mode', or `vertical'."
33c448cd
RS
414 ;; Give temporary modes such as isearch a chance to turn off.
415 (run-hooks 'mouse-leave-buffer-hook)
e07b9a6d 416 (let* ((echo-keystrokes 0)
ee0e2bdd 417 (start (event-start start-event))
e07b9a6d
MR
418 (window (posn-window start))
419 (frame (window-frame window))
420 (minibuffer-window (minibuffer-window frame))
ceaabbbd 421 (on-link (and mouse-1-click-follows-link
e07b9a6d 422 (mouse-on-link-p start)))
01ac65bd
CY
423 (side (and (eq line 'vertical)
424 (or (cdr (assq 'vertical-scroll-bars
425 (frame-parameters frame)))
426 'right)))
427 (draggable t)
428 event position growth dragged)
e07b9a6d
MR
429 (cond
430 ((eq line 'header)
431 ;; Check whether header-line can be dragged at all.
53ebff1f 432 (if (window-at-side-p window 'top)
01ac65bd 433 (setq draggable nil)
53ebff1f 434 (setq window (window-in-direction 'above window t))))
e07b9a6d
MR
435 ((eq line 'mode)
436 ;; Check whether mode-line can be dragged at all.
01ac65bd
CY
437 (and (window-at-side-p window 'bottom)
438 ;; Allow resizing the minibuffer window if it's on the same
439 ;; frame as and immediately below the clicked window, and
440 ;; it's active or `resize-mini-windows' is nil.
441 (not (and (eq (window-frame minibuffer-window) frame)
442 (= (nth 1 (window-edges minibuffer-window))
443 (nth 3 (window-edges window)))
444 (or (not resize-mini-windows)
445 (eq minibuffer-window
446 (active-minibuffer-window)))))
447 (setq draggable nil)))
e07b9a6d 448 ((eq line 'vertical)
01ac65bd
CY
449 ;; Get the window to adjust for the vertical case. If the
450 ;; scroll bar is on the window's right or there's no scroll bar
451 ;; at all, adjust the window where the start-event occurred. If
452 ;; the scroll bar is on the start-event window's left, adjust
453 ;; the window on the left of it.
454 (unless (eq side 'right)
455 (setq window (window-in-direction 'left window t)))))
e07b9a6d
MR
456
457 ;; Start tracking.
544e7e73 458 (track-mouse
c8e5a42c
MR
459 ;; Loop reading events and sampling the position of the mouse.
460 (while draggable
461 (setq event (read-event))
01ac65bd 462 (setq position (mouse-position))
c8e5a42c
MR
463 ;; Do nothing if
464 ;; - there is a switch-frame event.
465 ;; - the mouse isn't in the frame that we started in
466 ;; - the mouse isn't in any Emacs frame
467 ;; Drag if
468 ;; - there is a mouse-movement event
469 ;; - there is a scroll-bar-movement event (Why? -- cyd)
470 ;; (same as mouse movement for our purposes)
471 ;; Quit if
472 ;; - there is a keyboard event or some other unknown event.
e07b9a6d 473 (cond
c8e5a42c
MR
474 ((not (consp event))
475 (setq draggable nil))
476 ((memq (car event) '(switch-frame select-window))
477 nil)
478 ((not (memq (car event) '(mouse-movement scroll-bar-movement)))
479 (when (consp event)
480 ;; Do not unread a drag-mouse-1 event to avoid selecting
481 ;; some other window. For vertical line dragging do not
482 ;; unread mouse-1 events either (but only if we dragged at
483 ;; least once to allow mouse-1 clicks get through.
484 (unless (and dragged
485 (if (eq line 'vertical)
486 (memq (car event) '(drag-mouse-1 mouse-1))
487 (eq (car event) 'drag-mouse-1)))
488 (push event unread-command-events)))
489 (setq draggable nil))
01ac65bd 490 ((or (not (eq (car position) frame))
c8e5a42c 491 (null (car (cdr position))))
e07b9a6d
MR
492 nil)
493 ((eq line 'vertical)
01ac65bd
CY
494 ;; Drag vertical divider.
495 (setq growth (- (cadr position)
496 (if (eq side 'right) 0 2)
497 (nth 2 (window-edges window))
498 -1))
e07b9a6d 499 (unless (zerop growth)
e07b9a6d
MR
500 (setq dragged t))
501 (adjust-window-trailing-edge window growth t))
01ac65bd
CY
502 (draggable
503 ;; Drag horizontal divider.
e07b9a6d
MR
504 (setq growth
505 (if (eq line 'mode)
01ac65bd 506 (- (cddr position) (nth 3 (window-edges window)) -1)
e07b9a6d 507 ;; The window's top includes the header line!
01ac65bd 508 (- (nth 3 (window-edges window)) (cddr position))))
e07b9a6d 509 (unless (zerop growth)
e07b9a6d 510 (setq dragged t))
01ac65bd
CY
511 (adjust-window-trailing-edge window (if (eq line 'mode)
512 growth
513 (- growth)))))))
514 ;; Process the terminating event.
515 (when (and (mouse-event-p event) on-link (not dragged)
516 (mouse--remap-link-click-p start-event event))
517 ;; If mouse-2 has never been done by the user, it doesn't have
518 ;; the necessary property to be interpreted correctly.
519 (put 'mouse-2 'event-kind 'mouse-click)
520 (setcar event 'mouse-2))
521 (push event unread-command-events)))
e07b9a6d 522
b0d22e20
GM
523(defun mouse-drag-mode-line (start-event)
524 "Change the height of a window by dragging on the mode line."
525 (interactive "e")
e07b9a6d 526 (mouse-drag-line start-event 'mode))
b0d22e20
GM
527
528(defun mouse-drag-header-line (start-event)
e07b9a6d 529 "Change the height of a window by dragging on the header line."
b0d22e20 530 (interactive "e")
e07b9a6d 531 (mouse-drag-line start-event 'header))
73c8f64c 532
08a1c178
RS
533(defun mouse-drag-vertical-line (start-event)
534 "Change the width of a window by dragging on the vertical line."
535 (interactive "e")
e07b9a6d 536 (mouse-drag-line start-event 'vertical))
08a1c178 537\f
2a5fa27b 538(defun mouse-set-point (event)
cc0a8174 539 "Move point to the position clicked on with the mouse.
2a5fa27b 540This should be bound to a mouse click event type."
ec558adc 541 (interactive "e")
d65147f6 542 (mouse-minibuffer-check event)
2a5fa27b
RS
543 ;; Use event-end in case called from mouse-drag-region.
544 ;; If EVENT is a click, event-end and event-start give same value.
541a44d2 545 (posn-set-point (event-end event)))
cc0a8174 546
e6c2f5d4
RS
547(defvar mouse-last-region-beg nil)
548(defvar mouse-last-region-end nil)
549(defvar mouse-last-region-tick nil)
550
551(defun mouse-region-match ()
552 "Return non-nil if there's an active region that was set with the mouse."
553 (and (mark t) mark-active
554 (eq mouse-last-region-beg (region-beginning))
555 (eq mouse-last-region-end (region-end))
556 (eq mouse-last-region-tick (buffer-modified-tick))))
557
652ccd35 558(defun mouse-set-region (click)
e37de120 559 "Set the region to the text dragged over, and copy to kill ring.
43f5740b
LMI
560This should be bound to a mouse drag event.
561See the `mouse-drag-copy-region' variable to control whether this
562command alters the kill ring or not."
652ccd35 563 (interactive "e")
d65147f6 564 (mouse-minibuffer-check click)
f9be2e35
CY
565 (select-window (posn-window (event-start click)))
566 (let ((beg (posn-point (event-start click)))
567 (end (posn-point (event-end click))))
568 (and mouse-drag-copy-region (integerp beg) (integerp end)
569 ;; Don't set this-command to `kill-region', so a following
570 ;; C-w won't double the text in the kill ring. Ignore
571 ;; `last-command' so we don't append to a preceding kill.
572 (let (this-command last-command deactivate-mark)
573 (copy-region-as-kill beg end)))
574 (if (numberp beg) (goto-char beg))
575 ;; On a text terminal, bounce the cursor.
33a35434 576 (or transient-mark-mode
f9be2e35 577 (window-system)
fcfc3c63 578 (sit-for 1))
652ccd35 579 (push-mark)
1cc8a3f4 580 (set-mark (point))
f9be2e35 581 (if (numberp end) (goto-char end))
e6c2f5d4
RS
582 (mouse-set-region-1)))
583
584(defun mouse-set-region-1 ()
a5809dbd 585 ;; Set transient-mark-mode for a little while.
688468a0
CY
586 (unless (eq (car-safe transient-mark-mode) 'only)
587 (setq transient-mark-mode
588 (cons 'only
589 (unless (eq transient-mark-mode 'lambda)
590 transient-mark-mode))))
e6c2f5d4
RS
591 (setq mouse-last-region-beg (region-beginning))
592 (setq mouse-last-region-end (region-end))
593 (setq mouse-last-region-tick (buffer-modified-tick)))
652ccd35 594
3b558d41 595(defcustom mouse-scroll-delay 0.25
9201cc28 596 "The pause between scroll steps caused by mouse drags, in seconds.
600c6e3a
JB
597If you drag the mouse beyond the edge of a window, Emacs scrolls the
598window to bring the text beyond that edge into view, with a delay of
599this many seconds between scroll steps. Scrolling stops when you move
600the mouse back into the window, or release the button.
601This variable's value may be non-integral.
3b558d41
RS
602Setting this to zero causes Emacs to scroll as fast as it can."
603 :type 'number
604 :group 'mouse)
600c6e3a 605
3b558d41 606(defcustom mouse-scroll-min-lines 1
9201cc28 607 "The minimum number of lines scrolled by dragging mouse out of window.
a3d6bb97
RS
608Moving the mouse out the top or bottom edge of the window begins
609scrolling repeatedly. The number of lines scrolled per repetition
610is normally equal to the number of lines beyond the window edge that
611the mouse has moved. However, it always scrolls at least the number
3b558d41
RS
612of lines specified by this variable."
613 :type 'integer
614 :group 'mouse)
08a1c178 615
e919a622
RS
616(defun mouse-scroll-subr (window jump &optional overlay start)
617 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
600c6e3a
JB
618If OVERLAY is an overlay, let it stretch from START to the far edge of
619the newly visible text.
620Upon exit, point is at the far edge of the newly visible text."
08a1c178
RS
621 (cond
622 ((and (> jump 0) (< jump mouse-scroll-min-lines))
623 (setq jump mouse-scroll-min-lines))
624 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
625 (setq jump (- mouse-scroll-min-lines))))
4e399a53
RS
626 (let ((opoint (point)))
627 (while (progn
628 (goto-char (window-start window))
629 (if (not (zerop (vertical-motion jump window)))
630 (progn
631 (set-window-start window (point))
632 (if (natnump jump)
0320e66f
RS
633 (if (window-end window)
634 (progn
635 (goto-char (window-end window))
636 ;; window-end doesn't reflect the window's new
637 ;; start position until the next redisplay.
638 (vertical-motion (1- jump) window))
639 (vertical-motion (- (window-height window) 2)))
4e399a53
RS
640 (goto-char (window-start window)))
641 (if overlay
642 (move-overlay overlay start (point)))
643 ;; Now that we have scrolled WINDOW properly,
644 ;; put point back where it was for the redisplay
645 ;; so that we don't mess up the selected window.
646 (or (eq window (selected-window))
647 (goto-char opoint))
d2287ded 648 (sit-for mouse-scroll-delay)))))
4e399a53
RS
649 (or (eq window (selected-window))
650 (goto-char opoint))))
fcfc3c63 651
dd524dbd 652(defvar mouse-selection-click-count 0)
eb6ff46f 653
c8c5bd24
RS
654(defvar mouse-selection-click-count-buffer nil)
655
600c6e3a 656(defun mouse-drag-region (start-event)
bcd5aef1 657 "Set the region to the text that the mouse is dragged over.
78210c95
RS
658Highlight the drag area as you move the mouse.
659This must be bound to a button-down mouse event.
d017deb2 660In Transient Mark mode, the highlighting remains as long as the mark
6454615c
RS
661remains active. Otherwise, it remains until the next input event.
662
663If the click is in the echo area, display the `*Messages*' buffer."
bcd5aef1 664 (interactive "e")
fd6fa53f
SM
665 ;; Give temporary modes such as isearch a chance to turn off.
666 (run-hooks 'mouse-leave-buffer-hook)
667 (mouse-drag-track start-event t))
6454615c 668
65f76581 669
91a2acb2 670(defun mouse-posn-property (pos property)
9ed38a84
DK
671 "Look for a property at click position.
672POS may be either a buffer position or a click position like
89bf250e 673those returned from `event-start'. If the click position is on
9ed38a84
DK
674a string, the text property PROPERTY is examined.
675If this is nil or the click is not on a string, then
676the corresponding buffer position is searched for PROPERTY.
677If PROPERTY is encountered in one of those places,
678its value is returned."
91a2acb2
DK
679 (if (consp pos)
680 (let ((w (posn-window pos)) (pt (posn-point pos))
681 (str (posn-string pos)))
682 (or (and str
683 (get-text-property (cdr str) property (car str)))
684 (and pt
685 (get-char-property pt property w))))
686 (get-char-property pos property)))
687
65f76581
KS
688(defun mouse-on-link-p (pos)
689 "Return non-nil if POS is on a link in the current buffer.
23eb76c4
TTN
690POS must be a buffer position in the current buffer or a mouse
691event location in the selected window (see `event-start').
185a53bb
KS
692However, if `mouse-1-click-in-non-selected-windows' is non-nil,
693POS may be a mouse event location in any window.
65f76581
KS
694
695A clickable link is identified by one of the following methods:
696
b41a4019 697- If the character at POS has a non-nil `follow-link' text or
e5fb57e9 698overlay property, the value of that property determines what to do.
65f76581 699
b41a4019
KS
700- If there is a local key-binding or a keybinding at position POS
701for the `follow-link' event, the binding of that event determines
702what to do.
65f76581 703
b41a4019
KS
704The resulting value determine whether POS is inside a link:
705
706- If the value is `mouse-face', POS is inside a link if there
65f76581
KS
707is a non-nil `mouse-face' property at POS. Return t in this case.
708
b41a4019 709- If the value is a function, FUNC, POS is inside a link if
65f76581 710the call \(FUNC POS) returns non-nil. Return the return value
23eb76c4 711from that call. Arg is \(posn-point POS) if POS is a mouse event.
65f76581 712
b41a4019 713- Otherwise, return the value itself.
65f76581
KS
714
715The return value is interpreted as follows:
716
717- If it is a string, the mouse-1 event is translated into the
718first character of the string, i.e. the action of the mouse-1
719click is the local or global binding of that character.
720
721- If it is a vector, the mouse-1 event is translated into the
722first element of that vector, i.e. the action of the mouse-1
723click is the local or global binding of that event.
724
725- Otherwise, the mouse-1 event is translated into a mouse-2 event
726at the same position."
91a2acb2
DK
727 (let ((action
728 (and (or (not (consp pos))
729 mouse-1-click-in-non-selected-windows
730 (eq (selected-window) (posn-window pos)))
731 (or (mouse-posn-property pos 'follow-link)
732 (key-binding [follow-link] nil t pos)))))
733 (cond
734 ((eq action 'mouse-face)
735 (and (mouse-posn-property pos 'mouse-face) t))
736 ((functionp action)
9ed38a84 737 ;; FIXME: This seems questionable if the click is not in a buffer.
e9ae308c 738 ;; Should we instead decide that `action' takes a `posn'?
9ed38a84
DK
739 (if (consp pos)
740 (with-current-buffer (window-buffer (posn-window pos))
624c972d 741 (funcall action (posn-point pos)))
9ed38a84 742 (funcall action pos)))
91a2acb2 743 (t action))))
b0ca1b8a 744
5dbda518
KS
745(defun mouse-fixup-help-message (msg)
746 "Fix help message MSG for `mouse-1-click-follows-link'."
747 (let (mp pos)
748 (if (and mouse-1-click-follows-link
749 (stringp msg)
26ee77a6 750 (string-match-p "\\`mouse-2" msg)
5dbda518
KS
751 (setq mp (mouse-pixel-position))
752 (consp (setq pos (cdr mp)))
753 (car pos) (>= (car pos) 0)
754 (cdr pos) (>= (cdr pos) 0)
755 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
756 (windowp (posn-window pos)))
757 (with-current-buffer (window-buffer (posn-window pos))
758 (if (mouse-on-link-p pos)
759 (setq msg (concat
760 (cond
761 ((eq mouse-1-click-follows-link 'double) "double-")
762 ((and (integerp mouse-1-click-follows-link)
763 (< mouse-1-click-follows-link 0)) "Long ")
764 (t ""))
765 "mouse-1" (substring msg 7)))))))
766 msg)
65f76581 767
624c972d 768(defun mouse-drag-track (start-event &optional
f1c4f757
S
769 do-mouse-drag-region-post-process)
770 "Track mouse drags by highlighting area between point and cursor.
5592c08f
CY
771The region will be defined with mark and point.
772DO-MOUSE-DRAG-REGION-POST-PROCESS should only be used by
773`mouse-drag-region'."
d65147f6 774 (mouse-minibuffer-check start-event)
aaf15b8b 775 (setq mouse-selection-click-count-buffer (current-buffer))
3f5c9cad 776 (deactivate-mark)
371d6a61
CY
777 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
778 (original-window (selected-window))
aaf15b8b
SM
779 ;; We've recorded what we needed from the current buffer and
780 ;; window, now let's jump to the place of the event, where things
781 ;; are happening.
782 (_ (mouse-set-point start-event))
783 (echo-keystrokes 0)
4c9afcbe 784 (start-posn (event-start start-event))
600c6e3a
JB
785 (start-point (posn-point start-posn))
786 (start-window (posn-window start-posn))
02beb936 787 (start-window-start (window-start start-window))
1853aa6b 788 (start-hscroll (window-hscroll start-window))
600c6e3a 789 (bounds (window-edges start-window))
8413d0d2 790 (make-cursor-line-fully-visible nil)
600c6e3a
JB
791 (top (nth 1 bounds))
792 (bottom (if (window-minibuffer-p start-window)
793 (nth 3 bounds)
794 ;; Don't count the mode line.
e37de120 795 (1- (nth 3 bounds))))
b0ca1b8a 796 (on-link (and mouse-1-click-follows-link
aaf15b8b 797 ;; Use start-point before the intangibility
01ac65bd 798 ;; treatment, in case we click on a link inside
aaf15b8b 799 ;; intangible text.
91a2acb2 800 (mouse-on-link-p start-posn)))
81133808 801 (click-count (1- (event-click-count start-event)))
7adf5fdc
CY
802 (remap-double-click (and on-link
803 (eq mouse-1-click-follows-link 'double)
804 (= click-count 1)))
a9d01547
CY
805 ;; Suppress automatic hscrolling, because that is a nuisance
806 ;; when setting point near the right fringe (but see below).
41f03f4d
GM
807 (auto-hscroll-mode-saved auto-hscroll-mode)
808 (auto-hscroll-mode nil)
8cca9703 809 moved-off-start event end end-point)
5592c08f 810
81133808 811 (setq mouse-selection-click-count click-count)
08a1c178
RS
812 ;; In case the down click is in the middle of some intangible text,
813 ;; use the end of that text, and put it in START-POINT.
814 (if (< (point) start-point)
815 (goto-char start-point))
816 (setq start-point (point))
7adf5fdc
CY
817 (if remap-double-click
818 (setq click-count 0))
5592c08f 819
81133808
CY
820 ;; Activate the region, using `mouse-start-end' to determine where
821 ;; to put point and mark (e.g., double-click will select a word).
5592c08f
CY
822 (setq transient-mark-mode
823 (if (eq transient-mark-mode 'lambda)
824 '(only)
825 (cons 'only transient-mark-mode)))
9852377f 826 (let ((range (mouse-start-end start-point start-point click-count)))
6d3e82d2 827 (push-mark (nth 0 range) t t)
81133808 828 (goto-char (nth 1 range)))
5592c08f
CY
829
830 ;; Track the mouse until we get a non-movement event.
831 (track-mouse
832 (while (progn
833 (setq event (read-event))
834 (or (mouse-movement-p event)
835 (memq (car-safe event) '(switch-frame select-window))))
836 (unless (memq (car-safe event) '(switch-frame select-window))
837 ;; Automatic hscrolling did not occur during the call to
838 ;; `read-event'; but if the user subsequently drags the
839 ;; mouse, go ahead and hscroll.
41f03f4d 840 (let ((auto-hscroll-mode auto-hscroll-mode-saved))
5592c08f
CY
841 (redisplay))
842 (setq end (event-end event)
843 end-point (posn-point end))
8cca9703
CY
844 ;; Note whether the mouse has left the starting position.
845 (unless (eq end-point start-point)
846 (setq moved-off-start t))
5592c08f 847 (if (and (eq (posn-window end) start-window)
b846d039 848 (integer-or-marker-p end-point))
6d3e82d2
CY
849 (mouse--drag-set-mark-and-point start-point
850 end-point click-count)
5592c08f
CY
851 (let ((mouse-row (cdr (cdr (mouse-position)))))
852 (cond
853 ((null mouse-row))
854 ((< mouse-row top)
855 (mouse-scroll-subr start-window (- mouse-row top)
856 nil start-point))
857 ((>= mouse-row bottom)
858 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
859 nil start-point))))))))
860
861 ;; Handle the terminating event if possible.
862 (when (consp event)
863 ;; Ensure that point is on the end of the last event.
864 (when (and (setq end-point (posn-point (event-end event)))
2e3329a5 865 (eq (posn-window end) start-window)
5592c08f
CY
866 (integer-or-marker-p end-point)
867 (/= start-point end-point))
6d3e82d2
CY
868 (mouse--drag-set-mark-and-point start-point
869 end-point click-count))
870
5592c08f
CY
871 ;; Find its binding.
872 (let* ((fun (key-binding (vector (car event))))
873 (do-multi-click (and (> (event-click-count event) 0)
874 (functionp fun)
875 (not (memq fun '(mouse-set-point
876 mouse-set-region))))))
877 (if (and (/= (mark) (point))
878 (not do-multi-click))
f479ef6e 879
5592c08f
CY
880 ;; If point has moved, finish the drag.
881 (let* (last-command this-command)
5592c08f
CY
882 (and mouse-drag-copy-region
883 do-mouse-drag-region-post-process
884 (let (deactivate-mark)
9852377f 885 (copy-region-as-kill (mark) (point)))))
f479ef6e 886
8cca9703 887 ;; Otherwise, run binding of terminating up-event.
05e89fea
CY
888 (if do-multi-click
889 (goto-char start-point)
890 (deactivate-mark)
891 (unless moved-off-start
892 (pop-mark)))
893
5592c08f
CY
894 (when (and (functionp fun)
895 (= start-hscroll (window-hscroll start-window))
896 ;; Don't run the up-event handler if the window
897 ;; start changed in a redisplay after the
898 ;; mouse-set-point for the down-mouse event at
899 ;; the beginning of this function. When the
900 ;; window start has changed, the up-mouse event
901 ;; contains a different position due to the new
902 ;; window contents, and point is set again.
903 (or end-point
904 (= (window-start start-window)
905 start-window-start)))
906 (when (and on-link
907 (= start-point (point))
908 (mouse--remap-link-click-p start-event event))
909 ;; If we rebind to mouse-2, reselect previous selected
910 ;; window, so that the mouse-2 event runs in the same
911 ;; situation as if user had clicked it directly. Fixes
912 ;; the bug reported by juri@jurta.org on 2005-12-27.
913 (if (or (vectorp on-link) (stringp on-link))
914 (setq event (aref on-link 0))
915 (select-window original-window)
916 (setcar event 'mouse-2)
917 ;; If this mouse click has never been done by the
918 ;; user, it doesn't have the necessary property to be
919 ;; interpreted correctly.
920 (put 'mouse-2 'event-kind 'mouse-click)))
921 (push event unread-command-events)))))))
922
6d3e82d2
CY
923(defun mouse--drag-set-mark-and-point (start click click-count)
924 (let* ((range (mouse-start-end start click click-count))
925 (beg (nth 0 range))
926 (end (nth 1 range)))
927 (cond ((eq (mark) beg)
928 (goto-char end))
929 ((eq (mark) end)
930 (goto-char beg))
931 ((< click (mark))
932 (set-mark end)
933 (goto-char beg))
934 (t
935 (set-mark beg)
936 (goto-char end)))))
937
5592c08f
CY
938(defun mouse--remap-link-click-p (start-event end-event)
939 (or (and (eq mouse-1-click-follows-link 'double)
940 (= (event-click-count start-event) 2))
941 (and
942 (not (eq mouse-1-click-follows-link 'double))
943 (= (event-click-count start-event) 1)
944 (= (event-click-count end-event) 1)
945 (or (not (integerp mouse-1-click-follows-link))
946 (let ((t0 (posn-timestamp (event-start start-event)))
947 (t1 (posn-timestamp (event-end end-event))))
948 (and (integerp t0) (integerp t1)
949 (if (> mouse-1-click-follows-link 0)
950 (<= (- t1 t0) mouse-1-click-follows-link)
951 (< (- t0 t1) mouse-1-click-follows-link))))))))
952
e37de120
RS
953\f
954;; Commands to handle xterm-style multiple clicks.
e37de120
RS
955(defun mouse-skip-word (dir)
956 "Skip over word, over whitespace, or over identical punctuation.
957If DIR is positive skip forward; if negative, skip backward."
958 (let* ((char (following-char))
959 (syntax (char-to-string (char-syntax char))))
258e3295
KH
960 (cond ((string= syntax "w")
961 ;; Here, we can't use skip-syntax-forward/backward because
962 ;; they don't pay attention to word-separating-categories,
963 ;; and thus they will skip over a true word boundary. So,
fffa137c 964 ;; we simulate the original behavior by using forward-word.
258e3295
KH
965 (if (< dir 0)
966 (if (not (looking-at "\\<"))
967 (forward-word -1))
968 (if (or (looking-at "\\<") (not (looking-at "\\>")))
969 (forward-word 1))))
970 ((string= syntax " ")
08a1c178
RS
971 (if (< dir 0)
972 (skip-syntax-backward syntax)
973 (skip-syntax-forward syntax)))
974 ((string= syntax "_")
975 (if (< dir 0)
976 (skip-syntax-backward "w_")
977 (skip-syntax-forward "w_")))
978 ((< dir 0)
979 (while (and (not (bobp)) (= (preceding-char) char))
980 (forward-char -1)))
981 (t
982 (while (and (not (eobp)) (= (following-char) char))
983 (forward-char 1))))))
e37de120 984
eb6ff46f 985(defun mouse-start-end (start end mode)
c3bb6d70 986 "Return a list of region bounds based on START and END according to MODE.
a7df580d
DL
987If MODE is 0 then set point to (min START END), mark to (max START END).
988If MODE is 1 then set point to start of word at (min START END),
989mark to end of word at (max START END).
990If MODE is 2 then do the same for lines."
e37de120
RS
991 (if (> start end)
992 (let ((temp start))
993 (setq start end
994 end temp)))
9a974c88 995 (setq mode (mod mode 3))
e37de120
RS
996 (cond ((= mode 0)
997 (list start end))
998 ((and (= mode 1)
999 (= start end)
1ec71583 1000 (char-after start)
e37de120 1001 (= (char-syntax (char-after start)) ?\())
6f482eec
RS
1002 (list start
1003 (save-excursion
1004 (goto-char start)
1005 (forward-sexp 1)
1006 (point))))
e37de120
RS
1007 ((and (= mode 1)
1008 (= start end)
1ec71583 1009 (char-after start)
e37de120 1010 (= (char-syntax (char-after start)) ?\)))
7b611de0 1011 (list (save-excursion
e37de120 1012 (goto-char (1+ start))
d89a4a47
RS
1013 (backward-sexp 1)
1014 (point))
e37de120 1015 (1+ start)))
53770877
RS
1016 ((and (= mode 1)
1017 (= start end)
1018 (char-after start)
1019 (= (char-syntax (char-after start)) ?\"))
1020 (let ((open (or (eq start (point-min))
1021 (save-excursion
1022 (goto-char (- start 1))
1023 (looking-at "\\s(\\|\\s \\|\\s>")))))
1024 (if open
1025 (list start
1026 (save-excursion
1027 (condition-case nil
7b611de0 1028 (progn
53770877
RS
1029 (goto-char start)
1030 (forward-sexp 1)
1031 (point))
1032 (error end))))
f1017d55 1033 (list (save-excursion
53770877
RS
1034 (condition-case nil
1035 (progn
1036 (goto-char (1+ start))
1037 (backward-sexp 1)
1038 (point))
f1017d55
RS
1039 (error end)))
1040 (1+ start)))))
e37de120
RS
1041 ((= mode 1)
1042 (list (save-excursion
1043 (goto-char start)
1044 (mouse-skip-word -1)
1045 (point))
1046 (save-excursion
1047 (goto-char end)
1048 (mouse-skip-word 1)
1049 (point))))
1050 ((= mode 2)
1051 (list (save-excursion
1052 (goto-char start)
6d3e82d2 1053 (line-beginning-position 1))
e37de120
RS
1054 (save-excursion
1055 (goto-char end)
1056 (forward-line 1)
1057 (point))))))
e66feb07 1058\f
3f26b32a
RS
1059;; Subroutine: set the mark where CLICK happened,
1060;; but don't do anything else.
1061(defun mouse-set-mark-fast (click)
d65147f6 1062 (mouse-minibuffer-check click)
3f26b32a
RS
1063 (let ((posn (event-start click)))
1064 (select-window (posn-window posn))
1065 (if (numberp (posn-point posn))
1066 (push-mark (posn-point posn) t t))))
1067
98aac09d
MB
1068(defun mouse-undouble-last-event (events)
1069 (let* ((index (1- (length events)))
1070 (last (nthcdr index events))
1071 (event (car last))
1072 (basic (event-basic-type event))
397a88f3
RS
1073 (old-modifiers (event-modifiers event))
1074 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
98aac09d
MB
1075 (new
1076 (if (consp event)
7132500b
RS
1077 ;; Use reverse, not nreverse, since event-modifiers
1078 ;; does not copy the list it returns.
3bb846b8 1079 (cons (event-convert-list (reverse (cons basic modifiers)))
98aac09d
MB
1080 (cdr event))
1081 event)))
1082 (setcar last new)
397a88f3
RS
1083 (if (and (not (equal modifiers old-modifiers))
1084 (key-binding (apply 'vector events)))
98aac09d
MB
1085 t
1086 (setcar last event)
1087 nil)))
1088
7b611de0 1089;; Momentarily show where the mark is, if highlighting doesn't show it.
7aeb7370 1090
cc0a8174
JB
1091(defun mouse-set-mark (click)
1092 "Set mark at the position clicked on with the mouse.
1093Display cursor at that position for a second.
1094This must be bound to a mouse click."
ec558adc 1095 (interactive "e")
f598fb03
RS
1096 (mouse-minibuffer-check click)
1097 (select-window (posn-window (event-start click)))
1098 ;; We don't use save-excursion because that preserves the mark too.
72ea54a4
RS
1099 (let ((point-save (point)))
1100 (unwind-protect
cc0a8174 1101 (progn (mouse-set-point click)
897897e3
RS
1102 (push-mark nil t t)
1103 (or transient-mark-mode
1104 (sit-for 1)))
72ea54a4
RS
1105 (goto-char point-save))))
1106
cc0a8174
JB
1107(defun mouse-kill (click)
1108 "Kill the region between point and the mouse click.
1109The text is saved in the kill ring, as with \\[kill-region]."
ec558adc 1110 (interactive "e")
d65147f6 1111 (mouse-minibuffer-check click)
142c7672
KH
1112 (let* ((posn (event-start click))
1113 (click-posn (posn-point posn)))
1114 (select-window (posn-window posn))
bd307392
JB
1115 (if (numberp click-posn)
1116 (kill-region (min (point) click-posn)
1117 (max (point) click-posn)))))
72ea54a4 1118
87ef29fd
JB
1119(defun mouse-yank-at-click (click arg)
1120 "Insert the last stretch of killed text at the position clicked on.
0e57ab65 1121Also move point to one end of the text thus inserted (normally the end),
231c4d1a 1122and set mark at the beginning.
50f58001
RS
1123Prefix arguments are interpreted as with \\[yank].
1124If `mouse-yank-at-point' is non-nil, insert at point
9852377f 1125regardless of where you click."
a4cabe41 1126 (interactive "e\nP")
33c448cd
RS
1127 ;; Give temporary modes such as isearch a chance to turn off.
1128 (run-hooks 'mouse-leave-buffer-hook)
3f5c9cad
CY
1129 (when select-active-regions
1130 ;; Without this, confusing things happen upon e.g. inserting into
1131 ;; the middle of an active region.
6f3a2682 1132 (deactivate-mark))
50f58001 1133 (or mouse-yank-at-point (mouse-set-point click))
d89a4a47 1134 (setq this-command 'yank)
a814e9df 1135 (setq mouse-selection-click-count 0)
87ef29fd
JB
1136 (yank arg))
1137
63571b5a
RS
1138(defun mouse-yank-primary (click)
1139 "Insert the primary selection at the position clicked on.
e1eb5385
CY
1140Move point to the end of the inserted text, and set mark at
1141beginning. If `mouse-yank-at-point' is non-nil, insert at point
63571b5a
RS
1142regardless of where you click."
1143 (interactive "e")
1144 ;; Give temporary modes such as isearch a chance to turn off.
1145 (run-hooks 'mouse-leave-buffer-hook)
1c409d0b
CY
1146 ;; Without this, confusing things happen upon e.g. inserting into
1147 ;; the middle of an active region.
6c6a75d5 1148 (when select-active-regions
1c409d0b
CY
1149 (let (select-active-regions)
1150 (deactivate-mark)))
63571b5a 1151 (or mouse-yank-at-point (mouse-set-point click))
e7afcf30
EZ
1152 (let ((primary
1153 (cond
0fda9b75 1154 ((eq (framep (selected-frame)) 'w32)
e77714da
EZ
1155 ;; MS-Windows emulates PRIMARY in x-get-selection, but not
1156 ;; in x-get-selection-value (the latter only accesses the
1157 ;; clipboard). So try PRIMARY first, in case they selected
1158 ;; something with the mouse in the current Emacs session.
1159 (or (x-get-selection 'PRIMARY)
1160 (x-get-selection-value)))
1161 ((fboundp 'x-get-selection-value) ; MS-DOS and X.
1162 ;; On X, x-get-selection-value supports more formats and
1163 ;; encodings, so use it in preference to x-get-selection.
e7afcf30
EZ
1164 (or (x-get-selection-value)
1165 (x-get-selection 'PRIMARY)))
1166 ;; FIXME: What about xterm-mouse-mode etc.?
1167 (t
1168 (x-get-selection 'PRIMARY)))))
e1eb5385
CY
1169 (unless primary
1170 (error "No selection is available"))
1171 (push-mark (point))
1172 (insert primary)))
63571b5a 1173
87ef29fd 1174(defun mouse-kill-ring-save (click)
cc0a8174
JB
1175 "Copy the region between point and the mouse click in the kill ring.
1176This does not delete the region; it acts like \\[kill-ring-save]."
ec558adc 1177 (interactive "e")
3f26b32a 1178 (mouse-set-mark-fast click)
6452d8a6 1179 (let (this-command last-command)
5592c08f 1180 (kill-ring-save (point) (mark t))))
72ea54a4 1181
ad0d18af
SM
1182;; This function used to delete the text between point and the mouse
1183;; whenever it was equal to the front of the kill ring, but some
1184;; people found that confusing.
dbc4e1c1 1185
d2625c3d 1186;; The position of the last invocation of `mouse-save-then-kill'.
dbc4e1c1
JB
1187(defvar mouse-save-then-kill-posn nil)
1188
26d280b9 1189(defun mouse-save-then-kill-delete-region (beg end)
9a974c88
RS
1190 ;; We must make our own undo boundaries
1191 ;; because they happen automatically only for the current buffer.
1192 (undo-boundary)
dd524dbd
RS
1193 (if (or (= beg end) (eq buffer-undo-list t))
1194 ;; If we have no undo list in this buffer,
1195 ;; just delete.
1196 (delete-region beg end)
1197 ;; Delete, but make the undo-list entry share with the kill ring.
1198 ;; First, delete just one char, so in case buffer is being modified
1199 ;; for the first time, the undo list records that fact.
8faa9707 1200 (let (before-change-functions after-change-functions)
2f4b15ef
RS
1201 (delete-region beg
1202 (+ beg (if (> end beg) 1 -1))))
dd524dbd
RS
1203 (let ((buffer-undo-list buffer-undo-list))
1204 ;; Undo that deletion--but don't change the undo list!
8faa9707 1205 (let (before-change-functions after-change-functions)
2f4b15ef 1206 (primitive-undo 1 buffer-undo-list))
dd524dbd
RS
1207 ;; Now delete the rest of the specified region,
1208 ;; but don't record it.
1209 (setq buffer-undo-list t)
9a974c88
RS
1210 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1211 (error "Lossage in mouse-save-then-kill-delete-region"))
dd524dbd
RS
1212 (delete-region beg end))
1213 (let ((tail buffer-undo-list))
1214 ;; Search back in buffer-undo-list for the string
1215 ;; that came from deleting one character.
1216 (while (and tail (not (stringp (car (car tail)))))
1217 (setq tail (cdr tail)))
1218 ;; Replace it with an entry for the entire deleted text.
1219 (and tail
9a974c88
RS
1220 (setcar tail (cons (car kill-ring) (min beg end))))))
1221 (undo-boundary))
eb6ff46f 1222
947da0c4 1223(defun mouse-save-then-kill (click)
d2625c3d
CY
1224 "Set the region according to CLICK; the second time, kill it.
1225CLICK should be a mouse click event.
1226
1227If the region is inactive, activate it temporarily. Set mark at
1228the original point, and move point to the position of CLICK.
1229
1230If the region is already active, adjust it. Normally, do this by
1231moving point or mark, whichever is closer, to CLICK. But if you
1232have selected whole words or lines, move point or mark to the
1233word or line boundary closest to CLICK instead.
1234
93d68d4f
DDLHG
1235If `mouse-drag-copy-region' is non-nil, this command also saves the
1236new region to the kill ring (replacing the previous kill if the
1237previous region was just saved to the kill ring).
1238
d2625c3d 1239If this command is called a second consecutive time with the same
93d68d4f
DDLHG
1240CLICK position, kill the region (or delete it
1241if `mouse-drag-copy-region' is non-nil)"
947da0c4 1242 (interactive "e")
d2625c3d
CY
1243 (mouse-minibuffer-check click)
1244 (let* ((posn (event-start click))
1245 (click-pt (posn-point posn))
1246 (window (posn-window posn))
1247 (buf (window-buffer window))
1248 ;; Don't let a subsequent kill command append to this one.
1249 (this-command this-command)
1250 ;; Check if the user has multi-clicked to select words/lines.
1251 (click-count
1252 (if (and (eq mouse-selection-click-count-buffer buf)
1253 (with-current-buffer buf (mark t)))
1254 mouse-selection-click-count
1255 0)))
1256 (cond
1257 ((not (numberp click-pt)) nil)
1258 ;; If the user clicked without moving point, kill the region.
1259 ;; This also resets `mouse-selection-click-count'.
1260 ((and (eq last-command 'mouse-save-then-kill)
1261 (eq click-pt mouse-save-then-kill-posn)
1262 (eq window (selected-window)))
93d68d4f
DDLHG
1263 (if mouse-drag-copy-region
1264 ;; Region already saved in the previous click;
1265 ;; don't make a duplicate entry, just delete.
1266 (delete-region (mark t) (point))
1267 (kill-region (mark t) (point)))
d2625c3d
CY
1268 (setq mouse-selection-click-count 0)
1269 (setq mouse-save-then-kill-posn nil))
1270
1271 ;; Otherwise, if there is a suitable region, adjust it by moving
1272 ;; one end (whichever is closer) to CLICK-PT.
1273 ((or (with-current-buffer buf (region-active-p))
1274 (and (eq window (selected-window))
1275 (mark t)
1276 (or (and (eq last-command 'mouse-save-then-kill)
1277 mouse-save-then-kill-posn)
1278 (and (memq last-command '(mouse-drag-region
1279 mouse-set-region))
1280 (or mark-even-if-inactive
1281 (not transient-mark-mode))))))
1282 (select-window window)
1283 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1284 (if (< (abs (- click-pt (mark t)))
1285 (abs (- click-pt (point))))
1286 (set-mark (car range))
1287 (goto-char (nth 1 range)))
1288 (setq deactivate-mark nil)
1289 (mouse-set-region-1)
93d68d4f
DDLHG
1290 (when mouse-drag-copy-region
1291 ;; Region already copied to kill-ring once, so replace.
1292 (kill-new (filter-buffer-substring (mark t) (point)) t))
d2625c3d
CY
1293 ;; Arrange for a repeated mouse-3 to kill the region.
1294 (setq mouse-save-then-kill-posn click-pt)))
1295
1296 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1297 (t
1298 (select-window window)
1299 (mouse-set-mark-fast click)
1300 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1301 (if before-scroll (goto-char before-scroll)))
1302 (exchange-point-and-mark)
1303 (mouse-set-region-1)
93d68d4f
DDLHG
1304 (when mouse-drag-copy-region
1305 (kill-new (filter-buffer-substring (mark t) (point))))
d2625c3d
CY
1306 (setq mouse-save-then-kill-posn click-pt)))))
1307
e66feb07
RS
1308\f
1309(global-set-key [M-mouse-1] 'mouse-start-secondary)
1310(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1311(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1312(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
9a974c88 1313(global-set-key [M-mouse-2] 'mouse-yank-secondary)
e66feb07 1314
aaf15b8b
SM
1315(defconst mouse-secondary-overlay
1316 (let ((ol (make-overlay (point-min) (point-min))))
1317 (delete-overlay ol)
1318 (overlay-put ol 'face 'secondary-selection)
1319 ol)
1320 "An overlay which records the current secondary selection.
1321It is deleted when there is no secondary selection.")
e66feb07 1322
a94c7fcc
RS
1323(defvar mouse-secondary-click-count 0)
1324
e66feb07
RS
1325;; A marker which records the specified first end for a secondary selection.
1326;; May be nil.
1327(defvar mouse-secondary-start nil)
1328
1329(defun mouse-start-secondary (click)
1330 "Set one end of the secondary selection to the position clicked on.
1331Use \\[mouse-secondary-save-then-kill] to set the other end
1332and complete the secondary selection."
1333 (interactive "e")
d65147f6 1334 (mouse-minibuffer-check click)
e66feb07 1335 (let ((posn (event-start click)))
aaf15b8b 1336 (with-current-buffer (window-buffer (posn-window posn))
230aaa73 1337 ;; Cancel any preexisting secondary selection.
aaf15b8b 1338 (delete-overlay mouse-secondary-overlay)
230aaa73
RS
1339 (if (numberp (posn-point posn))
1340 (progn
1341 (or mouse-secondary-start
1342 (setq mouse-secondary-start (make-marker)))
1343 (move-marker mouse-secondary-start (posn-point posn)))))))
e66feb07
RS
1344
1345(defun mouse-set-secondary (click)
1346 "Set the secondary selection to the text that the mouse is dragged over.
1347This must be bound to a mouse drag event."
1348 (interactive "e")
d65147f6 1349 (mouse-minibuffer-check click)
e66feb07
RS
1350 (let ((posn (event-start click))
1351 beg
1352 (end (event-end click)))
aaf15b8b 1353 (with-current-buffer (window-buffer (posn-window posn))
230aaa73
RS
1354 (if (numberp (posn-point posn))
1355 (setq beg (posn-point posn)))
3e4f866b
CY
1356 (move-overlay mouse-secondary-overlay beg (posn-point end))
1357 (x-set-selection
1358 'SECONDARY
1359 (buffer-substring (overlay-start mouse-secondary-overlay)
1360 (overlay-end mouse-secondary-overlay))))))
947da0c4 1361
d89a4a47 1362(defun mouse-drag-secondary (start-event)
e66feb07 1363 "Set the secondary selection to the text that the mouse is dragged over.
d89a4a47 1364Highlight the drag area as you move the mouse.
efc10c97
RS
1365This must be bound to a button-down mouse event.
1366The function returns a non-nil value if it creates a secondary selection."
e66feb07 1367 (interactive "e")
d65147f6 1368 (mouse-minibuffer-check start-event)
4c9afcbe
RS
1369 (let* ((echo-keystrokes 0)
1370 (start-posn (event-start start-event))
d89a4a47
RS
1371 (start-point (posn-point start-posn))
1372 (start-window (posn-window start-posn))
d89a4a47
RS
1373 (bounds (window-edges start-window))
1374 (top (nth 1 bounds))
1375 (bottom (if (window-minibuffer-p start-window)
1376 (nth 3 bounds)
1377 ;; Don't count the mode line.
1378 (1- (nth 3 bounds))))
1379 (click-count (1- (event-click-count start-event))))
aaf15b8b 1380 (with-current-buffer (window-buffer start-window)
a94c7fcc 1381 (setq mouse-secondary-click-count click-count)
9a974c88 1382 (if (> (mod click-count 3) 0)
26d280b9
RS
1383 ;; Double or triple press: make an initial selection
1384 ;; of one word or line.
d89a4a47
RS
1385 (let ((range (mouse-start-end start-point start-point click-count)))
1386 (set-marker mouse-secondary-start nil)
d89a4a47
RS
1387 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1388 (window-buffer start-window)))
26d280b9 1389 ;; Single-press: cancel any preexisting secondary selection.
d89a4a47
RS
1390 (or mouse-secondary-start
1391 (setq mouse-secondary-start (make-marker)))
1392 (set-marker mouse-secondary-start start-point)
1393 (delete-overlay mouse-secondary-overlay))
1394 (let (event end end-point)
1395 (track-mouse
1396 (while (progn
1397 (setq event (read-event))
1398 (or (mouse-movement-p event)
fbd8dc8a 1399 (memq (car-safe event) '(switch-frame select-window))))
d89a4a47 1400
fbd8dc8a 1401 (if (memq (car-safe event) '(switch-frame select-window))
d89a4a47
RS
1402 nil
1403 (setq end (event-end event)
1404 end-point (posn-point end))
1405 (cond
d89a4a47
RS
1406 ;; Are we moving within the original window?
1407 ((and (eq (posn-window end) start-window)
1408 (integer-or-marker-p end-point))
d89a4a47
RS
1409 (let ((range (mouse-start-end start-point end-point
1410 click-count)))
0136e1e3
RS
1411 (if (or (/= start-point end-point)
1412 (null (marker-position mouse-secondary-start)))
1413 (progn
1414 (set-marker mouse-secondary-start nil)
1415 (move-overlay mouse-secondary-overlay
1416 (car range) (nth 1 range))))))
3b0aebe9
RS
1417 (t
1418 (let ((mouse-row (cdr (cdr (mouse-position)))))
1419 (cond
1420 ((null mouse-row))
1421 ((< mouse-row top)
e919a622
RS
1422 (mouse-scroll-subr start-window (- mouse-row top)
1423 mouse-secondary-overlay start-point))
d2287ded 1424 ((>= mouse-row bottom)
e919a622 1425 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
3b0aebe9 1426 mouse-secondary-overlay start-point)))))))))
d89a4a47 1427
4e399a53 1428 (if (consp event)
d89a4a47
RS
1429 (if (marker-position mouse-secondary-start)
1430 (save-window-excursion
1431 (delete-overlay mouse-secondary-overlay)
9a974c88 1432 (x-set-selection 'SECONDARY nil)
d89a4a47
RS
1433 (select-window start-window)
1434 (save-excursion
1435 (goto-char mouse-secondary-start)
efc10c97
RS
1436 (sit-for 1)
1437 nil))
9a974c88
RS
1438 (x-set-selection
1439 'SECONDARY
1440 (buffer-substring (overlay-start mouse-secondary-overlay)
1441 (overlay-end mouse-secondary-overlay)))))))))
e66feb07 1442
9a974c88 1443(defun mouse-yank-secondary (click)
50f58001
RS
1444 "Insert the secondary selection at the position clicked on.
1445Move point to the end of the inserted text.
1446If `mouse-yank-at-point' is non-nil, insert at point
1447regardless of where you click."
a4cabe41 1448 (interactive "e")
33c448cd
RS
1449 ;; Give temporary modes such as isearch a chance to turn off.
1450 (run-hooks 'mouse-leave-buffer-hook)
50f58001 1451 (or mouse-yank-at-point (mouse-set-point click))
b3709a53
RS
1452 (let ((secondary (x-get-selection 'SECONDARY)))
1453 (if secondary
aa2d4bd3 1454 (insert secondary)
b3709a53 1455 (error "No secondary selection"))))
9a974c88 1456
7e6404f6 1457(defun mouse-kill-secondary ()
9a974c88
RS
1458 "Kill the text in the secondary selection.
1459This is intended more as a keyboard command than as a mouse command
1460but it can work as either one.
1461
1462The current buffer (in case of keyboard use), or the buffer clicked on,
1463must be the one that the secondary selection is in. This requirement
1464is to prevent accidents."
7e6404f6
RS
1465 (interactive)
1466 (let* ((keys (this-command-keys))
1467 (click (elt keys (1- (length keys)))))
1468 (or (eq (overlay-buffer mouse-secondary-overlay)
1469 (if (listp click)
1470 (window-buffer (posn-window (event-start click)))
1471 (current-buffer)))
1472 (error "Select or click on the buffer where the secondary selection is")))
9dfab550 1473 (let (this-command)
aaf15b8b 1474 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
9dfab550
RS
1475 (kill-region (overlay-start mouse-secondary-overlay)
1476 (overlay-end mouse-secondary-overlay))))
f9be2e35 1477 (delete-overlay mouse-secondary-overlay))
e66feb07
RS
1478
1479(defun mouse-secondary-save-then-kill (click)
d2625c3d
CY
1480 "Set the secondary selection and save it to the kill ring.
1481The second time, kill it. CLICK should be a mouse click event.
1482
1483If you have not called `mouse-start-secondary' in the clicked
1484buffer, activate the secondary selection and set it between point
1485and the click position CLICK.
1486
1487Otherwise, adjust the bounds of the secondary selection.
1488Normally, do this by moving its beginning or end, whichever is
1489closer, to CLICK. But if you have selected whole words or lines,
1490adjust to the word or line boundary closest to CLICK instead.
1491
1492If this command is called a second consecutive time with the same
1493CLICK position, kill the secondary selection."
e66feb07 1494 (interactive "e")
d65147f6 1495 (mouse-minibuffer-check click)
d2625c3d
CY
1496 (let* ((posn (event-start click))
1497 (click-pt (posn-point posn))
1498 (window (posn-window posn))
1499 (buf (window-buffer window))
1500 ;; Don't let a subsequent kill command append to this one.
1501 (this-command this-command)
1502 ;; Check if the user has multi-clicked to select words/lines.
1503 (click-count
1504 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1505 mouse-secondary-click-count
1506 0))
1507 (beg (overlay-start mouse-secondary-overlay))
1508 (end (overlay-end mouse-secondary-overlay)))
1509
1510 (cond
1511 ((not (numberp click-pt)) nil)
1512
1513 ;; If the secondary selection is not active in BUF, activate it.
1514 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1515 (if mouse-secondary-start
1516 (marker-buffer mouse-secondary-start)))))
1517 (select-window window)
1518 (setq mouse-secondary-start (make-marker))
1519 (move-marker mouse-secondary-start (point))
1520 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1521 (kill-ring-save (point) click-pt))
1522
1523 ;; If the user clicked without moving point, delete the secondary
1524 ;; selection. This also resets `mouse-secondary-click-count'.
1525 ((and (eq last-command 'mouse-secondary-save-then-kill)
1526 (eq click-pt mouse-save-then-kill-posn)
1527 (eq window (selected-window)))
1528 (mouse-save-then-kill-delete-region beg end)
1529 (delete-overlay mouse-secondary-overlay)
1530 (setq mouse-secondary-click-count 0)
1531 (setq mouse-save-then-kill-posn nil))
1532
1533 ;; Otherwise, if there is a suitable secondary selection overlay,
1534 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1535 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1536 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1537 (if (< (abs (- click-pt beg))
1538 (abs (- click-pt end)))
1539 (move-overlay mouse-secondary-overlay (car range) end)
1540 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1541 (setq deactivate-mark nil)
1542 (if (eq last-command 'mouse-secondary-save-then-kill)
1543 ;; If the front of the kill ring comes from an immediately
1544 ;; previous use of this command, replace the entry.
1545 (kill-new
1546 (buffer-substring (overlay-start mouse-secondary-overlay)
1547 (overlay-end mouse-secondary-overlay))
1548 t)
1549 (let (deactivate-mark)
1550 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1551 (overlay-end mouse-secondary-overlay))))
1552 (setq mouse-save-then-kill-posn click-pt))
1553
1554 ;; Otherwise, set the secondary selection overlay.
1555 (t
1556 (select-window window)
1557 (if mouse-secondary-start
1558 ;; All we have is one end of a selection, so put the other
1559 ;; end here.
1560 (let ((start (+ 0 mouse-secondary-start)))
1561 (kill-ring-save start click-pt)
1562 (move-overlay mouse-secondary-overlay start click-pt)))
1563 (setq mouse-save-then-kill-posn click-pt))))
1564
1565 ;; Finally, set the window system's secondary selection.
1566 (let (str)
1567 (and (overlay-buffer mouse-secondary-overlay)
1568 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1569 (overlay-end mouse-secondary-overlay)))
1570 (> (length str) 0)
1571 (x-set-selection 'SECONDARY str))))
1572
e66feb07 1573\f
0d9be91c 1574(defcustom mouse-buffer-menu-maxlen 20
9201cc28 1575 "Number of buffers in one pane (submenu) of the buffer menu.
f1107960 1576If we have lots of buffers, divide them into groups of
0d9be91c 1577`mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
3b558d41
RS
1578 :type 'integer
1579 :group 'mouse)
f1107960 1580
3ca87c7b 1581(defcustom mouse-buffer-menu-mode-mult 4
9201cc28 1582 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
3ca87c7b
RS
1583This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1584will split the buffer menu by the major modes (see
1585`mouse-buffer-menu-mode-groups') or just by menu length.
1586Set to 1 (or even 0!) if you want to group by major mode always, and to
1587a large number if you prefer a mixed multitude. The default is 4."
1588 :type 'integer
1589 :group 'mouse
1590 :version "20.3")
1591
284a88a3 1592(defvar mouse-buffer-menu-mode-groups
e8e4d5c8 1593 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
284a88a3
RS
1594 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1595 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1596 . "Mail/News")
1597 ("\\<C\\>" . "C")
1598 ("ObjC" . "C")
1599 ("Text" . "Text")
1600 ("Outline" . "Text")
c71a58a3 1601 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
f399e4ad 1602 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
7bc61bb7
SS
1603 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1604 . "GDB")
a7610c52 1605 ("Lisp" . "Lisp")))
284a88a3
RS
1606 "How to group various major modes together in \\[mouse-buffer-menu].
1607Each element has the form (REGEXP . GROUPNAME).
1608If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1609
8b34e79d 1610(defun mouse-buffer-menu (event)
2d82f7b9
RS
1611 "Pop up a menu of buffers for selection with the mouse.
1612This switches buffers in the window that you clicked on,
1613and selects that window."
ec558adc 1614 (interactive "e")
d65147f6 1615 (mouse-minibuffer-check event)
3ca87c7b 1616 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
284a88a3 1617 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
cc2ee8ec
SS
1618 (dolist (buf buffers)
1619 ;; Divide all buffers into buckets for various major modes.
1620 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1621 (with-current-buffer buf
1622 (let* ((adjusted-major-mode major-mode) elt)
1623 (dolist (group mouse-buffer-menu-mode-groups)
1624 (when (string-match (car group) (format-mode-line mode-name))
1625 (setq adjusted-major-mode (cdr group))))
1626 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1627 (unless elt
1628 (setq elt (list adjusted-major-mode
1629 (if (stringp adjusted-major-mode)
1630 adjusted-major-mode
48d33090 1631 (format-mode-line mode-name nil nil buf)))
cc2ee8ec
SS
1632 split-by-major-mode (cons elt split-by-major-mode)))
1633 (or (memq buf (cdr (cdr elt)))
1634 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
284a88a3
RS
1635 ;; Compute the sum of squares of sizes of the major-mode buckets.
1636 (let ((tail split-by-major-mode))
1637 (setq sum-of-squares 0)
1638 (while tail
1639 (setq sum-of-squares
1640 (+ sum-of-squares
3ca87c7b 1641 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
284a88a3 1642 (setq tail (cdr tail))))
3ca87c7b
RS
1643 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1644 (* (length buffers) (length buffers)))
284a88a3
RS
1645 ;; Subdividing by major modes really helps, so let's do it.
1646 (let (subdivided-menus (buffers-left (length buffers)))
1647 ;; Sort the list to put the most popular major modes first.
1648 (setq split-by-major-mode
1649 (sort split-by-major-mode
1650 (function (lambda (elt1 elt2)
1651 (> (length elt1) (length elt2))))))
1652 ;; Make a separate submenu for each major mode
1653 ;; that has more than one buffer,
1654 ;; unless all the remaining buffers are less than 1/10 of them.
1655 (while (and split-by-major-mode
1656 (and (> (length (car split-by-major-mode)) 3)
1657 (> (* buffers-left 10) (length buffers))))
4b4ea1dc
EZ
1658 (let ((this-mode-list (mouse-buffer-menu-alist
1659 (cdr (cdr (car split-by-major-mode))))))
1660 (and this-mode-list
1661 (setq subdivided-menus
1662 (cons (cons
1663 (nth 1 (car split-by-major-mode))
1664 this-mode-list)
1665 subdivided-menus))))
284a88a3
RS
1666 (setq buffers-left
1667 (- buffers-left (length (cdr (car split-by-major-mode)))))
1668 (setq split-by-major-mode (cdr split-by-major-mode)))
1669 ;; If any major modes are left over,
1670 ;; make a single submenu for them.
1671 (if split-by-major-mode
4b4ea1dc
EZ
1672 (let ((others-list
1673 (mouse-buffer-menu-alist
1674 ;; we don't need split-by-major-mode any more,
1675 ;; so we can ditch it with nconc.
1676 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1677 (and others-list
1678 (setq subdivided-menus
1679 (cons (cons "Others" others-list)
1680 subdivided-menus)))))
3ca87c7b 1681 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
284a88a3
RS
1682 (progn
1683 (setq alist (mouse-buffer-menu-alist buffers))
1684 (setq menu (cons "Buffer Menu"
1685 (mouse-buffer-menu-split "Select Buffer" alist)))))
2d82f7b9
RS
1686 (let ((buf (x-popup-menu event menu))
1687 (window (posn-window (event-start event))))
3ca87c7b 1688 (when buf
f134ad18
RS
1689 (select-window
1690 (if (framep window) (frame-selected-window window)
1691 window))
3ca87c7b 1692 (switch-to-buffer buf)))))
284a88a3
RS
1693
1694(defun mouse-buffer-menu-alist (buffers)
1695 (let (tail
1696 (maxlen 0)
1697 head)
1698 (setq buffers
1699 (sort buffers
1700 (function (lambda (elt1 elt2)
1701 (string< (buffer-name elt1) (buffer-name elt2))))))
1702 (setq tail buffers)
1703 (while tail
fc2b429e 1704 (or (eq ?\s (aref (buffer-name (car tail)) 0))
284a88a3
RS
1705 (setq maxlen
1706 (max maxlen
1707 (length (buffer-name (car tail))))))
1708 (setq tail (cdr tail)))
1709 (setq tail buffers)
1710 (while tail
1711 (let ((elt (car tail)))
66564ab0 1712 (if (/= (aref (buffer-name elt) 0) ?\s)
284a88a3
RS
1713 (setq head
1714 (cons
1715 (cons
1716 (format
fc1f472b 1717 (format "%%-%ds %%s%%s %%s" maxlen)
284a88a3
RS
1718 (buffer-name elt)
1719 (if (buffer-modified-p elt) "*" " ")
7fdbcd83 1720 (with-current-buffer elt
284a88a3 1721 (if buffer-read-only "%" " "))
7b611de0 1722 (or (buffer-file-name elt)
7fdbcd83 1723 (with-current-buffer elt
284a88a3
RS
1724 (if list-buffers-directory
1725 (expand-file-name
1726 list-buffers-directory)))
1727 ""))
1728 elt)
1729 head))))
1730 (setq tail (cdr tail)))
1731 ;; Compensate for the reversal that the above loop does.
1732 (nreverse head)))
1733
1734(defun mouse-buffer-menu-split (title alist)
1735 ;; If we have lots of buffers, divide them into groups of 20
1736 ;; and make a pane (or submenu) for each one.
0d9be91c 1737 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
284a88a3
RS
1738 (let ((alist alist) sublists next
1739 (i 1))
1740 (while alist
0d9be91c 1741 ;; Pull off the next mouse-buffer-menu-maxlen buffers
284a88a3 1742 ;; and make them the next element of sublist.
0d9be91c 1743 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
284a88a3 1744 (if next
0d9be91c 1745 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
284a88a3
RS
1746 nil))
1747 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1748 sublists))
1749 (setq i (1+ i))
1750 (setq alist next))
1751 (nreverse sublists))
1752 ;; Few buffers--put them all in one pane.
1753 (list (cons title alist))))
72ea54a4 1754\f
d5e63715
SM
1755(define-obsolete-function-alias
1756 'mouse-choose-completion 'choose-completion "23.2")
f936ae06 1757
07a78410
RS
1758;; Font selection.
1759
0eb9fef3
RS
1760(defun font-menu-add-default ()
1761 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1762 (font-alist x-fixed-font-alist)
0d94f5ca 1763 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
0eb9fef3
RS
1764 (if (assoc "Default" elt)
1765 (delete (assoc "Default" elt) elt))
1766 (setcdr elt
6b8e486e 1767 (cons (list "Default" default)
0eb9fef3
RS
1768 (cdr elt)))))
1769
07a78410 1770(defvar x-fixed-font-alist
a7610c52
DN
1771 (list
1772 (purecopy "Font Menu")
1773 (cons
1774 (purecopy "Misc")
1775 (mapcar
1776 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
19d973e8 1777 ;; For these, we specify the pixel height and width.
a7610c52 1778 '(("fixed" "fixed")
19d973e8
RS
1779 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1780 ("6x12"
1781 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1782 ("6x13"
1783 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1784 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1785 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1786 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1787 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1788 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1789 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1790 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
fa21fdec 1791 ("")
f29c3695
RS
1792 ("clean 5x8"
1793 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1794 ("clean 6x8"
1795 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
19d973e8
RS
1796 ("clean 8x8"
1797 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1798 ("clean 8x10"
1799 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1800 ("clean 8x14"
1801 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1802 ("clean 8x16"
1803 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
fa21fdec 1804 ("")
29a3028d 1805 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
ad0d18af
SM
1806 ;; We don't seem to have these; who knows what they are.
1807 ;; ("fg-18" "fg-18")
1808 ;; ("fg-25" "fg-25")
29a3028d
DL
1809 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1810 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1811 ("lucidasanstypewriter-bold-24"
1812 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
ad0d18af
SM
1813 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1814 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
a7610c52
DN
1815 )))
1816
1817 (cons
1818 (purecopy "Courier")
1819 (mapcar
1820 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
19d973e8 1821 ;; For these, we specify the point height.
a7610c52 1822 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
82c048a9
RS
1823 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1824 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1825 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1826 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1827 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1828 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1829 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1830 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1831 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1832 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1833 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1834 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1835 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1836 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1837 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1838 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1839 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1840 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1841 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1842 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1843 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1844 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
a7610c52
DN
1845 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
1846 ))))
07a78410
RS
1847 "X fonts suitable for use in Emacs.")
1848
aa360da1
GM
1849(declare-function generate-fontset-menu "fontset" ())
1850
ecb044e1
CY
1851(defun mouse-select-font ()
1852 "Prompt for a font name, using `x-popup-menu', and return it."
1853 (interactive)
1854 (unless (display-multi-font-p)
1855 (error "Cannot change fonts on this display"))
c1202d4b
CY
1856 (car
1857 (x-popup-menu
1858 (if (listp last-nonmenu-event)
1859 last-nonmenu-event
1860 (list '(0 0) (selected-window)))
1861 (append x-fixed-font-alist
1862 (list (generate-fontset-menu))))))
ecb044e1 1863
3db09e26
CY
1864(declare-function text-scale-mode "face-remap")
1865
1900a92b 1866(defun mouse-set-font (&rest fonts)
ecb044e1
CY
1867 "Set the default font for the selected frame.
1868The argument FONTS is a list of font names; the first valid font
1869in this list is used.
1870
1871When called interactively, pop up a menu and allow the user to
1872choose a font."
07a78410 1873 (interactive
8eb1dc02
RS
1874 (progn (unless (display-multi-font-p)
1875 (error "Cannot change fonts on this display"))
1876 (x-popup-menu
91c51412
LT
1877 (if (listp last-nonmenu-event)
1878 last-nonmenu-event
1879 (list '(0 0) (selected-window)))
8eb1dc02
RS
1880 ;; Append list of fontsets currently defined.
1881 (append x-fixed-font-alist (list (generate-fontset-menu))))))
df4de8c6
KH
1882 (if fonts
1883 (let (font)
1884 (while fonts
1885 (condition-case nil
1886 (progn
b7d1d101 1887 (set-frame-font (car fonts))
df4de8c6
KH
1888 (setq font (car fonts))
1889 (setq fonts nil))
3fadec1a
RS
1890 (error
1891 (setq fonts (cdr fonts)))))
df4de8c6 1892 (if (null font)
8eb1dc02 1893 (error "Font not found")))))
3db09e26
CY
1894
1895(defvar mouse-appearance-menu-map nil)
9c257005 1896(declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
b816c5e3
GM
1897(declare-function buffer-face-mode-invoke "face-remap"
1898 (face arg &optional interactive))
f2d9c15f 1899(declare-function font-face-attributes "font.c" (font &optional frame))
3db09e26
CY
1900
1901(defun mouse-appearance-menu (event)
e8579ebc 1902 "Show a menu for changing the default face in the current buffer."
3db09e26
CY
1903 (interactive "@e")
1904 (require 'face-remap)
1905 (when (display-multi-font-p)
1906 (with-selected-window (car (event-start event))
1907 (if mouse-appearance-menu-map
1908 nil ; regenerate new fonts
1909 ;; Initialize mouse-appearance-menu-map
1910 (setq mouse-appearance-menu-map
1911 (make-sparse-keymap "Change Default Buffer Face"))
1912 (define-key mouse-appearance-menu-map [face-remap-reset-base]
1913 '(menu-item "Reset to Default" face-remap-reset-base))
1914 (define-key mouse-appearance-menu-map [text-scale-decrease]
1915 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
1916 (define-key mouse-appearance-menu-map [text-scale-increase]
1917 '(menu-item "Increase Buffer Text Size" text-scale-increase))
1918 ;; Font selector
1919 (if (functionp 'x-select-font)
1920 (define-key mouse-appearance-menu-map [x-select-font]
1921 '(menu-item "Change Buffer Font..." x-select-font))
1922 ;; If the select-font is unavailable, construct a menu.
1923 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
1924 (font-alist (cdr (append x-fixed-font-alist
1925 (list (generate-fontset-menu))))))
1926 (dolist (family font-alist)
1927 (let* ((submenu-name (car family))
1928 (submenu-map (make-sparse-keymap submenu-name)))
1929 (dolist (font (cdr family))
1930 (let ((font-name (car font))
1931 font-symbol)
1932 (if (string= font-name "")
1933 (define-key submenu-map [space]
1934 '("--"))
1935 (setq font-symbol (intern (cadr font)))
1936 (define-key submenu-map (vector font-symbol)
1937 (list 'menu-item (car font) font-symbol)))))
1938 (define-key font-submenu (vector (intern submenu-name))
1939 (list 'menu-item submenu-name submenu-map))))
1940 (define-key mouse-appearance-menu-map [font-submenu]
1941 (list 'menu-item "Change Text Font" font-submenu)))))
1942 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
1943 (setq choice (nth (1- (length choice)) choice))
1944 (cond ((eq choice 'text-scale-increase)
1945 (text-scale-increase 1))
1946 ((eq choice 'text-scale-decrease)
1947 (text-scale-increase -1))
1948 ((eq choice 'face-remap-reset-base)
1949 (text-scale-mode 0)
4271b424 1950 (buffer-face-mode 0))
520713e7 1951 (choice
3db09e26
CY
1952 ;; Either choice == 'x-select-font, or choice is a
1953 ;; symbol whose name is a font.
383dcbf9
CY
1954 (let ((font (if (eq choice 'x-select-font)
1955 (x-select-font)
1956 (symbol-name choice))))
1957 (buffer-face-mode-invoke
1958 (if (fontp font 'font-spec)
1959 (list :font font)
1960 (font-face-attributes font))
1961 t (called-interactively-p 'interactive)))))))))
3db09e26 1962
cc0a8174
JB
1963\f
1964;;; Bindings for mouse commands.
1965
fcfc3c63 1966(define-key global-map [down-mouse-1] 'mouse-drag-region)
dbc4e1c1 1967(global-set-key [mouse-1] 'mouse-set-point)
dbc4e1c1 1968(global-set-key [drag-mouse-1] 'mouse-set-region)
fcfc3c63 1969
e37de120
RS
1970;; These are tested for in mouse-drag-region.
1971(global-set-key [double-mouse-1] 'mouse-set-point)
1972(global-set-key [triple-mouse-1] 'mouse-set-point)
1973
fe3f64d5
SM
1974(defun mouse--strip-first-event (_prompt)
1975 (substring (this-single-command-raw-keys) 1))
1976
1977(define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
1978(define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
76693d12 1979
f9d71b42 1980(global-set-key [mouse-2] 'mouse-yank-primary)
3e9323d6 1981;; Allow yanking also when the corresponding cursor is "in the fringe".
fe3f64d5
SM
1982(define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
1983(define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
dbc4e1c1 1984(global-set-key [mouse-3] 'mouse-save-then-kill)
fe3f64d5
SM
1985(define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
1986(define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
8b34e79d 1987
dbc4e1c1
JB
1988;; By binding these to down-going events, we let the user use the up-going
1989;; event to make the selection, saving a click.
08a1c178
RS
1990(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1991(if (not (eq system-type 'ms-dos))
3db09e26 1992 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
eef805d7 1993;; C-down-mouse-2 is bound in facemenu.el.
0b2b62ff 1994(global-set-key [C-down-mouse-3]
1e8780b1 1995 `(menu-item ,(purecopy "Menu Bar") ignore
0b2b62ff
SM
1996 :filter (lambda (_)
1997 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
1998 (mouse-menu-bar-map)
1999 (mouse-menu-major-mode-map)))))
95132d1c 2000
06a8c9f8
EZ
2001;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2002;; vertical-line prevents Emacs from signaling an error when the mouse
2003;; button is released after dragging these lines, on non-toolkit
2004;; versions.
3c2dd2c0 2005(global-set-key [mode-line mouse-1] 'mouse-select-window)
544e7e73
RS
2006(global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2007(global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
b0d22e20 2008(global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
06a8c9f8 2009(global-set-key [header-line mouse-1] 'mouse-select-window)
3c2dd2c0 2010(global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
dbc4e1c1 2011(global-set-key [mode-line mouse-3] 'mouse-delete-window)
3c2dd2c0 2012(global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
9926ab64 2013(global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
b6522df6 2014(global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
08a1c178
RS
2015(global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2016(global-set-key [vertical-line mouse-1] 'mouse-select-window)
49116ac0
JB
2017
2018(provide 'mouse)
2019
6594deb0 2020;;; mouse.el ends here