[523079]: joe / pw.c Maximize Restore History

Download this file

pw.c    468 lines (405 with data), 9.8 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Prompt windows
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
/* The current directory */
int bg_prompt;
int nocurdir;
unsigned char *current_dir;
void set_current_dir(unsigned char *s,int simp)
{
if (s[0]=='!' || (s[0]=='>' && s[1]=='>'))
return;
obj_free(current_dir);
if (s) {
current_dir=dirprt(s);
if (simp) {
current_dir = simplify_prefix(current_dir);
}
}
else
current_dir = 0;
obj_perm(current_dir);
}
static void disppw(BW *bw, int flg)
{
unsigned char *bf;
int i;
W *w = bw->parent;
PW *pw = (PW *) bw->object;
if (!flg) {
return;
}
/* Try a nice graphics separator */
bf = joe_malloc(w->w + 1);
for (i = 0; i != w->w; ++i)
bf[i] = '-';
bf[i] = 0;
genfmt(w->t->t, w->x, w->y, 0, bf, bg_stalin, 0);
joe_free(bf);
/* Scroll buffer and position prompt */
if (pw->promptlen > w->w - 5) {
pw->promptofst = pw->promptlen - w->w / 2;
if (piscol(bw->cursor) < w->w - (pw->promptlen - pw->promptofst)) {
bw->offset = 0;
} else {
bw->offset = piscol(bw->cursor) - (w->w - (pw->promptlen - pw->promptofst) - 1);
}
} else {
if (piscol(bw->cursor) < w->w - pw->promptlen) {
pw->promptofst = 0;
bw->offset = 0;
} else if (piscol(bw->cursor) >= w->w) {
pw->promptofst = pw->promptlen;
bw->offset = piscol(bw->cursor) - (w->w - 1);
} else {
pw->promptofst = pw->promptlen - (w->w - piscol(bw->cursor) - 1);
bw->offset = piscol(bw->cursor) - (w->w - (pw->promptlen - pw->promptofst) - 1);
}
}
/* Set cursor position */
w->curx = piscol(bw->cursor) - bw->offset + pw->promptlen - pw->promptofst;
w->cury = bw->cursor->line - bw->top->line + 1;
/* w->cury = w->h - 1; */
/* Generate prompt */
w->t->t->updtab[w->y + w->cury] = 1;
if (w->cury != pw->oldcury) {
int n;
for (n = 0; n != w->h; ++n)
w->t->t->updtab[w->y + n] = 1;
pw->oldcury = w->cury;
}
/* w->t->t->updtab[w->y + w->h - 1] = 1;
genfmt(w->t->t, w->x, w->y + w->h - 1, pw->promptofst, pw->prompt, BG_COLOR(bg_prompt), 0); */
/* Position and size buffer */
/* bwmove(bw, w->x + pw->promptlen - pw->promptofst, w->y);
bwresz(bw, w->w - (pw->promptlen - pw->promptofst), w->h); */
bwmove(bw, w->x, w->y + 1);
bwresz(bw, w->w, w->h - 1);
/* Generate buffer */
bwgen(bw, 0);
}
/* History functions */
void setup_history(B **history)
{
if (!*history) {
*history = bmk(NULL);
}
}
/* Add line to history buffer */
void append_history(B *hist,unsigned char *s,int len)
{
P *q = pdup(hist->eof, USTR "append_history");
binsm(q, s, len);
p_goto_eof(q);
binsc(q, '\n');
prm(q);
}
/* Promote line to end of history buffer */
void promote_history(B *hist, long line)
{
P *q = pdup(hist->bof, USTR "promote_history");
P *r;
P *t;
pline(q, line);
r = pdup(q, USTR "promote_history");
pnextl(r);
t = pdup(hist->eof, USTR "promote_history");
binsb(t, bcpy(q, r));
bdel(q, r);
prm(q);
prm(r);
prm(t);
}
/* When user hits return in a prompt window */
static int rtnpw(BW *bw)
{
W *w = bw->parent;
PW *pw = (PW *) bw->object;
unsigned char *s;
W *win;
int (*pfunc) ();
void *object;
long byte;
/* Extract entered text from buffer */
p_goto_eol(bw->cursor);
byte = bw->cursor->byte;
p_goto_bol(bw->cursor);
s = brvs(NULL, bw->cursor, (int) (byte - bw->cursor->byte));
/* Save text into history buffer */
if (pw->hist) {
if (bw->b->changed) {
append_history(pw->hist, sv(s));
} else {
promote_history(pw->hist, bw->cursor->line);
}
}
/* Do ~ expansion and set new current directory */
if (pw->file_prompt&2) {
set_current_dir(s,1);
}
if (pw->file_prompt) {
s = canonical(s);
}
win = w->win;
pfunc = pw->pfunc;
object = pw->object;
bwrm(bw);
joe_free(pw->prompt);
joe_free(pw);
w->object = NULL;
wabort(w);
dostaupd = 1;
/* Call callback function */
if (pfunc) {
return pfunc(win->object, s, object);
} else {
return -1;
}
}
int ucmplt(BW *bw, int k)
{
PW *pw = (PW *) bw->object;
if (pw->tab) {
return pw->tab(bw, k);
} else {
return -1;
}
}
static void inspw(BW *bw, B *b, long l, long n, int flg)
{
if (b == bw->b) {
bwins(bw, l, n, flg);
}
}
static void delpw(BW *bw, B *b, long l, long n, int flg)
{
if (b == bw->b) {
bwdel(bw, l, n, flg);
}
}
static int abortpw(BW *b)
{
PW *pw = b->object;
void *object = pw->object;
int (*abrt) () = pw->abrt;
W *win = b->parent->win;
bwrm(b);
joe_free(pw->prompt);
joe_free(pw);
if (abrt) {
return abrt(win->object, object);
} else {
return -1;
}
}
WATOM watompw = {
USTR "prompt",
disppw,
bwfllwt,
abortpw,
rtnpw,
utypebw,
NULL,
NULL,
inspw,
delpw,
TYPEPW
};
/* Create a prompt window */
BW *wmkpw(W *w, unsigned char *prompt, B **history, int (*func) (), unsigned char *huh, int (*abrt) (), int (*tab) (), void *object, struct charmap *map,int file_prompt)
{
W *new;
PW *pw;
BW *bw;
new = wcreate(w->t, &watompw, w, w, w->main, 2, huh);
if (!new) {
return NULL;
}
new->fixed = 0;
wfit(new->t);
new->object = (void *) (bw = bwmk(new, bmk(NULL), 0, prompt));
bw->b->o.charmap = map;
bw->object = (void *) (pw = (PW *) joe_malloc(sizeof(PW)));
pw->abrt = abrt;
pw->oldcury = -1;
pw->tab = tab;
pw->object = object;
pw->prompt = zdup(prompt);
pw->promptlen = fmtlen(prompt);
pw->promptofst = 0;
pw->pfunc = func;
pw->file_prompt = file_prompt;
if (history) {
setup_history(history);
pw->hist = *history;
binsb(bw->cursor, bcpy(pw->hist->bof, pw->hist->eof));
bw->b->changed = 0;
p_goto_eof(bw->cursor);
/* p_goto_eof(bw->top);
p_goto_bol(bw->top); */
} else {
pw->hist = NULL;
}
/* Install current directory */
if ((file_prompt&4) && !nocurdir) {
binsm (bw->cursor, sv(current_dir));
p_goto_eof(bw->cursor);
bw->cursor->xcol = piscol(bw->cursor);
}
w->t->curwin = new;
return bw;
}
/* Tab completion functions */
unsigned char **regsub(unsigned char **z, int len, unsigned char *s)
{
unsigned char **lst = NULL;
int x;
for (x = 0; x != len; ++x)
if (rmatch(s, z[x]))
lst = vaadd(lst, vsncpy(NULL, 0, sz(z[x])));
return lst;
}
void cmplt_ins(BW *bw, unsigned char *line)
{
P *p = pdup(bw->cursor, USTR "cmplt_ins");
p_goto_bol(p);
p_goto_eol(bw->cursor);
bdel(p, bw->cursor);
binsm(bw->cursor, sv(line));
p_goto_eol(bw->cursor);
prm(p);
bw->cursor->xcol = piscol(bw->cursor);
}
int cmplt_abrt(BW *bw, int x, unsigned char *line)
{
if (line) {
/* cmplt_ins(bw, line); */
obj_free(line);
}
return -1;
}
int cmplt_rtn(MENU *m, int x, unsigned char *line)
{
cmplt_ins(m->parent->win->object, m->list[x]);
obj_free(line);
m->object = NULL;
wabort(m->parent);
return 0;
}
int simple_cmplt(BW *bw,unsigned char **list)
{
MENU *m;
P *p, *q;
unsigned char *line;
unsigned char *line1;
unsigned char **lst;
p = pdup(bw->cursor, USTR "simple_cmplt");
p_goto_bol(p);
q = pdup(bw->cursor, USTR "simple_cmplt");
p_goto_eol(q);
line = brvs(NULL, p, (int) (q->byte - p->byte)); /* Assumes short lines :-) */
prm(p);
prm(q);
line1 = vsncpy(NULL, 0, sv(line));
line1 = vsadd(line1, '*');
lst = regsub(av(list), line1);
if (!lst) {
ttputc(7);
return -1;
}
if (menu_above) {
if (bw->parent->link.prev->watom==&watommenu) {
wabort(bw->parent->link.prev);
}
} else {
if (bw->parent->link.next->watom==&watommenu) {
wabort(bw->parent->link.next);
}
}
obj_perm(line);
vaperm(lst);
m = mkmenu((menu_above ? bw->parent->link.prev : bw->parent), bw->parent, lst, cmplt_rtn, cmplt_abrt, NULL, 0, line);
if (!m) {
return -1;
}
if (valen(lst) == 1)
return cmplt_rtn(m, 0, line);
else if (smode || isreg(line)) {
if (!menu_jump)
bw->parent->t->curwin=bw->parent;
return 0;
} else {
unsigned char *com = mcomplete(m);
obj_free(m->object);
m->object = com;
obj_perm(com);
cmplt_ins(bw, com);
wabort(m->parent);
smode = 2;
ttputc(7);
return 0;
}
}
/* Simplified prompting... convert original event-driven style to
* coroutine model */
struct prompt_result {
Coroutine t;
unsigned char *answer;
};
int prompt_cont(BW *bw, unsigned char *s, void *object)
{
struct prompt_result *r = (struct prompt_result *)object;
r->answer = s;
/* move answer to original coroutine's obj_stack */
obj_perm(r->answer);
co_resume(&r->t, 0);
return 0;
}
int prompt_abrt(BW *bw, void *object)
{
struct prompt_result *r = (struct prompt_result *)object;
r->answer = 0;
co_resume(&r->t, -1);
return -1;
}
unsigned char *ask(W *w, /* Prompt goes below this window */
unsigned char *prompt, /* Prompt text */
B **history, /* History buffer to use */
unsigned char *huh, /* Name of help screen for this prompt */
int (*tab)(), /* Called when tab key is pressed */
struct charmap *map, /* Character map for prompt */
int file_prompt, /* Set for file-name tilde expansion */
int retrieve_last, /* Set for cursor to go on last line of history */
unsigned char *preload) /* Text to preload into prompt */
{
struct prompt_result t;
BW *bw = wmkpw(w, prompt, history, prompt_cont, huh, prompt_abrt, tab,
&t, map, file_prompt);
if (!bw)
return 0;
bw->parent->coro = &t.t;
if (preload) {
/* Load hint, put cursor after it */
binss(bw->cursor, preload);
pset(bw->cursor, bw->b->eof);
bw->cursor->xcol = piscol(bw->cursor);
} else if (retrieve_last) {
/* One step back through history */
uuparw(bw);
u_goto_eol(bw);
bw->cursor->xcol = piscol(bw->cursor);
}
/* We get woken up when user hits return */
if (!co_yield(&t.t, 0)) {
/* Moving answer to original coroutine's stack */
obj_temp(t.answer);
return t.answer;
} else {
return 0;
}
}