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