home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / keyboard.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  291KB  |  9,477 lines

  1. /* Keyboard and mouse input; editor command loop.
  2.    Copyright (C) 1985,86,87,88,89,93,94,95,96,97 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21. /* Modified for emx by Jeremy Bowen, May 1999 based on patches
  22.    to v19.33 by Eberhard Mattes */
  23.  
  24. /* Allow config.h to undefine symbols found here.  */
  25. #include <signal.h>
  26.  
  27. #include <config.h>
  28. #include <stdio.h>
  29. #include "termchar.h"
  30. #include "termopts.h"
  31. #include "lisp.h"
  32. #include "termhooks.h"
  33. #include "macros.h"
  34. #include "frame.h"
  35. #include "window.h"
  36. #include "commands.h"
  37. #include "buffer.h"
  38. #include "charset.h"
  39. #include "disptab.h"
  40. #include "dispextern.h"
  41. #include "keyboard.h"
  42. #include "syntax.h"
  43. #include "intervals.h"
  44. #include "blockinput.h"
  45. #include "puresize.h"
  46. #include <setjmp.h>
  47. #include <errno.h>
  48.  
  49. #ifdef MSDOS
  50. #include "msdos.h"
  51. #include <time.h>
  52. #else /* not MSDOS */
  53. #ifndef VMS
  54. #include <sys/ioctl.h>
  55. #endif
  56. #endif /* not MSDOS */
  57.  
  58. #include "syssignal.h"
  59. #include "systty.h"
  60.  
  61. /* This is to get the definitions of the XK_ symbols.  */
  62. #ifdef HAVE_X_WINDOWS
  63. #ifndef HAVE_PM
  64. #include "xterm.h"
  65. #else /* HAVE_PM */
  66. #include "pmterm.h"
  67. #endif /* HAVE_PM */
  68. #endif    /* HAVE_X_WINDOWS */
  69.  
  70. #ifdef HAVE_NTGUI
  71. #include "w32term.h"
  72. #endif /* HAVE_NTGUI */
  73.  
  74. /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
  75. #include "systime.h"
  76.  
  77. extern int errno;
  78.  
  79. /* Variables for blockinput.h: */
  80.  
  81. /* Non-zero if interrupt input is blocked right now.  */
  82. int interrupt_input_blocked;
  83.  
  84. /* Nonzero means an input interrupt has arrived
  85.    during the current critical section.  */
  86. int interrupt_input_pending;
  87.  
  88.  
  89. /* File descriptor to use for input.  */
  90. extern int input_fd;
  91.  
  92. #ifdef HAVE_WINDOW_SYSTEM
  93. /* Make all keyboard buffers much bigger when using X windows.  */
  94. #define KBD_BUFFER_SIZE 4096
  95. #else    /* No X-windows, character input */
  96. #define KBD_BUFFER_SIZE 256
  97. #endif    /* No X-windows */
  98.  
  99. /* Following definition copied from eval.c */
  100.  
  101. struct backtrace
  102.   {
  103.     struct backtrace *next;
  104.     Lisp_Object *function;
  105.     Lisp_Object *args;    /* Points to vector of args. */
  106.     int nargs;        /* length of vector.  If nargs is UNEVALLED,
  107.                args points to slot holding list of
  108.                unevalled args */
  109.     char evalargs;
  110.   };
  111.  
  112. #ifdef MULTI_KBOARD
  113. KBOARD *initial_kboard;
  114. KBOARD *current_kboard;
  115. KBOARD *all_kboards;
  116. int single_kboard;
  117. #else
  118. KBOARD the_only_kboard;
  119. #endif
  120.  
  121. /* Non-nil disable property on a command means
  122.    do not execute it; call disabled-command-hook's value instead.  */
  123. Lisp_Object Qdisabled, Qdisabled_command_hook;
  124.  
  125. #define NUM_RECENT_KEYS (100)
  126. int recent_keys_index;    /* Index for storing next element into recent_keys */
  127. int total_keys;        /* Total number of elements stored into recent_keys */
  128. Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
  129.  
  130. /* Vector holding the key sequence that invoked the current command.
  131.    It is reused for each command, and it may be longer than the current
  132.    sequence; this_command_key_count indicates how many elements
  133.    actually mean something.
  134.    It's easier to staticpro a single Lisp_Object than an array.  */
  135. Lisp_Object this_command_keys;
  136. int this_command_key_count;
  137.  
  138. /* This vector is used as a buffer to record the events that were actually read
  139.    by read_key_sequence.  */
  140. Lisp_Object raw_keybuf;
  141. int raw_keybuf_count;
  142.  
  143. #define GROW_RAW_KEYBUF                            \
  144. if (raw_keybuf_count == XVECTOR (raw_keybuf)->size)            \
  145.   {                                    \
  146.     int newsize = 2 * XVECTOR (raw_keybuf)->size;            \
  147.     Lisp_Object new;                            \
  148.     new = Fmake_vector (make_number (newsize), Qnil);            \
  149.     bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents,    \
  150.        raw_keybuf_count * sizeof (Lisp_Object));            \
  151.     raw_keybuf = new;                            \
  152.   }
  153.  
  154. /* Number of elements of this_command_keys
  155.    that precede this key sequence.  */
  156. int this_single_command_key_start;
  157.  
  158. /* Record values of this_command_key_count and echo_length ()
  159.    before this command was read.  */
  160. static int before_command_key_count;
  161. static int before_command_echo_length;
  162. /* Values of before_command_key_count and before_command_echo_length
  163.    saved by reset-this-command-lengths.  */
  164. static int before_command_key_count_1;
  165. static int before_command_echo_length_1;
  166. /* Flag set by reset-this-command-lengths,
  167.    saying to reset the lengths when add_command_key is called.  */
  168. static int before_command_restore_flag;
  169.  
  170. extern int minbuf_level;
  171.  
  172. extern int message_enable_multibyte;
  173.  
  174. extern struct backtrace *backtrace_list;
  175.  
  176. /* Nonzero means do menu prompting.  */
  177. static int menu_prompting;
  178.  
  179. /* Character to see next line of menu prompt.  */
  180. static Lisp_Object menu_prompt_more_char;
  181.  
  182. /* For longjmp to where kbd input is being done.  */
  183. static jmp_buf getcjmp;
  184.  
  185. /* True while doing kbd input.  */
  186. int waiting_for_input;
  187.  
  188. /* True while displaying for echoing.   Delays C-g throwing.  */
  189. static int echoing;
  190.  
  191. /* True means we can start echoing at the next input pause
  192.    even though there is something in the echo area.  */
  193. static char *ok_to_echo_at_next_pause;
  194.  
  195. /* Nonzero means disregard local maps for the menu bar.  */
  196. static int inhibit_local_menu_bar_menus;
  197.  
  198. /* Nonzero means C-g should cause immediate error-signal.  */
  199. int immediate_quit;
  200.  
  201. /* The user's ERASE setting.  */
  202. Lisp_Object Vtty_erase_char;
  203.  
  204. /* Character to recognize as the help char.  */
  205. Lisp_Object Vhelp_char;
  206.  
  207. /* List of other event types to recognize as meaning "help".  */
  208. Lisp_Object Vhelp_event_list;
  209.  
  210. /* Form to execute when help char is typed.  */
  211. Lisp_Object Vhelp_form;
  212.  
  213. /* Command to run when the help character follows a prefix key.  */
  214. Lisp_Object Vprefix_help_command;
  215.  
  216. /* List of items that should move to the end of the menu bar.  */
  217. Lisp_Object Vmenu_bar_final_items;
  218.  
  219. /* Non-nil means show the equivalent key-binding for
  220.    any M-x command that has one.
  221.    The value can be a length of time to show the message for.
  222.    If the value is non-nil and not a number, we wait 2 seconds.  */
  223. Lisp_Object Vsuggest_key_bindings;
  224.  
  225. /* Character that causes a quit.  Normally C-g.
  226.  
  227.    If we are running on an ordinary terminal, this must be an ordinary
  228.    ASCII char, since we want to make it our interrupt character.
  229.  
  230.    If we are not running on an ordinary terminal, it still needs to be
  231.    an ordinary ASCII char.  This character needs to be recognized in
  232.    the input interrupt handler.  At this point, the keystroke is
  233.    represented as a struct input_event, while the desired quit
  234.    character is specified as a lispy event.  The mapping from struct
  235.    input_events to lispy events cannot run in an interrupt handler,
  236.    and the reverse mapping is difficult for anything but ASCII
  237.    keystrokes.
  238.  
  239.    FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
  240.    ASCII character.  */
  241. int quit_char;
  242.  
  243. extern Lisp_Object current_global_map;
  244. extern int minibuf_level;
  245.  
  246. /* If non-nil, this is a map that overrides all other local maps.  */
  247. Lisp_Object Voverriding_local_map;
  248.  
  249. /* If non-nil, Voverriding_local_map applies to the menu bar.  */
  250. Lisp_Object Voverriding_local_map_menu_flag;
  251.  
  252. /* Keymap that defines special misc events that should
  253.    be processed immediately at a low level.  */
  254. Lisp_Object Vspecial_event_map;
  255.  
  256. /* Current depth in recursive edits.  */
  257. int command_loop_level;
  258.  
  259. /* Total number of times command_loop has read a key sequence.  */
  260. int num_input_keys;
  261.  
  262. /* Last input character read as a command.  */
  263. Lisp_Object last_command_char;
  264.  
  265. /* Last input character read as a command, not counting menus
  266.    reached by the mouse.  */
  267. Lisp_Object last_nonmenu_event;
  268.  
  269. /* Last input character read for any purpose.  */
  270. Lisp_Object last_input_char;
  271.  
  272. /* If not Qnil, a list of objects to be read as subsequent command input.  */
  273. Lisp_Object Vunread_command_events;
  274.  
  275. /* If not Qnil, a list of objects to be read as subsequent command input
  276.    including input method processing.  */
  277. Lisp_Object Vunread_input_method_events;
  278.  
  279. /* If not Qnil, a list of objects to be read as subsequent command input
  280.    but NOT including input method processing.  */
  281. Lisp_Object Vunread_post_input_method_events;
  282.  
  283. /* If not -1, an event to be read as subsequent command input.  */
  284. int unread_command_char;
  285.  
  286. /* If not Qnil, this is a switch-frame event which we decided to put
  287.    off until the end of a key sequence.  This should be read as the
  288.    next command input, after any unread_command_events.
  289.  
  290.    read_key_sequence uses this to delay switch-frame events until the
  291.    end of the key sequence; Fread_char uses it to put off switch-frame
  292.    events until a non-ASCII event is acceptable as input.  */
  293. Lisp_Object unread_switch_frame;
  294.  
  295. /* A mask of extra modifier bits to put into every keyboard char.  */
  296. int extra_keyboard_modifiers;
  297.  
  298. /* Char to use as prefix when a meta character is typed in.
  299.    This is bound on entry to minibuffer in case ESC is changed there.  */
  300.  
  301. Lisp_Object meta_prefix_char;
  302.  
  303. /* Last size recorded for a current buffer which is not a minibuffer.  */
  304. static int last_non_minibuf_size;
  305.  
  306. /* Number of idle seconds before an auto-save and garbage collection.  */
  307. static Lisp_Object Vauto_save_timeout;
  308.  
  309. /* Total number of times read_char has returned.  */
  310. int num_input_events;
  311.  
  312. /* Total number of times read_char has returned, outside of macros.  */
  313. int num_nonmacro_input_events;
  314.  
  315. /* Auto-save automatically when this many characters have been typed
  316.    since the last time.  */
  317.  
  318. static int auto_save_interval;
  319.  
  320. /* Value of num_nonmacro_input_events as of last auto save.  */
  321.  
  322. int last_auto_save;
  323.  
  324. /* The command being executed by the command loop.
  325.    Commands may set this, and the value set will be copied into
  326.    current_kboard->Vlast_command instead of the actual command.  */
  327. Lisp_Object Vthis_command;
  328.  
  329. /* This is like Vthis_command, except that commands never set it.  */
  330. Lisp_Object real_this_command;
  331.  
  332. /* The value of point when the last command was executed.  */
  333. int last_point_position;
  334.  
  335. /* The buffer that was current when the last command was started.  */
  336. Lisp_Object last_point_position_buffer;
  337.  
  338. /* The frame in which the last input event occurred, or Qmacro if the
  339.    last event came from a macro.  We use this to determine when to
  340.    generate switch-frame events.  This may be cleared by functions
  341.    like Fselect_frame, to make sure that a switch-frame event is
  342.    generated by the next character.  */
  343. Lisp_Object internal_last_event_frame;
  344.  
  345. /* A user-visible version of the above, intended to allow users to
  346.    figure out where the last event came from, if the event doesn't
  347.    carry that information itself (i.e. if it was a character).  */
  348. Lisp_Object Vlast_event_frame;
  349.  
  350. /* The timestamp of the last input event we received from the X server.
  351.    X Windows wants this for selection ownership.  */
  352. unsigned long last_event_timestamp;
  353.  
  354. Lisp_Object Qself_insert_command;
  355. Lisp_Object Qforward_char;
  356. Lisp_Object Qbackward_char;
  357. Lisp_Object Qundefined;
  358. Lisp_Object Qtimer_event_handler;
  359.  
  360. /* read_key_sequence stores here the command definition of the
  361.    key sequence that it reads.  */
  362. Lisp_Object read_key_sequence_cmd;
  363.  
  364. /* Form to evaluate (if non-nil) when Emacs is started.  */
  365. Lisp_Object Vtop_level;
  366.  
  367. /* User-supplied string to translate input characters through.  */
  368. Lisp_Object Vkeyboard_translate_table;
  369.  
  370. /* Keymap mapping ASCII function key sequences onto their preferred forms.  */
  371. extern Lisp_Object Vfunction_key_map;
  372.  
  373. /* Another keymap that maps key sequences into key sequences.
  374.    This one takes precedence over ordinary definitions.  */
  375. extern Lisp_Object Vkey_translation_map;
  376.  
  377. /* If non-nil, this implements the current input method.  */
  378. Lisp_Object Vinput_method_function;
  379. Lisp_Object Qinput_method_function;
  380.  
  381. /* When we call Vinput_method_function,
  382.    this holds the echo area message that was just erased.  */
  383. Lisp_Object Vinput_method_previous_message;
  384.  
  385. /* Non-nil means deactivate the mark at end of this command.  */
  386. Lisp_Object Vdeactivate_mark;
  387.  
  388. /* Menu bar specified in Lucid Emacs fashion.  */
  389.  
  390. Lisp_Object Vlucid_menu_bar_dirty_flag;
  391. Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
  392.  
  393. Lisp_Object Qecho_area_clear_hook;
  394.  
  395. /* Hooks to run before and after each command.  */
  396. Lisp_Object Qpre_command_hook, Vpre_command_hook;
  397. Lisp_Object Qpost_command_hook, Vpost_command_hook;
  398. Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
  399. /* Hook run after a command if there's no more input soon.  */
  400. Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
  401.  
  402. /* Delay time in microseconds before running post-command-idle-hook.  */
  403. int post_command_idle_delay;
  404.  
  405. /* List of deferred actions to be performed at a later time.
  406.    The precise format isn't relevant here; we just check whether it is nil.  */
  407. Lisp_Object Vdeferred_action_list;
  408.  
  409. /* Function to call to handle deferred actions, when there are any.  */
  410. Lisp_Object Vdeferred_action_function;
  411. Lisp_Object Qdeferred_action_function;
  412.  
  413. Lisp_Object Qinput_method_exit_on_first_char;
  414. Lisp_Object Qinput_method_use_echo_area;
  415.  
  416. /* File in which we write all commands we read.  */
  417. FILE *dribble;
  418.  
  419. /* Nonzero if input is available.  */
  420. int input_pending;
  421.  
  422. /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
  423.    keep 0200 bit in input chars.  0 to ignore the 0200 bit.  */
  424.  
  425. int meta_key;
  426.  
  427. extern char *pending_malloc_warning;
  428.  
  429. /* Circular buffer for pre-read keyboard input.  */
  430. static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
  431.  
  432. /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
  433.  
  434.    The interrupt-level event handlers will never enqueue an event on a
  435.    frame which is not in Vframe_list, and once an event is dequeued,
  436.    internal_last_event_frame or the event itself points to the frame.
  437.    So that's all fine.
  438.  
  439.    But while the event is sitting in the queue, it's completely
  440.    unprotected.  Suppose the user types one command which will run for
  441.    a while and then delete a frame, and then types another event at
  442.    the frame that will be deleted, before the command gets around to
  443.    it.  Suppose there are no references to this frame elsewhere in
  444.    Emacs, and a GC occurs before the second event is dequeued.  Now we
  445.    have an event referring to a freed frame, which will crash Emacs
  446.    when it is dequeued.
  447.  
  448.    Similar things happen when an event on a scroll bar is enqueued; the
  449.    window may be deleted while the event is in the queue.
  450.  
  451.    So, we use this vector to protect the frame_or_window field in the
  452.    event queue.  That way, they'll be dequeued as dead frames or
  453.    windows, but still valid lisp objects.
  454.  
  455.    If kbd_buffer[i].kind != no_event, then
  456.      (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
  457.       == kbd_buffer[i].frame_or_window.  */
  458. static Lisp_Object kbd_buffer_frame_or_window;
  459.  
  460. /* Pointer to next available character in kbd_buffer.
  461.    If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
  462.    This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
  463.    next available char is in kbd_buffer[0].  */
  464. static struct input_event *kbd_fetch_ptr;
  465.  
  466. /* Pointer to next place to store character in kbd_buffer.  This
  467.    may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
  468.    character should go in kbd_buffer[0].  */
  469. static volatile struct input_event *kbd_store_ptr;
  470.  
  471. /* The above pair of variables forms a "queue empty" flag.  When we
  472.    enqueue a non-hook event, we increment kbd_store_ptr.  When we
  473.    dequeue a non-hook event, we increment kbd_fetch_ptr.  We say that
  474.    there is input available iff the two pointers are not equal.
  475.  
  476.    Why not just have a flag set and cleared by the enqueuing and
  477.    dequeuing functions?  Such a flag could be screwed up by interrupts
  478.    at inopportune times.  */
  479.  
  480. /* If this flag is non-nil, we check mouse_moved to see when the
  481.    mouse moves, and motion events will appear in the input stream.
  482.    Otherwise, mouse motion is ignored.  */
  483. static Lisp_Object do_mouse_tracking;
  484.  
  485. /* Symbols to head events.  */
  486. Lisp_Object Qmouse_movement;
  487. Lisp_Object Qscroll_bar_movement;
  488. Lisp_Object Qswitch_frame;
  489. Lisp_Object Qdelete_frame;
  490. Lisp_Object Qiconify_frame;
  491. Lisp_Object Qmake_frame_visible;
  492.  
  493. /* Symbols to denote kinds of events.  */
  494. Lisp_Object Qfunction_key;
  495. Lisp_Object Qmouse_click;
  496. #ifdef WINDOWSNT
  497. Lisp_Object Qmouse_wheel;
  498. #endif
  499. Lisp_Object Qdrag_n_drop;
  500. /* Lisp_Object Qmouse_movement; - also an event header */
  501.  
  502. /* Properties of event headers.  */
  503. Lisp_Object Qevent_kind;
  504. Lisp_Object Qevent_symbol_elements;
  505.  
  506. /* menu item parts */
  507. Lisp_Object Qmenu_alias;
  508. Lisp_Object Qmenu_enable;
  509. Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
  510. Lisp_Object QCbutton, QCtoggle, QCradio;
  511. extern Lisp_Object Vdefine_key_rebound_commands;
  512. extern Lisp_Object Qmenu_item;
  513.  
  514. /* An event header symbol HEAD may have a property named
  515.    Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
  516.    BASE is the base, unmodified version of HEAD, and MODIFIERS is the
  517.    mask of modifiers applied to it.  If present, this is used to help
  518.    speed up parse_modifiers.  */
  519. Lisp_Object Qevent_symbol_element_mask;
  520.  
  521. /* An unmodified event header BASE may have a property named
  522.    Qmodifier_cache, which is an alist mapping modifier masks onto
  523.    modified versions of BASE.  If present, this helps speed up
  524.    apply_modifiers.  */
  525. Lisp_Object Qmodifier_cache;
  526.  
  527. /* Symbols to use for parts of windows.  */
  528. Lisp_Object Qmode_line;
  529. Lisp_Object Qvertical_line;
  530. Lisp_Object Qvertical_scroll_bar;
  531. Lisp_Object Qmenu_bar;
  532.  
  533. extern Lisp_Object Qmenu_enable;
  534.  
  535. Lisp_Object recursive_edit_unwind (), command_loop ();
  536. Lisp_Object Fthis_command_keys ();
  537. Lisp_Object Qextended_command_history;
  538. EMACS_TIME timer_check ();
  539.  
  540. extern Lisp_Object Vhistory_length;
  541.  
  542. extern char *x_get_keysym_name ();
  543.  
  544. static void record_menu_key ();
  545.  
  546. Lisp_Object Qpolling_period;
  547.  
  548. /* List of absolute timers.  Appears in order of next scheduled event.  */
  549. Lisp_Object Vtimer_list;
  550.  
  551. /* List of idle time timers.  Appears in order of next scheduled event.  */
  552. Lisp_Object Vtimer_idle_list;
  553.  
  554. /* Incremented whenever a timer is run.  */
  555. int timers_run;
  556.  
  557. extern Lisp_Object Vprint_level, Vprint_length;
  558.  
  559. /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
  560.    happens.  */
  561. EMACS_TIME *input_available_clear_time;
  562.  
  563. /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
  564.    Default is 1 if INTERRUPT_INPUT is defined.  */
  565. int interrupt_input;
  566.  
  567. /* Nonzero while interrupts are temporarily deferred during redisplay.  */
  568. int interrupts_deferred;
  569.  
  570. /* Nonzero means use ^S/^Q for flow control.  */
  571. int flow_control;
  572.  
  573. /* Allow m- file to inhibit use of FIONREAD.  */
  574. #ifdef BROKEN_FIONREAD
  575. #undef FIONREAD
  576. #endif
  577.  
  578. /* We are unable to use interrupts if FIONREAD is not available,
  579.    so flush SIGIO so we won't try.  */
  580. #ifndef FIONREAD
  581. #ifdef SIGIO
  582. #undef SIGIO
  583. #endif
  584. #endif
  585.  
  586. /* If we support a window system, turn on the code to poll periodically
  587.    to detect C-g.  It isn't actually used when doing interrupt input.  */
  588. #ifdef HAVE_WINDOW_SYSTEM
  589. /* pmemacs sends SIGINT to Emacs when C-g is hit.  We don't have to
  590.    poll. */
  591. #ifndef HAVE_PM
  592. #define POLL_FOR_INPUT
  593. #endif /* not HAVE_PM */
  594. #endif
  595.  
  596. /* Non-nil enables Column Number mode.  */
  597. Lisp_Object Vcolumn_number_mode;
  598.  
  599. /* Global variable declarations.  */
  600.  
  601. /* Function for init_keyboard to call with no args (if nonzero).  */
  602. void (*keyboard_init_hook) ();
  603.  
  604. static int read_avail_input ();
  605. static void get_input_pending ();
  606. static int readable_events ();
  607. static Lisp_Object read_char_x_menu_prompt ();
  608. static Lisp_Object read_char_minibuf_menu_prompt ();
  609. static Lisp_Object make_lispy_event ();
  610. #ifdef HAVE_MOUSE
  611. static Lisp_Object make_lispy_movement ();
  612. #endif
  613. static Lisp_Object modify_event_symbol ();
  614. static Lisp_Object make_lispy_switch_frame ();
  615. static int parse_solitary_modifier ();
  616. static void save_getcjmp ();
  617. static void restore_getcjmp ();
  618.  
  619. /* > 0 if we are to echo keystrokes.  */
  620. static int echo_keystrokes;
  621.  
  622. /* Nonzero means don't try to suspend even if the operating system seems
  623.    to support it.  */
  624. static int cannot_suspend;
  625.  
  626. #define    min(a,b)    ((a)<(b)?(a):(b))
  627. #define    max(a,b)    ((a)>(b)?(a):(b))
  628.  
  629. /* Install the string STR as the beginning of the string of echoing,
  630.    so that it serves as a prompt for the next character.
  631.    Also start echoing.  */
  632.  
  633. void
  634. echo_prompt (str)
  635.      char *str;
  636. {
  637.   int len = strlen (str);
  638.  
  639.   if (len > ECHOBUFSIZE - 4)
  640.     len = ECHOBUFSIZE - 4;
  641.   bcopy (str, current_kboard->echobuf, len);
  642.   current_kboard->echoptr = current_kboard->echobuf + len;
  643.   *current_kboard->echoptr = '\0';
  644.  
  645.   current_kboard->echo_after_prompt = len;
  646.  
  647.   echo_now ();
  648. }
  649.  
  650. /* Add C to the echo string, if echoing is going on.
  651.    C can be a character, which is printed prettily ("M-C-x" and all that
  652.    jazz), or a symbol, whose name is printed.  */
  653.  
  654. void
  655. echo_char (c)
  656.      Lisp_Object c;
  657. {
  658.   extern char *push_key_description ();
  659.  
  660.   if (current_kboard->immediate_echo)
  661.     {
  662.       char *ptr = current_kboard->echoptr;
  663.  
  664.       if (ptr != current_kboard->echobuf)
  665.     *ptr++ = ' ';
  666.  
  667.       /* If someone has passed us a composite event, use its head symbol.  */
  668.       c = EVENT_HEAD (c);
  669.  
  670.       if (INTEGERP (c))
  671.     {
  672.       if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
  673.         return;
  674.  
  675.       ptr = push_key_description (XINT (c), ptr);
  676.     }
  677.       else if (SYMBOLP (c))
  678.     {
  679.       struct Lisp_String *name = XSYMBOL (c)->name;
  680.       if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
  681.           > ECHOBUFSIZE)
  682.         return;
  683.       bcopy (name->data, ptr, STRING_BYTES (name));
  684.       ptr += STRING_BYTES (name);
  685.     }
  686.  
  687.       if (current_kboard->echoptr == current_kboard->echobuf
  688.       && help_char_p (c))
  689.     {
  690.       strcpy (ptr, " (Type ? for further options)");
  691.       ptr += strlen (ptr);
  692.     }
  693.  
  694.       *ptr = 0;
  695.       current_kboard->echoptr = ptr;
  696.  
  697.       echo_now ();
  698.     }
  699. }
  700.  
  701. /* Temporarily add a dash to the end of the echo string if it's not
  702.    empty, so that it serves as a mini-prompt for the very next character.  */
  703.  
  704. void
  705. echo_dash ()
  706. {
  707.   if (!current_kboard->immediate_echo
  708.       && current_kboard->echoptr == current_kboard->echobuf)
  709.     return;
  710.   /* Do nothing if we just printed a prompt.  */
  711.   if (current_kboard->echo_after_prompt
  712.       == current_kboard->echoptr - current_kboard->echobuf)
  713.     return;
  714.   /* Do nothing if not echoing at all.  */
  715.   if (current_kboard->echoptr == 0)
  716.     return;
  717.  
  718.   /* Put a dash at the end of the buffer temporarily,
  719.      but make it go away when the next character is added.  */
  720.   current_kboard->echoptr[0] = '-';
  721.   current_kboard->echoptr[1] = 0;
  722.  
  723.   echo_now ();
  724. }
  725.  
  726. /* Display the current echo string, and begin echoing if not already
  727.    doing so.  */
  728.  
  729. void
  730. echo_now ()
  731. {
  732.   if (!current_kboard->immediate_echo)
  733.     {
  734.       int i;
  735.       current_kboard->immediate_echo = 1;
  736.  
  737.       for (i = 0; i < this_command_key_count; i++)
  738.     {
  739.       Lisp_Object c;
  740.       c = XVECTOR (this_command_keys)->contents[i];
  741.       if (! (EVENT_HAS_PARAMETERS (c)
  742.          && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
  743.         echo_char (c);
  744.     }
  745.       echo_dash ();
  746.     }
  747.  
  748.   echoing = 1;
  749.   message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
  750.           ! NILP (current_buffer->enable_multibyte_characters));
  751.  
  752.   echoing = 0;
  753.  
  754.   if (waiting_for_input && !NILP (Vquit_flag))
  755.     quit_throw_to_read_char ();
  756. }
  757.  
  758. /* Turn off echoing, for the start of a new command.  */
  759.  
  760. void
  761. cancel_echoing ()
  762. {
  763.   current_kboard->immediate_echo = 0;
  764.   current_kboard->echoptr = current_kboard->echobuf;
  765.   current_kboard->echo_after_prompt = -1;
  766.   ok_to_echo_at_next_pause = 0;
  767. }
  768.  
  769. /* Return the length of the current echo string.  */
  770.  
  771. static int
  772. echo_length ()
  773. {
  774.   return current_kboard->echoptr - current_kboard->echobuf;
  775. }
  776.  
  777. /* Truncate the current echo message to its first LEN chars.
  778.    This and echo_char get used by read_key_sequence when the user
  779.    switches frames while entering a key sequence.  */
  780.  
  781. static void
  782. echo_truncate (len)
  783.      int len;
  784. {
  785.   current_kboard->echobuf[len] = '\0';
  786.   current_kboard->echoptr = current_kboard->echobuf + len;
  787.   truncate_echo_area (len);
  788. }
  789.  
  790.  
  791. /* Functions for manipulating this_command_keys.  */
  792. static void
  793. add_command_key (key)
  794.      Lisp_Object key;
  795. {
  796.   int size = XVECTOR (this_command_keys)->size;
  797.  
  798.   /* If reset-this-command-length was called recently, obey it now.
  799.      See the doc string of that function for an explanation of why.  */
  800.   if (before_command_restore_flag)
  801.     {
  802.       this_command_key_count = before_command_key_count_1;
  803.       if (this_command_key_count < this_single_command_key_start)
  804.     this_single_command_key_start = this_command_key_count;
  805.       echo_truncate (before_command_echo_length_1);
  806.       before_command_restore_flag = 0;
  807.     }
  808.  
  809.   if (this_command_key_count >= size)
  810.     {
  811.       Lisp_Object new_keys;
  812.  
  813.       new_keys = Fmake_vector (make_number (size * 2), Qnil);
  814.       bcopy (XVECTOR (this_command_keys)->contents,
  815.          XVECTOR (new_keys)->contents,
  816.          size * sizeof (Lisp_Object));
  817.  
  818.       this_command_keys = new_keys;
  819.     }
  820.  
  821.   XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
  822. }
  823.  
  824. Lisp_Object
  825. recursive_edit_1 ()
  826. {
  827.   int count = specpdl_ptr - specpdl;
  828.   Lisp_Object val;
  829.  
  830.   if (command_loop_level > 0)
  831.     {
  832.       specbind (Qstandard_output, Qt);
  833.       specbind (Qstandard_input, Qt);
  834.     }
  835.  
  836.   val = command_loop ();
  837.   if (EQ (val, Qt))
  838.     Fsignal (Qquit, Qnil);
  839.   /* Handle throw from read_minibuf when using minibuffer
  840.      while it's active but we're in another window.  */
  841.   if (STRINGP (val))
  842.     Fsignal (Qerror, Fcons (val, Qnil));
  843.  
  844.   return unbind_to (count, Qnil);
  845. }
  846.  
  847. /* When an auto-save happens, record the "time", and don't do again soon.  */
  848.  
  849. void
  850. record_auto_save ()
  851. {
  852.   last_auto_save = num_nonmacro_input_events;
  853. }
  854.  
  855. /* Make an auto save happen as soon as possible at command level.  */
  856.  
  857. void
  858. force_auto_save_soon ()
  859. {
  860.   last_auto_save = - auto_save_interval - 1;
  861.  
  862.   record_asynch_buffer_change ();
  863. }
  864.  
  865. DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
  866.   "Invoke the editor command loop recursively.\n\
  867. To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
  868. that tells this function to return.\n\
  869. Alternately, `(throw 'exit t)' makes this function signal an error.\n\
  870. This function is called by the editor initialization to begin editing.")
  871.   ()
  872. {
  873.   int count = specpdl_ptr - specpdl;
  874.   Lisp_Object val;
  875.  
  876.   command_loop_level++;
  877.   update_mode_lines = 1;
  878.  
  879.   record_unwind_protect (recursive_edit_unwind,
  880.              (command_loop_level
  881.               && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
  882.              ? Fcurrent_buffer ()
  883.              : Qnil);
  884.   recursive_edit_1 ();
  885.   return unbind_to (count, Qnil);
  886. }
  887.  
  888. Lisp_Object
  889. recursive_edit_unwind (buffer)
  890.      Lisp_Object buffer;
  891. {
  892.   if (!NILP (buffer))
  893.     Fset_buffer (buffer);
  894.  
  895.   command_loop_level--;
  896.   update_mode_lines = 1;
  897.   return Qnil;
  898. }
  899.  
  900. static void
  901. any_kboard_state ()
  902. {
  903. #ifdef MULTI_KBOARD
  904. #if 0 /* Theory: if there's anything in Vunread_command_events,
  905.      it will right away be read by read_key_sequence,
  906.      and then if we do switch KBOARDS, it will go into the side
  907.      queue then.  So we don't need to do anything special here -- rms.  */
  908.   if (CONSP (Vunread_command_events))
  909.     {
  910.       current_kboard->kbd_queue
  911.     = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
  912.       current_kboard->kbd_queue_has_data = 1;
  913.     }
  914.   Vunread_command_events = Qnil;
  915. #endif
  916.   single_kboard = 0;
  917. #endif
  918. }
  919.  
  920. /* Switch to the single-kboard state, making current_kboard
  921.    the only KBOARD from which further input is accepted.  */
  922.  
  923. void
  924. single_kboard_state ()
  925. {
  926. #ifdef MULTI_KBOARD
  927.   single_kboard = 1;
  928. #endif
  929. }
  930.  
  931. /* Maintain a stack of kboards, so other parts of Emacs
  932.    can switch temporarily to the kboard of a given frame
  933.    and then revert to the previous status.  */
  934.  
  935. struct kboard_stack
  936. {
  937.   KBOARD *kboard;
  938.   struct kboard_stack *next;
  939. };
  940.  
  941. static struct kboard_stack *kboard_stack;
  942.  
  943. void
  944. push_frame_kboard (f)
  945.      FRAME_PTR f;
  946. {
  947. #ifdef MULTI_KBOARD
  948.   struct kboard_stack *p
  949.     = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
  950.  
  951.   p->next = kboard_stack;
  952.   p->kboard = current_kboard;
  953.   kboard_stack = p;
  954.  
  955.   current_kboard = FRAME_KBOARD (f);
  956. #endif
  957. }
  958.  
  959. void
  960. pop_frame_kboard ()
  961. {
  962. #ifdef MULTI_KBOARD
  963.   struct kboard_stack *p = kboard_stack;
  964.   current_kboard = p->kboard;
  965.   kboard_stack = p->next;
  966.   xfree (p);
  967. #endif
  968. }
  969.  
  970. /* Handle errors that are not handled at inner levels
  971.    by printing an error message and returning to the editor command loop.  */
  972.  
  973. Lisp_Object
  974. cmd_error (data)
  975.      Lisp_Object data;
  976. {
  977.   Lisp_Object old_level, old_length;
  978.   char macroerror[50];
  979.  
  980.   if (!NILP (executing_macro))
  981.     {
  982.       if (executing_macro_iterations == 1)
  983.     sprintf (macroerror, "After 1 kbd macro iteration: ");
  984.       else
  985.     sprintf (macroerror, "After %d kbd macro iterations: ",
  986.          executing_macro_iterations);
  987.     }
  988.   else
  989.     *macroerror = 0;
  990.  
  991.   Vstandard_output = Qt;
  992.   Vstandard_input = Qt;
  993.   Vexecuting_macro = Qnil;
  994.   executing_macro = Qnil;
  995.   current_kboard->Vprefix_arg = Qnil;
  996.   current_kboard->Vlast_prefix_arg = Qnil;
  997.   cancel_echoing ();
  998.  
  999.   /* Avoid unquittable loop if data contains a circular list.  */
  1000.   old_level = Vprint_level;
  1001.   old_length = Vprint_length;
  1002.   XSETFASTINT (Vprint_level, 10);
  1003.   XSETFASTINT (Vprint_length, 10);
  1004.   cmd_error_internal (data, macroerror);
  1005.   Vprint_level = old_level;
  1006.   Vprint_length = old_length;
  1007.  
  1008.   Vquit_flag = Qnil;
  1009.  
  1010.   Vinhibit_quit = Qnil;
  1011. #ifdef MULTI_KBOARD
  1012.   any_kboard_state ();
  1013. #endif
  1014.  
  1015.   return make_number (0);
  1016. }
  1017.  
  1018. /* Take actions on handling an error.  DATA is the data that describes
  1019.    the error.
  1020.  
  1021.    CONTEXT is a C-string containing ASCII characters only which
  1022.    describes the context in which the error happened.  If we need to
  1023.    generalize CONTEXT to allow multibyte characters, make it a Lisp
  1024.    string.  */
  1025.  
  1026. void
  1027. cmd_error_internal (data, context)
  1028.      Lisp_Object data;
  1029.      char *context;
  1030. {
  1031.   Lisp_Object stream;
  1032.  
  1033.   Vquit_flag = Qnil;
  1034.   Vinhibit_quit = Qt;
  1035.   echo_area_glyphs = 0;
  1036.  
  1037.   /* If the window system or terminal frame hasn't been initialized
  1038.      yet, or we're not interactive, it's best to dump this message out
  1039.      to stderr and exit.  */
  1040.   if (! FRAME_MESSAGE_BUF (selected_frame)
  1041.       || noninteractive)
  1042.     stream = Qexternal_debugging_output;
  1043.   else
  1044.     {
  1045.       Fdiscard_input ();
  1046.       bitch_at_user ();
  1047.       stream = Qt;
  1048.     }
  1049.  
  1050.   if (context != 0)
  1051.     write_string_1 (context, -1, stream);
  1052.  
  1053.   print_error_message (data, stream);
  1054.  
  1055.   /* If the window system or terminal frame hasn't been initialized
  1056.      yet, or we're in -batch mode, this error should cause Emacs to exit.  */
  1057.   if (! FRAME_MESSAGE_BUF (selected_frame)
  1058.       || noninteractive)
  1059.     {
  1060.       Fterpri (stream);
  1061.       Fkill_emacs (make_number (-1));
  1062.     }
  1063. }
  1064.  
  1065. Lisp_Object command_loop_1 ();
  1066. Lisp_Object command_loop_2 ();
  1067. Lisp_Object top_level_1 ();
  1068.  
  1069. /* Entry to editor-command-loop.
  1070.    This level has the catches for exiting/returning to editor command loop.
  1071.    It returns nil to exit recursive edit, t to abort it.  */
  1072.  
  1073. Lisp_Object
  1074. command_loop ()
  1075. {
  1076.   if (command_loop_level > 0 || minibuf_level > 0)
  1077.     {
  1078.       Lisp_Object val = internal_catch (Qexit, command_loop_2, Qnil);
  1079.       executing_macro = Qnil;
  1080.       return val;
  1081.     }
  1082.   else
  1083.     while (1)
  1084.       {
  1085.     internal_catch (Qtop_level, top_level_1, Qnil);
  1086.     internal_catch (Qtop_level, command_loop_2, Qnil);
  1087.     executing_macro = Qnil;
  1088.  
  1089.     /* End of file in -batch run causes exit here.  */
  1090.     if (noninteractive)
  1091.       Fkill_emacs (Qt);
  1092.       }
  1093. }
  1094.  
  1095. /* Here we catch errors in execution of commands within the
  1096.    editing loop, and reenter the editing loop.
  1097.    When there is an error, cmd_error runs and returns a non-nil
  1098.    value to us.  A value of nil means that cmd_loop_1 itself
  1099.    returned due to end of file (or end of kbd macro).  */
  1100.  
  1101. Lisp_Object
  1102. command_loop_2 ()
  1103. {
  1104.   register Lisp_Object val;
  1105.  
  1106.   do
  1107.     val = internal_condition_case (command_loop_1, Qerror, cmd_error);
  1108.   while (!NILP (val));
  1109.  
  1110.   return Qnil;
  1111. }
  1112.  
  1113. Lisp_Object
  1114. top_level_2 ()
  1115. {
  1116.   return Feval (Vtop_level);
  1117. }
  1118.  
  1119. Lisp_Object
  1120. top_level_1 ()
  1121. {
  1122.   /* On entry to the outer level, run the startup file */
  1123.   if (!NILP (Vtop_level))
  1124.     internal_condition_case (top_level_2, Qerror, cmd_error);
  1125.   else if (!NILP (Vpurify_flag))
  1126.     message ("Bare impure Emacs (standard Lisp code not loaded)");
  1127.   else
  1128.     message ("Bare Emacs (standard Lisp code not loaded)");
  1129.   return Qnil;
  1130. }
  1131.  
  1132. DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
  1133.   "Exit all recursive editing levels.")
  1134.   ()
  1135. {
  1136.   Fthrow (Qtop_level, Qnil);
  1137. }
  1138.  
  1139. DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
  1140.   "Exit from the innermost recursive edit or minibuffer.")
  1141.   ()
  1142. {
  1143.   if (command_loop_level > 0 || minibuf_level > 0)
  1144.     Fthrow (Qexit, Qnil);
  1145.  
  1146.   error ("No recursive edit is in progress");
  1147. }
  1148.  
  1149. DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
  1150.   "Abort the command that requested this recursive edit or minibuffer input.")
  1151.   ()
  1152. {
  1153.   if (command_loop_level > 0 || minibuf_level > 0)
  1154.     Fthrow (Qexit, Qt);
  1155.  
  1156.   error ("No recursive edit is in progress");
  1157. }
  1158.  
  1159. /* This is the actual command reading loop,
  1160.    sans error-handling encapsulation.  */
  1161.  
  1162. Lisp_Object Fcommand_execute ();
  1163. static int read_key_sequence ();
  1164. void safe_run_hooks ();
  1165.  
  1166. Lisp_Object
  1167. command_loop_1 ()
  1168. {
  1169.   Lisp_Object cmd, tem;
  1170.   int lose, lose2;
  1171.   int nonundocount;
  1172.   Lisp_Object keybuf[30];
  1173.   int i;
  1174.   int no_redisplay;
  1175.   int no_direct;
  1176.   int prev_modiff;
  1177.   struct buffer *prev_buffer;
  1178. #ifdef MULTI_KBOARD
  1179.   int was_locked = single_kboard;
  1180. #endif
  1181.  
  1182.   current_kboard->Vprefix_arg = Qnil;
  1183.   current_kboard->Vlast_prefix_arg = Qnil;
  1184.   Vdeactivate_mark = Qnil;
  1185.   waiting_for_input = 0;
  1186.   cancel_echoing ();
  1187.  
  1188.   nonundocount = 0;
  1189.   no_redisplay = 0;
  1190.   this_command_key_count = 0;
  1191.   this_single_command_key_start = 0;
  1192.  
  1193.   /* Make sure this hook runs after commands that get errors and
  1194.      throw to top level.  */
  1195.   /* Note that the value cell will never directly contain nil
  1196.      if the symbol is a local variable.  */
  1197.   if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
  1198.     safe_run_hooks (Qpost_command_hook);
  1199.  
  1200.   if (!NILP (Vdeferred_action_list))
  1201.     call0 (Vdeferred_action_function);
  1202.  
  1203.   if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
  1204.     {
  1205.       if (NILP (Vunread_command_events)
  1206.       && NILP (Vunread_input_method_events)
  1207.       && NILP (Vunread_post_input_method_events)
  1208.       && NILP (Vexecuting_macro)
  1209.       && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
  1210.     safe_run_hooks (Qpost_command_idle_hook);
  1211.     }
  1212.  
  1213.   /* Do this after running Vpost_command_hook, for consistency.  */
  1214.   current_kboard->Vlast_command = Vthis_command;
  1215.   current_kboard->Vreal_last_command = real_this_command;
  1216.  
  1217.   while (1)
  1218.     {
  1219.       /* Make sure the current window's buffer is selected.  */
  1220.       if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  1221.     set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
  1222.  
  1223.       /* Display any malloc warning that just came out.  Use while because
  1224.      displaying one warning can cause another.  */
  1225.  
  1226.       while (pending_malloc_warning)
  1227.     display_malloc_warning ();
  1228.  
  1229.       no_direct = 0;
  1230.  
  1231.       Vdeactivate_mark = Qnil;
  1232.  
  1233.       /* If minibuffer on and echo area in use,
  1234.      wait 2 sec and redraw minibuffer.  */
  1235.  
  1236.       if (minibuf_level && echo_area_glyphs
  1237.       && EQ (minibuf_window, echo_area_window))
  1238.     {
  1239.       /* Bind inhibit-quit to t so that C-g gets read in
  1240.          rather than quitting back to the minibuffer.  */
  1241.       int count = specpdl_ptr - specpdl;
  1242.       specbind (Qinhibit_quit, Qt);
  1243.  
  1244.       Fsit_for (make_number (2), Qnil, Qnil);
  1245.       /* Clear the echo area.  */
  1246.       message2 (0, 0, 0);
  1247.       safe_run_hooks (Qecho_area_clear_hook);
  1248.  
  1249.       unbind_to (count, Qnil);
  1250.  
  1251.       /* If a C-g came in before, treat it as input now.  */
  1252.       if (!NILP (Vquit_flag))
  1253.         {
  1254.           Vquit_flag = Qnil;
  1255.           Vunread_command_events = Fcons (make_number (quit_char), Qnil);
  1256.         }
  1257.     }
  1258.  
  1259. #ifdef C_ALLOCA
  1260.       alloca (0);        /* Cause a garbage collection now */
  1261.                 /* Since we can free the most stuff here.  */
  1262. #endif /* C_ALLOCA */
  1263.  
  1264. #if 0
  1265.       /* Select the frame that the last event came from.  Usually,
  1266.      switch-frame events will take care of this, but if some lisp
  1267.      code swallows a switch-frame event, we'll fix things up here.
  1268.      Is this a good idea?  */
  1269.       if (FRAMEP (internal_last_event_frame)
  1270.       && XFRAME (internal_last_event_frame) != selected_frame)
  1271.     Fselect_frame (internal_last_event_frame, Qnil);
  1272. #endif
  1273.       /* If it has changed current-menubar from previous value,
  1274.      really recompute the menubar from the value.  */
  1275.       if (! NILP (Vlucid_menu_bar_dirty_flag)
  1276.       && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
  1277.     call0 (Qrecompute_lucid_menubar);
  1278.  
  1279.       before_command_key_count = this_command_key_count;
  1280.       before_command_echo_length = echo_length ();
  1281.  
  1282.       Vthis_command = Qnil;
  1283.       real_this_command = Qnil;
  1284.  
  1285.       /* Read next key sequence; i gets its length.  */
  1286.       i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
  1287.                  Qnil, 0, 1, 1);
  1288.  
  1289.       /* A filter may have run while we were reading the input.  */
  1290.       if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  1291.     set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
  1292.  
  1293.       ++num_input_keys;
  1294.  
  1295.       /* Now we have read a key sequence of length I,
  1296.      or else I is 0 and we found end of file.  */
  1297.  
  1298.       if (i == 0)        /* End of file -- happens only in */
  1299.     return Qnil;        /* a kbd macro, at the end.  */
  1300.       /* -1 means read_key_sequence got a menu that was rejected.
  1301.      Just loop around and read another command.  */
  1302.       if (i == -1)
  1303.     {
  1304.       cancel_echoing ();
  1305.       this_command_key_count = 0;
  1306.       this_single_command_key_start = 0;
  1307.       goto finalize;
  1308.     }
  1309.  
  1310.       last_command_char = keybuf[i - 1];
  1311.  
  1312.       /* If the previous command tried to force a specific window-start,
  1313.      forget about that, in case this command moves point far away
  1314.      from that position.  But also throw away beg_unchanged and
  1315.      end_unchanged information in that case, so that redisplay will
  1316.      update the whole window properly.  */
  1317.       if (!NILP (XWINDOW (selected_window)->force_start))
  1318.     {
  1319.       XWINDOW (selected_window)->force_start = Qnil;
  1320.       beg_unchanged = end_unchanged = 0;
  1321.     }
  1322.  
  1323.       cmd = read_key_sequence_cmd;
  1324.       if (!NILP (Vexecuting_macro))
  1325.     {
  1326.       if (!NILP (Vquit_flag))
  1327.         {
  1328.           Vexecuting_macro = Qt;
  1329.           QUIT;        /* Make some noise. */
  1330.                 /* Will return since macro now empty. */
  1331.         }
  1332.     }
  1333.  
  1334.       /* Do redisplay processing after this command except in special
  1335.      cases identified below that set no_redisplay to 1.
  1336.      (actually, there's currently no way to prevent the redisplay,
  1337.      and no_redisplay is ignored.
  1338.      Perhaps someday we will really implement it.)  */
  1339.       no_redisplay = 0;
  1340.  
  1341.       prev_buffer = current_buffer;
  1342.       prev_modiff = MODIFF;
  1343.       last_point_position = PT;
  1344.       XSETBUFFER (last_point_position_buffer, prev_buffer);
  1345.  
  1346.       /* Execute the command.  */
  1347.  
  1348.       Vthis_command = cmd;
  1349.       real_this_command = cmd;
  1350.       /* Note that the value cell will never directly contain nil
  1351.      if the symbol is a local variable.  */
  1352.       if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
  1353.     safe_run_hooks (Qpre_command_hook);
  1354.       
  1355.       if (NILP (Vthis_command))
  1356.     {
  1357.       /* nil means key is undefined.  */
  1358.       bitch_at_user ();
  1359.       current_kboard->defining_kbd_macro = Qnil;
  1360.       update_mode_lines = 1;
  1361.       current_kboard->Vprefix_arg = Qnil;
  1362.     }
  1363.       else
  1364.     {
  1365.       if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
  1366.         {
  1367.           /* In case we jump to directly_done.  */
  1368.           Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
  1369.  
  1370.           /* Recognize some common commands in common situations and
  1371.          do them directly.  */
  1372.           if (EQ (Vthis_command, Qforward_char) && PT < ZV)
  1373.         {
  1374.                   struct Lisp_Char_Table *dp
  1375.             = window_display_table (XWINDOW (selected_window));
  1376.           lose = FETCH_CHAR (PT_BYTE);
  1377.           SET_PT (PT + 1);
  1378.           if ((dp
  1379.                ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
  1380.               ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
  1381.                           : (NILP (DISP_CHAR_VECTOR (dp, lose))
  1382.                              && (lose >= 0x20 && lose < 0x7f)))
  1383.                : (lose >= 0x20 && lose < 0x7f))
  1384.               /* To extract the case of continuation on
  1385.                          wide-column characters.  */
  1386.               && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
  1387.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  1388.               >= MODIFF)
  1389.               && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
  1390.               >= OVERLAY_MODIFF)
  1391.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  1392.               == PT - 1)
  1393.               && !windows_or_buffers_changed
  1394.               && EQ (current_buffer->selective_display, Qnil)
  1395.               && !detect_input_pending ()
  1396.               && NILP (XWINDOW (selected_window)->column_number_displayed)
  1397.               && NILP (Vexecuting_macro))
  1398.             no_redisplay = direct_output_forward_char (1);
  1399.           goto directly_done;
  1400.         }
  1401.           else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
  1402.         {
  1403.                   struct Lisp_Char_Table *dp
  1404.             = window_display_table (XWINDOW (selected_window));
  1405.           SET_PT (PT - 1);
  1406.           lose = FETCH_CHAR (PT_BYTE);
  1407.           if ((dp
  1408.                ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
  1409.               ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
  1410.                           : (NILP (DISP_CHAR_VECTOR (dp, lose))
  1411.                              && (lose >= 0x20 && lose < 0x7f)))
  1412.                : (lose >= 0x20 && lose < 0x7f))
  1413.               && (XFASTINT (XWINDOW (selected_window)->last_modified)
  1414.               >= MODIFF)
  1415.               && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
  1416.               >= OVERLAY_MODIFF)
  1417.               && (XFASTINT (XWINDOW (selected_window)->last_point)
  1418.               == PT + 1)
  1419.               && !windows_or_buffers_changed
  1420.               && EQ (current_buffer->selective_display, Qnil)
  1421.               && !detect_input_pending ()
  1422.               && NILP (XWINDOW (selected_window)->column_number_displayed)
  1423.               && NILP (Vexecuting_macro))
  1424.             no_redisplay = direct_output_forward_char (-1);
  1425.           goto directly_done;
  1426.         }
  1427.           else if (EQ (Vthis_command, Qself_insert_command)
  1428.                /* Try this optimization only on ascii keystrokes.  */
  1429.                && INTEGERP (last_command_char))
  1430.         {
  1431.           unsigned int c = XINT (last_command_char);
  1432.           int value;
  1433.  
  1434.           if (NILP (Vexecuting_macro)
  1435.               && !EQ (minibuf_window, selected_window))
  1436.             {
  1437.               if (!nonundocount || nonundocount >= 20)
  1438.             {
  1439.               Fundo_boundary ();
  1440.               nonundocount = 0;
  1441.             }
  1442.               nonundocount++;
  1443.             }
  1444.           lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
  1445.                < MODIFF)
  1446.               || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
  1447.                   < OVERLAY_MODIFF)
  1448.               || (XFASTINT (XWINDOW (selected_window)->last_point)
  1449.                   != PT)
  1450.               || MODIFF <= SAVE_MODIFF
  1451.               || windows_or_buffers_changed
  1452.               || !EQ (current_buffer->selective_display, Qnil)
  1453.               || detect_input_pending ()
  1454.               || !NILP (XWINDOW (selected_window)->column_number_displayed)
  1455.               || !NILP (Vexecuting_macro));
  1456.           value = internal_self_insert (c, 0);
  1457.           if (value)
  1458.             lose = 1;
  1459.           if (value == 2)
  1460.             nonundocount = 0;
  1461.  
  1462.           if (!lose
  1463.               && (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n'))
  1464.             {
  1465.               struct Lisp_Char_Table *dp
  1466.             = window_display_table (XWINDOW (selected_window));
  1467.               int lose = c;
  1468.  
  1469.               /* Add the offset to the character, for Finsert_char.
  1470.              We pass internal_self_insert the unmodified character
  1471.              because it itself does this offsetting.  */
  1472.               if (! NILP (current_buffer->enable_multibyte_characters))
  1473.             lose = unibyte_char_to_multibyte (lose);
  1474.  
  1475.               if (dp)
  1476.             {
  1477.               Lisp_Object obj;
  1478.  
  1479.               obj = DISP_CHAR_VECTOR (dp, lose);
  1480.               if (NILP (obj))
  1481.                 {
  1482.                   /* Do it only for char codes
  1483.                  that by default display as themselves.  */
  1484.                   if (lose >= 0x20 && lose <= 0x7e)
  1485.                 no_redisplay = direct_output_for_insert (lose);
  1486.                 }
  1487.               else if (VECTORP (obj)
  1488.                    && XVECTOR (obj)->size == 1
  1489.                    && (obj = XVECTOR (obj)->contents[0],
  1490.                        INTEGERP (obj))
  1491.                    /* Insist face not specified in glyph.  */
  1492.                    && (XINT (obj) & ((-1) << 8)) == 0)
  1493.                 no_redisplay
  1494.                   = direct_output_for_insert (XINT (obj));
  1495.             }
  1496.               else
  1497.             {
  1498.               if (lose >= 0x20 && lose <= 0x7e)
  1499.                 no_redisplay = direct_output_for_insert (lose);
  1500.             }
  1501.             }
  1502.           goto directly_done;
  1503.         }
  1504.         }
  1505.  
  1506.       /* Here for a command that isn't executed directly */
  1507.  
  1508.       nonundocount = 0;
  1509.       if (NILP (current_kboard->Vprefix_arg))
  1510.         Fundo_boundary ();
  1511.       Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
  1512.     }
  1513.     directly_done: ;
  1514.       current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
  1515.  
  1516.       /* Note that the value cell will never directly contain nil
  1517.      if the symbol is a local variable.  */
  1518.       if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
  1519.     safe_run_hooks (Qpost_command_hook);
  1520.  
  1521.       if (!NILP (Vdeferred_action_list))
  1522.     safe_run_hooks (Qdeferred_action_function);
  1523.  
  1524.       if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
  1525.     {
  1526.       if (NILP (Vunread_command_events)
  1527.           && NILP (Vunread_input_method_events)
  1528.           && NILP (Vunread_post_input_method_events)
  1529.           && NILP (Vexecuting_macro)
  1530.           && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
  1531.         safe_run_hooks (Qpost_command_idle_hook);
  1532.     }
  1533.  
  1534.       /* If there is a prefix argument,
  1535.      1) We don't want Vlast_command to be ``universal-argument''
  1536.      (that would be dumb), so don't set Vlast_command,
  1537.      2) we want to leave echoing on so that the prefix will be
  1538.      echoed as part of this key sequence, so don't call
  1539.      cancel_echoing, and
  1540.      3) we want to leave this_command_key_count non-zero, so that
  1541.      read_char will realize that it is re-reading a character, and
  1542.      not echo it a second time.
  1543.  
  1544.      If the command didn't actually create a prefix arg,
  1545.      but is merely a frame event that is transparent to prefix args,
  1546.      then the above doesn't apply.  */
  1547.       if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
  1548.     {
  1549.       current_kboard->Vlast_command = Vthis_command;
  1550.       current_kboard->Vreal_last_command = real_this_command;
  1551.       cancel_echoing ();
  1552.       this_command_key_count = 0;
  1553.       this_single_command_key_start = 0;
  1554.     }
  1555.  
  1556.       if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
  1557.     {
  1558.       if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
  1559.         {
  1560.           current_buffer->mark_active = Qnil;
  1561.           call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
  1562.         }
  1563.       else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
  1564.         call1 (Vrun_hooks, intern ("activate-mark-hook"));
  1565.     }
  1566.  
  1567.     finalize:
  1568.       /* Install chars successfully executed in kbd macro.  */
  1569.  
  1570.       if (!NILP (current_kboard->defining_kbd_macro)
  1571.       && NILP (current_kboard->Vprefix_arg))
  1572.     finalize_kbd_macro_chars ();
  1573.  
  1574. #ifdef MULTI_KBOARD
  1575.       if (!was_locked)
  1576.     any_kboard_state ();
  1577. #endif
  1578.     }
  1579. }
  1580.  
  1581. /* Subroutine for safe_run_hooks: run the hook HOOK.  */
  1582.  
  1583. static Lisp_Object
  1584. safe_run_hooks_1 (hook)
  1585.      Lisp_Object hook;
  1586. {
  1587.   return call1 (Vrun_hooks, Vinhibit_quit);
  1588. }
  1589.  
  1590. /* Subroutine for safe_run_hooks: handle an error by clearing out the hook.  */
  1591.  
  1592. static Lisp_Object
  1593. safe_run_hooks_error (data)
  1594.      Lisp_Object data;
  1595. {
  1596.   Fset (Vinhibit_quit, Qnil);
  1597. }
  1598.  
  1599. /* If we get an error while running the hook, cause the hook variable
  1600.    to be nil.  Also inhibit quits, so that C-g won't cause the hook
  1601.    to mysteriously evaporate.  */
  1602.  
  1603. void
  1604. safe_run_hooks (hook)
  1605.      Lisp_Object hook;
  1606. {
  1607.   Lisp_Object value;
  1608.   int count = specpdl_ptr - specpdl;
  1609.   specbind (Qinhibit_quit, hook);
  1610.  
  1611.   internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
  1612.  
  1613.   unbind_to (count, Qnil);
  1614. }
  1615.  
  1616. /* Number of seconds between polling for input.  */
  1617. int polling_period;
  1618.  
  1619. /* Nonzero means polling for input is temporarily suppressed.  */
  1620. int poll_suppress_count;
  1621.  
  1622. /* Nonzero if polling_for_input is actually being used.  */
  1623. int polling_for_input;
  1624.  
  1625. #ifdef POLL_FOR_INPUT
  1626.  
  1627. /* Handle an alarm once each second and read pending input
  1628.    so as to handle a C-g if it comces in.  */
  1629.  
  1630. SIGTYPE
  1631. input_poll_signal (signalnum)    /* If we don't have an argument, */
  1632.      int signalnum;        /* some compilers complain in signal calls. */
  1633. {
  1634.   /* This causes the call to start_polling at the end
  1635.      to do its job.  It also arranges for a quit or error
  1636.      from within read_avail_input to resume polling.  */
  1637.   poll_suppress_count++;
  1638.   if (interrupt_input_blocked == 0
  1639.       && !waiting_for_input)
  1640.     read_avail_input (0);
  1641.   /* Turn on the SIGALRM handler and request another alarm.  */
  1642.   start_polling ();
  1643. }
  1644.  
  1645. #endif
  1646.  
  1647. /* Begin signals to poll for input, if they are appropriate.
  1648.    This function is called unconditionally from various places.  */
  1649.  
  1650. void
  1651. start_polling ()
  1652. {
  1653. #ifdef POLL_FOR_INPUT
  1654.   if (read_socket_hook && !interrupt_input)
  1655.     {
  1656.       poll_suppress_count--;
  1657.       if (poll_suppress_count == 0)
  1658.     {
  1659.       signal (SIGALRM, input_poll_signal);
  1660.       polling_for_input = 1;
  1661.       alarm (polling_period);
  1662.     }
  1663.     }
  1664. #endif
  1665. }
  1666.  
  1667. /* Nonzero if we are using polling to handle input asynchronously.  */
  1668.  
  1669. int
  1670. input_polling_used ()
  1671. {
  1672. #ifdef POLL_FOR_INPUT
  1673.   return read_socket_hook && !interrupt_input;
  1674. #else
  1675.   return 0;
  1676. #endif
  1677. }
  1678.  
  1679. /* Turn off polling.  */
  1680.  
  1681. void
  1682. stop_polling ()
  1683. {
  1684. #ifdef POLL_FOR_INPUT
  1685.   if (read_socket_hook && !interrupt_input)
  1686.     {
  1687.       if (poll_suppress_count == 0)
  1688.     {
  1689.       polling_for_input = 0;
  1690.       alarm (0);
  1691.     }
  1692.       poll_suppress_count++;
  1693.     }
  1694. #endif
  1695. }
  1696.  
  1697. /* Set the value of poll_suppress_count to COUNT
  1698.    and start or stop polling accordingly.  */
  1699.  
  1700. void
  1701. set_poll_suppress_count (count)
  1702.      int count;
  1703. {
  1704. #ifdef POLL_FOR_INPUT
  1705.   if (count == 0 && poll_suppress_count != 0)
  1706.     {
  1707.       poll_suppress_count = 1;
  1708.       start_polling ();
  1709.     }
  1710.   else if (count != 0 && poll_suppress_count == 0)
  1711.     {
  1712.       stop_polling ();
  1713.     }
  1714.   poll_suppress_count = count;
  1715. #endif
  1716. }
  1717.  
  1718. /* Bind polling_period to a value at least N.
  1719.    But don't decrease it.  */
  1720.  
  1721. void
  1722. bind_polling_period (n)
  1723.      int n;
  1724. {
  1725. #ifdef POLL_FOR_INPUT
  1726.   int new = polling_period;
  1727.  
  1728.   if (n > new)
  1729.     new = n;
  1730.  
  1731.   stop_polling ();
  1732.   specbind (Qpolling_period, make_number (new));
  1733.   /* Start a new alarm with the new period.  */
  1734.   start_polling ();
  1735. #endif
  1736. }
  1737.  
  1738. /* Apply the control modifier to CHARACTER.  */
  1739.  
  1740. int
  1741. make_ctrl_char (c)
  1742.      int c;
  1743. {
  1744.   /* Save the upper bits here.  */
  1745.   int upper = c & ~0177;
  1746.  
  1747.   c &= 0177;
  1748.  
  1749.   /* Everything in the columns containing the upper-case letters
  1750.      denotes a control character.  */
  1751.   if (c >= 0100 && c < 0140)
  1752.     {
  1753.       int oc = c;
  1754.       c &= ~0140;
  1755.       /* Set the shift modifier for a control char
  1756.      made from a shifted letter.  But only for letters!  */
  1757.       if (oc >= 'A' && oc <= 'Z')
  1758.     c |= shift_modifier;
  1759.     }
  1760.  
  1761.   /* The lower-case letters denote control characters too.  */
  1762.   else if (c >= 'a' && c <= 'z')
  1763.     c &= ~0140;
  1764.  
  1765.   /* Include the bits for control and shift
  1766.      only if the basic ASCII code can't indicate them.  */
  1767.   else if (c >= ' ')
  1768.     c |= ctrl_modifier;
  1769.  
  1770.   /* Replace the high bits.  */
  1771.   c |= (upper & ~ctrl_modifier);
  1772.  
  1773.   return c;
  1774. }
  1775.  
  1776.  
  1777.  
  1778. /* Input of single characters from keyboard */
  1779.  
  1780. Lisp_Object print_help ();
  1781. static Lisp_Object kbd_buffer_get_event ();
  1782. static void record_char ();
  1783.  
  1784. #ifdef MULTI_KBOARD
  1785. static jmp_buf wrong_kboard_jmpbuf;
  1786. #endif
  1787.  
  1788. /* read a character from the keyboard; call the redisplay if needed */
  1789. /* commandflag 0 means do not do auto-saving, but do do redisplay.
  1790.    -1 means do not do redisplay, but do do autosaving.
  1791.    1 means do both.  */
  1792.  
  1793. /* The arguments MAPS and NMAPS are for menu prompting.
  1794.    MAPS is an array of keymaps;  NMAPS is the length of MAPS.
  1795.  
  1796.    PREV_EVENT is the previous input event, or nil if we are reading
  1797.    the first event of a key sequence (or not reading a key sequence).
  1798.    If PREV_EVENT is t, that is a "magic" value that says
  1799.    not to run input methods, but in other respects to act as if
  1800.    not reading a key sequence.
  1801.  
  1802.    If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
  1803.    if we used a mouse menu to read the input, or zero otherwise.  If
  1804.    USED_MOUSE_MENU is null, we don't dereference it.
  1805.  
  1806.    Value is t if we showed a menu and the user rejected it.  */
  1807.  
  1808. Lisp_Object
  1809. read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
  1810.      int commandflag;
  1811.      int nmaps;
  1812.      Lisp_Object *maps;
  1813.      Lisp_Object prev_event;
  1814.      int *used_mouse_menu;
  1815. {
  1816.   Lisp_Object c;
  1817.   int count;
  1818.   jmp_buf local_getcjmp;
  1819.   jmp_buf save_jump;
  1820.   int key_already_recorded = 0;
  1821.   Lisp_Object tem, save;
  1822.   Lisp_Object echo_area_message;
  1823.   Lisp_Object also_record;
  1824.   int reread;
  1825.   struct gcpro gcpro1, gcpro2;
  1826.  
  1827.   also_record = Qnil;
  1828.  
  1829.   before_command_key_count = this_command_key_count;
  1830.   before_command_echo_length = echo_length ();
  1831.   c = Qnil;
  1832.   echo_area_message = Qnil;
  1833.  
  1834.   GCPRO2 (c, echo_area_message);
  1835.  
  1836.  retry:
  1837.  
  1838.   reread = 0;
  1839.   if (CONSP (Vunread_post_input_method_events))
  1840.     {
  1841.       c = XCONS (Vunread_post_input_method_events)->car;
  1842.       Vunread_post_input_method_events
  1843.     = XCONS (Vunread_post_input_method_events)->cdr;
  1844.  
  1845.       /* Undo what read_char_x_menu_prompt did when it unread
  1846.      additional keys returned by Fx_popup_menu.  */
  1847.       if (CONSP (c)
  1848.       && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
  1849.       && NILP (XCONS (c)->cdr))
  1850.     c = XCONS (c)->car;
  1851.  
  1852.       reread = 1;
  1853.       goto reread_first;
  1854.     }
  1855.  
  1856.   if (unread_command_char != -1)
  1857.     {
  1858.       XSETINT (c, unread_command_char);
  1859.       unread_command_char = -1;
  1860.  
  1861.       reread = 1;
  1862.       goto reread_first;
  1863.     }
  1864.  
  1865.   if (CONSP (Vunread_command_events))
  1866.     {
  1867.       c = XCONS (Vunread_command_events)->car;
  1868.       Vunread_command_events = XCONS (Vunread_command_events)->cdr;
  1869.  
  1870.       /* Undo what read_char_x_menu_prompt did when it unread
  1871.      additional keys returned by Fx_popup_menu.  */
  1872.       if (CONSP (c)
  1873.       && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
  1874.       && NILP (XCONS (c)->cdr))
  1875.     c = XCONS (c)->car;
  1876.  
  1877.       reread = 1;
  1878.       goto reread_for_input_method;
  1879.     }
  1880.  
  1881.   if (CONSP (Vunread_input_method_events))
  1882.     {
  1883.       c = XCONS (Vunread_input_method_events)->car;
  1884.       Vunread_input_method_events = XCONS (Vunread_input_method_events)->cdr;
  1885.  
  1886.       /* Undo what read_char_x_menu_prompt did when it unread
  1887.      additional keys returned by Fx_popup_menu.  */
  1888.       if (CONSP (c)
  1889.       && (SYMBOLP (XCONS (c)->car) || INTEGERP (XCONS (c)->car))
  1890.       && NILP (XCONS (c)->cdr))
  1891.     c = XCONS (c)->car;
  1892.       reread = 1;
  1893.       goto reread_for_input_method;
  1894.     }
  1895.  
  1896.   /* If there is no function key translated before
  1897.      reset-this-command-lengths takes effect, forget about it.  */
  1898.   before_command_restore_flag = 0;
  1899.  
  1900.   if (!NILP (Vexecuting_macro))
  1901.     {
  1902.       /* We set this to Qmacro; since that's not a frame, nobody will
  1903.      try to switch frames on us, and the selected window will
  1904.      remain unchanged.
  1905.  
  1906.          Since this event came from a macro, it would be misleading to
  1907.      leave internal_last_event_frame set to wherever the last
  1908.      real event came from.  Normally, a switch-frame event selects
  1909.      internal_last_event_frame after each command is read, but
  1910.      events read from a macro should never cause a new frame to be
  1911.      selected. */
  1912.       Vlast_event_frame = internal_last_event_frame = Qmacro;
  1913.  
  1914.       /* Exit the macro if we are at the end.
  1915.      Also, some things replace the macro with t
  1916.      to force an early exit.  */
  1917.       if (EQ (Vexecuting_macro, Qt)
  1918.       || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
  1919.     {
  1920.       XSETINT (c, -1);
  1921.       RETURN_UNGCPRO (c);
  1922.     }
  1923.  
  1924.       c = Faref (Vexecuting_macro, make_number (executing_macro_index));
  1925.       if (STRINGP (Vexecuting_macro)
  1926.       && (XINT (c) & 0x80))
  1927.     XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
  1928.  
  1929.       executing_macro_index++;
  1930.  
  1931.       goto from_macro;
  1932.     }
  1933.  
  1934.   if (!NILP (unread_switch_frame))
  1935.     {
  1936.       c = unread_switch_frame;
  1937.       unread_switch_frame = Qnil;
  1938.  
  1939.       /* This event should make it into this_command_keys, and get echoed
  1940.      again, so we do not set `reread'.  */
  1941.       goto reread_first;
  1942.     }
  1943.  
  1944.   /* if redisplay was requested */
  1945.   if (commandflag >= 0)
  1946.     {
  1947.     /* If there is pending input, process any events which are not
  1948.        user-visible, such as X selection_request events.  */
  1949.       if (input_pending
  1950.       || detect_input_pending_run_timers (0))
  1951.     swallow_events (0);        /* may clear input_pending */
  1952.  
  1953.       /* Redisplay if no pending input.  */
  1954.       while (!input_pending)
  1955.     {
  1956.       redisplay ();
  1957.  
  1958.       if (!input_pending)
  1959.         /* Normal case: no input arrived during redisplay.  */
  1960.         break;
  1961.  
  1962.       /* Input arrived and pre-empted redisplay.
  1963.          Process any events which are not user-visible.  */
  1964.       swallow_events (0);
  1965.       /* If that cleared input_pending, try again to redisplay.  */
  1966.     }
  1967.     }
  1968.  
  1969.   /* Message turns off echoing unless more keystrokes turn it on again. */
  1970.   if (echo_area_glyphs && *echo_area_glyphs
  1971.       && echo_area_glyphs != current_kboard->echobuf
  1972.       && ok_to_echo_at_next_pause != echo_area_glyphs)
  1973.     cancel_echoing ();
  1974.   else
  1975.     /* If already echoing, continue.  */
  1976.     echo_dash ();
  1977.  
  1978.   /* Try reading a character via menu prompting in the minibuf.
  1979.      Try this before the sit-for, because the sit-for
  1980.      would do the wrong thing if we are supposed to do
  1981.      menu prompting. If EVENT_HAS_PARAMETERS then we are reading
  1982.      after a mouse event so don't try a minibuf menu. */
  1983.   c = Qnil;
  1984.   if (nmaps > 0 && INTERACTIVE
  1985.       && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
  1986.       /* Don't bring up a menu if we already have another event.  */
  1987.       && NILP (Vunread_command_events)
  1988.       && unread_command_char < 0
  1989.       && !detect_input_pending_run_timers (0))
  1990.     {
  1991.       c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
  1992.       if (! NILP (c))
  1993.     {
  1994.       key_already_recorded = 1;
  1995.       goto non_reread_1;
  1996.     }
  1997.     }
  1998.  
  1999.   /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
  2000.      We will do that below, temporarily for short sections of code,
  2001.      when appropriate.  local_getcjmp must be in effect
  2002.      around any call to sit_for or kbd_buffer_get_event;
  2003.      it *must not* be in effect when we call redisplay.  */
  2004.  
  2005.   if (_setjmp (local_getcjmp))
  2006.     {
  2007.       XSETINT (c, quit_char);
  2008.       XSETFRAME (internal_last_event_frame, selected_frame);
  2009.       Vlast_event_frame = internal_last_event_frame;
  2010.       /* If we report the quit char as an event,
  2011.      don't do so more than once.  */
  2012.       if (!NILP (Vinhibit_quit))
  2013.     Vquit_flag = Qnil;
  2014.  
  2015. #ifdef MULTI_KBOARD
  2016.       {
  2017.     KBOARD *kb = FRAME_KBOARD (selected_frame);
  2018.     if (kb != current_kboard)
  2019.       {
  2020.         Lisp_Object *tailp = &kb->kbd_queue;
  2021.         /* We shouldn't get here if we were in single-kboard mode!  */
  2022.         if (single_kboard)
  2023.           abort ();
  2024.         while (CONSP (*tailp))
  2025.           tailp = &XCONS (*tailp)->cdr;
  2026.         if (!NILP (*tailp))
  2027.           abort ();
  2028.         *tailp = Fcons (c, Qnil);
  2029.         kb->kbd_queue_has_data = 1;
  2030.         current_kboard = kb;
  2031.         /* This is going to exit from read_char
  2032.            so we had better get rid of this frame's stuff.  */
  2033.         UNGCPRO;
  2034.         longjmp (wrong_kboard_jmpbuf, 1);
  2035.       }
  2036.       }
  2037. #endif
  2038.       goto non_reread;
  2039.     }
  2040.  
  2041.   timer_start_idle ();
  2042.  
  2043.   /* If in middle of key sequence and minibuffer not active,
  2044.      start echoing if enough time elapses.  */
  2045.  
  2046.   if (minibuf_level == 0 && !current_kboard->immediate_echo
  2047.       && this_command_key_count > 0
  2048.       && ! noninteractive
  2049.       && echo_keystrokes > 0
  2050.       && (echo_area_glyphs == 0 || *echo_area_glyphs == 0
  2051.       || ok_to_echo_at_next_pause == echo_area_glyphs))
  2052.     {
  2053.       Lisp_Object tem0;
  2054.  
  2055.       /* After a mouse event, start echoing right away.
  2056.      This is because we are probably about to display a menu,
  2057.      and we don't want to delay before doing so.  */
  2058.       if (EVENT_HAS_PARAMETERS (prev_event))
  2059.     echo_now ();
  2060.       else
  2061.     {
  2062.       save_getcjmp (save_jump);
  2063.       restore_getcjmp (local_getcjmp);
  2064.       tem0 = sit_for (echo_keystrokes, 0, 1, 1, 0);
  2065.       restore_getcjmp (save_jump);
  2066.       if (EQ (tem0, Qt)
  2067.           && ! CONSP (Vunread_command_events))
  2068.         echo_now ();
  2069.     }
  2070.     }
  2071.  
  2072.   /* Maybe auto save due to number of keystrokes.  */
  2073.  
  2074.   if (commandflag != 0
  2075.       && auto_save_interval > 0
  2076.       && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
  2077.       && !detect_input_pending_run_timers (0))
  2078.     {
  2079.       Fdo_auto_save (Qnil, Qnil);
  2080.       /* Hooks can actually change some buffers in auto save.  */
  2081.       redisplay ();
  2082.     }
  2083.  
  2084.   /* Try reading using an X menu.
  2085.      This is never confused with reading using the minibuf
  2086.      because the recursive call of read_char in read_char_minibuf_menu_prompt
  2087.      does not pass on any keymaps.  */
  2088.  
  2089.   if (nmaps > 0 && INTERACTIVE
  2090.       && !NILP (prev_event)
  2091.       && EVENT_HAS_PARAMETERS (prev_event)
  2092.       && !EQ (XCONS (prev_event)->car, Qmenu_bar)
  2093.       /* Don't bring up a menu if we already have another event.  */
  2094.       && NILP (Vunread_command_events)
  2095.       && unread_command_char < 0)
  2096.     {
  2097.       c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
  2098.  
  2099.       /* Now that we have read an event, Emacs is not idle.  */
  2100.       timer_stop_idle ();
  2101.  
  2102.       RETURN_UNGCPRO (c);
  2103.     }
  2104.  
  2105.   /* Maybe autosave and/or garbage collect due to idleness.  */
  2106.  
  2107.   if (INTERACTIVE && NILP (c))
  2108.     {
  2109.       int delay_level, buffer_size;
  2110.  
  2111.       /* Slow down auto saves logarithmically in size of current buffer,
  2112.      and garbage collect while we're at it.  */
  2113.       if (! MINI_WINDOW_P (XWINDOW (selected_window)))
  2114.     last_non_minibuf_size = Z - BEG;
  2115.       buffer_size = (last_non_minibuf_size >> 8) + 1;
  2116.       delay_level = 0;
  2117.       while (buffer_size > 64)
  2118.     delay_level++, buffer_size -= buffer_size >> 2;
  2119.       if (delay_level < 4) delay_level = 4;
  2120.       /* delay_level is 4 for files under around 50k, 7 at 100k,
  2121.      9 at 200k, 11 at 300k, and 12 at 500k.  It is 15 at 1 meg.  */
  2122.  
  2123.       /* Auto save if enough time goes by without input.  */
  2124.       if (commandflag != 0
  2125.       && num_nonmacro_input_events > last_auto_save
  2126.       && INTEGERP (Vauto_save_timeout)
  2127.       && XINT (Vauto_save_timeout) > 0)
  2128.     {
  2129.       Lisp_Object tem0;
  2130.  
  2131.       save_getcjmp (save_jump);
  2132.       restore_getcjmp (local_getcjmp);
  2133.       tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
  2134.               0, 1, 1, 0);
  2135.       restore_getcjmp (save_jump);
  2136.  
  2137.       if (EQ (tem0, Qt)
  2138.           && ! CONSP (Vunread_command_events))
  2139.         {
  2140.           Fdo_auto_save (Qnil, Qnil);
  2141.  
  2142.           /* If we have auto-saved and there is still no input
  2143.          available, garbage collect if there has been enough
  2144.          consing going on to make it worthwhile.  */
  2145.           if (!detect_input_pending_run_timers (0)
  2146.           && consing_since_gc > gc_cons_threshold / 2)
  2147.         Fgarbage_collect ();
  2148.  
  2149.           redisplay ();
  2150.         }
  2151.     }
  2152.     }
  2153.  
  2154.   /* If this has become non-nil here, it has been set by a timer
  2155.      or sentinel or filter.  */
  2156.   if (CONSP (Vunread_command_events))
  2157.     {
  2158.       c = XCONS (Vunread_command_events)->car;
  2159.       Vunread_command_events = XCONS (Vunread_command_events)->cdr;
  2160.     }
  2161.  
  2162.   /* Read something from current KBOARD's side queue, if possible.  */
  2163.  
  2164.   if (NILP (c))
  2165.     {
  2166.       if (current_kboard->kbd_queue_has_data)
  2167.     {
  2168.       if (!CONSP (current_kboard->kbd_queue))
  2169.         abort ();
  2170.       c = XCONS (current_kboard->kbd_queue)->car;
  2171.       current_kboard->kbd_queue
  2172.         = XCONS (current_kboard->kbd_queue)->cdr;
  2173.       if (NILP (current_kboard->kbd_queue))
  2174.         current_kboard->kbd_queue_has_data = 0;
  2175.       input_pending = readable_events (0);
  2176.       if (EVENT_HAS_PARAMETERS (c)
  2177.           && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
  2178.         internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
  2179.       Vlast_event_frame = internal_last_event_frame;
  2180.     }
  2181.     }
  2182.  
  2183. #ifdef MULTI_KBOARD
  2184.   /* If current_kboard's side queue is empty check the other kboards.
  2185.      If one of them has data that we have not yet seen here,
  2186.      switch to it and process the data waiting for it.
  2187.  
  2188.      Note: if the events queued up for another kboard
  2189.      have already been seen here, and therefore are not a complete command,
  2190.      the kbd_queue_has_data field is 0, so we skip that kboard here.
  2191.      That's to avoid an infinite loop switching between kboards here.  */
  2192.   if (NILP (c) && !single_kboard)
  2193.     {
  2194.       KBOARD *kb;
  2195.       for (kb = all_kboards; kb; kb = kb->next_kboard)
  2196.     if (kb->kbd_queue_has_data)
  2197.       {
  2198.         current_kboard = kb;
  2199.         /* This is going to exit from read_char
  2200.            so we had better get rid of this frame's stuff.  */
  2201.         UNGCPRO;
  2202.         longjmp (wrong_kboard_jmpbuf, 1);
  2203.       }
  2204.     }
  2205. #endif
  2206.  
  2207.  wrong_kboard:
  2208.  
  2209.   stop_polling ();
  2210.  
  2211.   /* Finally, we read from the main queue,
  2212.      and if that gives us something we can't use yet, we put it on the
  2213.      appropriate side queue and try again.  */
  2214.  
  2215.   if (NILP (c))
  2216.     {
  2217.       KBOARD *kb;
  2218.  
  2219.       /* Actually read a character, waiting if necessary.  */
  2220.       save_getcjmp (save_jump);
  2221.       restore_getcjmp (local_getcjmp);
  2222.       c = kbd_buffer_get_event (&kb, used_mouse_menu);
  2223.       restore_getcjmp (save_jump);
  2224.  
  2225. #ifdef MULTI_KBOARD
  2226.       if (! NILP (c) && (kb != current_kboard))
  2227.     {
  2228.       Lisp_Object *tailp = &kb->kbd_queue;
  2229.       while (CONSP (*tailp))
  2230.         tailp = &XCONS (*tailp)->cdr;
  2231.       if (!NILP (*tailp))
  2232.         abort ();
  2233.       *tailp = Fcons (c, Qnil);
  2234.       kb->kbd_queue_has_data = 1;
  2235.       c = Qnil;
  2236.       if (single_kboard)
  2237.         goto wrong_kboard;
  2238.       current_kboard = kb;
  2239.       /* This is going to exit from read_char
  2240.          so we had better get rid of this frame's stuff.  */
  2241.       UNGCPRO;
  2242.       longjmp (wrong_kboard_jmpbuf, 1);
  2243.     }
  2244. #endif
  2245.     }
  2246.  
  2247.   /* Terminate Emacs in batch mode if at eof.  */
  2248.   if (noninteractive && INTEGERP (c) && XINT (c) < 0)
  2249.     Fkill_emacs (make_number (1));
  2250.  
  2251.   if (INTEGERP (c))
  2252.     {
  2253.       /* Add in any extra modifiers, where appropriate.  */
  2254.       if ((extra_keyboard_modifiers & CHAR_CTL)
  2255.       || ((extra_keyboard_modifiers & 0177) < ' '
  2256.           && (extra_keyboard_modifiers & 0177) != 0))
  2257.     XSETINT (c, make_ctrl_char (XINT (c)));
  2258.  
  2259.       /* Transfer any other modifier bits directly from
  2260.      extra_keyboard_modifiers to c.  Ignore the actual character code
  2261.      in the low 16 bits of extra_keyboard_modifiers.  */
  2262.       XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
  2263.     }
  2264.  
  2265.  non_reread:
  2266.  
  2267.   timer_stop_idle ();
  2268.  
  2269.   start_polling ();
  2270.  
  2271.   if (NILP (c))
  2272.     {
  2273.       if (commandflag >= 0
  2274.       && !input_pending && !detect_input_pending_run_timers (0))
  2275.     redisplay ();
  2276.  
  2277.       goto wrong_kboard;
  2278.     }
  2279.  
  2280.  non_reread_1:
  2281.  
  2282.   /* Buffer switch events are only for internal wakeups
  2283.      so don't show them to the user.
  2284.      Also, don't record a key if we already did.  */
  2285.   if (BUFFERP (c) || key_already_recorded)
  2286.     RETURN_UNGCPRO (c);
  2287.  
  2288.   /* Process special events within read_char
  2289.      and loop around to read another event.  */
  2290.   save = Vquit_flag;
  2291.   Vquit_flag = Qnil;
  2292.   tem = get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map, 0, 0),
  2293.                    c, 0, 0), 1);
  2294.   Vquit_flag = save;
  2295.  
  2296.   if (!NILP (tem))
  2297.     {
  2298.       int was_locked = single_kboard;
  2299.  
  2300.       last_input_char = c;
  2301.       Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
  2302.  
  2303.       /* Resume allowing input from any kboard, if that was true before.  */
  2304.       if (!was_locked)
  2305.     any_kboard_state ();
  2306.  
  2307.       goto retry;
  2308.     }
  2309.  
  2310.   /* Handle things that only apply to characters.  */
  2311.   if (INTEGERP (c))
  2312.     {
  2313.       /* If kbd_buffer_get_event gave us an EOF, return that.  */
  2314.       if (XINT (c) == -1)
  2315.     RETURN_UNGCPRO (c);
  2316.  
  2317.       if ((STRINGP (Vkeyboard_translate_table)
  2318.        && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
  2319.       || (VECTORP (Vkeyboard_translate_table)
  2320.           && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
  2321.       || (CHAR_TABLE_P (Vkeyboard_translate_table)
  2322.           && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
  2323.     {
  2324.       Lisp_Object d;
  2325.       d = Faref (Vkeyboard_translate_table, c);
  2326.       /* nil in keyboard-translate-table means no translation.  */
  2327.       if (!NILP (d))
  2328.         c = d;
  2329.     }
  2330.     }
  2331.  
  2332.   /* If this event is a mouse click in the menu bar,
  2333.      return just menu-bar for now.  Modify the mouse click event
  2334.      so we won't do this twice, then queue it up.  */
  2335.   if (EVENT_HAS_PARAMETERS (c)
  2336.       && CONSP (XCONS (c)->cdr)
  2337.       && CONSP (EVENT_START (c))
  2338.       && CONSP (XCONS (EVENT_START (c))->cdr))
  2339.     {
  2340.       Lisp_Object posn;
  2341.  
  2342.       posn = POSN_BUFFER_POSN (EVENT_START (c));
  2343.       /* Handle menu-bar events:
  2344.      insert the dummy prefix event `menu-bar'.  */
  2345.       if (EQ (posn, Qmenu_bar))
  2346.     {
  2347.       /* Change menu-bar to (menu-bar) as the event "position".  */
  2348.       POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
  2349.  
  2350.       also_record = c;
  2351.       Vunread_command_events = Fcons (c, Vunread_command_events);
  2352.       c = posn;
  2353.     }
  2354.     }
  2355.  
  2356.   /* Store these characters into recent_keys, the dribble file if any,
  2357.      and the keyboard macro being defined, if any.  */
  2358.   record_char (c);
  2359.   if (! NILP (also_record))
  2360.     record_char (also_record);
  2361.  
  2362.   /* Wipe the echo area.
  2363.      But first, if we are about to use an input method,
  2364.      save the echo area contents for it to refer to.  */
  2365.   if (INTEGERP (c)
  2366.       && ! NILP (Vinput_method_function)
  2367.       && (unsigned) XINT (c) >= ' '
  2368.       && (unsigned) XINT (c) < 127)
  2369.     Vinput_method_previous_message = echo_area_message = Fcurrent_message ();
  2370.  
  2371.   /* Now wipe the echo area.  */
  2372.   if (echo_area_glyphs)
  2373.     safe_run_hooks (Qecho_area_clear_hook);
  2374.   echo_area_glyphs = 0;
  2375.  
  2376.  reread_for_input_method:
  2377.  from_macro:
  2378.   /* Pass this to the input method, if appropriate.  */
  2379.   if (INTEGERP (c)
  2380.       && ! NILP (Vinput_method_function)
  2381.       /* Don't run the input method within a key sequence,
  2382.      after the first event of the key sequence.  */
  2383.       && NILP (prev_event)
  2384.       && (unsigned) XINT (c) >= ' '
  2385.       && (unsigned) XINT (c) < 127)
  2386.     {
  2387.       Lisp_Object keys; 
  2388.       int key_count;
  2389.       struct gcpro gcpro1;
  2390.       int count = specpdl_ptr - specpdl;
  2391.  
  2392.       /* Save the echo status.  */
  2393.       int saved_immediate_echo = current_kboard->immediate_echo;
  2394.       char *saved_ok_to_echo = ok_to_echo_at_next_pause;
  2395.       int saved_echo_after_prompt = current_kboard->echo_after_prompt;
  2396.  
  2397.       if (before_command_restore_flag)
  2398.     {
  2399.       this_command_key_count = before_command_key_count_1;
  2400.       if (this_command_key_count < this_single_command_key_start)
  2401.         this_single_command_key_start = this_command_key_count;
  2402.       echo_truncate (before_command_echo_length_1);
  2403.       before_command_restore_flag = 0;
  2404.     }
  2405.  
  2406.       /* Save the this_command_keys status.  */
  2407.       key_count = this_command_key_count;
  2408.  
  2409.       if (key_count > 0)
  2410.     keys = Fcopy_sequence (this_command_keys);
  2411.       else
  2412.     keys = Qnil;
  2413.       GCPRO1 (keys);
  2414.  
  2415.       /* Clear out this_command_keys.  */
  2416.       this_command_key_count = 0;
  2417.  
  2418.       /* Now wipe the echo area.  */
  2419.       if (echo_area_glyphs)
  2420.     safe_run_hooks (Qecho_area_clear_hook);
  2421.       echo_area_glyphs = 0;
  2422.       echo_truncate (0);
  2423.  
  2424.       /* If we are not reading a key sequence,
  2425.      never use the echo area.  */
  2426.       if (maps == 0)
  2427.     {
  2428.       specbind (Qinput_method_exit_on_first_char, Qt);
  2429.       specbind (Qinput_method_use_echo_area, Qt);
  2430.     }
  2431.  
  2432.       /* Call the input method.  */
  2433.       tem = call1 (Vinput_method_function, c);
  2434.  
  2435.       tem = unbind_to (count, tem);
  2436.  
  2437.       /* Restore the saved echoing state
  2438.      and this_command_keys state.  */
  2439.       this_command_key_count = key_count;
  2440.       if (key_count > 0)
  2441.     this_command_keys = keys;
  2442.  
  2443.       cancel_echoing ();
  2444.       ok_to_echo_at_next_pause = saved_ok_to_echo;
  2445.       current_kboard->echo_after_prompt = saved_echo_after_prompt;
  2446.       if (saved_immediate_echo)
  2447.     echo_now ();
  2448.  
  2449.       UNGCPRO;
  2450.  
  2451.       /* The input method can return no events.  */
  2452.       if (! CONSP (tem))
  2453.     {
  2454.       /* Bring back the previous message, if any.  */
  2455.       if (! NILP (echo_area_message))
  2456.         message_with_string ("%s", echo_area_message, 0);
  2457.       goto retry;
  2458.     }
  2459.       /* It returned one event or more.  */
  2460.       c = XCONS (tem)->car;
  2461.       Vunread_post_input_method_events
  2462.     = nconc2 (XCONS (tem)->cdr, Vunread_post_input_method_events);
  2463.     }
  2464.  
  2465.  reread_first:
  2466.  
  2467.   if (this_command_key_count == 0 || ! reread)
  2468.     {
  2469.       before_command_key_count = this_command_key_count;
  2470.       before_command_echo_length = echo_length ();
  2471.  
  2472.       /* Don't echo mouse motion events.  */
  2473.       if (echo_keystrokes
  2474.       && ! (EVENT_HAS_PARAMETERS (c)
  2475.         && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
  2476.     {
  2477.       echo_char (c);
  2478.       if (! NILP (also_record))
  2479.         echo_char (also_record);
  2480.       /* Once we reread a character, echoing can happen
  2481.          the next time we pause to read a new one.  */
  2482.       ok_to_echo_at_next_pause = echo_area_glyphs;
  2483.     }
  2484.  
  2485.       /* Record this character as part of the current key.  */
  2486.       add_command_key (c);
  2487.       if (! NILP (also_record))
  2488.     add_command_key (also_record);
  2489.     }
  2490.  
  2491.   last_input_char = c;
  2492.   num_input_events++;
  2493.  
  2494.   /* Process the help character specially if enabled */
  2495.   if (!NILP (Vhelp_form) && help_char_p (c))
  2496.     {
  2497.       Lisp_Object tem0;
  2498.       count = specpdl_ptr - specpdl;
  2499.  
  2500.       record_unwind_protect (Fset_window_configuration,
  2501.                  Fcurrent_window_configuration (Qnil));
  2502.  
  2503.       tem0 = Feval (Vhelp_form);
  2504.       if (STRINGP (tem0))
  2505.     internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
  2506.  
  2507.       cancel_echoing ();
  2508.       do
  2509.     c = read_char (0, 0, 0, Qnil, 0);
  2510.       while (BUFFERP (c));
  2511.       /* Remove the help from the frame */
  2512.       unbind_to (count, Qnil);
  2513.  
  2514.       redisplay ();
  2515.       if (EQ (c, make_number (040)))
  2516.     {
  2517.       cancel_echoing ();
  2518.       do
  2519.         c = read_char (0, 0, 0, Qnil, 0);
  2520.       while (BUFFERP (c));
  2521.     }
  2522.     }
  2523.  
  2524.   RETURN_UNGCPRO (c);
  2525. }
  2526.  
  2527. /* Record a key that came from a mouse menu.
  2528.    Record it for echoing, for this-command-keys, and so on.  */
  2529.  
  2530. static void
  2531. record_menu_key (c)
  2532.      Lisp_Object c;
  2533. {
  2534.   /* Wipe the echo area.  */
  2535.   echo_area_glyphs = 0;
  2536.  
  2537.   record_char (c);
  2538.  
  2539.   before_command_key_count = this_command_key_count;
  2540.   before_command_echo_length = echo_length ();
  2541.  
  2542.   /* Don't echo mouse motion events.  */
  2543.   if (echo_keystrokes)
  2544.     {
  2545.       echo_char (c);
  2546.  
  2547.       /* Once we reread a character, echoing can happen
  2548.      the next time we pause to read a new one.  */
  2549.       ok_to_echo_at_next_pause = 0;
  2550.     }
  2551.  
  2552.   /* Record this character as part of the current key.  */
  2553.   add_command_key (c);
  2554.  
  2555.   /* Re-reading in the middle of a command */
  2556.   last_input_char = c;
  2557.   num_input_events++;
  2558. }
  2559.  
  2560. /* Return 1 if should recognize C as "the help character".  */
  2561.  
  2562. int
  2563. help_char_p (c)
  2564.      Lisp_Object c;
  2565. {
  2566.   Lisp_Object tail;
  2567.  
  2568.   if (EQ (c, Vhelp_char))
  2569.     return 1;
  2570.   for (tail = Vhelp_event_list; CONSP (tail); tail = XCONS (tail)->cdr)
  2571.     if (EQ (c, XCONS (tail)->car))
  2572.       return 1;
  2573.   return 0;
  2574. }
  2575.  
  2576. /* Record the input event C in various ways.  */
  2577.  
  2578. static void
  2579. record_char (c)
  2580.      Lisp_Object c;
  2581. {
  2582.   total_keys++;
  2583.   XVECTOR (recent_keys)->contents[recent_keys_index] = c;
  2584.   if (++recent_keys_index >= NUM_RECENT_KEYS)
  2585.     recent_keys_index = 0;
  2586.  
  2587.   /* Write c to the dribble file.  If c is a lispy event, write
  2588.      the event's symbol to the dribble file, in <brackets>.  Bleaugh.
  2589.      If you, dear reader, have a better idea, you've got the source.  :-) */
  2590.   if (dribble)
  2591.     {
  2592.       if (INTEGERP (c))
  2593.     {
  2594.       if (XUINT (c) < 0x100)
  2595.         putc (XINT (c), dribble);
  2596.       else
  2597.         fprintf (dribble, " 0x%x", (int) XUINT (c));
  2598.     }
  2599.       else
  2600.     {
  2601.       Lisp_Object dribblee;
  2602.  
  2603.       /* If it's a structured event, take the event header.  */
  2604.       dribblee = EVENT_HEAD (c);
  2605.  
  2606.       if (SYMBOLP (dribblee))
  2607.         {
  2608.           putc ('<', dribble);
  2609.           fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
  2610.               STRING_BYTES (XSYMBOL (dribblee)->name),
  2611.               dribble);
  2612.           putc ('>', dribble);
  2613.         }
  2614.     }
  2615.  
  2616.       fflush (dribble);
  2617.     }
  2618.  
  2619.   store_kbd_macro_char (c);
  2620.  
  2621.   num_nonmacro_input_events++;
  2622. }
  2623.  
  2624. Lisp_Object
  2625. print_help (object)
  2626.      Lisp_Object object;
  2627. {
  2628.   struct buffer *old = current_buffer;
  2629.   Fprinc (object, Qnil);
  2630.   set_buffer_internal (XBUFFER (Vstandard_output));
  2631.   call0 (intern ("help-mode"));
  2632.   set_buffer_internal (old);
  2633.   return Qnil;
  2634. }
  2635.  
  2636. /* Copy out or in the info on where C-g should throw to.
  2637.    This is used when running Lisp code from within get_char,
  2638.    in case get_char is called recursively.
  2639.    See read_process_output.  */
  2640.  
  2641. static void
  2642. save_getcjmp (temp)
  2643.      jmp_buf temp;
  2644. {
  2645.   bcopy (getcjmp, temp, sizeof getcjmp);
  2646. }
  2647.  
  2648. static void
  2649. restore_getcjmp (temp)
  2650.      jmp_buf temp;
  2651. {
  2652.   bcopy (temp, getcjmp, sizeof getcjmp);
  2653. }
  2654.  
  2655. #ifdef HAVE_MOUSE
  2656.  
  2657. /* Restore mouse tracking enablement.  See Ftrack_mouse for the only use
  2658.    of this function.  */
  2659.  
  2660. static Lisp_Object
  2661. tracking_off (old_value)
  2662.      Lisp_Object old_value;
  2663. {
  2664.   do_mouse_tracking = old_value;
  2665.   if (NILP (old_value))
  2666.     {
  2667. #ifdef HAVE_PM
  2668.       pm_mouse_tracking (0);
  2669. #endif /* HAVE_PM */
  2670.       /* Redisplay may have been preempted because there was input
  2671.      available, and it assumes it will be called again after the
  2672.      input has been processed.  If the only input available was
  2673.      the sort that we have just disabled, then we need to call
  2674.      redisplay.  */
  2675.       if (!readable_events (1))
  2676.     {
  2677.       redisplay_preserve_echo_area ();
  2678.       get_input_pending (&input_pending, 1);
  2679.     }
  2680.     }
  2681. }
  2682.  
  2683. DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
  2684.   "Evaluate BODY with mouse movement events enabled.\n\
  2685. Within a `track-mouse' form, mouse motion generates input events that\n\
  2686. you can read with `read-event'.\n\
  2687. Normally, mouse motion is ignored.")
  2688.   (args)
  2689.      Lisp_Object args;
  2690. {
  2691.   int count = specpdl_ptr - specpdl;
  2692.   Lisp_Object val;
  2693.  
  2694.   record_unwind_protect (tracking_off, do_mouse_tracking);
  2695.  
  2696. #ifdef HAVE_PM
  2697.   if (NILP (do_mouse_tracking))
  2698.     {
  2699.       Lisp_Object tail, frame;
  2700.  
  2701.       FOR_EACH_FRAME (tail, frame)
  2702.         XFRAME (frame)->mouse_moved = 0;
  2703.     }
  2704. #endif /* HAVE_PM */
  2705.  
  2706.   do_mouse_tracking = Qt;
  2707.  
  2708. #ifdef HAVE_PM
  2709.   pm_mouse_tracking (1);
  2710. #endif /* HAVE_PM */
  2711.  
  2712.   val = Fprogn (args);
  2713.   return unbind_to (count, val);
  2714. }
  2715.  
  2716. /* If mouse has moved on some frame, return one of those frames.
  2717.    Return 0 otherwise.  */
  2718.  
  2719. static FRAME_PTR
  2720. some_mouse_moved ()
  2721. {
  2722.   Lisp_Object tail, frame;
  2723.  
  2724.   FOR_EACH_FRAME (tail, frame)
  2725.     {
  2726.       if (XFRAME (frame)->mouse_moved)
  2727.     return XFRAME (frame);
  2728.     }
  2729.  
  2730.   return 0;
  2731. }
  2732.  
  2733. #endif    /* HAVE_MOUSE */
  2734.  
  2735. /* Low level keyboard/mouse input.
  2736.    kbd_buffer_store_event places events in kbd_buffer, and
  2737.    kbd_buffer_get_event retrieves them.  */
  2738.  
  2739. /* Return true iff there are any events in the queue that read-char
  2740.    would return.  If this returns false, a read-char would block.  */
  2741. static int
  2742. readable_events (do_timers_now)
  2743.      int do_timers_now;
  2744. {
  2745.   if (do_timers_now)
  2746.     timer_check (do_timers_now);
  2747.  
  2748.   if (kbd_fetch_ptr != kbd_store_ptr)
  2749.     return 1;
  2750. #ifdef HAVE_MOUSE
  2751.   if (!NILP (do_mouse_tracking) && some_mouse_moved ())
  2752.     return 1;
  2753. #endif
  2754.   if (single_kboard)
  2755.     {
  2756.       if (current_kboard->kbd_queue_has_data)
  2757.     return 1;
  2758.     }
  2759.   else
  2760.     {
  2761.       KBOARD *kb;
  2762.       for (kb = all_kboards; kb; kb = kb->next_kboard)
  2763.     if (kb->kbd_queue_has_data)
  2764.       return 1;
  2765.     }
  2766.   return 0;
  2767. }
  2768.  
  2769. /* Set this for debugging, to have a way to get out */
  2770. int stop_character;
  2771.  
  2772. #ifdef MULTI_KBOARD
  2773. static KBOARD *
  2774. event_to_kboard (event)
  2775.      struct input_event *event;
  2776. {
  2777.   Lisp_Object frame;
  2778.   frame = event->frame_or_window;
  2779.   if (CONSP (frame))
  2780.     frame = XCONS (frame)->car;
  2781.   else if (WINDOWP (frame))
  2782.     frame = WINDOW_FRAME (XWINDOW (frame));
  2783.  
  2784.   /* There are still some events that don't set this field.
  2785.      For now, just ignore the problem.
  2786.      Also ignore dead frames here.  */
  2787.   if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
  2788.     return 0;
  2789.   else
  2790.     return FRAME_KBOARD (XFRAME (frame));
  2791. }
  2792. #endif
  2793.  
  2794. /* Store an event obtained at interrupt level into kbd_buffer, fifo */
  2795.  
  2796. void
  2797. kbd_buffer_store_event (event)
  2798.      register struct input_event *event;
  2799. {
  2800.   if (event->kind == no_event)
  2801.     abort ();
  2802.  
  2803.   if (event->kind == ascii_keystroke)
  2804.     {
  2805.       register int c = event->code & 0377;
  2806.  
  2807.       if (event->modifiers & ctrl_modifier)
  2808.     c = make_ctrl_char (c);
  2809.  
  2810.       c |= (event->modifiers
  2811.         & (meta_modifier | alt_modifier
  2812.            | hyper_modifier | super_modifier));
  2813.  
  2814.       if (c == quit_char)
  2815.     {
  2816.       extern SIGTYPE interrupt_signal ();
  2817. #ifdef MULTI_KBOARD
  2818.       KBOARD *kb;
  2819.       struct input_event *sp;
  2820.  
  2821.       if (single_kboard
  2822.           && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
  2823.           kb != current_kboard))
  2824.         {
  2825.           kb->kbd_queue
  2826.         = Fcons (make_lispy_switch_frame (event->frame_or_window),
  2827.              Fcons (make_number (c), Qnil));
  2828.           kb->kbd_queue_has_data = 1;
  2829.           for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
  2830.         {
  2831.           if (sp == kbd_buffer + KBD_BUFFER_SIZE)
  2832.             sp = kbd_buffer;
  2833.  
  2834.           if (event_to_kboard (sp) == kb)
  2835.             {
  2836.               sp->kind = no_event;
  2837.               sp->frame_or_window = Qnil;
  2838.             }
  2839.         }
  2840.           return;
  2841.         }
  2842. #endif
  2843.  
  2844.       /* If this results in a quit_char being returned to Emacs as
  2845.          input, set Vlast_event_frame properly.  If this doesn't
  2846.          get returned to Emacs as an event, the next event read
  2847.          will set Vlast_event_frame again, so this is safe to do.  */
  2848.       {
  2849.         Lisp_Object focus;
  2850.  
  2851.         focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
  2852.         if (NILP (focus))
  2853.           focus = event->frame_or_window;
  2854.         internal_last_event_frame = focus;
  2855.         Vlast_event_frame = focus;
  2856.       }
  2857.  
  2858.       last_event_timestamp = event->timestamp;
  2859.       interrupt_signal ();
  2860.       return;
  2861.     }
  2862.  
  2863.       if (c && c == stop_character)
  2864.     {
  2865.       sys_suspend ();
  2866.       return;
  2867.     }
  2868.     }
  2869.   /* Don't insert two buffer_switch_event's in a row.
  2870.      Just ignore the second one.  */
  2871.   else if (event->kind == buffer_switch_event
  2872.        && kbd_fetch_ptr != kbd_store_ptr
  2873.        && kbd_store_ptr->kind == buffer_switch_event)
  2874.     return;
  2875.  
  2876.   if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
  2877.     kbd_store_ptr = kbd_buffer;
  2878.  
  2879.   /* Don't let the very last slot in the buffer become full,
  2880.      since that would make the two pointers equal,
  2881.      and that is indistinguishable from an empty buffer.
  2882.      Discard the event if it would fill the last slot.  */
  2883.   if (kbd_fetch_ptr - 1 != kbd_store_ptr)
  2884.     {
  2885.       volatile struct input_event *sp = kbd_store_ptr;
  2886.       sp->kind = event->kind;
  2887.       if (event->kind == selection_request_event)
  2888.     {
  2889.       /* We must not use the ordinary copying code for this case,
  2890.          since `part' is an enum and copying it might not copy enough
  2891.          in this case.  */
  2892.       bcopy (event, (char *) sp, sizeof (*event));
  2893.     }
  2894.       else
  2895.     {
  2896.       sp->code = event->code;
  2897.       sp->part = event->part;
  2898.       sp->frame_or_window = event->frame_or_window;
  2899.       sp->modifiers = event->modifiers;
  2900.       sp->x = event->x;
  2901.       sp->y = event->y;
  2902.       sp->timestamp = event->timestamp;
  2903.     }
  2904.       (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
  2905.                               - kbd_buffer]
  2906.        = event->frame_or_window);
  2907.  
  2908.       kbd_store_ptr++;
  2909.     }
  2910. }
  2911.  
  2912. /* Discard any mouse events in the event buffer by setting them to
  2913.    no_event.  */
  2914. void
  2915. discard_mouse_events ()
  2916. {
  2917.   struct input_event *sp;
  2918.   for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
  2919.     {
  2920.       if (sp == kbd_buffer + KBD_BUFFER_SIZE)
  2921.     sp = kbd_buffer;
  2922.  
  2923.       if (sp->kind == mouse_click
  2924. #ifdef WINDOWSNT
  2925.       || sp->kind == w32_scroll_bar_click
  2926. #endif
  2927.       || sp->kind == scroll_bar_click)
  2928.     {
  2929.       sp->kind = no_event;
  2930.     }
  2931.     }
  2932. }
  2933.  
  2934. /* Read one event from the event buffer, waiting if necessary.
  2935.    The value is a Lisp object representing the event.
  2936.    The value is nil for an event that should be ignored,
  2937.    or that was handled here.
  2938.    We always read and discard one event.  */
  2939.  
  2940. static Lisp_Object
  2941. kbd_buffer_get_event (kbp, used_mouse_menu)
  2942.      KBOARD **kbp;
  2943.      int *used_mouse_menu;
  2944. {
  2945.   register int c;
  2946.   Lisp_Object obj;
  2947.   EMACS_TIME next_timer_delay;
  2948.  
  2949.   if (noninteractive)
  2950.     {
  2951.       c = getchar ();
  2952.       XSETINT (obj, c);
  2953.       *kbp = current_kboard;
  2954.       return obj;
  2955.     }
  2956.  
  2957.   /* Wait until there is input available.  */
  2958.   for (;;)
  2959.     {
  2960.       if (kbd_fetch_ptr != kbd_store_ptr)
  2961.     break;
  2962. #ifdef HAVE_MOUSE
  2963.       if (!NILP (do_mouse_tracking) && some_mouse_moved ())
  2964.     break;
  2965. #endif
  2966.  
  2967.       /* If the quit flag is set, then read_char will return
  2968.      quit_char, so that counts as "available input."  */
  2969.       if (!NILP (Vquit_flag))
  2970.     quit_throw_to_read_char ();
  2971.  
  2972.       /* One way or another, wait until input is available; then, if
  2973.      interrupt handlers have not read it, read it now.  */
  2974.  
  2975. #ifdef OLDVMS
  2976.       wait_for_kbd_input ();
  2977. #else
  2978. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  2979. #ifdef SIGIO
  2980.       gobble_input (0);
  2981. #endif /* SIGIO */
  2982.       if (kbd_fetch_ptr != kbd_store_ptr)
  2983.     break;
  2984. #ifdef HAVE_MOUSE
  2985.       if (!NILP (do_mouse_tracking) && some_mouse_moved ())
  2986.     break;
  2987. #endif
  2988.       {
  2989.     Lisp_Object minus_one;
  2990.  
  2991.     XSETINT (minus_one, -1);
  2992.     wait_reading_process_input (0, 0, minus_one, 1);
  2993.  
  2994.     if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
  2995.       /* Pass 1 for EXPECT since we just waited to have input.  */
  2996.       read_avail_input (1);
  2997.       }
  2998. #endif /* not VMS */
  2999.     }
  3000.  
  3001.   if (CONSP (Vunread_command_events))
  3002.     {
  3003.       Lisp_Object first;
  3004.       first = XCONS (Vunread_command_events)->car;
  3005.       Vunread_command_events = XCONS (Vunread_command_events)->cdr;
  3006.       *kbp = current_kboard;
  3007.       return first;
  3008.     }
  3009.  
  3010.   /* At this point, we know that there is a readable event available
  3011.      somewhere.  If the event queue is empty, then there must be a
  3012.      mouse movement enabled and available.  */
  3013.   if (kbd_fetch_ptr != kbd_store_ptr)
  3014.     {
  3015.       struct input_event *event;
  3016.  
  3017.       event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
  3018.            ? kbd_fetch_ptr
  3019.            : kbd_buffer);
  3020.  
  3021.       last_event_timestamp = event->timestamp;
  3022.  
  3023. #ifdef MULTI_KBOARD
  3024.       *kbp = event_to_kboard (event);
  3025.       if (*kbp == 0)
  3026.     *kbp = current_kboard;  /* Better than returning null ptr?  */
  3027. #else
  3028.       *kbp = &the_only_kboard;
  3029. #endif
  3030.  
  3031.       obj = Qnil;
  3032.  
  3033.       /* These two kinds of events get special handling
  3034.      and don't actually appear to the command loop.
  3035.      We return nil for them.  */
  3036.       if (event->kind == selection_request_event)
  3037.     {
  3038. #ifdef HAVE_X11
  3039.       struct input_event copy;
  3040.  
  3041.       /* Remove it from the buffer before processing it,
  3042.          since otherwise swallow_events will see it
  3043.          and process it again.  */
  3044.       copy = *event;
  3045.       kbd_fetch_ptr = event + 1;
  3046.       input_pending = readable_events (0);
  3047.       x_handle_selection_request (©);
  3048. #else
  3049.       /* We're getting selection request events, but we don't have
  3050.              a window system.  */
  3051.       abort ();
  3052. #endif
  3053.     }
  3054.  
  3055.       else if (event->kind == selection_clear_event)
  3056.     {
  3057. #ifdef HAVE_X11
  3058.       struct input_event copy;
  3059.  
  3060.       /* Remove it from the buffer before processing it.  */
  3061.       copy = *event;
  3062.       kbd_fetch_ptr = event + 1;
  3063.       input_pending = readable_events (0);
  3064.       x_handle_selection_clear (©);
  3065. #else
  3066.       /* We're getting selection request events, but we don't have
  3067.              a window system.  */
  3068.       abort ();
  3069. #endif
  3070.     }
  3071. #if defined (HAVE_X11) || defined (HAVE_NTGUI)
  3072.       else if (event->kind == delete_window_event)
  3073.     {
  3074.       /* Make an event (delete-frame (FRAME)).  */
  3075.       obj = Fcons (event->frame_or_window, Qnil);
  3076.       obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
  3077.       kbd_fetch_ptr = event + 1;
  3078.     }
  3079.       else if (event->kind == iconify_event)
  3080.     {
  3081.       /* Make an event (iconify-frame (FRAME)).  */
  3082.       obj = Fcons (event->frame_or_window, Qnil);
  3083.       obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
  3084.       kbd_fetch_ptr = event + 1;
  3085.     }
  3086.       else if (event->kind == deiconify_event)
  3087.     {
  3088.       /* Make an event (make-frame-visible (FRAME)).  */
  3089.       obj = Fcons (event->frame_or_window, Qnil);
  3090.       obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
  3091.       kbd_fetch_ptr = event + 1;
  3092.     }
  3093. #endif
  3094.       else if (event->kind == buffer_switch_event)
  3095.     {
  3096.       /* The value doesn't matter here; only the type is tested.  */
  3097.       XSETBUFFER (obj, current_buffer);
  3098.       kbd_fetch_ptr = event + 1;
  3099.     }
  3100. #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
  3101.       else if (event->kind == menu_bar_activate_event)
  3102.     {
  3103.       kbd_fetch_ptr = event + 1;
  3104.       input_pending = readable_events (0);
  3105.       if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
  3106.         x_activate_menubar (XFRAME (event->frame_or_window));
  3107.     }
  3108. #endif
  3109.       /* Just discard these, by returning nil.
  3110.      With MULTI_KBOARD, these events are used as placeholders
  3111.      when we need to randomly delete events from the queue.
  3112.      (They shouldn't otherwise be found in the buffer,
  3113.      but on some machines it appears they do show up
  3114.      even without MULTI_KBOARD.)  */
  3115.       /* On Windows NT/9X, no_event is used to delete extraneous
  3116.          mouse events during a popup-menu call.  */
  3117.       else if (event->kind == no_event)
  3118.     kbd_fetch_ptr = event + 1;
  3119.  
  3120.       /* If this event is on a different frame, return a switch-frame this
  3121.      time, and leave the event in the queue for next time.  */
  3122.       else
  3123.     {
  3124.       Lisp_Object frame;
  3125.       Lisp_Object focus;
  3126.  
  3127.       frame = event->frame_or_window;
  3128.       if (CONSP (frame))
  3129.         frame = XCONS (frame)->car;
  3130.       else if (WINDOWP (frame))
  3131.         frame = WINDOW_FRAME (XWINDOW (frame));
  3132.  
  3133.       focus = FRAME_FOCUS_FRAME (XFRAME (frame));
  3134.       if (! NILP (focus))
  3135.         frame = focus;
  3136.  
  3137.       if (! EQ (frame, internal_last_event_frame)
  3138.           && XFRAME (frame) != selected_frame)
  3139.         obj = make_lispy_switch_frame (frame);
  3140.       internal_last_event_frame = frame;
  3141.  
  3142.       /* If we didn't decide to make a switch-frame event, go ahead
  3143.          and build a real event from the queue entry.  */
  3144.  
  3145.       if (NILP (obj))
  3146.         {
  3147.           obj = make_lispy_event (event);
  3148. #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
  3149.           /* If this was a menu selection, then set the flag to inhibit
  3150.          writing to last_nonmenu_event.  Don't do this if the event
  3151.          we're returning is (menu-bar), though; that indicates the
  3152.          beginning of the menu sequence, and we might as well leave
  3153.          that as the `event with parameters' for this selection.  */
  3154.           if (event->kind == menu_bar_event
  3155.           && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
  3156.           && used_mouse_menu)
  3157.         *used_mouse_menu = 1;
  3158. #endif
  3159.  
  3160.           /* Wipe out this event, to catch bugs.  */
  3161.           event->kind = no_event;
  3162.           XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
  3163.  
  3164.           kbd_fetch_ptr = event + 1;
  3165.         }
  3166.     }
  3167.     }
  3168. #ifdef HAVE_MOUSE
  3169.   /* Try generating a mouse motion event.  */
  3170.   else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
  3171.     {
  3172.       FRAME_PTR f = some_mouse_moved ();
  3173.       Lisp_Object bar_window;
  3174.       enum scroll_bar_part part;
  3175.       Lisp_Object x, y;
  3176.       unsigned long time;
  3177.  
  3178.       *kbp = current_kboard;
  3179.       /* Note that this uses F to determine which display to look at.
  3180.      If there is no valid info, it does not store anything
  3181.      so x remains nil.  */
  3182.       x = Qnil;
  3183.       (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
  3184.  
  3185.       obj = Qnil;
  3186.  
  3187.       /* Decide if we should generate a switch-frame event.  Don't
  3188.      generate switch-frame events for motion outside of all Emacs
  3189.      frames.  */
  3190.       if (!NILP (x) && f)
  3191.     {
  3192.       Lisp_Object frame;
  3193.  
  3194.       frame = FRAME_FOCUS_FRAME (f);
  3195.       if (NILP (frame))
  3196.         XSETFRAME (frame, f);
  3197.  
  3198.       if (! EQ (frame, internal_last_event_frame)
  3199.           && XFRAME (frame) != selected_frame)
  3200.         obj = make_lispy_switch_frame (frame);
  3201.       internal_last_event_frame = frame;
  3202.     }
  3203.  
  3204.       /* If we didn't decide to make a switch-frame event, go ahead and
  3205.      return a mouse-motion event.  */
  3206.       if (!NILP (x) && NILP (obj))
  3207.     obj = make_lispy_movement (f, bar_window, part, x, y, time);
  3208.     }
  3209. #endif    /* HAVE_MOUSE */
  3210.   else
  3211.     /* We were promised by the above while loop that there was
  3212.        something for us to read!  */
  3213.     abort ();
  3214.  
  3215.   input_pending = readable_events (0);
  3216.  
  3217.   Vlast_event_frame = internal_last_event_frame;
  3218.  
  3219.   return (obj);
  3220. }
  3221.  
  3222. /* Process any events that are not user-visible,
  3223.    then return, without reading any user-visible events.  */
  3224.  
  3225. void
  3226. swallow_events (do_display)
  3227.      int do_display;
  3228. {
  3229.   int old_timers_run;
  3230.  
  3231.   while (kbd_fetch_ptr != kbd_store_ptr)
  3232.     {
  3233.       struct input_event *event;
  3234.  
  3235.       event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
  3236.            ? kbd_fetch_ptr
  3237.            : kbd_buffer);
  3238.  
  3239.       last_event_timestamp = event->timestamp;
  3240.  
  3241.       /* These two kinds of events get special handling
  3242.      and don't actually appear to the command loop.  */
  3243.       if (event->kind == selection_request_event)
  3244.     {
  3245. #ifdef HAVE_X11
  3246.       struct input_event copy;
  3247.  
  3248.       /* Remove it from the buffer before processing it,
  3249.          since otherwise swallow_events called recursively could see it
  3250.          and process it again.  */
  3251.       copy = *event;
  3252.       kbd_fetch_ptr = event + 1;
  3253.       input_pending = readable_events (0);
  3254.       x_handle_selection_request (©);
  3255. #else
  3256.       /* We're getting selection request events, but we don't have
  3257.              a window system.  */
  3258.       abort ();
  3259. #endif
  3260.     }
  3261.  
  3262.       else if (event->kind == selection_clear_event)
  3263.     {
  3264. #ifdef HAVE_X11
  3265.       struct input_event copy;
  3266.  
  3267.       /* Remove it from the buffer before processing it,  */
  3268.       copy = *event;
  3269.  
  3270.       kbd_fetch_ptr = event + 1;
  3271.       input_pending = readable_events (0);
  3272.       x_handle_selection_clear (©);
  3273. #else
  3274.       /* We're getting selection request events, but we don't have
  3275.              a window system.  */
  3276.       abort ();
  3277. #endif
  3278.     }
  3279.       else
  3280.     break;
  3281.     }
  3282.  
  3283.   old_timers_run = timers_run;
  3284.   get_input_pending (&input_pending, 1);
  3285.  
  3286.   if (timers_run != old_timers_run && do_display)
  3287.     redisplay_preserve_echo_area ();
  3288. }
  3289.  
  3290. static EMACS_TIME timer_idleness_start_time;
  3291.  
  3292. /* Record the start of when Emacs is idle,
  3293.    for the sake of running idle-time timers.  */
  3294.  
  3295. void
  3296. timer_start_idle ()
  3297. {
  3298.   Lisp_Object timers;
  3299.  
  3300.   /* If we are already in the idle state, do nothing.  */
  3301.   if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
  3302.     return;
  3303.  
  3304.   EMACS_GET_TIME (timer_idleness_start_time);
  3305.  
  3306.   /* Mark all idle-time timers as once again candidates for running.  */
  3307.   for (timers = Vtimer_idle_list; CONSP (timers); timers = XCONS (timers)->cdr)
  3308.     {
  3309.       Lisp_Object timer;
  3310.  
  3311.       timer = XCONS (timers)->car;
  3312.  
  3313.       if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
  3314.     continue;
  3315.       XVECTOR (timer)->contents[0] = Qnil;
  3316.     }
  3317. }
  3318.  
  3319. /* Record that Emacs is no longer idle, so stop running idle-time timers.  */
  3320.  
  3321. void
  3322. timer_stop_idle ()
  3323. {
  3324.   EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
  3325. }
  3326.  
  3327. /* This is only for debugging.  */
  3328. struct input_event last_timer_event;
  3329.  
  3330. /* Check whether a timer has fired.  To prevent larger problems we simply
  3331.    disregard elements that are not proper timers.  Do not make a circular
  3332.    timer list for the time being.
  3333.  
  3334.    Returns the number of seconds to wait until the next timer fires.  If a
  3335.    timer is triggering now, return zero seconds.
  3336.    If no timer is active, return -1 seconds.
  3337.  
  3338.    If a timer is ripe, we run it, with quitting turned off.
  3339.  
  3340.    DO_IT_NOW is now ignored.  It used to mean that we should
  3341.    run the timer directly instead of queueing a timer-event.
  3342.    Now we always run timers directly.  */
  3343.  
  3344. EMACS_TIME
  3345. timer_check (do_it_now)
  3346.      int do_it_now;
  3347. {
  3348.   EMACS_TIME nexttime;
  3349.   EMACS_TIME now, idleness_now;
  3350.   Lisp_Object timers, idle_timers, chosen_timer;
  3351.   struct gcpro gcpro1, gcpro2, gcpro3;
  3352.  
  3353.   EMACS_SET_SECS (nexttime, -1);
  3354.   EMACS_SET_USECS (nexttime, -1);
  3355.  
  3356.   /* Always consider the ordinary timers.  */
  3357.   timers = Vtimer_list;
  3358.   /* Consider the idle timers only if Emacs is idle.  */
  3359.   if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
  3360.     idle_timers = Vtimer_idle_list;
  3361.   else
  3362.     idle_timers = Qnil;
  3363.   chosen_timer = Qnil;
  3364.   GCPRO3 (timers, idle_timers, chosen_timer);
  3365.  
  3366.   if (CONSP (timers) || CONSP (idle_timers))
  3367.     {
  3368.       EMACS_GET_TIME (now);
  3369.       if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
  3370.     EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
  3371.     }
  3372.  
  3373.   while (CONSP (timers) || CONSP (idle_timers))
  3374.     {
  3375.       int triggertime = EMACS_SECS (now);
  3376.       Lisp_Object *vector;
  3377.       Lisp_Object timer, idle_timer;
  3378.       EMACS_TIME timer_time, idle_timer_time;
  3379.       EMACS_TIME difference, timer_difference, idle_timer_difference;
  3380.  
  3381.       /* Skip past invalid timers and timers already handled.  */
  3382.       if (!NILP (timers))
  3383.     {
  3384.       timer = XCONS (timers)->car;
  3385.       if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
  3386.         {
  3387.           timers = XCONS (timers)->cdr;
  3388.           continue;
  3389.         }
  3390.       vector = XVECTOR (timer)->contents;
  3391.  
  3392.       if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
  3393.           || !INTEGERP (vector[3])
  3394.           || ! NILP (vector[0]))
  3395.         {
  3396.           timers = XCONS (timers)->cdr;
  3397.           continue;
  3398.         }
  3399.     }
  3400.       if (!NILP (idle_timers))
  3401.     {
  3402.       timer = XCONS (idle_timers)->car;
  3403.       if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
  3404.         {
  3405.           idle_timers = XCONS (idle_timers)->cdr;
  3406.           continue;
  3407.         }
  3408.       vector = XVECTOR (timer)->contents;
  3409.  
  3410.       if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
  3411.           || !INTEGERP (vector[3])
  3412.           || ! NILP (vector[0]))
  3413.         {
  3414.           idle_timers = XCONS (idle_timers)->cdr;
  3415.           continue;
  3416.         }
  3417.     }
  3418.  
  3419.       /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
  3420.      based on the next ordinary timer.
  3421.      TIMER_DIFFERENCE is the distance in time from NOW to when
  3422.      this timer becomes ripe (negative if it's already ripe).  */
  3423.       if (!NILP (timers))
  3424.     {
  3425.       timer = XCONS (timers)->car;
  3426.       vector = XVECTOR (timer)->contents;
  3427.       EMACS_SET_SECS (timer_time,
  3428.               (XINT (vector[1]) << 16) | (XINT (vector[2])));
  3429.       EMACS_SET_USECS (timer_time, XINT (vector[3]));
  3430.       EMACS_SUB_TIME (timer_difference, timer_time, now);
  3431.     }
  3432.  
  3433.       /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
  3434.      based on the next idle timer.  */
  3435.       if (!NILP (idle_timers))
  3436.     {
  3437.       idle_timer = XCONS (idle_timers)->car;
  3438.       vector = XVECTOR (idle_timer)->contents;
  3439.       EMACS_SET_SECS (idle_timer_time,
  3440.               (XINT (vector[1]) << 16) | (XINT (vector[2])));
  3441.       EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
  3442.       EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
  3443.     }
  3444.  
  3445.       /* Decide which timer is the next timer,
  3446.      and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
  3447.      Also step down the list where we found that timer.  */
  3448.  
  3449.       if (! NILP (timers) && ! NILP (idle_timers))
  3450.     {
  3451.       EMACS_TIME temp;
  3452.       EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
  3453.       if (EMACS_TIME_NEG_P (temp))
  3454.         {
  3455.           chosen_timer = timer;
  3456.           timers = XCONS (timers)->cdr;
  3457.           difference = timer_difference;
  3458.         }
  3459.       else
  3460.         {
  3461.           chosen_timer = idle_timer;
  3462.           idle_timers = XCONS (idle_timers)->cdr;
  3463.           difference = idle_timer_difference;
  3464.         }
  3465.     }
  3466.       else if (! NILP (timers))
  3467.     {
  3468.       chosen_timer = timer;
  3469.       timers = XCONS (timers)->cdr;
  3470.       difference = timer_difference;
  3471.     }
  3472.       else
  3473.     {
  3474.       chosen_timer = idle_timer;
  3475.       idle_timers = XCONS (idle_timers)->cdr;
  3476.       difference = idle_timer_difference;
  3477.     }
  3478.       vector = XVECTOR (chosen_timer)->contents;
  3479.     
  3480.       /* If timer is rupe, run it if it hasn't been run.  */
  3481.       if (EMACS_TIME_NEG_P (difference)
  3482.       || (EMACS_SECS (difference) == 0
  3483.           && EMACS_USECS (difference) == 0))
  3484.     {
  3485.       if (NILP (vector[0]))
  3486.         {
  3487.           Lisp_Object tem;
  3488.           int was_locked = single_kboard;
  3489.           int count = specpdl_ptr - specpdl;
  3490.  
  3491.           /* Mark the timer as triggered to prevent problems if the lisp
  3492.          code fails to reschedule it right.  */
  3493.           vector[0] = Qt;
  3494.  
  3495.           specbind (Qinhibit_quit, Qt);
  3496.  
  3497.           call1 (Qtimer_event_handler, chosen_timer);
  3498.           timers_run++;
  3499.  
  3500.           unbind_to (count, Qnil);
  3501.  
  3502.           /* Resume allowing input from any kboard, if that was true before.  */
  3503.           if (!was_locked)
  3504.         any_kboard_state ();
  3505.  
  3506.           /* Since we have handled the event,
  3507.          we don't need to tell the caller to wake up and do it.  */
  3508.         }
  3509.     }
  3510.       else
  3511.     /* When we encounter a timer that is still waiting,
  3512.        return the amount of time to wait before it is ripe.  */
  3513.     {
  3514.       UNGCPRO;
  3515.       return difference;
  3516.     }
  3517.     }
  3518.  
  3519.   /* No timers are pending in the future.  */
  3520.   /* Return 0 if we generated an event, and -1 if not.  */
  3521.   UNGCPRO;
  3522.   return nexttime;
  3523. }
  3524.  
  3525. /* Caches for modify_event_symbol.  */
  3526. static Lisp_Object accent_key_syms;
  3527. static Lisp_Object func_key_syms;
  3528. static Lisp_Object mouse_syms;
  3529. #ifdef WINDOWSNT
  3530. static Lisp_Object mouse_wheel_syms;
  3531. #endif
  3532. static Lisp_Object drag_n_drop_syms;
  3533.  
  3534. /* This is a list of keysym codes for special "accent" characters.
  3535.    It parallels lispy_accent_keys.  */
  3536.  
  3537. static int lispy_accent_codes[] =
  3538. {
  3539. #ifdef XK_dead_circumflex
  3540.   XK_dead_circumflex,
  3541. #else
  3542.   0,
  3543. #endif
  3544. #ifdef XK_dead_grave
  3545.   XK_dead_grave,
  3546. #else
  3547.   0,
  3548. #endif
  3549. #ifdef XK_dead_tilde
  3550.   XK_dead_tilde,
  3551. #else
  3552.   0,
  3553. #endif
  3554. #ifdef XK_dead_diaeresis
  3555.   XK_dead_diaeresis,
  3556. #else
  3557.   0,
  3558. #endif
  3559. #ifdef XK_dead_macron
  3560.   XK_dead_macron,
  3561. #else
  3562.   0,
  3563. #endif
  3564. #ifdef XK_dead_degree
  3565.   XK_dead_degree,
  3566. #else
  3567.   0,
  3568. #endif
  3569. #ifdef XK_dead_acute
  3570.   XK_dead_acute,
  3571. #else
  3572.   0,
  3573. #endif
  3574. #ifdef XK_dead_cedilla
  3575.   XK_dead_cedilla,
  3576. #else
  3577.   0,
  3578. #endif
  3579. #ifdef XK_dead_breve
  3580.   XK_dead_breve,
  3581. #else
  3582.   0,
  3583. #endif
  3584. #ifdef XK_dead_ogonek
  3585.   XK_dead_ogonek,
  3586. #else
  3587.   0,
  3588. #endif
  3589. #ifdef XK_dead_caron
  3590.   XK_dead_caron,
  3591. #else
  3592.   0,
  3593. #endif
  3594. #ifdef XK_dead_doubleacute
  3595.   XK_dead_doubleacute,
  3596. #else
  3597.   0,
  3598. #endif
  3599. #ifdef XK_dead_abovedot
  3600.   XK_dead_abovedot,
  3601. #else
  3602.   0,
  3603. #endif
  3604. };
  3605.  
  3606. /* This is a list of Lisp names for special "accent" characters.
  3607.    It parallels lispy_accent_codes.  */
  3608.  
  3609. static char *lispy_accent_keys[] =
  3610. {
  3611.   "dead-circumflex",
  3612.   "dead-grave",
  3613.   "dead-tilde",
  3614.   "dead-diaeresis",
  3615.   "dead-macron",
  3616.   "dead-degree",
  3617.   "dead-acute",
  3618.   "dead-cedilla",
  3619.   "dead-breve",
  3620.   "dead-ogonek",
  3621.   "dead-caron",
  3622.   "dead-doubleacute",
  3623.   "dead-abovedot",
  3624. };
  3625.  
  3626. #ifdef HAVE_NTGUI
  3627. #define FUNCTION_KEY_OFFSET 0x0
  3628.  
  3629. char *lispy_function_keys[] =
  3630.   {
  3631.     0,                /* 0                      */
  3632.     
  3633.     0,                /* VK_LBUTTON        0x01 */
  3634.     0,                /* VK_RBUTTON        0x02 */
  3635.     "cancel",         /* VK_CANCEL         0x03 */
  3636.     0,                /* VK_MBUTTON        0x04 */
  3637.     
  3638.     0, 0, 0,          /*    0x05 .. 0x07        */
  3639.     
  3640.     "backspace",      /* VK_BACK           0x08 */
  3641.     "tab",            /* VK_TAB            0x09 */
  3642.     
  3643.     0, 0,             /*    0x0A .. 0x0B        */
  3644.     
  3645.     "clear",          /* VK_CLEAR          0x0C */
  3646.     "return",         /* VK_RETURN         0x0D */
  3647.     
  3648.     0, 0,             /*    0x0E .. 0x0F        */
  3649.   
  3650.     "shift",          /* VK_SHIFT          0x10 */
  3651.     "control",        /* VK_CONTROL        0x11 */
  3652.     "menu",           /* VK_MENU           0x12 */
  3653.     "pause",          /* VK_PAUSE          0x13 */
  3654.     "capital",        /* VK_CAPITAL        0x14 */
  3655.     
  3656.     0, 0, 0, 0, 0, 0, /*    0x15 .. 0x1A        */
  3657.     
  3658.     0,                /* VK_ESCAPE         0x1B */
  3659.     
  3660.     0, 0, 0, 0,       /*    0x1C .. 0x1F        */
  3661.     
  3662.     0,                /* VK_SPACE          0x20 */
  3663.     "prior",          /* VK_PRIOR          0x21 */
  3664.     "next",           /* VK_NEXT           0x22 */
  3665.     "end",            /* VK_END            0x23 */
  3666.     "home",           /* VK_HOME           0x24 */
  3667.     "left",           /* VK_LEFT           0x25 */
  3668.     "up",             /* VK_UP             0x26 */
  3669.     "right",          /* VK_RIGHT          0x27 */
  3670.     "down",           /* VK_DOWN           0x28 */
  3671.     "select",         /* VK_SELECT         0x29 */
  3672.     "print",          /* VK_PRINT          0x2A */
  3673.     "execute",        /* VK_EXECUTE        0x2B */
  3674.     "snapshot",       /* VK_SNAPSHOT       0x2C */
  3675.     "insert",         /* VK_INSERT         0x2D */
  3676.     "delete",         /* VK_DELETE         0x2E */
  3677.     "help",           /* VK_HELP           0x2F */
  3678.   
  3679.     /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
  3680.     
  3681.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3682.     
  3683.     0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40       */
  3684.     
  3685.     /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
  3686.     
  3687.     0, 0, 0, 0, 0, 0, 0, 0, 0, 
  3688.     0, 0, 0, 0, 0, 0, 0, 0, 0, 
  3689.     0, 0, 0, 0, 0, 0, 0, 0,
  3690.     
  3691.     "lwindow",       /* VK_LWIN           0x5B */
  3692.     "rwindow",       /* VK_RWIN           0x5C */
  3693.     "apps",          /* VK_APPS           0x5D */
  3694.     
  3695.     0, 0,            /*    0x5E .. 0x5F        */
  3696.     
  3697.     "kp-0",          /* VK_NUMPAD0        0x60 */
  3698.     "kp-1",          /* VK_NUMPAD1        0x61 */
  3699.     "kp-2",          /* VK_NUMPAD2        0x62 */
  3700.     "kp-3",          /* VK_NUMPAD3        0x63 */
  3701.     "kp-4",          /* VK_NUMPAD4        0x64 */
  3702.     "kp-5",          /* VK_NUMPAD5        0x65 */
  3703.     "kp-6",          /* VK_NUMPAD6        0x66 */
  3704.     "kp-7",          /* VK_NUMPAD7        0x67 */
  3705.     "kp-8",          /* VK_NUMPAD8        0x68 */
  3706.     "kp-9",          /* VK_NUMPAD9        0x69 */
  3707.     "kp-multiply",   /* VK_MULTIPLY       0x6A */
  3708.     "kp-add",        /* VK_ADD            0x6B */
  3709.     "kp-separator",  /* VK_SEPARATOR      0x6C */
  3710.     "kp-subtract",   /* VK_SUBTRACT       0x6D */
  3711.     "kp-decimal",    /* VK_DECIMAL        0x6E */
  3712.     "kp-divide",     /* VK_DIVIDE         0x6F */
  3713.     "f1",            /* VK_F1             0x70 */
  3714.     "f2",            /* VK_F2             0x71 */
  3715.     "f3",            /* VK_F3             0x72 */
  3716.     "f4",            /* VK_F4             0x73 */
  3717.     "f5",            /* VK_F5             0x74 */
  3718.     "f6",            /* VK_F6             0x75 */
  3719.     "f7",            /* VK_F7             0x76 */
  3720.     "f8",            /* VK_F8             0x77 */
  3721.     "f9",            /* VK_F9             0x78 */
  3722.     "f10",           /* VK_F10            0x79 */
  3723.     "f11",           /* VK_F11            0x7A */
  3724.     "f12",           /* VK_F12            0x7B */
  3725.     "f13",           /* VK_F13            0x7C */
  3726.     "f14",           /* VK_F14            0x7D */
  3727.     "f15",           /* VK_F15            0x7E */
  3728.     "f16",           /* VK_F16            0x7F */
  3729.     "f17",           /* VK_F17            0x80 */
  3730.     "f18",           /* VK_F18            0x81 */
  3731.     "f19",           /* VK_F19            0x82 */
  3732.     "f20",           /* VK_F20            0x83 */
  3733.     "f21",           /* VK_F21            0x84 */
  3734.     "f22",           /* VK_F22            0x85 */
  3735.     "f23",           /* VK_F23            0x86 */
  3736.     "f24",           /* VK_F24            0x87 */
  3737.     
  3738.     0, 0, 0, 0,      /*    0x88 .. 0x8B        */
  3739.     0, 0, 0, 0,      /*    0x8C .. 0x8F        */
  3740.     
  3741.     "kp-numlock",    /* VK_NUMLOCK        0x90 */
  3742.     "scroll",        /* VK_SCROLL         0x91 */
  3743.     
  3744.     "kp-space",         /* VK_NUMPAD_CLEAR   0x92 */
  3745.     "kp-enter",         /* VK_NUMPAD_ENTER   0x93 */
  3746.     "kp-prior",         /* VK_NUMPAD_PRIOR   0x94 */
  3747.     "kp-next",         /* VK_NUMPAD_NEXT    0x95 */
  3748.     "kp-end",         /* VK_NUMPAD_END     0x96 */
  3749.     "kp-home",         /* VK_NUMPAD_HOME    0x97 */
  3750.     "kp-left",         /* VK_NUMPAD_LEFT    0x98 */
  3751.     "kp-up",         /* VK_NUMPAD_UP      0x99 */
  3752.     "kp-right",         /* VK_NUMPAD_RIGHT   0x9A */
  3753.     "kp-down",         /* VK_NUMPAD_DOWN    0x9B */
  3754.     "kp-insert",     /* VK_NUMPAD_INSERT  0x9C */
  3755.     "kp-delete",     /* VK_NUMPAD_DELETE  0x9D */
  3756.  
  3757.     0, 0,         /*    0x9E .. 0x9F        */
  3758.  
  3759.     /*
  3760.      * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
  3761.      * Used only as parameters to GetAsyncKeyState and GetKeyState.
  3762.      * No other API or message will distinguish left and right keys this way.
  3763.      */
  3764.     /* 0xA0 .. 0xEF */
  3765.     
  3766.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3767.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3768.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3769.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3770.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3771.     
  3772.     /* 0xF0 .. 0xF5 */
  3773.     
  3774.     0, 0, 0, 0, 0, 0,
  3775.     
  3776.     "attn",          /* VK_ATTN           0xF6 */
  3777.     "crsel",         /* VK_CRSEL          0xF7 */
  3778.     "exsel",         /* VK_EXSEL          0xF8 */
  3779.     "ereof",         /* VK_EREOF          0xF9 */
  3780.     "play",          /* VK_PLAY           0xFA */
  3781.     "zoom",          /* VK_ZOOM           0xFB */
  3782.     "noname",        /* VK_NONAME         0xFC */
  3783.     "pa1",           /* VK_PA1            0xFD */
  3784.     "oem_clear",     /* VK_OEM_CLEAR      0xFE */
  3785.   };
  3786.  
  3787. #else /* not HAVE_NTGUI */
  3788.  
  3789. #ifdef XK_kana_A
  3790. static char *lispy_kana_keys[] =
  3791.   {
  3792.     /* X Keysym value */
  3793.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x400 .. 0x40f */
  3794.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x410 .. 0x41f */
  3795.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x420 .. 0x42f */
  3796.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x430 .. 0x43f */
  3797.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x440 .. 0x44f */
  3798.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x450 .. 0x45f */
  3799.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x460 .. 0x46f */
  3800.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
  3801.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x480 .. 0x48f */
  3802.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x490 .. 0x49f */
  3803.     0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket", 
  3804.     "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
  3805.     "kana-i", "kana-u", "kana-e", "kana-o",
  3806.     "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
  3807.     "prolongedsound", "kana-A", "kana-I", "kana-U",
  3808.     "kana-E", "kana-O", "kana-KA", "kana-KI",
  3809.     "kana-KU", "kana-KE", "kana-KO", "kana-SA",
  3810.     "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
  3811.     "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
  3812.     "kana-TO", "kana-NA", "kana-NI", "kana-NU",
  3813.     "kana-NE", "kana-NO", "kana-HA", "kana-HI",
  3814.     "kana-FU", "kana-HE", "kana-HO", "kana-MA",
  3815.     "kana-MI", "kana-MU", "kana-ME", "kana-MO",
  3816.     "kana-YA", "kana-YU", "kana-YO", "kana-RA",
  3817.     "kana-RI", "kana-RU", "kana-RE", "kana-RO",
  3818.     "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
  3819.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x4e0 .. 0x4ef */
  3820.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    /* 0x4f0 .. 0x4ff */
  3821.   };
  3822. #endif /* XK_kana_A */
  3823.  
  3824. #define FUNCTION_KEY_OFFSET 0xff00
  3825.  
  3826. /* You'll notice that this table is arranged to be conveniently
  3827.    indexed by X Windows keysym values.  */
  3828. static char *lispy_function_keys[] =
  3829.   {
  3830.     /* X Keysym value */
  3831.  
  3832.     0, 0, 0, 0, 0, 0, 0, 0,                  /* 0xff00...0f */
  3833.     "backspace", "tab", "linefeed", "clear",
  3834.     0, "return", 0, 0,
  3835.     0, 0, 0, "pause",                      /* 0xff10...1f */
  3836.     0, 0, 0, 0, 0, 0, 0, "escape",
  3837.     0, 0, 0, 0,
  3838.     0, "kanji", "muhenkan", "henkan",              /* 0xff20...2f */
  3839.     "romaji", "hiragana", "katakana", "hiragana-katakana",
  3840.     "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
  3841.     "massyo", "kana-lock", "kana-shift", "eisu-shift",
  3842.     "eisu-toggle",                      /* 0xff30...3f */
  3843.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3844.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0xff40...4f */
  3845.  
  3846.     "home", "left", "up", "right", /* 0xff50 */    /* IsCursorKey */
  3847.     "down", "prior", "next", "end",
  3848.     "begin", 0, 0, 0, 0, 0, 0, 0,
  3849.     "select",            /* 0xff60 */    /* IsMiscFunctionKey */
  3850.     "print",
  3851.     "execute",
  3852.     "insert",
  3853.     0,        /* 0xff64 */
  3854.     "undo",
  3855.     "redo",
  3856.     "menu",
  3857.     "find",
  3858.     "cancel",
  3859.     "help",
  3860.     "break",            /* 0xff6b */
  3861.  
  3862.     0, 0, 0, 0,
  3863.     0, 0, 0, 0, "backtab", 0, 0, 0,        /* 0xff70... */
  3864.     0, 0, 0, 0, 0, 0, 0, "kp-numlock",        /* 0xff78... */
  3865.     "kp-space",            /* 0xff80 */    /* IsKeypadKey */
  3866.     0, 0, 0, 0, 0, 0, 0, 0,
  3867.     "kp-tab",            /* 0xff89 */
  3868.     0, 0, 0,
  3869.     "kp-enter",            /* 0xff8d */
  3870.     0, 0, 0,
  3871.     "kp-f1",            /* 0xff91 */
  3872.     "kp-f2",
  3873.     "kp-f3",
  3874.     "kp-f4",
  3875.     "kp-home",            /* 0xff95 */
  3876.     "kp-left",
  3877.     "kp-up",
  3878.     "kp-right",
  3879.     "kp-down",
  3880.     "kp-prior",            /* kp-page-up */
  3881.     "kp-next",            /* kp-page-down */
  3882.     "kp-end",
  3883.     "kp-begin",
  3884.     "kp-insert",
  3885.     "kp-delete",
  3886.     0,                /* 0xffa0 */
  3887.     0, 0, 0, 0, 0, 0, 0, 0, 0,
  3888.     "kp-multiply",        /* 0xffaa */
  3889.     "kp-add",
  3890.     "kp-separator",
  3891.     "kp-subtract",
  3892.     "kp-decimal",
  3893.     "kp-divide",        /* 0xffaf */
  3894.     "kp-0",            /* 0xffb0 */
  3895.     "kp-1",    "kp-2",    "kp-3",    "kp-4",    "kp-5",    "kp-6",    "kp-7",    "kp-8",    "kp-9",
  3896.     0,        /* 0xffba */
  3897.     0, 0,
  3898.     "kp-equal",            /* 0xffbd */
  3899.     "f1",            /* 0xffbe */    /* IsFunctionKey */
  3900.     "f2",
  3901.     "f3", "f4", "f5", "f6", "f7", "f8",    "f9", "f10", /* 0xffc0 */
  3902.     "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
  3903.     "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
  3904.     "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
  3905.     "f35", 0, 0, 0, 0, 0, 0, 0,    /* 0xffe0 */
  3906.     0, 0, 0, 0, 0, 0, 0, 0,
  3907.     0, 0, 0, 0, 0, 0, 0, 0,     /* 0xfff0 */
  3908.     0, 0, 0, 0, 0, 0, 0, "delete"
  3909.   };
  3910.  
  3911. /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE.  */
  3912. #define ISO_FUNCTION_KEY_OFFSET 0xfe00
  3913.  
  3914. static char *iso_lispy_function_keys[] =
  3915.   {
  3916.     0, 0, 0, 0, 0, 0, 0, 0,    /* 0xfe00 */
  3917.     0, 0, 0, 0, 0, 0, 0, 0,    /* 0xfe08 */
  3918.     0, 0, 0, 0, 0, 0, 0, 0,    /* 0xfe10 */
  3919.     0, 0, 0, 0, 0, 0, 0, 0,    /* 0xfe18 */
  3920.     "iso-lefttab",        /* 0xfe20 */
  3921.     "iso-move-line-up", "iso-move-line-down", 
  3922.     "iso-partial-line-up", "iso-partial-line-down", 
  3923.     "iso-partial-space-left", "iso-partial-space-right", 
  3924.     "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
  3925.     "iso-release-margin-left", "iso-release-margin-right",
  3926.     "iso-release-both-margins",
  3927.     "iso-fast-cursor-left", "iso-fast-cursor-right",
  3928.     "iso-fast-cursor-up", "iso-fast-cursor-down",
  3929.     "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
  3930.     "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
  3931.   };
  3932.  
  3933. #endif /* not HAVE_NTGUI */
  3934.  
  3935. static char *lispy_mouse_names[] =
  3936. {
  3937.   "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
  3938. #ifdef HAVE_PM
  3939.   , "drop-file", "drop-color"
  3940. #endif /* HAVE_PM */
  3941. };
  3942.  
  3943. #ifdef WINDOWSNT
  3944. /* mouse-wheel events are generated by the wheel on devices such as
  3945.    the MS Intellimouse.  The wheel sits in between the left and right
  3946.    mouse buttons, and is typically used to scroll or zoom the window
  3947.    underneath the pointer.  mouse-wheel events specify the object on
  3948.    which they operate, and a delta corresponding to the amount and
  3949.    direction that the wheel is rotated.  Clicking the mouse-wheel
  3950.    generates a mouse-2 event.  */
  3951. static char *lispy_mouse_wheel_names[] = 
  3952. {
  3953.   "mouse-wheel"
  3954. };
  3955.  
  3956. #endif /* WINDOWSNT */
  3957.  
  3958. /* drag-n-drop events are generated when a set of selected files are
  3959.    dragged from another application and dropped onto an Emacs window.  */
  3960. static char *lispy_drag_n_drop_names[] =
  3961. {
  3962.   "drag-n-drop"
  3963. };
  3964.  
  3965. /* Scroll bar parts.  */
  3966. Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
  3967. Lisp_Object Qup, Qdown;
  3968.  
  3969. /* An array of scroll bar parts, indexed by an enum scroll_bar_part value.  */
  3970. Lisp_Object *scroll_bar_parts[] = {
  3971.   &Qabove_handle, &Qhandle, &Qbelow_handle,
  3972.   &Qup, &Qdown,
  3973. };
  3974.  
  3975.  
  3976. /* A vector, indexed by button number, giving the down-going location
  3977.    of currently depressed buttons, both scroll bar and non-scroll bar.
  3978.  
  3979.    The elements have the form
  3980.      (BUTTON-NUMBER MODIFIER-MASK . REST)
  3981.    where REST is the cdr of a position as it would be reported in the event.
  3982.  
  3983.    The make_lispy_event function stores positions here to tell the
  3984.    difference between click and drag events, and to store the starting
  3985.    location to be included in drag events.  */
  3986.  
  3987. static Lisp_Object button_down_location;
  3988.  
  3989. /* Information about the most recent up-going button event:  Which
  3990.    button, what location, and what time. */
  3991.  
  3992. static int last_mouse_button;
  3993. static int last_mouse_x;
  3994. static int last_mouse_y;
  3995. static unsigned long button_down_time;
  3996.  
  3997. /* The maximum time between clicks to make a double-click,
  3998.    or Qnil to disable double-click detection,
  3999.    or Qt for no time limit.  */
  4000. Lisp_Object Vdouble_click_time;
  4001.  
  4002. /* The number of clicks in this multiple-click. */
  4003.  
  4004. int double_click_count;
  4005.  
  4006. /* Given a struct input_event, build the lisp event which represents
  4007.    it.  If EVENT is 0, build a mouse movement event from the mouse
  4008.    movement buffer, which should have a movement event in it.
  4009.  
  4010.    Note that events must be passed to this function in the order they
  4011.    are received; this function stores the location of button presses
  4012.    in order to build drag events when the button is released.  */
  4013.  
  4014. static Lisp_Object
  4015. make_lispy_event (event)
  4016.      struct input_event *event;
  4017. {
  4018.   int i;
  4019.  
  4020.   switch (SWITCH_ENUM_CAST (event->kind))
  4021.     {
  4022.       /* A simple keystroke.  */
  4023.     case ascii_keystroke:
  4024.       {
  4025.     Lisp_Object lispy_c;
  4026.     int c = event->code & 0377;
  4027.     /* Turn ASCII characters into control characters
  4028.        when proper.  */
  4029.     if (event->modifiers & ctrl_modifier)
  4030.       c = make_ctrl_char (c);
  4031.  
  4032.     /* Add in the other modifier bits.  We took care of ctrl_modifier
  4033.        just above, and the shift key was taken care of by the X code,
  4034.        and applied to control characters by make_ctrl_char.  */
  4035.     c |= (event->modifiers
  4036.           & (meta_modifier | alt_modifier
  4037.          | hyper_modifier | super_modifier));
  4038.     /* Distinguish Shift-SPC from SPC.  */
  4039.     if ((event->code & 0377) == 040
  4040.         && event->modifiers & shift_modifier)
  4041.       c |= shift_modifier;
  4042.     button_down_time = 0;
  4043.     XSETFASTINT (lispy_c, c);
  4044.     return lispy_c;
  4045.       }
  4046.  
  4047.       /* A function key.  The symbol may need to have modifier prefixes
  4048.      tacked onto it.  */
  4049.     case non_ascii_keystroke:
  4050.       button_down_time = 0;
  4051.  
  4052.       for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
  4053.     if (event->code == lispy_accent_codes[i])
  4054.       return modify_event_symbol (i,
  4055.                       event->modifiers,
  4056.                       Qfunction_key, Qnil,
  4057.                       lispy_accent_keys, &accent_key_syms,
  4058.                       (sizeof (lispy_accent_keys)
  4059.                        / sizeof (lispy_accent_keys[0])));
  4060.  
  4061.       /* Handle system-specific keysyms.  */
  4062.       if (event->code & (1 << 28))
  4063.     {
  4064.       /* We need to use an alist rather than a vector as the cache
  4065.          since we can't make a vector long enuf.  */
  4066.       if (NILP (current_kboard->system_key_syms))
  4067.         current_kboard->system_key_syms = Fcons (Qnil, Qnil);
  4068.       return modify_event_symbol (event->code,
  4069.                       event->modifiers,
  4070.                       Qfunction_key,
  4071.                       current_kboard->Vsystem_key_alist,
  4072.                       0, ¤t_kboard->system_key_syms,
  4073.                       (unsigned)-1);
  4074.     }
  4075.  
  4076. #ifdef XK_kana_A
  4077.       if (event->code >= 0x400 && event->code < 0x500)
  4078.     return modify_event_symbol (event->code - 0x400,
  4079.                     event->modifiers & ~shift_modifier,
  4080.                     Qfunction_key, Qnil,
  4081.                     lispy_kana_keys, &func_key_syms,
  4082.                     (sizeof (lispy_kana_keys)
  4083.                      / sizeof (lispy_kana_keys[0])));
  4084. #endif /* XK_kana_A */
  4085.  
  4086. #ifdef ISO_FUNCTION_KEY_OFFSET
  4087.       if (event->code < FUNCTION_KEY_OFFSET
  4088.       && event->code >= ISO_FUNCTION_KEY_OFFSET)
  4089.     return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
  4090.                     event->modifiers,
  4091.                     Qfunction_key, Qnil,
  4092.                     iso_lispy_function_keys, &func_key_syms,
  4093.                     (sizeof (iso_lispy_function_keys)
  4094.                      / sizeof (iso_lispy_function_keys[0])));
  4095.       else
  4096. #endif
  4097.     return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
  4098.                     event->modifiers,
  4099.                     Qfunction_key, Qnil,
  4100.                     lispy_function_keys, &func_key_syms,
  4101.                     (sizeof (lispy_function_keys)
  4102.                      / sizeof (lispy_function_keys[0])));
  4103.  
  4104. #ifdef HAVE_MOUSE
  4105.       /* A mouse click.  Figure out where it is, decide whether it's
  4106.          a press, click or drag, and build the appropriate structure.  */
  4107.     case mouse_click:
  4108.     case scroll_bar_click:
  4109.       {
  4110.     int button = event->code;
  4111.     int is_double;
  4112.     Lisp_Object position;
  4113.     Lisp_Object *start_pos_ptr;
  4114.     Lisp_Object start_pos;
  4115.  
  4116.     if (button < 0 || button >= NUM_MOUSE_BUTTONS)
  4117.       abort ();
  4118.  
  4119.     /* Build the position as appropriate for this mouse click.  */
  4120.     if (event->kind == mouse_click)
  4121.       {
  4122.         int part;
  4123.         FRAME_PTR f = XFRAME (event->frame_or_window);
  4124.         Lisp_Object window;
  4125.         Lisp_Object posn;
  4126.         int row, column;
  4127.  
  4128.         /* Ignore mouse events that were made on frame that
  4129.            have been deleted.  */
  4130.         if (! FRAME_LIVE_P (f))
  4131.           return Qnil;
  4132.  
  4133.         pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
  4134.                    &column, &row, NULL, 1);
  4135.  
  4136. #ifndef USE_X_TOOLKIT
  4137.         /* In the non-toolkit version, clicks on the menu bar
  4138.            are ordinary button events in the event buffer.
  4139.            Distinguish them, and invoke the menu.
  4140.  
  4141.            (In the toolkit version, the toolkit handles the menu bar
  4142.            and Emacs doesn't know about it until after the user
  4143.            makes a selection.)  */
  4144.         if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
  4145.         && (event->modifiers & down_modifier))
  4146.           {
  4147.         Lisp_Object items, item;
  4148.         int hpos;
  4149.         int i;
  4150.  
  4151. #if 0
  4152.         /* Activate the menu bar on the down event.  If the
  4153.            up event comes in before the menu code can deal with it,
  4154.            just ignore it.  */
  4155.         if (! (event->modifiers & down_modifier))
  4156.           return Qnil;
  4157. #endif
  4158.  
  4159.         item = Qnil;
  4160.         items = FRAME_MENU_BAR_ITEMS (f);
  4161.         for (i = 0; i < XVECTOR (items)->size; i += 4)
  4162.           {
  4163.             Lisp_Object pos, string;
  4164.             string = XVECTOR (items)->contents[i + 1];
  4165.             pos = XVECTOR (items)->contents[i + 3];
  4166.             if (NILP (string))
  4167.               break;
  4168.             if (column >= XINT (pos)
  4169.             && column < XINT (pos) + XSTRING (string)->size)
  4170.               {
  4171.             item = XVECTOR (items)->contents[i];
  4172.             break;
  4173.               }
  4174.           }
  4175.  
  4176.         position
  4177.           = Fcons (event->frame_or_window,
  4178.                Fcons (Qmenu_bar,
  4179.                   Fcons (Fcons (event->x, event->y),
  4180.                      Fcons (make_number (event->timestamp),
  4181.                         Qnil))));
  4182.  
  4183.         return Fcons (item, Fcons (position, Qnil));
  4184.           }
  4185. #endif /* not USE_X_TOOLKIT */
  4186.  
  4187.         window = window_from_coordinates (f, column, row, &part);
  4188.  
  4189.         if (!WINDOWP (window))
  4190.           {
  4191.         window = event->frame_or_window;
  4192.         posn = Qnil;
  4193.           }
  4194.         else
  4195.           {
  4196.         int pixcolumn, pixrow;
  4197.         column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
  4198.         row -= XINT (XWINDOW (window)->top);
  4199.         glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
  4200.         XSETINT (event->x, pixcolumn);
  4201.         XSETINT (event->y, pixrow);
  4202.  
  4203.         if (part == 1)
  4204.           posn = Qmode_line;
  4205.         else if (part == 2)
  4206.           posn = Qvertical_line;
  4207.         else
  4208.           XSETINT (posn,
  4209.                buffer_posn_from_coords (XWINDOW (window),
  4210.                             column, row));
  4211.           }
  4212.  
  4213.         position
  4214.           = Fcons (window,
  4215.                Fcons (posn,
  4216.                   Fcons (Fcons (event->x, event->y),
  4217.                      Fcons (make_number (event->timestamp),
  4218.                         Qnil))));
  4219.       }
  4220.     else
  4221.       {
  4222.         Lisp_Object window;
  4223.         Lisp_Object portion_whole;
  4224.         Lisp_Object part;
  4225.  
  4226.         window = event->frame_or_window;
  4227.         portion_whole = Fcons (event->x, event->y);
  4228.         part = *scroll_bar_parts[(int) event->part];
  4229.  
  4230.         position
  4231.           = Fcons (window,
  4232.                Fcons (Qvertical_scroll_bar,
  4233.                   Fcons (portion_whole,
  4234.                      Fcons (make_number (event->timestamp),
  4235.                         Fcons (part, Qnil)))));
  4236.       }
  4237.  
  4238.     start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
  4239.  
  4240. #ifdef HAVE_PM
  4241.         if (event->modifiers == 0)
  4242.           {
  4243.             Lisp_Object head;
  4244.  
  4245.             head = modify_event_symbol (button,
  4246.                                         event->modifiers,
  4247.                                         Qmouse_click, Qnil,
  4248.                                         lispy_mouse_names, &mouse_syms,
  4249.                                         (sizeof (lispy_mouse_names)
  4250.                                          / sizeof (lispy_mouse_names[0])));
  4251.         return Fcons (head,
  4252.               Fcons (position,
  4253.                  Qnil));
  4254.           }
  4255. #endif /* HAVE_PM */
  4256.  
  4257.     start_pos = *start_pos_ptr;
  4258.     *start_pos_ptr = Qnil;
  4259.  
  4260.     is_double = (button == last_mouse_button
  4261.              && XINT (event->x) == last_mouse_x
  4262.              && XINT (event->y) == last_mouse_y
  4263.              && button_down_time != 0
  4264.              && (EQ (Vdouble_click_time, Qt)
  4265.              || (INTEGERP (Vdouble_click_time)
  4266.                  && ((int)(event->timestamp - button_down_time)
  4267.                  < XINT (Vdouble_click_time)))));
  4268.     last_mouse_button = button;
  4269.     last_mouse_x = XINT (event->x);
  4270.     last_mouse_y = XINT (event->y);
  4271.  
  4272.     /* If this is a button press, squirrel away the location, so
  4273.            we can decide later whether it was a click or a drag.  */
  4274.     if (event->modifiers & down_modifier)
  4275.       {
  4276.         if (is_double)
  4277.           {
  4278.         double_click_count++;
  4279.         event->modifiers |= ((double_click_count > 2)
  4280.                      ? triple_modifier
  4281.                      : double_modifier);
  4282.           }
  4283.         else
  4284.           double_click_count = 1;
  4285.         button_down_time = event->timestamp;
  4286.         *start_pos_ptr = Fcopy_alist (position);
  4287.       }
  4288.  
  4289.     /* Now we're releasing a button - check the co-ordinates to
  4290.            see if this was a click or a drag.  */
  4291.     else if (event->modifiers & up_modifier)
  4292.       {
  4293.         /* If we did not see a down before this up,
  4294.            ignore the up.  Probably this happened because
  4295.            the down event chose a menu item.
  4296.            It would be an annoyance to treat the release
  4297.            of the button that chose the menu item
  4298.            as a separate event.  */
  4299.  
  4300.         if (!CONSP (start_pos))
  4301.           return Qnil;
  4302.  
  4303.         event->modifiers &= ~up_modifier;
  4304. #if 0 /* Formerly we treated an up with no down as a click event.  */
  4305.         if (!CONSP (start_pos))
  4306.           event->modifiers |= click_modifier;
  4307.         else
  4308. #endif
  4309.           {
  4310.         /* The third element of every position should be the (x,y)
  4311.            pair.  */
  4312.         Lisp_Object down;
  4313.  
  4314.         down = Fnth (make_number (2), start_pos);
  4315.         if (EQ (event->x, XCONS (down)->car)
  4316.             && EQ (event->y, XCONS (down)->cdr))
  4317.           {
  4318.             event->modifiers |= click_modifier;
  4319.           }
  4320.         else
  4321.           {
  4322.             button_down_time = 0;
  4323.             event->modifiers |= drag_modifier;
  4324.           }
  4325.         /* Don't check is_double; treat this as multiple
  4326.            if the down-event was multiple.  */
  4327.         if (double_click_count > 1)
  4328.           event->modifiers |= ((double_click_count > 2)
  4329.                        ? triple_modifier
  4330.                        : double_modifier);
  4331.           }
  4332.       }
  4333.     else
  4334.       /* Every mouse event should either have the down_modifier or
  4335.              the up_modifier set.  */
  4336.       abort ();
  4337.  
  4338.     {
  4339.       /* Get the symbol we should use for the mouse click.  */
  4340.       Lisp_Object head;
  4341.  
  4342.       head = modify_event_symbol (button,
  4343.                       event->modifiers,
  4344.                       Qmouse_click, Qnil,
  4345.                       lispy_mouse_names, &mouse_syms,
  4346.                       (sizeof (lispy_mouse_names)
  4347.                        / sizeof (lispy_mouse_names[0])));
  4348.       if (event->modifiers & drag_modifier)
  4349.         return Fcons (head,
  4350.               Fcons (start_pos,
  4351.                  Fcons (position,
  4352.                     Qnil)));
  4353.       else if (event->modifiers & (double_modifier | triple_modifier))
  4354.         return Fcons (head,
  4355.               Fcons (position,
  4356.                  Fcons (make_number (double_click_count),
  4357.                     Qnil)));
  4358.       else
  4359.         return Fcons (head,
  4360.               Fcons (position,
  4361.                  Qnil));
  4362.     }
  4363.       }
  4364.  
  4365. #ifdef WINDOWSNT
  4366.     case w32_scroll_bar_click:
  4367.       {
  4368.     int button = event->code;
  4369.     int is_double;
  4370.     Lisp_Object position;
  4371.     Lisp_Object *start_pos_ptr;
  4372.     Lisp_Object start_pos;
  4373.  
  4374.     if (button < 0 || button >= NUM_MOUSE_BUTTONS)
  4375.       abort ();
  4376.  
  4377.     {
  4378.       Lisp_Object window;
  4379.       Lisp_Object portion_whole;
  4380.       Lisp_Object part;
  4381.  
  4382.       window = event->frame_or_window;
  4383.       portion_whole = Fcons (event->x, event->y);
  4384.       part = *scroll_bar_parts[(int) event->part];
  4385.  
  4386.       position
  4387.         = Fcons (window,
  4388.              Fcons (Qvertical_scroll_bar,
  4389.                 Fcons (portion_whole,
  4390.                    Fcons (make_number (event->timestamp),
  4391.                       Fcons (part, Qnil)))));
  4392.     }
  4393.  
  4394.     /* Always treat W32 scroll bar events as clicks. */
  4395.     event->modifiers |= click_modifier;
  4396.  
  4397.     {
  4398.       /* Get the symbol we should use for the mouse click.  */
  4399.       Lisp_Object head;
  4400.  
  4401.       head = modify_event_symbol (button,
  4402.                       event->modifiers,
  4403.                       Qmouse_click, Qnil,
  4404.                       lispy_mouse_names, &mouse_syms,
  4405.                       (sizeof (lispy_mouse_names)
  4406.                        / sizeof (lispy_mouse_names[0])));
  4407.       return Fcons (head,
  4408.             Fcons (position,
  4409.                    Qnil));
  4410.     }
  4411.       }
  4412.     case mouse_wheel:
  4413.       {
  4414.     int part;
  4415.     FRAME_PTR f = XFRAME (event->frame_or_window);
  4416.     Lisp_Object window;
  4417.     Lisp_Object posn;
  4418.     Lisp_Object head, position;
  4419.     int row, column;
  4420.  
  4421.     /* Ignore mouse events that were made on frame that
  4422.        have been deleted.  */
  4423.     if (! FRAME_LIVE_P (f))
  4424.       return Qnil;
  4425.     pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
  4426.                    &column, &row, NULL, 1);
  4427.     window = window_from_coordinates (f, column, row, &part);
  4428.  
  4429.     if (!WINDOWP (window))
  4430.       {
  4431.         window = event->frame_or_window;
  4432.         posn = Qnil;
  4433.       }
  4434.     else
  4435.       {
  4436.         int pixcolumn, pixrow;
  4437.         column -= XINT (XWINDOW (window)->left);
  4438.         row -= XINT (XWINDOW (window)->top);
  4439.         glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
  4440.         XSETINT (event->x, pixcolumn);
  4441.         XSETINT (event->y, pixrow);
  4442.  
  4443.         if (part == 1)
  4444.           posn = Qmode_line;
  4445.         else if (part == 2)
  4446.           posn = Qvertical_line;
  4447.         else
  4448.           XSETINT (posn,
  4449.                buffer_posn_from_coords (XWINDOW (window),
  4450.                         column, row));
  4451.       }
  4452.  
  4453.     {
  4454.       Lisp_Object head, position;
  4455.  
  4456.       position
  4457.         = Fcons (window,
  4458.              Fcons (posn,
  4459.                 Fcons (Fcons (event->x, event->y),
  4460.                    Fcons (make_number (event->timestamp),
  4461.                       Qnil))));
  4462.  
  4463.       head = modify_event_symbol (0, event->modifiers,
  4464.                       Qmouse_wheel, Qnil,
  4465.                       lispy_mouse_wheel_names,
  4466.                       &mouse_wheel_syms, 1);
  4467.       return Fcons (head,
  4468.             Fcons (position,
  4469.                    Fcons (make_number (event->code),
  4470.                       Qnil)));
  4471.     }
  4472.       }
  4473. #endif /* WINDOWSNT */
  4474.  
  4475.     case drag_n_drop:
  4476.       {
  4477.     int part;
  4478.     FRAME_PTR f;
  4479.     Lisp_Object window;
  4480.     Lisp_Object posn;
  4481.     Lisp_Object head, position;
  4482.     Lisp_Object files;
  4483.     int row, column;
  4484.  
  4485.     /* The frame_or_window field should be a cons of the frame in
  4486.        which the event occurred and a list of the filenames
  4487.        dropped.  */
  4488.     if (! CONSP (event->frame_or_window))
  4489.       abort ();
  4490.  
  4491.     f = XFRAME (XCONS (event->frame_or_window)->car);
  4492.     files = XCONS (event->frame_or_window)->cdr;
  4493.  
  4494.     /* Ignore mouse events that were made on frames that
  4495.        have been deleted.  */
  4496.     if (! FRAME_LIVE_P (f))
  4497.       return Qnil;
  4498.     pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
  4499.                    &column, &row, NULL, 1);
  4500.     window = window_from_coordinates (f, column, row, &part);
  4501.  
  4502.     if (!WINDOWP (window))
  4503.       {
  4504.         window = XCONS (event->frame_or_window)->car;
  4505.         posn = Qnil;
  4506.       }
  4507.     else
  4508.       {
  4509.         int pixcolumn, pixrow;
  4510.         column -= XINT (XWINDOW (window)->left);
  4511.         row -= XINT (XWINDOW (window)->top);
  4512.         glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
  4513.         XSETINT (event->x, pixcolumn);
  4514.         XSETINT (event->y, pixrow);
  4515.  
  4516.         if (part == 1)
  4517.           posn = Qmode_line;
  4518.         else if (part == 2)
  4519.           posn = Qvertical_line;
  4520.         else
  4521.           XSETINT (posn,
  4522.                buffer_posn_from_coords (XWINDOW (window),
  4523.                         column, row));
  4524.       }
  4525.  
  4526.     {
  4527.       Lisp_Object head, position;
  4528.  
  4529.       position
  4530.         = Fcons (window,
  4531.              Fcons (posn,
  4532.                 Fcons (Fcons (event->x, event->y),
  4533.                    Fcons (make_number (event->timestamp),
  4534.                       Qnil))));
  4535.  
  4536.       head = modify_event_symbol (0, event->modifiers,
  4537.                       Qdrag_n_drop, Qnil,
  4538.                       lispy_drag_n_drop_names,
  4539.                       &drag_n_drop_syms, 1);
  4540.       return Fcons (head,
  4541.             Fcons (position,
  4542.                    Fcons (files,
  4543.                       Qnil)));
  4544.     }
  4545.       }
  4546. #endif /* HAVE_MOUSE */
  4547.  
  4548. #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
  4549.     case menu_bar_event:
  4550.       /* The event value is in the cdr of the frame_or_window slot.  */
  4551.       if (!CONSP (event->frame_or_window))
  4552.     abort ();
  4553.       return XCONS (event->frame_or_window)->cdr;
  4554. #endif
  4555.  
  4556.       /* The 'kind' field of the event is something we don't recognize.  */
  4557.     default:
  4558.       abort ();
  4559.     }
  4560. }
  4561.  
  4562. #ifdef HAVE_MOUSE
  4563.  
  4564. static Lisp_Object
  4565. make_lispy_movement (frame, bar_window, part, x, y, time)
  4566.      FRAME_PTR frame;
  4567.      Lisp_Object bar_window;
  4568.      enum scroll_bar_part part;
  4569.      Lisp_Object x, y;
  4570.      unsigned long time;
  4571. {
  4572.   /* Is it a scroll bar movement?  */
  4573.   if (frame && ! NILP (bar_window))
  4574.     {
  4575.       Lisp_Object part_sym;
  4576.  
  4577.       part_sym = *scroll_bar_parts[(int) part];
  4578.       return Fcons (Qscroll_bar_movement,
  4579.             (Fcons (Fcons (bar_window,
  4580.                    Fcons (Qvertical_scroll_bar,
  4581.                       Fcons (Fcons (x, y),
  4582.                          Fcons (make_number (time),
  4583.                             Fcons (part_sym,
  4584.                                    Qnil))))),
  4585.                 Qnil)));
  4586.     }
  4587.  
  4588.   /* Or is it an ordinary mouse movement?  */
  4589.   else
  4590.     {
  4591.       int area;
  4592.       Lisp_Object window;
  4593.       Lisp_Object posn;
  4594.       int column, row;
  4595.  
  4596.       if (frame)
  4597.     {
  4598.       /* It's in a frame; which window on that frame?  */
  4599.       pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
  4600.                  NULL, 1);
  4601.       window = window_from_coordinates (frame, column, row, &area);
  4602.     }
  4603.       else
  4604.     window = Qnil;
  4605.  
  4606.       if (WINDOWP (window))
  4607.     {
  4608.       int pixcolumn, pixrow;
  4609.       column -= WINDOW_LEFT_MARGIN (XWINDOW (window));
  4610.       row -= XINT (XWINDOW (window)->top);
  4611.       glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
  4612.       XSETINT (x, pixcolumn);
  4613.       XSETINT (y, pixrow);
  4614.  
  4615.       if (area == 1)
  4616.         posn = Qmode_line;
  4617.       else if (area == 2)
  4618.         posn = Qvertical_line;
  4619.       else
  4620.         XSETINT (posn,
  4621.              buffer_posn_from_coords (XWINDOW (window), column, row));
  4622.     }
  4623.       else if (frame != 0)
  4624.     {
  4625.       XSETFRAME (window, frame);
  4626.       posn = Qnil;
  4627.     }
  4628.       else
  4629.     {
  4630.       window = Qnil;
  4631.       posn = Qnil;
  4632.       XSETFASTINT (x, 0);
  4633.       XSETFASTINT (y, 0);
  4634.     }
  4635.  
  4636.       return Fcons (Qmouse_movement,
  4637.             Fcons (Fcons (window,
  4638.                   Fcons (posn,
  4639.                      Fcons (Fcons (x, y),
  4640.                         Fcons (make_number (time),
  4641.                                Qnil)))),
  4642.                Qnil));
  4643.     }
  4644. }
  4645.  
  4646. #endif /* HAVE_MOUSE */
  4647.  
  4648. /* Construct a switch frame event.  */
  4649. static Lisp_Object
  4650. make_lispy_switch_frame (frame)
  4651.      Lisp_Object frame;
  4652. {
  4653.   return Fcons (Qswitch_frame, Fcons (frame, Qnil));
  4654. }
  4655.  
  4656. /* Manipulating modifiers.  */
  4657.  
  4658. /* Parse the name of SYMBOL, and return the set of modifiers it contains.
  4659.  
  4660.    If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
  4661.    SYMBOL's name of the end of the modifiers; the string from this
  4662.    position is the unmodified symbol name.
  4663.  
  4664.    This doesn't use any caches.  */
  4665.  
  4666. static int
  4667. parse_modifiers_uncached (symbol, modifier_end)
  4668.      Lisp_Object symbol;
  4669.      int *modifier_end;
  4670. {
  4671.   struct Lisp_String *name;
  4672.   int i;
  4673.   int modifiers;
  4674.  
  4675.   CHECK_SYMBOL (symbol, 1);
  4676.  
  4677.   modifiers = 0;
  4678.   name = XSYMBOL (symbol)->name;
  4679.  
  4680.   for (i = 0; i+2 <= STRING_BYTES (name); )
  4681.     {
  4682.       int this_mod_end = 0;
  4683.       int this_mod = 0;
  4684.  
  4685.       /* See if the name continues with a modifier word.
  4686.      Check that the word appears, but don't check what follows it.
  4687.      Set this_mod and this_mod_end to record what we find.  */
  4688.  
  4689.       switch (name->data[i])
  4690.     {
  4691. #define SINGLE_LETTER_MOD(BIT)                \
  4692.       (this_mod_end = i + 1, this_mod = BIT)
  4693.  
  4694.     case 'A':
  4695.       SINGLE_LETTER_MOD (alt_modifier);
  4696.       break;
  4697.  
  4698.     case 'C':
  4699.       SINGLE_LETTER_MOD (ctrl_modifier);
  4700.       break;
  4701.  
  4702.     case 'H':
  4703.       SINGLE_LETTER_MOD (hyper_modifier);
  4704.       break;
  4705.  
  4706.     case 'M':
  4707.       SINGLE_LETTER_MOD (meta_modifier);
  4708.       break;
  4709.  
  4710.     case 'S':
  4711.       SINGLE_LETTER_MOD (shift_modifier);
  4712.       break;
  4713.  
  4714.     case 's':
  4715.       SINGLE_LETTER_MOD (super_modifier);
  4716.       break;
  4717.  
  4718. #undef SINGLE_LETTER_MOD
  4719.     }
  4720.  
  4721.       /* If we found no modifier, stop looking for them.  */
  4722.       if (this_mod_end == 0)
  4723.     break;
  4724.  
  4725.       /* Check there is a dash after the modifier, so that it
  4726.      really is a modifier.  */
  4727.       if (this_mod_end >= STRING_BYTES (name)
  4728.       || name->data[this_mod_end] != '-')
  4729.     break;
  4730.  
  4731.       /* This modifier is real; look for another.  */
  4732.       modifiers |= this_mod;
  4733.       i = this_mod_end + 1;
  4734.     }
  4735.  
  4736.   /* Should we include the `click' modifier?  */
  4737.   if (! (modifiers & (down_modifier | drag_modifier
  4738.               | double_modifier | triple_modifier))
  4739.       && i + 7 == STRING_BYTES (name)
  4740.       && strncmp (name->data + i, "mouse-", 6) == 0
  4741.       && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
  4742.     modifiers |= click_modifier;
  4743.  
  4744.   if (modifier_end)
  4745.     *modifier_end = i;
  4746.  
  4747.   return modifiers;
  4748. }
  4749.  
  4750. /* Return a symbol whose name is the modifier prefixes for MODIFIERS
  4751.    prepended to the string BASE[0..BASE_LEN-1].
  4752.    This doesn't use any caches.  */
  4753. static Lisp_Object
  4754. apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
  4755.      int modifiers;
  4756.      char *base;
  4757.      int base_len, base_len_byte;
  4758. {
  4759.   /* Since BASE could contain nulls, we can't use intern here; we have
  4760.      to use Fintern, which expects a genuine Lisp_String, and keeps a
  4761.      reference to it.  */
  4762.   char *new_mods
  4763.     = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
  4764.   int mod_len;
  4765.  
  4766.   {
  4767.     char *p = new_mods;
  4768.  
  4769.     /* Only the event queue may use the `up' modifier; it should always
  4770.        be turned into a click or drag event before presented to lisp code.  */
  4771.     if (modifiers & up_modifier)
  4772.       abort ();
  4773.  
  4774.     if (modifiers & alt_modifier)   { *p++ = 'A'; *p++ = '-'; }
  4775.     if (modifiers & ctrl_modifier)  { *p++ = 'C'; *p++ = '-'; }
  4776.     if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
  4777.     if (modifiers & meta_modifier)  { *p++ = 'M'; *p++ = '-'; }
  4778.     if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
  4779.     if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
  4780.     if (modifiers & double_modifier)  { strcpy (p, "double-");  p += 7; }
  4781.     if (modifiers & triple_modifier)  { strcpy (p, "triple-");  p += 7; }
  4782.     if (modifiers & down_modifier)  { strcpy (p, "down-");  p += 5; }
  4783.     if (modifiers & drag_modifier)  { strcpy (p, "drag-");  p += 5; }
  4784.     /* The click modifier is denoted by the absence of other modifiers.  */
  4785.  
  4786.     *p = '\0';
  4787.  
  4788.     mod_len = p - new_mods;
  4789.   }
  4790.  
  4791.   {
  4792.     Lisp_Object new_name;
  4793.  
  4794.     new_name = make_uninit_multibyte_string (mod_len + base_len,
  4795.                          mod_len + base_len_byte);
  4796.     bcopy (new_mods, XSTRING (new_name)->data,           mod_len);
  4797.     bcopy (base,     XSTRING (new_name)->data + mod_len, base_len_byte);
  4798.  
  4799.     return Fintern (new_name, Qnil);
  4800.   }
  4801. }
  4802.  
  4803.  
  4804. static char *modifier_names[] =
  4805. {
  4806.   "up", "down", "drag", "click", "double", "triple", 0, 0,
  4807.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4808.   0, 0, "alt", "super", "hyper", "shift", "control", "meta"
  4809. };
  4810. #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
  4811.  
  4812. static Lisp_Object modifier_symbols;
  4813.  
  4814. /* Return the list of modifier symbols corresponding to the mask MODIFIERS.  */
  4815. static Lisp_Object
  4816. lispy_modifier_list (modifiers)
  4817.      int modifiers;
  4818. {
  4819.   Lisp_Object modifier_list;
  4820.   int i;
  4821.  
  4822.   modifier_list = Qnil;
  4823.   for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
  4824.     if (modifiers & (1<<i))
  4825.       modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
  4826.                  modifier_list);
  4827.  
  4828.   return modifier_list;
  4829. }
  4830.  
  4831.  
  4832. /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
  4833.    where UNMODIFIED is the unmodified form of SYMBOL,
  4834.    MASK is the set of modifiers present in SYMBOL's name.
  4835.    This is similar to parse_modifiers_uncached, but uses the cache in
  4836.    SYMBOL's Qevent_symbol_element_mask property, and maintains the
  4837.    Qevent_symbol_elements property.  */
  4838.  
  4839. static Lisp_Object
  4840. parse_modifiers (symbol)
  4841.      Lisp_Object symbol;
  4842. {
  4843.   Lisp_Object elements;
  4844.  
  4845.   elements = Fget (symbol, Qevent_symbol_element_mask);
  4846.   if (CONSP (elements))
  4847.     return elements;
  4848.   else
  4849.     {
  4850.       int end;
  4851.       int modifiers = parse_modifiers_uncached (symbol, &end);
  4852.       Lisp_Object unmodified;
  4853.       Lisp_Object mask;
  4854.  
  4855.       unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
  4856.                      STRING_BYTES (XSYMBOL (symbol)->name) - end),
  4857.                 Qnil);
  4858.  
  4859.       if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
  4860.     abort ();
  4861.       XSETFASTINT (mask, modifiers);
  4862.       elements = Fcons (unmodified, Fcons (mask, Qnil));
  4863.  
  4864.       /* Cache the parsing results on SYMBOL.  */
  4865.       Fput (symbol, Qevent_symbol_element_mask,
  4866.         elements);
  4867.       Fput (symbol, Qevent_symbol_elements,
  4868.         Fcons (unmodified, lispy_modifier_list (modifiers)));
  4869.  
  4870.       /* Since we know that SYMBOL is modifiers applied to unmodified,
  4871.      it would be nice to put that in unmodified's cache.
  4872.      But we can't, since we're not sure that parse_modifiers is
  4873.      canonical.  */
  4874.  
  4875.       return elements;
  4876.     }
  4877. }
  4878.  
  4879. /* Apply the modifiers MODIFIERS to the symbol BASE.
  4880.    BASE must be unmodified.
  4881.  
  4882.    This is like apply_modifiers_uncached, but uses BASE's
  4883.    Qmodifier_cache property, if present.  It also builds
  4884.    Qevent_symbol_elements properties, since it has that info anyway.
  4885.  
  4886.    apply_modifiers copies the value of BASE's Qevent_kind property to
  4887.    the modified symbol.  */
  4888. static Lisp_Object
  4889. apply_modifiers (modifiers, base)
  4890.      int modifiers;
  4891.      Lisp_Object base;
  4892. {
  4893.   Lisp_Object cache, index, entry, new_symbol;
  4894.  
  4895.   /* Mask out upper bits.  We don't know where this value's been.  */
  4896.   modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
  4897.  
  4898.   /* The click modifier never figures into cache indices.  */
  4899.   cache = Fget (base, Qmodifier_cache);
  4900.   XSETFASTINT (index, (modifiers & ~click_modifier));
  4901.   entry = assq_no_quit (index, cache);
  4902.  
  4903.   if (CONSP (entry))
  4904.     new_symbol = XCONS (entry)->cdr;
  4905.   else
  4906.     {
  4907.       /* We have to create the symbol ourselves.  */
  4908.       new_symbol = apply_modifiers_uncached (modifiers,
  4909.                          XSYMBOL (base)->name->data,
  4910.                          XSYMBOL (base)->name->size,
  4911.                          STRING_BYTES (XSYMBOL (base)->name));
  4912.  
  4913.       /* Add the new symbol to the base's cache.  */
  4914.       entry = Fcons (index, new_symbol);
  4915.       Fput (base, Qmodifier_cache, Fcons (entry, cache));
  4916.  
  4917.       /* We have the parsing info now for free, so add it to the caches.  */
  4918.       XSETFASTINT (index, modifiers);
  4919.       Fput (new_symbol, Qevent_symbol_element_mask,
  4920.         Fcons (base, Fcons (index, Qnil)));
  4921.       Fput (new_symbol, Qevent_symbol_elements,
  4922.         Fcons (base, lispy_modifier_list (modifiers)));
  4923.     }
  4924.  
  4925.   /* Make sure this symbol is of the same kind as BASE.
  4926.  
  4927.      You'd think we could just set this once and for all when we
  4928.      intern the symbol above, but reorder_modifiers may call us when
  4929.      BASE's property isn't set right; we can't assume that just
  4930.      because it has a Qmodifier_cache property it must have its
  4931.      Qevent_kind set right as well.  */
  4932.   if (NILP (Fget (new_symbol, Qevent_kind)))
  4933.     {
  4934.       Lisp_Object kind;
  4935.  
  4936.       kind = Fget (base, Qevent_kind);
  4937.       if (! NILP (kind))
  4938.     Fput (new_symbol, Qevent_kind, kind);
  4939.     }
  4940.  
  4941.   return new_symbol;
  4942. }
  4943.  
  4944.  
  4945. /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
  4946.    return a symbol with the modifiers placed in the canonical order.
  4947.    Canonical order is alphabetical, except for down and drag, which
  4948.    always come last.  The 'click' modifier is never written out.
  4949.  
  4950.    Fdefine_key calls this to make sure that (for example) C-M-foo
  4951.    and M-C-foo end up being equivalent in the keymap.  */
  4952.  
  4953. Lisp_Object
  4954. reorder_modifiers (symbol)
  4955.      Lisp_Object symbol;
  4956. {
  4957.   /* It's hopefully okay to write the code this way, since everything
  4958.      will soon be in caches, and no consing will be done at all.  */
  4959.   Lisp_Object parsed;
  4960.  
  4961.   parsed = parse_modifiers (symbol);
  4962.   return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
  4963.               XCONS (parsed)->car);
  4964. }
  4965.  
  4966.  
  4967. /* For handling events, we often want to produce a symbol whose name
  4968.    is a series of modifier key prefixes ("M-", "C-", etcetera) attached
  4969.    to some base, like the name of a function key or mouse button.
  4970.    modify_event_symbol produces symbols of this sort.
  4971.  
  4972.    NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
  4973.    is the name of the i'th symbol.  TABLE_SIZE is the number of elements
  4974.    in the table.
  4975.  
  4976.    Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
  4977.    NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
  4978.  
  4979.    SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
  4980.    persist between calls to modify_event_symbol that it can use to
  4981.    store a cache of the symbols it's generated for this NAME_TABLE
  4982.    before.  The object stored there may be a vector or an alist.
  4983.  
  4984.    SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
  4985.  
  4986.    MODIFIERS is a set of modifier bits (as given in struct input_events)
  4987.    whose prefixes should be applied to the symbol name.
  4988.  
  4989.    SYMBOL_KIND is the value to be placed in the event_kind property of
  4990.    the returned symbol.
  4991.  
  4992.    The symbols we create are supposed to have an
  4993.    `event-symbol-elements' property, which lists the modifiers present
  4994.    in the symbol's name.  */
  4995.  
  4996. static Lisp_Object
  4997. modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
  4998.                      name_table, symbol_table, table_size)
  4999.      int symbol_num;
  5000.      unsigned modifiers;
  5001.      Lisp_Object symbol_kind;
  5002.      Lisp_Object name_alist;
  5003.      char **name_table;
  5004.      Lisp_Object *symbol_table;
  5005.      unsigned int table_size;
  5006. {
  5007.   Lisp_Object value;
  5008.   Lisp_Object symbol_int;
  5009.  
  5010.   /* Get rid of the "vendor-specific" bit here.  */
  5011.   XSETINT (symbol_int, symbol_num & 0xffffff);
  5012.  
  5013.   /* Is this a request for a valid symbol?  */
  5014.   if (symbol_num < 0 || symbol_num >= table_size)
  5015.     return Qnil;
  5016.  
  5017.   if (CONSP (*symbol_table))
  5018.     value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
  5019.  
  5020.   /* If *symbol_table doesn't seem to be initialized properly, fix that.
  5021.      *symbol_table should be a lisp vector TABLE_SIZE elements long,
  5022.      where the Nth element is the symbol for NAME_TABLE[N], or nil if
  5023.      we've never used that symbol before.  */
  5024.   else
  5025.     {
  5026.       if (! VECTORP (*symbol_table)
  5027.       || XVECTOR (*symbol_table)->size != table_size)
  5028.     {
  5029.       Lisp_Object size;
  5030.  
  5031.       XSETFASTINT (size, table_size);
  5032.       *symbol_table = Fmake_vector (size, Qnil);
  5033.     }
  5034.  
  5035.       value = XVECTOR (*symbol_table)->contents[symbol_num];
  5036.     }
  5037.  
  5038.   /* Have we already used this symbol before?  */
  5039.   if (NILP (value))
  5040.     {
  5041.       /* No; let's create it.  */
  5042.       if (!NILP (name_alist))
  5043.     value = Fcdr_safe (Fassq (symbol_int, name_alist));
  5044.       else if (name_table != 0 && name_table[symbol_num])
  5045.     value = intern (name_table[symbol_num]);
  5046.  
  5047. #ifdef HAVE_WINDOW_SYSTEM
  5048.       if (NILP (value))
  5049.     {
  5050.       char *name = x_get_keysym_name (symbol_num);
  5051.       if (name)
  5052.         value = intern (name);
  5053.     }
  5054. #endif
  5055.  
  5056.       if (NILP (value))
  5057.     {
  5058.       char buf[20];
  5059.       sprintf (buf, "key-%d", symbol_num);
  5060.       value = intern (buf);
  5061.     }
  5062.  
  5063.       if (CONSP (*symbol_table))
  5064.         *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
  5065.       else
  5066.     XVECTOR (*symbol_table)->contents[symbol_num] = value;
  5067.  
  5068.       /* Fill in the cache entries for this symbol; this also
  5069.      builds the Qevent_symbol_elements property, which the user
  5070.      cares about.  */
  5071.       apply_modifiers (modifiers & click_modifier, value);
  5072.       Fput (value, Qevent_kind, symbol_kind);
  5073.     }
  5074.  
  5075.   /* Apply modifiers to that symbol.  */
  5076.   return apply_modifiers (modifiers, value);
  5077. }
  5078.  
  5079. /* Convert a list that represents an event type,
  5080.    such as (ctrl meta backspace), into the usual representation of that
  5081.    event type as a number or a symbol.  */
  5082.  
  5083. DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
  5084.   "Convert the event description list EVENT-DESC to an event type.\n\
  5085. EVENT-DESC should contain one base event type (a character or symbol)\n\
  5086. and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
  5087. drag, down, double or triple).  The base must be last.\n\
  5088. The return value is an event type (a character or symbol) which\n\
  5089. has the same base event type and all the specified modifiers.")
  5090.   (event_desc)
  5091.      Lisp_Object event_desc;
  5092. {
  5093.   Lisp_Object base;
  5094.   int modifiers = 0;
  5095.   Lisp_Object rest;
  5096.  
  5097.   base = Qnil;
  5098.   rest = event_desc;
  5099.   while (CONSP (rest))
  5100.     {
  5101.       Lisp_Object elt;
  5102.       int this = 0;
  5103.  
  5104.       elt = XCONS (rest)->car;
  5105.       rest = XCONS (rest)->cdr;
  5106.  
  5107.       /* Given a symbol, see if it is a modifier name.  */
  5108.       if (SYMBOLP (elt) && CONSP (rest))
  5109.     this = parse_solitary_modifier (elt);
  5110.  
  5111.       if (this != 0)
  5112.     modifiers |= this;
  5113.       else if (!NILP (base))
  5114.     error ("Two bases given in one event");
  5115.       else
  5116.     base = elt;
  5117.  
  5118.     }
  5119.  
  5120.   /* Let the symbol A refer to the character A.  */
  5121.   if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
  5122.     XSETINT (base, XSYMBOL (base)->name->data[0]);
  5123.  
  5124.   if (INTEGERP (base))
  5125.     {
  5126.       /* Turn (shift a) into A.  */
  5127.       if ((modifiers & shift_modifier) != 0
  5128.       && (XINT (base) >= 'a' && XINT (base) <= 'z'))
  5129.     {
  5130.       XSETINT (base, XINT (base) - ('a' - 'A'));
  5131.       modifiers &= ~shift_modifier;
  5132.     }
  5133.  
  5134.       /* Turn (control a) into C-a.  */
  5135.       if (modifiers & ctrl_modifier)
  5136.     return make_number ((modifiers & ~ctrl_modifier)
  5137.                 | make_ctrl_char (XINT (base)));
  5138.       else
  5139.     return make_number (modifiers | XINT (base));
  5140.     }
  5141.   else if (SYMBOLP (base))
  5142.     return apply_modifiers (modifiers, base);
  5143.   else
  5144.     error ("Invalid base event");
  5145. }
  5146.  
  5147. /* Try to recognize SYMBOL as a modifier name.
  5148.    Return the modifier flag bit, or 0 if not recognized.  */
  5149.  
  5150. static int
  5151. parse_solitary_modifier (symbol)
  5152.      Lisp_Object symbol;
  5153. {
  5154.   struct Lisp_String *name = XSYMBOL (symbol)->name;
  5155.  
  5156.   switch (name->data[0])
  5157.     {
  5158. #define SINGLE_LETTER_MOD(BIT)                \
  5159.       if (STRING_BYTES (name) == 1)            \
  5160.     return BIT;
  5161.  
  5162. #define MULTI_LETTER_MOD(BIT, NAME, LEN)        \
  5163.       if (LEN == STRING_BYTES (name)            \
  5164.       && ! strncmp (name->data, NAME, LEN))        \
  5165.     return BIT;
  5166.  
  5167.     case 'A':
  5168.       SINGLE_LETTER_MOD (alt_modifier);
  5169.       break;
  5170.  
  5171.     case 'a':
  5172.       MULTI_LETTER_MOD (alt_modifier, "alt", 3);
  5173.       break;
  5174.  
  5175.     case 'C':
  5176.       SINGLE_LETTER_MOD (ctrl_modifier);
  5177.       break;
  5178.  
  5179.     case 'c':
  5180.       MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
  5181.       MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
  5182.       break;
  5183.  
  5184.     case 'H':
  5185.       SINGLE_LETTER_MOD (hyper_modifier);
  5186.       break;
  5187.  
  5188.     case 'h':
  5189.       MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
  5190.       break;
  5191.  
  5192.     case 'M':
  5193.       SINGLE_LETTER_MOD (meta_modifier);
  5194.       break;
  5195.  
  5196.     case 'm':
  5197.       MULTI_LETTER_MOD (meta_modifier, "meta", 4);
  5198.       break;
  5199.  
  5200.     case 'S':
  5201.       SINGLE_LETTER_MOD (shift_modifier);
  5202.       break;
  5203.  
  5204.     case 's':
  5205.       MULTI_LETTER_MOD (shift_modifier, "shift", 5);
  5206.       MULTI_LETTER_MOD (super_modifier, "super", 5);
  5207.       SINGLE_LETTER_MOD (super_modifier);
  5208.       break;
  5209.  
  5210.     case 'd':
  5211.       MULTI_LETTER_MOD (drag_modifier, "drag", 4);
  5212.       MULTI_LETTER_MOD (down_modifier, "down", 4);
  5213.       MULTI_LETTER_MOD (double_modifier, "double", 6);
  5214.       break;
  5215.  
  5216.     case 't':
  5217.       MULTI_LETTER_MOD (triple_modifier, "triple", 6);
  5218.       break;
  5219.  
  5220. #undef SINGLE_LETTER_MOD
  5221. #undef MULTI_LETTER_MOD
  5222.     }
  5223.  
  5224.   return 0;
  5225. }
  5226.  
  5227. /* Return 1 if EVENT is a list whose elements are all integers or symbols.
  5228.    Such a list is not valid as an event,
  5229.    but it can be a Lucid-style event type list.  */
  5230.  
  5231. int
  5232. lucid_event_type_list_p (object)
  5233.      Lisp_Object object;
  5234. {
  5235.   Lisp_Object tail;
  5236.  
  5237.   if (! CONSP (object))
  5238.     return 0;
  5239.  
  5240.   for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
  5241.     {
  5242.       Lisp_Object elt;
  5243.       elt = XCONS (tail)->car;
  5244.       if (! (INTEGERP (elt) || SYMBOLP (elt)))
  5245.     return 0;
  5246.     }
  5247.  
  5248.   return NILP (tail);
  5249. }
  5250.  
  5251. /* Store into *addr a value nonzero if terminal input chars are available.
  5252.    Serves the purpose of ioctl (0, FIONREAD, addr)
  5253.    but works even if FIONREAD does not exist.
  5254.    (In fact, this may actually read some input.)
  5255.  
  5256.    If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe.  */
  5257.  
  5258. static void
  5259. get_input_pending (addr, do_timers_now)
  5260.      int *addr;
  5261.      int do_timers_now;
  5262. {
  5263.   /* First of all, have we already counted some input?  */
  5264.   *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
  5265.  
  5266.   /* If input is being read as it arrives, and we have none, there is none.  */
  5267.   if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
  5268.     return;
  5269.  
  5270.   /* Try to read some input and see how much we get.  */
  5271.   gobble_input (0);
  5272.   *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
  5273. }
  5274.  
  5275. /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary.  */
  5276.  
  5277. void
  5278. gobble_input (expected)
  5279.      int expected;
  5280. {
  5281. #ifndef VMS
  5282. #ifdef SIGIO
  5283.   if (interrupt_input)
  5284.     {
  5285.       SIGMASKTYPE mask;
  5286.       mask = sigblock (sigmask (SIGIO));
  5287.       read_avail_input (expected);
  5288.       sigsetmask (mask);
  5289.     }
  5290.   else
  5291. #ifdef POLL_FOR_INPUT
  5292.   if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
  5293.     {
  5294.       SIGMASKTYPE mask;
  5295.       mask = sigblock (sigmask (SIGALRM));
  5296.       read_avail_input (expected);
  5297.       sigsetmask (mask);
  5298.     }
  5299.   else
  5300. #endif
  5301. #endif
  5302.     read_avail_input (expected);
  5303. #endif
  5304. }
  5305.  
  5306. /* Put a buffer_switch_event in the buffer
  5307.    so that read_key_sequence will notice the new current buffer.  */
  5308.  
  5309. void
  5310. record_asynch_buffer_change ()
  5311. {
  5312.   struct input_event event;
  5313.   Lisp_Object tem;
  5314.  
  5315.   event.kind = buffer_switch_event;
  5316.   event.frame_or_window = Qnil;
  5317.  
  5318. #ifdef subprocesses
  5319.   /* We don't need a buffer-switch event unless Emacs is waiting for input.
  5320.      The purpose of the event is to make read_key_sequence look up the
  5321.      keymaps again.  If we aren't in read_key_sequence, we don't need one,
  5322.      and the event could cause trouble by messing up (input-pending-p).  */
  5323.   tem = Fwaiting_for_user_input_p ();
  5324.   if (NILP (tem))
  5325.     return;
  5326. #else
  5327.   /* We never need these events if we have no asynchronous subprocesses.  */
  5328.   return;
  5329. #endif
  5330.  
  5331.   /* Make sure no interrupt happens while storing the event.  */
  5332. #ifdef SIGIO
  5333.   if (interrupt_input)
  5334.     {
  5335.       SIGMASKTYPE mask;
  5336.       mask = sigblock (sigmask (SIGIO));
  5337.       kbd_buffer_store_event (&event);
  5338.       sigsetmask (mask);
  5339.     }
  5340.   else
  5341. #endif
  5342.     {
  5343.       stop_polling ();
  5344.       kbd_buffer_store_event (&event);
  5345.       start_polling ();
  5346.     }
  5347. }
  5348.  
  5349. #ifndef VMS
  5350.  
  5351. /* Read any terminal input already buffered up by the system
  5352.    into the kbd_buffer, but do not wait.
  5353.  
  5354.    EXPECTED should be nonzero if the caller knows there is some input.
  5355.  
  5356.    Except on VMS, all input is read by this function.
  5357.    If interrupt_input is nonzero, this function MUST be called
  5358.    only when SIGIO is blocked.
  5359.  
  5360.    Returns the number of keyboard chars read, or -1 meaning
  5361.    this is a bad time to try to read input.  */
  5362.  
  5363. static int
  5364. read_avail_input (expected)
  5365.      int expected;
  5366. {
  5367.   struct input_event buf[KBD_BUFFER_SIZE];
  5368.   register int i;
  5369.   int nread;
  5370.  
  5371.   if (read_socket_hook)
  5372.     /* No need for FIONREAD or fcntl; just say don't wait.  */
  5373.     nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
  5374.   else
  5375.     {
  5376.       /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
  5377.      the kbd_buffer can really hold.  That may prevent loss
  5378.      of characters on some systems when input is stuffed at us.  */
  5379.       unsigned char cbuf[KBD_BUFFER_SIZE - 1];
  5380.       int n_to_read;
  5381.  
  5382.       /* Determine how many characters we should *try* to read.  */
  5383. #ifdef WINDOWSNT
  5384.       return 0;
  5385. #else /* not WINDOWSNT */
  5386. #ifdef MSDOS
  5387.       n_to_read = dos_keysns ();
  5388.       if (n_to_read == 0)
  5389.     return 0;
  5390. #else /* not MSDOS */
  5391. #ifdef FIONREAD
  5392.       /* Find out how much input is available.  */
  5393.       if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
  5394.     /* Formerly simply reported no input, but that sometimes led to
  5395.        a failure of Emacs to terminate.
  5396.        SIGHUP seems appropriate if we can't reach the terminal.  */
  5397.     /* ??? Is it really right to send the signal just to this process
  5398.        rather than to the whole process group?
  5399.        Perhaps on systems with FIONREAD Emacs is alone in its group.  */
  5400.     kill (getpid (), SIGHUP);
  5401.       if (n_to_read == 0)
  5402.     return 0;
  5403.       if (n_to_read > sizeof cbuf)
  5404.     n_to_read = sizeof cbuf;
  5405. #else /* no FIONREAD */
  5406. #if defined (USG) || defined (DGUX)
  5407.       /* Read some input if available, but don't wait.  */
  5408.       n_to_read = sizeof cbuf;
  5409.       fcntl (input_fd, F_SETFL, O_NDELAY);
  5410. #else
  5411.       you lose;
  5412. #endif
  5413. #endif
  5414. #endif /* not MSDOS */
  5415. #endif /* not WINDOWSNT */
  5416.  
  5417.       /* Now read; for one reason or another, this will not block.
  5418.      NREAD is set to the number of chars read.  */
  5419.       do
  5420.     {
  5421. #ifdef MSDOS
  5422.       cbuf[0] = dos_keyread ();
  5423.       nread = 1;
  5424. #else
  5425.       nread = read (input_fd, cbuf, n_to_read);
  5426. #endif
  5427.       /* POSIX infers that processes which are not in the session leader's
  5428.          process group won't get SIGHUP's at logout time.  BSDI adheres to
  5429.          this part standard and returns -1 from read (0) with errno==EIO
  5430.          when the control tty is taken away.
  5431.          Jeffrey Honig <jch@bsdi.com> says this is generally safe.  */
  5432.       if (nread == -1 && errno == EIO)
  5433.         kill (0, SIGHUP);
  5434. #if defined (AIX) && (! defined (aix386) && defined (_BSD))
  5435.       /* The kernel sometimes fails to deliver SIGHUP for ptys.
  5436.          This looks incorrect, but it isn't, because _BSD causes
  5437.          O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
  5438.          and that causes a value other than 0 when there is no input.  */
  5439.       if (nread == 0)
  5440.         kill (0, SIGHUP);
  5441. #endif
  5442.     }
  5443.       while (
  5444.          /* We used to retry the read if it was interrupted.
  5445.         But this does the wrong thing when O_NDELAY causes
  5446.         an EAGAIN error.  Does anybody know of a situation
  5447.         where a retry is actually needed?  */
  5448. #if 0
  5449.          nread < 0 && (errno == EAGAIN
  5450. #ifdef EFAULT
  5451.                || errno == EFAULT
  5452. #endif
  5453. #ifdef EBADSLT
  5454.                || errno == EBADSLT
  5455. #endif
  5456.                )
  5457. #else
  5458.          0
  5459. #endif
  5460.          );
  5461.  
  5462. #ifndef FIONREAD
  5463. #if defined (USG) || defined (DGUX)
  5464.       fcntl (input_fd, F_SETFL, 0);
  5465. #endif /* USG or DGUX */
  5466. #endif /* no FIONREAD */
  5467.       for (i = 0; i < nread; i++)
  5468.     {
  5469.       buf[i].kind = ascii_keystroke;
  5470.       buf[i].modifiers = 0;
  5471.       if (meta_key == 1 && (cbuf[i] & 0x80))
  5472.         buf[i].modifiers = meta_modifier;
  5473.       if (meta_key != 2)
  5474.         cbuf[i] &= ~0x80;
  5475.  
  5476.       buf[i].code = cbuf[i];
  5477.       XSETFRAME (buf[i].frame_or_window, selected_frame);
  5478.     }
  5479.     }
  5480.  
  5481.   /* Scan the chars for C-g and store them in kbd_buffer.  */
  5482.   for (i = 0; i < nread; i++)
  5483.     {
  5484.       kbd_buffer_store_event (&buf[i]);
  5485.       /* Don't look at input that follows a C-g too closely.
  5486.      This reduces lossage due to autorepeat on C-g.  */
  5487.       if (buf[i].kind == ascii_keystroke
  5488.       && buf[i].code == quit_char)
  5489.     break;
  5490.     }
  5491.  
  5492.   return nread;
  5493. }
  5494. #endif /* not VMS */
  5495.  
  5496. #ifdef SIGIO   /* for entire page */
  5497. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  5498.  
  5499. SIGTYPE
  5500. input_available_signal (signo)
  5501.      int signo;
  5502. {
  5503.   /* Must preserve main program's value of errno.  */
  5504.   int old_errno = errno;
  5505. #ifdef BSD4_1
  5506.   extern int select_alarmed;
  5507. #endif
  5508.  
  5509. #if defined (USG) && !defined (POSIX_SIGNALS)
  5510.   /* USG systems forget handlers when they are used;
  5511.      must reestablish each time */
  5512.   signal (signo, input_available_signal);
  5513. #endif /* USG */
  5514.  
  5515. #ifdef BSD4_1
  5516.   sigisheld (SIGIO);
  5517. #endif
  5518.  
  5519.   if (input_available_clear_time)
  5520.     EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
  5521.  
  5522.   while (1)
  5523.     {
  5524.       int nread;
  5525.       nread = read_avail_input (1);
  5526.       /* -1 means it's not ok to read the input now.
  5527.      UNBLOCK_INPUT will read it later; now, avoid infinite loop.
  5528.      0 means there was no keyboard input available.  */
  5529.       if (nread <= 0)
  5530.     break;
  5531.  
  5532. #ifdef BSD4_1
  5533.       select_alarmed = 1;  /* Force the select emulator back to life */
  5534. #endif
  5535.     }
  5536.  
  5537. #ifdef BSD4_1
  5538.   sigfree ();
  5539. #endif
  5540.   errno = old_errno;
  5541. }
  5542. #endif /* SIGIO */
  5543.  
  5544. /* Send ourselves a SIGIO.
  5545.  
  5546.    This function exists so that the UNBLOCK_INPUT macro in
  5547.    blockinput.h can have some way to take care of input we put off
  5548.    dealing with, without assuming that every file which uses
  5549.    UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
  5550. void
  5551. reinvoke_input_signal ()
  5552. {
  5553. #ifdef HAVE_PM
  5554.   while (read_avail_input (1) > 0)
  5555.     ;
  5556. #endif /* HAVE_PM */
  5557. #ifdef SIGIO
  5558.   kill (getpid (), SIGIO);
  5559. #endif
  5560. }
  5561.  
  5562.  
  5563.  
  5564. /* Return the prompt-string of a sparse keymap.
  5565.    This is the first element which is a string.
  5566.    Return nil if there is none.  */
  5567.  
  5568. Lisp_Object
  5569. map_prompt (map)
  5570.      Lisp_Object map;
  5571. {
  5572.   while (CONSP (map))
  5573.     {
  5574.       register Lisp_Object tem;
  5575.       tem = Fcar (map);
  5576.       if (STRINGP (tem))
  5577.     return tem;
  5578.       map = Fcdr (map);
  5579.     }
  5580.   return Qnil;
  5581. }
  5582.  
  5583. static void menu_bar_item ();
  5584. static void menu_bar_one_keymap ();
  5585.  
  5586. /* These variables hold the vector under construction within
  5587.    menu_bar_items and its subroutines, and the current index
  5588.    for storing into that vector.  */
  5589. static Lisp_Object menu_bar_items_vector;
  5590. static int menu_bar_items_index;
  5591.  
  5592. /* Return a vector of menu items for a menu bar, appropriate
  5593.    to the current buffer.  Each item has three elements in the vector:
  5594.    KEY STRING MAPLIST.
  5595.  
  5596.    OLD is an old vector we can optionally reuse, or nil.  */
  5597.  
  5598. Lisp_Object
  5599. menu_bar_items (old)
  5600.      Lisp_Object old;
  5601. {
  5602.   /* The number of keymaps we're scanning right now, and the number of
  5603.      keymaps we have allocated space for.  */
  5604.   int nmaps;
  5605.  
  5606.   /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
  5607.      in the current keymaps, or nil where it is not a prefix.  */
  5608.   Lisp_Object *maps;
  5609.  
  5610.   Lisp_Object def, tem, tail;
  5611.  
  5612.   Lisp_Object result;
  5613.  
  5614.   int mapno;
  5615.   Lisp_Object oquit;
  5616.  
  5617.   int i;
  5618.  
  5619.   struct gcpro gcpro1;
  5620.  
  5621.   /* In order to build the menus, we need to call the keymap
  5622.      accessors.  They all call QUIT.  But this function is called
  5623.      during redisplay, during which a quit is fatal.  So inhibit
  5624.      quitting while building the menus.
  5625.      We do this instead of specbind because (1) errors will clear it anyway
  5626.      and (2) this avoids risk of specpdl overflow.  */
  5627.   oquit = Vinhibit_quit;
  5628.   Vinhibit_quit = Qt;
  5629.  
  5630.   if (!NILP (old))
  5631.     menu_bar_items_vector = old;
  5632.   else
  5633.     menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
  5634.   menu_bar_items_index = 0;
  5635.  
  5636.   GCPRO1 (menu_bar_items_vector);
  5637.  
  5638.   /* Build our list of keymaps.
  5639.      If we recognize a function key and replace its escape sequence in
  5640.      keybuf with its symbol, or if the sequence starts with a mouse
  5641.      click and we need to switch buffers, we jump back here to rebuild
  5642.      the initial keymaps from the current buffer.  */
  5643.   {
  5644.     Lisp_Object *tmaps;
  5645.  
  5646.     /* Should overriding-terminal-local-map and overriding-local-map apply?  */
  5647.     if (!NILP (Voverriding_local_map_menu_flag))
  5648.       {
  5649.     /* Yes, use them (if non-nil) as well as the global map.  */
  5650.     maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
  5651.     nmaps = 0;
  5652.     if (!NILP (current_kboard->Voverriding_terminal_local_map))
  5653.       maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
  5654.     if (!NILP (Voverriding_local_map))
  5655.       maps[nmaps++] = Voverriding_local_map;
  5656.       }
  5657.     else
  5658.       {
  5659.     /* No, so use major and minor mode keymaps.  */
  5660.     nmaps = current_minor_maps (NULL, &tmaps);
  5661.     maps = (Lisp_Object *) alloca ((nmaps + 2) * sizeof (maps[0]));
  5662.     bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
  5663. #ifdef USE_TEXT_PROPERTIES
  5664.     maps[nmaps++] = get_local_map (PT, current_buffer);
  5665. #else
  5666.     maps[nmaps++] = current_buffer->keymap;
  5667. #endif
  5668.       }
  5669.     maps[nmaps++] = current_global_map;
  5670.   }
  5671.  
  5672.   /* Look up in each map the dummy prefix key `menu-bar'.  */
  5673.  
  5674.   result = Qnil;
  5675.  
  5676.   for (mapno = nmaps - 1; mapno >= 0; mapno--)
  5677.     {
  5678.       if (! NILP (maps[mapno]))
  5679.     def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0), 0);
  5680.       else
  5681.     def = Qnil;
  5682.  
  5683.       tem = Fkeymapp (def);
  5684.       if (!NILP (tem))
  5685.     menu_bar_one_keymap (def);
  5686.     }
  5687.  
  5688.   /* Move to the end those items that should be at the end.  */
  5689.  
  5690.   for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
  5691.     {
  5692.       int i;
  5693.       int end = menu_bar_items_index;
  5694.  
  5695.       for (i = 0; i < end; i += 4)
  5696.     if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
  5697.       {
  5698.         Lisp_Object tem0, tem1, tem2, tem3;
  5699.         /* Move the item at index I to the end,
  5700.            shifting all the others forward.  */
  5701.         tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
  5702.         tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
  5703.         tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
  5704.         tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
  5705.         if (end > i + 4)
  5706.           bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
  5707.              &XVECTOR (menu_bar_items_vector)->contents[i],
  5708.              (end - i - 4) * sizeof (Lisp_Object));
  5709.         XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
  5710.         XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
  5711.         XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
  5712.         XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
  5713.         break;
  5714.       }
  5715.     }
  5716.  
  5717.   /* Add nil, nil, nil, nil at the end.  */
  5718.   i = menu_bar_items_index;
  5719.   if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
  5720.     {
  5721.       Lisp_Object tem;
  5722.       int newsize = 2 * i;
  5723.       tem = Fmake_vector (make_number (2 * i), Qnil);
  5724.       bcopy (XVECTOR (menu_bar_items_vector)->contents,
  5725.          XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
  5726.       menu_bar_items_vector = tem;
  5727.     }
  5728.   /* Add this item.  */
  5729.   XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
  5730.   XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
  5731.   XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
  5732.   XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
  5733.   menu_bar_items_index = i;
  5734.  
  5735.   Vinhibit_quit = oquit;
  5736.   UNGCPRO;
  5737.   return menu_bar_items_vector;
  5738. }
  5739.  
  5740. /* Scan one map KEYMAP, accumulating any menu items it defines
  5741.    in menu_bar_items_vector.  */
  5742.  
  5743. static void
  5744. menu_bar_one_keymap (keymap)
  5745.      Lisp_Object keymap;
  5746. {
  5747.   Lisp_Object tail, item, table;
  5748.  
  5749.   /* Loop over all keymap entries that have menu strings.  */
  5750.   for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
  5751.     {
  5752.       item = XCONS (tail)->car;
  5753.       if (CONSP (item))
  5754.     menu_bar_item (XCONS (item)->car, XCONS (item)->cdr);
  5755.       else if (VECTORP (item))
  5756.     {
  5757.       /* Loop over the char values represented in the vector.  */
  5758.       int len = XVECTOR (item)->size;
  5759.       int c;
  5760.       for (c = 0; c < len; c++)
  5761.         {
  5762.           Lisp_Object character;
  5763.           XSETFASTINT (character, c);
  5764.           menu_bar_item (character, XVECTOR (item)->contents[c]);
  5765.         }
  5766.     }
  5767.     }
  5768. }
  5769.  
  5770. /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
  5771.    If there's already an item for KEY, add this DEF to it.  */
  5772.  
  5773. Lisp_Object item_properties;
  5774.  
  5775. static void
  5776. menu_bar_item (key, item)
  5777.      Lisp_Object key, item;
  5778. {
  5779.   struct gcpro gcpro1;
  5780.   int i;
  5781.  
  5782.   if (EQ (item, Qundefined))
  5783.     {
  5784.       /* If a map has an explicit `undefined' as definition,
  5785.      discard any previously made menu bar item.  */
  5786.  
  5787.       for (i = 0; i < menu_bar_items_index; i += 4)
  5788.     if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
  5789.       {
  5790.         if (menu_bar_items_index > i + 4)
  5791.           bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
  5792.              &XVECTOR (menu_bar_items_vector)->contents[i],
  5793.              (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
  5794.         menu_bar_items_index -= 4;
  5795.         return;
  5796.       }
  5797.  
  5798.       /* If there's no definition for this key yet,
  5799.      just ignore `undefined'.  */
  5800.       return;
  5801.     }
  5802.  
  5803.   GCPRO1 (key);            /* Is this necessary? */
  5804.   i = parse_menu_item (item, 0, 1);
  5805.   UNGCPRO;
  5806.   if (!i)
  5807.     return;
  5808.  
  5809.   item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
  5810.  
  5811.   /* Find any existing item for this KEY.  */
  5812.   for (i = 0; i < menu_bar_items_index; i += 4)
  5813.     if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
  5814.       break;
  5815.  
  5816.   /* If we did not find this KEY, add it at the end.  */
  5817.   if (i == menu_bar_items_index)
  5818.     {
  5819.       /* If vector is too small, get a bigger one.  */
  5820.       if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
  5821.     {
  5822.       Lisp_Object tem;
  5823.       int newsize = 2 * i;
  5824.       tem = Fmake_vector (make_number (2 * i), Qnil);
  5825.       bcopy (XVECTOR (menu_bar_items_vector)->contents,
  5826.          XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
  5827.       menu_bar_items_vector = tem;
  5828.     }
  5829.  
  5830.       /* Add this item.  */
  5831.       XVECTOR (menu_bar_items_vector)->contents[i++] = key;
  5832.       XVECTOR (menu_bar_items_vector)->contents[i++]
  5833.     = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
  5834.       XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
  5835.       XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
  5836.       menu_bar_items_index = i;
  5837.     }
  5838.   /* We did find an item for this KEY.  Add ITEM to its list of maps.  */
  5839.   else
  5840.     {
  5841.       Lisp_Object old;
  5842.       old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
  5843.       XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
  5844.     }
  5845. }
  5846.  
  5847.  /* This is used as the handler when calling menu_item_eval_property.  */
  5848. static Lisp_Object
  5849. menu_item_eval_property_1 (arg)
  5850.      Lisp_Object arg;
  5851. {
  5852.   /* If we got a quit from within the menu computation,
  5853.      quit all the way out of it.  This takes care of C-] in the debugger.  */
  5854.   if (CONSP (arg) && EQ (XCONS (arg)->car, Qquit))
  5855.     Fsignal (Qquit, Qnil);
  5856.  
  5857.   return Qnil;
  5858. }
  5859.  
  5860. /* Evaluate an expression and return the result (or nil if something 
  5861.    went wrong).  Used to evaluate dynamic parts of menu items.  */
  5862. static Lisp_Object
  5863. menu_item_eval_property (sexpr)
  5864.      Lisp_Object sexpr;
  5865. {
  5866.   int count = specpdl_ptr - specpdl;
  5867.   Lisp_Object val;
  5868.   specbind (Qinhibit_redisplay, Qt);
  5869.   val = internal_condition_case_1 (Feval, sexpr, Qerror,
  5870.                    menu_item_eval_property_1);
  5871.   return unbind_to (count, val);
  5872. }
  5873.  
  5874. /* This function parses a menu item and leaves the result in the
  5875.    vector item_properties.
  5876.    ITEM is a key binding, a possible menu item.
  5877.    If NOTREAL is nonzero, only check for equivalent key bindings, don't
  5878.    evaluate dynamic expressions in the menu item.
  5879.    INMENUBAR is > 0 when this is considered for an entry in a menu bar
  5880.    top level.
  5881.    INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
  5882.    parse_menu_item returns true if the item is a menu item and false
  5883.    otherwise.  */
  5884.  
  5885. int
  5886. parse_menu_item (item, notreal, inmenubar)
  5887.      Lisp_Object item;
  5888.      int notreal, inmenubar;
  5889. {
  5890.   Lisp_Object def, tem, item_string, start;
  5891.   Lisp_Object cachelist = Qnil;
  5892.   Lisp_Object filter = Qnil;
  5893.   Lisp_Object keyhint = Qnil;
  5894.   int i;
  5895.   int newcache = 0;
  5896.  
  5897.   if (!CONSP (item))
  5898.     return 0;
  5899.  
  5900.   /* Create item_properties vector if necessary.  */
  5901.   if (NILP (item_properties))
  5902.     item_properties
  5903.       = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
  5904.  
  5905.   /* Initialize optional entries.  */
  5906.   for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
  5907.     XVECTOR (item_properties)->contents[i] = Qnil;
  5908.   XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = Qt;
  5909.      
  5910.   /* Save the item here to protect it from GC.  */
  5911.   XVECTOR (item_properties)->contents[ITEM_PROPERTY_ITEM] = item;
  5912.  
  5913.   item_string = XCONS (item)->car;
  5914.  
  5915.   start = item;
  5916.   item = XCONS (item)->cdr;
  5917.   if (STRINGP (item_string))
  5918.     {
  5919.       /* Old format menu item.  */
  5920.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
  5921.  
  5922.       /* Maybe help string.  */
  5923.       if (CONSP (item) && STRINGP (XCONS (item)->car))
  5924.     {
  5925.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
  5926.         = XCONS (item)->car;
  5927.       start = item;
  5928.       item = XCONS (item)->cdr;
  5929.     }
  5930.       
  5931.       /* Maybee key binding cache.  */
  5932.       if (CONSP (item) && CONSP (XCONS (item)->car)
  5933.       && (NILP (XCONS (XCONS (item)->car)->car)
  5934.           || VECTORP (XCONS (XCONS (item)->car)->car)))
  5935.     {
  5936.       cachelist = XCONS (item)->car;
  5937.       item = XCONS (item)->cdr;
  5938.     }
  5939.       
  5940.       /* This is the real definition--the function to run.  */
  5941.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = item;
  5942.  
  5943.       /* Get enable property, if any.  */
  5944.       if (SYMBOLP (item))
  5945.     {
  5946.       tem = Fget (item, Qmenu_enable);
  5947.       if (!NILP (tem))
  5948.         XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
  5949.     }
  5950.     }
  5951.   else if (EQ (item_string, Qmenu_item) && CONSP (item))
  5952.     {
  5953.       /* New format menu item.  */
  5954.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME]
  5955.     = XCONS (item)->car;
  5956.       start = XCONS (item)->cdr;
  5957.       if (CONSP (start))
  5958.     {
  5959.       /* We have a real binding.  */
  5960.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF]
  5961.         = XCONS (start)->car;
  5962.  
  5963.       item = XCONS (start)->cdr;
  5964.       /* Is there a cache list with key equivalences. */
  5965.       if (CONSP (item) && CONSP (XCONS (item)->car))
  5966.         {
  5967.           cachelist = XCONS (item)->car;
  5968.           item = XCONS (item)->cdr;
  5969.         }
  5970.  
  5971.       /* Parse properties.  */
  5972.       while (CONSP (item) && CONSP (XCONS (item)->cdr))
  5973.         {
  5974.           tem = XCONS (item)->car;
  5975.           item = XCONS (item)->cdr;
  5976.  
  5977.           if (EQ (tem, QCenable))
  5978.         XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE]
  5979.           = XCONS (item)->car;
  5980.           else if (EQ (tem, QCvisible) && !notreal)
  5981.         {
  5982.           /* If got a visible property and that evaluates to nil
  5983.              then ignore this item.  */
  5984.           tem = menu_item_eval_property (XCONS (item)->car);
  5985.           if (NILP (tem))
  5986.             return 0;
  5987.          }
  5988.           else if (EQ (tem, QChelp))
  5989.         XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]
  5990.           = XCONS (item)->car;
  5991.           else if (EQ (tem, QCfilter))
  5992.         filter = item;
  5993.           else if (EQ (tem, QCkey_sequence))
  5994.         {
  5995.           tem = XCONS (item)->car;
  5996.           if (NILP (cachelist)
  5997.               && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
  5998.             /* Be GC protected. Set keyhint to item instead of tem. */
  5999.             keyhint = item;
  6000.         }
  6001.           else if (EQ (tem, QCkeys))
  6002.         {
  6003.           tem = XCONS (item)->car;
  6004.           if (CONSP (tem) || STRINGP (tem) && NILP (cachelist))
  6005.             XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ]
  6006.               = tem;
  6007.         }
  6008.           else if (EQ (tem, QCbutton) && CONSP (XCONS (item)->car))
  6009.         {
  6010.           Lisp_Object type;
  6011.           tem = XCONS (item)->car;
  6012.           type = XCONS (tem)->car;
  6013.           if (EQ (type, QCtoggle) || EQ (type, QCradio))
  6014.             {
  6015.               XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
  6016.             = XCONS (tem)->cdr;
  6017.               XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE]
  6018.             = type;
  6019.             }
  6020.         }
  6021.           item = XCONS (item)->cdr;
  6022.         }
  6023.     }
  6024.       else if (inmenubar || !NILP (start))
  6025.     return 0;
  6026.     }
  6027.   else
  6028.     return 0;            /* not a menu item */
  6029.  
  6030.   /* If item string is not a string, evaluate it to get string.
  6031.      If we don't get a string, skip this item.  */
  6032.   item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
  6033.   if (!(STRINGP (item_string) || notreal))
  6034.     {
  6035.       item_string = menu_item_eval_property (item_string);
  6036.       if (!STRINGP (item_string))
  6037.     return 0;
  6038.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME] = item_string;
  6039.     }
  6040.      
  6041.   /* If got a filter apply it on definition.  */
  6042.   def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
  6043.   if (!NILP (filter))
  6044.     {
  6045.       def = menu_item_eval_property (list2 (XCONS (filter)->car,
  6046.                         list2 (Qquote, def)));
  6047.  
  6048.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = def;
  6049.     }
  6050.  
  6051.   /* If we got no definition, this item is just unselectable text which
  6052.      is OK in a submenu but not in the menubar.  */
  6053.   if (NILP (def))
  6054.     return (inmenubar ? 0 : 1);
  6055.  
  6056.   /* Enable or disable selection of item.  */
  6057.   tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
  6058.   if (!EQ (tem, Qt))
  6059.     {
  6060.       if (notreal)
  6061.     tem = Qt;
  6062.       else
  6063.     tem = menu_item_eval_property (tem);
  6064.       if (inmenubar && NILP (tem))
  6065.     return 0;        /* Ignore disabled items in menu bar.  */
  6066.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE] = tem;
  6067.     }
  6068.  
  6069.   /* See if this is a separate pane or a submenu.  */
  6070.   def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
  6071.   tem = get_keymap_1 (def, 0, 1);
  6072.   if (!NILP (tem))
  6073.     {
  6074.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP] = tem;
  6075.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF] = tem;
  6076.       return 1;
  6077.     }
  6078.   else if (inmenubar > 0)
  6079.     return 0;            /* Entries in menu bar must be submenus.  */
  6080.  
  6081.   /* This is a command.  See if there is an equivalent key binding. */
  6082.   if (NILP (cachelist))
  6083.     {
  6084.       /* We have to create a cachelist.  */
  6085.       CHECK_IMPURE (start);
  6086.       XCONS (start)->cdr = Fcons (Fcons (Qnil, Qnil), XCONS (start)->cdr);
  6087.       cachelist = XCONS (XCONS (start)->cdr)->car;
  6088.       newcache = 1;
  6089.       tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
  6090.       if (!NILP (keyhint))
  6091.     {
  6092.       XCONS (cachelist)->car = XCONS (keyhint)->car;
  6093.       newcache = 0;
  6094.     }
  6095.       else if (STRINGP (tem))
  6096.     {
  6097.       XCONS (cachelist)->cdr = Fsubstitute_command_keys (tem);
  6098.       XCONS (cachelist)->car = Qt;
  6099.     }
  6100.     }
  6101.   tem = XCONS (cachelist)->car;
  6102.   if (!EQ (tem, Qt))
  6103.     {
  6104.       int chkcache = 0;
  6105.       Lisp_Object prefix;
  6106.  
  6107.       if (!NILP (tem))
  6108.     tem = Fkey_binding (tem, Qnil);
  6109.  
  6110.       prefix = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
  6111.       if (CONSP (prefix))
  6112.     {
  6113.       def = XCONS (prefix)->car;
  6114.       prefix = XCONS (prefix)->cdr;
  6115.     }
  6116.       else
  6117.     def = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
  6118.  
  6119.       if (NILP (XCONS (cachelist)->car)) /* Have no saved key.  */
  6120.     {
  6121.       if (newcache        /* Always check first time.  */
  6122.           /* Should we check everything when precomputing key
  6123.          bindings?  */
  6124.           /* || notreal */
  6125.           /* If something had no key binding before, don't recheck it
  6126.          because that is too slow--except if we have a list of
  6127.          rebound commands in Vdefine_key_rebound_commands, do
  6128.          recheck any command that appears in that list. */
  6129.           || (CONSP (Vdefine_key_rebound_commands)
  6130.           && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
  6131.         chkcache = 1;
  6132.     }
  6133.       /* We had a saved key. Is it still bound to the command?  */
  6134.       else if (NILP (tem)
  6135.            || !EQ (tem, def)
  6136.            /* If the command is an alias for another
  6137.           (such as lmenu.el set it up), check if the
  6138.           original command matches the cached command.  */
  6139.            && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function)))
  6140.     chkcache = 1;        /* Need to recompute key binding.  */
  6141.  
  6142.       if (chkcache)
  6143.     {
  6144.       /* Recompute equivalent key binding.  If the command is an alias
  6145.          for another (such as lmenu.el set it up), see if the original
  6146.          command name has equivalent keys.  Otherwise look up the
  6147.          specified command itself.  We don't try both, because that
  6148.          makes lmenu menus slow. */
  6149.       if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
  6150.           && ! NILP (Fget (def, Qmenu_alias)))
  6151.         def = XSYMBOL (def)->function;
  6152.       tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
  6153.       XCONS (cachelist)->car = tem;
  6154.       if (NILP (tem))
  6155.         {
  6156.           XCONS (cachelist)->cdr = Qnil;
  6157.           chkcache = 0;
  6158.         }
  6159.     }
  6160.       else if (!NILP (keyhint) && !NILP (XCONS (cachelist)->car))
  6161.     {
  6162.       tem = XCONS (cachelist)->car;
  6163.       chkcache = 1;
  6164.     }
  6165.  
  6166.       newcache = chkcache;
  6167.       if (chkcache)
  6168.     {
  6169.       tem = Fkey_description (tem);
  6170.       if (CONSP (prefix))
  6171.         {
  6172.           if (STRINGP (XCONS (prefix)->car))
  6173.         tem = concat2 (XCONS (prefix)->car, tem);
  6174.           if (STRINGP (XCONS (prefix)->cdr))
  6175.         tem = concat2 (tem, XCONS (prefix)->cdr);
  6176.         }
  6177.       XCONS (cachelist)->cdr = tem;
  6178.     }
  6179.     }
  6180.  
  6181.   tem = XCONS (cachelist)->cdr;
  6182.   if (newcache && !NILP (tem))
  6183.     {
  6184.       tem = concat3 (build_string ("  ("), tem, build_string (")"));
  6185.       XCONS (cachelist)->cdr = tem;
  6186.     }
  6187.  
  6188.   /* If we only want to precompute equivalent key bindings, stop here. */
  6189.   if (notreal)
  6190.     return 1;
  6191.  
  6192.   /* If we have an equivalent key binding, use that.  */
  6193.   XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ] = tem;
  6194.  
  6195.   /* Include this when menu help is implemented.
  6196.   tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
  6197.   if (!(NILP (tem) || STRINGP (tem)))
  6198.     {
  6199.       tem = menu_item_eval_property (tem);
  6200.       if (!STRINGP (tem))
  6201.     tem = Qnil;
  6202.       XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
  6203.     }
  6204.   */
  6205.  
  6206.   /* Handle radio buttons or toggle boxes.  */ 
  6207.   tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
  6208.   if (!NILP (tem))
  6209.     XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED]
  6210.       = menu_item_eval_property (tem);
  6211.  
  6212.   return 1;
  6213. }
  6214.  
  6215. /* Read a character using menus based on maps in the array MAPS.
  6216.    NMAPS is the length of MAPS.  Return nil if there are no menus in the maps.
  6217.    Return t if we displayed a menu but the user rejected it.
  6218.  
  6219.    PREV_EVENT is the previous input event, or nil if we are reading
  6220.    the first event of a key sequence.
  6221.  
  6222.    If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
  6223.    if we used a mouse menu to read the input, or zero otherwise.  If
  6224.    USED_MOUSE_MENU is null, we don't dereference it.
  6225.  
  6226.    The prompting is done based on the prompt-string of the map
  6227.    and the strings associated with various map elements.
  6228.  
  6229.    This can be done with X menus or with menus put in the minibuf.
  6230.    These are done in different ways, depending on how the input will be read.
  6231.    Menus using X are done after auto-saving in read-char, getting the input
  6232.    event from Fx_popup_menu; menus using the minibuf use read_char recursively
  6233.    and do auto-saving in the inner call of read_char. */
  6234.  
  6235. static Lisp_Object
  6236. read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
  6237.      int nmaps;
  6238.      Lisp_Object *maps;
  6239.      Lisp_Object prev_event;
  6240.      int *used_mouse_menu;
  6241. {
  6242.   int mapno;
  6243.   register Lisp_Object name;
  6244.   Lisp_Object rest, vector;
  6245.  
  6246.   if (used_mouse_menu)
  6247.     *used_mouse_menu = 0;
  6248.  
  6249.   /* Use local over global Menu maps */
  6250.  
  6251.   if (! menu_prompting)
  6252.     return Qnil;
  6253.  
  6254.   /* Optionally disregard all but the global map.  */
  6255.   if (inhibit_local_menu_bar_menus)
  6256.     {
  6257.       maps += (nmaps - 1);
  6258.       nmaps = 1;
  6259.     }
  6260.  
  6261.   /* Get the menu name from the first map that has one (a prompt string).  */
  6262.   for (mapno = 0; mapno < nmaps; mapno++)
  6263.     {
  6264.       name = map_prompt (maps[mapno]);
  6265.       if (!NILP (name))
  6266.     break;
  6267.     }
  6268.  
  6269.   /* If we don't have any menus, just read a character normally.  */
  6270.   if (mapno >= nmaps)
  6271.     return Qnil;
  6272.  
  6273. #ifdef HAVE_MENUS
  6274.   /* If we got to this point via a mouse click,
  6275.      use a real menu for mouse selection.  */
  6276.   if (EVENT_HAS_PARAMETERS (prev_event)
  6277.       && !EQ (XCONS (prev_event)->car, Qmenu_bar))
  6278.     {
  6279.       /* Display the menu and get the selection.  */
  6280.       Lisp_Object *realmaps
  6281.     = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
  6282.       Lisp_Object value;
  6283.       int nmaps1 = 0;
  6284.  
  6285.       /* Use the maps that are not nil.  */
  6286.       for (mapno = 0; mapno < nmaps; mapno++)
  6287.     if (!NILP (maps[mapno]))
  6288.       realmaps[nmaps1++] = maps[mapno];
  6289.  
  6290.       value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
  6291.       if (CONSP (value))
  6292.     {
  6293.       Lisp_Object tem;
  6294.  
  6295.       record_menu_key (XCONS (value)->car);
  6296.  
  6297.       /* If we got multiple events, unread all but
  6298.          the first.
  6299.          There is no way to prevent those unread events
  6300.          from showing up later in last_nonmenu_event.
  6301.          So turn symbol and integer events into lists,
  6302.          to indicate that they came from a mouse menu,
  6303.          so that when present in last_nonmenu_event
  6304.          they won't confuse things.  */
  6305.       for (tem = XCONS (value)->cdr; !NILP (tem);
  6306.            tem = XCONS (tem)->cdr)
  6307.         {
  6308.           record_menu_key (XCONS (tem)->car);
  6309.           if (SYMBOLP (XCONS (tem)->car)
  6310.           || INTEGERP (XCONS (tem)->car))
  6311.         XCONS (tem)->car
  6312.           = Fcons (XCONS (tem)->car, Qnil);
  6313.         }
  6314.  
  6315.       /* If we got more than one event, put all but the first
  6316.          onto this list to be read later.
  6317.          Return just the first event now.  */
  6318.       Vunread_command_events
  6319.         = nconc2 (XCONS (value)->cdr, Vunread_command_events);
  6320.       value = XCONS (value)->car;
  6321.     }
  6322.       else if (NILP (value))
  6323.     value = Qt;
  6324.       if (used_mouse_menu)
  6325.     *used_mouse_menu = 1;
  6326.       return value;
  6327.     }
  6328. #endif /* HAVE_MENUS */
  6329.   return Qnil ;
  6330. }
  6331.  
  6332. /* Buffer in use so far for the minibuf prompts for menu keymaps.
  6333.    We make this bigger when necessary, and never free it.  */
  6334. static char *read_char_minibuf_menu_text;
  6335. /* Size of that buffer.  */
  6336. static int read_char_minibuf_menu_width;
  6337.  
  6338. static Lisp_Object
  6339. read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
  6340.      int commandflag ;
  6341.      int nmaps;
  6342.      Lisp_Object *maps;
  6343. {
  6344.   int mapno;
  6345.   register Lisp_Object name;
  6346.   int nlength;
  6347.   int width = FRAME_WIDTH (selected_frame) - 4;
  6348.   int idx = -1;
  6349.   int nobindings = 1;
  6350.   Lisp_Object rest, vector;
  6351.   char *menu;
  6352.  
  6353.   if (! menu_prompting)
  6354.     return Qnil;
  6355.  
  6356.   /* Make sure we have a big enough buffer for the menu text.  */
  6357.   if (read_char_minibuf_menu_text == 0)
  6358.     {
  6359.       read_char_minibuf_menu_width = width + 4;
  6360.       read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
  6361.     }
  6362.   else if (width + 4 > read_char_minibuf_menu_width)
  6363.     {
  6364.       read_char_minibuf_menu_width = width + 4;
  6365.       read_char_minibuf_menu_text
  6366.     = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
  6367.     }
  6368.   menu = read_char_minibuf_menu_text;
  6369.  
  6370.   /* Get the menu name from the first map that has one (a prompt string).  */
  6371.   for (mapno = 0; mapno < nmaps; mapno++)
  6372.     {
  6373.       name = map_prompt (maps[mapno]);
  6374.       if (!NILP (name))
  6375.     break;
  6376.     }
  6377.  
  6378.   /* If we don't have any menus, just read a character normally.  */
  6379.   if (mapno >= nmaps)
  6380.     return Qnil;
  6381.  
  6382.   /* Prompt string always starts with map's prompt, and a space.  */
  6383.   strcpy (menu, XSTRING (name)->data);
  6384.   nlength = STRING_BYTES (XSTRING (name));
  6385.   menu[nlength++] = ':';
  6386.   menu[nlength++] = ' ';
  6387.   menu[nlength] = 0;
  6388.  
  6389.   /* Start prompting at start of first map.  */
  6390.   mapno = 0;
  6391.   rest = maps[mapno];
  6392.  
  6393.   /* Present the documented bindings, a line at a time.  */
  6394.   while (1)
  6395.     {
  6396.       int notfirst = 0;
  6397.       int i = nlength;
  6398.       Lisp_Object obj;
  6399.       int ch;
  6400.       Lisp_Object orig_defn_macro;
  6401.  
  6402.       /* Loop over elements of map.  */
  6403.       while (i < width)
  6404.     {
  6405.       Lisp_Object elt;
  6406.  
  6407.       /* If reached end of map, start at beginning of next map.  */
  6408.       if (NILP (rest))
  6409.         {
  6410.           mapno++;
  6411.           /* At end of last map, wrap around to first map if just starting,
  6412.          or end this line if already have something on it.  */
  6413.           if (mapno == nmaps)
  6414.         {
  6415.           mapno = 0;
  6416.           if (notfirst || nobindings) break;
  6417.         }
  6418.           rest = maps[mapno];
  6419.         }
  6420.  
  6421.       /* Look at the next element of the map.  */
  6422.       if (idx >= 0)
  6423.         elt = XVECTOR (vector)->contents[idx];
  6424.       else
  6425.         elt = Fcar_safe (rest);
  6426.  
  6427.       if (idx < 0 && VECTORP (elt))
  6428.         {
  6429.           /* If we found a dense table in the keymap,
  6430.          advanced past it, but start scanning its contents.  */
  6431.           rest = Fcdr_safe (rest);
  6432.           vector = elt;
  6433.           idx = 0;
  6434.         }
  6435.       else
  6436.         {
  6437.           /* An ordinary element.  */
  6438.           Lisp_Object event, tem;
  6439.  
  6440.           if (idx < 0)
  6441.         {
  6442.           event = Fcar_safe (elt); /* alist */
  6443.           elt = Fcdr_safe (elt);
  6444.         }
  6445.           else
  6446.         {
  6447.           XSETINT (event, idx); /* vector */
  6448.         }
  6449.  
  6450.           /* Ignore the element if it has no prompt string.  */
  6451.           if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
  6452.         {
  6453.           /* 1 if the char to type matches the string.  */
  6454.           int char_matches;
  6455.           Lisp_Object upcased_event, downcased_event;
  6456.           Lisp_Object desc;
  6457.           Lisp_Object s
  6458.             = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
  6459.  
  6460.           upcased_event = Fupcase (event);
  6461.           downcased_event = Fdowncase (event);
  6462.           char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
  6463.                   || XINT (downcased_event) == XSTRING (s)->data[0]);
  6464.           if (! char_matches)
  6465.             desc = Fsingle_key_description (event);
  6466.  
  6467.           tem
  6468.             = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
  6469.           if (!NILP (tem))
  6470.             /* Insert equivalent keybinding. */
  6471.             s = concat2 (s, tem);
  6472.  
  6473.           tem
  6474.             = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
  6475.           if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
  6476.             {
  6477.               /* Insert button prefix. */
  6478.               Lisp_Object selected
  6479.             = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
  6480.               if (EQ (tem, QCradio))
  6481.             tem = build_string (NILP (selected) ? "(*) " : "( ) ");
  6482.               else
  6483.             tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
  6484.               s = concat2 (tem, s);
  6485.             }
  6486.           
  6487.  
  6488.           /* If we have room for the prompt string, add it to this line.
  6489.              If this is the first on the line, always add it.  */
  6490.           if ((XSTRING (s)->size + i + 2
  6491.                + (char_matches ? 0 : XSTRING (desc)->size + 3))
  6492.               < width
  6493.               || !notfirst)
  6494.             {
  6495.               int thiswidth;
  6496.  
  6497.               /* Punctuate between strings.  */
  6498.               if (notfirst)
  6499.             {
  6500.               strcpy (menu + i, ", ");
  6501.               i += 2;
  6502.             }
  6503.               notfirst = 1;
  6504.               nobindings = 0 ;
  6505.  
  6506.               /* If the char to type doesn't match the string's
  6507.              first char, explicitly show what char to type.  */
  6508.               if (! char_matches)
  6509.             {
  6510.               /* Add as much of string as fits.  */
  6511.               thiswidth = XSTRING (desc)->size;
  6512.               if (thiswidth + i > width)
  6513.                 thiswidth = width - i;
  6514.               bcopy (XSTRING (desc)->data, menu + i, thiswidth);
  6515.               i += thiswidth;
  6516.               strcpy (menu + i, " = ");
  6517.               i += 3;
  6518.             }
  6519.  
  6520.               /* Add as much of string as fits.  */
  6521.               thiswidth = XSTRING (s)->size;
  6522.               if (thiswidth + i > width)
  6523.             thiswidth = width - i;
  6524.               bcopy (XSTRING (s)->data, menu + i, thiswidth);
  6525.               i += thiswidth;
  6526.               menu[i] = 0;
  6527.             }
  6528.           else
  6529.             {
  6530.               /* If this element does not fit, end the line now,
  6531.              and save the element for the next line.  */
  6532.               strcpy (menu + i, "...");
  6533.               break;
  6534.             }
  6535.         }
  6536.  
  6537.           /* Move past this element.  */
  6538.           if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
  6539.         /* Handle reaching end of dense table.  */
  6540.         idx = -1;
  6541.           if (idx >= 0)
  6542.         idx++;
  6543.           else
  6544.         rest = Fcdr_safe (rest);
  6545.         }
  6546.     }
  6547.  
  6548.       /* Prompt with that and read response.  */
  6549.       message2_nolog (menu, strlen (menu), 
  6550.               ! NILP (current_buffer->enable_multibyte_characters));
  6551.  
  6552.       /* Make believe its not a keyboard macro in case the help char
  6553.      is pressed.  Help characters are not recorded because menu prompting
  6554.      is not used on replay.
  6555.      */
  6556.       orig_defn_macro = current_kboard->defining_kbd_macro;
  6557.       current_kboard->defining_kbd_macro = Qnil;
  6558.       do
  6559.     obj = read_char (commandflag, 0, 0, Qnil, 0);
  6560.       while (BUFFERP (obj));
  6561.       current_kboard->defining_kbd_macro = orig_defn_macro;
  6562.  
  6563.       if (!INTEGERP (obj))
  6564.     return obj;
  6565.       else
  6566.     ch = XINT (obj);
  6567.  
  6568.       if (! EQ (obj, menu_prompt_more_char)
  6569.       && (!INTEGERP (menu_prompt_more_char)
  6570.           || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
  6571.     {
  6572.       if (!NILP (current_kboard->defining_kbd_macro))
  6573.         store_kbd_macro_char (obj);
  6574.       return obj;
  6575.     }
  6576.       /* Help char - go round again */
  6577.     }
  6578. }
  6579.  
  6580. /* Reading key sequences.  */
  6581.  
  6582. /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
  6583.    in DEFS[0..NMAPS-1].  Set NEXT[i] to DEFS[i] if DEFS[i] is a
  6584.    keymap, or nil otherwise.  Return the index of the first keymap in
  6585.    which KEY has any binding, or NMAPS if no map has a binding.
  6586.  
  6587.    If KEY is a meta ASCII character, treat it like meta-prefix-char
  6588.    followed by the corresponding non-meta character.  Keymaps in
  6589.    CURRENT with non-prefix bindings for meta-prefix-char become nil in
  6590.    NEXT.
  6591.  
  6592.    If KEY has no bindings in any of the CURRENT maps, NEXT is left
  6593.    unmodified.
  6594.  
  6595.    NEXT may be the same array as CURRENT.  */
  6596.  
  6597. static int
  6598. follow_key (key, nmaps, current, defs, next)
  6599.      Lisp_Object key;
  6600.      Lisp_Object *current, *defs, *next;
  6601.      int nmaps;
  6602. {
  6603.   int i, first_binding;
  6604.   int did_meta = 0;
  6605.  
  6606.   /* If KEY is a meta ASCII character, treat it like meta-prefix-char
  6607.      followed by the corresponding non-meta character.
  6608.      Put the results into DEFS, since we are going to alter that anyway.
  6609.      Do not alter CURRENT or NEXT.  */
  6610.   if (INTEGERP (key) && (XINT (key) & CHAR_META))
  6611.     {
  6612.       for (i = 0; i < nmaps; i++)
  6613.     if (! NILP (current[i]))
  6614.       {
  6615.         Lisp_Object def;
  6616.         def = get_keyelt (access_keymap (current[i],
  6617.                          meta_prefix_char, 1, 0), 0);
  6618.  
  6619.         /* Note that since we pass the resulting bindings through
  6620.            get_keymap_1, non-prefix bindings for meta-prefix-char
  6621.            disappear.  */
  6622.         defs[i] = get_keymap_1 (def, 0, 1);
  6623.       }
  6624.     else
  6625.       defs[i] = Qnil;
  6626.  
  6627.       did_meta = 1;
  6628.       XSETINT (key, XFASTINT (key) & ~CHAR_META);
  6629.     }
  6630.  
  6631.   first_binding = nmaps;
  6632.   for (i = nmaps - 1; i >= 0; i--)
  6633.     {
  6634.       if (! NILP (current[i]))
  6635.     {
  6636.       Lisp_Object map;
  6637.       if (did_meta)
  6638.         map = defs[i];
  6639.       else
  6640.         map = current[i];
  6641.  
  6642.       defs[i] = get_keyelt (access_keymap (map, key, 1, 0), 0);
  6643.       if (! NILP (defs[i]))
  6644.         first_binding = i;
  6645.     }
  6646.       else
  6647.     defs[i] = Qnil;
  6648.     }
  6649.  
  6650.   /* Given the set of bindings we've found, produce the next set of maps.  */
  6651.   if (first_binding < nmaps)
  6652.     for (i = 0; i < nmaps; i++)
  6653.       next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
  6654.  
  6655.   return first_binding;
  6656. }
  6657.  
  6658. /* Read a sequence of keys that ends with a non prefix character,
  6659.    storing it in KEYBUF, a buffer of size BUFSIZE.
  6660.    Prompt with PROMPT.
  6661.    Return the length of the key sequence stored.
  6662.    Return -1 if the user rejected a command menu.
  6663.  
  6664.    Echo starting immediately unless `prompt' is 0.
  6665.  
  6666.    Where a key sequence ends depends on the currently active keymaps.
  6667.    These include any minor mode keymaps active in the current buffer,
  6668.    the current buffer's local map, and the global map.
  6669.  
  6670.    If a key sequence has no other bindings, we check Vfunction_key_map
  6671.    to see if some trailing subsequence might be the beginning of a
  6672.    function key's sequence.  If so, we try to read the whole function
  6673.    key, and substitute its symbolic name into the key sequence.
  6674.  
  6675.    We ignore unbound `down-' mouse clicks.  We turn unbound `drag-' and
  6676.    `double-' events into similar click events, if that would make them
  6677.    bound.  We try to turn `triple-' events first into `double-' events,
  6678.    then into clicks.
  6679.  
  6680.    If we get a mouse click in a mode line, vertical divider, or other
  6681.    non-text area, we treat the click as if it were prefixed by the
  6682.    symbol denoting that area - `mode-line', `vertical-line', or
  6683.    whatever.
  6684.  
  6685.    If the sequence starts with a mouse click, we read the key sequence
  6686.    with respect to the buffer clicked on, not the current buffer.
  6687.  
  6688.    If the user switches frames in the midst of a key sequence, we put
  6689.    off the switch-frame event until later; the next call to
  6690.    read_char will return it.
  6691.  
  6692.    If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
  6693.    from the selected window's buffer.  */
  6694.  
  6695. static int
  6696. read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
  6697.            can_return_switch_frame, fix_current_buffer)
  6698.      Lisp_Object *keybuf;
  6699.      int bufsize;
  6700.      Lisp_Object prompt;
  6701.      int dont_downcase_last;
  6702.      int can_return_switch_frame;
  6703.      int fix_current_buffer;
  6704. {
  6705.   int count = specpdl_ptr - specpdl;
  6706.  
  6707.   /* How many keys there are in the current key sequence.  */
  6708.   int t;
  6709.  
  6710.   /* The length of the echo buffer when we started reading, and
  6711.      the length of this_command_keys when we started reading.  */
  6712.   int echo_start;
  6713.   int keys_start;
  6714.  
  6715.   /* The number of keymaps we're scanning right now, and the number of
  6716.      keymaps we have allocated space for.  */
  6717.   int nmaps;
  6718.   int nmaps_allocated = 0;
  6719.  
  6720.   /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
  6721.      the current keymaps.  */
  6722.   Lisp_Object *defs;
  6723.  
  6724.   /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
  6725.      in the current keymaps, or nil where it is not a prefix.  */
  6726.   Lisp_Object *submaps;
  6727.  
  6728.   /* The local map to start out with at start of key sequence.  */
  6729.   Lisp_Object orig_local_map;
  6730.  
  6731.   /* 1 if we have already considered switching to the local-map property
  6732.      of the place where a mouse click occurred.  */
  6733.   int localized_local_map = 0;
  6734.  
  6735.   /* The index in defs[] of the first keymap that has a binding for
  6736.      this key sequence.  In other words, the lowest i such that
  6737.      defs[i] is non-nil.  */
  6738.   int first_binding;
  6739.  
  6740.   /* If t < mock_input, then KEYBUF[t] should be read as the next
  6741.      input key.
  6742.  
  6743.      We use this to recover after recognizing a function key.  Once we
  6744.      realize that a suffix of the current key sequence is actually a
  6745.      function key's escape sequence, we replace the suffix with the
  6746.      function key's binding from Vfunction_key_map.  Now keybuf
  6747.      contains a new and different key sequence, so the echo area,
  6748.      this_command_keys, and the submaps and defs arrays are wrong.  In
  6749.      this situation, we set mock_input to t, set t to 0, and jump to
  6750.      restart_sequence; the loop will read keys from keybuf up until
  6751.      mock_input, thus rebuilding the state; and then it will resume
  6752.      reading characters from the keyboard.  */
  6753.   int mock_input = 0;
  6754.  
  6755.   /* If the sequence is unbound in submaps[], then
  6756.      keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
  6757.      and fkey_map is its binding.
  6758.  
  6759.      These might be > t, indicating that all function key scanning
  6760.      should hold off until t reaches them.  We do this when we've just
  6761.      recognized a function key, to avoid searching for the function
  6762.      key's again in Vfunction_key_map.  */
  6763.   int fkey_start = 0, fkey_end = 0;
  6764.   Lisp_Object fkey_map;
  6765.  
  6766.   /* Likewise, for key_translation_map.  */
  6767.   int keytran_start = 0, keytran_end = 0;
  6768.   Lisp_Object keytran_map;
  6769.  
  6770.   /* If we receive a ``switch-frame'' event in the middle of a key sequence,
  6771.      we put it off for later.  While we're reading, we keep the event here.  */
  6772.   Lisp_Object delayed_switch_frame;
  6773.  
  6774.   /* See the comment below... */
  6775. #if defined (GOBBLE_FIRST_EVENT)
  6776.   Lisp_Object first_event;
  6777. #endif
  6778.  
  6779.   Lisp_Object original_uppercase;
  6780.   int original_uppercase_position = -1;
  6781.  
  6782.   /* Gets around Microsoft compiler limitations.  */
  6783.   int dummyflag = 0;
  6784.  
  6785.   struct buffer *starting_buffer;
  6786.  
  6787.   /* Nonzero if we seem to have got the beginning of a binding
  6788.      in function_key_map.  */
  6789.   int function_key_possible = 0;
  6790.   int key_translation_possible = 0;
  6791.  
  6792.   /* Save the status of key translation before each step,
  6793.      so that we can restore this after downcasing.  */
  6794.   Lisp_Object prev_fkey_map;
  6795.   int prev_fkey_start;
  6796.   int prev_fkey_end;
  6797.  
  6798.   Lisp_Object prev_keytran_map;
  6799.   int prev_keytran_start;
  6800.   int prev_keytran_end;
  6801.  
  6802.   int junk;
  6803.  
  6804.   raw_keybuf_count = 0;
  6805.  
  6806.   last_nonmenu_event = Qnil;
  6807.  
  6808.   delayed_switch_frame = Qnil;
  6809.   fkey_map = Vfunction_key_map;
  6810.   keytran_map = Vkey_translation_map;
  6811.  
  6812.   /* If there is no function-key-map, turn off function key scanning.  */
  6813.   if (NILP (Fkeymapp (Vfunction_key_map)))
  6814.     fkey_start = fkey_end = bufsize + 1;
  6815.  
  6816.   /* If there is no key-translation-map, turn off scanning.  */
  6817.   if (NILP (Fkeymapp (Vkey_translation_map)))
  6818.     keytran_start = keytran_end = bufsize + 1;
  6819.  
  6820.   if (INTERACTIVE)
  6821.     {
  6822.       if (!NILP (prompt))
  6823.     echo_prompt (XSTRING (prompt)->data);
  6824.       else if (cursor_in_echo_area && echo_keystrokes)
  6825.     /* This doesn't put in a dash if the echo buffer is empty, so
  6826.        you don't always see a dash hanging out in the minibuffer.  */
  6827.     echo_dash ();
  6828.     }
  6829.  
  6830.   /* Record the initial state of the echo area and this_command_keys;
  6831.      we will need to restore them if we replay a key sequence.  */
  6832.   if (INTERACTIVE)
  6833.     echo_start = echo_length ();
  6834.   keys_start = this_command_key_count;
  6835.   this_single_command_key_start = keys_start;
  6836.  
  6837. #if defined (GOBBLE_FIRST_EVENT)
  6838.   /* This doesn't quite work, because some of the things that read_char
  6839.      does cannot safely be bypassed.  It seems too risky to try to make
  6840.      this work right.  */
  6841.  
  6842.   /* Read the first char of the sequence specially, before setting
  6843.      up any keymaps, in case a filter runs and switches buffers on us.  */
  6844.   first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
  6845.                &junk);
  6846. #endif /* GOBBLE_FIRST_EVENT */
  6847.  
  6848.   orig_local_map = get_local_map (PT, current_buffer);
  6849.  
  6850.   /* We jump here when the key sequence has been thoroughly changed, and
  6851.      we need to rescan it starting from the beginning.  When we jump here,
  6852.      keybuf[0..mock_input] holds the sequence we should reread.  */
  6853.  replay_sequence:
  6854.  
  6855.   starting_buffer = current_buffer;
  6856.   function_key_possible = 0;
  6857.   key_translation_possible = 0;
  6858.  
  6859.   /* Build our list of keymaps.
  6860.      If we recognize a function key and replace its escape sequence in
  6861.      keybuf with its symbol, or if the sequence starts with a mouse
  6862.      click and we need to switch buffers, we jump back here to rebuild
  6863.      the initial keymaps from the current buffer.  */
  6864.   {
  6865.     Lisp_Object *maps;
  6866.  
  6867.     if (!NILP (current_kboard->Voverriding_terminal_local_map)
  6868.     || !NILP (Voverriding_local_map))
  6869.       {
  6870.     if (3 > nmaps_allocated)
  6871.       {
  6872.         submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
  6873.         defs    = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
  6874.         nmaps_allocated = 3;
  6875.       }
  6876.     nmaps = 0;
  6877.     if (!NILP (current_kboard->Voverriding_terminal_local_map))
  6878.       submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
  6879.     if (!NILP (Voverriding_local_map))
  6880.       submaps[nmaps++] = Voverriding_local_map;
  6881.       }
  6882.     else
  6883.       {
  6884.     nmaps = current_minor_maps (0, &maps);
  6885.     if (nmaps + 2 > nmaps_allocated)
  6886.       {
  6887.         submaps = (Lisp_Object *) alloca ((nmaps+2) * sizeof (submaps[0]));
  6888.         defs    = (Lisp_Object *) alloca ((nmaps+2) * sizeof (defs[0]));
  6889.         nmaps_allocated = nmaps + 2;
  6890.       }
  6891.     bcopy (maps, submaps, nmaps * sizeof (submaps[0]));
  6892. #ifdef USE_TEXT_PROPERTIES
  6893.     submaps[nmaps++] = orig_local_map;
  6894. #else
  6895.     submaps[nmaps++] = current_buffer->keymap;
  6896. #endif
  6897.       }
  6898.     submaps[nmaps++] = current_global_map;
  6899.   }
  6900.  
  6901.   /* Find an accurate initial value for first_binding.  */
  6902.   for (first_binding = 0; first_binding < nmaps; first_binding++)
  6903.     if (! NILP (submaps[first_binding]))
  6904.       break;
  6905.  
  6906.   /* Start from the beginning in keybuf.  */
  6907.   t = 0;
  6908.  
  6909.   /* These are no-ops the first time through, but if we restart, they
  6910.      revert the echo area and this_command_keys to their original state.  */
  6911.   this_command_key_count = keys_start;
  6912.   if (INTERACTIVE && t < mock_input)
  6913.     echo_truncate (echo_start);
  6914.  
  6915.   /* If the best binding for the current key sequence is a keymap, or
  6916.      we may be looking at a function key's escape sequence, keep on
  6917.      reading.  */
  6918.   while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
  6919.      || (first_binding >= nmaps
  6920.          && fkey_start < t
  6921.          /* mock input is never part of a function key's sequence.  */
  6922.          && mock_input <= fkey_start)
  6923.      || (first_binding >= nmaps
  6924.          && keytran_start < t && key_translation_possible)
  6925.      /* Don't return in the middle of a possible function key sequence,
  6926.         if the only bindings we found were via case conversion.
  6927.         Thus, if ESC O a has a function-key-map translation
  6928.         and ESC o has a binding, don't return after ESC O,
  6929.         so that we can translate ESC O plus the next character.  */
  6930.      )
  6931.     {
  6932.       Lisp_Object key;
  6933.       int used_mouse_menu = 0;
  6934.  
  6935.       /* Where the last real key started.  If we need to throw away a
  6936.          key that has expanded into more than one element of keybuf
  6937.          (say, a mouse click on the mode line which is being treated
  6938.          as [mode-line (mouse-...)], then we backtrack to this point
  6939.          of keybuf.  */
  6940.       int last_real_key_start;
  6941.  
  6942.       /* These variables are analogous to echo_start and keys_start;
  6943.      while those allow us to restart the entire key sequence,
  6944.      echo_local_start and keys_local_start allow us to throw away
  6945.      just one key.  */
  6946.       int echo_local_start, keys_local_start, local_first_binding;
  6947.  
  6948.       if (t >= bufsize)
  6949.     error ("Key sequence too long");
  6950.  
  6951.       if (INTERACTIVE)
  6952.     echo_local_start = echo_length ();
  6953.       keys_local_start = this_command_key_count;
  6954.       local_first_binding = first_binding;
  6955.  
  6956.     replay_key:
  6957.       /* These are no-ops, unless we throw away a keystroke below and
  6958.      jumped back up to replay_key; in that case, these restore the
  6959.      variables to their original state, allowing us to replay the
  6960.      loop.  */
  6961.       if (INTERACTIVE && t < mock_input)
  6962.     echo_truncate (echo_local_start);
  6963.       this_command_key_count = keys_local_start;
  6964.       first_binding = local_first_binding;
  6965.  
  6966.       /* By default, assume each event is "real".  */
  6967.       last_real_key_start = t;
  6968.  
  6969.       /* Does mock_input indicate that we are re-reading a key sequence?  */
  6970.       if (t < mock_input)
  6971.     {
  6972.       key = keybuf[t];
  6973.       add_command_key (key);
  6974.       if (echo_keystrokes)
  6975.         echo_char (key);
  6976.     }
  6977.  
  6978.       /* If not, we should actually read a character.  */
  6979.       else
  6980.     {
  6981.       struct buffer *buf = current_buffer;
  6982.  
  6983.       {
  6984. #ifdef MULTI_KBOARD
  6985.         KBOARD *interrupted_kboard = current_kboard;
  6986.         struct frame *interrupted_frame = selected_frame;
  6987.         if (setjmp (wrong_kboard_jmpbuf))
  6988.           {
  6989.         if (!NILP (delayed_switch_frame))
  6990.           {
  6991.             interrupted_kboard->kbd_queue
  6992.               = Fcons (delayed_switch_frame,
  6993.                    interrupted_kboard->kbd_queue);
  6994.             delayed_switch_frame = Qnil;
  6995.           }
  6996.         while (t > 0)
  6997.           interrupted_kboard->kbd_queue
  6998.             = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
  6999.  
  7000.         /* If the side queue is non-empty, ensure it begins with a
  7001.            switch-frame, so we'll replay it in the right context.  */
  7002.         if (CONSP (interrupted_kboard->kbd_queue)
  7003.             && (key = XCONS (interrupted_kboard->kbd_queue)->car,
  7004.             !(EVENT_HAS_PARAMETERS (key)
  7005.               && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
  7006.                  Qswitch_frame))))
  7007.           {
  7008.             Lisp_Object frame;
  7009.             XSETFRAME (frame, interrupted_frame);
  7010.             interrupted_kboard->kbd_queue
  7011.               = Fcons (make_lispy_switch_frame (frame),
  7012.                    interrupted_kboard->kbd_queue);
  7013.           }
  7014.         mock_input = 0;
  7015.         orig_local_map = get_local_map (PT, current_buffer);
  7016.         goto replay_sequence;
  7017.           }
  7018. #endif
  7019.         key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
  7020.                  &used_mouse_menu);
  7021.       }
  7022.  
  7023.       /* read_char returns t when it shows a menu and the user rejects it.
  7024.          Just return -1.  */
  7025.       if (EQ (key, Qt))
  7026.         {
  7027.           unbind_to (count, Qnil);
  7028.           return -1;
  7029.         }
  7030.  
  7031.       /* read_char returns -1 at the end of a macro.
  7032.          Emacs 18 handles this by returning immediately with a
  7033.          zero, so that's what we'll do.  */
  7034.       if (INTEGERP (key) && XINT (key) == -1)
  7035.         {
  7036.           t = 0;
  7037.           /* The Microsoft C compiler can't handle the goto that
  7038.          would go here.  */
  7039.           dummyflag = 1;
  7040.           break;
  7041.         }
  7042.  
  7043.       /* If the current buffer has been changed from under us, the
  7044.          keymap may have changed, so replay the sequence.  */
  7045.       if (BUFFERP (key))
  7046.         {
  7047.           mock_input = t;
  7048.           /* Reset the current buffer from the selected window
  7049.          in case something changed the former and not the latter.
  7050.          This is to be more consistent with the behavior
  7051.          of the command_loop_1.  */
  7052.           if (fix_current_buffer)
  7053.         if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  7054.           Fset_buffer (XWINDOW (selected_window)->buffer);
  7055.  
  7056.           orig_local_map = get_local_map (PT, current_buffer);
  7057.           goto replay_sequence;
  7058.         }
  7059.  
  7060.       /* If we have a quit that was typed in another frame, and
  7061.          quit_throw_to_read_char switched buffers,
  7062.          replay to get the right keymap.  */
  7063.       if (XINT (key) == quit_char && current_buffer != starting_buffer)
  7064.         {
  7065.           GROW_RAW_KEYBUF;
  7066.           XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
  7067.           keybuf[t++] = key;
  7068.           mock_input = t;
  7069.           Vquit_flag = Qnil;
  7070.           orig_local_map = get_local_map (PT, current_buffer);
  7071.           goto replay_sequence;
  7072.         }
  7073.  
  7074.       Vquit_flag = Qnil;
  7075.  
  7076.       if (EVENT_HAS_PARAMETERS (key)
  7077.           && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
  7078.         {
  7079.           /* If we're at the beginning of a key sequence, and the caller
  7080.          says it's okay, go ahead and return this event.  If we're
  7081.          in the midst of a key sequence, delay it until the end. */
  7082.           if (t > 0 || !can_return_switch_frame)
  7083.         {
  7084.           delayed_switch_frame = key;
  7085.           goto replay_key;
  7086.         }
  7087.         }
  7088.  
  7089.       GROW_RAW_KEYBUF;
  7090.       XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
  7091.     }
  7092.  
  7093.       /* Clicks in non-text areas get prefixed by the symbol
  7094.      in their CHAR-ADDRESS field.  For example, a click on
  7095.      the mode line is prefixed by the symbol `mode-line'.
  7096.  
  7097.      Furthermore, key sequences beginning with mouse clicks
  7098.      are read using the keymaps of the buffer clicked on, not
  7099.      the current buffer.  So we may have to switch the buffer
  7100.      here.
  7101.  
  7102.      When we turn one event into two events, we must make sure
  7103.      that neither of the two looks like the original--so that,
  7104.      if we replay the events, they won't be expanded again.
  7105.      If not for this, such reexpansion could happen either here
  7106.      or when user programs play with this-command-keys.  */
  7107.       if (EVENT_HAS_PARAMETERS (key))
  7108.     {
  7109.       Lisp_Object kind;
  7110.  
  7111.       kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
  7112.       if (EQ (kind, Qmouse_click))
  7113.         {
  7114.           Lisp_Object window, posn;
  7115.  
  7116.           window = POSN_WINDOW      (EVENT_START (key));
  7117.           posn   = POSN_BUFFER_POSN (EVENT_START (key));
  7118.           if (CONSP (posn))
  7119.         {
  7120.           /* We're looking at the second event of a
  7121.              sequence which we expanded before.  Set
  7122.              last_real_key_start appropriately.  */
  7123.           if (t > 0)
  7124.             last_real_key_start = t - 1;
  7125.         }
  7126.  
  7127.           /* Key sequences beginning with mouse clicks are
  7128.          read using the keymaps in the buffer clicked on,
  7129.          not the current buffer.  If we're at the
  7130.          beginning of a key sequence, switch buffers.  */
  7131.           if (last_real_key_start == 0
  7132.           && WINDOWP (window)
  7133.           && BUFFERP (XWINDOW (window)->buffer)
  7134.           && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
  7135.         {
  7136.           XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
  7137.           keybuf[t] = key;
  7138.           mock_input = t + 1;
  7139.  
  7140.           /* Arrange to go back to the original buffer once we're
  7141.              done reading the key sequence.  Note that we can't
  7142.              use save_excursion_{save,restore} here, because they
  7143.              save point as well as the current buffer; we don't
  7144.              want to save point, because redisplay may change it,
  7145.              to accommodate a Fset_window_start or something.  We
  7146.              don't want to do this at the top of the function,
  7147.              because we may get input from a subprocess which
  7148.              wants to change the selected window and stuff (say,
  7149.              emacsclient).  */
  7150.           record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  7151.  
  7152.           set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
  7153.           orig_local_map = get_local_map (PT, current_buffer);
  7154.           goto replay_sequence;
  7155.         }
  7156.           /* For a mouse click, get the local text-property keymap
  7157.          of the place clicked on, rather than point.  */
  7158.           if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
  7159.           && ! localized_local_map)
  7160.         {
  7161.           Lisp_Object map_here, start, pos;
  7162.  
  7163.           localized_local_map = 1;
  7164.           start = EVENT_START (key);
  7165.           if (CONSP (start) && CONSP (XCONS (start)->cdr))
  7166.             {
  7167.               pos = POSN_BUFFER_POSN (start);
  7168.               if (INTEGERP (pos)
  7169.               && XINT (pos) >= BEG && XINT (pos) <= Z)
  7170.             {
  7171.               map_here = get_local_map (XINT (pos), current_buffer);
  7172.               if (!EQ (map_here, orig_local_map))
  7173.                 {
  7174.                   orig_local_map = map_here;
  7175.                   keybuf[t] = key;
  7176.                   mock_input = t + 1;
  7177.  
  7178.                   goto replay_sequence;
  7179.                 }
  7180.             }
  7181.             }
  7182.         }
  7183.  
  7184.           /* Expand mode-line and scroll-bar events into two events:
  7185.          use posn as a fake prefix key.  */
  7186.           if (SYMBOLP (posn))
  7187.         {
  7188.           if (t + 1 >= bufsize)
  7189.             error ("Key sequence too long");
  7190.           keybuf[t] = posn;
  7191.           keybuf[t+1] = key;
  7192.           mock_input = t + 2;
  7193.  
  7194.           /* Zap the position in key, so we know that we've
  7195.              expanded it, and don't try to do so again.  */
  7196.           POSN_BUFFER_POSN (EVENT_START (key))
  7197.             = Fcons (posn, Qnil);
  7198.           goto replay_key;
  7199.         }
  7200.         }
  7201.       else if (CONSP (XCONS (key)->cdr)
  7202.            && CONSP (EVENT_START (key))
  7203.            && CONSP (XCONS (EVENT_START (key))->cdr))
  7204.         {
  7205.           Lisp_Object posn;
  7206.  
  7207.           posn = POSN_BUFFER_POSN (EVENT_START (key));
  7208.           /* Handle menu-bar events:
  7209.          insert the dummy prefix event `menu-bar'.  */
  7210.           if (EQ (posn, Qmenu_bar))
  7211.         {
  7212.           if (t + 1 >= bufsize)
  7213.             error ("Key sequence too long");
  7214.           keybuf[t] = posn;
  7215.           keybuf[t+1] = key;
  7216.  
  7217.           /* Zap the position in key, so we know that we've
  7218.              expanded it, and don't try to do so again.  */
  7219.           POSN_BUFFER_POSN (EVENT_START (key))
  7220.             = Fcons (posn, Qnil);
  7221.  
  7222.           mock_input = t + 2;
  7223.           goto replay_sequence;
  7224.         }
  7225.           else if (CONSP (posn))
  7226.         {
  7227.           /* We're looking at the second event of a
  7228.              sequence which we expanded before.  Set
  7229.              last_real_key_start appropriately.  */
  7230.           if (last_real_key_start == t && t > 0)
  7231.             last_real_key_start = t - 1;
  7232.         }
  7233.         }
  7234.     }
  7235.  
  7236.       /* We have finally decided that KEY is something we might want
  7237.      to look up.  */
  7238.       first_binding = (follow_key (key,
  7239.                    nmaps   - first_binding,
  7240.                    submaps + first_binding,
  7241.                    defs    + first_binding,
  7242.                    submaps + first_binding)
  7243.                + first_binding);
  7244.  
  7245.       /* If KEY wasn't bound, we'll try some fallbacks.  */
  7246.       if (first_binding >= nmaps)
  7247.     {
  7248.       Lisp_Object head;
  7249.  
  7250.       head = EVENT_HEAD (key);
  7251.       if (help_char_p (head) && t > 0)
  7252.         {
  7253.           read_key_sequence_cmd = Vprefix_help_command;
  7254.           keybuf[t++] = key;
  7255.           last_nonmenu_event = key;
  7256.           /* The Microsoft C compiler can't handle the goto that
  7257.          would go here.  */
  7258.           dummyflag = 1;
  7259.           break;
  7260.         }
  7261.  
  7262.       if (SYMBOLP (head))
  7263.         {
  7264.           Lisp_Object breakdown;
  7265.           int modifiers;
  7266.  
  7267.           breakdown = parse_modifiers (head);
  7268.           modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
  7269.           /* Attempt to reduce an unbound mouse event to a simpler
  7270.          event that is bound:
  7271.            Drags reduce to clicks.
  7272.            Double-clicks reduce to clicks.
  7273.            Triple-clicks reduce to double-clicks, then to clicks.
  7274.            Down-clicks are eliminated.
  7275.            Double-downs reduce to downs, then are eliminated.
  7276.            Triple-downs reduce to double-downs, then to downs,
  7277.              then are eliminated. */
  7278.           if (modifiers & (down_modifier | drag_modifier
  7279.                    | double_modifier | triple_modifier))
  7280.         {
  7281.           while (modifiers & (down_modifier | drag_modifier
  7282.                       | double_modifier | triple_modifier))
  7283.             {
  7284.               Lisp_Object new_head, new_click;
  7285.               if (modifiers & triple_modifier)
  7286.             modifiers ^= (double_modifier | triple_modifier);
  7287.               else if (modifiers & double_modifier)
  7288.             modifiers &= ~double_modifier;
  7289.               else if (modifiers & drag_modifier)
  7290.             modifiers &= ~drag_modifier;
  7291.               else
  7292.             {
  7293.               /* Dispose of this `down' event by simply jumping
  7294.                  back to replay_key, to get another event.
  7295.  
  7296.                  Note that if this event came from mock input,
  7297.                  then just jumping back to replay_key will just
  7298.                  hand it to us again.  So we have to wipe out any
  7299.                  mock input.
  7300.  
  7301.                  We could delete keybuf[t] and shift everything
  7302.                  after that to the left by one spot, but we'd also
  7303.                  have to fix up any variable that points into
  7304.                  keybuf, and shifting isn't really necessary
  7305.                  anyway.
  7306.  
  7307.                  Adding prefixes for non-textual mouse clicks
  7308.                  creates two characters of mock input, and both
  7309.                  must be thrown away.  If we're only looking at
  7310.                  the prefix now, we can just jump back to
  7311.                  replay_key.  On the other hand, if we've already
  7312.                  processed the prefix, and now the actual click
  7313.                  itself is giving us trouble, then we've lost the
  7314.                  state of the keymaps we want to backtrack to, and
  7315.                  we need to replay the whole sequence to rebuild
  7316.                  it.
  7317.  
  7318.                  Beyond that, only function key expansion could
  7319.                  create more than two keys, but that should never
  7320.                  generate mouse events, so it's okay to zero
  7321.                  mock_input in that case too.
  7322.  
  7323.                  Isn't this just the most wonderful code ever?  */
  7324.               if (t == last_real_key_start)
  7325.                 {
  7326.                   mock_input = 0;
  7327.                   goto replay_key;
  7328.                 }
  7329.               else
  7330.                 {
  7331.                   mock_input = last_real_key_start;
  7332.                   goto replay_sequence;
  7333.                 }
  7334.             }
  7335.  
  7336.               new_head
  7337.             = apply_modifiers (modifiers, XCONS (breakdown)->car);
  7338.               new_click
  7339.             = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
  7340.  
  7341.               /* Look for a binding for this new key.  follow_key
  7342.              promises that it didn't munge submaps the
  7343.              last time we called it, since key was unbound.  */
  7344.               first_binding
  7345.             = (follow_key (new_click,
  7346.                        nmaps   - local_first_binding,
  7347.                        submaps + local_first_binding,
  7348.                        defs    + local_first_binding,
  7349.                        submaps + local_first_binding)
  7350.                + local_first_binding);
  7351.  
  7352.               /* If that click is bound, go for it.  */
  7353.               if (first_binding < nmaps)
  7354.             {
  7355.               key = new_click;
  7356.               break;
  7357.             }
  7358.               /* Otherwise, we'll leave key set to the drag event.  */
  7359.             }
  7360.         }
  7361.         }
  7362.     }
  7363.  
  7364.       keybuf[t++] = key;
  7365.       /* Normally, last_nonmenu_event gets the previous key we read.
  7366.      But when a mouse popup menu is being used,
  7367.      we don't update last_nonmenu_event; it continues to hold the mouse
  7368.      event that preceded the first level of menu.  */
  7369.       if (!used_mouse_menu)
  7370.     last_nonmenu_event = key;
  7371.  
  7372.       /* Record what part of this_command_keys is the current key sequence.  */
  7373.       this_single_command_key_start = this_command_key_count - t;
  7374.  
  7375.       prev_fkey_map = fkey_map;
  7376.       prev_fkey_start = fkey_start;
  7377.       prev_fkey_end = fkey_end;
  7378.  
  7379.       prev_keytran_map = keytran_map;
  7380.       prev_keytran_start = keytran_start;
  7381.       prev_keytran_end = keytran_end;
  7382.  
  7383.       /* If the sequence is unbound, see if we can hang a function key
  7384.      off the end of it.  We only want to scan real keyboard input
  7385.      for function key sequences, so if mock_input says that we're
  7386.      re-reading old events, don't examine it.  */
  7387.       if (first_binding >= nmaps
  7388.       && t >= mock_input)
  7389.     {
  7390.       Lisp_Object fkey_next;
  7391.  
  7392.       /* Continue scan from fkey_end until we find a bound suffix.
  7393.          If we fail, increment fkey_start
  7394.          and start fkey_end from there.  */
  7395.       while (fkey_end < t)
  7396.         {
  7397.           Lisp_Object key;
  7398.  
  7399.           key = keybuf[fkey_end++];
  7400.           /* Look up meta-characters by prefixing them
  7401.          with meta_prefix_char.  I hate this.  */
  7402.           if (INTEGERP (key) && XINT (key) & meta_modifier)
  7403.         {
  7404.           fkey_next
  7405.             = get_keymap_1
  7406.               (get_keyelt
  7407.                (access_keymap (fkey_map, meta_prefix_char, 1, 0), 0),
  7408.                0, 1);
  7409.           XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
  7410.         }
  7411.           else
  7412.         fkey_next = fkey_map;
  7413.  
  7414.           fkey_next
  7415.         = get_keyelt (access_keymap (fkey_next, key, 1, 0), 0);
  7416.  
  7417. #if 0 /* I didn't turn this on, because it might cause trouble
  7418.      for the mapping of return into C-m and tab into C-i.  */
  7419.           /* Optionally don't map function keys into other things.
  7420.          This enables the user to redefine kp- keys easily.  */
  7421.           if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
  7422.         fkey_next = Qnil;
  7423. #endif
  7424.  
  7425.           /* If the function key map gives a function, not an
  7426.          array, then call the function with no args and use
  7427.          its value instead.  */
  7428.           if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
  7429.           && fkey_end == t)
  7430.         {
  7431.           struct gcpro gcpro1, gcpro2, gcpro3;
  7432.           Lisp_Object tem;
  7433.           tem = fkey_next;
  7434.  
  7435.           GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
  7436.           fkey_next = call1 (fkey_next, prompt);
  7437.           UNGCPRO;
  7438.           /* If the function returned something invalid,
  7439.              barf--don't ignore it.
  7440.              (To ignore it safely, we would need to gcpro a bunch of
  7441.              other variables.)  */
  7442.           if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
  7443.             error ("Function in key-translation-map returns invalid key sequence");
  7444.         }
  7445.  
  7446.           function_key_possible = ! NILP (fkey_next);
  7447.  
  7448.           /* If keybuf[fkey_start..fkey_end] is bound in the
  7449.          function key map and it's a suffix of the current
  7450.          sequence (i.e. fkey_end == t), replace it with
  7451.          the binding and restart with fkey_start at the end. */
  7452.           if ((VECTORP (fkey_next) || STRINGP (fkey_next))
  7453.           && fkey_end == t)
  7454.         {
  7455.           int len = XFASTINT (Flength (fkey_next));
  7456.  
  7457.           t = fkey_start + len;
  7458.           if (t >= bufsize)
  7459.             error ("Key sequence too long");
  7460.  
  7461.           if (VECTORP (fkey_next))
  7462.             bcopy (XVECTOR (fkey_next)->contents,
  7463.                keybuf + fkey_start,
  7464.                (t - fkey_start) * sizeof (keybuf[0]));
  7465.           else if (STRINGP (fkey_next))
  7466.             {
  7467.               int i;
  7468.  
  7469.               for (i = 0; i < len; i++)
  7470.             XSETFASTINT (keybuf[fkey_start + i],
  7471.                      XSTRING (fkey_next)->data[i]);
  7472.             }
  7473.  
  7474.           mock_input = t;
  7475.           fkey_start = fkey_end = t;
  7476.           fkey_map = Vfunction_key_map;
  7477.  
  7478.           /* Do pass the results through key-translation-map.  */
  7479.           keytran_start = keytran_end = 0;
  7480.           keytran_map = Vkey_translation_map;
  7481.  
  7482.           goto replay_sequence;
  7483.         }
  7484.  
  7485.           fkey_map = get_keymap_1 (fkey_next, 0, 1);
  7486.  
  7487.           /* If we no longer have a bound suffix, try a new positions for
  7488.          fkey_start.  */
  7489.           if (NILP (fkey_map))
  7490.         {
  7491.           fkey_end = ++fkey_start;
  7492.           fkey_map = Vfunction_key_map;
  7493.           function_key_possible = 0;
  7494.         }
  7495.         }
  7496.     }
  7497.  
  7498.       /* Look for this sequence in key-translation-map.  */
  7499.       {
  7500.     Lisp_Object keytran_next;
  7501.  
  7502.     /* Scan from keytran_end until we find a bound suffix.  */
  7503.     while (keytran_end < t)
  7504.       {
  7505.         Lisp_Object key;
  7506.  
  7507.         key = keybuf[keytran_end++];
  7508.         /* Look up meta-characters by prefixing them
  7509.            with meta_prefix_char.  I hate this.  */
  7510.         if (INTEGERP (key) && XINT (key) & meta_modifier)
  7511.           {
  7512.         keytran_next
  7513.           = get_keymap_1
  7514.             (get_keyelt
  7515.              (access_keymap (keytran_map, meta_prefix_char, 1, 0), 0),
  7516.              0, 1);
  7517.         XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
  7518.           }
  7519.         else
  7520.           keytran_next = keytran_map;
  7521.  
  7522.         keytran_next
  7523.           = get_keyelt (access_keymap (keytran_next, key, 1, 0), 0);
  7524.  
  7525.         /* If the key translation map gives a function, not an
  7526.            array, then call the function with no args and use
  7527.            its value instead.  */
  7528.         if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
  7529.         && keytran_end == t)
  7530.           {
  7531.         struct gcpro gcpro1, gcpro2, gcpro3;
  7532.         Lisp_Object tem;
  7533.         tem = keytran_next;
  7534.  
  7535.         GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
  7536.         keytran_next = call1 (keytran_next, prompt);
  7537.         UNGCPRO;
  7538.         /* If the function returned something invalid,
  7539.            barf--don't ignore it.
  7540.            (To ignore it safely, we would need to gcpro a bunch of
  7541.            other variables.)  */
  7542.         if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
  7543.           error ("Function in key-translation-map returns invalid key sequence");
  7544.           }
  7545.  
  7546.         key_translation_possible = ! NILP (keytran_next);
  7547.  
  7548.         /* If keybuf[keytran_start..keytran_end] is bound in the
  7549.            key translation map and it's a suffix of the current
  7550.            sequence (i.e. keytran_end == t), replace it with
  7551.            the binding and restart with keytran_start at the end. */
  7552.         if ((VECTORP (keytran_next) || STRINGP (keytran_next))
  7553.         && keytran_end == t)
  7554.           {
  7555.         int len = XFASTINT (Flength (keytran_next));
  7556.  
  7557.         t = keytran_start + len;
  7558.         if (t >= bufsize)
  7559.           error ("Key sequence too long");
  7560.  
  7561.         if (VECTORP (keytran_next))
  7562.           bcopy (XVECTOR (keytran_next)->contents,
  7563.              keybuf + keytran_start,
  7564.              (t - keytran_start) * sizeof (keybuf[0]));
  7565.         else if (STRINGP (keytran_next))
  7566.           {
  7567.             int i;
  7568.  
  7569.             for (i = 0; i < len; i++)
  7570.               XSETFASTINT (keybuf[keytran_start + i],
  7571.                    XSTRING (keytran_next)->data[i]);
  7572.           }
  7573.  
  7574.         mock_input = t;
  7575.         keytran_start = keytran_end = t;
  7576.         keytran_map = Vkey_translation_map;
  7577.  
  7578.         /* Don't pass the results of key-translation-map
  7579.            through function-key-map.  */
  7580.         fkey_start = fkey_end = t;
  7581.         fkey_map = Vkey_translation_map;
  7582.  
  7583.         goto replay_sequence;
  7584.           }
  7585.  
  7586.         keytran_map = get_keymap_1 (keytran_next, 0, 1);
  7587.  
  7588.         /* If we no longer have a bound suffix, try a new positions for
  7589.            keytran_start.  */
  7590.         if (NILP (keytran_map))
  7591.           {
  7592.         keytran_end = ++keytran_start;
  7593.         keytran_map = Vkey_translation_map;
  7594.         key_translation_possible = 0;
  7595.           }
  7596.       }
  7597.       }
  7598.  
  7599.       /* If KEY is not defined in any of the keymaps,
  7600.      and cannot be part of a function key or translation,
  7601.      and is an upper case letter
  7602.      use the corresponding lower-case letter instead.  */
  7603.       if (first_binding == nmaps && ! function_key_possible
  7604.       && ! key_translation_possible
  7605.       && INTEGERP (key)
  7606.       && ((((XINT (key) & 0x3ffff)
  7607.         < XCHAR_TABLE (current_buffer->downcase_table)->size)
  7608.            && UPPERCASEP (XINT (key) & 0x3ffff))
  7609.           || (XINT (key) & shift_modifier)))
  7610.     {
  7611.       Lisp_Object new_key;
  7612.  
  7613.       original_uppercase = key;
  7614.       original_uppercase_position = t - 1;
  7615.  
  7616.       if (XINT (key) & shift_modifier)
  7617.         XSETINT (new_key, XINT (key) & ~shift_modifier);
  7618.       else
  7619.         XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
  7620.                    | (XINT (key) & ~0x3ffff)));
  7621.  
  7622.       /* We have to do this unconditionally, regardless of whether
  7623.          the lower-case char is defined in the keymaps, because they
  7624.          might get translated through function-key-map.  */
  7625.       keybuf[t - 1] = new_key;
  7626.       mock_input = t;
  7627.  
  7628.       fkey_map = prev_fkey_map;
  7629.       fkey_start = prev_fkey_start;
  7630.       fkey_end = prev_fkey_end;
  7631.  
  7632.       keytran_map = prev_keytran_map;
  7633.       keytran_start = prev_keytran_start;
  7634.       keytran_end = prev_keytran_end;
  7635.  
  7636.       goto replay_sequence;
  7637.     }
  7638.       /* If KEY is not defined in any of the keymaps,
  7639.      and cannot be part of a function key or translation,
  7640.      and is a shifted function key,
  7641.      use the corresponding unshifted function key instead.  */
  7642.       if (first_binding == nmaps && ! function_key_possible
  7643.       && ! key_translation_possible
  7644.       && SYMBOLP (key))
  7645.     {
  7646.       Lisp_Object breakdown;
  7647.       int modifiers;
  7648.  
  7649.       breakdown = parse_modifiers (key);
  7650.       modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
  7651.       if (modifiers & shift_modifier)
  7652.         {
  7653.           Lisp_Object new_key;
  7654.  
  7655.           original_uppercase = key;
  7656.           original_uppercase_position = t - 1;
  7657.  
  7658.           modifiers &= ~shift_modifier;
  7659.           new_key = apply_modifiers (modifiers,
  7660.                      XCONS (breakdown)->car);
  7661.  
  7662.           keybuf[t - 1] = new_key;
  7663.           mock_input = t;
  7664.  
  7665.           fkey_map = prev_fkey_map;
  7666.           fkey_start = prev_fkey_start;
  7667.           fkey_end = prev_fkey_end;
  7668.  
  7669.           keytran_map = prev_keytran_map;
  7670.           keytran_start = prev_keytran_start;
  7671.           keytran_end = prev_keytran_end;
  7672.  
  7673.           goto replay_sequence;
  7674.         }
  7675.     }
  7676.     }
  7677.  
  7678.   if (!dummyflag)
  7679.     read_key_sequence_cmd = (first_binding < nmaps
  7680.                  ? defs[first_binding]
  7681.                  : Qnil);
  7682.  
  7683.   unread_switch_frame = delayed_switch_frame;
  7684.   unbind_to (count, Qnil);
  7685.  
  7686.   /* Don't downcase the last character if the caller says don't.
  7687.      Don't downcase it if the result is undefined, either.  */
  7688.   if ((dont_downcase_last || first_binding >= nmaps)
  7689.       && t - 1 == original_uppercase_position)
  7690.     keybuf[t - 1] = original_uppercase;
  7691.  
  7692.   /* Occasionally we fabricate events, perhaps by expanding something
  7693.      according to function-key-map, or by adding a prefix symbol to a
  7694.      mouse click in the scroll bar or modeline.  In this cases, return
  7695.      the entire generated key sequence, even if we hit an unbound
  7696.      prefix or a definition before the end.  This means that you will
  7697.      be able to push back the event properly, and also means that
  7698.      read-key-sequence will always return a logical unit.
  7699.  
  7700.      Better ideas?  */
  7701.   for (; t < mock_input; t++)
  7702.     {
  7703.       if (echo_keystrokes)
  7704.     echo_char (keybuf[t]);
  7705.       add_command_key (keybuf[t]);
  7706.     }
  7707.  
  7708.   
  7709.  
  7710.   return t;
  7711. }
  7712.  
  7713. #if 0 /* This doc string is too long for some compilers.
  7714.      This commented-out definition serves for DOC.  */
  7715. DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
  7716.   "Read a sequence of keystrokes and return as a string or vector.\n\
  7717. The sequence is sufficient to specify a non-prefix command in the\n\
  7718. current local and global maps.\n\
  7719. \n\
  7720. First arg PROMPT is a prompt string.  If nil, do not prompt specially.\n\
  7721. Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
  7722. as a continuation of the previous key.\n\
  7723. \n\
  7724. The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
  7725. convert the last event to lower case.  (Normally any upper case event\n\
  7726. is converted to lower case if the original event is undefined and the lower\n\
  7727. case equivalent is defined.)  A non-nil value is appropriate for reading\n\
  7728. a key sequence to be defined.\n\
  7729. \n\
  7730. A C-g typed while in this function is treated like any other character,\n\
  7731. and `quit-flag' is not set.\n\
  7732. \n\
  7733. If the key sequence starts with a mouse click, then the sequence is read\n\
  7734. using the keymaps of the buffer of the window clicked in, not the buffer\n\
  7735. of the selected window as normal.\n\
  7736. ""\n\
  7737. `read-key-sequence' drops unbound button-down events, since you normally\n\
  7738. only care about the click or drag events which follow them.  If a drag\n\
  7739. or multi-click event is unbound, but the corresponding click event would\n\
  7740. be bound, `read-key-sequence' turns the event into a click event at the\n\
  7741. drag's starting position.  This means that you don't have to distinguish\n\
  7742. between click and drag, double, or triple events unless you want to.\n\
  7743. \n\
  7744. `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
  7745. lines separating windows, and scroll bars with imaginary keys\n\
  7746. `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
  7747. \n\
  7748. Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
  7749. function will process a switch-frame event if the user switches frames\n\
  7750. before typing anything.  If the user switches frames in the middle of a\n\
  7751. key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
  7752. is nil, then the event will be put off until after the current key sequence.\n\
  7753. \n\
  7754. `read-key-sequence' checks `function-key-map' for function key\n\
  7755. sequences, where they wouldn't conflict with ordinary bindings.  See\n\
  7756. `function-key-map' for more details.\n\
  7757. \n\
  7758. The optional fifth argument COMMAND-LOOP, if non-nil, means\n\
  7759. that this key sequence is being read by something that will\n\
  7760. read commands one after another.  It should be nil if the caller\n\
  7761. will read just one key sequence.")
  7762.   (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop)
  7763. #endif
  7764.  
  7765. DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
  7766.   0)
  7767.   (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
  7768.    command_loop)
  7769.      Lisp_Object prompt, continue_echo, dont_downcase_last;
  7770.      Lisp_Object can_return_switch_frame, command_loop;
  7771. {
  7772.   Lisp_Object keybuf[30];
  7773.   register int i;
  7774.   struct gcpro gcpro1, gcpro2;
  7775.   int count = specpdl_ptr - specpdl;
  7776.  
  7777.   if (!NILP (prompt))
  7778.     CHECK_STRING (prompt, 0);
  7779.   QUIT;
  7780.  
  7781.   specbind (Qinput_method_exit_on_first_char,
  7782.         (NILP (command_loop) ? Qt : Qnil));
  7783.   specbind (Qinput_method_use_echo_area,
  7784.         (NILP (command_loop) ? Qt : Qnil));
  7785.  
  7786.   bzero (keybuf, sizeof keybuf);
  7787.   GCPRO1 (keybuf[0]);
  7788.   gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
  7789.  
  7790.   if (NILP (continue_echo))
  7791.     {
  7792.       this_command_key_count = 0;
  7793.       this_single_command_key_start = 0;
  7794.     }
  7795.  
  7796.   i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
  7797.              prompt, ! NILP (dont_downcase_last),
  7798.              ! NILP (can_return_switch_frame), 0);
  7799.  
  7800.   if (i == -1)
  7801.     {
  7802.       Vquit_flag = Qt;
  7803.       QUIT;
  7804.     }
  7805.   UNGCPRO;
  7806.   return unbind_to (count, make_event_array (i, keybuf));
  7807. }
  7808.  
  7809. DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
  7810.        Sread_key_sequence_vector, 1, 5, 0,
  7811.   "Like `read-key-sequence' but always return a vector.")
  7812.   (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
  7813.    command_loop)
  7814.      Lisp_Object prompt, continue_echo, dont_downcase_last;
  7815.      Lisp_Object can_return_switch_frame, command_loop;
  7816. {
  7817.   Lisp_Object keybuf[30];
  7818.   register int i;
  7819.   struct gcpro gcpro1, gcpro2;
  7820.   int count = specpdl_ptr - specpdl;
  7821.  
  7822.   if (!NILP (prompt))
  7823.     CHECK_STRING (prompt, 0);
  7824.   QUIT;
  7825.  
  7826.   specbind (Qinput_method_exit_on_first_char,
  7827.         (NILP (command_loop) ? Qt : Qnil));
  7828.   specbind (Qinput_method_use_echo_area,
  7829.         (NILP (command_loop) ? Qt : Qnil));
  7830.  
  7831.   bzero (keybuf, sizeof keybuf);
  7832.   GCPRO1 (keybuf[0]);
  7833.   gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
  7834.  
  7835.   if (NILP (continue_echo))
  7836.     {
  7837.       this_command_key_count = 0;
  7838.       this_single_command_key_start = 0;
  7839.     }
  7840.  
  7841.   i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
  7842.              prompt, ! NILP (dont_downcase_last),
  7843.              ! NILP (can_return_switch_frame), 0);
  7844.  
  7845.   if (i == -1)
  7846.     {
  7847.       Vquit_flag = Qt;
  7848.       QUIT;
  7849.     }
  7850.   UNGCPRO;
  7851.   return unbind_to (count, Fvector (i, keybuf));
  7852. }
  7853.  
  7854. DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
  7855.  "Execute CMD as an editor command.\n\
  7856. CMD must be a symbol that satisfies the `commandp' predicate.\n\
  7857. Optional second arg RECORD-FLAG non-nil\n\
  7858. means unconditionally put this command in `command-history'.\n\
  7859. Otherwise, that is done only if an arg is read using the minibuffer.\n\
  7860. The argument KEYS specifies the value to use instead of (this-command-keys)\n\
  7861. when reading the arguments; if it is nil, (this-command-keys) is used.\n\
  7862. The argument SPECIAL, if non-nil, means that this command is executing\n\
  7863. a special event, so ignore the prefix argument and don't clear it.")
  7864.      (cmd, record_flag, keys, special)
  7865.      Lisp_Object cmd, record_flag, keys, special;
  7866. {
  7867.   register Lisp_Object final;
  7868.   register Lisp_Object tem;
  7869.   Lisp_Object prefixarg;
  7870.   struct backtrace backtrace;
  7871.   extern int debug_on_next_call;
  7872.  
  7873.   debug_on_next_call = 0;
  7874.  
  7875.   if (NILP (special))
  7876.     {
  7877.       prefixarg = current_kboard->Vprefix_arg;
  7878.       Vcurrent_prefix_arg = prefixarg;
  7879.       current_kboard->Vprefix_arg = Qnil;
  7880.     }
  7881.   else
  7882.     prefixarg = Qnil;
  7883.  
  7884.   if (SYMBOLP (cmd))
  7885.     {
  7886.       tem = Fget (cmd, Qdisabled);
  7887.       if (!NILP (tem) && !NILP (Vrun_hooks))
  7888.     {
  7889.       tem = Fsymbol_value (Qdisabled_command_hook);
  7890.       if (!NILP (tem))
  7891.         return call1 (Vrun_hooks, Qdisabled_command_hook);
  7892.     }
  7893.     }
  7894.  
  7895.   while (1)
  7896.     {
  7897.       final = Findirect_function (cmd);
  7898.  
  7899.       if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
  7900.     {
  7901.       struct gcpro gcpro1, gcpro2;
  7902.  
  7903.       GCPRO2 (cmd, prefixarg);
  7904.       do_autoload (final, cmd);
  7905.       UNGCPRO;
  7906.     }
  7907.       else
  7908.     break;
  7909.     }
  7910.  
  7911.   if (STRINGP (final) || VECTORP (final))
  7912.     {
  7913.       /* If requested, place the macro in the command history.  For
  7914.      other sorts of commands, call-interactively takes care of
  7915.      this.  */
  7916.       if (!NILP (record_flag))
  7917.     {
  7918.       Vcommand_history
  7919.         = Fcons (Fcons (Qexecute_kbd_macro,
  7920.                 Fcons (final, Fcons (prefixarg, Qnil))),
  7921.              Vcommand_history);
  7922.  
  7923.       /* Don't keep command history around forever.  */
  7924.       if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
  7925.         {
  7926.           tem = Fnthcdr (Vhistory_length, Vcommand_history);
  7927.           if (CONSP (tem))
  7928.         XCONS (tem)->cdr = Qnil;
  7929.         }
  7930.     }
  7931.  
  7932.       return Fexecute_kbd_macro (final, prefixarg);
  7933.     }
  7934.  
  7935.   if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
  7936.     {
  7937.       backtrace.next = backtrace_list;
  7938.       backtrace_list = &backtrace;
  7939.       backtrace.function = &Qcall_interactively;
  7940.       backtrace.args = &cmd;
  7941.       backtrace.nargs = 1;
  7942.       backtrace.evalargs = 0;
  7943.  
  7944.       tem = Fcall_interactively (cmd, record_flag, keys);
  7945.  
  7946.       backtrace_list = backtrace.next;
  7947.       return tem;
  7948.     }
  7949.   return Qnil;
  7950. }
  7951.  
  7952. DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
  7953.   1, 1, "P",
  7954.   "Read function name, then read its arguments and call it.")
  7955.   (prefixarg)
  7956.      Lisp_Object prefixarg;
  7957. {
  7958.   Lisp_Object function;
  7959.   char buf[40];
  7960.   Lisp_Object saved_keys;
  7961.   Lisp_Object bindings, value;
  7962.   struct gcpro gcpro1, gcpro2;
  7963.  
  7964.   saved_keys = Fvector (this_command_key_count,
  7965.             XVECTOR (this_command_keys)->contents);
  7966.   buf[0] = 0;
  7967.   GCPRO2 (saved_keys, prefixarg);
  7968.  
  7969.   if (EQ (prefixarg, Qminus))
  7970.     strcpy (buf, "- ");
  7971.   else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
  7972.     strcpy (buf, "C-u ");
  7973.   else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
  7974.     {
  7975.       if (sizeof (int) == sizeof (EMACS_INT))
  7976.     sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
  7977.       else if (sizeof (long) == sizeof (EMACS_INT))
  7978.     sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
  7979.       else
  7980.     abort ();
  7981.     }
  7982.   else if (INTEGERP (prefixarg))
  7983.     {
  7984.       if (sizeof (int) == sizeof (EMACS_INT))
  7985.     sprintf (buf, "%d ", XINT (prefixarg));
  7986.       else if (sizeof (long) == sizeof (EMACS_INT))
  7987.     sprintf (buf, "%ld ", XINT (prefixarg));
  7988.       else
  7989.     abort ();
  7990.     }
  7991.  
  7992.   /* This isn't strictly correct if execute-extended-command
  7993.      is bound to anything else.  Perhaps it should use
  7994.      this_command_keys?  */
  7995.   strcat (buf, "M-x ");
  7996.  
  7997.   /* Prompt with buf, and then read a string, completing from and
  7998.      restricting to the set of all defined commands.  Don't provide
  7999.      any initial input.  Save the command read on the extended-command
  8000.      history list. */
  8001.   function = Fcompleting_read (build_string (buf),
  8002.                    Vobarray, Qcommandp,
  8003.                    Qt, Qnil, Qextended_command_history, Qnil,
  8004.                    Qnil);
  8005.  
  8006.   if (STRINGP (function) && XSTRING (function)->size == 0)
  8007.     error ("No command name given");
  8008.  
  8009.   /* Set this_command_keys to the concatenation of saved_keys and
  8010.      function, followed by a RET.  */
  8011.   {
  8012.     struct Lisp_String *str;
  8013.     Lisp_Object *keys;
  8014.     int i;
  8015.  
  8016.     this_command_key_count = 0;
  8017.     this_single_command_key_start = 0;
  8018.  
  8019.     keys = XVECTOR (saved_keys)->contents;
  8020.     for (i = 0; i < XVECTOR (saved_keys)->size; i++)
  8021.       add_command_key (keys[i]);
  8022.  
  8023.     str = XSTRING (function);
  8024.     for (i = 0; i < str->size; i++)
  8025.       add_command_key (Faref (function, make_number (i)));
  8026.  
  8027.     add_command_key (make_number ('\015'));
  8028.   }
  8029.  
  8030.   UNGCPRO;
  8031.  
  8032.   function = Fintern (function, Qnil);
  8033.   current_kboard->Vprefix_arg = prefixarg;
  8034.   Vthis_command = function;
  8035.   real_this_command = function;
  8036.  
  8037.   /* If enabled, show which key runs this command.  */
  8038.   if (!NILP (Vsuggest_key_bindings)
  8039.       && NILP (Vexecuting_macro)
  8040.       && SYMBOLP (function))
  8041.     bindings = Fwhere_is_internal (function, Voverriding_local_map,
  8042.                    Qt, Qnil);
  8043.   else
  8044.     bindings = Qnil;
  8045.  
  8046.   value = Qnil;
  8047.   GCPRO2 (bindings, value);
  8048.   value = Fcommand_execute (function, Qt, Qnil, Qnil);
  8049.  
  8050.   /* If the command has a key binding, print it now.  */
  8051.   if (!NILP (bindings)
  8052.       && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
  8053.                       Qmouse_movement)))
  8054.     {
  8055.       /* But first wait, and skip the message if there is input.  */
  8056.       int delay_time;
  8057.       if (echo_area_glyphs != 0)
  8058.     /* This command displayed something in the echo area;
  8059.        so wait a few seconds, then display our suggestion message.  */
  8060.     delay_time = (NUMBERP (Vsuggest_key_bindings)
  8061.               ? XINT (Vsuggest_key_bindings) : 2);
  8062.       else
  8063.     /* This command left the echo area empty,
  8064.        so display our message immediately.  */
  8065.     delay_time = 0;
  8066.  
  8067.       if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
  8068.       && ! CONSP (Vunread_command_events))
  8069.     {
  8070.       Lisp_Object binding;
  8071.       char *newmessage;
  8072.       char *oldmessage = echo_area_glyphs;
  8073.       int oldmessage_len = echo_area_glyphs_length;
  8074.       int oldmultibyte = message_enable_multibyte;
  8075.  
  8076.       binding = Fkey_description (bindings);
  8077.  
  8078.       newmessage
  8079.         = (char *) alloca (XSYMBOL (function)->name->size
  8080.                    + STRING_BYTES (XSTRING (binding))
  8081.                    + 100);
  8082.       sprintf (newmessage, "You can run the command `%s' with %s",
  8083.            XSYMBOL (function)->name->data,
  8084.            XSTRING (binding)->data);
  8085.       message2_nolog (newmessage,
  8086.               strlen (newmessage),
  8087.               STRING_MULTIBYTE (binding));
  8088.       if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
  8089.                 ? Vsuggest_key_bindings : make_number (2)),
  8090.                    Qnil, Qnil)))
  8091.         message2_nolog (oldmessage, oldmessage_len, oldmultibyte);
  8092.     }
  8093.     }
  8094.  
  8095.   RETURN_UNGCPRO (value);
  8096. }
  8097.  
  8098. /* Find the set of keymaps now active.
  8099.    Store into *MAPS_P a vector holding the various maps
  8100.    and return the number of them.  The vector was malloc'd
  8101.    and the caller should free it.  */
  8102.  
  8103. int
  8104. current_active_maps (maps_p)
  8105.      Lisp_Object **maps_p;
  8106. {
  8107.   Lisp_Object *tmaps, *maps;
  8108.   int nmaps;
  8109.  
  8110.   /* Should overriding-terminal-local-map and overriding-local-map apply?  */
  8111.   if (!NILP (Voverriding_local_map_menu_flag))
  8112.     {
  8113.       /* Yes, use them (if non-nil) as well as the global map.  */
  8114.       maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
  8115.       nmaps = 0;
  8116.       if (!NILP (current_kboard->Voverriding_terminal_local_map))
  8117.     maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
  8118.       if (!NILP (Voverriding_local_map))
  8119.     maps[nmaps++] = Voverriding_local_map;
  8120.     }
  8121.   else
  8122.     {
  8123.       /* No, so use major and minor mode keymaps.  */
  8124.       nmaps = current_minor_maps (NULL, &tmaps);
  8125.       maps = (Lisp_Object *) xmalloc ((nmaps + 2) * sizeof (maps[0]));
  8126.       bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
  8127. #ifdef USE_TEXT_PROPERTIES
  8128.       maps[nmaps++] = get_local_map (PT, current_buffer);
  8129. #else
  8130.       maps[nmaps++] = current_buffer->keymap;
  8131. #endif
  8132.     }
  8133.   maps[nmaps++] = current_global_map;
  8134.  
  8135.   *maps_p = maps;
  8136.   return nmaps;
  8137. }
  8138.  
  8139. /* Return nonzero if input events are pending.  */
  8140.  
  8141. int
  8142. detect_input_pending ()
  8143. {
  8144.   if (!input_pending)
  8145.     get_input_pending (&input_pending, 0);
  8146.  
  8147.   return input_pending;
  8148. }
  8149.  
  8150. /* Return nonzero if input events are pending, and run any pending timers.  */
  8151.  
  8152. int
  8153. detect_input_pending_run_timers (do_display)
  8154.      int do_display;
  8155. {
  8156.   int old_timers_run = timers_run;
  8157.  
  8158.   if (!input_pending)
  8159.     get_input_pending (&input_pending, 1);
  8160.  
  8161.   if (old_timers_run != timers_run && do_display)
  8162.     redisplay_preserve_echo_area ();
  8163.  
  8164.   return input_pending;
  8165. }
  8166.  
  8167. /* This is called in some cases before a possible quit.
  8168.    It cases the next call to detect_input_pending to recompute input_pending.
  8169.    So calling this function unnecessarily can't do any harm.  */
  8170.  
  8171. void
  8172. clear_input_pending ()
  8173. {
  8174.   input_pending = 0;
  8175. }
  8176.  
  8177. /* Return nonzero if there are pending requeued events.
  8178.    This isn't used yet.  The hope is to make wait_reading_process_input
  8179.    call it, and return return if it runs Lisp code that unreads something.
  8180.    The problem is, kbd_buffer_get_event needs to be fixed to know what
  8181.    to do in that case.  It isn't trivial.  */
  8182.  
  8183. int
  8184. requeued_events_pending_p ()
  8185. {
  8186.   return (!NILP (Vunread_command_events) || unread_command_char != -1);
  8187. }
  8188.  
  8189.  
  8190. DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
  8191.   "T if command input is currently available with no waiting.\n\
  8192. Actually, the value is nil only if we can be sure that no input is available.")
  8193.   ()
  8194. {
  8195.   if (!NILP (Vunread_command_events) || unread_command_char != -1)
  8196.     return (Qt);
  8197.  
  8198.   get_input_pending (&input_pending, 1);
  8199.   return input_pending > 0 ? Qt : Qnil;
  8200. }
  8201.  
  8202. DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
  8203.   "Return vector of last 100 events, not counting those from keyboard macros.")
  8204.   ()
  8205. {
  8206.   Lisp_Object *keys = XVECTOR (recent_keys)->contents;
  8207.   Lisp_Object val;
  8208.  
  8209.   if (total_keys < NUM_RECENT_KEYS)
  8210.     return Fvector (total_keys, keys);
  8211.   else
  8212.     {
  8213.       val = Fvector (NUM_RECENT_KEYS, keys);
  8214.       bcopy (keys + recent_keys_index,
  8215.          XVECTOR (val)->contents,
  8216.          (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
  8217.       bcopy (keys,
  8218.          XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
  8219.          recent_keys_index * sizeof (Lisp_Object));
  8220.       return val;
  8221.     }
  8222. }
  8223.  
  8224. DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
  8225.   "Return the key sequence that invoked this command.\n\
  8226. The value is a string or a vector.")
  8227.   ()
  8228. {
  8229.   return make_event_array (this_command_key_count,
  8230.                XVECTOR (this_command_keys)->contents);
  8231. }
  8232.  
  8233. DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
  8234.   "Return the key sequence that invoked this command, as a vector.")
  8235.   ()
  8236. {
  8237.   return Fvector (this_command_key_count,
  8238.           XVECTOR (this_command_keys)->contents);
  8239. }
  8240.  
  8241. DEFUN ("this-single-command-keys", Fthis_single_command_keys,
  8242.        Sthis_single_command_keys, 0, 0, 0,
  8243.   "Return the key sequence that invoked this command.\n\
  8244. Unlike `this-command-keys', this function's value\n\
  8245. does not include prefix arguments.\n\
  8246. The value is always a vector.")
  8247.   ()
  8248. {
  8249.   return Fvector (this_command_key_count
  8250.           - this_single_command_key_start,
  8251.           (XVECTOR (this_command_keys)->contents
  8252.            + this_single_command_key_start));
  8253. }
  8254.  
  8255. DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
  8256.        Sthis_single_command_raw_keys, 0, 0, 0,
  8257.   "Return the raw events that were read for this command.\n\
  8258. Unlike `this-single-command-keys', this function's value\n\
  8259. shows the events before all translations (except for input methods).\n\
  8260. The value is always a vector.")
  8261.   ()
  8262. {
  8263.   return Fvector (raw_keybuf_count,
  8264.           (XVECTOR (raw_keybuf)->contents));
  8265. }
  8266.  
  8267. DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
  8268.   Sreset_this_command_lengths, 0, 0, 0,
  8269.   "Used for complicated reasons in `universal-argument-other-key'.\n\
  8270. \n\
  8271. `universal-argument-other-key' rereads the event just typed.\n\
  8272. It then gets translated through `function-key-map'.\n\
  8273. The translated event gets included in the echo area and in\n\
  8274. the value of `this-command-keys' in addition to the raw original event.\n\
  8275. That is not right.\n\
  8276. \n\
  8277. Calling this function directs the translated event to replace\n\
  8278. the original event, so that only one version of the event actually\n\
  8279. appears in the echo area and in the value of `this-command-keys.'.")
  8280.   ()
  8281. {
  8282.   before_command_restore_flag = 1;
  8283.   before_command_key_count_1 = before_command_key_count;
  8284.   before_command_echo_length_1 = before_command_echo_length;
  8285.   return Qnil;
  8286. }
  8287.  
  8288. DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
  8289.   "Return the current depth in recursive edits.")
  8290.   ()
  8291. {
  8292.   Lisp_Object temp;
  8293.   XSETFASTINT (temp, command_loop_level + minibuf_level);
  8294.   return temp;
  8295. }
  8296.  
  8297. DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
  8298.   "FOpen dribble file: ",
  8299.   "Start writing all keyboard characters to a dribble file called FILE.\n\
  8300. If FILE is nil, close any open dribble file.")
  8301.   (file)
  8302.      Lisp_Object file;
  8303. {
  8304.   if (dribble)
  8305.     {
  8306.       fclose (dribble);
  8307.       dribble = 0;
  8308.     }
  8309.   if (!NILP (file))
  8310.     {
  8311.       file = Fexpand_file_name (file, Qnil);
  8312.       dribble = fopen (XSTRING (file)->data, "w");
  8313.       if (dribble == 0)
  8314.     report_file_error ("Opening dribble", Fcons (file, Qnil));
  8315.     }
  8316.   return Qnil;
  8317. }
  8318.  
  8319. DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
  8320.   "Discard the contents of the terminal input buffer.\n\
  8321. Also cancel any kbd macro being defined.")
  8322.   ()
  8323. {
  8324.   current_kboard->defining_kbd_macro = Qnil;
  8325.   update_mode_lines++;
  8326.  
  8327.   Vunread_command_events = Qnil;
  8328.   unread_command_char = -1;
  8329.  
  8330.   discard_tty_input ();
  8331.  
  8332.   /* Without the cast, GCC complains that this assignment loses the
  8333.      volatile qualifier of kbd_store_ptr.  Is there anything wrong
  8334.      with that?  */
  8335.   kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
  8336.   Ffillarray (kbd_buffer_frame_or_window, Qnil);
  8337.   input_pending = 0;
  8338.  
  8339.   return Qnil;
  8340. }
  8341.  
  8342. DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
  8343.   "Stop Emacs and return to superior process.  You can resume later.\n\
  8344. If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
  8345. control, run a subshell instead.\n\n\
  8346. If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
  8347. to be read as terminal input by Emacs's parent, after suspension.\n\
  8348. \n\
  8349. Before suspending, run the normal hook `suspend-hook'.\n\
  8350. After resumption run the normal hook `suspend-resume-hook'.\n\
  8351. \n\
  8352. Some operating systems cannot stop the Emacs process and resume it later.\n\
  8353. On such systems, Emacs starts a subshell instead of suspending.")
  8354.   (stuffstring)
  8355.      Lisp_Object stuffstring;
  8356. {
  8357.   Lisp_Object tem;
  8358.   int count = specpdl_ptr - specpdl;
  8359.   int old_height, old_width;
  8360.   int width, height;
  8361.   struct gcpro gcpro1, gcpro2;
  8362.  
  8363.   if (!NILP (stuffstring))
  8364.     CHECK_STRING (stuffstring, 0);
  8365.  
  8366.   /* Run the functions in suspend-hook.  */
  8367.   if (!NILP (Vrun_hooks))
  8368.     call1 (Vrun_hooks, intern ("suspend-hook"));
  8369.  
  8370.   GCPRO1 (stuffstring);
  8371.   get_frame_size (&old_width, &old_height);
  8372.   reset_sys_modes ();
  8373.   /* sys_suspend can get an error if it tries to fork a subshell
  8374.      and the system resources aren't available for that.  */
  8375.   record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
  8376.              Qnil);
  8377.   stuff_buffered_input (stuffstring);
  8378.   if (cannot_suspend)
  8379.     sys_subshell ();
  8380.   else
  8381.     sys_suspend ();
  8382.   unbind_to (count, Qnil);
  8383.  
  8384.   /* Check if terminal/window size has changed.
  8385.      Note that this is not useful when we are running directly
  8386.      with a window system; but suspend should be disabled in that case.  */
  8387.   get_frame_size (&width, &height);
  8388.   if (width != old_width || height != old_height)
  8389.     change_frame_size (selected_frame, height, width, 0, 0);
  8390.  
  8391.   /* Run suspend-resume-hook.  */
  8392.   if (!NILP (Vrun_hooks))
  8393.     call1 (Vrun_hooks, intern ("suspend-resume-hook"));
  8394.  
  8395.   UNGCPRO;
  8396.   return Qnil;
  8397. }
  8398.  
  8399. /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
  8400.    Then in any case stuff anything Emacs has read ahead and not used.  */
  8401.  
  8402. void
  8403. stuff_buffered_input (stuffstring)
  8404.      Lisp_Object stuffstring;
  8405. {
  8406. /* stuff_char works only in BSD, versions 4.2 and up.  */
  8407. #ifdef BSD_SYSTEM
  8408. #ifndef BSD4_1
  8409.   register unsigned char *p;
  8410.  
  8411.   if (STRINGP (stuffstring))
  8412.     {
  8413.       register int count;
  8414.  
  8415.       p = XSTRING (stuffstring)->data;
  8416.       count = STRING_BYTES (XSTRING (stuffstring));
  8417.       while (count-- > 0)
  8418.     stuff_char (*p++);
  8419.       stuff_char ('\n');
  8420.     }
  8421.   /* Anything we have read ahead, put back for the shell to read.  */
  8422.   /* ?? What should this do when we have multiple keyboards??
  8423.      Should we ignore anything that was typed in at the "wrong" kboard?  */
  8424.   for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
  8425.     {
  8426.       if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
  8427.     kbd_fetch_ptr = kbd_buffer;
  8428.       if (kbd_fetch_ptr->kind == ascii_keystroke)
  8429.     stuff_char (kbd_fetch_ptr->code);
  8430.       kbd_fetch_ptr->kind = no_event;
  8431.       (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
  8432.                               - kbd_buffer]
  8433.        = Qnil);
  8434.     }
  8435.   input_pending = 0;
  8436. #endif
  8437. #endif /* BSD_SYSTEM and not BSD4_1 */
  8438. }
  8439.  
  8440. void
  8441. set_waiting_for_input (time_to_clear)
  8442.      EMACS_TIME *time_to_clear;
  8443. {
  8444.   input_available_clear_time = time_to_clear;
  8445.  
  8446.   /* Tell interrupt_signal to throw back to read_char,  */
  8447.   waiting_for_input = 1;
  8448.  
  8449.   /* If interrupt_signal was called before and buffered a C-g,
  8450.      make it run again now, to avoid timing error. */
  8451.   if (!NILP (Vquit_flag))
  8452.     quit_throw_to_read_char ();
  8453. }
  8454.  
  8455. void
  8456. clear_waiting_for_input ()
  8457. {
  8458.   /* Tell interrupt_signal not to throw back to read_char,  */
  8459.   waiting_for_input = 0;
  8460.   input_available_clear_time = 0;
  8461. }
  8462.  
  8463. /* This routine is called at interrupt level in response to C-G.
  8464.  If interrupt_input, this is the handler for SIGINT.
  8465.  Otherwise, it is called from kbd_buffer_store_event,
  8466.  in handling SIGIO or SIGTINT.
  8467.  
  8468.  If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
  8469.  immediately throw back to read_char.
  8470.  
  8471.  Otherwise it sets the Lisp variable  quit-flag  not-nil.
  8472.  This causes  eval  to throw, when it gets a chance.
  8473.  If  quit-flag  is already non-nil, it stops the job right away.  */
  8474.  
  8475. SIGTYPE
  8476. interrupt_signal (signalnum)    /* If we don't have an argument, */
  8477.      int signalnum;        /* some compilers complain in signal calls. */
  8478. {
  8479.   char c;
  8480.   /* Must preserve main program's value of errno.  */
  8481.   int old_errno = errno;
  8482.  
  8483. #ifdef EMX
  8484.   /* Under emx with non-POSIX signals, a signal is blocked until
  8485.      acknowledged. */
  8486. #ifndef POSIX_SIGNALS
  8487.   signal (SIGINT, SIG_ACK);
  8488. #endif
  8489. #else /* not EMX */
  8490. #if defined (USG) && !defined (POSIX_SIGNALS)
  8491.   if (!read_socket_hook && NILP (Vwindow_system))
  8492.     {
  8493.       /* USG systems forget handlers when they are used;
  8494.      must reestablish each time */
  8495.       signal (SIGINT, interrupt_signal);
  8496.       signal (SIGQUIT, interrupt_signal);
  8497.     }
  8498. #endif /* USG */
  8499. #endif /* not EMX */
  8500.  
  8501.   cancel_echoing ();
  8502.  
  8503.   if (!NILP (Vquit_flag)
  8504.       && (FRAME_TERMCAP_P (selected_frame) || FRAME_MSDOS_P (selected_frame)))
  8505.     {
  8506.       /* If SIGINT isn't blocked, don't let us be interrupted by
  8507.      another SIGINT, it might be harmful due to non-reentrancy
  8508.      in I/O functions.  */
  8509.       sigblock (sigmask (SIGINT));
  8510.  
  8511.       fflush (stdout);
  8512.       reset_sys_modes ();
  8513.  
  8514. #ifdef SIGTSTP            /* Support possible in later USG versions */
  8515. /*
  8516.  * On systems which can suspend the current process and return to the original
  8517.  * shell, this command causes the user to end up back at the shell.
  8518.  * The "Auto-save" and "Abort" questions are not asked until
  8519.  * the user elects to return to emacs, at which point he can save the current
  8520.  * job and either dump core or continue.
  8521.  */
  8522.       sys_suspend ();
  8523. #else
  8524. #ifdef VMS
  8525.       if (sys_suspend () == -1)
  8526.     {
  8527.       printf ("Not running as a subprocess;\n");
  8528.       printf ("you can continue or abort.\n");
  8529.     }
  8530. #else /* not VMS */
  8531.       /* Perhaps should really fork an inferior shell?
  8532.      But that would not provide any way to get back
  8533.      to the original shell, ever.  */
  8534.       printf ("No support for stopping a process on this operating system;\n");
  8535.       printf ("you can continue or abort.\n");
  8536. #endif /* not VMS */
  8537. #endif /* not SIGTSTP */
  8538. #ifdef EMX
  8539.       /* Still required??? */
  8540.       Vquit_flag = Qnil;
  8541. #endif */ EMX */
  8542. #ifdef MSDOS
  8543.       /* We must remain inside the screen area when the internal terminal
  8544.      is used.  Note that [Enter] is not echoed by dos.  */
  8545.       cursor_to (0, 0);
  8546. #endif
  8547.       /* It doesn't work to autosave while GC is in progress;
  8548.      the code used for auto-saving doesn't cope with the mark bit.  */
  8549.       if (!gc_in_progress)
  8550.     {
  8551.       printf ("Auto-save? (y or n) ");
  8552.       fflush (stdout);
  8553.       if (((c = getchar ()) & ~040) == 'Y')
  8554.         {
  8555.           Fdo_auto_save (Qt, Qnil);
  8556. #ifdef MSDOS
  8557.           printf ("\r\nAuto-save done");
  8558. #else /* not MSDOS */
  8559.           printf ("Auto-save done\n");
  8560. #endif /* not MSDOS */
  8561.         }
  8562.       while (c != '\n') c = getchar ();
  8563.     }
  8564.       else 
  8565.     {
  8566.       /* During GC, it must be safe to reenable quitting again.  */
  8567.       Vinhibit_quit = Qnil;
  8568. #ifdef MSDOS
  8569.       printf ("\r\n");
  8570. #endif /* not MSDOS */
  8571.       printf ("Garbage collection in progress; cannot auto-save now\r\n");
  8572.       printf ("but will instead do a real quit after garbage collection ends\r\n");
  8573.       fflush (stdout);
  8574.     }
  8575.  
  8576. #ifdef MSDOS
  8577.       printf ("\r\nAbort?  (y or n) ");
  8578. #else /* not MSDOS */
  8579. #ifdef VMS
  8580.       printf ("Abort (and enter debugger)? (y or n) ");
  8581. #else /* not VMS */
  8582.       printf ("Abort (and dump core)? (y or n) ");
  8583. #endif /* not VMS */
  8584. #endif /* not MSDOS */
  8585.       fflush (stdout);
  8586.       if (((c = getchar ()) & ~040) == 'Y')
  8587.     abort ();
  8588.       while (c != '\n') c = getchar ();
  8589. #ifdef MSDOS
  8590.       printf ("\r\nContinuing...\r\n");
  8591. #else /* not MSDOS */
  8592.       printf ("Continuing...\n");
  8593. #endif /* not MSDOS */
  8594.       fflush (stdout);
  8595.       init_sys_modes ();
  8596.       sigfree ();
  8597.     }
  8598.   else
  8599.     {
  8600.       /* If executing a function that wants to be interrupted out of
  8601.      and the user has not deferred quitting by binding `inhibit-quit'
  8602.      then quit right away.  */
  8603.       if (immediate_quit && NILP (Vinhibit_quit))
  8604.     {
  8605.       struct gl_state_s saved;
  8606.       struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  8607.  
  8608.       immediate_quit = 0;
  8609.           sigfree ();
  8610.       saved = gl_state;
  8611.       GCPRO4 (saved.object, saved.global_code,
  8612.           saved.current_syntax_table, saved.old_prop);
  8613.       Fsignal (Qquit, Qnil);
  8614.       gl_state = saved;
  8615.       UNGCPRO;
  8616.     }
  8617.       else
  8618.     /* Else request quit when it's safe */
  8619.     Vquit_flag = Qt;
  8620.     }
  8621.  
  8622.   if (waiting_for_input && !echoing)
  8623.     quit_throw_to_read_char ();
  8624.  
  8625.   errno = old_errno;
  8626. }
  8627.  
  8628. /* Handle a C-g by making read_char return C-g.  */
  8629.  
  8630. void
  8631. quit_throw_to_read_char ()
  8632. {
  8633.   quit_error_check ();
  8634.   sigfree ();
  8635.   /* Prevent another signal from doing this before we finish.  */
  8636.   clear_waiting_for_input ();
  8637.   input_pending = 0;
  8638.  
  8639.   Vunread_command_events = Qnil;
  8640.   unread_command_char = -1;
  8641.  
  8642. #if 0 /* Currently, sit_for is called from read_char without turning
  8643.      off polling.  And that can call set_waiting_for_input.
  8644.      It seems to be harmless.  */
  8645. #ifdef POLL_FOR_INPUT
  8646.   /* May be > 1 if in recursive minibuffer.  */
  8647.   if (poll_suppress_count == 0)
  8648.     abort ();
  8649. #endif
  8650. #endif
  8651.   if (FRAMEP (internal_last_event_frame)
  8652.       && XFRAME (internal_last_event_frame) != selected_frame)
  8653.     do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
  8654.              Qnil, 0);
  8655.  
  8656.   _longjmp (getcjmp, 1);
  8657. }
  8658.  
  8659. DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
  8660.   "Set mode of reading keyboard input.\n\
  8661. First arg INTERRUPT non-nil means use input interrupts;\n\
  8662.  nil means use CBREAK mode.\n\
  8663. Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
  8664.  (no effect except in CBREAK mode).\n\
  8665. Third arg META t means accept 8-bit input (for a Meta key).\n\
  8666.  META nil means ignore the top bit, on the assumption it is parity.\n\
  8667.  Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
  8668. Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
  8669. See also `current-input-mode'.")
  8670.   (interrupt, flow, meta, quit)
  8671.      Lisp_Object interrupt, flow, meta, quit;
  8672. {
  8673.   if (!NILP (quit)
  8674.       && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
  8675.     error ("set-input-mode: QUIT must be an ASCII character");
  8676.  
  8677. #ifdef POLL_FOR_INPUT
  8678.   stop_polling ();
  8679. #endif
  8680.  
  8681. #ifndef DOS_NT
  8682.   /* this causes startup screen to be restored and messes with the mouse */
  8683.   reset_sys_modes ();
  8684. #endif
  8685.  
  8686. #ifdef SIGIO
  8687. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  8688.   if (read_socket_hook)
  8689.     {
  8690.       /* When using X, don't give the user a real choice,
  8691.      because we haven't implemented the mechanisms to support it.  */
  8692. #ifdef NO_SOCK_SIGIO
  8693.       interrupt_input = 0;
  8694. #else /* not NO_SOCK_SIGIO */
  8695.       interrupt_input = 1;
  8696. #endif /* NO_SOCK_SIGIO */
  8697.     }
  8698.   else
  8699.     interrupt_input = !NILP (interrupt);
  8700. #else /* not SIGIO */
  8701.   interrupt_input = 0;
  8702. #endif /* not SIGIO */
  8703.  
  8704. /* Our VMS input only works by interrupts, as of now.  */
  8705. #ifdef VMS
  8706.   interrupt_input = 1;
  8707. #endif
  8708.  
  8709.   flow_control = !NILP (flow);
  8710.   if (NILP (meta))
  8711.     meta_key = 0;
  8712.   else if (EQ (meta, Qt))
  8713.     meta_key = 1;
  8714.   else
  8715.     meta_key = 2;
  8716.   if (!NILP (quit))
  8717.     /* Don't let this value be out of range.  */
  8718.     quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
  8719.  
  8720. #ifndef DOS_NT
  8721.   init_sys_modes ();
  8722. #endif
  8723.  
  8724. #ifdef POLL_FOR_INPUT
  8725.   poll_suppress_count = 1;
  8726.   start_polling ();
  8727. #endif
  8728.   return Qnil;
  8729. }
  8730.  
  8731. DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
  8732.   "Return information about the way Emacs currently reads keyboard input.\n\
  8733. The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
  8734.   INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
  8735.     nil, Emacs is using CBREAK mode.\n\
  8736.   FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
  8737.     terminal; this does not apply if Emacs uses interrupt-driven input.\n\
  8738.   META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
  8739.     META nil means ignoring the top bit, on the assumption it is parity.\n\
  8740.     META is neither t nor nil if accepting 8-bit input and using\n\
  8741.     all 8 bits as the character code.\n\
  8742.   QUIT is the character Emacs currently uses to quit.\n\
  8743. The elements of this list correspond to the arguments of\n\
  8744. `set-input-mode'.")
  8745.   ()
  8746. {
  8747.   Lisp_Object val[4];
  8748.  
  8749.   val[0] = interrupt_input ? Qt : Qnil;
  8750.   val[1] = flow_control ? Qt : Qnil;
  8751.   val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
  8752.   XSETFASTINT (val[3], quit_char);
  8753.  
  8754.   return Flist (sizeof (val) / sizeof (val[0]), val);
  8755. }
  8756.  
  8757.  
  8758. /*
  8759.  * Set up a new kboard object with reasonable initial values.
  8760.  */
  8761. void
  8762. init_kboard (kb)
  8763.      KBOARD *kb;
  8764. {
  8765.   kb->Voverriding_terminal_local_map = Qnil;
  8766.   kb->Vlast_command = Qnil;
  8767.   kb->Vreal_last_command = Qnil;
  8768.   kb->Vprefix_arg = Qnil;
  8769.   kb->Vlast_prefix_arg = Qnil;
  8770.   kb->kbd_queue = Qnil;
  8771.   kb->kbd_queue_has_data = 0;
  8772.   kb->immediate_echo = 0;
  8773.   kb->echoptr = kb->echobuf;
  8774.   kb->echo_after_prompt = -1;
  8775.   kb->kbd_macro_buffer = 0;
  8776.   kb->kbd_macro_bufsize = 0;
  8777.   kb->defining_kbd_macro = Qnil;
  8778.   kb->Vlast_kbd_macro = Qnil;
  8779.   kb->reference_count = 0;
  8780.   kb->Vsystem_key_alist = Qnil;
  8781.   kb->system_key_syms = Qnil;
  8782.   kb->Vdefault_minibuffer_frame = Qnil;
  8783. }
  8784.  
  8785. /*
  8786.  * Destroy the contents of a kboard object, but not the object itself.
  8787.  * We use this just before deleting it, or if we're going to initialize
  8788.  * it a second time.
  8789.  */
  8790. static void
  8791. wipe_kboard (kb)
  8792.      KBOARD *kb;
  8793. {
  8794.   if (kb->kbd_macro_buffer)
  8795.     xfree (kb->kbd_macro_buffer);
  8796. }
  8797.  
  8798. #ifdef MULTI_KBOARD
  8799. void
  8800. delete_kboard (kb)
  8801.   KBOARD *kb;
  8802. {
  8803.   KBOARD **kbp;
  8804.   for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
  8805.     if (*kbp == NULL)
  8806.       abort ();
  8807.   *kbp = kb->next_kboard;
  8808.   wipe_kboard (kb);
  8809.   xfree (kb);
  8810. }
  8811. #endif
  8812.  
  8813. void
  8814. init_keyboard ()
  8815. {
  8816.   /* This is correct before outermost invocation of the editor loop */
  8817.   command_loop_level = -1;
  8818.   immediate_quit = 0;
  8819.   quit_char = Ctl ('g');
  8820.   Vunread_command_events = Qnil;
  8821.   unread_command_char = -1;
  8822.   EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
  8823.   total_keys = 0;
  8824.   recent_keys_index = 0;
  8825.   kbd_fetch_ptr = kbd_buffer;
  8826.   kbd_store_ptr = kbd_buffer;
  8827.   kbd_buffer_frame_or_window
  8828.     = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
  8829. #ifdef HAVE_MOUSE
  8830.   do_mouse_tracking = Qnil;
  8831. #endif
  8832.   input_pending = 0;
  8833.  
  8834.   /* This means that command_loop_1 won't try to select anything the first
  8835.      time through.  */
  8836.   internal_last_event_frame = Qnil;
  8837.   Vlast_event_frame = internal_last_event_frame;
  8838.  
  8839. #ifdef MULTI_KBOARD
  8840.   current_kboard = initial_kboard;
  8841. #endif
  8842.   wipe_kboard (current_kboard);
  8843.   init_kboard (current_kboard);
  8844.  
  8845.   if (initialized)
  8846.     Ffillarray (kbd_buffer_frame_or_window, Qnil);
  8847.  
  8848.   kbd_buffer_frame_or_window
  8849.     = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
  8850.  
  8851. #ifdef HAVE_PM
  8852.   /* Use SIGINT even if window-system is non-nil; pmemacs sends SIGINT
  8853.      when C-g is typed.  */
  8854.   if (!noninteractive && !read_socket_hook)
  8855. #else /* not HAVE_PM */
  8856.   if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
  8857. #endif /* not HAVE_PM */
  8858.     {
  8859.       signal (SIGINT, interrupt_signal);
  8860. #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
  8861.       /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
  8862.      SIGQUIT and we can't tell which one it will give us.  */
  8863.       signal (SIGQUIT, interrupt_signal);
  8864. #endif /* HAVE_TERMIO */
  8865.     }
  8866. /* Note SIGIO has been undef'd if FIONREAD is missing.  */
  8867. #ifdef SIGIO
  8868.   if (!noninteractive)
  8869.     signal (SIGIO, input_available_signal);
  8870. #endif /* SIGIO */
  8871.  
  8872. /* Use interrupt input by default, if it works and noninterrupt input
  8873.    has deficiencies.  */
  8874.  
  8875. #ifdef INTERRUPT_INPUT
  8876.   interrupt_input = 1;
  8877. #else
  8878.   interrupt_input = 0;
  8879. #endif
  8880.  
  8881. /* Our VMS input only works by interrupts, as of now.  */
  8882. #ifdef VMS
  8883.   interrupt_input = 1;
  8884. #endif
  8885.  
  8886.   sigfree ();
  8887.   dribble = 0;
  8888.  
  8889.   if (keyboard_init_hook)
  8890.     (*keyboard_init_hook) ();
  8891.  
  8892. #ifdef POLL_FOR_INPUT
  8893.   poll_suppress_count = 1;
  8894.   start_polling ();
  8895. #endif
  8896. }
  8897.  
  8898. /* This type's only use is in syms_of_keyboard, to initialize the
  8899.    event header symbols and put properties on them.  */
  8900. struct event_head {
  8901.   Lisp_Object *var;
  8902.   char *name;
  8903.   Lisp_Object *kind;
  8904. };
  8905.  
  8906. struct event_head head_table[] = {
  8907.   &Qmouse_movement,    "mouse-movement",    &Qmouse_movement,
  8908.   &Qscroll_bar_movement, "scroll-bar-movement",    &Qmouse_movement,
  8909.   &Qswitch_frame,    "switch-frame",        &Qswitch_frame,
  8910.   &Qdelete_frame,    "delete-frame",        &Qdelete_frame,
  8911.   &Qiconify_frame,    "iconify-frame",    &Qiconify_frame,
  8912.   &Qmake_frame_visible,    "make-frame-visible",    &Qmake_frame_visible,
  8913. };
  8914.  
  8915. void
  8916. syms_of_keyboard ()
  8917. {
  8918.   staticpro (&item_properties);
  8919.   item_properties = Qnil;
  8920.  
  8921.   staticpro (&real_this_command);
  8922.   real_this_command = Qnil;
  8923.  
  8924.   Qtimer_event_handler = intern ("timer-event-handler");
  8925.   staticpro (&Qtimer_event_handler);
  8926.  
  8927.   Qdisabled_command_hook = intern ("disabled-command-hook");
  8928.   staticpro (&Qdisabled_command_hook);
  8929.  
  8930.   Qself_insert_command = intern ("self-insert-command");
  8931.   staticpro (&Qself_insert_command);
  8932.  
  8933.   Qforward_char = intern ("forward-char");
  8934.   staticpro (&Qforward_char);
  8935.  
  8936.   Qbackward_char = intern ("backward-char");
  8937.   staticpro (&Qbackward_char);
  8938.  
  8939.   Qdisabled = intern ("disabled");
  8940.   staticpro (&Qdisabled);
  8941.  
  8942.   Qundefined = intern ("undefined");
  8943.   staticpro (&Qundefined);
  8944.  
  8945.   Qpre_command_hook = intern ("pre-command-hook");
  8946.   staticpro (&Qpre_command_hook);
  8947.  
  8948.   Qpost_command_hook = intern ("post-command-hook");
  8949.   staticpro (&Qpost_command_hook);
  8950.  
  8951.   Qpost_command_idle_hook = intern ("post-command-idle-hook");
  8952.   staticpro (&Qpost_command_idle_hook);
  8953.  
  8954.   Qdeferred_action_function = intern ("deferred-action-function");
  8955.   staticpro (&Qdeferred_action_function);
  8956.  
  8957.   Qcommand_hook_internal = intern ("command-hook-internal");
  8958.   staticpro (&Qcommand_hook_internal);
  8959.  
  8960.   Qfunction_key = intern ("function-key");
  8961.   staticpro (&Qfunction_key);
  8962.   Qmouse_click = intern ("mouse-click");
  8963.   staticpro (&Qmouse_click);
  8964. #ifdef WINDOWSNT
  8965.   Qmouse_wheel = intern ("mouse-wheel");
  8966.   staticpro (&Qmouse_wheel);
  8967. #endif
  8968.   Qdrag_n_drop = intern ("drag-n-drop");
  8969.   staticpro (&Qdrag_n_drop);
  8970.  
  8971.   Qmenu_enable = intern ("menu-enable");
  8972.   staticpro (&Qmenu_enable);
  8973.   Qmenu_alias = intern ("menu-alias");
  8974.   staticpro (&Qmenu_alias);
  8975.   QCenable = intern (":enable");
  8976.   staticpro (&QCenable);
  8977.   QCvisible = intern (":visible");
  8978.   staticpro (&QCvisible);
  8979.   QCfilter = intern (":filter");
  8980.   staticpro (&QCfilter);
  8981.   QCbutton = intern (":button");
  8982.   staticpro (&QCbutton);
  8983.   QCkeys = intern (":keys");
  8984.   staticpro (&QCkeys);
  8985.   QCkey_sequence = intern (":key-sequence");
  8986.   staticpro (&QCkey_sequence);
  8987.   QCtoggle = intern (":toggle");
  8988.   staticpro (&QCtoggle);
  8989.   QCradio = intern (":radio");
  8990.   staticpro (&QCradio);
  8991.  
  8992.   Qmode_line = intern ("mode-line");
  8993.   staticpro (&Qmode_line);
  8994.   Qvertical_line = intern ("vertical-line");
  8995.   staticpro (&Qvertical_line);
  8996.   Qvertical_scroll_bar = intern ("vertical-scroll-bar");
  8997.   staticpro (&Qvertical_scroll_bar);
  8998.   Qmenu_bar = intern ("menu-bar");
  8999.   staticpro (&Qmenu_bar);
  9000.  
  9001.   Qabove_handle = intern ("above-handle");
  9002.   staticpro (&Qabove_handle);
  9003.   Qhandle = intern ("handle");
  9004.   staticpro (&Qhandle);
  9005.   Qbelow_handle = intern ("below-handle");
  9006.   staticpro (&Qbelow_handle);
  9007.   Qup = intern ("up");
  9008.   staticpro (&Qup);
  9009.   Qdown = intern ("down");
  9010.   staticpro (&Qdown);
  9011.  
  9012.   Qevent_kind = intern ("event-kind");
  9013.   staticpro (&Qevent_kind);
  9014.   Qevent_symbol_elements = intern ("event-symbol-elements");
  9015.   staticpro (&Qevent_symbol_elements);
  9016.   Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
  9017.   staticpro (&Qevent_symbol_element_mask);
  9018.   Qmodifier_cache = intern ("modifier-cache");
  9019.   staticpro (&Qmodifier_cache);
  9020.  
  9021.   Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
  9022.   staticpro (&Qrecompute_lucid_menubar);
  9023.   Qactivate_menubar_hook = intern ("activate-menubar-hook");
  9024.   staticpro (&Qactivate_menubar_hook);
  9025.  
  9026.   Qpolling_period = intern ("polling-period");
  9027.   staticpro (&Qpolling_period);
  9028.  
  9029.   Qinput_method_function = intern ("input-method-function");
  9030.   staticpro (&Qinput_method_function);
  9031.  
  9032.   Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
  9033.   staticpro (&Qinput_method_exit_on_first_char);
  9034.   Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
  9035.   staticpro (&Qinput_method_use_echo_area);
  9036.  
  9037.   Fset (Qinput_method_exit_on_first_char, Qnil);
  9038.   Fset (Qinput_method_use_echo_area, Qnil);
  9039.  
  9040.   {
  9041.     struct event_head *p;
  9042.  
  9043.     for (p = head_table;
  9044.      p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
  9045.      p++)
  9046.       {
  9047.     *p->var = intern (p->name);
  9048.     staticpro (p->var);
  9049.     Fput (*p->var, Qevent_kind, *p->kind);
  9050.     Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
  9051.       }
  9052.   }
  9053.  
  9054.   button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
  9055.   staticpro (&button_down_location);
  9056.  
  9057.   {
  9058.     int i;
  9059.     int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
  9060.  
  9061.     modifier_symbols = Fmake_vector (make_number (len), Qnil);
  9062.     for (i = 0; i < len; i++)
  9063.       if (modifier_names[i])
  9064.     XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
  9065.     staticpro (&modifier_symbols);
  9066.   }
  9067.  
  9068.   recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
  9069.   staticpro (&recent_keys);
  9070.  
  9071.   this_command_keys = Fmake_vector (make_number (40), Qnil);
  9072.   staticpro (&this_command_keys);
  9073.  
  9074.   raw_keybuf = Fmake_vector (make_number (30), Qnil);
  9075.   staticpro (&raw_keybuf);
  9076.  
  9077.   Qextended_command_history = intern ("extended-command-history");
  9078.   Fset (Qextended_command_history, Qnil);
  9079.   staticpro (&Qextended_command_history);
  9080.  
  9081.   kbd_buffer_frame_or_window
  9082.     = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
  9083.   staticpro (&kbd_buffer_frame_or_window);
  9084.  
  9085.   accent_key_syms = Qnil;
  9086.   staticpro (&accent_key_syms);
  9087.  
  9088.   func_key_syms = Qnil;
  9089.   staticpro (&func_key_syms);
  9090.  
  9091.   mouse_syms = Qnil;
  9092.   staticpro (&mouse_syms);
  9093.  
  9094. #ifdef WINDOWSNT
  9095.   mouse_wheel_syms = Qnil;
  9096.   staticpro (&mouse_wheel_syms);
  9097.   
  9098.   drag_n_drop_syms = Qnil;
  9099.   staticpro (&drag_n_drop_syms);
  9100. #endif
  9101.  
  9102.   unread_switch_frame = Qnil;
  9103.   staticpro (&unread_switch_frame);
  9104.  
  9105.   internal_last_event_frame = Qnil;
  9106.   staticpro (&internal_last_event_frame);
  9107.  
  9108.   read_key_sequence_cmd = Qnil;
  9109.   staticpro (&read_key_sequence_cmd);
  9110.  
  9111.   defsubr (&Sevent_convert_list);
  9112.   defsubr (&Sread_key_sequence);
  9113.   defsubr (&Sread_key_sequence_vector);
  9114.   defsubr (&Srecursive_edit);
  9115. #ifdef HAVE_MOUSE
  9116.   defsubr (&Strack_mouse);
  9117. #endif
  9118.   defsubr (&Sinput_pending_p);
  9119.   defsubr (&Scommand_execute);
  9120.   defsubr (&Srecent_keys);
  9121.   defsubr (&Sthis_command_keys);
  9122.   defsubr (&Sthis_command_keys_vector);
  9123.   defsubr (&Sthis_single_command_keys);
  9124.   defsubr (&Sthis_single_command_raw_keys);
  9125.   defsubr (&Sreset_this_command_lengths);
  9126.   defsubr (&Ssuspend_emacs);
  9127.   defsubr (&Sabort_recursive_edit);
  9128.   defsubr (&Sexit_recursive_edit);
  9129.   defsubr (&Srecursion_depth);
  9130.   defsubr (&Stop_level);
  9131.   defsubr (&Sdiscard_input);
  9132.   defsubr (&Sopen_dribble_file);
  9133.   defsubr (&Sset_input_mode);
  9134.   defsubr (&Scurrent_input_mode);
  9135.   defsubr (&Sexecute_extended_command);
  9136.  
  9137.   DEFVAR_LISP ("last-command-char", &last_command_char,
  9138.     "Last input event that was part of a command.");
  9139.  
  9140.   DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
  9141.     "Last input event that was part of a command.");
  9142.  
  9143.   DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
  9144.     "Last input event in a command, except for mouse menu events.\n\
  9145. Mouse menus give back keys that don't look like mouse events;\n\
  9146. this variable holds the actual mouse event that led to the menu,\n\
  9147. so that you can determine whether the command was run by mouse or not.");
  9148.  
  9149.   DEFVAR_LISP ("last-input-char", &last_input_char,
  9150.     "Last input event.");
  9151.  
  9152.   DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
  9153.     "Last input event.");
  9154.  
  9155.   DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
  9156.     "List of events to be read as the command input.\n\
  9157. These events are processed first, before actual keyboard input.");
  9158.   Vunread_command_events = Qnil;
  9159.  
  9160.   DEFVAR_INT ("unread-command-char", &unread_command_char,
  9161.     "If not -1, an object to be read as next command input event.");
  9162.  
  9163.   DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
  9164.     "List of events to be processed as input by input methods.\n\
  9165. These events are processed after `unread-command-events', but\n\
  9166. before actual keyboard input.");
  9167.   Vunread_post_input_method_events = Qnil;
  9168.  
  9169.   DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
  9170.     "List of events to be processed as input by input methods.\n\
  9171. These events are processed after `unread-command-events', but\n\
  9172. before actual keyboard input.");
  9173.   Vunread_input_method_events = Qnil;
  9174.  
  9175.   DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
  9176.     "Meta-prefix character code.  Meta-foo as command input\n\
  9177. turns into this character followed by foo.");
  9178.   XSETINT (meta_prefix_char, 033);
  9179.  
  9180.   DEFVAR_KBOARD ("last-command", Vlast_command,
  9181.     "The last command executed.  Normally a symbol with a function definition,\n\
  9182. but can be whatever was found in the keymap, or whatever the variable\n\
  9183. `this-command' was set to by that command.\n\
  9184. \n\
  9185. The value `mode-exit' is special; it means that the previous command\n\
  9186. read an event that told it to exit, and it did so and unread that event.\n\
  9187. In other words, the present command is the event that made the previous\n\
  9188. command exit.\n\
  9189. \n\
  9190. The value `kill-region' is special; it means that the previous command\n\
  9191. was a kill command.");
  9192.  
  9193.   DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
  9194.     "Same as `last-command', but never altered by Lisp code.");
  9195.  
  9196.   DEFVAR_LISP ("this-command", &Vthis_command,
  9197.     "The command now being executed.\n\
  9198. The command can set this variable; whatever is put here\n\
  9199. will be in `last-command' during the following command.");
  9200.   Vthis_command = Qnil;
  9201.  
  9202.   DEFVAR_INT ("auto-save-interval", &auto_save_interval,
  9203.     "*Number of keyboard input characters between auto-saves.\n\
  9204. Zero means disable autosaving due to number of characters typed.");
  9205.   auto_save_interval = 300;
  9206.  
  9207.   DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
  9208.     "*Number of seconds idle time before auto-save.\n\
  9209. Zero or nil means disable auto-saving due to idleness.\n\
  9210. After auto-saving due to this many seconds of idle time,\n\
  9211. Emacs also does a garbage collection if that seems to be warranted.");
  9212.   XSETFASTINT (Vauto_save_timeout, 30);
  9213.  
  9214.   DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
  9215.     "*Nonzero means echo unfinished commands after this many seconds of pause.");
  9216.   echo_keystrokes = 1;
  9217.  
  9218.   DEFVAR_INT ("polling-period", &polling_period,
  9219.     "*Interval between polling for input during Lisp execution.\n\
  9220. The reason for polling is to make C-g work to stop a running program.\n\
  9221. Polling is needed only when using X windows and SIGIO does not work.\n\
  9222. Polling is automatically disabled in all other cases.");
  9223.   polling_period = 2;
  9224.  
  9225.   DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
  9226.     "*Maximum time between mouse clicks to make a double-click.\n\
  9227. Measured in milliseconds.  nil means disable double-click recognition;\n\
  9228. t means double-clicks have no time limit and are detected\n\
  9229. by position only.");
  9230.   Vdouble_click_time = make_number (500);
  9231.  
  9232.   DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
  9233.     "*Non-nil means inhibit local map menu bar menus.");
  9234.   inhibit_local_menu_bar_menus = 0;
  9235.  
  9236.   DEFVAR_INT ("num-input-keys", &num_input_keys,
  9237.     "Number of complete key sequences read as input so far.\n\
  9238. This includes key sequences read from keyboard macros.\n\
  9239. The number is effectively the number of interactive command invocations.");
  9240.   num_input_keys = 0;
  9241.  
  9242.   DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
  9243.     "Number of input events read from the keyboard so far.\n\
  9244. This does not include events generated by keyboard macros.");
  9245.   num_nonmacro_input_events = 0;
  9246.  
  9247.   DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
  9248.     "The frame in which the most recently read event occurred.\n\
  9249. If the last event came from a keyboard macro, this is set to `macro'.");
  9250.   Vlast_event_frame = Qnil;
  9251.  
  9252.   /* This variable is set up in sysdep.c.  */
  9253.   DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
  9254.     "The ERASE character as set by the user with stty.");
  9255.  
  9256.   DEFVAR_LISP ("help-char", &Vhelp_char,
  9257.     "Character to recognize as meaning Help.\n\
  9258. When it is read, do `(eval help-form)', and display result if it's a string.\n\
  9259. If the value of `help-form' is nil, this char can be read normally.");
  9260.   XSETINT (Vhelp_char, Ctl ('H'));
  9261.  
  9262.   DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
  9263.     "List of input events to recognize as meaning Help.\n\
  9264. These work just like the value of `help-char' (see that).");
  9265.   Vhelp_event_list = Qnil;
  9266.  
  9267.   DEFVAR_LISP ("help-form", &Vhelp_form,
  9268.     "Form to execute when character `help-char' is read.\n\
  9269. If the form returns a string, that string is displayed.\n\
  9270. If `help-form' is nil, the help char is not recognized.");
  9271.   Vhelp_form = Qnil;
  9272.  
  9273.   DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
  9274.     "Command to run when `help-char' character follows a prefix key.\n\
  9275. This command is used only when there is no actual binding\n\
  9276. for that character after that prefix key.");
  9277.   Vprefix_help_command = Qnil;
  9278.  
  9279.   DEFVAR_LISP ("top-level", &Vtop_level,
  9280.     "Form to evaluate when Emacs starts up.\n\
  9281. Useful to set before you dump a modified Emacs.");
  9282.   Vtop_level = Qnil;
  9283.  
  9284.   DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
  9285.     "Translate table for keyboard input, or nil.\n\
  9286. Each character is looked up in this string and the contents used instead.\n\
  9287. The value may be a string, a vector, or a char-table.\n\
  9288. If it is a string or vector of length N,\n\
  9289. character codes N and up are untranslated.\n\
  9290. In a vector or a char-table, an element which is nil means \"no translation\".");
  9291.   Vkeyboard_translate_table = Qnil;
  9292.  
  9293.   DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
  9294.     "Non-nil means to always spawn a subshell instead of suspending,\n\
  9295. even if the operating system has support for stopping a process.");
  9296.   cannot_suspend = 0;
  9297.  
  9298.   DEFVAR_BOOL ("menu-prompting", &menu_prompting,
  9299.     "Non-nil means prompt with menus when appropriate.\n\
  9300. This is done when reading from a keymap that has a prompt string,\n\
  9301. for elements that have prompt strings.\n\
  9302. The menu is displayed on the screen\n\
  9303. if X menus were enabled at configuration\n\
  9304. time and the previous event was a mouse click prefix key.\n\
  9305. Otherwise, menu prompting uses the echo area.");
  9306.   menu_prompting = 1;
  9307.  
  9308.   DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
  9309.     "Character to see next line of menu prompt.\n\
  9310. Type this character while in a menu prompt to rotate around the lines of it.");
  9311.   XSETINT (menu_prompt_more_char, ' ');
  9312.  
  9313.   DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
  9314.     "A mask of additional modifier keys to use with every keyboard character.\n\
  9315. Emacs applies the modifiers of the character stored here to each keyboard\n\
  9316. character it reads.  For example, after evaluating the expression\n\
  9317.     (setq extra-keyboard-modifiers ?\\C-x)\n\
  9318. all input characters will have the control modifier applied to them.\n\
  9319. \n\
  9320. Note that the character ?\\C-@, equivalent to the integer zero, does\n\
  9321. not count as a control character; rather, it counts as a character\n\
  9322. with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
  9323. cancels any modification.");
  9324.   extra_keyboard_modifiers = 0;
  9325.  
  9326.   DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
  9327.     "If an editing command sets this to t, deactivate the mark afterward.\n\
  9328. The command loop sets this to nil before each command,\n\
  9329. and tests the value when the command returns.\n\
  9330. Buffer modification stores t in this variable.");
  9331.   Vdeactivate_mark = Qnil;
  9332.  
  9333.   DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
  9334.     "Temporary storage of pre-command-hook or post-command-hook.");
  9335.   Vcommand_hook_internal = Qnil;
  9336.  
  9337.   DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
  9338.     "Normal hook run before each command is executed.\n\
  9339. Errors running the hook are caught and ignored.");
  9340.   Vpre_command_hook = Qnil;
  9341.  
  9342.   DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
  9343.     "Normal hook run after each command is executed.\n\
  9344. Errors running the hook are caught and ignored.");
  9345.   Vpost_command_hook = Qnil;
  9346.  
  9347.   DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
  9348.     "Normal hook run after each command is executed, if idle.\n\
  9349. Errors running the hook are caught and ignored.\n\
  9350. This feature is obsolete; use idle timers instead.  See `etc/NEWS'.");
  9351.   Vpost_command_idle_hook = Qnil;
  9352.  
  9353.   DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
  9354.     "Delay time before running `post-command-idle-hook'.\n\
  9355. This is measured in microseconds.");
  9356.   post_command_idle_delay = 100000;
  9357.  
  9358. #if 0
  9359.   DEFVAR_LISP ("echo-area-clear-hook", ...,
  9360.     "Normal hook run when clearing the echo area.");
  9361. #endif
  9362.   Qecho_area_clear_hook = intern ("echo-area-clear-hook");
  9363.   XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
  9364.  
  9365.   DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
  9366.     "t means menu bar, specified Lucid style, needs to be recomputed.");
  9367.   Vlucid_menu_bar_dirty_flag = Qnil;
  9368.  
  9369.   DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
  9370.     "List of menu bar items to move to the end of the menu bar.\n\
  9371. The elements of the list are event types that may have menu bar bindings.");
  9372.   Vmenu_bar_final_items = Qnil;
  9373.  
  9374.   DEFVAR_KBOARD ("overriding-terminal-local-map",
  9375.          Voverriding_terminal_local_map,
  9376.     "Per-terminal keymap that overrides all other local keymaps.\n\
  9377. If this variable is non-nil, it is used as a keymap instead of the\n\
  9378. buffer's local map, and the minor mode keymaps and text property keymaps.\n\
  9379. This variable is intended to let commands such as `universal-argumemnt'\n\
  9380. set up a different keymap for reading the next command.");
  9381.  
  9382.   DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
  9383.     "Keymap that overrides all other local keymaps.\n\
  9384. If this variable is non-nil, it is used as a keymap instead of the\n\
  9385. buffer's local map, and the minor mode keymaps and text property keymaps.");
  9386.   Voverriding_local_map = Qnil;
  9387.  
  9388.   DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
  9389.     "Non-nil means `overriding-local-map' applies to the menu bar.\n\
  9390. Otherwise, the menu bar continues to reflect the buffer's local map\n\
  9391. and the minor mode maps regardless of `overriding-local-map'.");
  9392.   Voverriding_local_map_menu_flag = Qnil;
  9393.  
  9394.   DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
  9395.     "Keymap defining bindings for special events to execute at low level.");
  9396.   Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
  9397.  
  9398.   DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
  9399.     "*Non-nil means generate motion events for mouse motion.");
  9400.  
  9401.   DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
  9402.     "Alist of system-specific X windows key symbols.\n\
  9403. Each element should have the form (N . SYMBOL) where N is the\n\
  9404. numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
  9405. and SYMBOL is its name.");
  9406.  
  9407.   DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
  9408.     "List of deferred actions to be performed at a later time.\n\
  9409. The precise format isn't relevant here; we just check whether it is nil.");
  9410.   Vdeferred_action_list = Qnil;
  9411.  
  9412.   DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
  9413.     "Function to call to handle deferred actions, after each command.\n\
  9414. This function is called with no arguments after each command\n\
  9415. whenever `deferred-action-list' is non-nil.");
  9416.   Vdeferred_action_function = Qnil;
  9417.  
  9418.   DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
  9419.     "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
  9420. The value can be a length of time to show the message for.\n\
  9421. If the value is non-nil and not a number, we wait 2 seconds.");
  9422.   Vsuggest_key_bindings = Qt;
  9423.  
  9424.   DEFVAR_LISP ("timer-list", &Vtimer_list,
  9425.     "List of active absolute time timers in order of increasing time");
  9426.   Vtimer_list = Qnil;
  9427.  
  9428.   DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
  9429.     "List of active idle-time timers in order of increasing time");
  9430.   Vtimer_idle_list = Qnil;
  9431.  
  9432.   DEFVAR_LISP ("input-method-function", &Vinput_method_function,
  9433.     "If non-nil, the function that implements the current input method.\n\
  9434. It's called with one argument, a printing character that was just read.\n\
  9435. \(That means a character with code 040...0176.)\n\
  9436. Typically this function uses `read-event' to read additional events.\n\
  9437. When it does so, it should first bind `input-method-function' to nil\n\
  9438. so it will not be called recursively.\n\
  9439. \n\
  9440. The function should return a list of zero or more events\n\
  9441. to be used as input.  If it wants to put back some events\n\
  9442. to be reconsidered, separately, by the input method,\n\
  9443. it can add them to the beginning of `unread-command-events'.\n\
  9444. \n\
  9445. The input method function can find in `input-method-previous-method'\n\
  9446. the previous echo area message.\n\
  9447. \n\
  9448. The input method function should refer to the variables\n\
  9449. `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\
  9450. for guidance on what to do.");
  9451.   Vinput_method_function = Qnil;
  9452.  
  9453.   DEFVAR_LISP ("input-method-previous-message",
  9454.            &Vinput_method_previous_message,
  9455.            "When `input-mehod-function' is called, hold the previous echo area message.\n\
  9456. This variable exists because `read-event' clears the echo area\n\
  9457. before running the input method.  It is nil if there was no message.");
  9458.   Vinput_method_previous_message = Qnil;
  9459. }
  9460.  
  9461. void
  9462. keys_of_keyboard ()
  9463. {
  9464.   initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
  9465.   initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
  9466.   initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
  9467.   initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
  9468.   initial_define_key (meta_map, 'x', "execute-extended-command");
  9469.  
  9470.   initial_define_lispy_key (Vspecial_event_map, "delete-frame",
  9471.                 "handle-delete-frame");
  9472.   initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
  9473.                 "ignore-event");
  9474.   initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
  9475.                 "ignore-event");
  9476. }
  9477.