From: Paul Eggert Date: Sun, 10 Jun 2018 01:59:25 +0000 (-0700) Subject: Simplify read_key_sequence X-Git-Tag: emacs-27.0.90~4891 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=efa750e68466fb1bab03028701350a745e0504b6;p=emacs.git Simplify read_key_sequence * src/keyboard.c (READ_KEY_ELTS): New constant. (keyremap_step, read_key_sequence): Omit BUFSIZE arg, since it's always READ_KEY_ELTS. All callers changed. (grow_bool_vector): Remove; no longer needed. (read_key_sequence): Use a bool array instead of a Lisp bool vector, since it's small and this puts less pressure on the GC. --- diff --git a/src/keyboard.c b/src/keyboard.c index c92684d3a3e..e6b5cf79983 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -1251,7 +1251,8 @@ some_mouse_moved (void) /* This is the actual command reading loop, sans error-handling encapsulation. */ -static int read_key_sequence (Lisp_Object *, int, Lisp_Object, +enum { READ_KEY_ELTS = 30 }; +static int read_key_sequence (Lisp_Object *, Lisp_Object, bool, bool, bool, bool); static void adjust_point_for_property (ptrdiff_t, bool); @@ -1299,11 +1300,9 @@ command_loop_1 (void) if (!CONSP (last_command_event)) kset_last_repeatable_command (current_kboard, Vreal_this_command); - while (1) + while (true) { Lisp_Object cmd; - Lisp_Object keybuf[30]; - int i; if (! FRAME_LIVE_P (XFRAME (selected_frame))) Fkill_emacs (Qnil); @@ -1367,8 +1366,8 @@ command_loop_1 (void) /* Read next key sequence; i gets its length. */ raw_keybuf_count = 0; - i = read_key_sequence (keybuf, ARRAYELTS (keybuf), - Qnil, 0, 1, 1, 0); + Lisp_Object keybuf[READ_KEY_ELTS]; + int i = read_key_sequence (keybuf, Qnil, false, true, true, false); /* A filter may have run while we were reading the input. */ if (! FRAME_LIVE_P (XFRAME (selected_frame))) @@ -1604,16 +1603,14 @@ command_loop_1 (void) 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, ARRAYELTS (keybuf), - Qnil, 0, 1, 1, 1); + Lisp_Object keybuf[READ_KEY_ELTS]; + int i = read_key_sequence (keybuf, Qnil, false, true, true, true); unbind_to (count, Qnil); @@ -8778,8 +8775,7 @@ access_keymap_keyremap (Lisp_Object map, Lisp_Object key, Lisp_Object prompt, /* Do one step of the key remapping used for function-key-map and key-translation-map: - KEYBUF is the buffer holding the input events. - BUFSIZE is its maximum size. + KEYBUF is the READ_KEY_ELTS-size buffer holding the input events. FKEY is a pointer to the keyremap structure to use. INPUT is the index of the last element in KEYBUF. DOIT if true says that the remapping can actually take place. @@ -8789,7 +8785,7 @@ access_keymap_keyremap (Lisp_Object map, Lisp_Object key, Lisp_Object prompt, Return true if the remapping actually took place. */ static bool -keyremap_step (Lisp_Object *keybuf, int bufsize, volatile keyremap *fkey, +keyremap_step (Lisp_Object *keybuf, volatile keyremap *fkey, int input, bool doit, int *diff, Lisp_Object prompt) { Lisp_Object next, key; @@ -8811,7 +8807,7 @@ keyremap_step (Lisp_Object *keybuf, int bufsize, volatile keyremap *fkey, *diff = len - (fkey->end - fkey->start); - if (bufsize - input <= *diff) + if (READ_KEY_ELTS - input <= *diff) error ("Key sequence too long"); /* Shift the keys that follow fkey->end. */ @@ -8858,33 +8854,8 @@ void init_raw_keybuf_count (void) raw_keybuf_count = 0; } -/* Grow a vector to fit. - - On entry, *VECTOR is nil or a bool vector. Upon return, *VECTOR - contains a bool vector of size at least NEEDED_LENGTH, with any new - values to false. Return the new value of *VECTOR. */ -static Lisp_Object -grow_bool_vector (Lisp_Object *vector, int needed_length) -{ - EMACS_INT old_length = NILP (*vector) ? 0 : bool_vector_size (*vector); - if (NILP (*vector) || old_length < needed_length) - { - EMACS_INT new_length = old_length ? old_length : 64; - while (new_length < needed_length) - new_length *= 2; - Lisp_Object new_vector = - Fmake_bool_vector (make_number (needed_length), Qnil); - if (old_length) - memcpy (bool_vector_data (new_vector), - bool_vector_data (*vector), - bool_vector_bytes (old_length)); - *vector = new_vector; - } - return *vector; -} - /* Read a sequence of keys that ends with a non prefix character, - storing it in KEYBUF, a buffer of size BUFSIZE. + storing it in KEYBUF, a buffer of size READ_KEY_ELTS. Prompt with PROMPT. Return the length of the key sequence stored. Return -1 if the user rejected a command menu. @@ -8924,7 +8895,7 @@ grow_bool_vector (Lisp_Object *vector, int needed_length) from the selected window's buffer. */ static int -read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, +read_key_sequence (Lisp_Object *keybuf, Lisp_Object prompt, bool dont_downcase_last, bool can_return_switch_frame, bool fix_current_buffer, bool prevent_redisplay) { @@ -8959,10 +8930,8 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, reading characters from the keyboard. */ int mock_input = 0; - /* This bool vector remembers whether each event in the mocked input - came from a mouse menu. We ordinarily leave it nil and create it - the first time we remember a mouse event. */ - Lisp_Object used_mouse_menu_history = Qnil; + /* Whether each event in the mocked input came from a mouse menu. */ + bool used_mouse_menu_history[READ_KEY_ELTS] = {0}; /* Distinguish first time through from replay with mock_input == 0. */ bool is_replay = false; @@ -9057,7 +9026,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, replay_sequence: starting_buffer = current_buffer; - first_unbound = bufsize + 1; + first_unbound = READ_KEY_ELTS + 1; Lisp_Object first_event = mock_input > 0 ? keybuf[0] : Qnil; Lisp_Object second_event = mock_input > 1 ? keybuf[1] : Qnil; @@ -9139,7 +9108,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, goto replay_sequence; } - if (t >= bufsize) + if (t >= READ_KEY_ELTS) error ("Key sequence too long"); if (INTERACTIVE) @@ -9170,9 +9139,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, current_kboard->immediate_echo = false; echo_now (); } - used_mouse_menu = !NILP (used_mouse_menu_history) && - t < bool_vector_size (used_mouse_menu_history) && - bool_vector_bitref (used_mouse_menu_history, t); + used_mouse_menu = used_mouse_menu_history[t]; } /* If not, we should actually read a character. */ @@ -9186,10 +9153,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, key = read_char (prevent_redisplay ? -2 : NILP (prompt), current_binding, last_nonmenu_event, &used_mouse_menu, NULL); - if (used_mouse_menu || !NILP (used_mouse_menu_history)) - bool_vector_set ( - grow_bool_vector (&used_mouse_menu_history, t + 1), - t, used_mouse_menu); + used_mouse_menu_history[t] = used_mouse_menu; if ((INTEGERP (key) && XINT (key) == -2) /* wrong_kboard_jmpbuf */ /* When switching to a new tty (with a new keyboard), read_char returns the new buffer, rather than -2 @@ -9417,7 +9381,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, && (NILP (fake_prefixed_keys) || NILP (Fmemq (key, fake_prefixed_keys)))) { - if (bufsize - t <= 1) + if (READ_KEY_ELTS - t <= 1) error ("Key sequence too long"); keybuf[t] = posn; @@ -9443,7 +9407,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, insert the dummy prefix event `menu-bar'. */ if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar)) { - if (bufsize - t <= 1) + if (READ_KEY_ELTS - t <= 1) error ("Key sequence too long"); keybuf[t] = posn; keybuf[t + 1] = key; @@ -9646,8 +9610,8 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, bool done; int diff; - done = keyremap_step (keybuf, bufsize, &indec, max (t, mock_input), - 1, &diff, prompt); + done = keyremap_step (keybuf, &indec, max (t, mock_input), + true, &diff, prompt); if (done) { mock_input = diff + max (t, mock_input); @@ -9677,13 +9641,13 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, bool done; int diff; - done = keyremap_step (keybuf, bufsize, &fkey, + done = keyremap_step (keybuf, &fkey, max (t, mock_input), /* If there's a binding (i.e. first_binding >= nmaps) we don't want to apply this function-key-mapping. */ - fkey.end + 1 == t - && (test_undefined (current_binding)), + (fkey.end + 1 == t + && test_undefined (current_binding)), &diff, prompt); if (done) { @@ -9703,8 +9667,8 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt, bool done; int diff; - done = keyremap_step (keybuf, bufsize, &keytran, max (t, mock_input), - 1, &diff, prompt); + done = keyremap_step (keybuf, &keytran, max (t, mock_input), + true, &diff, prompt); if (done) { mock_input = diff + max (t, mock_input); @@ -9857,8 +9821,6 @@ read_key_sequence_vs (Lisp_Object prompt, Lisp_Object continue_echo, Lisp_Object can_return_switch_frame, Lisp_Object cmd_loop, bool allow_string) { - Lisp_Object keybuf[30]; - int i; ptrdiff_t count = SPECPDL_INDEX (); if (!NILP (prompt)) @@ -9882,9 +9844,9 @@ read_key_sequence_vs (Lisp_Object prompt, Lisp_Object continue_echo, #endif raw_keybuf_count = 0; - i = read_key_sequence (keybuf, ARRAYELTS (keybuf), - prompt, ! NILP (dont_downcase_last), - ! NILP (can_return_switch_frame), 0, 0); + Lisp_Object keybuf[READ_KEY_ELTS]; + int i = read_key_sequence (keybuf, prompt, ! NILP (dont_downcase_last), + ! NILP (can_return_switch_frame), false, false); #if 0 /* The following is fine for code reading a key sequence and then proceeding with a lengthy computation, but it's not good