editing: avoid creating blank lines when using autoindent

When Enter is pressed while the cursor is exactly on the current
indent width, remove the blank characters on that line to avoid
creating a line that consists only of trailing whitespace.

(When Enter is pressed somewhere in the middle of the blanks,
however, the whitespace is preserved.)

Suggested-by: Florian Zeitz <florob@babelmonkeys.de>
master
Benno Schulenberg 2017-04-06 20:34:04 +02:00
parent f3e2ad945d
commit 005ee8eda6
1 changed files with 15 additions and 6 deletions

View File

@ -768,11 +768,13 @@ void do_undo(void)
break;
}
undidmsg = _("line break");
size_t from_x = (u->begin == 0) ? 0 : u->mark_begin_x;
size_t to_x = (u->begin == 0) ? u->mark_begin_x : u->begin;
f->data = charealloc(f->data, strlen(f->data) +
strlen(&f->next->data[u->mark_begin_x]) + 1);
strcat(f->data, &f->next->data[u->mark_begin_x]);
strlen(&u->strdata[from_x]) + 1);
strcat(f->data, &u->strdata[from_x]);
unlink_node(f->next);
goto_line_posx(u->lineno, u->begin);
goto_line_posx(u->lineno, to_x);
break;
#ifdef ENABLE_COMMENT
case COMMENT:
@ -982,12 +984,11 @@ void do_enter(void)
{
filestruct *newnode = make_new_node(openfile->current);
size_t extra = 0;
bool allblanks = FALSE;
assert(openfile->current != NULL && openfile->current->data != NULL);
#ifndef NANO_TINY
add_undo(ENTER);
if (ISSET(AUTOINDENT)) {
extra = indent_length(openfile->current->data);
@ -995,6 +996,8 @@ void do_enter(void)
* indentation to the current x position. */
if (extra > openfile->current_x)
extra = openfile->current_x;
else if (extra == openfile->current_x)
allblanks = TRUE;
}
#endif
newnode->data = charalloc(strlen(openfile->current->data +
@ -1005,13 +1008,19 @@ void do_enter(void)
if (ISSET(AUTOINDENT)) {
/* Copy the whitespace from the current line to the new one. */
strncpy(newnode->data, openfile->current->data, extra);
openfile->totsize += extra;
/* If there were only blanks before the cursor, trim them. */
if (allblanks)
openfile->current_x = 0;
else
openfile->totsize += extra;
}
#endif
null_at(&openfile->current->data, openfile->current_x);
#ifndef NANO_TINY
add_undo(ENTER);
/* Adjust the mark if it was on the current line after the cursor. */
if (openfile->mark_set && openfile->current == openfile->mark_begin &&
openfile->current_x < openfile->mark_begin_x) {