mouse.el 90.2 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; mouse.el --- window system-independent mouse support
Eric S. Raymond's avatar
Eric S. Raymond committed
2

3
;; Copyright (C) 1993, 94, 95, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4
;;   Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Maintainer: FSF
7
;; Keywords: hardware, mouse
Eric S. Raymond's avatar
Eric S. Raymond committed
8

Richard M. Stallman's avatar
Richard M. Stallman committed
9
;; This file is part of GNU Emacs.
Richard M. Stallman's avatar
Richard M. Stallman committed
10

Richard M. Stallman's avatar
Richard M. Stallman committed
11 12 13 14
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
15

Richard M. Stallman's avatar
Richard M. Stallman committed
16 17 18 19
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
Richard M. Stallman's avatar
Richard M. Stallman committed
20

Richard M. Stallman's avatar
Richard M. Stallman committed
21
;; You should have received a copy of the GNU General Public License
22 23 24
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
25

26 27 28 29 30 31 32 33
;;; Commentary:

;; This package provides various useful commands (including help
;; system access) through the mouse.  All this code assumes that mouse
;; interpretation has been abstracted into Emacs input events.
;;
;; The code is rather X-dependent.

34 35
;;; Code:

Jim Blandy's avatar
Jim Blandy committed
36
;;; Utility functions.
Richard M. Stallman's avatar
Richard M. Stallman committed
37

Jim Blandy's avatar
Jim Blandy committed
38 39
;;; Indent track-mouse like progn.
(put 'track-mouse 'lisp-indent-function 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
40

Richard M. Stallman's avatar
Richard M. Stallman committed
41 42 43 44
(defcustom mouse-yank-at-point nil
  "*If non-nil, mouse yank commands yank at point instead of at click."
  :type 'boolean
  :group 'mouse)
45 46 47 48

(defcustom mouse-drag-copy-region t
  "*If non-nil, mouse drag copies region to kill-ring."
  :type 'boolean
49
  :version "22.1"
50 51
  :group 'mouse)

52
(defcustom mouse-1-click-follows-link 350
53
  "Non-nil means that clicking Mouse-1 on a link follows the link.
54

55 56 57 58
With the default setting, an ordinary Mouse-1 click on a link
performs the same action as Mouse-2 on that link, while a longer
Mouse-1 click \(hold down the Mouse-1 button for more than 350
milliseconds) performs the original Mouse-1 binding \(which
59 60 61 62
typically sets point where you click the mouse).

If value is an integer, the time elapsed between pressing and
releasing the mouse button determines whether to follow the link
63
or perform the normal Mouse-1 action (typically set point).
64 65 66 67 68 69 70
The absolute numeric value specifices the maximum duration of a
\"short click\" in milliseconds.  A positive value means that a
short click follows the link, and a longer click performs the
normal action.  A negative value gives the opposite behaviour.

If value is `double', a double click follows the link.

71
Otherwise, a single Mouse-1 click unconditionally follows the link.
72 73 74 75 76 77

Note that dragging the mouse never follows the link.

This feature only works in modes that specifically identify
clickable text as links, so it may not work with some external
packages.  See `mouse-on-link-p' for details."
78
  :version "22.1"
79 80 81 82 83 84
  :type '(choice (const :tag "Disabled" nil)
		 (const :tag "Double click" double)
                 (number :tag "Single click time limit" :value 350)
                 (other :tag "Single click" t))
  :group 'mouse)

Jim Blandy's avatar
Jim Blandy committed
85

86 87
;; Provide a mode-specific menu on a mouse button.

Stefan Monnier's avatar
Stefan Monnier committed
88 89
(defun popup-menu (menu &optional position prefix)
  "Popup the given menu and call the selected option.
90 91
MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
`x-popup-menu'.
Stefan Monnier's avatar
Stefan Monnier committed
92 93 94
POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
  the current mouse position.
PREFIX is the prefix argument (if any) to pass to the command."
95 96 97 98
  (let* ((map (cond
	       ((keymapp menu) menu)
	       ((and (listp menu) (keymapp (car menu))) menu)
	       (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
Stefan Monnier's avatar
Stefan Monnier committed
99 100 101
			 (filter (when (symbolp map)
				   (plist-get (get map 'menu-prop) :filter))))
		    (if filter (funcall filter (symbol-function map)) map)))))
102
	 event cmd)
103
    (unless position
Dave Love's avatar
Dave Love committed
104
      (let ((mp (mouse-pixel-position)))
105
	(setq position (list (list (cadr mp) (cddr mp)) (car mp)))))
Stefan Monnier's avatar
Stefan Monnier committed
106
    ;; The looping behavior was taken from lmenu's popup-menu-popup
107 108 109 110
    (while (and map (setq event
			  ;; map could be a prefix key, in which case
			  ;; we need to get its function cell
			  ;; definition.
Dave Love's avatar
Dave Love committed
111
			  (x-popup-menu position (indirect-function map))))
Stefan Monnier's avatar
Stefan Monnier committed
112 113 114
      ;; Strangely x-popup-menu returns a list.
      ;; mouse-major-mode-menu was using a weird:
      ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
      (setq cmd
	    (if (and (not (keymapp map)) (listp map))
		;; We were given a list of keymaps.  Search them all
		;; in sequence until a first binding is found.
		(let ((mouse-click (apply 'vector event))
		      binding)
		  (while (and map (null binding))
		    (setq binding (lookup-key (car map) mouse-click))
		    (if (numberp binding) ; `too long'
			(setq binding nil))
		    (setq map (cdr map)))
		  binding)
	      ;; We were given a single keymap.
	      (lookup-key map (apply 'vector event))))
      ;; Clear out echoing, which perhaps shows a prefix arg.
      (message "")
      ;; Maybe try again but with the submap.
      (setq map (if (keymapp cmd) cmd)))
133 134 135
    ;; If the user did not cancel by refusing to select,
    ;; and if the result is a command, run it.
    (when (and (null map) (commandp cmd))
136 137 138 139 140
      (setq prefix-arg prefix)
      ;; `setup-specified-language-environment', for instance,
      ;; expects this to be set from a menu keymap.
      (setq last-command-event (car (last event)))
      ;; mouse-major-mode-menu was using `command-execute' instead.
Miles Bader's avatar
Miles Bader committed
141
      (call-interactively cmd))))
