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