Commit | Line | Data |
---|---|---|
f448b834 RS |
1 | ;; Emulate the Lucid event data type and associated functions. |
2 | ;; Copyright (C) 1993 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 2, 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 | ;;; Notes: | |
21 | ||
22 | ;; Things we cannot emulate in Lisp: | |
23 | ;; It is not possible to emulate current-mouse-event as a variable, | |
24 | ;; though it is not hard to obtain the data from (this-command-keys). | |
25 | ||
f448b834 RS |
26 | ;; We do not have a variable unread-command-event; |
27 | ;; instead, we have the more general unread-command-events. | |
28 | ||
ecc71b7f RS |
29 | ;; Our read-key-sequence and read-char are not precisely |
30 | ;; compatible with those in Lucid Emacs, but they should work ok. | |
f448b834 RS |
31 | |
32 | ;;; Code: | |
33 | ||
34 | ;; Make events of type eval, menu and timeout | |
35 | ;; execute properly. | |
36 | ||
37 | (define-key global-map [menu] 'execute-eval-event) | |
38 | (define-key global-map [timeout] 'execute-eval-event) | |
39 | (define-key global-map [eval] 'execute-eval-event) | |
40 | ||
41 | (defun execute-eval-event (event) | |
42 | (interactive "e") | |
43 | (funcall (nth 1 event) (nth 2 event))) | |
44 | ||
45 | (put 'eval 'event-symbol-elements '(eval)) | |
46 | (put 'menu 'event-symbol-elements '(eval)) | |
47 | (put 'timeout 'event-symbol-elements '(eval)) | |
48 | ||
49 | (defsubst eventp (obj) | |
50 | "True if the argument is an event object." | |
51 | (or (integerp obj) | |
52 | (and (symbolp obj) | |
53 | (get obj 'event-symbol-elements)) | |
54 | (and (consp obj) | |
55 | (symbolp (car obj)) | |
56 | (get (car obj) 'event-symbol-elements)))) | |
57 | ||
58 | (defun allocate-event () | |
59 | "Returns an empty event structure. | |
60 | In this emulation, it returns nil." | |
61 | nil) | |
62 | ||
63 | (defun button-press-event-p (obj) | |
64 | "True if the argument is a mouse-button-press event object." | |
65 | (and (consp obj) (symbolp (car obj)) | |
66 | (memq 'down (get (car obj) 'event-symbol-elements)))) | |
67 | ||
68 | (defun button-release-event-p (obj) | |
69 | "True if the argument is a mouse-button-release event object." | |
70 | (and (consp obj) (symbolp (car obj)) | |
71 | (or (memq 'click (get (car obj) 'event-symbol-elements)) | |
72 | (memq 'drag (get (car obj) 'event-symbol-elements))))) | |
73 | ||
74 | (defun character-to-event (ch &optional event) | |
75 | "Converts a numeric ASCII value to an event structure, replete with | |
76 | bucky bits. The character is the first argument, and the event to fill | |
77 | in is the second. This function contains knowledge about what the codes | |
78 | mean -- for example, the number 9 is converted to the character Tab, | |
79 | not the distinct character Control-I. | |
80 | ||
81 | Beware that character-to-event and event-to-character are not strictly | |
82 | inverse functions, since events contain much more information than the | |
83 | ASCII character set can encode." | |
84 | ch) | |
85 | ||
86 | (defun copy-event (event1 &optional event2) | |
87 | "Make a copy of the given event object. | |
88 | In this emulation, `copy-event' just returns its argument." | |
89 | event1) | |
90 | ||
91 | (defun deallocate-event (event) | |
92 | "Allow the given event structure to be reused. | |
93 | In actual Lucid Emacs, you MUST NOT use this event object after | |
94 | calling this function with it. You will lose. It is not necessary to | |
95 | call this function, as event objects are garbage- collected like all | |
96 | other objects; however, it may be more efficient to explicitly | |
97 | deallocate events when you are sure that that is safe. | |
98 | ||
99 | This emulation does not actually deallocate or reuse events | |
100 | except via garbage collection and `cons'." | |
101 | nil) | |
102 | ||
103 | (defun dispatch-event (event) | |
104 | "Given an event object returned by next-event, execute it." | |
105 | (let ((type (car-safe event))) | |
106 | (cond ((eq type 'eval) | |
107 | (funcall (nth 1 event) (nth 2 event))) | |
108 | ((eq type 'menu) | |
109 | (funcall (nth 1 event) (nth 2 event))) | |
110 | ((eq type 'switch-frame) | |
111 | (internal-select-frame (nth 1 event))) | |
112 | (t (error "keyboard and mouse events not allowed in `dispatch-event'"))))) | |
113 | ||
114 | (defun enqueue-eval-event: (function object) | |
115 | "Add an eval event to the back of the queue. | |
116 | It will be the next event read after all pending events." | |
117 | (setq unread-command-events | |
118 | (nconc unread-command-events | |
119 | (list (list 'eval function object))))) | |
120 | ||
121 | (defun eval-event-p (obj) | |
122 | "True if the argument is an eval or menu event object." | |
123 | (eq (car-safe obj) 'eval)) | |
124 | ||
125 | (defun event-button (event) | |
126 | "Return the button-number of the given mouse-button-press event." | |
127 | (let ((sym (car (get (car event) 'event-symbol-elements)))) | |
128 | (cdr (assq sym '((mouse-1 . 1) (mouse-2 . 2) (mouse-3 . 3) | |
129 | (mouse-4 . 4) (mouse-5 . 5)))))) | |
130 | ||
131 | (defun event-function (event) | |
132 | "Return the callback function of the given timeout, menu, or eval event." | |
133 | (nth 1 event)) | |
134 | ||
135 | (defun event-key (event) | |
136 | "Returns the KeySym of the given key-press event. | |
137 | The value is an ASCII printing character (not upper case) or a symbol." | |
138 | (if (symbolp event) | |
139 | (car (get event 'event-symbol-elements)) | |
140 | (let ((base (logand event (1- (lsh 1 18))))) | |
141 | (downcase (if (< base 32) (logior base 64) base))))) | |
142 | ||
143 | (defun event-modifiers (event) | |
144 | "Returns a list of symbols representing the modifier keys in event EVENT. | |
145 | The elements of the list may include `meta', `control', | |
146 | `shift', `hyper', `super', `alt'. | |
147 | See also the function `event-modifier-bits'." | |
148 | (let ((type event)) | |
149 | (if (listp type) | |
150 | (setq type (car type))) | |
151 | (if (symbolp type) | |
152 | (cdr (get type 'event-symbol-elements)) | |
153 | (let ((list nil)) | |
154 | (or (zerop (logand type (lsh 1 23))) | |
155 | (setq list (cons 'meta list))) | |
156 | (or (and (zerop (logand type (lsh 1 22))) | |
157 | (>= (logand type 127) 32)) | |
158 | (setq list (cons 'control list))) | |
159 | (or (and (zerop (logand type (lsh 1 21))) | |
160 | (= (logand type 255) (downcase (logand type 255)))) | |
161 | (setq list (cons 'shift list))) | |
162 | (or (zerop (logand type (lsh 1 20))) | |
163 | (setq list (cons 'hyper list))) | |
164 | (or (zerop (logand type (lsh 1 19))) | |
165 | (setq list (cons 'super list))) | |
166 | (or (zerop (logand type (lsh 1 18))) | |
167 | (setq list (cons 'alt list))) | |
168 | list)))) | |
169 | ||
170 | (defun event-modifier-bits (event) | |
171 | "Returns a number representing the modifier keys in event EVENT. | |
172 | See also the function `event-modifiers'." | |
173 | (let ((type event)) | |
174 | (if (listp type) | |
175 | (setq type (car type))) | |
176 | (if (symbolp type) | |
177 | (logand (lsh 63 18) | |
178 | (nth 1 (get type 'event-symbol-element-mask))) | |
179 | (let ((bits (logand type (lsh 63 18))) | |
180 | (base (logand type 127))) | |
181 | ;; Put in Control and Shift bits | |
182 | ;; in the cases where the basic code expresses them. | |
183 | (if (< base 32) | |
184 | (setq bits (logior (lsh 1 22) bits))) | |
185 | (if (/= base (downcase base)) | |
186 | (setq bits (logior (lsh 1 21) bits))) | |
187 | bits)))) | |
188 | ||
189 | (defun event-object (event) | |
190 | "Returns the function argument of the given timeout, menu, or eval event." | |
191 | (nth 2 event)) | |
192 | ||
193 | (defun event-point (event) | |
194 | "Returns the character position of the given mouse-related event. | |
195 | If the event did not occur over a window, or did | |
196 | not occur over text, then this returns nil. Otherwise, it returns an index | |
197 | into the buffer visible in the event's window." | |
198 | (posn-point (event-end event))) | |
199 | ||
200 | (defun event-process (event) | |
201 | "Returns the process of the given process-output event." | |
202 | (nth 1 event)) | |
203 | ||
204 | (defun event-timestamp (event) | |
205 | "Returns the timestamp of the given event object. | |
206 | In Lucid Emacs, this works for any kind of event. | |
207 | In this emulation, it returns nil for non-mouse-related events." | |
208 | (and (listp event) | |
209 | (posn-timestamp (event-end event)))) | |
210 | ||
211 | (defun event-to-character (event &optional lenient) | |
212 | "Returns the closest ASCII approximation to the given event object. | |
213 | If the event isn't a keypress, this returns nil. | |
214 | If the second argument is non-nil, then this is lenient in its | |
215 | translation; it will ignore modifier keys other than control and meta, | |
216 | and will ignore the shift modifier on those characters which have no | |
217 | shifted ASCII equivalent (Control-Shift-A for example, will be mapped to | |
218 | the same ASCII code as Control-A.) If the second arg is nil, then nil | |
219 | will be returned for events which have no direct ASCII equivalent." | |
220 | (if (symbolp event) | |
221 | (and lenient | |
222 | (cdr (assq event '((backspace . 8) (delete . 127) (tab . 9) | |
223 | (return . 10) (enter . 10))))) | |
224 | ;; Our interpretation is, ASCII means anything a number can represent. | |
225 | (if (integerp event) | |
226 | event nil))) | |
227 | ||
228 | (defun event-window (event) | |
229 | "Returns the window of the given mouse-related event object." | |
230 | (posn-window (event-end event))) | |
231 | ||
232 | (defun event-x (event) | |
233 | "Returns the X position in characters of the given mouse-related event." | |
234 | (/ (car (posn-col-row (event-end event))) | |
235 | (character-width (window-frame (event-window event))))) | |
236 | ||
237 | (defun event-x-pixel (event) | |
238 | "Returns the X position in pixels of the given mouse-related event." | |
239 | (car (posn-col-row (event-end event)))) | |
240 | ||
241 | (defun event-y (event) | |
242 | "Returns the Y position in characters of the given mouse-related event." | |
243 | (/ (cdr (posn-col-row (event-end event))) | |
244 | (character-width (window-frame (event-window event))))) | |
245 | ||
246 | (defun event-y-pixel (event) | |
247 | "Returns the Y position in pixels of the given mouse-related event." | |
248 | (cdr (posn-col-row (event-end event)))) | |
249 | ||
250 | (defun key-press-event-p (obj) | |
251 | "True if the argument is a keyboard event object." | |
252 | (or (integerp obj) | |
253 | (and (symbolp obj) | |
254 | (get obj 'event-symbol-elements)))) | |
255 | ||
256 | (defun menu-event-p (obj) | |
257 | "True if the argument is a menu event object." | |
258 | (eq (car-safe obj) 'menu)) | |
259 | ||
260 | (defun motion-event-p (obj) | |
261 | "True if the argument is a mouse-motion event object." | |
262 | (eq (car-safe obj) 'mouse-movement)) | |
263 | ||
264 | (defun next-command-event (event) | |
265 | "Given an event structure, fills it in with the next keyboard, mouse | |
266 | press, or mouse release event available from the user. If there are | |
267 | non-command events available (mouse motion, sub-process output, etc) then | |
268 | these will be executed (with dispatch-event) and discarded." | |
269 | (while (progn | |
270 | (next-event event) | |
271 | (not (or (key-press-event-p event) | |
272 | (button-press-event-p event) | |
273 | (button-release-event-p event) | |
274 | (menu-event-p event)))) | |
275 | (dispatch-event event))) | |
276 | ||
277 | (defun next-event (event &optional ignore) | |
278 | "Given an event structure, fills it in with the next event available | |
279 | from the window system or terminal driver. Pass this object to | |
280 | `dispatch-event' to handle it. | |
281 | ||
282 | See also the function `next-command-event'. | |
283 | ||
284 | If the second optional argument is non-nil, then this will never return | |
285 | key-press and mouse-click events, but will delay them until later. You | |
286 | should probably never need to use this option; it is used for implementing | |
287 | the `wait-reading-process-input' function." | |
288 | (read-event)) | |
289 | ||
290 | (defun process-event-p (obj) | |
291 | "True if the argument is a process-output event object. | |
292 | GNU Emacs 19 does not currently generate process-output events." | |
293 | (eq (car-safe obj) 'process)) | |
294 | ||
295 | (defun timeout-event-p (obj) | |
296 | "True if the argument is a timeout event object. | |
297 | GNU Emacs 19 does not currently generate timeout events." | |
298 | (eq (car-safe obj) 'timeout)) | |
299 | ||
300 | ;;; levents.el ends here |