[RFC PATCH v1 1/1] macro: Cancel recording if nothing was entered

Charles Mirabile <[email protected]> Tue, 10 Feb 2026 12:21:21 -0500
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
If the user stops the macro recording again immediately after starting it
cancel the recording and revert to the previous stored macro instead of
recording a new empty macro.

Signed-off-by: Charles Mirabile <[email protected]>
---
 src/winio.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/winio.c b/src/winio.c
index f6d07f56..5e1108d4 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -79,6 +79,10 @@ static int *macro_buffer = NULL;
 		/* A buffer where the recorded key codes are stored. */
 static size_t macro_length = 0;
 		/* The current length of the macro. */
+static int *old_macro_buffer = NULL;
+		/* The previous macro to restore if recording is cancelled */
+static size_t old_macro_length = 0;
+		/* The previous length to restore if recording is cancelled */
 static size_t milestone = 0;
 		/* Where the last burst of recorded keystrokes started. */
 
@@ -96,12 +100,24 @@ void record_macro(void)
 	recording = !recording;
 
 	if (recording) {
+		old_macro_length = macro_length;
+		old_macro_buffer = macro_buffer;
+
 		macro_length = 0;
+		macro_buffer = NULL;
 		statusline(REMARK, _("Recording a macro..."));
 	} else {
 		/* Snip the keystroke that invoked this function. */
 		macro_length = milestone;
-		statusline(REMARK, _("Stopped recording"));
+		if (macro_length) {
+			statusline(REMARK, _("Stopped recording"));
+			free(old_macro_buffer);
+		} else {
+			macro_length = old_macro_length;
+			free(macro_buffer);
+			macro_buffer = old_macro_buffer;
+			statusline(REMARK, _("Cancelled"));
+		}
 	}
 
 	if (ISSET(STATEFLAGS))
-- 
2.51.0