]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/levents.el
(lisp-fill-paragraph): Several changes.
[gnu-emacs] / lisp / emacs-lisp / levents.el
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 (defun allocate-event ()
60 "Returns an empty event structure.
61 In this emulation, it returns nil."
62 nil)
63
64 (defun button-press-event-p (obj)
65 "True if the argument is a mouse-button-press event object."
66 (and (consp obj) (symbolp (car obj))
67 (memq 'down (get (car obj) 'event-symbol-elements))))
68
69 (defun button-release-event-p (obj)
70 "True if the argument is a mouse-button-release event object."
71 (and (consp obj) (symbolp (car obj))
72 (or (memq 'click (get (car obj) 'event-symbol-elements))
73 (memq 'drag (get (car obj) 'event-symbol-elements)))))
74
75 (defun character-to-event (ch &optional event)
76 "Converts a numeric ASCII value to an event structure, replete with
77 bucky bits. The character is the first argument, and the event to fill
78 in is the second. This function contains knowledge about what the codes
79 mean -- for example, the number 9 is converted to the character Tab,
80 not the distinct character Control-I.
81
82 Beware that character-to-event and event-to-character are not strictly
83 inverse functions, since events contain much more information than the
84 ASCII character set can encode."
85 ch)
86
87 (defun copy-event (event1 &optional event2)
88 "Make a copy of the given event object.
89 In this emulation, `copy-event' just returns its argument."
90 event1)
91
92 (defun deallocate-event (event)
93 "Allow the given event structure to be reused.
94 In actual Lucid Emacs, you MUST NOT use this event object after
95 calling this function with it. You will lose. It is not necessary to
96 call this function, as event objects are garbage- collected like all
97 other objects; however, it may be more efficient to explicitly
98 deallocate events when you are sure that that is safe.
99
100 This emulation does not actually deallocate or reuse events
101 except via garbage collection and `cons'."
102 nil)
103
104 (defun enqueue-eval-event: (function object)
105 "Add an eval event to the back of the queue.
106 It will be the next event read after all pending events."
107 (setq unread-command-events
108 (nconc unread-command-events
109 (list (list 'eval function object)))))
110
111 (defun eval-event-p (obj)
112 "True if the argument is an eval or menu event object."
113 (eq (car-safe obj) 'eval))
114
115 (defun event-button (event)
116 "Return the button-number of the given mouse-button-press event."
117 (let ((sym (car (get (car event) 'event-symbol-elements))))
118 (cdr (assq sym '((mouse-1 . 1) (mouse-2 . 2) (mouse-3 . 3)
119 (mouse-4 . 4) (mouse-5 . 5))))))
120
121 (defun event-function (event)
122 "Return the callback function of the given timeout, menu, or eval event."
123 (nth 1 event))
124
125 (defun event-key (event)
126 "Returns the KeySym of the given key-press event.
127 The value is an ASCII printing character (not upper case) or a symbol."
128 (if (symbolp event)
129 (car (get event 'event-symbol-elements))
130 (let ((base (logand event (1- (lsh 1 18)))))
131 (downcase (if (< base 32) (logior base 64) base)))))
132
133 (defun event-object (event)
134 "Returns the function argument of the given timeout, menu, or eval event."
135 (nth 2 event))
136
137 (defun event-point (event)
138 "Returns the character position of the given mouse-related event.
139 If the event did not occur over a window, or did
140 not occur over text, then this returns nil. Otherwise, it returns an index
141 into the buffer visible in the event's window."
142 (posn-point (event-end event)))
143
144 (defun event-process (event)
145 "Returns the process of the given process-output event."
146 (nth 1 event))
147
148 (defun event-timestamp (event)
149 "Returns the timestamp of the given event object.
150 In Lucid Emacs, this works for any kind of event.
151 In this emulation, it returns nil for non-mouse-related events."
152 (and (listp event)
153 (posn-timestamp (event-end event))))
154
155 (defun event-to-character (event &optional lenient)
156 "Returns the closest ASCII approximation to the given event object.
157 If the event isn't a keypress, this returns nil.
158 If the second argument is non-nil, then this is lenient in its
159 translation; it will ignore modifier keys other than control and meta,
160 and will ignore the shift modifier on those characters which have no
161 shifted ASCII equivalent (Control-Shift-A for example, will be mapped to
162 the same ASCII code as Control-A.) If the second arg is nil, then nil
163 will be returned for events which have no direct ASCII equivalent."
164 (if (symbolp event)
165 (and lenient
166 (cdr (assq event '((backspace . 8) (delete . 127) (tab . 9)
167 (return . 10) (enter . 10)))))
168 ;; Our interpretation is, ASCII means anything a number can represent.
169 (if (integerp event)
170 event nil)))
171
172 (defun event-window (event)
173 "Returns the window of the given mouse-related event object."
174 (posn-window (event-end event)))
175
176 (defun event-x (event)
177 "Returns the X position in characters of the given mouse-related event."
178 (/ (car (posn-col-row (event-end event)))
179 (frame-char-width (window-frame (event-window event)))))
180
181 (defun event-x-pixel (event)
182 "Returns the X position in pixels of the given mouse-related event."
183 (car (posn-col-row (event-end event))))
184
185 (defun event-y (event)
186 "Returns the Y position in characters of the given mouse-related event."
187 (/ (cdr (posn-col-row (event-end event)))
188 (frame-char-height (window-frame (event-window event)))))
189
190 (defun event-y-pixel (event)
191 "Returns the Y position in pixels of the given mouse-related event."
192 (cdr (posn-col-row (event-end event))))
193
194 (defun key-press-event-p (obj)
195 "True if the argument is a keyboard event object."
196 (or (integerp obj)
197 (and (symbolp obj)
198 (get obj 'event-symbol-elements))))
199
200 (defun menu-event-p (obj)
201 "True if the argument is a menu event object."
202 (eq (car-safe obj) 'menu))
203
204 (defun motion-event-p (obj)
205 "True if the argument is a mouse-motion event object."
206 (eq (car-safe obj) 'mouse-movement))
207
208 (defun read-command-event ()
209 "Return the next keyboard or mouse event; execute other events.
210 This is similar to the function `next-command-event' of Lucid Emacs,
211 but different in that it returns the event rather than filling in
212 an existing event object."
213 (let (event)
214 (while (progn
215 (setq event (read-event))
216 (not (or (key-press-event-p event)
217 (button-press-event-p event)
218 (button-release-event-p event)
219 (menu-event-p event))))
220 (let ((type (car-safe event)))
221 (cond ((eq type 'eval)
222 (funcall (nth 1 event) (nth 2 event)))
223 ((eq type 'switch-frame)
224 (select-frame (nth 1 event))))))
225 event))
226
227 (defun process-event-p (obj)
228 "True if the argument is a process-output event object.
229 GNU Emacs 19 does not currently generate process-output events."
230 (eq (car-safe obj) 'process))
231
232 (defun timeout-event-p (obj)
233 "True if the argument is a timeout event object.
234 GNU Emacs 19 does not currently generate timeout events."
235 (eq (car-safe obj) 'timeout))
236
237 ;;; levents.el ends here