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