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