aboutsummaryrefslogtreecommitdiff
path: root/text-objects.c
blob: f1181ef93aaadab5403a349aa80834ebd499f427 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <ctype.h>
#include "text-motions.h"
#include "text-objects.h"

static Filerange empty = {
	.start = -1,
	.end = -1,
};

Filerange text_object_word(Text *txt, size_t pos) {
	char c, prev = '0', next = '0';
	Filerange r;
	Iterator it = text_iterator_get(txt, pos);
	if (!text_iterator_byte_get(&it, &c))
		return empty;
	if (text_iterator_byte_prev(&it, &prev))
		text_iterator_byte_next(&it, NULL);
	text_iterator_byte_next(&it, &next);
	if (isspace(c)) {
		/* we are in the middle of two words */
		r.start = text_char_next(txt, text_word_end_prev(txt, pos));
		r.end = text_word_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_word_end_next(txt, pos));
	} else if (isspace(next)) {
		/* at end of a word */
		r.start = text_word_start_prev(txt, pos);
		r.end = text_char_next(txt, pos);
	} else {
		/* in the middle of a word */
		r.start = text_word_start_prev(txt, pos);
		r.end = text_char_next(txt, text_word_end_next(txt, pos));
	}
	return r;
}

Filerange text_object_sentence(Text *txt, size_t pos) {
	Filerange r;
	r.start = text_sentence_prev(txt, pos);
	r.end = text_sentence_next(txt, pos);
	return r;
}

Filerange text_object_paragraph(Text *txt, size_t pos) {
	Filerange r;
	r.start = text_paragraph_prev(txt, pos);
	r.end = text_paragraph_next(txt, pos);
	return r;
}