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