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