Commit 3a9a3299 authored by David Lawrence Ramsey's avatar David Lawrence Ramsey
Browse files

in do_find_bracket(), add comments and minor cleanups

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2905 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
parent c4037f30
Showing with 46 additions and 34 deletions
+46 -34
...@@ -171,6 +171,8 @@ CVS code - ...@@ -171,6 +171,8 @@ CVS code -
- Only include this function when DISABLE_SPELLER isn't defined, - Only include this function when DISABLE_SPELLER isn't defined,
as the alternate spell checking code is now the only place as the alternate spell checking code is now the only place
where it's used. (DLR) where it's used. (DLR)
do_find_bracket()
- Add comments and minor cleanups. (DLR)
find_history(), get_history_completion() find_history(), get_history_completion()
- Make parameters const where possible. (DLR) - Make parameters const where possible. (DLR)
- winio.c: - winio.c:
......
...@@ -1063,78 +1063,88 @@ void do_gotopos(ssize_t line, size_t pos_x, ssize_t pos_y, size_t ...@@ -1063,78 +1063,88 @@ void do_gotopos(ssize_t line, size_t pos_x, ssize_t pos_y, size_t
#ifdef HAVE_REGEX_H #ifdef HAVE_REGEX_H
void do_find_bracket(void) void do_find_bracket(void)
{ {
const char *pos, *bracket_pat = "([{<>}])"; const char *bracket_pat = "()<>[]{}", *pos;
char cursor_ch, wanted_ch, regexp_pat[] = "[ ]"; char regex_pat[5] = "[ ]", ch, wanted_ch;
filestruct *current_save;
size_t current_x_save, pww_save; size_t current_x_save, pww_save;
int count = 1;
bool regexp_set = ISSET(USE_REGEXP); bool regexp_set = ISSET(USE_REGEXP);
bool backwards_search_set = ISSET(BACKWARDS_SEARCH); bool backwards_search_set = ISSET(BACKWARDS_SEARCH);
filestruct *current_save; int count = 1;
assert(strlen(bracket_pat) % 2 == 0);
cursor_ch = openfile->current->data[openfile->current_x]; ch = openfile->current->data[openfile->current_x];
pos = strchr(bracket_pat, cursor_ch);
if (cursor_ch == '\0' || pos == NULL) { if (ch == '\0' || (pos = strchr(bracket_pat, ch)) == NULL) {
statusbar(_("Not a bracket")); statusbar(_("Not a bracket"));
return; return;
} }
assert(strlen(bracket_pat) % 2 == 0); /* Save where we are. */
wanted_ch =
bracket_pat[(strlen(bracket_pat) - 1) - (pos - bracket_pat)];
current_save = openfile->current; current_save = openfile->current;
current_x_save = openfile->current_x; current_x_save = openfile->current_x;
pww_save = openfile->placewewant; pww_save = openfile->placewewant;
/* Turn regular expression searches on. */
SET(USE_REGEXP); SET(USE_REGEXP);
/* Apparent near redundancy with regexp_pat[] here is needed. /* If we're on an opening bracket, we want to search forwards for a
* "[][]" works, "[[]]" doesn't. */ * closing bracket, and if we're on a closing bracket, we want to
if (pos < bracket_pat + (strlen(bracket_pat) / 2)) { * search backwards for an opening bracket.
/* On a left bracket. */ *
regexp_pat[1] = wanted_ch; * We replace the spaces in regex_pat with the opening and closing
regexp_pat[2] = cursor_ch; * brackets, and then do a regular expression search using it. We
* have to put the closing bracket first when we do the latter,
* since "[[]]" won't work properly, but "[][]" will. */
if ((pos - bracket_pat) % 2 == 0) {
wanted_ch = *(pos + 1);
regex_pat[1] = wanted_ch;
regex_pat[2] = ch;
UNSET(BACKWARDS_SEARCH); UNSET(BACKWARDS_SEARCH);
} else { } else {
/* On a right bracket. */ wanted_ch = *(pos - 1);
regexp_pat[1] = cursor_ch; regex_pat[1] = ch;
regexp_pat[2] = wanted_ch; regex_pat[2] = wanted_ch;
SET(BACKWARDS_SEARCH); SET(BACKWARDS_SEARCH);
} }
regexp_init(regexp_pat); /* Compile the regular expression in regex_pat. */
regexp_init(regex_pat);
/* We constructed regexp_pat to be a valid regular expression. */ /* The regular expression in regex_pat should always be valid. */
assert(regexp_compiled); assert(regexp_compiled);
findnextstr_wrap_reset(); findnextstr_wrap_reset();
while (TRUE) { while (TRUE) {
if (findnextstr(FALSE, FALSE, FALSE, openfile->current, if (findnextstr(FALSE, FALSE, FALSE, openfile->current,
openfile->current_x, regexp_pat, NULL)) { openfile->current_x, regex_pat, NULL)) {
/* Found identical bracket. */ /* If we found an identical bracket, increment count. If we
if (openfile->current->data[openfile->current_x] == * found a complementary bracket, decrement it. */
cursor_ch) count += (openfile->current->data[openfile->current_x] ==
count++; ch) ? 1 : -1;
/* Found complementary bracket. */
else if (--count == 0) { /* If count is zero, we've found a matching bracket. Update
openfile->placewewant = xplustabs(); * the screen and get out. */
if (count == 0) {
edit_redraw(current_save, pww_save); edit_redraw(current_save, pww_save);
break; break;
} }
} else { } else {
/* Didn't find either a left or right bracket. */ /* We didn't find either an opening or closing bracket.
* Indicate this, restore where we were, and get out. */
statusbar(_("No matching bracket")); statusbar(_("No matching bracket"));
openfile->current = current_save; openfile->current = current_save;
openfile->current_x = current_x_save; openfile->current_x = current_x_save;
update_line(openfile->current, openfile->current_x); openfile->placewewant = pww_save;
break; break;
} }
} }
/* Decompile the regular expression in regex_pat. */
regexp_cleanup(); regexp_cleanup();
/* Restore search direction. */ /* Restore search/replace direction. */
if (backwards_search_set) if (backwards_search_set)
SET(BACKWARDS_SEARCH); SET(BACKWARDS_SEARCH);
else else
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment