From: David Lawrence Ramsey Date: Wed, 16 Nov 2005 13:45:41 +0000 (+0000) Subject: refactor the statusbar prompt's bracket searching code to be closer to X-Git-Tag: v1.3.10~52 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=5ab56eaa8d54ac8507fab7f8f0680a7e2bfa66a0;p=nano.git refactor the statusbar prompt's bracket searching code to be closer to the edit window's bracket searching code git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3194 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index 73f43c20..a99d372b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -106,8 +106,9 @@ CVS code - and find_bracket_match(); changes to shortcut_init() and do_find_bracket(). (DLR) - Add the ability to do bracket searches at the statusbar - prompt. New function do_statusbar_find_bracket(); changes to - do_statusbar_input(). (DLR) + prompt. New functions find_statusbar_bracket_match() and + do_statusbar_find_bracket(); changes to do_statusbar_input(). + (DLR) - chars.c: mbwidth() - If wcwidth() returns -1 for the character passed in, treat the diff --git a/src/prompt.c b/src/prompt.c index fe3fff4c..49a9e87a 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -644,10 +644,46 @@ void do_statusbar_verbatim_input(bool *got_enter) } #ifndef NANO_TINY +/* Search for a match to one of the two characters in bracket_set. If + * reverse is TRUE, search backwards. Otherwise, search forwards. */ +bool find_statusbar_bracket_match(bool reverse, const char + *bracket_set) +{ + const char *rev_start = NULL, *found = NULL; + + assert(strlen(bracket_set) == 2); + + /* rev_start might end up 1 character before the start or after the + * end of the line. This won't be a problem because we'll skip over + * it below in that case. */ + rev_start = reverse ? answer + (statusbar_x - 1) : answer + + (statusbar_x + 1); + + while (TRUE) { + /* Look for either of the two characters in bracket_set. + * rev_start can be 1 character before the start or after the + * end of the line. In either case, just act as though no match + * is found. */ + found = ((rev_start > answer && *(rev_start - 1) == '\0') || + rev_start < answer) ? NULL : (reverse ? + revstrpbrk(answer, bracket_set, rev_start) : + strpbrk(rev_start, bracket_set)); + + /* We've found a potential match. */ + if (found != NULL) + break; + } + + /* We've definitely found something. */ + statusbar_x = found - answer; + statusbar_pww = statusbar_xplustabs(); + + return TRUE; +} + void do_statusbar_find_bracket(void) { size_t statusbar_x_save, pww_save; - const char *rev_start = NULL, *found = NULL; const char *bracket_list = "()<>[]{}"; /* The list of brackets we can find matches to. */ const char *pos; @@ -686,26 +722,7 @@ void do_statusbar_find_bracket(void) bracket_set[2] = '\0'; while (TRUE) { - /* rev_start might end up 1 character before the start or after - * the end of the line. This won't be a problem because we'll - * skip over it below in that case. */ - rev_start = reverse ? answer + (statusbar_x - 1) : answer + - (statusbar_x + 1); - - /* Look for either of the two characters in bracket_set. - * rev_start can be 1 character before the start or after the - * end of the line. In either case, just act as though no match - * is found. */ - found = ((rev_start > answer && *(rev_start - 1) == '\0') || - rev_start < answer) ? NULL : (reverse ? - revstrpbrk(answer, bracket_set, rev_start) : - strpbrk(rev_start, bracket_set)); - - if (found != NULL) { - /* We've definitely found something. */ - statusbar_x = found - answer; - statusbar_pww = statusbar_xplustabs(); - + if (find_statusbar_bracket_match(reverse, bracket_set)) { /* If we found an identical bracket, increment count. If we * found a complementary bracket, decrement it. */ count += (answer[statusbar_x] == ch) ? 1 : -1; diff --git a/src/proto.h b/src/proto.h index 1db1af71..74ab7e13 100644 --- a/src/proto.h +++ b/src/proto.h @@ -446,6 +446,8 @@ bool do_statusbar_prev_word(bool allow_punct); #endif void do_statusbar_verbatim_input(bool *got_enter); #ifndef NANO_TINY +bool find_statusbar_bracket_match(bool reverse, const char + *bracket_set); void do_statusbar_find_bracket(void); #endif size_t statusbar_xplustabs(void);