forked from n-t-roff/sc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigate.c
699 lines (646 loc) · 18.7 KB
/
navigate.c
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/* SC A Spreadsheet Calculator
* Navigation routines
*
* original by James Gosling, September 1982
* modifications by Mark Weiser and Bruce Israel,
* University of Maryland
* More mods Robert Bond, 12/86
* updated by Charlie Gordon: June, 2021
*
* $Revision: 9.1 $
*/
#if defined REGCOMP
#include <regex.h>
#elif defined RE_COMP
extern char *re_comp(char *s);
extern char *re_exec(char *s);
#elif defined REGCMP
char *regcmp();
char *regex();
#else
#endif
#include "sc.h"
/* Use this structure to save the last 'g' command */
// XXX: this duplicates the search_ctx_t feature
struct go_save gs;
/* Goto subroutines */
void go_free(sheet_t *sp) {
gs.g_type = G_NONE;
string_set(&gs.g_s, NULL);
}
/* repeat the last goto command */
void go_last(sheet_t *sp) {
switch (gs.g_type) {
case G_CELL:
moveto(sp, gs.g_rr, gs.st);
break;
case G_NUM:
case G_ERROR:
case G_INVALID:
case G_STR:
case G_NSTR:
case G_XSTR:
do_search(sp, gs.g_type, gs.g_rr, gs.g_n, string_dup(gs.g_s));
break;
default:
error("Nothing to repeat");
break;
}
}
/* Place the cursor on a given cell. If st.row >= 0, place the cell
* at row st.row and column st.col in the upper left corner of the
* screen if possible.
*/
void moveto(sheet_t *sp, rangeref_t rr, cellref_t st) {
remember(sp, 0);
sp->currow = rr.left.row;
sp->curcol = rr.left.col;
go_free(sp);
gs.g_type = G_CELL;
gs.g_rr = rr;
gs.st = st;
if (st.row >= 0) {
sp->strow = st.row;
sp->stcol = st.col;
gs.stflag = 1;
} else {
gs.stflag = 0;
}
rowsinrange = rows_height(sp, rr.left.row, rr.right.row - rr.left.row + 1);
colsinrange = cols_width(sp, rr.left.col, rr.right.col - rr.left.col + 1);
FullUpdate++;
if (loading) {
// XXX: shy update the screen now?
update(sp, 1);
changed = 0;
} else {
remember(sp, 1);
}
}
// XXX: this duplicates the go_save structure
typedef struct search_context {
sheet_t *sp;
int g_type;
int errsearch;
double n;
const char *s;
#if defined REGCOMP
regex_t preg;
int errcode;
#elif defined RE_COMP
char *tmp;
#elif defined REGCMP
char *tmp;
#else
#endif
} search_ctx_t;
static int search_init(search_ctx_t *sc, sheet_t *sp, int g_type, rangeref_t rr, double n, SCXMEM string_t *str) {
sc->sp = sp;
go_free(sp);
gs.g_type = g_type;
gs.g_rr = rr;
gs.g_n = n;
string_set(&gs.g_s, str);
switch (sc->g_type = g_type) {
case G_ERROR:
case G_INVALID:
// XXX: refine this, find all errors for now
sc->errsearch = -2;
break;
case G_NUM:
sc->n = n;
break;
case G_STR:
case G_NSTR:
case G_XSTR:
if (!str)
return -1;
sc->s = s2c(str);
#if defined REGCOMP
if ((sc->errcode = regcomp(&sc->preg, sc->s, REG_EXTENDED))) {
char buf[160];
regerror(sc->errcode, &sc->preg, buf, sizeof(buf));
error("%s", buf);
return -1;
}
#elif defined RE_COMP
if ((sc->tmp = re_comp(sc->s)) != NULL) {
error("%s", sc->tmp);
return -1;
}
#elif defined REGCMP
if ((sc->tmp = regcmp(sc->s, NULL)) == NULL) {
error("Invalid search string");
return -1;
}
#else
/* otherwise nothing to do, will just use strcmp() */
#endif
break;
}
return 0;
}
static int search_match(search_ctx_t *sc, int row, int col, struct ent *p) {
switch (sc->g_type) {
case G_ERROR:
case G_INVALID:
if (sc->errsearch & (1 << p->cellerror))
return 1;
break;
case G_NUM:
if (p->type == SC_NUMBER && p->v == sc->n)
return 1;
break;
case G_STR:
case G_NSTR:
case G_XSTR: {
/* convert cell contents, do not test width, ignore alignment */
char field[FBUFLEN];
const char *s1 = field;
int align = ALIGN_DEFAULT;
sheet_t *sp = sc->sp;
*field = '\0';
if (sc->g_type == G_NSTR) {
/* match regex on formated number, error or boolean */
if (p->cellerror) {
s1 = error_name[p->cellerror];
} else
if (p->type == SC_BOOLEAN) {
s1 = boolean_name[!!p->v];
} else
if (p->type == SC_NUMBER) {
if (p->format) {
format(field, sizeof field, s2c(p->format), sp->colfmt[col].precision, p->v, &align);
} else {
engformat(field, sizeof field, sp->colfmt[col].realfmt, sp->colfmt[col].precision, p->v, &align);
}
}
// XXX: should other types be matched too?
} else if (sc->g_type == G_XSTR) {
/* match regex on expression source code only */
if (p->expr) {
// XXX: should pass row, col as the cell reference
decompile(sp, field, sizeof field, p->expr, 0, 0, DCP_DEFAULT);
if (*field == '?')
*field = '\0';
}
} else if (sc->g_type == G_STR) {
if (p->type == SC_STRING) {
s1 = s2str(p->label);
}
}
if (s1 && *s1
#if defined REGCOMP
&& (regexec(&sc->preg, s1, 0, NULL, 0) == 0)
#elif defined RE_COMP
&& (re_exec(s1) != 0)
#elif defined REGCMP
&& (regex(sc->tmp, s1) != NULL)
#else
&& (strcmp(sc->s, s1) == 0) // case sensitive
#endif
) {
return 1;
}
break;
}
}
return 0;
}
static void search_close(search_ctx_t *sc, int found) {
switch (sc->g_type) {
case G_ERROR:
case G_INVALID:
if (!found)
error("no ERROR cell found");
break;
case G_NUM:
if (!found)
error("Number not found");
break;
case G_STR:
case G_NSTR:
case G_XSTR:
#if defined REGCOMP
regfree(&sc->preg);
#elif defined RE_COMP
#elif defined REGCMP
free(sc->tmp);
#else
#endif
if (!found)
error("String not found");
break;
}
}
/*
* 'goto' either a given number, string, regex, 'error', or 'invalid' starting at currow/curcol
*/
int do_search(sheet_t *sp, int g_type, rangeref_t rr, double n, SCXMEM string_t *str) {
search_ctx_t sc[1];
struct ent *p;
int firstrow, firstcol, lastrow, lastcol;
int row, col, endr, endc;
int found = 0;
if (search_init(sc, sp, g_type, rr, n, str))
return -1;
remember(sp, 0);
firstrow = rr.left.row;
firstcol = rr.left.col;
lastrow = rr.right.row;
lastcol = rr.right.col;
// XXX: should clip search area to active area
row = sp->currow;
col = sp->curcol;
if (row >= firstrow && row <= lastrow && col >= firstcol && col <= lastcol) {
endr = row;
endc = col;
} else {
endr = row = lastrow;
endc = col = lastcol;
}
for (;;) {
if (col++ >= lastcol) {
col = firstcol;
if (col > lastcol)
break;
if (row++ >= lastrow) {
row = firstrow;
if (row > lastrow)
break;
}
}
// XXX: should skip hidden rows
if (!row_hidden(sp, row) && !col_hidden(sp, col) && (p = getcell(sp, row, col))) {
if (search_match(sc, row, col, p)) {
found = 1;
break;
}
}
if (row == endr && col == endc)
break;
}
search_close(sc, found);
if (found) {
sp->currow = row;
sp->curcol = col;
if (loading) {
update(sp, 1);
changed = 0;
} else {
remember(sp, 1);
}
}
return found;
}
/* spreadsheet navigation primitives */
void doend(sheet_t *sp, int rowinc, int colinc) {
int r, c;
remember(sp, 0);
if (valid_cell(sp, sp->currow, sp->curcol)) {
r = sp->currow + rowinc;
c = sp->curcol + colinc;
if (r >= 0 && r < sp->maxrows &&
c >= 0 && c < sp->maxcols &&
!valid_cell(sp, r, c)) {
sp->currow = r;
sp->curcol = c;
}
}
// XXX: skip hidden cells?
if (!valid_cell(sp, sp->currow, sp->curcol)) {
switch (rowinc) {
case -1:
while (!valid_cell(sp, sp->currow, sp->curcol) && sp->currow > 0)
sp->currow--;
break;
case 1:
while (!valid_cell(sp, sp->currow, sp->curcol) && sp->currow < sp->maxrows - 1)
sp->currow++;
break;
case 0:
switch (colinc) {
case -1:
while (!valid_cell(sp, sp->currow, sp->curcol) && sp->curcol > 0)
sp->curcol--;
break;
case 1:
while (!valid_cell(sp, sp->currow, sp->curcol) && sp->curcol < sp->maxcols - 1)
sp->curcol++;
break;
}
break;
}
remember(sp, 1);
return;
}
switch (rowinc) {
case -1:
while (valid_cell(sp, sp->currow, sp->curcol) && sp->currow > 0)
sp->currow--;
break;
case 1:
while (valid_cell(sp, sp->currow, sp->curcol) && sp->currow < sp->maxrows - 1)
sp->currow++;
break;
case 0:
switch (colinc) {
case -1:
while (valid_cell(sp, sp->currow, sp->curcol) && sp->curcol > 0)
sp->curcol--;
break;
case 1:
while (valid_cell(sp, sp->currow, sp->curcol) && sp->curcol < sp->maxcols - 1)
sp->curcol++;
break;
}
break;
}
if (!valid_cell(sp, sp->currow, sp->curcol)) {
// XXX: this is bogus if already on maxcol or maxrow
sp->currow -= rowinc;
sp->curcol -= colinc;
}
}
/* moves currow down one page */
// XXX: hidden row issue
void forwpage(sheet_t *sp, int arg) {
int ps = sp->pagesize ? sp->pagesize : (screen_LINES - RESROW - framerows) / 2;
forwrow(sp, arg * ps);
sp->strow = sp->strow + arg * ps;
FullUpdate++;
}
/* moves currow up one page */
// XXX: hidden row issue
void backpage(sheet_t *sp, int arg) {
int ps = sp->pagesize ? sp->pagesize : (screen_LINES - RESROW - framerows) / 2;
backrow(sp, arg * ps);
sp->strow = sp->strow - arg * ps;
if (sp->strow < 0) sp->strow = 0;
FullUpdate++;
}
/* moves curcol forward to the next cell, wrapping at maxcols - 1 */
void forwcell(sheet_t *sp, int arg) {
while (arg --> 0) {
do {
if (sp->curcol < sp->maxcols - 1) {
sp->curcol++;
} else
if (sp->currow < sp->maxrows - 1) {
sp->curcol = 0;
while (++sp->currow < sp->maxrows - 1 && row_hidden(sp, sp->currow))
continue;
} else {
error("At end of table");
arg = 0;
break;
}
} while (col_hidden(sp, sp->curcol) || !valid_cell(sp, sp->currow, sp->curcol));
}
}
/* moves curcol backward to the previous cell, wrapping at 0 */
void backcell(sheet_t *sp, int arg) {
while (arg --> 0) {
do {
if (sp->curcol) {
sp->curcol--;
} else
if (sp->currow) {
sp->curcol = sp->maxcols - 1;
while (--sp->currow && row_hidden(sp, sp->currow))
continue;
} else {
error("At start of table");
arg = 0;
break;
}
} while (col_hidden(sp, sp->curcol) || !valid_cell(sp, sp->currow, sp->curcol));
}
}
/* moves curcol forward arg displayed columns */
void forwcol(sheet_t *sp, int arg) {
int col = sp->curcol;
while (arg > 0) {
if (col >= ABSMAXCOLS) {
error("At max col");
return;
}
col++;
if (!col_hidden(sp, col)) {
sp->curcol = col;
arg--;
}
}
}
/* moves curcol back arg displayed columns */
void backcol(sheet_t *sp, int arg) {
int col = sp->curcol;
while (arg > 0) {
if (col <= 0) {
error("At column A");
break;
}
col--;
if (!col_hidden(sp, col)) {
sp->curcol = col;
arg--;
}
}
}
/* moves currow forward arg displayed rows */
void forwrow(sheet_t *sp, int arg) {
int row = sp->currow;
while (arg > 0) {
if (row >= ABSMAXROWS) {
error("At max row");
return;
}
row++;
if (!row_hidden(sp, row)) {
sp->currow = row;
arg--;
}
}
}
/* moves currow backward arg displayed rows */
void backrow(sheet_t *sp, int arg) {
int row = sp->currow;
while (arg > 0) {
if (row <= 0) {
error("At min row");
break;
}
row--;
if (!row_hidden(sp, row)) {
sp->currow = row;
arg--;
}
}
}
void gotonote(sheet_t *sp) {
struct ent *p = getcell(sp, sp->currow, sp->curcol);
if (p && (p->flags & HAS_NOTE)) {
struct note *n = note_find(sp, cellref(sp->currow, sp->curcol));
if (n) {
if (!n->str) {
// XXX: what if target is hidden?
moveto(sp, n->rr, cellref(-1, -1));
} else {
error("No note target range");
}
} else {
error("Note not found");
}
} else {
error("No note attached");
}
}
/* If save is 0, remember the current position. Otherwise, if the current
* cell has changed since the last remember(sp, 0), save the remembered location
* for the `, ', and c comands.
*/
// XXX: move out of vi.c (maybe navigate.c ?)
void remember(sheet_t *sp, int save) {
if (loading)
return;
if (save) {
if (sp->currow != sp->remrow || sp->curcol != sp->remcol
|| sp->strow != sp->remstrow || sp->stcol != sp->remstcol) {
sp->savedcr[0] = cellref(sp->remrow, sp->remcol);
sp->savedst[0] = cellref(sp->remstrow, sp->remstcol);
}
} else {
sp->remrow = sp->currow;
sp->remcol = sp->curcol;
sp->remstrow = sp->strow;
sp->remstcol = sp->stcol;
}
}
void gohome(sheet_t *sp) {
struct frange *fr;
remember(sp, 0);
if ((fr = frange_get_current(sp))) {
if (cell_in_range(cellref(sp->currow, sp->curcol), fr->irr)
&& (sp->currow > fr->irr.left.row || sp->curcol > fr->irr.left.col)) {
sp->currow = fr->irr.left.row;
sp->curcol = fr->irr.left.col;
} else
if (sp->currow > fr->orr.left.row || sp->curcol > fr->orr.left.col) {
sp->currow = fr->orr.left.row;
sp->curcol = fr->orr.left.col;
} else {
sp->currow = 0;
sp->curcol = 0;
}
} else {
sp->currow = 0;
sp->curcol = 0;
}
remember(sp, 1);
FullUpdate++;
}
void leftlimit(sheet_t *sp) {
struct frange *fr;
remember(sp, 0);
if ((fr = frange_get_current(sp))) {
if (sp->currow >= fr->irr.left.row && sp->currow <= fr->irr.right.row &&
sp->curcol > fr->irr.left.col && sp->curcol <= fr->irr.right.col)
sp->curcol = fr->irr.left.col;
else
if (sp->curcol > fr->orr.left.col)
sp->curcol = fr->orr.left.col;
else
sp->curcol = 0;
} else {
sp->curcol = 0;
}
remember(sp, 1);
}
void rightlimit(sheet_t *sp) {
struct frange *fr;
remember(sp, 0);
if ((fr = frange_get_current(sp))) {
if (sp->currow >= fr->irr.left.row && sp->currow <= fr->irr.right.row &&
sp->curcol >= fr->irr.left.col && sp->curcol < fr->irr.right.col)
sp->curcol = fr->irr.right.col;
else
if (sp->curcol >= fr->orr.left.col && sp->curcol < fr->orr.right.col)
sp->curcol = fr->orr.right.col;
else {
sp->curcol = sp->maxcol;
while (!valid_cell(sp, sp->currow, sp->curcol) && sp->curcol > fr->orr.right.col)
sp->curcol--;
if ((fr = frange_get_current(sp)))
sp->curcol = fr->orr.right.col;
}
} else {
sp->curcol = sp->maxcol;
while (!valid_cell(sp, sp->currow, sp->curcol) && sp->curcol > 0)
sp->curcol--;
if ((fr = frange_get_current(sp)))
sp->curcol = fr->orr.right.col;
}
remember(sp, 1);
}
void gototop(sheet_t *sp) {
struct frange *fr;
remember(sp, 0);
if ((fr = frange_get_current(sp))) {
if (sp->curcol >= fr->irr.left.col && sp->curcol <= fr->irr.right.col &&
sp->currow > fr->irr.left.row && sp->currow <= fr->irr.right.row)
sp->currow = fr->irr.left.row;
else
if (sp->currow > fr->orr.left.row)
sp->currow = fr->orr.left.row;
else
sp->currow = 0;
} else {
sp->currow = 0;
}
remember(sp, 1);
}
void gotobottom(sheet_t *sp) {
struct frange *fr;
remember(sp, 0);
if ((fr = frange_get_current(sp))) {
if (sp->curcol >= fr->irr.left.col && sp->curcol <= fr->irr.right.col &&
sp->currow >= fr->irr.left.row && sp->currow < fr->irr.right.row)
sp->currow = fr->irr.right.row;
else
if (sp->currow < fr->orr.right.row)
sp->currow = fr->orr.right.row;
else {
sp->currow = sp->maxrow;
while (!valid_cell(sp, sp->currow, sp->curcol) && sp->currow > fr->orr.right.row)
sp->currow--;
if ((fr = frange_get_current(sp)))
sp->currow = fr->orr.right.row;
}
} else {
sp->currow = sp->maxrow;
while (!valid_cell(sp, sp->currow, sp->curcol) && sp->currow > 0)
sp->currow--;
if ((fr = frange_get_current(sp)))
sp->currow = fr->orr.right.row;
}
remember(sp, 1);
}
void scroll_down(sheet_t *sp) {
sp->strow++;
// XXX: check maximum row?
while (row_hidden(sp, sp->strow))
sp->strow++;
if (sp->currow < sp->strow)
sp->currow = sp->strow;
}
void scroll_up(sheet_t *sp, int x) {
if (sp->strow) {
sp->strow--;
while (sp->strow && row_hidden(sp, sp->strow))
sp->strow--;
}
forwrow(sp, x);
if (sp->currow >= lastendrow)
backrow(sp, 1);
backrow(sp, x);
}