forked from n-t-roff/sc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.c
2271 lines (2042 loc) · 65.8 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
* updated by Charlie Gordon: June, 2021
*
* $Revision: 9.1 $
*/
#include "sc.h"
/*
* This structure is used to keep ent structs around before they
* are deleted to allow the sync_refs routine a chance to fix the
* variable references.
* We use it for 36 named registers (a-z,0-9), 1 unnamed register ("),
* and 2 temporary registers.
*/
typedef struct subsheet {
int minrow, mincol, maxrow, maxcol;
int ncols, nrows, num, refs;
SCXMEM rowptr_t *tbl;
SCXMEM colfmt_t *colfmt;
SCXMEM rowfmt_t *rowfmt;
} subsheet_t;
#define DELBUF_COUNT 40 /* Number of named buffers + 4 */
#define DELBUF_DEF 0
#define DELBUF_TMP1 1
#define DELBUF_TMP2 2
#define DELBUF_A (DELBUF_COUNT - 26)
#define DELBUF_0 (DELBUF_COUNT - 36)
#define DELBUF_1 (DELBUF_0 + 1)
#define DELBUF_9 (DELBUF_0 + 9)
static subsheet_t delbuf_array[DELBUF_COUNT];
static subsheet_t *delbuf[DELBUF_COUNT];
static int qbuf; /* register no. specified by " command */
/* a linked list of free [struct ent]'s, uses .next as the pointer */
static struct ent *free_ents;
#define ATBL(sp, row, col) (&(sp)->tbl[row].cp[col])
static int any_locked_cells(sheet_t *sp, rangeref_t rr);
static void move_area(sheet_t *sp, int dr, int dc, rangeref_t rr);
static void yank_area(sheet_t *sp, int idx, rangeref_t rr);
#define KA_NO_FMT 1
#define KA_COPY 2
#define KA_MOVE 4
static void kill_area(sheet_t *sp, int idx, rangeref_t rr, int flags);
// XXX: should use rangeref_t for adjust range
static void ent_copy(sheet_t *sp, struct ent *n, struct ent *p,
int dr, int dc, int r1, int c1, int r2, int c2, int transpose);
#define fix_ranges(sp, row1, col1, row2, col2, delta1, delta2, fr)
#define sync_refs(sp)
#define sync_ranges(sp)
static void ent_free(struct ent *p) {
if (p) {
efree(p->expr);
p->expr = NULL;
string_set(&p->label, NULL);
string_set(&p->format, NULL);
p->v = 0.0;
p->cellerror = 0;
p->type = SC_EMPTY;
p->flags = IS_CHANGED | IS_CLEARED;
p->next = free_ents; /* put this ent on the front of free_ents */
free_ents = p;
}
}
void delbuf_init(void) {
int i;
subsheet_t *db;
for (i = 0, db = delbuf_array; i < DELBUF_COUNT; i++, db++) {
db->num = i + 1;
db->maxrow = db->maxcol = -1;
}
}
/* swap 2 entries in the delbuf_ptr array */
static void delbuf_swap(int idx1, int idx2) {
subsheet_t *tmpbuf = delbuf[idx1];
delbuf[idx1] = delbuf[idx2];
delbuf[idx2] = tmpbuf;
}
/* rotate a range of entries in the delbuf array */
static void delbuf_rotate(int idx1, int idx2) {
while (idx2 --> idx1) {
delbuf_swap(idx2 + 1, idx2);
}
}
static void delbuf_clear(subsheet_t *db) {
if (db) {
db->minrow = db->mincol = 0;
db->maxrow = db->maxcol = -1;
db->ncols = db->nrows = 0;
db->tbl = NULL;
db->colfmt = NULL;
db->rowfmt = NULL;
}
}
/* unlink subsheet data, preserve duplicates if any */
static void delbuf_free(int idx) {
subsheet_t *db;
if (idx < 0 || !(db = delbuf[idx]))
return;
delbuf[idx] = NULL;
if (--db->refs == 0) {
/* free subsheet data if no duplicate found */
// XXX: should use fast cell iterator
int r, c;
for (r = 0; r < db->nrows; r++) {
for (c = 0; c < db->ncols; c++) {
struct ent *p = db->tbl[r].cp[c];
if (p) {
ent_free(p);
db->tbl[r].cp[c] = NULL;
}
}
scxfree(db->tbl[r].cp);
db->tbl[r].cp = NULL;
}
scxfree(db->tbl);
db->tbl = NULL;
scxfree(db->rowfmt);
db->rowfmt = NULL;
scxfree(db->colfmt);
db->colfmt = NULL;
delbuf_clear(db);
}
}
static subsheet_t *delbuf_find(int idx) {
int i;
subsheet_t *db = &delbuf_array[idx];
if (db->refs > 0) {
for (i = 0, db = delbuf_array; i < DELBUF_COUNT; i++, db++) {
if (db->refs == 0)
break;
}
}
db->refs++;
return delbuf[idx] = db;
}
static void delbuf_copy(int src, int dest) {
if (dest != src) {
subsheet_t *db = delbuf[src];
if (db) db->refs++;
delbuf_free(dest);
delbuf[dest] = db;
}
}
// XXX: do we need this?
static void delbuf_unsync(int idx) {
subsheet_t *db = delbuf[idx];
int r, c;
if (db) {
// XXX: should use fast cell iterator
for (r = 0; r < db->nrows; r++) {
for (c = 0; c < db->ncols; c++) {
struct ent *p = db->tbl[r].cp[c];
if (p)
p->flags &= ~MAY_SYNC;
}
}
}
}
void delbuf_list(sheet_t *sp, FILE *f) {
int i, n = 0;
for (i = 0; i < DELBUF_COUNT; i++) {
int c = "\"@#$0123456789abcdefghijklmnopqrstuvwxyz"[i];
subsheet_t *db = delbuf[i];
if (db) {
fprintf(f, " \"%c %2d %2d %s\n", c, db->num, db->refs,
range_addr(sp, rangeref(db->minrow, db->mincol, db->maxrow, db->maxcol)));
if (brokenpipe) return;
n++;
}
}
if (!n) fprintf(f, " No active registers\n");
}
void delbuf_clean(void) {
struct ent *p, *next;
int i;
for (i = 0; i < DELBUF_COUNT; i++) {
delbuf_free(i);
}
for (p = free_ents, free_ents = NULL; p; p = next) {
next = p->next;
scxfree(p);
}
}
static void delbuf_setcell(subsheet_t *db, int row, int col, struct ent *p) {
if (db && row >= db->minrow && row <= db->maxrow && col >= db->mincol && col <= db->maxcol) {
db->tbl[row - db->minrow].cp[col - db->mincol] = p;
} else {
ent_free(p);
}
}
struct ent *getcell(sheet_t *sp, int row, int col) {
if (row >= 0 && row <= sp->maxrow && col >= 0 && col <= sp->maxcol)
return *ATBL(sp, row, col);
else
return NULL;
}
int valid_cell(sheet_t *sp, int row, int col) {
struct ent *p = getcell(sp, row, col);
return (p != NULL && (p->expr || p->type != SC_EMPTY));
}
// XXX: should extend sheet if required and p != NULL
static int setcell(sheet_t *sp, int row, int col, struct ent *p) {
if (row >= 0 && row <= sp->maxrow && col >= 0 && col <= sp->maxcol) {
struct ent **pp = ATBL(sp, row, col);
if (*pp && *pp != p) {
ent_free(*pp);
}
*pp = p;
FullUpdate++; // XXX: really?
changed++;
sp->modflg++;
return 1;
}
return 0;
}
static struct ent *ent_alloc(sheet_t *sp) {
struct ent *p;
if ((p = free_ents) != NULL) {
free_ents = p->next;
} else {
p = scxmalloc(sizeof(struct ent));
if (!p)
return NULL;
}
p->v = 0.0;
p->label = NULL;
p->expr = NULL;
p->format = NULL;
p->cellerror = 0;
p->type = SC_EMPTY;
p->flags = MAY_SYNC;
p->next = NULL;
return p;
}
/* return a pointer to a cell structure [struct ent *],
extending the sheet and allocating the structure if needed
*/
struct ent *lookat(sheet_t *sp, int row, int col) {
struct ent **pp;
if (row < 0 || col < 0)
return NULL;
if (row > sp->maxrow || col > sp->maxcol) {
if (checkbounds(sp, row, col) < 0)
return NULL;
if (sp->maxrow < row) sp->maxrow = row;
if (sp->maxcol < col) sp->maxcol = col;
}
pp = ATBL(sp, row, col);
if (*pp == NULL) {
*pp = ent_alloc(sp);
}
return *pp;
}
rangeref_t *range_normalize(rangeref_t *rr) {
if (rr->left.row > rr->right.row) {
int row = rr->left.row;
rr->left.row = rr->right.row;
rr->right.row = row;
rr->left.vf ^= rr->right.vf & FIX_ROW;
rr->right.vf ^= rr->left.vf & FIX_ROW;
rr->left.vf ^= rr->right.vf & FIX_ROW;
}
if (rr->left.col > rr->right.col) {
int col = rr->left.col;
rr->left.col = rr->right.col;
rr->right.col = col;
rr->left.vf ^= rr->right.vf & FIX_COL;
rr->right.vf ^= rr->left.vf & FIX_COL;
rr->left.vf ^= rr->right.vf & FIX_COL;
}
return rr;
}
static rangeref_t *range_clip(sheet_t *sp, rangeref_t *rr) {
if (rr->left.row < 0) rr->left.row = 0;
if (rr->left.col < 0) rr->left.col = 0;
if (rr->right.row > sp->maxrow) rr->right.row = sp->maxrow;
if (rr->right.col > sp->maxcol) rr->right.col = sp->maxcol;
return rr;
}
/* duplicate the row at `cr.row` below it into a new row */
int dup_row(sheet_t *sp, cellref_t cr) {
int row = cr.row, col;
int c1 = 0;
int c2 = sp->maxcol;
struct frange *fr;
if ((fr = frange_find(sp, cr.row, cr.col))) {
c1 = fr->orr.left.col;
c2 = fr->orr.right.col;
}
if (!insert_rows(sp, cr, 1, 1))
return 0;
sp->modflg++;
// XXX: should use copy_area(row + 1, c1, row + 1, c2, row, c1, row, c2)
for (col = c1; col <= c2; col++) {
// XXX: should pass coordinates to ent_copy ?
struct ent *p = getcell(sp, row, col);
if (p) {
struct ent *n = lookat(sp, row + 1, col);
if (n)
ent_copy(sp, n, p, 1, 0, 0, 0, sp->maxrow, sp->maxcol, 0);
}
}
return 1;
}
/* duplicate the column at `cr.col` to the right it into a new column */
int dup_col(sheet_t *sp, cellref_t cr) {
int row, col = cr.col;
if (!insert_cols(sp, cr, 1, 1))
return 0;
sp->modflg++;
// XXX: should use copy_area(0, col + 1, maxrow, col + 1, 0, col, maxrow, col)
for (row = 0; row <= sp->maxrow; row++) {
// XXX: should pass coordinates to ent_copy
struct ent *p = getcell(sp, row, col);
if (p) {
struct ent *n = lookat(sp, row, col + 1);
if (n)
ent_copy(sp, n, p, 0, 1, 0, 0, sp->maxrow, sp->maxcol, 0);
}
}
return 1;
}
#if 0
static void fix_cellref(cellref_t *rp, rangeref_t rr, int dr, int dc) {
/* if a cell reference is inside the target range, shift it */
if (rp->row >= rr.left.row && rp->row <= rr.right.row
&& rp->col >= rr.left.col && rp->col <= rr.right.col) {
rp->row += dr;
rp->col += dc;
}
}
#endif
/* Insert 'arg' rows. The row(s) will be inserted before cr.row if delta
* is 0; after if it is 1.
* return 0 on failure, 1 on success
*/
int insert_rows(sheet_t *sp, cellref_t cr, int arg, int delta) {
int r, lim;
rangeref_t rr = rangeref(cr.row + delta, 0, sp->maxrow, sp->maxcol);
adjust_ctx_t adjust_ctx;
struct frange *fr;
// XXX: no check for locked cells?
if (checkbounds(sp, sp->maxrow + arg, 0) < 0)
return 0;
// XXX: should clip cr reference
lim = cr.row + delta + arg - 1; /* last inserted row */
sp->maxrow += arg;
if ((fr = frange_find(sp, cr.row, cr.col))) {
// XXX: should verify if cr.row + delta is inside the frange
// XXX: inconsistent source range: marks are updated beyond rr.right.row
rr = rangeref(cr.row + delta, fr->orr.left.col,
fr->orr.right.row, fr->orr.right.col);
move_area(sp, rr.left.row + arg, rr.left.col, rr);
#if 0
if (!delta && fr->irr.left.row == cr.row + arg) {
//fr->ir_left = lookat(sp, fr->irr.left.row - arg, fr->irr.left.col);
}
if (delta && fr->irr.right.row == cr.row) {
//fr->ir_right = lookat(sp, fr->irr.right.row + arg, fr->irr.right.col);
}
#endif
} else {
/*
* save the last active row+1, shift the rows downward, put the last
* row in place of the first
*/
// XXX: this seems bogus, should implement a rotation
rowptr_t tmp_row = sp->tbl[sp->maxrow];
rowfmt_t tmp_fmt = sp->rowfmt[sp->maxrow];
for (r = sp->maxrow; r > lim; r--) {
sp->rowfmt[r] = sp->rowfmt[r-arg];
sp->rowfmt[r-arg] = sp->rowfmt[r-1];
sp->tbl[r] = sp->tbl[r-arg];
sp->tbl[r-arg] = sp->tbl[r-1];
}
sp->rowfmt[r] = tmp_fmt;
sp->tbl[r] = tmp_row; /* the last row was never used.... */
}
/* Adjust range references. */
adjust_ctx.sp = sp;
adjust_ctx.clamp_rr = rangeref(-1, -1, -1, -1);
adjust_ctx.clamp_newr = -1;
adjust_ctx.clamp_newc = -1;
adjust_ctx.move_rr = rr;
adjust_ctx.move_dr = arg;
adjust_ctx.move_dc = 0;
adjust_refs(&adjust_ctx);
// XXX: cell coordinates have been updated
fr = frange_find(sp, cr.row, cr.col);
if (delta) {
fix_ranges(sp, cr.row, -1, cr.row, -1, 0, arg, fr);
} else {
fix_ranges(sp, cr.row + arg, -1, cr.row + arg, -1, arg, 0, fr);
}
FullUpdate++;
sp->modflg++;
return 1;
}
/* Insert 'arg' cols. The col(s) will be inserted before cr.col if delta
* is 0; after if it is 1.
* return 0 on failure, 1 on success
*/
int insert_cols(sheet_t *sp, cellref_t cr, int arg, int delta) {
int r, c;
struct ent **pp;
/* cols are moved from sc1:sc2 to dc1:dc2 */
int sc1 = cr.col + delta;
int sc2 = sp->maxcol;
int dc1 = sc1 + arg;
int dc2 = sc2 + arg;
struct frange *fr;
colfmt_t def_colfmt = { FALSE, DEFWIDTH, DEFPREC, DEFREFMT };
adjust_ctx_t adjust_ctx;
if (checkbounds(sp, 0, sp->maxcol + arg) < 0)
return 0;
sp->maxcol += arg;
for (c = dc2; c >= dc1; c--) {
sp->colfmt[c] = sp->colfmt[c - arg];
}
for (c = sc1; c < dc1; c++) {
sp->colfmt[c] = def_colfmt;
}
for (r = 0; r <= sp->maxrow; r++) {
// XXX: should factorize as movecell()
pp = ATBL(sp, r, 0);
for (c = dc2; c >= dc1; c--) {
if ((pp[c] = pp[c - arg])) {
pp[c - arg] = NULL;
}
}
}
/* Adjust range references. */
adjust_ctx.sp = sp;
adjust_ctx.clamp_rr = rangeref(-1, -1, -1, -1);
adjust_ctx.clamp_newr = -1;
adjust_ctx.clamp_newc = -1;
adjust_ctx.move_rr = rangeref(0, sc1, sp->maxrow, sc2);
adjust_ctx.move_dr = 0;
adjust_ctx.move_dc = arg;
adjust_refs(&adjust_ctx);
// XXX: cell coordinates have been updated
fr = frange_find(sp, cr.row, cr.col);
if (delta) {
// XXX: why not always fix frame limits?
if (fr && fr->irr.right.col == cr.col) {
//fr->ir_right = lookat(sp, fr->irr.right.row, fr->irr.right.col + arg);
}
fix_ranges(sp, -1, cr.col, -1, cr.col, 0, arg, fr);
} else {
if (fr && fr->irr.left.col == cr.col + arg) {
//fr->ir_left = lookat(sp, fr->irr.left.row, fr->irr.left.col - arg);
}
fix_ranges(sp, -1, cr.col + arg, -1, cr.col + arg, arg, 0, fr);
}
FullUpdate++;
sp->modflg++;
return 1;
}
/* delete rows starting at r1 up to and including r2 */
void delete_rows(sheet_t *sp, int r1, int r2) {
int nrows;
int c1 = 0;
int c2 = sp->maxcol;
struct frange *fr = NULL;
adjust_ctx_t adjust_ctx;
if (r1 > r2) SWAPINT(r1, r2);
if (r2 > sp->maxrow)
r2 = sp->maxrow;
if (r1 > sp->maxrow) {
/* deleting rows beyond the active area: nothing to do */
// XXX: should still adjust references and set kill area?
return;
}
nrows = r2 - r1 + 1;
if (sp->currow == r1 && (fr = frange_get_current(sp))) {
c1 = fr->orr.left.col;
c2 = fr->orr.right.col;
}
if (any_locked_cells(sp, rangeref(r1, c1, r2, c2)))
return;
delbuf_free(DELBUF_9);
delbuf_free(qbuf);
sync_refs(sp);
kill_area(sp, DELBUF_DEF, rangeref(r1, c1, r2, c2), KA_MOVE);
// XXX: should delay until after area has moved
fix_ranges(sp, r1, -1, r2, -1, -1, -1, fr);
delbuf_copy(DELBUF_DEF, qbuf);
qbuf = 0;
delbuf_rotate(DELBUF_1, DELBUF_9);
delbuf_copy(DELBUF_DEF, DELBUF_1);
delbuf_unsync(DELBUF_DEF);
if (fr) {
// XXX: update current frame, possibly redundant with fix_ranges()
// should frame bottom rows should move up or not?
// the code is inconsistent
if (r1 + nrows > fr->irr.right.row && fr->irr.right.row >= r1) {
//fr->ir_right = lookat(sp, r1 - 1, fr->irr.right.col);
}
if (r1 + nrows > fr->orr.right.row) {
//fr->or_right = lookat(sp, r1 - 1, fr->orr.right.col);
} else {
move_area(sp, r1, c1, rangeref(r1 + nrows, c1, fr->orr.right.row, c2));
}
if (fr->irr.left.row > fr->irr.right.row) {
frange_delete(sp, fr);
fr = NULL;
}
} else {
/* moving whole rows by swapping row pointers */
int r, dr;
rowfmt_t def_rowfmt = { FALSE };
/* reset row formats in target range */
for (r = r1; r <= r2; r++) {
sp->rowfmt[r] = def_rowfmt;
}
/* rotate row pointers and row formats */
for (dr = r1; r <= sp->maxrow; r++, dr++) {
/* swap rows and fix cell entries */
rowptr_t tmprow = sp->tbl[dr];
rowfmt_t tmpfmt = sp->rowfmt[dr];
sp->tbl[dr] = sp->tbl[r];
sp->rowfmt[dr] = sp->rowfmt[r];
sp->tbl[r] = tmprow;
sp->rowfmt[r] = tmpfmt;
}
}
/* Adjust range references. */
adjust_ctx.sp = sp;
adjust_ctx.clamp_rr = rangeref(r1, c1, r2, c2);
adjust_ctx.clamp_newr = r1;
adjust_ctx.clamp_newc = -1;
adjust_ctx.move_rr = rangeref(r2 + 1, c1, sp->maxrow, c2);
adjust_ctx.move_dr = -nrows;
adjust_ctx.move_dc = 0;
adjust_refs(&adjust_ctx);
if (!fr) {
sp->maxrow -= nrows;
}
// XXX: missing fix_ranges() ?
FullUpdate++;
sp->modflg++;
/* sp->currow will not become > maxrow because maxrow was not decremented */
if (sp->currow > r1)
sp->currow = (sp->currow <= r2) ? r1 : sp->currow - nrows;
}
void yank_rows(sheet_t *sp, int r1, int r2) {
int arg, nrows;
int c1 = 0, c2 = sp->maxcol;
struct frange *fr;
if (r1 > r2) SWAPINT(r1, r2);
arg = r2 - r1 + 1;
nrows = sp->maxrow - r1 + 1;
// XXX: should check if r1 and r2 are inside current frange
if (r1 == sp->currow && (fr = frange_get_current(sp))) {
nrows = fr->orr.right.row - r1 + 1;
c1 = fr->orr.left.col;
c2 = fr->orr.right.col;
}
if (arg > nrows) {
// XXX: should grow table or clip args
error("Cannot yank %d %s, %d %s left",
arg, (arg == 1 ? "row" : "rows"),
nrows, (nrows == 1 ? "row" : "rows"));
return;
}
sync_refs(sp); // XXX: why here?
/* copy source cells to delbuf[0] */
yank_area(sp, DELBUF_DEF, rangeref(r1, c1, r1 + arg - 1, c2));
/* set yanked data in named buffer '0' or qbuf if any */
delbuf_copy(DELBUF_DEF, qbuf ? qbuf : DELBUF_0);
qbuf = 0;
}
void yank_cols(sheet_t *sp, int c1, int c2) {
int arg, ncols;
if (c1 > c2) SWAPINT(c1, c2);
arg = c2 - c1 + 1;
ncols = sp->maxcol - c1 + 1;
if (arg > ncols) {
// XXX: should grow table or clip args
error("Cannot yank %d %s, %d %s left",
arg, (arg == 1 ? "column" : "columns"),
ncols, (ncols == 1 ? "column" : "columns"));
return;
}
sync_refs(sp);
/* copy source cells to delbuf[0] */
yank_area(sp, DELBUF_DEF, rangeref(0, c1, sp->maxrow, c1 + arg - 1));
/* set yanked data in named buffer '0' or qbuf if any */
delbuf_copy(DELBUF_DEF, qbuf ? qbuf : DELBUF_0);
qbuf = 0;
}
/* move cell range to subsheet delbuf[idx] */
static void kill_area(sheet_t *sp, int idx, rangeref_t rr, int flags) {
subsheet_t *db;
int r, c;
int sr = rr.left.row;
int sc = rr.left.col;
int er = rr.right.row;
int ec = rr.right.col;
if (sr > er) SWAPINT(sr, er);
if (sc > ec) SWAPINT(sc, ec);
if (sr < 0) sr = 0;
if (sc < 0) sc = 0;
if (er > sp->maxrow) er = sp->maxrow;
if (ec > sp->maxcol) ec = sp->maxcol;
if (idx < 0) {
/* just free the allocated cells */
for (r = sr; r <= er; r++) {
for (c = sc; c <= ec; c++) {
struct ent **pp = ATBL(sp, r, c);
struct ent *p = *pp;
if (p) {
*pp = NULL;
ent_free(p);
FullUpdate++; // XXX: really?
changed++;
sp->modflg++;
}
}
}
return;
}
delbuf_free(idx);
db = delbuf_find(idx);
if (!db)
return;
db->minrow = sr;
db->mincol = sc;
db->maxrow = er;
db->maxcol = ec;
db->ncols = ec - sc + 1;
db->nrows = er - sr + 1;
if (db->ncols < 0 || db->nrows < 0)
return;
/* Allocate cell pointer matrix */
db->tbl = scxmalloc(db->nrows * sizeof(*db->tbl));
for (r = 0; r < db->nrows; r++) {
struct ent **pp = scxmalloc(db->ncols * sizeof(*pp));
for (c = 0; c < db->ncols; c++) {
pp[c] = NULL;
}
db->tbl[r].cp = pp;
db->tbl[r].maxcol = ec;
}
if (!(flags & KA_NO_FMT)) {
db->colfmt = scxmalloc(db->ncols * sizeof(*db->colfmt));
for (c = sc; c <= ec; c++) {
db->colfmt[c - sc] = sp->colfmt[c];
}
db->rowfmt = scxmalloc(db->nrows * sizeof(*db->rowfmt));
for (r = sr; r <= er; r++) {
db->rowfmt[r - sr] = sp->rowfmt[r];
}
}
if (flags & KA_COPY) {
for (r = sr; r <= er; r++) {
for (c = sc; c <= ec; c++) {
struct ent *p = getcell(sp, r, c);
if (p) {
struct ent *n = ent_alloc(sp);
ent_copy(sp, n, p, 0, 0, 0, 0, sp->maxrow, sp->maxcol, 0);
//db->tbl[r - sr].cp[c - sc] = n;
delbuf_setcell(db, r, c, n);
}
}
}
} else
if (flags & KA_MOVE) {
for (r = sr; r <= er; r++) {
for (c = sc; c <= ec; c++) {
/* move a cell to the delbuf subsheet */
struct ent **pp = ATBL(sp, r, c);
struct ent *p = *pp;
if (p) {
*pp = NULL;
p->flags |= IS_DELETED;
delbuf_setcell(db, r, c, p);
FullUpdate++; // XXX: really?
changed++;
sp->modflg++;
}
}
}
}
}
/* copy a range of cells to delbuf[idx] */
static void yank_area(sheet_t *sp, int idx, rangeref_t rr) {
range_clip(sp, range_normalize(&rr));
/* copy cell range to subsheet delbuf[idx] */
kill_area(sp, idx, rr, KA_COPY);
}
/* uses temporary subsheet DELBUF_TMP1 */
static void move_area(sheet_t *sp, int dr, int dc, rangeref_t rr) {
struct ent *p;
subsheet_t *db;
int r, c, deltar, deltac;
range_clip(sp, range_normalize(&rr));
/* First we kill the source range to temporary subsheet TMP1 */
kill_area(sp, DELBUF_TMP1, rr, KA_MOVE);
deltar = dr - rr.left.row;
deltac = dc - rr.left.col;
/* Now we erase the destination range. We then move the original source
* range from TMP1 to the destination range, adjusting the addresses as we go.
*/
/* discard cell range to black hole */
kill_area(sp, -1, rangeref(dr, dc, rr.right.row + deltar, rr.right.col + deltac), KA_NO_FMT);
// XXX: if moving entire columns or rows, the column or row flags should be copied
// XXX: this could be addressed with flags
// XXX: references are updated by the caller
db = delbuf[DELBUF_TMP1];
if (db) {
for (r = 0; r < db->nrows; r++) {
for (c = 0; c < db->ncols; c++) {
p = db->tbl[r].cp[c];
if (p) {
db->tbl[r].cp[c] = NULL;
p->flags &= ~IS_DELETED;
setcell(sp, dr + r, dc + c, p);
}
}
}
}
delbuf_free(DELBUF_TMP1);
}
/*
* deletes the expression associated w/ a cell and turns it into a constant
* containing whatever was on the screen
*/
void valueize_area(sheet_t *sp, rangeref_t rr) {
int r, c;
range_normalize(&rr);
if (any_locked_cells(sp, rr))
return;
for (r = rr.left.row; r <= rr.right.row; r++) {
for (c = rr.left.col; c <= rr.right.col; c++) {
struct ent *p = getcell(sp, r, c);
if (p && p->expr) {
efree(p->expr);
p->expr = NULL;
sp->modflg++;
}
}
}
}
/* copy/move cell range from subsheet delbuf[src]
using temporary subsheets DELBUF_TMP1 and DELBUF_TMP2 */
// XXX: should take destination range, instead of just corner cell?
static void pullcells(sheet_t *sp, int src, int cmd, cellref_t cr) {
struct ent *p, *n;
int minrow, mincol, maxrow, maxcol;
int numrows, numcols, deltar, deltac;
int i, row, col, r, c;
struct frange *fr;
subsheet_t *db = delbuf[src];
subsheet_t *db1;
if (!db || !db->nrows || !db->ncols) {
error("No data to pull");
return;
}
if (cmd == 'C') {
/* pullcells is called with qbuf == idx when cmd == 'C' */
copy_range(sp, COPY_FROM_QBUF, rangeref_current(sp), rangeref_empty());
return;
}
/* compute delbuf range */
minrow = db->minrow;
maxrow = db->maxrow;
mincol = db->mincol;
maxcol = db->maxcol;
// XXX: should check consistency with delbuf[src].ncols/nrows
numrows = maxrow - minrow + 1;
numcols = maxcol - mincol + 1;
deltar = cr.row - minrow;
deltac = cr.col - mincol;
/* pull commands:
'pr' -> PULLROWS
'pc' -> PULLCOLS
'pp' -> PULL (paste+pop)
'pm' -> PULLMERGE
'px' -> PULLXCHG
'pt' -> PULLTP (transpose)
'pf' -> PULLFMT
'pC' -> PULLCOPY implemented above as copy(COPY_FROM_QBUF)
'p.' -> does not get here, redirected to PULLCOPY
// XXX: should have 'pv' -> PULLVALUES
*/
if (cmd == 'r') { /* PULLROWS */
if (!insert_rows(sp, cr, numrows, 0))
return;
if ((fr = frange_find(sp, cr.row, cr.col))) {
deltac = fr->orr.left.col - mincol;
} else {
if (db->rowfmt && db->nrows >= numrows) {
// XXX: incorrect if destination range has more rows than src
for (i = 0; i < numrows; i++) {
sp->rowfmt[cr.row + i] = db->rowfmt[i];
}
}
deltac = 0;
}
} else
if (cmd == 'c') { /* PULLCOLS */
if (!insert_cols(sp, cr, numcols, 0))
return;
if (db->colfmt && db->ncols >= numcols) {
// XXX: incorrect if destination range has more columns than src
for (i = 0; i < numcols; i++) {
sp->colfmt[cr.col + i] = db->colfmt[i];
}
}
deltar = 0;
} else
if (cmd == 'x') { /* PULLXCHG */
/* Save the original contents of the destination range on the
* delete buffer stack in preparation for the exchange.
*/
/* move cell range to temporary subsheet */
kill_area(sp, DELBUF_TMP1, rangeref(minrow + deltar, mincol + deltac,
maxrow + deltar, maxcol + deltac), KA_MOVE);
} else
if (cmd == 'p') { /* PULL */
// XXX: should just clear area (free cells) and sync the references
kill_area(sp, DELBUF_TMP1, rangeref(minrow + deltar, mincol + deltac,
maxrow + deltar, maxcol + deltac), KA_MOVE | KA_NO_FMT);
sync_refs(sp);
delbuf_free(DELBUF_TMP1);
} else
if (cmd == 't') { /* PULLTP (transpose) */
// XXX: named ranges and range references may not be updated properly
// XXX: should just clear area (free cells) and sync the references
kill_area(sp, DELBUF_TMP1, rangeref(minrow + deltar, mincol + deltac,
minrow + deltar + maxcol - mincol,
mincol + deltac + maxrow - minrow), KA_MOVE | KA_NO_FMT);
sync_refs(sp);
delbuf_free(DELBUF_TMP1);
}
if (cmd != 'x') { /* PULLXCHG */
/* At this point, we copy the cells from the delete buffer into the
* destination range.
*/
for (r = 0; r < db->nrows; r++) {
for (c = 0; c < db->ncols; c++) {
p = db->tbl[r].cp[c];
if (p) {
// XXX: should pass coordinates to ent_copy
if (cmd == 't') { /* Transpose rows and columns while pulling. */
n = lookat(sp, minrow + deltar + c, mincol + deltac + r);
} else {
n = lookat(sp, minrow + deltar + r, mincol + deltac + c);
}
if (n)
ent_copy(sp, n, p, deltar, deltac, minrow, mincol, maxrow, maxcol, cmd);
}
}
}
}
/* 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 transposing or merging (including merging
* cell formats), or if the expressions in the destination cells have
* been adjusted during a copy.
*/
if (cmd != 't' && cmd != 'm' && cmd != 'f') {
/* cmd is in PULL, PULLROWS, PULLCOLS, PULLXCHG */
if (cmd != 'x') { /* PULLXCHG */
/* cmd is in PULL, PULLROWS, PULLCOLS */
/* allocate cell pointer matrix in TMP1 */
kill_area(sp, DELBUF_TMP1, rangeref(minrow + deltar, mincol + deltac,
maxrow + deltar, maxcol + deltac), KA_NO_FMT);
// XXX: db1->colfmt and db1->rowfmt are NULL
// XXX: move the copied cells from destination range to temporary subsheet? @@@
}
/* move the cells from delbuf to destination */
for (r = 0; r < db->nrows; r++) {
for (c = 0; c < db->ncols; c++) {
p = db->tbl[r].cp[c];
if (p) {
db->tbl[r].cp[c] = NULL;
// XXX: what if destination is locked ?
row = db->minrow + r + deltar;
col = db->mincol + c + deltac;
p->flags &= ~IS_DELETED;
setcell(sp, row, col, p);
}
}
}
/* Replace the pulled cells in the source register with the
* new set of cells.
*/
db1 = delbuf[DELBUF_TMP1];
{
SCXMEM rowptr_t *tbl = db1->tbl;
db1->tbl = db->tbl;
db->tbl = tbl;
}
delbuf_free(DELBUF_TMP1);
}
sync_refs(sp);
FullUpdate++;
sp->modflg++;
}
void cmd_pullcells(sheet_t *sp, int cmd, int uarg) {