xref: /386bsd/usr/local/lib/emacs/19.25/lisp/levents.el (revision a2142627)
1;;; levents.el --- emulate the Lucid event data type and associated functions.
2
3;; Copyright (C) 1993 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 2, or (at your option)
10;; any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING.  If not, write to
19;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21;;; Commentary:
22
23;; Things we cannot emulate in Lisp:
24;; It is not possible to emulate current-mouse-event as a variable,
25;; though it is not hard to obtain the data from (this-command-keys).
26
27;; We do not have a variable unread-command-event;
28;; instead, we have the more general unread-command-events.
29
30;; Our read-key-sequence and read-char are not precisely
31;; compatible with those in Lucid Emacs, but they should work ok.
32
33;;; Code:
34
35(defun next-command-event (event)
36  (error "You must rewrite to use `read-command-event' instead of `next-command-event'"))
37
38(defun next-event (event)
39  (error "You must rewrite to use `read-event' instead of `next-event'"))
40
41(defun dispatch-event (event)
42  (error "`dispatch-event' not supported"))
43
44;; Make events of type eval, menu and timeout
45;; execute properly.
46
47(define-key global-map [menu] 'execute-eval-event)
48(define-key global-map [timeout] 'execute-eval-event)
49(define-key global-map [eval] 'execute-eval-event)
50
51(defun execute-eval-event (event)
52  (interactive "e")
53  (funcall (nth 1 event) (nth 2 event)))
54
55(put 'eval 'event-symbol-elements '(eval))
56(put 'menu 'event-symbol-elements '(eval))
57(put 'timeout 'event-symbol-elements '(eval))
58
59(defsubst eventp (obj)
60  "True if the argument is an event object."
61  (or (integerp obj)
62      (and (symbolp obj)
63	   (get obj 'event-symbol-elements))
64      (and (consp obj)
65	   (symbolp (car obj))
66	   (get (car obj) 'event-symbol-elements))))
67
68(defun allocate-event ()
69  "Returns an empty event structure.
70In this emulation, it returns nil."
71  nil)
72
73(defun button-press-event-p (obj)
74  "True if the argument is a mouse-button-press event object."
75  (and (consp obj) (symbolp (car obj))
76       (memq 'down (get (car obj) 'event-symbol-elements))))
77
78(defun button-release-event-p (obj)
79  "True if the argument is a mouse-button-release event object."
80  (and (consp obj) (symbolp (car obj))
81       (or (memq 'click (get (car obj) 'event-symbol-elements))
82	   (memq 'drag (get (car obj) 'event-symbol-elements)))))
83
84(defun character-to-event (ch &optional event)
85  "Converts a numeric ASCII value to an event structure, replete with
86bucky bits.  The character is the first argument, and the event to fill
87in is the second.  This function contains knowledge about what the codes
88mean -- for example, the number 9 is converted to the character Tab,
89not the distinct character Control-I.
90
91Beware that character-to-event and event-to-character are not strictly
92inverse functions, since events contain much more information than the
93ASCII character set can encode."
94  ch)
95
96(defun copy-event (event1 &optional event2)
97  "Make a copy of the given event object.
98In this emulation, `copy-event' just returns its argument."
99  event1)
100
101(defun deallocate-event (event)
102  "Allow the given event structure to be reused.
103In actual Lucid Emacs, you MUST NOT use this event object after
104calling this function with it.  You will lose.  It is not necessary to
105call this function, as event objects are garbage- collected like all
106other objects; however, it may be more efficient to explicitly
107deallocate events when you are sure that that is safe.
108
109This emulation does not actually deallocate or reuse events
110except via garbage collection and `cons'."
111  nil)
112
113(defun enqueue-eval-event: (function object)
114  "Add an eval event to the back of the queue.
115It will be the next event read after all pending events."
116  (setq unread-command-events
117	(nconc unread-command-events
118	       (list (list 'eval function object)))))
119
120(defun eval-event-p (obj)
121  "True if the argument is an eval or menu event object."
122  (eq (car-safe obj) 'eval))
123
124(defun event-button (event)
125  "Return the button-number of the given mouse-button-press event."
126  (let ((sym (car (get (car event) 'event-symbol-elements))))
127    (cdr (assq sym '((mouse-1 . 1) (mouse-2 . 2) (mouse-3 . 3)
128		     (mouse-4 . 4) (mouse-5 . 5))))))
129
130(defun event-function (event)
131  "Return the callback function of the given timeout, menu, or eval event."
132  (nth 1 event))
133
134(defun event-key (event)
135  "Returns the KeySym of the given key-press event.
136The value is an ASCII printing character (not upper case) or a symbol."
137  (if (symbolp event)
138      (car (get event 'event-symbol-elements))
139    (let ((base (logand event (1- (lsh 1 18)))))
140      (downcase (if (< base 32) (logior base 64) base)))))
141
142(defun event-object (event)
143  "Returns the function argument of the given timeout, menu, or eval event."
144  (nth 2 event))
145
146(defun event-point (event)
147  "Returns the character position of the given mouse-related event.
148If the event did not occur over a window, or did
149not occur over text, then this returns nil.  Otherwise, it returns an index
150into the buffer visible in the event's window."
151  (posn-point (event-end event)))
152
153(defun event-process (event)
154  "Returns the process of the given process-output event."
155  (nth 1 event))
156
157(defun event-timestamp (event)
158  "Returns the timestamp of the given event object.
159In Lucid Emacs, this works for any kind of event.
160In this emulation, it returns nil for non-mouse-related events."
161  (and (listp event)
162       (posn-timestamp (event-end event))))
163
164(defun event-to-character (event &optional lenient)
165  "Returns the closest ASCII approximation to the given event object.
166If the event isn't a keypress, this returns nil.
167If the second argument is non-nil, then this is lenient in its
168translation; it will ignore modifier keys other than control and meta,
169and will ignore the shift modifier on those characters which have no
170shifted ASCII equivalent (Control-Shift-A for example, will be mapped to
171the same ASCII code as Control-A.)  If the second arg is nil, then nil
172will be returned for events which have no direct ASCII equivalent."
173  (if (symbolp event)
174      (and lenient
175	   (cdr (assq event '((backspace . 8) (delete . 127) (tab . 9)
176			      (return . 10) (enter . 10)))))
177    ;; Our interpretation is, ASCII means anything a number can represent.
178    (if (integerp event)
179	event nil)))
180
181(defun event-window (event)
182  "Returns the window of the given mouse-related event object."
183  (posn-window (event-end event)))
184
185(defun event-x (event)
186  "Returns the X position in characters of the given mouse-related event."
187  (/ (car (posn-col-row (event-end event)))
188     (frame-char-width (window-frame (event-window event)))))
189
190(defun event-x-pixel (event)
191  "Returns the X position in pixels of the given mouse-related event."
192  (car (posn-col-row (event-end event))))
193
194(defun event-y (event)
195  "Returns the Y position in characters of the given mouse-related event."
196  (/ (cdr (posn-col-row (event-end event)))
197     (frame-char-height (window-frame (event-window event)))))
198
199(defun event-y-pixel (event)
200  "Returns the Y position in pixels of the given mouse-related event."
201  (cdr (posn-col-row (event-end event))))
202
203(defun key-press-event-p (obj)
204  "True if the argument is a keyboard event object."
205  (or (integerp obj)
206      (and (symbolp obj)
207	   (get obj 'event-symbol-elements))))
208
209(defun menu-event-p (obj)
210  "True if the argument is a menu event object."
211  (eq (car-safe obj) 'menu))
212
213(defun motion-event-p (obj)
214  "True if the argument is a mouse-motion event object."
215  (eq (car-safe obj) 'mouse-movement))
216
217(defun read-command-event ()
218  "Return the next keyboard or mouse event; execute other events.
219This is similar to the function `next-command-event' of Lucid Emacs,
220but different in that it returns the event rather than filling in
221an existing event object."
222  (let (event)
223    (while (progn
224	     (setq event (read-event))
225	     (not (or (key-press-event-p event)
226		      (button-press-event-p event)
227		      (button-release-event-p event)
228		      (menu-event-p event))))
229      (let ((type (car-safe event)))
230	(cond ((eq type 'eval)
231	       (funcall (nth 1 event) (nth 2 event)))
232	      ((eq type 'switch-frame)
233	       (select-frame (nth 1 event))))))
234    event))
235
236(defun process-event-p (obj)
237  "True if the argument is a process-output event object.
238GNU Emacs 19 does not currently generate process-output events."
239  (eq (car-safe obj) 'process))
240
241(defun timeout-event-p (obj)
242  "True if the argument is a timeout event object.
243GNU Emacs 19 does not currently generate timeout events."
244  (eq (car-safe obj) 'timeout))
245
246;;; levents.el ends here
247