RFC: [PATCH] execute: retain what the user typed before a tool was invoked

Benno Schulenberg <[email protected]>
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
When the user types something at the Execute prompt and then invokes
one of the six tools or special functions (that don't take any input),
stash the typed string and restore it upon the next invocation of the
Execute menu.  This way the typed string is not lost when the tool or
special function was invoked by accident -- which it likely was, as
those tools and functions don't take any input from the prompt.

This addresses https://savannah.gnu.org/bugs/?66637.
Suggested-by: Evgeny Pestov <[email protected]>


  Does anyone foresee problems in their workflow when this patch is applied?


---
 src/files.c      |  6 ++++++
 src/global.c     |  5 +++++
 src/nano.c       |  3 +++
 src/prompt.c     | 10 ++++++++++
 src/prototypes.h |  2 ++
 5 files changed, 26 insertions(+)

diff --git a/src/files.c b/src/files.c
index 416192dc..28c864a8 100644
--- a/src/files.c
+++ b/src/files.c
@@ -1197,6 +1197,12 @@ void insert_a_file_or(bool execute)
 	/* Reset the flag that is set by the Spell Checker and Linter and such. */
 	ran_a_tool = FALSE;
 
+#ifndef NANO_TINY
+	/* If something was typed at the Execute prompt without being run, restore it. */
+	if (execute && *foretext)
+		given = mallocstrcpy(given, foretext);
+#endif
+
 	while (TRUE) {
 #ifndef NANO_TINY
 		if (execute) {
diff --git a/src/global.c b/src/global.c
index fb1c6db3..56364238 100644
--- a/src/global.c
+++ b/src/global.c
@@ -52,8 +52,13 @@ bool more_than_one = FALSE;
 		/* Whether more than one buffer is or has been open. */
 bool report_size = TRUE;
 		/* Whether to show the number of lines when the minibar is used. */
+
 bool ran_a_tool = FALSE;
 		/* Whether a tool has been run at the Execute-Command prompt. */
+#ifndef NANO_TINY
+char *foretext = NULL;
+		/* What was typed at the Execute prompt before invoking a tool. */
+#endif
 
 bool inhelp = FALSE;
 		/* Whether we are in the help viewer. */
diff --git a/src/nano.c b/src/nano.c
index b3f51e06..f2b4680b 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2384,6 +2384,9 @@ int main(int argc, char **argv)
 			whitelen[1] = 1;
 		}
 	}
+
+	/* Initialize a special stash for something typed at the Execute prompt. */
+	foretext = copy_of("");
 #endif /* !NANO_TINY */
 
 	/* Initialize the search string. */
diff --git a/src/prompt.c b/src/prompt.c
index 3129a4f8..e47f24ab 100644
--- a/src/prompt.c
+++ b/src/prompt.c
@@ -538,6 +538,11 @@ functionptrtype acquire_an_answer(int *actual, bool *listed,
 		else if (function && !handle_editing(function)) {
 			/* When it's a permissible shortcut, run it and done. */
 			if (!ISSET(VIEW_MODE) || !changes_something(function)) {
+#ifndef NANO_TINY
+				/* When invoking a tool at the Execute prompt, stash an "answer". */
+				if (currmenu == MEXECUTE)
+					foretext = mallocstrcpy(foretext, answer);
+#endif
 				function();
 				break;
 			} else
@@ -549,6 +554,11 @@ functionptrtype acquire_an_answer(int *actual, bool *listed,
 #endif
 	}
 
+#ifndef NANO_TINY
+	/* When an external command was run, clear a possibly stashed answer. */
+	if (currmenu == MEXECUTE && function == do_enter)
+		*foretext = '\0';
+#endif
 #ifdef ENABLE_HISTORIES
 	/* If the history pointer was moved, point it at the bottom again. */
 	if (stored_string != NULL) {
diff --git a/src/prototypes.h b/src/prototypes.h
index 9bbca254..d033f292 100644
--- a/src/prototypes.h
+++ b/src/prototypes.h
@@ -37,7 +37,9 @@ extern bool bracketed_paste;
 extern bool we_are_running;
 extern bool more_than_one;
 extern bool report_size;
+
 extern bool ran_a_tool;
+extern char *foretext;
 
 extern bool inhelp;
 extern char *title;
-- 
2.48.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.