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