home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / mouse.el < prev    next >
Encoding:
Text File  |  1995-08-11  |  49.8 KB  |  1,376 lines

  1. ;;; mouse.el --- window system-independent mouse support.
  2. ;; Keywords: hardware
  3.  
  4. ;; Copyright (C) 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
  5. ;; Copyright (C) 1995 Tinker Systems
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. (provide 'mouse)
  24.  
  25. (require 'mode-motion)
  26.  
  27. (global-set-key 'button1 'mouse-track)
  28. (global-set-key '(shift button1) 'mouse-track-adjust)
  29. (global-set-key '(control button1) 'mouse-track-insert)
  30. (global-set-key '(control shift button1) 'mouse-track-delete-and-insert)
  31. (global-set-key '(meta button1) 'mouse-track-do-rectangle)
  32.  
  33. (global-set-key 'button2 'mouse-yank)
  34.  
  35. (define-key modeline-map 'button1 'mouse-drag-modeline)
  36. (define-key modeline-map 'button3 'modeline-menu)
  37.  
  38. ;; #### Is this actually needed or will the code in x-track-pointer suffice?
  39. (define-key global-map 'button1up 'release-toolbar-button)
  40. (define-key toolbar-map 'button1 'press-toolbar-button)
  41. (define-key toolbar-map 'button1up 'release-and-activate-toolbar-button)
  42. (defvar last-pressed-toolbar-button nil)
  43. (defvar toolbar-active nil)
  44.  
  45. (defvar mouse-track-rectangle-p nil
  46.   "*If true, then dragging out a region with the mouse selects rectangles
  47. instead of simple start/end regions.")
  48.  
  49. (defvar mouse-yank-at-point nil
  50.   "*If non-nil, the function `mouse-yank' will yank text at the cursor location.
  51. Otherwise, the cursor will be moved to the location of the pointer click before
  52. text is inserted.")
  53.  
  54. (defvar mouse-yank-function 'yank    ; x11/x-mouse changes this...
  55.   "Function that is called upon by `mouse-yank' to actually insert text.")
  56.  
  57.  
  58. (defun mouse-select ()
  59.   "Select Emacs window the mouse is on."
  60.   (interactive "@"))
  61.  
  62. (defun mouse-delete-window ()
  63.   "Delete the Emacs window the mouse is on."
  64.   (interactive "@")
  65.   (delete-window))
  66.  
  67. (defun mouse-keep-one-window ()
  68.   "Select Emacs window mouse is on, then kill all other Emacs windows."
  69.   (interactive "@")
  70.   (delete-other-windows))
  71.  
  72. (defun mouse-select-and-split ()
  73.   "Select Emacs window mouse is on, then split it vertically in half."
  74.   (interactive "@")
  75.   (split-window-vertically nil))
  76.  
  77. (defun mouse-set-point (event)
  78.   "Select Emacs window mouse is on, and move point to mouse position."
  79.   (interactive "@e")
  80.   (let ((window (event-window event))
  81.     (pos (event-point event))
  82.     (close-pos (event-closest-point event)))
  83.     (or window (error "not in a window"))
  84.     (select-window window)
  85.     (if (and pos (> pos 0))
  86.     ;; If the event was over a text char, it's easy.
  87.     (goto-char (max (min pos (point-max)) (point-min)))
  88.       (if (and close-pos (> close-pos 0))
  89.       (goto-char (max (min close-pos (point-max)) (point-min)))
  90.     ;; When the event occurs outside of the frame directly to the
  91.     ;; left or right of a modeline, close-point is nil, but
  92.     ;; event-over-modeline is also nil.  That will drop us to this
  93.     ;; point.  So instead of erroring, just return nil.
  94.     nil))))
  95.  
  96. (defun mouse-yank (event)
  97.   "Paste text with the mouse.
  98. If the variable `mouse-yank-at-point' is nil, then pasting occurs at the
  99. location of the click; otherwise, pasting occurs at the current cursor location."
  100.   (interactive "e")
  101.   (and (not mouse-yank-at-point)
  102.        (mouse-set-point event))
  103.   (funcall mouse-yank-function))
  104.  
  105. (defun click-inside-extent-p (click extent)
  106.   "Returns non-nil if the button event is within the bounds of the primary
  107. selection-extent, nil otherwise."
  108.   ;; stig@hackvan.com
  109.   (let ((ewin (event-window click))
  110.     (epnt (event-point click)))
  111.     (and ewin
  112.      epnt
  113.      extent
  114.      (eq (window-buffer ewin)
  115.          (extent-buffer extent))
  116.      (extent-start-position extent)
  117.      (> epnt (extent-start-position extent))
  118.      (> (extent-end-position extent) epnt))))
  119.  
  120. (defun click-inside-selection-p (click)
  121.   (or (click-inside-extent-p click primary-selection-extent)
  122.       (click-inside-extent-p click zmacs-region-extent)
  123.       ))
  124.  
  125. (defun point-inside-extent-p (extent)
  126.   "Returns non-nil if the point is within or just after the bounds of the
  127. primary selection-extent, nil otherwise."
  128.   ;; stig@hackvan.com
  129.   (and extent
  130.        (eq (current-buffer) 
  131.        (extent-buffer extent))
  132.        (> (point) (extent-start-position extent))
  133.        (>= (extent-end-position extent) (point))))
  134.  
  135. (defun point-inside-selection-p ()
  136.   ;; by Stig@hackvan.com
  137.   (or (point-inside-extent-p primary-selection-extent)
  138.       (point-inside-extent-p zmacs-region-extent)))
  139.  
  140. ;;; #### - finish this...
  141. ;;; (defun mouse-drag-or-yank (event)
  142. ;;;   "Either drag or paste the current selection.  If the variable
  143. ;;; `mouse-yank-at-point' is non-nil, then moves the cursor to the location of
  144. ;;; the click before pasting."
  145. ;;;   (interactive "e")
  146. ;;;   (if (click-inside-selection-p event)
  147. ;;;       ;; okay, this is a drag
  148. ;;;       )
  149. ;;;   )
  150.  
  151. (defun mouse-eval-sexp (click force-window)
  152.   "Evaluate the sexp under the mouse.  Usually, this is the last sexp before
  153. the click, but if you click on a left paren, then it is the sexp beginning
  154. with the paren that is evaluated.  Also, since strings evaluate to themselves,
  155. they're fed to re-search-forward and the matched region is highlighted until
  156. the mouse button is released.
  157.  
  158. Perhaps the most useful thing about this function is that the evaluation of
  159. the expression which is clicked upon is relative not to the window where you
  160. click, but to the current window and the current position of point.  Thus,
  161. you can use `mouse-eval-sexp' to interactively test code that acts upon a
  162. buffer...something you cannot do with the standard `eval-last-sexp' function.
  163. It's also fantastic for debugging regular expressions."
  164.   ;; by Stig@hackvan.com
  165.   (interactive "e\nP")
  166.   (let (exp val result-str)
  167.     (setq exp (save-window-excursion
  168.         (save-excursion 
  169.           (mouse-set-point click)
  170.           (save-excursion
  171.             (or (looking-at "(") (forward-sexp -1))
  172.             (read (point-marker))))))
  173.     (cond ((stringp exp)
  174.        (if (setq val (re-search-forward exp nil t))
  175.            (let* ((oo (make-extent (match-beginning 0) (match-end 0))))
  176.          (set-extent-face oo 'highlight)
  177.          (set-extent-priority oo 1000)
  178.          ;; wait for button release...
  179.          (setq unread-command-event (next-command-event))
  180.          (delete-extent oo))
  181.          (message "Regex \"%s\" not found" exp)
  182.          (ding nil 'quiet)))
  183.       (t (setq val (if (fboundp 'eval-interactive)
  184.                (eval-interactive exp)
  185.              (eval exp)))))
  186.     (setq result-str (prin1-to-string val))
  187.     ;; #### -- need better test
  188.     (if (and (not force-window)
  189.          (<= (length result-str) (window-width (selected-window))))
  190.     (message "%s" result-str)
  191.       (with-output-to-temp-buffer "*Mouse-Eval*"
  192.     (condition-case nil
  193.         (pprint val)
  194.       (error (prin1 val))))
  195.       )))
  196.  
  197. (defun mouse-line-length (event)
  198.   "Print the length of the line indicated by the pointer."
  199.   (interactive "@e")
  200.   (save-excursion
  201.     (mouse-set-point event)
  202.     (message "Line length: %d" (- (progn (end-of-line) (point))
  203.                   (progn (beginning-of-line) (point)))))
  204.   (sleep-for 1))
  205.  
  206. (defun mouse-set-mark (event)
  207.   "Select Emacs window mouse is on, and set mark at mouse position.
  208. Display cursor at that position for a second."
  209.   (interactive "@e")
  210.   (let ((point-save (point)))
  211.     (unwind-protect
  212.     (progn (mouse-set-point event)
  213.            (push-mark nil t)
  214.            (sit-for 1))
  215.       (goto-char point-save))))
  216.  
  217. (defun mouse-scroll (event)
  218.   "Scroll point to the mouse position."
  219.   (interactive "@e")
  220.   (save-excursion
  221.     (mouse-set-point event)
  222.     (recenter 0)
  223.     (scroll-right (event-x event))))
  224.  
  225. (defun mouse-del-char (event)
  226.   "Delete the char pointed to by the mouse."
  227.   (interactive "@e")
  228.   (save-excursion
  229.     (mouse-set-point event)
  230.     (delete-char 1 nil)))
  231.  
  232. (defun mouse-kill-line (event)
  233.   "Kill the line pointed to by the mouse."
  234.   (interactive "@e")
  235.   (save-excursion
  236.     (mouse-set-point event)
  237.     (kill-line nil)))
  238.  
  239.  
  240. (defun narrow-window-to-region (m n)
  241.   "Narrow window to region between point and last mark"
  242.   (interactive "r")
  243.   (save-excursion
  244.     (save-restriction
  245.       (if (eq (selected-window) (next-window))
  246.       (split-window))
  247.       (goto-char m)
  248.       (recenter 0)
  249.       (if (eq (selected-window)
  250.           (if (zerop (minibuffer-depth))
  251.           (next-window)))
  252.       ()
  253.     (shrink-window (- (- (window-height) (count-lines m n)) 1))))))
  254.  
  255. (defun mouse-window-to-region (event)
  256.   "Narrow window to region between cursor and mouse pointer."
  257.   (interactive "@e")
  258.   (let ((point-save (point)))
  259.     (unwind-protect
  260.     (progn (mouse-set-point event)
  261.            (push-mark nil t)
  262.            (sit-for 1))
  263.       (goto-char point-save)
  264.       (narrow-window-to-region (region-beginning) (region-end)))))
  265.  
  266. (defun mouse-ignore ()
  267.   "Don't do anything."
  268.   (interactive))
  269.  
  270.  
  271. ;;
  272. ;; Commands for the scroll bar.
  273. ;;
  274.  
  275. ;; Vertical bar
  276.  
  277. (defun mouse-scroll-down (nlines)
  278.   (interactive "@p")
  279.   (scroll-down nlines))
  280.  
  281. (defun mouse-scroll-up (nlines)
  282.   (interactive "@p")
  283.   (scroll-up nlines))
  284.  
  285. (defun mouse-scroll-down-full ()
  286.   (interactive "@")
  287.   (scroll-down nil))
  288.  
  289. (defun mouse-scroll-up-full ()
  290.   (interactive "@")
  291.   (scroll-up nil))
  292.  
  293. (defun mouse-scroll-move-cursor (nlines)
  294.   (interactive "@p")
  295.   (move-to-window-line nlines))
  296.  
  297. (defun mouse-scroll-absolute (event)
  298.   (interactive "@e")
  299.   (let* ((position (event-x event))
  300.      (length (event-y event))
  301.      (size (buffer-size))
  302.      (scale-factor (max 1 (/ 8000000 size)))
  303.      (newpos (* (/ (* (/ size scale-factor) position) length)
  304.             scale-factor)))
  305.     (goto-char newpos)
  306.     (recenter '(4))))
  307.  
  308. ;; These scroll while the invoking button is depressed.
  309.  
  310. (defvar scrolled-lines 0)
  311. (defvar scroll-speed 1)
  312.  
  313. (defun incr-scroll-down (event)
  314.   (interactive "@e")
  315.   (setq scrolled-lines 0)
  316.   (incremental-scroll scroll-speed))
  317.  
  318. (defun incr-scroll-up (event)
  319.   (interactive "@e")
  320.   (setq scrolled-lines 0)
  321.   (incremental-scroll (- scroll-speed)))
  322.  
  323. (defun incremental-scroll (n)
  324.   (let ((down t))
  325.     (while down
  326.       (sit-for mouse-track-scroll-delay)
  327.       (cond ((input-pending-p)
  328.          (let ((event (next-command-event)))
  329.            (if (or (button-press-event-p event)
  330.                (button-release-event-p event))
  331.            (setq down nil))
  332.            (dispatch-event event))))
  333.       (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
  334.       (scroll-down n))))
  335.  
  336. (defun incr-scroll-stop (event)
  337.   (interactive "@e")
  338.   (setq scrolled-lines 0)
  339.   (sleep-for 1))
  340.  
  341.  
  342. (defun mouse-scroll-left (ncolumns)
  343.   (interactive "@p")
  344.   (scroll-left ncolumns))
  345.  
  346. (defun mouse-scroll-right (ncolumns)
  347.   (interactive "@p")
  348.   (scroll-right ncolumns))
  349.  
  350. (defun mouse-scroll-left-full ()
  351.   (interactive "@")
  352.   (scroll-left nil))
  353.  
  354. (defun mouse-scroll-right-full ()
  355.   (interactive "@")
  356.   (scroll-right nil))
  357.  
  358. (defun mouse-scroll-move-cursor-horizontally (ncolumns)
  359.   (interactive "@p")
  360.   (move-to-column ncolumns))
  361.  
  362. (defun mouse-scroll-absolute-horizontally (event)
  363.   (interactive "@e")
  364.   (set-window-hscroll (selected-window) 33))
  365.  
  366.  
  367.  
  368. ;;; mouse/selection tracking
  369. ;;; generalized mouse-track
  370.  
  371. (defvar mouse-track-down-hook nil
  372.   "Function or functions called when the user presses the mouse.
  373. This hook is invoked by `mouse-track'; thus, it will not be called
  374. for any buttons with a different binding.  The functions will be
  375. called with two arguments: the button-press event and a click
  376. count (see `mouse-track-click-hook').
  377.  
  378. If any function returns non-nil, the remaining functions will not be
  379. called.
  380.  
  381. Note that most applications should take action when the mouse is
  382. released, not when it is pressed.'")
  383.  
  384. (defvar mouse-track-drag-hook nil
  385.   "Function or functions called when the user drags the mouse.
  386. This hook is invoked by `mouse-track'; thus, it will not be called
  387. for any buttons with a different binding.  The functions will be
  388. called with three arguments: the mouse-motion event, a click
  389. count (see `mouse-track-click-hook'), and whether the call to
  390. this hook occurred as a result of a drag timeout (see
  391. `mouse-track-scroll-delay').
  392.  
  393. If any function returns non-nil, the remaining functions will not be
  394. called.
  395.  
  396. Note that no calls to this function will be made until the user
  397. initiates a drag (i.e. moves the mouse more than a certain
  398. threshold in either the X or the Y direction, as defined by
  399. `mouse-track-x-threshold' and `mouse-track-y-threshold').
  400.  
  401. See also `mouse-track-drag-up-hook'.")
  402.  
  403. (defvar mouse-track-drag-up-hook nil
  404.   "Function or functions called when the user finishes a drag.
  405. This hook is invoked by `mouse-track'; thus, it will not be called
  406. for any buttons with a different binding.  The functions will be
  407. called with two arguments: the button-press event and a click
  408. count (see `mouse-track-click-hook').
  409.  
  410. If any function returns non-nil, the remaining functions will not be
  411. called.
  412.  
  413. Note that this hook will not be invoked unless the user has
  414. initiated a drag, i.e. moved the mouse more than a certain threshold
  415. (see `mouse-track-drag-hook').  When this function is invoked,
  416. `mouse-track-drag-hook' will have been invoked at least once.
  417.  
  418. See also `mouse-track-click-hook'.")
  419.  
  420. (defvar mouse-track-click-hook nil
  421.   "Function or functions called when the user clicks the mouse.
  422. `Clicking' means pressing and releasing the mouse without having
  423. initiated a drag (i.e. without having moved more than a certain
  424. threshold -- see `mouse-track-drag-hook').
  425.  
  426. This hook is invoked by `mouse-track'; thus, it will not be called
  427. for any buttons with a different binding.  The functions will be
  428. called with two arguments: the button-release event and a click
  429. count, which specifies the number of times that the mouse has been
  430. clicked in a series of clicks, each of which is separated by at most
  431. `mouse-track-multi-click-time'.  This can be used to implement actions
  432. that are called on double clicks, triple clicks, etc.
  433.  
  434. If any function returns non-nil, the remaining functions will not be
  435. called.
  436.  
  437. See also `mouse-track-drag-up-hook.")
  438.  
  439. (defvar mouse-track-up-hook nil
  440.   "Function or functions called when the user releases the mouse.
  441. This hook is invoked by `mouse-track'; thus, it will not be called
  442. for any buttons with a different binding.  The functions will be
  443. called with two arguments: the button-release event and a click
  444. count (see `mouse-track-click-hook').
  445.  
  446. For many applications, it is more appropriate to use one or both
  447. of `mouse-track-click-hook' and `mouse-track-drag-up-hook'.")
  448.  
  449. (defvar mouse-track-cleanup-hook nil
  450.   "Function or functions called when `mouse-track' terminates.
  451. This hook will be called in all circumstances, even upon a
  452. non-local exit out of `mouse-track', and so is useful for
  453. doing cleanup work such as removing extents that may have
  454. been created during the operation of `mouse-track'.
  455.  
  456. Unlike all of the other mouse-track hooks, this is a \"normal\"
  457. hook: the hook functions are called with no arguments, and
  458. all hook functions are called regardless of their return
  459. values.")
  460.  
  461. (defvar mouse-track-multi-click-time 400
  462.   "Maximum number of milliseconds allowed between clicks for a multi-click.
  463. See `mouse-track-click-hook'.")
  464.  
  465. (defvar mouse-track-scroll-delay 100
  466.   "Maximum of milliseconds between calls to `mouse-track-drag-hook'.
  467. If the user is dragging the mouse (i.e. the button is held down and
  468. a drag has been initiated) and does not move the mouse for this many
  469. milliseconds, the hook will be called with t as the value of the
  470. WAS-TIMEOUT parameter.  This can be used to implement scrolling
  471. in a selection when the user drags the mouse out the window it
  472. was in.
  473.  
  474. A value of nil disables the timeout feature.")
  475.  
  476. (defvar mouse-track-x-threshold '(face-width 'default)
  477.   "Minimum number of pixels in the X direction for a drag to be initiated.
  478. If the mouse is moved more than either the X or Y threshold while the
  479. button is held down (see also `mouse-track-y-threshold'), then a drag
  480. is initiated; otherwise the gesture is considered to be a click.
  481. See `mouse-track'.
  482.  
  483. The value should be either a number of a form to be evaluated to
  484. produce a number.")
  485.  
  486. (defvar mouse-track-y-threshold '(face-height 'default)
  487.   "Minimum number of pixels in the Y direction for a drag to be initiated.
  488. If the mouse is moved more than either the X or Y threshold while the
  489. button is held down (see also `mouse-track-x-threshold'), then a drag
  490. is initiated; otherwise the gesture is considered to be a click.
  491. See `mouse-track'.
  492.  
  493. The value should be either a number of a form to be evaluated to
  494. produce a number.")
  495.  
  496. ;; these variables are private to mouse-track.
  497. (defvar mouse-track-up-time nil)
  498. (defvar mouse-track-up-x nil)
  499. (defvar mouse-track-up-y nil)
  500. (defvar mouse-track-timeout-id nil)
  501. (defvar mouse-track-click-count nil)
  502.  
  503. (defun mouse-track-set-timeout (event)
  504.   (if mouse-track-timeout-id
  505.       (disable-timeout mouse-track-timeout-id))
  506.   (if mouse-track-scroll-delay
  507.       (setq mouse-track-timeout-id
  508.         (add-timeout (/ mouse-track-scroll-delay 1000.0)
  509.              'mouse-track-scroll-undefined
  510.              (copy-event event)))))
  511.  
  512. (defun mouse-track-run-hook (hook event &rest args)
  513.   ;; ugh, can't use run-special-hook-with-args because we
  514.   ;; have to get the value using symbol-value-in-buffer.
  515.   ;; Doing a save-excursion/set-buffer is wrong because
  516.   ;; the hook might want to change the buffer, but just
  517.   ;; doing a set-buffer is wrong because the hook might
  518.   ;; not want to change the buffer.
  519.   (let ((buffer (event-buffer event)))
  520.     (if mouse-grabbed-buffer (setq buffer mouse-grabbed-buffer))
  521.     (if buffer
  522.     (let ((value (symbol-value-in-buffer hook buffer nil)))
  523.       (if (and (listp value) (not (eq (car value) 'lambda)))
  524.           (let (retval)
  525.         (while (and value
  526.                 (not (setq retval (apply (car value) event args))))
  527.           (setq value (cdr value)))
  528.         retval)
  529.         (apply value event args))))))
  530.  
  531. (defun mouse-track-scroll-undefined (random)
  532.   ;; the old implementation didn't actually define this function,
  533.   ;; and in normal use it won't ever be called because the timeout
  534.   ;; will either be removed before it fires or will be picked off
  535.   ;; with next-event and not dispatched.  However, if you're
  536.   ;; attempting to debug a click-hook (which is pretty damn
  537.   ;; difficult to do), this function may get called.
  538. )
  539.  
  540. (defun mouse-track (event)
  541.   "Make a selection with the mouse.  This should be bound to a mouse button.
  542. The behavior of XEmacs during mouse selection is customizable using various
  543. hooks and variables: see `mouse-track-click-hook', `mouse-track-drag-hook',
  544. `mouse-track-drag-up-hook', `mouse-track-down-hook', `mouse-track-up-hook',
  545. `mouse-track-cleanup-hook', `mouse-track-multi-click-time',
  546. `mouse-track-scroll-delay', `mouse-track-x-threshold', and
  547. `mouse-track-y-threshold'.
  548.  
  549. Default handlers are provided to implement standard selecting/positioning
  550. behavior.  You can explicitly request this default behavior, and override
  551. any custom-supplied handlers, by using the function `mouse-track-default'
  552. instead of `mouse-track'.
  553.  
  554. Default behavior is as follows: 
  555.  
  556. If you click-and-drag, the selection will be set to the region between the
  557. point of the initial click and the point at which you release the button.
  558. These positions need not be ordered.
  559.  
  560. If you click-and-release without moving the mouse, then the point is moved
  561. and the selection is disowned (there will be no selection owner).  The mark
  562. will be set to the previous position of point.
  563.  
  564. If you double-click, the selection will extend by symbols instead of by
  565. characters.  If you triple-click, the selection will extend by lines.
  566.  
  567. If you drag the mouse off the top or bottom of the window, you can select
  568. pieces of text which are larger than the visible part of the buffer; the
  569. buffer will scroll as necessary.
  570.  
  571. The selected text becomes the current X Selection, and is also copied to the
  572. top of the kill ring.  The point will be left at the position at which you
  573. released the button, and the mark will be left at the initial click position."
  574.   (interactive "e")
  575.   (let ((mouse-down t)
  576.     (xthresh (eval mouse-track-x-threshold))
  577.     (ythresh (eval mouse-track-y-threshold))
  578.     (orig-x (event-x-pixel event))
  579.     (orig-y (event-y-pixel event))
  580.     (buffer (event-buffer event))
  581.     (mouse-grabbed-buffer (event-buffer event))
  582.     mouse-moved)
  583.     (if (or (not mouse-track-up-x)
  584.         (not mouse-track-up-y)
  585.         (not mouse-track-up-time)
  586.         (> (- (event-timestamp event) mouse-track-up-time)
  587.            mouse-track-multi-click-time)
  588.         (> (abs (- mouse-track-up-x orig-x)) xthresh)
  589.         (> (abs (- mouse-track-up-y orig-y)) ythresh))
  590.     (setq mouse-track-click-count 1)
  591.       (setq mouse-track-click-count (1+ mouse-track-click-count)))
  592.     (if (not (event-window event))
  593.     (error "Not over a window."))
  594.     (mouse-track-run-hook 'mouse-track-down-hook
  595.               event mouse-track-click-count)
  596.     (unwind-protect
  597.     (while mouse-down
  598.       (setq event (next-event event))
  599.       (cond ((motion-event-p event)
  600.          (if (and (not mouse-moved)
  601.               (or (> (abs (- (event-x-pixel event) orig-x))
  602.                  xthresh)
  603.                   (> (abs (- (event-y-pixel event) orig-y))
  604.                  ythresh)))
  605.              (setq mouse-moved t))
  606.          (if mouse-moved
  607.              (mouse-track-run-hook 'mouse-track-drag-hook
  608.               event mouse-track-click-count nil))
  609.          (mouse-track-set-timeout event))
  610.         ((and (timeout-event-p event)
  611.               (eq (event-function event)
  612.               'mouse-track-scroll-undefined))
  613.          (if mouse-moved
  614.              (mouse-track-run-hook 'mouse-track-drag-hook
  615.               (event-object event) mouse-track-click-count t))
  616.          (mouse-track-set-timeout (event-object event)))
  617.         ((button-release-event-p event)
  618.          (setq mouse-track-up-time (event-timestamp event))
  619.          (setq mouse-track-up-x (event-x-pixel event))
  620.          (setq mouse-track-up-y (event-y-pixel event))
  621.          (setq mouse-down nil)
  622.          (mouse-track-run-hook 'mouse-track-up-hook
  623.           event mouse-track-click-count)
  624.          (if mouse-moved
  625.              (mouse-track-run-hook 'mouse-track-drag-up-hook
  626.               event mouse-track-click-count)
  627.            (mouse-track-run-hook 'mouse-track-click-hook
  628.             event mouse-track-click-count)))
  629.         ((key-press-event-p event)
  630.          (error "Selection aborted"))
  631.         (t
  632.          (dispatch-event event))))
  633.       ;; protected
  634.       (if mouse-track-timeout-id
  635.       (disable-timeout mouse-track-timeout-id))
  636.       (setq mouse-track-timeout-id nil)
  637.       (and buffer
  638.        (save-excursion
  639.          (set-buffer buffer)
  640.          (run-hooks 'mouse-track-cleanup-hook))))))
  641.  
  642.  
  643. ;;;;;;;;;;;; default handlers: new version of mouse-track
  644.  
  645. (defvar default-mouse-track-type nil)
  646. (defvar default-mouse-track-type-list '(char word line))
  647. (defvar default-mouse-track-window nil)
  648. (defvar default-mouse-track-extent nil)
  649. (defvar default-mouse-track-adjust nil)
  650. (defvar default-mouse-track-min-anchor nil)
  651. (defvar default-mouse-track-max-anchor nil)
  652. (defvar default-mouse-track-result nil)
  653. (defvar default-mouse-track-down-event nil)
  654.  
  655. (defun default-mouse-track-set-point-in-window (event window)
  656.   (if (not (and (not (event-over-modeline-p event))
  657.         (eq (event-window event) window)
  658.         (let ((p (event-closest-point event)))
  659.           (and p (pos-visible-in-window-p p window)))))
  660.       nil
  661.     (mouse-set-point event)
  662.     t))
  663.  
  664. (defun default-mouse-track-scroll-and-set-point (event window)
  665.   (select-window window)
  666.   (let ((edges (window-pixel-edges window))
  667.     (row (event-y-pixel event))
  668.     (height (face-height 'default)))
  669.     (cond ((< (abs (- row (nth 1 edges))) (abs (- row (nth 3 edges))))
  670.        ;; closer to window's top than to bottom, so move up
  671.        (let ((delta (max 1 (/ (- (nth 1 edges) row) height))))
  672.          (condition-case () (scroll-down delta) (error))
  673.          (goto-char (window-start))))
  674.       ((>= (point) (point-max)))
  675.       (t
  676.        ;; scroll by one line if over the modeline or a clipped line
  677.        (let ((delta (if (or (event-over-modeline-p event)
  678.                 (< row (nth 3 edges)))
  679.                 1
  680.               (+ (/ (- row (nth 3 edges)) height) 1)))
  681.          (close-pos (event-closest-point event)))
  682.          (condition-case () (scroll-up delta) (error))
  683.          (if (and close-pos (pos-visible-in-window-p close-pos))
  684.          (goto-char close-pos)
  685.            (goto-char (window-end))
  686.            (vertical-motion delta)
  687.            ;; window-end reports the end of the clipped line, even if
  688.            ;; scroll-on-clipped-lines is t.  compensate.
  689.            ;; (If window-end gets fixed this can be removed.)
  690.            (if (not (pos-visible-in-window-p (max (1- (point)) 
  691.                               (point-min))))
  692.            (vertical-motion -1))
  693.            (condition-case () (backward-char 1) 
  694.          (error (end-of-line)))))))))
  695.  
  696.  
  697. ;; This remembers the last position at which the user clicked, for the
  698. ;; benefit of mouse-track-adjust (for example, button1; scroll until the
  699. ;; position of the click is off the frame; then Sh-button1 to select the
  700. ;; new region.
  701. (defvar default-mouse-track-previous-point nil)
  702.  
  703. (defun default-mouse-track-set-point (event window)
  704.   (if (default-mouse-track-set-point-in-window event window)
  705.       nil
  706.     (default-mouse-track-scroll-and-set-point event window)))
  707.  
  708. (defsubst default-mouse-track-beginning-of-word (symbolp)
  709.   (let ((word-constituent (cond ((eq symbolp t) "\\w\\|\\s_\\|\\s'")
  710.                 ((null symbolp) "\\w")
  711.                 (t "[^ \t\n]")))
  712.     (white-space "[ \t]"))
  713.     (cond ((bobp) nil)
  714.       ((looking-at word-constituent)
  715.        (backward-char)
  716.        (while (and (not (bobp)) (looking-at word-constituent))
  717.          (backward-char))
  718.        (if (or (not (bobp)) (not (looking-at word-constituent)))
  719.            (forward-char)))
  720.       ((looking-at white-space)
  721.        (backward-char)
  722.        (while (looking-at white-space)
  723.          (backward-char))
  724.        (forward-char)))))
  725.  
  726. (defun default-mouse-track-end-of-word (symbolp)
  727.   (let ((word-constituent (cond ((eq symbolp t) "\\w\\|\\s_\\|\\s'")
  728.                 ((null symbolp) "\\w")
  729.                 (t "[^ \t\n]")))
  730.     (white-space "[ \t]"))
  731.     (cond ((looking-at word-constituent) ; word or symbol constituent
  732.        (while (looking-at word-constituent)
  733.          (forward-char)))
  734.       ((looking-at white-space) ; word or symbol constituent
  735.        (while (looking-at white-space)
  736.          (forward-char))))))
  737.  
  738. (defun default-mouse-track-normalize-point (type forwardp)
  739.   (cond ((eq type 'word)
  740.      ;; trap the beginning and end of buffer errors
  741.      (condition-case ()
  742.          (if forwardp
  743.          (default-mouse-track-end-of-word t)
  744.            (default-mouse-track-beginning-of-word t))
  745.        (error ())))
  746.     ((eq type 'line)
  747.      (if forwardp (end-of-line) (beginning-of-line)))
  748.     ((eq type 'buffer)
  749.      (if forwardp (end-of-buffer) (beginning-of-buffer)))))
  750.  
  751. (defun default-mouse-track-next-move (min-anchor max-anchor extent)
  752.   (let ((anchor (if (<= (point) min-anchor) max-anchor min-anchor)))
  753.     (default-mouse-track-normalize-point
  754.       default-mouse-track-type (> (point) anchor))
  755.     (if (consp extent)
  756.     (default-mouse-track-next-move-rect anchor (point) extent)
  757.       (if extent
  758.       (if (<= anchor (point))
  759.           (set-extent-endpoints extent anchor (point))
  760.         (set-extent-endpoints extent (point) anchor))))))
  761.  
  762. (defun default-mouse-track-next-move-rect (start end extents &optional pad-p)
  763.   (if (< end start)
  764.       (let ((tmp start)) (setq start end end tmp)))
  765.   (cond
  766.    ((= start end)        ; never delete the last remaining extent
  767.     (mapcar 'delete-extent (cdr extents))
  768.     (setcdr extents nil)
  769.     (set-extent-endpoints (car extents) start start))
  770.    (t
  771.     (let ((indent-tabs-mode nil)    ; if pad-p, don't use tabs
  772.       (rest extents)
  773.       left right last p)
  774.       (save-excursion
  775.     (save-restriction
  776.       (goto-char end)
  777.       (setq right (current-column))
  778.       (goto-char start)
  779.       (setq left (current-column))
  780.       (if (< right left)
  781.           (let ((tmp left))
  782.         (setq left right right tmp)
  783.         (setq start (- start (- right left))
  784.               end (+ end (- right left)))))
  785.       ;; End may have been set to a value greater than point-max if drag
  786.       ;; or movement extends to end of buffer, so reset it.
  787.       (setq end (min end (point-max)))
  788.       (beginning-of-line)
  789.       (narrow-to-region (point) end)
  790.       (goto-char start)
  791.       (while (and rest (not (eobp)))
  792.         (setq p (point))
  793.         (move-to-column right pad-p)
  794.         (set-extent-endpoints (car rest) p (point))
  795.         ;; this code used to look at the return value
  796.         ;; of forward-line, but that doesn't work because
  797.         ;; forward-line has bogus behavior: If you're on
  798.         ;; the last line of a buffer but not at the very
  799.         ;; end, forward-line will move you to the very
  800.         ;; end and return 0 instead of 1, like it should.
  801.         ;; the result was frequent infinite loops here,
  802.         ;; creating very large numbers of extents at
  803.         ;; the same position.  There was an N^2 sorting
  804.         ;; algorithm in extents.c for extents at a
  805.         ;; particular position, and the result was very
  806.         ;; bad news.
  807.         (forward-line 1)
  808.         (if (not (eobp))
  809.         (move-to-column left pad-p))
  810.         (setq last rest
  811.           rest (cdr rest)))
  812.       (cond (rest
  813.          (mapcar 'delete-extent rest)
  814.          (setcdr last nil))
  815.         ((not (eobp))
  816.          (while (not (eobp))
  817.            (setq p (point))
  818.            (move-to-column right pad-p)
  819.            (let ((e (make-extent p (point))))
  820.              (set-extent-face e (extent-face (car extents)))
  821.              (set-extent-priority e (extent-priority (car extents)))
  822.              (setcdr last (cons e nil))
  823.              (setq last (cdr last)))
  824.            (forward-line 1)
  825.            (if (not (eobp))
  826.                (move-to-column left pad-p))
  827.            )))))
  828.       ))))
  829.  
  830. (defun default-mouse-track-has-selection-p (buffer)
  831.   (and (or (not (eq 'x (device-type (selected-device))))
  832.        (x-selection-owner-p))
  833.        (extentp primary-selection-extent)
  834.        (eq buffer (extent-buffer primary-selection-extent))))
  835.  
  836. (defun default-mouse-track-anchor (adjust previous-point)
  837.   (if adjust
  838.       (if (default-mouse-track-has-selection-p (current-buffer))
  839.       (let ((start (extent-start-position primary-selection-extent))
  840.         (end (extent-end-position primary-selection-extent)))
  841.         (cond ((< (point) start) end)
  842.           ((> (point) end) start)
  843.           ((> (- (point) start) (- end (point))) start)
  844.           (t end)))
  845.     previous-point)
  846.     (point)))
  847.  
  848. (defun default-mouse-track-maybe-own-selection (pair type)
  849.   (let ((start (car pair))
  850.     (end (cdr pair)))
  851.     (or (= start end) (push-mark (if (= (point) start) end start)))
  852.     (cond (zmacs-regions
  853.        (if (= start end)
  854.            nil
  855.          ;; #### UTTER KLUDGE.
  856.          ;; If we don't have this sit-for here, then triple-clicking
  857.          ;; will result in the line not being highlighted as it
  858.          ;; should.  What appears to be happening is this:
  859.          ;;
  860.          ;; -- each time the button goes down, the selection is
  861.          ;;    disowned (see comment "remove the existing selection
  862.          ;;    to unclutter the display", below).
  863.          ;; -- this causes a SelectionClear event to be sent to
  864.          ;;    XEmacs.
  865.          ;; -- each time the button goes up except the first, the
  866.          ;;    selection is owned again.
  867.          ;; -- later, XEmacs processes the SelectionClear event.
  868.          ;;    The selection code attempts to keep track of the
  869.          ;;    time that it last asserted the selection, and
  870.          ;;    compare it to the time of the SelectionClear event,
  871.          ;;    to see if it's a bogus notification or not (as
  872.          ;;    is the case here).  However, for some unknown
  873.          ;;    reason this doesn't work in the triple-clicking
  874.          ;;    case, and the selection code bogusly thinks this
  875.          ;;    SelectionClear event is the real thing.
  876.          ;; -- putting the sit-for in causes the pending
  877.          ;;    SelectionClear events to get processed before
  878.          ;;    the selection is reasserted, so everything works
  879.          ;;    out OK.
  880.          ;;
  881.          ;; Presumably(?) this means there is a weird timing bug
  882.          ;; in the selection code, but there's not a chance in hell
  883.          ;; that I have the patience to track it down.  Blame the
  884.          ;; designers of X for fucking everything up so badly.
  885.          ;;
  886.          ;; This was originally a sit-for 0 but that wasn't
  887.          ;; sufficient to make things work.  Even this isn't
  888.          ;; always sufficient but it seems to give something
  889.          ;; approaching a 99% success rate.  Making it higher yet
  890.          ;; would help guarantee success with the price that the
  891.          ;; delay would start to become noticable.
  892.          ;;
  893.          (sit-for 0.15 t)
  894.          (zmacs-activate-region)))
  895.       ((eq 'x (device-type (selected-device)))
  896.        (if (= start end)
  897.            (x-disown-selection type)
  898.          (x-own-selection (cons (set-marker (make-marker) start)
  899.                     (set-marker (make-marker) end))
  900.                   type))))
  901.     (if (and (eq 'x (device-type (selected-device)))
  902.          (not (= start end)))
  903.     (x-store-cutbuffer (buffer-substring start end)))))
  904.  
  905. (defun default-mouse-track-deal-with-down-event (click-count)
  906.   (let ((event default-mouse-track-down-event))
  907.     (if (null event) nil
  908.       (select-frame (event-frame event))
  909.       (let ((adjust default-mouse-track-adjust)
  910.         ;; ####When you click on the splash-screen,
  911.         ;; event-{closest-,}point can be out of bounds.  Should
  912.         ;; event-closest-point really be allowed to return a bad
  913.         ;; position like that?  Maybe pixel_to_glyph_translation
  914.         ;; needs to invalidate its cache when the buffer changes.
  915.         ;; -dkindred@cs.cmu.edu
  916.         (close-pos  (save-excursion
  917.               (set-buffer (event-buffer event))
  918.               (let ((p (event-closest-point event)))
  919.                 (and p (min (max p (point-min)) (point-max))))))
  920.         extent previous-point)
  921.     
  922.     (if (not (event-window event))
  923.         (error "not over window?"))
  924.     (setq default-mouse-track-type
  925.           (nth (mod (1- click-count)
  926.             (length default-mouse-track-type-list))
  927.            default-mouse-track-type-list))
  928.     (setq default-mouse-track-window (event-window event))
  929.     ;; Note that the extent used here is NOT the extent which
  930.     ;; ends up as the value of zmacs-region-extent - this one is used
  931.     ;; just during mouse-dragging.
  932.     (setq default-mouse-track-extent
  933.           (make-extent close-pos close-pos (event-buffer event)))
  934.     (setq extent default-mouse-track-extent)
  935.     (set-extent-face extent 'zmacs-region)
  936.     ;; While the selection is being dragged out, give the selection extent
  937.     ;; slightly higher priority than any mouse-highlighted extent, so that
  938.     ;; the exact endpoints of the selection will be visible while the mouse
  939.     ;; is down.  Normally, the selection and mouse highlighting have the
  940.     ;; same priority, so that conflicts between the two of them are
  941.     ;; resolved by the usual size-and-endpoint-comparison method.
  942.     (set-extent-priority extent (1+ mouse-highlight-priority))
  943.     (if mouse-track-rectangle-p
  944.         (setq default-mouse-track-extent
  945.           (list default-mouse-track-extent)))
  946.     
  947.     (setq previous-point
  948.           (if (and adjust
  949.                (markerp default-mouse-track-previous-point)
  950.                (eq (current-buffer)
  951.                (marker-buffer default-mouse-track-previous-point)))
  952.           (marker-position default-mouse-track-previous-point)
  953.         (point)))
  954.     (default-mouse-track-set-point event default-mouse-track-window)
  955.     (if (not adjust)
  956.         (if (markerp default-mouse-track-previous-point)
  957.         (set-marker default-mouse-track-previous-point (point))
  958.           (setq default-mouse-track-previous-point (point-marker))))
  959.     ;;
  960.     ;; adjust point to a word or line boundary if appropriate
  961.     (let ((anchor (default-mouse-track-anchor adjust previous-point)))
  962.       (setq default-mouse-track-min-anchor
  963.         (save-excursion (goto-char anchor)
  964.                 (default-mouse-track-normalize-point
  965.                  default-mouse-track-type nil)
  966.                 (point)))
  967.       (setq default-mouse-track-max-anchor
  968.         (save-excursion (goto-char anchor)
  969.                 (default-mouse-track-normalize-point
  970.                  default-mouse-track-type t)
  971.                 (point))))
  972.     ;;
  973.     ;; remove the existing selection to unclutter the display
  974.     (if (not adjust)
  975.         (cond (zmacs-regions
  976.            (zmacs-deactivate-region))
  977.           ((eq 'x (device-type (selected-device)))
  978.            (x-disown-selection)))))
  979.       (setq default-mouse-track-down-event nil))))
  980.  
  981. (defun default-mouse-track-down-hook (event click-count)
  982.   (setq default-mouse-track-down-event (copy-event event))
  983.   nil)
  984.  
  985. (defun default-mouse-track-cleanup-hook ()
  986.   (let ((extent default-mouse-track-extent))
  987.     (if (consp extent) ; rectangle-p
  988.     (mapcar 'delete-extent extent)
  989.       (if extent
  990.       (delete-extent extent)))))
  991.  
  992. (defun default-mouse-track-cleanup-extent ()
  993.   (let ((dead-func
  994.      (function (lambda (x)
  995.              (or (not (extent-live-p x))
  996.              (extent-detached-p x)))))
  997.     (extent default-mouse-track-extent))
  998.     (if (consp extent)
  999.     (if (some dead-func extent)
  1000.         (let (newval)
  1001.           (mapcar (function (lambda (x)
  1002.                   (if (not (funcall dead-func x))
  1003.                       (setq newval (cons x newval)))))
  1004.               extent)
  1005.           (setq default-mouse-track-extent (nreverse newval))))
  1006.       (if (funcall dead-func extent)
  1007.       (setq default-mouse-track-extent nil)))))
  1008.  
  1009. (defun default-mouse-track-drag-hook (event click-count was-timeout)
  1010.   (default-mouse-track-deal-with-down-event click-count)
  1011.   (default-mouse-track-set-point event default-mouse-track-window)
  1012.   (default-mouse-track-cleanup-extent)
  1013.   (default-mouse-track-next-move default-mouse-track-min-anchor
  1014.     default-mouse-track-max-anchor
  1015.     default-mouse-track-extent)
  1016.   t)
  1017.  
  1018. (defun default-mouse-track-return-dragged-selection (event)
  1019.   (default-mouse-track-cleanup-extent)
  1020.   (let ((extent default-mouse-track-extent)
  1021.     result)
  1022.     (default-mouse-track-set-point-in-window event default-mouse-track-window)
  1023.     (default-mouse-track-next-move default-mouse-track-min-anchor
  1024.                default-mouse-track-max-anchor
  1025.                extent)
  1026.     (cond ((consp extent) ; rectangle-p
  1027.        (let ((first (car extent))
  1028.          (last (car (setq extent (nreverse extent)))))
  1029.          ;; nreverse is destructive so we need to reset this
  1030.          (setq default-mouse-track-extent extent)
  1031.          (setq result (cons (extent-start-position first)
  1032.                 (extent-end-position last)))
  1033.          ;; kludge to fix up region when dragging backwards...
  1034.          (if (and (/= (point) (extent-start-position first))
  1035.               (/= (point) (extent-end-position last))
  1036.               (= (point) (extent-end-position first)))
  1037.          (goto-char (car result)))))
  1038.       (extent
  1039.        (setq result (cons (extent-start-position extent)
  1040.                   (extent-end-position extent)))))
  1041.     ;; Minor kludge: if we're selecting in line-mode, include the
  1042.     ;; final newline.  It's hard to do this in *-normalize-point.
  1043.     (if (and result (eq default-mouse-track-type 'line))
  1044.     (let ((end-p (= (point) (cdr result))))
  1045.       (goto-char (cdr result))
  1046.       (if (not (eobp))
  1047.           (setcdr result (1+ (cdr result))))
  1048.       (goto-char (if end-p (cdr result) (car result)))))
  1049. ;;;      ;; Minor kludge sub 2.  If in char mode, and we drag the
  1050. ;;;      ;; mouse past EOL, include the newline.
  1051. ;;;      ;;
  1052. ;;;      ;; Major problem: can't easily distinguish between being
  1053. ;;;      ;; just past the last char on a line, and well past it,
  1054. ;;;      ;; to determine whether or not to include it in the region
  1055. ;;;      ;;
  1056. ;;;      (if nil ; (eq default-mouse-track-type 'char)
  1057. ;;;          (let ((after-end-p (and (not (eobp))
  1058. ;;;                       (eolp)
  1059. ;;;                      (> (point) (car result)))))
  1060. ;;;        (if after-end-p
  1061. ;;;            (progn
  1062. ;;;              (setcdr result (1+ (cdr result)))
  1063. ;;;              (goto-char (cdr result))))))
  1064.     result))
  1065.  
  1066. (defun default-mouse-track-drag-up-hook (event click-count)
  1067.   (let ((result (default-mouse-track-return-dragged-selection event)))
  1068.     (if result
  1069.     (default-mouse-track-maybe-own-selection result 'PRIMARY)))
  1070.   t)
  1071.  
  1072. (defun default-mouse-track-click-hook (event click-count)
  1073.   (default-mouse-track-drag-hook event click-count nil)
  1074.   (default-mouse-track-drag-up-hook event click-count)
  1075.   t)
  1076.  
  1077. (add-hook 'mouse-track-down-hook 'default-mouse-track-down-hook)
  1078. (add-hook 'mouse-track-drag-hook 'default-mouse-track-drag-hook)
  1079. (add-hook 'mouse-track-drag-up-hook 'default-mouse-track-drag-up-hook)
  1080. (add-hook 'mouse-track-click-hook 'default-mouse-track-click-hook)
  1081. (add-hook 'mouse-track-cleanup-hook 'default-mouse-track-cleanup-hook)
  1082.  
  1083.  
  1084. ;;;;;;;;;;;; other mouse-track stuff (mostly associated with the
  1085. ;;;;;;;;;;;; default handlers)
  1086.  
  1087. (defun mouse-track-default (event)
  1088.   "Invoke `mouse-track' with only the default handlers active."
  1089.   (interactive "e")
  1090.   (let ((mouse-track-down-hook 'default-mouse-track-down-hook)
  1091.     (mouse-track-drag-hook 'default-mouse-track-drag-hook)
  1092.     (mouse-track-drag-up-hook 'default-mouse-track-drag-up-hook)
  1093.     (mouse-track-click-hook 'default-mouse-track-click-hook)
  1094.     (mouse-track-cleanup-hook 'default-mouse-track-cleanup-hook))
  1095.     (mouse-track event)))
  1096.  
  1097. (defun mouse-track-do-rectangle (event)
  1098.   "Like `mouse-track' but selects rectangles instead of regions."
  1099.   (interactive "e")
  1100.   (let ((mouse-track-rectangle-p t))
  1101.     (mouse-track event)))
  1102.  
  1103. (defun mouse-track-adjust (event)
  1104.   "Extend the existing selection.  This should be bound to a mouse button.
  1105. The selection will be enlarged or shrunk so that the point of the mouse
  1106. click is one of its endpoints.  This function in fact behaves fairly
  1107. similarly to `mouse-track', but begins by extending the existing selection
  1108. (or creating a new selection from the previous text cursor position to
  1109. the current mouse position) instead of creating a new, empty selection.
  1110.  
  1111. The mouse-track handlers are run from this command just like from
  1112. `mouse-track'.  Therefore, do not call this command from a mouse-track
  1113. handler!"
  1114.   (interactive "e")
  1115.   (let ((default-mouse-track-adjust t))
  1116.     (mouse-track event)))
  1117.  
  1118. (defun mouse-track-adjust-default (event)
  1119.   "Extend the existing selection, using only the default handlers.
  1120. This is just like `mouse-track-adjust' but will override any
  1121. custom mouse-track handlers that the user may have installed."
  1122.   (interactive "e")
  1123.   (let ((default-mouse-track-adjust t))
  1124.     (mouse-track-default event)))
  1125.  
  1126. (defvar mouse-track-insert-selected-region nil)
  1127.  
  1128. (defun mouse-track-insert-drag-up-hook (event click-count)
  1129.   (setq mouse-track-insert-selected-region
  1130.     (default-mouse-track-return-dragged-selection event)))
  1131.   
  1132. (defun mouse-track-insert (event &optional delete)
  1133.   "Make a selection with the mouse and insert it at point.
  1134. This is exactly the same as the `mouse-track' command on \\[mouse-track],
  1135. except that point is not moved; the selected text is immediately inserted
  1136. after being selected\; and the selection is immediately disowned afterwards."
  1137.   (interactive "*e")
  1138.   (setq mouse-track-insert-selected-region nil)
  1139.   (let ((mouse-track-drag-up-hook 'mouse-track-insert-drag-up-hook)
  1140.      (mouse-track-click-hook 'mouse-track-insert-click-hook)
  1141.     s)
  1142.     (save-excursion
  1143.       (save-window-excursion
  1144.     (mouse-track event)
  1145.     (if (consp mouse-track-insert-selected-region)
  1146.         (let ((pair mouse-track-insert-selected-region))
  1147.           (setq s (prog1
  1148.               (buffer-substring (car pair) (cdr pair))
  1149.             (if delete
  1150.                 (kill-region (car pair) (cdr pair)))))))))
  1151.     (or (null s) (equal s "") (insert s))))
  1152.  
  1153. (defun mouse-track-insert-click-hook (event click-count)
  1154.   (default-mouse-track-drag-hook event click-count nil)
  1155.   (mouse-track-insert-drag-up-hook event click-count)
  1156.   t)
  1157.  
  1158. (defun mouse-track-delete-and-insert (event)
  1159.   "Make a selection with the mouse and insert it at point.
  1160. This is exactly the same as the `mouse-track' command on \\[mouse-track],
  1161. except that point is not moved; the selected text is immediately inserted
  1162. after being selected\; and the text of the selection is deleted."
  1163.   (interactive "*e")
  1164.   (mouse-track-insert event t))
  1165.  
  1166.  
  1167. ;;; Modeline hackery
  1168.  
  1169. (defvar drag-modeline-event-lag 150
  1170.   "*The amount of time to wait (in msecs) between drag modeline events
  1171. before updating the display. If this value is too small, dragging will
  1172. be choppy because redisplay cannot keep up. If it is too large, dragging
  1173. will be choppy because of the explicit redisplay delay specified.")
  1174.  
  1175. (defvar modeline-click-swaps-buffers nil
  1176.   "*If non-nil, clicking on the modeline changes the current buffer.
  1177. Click on the left half of the modeline cycles forward through the
  1178. buffer list and clicking on the right half cycles backward.")
  1179.  
  1180. (defun mouse-drag-modeline (event)
  1181.   "Resize the window by dragging the modeline.
  1182. This should be bound to a mouse button in `modeline-map'."
  1183.   (interactive "e")
  1184.   (or (button-press-event-p event)
  1185.       (error "%s must be invoked by a mouse-press" this-command))
  1186.   (or (event-over-modeline-p event)
  1187.       (error "not over a modeline"))
  1188.   (let ((depress-line (event-y event))
  1189.     (mouse-down t)
  1190.     (window (event-window event))
  1191.     (old-window (selected-window))
  1192.     (def-line-height (face-height 'default))
  1193.     (prior-drag-modeline-event-time 0)
  1194.     delta)
  1195.     (while mouse-down
  1196.       (setq event (next-event event))
  1197.       (cond ((motion-event-p event)
  1198.          (if (window-lowest-p window)
  1199.          (error "can't drag bottommost modeline"))
  1200.          (cond ((> (- (event-timestamp event)
  1201.               prior-drag-modeline-event-time)
  1202.                drag-modeline-event-lag)
  1203.  
  1204.            (setq prior-drag-modeline-event-time (event-timestamp event))
  1205.  
  1206.            (if (event-over-modeline-p event)
  1207.            (setq delta 0)
  1208.          (setq delta (- (event-y-pixel event)
  1209.                 (nth 3 (window-pixel-edges window))))
  1210.          (if (> delta 0)
  1211.              (setq delta (+ delta def-line-height)))
  1212.          (setq delta (/ delta def-line-height)))
  1213.  
  1214.            ;; cough sputter hack kludge.  It shouldn't be possible
  1215.            ;; to get in here when we are over the minibuffer.  But
  1216.            ;; it is happening and that cause next-vertical-window to
  1217.            ;; return nil which does not lead to window-height returning
  1218.            ;; anything remotely resembling a sensible value.  So catch
  1219.            ;; the situation and die a happy death.
  1220.            ;;
  1221.            ;; Oh, and the BLAT FOOP error messages suck as well but
  1222.            ;; I don't know what should be there.  This should be
  1223.            ;; looked at again when the new redisplay is done.
  1224.            (if (not (next-vertical-window window))
  1225.            (error "Try again: dragging in minibuffer does nothing"))
  1226.            (cond ((and (> delta 0)
  1227.                (<= (- (window-height (next-vertical-window window))
  1228.                   delta)
  1229.                    window-min-height))
  1230.               (setq delta (- (window-height
  1231.                       (next-vertical-window window))
  1232.                      window-min-height))
  1233.               (if (< delta 0) (error "BLAT")))
  1234.              ((and (< delta 0)
  1235.                (< (+ (window-height window) delta)
  1236.                   window-min-height))
  1237.               (setq delta (- window-min-height
  1238.                      (window-height window)))
  1239.               (if (> delta 0) (error "FOOP"))))
  1240.            (if (= delta 0)
  1241.            nil
  1242.          (select-window window)
  1243.          (enlarge-window delta)
  1244.          ;; The call to enlarge-window may have caused the old
  1245.          ;; window to disappear.  Don't try and select it in
  1246.          ;; that case.
  1247.          (if (window-live-p old-window)
  1248.              (select-window old-window))
  1249.          (sit-for 0)
  1250.          ))))
  1251.         ((button-release-event-p event)
  1252.          (setq mouse-down nil)
  1253.          (if modeline-click-swaps-buffers
  1254.          (mouse-release-modeline event depress-line)))
  1255.         ((or (button-press-event-p event)
  1256.          (key-press-event-p event))
  1257.          (error ""))
  1258.         (t
  1259.          (dispatch-event event)))
  1260.       )))
  1261.  
  1262. ;; from Bob Weiner (bob_weiner@pts.mot.com)
  1263. (defun mouse-release-modeline (event line-num)
  1264.   "Handle modeline click EVENT on LINE-NUM by switching buffers.
  1265. If click on left half of a frame's modeline, bury current buffer.
  1266. If click on right half of a frame's modeline, raise bottommost buffer.
  1267. Args are: EVENT, the mouse release event, and LINE-NUM, the line number
  1268. within the frame at which the mouse was first depressed."
  1269.   (if (= line-num (event-y event))
  1270.       ;; Button press and release are at same line, treat this as
  1271.       ;; a click and switch buffers.
  1272.       (let ((oldwin (selected-window)))
  1273.     (select-window (event-window event))
  1274.     (if (< (event-x event) (/ (window-width) 2))
  1275.         ;; On left half of modeline, bury current buffer,
  1276.         ;; displaying second buffer on list.
  1277.         (bury-buffer)
  1278.       ;; On right half of modeline, raise and display bottommost
  1279.       ;; buffer in buffer list.
  1280.       (let* ((bufs (buffer-list))
  1281.          (entry (1- (length bufs)))
  1282.          val)
  1283.         (while (not (setq val (nth entry bufs)
  1284.                   val (and (/= (aref (buffer-name val) 0)
  1285.                        ? )
  1286.                        val)))
  1287.           (setq entry (1- entry)))
  1288.         (switch-to-buffer val)))
  1289.     (select-window oldwin))))
  1290.  
  1291. (defconst modeline-menu
  1292.   '("Window Commands"
  1293.     ["Delete Window"         delete-window            t]
  1294.     ["Delete Other Windows"     delete-other-windows        t]
  1295.     ["Split Window"         split-window-vertically    t]
  1296.     ["Split Window Horizontally" split-window-horizontally    t]
  1297.     ["Balance Windows"         balance-windows        t]
  1298.     ))
  1299.  
  1300. (defun modeline-menu (event)
  1301.   (interactive "e")
  1302.   (let* ((window (and (event-over-modeline-p event) (event-window event))))
  1303.     ;; kludge; don't select the minibuffer window...
  1304.     (if (eq window (minibuffer-window (event-frame event)))
  1305.     (setq window (previous-window window)))
  1306.     (select-window window)
  1307.     (let ((popup-menu-titles t))
  1308.       (popup-menu (cons (format "Window Commands for %S:"
  1309.                 (buffer-name (window-buffer window)))
  1310.             (cdr modeline-menu))))))
  1311.  
  1312. ;;
  1313. ;; It really sucks that we also have to tie onto x-track-pointer to
  1314. ;; make sliding buttons work right.
  1315. ;;
  1316. (defun press-toolbar-button (event)
  1317.   "Press a toolbar button.  This only changes its appearance."
  1318.   (interactive "_e")
  1319.   (setq this-command last-command)
  1320.   (let ((button (event-toolbar-button event)))
  1321.     ;; We silently ignore non-buttons.  This most likely means we are
  1322.     ;; over a blank part of the toolbar.
  1323.     (setq toolbar-active t)
  1324.     (if (toolbar-button-p button)
  1325.     (progn
  1326.       (set-toolbar-button-down-flag button t)
  1327.       (setq last-pressed-toolbar-button button)))))
  1328.  
  1329. (defun release-and-activate-toolbar-button (event)
  1330.   "Release a toolbar button and activate its callback."
  1331.   (interactive "_e")
  1332.   (or (button-release-event-p event)
  1333.       (error "%s must be invoked by a mouse-release" this-command))
  1334.   (let ((button (event-toolbar-button event)))
  1335.     (if (and (toolbar-button-p button)
  1336.          (toolbar-button-enabled-p button)
  1337.          (toolbar-button-callback button))
  1338.     (let ((callback (toolbar-button-callback button)))
  1339.       (setq this-command callback)
  1340.       (unwind-protect
  1341.           (if (symbolp callback)
  1342.           (call-interactively callback)
  1343.         (eval callback))
  1344.         (release-toolbar-button event)))
  1345.       (release-toolbar-button event))))
  1346.  
  1347. ;; If current is not t, then only release the toolbar button stored in
  1348. ;; last-pressed-toolbar-button
  1349. (defun release-toolbar-button-internal (event current)
  1350.   (let ((button (event-toolbar-button event)))
  1351.     (setq zmacs-region-stays t)
  1352.     (if (and last-pressed-toolbar-button
  1353.          (not (eq last-pressed-toolbar-button button))
  1354.          (toolbar-button-p last-pressed-toolbar-button))
  1355.     (progn
  1356.       (set-toolbar-button-down-flag last-pressed-toolbar-button nil)
  1357.       (setq last-pressed-toolbar-button nil)))
  1358.     (if (and current (toolbar-button-p button))
  1359.     (set-toolbar-button-down-flag button nil))))
  1360.  
  1361. (defun release-toolbar-button (event)
  1362.   "Release all pressed toolbar buttons."
  1363.   (interactive "_e")
  1364.   (or (button-release-event-p event)
  1365.       (error "%s must be invoked by a mouse-release" this-command))
  1366.   (release-toolbar-button-internal event t)
  1367.   ;; Don't set this-command if we're being called
  1368.   ;; from release-and-activate-toolbar-button.
  1369.   (if (interactive-p)
  1370.       (setq this-command last-command))
  1371.   (setq toolbar-active nil))
  1372.  
  1373. (defun release-previous-toolbar-button (event)
  1374.   (setq zmacs-region-stays t)
  1375.   (release-toolbar-button-internal event nil))
  1376.