142 143 144

(defvar mouse-major-mode-menu-prefix)	; dynamically bound

145
(defun mouse-major-mode-menu (event prefix)
146 147
  "Pop up a mode-specific menu of mouse commands.
Default to the Edit menu if the major mode doesn't define a menu."
148 149
  ;; Switch to the window clicked on, because otherwise
  ;; the mode's commands may not make sense.
150
  (interactive "@e\nP")
151
  ;; Let the mode update its menus first.
152
  (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
153 154 155 156 157 158 159
  (let* (;; This is where mouse-major-mode-menu-prefix
	 ;; returns the prefix we should use (after menu-bar).
	 ;; It is either nil or (SOME-SYMBOL).
	 (mouse-major-mode-menu-prefix nil)
	 ;; Keymap from which to inherit; may be null.
	 (ancestor (mouse-major-mode-menu-1
		    (and (current-local-map)
160
			 (local-key-binding [menu-bar]))))
161 162 163 164 165 166 167 168 169 170
	 ;; Make a keymap in which our last command leads to a menu or
	 ;; default to the edit menu.
	 (newmap (if ancestor
		     (make-sparse-keymap (concat mode-name " Mode"))
		   menu-bar-edit-menu))
	 result)
    (if ancestor
	;; Make our menu inherit from the desired keymap which we want
	;; to display as the menu now.
	(set-keymap-parent newmap ancestor))
Stefan Monnier's avatar
Stefan Monnier committed
171 172
    (popup-menu newmap event prefix)))

173 174

;; Compute and cache the equivalent keys in MENU and all its submenus.
175 176 177 178 179 180 181 182 183 184 185 186 187
;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
;;;  (and (eq (car menu) 'keymap)
;;;       (x-popup-menu nil menu))
;;;  (while menu
;;;    (and (consp (car menu))
;;;	 (consp (cdr (car menu)))
;;;	 (let ((tail (cdr (car menu))))
;;;	   (while (and (consp tail)
;;;		       (not (eq (car tail) 'keymap)))
;;;	     (setq tail (cdr tail)))
;;;	   (if (consp tail)
;;;	       (mouse-major-mode-menu-compute-equiv-keys tail))))
;;;    (setq menu (cdr menu))))
188 189 190 191 192 193 194 195 196 197 198 199 200

;; Given a mode's menu bar keymap,
;; if it defines exactly one menu bar menu,
;; return just that menu.
;; Otherwise return a menu for all of them.
(defun mouse-major-mode-menu-1 (menubar)
  (if menubar
      (let ((tail menubar)
	    submap)
	(while tail
	  (if (consp (car tail))
	      (if submap
		  (setq submap t)
201
		(setq submap (car tail))))
202
	  (setq tail (cdr tail)))
203
	(if (eq submap t)
204
	    menubar
205
	  (setq mouse-major-mode-menu-prefix (list (car submap)))
206
	  (lookup-key menubar (vector (car submap)))))))
207 208

(defun mouse-popup-menubar (event prefix)
209
  "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
210 211 212
The contents are the items that would be in the menu bar whether or
not it is actually displayed."
  (interactive "@e \nP")
213
  (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
214 215 216
  (let* ((local-menu (and (current-local-map)
			  (lookup-key (current-local-map) [menu-bar])))
	 (global-menu (lookup-key global-map [menu-bar]))
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	 ;; If a keymap doesn't have a prompt string (a lazy
	 ;; programmer didn't bother to provide one), create it and
	 ;; insert it into the keymap; each keymap gets its own
	 ;; prompt.  This is required for non-toolkit versions to
	 ;; display non-empty menu pane names.
	 (minor-mode-menus
	  (mapcar
	   (function
	    (lambda (menu)
	      (let* ((minor-mode (car menu))
		     (menu (cdr menu))
		     (title-or-map (cadr menu)))
		(or (stringp title-or-map)
		    (setq menu
			  (cons 'keymap
				(cons (concat
				       (capitalize (subst-char-in-string
						    ?- ?\  (symbol-name
							    minor-mode)))
				       " Menu")
				      (cdr menu)))))
		menu)))
	   (minor-mode-key-binding [menu-bar])))
240 241 242 243 244 245 246 247 248 249 250
	 (local-title-or-map (and local-menu (cadr local-menu)))
	 (global-title-or-map (cadr global-menu)))
    (or (null local-menu)
	(stringp local-title-or-map)
	(setq local-menu (cons 'keymap
			       (cons (concat mode-name " Mode Menu")
				     (cdr local-menu)))))
    (or (stringp global-title-or-map)
	(setq global-menu (cons 'keymap
			        (cons "Global Menu"
				      (cdr global-menu)))))
251
    ;; Supplying the list is faster than making a new map.
252 253 254 255
    (popup-menu (append (list global-menu)
			(if local-menu
			    (list local-menu))
			minor-mode-menus)
256
		event prefix)))
257 258 259 260 261 262 263 264

(defun mouse-popup-menubar-stuff (event prefix)
  "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
Use the former if the menu bar is showing, otherwise the latter."
  (interactive "@e \nP")
  (if (zerop (assoc-default 'menu-bar-lines (frame-parameters) 'eq 0))
      (mouse-popup-menubar event prefix)
    (mouse-major-mode-menu event prefix)))
265

266 267
;; Commands that operate on windows.

268 269 270 271
(defun mouse-minibuffer-check (event)
  (let ((w (posn-window (event-start event))))
    (and (window-minibuffer-p w)
	 (not (minibuffer-window-active-p w))
272 273 274
	 (error "Minibuffer window is not active")))
  ;; Give temporary modes such as isearch a chance to turn off.
  (run-hooks 'mouse-leave-buffer-hook))
275

Jim Blandy's avatar
Jim Blandy committed
276
(defun mouse-delete-window (click)
277
  "Delete the window you click on.
278
Do nothing if the frame has just one window.
279
This command must be bound to a mouse click."
Jim Blandy's avatar
Jim Blandy committed
280
  (interactive "e")
281
  (unless (one-window-p t)
282 283
    (mouse-minibuffer-check click)
    (delete-window (posn-window (event-start click)))))
Jim Blandy's avatar
Jim Blandy committed
284

285 286 287
(defun mouse-select-window (click)
  "Select the window clicked on; don't move point."
  (interactive "e")
288
  (mouse-minibuffer-check click)
289 290 291 292 293 294
  (let ((oframe (selected-frame))
	(frame (window-frame (posn-window (event-start click)))))
    (select-window (posn-window (event-start click)))
    (raise-frame frame)
    (select-frame frame)
    (or (eq frame oframe)
295
	(set-mouse-position (selected-frame) (1- (frame-width)) 0))))
296

297 298 299
(defun mouse-tear-off-window (click)
  "Delete the window clicked on, and create a new frame displaying its buffer."
  (interactive "e")
300
  (mouse-minibuffer-check click)
301 302
  (let* ((window (posn-window (event-start click)))
	 (buf (window-buffer window))
303
	 (frame (make-frame)))
304 305 306 307
    (select-frame frame)
    (switch-to-buffer buf)
    (delete-window window)))

308
(defun mouse-delete-other-windows ()
309
  "Delete all windows except the one you click on."
310
  (interactive "@")
Jim Blandy's avatar
Jim Blandy committed
311
  (delete-other-windows))
Richard M. Stallman's avatar
Richard M. Stallman committed
312

Jim Blandy's avatar
Jim Blandy committed
313 314 315 316
(defun mouse-split-window-vertically (click)
  "Select Emacs window mouse is on, then split it vertically in half.
The window is split at the line clicked on.
This command must be bound to a mouse click."
317
  (interactive "@e")
318
  (mouse-minibuffer-check click)
319 320
  (let ((start (event-start click)))
    (select-window (posn-window start))
321
    (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
322 323 324
	  (first-line window-min-height)
	  (last-line (- (window-height) window-min-height)))
      (if (< last-line first-line)
325
	  (error "Window too short to split")
326 327
	(split-window-vertically
	 (min (max new-height first-line) last-line))))))
Jim Blandy's avatar
Jim Blandy committed
328

329 330 331 332 333
(defun mouse-split-window-horizontally (click)
  "Select Emacs window mouse is on, then split it horizontally in half.
The window is split at the column clicked on.
This command must be bound to a mouse click."
  (interactive "@e")
334
  (mouse-minibuffer-check click)
335 336 337 338 339 340
  (let ((start (event-start click)))
    (select-window (posn-window start))
    (let ((new-width (1+ (car (posn-col-row (event-end click)))))
	  (first-col window-min-width)
	  (last-col (- (window-width) window-min-width)))
      (if (< last-col first-col)
341
	  (error "Window too narrow to split")
342 343
	(split-window-horizontally
	 (min (max new-width first-col) last-col))))))
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
(defun mouse-drag-window-above (window)
  "Return the (or a) window directly above WINDOW.
That means one whose bottom edge is at the same height as WINDOW's top edge."
  (let ((top (nth 1 (window-edges window)))
	(start-window window)
	above-window)
    (setq window (previous-window window 0))
    (while (and (not above-window) (not (eq window start-window)))
      (if (= (+ (window-height window) (nth 1 (window-edges window)))
	     top)
	  (setq above-window window))
      (setq window (previous-window window)))
    above-window))

(defun mouse-drag-move-window-bottom (window growth)
  "Move the bottom of WINDOW up or down by GROWTH lines.
Move it down if GROWTH is positive, or up if GROWTH is negative.
If this would make WINDOW too short,
shrink the window or windows above it to make room."
  (let ((excess (- window-min-height (+ (window-height window) growth))))
    ;; EXCESS is the number of lines we need to take from windows above.
    (if (> excess 0)
	;; This can recursively shrink windows all the way up.
	(let ((window-above (mouse-drag-window-above window)))
	  (if window-above
	      (mouse-drag-move-window-bottom window-above (- excess))))))
  (save-selected-window
    (select-window window)
    (enlarge-window growth nil (> growth 0))))

375 376 377 378 379 380 381 382 383 384 385
(defsubst mouse-drag-move-window-top (window growth)
  "Move the top of WINDOW up or down by GROWTH lines.
Move it down if GROWTH is positive, or up if GROWTH is negative.
If this would make WINDOW too short, shrink the window or windows
above it to make room."
  ;; Moving the top of WINDOW is actually moving the bottom of the
  ;; window above.
  (let ((window-above (mouse-drag-window-above window)))
    (and window-above
	 (mouse-drag-move-window-bottom window-above (- growth)))))

386 387 388
(defun mouse-drag-mode-line-1 (start-event mode-line-p)
  "Change the height of a window by dragging on the mode or header line.
START-EVENT is the starting mouse-event of the drag action.
389
MODE-LINE-P non-nil means dragging a mode line; nil means a header line."
390 391
  ;; Give temporary modes such as isearch a chance to turn off.
  (run-hooks 'mouse-leave-buffer-hook)
392 393 394 395 396 397 398 399 400
  (let* ((done nil)
	 (echo-keystrokes 0)
	 (start (event-start start-event))
	 (start-event-window (posn-window start))
	 (start-event-frame (window-frame start-event-window))
	 (start-nwindows (count-windows t))
	 (old-selected-window (selected-window))
	 (minibuffer (frame-parameter nil 'minibuffer))
	 should-enlarge-minibuffer event mouse y top bot edges wconfig growth)
401 402 403 404 405 406
    (track-mouse
      (progn
	;; enlarge-window only works on the selected window, so
	;; we must select the window where the start event originated.
	;; unwind-protect will restore the old selected window later.
	(select-window start-event-window)
407

408 409 410 411
	;; if this is the bottommost ordinary window, then to
	;; move its modeline the minibuffer must be enlarged.
	(setq should-enlarge-minibuffer
	      (and minibuffer
412
		   mode-line-p
413 414 415
		   (not (one-window-p t))
		   (= (nth 1 (window-edges minibuffer))
		      (nth 3 (window-edges)))))
416

417 418 419 420 421
	;; loop reading events and sampling the position of
	;; the mouse.
	(while (not done)
	  (setq event (read-event)
		mouse (mouse-position))
422

423 424 425 426 427 428 429 430 431 432 433 434 435
	  ;; do nothing if
	  ;;   - there is a switch-frame event.
	  ;;   - the mouse isn't in the frame that we started in
	  ;;   - the mouse isn't in any Emacs frame
	  ;; drag if
	  ;;   - there is a mouse-movement event
	  ;;   - there is a scroll-bar-movement event
	  ;;     (same as mouse movement for our purposes)
	  ;; quit if
	  ;;   - there is a keyboard event or some other unknown event
	  ;;     unknown event.
	  (cond ((integerp event)
		 (setq done t))
436

437
		((memq (car event) '(switch-frame select-window))
438
		 nil)
439

440 441 442
		((not (memq (car event) '(mouse-movement scroll-bar-movement)))
		 (when (consp event)
		   (push event unread-command-events))
443
		 (setq done t))
444

445 446
		((not (eq (car mouse) start-event-frame))
		 nil)
447

448 449
		((null (car (cdr mouse)))
		 nil)
450

451 452 453 454 455
		(t
		 (setq y (cdr (cdr mouse))
		       edges (window-edges)
		       top (nth 1 edges)
		       bot (nth 3 edges))
456

457
		 ;; compute size change needed
458 459
		 (cond (mode-line-p
			(setq growth (- y bot -1)))
460 461 462 463 464
		       (t	; header line
			(when (< (- bot y) window-min-height)
			  (setq y (- bot window-min-height)))
			;; The window's top includes the header line!
			(setq growth (- top y))))
465
		 (setq wconfig (current-window-configuration))
466

467
		 ;; Check for an error case.
468 469 470 471
		 (when (and (/= growth 0)
			    (not minibuffer)
			    (one-window-p t))
		   (error "Attempt to resize sole window"))
472

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
		 ;; grow/shrink minibuffer?
		 (if should-enlarge-minibuffer
		     (progn
		       ;; yes.  briefly select minibuffer so
		       ;; enlarge-window will affect the
		       ;; correct window.
		       (select-window minibuffer)
		       ;; scale back shrinkage if it would
		       ;; make the minibuffer less than 1
		       ;; line tall.
		       (if (and (> growth 0)
				(< (- (window-height minibuffer)
				      growth)
				   1))
			   (setq growth (1- (window-height minibuffer))))
		       (enlarge-window (- growth))
		       (select-window start-event-window))
		   ;; no.  grow/shrink the selected window
491
		   ;(message "growth = %d" growth)
492 493 494
		   (if mode-line-p
		       (mouse-drag-move-window-bottom start-event-window growth)
		     (mouse-drag-move-window-top start-event-window growth)))
495

496 497 498 499 500 501
		 ;; if this window's growth caused another
		 ;; window to be deleted because it was too
		 ;; short, rescind the change.
		 ;;
		 ;; if size change caused space to be stolen
		 ;; from a window above this one, rescind the
502
		 ;; change, but only if we didn't grow/shrink
503 504 505
		 ;; the minibuffer.  minibuffer size changes
		 ;; can cause all windows to shrink... no way
		 ;; around it.
506 507
		 (when (or (/= start-nwindows (count-windows t))
			   (and (not should-enlarge-minibuffer)
508
				(> growth 0)
509 510 511
				mode-line-p
				(/= top (nth 1 (window-edges)))))
		   (set-window-configuration wconfig)))))))))
512 513 514 515 516 517 518

(defun mouse-drag-mode-line (start-event)
  "Change the height of a window by dragging on the mode line."
  (interactive "e")
  (mouse-drag-mode-line-1 start-event t))

(defun mouse-drag-header-line (start-event)
519 520 521
  "Change the height of a window by dragging on the header line.
Windows whose header-lines are at the top of the frame cannot be
resized by dragging their header-line."
522
  (interactive "e")
523 524 525 526 527 528 529 530 531 532 533 534
  ;; Changing the window's size by dragging its header-line when the
  ;; header-line is at the top of the frame is somewhat strange,
  ;; because the header-line doesn't move, so don't do it.
  (let* ((start (event-start start-event))
	 (window (posn-window start))
	 (frame (window-frame window))
	 (first-window (frame-first-window frame)))
    (when (or (eq window first-window)
	      (= (nth 1 (window-edges window))
		 (nth 1 (window-edges first-window))))
      (error "Cannot move header-line at the top of the frame"))
    (mouse-drag-mode-line-1 start-event nil)))
535

536

537 538 539 540 541
(defun mouse-drag-vertical-line (start-event)
  "Change the width of a window by dragging on the vertical line."
  (interactive "e")
  ;; Give temporary modes such as isearch a chance to turn off.
  (run-hooks 'mouse-leave-buffer-hook)
542 543 544 545 546 547 548 549 550 551
  (let* ((done nil)
	 (echo-keystrokes 0)
	 (start-event-frame (window-frame (car (car (cdr start-event)))))
	 (start-event-window (car (car (cdr start-event))))
	 (start-nwindows (count-windows t))
	 (old-selected-window (selected-window))
	 event mouse x left right edges wconfig growth
	 (which-side
	  (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
	      'right)))
552 553
    (if (one-window-p t)
	(error "Attempt to resize sole ordinary window"))
Karl Heuer's avatar
Karl Heuer committed
554 555 556 557 558 559
    (if (eq which-side 'right)
	(if (= (nth 2 (window-edges start-event-window))
	       (frame-width start-event-frame))
	    (error "Attempt to drag rightmost scrollbar"))
      (if (= (nth 0 (window-edges start-event-window)) 0)
	  (error "Attempt to drag leftmost scrollbar")))
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    (track-mouse
      (progn
	;; enlarge-window only works on the selected window, so
	;; we must select the window where the start event originated.
	;; unwind-protect will restore the old selected window later.
	(select-window start-event-window)
	;; loop reading events and sampling the position of
	;; the mouse.
	(while (not done)
	  (setq event (read-event)
		mouse (mouse-position))
	  ;; do nothing if
	  ;;   - there is a switch-frame event.
	  ;;   - the mouse isn't in the frame that we started in
	  ;;   - the mouse isn't in any Emacs frame
	  ;; drag if
	  ;;   - there is a mouse-movement event
	  ;;   - there is a scroll-bar-movement event
	  ;;     (same as mouse movement for our purposes)
	  ;; quit if
	  ;;   - there is a keyboard event or some other unknown event
	  ;;     unknown event.
	  (cond ((integerp event)
		 (setq done t))
584
		((memq (car event) '(switch-frame select-window))
585 586 587 588 589 590 591 592 593 594 595 596
		 nil)
		((not (memq (car event)
			    '(mouse-movement scroll-bar-movement)))
		 (if (consp event)
		     (setq unread-command-events
			   (cons event unread-command-events)))
		 (setq done t))
		((not (eq (car mouse) start-event-frame))
		 nil)
		((null (car (cdr mouse)))
		 nil)
		(t
597 598 599
		 (save-selected-window
		   ;; If the scroll bar is on the window's left,
		   ;; adjust the window on the left.
Karl Heuer's avatar
Karl Heuer committed
600 601
		   (unless (eq which-side 'right)
		     (select-window (previous-window)))
602
		   (setq x (- (car (cdr mouse))
Karl Heuer's avatar
Karl Heuer committed
603
			      (if (eq which-side 'right) 0 2))
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
			 edges (window-edges)
			 left (nth 0 edges)
			 right (nth 2 edges))
		   ;; scale back a move that would make the
		   ;; window too thin.
		   (if (< (- x left -1) window-min-width)
		       (setq x (+ left window-min-width -1)))
		   ;; compute size change needed
		   (setq growth (- x right -1)
			 wconfig (current-window-configuration))
		   (enlarge-window growth t)
		   ;; if this window's growth caused another
		   ;; window to be deleted because it was too
		   ;; thin, rescind the change.
		   ;;
		   ;; if size change caused space to be stolen
		   ;; from a window to the left of this one,
		   ;; rescind the change.
		   (if (or (/= start-nwindows (count-windows t))
			   (/= left (nth 0 (window-edges))))
		       (set-window-configuration wconfig))))))))))
625

626
(defun mouse-set-point (event)
Jim Blandy's avatar
Jim Blandy committed
627
  "Move point to the position clicked on with the mouse.
628
This should be bound to a mouse click event type."
Jim Blandy's avatar
Jim Blandy committed
629
  (interactive "e")
630
  (mouse-minibuffer-check event)
631 632
  ;; Use event-end in case called from mouse-drag-region.
  ;; If EVENT is a click, event-end and event-start give same value.
Stefan Monnier's avatar
Stefan Monnier committed
633
  (posn-set-point (event-end event)))
Jim Blandy's avatar
Jim Blandy committed
634

635 636 637 638 639 640 641 642 643 644 645
(defvar mouse-last-region-beg nil)
(defvar mouse-last-region-end nil)
(defvar mouse-last-region-tick nil)

(defun mouse-region-match ()
  "Return non-nil if there's an active region that was set with the mouse."
  (and (mark t) mark-active
       (eq mouse-last-region-beg (region-beginning))
       (eq mouse-last-region-end (region-end))
       (eq mouse-last-region-tick (buffer-modified-tick))))

646
(defun mouse-set-region (click)
647
  "Set the region to the text dragged over, and copy to kill ring.
648
This should be bound to a mouse drag event."
649
  (interactive "e")
650
  (mouse-minibuffer-check click)
651 652 653 654 655
  (let ((posn (event-start click))
	(end (event-end click)))
    (select-window (posn-window posn))
    (if (numberp (posn-point posn))
	(goto-char (posn-point posn)))
656
    ;; If mark is highlighted, no need to bounce the cursor.
657 658
    ;; On X, we highlight while dragging, thus once again no need to bounce.
    (or transient-mark-mode
659
	(memq (framep (selected-frame)) '(x pc w32))
660
	(sit-for 1))
661
    (push-mark)
662
    (set-mark (point))
663
    (if (numberp (posn-point end))
664 665 666
	(goto-char (posn-point end)))
    ;; Don't set this-command to kill-region, so that a following
    ;; C-w will not double the text in the kill ring.
667
    ;; Ignore last-command so we don't append to a preceding kill.
668 669 670
    (when mouse-drag-copy-region
      (let (this-command last-command deactivate-mark)
	(copy-region-as-kill (mark) (point))))
671 672 673
    (mouse-set-region-1)))

(defun mouse-set-region-1 ()
674
  ;; Set transient-mark-mode for a little while.
675 676
  (if (memq transient-mark-mode '(nil identity))
      (setq transient-mark-mode 'only))
677 678 679
  (setq mouse-last-region-beg (region-beginning))
  (setq mouse-last-region-end (region-end))
  (setq mouse-last-region-tick (buffer-modified-tick)))
680

Richard M. Stallman's avatar
Richard M. Stallman committed
681
(defcustom mouse-scroll-delay 0.25
682 683 684 685 686 687
  "*The pause between scroll steps caused by mouse drags, in seconds.
If you drag the mouse beyond the edge of a window, Emacs scrolls the
window to bring the text beyond that edge into view, with a delay of
this many seconds between scroll steps.  Scrolling stops when you move
the mouse back into the window, or release the button.
This variable's value may be non-integral.
Richard M. Stallman's avatar
Richard M. Stallman committed
688 689 690
Setting this to zero causes Emacs to scroll as fast as it can."
  :type 'number
  :group 'mouse)
691

Richard M. Stallman's avatar
Richard M. Stallman committed
692
(defcustom mouse-scroll-min-lines 1
693 694 695 696 697
  "*The minimum number of lines scrolled by dragging mouse out of window.
Moving the mouse out the top or bottom edge of the window begins
scrolling repeatedly.  The number of lines scrolled per repetition
is normally equal to the number of lines beyond the window edge that
the mouse has moved.  However, it always scrolls at least the number
Richard M. Stallman's avatar
Richard M. Stallman committed
698 699 700
of lines specified by this variable."
  :type 'integer
  :group 'mouse)
701

702 703
(defun mouse-scroll-subr (window jump &optional overlay start)
  "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
704 705 706
If OVERLAY is an overlay, let it stretch from START to the far edge of
the newly visible text.
Upon exit, point is at the far edge of the newly visible text."
707 708 709 710 711
  (cond
   ((and (> jump 0) (< jump mouse-scroll-min-lines))
    (setq jump mouse-scroll-min-lines))
   ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
    (setq jump (- mouse-scroll-min-lines))))
712 713 714 715 716 717 718
  (let ((opoint (point)))
    (while (progn
	     (goto-char (window-start window))
	     (if (not (zerop (vertical-motion jump window)))
		 (progn
		   (set-window-start window (point))
		   (if (natnump jump)
719 720 721 722 723 724 725
		       (if (window-end window)
			   (progn
			     (goto-char (window-end window))
			     ;; window-end doesn't reflect the window's new
			     ;; start position until the next redisplay.
			     (vertical-motion (1- jump) window))
			 (vertical-motion (- (window-height window) 2)))
726 727 728 729 730 731 732 733
		     (goto-char (window-start window)))
		   (if overlay
		       (move-overlay overlay start (point)))
		   ;; Now that we have scrolled WINDOW properly,
		   ;; put point back where it was for the redisplay
		   ;; so that we don't mess up the selected window.
		   (or (eq window (selected-window))
		       (goto-char opoint))
734
		   (sit-for mouse-scroll-delay)))))
735 736
    (or (eq window (selected-window))
	(goto-char opoint))))
737

738
;; Create an overlay and immediately delete it, to get "overlay in no buffer".
739
(defvar mouse-drag-overlay (make-overlay 1 1))
740
(delete-overlay mouse-drag-overlay)
741 742
(overlay-put mouse-drag-overlay 'face 'region)

743
(defvar mouse-selection-click-count 0)
744

745 746
(defvar mouse-selection-click-count-buffer nil)

747
(defun mouse-drag-region (start-event)
748
  "Set the region to the text that the mouse is dragged over.
749 750
Highlight the drag area as you move the mouse.
This must be bound to a button-down mouse event.
751
In Transient Mark mode, the highlighting remains as long as the mark
752 753 754
remains active.  Otherwise, it remains until the next input event.

If the click is in the echo area, display the `*Messages*' buffer."
755
  (interactive "e")
756 757 758
  (let ((w (posn-window (event-start start-event))))
    (if (and (window-minibuffer-p w)
	     (not (minibuffer-window-active-p w)))
759
	(save-excursion
Richard M. Stallman's avatar
Richard M. Stallman committed
760
	  ;; Swallow the up-event.
761 762 763 764 765 766 767 768
	  (read-event)
	  (set-buffer "*Messages*")
	  (goto-char (point-max))
	  (display-buffer (current-buffer)))
      ;; Give temporary modes such as isearch a chance to turn off.
      (run-hooks 'mouse-leave-buffer-hook)
      (mouse-drag-region-1 start-event))))

769 770 771

(defun mouse-on-link-p (pos)
  "Return non-nil if POS is on a link in the current buffer.
772 773
POS must be a buffer position in the current buffer or an mouse
event location in the selected window, see `event-start'.
774 775 776

A clickable link is identified by one of the following methods:

Kim F. Storm's avatar
Kim F. Storm committed
777 778 779
- If the character at POS has a non-nil `follow-link' text or
overlay property, use the value of that property determines what
to do.
780

Kim F. Storm's avatar
Kim F. Storm committed
781 782 783
- If there is a local key-binding or a keybinding at position POS
for the `follow-link' event, the binding of that event determines
what to do.
784

Kim F. Storm's avatar
Kim F. Storm committed
785 786 787
The resulting value determine whether POS is inside a link:

- If the value is `mouse-face', POS is inside a link if there
788 789
is a non-nil `mouse-face' property at POS.  Return t in this case.

Kim F. Storm's avatar
Kim F. Storm committed
790
- If the value is a function, FUNC, POS is inside a link if
791
the call \(FUNC POS) returns non-nil.  Return the return value
792
from that call.  Arg is \(posn-point POS) if POS is a mouse event,
793

Kim F. Storm's avatar
Kim F. Storm committed
794
- Otherwise, return the value itself.
795 796 797 798 799 800 801 802 803 804 805 806 807

The return value is interpreted as follows:

- If it is a string, the mouse-1 event is translated into the
first character of the string, i.e. the action of the mouse-1
click is the local or global binding of that character.

- If it is a vector, the mouse-1 event is translated into the
first element of that vector, i.e. the action of the mouse-1
click is the local or global binding of that event.

- Otherwise, the mouse-1 event is translated into a mouse-2 event
at the same position."
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  (if (consp pos)
      (setq pos (and (eq (selected-window) (posn-window pos))
		     (posn-point pos))))
  (when pos
    (let ((action
	   (or (get-char-property pos 'follow-link)
	       (save-excursion
		 (goto-char pos)
		 (key-binding [follow-link] nil t)))))
      (cond
       ((eq action 'mouse-face)
	(and (get-char-property pos 'mouse-face) t))
       ((functionp action)
	(funcall action pos))
       (t action)))))

824

825
(defun mouse-drag-region-1 (start-event)
826
  (mouse-minibuffer-check start-event)
827 828
  (let* ((echo-keystrokes 0)
	 (start-posn (event-start start-event))
829 830
	 (start-point (posn-point start-posn))
	 (start-window (posn-window start-posn))
831
	 (start-window-start (window-start start-window))
832
	 (start-frame (window-frame start-window))
833
	 (start-hscroll (window-hscroll start-window))
834
	 (bounds (window-edges start-window))
835
	 (make-cursor-line-fully-visible nil)
836 837 838 839
	 (top (nth 1 bounds))
	 (bottom (if (window-minibuffer-p start-window)
		     (nth 3 bounds)
		   ;; Don't count the mode line.
840
		   (1- (nth 3 bounds))))
841 842 843
	 (on-link (and mouse-1-click-follows-link
		       (eq start-window (selected-window))))
	 remap-double-click
844
	 (click-count (1- (event-click-count start-event))))
845
    (setq mouse-selection-click-count click-count)
846
    (setq mouse-selection-click-count-buffer (current-buffer))
847
    (mouse-set-point start-event)
848 849 850 851 852
    ;; In case the down click is in the middle of some intangible text,
    ;; use the end of that text, and put it in START-POINT.
    (if (< (point) start-point)
	(goto-char start-point))
    (setq start-point (point))
853
    (setq on-link (and on-link
854 855 856 857 858 859
		       (mouse-on-link-p start-point)))
    (setq remap-double-click (and on-link
				  (eq mouse-1-click-follows-link 'double)
				  (= click-count 1)))
    (if remap-double-click  ;; Don't expand mouse overlay in links
	(setq click-count 0))
860 861
    (let ((range (mouse-start-end start-point start-point click-count)))
      (move-overlay mouse-drag-overlay (car range) (nth 1 range)
862 863
		    (window-buffer start-window))
      (overlay-put mouse-drag-overlay 'window (selected-window)))
864
    (deactivate-mark)
865 866 867
    ;; end-of-range is used only in the single-click case.
    ;; It is the place where the drag has reached so far
    ;; (but not outside the window where the drag started).
868
    (let (event end end-point last-end-point (end-of-range (point)))
869
      (track-mouse
870
	(while (progn
871 872
		 (setq event (read-event))
		 (or (mouse-movement-p event)
873 874
		     (memq (car-safe event) '(switch-frame select-window))))
	  (if (memq (car-safe event) '(switch-frame select-window))
875 876 877
	      nil
	    (setq end (event-end event)
		  end-point (posn-point end))
878
	    (if (numberp end-point)
879
		(setq last-end-point end-point))
880 881 882 883 884

	    (cond
	     ;; Are we moving within the original window?
	     ((and (eq (posn-window end) start-window)
		   (integer-or-marker-p end-point))
885 886 887 888
	      ;; Go to START-POINT first, so that when we move to END-POINT,
	      ;; if it's in the middle of intangible text,
	      ;; point jumps in the direction away from START-POINT.
	      (goto-char start-point)
889
	      (goto-char end-point)
890 891
	      (if (zerop (% click-count 3))
		  (setq end-of-range (point)))
892 893
	      (let ((range (mouse-start-end start-point (point) click-count)))
		(move-overlay mouse-drag-overlay (car range) (nth 1 range))))
894

895 896
	     (t
	      (let ((mouse-row (cdr (cdr (mouse-position)))))
897
		(cond
898
		 ((null mouse-row))
899
		 ((< mouse-row top)
900
		  (mouse-scroll-subr start-window (- mouse-row top)
901 902 903 904
				     mouse-drag-overlay start-point)
		  ;; Without this, point tends to jump back to the starting
		  ;; position where the mouse button was pressed down.
		  (setq end-of-range (overlay-start mouse-drag-overlay)))
905
		 ((>= mouse-row bottom)
906
		  (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
907
				     mouse-drag-overlay start-point)
908
		  (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
909

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
      ;; In case we did not get a mouse-motion event
      ;; for the final move of the mouse before a drag event
      ;; pretend that we did get one.
      (when (and (memq 'drag (event-modifiers (car-safe event)))
		 (setq end (event-end event)
		       end-point (posn-point end))
		 (eq (posn-window end) start-window)
		 (integer-or-marker-p end-point))
	;; Go to START-POINT first, so that when we move to END-POINT,
	;; if it's in the middle of intangible text,
	;; point jumps in the direction away from START-POINT.
	(goto-char start-point)
	(goto-char end-point)
	(if (zerop (% click-count 3))
	    (setq end-of-range (point)))
	(let ((range (mouse-start-end start-point (point) click-count)))
	  (move-overlay mouse-drag-overlay (car range) (nth 1 range))))

928
      (if (consp event)
929
	  (let ((fun (key-binding (vector (car event)))))
930 931 932
	    ;; Run the binding of the terminating up-event, if possible.
	    ;; In the case of a multiple click, it gives the wrong results,
	    ;; because it would fail to set up a region.
933 934 935 936 937 938 939 940 941 942 943
	    (if (not (= (overlay-start mouse-drag-overlay)
			(overlay-end mouse-drag-overlay)))
		(let* ((stop-point
			(if (numberp (posn-point (event-end event)))
			    (posn-point (event-end event))
			  last-end-point))
		       ;; The end that comes from where we ended the drag.
		       ;; Point goes here.
		       (region-termination
			(if (and stop-point (< stop-point start-point))
			    (overlay-start mouse-drag-overlay)
944
			  (overlay-end mouse-drag-overlay)))
945 946 947 948 949 950 951 952 953
		       ;; The end that comes from where we started the drag.
		       ;; Mark goes there.
		       (region-commencement
			(- (+ (overlay-end mouse-drag-overlay)
			      (overlay-start mouse-drag-overlay))
			   region-termination))
		       last-command this-command)
		  (push-mark region-commencement t t)
		  (goto-char region-termination)
954
		  ;; Don't let copy-region-as-kill set deactivate-mark.
955 956 957
		  (when mouse-drag-copy-region
		    (let (deactivate-mark)
		      (copy-region-as-kill (point) (mark t))))
958 959 960 961
		  (let ((buffer (current-buffer)))
		    (mouse-show-mark)
		    ;; mouse-show-mark can call read-event,
		    ;; and that means the Emacs server could switch buffers
962
		    ;; under us.  If that happened,
963 964 965 966 967 968
		    ;; avoid trying to use the region.
		    (and (mark t) mark-active
			 (eq buffer (current-buffer))
			 (mouse-set-region-1))))
	      (delete-overlay mouse-drag-overlay)
	      ;; Run the binding of the terminating up-event.
969
	      (when (and (functionp fun)
970 971 972 973 974 975 976 977 978 979 980 981
			 (= start-hscroll (window-hscroll start-window))
			 ;; Don't run the up-event handler if the
			 ;; window start changed in a redisplay after
			 ;; the mouse-set-point for the down-mouse
			 ;; event at the beginning of this function.
			 ;; When the window start has changed, the
			 ;; up-mouse event will contain a different
			 ;; position due to the new window contents,
			 ;; and point is set again.
			 (or end-point
			     (= (window-start start-window)
				start-window-start)))
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
		(if (and on-link
			 (not end-point)
			 (consp event)
			 (or remap-double-click
			     (and
			      (not (eq mouse-1-click-follows-link 'double))
			      (= click-count 0)
			      (= (event-click-count event) 1)
			      (not (input-pending-p))
			      (or (not (integerp mouse-1-click-follows-link))
				  (let ((t0 (posn-timestamp (event-start start-event)))
					(t1 (posn-timestamp (event-end event))))
				    (and (integerp t0) (integerp t1)
					 (if (> mouse-1-click-follows-link 0)
					     (<= (- t1 t0) mouse-1-click-follows-link)
					   (< (- t0 t1) mouse-1-click-follows-link)))))
			      (or (not double-click-time)
				  (sit-for 0 (if (integerp double-click-time)
						 double-click-time 500) t)))))
		    (if (or (vectorp on-link) (stringp on-link))
			(setq event (aref on-link 0))
		      (setcar event 'mouse-2)))
1004 1005
		(setq unread-command-events
		      (cons event unread-command-events)))))
1006
	(delete-overlay mouse-drag-overlay)))))
1007 1008

;; Commands to handle xterm-style multiple clicks.
1009

1010 1011 1012 1013