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

Karl Heuer's avatar
Karl Heuer committed
3
;;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

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

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

Jim Blandy's avatar
Jim Blandy committed
10 11
;;; 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
Jim Blandy's avatar
Jim Blandy committed
12
;;; the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
13
;;; any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
14

Jim Blandy's avatar
Jim Blandy committed
15 16 17 18
;;; 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
19

Jim Blandy's avatar
Jim Blandy committed
20 21 22
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Emacs; see the file COPYING.  If not, write to
;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
23

24 25 26 27 28 29 30 31
;;; 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.

32 33
;;; Code:

Jim Blandy's avatar
Jim Blandy committed
34
;;; Utility functions.
Richard M. Stallman's avatar
Richard M. Stallman committed
35

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

39 40
(defvar mouse-yank-at-point nil
  "*If non-nil, mouse yank commands yank at point instead of at click.")
Jim Blandy's avatar
Jim Blandy committed
41

42 43 44 45 46 47 48 49 50 51 52 53 54
;; Provide a mode-specific menu on a mouse button.

(defun mouse-major-mode-menu (event)
  "Pop up a mode-specific menu of mouse commands."
  ;; Switch to the window clicked on, because otherwise
  ;; the mode's commands may not make sense.
  (interactive "@e")
  (let ((newmap (make-sparse-keymap))
	(unread-command-events (list event)))
    ;; Make a keymap in which our last command leads to a menu
    (define-key newmap (vector (car event))
      (nconc (make-sparse-keymap "Menu")
	     (mouse-major-mode-menu-1
55 56
	      (and (current-local-map)
		   (lookup-key (current-local-map) [menu-bar])))))
57
    (mouse-major-mode-menu-compute-equiv-keys newmap)
58 59 60 61 62 63 64 65 66 67 68 69 70
    ;; Make NEWMAP override the usual definition
    ;; of the mouse button that got us here.
    ;; Then read the user's menu choice.
    (let* ((minor-mode-map-alist
	    (cons (cons t newmap) minor-mode-map-alist))
	   ;; read-key-sequence quits if the user aborts the menu.
	   ;; If that happens, do nothing silently.
	   (keyseq (condition-case nil
		       (read-key-sequence "")
		     (quit nil)))
	   (command (if keyseq (lookup-key newmap keyseq))))
      (if command
	  (command-execute command)))))
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

;; Compute and cache the equivalent keys in MENU and all its submenus.
(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))))

;; 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)
		(setq submap (cdr (car tail)))))
	  (setq tail (cdr tail)))
	(if (eq submap t) menubar
	  submap))))

104 105
;; Commands that operate on windows.

106 107 108 109
(defun mouse-minibuffer-check (event)
  (let ((w (posn-window (event-start event))))
    (and (window-minibuffer-p w)
	 (not (minibuffer-window-active-p w))
110 111 112
	 (error "Minibuffer window is not active")))
  ;; Give temporary modes such as isearch a chance to turn off.
  (run-hooks 'mouse-leave-buffer-hook))
113

Jim Blandy's avatar
Jim Blandy committed
114
(defun mouse-delete-window (click)
115
  "Delete the window you click on.
Jim Blandy's avatar
Jim Blandy committed
116
This must be bound to a mouse click."
Jim Blandy's avatar
Jim Blandy committed
117
  (interactive "e")
118
  (mouse-minibuffer-check click)
119
  (delete-window (posn-window (event-start click))))
Jim Blandy's avatar
Jim Blandy committed
120

121 122 123
(defun mouse-select-window (click)
  "Select the window clicked on; don't move point."
  (interactive "e")
124
  (mouse-minibuffer-check click)
125 126 127 128 129 130 131 132 133
  (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)
	(set-mouse-position (selected-frame) (1- (frame-width)) 0))
    (unfocus-frame)))

134 135 136
(defun mouse-tear-off-window (click)
  "Delete the window clicked on, and create a new frame displaying its buffer."
  (interactive "e")
137
  (mouse-minibuffer-check click)
138 139
  (let* ((window (posn-window (event-start click)))
	 (buf (window-buffer window))
140
	 (frame (make-frame)))
141 142 143 144
    (select-frame frame)
    (switch-to-buffer buf)
    (delete-window window)))

145
(defun mouse-delete-other-windows ()
146
  "Delete all window except the one you click on."
147
  (interactive "@")
Jim Blandy's avatar
Jim Blandy committed
148
  (delete-other-windows))
Richard M. Stallman's avatar
Richard M. Stallman committed
149

Jim Blandy's avatar
Jim Blandy committed
150 151 152 153
(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."
154
  (interactive "@e")
155
  (mouse-minibuffer-check click)
156 157
  (let ((start (event-start click)))
    (select-window (posn-window start))
158
    (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
159 160 161
	  (first-line window-min-height)
	  (last-line (- (window-height) window-min-height)))
      (if (< last-line first-line)
162
	  (error "Window too short to split")
163 164
	(split-window-vertically
	 (min (max new-height first-line) last-line))))))
