*** empty log message ***
[bpt/emacs.git] / lisp / term / sun-mouse.el
CommitLineData
c88ab9ce
ER
1;;; sun-mouse.el --- mouse handling for Sun windows
2
0d20f9a0
JB
3;; Copyright (C) 1987 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 1, or (at your option)
10;; any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING. If not, write to
19;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21;;; Jeff Peck, Sun Microsystems, Jan 1987.
22;;; Original idea by Stan Jefferson
23
0d20f9a0
JB
24;;;
25;;; Modelled after the GNUEMACS keymap interface.
26;;;
27;;; User Functions:
28;;; make-mousemap, copy-mousemap,
29;;; define-mouse, global-set-mouse, local-set-mouse,
30;;; use-global-mousemap, use-local-mousemap,
31;;; mouse-lookup, describe-mouse-bindings
32;;;
33;;; Options:
34;;; extra-click-wait, scrollbar-width
35;;;
36
37(defvar extra-click-wait 150
38 "*Number of milliseconds to wait for an extra click.
39Set this to zero if you don't want chords or double clicks.")
40
41(defvar scrollbar-width 5
42 "*The character width of the scrollbar.
43The cursor is deemed to be in the right edge scrollbar if it is this near the
44right edge, and more than two chars past the end of the indicated line.
45Setting to nil limits the scrollbar to the edge or vertical dividing bar.")
46\f
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.
60MOUSE-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.
70The atom `double' is used with a button designator to denote a double click.
71Two button chords are denoted by listing the two buttons.
72See sun-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.
77See define-mouse for a description of MOUSE-EVENT-LIST and DEF.
78Note that if MOUSE-EVENT-LIST has a local definition in the current buffer,
79that 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.
85See define-mouse for a description of the arguments.
86The definition goes in the current buffer's local mousemap.
87Normally 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.
99nil for MOUSEMAP means no local mousemap."
100 (setq current-local-mousemap mousemap))
101
102\f
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.
114Just 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 sm::ButtonBits 7) ; Lowest 3 bits.
123(defconst sm::ShiftmaskBits 56) ; Second lowest 3 bits (56 = 63 - 7).
124(defconst sm::DoubleBits 64) ; Bit 7.
125(defconst sm::UpBits 128) ; Bit 8.
126
127;;; All the useful code bits
128(defmacro sm::hit-code (hit)
129 (` (nth 0 (, hit))))
130;;; The button, or buttons if a chord.
131(defmacro sm::hit-button (hit)
132 (` (logand sm::ButtonBits (nth 0 (, hit)))))
133;;; The shift, control, and meta flags.
134(defmacro sm::hit-shiftmask (hit)
135 (` (logand sm::ShiftmaskBits (nth 0 (, hit)))))
136;;; Set if a double click (but not a chord).
137(defmacro sm::hit-double (hit)
138 (` (logand sm::DoubleBits (nth 0 (, hit)))))
139;;; Set on button release (as opposed to button press).
140(defmacro sm::hit-up (hit)
141 (` (logand sm::UpBits (nth 0 (, hit)))))
142;;; Screen x position.
143(defmacro sm::hit-x (hit) (list 'nth 1 hit))
144;;; Screen y position.
145(defmacro sm::hit-y (hit) (list 'nth 2 hit))
146;;; Millisconds since last hit.
147(defmacro sm::hit-delta (hit) (list 'nth 3 hit))
148
149(defmacro sm::hit-up-p (hit) ; A predicate.
150 (` (not (zerop (sm::hit-up (, hit))))))
151
152;;;
153;;; Loc accessors. for sm::window-xy
154;;;
155(defmacro sm::loc-w (loc) (list 'nth 0 loc))
156(defmacro sm::loc-x (loc) (list 'nth 1 loc))
157(defmacro sm::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-function 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-function 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
190YESMINI says to include the minibuffer as a window.
191This 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-function 0)
201
202(defun move-to-loc (x y)
203 "Move cursor to window location X, Y.
204Handles 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\f
226(defun sun-mouse-handler (&optional hit)
227 "Evaluates the function or list associated with a mouse hit.
228Expecting to read a hit, which is a list: (button x y delta).
229A form bound to button by define-mouse is found by mouse-lookup.
230The variables: *mouse-window*, *mouse-x*, *mouse-y* are bound.
231If 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),
233the form is eval'ed; if the form is neither of these, it is an error.
234Returns nil."
235 (interactive)
236 (if (null hit) (setq hit (sm::combined-hits)))
237 (let ((loc (sm::window-xy (sm::hit-x hit) (sm::hit-y hit))))
238 (let ((*mouse-window* (sm::loc-w loc))
239 (*mouse-x* (sm::loc-x loc))
240 (*mouse-y* (sm::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 (sm::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 'sun-mouse-handler get on last-command,
259 ;; since this function should be transparent.
260 (if (eq this-command 'sun-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 sm::combined-hits ()
266 "Read and return next mouse-hit, include possible double click"
267 (let ((hit1 (mouse-hit-read)))
268 (if (not (sm::hit-up-p hit1)) ; Up hits dont start doubles or chords.
269 (let ((hit2 (mouse-second-hit extra-click-wait)))
270 (if hit2 ; we cons'd it, we can smash it.
271 ; (setf (sm::hit-code hit1) (logior (sm::hit-code hit1) ...))
272 (setcar hit1 (logior (sm::hit-code hit1)
273 (sm::hit-code hit2)
274 (if (= (sm::hit-button hit1)
275 (sm::hit-button hit2))
276 sm::DoubleBits 0))))))
277 hit1))
278
279(defun mouse-hit-read ()
280 "Read mouse-hit list from keyboard. Like (read 'read-char),
281but that uses minibuffer, and mucks up last-command."
282 (let ((char-list nil) (char nil))
283 (while (not (equal 13 ; Carriage return.
284 (prog1 (setq char (read-char))
285 (setq char-list (cons char char-list))))))
286 (read (mapconcat 'char-to-string (nreverse char-list) ""))
287 ))
288
289;;; Second Click Hackery....
290;;; if prefix is not mouse-prefix, need a way to unread the char...
291;;; or else have mouse flush input queue, or else need a peek at next char.
292
293;;; There is no peek, but since one character can be unread, we only
294;;; have to flush the queue when the command after a mouse click
295;;; starts with mouse-prefix1 (see below).
296;;; Something to do later: We could buffer the read commands and
297;;; execute them ourselves after doing the mouse command (using
298;;; lookup-key ??).
299
300(defvar mouse-prefix1 24 ; C-x
301 "First char of mouse-prefix. Used to detect double clicks and chords.")
302
303(defvar mouse-prefix2 0 ; C-@
304 "Second char of mouse-prefix. Used to detect double clicks and chords.")
305
306
307(defun mouse-second-hit (hit-wait)
308 "Returns the next mouse hit occurring within HIT-WAIT milliseconds."
309 (if (sit-for-millisecs hit-wait) nil ; No input within hit-wait millisecs.
310 (let ((pc1 (read-char)))
311 (if (or (not (equal pc1 mouse-prefix1))
312 (sit-for-millisecs 3)) ; a mouse prefix will have second char
313 (progn (setq unread-command-char pc1) ; Can get away with one unread.
314 nil) ; Next input not mouse event.
315 (let ((pc2 (read-char)))
316 (if (not (equal pc2 mouse-prefix2))
317 (progn (setq unread-command-char pc1) ; put back the ^X
318;;; Too bad can't do two: (setq unread-command-char (list pc1 pc2))
319 (ding) ; user will have to retype that pc2.
320 nil) ; This input is not a mouse event.
321 ;; Next input has mouse prefix and is within time limit.
322 (let ((new-hit (mouse-hit-read))) ; Read the new hit.
323 (if (sm::hit-up-p new-hit) ; Ignore up events when timing.
324 (mouse-second-hit (- hit-wait (sm::hit-delta new-hit)))
325 new-hit ; New down hit within limit, return it.
326 ))))))))
327\f
328(defun sm::window-xy (x y)
329 "Find window containing screen coordinates X and Y.
330Returns list (window x y) where x and y are relative to window."
331 (or
332 (catch 'found
333 (eval-in-windows
334 (let ((we (window-edges (selected-window))))
335 (let ((le (nth 0 we))
336 (te (nth 1 we))
337 (re (nth 2 we))
338 (be (nth 3 we)))
339 (if (= re (screen-width))
340 ;; include the continuation column with this window
341 (setq re (1+ re)))
342 (if (= be (screen-height))
343 ;; include partial line at bottom of screen with this window
344 ;; id est, if window is not multple of char size.
345 (setq be (1+ be)))
346
347 (if (and (>= x le) (< x re)
348 (>= y te) (< y be))
349 (throw 'found
350 (list (selected-window) (- x le) (- y te))))))
351 t)) ; include minibuffer in eval-in-windows
352 ;;If x,y from a real mouse click, we shouldn't get here.
353 (list nil x y)
354 ))
355
356(defun sm::window-region (loc)
357 "Parse LOC into a region symbol.
358Returns one of (text scrollbar modeline minibuffer)"
359 (let ((w (sm::loc-w loc))
360 (x (sm::loc-x loc))
361 (y (sm::loc-y loc)))
362 (let ((right (1- (window-width w)))
363 (bottom (1- (window-height w))))
364 (cond ((minibuffer-window-p w) 'minibuffer)
365 ((>= y bottom) 'modeline)
366 ((>= x right) 'scrollbar)
367 ;; far right column (window seperator) is always a scrollbar
368 ((and scrollbar-width
369 ;; mouse within scrollbar-width of edge.
370 (>= x (- right scrollbar-width))
371 ;; mouse a few chars past the end of line.
372 (>= x (+ 2 (window-line-end w x y))))
373 'scrollbar)
374 (t 'text)))))
375
376(defun window-line-end (w x y)
377 "Return WINDOW column (ignore X) containing end of line Y"
378 (eval-in-window w (save-excursion (move-to-loc (screen-width) y))))
379\f
380;;;
381;;; The encoding of mouse events into a mousemap.
382;;; These values must agree with coding in emacstool:
383;;;
384(defconst sm::keyword-alist
385 '((left . 1) (middle . 2) (right . 4)
386 (shift . 8) (control . 16) (meta . 32) (double . 64) (up . 128)
387 (text . 256) (scrollbar . 512) (modeline . 1024) (minibuffer . 2048)
388 ))
389
390(defun mouse-event-code (hit loc)
391 "Maps MOUSE-HIT and LOC into a mouse-code."
392;;;Region is a code for one of text, modeline, scrollbar, or minibuffer.
393 (logior (sm::hit-code hit)
394 (mouse-region-to-code (sm::window-region loc))))
395
396(defun mouse-region-to-code (region)
397 "Returns partial mouse-code for specified REGION."
398 (cdr (assq region sm::keyword-alist)))
399
400(defun mouse-list-to-mouse-code (mouse-list)
401 "Map a MOUSE-LIST to a mouse-code."
402 (apply 'logior
403 (mapcar (function (lambda (x)
404 (cdr (assq x sm::keyword-alist))))
405 mouse-list)))
406
407(defun mouse-code-to-mouse-list (mouse-code)
408 "Map a MOUSE-CODE to a mouse-list."
409 (apply 'nconc (mapcar
410 (function (lambda (x)
411 (if (logtest mouse-code (cdr x))
412 (list (car x)))))
413 sm::keyword-alist)))
414
415(defun mousemap-set (code mousemap value)
416 (let* ((alist (cdr mousemap))
417 (assq-result (assq code alist)))
418 (if assq-result
419 (setcdr assq-result value)
420 (setcdr mousemap (cons (cons code value) alist)))))
421
422(defun mousemap-get (code mousemap)
423 (cdr (assq code (cdr mousemap))))
424
425(defun mouse-lookup (mouse-code)
426 "Look up MOUSE-EVENT and return the definition. nil means undefined."
427 (or (mousemap-get mouse-code current-local-mousemap)
428 (mousemap-get mouse-code current-global-mousemap)))
429
430;;;
431;;; I (jpeck) don't understand the utility of the next four functions
432;;; ask Steven Greenbaum <froud@kestrel>
433;;;
434(defun mouse-mask-lookup (mask list)
435 "Args MASK (a bit mask) and LIST (a list of (code . form) pairs).
436Returns a list of elements of LIST whose code or'ed with MASK is non-zero."
437 (let ((result nil))
438 (while list
439 (if (logtest mask (car (car list)))
440 (setq result (cons (car list) result)))
441 (setq list (cdr list)))
442 result))
443
444(defun mouse-union (l l-unique)
445 "Return the union of list of mouse (code . form) pairs L and L-UNIQUE,
446where L-UNIQUE is considered to be union'ized already."
447 (let ((result l-unique))
448 (while l
449 (let ((code-form-pair (car l)))
450 (if (not (assq (car code-form-pair) result))
451 (setq result (cons code-form-pair result))))
452 (setq l (cdr l)))
453 result))
454
455(defun mouse-union-first-prefered (l1 l2)
456 "Return the union of lists of mouse (code . form) pairs L1 and L2,
457based on the code's, with preference going to elements in L1."
458 (mouse-union l2 (mouse-union l1 nil)))
459
460(defun mouse-code-function-pairs-of-region (region)
461 "Return a list of (code . function) pairs, where each code is
462currently set in the REGION."
463 (let ((mask (mouse-region-to-code region)))
464 (mouse-union-first-prefered
465 (mouse-mask-lookup mask (cdr current-local-mousemap))
466 (mouse-mask-lookup mask (cdr current-global-mousemap))
467 )))
468\f
469;;;
470;;; Functions for DESCRIBE-MOUSE-BINDINGS
471;;; And other mouse documentation functions
472;;; Still need a good procedure to print out a help sheet in readable format.
473;;;
474
475(defun one-line-doc-string (function)
476 "Returns first line of documentation string for FUNCTION.
477If there is no documentation string, then the string
478\"No documentation\" is returned."
479 (while (consp function) (setq function (car function)))
480 (let ((doc (documentation function)))
481 (if (null doc)
482 "No documentation."
483 (string-match "^.*$" doc)
484 (substring doc 0 (match-end 0)))))
485
486(defun print-mouse-format (binding)
487 (princ (car binding))
488 (princ ": ")
489 (mapcar (function
490 (lambda (mouse-list)
491 (princ mouse-list)
492 (princ " ")))
493 (cdr binding))
494 (terpri)
495 (princ " ")
496 (princ (one-line-doc-string (car binding)))
497 (terpri)
498 )
499
500(defun print-mouse-bindings (region)
501 "Prints mouse-event bindings for REGION."
502 (mapcar 'print-mouse-format (sm::event-bindings region)))
503
504(defun sm::event-bindings (region)
505 "Returns an alist of (function . (mouse-list1 ... mouse-listN)) for REGION,
506where each mouse-list is bound to the function in REGION."
507 (let ((mouse-bindings (mouse-code-function-pairs-of-region region))
508 (result nil))
509 (while mouse-bindings
510 (let* ((code-function-pair (car mouse-bindings))
511 (current-entry (assoc (cdr code-function-pair) result)))
512 (if current-entry
513 (setcdr current-entry
514 (cons (mouse-code-to-mouse-list (car code-function-pair))
515 (cdr current-entry)))
516 (setq result (cons (cons (cdr code-function-pair)
517 (list (mouse-code-to-mouse-list
518 (car code-function-pair))))
519 result))))
520 (setq mouse-bindings (cdr mouse-bindings))
521 )
522 result))
523
524(defun describe-mouse-bindings ()
525 "Lists all current mouse-event bindings."
526 (interactive)
527 (with-output-to-temp-buffer "*Help*"
528 (princ "Text Region") (terpri)
529 (princ "---- ------") (terpri)
530 (print-mouse-bindings 'text) (terpri)
531 (princ "Modeline Region") (terpri)
532 (princ "-------- ------") (terpri)
533 (print-mouse-bindings 'modeline) (terpri)
534 (princ "Scrollbar Region") (terpri)
535 (princ "--------- ------") (terpri)
536 (print-mouse-bindings 'scrollbar)))
537
538(defun describe-mouse-briefly (mouse-list)
539 "Print a short description of the function bound to MOUSE-LIST."
540 (interactive "xDescibe mouse list briefly: ")
541 (let ((function (mouse-lookup (mouse-list-to-mouse-code mouse-list))))
542 (if function
543 (message "%s runs the command %s" mouse-list function)
544 (message "%s is undefined" mouse-list))))
545
546(defun mouse-help-menu (function-and-binding)
547 (cons (prin1-to-string (car function-and-binding))
548 (menu-create ; Two sub-menu items of form ("String" . nil)
549 (list (list (one-line-doc-string (car function-and-binding)))
550 (list (prin1-to-string (cdr function-and-binding)))))))
551
552(defun mouse-help-region (w x y &optional region)
553 "Displays a menu of mouse functions callable in this region."
554 (let* ((region (or region (sm::window-region (list w x y))))
555 (mlist (mapcar (function mouse-help-menu)
556 (sm::event-bindings region)))
557 (menu (menu-create (cons (list (symbol-name region)) mlist)))
558 (item (sun-menu-evaluate w 0 y menu))
559 )))
560\f
561;;;
562;;; Menu interface functions
563;;;
564;;; use defmenu, because this interface is subject to change
565;;; really need a menu-p, but we use vectorp and the context...
566;;;
567(defun menu-create (items)
568 "Functional form for defmenu, given a list of ITEMS returns a menu.
569Each ITEM is a (STRING . VALUE) pair."
570 (apply 'vector items)
571 )
572
573(defmacro defmenu (menu &rest itemlist)
574 "Defines MENU to be a menu, the ITEMS are (STRING . VALUE) pairs.
575See sun-menu-evaluate for interpretation of ITEMS."
576 (list 'defconst menu (funcall 'menu-create itemlist))
577 )
578
579(defun sun-menu-evaluate (*menu-window* *menu-x* *menu-y* menu)
580 "Display a pop-up menu in WINDOW at X Y and evaluate selected item
581of MENU. MENU (or its symbol-value) should be a menu defined by defmenu.
582 A menu ITEM is a (STRING . FORM) pair;
583the FORM associated with the selected STRING is evaluated,
584and the resulting value is returned. Generally these FORMs are
585evaluated for their side-effects rather than their values.
586 If the selected form is a menu or a symbol whose value is a menu,
587then it is displayed and evaluated as a pullright menu item.
588 If the the FORM of the first ITEM is nil, the STRING of the item
589is used as a label for the menu, i.e. it's inverted and not selectible."
590
591 (if (symbolp menu) (setq menu (symbol-value menu)))
592 (eval (sun-menu-internal *menu-window* *menu-x* *menu-y* 4 menu)))
593
594(defun sun-get-frame-data (code)
595 "Sends the tty-sub-window escape sequence CODE to terminal,
596and returns a cons of the two numbers in returned escape sequence.
597That is it returns (cons <car> <cdr>) from \"\\E[n;<car>;<cdr>t\".
598CODE values: 13 = Tool-Position, 14 = Size-in-Pixels, 18 = Size-in-Chars."
599 (send-string-to-terminal (concat "\033[" (int-to-string code) "t"))
600 (let (char str x y)
601 (while (not (equal 116 (setq char (read-char)))) ; #\t = 116
602 (setq str (cons char str)))
603 (setq str (mapconcat 'char-to-string (nreverse str) ""))
604 (string-match ";[0-9]*" str)
605 (setq y (substring str (1+ (match-beginning 0)) (match-end 0)))
606 (setq str (substring str (match-end 0)))
607 (string-match ";[0-9]*" str)
608 (setq x (substring str (1+ (match-beginning 0)) (match-end 0)))
609 (cons (string-to-int y) (string-to-int x))))
610
611(defun sm::font-size ()
612 "Returns font size in pixels: (cons Ysize Xsize)"
613 (let ((pix (sun-get-frame-data 14)) ; returns size in pixels
614 (chr (sun-get-frame-data 18))) ; returns size in chars
615 (cons (/ (car pix) (car chr)) (/ (cdr pix) (cdr chr)))))
616
617(defvar sm::menu-kludge-x nil
618 "Cached frame-to-window X-Offset for sm::menu-kludge")
619(defvar sm::menu-kludge-y nil
620 "Cached frame-to-window Y-Offset for sm::menu-kludge")
621
622(defun sm::menu-kludge ()
623 "If sunfns.c uses <Menu_Base_Kludge> this function must be here!"
624 (or sm::menu-kludge-y
625 (let ((fs (sm::font-size)))
626 (setq sm::menu-kludge-y (+ 8 (car fs)) ; a title line and borders
627 sm::menu-kludge-x 4))) ; best values depend on .defaults/Menu
628 (let ((wl (sun-get-frame-data 13))) ; returns frame location
629 (cons (+ (car wl) sm::menu-kludge-y)
630 (+ (cdr wl) sm::menu-kludge-x))))
631\f
632;;;
633;;; Function interface to selection/region
634;;; primative functions are defined in sunfns.c
635;;;
636(defun sun-yank-selection ()
637 "Set mark and yank the contents of the current sunwindows selection
638into the current buffer at point."
639 (interactive "*")
640 (set-mark-command nil)
641 (insert-string (sun-get-selection)))
642
643(defun sun-select-region (beg end)
644 "Set the sunwindows selection to the region in the current buffer."
645 (interactive "r")
646 (sun-set-selection (buffer-substring beg end)))
647
648;;;
649;;; Support for emacstool
650;;; This closes the window instead of stopping emacs.
651;;;
652(defun suspend-emacstool (&optional stuffstring)
653 "If running under as a detached process emacstool,
654you don't want to suspend (there is no way to resume),
655just close the window, and wait for reopening."
656 (interactive)
657 (run-hooks 'suspend-hook)
658 (if stuffstring (send-string-to-terminal stuffstring))
659 (send-string-to-terminal "\033[2t") ; To close EmacsTool window.
660 (run-hooks 'suspend-resume-hook))
661;;;
662;;; initialize mouse maps
663;;;
664
665(make-variable-buffer-local 'current-local-mousemap)
666(setq-default current-local-mousemap nil)
667(defvar current-global-mousemap (make-mousemap))
49116ac0
JB
668
669(provide 'sun-mouse)
670
c88ab9ce 671;;; sun-mouse.el ends here