robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
6f5fae3f8c
master
1#include <stdbool.h> 2#include <string.h> 3 4#include "nvim/ascii_defs.h" 5#include "nvim/autocmd.h" 6#include "nvim/autocmd_defs.h" 7#include "nvim/buffer_defs.h" 8#include "nvim/drawscreen.h" 9#include "nvim/eval.h" 10#include "nvim/eval/typval.h" 11#include "nvim/eval/typval_defs.h" 12#include "nvim/event/defs.h" 13#include "nvim/event/loop.h" 14#include "nvim/event/multiqueue.h" 15#include "nvim/ex_getln.h" 16#include "nvim/globals.h" 17#include "nvim/input.h" 18#include "nvim/insexpand.h" 19#include "nvim/keycodes.h" 20#include "nvim/log.h" 21#include "nvim/macros_defs.h" 22#include "nvim/main.h" 23#include "nvim/memory.h" 24#include "nvim/option.h" 25#include "nvim/option_vars.h" 26#include "nvim/os/input.h" 27#include "nvim/state.h" 28#include "nvim/strings.h" 29#include "nvim/types_defs.h" 30#include "nvim/ui.h" 31 32#include "state.c.generated.h" 33 34void state_enter (VimState * s ) 35FUNC_ATTR_NONNULL_ALL 36{ 37while (true) { 38int check_result = s -> check ?s -> check (s ) :1 ; 39 40if (!check_result ) { 41break ;// Terminate this state. 42 }else if (check_result == -1 ) { 43continue ;// check() again. 44 } 45// Execute this state. 46 47int key ; 48 49getkey : 50// Apply mappings first by calling vpeekc() directly. 51// - If vpeekc() returns non-NUL, there is a character already available for processing, so 52// don't block for events. vgetc() may still block, in case of an incomplete UTF-8 sequence. 53// - If vpeekc() returns NUL, vgetc() will block, and there are three cases: 54// - There is no input available. 55// - All of available input maps to an empty string. 56// - There is an incomplete mapping. 57// A blocking wait for a character should only be done in the third case, which is the only 58// case of the three where typebuf.tb_len > 0 after vpeekc() returns NUL. 59if (vpeekc ()!= NUL || typebuf .tb_len > 0 ) { 60key = safe_vgetc (); 61 }else if (!multiqueue_empty (main_loop .events )) { 62// No input available and processing events may take time, flush now. 63ui_flush (); 64// Event was made available after the last multiqueue_process_events call 65key = K_EVENT ; 66 }else { 67// Ensure the screen is fully updated before blocking for input. Because of the duality of 68// redraw_later, this can't be done in command-line or when waiting for "Press ENTER". 69// In many of those cases the redraw is expected AFTER the key press, while normally it should 70// update the screen immediately. 71if (must_redraw != 0 && !need_wait_return && (State & MODE_CMDLINE )== 0 ) { 72update_screen (); 73setcursor ();// put cursor back where it belongs 74 } 75// Flush screen updates before blocking. 76ui_flush (); 77// Call `input_get` directly to block for events or user input without consuming anything from 78// `os/input.c:input_buffer` or calling the mapping engine. 79input_get (NULL ,0 ,-1 ,typebuf .tb_change_cnt ,main_loop .events ); 80// If an event was put into the queue, we send K_EVENT directly. 81if (!input_available ()&& !multiqueue_empty (main_loop .events )) { 82key = K_EVENT ; 83 }else { 84 gotogetkey ; 85 } 86 } 87 88if (key == K_EVENT ) { 89// An event handler may use the value of reg_executing. 90// Clear it if it should be cleared when getting the next character. 91check_end_reg_executing (true); 92may_sync_undo (); 93 } 94 95#ifdef NVIM_LOG_DEBUG 96char * keyname = key == K_EVENT ?"K_EVENT" :get_special_key_name (key ,mod_mask ); 97DLOG ("input: %s" ,keyname ); 98#endif 99 100int execute_result = s -> execute (s ,key ); 101if (!execute_result ) { 102break ; 103 }else if (execute_result == -1 ) { 104 gotogetkey ; 105 } 106 } 107} 108 109/// process events on main_loop, but interrupt if input is available 110/// 111/// This should be used to handle K_EVENT in states accepting input 112/// otherwise bursts of events can block break checking indefinitely. 113void state_handle_k_event (void ) 114{ 115while (true) { 116Event event = multiqueue_get (main_loop .events ); 117if (event .handler ) { 118event .handler (event .argv ); 119 } 120 121if (multiqueue_empty (main_loop .events )) { 122// don't breakcheck before return, caller should return to main-loop 123// and handle input already. 124return ; 125 } 126 127// TODO(bfredl): as an further micro-optimization, we could check whether 128// event.handler already checked input. 129os_breakcheck (); 130if (input_available ()|| got_int ) { 131return ; 132 } 133 } 134} 135 136/// Return true if in the current mode we need to use virtual. 137bool virtual_active (win_T * wp ) 138{ 139// In Terminal mode the cursor can be positioned anywhere by the application 140if (State & MODE_TERMINAL ) { 141return true; 142 } 143 144unsigned cur_ve_flags = get_ve_flags (wp ); 145 146if (cur_ve_flags == kOptVeFlagAll 147|| ((cur_ve_flags & kOptVeFlagInsert )&& (State & MODE_INSERT ))) { 148return true; 149 } 150 151// While an operator is being executed we return "virtual_op", because 152// Visual.active has already been reset, thus we can't check for "block" 153// being used. 154if (virtual_op != kNone ) { 155return virtual_op ; 156 } 157return (cur_ve_flags & kOptVeFlagBlock )&& Visual .active && Visual .mode == Ctrl_V ; 158} 159 160/// MODE_VISUAL, MODE_SELECT and MODE_OP_PENDING State are never set, they are 161/// equal to MODE_NORMAL State with a condition. This function returns the real 162/// State. 163int get_real_state (void ) 164{ 165if (State & MODE_NORMAL ) { 166if (Visual .active ) { 167if (Visual .select ) { 168return MODE_SELECT ; 169 } 170return MODE_VISUAL ; 171 }else if (finish_op ) { 172return MODE_OP_PENDING ; 173 } 174 } 175return State ; 176} 177 178/// Returns the current mode as a string in "buf[MODE_MAX_LENGTH]", NUL 179/// terminated. 180/// The first character represents the major mode, the following ones the minor 181/// ones. 182void get_mode (char * buf ) 183FUNC_ATTR_NONNULL_ALL 184{ 185int i = 0 ; 186 187if (State == MODE_HITRETURN || State == MODE_ASKMORE || State == MODE_SETWSIZE 188|| ((State & MODE_CMDLINE )&& get_cmdline_info ()-> one_key )) { 189buf [i ++ ]= 'r' ; 190if (State == MODE_ASKMORE ) { 191buf [i ++ ]= 'm' ; 192 }else if (State & MODE_CMDLINE ) { 193buf [i ++ ]= '?' ; 194 } 195 }else if (State == MODE_EXTERNCMD ) { 196buf [i ++ ]= '!' ; 197 }else if (State & MODE_INSERT ) { 198if (State & VREPLACE_FLAG ) { 199buf [i ++ ]= 'R' ; 200buf [i ++ ]= 'v' ; 201 }else { 202if (State & REPLACE_FLAG ) { 203buf [i ++ ]= 'R' ; 204 }else { 205buf [i ++ ]= 'i' ; 206 } 207 } 208if (ins_compl_active ()) { 209buf [i ++ ]= 'c' ; 210 }else if (ctrl_x_mode_not_defined_yet ()) { 211buf [i ++ ]= 'x' ; 212 } 213 }else if ((State & MODE_CMDLINE )|| exmode_active ) { 214buf [i ++ ]= 'c' ; 215if (exmode_active ) { 216buf [i ++ ]= 'v' ; 217 } 218if ((State & MODE_CMDLINE )&& cmdline_overstrike ()) { 219buf [i ++ ]= 'r' ; 220 } 221 }else if (State & MODE_TERMINAL ) { 222buf [i ++ ]= 't' ; 223 }else if (Visual .active ) { 224if (Visual .select ) { 225buf [i ++ ]= (char )(Visual .mode + 's' - 'v' ); 226 }else { 227buf [i ++ ]= (char )Visual .mode ; 228if (Visual .restart_select ) { 229buf [i ++ ]= 's' ; 230 } 231 } 232 }else { 233buf [i ++ ]= 'n' ; 234if (finish_op ) { 235buf [i ++ ]= 'o' ; 236// to be able to detect force-linewise/blockwise/charwise operations 237buf [i ++ ]= (char )motion_force ; 238 }else if (curbuf -> terminal ) { 239buf [i ++ ]= 't' ; 240if (restart_edit == 'I' ) { 241buf [i ++ ]= 'T' ; 242 } 243 }else if (restart_edit == 'I' || restart_edit == 'R' || restart_edit == 'V' ) { 244buf [i ++ ]= 'i' ; 245buf [i ++ ]= (char )restart_edit ; 246 } 247 } 248 249buf [i ]= NUL ; 250} 251 252/// Fires a ModeChanged autocmd if appropriate. 253void may_trigger_modechanged (void ) 254{ 255// Skip this when got_int is set, the autocommand will not be executed. 256// Better trigger it next time. 257if (!has_event (EVENT_MODECHANGED )|| got_int ) { 258return ; 259 } 260 261char curr_mode [MODE_MAX_LENGTH ]; 262char pattern_buf [2 * MODE_MAX_LENGTH ]; 263 264get_mode (curr_mode ); 265if (strcmp (curr_mode ,last_mode )== 0 ) { 266return ; 267 } 268 269save_v_event_T save_v_event ; 270dict_T * v_event = get_v_event (& save_v_event ); 271tv_dict_add_str (v_event ,S_LEN ("new_mode" ),curr_mode ); 272tv_dict_add_str (v_event ,S_LEN ("old_mode" ),last_mode ); 273tv_dict_set_keys_readonly (v_event ); 274 275// concatenate modes in format "old_mode:new_mode" 276vim_snprintf (pattern_buf ,sizeof (pattern_buf ),"%s:%s" ,last_mode ,curr_mode ); 277 278apply_autocmds (EVENT_MODECHANGED ,pattern_buf ,NULL , false,curbuf ); 279STRCPY (last_mode ,curr_mode ); 280 281restore_v_event (v_event ,& save_v_event ); 282} 283 284/// When true in a safe state when starting to wait for a character. 285static bool was_safe = false; 286 287/// Return whether currently it is safe, assuming it was safe before (high level 288/// state didn't change). 289static bool is_safe_now (void ) 290{ 291return stuff_empty () 292&& typebuf .tb_len == 0 293&& !using_script () 294&& !global_busy 295&& !debug_mode ; 296} 297 298/// Trigger SafeState if currently in a safe state, that is "safe" is true and 299/// there is no typeahead. 300void may_trigger_safestate (bool safe ) 301{ 302bool is_safe = safe && is_safe_now (); 303 304if (was_safe != is_safe ) { 305// Only log when the state changes, otherwise it happens at nearly 306// every key stroke. 307DLOG (is_safe ?"SafeState: Start triggering" :"SafeState: Stop triggering" ); 308 } 309if (is_safe ) { 310apply_autocmds (EVENT_SAFESTATE ,NULL ,NULL , false,curbuf ); 311 } 312was_safe = is_safe ; 313} 314 315/// Something changed which causes the state possibly to be unsafe, e.g. a 316/// character was typed. It will remain unsafe until the next call to 317/// may_trigger_safestate(). 318void state_no_longer_safe (const char * reason ) 319{ 320if (was_safe && reason != NULL ) { 321DLOG ("SafeState reset: %s" ,reason ); 322 } 323was_safe = false; 324} 325 326bool get_was_safe_state (void ) 327{ 328return was_safe ; 329}