Menu

[76e07a]: / joe / uisrch.c  Maximize  Restore  History

Download this file

314 lines (275 with data), 7.3 kB

  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
/*
* Incremental search
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
typedef struct irec IREC;
struct irec {
LINK(IREC) link;
ptrdiff_t what; /* 0 repeat, >0 append n chars */
off_t start; /* Cursor search position */
off_t disp; /* Original cursor position */
int wrap_flag; /* Wrap flag */
};
struct isrch {
IREC irecs; /* Linked list of positions */
char *pattern; /* Search pattern string */
char *prompt; /* Prompt (usually same as pattern unless utf-8/byte conversion) */
ptrdiff_t ofst; /* Offset in pattern past prompt */
int dir; /* 0=fwrd, 1=bkwd */
int quote; /* Set to quote next char */
};
struct isrch *lastisrch = NULL; /* Previous search */
char *lastpat = NULL; /* Previous pattern */
IREC fri = { {&fri, &fri} }; /* Free-list of irecs */
static IREC *alirec(void)
{ /* Allocate an IREC */
return (IREC *)alitem(&fri, SIZEOF(IREC));
}
static void frirec(IREC *i)
{ /* Free an IREC */
enquef(IREC, link, &fri, i);
}
static void rmisrch(struct isrch *isrch)
{ /* Eliminate a struct isrch */
if (isrch) {
vsrm(isrch->pattern);
vsrm(isrch->prompt);
frchn(&fri, &isrch->irecs);
joe_free(isrch);
}
}
static int iabrt(W *w, void *obj)
{ /* User hit ^C */
struct isrch *isrch = (struct isrch *)obj;
rmisrch(isrch);
return -1;
}
static void iappend(BW *bw, struct isrch *isrch, char *s, ptrdiff_t len)
{ /* Append text and search */
/* Append char and search */
IREC *i = alirec();
SRCH *srch;
i->what = len;
i->disp = bw->cursor->byte;
isrch->pattern = vsncpy(sv(isrch->pattern), s, len);
if (!qempty(IREC, link, &isrch->irecs)) {
pgoto(bw->cursor, isrch->irecs.link.prev->start);
if (globalsrch)
globalsrch->wrap_flag = isrch->irecs.link.prev->wrap_flag;
}
i->start = bw->cursor->byte;
if (!globalsrch)
srch = mksrch(NULL,NULL,opt_icase,isrch->dir,-1,0,0,0,0);
else {
srch = globalsrch;
globalsrch = 0;
}
srch->addr = bw->cursor->byte;
if (!srch->wrap_p || srch->wrap_p->b!=bw->b) {
prm(srch->wrap_p);
srch->wrap_p = pdup(bw->cursor, "iappend");
srch->wrap_p->owner = &srch->wrap_p;
srch->wrap_flag = 0;
}
i->wrap_flag = srch->wrap_flag;
setpat(srch, vsncpy(NULL, 0, isrch->pattern, sLen(isrch->pattern)));
srch->backwards = isrch->dir;
if (dopfnext(bw, srch, NULL)) {
if(joe_beep)
ttputc(7);
}
enqueb(IREC, link, &isrch->irecs, i);
}
/* Main user interface */
/* When called with c==-1, it just creates the prompt */
static int itype(W *w, int c, void *obj, int *notify)
{
IREC *i;
int omid;
BW *bw;
struct isrch *isrch = (struct isrch *)obj;
WIND_BW(bw,w);
if (isrch->quote) {
goto in;
}
if (c == 8 || c == 127) { /* Backup */
if ((i = isrch->irecs.link.prev) != &isrch->irecs) {
pgoto(bw->cursor, i->disp);
if (globalsrch)
globalsrch->wrap_flag = i->wrap_flag;
omid = opt_mid;
opt_mid = 1;
dofollows();
opt_mid = omid;
isrch->pattern = vstrunc(isrch->pattern, sLEN(isrch->pattern) - i->what);
frirec(deque_f(IREC, link, i));
} else {
if(joe_beep)
ttputc(7);
}
} else if (c == 'Q' - '@' /* || c == '`' */) {
isrch->quote = 1;
} else if (c == 'S' - '@' || c == '\\' - '@' || c == 'L' - '@' || c == 'R' - '@') {
/* Repeat */
if (c == 'R' - '@') {
isrch->dir = 1;
} else {
isrch->dir = 0;
}
if (qempty(IREC, link, &isrch->irecs)) {
if (lastpat && lastpat[0]) {
iappend(bw, isrch, sv(lastpat));
}
} else {
SRCH *srch;
i = alirec();
i->disp = i->start = bw->cursor->byte;
i->what = 0;
if (!globalsrch)
srch = mksrch(NULL,NULL,opt_icase,isrch->dir,-1,0,0,0,0);
else {
srch = globalsrch;
globalsrch = 0;
}
srch->addr = bw->cursor->byte;
if (!srch->wrap_p || srch->wrap_p->b!=bw->b) {
prm(srch->wrap_p);
srch->wrap_p = pdup(bw->cursor, "itype");
srch->wrap_p->owner = &srch->wrap_p;
srch->wrap_flag = 0;
}
i->wrap_flag = srch->wrap_flag;
setpat(srch, vsncpy(NULL, 0, isrch->pattern, sLen(isrch->pattern)));
srch->backwards = isrch->dir;
if (dopfnext(bw, srch, NULL)) {
if(joe_beep)
ttputc(7);
frirec(i);
} else {
enqueb(IREC, link, &isrch->irecs, i);
}
}
} else if (c >= 0 && c < 32) {
/* Done when a control character is received */
nungetc(c);
if (notify) {
*notify = 1;
}
smode = 2;
if (lastisrch) {
lastpat = vstrunc(lastpat, 0);
lastpat = vsncpy(lastpat, 0, lastisrch->pattern, sLen(lastisrch->pattern));
rmisrch(lastisrch);
}
lastisrch = isrch;
return 0;
} else if (c != -1) {
char buf[16];
ptrdiff_t buf_len;
/* Search */
in:
if (bw->b->o.charmap->type) {
buf_len = utf8_encode(buf, c);
} else {
buf[0] = TO_CHAR_OK(from_uni(bw->b->o.charmap, c));
buf_len = 1;
}
isrch->quote = 0;
iappend(bw, isrch, buf, buf_len);
}
omid = opt_mid;
opt_mid = 1;
bw->cursor->xcol = piscol(bw->cursor);
dofollows();
opt_mid = omid;
isrch->prompt = vstrunc(isrch->prompt, isrch->ofst);
if (locale_map->type && !bw->b->o.charmap->type) {
/* Translate bytes to utf-8 */
char buf[16];
int x;
for (x=0; x!=sLEN(isrch->pattern); ++x) {
int tc = to_uni(bw->b->o.charmap, isrch->pattern[x]);
utf8_encode(buf, tc);
isrch->prompt = vsncpy(sv(isrch->prompt),sz(buf));
}
} else if (!locale_map->type && bw->b->o.charmap->type) {
/* Translate utf-8 to bytes */
const char *p = isrch->pattern;
ptrdiff_t len = sLEN(isrch->pattern);
while (len) {
int tc = utf8_decode_fwrd(&p, &len);
if (tc >= 0) {
tc = from_uni(locale_map, tc);
isrch->prompt = vsadd(isrch->prompt, TO_CHAR_OK(tc));
}
}
} else {
/* FIXME: translate when charmaps do not match */
isrch->prompt = vsncpy(sv(isrch->prompt),sv(isrch->pattern));
}
if (mkqwnsr(bw->parent, sv(isrch->prompt), itype, iabrt, isrch, notify)) {
return 0;
} else {
rmisrch(isrch);
return -1;
}
}
static int doisrch(BW *bw, int dir)
{ /* Create a struct isrch */
struct isrch *isrch = (struct isrch *) joe_malloc(SIZEOF(struct isrch));
izque(IREC, link, &isrch->irecs);
isrch->pattern = vsncpy(NULL, 0, NULL, 0);
isrch->dir = dir;
isrch->quote = 0;
isrch->prompt = vsncpy(NULL, 0, sz(joe_gettext(_("I-find: "))));
isrch->ofst = sLen(isrch->prompt);
return itype(bw->parent, -1, isrch, NULL);
}
int uisrch(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (smode && lastisrch) {
struct isrch *isrch = lastisrch;
lastisrch = 0;
return itype(bw->parent, 'S' - '@', isrch, NULL);
} else {
if (globalsrch) {
rmsrch(globalsrch);
globalsrch = 0;
}
if (lastisrch) {
lastpat = vstrunc(lastpat, 0);
lastpat = vsncpy(lastpat, 0, lastisrch->pattern, sLen(lastisrch->pattern));
rmisrch(lastisrch);
lastisrch = 0;
}
return doisrch(bw, 0);
}
}
int ursrch(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (smode && lastisrch) {
struct isrch *isrch = lastisrch;
lastisrch = 0;
return itype(bw->parent, 'R' - '@', isrch, NULL);
} else {
if (globalsrch) {
rmsrch(globalsrch);
globalsrch = 0;
}
if (lastisrch) {
lastpat = vstrunc(lastpat, 0);
lastpat = vsncpy(lastpat, 0, lastisrch->pattern, sLen(lastisrch->pattern));
rmisrch(lastisrch);
lastisrch = 0;
}
return doisrch(bw, 1);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.