From ac4570c5534fbc9499d15eb58d97fbb2048e5fc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Thu, 28 Aug 2014 16:55:48 +0200 Subject: motion: improve matching of single and double quotes --- text-motions.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/text-motions.c b/text-motions.c index e57738c..090e727 100644 --- a/text-motions.c +++ b/text-motions.c @@ -1,4 +1,5 @@ #include +#include #include "text-motions.h" #include "util.h" @@ -241,31 +242,38 @@ size_t text_bracket_match(Text *txt, size_t pos) { case ']': search = '['; direction = -1; break; case '<': search = '>'; direction = 1; break; case '>': search = '<'; direction = -1; break; - // TODO: heuristic basic on neighbouring chars shared with text-object - case '"': search = '"'; direction = 1; break; - case '\'': search = '\''; direction = 1; break; + case '"': + case '\'': { + char special[] = " \n)}]>.,"; + search = current; + direction = 1; + if (text_iterator_byte_next(&it, &c)) { + /* if a single or double quote is followed by + * a special character, search backwards */ + if (memchr(special, c, sizeof(special))) + direction = -1; + text_iterator_byte_prev(&it, NULL); + } + break; + } default: return pos; } if (direction >= 0) { /* forward search */ while (text_iterator_byte_next(&it, &c)) { - if (c == search && --count == 0) { - pos = it.pos; - break; - } else if (c == current) { + if (c == search && --count == 0) + return it.pos; + else if (c == current) count++; - } } } else { /* backwards */ while (text_iterator_byte_prev(&it, &c)) { - if (c == search && --count == 0) { - pos = it.pos; - break; - } else if (c == current) { + if (c == search && --count == 0) + return it.pos; + else if (c == current) count++; - } } } - return pos; + return pos; /* no match found */ } -- cgit v1.2.3