From f9103a5cb57296ddfaa44e737e1b59a2e20be14e Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@telfort.nl>
Date: Sat, 3 Mar 2018 16:54:55 +0100
Subject: [PATCH] tweaks: make the fsfromline() call only for the undo types
 that need it

The 'f' variable is used only in the ADD, BACK, DEL, ENTER, JOIN, and
REPLACE undo/redo cases.  So, avoid making a somewhat costly call when
it is entirely superfluous.  Rearrange the undo types to make checking
for the above six types easier.
---
 src/nano.h |  5 +++--
 src/text.c | 22 ++++++++++++----------
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/src/nano.h b/src/nano.h
index c092d3e1..6c031944 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -163,8 +163,9 @@ typedef enum {
 	CENTERING, FLOWING, STATIONARY
 } update_type;
 
+/* The kinds of undo actions.  ADD...REPLACE must come first. */
 typedef enum {
-	ADD, DEL, BACK, CUT, CUT_TO_EOF, REPLACE,
+	ADD, BACK, DEL, ENTER, JOIN, REPLACE,
 #ifdef ENABLE_WRAPPING
 	SPLIT_BEGIN, SPLIT_END,
 #endif
@@ -172,7 +173,7 @@ typedef enum {
 #ifdef ENABLE_COMMENT
 	COMMENT, UNCOMMENT, PREFLIGHT,
 #endif
-	JOIN, PASTE, INSERT, ENTER, OTHER
+	CUT, CUT_TO_EOF, PASTE, INSERT, OTHER
 } undo_type;
 
 /* Structure types. */
diff --git a/src/text.c b/src/text.c
index ce5e9bff..9ef0f86d 100644
--- a/src/text.c
+++ b/src/text.c
@@ -678,7 +678,7 @@ void redo_cut(undo *u)
 void do_undo(void)
 {
 	undo *u = openfile->current_undo;
-	filestruct *f, *t = NULL;
+	filestruct *f = NULL, *t = NULL;
 	filestruct *oldcutbuffer, *oldcutbottom;
 	char *data, *undidmsg = NULL;
 	size_t from_x, to_x;
@@ -688,13 +688,14 @@ void do_undo(void)
 		return;
 	}
 
-	f = fsfromline(u->mark_begin_lineno);
-	if (!f)
-		return;
+	if (u->type <= REPLACE) {
+		f = fsfromline(u->mark_begin_lineno);
+		if (f == NULL)
+			return;
+	}
 
 #ifdef DEBUG
 	fprintf(stderr, "  >> Undoing a type %d...\n", u->type);
-	fprintf(stderr, "  >> Data we're about to undo = \"%s\"\n", f->data);
 #endif
 
 	openfile->current_x = u->begin;
@@ -848,7 +849,7 @@ void do_undo(void)
 /* Redo the last thing(s) we undid. */
 void do_redo(void)
 {
-	filestruct *f, *shoveline;
+	filestruct *f = NULL, *shoveline;
 	char *data, *redidmsg = NULL;
 	undo *u = openfile->undotop;
 
@@ -866,13 +867,14 @@ void do_redo(void)
 		return;
 	}
 
-	f = fsfromline(u->type == INSERT ? 1 : u->mark_begin_lineno);
-	if (!f)
-		return;
+	if (u->type <= REPLACE) {
+		f = fsfromline(u->mark_begin_lineno);
+		if (f == NULL)
+			return;
+	}
 
 #ifdef DEBUG
 	fprintf(stderr, "  >> Redo running for type %d\n", u->type);
-	fprintf(stderr, "  >> Data we're about to redo = \"%s\"\n", f->data);
 #endif
 
 	switch (u->type) {
-- 
GitLab