diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-02-15 16:05:18 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-02-15 18:21:51 +0100 |
| commit | 44c8e46bc0a94c82dbbf2f13e2897c951f1e3b1a (patch) | |
| tree | ed9bf0f74c218526cf37754fd98e84c2358d76c6 /text-objects.c | |
| parent | c7ab22b9b1b191596e6a6fed3437717d47e1ce72 (diff) | |
| download | vis-44c8e46bc0a94c82dbbf2f13e2897c951f1e3b1a.tar.gz vis-44c8e46bc0a94c82dbbf2f13e2897c951f1e3b1a.tar.xz | |
Add text object to cover lines with same indentation level
By default it is mapped to i<Tab> and a<Tab> however there
is currently no difference between the inner and regular
version.
Diffstat (limited to 'text-objects.c')
| -rw-r--r-- | text-objects.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/text-objects.c b/text-objects.c index d1abecb..db7ff63 100644 --- a/text-objects.c +++ b/text-objects.c @@ -359,6 +359,62 @@ Filerange text_object_search_backward(Text *txt, size_t pos, Regex *regex) { return text_range_empty(); } +Filerange text_object_indentation(Text *txt, size_t pos) { + char c; + size_t bol = text_line_begin(txt, pos); + size_t sol = text_line_start(txt, bol); + size_t start = bol; + size_t end = text_line_next(txt, bol); + size_t line_indent = sol - bol; + bool line_empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n'); + + char *buf = text_bytes_alloc0(txt, bol, line_indent); + char *tmp = malloc(line_indent); + + if (!buf || !tmp) { + free(buf); + free(tmp); + return text_range_empty(); + } + + while ((bol = text_line_begin(txt, text_line_prev(txt, start))) != start) { + sol = text_line_start(txt, bol); + size_t indent = sol - bol; + if (indent < line_indent) + break; + bool empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n'); + if (line_empty && !empty) + break; + if (line_indent == 0 && empty) + break; + text_bytes_get(txt, bol, line_indent, tmp); + if (memcmp(buf, tmp, line_indent)) + break; + start = bol; + } + + do { + bol = end; + sol = text_line_start(txt, bol); + size_t indent = sol - bol; + if (indent < line_indent) + break; + bool empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n'); + if (line_empty && !empty) + break; + if (line_indent == 0 && empty) + break; + text_bytes_get(txt, bol, line_indent, tmp); + if (memcmp(buf, tmp, line_indent)) + break; + end = text_line_next(txt, bol); + } while (bol != end); + + free(buf); + free(tmp); + return text_range_new(start, end); +} + Filerange text_range_linewise(Text *txt, Filerange *rin) { Filerange rout = *rin; rout.start = text_line_begin(txt, rin->start); |
