lbuf_pair() is bugged with utf-8 and some other pair character
kyx0r opened this issue · comments
@aligrudi Hello Ali, I found a weird corner case when lbuf_pair does not work.
Consider the following text:
'☺' ) {
}
paste it, and try using % while cursor is on { character, it will not work. Oddly enough it works if you do it from } character, or
if you remove the smiley face or if you remove the ) character
Does the following patch fix the problem?
Thanks,
Ali
diff --git a/mot.c b/mot.c
index ed0586b..e9635b2 100644
--- a/mot.c
+++ b/mot.c
@@ -194,16 +194,15 @@ int lbuf_wordend(struct lbuf *lb, int big, int dir, int *row, int *off)
int lbuf_pair(struct lbuf *lb, int *row, int *off)
{
int r = *row, o = *off;
- char *ln = lbuf_get(lb, *row);
char *pairs = "()[]{}";
+ int c; /* parenthesis character */
int p; /* index for pairs[] */
int dep = 1; /* parenthesis depth */
- if (!ln || !ln[o])
+ while ((c = (unsigned char) lbuf_chr(lb, r, o)[0]) && !strchr(pairs, c))
+ o++;
+ if (!c)
return 1;
- while (!strchr(pairs, ln[o]))
- if (!ln[++o])
- return 1;
- p = strchr(pairs, ln[o]) - pairs;
+ p = strchr(pairs, c) - pairs;
while (!lbuf_next(lb, (p & 1) ? -1 : +1, &r, &o)) {
int c = (unsigned char) lbuf_chr(lb, r, o)[0];
if (c == pairs[p ^ 1])