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
|
#include "history.h"
#include "util.h"
#include <string.h>
#include <wchar.h>
void
history_init(struct history *history)
{
history->list = LIST_HEAD;
history->input = inputln_alloc();
history->sel = history->input;
}
void
history_submit(struct history *history)
{
/* if chose from history free input */
if (history->sel != history->input) {
link_pop(LINK(history->input));
inputln_free(history->input);
}
/* pop first in case already in history */
link_pop(LINK(history->sel));
history_add(history, history->sel);
/* create new input buf and add to hist */
history->input = inputln_alloc();
history->sel = history->input;
history_add(history, history->sel);
}
void
history_free(struct history *history)
{
struct link *iter, *next;
struct link *ln;
ln = link_pop(LINK(history->input));
inputln_free(UPCAST(ln, struct inputln));
for (iter = history->list.next; iter; ) {
next = iter->next;
inputln_free(UPCAST(iter, struct inputln));
iter = next;
}
history->list = LIST_HEAD;
history->input = NULL;
history->sel = NULL;
}
struct inputln *
history_list_prev(struct inputln *cur, const wchar_t *search)
{
struct link *iter;
struct inputln *ln;
for (iter = cur->link.prev; iter && iter->prev; iter = iter->prev) {
ln = UPCAST(iter, struct inputln);
if (!search || !*search || wcsstr(ln->buf, search))
return ln;
}
return cur;
}
struct inputln *
history_list_next(struct inputln *cur, const wchar_t *search)
{
struct link *iter;
struct inputln *ln;
for (iter = cur->link.next; iter; iter = iter->next) {
ln = UPCAST(iter, struct inputln);
if (!search || !*search || wcsstr(ln->buf, search))
return ln;
}
return cur;
}
void
history_prev(struct history *history)
{
history->sel = history_list_prev(history->sel, history->input->buf);
}
void
history_next(struct history *history)
{
history->sel = history_list_next(history->sel, history->input->buf);
}
void
history_add(struct history *history, struct inputln *line)
{
struct inputln *ln;
struct link *back;
if (list_len(&history->list) == HISTORY_MAX) {
/* pop last item to make space */
back = link_pop(link_back(&history->list));
inputln_free(UPCAST(back, struct inputln));
}
list_push_front(&history->list, LINK(line));
}
void
inputln_init(struct inputln *ln)
{
ln->buf = NULL;
ln->len = 0;
ln->cap = 0;
ln->cur = 0;
ln->link = LINK_EMPTY;
}
struct inputln *
inputln_alloc(void)
{
struct inputln *ln;
ln = malloc(sizeof(struct inputln));
ASSERT(ln != NULL);
inputln_init(ln);
inputln_resize(ln, 128);
return ln;
}
void
inputln_free(struct inputln *ln)
{
free(ln->buf);
free(ln);
}
void
inputln_left(struct inputln *ln)
{
ln->cur = MAX(0, ln->cur-1);
}
void
inputln_right(struct inputln *ln)
{
ln->cur = MIN(ln->len, ln->cur+1);
}
void
inputln_addch(struct inputln *line, wchar_t c)
{
int i;
if (line->len + 1 >= line->cap) {
line->cap *= 2;
line->buf = realloc(line->buf,
line->cap * sizeof(wchar_t));
}
for (i = line->len; i > line->cur; i--)
line->buf[i] = line->buf[i-1];
line->buf[line->cur] = c;
line->len++;
line->cur++;
line->buf[line->len] = '\0';
}
void
inputln_del(struct inputln *line, int n)
{
int i;
if (!line->cur) return;
n = MIN(n, line->cur);
for (i = line->cur; i <= line->len; i++)
line->buf[i-n] = line->buf[i];
for (i = line->len - n; i <= line->len; i++)
line->buf[i] = 0;
line->len -= n;
line->cur -= n;
}
void
inputln_copy(struct inputln *dst, struct inputln *src)
{
if (dst->buf) {
free(dst->buf);
dst->buf = NULL;
}
dst->len = src->len;
dst->buf = wcsdup(src->buf);
ASSERT(dst->buf != NULL);
dst->cap = src->len + 1;
dst->cur = dst->len;
}
void
inputln_replace(struct inputln *line, const wchar_t *str)
{
free(line->buf);
line->buf = wcsdup(str);
ASSERT(line->buf != NULL);
line->len = wcslen(str);
line->cap = line->len + 1;
line->cur = line->len;
}
void
inputln_resize(struct inputln *ln, size_t size)
{
ASSERT(size != 0);
ln->cap = size;
ln->buf = realloc(ln->buf, ln->cap * sizeof(wchar_t));
ASSERT(ln->buf != NULL);
ln->len = MIN(ln->len, ln->cap-1);
ln->buf[ln->len] = '\0';
}
|