Index: src/cut.c =================================================================== --- src/cut.c (revision 5003) +++ src/cut.c (working copy) @@ -284,6 +284,10 @@ if (cutbuffer == NULL) return; +#ifndef DISABLE_WRAPPING + ssize_t startline = openfile->current->lineno; +#endif + #ifndef NANO_TINY add_undo(PASTE); #endif @@ -296,6 +300,26 @@ update_undo(PASTE); #endif +#ifndef DISABLE_WRAPPING + if (!ISSET(NO_WRAP)){ + +#ifndef NANO_TINY + add_undo(SPLIT_BEGIN); +#endif + + if(xplustabs() == 0){ + openfile->current = openfile->current->prev; + join_currentline(); + } + + do_wrap( fsfromline(startline), openfile->current, FALSE); + +#ifndef NANO_TINY + add_undo(SPLIT_END); +#endif + } +#endif + /* Set the current place we want to where the text from the * cutbuffer ends. */ openfile->placewewant = xplustabs(); Index: src/nano.c =================================================================== --- src/nano.c (revision 5003) +++ src/nano.c (working copy) @@ -2065,7 +2065,7 @@ #ifndef DISABLE_WRAPPING /* If we're wrapping text, we need to call edit_refresh(). */ if (!ISSET(NO_WRAP)) - if (do_wrap(openfile->current)) + if (do_wrap(openfile->current, NULL, TRUE)) edit_refresh_needed = TRUE; #endif Index: src/proto.h =================================================================== --- src/proto.h (revision 5003) +++ src/proto.h (working copy) @@ -659,7 +659,8 @@ #endif #ifndef DISABLE_WRAPPING void wrap_reset(void); -bool do_wrap(filestruct *line); +bool do_wrap(filestruct *line, filestruct *until_line, bool addundo); +void join_currentline(void); #endif #if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY) ssize_t break_line(const char *line, ssize_t goal Index: src/text.c =================================================================== --- src/text.c (revision 5003) +++ src/text.c (working copy) @@ -1112,48 +1112,12 @@ prepend_wrap = FALSE; } -/* Try wrapping the given line. Return TRUE if wrapped, FALSE otherwise. */ -bool do_wrap(filestruct *line) +/* 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) { - size_t line_len; - /* 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; - /* The length of after_break. */ - const char *next_line = NULL; - /* The next line, minus indentation. */ - size_t next_line_len = 0; - /* The length of next_line. */ - - /* There are three steps. First, we decide where to wrap. Then, we - * create the new wrap line. Finally, we clean up. */ - - /* Step 1, finding where to wrap. We are going to add a new line - * after a blank character. In this step, we call break_line() to - * get the location of the last blank we can break the line at, and - * set wrap_loc to the location of the character after it, so that - * the blank is preserved at the end of the line. - * - * If there is no legal wrap point, or we reach the last character - * of the line while trying to find one, we should return without - * wrapping. Note that if autoindent is turned on, we don't break - * at the end of it! */ - assert(line != NULL && line->data != NULL); - - /* Save the length of the line. */ - line_len = strlen(line->data); - /* Find the last blank where we can break the line. */ - wrap_loc = break_line(line->data, fill + ssize_t wrap_loc = break_line(line->data, fill #ifndef DISABLE_HELP , FALSE #endif @@ -1161,58 +1125,32 @@ /* 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') - return FALSE; + 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); - /* If we've reached the end of the line, we don't wrap. */ - if (line->data[wrap_loc] == '\0') - return FALSE; + /* 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; -#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; + return wrap_loc; } - add_undo(SPLIT_BEGIN); -#endif +/*join line with line->next */ +void join_currentline(void){ + filestruct *line = openfile->current; + if(line == openfile->filebot) return; - size_t old_x = openfile->current_x; - filestruct * oldLine = openfile->current; - openfile->current = line; + size_t line_len = strlen(line->data); + const char *end = line->data + move_mbleft(line->data, line_len); - /* Step 2, making the new wrap line. It will consist of indentation - * followed by the text after the wrap point, optionally followed by - * a space (if the text after the wrap point doesn't end in a blank) - * and the text of the next line, if they can fit without wrapping, - * the next line exists, and the prepend_wrap flag is set. */ - - /* after_break is the text that will be wrapped to the next line. */ - after_break = line->data + wrap_loc; - after_break_len = line_len - wrap_loc; - - assert(strlen(after_break) == after_break_len); - - /* We prepend the wrapped text to the next line, if the prepend_wrap - * flag is set, there is a next line, and prepending would not make - * the line too long. */ - if (prepend_wrap && line != openfile->filebot) { - const char *end = after_break + move_mbleft(after_break, - after_break_len); - /* Go to the end of the line. */ openfile->current_x = line_len; - /* If after_break doesn't end in a blank, make sure it ends in a + /* If the line doesn't end in a blank, make sure it ends in a * space. */ if (!is_blank_mbchar(end)) { #ifndef NANO_TINY @@ -1222,8 +1160,7 @@ line->data = charealloc(line->data, line_len + 1); line->data[line_len - 1] = ' '; line->data[line_len] = '\0'; - after_break = line->data + wrap_loc; - after_break_len++; + openfile->totsize++; openfile->current_x++; #ifndef NANO_TINY @@ -1231,10 +1168,6 @@ #endif } - next_line = line->next->data; - next_line_len = strlen(next_line); - - if (after_break_len + next_line_len <= fill) { /* Delete the LF to join the two lines. */ do_delete(); /* Delete any leading blanks from the joined-on line. */ @@ -1242,24 +1175,86 @@ do_delete(); renumber(line); } - } +/* Try wrapping the given line. Return TRUE if wrapped, FALSE otherwise. */ +bool do_wrap(filestruct *line, filestruct *until_line, bool addundo) +{ + ssize_t wrap_loc; + /* The index of line->data where we wrap. */ + + if(until_line == NULL) + until_line = line; + size_t old_x = openfile->current_x; + filestruct *oldline = openfile->current; + + while (line->lineno <= until_line->lineno){ + + /* There are three steps. First, we decide where to wrap. Then, we + * create the new wrap line. Finally, we clean up. */ + + /* Step 1, finding where to wrap. We are going to add a new line + * after a blank character. In this step, we call break_line() to + * get the location of the last blank we can break the line at, and + * set wrap_loc to the location of the character after it, so that + * the blank is preserved at the end of the line. + * + * If there is no legal wrap point, or we reach the last character + * of the line while trying to find one, we should return without + * wrapping. Note that if autoindent is turned on, we don't break + * at the end of it! */ + assert(line != NULL && line->data != NULL); + + /* Find the last blank where we can break the line. */ + wrap_loc = get_wrap_index(line, strlen(line->data)); + if (!wrap_loc) + return FALSE; + +#ifndef NANO_TINY + if(addundo) + add_undo(SPLIT_BEGIN); +#endif + + + openfile->current = line; + + /* Step 2, making the new wrap line. It will consist of indentation + * followed by the text after the wrap point, optionally followed by + * a space (if the text after the wrap point doesn't end in a blank) + * and the text of the next line, if they can fit without wrapping, + * the next line exists, and the prepend_wrap flag is set. */ + + join_currentline(); + + prepend_wrap = (old_x < wrap_loc); + + while (wrap_loc) { /* Go to the wrap location and split the line there. */ openfile->current_x = wrap_loc; do_enter(FALSE); - if (old_x < wrap_loc) { - openfile->current_x = old_x; - openfile->current = oldLine; + 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; - } else { - openfile->current_x += (old_x - wrap_loc); - prepend_wrap = FALSE; } + } + } + if (prepend_wrap) + openfile->current = oldline; + openfile->current_x = old_x; openfile->placewewant = xplustabs(); #ifndef NANO_TINY + if(addundo) add_undo(SPLIT_END); #endif