Debug message fix
[sxemacs] / lisp / term / sun-mouse.el
1 ;;; sun-mouse.el --- mouse handling for Sun windows
2
3 ;; Copyright (C) 1987, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Jeff Peck
6 ;; Maintainer: FSF
7 ;; Keywords: hardware
8
9 ;; This file is part of SXEmacs.
10
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; SXEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Synched up with:  Unknown
25
26 ;;; Commentary:
27
28 ;; Jeff Peck, Sun Microsystems, Jan 1987.
29 ;; Original idea by Stan Jefferson
30
31 ;;
32 ;;     Modelled after the GNUEMACS keymap interface.
33 ;;
34 ;; User Functions:
35 ;;   make-mousemap, copy-mousemap,
36 ;;   define-mouse, global-set-mouse, local-set-mouse,
37 ;;   use-global-mousemap, use-local-mousemap,
38 ;;   mouse-lookup, describe-mouse-bindings
39 ;;
40 ;; Options:
41 ;;   extra-click-wait, scrollbar-width
42 ;;
43
44 ;;; Code:
45 (eval-when-compile
46   (globally-declare-boundp
47    '(current-global-mousemap current-local-mousemap))
48   (globally-declare-fboundp
49    '(window-edges sit-for-millisecs sun-menu-internal sun-get-selection sun-set-selection)))
50
51 (defvar extra-click-wait 150
52   "*Number of milliseconds to wait for an extra click.
53 Set this to zero if you don't want chords or double clicks.")
54
55 (defvar scrollbar-width 5
56   "*The character width of the scrollbar.
57 The cursor is deemed to be in the right edge scrollbar if it is this near the
58 right edge, and more than two chars past the end of the indicated line.
59 Setting to nil limits the scrollbar to the edge or vertical dividing bar.")
60 \f
61 ;;;
62 ;;; Mousemaps
63 ;;;
64 (defun make-mousemap ()
65   "Returns a new mousemap."
66   (cons 'mousemap nil))
67
68 (defun copy-mousemap (mousemap)
69   "Return a copy of mousemap."
70   (copy-alist mousemap))
71
72 (defun define-mouse (mousemap mouse-list def)
73   "Args MOUSEMAP, MOUSE-LIST, DEF.  Define MOUSE-LIST in MOUSEMAP as DEF.
74 MOUSE-LIST is a list of atoms specifying a mouse hit according to these rules:
75   * One of these atoms specifies the active region of the definition.
76         text, scrollbar, modeline, minibuffer
77   * One or two or these atoms specify the button or button combination.
78         left, middle, right, double
79   * Any combination of these atoms specify the active shift keys.
80         control, shift, meta
81   * With a single unshifted button, you can add
82         up
83     to indicate an up-click.
84 The atom `double' is used with a button designator to denote a double click.
85 Two button chords are denoted by listing the two buttons.
86 See sun-mouse-handler for the treatment of the form DEF."
87   (mousemap-set (mouse-list-to-mouse-code mouse-list) mousemap def))
88
89 (defun global-set-mouse (mouse-list def)
90   "Give MOUSE-EVENT-LIST a local definition of DEF.
91 See define-mouse for a description of MOUSE-EVENT-LIST and DEF.
92 Note that if MOUSE-EVENT-LIST has a local definition in the current buffer,
93 that local definition will continue to shadow any global definition."
94   (interactive "xMouse event: \nxDefinition: ")
95   (define-mouse current-global-mousemap mouse-list def))
96
97 (defun local-set-mouse (mouse-list def)
98   "Give MOUSE-EVENT-LIST a local definition of DEF.
99 See define-mouse for a description of the arguments.
100 The definition goes in the current buffer's local mousemap.
101 Normally buffers in the same major mode share a local mousemap."
102   (interactive "xMouse event: \nxDefinition: ")
103   (if (null current-local-mousemap)
104       (setq current-local-mousemap (make-mousemap)))
105   (define-mouse current-local-mousemap mouse-list def))
106
107 (defun use-global-mousemap (mousemap)
108   "Selects MOUSEMAP as the global mousemap."
109   (setq current-global-mousemap mousemap))
110
111 (defun use-local-mousemap (mousemap)
112   "Selects MOUSEMAP as the local mousemap.
113 nil for MOUSEMAP means no local mousemap."
114   (setq current-local-mousemap mousemap))
115
116 \f
117 ;;;
118 ;;; Interface to the Mouse encoding defined in Emacstool.c
119 ;;;
120 ;;; Called when mouse-prefix is sent to emacs, additional
121 ;;; information is read in as a list (button x y time-delta)
122 ;;;
123 ;;; First, some generally useful functions:
124 ;;;
125
126 (defun logtest (x y)
127   "True if any bits set in X are also set in Y.
128 Just like the Common Lisp function of the same name."
129   (not (zerop (logand x y))))
130
131
132 ;;;
133 ;;; Hit accessors.
134 ;;;
135
136 (defconst sm::ButtonBits 7)             ; Lowest 3 bits.
137 (defconst sm::ShiftmaskBits 56)         ; Second lowest 3 bits (56 = 63 - 7).
138 (defconst sm::DoubleBits 64)            ; Bit 7.
139 (defconst sm::UpBits 128)               ; Bit 8.
140
141 ;;; All the useful code bits
142 (defmacro sm::hit-code (hit)
143   `(nth 0 ,hit))
144 ;;; The button, or buttons if a chord.
145 (defmacro sm::hit-button (hit)
146   `(logand sm::ButtonBits (nth 0 ,hit)))
147 ;;; The shift, control, and meta flags.
148 (defmacro sm::hit-shiftmask (hit)
149   `(logand sm::ShiftmaskBits (nth 0 ,hit)))
150 ;;; Set if a double click (but not a chord).
151 (defmacro sm::hit-double (hit)
152   `(logand sm::DoubleBits (nth 0 ,hit)))
153 ;;; Set on button release (as opposed to button press).
154 (defmacro sm::hit-up (hit)
155   `(logand sm::UpBits (nth 0 ,hit)))
156 ;;; Screen x position.
157 (defmacro sm::hit-x (hit) `(nth 1 ,hit))
158 ;;; Screen y position.
159 (defmacro sm::hit-y (hit) `(nth 2 ,hit))
160 ;;; Milliseconds since last hit.
161 (defmacro sm::hit-delta (hit) `(nth 3 ,hit))
162
163 (defmacro sm::hit-up-p (hit)            ; A predicate.
164   `(not (zerop (sm::hit-up ,hit))))
165
166 ;;;
167 ;;; Loc accessors.  for sm::window-xy
168 ;;;
169 (defmacro sm::loc-w (loc) `(nth 0 ,loc))
170 (defmacro sm::loc-x (loc) `(nth 1 ,loc))
171 (defmacro sm::loc-y (loc) `(nth 2 ,loc))
172
173 ;;; this is used extensively by sun-fns.el
174 ;;;
175 (defmacro eval-in-window (window &rest forms)
176   "Switch to WINDOW, evaluate FORMS, return to original window."
177   `(let ((OriginallySelectedWindow (selected-window)))
178      (unwind-protect
179          (progn
180            (select-window ,window)
181            ,@forms)
182        (select-window OriginallySelectedWindow))))
183 (put 'eval-in-window 'lisp-indent-function 1)
184
185 ;;;
186 ;;; handy utility, generalizes window_loop
187 ;;;
188
189 ;;; It's a macro (and does not evaluate its arguments).
190 (defmacro eval-in-windows (form &optional yesmini)
191   "Switches to each window and evaluates FORM.  Optional argument
192 YESMINI says to include the minibuffer as a window.
193 This is a macro, and does not evaluate its arguments."
194   `(let ((OriginallySelectedWindow (selected-window)))
195      (unwind-protect
196          (while (progn
197                   ,form
198                   (not (eq OriginallySelectedWindow
199                            (select-window
200                             (next-window nil ,yesmini))))))
201        (select-window OriginallySelectedWindow))))
202 (put 'eval-in-window 'lisp-indent-function 0)
203
204 (defun move-to-loc (x y)
205   "Move cursor to window location X, Y.
206 Handles wrapped and horizontally scrolled lines correctly."
207   (move-to-window-line y)
208   ;; window-line-end expects this to return the window column it moved to.
209   (let ((cc (current-column))
210         (nc (move-to-column
211              (if (zerop (window-hscroll))
212                  (+ (current-column)
213                     (min (- (window-width) 2)   ; To stay on the line.
214                          x))
215                (+ (window-hscroll) -1
216                   (min (1- (window-width))      ; To stay on the line.
217                        x))))))
218     (- nc cc)))
219
220
221 (defun minibuffer-window-p (window)
222   "Return t if this WINDOW is a minibuffer."
223   (= (frame-height)
224      (nth 3 (window-edges window))      ; The bottom edge.
225      ))
226
227 \f
228 (defun sun-mouse-handler (&optional hit)
229   "Evaluates the function or list associated with a mouse hit.
230 Expecting to read a hit, which is a list: (button x y delta).
231 A form bound to button by define-mouse is found by mouse-lookup.
232 The variables: *mouse-window*, *mouse-x*, *mouse-y* are bound.
233 If the form is a symbol (symbolp), it is funcall'ed with *mouse-window*,
234 *mouse-x*, and *mouse-y* as arguments; if the form is a list (listp),
235 the form is eval'ed; if the form is neither of these, it is an error.
236 Returns nil."
237   (interactive)
238   (if (null hit) (setq hit (sm::combined-hits)))
239   (let ((loc (sm::window-xy (sm::hit-x hit) (sm::hit-y hit))))
240     (let ((*mouse-window* (sm::loc-w loc))
241           (*mouse-x* (sm::loc-x loc))
242           (*mouse-y* (sm::loc-y loc))
243           (mouse-code (mouse-event-code hit loc)))
244       (let ((form (with-current-buffer (window-buffer *mouse-window*)
245                     (mouse-lookup mouse-code))))
246         (cond ((null form)
247                (if (not (sm::hit-up-p hit))     ; undefined up hits are ok.
248                    (error "Undefined mouse event: %s"
249                           (prin1-to-string
250                            (mouse-code-to-mouse-list mouse-code)))))
251               ((symbolp form)
252                (setq this-command form)
253                (funcall form *mouse-window* *mouse-x* *mouse-y*))
254               ((listp form)
255                (setq this-command (car form))
256                (eval form))
257               (t
258                (error "Mouse action must be symbol or list, but was: %s"
259                       form))))))
260   ;; Don't let 'sun-mouse-handler get on last-command,
261   ;; since this function should be transparent.
262   (if (eq this-command 'sun-mouse-handler)
263       (setq this-command last-command))
264   ;; (message (prin1-to-string this-command))   ; to see what your buttons did
265   nil)
266
267 (defun sm::combined-hits ()
268   "Read and return next mouse-hit, include possible double click"
269   (let ((hit1 (mouse-hit-read)))
270     (if (not (sm::hit-up-p hit1))       ; Up hits dont start doubles or chords.
271         (let ((hit2 (mouse-second-hit extra-click-wait)))
272           (if hit2      ; we cons'd it, we can smash it.
273               ; (setf (sm::hit-code hit1) (logior (sm::hit-code hit1) ...))
274               (setcar hit1 (logior (sm::hit-code hit1)
275                                    (sm::hit-code hit2)
276                                    (if (= (sm::hit-button hit1)
277                                           (sm::hit-button hit2))
278                                        sm::DoubleBits 0))))))
279     hit1))
280
281 (defun mouse-hit-read ()
282   "Read mouse-hit list from keyboard.  Like (read 'read-char),
283 but that uses minibuffer, and mucks up last-command."
284   (let ((char-list nil) (char nil))
285     (while (not (equal 13               ; Carriage return.
286                        (prog1 (setq char (read-char))
287                          (setq char-list (cons char char-list))))))
288     (read (mapconcat 'char-to-string (nreverse char-list) ""))
289     ))
290
291 ;;; Second Click Hackery....
292 ;;; if prefix is not mouse-prefix, need a way to unread the char...
293 ;;; or else have mouse flush input queue, or else need a peek at next char.
294
295 ;;; There is no peek, but since one character can be unread, we only
296 ;;; have to flush the queue when the command after a mouse click
297 ;;; starts with mouse-prefix1 (see below).
298 ;;;   Something to do later:  We could buffer the read commands and
299 ;;; execute them ourselves after doing the mouse command (using
300 ;;; lookup-key ??).
301
302 (defvar mouse-prefix1 24                ; C-x
303   "First char of mouse-prefix.  Used to detect double clicks and chords.")
304
305 (defvar mouse-prefix2 0                 ; C-@
306   "Second char of mouse-prefix.  Used to detect double clicks and chords.")
307
308
309 (defun mouse-second-hit (hit-wait)
310   "Returns the next mouse hit occurring within HIT-WAIT milliseconds."
311   (if (sit-for-millisecs hit-wait) nil  ; No input within hit-wait millisecs.
312     (let ((pc1 (read-char)))
313       (if (or (not (equal pc1 mouse-prefix1))
314               (sit-for-millisecs 3))    ; a mouse prefix will have second char
315           ;; Can get away with one unread.
316           (progn (setq unread-command-events (list pc1))
317                  nil)                   ; Next input not mouse event.
318         (let ((pc2 (read-char)))
319           (if (not (equal pc2 mouse-prefix2))
320               (progn (setq unread-command-events (list pc1)) ; put back the ^X
321 ;;; Too bad can't do two: (setq unread-command-event (list pc1 pc2))
322 ;;; Well, now we can, but I don't understand this code well enough to fix it...
323                 (ding)                  ; user will have to retype that pc2.
324                 nil)                    ; This input is not a mouse event.
325             ;; Next input has mouse prefix and is within time limit.
326             (let ((new-hit (mouse-hit-read))) ; Read the new hit.
327                 (if (sm::hit-up-p new-hit)      ; Ignore up events when timing.
328                     (mouse-second-hit (- hit-wait (sm::hit-delta new-hit)))
329                   new-hit               ; New down hit within limit, return it.
330                   ))))))))
331 \f
332 (defun sm::window-xy (x y)
333   "Find window containing screen coordinates X and Y.
334 Returns list (window x y) where x and y are relative to window."
335   (or
336    (catch 'found
337      (eval-in-windows
338       (let ((we (window-edges (selected-window))))
339         (let ((le (nth 0 we))
340               (te (nth 1 we))
341               (re (nth 2 we))
342               (be (nth 3 we)))
343           (if (= re (frame-width))
344               ;; include the continuation column with this window
345               (setq re (1+ re)))
346           (if (= be (frame-height))
347               ;; include partial line at bottom of frame with this window
348               ;; id est, if window is not multple of char size.
349               (setq be (1+ be)))
350
351           (if (and (>= x le) (< x re)
352                    (>= y te) (< y be))
353               (throw 'found
354                      (list (selected-window) (- x le) (- y te))))))
355       t))                               ; include minibuffer in eval-in-windows
356    ;;If x,y from a real mouse click, we shouldn't get here.
357    (list nil x y)
358    ))
359
360 (defun sm::window-region (loc)
361   "Parse LOC into a region symbol.
362 Returns one of (text scrollbar modeline minibuffer)"
363   (let ((w (sm::loc-w loc))
364         (x (sm::loc-x loc))
365         (y (sm::loc-y loc)))
366     (let ((right (1- (window-width w)))
367           (bottom (1- (window-height w))))
368       (cond ((minibuffer-window-p w) 'minibuffer)
369             ((>= y bottom) 'modeline)
370             ((>= x right) 'scrollbar)
371             ;; far right column (window separator) is always a scrollbar
372             ((and scrollbar-width
373                   ;; mouse within scrollbar-width of edge.
374                   (>= x (- right scrollbar-width))
375                   ;; mouse a few chars past the end of line.
376                   (>= x (+ 2 (window-line-end w x y))))
377              'scrollbar)
378             (t 'text)))))
379
380 (defun window-line-end (w x y)
381   "Return WINDOW column (ignore X) containing end of line Y."
382   (eval-in-window w (save-excursion (move-to-loc (frame-width) y))))
383 \f
384 ;;;
385 ;;; The encoding of mouse events into a mousemap.
386 ;;; These values must agree with coding in emacstool:
387 ;;;
388 (defconst sm::keyword-alist
389   '((left . 1) (middle . 2) (right . 4)
390     (shift . 8) (control . 16) (meta . 32) (double . 64) (up . 128)
391     (text . 256) (scrollbar . 512) (modeline . 1024) (minibuffer . 2048)
392     ))
393
394 (defun mouse-event-code (hit loc)
395   "Maps MOUSE-HIT and LOC into a mouse-code."
396 ;;;Region is a code for one of text, modeline, scrollbar, or minibuffer.
397   (logior (sm::hit-code hit)
398           (mouse-region-to-code (sm::window-region loc))))
399
400 (defun mouse-region-to-code (region)
401   "Returns partial mouse-code for specified REGION."
402   (cdr (assq region sm::keyword-alist)))
403
404 (defun mouse-list-to-mouse-code (mouse-list)
405   "Map a MOUSE-LIST to a mouse-code."
406   (apply 'logior
407          (mapcar (function (lambda (x)
408                              (cdr (assq x sm::keyword-alist))))
409                   mouse-list)))
410
411 (defun mouse-code-to-mouse-list (mouse-code)
412   "Map a MOUSE-CODE to a mouse-list."
413   (apply 'nconc (mapcar
414                  (function (lambda (x)
415                              (if (logtest mouse-code (cdr x))
416                                  (list (car x)))))
417                  sm::keyword-alist)))
418
419 (defun mousemap-set (code mousemap value)
420   (let* ((alist (cdr mousemap))
421          (assq-result (assq code alist)))
422     (if assq-result
423         (setcdr assq-result value)
424       (setcdr mousemap (cons (cons code value) alist)))))
425
426 (defun mousemap-get (code mousemap)
427   (cdr (assq code (cdr mousemap))))
428
429 (defun mouse-lookup (mouse-code)
430   "Look up MOUSE-EVENT and return the definition. nil means undefined."
431   (or (mousemap-get mouse-code current-local-mousemap)
432       (mousemap-get mouse-code current-global-mousemap)))
433
434 ;;;
435 ;;; I (jpeck) don't understand the utility of the next four functions
436 ;;; ask Steven Greenbaum <froud@kestrel>
437 ;;;
438 (defun mouse-mask-lookup (mask list)
439   "Args MASK (a bit mask) and LIST (a list of (code . form) pairs).
440 Returns a list of elements of LIST whose code or'ed with MASK is non-zero."
441   (let ((result nil))
442     (while list
443       (if (logtest mask (car (car list)))
444           (setq result (cons (car list) result)))
445       (setq list (cdr list)))
446     result))
447
448 (defun mouse-union (l l-unique)
449   "Return the union of list of mouse (code . form) pairs L and L-UNIQUE,
450 where L-UNIQUE is considered to be union'ized already."
451   (let ((result l-unique))
452     (while l
453       (let ((code-form-pair (car l)))
454         (if (not (assq (car code-form-pair) result))
455             (setq result (cons code-form-pair result))))
456       (setq l (cdr l)))
457     result))
458
459 (defun mouse-union-first-preferred (l1 l2)
460   "Return the union of lists of mouse (code . form) pairs L1 and L2,
461 based on the code's, with preference going to elements in L1."
462   (mouse-union l2 (mouse-union l1 nil)))
463
464 (defun mouse-code-function-pairs-of-region (region)
465   "Return a list of (code . function) pairs, where each code is
466 currently set in the REGION."
467   (let ((mask (mouse-region-to-code region)))
468     (mouse-union-first-preferred
469      (mouse-mask-lookup mask (cdr current-local-mousemap))
470      (mouse-mask-lookup mask (cdr current-global-mousemap))
471      )))
472 \f
473 ;;;
474 ;;; Functions for DESCRIBE-MOUSE-BINDINGS
475 ;;; And other mouse documentation functions
476 ;;; Still need a good procedure to print out a help sheet in readable format.
477 ;;;
478
479 (defun one-line-doc-string (function)
480   "Returns first line of documentation string for FUNCTION.
481 If there is no documentation string, then the string
482 \"No documentation\" is returned."
483   (while (consp function) (setq function (car function)))
484   (let ((doc (documentation function)))
485     (if (null doc)
486         "No documentation."
487       (string-match "^.*$" doc)
488       (substring doc 0 (match-end 0)))))
489
490 (defun print-mouse-format (binding)
491   (princ (car binding))
492   (princ ": ")
493   (mapcar (function
494            (lambda (mouse-list)
495              (princ mouse-list)
496              (princ " ")))
497           (cdr binding))
498   (terpri)
499   (princ "  ")
500   (princ (one-line-doc-string (car binding)))
501   (terpri)
502   )
503
504 (defun print-mouse-bindings (region)
505   "Prints mouse-event bindings for REGION."
506   (mapcar 'print-mouse-format (sm::event-bindings region)))
507
508 (defun sm::event-bindings (region)
509   "Returns an alist of (function . (mouse-list1 ... mouse-listN)) for REGION,
510 where each mouse-list is bound to the function in REGION."
511   (let ((mouse-bindings (mouse-code-function-pairs-of-region region))
512         (result nil))
513     (while mouse-bindings
514       (let* ((code-function-pair (car mouse-bindings))
515              (current-entry (assoc (cdr code-function-pair) result)))
516         (if current-entry
517             (setcdr current-entry
518                     (cons (mouse-code-to-mouse-list (car code-function-pair))
519                           (cdr current-entry)))
520           (setq result (cons (cons (cdr code-function-pair)
521                                    (list (mouse-code-to-mouse-list
522                                           (car code-function-pair))))
523                              result))))
524       (setq mouse-bindings (cdr mouse-bindings))
525       )
526     result))
527
528 (defun describe-mouse-bindings ()
529   "Lists all current mouse-event bindings."
530   (interactive)
531   (with-output-to-temp-buffer "*Help*"
532     (princ "Text Region") (terpri)
533     (princ "---- ------") (terpri)
534     (print-mouse-bindings 'text) (terpri)
535     (princ "Modeline Region") (terpri)
536     (princ "-------- ------") (terpri)
537     (print-mouse-bindings 'modeline) (terpri)
538     (princ "Scrollbar Region") (terpri)
539     (princ "--------- ------") (terpri)
540     (print-mouse-bindings 'scrollbar)))
541
542 (defun describe-mouse-briefly (mouse-list)
543   "Print a short description of the function bound to MOUSE-LIST."
544   (interactive "xDescribe mouse list briefly: ")
545   (let ((function (mouse-lookup (mouse-list-to-mouse-code mouse-list))))
546     (if function
547         (message "%s runs the command %s" mouse-list function)
548       (message "%s is undefined" mouse-list))))
549
550 (defun mouse-help-menu (function-and-binding)
551   (cons (prin1-to-string (car function-and-binding))
552         (menu-create    ; Two sub-menu items of form ("String" . nil)
553          (list (list (one-line-doc-string (car function-and-binding)))
554                (list (prin1-to-string (cdr function-and-binding)))))))
555
556 (defun mouse-help-region (w x y &optional region)
557   "Displays a menu of mouse functions callable in this region."
558   (let* ((region (or region (sm::window-region (list w x y))))
559          (mlist (mapcar (function mouse-help-menu)
560                         (sm::event-bindings region)))
561          (menu (menu-create (cons (list (symbol-name region)) mlist)))
562          (item (sun-menu-evaluate w 0 y menu))
563          )))
564 \f
565 ;;;
566 ;;; Menu interface functions
567 ;;;
568 ;;; use defmenu, because this interface is subject to change
569 ;;; really need a menu-p, but we use vectorp and the context...
570 ;;;
571 (defun menu-create (items)
572   "Functional form for defmenu, given a list of ITEMS returns a menu.
573 Each ITEM is a (STRING . VALUE) pair."
574   (apply 'vector items)
575   )
576
577 (defmacro defmenu (menu &rest itemlist)
578   "Defines MENU to be a menu, the ITEMS are (STRING . VALUE) pairs.
579 See sun-menu-evaluate for interpretation of ITEMS."
580   (list 'defconst menu (funcall 'menu-create itemlist))
581   )
582
583 (defun sun-menu-evaluate (*menu-window* *menu-x* *menu-y* menu)
584   "Display a pop-up menu in WINDOW at X Y and evaluate selected item
585 of MENU.  MENU (or its symbol-value) should be a menu defined by defmenu.
586   A menu ITEM is a (STRING . FORM) pair;
587 the FORM associated with the selected STRING is evaluated,
588 and the resulting value is returned.  Generally these FORMs are
589 evaluated for their side-effects rather than their values.
590   If the selected form is a menu or a symbol whose value is a menu,
591 then it is displayed and evaluated as a pullright menu item.
592   If the FORM of the first ITEM is nil, the STRING of the item
593 is used as a label for the menu, i.e. it's inverted and not selectable."
594
595   (if (symbolp menu) (setq menu (symbol-value menu)))
596   (eval (sun-menu-internal *menu-window* *menu-x* *menu-y* 4 menu)))
597
598 (defun sun-get-frame-data (code)
599   "Sends the tty-sub-window escape sequence CODE to terminal,
600 and returns a cons of the two numbers in returned escape sequence.
601 That is it returns (cons <car> <cdr>) from \"\\E[n;<car>;<cdr>t\".
602 CODE values: 13 = Tool-Position, 14 = Size-in-Pixels, 18 = Size-in-Chars."
603   (send-string-to-terminal (concat "\033[" (int-to-string code) "t"))
604   (let (char str x y)
605     (while (not (equal 116 (setq char (read-char)))) ; #\t = 116
606       (setq str (cons char str)))
607     (setq str (mapconcat 'char-to-string (nreverse str) ""))
608     (string-match ";[0-9]*" str)
609     (setq y (substring str (1+ (match-beginning 0)) (match-end 0)))
610     (setq str (substring str (match-end 0)))
611     (string-match ";[0-9]*" str)
612     (setq x (substring str (1+ (match-beginning 0)) (match-end 0)))
613     (cons (string-to-int y) (string-to-int x))))
614
615 (defun sm::font-size ()
616   "Returns font size in pixels: (cons Ysize Xsize)"
617   (let ((pix (sun-get-frame-data 14))   ; returns size in pixels
618         (chr (sun-get-frame-data 18)))  ; returns size in chars
619     (cons (/ (car pix) (car chr)) (/ (cdr pix) (cdr chr)))))
620
621 (defvar sm::menu-kludge-x nil
622   "Cached frame-to-window X-Offset for sm::menu-kludge")
623 (defvar sm::menu-kludge-y nil
624   "Cached frame-to-window Y-Offset for sm::menu-kludge")
625
626 (defun sm::menu-kludge ()
627   "If sunfns.c uses <Menu_Base_Kludge> this function must be here!"
628   (or sm::menu-kludge-y
629       (let ((fs (sm::font-size)))
630         (setq sm::menu-kludge-y (+ 8 (car fs))  ; a title line and borders
631               sm::menu-kludge-x 4)))    ; best values depend on .defaults/Menu
632   (let ((wl (sun-get-frame-data 13)))           ; returns frame location
633     (cons (+ (car wl) sm::menu-kludge-y)
634           (+ (cdr wl) sm::menu-kludge-x))))
635 \f
636 ;;;
637 ;;;  Function interface to selection/region
638 ;;;  primitive functions are defined in sunfns.c
639 ;;;
640 (defun sun-yank-selection ()
641   "Set mark and yank the contents of the current sunwindows selection.
642 Insert contents into the current buffer at point."
643   (interactive "*")
644   (set-mark-command nil)
645   (insert-string (sun-get-selection)))
646
647 (defun sun-select-region (start end)
648   "Set the sunwindows selection to the region in the current buffer."
649   (interactive "r")
650   (sun-set-selection (buffer-substring start end)))
651
652 ;;;
653 ;;; Support for emacstool
654 ;;; This closes the window instead of stopping emacs.
655 ;;;
656 (defun suspend-emacstool (&optional stuffstring)
657   "Suspend emacstool.
658 If running under as a detached process emacstool,
659 you don't want to suspend  (there is no way to resume),
660 just close the window, and wait for reopening."
661   (interactive)
662   (run-hooks 'suspend-hook)
663   (if stuffstring (send-string-to-terminal stuffstring))
664   (send-string-to-terminal "\033[2t")   ; To close EmacsTool window.
665   (run-hooks 'suspend-resume-hook))
666 ;;;
667 ;;; initialize mouse maps
668 ;;;
669
670 (with-boundp 'current-local-mousemap
671   (make-variable-buffer-local 'current-local-mousemap)
672   (setq-default current-local-mousemap nil))
673 (defvar current-global-mousemap (make-mousemap))
674
675 (provide 'sun-mouse)
676
677 ;;; sun-mouse.el ends here