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