home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / EmacsTeX / Emacs-3.0.1 / Source / lisp / eterm-mouse.el < prev   
Encoding:
Text File  |  1995-06-12  |  16.9 KB  |  502 lines

  1. ;; Mouse handling for NeXT "Emacs" front end.
  2. ;; Ripped off of sun-mouse.el
  3. ;; Copyright (C) 1987 Free Software Foundation, Inc.
  4.  
  5. ;; This file is NOT part of the standard GNU Emacs distribution.
  6.  
  7. ;; GNU Emacs is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  9. ;; accepts responsibility to anyone for the consequences of using it
  10. ;; or for whether it serves any particular purpose or works at all,
  11. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  12. ;; License for full details.
  13.  
  14. ;; Everyone is granted permission to copy, modify and redistribute
  15. ;; GNU Emacs, but only under the conditions described in the
  16. ;; GNU Emacs General Public License.   A copy of this license is
  17. ;; supposed to have been given to you along with GNU Emacs so you
  18. ;; can know your rights and responsibilities.  It should be in a
  19. ;; file named COPYING.  Among other things, the copyright notice
  20. ;; and this notice must be preserved on all copies.
  21.  
  22. ;;; converted from sun-mouse.el by John G. Myers, Feb 1990.
  23. ;;; sun-mouse by Jeff Peck, Sun Microsystems, Jan 1987.
  24. ;;; Original idea by Stan Jefferson
  25.  
  26. ;;(provide 'eterm-mouse)
  27.  
  28. ;;;
  29. ;;;     Modelled after the GNUEMACS keymap interface.
  30. ;;;
  31. ;;; User Functions:
  32. ;;;   make-mousemap, copy-mousemap, 
  33. ;;;   define-mouse, global-set-mouse, local-set-mouse,
  34. ;;;   use-global-mousemap, use-local-mousemap,
  35. ;;;   mouse-lookup, describe-mouse-bindings
  36. ;;;
  37. ;;; Options:
  38. ;;;   scrollbar-width
  39. ;;;
  40.  
  41. (defvar scrollbar-width 5
  42.   "*The character width of the scrollbar.
  43. The cursor is deemed to be in the right edge scrollbar if it is this near the
  44. right edge, and more than two chars past the end of the indicated line.
  45. Setting to nil limits the scrollbar to the edge or vertical dividing bar.")
  46.  
  47. ;;;
  48. ;;; Mousemaps
  49. ;;;
  50. (defun make-mousemap ()
  51.   "Returns a new mousemap."
  52.   (cons 'mousemap nil))
  53.  
  54. (defun copy-mousemap (mousemap)
  55.   "Return a copy of mousemap."
  56.   (copy-alist mousemap))
  57.  
  58. (defun define-mouse (mousemap mouse-list def)
  59.   "Args MOUSEMAP, MOUSE-LIST, DEF.  Define MOUSE-LIST in MOUSEMAP as DEF.
  60. MOUSE-LIST is a list of atoms specifing a mouse hit according to these rules:
  61.   * One of these atoms specifies the active region of the definition.
  62.     text, scrollbar, modeline, minibuffer
  63.   * One or two or these atoms specify the button or button combination.
  64.         left, middle, right, double
  65.   * Any combination of these atoms specify the active shift keys.
  66.         control, shift, meta
  67.   * With a single unshifted button, you can add
  68.     up
  69.     to indicate an up-click.
  70. The atom `double' is used with a button designator to denote a double click.
  71. Two button chords are denoted by listing the two buttons.
  72. See eterm-mouse-handler for the treatment of the form DEF."
  73.   (mousemap-set (mouse-list-to-mouse-code mouse-list) mousemap def))
  74.  
  75. (defun global-set-mouse (mouse-list def)
  76.   "Give MOUSE-EVENT-LIST a local definition of DEF.
  77. See define-mouse for a description of MOUSE-EVENT-LIST and DEF.
  78. Note that if MOUSE-EVENT-LIST has a local definition in the current buffer,
  79. that local definition will continue to shadow any global definition."
  80.   (interactive "xMouse event: \nxDefinition: ")
  81.   (define-mouse current-global-mousemap mouse-list def))
  82.  
  83. (defun local-set-mouse (mouse-list def)
  84.   "Give MOUSE-EVENT-LIST a local definition of DEF.
  85. See define-mouse for a description of the arguments.
  86. The definition goes in the current buffer's local mousemap.
  87. Normally buffers in the same major mode share a local mousemap."
  88.   (interactive "xMouse event: \nxDefinition: ")
  89.   (if (null current-local-mousemap)
  90.       (setq current-local-mousemap (make-mousemap)))
  91.   (define-mouse current-local-mousemap mouse-list def))
  92.  
  93. (defun use-global-mousemap (mousemap)
  94.   "Selects MOUSEMAP as the global mousemap."
  95.   (setq current-global-mousemap mousemap))
  96.  
  97. (defun use-local-mousemap (mousemap)
  98.   "Selects MOUSEMAP as the local mousemap.
  99. nil for MOUSEMAP means no local mousemap."
  100.   (setq current-local-mousemap mousemap))
  101.  
  102.  
  103. ;;;
  104. ;;; Interface to the Mouse encoding defined in Emacstool.c
  105. ;;;
  106. ;;; Called when mouse-prefix is sent to emacs, additional
  107. ;;; information is read in as a list (button x y time-delta)
  108. ;;;
  109. ;;; First, some generally useful functions:
  110. ;;;
  111.  
  112. (defun logtest (x y)
  113.   "True if any bits set in X are also set in Y.
  114. Just like the Common Lisp function of the same name."
  115.   (not (zerop (logand x y))))
  116.  
  117.  
  118. ;;;
  119. ;;; Hit accessors.
  120. ;;;
  121.  
  122. (defconst em::ButtonBits 7)        ; Lowest 3 bits.
  123. (defconst em::ShiftmaskBits 56)        ; Second lowest 3 bits (56 = 63 - 7).
  124. (defconst em::DoubleBits 64)        ; Bit 7.
  125. (defconst em::UpBits 128)        ; Bit 8.
  126.  
  127. ;;; All the useful code bits
  128. (defmacro em::hit-code (hit)
  129.   (` (nth 0 (, hit))))
  130. ;;; The button, or buttons if a chord.
  131. (defmacro em::hit-button (hit)
  132.   (` (logand em::ButtonBits (nth 0 (, hit)))))
  133. ;;; The shift, control, and meta flags.
  134. (defmacro em::hit-shiftmask (hit)
  135.   (` (logand em::ShiftmaskBits (nth 0 (, hit)))))
  136. ;;; Set if a double click (but not a chord).
  137. (defmacro em::hit-double (hit)
  138.   (` (logand em::DoubleBits (nth 0 (, hit)))))
  139. ;;; Set on button release (as opposed to button press).
  140. (defmacro em::hit-up (hit)
  141.   (` (logand em::UpBits (nth 0 (, hit)))))
  142. ;;; Screen x position.
  143. (defmacro em::hit-x (hit) (list 'nth 1 hit))
  144. ;;; Screen y position.
  145. (defmacro em::hit-y (hit) (list 'nth 2 hit))
  146. ;;; Millisconds since last hit.
  147. (defmacro em::hit-delta (hit) (list 'nth 3 hit))
  148.  
  149. (defmacro em::hit-up-p (hit)        ; A predicate.
  150.   (` (not (zerop (em::hit-up (, hit))))))
  151.  
  152. ;;;
  153. ;;; Loc accessors.  for em::window-xy
  154. ;;;
  155. (defmacro em::loc-w (loc) (list 'nth 0 loc))
  156. (defmacro em::loc-x (loc) (list 'nth 1 loc))
  157. (defmacro em::loc-y (loc) (list 'nth 2 loc))
  158.  
  159. (defmacro eval-in-buffer (buffer &rest forms)
  160.   "Macro to switches to BUFFER, evaluates FORMS, returns to original buffer."
  161.   ;; When you don't need the complete window context of eval-in-window
  162.   (` (let ((StartBuffer (current-buffer)))
  163.     (unwind-protect
  164.     (progn
  165.       (set-buffer (, buffer))
  166.       (,@ forms))
  167.     (set-buffer StartBuffer)))))
  168.  
  169. (put 'eval-in-buffer 'lisp-indent-hook 1)
  170.  
  171. ;;; this is used extensively by sun-fns.el
  172. ;;;
  173. (defmacro eval-in-window (window &rest forms)
  174.   "Switch to WINDOW, evaluate FORMS, return to original window."
  175.   (` (let ((OriginallySelectedWindow (selected-window)))
  176.        (unwind-protect
  177.        (progn
  178.          (select-window (, window))
  179.          (,@ forms))
  180.      (select-window OriginallySelectedWindow)))))
  181. (put 'eval-in-window 'lisp-indent-hook 1)
  182.  
  183. ;;;
  184. ;;; handy utility, generalizes window_loop
  185. ;;;
  186.  
  187. ;;; It's a macro (and does not evaluate its arguments).
  188. (defmacro eval-in-windows (form &optional yesmini)
  189.   "Switches to each window and evaluates FORM.  Optional argument
  190. YESMINI says to include the minibuffer as a window.
  191. This is a macro, and does not evaluate its arguments."
  192.   (` (let ((OriginallySelectedWindow (selected-window)))
  193.        (unwind-protect 
  194.        (while (progn
  195.             (, form)
  196.             (not (eq OriginallySelectedWindow
  197.                  (select-window
  198.                   (next-window nil (, yesmini)))))))
  199.      (select-window OriginallySelectedWindow)))))
  200. (put 'eval-in-window 'lisp-indent-hook 0)
  201.  
  202. (defun move-to-loc (x y)
  203.   "Move cursor to window location X, Y.
  204. Handles wrapped and horizontally scrolled lines correctly."
  205.   (move-to-window-line y)
  206.   ;; window-line-end expects this to return the window column it moved to.
  207.   (let ((cc (current-column))
  208.     (nc (move-to-column
  209.          (if (zerop (window-hscroll))
  210.          (+ (current-column)
  211.             (min (- (window-width) 2)    ; To stay on the line.
  212.              x))
  213.            (+ (window-hscroll) -1
  214.           (min (1- (window-width))    ; To stay on the line.
  215.                x))))))
  216.     (- nc cc)))
  217.  
  218.  
  219. (defun minibuffer-window-p (window)
  220.   "True iff this WINDOW is minibuffer."
  221.   (= (screen-height)
  222.      (nth 3 (window-edges window))    ; The bottom edge.
  223.      ))
  224.  
  225.  
  226. (defun eterm-mouse-handler (&optional hit)
  227.   "Evaluates the function or list associated with a mouse hit.
  228. Expecting to read a hit, which is a list: (button x y unused).  
  229. A form bound to button by define-mouse is found by mouse-lookup. 
  230. The variables: *mouse-window*, *mouse-x*, *mouse-y* are bound.  
  231. If the form is a symbol (symbolp), it is funcall'ed with *mouse-window*,
  232. *mouse-x*, and *mouse-y* as arguments; if the form is a list (listp),
  233. the form is eval'ed; if the form is neither of these, it is an error.
  234. Returns nil."
  235.   (interactive)
  236.   (if (null hit) (setq hit (mouse-hit-read)))
  237.   (let ((loc (em::window-xy (em::hit-x hit) (em::hit-y hit))))
  238.     (let ((*mouse-window* (em::loc-w loc))
  239.       (*mouse-x* (em::loc-x loc))
  240.       (*mouse-y* (em::loc-y loc))
  241.       (mouse-code (mouse-event-code hit loc)))
  242.       (let ((form (eval-in-buffer (window-buffer *mouse-window*)
  243.             (mouse-lookup mouse-code))))
  244.     (cond ((null form)
  245.            (if (not (em::hit-up-p hit))    ; undefined up hits are ok.
  246.            (error "Undefined mouse event: %s" 
  247.               (prin1-to-string 
  248.                (mouse-code-to-mouse-list mouse-code)))))
  249.           ((symbolp form)
  250.            (setq this-command form)
  251.            (funcall form *mouse-window* *mouse-x* *mouse-y*))
  252.           ((listp form)
  253.            (setq this-command (car form))
  254.            (eval form))
  255.           (t
  256.            (error "Mouse action must be symbol or list, but was: %s"
  257.               form))))))
  258.   ;; Don't let 'eterm-mouse-handler get on last-command,
  259.   ;; since this function should be transparent.
  260.   (if (eq this-command 'eterm-mouse-handler)
  261.       (setq this-command last-command))
  262.   ;; (message (prin1-to-string this-command))    ; to see what your buttons did
  263.   nil)
  264.  
  265. (defun mouse-hit-read ()
  266.   "Read mouse-hit list from keyboard.  Like (read 'read-char),
  267. but that uses minibuffer, and mucks up last-command."
  268.   (let ((char-list nil) (char nil))
  269.     (while (not (equal 13        ; Carriage return.
  270.                (prog1 (setq char (read-char)) 
  271.              (setq char-list (cons char char-list))))))
  272.     (read (mapconcat 'char-to-string (nreverse char-list) ""))
  273.     ))
  274.  
  275.  
  276. (defun em::window-xy (x y)
  277.   "Find window containing screen coordinates X and Y.
  278. Returns list (window x y) where x and y are relative to window."
  279.   (or
  280.    (catch 'found
  281.      (eval-in-windows 
  282.       (let ((we (window-edges (selected-window))))
  283.     (let ((le (nth 0 we))
  284.           (te (nth 1 we))
  285.           (re (nth 2 we))
  286.           (be (nth 3 we)))
  287.       (if (= re (screen-width))
  288.           ;; include the continuation column with this window
  289.           (setq re (1+ re)))
  290.       (if (= be (screen-height))
  291.           ;; include partial line at bottom of screen with this window
  292.           ;; id est, if window is not multple of char size.
  293.           (setq be (1+ be)))
  294.  
  295.       (if (and (>= x le) (< x re)
  296.            (>= y te) (< y be))
  297.           (throw 'found 
  298.              (list (selected-window) (- x le) (- y te))))))
  299.       t))                ; include minibuffer in eval-in-windows
  300.    ;;If x,y from a real mouse click, we shouldn't get here.
  301.    (list nil x y)
  302.    ))
  303.  
  304. (defun em::window-region (loc)
  305.   "Parse LOC into a region symbol.
  306. Returns one of (text scrollbar modeline minibuffer)"
  307.   (let ((w (em::loc-w loc))
  308.     (x (em::loc-x loc))
  309.     (y (em::loc-y loc)))
  310.     (let ((right (1- (window-width w)))
  311.       (bottom (1- (window-height w))))
  312.       (cond ((minibuffer-window-p w) 'minibuffer)
  313.         ((>= y bottom) 'modeline)
  314.         ((>= x right) 'scrollbar)
  315.         ;; far right column (window seperator) is always a scrollbar
  316.         ((and scrollbar-width
  317.           ;; mouse within scrollbar-width of edge.
  318.           (>= x (- right scrollbar-width))
  319.           ;; mouse a few chars past the end of line.
  320.           (>= x (+ 2 (window-line-end w x y))))
  321.          'scrollbar)
  322.         (t 'text)))))
  323.  
  324. (defun window-line-end (w x y)
  325.   "Return WINDOW column (ignore X) containing end of line Y"
  326.   (eval-in-window w (save-excursion (move-to-loc (screen-width) y))))
  327.  
  328. ;;;
  329. ;;; The encoding of mouse events into a mousemap.
  330. ;;; These values must agree with coding in emacstool:
  331. ;;;
  332. (defconst em::keyword-alist 
  333.   '((left . 1) (middle . 2) (right . 4)
  334.     (shift . 8) (control . 16) (meta . 32) (double . 64) (up . 128)
  335.     (text . 256) (scrollbar . 512) (modeline . 1024) (minibuffer . 2048)
  336.     ))
  337.  
  338. (defun mouse-event-code (hit loc)
  339.   "Maps MOUSE-HIT and LOC into a mouse-code."
  340. ;;;Region is a code for one of text, modeline, scrollbar, or minibuffer.
  341.   (logior (em::hit-code hit)
  342.       (mouse-region-to-code (em::window-region loc))))
  343.  
  344. (defun mouse-region-to-code (region)
  345.   "Returns partial mouse-code for specified REGION."
  346.   (cdr (assq region em::keyword-alist)))
  347.  
  348. (defun mouse-list-to-mouse-code (mouse-list)
  349.   "Map a MOUSE-LIST to a mouse-code."
  350.   (apply 'logior
  351.      (mapcar (function (lambda (x)
  352.                  (cdr (assq x em::keyword-alist))))
  353.           mouse-list)))
  354.  
  355. (defun mouse-code-to-mouse-list (mouse-code)
  356.   "Map a MOUSE-CODE to a mouse-list."
  357.   (apply 'nconc (mapcar
  358.          (function (lambda (x)
  359.                  (if (logtest mouse-code (cdr x))
  360.                  (list (car x)))))
  361.          em::keyword-alist)))
  362.  
  363. (defun mousemap-set (code mousemap value)
  364.   (let* ((alist (cdr mousemap))
  365.      (assq-result (assq code alist)))
  366.     (if assq-result
  367.     (setcdr assq-result value)
  368.       (setcdr mousemap (cons (cons code value) alist)))))
  369.  
  370. (defun mousemap-get (code mousemap)
  371.   (cdr (assq code (cdr mousemap))))
  372.  
  373. (defun mouse-lookup (mouse-code)
  374.   "Look up MOUSE-EVENT and return the definition. nil means undefined."
  375.   (or (mousemap-get mouse-code current-local-mousemap)
  376.       (mousemap-get mouse-code current-global-mousemap)))
  377.  
  378. ;;;
  379. ;;; I (jpeck) don't understand the utility of the next four functions
  380. ;;; ask Steven Greenbaum <froud@kestrel>
  381. ;;;
  382. (defun mouse-mask-lookup (mask list)
  383.   "Args MASK (a bit mask) and LIST (a list of (code . form) pairs).
  384. Returns a list of elements of LIST whose code or'ed with MASK is non-zero."
  385.   (let ((result nil))
  386.     (while list
  387.       (if (logtest mask (car (car list)))
  388.       (setq result (cons (car list) result)))
  389.       (setq list (cdr list)))
  390.     result))
  391.  
  392. (defun mouse-union (l l-unique)
  393.   "Return the union of list of mouse (code . form) pairs L and L-UNIQUE,
  394. where L-UNIQUE is considered to be union'ized already."
  395.   (let ((result l-unique))
  396.     (while l
  397.       (let ((code-form-pair (car l)))
  398.     (if (not (assq (car code-form-pair) result))
  399.         (setq result (cons code-form-pair result))))
  400.       (setq l (cdr l)))
  401.     result))
  402.  
  403. (defun mouse-union-first-prefered (l1 l2)
  404.   "Return the union of lists of mouse (code . form) pairs L1 and L2,
  405. based on the code's, with preference going to elements in L1."
  406.   (mouse-union l2 (mouse-union l1 nil)))
  407.  
  408. (defun mouse-code-function-pairs-of-region (region)
  409.   "Return a list of (code . function) pairs, where each code is
  410. currently set in the REGION."
  411.   (let ((mask (mouse-region-to-code region)))
  412.     (mouse-union-first-prefered
  413.      (mouse-mask-lookup mask (cdr current-local-mousemap))
  414.      (mouse-mask-lookup mask (cdr current-global-mousemap))
  415.      )))
  416.  
  417. ;;;
  418. ;;; Functions for DESCRIBE-MOUSE-BINDINGS
  419. ;;; And other mouse documentation functions
  420. ;;; Still need a good procedure to print out a help sheet in readable format.
  421. ;;;
  422.  
  423. (defun one-line-doc-string (function)
  424.   "Returns first line of documentation string for FUNCTION.
  425. If there is no documentation string, then the string
  426. \"No documentation\" is returned."
  427.   (while (consp function) (setq function (car function)))
  428.   (let ((doc (documentation function)))
  429.     (if (null doc)
  430.     "No documentation."
  431.       (string-match "^.*$" doc)
  432.       (substring doc 0 (match-end 0)))))
  433.  
  434. (defun print-mouse-format (binding)
  435.   (princ (car binding))
  436.   (princ ": ")
  437.   (mapcar (function
  438.        (lambda (mouse-list)
  439.          (princ mouse-list)
  440.          (princ " ")))
  441.       (cdr binding))
  442.   (terpri)
  443.   (princ "  ")
  444.   (princ (one-line-doc-string (car binding)))
  445.   (terpri)
  446.   )
  447.  
  448. (defun print-mouse-bindings (region)
  449.   "Prints mouse-event bindings for REGION."
  450.   (mapcar 'print-mouse-format (em::event-bindings region)))
  451.  
  452. (defun em::event-bindings (region)
  453.   "Returns an alist of (function . (mouse-list1 ... mouse-listN)) for REGION,
  454. where each mouse-list is bound to the function in REGION."
  455.   (let ((mouse-bindings (mouse-code-function-pairs-of-region region))
  456.     (result nil))
  457.     (while mouse-bindings
  458.       (let* ((code-function-pair (car mouse-bindings))
  459.          (current-entry (assoc (cdr code-function-pair) result)))
  460.     (if current-entry
  461.         (setcdr current-entry
  462.             (cons (mouse-code-to-mouse-list (car code-function-pair))
  463.               (cdr current-entry)))
  464.       (setq result (cons (cons (cdr code-function-pair)
  465.                    (list (mouse-code-to-mouse-list
  466.                       (car code-function-pair))))
  467.                  result))))
  468.       (setq mouse-bindings (cdr mouse-bindings))
  469.       )
  470.     result))
  471.  
  472. (defun describe-mouse-bindings ()
  473.   "Lists all current mouse-event bindings."
  474.   (interactive)
  475.   (with-output-to-temp-buffer "*Help*"
  476.     (princ "Text Region") (terpri)
  477.     (princ "---- ------") (terpri)
  478.     (print-mouse-bindings 'text) (terpri)
  479.     (princ "Modeline Region") (terpri)
  480.     (princ "-------- ------") (terpri)
  481.     (print-mouse-bindings 'modeline) (terpri)
  482.     (princ "Scrollbar Region") (terpri)
  483.     (princ "--------- ------") (terpri)
  484.     (print-mouse-bindings 'scrollbar)))
  485.  
  486. (defun describe-mouse-briefly (mouse-list)
  487.   "Print a short description of the function bound to MOUSE-LIST."
  488.   (interactive "xDescibe mouse list briefly: ")
  489.   (let ((function (mouse-lookup (mouse-list-to-mouse-code mouse-list))))
  490.     (if function
  491.     (message "%s runs the command %s" mouse-list function)
  492.       (message "%s is undefined" mouse-list))))
  493.  
  494.  
  495. ;;;
  496. ;;; initialize mouse maps
  497. ;;;
  498.  
  499. (make-variable-buffer-local 'current-local-mousemap)
  500. (setq-default current-local-mousemap nil)
  501. (defvar current-global-mousemap (make-mousemap))
  502.