aboutsummaryrefslogtreecommitdiff
path: root/text-objects.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-12-23 17:21:00 +0100
committerMarc André Tanner <mat@brain-dump.org>2014-12-23 17:21:00 +0100
commit985a828346a5d3185f6ee1e00d2ab4154024a9c8 (patch)
tree255db2a1dae37efaebc4f039efa60e7784deee05 /text-objects.c
parent3314a2843f3c940e6fb96d8612f23172832f3804 (diff)
downloadvis-985a828346a5d3185f6ee1e00d2ab4154024a9c8.tar.gz
vis-985a828346a5d3185f6ee1e00d2ab4154024a9c8.tar.xz
Distinct between inner and outer word text objects
Diffstat (limited to 'text-objects.c')
-rw-r--r--text-objects.c84
1 files changed, 78 insertions, 6 deletions
diff --git a/text-objects.c b/text-objects.c
index a8aef0a..44515e1 100644
--- a/text-objects.c
+++ b/text-objects.c
@@ -18,6 +18,10 @@
#include "text-objects.h"
#include "util.h"
+#define isboundry is_word_boundry
+
+/* TODO: reduce code duplication? */
+
Filerange text_object_longword(Text *txt, size_t pos) {
Filerange r;
char c, prev = '0', next = '0';
@@ -28,6 +32,39 @@ Filerange text_object_longword(Text *txt, size_t pos) {
text_iterator_byte_next(&it, NULL);
text_iterator_byte_next(&it, &next);
if (isspace(c)) {
+ /* middle of two words */
+ r.start = text_char_next(txt, text_longword_end_prev(txt, pos));
+ r.end = text_longword_start_next(txt, pos);
+ } else if (isspace(prev) && isspace(next)) {
+ /* on a single character */
+ r.start = pos;
+ r.end = text_char_next(txt, pos);
+ } else if (isspace(prev)) {
+ /* at start of a word */
+ r.start = pos;
+ r.end = text_char_next(txt, text_longword_end_next(txt, pos));
+ } else if (isspace(next)) {
+ /* at end of a word */
+ r.start = text_longword_start_prev(txt, pos);
+ r.end = text_char_next(txt, pos);
+ } else {
+ /* in the middle of a word */
+ r.start = text_longword_start_prev(txt, pos);
+ r.end = text_char_next(txt, text_longword_end_next(txt, pos));
+ }
+ return r;
+}
+
+Filerange text_object_longword_outer(Text *txt, size_t pos) {
+ Filerange r;
+ char c, prev = '0', next = '0';
+ Iterator it = text_iterator_get(txt, pos);
+ if (!text_iterator_byte_get(&it, &c))
+ return text_range_empty();
+ if (text_iterator_byte_prev(&it, &prev))
+ text_iterator_byte_next(&it, NULL);
+ text_iterator_byte_next(&it, &next);
+ if (isspace(c)) {
/* middle of two words, include leading white space */
r.start = text_char_next(txt, text_longword_end_prev(txt, pos));
r.end = text_char_next(txt, text_longword_end_next(txt, pos));
@@ -52,7 +89,6 @@ Filerange text_object_longword(Text *txt, size_t pos) {
}
Filerange text_object_word(Text *txt, size_t pos) {
- #define isboundry is_word_boundry
Filerange r;
char c, prev = '0', next = '0';
Iterator it = text_iterator_get(txt, pos);
@@ -62,14 +98,11 @@ Filerange text_object_word(Text *txt, size_t pos) {
text_iterator_byte_next(&it, NULL);
text_iterator_byte_next(&it, &next);
if (isspace(c)) {
- /* middle of two words, include leading white space */
r.start = text_char_next(txt, text_word_end_prev(txt, pos));
- r.end = text_word_end_next(txt, pos);
- if (!text_byte_get(txt, r.end, &c) && !isboundry(c))
- r.end = text_char_next(txt, r.end);
+ r.end = text_word_start_next(txt, pos);
} else if (isboundry(prev) && isboundry(next)) {
if (isboundry(c)) {
- r.start = text_word_end_prev(txt, pos);
+ r.start = text_char_next(txt, text_word_end_prev(txt, pos));
r.end = text_char_next(txt, text_word_end_next(txt, pos));
} else {
/* on a single character */
@@ -93,6 +126,45 @@ Filerange text_object_word(Text *txt, size_t pos) {
return r;
}
+Filerange text_object_word_outer(Text *txt, size_t pos) {
+ Filerange r;
+ char c, prev = '0', next = '0';
+ Iterator it = text_iterator_get(txt, pos);
+ if (!text_iterator_byte_get(&it, &c))
+ return text_range_empty();
+ if (text_iterator_byte_prev(&it, &prev))
+ text_iterator_byte_next(&it, NULL);
+ text_iterator_byte_next(&it, &next);
+ if (isspace(c)) {
+ /* middle of two words, include leading white space */
+ r.start = text_char_next(txt, text_word_end_prev(txt, pos));
+ r.end = text_word_end_next(txt, pos);
+ } else if (isboundry(prev) && isboundry(next)) {
+ if (isboundry(c)) {
+ r.start = text_char_next(txt, text_word_end_prev(txt, pos));
+ r.end = text_word_start_next(txt, text_word_end_next(txt, pos));
+ } else {
+ /* on a single character */
+ r.start = pos;
+ r.end = text_char_next(txt, pos);
+ }
+ } else if (isboundry(prev)) {
+ /* at start of a word */
+ r.start = pos;
+ r.end = text_word_start_next(txt, text_word_end_next(txt, pos));
+ } else if (isboundry(next)) {
+ /* at end of a word */
+ r.start = text_word_start_prev(txt, pos);
+ r.end = text_word_start_next(txt, pos);
+ } else {
+ /* in the middle of a word */
+ r.start = text_word_start_prev(txt, pos);
+ r.end = text_word_start_next(txt, text_word_end_next(txt, pos));
+ }
+
+ return r;
+}
+
Filerange text_object_line(Text *txt, size_t pos) {
Filerange r;
r.start = text_line_begin(txt, pos);