File: util.c

package info (click to toggle)
aoeui 1.4-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 440 kB
  • ctags: 574
  • sloc: ansic: 6,005; makefile: 76; sh: 11
file content (195 lines) | stat: -rw-r--r-- 4,352 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Copyright 2007, 2008 Peter Klausler.  See COPYING for license. */
#include "all.h"

ssize_t view_vprintf(struct view *view, const char *msg, va_list ap)
{
	char buff[1024];
	vsnprintf(buff, sizeof buff, msg, ap);
	return view_insert(view, buff, view->bytes, -1);
}

ssize_t view_printf(struct view *view, const char *msg, ...)
{
	va_list ap;
	int result;

	va_start(ap, msg);
	result = view_vprintf(view, msg, ap);
	va_end(ap);
	return result;
}

size_t view_get_selection(struct view *view, position_t *offset,
			  Boolean_t *append)
{
	position_t cursor = locus_get(view, CURSOR);
	position_t mark = locus_get(view, MARK);

	if (mark == UNSET)
		mark = cursor + (cursor < view->bytes);
	if (append)
		*append = cursor >= mark;
	if (mark <= cursor)
		return cursor - (*offset = mark);
	return mark - (*offset = cursor);
}

char *view_extract(struct view *view, position_t offset, unsigned bytes)
{
	char *str;

	if (!view)
		return NULL;
	if (offset > view->bytes)
		return NULL;
	if (offset + bytes > view->bytes)
		bytes = view->bytes - offset;
	if (!bytes)
		return NULL;
	str = allocate(bytes+1);
	str[view_get(view, str, offset, bytes)] = '\0';
	return str;
}

char *view_extract_selection(struct view *view)
{
	position_t offset;
	size_t bytes = view_get_selection(view, &offset, NULL);
	return view_extract(view, offset, bytes);
}

size_t view_delete_selection(struct view *view)
{
	position_t offset;
	size_t bytes = view_get_selection(view, &offset, NULL);
	view_delete(view, offset, bytes);
	locus_set(view, MARK, UNSET);
	return bytes;
}

struct view *view_next(struct view *view)
{
	struct view *new = view;
	do {
		if (new->next)
			new = new->next;
		else if (new->text->next)
			new = new->text->next->views;
		else
			new = text_list->views;
	} while (new != view && new->window);
	return new == view ? text_new() : new;
}

Unicode_t view_unicode(struct view *view, position_t offset, position_t *next)
{
	Unicode_t ch = view_byte(view, offset);
	char *raw;
	size_t length;

	if (!IS_UNICODE(ch) ||
	    ch < 0x80 ||
	    view->text->flags & TEXT_NO_UTF8) {
		if (ch == '\r' &&
		    view->text->flags & TEXT_CRNL &&
		    view_byte(view, offset + 1) == '\n') {
			if (next)
				*next = offset + 2;
			return '\n';
		}
		if (next)
			*next = offset + IS_UNICODE(ch);
		return ch;
	}

	length = view_raw(view, &raw, offset, 8);
	length = utf8_length(raw, length);
	if (next)
		*next = offset + length;
	return utf8_unicode(raw, length);
}

Unicode_t view_unicode_prior(struct view *view, position_t offset,
			     position_t *prev)
{
	Unicode_t ch = UNICODE_BAD;
	char *raw;

	if (offset) {
		ch = view_byte(view, --offset);
		if (IS_UNICODE(ch) &&
		    ch >= 0x80 &&
		    !(view->text->flags & TEXT_NO_UTF8)) {
			unsigned at = offset >= 7 ? offset-7 : 0;
			view_raw(view, &raw, at, offset-at+1);
			offset -= utf8_length_backwards(raw+offset-at,
					offset-at+1) - 1;
			ch = view_unicode(view, offset, NULL);
		} else if (ch == '\n' &&
			   view->text->flags & TEXT_CRNL &&
			   offset &&
			   view_byte(view, offset-1) == '\r')
			offset--;
	}
	if (prev)
		*prev = offset;
	return ch;
}

Unicode_t view_char(struct view *view, position_t offset, position_t *next)
{
	position_t next0;
	Unicode_t ch = view_unicode(view, offset, &next0);

	if (!next)
		return ch;
	*next = next0;
	if (IS_FOLDED(ch)) {
		size_t fbytes = FOLDED_BYTES(ch);
		position_t next2;
		if (view_unicode(view, next0 + fbytes, &next2) ==
		    FOLD_END + fbytes)
			*next = next2;
	}
	return ch;
}

Unicode_t view_char_prior(struct view *view, position_t offset,
			  position_t *prev)
{
	Unicode_t ch = view_unicode_prior(view, offset, &offset), ch0;
	size_t fbytes;
	position_t offset0;

	if (ch >= FOLD_END &&
	    (fbytes = FOLDED_BYTES(ch)) <= offset &&
	    IS_FOLDED(ch0 = view_unicode_prior(view, offset - fbytes,
					       &offset0)) &&
	    FOLDED_BYTES(ch0) == fbytes) {
		ch = ch0;
		offset = offset0;
	}
	if (prev)
		*prev = offset;
	return ch;
}

Boolean_t is_open_bracket(const char *brackets, Unicode_t ch)
{
	if (ch >= 0x80)
		return FALSE;
	for (; *brackets; brackets += 2)
		if (*brackets == ch)
			return TRUE;
	return FALSE;
}

Boolean_t is_close_bracket(const char *brackets, Unicode_t ch)
{
	if (ch >= 0x80)
		return FALSE;
	for (brackets++; *brackets; brackets += 2)
		if (*brackets == ch)
			return TRUE;
	return FALSE;
}