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