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