>From 8fce23152de82fac32f8c8faca0ccf9b60f8d93b Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Sat, 28 Jul 2018 02:44:43 -0600 Subject: [PATCH] only newline "words" span lines nextword and cutwordright stop at end of line. if at end of line, they stop after whitespace of next non-blank line. prevword and cutwordleft stop at start of line. if at start of line or after whitespace indentation, they stop at end of previous non-blank line. Signed-off-by: Brand Huntsman --- src/move.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/src/move.c b/src/move.c index bf6a0c11..ef2f2e68 100644 --- a/src/move.c +++ b/src/move.c @@ -278,20 +278,57 @@ void do_prev_word(bool allow_punct, bool update_screen) filestruct *was_current = openfile->current; bool seen_a_word = FALSE, step_forward = FALSE; - /* Move backward until we pass over the start of a word. */ - while (TRUE) { - /* If at the head of a line, move to the end of the preceding one. */ if (openfile->current_x == 0) { + do { if (openfile->current->prev == NULL) break; openfile->current = openfile->current->prev; openfile->current_x = strlen(openfile->current->data); - } + } while(openfile->current_x == 0); + } else { + bool seen_space = FALSE; + /* Move backward until we pass over the start of a word. */ + while (TRUE) { /* Step back one character. */ openfile->current_x = move_mbleft(openfile->current->data, openfile->current_x); + /* Move backward over all whitespace characters. */ + if (!seen_a_word && is_blank_mbchar(openfile->current->data + + openfile->current_x)) { + if (seen_space) { + step_forward = TRUE; + break; + } + seen_space = TRUE; + + do { + openfile->current_x = move_mbleft(openfile->current->data, + openfile->current_x); + } while (openfile->current_x > 0 && + is_blank_mbchar(openfile->current->data + + openfile->current_x)); + + if (openfile->current_x == 0) { + /* Move backward until we reach a non-newline character. */ + do { + if (openfile->current->prev == NULL) + break; + openfile->current = openfile->current->prev; + openfile->current_x = strlen(openfile->current->data); + } while(openfile->current_x == 0); + break; + } + } + + /* Stop at head of line. */ + if (openfile->current_x == 0) { + step_forward = is_blank_mbchar(openfile->current->data + + openfile->current_x); + break; + } + if (is_word_mbchar(openfile->current->data + openfile->current_x, allow_punct)) { seen_a_word = TRUE; @@ -309,6 +346,7 @@ void do_prev_word(bool allow_punct, bool update_screen) /* Move one character forward again to sit on the start of the word. */ openfile->current_x = move_mbright(openfile->current->data, openfile->current_x); + } if (update_screen) edit_redraw(was_current, FLOWING); @@ -328,21 +366,30 @@ bool do_next_word(bool after_ends, bool allow_punct, bool update_screen) bool seen_word = started_on_word; #endif - /* Move forward until we reach the start of a word. */ - while (TRUE) { - /* If at the end of a line, move to the beginning of the next one. */ if (openfile->current->data[openfile->current_x] == '\0') { + /* Move forward until we reach a non-newline character. */ + do { /* When at end of file, stop. */ if (openfile->current->next == NULL) break; openfile->current = openfile->current->next; openfile->current_x = 0; - seen_space = TRUE; - } else { + } while(openfile->current->data[openfile->current_x] == '\0'); + + /* Move forward over all whitespace characters. */ + while (is_blank_mbchar(openfile->current->data + openfile->current_x)) + openfile->current_x = move_mbright(openfile->current->data, + openfile->current_x); + } else { + /* Move forward until we reach the start of a word. */ + while (TRUE) { /* Step forward one character. */ openfile->current_x = move_mbright(openfile->current->data, openfile->current_x); - } + + /* Stop at end of line. */ + if (openfile->current->data[openfile->current_x] == '\0') + break; #ifndef NANO_TINY if (after_ends) { @@ -364,6 +411,7 @@ bool do_next_word(bool after_ends, bool allow_punct, bool update_screen) else if (seen_space) break; } + } } if (update_screen) -- 2.16.4