From 364717d64455e1deb7f28a3544a56a9c8dc001a4 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 1 Jun 2016 09:36:05 +0200 Subject: [PATCH 1/2] text: use the general undo-adding mechanism also for commenting --- src/proto.h | 2 -- src/text.c | 57 ++++++++++++++++++++------------------------------------- 2 files changed, 20 insertions(+), 39 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..40712cf 100644 --- a/src/text.c +++ b/src/text.c @@ -43,6 +43,10 @@ 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; + /* The string to be used for commenting a line. */ +#endif #ifndef NANO_TINY /* Toggle the mark. */ @@ -440,17 +444,15 @@ 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; - /* Whether any comment has been added or deleted. */ - assert(openfile->current != NULL && openfile->current->data != NULL); + comment_seq = "#"; + #ifndef DISABLE_COLOR if (openfile->syntax && openfile->syntax->comment) comment_seq = openfile->syntax->comment; @@ -471,9 +473,10 @@ 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; + if (!ISSET(NO_NEWLINES) && top == bot && bot == openfile->filebot) { + statusbar(_("Cannot comment past end of file")); + return; + } /* Figure out whether to comment or uncomment the selected line or lines. */ for (f = top; f != bot->next; f = f->next) { @@ -490,24 +493,17 @@ 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. */ + /* Comment/uncomment a line, and add undo data when line changed. */ + if (comment_line(action, f, comment_seq)) update_comment_undo(f->lineno); - } } - if (file_changed) { - set_modified(); - refresh_needed = TRUE; - } else - statusbar(_("Cannot comment past end of file")); + set_modified(); + refresh_needed = TRUE; } /* Test whether the given line can be uncommented, or add or remove a comment, @@ -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