Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
11939 lines (10152 sloc) 361 KB
/* Keyboard and mouse input; editor command loop.
Copyright (C) 1985-1989, 1993-1997, 1999-2015 Free Software Foundation,
Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "sysstdio.h"
#include <sys/stat.h>
#include "lisp.h"
#include "termchar.h"
#include "termopts.h"
#include "frame.h"
#include "termhooks.h"
#include "macros.h"
#include "keyboard.h"
#include "window.h"
#include "commands.h"
#include "character.h"
#include "buffer.h"
#include "disptab.h"
#include "dispextern.h"
#include "syntax.h"
#include "intervals.h"
#include "keymap.h"
#include "blockinput.h"
#include "puresize.h"
#include "systime.h"
#include "atimer.h"
#include "process.h"
#include <errno.h>
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#ifdef MSDOS
#include "msdos.h"
#include <time.h>
#else /* not MSDOS */
#include <sys/ioctl.h>
#endif /* not MSDOS */
#if defined USABLE_FIONREAD && defined USG5_4
# include <sys/filio.h>
#endif
#include "syssignal.h"
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */
/* Variables for blockinput.h: */
/* Positive if interrupt input is blocked right now. */
volatile int interrupt_input_blocked;
/* True means an input interrupt or alarm signal has arrived.
The QUIT macro checks this. */
volatile bool pending_signals;
#define KBD_BUFFER_SIZE 4096
KBOARD *initial_kboard;
KBOARD *current_kboard;
static KBOARD *all_kboards;
/* True in the single-kboard state, false in the any-kboard state. */
static bool single_kboard;
/* Non-nil disable property on a command means
do not execute it; call disabled-command-function's value instead. */
Lisp_Object Qdisabled;
static Lisp_Object Qdisabled_command_function;
#define NUM_RECENT_KEYS (300)
/* Index for storing next element into recent_keys. */
static int recent_keys_index;
/* Total number of elements stored into recent_keys. */
static int total_keys;
/* This vector holds the last NUM_RECENT_KEYS keystrokes. */
static Lisp_Object recent_keys;
/* Vector holding the key sequence that invoked the current command.
It is reused for each command, and it may be longer than the current
sequence; this_command_key_count indicates how many elements
actually mean something.
It's easier to staticpro a single Lisp_Object than an array. */
Lisp_Object this_command_keys;
ptrdiff_t this_command_key_count;
/* True after calling Freset_this_command_lengths.
Usually it is false. */
static bool this_command_key_count_reset;
/* This vector is used as a buffer to record the events that were actually read
by read_key_sequence. */
static Lisp_Object raw_keybuf;
static int raw_keybuf_count;
#define GROW_RAW_KEYBUF \
if (raw_keybuf_count == ASIZE (raw_keybuf)) \
raw_keybuf = larger_vector (raw_keybuf, 1, -1)
/* Number of elements of this_command_keys
that precede this key sequence. */
static ptrdiff_t this_single_command_key_start;
/* Record values of this_command_key_count and echo_length ()
before this command was read. */
static ptrdiff_t before_command_key_count;
static ptrdiff_t before_command_echo_length;
/* For longjmp to where kbd input is being done. */
static sys_jmp_buf getcjmp;
/* True while doing kbd input. */
bool waiting_for_input;
/* True while displaying for echoing. Delays C-g throwing. */
static bool echoing;
/* Non-null means we can start echoing at the next input pause even
though there is something in the echo area. */
static struct kboard *ok_to_echo_at_next_pause;
/* The kboard last echoing, or null for none. Reset to 0 in
cancel_echoing. If non-null, and a current echo area message
exists, and echo_message_buffer is eq to the current message
buffer, we know that the message comes from echo_kboard. */
struct kboard *echo_kboard;
/* The buffer used for echoing. Set in echo_now, reset in
cancel_echoing. */
Lisp_Object echo_message_buffer;
/* True means C-g should cause immediate error-signal. */
bool immediate_quit;
/* Character that causes a quit. Normally C-g.
If we are running on an ordinary terminal, this must be an ordinary
ASCII char, since we want to make it our interrupt character.
If we are not running on an ordinary terminal, it still needs to be
an ordinary ASCII char. This character needs to be recognized in
the input interrupt handler. At this point, the keystroke is
represented as a struct input_event, while the desired quit
character is specified as a lispy event. The mapping from struct
input_events to lispy events cannot run in an interrupt handler,
and the reverse mapping is difficult for anything but ASCII
keystrokes.
FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
ASCII character. */
int quit_char;
/* Current depth in recursive edits. */
EMACS_INT command_loop_level;
/* If not Qnil, this is a switch-frame event which we decided to put
off until the end of a key sequence. This should be read as the
next command input, after any unread_command_events.
read_key_sequence uses this to delay switch-frame events until the
end of the key sequence; Fread_char uses it to put off switch-frame
events until a non-ASCII event is acceptable as input. */
Lisp_Object unread_switch_frame;
/* Last size recorded for a current buffer which is not a minibuffer. */
static ptrdiff_t last_non_minibuf_size;
/* Total number of times read_char has returned, modulo UINTMAX_MAX + 1. */
uintmax_t num_input_events;
/* Value of num_nonmacro_input_events as of last auto save. */
static EMACS_INT last_auto_save;
/* The value of point when the last command was started. */
static ptrdiff_t last_point_position;
/* The frame in which the last input event occurred, or Qmacro if the
last event came from a macro. We use this to determine when to
generate switch-frame events. This may be cleared by functions
like Fselect_frame, to make sure that a switch-frame event is
generated by the next character.
FIXME: This is modified by a signal handler so it should be volatile.
It's exported to Lisp, though, so it can't simply be marked
'volatile' here. */
Lisp_Object internal_last_event_frame;
static Lisp_Object Qx_set_selection, Qhandle_switch_frame;
static Lisp_Object Qhandle_select_window;
Lisp_Object QPRIMARY;
static Lisp_Object Qself_insert_command;
static Lisp_Object Qforward_char;
static Lisp_Object Qbackward_char;
Lisp_Object Qundefined;
static Lisp_Object Qtimer_event_handler;
/* read_key_sequence stores here the command definition of the
key sequence that it reads. */
static Lisp_Object read_key_sequence_cmd;
static Lisp_Object read_key_sequence_remapped;
static Lisp_Object Qinput_method_function;
static Lisp_Object Qdeactivate_mark;
Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
static Lisp_Object Qecho_area_clear_hook;
/* Hooks to run before and after each command. */
static Lisp_Object Qpre_command_hook;
static Lisp_Object Qpost_command_hook;
static Lisp_Object Qdeferred_action_function;
static Lisp_Object Qdelayed_warnings_hook;
static Lisp_Object Qinput_method_exit_on_first_char;
static Lisp_Object Qinput_method_use_echo_area;
static Lisp_Object Qhelp_form_show;
/* File in which we write all commands we read. */
static FILE *dribble;
/* Nonzero if input is available. */
bool input_pending;
/* Circular buffer for pre-read keyboard input. */
static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
/* Pointer to next available character in kbd_buffer.
If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the
next available char is in kbd_buffer[0]. */
static struct input_event *kbd_fetch_ptr;
/* Pointer to next place to store character in kbd_buffer. This
may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
character should go in kbd_buffer[0]. */
static struct input_event * volatile kbd_store_ptr;
/* The above pair of variables forms a "queue empty" flag. When we
enqueue a non-hook event, we increment kbd_store_ptr. When we
dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
there is input available if the two pointers are not equal.
Why not just have a flag set and cleared by the enqueuing and
dequeuing functions? Such a flag could be screwed up by interrupts
at inopportune times. */
/* Symbols to head events. */
static Lisp_Object Qmouse_movement;
static Lisp_Object Qscroll_bar_movement;
Lisp_Object Qswitch_frame;
static Lisp_Object Qfocus_in, Qfocus_out;
static Lisp_Object Qdelete_frame;
static Lisp_Object Qiconify_frame;
static Lisp_Object Qmake_frame_visible;
static Lisp_Object Qselect_window;
Lisp_Object Qhelp_echo;
static Lisp_Object Qmouse_fixup_help_message;
/* Symbols to denote kinds of events. */
static Lisp_Object Qfunction_key;
Lisp_Object Qmouse_click;
#ifdef HAVE_NTGUI
Lisp_Object Qlanguage_change;
#endif
static Lisp_Object Qdrag_n_drop;
static Lisp_Object Qsave_session;
#ifdef HAVE_MACGUI
Lisp_Object Qmac_apple_event;
#endif
#ifdef HAVE_DBUS
static Lisp_Object Qdbus_event;
#endif
#ifdef USE_FILE_NOTIFY
static Lisp_Object Qfile_notify;
#endif /* USE_FILE_NOTIFY */
static Lisp_Object Qconfig_changed_event;
/* Lisp_Object Qmouse_movement; - also an event header */
/* Properties of event headers. */
Lisp_Object Qevent_kind;
static Lisp_Object Qevent_symbol_elements;
/* Menu and tool bar item parts. */
static Lisp_Object Qmenu_enable;
static Lisp_Object QCenable, QCvisible, QChelp, QCkeys, QCkey_sequence;
Lisp_Object QCfilter;
/* Non-nil disable property on a command means
do not execute it; call disabled-command-function's value instead. */
Lisp_Object QCtoggle, QCradio;
static Lisp_Object QCbutton, QClabel;
static Lisp_Object QCvert_only;
/* An event header symbol HEAD may have a property named
Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
BASE is the base, unmodified version of HEAD, and MODIFIERS is the
mask of modifiers applied to it. If present, this is used to help
speed up parse_modifiers. */
Lisp_Object Qevent_symbol_element_mask;
/* An unmodified event header BASE may have a property named
Qmodifier_cache, which is an alist mapping modifier masks onto
modified versions of BASE. If present, this helps speed up
apply_modifiers. */
static Lisp_Object Qmodifier_cache;
/* Symbols to use for parts of windows. */
Lisp_Object Qmode_line;
Lisp_Object Qvertical_line;
Lisp_Object Qright_divider, Qbottom_divider;
static Lisp_Object Qvertical_scroll_bar;
Lisp_Object Qmenu_bar;
static Lisp_Object Qecho_keystrokes;
static void recursive_edit_unwind (Lisp_Object buffer);
static Lisp_Object command_loop (void);
static Lisp_Object Qcommand_execute;
struct timespec timer_check (void);
static void echo_now (void);
static ptrdiff_t echo_length (void);
static Lisp_Object Qpolling_period;
/* Incremented whenever a timer is run. */
unsigned timers_run;
/* Address (if not 0) of struct timespec to zero out if a SIGIO interrupt
happens. */
struct timespec *input_available_clear_time;
/* True means use SIGIO interrupts; false means use CBREAK mode.
Default is true if INTERRUPT_INPUT is defined. */
bool interrupt_input;
/* Nonzero while interrupts are temporarily deferred during redisplay. */
bool interrupts_deferred;
/* If we support a window system, turn on the code to poll periodically
to detect C-g. It isn't actually used when doing interrupt input. */
#ifdef HAVE_WINDOW_SYSTEM
#define POLL_FOR_INPUT
#endif
/* The time when Emacs started being idle. */
static struct timespec timer_idleness_start_time;
/* After Emacs stops being idle, this saves the last value
of timer_idleness_start_time from when it was idle. */
static struct timespec timer_last_idleness_start_time;
/* Global variable declarations. */
/* Flags for readable_events. */
#define READABLE_EVENTS_DO_TIMERS_NOW (1 << 0)
#define READABLE_EVENTS_FILTER_EVENTS (1 << 1)
#define READABLE_EVENTS_IGNORE_SQUEEZABLES (1 << 2)
/* Function for init_keyboard to call with no args (if nonzero). */
static void (*keyboard_init_hook) (void);
static bool get_input_pending (int);
static bool readable_events (int);
static Lisp_Object read_char_x_menu_prompt (Lisp_Object,
Lisp_Object, bool *);
static Lisp_Object read_char_minibuf_menu_prompt (int, Lisp_Object);
static Lisp_Object make_lispy_event (struct input_event *);
static Lisp_Object make_lispy_movement (struct frame *, Lisp_Object,
enum scroll_bar_part,
Lisp_Object, Lisp_Object,
Time);
static Lisp_Object modify_event_symbol (ptrdiff_t, int, Lisp_Object,
Lisp_Object, const char *const *,
Lisp_Object *, ptrdiff_t);
static Lisp_Object make_lispy_switch_frame (Lisp_Object);
static Lisp_Object make_lispy_focus_in (Lisp_Object);
#ifdef HAVE_WINDOW_SYSTEM
static Lisp_Object make_lispy_focus_out (Lisp_Object);
#endif /* HAVE_WINDOW_SYSTEM */
static bool help_char_p (Lisp_Object);
static void save_getcjmp (sys_jmp_buf);
static void restore_getcjmp (sys_jmp_buf);
static Lisp_Object apply_modifiers (int, Lisp_Object);
static void clear_event (struct input_event *);
static void restore_kboard_configuration (int);
#ifdef USABLE_SIGIO
static void deliver_input_available_signal (int signo);
#endif
static void handle_interrupt (bool);
static _Noreturn void quit_throw_to_read_char (bool);
static void process_special_events (void);
static void timer_start_idle (void);
static void timer_stop_idle (void);
static void timer_resume_idle (void);
static void deliver_user_signal (int);
static char *find_user_signal_name (int);
static void store_user_signal_events (void);
/* These setters are used only in this file, so they can be private. */
static void
kset_echo_string (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (echo_string) = val;
}
static void
kset_kbd_queue (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (kbd_queue) = val;
}
static void
kset_keyboard_translate_table (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (Vkeyboard_translate_table) = val;
}
static void
kset_last_prefix_arg (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (Vlast_prefix_arg) = val;
}
static void
kset_last_repeatable_command (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (Vlast_repeatable_command) = val;
}
static void
kset_local_function_key_map (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (Vlocal_function_key_map) = val;
}
static void
kset_overriding_terminal_local_map (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (Voverriding_terminal_local_map) = val;
}
static void
kset_real_last_command (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (Vreal_last_command) = val;
}
static void
kset_system_key_syms (struct kboard *kb, Lisp_Object val)
{
kb->INTERNAL_FIELD (system_key_syms) = val;
}
/* Add C to the echo string, without echoing it immediately. C can be
a character, which is pretty-printed, or a symbol, whose name is
printed. */
static void
echo_add_key (Lisp_Object c)
{
int size = KEY_DESCRIPTION_SIZE + 100;
char *buffer = alloca (size);
char *ptr = buffer;
Lisp_Object echo_string;
echo_string = KVAR (current_kboard, echo_string);
/* If someone has passed us a composite event, use its head symbol. */
c = EVENT_HEAD (c);
if (INTEGERP (c))
ptr = push_key_description (XINT (c), ptr);
else if (SYMBOLP (c))
{
Lisp_Object name = SYMBOL_NAME (c);
int nbytes = SBYTES (name);
if (size - (ptr - buffer) < nbytes)
{
int offset = ptr - buffer;
size = max (2 * size, size + nbytes);
buffer = alloca (size);
ptr = buffer + offset;
}
ptr += copy_text (SDATA (name), (unsigned char *) ptr, nbytes,
STRING_MULTIBYTE (name), 1);
}
if ((NILP (echo_string) || SCHARS (echo_string) == 0)
&& help_char_p (c))
{
const char *text = " (Type ? for further options)";
int len = strlen (text);
if (size - (ptr - buffer) < len)
{
int offset = ptr - buffer;
size += len;
buffer = alloca (size);
ptr = buffer + offset;
}
memcpy (ptr, text, len);
ptr += len;
}
/* Replace a dash from echo_dash with a space, otherwise add a space
at the end as a separator between keys. */
if (STRINGP (echo_string) && SCHARS (echo_string) > 1)
{
Lisp_Object last_char, prev_char, idx;
idx = make_number (SCHARS (echo_string) - 2);
prev_char = Faref (echo_string, idx);
idx = make_number (SCHARS (echo_string) - 1);
last_char = Faref (echo_string, idx);
/* We test PREV_CHAR to make sure this isn't the echoing of a
minus-sign. */
if (XINT (last_char) == '-' && XINT (prev_char) != ' ')
Faset (echo_string, idx, make_number (' '));
else
echo_string = concat2 (echo_string, build_string (" "));
}
else if (STRINGP (echo_string) && SCHARS (echo_string) > 0)
echo_string = concat2 (echo_string, build_string (" "));
kset_echo_string
(current_kboard,
concat2 (echo_string, make_string (buffer, ptr - buffer)));
}
/* Add C to the echo string, if echoing is going on. C can be a
character or a symbol. */
static void
echo_char (Lisp_Object c)
{
if (current_kboard->immediate_echo)
{
echo_add_key (c);
echo_now ();
}
}
/* Temporarily add a dash to the end of the echo string if it's not
empty, so that it serves as a mini-prompt for the very next
character. */
static void
echo_dash (void)
{
/* Do nothing if not echoing at all. */
if (NILP (KVAR (current_kboard, echo_string)))
return;
if (this_command_key_count == 0)
return;
if (!current_kboard->immediate_echo
&& SCHARS (KVAR (current_kboard, echo_string)) == 0)
return;
/* Do nothing if we just printed a prompt. */
if (current_kboard->echo_after_prompt
== SCHARS (KVAR (current_kboard, echo_string)))
return;
/* Do nothing if we have already put a dash at the end. */
if (SCHARS (KVAR (current_kboard, echo_string)) > 1)
{
Lisp_Object last_char, prev_char, idx;
idx = make_number (SCHARS (KVAR (current_kboard, echo_string)) - 2);
prev_char = Faref (KVAR (current_kboard, echo_string), idx);
idx = make_number (SCHARS (KVAR (current_kboard, echo_string)) - 1);
last_char = Faref (KVAR (current_kboard, echo_string), idx);
if (XINT (last_char) == '-' && XINT (prev_char) != ' ')
return;
}
/* Put a dash at the end of the buffer temporarily,
but make it go away when the next character is added. */
kset_echo_string
(current_kboard,
concat2 (KVAR (current_kboard, echo_string), build_string ("-")));
echo_now ();
}
/* Display the current echo string, and begin echoing if not already
doing so. */
static void
echo_now (void)
{
if (!current_kboard->immediate_echo)
{
ptrdiff_t i;
current_kboard->immediate_echo = 1;
for (i = 0; i < this_command_key_count; i++)
{
Lisp_Object c;
/* Set before_command_echo_length to the value that would
have been saved before the start of this subcommand in
command_loop_1, if we had already been echoing then. */
if (i == this_single_command_key_start)
before_command_echo_length = echo_length ();
c = AREF (this_command_keys, i);
if (! (EVENT_HAS_PARAMETERS (c)
&& EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
echo_char (c);
}
/* Set before_command_echo_length to the value that would
have been saved before the start of this subcommand in
command_loop_1, if we had already been echoing then. */
if (this_command_key_count == this_single_command_key_start)
before_command_echo_length = echo_length ();
/* Put a dash at the end to invite the user to type more. */
echo_dash ();
}
echoing = 1;
/* FIXME: Use call (Qmessage) so it can be advised (e.g. emacspeak). */
message3_nolog (KVAR (current_kboard, echo_string));
echoing = 0;
/* Record in what buffer we echoed, and from which kboard. */
echo_message_buffer = echo_area_buffer[0];
echo_kboard = current_kboard;
if (waiting_for_input && !NILP (Vquit_flag))
quit_throw_to_read_char (0);
}
/* Turn off echoing, for the start of a new command. */
void
cancel_echoing (void)
{
current_kboard->immediate_echo = 0;
current_kboard->echo_after_prompt = -1;
kset_echo_string (current_kboard, Qnil);
ok_to_echo_at_next_pause = NULL;
echo_kboard = NULL;
echo_message_buffer = Qnil;
}
/* Return the length of the current echo string. */
static ptrdiff_t
echo_length (void)
{
return (STRINGP (KVAR (current_kboard, echo_string))
? SCHARS (KVAR (current_kboard, echo_string))
: 0);
}
/* Truncate the current echo message to its first LEN chars.
This and echo_char get used by read_key_sequence when the user
switches frames while entering a key sequence. */
static void
echo_truncate (ptrdiff_t nchars)
{
if (STRINGP (KVAR (current_kboard, echo_string)))
kset_echo_string (current_kboard,
Fsubstring (KVAR (current_kboard, echo_string),
make_number (0), make_number (nchars)));
truncate_echo_area (nchars);
}
/* Functions for manipulating this_command_keys. */
static void
add_command_key (Lisp_Object key)
{
#if 0 /* Not needed after we made Freset_this_command_lengths
do the job immediately. */
/* If reset-this-command-length was called recently, obey it now.
See the doc string of that function for an explanation of why. */
if (before_command_restore_flag)
{
this_command_key_count = before_command_key_count_1;
if (this_command_key_count < this_single_command_key_start)
this_single_command_key_start = this_command_key_count;
echo_truncate (before_command_echo_length_1);
before_command_restore_flag = 0;
}
#endif
if (this_command_key_count >= ASIZE (this_command_keys))
this_command_keys = larger_vector (this_command_keys, 1, -1);
ASET (this_command_keys, this_command_key_count, key);
++this_command_key_count;
}
Lisp_Object
recursive_edit_1 (void)
{
ptrdiff_t count = SPECPDL_INDEX ();
Lisp_Object val;
if (command_loop_level > 0)
{
specbind (Qstandard_output, Qt);
specbind (Qstandard_input, Qt);
}
#ifdef HAVE_WINDOW_SYSTEM
/* The command loop has started an hourglass timer, so we have to
cancel it here, otherwise it will fire because the recursive edit
can take some time. Do not check for display_hourglass_p here,
because it could already be nil. */
cancel_hourglass ();
#endif
/* This function may have been called from a debugger called from
within redisplay, for instance by Edebugging a function called
from fontification-functions. We want to allow redisplay in
the debugging session.
The recursive edit is left with a `(throw exit ...)'. The `exit'
tag is not caught anywhere in redisplay, i.e. when we leave the
recursive edit, the original redisplay leading to the recursive
edit will be unwound. The outcome should therefore be safe. */
specbind (Qinhibit_redisplay, Qnil);
redisplaying_p = 0;
val = command_loop ();
if (EQ (val, Qt))
Fsignal (Qquit, Qnil);
/* Handle throw from read_minibuf when using minibuffer
while it's active but we're in another window. */
if (STRINGP (val))
xsignal1 (Qerror, val);
return unbind_to (count, Qnil);
}
/* When an auto-save happens, record the "time", and don't do again soon. */
void
record_auto_save (void)
{
last_auto_save = num_nonmacro_input_events;
}
/* Make an auto save happen as soon as possible at command level. */
#ifdef SIGDANGER
void
force_auto_save_soon (void)
{
last_auto_save = - auto_save_interval - 1;
record_asynch_buffer_change ();
}
#endif
DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
doc: /* Invoke the editor command loop recursively.
To get out of the recursive edit, a command can throw to `exit' -- for
instance `(throw 'exit nil)'.
If you throw a value other than t, `recursive-edit' returns normally
to the function that called it. Throwing a t value causes
`recursive-edit' to quit, so that control returns to the command loop
one level up.
This function is called by the editor initialization to begin editing. */)
(void)
{
ptrdiff_t count = SPECPDL_INDEX ();
Lisp_Object buffer;
/* If we enter while input is blocked, don't lock up here.
This may happen through the debugger during redisplay. */
if (input_blocked_p ())
return Qnil;
if (command_loop_level >= 0
&& current_buffer != XBUFFER (XWINDOW (selected_window)->contents))
buffer = Fcurrent_buffer ();
else
buffer = Qnil;
/* Don't do anything interesting between the increment and the
record_unwind_protect! Otherwise, we could get distracted and
never decrement the counter again. */
command_loop_level++;
update_mode_lines = 17;
record_unwind_protect (recursive_edit_unwind, buffer);
/* If we leave recursive_edit_1 below with a `throw' for instance,
like it is done in the splash screen display, we have to
make sure that we restore single_kboard as command_loop_1
would have done if it were left normally. */
if (command_loop_level > 0)
temporarily_switch_to_single_kboard (SELECTED_FRAME ());
recursive_edit_1 ();
return unbind_to (count, Qnil);
}
void
recursive_edit_unwind (Lisp_Object buffer)
{
if (BUFFERP (buffer))
Fset_buffer (buffer);
command_loop_level--;
update_mode_lines = 18;
}
#if 0 /* These two functions are now replaced with
temporarily_switch_to_single_kboard. */
static void
any_kboard_state ()
{
#if 0 /* Theory: if there's anything in Vunread_command_events,
it will right away be read by read_key_sequence,
and then if we do switch KBOARDS, it will go into the side
queue then. So we don't need to do anything special here -- rms. */
if (CONSP (Vunread_command_events))
{
current_kboard->kbd_queue
= nconc2 (Vunread_command_events, current_kboard->kbd_queue);
current_kboard->kbd_queue_has_data = 1;
}
Vunread_command_events = Qnil;
#endif
single_kboard = 0;
}
/* Switch to the single-kboard state, making current_kboard
the only KBOARD from which further input is accepted. */
void
single_kboard_state ()
{
single_kboard = 1;
}
#endif
/* If we're in single_kboard state for kboard KBOARD,
get out of it. */
void
not_single_kboard_state (KBOARD *kboard)
{
if (kboard == current_kboard)
single_kboard = 0;
}
/* Maintain a stack of kboards, so other parts of Emacs
can switch temporarily to the kboard of a given frame
and then revert to the previous status. */
struct kboard_stack
{
KBOARD *kboard;
struct kboard_stack *next;
};
static struct kboard_stack *kboard_stack;
void
push_kboard (struct kboard *k)
{
struct kboard_stack *p = xmalloc (sizeof *p);
p->next = kboard_stack;
p->kboard = current_kboard;
kboard_stack = p;
current_kboard = k;
}
void
pop_kboard (void)
{
struct terminal *t;
struct kboard_stack *p = kboard_stack;
bool found = 0;
for (t = terminal_list; t; t = t->next_terminal)
{
if (t->kboard == p->kboard)
{
current_kboard = p->kboard;
found = 1;
break;
}
}
if (!found)
{
/* The terminal we remembered has been deleted. */
current_kboard = FRAME_KBOARD (SELECTED_FRAME ());
single_kboard = 0;
}
kboard_stack = p->next;
xfree (p);
}
/* Switch to single_kboard mode, making current_kboard the only KBOARD
from which further input is accepted. If F is non-nil, set its
KBOARD as the current keyboard.
This function uses record_unwind_protect_int to return to the previous
state later.
If Emacs is already in single_kboard mode, and F's keyboard is
locked, then this function will throw an error. */
void
temporarily_switch_to_single_kboard (struct frame *f)
{
bool was_locked = single_kboard;
if (was_locked)
{
if (f != NULL && FRAME_KBOARD (f) != current_kboard)
/* We can not switch keyboards while in single_kboard mode.
In rare cases, Lisp code may call `recursive-edit' (or
`read-minibuffer' or `y-or-n-p') after it switched to a
locked frame. For example, this is likely to happen
when server.el connects to a new terminal while Emacs is in
single_kboard mode. It is best to throw an error instead
of presenting the user with a frozen screen. */
error ("Terminal %d is locked, cannot read from it",
FRAME_TERMINAL (f)->id);
else
/* This call is unnecessary, but helps
`restore_kboard_configuration' discover if somebody changed
`current_kboard' behind our back. */
push_kboard (current_kboard);
}
else if (f != NULL)
current_kboard = FRAME_KBOARD (f);
single_kboard = 1;
record_unwind_protect_int (restore_kboard_configuration, was_locked);
}
#if 0 /* This function is not needed anymore. */
void
record_single_kboard_state ()
{
if (single_kboard)
push_kboard (current_kboard);
record_unwind_protect_int (restore_kboard_configuration, single_kboard);
}
#endif
static void
restore_kboard_configuration (int was_locked)
{
single_kboard = was_locked;
if (was_locked)
{
struct kboard *prev = current_kboard;
pop_kboard ();
/* The pop should not change the kboard. */
if (single_kboard && current_kboard != prev)
emacs_abort ();
}
}
/* Handle errors that are not handled at inner levels
by printing an error message and returning to the editor command loop. */
static Lisp_Object
cmd_error (Lisp_Object data)
{
Lisp_Object old_level, old_length;
char macroerror[sizeof "After..kbd macro iterations: "
+ INT_STRLEN_BOUND (EMACS_INT)];
#ifdef HAVE_WINDOW_SYSTEM
if (display_hourglass_p)
cancel_hourglass ();
#endif
if (!NILP (executing_kbd_macro))
{
if (executing_kbd_macro_iterations == 1)
sprintf (macroerror, "After 1 kbd macro iteration: ");
else
sprintf (macroerror, "After %"pI"d kbd macro iterations: ",
executing_kbd_macro_iterations);
}
else
*macroerror = 0;
Vstandard_output = Qt;
Vstandard_input = Qt;
Vexecuting_kbd_macro = Qnil;
executing_kbd_macro = Qnil;
kset_prefix_arg (current_kboard, Qnil);
kset_last_prefix_arg (current_kboard, Qnil);
cancel_echoing ();
/* Avoid unquittable loop if data contains a circular list. */
old_level = Vprint_level;
old_length = Vprint_length;
XSETFASTINT (Vprint_level, 10);
XSETFASTINT (Vprint_length, 10);
cmd_error_internal (data, macroerror);
Vprint_level = old_level;
Vprint_length = old_length;
Vquit_flag = Qnil;
Vinhibit_quit = Qnil;
return make_number (0);
}
/* Take actions on handling an error. DATA is the data that describes
the error.
CONTEXT is a C-string containing ASCII characters only which
describes the context in which the error happened. If we need to
generalize CONTEXT to allow multibyte characters, make it a Lisp
string. */
void
cmd_error_internal (Lisp_Object data, const char *context)
{
/* The immediate context is not interesting for Quits,
since they are asynchronous. */
if (EQ (XCAR (data), Qquit))
Vsignaling_function = Qnil;
Vquit_flag = Qnil;
Vinhibit_quit = Qt;
/* Use user's specified output function if any. */
if (!NILP (Vcommand_error_function))
call3 (Vcommand_error_function, data,
context ? build_string (context) : empty_unibyte_string,
Vsignaling_function);
Vsignaling_function = Qnil;
}
DEFUN ("command-error-default-function", Fcommand_error_default_function,
Scommand_error_default_function, 3, 3, 0,
doc: /* Produce default output for unhandled error message.
Default value of `command-error-function'. */)
(Lisp_Object data, Lisp_Object context, Lisp_Object signal)
{
struct frame *sf = SELECTED_FRAME ();
CHECK_STRING (context);
/* If the window system or terminal frame hasn't been initialized
yet, or we're not interactive, write the message to stderr and exit. */
if (!sf->glyphs_initialized_p
/* The initial frame is a special non-displaying frame. It
will be current in daemon mode when there are no frames
to display, and in non-daemon mode before the real frame
has finished initializing. If an error is thrown in the
latter case while creating the frame, then the frame
will never be displayed, so the safest thing to do is
write to stderr and quit. In daemon mode, there are
many other potential errors that do not prevent frames
from being created, so continuing as normal is better in
that case. */
|| (!IS_DAEMON && FRAME_INITIAL_P (sf))
|| noninteractive)
{
print_error_message (data, Qexternal_debugging_output,
SSDATA (context), signal);
Fterpri (Qexternal_debugging_output);
Fkill_emacs (make_number (-1));
}
else
{
clear_message (1, 0);
Fdiscard_input ();
message_log_maybe_newline ();
bitch_at_user ();
print_error_message (data, Qt, SSDATA (context), signal);
}
return Qnil;
}
static Lisp_Object command_loop_2 (Lisp_Object);
static Lisp_Object top_level_1 (Lisp_Object);
/* Entry to editor-command-loop.
This level has the catches for exiting/returning to editor command loop.
It returns nil to exit recursive edit, t to abort it. */
Lisp_Object
command_loop (void)
{
if (command_loop_level > 0 || minibuf_level > 0)
{
Lisp_Object val;
val = internal_catch (Qexit, command_loop_2, Qnil);
executing_kbd_macro = Qnil;
return val;
}
else
while (1)
{
#if MAC_USE_AUTORELEASE_LOOP
mac_autorelease_loop (^{
internal_catch (Qtop_level, top_level_1, Qnil);
return Qnil;
});
#else
#ifdef HAVE_MACGUI
void *pool = mac_alloc_autorelease_pool ();
#endif
internal_catch (Qtop_level, top_level_1, Qnil);
#ifdef HAVE_MACGUI
mac_release_autorelease_pool (pool);
#endif
#endif
internal_catch (Qtop_level, command_loop_2, Qnil);
executing_kbd_macro = Qnil;
/* End of file in -batch run causes exit here. */
if (noninteractive)
Fkill_emacs (Qt);
}
}
/* Here we catch errors in execution of commands within the
editing loop, and reenter the editing loop.
When there is an error, cmd_error runs and returns a non-nil
value to us. A value of nil means that command_loop_1 itself
returned due to end of file (or end of kbd macro). */
static Lisp_Object
command_loop_2 (Lisp_Object ignore)
{
#if MAC_USE_AUTORELEASE_LOOP
mac_autorelease_loop (^{
return internal_condition_case (command_loop_1, Qerror, cmd_error);
});
#else
register Lisp_Object val;
do
{
#ifdef HAVE_MACGUI
void *pool = mac_alloc_autorelease_pool ();
#endif
val = internal_condition_case (command_loop_1, Qerror, cmd_error);
#ifdef HAVE_MACGUI
mac_release_autorelease_pool (pool);
#endif
}
while (!NILP (val));
#endif
return Qnil;
}
static Lisp_Object
top_level_2 (void)
{
return Feval (Vtop_level, Qnil);
}
static Lisp_Object
top_level_1 (Lisp_Object ignore)
{
/* On entry to the outer level, run the startup file. */
if (!NILP (Vtop_level))
internal_condition_case (top_level_2, Qerror, cmd_error);
else if (!NILP (Vpurify_flag))
message1 ("Bare impure Emacs (standard Lisp code not loaded)");
else
message1 ("Bare Emacs (standard Lisp code not loaded)");
return Qnil;
}
DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
doc: /* Exit all recursive editing levels.
This also exits all active minibuffers. */)
(void)
{
#ifdef HAVE_WINDOW_SYSTEM
if (display_hourglass_p)
cancel_hourglass ();
#endif
/* Unblock input if we enter with input blocked. This may happen if
redisplay traps e.g. during tool-bar update with input blocked. */
totally_unblock_input ();
Fthrow (Qtop_level, Qnil);
}
static _Noreturn void
user_error (const char *msg)
{
xsignal1 (Quser_error, build_string (msg));
}
_Noreturn
DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
doc: /* Exit from the innermost recursive edit or minibuffer. */)
(void)
{
if (command_loop_level > 0 || minibuf_level > 0)
Fthrow (Qexit, Qnil);
user_error ("No recursive edit is in progress");
}
_Noreturn
DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
doc: /* Abort the command that requested this recursive edit or minibuffer input. */)
(void)
{
if (command_loop_level > 0 || minibuf_level > 0)
Fthrow (Qexit, Qt);
user_error ("No recursive edit is in progress");
}
/* Restore mouse tracking enablement. See Ftrack_mouse for the only use
of this function. */
static void
tracking_off (Lisp_Object old_value)
{
do_mouse_tracking = old_value;
if (NILP (old_value))
{
/* Redisplay may have been preempted because there was input
available, and it assumes it will be called again after the
input has been processed. If the only input available was
the sort that we have just disabled, then we need to call
redisplay. */
if (!readable_events (READABLE_EVENTS_DO_TIMERS_NOW))
{
redisplay_preserve_echo_area (6);
get_input_pending (READABLE_EVENTS_DO_TIMERS_NOW);
}
}
}
DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
doc: /* Evaluate BODY with mouse movement events enabled.
Within a `track-mouse' form, mouse motion generates input events that
you can read with `read-event'.
Normally, mouse motion is ignored.
usage: (track-mouse BODY...) */)
(Lisp_Object args)
{
ptrdiff_t count = SPECPDL_INDEX ();
Lisp_Object val;
record_unwind_protect (tracking_off, do_mouse_tracking);
do_mouse_tracking = Qt;
val = Fprogn (args);
return unbind_to (count, val);
}
/* If mouse has moved on some frame, return one of those frames.
Return 0 otherwise.
If ignore_mouse_drag_p is non-zero, ignore (implicit) mouse movement
after resizing the tool-bar window. */
#if !defined HAVE_WINDOW_SYSTEM || defined USE_GTK || defined HAVE_NS
static
#endif
bool ignore_mouse_drag_p;
static struct frame *
some_mouse_moved (void)
{
Lisp_Object tail, frame;
if (ignore_mouse_drag_p)
{
/* ignore_mouse_drag_p = 0; */
return 0;
}
FOR_EACH_FRAME (tail, frame)
{
if (XFRAME (frame)->mouse_moved)
return XFRAME (frame);
}
return 0;
}
/* This is the actual command reading loop,
sans error-handling encapsulation. */
static int read_key_sequence (Lisp_Object *, int, Lisp_Object,
bool, bool, bool, bool);
void safe_run_hooks (Lisp_Object);
static void adjust_point_for_property (ptrdiff_t, bool);
/* The last boundary auto-added to buffer-undo-list. */
Lisp_Object last_undo_boundary;
extern Lisp_Object Qregion_extract_function;
/* FIXME: This is wrong rather than test window-system, we should call
a new set-selection, which will then dispatch to x-set-selection, or
tty-set-selection, or w32-set-selection, ... */
Lisp_Object
command_loop_1 (void)
{
#if MAC_USE_AUTORELEASE_LOOP
#define BLOCK __block
#else
#define BLOCK
#endif
EMACS_INT BLOCK prev_modiff = 0;
struct buffer * BLOCK prev_buffer = NULL;
bool BLOCK already_adjusted = 0;
#undef BLOCK
kset_prefix_arg (current_kboard, Qnil);
kset_last_prefix_arg (current_kboard, Qnil);
Vdeactivate_mark = Qnil;
waiting_for_input = 0;
cancel_echoing ();
this_command_key_count = 0;
this_command_key_count_reset = 0;
this_single_command_key_start = 0;
if (NILP (Vmemory_full))
{
/* Make sure this hook runs after commands that get errors and
throw to top level. */
/* Note that the value cell will never directly contain nil
if the symbol is a local variable. */
if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
safe_run_hooks (Qpost_command_hook);
/* If displaying a message, resize the echo area window to fit
that message's size exactly. */
if (!NILP (echo_area_buffer[0]))
resize_echo_area_exactly ();
/* If there are warnings waiting, process them. */
if (!NILP (Vdelayed_warnings_list))
safe_run_hooks (Qdelayed_warnings_hook);
if (!NILP (Vdeferred_action_list))
safe_run_hooks (Qdeferred_action_function);
}
/* Do this after running Vpost_command_hook, for consistency. */
kset_last_command (current_kboard, Vthis_command);
kset_real_last_command (current_kboard, Vreal_this_command);
if (!CONSP (last_command_event))
kset_last_repeatable_command (current_kboard, Vreal_this_command);
#if MAC_USE_AUTORELEASE_LOOP
mac_autorelease_loop (^
#else
while (1)
#endif
{
#if defined (HAVE_MACGUI) && !MAC_USE_AUTORELEASE_LOOP
void *pool = mac_alloc_autorelease_pool ();
#endif
Lisp_Object cmd;
Lisp_Object keybuf[30];
int i;
if (! FRAME_LIVE_P (XFRAME (selected_frame)))
Fkill_emacs (Qnil);
/* Make sure the current window's buffer is selected. */
set_buffer_internal (XBUFFER (XWINDOW (selected_window)->contents));
/* Display any malloc warning that just came out. Use while because
displaying one warning can cause another. */
while (pending_malloc_warning)
display_malloc_warning ();
Vdeactivate_mark = Qnil;
/* Don't ignore mouse movements for more than a single command
loop. (This flag is set in xdisp.c whenever the tool bar is
resized, because the resize moves text up or down, and would
generate false mouse drag events if we don't ignore them.) */
ignore_mouse_drag_p = 0;
/* If minibuffer on and echo area in use,
wait a short time and redraw minibuffer. */
if (minibuf_level
&& !NILP (echo_area_buffer[0])
&& EQ (minibuf_window, echo_area_window)
&& NUMBERP (Vminibuffer_message_timeout))
{
/* Bind inhibit-quit to t so that C-g gets read in
rather than quitting back to the minibuffer. */
ptrdiff_t count = SPECPDL_INDEX ();
specbind (Qinhibit_quit, Qt);
sit_for (Vminibuffer_message_timeout, 0, 2);
/* Clear the echo area. */
message1 (0);
safe_run_hooks (Qecho_area_clear_hook);
unbind_to (count, Qnil);
/* If a C-g came in before, treat it as input now. */
if (!NILP (Vquit_flag))
{
Vquit_flag = Qnil;
Vunread_command_events = list1 (make_number (quit_char));
}
}
/* If it has changed current-menubar from previous value,
really recompute the menubar from the value. */
if (! NILP (Vlucid_menu_bar_dirty_flag)
&& !NILP (Ffboundp (Qrecompute_lucid_menubar)))
call0 (Qrecompute_lucid_menubar);
before_command_key_count = this_command_key_count;
before_command_echo_length = echo_length ();
Vthis_command = Qnil;
Vreal_this_command = Qnil;
Vthis_original_command = Qnil;
Vthis_command_keys_shift_translated = Qnil;
/* Read next key sequence; i gets its length. */
i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
Qnil, 0, 1, 1, 0);
/* A filter may have run while we were reading the input. */
if (! FRAME_LIVE_P (XFRAME (selected_frame)))
Fkill_emacs (Qnil);
set_buffer_internal (XBUFFER (XWINDOW (selected_window)->contents));
++num_input_keys;
/* Now we have read a key sequence of length I,
or else I is 0 and we found end of file. */
if (i == 0) /* End of file -- happens only in */
return Qnil; /* a kbd macro, at the end. */
/* -1 means read_key_sequence got a menu that was rejected.
Just loop around and read another command. */
if (i == -1)
{
cancel_echoing ();
this_command_key_count = 0;
this_command_key_count_reset = 0;
this_single_command_key_start = 0;
goto finalize;
}
last_command_event = keybuf[i - 1];
/* If the previous command tried to force a specific window-start,
forget about that, in case this command moves point far away
from that position. But also throw away beg_unchanged and
end_unchanged information in that case, so that redisplay will
update the whole window properly. */
if (XWINDOW (selected_window)->force_start)
{
struct buffer *b;
XWINDOW (selected_window)->force_start = 0;
b = XBUFFER (XWINDOW (selected_window)->contents);
BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
}
cmd = read_key_sequence_cmd;
if (!NILP (Vexecuting_kbd_macro))
{
if (!NILP (Vquit_flag))
{
Vexecuting_kbd_macro = Qt;
QUIT; /* Make some noise. */
/* Will return since macro now empty. */
}
}
/* Do redisplay processing after this command except in special
cases identified below. */
prev_buffer = current_buffer;
prev_modiff = MODIFF;
last_point_position = PT;
/* By default, we adjust point to a boundary of a region that
has such a property that should be treated intangible
(e.g. composition, display). But, some commands will set
this variable differently. */
Vdisable_point_adjustment = Qnil;
/* Process filters and timers may have messed with deactivate-mark.
reset it before we execute the command. */
Vdeactivate_mark = Qnil;
/* Remap command through active keymaps. */
Vthis_original_command = cmd;
if (!NILP (read_key_sequence_remapped))
cmd = read_key_sequence_remapped;
/* Execute the command. */
Vthis_command = cmd;
Vreal_this_command = cmd;
safe_run_hooks (Qpre_command_hook);
already_adjusted = 0;
if (NILP (Vthis_command))
/* nil means key is undefined. */
call0 (Qundefined);
else
{
/* Here for a command that isn't executed directly. */
#ifdef HAVE_WINDOW_SYSTEM
ptrdiff_t scount = SPECPDL_INDEX ();
if (display_hourglass_p
&& NILP (Vexecuting_kbd_macro))
{
record_unwind_protect_void (cancel_hourglass);
start_hourglass ();
}
#endif
if (NILP (KVAR (current_kboard, Vprefix_arg))) /* FIXME: Why? --Stef */
{
Lisp_Object undo = BVAR (current_buffer, undo_list);
Fundo_boundary ();
last_undo_boundary
= (EQ (undo, BVAR (current_buffer, undo_list))
? Qnil : BVAR (current_buffer, undo_list));
}
call1 (Qcommand_execute, Vthis_command);
#ifdef HAVE_WINDOW_SYSTEM
/* Do not check display_hourglass_p here, because
`command-execute' could change it, but we should cancel
hourglass cursor anyway.
But don't cancel the hourglass within a macro
just because a command in the macro finishes. */
if (NILP (Vexecuting_kbd_macro))
unbind_to (scount, Qnil);
#endif
}
kset_last_prefix_arg (current_kboard, Vcurrent_prefix_arg);
safe_run_hooks (Qpost_command_hook);
/* If displaying a message, resize the echo area window to fit
that message's size exactly. */
if (!NILP (echo_area_buffer[0]))
resize_echo_area_exactly ();
/* If there are warnings waiting, process them. */
if (!NILP (Vdelayed_warnings_list))
safe_run_hooks (Qdelayed_warnings_hook);
safe_run_hooks (Qdeferred_action_function);
/* If there is a prefix argument,
1) We don't want Vlast_command to be ``universal-argument''
(that would be dumb), so don't set Vlast_command,
2) we want to leave echoing on so that the prefix will be
echoed as part of this key sequence, so don't call
cancel_echoing, and
3) we want to leave this_command_key_count non-zero, so that
read_char will realize that it is re-reading a character, and
not echo it a second time.
If the command didn't actually create a prefix arg,
but is merely a frame event that is transparent to prefix args,
then the above doesn't apply. */
if (NILP (KVAR (current_kboard, Vprefix_arg))
|| CONSP (last_command_event))
{
kset_last_command (current_kboard, Vthis_command);
kset_real_last_command (current_kboard, Vreal_this_command);
if (!CONSP (last_command_event))
kset_last_repeatable_command (current_kboard, Vreal_this_command);
cancel_echoing ();
this_command_key_count = 0;
this_command_key_count_reset = 0;
this_single_command_key_start = 0;
}
if (!NILP (BVAR (current_buffer, mark_active))
&& !NILP (Vrun_hooks))
{
/* In Emacs 22, setting transient-mark-mode to `only' was a
way of turning it on for just one command. This usage is
obsolete, but support it anyway. */
if (EQ (Vtransient_mark_mode, Qidentity))
Vtransient_mark_mode = Qnil;
else if (EQ (Vtransient_mark_mode, Qonly))
Vtransient_mark_mode = Qidentity;
if (!NILP (Vdeactivate_mark))
/* If `select-active-regions' is non-nil, this call to
`deactivate-mark' also sets the PRIMARY selection. */
call0 (Qdeactivate_mark);
else
{
/* Even if not deactivating the mark, set PRIMARY if
`select-active-regions' is non-nil. */
if (!NILP (Fwindow_system (Qnil))
/* Even if mark_active is non-nil, the actual buffer
marker may not have been set yet (Bug#7044). */
&& XMARKER (BVAR (current_buffer, mark))->buffer
&& (EQ (Vselect_active_regions, Qonly)
? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
: (!NILP (Vselect_active_regions)
&& !NILP (Vtransient_mark_mode)))
&& NILP (Fmemq (Vthis_command,
Vselection_inhibit_update_commands)))
{
Lisp_Object txt
= call1 (Fsymbol_value (Qregion_extract_function), Qnil);
if (XINT (Flength (txt)) > 0)
/* Don't set empty selections. */
call2 (Qx_set_selection, QPRIMARY, txt);
}
if (current_buffer != prev_buffer || MODIFF != prev_modiff)
{
Lisp_Object hook = intern ("activate-mark-hook");
Frun_hooks (1, &hook);
}
}
Vsaved_region_selection = Qnil;
}
finalize:
if (current_buffer == prev_buffer
&& last_point_position != PT
&& NILP (Vdisable_point_adjustment)
&& NILP (Vglobal_disable_point_adjustment))
{
if (last_point_position > BEGV
&& last_point_position < ZV
&& (composition_adjust_point (last_point_position,
last_point_position)
!= last_point_position))
/* The last point was temporarily set within a grapheme
cluster to prevent automatic composition. To recover
the automatic composition, we must update the
display. */
windows_or_buffers_changed = 21;
if (!already_adjusted)
adjust_point_for_property (last_point_position,
MODIFF != prev_modiff);
}
/* Install chars successfully executed in kbd macro. */
if (!NILP (KVAR (current_kboard, defining_kbd_macro))
&& NILP (KVAR (current_kboard, Vprefix_arg)))
finalize_kbd_macro_chars ();
#if defined (HAVE_MACGUI) && !MAC_USE_AUTORELEASE_LOOP
mac_release_autorelease_pool (pool);
#endif
#if MAC_USE_AUTORELEASE_LOOP
return Qt;
#endif
}
#if MAC_USE_AUTORELEASE_LOOP
);
return Qnil;
#endif
}
Lisp_Object
read_menu_command (void)
{
Lisp_Object keybuf[30];
ptrdiff_t count = SPECPDL_INDEX ();
int i;
/* We don't want to echo the keystrokes while navigating the
menus. */
specbind (Qecho_keystrokes, make_number (0));
i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
Qnil, 0, 1, 1, 1);
unbind_to (count, Qnil);
if (! FRAME_LIVE_P (XFRAME (selected_frame)))
Fkill_emacs (Qnil);
if (i == 0 || i == -1)
return Qt;
return read_key_sequence_cmd;
}
/* Adjust point to a boundary of a region that has such a property
that should be treated intangible. For the moment, we check
`composition', `display' and `invisible' properties.
LAST_PT is the last position of point. */
static void
adjust_point_for_property (ptrdiff_t last_pt, bool modified)
{
ptrdiff_t beg, end;
Lisp_Object val, overlay, tmp;
/* When called after buffer modification, we should temporarily
suppress the point adjustment for automatic composition so that a
user can keep inserting another character at point or keep
deleting characters around point. */
bool check_composition = ! modified, check_display = 1, check_invisible = 1;
ptrdiff_t orig_pt = PT;
/* FIXME: cycling is probably not necessary because these properties
can't be usefully combined anyway. */
while (check_composition || check_display || check_invisible)
{
/* FIXME: check `intangible'. */
if (check_composition
&& PT > BEGV && PT < ZV
&& (beg = composition_adjust_point (last_pt, PT)) != PT)
{
SET_PT (beg);
check_display = check_invisible = 1;
}
check_composition = 0;
if (check_display
&& PT > BEGV && PT < ZV
&& !NILP (val = get_char_property_and_overlay
(make_number (PT), Qdisplay, Qnil, &overlay))
&& display_prop_intangible_p (val, overlay, PT, PT_BYTE)
&& (!OVERLAYP (overlay)
? get_property_and_range (PT, Qdisplay, &val, &beg, &end, Qnil)
: (beg = OVERLAY_POSITION (OVERLAY_START (overlay)),
end = OVERLAY_POSITION (OVERLAY_END (overlay))))
&& (beg < PT /* && end > PT <- It's always the case. */
|| (beg <= PT && STRINGP (val) && SCHARS (val) == 0)))
{
eassert (end > PT);
SET_PT (PT < last_pt
? (STRINGP (val) && SCHARS (val) == 0
? max (beg - 1, BEGV)
: beg)
: end);
check_composition = check_invisible = 1;
}
check_display = 0;
if (check_invisible && PT > BEGV && PT < ZV)
{
int inv;
bool ellipsis = 0;
beg = end = PT;
/* Find boundaries `beg' and `end' of the invisible area, if any. */
while (end < ZV
#if 0
/* FIXME: We should stop if we find a spot between
two runs of `invisible' where inserted text would
be visible. This is important when we have two
invisible boundaries that enclose an area: if the
area is empty, we need this test in order to make
it possible to place point in the middle rather
than skip both boundaries. However, this code
also stops anywhere in a non-sticky text-property,
which breaks (e.g.) Org mode. */
&& (val = Fget_pos_property (make_number (end),
Qinvisible, Qnil),
TEXT_PROP_MEANS_INVISIBLE (val))
#endif
&& !NILP (val = get_char_property_and_overlay
(make_number (end), Qinvisible, Qnil, &overlay))
&& (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
{
ellipsis = ellipsis || inv > 1
|| (OVERLAYP (overlay)
&& (!NILP (Foverlay_get (overlay, Qafter_string))
|| !NILP (Foverlay_get (overlay, Qbefore_string))));
tmp = Fnext_single_char_property_change
(make_number (end), Qinvisible, Qnil, Qnil);
end = NATNUMP (tmp) ? XFASTINT (tmp) : ZV;
}
while (beg > BEGV
#if 0
&& (val = Fget_pos_property (make_number (beg),
Qinvisible, Qnil),
TEXT_PROP_MEANS_INVISIBLE (val))
#endif
&& !NILP (val = get_char_property_and_overlay
(make_number (beg - 1), Qinvisible, Qnil, &overlay))
&& (inv = TEXT_PROP_MEANS_INVISIBLE (val)))
{
ellipsis = ellipsis || inv > 1
|| (OVERLAYP (overlay)
&& (!NILP (Foverlay_get (overlay, Qafter_string))
|| !NILP (Foverlay_get (overlay, Qbefore_string))));
tmp = Fprevious_single_char_property_change
(make_number (beg), Qinvisible, Qnil, Qnil);
beg = NATNUMP (tmp) ? XFASTINT (tmp) : BEGV;
}
/* Move away from the inside area. */
if (beg < PT && end > PT)
{
SET_PT ((orig_pt == PT && (last_pt < beg || last_pt > end))
/* We haven't moved yet (so we don't need to fear
infinite-looping) and we were outside the range
before (so either end of the range still corresponds
to a move in the right direction): pretend we moved
less than we actually did, so that we still have
more freedom below in choosing which end of the range
to go to. */
? (orig_pt = -1, PT < last_pt ? end : beg)
/* We either have moved already or the last point
was already in the range: we don't get to choose
which end of the range we have to go to. */
: (PT < last_pt ? beg : end));
check_composition = check_display = 1;
}
#if 0 /* This assertion isn't correct, because SET_PT may end up setting
the point to something other than its argument, due to
point-motion hooks, intangibility, etc. */
eassert (PT == beg || PT == end);
#endif
/* Pretend the area doesn't exist if the buffer is not
modified. */
if (!modified && !ellipsis && beg < end)
{
if (last_pt == beg && PT == end && end < ZV)
(check_composition = check_display = 1, SET_PT (end + 1));
else if (last_pt == end && PT == beg && beg > BEGV)
(check_composition = check_display = 1, SET_PT (beg - 1));
else if (PT == ((PT < last_pt) ? beg : end))
/* We've already moved as far as we can. Trying to go
to the other end would mean moving backwards and thus
could lead to an infinite loop. */
;
else if (val = Fget_pos_property (make_number (PT),
Qinvisible, Qnil),
TEXT_PROP_MEANS_INVISIBLE (val)
&& (val = (Fget_pos_property
(make_number (PT == beg ? end : beg),
Qinvisible, Qnil)),
!TEXT_PROP_MEANS_INVISIBLE (val)))
(check_composition = check_display = 1,
SET_PT (PT == beg ? end : beg));
}
}
check_invisible = 0;
}
}
/* Subroutine for safe_run_hooks: run the hook HOOK. */
static Lisp_Object
safe_run_hooks_1 (void)
{
eassert (CONSP (Vinhibit_quit));
return call0 (XCDR (Vinhibit_quit));
}
/* Subroutine for safe_run_hooks: handle an error by clearing out the function
from the hook. */
static Lisp_Object
safe_run_hooks_error (Lisp_Object error_data)
{
Lisp_Object hook
= CONSP (Vinhibit_quit) ? XCAR (Vinhibit_quit) : Vinhibit_quit;
Lisp_Object fun = CONSP (Vinhibit_quit) ? XCDR (Vinhibit_quit) : Qnil;
Lisp_Object args[4];
args[0] = build_string ("Error in %s (%S): %S");
args[1] = hook;
args[2] = fun;
args[3] = error_data;
Fmessage (4, args);
if (SYMBOLP (hook))
{
Lisp_Object val;
bool found = 0;
Lisp_Object newval = Qnil;
for (val = find_symbol_value (hook); CONSP (val); val = XCDR (val))
if (EQ (fun, XCAR (val)))
found = 1;
else
newval = Fcons (XCAR (val), newval);
if (found)
return Fset (hook, Fnreverse (newval));
/* Not found in the local part of the hook. Let's look at the global
part. */
newval = Qnil;
for (val = (NILP (Fdefault_boundp (hook)) ? Qnil
: Fdefault_value (hook));
CONSP (val); val = XCDR (val))
if (EQ (fun, XCAR (val)))
found = 1;
else
newval = Fcons (XCAR (val), newval);
if (found)
return Fset_default (hook, Fnreverse (newval));
}
return Qnil;
}
static Lisp_Object
safe_run_hook_funcall (ptrdiff_t nargs, Lisp_Object *args)
{
eassert (nargs == 1);
if (CONSP (Vinhibit_quit))
XSETCDR (Vinhibit_quit, args[0]);
else
Vinhibit_quit = Fcons (Vinhibit_quit, args[0]);
internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
return Qnil;
}
/* If we get an error while running the hook, cause the hook variable
to be nil. Also inhibit quits, so that C-g won't cause the hook
to mysteriously evaporate. */
void
safe_run_hooks (Lisp_Object hook)
{
/* FIXME: our `internal_condition_case' does not provide any way to pass data
to its body or to its handlers other than via globals such as
dynamically-bound variables ;-) */
ptrdiff_t count = SPECPDL_INDEX ();
specbind (Qinhibit_quit, hook);
run_hook_with_args (1, &hook, safe_run_hook_funcall);
unbind_to (count, Qnil);
}
/* Nonzero means polling for input is temporarily suppressed. */
int poll_suppress_count;
#ifdef POLL_FOR_INPUT
/* Asynchronous timer for polling. */
static struct atimer *poll_timer;
/* Poll for input, so that we catch a C-g if it comes in. */
void
poll_for_input_1 (void)
{
if (! input_blocked_p ()
&& !waiting_for_input)
gobble_input ();
}
/* Timer callback function for poll_timer. TIMER is equal to
poll_timer. */
static void
poll_for_input (struct atimer *timer)
{
if (poll_suppress_count == 0)
pending_signals = 1;
}
#endif /* POLL_FOR_INPUT */
/* Begin signals to poll for input, if they are appropriate.
This function is called unconditionally from various places. */
void
start_polling (void)
{
#ifdef POLL_FOR_INPUT
/* XXX This condition was (read_socket_hook && !interrupt_input),
but read_socket_hook is not global anymore. Let's pretend that
it's always set. */
if (!interrupt_input)
{
/* Turn alarm handling on unconditionally. It might have
been turned off in process.c. */
turn_on_atimers (1);
/* If poll timer doesn't exist, or we need one with
a different interval, start a new one. */
if (poll_timer == NULL
|| poll_timer->interval.tv_sec != polling_period)
{
time_t period = max (1, min (polling_period, TYPE_MAXIMUM (time_t)));
struct timespec interval = make_timespec (period, 0);
if (poll_timer)
cancel_atimer (poll_timer);
poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
poll_for_input, NULL);
}
/* Let the timer's callback function poll for input
if this becomes zero. */
--poll_suppress_count;
}
#endif
}
/* True if we are using polling to handle input asynchronously. */
bool
input_polling_used (void)
{
#ifdef POLL_FOR_INPUT
/* XXX This condition was (read_socket_hook && !interrupt_input),
but read_socket_hook is not global anymore. Let's pretend that
it's always set. */
return !interrupt_input;
#else
return 0;
#endif
}
/* Turn off polling. */
void
stop_polling (void)
{
#ifdef POLL_FOR_INPUT
/* XXX This condition was (read_socket_hook && !interrupt_input),
but read_socket_hook is not global anymore. Let's pretend that
it's always set. */
if (!interrupt_input)
++poll_suppress_count;
#endif
}
/* Set the value of poll_suppress_count to COUNT
and start or stop polling accordingly. */
void
set_poll_suppress_count (int count)
{
#ifdef POLL_FOR_INPUT
if (count == 0 && poll_suppress_count != 0)
{
poll_suppress_count = 1;
start_polling ();
}
else if (count != 0 && poll_suppress_count == 0)
{
stop_polling ();
}
poll_suppress_count = count;
#endif
}
/* Bind polling_period to a value at least N.
But don't decrease it. */
void
bind_polling_period (int n)
{
#ifdef POLL_FOR_INPUT
EMACS_INT new = polling_period;
if (n > new)
new = n;
stop_other_atimers (poll_timer);
stop_polling ();
specbind (Qpolling_period, make_number (new));
/* Start a new alarm with the new period. */
start_polling ();
#endif
}
/* Apply the control modifier to CHARACTER. */
#if !defined (HAVE_NTGUI) && !defined (HAVE_MACGUI)
static
#endif
int
make_ctrl_char (int c)
{
/* Save the upper bits here. */
int upper = c & ~0177;
if (! ASCII_BYTE_P (c))
return c |= ctrl_modifier;
c &= 0177;
/* Everything in the columns containing the upper-case letters
denotes a control character. */
if (c >= 0100 && c < 0140)
{
int oc = c;
c &= ~0140;
/* Set the shift modifier for a control char
made from a shifted letter. But only for letters! */
if (oc >= 'A' && oc <= 'Z')
c |= shift_modifier;
}
/* The lower-case letters denote control characters too. */
else if (c >= 'a' && c <= 'z')
c &= ~0140;
/* Include the bits for control and shift
only if the basic ASCII code can't indicate them. */
else if (c >= ' ')
c |= ctrl_modifier;
/* Replace the high bits. */
c |= (upper & ~ctrl_modifier);
return c;
}
/* Display the help-echo property of the character after the mouse pointer.
Either show it in the echo area, or call show-help-function to display
it by other means (maybe in a tooltip).
If HELP is nil, that means clear the previous help echo.
If HELP is a string, display that string. If HELP is a function,
call it with OBJECT and POS as arguments; the function should
return a help string or nil for none. For all other types of HELP,
evaluate it to obtain a string.
WINDOW is the window in which the help was generated, if any.
It is nil if not in a window.
If OBJECT is a buffer, POS is the position in the buffer where the
`help-echo' text property was found.
If OBJECT is an overlay, that overlay has a `help-echo' property,
and POS is the position in the overlay's buffer under the mouse.
If OBJECT is a string (an overlay string or a string displayed with
the `display' property). POS is the position in that string under
the mouse.
Note: this function may only be called with HELP nil or a string
from X code running asynchronously. */
void
show_help_echo (Lisp_Object help, Lisp_Object window, Lisp_Object object,
Lisp_Object pos)
{
if (!NILP (help) && !STRINGP (help))
{
if (FUNCTIONP (help))
help = safe_call (4, help, window, object, pos);
else
help = safe_eval (help);
if (!STRINGP (help))
return;
}
if (!noninteractive && STRINGP (help))
{
/* The mouse-fixup-help-message Lisp function can call
mouse_position_hook, which resets the mouse_moved flags.
This causes trouble if we are trying to read a mouse motion
event (i.e., if we are inside a `track-mouse' form), so we
restore the mouse_moved flag. */
struct frame *f = NILP (do_mouse_tracking) ? NULL : some_mouse_moved ();
help = call1 (Qmouse_fixup_help_message, help);
if (f)
f->mouse_moved = 1;
}
if (STRINGP (help) || NILP (help))
{
if (!NILP (Vshow_help_function))
call1 (Vshow_help_function, help);
help_echo_showing_p = STRINGP (help);
}
}
/* Input of single characters from keyboard. */
static Lisp_Object kbd_buffer_get_event (KBOARD **kbp, bool *used_mouse_menu,
struct timespec *end_time);
static void record_char (Lisp_Object c);
static Lisp_Object help_form_saved_window_configs;
static void
read_char_help_form_unwind (void)
{
Lisp_Object window_config = XCAR (help_form_saved_window_configs);
help_form_saved_window_configs = XCDR (help_form_saved_window_configs);
if (!NILP (window_config))
Fset_window_configuration (window_config);
}
#define STOP_POLLING \
do { if (! polling_stopped_here) stop_polling (); \
polling_stopped_here = 1; } while (0)
#define RESUME_POLLING \
do { if (polling_stopped_here) start_polling (); \
polling_stopped_here = 0; } while (0)
static Lisp_Object
read_event_from_main_queue (struct timespec *end_time,
sys_jmp_buf local_getcjmp,
bool *used_mouse_menu)
{
Lisp_Object c = Qnil;
sys_jmp_buf save_jump;
KBOARD *kb IF_LINT (= NULL);
start:
/* Read from the main queue, and if that gives us something we can't use yet,
we put it on the appropriate side queue and try again. */
if (end_time && timespec_cmp (*end_time, current_timespec ()) <= 0)
return c;
/* Actually read a character, waiting if necessary. */
save_getcjmp (save_jump);
restore_getcjmp (local_getcjmp);
if (!end_time)
timer_start_idle ();
c = kbd_buffer_get_event (&kb, used_mouse_menu, end_time);
restore_getcjmp (save_jump);
if (! NILP (c) && (kb != current_kboard))
{
Lisp_Object last = KVAR (kb, kbd_queue);
if (CONSP (last))
{
while (CONSP (XCDR (last)))
last = XCDR (last);
if (!NILP (XCDR (last)))
emacs_abort ();
}
if (!CONSP (last))
kset_kbd_queue (kb, list1 (c));
else
XSETCDR (last, list1 (c));
kb->kbd_queue_has_data = 1;
c = Qnil;
if (single_kboard)
goto start;
current_kboard = kb;
/* This is going to exit from read_char
so we had better get rid of this frame's stuff. */
return make_number (-2);
}
/* Terminate Emacs in batch mode if at eof. */
if (noninteractive && INTEGERP (c) && XINT (c) < 0)
Fkill_emacs (make_number (1));
if (INTEGERP (c))
{
/* Add in any extra modifiers, where appropriate. */
if ((extra_keyboard_modifiers & CHAR_CTL)
|| ((extra_keyboard_modifiers & 0177) < ' '
&& (extra_keyboard_modifiers & 0177) != 0))
XSETINT (c, make_ctrl_char (XINT (c)));
/* Transfer any other modifier bits directly from
extra_keyboard_modifiers to c. Ignore the actual character code
in the low 16 bits of extra_keyboard_modifiers. */
XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
}
return c;
}
/* Like `read_event_from_main_queue' but applies keyboard-coding-system
to tty input. */
static Lisp_Object
read_decoded_event_from_main_queue (struct timespec *end_time,
sys_jmp_buf local_getcjmp,
Lisp_Object prev_event,
bool *used_mouse_menu)
{
#define MAX_ENCODED_BYTES 16
Lisp_Object events[MAX_ENCODED_BYTES];
int n = 0;
while (true)
{
Lisp_Object nextevt
= read_event_from_main_queue (end_time, local_getcjmp,
used_mouse_menu);
#ifdef WINDOWSNT
/* w32_console already returns decoded events. It either reads
Unicode characters from the Windows keyboard input, or
converts characters encoded in the current codepage into
Unicode. See w32inevt.c:key_event, near its end. */
return nextevt;
#else
struct frame *frame = XFRAME (selected_frame);
struct terminal *terminal = frame->terminal;
if (!((FRAME_TERMCAP_P (frame) || FRAME_MSDOS_P (frame))
/* Don't apply decoding if we're just reading a raw event
(e.g. reading bytes sent by the xterm to specify the position
of a mouse click). */
&& (!EQ (prev_event, Qt))
&& (TERMINAL_KEYBOARD_CODING (terminal)->common_flags
& CODING_REQUIRE_DECODING_MASK)))
return nextevt; /* No decoding needed. */
else
{
int meta_key = terminal->display_info.tty->meta_key;
eassert (n < MAX_ENCODED_BYTES);
events[n++] = nextevt;
if (NATNUMP (nextevt)
&& XINT (nextevt) < (meta_key == 1 ? 0x80 : 0x100))
{ /* An encoded byte sequence, let's try to decode it. */
struct coding_system *coding
= TERMINAL_KEYBOARD_CODING (terminal);
unsigned char *src = alloca (n);
int i;
for (i = 0; i < n; i++)
src[i] = XINT (events[i]);
if (meta_key != 2)
for (i = 0; i < n; i++)
src[i] &= ~0x80;
coding->destination = alloca (n * 4);
coding->dst_bytes = n * 4;
decode_coding_c_string (coding, src, n, Qnil);
eassert (coding->produced_char <= n);
if (coding->produced_char == 0)
{ /* The encoded sequence is incomplete. */
if (n < MAX_ENCODED_BYTES) /* Avoid buffer overflow. */
continue; /* Read on! */
}
else
{
const unsigned char *p = coding->destination;
eassert (coding->carryover_bytes == 0);
n = 0;
while (n < coding->produced_char)
events[n++] = make_number (STRING_CHAR_ADVANCE (p));
}
}
/* Now `events' should hold decoded events.
Normally, n should be equal to 1, but better not rely on it.
We can only return one event here, so return the first we
had and keep the others (if any) for later. */
while (n > 1)
Vunread_command_events
= Fcons (events[--n], Vunread_command_events);
return events[0];
}
#endif
}
}
/* Read a character from the keyboard; call the redisplay if needed. */
/* commandflag 0 means do not autosave, but do redisplay.
-1 means do not redisplay, but do autosave.
-2 means do neither.
1 means do both. */
/* The argument MAP is a keymap for menu prompting.
PREV_EVENT is the previous input event, or nil if we are reading
the first event of a key sequence (or not reading a key sequence).
If PREV_EVENT is t, that is a "magic" value that says
not to run input methods, but in other respects to act as if
not reading a key sequence.
If USED_MOUSE_MENU is non-null, then set *USED_MOUSE_MENU to true
if we used a mouse menu to read the input, or false otherwise. If
USED_MOUSE_MENU is null, don't dereference it.
Value is -2 when we find input on another keyboard. A second call
to read_char will read it.
If END_TIME is non-null, it is a pointer to a struct timespec
specifying the maximum time to wait until. If no input arrives by
that time, stop waiting and return nil.
Value is t if we showed a menu and the user rejected it. */
Lisp_Object
read_char (int commandflag, Lisp_Object map,
Lisp_Object prev_event,
bool *used_mouse_menu, struct timespec *end_time)
{
Lisp_Object c;
ptrdiff_t jmpcount;
sys_jmp_buf local_getcjmp;
sys_jmp_buf save_jump;
Lisp_Object tem, save;
volatile Lisp_Object previous_echo_area_message;
volatile Lisp_Object also_record;
volatile bool reread;
struct gcpro gcpro1, gcpro2;
bool volatile polling_stopped_here = 0;
struct kboard *orig_kboard = current_kboard;
also_record = Qnil;
#if 0 /* This was commented out as part of fixing echo for C-u left. */
before_command_key_count = this_command_key_count;
before_command_echo_length = echo_length ();
#endif
c = Qnil;
previous_echo_area_message = Qnil;
GCPRO2 (c, previous_echo_area_message);
retry:
reread = 0;
if (CONSP (Vunread_post_input_method_events))
{
c = XCAR (Vunread_post_input_method_events);
Vunread_post_input_method_events
= XCDR (Vunread_post_input_method_events);
/* Undo what read_char_x_menu_prompt did when it unread
additional keys returned by Fx_popup_menu. */
if (CONSP (c)
&& (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
&& NILP (XCDR (c)))
c = XCAR (c);
reread = 1;
goto reread_first;
}
if (CONSP (Vunread_command_events))
{
bool was_disabled = 0;
c = XCAR (Vunread_command_events);
Vunread_command_events = XCDR (Vunread_command_events);
reread = 1;
/* Undo what sit-for did when it unread additional keys
inside universal-argument. */
if (CONSP (c)
&& EQ (XCAR (c), Qt))
{
reread = 0;
c = XCDR (c);
}
/* Undo what read_char_x_menu_prompt did when it unread
additional keys returned by Fx_popup_menu. */
if (CONSP (c)
&& EQ (XCDR (c), Qdisabled)
&& (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c))))
{
was_disabled = 1;
c = XCAR (c);
}
/* If the queued event is something that used the mouse,
set used_mouse_menu accordingly. */
if (used_mouse_menu
/* Also check was_disabled so last-nonmenu-event won't return
a bad value when submenus are involved. (Bug#447) */
&& (EQ (c, Qtool_bar) || EQ (c, Qmenu_bar) || was_disabled))
*used_mouse_menu = 1;
goto reread_for_input_method;
}
if (CONSP (Vunread_input_method_events))
{
c = XCAR (Vunread_input_method_events);
Vunread_input_method_events = XCDR (Vunread_input_method_events);
/* Undo what read_char_x_menu_prompt did when it unread
additional keys returned by Fx_popup_menu. */
if (CONSP (c)
&& (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
&& NILP (XCDR (c)))
c = XCAR (c);
reread = 1;
goto reread_for_input_method;
}
this_command_key_count_reset = 0;
if (!NILP (Vexecuting_kbd_macro))
{
/* We set this to Qmacro; since that's not a frame, nobody will
try to switch frames on us, and the selected window will
remain unchanged.
Since this event came from a macro, it would be misleading to
leave internal_last_event_frame set to wherever the last
real event came from. Normally, a switch-frame event selects
internal_last_event_frame after each command is read, but
events read from a macro should never cause a new frame to be
selected. */
Vlast_event_frame = internal_last_event_frame = Qmacro;
/* Exit the macro if we are at the end.
Also, some things replace the macro with t
to force an early exit. */
if (EQ (Vexecuting_kbd_macro, Qt)
|| executing_kbd_macro_index >= XFASTINT (Flength (Vexecuting_kbd_macro)))
{
XSETINT (c, -1);
goto exit;
}
c = Faref (Vexecuting_kbd_macro, make_number (executing_kbd_macro_index));
if (STRINGP (Vexecuting_kbd_macro)
&& (XFASTINT (c) & 0x80) && (XFASTINT (c) <= 0xff))
XSETFASTINT (c, CHAR_META | (XFASTINT (c) & ~0x80));
executing_kbd_macro_index++;
goto from_macro;
}
if (!NILP (unread_switch_frame))
{
c = unread_switch_frame;
unread_switch_frame = Qnil;
/* This event should make it into this_command_keys, and get echoed
again, so we do not set `reread'. */
goto reread_first;
}
/* If redisplay was requested. */
if (commandflag >= 0)
{
bool echo_current = EQ (echo_message_buffer, echo_area_buffer[0]);
/* If there is pending input, process any events which are not
user-visible, such as X selection_request events. */
if (input_pending
|| detect_input_pending_run_timers (0))
swallow_events (0); /* May clear input_pending. */
/* Redisplay if no pending input. */
while (!input_pending)
{
if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
redisplay_preserve_echo_area (5);
else
redisplay ();
if (!input_pending)
/* Normal case: no input arrived during redisplay. */
break;
/* Input arrived and pre-empted redisplay.
Process any events which are not user-visible. */
swallow_events (0);
/* If that cleared input_pending, try again to redisplay. */
}
/* Prevent the redisplay we just did
from messing up echoing of the input after the prompt. */
if (commandflag == 0 && echo_current)
echo_message_buffer = echo_area_buffer[0];
}
/* Message turns off echoing unless more keystrokes turn it on again.
The code in 20.x for the condition was
1. echo_area_glyphs && *echo_area_glyphs
2. && echo_area_glyphs != current_kboard->echobuf
3. && ok_to_echo_at_next_pause != echo_area_glyphs
(1) means there's a current message displayed
(2) means it's not the message from echoing from the current
kboard.
(3) There's only one place in 20.x where ok_to_echo_at_next_pause
is set to a non-null value. This is done in read_char and it is
set to echo_area_glyphs after a call to echo_char. That means
ok_to_echo_at_next_pause is either null or
current_kboard->echobuf with the appropriate current_kboard at
that time.
So, condition (3) means in clear text ok_to_echo_at_next_pause
must be either null, or the current message isn't from echoing at
all, or it's from echoing from a different kboard than the
current one. */
if (/* There currently is something in the echo area. */
!NILP (echo_area_buffer[0])
&& (/* It's an echo from a different kboard. */
echo_kboard != current_kboard
/* Or we explicitly allow overwriting whatever there is. */
|| ok_to_echo_at_next_pause == NULL))
cancel_echoing ();
else
echo_dash ();
/* Try reading a character via menu prompting in the minibuf.
Try this before the sit-for, because the sit-for
would do the wrong thing if we are supposed to do
menu prompting. If EVENT_HAS_PARAMETERS then we are reading
after a mouse event so don't try a minibuf menu. */
c = Qnil;
if (KEYMAPP (map) && INTERACTIVE
&& !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
/* Don't bring up a menu if we already have another event. */
&& NILP (Vunread_command_events)
&& !detect_input_pending_run_timers (0))
{
c = read_char_minibuf_menu_prompt (commandflag, map);
if (INTEGERP (c) && XINT (c) == -2)
return c; /* wrong_kboard_jmpbuf */
if (! NILP (c))
goto exit;
}
/* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
We will do that below, temporarily for short sections of code,
when appropriate. local_getcjmp must be in effect
around any call to sit_for or kbd_buffer_get_event;
it *must not* be in effect when we call redisplay. */
jmpcount = SPECPDL_INDEX ();
if (sys_setjmp (local_getcjmp))
{
/* Handle quits while reading the keyboard. */
/* We must have saved the outer value of getcjmp here,
so restore it now. */
restore_getcjmp (save_jump);
unbind_to (jmpcount, Qnil);
XSETINT (c, quit_char);
internal_last_event_frame = selected_frame;
Vlast_event_frame = internal_last_event_frame;
/* If we report the quit char as an event,
don't do so more than once. */
if (!NILP (Vinhibit_quit))
Vquit_flag = Qnil;
{
KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
if (kb != current_kboard)
{
Lisp_Object last = KVAR (kb, kbd_queue);
/* We shouldn't get here if we were in single-kboard mode! */
if (single_kboard)
emacs_abort ();
if (CONSP (last))
{
while (CONSP (XCDR (last)))
last = XCDR (last);
if (!NILP (XCDR (last)))
emacs_abort ();
}
if (!CONSP (last))
kset_kbd_queue (kb, list1 (c));
else
XSETCDR (last, list1 (c));
kb->kbd_queue_has_data = 1;
current_kboard = kb;
/* This is going to exit from read_char
so we had better get rid of this frame's stuff. */
UNGCPRO;
return make_number (-2); /* wrong_kboard_jmpbuf */
}
}
goto non_reread;
}
/* Start idle timers if no time limit is supplied. We don't do it
if a time limit is supplied to avoid an infinite recursion in the
situation where an idle timer calls `sit-for'. */
if (!end_time)
timer_start_idle ();
/* If in middle of key sequence and minibuffer not active,
start echoing if enough time elapses. */
if (minibuf_level == 0
&& !end_time
&& !current_kboard->immediate_echo
&& this_command_key_count > 0
&& ! noninteractive
&& (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
&& NILP (Fzerop (Vecho_keystrokes))
&& (/* No message. */
NILP (echo_area_buffer[0])
/* Or empty message. */
|| (BUF_BEG (XBUFFER (echo_area_buffer[0]))
== BUF_Z (XBUFFER (echo_area_buffer[0])))
/* Or already echoing from same kboard. */
|| (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
/* Or not echoing before and echoing allowed. */
|| (!echo_kboard && ok_to_echo_at_next_pause)))
{
/* After a mouse event, start echoing right away.
This is because we are probably about to display a menu,
and we don't want to delay before doing so. */
if (EVENT_HAS_PARAMETERS (prev_event))
echo_now ();
else
{
Lisp_Object tem0;
save_getcjmp (save_jump);
restore_getcjmp (local_getcjmp);
tem0 = sit_for (Vecho_keystrokes, 1, 1);
restore_getcjmp (save_jump);
if (EQ (tem0, Qt)
&& ! CONSP (Vunread_command_events))
echo_now ();
}
}
/* Maybe auto save due to number of keystrokes. */
if (commandflag != 0 && commandflag != -2
&& auto_save_interval > 0
&& num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
&& !detect_input_pending_run_timers (0))
{
Fdo_auto_save (Qnil, Qnil);
/* Hooks can actually change some buffers in auto save. */
redisplay ();
}
/* Try reading using an X menu.
This is never confused with reading using the minibuf
because the recursive call of read_char in read_char_minibuf_menu_prompt
does not pass on any keymaps. */
if (KEYMAPP (map) && INTERACTIVE
&& !NILP (prev_event)
&& EVENT_HAS_PARAMETERS (prev_event)
&& !EQ (XCAR (prev_event), Qmenu_bar)
&& !EQ (XCAR (prev_event), Qtool_bar)
/* Don't bring up a menu if we already have another event. */
&& NILP (Vunread_command_events))
{
c = read_char_x_menu_prompt (map, prev_event, used_mouse_menu);
/* Now that we have read an event, Emacs is not idle. */
if (!end_time)
timer_stop_idle ();
goto exit;
}
/* Maybe autosave and/or garbage collect due to idleness. */
if (INTERACTIVE && NILP (c))
{
int delay_level;
ptrdiff_t buffer_size;
/* Slow down auto saves logarithmically in size of current buffer,
and garbage collect while we're at it. */
if (! MINI_WINDOW_P (XWINDOW (selected_window)))
last_non_minibuf_size = Z - BEG;
buffer_size = (last_non_minibuf_size >> 8) + 1;
delay_level = 0;
while (buffer_size > 64)
delay_level++, buffer_size -= buffer_size >> 2;
if (delay_level < 4) delay_level = 4;
/* delay_level is 4 for files under around 50k, 7 at 100k,
9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
/* Auto save if enough time goes by without input. */
if (commandflag != 0 && commandflag != -2
&& num_nonmacro_input_events > last_auto_save
&& INTEGERP (Vauto_save_timeout)
&& XINT (Vauto_save_timeout) > 0)
{
Lisp_Object tem0;
EMACS_INT timeout = XFASTINT (Vauto_save_timeout);
timeout = min (timeout, MOST_POSITIVE_FIXNUM / delay_level * 4);
timeout = delay_level * timeout / 4;
save_getcjmp (save_jump);
restore_getcjmp (local_getcjmp);
tem0 = sit_for (make_number (timeout), 1, 1);
restore_getcjmp (save_jump);
if (EQ (tem0, Qt)
&& ! CONSP (Vunread_command_events))
{
Fdo_auto_save (Qnil, Qnil);
redisplay ();
}
}
/* If there is still no input available, ask for GC. */
if (!detect_input_pending_run_timers (0))
maybe_gc ();
}
/* Notify the caller if an autosave hook, or a timer, sentinel or
filter in the sit_for calls above have changed the current
kboard. This could happen if they use the minibuffer or start a
recursive edit, like the fancy splash screen in server.el's
filter. If this longjmp wasn't here, read_key_sequence would
interpret the next key sequence using the wrong translation
tables and function keymaps. */
if (NILP (c) && current_kboard != orig_kboard)
{
UNGCPRO;
return make_number (-2); /* wrong_kboard_jmpbuf */
}
/* If this has become non-nil here, it has been set by a timer
or sentinel or filter. */
if (CONSP (Vunread_command_events))
{
c = XCAR (Vunread_command_events);
Vunread_command_events = XCDR (Vunread_command_events);
}
/* Read something from current KBOARD's side queue, if possible. */
if (NILP (c))
{
if (current_kboard->kbd_queue_has_data)
{
if (!CONSP (KVAR (current_kboard, kbd_queue)))
emacs_abort ();
c = XCAR (KVAR (current_kboard, kbd_queue));
kset_kbd_queue (current_kboard,
XCDR (KVAR (current_kboard, kbd_queue)));
if (NILP (KVAR (current_kboard, kbd_queue)))
current_kboard->kbd_queue_has_data = 0;
input_pending = readable_events (0);
if (EVENT_HAS_PARAMETERS (c)
&& EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
internal_last_event_frame = XCAR (XCDR (c));
Vlast_event_frame = internal_last_event_frame;
}
}
/* If current_kboard's side queue is empty check the other kboards.
If one of them has data that we have not yet seen here,
switch to it and process the data waiting for it.
Note: if the events queued up for another kboard
have already been seen here, and therefore are not a complete command,
the kbd_queue_has_data field is 0, so we skip that kboard here.
That's to avoid an infinite loop switching between kboards here. */
if (NILP (c) && !single_kboard)
{
KBOARD *kb;
for (kb = all_kboards; kb; kb = kb->next_kboard)
if (kb->kbd_queue_has_data)
{
current_kboard = kb;
/* This is going to exit from read_char
so we had better get rid of this frame's stuff. */
UNGCPRO;
return make_number (-2); /* wrong_kboard_jmpbuf */
}
}
wrong_kboard:
STOP_POLLING;
if (NILP (c))
{
c = read_decoded_event_from_main_queue (end_time, local_getcjmp,
prev_event, used_mouse_menu);
if (NILP(c) && end_time &&
timespec_cmp (*end_time, current_timespec ()) <= 0)
{
goto exit;
}
if (EQ (c, make_number (-2)))
{
/* This is going to exit from read_char
so we had better get rid of this frame's stuff. */
UNGCPRO;
return c;
}
}
non_reread:
if (!end_time)
timer_stop_idle ();
RESUME_POLLING;
if (NILP (c))
{
if (commandflag >= 0
&& !input_pending && !detect_input_pending_run_timers (0))
redisplay ();
goto wrong_kboard;
}
/* Buffer switch events are only for internal wakeups
so don't show them to the user.
Also, don't record a key if we already did. */
if (BUFFERP (c))
goto exit;
/* Process special events within read_char
and loop around to read another event. */
save = Vquit_flag;
Vquit_flag = Qnil;
tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1);
Vquit_flag = save;
if (!NILP (tem))
{
struct buffer *prev_buffer = current_buffer;
last_input_event = c;
call4 (Qcommand_execute, tem, Qnil, Fvector (1, &last_input_event), Qt);
if (CONSP (c) && EQ (XCAR (c), Qselect_window) && !end_time)
/* We stopped being idle for this event; undo that. This
prevents automatic window selection (under
mouse_autoselect_window from acting as a real input event, for
example banishing the mouse under mouse-avoidance-mode. */
timer_resume_idle ();
if (current_buffer != prev_buffer)
{
/* The command may have changed the keymaps. Pretend there
is input in another keyboard and return. This will
recalculate keymaps. */
c = make_number (-2);
goto exit;
}
else
goto retry;
}
/* Handle things that only apply to characters. */
if (INTEGERP (c))
{
/* If kbd_buffer_get_event gave us an EOF, return that. */
if (XINT (c) == -1)
goto exit;
if ((STRINGP (KVAR (current_kboard, Vkeyboard_translate_table))
&& UNSIGNED_CMP (XFASTINT (c), <,
SCHARS (KVAR (current_kboard,
Vkeyboard_translate_table))))
|| (VECTORP (KVAR (current_kboard, Vkeyboard_translate_table))
&& UNSIGNED_CMP (XFASTINT (c), <,
ASIZE (KVAR (current_kboard,
Vkeyboard_translate_table))))
|| (CHAR_TABLE_P (KVAR (current_kboard, Vkeyboard_translate_table))
&& CHARACTERP (c)))
{
Lisp_Object d;
d = Faref (KVAR (current_kboard, Vkeyboard_translate_table), c);
/* nil in keyboard-translate-table means no translation. */
if (!NILP (d))
c = d;
}
}
/* If this event is a mouse click in the menu bar,
return just menu-bar for now. Modify the mouse click event
so we won't do this twice, then queue it up. */
if (EVENT_HAS_PARAMETERS (c)
&& CONSP (XCDR (c))
&& CONSP (EVENT_START (c))
&& CONSP (XCDR (EVENT_START (c))))
{
Lisp_Object posn;
posn = POSN_POSN (EVENT_START (c));
/* Handle menu-bar events:
insert the dummy prefix event `menu-bar'. */
if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
{
/* Change menu-bar to (menu-bar) as the event "position". */
POSN_SET_POSN (EVENT_START (c), list1 (posn));
also_record = c;
Vunread_command_events = Fcons (c, Vunread_command_events);
c = posn;
}
}
/* Store these characters into recent_keys, the dribble file if any,
and the keyboard macro being defined, if any. */
record_char (c);
if (! NILP (also_record))
record_char (also_record);
/* Wipe the echo area.
But first, if we are about to use an input method,
save the echo area contents for it to refer to. */
if (INTEGERP (c)
&& ! NILP (Vinput_method_function)
&& ' ' <= XINT (c) && XINT (c) < 256 && XINT (c) != 127)
{
previous_echo_area_message = Fcurrent_message ();
Vinput_method_previous_message = previous_echo_area_message;
}
/* Now wipe the echo area, except for help events which do their
own stuff with the echo area. */
if (!CONSP (c)
|| (!(EQ (Qhelp_echo, XCAR (c)))
&& !(EQ (Qswitch_frame, XCAR (c)))
/* Don't wipe echo area for select window events: These might
get delayed via `mouse-autoselect-window' (Bug#11304). */
&& !(EQ (Qselect_window, XCAR (c)))))
{
if (!NILP (echo_area_buffer[0]))
{
safe_run_hooks (Qecho_area_clear_hook);
clear_message (1, 0);
}
}
reread_for_input_method:
from_macro:
/* Pass this to the input method, if appropriate. */
if (INTEGERP (c)
&& ! NILP (Vinput_method_function)
/* Don't run the input method within a key sequence,
after the first event of the key sequence. */
&& NILP (prev_event)
&& ' ' <= XINT (c) && XINT (c) < 256 && XINT (c) != 127)
{
Lisp_Object keys;
ptrdiff_t key_count;
bool key_count_reset;
struct gcpro gcpro1;
ptrdiff_t count = SPECPDL_INDEX ();
/* Save the echo status. */
bool saved_immediate_echo = current_kboard->immediate_echo;
struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
Lisp_Object saved_echo_string = KVAR (current_kboard, echo_string);
ptrdiff_t saved_echo_after_prompt = current_kboard->echo_after_prompt;
#if 0
if (before_command_restore_flag)
{
this_command_key_count = before_command_key_count_1;
if (this_command_key_count < this_single_command_key_start)
this_single_command_key_start = this_command_key_count;
echo_truncate (before_command_echo_length_1);
before_command_restore_flag = 0;
}
#endif
/* Save the this_command_keys status. */
key_count = this_command_key_count;
key_count_reset = this_command_key_count_reset;
if (key_count > 0)
keys = Fcopy_sequence (this_command_keys);
else
keys = Qnil;
GCPRO1 (keys);
/* Clear out this_command_keys. */
this_command_key_count = 0;
this_command_key_count_reset = 0;
/* Now wipe the echo area. */
if (!NILP (echo_area_buffer[0]))
safe_run_hooks (Qecho_area_clear_hook);
clear_message (1, 0);
echo_truncate (0);
/* If we are not reading a key sequence,
never use the echo area. */
if (!KEYMAPP (map))
{
specbind (Qinput_method_use_echo_area, Qt);
}
/* Call the input method. */
tem = call1 (Vinput_method_function, c);
tem = unbind_to (count, tem);
/* Restore the saved echoing state
and this_command_keys state. */
this_command_key_count = key_count;
this_command_key_count_reset = key_count_reset;
if (key_count > 0)
this_command_keys = keys;
cancel_echoing ();
ok_to_echo_at_next_pause = saved_ok_to_echo;
kset_echo_string (current_kboard, saved_echo_string);
current_kboard->echo_after_prompt = saved_echo_after_prompt;
if (saved_immediate_echo)
echo_now ();
UNGCPRO;
/* The input method can return no events. */
if (! CONSP (tem))
{
/* Bring back the previous message, if any. */
if (! NILP (previous_echo_area_message))
message_with_string ("%s", previous_echo_area_message, 0);
goto retry;
}
/* It returned one event or more. */
c = XCAR (tem);
Vunread_post_input_method_events
= nconc2 (XCDR (tem), Vunread_post_input_method_events);
}
reread_first:
/* Display help if not echoing. */
if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
{
/* (help-echo FRAME HELP WINDOW OBJECT POS). */
Lisp_Object help, object, position, window, htem;
htem = Fcdr (XCDR (c));
help = Fcar (htem);
htem = Fcdr (htem);
window = Fcar (htem);
htem = Fcdr (htem);
object = Fcar (htem);
htem = Fcdr (htem);
position = Fcar (htem);
show_help_echo (help, window, object, position);
/* We stopped being idle for this event; undo that. */
if (!end_time)
timer_resume_idle ();
goto retry;
}
if ((! reread || this_command_key_count == 0
|| this_command_key_count_reset)
&& !end_time)
{
/* Don't echo mouse motion events. */
if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
&& NILP (Fzerop (Vecho_keystrokes))
&& ! (EVENT_HAS_PARAMETERS (c)
&& EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
{
echo_char (c);
if (! NILP (also_record))
echo_char (also_record);
/* Once we reread a character, echoing can happen
the next time we pause to read a new one. */
ok_to_echo_at_next_pause = current_kboard;
}
/* Record this character as part of the current key. */
add_command_key (c);
if (! NILP (also_record))
add_command_key (also_record);
}
last_input_event = c;
num_input_events++;
/* Process the help character specially if enabled. */
if (!NILP (Vhelp_form) && help_char_p (c))
{
ptrdiff_t count = SPECPDL_INDEX ();
help_form_saved_window_configs
= Fcons (Fcurrent_window_configuration (Qnil),
help_form_saved_window_configs);
record_unwind_protect_void (read_char_help_form_unwind);
call0 (Qhelp_form_show);
cancel_echoing ();
do
{
c = read_char (0, Qnil, Qnil, 0, NULL);
if (EVENT_HAS_PARAMETERS (c)
&& EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_click))
XSETCAR (help_form_saved_window_configs, Qnil);
}
while (BUFFERP (c));
/* Remove the help from the frame. */
unbind_to (count, Qnil);
redisplay ();
if (EQ (c, make_number (040)))
{
cancel_echoing ();
do
c = read_char (0, Qnil, Qnil, 0, NULL);
while (BUFFERP (c));
}
}
exit:
RESUME_POLLING;
RETURN_UNGCPRO (c);
}
/* Record a key that came from a mouse menu.
Record it for echoing, for this-command-keys, and so on. */
static void
record_menu_key (Lisp_Object c)
{
/* Wipe the echo area. */
clear_message (1, 0);
record_char (c);
#if 0
before_command_key_count = this_command_key_count;
before_command_echo_length = echo_length ();
#endif
/* Don't echo mouse motion events. */
if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
&& NILP (Fzerop (Vecho_keystrokes)))
{
echo_char (c);
/* Once we reread a character, echoing can happen
the next time we pause to read a new one. */
ok_to_echo_at_next_pause = 0;
}
/* Record this character as part of the current key. */
add_command_key (c);
/* Re-reading in the middle of a command. */
last_input_event = c;
num_input_events++;
}
/* Return true if should recognize C as "the help character". */
static bool
help_char_p (Lisp_Object c)
{
Lisp_Object tail;
if (EQ (c, Vhelp_char))
return 1;
for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
if (EQ (c, XCAR (tail)))
return 1;
return 0;
}
/* Record the input event C in various ways. */
static void
record_char (Lisp_Object c)
{
int recorded = 0;
if (CONSP (c) && (EQ (XCAR (c), Qhelp_echo) || EQ (XCAR (c), Qmouse_movement)))
{
/* To avoid filling recent_keys with help-echo and mouse-movement
events, we filter out repeated help-echo events, only store the
first and last in a series of mouse-movement events, and don't
store repeated help-echo events which are only separated by
mouse-movement events. */
Lisp_Object ev1, ev2, ev3;
int ix1, ix2, ix3;
if ((ix1 = recent_keys_index - 1) < 0)
ix1 = NUM_RECENT_KEYS - 1;
ev1 = AREF (recent_keys, ix1);
if ((ix2 = ix1 - 1) < 0)
ix2 = NUM_RECENT_KEYS - 1;
ev2 = AREF (recent_keys, ix2);
if ((ix3 = ix2 - 1) < 0)
ix3 = NUM_RECENT_KEYS - 1;
ev3 = AREF (recent_keys, ix3);
if (EQ (XCAR (c), Qhelp_echo))
{
/* Don't record `help-echo' in recent_keys unless it shows some help
message, and a different help than the previously recorded
event. */
Lisp_Object help, last_help;
help = Fcar_safe (Fcdr_safe (XCDR (c)));
if (!STRINGP (help))
recorded = 1;
else if (CONSP (ev1) && EQ (XCAR (ev1), Qhelp_echo)
&& (last_help = Fcar_safe (Fcdr_safe (XCDR (ev1))), EQ (last_help, help)))
recorded = 1;
else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
&& CONSP (ev2) && EQ (XCAR (ev2), Qhelp_echo)
&& (last_help = Fcar_safe (Fcdr_safe (XCDR (ev2))), EQ (last_help, help)))
recorded = -1;
else if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
&& CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
&& CONSP (ev3) && EQ (XCAR (ev3), Qhelp_echo)
&& (last_help = Fcar_safe (Fcdr_safe (XCDR (ev3))), EQ (last_help, help)))
recorded = -2;
}
else if (EQ (XCAR (c), Qmouse_movement))
{
/* Only record one pair of `mouse-movement' on a window in recent_keys.
So additional mouse movement events replace the last element. */
Lisp_Object last_window, window;
window = Fcar_safe (Fcar_safe (XCDR (c)));
if (CONSP (ev1) && EQ (XCAR (ev1), Qmouse_movement)
&& (last_window = Fcar_safe (Fcar_safe (XCDR (ev1))), EQ (last_window, window))
&& CONSP (ev2) && EQ (XCAR (ev2), Qmouse_movement)
&& (last_window = Fcar_safe (Fcar_safe (XCDR (ev2))), EQ (last_window, window)))
{
ASET (recent_keys, ix1, c);
recorded = 1;
}
}
}
else
store_kbd_macro_char (c);
if (!recorded)
{
total_keys += total_keys < NUM_RECENT_KEYS;
ASET (recent_keys, recent_keys_index, c);
if (++recent_keys_index >= NUM_RECENT_KEYS)
recent_keys_index = 0;
}
else if (recorded < 0)
{
/* We need to remove one or two events from recent_keys.
To do this, we simply put nil at those events and move the
recent_keys_index backwards over those events. Usually,
users will never see those nil events, as they will be
overwritten by the command keys entered to see recent_keys
(e.g. C-h l). */
while (recorded++ < 0 && total_keys > 0)
{
if (total_keys < NUM_RECENT_KEYS)
total_keys--;
if (--recent_keys_index < 0)
recent_keys_index = NUM_RECENT_KEYS - 1;
ASET (recent_keys, recent_keys_index, Qnil);
}
}
num_nonmacro_input_events++;
/* Write c to the dribble file. If c is a lispy event, write
the event's symbol to the dribble file, in <brackets>. Bleaugh.
If you, dear reader, have a better idea, you've got the source. :-) */
if (dribble)
{
block_input ();
if (INTEGERP (c))
{
if (XUINT (c) < 0x100)
putc (XUINT (c), dribble);
else
fprintf (dribble, " 0x%"pI"x", XUINT (c));
}
else
{
Lisp_Object dribblee;
/* If it's a structured event, take the event header. */
dribblee = EVENT_HEAD (c);
if (SYMBOLP (dribblee))
{
putc ('<', dribble);
fwrite (SDATA (SYMBOL_NAME (dribblee)), sizeof (char),
SBYTES (SYMBOL_NAME (dribblee)),
dribble);
putc ('>', dribble);
}
}
fflush (dribble);
unblock_input ();
}
}
/* Copy out or in the info on where C-g should throw to.
This is used when running Lisp code from within get_char,
in case get_char is called recursively.
See read_process_output. */
static void
save_getcjmp (sys_jmp_buf temp)
{
memcpy (temp, getcjmp, sizeof getcjmp);
}
static void
restore_getcjmp (sys_jmp_buf temp)
{
memcpy (getcjmp, temp, sizeof getcjmp);
}
/* Low level keyboard/mouse input.
kbd_buffer_store_event places events in kbd_buffer, and
kbd_buffer_get_event retrieves them. */
/* Return true if there are any events in the queue that read-char
would return. If this returns false, a read-char would block. */
static bool
readable_events (int flags)
{
if (flags & READABLE_EVENTS_DO_TIMERS_NOW)
timer_check ();
/* If the buffer contains only FOCUS_IN_EVENT events, and
READABLE_EVENTS_FILTER_EVENTS is set, report it as empty. */
if (kbd_fetch_ptr != kbd_store_ptr)
{
if (flags & (READABLE_EVENTS_FILTER_EVENTS
#ifdef USE_TOOLKIT_SCROLL_BARS
| READABLE_EVENTS_IGNORE_SQUEEZABLES
#endif
))
{
struct input_event *event;
event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
? kbd_fetch_ptr
: kbd_buffer);
do
{
if (!(
#ifdef USE_TOOLKIT_SCROLL_BARS
(flags & READABLE_EVENTS_FILTER_EVENTS) &&
#endif
event->kind == FOCUS_IN_EVENT)
#ifdef USE_TOOLKIT_SCROLL_BARS
&& !((flags & READABLE_EVENTS_IGNORE_SQUEEZABLES)
&& event->kind == SCROLL_BAR_CLICK_EVENT
&& event->part == scroll_bar_handle
&& event->modifiers == 0)
#endif
)
return 1;
event++;
if (event == kbd_buffer + KBD_BUFFER_SIZE)
event = kbd_buffer;
}
while (event != kbd_store_ptr);
}
else
return 1;
}
if (!(flags & READABLE_EVENTS_IGNORE_SQUEEZABLES)
&& !NILP (do_mouse_tracking) && some_mouse_moved ())
return 1;
if (single_kboard)
{
if (current_kboard->kbd_queue_has_data)
return 1;
}
else
{
KBOARD *kb;
for (kb = all_kboards; kb; kb = kb->next_kboard)
if (kb->kbd_queue_has_data)
return 1;
}
return 0;
}
/* Set this for debugging, to have a way to get out */
int stop_character EXTERNALLY_VISIBLE;
static KBOARD *
event_to_kboard (struct input_event *event)
{
/* Not applicable for these special events. */
if (event->kind == SELECTION_REQUEST_EVENT
|| event->kind == SELECTION_CLEAR_EVENT)
return NULL;
else
{
Lisp_Object obj = event->frame_or_window;
/* There are some events that set this field to nil or string. */
if (WINDOWP (obj))
obj = WINDOW_FRAME (XWINDOW (obj));
/* Also ignore dead frames here. */
return ((FRAMEP (obj) && FRAME_LIVE_P (XFRAME (obj)))
? FRAME_KBOARD (XFRAME (obj)) : NULL);
}
}
#ifdef subprocesses
/* Return the number of slots occupied in kbd_buffer. */
static int
kbd_buffer_nr_stored (void)
{
return kbd_fetch_ptr == kbd_store_ptr
? 0
: (kbd_fetch_ptr < kbd_store_ptr
? kbd_store_ptr - kbd_fetch_ptr
: ((kbd_buffer + KBD_BUFFER_SIZE) - kbd_fetch_ptr
+ (kbd_store_ptr - kbd_buffer)));
}
#endif /* Store an event obtained at interrupt level into kbd_buffer, fifo */
void
kbd_buffer_store_event (register struct input_event *event)
{
kbd_buffer_store_event_hold (event, 0);
}
/* Store EVENT obtained at interrupt level into kbd_buffer, fifo.
If HOLD_QUIT is 0, just stuff EVENT into the fifo.
Else, if HOLD_QUIT.kind != NO_EVENT, discard EVENT.
Else, if EVENT is a quit event, store the quit event
in HOLD_QUIT, and return (thus ignoring further events).
This is used to postpone the processing of the quit event until all
subsequent input events have been parsed (and discarded). */
void
kbd_buffer_store_event_hold (register struct input_event *event,
struct input_event *hold_quit)
{
if (event->kind == NO_EVENT)
emacs_abort ();
if (hold_quit && hold_quit->kind != NO_EVENT)
return;
if (event->kind == ASCII_KEYSTROKE_EVENT)
{
register int c = event->code & 0377;
if (event->modifiers & ctrl_modifier)
c = make_ctrl_char (c);
c |= (event->modifiers
& (meta_modifier | alt_modifier
| hyper_modifier | super_modifier));
if (c == quit_char)
{
KBOARD *kb = FRAME_KBOARD (XFRAME (event->frame_or_window));
struct input_event *sp;
if (single_kboard && kb != current_kboard)
{
kset_kbd_queue
(kb, list2 (make_lispy_switch_frame (event->frame_or_window),
make_number (c)));
kb->kbd_queue_has_data = 1;
for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
{
if (sp == kbd_buffer + KBD_BUFFER_SIZE)
sp = kbd_buffer;
if (event_to_kboard (sp) == kb)
{
sp->kind = NO_EVENT;
sp->frame_or_window = Qnil;
sp->arg = Qnil;
}
}
return;
}
if (hold_quit)
{
*hold_quit = *event;
return;
}
/* If this results in a quit_char being returned to Emacs as
input, set Vlast_event_frame properly. If this doesn't
get returned to Emacs as an event, the next event read
will set Vlast_event_frame again, so this is safe to do. */
{
Lisp_Object focus;
focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
if (NILP (focus))
focus = event->frame_or_window;
internal_last_event_frame = focus;
Vlast_event_frame = focus;
}
handle_interrupt (0);
return;
}
if (c && c == stop_character)
{
sys_suspend ();
return;
}
}
/* Don't insert two BUFFER_SWITCH_EVENT's in a row.
Just ignore the second one. */
else if (event->kind == BUFFER_SWITCH_EVENT
&& kbd_fetch_ptr != kbd_store_ptr
&& ((kbd_store_ptr == kbd_buffer
? kbd_buffer + KBD_BUFFER_SIZE - 1
: kbd_store_ptr - 1)->kind) == BUFFER_SWITCH_EVENT)
return;
if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
kbd_store_ptr = kbd_buffer;
/* Don't let the very last slot in the buffer become full,
since that would make the two pointers equal,
and that is indistinguishable from an empty buffer.
Discard the event if it would fill the last slot. */
if (kbd_fetch_ptr - 1 != kbd_store_ptr)
{
*kbd_store_ptr = *event;
++kbd_store_ptr;
#ifdef subprocesses
if (kbd_buffer_nr_stored () > KBD_BUFFER_SIZE / 2
&& ! kbd_on_hold_p ())
{
/* Don't read keyboard input until we have processed kbd_buffer.
This happens when pasting text longer than KBD_BUFFER_SIZE/2. */
hold_keyboard_input ();
if (!noninteractive)
ignore_sigio ();
stop_polling ();
}
#endif /* subprocesses */
}
/* If we're inside while-no-input, and this event qualifies
as input, set quit-flag to cause an interrupt. */
if (!NILP (Vthrow_on_input)
&& event->kind != FOCUS_IN_EVENT
&& event->kind != HELP_EVENT
&& event->kind != DEICONIFY_EVENT)
{
Vquit_flag = Vthrow_on_input;
/* If we're inside a function that wants immediate quits,
do it now. */
if (immediate_quit && NILP (Vinhibit_quit))
{
immediate_quit = 0;
QUIT;
}
}
}
/* Put an input event back in the head of the event queue. */
void
kbd_buffer_unget_event (register struct input_event *event)
{
if (kbd_fetch_ptr == kbd_buffer)
kbd_fetch_ptr = kbd_buffer + KBD_BUFFER_SIZE;
/* Don't let the very last slot in the buffer become full, */
if (kbd_fetch_ptr - 1 != kbd_store_ptr)
{
--kbd_fetch_ptr;
*kbd_fetch_ptr = *event;
}
}
/* Generate a HELP_EVENT input_event and store it in the keyboard
buffer.
HELP is the help form.
FRAME and WINDOW are the frame and window where the help is
generated. OBJECT is the Lisp object where the help was found (a
buffer, a string, an overlay, or nil if neither from a string nor
from a buffer). POS is the position within OBJECT where the help
was found. */
void
gen_help_event (Lisp_Object help, Lisp_Object frame, Lisp_Object window,
Lisp_Object object, ptrdiff_t pos)
{
struct input_event event;
EVENT_INIT (event);
event.kind = HELP_EVENT;
event.frame_or_window = frame;
event.arg = object;
event.x = WINDOWP (window) ? window : frame;
event.y = help;
event.code = pos;
kbd_buffer_store_event (&event);
}
/* Store HELP_EVENTs for HELP on FRAME in the input queue. */
void
kbd_buffer_store_help_event (Lisp_Object frame, Lisp_Object help)
{
struct input_event event;
event.kind = HELP_EVENT;
event.frame_or_window = frame;
event.arg = Qnil;
event.x = Qnil;
event.y = help;
event.code = 0;
kbd_buffer_store_event (&event);
}
/* Discard any mouse events in the event buffer by setting them to
NO_EVENT. */
void
discard_mouse_events (void)
{
struct input_event *sp;
for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
{
if (sp == kbd_buffer + KBD_BUFFER_SIZE)
sp = kbd_buffer;
if (sp->kind == MOUSE_CLICK_EVENT
|| sp->kind == WHEEL_EVENT
|| sp->kind == HORIZ_WHEEL_EVENT
#ifdef HAVE_GPM
|| sp->kind == GPM_CLICK_EVENT
#endif
|| sp->kind == SCROLL_BAR_CLICK_EVENT)
{
sp->kind = NO_EVENT;
}
}
}
/* Return true if there are any real events waiting in the event
buffer, not counting `NO_EVENT's.
Discard NO_EVENT events at the front of the input queue, possibly
leaving the input queue empty if there are no real input events. */
bool
kbd_buffer_events_waiting (void)
{
struct input_event *sp;
for (sp = kbd_fetch_ptr;
sp != kbd_store_ptr && sp->kind == NO_EVENT;
++sp)
{
if (sp == kbd_buffer + KBD_BUFFER_SIZE)
sp = kbd_buffer;
}
kbd_fetch_ptr = sp;
return sp != kbd_store_ptr && sp->kind != NO_EVENT;
}
/* Clear input event EVENT. */
static void
clear_event (struct input_event *event)
{
event->kind = NO_EVENT;
}
/* Read one event from the event buffer, waiting if necessary.
The value is a Lisp object representing the event.
The value is nil for an event that should be ignored,
or that was handled here.
We always read and discard one event. */
static Lisp_Object
kbd_buffer_get_event (KBOARD **kbp,
bool *used_mouse_menu,
struct timespec *end_time)
{
Lisp_Object obj;
#ifdef subprocesses
if (kbd_on_hold_p () && kbd_buffer_nr_stored () < KBD_BUFFER_SIZE/4)
{
/* Start reading input again because we have processed enough to
be able to accept new events again. */
unhold_keyboard_input ();
start_polling ();
}
#endif /* subprocesses */
#if !defined HAVE_DBUS && !defined USE_FILE_NOTIFY
if (noninteractive
/* In case we are running as a daemon, only do this before
detaching from the terminal. */
|| (IS_DAEMON && daemon_pipe[1] >= 0))
{
int c = getchar ();
XSETINT (obj, c);
*kbp = current_kboard;
return obj;
}
#endif /* !defined HAVE_DBUS && !defined USE_FILE_NOTIFY */
/* Wait until there is input available. */
for (;;)
{
/* Break loop if there's an unread command event. Needed in
moused window autoselection which uses a timer to insert such
events. */
if (CONSP (Vunread_command_events))
break;
if (kbd_fetch_ptr != kbd_store_ptr)
break;
if (!NILP (do_mouse_tracking) && some_mouse_moved ())
break;
/* If the quit flag is set, then read_char will return
quit_char, so that counts as "available input." */
if (!NILP (Vquit_flag))
quit_throw_to_read_char (0);
/* One way or another, wait until input is available; then, if
interrupt handlers have not read it, read it now. */
#ifdef USABLE_SIGIO
gobble_input ();
#endif
if (kbd_fetch_ptr != kbd_store_ptr)
break;
if (!NILP (do_mouse_tracking) && some_mouse_moved ())
break;
if (end_time)
{
struct timespec now = current_timespec ();
if (timespec_cmp (*end_time, now) <= 0)
return Qnil; /* Finished waiting. */
else
{
struct timespec duration = timespec_sub (*end_time, now);
wait_reading_process_output (min (duration.tv_sec,
WAIT_READING_MAX),
duration.tv_nsec,
-1, 1, Qnil, NULL, 0);
}
}
else
{
bool do_display = true;
if (FRAME_TERMCAP_P (SELECTED_FRAME ()))
{
struct tty_display_info *tty = CURTTY ();
/* When this TTY is displaying a menu, we must prevent
any redisplay, because we modify the frame's glyph
matrix behind the back of the display engine. */
if (tty->showing_menu)
do_display = false;
}
wait_reading_process_output (0, 0, -1, do_display, Qnil, NULL, 0);
}
if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
gobble_input ();
}
if (CONSP (Vunread_command_events))
{
Lisp_Object first;
first = XCAR (Vunread_command_events);
Vunread_command_events = XCDR (Vunread_command_events);
*kbp = current_kboard;
return first;
}
/* At this point, we know that there is a readable event available
somewhere. If the event queue is empty, then there must be a
mouse movement enabled and available. */
if (kbd_fetch_ptr != kbd_store_ptr)
{
struct input_event *event;
event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
? kbd_fetch_ptr
: kbd_buffer);
*kbp = event_to_kboard (event);
if (*kbp == 0)
*kbp = current_kboard; /* Better than returning null ptr? */
obj = Qnil;
/* These two kinds of events get special handling
and don't actually appear to the command loop.
We return nil for them. */
if (event->kind == SELECTION_REQUEST_EVENT
|| event->kind == SELECTION_CLEAR_EVENT)
{
#ifdef HAVE_X11
struct input_event copy;
/* Remove it from the buffer before processing it,
since otherwise swallow_events will see it
and process it again. */
copy = *event;
kbd_fetch_ptr = event + 1;
input_pending = readable_events (0);
x_handle_selection_event (&copy);
#else
/* We're getting selection request events, but we don't have
a window system. */
emacs_abort ();
#endif
}
#if defined (HAVE_NS)
else if (event->kind == NS_TEXT_EVENT)
{
if (event->code == KEY_NS_PUT_WORKING_TEXT)
obj = list1 (intern ("ns-put-working-text"));
else
obj = list1 (intern ("ns-unput-working-text"));
kbd_fetch_ptr = event + 1;
if (used_mouse_menu)
*used_mouse_menu = 1;
}
#endif
#if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (HAVE_MACGUI) \
|| defined (HAVE_NS)
else if (event->kind == DELETE_WINDOW_EVENT)
{
/* Make an event (delete-frame (FRAME)). */
obj = list2 (Qdelete_frame, list1 (event->frame_or_window));
kbd_fetch_ptr = event + 1;
}
#endif
#if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (HAVE_MACGUI) \
|| defined (HAVE_NS)
else if (event->kind == ICONIFY_EVENT)
{
/* Make an event (iconify-frame (FRAME)). */
obj = list2 (Qiconify_frame, list1 (event->frame_or_window));
kbd_fetch_ptr = event + 1;
}
else if (event->kind == DEICONIFY_EVENT)
{
/* Make an event (make-frame-visible (FRAME)). */
obj = list2 (Qmake_frame_visible, list1 (event->frame_or_window));
kbd_fetch_ptr = event + 1;
}
#endif
else if (event->kind == BUFFER_SWITCH_EVENT)
{
/* The value doesn't matter here; only the type is tested. */
XSETBUFFER (obj, current_buffer);
kbd_fetch_ptr = event + 1;
}
#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (HAVE_MACGUI) \
|| defined (HAVE_NS) || defined (USE_GTK)
else if (event->kind == MENU_BAR_ACTIVATE_EVENT)
{
kbd_fetch_ptr = event + 1;
input_pending = readable_events (0);
if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
x_activate_menubar (XFRAME (event->frame_or_window));
}
#endif
#ifdef HAVE_NTGUI
else if (event->kind == LANGUAGE_CHANGE_EVENT)
{
/* Make an event (language-change FRAME CODEPAGE LANGUAGE-ID). */
obj = list4 (Qlanguage_change,
event->frame_or_window,
make_number (event->code),
make_number (event->modifiers));
kbd_fetch_ptr = event + 1;
}
#endif
#ifdef USE_FILE_NOTIFY
else if (event->kind == FILE_NOTIFY_EVENT)
{
#ifdef HAVE_W32NOTIFY
/* Make an event (file-notify (DESCRIPTOR ACTION FILE) CALLBACK). */
obj = list3 (Qfile_notify,
list3 (make_number (event->code),
XCAR (event->arg),
XCDR (event->arg)),
event->frame_or_window);
#else
obj = make_lispy_event (event);
#endif
kbd_fetch_ptr = event + 1;
}
#endif /* USE_FILE_NOTIFY */
else if (event->kind == SAVE_SESSION_EVENT)
{
obj = list2 (Qsave_session, event->arg);
kbd_fetch_ptr = event + 1;
}
/* Just discard these, by returning nil.
With MULTI_KBOARD, these events are used as placeholders
when we need to randomly delete events from the queue.
(They shouldn't otherwise be found in the buffer,
but on some machines it appears they do show up
even without MULTI_KBOARD.) */
/* On Windows NT/9X, NO_EVENT is used to delete extraneous
mouse events during a popup-menu call. */
else if (event->kind == NO_EVENT)
kbd_fetch_ptr = event + 1;
else if (event->kind == HELP_EVENT)
{
Lisp_Object object, position, help, frame, window;
frame = event->frame_or_window;
object = event->arg;
position = make_number (event->code);
window = event->x;
help = event->y;
clear_event (event);
kbd_fetch_ptr = event + 1;
if (!WINDOWP (window))
window = Qnil;
obj = Fcons (Qhelp_echo,
list5 (frame, help, window, object, position));
}
else if (event->kind == FOCUS_IN_EVENT)
{
/* Notification of a FocusIn event. The frame receiving the
focus is in event->frame_or_window. Generate a
switch-frame event if necessary. */
Lisp_Object frame, focus;
frame = event->frame_or_window;
focus = FRAME_FOCUS_FRAME (XFRAME (frame));
if (FRAMEP (focus))
frame = focus;
if (
#ifdef HAVE_X11
! NILP (event->arg)
&&
#endif
!EQ (frame, internal_last_event_frame)
&& !EQ (frame, selected_frame))
obj = make_lispy_switch_frame (frame);
else
obj = make_lispy_focus_in (frame);
internal_last_event_frame = frame;
kbd_fetch_ptr = event + 1;
}
else if (event->kind == FOCUS_OUT_EVENT)
{
#ifdef HAVE_WINDOW_SYSTEM
Display_Info *di;
Lisp_Object frame = event->frame_or_window;
bool focused = false;
for (di = x_display_list; di && ! focused; di = di->next)
focused = di->x_highlight_frame != 0;
if (!focused)
obj = make_lispy_focus_out (frame);
#endif /* HAVE_WINDOW_SYSTEM */
kbd_fetch_ptr = event + 1;
}
#ifdef HAVE_DBUS
else if (event->kind == DBUS_EVENT)
{
obj = make_lispy_event (event);
kbd_fetch_ptr = event + 1;
}
#endif
else if (event->kind == CONFIG_CHANGED_EVENT)
{
obj = make_lispy_event (event);
kbd_fetch_ptr = event + 1;
}
else
{
/* If this event is on a different frame, return a switch-frame this
time, and leave the event in the queue for next time. */
Lisp_Object frame;
Lisp_Object focus;
frame = event->frame_or_window;
if (CONSP (frame))
frame = XCAR (frame);
else if (WINDOWP (frame))
frame = WINDOW_FRAME (XWINDOW (frame));
focus = FRAME_FOCUS_FRAME (XFRAME (frame));
if (! NILP (focus))
frame = focus;
if (! EQ (frame, internal_last_event_frame)
&& !EQ (frame, selected_frame))
obj = make_lispy_switch_frame (frame);
internal_last_event_frame = frame;
/* If we didn't decide to make a switch-frame event, go ahead
and build a real event from the queue entry. */
if (NILP (obj))
{
obj = make_lispy_event (event);
#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (HAVE_MACGUI) \
|| defined(HAVE_NS) || defined (USE_GTK)
/* If this was a menu selection, then set the flag to inhibit
writing to last_nonmenu_event. Don't do this if the event
we're returning is (menu-bar), though; that indicates the
beginning of the menu sequence, and we might as well leave
that as the `event with parameters' for this selection. */
if (used_mouse_menu
&& !EQ (event->frame_or_window, event->arg)
&& (event->kind == MENU_BAR_EVENT
|| event->kind == TOOL_BAR_EVENT))