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