home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / mouse.el < prev    next >
Encoding:
Text File  |  1994-05-23  |  49.8 KB  |  1,327 lines

  1. ;;; mouse.el --- window system-independent mouse support.
  2.  
  3. ;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: hardware
  7.  
  8. ;;; This file is part of GNU Emacs.
  9.  
  10. ;;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;;; it under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 2, or (at your option)
  13. ;;; any later version.
  14.  
  15. ;;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;;; GNU General Public License for more details.
  19.  
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This package provides various useful commands (including help
  27. ;; system access) through the mouse.  All this code assumes that mouse
  28. ;; interpretation has been abstracted into Emacs input events.
  29. ;;
  30. ;; The code is rather X-dependent.
  31.  
  32. ;;; Code:
  33.  
  34. ;;; Utility functions.
  35.  
  36. ;;; Indent track-mouse like progn.
  37. (put 'track-mouse 'lisp-indent-function 0)
  38.  
  39. (defvar mouse-yank-at-point nil
  40.   "*If non-nil, mouse yank commands yank at point instead of at click.")
  41.  
  42. (defun mouse-minibuffer-check (event)
  43.   (let ((w (posn-window (event-start event))))
  44.     (and (window-minibuffer-p w)
  45.      (not (minibuffer-window-active-p w))
  46.      (error "Minibuffer window is not active"))))
  47.  
  48. (defun mouse-delete-window (click)
  49.   "Delete the window you click on.
  50. This must be bound to a mouse click."
  51.   (interactive "e")
  52.   (mouse-minibuffer-check click)
  53.   (delete-window (posn-window (event-start click))))
  54.  
  55. (defun mouse-select-window (click)
  56.   "Select the window clicked on; don't move point."
  57.   (interactive "e")
  58.   (mouse-minibuffer-check click)
  59.   (let ((oframe (selected-frame))
  60.     (frame (window-frame (posn-window (event-start click)))))
  61.     (select-window (posn-window (event-start click)))
  62.     (raise-frame frame)
  63.     (select-frame frame)
  64.     (or (eq frame oframe)
  65.     (set-mouse-position (selected-frame) (1- (frame-width)) 0))
  66.     (unfocus-frame)))
  67.  
  68. (defun mouse-tear-off-window (click)
  69.   "Delete the window clicked on, and create a new frame displaying its buffer."
  70.   (interactive "e")
  71.   (mouse-minibuffer-check click)
  72.   (let* ((window (posn-window (event-start click)))
  73.      (buf (window-buffer window))
  74.      (frame (make-frame)))
  75.     (select-frame frame)
  76.     (switch-to-buffer buf)
  77.     (delete-window window)))
  78.  
  79. (defun mouse-delete-other-windows ()
  80.   "Delete all window except the one you click on."
  81.   (interactive "@")
  82.   (delete-other-windows))
  83.  
  84. (defun mouse-split-window-vertically (click)
  85.   "Select Emacs window mouse is on, then split it vertically in half.
  86. The window is split at the line clicked on.
  87. This command must be bound to a mouse click."
  88.   (interactive "@e")
  89.   (mouse-minibuffer-check click)
  90.   (let ((start (event-start click)))
  91.     (select-window (posn-window start))
  92.     (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
  93.       (first-line window-min-height)
  94.       (last-line (- (window-height) window-min-height)))
  95.       (if (< last-line first-line)
  96.       (error "window too short to split")
  97.     (split-window-vertically
  98.      (min (max new-height first-line) last-line))))))
  99.  
  100. (defun mouse-split-window-horizontally (click)
  101.   "Select Emacs window mouse is on, then split it horizontally in half.
  102. The window is split at the column clicked on.
  103. This command must be bound to a mouse click."
  104.   (interactive "@e")
  105.   (mouse-minibuffer-check click)
  106.   (let ((start (event-start click)))
  107.     (select-window (posn-window start))
  108.     (let ((new-width (1+ (car (posn-col-row (event-end click)))))
  109.       (first-col window-min-width)
  110.       (last-col (- (window-width) window-min-width)))
  111.       (if (< last-col first-col)
  112.       (error "window too narrow to split")
  113.     (split-window-horizontally
  114.      (min (max new-width first-col) last-col))))))
  115.  
  116. (defun mouse-set-point (event)
  117.   "Move point to the position clicked on with the mouse.
  118. This should be bound to a mouse click event type."
  119.   (interactive "e")
  120.   (mouse-minibuffer-check event)
  121.   ;; Use event-end in case called from mouse-drag-region.
  122.   ;; If EVENT is a click, event-end and event-start give same value.
  123.   (let ((posn (event-end event)))
  124.     (select-window (posn-window posn))
  125.     (if (numberp (posn-point posn))
  126.     (goto-char (posn-point posn)))))
  127.  
  128. (defun mouse-set-region (click)
  129.   "Set the region to the text dragged over, and copy to kill ring.
  130. This should be bound to a mouse drag event."
  131.   (interactive "e")
  132.   (mouse-minibuffer-check click)
  133.   (let ((posn (event-start click))
  134.     (end (event-end click)))
  135.     (select-window (posn-window posn))
  136.     (if (numberp (posn-point posn))
  137.     (goto-char (posn-point posn)))
  138.     ;; If mark is highlighted, no need to bounce the cursor.
  139.     (or (and transient-mark-mode
  140.          (eq (framep (selected-frame)) 'x))
  141.     (sit-for 1))
  142.     (push-mark)
  143.     (set-mark (point))
  144.     (if (numberp (posn-point end))
  145.     (goto-char (posn-point end)))
  146.     ;; Don't set this-command to kill-region, so that a following
  147.     ;; C-w will not double the text in the kill ring.
  148.     (let (this-command)
  149.       (copy-region-as-kill (mark) (point)))))
  150.  
  151. (defvar mouse-scroll-delay 0.25
  152.   "*The pause between scroll steps caused by mouse drags, in seconds.
  153. If you drag the mouse beyond the edge of a window, Emacs scrolls the
  154. window to bring the text beyond that edge into view, with a delay of
  155. this many seconds between scroll steps.  Scrolling stops when you move
  156. the mouse back into the window, or release the button.
  157. This variable's value may be non-integral.
  158. Setting this to zero causes Emacs to scroll as fast as it can.")
  159.  
  160. (defun mouse-scroll-subr (jump &optional overlay start)
  161.   "Scroll the selected window JUMP lines at a time, until new input arrives.
  162. If OVERLAY is an overlay, let it stretch from START to the far edge of
  163. the newly visible text.
  164. Upon exit, point is at the far edge of the newly visible text."
  165.   (while (progn
  166.        (goto-char (window-start))
  167.        (if (not (zerop (vertical-motion jump)))
  168.            (progn
  169.          (set-window-start (selected-window) (point))
  170.          (if (natnump jump)
  171.              (progn
  172.                (goto-char (window-end (selected-window)))
  173.                ;; window-end doesn't reflect the window's new
  174.                ;; start position until the next redisplay.  Hurrah.
  175.                (vertical-motion (1- jump)))
  176.            (goto-char (window-start (selected-window))))
  177.          (if overlay
  178.              (move-overlay overlay start (point)))
  179.          (if (not (eobp))
  180.              (sit-for mouse-scroll-delay))))))
  181.   (point))
  182.  
  183. (defvar mouse-drag-overlay (make-overlay 1 1))
  184. (overlay-put mouse-drag-overlay 'face 'region)
  185.  
  186. (defvar mouse-selection-click-count 0)
  187.  
  188. (defun mouse-drag-region (start-event)
  189.   "Set the region to the text that the mouse is dragged over.
  190. Highlight the drag area as you move the mouse.
  191. This must be bound to a button-down mouse event.
  192. In Transient Mark mode, the highlighting remains once you
  193. release the mouse button.  Otherwise, it does not."
  194.   (interactive "e")
  195.   (mouse-minibuffer-check start-event)
  196.   (let* ((start-posn (event-start start-event))
  197.      (start-point (posn-point start-posn))
  198.      (start-window (posn-window start-posn))
  199.      (start-frame (window-frame start-window))
  200.      (bounds (window-edges start-window))
  201.      (top (nth 1 bounds))
  202.      (bottom (if (window-minibuffer-p start-window)
  203.              (nth 3 bounds)
  204.            ;; Don't count the mode line.
  205.            (1- (nth 3 bounds))))
  206.      (click-count (1- (event-click-count start-event))))
  207.     (setq mouse-selection-click-count click-count)
  208.     (mouse-set-point start-event)
  209.     (let ((range (mouse-start-end start-point start-point click-count)))
  210.       (move-overlay mouse-drag-overlay (car range) (nth 1 range)
  211.             (window-buffer start-window)))
  212.     (deactivate-mark)
  213.     (let (event end end-point)
  214.       (track-mouse
  215.     (while (progn
  216.          (setq event (read-event))
  217.          (or (mouse-movement-p event)
  218.              (eq (car-safe event) 'switch-frame)))
  219.       (if (eq (car-safe event) 'switch-frame)
  220.           nil
  221.         (setq end (event-end event)
  222.           end-point (posn-point end))
  223.  
  224.         (cond
  225.  
  226.          ;; Ignore switch-frame events.
  227.          ((eq (car-safe event) 'switch-frame))
  228.  
  229.          ;; Are we moving within the original window?
  230.          ((and (eq (posn-window end) start-window)
  231.            (integer-or-marker-p end-point))
  232.           (goto-char end-point)
  233.           (let ((range (mouse-start-end start-point (point) click-count)))
  234.         (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
  235.  
  236.          (t
  237.           (let ((mouse-row (cdr (cdr (mouse-position)))))
  238.         (cond
  239.          ((null mouse-row))
  240.          ((< mouse-row top)
  241.           (mouse-scroll-subr
  242.            (- mouse-row top) mouse-drag-overlay start-point))
  243.          ((and (not (eobp))
  244.                (>= mouse-row bottom))
  245.           (mouse-scroll-subr (1+ (- mouse-row bottom))
  246.                      mouse-drag-overlay start-point)))))))))
  247.  
  248.       (if (and (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
  249.            (eq (posn-window (event-end event)) start-window)
  250.            (numberp (posn-point (event-end event))))
  251.       (let ((fun (key-binding (vector (car event)))))
  252.         (if (memq fun '(mouse-set-region mouse-set-point))
  253.         (if (not (= (overlay-start mouse-drag-overlay)
  254.                 (overlay-end mouse-drag-overlay)))
  255.             (let (this-command)
  256.               (push-mark (overlay-start mouse-drag-overlay) t t)
  257.               (goto-char (overlay-end mouse-drag-overlay))
  258.               (copy-region-as-kill (point) (mark t)))
  259.           (goto-char (overlay-end mouse-drag-overlay))
  260.           (setq this-command 'mouse-set-point))
  261.           (if (fboundp fun)
  262.           (funcall fun event)))))
  263.       (delete-overlay mouse-drag-overlay))))
  264.  
  265. ;; Commands to handle xterm-style multiple clicks.
  266.  
  267. (defun mouse-skip-word (dir)
  268.   "Skip over word, over whitespace, or over identical punctuation.
  269. If DIR is positive skip forward; if negative, skip backward."
  270.   (let* ((char (following-char))
  271.      (syntax (char-to-string (char-syntax char))))
  272.     (if (or (string= syntax "w") (string= syntax " "))
  273.     (if (< dir 0)
  274.         (skip-syntax-backward syntax)
  275.       (skip-syntax-forward syntax))
  276.       (if (< dir 0)
  277.       (while (and (not (bobp)) (= (preceding-char) char))
  278.         (forward-char -1))
  279.     (while (and (not (eobp)) (= (following-char) char))
  280.       (forward-char 1))))))
  281.  
  282. ;; Return a list of region bounds based on START and END according to MODE.
  283. ;; If MODE is 0 then set point to (min START END), mark to (max START END).
  284. ;; If MODE is 1 then set point to start of word at (min START END),
  285. ;; mark to end of word at (max START END).
  286. ;; If MODE is 2 then do the same for lines.
  287. (defun mouse-start-end (start end mode)
  288.   (if (> start end)
  289.       (let ((temp start))
  290.         (setq start end
  291.               end temp)))
  292.   (setq mode (mod mode 3))
  293.   (cond ((= mode 0)
  294.      (list start end))
  295.         ((and (= mode 1)
  296.               (= start end)
  297.           (char-after start)
  298.               (= (char-syntax (char-after start)) ?\())
  299.      (list start
  300.            (save-excursion
  301.          (goto-char start)
  302.          (forward-sexp 1)
  303.          (point))))
  304.         ((and (= mode 1)
  305.               (= start end)
  306.           (char-after start)
  307.               (= (char-syntax (char-after start)) ?\)))
  308.      (list (save-excursion 
  309.          (goto-char (1+ start))
  310.          (backward-sexp 1)
  311.          (point))
  312.            (1+ start)))
  313.         ((= mode 1)
  314.      (list (save-excursion
  315.          (goto-char start)
  316.          (mouse-skip-word -1)
  317.          (point))
  318.            (save-excursion
  319.          (goto-char end)
  320.          (mouse-skip-word 1)
  321.          (point))))
  322.         ((= mode 2)
  323.      (list (save-excursion
  324.          (goto-char start)
  325.          (beginning-of-line 1)
  326.          (point))
  327.            (save-excursion
  328.          (goto-char end)
  329.          (forward-line 1)
  330.          (point))))))
  331.  
  332. ;; Subroutine: set the mark where CLICK happened,
  333. ;; but don't do anything else.
  334. (defun mouse-set-mark-fast (click)
  335.   (mouse-minibuffer-check click)
  336.   (let ((posn (event-start click)))
  337.     (select-window (posn-window posn))
  338.     (if (numberp (posn-point posn))
  339.     (push-mark (posn-point posn) t t))))
  340.  
  341. ;; Momentarily show where the mark is, if highlighting doesn't show it. 
  342. (defun mouse-show-mark ()
  343.   (or transient-mark-mode
  344.       (save-excursion
  345.     (goto-char (mark t))
  346.     (sit-for 1))))
  347.  
  348. (defun mouse-set-mark (click)
  349.   "Set mark at the position clicked on with the mouse.
  350. Display cursor at that position for a second.
  351. This must be bound to a mouse click."
  352.   (interactive "e")
  353.   (let ((point-save (point)))
  354.     (unwind-protect
  355.     (progn (mouse-set-point click)
  356.            (push-mark nil t t)
  357.            (or transient-mark-mode
  358.            (sit-for 1)))
  359.       (goto-char point-save))))
  360.  
  361. (defun mouse-kill (click)
  362.   "Kill the region between point and the mouse click.
  363. The text is saved in the kill ring, as with \\[kill-region]."
  364.   (interactive "e")
  365.   (mouse-minibuffer-check click)
  366.   (let* ((posn (event-start click))
  367.      (click-posn (posn-point posn)))
  368.     (select-window (posn-window posn))
  369.     (if (numberp click-posn)
  370.     (kill-region (min (point) click-posn)
  371.              (max (point) click-posn)))))
  372.  
  373. (defun mouse-yank-at-click (click arg)
  374.   "Insert the last stretch of killed text at the position clicked on.
  375. Also move point to one end of the text thus inserted (normally the end).
  376. Prefix arguments are interpreted as with \\[yank].
  377. If `mouse-yank-at-point' is non-nil, insert at point
  378. regardless of where you click."
  379.   (interactive "e\nP")
  380.   (or mouse-yank-at-point (mouse-set-point click))
  381.   (setq this-command 'yank)
  382.   (yank arg))
  383.  
  384. (defun mouse-kill-ring-save (click)
  385.   "Copy the region between point and the mouse click in the kill ring.
  386. This does not delete the region; it acts like \\[kill-ring-save]."
  387.   (interactive "e")
  388.   (mouse-set-mark-fast click)
  389.   (kill-ring-save (point) (mark t))
  390.   (mouse-show-mark))
  391.  
  392. ;;; This function used to delete the text between point and the mouse
  393. ;;; whenever it was equal to the front of the kill ring, but some
  394. ;;; people found that confusing.
  395.  
  396. ;;; A list (TEXT START END), describing the text and position of the last
  397. ;;; invocation of mouse-save-then-kill.
  398. (defvar mouse-save-then-kill-posn nil)
  399.  
  400. (defun mouse-save-then-kill-delete-region (beg end)
  401.   ;; We must make our own undo boundaries
  402.   ;; because they happen automatically only for the current buffer.
  403.   (undo-boundary)
  404.   (if (or (= beg end) (eq buffer-undo-list t))
  405.       ;; If we have no undo list in this buffer,
  406.       ;; just delete.
  407.       (delete-region beg end)
  408.     ;; Delete, but make the undo-list entry share with the kill ring.
  409.     ;; First, delete just one char, so in case buffer is being modified
  410.     ;; for the first time, the undo list records that fact.
  411.     (delete-region beg
  412.            (+ beg (if (> end beg) 1 -1)))
  413.     (let ((buffer-undo-list buffer-undo-list))
  414.       ;; Undo that deletion--but don't change the undo list!
  415.       (primitive-undo 1 buffer-undo-list)
  416.       ;; Now delete the rest of the specified region,
  417.       ;; but don't record it.
  418.       (setq buffer-undo-list t)
  419.       (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
  420.       (error "Lossage in mouse-save-then-kill-delete-region"))
  421.       (delete-region beg end))
  422.     (let ((tail buffer-undo-list))
  423.       ;; Search back in buffer-undo-list for the string
  424.       ;; that came from deleting one character.
  425.       (while (and tail (not (stringp (car (car tail)))))
  426.     (setq tail (cdr tail)))
  427.       ;; Replace it with an entry for the entire deleted text.
  428.       (and tail
  429.        (setcar tail (cons (car kill-ring) (min beg end))))))
  430.   (undo-boundary))
  431.  
  432. (defun mouse-save-then-kill (click)
  433.   "Save text to point in kill ring; the second time, kill the text.
  434. If the text between point and the mouse is the same as what's
  435. at the front of the kill ring, this deletes the text.
  436. Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
  437. which prepares for a second click to delete the text.
  438.  
  439. If you have selected words or lines, this command extends the
  440. selection through the word or line clicked on.  If you do this
  441. again in a different position, it extends the selection again.
  442. If you do this twice in the same position, the selection is killed." 
  443.   (interactive "e")
  444.   (mouse-minibuffer-check click)
  445.   (let ((click-posn (posn-point (event-start click)))
  446.     ;; Don't let a subsequent kill command append to this one:
  447.     ;; prevent setting this-command to kill-region.
  448.     (this-command this-command))
  449.     (if (> (mod mouse-selection-click-count 3) 0)
  450.     (if (not (and (eq last-command 'mouse-save-then-kill)
  451.               (equal click-posn
  452.                  (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
  453.         ;; Find both ends of the object selected by this click.
  454.         (let* ((range
  455.             (mouse-start-end click-posn click-posn
  456.                      mouse-selection-click-count)))
  457.           ;; Move whichever end is closer to the click.
  458.           ;; That's what xterm does, and it seems reasonable.
  459.           (if (< (abs (- click-posn (mark t)))
  460.              (abs (- click-posn (point))))
  461.           (set-mark (car range))
  462.         (goto-char (nth 1 range)))
  463.           ;; We have already put the old region in the kill ring.
  464.           ;; Replace it with the extended region.
  465.           ;; (It would be annoying to make a separate entry.)
  466.           (setcar kill-ring (buffer-substring (point) (mark t)))
  467.           (if interprogram-cut-function
  468.           (funcall interprogram-cut-function (car kill-ring)))
  469.           ;; Arrange for a repeated mouse-3 to kill this region.
  470.           (setq mouse-save-then-kill-posn
  471.             (list (car kill-ring) (point) click-posn))
  472.           (mouse-show-mark))
  473.       ;; If we click this button again without moving it,
  474.       ;; that time kill.
  475.       (mouse-save-then-kill-delete-region (point) (mark))
  476.       (setq mouse-selection-click-count 0)
  477.       (setq mouse-save-then-kill-posn nil))
  478.       (if (and (eq last-command 'mouse-save-then-kill)
  479.            mouse-save-then-kill-posn
  480.            (eq (car mouse-save-then-kill-posn) (car kill-ring))
  481.            (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
  482.       ;; If this is the second time we've called
  483.       ;; mouse-save-then-kill, delete the text from the buffer.
  484.       (progn
  485.         (mouse-save-then-kill-delete-region (point) (mark))
  486.         ;; After we kill, another click counts as "the first time".
  487.         (setq mouse-save-then-kill-posn nil))
  488.     (if (or (and (eq last-command 'mouse-save-then-kill)
  489.              mouse-save-then-kill-posn)
  490.         (and mark-active transient-mark-mode)
  491.         (and (eq last-command 'mouse-drag-region)
  492.              (or mark-even-if-inactive
  493.              (not transient-mark-mode))))
  494.         ;; We have a selection or suitable region, so adjust it.
  495.         (let* ((posn (event-start click))
  496.            (new (posn-point posn)))
  497.           (select-window (posn-window posn))
  498.           (if (numberp new)
  499.           (progn
  500.             ;; Move whichever end of the region is closer to the click.
  501.             ;; That is what xterm does, and it seems reasonable.
  502.             (if (< (abs (- new (point))) (abs (- new (mark t))))
  503.             (goto-char new)
  504.               (set-mark new))
  505.             (setq deactivate-mark nil)))
  506.           (setcar kill-ring (buffer-substring (point) (mark t)))
  507.           (if interprogram-cut-function
  508.           (funcall interprogram-cut-function (car kill-ring))))
  509.       ;; We just have point, so set mark here.
  510.       (mouse-set-mark-fast click)
  511.       (kill-ring-save (point) (mark t))
  512.       (mouse-show-mark))
  513.     (setq mouse-save-then-kill-posn
  514.           (list (car kill-ring) (point) click-posn))))))
  515.  
  516. (global-set-key [M-mouse-1] 'mouse-start-secondary)
  517. (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
  518. (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
  519. (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
  520. (global-set-key [M-mouse-2] 'mouse-yank-secondary)
  521.  
  522. ;; An overlay which records the current secondary selection
  523. ;; or else is deleted when there is no secondary selection.
  524. ;; May be nil.
  525. (defvar mouse-secondary-overlay nil)
  526.  
  527. ;; A marker which records the specified first end for a secondary selection.
  528. ;; May be nil.
  529. (defvar mouse-secondary-start nil)
  530.  
  531. (defun mouse-start-secondary (click)
  532.   "Set one end of the secondary selection to the position clicked on.
  533. Use \\[mouse-secondary-save-then-kill] to set the other end
  534. and complete the secondary selection."
  535.   (interactive "e")
  536.   (mouse-minibuffer-check click)
  537.   (let ((posn (event-start click)))
  538.     (save-excursion
  539.       (set-buffer (window-buffer (posn-window posn)))
  540.       ;; Cancel any preexisting secondary selection.
  541.       (if mouse-secondary-overlay
  542.       (delete-overlay mouse-secondary-overlay))
  543.       (if (numberp (posn-point posn))
  544.       (progn
  545.         (or mouse-secondary-start
  546.         (setq mouse-secondary-start (make-marker)))
  547.         (move-marker mouse-secondary-start (posn-point posn)))))))
  548.  
  549. (defun mouse-set-secondary (click)
  550.   "Set the secondary selection to the text that the mouse is dragged over.
  551. This must be bound to a mouse drag event."
  552.   (interactive "e")
  553.   (mouse-minibuffer-check click)
  554.   (let ((posn (event-start click))
  555.     beg
  556.     (end (event-end click)))
  557.     (save-excursion
  558.       (set-buffer (window-buffer (posn-window posn)))
  559.       (if (numberp (posn-point posn))
  560.       (setq beg (posn-point posn)))
  561.       (if mouse-secondary-overlay
  562.       (move-overlay mouse-secondary-overlay beg (posn-point end))
  563.     (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
  564.       (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
  565.  
  566. (defun mouse-drag-secondary (start-event)
  567.   "Set the secondary selection to the text that the mouse is dragged over.
  568. Highlight the drag area as you move the mouse.
  569. This must be bound to a button-down mouse event."
  570.   (interactive "e")
  571.   (mouse-minibuffer-check start-event)
  572.   (let* ((start-posn (event-start start-event))
  573.      (start-point (posn-point start-posn))
  574.      (start-window (posn-window start-posn))
  575.      (start-frame (window-frame start-window))
  576.      (bounds (window-edges start-window))
  577.      (top (nth 1 bounds))
  578.      (bottom (if (window-minibuffer-p start-window)
  579.              (nth 3 bounds)
  580.            ;; Don't count the mode line.
  581.            (1- (nth 3 bounds))))
  582.      (click-count (1- (event-click-count start-event))))
  583.     (save-excursion
  584.       (set-buffer (window-buffer start-window))
  585.       (setq mouse-selection-click-count click-count)
  586.       (or mouse-secondary-overlay
  587.       (setq mouse-secondary-overlay
  588.         (make-overlay (point) (point))))
  589.       (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
  590.       (if (> (mod click-count 3) 0)
  591.       ;; Double or triple press: make an initial selection
  592.       ;; of one word or line.
  593.       (let ((range (mouse-start-end start-point start-point click-count)))
  594.         (set-marker mouse-secondary-start nil)
  595.         (move-overlay mouse-secondary-overlay 1 1
  596.               (window-buffer start-window))
  597.         (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
  598.               (window-buffer start-window)))
  599.     ;; Single-press: cancel any preexisting secondary selection.
  600.     (or mouse-secondary-start
  601.         (setq mouse-secondary-start (make-marker)))
  602.     (set-marker mouse-secondary-start start-point)
  603.     (delete-overlay mouse-secondary-overlay))
  604.       (let (event end end-point)
  605.     (track-mouse
  606.       (while (progn
  607.            (setq event (read-event))
  608.            (or (mouse-movement-p event)
  609.                (eq (car-safe event) 'switch-frame)))
  610.  
  611.         (if (eq (car-safe event) 'switch-frame)
  612.         nil
  613.           (setq end (event-end event)
  614.             end-point (posn-point end))
  615.           (cond
  616.  
  617.            ;; Ignore switch-frame events.
  618.            ((eq (car-safe event) 'switch-frame))
  619.  
  620.            ;; Are we moving within the original window?
  621.            ((and (eq (posn-window end) start-window)
  622.              (integer-or-marker-p end-point))
  623.         (if (/= start-point end-point)
  624.             (set-marker mouse-secondary-start nil))
  625.         (let ((range (mouse-start-end start-point end-point
  626.                           click-count)))
  627.           (move-overlay mouse-secondary-overlay
  628.                 (car range) (nth 1 range))))
  629.                (t
  630.                 (let ((mouse-row (cdr (cdr (mouse-position)))))
  631.                   (cond
  632.                    ((null mouse-row))
  633.                    ((< mouse-row top)
  634.                     (mouse-scroll-subr
  635.                      (- mouse-row top) mouse-secondary-overlay start-point))
  636.                    ((and (not (eobp))
  637.                          (>= mouse-row bottom))
  638.                     (mouse-scroll-subr (1+ (- mouse-row bottom))
  639.                                        mouse-secondary-overlay start-point)))))))))
  640.  
  641.     (if (and (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
  642.          (eq (posn-window (event-end event)) start-window)
  643.          (numberp (posn-point (event-end event))))
  644.         (if (marker-position mouse-secondary-start)
  645.         (save-window-excursion
  646.           (delete-overlay mouse-secondary-overlay)
  647.           (x-set-selection 'SECONDARY nil)
  648.           (select-window start-window)
  649.           (save-excursion
  650.             (goto-char mouse-secondary-start)
  651.             (sit-for 1)))
  652.           (x-set-selection
  653.            'SECONDARY
  654.            (buffer-substring (overlay-start mouse-secondary-overlay)
  655.                  (overlay-end mouse-secondary-overlay)))))))))
  656.  
  657. (defun mouse-yank-secondary (click)
  658.   "Insert the secondary selection at the position clicked on.
  659. Move point to the end of the inserted text.
  660. If `mouse-yank-at-point' is non-nil, insert at point
  661. regardless of where you click."
  662.   (interactive "e")
  663.   (or mouse-yank-at-point (mouse-set-point click))
  664.   (insert (x-get-selection 'SECONDARY)))
  665.  
  666. (defun mouse-kill-secondary ()
  667.   "Kill the text in the secondary selection.
  668. This is intended more as a keyboard command than as a mouse command
  669. but it can work as either one.
  670.  
  671. The current buffer (in case of keyboard use), or the buffer clicked on,
  672. must be the one that the secondary selection is in.  This requirement
  673. is to prevent accidents."
  674.   (interactive)
  675.   (let* ((keys (this-command-keys))
  676.      (click (elt keys (1- (length keys)))))
  677.     (or (eq (overlay-buffer mouse-secondary-overlay)
  678.         (if (listp click)
  679.         (window-buffer (posn-window (event-start click)))
  680.           (current-buffer)))
  681.     (error "Select or click on the buffer where the secondary selection is")))
  682.   (save-excursion
  683.     (set-buffer (overlay-buffer mouse-secondary-overlay))
  684.     (kill-region (overlay-start mouse-secondary-overlay)
  685.          (overlay-end mouse-secondary-overlay)))
  686.   (delete-overlay mouse-secondary-overlay)
  687.   (x-set-selection 'SECONDARY nil)
  688.   (setq mouse-secondary-overlay nil))
  689.  
  690. (defun mouse-secondary-save-then-kill (click)
  691.   "Save text to point in kill ring; the second time, kill the text.
  692. You must use this in a buffer where you have recently done \\[mouse-start-secondary].
  693. If the text between where you did \\[mouse-start-secondary] and where
  694. you use this command matches the text at the front of the kill ring,
  695. this command deletes the text.
  696. Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
  697. which prepares for a second click with this command to delete the text.
  698.  
  699. If you have already made a secondary selection in that buffer,
  700. this command extends or retracts the selection to where you click.
  701. If you do this again in a different position, it extends or retracts
  702. again.  If you do this twice in the same position, it kills the selection."
  703.   (interactive "e")
  704.   (mouse-minibuffer-check click)
  705.   (let ((posn (event-start click))
  706.     (click-posn (posn-point (event-start click)))
  707.     ;; Don't let a subsequent kill command append to this one:
  708.     ;; prevent setting this-command to kill-region.
  709.     (this-command this-command))
  710.     (or (eq (window-buffer (posn-window posn))
  711.         (or (and mouse-secondary-overlay
  712.              (overlay-buffer mouse-secondary-overlay))
  713.         (if mouse-secondary-start
  714.             (marker-buffer mouse-secondary-start))))
  715.     (error "Wrong buffer"))
  716.     (save-excursion
  717.       (set-buffer (window-buffer (posn-window posn)))
  718.       (if (> (mod mouse-selection-click-count 3) 0)
  719.       (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
  720.             (equal click-posn
  721.                    (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
  722.           ;; Find both ends of the object selected by this click.
  723.           (let* ((range
  724.               (mouse-start-end click-posn click-posn
  725.                        mouse-selection-click-count)))
  726.         ;; Move whichever end is closer to the click.
  727.         ;; That's what xterm does, and it seems reasonable.
  728.         (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
  729.                (abs (- click-posn (overlay-end mouse-secondary-overlay))))
  730.             (move-overlay mouse-secondary-overlay (car range)
  731.                   (overlay-end mouse-secondary-overlay))
  732.           (move-overlay mouse-secondary-overlay
  733.                 (overlay-start mouse-secondary-overlay)
  734.                 (nth 1 range)))
  735.         ;; We have already put the old region in the kill ring.
  736.         ;; Replace it with the extended region.
  737.         ;; (It would be annoying to make a separate entry.)
  738.         (setcar kill-ring (buffer-substring
  739.                    (overlay-start mouse-secondary-overlay)
  740.                    (overlay-end mouse-secondary-overlay)))
  741.         (if interprogram-cut-function
  742.             (funcall interprogram-cut-function (car kill-ring)))
  743.         ;; Arrange for a repeated mouse-3 to kill this region.
  744.         (setq mouse-save-then-kill-posn
  745.               (list (car kill-ring) (point) click-posn)))
  746.         ;; If we click this button again without moving it,
  747.         ;; that time kill.
  748.         (progn
  749.           (mouse-save-then-kill-delete-region
  750.            (overlay-start mouse-secondary-overlay)
  751.            (overlay-end mouse-secondary-overlay))
  752.           (setq mouse-save-then-kill-posn nil)
  753.           (setq mouse-selection-click-count 0)
  754.           (delete-overlay mouse-secondary-overlay)))
  755.     (if (and (eq last-command 'mouse-secondary-save-then-kill)
  756.          mouse-save-then-kill-posn
  757.          (eq (car mouse-save-then-kill-posn) (car kill-ring))
  758.          (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
  759.         ;; If this is the second time we've called
  760.         ;; mouse-secondary-save-then-kill, delete the text from the buffer.
  761.         (progn
  762.           (mouse-save-then-kill-delete-region
  763.            (overlay-start mouse-secondary-overlay)
  764.            (overlay-end mouse-secondary-overlay))
  765.           (setq mouse-save-then-kill-posn nil)
  766.           (delete-overlay mouse-secondary-overlay))
  767.       (if (overlay-start mouse-secondary-overlay)
  768.           ;; We have a selection, so adjust it.
  769.           (progn
  770.         (if (numberp click-posn)
  771.             (progn
  772.               ;; Move whichever end of the region is closer to the click.
  773.               ;; That is what xterm does, and it seems reasonable.
  774.               (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
  775.                  (abs (- click-posn (overlay-end mouse-secondary-overlay))))
  776.               (move-overlay mouse-secondary-overlay click-posn
  777.                     (overlay-end mouse-secondary-overlay))
  778.             (move-overlay mouse-secondary-overlay
  779.                       (overlay-start mouse-secondary-overlay)
  780.                       click-posn))
  781.               (setq deactivate-mark nil)))
  782.         (setcar kill-ring (buffer-substring
  783.                    (overlay-start mouse-secondary-overlay)
  784.                    (overlay-end mouse-secondary-overlay)))
  785.         (if interprogram-cut-function
  786.             (funcall interprogram-cut-function (car kill-ring))))
  787.         (if mouse-secondary-start
  788.         ;; All we have is one end of a selection,
  789.         ;; so put the other end here.
  790.         (let ((start (+ 0 mouse-secondary-start)))
  791.           (kill-ring-save start click-posn)
  792.           (if mouse-secondary-overlay
  793.               (move-overlay mouse-secondary-overlay start click-posn)
  794.             (setq mouse-secondary-overlay (make-overlay start click-posn)))
  795.           (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
  796.       (setq mouse-save-then-kill-posn
  797.         (list (car kill-ring) (point) click-posn))))
  798.       (x-set-selection 'SECONDARY
  799.                (if (overlay-buffer mouse-secondary-overlay)
  800.                (buffer-substring
  801.                 (overlay-start mouse-secondary-overlay)
  802.                 (overlay-end mouse-secondary-overlay)))))))
  803.  
  804. (defun mouse-buffer-menu (event)
  805.   "Pop up a menu of buffers for selection with the mouse.
  806. This switches buffers in the window that you clicked on,
  807. and selects that window."
  808.   (interactive "e")
  809.   (mouse-minibuffer-check event)
  810.   (let ((menu
  811.      (list "Buffer Menu"
  812.            (cons "Select Buffer"
  813.              (let ((tail (buffer-list))
  814.                (maxbuf 0)
  815.                head)
  816.                (while tail
  817.              (or (eq ?\ (aref (buffer-name (car tail)) 0))
  818.                  (setq maxbuf
  819.                    (max maxbuf
  820.                     (length (buffer-name (car tail))))))
  821.              (setq tail (cdr tail)))
  822.                (setq tail (buffer-list))
  823.                (while tail
  824.              (let ((elt (car tail)))
  825.                (if (not (string-match "^ "
  826.                           (buffer-name elt)))
  827.                    (setq head (cons
  828.                        (cons
  829.                         (format
  830.                          (format "%%%ds  %%s%%s  %%s"
  831.                              maxbuf)
  832.                          (buffer-name elt)
  833.                          (if (buffer-modified-p elt)
  834.                          "*" " ")
  835.                          (save-excursion
  836.                            (set-buffer elt)
  837.                            (if buffer-read-only "%" " "))
  838.                          (or (buffer-file-name elt) ""))
  839.                         elt)
  840.                        head))))
  841.              (setq tail (cdr tail)))
  842.                (reverse head))))))
  843.     (let ((buf (x-popup-menu event menu))
  844.       (window (posn-window (event-start event))))
  845.       (if buf
  846.       (progn
  847.         (or (framep window) (select-window window))
  848.         (switch-to-buffer buf))))))
  849.  
  850. ;;; These need to be rewritten for the new scroll bar implementation.
  851.  
  852. ;;;!! ;; Commands for the scroll bar.
  853. ;;;!! 
  854. ;;;!! (defun mouse-scroll-down (click)
  855. ;;;!!   (interactive "@e")
  856. ;;;!!   (scroll-down (1+ (cdr (mouse-coords click)))))
  857. ;;;!! 
  858. ;;;!! (defun mouse-scroll-up (click)
  859. ;;;!!   (interactive "@e")
  860. ;;;!!   (scroll-up (1+ (cdr (mouse-coords click)))))
  861. ;;;!! 
  862. ;;;!! (defun mouse-scroll-down-full ()
  863. ;;;!!   (interactive "@")
  864. ;;;!!   (scroll-down nil))
  865. ;;;!! 
  866. ;;;!! (defun mouse-scroll-up-full ()
  867. ;;;!!   (interactive "@")
  868. ;;;!!   (scroll-up nil))
  869. ;;;!! 
  870. ;;;!! (defun mouse-scroll-move-cursor (click)
  871. ;;;!!   (interactive "@e")
  872. ;;;!!   (move-to-window-line (1+ (cdr (mouse-coords click)))))
  873. ;;;!! 
  874. ;;;!! (defun mouse-scroll-absolute (event)
  875. ;;;!!   (interactive "@e")
  876. ;;;!!   (let* ((pos (car event))
  877. ;;;!!      (position (car pos))
  878. ;;;!!      (length (car (cdr pos))))
  879. ;;;!!     (if (<= length 0) (setq length 1))
  880. ;;;!!     (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
  881. ;;;!!        (newpos (* (/ (* (/ (buffer-size) scale-factor)
  882. ;;;!!                 position)
  883. ;;;!!              length)
  884. ;;;!!               scale-factor)))
  885. ;;;!!       (goto-char newpos)
  886. ;;;!!       (recenter '(4)))))
  887. ;;;!! 
  888. ;;;!! (defun mouse-scroll-left (click)
  889. ;;;!!   (interactive "@e")
  890. ;;;!!   (scroll-left (1+ (car (mouse-coords click)))))
  891. ;;;!! 
  892. ;;;!! (defun mouse-scroll-right (click)
  893. ;;;!!   (interactive "@e")
  894. ;;;!!   (scroll-right (1+ (car (mouse-coords click)))))
  895. ;;;!! 
  896. ;;;!! (defun mouse-scroll-left-full ()
  897. ;;;!!   (interactive "@")
  898. ;;;!!   (scroll-left nil))
  899. ;;;!! 
  900. ;;;!! (defun mouse-scroll-right-full ()
  901. ;;;!!   (interactive "@")
  902. ;;;!!   (scroll-right nil))
  903. ;;;!! 
  904. ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
  905. ;;;!!   (interactive "@e")
  906. ;;;!!   (move-to-column (1+ (car (mouse-coords click)))))
  907. ;;;!! 
  908. ;;;!! (defun mouse-scroll-absolute-horizontally (event)
  909. ;;;!!   (interactive "@e")
  910. ;;;!!   (let* ((pos (car event))
  911. ;;;!!      (position (car pos))
  912. ;;;!!      (length (car (cdr pos))))
  913. ;;;!!   (set-window-hscroll (selected-window) 33)))
  914. ;;;!! 
  915. ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
  916. ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
  917. ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
  918. ;;;!! 
  919. ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
  920. ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
  921. ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
  922. ;;;!! 
  923. ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
  924. ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
  925. ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
  926. ;;;!! 
  927. ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
  928. ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
  929. ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
  930. ;;;!! 
  931. ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
  932. ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
  933. ;;;!!         'mouse-scroll-absolute-horizontally)
  934. ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
  935. ;;;!! 
  936. ;;;!! (global-set-key [horizontal-slider mouse-1]
  937. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  938. ;;;!! (global-set-key [horizontal-slider mouse-2]
  939. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  940. ;;;!! (global-set-key [horizontal-slider mouse-3]
  941. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  942. ;;;!! 
  943. ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
  944. ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
  945. ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
  946. ;;;!! 
  947. ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
  948. ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
  949. ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
  950. ;;;!! 
  951. ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
  952. ;;;!!         'mouse-split-window-horizontally)
  953. ;;;!! (global-set-key [mode-line S-mouse-2]
  954. ;;;!!         'mouse-split-window-horizontally)
  955. ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
  956. ;;;!!         'mouse-split-window)
  957.  
  958. ;;;!! ;;;;
  959. ;;;!! ;;;; Here are experimental things being tested.  Mouse events
  960. ;;;!! ;;;; are of the form:
  961. ;;;!! ;;;;    ((x y) window screen-part key-sequence timestamp)
  962. ;;;!! ;;
  963. ;;;!! ;;;;
  964. ;;;!! ;;;; Dynamically track mouse coordinates
  965. ;;;!! ;;;;
  966. ;;;!! ;;
  967. ;;;!! ;;(defun track-mouse (event)
  968. ;;;!! ;;  "Track the coordinates, absolute and relative, of the mouse."
  969. ;;;!! ;;  (interactive "@e")
  970. ;;;!! ;;  (while mouse-grabbed
  971. ;;;!! ;;    (let* ((pos (read-mouse-position (selected-screen)))
  972. ;;;!! ;;       (abs-x (car pos))
  973. ;;;!! ;;       (abs-y (cdr pos))
  974. ;;;!! ;;       (relative-coordinate (coordinates-in-window-p
  975. ;;;!! ;;                 (list (car pos) (cdr pos))
  976. ;;;!! ;;                 (selected-window))))
  977. ;;;!! ;;      (if (consp relative-coordinate)
  978. ;;;!! ;;      (message "mouse: [%d %d], (%d %d)" abs-x abs-y
  979. ;;;!! ;;           (car relative-coordinate)
  980. ;;;!! ;;           (car (cdr relative-coordinate)))
  981. ;;;!! ;;    (message "mouse: [%d %d]" abs-x abs-y)))))
  982. ;;;!! 
  983. ;;;!! ;;
  984. ;;;!! ;; Dynamically put a box around the line indicated by point
  985. ;;;!! ;;
  986. ;;;!! ;;
  987. ;;;!! ;;(require 'backquote)
  988. ;;;!! ;;
  989. ;;;!! ;;(defun mouse-select-buffer-line (event)
  990. ;;;!! ;;  (interactive "@e")
  991. ;;;!! ;;  (let ((relative-coordinate
  992. ;;;!! ;;     (coordinates-in-window-p (car event) (selected-window)))
  993. ;;;!! ;;    (abs-y (car (cdr (car event)))))
  994. ;;;!! ;;    (if (consp relative-coordinate)
  995. ;;;!! ;;    (progn
  996. ;;;!! ;;      (save-excursion
  997. ;;;!! ;;        (move-to-window-line (car (cdr relative-coordinate)))
  998. ;;;!! ;;        (x-draw-rectangle
  999. ;;;!! ;;         (selected-screen)
  1000. ;;;!! ;;         abs-y 0
  1001. ;;;!! ;;         (save-excursion
  1002. ;;;!! ;;         (move-to-window-line (car (cdr relative-coordinate)))
  1003. ;;;!! ;;         (end-of-line)
  1004. ;;;!! ;;         (push-mark nil t)
  1005. ;;;!! ;;         (beginning-of-line)
  1006. ;;;!! ;;         (- (region-end) (region-beginning))) 1))
  1007. ;;;!! ;;      (sit-for 1)
  1008. ;;;!! ;;      (x-erase-rectangle (selected-screen))))))
  1009. ;;;!! ;;
  1010. ;;;!! ;;(defvar last-line-drawn nil)
  1011. ;;;!! ;;(defvar begin-delim "[^ \t]")
  1012. ;;;!! ;;(defvar end-delim   "[^ \t]")
  1013. ;;;!! ;;
  1014. ;;;!! ;;(defun mouse-boxing (event)
  1015. ;;;!! ;;  (interactive "@e")
  1016. ;;;!! ;;  (save-excursion
  1017. ;;;!! ;;    (let ((screen (selected-screen)))
  1018. ;;;!! ;;      (while (= (x-mouse-events) 0)
  1019. ;;;!! ;;    (let* ((pos (read-mouse-position screen))
  1020. ;;;!! ;;           (abs-x (car pos))
  1021. ;;;!! ;;           (abs-y (cdr pos))
  1022. ;;;!! ;;           (relative-coordinate
  1023. ;;;!! ;;        (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
  1024. ;;;!! ;;                     (selected-window)))
  1025. ;;;!! ;;           (begin-reg nil)
  1026. ;;;!! ;;           (end-reg nil)
  1027. ;;;!! ;;           (end-column nil)
  1028. ;;;!! ;;           (begin-column nil))
  1029. ;;;!! ;;      (if (and (consp relative-coordinate)
  1030. ;;;!! ;;           (or (not last-line-drawn)
  1031. ;;;!! ;;               (not (= last-line-drawn abs-y))))
  1032. ;;;!! ;;          (progn
  1033. ;;;!! ;;        (move-to-window-line (car (cdr relative-coordinate)))
  1034. ;;;!! ;;        (if (= (following-char) 10)
  1035. ;;;!! ;;            ()
  1036. ;;;!! ;;          (progn
  1037. ;;;!! ;;            (setq begin-reg (1- (re-search-forward end-delim)))
  1038. ;;;!! ;;            (setq begin-column (1- (current-column)))
  1039. ;;;!! ;;            (end-of-line)
  1040. ;;;!! ;;            (setq end-reg (1+ (re-search-backward begin-delim)))
  1041. ;;;!! ;;            (setq end-column (1+ (current-column)))
  1042. ;;;!! ;;            (message "%s" (buffer-substring begin-reg end-reg))
  1043. ;;;!! ;;            (x-draw-rectangle screen
  1044. ;;;!! ;;                      (setq last-line-drawn abs-y)
  1045. ;;;!! ;;                      begin-column
  1046. ;;;!! ;;                      (- end-column begin-column) 1))))))))))
  1047. ;;;!! ;;
  1048. ;;;!! ;;(defun mouse-erase-box ()
  1049. ;;;!! ;;  (interactive)
  1050. ;;;!! ;;  (if last-line-drawn
  1051. ;;;!! ;;      (progn
  1052. ;;;!! ;;    (x-erase-rectangle (selected-screen))
  1053. ;;;!! ;;    (setq last-line-drawn nil))))
  1054. ;;;!! 
  1055. ;;;!! ;;; (defun test-x-rectangle ()
  1056. ;;;!! ;;;   (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
  1057. ;;;!! ;;;   (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
  1058. ;;;!! ;;;   (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
  1059. ;;;!! 
  1060. ;;;!! ;;
  1061. ;;;!! ;; Here is how to do double clicking in lisp.  About to change.
  1062. ;;;!! ;;
  1063. ;;;!! 
  1064. ;;;!! (defvar double-start nil)
  1065. ;;;!! (defconst double-click-interval 300
  1066. ;;;!!   "Max ticks between clicks")
  1067. ;;;!! 
  1068. ;;;!! (defun double-down (event)
  1069. ;;;!!   (interactive "@e")
  1070. ;;;!!   (if double-start
  1071. ;;;!!       (let ((interval (- (nth 4 event) double-start)))
  1072. ;;;!!     (if (< interval double-click-interval)
  1073. ;;;!!         (progn
  1074. ;;;!!           (backward-up-list 1)
  1075. ;;;!!           ;;      (message "Interval %d" interval)
  1076. ;;;!!           (sleep-for 1)))
  1077. ;;;!!     (setq double-start nil))
  1078. ;;;!!     (setq double-start (nth 4 event))))
  1079. ;;;!!     
  1080. ;;;!! (defun double-up (event)
  1081. ;;;!!   (interactive "@e")
  1082. ;;;!!   (and double-start
  1083. ;;;!!        (> (- (nth 4 event ) double-start) double-click-interval)
  1084. ;;;!!        (setq double-start nil)))
  1085. ;;;!! 
  1086. ;;;!! ;;; (defun x-test-doubleclick ()
  1087. ;;;!! ;;;   (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
  1088. ;;;!! ;;;   (define-key doubleclick-test-map mouse-button-left 'double-down)
  1089. ;;;!! ;;;   (define-key doubleclick-test-map mouse-button-left-up 'double-up))
  1090. ;;;!! 
  1091. ;;;!! ;;
  1092. ;;;!! ;; This scrolls while button is depressed.  Use preferable in scroll bar.
  1093. ;;;!! ;;
  1094. ;;;!! 
  1095. ;;;!! (defvar scrolled-lines 0)
  1096. ;;;!! (defconst scroll-speed 1)
  1097. ;;;!! 
  1098. ;;;!! (defun incr-scroll-down (event)
  1099. ;;;!!   (interactive "@e")
  1100. ;;;!!   (setq scrolled-lines 0)
  1101. ;;;!!   (incremental-scroll scroll-speed))
  1102. ;;;!! 
  1103. ;;;!! (defun incr-scroll-up (event)
  1104. ;;;!!   (interactive "@e")
  1105. ;;;!!   (setq scrolled-lines 0)
  1106. ;;;!!   (incremental-scroll (- scroll-speed)))
  1107. ;;;!! 
  1108. ;;;!! (defun incremental-scroll (n)
  1109. ;;;!!   (while (= (x-mouse-events) 0)
  1110. ;;;!!     (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
  1111. ;;;!!     (scroll-down n)
  1112. ;;;!!     (sit-for 300 t)))
  1113. ;;;!! 
  1114. ;;;!! (defun incr-scroll-stop (event)
  1115. ;;;!!   (interactive "@e")
  1116. ;;;!!   (message "Scrolled %d lines" scrolled-lines)
  1117. ;;;!!   (setq scrolled-lines 0)
  1118. ;;;!!   (sleep-for 1))
  1119. ;;;!! 
  1120. ;;;!! ;;; (defun x-testing-scroll ()
  1121. ;;;!! ;;;   (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
  1122. ;;;!! ;;;     (define-key scrolling-map mouse-button-left 'incr-scroll-down)
  1123. ;;;!! ;;;     (define-key scrolling-map mouse-button-right 'incr-scroll-up)
  1124. ;;;!! ;;;     (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
  1125. ;;;!! ;;;     (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
  1126. ;;;!! 
  1127. ;;;!! ;;
  1128. ;;;!! ;; Some playthings suitable for picture mode?  They need work.
  1129. ;;;!! ;;
  1130. ;;;!! 
  1131. ;;;!! (defun mouse-kill-rectangle (event)
  1132. ;;;!!   "Kill the rectangle between point and the mouse cursor."
  1133. ;;;!!   (interactive "@e")
  1134. ;;;!!   (let ((point-save (point)))
  1135. ;;;!!     (save-excursion
  1136. ;;;!!       (mouse-set-point event)
  1137. ;;;!!       (push-mark nil t)
  1138. ;;;!!       (if (> point-save (point))
  1139. ;;;!!       (kill-rectangle (point) point-save)
  1140. ;;;!!     (kill-rectangle point-save (point))))))
  1141. ;;;!! 
  1142. ;;;!! (defun mouse-open-rectangle (event)
  1143. ;;;!!   "Kill the rectangle between point and the mouse cursor."
  1144. ;;;!!   (interactive "@e")
  1145. ;;;!!   (let ((point-save (point)))
  1146. ;;;!!     (save-excursion
  1147. ;;;!!       (mouse-set-point event)
  1148. ;;;!!       (push-mark nil t)
  1149. ;;;!!       (if (> point-save (point))
  1150. ;;;!!       (open-rectangle (point) point-save)
  1151. ;;;!!     (open-rectangle point-save (point))))))
  1152. ;;;!! 
  1153. ;;;!! ;; Must be a better way to do this.
  1154. ;;;!! 
  1155. ;;;!! (defun mouse-multiple-insert (n char)
  1156. ;;;!!   (while (> n 0)
  1157. ;;;!!     (insert char)
  1158. ;;;!!     (setq n (1- n))))
  1159. ;;;!! 
  1160. ;;;!! ;; What this could do is not finalize until button was released.
  1161. ;;;!! 
  1162. ;;;!! (defun mouse-move-text (event)
  1163. ;;;!!   "Move text from point to cursor position, inserting spaces."
  1164. ;;;!!   (interactive "@e")
  1165. ;;;!!   (let* ((relative-coordinate
  1166. ;;;!!       (coordinates-in-window-p (car event) (selected-window))))
  1167. ;;;!!     (if (consp relative-coordinate)
  1168. ;;;!!     (cond ((> (current-column) (car relative-coordinate))
  1169. ;;;!!            (delete-char
  1170. ;;;!!         (- (car relative-coordinate) (current-column))))
  1171. ;;;!!           ((< (current-column) (car relative-coordinate))
  1172. ;;;!!            (mouse-multiple-insert
  1173. ;;;!!         (- (car relative-coordinate) (current-column)) " "))
  1174. ;;;!!           ((= (current-column) (car relative-coordinate)) (ding))))))
  1175.  
  1176. ;; Choose a completion with the mouse.
  1177.  
  1178. (defun mouse-choose-completion (event)
  1179.   "Click on an alternative in the `*Completions*' buffer to choose it."
  1180.   (interactive "e")
  1181.   (let ((buffer (window-buffer))
  1182.         choice)
  1183.     (save-excursion
  1184.       (set-buffer (window-buffer (posn-window (event-start event))))
  1185.       (if completion-reference-buffer
  1186.       (setq buffer completion-reference-buffer))
  1187.       (save-excursion
  1188.     (goto-char (posn-point (event-start event)))
  1189.     (let (beg end)
  1190.       (skip-chars-forward "^ \t\n")
  1191.       (while (looking-at " [^ \n\t]")
  1192.         (forward-char 1)
  1193.         (skip-chars-forward "^ \t\n"))
  1194.       (setq end (point))
  1195.       (skip-chars-backward "^ \t\n")
  1196.       (while (and (= (preceding-char) ?\ )
  1197.               (not (and (> (point) (1+ (point-min)))
  1198.                 (= (char-after (- (point) 2)) ?\ ))))
  1199.         (backward-char 1)
  1200.         (skip-chars-backward "^ \t\n"))
  1201.       (setq beg (point))
  1202.       (setq choice (buffer-substring beg end)))))
  1203.     (let ((owindow (selected-window)))
  1204.       (select-window (posn-window (event-start event)))
  1205.       (bury-buffer)
  1206.       (select-window owindow))
  1207.     (choose-completion-string choice buffer)))
  1208.  
  1209. ;; Font selection.
  1210.  
  1211. (defun font-menu-add-default ()
  1212.   (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
  1213.      (font-alist x-fixed-font-alist)
  1214.      (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
  1215.     (if (assoc "Default" elt)
  1216.     (delete (assoc "Default" elt) elt))
  1217.     (setcdr elt
  1218.         (cons (list "Default"
  1219.             (cdr (assq 'font (frame-parameters (selected-frame)))))
  1220.           (cdr elt)))))
  1221.  
  1222. (defvar x-fixed-font-alist
  1223.   '("Font menu"
  1224.     ("Misc"
  1225.      ("6x10" "-misc-fixed-medium-r-normal--10-100-75-75-c-60-*-1" "6x10")
  1226.      ("6x12" "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-*-1" "6x12")
  1227.      ("6x13" "-misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-*-1" "6x13")
  1228.      ("lucida 13"
  1229.       "-b&h-lucidatypewriter-medium-r-normal-sans-0-0-0-0-m-0-*-1")
  1230.      ("7x13" "-misc-fixed-medium-r-normal--13-120-75-75-c-70-*-1" "7x13")
  1231.      ("7x14" "-misc-fixed-medium-r-normal--14-130-75-75-c-70-*-1" "7x14")
  1232.      ("9x15" "-misc-fixed-medium-r-normal--15-140-*-*-c-*-*-1" "9x15")
  1233.      ("")
  1234.      ("clean 8x8" "-schumacher-clean-medium-r-normal--*-80-*-*-c-*-*-1")
  1235.      ("clean 8x14" "-schumacher-clean-medium-r-normal--*-140-*-*-c-*-*-1")
  1236.      ("clean 8x10" "-schumacher-clean-medium-r-normal--*-100-*-*-c-*-*-1")
  1237.      ("clean 8x16" "-schumacher-clean-medium-r-normal--*-160-*-*-c-*-*-1")
  1238.      ("")
  1239.      ("sony 8x16" "-sony-fixed-medium-r-normal--16-120-100-100-c-80-*-1")
  1240.      ("")
  1241.      ("fixed" "fixed")
  1242.      ("10x20" "10x20")
  1243.      ("11x18" "11x18")
  1244.      ("12x24" "12x24"))
  1245. ;;; We don't seem to have these; who knows what they are.
  1246. ;;;    ("fg-18" "fg-18")
  1247. ;;;    ("fg-25" "fg-25")
  1248. ;;;    ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
  1249. ;;;    ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
  1250. ;;;    ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
  1251. ;;;    ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
  1252. ;;;    ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
  1253.     ("Courier"
  1254.      ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
  1255.      ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
  1256.      ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
  1257.      ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
  1258.      ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
  1259.      ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
  1260.      ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
  1261.      ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
  1262.      ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
  1263.      ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
  1264.      ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
  1265.      ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
  1266.      ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
  1267.      ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
  1268.      ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
  1269.      ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
  1270.      ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
  1271.      ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
  1272.      ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
  1273.      ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
  1274.      ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
  1275.      ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
  1276.      ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
  1277.      ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
  1278.     )
  1279.   "X fonts suitable for use in Emacs.")
  1280.  
  1281. (defun mouse-set-font (&rest fonts)
  1282.   "Select an emacs font from a list of known good fonts"
  1283.   (interactive
  1284.    (x-popup-menu last-nonmenu-event x-fixed-font-alist))
  1285.   (if fonts
  1286.       (let (font)
  1287.     (while fonts
  1288.       (condition-case nil
  1289.           (progn
  1290.         (set-default-font (car fonts))
  1291.         (setq font (car fonts))
  1292.         (setq fonts nil))
  1293.         (error
  1294.          (setq fonts (cdr fonts)))))
  1295.     (if (null font)
  1296.         (error "Font not found")))))
  1297.  
  1298. ;;; Bindings for mouse commands.
  1299.  
  1300. (define-key global-map [down-mouse-1] 'mouse-drag-region)
  1301. (global-set-key [mouse-1]    'mouse-set-point)
  1302. (global-set-key [drag-mouse-1]    'mouse-set-region)
  1303.  
  1304. ;; These are tested for in mouse-drag-region.
  1305. (global-set-key [double-mouse-1] 'mouse-set-point)
  1306. (global-set-key [triple-mouse-1] 'mouse-set-point)
  1307.  
  1308. (global-set-key [mouse-2]    'mouse-yank-at-click)
  1309. (global-set-key [mouse-3]    'mouse-save-then-kill)
  1310.  
  1311. ;; By binding these to down-going events, we let the user use the up-going
  1312. ;; event to make the selection, saving a click.
  1313. (global-set-key [C-down-mouse-1]    'mouse-buffer-menu)
  1314. (global-set-key [C-down-mouse-3]    'mouse-set-font)
  1315.  
  1316. ;; Replaced with dragging mouse-1
  1317. ;; (global-set-key [S-mouse-1]    'mouse-set-mark)
  1318.  
  1319. (global-set-key [mode-line mouse-1] 'mouse-select-window)
  1320. (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
  1321. (global-set-key [mode-line mouse-3] 'mouse-delete-window)
  1322. (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
  1323.  
  1324. (provide 'mouse)
  1325.  
  1326. ;;; mouse.el ends here
  1327.