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