about summary refs log tree commit diff
path: root/edit.c
blob: 47478ec6497416ef33397dcd4f8ab6c1b7150b6d (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
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
/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

#include "chat.h"

enum { Cap = 512 };
static wchar_t buf[Cap];
static size_t len;
static size_t pos;

char *editBuffer(size_t *mbsPos) {
	static char mbs[MB_LEN_MAX * Cap];

	const wchar_t *ptr = buf;
	size_t mbsLen = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs) - 1, NULL);
	assert(mbsLen != (size_t)-1);
	if (mbsPos) *mbsPos = mbsLen;

	ptr = &buf[pos];
	size_t n = wcsnrtombs(
		&mbs[mbsLen], &ptr, len - pos, sizeof(mbs) - mbsLen - 1, NULL
	);
	assert(n != (size_t)-1);
	mbsLen += n;

	mbs[mbsLen] = '\0';
	return mbs;
}

static void reserve(size_t index, size_t count) {
	if (len + count > Cap) return;
	memmove(&buf[index + count], &buf[index], sizeof(*buf) * (len - index));
	len += count;
}

static void delete(size_t index, size_t count) {
	if (index + count > len) return;
	memmove(
		&buf[index], &buf[index + count], sizeof(*buf) * (len - index - count)
	);
	len -= count;
}

static struct {
	size_t pos;
	size_t pre;
	size_t len;
} tab;

static void tabComplete(size_t id) {
	if (!tab.len) {
		tab.pos = pos;
		while (tab.pos && buf[tab.pos - 1] != L' ') tab.pos--;
		if (tab.pos == pos) return;
		tab.pre = pos - tab.pos;
		tab.len = tab.pre;
	}

	char mbs[MB_LEN_MAX * tab.pre + 1];
	const wchar_t *ptr = &buf[tab.pos];
	size_t n = wcsnrtombs(mbs, &ptr, tab.pre, sizeof(mbs) - 1, NULL);
	assert(n != (size_t)-1);
	mbs[n] = '\0';

	const char *comp = complete(id, mbs);
	if (!comp) comp = complete(id, mbs);
	if (!comp) {
		tab.len = 0;
		return;
	}

	wchar_t wcs[strlen(comp) + 1];
	n = mbstowcs(wcs, comp, sizeof(wcs));
	assert(n != (size_t)-1);
	if (tab.pos + n + 2 > Cap) {
		completeReject();
		tab.len = 0;
		return;
	}

	delete(tab.pos, tab.len);
	if (wcs[0] != L'/' && !tab.pos) {
		tab.len = n + 2;
		reserve(tab.pos, tab.len);
		buf[tab.pos + n + 0] = L':';
		buf[tab.pos + n + 1] = L' ';
	} else if (
		tab.pos >= 2 && (buf[tab.pos - 2] == L':' || buf[tab.pos - 2] == L',')
	) {
		tab.len = n + 2;
		reserve(tab.pos, tab.len);
		buf[tab.pos - 2] = L',';
		buf[tab.pos + n + 0] = L':';
		buf[tab.pos + n + 1] = L' ';
	} else {
		tab.len = n + 1;
		reserve(tab.pos, tab.len);
		buf[tab.pos + n] = L' ';
	}
	memcpy(&buf[tab.pos], wcs, sizeof(*wcs) * n);
	pos = tab.pos + tab.len;
}

static void tabAccept(void) {
	completeAccept();
	tab.len = 0;
}

static void tabReject(void) {
	completeReject();
	tab.len = 0;
}

void edit(size_t id, enum Edit op, wchar_t ch) {
	size_t init = pos;
	switch (op) {
		break; case EditHead: pos = 0;
		break; case EditTail: pos = len;
		break; case EditPrev: if (pos) pos--;
		break; case EditNext: if (pos < len) pos++;
		break; case EditPrevWord: {
			if (pos) pos--;
			while (pos && buf[pos - 1] != L' ') pos--;
		}
		break; case EditNextWord: {
			if (pos < len) pos++;
			while (pos < len && buf[pos] != L' ') pos++;
		}

		break; case EditDeleteHead: delete(0, pos); pos = 0;
		break; case EditDeleteTail: delete(pos, len - pos);
		break; case EditDeletePrev: if (pos) delete(--pos, 1);
		break; case EditDeleteNext: delete(pos, 1);
		break; case EditDeletePrevWord: {
			if (!pos) break;
			size_t word = pos - 1;
			while (word && buf[word - 1] != L' ') word--;
			delete(word, pos - word);
			pos = word;
		}
		break; case EditDeleteNextWord: {
			if (pos == len) break;
			size_t word = pos + 1;
			while (word < len && buf[word] != L' ') word++;
			delete(pos, word - pos);
		}

		break; case EditInsert: {
			reserve(pos, 1);
			if (pos < Cap) buf[pos++] = ch;
		}
		break; case EditComplete: {
			tabComplete(id);
			return;
		}
		break; case EditEnter: {
			tabAccept();
			command(id, editBuffer(NULL));
			len = pos = 0;
			return;
		}
	}

	if (pos < init) {
		tabReject();
	} else {
		tabAccept();
	}
}