~rcr/rirc

ref: b0f118812f36ec7dbaedc131a4b7a0fbc52e0d61 rirc/src/components/input.c -rw-r--r-- 6.5 KiB
b0f11881 — Richard Robbins fix pasting of control characters to input a month ago
                                                                                
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "src/components/input.h"

#include "src/utils/utils.h"

#include <ctype.h>
#include <errno.h>
#include <string.h>

#define INPUT_MASK(X) ((X) & (INPUT_HIST_MAX - 1))

#if INPUT_MASK(INPUT_HIST_MAX)
/* Required for proper masking when indexing */
#error INPUT_HIST_MAX must be a power of 2
#endif

static char *input_text_copy(struct input*);
static int input_text_isfull(struct input*);
static int input_text_iszero(struct input*);
static uint16_t input_hist_size(struct input*);
static uint16_t input_text_size(struct input*);

void
input_init(struct input *inp)
{
	memset(inp, 0, sizeof(*inp));

	inp->tail = INPUT_LEN_MAX;
}

void
input_free(struct input *inp)
{
	free(inp->hist.save);

	while (inp->hist.tail != inp->hist.head)
		free(inp->hist.ptrs[INPUT_MASK(inp->hist.tail++)]);
}

int
input_cursor_back(struct input *inp)
{
	if (inp->head == 0)
		return 0;

	inp->text[--inp->tail] = inp->text[--inp->head];

	return 1;
}

int
input_cursor_forw(struct input *inp)
{
	if (inp->tail == INPUT_LEN_MAX)
		return 0;

	inp->text[inp->head++] = inp->text[inp->tail++];

	return 1;
}

int
input_delete_back(struct input *inp)
{
	if (inp->head == 0)
		return 0;

	inp->head--;

	return 1;
}

int
input_delete_forw(struct input *inp)
{
	if (inp->tail == INPUT_LEN_MAX)
		return 0;

	inp->tail++;

	return 1;
}

int
input_insert(struct input *inp, const char *c, size_t count)
{
	if (input_text_isfull(inp))
		return 0;

	while (!input_text_isfull(inp) && count--) {

		if (iscntrl(*c))
			inp->text[inp->head++] = ' ';

		if (isprint(*c))
			inp->text[inp->head++] = *c;

		c++;
	}

	return 1;
}

int
input_reset(struct input *inp)
{
	if (input_text_iszero(inp))
		return 0;

	free(inp->hist.save);

	inp->hist.current = inp->hist.head;
	inp->hist.save = NULL;

	inp->head = 0;
	inp->tail = INPUT_LEN_MAX;
	inp->window = 0;

	return 1;
}

int
input_complete(struct input *inp, f_completion_cb cb)
{
	/* Completion is valid when the cursor is:
	 *  - above any character in a word
	 *  - above a space immediately following a word
	 *  - at the end of a line following a word */

	uint16_t head, tail, ret;

	if (input_text_iszero(inp))
		return 0;

	head = inp->head;
	tail = inp->tail;

	while (head && inp->text[head - 1] != ' ')
		head--;

	if (inp->text[head] == ' ')
		return 0;

	while (tail < INPUT_LEN_MAX && inp->text[tail] != ' ')
		tail++;

	ret = (*cb)(
		(inp->text + head),
		(inp->head - head - inp->tail + tail),
		(INPUT_LEN_MAX - input_text_size(inp)),
		(head == 0));

	if (ret) {
		inp->head = head + ret;
		inp->tail = tail;
	}

	return (ret != 0);
}

int
input_hist_back(struct input *inp)
{
	size_t len;

	if (input_hist_size(inp) == 0 || inp->hist.current == inp->hist.tail)
		return 0;

	if (inp->hist.current == inp->hist.head) {
		inp->hist.save = input_text_copy(inp);
	} else {
		free(inp->hist.ptrs[INPUT_MASK(inp->hist.current)]);
		inp->hist.ptrs[INPUT_MASK(inp->hist.current)] = input_text_copy(inp);
	}

	inp->hist.current--;

	len = strlen(inp->hist.ptrs[INPUT_MASK(inp->hist.current)]);
	memcpy(inp->text, inp->hist.ptrs[INPUT_MASK(inp->hist.current)], len);

	inp->head = len;
	inp->tail = INPUT_LEN_MAX;

	return 1;
}

