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