@@ -1048,25 +1048,34 @@ op_pending(void)
10481048 && current_oap -> regname == NUL );
10491049}
10501050
1051+ /*
1052+ * Return whether currently it is safe, assuming it was safe before (high level
1053+ * state didn't change).
1054+ */
1055+ static int
1056+ is_safe_now (void )
1057+ {
1058+ return stuff_empty ()
1059+ && typebuf .tb_len == 0
1060+ && scriptin [curscript ] == NULL
1061+ && !global_busy ;
1062+ }
1063+
10511064/*
10521065 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
10531066 * there is no typeahead.
10541067 */
10551068 void
10561069may_trigger_safestate (int safe )
10571070{
1058- int is_safe = safe
1059- && stuff_empty ()
1060- && typebuf .tb_len == 0
1061- && scriptin [curscript ] == NULL
1062- && !global_busy ;
1071+ int is_safe = safe && is_safe_now ();
10631072
10641073#ifdef FEAT_JOB_CHANNEL
10651074 if (was_safe != is_safe )
10661075 // Only log when the state changes, otherwise it happens at nearly
10671076 // every key stroke.
1068- ch_log (NULL , is_safe ? "Start triggering SafeState "
1069- : "Stop triggering SafeState " );
1077+ ch_log (NULL , is_safe ? "SafeState: Start triggering"
1078+ : "SafeState: Stop triggering" );
10701079#endif
10711080 if (is_safe )
10721081 apply_autocmds (EVENT_SAFESTATE , NULL , NULL , FALSE, curbuf );
@@ -1079,32 +1088,50 @@ may_trigger_safestate(int safe)
10791088 * may_trigger_safestate().
10801089 */
10811090 void
1082- state_no_longer_safe (void )
1091+ state_no_longer_safe (char * reason UNUSED )
10831092{
10841093#ifdef FEAT_JOB_CHANNEL
10851094 if (was_safe )
1086- ch_log (NULL , "safe state reset" );
1095+ ch_log (NULL , "SafeState: reset: %s" , reason );
10871096#endif
10881097 was_safe = FALSE;
10891098}
10901099
1100+ int
1101+ get_was_safe_state (void )
1102+ {
1103+ return was_safe ;
1104+ }
1105+
10911106/*
10921107 * Invoked when leaving code that invokes callbacks. Then trigger
10931108 * SafeStateAgain, if it was safe when starting to wait for a character.
10941109 */
10951110 void
10961111may_trigger_safestateagain (void )
10971112{
1113+ if (!was_safe )
1114+ {
1115+ // If the safe state was reset in state_no_longer_safe(), e.g. because
1116+ // of calling feedkeys(), we check if it's now safe again (all keys
1117+ // were consumed).
1118+ was_safe = is_safe_now ();
1119+ #ifdef FEAT_JOB_CHANNEL
1120+ if (was_safe )
1121+ ch_log (NULL , "SafeState: undo reset" );
1122+ #endif
1123+ }
10981124 if (was_safe )
10991125 {
11001126#ifdef FEAT_JOB_CHANNEL
1101- ch_log (NULL , "Leaving unsafe area , triggering SafeStateAgain" );
1127+ ch_log (NULL , "SafeState: back to waiting , triggering SafeStateAgain" );
11021128#endif
11031129 apply_autocmds (EVENT_SAFESTATEAGAIN , NULL , NULL , FALSE, curbuf );
11041130 }
11051131#ifdef FEAT_JOB_CHANNEL
11061132 else
1107- ch_log (NULL , "Leaving unsafe area, not triggering SafeStateAgain" );
1133+ ch_log (NULL ,
1134+ "SafeState: back to waiting, not triggering SafeStateAgain" );
11081135#endif
11091136}
11101137
0 commit comments