From 0cc2104257fb93ec2b71322b491d8672388a8399 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@telfort.nl>
Date: Sun, 29 Mar 2020 13:14:17 +0200
Subject: [PATCH] tweaks: elide a parameter, by calling the relevant function
 beforehand

This means that in most cases mark_is_before_cursor() is called twice:
once before get_region() is called, and once by get_region() itself.
This small duplication of effort is acceptable: the affected functions
are not time critical, and it makes the code shorter.
---
 src/cut.c    |  8 ++++----
 src/files.c  |  2 +-
 src/proto.h  |  4 ++--
 src/search.c |  4 ++--
 src/text.c   | 12 ++++++------
 src/utils.c  | 11 +++--------
 src/winio.c  |  2 +-
 7 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/src/cut.c b/src/cut.c
index b5231371..b85a221e 100644
--- a/src/cut.c
+++ b/src/cut.c
@@ -429,13 +429,13 @@ void cut_to_eol(void)
 }
 
 /* Move all marked text from the current buffer into the cutbuffer. */
-void cut_marked(bool *right_side_up)
+void cut_marked_region(void)
 {
 	linestruct *top, *bot;
 	size_t top_x, bot_x;
 
 	get_region((const linestruct **)&top, &top_x,
-				(const linestruct **)&bot, &bot_x, right_side_up);
+				(const linestruct **)&bot, &bot_x);
 
 	extract_segment(top, top_x, bot, bot_x);
 
@@ -465,7 +465,7 @@ void do_snip(bool copying, bool marked, bool until_eof, bool append)
 		 * before we add text to it. */
 	bool using_magicline = !ISSET(NO_NEWLINES);
 		/* Whether an automatic newline should be added at end-of-buffer. */
-	bool right_side_up = TRUE;
+	bool right_side_up = (openfile->mark == NULL || mark_is_before_cursor());
 		/* There *is* no region, *or* it is marked forward. */
 #endif
 	static bool precedent = FALSE;
@@ -497,7 +497,7 @@ void do_snip(bool copying, bool marked, bool until_eof, bool append)
 	if (until_eof)
 		cut_to_eof();
 	else if (openfile->mark) {
-		cut_marked(&right_side_up);
+		cut_marked_region();
 		openfile->mark = NULL;
 	} else if (ISSET(CUT_FROM_CURSOR))
 		cut_to_eol();
diff --git a/src/files.c b/src/files.c
index cbeba2ed..f3e8764a 100644
--- a/src/files.c
+++ b/src/files.c
@@ -2023,7 +2023,7 @@ bool write_marked_file(const char *name, FILE *stream, bool tmp,
 
 	/* Partition the buffer so that it contains only the marked text. */
 	get_region((const linestruct **)&top, &top_x,
-				(const linestruct **)&bot, &bot_x, NULL);
+				(const linestruct **)&bot, &bot_x);
 	partition_buffer(top, top_x, bot, bot_x);
 
 	/* If we are using a magic line, and the last line of the partition
diff --git a/src/proto.h b/src/proto.h
index a69087e9..66acdb93 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -261,7 +261,7 @@ void extract_segment(linestruct *top, size_t top_x,
 void ingraft_buffer(linestruct *somebuffer);
 void copy_from_buffer(linestruct *somebuffer);
 #ifndef NANO_TINY
-void cut_marked(bool *right_side_up);
+void cut_marked_region(void);
 #endif
 void do_snip(bool copying, bool marked, bool until_eof, bool append);
 void cut_text(void);
@@ -574,7 +574,7 @@ void remove_magicline(void);
 #ifndef NANO_TINY
 bool mark_is_before_cursor(void);
 void get_region(const linestruct **top, size_t *top_x,
-		const linestruct **bot, size_t *bot_x, bool *right_side_up);
+		const linestruct **bot, size_t *bot_x);
 void get_range(const linestruct **top, const linestruct **bot);
 #endif
 size_t get_totsize(const linestruct *begin, const linestruct *end);
diff --git a/src/search.c b/src/search.c
index ef50c38b..4abb3954 100644
--- a/src/search.c
+++ b/src/search.c
@@ -518,12 +518,12 @@ ssize_t do_replace_loop(const char *needle, bool whole_word_only,
 	linestruct *was_mark = openfile->mark;
 	linestruct *top, *bot;
 	size_t top_x, bot_x;
-	bool right_side_up = FALSE;
+	bool right_side_up = (openfile->mark && mark_is_before_cursor());
 
 	/* If the mark is on, frame the region, and turn the mark off. */
 	if (openfile->mark) {
 		get_region((const linestruct **)&top, &top_x,
-					(const linestruct **)&bot, &bot_x, &right_side_up);
+					(const linestruct **)&bot, &bot_x);
 		openfile->mark = NULL;
 		modus = INREGION;
 