int
input_hist_forw(struct input *inp)
{
	char *str;
	size_t len = 0;

	if (input_hist_size(inp) == 0 || inp->hist.current == inp->hist.head)
		return 0;

	free(inp->hist.ptrs[INPUT_MASK(inp->hist.current)]);
	inp->hist.ptrs[INPUT_MASK(inp->hist.current)] = input_text_copy(inp);

	inp->hist.current++;

	if (inp->hist.current == inp->hist.head)
		str = inp->hist.save;
	else
		str = inp->hist.ptrs[INPUT_MASK(inp->hist.current)];

	if (str) {
		len = strlen(str);
		memcpy(inp->text, str, len);
	}

	if (inp->hist.current == inp->hist.head) {
		free(inp->hist.save);
		inp->hist.save = NULL;
	}

	inp->head = len;
	inp->tail = INPUT_LEN_MAX;

	return 1;
}

int
input_hist_push(struct input *inp)
{
	char *save;

	if ((save = input_text_copy(inp)) == NULL)
		return 0;

	if (inp->hist.current < inp->hist.head) {

		uint16_t i;

		free(inp->hist.ptrs[INPUT_MASK(inp->hist.current)]);

		for (i = inp->hist.current; i < inp->hist.head - 1; i++)
			inp->hist.ptrs[INPUT_MASK(i)] = inp->hist.ptrs[INPUT_MASK(i + 1)];

		inp->hist.current = i;
		inp->hist.ptrs[INPUT_MASK(inp->hist.current)] = save;

	} else if (input_hist_size(inp) == INPUT_HIST_MAX) {

		free(inp->hist.ptrs[INPUT_MASK(inp->hist.tail++)]);
		inp->hist.ptrs[INPUT_MASK(inp->hist.head++)] = save;

	} else {

		inp->hist.ptrs[INPUT_MASK(inp->hist.head++)] = save;
	}

	return input_reset(inp);
}

uint16_t
input_frame(struct input *inp, char *buf, uint16_t max)
{
	/*  Keep the input head in view, reframing if the cursor would be
	 *  drawn outside [A, B] as a function of the given max width
	 *
	 * 0       W             W + M
	 * |-------|---------------|------
	 * |       |A             B|
	 *         |<--         -->| : max
	 *
	 * The cursor should track the input head, where the next
	 * character would be entered
	 *
	 * In the <= A case: deletions occurred since previous reframe;
	 * the head is less than or equal to the window
	 *
	 * In the >= B case: insertions occurred since previous reframe;
	 * the distance from window to head is greater than the distance
	 * from [A, B]
	 *
	 * Set the window 2/3 of the text area width backwards from the head
	 * and returns the cursor position relative to the window */

	if (inp->window >= inp->head || (inp->window + (max - 1)) <= inp->head)
		inp->window = (((max - 1) * 2 / 3) >= inp->head) ? 0 : inp->head - ((max - 1) * 2 / 3);

	input_write(inp, buf, max, inp->window);

	return (inp->head - inp->window);
}

uint16_t
input_write(struct input *inp, char *buf, uint16_t max, uint16_t pos)
{
	uint16_t buf_len = 0;

	while (max > 1 && pos < inp->head) {
		buf[buf_len++] = inp->text[pos++];
		max--;
	}

	pos = inp->tail;

	while (max > 1 && pos < INPUT_LEN_MAX) {
		buf[buf_len++] = inp->text[pos++];
		max--;
	}

	buf[buf_len] = 0;

	return buf_len;
}

static char*
input_text_copy(struct input *inp)
{
	char *str;
	size_t len;

	if ((len = input_text_size(inp)) == 0)
		return NULL;

	if ((str = malloc(len + 1)) == NULL)
		fatal("malloc: %s", strerror(errno));

	input_write(inp, str, len + 1, 0);

	return str;
}

static int
input_text_isfull(struct input *inp)
{
	return (input_text_size(inp) == INPUT_LEN_MAX);
}

static int
input_text_iszero(struct input *inp)
{
	return (input_text_size(inp) == 0);
}

static uint16_t
input_text_size(struct input *inp)
{
	return (inp->head + (INPUT_LEN_MAX - inp->tail));
}

static uint16_t
input_hist_size(struct input *inp)
{
	return (inp->hist.head - inp->hist.tail);
}