patch 8.1.2047: cannot check the current state

Problem:    Cannot check the current state.
Solution:   Add the state() function.
diff --git a/src/main.c b/src/main.c
index 51e3915..42aa990 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1031,20 +1031,21 @@
 
 // When TRUE in a safe state when starting to wait for a character.
 static int	was_safe = FALSE;
+static oparg_T	*current_oap = NULL;
 
 /*
- * Trigger SafeState if currently in a safe state for main_loop().
+ * Return TRUE if an operator was started but not finished yet.
+ * Includes typing a count or a register name.
  */
-    static void
-may_trigger_safestate_main(oparg_T *oap)
+    int
+op_pending(void)
 {
-    may_trigger_safestate(
-	    !finish_op
-	    && oap->prev_opcount > 0
-	    && oap->prev_count0 == 0
-	    && oap->op_type == OP_NOP
-	    && oap->regname == NUL
-	    && restart_edit == 0);
+    return !(current_oap != NULL
+	    && !finish_op
+	    && current_oap->prev_opcount == 0
+	    && current_oap->prev_count0 == 0
+	    && current_oap->op_type == OP_NOP
+	    && current_oap->regname == NUL);
 }
 
 /*
@@ -1100,15 +1101,19 @@
     int		cmdwin,	    /* TRUE when working in the command-line window */
     int		noexmode)   /* TRUE when return on entering Ex mode */
 {
-    oparg_T	oa;	/* operator arguments */
-    volatile int previous_got_int = FALSE;	/* "got_int" was TRUE */
+    oparg_T	oa;		// operator arguments
+    oparg_T	*prev_oap;	// operator arguments
+    volatile int previous_got_int = FALSE;	// "got_int" was TRUE
 #ifdef FEAT_CONCEAL
-    /* these are static to avoid a compiler warning */
+    // these are static to avoid a compiler warning
     static linenr_T	conceal_old_cursor_line = 0;
     static linenr_T	conceal_new_cursor_line = 0;
     static int		conceal_update_lines = FALSE;
 #endif
 
+    prev_oap = current_oap;
+    current_oap = &oa;
+
 #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
     /* Setup to catch a terminating error from the X server.  Just ignore
      * it, restore the state and continue.  This might not always work
@@ -1276,7 +1281,7 @@
 
 	    // If nothing is pending and we are going to wait for the user to
 	    // type a character, trigger SafeState.
-	    may_trigger_safestate_main(&oa);
+	    may_trigger_safestate(!op_pending() && restart_edit == 0);
 
 #if defined(FEAT_DIFF)
 	    // Updating diffs from changed() does not always work properly,
@@ -1430,7 +1435,7 @@
 	if (exmode_active)
 	{
 	    if (noexmode)   /* End of ":global/path/visual" commands */
-		return;
+		goto theend;
 	    do_exmode(exmode_active == EXMODE_VIM);
 	}
 	else
@@ -1457,6 +1462,9 @@
 	    }
 	}
     }
+
+theend:
+    current_oap = prev_oap;
 }