Jim Blandy's avatar
Jim Blandy committed
165

166 167 168 169 170
(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")
171
  (mouse-minibuffer-check click)
172 173 174 175 176 177
  (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)
178
	  (error "Window too narrow to split")
179 180
	(split-window-horizontally
	 (min (max new-width first-col) last-col))))))
181

182 183 184
(defun mouse-drag-mode-line (start-event)
  "Change the height of a window by dragging on the mode line."
  (interactive "e")
185 186
  ;; Give temporary modes such as isearch a chance to turn off.
  (run-hooks 'mouse-leave-buffer-hook)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  (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))
	should-enlarge-minibuffer
	event mouse minibuffer y top bot edges wconfig params growth)
    (setq params (frame-parameters))
    (if (and (not (setq minibuffer (cdr (assq 'minibuffer params))))
	     (one-window-p t))
	(error "Attempt to resize sole window"))
    (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)
	;; if this is the bottommost ordinary window, then to
	;; move its modeline the minibuffer must be enlarged.
	(setq should-enlarge-minibuffer
	      (and minibuffer
		   (not (one-window-p t))
		   (= (nth 1 (window-edges minibuffer))
		      (nth 3 (window-edges)))))
	;; 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))
		((eq (car event) 'switch-frame)
		 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
		 (setq y (cdr (cdr mouse))
		       edges (window-edges)
		       top (nth 1 edges)
		       bot (nth 3 edges))
		 ;; scale back a move that would make the
		 ;; window too short.
		 (cond ((< (- y top -1) window-min-height)
			(setq y (+ top window-min-height -1))))
		 ;; compute size change needed
		 (setq growth (- y bot -1)
		       wconfig (current-window-configuration))
		 ;; 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
		   (enlarge-window growth))
		 ;; 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
		 ;; change, but only if we didn't grow/srhink
		 ;; the minibuffer.  minibuffer size changes
		 ;; can cause all windows to shrink... no way
		 ;; around it.
		 (if (or (/= start-nwindows (count-windows t))
			 (and (not should-enlarge-minibuffer)
			      (/= top (nth 1 (window-edges)))))
		     (set-window-configuration wconfig)))))))))

288
(defun mouse-set-point (event)
Jim Blandy's avatar
Jim Blandy committed
289
  "Move point to the position clicked on with the mouse.
290
This should be bound to a mouse click event type."
Jim Blandy's avatar
Jim Blandy committed
291
  (interactive "e")
292
  (mouse-minibuffer-check event)
293 294 295
  ;; Use event-end in case called from mouse-drag-region.
  ;; If EVENT is a click, event-end and event-start give same value.
  (let ((posn (event-end event)))
296 297
    (if (not (windowp (posn-window posn)))
	(error "Cursor not in text area of window"))
298 299 300
    (select-window (posn-window posn))
    (if (numberp (posn-point posn))
	(goto-char (posn-point posn)))))
Jim Blandy's avatar
Jim Blandy committed
301

302 303 304 305 306 307 308 309 310 311 312
(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))))

313
(defun mouse-set-region (click)
314
  "Set the region to the text dragged over, and copy to kill ring.
315
This should be bound to a mouse drag event."
316
  (interactive "e")
317
  (mouse-minibuffer-check click)
318 319 320 321 322
  (let ((posn (event-start click))
	(end (event-end click)))
    (select-window (posn-window posn))
    (if (numberp (posn-point posn))
	(goto-char (posn-point posn)))
323
    ;; If mark is highlighted, no need to bounce the cursor.
324 325 326
    ;; On X, we highlight while dragging, thus once again no need to bounce.
    (or transient-mark-mode
	(eq (framep (selected-frame)) 'x)
327
	(sit-for 1))
328
    (push-mark)
329
    (set-mark (point))
330
    (if (numberp (posn-point end))
331 332 333
	(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.
334 335
    ;; Ignore last-command so we don't append to a preceding kill.
    (let (this-command last-command)
336 337 338 339 340 341 342
      (copy-region-as-kill (mark) (point)))
    (mouse-set-region-1)))

(defun mouse-set-region-1 ()
  (setq mouse-last-region-beg (region-beginning))
  (setq mouse-last-region-end (region-end))
  (setq mouse-last-region-tick (buffer-modified-tick)))
343

344 345 346 347 348 349 350 351 352
(defvar mouse-scroll-delay 0.25
  "*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.
Setting this to zero causes Emacs to scroll as fast as it can.")

353 354
(defun mouse-scroll-subr (window jump &optional overlay start)
  "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
355 356 357
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."
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  (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)
		       (progn
			 (goto-char (window-end window))
			 ;; window-end doesn't reflect the window's new
			 ;; start position until the next redisplay.  Hurrah.
			 (vertical-motion (1- jump) window))
		     (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))
378
		   (sit-for mouse-scroll-delay)))))
379 380
    (or (eq window (selected-window))
	(goto-char opoint))))
381

382
;; Create an overlay and immediately delete it, to get "overlay in no buffer".
383
(defvar mouse-drag-overlay (make-overlay 1 1))
384
(delete-overlay mouse-drag-overlay)
385 386
(overlay-put mouse-drag-overlay 'face 'region)

