forked from erithmetic/sc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.c
2891 lines (2626 loc) · 67.4 KB
/
cmds.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SC A Spreadsheet Calculator
* Command routines
*
* original by James Gosling, September 1982
* modifications by Mark Weiser and Bruce Israel,
* University of Maryland
*
* More mods Robert Bond, 12/86
*
* $Revision: 7.13 $
*/
#include <sys/types.h>
#include <sys/wait.h>
#if defined(BSD42) || defined(BSD43)
#include <strings.h>
#else
#ifndef SYSIII
#include <string.h>
#endif
#endif
#include <curses.h>
#include <time.h>
#if defined(BSD42) || defined(BSD43) || defined(VMS)
#include <sys/file.h>
#else
#include <fcntl.h>
#endif
#include "sc.h"
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#ifndef MSDOS
#include <unistd.h>
#endif
void syncref(register struct enode *e);
void unspecial(FILE *f, char *str, int delim);
/* a linked list of free [struct ent]'s, uses .next as the pointer */
extern struct ent *freeents;
/* a linked list of free [struct enodes]'s, uses .e.o.left as the pointer */
extern struct enode *freeenodes;
#define DEFCOLDELIM ':'
extern char *scext;
extern char *ascext;
extern char *tbl0ext;
extern char *tblext;
extern char *latexext;
extern char *slatexext;
extern char *texext;
extern int Vopt;
extern struct go_save gs;
int macrofd;
int cslop;
/* copy the current row (currow) and place the cursor in the new row */
void
duprow()
{
int c1, c2, coltmp = curcol;
struct frange *fr;
fr = find_frange(currow, curcol);
c1 = fr ? fr->or_left->col : 0;
c2 = fr ? fr->or_right->col : maxcol;
if (currow >= maxrows - 1 || (!fr && maxrow >= maxrows - 1) ||
(fr && fr->or_right->row >= maxrows - 1)) {
if (!growtbl(GROWROW, 0, 0))
return;
}
modflg++;
insertrow(1, 1);
for (curcol = c1; curcol <= c2; curcol++) {
register struct ent *p = *ATBL(tbl, currow - 1, curcol);
if (p) {
register struct ent *n;
n = lookat(currow, curcol);
(void) copyent(n, p, 1, 0);
}
}
for (curcol = c1; curcol <= c2; curcol++) {
register struct ent *p = *ATBL(tbl, currow, curcol);
if (p && (p->flags & is_valid) && !p->expr)
break;
}
curcol = coltmp;
}
/* copy the current column (curcol) and place the cursor in the new column */
void
dupcol()
{
int rowtmp = currow;
if (curcol >= maxcols - 1 || maxcol >= maxcols - 1) {
if (!growtbl(GROWCOL, 0, 0))
return;
}
modflg++;
insertcol(1, 1);
for (currow = 0; currow <= maxrow; currow++) {
register struct ent *p = *ATBL(tbl, currow, curcol - 1);
if (p) {
register struct ent *n;
n = lookat(currow, curcol);
copyent(n, p, 0, 1);
}
}
for (currow = 0; currow <= maxrow; currow++) {
register struct ent *p = *ATBL(tbl, currow, curcol);
if (p && (p -> flags & is_valid) && !p -> expr)
break;
}
currow = rowtmp;
}
/* Insert 'arg' rows. The row(s) will be inserted before currow if delta
* is 0; after if it is 1.
*/
void
insertrow(int arg, int delta)
{
int r, c, i;
struct ent **tmprow, **pp;
int lim = maxrow - currow + 1;
struct frange *fr;
if (currow > maxrow)
maxrow = currow;
maxrow += arg;
lim = maxrow - lim + delta;
if ((maxrow >= maxrows) && !growtbl(GROWROW, maxrow, 0))
return;
if ((fr = find_frange(currow + delta, curcol))) {
move_area(currow + arg + delta, fr->or_left->col, currow + delta,
fr->or_left->col, fr->or_right->row, fr->or_right->col);
flush_saved();
if (!delta && fr->ir_left->row == currow + arg)
fr->ir_left = lookat(fr->ir_left->row - arg, fr->ir_left->col);
if (delta && fr->ir_right->row == currow)
fr->ir_right = lookat(fr->ir_right->row + arg, fr->ir_right->col);
for (i=0; i < 27; i++) { /* update all marked cells */
if (savedrow[i] >= currow + delta &&
savedcol[i] >= fr->or_left->col &&
savedcol[i] <= fr->or_right->col)
savedrow[i] += arg;
if (savedstrow[i] >= currow + delta &&
savedstcol[i] >= fr->or_left->col &&
savedstcol[i] <= fr->or_right->col)
savedstrow[i] += arg;
}
if (gs.g_row >= currow + delta &&
gs.g_col >= fr->or_left->col &&
gs.g_col <= fr->or_right->col)
gs.g_row += arg;
if (gs.g_lastrow >= currow + delta &&
gs.g_lastcol >= fr->or_left->col &&
gs.g_lastcol <= fr->or_right->col)
gs.g_lastrow += arg;
if (gs.strow >= currow + delta &&
gs.stcol >= fr->or_left->col &&
gs.stcol <= fr->or_right->col)
gs.strow += arg;
} else {
/*
* save the last active row+1, shift the rows downward, put the last
* row in place of the first
*/
tmprow = tbl[maxrow];
for (r = maxrow; r > lim; r--) {
row_hidden[r] = row_hidden[r-arg];
row_hidden[r-arg] = row_hidden[r-1];
tbl[r] = tbl[r-arg];
tbl[r-arg] = tbl[r-1];
pp = ATBL(tbl, r, 0);
for (c = 0; c < maxcols; c++, pp++)
if (*pp)
(*pp)->row = r;
}
tbl[r] = tmprow; /* the last row was never used.... */
for (i=0; i < 27; i++) { /* update all marked cells */
if (savedrow[i] >= currow + delta)
savedrow[i] += arg;
if (savedstrow[i] >= currow + delta)
savedstrow[i] += arg;
}
if (gs.g_row >= currow + delta)
gs.g_row += arg;
if (gs.g_lastrow >= currow + delta)
gs.g_lastrow += arg;
if (gs.strow >= currow + delta)
gs.strow += arg;
for (r = 0; r <= maxrow; r++) {
for (c = 0; c <= maxcol; c++) {
pp = ATBL(tbl, r, c);
if (*pp) {
if ((*pp)->nrow >= currow + delta)
((*pp)->nrow) += arg;
if ((*pp)->nlastrow >= currow + delta)
((*pp)->nlastrow) += arg;
}
}
}
}
fix_ranges(currow + arg * (1 - delta), -1, currow + arg * (1 - delta), -1,
delta?0:arg, delta?arg:0);
currow += delta;
FullUpdate++;
modflg++;
}
/* Insert 'arg' cols. The col(s) will be inserted before curcol if delta
* is 0; after if it is 1.
*/
void
insertcol(int arg, int delta)
{
int r, c;
register struct ent **pp;
int lim = maxcol - curcol - delta + 1;
struct frange *fr;
if (curcol + delta > maxcol)
maxcol = curcol + delta;
maxcol += arg;
if ((maxcol >= maxcols) && !growtbl(GROWCOL, 0, maxcol))
return;
for (c = maxcol; c >= curcol + delta + arg; c--) {
fwidth[c] = fwidth[c-arg];
precision[c] = precision[c-arg];
realfmt[c] = realfmt[c-arg];
col_hidden[c] = col_hidden[c-arg];
}
for (c = curcol + delta; c - curcol - delta < arg; c++) {
fwidth[c] = DEFWIDTH;
precision[c] = DEFPREC;
realfmt[c] = DEFREFMT;
col_hidden[c] = FALSE;
}
for (r=0; r <= maxrow; r++) {
pp = ATBL(tbl, r, maxcol);
for (c = lim; --c >= 0; pp--)
if ((pp[0] = pp[-arg]))
pp[0]->col += arg;
pp = ATBL(tbl, r, curcol + delta);
for (c = curcol + delta; c - curcol - delta < arg; c++, pp++)
*pp = (struct ent *)0;
}
/* Update all marked cells. */
for (c=0; c < 27; c++) {
if (savedcol[c] >= curcol + delta)
savedcol[c] += arg;
if (savedstcol[c] >= curcol + delta)
savedstcol[c] += arg;
}
if (gs.g_col >= curcol + delta)
gs.g_col += arg;
if (gs.g_lastcol >= curcol + delta)
gs.g_lastcol += arg;
if (gs.stcol >= curcol + delta)
gs.stcol += arg;
/* Update note links. */
for (r = 0; r <= maxrow; r++) {
for (c = 0; c <= maxcol; c++) {
pp = ATBL(tbl, r, c);
if (*pp) {
if ((*pp)->ncol >= curcol + delta)
((*pp)->ncol) += arg;
if ((*pp)->nlastcol >= curcol + delta)
((*pp)->nlastcol) += arg;
}
}
}
if (!delta && (fr = find_frange(currow, curcol)) &&
fr->ir_left->col == curcol + arg)
fr->ir_left = lookat(fr->ir_left->row, fr->ir_left->col - arg);
if (delta && (fr = find_frange(currow, curcol)) &&
fr->ir_right->col == curcol)
fr->ir_right = lookat(fr->ir_right->row, fr->ir_right->col + arg);
fix_ranges(-1, curcol + arg * (1 - delta), -1, curcol + arg * (1 - delta),
delta?0:arg, delta?arg:0);
curcol += delta;
FullUpdate++;
modflg++;
}
/* delete 'arg' rows starting at currow (deletes from currow downward) */
void
deleterow(register int arg)
{
int rs = maxrow - currow + 1;
int i;
char buf[50];
struct frange *fr;
if ((fr = find_frange(currow, curcol)))
rs = fr->or_right->row - currow + 1;
if (rs - arg < 0) {
rs = rs > 0 ? rs : 0;
(void) sprintf(buf, "Can't delete %d row%s %d row%s left", arg,
(arg != 1 ? "s," : ","), rs, (rs != 1 ? "s" : ""));
error(buf);
return;
}
if (fr) {
if (any_locked_cells(currow, fr->or_left->col,
currow + arg - 1, fr->or_right->col))
error("Locked cells encountered. Nothing changed");
else {
FullUpdate++;
modflg++;
flush_saved();
sync_refs();
erase_area(currow, fr->or_left->col, currow + arg - 1,
fr->or_right->col);
fix_ranges(currow, -1, currow + arg - 1, -1, -1, -1);
if (currow + arg > fr->or_right->row)
fr->or_right = lookat(currow - 1, fr->or_right->col);
else {
move_area(currow, fr->or_left->col, currow + arg,
fr->or_left->col, fr->or_right->row, fr->or_right->col);
flush_saved();
}
if (currow + arg > fr->ir_right->row && fr->ir_right->row >= currow)
fr->ir_right = lookat(currow - 1, fr->ir_right->col);
/* Update all marked cells. */
for (i=0; i < 27; i++) {
if (savedcol[i] >= fr->or_left->col &&
savedcol[i] <= fr->or_right->col) {
if (savedrow[i] >= currow && savedrow[i] < currow + arg)
savedrow[i] = savedcol[i] = -1;
if (savedrow[i] >= currow + arg)
savedrow[i] -= arg;
}
if (savedstcol[i] >= fr->or_left->col &&
savedstcol[i] <= fr->or_right->col) {
if (savedstrow[i] >= currow && savedstrow[i] < currow + arg)
savedstrow[i] = currow;
if (savedstrow[i] >= currow + arg)
savedstrow[i] -= arg;
}
}
if (gs.g_col >= fr->or_left->col &&
gs.g_col <= fr->or_right->col) {
if (gs.g_row >= currow && gs.g_row < currow + arg)
gs.g_row = currow;
if (gs.g_row >= currow + arg)
gs.g_row -= arg;
}
if (gs.g_lastcol >= fr->or_left->col &&
gs.g_lastcol <= fr->or_right->col) {
if (gs.g_lastrow >= currow && gs.g_lastrow < currow + arg)
gs.g_lastrow = currow - 1;
if (gs.g_lastrow >= currow + arg)
gs.g_lastrow -= arg;
}
if (gs.g_row > gs.g_lastrow)
gs.g_row = gs.g_col = -1;
if (gs.stcol >= fr->or_left->col &&
gs.stcol <= fr->or_right->col) {
if (gs.strow >= currow && gs.strow < currow + arg)
gs.strow = currow;
if (gs.strow >= currow + arg)
gs.strow -= arg;
}
}
} else {
if (any_locked_cells(currow, 0, currow + arg - 1, maxcol))
error("Locked cells encountered. Nothing changed");
else {
flush_saved();
sync_refs();
erase_area(currow, 0, currow + arg - 1, maxcol);
fix_ranges(currow, -1, currow + arg - 1, -1, -1, -1);
closerow(currow, arg);
}
}
}
void
yankrow(int arg)
{
int rs = maxrow - currow + 1;
char buf[50];
struct frange *fr;
if ((fr = find_frange(currow, curcol)))
rs = fr->or_right->row - currow + 1;
if (rs - arg < 0) {
rs = rs > 0 ? rs : 0;
(void) sprintf(buf, "Can't yank %d row%s %d row%s left", arg,
(arg != 1 ? "s," : ","), rs, (rs != 1 ? "s" : ""));
error(buf);
return;
}
if (fr) {
sync_refs();
flush_saved();
yank_area(currow, fr->or_left->col, currow + arg - 1,
fr->or_right->col);
} else {
sync_refs();
flush_saved();
yank_area(currow, 0, currow + arg - 1, maxcol);
}
}
void
yankcol(int arg)
{
int cs = maxcol - curcol + 1;
char buf[50];
if (cs - arg < 0) {
cs = cs > 0 ? cs : 0;
(void) sprintf(buf, "Can't yank %d column%s %d column%s left", arg,
(arg != 1 ? "s," : ","), cs, (cs != 1 ? "s" : ""));
error(buf);
return;
}
sync_refs();
flush_saved();
yank_area(0, curcol, maxrow, curcol + arg - 1);
}
void
erase_area(int sr, int sc, int er, int ec)
{
int r, c;
struct ent **pp;
if (sr > er) {
r = sr; sr = er; er = r;
}
if (sc > ec) {
c = sc; sc = ec; ec = c;
}
if (sr < 0)
sr = 0;
if (sc < 0)
sc = 0;
checkbounds(&er, &ec);
/* Do a lookat() for the upper left and lower right cells of the range
* being erased to make sure they are included in the delete buffer so
* that pulling cells always works correctly even if the cells at one
* or more edges of the range are all empty.
*/
(void) lookat(sr, sc);
(void) lookat(er, ec);
dbidx++;
for (r = sr; r <= er; r++) {
for (c = sc; c <= ec; c++) {
pp = ATBL(tbl, r, c);
if (*pp && !((*pp)->flags&is_locked)) {
free_ent(*pp);
*pp = NULL;
}
}
}
}
void
yank_area(int sr, int sc, int er, int ec)
{
int r, c;
if (sr > er) {
r = sr; sr = er; er = r;
}
if (sc > ec) {
c = sc; sc = ec; ec = c;
}
if (sr < 0)
sr = 0;
if (sc < 0)
sc = 0;
checkbounds(&er, &ec);
r = currow;
currow = sr;
c = curcol;
curcol = sc;
erase_area(sr, sc, er, ec);
pullcells('m');
currow = r;
curcol = c;
}
void
move_area(int dr, int dc, int sr, int sc, int er, int ec)
{
struct ent *p;
struct ent **pp;
int deltar, deltac;
int r, c;
if (sr > er) {
r = sr; sr = er; er = r;
}
if (sc > ec) {
c = sc; sc = ec; ec = c;
}
if (sr < 0)
sr = 0;
if (sc < 0)
sc = 0;
checkbounds(&er, &ec);
r = currow;
currow = sr;
c = curcol;
curcol = sc;
/* First we erase the source range, which puts the cells on the delete
* buffer stack. The pullcells(0) then makes a copy in the original
* location, which we then erase, putting the copy on the delete buffer
* stack, too.
*/
erase_area(sr, sc, er, ec);
pullcells(0);
erase_area(sr, sc, er, ec);
currow = r;
curcol = c;
deltar = dr - sr;
deltac = dc - sc;
/* Now we erase the destination range, which adds it to the delete buffer
* stack, but then we flush it off. We then move the original source
* range from the stack to the destination range, adjusting the addresses
* as we go, which leaves only the copy on the stack for future pulls.
*/
erase_area(dr, dc, er + deltar, ec + deltac);
flush_saved();
for (p = delbuf[dbidx - 1]; p; p = p->next) {
pp = ATBL(tbl, p->row + deltar, p->col + deltac);
*pp = p;
p->row += deltar;
p->col += deltac;
p->flags &= ~is_deleted;
}
delbuf[dbidx - 1] = delbuf[dbidx];
delbuf[dbidx--] = NULL;
}
/*
* deletes the expression associated w/ a cell and turns it into a constant
* containing whatever was on the screen
*/
void
valueize_area(int sr, int sc, int er, int ec)
{
int r, c;
struct ent *p;
if (sr > er) {
r = sr; sr = er; er= r;
}
if (sc > ec) {
c = sc; sc = ec; ec= c;
}
if (sr < 0)
sr = 0;
if (sc < 0)
sc = 0;
checkbounds(&er, &ec);
for (r = sr; r <= er; r++) {
for (c = sc; c <= ec; c++) {
p = *ATBL(tbl, r, c);
if (p && p->flags&is_locked) {
error(" Cell %s%d is locked", coltoa(c), r);
continue;
}
if (p && p->expr) {
efree(p->expr);
p->expr = (struct enode *)0;
p->flags &= ~is_strexpr;
}
}
}
}
void
pullcells(int to_insert)
{
struct ent *p, *n;
struct ent **pp;
int deltar, deltac;
int minrow, mincol;
int mxrow, mxcol;
int numrows, numcols;
struct frange *fr;
if (dbidx < 0) {
error("No data to pull");
return;
}
minrow = maxrows;
mincol = maxcols;
mxrow = 0;
mxcol = 0;
for (p = delbuf[dbidx]; p; p = p->next) {
if (p->row < minrow)
minrow = p->row;
if (p->row > mxrow)
mxrow = p->row;
if (p->col < mincol)
mincol = p->col;
if (p->col > mxcol)
mxcol = p->col;
}
numrows = mxrow - minrow + 1;
numcols = mxcol - mincol + 1;
deltar = currow - minrow;
deltac = curcol - mincol;
if (to_insert == 'r') {
insertrow(numrows, 0);
if (fr = find_frange(currow, curcol))
deltac = fr->or_left->col - mincol;
else
deltac = 0;
} else if (to_insert == 'c') {
insertcol(numcols, 0);
deltar = 0;
} else if (to_insert == 'x') { /* Do an exchange. */
struct ent *tmp;
/* Save the original contents of the destination range in preparation
* for the exchange.
*/
erase_area(minrow + deltar, mincol + deltac, mxrow + deltar,
mxcol + deltac);
tmp = delbuf[dbidx];
delbuf[dbidx] = delbuf[dbidx - 1];
delbuf[dbidx - 1] = tmp;
}
FullUpdate++;
modflg++;
/* First copy the cells from the delete buffer into the destination
* range.
*/
for (p = delbuf[dbidx]; p; p = p->next) {
if (to_insert == 't')
/* Transpose rows and columns while pulling. */
n = lookat(minrow + deltar + p->col - mincol,
mincol + deltac + p->row - minrow);
else
n = lookat(p->row + deltar, p->col + deltac);
(void) clearent(n);
copyent(n, p, 0, 0);
n->flags = p->flags & ~is_deleted;
}
/* Now exchange them so that the original cells from the delete buffer
* are in the destination range instead of the copies. When doing a
* "pull exchange" ("px" or "pullxchg"), exchange the original contents
* of the destination range with the contents of the delete buffer
* instead. Don't do this if called from move_area() or when transposing.
*/
if (to_insert && to_insert != 't') {
if (to_insert == 'x') {
struct ent *tmp = delbuf[dbidx];
delbuf[dbidx] = delbuf[dbidx - 1];
delbuf[dbidx - 1] = tmp;
} else
erase_area(minrow + deltar, mincol + deltac, mxrow + deltar,
mxcol + deltac);
for (p = delbuf[dbidx - 1]; p; p = p->next) {
pp = ATBL(tbl, p->row + deltar, p->col + deltac);
*pp = p;
p->row += deltar;
p->col += deltac;
p->flags &= ~is_deleted;
}
delbuf[dbidx - 1] = delbuf[dbidx];
delbuf[dbidx--] = NULL;
sync_refs();
/*
* Now change the cell addresses in the delete buffer to match
* where the original cells came from.
*/
for (p = delbuf[dbidx]; p; p = p->next) {
p->row -= deltar;
p->col -= deltac;
}
}
}
void
colshow_op()
{
register int i,j;
for (i=0; i<maxcols; i++)
if (col_hidden[i])
break;
for(j=i; j<maxcols; j++)
if (!col_hidden[j])
break;
j--;
if (i>=maxcols)
error("No hidden columns to show");
else {
(void) sprintf(line,"show %s:", coltoa(i));
(void) sprintf(line + strlen(line),"%s",coltoa(j));
linelim = strlen(line);
}
}
void
rowshow_op()
{
register int i,j;
for (i=0; i<maxrows; i++)
if (row_hidden[i])
break;
for(j=i; j<maxrows; j++)
if (!row_hidden[j]) {
break;
}
j--;
if (i>=maxrows)
error("No hidden rows to show");
else {
(void)sprintf(line,"show %d:%d", i, j);
linelim = strlen(line);
}
}
/*
* Given a row/column command letter, emit a small menu, then read a qualifier
* character for a row/column command and convert it to 'r' (row), 'c'
* (column), or 0 (unknown). If ch is 'p', three extra qualifiers, 'm', 'x',
* and 't', are allowed. If ch is 'Z', an extra qualifier 'Z' is allowed.
*/
#define SHOWROWS 2
#define SHOWCOLS 3
int
get_rcqual(int ch)
{
int c;
error("%sow/column: r: row c: column%s",
#ifdef KEY_IC
(ch == KEY_IC) ? "Insert r" :
#endif
(ch == 'i') ? "Insert r" :
(ch == 'o') ? "Open r" :
(ch == 'a') ? "Append r" :
(ch == 'd') ? "Delete r" :
(ch == 'y') ? "Yank r" :
(ch == 'p') ? "Pull r" :
(ch == 'v') ? "Values r" :
(ch == 'Z') ? "Zap r" :
(ch == 's') ? "Show r" : "R",
(ch == 'p') ? " m: merge x: exchange t: transpose" : "");
(void) refresh();
switch (c = nmgetch())
{
case 'r': return ('r');
case 'c': return ('c');
case 'm': return ((ch == 'p') ? 'm' : 0);
case 'x': return ((ch == 'p') ? 'x' : 0);
case 't': return ((ch == 'p') ? 't' : 0);
case 'Z': return ((ch == 'Z') ? 'Z' : 0);
case ESC:
case ctl('g'): return (ESC);
case 'd': if (ch == 'd') {
ungetch('x');
return (ESC);
} else
return (0);
case 'y': if (ch == 'y') {
flush_saved();
yank_area(currow, curcol, currow, curcol);
return (ESC);
} else
return (0);
case 'v': if (ch == 'v') {
valueize_area(currow, curcol, currow, curcol);
return (ESC);
} else
return (0);
case KEY_UP:
case KEY_DOWN:
case KEY_PPAGE:
case KEY_NPAGE:
case 'j':
case 'k':
case 'J':
case 'K':
case ctl('f'):
case ctl('b'):
case ctl('n'):
case ctl('p'): if (ch == 'd')
(void) sprintf(line,"deleterow [range] ");
else if (ch == 'y')
(void) sprintf(line,"yankrow [range] ");
else
return (0);
edit_mode();
write_line('A');
startshow();
showrange = SHOWROWS;
showsr = currow;
ungetch(c);
return (ESC);
case KEY_BACKSPACE:
case KEY_LEFT:
case KEY_RIGHT:
case ' ':
case 'h':
case 'l':
case 'H':
case 'L': if (ch == 'd')
(void) sprintf(line,"deletecol [range] ");
else if (ch == 'y')
(void) sprintf(line,"yankcol [range] ");
else
return (0);
edit_mode();
write_line('A');
startshow();
showrange = SHOWCOLS;
showsc = curcol;
ungetch(c);
return (ESC);
default: return (0);
}
/*NOTREACHED*/
}
/* delete numrow rows, starting with rs */
void
closerow(int rs, int numrow)
{
register struct ent **pp;
int r, c, i;
struct ent **tmprow;
if (rs + numrow - 1 > maxrow) return;
r = rs;
/*
* Rows are dealt with in numrow groups, each group of rows spaced numrow
* rows apart.
*/
for (i = 0; i < numrow; i++) {
r = rs + i;
/* save the first row of the group and empty it out */
tmprow = tbl[r];
pp = ATBL(tbl, r, 0);
for (c = maxcol + 1; --c >= 0; pp++) {
if (*pp) {
free_ent(*pp);
*pp = (struct ent *)0;
}
}
/* move the rows, put the deleted, but now empty, row at the end */
for (; r + numrow < maxrows - 1; r += numrow) {
row_hidden[r] = row_hidden[r + numrow];
tbl[r] = tbl[r + numrow];
pp = ATBL(tbl, r, 0);
for (c = 0; c < maxcols; c++, pp++)
if (*pp)
(*pp)->row = r;
}
tbl[r] = tmprow;
}
/* Update all marked cells. */
for (i=0; i < 27; i++) {
if (savedrow[i] >= rs && savedrow[i] < rs + numrow)
savedrow[i] = savedcol[i] = -1;
if (savedrow[i] >= rs + numrow)
savedrow[i] -= numrow;
if (savedstrow[i] >= rs && savedstrow[i] < rs + numrow)
savedstrow[i] = rs;
if (savedstrow[i] >= rs + numrow)
savedstrow[i] -= numrow;
}
if (gs.g_row >= rs && gs.g_row < rs + numrow)
gs.g_row = rs;
if (gs.g_row >= rs + numrow)
gs.g_row -= numrow;
if (gs.g_lastrow >= rs && gs.g_lastrow < rs + numrow)
gs.g_lastrow = rs - 1;
if (gs.g_lastrow >= rs + numrow)
gs.g_lastrow -= numrow;
if (gs.g_row > gs.g_lastrow)
gs.g_row = gs.g_col = -1;
if (gs.strow >= rs && gs.strow < rs + numrow)
gs.strow = rs;
if (gs.strow >= rs + numrow)
gs.strow -= numrow;
maxrow -= numrow;
/* Update note links. */
for (r = 0; r <= maxrow; r++) {
for (c = 0; c <= maxcol; c++) {
pp = ATBL(tbl, r, c);
if (*pp) {
if ((*pp)->nrow >= rs && (*pp)->nrow < rs + numrow)
(*pp)->nrow = rs;
if ((*pp)->nrow >= rs + numrow)
((*pp)->nrow) -= numrow;
if ((*pp)->nlastrow >= rs && (*pp)->nlastrow < rs + numrow)
(*pp)->nlastrow = rs - 1;
if ((*pp)->nlastrow >= rs + numrow)
((*pp)->nlastrow) -= numrow;
if ((*pp)->nlastrow < (*pp)->nrow)
(*pp)->nrow = (*pp)->ncol = -1;
}
}
}
FullUpdate++;
modflg++;
}
/* delete group of columns (1 or more) */
void
closecol(int arg)
{
int r, c, i;
register struct ent **pp;
register struct ent *q;
int cs = maxcol - curcol + 1;
char buf[50];
if (cs - arg < 0) {
cs = cs > 0 ? cs : 0;
(void) sprintf(buf, "Can't delete %d column%s %d column%s left", arg,
(arg != 1 ? "s," : ","), cs, (cs != 1 ? "s" : ""));
error(buf);
return;
}
if (any_locked_cells(0, curcol, maxrow, curcol + arg - 1)) {
error("Locked cells encountered. Nothing changed");