From 618d0d515abec1244a1398d67c3e6cffc44b76c7 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 1 Jun 2016 09:36:05 +0200 Subject: [PATCH] text: use the general undo-adding mechanism also for commenting --- src/proto.h | 2 -- src/text.c | 37 ++++++++++--------------------------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/src/proto.h b/src/proto.h index cf2b4ad..a5933b9 100644 --- a/src/proto.h +++ b/src/proto.h @@ -749,8 +749,6 @@ void discard_until(const undo *thisitem, openfilestruct *thefile); void add_undo(undo_type action); void update_undo(undo_type action); #ifndef DISABLE_COMMENT -void add_comment_undo(undo_type action, const char *comment_seq, size_t was_x, - size_t was_size); void update_comment_undo(ssize_t lineno); bool comment_line(undo_type action, filestruct *f, const char *comment_seq); #endif diff --git a/src/text.c b/src/text.c index d59da21..7cb6b2e 100644 --- a/src/text.c +++ b/src/text.c @@ -43,6 +43,9 @@ static bool prepend_wrap = FALSE; static filestruct *jusbottom = NULL; /* Pointer to the end of the justify buffer. */ #endif +#ifdef ENABLE_COMMENT +static char *comment_seq = "#"; +#endif #ifndef NANO_TINY /* Toggle the mark. */ @@ -440,10 +443,9 @@ bool white_string(const char *s) /* Comment or uncomment the current line or the marked lines. */ void do_comment() { - const char *comment_seq = "#"; undo_type action = UNCOMMENT; filestruct *top, *bot, *f; - size_t top_x, bot_x, was_x, was_size; + size_t top_x, bot_x; bool empty, all_empty = TRUE; bool file_changed = FALSE; @@ -471,10 +473,6 @@ void do_comment() bot = top; } - /* Remember cursor position and file size, to be restored when undoing. */ - was_x = openfile->current_x; - was_size = openfile->totsize; - /* Figure out whether to comment or uncomment the selected line or lines. */ for (f = top; f != bot->next; f = f->next) { empty = white_string(f->data); @@ -490,16 +488,14 @@ void do_comment() /* If all selected lines are blank, we comment them. */ action = all_empty ? COMMENT : action; + add_undo(action); + /* Process the selected line or lines. */ for (f = top; f != bot->next; f = f->next) { if (comment_line(action, f, comment_seq)) { - if (!file_changed) { - /* Start building undo data on the first modified line. */ - add_comment_undo(action, comment_seq, was_x, was_size); - file_changed = TRUE; - } /* Add undo data for each modified line. */ update_comment_undo(f->lineno); + file_changed = TRUE; } } @@ -1257,6 +1253,9 @@ void add_undo(undo_type action) #ifdef ENABLE_COMMENT case COMMENT: case UNCOMMENT: + /* Store the comment sequence used for the operation, because it could + * change when the file name changes; we need to know what it was. */ + u->strdata = mallocstrcpy(NULL, comment_seq); break; #endif default: @@ -1273,22 +1272,6 @@ void add_undo(undo_type action) } #ifdef ENABLE_COMMENT -/* Add a comment undo item. This should be called once for each use - * of the comment/uncomment feature that modifies the document. */ -void add_comment_undo(undo_type action, const char *comment_seq, size_t was_x, - size_t was_size) -{ - add_undo(action); - - /* Store the comment sequence used for the operation, because it could - * change when the file name changes; we need to know what it was. */ - openfile->current_undo->strdata = mallocstrcpy(NULL, comment_seq); - - /* Store cursor position and file size from before the change. */ - openfile->current_undo->begin = was_x; - openfile->current_undo->wassize= was_size; -} - /* Update a comment undo item. This should be called once for each line * affected by the comment/uncomment feature. */ void update_comment_undo(ssize_t lineno) -- 2.8.1