Index: src/text.c =================================================================== --- src/text.c (revisión: 4966) +++ src/text.c (copia de trabajo) @@ -1121,6 +1121,33 @@ prepend_wrap = FALSE; } +/* Return the index where the given line should be broken, or 0 if the + * line should not be broken. */ +ssize_t get_wrap_index(filestruct *line, size_t line_len) +{ + /* Find the last blank where we can break the line. */ + ssize_t wrap_loc = break_line(line->data, fill +#ifndef DISABLE_HELP + , FALSE +#endif + ); + + /* If we couldn't break the line, or we've reached the end of it, we + * don't wrap. */ + if (wrap_loc == -1 || wrap_loc >= line_len) + return 0; + + /* Otherwise, move forward to the character just after the blank. */ + wrap_loc += move_mbright(line->data + wrap_loc, 0); + + /* After the move, check again. If we've reached the end of the line, + * we don't wrap. */ + if (wrap_loc >= line_len) + return 0; + + return wrap_loc; +} + /* Try wrapping the given line. Return TRUE if wrapped, FALSE otherwise. */ bool do_wrap(filestruct *line) { @@ -1128,12 +1155,6 @@ /* The length of the line we wrap. */ ssize_t wrap_loc; /* The index of line->data where we wrap. */ -#ifndef NANO_TINY - const char *indent_string = NULL; - /* Indentation to prepend to the new line. */ - size_t indent_len = 0; - /* The length of indent_string. */ -#endif const char *after_break; /* The text after the wrap point. */ size_t after_break_len; @@ -1162,41 +1183,16 @@ line_len = strlen(line->data); /* Find the last blank where we can break the line. */ - wrap_loc = break_line(line->data, fill -#ifndef DISABLE_HELP - , FALSE -#endif - ); - - /* If we couldn't break the line, or we've reached the end of it, we - * don't wrap. */ - if (wrap_loc == -1 || line->data[wrap_loc] == '\0') + wrap_loc = get_wrap_index(line, line_len); + if (!wrap_loc) return FALSE; - /* Otherwise, move forward to the character just after the blank. */ - wrap_loc += move_mbright(line->data + wrap_loc, 0); - - /* If we've reached the end of the line, we don't wrap. */ - if (line->data[wrap_loc] == '\0') - return FALSE; - #ifndef NANO_TINY - /* If autoindent is turned on, and we're on the character just after - * the indentation, we don't wrap. */ - if (ISSET(AUTOINDENT)) { - /* Get the indentation of this line. */ - indent_string = line->data; - indent_len = indent_length(indent_string); - - if (wrap_loc == indent_len) - return FALSE; - } - add_undo(SPLIT_BEGIN); #endif size_t old_x = openfile->current_x; - filestruct * oldLine = openfile->current; + filestruct *oldline = openfile->current; openfile->current = line; /* Step 2, making the new wrap line. It will consist of indentation @@ -1249,19 +1245,31 @@ } } - /* Go to the wrap location and split the line there. */ - openfile->current_x = wrap_loc; - do_enter(FALSE); + prepend_wrap = (old_x < wrap_loc); - if (old_x < wrap_loc) { - openfile->current_x = old_x; - openfile->current = oldLine; - prepend_wrap = TRUE; - } else { - openfile->current_x += (old_x - wrap_loc); - prepend_wrap = FALSE; + while (wrap_loc) { + /* Go to the wrap location and split the line there. */ + openfile->current_x = wrap_loc; + do_enter(FALSE); + + line = openfile->current; + assert(line != NULL && line->data != NULL); + + if (!prepend_wrap) + old_x = openfile->current_x + old_x - wrap_loc; + + /* Find the last blank where we can break the line. */ + wrap_loc = get_wrap_index(line, strlen(line->data)); + + if (!prepend_wrap && old_x < wrap_loc) { + oldline = openfile->current; + prepend_wrap = TRUE; + } } + if (prepend_wrap) + openfile->current = oldline; + openfile->current_x = old_x; openfile->placewewant = xplustabs(); #ifndef NANO_TINY Index: src/cut.c =================================================================== --- src/cut.c (revisión: 4966) +++ src/cut.c (copia de trabajo) @@ -280,6 +280,11 @@ update_undo(PASTE); #endif +#ifndef DISABLE_WRAPPING + if (!ISSET(NO_WRAP)) + do_wrap(openfile->current); +#endif + /* Set the current place we want to where the text from the * cutbuffer ends. */ openfile->placewewant = xplustabs();