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