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