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