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