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