387
(defvar mouse-selection-click-count 0)
388

389 390
(defvar mouse-selection-click-count-buffer nil)

391
(defun mouse-drag-region (start-event)
392
  "Set the region to the text that the mouse is dragged over.
393 394 395 396
Highlight the drag area as you move the mouse.
This must be bound to a button-down mouse event.
In Transient Mark mode, the highlighting remains once you
release the mouse button.  Otherwise, it does not."
397
  (interactive "e")
398
  (mouse-minibuffer-check start-event)
399 400 401
  (let* ((start-posn (event-start start-event))
	 (start-point (posn-point start-posn))
	 (start-window (posn-window start-posn))
402
	 (start-frame (window-frame start-window))
403 404 405 406 407
	 (bounds (window-edges start-window))
	 (top (nth 1 bounds))
	 (bottom (if (window-minibuffer-p start-window)
		     (nth 3 bounds)
		   ;; Don't count the mode line.
408 409
		   (1- (nth 3 bounds))))
	 (click-count (1- (event-click-count start-event))))
410
    (setq mouse-selection-click-count click-count)
411
    (setq mouse-selection-click-count-buffer (current-buffer))
412
    (mouse-set-point start-event)
413 414 415
    (let ((range (mouse-start-end start-point start-point click-count)))
      (move-overlay mouse-drag-overlay (car range) (nth 1 range)
		    (window-buffer start-window)))
416
    (deactivate-mark)
417
    (let (event end end-point)
418
      (track-mouse
419
	(while (progn
420 421 422 423 424 425 426 427 428 429 430 431 432
		 (setq event (read-event))
		 (or (mouse-movement-p event)
		     (eq (car-safe event) 'switch-frame)))
	  (if (eq (car-safe event) 'switch-frame)
	      nil
	    (setq end (event-end event)
		  end-point (posn-point end))

	    (cond
	     ;; Are we moving within the original window?
	     ((and (eq (posn-window end) start-window)
		   (integer-or-marker-p end-point))
	      (goto-char end-point)
433 434
	      (let ((range (mouse-start-end start-point (point) click-count)))
		(move-overlay mouse-drag-overlay (car range) (nth 1 range))))
435

436 437
	     (t
	      (let ((mouse-row (cdr (cdr (mouse-position)))))
438
		(cond
439
		 ((null mouse-row))
440
		 ((< mouse-row top)
441 442
		  (mouse-scroll-subr start-window (- mouse-row top)
				     mouse-drag-overlay start-point))
443
		 ((>= mouse-row bottom)
444
		  (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
445
				     mouse-drag-overlay start-point)))))))))
446
      (if (consp event)
447
	  (let ((fun (key-binding (vector (car event)))))
448 449 450 451
	    ;; 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.
	    (if (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
452 453
		(progn
		  (setq this-command fun)
454 455 456
		  ;; Delete the overlay before calling the function,
		  ;; because delete-overlay increases buffer-modified-tick.
		  (delete-overlay mouse-drag-overlay)
457
		  (funcall fun event))
458 459 460 461 462
	      (if (not (= (overlay-start mouse-drag-overlay)
			  (overlay-end mouse-drag-overlay)))
		  (let (last-command this-command)
		    (push-mark (overlay-start mouse-drag-overlay) t t)
		    (goto-char (overlay-end mouse-drag-overlay))
463 464 465
		    (delete-overlay mouse-drag-overlay)
		    (copy-region-as-kill (point) (mark t))
		    (mouse-set-region-1))
466
		(goto-char (overlay-end mouse-drag-overlay))
467 468 469
		(setq this-command 'mouse-set-point)
		(delete-overlay mouse-drag-overlay))))
	(delete-overlay mouse-drag-overlay)))))
470 471

;; Commands to handle xterm-style multiple clicks.
472

473 474 475 476 477 478 479 480 481 482
(defun mouse-skip-word (dir)
  "Skip over word, over whitespace, or over identical punctuation.
If DIR is positive skip forward; if negative, skip backward."
  (let* ((char (following-char))
	 (syntax (char-to-string (char-syntax char))))
    (if (or (string= syntax "w") (string= syntax " "))
	(if (< dir 0)
	    (skip-syntax-backward syntax)
	  (skip-syntax-forward syntax))
      (if (< dir 0)
483
	  (while (and (not (bobp)) (= (preceding-char) char))
484
	    (forward-char -1))
485
	(while (and (not (eobp)) (= (following-char) char))
486 487 488 489 490 491 492
	  (forward-char 1))))))

;; Return a list of region bounds based on START and END according to MODE.
;; If MODE is 0 then set point to (min START END), mark to (max START END).
;; If MODE is 1 then set point to start of word at (min START END),
;; mark to end of word at (max START END).
;; If MODE is 2 then do the same for lines.
493
(defun mouse-start-end (start end mode)
494 495 496 497
  (if (> start end)
      (let ((temp start))
        (setq start end
              end temp)))
498
  (setq mode (mod mode 3))
499 500 501 502
  (cond ((= mode 0)
	 (list start end))
        ((and (= mode 1)
              (= start end)
503
	      (char-after start)
504
              (= (char-syntax (char-after start)) ?\())
505 506 507 508 509
	 (list start
	       (save-excursion
		 (goto-char start)
		 (forward-sexp 1)
		 (point))))
510 511
        ((and (= mode 1)
              (= start end)
512
	      (char-after start)
513 514 515
              (= (char-syntax (char-after start)) ?\)))
	 (list (save-excursion 
		 (goto-char (1+ start))
516 517
		 (backward-sexp 1)
		 (point))
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	       (1+ start)))
        ((= mode 1)
	 (list (save-excursion
		 (goto-char start)
		 (mouse-skip-word -1)
		 (point))
	       (save-excursion
		 (goto-char end)
		 (mouse-skip-word 1)
		 (point))))
        ((= mode 2)
	 (list (save-excursion
		 (goto-char start)
		 (beginning-of-line 1)
		 (point))
	       (save-excursion
		 (goto-char end)
		 (forward-line 1)
		 (point))))))
537

538 539 540
;; Subroutine: set the mark where CLICK happened,
;; but don't do anything else.
(defun mouse-set-mark-fast (click)
541
  (mouse-minibuffer-check click)
542 543 544 545 546 547 548 549 550 551 552 553
  (let ((posn (event-start click)))
    (select-window (posn-window posn))
    (if (numberp (posn-point posn))
	(push-mark (posn-point posn) t t))))

;; Momentarily show where the mark is, if highlighting doesn't show it. 
(defun mouse-show-mark ()
  (or transient-mark-mode
      (save-excursion
	(goto-char (mark t))
	(sit-for 1))))

Jim Blandy's avatar
Jim Blandy committed
554 555 556 557
(defun mouse-set-mark (click)
  "Set mark at the position clicked on with the mouse.
Display cursor at that position for a second.
This must be bound to a mouse click."
Jim Blandy's avatar
Jim Blandy committed
558
  (interactive "e")
559 560 561
  (mouse-minibuffer-check click)
  (select-window (posn-window (event-start click)))
  ;; We don't use save-excursion because that preserves the mark too.
Richard M. Stallman's avatar
Richard M. Stallman committed
562 563
  (let ((point-save (point)))
    (unwind-protect
Jim Blandy's avatar
Jim Blandy committed
564
	(progn (mouse-set-point click)
565 566 567
	       (push-mark nil t t)
	       (or transient-mark-mode
		   (sit-for 1)))
Richard M. Stallman's avatar
Richard M. Stallman committed
568 569
      (goto-char point-save))))

Jim Blandy's avatar
Jim Blandy committed
570 571 572
(defun mouse-kill (click)
  "Kill the region between point and the mouse click.
The text is saved in the kill ring, as with \\[kill-region]."
Jim Blandy's avatar
Jim Blandy committed
573
  (interactive "e")
574
  (mouse-minibuffer-check click)
575 576 577
  (let* ((posn (event-start click))
	 (click-posn (posn-point posn)))
    (select-window (posn-window posn))
Jim Blandy's avatar
Jim Blandy committed
578 579 580
    (if (numberp click-posn)
	(kill-region (min (point) click-posn)
		     (max (point) click-posn)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
581

Jim Blandy's avatar
Jim Blandy committed
582 583
(defun mouse-yank-at-click (click arg)
  "Insert the last stretch of killed text at the position clicked on.
584 585 586 587
Also move point to one end of the text thus inserted (normally the end).
Prefix arguments are interpreted as with \\[yank].
If `mouse-yank-at-point' is non-nil, insert at point
regardless of where you click."
Jim Blandy's avatar
Jim Blandy committed
588
  (interactive "e\nP")
589 590
  ;; Give temporary modes such as isearch a chance to turn off.
  (run-hooks 'mouse-leave-buffer-hook)
591
  (or mouse-yank-at-point (mouse-set-point click))
592
  (setq this-command 'yank)
593
  (setq mouse-selection-click-count 0)
Jim Blandy's avatar
Jim Blandy committed
594 595 596
  (yank arg))

(defun mouse-kill-ring-save (click)
Jim Blandy's avatar
Jim Blandy committed
597 598
  "Copy the region between point and the mouse click in the kill ring.
This does not delete the region; it acts like \\[kill-ring-save]."
Jim Blandy's avatar
Jim Blandy committed
599
  (interactive "e")
600
  (mouse-set-mark-fast click)
601 602
  (let (this-command last-command)
    (kill-ring-save (point) (mark t)))
603
  (mouse-show-mark))
Richard M. Stallman's avatar
Richard M. Stallman committed
604

Jim Blandy's avatar
Jim Blandy committed
605 606 607 608 609 610 611 612
;;; This function used to delete the text between point and the mouse
;;; whenever it was equal to the front of the kill ring, but some
;;; people found that confusing.

;;; A list (TEXT START END), describing the text and position of the last
;;; invocation of mouse-save-then-kill.
(defvar mouse-save-then-kill-posn nil)

613
(defun mouse-save-then-kill-delete-region (beg end)
614 615 616
  ;; We must make our own undo boundaries
  ;; because they happen automatically only for the current buffer.
  (undo-boundary)
617 618 619 620 621 622 623
  (if (or (= beg end) (eq buffer-undo-list t))
      ;; If we have no undo list in this buffer,
      ;; just delete.
      (delete-region beg end)
    ;; Delete, but make the undo-list entry share with the kill ring.
    ;; First, delete just one char, so in case buffer is being modified
    ;; for the first time, the undo list records that fact.
624 625 626 627
    (let (before-change-function after-change-function
	  before-change-functions after-change-functions)
      (delete-region beg
		     (+ beg (if (> end beg) 1 -1))))
628 629
    (let ((buffer-undo-list buffer-undo-list))
      ;; Undo that deletion--but don't change the undo list!
630 631 632
      (let (before-change-function after-change-function
	    before-change-functions after-change-functions)
	(primitive-undo 1 buffer-undo-list))
633 634 635
      ;; Now delete the rest of the specified region,
      ;; but don't record it.
      (setq buffer-undo-list t)
636 637
      (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
	  (error "Lossage in mouse-save-then-kill-delete-region"))
638 639 640 641 642 643 644 645
      (delete-region beg end))
    (let ((tail buffer-undo-list))
      ;; Search back in buffer-undo-list for the string
      ;; that came from deleting one character.
      (while (and tail (not (stringp (car (car tail)))))
	(setq tail (cdr tail)))
      ;; Replace it with an entry for the entire deleted text.
      (and tail
646 647
	   (setcar tail (cons (car kill-ring) (min beg end))))))
  (undo-boundary))
648

649
(defun mouse-save-then-kill (click)
650 651 652 653
  "Save text to point in kill ring; the second time, kill the text.
If the text between point and the mouse is the same as what's
at the front of the kill ring, this deletes the text.
Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
654 655 656 657 658 659
which prepares for a second click to delete the text.

If you have selected words or lines, this command extends the
selection through the word or line clicked on.  If you do this
again in a different position, it extends the selection again.
If you do this twice in the same position, the selection is killed." 
660
  (interactive "e")
661 662 663 664 665 666
  (let ((before-scroll point-before-scroll))
    (mouse-minibuffer-check click)
    (let ((click-posn (posn-point (event-start click)))
	  ;; Don't let a subsequent kill command append to this one:
	  ;; prevent setting this-command to kill-region.
	  (this-command this-command))
667 668 669 670 671 672
      (if (and (save-excursion
		 (set-buffer (window-buffer (posn-window (event-start click))))
		 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
		      ;; Don't be fooled by a recent click in some other buffer.
		      (eq mouse-selection-click-count-buffer 
			  (current-buffer)))))
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
	  (if (not (and (eq last-command 'mouse-save-then-kill)
			(equal click-posn
			       (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
	      ;; Find both ends of the object selected by this click.
	      (let* ((range
		      (mouse-start-end click-posn click-posn
				       mouse-selection-click-count)))
		;; Move whichever end is closer to the click.
		;; That's what xterm does, and it seems reasonable.
		(if (< (abs (- click-posn (mark t)))
		       (abs (- click-posn (point))))
		    (set-mark (car range))
		  (goto-char (nth 1 range)))
		;; We have already put the old region in the kill ring.
		;; Replace it with the extended region.
		;; (It would be annoying to make a separate entry.)
		(kill-new (buffer-substring (point) (mark t)) t)
690
		(mouse-set-region-1)
691 692 693 694 695 696
		;; Arrange for a repeated mouse-3 to kill this region.
		(setq mouse-save-then-kill-posn
		      (list (car kill-ring) (point) click-posn))
		(mouse-show-mark))
	    ;; If we click this button again without moving it,
	    ;; that time kill.
697
	    (mouse-save-then-kill-delete-region (mark) (point))
698
	    (setq mouse-selection-click-count 0)
699
	    (setq mouse-save-then-kill-posn nil))
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
	(if (and (eq last-command 'mouse-save-then-kill)
		 mouse-save-then-kill-posn
		 (eq (car mouse-save-then-kill-posn) (car kill-ring))
		 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
	    ;; If this is the second time we've called
	    ;; mouse-save-then-kill, delete the text from the buffer.
	    (progn
	      (mouse-save-then-kill-delete-region (point) (mark))
	      ;; After we kill, another click counts as "the first time".
	      (setq mouse-save-then-kill-posn nil))
	  (if (or (and (eq last-command 'mouse-save-then-kill)
		       mouse-save-then-kill-posn)
		  (and mark-active transient-mark-mode)
		  (and (memq last-command
			     '(mouse-drag-region mouse-set-region))
		       (or mark-even-if-inactive
			   (not transient-mark-mode))))
	      ;; We have a selection or suitable region, so adjust it.
	      (let* ((posn (event-start click))
		     (new (posn-point posn)))
		(select-window (posn-window posn))
		(if (numberp new)
		    (progn
		      ;; Move whichever end of the region is closer to the click.
		      ;; That is what xterm does, and it seems reasonable.
		      (if (< (abs (- new (point))) (abs (- new (mark t))))
			  (goto-char new)
			(set-mark new))
		      (setq deactivate-mark nil)))
729 730
		(kill-new (buffer-substring (point) (mark t)) t)
		(mouse-show-mark))
731 732 733 734 735
	    ;; Set the mark where point is, then move where clicked.
	    (mouse-set-mark-fast click)
	    (if before-scroll
		(goto-char before-scroll))
	    (exchange-point-and-mark)
736
	    (kill-new (buffer-substring (point) (mark t))))
737
	  (mouse-set-region-1)
738 739
	  (setq mouse-save-then-kill-posn
		(list (car kill-ring) (point) click-posn)))))))
740 741 742 743 744

(global-set-key [M-mouse-1] 'mouse-start-secondary)
(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
745
(global-set-key [M-mouse-2] 'mouse-yank-secondary)
746 747 748 749 750 751

;; An overlay which records the current secondary selection
;; or else is deleted when there is no secondary selection.
;; May be nil.
(defvar mouse-secondary-overlay nil)

752 753
(defvar mouse-secondary-click-count 0)

754 755 756 757 758 759 760 761 762
;; A marker which records the specified first end for a secondary selection.
;; May be nil.
(defvar mouse-secondary-start nil)

(defun mouse-start-secondary (click)
  "Set one end of the secondary selection to the position clicked on.
Use \\[mouse-secondary-save-then-kill] to set the other end
and complete the secondary selection."
  (interactive "e")
763
  (mouse-minibuffer-check click)
764
  (let ((posn (event-start click)))
765 766 767 768 769 770 771 772 773 774
    (save-excursion
      (set-buffer (window-buffer (posn-window posn)))
      ;; Cancel any preexisting secondary selection.
      (if mouse-secondary-overlay
	  (delete-overlay mouse-secondary-overlay))
      (if (numberp (posn-point posn))
	  (progn
	    (or mouse-secondary-start
		(setq mouse-secondary-start (make-marker)))
	    (move-marker mouse-secondary-start (posn-point posn)))))))
775 776 777 778 779

(defun mouse-set-secondary (click)
  "Set the secondary selection to the text that the mouse is dragged over.
This must be bound to a mouse drag event."
  (interactive "e")
780
  (mouse-minibuffer-check click)
781 782 783
  (let ((posn (event-start click))
	beg
	(end (event-end click)))
784 785 786 787 788 789 790 791
    (save-excursion
      (set-buffer (window-buffer (posn-window posn)))
      (if (numberp (posn-point posn))
	  (setq beg (posn-point posn)))
      (if mouse-secondary-overlay
	  (move-overlay mouse-secondary-overlay beg (posn-point end))
	(setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
      (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
792

793
(defun mouse-drag-secondary (start-event)
794
  "Set the secondary selection to the text that the mouse is dragged over.
795
Highlight the drag area as you move the mouse.
796 797
This must be bound to a button-down mouse event."
  (interactive "e")
798
  (mouse-minibuffer-check start-event)
799 800 801 802 803 804 805 806 807 808 809 810 811
  (let* ((start-posn (event-start start-event))
	 (start-point (posn-point start-posn))
	 (start-window (posn-window start-posn))
	 (start-frame (window-frame start-window))
	 (bounds (window-edges start-window))
	 (top (nth 1 bounds))
	 (bottom (if (window-minibuffer-p start-window)
		     (nth 3 bounds)
		   ;; Don't count the mode line.
		   (1- (nth 3 bounds))))
	 (click-count (1- (event-click-count start-event))))
    (save-excursion
      (set-buffer (window-buffer start-window))
812
      (setq mouse-secondary-click-count click-count)
813 814 815
      (or mouse-secondary-overlay
	  (setq mouse-secondary-overlay
		(make-overlay (point) (point))))
816
      (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
817
      (if (> (mod click-count 3) 0)
818 819
	  ;; Double or triple press: make an initial selection
	  ;; of one word or line.
820 821 822 823 824 825
	  (let ((range (mouse-start-end start-point start-point click-count)))
	    (set-marker mouse-secondary-start nil)
	    (move-overlay mouse-secondary-overlay 1 1
			  (window-buffer start-window))
	    (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
			  (window-buffer start-window)))
826
	;; Single-press: cancel any preexisting secondary selection.
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
	(or mouse-secondary-start
	    (setq mouse-secondary-start (make-marker)))
	(set-marker mouse-secondary-start start-point)
	(delete-overlay mouse-secondary-overlay))
      (let (event end end-point)
	(track-mouse
	  (while (progn
		   (setq event (read-event))
		   (or (mouse-movement-p event)
		       (eq (car-safe event) 'switch-frame)))

	    (if (eq (car-safe event) 'switch-frame)
		nil
	      (setq end (event-end event)
		    end-point (posn-point end))
	      (cond
	       ;; Are we moving within the original window?
	       ((and (eq (posn-window end) start-window)
		     (integer-or-marker-p end-point))
		(let ((range (mouse-start-end start-point end-point
					      click-count)))
848 849 850 851 852 853
		  (if (or (/= start-point end-point)
			  (null (marker-position mouse-secondary-start)))
		      (progn
			(set-marker mouse-secondary-start nil)
			(move-overlay mouse-secondary-overlay
				      (car range) (nth 1 range))))))
854 855 856 857 858
               (t
                (let ((mouse-row (cdr (cdr (mouse-position)))))
                  (cond
                   ((null mouse-row))
                   ((< mouse-row top)
859 860
                    (mouse-scroll-subr start-window (- mouse-row top)
				       mouse-secondary-overlay start-point))
861
                   ((>= mouse-row bottom)
862
                    (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
863
                                       mouse-secondary-overlay start-point)))))))))
864

865 866 867 868
	(if (consp event)
;;;		 (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
;;;		 (eq (posn-window (event-end event)) start-window)
;;;		 (numberp (posn-point (event-end event)))
869 870 871
	    (if (marker-position mouse-secondary-start)
		(save-window-excursion
		  (delete-overlay mouse-secondary-overlay)
872
		  (x-set-selection 'SECONDARY nil)
873 874 875 876
		  (select-window start-window)
		  (save-excursion
		    (goto-char mouse-secondary-start)
		    (sit-for 1)))
877 878 879 880
	      (x-set-selection
	       'SECONDARY
	       (buffer-substring (overlay-start mouse-secondary-overlay)
				 (overlay-end mouse-secondary-overlay)))))))))
881

882
(defun mouse-yank-secondary (click)
883 884 885 886
  "Insert the secondary selection at the position clicked on.
Move point to the end of the inserted text.
If `mouse-yank-at-point' is non-nil, insert at point
regardless of where you click."
887
  (interactive "e")
888 889
  ;; Give temporary modes such as isearch a chance to turn off.
  (run-hooks 'mouse-leave-buffer-hook)
890 891
  (or mouse-yank-at-point (mouse-set-point click))
  (insert (x-get-selection 'SECONDARY)))
892

893
(defun mouse-kill-secondary ()
894 895 896 897 898 899 900
  "Kill the text in the secondary selection.
This is intended more as a keyboard command than as a mouse command
but it can work as either one.

The current buffer (in case of keyboard use), or the buffer clicked on,
must be the one that the secondary selection is in.  This requirement
is to prevent accidents."
901 902 903 904 905 906 907 908
  (interactive)
  (let* ((keys (this-command-keys))
	 (click (elt keys (1- (length keys)))))
    (or (eq (overlay-buffer mouse-secondary-overlay)
	    (if (listp click)
		(window-buffer (posn-window (event-start click)))
	      (current-buffer)))
	(error "Select or click on the buffer where the secondary selection is")))
909 910 911 912 913
  (let (this-command)
    (save-excursion
      (set-buffer (overlay-buffer mouse-secondary-overlay))
      (kill-region (overlay-start mouse-secondary-overlay)
		   (overlay-end mouse-secondary-overlay))))
914
  (delete-overlay mouse-secondary-overlay)
915
;;;  (x-set-selection 'SECONDARY nil)
916 917 918
  (setq mouse-secondary-overlay nil))

(defun mouse-secondary-save-then-kill (click)
919
  "Save text to point in kill ring; the second time, kill the text.
920 921 922 923
You must use this in a buffer where you have recently done \\[mouse-start-secondary].
If the text between where you did \\[mouse-start-secondary] and where
you use this command matches the text at the front of the kill ring,
this command deletes the text.
924
Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
925
which prepares for a second click with this command to delete the text.
926

927 928 929 930
If you have already made a secondary selection in that buffer,
this command extends or retracts the selection to where you click.
If you do this again in a different position, it extends or retracts
again.  If you do this twice in the same position, it kills the selection."
931
  (interactive "e")
932
  (mouse-minibuffer-check click)
933 934
  (let ((posn (event-start click))
	(click-posn (posn-point (event-start click)))
935 936 937
	;; Don't let a subsequent kill command append to this one:
	;; prevent setting this-command to kill-region.
	(this-command this-command))
938 939 940 941 942 943 944 945
    (or (eq (window-buffer (posn-window posn))
	    (or (and mouse-secondary-overlay
		     (overlay-buffer mouse-secondary-overlay))
		(if mouse-secondary-start
		    (marker-buffer mouse-secondary-start))))
	(error "Wrong buffer"))
    (save-excursion
      (set-buffer (window-buffer (posn-window posn)))
946
      (if (> (mod mouse-secondary-click-count 3) 0)
947 948 949 950 951 952
	  (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
			(equal click-posn
			       (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
	      ;; Find both ends of the object selected by this click.
	      (let* ((range
		      (mouse-start-end click-posn click-posn
953
				       mouse-secondary-click-count)))
954 955 956 957 958 959
		;; Move whichever end is closer to the click.
		;; That's what xterm does, and it seems reasonable.
		(if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
		       (abs (- click-posn (overlay-end mouse-secondary-overlay))))
		    (move-overlay mouse-secondary-overlay (car range)
				  (overlay-end mouse-secondary-overlay))
960 961 962
		  (move-overlay mouse-secondary-overlay
				(overlay-start mouse-secondary-overlay)
				(nth 1 range)))
963 964 965
		;; We have already put the old region in the kill ring.
		;; Replace it with the extended region.
		;; (It would be annoying to make a separate entry.)
966 967 968
		(kill-new (buffer-substring
			   (overlay-start mouse-secondary-overlay)
			   (overlay-end mouse-secondary-overlay)) t)
969 970 971 972 973
		;; Arrange for a repeated mouse-3 to kill this region.
		(setq mouse-save-then-kill-posn
		      (list (car kill-ring) (point) click-posn)))
	    ;; If we click this button again without moving it,
	    ;; that time kill.
974
	    (progn
975 976 977 978
	      (mouse-save-then-kill-delete-region
	       (overlay-start mouse-secondary-overlay)
	       (overlay-end mouse-secondary-overlay))
	      (setq mouse-save-then-kill-posn nil)
979
	      (setq mouse-secondary-click-count 0)
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	      (delete-overlay mouse-secondary-overlay)))
	(if (and (eq last-command 'mouse-secondary-save-then-kill)
		 mouse-save-then-kill-posn
		 (eq (car mouse-save-then-kill-posn) (car kill-ring))
		 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
	    ;; If this is the second time we've called
	    ;; mouse-secondary-save-then-kill, delete the text from the buffer.
	    (progn
	      (mouse-save-then-kill-delete-region
	       (overlay-start mouse-secondary-overlay)
	       (overlay-end mouse-secondary-overlay))
	      (setq mouse-save-then-kill-posn nil)
	      (delete-overlay mouse-secondary-overlay))
	  (if (overlay-start mouse-secondary-overlay)
	      ;; We have a selection, so adjust it.
	      (progn
		(if (numberp click-posn)
		    (progn
		      ;; Move whichever end of the region is closer to the click.
		      ;; That is what xterm does, and it seems reasonable.
		      (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
			     (abs (- click-posn (overlay-end mouse-secondary-overlay))))
			  (move-overlay mouse-secondary-overlay click-posn
					(overlay-end mouse-secondary-overlay))
1004 1005 1006
			(move-overlay mouse-secondary-overlay
				      (overlay-start mouse-secondary-overlay)
				      click-posn))
1007
		      (setq deactivate-mark nil)))
1008
		(if (eq last-command 'mouse-secondary-save-then-kill)
1009 1010 1011 1012 1013
		    ;; If the front of the kill ring comes from 
		    ;; an immediately previous use of this command,
		    ;; replace it with the extended region.
		    ;; (It would be annoying to make a separate entry.)
		    (kill-new (buffer-substring
1014
			       (overlay-start mouse-secondary-overlay)
1015
			       (overlay-end mouse-secondary-overlay)) t)
1016 1017
		  (copy-region-as-kill (overlay-start mouse-secondary-overlay)
				       (overlay-end mouse-secondary-overlay))))
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	    (if mouse-secondary-start
		;; All we have is one end of a selection,
		;; so put the other end here.
		(let ((start (+ 0 mouse-secondary-start)))
		  (kill-ring-save start click-posn)
		  (if mouse-secondary-overlay
		      (move-overlay mouse-secondary-overlay start click-posn)
		    (setq mouse-secondary-overlay (make-overlay start click-posn)))
		  (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
	  (setq mouse-save-then-kill-posn
		(list (car kill-ring) (point) click-posn))))
1029 1030
      (if (overlay-buffer mouse-secondary-overlay)
	  (x-set-selection 'SECONDARY
1031 1032 1033
			   (buffer-substring
			    (overlay-start mouse-secondary-overlay)
			    (overlay-end mouse-secondary-overlay)))))))
1034

1035
(defun mouse-buffer-menu (event)
1036 1037 1038
  "Pop up a menu of buffers for selection with the mouse.
This switches buffers in the window that you clicked on,
and selects that window."
Jim Blandy's avatar
Jim Blandy committed
1039
  (interactive "e")
1040
  (mouse-minibuffer-check event)
1041 1042 1043 1044
  (let ((menu
	 (list "Buffer Menu"
	       (cons "Select Buffer"
		     (let ((tail (buffer-list))
1045
			   (maxbuf 0)
1046
			   head)
1047 1048 1049 1050 1051 1052 1053
		       (while tail
			 (or (eq ?\ (aref (buffer-name (car tail)) 0))
			     (setq maxbuf
				   (max maxbuf
					(length (buffer-name (car tail))))))
			 (setq tail (cdr tail)))
		       (setq tail (buffer-list))
1054 1055 1056 1057
		       (while tail
			 (let ((elt (car tail)))
			   (if (not (string-match "^ "
						  (buffer-name elt