
1: /* Keyboard and mouse input; editor command loop. 2: Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995, 3: 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 4: 2005, 2006, 2007 Free Software Foundation, Inc. 5: 6: This file is part of GNU Emacs. 7: 8: GNU Emacs is free software; you can redistribute it and/or modify 9: it under the terms of the GNU General Public License as published by 10: the Free Software Foundation; either version 2, or (at your option) 11: any later version. 12: 13: GNU Emacs is distributed in the hope that it will be useful, 14: but WITHOUT ANY WARRANTY; without even the implied warranty of 15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16: GNU General Public License for more details. 17: 18: You should have received a copy of the GNU General Public License 19: along with GNU Emacs; see the file COPYING. If not, write to 20: the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21: Boston, MA 02110-1301, USA. */ 22: 23: #include <config.h> 24: #include <signal.h> 25: #include <stdio.h> 26: #include "termchar.h" 27: #include "termopts.h" 28: #include "lisp.h" 29: #include "termhooks.h" 30: #include "macros.h" 31: #include "keyboard.h" 32: #include "frame.h" 33: #include "window.h" 34: #include "commands.h" 35: #include "buffer.h" 36: #include "charset.h" 37: #include "disptab.h" 38: #include "dispextern.h" 39: #include "syntax.h" 40: #include "intervals.h" 41: #include "keymap.h" 42: #include "blockinput.h" 43: #include "puresize.h" 44: #include "systime.h" 45: #include "atimer.h" 46: #include <setjmp.h> 47: #include <errno.h> 48: 49: #ifdef HAVE_GTK_AND_PTHREAD 50: #include <pthread.h> 51: #endif 52: #ifdef MSDOS 53: #include "msdos.h" 54: #include <time.h> 55: #else /* not MSDOS */ 56: #ifndef VMS 57: #include <sys/ioctl.h> 58: #endif 59: #endif /* not MSDOS */ 60: 61: #include "syssignal.h" 62: #include "systty.h" 63: 64: #include <sys/types.h> 65: #ifdef HAVE_UNISTD_H 66: #include <unistd.h> 67: #endif 68: 69: #ifdef HAVE_FCNTL_H 70: #include <fcntl.h> 71: #endif 72: 73: /* This is to get the definitions of the XK_ symbols. */ 74: #ifdef HAVE_X_WINDOWS 75: #include "xterm.h" 76: #endif 77: 78: #ifdef HAVE_NTGUI 79: #include "w32term.h" 80: #endif /* HAVE_NTGUI */ 81: 82: #ifdef MAC_OS 83: #include "macterm.h" 84: #endif 85: 86: #ifndef USE_CRT_DLL 87: extern int errno; 88: #endif 89: 90: /* Variables for blockinput.h: */ 91: 92: /* Non-zero if interrupt input is blocked right now. */ 93: volatile int interrupt_input_blocked; 94: 95: /* Nonzero means an input interrupt has arrived 96: during the current critical section. */ 97: int interrupt_input_pending; 98: 99: 100: /* File descriptor to use for input. */ 101: extern int input_fd; 102: 103: #ifdef HAVE_WINDOW_SYSTEM 104: /* Make all keyboard buffers much bigger when using X windows. */ 105: #ifdef MAC_OS8 106: /* But not too big (local data > 32K error) if on Mac OS Classic. */ 107: #define KBD_BUFFER_SIZE 512 108: #else 109: #define KBD_BUFFER_SIZE 4096 110: #endif 111: #else /* No X-windows, character input */ 112: #define KBD_BUFFER_SIZE 4096 113: #endif /* No X-windows */ 114: 115: #define abs(x) ((x) >= 0 ? (x) : -(x)) 116: 117: /* Following definition copied from eval.c */ 118: 119: struct backtrace 120: { 121: struct backtrace *next; 122: Lisp_Object *function; 123: Lisp_Object *args; /* Points to vector of args. */ 124: int nargs; /* length of vector. If nargs is UNEVALLED, 125: args points to slot holding list of 126: unevalled args */ 127: char evalargs; 128: /* Nonzero means call value of debugger when done with this operation. */ 129: char debug_on_exit; 130: }; 131: 132: #ifdef MULTI_KBOARD 133: KBOARD *initial_kboard; 134: KBOARD *current_kboard; 135: KBOARD *all_kboards; 136: int single_kboard; 137: #else 138: KBOARD the_only_kboard; 139: #endif 140: 141: /* Non-nil disable property on a command means 142: do not execute it; call disabled-command-function's value instead. */ 143: Lisp_Object Qdisabled, Qdisabled_command_function; 144: 145: #define NUM_RECENT_KEYS (300) 146: int recent_keys_index; /* Index for storing next element into recent_keys */ 147: int total_keys; /* Total number of elements stored into recent_keys */ 148: Lisp_Object recent_keys; /* Vector holds the last NUM_RECENT_KEYS keystrokes */ 149: 150: /* Vector holding the key sequence that invoked the current command. 151: It is reused for each command, and it may be longer than the current 152: sequence; this_command_key_count indicates how many elements 153: actually mean something. 154: It's easier to staticpro a single Lisp_Object than an array. */ 155: Lisp_Object this_command_keys; 156: int this_command_key_count; 157: 158: /* 1 after calling Freset_this_command_lengths. 159: Usually it is 0. */ 160: int this_command_key_count_reset; 161: 162: /* This vector is used as a buffer to record the events that were actually read 163: by read_key_sequence. */ 164: Lisp_Object raw_keybuf; 165: int raw_keybuf_count; 166: 167: #define GROW_RAW_KEYBUF \ 168: if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \ 169: { \ 170: int newsize = 2 * XVECTOR (raw_keybuf)->size; \ 171: Lisp_Object new; \ 172: new = Fmake_vector (make_number (newsize), Qnil); \ 173: bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \ 174: raw_keybuf_count * sizeof (Lisp_Object)); \ 175: raw_keybuf = new; \ 176: } 177: 178: /* Number of elements of this_command_keys 179: that precede this key sequence. */ 180: int this_single_command_key_start; 181: 182: /* Record values of this_command_key_count and echo_length () 183: before this command was read. */ 184: static int before_command_key_count; 185: static int before_command_echo_length; 186: 187: extern int minbuf_level; 188: 189: extern int message_enable_multibyte; 190: 191: extern struct backtrace *backtrace_list; 192: 193: /* If non-nil, the function that implements the display of help. 194: It's called with one argument, the help string to display. */ 195: 196: Lisp_Object Vshow_help_function; 197: 198: /* If a string, the message displayed before displaying a help-echo 199: in the echo area. */ 200: 201: Lisp_Object Vpre_help_message; 202: 203: /* Nonzero means do menu prompting. */ 204: 205: static int menu_prompting; 206: 207: /* Character to see next line of menu prompt. */ 208: 209: static Lisp_Object menu_prompt_more_char; 210: 211: /* For longjmp to where kbd input is being done. */ 212: 213: static jmp_buf getcjmp; 214: 215: /* True while doing kbd input. */ 216: int waiting_for_input; 217: 218: /* True while displaying for echoing. Delays C-g throwing. */ 219: 220: int echoing; 221: 222: /* Non-null means we can start echoing at the next input pause even 223: though there is something in the echo area. */ 224: 225: static struct kboard *ok_to_echo_at_next_pause; 226: 227: /* The kboard last echoing, or null for none. Reset to 0 in 228: cancel_echoing. If non-null, and a current echo area message 229: exists, and echo_message_buffer is eq to the current message 230: buffer, we know that the message comes from echo_kboard. */ 231: 232: struct kboard *echo_kboard; 233: 234: /* The buffer used for echoing. Set in echo_now, reset in 235: cancel_echoing. */ 236: 237: Lisp_Object echo_message_buffer; 238: 239: /* Nonzero means disregard local maps for the menu bar. */ 240: static int inhibit_local_menu_bar_menus; 241: 242: /* Nonzero means C-g should cause immediate error-signal. */ 243: int immediate_quit; 244: 245: /* The user's hook function for outputting an error message. */ 246: Lisp_Object Vcommand_error_function; 247: 248: /* The user's ERASE setting. */ 249: Lisp_Object Vtty_erase_char; 250: 251: /* Character to recognize as the help char. */ 252: Lisp_Object Vhelp_char; 253: 254: /* List of other event types to recognize as meaning "help". */ 255: Lisp_Object Vhelp_event_list; 256: 257: /* Form to execute when help char is typed. */ 258: Lisp_Object Vhelp_form; 259: 260: /* Command to run when the help character follows a prefix key. */ 261: Lisp_Object Vprefix_help_command; 262: 263: /* List of items that should move to the end of the menu bar. */ 264: Lisp_Object Vmenu_bar_final_items; 265: 266: /* Non-nil means show the equivalent key-binding for 267: any M-x command that has one. 268: The value can be a length of time to show the message for. 269: If the value is non-nil and not a number, we wait 2 seconds. */ 270: Lisp_Object Vsuggest_key_bindings; 271: 272: /* How long to display an echo-area message when the minibuffer is active. 273: If the value is not a number, such messages don't time out. */ 274: Lisp_Object Vminibuffer_message_timeout; 275: 276: /* Character that causes a quit. Normally C-g. 277: 278: If we are running on an ordinary terminal, this must be an ordinary 279: ASCII char, since we want to make it our interrupt character. 280: 281: If we are not running on an ordinary terminal, it still needs to be 282: an ordinary ASCII char. This character needs to be recognized in 283: the input interrupt handler. At this point, the keystroke is 284: represented as a struct input_event, while the desired quit 285: character is specified as a lispy event. The mapping from struct 286: input_events to lispy events cannot run in an interrupt handler, 287: and the reverse mapping is difficult for anything but ASCII 288: keystrokes. 289: 290: FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an 291: ASCII character. */ 292: int quit_char; 293: 294: extern Lisp_Object current_global_map; 295: extern int minibuf_level; 296: 297: /* If non-nil, this is a map that overrides all other local maps. */ 298: Lisp_Object Voverriding_local_map; 299: 300: /* If non-nil, Voverriding_local_map applies to the menu bar. */ 301: Lisp_Object Voverriding_local_map_menu_flag; 302: 303: /* Keymap that defines special misc events that should 304: be processed immediately at a low level. */ 305: Lisp_Object Vspecial_event_map; 306: 307: /* Current depth in recursive edits. */ 308: int command_loop_level; 309: 310: /* Total number of times command_loop has read a key sequence. */ 311: EMACS_INT num_input_keys; 312: 313: /* Last input character read as a command. */ 314: Lisp_Object last_command_char; 315: 316: /* Last input character read as a command, not counting menus 317: reached by the mouse. */ 318: Lisp_Object last_nonmenu_event; 319: 320: /* Last input character read for any purpose. */ 321: Lisp_Object last_input_char; 322: 323: /* If not Qnil, a list of objects to be read as subsequent command input. */ 324: Lisp_Object Vunread_command_events; 325: 326: /* If not Qnil, a list of objects to be read as subsequent command input 327: including input method processing. */ 328: Lisp_Object Vunread_input_method_events; 329: 330: /* If not Qnil, a list of objects to be read as subsequent command input 331: but NOT including input method processing. */ 332: Lisp_Object Vunread_post_input_method_events; 333: 334: /* If not -1, an event to be read as subsequent command input. */ 335: EMACS_INT unread_command_char; 336: 337: /* If not Qnil, this is a switch-frame event which we decided to put 338: off until the end of a key sequence. This should be read as the 339: next command input, after any unread_command_events. 340: 341: read_key_sequence uses this to delay switch-frame events until the 342: end of the key sequence; Fread_char uses it to put off switch-frame 343: events until a non-ASCII event is acceptable as input. */ 344: Lisp_Object unread_switch_frame; 345: 346: /* A mask of extra modifier bits to put into every keyboard char. */ 347: EMACS_INT extra_keyboard_modifiers; 348: 349: /* Char to use as prefix when a meta character is typed in. 350: This is bound on entry to minibuffer in case ESC is changed there. */ 351: 352: Lisp_Object meta_prefix_char; 353: 354: /* Last size recorded for a current buffer which is not a minibuffer. */ 355: static int last_non_minibuf_size; 356: 357: /* Number of idle seconds before an auto-save and garbage collection. */ 358: static Lisp_Object Vauto_save_timeout; 359: 360: /* Total number of times read_char has returned. */ 361: int num_input_events; 362: 363: /* Total number of times read_char has returned, outside of macros. */ 364: EMACS_INT num_nonmacro_input_events; 365: 366: /* Auto-save automatically when this many characters have been typed 367: since the last time. */ 368: 369: static EMACS_INT auto_save_interval; 370: 371: /* Value of num_nonmacro_input_events as of last auto save. */ 372: 373: int last_auto_save; 374: 375: /* The command being executed by the command loop. 376: Commands may set this, and the value set will be copied into 377: current_kboard->Vlast_command instead of the actual command. */ 378: Lisp_Object Vthis_command; 379: 380: /* This is like Vthis_command, except that commands never set it. */ 381: Lisp_Object real_this_command; 382: 383: /* If the lookup of the command returns a binding, the original 384: command is stored in this-original-command. It is nil otherwise. */ 385: Lisp_Object Vthis_original_command; 386: 387: /* The value of point when the last command was started. */ 388: int last_point_position; 389: 390: /* The buffer that was current when the last command was started. */ 391: Lisp_Object last_point_position_buffer; 392: 393: /* The window that was selected when the last command was started. */ 394: Lisp_Object last_point_position_window; 395: 396: /* The frame in which the last input event occurred, or Qmacro if the 397: last event came from a macro. We use this to determine when to 398: generate switch-frame events. This may be cleared by functions 399: like Fselect_frame, to make sure that a switch-frame event is 400: generated by the next character. */ 401: Lisp_Object internal_last_event_frame; 402: 403: /* A user-visible version of the above, intended to allow users to 404: figure out where the last event came from, if the event doesn't 405: carry that information itself (i.e. if it was a character). */ 406: Lisp_Object Vlast_event_frame; 407: 408: /* The timestamp of the last input event we received from the X server. 409: X Windows wants this for selection ownership. */ 410: unsigned long last_event_timestamp; 411: 412: Lisp_Object Qself_insert_command; 413: Lisp_Object Qforward_char; 414: Lisp_Object Qbackward_char; 415: Lisp_Object Qundefined; 416: Lisp_Object Qtimer_event_handler; 417: 418: /* read_key_sequence stores here the command definition of the 419: key sequence that it reads. */ 420: Lisp_Object read_key_sequence_cmd; 421: 422: /* Echo unfinished commands after this many seconds of pause. */ 423: Lisp_Object Vecho_keystrokes; 424: 425: /* Form to evaluate (if non-nil) when Emacs is started. */ 426: Lisp_Object Vtop_level; 427: 428: /* User-supplied table to translate input characters. */ 429: Lisp_Object Vkeyboard_translate_table; 430: 431: /* Keymap mapping ASCII function key sequences onto their preferred forms. */ 432: extern Lisp_Object Vfunction_key_map; 433: 434: /* Another keymap that maps key sequences into key sequences. 435: This one takes precedence over ordinary definitions. */ 436: extern Lisp_Object Vkey_translation_map; 437: 438: /* If non-nil, this implements the current input method. */ 439: Lisp_Object Vinput_method_function; 440: Lisp_Object Qinput_method_function; 441: 442: /* When we call Vinput_method_function, 443: this holds the echo area message that was just erased. */ 444: Lisp_Object Vinput_method_previous_message; 445: 446: /* Non-nil means deactivate the mark at end of this command. */ 447: Lisp_Object Vdeactivate_mark; 448: 449: /* Menu bar specified in Lucid Emacs fashion. */ 450: 451: Lisp_Object Vlucid_menu_bar_dirty_flag; 452: Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook; 453: 454: Lisp_Object Qecho_area_clear_hook; 455: 456: /* Hooks to run before and after each command. */ 457: Lisp_Object Qpre_command_hook, Vpre_command_hook; 458: Lisp_Object Qpost_command_hook, Vpost_command_hook; 459: Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal; 460: 461: /* List of deferred actions to be performed at a later time. 462: The precise format isn't relevant here; we just check whether it is nil. */ 463: Lisp_Object Vdeferred_action_list; 464: 465: /* Function to call to handle deferred actions, when there are any. */ 466: Lisp_Object Vdeferred_action_function; 467: Lisp_Object Qdeferred_action_function; 468: 469: Lisp_Object Qinput_method_exit_on_first_char; 470: Lisp_Object Qinput_method_use_echo_area; 471: 472: /* File in which we write all commands we read. */ 473: FILE *dribble; 474: 475: /* Nonzero if input is available. */ 476: int input_pending; 477: 478: /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should 479: keep 0200 bit in input chars. 0 to ignore the 0200 bit. */ 480: 481: int meta_key; 482: 483: extern char *pending_malloc_warning; 484: 485: /* Circular buffer for pre-read keyboard input. */ 486: 487: static struct input_event kbd_buffer[KBD_BUFFER_SIZE]; 488: 489: /* Pointer to next available character in kbd_buffer. 490: If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty. 491: This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the 492: next available char is in kbd_buffer[0]. */ 493: static struct input_event *kbd_fetch_ptr; 494: 495: /* Pointer to next place to store character in kbd_buffer. This 496: may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next 497: character should go in kbd_buffer[0]. */ 498: static struct input_event * volatile kbd_store_ptr; 499: 500: /* The above pair of variables forms a "queue empty" flag. When we 501: enqueue a non-hook event, we increment kbd_store_ptr. When we 502: dequeue a non-hook event, we increment kbd_fetch_ptr. We say that 503: there is input available iff the two pointers are not equal. 504: 505: Why not just have a flag set and cleared by the enqueuing and 506: dequeuing functions? Such a flag could be screwed up by interrupts 507: at inopportune times. */ 508: 509: /* If this flag is non-nil, we check mouse_moved to see when the 510: mouse moves, and motion events will appear in the input stream. 511: Otherwise, mouse motion is ignored. */ 512: Lisp_Object do_mouse_tracking; 513: 514: /* Symbols to head events. */ 515: Lisp_Object Qmouse_movement; 516: Lisp_Object Qscroll_bar_movement; 517: Lisp_Object Qswitch_frame; 518: Lisp_Object Qdelete_frame; 519: Lisp_Object Qiconify_frame; 520: Lisp_Object Qmake_frame_visible; 521: Lisp_Object Qselect_window; 522: Lisp_Object Qhelp_echo; 523: 524: #ifdef HAVE_MOUSE 525: Lisp_Object Qmouse_fixup_help_message; 526: #endif 527: 528: /* Symbols to denote kinds of events. */ 529: Lisp_Object Qfunction_key; 530: Lisp_Object Qmouse_click; 531: #if defined (WINDOWSNT) || defined (MAC_OS) 532: Lisp_Object Qlanguage_change; 533: #endif 534: Lisp_Object Qdrag_n_drop; 535: Lisp_Object Qsave_session; 536: #ifdef MAC_OS 537: Lisp_Object Qmac_apple_event; 538: #endif 539: 540: /* Lisp_Object Qmouse_movement; - also an event header */ 541: 542: /* Properties of event headers. */ 543: Lisp_Object Qevent_kind; 544: Lisp_Object Qevent_symbol_elements; 545: 546: /* menu item parts */ 547: Lisp_Object Qmenu_alias; 548: Lisp_Object Qmenu_enable; 549: Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence; 550: Lisp_Object QCbutton, QCtoggle, QCradio; 551: extern Lisp_Object Vdefine_key_rebound_commands; 552: extern Lisp_Object Qmenu_item; 553: 554: /* An event header symbol HEAD may have a property named 555: Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS); 556: BASE is the base, unmodified version of HEAD, and MODIFIERS is the 557: mask of modifiers applied to it. If present, this is used to help 558: speed up parse_modifiers. */ 559: Lisp_Object Qevent_symbol_element_mask; 560: 561: /* An unmodified event header BASE may have a property named 562: Qmodifier_cache, which is an alist mapping modifier masks onto 563: modified versions of BASE. If present, this helps speed up 564: apply_modifiers. */ 565: Lisp_Object Qmodifier_cache; 566: 567: /* Symbols to use for parts of windows. */ 568: Lisp_Object Qmode_line; 569: Lisp_Object Qvertical_line; 570: Lisp_Object Qvertical_scroll_bar; 571: Lisp_Object Qmenu_bar; 572: extern Lisp_Object Qleft_margin, Qright_margin; 573: extern Lisp_Object Qleft_fringe, Qright_fringe; 574: extern Lisp_Object QCmap; 575: 576: Lisp_Object recursive_edit_unwind (), command_loop (); 577: Lisp_Object Fthis_command_keys (); 578: Lisp_Object Qextended_command_history; 579: EMACS_TIME timer_check (); 580: 581: extern Lisp_Object Vhistory_length, Vtranslation_table_for_input; 582: 583: extern char *x_get_keysym_name (); 584: 585: static void record_menu_key (); 586: static int echo_length (); 587: 588: Lisp_Object Qpolling_period; 589: 590: /* List of absolute timers. Appears in order of next scheduled event. */ 591: Lisp_Object Vtimer_list; 592: 593: /* List of idle time timers. Appears in order of next scheduled event. */ 594: Lisp_Object Vtimer_idle_list; 595: 596: /* Incremented whenever a timer is run. */ 597: int timers_run; 598: 599: extern Lisp_Object Vprint_level, Vprint_length; 600: 601: /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt 602: happens. */ 603: EMACS_TIME *input_available_clear_time; 604: 605: /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode. 606: Default is 1 if INTERRUPT_INPUT is defined. */ 607: int interrupt_input; 608: 609: /* Nonzero while interrupts are temporarily deferred during redisplay. */ 610: int interrupts_deferred; 611: 612: /* Nonzero means use ^S/^Q for flow control. */ 613: int flow_control; 614: 615: /* Allow m- file to inhibit use of FIONREAD. */ 616: #ifdef BROKEN_FIONREAD 617: #undef FIONREAD 618: #endif 619: 620: /* We are unable to use interrupts if FIONREAD is not available, 621: so flush SIGIO so we won't try. */ 622: #if !defined (FIONREAD) 623: #ifdef SIGIO 624: #undef SIGIO