Commit | Line | Data |
---|---|---|
284f4730 | 1 | /* Keyboard and mouse input; editor command loop. |
39aab679 DL |
2 | Copyright (C) 1985,86,87,88,89,93,94,95,96,97,99, 2000 |
3 | Free Software Foundation, Inc. | |
284f4730 JB |
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 | |
7b4aedb9 | 9 | the Free Software Foundation; either version 2, or (at your option) |
284f4730 JB |
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 | |
3b7ad313 EN |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
284f4730 | 21 | |
18160b98 | 22 | #include <config.h> |
68c45bf0 | 23 | #include <signal.h> |
284f4730 | 24 | #include <stdio.h> |
284f4730 JB |
25 | #include "termchar.h" |
26 | #include "termopts.h" | |
27 | #include "lisp.h" | |
28 | #include "termhooks.h" | |
29 | #include "macros.h" | |
02639609 | 30 | #include "keyboard.h" |
ff11dfa1 | 31 | #include "frame.h" |
284f4730 JB |
32 | #include "window.h" |
33 | #include "commands.h" | |
34 | #include "buffer.h" | |
37cd9f30 | 35 | #include "charset.h" |
284f4730 | 36 | #include "disptab.h" |
f4255cd1 | 37 | #include "dispextern.h" |
e39da3d7 | 38 | #include "syntax.h" |
497ba7a1 | 39 | #include "intervals.h" |
9ac0d9e0 | 40 | #include "blockinput.h" |
e8886a1d | 41 | #include "puresize.h" |
8a9f5d3c GM |
42 | #include "systime.h" |
43 | #include "atimer.h" | |
284f4730 JB |
44 | #include <setjmp.h> |
45 | #include <errno.h> | |
46 | ||
80e4aa30 RS |
47 | #ifdef MSDOS |
48 | #include "msdos.h" | |
49 | #include <time.h> | |
50 | #else /* not MSDOS */ | |
284f4730 JB |
51 | #ifndef VMS |
52 | #include <sys/ioctl.h> | |
284f4730 | 53 | #endif |
80e4aa30 | 54 | #endif /* not MSDOS */ |
284f4730 | 55 | |
52baf19e | 56 | #include "syssignal.h" |
6ef5b54f | 57 | #include "systty.h" |
52baf19e | 58 | |
03cee6ae GM |
59 | #include <sys/types.h> |
60 | #ifdef HAVE_UNISTD_H | |
61 | #include <unistd.h> | |
62 | #endif | |
63 | ||
c5e3b6c5 RS |
64 | /* This is to get the definitions of the XK_ symbols. */ |
65 | #ifdef HAVE_X_WINDOWS | |
66 | #include "xterm.h" | |
67 | #endif | |
68 | ||
e98a93eb GV |
69 | #ifdef HAVE_NTGUI |
70 | #include "w32term.h" | |
71 | #endif /* HAVE_NTGUI */ | |
72 | ||
1a578e9b AC |
73 | #ifdef macintosh |
74 | #include "macterm.h" | |
75 | #endif | |
76 | ||
0c2611c5 RS |
77 | /* Include systime.h after xterm.h to avoid double inclusion of time.h. */ |
78 | #include "systime.h" | |
79 | ||
02639609 | 80 | #ifndef USE_CRT_DLL |
52baf19e | 81 | extern int errno; |
02639609 | 82 | #endif |
52baf19e | 83 | |
9ac0d9e0 JB |
84 | /* Variables for blockinput.h: */ |
85 | ||
86 | /* Non-zero if interrupt input is blocked right now. */ | |
63927c41 | 87 | int interrupt_input_blocked; |
9ac0d9e0 JB |
88 | |
89 | /* Nonzero means an input interrupt has arrived | |
90 | during the current critical section. */ | |
63927c41 | 91 | int interrupt_input_pending; |
9ac0d9e0 JB |
92 | |
93 | ||
437f6112 RS |
94 | /* File descriptor to use for input. */ |
95 | extern int input_fd; | |
284f4730 | 96 | |
e98a93eb | 97 | #ifdef HAVE_WINDOW_SYSTEM |
284f4730 | 98 | /* Make all keyboard buffers much bigger when using X windows. */ |
f019cded | 99 | #ifdef macintosh |
1a578e9b | 100 | /* But not too big (local data > 32K error) if on macintosh. */ |
f019cded RS |
101 | #define KBD_BUFFER_SIZE 512 |
102 | #else | |
284f4730 | 103 | #define KBD_BUFFER_SIZE 4096 |
f019cded | 104 | #endif |
284f4730 JB |
105 | #else /* No X-windows, character input */ |
106 | #define KBD_BUFFER_SIZE 256 | |
107 | #endif /* No X-windows */ | |
108 | ||
109 | /* Following definition copied from eval.c */ | |
110 | ||
111 | struct backtrace | |
112 | { | |
113 | struct backtrace *next; | |
114 | Lisp_Object *function; | |
115 | Lisp_Object *args; /* Points to vector of args. */ | |
116 | int nargs; /* length of vector. If nargs is UNEVALLED, | |
117 | args points to slot holding list of | |
118 | unevalled args */ | |
119 | char evalargs; | |
120 | }; | |
121 | ||
c5fdd383 KH |
122 | #ifdef MULTI_KBOARD |
123 | KBOARD *initial_kboard; | |
124 | KBOARD *current_kboard; | |
125 | KBOARD *all_kboards; | |
1e8bd3da | 126 | int single_kboard; |
6c6083a9 | 127 | #else |
c5fdd383 | 128 | KBOARD the_only_kboard; |
6c6083a9 | 129 | #endif |
612b78ef | 130 | |
284f4730 JB |
131 | /* Non-nil disable property on a command means |
132 | do not execute it; call disabled-command-hook's value instead. */ | |
2e894dab | 133 | Lisp_Object Qdisabled, Qdisabled_command_hook; |
284f4730 JB |
134 | |
135 | #define NUM_RECENT_KEYS (100) | |
136 | int recent_keys_index; /* Index for storing next element into recent_keys */ | |
137 | int total_keys; /* Total number of elements stored into recent_keys */ | |
5160df46 | 138 | Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */ |
284f4730 | 139 | |
6569cc8d JB |
140 | /* Vector holding the key sequence that invoked the current command. |
141 | It is reused for each command, and it may be longer than the current | |
142 | sequence; this_command_key_count indicates how many elements | |
143 | actually mean something. | |
144 | It's easier to staticpro a single Lisp_Object than an array. */ | |
145 | Lisp_Object this_command_keys; | |
146 | int this_command_key_count; | |
284f4730 | 147 | |
7d18f9ae RS |
148 | /* This vector is used as a buffer to record the events that were actually read |
149 | by read_key_sequence. */ | |
150 | Lisp_Object raw_keybuf; | |
151 | int raw_keybuf_count; | |
152 | ||
153 | #define GROW_RAW_KEYBUF \ | |
154 | if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \ | |
155 | { \ | |
156 | int newsize = 2 * XVECTOR (raw_keybuf)->size; \ | |
157 | Lisp_Object new; \ | |
158 | new = Fmake_vector (make_number (newsize), Qnil); \ | |
159 | bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \ | |
160 | raw_keybuf_count * sizeof (Lisp_Object)); \ | |
161 | raw_keybuf = new; \ | |
162 | } | |
163 | ||
6321824f RS |
164 | /* Number of elements of this_command_keys |
165 | that precede this key sequence. */ | |
166 | int this_single_command_key_start; | |
167 | ||
71918b75 RS |
168 | /* Record values of this_command_key_count and echo_length () |
169 | before this command was read. */ | |
170 | static int before_command_key_count; | |
171 | static int before_command_echo_length; | |
172 | /* Values of before_command_key_count and before_command_echo_length | |
173 | saved by reset-this-command-lengths. */ | |
174 | static int before_command_key_count_1; | |
175 | static int before_command_echo_length_1; | |
176 | /* Flag set by reset-this-command-lengths, | |
177 | saying to reset the lengths when add_command_key is called. */ | |
178 | static int before_command_restore_flag; | |
179 | ||
284f4730 JB |
180 | extern int minbuf_level; |
181 | ||
a59b172a RS |
182 | extern int message_enable_multibyte; |
183 | ||
284f4730 JB |
184 | extern struct backtrace *backtrace_list; |
185 | ||
7ee32cda GM |
186 | /* If non-nil, the function that implements the display of help. |
187 | It's called with one argument, the help string to display. */ | |
188 | ||
189 | Lisp_Object Vshow_help_function; | |
190 | ||
284f4730 JB |
191 | /* Nonzero means do menu prompting. */ |
192 | static int menu_prompting; | |
193 | ||
194 | /* Character to see next line of menu prompt. */ | |
195 | static Lisp_Object menu_prompt_more_char; | |
196 | ||
197 | /* For longjmp to where kbd input is being done. */ | |
198 | static jmp_buf getcjmp; | |
199 | ||
200 | /* True while doing kbd input. */ | |
201 | int waiting_for_input; | |
202 | ||
203 | /* True while displaying for echoing. Delays C-g throwing. */ | |
985f9f66 | 204 | |
284f4730 JB |
205 | static int echoing; |
206 | ||
985f9f66 GM |
207 | /* Non-null means we can start echoing at the next input pause even |
208 | though there is something in the echo area. */ | |
209 | ||
210 | static struct kboard *ok_to_echo_at_next_pause; | |
211 | ||
59a84f8e GM |
212 | /* The kboard last echoing, or null for none. Reset to 0 in |
213 | cancel_echoing. If non-null, and a current echo area message | |
214 | exists, and echo_message_buffer is eq to the current message | |
215 | buffer, we know that the message comes from echo_kboard. */ | |
985f9f66 GM |
216 | |
217 | static struct kboard *echo_kboard; | |
1fc93d49 | 218 | |
59a84f8e GM |
219 | /* The buffer used for echoing. Set in echo_now, reset in |
220 | cancel_echoing. */ | |
221 | ||
222 | static Lisp_Object echo_message_buffer; | |
223 | ||
03361bcc RS |
224 | /* Nonzero means disregard local maps for the menu bar. */ |
225 | static int inhibit_local_menu_bar_menus; | |
226 | ||
80e4aa30 | 227 | /* Nonzero means C-g should cause immediate error-signal. */ |
284f4730 JB |
228 | int immediate_quit; |
229 | ||
fa90970d RS |
230 | /* The user's ERASE setting. */ |
231 | Lisp_Object Vtty_erase_char; | |
232 | ||
284f4730 | 233 | /* Character to recognize as the help char. */ |
7e85b935 | 234 | Lisp_Object Vhelp_char; |
284f4730 | 235 | |
ecb7cb34 KH |
236 | /* List of other event types to recognize as meaning "help". */ |
237 | Lisp_Object Vhelp_event_list; | |
238 | ||
284f4730 JB |
239 | /* Form to execute when help char is typed. */ |
240 | Lisp_Object Vhelp_form; | |
241 | ||
7e85b935 RS |
242 | /* Command to run when the help character follows a prefix key. */ |
243 | Lisp_Object Vprefix_help_command; | |
244 | ||
9f9c0e27 RS |
245 | /* List of items that should move to the end of the menu bar. */ |
246 | Lisp_Object Vmenu_bar_final_items; | |
a73c5e29 | 247 | |
6526ab49 RS |
248 | /* Non-nil means show the equivalent key-binding for |
249 | any M-x command that has one. | |
250 | The value can be a length of time to show the message for. | |
251 | If the value is non-nil and not a number, we wait 2 seconds. */ | |
252 | Lisp_Object Vsuggest_key_bindings; | |
253 | ||
00392ce6 MB |
254 | /* How long to display an echo-area message when the minibuffer is active. |
255 | If the value is not a number, such messages don't time out. */ | |
256 | Lisp_Object Vminibuffer_message_timeout; | |
257 | ||
284f4730 JB |
258 | /* Character that causes a quit. Normally C-g. |
259 | ||
260 | If we are running on an ordinary terminal, this must be an ordinary | |
261 | ASCII char, since we want to make it our interrupt character. | |
262 | ||
263 | If we are not running on an ordinary terminal, it still needs to be | |
264 | an ordinary ASCII char. This character needs to be recognized in | |
265 | the input interrupt handler. At this point, the keystroke is | |
266 | represented as a struct input_event, while the desired quit | |
267 | character is specified as a lispy event. The mapping from struct | |
268 | input_events to lispy events cannot run in an interrupt handler, | |
269 | and the reverse mapping is difficult for anything but ASCII | |
270 | keystrokes. | |
271 | ||
272 | FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an | |
273 | ASCII character. */ | |
274 | int quit_char; | |
275 | ||
276 | extern Lisp_Object current_global_map; | |
277 | extern int minibuf_level; | |
278 | ||
9dd3131c RS |
279 | /* If non-nil, this is a map that overrides all other local maps. */ |
280 | Lisp_Object Voverriding_local_map; | |
281 | ||
d0a49716 RS |
282 | /* If non-nil, Voverriding_local_map applies to the menu bar. */ |
283 | Lisp_Object Voverriding_local_map_menu_flag; | |
284 | ||
7f07d5ca RS |
285 | /* Keymap that defines special misc events that should |
286 | be processed immediately at a low level. */ | |
287 | Lisp_Object Vspecial_event_map; | |
288 | ||
284f4730 JB |
289 | /* Current depth in recursive edits. */ |
290 | int command_loop_level; | |
291 | ||
292 | /* Total number of times command_loop has read a key sequence. */ | |
293 | int num_input_keys; | |
294 | ||
295 | /* Last input character read as a command. */ | |
296 | Lisp_Object last_command_char; | |
297 | ||
7d6de002 RS |
298 | /* Last input character read as a command, not counting menus |
299 | reached by the mouse. */ | |
300 | Lisp_Object last_nonmenu_event; | |
301 | ||
284f4730 JB |
302 | /* Last input character read for any purpose. */ |
303 | Lisp_Object last_input_char; | |
304 | ||
dbc4e1c1 | 305 | /* If not Qnil, a list of objects to be read as subsequent command input. */ |
24597608 | 306 | Lisp_Object Vunread_command_events; |
284f4730 | 307 | |
7d18f9ae RS |
308 | /* If not Qnil, a list of objects to be read as subsequent command input |
309 | including input method processing. */ | |
310 | Lisp_Object Vunread_input_method_events; | |
311 | ||
312 | /* If not Qnil, a list of objects to be read as subsequent command input | |
313 | but NOT including input method processing. */ | |
314 | Lisp_Object Vunread_post_input_method_events; | |
315 | ||
86e5706b RS |
316 | /* If not -1, an event to be read as subsequent command input. */ |
317 | int unread_command_char; | |
318 | ||
cd21b839 JB |
319 | /* If not Qnil, this is a switch-frame event which we decided to put |
320 | off until the end of a key sequence. This should be read as the | |
dbc4e1c1 | 321 | next command input, after any unread_command_events. |
8f805655 JB |
322 | |
323 | read_key_sequence uses this to delay switch-frame events until the | |
324 | end of the key sequence; Fread_char uses it to put off switch-frame | |
325 | events until a non-ASCII event is acceptable as input. */ | |
326 | Lisp_Object unread_switch_frame; | |
cd21b839 | 327 | |
9fa4395d RS |
328 | /* A mask of extra modifier bits to put into every keyboard char. */ |
329 | int extra_keyboard_modifiers; | |
330 | ||
284f4730 JB |
331 | /* Char to use as prefix when a meta character is typed in. |
332 | This is bound on entry to minibuffer in case ESC is changed there. */ | |
333 | ||
334 | Lisp_Object meta_prefix_char; | |
335 | ||
336 | /* Last size recorded for a current buffer which is not a minibuffer. */ | |
337 | static int last_non_minibuf_size; | |
338 | ||
06ef7355 | 339 | /* Number of idle seconds before an auto-save and garbage collection. */ |
284f4730 JB |
340 | static Lisp_Object Vauto_save_timeout; |
341 | ||
342 | /* Total number of times read_char has returned. */ | |
4abfba1f | 343 | int num_input_events; |
284f4730 | 344 | |
51172b6d | 345 | /* Total number of times read_char has returned, outside of macros. */ |
c43b1734 | 346 | int num_nonmacro_input_events; |
51172b6d | 347 | |
284f4730 JB |
348 | /* Auto-save automatically when this many characters have been typed |
349 | since the last time. */ | |
350 | ||
351 | static int auto_save_interval; | |
352 | ||
c43b1734 | 353 | /* Value of num_nonmacro_input_events as of last auto save. */ |
284f4730 JB |
354 | |
355 | int last_auto_save; | |
356 | ||
284f4730 | 357 | /* The command being executed by the command loop. |
6c7178b9 KH |
358 | Commands may set this, and the value set will be copied into |
359 | current_kboard->Vlast_command instead of the actual command. */ | |
d5eecefb RS |
360 | Lisp_Object Vthis_command; |
361 | ||
362 | /* This is like Vthis_command, except that commands never set it. */ | |
363 | Lisp_Object real_this_command; | |
284f4730 | 364 | |
b453f72e KH |
365 | /* The value of point when the last command was executed. */ |
366 | int last_point_position; | |
367 | ||
047688cb RS |
368 | /* The buffer that was current when the last command was started. */ |
369 | Lisp_Object last_point_position_buffer; | |
370 | ||
4c52b668 KH |
371 | /* The frame in which the last input event occurred, or Qmacro if the |
372 | last event came from a macro. We use this to determine when to | |
373 | generate switch-frame events. This may be cleared by functions | |
374 | like Fselect_frame, to make sure that a switch-frame event is | |
375 | generated by the next character. */ | |
376 | Lisp_Object internal_last_event_frame; | |
4c52b668 KH |
377 | |
378 | /* A user-visible version of the above, intended to allow users to | |
379 | figure out where the last event came from, if the event doesn't | |
380 | carry that information itself (i.e. if it was a character). */ | |
381 | Lisp_Object Vlast_event_frame; | |
382 | ||
1113d9db JB |
383 | /* The timestamp of the last input event we received from the X server. |
384 | X Windows wants this for selection ownership. */ | |
284f4730 JB |
385 | unsigned long last_event_timestamp; |
386 | ||
387 | Lisp_Object Qself_insert_command; | |
388 | Lisp_Object Qforward_char; | |
389 | Lisp_Object Qbackward_char; | |
e58aa385 | 390 | Lisp_Object Qundefined; |
d925fb39 | 391 | Lisp_Object Qtimer_event_handler; |
284f4730 JB |
392 | |
393 | /* read_key_sequence stores here the command definition of the | |
394 | key sequence that it reads. */ | |
395 | Lisp_Object read_key_sequence_cmd; | |
396 | ||
39aab679 DL |
397 | /* Echo unfinished commands after this many seconds of pause. */ |
398 | Lisp_Object Vecho_keystrokes; | |
399 | ||
284f4730 JB |
400 | /* Form to evaluate (if non-nil) when Emacs is started. */ |
401 | Lisp_Object Vtop_level; | |
402 | ||
403 | /* User-supplied string to translate input characters through. */ | |
404 | Lisp_Object Vkeyboard_translate_table; | |
405 | ||
406 | /* Keymap mapping ASCII function key sequences onto their preferred forms. */ | |
407 | extern Lisp_Object Vfunction_key_map; | |
408 | ||
e0301c07 RS |
409 | /* Another keymap that maps key sequences into key sequences. |
410 | This one takes precedence over ordinary definitions. */ | |
411 | extern Lisp_Object Vkey_translation_map; | |
a612e298 | 412 | |
7d18f9ae RS |
413 | /* If non-nil, this implements the current input method. */ |
414 | Lisp_Object Vinput_method_function; | |
415 | Lisp_Object Qinput_method_function; | |
416 | ||
d5eecefb RS |
417 | /* When we call Vinput_method_function, |
418 | this holds the echo area message that was just erased. */ | |
419 | Lisp_Object Vinput_method_previous_message; | |
420 | ||
86e5706b RS |
421 | /* Non-nil means deactivate the mark at end of this command. */ |
422 | Lisp_Object Vdeactivate_mark; | |
423 | ||
48e416d4 RS |
424 | /* Menu bar specified in Lucid Emacs fashion. */ |
425 | ||
426 | Lisp_Object Vlucid_menu_bar_dirty_flag; | |
427 | Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook; | |
428 | ||
cdb9d665 RS |
429 | Lisp_Object Qecho_area_clear_hook; |
430 | ||
86e5706b | 431 | /* Hooks to run before and after each command. */ |
59aadc81 RS |
432 | Lisp_Object Qpre_command_hook, Vpre_command_hook; |
433 | Lisp_Object Qpost_command_hook, Vpost_command_hook; | |
40932d1a | 434 | Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal; |
59aadc81 RS |
435 | /* Hook run after a command if there's no more input soon. */ |
436 | Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook; | |
437 | ||
438 | /* Delay time in microseconds before running post-command-idle-hook. */ | |
439 | int post_command_idle_delay; | |
86e5706b | 440 | |
8a792f3a RS |
441 | /* List of deferred actions to be performed at a later time. |
442 | The precise format isn't relevant here; we just check whether it is nil. */ | |
443 | Lisp_Object Vdeferred_action_list; | |
444 | ||
445 | /* Function to call to handle deferred actions, when there are any. */ | |
446 | Lisp_Object Vdeferred_action_function; | |
3ef14e46 | 447 | Lisp_Object Qdeferred_action_function; |
8a792f3a | 448 | |
d5eecefb RS |
449 | Lisp_Object Qinput_method_exit_on_first_char; |
450 | Lisp_Object Qinput_method_use_echo_area; | |
451 | ||
284f4730 JB |
452 | /* File in which we write all commands we read. */ |
453 | FILE *dribble; | |
454 | ||
455 | /* Nonzero if input is available. */ | |
456 | int input_pending; | |
457 | ||
b04904fb RS |
458 | /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should |
459 | keep 0200 bit in input chars. 0 to ignore the 0200 bit. */ | |
460 | ||
284f4730 JB |
461 | int meta_key; |
462 | ||
3626fb1a GM |
463 | /* Non-zero means force key bindings update in parse_menu_item. */ |
464 | ||
465 | int update_menu_bindings; | |
466 | ||
284f4730 JB |
467 | extern char *pending_malloc_warning; |
468 | ||
beecf6a1 | 469 | /* Circular buffer for pre-read keyboard input. */ |
da8f7368 | 470 | |
beecf6a1 KH |
471 | static struct input_event kbd_buffer[KBD_BUFFER_SIZE]; |
472 | ||
da8f7368 | 473 | /* Vector to GCPRO the Lisp objects referenced from kbd_buffer. |
beecf6a1 KH |
474 | |
475 | The interrupt-level event handlers will never enqueue an event on a | |
476 | frame which is not in Vframe_list, and once an event is dequeued, | |
477 | internal_last_event_frame or the event itself points to the frame. | |
478 | So that's all fine. | |
479 | ||
480 | But while the event is sitting in the queue, it's completely | |
481 | unprotected. Suppose the user types one command which will run for | |
482 | a while and then delete a frame, and then types another event at | |
483 | the frame that will be deleted, before the command gets around to | |
484 | it. Suppose there are no references to this frame elsewhere in | |
485 | Emacs, and a GC occurs before the second event is dequeued. Now we | |
486 | have an event referring to a freed frame, which will crash Emacs | |
487 | when it is dequeued. | |
488 | ||
489 | Similar things happen when an event on a scroll bar is enqueued; the | |
490 | window may be deleted while the event is in the queue. | |
491 | ||
da8f7368 GM |
492 | So, we use this vector to protect the Lisp_Objects in the event |
493 | queue. That way, they'll be dequeued as dead frames or windows, | |
494 | but still valid Lisp objects. | |
beecf6a1 KH |
495 | |
496 | If kbd_buffer[i].kind != no_event, then | |
da8f7368 GM |
497 | |
498 | AREF (kbd_buffer_gcpro, 2 * i) == kbd_buffer[i].frame_or_window. | |
499 | AREF (kbd_buffer_gcpro, 2 * i + 1) == kbd_buffer[i].arg. */ | |
500 | ||
501 | static Lisp_Object kbd_buffer_gcpro; | |
beecf6a1 KH |
502 | |
503 | /* Pointer to next available character in kbd_buffer. | |
504 | If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty. | |
505 | This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the | |
506 | next available char is in kbd_buffer[0]. */ | |
507 | static struct input_event *kbd_fetch_ptr; | |
508 | ||
509 | /* Pointer to next place to store character in kbd_buffer. This | |
510 | may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next | |
511 | character should go in kbd_buffer[0]. */ | |
7ee32cda | 512 | static struct input_event * volatile kbd_store_ptr; |
beecf6a1 KH |
513 | |
514 | /* The above pair of variables forms a "queue empty" flag. When we | |
515 | enqueue a non-hook event, we increment kbd_store_ptr. When we | |
516 | dequeue a non-hook event, we increment kbd_fetch_ptr. We say that | |
517 | there is input available iff the two pointers are not equal. | |
518 | ||
519 | Why not just have a flag set and cleared by the enqueuing and | |
520 | dequeuing functions? Such a flag could be screwed up by interrupts | |
521 | at inopportune times. */ | |
522 | ||
f3253854 | 523 | /* If this flag is non-nil, we check mouse_moved to see when the |
a9d77f1f RS |
524 | mouse moves, and motion events will appear in the input stream. |
525 | Otherwise, mouse motion is ignored. */ | |
526 | static Lisp_Object do_mouse_tracking; | |
284f4730 | 527 | |
284f4730 JB |
528 | /* Symbols to head events. */ |
529 | Lisp_Object Qmouse_movement; | |
3c370943 | 530 | Lisp_Object Qscroll_bar_movement; |
cd21b839 | 531 | Lisp_Object Qswitch_frame; |
bbdc2092 | 532 | Lisp_Object Qdelete_frame; |
af17bd2b KH |
533 | Lisp_Object Qiconify_frame; |
534 | Lisp_Object Qmake_frame_visible; | |
7ee32cda | 535 | Lisp_Object Qhelp_echo; |
cd21b839 | 536 | |
284f4730 JB |
537 | /* Symbols to denote kinds of events. */ |
538 | Lisp_Object Qfunction_key; | |
539 | Lisp_Object Qmouse_click; | |
07de30b9 GV |
540 | #ifdef WINDOWSNT |
541 | Lisp_Object Qmouse_wheel; | |
1161d367 | 542 | Lisp_Object Qlanguage_change; |
07de30b9 | 543 | #endif |
a24dc617 | 544 | Lisp_Object Qdrag_n_drop; |
284f4730 | 545 | /* Lisp_Object Qmouse_movement; - also an event header */ |
284f4730 JB |
546 | |
547 | /* Properties of event headers. */ | |
548 | Lisp_Object Qevent_kind; | |
88cb0656 | 549 | Lisp_Object Qevent_symbol_elements; |
284f4730 | 550 | |
e8886a1d RS |
551 | /* menu item parts */ |
552 | Lisp_Object Qmenu_alias; | |
598a9fa7 | 553 | Lisp_Object Qmenu_enable; |
74c1de23 RS |
554 | Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence; |
555 | Lisp_Object QCbutton, QCtoggle, QCradio; | |
e8886a1d RS |
556 | extern Lisp_Object Vdefine_key_rebound_commands; |
557 | extern Lisp_Object Qmenu_item; | |
598a9fa7 | 558 | |
0a7f1fc0 JB |
559 | /* An event header symbol HEAD may have a property named |
560 | Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS); | |
561 | BASE is the base, unmodified version of HEAD, and MODIFIERS is the | |
562 | mask of modifiers applied to it. If present, this is used to help | |
563 | speed up parse_modifiers. */ | |
564 | Lisp_Object Qevent_symbol_element_mask; | |
565 | ||
566 | /* An unmodified event header BASE may have a property named | |
567 | Qmodifier_cache, which is an alist mapping modifier masks onto | |
568 | modified versions of BASE. If present, this helps speed up | |
569 | apply_modifiers. */ | |
570 | Lisp_Object Qmodifier_cache; | |
571 | ||
5ec75a55 | 572 | /* Symbols to use for parts of windows. */ |
284f4730 | 573 | Lisp_Object Qmode_line; |
e5d77022 | 574 | Lisp_Object Qvertical_line; |
3c370943 | 575 | Lisp_Object Qvertical_scroll_bar; |
5ec75a55 RS |
576 | Lisp_Object Qmenu_bar; |
577 | ||
f4255cd1 JB |
578 | Lisp_Object recursive_edit_unwind (), command_loop (); |
579 | Lisp_Object Fthis_command_keys (); | |
03b4122a | 580 | Lisp_Object Qextended_command_history; |
c04cbc3b | 581 | EMACS_TIME timer_check (); |
284f4730 | 582 | |
f4385381 RS |
583 | extern Lisp_Object Vhistory_length; |
584 | ||
2c834fb3 KH |
585 | extern char *x_get_keysym_name (); |
586 | ||
8eb4d8ef RS |
587 | static void record_menu_key (); |
588 | ||
f4eef8b4 RS |
589 | Lisp_Object Qpolling_period; |
590 | ||
d9d4c147 | 591 | /* List of absolute timers. Appears in order of next scheduled event. */ |
c04cbc3b RS |
592 | Lisp_Object Vtimer_list; |
593 | ||
d9d4c147 KH |
594 | /* List of idle time timers. Appears in order of next scheduled event. */ |
595 | Lisp_Object Vtimer_idle_list; | |
596 | ||
87dd9b9b RS |
597 | /* Incremented whenever a timer is run. */ |
598 | int timers_run; | |
599 | ||
a9f16aa9 KH |
600 | extern Lisp_Object Vprint_level, Vprint_length; |
601 | ||
ffd56f97 JB |
602 | /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt |
603 | happens. */ | |
604 | EMACS_TIME *input_available_clear_time; | |
284f4730 JB |
605 | |
606 | /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode. | |
607 | Default is 1 if INTERRUPT_INPUT is defined. */ | |
608 | int interrupt_input; | |
609 | ||
610 | /* Nonzero while interrupts are temporarily deferred during redisplay. */ | |
611 | int interrupts_deferred; | |
612 | ||
87dd9b9b | 613 | /* Nonzero means use ^S/^Q for flow control. */ |
284f4730 JB |
614 | int flow_control; |
615 | ||
284f4730 JB |
616 | /* Allow m- file to inhibit use of FIONREAD. */ |
617 | #ifdef BROKEN_FIONREAD | |
618 | #undef FIONREAD | |
619 | #endif | |
620 | ||
621 | /* We are unable to use interrupts if FIONREAD is not available, | |
622 | so flush SIGIO so we won't try. */ | |
623 | #ifndef FIONREAD | |
624 | #ifdef SIGIO | |
625 | #undef SIGIO | |
626 | #endif | |
627 | #endif | |
628 | ||
e98a93eb | 629 | /* If we support a window system, turn on the code to poll periodically |
34f04431 | 630 | to detect C-g. It isn't actually used when doing interrupt input. */ |
e98a93eb | 631 | #ifdef HAVE_WINDOW_SYSTEM |
284f4730 JB |
632 | #define POLL_FOR_INPUT |
633 | #endif | |
adf5cb9c KH |
634 | |
635 | /* After a command is executed, if point is moved into a region that | |
636 | has specific properties (e.g. composition, display), we adjust | |
637 | point to the boundary of the region. But, if a command sets this | |
638 | valiable to non-nil, we suppress this point adjustment. This | |
639 | variable is set to nil before reading a command. */ | |
da8f7368 | 640 | |
adf5cb9c KH |
641 | Lisp_Object Vdisable_point_adjustment; |
642 | ||
643 | /* If non-nil, always disable point adjustment. */ | |
da8f7368 | 644 | |
adf5cb9c KH |
645 | Lisp_Object Vglobal_disable_point_adjustment; |
646 | ||
284f4730 JB |
647 | \f |
648 | /* Global variable declarations. */ | |
649 | ||
650 | /* Function for init_keyboard to call with no args (if nonzero). */ | |
651 | void (*keyboard_init_hook) (); | |
652 | ||
0bbfdc25 GM |
653 | static int read_avail_input P_ ((int)); |
654 | static void get_input_pending P_ ((int *, int)); | |
655 | static int readable_events P_ ((int)); | |
656 | static Lisp_Object read_char_x_menu_prompt P_ ((int, Lisp_Object *, | |
657 | Lisp_Object, int *)); | |
8150596a | 658 | static Lisp_Object read_char_x_menu_prompt (); |
0bbfdc25 GM |
659 | static Lisp_Object read_char_minibuf_menu_prompt P_ ((int, int, |
660 | Lisp_Object *)); | |
661 | static Lisp_Object make_lispy_event P_ ((struct input_event *)); | |
514354e9 | 662 | #ifdef HAVE_MOUSE |
0bbfdc25 GM |
663 | static Lisp_Object make_lispy_movement P_ ((struct frame *, Lisp_Object, |
664 | enum scroll_bar_part, | |
665 | Lisp_Object, Lisp_Object, | |
666 | unsigned long)); | |
514354e9 | 667 | #endif |
0bbfdc25 GM |
668 | static Lisp_Object modify_event_symbol P_ ((int, unsigned, Lisp_Object, |
669 | Lisp_Object, char **, | |
670 | Lisp_Object *, unsigned)); | |
671 | static Lisp_Object make_lispy_switch_frame P_ ((Lisp_Object)); | |
672 | static int parse_solitary_modifier P_ ((Lisp_Object)); | |
3d31316f | 673 | static int parse_solitary_modifier (); |
0bbfdc25 | 674 | static void save_getcjmp P_ ((jmp_buf)); |
dfcf069d | 675 | static void save_getcjmp (); |
0bbfdc25 | 676 | static void restore_getcjmp P_ ((jmp_buf)); |
7ee32cda | 677 | static Lisp_Object apply_modifiers P_ ((int, Lisp_Object)); |
0bbfdc25 | 678 | static void clear_event P_ ((struct input_event *)); |
284f4730 | 679 | |
8026024c KH |
680 | /* Nonzero means don't try to suspend even if the operating system seems |
681 | to support it. */ | |
682 | static int cannot_suspend; | |
683 | ||
284f4730 JB |
684 | #define min(a,b) ((a)<(b)?(a):(b)) |
685 | #define max(a,b) ((a)>(b)?(a):(b)) | |
686 | ||
687 | /* Install the string STR as the beginning of the string of echoing, | |
688 | so that it serves as a prompt for the next character. | |
689 | Also start echoing. */ | |
690 | ||
dfcf069d | 691 | void |
284f4730 JB |
692 | echo_prompt (str) |
693 | char *str; | |
694 | { | |
695 | int len = strlen (str); | |
7a80a6f6 | 696 | |
ba72822c KH |
697 | if (len > ECHOBUFSIZE - 4) |
698 | len = ECHOBUFSIZE - 4; | |
c5fdd383 KH |
699 | bcopy (str, current_kboard->echobuf, len); |
700 | current_kboard->echoptr = current_kboard->echobuf + len; | |
701 | *current_kboard->echoptr = '\0'; | |
284f4730 | 702 | |
c5fdd383 | 703 | current_kboard->echo_after_prompt = len; |
7a80a6f6 | 704 | |
3dbd9ee4 | 705 | echo_now (); |
284f4730 JB |
706 | } |
707 | ||
df0f2ba1 | 708 | /* Add C to the echo string, if echoing is going on. |
284f4730 JB |
709 | C can be a character, which is printed prettily ("M-C-x" and all that |
710 | jazz), or a symbol, whose name is printed. */ | |
711 | ||
dfcf069d | 712 | void |
284f4730 JB |
713 | echo_char (c) |
714 | Lisp_Object c; | |
715 | { | |
716 | extern char *push_key_description (); | |
717 | ||
c5fdd383 | 718 | if (current_kboard->immediate_echo) |
284f4730 | 719 | { |
c5fdd383 | 720 | char *ptr = current_kboard->echoptr; |
df0f2ba1 | 721 | |
c5fdd383 | 722 | if (ptr != current_kboard->echobuf) |
284f4730 JB |
723 | *ptr++ = ' '; |
724 | ||
725 | /* If someone has passed us a composite event, use its head symbol. */ | |
88cb0656 | 726 | c = EVENT_HEAD (c); |
284f4730 | 727 | |
8c18cbfb | 728 | if (INTEGERP (c)) |
284f4730 | 729 | { |
2d0ffc9a KH |
730 | if (ptr - current_kboard->echobuf |
731 | > ECHOBUFSIZE - KEY_DESCRIPTION_SIZE) | |
284f4730 JB |
732 | return; |
733 | ||
cb5df6ae | 734 | ptr = push_key_description (XINT (c), ptr); |
284f4730 | 735 | } |
8c18cbfb | 736 | else if (SYMBOLP (c)) |
284f4730 JB |
737 | { |
738 | struct Lisp_String *name = XSYMBOL (c)->name; | |
fc932ac6 | 739 | if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4 |
301738ed | 740 | > ECHOBUFSIZE) |
284f4730 | 741 | return; |
fc932ac6 RS |
742 | bcopy (name->data, ptr, STRING_BYTES (name)); |
743 | ptr += STRING_BYTES (name); | |
284f4730 JB |
744 | } |
745 | ||
c5fdd383 | 746 | if (current_kboard->echoptr == current_kboard->echobuf |
ecb7cb34 | 747 | && help_char_p (c)) |
284f4730 JB |
748 | { |
749 | strcpy (ptr, " (Type ? for further options)"); | |
750 | ptr += strlen (ptr); | |
751 | } | |
752 | ||
753 | *ptr = 0; | |
c5fdd383 | 754 | current_kboard->echoptr = ptr; |
284f4730 | 755 | |
3dbd9ee4 | 756 | echo_now (); |
284f4730 JB |
757 | } |
758 | } | |
759 | ||
760 | /* Temporarily add a dash to the end of the echo string if it's not | |
761 | empty, so that it serves as a mini-prompt for the very next character. */ | |
762 | ||
dfcf069d | 763 | void |
284f4730 JB |
764 | echo_dash () |
765 | { | |
c5fdd383 KH |
766 | if (!current_kboard->immediate_echo |
767 | && current_kboard->echoptr == current_kboard->echobuf) | |
284f4730 | 768 | return; |
7a80a6f6 | 769 | /* Do nothing if we just printed a prompt. */ |
c5fdd383 KH |
770 | if (current_kboard->echo_after_prompt |
771 | == current_kboard->echoptr - current_kboard->echobuf) | |
7a80a6f6 | 772 | return; |
4bafa972 | 773 | /* Do nothing if not echoing at all. */ |
c5fdd383 | 774 | if (current_kboard->echoptr == 0) |
4bafa972 | 775 | return; |
284f4730 JB |
776 | |
777 | /* Put a dash at the end of the buffer temporarily, | |
778 | but make it go away when the next character is added. */ | |
c5fdd383 KH |
779 | current_kboard->echoptr[0] = '-'; |
780 | current_kboard->echoptr[1] = 0; | |
284f4730 | 781 | |
3dbd9ee4 | 782 | echo_now (); |
284f4730 JB |
783 | } |
784 | ||
785 | /* Display the current echo string, and begin echoing if not already | |
786 | doing so. */ | |
787 | ||
07a59269 | 788 | void |
3dbd9ee4 | 789 | echo_now () |
284f4730 | 790 | { |
c5fdd383 | 791 | if (!current_kboard->immediate_echo) |
284f4730 JB |
792 | { |
793 | int i; | |
c5fdd383 | 794 | current_kboard->immediate_echo = 1; |
284f4730 JB |
795 | |
796 | for (i = 0; i < this_command_key_count; i++) | |
d0a57728 RS |
797 | { |
798 | Lisp_Object c; | |
799 | c = XVECTOR (this_command_keys)->contents[i]; | |
800 | if (! (EVENT_HAS_PARAMETERS (c) | |
801 | && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement))) | |
802 | echo_char (c); | |
803 | } | |
284f4730 JB |
804 | echo_dash (); |
805 | } | |
806 | ||
807 | echoing = 1; | |
301738ed RS |
808 | message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf), |
809 | ! NILP (current_buffer->enable_multibyte_characters)); | |
284f4730 JB |
810 | echoing = 0; |
811 | ||
59a84f8e GM |
812 | /* Record in what buffer we echoed, and from which kboard. */ |
813 | echo_message_buffer = echo_area_buffer[0]; | |
814 | echo_kboard = current_kboard; | |
815 | ||
284f4730 JB |
816 | if (waiting_for_input && !NILP (Vquit_flag)) |
817 | quit_throw_to_read_char (); | |
818 | } | |
819 | ||
820 | /* Turn off echoing, for the start of a new command. */ | |
821 | ||
dfcf069d | 822 | void |
284f4730 JB |
823 | cancel_echoing () |
824 | { | |
c5fdd383 KH |
825 | current_kboard->immediate_echo = 0; |
826 | current_kboard->echoptr = current_kboard->echobuf; | |
827 | current_kboard->echo_after_prompt = -1; | |
59a84f8e GM |
828 | ok_to_echo_at_next_pause = NULL; |
829 | echo_kboard = NULL; | |
830 | echo_message_buffer = Qnil; | |
284f4730 JB |
831 | } |
832 | ||
833 | /* Return the length of the current echo string. */ | |
834 | ||
835 | static int | |
836 | echo_length () | |
837 | { | |
c5fdd383 | 838 | return current_kboard->echoptr - current_kboard->echobuf; |
284f4730 JB |
839 | } |
840 | ||
841 | /* Truncate the current echo message to its first LEN chars. | |
842 | This and echo_char get used by read_key_sequence when the user | |
ff11dfa1 | 843 | switches frames while entering a key sequence. */ |
284f4730 JB |
844 | |
845 | static void | |
846 | echo_truncate (len) | |
847 | int len; | |
848 | { | |
c5fdd383 KH |
849 | current_kboard->echobuf[len] = '\0'; |
850 | current_kboard->echoptr = current_kboard->echobuf + len; | |
40932d1a | 851 | truncate_echo_area (len); |
284f4730 JB |
852 | } |
853 | ||
854 | \f | |
855 | /* Functions for manipulating this_command_keys. */ | |
856 | static void | |
857 | add_command_key (key) | |
858 | Lisp_Object key; | |
859 | { | |
6569cc8d JB |
860 | int size = XVECTOR (this_command_keys)->size; |
861 | ||
71918b75 RS |
862 | /* If reset-this-command-length was called recently, obey it now. |
863 | See the doc string of that function for an explanation of why. */ | |
864 | if (before_command_restore_flag) | |
865 | { | |
866 | this_command_key_count = before_command_key_count_1; | |
6321824f RS |
867 | if (this_command_key_count < this_single_command_key_start) |
868 | this_single_command_key_start = this_command_key_count; | |
71918b75 RS |
869 | echo_truncate (before_command_echo_length_1); |
870 | before_command_restore_flag = 0; | |
871 | } | |
872 | ||
6569cc8d | 873 | if (this_command_key_count >= size) |
284f4730 | 874 | { |
9b8eb840 | 875 | Lisp_Object new_keys; |
6569cc8d | 876 | |
9b8eb840 | 877 | new_keys = Fmake_vector (make_number (size * 2), Qnil); |
6569cc8d JB |
878 | bcopy (XVECTOR (this_command_keys)->contents, |
879 | XVECTOR (new_keys)->contents, | |
8f805655 | 880 | size * sizeof (Lisp_Object)); |
6569cc8d JB |
881 | |
882 | this_command_keys = new_keys; | |
284f4730 | 883 | } |
6569cc8d JB |
884 | |
885 | XVECTOR (this_command_keys)->contents[this_command_key_count++] = key; | |
284f4730 JB |
886 | } |
887 | \f | |
888 | Lisp_Object | |
889 | recursive_edit_1 () | |
890 | { | |
891 | int count = specpdl_ptr - specpdl; | |
892 | Lisp_Object val; | |
893 | ||
894 | if (command_loop_level > 0) | |
895 | { | |
896 | specbind (Qstandard_output, Qt); | |
897 | specbind (Qstandard_input, Qt); | |
898 | } | |
899 | ||
84265027 GM |
900 | #ifdef HAVE_X_WINDOWS |
901 | /* The command loop has started a busy-cursor timer, so we have to | |
902 | cancel it here, otherwise it will fire because the recursive edit | |
903 | can take some time. */ | |
904 | if (display_busy_cursor_p) | |
905 | cancel_busy_cursor (); | |
906 | #endif | |
907 | ||
284f4730 JB |
908 | val = command_loop (); |
909 | if (EQ (val, Qt)) | |
910 | Fsignal (Qquit, Qnil); | |
cb252880 RS |
911 | /* Handle throw from read_minibuf when using minibuffer |
912 | while it's active but we're in another window. */ | |
913 | if (STRINGP (val)) | |
914 | Fsignal (Qerror, Fcons (val, Qnil)); | |
284f4730 | 915 | |
cb5df6ae | 916 | return unbind_to (count, Qnil); |
284f4730 JB |
917 | } |
918 | ||
919 | /* When an auto-save happens, record the "time", and don't do again soon. */ | |
5846638c | 920 | |
07a59269 | 921 | void |
284f4730 JB |
922 | record_auto_save () |
923 | { | |
c43b1734 | 924 | last_auto_save = num_nonmacro_input_events; |
284f4730 | 925 | } |
5846638c RS |
926 | |
927 | /* Make an auto save happen as soon as possible at command level. */ | |
928 | ||
dfcf069d | 929 | void |
5846638c RS |
930 | force_auto_save_soon () |
931 | { | |
932 | last_auto_save = - auto_save_interval - 1; | |
241ceaf7 RS |
933 | |
934 | record_asynch_buffer_change (); | |
5846638c | 935 | } |
284f4730 | 936 | \f |
284f4730 JB |
937 | DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "", |
938 | "Invoke the editor command loop recursively.\n\ | |
939 | To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\ | |
940 | that tells this function to return.\n\ | |
941 | Alternately, `(throw 'exit t)' makes this function signal an error.\n\ | |
942 | This function is called by the editor initialization to begin editing.") | |
943 | () | |
944 | { | |
945 | int count = specpdl_ptr - specpdl; | |
284f4730 JB |
946 | |
947 | command_loop_level++; | |
948 | update_mode_lines = 1; | |
949 | ||
1db0076e GM |
950 | /* This function may have been called from a debugger called from |
951 | within redisplay, for instance by Edebugging a function called | |
952 | from fontification-functions. We want to allow redisplay in | |
953 | the debugging session. | |
954 | ||
955 | The recursive edit is left with a `(throw exit ...)'. The `exit' | |
956 | tag is not caught anywhere in redisplay, i.e. when we leave the | |
957 | recursive edit, the original redisplay leading to the recursive | |
958 | edit will be unwound. The outcome should therefore be safe. */ | |
959 | specbind (Qinhibit_redisplay, Qnil); | |
960 | redisplaying_p = 0; | |
961 | ||
284f4730 JB |
962 | record_unwind_protect (recursive_edit_unwind, |
963 | (command_loop_level | |
964 | && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer)) | |
965 | ? Fcurrent_buffer () | |
966 | : Qnil); | |
967 | recursive_edit_1 (); | |
968 | return unbind_to (count, Qnil); | |
969 | } | |
970 | ||
971 | Lisp_Object | |
972 | recursive_edit_unwind (buffer) | |
973 | Lisp_Object buffer; | |
974 | { | |
975 | if (!NILP (buffer)) | |
976 | Fset_buffer (buffer); | |
977 | ||
978 | command_loop_level--; | |
979 | update_mode_lines = 1; | |
980 | return Qnil; | |
981 | } | |
982 | \f | |
604ccd1d | 983 | static void |
1e8bd3da | 984 | any_kboard_state () |
604ccd1d | 985 | { |
1e8bd3da RS |
986 | #ifdef MULTI_KBOARD |
987 | #if 0 /* Theory: if there's anything in Vunread_command_events, | |
988 | it will right away be read by read_key_sequence, | |
989 | and then if we do switch KBOARDS, it will go into the side | |
990 | queue then. So we don't need to do anything special here -- rms. */ | |
604ccd1d | 991 | if (CONSP (Vunread_command_events)) |
4524b161 | 992 | { |
c5fdd383 KH |
993 | current_kboard->kbd_queue |
994 | = nconc2 (Vunread_command_events, current_kboard->kbd_queue); | |
995 | current_kboard->kbd_queue_has_data = 1; | |
4524b161 | 996 | } |
604ccd1d | 997 | Vunread_command_events = Qnil; |
1e8bd3da RS |
998 | #endif |
999 | single_kboard = 0; | |
1000 | #endif | |
604ccd1d | 1001 | } |
1e8bd3da RS |
1002 | |
1003 | /* Switch to the single-kboard state, making current_kboard | |
1004 | the only KBOARD from which further input is accepted. */ | |
1005 | ||
1006 | void | |
1007 | single_kboard_state () | |
1008 | { | |
1009 | #ifdef MULTI_KBOARD | |
1010 | single_kboard = 1; | |
604ccd1d | 1011 | #endif |
1e8bd3da RS |
1012 | } |
1013 | ||
1014 | /* Maintain a stack of kboards, so other parts of Emacs | |
1015 | can switch temporarily to the kboard of a given frame | |
1016 | and then revert to the previous status. */ | |
1017 | ||
1018 | struct kboard_stack | |
1019 | { | |
1020 | KBOARD *kboard; | |
1021 | struct kboard_stack *next; | |
1022 | }; | |
1023 | ||
1024 | static struct kboard_stack *kboard_stack; | |
1025 | ||
1026 | void | |
1027 | push_frame_kboard (f) | |
1028 | FRAME_PTR f; | |
1029 | { | |
ab48365b | 1030 | #ifdef MULTI_KBOARD |
1e8bd3da RS |
1031 | struct kboard_stack *p |
1032 | = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack)); | |
1033 | ||
1034 | p->next = kboard_stack; | |
1035 | p->kboard = current_kboard; | |
1036 | kboard_stack = p; | |
1037 | ||
1038 | current_kboard = FRAME_KBOARD (f); | |
ab48365b | 1039 | #endif |
1e8bd3da RS |
1040 | } |
1041 | ||
1042 | void | |
1043 | pop_frame_kboard () | |
1044 | { | |
ab48365b | 1045 | #ifdef MULTI_KBOARD |
1e8bd3da RS |
1046 | struct kboard_stack *p = kboard_stack; |
1047 | current_kboard = p->kboard; | |
1048 | kboard_stack = p->next; | |
1049 | xfree (p); | |
ab48365b | 1050 | #endif |
1e8bd3da RS |
1051 | } |
1052 | \f | |
1053 | /* Handle errors that are not handled at inner levels | |
1054 | by printing an error message and returning to the editor command loop. */ | |
604ccd1d | 1055 | |
284f4730 JB |
1056 | Lisp_Object |
1057 | cmd_error (data) | |
1058 | Lisp_Object data; | |
a1341f75 | 1059 | { |
a9f16aa9 | 1060 | Lisp_Object old_level, old_length; |
e881d8b2 RS |
1061 | char macroerror[50]; |
1062 | ||
1063 | if (!NILP (executing_macro)) | |
1064 | { | |
1065 | if (executing_macro_iterations == 1) | |
1066 | sprintf (macroerror, "After 1 kbd macro iteration: "); | |
1067 | else | |
1068 | sprintf (macroerror, "After %d kbd macro iterations: ", | |
1069 | executing_macro_iterations); | |
1070 | } | |
1071 | else | |
1072 | *macroerror = 0; | |
a9f16aa9 | 1073 | |
a1341f75 RS |
1074 | Vstandard_output = Qt; |
1075 | Vstandard_input = Qt; | |
1076 | Vexecuting_macro = Qnil; | |
9f58e89e | 1077 | executing_macro = Qnil; |
d8bcf58e | 1078 | current_kboard->Vprefix_arg = Qnil; |
75045dcb | 1079 | current_kboard->Vlast_prefix_arg = Qnil; |
df0f2ba1 | 1080 | cancel_echoing (); |
a9f16aa9 KH |
1081 | |
1082 | /* Avoid unquittable loop if data contains a circular list. */ | |
1083 | old_level = Vprint_level; | |
1084 | old_length = Vprint_length; | |
0c04a67e RS |
1085 | XSETFASTINT (Vprint_level, 10); |
1086 | XSETFASTINT (Vprint_length, 10); | |
e881d8b2 | 1087 | cmd_error_internal (data, macroerror); |
a9f16aa9 KH |
1088 | Vprint_level = old_level; |
1089 | Vprint_length = old_length; | |
a1341f75 RS |
1090 | |
1091 | Vquit_flag = Qnil; | |
1092 | ||
1093 | Vinhibit_quit = Qnil; | |
c5fdd383 | 1094 | #ifdef MULTI_KBOARD |
1e8bd3da | 1095 | any_kboard_state (); |
ff4b06d3 | 1096 | #endif |
a1341f75 RS |
1097 | |
1098 | return make_number (0); | |
1099 | } | |
1100 | ||
301738ed RS |
1101 | /* Take actions on handling an error. DATA is the data that describes |
1102 | the error. | |
1103 | ||
1104 | CONTEXT is a C-string containing ASCII characters only which | |
1105 | describes the context in which the error happened. If we need to | |
1106 | generalize CONTEXT to allow multibyte characters, make it a Lisp | |
1107 | string. */ | |
1108 | ||
07a59269 | 1109 | void |
a1341f75 RS |
1110 | cmd_error_internal (data, context) |
1111 | Lisp_Object data; | |
1112 | char *context; | |
284f4730 | 1113 | { |
284f4730 | 1114 | Lisp_Object stream; |
7ee32cda | 1115 | int kill_emacs_p = 0; |
788f89eb | 1116 | struct frame *sf = SELECTED_FRAME (); |
284f4730 JB |
1117 | |
1118 | Vquit_flag = Qnil; | |
1119 | Vinhibit_quit = Qt; | |
985f9f66 | 1120 | clear_message (1, 0); |
284f4730 | 1121 | |
ff11dfa1 | 1122 | /* If the window system or terminal frame hasn't been initialized |
284f4730 JB |
1123 | yet, or we're not interactive, it's best to dump this message out |
1124 | to stderr and exit. */ | |
788f89eb | 1125 | if (!sf->glyphs_initialized_p |
7ee32cda GM |
1126 | /* This is the case of the frame dumped with Emacs, when we're |
1127 | running under a window system. */ | |
1128 | || (!NILP (Vwindow_system) | |
1129 | && !inhibit_window_system | |
788f89eb | 1130 | && FRAME_TERMCAP_P (sf)) |
284f4730 | 1131 | || noninteractive) |
7ee32cda GM |
1132 | { |
1133 | stream = Qexternal_debugging_output; | |
1134 | kill_emacs_p = 1; | |
1135 | } | |
284f4730 JB |
1136 | else |
1137 | { | |
1138 | Fdiscard_input (); | |
1139 | bitch_at_user (); | |
1140 | stream = Qt; | |
1141 | } | |
1142 | ||
a1341f75 RS |
1143 | if (context != 0) |
1144 | write_string_1 (context, -1, stream); | |
1145 | ||
22a51344 | 1146 | print_error_message (data, stream); |
284f4730 | 1147 | |
ff11dfa1 | 1148 | /* If the window system or terminal frame hasn't been initialized |
284f4730 | 1149 | yet, or we're in -batch mode, this error should cause Emacs to exit. */ |
7ee32cda | 1150 | if (kill_emacs_p) |
284f4730 JB |
1151 | { |
1152 | Fterpri (stream); | |
1153 | Fkill_emacs (make_number (-1)); | |
1154 | } | |
284f4730 JB |
1155 | } |
1156 | \f | |
1157 | Lisp_Object command_loop_1 (); | |
1158 | Lisp_Object command_loop_2 (); | |
1159 | Lisp_Object top_level_1 (); | |
1160 | ||
1161 | /* Entry to editor-command-loop. | |
1162 | This level has the catches for exiting/returning to editor command loop. | |
1163 | It returns nil to exit recursive edit, t to abort it. */ | |
1164 | ||
1165 | Lisp_Object | |
1166 | command_loop () | |
1167 | { | |
1168 | if (command_loop_level > 0 || minibuf_level > 0) | |
1169 | { | |
07ba902e RS |
1170 | Lisp_Object val; |
1171 | val = internal_catch (Qexit, command_loop_2, Qnil); | |
68c46464 RS |
1172 | executing_macro = Qnil; |
1173 | return val; | |
284f4730 JB |
1174 | } |
1175 | else | |
1176 | while (1) | |
1177 | { | |
1178 | internal_catch (Qtop_level, top_level_1, Qnil); | |
1179 | internal_catch (Qtop_level, command_loop_2, Qnil); | |
68c46464 | 1180 | executing_macro = Qnil; |
df0f2ba1 | 1181 | |
284f4730 JB |
1182 | /* End of file in -batch run causes exit here. */ |
1183 | if (noninteractive) | |
1184 | Fkill_emacs (Qt); | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | /* Here we catch errors in execution of commands within the | |
1189 | editing loop, and reenter the editing loop. | |
1190 | When there is an error, cmd_error runs and returns a non-nil | |
1191 | value to us. A value of nil means that cmd_loop_1 itself | |
1192 | returned due to end of file (or end of kbd macro). */ | |
1193 | ||
1194 | Lisp_Object | |
1195 | command_loop_2 () | |
1196 | { | |
1197 | register Lisp_Object val; | |
1198 | ||
1199 | do | |
1200 | val = internal_condition_case (command_loop_1, Qerror, cmd_error); | |
1201 | while (!NILP (val)); | |
1202 | ||
1203 | return Qnil; | |
1204 | } | |
1205 | ||
1206 | Lisp_Object | |
1207 | top_level_2 () | |
1208 | { | |
1209 | return Feval (Vtop_level); | |
1210 | } | |
1211 | ||
1212 | Lisp_Object | |
1213 | top_level_1 () | |
1214 | { | |
1215 | /* On entry to the outer level, run the startup file */ | |
1216 | if (!NILP (Vtop_level)) | |
1217 | internal_condition_case (top_level_2, Qerror, cmd_error); | |
1218 | else if (!NILP (Vpurify_flag)) | |
1219 | message ("Bare impure Emacs (standard Lisp code not loaded)"); | |
1220 | else | |
1221 | message ("Bare Emacs (standard Lisp code not loaded)"); | |
1222 | return Qnil; | |
1223 | } | |
1224 | ||
1225 | DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "", | |
1226 | "Exit all recursive editing levels.") | |
1227 | () | |
1228 | { | |
2c9cf2c8 GM |
1229 | #ifdef HAVE_X_WINDOWS |
1230 | if (display_busy_cursor_p) | |
1231 | cancel_busy_cursor (); | |
1232 | #endif | |
8c907a56 | 1233 | return Fthrow (Qtop_level, Qnil); |
284f4730 JB |
1234 | } |
1235 | ||
1236 | DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "", | |
1237 | "Exit from the innermost recursive edit or minibuffer.") | |
1238 | () | |
1239 | { | |
1240 | if (command_loop_level > 0 || minibuf_level > 0) | |
1241 | Fthrow (Qexit, Qnil); | |
1242 | ||
1243 | error ("No recursive edit is in progress"); | |
8c907a56 | 1244 | return Qnil; |
284f4730 JB |
1245 | } |
1246 | ||
1247 | DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "", | |
1248 | "Abort the command that requested this recursive edit or minibuffer input.") | |
1249 | () | |
1250 | { | |
1251 | if (command_loop_level > 0 || minibuf_level > 0) | |
1252 | Fthrow (Qexit, Qt); | |
1253 | ||
1254 | error ("No recursive edit is in progress"); | |
8c907a56 | 1255 | return Qnil; |
284f4730 JB |
1256 | } |
1257 | \f | |
1258 | /* This is the actual command reading loop, | |
1259 | sans error-handling encapsulation. */ | |
1260 | ||
1261 | Lisp_Object Fcommand_execute (); | |
1262 | static int read_key_sequence (); | |
68f297c5 | 1263 | void safe_run_hooks (); |
adf5cb9c | 1264 | static void adjust_point_for_property (); |
284f4730 JB |
1265 | |
1266 | Lisp_Object | |
1267 | command_loop_1 () | |
1268 | { | |
03cee6ae GM |
1269 | Lisp_Object cmd; |
1270 | int lose; | |
284f4730 JB |
1271 | int nonundocount; |
1272 | Lisp_Object keybuf[30]; | |
1273 | int i; | |
284f4730 | 1274 | int no_direct; |
86e5706b | 1275 | int prev_modiff; |
8c907a56 | 1276 | struct buffer *prev_buffer = NULL; |
c5fdd383 | 1277 | #ifdef MULTI_KBOARD |
1e8bd3da | 1278 | int was_locked = single_kboard; |
bded54dd | 1279 | #endif |
284f4730 | 1280 | |
d9b641bb | 1281 | current_kboard->Vprefix_arg = Qnil; |
75045dcb | 1282 | current_kboard->Vlast_prefix_arg = Qnil; |
86e5706b | 1283 | Vdeactivate_mark = Qnil; |
284f4730 | 1284 | waiting_for_input = 0; |
df0f2ba1 | 1285 | cancel_echoing (); |
284f4730 | 1286 | |
284f4730 | 1287 | nonundocount = 0; |
284f4730 | 1288 | this_command_key_count = 0; |
6321824f | 1289 | this_single_command_key_start = 0; |
284f4730 | 1290 | |
a612e298 RS |
1291 | /* Make sure this hook runs after commands that get errors and |
1292 | throw to top level. */ | |
a98ea3f9 RS |
1293 | /* Note that the value cell will never directly contain nil |
1294 | if the symbol is a local variable. */ | |
e98a93eb | 1295 | if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks)) |
a98ea3f9 | 1296 | safe_run_hooks (Qpost_command_hook); |
a612e298 | 1297 | |
8f12e41d GM |
1298 | /* If displaying a message, resize the echo area window to fit |
1299 | that message's size exactly. */ | |
1300 | if (!NILP (echo_area_buffer[0])) | |
1301 | resize_echo_area_axactly (); | |
1302 | ||
8a792f3a RS |
1303 | if (!NILP (Vdeferred_action_list)) |
1304 | call0 (Vdeferred_action_function); | |
1305 | ||
e98a93eb | 1306 | if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks)) |
59aadc81 RS |
1307 | { |
1308 | if (NILP (Vunread_command_events) | |
7d18f9ae RS |
1309 | && NILP (Vunread_input_method_events) |
1310 | && NILP (Vunread_post_input_method_events) | |
59aadc81 | 1311 | && NILP (Vexecuting_macro) |
41365083 | 1312 | && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1))) |
59aadc81 RS |
1313 | safe_run_hooks (Qpost_command_idle_hook); |
1314 | } | |
1315 | ||
51d5a2c9 | 1316 | /* Do this after running Vpost_command_hook, for consistency. */ |
d5eecefb RS |
1317 | current_kboard->Vlast_command = Vthis_command; |
1318 | current_kboard->Vreal_last_command = real_this_command; | |
51d5a2c9 | 1319 | |
284f4730 JB |
1320 | while (1) |
1321 | { | |
788f89eb | 1322 | if (! FRAME_LIVE_P (XFRAME (selected_frame))) |
a94a4335 KH |
1323 | Fkill_emacs (Qnil); |
1324 | ||
284f4730 JB |
1325 | /* Make sure the current window's buffer is selected. */ |
1326 | if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer) | |
1327 | set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer)); | |
1328 | ||
1329 | /* Display any malloc warning that just came out. Use while because | |
1330 | displaying one warning can cause another. */ | |
1331 | ||
1332 | while (pending_malloc_warning) | |
1333 | display_malloc_warning (); | |
1334 | ||
1335 | no_direct = 0; | |
1336 | ||
86e5706b RS |
1337 | Vdeactivate_mark = Qnil; |
1338 | ||
284f4730 | 1339 | /* If minibuffer on and echo area in use, |
00392ce6 | 1340 | wait a short time and redraw minibuffer. */ |
284f4730 | 1341 | |
7ee32cda | 1342 | if (minibuf_level |
985f9f66 | 1343 | && !NILP (echo_area_buffer[0]) |
00392ce6 MB |
1344 | && EQ (minibuf_window, echo_area_window) |
1345 | && NUMBERP (Vminibuffer_message_timeout)) | |
284f4730 | 1346 | { |
f1bed6d8 RS |
1347 | /* Bind inhibit-quit to t so that C-g gets read in |
1348 | rather than quitting back to the minibuffer. */ | |
1349 | int count = specpdl_ptr - specpdl; | |
1350 | specbind (Qinhibit_quit, Qt); | |
f1bed6d8 | 1351 | |
00392ce6 | 1352 | Fsit_for (Vminibuffer_message_timeout, Qnil, Qnil); |
e6aa7813 | 1353 | /* Clear the echo area. */ |
301738ed | 1354 | message2 (0, 0, 0); |
cdb9d665 | 1355 | safe_run_hooks (Qecho_area_clear_hook); |
e6aa7813 | 1356 | |
db08707d RS |
1357 | unbind_to (count, Qnil); |
1358 | ||
e6aa7813 | 1359 | /* If a C-g came in before, treat it as input now. */ |
284f4730 JB |
1360 | if (!NILP (Vquit_flag)) |
1361 | { | |
1362 | Vquit_flag = Qnil; | |
24597608 | 1363 | Vunread_command_events = Fcons (make_number (quit_char), Qnil); |
284f4730 JB |
1364 | } |
1365 | } | |
1366 | ||
1367 | #ifdef C_ALLOCA | |
ff4b06d3 | 1368 | alloca (0); /* Cause a garbage collection now */ |
284f4730 JB |
1369 | /* Since we can free the most stuff here. */ |
1370 | #endif /* C_ALLOCA */ | |
1371 | ||
8f805655 | 1372 | #if 0 |
8f805655 JB |
1373 | /* Select the frame that the last event came from. Usually, |
1374 | switch-frame events will take care of this, but if some lisp | |
1375 | code swallows a switch-frame event, we'll fix things up here. | |
1376 | Is this a good idea? */ | |
8c18cbfb | 1377 | if (FRAMEP (internal_last_event_frame) |
788f89eb | 1378 | && !EQ (internal_last_event_frame, selected_frame)) |
3c370943 | 1379 | Fselect_frame (internal_last_event_frame, Qnil); |
284f4730 | 1380 | #endif |
48e416d4 RS |
1381 | /* If it has changed current-menubar from previous value, |
1382 | really recompute the menubar from the value. */ | |
a646e520 RS |
1383 | if (! NILP (Vlucid_menu_bar_dirty_flag) |
1384 | && !NILP (Ffboundp (Qrecompute_lucid_menubar))) | |
48e416d4 RS |
1385 | call0 (Qrecompute_lucid_menubar); |
1386 | ||
71918b75 RS |
1387 | before_command_key_count = this_command_key_count; |
1388 | before_command_echo_length = echo_length (); | |
1389 | ||
d5eecefb RS |
1390 | Vthis_command = Qnil; |
1391 | real_this_command = Qnil; | |
d7437ef6 | 1392 | |
8f805655 | 1393 | /* Read next key sequence; i gets its length. */ |
ce98e608 | 1394 | i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0], |
f571ae0d | 1395 | Qnil, 0, 1, 1); |
8f805655 | 1396 | |
6fac1409 | 1397 | /* A filter may have run while we were reading the input. */ |
788f89eb | 1398 | if (! FRAME_LIVE_P (XFRAME (selected_frame))) |
a94a4335 | 1399 | Fkill_emacs (Qnil); |
6fac1409 RS |
1400 | if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer) |
1401 | set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer)); | |
1402 | ||
8f805655 JB |
1403 | ++num_input_keys; |
1404 | ||
284f4730 JB |
1405 | /* Now we have read a key sequence of length I, |
1406 | or else I is 0 and we found end of file. */ | |
1407 | ||
1408 | if (i == 0) /* End of file -- happens only in */ | |
1409 | return Qnil; /* a kbd macro, at the end. */ | |
dcc408a0 RS |
1410 | /* -1 means read_key_sequence got a menu that was rejected. |
1411 | Just loop around and read another command. */ | |
1412 | if (i == -1) | |
1413 | { | |
1414 | cancel_echoing (); | |
1415 | this_command_key_count = 0; | |
6321824f | 1416 | this_single_command_key_start = 0; |
ff4b06d3 | 1417 | goto finalize; |
dcc408a0 | 1418 | } |
284f4730 | 1419 | |
284f4730 JB |
1420 | last_command_char = keybuf[i - 1]; |
1421 | ||
75c0b143 RS |
1422 | /* If the previous command tried to force a specific window-start, |
1423 | forget about that, in case this command moves point far away | |
c422836d KH |
1424 | from that position. But also throw away beg_unchanged and |
1425 | end_unchanged information in that case, so that redisplay will | |
1426 | update the whole window properly. */ | |
1427 | if (!NILP (XWINDOW (selected_window)->force_start)) | |
1428 | { | |
9351ebd0 | 1429 | struct buffer *b; |
c422836d | 1430 | XWINDOW (selected_window)->force_start = Qnil; |
9351ebd0 GM |
1431 | b = XBUFFER (XWINDOW (selected_window)->buffer); |
1432 | BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0; | |
c422836d | 1433 | } |
75c0b143 | 1434 | |
284f4730 JB |
1435 | cmd = read_key_sequence_cmd; |
1436 | if (!NILP (Vexecuting_macro)) | |
1437 | { | |
1438 | if (!NILP (Vquit_flag)) | |
1439 | { | |
1440 | Vexecuting_macro = Qt; | |
1441 | QUIT; /* Make some noise. */ | |
1442 | /* Will return since macro now empty. */ | |
1443 | } | |
1444 | } | |
1445 | ||
1446 | /* Do redisplay processing after this command except in special | |
e35b6123 | 1447 | cases identified below. */ |
86e5706b RS |
1448 | prev_buffer = current_buffer; |
1449 | prev_modiff = MODIFF; | |
8746da95 | 1450 | last_point_position = PT; |
18cd2eeb | 1451 | XSETBUFFER (last_point_position_buffer, prev_buffer); |
86e5706b | 1452 | |
adf5cb9c KH |
1453 | /* By default, we adjust point to a boundary of a region that |
1454 | has such a property that should be treated intangible | |
1455 | (e.g. composition, display). But, some commands will set | |
1456 | this variable differently. */ | |
1457 | Vdisable_point_adjustment = Qnil; | |
1458 | ||
284f4730 JB |
1459 | /* Execute the command. */ |
1460 | ||
d5eecefb RS |
1461 | Vthis_command = cmd; |
1462 | real_this_command = cmd; | |
a98ea3f9 RS |
1463 | /* Note that the value cell will never directly contain nil |
1464 | if the symbol is a local variable. */ | |
e98a93eb | 1465 | if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks)) |
a98ea3f9 | 1466 | safe_run_hooks (Qpre_command_hook); |
75045dcb | 1467 | |
d5eecefb | 1468 | if (NILP (Vthis_command)) |
284f4730 JB |
1469 | { |
1470 | /* nil means key is undefined. */ | |
1471 | bitch_at_user (); | |
c5fdd383 | 1472 | current_kboard->defining_kbd_macro = Qnil; |
284f4730 | 1473 | update_mode_lines = 1; |
d8bcf58e | 1474 | current_kboard->Vprefix_arg = Qnil; |
284f4730 JB |
1475 | } |
1476 | else | |
1477 | { | |
d8bcf58e | 1478 | if (NILP (current_kboard->Vprefix_arg) && ! no_direct) |
284f4730 | 1479 | { |
75045dcb RS |
1480 | /* In case we jump to directly_done. */ |
1481 | Vcurrent_prefix_arg = current_kboard->Vprefix_arg; | |
1482 | ||
284f4730 JB |
1483 | /* Recognize some common commands in common situations and |
1484 | do them directly. */ | |
d5eecefb | 1485 | if (EQ (Vthis_command, Qforward_char) && PT < ZV) |
284f4730 | 1486 | { |
51ad8a68 | 1487 | struct Lisp_Char_Table *dp |
284f4730 | 1488 | = window_display_table (XWINDOW (selected_window)); |
aaf35234 | 1489 | lose = FETCH_CHAR (PT_BYTE); |
8458ede6 | 1490 | SET_PT (PT + 1); |
0f7a8fee | 1491 | if ((dp |
82ba47d7 | 1492 | ? (VECTORP (DISP_CHAR_VECTOR (dp, lose)) |
9a5540db RS |
1493 | ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1 |
1494 | : (NILP (DISP_CHAR_VECTOR (dp, lose)) | |
1495 | && (lose >= 0x20 && lose < 0x7f))) | |
0f7a8fee | 1496 | : (lose >= 0x20 && lose < 0x7f)) |
37cd9f30 KH |
1497 | /* To extract the case of continuation on |
1498 | wide-column characters. */ | |
8458ede6 | 1499 | && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1) |
284f4730 JB |
1500 | && (XFASTINT (XWINDOW (selected_window)->last_modified) |
1501 | >= MODIFF) | |
598ba4c7 RS |
1502 | && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified) |
1503 | >= OVERLAY_MODIFF) | |
284f4730 | 1504 | && (XFASTINT (XWINDOW (selected_window)->last_point) |
8001d352 | 1505 | == PT - 1) |
284f4730 JB |
1506 | && !windows_or_buffers_changed |
1507 | && EQ (current_buffer->selective_display, Qnil) | |
1508 | && !detect_input_pending () | |
962ae636 | 1509 | && NILP (XWINDOW (selected_window)->column_number_displayed) |
284f4730 | 1510 | && NILP (Vexecuting_macro)) |
e35b6123 | 1511 | direct_output_forward_char (1); |
284f4730 JB |
1512 | goto directly_done; |
1513 | } | |
d5eecefb | 1514 | else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV) |
284f4730 | 1515 | { |
51ad8a68 | 1516 | struct Lisp_Char_Table *dp |
284f4730 | 1517 | = window_display_table (XWINDOW (selected_window)); |
8458ede6 | 1518 | SET_PT (PT - 1); |
aaf35234 | 1519 | lose = FETCH_CHAR (PT_BYTE); |
0f7a8fee | 1520 | if ((dp |
ca873d73 | 1521 | ? (VECTORP (DISP_CHAR_VECTOR (dp, lose)) |
9a5540db RS |
1522 | ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1 |
1523 | : (NILP (DISP_CHAR_VECTOR (dp, lose)) | |
1524 | && (lose >= 0x20 && lose < 0x7f))) | |
0f7a8fee | 1525 | : (lose >= 0x20 && lose < 0x7f)) |
284f4730 JB |
1526 | && (XFASTINT (XWINDOW (selected_window)->last_modified) |
1527 | >= MODIFF) | |
598ba4c7 RS |
1528 | && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified) |
1529 | >= OVERLAY_MODIFF) | |
284f4730 | 1530 | && (XFASTINT (XWINDOW (selected_window)->last_point) |
8001d352 | 1531 | == PT + 1) |
284f4730 JB |
1532 | && !windows_or_buffers_changed |
1533 | && EQ (current_buffer->selective_display, Qnil) | |
1534 | && !detect_input_pending () | |
962ae636 | 1535 | && NILP (XWINDOW (selected_window)->column_number_displayed) |
284f4730 | 1536 | && NILP (Vexecuting_macro)) |
e35b6123 | 1537 | direct_output_forward_char (-1); |
284f4730 JB |
1538 | goto directly_done; |
1539 | } | |
d5eecefb | 1540 | else if (EQ (Vthis_command, Qself_insert_command) |
284f4730 | 1541 | /* Try this optimization only on ascii keystrokes. */ |
8c18cbfb | 1542 | && INTEGERP (last_command_char)) |
284f4730 | 1543 | { |
89599794 | 1544 | unsigned int c = XINT (last_command_char); |
fc9cce4e | 1545 | int value; |
fc9cce4e RS |
1546 | if (NILP (Vexecuting_macro) |
1547 | && !EQ (minibuf_window, selected_window)) | |
284f4730 JB |
1548 | { |
1549 | if (!nonundocount || nonundocount >= 20) | |
1550 | { | |
1551 | Fundo_boundary (); | |
1552 | nonundocount = 0; | |
1553 | } | |
1554 | nonundocount++; | |
1555 | } | |
7ee32cda | 1556 | |
fc9cce4e RS |
1557 | lose = ((XFASTINT (XWINDOW (selected_window)->last_modified) |
1558 | < MODIFF) | |
598ba4c7 RS |
1559 | || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified) |
1560 | < OVERLAY_MODIFF) | |
fc9cce4e RS |
1561 | || (XFASTINT (XWINDOW (selected_window)->last_point) |
1562 | != PT) | |
4c61f38e | 1563 | || MODIFF <= SAVE_MODIFF |
fc9cce4e RS |
1564 | || windows_or_buffers_changed |
1565 | || !EQ (current_buffer->selective_display, Qnil) | |
1566 | || detect_input_pending () | |
962ae636 | 1567 | || !NILP (XWINDOW (selected_window)->column_number_displayed) |
fc9cce4e | 1568 | || !NILP (Vexecuting_macro)); |
7ee32cda | 1569 | |
fc9cce4e | 1570 | value = internal_self_insert (c, 0); |
7ee32cda | 1571 | |
fc9cce4e RS |
1572 | if (value == 2) |
1573 | nonundocount = 0; | |
1574 | ||
7ee32cda GM |
1575 | /* VALUE == 1 when AFTER-CHANGE functions are |
1576 | installed which is the case most of the time | |
1577 | because FONT-LOCK installs one. */ | |
1578 | if (!lose && !value) | |
e35b6123 | 1579 | direct_output_for_insert (c); |
284f4730 JB |
1580 | goto directly_done; |
1581 | } | |
1582 | } | |
1583 | ||
1584 | /* Here for a command that isn't executed directly */ | |
1585 | ||
7ee32cda GM |
1586 | #ifdef HAVE_X_WINDOWS |
1587 | if (display_busy_cursor_p) | |
d0c48478 | 1588 | start_busy_cursor (); |
7ee32cda GM |
1589 | #endif |
1590 | ||
284f4730 | 1591 | nonundocount = 0; |
d8bcf58e | 1592 | if (NILP (current_kboard->Vprefix_arg)) |
284f4730 | 1593 | Fundo_boundary (); |
d5eecefb | 1594 | Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil); |
d0c48478 GM |
1595 | |
1596 | #ifdef HAVE_X_WINDOWS | |
1597 | if (display_busy_cursor_p) | |
1598 | cancel_busy_cursor (); | |
1599 | #endif | |
284f4730 | 1600 | } |
a764a753 | 1601 | directly_done: ; |
75045dcb | 1602 | current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg; |
284f4730 | 1603 | |
84ee6048 RS |
1604 | /* Note that the value cell will never directly contain nil |
1605 | if the symbol is a local variable. */ | |
1606 | if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks)) | |
1607 | safe_run_hooks (Qpost_command_hook); | |
1608 | ||
8f12e41d GM |
1609 | /* If displaying a message, resize the echo area window to fit |
1610 | that message's size exactly. */ | |
1611 | if (!NILP (echo_area_buffer[0])) | |
1612 | resize_echo_area_axactly (); | |
1613 | ||
84ee6048 RS |
1614 | if (!NILP (Vdeferred_action_list)) |
1615 | safe_run_hooks (Qdeferred_action_function); | |
1616 | ||
1617 | if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks)) | |
1618 | { | |
1619 | if (NILP (Vunread_command_events) | |
7d18f9ae RS |
1620 | && NILP (Vunread_input_method_events) |
1621 | && NILP (Vunread_post_input_method_events) | |
84ee6048 RS |
1622 | && NILP (Vexecuting_macro) |
1623 | && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1))) | |
1624 | safe_run_hooks (Qpost_command_idle_hook); | |
1625 | } | |
1626 | ||
284f4730 | 1627 | /* If there is a prefix argument, |
6c7178b9 KH |
1628 | 1) We don't want Vlast_command to be ``universal-argument'' |
1629 | (that would be dumb), so don't set Vlast_command, | |
284f4730 JB |
1630 | 2) we want to leave echoing on so that the prefix will be |
1631 | echoed as part of this key sequence, so don't call | |
1632 | cancel_echoing, and | |
1633 | 3) we want to leave this_command_key_count non-zero, so that | |
1634 | read_char will realize that it is re-reading a character, and | |
217258d5 KH |
1635 | not echo it a second time. |
1636 | ||
1637 | If the command didn't actually create a prefix arg, | |
1638 | but is merely a frame event that is transparent to prefix args, | |
1639 | then the above doesn't apply. */ | |
1640 | if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char)) | |
284f4730 | 1641 | { |
d5eecefb RS |
1642 | current_kboard->Vlast_command = Vthis_command; |
1643 | current_kboard->Vreal_last_command = real_this_command; | |
284f4730 JB |
1644 | cancel_echoing (); |
1645 | this_command_key_count = 0; | |
6321824f | 1646 | this_single_command_key_start = 0; |
284f4730 | 1647 | } |
86e5706b | 1648 | |
88ce066e | 1649 | if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks)) |
86e5706b RS |
1650 | { |
1651 | if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode)) | |
1652 | { | |
1653 | current_buffer->mark_active = Qnil; | |
1654 | call1 (Vrun_hooks, intern ("deactivate-mark-hook")); | |
1655 | } | |
1656 | else if (current_buffer != prev_buffer || MODIFF != prev_modiff) | |
1657 | call1 (Vrun_hooks, intern ("activate-mark-hook")); | |
1658 | } | |
ff4b06d3 KH |
1659 | |
1660 | finalize: | |
adf5cb9c KH |
1661 | |
1662 | if (current_buffer == prev_buffer | |
1663 | && last_point_position != PT | |
1664 | && NILP (Vdisable_point_adjustment) | |
1665 | && NILP (Vglobal_disable_point_adjustment)) | |
1666 | adjust_point_for_property (last_point_position); | |
1667 | ||
ff4b06d3 KH |
1668 | /* Install chars successfully executed in kbd macro. */ |
1669 | ||
d8bcf58e KH |
1670 | if (!NILP (current_kboard->defining_kbd_macro) |
1671 | && NILP (current_kboard->Vprefix_arg)) | |
ff4b06d3 KH |
1672 | finalize_kbd_macro_chars (); |
1673 | ||
c5fdd383 | 1674 | #ifdef MULTI_KBOARD |
604ccd1d | 1675 | if (!was_locked) |
1e8bd3da | 1676 | any_kboard_state (); |
ff4b06d3 | 1677 | #endif |
284f4730 JB |
1678 | } |
1679 | } | |
1c9784c9 | 1680 | |
adf5cb9c KH |
1681 | extern Lisp_Object Qcomposition, Qdisplay; |
1682 | ||
1683 | /* Adjust point to a boundary of a region that has such a property | |
1684 | that should be treated intangible. For the moment, we check | |
1685 | `composition' and `display' property. LAST_PT is the last position | |
1686 | of point. */ | |
1687 | ||
1688 | static void | |
1689 | adjust_point_for_property (last_pt) | |
1690 | int last_pt; | |
1691 | { | |
1692 | int start, end; | |
1693 | Lisp_Object val; | |
1694 | int check_composition = 1, check_display = 1; | |
1695 | ||
1696 | while (check_composition || check_display) | |
1697 | { | |
1698 | if (check_composition | |
1699 | && PT > BEGV && PT < ZV | |
1700 | && get_property_and_range (PT, Qcomposition, &val, &start, &end, Qnil) | |
1701 | && COMPOSITION_VALID_P (start, end, val) | |
1702 | && start < PT && end > PT | |
1703 | && (last_pt <= start || last_pt >= end)) | |
1704 | { | |
1705 | if (PT < last_pt) | |
1706 | SET_PT (start); | |
1707 | else | |
1708 | SET_PT (end); | |
1709 | check_display = 1; | |
1710 | } | |
1711 | check_composition = 0; | |
1712 | if (check_display | |
1713 | && PT > BEGV && PT < ZV | |
1714 | && get_property_and_range (PT, Qdisplay, &val, &start, &end, Qnil) | |
3e9ac4b7 | 1715 | && display_prop_intangible_p (val) |
adf5cb9c KH |
1716 | && start < PT && end > PT |
1717 | && (last_pt <= start || last_pt >= end)) | |
1718 | { | |
1719 | if (PT < last_pt) | |
1720 | SET_PT (start); | |
1721 | else | |
1722 | SET_PT (end); | |
1723 | check_composition = 1; | |
1724 | } | |
1725 | check_display = 0; | |
1726 | } | |
1727 | } | |
1728 | ||
0bc3db2b RS |
1729 | /* Subroutine for safe_run_hooks: run the hook HOOK. */ |
1730 | ||
1731 | static Lisp_Object | |
1732 | safe_run_hooks_1 (hook) | |
1733 | Lisp_Object hook; | |
1734 | { | |
1735 | return call1 (Vrun_hooks, Vinhibit_quit); | |
1736 | } | |
1737 | ||
1738 | /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */ | |
1739 | ||
1740 | static Lisp_Object | |
1741 | safe_run_hooks_error (data) | |
1742 | Lisp_Object data; | |
1743 | { | |
30690496 | 1744 | return Fset (Vinhibit_quit, Qnil); |
0bc3db2b RS |
1745 | } |
1746 | ||
1c9784c9 KH |
1747 | /* If we get an error while running the hook, cause the hook variable |
1748 | to be nil. Also inhibit quits, so that C-g won't cause the hook | |
1749 | to mysteriously evaporate. */ | |
0bc3db2b | 1750 | |
68f297c5 | 1751 | void |
1c9784c9 | 1752 | safe_run_hooks (hook) |
a98ea3f9 | 1753 | Lisp_Object hook; |
1c9784c9 KH |
1754 | { |
1755 | int count = specpdl_ptr - specpdl; | |
0bc3db2b RS |
1756 | specbind (Qinhibit_quit, hook); |
1757 | ||
e702932d | 1758 | internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error); |
1c9784c9 KH |
1759 | |
1760 | unbind_to (count, Qnil); | |
1761 | } | |
8a9f5d3c | 1762 | |
284f4730 | 1763 | \f |
8a9f5d3c GM |
1764 | /* Number of seconds between polling for input. This is a Lisp |
1765 | variable that can be bound. */ | |
1766 | ||
284f4730 JB |
1767 | int polling_period; |
1768 | ||
eb8c3be9 | 1769 | /* Nonzero means polling for input is temporarily suppressed. */ |
8a9f5d3c | 1770 | |
284f4730 JB |
1771 | int poll_suppress_count; |
1772 | ||
8a9f5d3c GM |
1773 | /* Asynchronous timer for polling. */ |
1774 | ||
1775 | struct atimer *poll_timer; | |
1776 | ||
284f4730 | 1777 | |
36922b18 RS |
1778 | #ifdef POLL_FOR_INPUT |
1779 | ||
8a9f5d3c GM |
1780 | /* Poll for input, so what we catch a C-g if it comes in. This |
1781 | function is called from x_make_frame_visible, see comment | |
1782 | there. */ | |
284f4730 | 1783 | |
8a9f5d3c GM |
1784 | void |
1785 | poll_for_input_1 () | |
284f4730 | 1786 | { |
9ac0d9e0 JB |
1787 | if (interrupt_input_blocked == 0 |
1788 | && !waiting_for_input) | |
1789 | read_avail_input (0); | |
284f4730 JB |
1790 | } |
1791 | ||
8a9f5d3c GM |
1792 | /* Timer callback function for poll_timer. TIMER is equal to |
1793 | poll_timer. */ | |
1794 | ||
1795 | void | |
1796 | poll_for_input (timer) | |
1797 | struct atimer *timer; | |
1798 | { | |
1799 | if (poll_suppress_count == 0) | |
1800 | poll_for_input_1 (); | |
1801 | } | |
1802 | ||
1803 | #endif /* POLL_FOR_INPUT */ | |
284f4730 JB |
1804 | |
1805 | /* Begin signals to poll for input, if they are appropriate. | |
1806 | This function is called unconditionally from various places. */ | |
1807 | ||
07a59269 | 1808 | void |
284f4730 JB |
1809 | start_polling () |
1810 | { | |
1811 | #ifdef POLL_FOR_INPUT | |
34f04431 | 1812 | if (read_socket_hook && !interrupt_input) |
284f4730 | 1813 | { |
8a9f5d3c GM |
1814 | /* Turn alarm handling on unconditionally. It might have |
1815 | been turned off in process.c. */ | |
1816 | turn_on_atimers (1); | |
1817 | ||
1818 | /* If poll timer doesn't exist, are we need one with | |
1819 | a different interval, start a new one. */ | |
1820 | if (poll_timer == NULL | |
1821 | || EMACS_SECS (poll_timer->interval) != polling_period) | |
284f4730 | 1822 | { |
8a9f5d3c GM |
1823 | EMACS_TIME interval; |
1824 | ||
1825 | if (poll_timer) | |
1826 | cancel_atimer (poll_timer); | |
1827 | ||
1828 | EMACS_SET_SECS_USECS (interval, polling_period, 0); | |
1829 | poll_timer = start_atimer (ATIMER_CONTINUOUS, interval, | |
1830 | poll_for_input, NULL); | |
284f4730 | 1831 | } |
8a9f5d3c GM |
1832 | |
1833 | /* Let the timer's callback function poll for input | |
1834 | if this becomes zero. */ | |
1835 | --poll_suppress_count; | |
284f4730 JB |
1836 | } |
1837 | #endif | |
1838 | } | |
1839 | ||
1d3195db RS |
1840 | /* Nonzero if we are using polling to handle input asynchronously. */ |
1841 | ||
1842 | int | |
1843 | input_polling_used () | |
1844 | { | |
1845 | #ifdef POLL_FOR_INPUT | |
1846 | return read_socket_hook && !interrupt_input; | |
1847 | #else | |
1848 | return 0; | |
1849 | #endif | |
1850 | } | |
1851 | ||
284f4730 JB |
1852 | /* Turn off polling. */ |
1853 | ||
07a59269 | 1854 | void |
284f4730 JB |
1855 | stop_polling () |
1856 | { | |
1857 | #ifdef POLL_FOR_INPUT | |
34f04431 | 1858 | if (read_socket_hook && !interrupt_input) |
8a9f5d3c | 1859 | ++poll_suppress_count; |
284f4730 JB |
1860 | #endif |
1861 | } | |
fe8aeef3 RS |
1862 | |
1863 | /* Set the value of poll_suppress_count to COUNT | |
1864 | and start or stop polling accordingly. */ | |
1865 | ||
1866 | void | |
1867 | set_poll_suppress_count (count) | |
1868 | int count; | |
1869 | { | |
1870 | #ifdef POLL_FOR_INPUT | |
1871 | if (count == 0 && poll_suppress_count != 0) | |
1872 | { | |
1873 | poll_suppress_count = 1; | |
1874 | start_polling (); | |
1875 | } | |
1876 | else if (count != 0 && poll_suppress_count == 0) | |
1877 | { | |
1878 | stop_polling (); | |
1879 | } | |
1880 | poll_suppress_count = count; | |
1881 | #endif | |
1882 | } | |
f4eef8b4 | 1883 | |
d0a57728 RS |
1884 | /* Bind polling_period to a value at least N. |
1885 | But don't decrease it. */ | |
1886 | ||
07a59269 | 1887 | void |
f4eef8b4 RS |
1888 | bind_polling_period (n) |
1889 | int n; | |
1890 | { | |
1891 | #ifdef POLL_FOR_INPUT | |
d0a57728 RS |
1892 | int new = polling_period; |
1893 | ||
1894 | if (n > new) | |
1895 | new = n; | |
1896 | ||
6fe007f7 | 1897 | stop_other_atimers (poll_timer); |
f4eef8b4 | 1898 | stop_polling (); |
d0a57728 RS |
1899 | specbind (Qpolling_period, make_number (new)); |
1900 | /* Start a new alarm with the new period. */ | |
f4eef8b4 RS |
1901 | start_polling (); |
1902 | #endif | |
1903 | } | |
284f4730 | 1904 | \f |
6da3dd3a RS |
1905 | /* Apply the control modifier to CHARACTER. */ |
1906 | ||
faf5e407 JB |
1907 | int |
1908 | make_ctrl_char (c) | |
1909 | int c; | |
1910 | { | |
d205953b JB |
1911 | /* Save the upper bits here. */ |
1912 | int upper = c & ~0177; | |
1913 | ||
1914 | c &= 0177; | |
1915 | ||
1916 | /* Everything in the columns containing the upper-case letters | |
1917 | denotes a control character. */ | |
1918 | if (c >= 0100 && c < 0140) | |
1919 | { | |
1920 | int oc = c; | |
1921 | c &= ~0140; | |
1922 | /* Set the shift modifier for a control char | |
1923 | made from a shifted letter. But only for letters! */ | |
1924 | if (oc >= 'A' && oc <= 'Z') | |
1925 | c |= shift_modifier; | |
1926 | } | |
1927 | ||
1928 | /* The lower-case letters denote control characters too. */ | |
1929 | else if (c >= 'a' && c <= 'z') | |
1930 | c &= ~0140; | |
1931 | ||
1932 | /* Include the bits for control and shift | |
1933 | only if the basic ASCII code can't indicate them. */ | |
1934 | else if (c >= ' ') | |
1935 | c |= ctrl_modifier; | |
1936 | ||
1937 | /* Replace the high bits. */ | |
1938 | c |= (upper & ~ctrl_modifier); | |
faf5e407 JB |
1939 | |
1940 | return c; | |
1941 | } | |
1942 | ||
d4e68eea GM |
1943 | /* Display help echo in the echo area. |
1944 | ||
8dfd92c9 GM |
1945 | HELP a string means display that string, HELP nil means clear the |
1946 | help echo. If HELP is a function, call it with OBJECT and POS as | |
1947 | arguments; the function should return a help string or nil for | |
1948 | none. For all other types of HELP evaluate it to obtain a string. | |
1949 | ||
2190735a GM |
1950 | WINDOW is the window in which the help was generated, if any. |
1951 | It is nil if not in a window. | |
1952 | ||
5b2ec2d0 GM |
1953 | If OBJECT is a buffer, POS is the position in the buffer where the |
1954 | `help-echo' text property was found. | |
1955 | ||
1956 | If OBJECT is an overlay, that overlay has a `help-echo' property, | |
1957 | and POS is the position in the overlay's buffer under the mouse. | |
1958 | ||
1959 | If OBJECT is a string (an overlay string or a string displayed with | |
1960 | the `display' property). POS is the position in that string under | |
1961 | the mouse. | |
d4e68eea GM |
1962 | |
1963 | OK_TO_IVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help | |
1964 | echo overwrites a keystroke echo currently displayed in the echo | |
1965 | area. | |
1966 | ||
8dfd92c9 GM |
1967 | Note: this function may only be called with HELP nil or a string |
1968 | from X code running asynchronously. */ | |
d4e68eea | 1969 | |
31f84d03 | 1970 | void |
2190735a GM |
1971 | show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo) |
1972 | Lisp_Object help, window, object, pos; | |
adc84f48 | 1973 | int ok_to_overwrite_keystroke_echo; |
31f84d03 | 1974 | { |
8dfd92c9 | 1975 | if (!NILP (help) && !STRINGP (help)) |
d4e68eea | 1976 | { |
8dfd92c9 GM |
1977 | if (FUNCTIONP (help)) |
1978 | { | |
2190735a | 1979 | Lisp_Object args[4]; |
8dfd92c9 | 1980 | args[0] = help; |
2190735a GM |
1981 | args[1] = window; |
1982 | args[2] = object; | |
1983 | args[3] = pos; | |
1db0076e | 1984 | help = safe_call (4, args); |
8dfd92c9 GM |
1985 | } |
1986 | else | |
1db0076e | 1987 | help = safe_eval (help); |
8dfd92c9 GM |
1988 | |
1989 | if (!STRINGP (help)) | |
d4e68eea | 1990 | return; |
31f84d03 SM |
1991 | } |
1992 | ||
8dfd92c9 | 1993 | if (STRINGP (help) || NILP (help)) |
d4e68eea GM |
1994 | { |
1995 | if (!NILP (Vshow_help_function)) | |
8dfd92c9 | 1996 | call1 (Vshow_help_function, help); |
d4e68eea GM |
1997 | else if (/* Don't overwrite minibuffer contents. */ |
1998 | !MINI_WINDOW_P (XWINDOW (selected_window)) | |
1999 | /* Don't overwrite a keystroke echo. */ | |
8dfd92c9 GM |
2000 | && (NILP (echo_message_buffer) |
2001 | || ok_to_overwrite_keystroke_echo) | |
d4e68eea GM |
2002 | /* Don't overwrite a prompt. */ |
2003 | && !cursor_in_echo_area) | |
2004 | { | |
8dfd92c9 | 2005 | if (STRINGP (help)) |
d4e68eea GM |
2006 | { |
2007 | int count = specpdl_ptr - specpdl; | |
2008 | specbind (Qmessage_truncate_lines, Qt); | |
5ca020fc | 2009 | message3_nolog (help, STRING_BYTES (XSTRING (help)), |
8dfd92c9 | 2010 | STRING_MULTIBYTE (help)); |
d4e68eea GM |
2011 | unbind_to (count, Qnil); |
2012 | } | |
2013 | else | |
5295a500 | 2014 | message (0); |
d4e68eea | 2015 | } |
5295a500 GM |
2016 | |
2017 | help_echo_showing_p = STRINGP (help); | |
d4e68eea | 2018 | } |
31f84d03 SM |
2019 | } |
2020 | ||
faf5e407 JB |
2021 | |
2022 | \f | |
284f4730 JB |
2023 | /* Input of single characters from keyboard */ |
2024 | ||
2025 | Lisp_Object print_help (); | |
2026 | static Lisp_Object kbd_buffer_get_event (); | |
e4fe371d | 2027 | static void record_char (); |
284f4730 | 2028 | |
c5fdd383 KH |
2029 | #ifdef MULTI_KBOARD |
2030 | static jmp_buf wrong_kboard_jmpbuf; | |
bded54dd | 2031 | #endif |
beecf6a1 | 2032 | |
284f4730 JB |
2033 | /* read a character from the keyboard; call the redisplay if needed */ |
2034 | /* commandflag 0 means do not do auto-saving, but do do redisplay. | |
2035 | -1 means do not do redisplay, but do do autosaving. | |
2036 | 1 means do both. */ | |
2037 | ||
7d6de002 RS |
2038 | /* The arguments MAPS and NMAPS are for menu prompting. |
2039 | MAPS is an array of keymaps; NMAPS is the length of MAPS. | |
2040 | ||
2041 | PREV_EVENT is the previous input event, or nil if we are reading | |
b638f328 RS |
2042 | the first event of a key sequence (or not reading a key sequence). |
2043 | If PREV_EVENT is t, that is a "magic" value that says | |
2044 | not to run input methods, but in other respects to act as if | |
2045 | not reading a key sequence. | |
7d6de002 | 2046 | |
83d68044 | 2047 | If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1 |
6569cc8d | 2048 | if we used a mouse menu to read the input, or zero otherwise. If |
83d68044 | 2049 | USED_MOUSE_MENU is null, we don't dereference it. |
dcc408a0 RS |
2050 | |
2051 | Value is t if we showed a menu and the user rejected it. */ | |
7d6de002 | 2052 | |
284f4730 | 2053 | Lisp_Object |
7d6de002 | 2054 | read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu) |
284f4730 | 2055 | int commandflag; |
7d6de002 RS |
2056 | int nmaps; |
2057 | Lisp_Object *maps; | |
2058 | Lisp_Object prev_event; | |
2059 | int *used_mouse_menu; | |
284f4730 | 2060 | { |
8c907a56 | 2061 | volatile Lisp_Object c; |
284f4730 | 2062 | int count; |
410d4de9 | 2063 | jmp_buf local_getcjmp; |
284f4730 | 2064 | jmp_buf save_jump; |
8c907a56 | 2065 | volatile int key_already_recorded = 0; |
017c7cb6 | 2066 | Lisp_Object tem, save; |
8c907a56 GM |
2067 | volatile Lisp_Object previous_echo_area_message; |
2068 | volatile Lisp_Object also_record; | |
2069 | volatile int reread; | |
d5eecefb | 2070 | struct gcpro gcpro1, gcpro2; |
7c3bc944 | 2071 | |
e4fe371d | 2072 | also_record = Qnil; |
284f4730 | 2073 | |
71918b75 RS |
2074 | before_command_key_count = this_command_key_count; |
2075 | before_command_echo_length = echo_length (); | |
ef6661f7 | 2076 | c = Qnil; |
7ee32cda | 2077 | previous_echo_area_message = Qnil; |
71918b75 | 2078 | |
7ee32cda | 2079 | GCPRO2 (c, previous_echo_area_message); |
7c3bc944 | 2080 | |
7f07d5ca RS |
2081 | retry: |
2082 | ||
7d18f9ae RS |
2083 | reread = 0; |
2084 | if (CONSP (Vunread_post_input_method_events)) | |
284f4730 | 2085 | { |
7539e11f | 2086 | c = XCAR (Vunread_post_input_method_events); |
7d18f9ae | 2087 | Vunread_post_input_method_events |
7539e11f | 2088 | = XCDR (Vunread_post_input_method_events); |
284f4730 | 2089 | |
2479e91e RS |
2090 | /* Undo what read_char_x_menu_prompt did when it unread |
2091 | additional keys returned by Fx_popup_menu. */ | |
2092 | if (CONSP (c) | |
7539e11f KR |
2093 | && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))) |
2094 | && NILP (XCDR (c))) | |
2095 | c = XCAR (c); | |
2479e91e | 2096 | |
7d18f9ae RS |
2097 | reread = 1; |
2098 | goto reread_first; | |
284f4730 JB |
2099 | } |
2100 | ||
86e5706b RS |
2101 | if (unread_command_char != -1) |
2102 | { | |
18cd2eeb | 2103 | XSETINT (c, unread_command_char); |
86e5706b RS |
2104 | unread_command_char = -1; |
2105 | ||
7d18f9ae RS |
2106 | reread = 1; |
2107 | goto reread_first; | |
2108 | } | |
2109 | ||
2110 | if (CONSP (Vunread_command_events)) | |
2111 | { | |
7539e11f KR |
2112 | c = XCAR (Vunread_command_events); |
2113 | Vunread_command_events = XCDR (Vunread_command_events); | |
7d18f9ae RS |
2114 | |
2115 | /* Undo what read_char_x_menu_prompt did when it unread | |
2116 | additional keys returned by Fx_popup_menu. */ | |
2117 | if (CONSP (c) | |
7539e11f KR |
2118 | && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))) |
2119 | && NILP (XCDR (c))) | |
2120 | c = XCAR (c); | |
7d18f9ae RS |
2121 | |
2122 | reread = 1; | |
2123 | goto reread_for_input_method; | |
2124 | } | |
2125 | ||
2126 | if (CONSP (Vunread_input_method_events)) | |
2127 | { | |
7539e11f KR |
2128 | c = XCAR (Vunread_input_method_events); |
2129 | Vunread_input_method_events = XCDR (Vunread_input_method_events); | |
7d18f9ae RS |
2130 | |
2131 | /* Undo what read_char_x_menu_prompt did when it unread | |
2132 | additional keys returned by Fx_popup_menu. */ | |
2133 | if (CONSP (c) | |
7539e11f KR |
2134 | && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))) |
2135 | && NILP (XCDR (c))) | |
2136 | c = XCAR (c); | |
7d18f9ae RS |
2137 | reread = 1; |
2138 | goto reread_for_input_method; | |
86e5706b RS |
2139 | } |
2140 | ||
71918b75 RS |
2141 | /* If there is no function key translated before |
2142 | reset-this-command-lengths takes effect, forget about it. */ | |
2143 | before_command_restore_flag = 0; | |
2144 | ||
284f4730 JB |
2145 | if (!NILP (Vexecuting_macro)) |
2146 | { | |
fce33686 JB |
2147 | /* We set this to Qmacro; since that's not a frame, nobody will |
2148 | try to switch frames on us, and the selected window will | |
2149 | remain unchanged. | |
2150 | ||
2151 | Since this event came from a macro, it would be misleading to | |
eb8c3be9 | 2152 | leave internal_last_event_frame set to wherever the last |
3c370943 JB |
2153 | real event came from. Normally, a switch-frame event selects |
2154 | internal_last_event_frame after each command is read, but | |
2155 | events read from a macro should never cause a new frame to be | |
2156 | selected. */ | |
4c52b668 | 2157 | Vlast_event_frame = internal_last_event_frame = Qmacro; |
fce33686 | 2158 | |
663258f2 JB |
2159 | /* Exit the macro if we are at the end. |
2160 | Also, some things replace the macro with t | |
2161 | to force an early exit. */ | |
2162 | if (EQ (Vexecuting_macro, Qt) | |
2163 | || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro))) | |
284f4730 | 2164 | { |
18cd2eeb | 2165 | XSETINT (c, -1); |
7c3bc944 | 2166 | RETURN_UNGCPRO (c); |
284f4730 | 2167 | } |
df0f2ba1 | 2168 | |
284f4730 | 2169 | c = Faref (Vexecuting_macro, make_number (executing_macro_index)); |
8c18cbfb | 2170 | if (STRINGP (Vexecuting_macro) |
86e5706b | 2171 | && (XINT (c) & 0x80)) |
bb9e9bed | 2172 | XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80)); |
86e5706b | 2173 | |
284f4730 JB |
2174 | executing_macro_index++; |
2175 | ||
2176 | goto from_macro; | |
2177 | } | |
2178 | ||
cd21b839 JB |
2179 | if (!NILP (unread_switch_frame)) |
2180 | { | |
2181 | c = unread_switch_frame; | |
2182 | unread_switch_frame = Qnil; | |
2183 | ||
2184 | /* This event should make it into this_command_keys, and get echoed | |
7d18f9ae | 2185 | again, so we do not set `reread'. */ |
f4255cd1 | 2186 | goto reread_first; |
cd21b839 JB |
2187 | } |
2188 | ||
adc1d5c8 | 2189 | /* if redisplay was requested */ |
6e4e64a8 RS |
2190 | if (commandflag >= 0) |
2191 | { | |
adc1d5c8 RS |
2192 | /* If there is pending input, process any events which are not |
2193 | user-visible, such as X selection_request events. */ | |
6e4e64a8 RS |
2194 | if (input_pending |
2195 | || detect_input_pending_run_timers (0)) | |
adc1d5c8 | 2196 | swallow_events (0); /* may clear input_pending */ |
6e4e64a8 | 2197 | |
adc1d5c8 RS |
2198 | /* Redisplay if no pending input. */ |
2199 | while (!input_pending) | |
2200 | { | |
5295a500 GM |
2201 | if (help_echo_showing_p && !EQ (selected_window, minibuf_window)) |
2202 | redisplay_preserve_echo_area (); | |
2203 | else | |
2204 | redisplay (); | |
adc1d5c8 RS |
2205 | |
2206 | if (!input_pending) | |
2207 | /* Normal case: no input arrived during redisplay. */ | |
2208 | break; | |
2209 | ||
2210 | /* Input arrived and pre-empted redisplay. | |
2211 | Process any events which are not user-visible. */ | |
2212 | swallow_events (0); | |
2213 | /* If that cleared input_pending, try again to redisplay. */ | |
2214 | } | |
6e4e64a8 | 2215 | } |
e9bf89a0 | 2216 | |
59a84f8e GM |
2217 | /* Message turns off echoing unless more keystrokes turn it on again. |
2218 | ||
2219 | The code in 20.x for the condition was | |
2220 | ||
2221 | 1. echo_area_glyphs && *echo_area_glyphs | |
2222 | 2. && echo_area_glyphs != current_kboard->echobuf | |
2223 | 3. && ok_to_echo_at_next_pause != echo_area_glyphs | |
2224 | ||
2225 | (1) means there's a current message displayed | |
2226 | ||
2227 | (2) means it's not the message from echoing from the current | |
2228 | kboard. | |
2229 | ||
2230 | (3) There's only one place in 20.x where ok_to_echo_at_next_pause | |
2231 | is set to a non-null value. This is done in read_char and it is | |
2232 | set to echo_area_glyphs after a call to echo_char. That means | |
2233 | ok_to_echo_at_next_pause is either null or | |
2234 | current_kboard->echobuf with the appropriate current_kboard at | |
2235 | that time. | |
2236 | ||
2237 | So, condition (3) means in clear text ok_to_echo_at_next_pause | |
2238 | must be either null, or the current message isn't from echoing at | |
2239 | all, or it's from echoing from a different kboard than the | |
2240 | current one. */ | |
2241 | ||
2242 | if (/* There currently something in the echo area */ | |
985f9f66 | 2243 | !NILP (echo_area_buffer[0]) |
59a84f8e GM |
2244 | && (/* And it's either not from echoing. */ |
2245 | !EQ (echo_area_buffer[0], echo_message_buffer) | |
2246 | /* Or it's an echo from a different kboard. */ | |
2247 | || echo_kboard != current_kboard | |
2248 | /* Or we explicitly allow overwriting whatever there is. */ | |
2249 | || ok_to_echo_at_next_pause == NULL)) | |
7ee32cda | 2250 | cancel_echoing (); |
410d4de9 | 2251 | else |
410d4de9 | 2252 | echo_dash (); |
59a84f8e | 2253 | |
410d4de9 RS |
2254 | /* Try reading a character via menu prompting in the minibuf. |
2255 | Try this before the sit-for, because the sit-for | |
2256 | would do the wrong thing if we are supposed to do | |
2257 | menu prompting. If EVENT_HAS_PARAMETERS then we are reading | |
2258 | after a mouse event so don't try a minibuf menu. */ | |
2259 | c = Qnil; | |
2260 | if (nmaps > 0 && INTERACTIVE | |
2261 | && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event) | |
2262 | /* Don't bring up a menu if we already have another event. */ | |
2263 | && NILP (Vunread_command_events) | |
2264 | && unread_command_char < 0 | |
4ec4ed6a | 2265 | && !detect_input_pending_run_timers (0)) |
410d4de9 RS |
2266 | { |
2267 | c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps); | |
2268 | if (! NILP (c)) | |
2269 | { | |
2270 | key_already_recorded = 1; | |
2271 | goto non_reread_1; | |
2272 | } | |
2273 | } | |
284f4730 | 2274 | |
410d4de9 RS |
2275 | /* Make a longjmp point for quits to use, but don't alter getcjmp just yet. |
2276 | We will do that below, temporarily for short sections of code, | |
2277 | when appropriate. local_getcjmp must be in effect | |
2278 | around any call to sit_for or kbd_buffer_get_event; | |
2279 | it *must not* be in effect when we call redisplay. */ | |
284f4730 | 2280 | |
410d4de9 | 2281 | if (_setjmp (local_getcjmp)) |
284f4730 | 2282 | { |
18cd2eeb | 2283 | XSETINT (c, quit_char); |
788f89eb | 2284 | internal_last_event_frame = selected_frame; |
4c52b668 | 2285 | Vlast_event_frame = internal_last_event_frame; |
04904c29 RS |
2286 | /* If we report the quit char as an event, |
2287 | don't do so more than once. */ | |
2288 | if (!NILP (Vinhibit_quit)) | |
2289 | Vquit_flag = Qnil; | |
284f4730 | 2290 | |
c5fdd383 | 2291 | #ifdef MULTI_KBOARD |
df0f2ba1 | 2292 | { |
788f89eb | 2293 | KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame)); |
c5fdd383 | 2294 | if (kb != current_kboard) |
df0f2ba1 | 2295 | { |
c5fdd383 | 2296 | Lisp_Object *tailp = &kb->kbd_queue; |
1e8bd3da RS |
2297 | /* We shouldn't get here if we were in single-kboard mode! */ |
2298 | if (single_kboard) | |
df0f2ba1 KH |
2299 | abort (); |
2300 | while (CONSP (*tailp)) | |
7539e11f | 2301 | tailp = &XCDR (*tailp); |
df0f2ba1 KH |
2302 | if (!NILP (*tailp)) |
2303 | abort (); | |
2304 | *tailp = Fcons (c, Qnil); | |
c5fdd383 KH |
2305 | kb->kbd_queue_has_data = 1; |
2306 | current_kboard = kb; | |
ef6661f7 RS |
2307 | /* This is going to exit from read_char |
2308 | so we had better get rid of this frame's stuff. */ | |
2309 | UNGCPRO; | |
c5fdd383 | 2310 | longjmp (wrong_kboard_jmpbuf, 1); |
df0f2ba1 KH |
2311 | } |
2312 | } | |
2313 | #endif | |
284f4730 JB |
2314 | goto non_reread; |
2315 | } | |
2316 | ||
d9d4c147 KH |
2317 | timer_start_idle (); |
2318 | ||
284f4730 JB |
2319 | /* If in middle of key sequence and minibuffer not active, |
2320 | start echoing if enough time elapses. */ | |
410d4de9 | 2321 | |
7ee32cda GM |
2322 | if (minibuf_level == 0 |
2323 | && !current_kboard->immediate_echo | |
6c6083a9 | 2324 | && this_command_key_count > 0 |
27203ead | 2325 | && ! noninteractive |
f2647d04 DL |
2326 | && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes)) |
2327 | && NILP (Fzerop (Vecho_keystrokes)) | |
985f9f66 GM |
2328 | && (/* No message. */ |
2329 | NILP (echo_area_buffer[0]) | |
2330 | /* Or empty message. */ | |
2331 | || (BUF_BEG (XBUFFER (echo_area_buffer[0])) | |
2332 | == BUF_Z (XBUFFER (echo_area_buffer[0]))) | |
2333 | /* Or already echoing from same kboard. */ | |
2334 | || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard) | |
2335 | /* Or not echoing before and echoing allowed. */ | |
2336 | || (!echo_kboard && ok_to_echo_at_next_pause))) | |
284f4730 JB |
2337 | { |
2338 | Lisp_Object tem0; | |
985f9f66 | 2339 | |
7d6de002 RS |
2340 | /* After a mouse event, start echoing right away. |
2341 | This is because we are probably about to display a menu, | |
2342 | and we don't want to delay before doing so. */ | |
dbc4e1c1 | 2343 | if (EVENT_HAS_PARAMETERS (prev_event)) |
3dbd9ee4 | 2344 | echo_now (); |
7d6de002 RS |
2345 | else |
2346 | { | |
39aab679 DL |
2347 | int sec, usec; |
2348 | double duration = extract_float (Vecho_keystrokes); | |
2349 | sec = (int) duration; | |
15fa88ab | 2350 | usec = (duration - sec) * 1000000; |
410d4de9 RS |
2351 | save_getcjmp (save_jump); |
2352 | restore_getcjmp (local_getcjmp); | |
39aab679 | 2353 | tem0 = sit_for (sec, usec, 1, 1, 0); |
410d4de9 | 2354 | restore_getcjmp (save_jump); |
303b5b3f RS |
2355 | if (EQ (tem0, Qt) |
2356 | && ! CONSP (Vunread_command_events)) | |
3dbd9ee4 | 2357 | echo_now (); |
7d6de002 | 2358 | } |
284f4730 JB |
2359 | } |
2360 | ||
410d4de9 | 2361 | /* Maybe auto save due to number of keystrokes. */ |
284f4730 JB |
2362 | |
2363 | if (commandflag != 0 | |
2364 | && auto_save_interval > 0 | |
c43b1734 | 2365 | && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20) |
4ec4ed6a | 2366 | && !detect_input_pending_run_timers (0)) |
284f4730 | 2367 | { |
284f4730 | 2368 | Fdo_auto_save (Qnil, Qnil); |
ef8fd672 RS |
2369 | /* Hooks can actually change some buffers in auto save. */ |
2370 | redisplay (); | |
284f4730 JB |
2371 | } |
2372 | ||
8150596a | 2373 | /* Try reading using an X menu. |
24597608 RS |
2374 | This is never confused with reading using the minibuf |
2375 | because the recursive call of read_char in read_char_minibuf_menu_prompt | |
2376 | does not pass on any keymaps. */ | |
410d4de9 | 2377 | |
24597608 | 2378 | if (nmaps > 0 && INTERACTIVE |
5a8d99e0 KH |
2379 | && !NILP (prev_event) |
2380 | && EVENT_HAS_PARAMETERS (prev_event) | |
7539e11f KR |
2381 | && !EQ (XCAR (prev_event), Qmenu_bar) |
2382 | && !EQ (XCAR (prev_event), Qtool_bar) | |
24597608 RS |
2383 | /* Don't bring up a menu if we already have another event. */ |
2384 | && NILP (Vunread_command_events) | |
b8556aee | 2385 | && unread_command_char < 0) |
8eb4d8ef RS |
2386 | { |
2387 | c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu); | |
2388 | ||
2389 | /* Now that we have read an event, Emacs is not idle. */ | |
2390 | timer_stop_idle (); | |
2391 | ||
7c3bc944 | 2392 | RETURN_UNGCPRO (c); |
8eb4d8ef | 2393 | } |
7d6de002 | 2394 | |
410d4de9 RS |
2395 | /* Maybe autosave and/or garbage collect due to idleness. */ |
2396 | ||
26c1639e | 2397 | if (INTERACTIVE && NILP (c)) |
7d6de002 RS |
2398 | { |
2399 | int delay_level, buffer_size; | |
2400 | ||
410d4de9 RS |
2401 | /* Slow down auto saves logarithmically in size of current buffer, |
2402 | and garbage collect while we're at it. */ | |
7d6de002 RS |
2403 | if (! MINI_WINDOW_P (XWINDOW (selected_window))) |
2404 | last_non_minibuf_size = Z - BEG; | |
2405 | buffer_size = (last_non_minibuf_size >> 8) + 1; | |
2406 | delay_level = 0; | |
2407 | while (buffer_size > 64) | |
2408 | delay_level++, buffer_size -= buffer_size >> 2; | |
2409 | if (delay_level < 4) delay_level = 4; | |
2410 | /* delay_level is 4 for files under around 50k, 7 at 100k, | |
2411 | 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */ | |
2412 | ||
2413 | /* Auto save if enough time goes by without input. */ | |
2414 | if (commandflag != 0 | |
c43b1734 | 2415 | && num_nonmacro_input_events > last_auto_save |
8c18cbfb | 2416 | && INTEGERP (Vauto_save_timeout) |
7d6de002 RS |
2417 | && XINT (Vauto_save_timeout) > 0) |
2418 | { | |
2419 | Lisp_Object tem0; | |
410d4de9 RS |
2420 | |
2421 | save_getcjmp (save_jump); | |
2422 | restore_getcjmp (local_getcjmp); | |
d9d4c147 | 2423 | tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4, |
41365083 | 2424 | 0, 1, 1, 0); |
410d4de9 RS |
2425 | restore_getcjmp (save_jump); |
2426 | ||
303b5b3f RS |
2427 | if (EQ (tem0, Qt) |
2428 | && ! CONSP (Vunread_command_events)) | |
7d6de002 | 2429 | { |
7d6de002 | 2430 | Fdo_auto_save (Qnil, Qnil); |
7d6de002 RS |
2431 | |
2432 | /* If we have auto-saved and there is still no input | |
2433 | available, garbage collect if there has been enough | |
2434 | consing going on to make it worthwhile. */ | |
4ec4ed6a | 2435 | if (!detect_input_pending_run_timers (0) |
7d6de002 | 2436 | && consing_since_gc > gc_cons_threshold / 2) |
ef8fd672 | 2437 | Fgarbage_collect (); |
410d4de9 | 2438 | |
ef8fd672 | 2439 | redisplay (); |
7d6de002 RS |
2440 | } |
2441 | } | |
2442 | } | |
284f4730 | 2443 | |
303b5b3f RS |
2444 | /* If this has become non-nil here, it has been set by a timer |
2445 | or sentinel or filter. */ | |
2446 | if (CONSP (Vunread_command_events)) | |
2447 | { | |
7539e11f KR |
2448 | c = XCAR (Vunread_command_events); |
2449 | Vunread_command_events = XCDR (Vunread_command_events); | |
303b5b3f RS |
2450 | } |
2451 | ||
410d4de9 RS |
2452 | /* Read something from current KBOARD's side queue, if possible. */ |
2453 | ||
beecf6a1 | 2454 | if (NILP (c)) |
1e12dd87 | 2455 | { |
c5fdd383 | 2456 | if (current_kboard->kbd_queue_has_data) |
beecf6a1 | 2457 | { |
c5fdd383 | 2458 | if (!CONSP (current_kboard->kbd_queue)) |
4524b161 | 2459 | abort (); |
7539e11f | 2460 | c = XCAR (current_kboard->kbd_queue); |
c5fdd383 | 2461 | current_kboard->kbd_queue |
7539e11f | 2462 | = XCDR (current_kboard->kbd_queue); |
c5fdd383 KH |
2463 | if (NILP (current_kboard->kbd_queue)) |
2464 | current_kboard->kbd_queue_has_data = 0; | |
d9d4c147 | 2465 | input_pending = readable_events (0); |
4c52b668 KH |
2466 | if (EVENT_HAS_PARAMETERS (c) |
2467 | && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame)) | |
7539e11f | 2468 | internal_last_event_frame = XCAR (XCDR (c)); |
4c52b668 | 2469 | Vlast_event_frame = internal_last_event_frame; |
beecf6a1 | 2470 | } |
1e8bd3da RS |
2471 | } |
2472 | ||
c5fdd383 | 2473 | #ifdef MULTI_KBOARD |
1e8bd3da RS |
2474 | /* If current_kboard's side queue is empty check the other kboards. |
2475 | If one of them has data that we have not yet seen here, | |
2476 | switch to it and process the data waiting for it. | |
2477 | ||
2478 | Note: if the events queued up for another kboard | |
2479 | have already been seen here, and therefore are not a complete command, | |
2480 | the kbd_queue_has_data field is 0, so we skip that kboard here. | |
2481 | That's to avoid an infinite loop switching between kboards here. */ | |
2482 | if (NILP (c) && !single_kboard) | |
2483 | { | |
2484 | KBOARD *kb; | |
2485 | for (kb = all_kboards; kb; kb = kb->next_kboard) | |
2486 | if (kb->kbd_queue_has_data) | |
2487 | { | |
2488 | current_kboard = kb; | |
ef6661f7 RS |
2489 | /* This is going to exit from read_char |
2490 | so we had better get rid of this frame's stuff. */ | |
2491 | UNGCPRO; | |
1e8bd3da RS |
2492 | longjmp (wrong_kboard_jmpbuf, 1); |
2493 | } | |
2494 | } | |
df0f2ba1 KH |
2495 | #endif |
2496 | ||
410d4de9 RS |
2497 | wrong_kboard: |
2498 | ||
2499 | stop_polling (); | |
2500 | ||
1e8bd3da RS |
2501 | /* Finally, we read from the main queue, |
2502 | and if that gives us something we can't use yet, we put it on the | |
2503 | appropriate side queue and try again. */ | |
410d4de9 | 2504 | |
1e8bd3da RS |
2505 | if (NILP (c)) |
2506 | { | |
2507 | KBOARD *kb; | |
2508 | ||
1e8bd3da | 2509 | /* Actually read a character, waiting if necessary. */ |
410d4de9 RS |
2510 | save_getcjmp (save_jump); |
2511 | restore_getcjmp (local_getcjmp); | |
5b7bc0da | 2512 | timer_start_idle (); |
83d68044 | 2513 | c = kbd_buffer_get_event (&kb, used_mouse_menu); |
410d4de9 RS |
2514 | restore_getcjmp (save_jump); |
2515 | ||
c5fdd383 | 2516 | #ifdef MULTI_KBOARD |
410d4de9 | 2517 | if (! NILP (c) && (kb != current_kboard)) |
1e8bd3da RS |
2518 | { |
2519 | Lisp_Object *tailp = &kb->kbd_queue; | |
2520 | while (CONSP (*tailp)) | |
7539e11f | 2521 | tailp = &XCDR (*tailp); |
1e8bd3da RS |
2522 | if (!NILP (*tailp)) |
2523 | abort (); | |
2524 | *tailp = Fcons (c, Qnil); | |
2525 | kb->kbd_queue_has_data = 1; | |
46b84797 | 2526 | c = Qnil; |
1e8bd3da RS |
2527 | if (single_kboard) |
2528 | goto wrong_kboard; | |
2529 | current_kboard = kb; | |
ef6661f7 RS |
2530 | /* This is going to exit from read_char |
2531 | so we had better get rid of this frame's stuff. */ | |
2532 | UNGCPRO; | |
1e8bd3da | 2533 | longjmp (wrong_kboard_jmpbuf, 1); |
df0f2ba1 | 2534 | } |
1e8bd3da | 2535 | #endif |
beecf6a1 | 2536 | } |
1e8bd3da | 2537 | |
284f4730 | 2538 | /* Terminate Emacs in batch mode if at eof. */ |
8c18cbfb | 2539 | if (noninteractive && INTEGERP (c) && XINT (c) < 0) |
284f4730 JB |
2540 | Fkill_emacs (make_number (1)); |
2541 | ||
8c18cbfb | 2542 | if (INTEGERP (c)) |
80645119 JB |
2543 | { |
2544 | /* Add in any extra modifiers, where appropriate. */ | |
2545 | if ((extra_keyboard_modifiers & CHAR_CTL) | |
2546 | || ((extra_keyboard_modifiers & 0177) < ' ' | |
2547 | && (extra_keyboard_modifiers & 0177) != 0)) | |
faf5e407 | 2548 | XSETINT (c, make_ctrl_char (XINT (c))); |
80645119 JB |
2549 | |
2550 | /* Transfer any other modifier bits directly from | |
2551 | extra_keyboard_modifiers to c. Ignore the actual character code | |
2552 | in the low 16 bits of extra_keyboard_modifiers. */ | |
b8d9050d | 2553 | XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL)); |
80645119 | 2554 | } |
9fa4395d | 2555 | |
284f4730 JB |
2556 | non_reread: |
2557 | ||
2fb9049e | 2558 | timer_stop_idle (); |
284f4730 JB |
2559 | start_polling (); |
2560 | ||
410d4de9 RS |
2561 | if (NILP (c)) |
2562 | { | |
2563 | if (commandflag >= 0 | |
4ec4ed6a | 2564 | && !input_pending && !detect_input_pending_run_timers (0)) |
410d4de9 RS |
2565 | redisplay (); |
2566 | ||
2567 | goto wrong_kboard; | |
2568 | } | |
2569 | ||
2570 | non_reread_1: | |
2571 | ||
dfd11da7 | 2572 | /* Buffer switch events are only for internal wakeups |
7c3bc944 RS |
2573 | so don't show them to the user. |
2574 | Also, don't record a key if we already did. */ | |
2575 | if (BUFFERP (c) || key_already_recorded) | |
2576 | RETURN_UNGCPRO (c); | |
a1341f75 | 2577 | |
7f07d5ca RS |
2578 | /* Process special events within read_char |
2579 | and loop around to read another event. */ | |
017c7cb6 RS |
2580 | save = Vquit_flag; |
2581 | Vquit_flag = Qnil; | |
02067692 | 2582 | tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1); |
017c7cb6 | 2583 | Vquit_flag = save; |
7f07d5ca RS |
2584 | |
2585 | if (!NILP (tem)) | |
2586 | { | |
ba8dfba8 RS |
2587 | int was_locked = single_kboard; |
2588 | ||
7f07d5ca | 2589 | last_input_char = c; |
158f7532 | 2590 | Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt); |
ba8dfba8 RS |
2591 | |
2592 | /* Resume allowing input from any kboard, if that was true before. */ | |
2593 | if (!was_locked) | |
2594 | any_kboard_state (); | |
2595 | ||
7f07d5ca RS |
2596 | goto retry; |
2597 | } | |
2598 | ||
284f4730 | 2599 | /* Handle things that only apply to characters. */ |
8c18cbfb | 2600 | if (INTEGERP (c)) |
284f4730 JB |
2601 | { |
2602 | /* If kbd_buffer_get_event gave us an EOF, return that. */ | |
86e5706b | 2603 | if (XINT (c) == -1) |
7c3bc944 | 2604 | RETURN_UNGCPRO (c); |
284f4730 | 2605 | |
301738ed RS |
2606 | if ((STRINGP (Vkeyboard_translate_table) |
2607 | && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c)) | |
2608 | || (VECTORP (Vkeyboard_translate_table) | |
2609 | && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c)) | |
2610 | || (CHAR_TABLE_P (Vkeyboard_translate_table) | |
2611 | && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c))) | |
f9414d62 RS |
2612 | { |
2613 | Lisp_Object d; | |
2614 | d = Faref (Vkeyboard_translate_table, c); | |
2615 | /* nil in keyboard-translate-table means no translation. */ | |
2616 | if (!NILP (d)) | |
2617 | c = d; | |
2618 | } | |
284f4730 JB |
2619 | } |
2620 | ||
e4fe371d RS |
2621 | /* If this event is a mouse click in the menu bar, |
2622 | return just menu-bar for now. Modify the mouse click event | |
2623 | so we won't do this twice, then queue it up. */ | |
2624 | if (EVENT_HAS_PARAMETERS (c) | |
7539e11f | 2625 | && CONSP (XCDR (c)) |
e4fe371d | 2626 | && CONSP (EVENT_START (c)) |
7539e11f | 2627 | && CONSP (XCDR (EVENT_START (c)))) |
284f4730 | 2628 | { |
e4fe371d | 2629 | Lisp_Object posn; |
284f4730 | 2630 | |
e4fe371d RS |
2631 | posn = POSN_BUFFER_POSN (EVENT_START (c)); |
2632 | /* Handle menu-bar events: | |
2633 | insert the dummy prefix event `menu-bar'. */ | |
9ea173e8 | 2634 | if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar)) |
e4fe371d RS |
2635 | { |
2636 | /* Change menu-bar to (menu-bar) as the event "position". */ | |
2637 | POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil); | |
284f4730 | 2638 | |
e4fe371d RS |
2639 | also_record = c; |
2640 | Vunread_command_events = Fcons (c, Vunread_command_events); | |
2641 | c = posn; | |
284f4730 | 2642 | } |
284f4730 JB |
2643 | } |
2644 | ||
7d18f9ae RS |
2645 | /* Store these characters into recent_keys, the dribble file if any, |
2646 | and the keyboard macro being defined, if any. */ | |
e4fe371d RS |
2647 | record_char (c); |
2648 | if (! NILP (also_record)) | |
2649 | record_char (also_record); | |
51172b6d | 2650 | |
d5eecefb RS |
2651 | /* Wipe the echo area. |
2652 | But first, if we are about to use an input method, | |
2653 | save the echo area contents for it to refer to. */ | |
2654 | if (INTEGERP (c) | |
2655 | && ! NILP (Vinput_method_function) | |
2656 | && (unsigned) XINT (c) >= ' ' | |
8d769115 KH |
2657 | && (unsigned) XINT (c) != 127 |
2658 | && (unsigned) XINT (c) < 256) | |
7ee32cda GM |
2659 | { |
2660 | previous_echo_area_message = Fcurrent_message (); | |
2661 | Vinput_method_previous_message = previous_echo_area_message; | |
2662 | } | |
d5eecefb | 2663 | |
1172eb8d GM |
2664 | /* Now wipe the echo area, except for help events which do their |
2665 | own stuff with the echo area. */ | |
2666 | if (!CONSP (c) || !(EQ (Qhelp_echo, XCAR (c)))) | |
2667 | { | |
2668 | if (!NILP (echo_area_buffer[0])) | |
2669 | safe_run_hooks (Qecho_area_clear_hook); | |
2670 | clear_message (1, 0); | |
2671 | } | |
d5eecefb | 2672 | |
7d18f9ae | 2673 | reread_for_input_method: |
284f4730 | 2674 | from_macro: |
7d18f9ae | 2675 | /* Pass this to the input method, if appropriate. */ |
d5eecefb RS |
2676 | if (INTEGERP (c) |
2677 | && ! NILP (Vinput_method_function) | |
b638f328 RS |
2678 | /* Don't run the input method within a key sequence, |
2679 | after the first event of the key sequence. */ | |
2680 | && NILP (prev_event) | |
d5eecefb | 2681 | && (unsigned) XINT (c) >= ' ' |
8d769115 KH |
2682 | && (unsigned) XINT (c) != 127 |
2683 | && (unsigned) XINT (c) < 256) | |
d5eecefb RS |
2684 | { |
2685 | Lisp_Object keys; | |
6e5742a0 | 2686 | int key_count; |
d5eecefb | 2687 | struct gcpro gcpro1; |
b638f328 | 2688 | int count = specpdl_ptr - specpdl; |
d5eecefb | 2689 | |
6e5742a0 RS |
2690 | /* Save the echo status. */ |
2691 | int saved_immediate_echo = current_kboard->immediate_echo; | |
985f9f66 | 2692 | struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause; |
6e5742a0 RS |
2693 | int saved_echo_after_prompt = current_kboard->echo_after_prompt; |
2694 | ||
2695 | if (before_command_restore_flag) | |
2696 | { | |
2697 | this_command_key_count = before_command_key_count_1; | |
2698 | if (this_command_key_count < this_single_command_key_start) | |
2699 | this_single_command_key_start = this_command_key_count; | |
2700 | echo_truncate (before_command_echo_length_1); | |
2701 | before_command_restore_flag = 0; | |
2702 | } | |
2703 | ||
2704 | /* Save the this_command_keys status. */ | |
2705 | key_count = this_command_key_count; | |
2706 | ||
2707 | if (key_count > 0) | |
2708 | keys = Fcopy_sequence (this_command_keys); | |
2709 | else | |
2710 | keys = Qnil; | |
d5eecefb | 2711 | GCPRO1 (keys); |
6e5742a0 RS |
2712 | |
2713 | /* Clear out this_command_keys. */ | |
2714 | this_command_key_count = 0; | |
2715 | ||
2716 | /* Now wipe the echo area. */ | |
985f9f66 | 2717 | if (!NILP (echo_area_buffer[0])) |
6e5742a0 | 2718 | safe_run_hooks (Qecho_area_clear_hook); |
985f9f66 | 2719 | clear_message (1, 0); |
6e5742a0 RS |
2720 | echo_truncate (0); |
2721 | ||
b638f328 RS |
2722 | /* If we are not reading a key sequence, |
2723 | never use the echo area. */ | |
2724 | if (maps == 0) | |
2725 | { | |
b638f328 RS |
2726 | specbind (Qinput_method_use_echo_area, Qt); |
2727 | } | |
2728 | ||
6e5742a0 | 2729 | /* Call the input method. */ |
d5eecefb | 2730 | tem = call1 (Vinput_method_function, c); |
b638f328 RS |
2731 | |
2732 | tem = unbind_to (count, tem); | |
2733 | ||
6e5742a0 RS |
2734 | /* Restore the saved echoing state |
2735 | and this_command_keys state. */ | |
2736 | this_command_key_count = key_count; | |
2737 | if (key_count > 0) | |
2738 | this_command_keys = keys; | |
2739 | ||
2740 | cancel_echoing (); | |
2741 | ok_to_echo_at_next_pause = saved_ok_to_echo; | |
2742 | current_kboard->echo_after_prompt = saved_echo_after_prompt; | |
2743 | if (saved_immediate_echo) | |
2744 | echo_now (); | |
2745 | ||
d5eecefb | 2746 | UNGCPRO; |
6e5742a0 | 2747 | |
d5eecefb RS |
2748 | /* The input method can return no events. */ |
2749 | if (! CONSP (tem)) | |
7d18f9ae | 2750 | { |
d5eecefb | 2751 | /* Bring back the previous message, if any. */ |
7ee32cda GM |
2752 | if (! NILP (previous_echo_area_message)) |
2753 | message_with_string ("%s", previous_echo_area_message, 0); | |
d5eecefb | 2754 | goto retry; |
7d18f9ae | 2755 | } |
d5eecefb | 2756 | /* It returned one event or more. */ |
7539e11f | 2757 | c = XCAR (tem); |
d5eecefb | 2758 | Vunread_post_input_method_events |
7539e11f | 2759 | = nconc2 (XCDR (tem), Vunread_post_input_method_events); |
7d18f9ae | 2760 | } |
7c3bc944 | 2761 | |
7d18f9ae | 2762 | reread_first: |
284f4730 | 2763 | |
7ee32cda | 2764 | /* Display help if not echoing. */ |
1172eb8d | 2765 | if (CONSP (c) && EQ (XCAR (c), Qhelp_echo)) |
7ee32cda | 2766 | { |
2190735a GM |
2767 | /* (help-echo FRAME HELP WINDOW OBJECT POS). */ |
2768 | Lisp_Object help, object, position, window; | |
8dfd92c9 | 2769 | help = Fnth (make_number (2), c); |
2190735a GM |
2770 | window = Fnth (make_number (3), c); |
2771 | object = Fnth (make_number (4), c); | |
2772 | position = Fnth (make_number (5), c); | |
2773 | show_help_echo (help, window, object, position, 0); | |
7ee32cda GM |
2774 | goto retry; |
2775 | } | |
2776 | ||
7d18f9ae | 2777 | if (this_command_key_count == 0 || ! reread) |
e4fe371d | 2778 | { |
7d18f9ae RS |
2779 | before_command_key_count = this_command_key_count; |
2780 | before_command_echo_length = echo_length (); | |
2781 | ||
2782 | /* Don't echo mouse motion events. */ | |
f2647d04 DL |
2783 | if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes)) |
2784 | && NILP (Fzerop (Vecho_keystrokes)) | |
7d18f9ae RS |
2785 | && ! (EVENT_HAS_PARAMETERS (c) |
2786 | && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement))) | |
2787 | { | |
2788 | echo_char (c); | |
2789 | if (! NILP (also_record)) | |
2790 | echo_char (also_record); | |
2791 | /* Once we reread a character, echoing can happen | |
2792 | the next time we pause to read a new one. */ | |
985f9f66 | 2793 | ok_to_echo_at_next_pause = current_kboard; |
7d18f9ae RS |
2794 | } |
2795 | ||
2796 | /* Record this character as part of the current key. */ | |
2797 | add_command_key (c); | |
e4fe371d | 2798 | if (! NILP (also_record)) |
7d18f9ae | 2799 | add_command_key (also_record); |
e4fe371d | 2800 | } |
b8556aee | 2801 | |
284f4730 | 2802 | last_input_char = c; |
c43b1734 | 2803 | num_input_events++; |
284f4730 JB |
2804 | |
2805 | /* Process the help character specially if enabled */ | |
ecb7cb34 | 2806 | if (!NILP (Vhelp_form) && help_char_p (c)) |
284f4730 JB |
2807 | { |
2808 | Lisp_Object tem0; | |
2809 | count = specpdl_ptr - specpdl; | |
2810 | ||
2811 | record_unwind_protect (Fset_window_configuration, | |
2812 | Fcurrent_window_configuration (Qnil)); | |
2813 | ||
2814 | tem0 = Feval (Vhelp_form); | |
8c18cbfb | 2815 | if (STRINGP (tem0)) |
284f4730 JB |
2816 | internal_with_output_to_temp_buffer ("*Help*", print_help, tem0); |
2817 | ||
2818 | cancel_echoing (); | |
3cb81011 KH |
2819 | do |
2820 | c = read_char (0, 0, 0, Qnil, 0); | |
8c18cbfb | 2821 | while (BUFFERP (c)); |
ff11dfa1 | 2822 | /* Remove the help from the frame */ |
284f4730 | 2823 | unbind_to (count, Qnil); |
410d4de9 | 2824 | |
284f4730 JB |
2825 | redisplay (); |
2826 | if (EQ (c, make_number (040))) | |
2827 | { | |
2828 | cancel_echoing (); | |
3cb81011 KH |
2829 | do |
2830 | c = read_char (0, 0, 0, Qnil, 0); | |
8c18cbfb | 2831 | while (BUFFERP (c)); |
284f4730 JB |
2832 | } |
2833 | } | |
2834 | ||
7c3bc944 | 2835 | RETURN_UNGCPRO (c); |
284f4730 JB |
2836 | } |
2837 | ||
8eb4d8ef RS |
2838 | /* Record a key that came from a mouse menu. |
2839 | Record it for echoing, for this-command-keys, and so on. */ | |
2840 | ||
2841 | static void | |
2842 | record_menu_key (c) | |
2843 | Lisp_Object c; | |
2844 | { | |
2845 | /* Wipe the echo area. */ | |
985f9f66 | 2846 | clear_message (1, 0); |
8eb4d8ef RS |
2847 | |
2848 | record_char (c); | |
2849 | ||
2850 | before_command_key_count = this_command_key_count; | |
2851 | before_command_echo_length = echo_length (); | |
2852 | ||
2853 | /* Don't echo mouse motion events. */ | |
f2647d04 DL |
2854 | if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes)) |
2855 | && NILP (Fzerop (Vecho_keystrokes))) | |
8eb4d8ef RS |
2856 | { |
2857 | echo_char (c); | |
2858 | ||
2859 | /* Once we reread a character, echoing can happen | |
2860 | the next time we pause to read a new one. */ | |
2861 | ok_to_echo_at_next_pause = 0; | |
2862 | } | |
2863 | ||
2864 | /* Record this character as part of the current key. */ | |
2865 | add_command_key (c); | |
2866 | ||
2867 | /* Re-reading in the middle of a command */ | |
2868 | last_input_char = c; | |
c43b1734 | 2869 | num_input_events++; |
8eb4d8ef RS |
2870 | } |
2871 | ||
ecb7cb34 KH |
2872 | /* Return 1 if should recognize C as "the help character". */ |
2873 | ||
2874 | int | |
2875 | help_char_p (c) | |
2876 | Lisp_Object c; | |
2877 | { | |
2878 | Lisp_Object tail; | |
2879 | ||
2880 | if (EQ (c, Vhelp_char)) | |
2881 | return 1; | |
7539e11f KR |
2882 | for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail)) |
2883 | if (EQ (c, XCAR (tail))) | |
ecb7cb34 KH |
2884 | return 1; |
2885 | return 0; | |
2886 | } | |
2887 | ||
e4fe371d RS |
2888 | /* Record the input event C in various ways. */ |
2889 | ||
2890 | static void | |
2891 | record_char (c) | |
2892 | Lisp_Object c; | |
2893 | { | |
e8a50785 GM |
2894 | Lisp_Object help; |
2895 | ||
2896 | /* Don't record `help-echo' in recent_keys unless it shows some help | |
52be17cc GM |
2897 | message, and a different help than the previoiusly recorded |
2898 | event. */ | |
2899 | if (CONSP (c) && EQ (XCAR (c), Qhelp_echo)) | |
2900 | { | |
2901 | Lisp_Object help; | |
2902 | ||
2903 | help = Fnth (make_number (2), c); | |
2904 | if (STRINGP (help)) | |
2905 | { | |
2906 | int last_idx; | |
2907 | Lisp_Object last_c, last_help; | |
2908 | ||
2909 | last_idx = recent_keys_index - 1; | |
2910 | if (last_idx < 0) | |
2911 | last_idx = NUM_RECENT_KEYS - 1; | |
2912 | last_c = AREF (recent_keys, last_idx); | |
2913 | ||
2914 | if (!CONSP (last_c) | |
2915 | || !EQ (XCAR (last_c), Qhelp_echo) | |
2916 | || (last_help = Fnth (make_number (2), last_c), | |
2917 | !EQ (last_help, help))) | |
2918 | { | |
2919 | total_keys++; | |
2920 | ASET (recent_keys, recent_keys_index, c); | |
2921 | if (++recent_keys_index >= NUM_RECENT_KEYS) | |
2922 | recent_keys_index = 0; | |
2923 | } | |
2924 | } | |
2925 | } | |
2926 | else | |
e8a50785 GM |
2927 | { |
2928 | total_keys++; | |
2929 | ASET (recent_keys, recent_keys_index, c); | |
2930 | if (++recent_keys_index >= NUM_RECENT_KEYS) | |
2931 | recent_keys_index = 0; | |
2932 | } | |
52be17cc | 2933 | |
e4fe371d RS |
2934 | /* Write c to the dribble file. If c is a lispy event, write |
2935 | the event's symbol to the dribble file, in <brackets>. Bleaugh. | |
2936 | If you, dear reader, have a better idea, you've got the source. :-) */ | |
2937 | if (dribble) | |
2938 | { | |
2939 | if (INTEGERP (c)) | |
2940 | { | |
2941 | if (XUINT (c) < 0x100) | |
2942 | putc (XINT (c), dribble); | |
2943 | else | |
6de34814 | 2944 | fprintf (dribble, " 0x%x", (int) XUINT (c)); |
e4fe371d RS |
2945 | } |
2946 | else | |
2947 | { | |
2948 | Lisp_Object dribblee; | |
2949 | ||
2950 | /* If it's a structured event, take the event header. */ | |
2951 | dribblee = EVENT_HEAD (c); | |
2952 | ||
2953 | if (SYMBOLP (dribblee)) | |
2954 | { | |
2955 | putc ('<', dribble); | |
2956 | fwrite (XSYMBOL (dribblee)->name->data, sizeof (char), | |
fc932ac6 | 2957 | STRING_BYTES (XSYMBOL (dribblee)->name), |
e4fe371d RS |
2958 | dribble); |
2959 | putc ('>', dribble); | |
2960 | } | |
2961 | } | |
2962 | ||
2963 | fflush (dribble); | |
2964 | } | |
2965 | ||
e8a50785 GM |
2966 | if (!CONSP (c) || !EQ (Qhelp_echo, XCAR (c))) |
2967 | store_kbd_macro_char (c); | |
e4fe371d | 2968 | |
c43b1734 | 2969 | num_nonmacro_input_events++; |
e4fe371d RS |
2970 | } |
2971 | ||
284f4730 JB |
2972 | Lisp_Object |
2973 | print_help (object) | |
2974 | Lisp_Object object; | |
2975 | { | |
622de3e9 | 2976 | struct buffer *old = current_buffer; |
284f4730 | 2977 | Fprinc (object, Qnil); |
622de3e9 KH |
2978 | set_buffer_internal (XBUFFER (Vstandard_output)); |
2979 | call0 (intern ("help-mode")); | |
2980 | set_buffer_internal (old); | |
284f4730 JB |
2981 | return Qnil; |
2982 | } | |
2983 | ||
2984 | /* Copy out or in the info on where C-g should throw to. | |
2985 | This is used when running Lisp code from within get_char, | |
2986 | in case get_char is called recursively. | |
2987 | See read_process_output. */ | |
2988 | ||
dfcf069d | 2989 | static void |
284f4730 JB |
2990 | save_getcjmp (temp) |
2991 | jmp_buf temp; | |
2992 | { | |
2993 | bcopy (getcjmp, temp, sizeof getcjmp); | |
2994 | } | |
2995 | ||
dfcf069d | 2996 | static void |
284f4730 JB |
2997 | restore_getcjmp (temp) |
2998 | jmp_buf temp; | |
2999 | { | |
3000 | bcopy (temp, getcjmp, sizeof getcjmp); | |
3001 | } | |
284f4730 | 3002 | \f |
2eb6bfbe RM |
3003 | #ifdef HAVE_MOUSE |
3004 | ||
284f4730 JB |
3005 | /* Restore mouse tracking enablement. See Ftrack_mouse for the only use |
3006 | of this function. */ | |
a9d77f1f | 3007 | |
284f4730 JB |
3008 | static Lisp_Object |
3009 | tracking_off (old_value) | |
3010 | Lisp_Object old_value; | |
3011 | { | |
71edead1 RS |
3012 | do_mouse_tracking = old_value; |
3013 | if (NILP (old_value)) | |
284f4730 | 3014 | { |
284f4730 JB |
3015 | /* Redisplay may have been preempted because there was input |
3016 | available, and it assumes it will be called again after the | |
3017 | input has been processed. If the only input available was | |
3018 | the sort that we have just disabled, then we need to call | |
3019 | redisplay. */ | |
d9d4c147 | 3020 | if (!readable_events (1)) |
284f4730 JB |
3021 | { |
3022 | redisplay_preserve_echo_area (); | |
d9d4c147 | 3023 | get_input_pending (&input_pending, 1); |
284f4730 JB |
3024 | } |
3025 | } | |
30690496 | 3026 | return Qnil; |
284f4730 JB |
3027 | } |
3028 | ||
3029 | DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0, | |
4bb994d1 JB |
3030 | "Evaluate BODY with mouse movement events enabled.\n\ |
3031 | Within a `track-mouse' form, mouse motion generates input events that\n\ | |
3032 | you can read with `read-event'.\n\ | |
3033 | Normally, mouse motion is ignored.") | |
284f4730 JB |
3034 | (args) |
3035 | Lisp_Object args; | |
3036 | { | |
3037 | int count = specpdl_ptr - specpdl; | |
3038 | Lisp_Object val; | |
3039 | ||
a9d77f1f | 3040 | record_unwind_protect (tracking_off, do_mouse_tracking); |
284f4730 | 3041 | |
f3253854 | 3042 | do_mouse_tracking = Qt; |
df0f2ba1 | 3043 | |
284f4730 JB |
3044 | val = Fprogn (args); |
3045 | return unbind_to (count, val); | |
3046 | } | |
2eb6bfbe | 3047 | |
f3253854 KH |
3048 | /* If mouse has moved on some frame, return one of those frames. |
3049 | Return 0 otherwise. */ | |
3050 | ||
3051 | static FRAME_PTR | |
3052 | some_mouse_moved () | |
3053 | { | |
3054 | Lisp_Object tail, frame; | |
3055 | ||
3056 | FOR_EACH_FRAME (tail, frame) | |
3057 | { | |
3058 | if (XFRAME (frame)->mouse_moved) | |
3059 | return XFRAME (frame); | |
3060 | } | |
3061 | ||
3062 | return 0; | |
3063 | } | |
3064 | ||
2eb6bfbe | 3065 | #endif /* HAVE_MOUSE */ |
a612e298 RS |
3066 | \f |
3067 | /* Low level keyboard/mouse input. | |
3068 | kbd_buffer_store_event places events in kbd_buffer, and | |
0646c0dd | 3069 | kbd_buffer_get_event retrieves them. */ |
a612e298 RS |
3070 | |
3071 | /* Return true iff there are any events in the queue that read-char | |
3072 | would return. If this returns false, a read-char would block. */ | |
3073 | static int | |
d9d4c147 KH |
3074 | readable_events (do_timers_now) |
3075 | int do_timers_now; | |
a612e298 | 3076 | { |
4ec4ed6a RS |
3077 | if (do_timers_now) |
3078 | timer_check (do_timers_now); | |
3079 | ||
beecf6a1 KH |
3080 | if (kbd_fetch_ptr != kbd_store_ptr) |
3081 | return 1; | |
3082 | #ifdef HAVE_MOUSE | |
f3253854 | 3083 | if (!NILP (do_mouse_tracking) && some_mouse_moved ()) |
beecf6a1 KH |
3084 | return 1; |
3085 | #endif | |
1e8bd3da | 3086 | if (single_kboard) |
4c52b668 | 3087 | { |
c5fdd383 | 3088 | if (current_kboard->kbd_queue_has_data) |
4c52b668 KH |
3089 | return 1; |
3090 | } | |
3091 | else | |
3092 | { | |
c5fdd383 KH |
3093 | KBOARD *kb; |
3094 | for (kb = all_kboards; kb; kb = kb->next_kboard) | |
3095 | if (kb->kbd_queue_has_data) | |
4c52b668 KH |
3096 | return 1; |
3097 | } | |
beecf6a1 | 3098 | return 0; |
a612e298 RS |
3099 | } |
3100 | ||
3101 | /* Set this for debugging, to have a way to get out */ | |
3102 | int stop_character; | |
284f4730 | 3103 | |
c5fdd383 KH |
3104 | #ifdef MULTI_KBOARD |
3105 | static KBOARD * | |
3106 | event_to_kboard (event) | |
5798cf15 KH |
3107 | struct input_event *event; |
3108 | { | |
3109 | Lisp_Object frame; | |
3110 | frame = event->frame_or_window; | |
3111 | if (CONSP (frame)) | |
7539e11f | 3112 | frame = XCAR (frame); |
5798cf15 KH |
3113 | else if (WINDOWP (frame)) |
3114 | frame = WINDOW_FRAME (XWINDOW (frame)); | |
3115 | ||
3116 | /* There are still some events that don't set this field. | |
f5b56972 KH |
3117 | For now, just ignore the problem. |
3118 | Also ignore dead frames here. */ | |
3119 | if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame))) | |
5798cf15 KH |
3120 | return 0; |
3121 | else | |
c5fdd383 | 3122 | return FRAME_KBOARD (XFRAME (frame)); |
5798cf15 KH |
3123 | } |
3124 | #endif | |
3125 | ||
284f4730 JB |
3126 | /* Store an event obtained at interrupt level into kbd_buffer, fifo */ |
3127 | ||
3128 | void | |
3129 | kbd_buffer_store_event (event) | |
3130 | register struct input_event *event; | |
3131 | { | |
3132 | if (event->kind == no_event) | |
3133 | abort (); | |
3134 | ||
3135 | if (event->kind == ascii_keystroke) | |
3136 | { | |
e9bf89a0 | 3137 | register int c = event->code & 0377; |
284f4730 | 3138 | |
faf5e407 JB |
3139 | if (event->modifiers & ctrl_modifier) |
3140 | c = make_ctrl_char (c); | |
3141 | ||
9fd7d808 RS |
3142 | c |= (event->modifiers |
3143 | & (meta_modifier | alt_modifier | |
3144 | | hyper_modifier | super_modifier)); | |
3145 | ||
86e5706b | 3146 | if (c == quit_char) |
284f4730 | 3147 | { |
3e51c7b7 | 3148 | extern SIGTYPE interrupt_signal (); |
c5fdd383 KH |
3149 | #ifdef MULTI_KBOARD |
3150 | KBOARD *kb; | |
5798cf15 KH |
3151 | struct input_event *sp; |
3152 | ||
1e8bd3da | 3153 | if (single_kboard |
c5fdd383 KH |
3154 | && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)), |
3155 | kb != current_kboard)) | |
5798cf15 | 3156 | { |
c5fdd383 | 3157 | kb->kbd_queue |
5798cf15 KH |
3158 | = Fcons (make_lispy_switch_frame (event->frame_or_window), |
3159 | Fcons (make_number (c), Qnil)); | |
c5fdd383 | 3160 | kb->kbd_queue_has_data = 1; |
5798cf15 KH |
3161 | for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++) |
3162 | { | |
3163 | if (sp == kbd_buffer + KBD_BUFFER_SIZE) | |
3164 | sp = kbd_buffer; | |
3165 | ||
c5fdd383 | 3166 | if (event_to_kboard (sp) == kb) |
5798cf15 KH |
3167 | { |
3168 | sp->kind = no_event; | |
3169 | sp->frame_or_window = Qnil; | |
da8f7368 | 3170 | sp->arg = Qnil; |
5798cf15 KH |
3171 | } |
3172 | } | |
3173 | return; | |
3174 | } | |
3175 | #endif | |
3e51c7b7 | 3176 | |
284f4730 | 3177 | /* If this results in a quit_char being returned to Emacs as |
3c370943 | 3178 | input, set Vlast_event_frame properly. If this doesn't |
284f4730 | 3179 | get returned to Emacs as an event, the next event read |
ff11dfa1 | 3180 | will set Vlast_event_frame again, so this is safe to do. */ |
4bb994d1 | 3181 | { |
9b8eb840 | 3182 | Lisp_Object focus; |
4bb994d1 | 3183 | |
9b8eb840 | 3184 | focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window)); |
4bb994d1 | 3185 | if (NILP (focus)) |
beecf6a1 | 3186 | focus = event->frame_or_window; |
4c52b668 KH |
3187 | internal_last_event_frame = focus; |
3188 | Vlast_event_frame = focus; | |
4bb994d1 | 3189 | } |
3e51c7b7 | 3190 | |
ffd56f97 | 3191 | last_event_timestamp = event->timestamp; |
284f4730 JB |
3192 | interrupt_signal (); |
3193 | return; | |
3194 | } | |
3195 | ||
3196 | if (c && c == stop_character) | |
3197 | { | |
3198 | sys_suspend (); | |
3199 | return; | |
3200 | } | |
284f4730 | 3201 | } |
3fe8e9a2 RS |
3202 | /* Don't insert two buffer_switch_event's in a row. |
3203 | Just ignore the second one. */ | |
3204 | else if (event->kind == buffer_switch_event | |
3205 | && kbd_fetch_ptr != kbd_store_ptr | |
3206 | && kbd_store_ptr->kind == buffer_switch_event) | |
3207 | return; | |
284f4730 | 3208 | |
beecf6a1 KH |
3209 | if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE) |
3210 | kbd_store_ptr = kbd_buffer; | |
284f4730 JB |
3211 | |
3212 | /* Don't let the very last slot in the buffer become full, | |
3213 | since that would make the two pointers equal, | |
3214 | and that is indistinguishable from an empty buffer. | |
3215 | Discard the event if it would fill the last slot. */ | |
beecf6a1 | 3216 | if (kbd_fetch_ptr - 1 != kbd_store_ptr) |
284f4730 | 3217 | { |
da8f7368 GM |
3218 | int idx; |
3219 | ||
3220 | #if 0 /* The selection_request_event case looks bogus, and it's error | |
3221 | prone to assign individual members for other events, in case | |
3222 | the input_event structure is changed. --2000-07-13, gerd. */ | |
7ee32cda | 3223 | struct input_event *sp = kbd_store_ptr; |
612b78ef | 3224 | sp->kind = event->kind; |
27203ead RS |
3225 | if (event->kind == selection_request_event) |
3226 | { | |
3227 | /* We must not use the ordinary copying code for this case, | |
3228 | since `part' is an enum and copying it might not copy enough | |
3229 | in this case. */ | |
612b78ef | 3230 | bcopy (event, (char *) sp, sizeof (*event)); |
27203ead RS |
3231 | } |
3232 | else | |
da8f7368 | 3233 | |
27203ead | 3234 | { |
612b78ef KH |
3235 | sp->code = event->code; |
3236 | sp->part = event->part; | |
3237 | sp->frame_or_window = event->frame_or_window; | |
da8f7368 | 3238 | sp->arg = event->arg; |
612b78ef KH |
3239 | sp->modifiers = event->modifiers; |
3240 | sp->x = event->x; | |
3241 | sp->y = event->y; | |
3242 | sp->timestamp = event->timestamp; | |
27203ead | 3243 | } |
da8f7368 GM |
3244 | #else |
3245 | *kbd_store_ptr = *event; | |
3246 | #endif | |
284f4730 | 3247 | |
da8f7368 GM |
3248 | idx = 2 * (kbd_store_ptr - kbd_buffer); |
3249 | ASET (kbd_buffer_gcpro, idx, event->frame_or_window); | |
3250 | ASET (kbd_buffer_gcpro, idx + 1, event->arg); | |
3251 | ++kbd_store_ptr; | |
284f4730 JB |
3252 | } |
3253 | } | |
8dfd92c9 GM |
3254 | |
3255 | ||
f139e559 | 3256 | /* Generate HELP_EVENT input_events in BUFP which has room for |
0bbfdc25 GM |
3257 | SIZE events. If there's not enough room in BUFP, ignore this |
3258 | event. | |
8dfd92c9 GM |
3259 | |
3260 | HELP is the help form. | |
3261 | ||
3262 | FRAME is the frame on which the help is generated. OBJECT is the | |
5b2ec2d0 GM |
3263 | Lisp object where the help was found (a buffer, a string, an |
3264 | overlay, or nil if neither from a string nor from a buffer. POS is | |
3265 | the position within OBJECT where the help was found. | |
8dfd92c9 GM |
3266 | |
3267 | Value is the number of input_events generated. */ | |
3268 | ||
3269 | int | |
0bbfdc25 | 3270 | gen_help_event (bufp, size, help, frame, window, object, pos) |
8dfd92c9 | 3271 | struct input_event *bufp; |
0bbfdc25 | 3272 | int size; |
2190735a | 3273 | Lisp_Object help, frame, object, window; |
8dfd92c9 GM |
3274 | int pos; |
3275 | { | |
0bbfdc25 GM |
3276 | int nevents_stored = 0; |
3277 | ||
3278 | if (size >= 2) | |
3279 | { | |
3280 | bufp->kind = HELP_EVENT; | |
3281 | bufp->frame_or_window = frame; | |
3282 | bufp->arg = object; | |
3283 | bufp->x = make_number (pos); | |
3284 | bufp->code = 0; | |
8dfd92c9 | 3285 | |
0bbfdc25 GM |
3286 | ++bufp; |
3287 | bufp->kind = HELP_EVENT; | |
3288 | bufp->frame_or_window = WINDOWP (window) ? window : frame; | |
3289 | bufp->arg = help; | |
3290 | bufp->code = 1; | |
3291 | nevents_stored = 2; | |
3292 | } | |
2190735a | 3293 | |
0bbfdc25 | 3294 | return nevents_stored; |
8dfd92c9 GM |
3295 | } |
3296 | ||
3297 | ||
3298 | /* Store HELP_EVENTs for HELP on FRAME in the input queue. */ | |
3299 | ||
3300 | void | |
3301 | kbd_buffer_store_help_event (frame, help) | |
3302 | Lisp_Object frame, help; | |
3303 | { | |
3304 | struct input_event event; | |
3305 | ||
3306 | event.kind = HELP_EVENT; | |
3307 | event.frame_or_window = frame; | |
3308 | event.arg = Qnil; | |
3309 | event.x = make_number (0); | |
3310 | event.code = 0; | |
3311 | kbd_buffer_store_event (&event); | |
3312 | ||
3313 | event.kind = HELP_EVENT; | |
3314 | event.frame_or_window = frame; | |
3315 | event.arg = help; | |
3316 | event.x = make_number (0); | |
3317 | event.code = 1; | |
3318 | kbd_buffer_store_event (&event); | |
3319 | } | |
3320 | ||
a612e298 | 3321 | \f |
07de30b9 GV |
3322 | /* Discard any mouse events in the event buffer by setting them to |
3323 | no_event. */ | |
3324 | void | |
3325 | discard_mouse_events () | |
3326 | { | |
3327 | struct input_event *sp; | |
3328 | for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++) | |
3329 | { | |
3330 | if (sp == kbd_buffer + KBD_BUFFER_SIZE) | |
3331 | sp = kbd_buffer; | |
3332 | ||
3333 | if (sp->kind == mouse_click | |
3334 | #ifdef WINDOWSNT | |
3335 | || sp->kind == w32_scroll_bar_click | |
3336 | #endif | |
3337 | || sp->kind == scroll_bar_click) | |
3338 | { | |
3339 | sp->kind = no_event; | |
3340 | } | |
3341 | } | |
3342 | } | |
eeabfe76 | 3343 | |
0bbfdc25 GM |
3344 | |
3345 | /* Return non-zero if there are any real events waiting in the event | |
3346 | buffer, not counting `no_event's. | |
3347 | ||
3348 | If DISCARD is non-zero, discard no_event events at the front of | |
3349 | the input queue, possibly leaving the input queue empty if there | |
3350 | are no real input events. */ | |
3351 | ||
eeabfe76 EZ |
3352 | int |
3353 | kbd_buffer_events_waiting (discard) | |
3354 | int discard; | |
3355 | { | |
3356 | struct input_event *sp; | |
0bbfdc25 GM |
3357 | |
3358 | for (sp = kbd_fetch_ptr; | |
3359 | sp != kbd_store_ptr && sp->kind == no_event; | |
3360 | ++sp) | |
eeabfe76 EZ |
3361 | { |
3362 | if (sp == kbd_buffer + KBD_BUFFER_SIZE) | |
3363 | sp = kbd_buffer; | |
eeabfe76 | 3364 | } |
0bbfdc25 | 3365 | |
eeabfe76 EZ |
3366 | if (discard) |
3367 | kbd_fetch_ptr = sp; | |
0bbfdc25 GM |
3368 | |
3369 | return sp != kbd_store_ptr && sp->kind != no_event; | |
eeabfe76 | 3370 | } |
0bbfdc25 | 3371 | |
07de30b9 | 3372 | \f |
0bbfdc25 GM |
3373 | /* Clear input event EVENT. */ |
3374 | ||
3375 | static INLINE void | |
3376 | clear_event (event) | |
3377 | struct input_event *event; | |
3378 | { | |
3379 | int idx = 2 * (event - kbd_buffer); | |
3380 | ASET (kbd_buffer_gcpro, idx, Qnil); | |
3381 | ASET (kbd_buffer_gcpro, idx + 1, Qnil); | |
3382 | event->kind = no_event; | |
3383 | } | |
3384 | ||
3385 | ||
a612e298 RS |
3386 | /* Read one event from the event buffer, waiting if necessary. |
3387 | The value is a Lisp object representing the event. | |
3388 | The value is nil for an event that should be ignored, | |
3389 | or that was handled here. | |
3390 | We always read and discard one event. */ | |
284f4730 JB |
3391 | |
3392 | static Lisp_Object | |
83d68044 | 3393 | kbd_buffer_get_event (kbp, used_mouse_menu) |
410d4de9 | 3394 | KBOARD **kbp; |
83d68044 | 3395 | int *used_mouse_menu; |
284f4730 JB |
3396 | { |
3397 | register int c; | |
3398 | Lisp_Object obj; | |
3399 | ||
3400 | if (noninteractive) | |
3401 | { | |
3402 | c = getchar (); | |
18cd2eeb | 3403 | XSETINT (obj, c); |
f5b56972 | 3404 | *kbp = current_kboard; |
284f4730 JB |
3405 | return obj; |
3406 | } | |
3407 | ||
3408 | /* Wait until there is input available. */ | |
3409 | for (;;) | |
3410 | { | |
beecf6a1 KH |
3411 | if (kbd_fetch_ptr != kbd_store_ptr) |
3412 | break; | |
3413 | #ifdef HAVE_MOUSE | |
f3253854 | 3414 | if (!NILP (do_mouse_tracking) && some_mouse_moved ()) |
284f4730 | 3415 | break; |
beecf6a1 | 3416 | #endif |
284f4730 JB |
3417 | |
3418 | /* If the quit flag is set, then read_char will return | |
3419 | quit_char, so that counts as "available input." */ | |
3420 | if (!NILP (Vquit_flag)) | |
3421 | quit_throw_to_read_char (); | |
3422 | ||
3423 | /* One way or another, wait until input is available; then, if | |
3424 | interrupt handlers have not read it, read it now. */ | |
3425 | ||
3426 | #ifdef OLDVMS | |
3427 | wait_for_kbd_input (); | |
3428 | #else | |
3429 | /* Note SIGIO has been undef'd if FIONREAD is missing. */ | |
3430 | #ifdef SIGIO | |
3431 | gobble_input (0); | |
3432 | #endif /* SIGIO */ | |
beecf6a1 KH |
3433 | if (kbd_fetch_ptr != kbd_store_ptr) |
3434 | break; | |
3435 | #ifdef HAVE_MOUSE | |
f3253854 | 3436 | if (!NILP (do_mouse_tracking) && some_mouse_moved ()) |
beecf6a1 KH |
3437 | break; |
3438 | #endif | |
3439 | { | |
3440 | Lisp_Object minus_one; | |
f76475ad | 3441 | |
beecf6a1 | 3442 | XSETINT (minus_one, -1); |
d9d4c147 | 3443 | wait_reading_process_input (0, 0, minus_one, 1); |
284f4730 | 3444 | |
beecf6a1 KH |
3445 | if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr) |
3446 | /* Pass 1 for EXPECT since we just waited to have input. */ | |
3447 | read_avail_input (1); | |
3448 | } | |
284f4730 JB |
3449 | #endif /* not VMS */ |
3450 | } | |
3451 | ||
303b5b3f RS |
3452 | if (CONSP (Vunread_command_events)) |
3453 | { | |
3454 | Lisp_Object first; | |
7539e11f KR |
3455 | first = XCAR (Vunread_command_events); |
3456 | Vunread_command_events = XCDR (Vunread_command_events); | |
303b5b3f RS |
3457 | *kbp = current_kboard; |
3458 | return first; | |
3459 | } | |
3460 | ||
284f4730 JB |
3461 | /* At this point, we know that there is a readable event available |
3462 | somewhere. If the event queue is empty, then there must be a | |
3463 | mouse movement enabled and available. */ | |
beecf6a1 | 3464 | if (kbd_fetch_ptr != kbd_store_ptr) |
284f4730 | 3465 | { |
cd21b839 | 3466 | struct input_event *event; |
3e51c7b7 | 3467 | |
beecf6a1 KH |
3468 | event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE) |
3469 | ? kbd_fetch_ptr | |
3470 | : kbd_buffer); | |
3e51c7b7 | 3471 | |
cd21b839 | 3472 | last_event_timestamp = event->timestamp; |
cd21b839 | 3473 | |
c5fdd383 KH |
3474 | #ifdef MULTI_KBOARD |
3475 | *kbp = event_to_kboard (event); | |
3476 | if (*kbp == 0) | |
3477 | *kbp = current_kboard; /* Better than returning null ptr? */ | |
5798cf15 | 3478 | #else |
c5fdd383 | 3479 | *kbp = &the_only_kboard; |
5798cf15 | 3480 | #endif |
beecf6a1 | 3481 | |
4bb994d1 JB |
3482 | obj = Qnil; |
3483 | ||
48e416d4 | 3484 | /* These two kinds of events get special handling |
a612e298 RS |
3485 | and don't actually appear to the command loop. |
3486 | We return nil for them. */ | |
48e416d4 RS |
3487 | if (event->kind == selection_request_event) |
3488 | { | |
598a9fa7 | 3489 | #ifdef HAVE_X11 |
1e8bd3da RS |
3490 | struct input_event copy; |
3491 | ||
4581e928 RS |
3492 | /* Remove it from the buffer before processing it, |
3493 | since otherwise swallow_events will see it | |
3494 | and process it again. */ | |
1e8bd3da | 3495 | copy = *event; |
beecf6a1 | 3496 | kbd_fetch_ptr = event + 1; |
d9d4c147 | 3497 | input_pending = readable_events (0); |
4581e928 | 3498 | x_handle_selection_request (©); |
598a9fa7 JB |
3499 | #else |
3500 | /* We're getting selection request events, but we don't have | |
3501 | a window system. */ | |
3502 | abort (); | |
3503 | #endif | |
48e416d4 RS |
3504 | } |
3505 | ||
1e12dd87 | 3506 | else if (event->kind == selection_clear_event) |
48e416d4 | 3507 | { |
598a9fa7 | 3508 | #ifdef HAVE_X11 |
e0301c07 RS |
3509 | struct input_event copy; |
3510 | ||
3511 | /* Remove it from the buffer before processing it. */ | |
3512 | copy = *event; | |
beecf6a1 | 3513 | kbd_fetch_ptr = event + 1; |
d9d4c147 | 3514 | input_pending = readable_events (0); |
90c2bb0c | 3515 | x_handle_selection_clear (©); |
598a9fa7 JB |
3516 | #else |
3517 | /* We're getting selection request events, but we don't have | |
3518 | a window system. */ | |
3519 | abort (); | |
3520 | #endif | |
48e416d4 | 3521 | } |
1a578e9b | 3522 | #if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (macintosh) |
990acea3 RS |
3523 | else if (event->kind == delete_window_event) |
3524 | { | |
bbdc2092 RS |
3525 | /* Make an event (delete-frame (FRAME)). */ |
3526 | obj = Fcons (event->frame_or_window, Qnil); | |
af17bd2b | 3527 | obj = Fcons (Qdelete_frame, Fcons (obj, Qnil)); |
beecf6a1 | 3528 | kbd_fetch_ptr = event + 1; |
af17bd2b | 3529 | } |
1a578e9b AC |
3530 | #endif |
3531 | #if defined (HAVE_X11) || defined (HAVE_NTGUI) | |
af17bd2b KH |
3532 | else if (event->kind == iconify_event) |
3533 | { | |
3534 | /* Make an event (iconify-frame (FRAME)). */ | |
3535 | obj = Fcons (event->frame_or_window, Qnil); | |
3536 | obj = Fcons (Qiconify_frame, Fcons (obj, Qnil)); | |
beecf6a1 | 3537 | kbd_fetch_ptr = event + 1; |
af17bd2b KH |
3538 | } |
3539 | else if (event->kind == deiconify_event) | |
3540 | { | |
3541 | /* Make an event (make-frame-visible (FRAME)). */ | |
3542 | obj = Fcons (event->frame_or_window, Qnil); | |
3543 | obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil)); | |
beecf6a1 | 3544 | kbd_fetch_ptr = event + 1; |
990acea3 RS |
3545 | } |
3546 | #endif | |
a8015ab5 KH |
3547 | else if (event->kind == buffer_switch_event) |
3548 | { | |
3549 | /* The value doesn't matter here; only the type is tested. */ | |
18cd2eeb | 3550 | XSETBUFFER (obj, current_buffer); |
beecf6a1 | 3551 | kbd_fetch_ptr = event + 1; |
a8015ab5 | 3552 | } |
1a578e9b | 3553 | #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh) |
099787c1 RS |
3554 | else if (event->kind == menu_bar_activate_event) |
3555 | { | |
3556 | kbd_fetch_ptr = event + 1; | |
d9d4c147 | 3557 | input_pending = readable_events (0); |
e649d076 RS |
3558 | if (FRAME_LIVE_P (XFRAME (event->frame_or_window))) |
3559 | x_activate_menubar (XFRAME (event->frame_or_window)); | |
099787c1 | 3560 | } |
1161d367 GV |
3561 | #endif |
3562 | #ifdef WINDOWSNT | |
3563 | else if (event->kind == language_change_event) | |
3564 | { | |
3565 | /* Make an event (language-change (FRAME CHARSET LCID)). */ | |
3566 | obj = Fcons (event->modifiers, Qnil); | |
3567 | obj = Fcons (event->code, Qnil); | |
3568 | obj = Fcons (event->frame_or_window, obj); | |
3569 | obj = Fcons (Qlanguage_change, Fcons (obj, Qnil)); | |
3570 | kbd_fetch_ptr = event + 1; | |
3571 | } | |
099787c1 | 3572 | #endif |
a612e298 | 3573 | /* Just discard these, by returning nil. |
c5fdd383 | 3574 | With MULTI_KBOARD, these events are used as placeholders |
5798cf15 KH |
3575 | when we need to randomly delete events from the queue. |
3576 | (They shouldn't otherwise be found in the buffer, | |
3577 | but on some machines it appears they do show up | |
c5fdd383 | 3578 | even without MULTI_KBOARD.) */ |
07de30b9 GV |
3579 | /* On Windows NT/9X, no_event is used to delete extraneous |
3580 | mouse events during a popup-menu call. */ | |
a612e298 | 3581 | else if (event->kind == no_event) |
beecf6a1 | 3582 | kbd_fetch_ptr = event + 1; |
7ee32cda GM |
3583 | else if (event->kind == HELP_EVENT) |
3584 | { | |
e4457b09 | 3585 | /* There are always two HELP_EVENTs in the input queue. */ |
2190735a | 3586 | Lisp_Object object, position, help, frame, window; |
e4457b09 | 3587 | |
8dfd92c9 GM |
3588 | xassert (event->code == 0); |
3589 | frame = event->frame_or_window; | |
3590 | object = event->arg; | |
3591 | position = event->x; | |
0bbfdc25 | 3592 | clear_event (event); |
e4457b09 GM |
3593 | |
3594 | kbd_fetch_ptr = event + 1; | |
3595 | event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE) | |
3596 | ? kbd_fetch_ptr | |
3597 | : kbd_buffer); | |
3598 | xassert (event->code == 1); | |
3599 | help = event->arg; | |
3600 | window = event->frame_or_window; | |
2190735a GM |
3601 | if (!WINDOWP (window)) |
3602 | window = Qnil; | |
3603 | obj = Fcons (Qhelp_echo, | |
3604 | list5 (frame, help, window, object, position)); | |
0bbfdc25 | 3605 | clear_event (event); |
e4457b09 | 3606 | kbd_fetch_ptr = event + 1; |
7ee32cda | 3607 | } |
c51c7093 GM |
3608 | else if (event->kind == FOCUS_IN_EVENT) |
3609 | { | |
3610 | /* Notification of a FocusIn event. The frame receiving the | |
3611 | focus is in event->frame_or_window. Generate a | |
3612 | switch-frame event if necessary. */ | |
3613 | Lisp_Object frame, focus; | |
3614 | ||
3615 | frame = event->frame_or_window; | |
3616 | focus = FRAME_FOCUS_FRAME (XFRAME (frame)); | |
3617 | if (FRAMEP (focus)) | |
3618 | frame = focus; | |
3619 | ||
3620 | if (!EQ (frame, internal_last_event_frame) | |
3621 | && !EQ (frame, selected_frame)) | |
3622 | obj = make_lispy_switch_frame (frame); | |
3623 | internal_last_event_frame = frame; | |
3624 | kbd_fetch_ptr = event + 1; | |
3625 | } | |
1e12dd87 RS |
3626 | else |
3627 | { | |
c51c7093 GM |
3628 | /* If this event is on a different frame, return a switch-frame this |
3629 | time, and leave the event in the queue for next time. */ | |
9b8eb840 | 3630 | Lisp_Object frame; |
1e12dd87 | 3631 | Lisp_Object focus; |
7b4aedb9 | 3632 | |
9b8eb840 | 3633 | frame = event->frame_or_window; |
2470a66f | 3634 | if (CONSP (frame)) |
7539e11f | 3635 | frame = XCAR (frame); |
2470a66f | 3636 | else if (WINDOWP (frame)) |
1e12dd87 | 3637 | frame = WINDOW_FRAME (XWINDOW (frame)); |
4bb994d1 | 3638 | |
1e12dd87 RS |
3639 | focus = FRAME_FOCUS_FRAME (XFRAME (frame)); |
3640 | if (! NILP (focus)) | |
3641 | frame = focus; | |
07d2b8de | 3642 | |
4c52b668 | 3643 | if (! EQ (frame, internal_last_event_frame) |
788f89eb | 3644 | && !EQ (frame, selected_frame)) |
1e12dd87 | 3645 | obj = make_lispy_switch_frame (frame); |
4c52b668 | 3646 | internal_last_event_frame = frame; |
4bb994d1 | 3647 | |
1e12dd87 RS |
3648 | /* If we didn't decide to make a switch-frame event, go ahead |
3649 | and build a real event from the queue entry. */ | |
cd21b839 | 3650 | |
1e12dd87 RS |
3651 | if (NILP (obj)) |
3652 | { | |
da8f7368 GM |
3653 | int idx; |
3654 | ||
1e12dd87 | 3655 | obj = make_lispy_event (event); |
da8f7368 | 3656 | |
e98a93eb | 3657 | #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) |
83d68044 KH |
3658 | /* If this was a menu selection, then set the flag to inhibit |
3659 | writing to last_nonmenu_event. Don't do this if the event | |
3660 | we're returning is (menu-bar), though; that indicates the | |
3661 | beginning of the menu sequence, and we might as well leave | |
3662 | that as the `event with parameters' for this selection. */ | |
da8f7368 GM |
3663 | if (used_mouse_menu |
3664 | && !EQ (event->frame_or_window, event->arg) | |
3665 | && (event->kind == MENU_BAR_EVENT | |
3666 | || event->kind == TOOL_BAR_EVENT)) | |
83d68044 KH |
3667 | *used_mouse_menu = 1; |
3668 | #endif | |
1e12dd87 RS |
3669 | |
3670 | /* Wipe out this event, to catch bugs. */ | |
0bbfdc25 | 3671 | clear_event (event); |
beecf6a1 | 3672 | kbd_fetch_ptr = event + 1; |
1e12dd87 | 3673 | } |
4bb994d1 | 3674 | } |
284f4730 | 3675 | } |
2eb6bfbe | 3676 | #ifdef HAVE_MOUSE |
a612e298 | 3677 | /* Try generating a mouse motion event. */ |
f3253854 | 3678 | else if (!NILP (do_mouse_tracking) && some_mouse_moved ()) |
284f4730 | 3679 | { |
f3253854 | 3680 | FRAME_PTR f = some_mouse_moved (); |
7b4aedb9 | 3681 | Lisp_Object bar_window; |
3c370943 | 3682 | enum scroll_bar_part part; |
e5d77022 JB |
3683 | Lisp_Object x, y; |
3684 | unsigned long time; | |
284f4730 | 3685 | |
c5fdd383 | 3686 | *kbp = current_kboard; |
e177ac3a RS |
3687 | /* Note that this uses F to determine which display to look at. |
3688 | If there is no valid info, it does not store anything | |
3689 | so x remains nil. */ | |
3690 | x = Qnil; | |
dd26ab75 | 3691 | (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time); |
4bb994d1 JB |
3692 | |
3693 | obj = Qnil; | |
284f4730 | 3694 | |
4bb994d1 JB |
3695 | /* Decide if we should generate a switch-frame event. Don't |
3696 | generate switch-frame events for motion outside of all Emacs | |
3697 | frames. */ | |
e177ac3a | 3698 | if (!NILP (x) && f) |
cd21b839 | 3699 | { |
9b8eb840 | 3700 | Lisp_Object frame; |
4bb994d1 | 3701 | |
9b8eb840 | 3702 | frame = FRAME_FOCUS_FRAME (f); |
4bb994d1 | 3703 | if (NILP (frame)) |
18cd2eeb | 3704 | XSETFRAME (frame, f); |
4bb994d1 | 3705 | |
4c52b668 | 3706 | if (! EQ (frame, internal_last_event_frame) |
788f89eb | 3707 | && !EQ (frame, selected_frame)) |
764cb3f9 | 3708 | obj = make_lispy_switch_frame (frame); |
4c52b668 | 3709 | internal_last_event_frame = frame; |
cd21b839 | 3710 | } |
4bb994d1 | 3711 | |
df0f2ba1 | 3712 | /* If we didn't decide to make a switch-frame event, go ahead and |
4bb994d1 | 3713 | return a mouse-motion event. */ |
e177ac3a | 3714 | if (!NILP (x) && NILP (obj)) |
7b4aedb9 | 3715 | obj = make_lispy_movement (f, bar_window, part, x, y, time); |
6cbff1cb | 3716 | } |
2eb6bfbe | 3717 | #endif /* HAVE_MOUSE */ |
284f4730 JB |
3718 | else |
3719 | /* We were promised by the above while loop that there was | |
3720 | something for us to read! */ | |
3721 | abort (); | |
3722 | ||
d9d4c147 | 3723 | input_pending = readable_events (0); |
284f4730 | 3724 | |
4c52b668 | 3725 | Vlast_event_frame = internal_last_event_frame; |
3c370943 | 3726 | |
284f4730 JB |
3727 | return (obj); |
3728 | } | |
a612e298 RS |
3729 | \f |
3730 | /* Process any events that are not user-visible, | |
3731 | then return, without reading any user-visible events. */ | |
3a3b9632 RS |
3732 | |
3733 | void | |
d9d4c147 KH |
3734 | swallow_events (do_display) |
3735 | int do_display; | |
3a3b9632 | 3736 | { |
87dd9b9b RS |
3737 | int old_timers_run; |
3738 | ||
beecf6a1 | 3739 | while (kbd_fetch_ptr != kbd_store_ptr) |
3a3b9632 RS |
3740 | { |
3741 | struct input_event *event; | |
3742 | ||
beecf6a1 KH |
3743 | event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE) |
3744 | ? kbd_fetch_ptr | |
3745 | : kbd_buffer); | |
3a3b9632 RS |
3746 | |
3747 | last_event_timestamp = event->timestamp; | |
3748 | ||
3749 | /* These two kinds of events get special handling | |
3750 | and don't actually appear to the command loop. */ | |
3751 | if (event->kind == selection_request_event) | |
3752 | { | |
3753 | #ifdef HAVE_X11 | |
4581e928 | 3754 | struct input_event copy; |
e0301c07 RS |
3755 | |
3756 | /* Remove it from the buffer before processing it, | |
3757 | since otherwise swallow_events called recursively could see it | |
3758 | and process it again. */ | |
4581e928 | 3759 | copy = *event; |
beecf6a1 | 3760 | kbd_fetch_ptr = event + 1; |
d9d4c147 | 3761 | input_pending = readable_events (0); |
4581e928 | 3762 | x_handle_selection_request (©); |
3a3b9632 RS |
3763 | #else |
3764 | /* We're getting selection request events, but we don't have | |
3765 | a window system. */ | |
3766 | abort (); | |
3767 | #endif | |
3768 | } | |
3769 | ||
3770 | else if (event->kind == selection_clear_event) | |
3771 | { | |
3772 | #ifdef HAVE_X11 | |
e0301c07 RS |
3773 | struct input_event copy; |
3774 | ||
3775 | /* Remove it from the buffer before processing it, */ | |
3776 | copy = *event; | |
3777 | ||
beecf6a1 | 3778 | kbd_fetch_ptr = event + 1; |
d9d4c147 | 3779 | input_pending = readable_events (0); |
90c2bb0c | 3780 | x_handle_selection_clear (©); |
3a3b9632 RS |
3781 | #else |
3782 | /* We're getting selection request events, but we don't have | |
3783 | a window system. */ | |
3784 | abort (); | |
3785 | #endif | |
3786 | } | |
3787 | else | |
3788 | break; | |
3789 | } | |
3790 | ||
87dd9b9b | 3791 | old_timers_run = timers_run; |
d9d4c147 | 3792 | get_input_pending (&input_pending, 1); |
87dd9b9b RS |
3793 | |
3794 | if (timers_run != old_timers_run && do_display) | |
3795 | redisplay_preserve_echo_area (); | |
3a3b9632 | 3796 | } |
a612e298 | 3797 | \f |
d9d4c147 KH |
3798 | static EMACS_TIME timer_idleness_start_time; |
3799 | ||
3800 | /* Record the start of when Emacs is idle, | |
3801 | for the sake of running idle-time timers. */ | |
3802 | ||
07a59269 | 3803 | void |
d9d4c147 KH |
3804 | timer_start_idle () |
3805 | { | |
3806 | Lisp_Object timers; | |
3807 | ||
3808 | /* If we are already in the idle state, do nothing. */ | |
3809 | if (! EMACS_TIME_NEG_P (timer_idleness_start_time)) | |
3810 | return; | |
3811 | ||
3812 | EMACS_GET_TIME (timer_idleness_start_time); | |
3813 | ||
3814 | /* Mark all idle-time timers as once again candidates for running. */ | |
7539e11f | 3815 | for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers)) |
d9d4c147 KH |
3816 | { |
3817 | Lisp_Object timer; | |
3818 | ||
7539e11f | 3819 | timer = XCAR (timers); |
d9d4c147 KH |
3820 | |
3821 | if (!VECTORP (timer) || XVECTOR (timer)->size != 8) | |
3822 | continue; | |
3823 | XVECTOR (timer)->contents[0] = Qnil; | |
3824 | } | |
3825 | } | |
3826 | ||
3827 | /* Record that Emacs is no longer idle, so stop running idle-time timers. */ | |
3828 | ||
07a59269 | 3829 | void |
d9d4c147 KH |
3830 | timer_stop_idle () |
3831 | { | |
3832 | EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1); | |
3833 | } | |
3834 | ||
e044e87c RS |
3835 | /* This is only for debugging. */ |
3836 | struct input_event last_timer_event; | |
3837 | ||
c04cbc3b RS |
3838 | /* Check whether a timer has fired. To prevent larger problems we simply |
3839 | disregard elements that are not proper timers. Do not make a circular | |
3840 | timer list for the time being. | |
3841 | ||
3842 | Returns the number of seconds to wait until the next timer fires. If a | |
3843 | timer is triggering now, return zero seconds. | |
3844 | If no timer is active, return -1 seconds. | |
3845 | ||
4ec4ed6a RS |
3846 | If a timer is ripe, we run it, with quitting turned off. |
3847 | ||
3848 | DO_IT_NOW is now ignored. It used to mean that we should | |
3849 | run the timer directly instead of queueing a timer-event. | |
3850 | Now we always run timers directly. */ | |
c04cbc3b RS |
3851 | |
3852 | EMACS_TIME | |
3853 | timer_check (do_it_now) | |
3854 | int do_it_now; | |
3855 | { | |
3856 | EMACS_TIME nexttime; | |
9291c072 RS |
3857 | EMACS_TIME now, idleness_now; |
3858 | Lisp_Object timers, idle_timers, chosen_timer; | |
9291c072 | 3859 | struct gcpro gcpro1, gcpro2, gcpro3; |
c04cbc3b | 3860 | |
c04cbc3b RS |
3861 | EMACS_SET_SECS (nexttime, -1); |
3862 | EMACS_SET_USECS (nexttime, -1); | |
3863 | ||
9291c072 | 3864 | /* Always consider the ordinary timers. */ |
7ea13e12 | 3865 | timers = Vtimer_list; |
9291c072 RS |
3866 | /* Consider the idle timers only if Emacs is idle. */ |
3867 | if (! EMACS_TIME_NEG_P (timer_idleness_start_time)) | |
3868 | idle_timers = Vtimer_idle_list; | |
3869 | else | |
3870 | idle_timers = Qnil; | |
3871 | chosen_timer = Qnil; | |
3872 | GCPRO3 (timers, idle_timers, chosen_timer); | |
7ea13e12 | 3873 | |
9291c072 | 3874 | if (CONSP (timers) || CONSP (idle_timers)) |
c04cbc3b | 3875 | { |
9291c072 RS |
3876 | EMACS_GET_TIME (now); |
3877 | if (! EMACS_TIME_NEG_P (timer_idleness_start_time)) | |
3878 | EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time); | |
3879 | } | |
c04cbc3b | 3880 | |
9291c072 RS |
3881 | while (CONSP (timers) || CONSP (idle_timers)) |
3882 | { | |
9291c072 | 3883 | Lisp_Object *vector; |
8c907a56 | 3884 | Lisp_Object timer = Qnil, idle_timer = Qnil; |
9291c072 RS |
3885 | EMACS_TIME timer_time, idle_timer_time; |
3886 | EMACS_TIME difference, timer_difference, idle_timer_difference; | |
3887 | ||
3888 | /* Skip past invalid timers and timers already handled. */ | |
3889 | if (!NILP (timers)) | |
c04cbc3b | 3890 | { |
7539e11f | 3891 | timer = XCAR (timers); |
9291c072 RS |
3892 | if (!VECTORP (timer) || XVECTOR (timer)->size != 8) |
3893 | { | |
7539e11f | 3894 | timers = XCDR (timers); |
9291c072 RS |
3895 | continue; |
3896 | } | |
3897 | vector = XVECTOR (timer)->contents; | |
d9d4c147 | 3898 | |
9291c072 RS |
3899 | if (!INTEGERP (vector[1]) || !INTEGERP (vector[2]) |
3900 | || !INTEGERP (vector[3]) | |
3901 | || ! NILP (vector[0])) | |
3902 | { | |
7539e11f | 3903 | timers = XCDR (timers); |
9291c072 RS |
3904 | continue; |
3905 | } | |
3906 | } | |
3907 | if (!NILP (idle_timers)) | |
3908 | { | |
7539e11f | 3909 | timer = XCAR (idle_timers); |
d9d4c147 | 3910 | if (!VECTORP (timer) || XVECTOR (timer)->size != 8) |
9291c072 | 3911 | { |
7539e11f | 3912 | idle_timers = XCDR (idle_timers); |
9291c072 RS |
3913 | continue; |
3914 | } | |
d9d4c147 KH |
3915 | vector = XVECTOR (timer)->contents; |
3916 | ||
3917 | if (!INTEGERP (vector[1]) || !INTEGERP (vector[2]) | |
9291c072 RS |
3918 | || !INTEGERP (vector[3]) |
3919 | || ! NILP (vector[0])) | |
3920 | { | |
7539e11f | 3921 | idle_timers = XCDR (idle_timers); |
9291c072 RS |
3922 | continue; |
3923 | } | |
3924 | } | |
d9d4c147 | 3925 | |
9291c072 RS |
3926 | /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE |
3927 | based on the next ordinary timer. | |
3928 | TIMER_DIFFERENCE is the distance in time from NOW to when | |
3929 | this timer becomes ripe (negative if it's already ripe). */ | |
3930 | if (!NILP (timers)) | |
3931 | { | |
7539e11f | 3932 | timer = XCAR (timers); |
9291c072 | 3933 | vector = XVECTOR (timer)->contents; |
d9d4c147 KH |
3934 | EMACS_SET_SECS (timer_time, |
3935 | (XINT (vector[1]) << 16) | (XINT (vector[2]))); | |
3936 | EMACS_SET_USECS (timer_time, XINT (vector[3])); | |
9291c072 RS |
3937 | EMACS_SUB_TIME (timer_difference, timer_time, now); |
3938 | } | |
ba8dfba8 | 3939 | |
9291c072 RS |
3940 | /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE |
3941 | based on the next idle timer. */ | |
3942 | if (!NILP (idle_timers)) | |
3943 | { | |
7539e11f | 3944 | idle_timer = XCAR (idle_timers); |
9291c072 RS |
3945 | vector = XVECTOR (idle_timer)->contents; |
3946 | EMACS_SET_SECS (idle_timer_time, | |
3947 | (XINT (vector[1]) << 16) | (XINT (vector[2]))); | |
3948 | EMACS_SET_USECS (idle_timer_time, XINT (vector[3])); | |
3949 | EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now); | |
3950 | } | |
ba8dfba8 | 3951 | |
9291c072 RS |
3952 | /* Decide which timer is the next timer, |
3953 | and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly. | |
3954 | Also step down the list where we found that timer. */ | |
d9d4c147 | 3955 | |
9291c072 RS |
3956 | if (! NILP (timers) && ! NILP (idle_timers)) |
3957 | { | |
3958 | EMACS_TIME temp; | |
3959 | EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference); | |
3960 | if (EMACS_TIME_NEG_P (temp)) | |
3961 | { | |
3962 | chosen_timer = timer; | |
7539e11f | 3963 | timers = XCDR (timers); |
9291c072 | 3964 | difference = timer_difference; |
c04cbc3b | 3965 | } |
d9d4c147 | 3966 | else |
d9d4c147 | 3967 | { |
9291c072 | 3968 | chosen_timer = idle_timer; |
7539e11f | 3969 | idle_timers = XCDR (idle_timers); |
9291c072 | 3970 | difference = idle_timer_difference; |
d9d4c147 | 3971 | } |
7ea13e12 | 3972 | } |
9291c072 RS |
3973 | else if (! NILP (timers)) |
3974 | { | |
3975 | chosen_timer = timer; | |
7539e11f | 3976 | timers = XCDR (timers); |
9291c072 RS |
3977 | difference = timer_difference; |
3978 | } | |
3979 | else | |
3980 | { | |
3981 | chosen_timer = idle_timer; | |
7539e11f | 3982 | idle_timers = XCDR (idle_timers); |
9291c072 RS |
3983 | difference = idle_timer_difference; |
3984 | } | |
3985 | vector = XVECTOR (chosen_timer)->contents; | |
3986 | ||
bd55b860 | 3987 | /* If timer is ripe, run it if it hasn't been run. */ |
9291c072 RS |
3988 | if (EMACS_TIME_NEG_P (difference) |
3989 | || (EMACS_SECS (difference) == 0 | |
3990 | && EMACS_USECS (difference) == 0)) | |
3991 | { | |
3992 | if (NILP (vector[0])) | |
3993 | { | |
d925fb39 RS |
3994 | int was_locked = single_kboard; |
3995 | int count = specpdl_ptr - specpdl; | |
3996 | ||
9291c072 RS |
3997 | /* Mark the timer as triggered to prevent problems if the lisp |
3998 | code fails to reschedule it right. */ | |
3999 | vector[0] = Qt; | |
4000 | ||
d925fb39 | 4001 | specbind (Qinhibit_quit, Qt); |
9291c072 | 4002 | |
d925fb39 RS |
4003 | call1 (Qtimer_event_handler, chosen_timer); |
4004 | timers_run++; | |
9291c072 | 4005 | |
d925fb39 | 4006 | unbind_to (count, Qnil); |
4ec4ed6a | 4007 | |
d925fb39 RS |
4008 | /* Resume allowing input from any kboard, if that was true before. */ |
4009 | if (!was_locked) | |
4010 | any_kboard_state (); | |
9291c072 | 4011 | |
d925fb39 RS |
4012 | /* Since we have handled the event, |
4013 | we don't need to tell the caller to wake up and do it. */ | |
9291c072 RS |
4014 | } |
4015 | } | |
4016 | else | |
4017 | /* When we encounter a timer that is still waiting, | |
4018 | return the amount of time to wait before it is ripe. */ | |
4019 | { | |
4020 | UNGCPRO; | |
9291c072 RS |
4021 | return difference; |
4022 | } | |
c04cbc3b | 4023 | } |
9291c072 | 4024 | |
7ea13e12 RS |
4025 | /* No timers are pending in the future. */ |
4026 | /* Return 0 if we generated an event, and -1 if not. */ | |
4027 | UNGCPRO; | |
c04cbc3b RS |
4028 | return nexttime; |
4029 | } | |
4030 | \f | |
284f4730 | 4031 | /* Caches for modify_event_symbol. */ |
e9bf89a0 | 4032 | static Lisp_Object accent_key_syms; |
284f4730 JB |
4033 | static Lisp_Object func_key_syms; |
4034 | static Lisp_Object mouse_syms; | |
07de30b9 GV |
4035 | #ifdef WINDOWSNT |
4036 | static Lisp_Object mouse_wheel_syms; | |
4037 | #endif | |
a24dc617 | 4038 | static Lisp_Object drag_n_drop_syms; |
284f4730 | 4039 | |
e9bf89a0 RS |
4040 | /* This is a list of keysym codes for special "accent" characters. |
4041 | It parallels lispy_accent_keys. */ | |
4042 | ||
4043 | static int lispy_accent_codes[] = | |
4044 | { | |
79a7046c | 4045 | #ifdef XK_dead_circumflex |
e9bf89a0 | 4046 | XK_dead_circumflex, |
79a7046c RS |
4047 | #else |
4048 | 0, | |
4049 | #endif | |
4050 | #ifdef XK_dead_grave | |
e9bf89a0 | 4051 | XK_dead_grave, |
79a7046c RS |
4052 | #else |
4053 | 0, | |
4054 | #endif | |
4055 | #ifdef XK_dead_tilde | |
e9bf89a0 | 4056 | XK_dead_tilde, |
79a7046c RS |
4057 | #else |
4058 | 0, | |
4059 | #endif | |
4060 | #ifdef XK_dead_diaeresis | |
e9bf89a0 | 4061 | XK_dead_diaeresis, |
79a7046c RS |
4062 | #else |
4063 | 0, | |
4064 | #endif | |
4065 | #ifdef XK_dead_macron | |
e9bf89a0 | 4066 | XK_dead_macron, |
79a7046c RS |
4067 | #else |
4068 | 0, | |
4069 | #endif | |
4070 | #ifdef XK_dead_degree | |
e9bf89a0 | 4071 | XK_dead_degree, |
79a7046c RS |
4072 | #else |
4073 | 0, | |
4074 | #endif | |
4075 | #ifdef XK_dead_acute | |
e9bf89a0 | 4076 | XK_dead_acute, |
79a7046c RS |
4077 | #else |
4078 | 0, | |
4079 | #endif | |
4080 | #ifdef XK_dead_cedilla | |
e9bf89a0 | 4081 | XK_dead_cedilla, |
79a7046c RS |
4082 | #else |
4083 | 0, | |
4084 | #endif | |
4085 | #ifdef XK_dead_breve | |
e9bf89a0 | 4086 | XK_dead_breve, |
79a7046c RS |
4087 | #else |
4088 | 0, | |
4089 | #endif | |
4090 | #ifdef XK_dead_ogonek | |
e9bf89a0 | 4091 | XK_dead_ogonek, |
79a7046c RS |
4092 | #else |
4093 | 0, | |
4094 | #endif | |
4095 | #ifdef XK_dead_caron | |
e9bf89a0 | 4096 | XK_dead_caron, |
79a7046c RS |
4097 | #else |
4098 | 0, | |
4099 | #endif | |
4100 | #ifdef XK_dead_doubleacute | |
e9bf89a0 | 4101 | XK_dead_doubleacute, |
79a7046c RS |
4102 | #else |
4103 | 0, | |
4104 | #endif | |
4105 | #ifdef XK_dead_abovedot | |
e9bf89a0 | 4106 | XK_dead_abovedot, |
79a7046c RS |
4107 | #else |
4108 | 0, | |
4109 | #endif | |
e9bf89a0 RS |
4110 | }; |
4111 | ||
4112 | /* This is a list of Lisp names for special "accent" characters. | |
4113 | It parallels lispy_accent_codes. */ | |
4114 | ||
4115 | static char *lispy_accent_keys[] = | |
4116 | { | |
4117 | "dead-circumflex", | |
4118 | "dead-grave", | |
4119 | "dead-tilde", | |
4120 | "dead-diaeresis", | |
4121 | "dead-macron", | |
4122 | "dead-degree", | |
4123 | "dead-acute", | |
4124 | "dead-cedilla", | |
4125 | "dead-breve", | |
4126 | "dead-ogonek", | |
4127 | "dead-caron", | |
4128 | "dead-doubleacute", | |
4129 | "dead-abovedot", | |
4130 | }; | |
4131 | ||
e98a93eb GV |
4132 | #ifdef HAVE_NTGUI |
4133 | #define FUNCTION_KEY_OFFSET 0x0 | |
4134 | ||
4135 | char *lispy_function_keys[] = | |
4136 | { | |
4137 | 0, /* 0 */ | |
4138 | ||
4139 | 0, /* VK_LBUTTON 0x01 */ | |
4140 | 0, /* VK_RBUTTON 0x02 */ | |
4141 | "cancel", /* VK_CANCEL 0x03 */ | |
4142 | 0, /* VK_MBUTTON 0x04 */ | |
4143 | ||
4144 | 0, 0, 0, /* 0x05 .. 0x07 */ | |
4145 | ||
4146 | "backspace", /* VK_BACK 0x08 */ | |
4147 | "tab", /* VK_TAB 0x09 */ | |
4148 | ||
4149 | 0, 0, /* 0x0A .. 0x0B */ | |
4150 | ||
4151 | "clear", /* VK_CLEAR 0x0C */ | |
4152 | "return", /* VK_RETURN 0x0D */ | |
4153 | ||
4154 | 0, 0, /* 0x0E .. 0x0F */ | |
4155 | ||
1161d367 GV |
4156 | 0, /* VK_SHIFT 0x10 */ |
4157 | 0, /* VK_CONTROL 0x11 */ | |
4158 | 0, /* VK_MENU 0x12 */ | |
e98a93eb | 4159 | "pause", /* VK_PAUSE 0x13 */ |
1161d367 | 4160 | "capslock", /* VK_CAPITAL 0x14 */ |
e98a93eb GV |
4161 | |
4162 | 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */ | |
4163 | ||
1161d367 | 4164 | "escape", /* VK_ESCAPE 0x1B */ |
e98a93eb GV |
4165 | |
4166 | 0, 0, 0, 0, /* 0x1C .. 0x1F */ | |
4167 | ||
4168 | 0, /* VK_SPACE 0x20 */ | |
4169 | "prior", /* VK_PRIOR 0x21 */ | |
4170 | "next", /* VK_NEXT 0x22 */ | |
4171 | "end", /* VK_END 0x23 */ | |
4172 | "home", /* VK_HOME 0x24 */ | |
4173 | "left", /* VK_LEFT 0x25 */ | |
4174 | "up", /* VK_UP 0x26 */ | |
4175 | "right", /* VK_RIGHT 0x27 */ | |
4176 | "down", /* VK_DOWN 0x28 */ | |
4177 | "select", /* VK_SELECT 0x29 */ | |
4178 | "print", /* VK_PRINT 0x2A */ | |
4179 | "execute", /* VK_EXECUTE 0x2B */ | |
4180 | "snapshot", /* VK_SNAPSHOT 0x2C */ | |
4181 | "insert", /* VK_INSERT 0x2D */ | |
4182 | "delete", /* VK_DELETE 0x2E */ | |
4183 | "help", /* VK_HELP 0x2F */ | |
4184 | ||
4185 | /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */ | |
4186 | ||
4187 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
4188 | ||
4189 | 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */ | |
4190 | ||
4191 | /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */ | |
4192 | ||
4193 | 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
4194 | 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
4195 | 0, 0, 0, 0, 0, 0, 0, 0, | |
4196 | ||
e376f90d RS |
4197 | "lwindow", /* VK_LWIN 0x5B */ |
4198 | "rwindow", /* VK_RWIN 0x5C */ | |
4199 | "apps", /* VK_APPS 0x5D */ | |
e98a93eb GV |
4200 | |
4201 | 0, 0, /* 0x5E .. 0x5F */ | |
4202 | ||
4203 | "kp-0", /* VK_NUMPAD0 0x60 */ | |
4204 | "kp-1", /* VK_NUMPAD1 0x61 */ | |
4205 | "kp-2", /* VK_NUMPAD2 0x62 */ | |
4206 | "kp-3", /* VK_NUMPAD3 0x63 */ | |
4207 | "kp-4", /* VK_NUMPAD4 0x64 */ | |
4208 | "kp-5", /* VK_NUMPAD5 0x65 */ | |
4209 | "kp-6", /* VK_NUMPAD6 0x66 */ | |
4210 | "kp-7", /* VK_NUMPAD7 0x67 */ | |
4211 | "kp-8", /* VK_NUMPAD8 0x68 */ | |
4212 | "kp-9", /* VK_NUMPAD9 0x69 */ | |
4213 | "kp-multiply", /* VK_MULTIPLY 0x6A */ | |
4214 | "kp-add", /* VK_ADD 0x6B */ | |
4215 | "kp-separator", /* VK_SEPARATOR 0x6C */ | |
4216 | "kp-subtract", /* VK_SUBTRACT 0x6D */ | |
4217 | "kp-decimal", /* VK_DECIMAL 0x6E */ | |
4218 | "kp-divide", /* VK_DIVIDE 0x6F */ | |
4219 | "f1", /* VK_F1 0x70 */ | |
4220 | "f2", /* VK_F2 0x71 */ | |
4221 | "f3", /* VK_F3 0x72 */ | |
4222 | "f4", /* VK_F4 0x73 */ | |
4223 | "f5", /* VK_F5 0x74 */ | |
4224 | "f6", /* VK_F6 0x75 */ | |
4225 | "f7", /* VK_F7 0x76 */ | |
4226 | "f8", /* VK_F8 0x77 */ | |
4227 | "f9", /* VK_F9 0x78 */ | |
4228 | "f10", /* VK_F10 0x79 */ | |
4229 | "f11", /* VK_F11 0x7A */ | |
4230 | "f12", /* VK_F12 0x7B */ | |
4231 | "f13", /* VK_F13 0x7C */ | |
4232 | "f14", /* VK_F14 0x7D */ | |
4233 | "f15", /* VK_F15 0x7E */ | |
4234 | "f16", /* VK_F16 0x7F */ | |
4235 | "f17", /* VK_F17 0x80 */ | |
4236 | "f18", /* VK_F18 0x81 */ | |
4237 | "f19", /* VK_F19 0x82 */ | |
4238 | "f20", /* VK_F20 0x83 */ | |
4239 | "f21", /* VK_F21 0x84 */ | |
4240 | "f22", /* VK_F22 0x85 */ | |
4241 | "f23", /* VK_F23 0x86 */ | |
4242 | "f24", /* VK_F24 0x87 */ | |
4243 | ||
4244 | 0, 0, 0, 0, /* 0x88 .. 0x8B */ | |
4245 | 0, 0, 0, 0, /* 0x8C .. 0x8F */ | |
4246 | ||
4247 | "kp-numlock", /* VK_NUMLOCK 0x90 */ | |
4248 | "scroll", /* VK_SCROLL 0x91 */ | |
4249 | ||
e376f90d RS |
4250 | "kp-space", /* VK_NUMPAD_CLEAR 0x92 */ |
4251 | "kp-enter", /* VK_NUMPAD_ENTER 0x93 */ | |
4252 | "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */ | |
4253 | "kp-next", /* VK_NUMPAD_NEXT 0x95 */ | |
4254 | "kp-end", /* VK_NUMPAD_END 0x96 */ | |
4255 | "kp-home", /* VK_NUMPAD_HOME 0x97 */ | |
4256 | "kp-left", /* VK_NUMPAD_LEFT 0x98 */ | |
4257 | "kp-up", /* VK_NUMPAD_UP 0x99 */ | |
4258 | "kp-right", /* VK_NUMPAD_RIGHT 0x9A */ | |
4259 | "kp-down", /* VK_NUMPAD_DOWN 0x9B */ | |
4260 | "kp-insert", /* VK_NUMPAD_INSERT 0x9C */ | |
4261 | "kp-delete", /* VK_NUMPAD_DELETE 0x9D */ | |
4262 | ||
4263 | 0, 0, /* 0x9E .. 0x9F */ | |
4264 | ||
e98a93eb GV |
4265 | /* |
4266 | * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys. | |
e8886a1d | 4267 | * Used only as parameters to GetAsyncKeyState and GetKeyState. |
e98a93eb GV |
4268 | * No other API or message will distinguish left and right keys this way. |
4269 | */ | |
4270 | /* 0xA0 .. 0xEF */ | |
4271 | ||
4272 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
4273 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
4274 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
4275 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
4276 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
4277 | ||
4278 | /* 0xF0 .. 0xF5 */ | |
4279 | ||
4280 | 0, 0, 0, 0, 0, 0, | |
4281 | ||
4282 | "attn", /* VK_ATTN 0xF6 */ | |
4283 | "crsel", /* VK_CRSEL 0xF7 */ | |
4284 | "exsel", /* VK_EXSEL 0xF8 */ | |
4285 | "ereof", /* VK_EREOF 0xF9 */ | |
4286 | "play", /* VK_PLAY 0xFA */ | |
4287 | "zoom", /* VK_ZOOM 0xFB */ | |
4288 | "noname", /* VK_NONAME 0xFC */ | |
4289 | "pa1", /* VK_PA1 0xFD */ | |
4290 | "oem_clear", /* VK_OEM_CLEAR 0xFE */ | |
1161d367 | 4291 | 0 /* 0xFF */ |
e98a93eb GV |
4292 | }; |
4293 | ||
04f215f0 | 4294 | #else /* not HAVE_NTGUI */ |
e98a93eb | 4295 | |
37cd9f30 KH |
4296 | #ifdef XK_kana_A |
4297 | static char *lispy_kana_keys[] = | |
4298 | { | |
4299 | /* X Keysym value */ | |
4300 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */ | |
4301 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */ | |
4302 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */ | |
4303 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */ | |
4304 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */ | |
4305 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */ | |
4306 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */ | |
4307 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0, | |
4308 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */ | |
4309 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */ | |
4310 | 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket", | |
4311 | "kana-comma", "kana-conjunctive", "kana-WO", "kana-a", | |
4312 | "kana-i", "kana-u", "kana-e", "kana-o", | |
4313 | "kana-ya", "kana-yu", "kana-yo", "kana-tsu", | |
4314 | "prolongedsound", "kana-A", "kana-I", "kana-U", | |
4315 | "kana-E", "kana-O", "kana-KA", "kana-KI", | |
4316 | "kana-KU", "kana-KE", "kana-KO", "kana-SA", | |
4317 | "kana-SHI", "kana-SU", "kana-SE", "kana-SO", | |
4318 | "kana-TA", "kana-CHI", "kana-TSU", "kana-TE", | |
4319 | "kana-TO", "kana-NA", "kana-NI", "kana-NU", | |
4320 | "kana-NE", "kana-NO", "kana-HA", "kana-HI", | |
4321 | "kana-FU", "kana-HE", "kana-HO", "kana-MA", | |
4322 | "kana-MI", "kana-MU", "kana-ME", "kana-MO", | |
4323 | "kana-YA", "kana-YU", "kana-YO", "kana-RA", | |
4324 | "kana-RI", "kana-RU", "kana-RE", "kana-RO", | |
4325 | "kana-WA", "kana-N", "voicedsound", "semivoicedsound", | |
4326 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */ | |
4327 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */ | |
4328 | }; | |
4329 | #endif /* XK_kana_A */ | |
4330 | ||
04f215f0 RS |
4331 | #define FUNCTION_KEY_OFFSET 0xff00 |
4332 | ||
284f4730 JB |
4333 | /* You'll notice that this table is arranged to be conveniently |
4334 | indexed by X Windows keysym values. */ | |
4335 | static char *lispy_function_keys[] = | |
4336 | { | |
4337 | /* X Keysym value */ | |
4338 | ||
75045dcb RS |
4339 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */ |
4340 | "backspace", "tab", "linefeed", "clear", | |
4341 | 0, "return", 0, 0, | |
4342 | 0, 0, 0, "pause", /* 0xff10...1f */ | |
4343 | 0, 0, 0, 0, 0, 0, 0, "escape", | |
86e5706b | 4344 | 0, 0, 0, 0, |
75045dcb RS |
4345 | 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */ |
4346 | "romaji", "hiragana", "katakana", "hiragana-katakana", | |
4347 | "zenkaku", "hankaku", "zenkaku-hankaku", "touroku", | |
4348 | "massyo", "kana-lock", "kana-shift", "eisu-shift", | |
4349 | "eisu-toggle", /* 0xff30...3f */ | |
4350 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
86e5706b RS |
4351 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */ |
4352 | ||
75045dcb RS |
4353 | "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */ |
4354 | "down", "prior", "next", "end", | |
4355 | "begin", 0, 0, 0, 0, 0, 0, 0, | |
284f4730 JB |
4356 | "select", /* 0xff60 */ /* IsMiscFunctionKey */ |
4357 | "print", | |
4358 | "execute", | |
4359 | "insert", | |
4360 | 0, /* 0xff64 */ | |
4361 | "undo", | |
4362 | "redo", | |
4363 | "menu", | |
4364 | "find", | |
4365 | "cancel", | |
4366 | "help", | |
4367 | "break", /* 0xff6b */ | |
4368 | ||
75045dcb RS |
4369 | 0, 0, 0, 0, |
4370 | 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */ | |
4371 | 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */ | |
284f4730 JB |
4372 | "kp-space", /* 0xff80 */ /* IsKeypadKey */ |
4373 | 0, 0, 0, 0, 0, 0, 0, 0, | |
4374 | "kp-tab", /* 0xff89 */ | |
4375 | 0, 0, 0, | |
4376 | "kp-enter", /* 0xff8d */ | |
4377 | 0, 0, 0, | |
4378 | "kp-f1", /* 0xff91 */ | |
4379 | "kp-f2", | |
4380 | "kp-f3", | |
4381 | "kp-f4", | |
872157e7 RS |
4382 | "kp-home", /* 0xff95 */ |
4383 | "kp-left", | |
4384 | "kp-up", | |
4385 | "kp-right", | |
4386 | "kp-down", | |
4387 | "kp-prior", /* kp-page-up */ | |
4388 | "kp-next", /* kp-page-down */ | |
4389 | "kp-end", | |
4390 | "kp-begin", | |
4391 | "kp-insert", | |
4392 | "kp-delete", | |
4393 | 0, /* 0xffa0 */ | |
4394 | 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
284f4730 JB |
4395 | "kp-multiply", /* 0xffaa */ |
4396 | "kp-add", | |
4397 | "kp-separator", | |
4398 | "kp-subtract", | |
4399 | "kp-decimal", | |
4400 | "kp-divide", /* 0xffaf */ | |
4401 | "kp-0", /* 0xffb0 */ | |
4402 | "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9", | |
4403 | 0, /* 0xffba */ | |
4404 | 0, 0, | |
4405 | "kp-equal", /* 0xffbd */ | |
4406 | "f1", /* 0xffbe */ /* IsFunctionKey */ | |
86e5706b RS |
4407 | "f2", |
4408 | "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */ | |
4409 | "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", | |
4410 | "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */ | |
4411 | "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34", | |
4412 | "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */ | |
4413 | 0, 0, 0, 0, 0, 0, 0, 0, | |
4414 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */ | |
4415 | 0, 0, 0, 0, 0, 0, 0, "delete" | |
04f215f0 | 4416 | }; |
284f4730 | 4417 | |
04f215f0 RS |
4418 | /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */ |
4419 | #define ISO_FUNCTION_KEY_OFFSET 0xfe00 | |
4420 | ||
4421 | static char *iso_lispy_function_keys[] = | |
4422 | { | |
4423 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */ | |
4424 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */ | |
4425 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */ | |
4426 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */ | |
4427 | "iso-lefttab", /* 0xfe20 */ | |
4428 | "iso-move-line-up", "iso-move-line-down", | |
4429 | "iso-partial-line-up", "iso-partial-line-down", | |
4430 | "iso-partial-space-left", "iso-partial-space-right", | |
4431 | "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */ | |
4432 | "iso-release-margin-left", "iso-release-margin-right", | |
4433 | "iso-release-both-margins", | |
4434 | "iso-fast-cursor-left", "iso-fast-cursor-right", | |
4435 | "iso-fast-cursor-up", "iso-fast-cursor-down", | |
4436 | "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */ | |
4437 | "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */ | |
4438 | }; | |
4439 | ||
4440 | #endif /* not HAVE_NTGUI */ | |
e98a93eb | 4441 | |
8e1e4240 | 4442 | Lisp_Object Vlispy_mouse_stem; |
284f4730 | 4443 | |
07de30b9 GV |
4444 | #ifdef WINDOWSNT |
4445 | /* mouse-wheel events are generated by the wheel on devices such as | |
4446 | the MS Intellimouse. The wheel sits in between the left and right | |
4447 | mouse buttons, and is typically used to scroll or zoom the window | |
4448 | underneath the pointer. mouse-wheel events specify the object on | |
4449 | which they operate, and a delta corresponding to the amount and | |
4450 | direction that the wheel is rotated. Clicking the mouse-wheel | |
4451 | generates a mouse-2 event. */ | |
4452 | static char *lispy_mouse_wheel_names[] = | |
4453 | { | |
4454 | "mouse-wheel" | |
4455 | }; | |
a24dc617 | 4456 | |
07de30b9 GV |
4457 | #endif /* WINDOWSNT */ |
4458 | ||
a24dc617 RS |
4459 | /* drag-n-drop events are generated when a set of selected files are |
4460 | dragged from another application and dropped onto an Emacs window. */ | |
4461 | static char *lispy_drag_n_drop_names[] = | |
4462 | { | |
4463 | "drag-n-drop" | |
4464 | }; | |
4465 | ||
3c370943 | 4466 | /* Scroll bar parts. */ |
4bb994d1 | 4467 | Lisp_Object Qabove_handle, Qhandle, Qbelow_handle; |
7ee32cda | 4468 | Lisp_Object Qup, Qdown, Qbottom, Qend_scroll; |
eef28553 | 4469 | Lisp_Object Qtop, Qratio; |
4bb994d1 | 4470 | |
3c370943 JB |
4471 | /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */ |
4472 | Lisp_Object *scroll_bar_parts[] = { | |
db08707d | 4473 | &Qabove_handle, &Qhandle, &Qbelow_handle, |
eef28553 | 4474 | &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio |
4bb994d1 JB |
4475 | }; |
4476 | ||
5bf68f6e AS |
4477 | /* User signal events. */ |
4478 | Lisp_Object Qusr1_signal, Qusr2_signal; | |
4479 | ||
4480 | Lisp_Object *lispy_user_signals[] = | |
4481 | { | |
4482 | &Qusr1_signal, &Qusr2_signal | |
4483 | }; | |
4484 | ||
4bb994d1 | 4485 | |
7b4aedb9 | 4486 | /* A vector, indexed by button number, giving the down-going location |
3c370943 | 4487 | of currently depressed buttons, both scroll bar and non-scroll bar. |
7b4aedb9 JB |
4488 | |
4489 | The elements have the form | |
4490 | (BUTTON-NUMBER MODIFIER-MASK . REST) | |
4491 | where REST is the cdr of a position as it would be reported in the event. | |
4492 | ||
4493 | The make_lispy_event function stores positions here to tell the | |
4494 | difference between click and drag events, and to store the starting | |
4495 | location to be included in drag events. */ | |
4496 | ||
4497 | static Lisp_Object button_down_location; | |
88cb0656 | 4498 | |
fbcd35bd JB |
4499 | /* Information about the most recent up-going button event: Which |
4500 | button, what location, and what time. */ | |
4501 | ||
559f9d04 RS |
4502 | static int last_mouse_button; |
4503 | static int last_mouse_x; | |
4504 | static int last_mouse_y; | |
4505 | static unsigned long button_down_time; | |
fbcd35bd | 4506 | |
564dc952 JB |
4507 | /* The maximum time between clicks to make a double-click, |
4508 | or Qnil to disable double-click detection, | |
4509 | or Qt for no time limit. */ | |
4510 | Lisp_Object Vdouble_click_time; | |
fbcd35bd JB |
4511 | |
4512 | /* The number of clicks in this multiple-click. */ | |
4513 | ||
4514 | int double_click_count; | |
4515 | ||
284f4730 JB |
4516 | /* Given a struct input_event, build the lisp event which represents |
4517 | it. If EVENT is 0, build a mouse movement event from the mouse | |
88cb0656 JB |
4518 | movement buffer, which should have a movement event in it. |
4519 | ||
4520 | Note that events must be passed to this function in the order they | |
4521 | are received; this function stores the location of button presses | |
4522 | in order to build drag events when the button is released. */ | |
284f4730 JB |
4523 | |
4524 | static Lisp_Object | |
4525 | make_lispy_event (event) | |
4526 | struct input_event *event; | |
4527 | { | |
79a7046c RS |
4528 | int i; |
4529 | ||
0220c518 | 4530 | switch (SWITCH_ENUM_CAST (event->kind)) |
284f4730 | 4531 | { |
284f4730 JB |
4532 | /* A simple keystroke. */ |
4533 | case ascii_keystroke: | |
86e5706b | 4534 | { |
9343ab07 | 4535 | Lisp_Object lispy_c; |
e9bf89a0 | 4536 | int c = event->code & 0377; |
5a1c6df8 JB |
4537 | /* Turn ASCII characters into control characters |
4538 | when proper. */ | |
4539 | if (event->modifiers & ctrl_modifier) | |
d205953b JB |
4540 | c = make_ctrl_char (c); |
4541 | ||
4542 | /* Add in the other modifier bits. We took care of ctrl_modifier | |
4543 | just above, and the shift key was taken care of by the X code, | |
4544 | and applied to control characters by make_ctrl_char. */ | |
86e5706b RS |
4545 | c |= (event->modifiers |
4546 | & (meta_modifier | alt_modifier | |
4547 | | hyper_modifier | super_modifier)); | |
32454a9f RS |
4548 | /* Distinguish Shift-SPC from SPC. */ |
4549 | if ((event->code & 0377) == 040 | |
4550 | && event->modifiers & shift_modifier) | |
4551 | c |= shift_modifier; | |
559f9d04 | 4552 | button_down_time = 0; |
bb9e9bed | 4553 | XSETFASTINT (lispy_c, c); |
9343ab07 | 4554 | return lispy_c; |
86e5706b | 4555 | } |
284f4730 | 4556 | |
a50e723f KH |
4557 | case multibyte_char_keystroke: |
4558 | { | |
4559 | Lisp_Object lispy_c; | |
4560 | ||
4561 | XSETFASTINT (lispy_c, event->code); | |
4562 | return lispy_c; | |
4563 | } | |
4564 | ||
284f4730 JB |
4565 | /* A function key. The symbol may need to have modifier prefixes |
4566 | tacked onto it. */ | |
4567 | case non_ascii_keystroke: | |
559f9d04 | 4568 | button_down_time = 0; |
e9bf89a0 RS |
4569 | |
4570 | for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++) | |
4571 | if (event->code == lispy_accent_codes[i]) | |
4572 | return modify_event_symbol (i, | |
4573 | event->modifiers, | |
80e4aa30 | 4574 | Qfunction_key, Qnil, |
e9bf89a0 RS |
4575 | lispy_accent_keys, &accent_key_syms, |
4576 | (sizeof (lispy_accent_keys) | |
4577 | / sizeof (lispy_accent_keys[0]))); | |
4578 | ||
270a208f | 4579 | /* Handle system-specific keysyms. */ |
80e4aa30 RS |
4580 | if (event->code & (1 << 28)) |
4581 | { | |
4582 | /* We need to use an alist rather than a vector as the cache | |
4583 | since we can't make a vector long enuf. */ | |
142e6c73 KH |
4584 | if (NILP (current_kboard->system_key_syms)) |
4585 | current_kboard->system_key_syms = Fcons (Qnil, Qnil); | |
2c834fb3 | 4586 | return modify_event_symbol (event->code, |
80e4aa30 | 4587 | event->modifiers, |
7c97ffdc KH |
4588 | Qfunction_key, |
4589 | current_kboard->Vsystem_key_alist, | |
142e6c73 KH |
4590 | 0, ¤t_kboard->system_key_syms, |
4591 | (unsigned)-1); | |
80e4aa30 RS |
4592 | } |
4593 | ||
37cd9f30 KH |
4594 | #ifdef XK_kana_A |
4595 | if (event->code >= 0x400 && event->code < 0x500) | |
4596 | return modify_event_symbol (event->code - 0x400, | |
4597 | event->modifiers & ~shift_modifier, | |
4598 | Qfunction_key, Qnil, | |
4599 | lispy_kana_keys, &func_key_syms, | |
4600 | (sizeof (lispy_kana_keys) | |
4601 | / sizeof (lispy_kana_keys[0]))); | |
4602 | #endif /* XK_kana_A */ | |
4603 | ||
111c4138 | 4604 | #ifdef ISO_FUNCTION_KEY_OFFSET |
04f215f0 RS |
4605 | if (event->code < FUNCTION_KEY_OFFSET |
4606 | && event->code >= ISO_FUNCTION_KEY_OFFSET) | |
4607 | return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET, | |
4608 | event->modifiers, | |
4609 | Qfunction_key, Qnil, | |
4610 | iso_lispy_function_keys, &func_key_syms, | |
4611 | (sizeof (iso_lispy_function_keys) | |
4612 | / sizeof (iso_lispy_function_keys[0]))); | |
4613 | else | |
111c4138 | 4614 | #endif |
04f215f0 RS |
4615 | return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET, |
4616 | event->modifiers, | |
4617 | Qfunction_key, Qnil, | |
4618 | lispy_function_keys, &func_key_syms, | |
4619 | (sizeof (lispy_function_keys) | |
4620 | / sizeof (lispy_function_keys[0]))); | |
284f4730 | 4621 | |
514354e9 | 4622 | #ifdef HAVE_MOUSE |
df0f2ba1 | 4623 | /* A mouse click. Figure out where it is, decide whether it's |
88cb0656 | 4624 | a press, click or drag, and build the appropriate structure. */ |
284f4730 | 4625 | case mouse_click: |
7ee32cda | 4626 | #ifndef USE_TOOLKIT_SCROLL_BARS |
3c370943 | 4627 | case scroll_bar_click: |
7ee32cda | 4628 | #endif |
284f4730 | 4629 | { |
e9bf89a0 | 4630 | int button = event->code; |
559f9d04 | 4631 | int is_double; |
7b4aedb9 | 4632 | Lisp_Object position; |
dbc4e1c1 JB |
4633 | Lisp_Object *start_pos_ptr; |
4634 | Lisp_Object start_pos; | |
284f4730 | 4635 | |
8c907a56 GM |
4636 | position = Qnil; |
4637 | ||
7b4aedb9 JB |
4638 | /* Build the position as appropriate for this mouse click. */ |
4639 | if (event->kind == mouse_click) | |
284f4730 | 4640 | { |
7b4aedb9 | 4641 | int part; |
598a9fa7 | 4642 | FRAME_PTR f = XFRAME (event->frame_or_window); |
0aafc975 | 4643 | Lisp_Object window; |
7b4aedb9 | 4644 | Lisp_Object posn; |
7ee32cda | 4645 | Lisp_Object string_info = Qnil; |
9e20143a RS |
4646 | int row, column; |
4647 | ||
5da3133a RS |
4648 | /* Ignore mouse events that were made on frame that |
4649 | have been deleted. */ | |
4650 | if (! FRAME_LIVE_P (f)) | |
4651 | return Qnil; | |
4652 | ||
7ee32cda GM |
4653 | /* EVENT->x and EVENT->y are frame-relative pixel |
4654 | coordinates at this place. Under old redisplay, COLUMN | |
4655 | and ROW are set to frame relative glyph coordinates | |
4656 | which are then used to determine whether this click is | |
4657 | in a menu (non-toolkit version). */ | |
4658 | pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y), | |
4659 | &column, &row, NULL, 1); | |
7b4aedb9 | 4660 | |
eef045bf RS |
4661 | #ifndef USE_X_TOOLKIT |
4662 | /* In the non-toolkit version, clicks on the menu bar | |
4663 | are ordinary button events in the event buffer. | |
4664 | Distinguish them, and invoke the menu. | |
4665 | ||
4666 | (In the toolkit version, the toolkit handles the menu bar | |
4667 | and Emacs doesn't know about it until after the user | |
4668 | makes a selection.) */ | |
2ee250ec RS |
4669 | if (row >= 0 && row < FRAME_MENU_BAR_LINES (f) |
4670 | && (event->modifiers & down_modifier)) | |
bb936752 | 4671 | { |
b7c49376 | 4672 | Lisp_Object items, item; |
0a0e8fe6 RS |
4673 | int hpos; |
4674 | int i; | |
4675 | ||
2ee250ec | 4676 | #if 0 |
0a0e8fe6 RS |
4677 | /* Activate the menu bar on the down event. If the |
4678 | up event comes in before the menu code can deal with it, | |
4679 | just ignore it. */ | |
4680 | if (! (event->modifiers & down_modifier)) | |
4681 | return Qnil; | |
2ee250ec | 4682 | #endif |
0aafc975 | 4683 | |
7ee32cda | 4684 | /* Find the menu bar item under `column'. */ |
f2ae6b3f | 4685 | item = Qnil; |
5ec75a55 | 4686 | items = FRAME_MENU_BAR_ITEMS (f); |
35b3402f | 4687 | for (i = 0; i < XVECTOR (items)->size; i += 4) |
5ec75a55 RS |
4688 | { |
4689 | Lisp_Object pos, string; | |
b7c49376 | 4690 | string = XVECTOR (items)->contents[i + 1]; |
35b3402f | 4691 | pos = XVECTOR (items)->contents[i + 3]; |
b7c49376 RS |
4692 | if (NILP (string)) |
4693 | break; | |
9e20143a RS |
4694 | if (column >= XINT (pos) |
4695 | && column < XINT (pos) + XSTRING (string)->size) | |
b7c49376 RS |
4696 | { |
4697 | item = XVECTOR (items)->contents[i]; | |
4698 | break; | |
4699 | } | |
5ec75a55 | 4700 | } |
9e20143a | 4701 | |
7ee32cda GM |
4702 | /* ELisp manual 2.4b says (x y) are window relative but |
4703 | code says they are frame-relative. */ | |
5ec75a55 RS |
4704 | position |
4705 | = Fcons (event->frame_or_window, | |
4706 | Fcons (Qmenu_bar, | |
4707 | Fcons (Fcons (event->x, event->y), | |
4708 | Fcons (make_number (event->timestamp), | |
4709 | Qnil)))); | |
4710 | ||
b7c49376 | 4711 | return Fcons (item, Fcons (position, Qnil)); |
5ec75a55 | 4712 | } |
eef045bf | 4713 | #endif /* not USE_X_TOOLKIT */ |
0aafc975 | 4714 | |
7ee32cda GM |
4715 | /* Set `window' to the window under frame pixel coordinates |
4716 | event->x/event->y. */ | |
4717 | window = window_from_coordinates (f, XINT (event->x), | |
4718 | XINT (event->y), &part, 0); | |
0aafc975 | 4719 | |
8c18cbfb | 4720 | if (!WINDOWP (window)) |
78ced549 RS |
4721 | { |
4722 | window = event->frame_or_window; | |
4723 | posn = Qnil; | |
4724 | } | |
284f4730 | 4725 | else |
7b4aedb9 | 4726 | { |
7ee32cda GM |
4727 | /* It's a click in window window at frame coordinates |
4728 | event->x/ event->y. */ | |
4729 | struct window *w = XWINDOW (window); | |
4730 | ||
4731 | /* Get window relative coordinates. Original code | |
4732 | `rounded' this to glyph boundaries. */ | |
4733 | int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x)); | |
4734 | int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y)); | |
4735 | ||
4736 | /* Set event coordinates to window-relative coordinates | |
4737 | for constructing the Lisp event below. */ | |
4738 | XSETINT (event->x, wx); | |
4739 | XSETINT (event->y, wy); | |
4740 | ||
4741 | if (part == 1 || part == 3) | |
4742 | { | |
902ae620 | 4743 | /* Mode line or header line. Look for a string under |
7ee32cda GM |
4744 | the mouse that may have a `local-map' property. */ |
4745 | Lisp_Object string; | |
4746 | int charpos; | |
4747 | ||
045dee35 | 4748 | posn = part == 1 ? Qmode_line : Qheader_line; |
7ee32cda GM |
4749 | string = mode_line_string (w, wx, wy, part == 1, &charpos); |
4750 | if (STRINGP (string)) | |
4751 | string_info = Fcons (string, make_number (charpos)); | |
4752 | } | |
7b4aedb9 JB |
4753 | else if (part == 2) |
4754 | posn = Qvertical_line; | |
4755 | else | |
7ee32cda | 4756 | XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy)); |
7b4aedb9 JB |
4757 | } |
4758 | ||
5ec75a55 RS |
4759 | position |
4760 | = Fcons (window, | |
4761 | Fcons (posn, | |
4762 | Fcons (Fcons (event->x, event->y), | |
4763 | Fcons (make_number (event->timestamp), | |
7ee32cda GM |
4764 | (NILP (string_info) |
4765 | ? Qnil | |
4766 | : Fcons (string_info, Qnil)))))); | |
284f4730 | 4767 | } |
7ee32cda | 4768 | #ifndef USE_TOOLKIT_SCROLL_BARS |
7b4aedb9 | 4769 | else |
88cb0656 | 4770 | { |
7ee32cda | 4771 | /* It's a scrollbar click. */ |
9e20143a RS |
4772 | Lisp_Object window; |
4773 | Lisp_Object portion_whole; | |
4774 | Lisp_Object part; | |
4775 | ||
4776 | window = event->frame_or_window; | |
4777 | portion_whole = Fcons (event->x, event->y); | |
4778 | part = *scroll_bar_parts[(int) event->part]; | |
7b4aedb9 | 4779 | |
db08707d RS |
4780 | position |
4781 | = Fcons (window, | |
4782 | Fcons (Qvertical_scroll_bar, | |
4783 | Fcons (portion_whole, | |
4784 | Fcons (make_number (event->timestamp), | |
4785 | Fcons (part, Qnil))))); | |
88cb0656 | 4786 | } |
7ee32cda | 4787 | #endif /* not USE_TOOLKIT_SCROLL_BARS */ |
88cb0656 | 4788 | |
8e1e4240 GM |
4789 | if (button >= XVECTOR (button_down_location)->size) |
4790 | { | |
4791 | button_down_location = larger_vector (button_down_location, | |
4792 | button + 1, Qnil); | |
4793 | mouse_syms = larger_vector (mouse_syms, button + 1, Qnil); | |
4794 | } | |
4795 | ||
dbc4e1c1 JB |
4796 | start_pos_ptr = &XVECTOR (button_down_location)->contents[button]; |
4797 | ||
4798 | start_pos = *start_pos_ptr; | |
4799 | *start_pos_ptr = Qnil; | |
7b4aedb9 | 4800 | |
559f9d04 RS |
4801 | is_double = (button == last_mouse_button |
4802 | && XINT (event->x) == last_mouse_x | |
4803 | && XINT (event->y) == last_mouse_y | |
4804 | && button_down_time != 0 | |
4805 | && (EQ (Vdouble_click_time, Qt) | |
4806 | || (INTEGERP (Vdouble_click_time) | |
4807 | && ((int)(event->timestamp - button_down_time) | |
4808 | < XINT (Vdouble_click_time))))); | |
4809 | last_mouse_button = button; | |
4810 | last_mouse_x = XINT (event->x); | |
4811 | last_mouse_y = XINT (event->y); | |
4812 | ||
7b4aedb9 JB |
4813 | /* If this is a button press, squirrel away the location, so |
4814 | we can decide later whether it was a click or a drag. */ | |
4815 | if (event->modifiers & down_modifier) | |
559f9d04 RS |
4816 | { |
4817 | if (is_double) | |
4818 | { | |
4819 | double_click_count++; | |
4820 | event->modifiers |= ((double_click_count > 2) | |
4821 | ? triple_modifier | |
4822 | : double_modifier); | |
4823 | } | |
4824 | else | |
4825 | double_click_count = 1; | |
4826 | button_down_time = event->timestamp; | |
4827 | *start_pos_ptr = Fcopy_alist (position); | |
4828 | } | |
7b4aedb9 | 4829 | |
88cb0656 | 4830 | /* Now we're releasing a button - check the co-ordinates to |
7b4aedb9 | 4831 | see if this was a click or a drag. */ |
88cb0656 JB |
4832 | else if (event->modifiers & up_modifier) |
4833 | { | |
48e416d4 RS |
4834 | /* If we did not see a down before this up, |
4835 | ignore the up. Probably this happened because | |
4836 | the down event chose a menu item. | |
4837 | It would be an annoyance to treat the release | |
4838 | of the button that chose the menu item | |
4839 | as a separate event. */ | |
4840 | ||
8c18cbfb | 4841 | if (!CONSP (start_pos)) |
48e416d4 RS |
4842 | return Qnil; |
4843 | ||
88cb0656 | 4844 | event->modifiers &= ~up_modifier; |
48e416d4 | 4845 | #if 0 /* Formerly we treated an up with no down as a click event. */ |
8c18cbfb | 4846 | if (!CONSP (start_pos)) |
dbc4e1c1 JB |
4847 | event->modifiers |= click_modifier; |
4848 | else | |
48e416d4 | 4849 | #endif |
dbc4e1c1 JB |
4850 | { |
4851 | /* The third element of every position should be the (x,y) | |
4852 | pair. */ | |
9b8eb840 | 4853 | Lisp_Object down; |
dbc4e1c1 | 4854 | |
9b8eb840 | 4855 | down = Fnth (make_number (2), start_pos); |
7539e11f KR |
4856 | if (EQ (event->x, XCAR (down)) |
4857 | && EQ (event->y, XCDR (down))) | |
fbcd35bd | 4858 | { |
bc536d84 | 4859 | event->modifiers |= click_modifier; |
fbcd35bd JB |
4860 | } |
4861 | else | |
4862 | { | |
559f9d04 | 4863 | button_down_time = 0; |
fbcd35bd JB |
4864 | event->modifiers |= drag_modifier; |
4865 | } | |
bc536d84 RS |
4866 | /* Don't check is_double; treat this as multiple |
4867 | if the down-event was multiple. */ | |
4868 | if (double_click_count > 1) | |
4869 | event->modifiers |= ((double_click_count > 2) | |
4870 | ? triple_modifier | |
4871 | : double_modifier); | |
dbc4e1c1 | 4872 | } |
88cb0656 JB |
4873 | } |
4874 | else | |
4875 | /* Every mouse event should either have the down_modifier or | |
7b4aedb9 | 4876 | the up_modifier set. */ |
88cb0656 JB |
4877 | abort (); |
4878 | ||
88cb0656 | 4879 | { |
7b4aedb9 | 4880 | /* Get the symbol we should use for the mouse click. */ |
9b8eb840 KH |
4881 | Lisp_Object head; |
4882 | ||
4883 | head = modify_event_symbol (button, | |
4884 | event->modifiers, | |
8e1e4240 GM |
4885 | Qmouse_click, Vlispy_mouse_stem, |
4886 | NULL, | |
4887 | &mouse_syms, | |
4888 | XVECTOR (mouse_syms)->size); | |
88cb0656 | 4889 | if (event->modifiers & drag_modifier) |
dbc4e1c1 JB |
4890 | return Fcons (head, |
4891 | Fcons (start_pos, | |
4892 | Fcons (position, | |
4893 | Qnil))); | |
fbcd35bd JB |
4894 | else if (event->modifiers & (double_modifier | triple_modifier)) |
4895 | return Fcons (head, | |
4896 | Fcons (position, | |
4897 | Fcons (make_number (double_click_count), | |
4898 | Qnil))); | |
88cb0656 JB |
4899 | else |
4900 | return Fcons (head, | |
7b4aedb9 | 4901 | Fcons (position, |
88cb0656 JB |
4902 | Qnil)); |
4903 | } | |
284f4730 | 4904 | } |
db08707d | 4905 | |
7ee32cda GM |
4906 | #if USE_TOOLKIT_SCROLL_BARS |
4907 | ||
4908 | /* We don't have down and up events if using toolkit scroll bars, | |
4909 | so make this always a click event. Store in the `part' of | |
4910 | the Lisp event a symbol which maps to the following actions: | |
4911 | ||
4912 | `above_handle' page up | |
4913 | `below_handle' page down | |
4914 | `up' line up | |
4915 | `down' line down | |
4916 | `top' top of buffer | |
4917 | `bottom' bottom of buffer | |
4918 | `handle' thumb has been dragged. | |
4919 | `end-scroll' end of interaction with scroll bar | |
4920 | ||
4921 | The incoming input_event contains in its `part' member an | |
4922 | index of type `enum scroll_bar_part' which we can use as an | |
4923 | index in scroll_bar_parts to get the appropriate symbol. */ | |
4924 | ||
4925 | case scroll_bar_click: | |
4926 | { | |
4927 | Lisp_Object position, head, window, portion_whole, part; | |
4928 | ||
4929 | window = event->frame_or_window; | |
4930 | portion_whole = Fcons (event->x, event->y); | |
4931 | part = *scroll_bar_parts[(int) event->part]; | |
4932 | ||
4933 | position | |
4934 | = Fcons (window, | |
4935 | Fcons (Qvertical_scroll_bar, | |
4936 | Fcons (portion_whole, | |
4937 | Fcons (make_number (event->timestamp), | |
4938 | Fcons (part, Qnil))))); | |
4939 | ||
4940 | /* Always treat scroll bar events as clicks. */ | |
4941 | event->modifiers |= click_modifier; | |
4942 | ||
4943 | /* Get the symbol we should use for the mouse click. */ | |
4944 | head = modify_event_symbol (event->code, | |
4945 | event->modifiers, | |
8e1e4240 GM |
4946 | Qmouse_click, |
4947 | Vlispy_mouse_stem, | |
4948 | NULL, &mouse_syms, | |
4949 | XVECTOR (mouse_syms)->size); | |
7ee32cda GM |
4950 | return Fcons (head, Fcons (position, Qnil)); |
4951 | } | |
4952 | ||
4953 | #endif /* USE_TOOLKIT_SCROLL_BARS */ | |
4954 | ||
db08707d | 4955 | #ifdef WINDOWSNT |
fbd6baed | 4956 | case w32_scroll_bar_click: |
db08707d RS |
4957 | { |
4958 | int button = event->code; | |
4959 | int is_double; | |
4960 | Lisp_Object position; | |
4961 | Lisp_Object *start_pos_ptr; | |
4962 | Lisp_Object start_pos; | |
4963 | ||
db08707d RS |
4964 | { |
4965 | Lisp_Object window; | |
4966 | Lisp_Object portion_whole; | |
4967 | Lisp_Object part; | |
4968 | ||
4969 | window = event->frame_or_window; | |
4970 | portion_whole = Fcons (event->x, event->y); | |
4971 | part = *scroll_bar_parts[(int) event->part]; | |
4972 | ||
e8886a1d RS |
4973 | position |
4974 | = Fcons (window, | |
4975 | Fcons (Qvertical_scroll_bar, | |
4976 | Fcons (portion_whole, | |
4977 | Fcons (make_number (event->timestamp), | |
4978 | Fcons (part, Qnil))))); | |
db08707d RS |
4979 | } |
4980 | ||
fbd6baed | 4981 | /* Always treat W32 scroll bar events as clicks. */ |
db08707d RS |
4982 | event->modifiers |= click_modifier; |
4983 | ||
4984 | { | |
4985 | /* Get the symbol we should use for the mouse click. */ | |
4986 | Lisp_Object head; | |
4987 | ||
4988 | head = modify_event_symbol (button, | |
4989 | event->modifiers, | |
8e1e4240 GM |
4990 | Qmouse_click, |
4991 | Vlispy_mouse_stem, | |
4992 | NULL, &mouse_syms, | |
4993 | XVECTOR (mouse_syms)->size); | |
db08707d RS |
4994 | return Fcons (head, |
4995 | Fcons (position, | |
4996 | Qnil)); | |
4997 | } | |
4998 | } | |
07de30b9 GV |
4999 | case mouse_wheel: |
5000 | { | |
5001 | int part; | |
5002 | FRAME_PTR f = XFRAME (event->frame_or_window); | |
5003 | Lisp_Object window; | |
5004 | Lisp_Object posn; | |
5005 | Lisp_Object head, position; | |
5006 | int row, column; | |
5007 | ||
5008 | /* Ignore mouse events that were made on frame that | |
5009 | have been deleted. */ | |
5010 | if (! FRAME_LIVE_P (f)) | |
5011 | return Qnil; | |
5012 | pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y), | |
5013 | &column, &row, NULL, 1); | |
afabdbe5 JR |
5014 | window = window_from_coordinates (f, XINT (event->x), |
5015 | XINT (event->y), &part, 0); | |
07de30b9 GV |
5016 | |
5017 | if (!WINDOWP (window)) | |
5018 | { | |
5019 | window = event->frame_or_window; | |
5020 | posn = Qnil; | |
5021 | } | |
5022 | else | |
5023 | { | |
5024 | int pixcolumn, pixrow; | |
5025 | column -= XINT (XWINDOW (window)->left); | |
5026 | row -= XINT (XWINDOW (window)->top); | |
e1bfeab2 JR |
5027 | glyph_to_pixel_coords (XWINDOW(window), column, row, |
5028 | &pixcolumn, &pixrow); | |
07de30b9 GV |
5029 | XSETINT (event->x, pixcolumn); |
5030 | XSETINT (event->y, pixrow); | |
5031 | ||
5032 | if (part == 1) | |
5033 | posn = Qmode_line; | |
5034 | else if (part == 2) | |
5035 | posn = Qvertical_line; | |
7ee32cda | 5036 | else if (part == 3) |
045dee35 | 5037 | posn = Qheader_line; |
07de30b9 GV |
5038 | else |
5039 | XSETINT (posn, | |
5040 | buffer_posn_from_coords (XWINDOW (window), | |
3cd2f327 | 5041 | &column, &row)); |
07de30b9 | 5042 | } |
db08707d | 5043 | |
07de30b9 GV |
5044 | { |
5045 | Lisp_Object head, position; | |
5046 | ||
5047 | position | |
5048 | = Fcons (window, | |
5049 | Fcons (posn, | |
5050 | Fcons (Fcons (event->x, event->y), | |
5051 | Fcons (make_number (event->timestamp), | |
5052 | Qnil)))); | |
5053 | ||
5054 | head = modify_event_symbol (0, event->modifiers, | |
5055 | Qmouse_wheel, Qnil, | |
5056 | lispy_mouse_wheel_names, | |
5057 | &mouse_wheel_syms, 1); | |
5058 | return Fcons (head, | |
5059 | Fcons (position, | |
5060 | Fcons (make_number (event->code), | |
5061 | Qnil))); | |
5062 | } | |
5063 | } | |
5064 | #endif /* WINDOWSNT */ | |
a24dc617 RS |
5065 | |
5066 | case drag_n_drop: | |
5067 | { | |
5068 | int part; | |
5069 | FRAME_PTR f; | |
5070 | Lisp_Object window; | |
5071 | Lisp_Object posn; | |
a24dc617 | 5072 | Lisp_Object files; |
a24dc617 RS |
5073 | |
5074 | /* The frame_or_window field should be a cons of the frame in | |
5075 | which the event occurred and a list of the filenames | |
5076 | dropped. */ | |
5077 | if (! CONSP (event->frame_or_window)) | |
5078 | abort (); | |
5079 | ||
7539e11f KR |
5080 | f = XFRAME (XCAR (event->frame_or_window)); |
5081 | files = XCDR (event->frame_or_window); | |
a24dc617 RS |
5082 | |
5083 | /* Ignore mouse events that were made on frames that | |
5084 | have been deleted. */ | |
5085 | if (! FRAME_LIVE_P (f)) | |
5086 | return Qnil; | |
afabdbe5 JR |
5087 | |
5088 | window = window_from_coordinates (f, XINT (event->x), | |
5089 | XINT (event->y), &part, 0); | |
a24dc617 RS |
5090 | |
5091 | if (!WINDOWP (window)) | |
5092 | { | |
7539e11f | 5093 | window = XCAR (event->frame_or_window); |
a24dc617 RS |
5094 | posn = Qnil; |
5095 | } | |
5096 | else | |
5097 | { | |
7ee32cda GM |
5098 | /* It's an event in window `window' at frame coordinates |
5099 | event->x/ event->y. */ | |
5100 | struct window *w = XWINDOW (window); | |
5101 | ||
5102 | /* Get window relative coordinates. */ | |
5103 | int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x)); | |
5104 | int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y)); | |
5105 | ||
5106 | /* Set event coordinates to window-relative coordinates | |
5107 | for constructing the Lisp event below. */ | |
5108 | XSETINT (event->x, wx); | |
5109 | XSETINT (event->y, wy); | |
a24dc617 RS |
5110 | |
5111 | if (part == 1) | |
5112 | posn = Qmode_line; | |
5113 | else if (part == 2) | |
5114 | posn = Qvertical_line; | |
7ee32cda | 5115 | else if (part == 3) |
045dee35 | 5116 | posn = Qheader_line; |
a24dc617 | 5117 | else |
7ee32cda | 5118 | XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy)); |
a24dc617 RS |
5119 | } |
5120 | ||
5121 | { | |
5122 | Lisp_Object head, position; | |
5123 | ||
5124 | position | |
5125 | = Fcons (window, | |
5126 | Fcons (posn, | |
5127 | Fcons (Fcons (event->x, event->y), | |
5128 | Fcons (make_number (event->timestamp), | |
5129 | Qnil)))); | |
5130 | ||
5131 | head = modify_event_symbol (0, event->modifiers, | |
5132 | Qdrag_n_drop, Qnil, | |
5133 | lispy_drag_n_drop_names, | |
5134 | &drag_n_drop_syms, 1); | |
5135 | return Fcons (head, | |
5136 | Fcons (position, | |
5137 | Fcons (files, | |
5138 | Qnil))); | |
5139 | } | |
5140 | } | |
514354e9 | 5141 | #endif /* HAVE_MOUSE */ |
284f4730 | 5142 | |
1a578e9b | 5143 | #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh) |
da8f7368 GM |
5144 | case MENU_BAR_EVENT: |
5145 | if (EQ (event->arg, event->frame_or_window)) | |
5146 | /* This is the prefix key. We translate this to | |
5147 | `(menu_bar)' because the code in keyboard.c for menu | |
5148 | events, which we use, relies on this. */ | |
5149 | return Fcons (Qmenu_bar, Qnil); | |
5150 | return event->arg; | |
2470a66f KH |
5151 | #endif |
5152 | ||
9ea173e8 | 5153 | case TOOL_BAR_EVENT: |
da8f7368 GM |
5154 | if (EQ (event->arg, event->frame_or_window)) |
5155 | /* This is the prefix key. We translate this to | |
5156 | `(tool_bar)' because the code in keyboard.c for menu | |
5157 | events, which we use, relies on this. */ | |
5158 | return Fcons (Qtool_bar, Qnil); | |
5159 | else if (SYMBOLP (event->arg)) | |
5160 | return apply_modifiers (event->modifiers, event->arg); | |
5161 | return event->arg; | |
5162 | ||
5163 | case USER_SIGNAL_EVENT: | |
5bf68f6e AS |
5164 | /* A user signal. */ |
5165 | return *lispy_user_signals[event->code]; | |
5166 | ||
284f4730 JB |
5167 | /* The 'kind' field of the event is something we don't recognize. */ |
5168 | default: | |
48e416d4 | 5169 | abort (); |
284f4730 JB |
5170 | } |
5171 | } | |
5172 | ||
514354e9 | 5173 | #ifdef HAVE_MOUSE |
6cbff1cb | 5174 | |
284f4730 | 5175 | static Lisp_Object |
7b4aedb9 | 5176 | make_lispy_movement (frame, bar_window, part, x, y, time) |
ff11dfa1 | 5177 | FRAME_PTR frame; |
7b4aedb9 | 5178 | Lisp_Object bar_window; |
3c370943 | 5179 | enum scroll_bar_part part; |
284f4730 | 5180 | Lisp_Object x, y; |
e5d77022 | 5181 | unsigned long time; |
284f4730 | 5182 | { |
3c370943 | 5183 | /* Is it a scroll bar movement? */ |
7b4aedb9 | 5184 | if (frame && ! NILP (bar_window)) |
4bb994d1 | 5185 | { |
9b8eb840 | 5186 | Lisp_Object part_sym; |
4bb994d1 | 5187 | |
9b8eb840 | 5188 | part_sym = *scroll_bar_parts[(int) part]; |
3c370943 | 5189 | return Fcons (Qscroll_bar_movement, |
7b4aedb9 | 5190 | (Fcons (Fcons (bar_window, |
3c370943 | 5191 | Fcons (Qvertical_scroll_bar, |
4bb994d1 JB |
5192 | Fcons (Fcons (x, y), |
5193 | Fcons (make_number (time), | |
cb5df6ae | 5194 | Fcons (part_sym, |
4bb994d1 JB |
5195 | Qnil))))), |
5196 | Qnil))); | |
5197 | } | |
5198 | ||
5199 | /* Or is it an ordinary mouse movement? */ | |
284f4730 JB |
5200 | else |
5201 | { | |
4bb994d1 | 5202 | int area; |
9e20143a | 5203 | Lisp_Object window; |
4bb994d1 | 5204 | Lisp_Object posn; |
9e20143a | 5205 | |
9e20143a | 5206 | if (frame) |
7ee32cda GM |
5207 | /* It's in a frame; which window on that frame? */ |
5208 | window = window_from_coordinates (frame, XINT (x), XINT (y), &area, 0); | |
9e20143a RS |
5209 | else |
5210 | window = Qnil; | |
4bb994d1 | 5211 | |
8c18cbfb | 5212 | if (WINDOWP (window)) |
4bb994d1 | 5213 | { |
7ee32cda | 5214 | struct window *w = XWINDOW (window); |
7ee32cda | 5215 | int wx, wy; |
7ee32cda GM |
5216 | |
5217 | /* Get window relative coordinates. */ | |
5218 | wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (x)); | |
5219 | wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (y)); | |
5220 | XSETINT (x, wx); | |
5221 | XSETINT (y, wy); | |
5222 | ||
4bb994d1 JB |
5223 | if (area == 1) |
5224 | posn = Qmode_line; | |
5225 | else if (area == 2) | |
5226 | posn = Qvertical_line; | |
7ee32cda | 5227 | else if (area == 3) |
045dee35 | 5228 | posn = Qheader_line; |
4bb994d1 | 5229 | else |
7ee32cda | 5230 | XSETINT (posn, buffer_posn_from_coords (w, &wx, &wy)); |
4bb994d1 | 5231 | } |
e9bf89a0 RS |
5232 | else if (frame != 0) |
5233 | { | |
18cd2eeb | 5234 | XSETFRAME (window, frame); |
e9bf89a0 RS |
5235 | posn = Qnil; |
5236 | } | |
284f4730 | 5237 | else |
4bb994d1 JB |
5238 | { |
5239 | window = Qnil; | |
5240 | posn = Qnil; | |
bb9e9bed KH |
5241 | XSETFASTINT (x, 0); |
5242 | XSETFASTINT (y, 0); | |
4bb994d1 | 5243 | } |
284f4730 | 5244 | |
4bb994d1 JB |
5245 | return Fcons (Qmouse_movement, |
5246 | Fcons (Fcons (window, | |
5247 | Fcons (posn, | |
5248 | Fcons (Fcons (x, y), | |
5249 | Fcons (make_number (time), | |
5250 | Qnil)))), | |
5251 | Qnil)); | |
5252 | } | |
284f4730 JB |
5253 | } |
5254 | ||
514354e9 | 5255 | #endif /* HAVE_MOUSE */ |
6cbff1cb | 5256 | |
cd21b839 JB |
5257 | /* Construct a switch frame event. */ |
5258 | static Lisp_Object | |
5259 | make_lispy_switch_frame (frame) | |
5260 | Lisp_Object frame; | |
5261 | { | |
5262 | return Fcons (Qswitch_frame, Fcons (frame, Qnil)); | |
5263 | } | |
0a7f1fc0 JB |
5264 | \f |
5265 | /* Manipulating modifiers. */ | |
284f4730 | 5266 | |
0a7f1fc0 | 5267 | /* Parse the name of SYMBOL, and return the set of modifiers it contains. |
284f4730 | 5268 | |
0a7f1fc0 JB |
5269 | If MODIFIER_END is non-zero, set *MODIFIER_END to the position in |
5270 | SYMBOL's name of the end of the modifiers; the string from this | |
5271 | position is the unmodified symbol name. | |
284f4730 | 5272 | |
0a7f1fc0 | 5273 | This doesn't use any caches. */ |
6da3dd3a | 5274 | |
0a7f1fc0 JB |
5275 | static int |
5276 | parse_modifiers_uncached (symbol, modifier_end) | |
284f4730 | 5277 | Lisp_Object symbol; |
0a7f1fc0 | 5278 | int *modifier_end; |
284f4730 JB |
5279 | { |
5280 | struct Lisp_String *name; | |
5281 | int i; | |
5282 | int modifiers; | |
284f4730 JB |
5283 | |
5284 | CHECK_SYMBOL (symbol, 1); | |
df0f2ba1 | 5285 | |
284f4730 JB |
5286 | modifiers = 0; |
5287 | name = XSYMBOL (symbol)->name; | |
5288 | ||
fc932ac6 | 5289 | for (i = 0; i+2 <= STRING_BYTES (name); ) |
6da3dd3a RS |
5290 | { |
5291 | int this_mod_end = 0; | |
5292 | int this_mod = 0; | |
284f4730 | 5293 | |
6da3dd3a RS |
5294 | /* See if the name continues with a modifier word. |
5295 | Check that the word appears, but don't check what follows it. | |
5296 | Set this_mod and this_mod_end to record what we find. */ | |
fce33686 | 5297 | |
6da3dd3a RS |
5298 | switch (name->data[i]) |
5299 | { | |
5300 | #define SINGLE_LETTER_MOD(BIT) \ | |
5301 | (this_mod_end = i + 1, this_mod = BIT) | |
5302 | ||
6da3dd3a RS |
5303 | case 'A': |
5304 | SINGLE_LETTER_MOD (alt_modifier); | |
5305 | break; | |
284f4730 | 5306 | |
6da3dd3a RS |
5307 | case 'C': |
5308 | SINGLE_LETTER_MOD (ctrl_modifier); | |
5309 | break; | |
284f4730 | 5310 | |
6da3dd3a RS |
5311 | case 'H': |
5312 | SINGLE_LETTER_MOD (hyper_modifier); | |
5313 | break; | |
5314 | ||
6da3dd3a RS |
5315 | case 'M': |
5316 | SINGLE_LETTER_MOD (meta_modifier); | |
5317 | break; | |
5318 | ||
6da3dd3a RS |
5319 | case 'S': |
5320 | SINGLE_LETTER_MOD (shift_modifier); | |
5321 | break; | |
5322 | ||
5323 | case 's': | |
6da3dd3a RS |
5324 | SINGLE_LETTER_MOD (super_modifier); |
5325 | break; | |
5326 | ||
0a7f1fc0 | 5327 | #undef SINGLE_LETTER_MOD |
6da3dd3a RS |
5328 | } |
5329 | ||
5330 | /* If we found no modifier, stop looking for them. */ | |
5331 | if (this_mod_end == 0) | |
5332 | break; | |
5333 | ||
5334 | /* Check there is a dash after the modifier, so that it | |
5335 | really is a modifier. */ | |
fc932ac6 RS |
5336 | if (this_mod_end >= STRING_BYTES (name) |
5337 | || name->data[this_mod_end] != '-') | |
6da3dd3a RS |
5338 | break; |
5339 | ||
5340 | /* This modifier is real; look for another. */ | |
5341 | modifiers |= this_mod; | |
5342 | i = this_mod_end + 1; | |
5343 | } | |
284f4730 | 5344 | |
0a7f1fc0 | 5345 | /* Should we include the `click' modifier? */ |
fbcd35bd JB |
5346 | if (! (modifiers & (down_modifier | drag_modifier |
5347 | | double_modifier | triple_modifier)) | |
fc932ac6 | 5348 | && i + 7 == STRING_BYTES (name) |
4bb994d1 | 5349 | && strncmp (name->data + i, "mouse-", 6) == 0 |
6569cc8d | 5350 | && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9')) |
0a7f1fc0 JB |
5351 | modifiers |= click_modifier; |
5352 | ||
5353 | if (modifier_end) | |
5354 | *modifier_end = i; | |
5355 | ||
5356 | return modifiers; | |
5357 | } | |
5358 | ||
0a7f1fc0 JB |
5359 | /* Return a symbol whose name is the modifier prefixes for MODIFIERS |
5360 | prepended to the string BASE[0..BASE_LEN-1]. | |
5361 | This doesn't use any caches. */ | |
5362 | static Lisp_Object | |
301738ed | 5363 | apply_modifiers_uncached (modifiers, base, base_len, base_len_byte) |
0a7f1fc0 JB |
5364 | int modifiers; |
5365 | char *base; | |
301738ed | 5366 | int base_len, base_len_byte; |
0a7f1fc0 JB |
5367 | { |
5368 | /* Since BASE could contain nulls, we can't use intern here; we have | |
5369 | to use Fintern, which expects a genuine Lisp_String, and keeps a | |
5370 | reference to it. */ | |
301738ed RS |
5371 | char *new_mods |
5372 | = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-")); | |
0a7f1fc0 | 5373 | int mod_len; |
284f4730 | 5374 | |
284f4730 | 5375 | { |
0a7f1fc0 JB |
5376 | char *p = new_mods; |
5377 | ||
5378 | /* Only the event queue may use the `up' modifier; it should always | |
5379 | be turned into a click or drag event before presented to lisp code. */ | |
5380 | if (modifiers & up_modifier) | |
5381 | abort (); | |
5382 | ||
5383 | if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; } | |
5384 | if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; } | |
5385 | if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; } | |
5386 | if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; } | |
5387 | if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; } | |
86e5706b | 5388 | if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; } |
fbcd35bd JB |
5389 | if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; } |
5390 | if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; } | |
559f9d04 RS |
5391 | if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; } |
5392 | if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; } | |
0a7f1fc0 JB |
5393 | /* The click modifier is denoted by the absence of other modifiers. */ |
5394 | ||
5395 | *p = '\0'; | |
5396 | ||
5397 | mod_len = p - new_mods; | |
5398 | } | |
284f4730 | 5399 | |
0a7f1fc0 | 5400 | { |
9b8eb840 | 5401 | Lisp_Object new_name; |
df0f2ba1 | 5402 | |
301738ed RS |
5403 | new_name = make_uninit_multibyte_string (mod_len + base_len, |
5404 | mod_len + base_len_byte); | |
0a7f1fc0 | 5405 | bcopy (new_mods, XSTRING (new_name)->data, mod_len); |
301738ed | 5406 | bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte); |
284f4730 JB |
5407 | |
5408 | return Fintern (new_name, Qnil); | |
5409 | } | |
5410 | } | |
5411 | ||
5412 | ||
0a7f1fc0 JB |
5413 | static char *modifier_names[] = |
5414 | { | |
fbcd35bd | 5415 | "up", "down", "drag", "click", "double", "triple", 0, 0, |
f335fabe | 5416 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
86e5706b | 5417 | 0, 0, "alt", "super", "hyper", "shift", "control", "meta" |
0a7f1fc0 | 5418 | }; |
80645119 | 5419 | #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0])) |
0a7f1fc0 JB |
5420 | |
5421 | static Lisp_Object modifier_symbols; | |
5422 | ||
5423 | /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */ | |
5424 | static Lisp_Object | |
5425 | lispy_modifier_list (modifiers) | |
5426 | int modifiers; | |
5427 | { | |
5428 | Lisp_Object modifier_list; | |
5429 | int i; | |
5430 | ||
5431 | modifier_list = Qnil; | |
80645119 | 5432 | for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++) |
0a7f1fc0 | 5433 | if (modifiers & (1<<i)) |
80645119 JB |
5434 | modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i], |
5435 | modifier_list); | |
0a7f1fc0 JB |
5436 | |
5437 | return modifier_list; | |
5438 | } | |
5439 | ||
5440 | ||
5441 | /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK), | |
5442 | where UNMODIFIED is the unmodified form of SYMBOL, | |
5443 | MASK is the set of modifiers present in SYMBOL's name. | |
5444 | This is similar to parse_modifiers_uncached, but uses the cache in | |
5445 | SYMBOL's Qevent_symbol_element_mask property, and maintains the | |
5446 | Qevent_symbol_elements property. */ | |
3d31316f | 5447 | |
1161d367 | 5448 | Lisp_Object |
0a7f1fc0 JB |
5449 | parse_modifiers (symbol) |
5450 | Lisp_Object symbol; | |
5451 | { | |
9b8eb840 | 5452 | Lisp_Object elements; |
0a7f1fc0 | 5453 | |
9b8eb840 | 5454 | elements = Fget (symbol, Qevent_symbol_element_mask); |
0a7f1fc0 JB |
5455 | if (CONSP (elements)) |
5456 | return elements; | |
5457 | else | |
5458 | { | |
5459 | int end; | |
ec0faad2 | 5460 | int modifiers = parse_modifiers_uncached (symbol, &end); |
9b8eb840 | 5461 | Lisp_Object unmodified; |
0a7f1fc0 JB |
5462 | Lisp_Object mask; |
5463 | ||
9b8eb840 | 5464 | unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end, |
fc932ac6 | 5465 | STRING_BYTES (XSYMBOL (symbol)->name) - end), |
9b8eb840 KH |
5466 | Qnil); |
5467 | ||
ec0faad2 | 5468 | if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1)) |
734fef94 | 5469 | abort (); |
bb9e9bed | 5470 | XSETFASTINT (mask, modifiers); |
0a7f1fc0 JB |
5471 | elements = Fcons (unmodified, Fcons (mask, Qnil)); |
5472 | ||
5473 | /* Cache the parsing results on SYMBOL. */ | |
5474 | Fput (symbol, Qevent_symbol_element_mask, | |
5475 | elements); | |
5476 | Fput (symbol, Qevent_symbol_elements, | |
5477 | Fcons (unmodified, lispy_modifier_list (modifiers))); | |
5478 | ||
5479 | /* Since we know that SYMBOL is modifiers applied to unmodified, | |
5480 | it would be nice to put that in unmodified's cache. | |
5481 | But we can't, since we're not sure that parse_modifiers is | |
5482 | canonical. */ | |
5483 | ||
5484 | return elements; | |
5485 | } | |
5486 | } | |
5487 | ||
5488 | /* Apply the modifiers MODIFIERS to the symbol BASE. | |
5489 | BASE must be unmodified. | |
5490 | ||
5491 | This is like apply_modifiers_uncached, but uses BASE's | |
5492 | Qmodifier_cache property, if present. It also builds | |
cd21b839 JB |
5493 | Qevent_symbol_elements properties, since it has that info anyway. |
5494 | ||
5495 | apply_modifiers copies the value of BASE's Qevent_kind property to | |
5496 | the modified symbol. */ | |
0a7f1fc0 JB |
5497 | static Lisp_Object |
5498 | apply_modifiers (modifiers, base) | |
5499 | int modifiers; | |
5500 | Lisp_Object base; | |
5501 | { | |
7b4aedb9 | 5502 | Lisp_Object cache, index, entry, new_symbol; |
0a7f1fc0 | 5503 | |
80645119 | 5504 | /* Mask out upper bits. We don't know where this value's been. */ |
ec0faad2 | 5505 | modifiers &= ((EMACS_INT)1 << VALBITS) - 1; |
80645119 | 5506 | |
0a7f1fc0 | 5507 | /* The click modifier never figures into cache indices. */ |
0a7f1fc0 | 5508 | cache = Fget (base, Qmodifier_cache); |
bb9e9bed | 5509 | XSETFASTINT (index, (modifiers & ~click_modifier)); |
697e4895 | 5510 | entry = assq_no_quit (index, cache); |
0a7f1fc0 JB |
5511 | |
5512 | if (CONSP (entry)) | |
7539e11f | 5513 | new_symbol = XCDR (entry); |
7b4aedb9 JB |
5514 | else |
5515 | { | |
df0f2ba1 | 5516 | /* We have to create the symbol ourselves. */ |
7b4aedb9 JB |
5517 | new_symbol = apply_modifiers_uncached (modifiers, |
5518 | XSYMBOL (base)->name->data, | |
301738ed | 5519 | XSYMBOL (base)->name->size, |
fc932ac6 | 5520 | STRING_BYTES (XSYMBOL (base)->name)); |
7b4aedb9 JB |
5521 | |
5522 | /* Add the new symbol to the base's cache. */ | |
5523 | entry = Fcons (index, new_symbol); | |
5524 | Fput (base, Qmodifier_cache, Fcons (entry, cache)); | |
5525 | ||
5526 | /* We have the parsing info now for free, so add it to the caches. */ | |
bb9e9bed | 5527 | XSETFASTINT (index, modifiers); |
7b4aedb9 JB |
5528 | Fput (new_symbol, Qevent_symbol_element_mask, |
5529 | Fcons (base, Fcons (index, Qnil))); | |
5530 | Fput (new_symbol, Qevent_symbol_elements, | |
5531 | Fcons (base, lispy_modifier_list (modifiers))); | |
5532 | } | |
0a7f1fc0 | 5533 | |
df0f2ba1 | 5534 | /* Make sure this symbol is of the same kind as BASE. |
7b4aedb9 JB |
5535 | |
5536 | You'd think we could just set this once and for all when we | |
5537 | intern the symbol above, but reorder_modifiers may call us when | |
5538 | BASE's property isn't set right; we can't assume that just | |
80645119 JB |
5539 | because it has a Qmodifier_cache property it must have its |
5540 | Qevent_kind set right as well. */ | |
7b4aedb9 JB |
5541 | if (NILP (Fget (new_symbol, Qevent_kind))) |
5542 | { | |
9b8eb840 | 5543 | Lisp_Object kind; |
7b4aedb9 | 5544 | |
9b8eb840 | 5545 | kind = Fget (base, Qevent_kind); |
7b4aedb9 JB |
5546 | if (! NILP (kind)) |
5547 | Fput (new_symbol, Qevent_kind, kind); | |
5548 | } | |
5549 | ||
5550 | return new_symbol; | |
0a7f1fc0 JB |
5551 | } |
5552 | ||
5553 | ||
5554 | /* Given a symbol whose name begins with modifiers ("C-", "M-", etc), | |
5555 | return a symbol with the modifiers placed in the canonical order. | |
5556 | Canonical order is alphabetical, except for down and drag, which | |
5557 | always come last. The 'click' modifier is never written out. | |
5558 | ||
5559 | Fdefine_key calls this to make sure that (for example) C-M-foo | |
5560 | and M-C-foo end up being equivalent in the keymap. */ | |
5561 | ||
5562 | Lisp_Object | |
5563 | reorder_modifiers (symbol) | |
5564 | Lisp_Object symbol; | |
5565 | { | |
5566 | /* It's hopefully okay to write the code this way, since everything | |
5567 | will soon be in caches, and no consing will be done at all. */ | |
9b8eb840 | 5568 | Lisp_Object parsed; |
0a7f1fc0 | 5569 | |
9b8eb840 | 5570 | parsed = parse_modifiers (symbol); |
7539e11f KR |
5571 | return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))), |
5572 | XCAR (parsed)); | |
0a7f1fc0 JB |
5573 | } |
5574 | ||
5575 | ||
284f4730 JB |
5576 | /* For handling events, we often want to produce a symbol whose name |
5577 | is a series of modifier key prefixes ("M-", "C-", etcetera) attached | |
5578 | to some base, like the name of a function key or mouse button. | |
5579 | modify_event_symbol produces symbols of this sort. | |
5580 | ||
5581 | NAME_TABLE should point to an array of strings, such that NAME_TABLE[i] | |
5582 | is the name of the i'th symbol. TABLE_SIZE is the number of elements | |
5583 | in the table. | |
5584 | ||
8e1e4240 GM |
5585 | Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes |
5586 | into symbol names, or a string specifying a name stem used to | |
a50e723f | 5587 | construct a symbol name or the form `STEM-N', where N is the decimal |
8e1e4240 GM |
5588 | representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is |
5589 | non-nil; otherwise NAME_TABLE is used. | |
80e4aa30 | 5590 | |
284f4730 JB |
5591 | SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will |
5592 | persist between calls to modify_event_symbol that it can use to | |
5593 | store a cache of the symbols it's generated for this NAME_TABLE | |
80e4aa30 | 5594 | before. The object stored there may be a vector or an alist. |
284f4730 JB |
5595 | |
5596 | SYMBOL_NUM is the number of the base name we want from NAME_TABLE. | |
df0f2ba1 | 5597 | |
284f4730 JB |
5598 | MODIFIERS is a set of modifier bits (as given in struct input_events) |
5599 | whose prefixes should be applied to the symbol name. | |
5600 | ||
5601 | SYMBOL_KIND is the value to be placed in the event_kind property of | |
df0f2ba1 | 5602 | the returned symbol. |
88cb0656 JB |
5603 | |
5604 | The symbols we create are supposed to have an | |
eb8c3be9 | 5605 | `event-symbol-elements' property, which lists the modifiers present |
88cb0656 JB |
5606 | in the symbol's name. */ |
5607 | ||
284f4730 | 5608 | static Lisp_Object |
8e1e4240 | 5609 | modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem, |
80e4aa30 | 5610 | name_table, symbol_table, table_size) |
284f4730 JB |
5611 | int symbol_num; |
5612 | unsigned modifiers; | |
5613 | Lisp_Object symbol_kind; | |
8e1e4240 | 5614 | Lisp_Object name_alist_or_stem; |
284f4730 JB |
5615 | char **name_table; |
5616 | Lisp_Object *symbol_table; | |
2c834fb3 | 5617 | unsigned int table_size; |
284f4730 | 5618 | { |
80e4aa30 RS |
5619 | Lisp_Object value; |
5620 | Lisp_Object symbol_int; | |
5621 | ||
2c834fb3 KH |
5622 | /* Get rid of the "vendor-specific" bit here. */ |
5623 | XSETINT (symbol_int, symbol_num & 0xffffff); | |
284f4730 JB |
5624 | |
5625 | /* Is this a request for a valid symbol? */ | |
88cb0656 | 5626 | if (symbol_num < 0 || symbol_num >= table_size) |
0c2611c5 | 5627 | return Qnil; |
284f4730 | 5628 | |
80e4aa30 RS |
5629 | if (CONSP (*symbol_table)) |
5630 | value = Fcdr (assq_no_quit (symbol_int, *symbol_table)); | |
5631 | ||
0a7f1fc0 | 5632 | /* If *symbol_table doesn't seem to be initialized properly, fix that. |
88cb0656 | 5633 | *symbol_table should be a lisp vector TABLE_SIZE elements long, |
4bb994d1 JB |
5634 | where the Nth element is the symbol for NAME_TABLE[N], or nil if |
5635 | we've never used that symbol before. */ | |
80e4aa30 | 5636 | else |
88cb0656 | 5637 | { |
80e4aa30 RS |
5638 | if (! VECTORP (*symbol_table) |
5639 | || XVECTOR (*symbol_table)->size != table_size) | |
5640 | { | |
5641 | Lisp_Object size; | |
0a7f1fc0 | 5642 | |
bb9e9bed | 5643 | XSETFASTINT (size, table_size); |
80e4aa30 RS |
5644 | *symbol_table = Fmake_vector (size, Qnil); |
5645 | } | |
284f4730 | 5646 | |
80e4aa30 RS |
5647 | value = XVECTOR (*symbol_table)->contents[symbol_num]; |
5648 | } | |
284f4730 | 5649 | |
0a7f1fc0 | 5650 | /* Have we already used this symbol before? */ |
80e4aa30 | 5651 | if (NILP (value)) |
284f4730 | 5652 | { |
0a7f1fc0 | 5653 | /* No; let's create it. */ |
8e1e4240 GM |
5654 | if (CONSP (name_alist_or_stem)) |
5655 | value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem)); | |
5656 | else if (STRINGP (name_alist_or_stem)) | |
5657 | { | |
5658 | int len = STRING_BYTES (XSTRING (name_alist_or_stem)); | |
5659 | char *buf = (char *) alloca (len + 50); | |
5660 | sprintf (buf, "%s-%d", XSTRING (name_alist_or_stem)->data, | |
5661 | XINT (symbol_int) + 1); | |
5662 | value = intern (buf); | |
5663 | } | |
2ff6714d | 5664 | else if (name_table != 0 && name_table[symbol_num]) |
80e4aa30 | 5665 | value = intern (name_table[symbol_num]); |
b64b4075 | 5666 | |
e98a93eb | 5667 | #ifdef HAVE_WINDOW_SYSTEM |
2c834fb3 KH |
5668 | if (NILP (value)) |
5669 | { | |
5670 | char *name = x_get_keysym_name (symbol_num); | |
5671 | if (name) | |
5672 | value = intern (name); | |
5673 | } | |
5674 | #endif | |
5675 | ||
b64b4075 | 5676 | if (NILP (value)) |
d1f50460 RS |
5677 | { |
5678 | char buf[20]; | |
5679 | sprintf (buf, "key-%d", symbol_num); | |
80e4aa30 | 5680 | value = intern (buf); |
d1f50460 | 5681 | } |
0a7f1fc0 | 5682 | |
80e4aa30 | 5683 | if (CONSP (*symbol_table)) |
4205cb08 | 5684 | *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table); |
80e4aa30 RS |
5685 | else |
5686 | XVECTOR (*symbol_table)->contents[symbol_num] = value; | |
5687 | ||
df0f2ba1 | 5688 | /* Fill in the cache entries for this symbol; this also |
0a7f1fc0 JB |
5689 | builds the Qevent_symbol_elements property, which the user |
5690 | cares about. */ | |
80e4aa30 RS |
5691 | apply_modifiers (modifiers & click_modifier, value); |
5692 | Fput (value, Qevent_kind, symbol_kind); | |
284f4730 | 5693 | } |
88cb0656 | 5694 | |
0a7f1fc0 | 5695 | /* Apply modifiers to that symbol. */ |
80e4aa30 | 5696 | return apply_modifiers (modifiers, value); |
284f4730 | 5697 | } |
6da3dd3a RS |
5698 | \f |
5699 | /* Convert a list that represents an event type, | |
5700 | such as (ctrl meta backspace), into the usual representation of that | |
5701 | event type as a number or a symbol. */ | |
5702 | ||
a1706c30 | 5703 | DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0, |
e57d8fd8 EN |
5704 | "Convert the event description list EVENT-DESC to an event type.\n\ |
5705 | EVENT-DESC should contain one base event type (a character or symbol)\n\ | |
a1706c30 | 5706 | and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\ |
377f24f5 | 5707 | drag, down, double or triple). The base must be last.\n\ |
a1706c30 KH |
5708 | The return value is an event type (a character or symbol) which\n\ |
5709 | has the same base event type and all the specified modifiers.") | |
e57d8fd8 EN |
5710 | (event_desc) |
5711 | Lisp_Object event_desc; | |
6da3dd3a RS |
5712 | { |
5713 | Lisp_Object base; | |
5714 | int modifiers = 0; | |
5715 | Lisp_Object rest; | |
5716 | ||
5717 | base = Qnil; | |
e57d8fd8 | 5718 | rest = event_desc; |
6da3dd3a RS |
5719 | while (CONSP (rest)) |
5720 | { | |
5721 | Lisp_Object elt; | |
5722 | int this = 0; | |
5723 | ||
7539e11f KR |
5724 | elt = XCAR (rest); |
5725 | rest = XCDR (rest); | |
6da3dd3a | 5726 | |
3d31316f | 5727 | /* Given a symbol, see if it is a modifier name. */ |
377f24f5 | 5728 | if (SYMBOLP (elt) && CONSP (rest)) |
3d31316f | 5729 | this = parse_solitary_modifier (elt); |
6da3dd3a RS |
5730 | |
5731 | if (this != 0) | |
5732 | modifiers |= this; | |
5733 | else if (!NILP (base)) | |
5734 | error ("Two bases given in one event"); | |
5735 | else | |
5736 | base = elt; | |
5737 | ||
6da3dd3a RS |
5738 | } |
5739 | ||
3d31316f RS |
5740 | /* Let the symbol A refer to the character A. */ |
5741 | if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1) | |
5742 | XSETINT (base, XSYMBOL (base)->name->data[0]); | |
5743 | ||
6da3dd3a RS |
5744 | if (INTEGERP (base)) |
5745 | { | |
3d31316f RS |
5746 | /* Turn (shift a) into A. */ |
5747 | if ((modifiers & shift_modifier) != 0 | |
5748 | && (XINT (base) >= 'a' && XINT (base) <= 'z')) | |
5749 | { | |
5750 | XSETINT (base, XINT (base) - ('a' - 'A')); | |
5751 | modifiers &= ~shift_modifier; | |
5752 | } | |
5753 | ||
5754 | /* Turn (control a) into C-a. */ | |
6da3dd3a | 5755 | if (modifiers & ctrl_modifier) |
3d31316f | 5756 | return make_number ((modifiers & ~ctrl_modifier) |
6da3dd3a RS |
5757 | | make_ctrl_char (XINT (base))); |
5758 | else | |
5759 | return make_number (modifiers | XINT (base)); | |
5760 | } | |
5761 | else if (SYMBOLP (base)) | |
5762 | return apply_modifiers (modifiers, base); | |
5763 | else | |
8c907a56 GM |
5764 | { |
5765 | error ("Invalid base event"); | |
5766 | return Qnil; | |
5767 | } | |
6da3dd3a RS |
5768 | } |
5769 | ||
3d31316f RS |
5770 | /* Try to recognize SYMBOL as a modifier name. |
5771 | Return the modifier flag bit, or 0 if not recognized. */ | |
5772 | ||
5773 | static int | |
5774 | parse_solitary_modifier (symbol) | |
5775 | Lisp_Object symbol; | |
5776 | { | |
5777 | struct Lisp_String *name = XSYMBOL (symbol)->name; | |
5778 | ||
5779 | switch (name->data[0]) | |
5780 | { | |
5781 | #define SINGLE_LETTER_MOD(BIT) \ | |
fc932ac6 | 5782 | if (STRING_BYTES (name) == 1) \ |
3d31316f RS |
5783 | return BIT; |
5784 | ||
5785 | #define MULTI_LETTER_MOD(BIT, NAME, LEN) \ | |
fc932ac6 | 5786 | if (LEN == STRING_BYTES (name) \ |
3d31316f RS |
5787 | && ! strncmp (name->data, NAME, LEN)) \ |
5788 | return BIT; | |
5789 | ||
5790 | case 'A': | |
5791 | SINGLE_LETTER_MOD (alt_modifier); | |
5792 | break; | |
5793 | ||
5794 | case 'a': | |
5795 | MULTI_LETTER_MOD (alt_modifier, "alt", 3); | |
5796 | break; | |
5797 | ||
5798 | case 'C': | |
5799 | SINGLE_LETTER_MOD (ctrl_modifier); | |
5800 | break; | |
5801 | ||
5802 | case 'c': | |
5803 | MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4); | |
5804 | MULTI_LETTER_MOD (ctrl_modifier, "control", 7); | |
5805 | break; | |
5806 | ||
5807 | case 'H': | |
5808 | SINGLE_LETTER_MOD (hyper_modifier); | |
5809 | break; | |
5810 | ||
5811 | case 'h': | |
5812 | MULTI_LETTER_MOD (hyper_modifier, "hyper", 5); | |
5813 | break; | |
5814 | ||
5815 | case 'M': | |
5816 | SINGLE_LETTER_MOD (meta_modifier); | |
5817 | break; | |
5818 | ||
5819 | case 'm': | |
5820 | MULTI_LETTER_MOD (meta_modifier, "meta", 4); | |
5821 | break; | |
5822 | ||
5823 | case 'S': | |
5824 | SINGLE_LETTER_MOD (shift_modifier); | |
5825 | break; | |
5826 | ||
5827 | case 's': | |
5828 | MULTI_LETTER_MOD (shift_modifier, "shift", 5); | |
5829 | MULTI_LETTER_MOD (super_modifier, "super", 5); | |
5830 | SINGLE_LETTER_MOD (super_modifier); | |
5831 | break; | |
5832 | ||
5833 | case 'd': | |
5834 | MULTI_LETTER_MOD (drag_modifier, "drag", 4); | |
5835 | MULTI_LETTER_MOD (down_modifier, "down", 4); | |
5836 | MULTI_LETTER_MOD (double_modifier, "double", 6); | |
5837 | break; | |
5838 | ||
5839 | case 't': | |
5840 | MULTI_LETTER_MOD (triple_modifier, "triple", 6); | |
5841 | break; | |
5842 | ||
5843 | #undef SINGLE_LETTER_MOD | |
5844 | #undef MULTI_LETTER_MOD | |
5845 | } | |
5846 | ||
5847 | return 0; | |
5848 | } | |
5849 | ||
6da3dd3a RS |
5850 | /* Return 1 if EVENT is a list whose elements are all integers or symbols. |
5851 | Such a list is not valid as an event, | |
5852 | but it can be a Lucid-style event type list. */ | |
5853 | ||
5854 | int | |
5855 | lucid_event_type_list_p (object) | |
5856 | Lisp_Object object; | |
5857 | { | |
5858 | Lisp_Object tail; | |
5859 | ||
5860 | if (! CONSP (object)) | |
5861 | return 0; | |
902ae620 GM |
5862 | |
5863 | if (EQ (XCAR (object), Qhelp_echo) | |
5864 | || EQ (XCAR (object), Qvertical_line) | |
5865 | || EQ (XCAR (object), Qmode_line) | |
5866 | || EQ (XCAR (object), Qheader_line)) | |
5867 | return 0; | |
6da3dd3a | 5868 | |
7539e11f | 5869 | for (tail = object; CONSP (tail); tail = XCDR (tail)) |
6da3dd3a RS |
5870 | { |
5871 | Lisp_Object elt; | |
7539e11f | 5872 | elt = XCAR (tail); |
6da3dd3a RS |
5873 | if (! (INTEGERP (elt) || SYMBOLP (elt))) |
5874 | return 0; | |
5875 | } | |
5876 | ||
5877 | return NILP (tail); | |
5878 | } | |
284f4730 | 5879 | \f |
284f4730 JB |
5880 | /* Store into *addr a value nonzero if terminal input chars are available. |
5881 | Serves the purpose of ioctl (0, FIONREAD, addr) | |
5882 | but works even if FIONREAD does not exist. | |
d9d4c147 KH |
5883 | (In fact, this may actually read some input.) |
5884 | ||
5885 | If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */ | |
284f4730 JB |
5886 | |
5887 | static void | |
d9d4c147 | 5888 | get_input_pending (addr, do_timers_now) |
284f4730 | 5889 | int *addr; |
d9d4c147 | 5890 | int do_timers_now; |
284f4730 JB |
5891 | { |
5892 | /* First of all, have we already counted some input? */ | |
d9d4c147 | 5893 | *addr = !NILP (Vquit_flag) || readable_events (do_timers_now); |
284f4730 JB |
5894 | |
5895 | /* If input is being read as it arrives, and we have none, there is none. */ | |
5896 | if (*addr > 0 || (interrupt_input && ! interrupts_deferred)) | |
5897 | return; | |
5898 | ||
5899 | /* Try to read some input and see how much we get. */ | |
5900 | gobble_input (0); | |
d9d4c147 | 5901 | *addr = !NILP (Vquit_flag) || readable_events (do_timers_now); |
284f4730 JB |
5902 | } |
5903 | ||
81931ba1 | 5904 | /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */ |
284f4730 | 5905 | |
07a59269 | 5906 | void |
284f4730 JB |
5907 | gobble_input (expected) |
5908 | int expected; | |
5909 | { | |
5910 | #ifndef VMS | |
5911 | #ifdef SIGIO | |
5912 | if (interrupt_input) | |
5913 | { | |
32676c08 | 5914 | SIGMASKTYPE mask; |
4f8aaa74 | 5915 | mask = sigblock (sigmask (SIGIO)); |
284f4730 | 5916 | read_avail_input (expected); |
e065a56e | 5917 | sigsetmask (mask); |
284f4730 JB |
5918 | } |
5919 | else | |
81931ba1 RS |
5920 | #ifdef POLL_FOR_INPUT |
5921 | if (read_socket_hook && !interrupt_input && poll_suppress_count == 0) | |
5922 | { | |
5923 | SIGMASKTYPE mask; | |
4f8aaa74 | 5924 | mask = sigblock (sigmask (SIGALRM)); |
81931ba1 RS |
5925 | read_avail_input (expected); |
5926 | sigsetmask (mask); | |
5927 | } | |
5928 | else | |
87485d6f | 5929 | #endif |
284f4730 JB |
5930 | #endif |
5931 | read_avail_input (expected); | |
5932 | #endif | |
5933 | } | |
a8015ab5 | 5934 | |
241ceaf7 RS |
5935 | /* Put a buffer_switch_event in the buffer |
5936 | so that read_key_sequence will notice the new current buffer. */ | |
5937 | ||
07a59269 | 5938 | void |
a8015ab5 KH |
5939 | record_asynch_buffer_change () |
5940 | { | |
5941 | struct input_event event; | |
a30f0615 RS |
5942 | Lisp_Object tem; |
5943 | ||
a8015ab5 KH |
5944 | event.kind = buffer_switch_event; |
5945 | event.frame_or_window = Qnil; | |
da8f7368 | 5946 | event.arg = Qnil; |
241ceaf7 | 5947 | |
f65e6f7d | 5948 | #ifdef subprocesses |
a30f0615 RS |
5949 | /* We don't need a buffer-switch event unless Emacs is waiting for input. |
5950 | The purpose of the event is to make read_key_sequence look up the | |
5951 | keymaps again. If we aren't in read_key_sequence, we don't need one, | |
5952 | and the event could cause trouble by messing up (input-pending-p). */ | |
5953 | tem = Fwaiting_for_user_input_p (); | |
5954 | if (NILP (tem)) | |
5955 | return; | |
f65e6f7d RS |
5956 | #else |
5957 | /* We never need these events if we have no asynchronous subprocesses. */ | |
5958 | return; | |
5959 | #endif | |
a30f0615 | 5960 | |
241ceaf7 RS |
5961 | /* Make sure no interrupt happens while storing the event. */ |
5962 | #ifdef SIGIO | |
5963 | if (interrupt_input) | |
5964 | { | |
5965 | SIGMASKTYPE mask; | |
4f8aaa74 | 5966 | mask = sigblock (sigmask (SIGIO)); |
241ceaf7 RS |
5967 | kbd_buffer_store_event (&event); |
5968 | sigsetmask (mask); | |
5969 | } | |
5970 | else | |
5971 | #endif | |
5972 | { | |
5973 | stop_polling (); | |
5974 | kbd_buffer_store_event (&event); | |
5975 | start_polling (); | |
5976 | } | |
a8015ab5 | 5977 | } |
284f4730 JB |
5978 | \f |
5979 | #ifndef VMS | |
5980 | ||
5981 | /* Read any terminal input already buffered up by the system | |
5982 | into the kbd_buffer, but do not wait. | |
5983 | ||
5984 | EXPECTED should be nonzero if the caller knows there is some input. | |
5985 | ||
5986 | Except on VMS, all input is read by this function. | |
5987 | If interrupt_input is nonzero, this function MUST be called | |
5988 | only when SIGIO is blocked. | |
5989 | ||
5990 | Returns the number of keyboard chars read, or -1 meaning | |
5991 | this is a bad time to try to read input. */ | |
5992 | ||
5993 | static int | |
5994 | read_avail_input (expected) | |
5995 | int expected; | |
5996 | { | |
5997 | struct input_event buf[KBD_BUFFER_SIZE]; | |
5998 | register int i; | |
5999 | int nread; | |
6000 | ||
6001 | if (read_socket_hook) | |
6002 | /* No need for FIONREAD or fcntl; just say don't wait. */ | |
33e19c6e | 6003 | nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected); |
284f4730 JB |
6004 | else |
6005 | { | |
17270835 RS |
6006 | /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than |
6007 | the kbd_buffer can really hold. That may prevent loss | |
6008 | of characters on some systems when input is stuffed at us. */ | |
6009 | unsigned char cbuf[KBD_BUFFER_SIZE - 1]; | |
58788063 | 6010 | int n_to_read; |
284f4730 | 6011 | |
58788063 | 6012 | /* Determine how many characters we should *try* to read. */ |
bc536d84 RS |
6013 | #ifdef WINDOWSNT |
6014 | return 0; | |
6015 | #else /* not WINDOWSNT */ | |
80e4aa30 | 6016 | #ifdef MSDOS |
58788063 RS |
6017 | n_to_read = dos_keysns (); |
6018 | if (n_to_read == 0) | |
6019 | return 0; | |
c3a2738c | 6020 | #else /* not MSDOS */ |
284f4730 JB |
6021 | #ifdef FIONREAD |
6022 | /* Find out how much input is available. */ | |
437f6112 | 6023 | if (ioctl (input_fd, FIONREAD, &n_to_read) < 0) |
284f4730 JB |
6024 | /* Formerly simply reported no input, but that sometimes led to |
6025 | a failure of Emacs to terminate. | |
6026 | SIGHUP seems appropriate if we can't reach the terminal. */ | |
e4535288 RS |
6027 | /* ??? Is it really right to send the signal just to this process |
6028 | rather than to the whole process group? | |
6029 | Perhaps on systems with FIONREAD Emacs is alone in its group. */ | |
284f4730 | 6030 | kill (getpid (), SIGHUP); |
58788063 | 6031 | if (n_to_read == 0) |
284f4730 | 6032 | return 0; |
58788063 RS |
6033 | if (n_to_read > sizeof cbuf) |
6034 | n_to_read = sizeof cbuf; | |
284f4730 | 6035 | #else /* no FIONREAD */ |
0c04a67e | 6036 | #if defined (USG) || defined (DGUX) |
284f4730 | 6037 | /* Read some input if available, but don't wait. */ |
58788063 | 6038 | n_to_read = sizeof cbuf; |
437f6112 | 6039 | fcntl (input_fd, F_SETFL, O_NDELAY); |
284f4730 JB |
6040 | #else |
6041 | you lose; | |
6042 | #endif | |
6043 | #endif | |
80e4aa30 | 6044 | #endif /* not MSDOS */ |
bc536d84 | 6045 | #endif /* not WINDOWSNT */ |
284f4730 | 6046 | |
58788063 RS |
6047 | /* Now read; for one reason or another, this will not block. |
6048 | NREAD is set to the number of chars read. */ | |
9134775b | 6049 | do |
284f4730 | 6050 | { |
80e4aa30 | 6051 | #ifdef MSDOS |
0c04a67e | 6052 | cbuf[0] = dos_keyread (); |
80e4aa30 RS |
6053 | nread = 1; |
6054 | #else | |
68c45bf0 | 6055 | nread = emacs_read (input_fd, cbuf, n_to_read); |
80e4aa30 | 6056 | #endif |
49854566 RS |
6057 | /* POSIX infers that processes which are not in the session leader's |
6058 | process group won't get SIGHUP's at logout time. BSDI adheres to | |
e8886a1d | 6059 | this part standard and returns -1 from read (0) with errno==EIO |
49854566 RS |
6060 | when the control tty is taken away. |
6061 | Jeffrey Honig <jch@bsdi.com> says this is generally safe. */ | |
6062 | if (nread == -1 && errno == EIO) | |
6063 | kill (0, SIGHUP); | |
762f2b92 | 6064 | #if defined (AIX) && (! defined (aix386) && defined (_BSD)) |
284f4730 JB |
6065 | /* The kernel sometimes fails to deliver SIGHUP for ptys. |
6066 | This looks incorrect, but it isn't, because _BSD causes | |
6067 | O_NDELAY to be defined in fcntl.h as O_NONBLOCK, | |
6068 | and that causes a value other than 0 when there is no input. */ | |
854f3a54 | 6069 | if (nread == 0) |
80e4aa30 | 6070 | kill (0, SIGHUP); |
284f4730 | 6071 | #endif |
9134775b | 6072 | } |
791587ee KH |
6073 | while ( |
6074 | /* We used to retry the read if it was interrupted. | |
6075 | But this does the wrong thing when O_NDELAY causes | |
6076 | an EAGAIN error. Does anybody know of a situation | |
6077 | where a retry is actually needed? */ | |
6078 | #if 0 | |
6079 | nread < 0 && (errno == EAGAIN | |
6aec06f5 | 6080 | #ifdef EFAULT |
9134775b | 6081 | || errno == EFAULT |
80e4aa30 | 6082 | #endif |
284f4730 | 6083 | #ifdef EBADSLT |
9134775b | 6084 | || errno == EBADSLT |
284f4730 | 6085 | #endif |
791587ee KH |
6086 | ) |
6087 | #else | |
6088 | 0 | |
6089 | #endif | |
6090 | ); | |
284f4730 JB |
6091 | |
6092 | #ifndef FIONREAD | |
02c2c53f | 6093 | #if defined (USG) || defined (DGUX) |
437f6112 | 6094 | fcntl (input_fd, F_SETFL, 0); |
02c2c53f | 6095 | #endif /* USG or DGUX */ |
284f4730 JB |
6096 | #endif /* no FIONREAD */ |
6097 | for (i = 0; i < nread; i++) | |
6098 | { | |
6099 | buf[i].kind = ascii_keystroke; | |
86e5706b | 6100 | buf[i].modifiers = 0; |
b04904fb | 6101 | if (meta_key == 1 && (cbuf[i] & 0x80)) |
86e5706b | 6102 | buf[i].modifiers = meta_modifier; |
b04904fb RS |
6103 | if (meta_key != 2) |
6104 | cbuf[i] &= ~0x80; | |
f3e59d5e KH |
6105 | |
6106 | buf[i].code = cbuf[i]; | |
788f89eb | 6107 | buf[i].frame_or_window = selected_frame; |
da8f7368 | 6108 | buf[i].arg = Qnil; |
284f4730 JB |
6109 | } |
6110 | } | |
6111 | ||
6112 | /* Scan the chars for C-g and store them in kbd_buffer. */ | |
6113 | for (i = 0; i < nread; i++) | |
6114 | { | |
6115 | kbd_buffer_store_event (&buf[i]); | |
6116 | /* Don't look at input that follows a C-g too closely. | |
6117 | This reduces lossage due to autorepeat on C-g. */ | |
6118 | if (buf[i].kind == ascii_keystroke | |
9343ab07 | 6119 | && buf[i].code == quit_char) |
284f4730 JB |
6120 | break; |
6121 | } | |
6122 | ||
6123 | return nread; | |
6124 | } | |
6125 | #endif /* not VMS */ | |
6126 | \f | |
6127 | #ifdef SIGIO /* for entire page */ | |
6128 | /* Note SIGIO has been undef'd if FIONREAD is missing. */ | |
6129 | ||
2ce30ea2 | 6130 | SIGTYPE |
284f4730 JB |
6131 | input_available_signal (signo) |
6132 | int signo; | |
6133 | { | |
6134 | /* Must preserve main program's value of errno. */ | |
6135 | int old_errno = errno; | |
6136 | #ifdef BSD4_1 | |
6137 | extern int select_alarmed; | |
6138 | #endif | |
6139 | ||
5970a8cb | 6140 | #if defined (USG) && !defined (POSIX_SIGNALS) |
284f4730 JB |
6141 | /* USG systems forget handlers when they are used; |
6142 | must reestablish each time */ | |
6143 | signal (signo, input_available_signal); | |
6144 | #endif /* USG */ | |
6145 | ||
6146 | #ifdef BSD4_1 | |
6147 | sigisheld (SIGIO); | |
6148 | #endif | |
6149 | ||
ffd56f97 JB |
6150 | if (input_available_clear_time) |
6151 | EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0); | |
284f4730 JB |
6152 | |
6153 | while (1) | |
6154 | { | |
6155 | int nread; | |
6156 | nread = read_avail_input (1); | |
6157 | /* -1 means it's not ok to read the input now. | |
6158 | UNBLOCK_INPUT will read it later; now, avoid infinite loop. | |
6159 | 0 means there was no keyboard input available. */ | |
6160 | if (nread <= 0) | |
6161 | break; | |
6162 | ||
6163 | #ifdef BSD4_1 | |
6164 | select_alarmed = 1; /* Force the select emulator back to life */ | |
6165 | #endif | |
6166 | } | |
6167 | ||
6168 | #ifdef BSD4_1 | |
6169 | sigfree (); | |
6170 | #endif | |
6171 | errno = old_errno; | |
6172 | } | |
6173 | #endif /* SIGIO */ | |
ad163903 JB |
6174 | |
6175 | /* Send ourselves a SIGIO. | |
6176 | ||
6177 | This function exists so that the UNBLOCK_INPUT macro in | |
6178 | blockinput.h can have some way to take care of input we put off | |
6179 | dealing with, without assuming that every file which uses | |
6180 | UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */ | |
6181 | void | |
6182 | reinvoke_input_signal () | |
6183 | { | |
df0f2ba1 | 6184 | #ifdef SIGIO |
87dd9b9b | 6185 | kill (getpid (), SIGIO); |
ad163903 JB |
6186 | #endif |
6187 | } | |
6188 | ||
6189 | ||
284f4730 JB |
6190 | \f |
6191 | /* Return the prompt-string of a sparse keymap. | |
6192 | This is the first element which is a string. | |
6193 | Return nil if there is none. */ | |
6194 | ||
6195 | Lisp_Object | |
6196 | map_prompt (map) | |
6197 | Lisp_Object map; | |
6198 | { | |
6199 | while (CONSP (map)) | |
6200 | { | |
6201 | register Lisp_Object tem; | |
6202 | tem = Fcar (map); | |
8c18cbfb | 6203 | if (STRINGP (tem)) |
284f4730 JB |
6204 | return tem; |
6205 | map = Fcdr (map); | |
6206 | } | |
6207 | return Qnil; | |
6208 | } | |
6209 | ||
25126faa GM |
6210 | static void menu_bar_item P_ ((Lisp_Object, Lisp_Object)); |
6211 | static void menu_bar_one_keymap P_ ((Lisp_Object)); | |
b7c49376 RS |
6212 | |
6213 | /* These variables hold the vector under construction within | |
6214 | menu_bar_items and its subroutines, and the current index | |
6215 | for storing into that vector. */ | |
6216 | static Lisp_Object menu_bar_items_vector; | |
9343ab07 | 6217 | static int menu_bar_items_index; |
5ec75a55 | 6218 | |
b7c49376 RS |
6219 | /* Return a vector of menu items for a menu bar, appropriate |
6220 | to the current buffer. Each item has three elements in the vector: | |
f5e09c8b | 6221 | KEY STRING MAPLIST. |
b7c49376 RS |
6222 | |
6223 | OLD is an old vector we can optionally reuse, or nil. */ | |
5ec75a55 RS |
6224 | |
6225 | Lisp_Object | |
b7c49376 RS |
6226 | menu_bar_items (old) |
6227 | Lisp_Object old; | |
5ec75a55 RS |
6228 | { |
6229 | /* The number of keymaps we're scanning right now, and the number of | |
6230 | keymaps we have allocated space for. */ | |
6231 | int nmaps; | |
6232 | ||
6233 | /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1] | |
6234 | in the current keymaps, or nil where it is not a prefix. */ | |
6235 | Lisp_Object *maps; | |
6236 | ||
9f9c0e27 | 6237 | Lisp_Object def, tem, tail; |
5ec75a55 RS |
6238 | |
6239 | Lisp_Object result; | |
6240 | ||
6241 | int mapno; | |
47d319aa | 6242 | Lisp_Object oquit; |
5ec75a55 | 6243 | |
b7c49376 RS |
6244 | int i; |
6245 | ||
6246 | struct gcpro gcpro1; | |
6247 | ||
db60d856 JB |
6248 | /* In order to build the menus, we need to call the keymap |
6249 | accessors. They all call QUIT. But this function is called | |
6250 | during redisplay, during which a quit is fatal. So inhibit | |
47d319aa RS |
6251 | quitting while building the menus. |
6252 | We do this instead of specbind because (1) errors will clear it anyway | |
6253 | and (2) this avoids risk of specpdl overflow. */ | |
6254 | oquit = Vinhibit_quit; | |
df0f2ba1 | 6255 | Vinhibit_quit = Qt; |
db60d856 | 6256 | |
b7c49376 RS |
6257 | if (!NILP (old)) |
6258 | menu_bar_items_vector = old; | |
6259 | else | |
6260 | menu_bar_items_vector = Fmake_vector (make_number (24), Qnil); | |
6261 | menu_bar_items_index = 0; | |
6262 | ||
6263 | GCPRO1 (menu_bar_items_vector); | |
6264 | ||
5ec75a55 RS |
6265 | /* Build our list of keymaps. |
6266 | If we recognize a function key and replace its escape sequence in | |
6267 | keybuf with its symbol, or if the sequence starts with a mouse | |
6268 | click and we need to switch buffers, we jump back here to rebuild | |
6269 | the initial keymaps from the current buffer. */ | |
df0f2ba1 | 6270 | { |
5ec75a55 RS |
6271 | Lisp_Object *tmaps; |
6272 | ||
217258d5 | 6273 | /* Should overriding-terminal-local-map and overriding-local-map apply? */ |
d0a49716 | 6274 | if (!NILP (Voverriding_local_map_menu_flag)) |
9dd3131c | 6275 | { |
217258d5 KH |
6276 | /* Yes, use them (if non-nil) as well as the global map. */ |
6277 | maps = (Lisp_Object *) alloca (3 * sizeof (maps[0])); | |
6278 | nmaps = 0; | |
6279 | if (!NILP (current_kboard->Voverriding_terminal_local_map)) | |
6280 | maps[nmaps++] = current_kboard->Voverriding_terminal_local_map; | |
6281 | if (!NILP (Voverriding_local_map)) | |
6282 | maps[nmaps++] = Voverriding_local_map; | |
9dd3131c RS |
6283 | } |
6284 | else | |
6285 | { | |
30690496 DL |
6286 | /* No, so use major and minor mode keymaps and keymap property. */ |
6287 | int extra_maps = 2; | |
6288 | Lisp_Object map = get_local_map (PT, current_buffer, keymap); | |
6289 | if (!NILP (map)) | |
6290 | extra_maps = 3; | |
217258d5 | 6291 | nmaps = current_minor_maps (NULL, &tmaps); |
30690496 DL |
6292 | maps = (Lisp_Object *) alloca ((nmaps + extra_maps) |
6293 | * sizeof (maps[0])); | |
217258d5 | 6294 | bcopy (tmaps, maps, nmaps * sizeof (maps[0])); |
30690496 | 6295 | if (!NILP (map)) |
9f83cf0b | 6296 | maps[nmaps++] = map; |
30690496 | 6297 | maps[nmaps++] = get_local_map (PT, current_buffer, local_map); |
9dd3131c | 6298 | } |
217258d5 | 6299 | maps[nmaps++] = current_global_map; |
5ec75a55 RS |
6300 | } |
6301 | ||
6302 | /* Look up in each map the dummy prefix key `menu-bar'. */ | |
6303 | ||
6304 | result = Qnil; | |
6305 | ||
e58aa385 | 6306 | for (mapno = nmaps - 1; mapno >= 0; mapno--) |
25126faa GM |
6307 | if (!NILP (maps[mapno])) |
6308 | { | |
341a09cf SM |
6309 | def = get_keymap (access_keymap (maps[mapno], Qmenu_bar, 1, 0, 1), |
6310 | 0, 1); | |
02067692 | 6311 | if (CONSP (def)) |
25126faa GM |
6312 | menu_bar_one_keymap (def); |
6313 | } | |
5ec75a55 | 6314 | |
b7c49376 RS |
6315 | /* Move to the end those items that should be at the end. */ |
6316 | ||
7539e11f | 6317 | for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail)) |
9f9c0e27 | 6318 | { |
b7c49376 RS |
6319 | int i; |
6320 | int end = menu_bar_items_index; | |
6321 | ||
35b3402f | 6322 | for (i = 0; i < end; i += 4) |
7539e11f | 6323 | if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i])) |
b7c49376 | 6324 | { |
35b3402f | 6325 | Lisp_Object tem0, tem1, tem2, tem3; |
0301268e RS |
6326 | /* Move the item at index I to the end, |
6327 | shifting all the others forward. */ | |
6328 | tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0]; | |
6329 | tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1]; | |
6330 | tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2]; | |
35b3402f RS |
6331 | tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3]; |
6332 | if (end > i + 4) | |
6333 | bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4], | |
0301268e | 6334 | &XVECTOR (menu_bar_items_vector)->contents[i], |
35b3402f RS |
6335 | (end - i - 4) * sizeof (Lisp_Object)); |
6336 | XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0; | |
6337 | XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1; | |
6338 | XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2; | |
6339 | XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3; | |
0301268e | 6340 | break; |
b7c49376 RS |
6341 | } |
6342 | } | |
9f9c0e27 | 6343 | |
0c9071cd | 6344 | /* Add nil, nil, nil, nil at the end. */ |
b7c49376 | 6345 | i = menu_bar_items_index; |
35b3402f | 6346 | if (i + 4 > XVECTOR (menu_bar_items_vector)->size) |
b7c49376 RS |
6347 | { |
6348 | Lisp_Object tem; | |
b7c49376 RS |
6349 | tem = Fmake_vector (make_number (2 * i), Qnil); |
6350 | bcopy (XVECTOR (menu_bar_items_vector)->contents, | |
6351 | XVECTOR (tem)->contents, i * sizeof (Lisp_Object)); | |
6352 | menu_bar_items_vector = tem; | |
9f9c0e27 | 6353 | } |
b7c49376 RS |
6354 | /* Add this item. */ |
6355 | XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil; | |
6356 | XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil; | |
6357 | XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil; | |
35b3402f | 6358 | XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil; |
b7c49376 | 6359 | menu_bar_items_index = i; |
a73c5e29 | 6360 | |
47d319aa | 6361 | Vinhibit_quit = oquit; |
b7c49376 RS |
6362 | UNGCPRO; |
6363 | return menu_bar_items_vector; | |
5ec75a55 RS |
6364 | } |
6365 | \f | |
6366 | /* Scan one map KEYMAP, accumulating any menu items it defines | |
f5e09c8b | 6367 | in menu_bar_items_vector. */ |
5ec75a55 | 6368 | |
759860a6 RS |
6369 | static Lisp_Object menu_bar_one_keymap_changed_items; |
6370 | ||
b7c49376 RS |
6371 | static void |
6372 | menu_bar_one_keymap (keymap) | |
6373 | Lisp_Object keymap; | |
5ec75a55 | 6374 | { |
03cee6ae | 6375 | Lisp_Object tail, item; |
5ec75a55 | 6376 | |
759860a6 RS |
6377 | menu_bar_one_keymap_changed_items = Qnil; |
6378 | ||
5ec75a55 | 6379 | /* Loop over all keymap entries that have menu strings. */ |
7539e11f | 6380 | for (tail = keymap; CONSP (tail); tail = XCDR (tail)) |
5ec75a55 | 6381 | { |
7539e11f | 6382 | item = XCAR (tail); |
8c18cbfb | 6383 | if (CONSP (item)) |
7539e11f | 6384 | menu_bar_item (XCAR (item), XCDR (item)); |
8c18cbfb | 6385 | else if (VECTORP (item)) |
5ec75a55 RS |
6386 | { |
6387 | /* Loop over the char values represented in the vector. */ | |
6388 | int len = XVECTOR (item)->size; | |
6389 | int c; | |
6390 | for (c = 0; c < len; c++) | |
6391 | { | |
6392 | Lisp_Object character; | |
bb9e9bed | 6393 | XSETFASTINT (character, c); |
e8886a1d | 6394 | menu_bar_item (character, XVECTOR (item)->contents[c]); |
5ec75a55 RS |
6395 | } |
6396 | } | |
6397 | } | |
5ec75a55 RS |
6398 | } |
6399 | ||
f5e09c8b RS |
6400 | /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF. |
6401 | If there's already an item for KEY, add this DEF to it. */ | |
6402 | ||
e8886a1d RS |
6403 | Lisp_Object item_properties; |
6404 | ||
b7c49376 | 6405 | static void |
e8886a1d RS |
6406 | menu_bar_item (key, item) |
6407 | Lisp_Object key, item; | |
5ec75a55 | 6408 | { |
e8886a1d | 6409 | struct gcpro gcpro1; |
b7c49376 | 6410 | int i; |
759860a6 | 6411 | Lisp_Object tem; |
5ec75a55 | 6412 | |
e8886a1d | 6413 | if (EQ (item, Qundefined)) |
e58aa385 | 6414 | { |
f5e09c8b | 6415 | /* If a map has an explicit `undefined' as definition, |
e58aa385 | 6416 | discard any previously made menu bar item. */ |
b7c49376 | 6417 | |
35b3402f | 6418 | for (i = 0; i < menu_bar_items_index; i += 4) |
b7c49376 RS |
6419 | if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i])) |
6420 | { | |
35b3402f RS |
6421 | if (menu_bar_items_index > i + 4) |
6422 | bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4], | |
b7c49376 | 6423 | &XVECTOR (menu_bar_items_vector)->contents[i], |
35b3402f RS |
6424 | (menu_bar_items_index - i - 4) * sizeof (Lisp_Object)); |
6425 | menu_bar_items_index -= 4; | |
b7c49376 | 6426 | } |
e58aa385 RS |
6427 | } |
6428 | ||
759860a6 RS |
6429 | /* If this keymap has already contributed to this KEY, |
6430 | don't contribute to it a second time. */ | |
6431 | tem = Fmemq (key, menu_bar_one_keymap_changed_items); | |
9cd2ced7 | 6432 | if (!NILP (tem) || NILP (item)) |
759860a6 RS |
6433 | return; |
6434 | ||
6435 | menu_bar_one_keymap_changed_items | |
6436 | = Fcons (key, menu_bar_one_keymap_changed_items); | |
6437 | ||
9cd2ced7 SM |
6438 | /* We add to menu_bar_one_keymap_changed_items before doing the |
6439 | parse_menu_item, so that if it turns out it wasn't a menu item, | |
6440 | it still correctly hides any further menu item. */ | |
6441 | GCPRO1 (key); | |
6442 | i = parse_menu_item (item, 0, 1); | |
6443 | UNGCPRO; | |
6444 | if (!i) | |
6445 | return; | |
6446 | ||
e8886a1d RS |
6447 | item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF]; |
6448 | ||
f5e09c8b | 6449 | /* Find any existing item for this KEY. */ |
35b3402f | 6450 | for (i = 0; i < menu_bar_items_index; i += 4) |
b7c49376 RS |
6451 | if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i])) |
6452 | break; | |
6453 | ||
f5e09c8b | 6454 | /* If we did not find this KEY, add it at the end. */ |
b7c49376 RS |
6455 | if (i == menu_bar_items_index) |
6456 | { | |
6457 | /* If vector is too small, get a bigger one. */ | |
35b3402f | 6458 | if (i + 4 > XVECTOR (menu_bar_items_vector)->size) |
b7c49376 RS |
6459 | { |
6460 | Lisp_Object tem; | |
b7c49376 RS |
6461 | tem = Fmake_vector (make_number (2 * i), Qnil); |
6462 | bcopy (XVECTOR (menu_bar_items_vector)->contents, | |
6463 | XVECTOR (tem)->contents, i * sizeof (Lisp_Object)); | |
6464 | menu_bar_items_vector = tem; | |
6465 | } | |
e8886a1d | 6466 | |
b7c49376 RS |
6467 | /* Add this item. */ |
6468 | XVECTOR (menu_bar_items_vector)->contents[i++] = key; | |
e8886a1d RS |
6469 | XVECTOR (menu_bar_items_vector)->contents[i++] |
6470 | = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]; | |
6471 | XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil); | |
35b3402f | 6472 | XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0); |
b7c49376 RS |
6473 | menu_bar_items_index = i; |
6474 | } | |
e8886a1d | 6475 | /* We did find an item for this KEY. Add ITEM to its list of maps. */ |
f5e09c8b RS |
6476 | else |
6477 | { | |
6478 | Lisp_Object old; | |
6479 | old = XVECTOR (menu_bar_items_vector)->contents[i + 2]; | |
e8886a1d | 6480 | XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old); |
f5e09c8b | 6481 | } |
5ec75a55 RS |
6482 | } |
6483 | \f | |
e8886a1d RS |
6484 | /* This is used as the handler when calling menu_item_eval_property. */ |
6485 | static Lisp_Object | |
6486 | menu_item_eval_property_1 (arg) | |
6487 | Lisp_Object arg; | |
6488 | { | |
6489 | /* If we got a quit from within the menu computation, | |
6490 | quit all the way out of it. This takes care of C-] in the debugger. */ | |
7539e11f | 6491 | if (CONSP (arg) && EQ (XCAR (arg), Qquit)) |
e8886a1d RS |
6492 | Fsignal (Qquit, Qnil); |
6493 | ||
6494 | return Qnil; | |
6495 | } | |
6496 | ||
6497 | /* Evaluate an expression and return the result (or nil if something | |
6498 | went wrong). Used to evaluate dynamic parts of menu items. */ | |
7ee32cda | 6499 | Lisp_Object |
e8886a1d RS |
6500 | menu_item_eval_property (sexpr) |
6501 | Lisp_Object sexpr; | |
6502 | { | |
44e553a3 | 6503 | int count = specpdl_ptr - specpdl; |
e8886a1d | 6504 | Lisp_Object val; |
44e553a3 | 6505 | specbind (Qinhibit_redisplay, Qt); |
e8886a1d RS |
6506 | val = internal_condition_case_1 (Feval, sexpr, Qerror, |
6507 | menu_item_eval_property_1); | |
44e553a3 | 6508 | return unbind_to (count, val); |
e8886a1d RS |
6509 | } |
6510 | ||
6511 | /* This function parses a menu item and leaves the result in the | |
6512 | vector item_properties. | |
6513 | ITEM is a key binding, a possible menu item. | |
6514 | If NOTREAL is nonzero, only check for equivalent key bindings, don't | |
6515 | evaluate dynamic expressions in the menu item. | |
fd3613d7 | 6516 | INMENUBAR is > 0 when this is considered for an entry in a menu bar |
e8886a1d | 6517 | top level. |
fd3613d7 | 6518 | INMENUBAR is < 0 when this is considered for an entry in a keyboard menu. |
e8886a1d RS |
6519 | parse_menu_item returns true if the item is a menu item and false |
6520 | otherwise. */ | |
6521 | ||
6522 | int | |
6523 | parse_menu_item (item, notreal, inmenubar) | |
6524 | Lisp_Object item; | |
6525 | int notreal, inmenubar; | |
6526 | { | |
adc1d5c8 | 6527 | Lisp_Object def, tem, item_string, start; |
07ba902e RS |
6528 | Lisp_Object cachelist; |
6529 | Lisp_Object filter; | |
6530 | Lisp_Object keyhint; | |
e8886a1d | 6531 | int i; |
74c1de23 RS |
6532 | int newcache = 0; |
6533 | ||
07ba902e RS |
6534 | cachelist = Qnil; |
6535 | filter = Qnil; | |
6536 | keyhint = Qnil; | |
6537 | ||
e8886a1d RS |
6538 | if (!CONSP (item)) |
6539 | return 0; | |
6540 | ||
e8886a1d RS |
6541 | /* Create item_properties vector if necessary. */ |
6542 | if (NILP (item_properties)) | |
6543 | item_properties | |
6544 | = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil); | |
6545 | ||
6546 | /* Initialize optional entries. */ | |
6547 | for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++) | |
3626fb1a GM |
6548 | AREF (item_properties, i) = Qnil; |
6549 | AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt; | |
e8886a1d RS |
6550 | |
6551 | /* Save the item here to protect it from GC. */ | |
3626fb1a | 6552 | AREF (item_properties, ITEM_PROPERTY_ITEM) = item; |
e8886a1d | 6553 | |
7539e11f | 6554 | item_string = XCAR (item); |
e8886a1d RS |
6555 | |
6556 | start = item; | |
7539e11f | 6557 | item = XCDR (item); |
e8886a1d RS |
6558 | if (STRINGP (item_string)) |
6559 | { | |
6560 | /* Old format menu item. */ | |
3626fb1a | 6561 | AREF (item_properties, ITEM_PROPERTY_NAME) = item_string; |
e8886a1d RS |
6562 | |
6563 | /* Maybe help string. */ | |
7539e11f | 6564 | if (CONSP (item) && STRINGP (XCAR (item))) |
e8886a1d | 6565 | { |
3626fb1a | 6566 | AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item); |
e8886a1d | 6567 | start = item; |
7539e11f | 6568 | item = XCDR (item); |
e8886a1d RS |
6569 | } |
6570 | ||
31f84d03 | 6571 | /* Maybe key binding cache. */ |
7539e11f KR |
6572 | if (CONSP (item) && CONSP (XCAR (item)) |
6573 | && (NILP (XCAR (XCAR (item))) | |
6574 | || VECTORP (XCAR (XCAR (item))))) | |
e8886a1d | 6575 | { |
7539e11f KR |
6576 | cachelist = XCAR (item); |
6577 | item = XCDR (item); | |
e8886a1d RS |
6578 | } |
6579 | ||
6580 | /* This is the real definition--the function to run. */ | |
3626fb1a | 6581 | AREF (item_properties, ITEM_PROPERTY_DEF) = item; |
e8886a1d RS |
6582 | |
6583 | /* Get enable property, if any. */ | |
6584 | if (SYMBOLP (item)) | |
6585 | { | |
6586 | tem = Fget (item, Qmenu_enable); | |
6587 | if (!NILP (tem)) | |
3626fb1a | 6588 | AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem; |
e8886a1d RS |
6589 | } |
6590 | } | |
6591 | else if (EQ (item_string, Qmenu_item) && CONSP (item)) | |
6592 | { | |
6593 | /* New format menu item. */ | |
3626fb1a | 6594 | AREF (item_properties, ITEM_PROPERTY_NAME) = XCAR (item); |
7539e11f | 6595 | start = XCDR (item); |
e8886a1d RS |
6596 | if (CONSP (start)) |
6597 | { | |
6598 | /* We have a real binding. */ | |
3626fb1a | 6599 | AREF (item_properties, ITEM_PROPERTY_DEF) = XCAR (start); |
e8886a1d | 6600 | |
7539e11f | 6601 | item = XCDR (start); |
e8886a1d | 6602 | /* Is there a cache list with key equivalences. */ |
7539e11f | 6603 | if (CONSP (item) && CONSP (XCAR (item))) |
e8886a1d | 6604 | { |
7539e11f KR |
6605 | cachelist = XCAR (item); |
6606 | item = XCDR (item); | |
e8886a1d RS |
6607 | } |
6608 | ||
6609 | /* Parse properties. */ | |
7539e11f | 6610 | while (CONSP (item) && CONSP (XCDR (item))) |
e8886a1d | 6611 | { |
7539e11f KR |
6612 | tem = XCAR (item); |
6613 | item = XCDR (item); | |
e8886a1d RS |
6614 | |
6615 | if (EQ (tem, QCenable)) | |
3626fb1a | 6616 | AREF (item_properties, ITEM_PROPERTY_ENABLE) = XCAR (item); |
e8886a1d RS |
6617 | else if (EQ (tem, QCvisible) && !notreal) |
6618 | { | |
6619 | /* If got a visible property and that evaluates to nil | |
6620 | then ignore this item. */ | |
7539e11f | 6621 | tem = menu_item_eval_property (XCAR (item)); |
e8886a1d | 6622 | if (NILP (tem)) |
adc1d5c8 | 6623 | return 0; |
e8886a1d RS |
6624 | } |
6625 | else if (EQ (tem, QChelp)) | |
3626fb1a | 6626 | AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item); |
e8886a1d | 6627 | else if (EQ (tem, QCfilter)) |
74c1de23 RS |
6628 | filter = item; |
6629 | else if (EQ (tem, QCkey_sequence)) | |
6630 | { | |
7539e11f | 6631 | tem = XCAR (item); |
74c1de23 RS |
6632 | if (NILP (cachelist) |
6633 | && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem))) | |
6634 | /* Be GC protected. Set keyhint to item instead of tem. */ | |
6635 | keyhint = item; | |
6636 | } | |
6637 | else if (EQ (tem, QCkeys)) | |
6638 | { | |
7539e11f | 6639 | tem = XCAR (item); |
03cee6ae | 6640 | if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist))) |
3626fb1a | 6641 | AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem; |
74c1de23 | 6642 | } |
7539e11f | 6643 | else if (EQ (tem, QCbutton) && CONSP (XCAR (item))) |
e8886a1d | 6644 | { |
74c1de23 | 6645 | Lisp_Object type; |
7539e11f KR |
6646 | tem = XCAR (item); |
6647 | type = XCAR (tem); | |
e8886a1d RS |
6648 | if (EQ (type, QCtoggle) || EQ (type, QCradio)) |
6649 | { | |
3626fb1a | 6650 | AREF (item_properties, ITEM_PROPERTY_SELECTED) |
7539e11f | 6651 | = XCDR (tem); |
3626fb1a | 6652 | AREF (item_properties, ITEM_PROPERTY_TYPE) |
e8886a1d RS |
6653 | = type; |
6654 | } | |
6655 | } | |
7539e11f | 6656 | item = XCDR (item); |
e8886a1d RS |
6657 | } |
6658 | } | |
6659 | else if (inmenubar || !NILP (start)) | |
adc1d5c8 | 6660 | return 0; |
e8886a1d RS |
6661 | } |
6662 | else | |
adc1d5c8 | 6663 | return 0; /* not a menu item */ |
e8886a1d RS |
6664 | |
6665 | /* If item string is not a string, evaluate it to get string. | |
6666 | If we don't get a string, skip this item. */ | |
3626fb1a | 6667 | item_string = AREF (item_properties, ITEM_PROPERTY_NAME); |
e8886a1d RS |
6668 | if (!(STRINGP (item_string) || notreal)) |
6669 | { | |
6670 | item_string = menu_item_eval_property (item_string); | |
6671 | if (!STRINGP (item_string)) | |
adc1d5c8 | 6672 | return 0; |
3626fb1a | 6673 | AREF (item_properties, ITEM_PROPERTY_NAME) = item_string; |
e8886a1d RS |
6674 | } |
6675 | ||
6676 | /* If got a filter apply it on definition. */ | |
3626fb1a | 6677 | def = AREF (item_properties, ITEM_PROPERTY_DEF); |
e8886a1d RS |
6678 | if (!NILP (filter)) |
6679 | { | |
7539e11f | 6680 | def = menu_item_eval_property (list2 (XCAR (filter), |
c5c5a6f8 RS |
6681 | list2 (Qquote, def))); |
6682 | ||
3626fb1a | 6683 | AREF (item_properties, ITEM_PROPERTY_DEF) = def; |
e8886a1d RS |
6684 | } |
6685 | ||
6686 | /* If we got no definition, this item is just unselectable text which | |
74c1de23 | 6687 | is OK in a submenu but not in the menubar. */ |
e8886a1d | 6688 | if (NILP (def)) |
adc1d5c8 | 6689 | return (inmenubar ? 0 : 1); |
e8886a1d RS |
6690 | |
6691 | /* Enable or disable selection of item. */ | |
3626fb1a | 6692 | tem = AREF (item_properties, ITEM_PROPERTY_ENABLE); |
e8886a1d RS |
6693 | if (!EQ (tem, Qt)) |
6694 | { | |
6695 | if (notreal) | |
6696 | tem = Qt; | |
6697 | else | |
6698 | tem = menu_item_eval_property (tem); | |
6699 | if (inmenubar && NILP (tem)) | |
adc1d5c8 | 6700 | return 0; /* Ignore disabled items in menu bar. */ |
3626fb1a | 6701 | AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem; |
e8886a1d RS |
6702 | } |
6703 | ||
6704 | /* See if this is a separate pane or a submenu. */ | |
3626fb1a | 6705 | def = AREF (item_properties, ITEM_PROPERTY_DEF); |
02067692 | 6706 | tem = get_keymap (def, 0, 1); |
9ac425d1 | 6707 | /* For a subkeymap, just record its details and exit. */ |
02067692 | 6708 | if (CONSP (tem)) |
e8886a1d | 6709 | { |
3626fb1a GM |
6710 | AREF (item_properties, ITEM_PROPERTY_MAP) = tem; |
6711 | AREF (item_properties, ITEM_PROPERTY_DEF) = tem; | |
e8886a1d RS |
6712 | return 1; |
6713 | } | |
3626fb1a | 6714 | |
9ac425d1 RS |
6715 | /* At the top level in the menu bar, do likewise for commands also. |
6716 | The menu bar does not display equivalent key bindings anyway. | |
6717 | ITEM_PROPERTY_DEF is already set up properly. */ | |
6718 | if (inmenubar > 0) | |
6719 | return 1; | |
e8886a1d RS |
6720 | |
6721 | /* This is a command. See if there is an equivalent key binding. */ | |
6722 | if (NILP (cachelist)) | |
6723 | { | |
74c1de23 | 6724 | /* We have to create a cachelist. */ |
e8886a1d | 6725 | CHECK_IMPURE (start); |
7539e11f KR |
6726 | XCDR (start) = Fcons (Fcons (Qnil, Qnil), XCDR (start)); |
6727 | cachelist = XCAR (XCDR (start)); | |
74c1de23 | 6728 | newcache = 1; |
3626fb1a | 6729 | tem = AREF (item_properties, ITEM_PROPERTY_KEYEQ); |
74c1de23 RS |
6730 | if (!NILP (keyhint)) |
6731 | { | |
7539e11f | 6732 | XCAR (cachelist) = XCAR (keyhint); |
74c1de23 RS |
6733 | newcache = 0; |
6734 | } | |
6735 | else if (STRINGP (tem)) | |
6736 | { | |
7539e11f KR |
6737 | XCDR (cachelist) = Fsubstitute_command_keys (tem); |
6738 | XCAR (cachelist) = Qt; | |
74c1de23 RS |
6739 | } |
6740 | } | |
3626fb1a | 6741 | |
7539e11f | 6742 | tem = XCAR (cachelist); |
74c1de23 RS |
6743 | if (!EQ (tem, Qt)) |
6744 | { | |
6745 | int chkcache = 0; | |
6746 | Lisp_Object prefix; | |
6747 | ||
6748 | if (!NILP (tem)) | |
6749 | tem = Fkey_binding (tem, Qnil); | |
6750 | ||
3626fb1a | 6751 | prefix = AREF (item_properties, ITEM_PROPERTY_KEYEQ); |
74c1de23 RS |
6752 | if (CONSP (prefix)) |
6753 | { | |
7539e11f KR |
6754 | def = XCAR (prefix); |
6755 | prefix = XCDR (prefix); | |
74c1de23 | 6756 | } |
e8886a1d | 6757 | else |
3626fb1a | 6758 | def = AREF (item_properties, ITEM_PROPERTY_DEF); |
74c1de23 | 6759 | |
3626fb1a GM |
6760 | if (!update_menu_bindings) |
6761 | chkcache = 0; | |
6762 | else if (NILP (XCAR (cachelist))) /* Have no saved key. */ | |
74c1de23 RS |
6763 | { |
6764 | if (newcache /* Always check first time. */ | |
6765 | /* Should we check everything when precomputing key | |
6766 | bindings? */ | |
74c1de23 RS |
6767 | /* If something had no key binding before, don't recheck it |
6768 | because that is too slow--except if we have a list of | |
6769 | rebound commands in Vdefine_key_rebound_commands, do | |
6770 | recheck any command that appears in that list. */ | |
6771 | || (CONSP (Vdefine_key_rebound_commands) | |
6772 | && !NILP (Fmemq (def, Vdefine_key_rebound_commands)))) | |
6773 | chkcache = 1; | |
6774 | } | |
6775 | /* We had a saved key. Is it still bound to the command? */ | |
6776 | else if (NILP (tem) | |
03cee6ae GM |
6777 | || (!EQ (tem, def) |
6778 | /* If the command is an alias for another | |
6779 | (such as lmenu.el set it up), check if the | |
6780 | original command matches the cached command. */ | |
6781 | && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function)))) | |
74c1de23 RS |
6782 | chkcache = 1; /* Need to recompute key binding. */ |
6783 | ||
6784 | if (chkcache) | |
6785 | { | |
6786 | /* Recompute equivalent key binding. If the command is an alias | |
6787 | for another (such as lmenu.el set it up), see if the original | |
6788 | command name has equivalent keys. Otherwise look up the | |
6789 | specified command itself. We don't try both, because that | |
6790 | makes lmenu menus slow. */ | |
3626fb1a GM |
6791 | if (SYMBOLP (def) |
6792 | && SYMBOLP (XSYMBOL (def)->function) | |
74c1de23 RS |
6793 | && ! NILP (Fget (def, Qmenu_alias))) |
6794 | def = XSYMBOL (def)->function; | |
6795 | tem = Fwhere_is_internal (def, Qnil, Qt, Qnil); | |
7539e11f | 6796 | XCAR (cachelist) = tem; |
74c1de23 RS |
6797 | if (NILP (tem)) |
6798 | { | |
7539e11f | 6799 | XCDR (cachelist) = Qnil; |
74c1de23 RS |
6800 | chkcache = 0; |
6801 | } | |
6802 | } | |
7539e11f | 6803 | else if (!NILP (keyhint) && !NILP (XCAR (cachelist))) |
74c1de23 | 6804 | { |
7539e11f | 6805 | tem = XCAR (cachelist); |
74c1de23 RS |
6806 | chkcache = 1; |
6807 | } | |
6808 | ||
6809 | newcache = chkcache; | |
6810 | if (chkcache) | |
6811 | { | |
6812 | tem = Fkey_description (tem); | |
6813 | if (CONSP (prefix)) | |
6814 | { | |
7539e11f KR |
6815 | if (STRINGP (XCAR (prefix))) |
6816 | tem = concat2 (XCAR (prefix), tem); | |
6817 | if (STRINGP (XCDR (prefix))) | |
6818 | tem = concat2 (tem, XCDR (prefix)); | |
74c1de23 | 6819 | } |
7539e11f | 6820 | XCDR (cachelist) = tem; |
74c1de23 RS |
6821 | } |
6822 | } | |
6823 | ||
7539e11f | 6824 | tem = XCDR (cachelist); |
74c1de23 | 6825 | if (newcache && !NILP (tem)) |
e8886a1d | 6826 | { |
74c1de23 | 6827 | tem = concat3 (build_string (" ("), tem, build_string (")")); |
7539e11f | 6828 | XCDR (cachelist) = tem; |
e8886a1d RS |
6829 | } |
6830 | ||
6831 | /* If we only want to precompute equivalent key bindings, stop here. */ | |
6832 | if (notreal) | |
adc1d5c8 | 6833 | return 1; |
e8886a1d RS |
6834 | |
6835 | /* If we have an equivalent key binding, use that. */ | |
3626fb1a | 6836 | AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem; |
adc1d5c8 RS |
6837 | |
6838 | /* Include this when menu help is implemented. | |
6839 | tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]; | |
6840 | if (!(NILP (tem) || STRINGP (tem))) | |
6841 | { | |
6842 | tem = menu_item_eval_property (tem); | |
6843 | if (!STRINGP (tem)) | |
6844 | tem = Qnil; | |
6845 | XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem; | |
6846 | } | |
e8886a1d RS |
6847 | */ |
6848 | ||
6849 | /* Handle radio buttons or toggle boxes. */ | |
3626fb1a | 6850 | tem = AREF (item_properties, ITEM_PROPERTY_SELECTED); |
e8886a1d | 6851 | if (!NILP (tem)) |
3626fb1a | 6852 | AREF (item_properties, ITEM_PROPERTY_SELECTED) |
e8886a1d RS |
6853 | = menu_item_eval_property (tem); |
6854 | ||
e8886a1d RS |
6855 | return 1; |
6856 | } | |
7ee32cda GM |
6857 | |
6858 | ||
6859 | \f | |
6860 | /*********************************************************************** | |
6861 | Tool-bars | |
6862 | ***********************************************************************/ | |
6863 | ||
9ea173e8 GM |
6864 | /* A vector holding tool bar items while they are parsed in function |
6865 | tool_bar_items runs Each item occupies TOOL_BAR_ITEM_NSCLOTS elements | |
6866 | in the vector. */ | |
7ee32cda | 6867 | |
9ea173e8 | 6868 | static Lisp_Object tool_bar_items_vector; |
7ee32cda | 6869 | |
9ea173e8 GM |
6870 | /* A vector holding the result of parse_tool_bar_item. Layout is like |
6871 | the one for a single item in tool_bar_items_vector. */ | |
7ee32cda | 6872 | |
9ea173e8 | 6873 | static Lisp_Object tool_bar_item_properties; |
7ee32cda | 6874 | |
9ea173e8 | 6875 | /* Next free index in tool_bar_items_vector. */ |
7ee32cda | 6876 | |
9ea173e8 | 6877 | static int ntool_bar_items; |
7ee32cda | 6878 | |
9ea173e8 | 6879 | /* The symbols `tool-bar', and `:image'. */ |
7ee32cda | 6880 | |
9ea173e8 | 6881 | extern Lisp_Object Qtool_bar; |
7ee32cda GM |
6882 | Lisp_Object QCimage; |
6883 | ||
6884 | /* Function prototypes. */ | |
6885 | ||
9ea173e8 GM |
6886 | static void init_tool_bar_items P_ ((Lisp_Object)); |
6887 | static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object)); | |
6888 | static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object)); | |
6889 | static void append_tool_bar_item P_ ((void)); | |
7ee32cda GM |
6890 | |
6891 | ||
9ea173e8 | 6892 | /* Return a vector of tool bar items for keymaps currently in effect. |
7ee32cda | 6893 | Reuse vector REUSE if non-nil. Return in *NITEMS the number of |
9ea173e8 | 6894 | tool bar items found. */ |
7ee32cda GM |
6895 | |
6896 | Lisp_Object | |
9ea173e8 | 6897 | tool_bar_items (reuse, nitems) |
7ee32cda GM |
6898 | Lisp_Object reuse; |
6899 | int *nitems; | |
6900 | { | |
6901 | Lisp_Object *maps; | |
6902 | int nmaps, i; | |
6903 | Lisp_Object oquit; | |
6904 | Lisp_Object *tmaps; | |
6905 | extern Lisp_Object Voverriding_local_map_menu_flag; | |
6906 | extern Lisp_Object Voverriding_local_map; | |
6907 | ||
6908 | *nitems = 0; | |
6909 | ||
6910 | /* In order to build the menus, we need to call the keymap | |
6911 | accessors. They all call QUIT. But this function is called | |
6912 | during redisplay, during which a quit is fatal. So inhibit | |
6913 | quitting while building the menus. We do this instead of | |
6914 | specbind because (1) errors will clear it anyway and (2) this | |
6915 | avoids risk of specpdl overflow. */ | |
6916 | oquit = Vinhibit_quit; | |
6917 | Vinhibit_quit = Qt; | |
6918 | ||
9ea173e8 GM |
6919 | /* Initialize tool_bar_items_vector and protect it from GC. */ |
6920 | init_tool_bar_items (reuse); | |
7ee32cda GM |
6921 | |
6922 | /* Build list of keymaps in maps. Set nmaps to the number of maps | |
6923 | to process. */ | |
6924 | ||
6925 | /* Should overriding-terminal-local-map and overriding-local-map apply? */ | |
6926 | if (!NILP (Voverriding_local_map_menu_flag)) | |
6927 | { | |
6928 | /* Yes, use them (if non-nil) as well as the global map. */ | |
6929 | maps = (Lisp_Object *) alloca (3 * sizeof (maps[0])); | |
6930 | nmaps = 0; | |
6931 | if (!NILP (current_kboard->Voverriding_terminal_local_map)) | |
6932 | maps[nmaps++] = current_kboard->Voverriding_terminal_local_map; | |
6933 | if (!NILP (Voverriding_local_map)) | |
6934 | maps[nmaps++] = Voverriding_local_map; | |
6935 | } | |
6936 | else | |
6937 | { | |
30690496 DL |
6938 | /* No, so use major and minor mode keymaps and keymap property. */ |
6939 | int extra_maps = 2; | |
6940 | Lisp_Object map = get_local_map (PT, current_buffer, keymap); | |
6941 | if (!NILP (map)) | |
6942 | extra_maps = 3; | |
7ee32cda | 6943 | nmaps = current_minor_maps (NULL, &tmaps); |
30690496 DL |
6944 | maps = (Lisp_Object *) alloca ((nmaps + extra_maps) |
6945 | * sizeof (maps[0])); | |
7ee32cda | 6946 | bcopy (tmaps, maps, nmaps * sizeof (maps[0])); |
30690496 | 6947 | if (!NILP (map)) |
9f83cf0b | 6948 | maps[nmaps++] = map; |
30690496 | 6949 | maps[nmaps++] = get_local_map (PT, current_buffer, local_map); |
7ee32cda GM |
6950 | } |
6951 | ||
6952 | /* Add global keymap at the end. */ | |
6953 | maps[nmaps++] = current_global_map; | |
6954 | ||
6955 | /* Process maps in reverse order and look up in each map the prefix | |
9ea173e8 | 6956 | key `tool-bar'. */ |
7ee32cda GM |
6957 | for (i = nmaps - 1; i >= 0; --i) |
6958 | if (!NILP (maps[i])) | |
6959 | { | |
6960 | Lisp_Object keymap; | |
db785038 | 6961 | |
341a09cf | 6962 | keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1); |
02067692 | 6963 | if (CONSP (keymap)) |
7ee32cda GM |
6964 | { |
6965 | Lisp_Object tail; | |
6966 | ||
6967 | /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */ | |
7539e11f | 6968 | for (tail = keymap; CONSP (tail); tail = XCDR (tail)) |
7ee32cda GM |
6969 | { |
6970 | Lisp_Object keydef = XCAR (tail); | |
6971 | if (CONSP (keydef)) | |
9ea173e8 | 6972 | process_tool_bar_item (XCAR (keydef), XCDR (keydef)); |
7ee32cda GM |
6973 | } |
6974 | } | |
6975 | } | |
6976 | ||
6977 | Vinhibit_quit = oquit; | |
9ea173e8 GM |
6978 | *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS; |
6979 | return tool_bar_items_vector; | |
7ee32cda GM |
6980 | } |
6981 | ||
6982 | ||
6983 | /* Process the definition of KEY which is DEF. */ | |
6984 | ||
6985 | static void | |
9ea173e8 | 6986 | process_tool_bar_item (key, def) |
7ee32cda GM |
6987 | Lisp_Object key, def; |
6988 | { | |
6989 | int i; | |
6990 | extern Lisp_Object Qundefined; | |
6991 | struct gcpro gcpro1, gcpro2; | |
6992 | ||
9ea173e8 | 6993 | /* Protect KEY and DEF from GC because parse_tool_bar_item may call |
7ee32cda GM |
6994 | eval. */ |
6995 | GCPRO2 (key, def); | |
6996 | ||
6997 | if (EQ (def, Qundefined)) | |
6998 | { | |
6999 | /* If a map has an explicit `undefined' as definition, | |
7000 | discard any previously made item. */ | |
9ea173e8 | 7001 | for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS) |
7ee32cda | 7002 | { |
9ea173e8 | 7003 | Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i; |
7ee32cda | 7004 | |
9ea173e8 | 7005 | if (EQ (key, v[TOOL_BAR_ITEM_KEY])) |
7ee32cda | 7006 | { |
9ea173e8 GM |
7007 | if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS) |
7008 | bcopy (v + TOOL_BAR_ITEM_NSLOTS, v, | |
7009 | ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS) | |
7ee32cda | 7010 | * sizeof (Lisp_Object))); |
9ea173e8 | 7011 | ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS; |
7ee32cda GM |
7012 | break; |
7013 | } | |
7014 | } | |
7015 | } | |
9ea173e8 GM |
7016 | else if (parse_tool_bar_item (key, def)) |
7017 | /* Append a new tool bar item to tool_bar_items_vector. Accept | |
7ee32cda | 7018 | more than one definition for the same key. */ |
9ea173e8 | 7019 | append_tool_bar_item (); |
7ee32cda GM |
7020 | |
7021 | UNGCPRO; | |
7022 | } | |
7023 | ||
7024 | ||
9ea173e8 GM |
7025 | /* Parse a tool bar item specification ITEM for key KEY and return the |
7026 | result in tool_bar_item_properties. Value is zero if ITEM is | |
7ee32cda GM |
7027 | invalid. |
7028 | ||
7029 | ITEM is a list `(menu-item CAPTION BINDING PROPS...)'. | |
7030 | ||
7031 | CAPTION is the caption of the item, If it's not a string, it is | |
7032 | evaluated to get a string. | |
7033 | ||
9ea173e8 | 7034 | BINDING is the tool bar item's binding. Tool-bar items with keymaps |
7ee32cda GM |
7035 | as binding are currently ignored. |
7036 | ||
7037 | The following properties are recognized: | |
7038 | ||
7039 | - `:enable FORM'. | |
7040 | ||
9ea173e8 GM |
7041 | FORM is evaluated and specifies whether the tool bar item is |
7042 | enabled or disabled. | |
7ee32cda GM |
7043 | |
7044 | - `:visible FORM' | |
7045 | ||
9ea173e8 | 7046 | FORM is evaluated and specifies whether the tool bar item is visible. |
7ee32cda GM |
7047 | |
7048 | - `:filter FUNCTION' | |
7049 | ||
7050 | FUNCTION is invoked with one parameter `(quote BINDING)'. Its | |
7051 | result is stored as the new binding. | |
7052 | ||
7053 | - `:button (TYPE SELECTED)' | |
7054 | ||
7055 | TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated | |
7056 | and specifies whether the button is selected (pressed) or not. | |
7057 | ||
7058 | - `:image IMAGES' | |
7059 | ||
7060 | IMAGES is either a single image specification or a vector of four | |
9ea173e8 | 7061 | image specifications. See enum tool_bar_item_images. |
7ee32cda GM |
7062 | |
7063 | - `:help HELP-STRING'. | |
7064 | ||
9ea173e8 | 7065 | Gives a help string to display for the tool bar item. */ |
7ee32cda GM |
7066 | |
7067 | static int | |
9ea173e8 | 7068 | parse_tool_bar_item (key, item) |
7ee32cda GM |
7069 | Lisp_Object key, item; |
7070 | { | |
9ea173e8 GM |
7071 | /* Access slot with index IDX of vector tool_bar_item_properties. */ |
7072 | #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX] | |
7ee32cda GM |
7073 | |
7074 | Lisp_Object filter = Qnil; | |
7075 | Lisp_Object caption; | |
7076 | extern Lisp_Object QCenable, QCvisible, QChelp, QCfilter; | |
7077 | extern Lisp_Object QCbutton, QCtoggle, QCradio; | |
7078 | int i; | |
7ee32cda | 7079 | |
8c907a56 GM |
7080 | /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'. |
7081 | Rule out items that aren't lists, don't start with | |
7082 | `menu-item' or whose rest following `tool-bar-item' is not a | |
7ee32cda GM |
7083 | list. */ |
7084 | if (!CONSP (item) | |
7085 | || !EQ (XCAR (item), Qmenu_item) | |
7086 | || (item = XCDR (item), | |
7087 | !CONSP (item))) | |
7088 | return 0; | |
7089 | ||
9ea173e8 | 7090 | /* Create tool_bar_item_properties vector if necessary. Reset it to |
7ee32cda | 7091 | defaults. */ |
9ea173e8 | 7092 | if (VECTORP (tool_bar_item_properties)) |
7ee32cda | 7093 | { |
9ea173e8 | 7094 | for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i) |
7ee32cda GM |
7095 | PROP (i) = Qnil; |
7096 | } | |
7097 | else | |
9ea173e8 GM |
7098 | tool_bar_item_properties |
7099 | = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil); | |
7ee32cda GM |
7100 | |
7101 | /* Set defaults. */ | |
9ea173e8 GM |
7102 | PROP (TOOL_BAR_ITEM_KEY) = key; |
7103 | PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt; | |
7ee32cda GM |
7104 | |
7105 | /* Get the caption of the item. If the caption is not a string, | |
7106 | evaluate it to get a string. If we don't get a string, skip this | |
7107 | item. */ | |
7108 | caption = XCAR (item); | |
7109 | if (!STRINGP (caption)) | |
7110 | { | |
7111 | caption = menu_item_eval_property (caption); | |
7112 | if (!STRINGP (caption)) | |
7113 | return 0; | |
7114 | } | |
9ea173e8 | 7115 | PROP (TOOL_BAR_ITEM_CAPTION) = caption; |
7ee32cda GM |
7116 | |
7117 | /* Give up if rest following the caption is not a list. */ | |
7118 | item = XCDR (item); | |
7119 | if (!CONSP (item)) | |
7120 | return 0; | |
7121 | ||
7122 | /* Store the binding. */ | |
9ea173e8 | 7123 | PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item); |
7ee32cda GM |
7124 | item = XCDR (item); |
7125 | ||
8c907a56 GM |
7126 | /* Ignore cached key binding, if any. */ |
7127 | if (CONSP (item) && CONSP (XCAR (item))) | |
7128 | item = XCDR (item); | |
7129 | ||
7ee32cda GM |
7130 | /* Process the rest of the properties. */ |
7131 | for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item))) | |
7132 | { | |
7133 | Lisp_Object key, value; | |
7134 | ||
7135 | key = XCAR (item); | |
7136 | value = XCAR (XCDR (item)); | |
7137 | ||
7138 | if (EQ (key, QCenable)) | |
7139 | /* `:enable FORM'. */ | |
9ea173e8 | 7140 | PROP (TOOL_BAR_ITEM_ENABLED_P) = value; |
7ee32cda GM |
7141 | else if (EQ (key, QCvisible)) |
7142 | { | |
7143 | /* `:visible FORM'. If got a visible property and that | |
7144 | evaluates to nil then ignore this item. */ | |
7145 | if (NILP (menu_item_eval_property (value))) | |
7146 | return 0; | |
7147 | } | |
7148 | else if (EQ (key, QChelp)) | |
7149 | /* `:help HELP-STRING'. */ | |
9ea173e8 | 7150 | PROP (TOOL_BAR_ITEM_HELP) = value; |
7ee32cda GM |
7151 | else if (EQ (key, QCfilter)) |
7152 | /* ':filter FORM'. */ | |
7153 | filter = value; | |
7154 | else if (EQ (key, QCbutton) && CONSP (value)) | |
7155 | { | |
7156 | /* `:button (TYPE . SELECTED)'. */ | |
7157 | Lisp_Object type, selected; | |
7158 | ||
7159 | type = XCAR (value); | |
7160 | selected = XCDR (value); | |
7161 | if (EQ (type, QCtoggle) || EQ (type, QCradio)) | |
7162 | { | |
9ea173e8 GM |
7163 | PROP (TOOL_BAR_ITEM_SELECTED_P) = selected; |
7164 | PROP (TOOL_BAR_ITEM_TYPE) = type; | |
7ee32cda GM |
7165 | } |
7166 | } | |
7167 | else if (EQ (key, QCimage) | |
7168 | && (CONSP (value) | |
7169 | || (VECTORP (value) && XVECTOR (value)->size == 4))) | |
7170 | /* Value is either a single image specification or a vector | |
7171 | of 4 such specifications for the different buttion states. */ | |
9ea173e8 | 7172 | PROP (TOOL_BAR_ITEM_IMAGES) = value; |
7ee32cda GM |
7173 | } |
7174 | ||
7175 | /* If got a filter apply it on binding. */ | |
7176 | if (!NILP (filter)) | |
9ea173e8 | 7177 | PROP (TOOL_BAR_ITEM_BINDING) |
7ee32cda GM |
7178 | = menu_item_eval_property (list2 (filter, |
7179 | list2 (Qquote, | |
9ea173e8 | 7180 | PROP (TOOL_BAR_ITEM_BINDING)))); |
7ee32cda GM |
7181 | |
7182 | /* See if the binding is a keymap. Give up if it is. */ | |
02067692 | 7183 | if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1))) |
7ee32cda GM |
7184 | return 0; |
7185 | ||
7186 | /* Enable or disable selection of item. */ | |
9ea173e8 GM |
7187 | if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt)) |
7188 | PROP (TOOL_BAR_ITEM_ENABLED_P) | |
7189 | = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P)); | |
7ee32cda GM |
7190 | |
7191 | /* Handle radio buttons or toggle boxes. */ | |
9ea173e8 GM |
7192 | if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P))) |
7193 | PROP (TOOL_BAR_ITEM_SELECTED_P) | |
7194 | = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P)); | |
7ee32cda GM |
7195 | |
7196 | return 1; | |
7197 | ||
7198 | #undef PROP | |
7199 | } | |
7200 | ||
7201 | ||
9ea173e8 GM |
7202 | /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector |
7203 | that can be reused. */ | |
7ee32cda GM |
7204 | |
7205 | static void | |
9ea173e8 | 7206 | init_tool_bar_items (reuse) |
7ee32cda GM |
7207 | Lisp_Object reuse; |
7208 | { | |
7209 | if (VECTORP (reuse)) | |
9ea173e8 | 7210 | tool_bar_items_vector = reuse; |
7ee32cda | 7211 | else |
9ea173e8 GM |
7212 | tool_bar_items_vector = Fmake_vector (make_number (64), Qnil); |
7213 | ntool_bar_items = 0; | |
7ee32cda GM |
7214 | } |
7215 | ||
7216 | ||
9ea173e8 GM |
7217 | /* Append parsed tool bar item properties from |
7218 | tool_bar_item_properties */ | |
7ee32cda GM |
7219 | |
7220 | static void | |
9ea173e8 | 7221 | append_tool_bar_item () |
7ee32cda GM |
7222 | { |
7223 | Lisp_Object *to, *from; | |
7224 | ||
9ea173e8 GM |
7225 | /* Enlarge tool_bar_items_vector if necessary. */ |
7226 | if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS | |
7227 | >= XVECTOR (tool_bar_items_vector)->size) | |
7ee32cda GM |
7228 | { |
7229 | Lisp_Object new_vector; | |
9ea173e8 | 7230 | int old_size = XVECTOR (tool_bar_items_vector)->size; |
7ee32cda GM |
7231 | |
7232 | new_vector = Fmake_vector (make_number (2 * old_size), Qnil); | |
9ea173e8 | 7233 | bcopy (XVECTOR (tool_bar_items_vector)->contents, |
7ee32cda GM |
7234 | XVECTOR (new_vector)->contents, |
7235 | old_size * sizeof (Lisp_Object)); | |
9ea173e8 | 7236 | tool_bar_items_vector = new_vector; |
7ee32cda GM |
7237 | } |
7238 | ||
9ea173e8 GM |
7239 | /* Append entries from tool_bar_item_properties to the end of |
7240 | tool_bar_items_vector. */ | |
7241 | to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items; | |
7242 | from = XVECTOR (tool_bar_item_properties)->contents; | |
7243 | bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to); | |
7244 | ntool_bar_items += TOOL_BAR_ITEM_NSLOTS; | |
7ee32cda GM |
7245 | } |
7246 | ||
7247 | ||
7248 | ||
7249 | ||
e8886a1d | 7250 | \f |
dcc408a0 RS |
7251 | /* Read a character using menus based on maps in the array MAPS. |
7252 | NMAPS is the length of MAPS. Return nil if there are no menus in the maps. | |
7253 | Return t if we displayed a menu but the user rejected it. | |
7d6de002 RS |
7254 | |
7255 | PREV_EVENT is the previous input event, or nil if we are reading | |
7256 | the first event of a key sequence. | |
7257 | ||
83d68044 | 7258 | If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1 |
6569cc8d | 7259 | if we used a mouse menu to read the input, or zero otherwise. If |
83d68044 | 7260 | USED_MOUSE_MENU is null, we don't dereference it. |
284f4730 JB |
7261 | |
7262 | The prompting is done based on the prompt-string of the map | |
df0f2ba1 | 7263 | and the strings associated with various map elements. |
8150596a RS |
7264 | |
7265 | This can be done with X menus or with menus put in the minibuf. | |
7266 | These are done in different ways, depending on how the input will be read. | |
7267 | Menus using X are done after auto-saving in read-char, getting the input | |
7268 | event from Fx_popup_menu; menus using the minibuf use read_char recursively | |
7269 | and do auto-saving in the inner call of read_char. */ | |
284f4730 | 7270 | |
7617111f | 7271 | static Lisp_Object |
8150596a | 7272 | read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu) |
7d6de002 RS |
7273 | int nmaps; |
7274 | Lisp_Object *maps; | |
7275 | Lisp_Object prev_event; | |
7276 | int *used_mouse_menu; | |
284f4730 | 7277 | { |
7d6de002 RS |
7278 | int mapno; |
7279 | register Lisp_Object name; | |
7d6de002 | 7280 | |
6569cc8d JB |
7281 | if (used_mouse_menu) |
7282 | *used_mouse_menu = 0; | |
284f4730 JB |
7283 | |
7284 | /* Use local over global Menu maps */ | |
7285 | ||
7d6de002 RS |
7286 | if (! menu_prompting) |
7287 | return Qnil; | |
7288 | ||
03361bcc RS |
7289 | /* Optionally disregard all but the global map. */ |
7290 | if (inhibit_local_menu_bar_menus) | |
7291 | { | |
7292 | maps += (nmaps - 1); | |
7293 | nmaps = 1; | |
7294 | } | |
7295 | ||
7d6de002 RS |
7296 | /* Get the menu name from the first map that has one (a prompt string). */ |
7297 | for (mapno = 0; mapno < nmaps; mapno++) | |
7298 | { | |
7299 | name = map_prompt (maps[mapno]); | |
7300 | if (!NILP (name)) | |
7301 | break; | |
7302 | } | |
284f4730 | 7303 | |
7d6de002 | 7304 | /* If we don't have any menus, just read a character normally. */ |
dbc4e1c1 | 7305 | if (mapno >= nmaps) |
7d6de002 RS |
7306 | return Qnil; |
7307 | ||
1f5b1641 | 7308 | #ifdef HAVE_MENUS |
7d6de002 RS |
7309 | /* If we got to this point via a mouse click, |
7310 | use a real menu for mouse selection. */ | |
5a8d99e0 | 7311 | if (EVENT_HAS_PARAMETERS (prev_event) |
7539e11f KR |
7312 | && !EQ (XCAR (prev_event), Qmenu_bar) |
7313 | && !EQ (XCAR (prev_event), Qtool_bar)) | |
7d6de002 RS |
7314 | { |
7315 | /* Display the menu and get the selection. */ | |
7316 | Lisp_Object *realmaps | |
7317 | = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object)); | |
7318 | Lisp_Object value; | |
7319 | int nmaps1 = 0; | |
7320 | ||
7321 | /* Use the maps that are not nil. */ | |
7322 | for (mapno = 0; mapno < nmaps; mapno++) | |
7323 | if (!NILP (maps[mapno])) | |
7324 | realmaps[nmaps1++] = maps[mapno]; | |
7325 | ||
7326 | value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps)); | |
663258f2 JB |
7327 | if (CONSP (value)) |
7328 | { | |
68f297c5 RS |
7329 | Lisp_Object tem; |
7330 | ||
7539e11f | 7331 | record_menu_key (XCAR (value)); |
8eb4d8ef | 7332 | |
68f297c5 RS |
7333 | /* If we got multiple events, unread all but |
7334 | the first. | |
7335 | There is no way to prevent those unread events | |
7336 | from showing up later in last_nonmenu_event. | |
7337 | So turn symbol and integer events into lists, | |
7338 | to indicate that they came from a mouse menu, | |
7339 | so that when present in last_nonmenu_event | |
7340 | they won't confuse things. */ | |
7539e11f KR |
7341 | for (tem = XCDR (value); !NILP (tem); |
7342 | tem = XCDR (tem)) | |
8eb4d8ef | 7343 | { |
7539e11f KR |
7344 | record_menu_key (XCAR (tem)); |
7345 | if (SYMBOLP (XCAR (tem)) | |
7346 | || INTEGERP (XCAR (tem))) | |
7347 | XCAR (tem) | |
7348 | = Fcons (XCAR (tem), Qnil); | |
8eb4d8ef | 7349 | } |
68f297c5 | 7350 | |
663258f2 JB |
7351 | /* If we got more than one event, put all but the first |
7352 | onto this list to be read later. | |
7353 | Return just the first event now. */ | |
24597608 | 7354 | Vunread_command_events |
7539e11f KR |
7355 | = nconc2 (XCDR (value), Vunread_command_events); |
7356 | value = XCAR (value); | |
663258f2 | 7357 | } |
1c90c381 | 7358 | else if (NILP (value)) |
dcc408a0 | 7359 | value = Qt; |
6569cc8d JB |
7360 | if (used_mouse_menu) |
7361 | *used_mouse_menu = 1; | |
7d6de002 RS |
7362 | return value; |
7363 | } | |
1f5b1641 | 7364 | #endif /* HAVE_MENUS */ |
8150596a RS |
7365 | return Qnil ; |
7366 | } | |
7367 | ||
68e0de82 RS |
7368 | /* Buffer in use so far for the minibuf prompts for menu keymaps. |
7369 | We make this bigger when necessary, and never free it. */ | |
7370 | static char *read_char_minibuf_menu_text; | |
7371 | /* Size of that buffer. */ | |
7372 | static int read_char_minibuf_menu_width; | |
7373 | ||
8150596a | 7374 | static Lisp_Object |
24597608 | 7375 | read_char_minibuf_menu_prompt (commandflag, nmaps, maps) |
8150596a RS |
7376 | int commandflag ; |
7377 | int nmaps; | |
7378 | Lisp_Object *maps; | |
7379 | { | |
7380 | int mapno; | |
7381 | register Lisp_Object name; | |
7382 | int nlength; | |
788f89eb | 7383 | int width = FRAME_WIDTH (SELECTED_FRAME ()) - 4; |
8150596a | 7384 | int idx = -1; |
9fdbfdf8 | 7385 | int nobindings = 1; |
8150596a | 7386 | Lisp_Object rest, vector; |
68e0de82 | 7387 | char *menu; |
8150596a | 7388 | |
8c907a56 GM |
7389 | vector = Qnil; |
7390 | ||
8150596a RS |
7391 | if (! menu_prompting) |
7392 | return Qnil; | |
7393 | ||
68e0de82 RS |
7394 | /* Make sure we have a big enough buffer for the menu text. */ |
7395 | if (read_char_minibuf_menu_text == 0) | |
7396 | { | |
7397 | read_char_minibuf_menu_width = width + 4; | |
7398 | read_char_minibuf_menu_text = (char *) xmalloc (width + 4); | |
7399 | } | |
7400 | else if (width + 4 > read_char_minibuf_menu_width) | |
7401 | { | |
7402 | read_char_minibuf_menu_width = width + 4; | |
7403 | read_char_minibuf_menu_text | |
7404 | = (char *) xrealloc (read_char_minibuf_menu_text, width + 4); | |
7405 | } | |
7406 | menu = read_char_minibuf_menu_text; | |
7407 | ||
8150596a RS |
7408 | /* Get the menu name from the first map that has one (a prompt string). */ |
7409 | for (mapno = 0; mapno < nmaps; mapno++) | |
7410 | { | |
7411 | name = map_prompt (maps[mapno]); | |
7412 | if (!NILP (name)) | |
7413 | break; | |
7414 | } | |
7415 | ||
7416 | /* If we don't have any menus, just read a character normally. */ | |
7417 | if (mapno >= nmaps) | |
7418 | return Qnil; | |
284f4730 JB |
7419 | |
7420 | /* Prompt string always starts with map's prompt, and a space. */ | |
7421 | strcpy (menu, XSTRING (name)->data); | |
fc932ac6 | 7422 | nlength = STRING_BYTES (XSTRING (name)); |
7d6de002 | 7423 | menu[nlength++] = ':'; |
284f4730 JB |
7424 | menu[nlength++] = ' '; |
7425 | menu[nlength] = 0; | |
7426 | ||
7d6de002 RS |
7427 | /* Start prompting at start of first map. */ |
7428 | mapno = 0; | |
7429 | rest = maps[mapno]; | |
284f4730 JB |
7430 | |
7431 | /* Present the documented bindings, a line at a time. */ | |
7432 | while (1) | |
7433 | { | |
7434 | int notfirst = 0; | |
7435 | int i = nlength; | |
7436 | Lisp_Object obj; | |
7437 | int ch; | |
8066f1a1 | 7438 | Lisp_Object orig_defn_macro; |
284f4730 | 7439 | |
284f4730 | 7440 | /* Loop over elements of map. */ |
7d6de002 | 7441 | while (i < width) |
284f4730 | 7442 | { |
fd3613d7 | 7443 | Lisp_Object elt; |
284f4730 | 7444 | |
7d6de002 RS |
7445 | /* If reached end of map, start at beginning of next map. */ |
7446 | if (NILP (rest)) | |
7447 | { | |
7448 | mapno++; | |
7449 | /* At end of last map, wrap around to first map if just starting, | |
7450 | or end this line if already have something on it. */ | |
7451 | if (mapno == nmaps) | |
284f4730 | 7452 | { |
8150596a | 7453 | mapno = 0; |
40932d1a | 7454 | if (notfirst || nobindings) break; |
284f4730 | 7455 | } |
7d6de002 | 7456 | rest = maps[mapno]; |
284f4730 | 7457 | } |
7d6de002 RS |
7458 | |
7459 | /* Look at the next element of the map. */ | |
7460 | if (idx >= 0) | |
7461 | elt = XVECTOR (vector)->contents[idx]; | |
284f4730 | 7462 | else |
7d6de002 RS |
7463 | elt = Fcar_safe (rest); |
7464 | ||
8c18cbfb | 7465 | if (idx < 0 && VECTORP (elt)) |
284f4730 | 7466 | { |
7d6de002 RS |
7467 | /* If we found a dense table in the keymap, |
7468 | advanced past it, but start scanning its contents. */ | |
7469 | rest = Fcdr_safe (rest); | |
7470 | vector = elt; | |
7471 | idx = 0; | |
284f4730 | 7472 | } |
7d6de002 RS |
7473 | else |
7474 | { | |
7475 | /* An ordinary element. */ | |
fd3613d7 | 7476 | Lisp_Object event, tem; |
0a2ea221 KH |
7477 | |
7478 | if (idx < 0) | |
7479 | { | |
fd3613d7 RS |
7480 | event = Fcar_safe (elt); /* alist */ |
7481 | elt = Fcdr_safe (elt); | |
0a2ea221 | 7482 | } |
8150596a | 7483 | else |
7d6de002 | 7484 | { |
fd3613d7 | 7485 | XSETINT (event, idx); /* vector */ |
0a2ea221 KH |
7486 | } |
7487 | ||
7488 | /* Ignore the element if it has no prompt string. */ | |
fd3613d7 | 7489 | if (INTEGERP (event) && parse_menu_item (elt, 0, -1)) |
0a2ea221 KH |
7490 | { |
7491 | /* 1 if the char to type matches the string. */ | |
7492 | int char_matches; | |
7493 | Lisp_Object upcased_event, downcased_event; | |
8c907a56 | 7494 | Lisp_Object desc = Qnil; |
fd3613d7 RS |
7495 | Lisp_Object s |
7496 | = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]; | |
0a2ea221 KH |
7497 | |
7498 | upcased_event = Fupcase (event); | |
7499 | downcased_event = Fdowncase (event); | |
7500 | char_matches = (XINT (upcased_event) == XSTRING (s)->data[0] | |
7501 | || XINT (downcased_event) == XSTRING (s)->data[0]); | |
7502 | if (! char_matches) | |
efff6e49 | 7503 | desc = Fsingle_key_description (event, Qnil); |
0a2ea221 | 7504 | |
fd3613d7 RS |
7505 | tem |
7506 | = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]; | |
7507 | if (!NILP (tem)) | |
7508 | /* Insert equivalent keybinding. */ | |
7509 | s = concat2 (s, tem); | |
7510 | ||
7511 | tem | |
7512 | = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]; | |
7513 | if (EQ (tem, QCradio) || EQ (tem, QCtoggle)) | |
7514 | { | |
7515 | /* Insert button prefix. */ | |
7516 | Lisp_Object selected | |
7517 | = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]; | |
7518 | if (EQ (tem, QCradio)) | |
7519 | tem = build_string (NILP (selected) ? "(*) " : "( ) "); | |
7520 | else | |
7521 | tem = build_string (NILP (selected) ? "[X] " : "[ ] "); | |
7522 | s = concat2 (tem, s); | |
7523 | } | |
7524 | ||
7525 | ||
0a2ea221 KH |
7526 | /* If we have room for the prompt string, add it to this line. |
7527 | If this is the first on the line, always add it. */ | |
7528 | if ((XSTRING (s)->size + i + 2 | |
7529 | + (char_matches ? 0 : XSTRING (desc)->size + 3)) | |
7530 | < width | |
7531 | || !notfirst) | |
7532 | { | |
7533 | int thiswidth; | |
7534 | ||
7535 | /* Punctuate between strings. */ | |
7536 | if (notfirst) | |
7537 | { | |
7538 | strcpy (menu + i, ", "); | |
7539 | i += 2; | |
7540 | } | |
7541 | notfirst = 1; | |
7542 | nobindings = 0 ; | |
284f4730 | 7543 | |
0a2ea221 KH |
7544 | /* If the char to type doesn't match the string's |
7545 | first char, explicitly show what char to type. */ | |
7546 | if (! char_matches) | |
7547 | { | |
7548 | /* Add as much of string as fits. */ | |
7549 | thiswidth = XSTRING (desc)->size; | |
7550 | if (thiswidth + i > width) | |
7551 | thiswidth = width - i; | |
7552 | bcopy (XSTRING (desc)->data, menu + i, thiswidth); | |
7553 | i += thiswidth; | |
7554 | strcpy (menu + i, " = "); | |
7555 | i += 3; | |
7556 | } | |
7557 | ||
7558 | /* Add as much of string as fits. */ | |
7559 | thiswidth = XSTRING (s)->size; | |
7560 | if (thiswidth + i > width) | |
7561 | thiswidth = width - i; | |
7562 | bcopy (XSTRING (s)->data, menu + i, thiswidth); | |
7563 | i += thiswidth; | |
7564 | menu[i] = 0; | |
7565 | } | |
7566 | else | |
7d6de002 | 7567 | { |
0a2ea221 KH |
7568 | /* If this element does not fit, end the line now, |
7569 | and save the element for the next line. */ | |
7570 | strcpy (menu + i, "..."); | |
7571 | break; | |
7d6de002 | 7572 | } |
7d6de002 RS |
7573 | } |
7574 | ||
7575 | /* Move past this element. */ | |
8150596a | 7576 | if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size) |
7d6de002 RS |
7577 | /* Handle reaching end of dense table. */ |
7578 | idx = -1; | |
7579 | if (idx >= 0) | |
7580 | idx++; | |
7581 | else | |
7582 | rest = Fcdr_safe (rest); | |
7583 | } | |
284f4730 JB |
7584 | } |
7585 | ||
7586 | /* Prompt with that and read response. */ | |
301738ed RS |
7587 | message2_nolog (menu, strlen (menu), |
7588 | ! NILP (current_buffer->enable_multibyte_characters)); | |
8150596a | 7589 | |
df0f2ba1 | 7590 | /* Make believe its not a keyboard macro in case the help char |
8150596a RS |
7591 | is pressed. Help characters are not recorded because menu prompting |
7592 | is not used on replay. | |
7593 | */ | |
c5fdd383 KH |
7594 | orig_defn_macro = current_kboard->defining_kbd_macro; |
7595 | current_kboard->defining_kbd_macro = Qnil; | |
3cb81011 | 7596 | do |
7735786b | 7597 | obj = read_char (commandflag, 0, 0, Qt, 0); |
8c18cbfb | 7598 | while (BUFFERP (obj)); |
c5fdd383 | 7599 | current_kboard->defining_kbd_macro = orig_defn_macro; |
284f4730 | 7600 | |
8c18cbfb | 7601 | if (!INTEGERP (obj)) |
284f4730 JB |
7602 | return obj; |
7603 | else | |
7604 | ch = XINT (obj); | |
7605 | ||
f4255cd1 | 7606 | if (! EQ (obj, menu_prompt_more_char) |
8c18cbfb | 7607 | && (!INTEGERP (menu_prompt_more_char) |
f4255cd1 | 7608 | || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char)))))) |
8150596a | 7609 | { |
c5fdd383 | 7610 | if (!NILP (current_kboard->defining_kbd_macro)) |
8066f1a1 | 7611 | store_kbd_macro_char (obj); |
8150596a RS |
7612 | return obj; |
7613 | } | |
7614 | /* Help char - go round again */ | |
284f4730 JB |
7615 | } |
7616 | } | |
284f4730 JB |
7617 | \f |
7618 | /* Reading key sequences. */ | |
7619 | ||
7620 | /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings | |
7621 | in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a | |
7622 | keymap, or nil otherwise. Return the index of the first keymap in | |
7623 | which KEY has any binding, or NMAPS if no map has a binding. | |
7624 | ||
7625 | If KEY is a meta ASCII character, treat it like meta-prefix-char | |
7626 | followed by the corresponding non-meta character. Keymaps in | |
7627 | CURRENT with non-prefix bindings for meta-prefix-char become nil in | |
7628 | NEXT. | |
7629 | ||
88cb0656 JB |
7630 | If KEY has no bindings in any of the CURRENT maps, NEXT is left |
7631 | unmodified. | |
7632 | ||
569871d2 | 7633 | NEXT may be the same array as CURRENT. */ |
284f4730 JB |
7634 | |
7635 | static int | |
4e50f26a | 7636 | follow_key (key, nmaps, current, defs, next) |
284f4730 JB |
7637 | Lisp_Object key; |
7638 | Lisp_Object *current, *defs, *next; | |
7639 | int nmaps; | |
7640 | { | |
7641 | int i, first_binding; | |
569871d2 | 7642 | int did_meta = 0; |
284f4730 | 7643 | |
284f4730 JB |
7644 | first_binding = nmaps; |
7645 | for (i = nmaps - 1; i >= 0; i--) | |
7646 | { | |
7647 | if (! NILP (current[i])) | |
7648 | { | |
569871d2 RS |
7649 | Lisp_Object map; |
7650 | if (did_meta) | |
7651 | map = defs[i]; | |
7652 | else | |
7653 | map = current[i]; | |
7654 | ||
db785038 | 7655 | defs[i] = access_keymap (map, key, 1, 0, 1); |
284f4730 JB |
7656 | if (! NILP (defs[i])) |
7657 | first_binding = i; | |
7658 | } | |
7659 | else | |
7660 | defs[i] = Qnil; | |
7661 | } | |
7662 | ||
284f4730 | 7663 | /* Given the set of bindings we've found, produce the next set of maps. */ |
0a7f1fc0 JB |
7664 | if (first_binding < nmaps) |
7665 | for (i = 0; i < nmaps; i++) | |
02067692 | 7666 | next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1); |
284f4730 JB |
7667 | |
7668 | return first_binding; | |
7669 | } | |
7670 | ||
df0f2ba1 | 7671 | /* Read a sequence of keys that ends with a non prefix character, |
f4255cd1 JB |
7672 | storing it in KEYBUF, a buffer of size BUFSIZE. |
7673 | Prompt with PROMPT. | |
284f4730 | 7674 | Return the length of the key sequence stored. |
dcc408a0 | 7675 | Return -1 if the user rejected a command menu. |
284f4730 | 7676 | |
f4255cd1 JB |
7677 | Echo starting immediately unless `prompt' is 0. |
7678 | ||
7679 | Where a key sequence ends depends on the currently active keymaps. | |
7680 | These include any minor mode keymaps active in the current buffer, | |
7681 | the current buffer's local map, and the global map. | |
7682 | ||
7683 | If a key sequence has no other bindings, we check Vfunction_key_map | |
7684 | to see if some trailing subsequence might be the beginning of a | |
7685 | function key's sequence. If so, we try to read the whole function | |
7686 | key, and substitute its symbolic name into the key sequence. | |
7687 | ||
fbcd35bd JB |
7688 | We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and |
7689 | `double-' events into similar click events, if that would make them | |
7690 | bound. We try to turn `triple-' events first into `double-' events, | |
7691 | then into clicks. | |
f4255cd1 JB |
7692 | |
7693 | If we get a mouse click in a mode line, vertical divider, or other | |
7694 | non-text area, we treat the click as if it were prefixed by the | |
7695 | symbol denoting that area - `mode-line', `vertical-line', or | |
7696 | whatever. | |
7697 | ||
7698 | If the sequence starts with a mouse click, we read the key sequence | |
7699 | with respect to the buffer clicked on, not the current buffer. | |
284f4730 | 7700 | |
f4255cd1 JB |
7701 | If the user switches frames in the midst of a key sequence, we put |
7702 | off the switch-frame event until later; the next call to | |
f571ae0d RS |
7703 | read_char will return it. |
7704 | ||
7705 | If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer | |
7706 | from the selected window's buffer. */ | |
48e416d4 | 7707 | |
284f4730 | 7708 | static int |
ce98e608 | 7709 | read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last, |
f571ae0d | 7710 | can_return_switch_frame, fix_current_buffer) |
284f4730 JB |
7711 | Lisp_Object *keybuf; |
7712 | int bufsize; | |
84d91fda | 7713 | Lisp_Object prompt; |
309b0fc8 | 7714 | int dont_downcase_last; |
ce98e608 | 7715 | int can_return_switch_frame; |
f571ae0d | 7716 | int fix_current_buffer; |
284f4730 | 7717 | { |
8c907a56 | 7718 | volatile int count = specpdl_ptr - specpdl; |
f4255cd1 | 7719 | |
284f4730 | 7720 | /* How many keys there are in the current key sequence. */ |
8c907a56 | 7721 | volatile int t; |
284f4730 | 7722 | |
284f4730 JB |
7723 | /* The length of the echo buffer when we started reading, and |
7724 | the length of this_command_keys when we started reading. */ | |
8c907a56 GM |
7725 | volatile int echo_start; |
7726 | volatile int keys_start; | |
284f4730 JB |
7727 | |
7728 | /* The number of keymaps we're scanning right now, and the number of | |
7729 | keymaps we have allocated space for. */ | |
8c907a56 GM |
7730 | volatile int nmaps; |
7731 | volatile int nmaps_allocated = 0; | |
284f4730 | 7732 | |
284f4730 JB |
7733 | /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in |
7734 | the current keymaps. */ | |
8c907a56 | 7735 | Lisp_Object *volatile defs = NULL; |
284f4730 | 7736 | |
f4255cd1 JB |
7737 | /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1] |
7738 | in the current keymaps, or nil where it is not a prefix. */ | |
8c907a56 | 7739 | Lisp_Object *volatile submaps = NULL; |
f4255cd1 | 7740 | |
e0dff5f6 | 7741 | /* The local map to start out with at start of key sequence. */ |
8c907a56 | 7742 | volatile Lisp_Object orig_local_map; |
e0dff5f6 | 7743 | |
30690496 DL |
7744 | /* The map from the `keymap' property to start out with at start of |
7745 | key sequence. */ | |
8c907a56 | 7746 | volatile Lisp_Object orig_keymap; |
30690496 | 7747 | |
e0dff5f6 RS |
7748 | /* 1 if we have already considered switching to the local-map property |
7749 | of the place where a mouse click occurred. */ | |
8c907a56 | 7750 | volatile int localized_local_map = 0; |
e0dff5f6 | 7751 | |
f4255cd1 JB |
7752 | /* The index in defs[] of the first keymap that has a binding for |
7753 | this key sequence. In other words, the lowest i such that | |
7754 | defs[i] is non-nil. */ | |
8c907a56 | 7755 | volatile int first_binding; |
284f4730 | 7756 | |
f4255cd1 | 7757 | /* If t < mock_input, then KEYBUF[t] should be read as the next |
253598e4 JB |
7758 | input key. |
7759 | ||
7760 | We use this to recover after recognizing a function key. Once we | |
7761 | realize that a suffix of the current key sequence is actually a | |
7762 | function key's escape sequence, we replace the suffix with the | |
7763 | function key's binding from Vfunction_key_map. Now keybuf | |
f4255cd1 JB |
7764 | contains a new and different key sequence, so the echo area, |
7765 | this_command_keys, and the submaps and defs arrays are wrong. In | |
7766 | this situation, we set mock_input to t, set t to 0, and jump to | |
7767 | restart_sequence; the loop will read keys from keybuf up until | |
7768 | mock_input, thus rebuilding the state; and then it will resume | |
7769 | reading characters from the keyboard. */ | |
8c907a56 | 7770 | volatile int mock_input = 0; |
284f4730 | 7771 | |
253598e4 | 7772 | /* If the sequence is unbound in submaps[], then |
f4255cd1 JB |
7773 | keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map, |
7774 | and fkey_map is its binding. | |
253598e4 | 7775 | |
f4255cd1 JB |
7776 | These might be > t, indicating that all function key scanning |
7777 | should hold off until t reaches them. We do this when we've just | |
7778 | recognized a function key, to avoid searching for the function | |
7779 | key's again in Vfunction_key_map. */ | |
8c907a56 GM |
7780 | volatile int fkey_start = 0, fkey_end = 0; |
7781 | volatile Lisp_Object fkey_map; | |
284f4730 | 7782 | |
a612e298 | 7783 | /* Likewise, for key_translation_map. */ |
8c907a56 GM |
7784 | volatile int keytran_start = 0, keytran_end = 0; |
7785 | volatile Lisp_Object keytran_map; | |
a612e298 | 7786 | |
cd21b839 JB |
7787 | /* If we receive a ``switch-frame'' event in the middle of a key sequence, |
7788 | we put it off for later. While we're reading, we keep the event here. */ | |
8c907a56 | 7789 | volatile Lisp_Object delayed_switch_frame; |
cd21b839 | 7790 | |
51763820 BF |
7791 | /* See the comment below... */ |
7792 | #if defined (GOBBLE_FIRST_EVENT) | |
4efda7dd | 7793 | Lisp_Object first_event; |
51763820 | 7794 | #endif |
4efda7dd | 7795 | |
8c907a56 GM |
7796 | volatile Lisp_Object original_uppercase; |
7797 | volatile int original_uppercase_position = -1; | |
309b0fc8 | 7798 | |
bc536d84 | 7799 | /* Gets around Microsoft compiler limitations. */ |
309b0fc8 | 7800 | int dummyflag = 0; |
bc536d84 | 7801 | |
3b9189f8 RS |
7802 | struct buffer *starting_buffer; |
7803 | ||
e9bf89a0 RS |
7804 | /* Nonzero if we seem to have got the beginning of a binding |
7805 | in function_key_map. */ | |
8c907a56 GM |
7806 | volatile int function_key_possible = 0; |
7807 | volatile int key_translation_possible = 0; | |
e9bf89a0 | 7808 | |
3fe8e9a2 RS |
7809 | /* Save the status of key translation before each step, |
7810 | so that we can restore this after downcasing. */ | |
7811 | Lisp_Object prev_fkey_map; | |
3fdfceb3 RS |
7812 | int prev_fkey_start; |
7813 | int prev_fkey_end; | |
3fe8e9a2 RS |
7814 | |
7815 | Lisp_Object prev_keytran_map; | |
3fdfceb3 RS |
7816 | int prev_keytran_start; |
7817 | int prev_keytran_end; | |
3fe8e9a2 | 7818 | |
03cee6ae | 7819 | #if defined (GOBBLE_FIRST_EVENT) |
4efda7dd | 7820 | int junk; |
03cee6ae | 7821 | #endif |
4efda7dd | 7822 | |
7d18f9ae RS |
7823 | raw_keybuf_count = 0; |
7824 | ||
4efda7dd RS |
7825 | last_nonmenu_event = Qnil; |
7826 | ||
7827 | delayed_switch_frame = Qnil; | |
7828 | fkey_map = Vfunction_key_map; | |
a612e298 | 7829 | keytran_map = Vkey_translation_map; |
f4255cd1 | 7830 | |
a612e298 | 7831 | /* If there is no function-key-map, turn off function key scanning. */ |
02067692 | 7832 | if (!KEYMAPP (Vfunction_key_map)) |
f4255cd1 JB |
7833 | fkey_start = fkey_end = bufsize + 1; |
7834 | ||
a612e298 | 7835 | /* If there is no key-translation-map, turn off scanning. */ |
02067692 | 7836 | if (!KEYMAPP (Vkey_translation_map)) |
a612e298 RS |
7837 | keytran_start = keytran_end = bufsize + 1; |
7838 | ||
284f4730 JB |
7839 | if (INTERACTIVE) |
7840 | { | |
84d91fda RS |
7841 | if (!NILP (prompt)) |
7842 | echo_prompt (XSTRING (prompt)->data); | |
f2647d04 DL |
7843 | else if (cursor_in_echo_area |
7844 | && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes)) | |
7845 | && NILP (Fzerop (Vecho_keystrokes))) | |
284f4730 JB |
7846 | /* This doesn't put in a dash if the echo buffer is empty, so |
7847 | you don't always see a dash hanging out in the minibuffer. */ | |
7848 | echo_dash (); | |
284f4730 JB |
7849 | } |
7850 | ||
f4255cd1 JB |
7851 | /* Record the initial state of the echo area and this_command_keys; |
7852 | we will need to restore them if we replay a key sequence. */ | |
0a7f1fc0 | 7853 | if (INTERACTIVE) |
df0f2ba1 | 7854 | echo_start = echo_length (); |
f4255cd1 | 7855 | keys_start = this_command_key_count; |
6321824f | 7856 | this_single_command_key_start = keys_start; |
0a7f1fc0 | 7857 | |
51763820 BF |
7858 | #if defined (GOBBLE_FIRST_EVENT) |
7859 | /* This doesn't quite work, because some of the things that read_char | |
7860 | does cannot safely be bypassed. It seems too risky to try to make | |
df0f2ba1 | 7861 | this work right. */ |
51763820 | 7862 | |
4efda7dd RS |
7863 | /* Read the first char of the sequence specially, before setting |
7864 | up any keymaps, in case a filter runs and switches buffers on us. */ | |
84d91fda | 7865 | first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event, |
4efda7dd | 7866 | &junk); |
51763820 | 7867 | #endif /* GOBBLE_FIRST_EVENT */ |
4efda7dd | 7868 | |
30690496 DL |
7869 | orig_local_map = get_local_map (PT, current_buffer, local_map); |
7870 | orig_keymap = get_local_map (PT, current_buffer, keymap); | |
e0dff5f6 | 7871 | |
7b4aedb9 JB |
7872 | /* We jump here when the key sequence has been thoroughly changed, and |
7873 | we need to rescan it starting from the beginning. When we jump here, | |
7874 | keybuf[0..mock_input] holds the sequence we should reread. */ | |
07d2b8de | 7875 | replay_sequence: |
7b4aedb9 | 7876 | |
3b9189f8 | 7877 | starting_buffer = current_buffer; |
e9bf89a0 | 7878 | function_key_possible = 0; |
00a78037 | 7879 | key_translation_possible = 0; |
3b9189f8 | 7880 | |
f4255cd1 | 7881 | /* Build our list of keymaps. |
07d2b8de JB |
7882 | If we recognize a function key and replace its escape sequence in |
7883 | keybuf with its symbol, or if the sequence starts with a mouse | |
7884 | click and we need to switch buffers, we jump back here to rebuild | |
7885 | the initial keymaps from the current buffer. */ | |
df0f2ba1 | 7886 | { |
284f4730 JB |
7887 | Lisp_Object *maps; |
7888 | ||
217258d5 KH |
7889 | if (!NILP (current_kboard->Voverriding_terminal_local_map) |
7890 | || !NILP (Voverriding_local_map)) | |
284f4730 | 7891 | { |
217258d5 | 7892 | if (3 > nmaps_allocated) |
9dd3131c | 7893 | { |
217258d5 KH |
7894 | submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0])); |
7895 | defs = (Lisp_Object *) alloca (3 * sizeof (defs[0])); | |
7896 | nmaps_allocated = 3; | |
9dd3131c | 7897 | } |
217258d5 KH |
7898 | nmaps = 0; |
7899 | if (!NILP (current_kboard->Voverriding_terminal_local_map)) | |
7900 | submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map; | |
7901 | if (!NILP (Voverriding_local_map)) | |
7902 | submaps[nmaps++] = Voverriding_local_map; | |
284f4730 | 7903 | } |
9dd3131c RS |
7904 | else |
7905 | { | |
30690496 | 7906 | int extra_maps = 2; |
217258d5 | 7907 | nmaps = current_minor_maps (0, &maps); |
30690496 DL |
7908 | if (!NILP (orig_keymap)) |
7909 | extra_maps = 3; | |
7910 | if (nmaps + extra_maps > nmaps_allocated) | |
9dd3131c | 7911 | { |
30690496 DL |
7912 | submaps = (Lisp_Object *) alloca ((nmaps+extra_maps) |
7913 | * sizeof (submaps[0])); | |
7914 | defs = (Lisp_Object *) alloca ((nmaps+extra_maps) | |
7915 | * sizeof (defs[0])); | |
7916 | nmaps_allocated = nmaps + extra_maps; | |
9dd3131c | 7917 | } |
8c907a56 | 7918 | bcopy (maps, (void *) submaps, nmaps * sizeof (submaps[0])); |
30690496 DL |
7919 | if (!NILP (orig_keymap)) |
7920 | submaps[nmaps++] = orig_keymap; | |
217258d5 | 7921 | submaps[nmaps++] = orig_local_map; |
9dd3131c | 7922 | } |
217258d5 | 7923 | submaps[nmaps++] = current_global_map; |
284f4730 JB |
7924 | } |
7925 | ||
7926 | /* Find an accurate initial value for first_binding. */ | |
7927 | for (first_binding = 0; first_binding < nmaps; first_binding++) | |
253598e4 | 7928 | if (! NILP (submaps[first_binding])) |
284f4730 JB |
7929 | break; |
7930 | ||
3b9189f8 | 7931 | /* Start from the beginning in keybuf. */ |
f4255cd1 JB |
7932 | t = 0; |
7933 | ||
7934 | /* These are no-ops the first time through, but if we restart, they | |
7935 | revert the echo area and this_command_keys to their original state. */ | |
7936 | this_command_key_count = keys_start; | |
df0f2ba1 | 7937 | if (INTERACTIVE && t < mock_input) |
f4255cd1 JB |
7938 | echo_truncate (echo_start); |
7939 | ||
cca310da JB |
7940 | /* If the best binding for the current key sequence is a keymap, or |
7941 | we may be looking at a function key's escape sequence, keep on | |
7942 | reading. */ | |
253598e4 | 7943 | while ((first_binding < nmaps && ! NILP (submaps[first_binding])) |
cca310da JB |
7944 | || (first_binding >= nmaps |
7945 | && fkey_start < t | |
7946 | /* mock input is never part of a function key's sequence. */ | |
a612e298 | 7947 | && mock_input <= fkey_start) |
0d882d52 KH |
7948 | || (first_binding >= nmaps |
7949 | && keytran_start < t && key_translation_possible) | |
e9bf89a0 RS |
7950 | /* Don't return in the middle of a possible function key sequence, |
7951 | if the only bindings we found were via case conversion. | |
7952 | Thus, if ESC O a has a function-key-map translation | |
7953 | and ESC o has a binding, don't return after ESC O, | |
7954 | so that we can translate ESC O plus the next character. */ | |
4e50f26a | 7955 | ) |
284f4730 JB |
7956 | { |
7957 | Lisp_Object key; | |
7d6de002 | 7958 | int used_mouse_menu = 0; |
284f4730 | 7959 | |
7b4aedb9 JB |
7960 | /* Where the last real key started. If we need to throw away a |
7961 | key that has expanded into more than one element of keybuf | |
7962 | (say, a mouse click on the mode line which is being treated | |
7963 | as [mode-line (mouse-...)], then we backtrack to this point | |
7964 | of keybuf. */ | |
8c907a56 | 7965 | volatile int last_real_key_start; |
7b4aedb9 | 7966 | |
0a7f1fc0 JB |
7967 | /* These variables are analogous to echo_start and keys_start; |
7968 | while those allow us to restart the entire key sequence, | |
7969 | echo_local_start and keys_local_start allow us to throw away | |
7970 | just one key. */ | |
8c907a56 | 7971 | volatile int echo_local_start, keys_local_start, local_first_binding; |
f4255cd1 | 7972 | |
284f4730 | 7973 | if (t >= bufsize) |
3fe8e9a2 | 7974 | error ("Key sequence too long"); |
284f4730 | 7975 | |
f4255cd1 JB |
7976 | if (INTERACTIVE) |
7977 | echo_local_start = echo_length (); | |
7978 | keys_local_start = this_command_key_count; | |
7979 | local_first_binding = first_binding; | |
df0f2ba1 | 7980 | |
f4255cd1 | 7981 | replay_key: |
0a7f1fc0 | 7982 | /* These are no-ops, unless we throw away a keystroke below and |
f4255cd1 JB |
7983 | jumped back up to replay_key; in that case, these restore the |
7984 | variables to their original state, allowing us to replay the | |
0a7f1fc0 | 7985 | loop. */ |
40932d1a | 7986 | if (INTERACTIVE && t < mock_input) |
f4255cd1 | 7987 | echo_truncate (echo_local_start); |
0a7f1fc0 JB |
7988 | this_command_key_count = keys_local_start; |
7989 | first_binding = local_first_binding; | |
7990 | ||
7e85b935 RS |
7991 | /* By default, assume each event is "real". */ |
7992 | last_real_key_start = t; | |
7993 | ||
f4255cd1 | 7994 | /* Does mock_input indicate that we are re-reading a key sequence? */ |
284f4730 JB |
7995 | if (t < mock_input) |
7996 | { | |
7997 | key = keybuf[t]; | |
7998 | add_command_key (key); | |
f2647d04 DL |
7999 | if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes)) |
8000 | && NILP (Fzerop (Vecho_keystrokes))) | |
a98ea3f9 | 8001 | echo_char (key); |
284f4730 | 8002 | } |
253598e4 JB |
8003 | |
8004 | /* If not, we should actually read a character. */ | |
284f4730 JB |
8005 | else |
8006 | { | |
beecf6a1 | 8007 | { |
c5fdd383 KH |
8008 | #ifdef MULTI_KBOARD |
8009 | KBOARD *interrupted_kboard = current_kboard; | |
788f89eb | 8010 | struct frame *interrupted_frame = SELECTED_FRAME (); |
c5fdd383 | 8011 | if (setjmp (wrong_kboard_jmpbuf)) |
beecf6a1 | 8012 | { |
5798cf15 KH |
8013 | if (!NILP (delayed_switch_frame)) |
8014 | { | |
c5fdd383 | 8015 | interrupted_kboard->kbd_queue |
5798cf15 | 8016 | = Fcons (delayed_switch_frame, |
c5fdd383 | 8017 | interrupted_kboard->kbd_queue); |
5798cf15 KH |
8018 | delayed_switch_frame = Qnil; |
8019 | } | |
beecf6a1 | 8020 | while (t > 0) |
c5fdd383 KH |
8021 | interrupted_kboard->kbd_queue |
8022 | = Fcons (keybuf[--t], interrupted_kboard->kbd_queue); | |
5798cf15 KH |
8023 | |
8024 | /* If the side queue is non-empty, ensure it begins with a | |
8025 | switch-frame, so we'll replay it in the right context. */ | |
c5fdd383 | 8026 | if (CONSP (interrupted_kboard->kbd_queue) |
7539e11f | 8027 | && (key = XCAR (interrupted_kboard->kbd_queue), |
5798cf15 KH |
8028 | !(EVENT_HAS_PARAMETERS (key) |
8029 | && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), | |
8030 | Qswitch_frame)))) | |
df0f2ba1 KH |
8031 | { |
8032 | Lisp_Object frame; | |
8033 | XSETFRAME (frame, interrupted_frame); | |
c5fdd383 | 8034 | interrupted_kboard->kbd_queue |
df0f2ba1 | 8035 | = Fcons (make_lispy_switch_frame (frame), |
c5fdd383 | 8036 | interrupted_kboard->kbd_queue); |
df0f2ba1 | 8037 | } |
beecf6a1 | 8038 | mock_input = 0; |
30690496 DL |
8039 | orig_local_map = get_local_map (PT, current_buffer, local_map); |
8040 | orig_keymap = get_local_map (PT, current_buffer, keymap); | |
beecf6a1 KH |
8041 | goto replay_sequence; |
8042 | } | |
bded54dd | 8043 | #endif |
8c907a56 GM |
8044 | key = read_char (NILP (prompt), nmaps, |
8045 | (Lisp_Object *) submaps, last_nonmenu_event, | |
beecf6a1 KH |
8046 | &used_mouse_menu); |
8047 | } | |
284f4730 | 8048 | |
dcc408a0 RS |
8049 | /* read_char returns t when it shows a menu and the user rejects it. |
8050 | Just return -1. */ | |
8051 | if (EQ (key, Qt)) | |
7d18f9ae RS |
8052 | { |
8053 | unbind_to (count, Qnil); | |
8054 | return -1; | |
8055 | } | |
dcc408a0 | 8056 | |
f4255cd1 | 8057 | /* read_char returns -1 at the end of a macro. |
284f4730 JB |
8058 | Emacs 18 handles this by returning immediately with a |
8059 | zero, so that's what we'll do. */ | |
8c18cbfb | 8060 | if (INTEGERP (key) && XINT (key) == -1) |
cd21b839 | 8061 | { |
f4255cd1 | 8062 | t = 0; |
bc536d84 RS |
8063 | /* The Microsoft C compiler can't handle the goto that |
8064 | would go here. */ | |
309b0fc8 | 8065 | dummyflag = 1; |
bc536d84 | 8066 | break; |
cd21b839 | 8067 | } |
df0f2ba1 | 8068 | |
3cb81011 KH |
8069 | /* If the current buffer has been changed from under us, the |
8070 | keymap may have changed, so replay the sequence. */ | |
8c18cbfb | 8071 | if (BUFFERP (key)) |
3cb81011 KH |
8072 | { |
8073 | mock_input = t; | |
f571ae0d RS |
8074 | /* Reset the current buffer from the selected window |
8075 | in case something changed the former and not the latter. | |
8076 | This is to be more consistent with the behavior | |
8077 | of the command_loop_1. */ | |
8078 | if (fix_current_buffer) | |
a94a4335 | 8079 | { |
788f89eb | 8080 | if (! FRAME_LIVE_P (XFRAME (selected_frame))) |
a94a4335 KH |
8081 | Fkill_emacs (Qnil); |
8082 | if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer) | |
8083 | Fset_buffer (XWINDOW (selected_window)->buffer); | |
8084 | } | |
f571ae0d | 8085 | |
30690496 DL |
8086 | orig_local_map = get_local_map (PT, current_buffer, local_map); |
8087 | orig_keymap = get_local_map (PT, current_buffer, keymap); | |
3cb81011 KH |
8088 | goto replay_sequence; |
8089 | } | |
8090 | ||
3b9189f8 RS |
8091 | /* If we have a quit that was typed in another frame, and |
8092 | quit_throw_to_read_char switched buffers, | |
8093 | replay to get the right keymap. */ | |
9343ab07 | 8094 | if (XINT (key) == quit_char && current_buffer != starting_buffer) |
3b9189f8 | 8095 | { |
7d18f9ae RS |
8096 | GROW_RAW_KEYBUF; |
8097 | XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key; | |
3b9189f8 RS |
8098 | keybuf[t++] = key; |
8099 | mock_input = t; | |
8100 | Vquit_flag = Qnil; | |
30690496 DL |
8101 | orig_local_map = get_local_map (PT, current_buffer, local_map); |
8102 | orig_keymap = get_local_map (PT, current_buffer, keymap); | |
3b9189f8 RS |
8103 | goto replay_sequence; |
8104 | } | |
3cb81011 | 8105 | |
284f4730 | 8106 | Vquit_flag = Qnil; |
7d18f9ae RS |
8107 | |
8108 | if (EVENT_HAS_PARAMETERS (key) | |
8109 | && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame)) | |
8110 | { | |
8111 | /* If we're at the beginning of a key sequence, and the caller | |
8112 | says it's okay, go ahead and return this event. If we're | |
8113 | in the midst of a key sequence, delay it until the end. */ | |
8114 | if (t > 0 || !can_return_switch_frame) | |
8115 | { | |
8116 | delayed_switch_frame = key; | |
8117 | goto replay_key; | |
8118 | } | |
8119 | } | |
8120 | ||
8121 | GROW_RAW_KEYBUF; | |
8122 | XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key; | |
7e85b935 | 8123 | } |
284f4730 | 8124 | |
df0f2ba1 | 8125 | /* Clicks in non-text areas get prefixed by the symbol |
7e85b935 RS |
8126 | in their CHAR-ADDRESS field. For example, a click on |
8127 | the mode line is prefixed by the symbol `mode-line'. | |
8128 | ||
8129 | Furthermore, key sequences beginning with mouse clicks | |
8130 | are read using the keymaps of the buffer clicked on, not | |
8131 | the current buffer. So we may have to switch the buffer | |
8132 | here. | |
8133 | ||
8134 | When we turn one event into two events, we must make sure | |
8135 | that neither of the two looks like the original--so that, | |
8136 | if we replay the events, they won't be expanded again. | |
8137 | If not for this, such reexpansion could happen either here | |
8138 | or when user programs play with this-command-keys. */ | |
8139 | if (EVENT_HAS_PARAMETERS (key)) | |
8140 | { | |
9b8eb840 | 8141 | Lisp_Object kind; |
cca310da | 8142 | |
9b8eb840 | 8143 | kind = EVENT_HEAD_KIND (EVENT_HEAD (key)); |
7e85b935 | 8144 | if (EQ (kind, Qmouse_click)) |
0a7f1fc0 | 8145 | { |
9b8eb840 | 8146 | Lisp_Object window, posn; |
f4255cd1 | 8147 | |
9b8eb840 KH |
8148 | window = POSN_WINDOW (EVENT_START (key)); |
8149 | posn = POSN_BUFFER_POSN (EVENT_START (key)); | |
7ee32cda | 8150 | |
8c18cbfb | 8151 | if (CONSP (posn)) |
0a7f1fc0 | 8152 | { |
7e85b935 RS |
8153 | /* We're looking at the second event of a |
8154 | sequence which we expanded before. Set | |
8155 | last_real_key_start appropriately. */ | |
8156 | if (t > 0) | |
8157 | last_real_key_start = t - 1; | |
cd21b839 | 8158 | } |
7e85b935 RS |
8159 | |
8160 | /* Key sequences beginning with mouse clicks are | |
8161 | read using the keymaps in the buffer clicked on, | |
8162 | not the current buffer. If we're at the | |
8163 | beginning of a key sequence, switch buffers. */ | |
8164 | if (last_real_key_start == 0 | |
8c18cbfb KH |
8165 | && WINDOWP (window) |
8166 | && BUFFERP (XWINDOW (window)->buffer) | |
7e85b935 | 8167 | && XBUFFER (XWINDOW (window)->buffer) != current_buffer) |
cd21b839 | 8168 | { |
7d18f9ae | 8169 | XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key; |
7e85b935 RS |
8170 | keybuf[t] = key; |
8171 | mock_input = t + 1; | |
8172 | ||
8173 | /* Arrange to go back to the original buffer once we're | |
8174 | done reading the key sequence. Note that we can't | |
8175 | use save_excursion_{save,restore} here, because they | |
8176 | save point as well as the current buffer; we don't | |
8177 | want to save point, because redisplay may change it, | |
8178 | to accommodate a Fset_window_start or something. We | |
8179 | don't want to do this at the top of the function, | |
8180 | because we may get input from a subprocess which | |
8181 | wants to change the selected window and stuff (say, | |
8182 | emacsclient). */ | |
8183 | record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
8184 | ||
788f89eb | 8185 | if (! FRAME_LIVE_P (XFRAME (selected_frame))) |
a94a4335 | 8186 | Fkill_emacs (Qnil); |
30690496 DL |
8187 | set_buffer_internal (XBUFFER (XWINDOW |
8188 | (window)->buffer) | |
8189 | ); | |
8190 | orig_local_map = get_local_map (PT, current_buffer, | |
8191 | local_map); | |
8192 | orig_keymap = get_local_map (PT, current_buffer, keymap); | |
7e85b935 | 8193 | goto replay_sequence; |
0a7f1fc0 | 8194 | } |
7ee32cda | 8195 | |
e0dff5f6 RS |
8196 | /* For a mouse click, get the local text-property keymap |
8197 | of the place clicked on, rather than point. */ | |
7ee32cda | 8198 | if (last_real_key_start == 0 |
7539e11f | 8199 | && CONSP (XCDR (key)) |
e0dff5f6 | 8200 | && ! localized_local_map) |
5ec75a55 | 8201 | { |
e0dff5f6 RS |
8202 | Lisp_Object map_here, start, pos; |
8203 | ||
8204 | localized_local_map = 1; | |
8205 | start = EVENT_START (key); | |
7ee32cda | 8206 | |
7539e11f | 8207 | if (CONSP (start) && CONSP (XCDR (start))) |
e0dff5f6 RS |
8208 | { |
8209 | pos = POSN_BUFFER_POSN (start); | |
b78ce8fb RS |
8210 | if (INTEGERP (pos) |
8211 | && XINT (pos) >= BEG && XINT (pos) <= Z) | |
e0dff5f6 | 8212 | { |
30690496 DL |
8213 | map_here = get_local_map (XINT (pos), |
8214 | current_buffer, local_map); | |
e0dff5f6 RS |
8215 | if (!EQ (map_here, orig_local_map)) |
8216 | { | |
8217 | orig_local_map = map_here; | |
8218 | keybuf[t] = key; | |
8219 | mock_input = t + 1; | |
5ec75a55 | 8220 | |
30690496 DL |
8221 | goto replay_sequence; |
8222 | } | |
8223 | map_here = get_local_map (XINT (pos), | |
8224 | current_buffer, keymap); | |
8225 | if (!EQ (map_here, orig_keymap)) | |
8226 | { | |
8227 | orig_keymap = map_here; | |
8228 | keybuf[t] = key; | |
8229 | mock_input = t + 1; | |
8230 | ||
e0dff5f6 RS |
8231 | goto replay_sequence; |
8232 | } | |
8233 | } | |
8234 | } | |
8235 | } | |
8236 | ||
8237 | /* Expand mode-line and scroll-bar events into two events: | |
8238 | use posn as a fake prefix key. */ | |
8239 | if (SYMBOLP (posn)) | |
8240 | { | |
7e85b935 | 8241 | if (t + 1 >= bufsize) |
3fe8e9a2 | 8242 | error ("Key sequence too long"); |
7e85b935 RS |
8243 | keybuf[t] = posn; |
8244 | keybuf[t+1] = key; | |
8245 | mock_input = t + 2; | |
7ee32cda | 8246 | |
7e85b935 RS |
8247 | /* Zap the position in key, so we know that we've |
8248 | expanded it, and don't try to do so again. */ | |
8249 | POSN_BUFFER_POSN (EVENT_START (key)) | |
8250 | = Fcons (posn, Qnil); | |
7ee32cda GM |
8251 | |
8252 | /* If on a mode line string with a local keymap, | |
8253 | reconsider the key sequence with that keymap. */ | |
8254 | if (CONSP (POSN_STRING (EVENT_START (key)))) | |
8255 | { | |
30690496 | 8256 | Lisp_Object string, pos, map, map2; |
7ee32cda GM |
8257 | |
8258 | string = POSN_STRING (EVENT_START (key)); | |
8259 | pos = XCDR (string); | |
8260 | string = XCAR (string); | |
52e386c2 KR |
8261 | if (XINT (pos) >= 0 |
8262 | && XINT (pos) < XSTRING (string)->size) | |
30690496 DL |
8263 | { |
8264 | map = Fget_text_property (pos, Qlocal_map, string); | |
8265 | if (!NILP (map)) | |
8266 | orig_local_map = map; | |
8267 | map2 = Fget_text_property (pos, Qkeymap, string); | |
8268 | if (!NILP (map2)) | |
8269 | orig_keymap = map2; | |
8270 | if (!NILP (map) || !NILP (map2)) | |
8271 | goto replay_sequence; | |
8272 | } | |
7ee32cda GM |
8273 | } |
8274 | ||
7e85b935 | 8275 | goto replay_key; |
5ec75a55 | 8276 | } |
0a7f1fc0 | 8277 | } |
7539e11f | 8278 | else if (CONSP (XCDR (key)) |
7a80a6f6 | 8279 | && CONSP (EVENT_START (key)) |
7539e11f | 8280 | && CONSP (XCDR (EVENT_START (key)))) |
7e85b935 | 8281 | { |
9b8eb840 | 8282 | Lisp_Object posn; |
7e85b935 | 8283 | |
9b8eb840 | 8284 | posn = POSN_BUFFER_POSN (EVENT_START (key)); |
7e85b935 RS |
8285 | /* Handle menu-bar events: |
8286 | insert the dummy prefix event `menu-bar'. */ | |
9ea173e8 | 8287 | if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar)) |
7e85b935 RS |
8288 | { |
8289 | if (t + 1 >= bufsize) | |
3fe8e9a2 | 8290 | error ("Key sequence too long"); |
7e85b935 RS |
8291 | keybuf[t] = posn; |
8292 | keybuf[t+1] = key; | |
8293 | ||
8294 | /* Zap the position in key, so we know that we've | |
8295 | expanded it, and don't try to do so again. */ | |
8296 | POSN_BUFFER_POSN (EVENT_START (key)) | |
8297 | = Fcons (posn, Qnil); | |
8298 | ||
8299 | mock_input = t + 2; | |
8300 | goto replay_sequence; | |
8301 | } | |
8c18cbfb | 8302 | else if (CONSP (posn)) |
7e85b935 RS |
8303 | { |
8304 | /* We're looking at the second event of a | |
8305 | sequence which we expanded before. Set | |
8306 | last_real_key_start appropriately. */ | |
8307 | if (last_real_key_start == t && t > 0) | |
8308 | last_real_key_start = t - 1; | |
8309 | } | |
a6d53864 | 8310 | } |
284f4730 | 8311 | } |
f4255cd1 JB |
8312 | |
8313 | /* We have finally decided that KEY is something we might want | |
8314 | to look up. */ | |
284f4730 JB |
8315 | first_binding = (follow_key (key, |
8316 | nmaps - first_binding, | |
253598e4 | 8317 | submaps + first_binding, |
284f4730 | 8318 | defs + first_binding, |
4e50f26a | 8319 | submaps + first_binding) |
284f4730 | 8320 | + first_binding); |
0a7f1fc0 | 8321 | |
f4255cd1 | 8322 | /* If KEY wasn't bound, we'll try some fallbacks. */ |
0a7f1fc0 JB |
8323 | if (first_binding >= nmaps) |
8324 | { | |
9b8eb840 | 8325 | Lisp_Object head; |
0a7f1fc0 | 8326 | |
9b8eb840 | 8327 | head = EVENT_HEAD (key); |
24736fbc | 8328 | if (help_char_p (head) && t > 0) |
7e85b935 RS |
8329 | { |
8330 | read_key_sequence_cmd = Vprefix_help_command; | |
8331 | keybuf[t++] = key; | |
8332 | last_nonmenu_event = key; | |
bc536d84 RS |
8333 | /* The Microsoft C compiler can't handle the goto that |
8334 | would go here. */ | |
309b0fc8 | 8335 | dummyflag = 1; |
0d882d52 | 8336 | break; |
7e85b935 RS |
8337 | } |
8338 | ||
8c18cbfb | 8339 | if (SYMBOLP (head)) |
0a7f1fc0 | 8340 | { |
9b8eb840 KH |
8341 | Lisp_Object breakdown; |
8342 | int modifiers; | |
0a7f1fc0 | 8343 | |
9b8eb840 | 8344 | breakdown = parse_modifiers (head); |
7539e11f | 8345 | modifiers = XINT (XCAR (XCDR (breakdown))); |
559f9d04 RS |
8346 | /* Attempt to reduce an unbound mouse event to a simpler |
8347 | event that is bound: | |
8348 | Drags reduce to clicks. | |
8349 | Double-clicks reduce to clicks. | |
8350 | Triple-clicks reduce to double-clicks, then to clicks. | |
8351 | Down-clicks are eliminated. | |
8352 | Double-downs reduce to downs, then are eliminated. | |
8353 | Triple-downs reduce to double-downs, then to downs, | |
8354 | then are eliminated. */ | |
8355 | if (modifiers & (down_modifier | drag_modifier | |
8356 | | double_modifier | triple_modifier)) | |
0a7f1fc0 | 8357 | { |
559f9d04 RS |
8358 | while (modifiers & (down_modifier | drag_modifier |
8359 | | double_modifier | triple_modifier)) | |
fbcd35bd JB |
8360 | { |
8361 | Lisp_Object new_head, new_click; | |
8362 | if (modifiers & triple_modifier) | |
8363 | modifiers ^= (double_modifier | triple_modifier); | |
bc536d84 RS |
8364 | else if (modifiers & double_modifier) |
8365 | modifiers &= ~double_modifier; | |
8366 | else if (modifiers & drag_modifier) | |
8367 | modifiers &= ~drag_modifier; | |
559f9d04 RS |
8368 | else |
8369 | { | |
8370 | /* Dispose of this `down' event by simply jumping | |
8371 | back to replay_key, to get another event. | |
8372 | ||
8373 | Note that if this event came from mock input, | |
8374 | then just jumping back to replay_key will just | |
8375 | hand it to us again. So we have to wipe out any | |
8376 | mock input. | |
8377 | ||
8378 | We could delete keybuf[t] and shift everything | |
8379 | after that to the left by one spot, but we'd also | |
8380 | have to fix up any variable that points into | |
8381 | keybuf, and shifting isn't really necessary | |
8382 | anyway. | |
8383 | ||
8384 | Adding prefixes for non-textual mouse clicks | |
8385 | creates two characters of mock input, and both | |
8386 | must be thrown away. If we're only looking at | |
8387 | the prefix now, we can just jump back to | |
8388 | replay_key. On the other hand, if we've already | |
8389 | processed the prefix, and now the actual click | |
8390 | itself is giving us trouble, then we've lost the | |
8391 | state of the keymaps we want to backtrack to, and | |
8392 | we need to replay the whole sequence to rebuild | |
8393 | it. | |
8394 | ||
8395 | Beyond that, only function key expansion could | |
8396 | create more than two keys, but that should never | |
8397 | generate mouse events, so it's okay to zero | |
8398 | mock_input in that case too. | |
8399 | ||
8400 | Isn't this just the most wonderful code ever? */ | |
8401 | if (t == last_real_key_start) | |
8402 | { | |
8403 | mock_input = 0; | |
8404 | goto replay_key; | |
8405 | } | |
8406 | else | |
8407 | { | |
8408 | mock_input = last_real_key_start; | |
8409 | goto replay_sequence; | |
8410 | } | |
8411 | } | |
8412 | ||
27203ead | 8413 | new_head |
7539e11f | 8414 | = apply_modifiers (modifiers, XCAR (breakdown)); |
27203ead RS |
8415 | new_click |
8416 | = Fcons (new_head, Fcons (EVENT_START (key), Qnil)); | |
fbcd35bd JB |
8417 | |
8418 | /* Look for a binding for this new key. follow_key | |
8419 | promises that it didn't munge submaps the | |
8420 | last time we called it, since key was unbound. */ | |
27203ead RS |
8421 | first_binding |
8422 | = (follow_key (new_click, | |
8423 | nmaps - local_first_binding, | |
8424 | submaps + local_first_binding, | |
8425 | defs + local_first_binding, | |
4e50f26a | 8426 | submaps + local_first_binding) |
27203ead | 8427 | + local_first_binding); |
fbcd35bd JB |
8428 | |
8429 | /* If that click is bound, go for it. */ | |
8430 | if (first_binding < nmaps) | |
8431 | { | |
8432 | key = new_click; | |
8433 | break; | |
8434 | } | |
8435 | /* Otherwise, we'll leave key set to the drag event. */ | |
8436 | } | |
0a7f1fc0 JB |
8437 | } |
8438 | } | |
8439 | } | |
8440 | ||
284f4730 | 8441 | keybuf[t++] = key; |
7d6de002 RS |
8442 | /* Normally, last_nonmenu_event gets the previous key we read. |
8443 | But when a mouse popup menu is being used, | |
8444 | we don't update last_nonmenu_event; it continues to hold the mouse | |
8445 | event that preceded the first level of menu. */ | |
8446 | if (!used_mouse_menu) | |
8447 | last_nonmenu_event = key; | |
284f4730 | 8448 | |
6321824f RS |
8449 | /* Record what part of this_command_keys is the current key sequence. */ |
8450 | this_single_command_key_start = this_command_key_count - t; | |
8451 | ||
3fe8e9a2 RS |
8452 | prev_fkey_map = fkey_map; |
8453 | prev_fkey_start = fkey_start; | |
8454 | prev_fkey_end = fkey_end; | |
8455 | ||
8456 | prev_keytran_map = keytran_map; | |
8457 | prev_keytran_start = keytran_start; | |
8458 | prev_keytran_end = keytran_end; | |
8459 | ||
284f4730 | 8460 | /* If the sequence is unbound, see if we can hang a function key |
253598e4 JB |
8461 | off the end of it. We only want to scan real keyboard input |
8462 | for function key sequences, so if mock_input says that we're | |
f4255cd1 | 8463 | re-reading old events, don't examine it. */ |
4e50f26a | 8464 | if (first_binding >= nmaps |
253598e4 | 8465 | && t >= mock_input) |
284f4730 JB |
8466 | { |
8467 | Lisp_Object fkey_next; | |
8468 | ||
e9bf89a0 RS |
8469 | /* Continue scan from fkey_end until we find a bound suffix. |
8470 | If we fail, increment fkey_start | |
8471 | and start fkey_end from there. */ | |
284f4730 JB |
8472 | while (fkey_end < t) |
8473 | { | |
f4255cd1 JB |
8474 | Lisp_Object key; |
8475 | ||
8476 | key = keybuf[fkey_end++]; | |
e74fbc70 | 8477 | fkey_next |
db785038 | 8478 | = access_keymap (fkey_map, key, 1, 0, 1); |
c700c260 RS |
8479 | |
8480 | /* Handle symbol with autoload definition. */ | |
0afc861b RS |
8481 | if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next)) |
8482 | && CONSP (XSYMBOL (fkey_next)->function) | |
7539e11f | 8483 | && EQ (XCAR (XSYMBOL (fkey_next)->function), Qautoload)) |
0afc861b RS |
8484 | do_autoload (XSYMBOL (fkey_next)->function, |
8485 | fkey_next); | |
c700c260 RS |
8486 | |
8487 | /* Handle a symbol whose function definition is a keymap | |
8488 | or an array. */ | |
0afc861b RS |
8489 | if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next)) |
8490 | && (!NILP (Farrayp (XSYMBOL (fkey_next)->function)) | |
02067692 | 8491 | || KEYMAPP (XSYMBOL (fkey_next)->function))) |
0afc861b | 8492 | fkey_next = XSYMBOL (fkey_next)->function; |
067ffa38 | 8493 | |
7a80a6f6 RS |
8494 | #if 0 /* I didn't turn this on, because it might cause trouble |
8495 | for the mapping of return into C-m and tab into C-i. */ | |
8496 | /* Optionally don't map function keys into other things. | |
8497 | This enables the user to redefine kp- keys easily. */ | |
8498 | if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping)) | |
8499 | fkey_next = Qnil; | |
8500 | #endif | |
8501 | ||
1abe6abe RS |
8502 | /* If the function key map gives a function, not an |
8503 | array, then call the function with no args and use | |
8504 | its value instead. */ | |
8505 | if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next)) | |
8506 | && fkey_end == t) | |
8507 | { | |
8508 | struct gcpro gcpro1, gcpro2, gcpro3; | |
8509 | Lisp_Object tem; | |
8510 | tem = fkey_next; | |
8511 | ||
8512 | GCPRO3 (fkey_map, keytran_map, delayed_switch_frame); | |
84d91fda | 8513 | fkey_next = call1 (fkey_next, prompt); |
1abe6abe RS |
8514 | UNGCPRO; |
8515 | /* If the function returned something invalid, | |
8516 | barf--don't ignore it. | |
df0f2ba1 | 8517 | (To ignore it safely, we would need to gcpro a bunch of |
1abe6abe RS |
8518 | other variables.) */ |
8519 | if (! (VECTORP (fkey_next) || STRINGP (fkey_next))) | |
6321824f | 8520 | error ("Function in key-translation-map returns invalid key sequence"); |
1abe6abe RS |
8521 | } |
8522 | ||
e9bf89a0 RS |
8523 | function_key_possible = ! NILP (fkey_next); |
8524 | ||
85bc5181 | 8525 | /* If keybuf[fkey_start..fkey_end] is bound in the |
a764a753 | 8526 | function key map and it's a suffix of the current |
85bc5181 | 8527 | sequence (i.e. fkey_end == t), replace it with |
a764a753 | 8528 | the binding and restart with fkey_start at the end. */ |
f5ea6163 | 8529 | if ((VECTORP (fkey_next) || STRINGP (fkey_next)) |
284f4730 JB |
8530 | && fkey_end == t) |
8531 | { | |
2e864a76 | 8532 | int len = XFASTINT (Flength (fkey_next)); |
f5ea6163 JB |
8533 | |
8534 | t = fkey_start + len; | |
284f4730 | 8535 | if (t >= bufsize) |
3fe8e9a2 | 8536 | error ("Key sequence too long"); |
284f4730 | 8537 | |
f5ea6163 JB |
8538 | if (VECTORP (fkey_next)) |
8539 | bcopy (XVECTOR (fkey_next)->contents, | |
8540 | keybuf + fkey_start, | |
8541 | (t - fkey_start) * sizeof (keybuf[0])); | |
8542 | else if (STRINGP (fkey_next)) | |
8543 | { | |
8544 | int i; | |
8545 | ||
8546 | for (i = 0; i < len; i++) | |
bb9e9bed KH |
8547 | XSETFASTINT (keybuf[fkey_start + i], |
8548 | XSTRING (fkey_next)->data[i]); | |
f5ea6163 | 8549 | } |
df0f2ba1 | 8550 | |
284f4730 JB |
8551 | mock_input = t; |
8552 | fkey_start = fkey_end = t; | |
32e6d806 | 8553 | fkey_map = Vfunction_key_map; |
284f4730 | 8554 | |
3bd0dffb KH |
8555 | /* Do pass the results through key-translation-map. |
8556 | But don't retranslate what key-translation-map | |
8557 | has already translated. */ | |
8558 | keytran_end = keytran_start; | |
00a78037 RS |
8559 | keytran_map = Vkey_translation_map; |
8560 | ||
f4255cd1 | 8561 | goto replay_sequence; |
284f4730 | 8562 | } |
df0f2ba1 | 8563 | |
02067692 | 8564 | fkey_map = get_keymap (fkey_next, 0, 1); |
284f4730 | 8565 | |
df0f2ba1 | 8566 | /* If we no longer have a bound suffix, try a new positions for |
a764a753 | 8567 | fkey_start. */ |
02067692 | 8568 | if (!CONSP (fkey_map)) |
284f4730 JB |
8569 | { |
8570 | fkey_end = ++fkey_start; | |
8571 | fkey_map = Vfunction_key_map; | |
e9bf89a0 | 8572 | function_key_possible = 0; |
284f4730 JB |
8573 | } |
8574 | } | |
8575 | } | |
a612e298 RS |
8576 | |
8577 | /* Look for this sequence in key-translation-map. */ | |
8578 | { | |
8579 | Lisp_Object keytran_next; | |
8580 | ||
8581 | /* Scan from keytran_end until we find a bound suffix. */ | |
8582 | while (keytran_end < t) | |
8583 | { | |
8584 | Lisp_Object key; | |
8585 | ||
8586 | key = keybuf[keytran_end++]; | |
a612e298 | 8587 | keytran_next |
db785038 | 8588 | = access_keymap (keytran_map, key, 1, 0, 1); |
a612e298 | 8589 | |
c700c260 | 8590 | /* Handle symbol with autoload definition. */ |
e83f6bb0 RS |
8591 | if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next)) |
8592 | && CONSP (XSYMBOL (keytran_next)->function) | |
7539e11f | 8593 | && EQ (XCAR (XSYMBOL (keytran_next)->function), Qautoload)) |
e83f6bb0 RS |
8594 | do_autoload (XSYMBOL (keytran_next)->function, |
8595 | keytran_next); | |
8596 | ||
c700c260 RS |
8597 | /* Handle a symbol whose function definition is a keymap |
8598 | or an array. */ | |
e83f6bb0 | 8599 | if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next)) |
c700c260 | 8600 | && (!NILP (Farrayp (XSYMBOL (keytran_next)->function)) |
02067692 | 8601 | || KEYMAPP (XSYMBOL (keytran_next)->function))) |
e83f6bb0 RS |
8602 | keytran_next = XSYMBOL (keytran_next)->function; |
8603 | ||
1abe6abe | 8604 | /* If the key translation map gives a function, not an |
e83f6bb0 | 8605 | array, then call the function with one arg and use |
1abe6abe RS |
8606 | its value instead. */ |
8607 | if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next)) | |
8608 | && keytran_end == t) | |
8609 | { | |
8610 | struct gcpro gcpro1, gcpro2, gcpro3; | |
8611 | Lisp_Object tem; | |
8612 | tem = keytran_next; | |
8613 | ||
40932d1a | 8614 | GCPRO3 (fkey_map, keytran_map, delayed_switch_frame); |
84d91fda | 8615 | keytran_next = call1 (keytran_next, prompt); |
1abe6abe RS |
8616 | UNGCPRO; |
8617 | /* If the function returned something invalid, | |
8618 | barf--don't ignore it. | |
df0f2ba1 | 8619 | (To ignore it safely, we would need to gcpro a bunch of |
1abe6abe RS |
8620 | other variables.) */ |
8621 | if (! (VECTORP (keytran_next) || STRINGP (keytran_next))) | |
40932d1a | 8622 | error ("Function in key-translation-map returns invalid key sequence"); |
1abe6abe RS |
8623 | } |
8624 | ||
00a78037 RS |
8625 | key_translation_possible = ! NILP (keytran_next); |
8626 | ||
a612e298 | 8627 | /* If keybuf[keytran_start..keytran_end] is bound in the |
1abe6abe | 8628 | key translation map and it's a suffix of the current |
a612e298 RS |
8629 | sequence (i.e. keytran_end == t), replace it with |
8630 | the binding and restart with keytran_start at the end. */ | |
8631 | if ((VECTORP (keytran_next) || STRINGP (keytran_next)) | |
8632 | && keytran_end == t) | |
8633 | { | |
2e864a76 | 8634 | int len = XFASTINT (Flength (keytran_next)); |
a612e298 RS |
8635 | |
8636 | t = keytran_start + len; | |
8637 | if (t >= bufsize) | |
3fe8e9a2 | 8638 | error ("Key sequence too long"); |
a612e298 RS |
8639 | |
8640 | if (VECTORP (keytran_next)) | |
8641 | bcopy (XVECTOR (keytran_next)->contents, | |
8642 | keybuf + keytran_start, | |
8643 | (t - keytran_start) * sizeof (keybuf[0])); | |
8644 | else if (STRINGP (keytran_next)) | |
8645 | { | |
8646 | int i; | |
8647 | ||
8648 | for (i = 0; i < len; i++) | |
bb9e9bed KH |
8649 | XSETFASTINT (keybuf[keytran_start + i], |
8650 | XSTRING (keytran_next)->data[i]); | |
a612e298 RS |
8651 | } |
8652 | ||
8653 | mock_input = t; | |
8654 | keytran_start = keytran_end = t; | |
8655 | keytran_map = Vkey_translation_map; | |
8656 | ||
00a78037 RS |
8657 | /* Don't pass the results of key-translation-map |
8658 | through function-key-map. */ | |
8659 | fkey_start = fkey_end = t; | |
3bd0dffb | 8660 | fkey_map = Vfunction_key_map; |
00a78037 | 8661 | |
a612e298 RS |
8662 | goto replay_sequence; |
8663 | } | |
8664 | ||
02067692 | 8665 | keytran_map = get_keymap (keytran_next, 0, 1); |
a612e298 | 8666 | |
df0f2ba1 | 8667 | /* If we no longer have a bound suffix, try a new positions for |
a612e298 | 8668 | keytran_start. */ |
02067692 | 8669 | if (!CONSP (keytran_map)) |
a612e298 RS |
8670 | { |
8671 | keytran_end = ++keytran_start; | |
8672 | keytran_map = Vkey_translation_map; | |
00a78037 | 8673 | key_translation_possible = 0; |
a612e298 RS |
8674 | } |
8675 | } | |
8676 | } | |
4e50f26a RS |
8677 | |
8678 | /* If KEY is not defined in any of the keymaps, | |
8679 | and cannot be part of a function key or translation, | |
8680 | and is an upper case letter | |
8681 | use the corresponding lower-case letter instead. */ | |
8682 | if (first_binding == nmaps && ! function_key_possible | |
00a78037 | 8683 | && ! key_translation_possible |
8c18cbfb | 8684 | && INTEGERP (key) |
4e50f26a | 8685 | && ((((XINT (key) & 0x3ffff) |
301738ed | 8686 | < XCHAR_TABLE (current_buffer->downcase_table)->size) |
4e50f26a RS |
8687 | && UPPERCASEP (XINT (key) & 0x3ffff)) |
8688 | || (XINT (key) & shift_modifier))) | |
8689 | { | |
569871d2 | 8690 | Lisp_Object new_key; |
569871d2 | 8691 | |
309b0fc8 RS |
8692 | original_uppercase = key; |
8693 | original_uppercase_position = t - 1; | |
8694 | ||
831f35a2 | 8695 | if (XINT (key) & shift_modifier) |
569871d2 | 8696 | XSETINT (new_key, XINT (key) & ~shift_modifier); |
4e50f26a | 8697 | else |
569871d2 RS |
8698 | XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff) |
8699 | | (XINT (key) & ~0x3ffff))); | |
8700 | ||
3fe8e9a2 RS |
8701 | /* We have to do this unconditionally, regardless of whether |
8702 | the lower-case char is defined in the keymaps, because they | |
8703 | might get translated through function-key-map. */ | |
8704 | keybuf[t - 1] = new_key; | |
8705 | mock_input = t; | |
8706 | ||
8707 | fkey_map = prev_fkey_map; | |
8708 | fkey_start = prev_fkey_start; | |
8709 | fkey_end = prev_fkey_end; | |
8710 | ||
8711 | keytran_map = prev_keytran_map; | |
8712 | keytran_start = prev_keytran_start; | |
8713 | keytran_end = prev_keytran_end; | |
8714 | ||
8715 | goto replay_sequence; | |
4e50f26a | 8716 | } |
ef8fd672 RS |
8717 | /* If KEY is not defined in any of the keymaps, |
8718 | and cannot be part of a function key or translation, | |
8719 | and is a shifted function key, | |
8720 | use the corresponding unshifted function key instead. */ | |
8721 | if (first_binding == nmaps && ! function_key_possible | |
8722 | && ! key_translation_possible | |
8723 | && SYMBOLP (key)) | |
8724 | { | |
8725 | Lisp_Object breakdown; | |
8726 | int modifiers; | |
8727 | ||
8728 | breakdown = parse_modifiers (key); | |
7539e11f | 8729 | modifiers = XINT (XCAR (XCDR (breakdown))); |
ef8fd672 RS |
8730 | if (modifiers & shift_modifier) |
8731 | { | |
569871d2 | 8732 | Lisp_Object new_key; |
3fe8e9a2 RS |
8733 | |
8734 | original_uppercase = key; | |
8735 | original_uppercase_position = t - 1; | |
ef8fd672 | 8736 | |
569871d2 RS |
8737 | modifiers &= ~shift_modifier; |
8738 | new_key = apply_modifiers (modifiers, | |
7539e11f | 8739 | XCAR (breakdown)); |
569871d2 | 8740 | |
3fe8e9a2 RS |
8741 | keybuf[t - 1] = new_key; |
8742 | mock_input = t; | |
8743 | ||
8744 | fkey_map = prev_fkey_map; | |
8745 | fkey_start = prev_fkey_start; | |
8746 | fkey_end = prev_fkey_end; | |
8747 | ||
8748 | keytran_map = prev_keytran_map; | |
8749 | keytran_start = prev_keytran_start; | |
8750 | keytran_end = prev_keytran_end; | |
8751 | ||
8752 | goto replay_sequence; | |
ef8fd672 RS |
8753 | } |
8754 | } | |
284f4730 JB |
8755 | } |
8756 | ||
309b0fc8 | 8757 | if (!dummyflag) |
bc536d84 RS |
8758 | read_key_sequence_cmd = (first_binding < nmaps |
8759 | ? defs[first_binding] | |
8760 | : Qnil); | |
284f4730 | 8761 | |
cd21b839 | 8762 | unread_switch_frame = delayed_switch_frame; |
f4255cd1 | 8763 | unbind_to (count, Qnil); |
07f76a14 | 8764 | |
3fe8e9a2 RS |
8765 | /* Don't downcase the last character if the caller says don't. |
8766 | Don't downcase it if the result is undefined, either. */ | |
8767 | if ((dont_downcase_last || first_binding >= nmaps) | |
8768 | && t - 1 == original_uppercase_position) | |
309b0fc8 RS |
8769 | keybuf[t - 1] = original_uppercase; |
8770 | ||
07f76a14 JB |
8771 | /* Occasionally we fabricate events, perhaps by expanding something |
8772 | according to function-key-map, or by adding a prefix symbol to a | |
8773 | mouse click in the scroll bar or modeline. In this cases, return | |
8774 | the entire generated key sequence, even if we hit an unbound | |
8775 | prefix or a definition before the end. This means that you will | |
8776 | be able to push back the event properly, and also means that | |
8777 | read-key-sequence will always return a logical unit. | |
8778 | ||
8779 | Better ideas? */ | |
cca310da JB |
8780 | for (; t < mock_input; t++) |
8781 | { | |
f2647d04 DL |
8782 | if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes)) |
8783 | && NILP (Fzerop (Vecho_keystrokes))) | |
a98ea3f9 | 8784 | echo_char (keybuf[t]); |
cca310da JB |
8785 | add_command_key (keybuf[t]); |
8786 | } | |
07f76a14 | 8787 | |
7d18f9ae RS |
8788 | |
8789 | ||
284f4730 JB |
8790 | return t; |
8791 | } | |
8792 | ||
a612e298 RS |
8793 | #if 0 /* This doc string is too long for some compilers. |
8794 | This commented-out definition serves for DOC. */ | |
ce98e608 | 8795 | DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0, |
284f4730 JB |
8796 | "Read a sequence of keystrokes and return as a string or vector.\n\ |
8797 | The sequence is sufficient to specify a non-prefix command in the\n\ | |
8798 | current local and global maps.\n\ | |
8799 | \n\ | |
c0a58692 RS |
8800 | First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\ |
8801 | Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\ | |
8802 | as a continuation of the previous key.\n\ | |
284f4730 | 8803 | \n\ |
309b0fc8 RS |
8804 | The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\ |
8805 | convert the last event to lower case. (Normally any upper case event\n\ | |
8806 | is converted to lower case if the original event is undefined and the lower\n\ | |
8807 | case equivalent is defined.) A non-nil value is appropriate for reading\n\ | |
8808 | a key sequence to be defined.\n\ | |
8809 | \n\ | |
cb5df6ae JB |
8810 | A C-g typed while in this function is treated like any other character,\n\ |
8811 | and `quit-flag' is not set.\n\ | |
8812 | \n\ | |
8813 | If the key sequence starts with a mouse click, then the sequence is read\n\ | |
8814 | using the keymaps of the buffer of the window clicked in, not the buffer\n\ | |
8815 | of the selected window as normal.\n\ | |
ede41463 | 8816 | ""\n\ |
cb5df6ae JB |
8817 | `read-key-sequence' drops unbound button-down events, since you normally\n\ |
8818 | only care about the click or drag events which follow them. If a drag\n\ | |
fbcd35bd JB |
8819 | or multi-click event is unbound, but the corresponding click event would\n\ |
8820 | be bound, `read-key-sequence' turns the event into a click event at the\n\ | |
cb5df6ae | 8821 | drag's starting position. This means that you don't have to distinguish\n\ |
fbcd35bd | 8822 | between click and drag, double, or triple events unless you want to.\n\ |
cb5df6ae JB |
8823 | \n\ |
8824 | `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\ | |
3c370943 JB |
8825 | lines separating windows, and scroll bars with imaginary keys\n\ |
8826 | `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\ | |
cb5df6ae | 8827 | \n\ |
ce98e608 KH |
8828 | Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\ |
8829 | function will process a switch-frame event if the user switches frames\n\ | |
8830 | before typing anything. If the user switches frames in the middle of a\n\ | |
8831 | key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\ | |
8832 | is nil, then the event will be put off until after the current key sequence.\n\ | |
cb5df6ae JB |
8833 | \n\ |
8834 | `read-key-sequence' checks `function-key-map' for function key\n\ | |
8835 | sequences, where they wouldn't conflict with ordinary bindings. See\n\ | |
d5eecefb RS |
8836 | `function-key-map' for more details.\n\ |
8837 | \n\ | |
8838 | The optional fifth argument COMMAND-LOOP, if non-nil, means\n\ | |
8839 | that this key sequence is being read by something that will\n\ | |
8840 | read commands one after another. It should be nil if the caller\n\ | |
8841 | will read just one key sequence.") | |
b638f328 | 8842 | (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop) |
a612e298 RS |
8843 | #endif |
8844 | ||
d5eecefb | 8845 | DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0, |
a612e298 | 8846 | 0) |
d5eecefb RS |
8847 | (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, |
8848 | command_loop) | |
309b0fc8 | 8849 | Lisp_Object prompt, continue_echo, dont_downcase_last; |
d5eecefb | 8850 | Lisp_Object can_return_switch_frame, command_loop; |
284f4730 JB |
8851 | { |
8852 | Lisp_Object keybuf[30]; | |
8853 | register int i; | |
03cee6ae | 8854 | struct gcpro gcpro1; |
d5eecefb | 8855 | int count = specpdl_ptr - specpdl; |
284f4730 JB |
8856 | |
8857 | if (!NILP (prompt)) | |
8858 | CHECK_STRING (prompt, 0); | |
8859 | QUIT; | |
8860 | ||
d5eecefb RS |
8861 | specbind (Qinput_method_exit_on_first_char, |
8862 | (NILP (command_loop) ? Qt : Qnil)); | |
8863 | specbind (Qinput_method_use_echo_area, | |
8864 | (NILP (command_loop) ? Qt : Qnil)); | |
8865 | ||
284f4730 JB |
8866 | bzero (keybuf, sizeof keybuf); |
8867 | GCPRO1 (keybuf[0]); | |
8868 | gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0])); | |
8869 | ||
daa37602 | 8870 | if (NILP (continue_echo)) |
6321824f RS |
8871 | { |
8872 | this_command_key_count = 0; | |
8873 | this_single_command_key_start = 0; | |
8874 | } | |
c0a58692 | 8875 | |
d0c48478 GM |
8876 | #ifdef HAVE_X_WINDOWS |
8877 | if (display_busy_cursor_p) | |
8878 | cancel_busy_cursor (); | |
8879 | #endif | |
8880 | ||
309b0fc8 | 8881 | i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])), |
ce98e608 | 8882 | prompt, ! NILP (dont_downcase_last), |
f571ae0d | 8883 | ! NILP (can_return_switch_frame), 0); |
284f4730 | 8884 | |
ae18aa3b GM |
8885 | #if 0 /* The following is fine for code reading a key sequence and |
8886 | then proceeding with a lenghty compuation, but it's not good | |
8887 | for code reading keys in a loop, like an input method. */ | |
d0c48478 GM |
8888 | #ifdef HAVE_X_WINDOWS |
8889 | if (display_busy_cursor_p) | |
8890 | start_busy_cursor (); | |
ae18aa3b | 8891 | #endif |
d0c48478 GM |
8892 | #endif |
8893 | ||
dcc408a0 RS |
8894 | if (i == -1) |
8895 | { | |
8896 | Vquit_flag = Qt; | |
8897 | QUIT; | |
8898 | } | |
284f4730 | 8899 | UNGCPRO; |
d5eecefb | 8900 | return unbind_to (count, make_event_array (i, keybuf)); |
284f4730 | 8901 | } |
e39da3d7 RS |
8902 | |
8903 | DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector, | |
d5eecefb | 8904 | Sread_key_sequence_vector, 1, 5, 0, |
e39da3d7 | 8905 | "Like `read-key-sequence' but always return a vector.") |
d5eecefb RS |
8906 | (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, |
8907 | command_loop) | |
e39da3d7 | 8908 | Lisp_Object prompt, continue_echo, dont_downcase_last; |
d5eecefb | 8909 | Lisp_Object can_return_switch_frame, command_loop; |
e39da3d7 RS |
8910 | { |
8911 | Lisp_Object keybuf[30]; | |
8912 | register int i; | |
03cee6ae | 8913 | struct gcpro gcpro1; |
d5eecefb | 8914 | int count = specpdl_ptr - specpdl; |
e39da3d7 RS |
8915 | |
8916 | if (!NILP (prompt)) | |
8917 | CHECK_STRING (prompt, 0); | |
8918 | QUIT; | |
8919 | ||
d5eecefb RS |
8920 | specbind (Qinput_method_exit_on_first_char, |
8921 | (NILP (command_loop) ? Qt : Qnil)); | |
8922 | specbind (Qinput_method_use_echo_area, | |
8923 | (NILP (command_loop) ? Qt : Qnil)); | |
8924 | ||
e39da3d7 RS |
8925 | bzero (keybuf, sizeof keybuf); |
8926 | GCPRO1 (keybuf[0]); | |
8927 | gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0])); | |
8928 | ||
8929 | if (NILP (continue_echo)) | |
8930 | { | |
8931 | this_command_key_count = 0; | |
8932 | this_single_command_key_start = 0; | |
8933 | } | |
8934 | ||
d0c48478 GM |
8935 | #ifdef HAVE_X_WINDOWS |
8936 | if (display_busy_cursor_p) | |
8937 | cancel_busy_cursor (); | |
8938 | #endif | |
8939 | ||
e39da3d7 RS |
8940 | i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])), |
8941 | prompt, ! NILP (dont_downcase_last), | |
8942 | ! NILP (can_return_switch_frame), 0); | |
8943 | ||
d0c48478 GM |
8944 | #ifdef HAVE_X_WINDOWS |
8945 | if (display_busy_cursor_p) | |
8946 | start_busy_cursor (); | |
8947 | #endif | |
8948 | ||
e39da3d7 RS |
8949 | if (i == -1) |
8950 | { | |
8951 | Vquit_flag = Qt; | |
8952 | QUIT; | |
8953 | } | |
8954 | UNGCPRO; | |
d5eecefb | 8955 | return unbind_to (count, Fvector (i, keybuf)); |
e39da3d7 | 8956 | } |
284f4730 | 8957 | \f |
158f7532 | 8958 | DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0, |
284f4730 JB |
8959 | "Execute CMD as an editor command.\n\ |
8960 | CMD must be a symbol that satisfies the `commandp' predicate.\n\ | |
8961 | Optional second arg RECORD-FLAG non-nil\n\ | |
8962 | means unconditionally put this command in `command-history'.\n\ | |
aaf2ead7 RS |
8963 | Otherwise, that is done only if an arg is read using the minibuffer.\n\ |
8964 | The argument KEYS specifies the value to use instead of (this-command-keys)\n\ | |
6321824f | 8965 | when reading the arguments; if it is nil, (this-command-keys) is used.\n\ |
158f7532 RS |
8966 | The argument SPECIAL, if non-nil, means that this command is executing\n\ |
8967 | a special event, so ignore the prefix argument and don't clear it.") | |
8968 | (cmd, record_flag, keys, special) | |
8969 | Lisp_Object cmd, record_flag, keys, special; | |
284f4730 JB |
8970 | { |
8971 | register Lisp_Object final; | |
8972 | register Lisp_Object tem; | |
8973 | Lisp_Object prefixarg; | |
8974 | struct backtrace backtrace; | |
8975 | extern int debug_on_next_call; | |
8976 | ||
284f4730 JB |
8977 | debug_on_next_call = 0; |
8978 | ||
158f7532 RS |
8979 | if (NILP (special)) |
8980 | { | |
8981 | prefixarg = current_kboard->Vprefix_arg; | |
8982 | Vcurrent_prefix_arg = prefixarg; | |
8983 | current_kboard->Vprefix_arg = Qnil; | |
8984 | } | |
8985 | else | |
8986 | prefixarg = Qnil; | |
8987 | ||
8c18cbfb | 8988 | if (SYMBOLP (cmd)) |
284f4730 JB |
8989 | { |
8990 | tem = Fget (cmd, Qdisabled); | |
88ce066e | 8991 | if (!NILP (tem) && !NILP (Vrun_hooks)) |
b78ce8fb RS |
8992 | { |
8993 | tem = Fsymbol_value (Qdisabled_command_hook); | |
8994 | if (!NILP (tem)) | |
8995 | return call1 (Vrun_hooks, Qdisabled_command_hook); | |
8996 | } | |
284f4730 JB |
8997 | } |
8998 | ||
01e26217 | 8999 | while (1) |
284f4730 | 9000 | { |
ffd56f97 | 9001 | final = Findirect_function (cmd); |
284f4730 JB |
9002 | |
9003 | if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload))) | |
b516a185 RS |
9004 | { |
9005 | struct gcpro gcpro1, gcpro2; | |
9006 | ||
9007 | GCPRO2 (cmd, prefixarg); | |
9008 | do_autoload (final, cmd); | |
9009 | UNGCPRO; | |
9010 | } | |
284f4730 JB |
9011 | else |
9012 | break; | |
9013 | } | |
9014 | ||
8c18cbfb | 9015 | if (STRINGP (final) || VECTORP (final)) |
284f4730 JB |
9016 | { |
9017 | /* If requested, place the macro in the command history. For | |
9018 | other sorts of commands, call-interactively takes care of | |
9019 | this. */ | |
e57d8fd8 | 9020 | if (!NILP (record_flag)) |
f4385381 RS |
9021 | { |
9022 | Vcommand_history | |
9023 | = Fcons (Fcons (Qexecute_kbd_macro, | |
9024 | Fcons (final, Fcons (prefixarg, Qnil))), | |
9025 | Vcommand_history); | |
9026 | ||
9027 | /* Don't keep command history around forever. */ | |
9028 | if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0) | |
9029 | { | |
9030 | tem = Fnthcdr (Vhistory_length, Vcommand_history); | |
9031 | if (CONSP (tem)) | |
7539e11f | 9032 | XCDR (tem) = Qnil; |
f4385381 RS |
9033 | } |
9034 | } | |
284f4730 JB |
9035 | |
9036 | return Fexecute_kbd_macro (final, prefixarg); | |
9037 | } | |
f4385381 | 9038 | |
8c18cbfb | 9039 | if (CONSP (final) || SUBRP (final) || COMPILEDP (final)) |
284f4730 JB |
9040 | { |
9041 | backtrace.next = backtrace_list; | |
9042 | backtrace_list = &backtrace; | |
9043 | backtrace.function = &Qcall_interactively; | |
9044 | backtrace.args = &cmd; | |
9045 | backtrace.nargs = 1; | |
9046 | backtrace.evalargs = 0; | |
9047 | ||
e57d8fd8 | 9048 | tem = Fcall_interactively (cmd, record_flag, keys); |
284f4730 JB |
9049 | |
9050 | backtrace_list = backtrace.next; | |
9051 | return tem; | |
9052 | } | |
9053 | return Qnil; | |
9054 | } | |
9055 | \f | |
284f4730 JB |
9056 | DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command, |
9057 | 1, 1, "P", | |
9058 | "Read function name, then read its arguments and call it.") | |
9059 | (prefixarg) | |
9060 | Lisp_Object prefixarg; | |
9061 | { | |
9062 | Lisp_Object function; | |
9063 | char buf[40]; | |
9064 | Lisp_Object saved_keys; | |
5434fce6 | 9065 | Lisp_Object bindings, value; |
214360e9 | 9066 | struct gcpro gcpro1, gcpro2; |
284f4730 | 9067 | |
b0f2a7bf KH |
9068 | saved_keys = Fvector (this_command_key_count, |
9069 | XVECTOR (this_command_keys)->contents); | |
284f4730 | 9070 | buf[0] = 0; |
fde7aff8 | 9071 | GCPRO2 (saved_keys, prefixarg); |
284f4730 JB |
9072 | |
9073 | if (EQ (prefixarg, Qminus)) | |
9074 | strcpy (buf, "- "); | |
7539e11f | 9075 | else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4) |
284f4730 | 9076 | strcpy (buf, "C-u "); |
7539e11f | 9077 | else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg))) |
5d5b907f RS |
9078 | { |
9079 | if (sizeof (int) == sizeof (EMACS_INT)) | |
7539e11f | 9080 | sprintf (buf, "%d ", XINT (XCAR (prefixarg))); |
5d5b907f | 9081 | else if (sizeof (long) == sizeof (EMACS_INT)) |
7539e11f | 9082 | sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg))); |
5d5b907f RS |
9083 | else |
9084 | abort (); | |
9085 | } | |
8c18cbfb | 9086 | else if (INTEGERP (prefixarg)) |
5d5b907f RS |
9087 | { |
9088 | if (sizeof (int) == sizeof (EMACS_INT)) | |
9089 | sprintf (buf, "%d ", XINT (prefixarg)); | |
9090 | else if (sizeof (long) == sizeof (EMACS_INT)) | |
03cee6ae | 9091 | sprintf (buf, "%ld ", (long) XINT (prefixarg)); |
5d5b907f RS |
9092 | else |
9093 | abort (); | |
9094 | } | |
284f4730 JB |
9095 | |
9096 | /* This isn't strictly correct if execute-extended-command | |
9097 | is bound to anything else. Perhaps it should use | |
9098 | this_command_keys? */ | |
9099 | strcat (buf, "M-x "); | |
9100 | ||
9101 | /* Prompt with buf, and then read a string, completing from and | |
9102 | restricting to the set of all defined commands. Don't provide | |
51763820 | 9103 | any initial input. Save the command read on the extended-command |
03b4122a | 9104 | history list. */ |
284f4730 JB |
9105 | function = Fcompleting_read (build_string (buf), |
9106 | Vobarray, Qcommandp, | |
4328577a KH |
9107 | Qt, Qnil, Qextended_command_history, Qnil, |
9108 | Qnil); | |
284f4730 | 9109 | |
1f5b1641 RS |
9110 | if (STRINGP (function) && XSTRING (function)->size == 0) |
9111 | error ("No command name given"); | |
9112 | ||
1113d9db JB |
9113 | /* Set this_command_keys to the concatenation of saved_keys and |
9114 | function, followed by a RET. */ | |
284f4730 | 9115 | { |
1113d9db | 9116 | struct Lisp_String *str; |
b0f2a7bf | 9117 | Lisp_Object *keys; |
284f4730 | 9118 | int i; |
284f4730 | 9119 | |
1113d9db | 9120 | this_command_key_count = 0; |
6321824f | 9121 | this_single_command_key_start = 0; |
1113d9db | 9122 | |
b0f2a7bf KH |
9123 | keys = XVECTOR (saved_keys)->contents; |
9124 | for (i = 0; i < XVECTOR (saved_keys)->size; i++) | |
9125 | add_command_key (keys[i]); | |
1113d9db JB |
9126 | |
9127 | str = XSTRING (function); | |
9128 | for (i = 0; i < str->size; i++) | |
301738ed | 9129 | add_command_key (Faref (function, make_number (i))); |
1113d9db | 9130 | |
301738ed | 9131 | add_command_key (make_number ('\015')); |
284f4730 JB |
9132 | } |
9133 | ||
9134 | UNGCPRO; | |
9135 | ||
0a7f1fc0 | 9136 | function = Fintern (function, Qnil); |
d8bcf58e | 9137 | current_kboard->Vprefix_arg = prefixarg; |
d5eecefb RS |
9138 | Vthis_command = function; |
9139 | real_this_command = function; | |
284f4730 | 9140 | |
6526ab49 RS |
9141 | /* If enabled, show which key runs this command. */ |
9142 | if (!NILP (Vsuggest_key_bindings) | |
71012575 | 9143 | && NILP (Vexecuting_macro) |
6526ab49 | 9144 | && SYMBOLP (function)) |
5434fce6 RS |
9145 | bindings = Fwhere_is_internal (function, Voverriding_local_map, |
9146 | Qt, Qnil); | |
9147 | else | |
9148 | bindings = Qnil; | |
6526ab49 | 9149 | |
5434fce6 RS |
9150 | value = Qnil; |
9151 | GCPRO2 (bindings, value); | |
9152 | value = Fcommand_execute (function, Qt, Qnil, Qnil); | |
6526ab49 | 9153 | |
5434fce6 | 9154 | /* If the command has a key binding, print it now. */ |
3ababa60 | 9155 | if (!NILP (bindings) |
ee112567 KH |
9156 | && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)), |
9157 | Qmouse_movement))) | |
5434fce6 RS |
9158 | { |
9159 | /* But first wait, and skip the message if there is input. */ | |
426939cc | 9160 | int delay_time; |
985f9f66 | 9161 | if (!NILP (echo_area_buffer[0])) |
426939cc RS |
9162 | /* This command displayed something in the echo area; |
9163 | so wait a few seconds, then display our suggestion message. */ | |
9164 | delay_time = (NUMBERP (Vsuggest_key_bindings) | |
9165 | ? XINT (Vsuggest_key_bindings) : 2); | |
9166 | else | |
9167 | /* This command left the echo area empty, | |
9168 | so display our message immediately. */ | |
9169 | delay_time = 0; | |
9170 | ||
9171 | if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil)) | |
303b5b3f | 9172 | && ! CONSP (Vunread_command_events)) |
6526ab49 | 9173 | { |
5434fce6 RS |
9174 | Lisp_Object binding; |
9175 | char *newmessage; | |
985f9f66 | 9176 | int message_p = push_message (); |
5434fce6 RS |
9177 | |
9178 | binding = Fkey_description (bindings); | |
9179 | ||
9180 | newmessage | |
9181 | = (char *) alloca (XSYMBOL (function)->name->size | |
fc932ac6 | 9182 | + STRING_BYTES (XSTRING (binding)) |
5434fce6 | 9183 | + 100); |
3ababa60 | 9184 | sprintf (newmessage, "You can run the command `%s' with %s", |
6526ab49 | 9185 | XSYMBOL (function)->name->data, |
5434fce6 | 9186 | XSTRING (binding)->data); |
301738ed RS |
9187 | message2_nolog (newmessage, |
9188 | strlen (newmessage), | |
9189 | STRING_MULTIBYTE (binding)); | |
5434fce6 RS |
9190 | if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings) |
9191 | ? Vsuggest_key_bindings : make_number (2)), | |
985f9f66 GM |
9192 | Qnil, Qnil)) |
9193 | && message_p) | |
9194 | restore_message (); | |
9195 | ||
9196 | pop_message (); | |
6526ab49 RS |
9197 | } |
9198 | } | |
9199 | ||
5434fce6 | 9200 | RETURN_UNGCPRO (value); |
284f4730 | 9201 | } |
6526ab49 RS |
9202 | |
9203 | /* Find the set of keymaps now active. | |
9204 | Store into *MAPS_P a vector holding the various maps | |
9205 | and return the number of them. The vector was malloc'd | |
9206 | and the caller should free it. */ | |
9207 | ||
9208 | int | |
9209 | current_active_maps (maps_p) | |
9210 | Lisp_Object **maps_p; | |
9211 | { | |
9212 | Lisp_Object *tmaps, *maps; | |
9213 | int nmaps; | |
9214 | ||
9215 | /* Should overriding-terminal-local-map and overriding-local-map apply? */ | |
9216 | if (!NILP (Voverriding_local_map_menu_flag)) | |
9217 | { | |
9218 | /* Yes, use them (if non-nil) as well as the global map. */ | |
9219 | maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0])); | |
9220 | nmaps = 0; | |
9221 | if (!NILP (current_kboard->Voverriding_terminal_local_map)) | |
9222 | maps[nmaps++] = current_kboard->Voverriding_terminal_local_map; | |
9223 | if (!NILP (Voverriding_local_map)) | |
9224 | maps[nmaps++] = Voverriding_local_map; | |
9225 | } | |
9226 | else | |
9227 | { | |
30690496 DL |
9228 | /* No, so use major and minor mode keymaps and keymap property. */ |
9229 | int extra_maps = 2; | |
9230 | Lisp_Object map = get_local_map (PT, current_buffer, keymap); | |
9231 | if (!NILP (map)) | |
9232 | extra_maps = 3; | |
6526ab49 | 9233 | nmaps = current_minor_maps (NULL, &tmaps); |
30690496 DL |
9234 | maps = (Lisp_Object *) alloca ((nmaps + extra_maps) |
9235 | * sizeof (maps[0])); | |
6526ab49 | 9236 | bcopy (tmaps, maps, nmaps * sizeof (maps[0])); |
30690496 | 9237 | if (!NILP (map)) |
9f83cf0b | 9238 | maps[nmaps++] = map; |
30690496 | 9239 | maps[nmaps++] = get_local_map (PT, current_buffer, local_map); |
6526ab49 RS |
9240 | } |
9241 | maps[nmaps++] = current_global_map; | |
9242 | ||
9243 | *maps_p = maps; | |
9244 | return nmaps; | |
9245 | } | |
284f4730 | 9246 | \f |
d9d4c147 | 9247 | /* Return nonzero if input events are pending. */ |
284f4730 | 9248 | |
dfcf069d | 9249 | int |
284f4730 JB |
9250 | detect_input_pending () |
9251 | { | |
9252 | if (!input_pending) | |
d9d4c147 KH |
9253 | get_input_pending (&input_pending, 0); |
9254 | ||
9255 | return input_pending; | |
9256 | } | |
9257 | ||
b1878f45 | 9258 | /* Return nonzero if input events are pending, and run any pending timers. */ |
d9d4c147 | 9259 | |
dfcf069d | 9260 | int |
87dd9b9b RS |
9261 | detect_input_pending_run_timers (do_display) |
9262 | int do_display; | |
d9d4c147 | 9263 | { |
87dd9b9b RS |
9264 | int old_timers_run = timers_run; |
9265 | ||
d9d4c147 KH |
9266 | if (!input_pending) |
9267 | get_input_pending (&input_pending, 1); | |
284f4730 | 9268 | |
87dd9b9b | 9269 | if (old_timers_run != timers_run && do_display) |
7ee32cda GM |
9270 | { |
9271 | redisplay_preserve_echo_area (); | |
9272 | /* The following fixes a bug when using lazy-lock with | |
9273 | lazy-lock-defer-on-the-fly set to t, i.e. when fontifying | |
9274 | from an idle timer function. The symptom of the bug is that | |
9275 | the cursor sometimes doesn't become visible until the next X | |
9276 | event is processed. --gerd. */ | |
9277 | if (rif) | |
9278 | rif->flush_display (NULL); | |
9279 | } | |
87dd9b9b | 9280 | |
284f4730 JB |
9281 | return input_pending; |
9282 | } | |
9283 | ||
ffd56f97 JB |
9284 | /* This is called in some cases before a possible quit. |
9285 | It cases the next call to detect_input_pending to recompute input_pending. | |
9286 | So calling this function unnecessarily can't do any harm. */ | |
07a59269 KH |
9287 | |
9288 | void | |
ffd56f97 JB |
9289 | clear_input_pending () |
9290 | { | |
9291 | input_pending = 0; | |
9292 | } | |
9293 | ||
b1878f45 RS |
9294 | /* Return nonzero if there are pending requeued events. |
9295 | This isn't used yet. The hope is to make wait_reading_process_input | |
9296 | call it, and return return if it runs Lisp code that unreads something. | |
9297 | The problem is, kbd_buffer_get_event needs to be fixed to know what | |
9298 | to do in that case. It isn't trivial. */ | |
9299 | ||
dfcf069d | 9300 | int |
b1878f45 RS |
9301 | requeued_events_pending_p () |
9302 | { | |
9303 | return (!NILP (Vunread_command_events) || unread_command_char != -1); | |
9304 | } | |
9305 | ||
9306 | ||
284f4730 JB |
9307 | DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0, |
9308 | "T if command input is currently available with no waiting.\n\ | |
9309 | Actually, the value is nil only if we can be sure that no input is available.") | |
9310 | () | |
9311 | { | |
24597608 | 9312 | if (!NILP (Vunread_command_events) || unread_command_char != -1) |
284f4730 JB |
9313 | return (Qt); |
9314 | ||
d9d4c147 KH |
9315 | get_input_pending (&input_pending, 1); |
9316 | return input_pending > 0 ? Qt : Qnil; | |
284f4730 JB |
9317 | } |
9318 | ||
9319 | DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0, | |
22d7cb89 | 9320 | "Return vector of last 100 events, not counting those from keyboard macros.") |
284f4730 JB |
9321 | () |
9322 | { | |
5160df46 | 9323 | Lisp_Object *keys = XVECTOR (recent_keys)->contents; |
284f4730 JB |
9324 | Lisp_Object val; |
9325 | ||
9326 | if (total_keys < NUM_RECENT_KEYS) | |
5160df46 | 9327 | return Fvector (total_keys, keys); |
284f4730 JB |
9328 | else |
9329 | { | |
5160df46 JB |
9330 | val = Fvector (NUM_RECENT_KEYS, keys); |
9331 | bcopy (keys + recent_keys_index, | |
284f4730 JB |
9332 | XVECTOR (val)->contents, |
9333 | (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object)); | |
5160df46 | 9334 | bcopy (keys, |
284f4730 JB |
9335 | XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index, |
9336 | recent_keys_index * sizeof (Lisp_Object)); | |
9337 | return val; | |
9338 | } | |
9339 | } | |
9340 | ||
9341 | DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0, | |
e5f920d7 RS |
9342 | "Return the key sequence that invoked this command.\n\ |
9343 | The value is a string or a vector.") | |
284f4730 JB |
9344 | () |
9345 | { | |
86e5706b RS |
9346 | return make_event_array (this_command_key_count, |
9347 | XVECTOR (this_command_keys)->contents); | |
284f4730 JB |
9348 | } |
9349 | ||
e39da3d7 RS |
9350 | DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0, |
9351 | "Return the key sequence that invoked this command, as a vector.") | |
9352 | () | |
9353 | { | |
9354 | return Fvector (this_command_key_count, | |
9355 | XVECTOR (this_command_keys)->contents); | |
9356 | } | |
9357 | ||
6321824f RS |
9358 | DEFUN ("this-single-command-keys", Fthis_single_command_keys, |
9359 | Sthis_single_command_keys, 0, 0, 0, | |
9360 | "Return the key sequence that invoked this command.\n\ | |
9361 | Unlike `this-command-keys', this function's value\n\ | |
9362 | does not include prefix arguments.\n\ | |
e39da3d7 | 9363 | The value is always a vector.") |
6321824f RS |
9364 | () |
9365 | { | |
e39da3d7 RS |
9366 | return Fvector (this_command_key_count |
9367 | - this_single_command_key_start, | |
9368 | (XVECTOR (this_command_keys)->contents | |
9369 | + this_single_command_key_start)); | |
6321824f RS |
9370 | } |
9371 | ||
7d18f9ae RS |
9372 | DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys, |
9373 | Sthis_single_command_raw_keys, 0, 0, 0, | |
9374 | "Return the raw events that were read for this command.\n\ | |
9375 | Unlike `this-single-command-keys', this function's value\n\ | |
9376 | shows the events before all translations (except for input methods).\n\ | |
9377 | The value is always a vector.") | |
9378 | () | |
9379 | { | |
9380 | return Fvector (raw_keybuf_count, | |
9381 | (XVECTOR (raw_keybuf)->contents)); | |
9382 | } | |
9383 | ||
71918b75 RS |
9384 | DEFUN ("reset-this-command-lengths", Freset_this_command_lengths, |
9385 | Sreset_this_command_lengths, 0, 0, 0, | |
9386 | "Used for complicated reasons in `universal-argument-other-key'.\n\ | |
9387 | \n\ | |
9388 | `universal-argument-other-key' rereads the event just typed.\n\ | |
9389 | It then gets translated through `function-key-map'.\n\ | |
9390 | The translated event gets included in the echo area and in\n\ | |
9391 | the value of `this-command-keys' in addition to the raw original event.\n\ | |
9392 | That is not right.\n\ | |
9393 | \n\ | |
9394 | Calling this function directs the translated event to replace\n\ | |
9395 | the original event, so that only one version of the event actually\n\ | |
9396 | appears in the echo area and in the value of `this-command-keys.'.") | |
9397 | () | |
9398 | { | |
9399 | before_command_restore_flag = 1; | |
9400 | before_command_key_count_1 = before_command_key_count; | |
9401 | before_command_echo_length_1 = before_command_echo_length; | |
6e5742a0 | 9402 | return Qnil; |
71918b75 RS |
9403 | } |
9404 | ||
82e6e5af RS |
9405 | DEFUN ("clear-this-command-keys", Fclear_this_command_keys, |
9406 | Sclear_this_command_keys, 0, 0, 0, | |
e27b53f6 GM |
9407 | "Clear out the vector that `this-command-keys' returns.\n\ |
9408 | Clear vector containing last 100 events.") | |
82e6e5af RS |
9409 | () |
9410 | { | |
fb0dde6c GM |
9411 | int i; |
9412 | ||
82e6e5af | 9413 | this_command_key_count = 0; |
fb0dde6c GM |
9414 | |
9415 | for (i = 0; i < XVECTOR (recent_keys)->size; ++i) | |
9416 | XVECTOR (recent_keys)->contents[i] = Qnil; | |
9417 | total_keys = 0; | |
9418 | recent_keys_index = 0; | |
82e6e5af RS |
9419 | return Qnil; |
9420 | } | |
9421 | ||
284f4730 JB |
9422 | DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0, |
9423 | "Return the current depth in recursive edits.") | |
9424 | () | |
9425 | { | |
9426 | Lisp_Object temp; | |
bb9e9bed | 9427 | XSETFASTINT (temp, command_loop_level + minibuf_level); |
284f4730 JB |
9428 | return temp; |
9429 | } | |
9430 | ||
9431 | DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1, | |
9432 | "FOpen dribble file: ", | |
9b2471df RS |
9433 | "Start writing all keyboard characters to a dribble file called FILE.\n\ |
9434 | If FILE is nil, close any open dribble file.") | |
284f4730 JB |
9435 | (file) |
9436 | Lisp_Object file; | |
9437 | { | |
6cb52def | 9438 | if (dribble) |
284f4730 | 9439 | { |
6cb52def KH |
9440 | fclose (dribble); |
9441 | dribble = 0; | |
284f4730 | 9442 | } |
6cb52def | 9443 | if (!NILP (file)) |
284f4730 JB |
9444 | { |
9445 | file = Fexpand_file_name (file, Qnil); | |
9446 | dribble = fopen (XSTRING (file)->data, "w"); | |
ab6ca1de KH |
9447 | if (dribble == 0) |
9448 | report_file_error ("Opening dribble", Fcons (file, Qnil)); | |
284f4730 JB |
9449 | } |
9450 | return Qnil; | |
9451 | } | |
9452 | ||
9453 | DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0, | |
9454 | "Discard the contents of the terminal input buffer.\n\ | |
9455 | Also cancel any kbd macro being defined.") | |
9456 | () | |
9457 | { | |
c5fdd383 | 9458 | current_kboard->defining_kbd_macro = Qnil; |
284f4730 JB |
9459 | update_mode_lines++; |
9460 | ||
24597608 | 9461 | Vunread_command_events = Qnil; |
86e5706b | 9462 | unread_command_char = -1; |
284f4730 JB |
9463 | |
9464 | discard_tty_input (); | |
9465 | ||
7ee32cda | 9466 | kbd_fetch_ptr = kbd_store_ptr; |
da8f7368 | 9467 | Ffillarray (kbd_buffer_gcpro, Qnil); |
284f4730 JB |
9468 | input_pending = 0; |
9469 | ||
9470 | return Qnil; | |
9471 | } | |
9472 | \f | |
9473 | DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "", | |
9474 | "Stop Emacs and return to superior process. You can resume later.\n\ | |
8026024c KH |
9475 | If `cannot-suspend' is non-nil, or if the system doesn't support job\n\ |
9476 | control, run a subshell instead.\n\n\ | |
284f4730 | 9477 | If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\ |
b7d2ebbf RS |
9478 | to be read as terminal input by Emacs's parent, after suspension.\n\ |
9479 | \n\ | |
bbdc2092 RS |
9480 | Before suspending, run the normal hook `suspend-hook'.\n\ |
9481 | After resumption run the normal hook `suspend-resume-hook'.\n\ | |
284f4730 JB |
9482 | \n\ |
9483 | Some operating systems cannot stop the Emacs process and resume it later.\n\ | |
b7d2ebbf | 9484 | On such systems, Emacs starts a subshell instead of suspending.") |
284f4730 JB |
9485 | (stuffstring) |
9486 | Lisp_Object stuffstring; | |
9487 | { | |
284f4730 JB |
9488 | int count = specpdl_ptr - specpdl; |
9489 | int old_height, old_width; | |
9490 | int width, height; | |
03cee6ae | 9491 | struct gcpro gcpro1; |
284f4730 JB |
9492 | |
9493 | if (!NILP (stuffstring)) | |
9494 | CHECK_STRING (stuffstring, 0); | |
284f4730 | 9495 | |
1e95ed28 JB |
9496 | /* Run the functions in suspend-hook. */ |
9497 | if (!NILP (Vrun_hooks)) | |
9498 | call1 (Vrun_hooks, intern ("suspend-hook")); | |
284f4730 | 9499 | |
b7d2ebbf | 9500 | GCPRO1 (stuffstring); |
ff11dfa1 | 9501 | get_frame_size (&old_width, &old_height); |
284f4730 JB |
9502 | reset_sys_modes (); |
9503 | /* sys_suspend can get an error if it tries to fork a subshell | |
9504 | and the system resources aren't available for that. */ | |
91a0da02 | 9505 | record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes, |
d52a7a92 | 9506 | Qnil); |
284f4730 | 9507 | stuff_buffered_input (stuffstring); |
8026024c KH |
9508 | if (cannot_suspend) |
9509 | sys_subshell (); | |
9510 | else | |
9511 | sys_suspend (); | |
284f4730 JB |
9512 | unbind_to (count, Qnil); |
9513 | ||
9514 | /* Check if terminal/window size has changed. | |
9515 | Note that this is not useful when we are running directly | |
9516 | with a window system; but suspend should be disabled in that case. */ | |
ff11dfa1 | 9517 | get_frame_size (&width, &height); |
284f4730 | 9518 | if (width != old_width || height != old_height) |
788f89eb | 9519 | change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0); |
284f4730 | 9520 | |
1e95ed28 | 9521 | /* Run suspend-resume-hook. */ |
284f4730 JB |
9522 | if (!NILP (Vrun_hooks)) |
9523 | call1 (Vrun_hooks, intern ("suspend-resume-hook")); | |
df0f2ba1 | 9524 | |
284f4730 JB |
9525 | UNGCPRO; |
9526 | return Qnil; | |
9527 | } | |
9528 | ||
9529 | /* If STUFFSTRING is a string, stuff its contents as pending terminal input. | |
eb8c3be9 | 9530 | Then in any case stuff anything Emacs has read ahead and not used. */ |
284f4730 | 9531 | |
07a59269 | 9532 | void |
284f4730 JB |
9533 | stuff_buffered_input (stuffstring) |
9534 | Lisp_Object stuffstring; | |
9535 | { | |
284f4730 | 9536 | /* stuff_char works only in BSD, versions 4.2 and up. */ |
6df54671 | 9537 | #ifdef BSD_SYSTEM |
284f4730 | 9538 | #ifndef BSD4_1 |
612b78ef | 9539 | register unsigned char *p; |
612b78ef | 9540 | |
8c18cbfb | 9541 | if (STRINGP (stuffstring)) |
284f4730 JB |
9542 | { |
9543 | register int count; | |
9544 | ||
9545 | p = XSTRING (stuffstring)->data; | |
fc932ac6 | 9546 | count = STRING_BYTES (XSTRING (stuffstring)); |
284f4730 JB |
9547 | while (count-- > 0) |
9548 | stuff_char (*p++); | |
9549 | stuff_char ('\n'); | |
9550 | } | |
da8f7368 | 9551 | |
284f4730 | 9552 | /* Anything we have read ahead, put back for the shell to read. */ |
beecf6a1 | 9553 | /* ?? What should this do when we have multiple keyboards?? |
c5fdd383 | 9554 | Should we ignore anything that was typed in at the "wrong" kboard? */ |
beecf6a1 | 9555 | for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++) |
284f4730 | 9556 | { |
da8f7368 GM |
9557 | int idx; |
9558 | ||
beecf6a1 KH |
9559 | if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE) |
9560 | kbd_fetch_ptr = kbd_buffer; | |
9561 | if (kbd_fetch_ptr->kind == ascii_keystroke) | |
9562 | stuff_char (kbd_fetch_ptr->code); | |
da8f7368 | 9563 | |
beecf6a1 | 9564 | kbd_fetch_ptr->kind = no_event; |
da8f7368 GM |
9565 | idx = 2 * (kbd_fetch_ptr - kbd_buffer); |
9566 | ASET (kbd_buffer_gcpro, idx, Qnil); | |
9567 | ASET (kbd_buffer_gcpro, idx + 1, Qnil); | |
284f4730 | 9568 | } |
da8f7368 | 9569 | |
284f4730 JB |
9570 | input_pending = 0; |
9571 | #endif | |
6df54671 | 9572 | #endif /* BSD_SYSTEM and not BSD4_1 */ |
284f4730 JB |
9573 | } |
9574 | \f | |
dfcf069d | 9575 | void |
ffd56f97 JB |
9576 | set_waiting_for_input (time_to_clear) |
9577 | EMACS_TIME *time_to_clear; | |
284f4730 | 9578 | { |
ffd56f97 | 9579 | input_available_clear_time = time_to_clear; |
284f4730 JB |
9580 | |
9581 | /* Tell interrupt_signal to throw back to read_char, */ | |
9582 | waiting_for_input = 1; | |
9583 | ||
9584 | /* If interrupt_signal was called before and buffered a C-g, | |
9585 | make it run again now, to avoid timing error. */ | |
9586 | if (!NILP (Vquit_flag)) | |
9587 | quit_throw_to_read_char (); | |
284f4730 JB |
9588 | } |
9589 | ||
07a59269 | 9590 | void |
284f4730 JB |
9591 | clear_waiting_for_input () |
9592 | { | |
9593 | /* Tell interrupt_signal not to throw back to read_char, */ | |
9594 | waiting_for_input = 0; | |
ffd56f97 | 9595 | input_available_clear_time = 0; |
284f4730 JB |
9596 | } |
9597 | ||
9598 | /* This routine is called at interrupt level in response to C-G. | |
d4e68eea GM |
9599 | |
9600 | If interrupt_input, this is the handler for SIGINT. Otherwise, it | |
9601 | is called from kbd_buffer_store_event, in handling SIGIO or | |
9602 | SIGTINT. | |
284f4730 | 9603 | |
d4e68eea GM |
9604 | If `waiting_for_input' is non zero, then unless `echoing' is |
9605 | nonzero, immediately throw back to read_char. | |
284f4730 | 9606 | |
d4e68eea GM |
9607 | Otherwise it sets the Lisp variable quit-flag not-nil. This causes |
9608 | eval to throw, when it gets a chance. If quit-flag is already | |
9609 | non-nil, it stops the job right away. */ | |
284f4730 JB |
9610 | |
9611 | SIGTYPE | |
91c049d4 RS |
9612 | interrupt_signal (signalnum) /* If we don't have an argument, */ |
9613 | int signalnum; /* some compilers complain in signal calls. */ | |
284f4730 JB |
9614 | { |
9615 | char c; | |
9616 | /* Must preserve main program's value of errno. */ | |
9617 | int old_errno = errno; | |
788f89eb | 9618 | struct frame *sf = SELECTED_FRAME (); |
284f4730 | 9619 | |
5970a8cb | 9620 | #if defined (USG) && !defined (POSIX_SIGNALS) |
7a80a6f6 RS |
9621 | if (!read_socket_hook && NILP (Vwindow_system)) |
9622 | { | |
9623 | /* USG systems forget handlers when they are used; | |
9624 | must reestablish each time */ | |
9625 | signal (SIGINT, interrupt_signal); | |
9626 | signal (SIGQUIT, interrupt_signal); | |
9627 | } | |
284f4730 JB |
9628 | #endif /* USG */ |
9629 | ||
9630 | cancel_echoing (); | |
9631 | ||
31e4e97b | 9632 | if (!NILP (Vquit_flag) |
788f89eb | 9633 | && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))) |
284f4730 | 9634 | { |
31e4e97b EZ |
9635 | /* If SIGINT isn't blocked, don't let us be interrupted by |
9636 | another SIGINT, it might be harmful due to non-reentrancy | |
9637 | in I/O functions. */ | |
9638 | sigblock (sigmask (SIGINT)); | |
9639 | ||
284f4730 JB |
9640 | fflush (stdout); |
9641 | reset_sys_modes (); | |
31e4e97b | 9642 | |
284f4730 JB |
9643 | #ifdef SIGTSTP /* Support possible in later USG versions */ |
9644 | /* | |
9645 | * On systems which can suspend the current process and return to the original | |
9646 | * shell, this command causes the user to end up back at the shell. | |
9647 | * The "Auto-save" and "Abort" questions are not asked until | |
9648 | * the user elects to return to emacs, at which point he can save the current | |
9649 | * job and either dump core or continue. | |
9650 | */ | |
9651 | sys_suspend (); | |
9652 | #else | |
9653 | #ifdef VMS | |
9654 | if (sys_suspend () == -1) | |
9655 | { | |
9656 | printf ("Not running as a subprocess;\n"); | |
9657 | printf ("you can continue or abort.\n"); | |
9658 | } | |
9659 | #else /* not VMS */ | |
9660 | /* Perhaps should really fork an inferior shell? | |
9661 | But that would not provide any way to get back | |
9662 | to the original shell, ever. */ | |
9663 | printf ("No support for stopping a process on this operating system;\n"); | |
9664 | printf ("you can continue or abort.\n"); | |
9665 | #endif /* not VMS */ | |
9666 | #endif /* not SIGTSTP */ | |
80e4aa30 RS |
9667 | #ifdef MSDOS |
9668 | /* We must remain inside the screen area when the internal terminal | |
9669 | is used. Note that [Enter] is not echoed by dos. */ | |
9670 | cursor_to (0, 0); | |
9671 | #endif | |
118d6ca9 RS |
9672 | /* It doesn't work to autosave while GC is in progress; |
9673 | the code used for auto-saving doesn't cope with the mark bit. */ | |
9674 | if (!gc_in_progress) | |
9fd7d808 | 9675 | { |
118d6ca9 RS |
9676 | printf ("Auto-save? (y or n) "); |
9677 | fflush (stdout); | |
9678 | if (((c = getchar ()) & ~040) == 'Y') | |
9679 | { | |
9680 | Fdo_auto_save (Qt, Qnil); | |
80e4aa30 | 9681 | #ifdef MSDOS |
118d6ca9 | 9682 | printf ("\r\nAuto-save done"); |
80e4aa30 | 9683 | #else /* not MSDOS */ |
118d6ca9 | 9684 | printf ("Auto-save done\n"); |
80e4aa30 | 9685 | #endif /* not MSDOS */ |
118d6ca9 RS |
9686 | } |
9687 | while (c != '\n') c = getchar (); | |
9fd7d808 | 9688 | } |
118d6ca9 RS |
9689 | else |
9690 | { | |
9691 | /* During GC, it must be safe to reenable quitting again. */ | |
9692 | Vinhibit_quit = Qnil; | |
9693 | #ifdef MSDOS | |
9694 | printf ("\r\n"); | |
9695 | #endif /* not MSDOS */ | |
9696 | printf ("Garbage collection in progress; cannot auto-save now\r\n"); | |
9697 | printf ("but will instead do a real quit after garbage collection ends\r\n"); | |
9698 | fflush (stdout); | |
9699 | } | |
9700 | ||
80e4aa30 RS |
9701 | #ifdef MSDOS |
9702 | printf ("\r\nAbort? (y or n) "); | |
9703 | #else /* not MSDOS */ | |
284f4730 JB |
9704 | #ifdef VMS |
9705 | printf ("Abort (and enter debugger)? (y or n) "); | |
9706 | #else /* not VMS */ | |
9707 | printf ("Abort (and dump core)? (y or n) "); | |
9708 | #endif /* not VMS */ | |
80e4aa30 | 9709 | #endif /* not MSDOS */ |
284f4730 JB |
9710 | fflush (stdout); |
9711 | if (((c = getchar ()) & ~040) == 'Y') | |
9712 | abort (); | |
9713 | while (c != '\n') c = getchar (); | |
80e4aa30 RS |
9714 | #ifdef MSDOS |
9715 | printf ("\r\nContinuing...\r\n"); | |
9716 | #else /* not MSDOS */ | |
284f4730 | 9717 | printf ("Continuing...\n"); |
80e4aa30 | 9718 | #endif /* not MSDOS */ |
284f4730 JB |
9719 | fflush (stdout); |
9720 | init_sys_modes (); | |
31e4e97b | 9721 | sigfree (); |
284f4730 JB |
9722 | } |
9723 | else | |
9724 | { | |
9725 | /* If executing a function that wants to be interrupted out of | |
9726 | and the user has not deferred quitting by binding `inhibit-quit' | |
9727 | then quit right away. */ | |
9728 | if (immediate_quit && NILP (Vinhibit_quit)) | |
9729 | { | |
e39da3d7 RS |
9730 | struct gl_state_s saved; |
9731 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
9732 | ||
284f4730 JB |
9733 | immediate_quit = 0; |
9734 | sigfree (); | |
e39da3d7 RS |
9735 | saved = gl_state; |
9736 | GCPRO4 (saved.object, saved.global_code, | |
9737 | saved.current_syntax_table, saved.old_prop); | |
284f4730 | 9738 | Fsignal (Qquit, Qnil); |
e39da3d7 RS |
9739 | gl_state = saved; |
9740 | UNGCPRO; | |
284f4730 JB |
9741 | } |
9742 | else | |
9743 | /* Else request quit when it's safe */ | |
9744 | Vquit_flag = Qt; | |
9745 | } | |
9746 | ||
9747 | if (waiting_for_input && !echoing) | |
9748 | quit_throw_to_read_char (); | |
9749 | ||
9750 | errno = old_errno; | |
9751 | } | |
9752 | ||
9753 | /* Handle a C-g by making read_char return C-g. */ | |
9754 | ||
07a59269 | 9755 | void |
284f4730 JB |
9756 | quit_throw_to_read_char () |
9757 | { | |
284f4730 JB |
9758 | sigfree (); |
9759 | /* Prevent another signal from doing this before we finish. */ | |
f76475ad | 9760 | clear_waiting_for_input (); |
284f4730 JB |
9761 | input_pending = 0; |
9762 | ||
24597608 | 9763 | Vunread_command_events = Qnil; |
86e5706b | 9764 | unread_command_char = -1; |
284f4730 | 9765 | |
087feab3 RS |
9766 | #if 0 /* Currently, sit_for is called from read_char without turning |
9767 | off polling. And that can call set_waiting_for_input. | |
9768 | It seems to be harmless. */ | |
e6b01c14 JB |
9769 | #ifdef POLL_FOR_INPUT |
9770 | /* May be > 1 if in recursive minibuffer. */ | |
9771 | if (poll_suppress_count == 0) | |
9772 | abort (); | |
9773 | #endif | |
087feab3 | 9774 | #endif |
4c52b668 | 9775 | if (FRAMEP (internal_last_event_frame) |
788f89eb | 9776 | && !EQ (internal_last_event_frame, selected_frame)) |
719191cf RS |
9777 | do_switch_frame (make_lispy_switch_frame (internal_last_event_frame), |
9778 | Qnil, 0); | |
e6b01c14 | 9779 | |
284f4730 JB |
9780 | _longjmp (getcjmp, 1); |
9781 | } | |
9782 | \f | |
9783 | DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0, | |
9784 | "Set mode of reading keyboard input.\n\ | |
464f8898 RS |
9785 | First arg INTERRUPT non-nil means use input interrupts;\n\ |
9786 | nil means use CBREAK mode.\n\ | |
9787 | Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\ | |
284f4730 | 9788 | (no effect except in CBREAK mode).\n\ |
b04904fb RS |
9789 | Third arg META t means accept 8-bit input (for a Meta key).\n\ |
9790 | META nil means ignore the top bit, on the assumption it is parity.\n\ | |
9791 | Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\ | |
a8ee7ef9 RS |
9792 | Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\ |
9793 | See also `current-input-mode'.") | |
284f4730 JB |
9794 | (interrupt, flow, meta, quit) |
9795 | Lisp_Object interrupt, flow, meta, quit; | |
9796 | { | |
9797 | if (!NILP (quit) | |
8c18cbfb | 9798 | && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400)) |
34f04431 RS |
9799 | error ("set-input-mode: QUIT must be an ASCII character"); |
9800 | ||
9801 | #ifdef POLL_FOR_INPUT | |
9802 | stop_polling (); | |
9803 | #endif | |
284f4730 | 9804 | |
07de30b9 | 9805 | #ifndef DOS_NT |
2ee250ec | 9806 | /* this causes startup screen to be restored and messes with the mouse */ |
284f4730 | 9807 | reset_sys_modes (); |
2ee250ec RS |
9808 | #endif |
9809 | ||
284f4730 JB |
9810 | #ifdef SIGIO |
9811 | /* Note SIGIO has been undef'd if FIONREAD is missing. */ | |
284f4730 | 9812 | if (read_socket_hook) |
9a0f60bb KH |
9813 | { |
9814 | /* When using X, don't give the user a real choice, | |
9815 | because we haven't implemented the mechanisms to support it. */ | |
9816 | #ifdef NO_SOCK_SIGIO | |
9817 | interrupt_input = 0; | |
9818 | #else /* not NO_SOCK_SIGIO */ | |
9819 | interrupt_input = 1; | |
284f4730 | 9820 | #endif /* NO_SOCK_SIGIO */ |
9a0f60bb KH |
9821 | } |
9822 | else | |
284f4730 JB |
9823 | interrupt_input = !NILP (interrupt); |
9824 | #else /* not SIGIO */ | |
9825 | interrupt_input = 0; | |
9826 | #endif /* not SIGIO */ | |
9a0f60bb | 9827 | |
284f4730 JB |
9828 | /* Our VMS input only works by interrupts, as of now. */ |
9829 | #ifdef VMS | |
9830 | interrupt_input = 1; | |
9831 | #endif | |
9a0f60bb | 9832 | |
284f4730 | 9833 | flow_control = !NILP (flow); |
b04904fb RS |
9834 | if (NILP (meta)) |
9835 | meta_key = 0; | |
9836 | else if (EQ (meta, Qt)) | |
9837 | meta_key = 1; | |
9838 | else | |
9839 | meta_key = 2; | |
284f4730 JB |
9840 | if (!NILP (quit)) |
9841 | /* Don't let this value be out of range. */ | |
9842 | quit_char = XINT (quit) & (meta_key ? 0377 : 0177); | |
9843 | ||
07de30b9 | 9844 | #ifndef DOS_NT |
284f4730 | 9845 | init_sys_modes (); |
2ee250ec | 9846 | #endif |
34f04431 RS |
9847 | |
9848 | #ifdef POLL_FOR_INPUT | |
9849 | poll_suppress_count = 1; | |
9850 | start_polling (); | |
9851 | #endif | |
284f4730 JB |
9852 | return Qnil; |
9853 | } | |
80645119 JB |
9854 | |
9855 | DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0, | |
9856 | "Return information about the way Emacs currently reads keyboard input.\n\ | |
9857 | The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\ | |
9858 | INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\ | |
9859 | nil, Emacs is using CBREAK mode.\n\ | |
9860 | FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\ | |
9861 | terminal; this does not apply if Emacs uses interrupt-driven input.\n\ | |
a8ee7ef9 RS |
9862 | META is t if accepting 8-bit input with 8th bit as Meta flag.\n\ |
9863 | META nil means ignoring the top bit, on the assumption it is parity.\n\ | |
9864 | META is neither t nor nil if accepting 8-bit input and using\n\ | |
9865 | all 8 bits as the character code.\n\ | |
80645119 JB |
9866 | QUIT is the character Emacs currently uses to quit.\n\ |
9867 | The elements of this list correspond to the arguments of\n\ | |
a8ee7ef9 | 9868 | `set-input-mode'.") |
80645119 JB |
9869 | () |
9870 | { | |
9871 | Lisp_Object val[4]; | |
9872 | ||
9873 | val[0] = interrupt_input ? Qt : Qnil; | |
9874 | val[1] = flow_control ? Qt : Qnil; | |
a8ee7ef9 | 9875 | val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil; |
bb9e9bed | 9876 | XSETFASTINT (val[3], quit_char); |
80645119 | 9877 | |
bf673a7a | 9878 | return Flist (sizeof (val) / sizeof (val[0]), val); |
80645119 JB |
9879 | } |
9880 | ||
284f4730 | 9881 | \f |
6c6083a9 | 9882 | /* |
c5fdd383 | 9883 | * Set up a new kboard object with reasonable initial values. |
6c6083a9 KH |
9884 | */ |
9885 | void | |
c5fdd383 KH |
9886 | init_kboard (kb) |
9887 | KBOARD *kb; | |
6c6083a9 | 9888 | { |
217258d5 | 9889 | kb->Voverriding_terminal_local_map = Qnil; |
6c7178b9 | 9890 | kb->Vlast_command = Qnil; |
75045dcb | 9891 | kb->Vreal_last_command = Qnil; |
d8bcf58e | 9892 | kb->Vprefix_arg = Qnil; |
75045dcb | 9893 | kb->Vlast_prefix_arg = Qnil; |
c5fdd383 KH |
9894 | kb->kbd_queue = Qnil; |
9895 | kb->kbd_queue_has_data = 0; | |
9896 | kb->immediate_echo = 0; | |
9897 | kb->echoptr = kb->echobuf; | |
9898 | kb->echo_after_prompt = -1; | |
9899 | kb->kbd_macro_buffer = 0; | |
9900 | kb->kbd_macro_bufsize = 0; | |
9901 | kb->defining_kbd_macro = Qnil; | |
9902 | kb->Vlast_kbd_macro = Qnil; | |
9903 | kb->reference_count = 0; | |
7c97ffdc | 9904 | kb->Vsystem_key_alist = Qnil; |
142e6c73 | 9905 | kb->system_key_syms = Qnil; |
9ba47203 | 9906 | kb->Vdefault_minibuffer_frame = Qnil; |
6c6083a9 KH |
9907 | } |
9908 | ||
9909 | /* | |
c5fdd383 | 9910 | * Destroy the contents of a kboard object, but not the object itself. |
8e6208c5 | 9911 | * We use this just before deleting it, or if we're going to initialize |
6c6083a9 KH |
9912 | * it a second time. |
9913 | */ | |
e50b8090 | 9914 | static void |
c5fdd383 KH |
9915 | wipe_kboard (kb) |
9916 | KBOARD *kb; | |
6c6083a9 | 9917 | { |
c5fdd383 KH |
9918 | if (kb->kbd_macro_buffer) |
9919 | xfree (kb->kbd_macro_buffer); | |
6c6083a9 KH |
9920 | } |
9921 | ||
e50b8090 KH |
9922 | #ifdef MULTI_KBOARD |
9923 | void | |
9924 | delete_kboard (kb) | |
9925 | KBOARD *kb; | |
9926 | { | |
9927 | KBOARD **kbp; | |
9928 | for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard) | |
9929 | if (*kbp == NULL) | |
9930 | abort (); | |
9931 | *kbp = kb->next_kboard; | |
9932 | wipe_kboard (kb); | |
9933 | xfree (kb); | |
9934 | } | |
9935 | #endif | |
9936 | ||
dfcf069d | 9937 | void |
284f4730 JB |
9938 | init_keyboard () |
9939 | { | |
284f4730 JB |
9940 | /* This is correct before outermost invocation of the editor loop */ |
9941 | command_loop_level = -1; | |
9942 | immediate_quit = 0; | |
9943 | quit_char = Ctl ('g'); | |
24597608 | 9944 | Vunread_command_events = Qnil; |
86e5706b | 9945 | unread_command_char = -1; |
87dd9b9b | 9946 | EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1); |
284f4730 | 9947 | total_keys = 0; |
9deb415a | 9948 | recent_keys_index = 0; |
beecf6a1 KH |
9949 | kbd_fetch_ptr = kbd_buffer; |
9950 | kbd_store_ptr = kbd_buffer; | |
da8f7368 | 9951 | kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil); |
2eb6bfbe | 9952 | #ifdef HAVE_MOUSE |
a9d77f1f | 9953 | do_mouse_tracking = Qnil; |
2eb6bfbe | 9954 | #endif |
284f4730 JB |
9955 | input_pending = 0; |
9956 | ||
4c52b668 KH |
9957 | /* This means that command_loop_1 won't try to select anything the first |
9958 | time through. */ | |
9959 | internal_last_event_frame = Qnil; | |
9960 | Vlast_event_frame = internal_last_event_frame; | |
4c52b668 | 9961 | |
c5fdd383 | 9962 | #ifdef MULTI_KBOARD |
aaca43a1 | 9963 | current_kboard = initial_kboard; |
6c6083a9 | 9964 | #endif |
aaca43a1 | 9965 | wipe_kboard (current_kboard); |
c5fdd383 | 9966 | init_kboard (current_kboard); |
07d2b8de | 9967 | |
7a80a6f6 | 9968 | if (!noninteractive && !read_socket_hook && NILP (Vwindow_system)) |
284f4730 JB |
9969 | { |
9970 | signal (SIGINT, interrupt_signal); | |
cb5df6ae | 9971 | #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS) |
284f4730 JB |
9972 | /* For systems with SysV TERMIO, C-g is set up for both SIGINT and |
9973 | SIGQUIT and we can't tell which one it will give us. */ | |
9974 | signal (SIGQUIT, interrupt_signal); | |
9975 | #endif /* HAVE_TERMIO */ | |
7a80a6f6 | 9976 | } |
284f4730 JB |
9977 | /* Note SIGIO has been undef'd if FIONREAD is missing. */ |
9978 | #ifdef SIGIO | |
7a80a6f6 RS |
9979 | if (!noninteractive) |
9980 | signal (SIGIO, input_available_signal); | |
8ea0a720 | 9981 | #endif /* SIGIO */ |
284f4730 JB |
9982 | |
9983 | /* Use interrupt input by default, if it works and noninterrupt input | |
9984 | has deficiencies. */ | |
9985 | ||
9986 | #ifdef INTERRUPT_INPUT | |
9987 | interrupt_input = 1; | |
9988 | #else | |
9989 | interrupt_input = 0; | |
9990 | #endif | |
9991 | ||
9992 | /* Our VMS input only works by interrupts, as of now. */ | |
9993 | #ifdef VMS | |
9994 | interrupt_input = 1; | |
9995 | #endif | |
9996 | ||
9997 | sigfree (); | |
9998 | dribble = 0; | |
9999 | ||
10000 | if (keyboard_init_hook) | |
10001 | (*keyboard_init_hook) (); | |
10002 | ||
10003 | #ifdef POLL_FOR_INPUT | |
10004 | poll_suppress_count = 1; | |
10005 | start_polling (); | |
10006 | #endif | |
10007 | } | |
10008 | ||
df0f2ba1 | 10009 | /* This type's only use is in syms_of_keyboard, to initialize the |
284f4730 JB |
10010 | event header symbols and put properties on them. */ |
10011 | struct event_head { | |
10012 | Lisp_Object *var; | |
10013 | char *name; | |
10014 | Lisp_Object *kind; | |
10015 | }; | |
10016 | ||
10017 | struct event_head head_table[] = { | |
7b4aedb9 | 10018 | &Qmouse_movement, "mouse-movement", &Qmouse_movement, |
3c370943 | 10019 | &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement, |
7b4aedb9 | 10020 | &Qswitch_frame, "switch-frame", &Qswitch_frame, |
bbdc2092 | 10021 | &Qdelete_frame, "delete-frame", &Qdelete_frame, |
af17bd2b KH |
10022 | &Qiconify_frame, "iconify-frame", &Qiconify_frame, |
10023 | &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible, | |
284f4730 JB |
10024 | }; |
10025 | ||
dfcf069d | 10026 | void |
284f4730 JB |
10027 | syms_of_keyboard () |
10028 | { | |
8e1e4240 GM |
10029 | Vlispy_mouse_stem = build_string ("mouse"); |
10030 | staticpro (&Vlispy_mouse_stem); | |
10031 | ||
9ea173e8 | 10032 | /* Tool-bars. */ |
7ee32cda GM |
10033 | QCimage = intern (":image"); |
10034 | staticpro (&QCimage); | |
10035 | ||
10036 | staticpro (&Qhelp_echo); | |
10037 | Qhelp_echo = intern ("help-echo"); | |
10038 | ||
e8886a1d RS |
10039 | staticpro (&item_properties); |
10040 | item_properties = Qnil; | |
10041 | ||
9ea173e8 GM |
10042 | staticpro (&tool_bar_item_properties); |
10043 | tool_bar_item_properties = Qnil; | |
10044 | staticpro (&tool_bar_items_vector); | |
10045 | tool_bar_items_vector = Qnil; | |
7ee32cda | 10046 | |
d5eecefb RS |
10047 | staticpro (&real_this_command); |
10048 | real_this_command = Qnil; | |
10049 | ||
d925fb39 RS |
10050 | Qtimer_event_handler = intern ("timer-event-handler"); |
10051 | staticpro (&Qtimer_event_handler); | |
10052 | ||
2e894dab RS |
10053 | Qdisabled_command_hook = intern ("disabled-command-hook"); |
10054 | staticpro (&Qdisabled_command_hook); | |
10055 | ||
284f4730 JB |
10056 | Qself_insert_command = intern ("self-insert-command"); |
10057 | staticpro (&Qself_insert_command); | |
10058 | ||
10059 | Qforward_char = intern ("forward-char"); | |
10060 | staticpro (&Qforward_char); | |
10061 | ||
10062 | Qbackward_char = intern ("backward-char"); | |
10063 | staticpro (&Qbackward_char); | |
10064 | ||
10065 | Qdisabled = intern ("disabled"); | |
10066 | staticpro (&Qdisabled); | |
10067 | ||
e58aa385 RS |
10068 | Qundefined = intern ("undefined"); |
10069 | staticpro (&Qundefined); | |
10070 | ||
86e5706b RS |
10071 | Qpre_command_hook = intern ("pre-command-hook"); |
10072 | staticpro (&Qpre_command_hook); | |
10073 | ||
10074 | Qpost_command_hook = intern ("post-command-hook"); | |
10075 | staticpro (&Qpost_command_hook); | |
10076 | ||
59aadc81 RS |
10077 | Qpost_command_idle_hook = intern ("post-command-idle-hook"); |
10078 | staticpro (&Qpost_command_idle_hook); | |
10079 | ||
3ef14e46 RS |
10080 | Qdeferred_action_function = intern ("deferred-action-function"); |
10081 | staticpro (&Qdeferred_action_function); | |
10082 | ||
40932d1a RS |
10083 | Qcommand_hook_internal = intern ("command-hook-internal"); |
10084 | staticpro (&Qcommand_hook_internal); | |
10085 | ||
284f4730 JB |
10086 | Qfunction_key = intern ("function-key"); |
10087 | staticpro (&Qfunction_key); | |
13b5e56c | 10088 | Qmouse_click = intern ("mouse-click"); |
284f4730 | 10089 | staticpro (&Qmouse_click); |
07de30b9 GV |
10090 | #ifdef WINDOWSNT |
10091 | Qmouse_wheel = intern ("mouse-wheel"); | |
10092 | staticpro (&Qmouse_wheel); | |
1161d367 GV |
10093 | Qlanguage_change = intern ("language-change"); |
10094 | staticpro (&Qlanguage_change); | |
07de30b9 | 10095 | #endif |
a24dc617 RS |
10096 | Qdrag_n_drop = intern ("drag-n-drop"); |
10097 | staticpro (&Qdrag_n_drop); | |
284f4730 | 10098 | |
5bf68f6e AS |
10099 | Qusr1_signal = intern ("usr1-signal"); |
10100 | staticpro (&Qusr1_signal); | |
10101 | Qusr2_signal = intern ("usr2-signal"); | |
10102 | staticpro (&Qusr2_signal); | |
10103 | ||
598a9fa7 JB |
10104 | Qmenu_enable = intern ("menu-enable"); |
10105 | staticpro (&Qmenu_enable); | |
e8886a1d RS |
10106 | Qmenu_alias = intern ("menu-alias"); |
10107 | staticpro (&Qmenu_alias); | |
10108 | QCenable = intern (":enable"); | |
10109 | staticpro (&QCenable); | |
10110 | QCvisible = intern (":visible"); | |
10111 | staticpro (&QCvisible); | |
7ee32cda GM |
10112 | QChelp = intern (":help"); |
10113 | staticpro (&QChelp); | |
e8886a1d RS |
10114 | QCfilter = intern (":filter"); |
10115 | staticpro (&QCfilter); | |
10116 | QCbutton = intern (":button"); | |
10117 | staticpro (&QCbutton); | |
74c1de23 RS |
10118 | QCkeys = intern (":keys"); |
10119 | staticpro (&QCkeys); | |
10120 | QCkey_sequence = intern (":key-sequence"); | |
10121 | staticpro (&QCkey_sequence); | |
e8886a1d RS |
10122 | QCtoggle = intern (":toggle"); |
10123 | staticpro (&QCtoggle); | |
10124 | QCradio = intern (":radio"); | |
10125 | staticpro (&QCradio); | |
598a9fa7 | 10126 | |
284f4730 JB |
10127 | Qmode_line = intern ("mode-line"); |
10128 | staticpro (&Qmode_line); | |
e5d77022 JB |
10129 | Qvertical_line = intern ("vertical-line"); |
10130 | staticpro (&Qvertical_line); | |
3c370943 JB |
10131 | Qvertical_scroll_bar = intern ("vertical-scroll-bar"); |
10132 | staticpro (&Qvertical_scroll_bar); | |
5ec75a55 RS |
10133 | Qmenu_bar = intern ("menu-bar"); |
10134 | staticpro (&Qmenu_bar); | |
4bb994d1 JB |
10135 | |
10136 | Qabove_handle = intern ("above-handle"); | |
10137 | staticpro (&Qabove_handle); | |
10138 | Qhandle = intern ("handle"); | |
10139 | staticpro (&Qhandle); | |
10140 | Qbelow_handle = intern ("below-handle"); | |
10141 | staticpro (&Qbelow_handle); | |
db08707d RS |
10142 | Qup = intern ("up"); |
10143 | staticpro (&Qup); | |
10144 | Qdown = intern ("down"); | |
10145 | staticpro (&Qdown); | |
7ee32cda GM |
10146 | Qtop = intern ("top"); |
10147 | staticpro (&Qtop); | |
10148 | Qbottom = intern ("bottom"); | |
10149 | staticpro (&Qbottom); | |
10150 | Qend_scroll = intern ("end-scroll"); | |
10151 | staticpro (&Qend_scroll); | |
eef28553 SM |
10152 | Qratio = intern ("ratio"); |
10153 | staticpro (&Qratio); | |
284f4730 | 10154 | |
cd21b839 | 10155 | Qevent_kind = intern ("event-kind"); |
284f4730 | 10156 | staticpro (&Qevent_kind); |
88cb0656 JB |
10157 | Qevent_symbol_elements = intern ("event-symbol-elements"); |
10158 | staticpro (&Qevent_symbol_elements); | |
0a7f1fc0 JB |
10159 | Qevent_symbol_element_mask = intern ("event-symbol-element-mask"); |
10160 | staticpro (&Qevent_symbol_element_mask); | |
10161 | Qmodifier_cache = intern ("modifier-cache"); | |
10162 | staticpro (&Qmodifier_cache); | |
284f4730 | 10163 | |
48e416d4 RS |
10164 | Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar"); |
10165 | staticpro (&Qrecompute_lucid_menubar); | |
10166 | Qactivate_menubar_hook = intern ("activate-menubar-hook"); | |
10167 | staticpro (&Qactivate_menubar_hook); | |
10168 | ||
f4eef8b4 RS |
10169 | Qpolling_period = intern ("polling-period"); |
10170 | staticpro (&Qpolling_period); | |
10171 | ||
7d18f9ae RS |
10172 | Qinput_method_function = intern ("input-method-function"); |
10173 | staticpro (&Qinput_method_function); | |
10174 | ||
d5eecefb RS |
10175 | Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char"); |
10176 | staticpro (&Qinput_method_exit_on_first_char); | |
10177 | Qinput_method_use_echo_area = intern ("input-method-use-echo-area"); | |
10178 | staticpro (&Qinput_method_use_echo_area); | |
10179 | ||
10180 | Fset (Qinput_method_exit_on_first_char, Qnil); | |
10181 | Fset (Qinput_method_use_echo_area, Qnil); | |
10182 | ||
e18dfbf4 KR |
10183 | last_point_position_buffer = Qnil; |
10184 | ||
284f4730 JB |
10185 | { |
10186 | struct event_head *p; | |
10187 | ||
10188 | for (p = head_table; | |
10189 | p < head_table + (sizeof (head_table) / sizeof (head_table[0])); | |
10190 | p++) | |
10191 | { | |
10192 | *p->var = intern (p->name); | |
10193 | staticpro (p->var); | |
10194 | Fput (*p->var, Qevent_kind, *p->kind); | |
88cb0656 | 10195 | Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil)); |
284f4730 JB |
10196 | } |
10197 | } | |
10198 | ||
8e1e4240 | 10199 | button_down_location = Fmake_vector (make_number (1), Qnil); |
7b4aedb9 | 10200 | staticpro (&button_down_location); |
8e1e4240 GM |
10201 | mouse_syms = Fmake_vector (make_number (1), Qnil); |
10202 | staticpro (&mouse_syms); | |
88cb0656 JB |
10203 | |
10204 | { | |
10205 | int i; | |
10206 | int len = sizeof (modifier_names) / sizeof (modifier_names[0]); | |
10207 | ||
10208 | modifier_symbols = Fmake_vector (make_number (len), Qnil); | |
10209 | for (i = 0; i < len; i++) | |
86e5706b RS |
10210 | if (modifier_names[i]) |
10211 | XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]); | |
88cb0656 JB |
10212 | staticpro (&modifier_symbols); |
10213 | } | |
10214 | ||
9deb415a JB |
10215 | recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil); |
10216 | staticpro (&recent_keys); | |
10217 | ||
6569cc8d | 10218 | this_command_keys = Fmake_vector (make_number (40), Qnil); |
715d9345 | 10219 | staticpro (&this_command_keys); |
6569cc8d | 10220 | |
7d18f9ae RS |
10221 | raw_keybuf = Fmake_vector (make_number (30), Qnil); |
10222 | staticpro (&raw_keybuf); | |
10223 | ||
03b4122a BF |
10224 | Qextended_command_history = intern ("extended-command-history"); |
10225 | Fset (Qextended_command_history, Qnil); | |
10226 | staticpro (&Qextended_command_history); | |
10227 | ||
da8f7368 GM |
10228 | kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil); |
10229 | staticpro (&kbd_buffer_gcpro); | |
beecf6a1 | 10230 | |
24597608 RS |
10231 | accent_key_syms = Qnil; |
10232 | staticpro (&accent_key_syms); | |
10233 | ||
284f4730 JB |
10234 | func_key_syms = Qnil; |
10235 | staticpro (&func_key_syms); | |
10236 | ||
07de30b9 GV |
10237 | #ifdef WINDOWSNT |
10238 | mouse_wheel_syms = Qnil; | |
10239 | staticpro (&mouse_wheel_syms); | |
a24dc617 RS |
10240 | |
10241 | drag_n_drop_syms = Qnil; | |
10242 | staticpro (&drag_n_drop_syms); | |
07de30b9 GV |
10243 | #endif |
10244 | ||
cd21b839 JB |
10245 | unread_switch_frame = Qnil; |
10246 | staticpro (&unread_switch_frame); | |
10247 | ||
fe412364 EN |
10248 | internal_last_event_frame = Qnil; |
10249 | staticpro (&internal_last_event_frame); | |
10250 | ||
10251 | read_key_sequence_cmd = Qnil; | |
10252 | staticpro (&read_key_sequence_cmd); | |
10253 | ||
759860a6 RS |
10254 | menu_bar_one_keymap_changed_items = Qnil; |
10255 | staticpro (&menu_bar_one_keymap_changed_items); | |
10256 | ||
a1706c30 | 10257 | defsubr (&Sevent_convert_list); |
284f4730 | 10258 | defsubr (&Sread_key_sequence); |
e39da3d7 | 10259 | defsubr (&Sread_key_sequence_vector); |
284f4730 | 10260 | defsubr (&Srecursive_edit); |
2eb6bfbe | 10261 | #ifdef HAVE_MOUSE |
284f4730 | 10262 | defsubr (&Strack_mouse); |
2eb6bfbe | 10263 | #endif |
284f4730 JB |
10264 | defsubr (&Sinput_pending_p); |
10265 | defsubr (&Scommand_execute); | |
10266 | defsubr (&Srecent_keys); | |
10267 | defsubr (&Sthis_command_keys); | |
e39da3d7 | 10268 | defsubr (&Sthis_command_keys_vector); |
6321824f | 10269 | defsubr (&Sthis_single_command_keys); |
7d18f9ae | 10270 | defsubr (&Sthis_single_command_raw_keys); |
71918b75 | 10271 | defsubr (&Sreset_this_command_lengths); |
82e6e5af | 10272 | defsubr (&Sclear_this_command_keys); |
284f4730 JB |
10273 | defsubr (&Ssuspend_emacs); |
10274 | defsubr (&Sabort_recursive_edit); | |
10275 | defsubr (&Sexit_recursive_edit); | |
10276 | defsubr (&Srecursion_depth); | |
10277 | defsubr (&Stop_level); | |
10278 | defsubr (&Sdiscard_input); | |
10279 | defsubr (&Sopen_dribble_file); | |
10280 | defsubr (&Sset_input_mode); | |
80645119 | 10281 | defsubr (&Scurrent_input_mode); |
284f4730 JB |
10282 | defsubr (&Sexecute_extended_command); |
10283 | ||
284f4730 | 10284 | DEFVAR_LISP ("last-command-char", &last_command_char, |
86e5706b RS |
10285 | "Last input event that was part of a command."); |
10286 | ||
186cf719 | 10287 | DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char, |
86e5706b | 10288 | "Last input event that was part of a command."); |
284f4730 | 10289 | |
7d6de002 | 10290 | DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event, |
86e5706b | 10291 | "Last input event in a command, except for mouse menu events.\n\ |
7d6de002 RS |
10292 | Mouse menus give back keys that don't look like mouse events;\n\ |
10293 | this variable holds the actual mouse event that led to the menu,\n\ | |
10294 | so that you can determine whether the command was run by mouse or not."); | |
10295 | ||
284f4730 | 10296 | DEFVAR_LISP ("last-input-char", &last_input_char, |
86e5706b RS |
10297 | "Last input event."); |
10298 | ||
186cf719 | 10299 | DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char, |
86e5706b | 10300 | "Last input event."); |
284f4730 | 10301 | |
24597608 | 10302 | DEFVAR_LISP ("unread-command-events", &Vunread_command_events, |
7d18f9ae RS |
10303 | "List of events to be read as the command input.\n\ |
10304 | These events are processed first, before actual keyboard input."); | |
10305 | Vunread_command_events = Qnil; | |
284f4730 | 10306 | |
86e5706b RS |
10307 | DEFVAR_INT ("unread-command-char", &unread_command_char, |
10308 | "If not -1, an object to be read as next command input event."); | |
10309 | ||
7d18f9ae RS |
10310 | DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events, |
10311 | "List of events to be processed as input by input methods.\n\ | |
10312 | These events are processed after `unread-command-events', but\n\ | |
10313 | before actual keyboard input."); | |
10314 | Vunread_post_input_method_events = Qnil; | |
10315 | ||
10316 | DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events, | |
10317 | "List of events to be processed as input by input methods.\n\ | |
10318 | These events are processed after `unread-command-events', but\n\ | |
10319 | before actual keyboard input."); | |
10320 | Vunread_input_method_events = Qnil; | |
10321 | ||
284f4730 | 10322 | DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char, |
b283f79e KH |
10323 | "Meta-prefix character code.\n\ |
10324 | Meta-foo as command input turns into this character followed by foo."); | |
18cd2eeb | 10325 | XSETINT (meta_prefix_char, 033); |
284f4730 | 10326 | |
6c7178b9 | 10327 | DEFVAR_KBOARD ("last-command", Vlast_command, |
b283f79e KH |
10328 | "The last command executed.\n\ |
10329 | Normally a symbol with a function definition, but can be whatever was found\n\ | |
10330 | in the keymap, or whatever the variable `this-command' was set to by that\n\ | |
10331 | command.\n\ | |
18f29056 RS |
10332 | \n\ |
10333 | The value `mode-exit' is special; it means that the previous command\n\ | |
10334 | read an event that told it to exit, and it did so and unread that event.\n\ | |
10335 | In other words, the present command is the event that made the previous\n\ | |
10336 | command exit.\n\ | |
10337 | \n\ | |
10338 | The value `kill-region' is special; it means that the previous command\n\ | |
10339 | was a kill command."); | |
284f4730 | 10340 | |
75045dcb RS |
10341 | DEFVAR_KBOARD ("real-last-command", Vreal_last_command, |
10342 | "Same as `last-command', but never altered by Lisp code."); | |
10343 | ||
d5eecefb | 10344 | DEFVAR_LISP ("this-command", &Vthis_command, |
284f4730 JB |
10345 | "The command now being executed.\n\ |
10346 | The command can set this variable; whatever is put here\n\ | |
10347 | will be in `last-command' during the following command."); | |
d5eecefb | 10348 | Vthis_command = Qnil; |
284f4730 JB |
10349 | |
10350 | DEFVAR_INT ("auto-save-interval", &auto_save_interval, | |
dace7f4e | 10351 | "*Number of input events between auto-saves.\n\ |
284f4730 JB |
10352 | Zero means disable autosaving due to number of characters typed."); |
10353 | auto_save_interval = 300; | |
10354 | ||
10355 | DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout, | |
10356 | "*Number of seconds idle time before auto-save.\n\ | |
06ef7355 RS |
10357 | Zero or nil means disable auto-saving due to idleness.\n\ |
10358 | After auto-saving due to this many seconds of idle time,\n\ | |
84447c71 | 10359 | Emacs also does a garbage collection if that seems to be warranted."); |
bb9e9bed | 10360 | XSETFASTINT (Vauto_save_timeout, 30); |
284f4730 | 10361 | |
39aab679 | 10362 | DEFVAR_LISP ("echo-keystrokes", &Vecho_keystrokes, |
f2647d04 DL |
10363 | "*Nonzero means echo unfinished commands after this many seconds of pause.\n\ |
10364 | The value may be integer or floating point."); | |
39aab679 | 10365 | Vecho_keystrokes = make_number (1); |
284f4730 JB |
10366 | |
10367 | DEFVAR_INT ("polling-period", &polling_period, | |
10368 | "*Interval between polling for input during Lisp execution.\n\ | |
10369 | The reason for polling is to make C-g work to stop a running program.\n\ | |
10370 | Polling is needed only when using X windows and SIGIO does not work.\n\ | |
10371 | Polling is automatically disabled in all other cases."); | |
10372 | polling_period = 2; | |
df0f2ba1 | 10373 | |
564dc952 | 10374 | DEFVAR_LISP ("double-click-time", &Vdouble_click_time, |
fbcd35bd | 10375 | "*Maximum time between mouse clicks to make a double-click.\n\ |
564dc952 JB |
10376 | Measured in milliseconds. nil means disable double-click recognition;\n\ |
10377 | t means double-clicks have no time limit and are detected\n\ | |
fbcd35bd | 10378 | by position only."); |
aab06933 | 10379 | Vdouble_click_time = make_number (500); |
fbcd35bd | 10380 | |
03361bcc RS |
10381 | DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus, |
10382 | "*Non-nil means inhibit local map menu bar menus."); | |
10383 | inhibit_local_menu_bar_menus = 0; | |
10384 | ||
284f4730 | 10385 | DEFVAR_INT ("num-input-keys", &num_input_keys, |
c43b1734 | 10386 | "Number of complete key sequences read as input so far.\n\ |
58ce35fb KH |
10387 | This includes key sequences read from keyboard macros.\n\ |
10388 | The number is effectively the number of interactive command invocations."); | |
284f4730 JB |
10389 | num_input_keys = 0; |
10390 | ||
c43b1734 RS |
10391 | DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events, |
10392 | "Number of input events read from the keyboard so far.\n\ | |
10393 | This does not include events generated by keyboard macros."); | |
10394 | num_nonmacro_input_events = 0; | |
fa90970d | 10395 | |
4c52b668 KH |
10396 | DEFVAR_LISP ("last-event-frame", &Vlast_event_frame, |
10397 | "The frame in which the most recently read event occurred.\n\ | |
10398 | If the last event came from a keyboard macro, this is set to `macro'."); | |
10399 | Vlast_event_frame = Qnil; | |
10400 | ||
fa90970d RS |
10401 | /* This variable is set up in sysdep.c. */ |
10402 | DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char, | |
10403 | "The ERASE character as set by the user with stty."); | |
10404 | ||
7e85b935 | 10405 | DEFVAR_LISP ("help-char", &Vhelp_char, |
284f4730 JB |
10406 | "Character to recognize as meaning Help.\n\ |
10407 | When it is read, do `(eval help-form)', and display result if it's a string.\n\ | |
10408 | If the value of `help-form' is nil, this char can be read normally."); | |
18cd2eeb | 10409 | XSETINT (Vhelp_char, Ctl ('H')); |
284f4730 | 10410 | |
ecb7cb34 KH |
10411 | DEFVAR_LISP ("help-event-list", &Vhelp_event_list, |
10412 | "List of input events to recognize as meaning Help.\n\ | |
10413 | These work just like the value of `help-char' (see that)."); | |
10414 | Vhelp_event_list = Qnil; | |
10415 | ||
284f4730 | 10416 | DEFVAR_LISP ("help-form", &Vhelp_form, |
7e85b935 | 10417 | "Form to execute when character `help-char' is read.\n\ |
284f4730 JB |
10418 | If the form returns a string, that string is displayed.\n\ |
10419 | If `help-form' is nil, the help char is not recognized."); | |
10420 | Vhelp_form = Qnil; | |
10421 | ||
7e85b935 RS |
10422 | DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command, |
10423 | "Command to run when `help-char' character follows a prefix key.\n\ | |
10424 | This command is used only when there is no actual binding\n\ | |
10425 | for that character after that prefix key."); | |
10426 | Vprefix_help_command = Qnil; | |
10427 | ||
284f4730 JB |
10428 | DEFVAR_LISP ("top-level", &Vtop_level, |
10429 | "Form to evaluate when Emacs starts up.\n\ | |
10430 | Useful to set before you dump a modified Emacs."); | |
10431 | Vtop_level = Qnil; | |
10432 | ||
10433 | DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table, | |
f9414d62 | 10434 | "Translate table for keyboard input, or nil.\n\ |
284f4730 | 10435 | Each character is looked up in this string and the contents used instead.\n\ |
f9414d62 RS |
10436 | The value may be a string, a vector, or a char-table.\n\ |
10437 | If it is a string or vector of length N,\n\ | |
10438 | character codes N and up are untranslated.\n\ | |
10439 | In a vector or a char-table, an element which is nil means \"no translation\"."); | |
284f4730 JB |
10440 | Vkeyboard_translate_table = Qnil; |
10441 | ||
8026024c | 10442 | DEFVAR_BOOL ("cannot-suspend", &cannot_suspend, |
b283f79e KH |
10443 | "Non-nil means to always spawn a subshell instead of suspending.\n\ |
10444 | \(Even if the operating system has support for stopping a process.\)"); | |
8026024c KH |
10445 | cannot_suspend = 0; |
10446 | ||
284f4730 | 10447 | DEFVAR_BOOL ("menu-prompting", &menu_prompting, |
7d6de002 | 10448 | "Non-nil means prompt with menus when appropriate.\n\ |
284f4730 | 10449 | This is done when reading from a keymap that has a prompt string,\n\ |
7d6de002 RS |
10450 | for elements that have prompt strings.\n\ |
10451 | The menu is displayed on the screen\n\ | |
10452 | if X menus were enabled at configuration\n\ | |
10453 | time and the previous event was a mouse click prefix key.\n\ | |
10454 | Otherwise, menu prompting uses the echo area."); | |
284f4730 JB |
10455 | menu_prompting = 1; |
10456 | ||
10457 | DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char, | |
10458 | "Character to see next line of menu prompt.\n\ | |
10459 | Type this character while in a menu prompt to rotate around the lines of it."); | |
18cd2eeb | 10460 | XSETINT (menu_prompt_more_char, ' '); |
9fa4395d RS |
10461 | |
10462 | DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers, | |
10463 | "A mask of additional modifier keys to use with every keyboard character.\n\ | |
ad163903 JB |
10464 | Emacs applies the modifiers of the character stored here to each keyboard\n\ |
10465 | character it reads. For example, after evaluating the expression\n\ | |
9d9f56dd | 10466 | (setq extra-keyboard-modifiers ?\\C-x)\n\ |
80645119 JB |
10467 | all input characters will have the control modifier applied to them.\n\ |
10468 | \n\ | |
9d9f56dd | 10469 | Note that the character ?\\C-@, equivalent to the integer zero, does\n\ |
80645119 | 10470 | not count as a control character; rather, it counts as a character\n\ |
27203ead | 10471 | with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\ |
80645119 | 10472 | cancels any modification."); |
9fa4395d | 10473 | extra_keyboard_modifiers = 0; |
86e5706b RS |
10474 | |
10475 | DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark, | |
10476 | "If an editing command sets this to t, deactivate the mark afterward.\n\ | |
10477 | The command loop sets this to nil before each command,\n\ | |
10478 | and tests the value when the command returns.\n\ | |
10479 | Buffer modification stores t in this variable."); | |
10480 | Vdeactivate_mark = Qnil; | |
10481 | ||
b0f2a7bf KH |
10482 | DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal, |
10483 | "Temporary storage of pre-command-hook or post-command-hook."); | |
10484 | Vcommand_hook_internal = Qnil; | |
10485 | ||
86e5706b | 10486 | DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook, |
a1fd42c0 | 10487 | "Normal hook run before each command is executed.\n\ |
307958d6 RS |
10488 | If an unhandled error happens in running this hook,\n\ |
10489 | the hook value is set to nil, since otherwise the error\n\ | |
10490 | might happen repeatedly and make Emacs nonfunctional."); | |
86e5706b RS |
10491 | Vpre_command_hook = Qnil; |
10492 | ||
10493 | DEFVAR_LISP ("post-command-hook", &Vpost_command_hook, | |
a1fd42c0 | 10494 | "Normal hook run after each command is executed.\n\ |
307958d6 RS |
10495 | If an unhandled error happens in running this hook,\n\ |
10496 | the hook value is set to nil, since otherwise the error\n\ | |
10497 | might happen repeatedly and make Emacs nonfunctional."); | |
86e5706b | 10498 | Vpost_command_hook = Qnil; |
48e416d4 | 10499 | |
59aadc81 RS |
10500 | DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook, |
10501 | "Normal hook run after each command is executed, if idle.\n\ | |
d4d62e8d RS |
10502 | Errors running the hook are caught and ignored.\n\ |
10503 | This feature is obsolete; use idle timers instead. See `etc/NEWS'."); | |
59aadc81 RS |
10504 | Vpost_command_idle_hook = Qnil; |
10505 | ||
10506 | DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay, | |
7051b69b | 10507 | "Delay time before running `post-command-idle-hook'.\n\ |
59aadc81 RS |
10508 | This is measured in microseconds."); |
10509 | post_command_idle_delay = 100000; | |
10510 | ||
cdb9d665 RS |
10511 | #if 0 |
10512 | DEFVAR_LISP ("echo-area-clear-hook", ..., | |
10513 | "Normal hook run when clearing the echo area."); | |
10514 | #endif | |
10515 | Qecho_area_clear_hook = intern ("echo-area-clear-hook"); | |
10516 | XSYMBOL (Qecho_area_clear_hook)->value = Qnil; | |
10517 | ||
48e416d4 RS |
10518 | DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag, |
10519 | "t means menu bar, specified Lucid style, needs to be recomputed."); | |
10520 | Vlucid_menu_bar_dirty_flag = Qnil; | |
a73c5e29 | 10521 | |
9f9c0e27 RS |
10522 | DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items, |
10523 | "List of menu bar items to move to the end of the menu bar.\n\ | |
a612e298 | 10524 | The elements of the list are event types that may have menu bar bindings."); |
9f9c0e27 | 10525 | Vmenu_bar_final_items = Qnil; |
e9bf89a0 | 10526 | |
217258d5 KH |
10527 | DEFVAR_KBOARD ("overriding-terminal-local-map", |
10528 | Voverriding_terminal_local_map, | |
779b34df | 10529 | "Per-terminal keymap that overrides all other local keymaps.\n\ |
217258d5 | 10530 | If this variable is non-nil, it is used as a keymap instead of the\n\ |
779b34df RS |
10531 | buffer's local map, and the minor mode keymaps and text property keymaps.\n\ |
10532 | This variable is intended to let commands such as `universal-argumemnt'\n\ | |
10533 | set up a different keymap for reading the next command."); | |
217258d5 | 10534 | |
9dd3131c RS |
10535 | DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map, |
10536 | "Keymap that overrides all other local keymaps.\n\ | |
10537 | If this variable is non-nil, it is used as a keymap instead of the\n\ | |
10538 | buffer's local map, and the minor mode keymaps and text property keymaps."); | |
10539 | Voverriding_local_map = Qnil; | |
10540 | ||
d0a49716 RS |
10541 | DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag, |
10542 | "Non-nil means `overriding-local-map' applies to the menu bar.\n\ | |
10543 | Otherwise, the menu bar continues to reflect the buffer's local map\n\ | |
10544 | and the minor mode maps regardless of `overriding-local-map'."); | |
10545 | Voverriding_local_map_menu_flag = Qnil; | |
10546 | ||
7f07d5ca RS |
10547 | DEFVAR_LISP ("special-event-map", &Vspecial_event_map, |
10548 | "Keymap defining bindings for special events to execute at low level."); | |
10549 | Vspecial_event_map = Fcons (intern ("keymap"), Qnil); | |
10550 | ||
71edead1 | 10551 | DEFVAR_LISP ("track-mouse", &do_mouse_tracking, |
a53c7666 | 10552 | "*Non-nil means generate motion events for mouse motion."); |
80e4aa30 | 10553 | |
7c97ffdc | 10554 | DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist, |
270a208f | 10555 | "Alist of system-specific X windows key symbols.\n\ |
80e4aa30 | 10556 | Each element should have the form (N . SYMBOL) where N is the\n\ |
270a208f | 10557 | numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\ |
80e4aa30 | 10558 | and SYMBOL is its name."); |
8a792f3a RS |
10559 | |
10560 | DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list, | |
10561 | "List of deferred actions to be performed at a later time.\n\ | |
10562 | The precise format isn't relevant here; we just check whether it is nil."); | |
10563 | Vdeferred_action_list = Qnil; | |
10564 | ||
10565 | DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function, | |
10566 | "Function to call to handle deferred actions, after each command.\n\ | |
10567 | This function is called with no arguments after each command\n\ | |
10568 | whenever `deferred-action-list' is non-nil."); | |
10569 | Vdeferred_action_function = Qnil; | |
6526ab49 RS |
10570 | |
10571 | DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings, | |
ac52ebd5 | 10572 | "*Non-nil means show the equivalent key-binding when M-x command has one.\n\ |
6526ab49 RS |
10573 | The value can be a length of time to show the message for.\n\ |
10574 | If the value is non-nil and not a number, we wait 2 seconds."); | |
10575 | Vsuggest_key_bindings = Qt; | |
8bb1c042 | 10576 | |
c04cbc3b | 10577 | DEFVAR_LISP ("timer-list", &Vtimer_list, |
d9d4c147 | 10578 | "List of active absolute time timers in order of increasing time"); |
c04cbc3b | 10579 | Vtimer_list = Qnil; |
d9d4c147 KH |
10580 | |
10581 | DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list, | |
10582 | "List of active idle-time timers in order of increasing time"); | |
10583 | Vtimer_idle_list = Qnil; | |
7d18f9ae RS |
10584 | |
10585 | DEFVAR_LISP ("input-method-function", &Vinput_method_function, | |
10586 | "If non-nil, the function that implements the current input method.\n\ | |
10587 | It's called with one argument, a printing character that was just read.\n\ | |
10588 | \(That means a character with code 040...0176.)\n\ | |
10589 | Typically this function uses `read-event' to read additional events.\n\ | |
10590 | When it does so, it should first bind `input-method-function' to nil\n\ | |
10591 | so it will not be called recursively.\n\ | |
10592 | \n\ | |
10593 | The function should return a list of zero or more events\n\ | |
10594 | to be used as input. If it wants to put back some events\n\ | |
10595 | to be reconsidered, separately, by the input method,\n\ | |
d5eecefb RS |
10596 | it can add them to the beginning of `unread-command-events'.\n\ |
10597 | \n\ | |
10598 | The input method function can find in `input-method-previous-method'\n\ | |
10599 | the previous echo area message.\n\ | |
10600 | \n\ | |
10601 | The input method function should refer to the variables\n\ | |
10602 | `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\ | |
10603 | for guidance on what to do."); | |
7d18f9ae | 10604 | Vinput_method_function = Qnil; |
d5eecefb RS |
10605 | |
10606 | DEFVAR_LISP ("input-method-previous-message", | |
10607 | &Vinput_method_previous_message, | |
a8b13665 | 10608 | "When `input-method-function' is called, hold the previous echo area message.\n\ |
d5eecefb RS |
10609 | This variable exists because `read-event' clears the echo area\n\ |
10610 | before running the input method. It is nil if there was no message."); | |
10611 | Vinput_method_previous_message = Qnil; | |
7ee32cda GM |
10612 | |
10613 | DEFVAR_LISP ("show-help-function", &Vshow_help_function, | |
10614 | "If non-nil, the function that implements the display of help.\n\ | |
10615 | It's called with one argument, the help string to display."); | |
10616 | Vshow_help_function = Qnil; | |
adf5cb9c KH |
10617 | |
10618 | DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment, | |
10619 | "If non-nil, suppress point adjustment after executing a command.\n\ | |
10620 | \n\ | |
10621 | After a command is executed, if point is moved into a region that has\n\ | |
10622 | special properties (e.g. composition, display), we adjust point to\n\ | |
10623 | the boundary of the region. But, several special commands sets this\n\ | |
10624 | variable to non-nil, then we suppress the point adjustment.\n\ | |
10625 | \n\ | |
10626 | This variable is set to nil before reading a command, and is checked\n\ | |
10627 | just after executing the command"); | |
10628 | Vdisable_point_adjustment = Qnil; | |
10629 | ||
10630 | DEFVAR_LISP ("global-disable-point-adjustment", | |
10631 | &Vglobal_disable_point_adjustment, | |
10632 | "*If non-nil, always suppress point adjustment.\n\ | |
10633 | \n\ | |
10634 | The default value is nil, in which case, point adjustment are\n\ | |
10635 | suppressed only after special commands that set\n\ | |
10636 | `disable-point-adjustment' (which see) to non-nil."); | |
10637 | Vglobal_disable_point_adjustment = Qnil; | |
3626fb1a | 10638 | |
a1d34b1e | 10639 | DEFVAR_BOOL ("update-menu-bindings", &update_menu_bindings, |
3626fb1a GM |
10640 | "Non-nil means updating menu bindings is allowed.\n\ |
10641 | A value of nil means menu bindings should not be updated.\n\ | |
10642 | Used during Emacs' startup."); | |
10643 | update_menu_bindings = 1; | |
00392ce6 MB |
10644 | |
10645 | DEFVAR_LISP ("minibuffer-message-timeout", &Vminibuffer_message_timeout, | |
10646 | "*How long to display an echo-area message when the minibuffer is active.\n\ | |
10647 | If the value is not a number, such messages don't time out."); | |
10648 | Vminibuffer_message_timeout = make_number (2); | |
284f4730 JB |
10649 | } |
10650 | ||
dfcf069d | 10651 | void |
284f4730 JB |
10652 | keys_of_keyboard () |
10653 | { | |
10654 | initial_define_key (global_map, Ctl ('Z'), "suspend-emacs"); | |
10655 | initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs"); | |
10656 | initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit"); | |
10657 | initial_define_key (global_map, Ctl (']'), "abort-recursive-edit"); | |
10658 | initial_define_key (meta_map, 'x', "execute-extended-command"); | |
7f07d5ca RS |
10659 | |
10660 | initial_define_lispy_key (Vspecial_event_map, "delete-frame", | |
10661 | "handle-delete-frame"); | |
10662 | initial_define_lispy_key (Vspecial_event_map, "iconify-frame", | |
10663 | "ignore-event"); | |
10664 | initial_define_lispy_key (Vspecial_event_map, "make-frame-visible", | |
10665 | "ignore-event"); | |
284f4730 | 10666 | } |