diff --git a/src/text.c b/src/text.c
index 9137c8f7..758e26fa 100644
--- a/src/text.c
+++ b/src/text.c
@@ -602,7 +602,7 @@ void do_undo(void)
 		goto_line_posx(u->head_lineno, u->head_x);
 		openfile->mark = line_from_number(u->tail_lineno);
 		openfile->mark_x = u->tail_x;
-		cut_marked(NULL);
+		cut_marked_region();
 		u->cutbuffer = cutbuffer;
 		cutbuffer = oldcutbuffer;
 		if ((u->xflags & INCLUDED_LAST_LINE) && !ISSET(NO_NEWLINES) &&
@@ -1721,7 +1721,7 @@ void do_justify(bool full_justify)
 	linestruct *jusline;
 		/* The line that we're justifying in the current cutbuffer. */
 #ifndef NANO_TINY
-	bool right_side_up = FALSE;
+	bool right_side_up = (openfile->mark && mark_is_before_cursor());
 		/* Whether the mark (if any) is before the cursor. */
 	bool before_eol = FALSE;
 		/* Whether the end of a marked region is before the end of its line. */
@@ -1744,7 +1744,7 @@ void do_justify(bool full_justify)
 		linestruct *sampleline;
 
 		get_region((const linestruct **)&startline, &start_x,
-					(const linestruct **)&endline, &end_x, &right_side_up);
+					(const linestruct **)&endline, &end_x);
 
 		/* When the marked region is empty, do nothing. */
 		if (startline == endline && start_x == end_x) {
@@ -2023,7 +2023,7 @@ bool fix_spello(const char *word)
 #ifndef NANO_TINY
 	linestruct *top, *bot;
 	size_t top_x, bot_x;
-	bool right_side_up = FALSE;
+	bool right_side_up = (openfile->mark && mark_is_before_cursor());
 #endif
 
 	/* Save the current search string, then set it to the misspelled word. */
@@ -2034,7 +2034,7 @@ bool fix_spello(const char *word)
 	/* If the mark is on, start at the beginning of the marked region. */
 	if (openfile->mark) {
 		get_region((const linestruct **)&top, &top_x,
-					(const linestruct **)&bot, &bot_x, &right_side_up);
+					(const linestruct **)&bot, &bot_x);
 		/* If the region is marked normally, swap the end points, so that
 		 * (current, current_x) (where searching starts) is at the top. */
 		if (right_side_up) {
@@ -2913,7 +2913,7 @@ void do_wordlinechar_count(void)
 	 * contains only the marked text, and turn the mark off. */
 	if (openfile->mark) {
 		get_region((const linestruct **)&top, &top_x,
-					(const linestruct **)&bot, &bot_x, NULL);
+					(const linestruct **)&bot, &bot_x);
 		partition_buffer(top, top_x, bot, bot_x);
 	}
 
diff --git a/src/utils.c b/src/utils.c
index e30668eb..4424cf1c 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -459,25 +459,20 @@ bool mark_is_before_cursor(void)
 }
 
 /* Return in (top, top_x) and (bot, bot_x) the start and end "coordinates"
- * of the marked region.  If right_side_up isn't NULL, set it to TRUE when
- * the mark is at the top of the marked region, and to FALSE otherwise. */
+ * of the marked region. */
 void get_region(const linestruct **top, size_t *top_x,
-				const linestruct **bot, size_t *bot_x, bool *right_side_up)
+				const linestruct **bot, size_t *bot_x)
 {
 	if (mark_is_before_cursor()) {
 		*top = openfile->mark;
 		*top_x = openfile->mark_x;
 		*bot = openfile->current;
 		*bot_x = openfile->current_x;
-		if (right_side_up != NULL)
-			*right_side_up = TRUE;
 	} else {
 		*bot = openfile->mark;
 		*bot_x = openfile->mark_x;
 		*top = openfile->current;
 		*top_x = openfile->current_x;
-		if (right_side_up != NULL)
-			*right_side_up = FALSE;
 	}
 }
 
@@ -492,7 +487,7 @@ void get_range(const linestruct **top, const linestruct **bot)
 	} else {
 		size_t top_x, bot_x;
 
-		get_region(top, &top_x, bot, &bot_x, NULL);
+		get_region(top, &top_x, bot, &bot_x);
 
 		if (bot_x == 0 && *bot != *top && !also_the_last)
 			*bot = (*bot)->prev;
diff --git a/src/winio.c b/src/winio.c
index 934d774b..5c6ab66b 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -2688,7 +2688,7 @@ void draw_row(int row, const char *converted, linestruct *line, size_t from_col)
 		int paintlen = -1;
 			/* The number of characters to paint.  Negative means "all". */
 
-		get_region(&top, &top_x, &bot, &bot_x, NULL);
+		get_region(&top, &top_x, &bot, &bot_x);
 
 		if (top->lineno < line->lineno || top_x < from_x)
 			top_x = from_x;