diff options
author | Bram Moolenaar <Bram@vim.org> | 2019-09-15 23:02:04 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2019-09-15 23:02:04 +0200 |
commit | 8aeec40207b5adcd3a155277dc4f29189343b963 (patch) | |
tree | 4c62d41b6474eaddb0d2ad4c088bb6931b39b9ce /src/main.c | |
parent | ea8dcf8346f488786023fd03ec1c013cda243040 (diff) | |
download | vim-git-8aeec40207b5adcd3a155277dc4f29189343b963.tar.gz |
patch 8.1.2044: no easy way to process postponed workv8.1.2044
Problem: No easy way to process postponed work. (Paul Jolly)
Solution: Add the SafeState autocommand event.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c index d1d9e32e4..94bb84680 100644 --- a/src/main.c +++ b/src/main.c @@ -1028,6 +1028,64 @@ is_not_a_term() return params.not_a_term; } + +static int was_safe = FALSE; +static int not_safe_now = 0; + +/* + * Trigger SafeState if currently in a safe state for main_loop(). + */ + static void +may_trigger_safestate_main(oparg_T *oap) +{ + may_trigger_safestate( + !finish_op + && oap->prev_opcount > 0 + && oap->prev_count0 == 0 + && oap->op_type == OP_NOP + && oap->regname == NUL + && restart_edit == 0); +} + +/* + * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and + * there is no typeahead. + */ + void +may_trigger_safestate(int safe) +{ + int is_safe = safe + && stuff_empty() + && typebuf.tb_len == 0 + && !global_busy; + + if (is_safe) + apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf); + was_safe = is_safe; +} + +/* + * Entering a not-safe state. + */ + void +enter_unsafe_state(void) +{ + ++not_safe_now; +} + +/* + * Leaving a not-safe state. Trigger SafeState if we were in a safe state + * before first calling enter_not_safe_state(). + */ + void +leave_unsafe_state(void) +{ + --not_safe_now; + if (not_safe_now == 0 && was_safe) + apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf); +} + + /* * Main loop: Execute Normal mode commands until exiting Vim. * Also used to handle commands in the command-line window, until the window @@ -1133,6 +1191,9 @@ main_loop( msg_scroll = FALSE; quit_more = FALSE; + // it's not safe unless may_trigger_safestate_main() is called + was_safe = FALSE; + /* * If skip redraw is set (for ":" in wait_return()), don't redraw now. * If there is nothing in the stuff_buffer or do_redraw is TRUE, @@ -1211,6 +1272,10 @@ main_loop( curbuf->b_last_changedtick = CHANGEDTICK(curbuf); } + // If nothing is pending and we are going to wait for the user to + // type a character, trigger SafeState. + may_trigger_safestate_main(&oa); + #if defined(FEAT_DIFF) // Updating diffs from changed() does not always work properly, // esp. updating folds. Do an update just before redrawing if |