commit 9192b7fe8c121661db803cd96146e823920acda0
parent 5dee9fb2c663f698a107c4cea00772c9c5298e0e
Author: Benno Schulenberg <bensberg@justemail.net>
Date: Wed, 1 Jun 2016 09:36:05 +0200
text: add an undo item before starting to comment or uncomment lines
By moving the test for the only situation where do_comment() does nothing
(when only the magic line is selected) further up front, the add_undo()
can be called before the commenting/uncommenting starts and two variables
and an extra function are made unnecessary.
Diffstat:
2 files changed, 16 insertions(+), 38 deletions(-)
diff --git 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
@@ -454,12 +454,9 @@ 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);
#ifndef DISABLE_COLOR
@@ -482,9 +479,11 @@ 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 only the magic line is selected, don't do anything. */
+ if (top == bot && bot == openfile->filebot && !ISSET(NO_NEWLINES)) {
+ 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) {
@@ -501,24 +500,21 @@ void do_comment()
/* If all selected lines are blank, we comment them. */
action = all_empty ? COMMENT : action;
+ 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);
+
/* 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,
@@ -1284,22 +1280,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)