From c22fef18e424d68b8d1cea216b0a1628310001b5 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@telfort.nl>
Date: Sat, 23 Dec 2017 21:32:47 +0100
Subject: [PATCH] tweaks: reshuffle a bit of common code, moving it to an
 existing function

---
 src/proto.h |  2 +-
 src/text.c  | 21 +++------------------
 src/utils.c | 20 +++++++++++++-------
 3 files changed, 17 insertions(+), 26 deletions(-)

diff --git a/src/proto.h b/src/proto.h
index 89b6485d..b1e9d188 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -609,7 +609,7 @@ void remove_magicline(void);
 #ifndef NANO_TINY
 void mark_order(const filestruct **top, size_t *top_x, const filestruct
 	**bot, size_t *bot_x, bool *right_side_up);
-void get_region(const filestruct **top, const filestruct **bot);
+void get_range(const filestruct **top, const filestruct **bot);
 #endif
 size_t get_totsize(const filestruct *begin, const filestruct *end);
 #ifndef NANO_TINY
diff --git a/src/text.c b/src/text.c
index c8d1f650..0a98a1e1 100644
--- a/src/text.c
+++ b/src/text.c
@@ -298,12 +298,7 @@ void do_indent(void)
     filestruct *top, *bot, *line;
 
     /* Use either all the marked lines or just the current line. */
-    if (openfile->mark)
-	get_region((const filestruct **)&top, (const filestruct **)&bot);
-    else {
-	top = openfile->current;
-	bot = top;
-    }
+    get_range((const filestruct **)&top, (const filestruct **)&bot);
 
     /* Go through the lines to see if there's a non-empty one. */
     for (line = top; line != bot->next; line = line->next) {
@@ -401,12 +396,7 @@ void do_unindent(void)
     filestruct *top, *bot, *line;
 
     /* Use either all the marked lines or just the current line. */
-    if (openfile->mark)
-	get_region((const filestruct **)&top, (const filestruct **)&bot);
-    else {
-	top = openfile->current;
-	bot = top;
-    }
+    get_range((const filestruct **)&top, (const filestruct **)&bot);
 
     /* Check if there is a line that can be unindented. */
     for (line = top; line != bot->next; line = line->next) {
@@ -503,12 +493,7 @@ void do_comment(void)
 #endif
 
     /* Determine which lines to work on. */
-    if (openfile->mark)
-	get_region((const filestruct **)&top, (const filestruct **)&bot);
-    else {
-	top = openfile->current;
-	bot = top;
-    }
+    get_range((const filestruct **)&top, (const filestruct **)&bot);
 
     /* If only the magic line is selected, don't do anything. */
     if (top == bot && bot == openfile->filebot && !ISSET(NO_NEWLINES)) {
diff --git a/src/utils.c b/src/utils.c
index 5a2fbaed..325c69f1 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -532,16 +532,22 @@ void mark_order(const filestruct **top, size_t *top_x, const filestruct
     }
 }
 
-/* Get the start and end points of the marked region, but
- * push the end point back if it's at the start of a line. */
-void get_region(const filestruct **top, const filestruct **bot)
+/* Get the set of lines to work on -- either just the current line, or the
+ * first to last lines of the marked region.  When the cursor (or mark) is
+ * at the start of the last line of the region, exclude that line. */
+void get_range(const filestruct **top, const filestruct **bot)
 {
-    size_t top_x, bot_x;
+    if (!openfile->mark) {
+	*top = openfile->current;
+	*bot = openfile->current;
+    } else {
+	size_t top_x, bot_x;
 
-    mark_order(top, &top_x, bot, &bot_x, NULL);
+	mark_order(top, &top_x, bot, &bot_x, NULL);
 
-    if (bot_x == 0 && *bot != *top)
-	*bot = (*bot)->prev;
+	if (bot_x == 0 && *bot != *top)
+	    *bot = (*bot)->prev;
+    }
 }
 
 /* Given a line number, return a pointer to the corresponding struct. */
-- 
GitLab