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