1 /* Declarations useful when processing input.
2 Copyright (C) 1985-1987, 1993, 2001-2015 Free Software Foundation,
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #ifndef EMACS_KEYBOARD_H
21 #define EMACS_KEYBOARD_H
23 #include "systime.h" /* for struct timespec, Time */
24 #include "coding.h" /* for ENCODE_UTF_8 and ENCODE_SYSTEM */
25 #include "termhooks.h"
28 # include "xterm.h" /* for struct selection_input_event */
33 /* Most code should use this macro to access Lisp fields in struct kboard. */
35 #define KVAR(kboard, field) ((kboard)->field ## _)
37 /* Each KBOARD represents one logical input stream from which Emacs
38 gets input. If we are using ordinary terminals, it has one KBOARD
39 object for each terminal device.
40 Usually each X display screen has its own KBOARD,
41 but when two of them are on the same X server,
42 we assume they share a keyboard and give them one KBOARD in common.
44 Some Lisp variables are per-kboard; they are stored in the KBOARD structure
45 and accessed indirectly via a Lisp_Misc_Kboard_Objfwd object.
47 So that definition of keyboard macros, and reading of prefix arguments,
48 can happen in parallel on various KBOARDs at once,
49 the state information for those activities is stored in the KBOARD.
51 Emacs has two states for reading input:
53 ** Any kboard. Emacs can accept input from any KBOARD,
54 and as soon as any of them provides a complete command, Emacs can run it.
56 ** Single kboard. Then Emacs is running a command for one KBOARD
57 and can only read input from that KBOARD.
59 All input, from all KBOARDs, goes together in a single event queue
60 at interrupt level. read_char sees the events sequentially,
61 but deals with them in accord with the current input state.
63 In the any-kboard state, read_key_sequence processes input from any KBOARD
64 immediately. When a new event comes in from a particular KBOARD,
65 read_key_sequence switches to that KBOARD. As a result,
66 as soon as a complete key arrives from some KBOARD or other,
67 Emacs starts executing that key's binding. It switches to the
68 single-kboard state for the execution of that command,
69 so that that command can get input only from its own KBOARD.
71 While in the single-kboard state, read_char can consider input only
72 from the current KBOARD. If events come from other KBOARDs, they
73 are put aside for later in the KBOARDs' kbd_queue lists.
74 The flag kbd_queue_has_data in a KBOARD is 1 if this has happened.
75 When Emacs goes back to the any-kboard state, it looks at all the KBOARDs
76 to find those; and it tries processing their input right away. */
78 typedef struct kboard KBOARD
;
83 /* If non-nil, a keymap that overrides all others but applies only to
84 this KBOARD. Lisp code that uses this instead of calling read-char
85 can effectively wait for input in the any-kboard state, and hence
86 avoid blocking out the other KBOARDs. See universal-argument in
87 lisp/simple.el for an example. */
88 Lisp_Object Voverriding_terminal_local_map_
;
90 /* Last command executed by the editor command loop, not counting
91 commands that set the prefix argument. */
92 Lisp_Object Vlast_command_
;
94 /* Normally same as last-command, but never modified by other commands. */
95 Lisp_Object Vreal_last_command_
;
97 /* User-supplied table to translate input characters through. */
98 Lisp_Object Vkeyboard_translate_table_
;
100 /* Last command that may be repeated by `repeat'. */
101 Lisp_Object Vlast_repeatable_command_
;
103 /* The prefix argument for the next command, in raw form. */
104 Lisp_Object Vprefix_arg_
;
106 /* Saved prefix argument for the last command, in raw form. */
107 Lisp_Object Vlast_prefix_arg_
;
109 /* Unread events specific to this kboard. */
110 Lisp_Object kbd_queue_
;
112 /* Non-nil while a kbd macro is being defined. */
113 Lisp_Object defining_kbd_macro_
;
115 /* The start of storage for the current keyboard macro. */
116 Lisp_Object
*kbd_macro_buffer
;
118 /* Where to store the next keystroke of the macro. */
119 Lisp_Object
*kbd_macro_ptr
;
121 /* The finalized section of the macro starts at kbd_macro_buffer and
122 ends before this. This is not the same as kbd_macro_ptr, because
123 we advance this to kbd_macro_ptr when a key's command is complete.
124 This way, the keystrokes for "end-kbd-macro" are not included in the
125 macro. This also allows us to throw away the events added to the
126 macro by the last command: all the events between kbd_macro_end and
127 kbd_macro_ptr belong to the last command; see
128 cancel-kbd-macro-events. */
129 Lisp_Object
*kbd_macro_end
;
131 /* Allocated size of kbd_macro_buffer. */
132 ptrdiff_t kbd_macro_bufsize
;
134 /* Last anonymous kbd macro defined. */
135 Lisp_Object Vlast_kbd_macro_
;
137 /* Alist of system-specific X windows key symbols. */
138 Lisp_Object Vsystem_key_alist_
;
140 /* Cache for modify_event_symbol. */
141 Lisp_Object system_key_syms_
;
143 /* The kind of display: x, w32, ... */
144 Lisp_Object Vwindow_system_
;
146 /* Keymap mapping keys to alternative preferred forms.
147 See the DEFVAR for more documentation. */
148 Lisp_Object Vlocal_function_key_map_
;
150 /* Keymap mapping ASCII function key sequences onto their preferred
151 forms. Initialized by the terminal-specific lisp files. See the
152 DEFVAR for more documentation. */
153 Lisp_Object Vinput_decode_map_
;
155 /* Minibufferless frames on this display use this frame's minibuffer. */
156 Lisp_Object Vdefault_minibuffer_frame_
;
158 /* Number of displays using this KBOARD. Normally 1, but can be
159 larger when you have multiple screens on a single X display. */
162 /* The text we're echoing in the modeline - partial key sequences,
163 usually. This is nil when not echoing. */
164 Lisp_Object echo_string_
;
166 /* This flag indicates that events were put into kbd_queue
167 while Emacs was running for some other KBOARD.
168 The flag means that, when Emacs goes into the any-kboard state again,
169 it should check this KBOARD to see if there is a complete command
172 Note that the kbd_queue field can be non-nil even when
173 kbd_queue_has_data is 0. When we push back an incomplete
174 command, then this flag is 0, meaning we don't want to try
175 reading from this KBOARD again until more input arrives. */
176 bool_bf kbd_queue_has_data
;
178 /* True means echo each character as typed. */
179 bool_bf immediate_echo
: 1;
181 /* If we have a prompt string specified by the user, this is it. */
182 Lisp_Object echo_prompt_
;
186 kset_default_minibuffer_frame (struct kboard
*kb
, Lisp_Object val
)
188 kb
->Vdefault_minibuffer_frame_
= val
;
191 kset_defining_kbd_macro (struct kboard
*kb
, Lisp_Object val
)
193 kb
->defining_kbd_macro_
= val
;
196 kset_input_decode_map (struct kboard
*kb
, Lisp_Object val
)
198 kb
->Vinput_decode_map_
= val
;
201 kset_last_command (struct kboard
*kb
, Lisp_Object val
)
203 kb
->Vlast_command_
= val
;
206 kset_last_kbd_macro (struct kboard
*kb
, Lisp_Object val
)
208 kb
->Vlast_kbd_macro_
= val
;
211 kset_prefix_arg (struct kboard
*kb
, Lisp_Object val
)
213 kb
->Vprefix_arg_
= val
;
216 kset_system_key_alist (struct kboard
*kb
, Lisp_Object val
)
218 kb
->Vsystem_key_alist_
= val
;
221 kset_window_system (struct kboard
*kb
, Lisp_Object val
)
223 kb
->Vwindow_system_
= val
;
226 union buffered_input_event
228 ENUM_BF (event_kind
) kind
: EVENT_KIND_WIDTH
;
229 struct input_event ie
;
231 struct selection_input_event sie
;
235 /* Temporarily used before a frame has been opened. */
236 extern KBOARD
*initial_kboard
;
238 /* In the single-kboard state, this is the kboard
239 from which input is accepted.
241 In the any-kboard state, this is the kboard from which we are
242 right now considering input. We can consider input from another
243 kboard, but doing so requires throwing to wrong_kboard_jmpbuf. */
244 extern KBOARD
*current_kboard
;
246 /* Total number of times read_char has returned, modulo UINTMAX_MAX + 1. */
247 extern uintmax_t num_input_events
;
249 /* Nonzero means polling for input is temporarily suppressed. */
250 extern int poll_suppress_count
;
252 /* Vector holding the key sequence that invoked the current command.
253 It is reused for each command, and it may be longer than the current
254 sequence; this_command_key_count indicates how many elements
255 actually mean something. */
256 extern Lisp_Object this_command_keys
;
257 extern ptrdiff_t this_command_key_count
;
259 /* The frame in which the last input event occurred, or Qmacro if the
260 last event came from a macro. We use this to determine when to
261 generate switch-frame events. This may be cleared by functions
262 like Fselect_frame, to make sure that a switch-frame event is
263 generated by the next character. */
264 extern Lisp_Object internal_last_event_frame
;
266 /* This holds a Lisp vector that holds the properties of a single
267 menu item while decoding it in parse_menu_item.
268 Using a Lisp vector to hold this information while we decode it
269 takes care of protecting all the data from GC. */
270 extern Lisp_Object item_properties
;
272 /* This describes the elements of item_properties.
273 The first element is not a property, it is a pointer to the item properties
274 that is saved for GC protection. */
275 #define ITEM_PROPERTY_ITEM 0
276 /* The item string. */
277 #define ITEM_PROPERTY_NAME 1
278 /* Start of initialize to nil */
279 /* The binding: nil, a command or a keymap. */
280 #define ITEM_PROPERTY_DEF 2
281 /* The keymap if the binding is a keymap, otherwise nil. */
282 #define ITEM_PROPERTY_MAP 3
283 /* Nil, :radio or :toggle. */
284 #define ITEM_PROPERTY_TYPE 4
285 /* Nil or a string describing an equivalent key binding. */
286 #define ITEM_PROPERTY_KEYEQ 5
287 /* Not nil if a selected toggle box or radio button, otherwise nil. */
288 #define ITEM_PROPERTY_SELECTED 6
289 /* Place for a help string. Not yet used. */
290 #define ITEM_PROPERTY_HELP 7
291 /* Start of initialize to t */
293 /* Not nil if item is enabled. */
294 #define ITEM_PROPERTY_ENABLE 8
296 /* This holds a Lisp vector that holds the results of decoding
297 the keymaps or alist-of-alists that specify a menu.
299 It describes the panes and items within the panes.
301 Each pane is described by 3 elements in the vector:
302 t, the pane name, the pane's prefix key.
303 Then follow the pane's items, with 5 elements per item:
304 the item string, the enable flag, the item's value,
305 the definition, and the equivalent keyboard key's description string.
307 In some cases, multiple levels of menus may be described.
308 A single vector slot containing nil indicates the start of a submenu.
309 A single vector slot containing lambda indicates the end of a submenu.
310 The submenu follows a menu item which is the way to reach the submenu.
312 A single vector slot containing quote indicates that the
313 following items should appear on the right of a dialog box.
315 Using a Lisp vector to hold this information while we decode it
316 takes care of protecting all the data from GC. */
317 extern Lisp_Object menu_items
;
319 /* If non-nil, means that the global vars defined here are already in use.
320 Used to detect cases where we try to re-enter this non-reentrant code. */
321 extern Lisp_Object menu_items_inuse
;
323 /* Number of slots currently allocated in menu_items. */
324 extern int menu_items_allocated
;
326 /* This is the index in menu_items of the first empty slot. */
327 extern int menu_items_used
;
329 /* The number of panes currently recorded in menu_items,
330 excluding those within submenus. */
331 extern int menu_items_n_panes
;
333 #define MENU_ITEMS_PANE_NAME 1
334 #define MENU_ITEMS_PANE_PREFIX 2
335 #define MENU_ITEMS_PANE_LENGTH 3
339 MENU_ITEMS_ITEM_NAME
= 0,
340 MENU_ITEMS_ITEM_ENABLE
,
341 MENU_ITEMS_ITEM_VALUE
,
342 MENU_ITEMS_ITEM_EQUIV_KEY
,
343 MENU_ITEMS_ITEM_DEFINITION
,
344 MENU_ITEMS_ITEM_TYPE
,
345 MENU_ITEMS_ITEM_SELECTED
,
346 MENU_ITEMS_ITEM_HELP
,
347 MENU_ITEMS_ITEM_LENGTH
350 extern void unuse_menu_items (void);
352 /* This is how to deal with multibyte text if HAVE_MULTILINGUAL_MENU
353 isn't defined. The use of HAVE_MULTILINGUAL_MENU could probably be
354 confined to an extended version of this with sections of code below
355 using it unconditionally. */
357 #if defined (USE_GTK) || defined (HAVE_NS)
358 # define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
359 #elif defined HAVE_X_I18N
360 #define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
362 #define ENCODE_MENU_STRING(str) string_make_unibyte (str)
364 #else /* HAVE_NTGUI */
365 #define ENCODE_MENU_STRING(str) (str)
368 /* Macros for dealing with lispy events. */
370 /* True if EVENT has data fields describing it (i.e. a mouse click). */
371 #define EVENT_HAS_PARAMETERS(event) (CONSP (event))
373 /* Extract the head from an event.
374 This works on composite and simple events. */
375 #define EVENT_HEAD(event) \
376 (EVENT_HAS_PARAMETERS (event) ? XCAR (event) : (event))
378 /* Extract the starting and ending positions from a composite event. */
379 #define EVENT_START(event) (CAR_SAFE (CDR_SAFE (event)))
380 #define EVENT_END(event) (CAR_SAFE (CDR_SAFE (CDR_SAFE (event))))
382 /* Extract the click count from a multi-click event. */
383 #define EVENT_CLICK_COUNT(event) (Fnth (make_number (2), (event)))
385 /* Extract the fields of a position. */
386 #define POSN_WINDOW(posn) (CAR_SAFE (posn))
387 #define POSN_POSN(posn) (CAR_SAFE (CDR_SAFE (posn)))
388 #define POSN_SET_POSN(posn,x) (XSETCAR (XCDR (posn), (x)))
389 #define POSN_WINDOW_POSN(posn) (CAR_SAFE (CDR_SAFE (CDR_SAFE (posn))))
390 #define POSN_TIMESTAMP(posn) (CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (posn)))))
391 #define POSN_SCROLLBAR_PART(posn) (Fnth (make_number (4), (posn)))
393 /* A cons (STRING . STRING-CHARPOS), or nil in mouse-click events.
394 It's a cons if the click is over a string in the mode line. */
396 #define POSN_STRING(posn) (Fnth (make_number (4), (posn)))
398 /* If POSN_STRING is nil, event refers to buffer location. */
400 #define POSN_INBUFFER_P(posn) (NILP (POSN_STRING (posn)))
401 #define POSN_BUFFER_POSN(posn) (Fnth (make_number (5), (posn)))
403 /* Getting the kind of an event head. */
404 #define EVENT_HEAD_KIND(event_head) \
405 (Fget ((event_head), Qevent_kind))
407 /* True while doing kbd input. */
408 extern bool waiting_for_input
;
410 /* Address (if not 0) of struct timespec to zero out if a SIGIO interrupt
412 extern struct timespec
*input_available_clear_time
;
414 extern bool ignore_mouse_drag_p
;
416 extern Lisp_Object
parse_modifiers (Lisp_Object
);
417 extern Lisp_Object
reorder_modifiers (Lisp_Object
);
418 extern Lisp_Object
read_char (int, Lisp_Object
, Lisp_Object
,
419 bool *, struct timespec
*);
420 extern int parse_solitary_modifier (Lisp_Object symbol
);
423 /* This is like Vthis_command, except that commands never set it. */
424 extern Lisp_Object real_this_command
;
426 extern int quit_char
;
428 extern unsigned int timers_run
;
430 extern bool menu_separator_name_p (const char *);
431 extern bool parse_menu_item (Lisp_Object
, int);
433 extern KBOARD
*allocate_kboard (Lisp_Object
);
434 extern void delete_kboard (KBOARD
*);
435 extern void not_single_kboard_state (KBOARD
*);
436 extern void push_kboard (struct kboard
*);
437 extern void push_frame_kboard (struct frame
*);
438 extern void pop_kboard (void);
439 extern void temporarily_switch_to_single_kboard (struct frame
*);
440 extern void record_asynch_buffer_change (void);
441 extern void input_poll_signal (int);
442 extern void start_polling (void);
443 extern void stop_polling (void);
444 extern void set_poll_suppress_count (int);
445 extern int gobble_input (void);
446 extern bool input_polling_used (void);
447 extern void clear_input_pending (void);
448 extern bool requeued_events_pending_p (void);
449 extern void bind_polling_period (int);
450 extern int make_ctrl_char (int) ATTRIBUTE_CONST
;
451 extern void stuff_buffered_input (Lisp_Object
);
452 extern void clear_waiting_for_input (void);
453 extern void swallow_events (bool);
454 extern bool lucid_event_type_list_p (Lisp_Object
);
455 extern void kbd_buffer_store_event (struct input_event
*);
456 extern void kbd_buffer_store_buffered_event (union buffered_input_event
*,
457 struct input_event
*);
459 kbd_buffer_store_event_hold (struct input_event
*event
,
460 struct input_event
*hold_quit
)
462 verify (alignof (struct input_event
) == alignof (union buffered_input_event
)
463 && sizeof (struct input_event
) == sizeof (union buffered_input_event
));
464 kbd_buffer_store_buffered_event ((union buffered_input_event
*) event
,
468 extern void kbd_buffer_unget_event (struct selection_input_event
*);
470 extern void poll_for_input_1 (void);
471 extern void show_help_echo (Lisp_Object
, Lisp_Object
, Lisp_Object
,
473 extern void gen_help_event (Lisp_Object
, Lisp_Object
, Lisp_Object
,
474 Lisp_Object
, ptrdiff_t);
475 extern void kbd_buffer_store_help_event (Lisp_Object
, Lisp_Object
);
476 extern Lisp_Object
menu_item_eval_property (Lisp_Object
);
477 extern bool kbd_buffer_events_waiting (void);
478 extern void add_user_signal (int, const char *);
480 extern int tty_read_avail_input (struct terminal
*, struct input_event
*);
481 extern struct timespec
timer_check (void);
482 extern void mark_kboards (void);
485 extern const char *const lispy_function_keys
[];
490 #endif /* EMACS_KEYBOARD_H */