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