commit 2bc5c1987c1499b2395fdbefb07d81af16429198
parent e39e2ddf5256ae90a5564182c1d4934f9a7f2d16
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Thu, 5 Mar 2020 16:09:56 +0100
tweaks: factor out a three-line condition into a separate function
For clarity.
Diffstat:
4 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/src/cut.c b/src/cut.c
@@ -330,10 +330,8 @@ void ingraft_buffer(linestruct *topline)
bool edittop_inside = (openfile->edittop == openfile->current);
#ifndef NANO_TINY
/* Remember whether mark and cursor are on the same line, and their order. */
+ bool right_side_up = (openfile->mark && mark_is_before_cursor());
bool same_line = (openfile->mark == openfile->current);
- bool right_side_up = (openfile->mark &&
- (openfile->mark->lineno < openfile->current->lineno ||
- (same_line && openfile->mark_x <= openfile->current_x)));
#endif
/* Partition the buffer so that it contains no text, then delete it.*/
diff --git a/src/proto.h b/src/proto.h
@@ -578,6 +578,7 @@ void new_magicline(void);
void remove_magicline(void);
#endif
#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);
void get_range(const linestruct **top, const linestruct **bot);
diff --git a/src/text.c b/src/text.c
@@ -1181,9 +1181,7 @@ void add_undo(undo_type action, const char *message)
case CUT:
if (openfile->mark) {
u->xflags |= MARK_WAS_SET;
- if (openfile->mark->lineno < openfile->current->lineno ||
- (openfile->mark == openfile->current &&
- openfile->mark_x < openfile->current_x)) {
+ if (mark_is_before_cursor()){
u->head_lineno = openfile->mark->lineno;
u->head_x = openfile->mark_x;
} else {
@@ -2552,10 +2550,8 @@ const char *treat(char *tempfile_name, char *theprogram, bool spelling)
#ifndef NANO_TINY
/* Replace the marked text (or entire text) with the corrected text. */
if (spelling && openfile->mark) {
- bool upright = (openfile->mark->lineno < openfile->current->lineno ||
- (openfile->mark == openfile->current &&
- openfile->mark_x < openfile->current_x));
ssize_t was_mark_lineno = openfile->mark->lineno;
+ bool upright = mark_is_before_cursor();
replaced = replace_buffer(tempfile_name, CUT, TRUE, "spelling correction");
diff --git a/src/utils.c b/src/utils.c
@@ -450,15 +450,21 @@ void remove_magicline(void)
#endif
#ifndef NANO_TINY
+/* Return TRUE when the mark is before or at the cursor, and FALSE otherwise. */
+bool mark_is_before_cursor(void)
+{
+ return (openfile->mark->lineno < openfile->current->lineno ||
+ (openfile->mark == openfile->current &&
+ openfile->mark_x <= openfile->current_x));
+}
+
/* 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. */
void get_region(const linestruct **top, size_t *top_x,
const linestruct **bot, size_t *bot_x, bool *right_side_up)
{
- if (openfile->mark->lineno < openfile->current->lineno ||
- (openfile->mark == openfile->current &&
- openfile->mark_x < openfile->current_x)) {
+ if (mark_is_before_cursor()) {
*top = openfile->mark;
*top_x = openfile->mark_x;
*bot = openfile->current;