-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1947 lines (1849 loc) · 53.8 KB
/
main.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
/* #includes */ /*{{{C}}}*//*{{{*/
#ifndef NO_POSIX_SOURCE
#undef _POSIX_SOURCE
#define _POSIX_SOURCE 1
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 2
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#endif
#ifdef DMALLOC
#include "dmalloc.h"
#endif
#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
extern char *optarg;
extern int optind,opterr,optopt;
int getopt(int argc, char * const *argv, const char *optstring);
#include <string.h>
#include <unistd.h>
#include "default.h"
#include "display.h"
#include "eval.h"
#include "htmlio.h"
#include "latex.h"
#include "context.h"
#include "main.h"
#include "misc.h"
#include "sc.h"
#include "scanner.h"
#include "utf8.h"
#include "parser.h"
#include "sheet.h"
#include "wk1.h"
/*}}}*/
/* variables */ /*{{{*/
char helpfile[PATH_MAX];
int batch=0;
unsigned int batchln=0;
int def_precision=DEF_PRECISION;
int quote=0;
int header=1;
static int usexdr=1;
/*}}}*/
static void get_mark(Sheet *sheet, int *x1, int *x2, int *y1, int *y2, int *z1, int *z2, int everything)
{
if (sheet->marking) {
posorder(&sheet->mark1x, &sheet->mark2x);
posorder(&sheet->mark1y, &sheet->mark2y);
posorder(&sheet->mark1z, &sheet->mark2z);
sheet->marking = 0;
}
if (x1) {
if (sheet->mark1x >= 0) {
*x1 = sheet->mark1x;
*x2 = sheet->mark2x;
*y1 = sheet->mark1y;
*y2 = sheet->mark2y;
*z1 = sheet->mark1z;
*z2 = sheet->mark2z;
} else if (everything) {
*x1 = 0;
*x2 = sheet->dimx-1;
*y1 = 0;
*y2 = sheet->dimy-1;
*z1 = 0;
*z2 = sheet->dimz-1;
} else {
*x1 = *x2 = sheet->curx;
*y1 = *y2 = sheet->cury;
*z1 = *z2 = sheet->curz;
}
}
}
void moveto(Sheet *sheet, int x, int y, int z)
{
int need_redraw = 0;
int xdir = x > sheet->curx?1:-1;
if (x >= 0) sheet->curx = x;
if (y >= 0) sheet->cury = y;
if (z >= 0) need_redraw++, sheet->curz = z;
while (sheet->curx > 0 && shadowed(sheet, sheet->curx, sheet->cury, sheet->curz)) sheet->curx += xdir;
if (sheet->marking) {
sheet->mark2x = sheet->curx;
sheet->mark2y = sheet->cury;
sheet->mark2z = sheet->curz;
}
if (sheet->curx <= sheet->offx && sheet->offx) need_redraw++, sheet->offx = (sheet->curx?sheet->curx-1:0);
if (sheet->cury <= sheet->offy && sheet->offy) need_redraw++, sheet->offy = (sheet->cury?sheet->cury-1:0);
if (sheet->curx >= sheet->offx+sheet->maxx) need_redraw++, sheet->offx = sheet->curx-sheet->maxx+2;
if (sheet->cury >= sheet->offy+sheet->maxy) need_redraw++, sheet->offy = sheet->cury-sheet->maxy+2;
if (need_redraw) redraw_sheet(sheet);
else if (x != sheet->curx || y != sheet->cury || z != sheet->curz) redraw_cell(sheet, sheet->curx, sheet->cury, sheet->curz);
}
void relmoveto(Sheet *sheet, int x, int y, int z)
{
moveto(sheet, sheet->curx+x, sheet->cury+y, (z?sheet->curz+z:-1));
}
/* line_numedit -- number line editor function */ /*{{{*/
static int line_numedit(int *n, const char *prompt, size_t *x, size_t *offx)
{
/* variables */ /*{{{*/
char buf[20];
const char *s;
Token **t;
int c;
/*}}}*/
/* asserts */ /*{{{*/
assert(prompt!=(char*)0);
assert(x!=(size_t*)0);
assert(offx!=(size_t*)0);
/*}}}*/
t=(Token**)0;
sprintf(buf,"%d",*n);
s=buf+strlen(buf);
do
{
tvecfree(t);
*x=s-buf;
if ((c=line_edit((Sheet*)0,buf,sizeof(buf),prompt,x,offx))<0) return c;
s=buf;
t=scan(&s);
} while ((*s!='\0' && t==(Token**)0) || !(t!=(Token**)0 && ((*t)==(Token*)0 || ((*t)->type==INT && (*t)->u.integer>=0 && *(t+1)==(Token*)0))));
if (t==(Token**)0 || *t==(Token*)0) *n=-1;
else *n=(*t)->u.integer;
tvecfree(t);
return 0;
}
/*}}}*/
/* line_lidedit -- label identifier line editor function */ /*{{{*/
static int line_idedit(char *ident, size_t size, const char *prompt, size_t *x, size_t *offx)
{
/* variables */ /*{{{*/
const char *s;
Token **t;
int c;
/*}}}*/
t=(Token**)0;
s=ident+strlen(ident);
do
{
tvecfree(t);
*x=s-ident;
if ((c=line_edit((Sheet*)0,ident,size,prompt,x,offx))<0) return c;
s=ident;
t=scan(&s);
} while ((*s!='\0' && t==(Token**)0) || !(t!=(Token**)0 && ((*t)==(Token*)0 || ((*t)->type==LIDENT && *(t+1)==(Token*)0))));
tvecfree(t);
return 0;
}
/*}}}*/
/* doanyway -- ask if action should be done despite unsaved changes */ /*{{{*/
int doanyway(Sheet *sheet, const char *msg)
{
int result;
if (sheet->changed) {
result=line_ok(msg,0);
if (result < 0) return 0;
return result;
}
return 1;
}
/*}}}*/
/* do_edit -- set or modify cell contents */ /*{{{*/
static int do_edit(Sheet *cursheet, Key c, const char *expr, int clocked)
{
/* variables */ /*{{{*/
char buf[1024];
const char *s;
size_t x,offx;
int curx,cury,curz;
Token **t;
/*}}}*/
if (locked(cursheet,cursheet->curx,cursheet->cury,cursheet->curz)) line_msg(_("Edit cell:"),_("Cell is locked"));
else
{
curx=cursheet->curx;
cury=cursheet->cury;
curz=cursheet->curz;
if (expr)
{
s=expr;
t=scan(&s);
if (*s!='\0' && t==(Token**)0) line_msg(clocked ? _("Clocked cell contents:") : _("Cell contents:"),"XXX invalid expression");
}
else
{
offx=0;
if (c==K_NONE)
{
print(buf,sizeof(buf),0,1,getscientific(cursheet,cursheet->curx,cursheet->cury,cursheet->curz),-1,getcont(cursheet,cursheet->curx,cursheet->cury,cursheet->curz,clocked));
s=buf+strlen(buf);
}
else if (c==K_BACKSPACE)
{
print(buf,sizeof(buf),0,1,getscientific(cursheet,cursheet->curx,cursheet->cury,cursheet->curz),-1,getcont(cursheet,cursheet->curx,cursheet->cury,cursheet->curz,clocked));
if (strlen(buf)) *mbspos(buf+strlen(buf),-1)='\0';
s=buf+strlen(buf);
}
else if (c==K_DC)
{
print(buf,sizeof(buf),0,1,getscientific(cursheet,cursheet->curx,cursheet->cury,cursheet->curz),-1,getcont(cursheet,cursheet->curx,cursheet->cury,cursheet->curz,clocked));
memmove(buf,mbspos(buf,1),strlen(mbspos(buf,1))+1);
s=buf;
}
else if (isalpha(c))
{
buf[0] = '"';
buf[1] = c;
buf[2] = 0;
s=buf+2;
}
else
{
if (c < 256) buf[0]=c;
else buf[0] = 0;
buf[1]='\0';
s=buf+1;
}
do
{
int r;
x=mbslen(buf)-mbslen(s);
if ((r=line_edit(cursheet,buf,sizeof(buf),clocked ? _("Clocked cell contents:") : _("Cell contents:"),&x,&offx))<0) return r;
s=buf;
if (buf[0] == '"' && buf[strlen(buf)-1] != '"' && strlen(buf)+1 < sizeof(buf)) {
buf[strlen(buf)+1] = 0;
buf[strlen(buf)] = '"';
}
t=scan(&s);
} while (*s!='\0' && t==(Token**)0);
}
if (t!=(Token**)0 && *t==(Token*)0) { free(t); t=(Token**)0; }
moveto(cursheet,curx,cury,curz);
putcont(cursheet,cursheet->curx,cursheet->cury,cursheet->curz,t,clocked);
forceupdate(cursheet);
}
return 0;
}
/*}}}*/
/* do_label -- modify cell label */ /*{{{*/
static int do_label(Sheet *sheet)
{
/* variables */ /*{{{*/
char buf[1024],oldlabel[1024];
size_t edx,offx,ok;
Token t;
int x,y,z,x1,y1,z1,x2,y2,z2;
int c;
/*}}}*/
assert(sheet!=(Sheet*)0);
if (sheet->mark1x==-1 && locked(sheet,sheet->curx,sheet->cury,sheet->curz)) line_msg(_("Cell label:"),_("Cell is locked"));
else
{
get_mark(sheet, &x1, &x2, &y1, &y2, &z1, &z2, 0);
ok=edx=offx=0;
(void)strcpy(buf,getlabel(sheet,sheet->curx,sheet->cury,sheet->curz));
(void)strcpy(oldlabel,buf);
do
{
if ((c=line_idedit(buf,sizeof(buf),_("Cell label:"),&edx,&offx))<0) return c;
if (buf[0]=='\0') ok=1;
else
{
ok=((t=findlabel(sheet,buf)).type==EEK || (t.type==LOCATION && t.u.location[0]==sheet->curx && t.u.location[1]==sheet->cury && t.u.location[2]==sheet->curz));
tfree(&t);
}
} while (!ok);
setlabel(sheet,sheet->curx,sheet->cury,sheet->curz,buf,1);
if (buf[0]!='\0') for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) relabel(sheet,oldlabel,buf,x,y,z);
}
return -1;
}
/*}}}*/
/* do_columnwidth -- set the column width */ /*{{{*/
static int do_columnwidth(Sheet *cursheet)
{
/* variables */ /*{{{*/
size_t edx,offx;
int n;
int x,x1,x2,z,z1,z2;
int c;
/*}}}*/
offx=0;
edx=0;
n=columnwidth(cursheet,cursheet->curx,cursheet->curz);
do if ((c=line_numedit(&n,_("Column width:"),&edx,&offx))<0) return c; while (n<=0);
if (cursheet->mark1x==-1)
/* range is the current cell */ /*{{{*/
{
x1=x2=cursheet->curx;
z1=z2=cursheet->curz;
}
/*}}}*/
else
/* range is the marked cube */ /*{{{*/
{
x1=cursheet->mark1x; x2=cursheet->mark2x;
z1=cursheet->mark1z; z2=cursheet->mark2z;
}
/*}}}*/
for (x=x1; x<=x2; ++x) for (z=z1; z<=z2; ++z) setwidth(cursheet,x,z,n);
return -1;
}
/*}}}*/
/* do_attribute -- set cell attributes */ /*{{{*/
static void do_attribute(Sheet *cursheet, Key action)
{
/* variables */ /*{{{*/
int x,y,z;
int x1,y1,z1;
int x2,y2,z2;
int c = 0;
/*}}}*/
get_mark(cursheet, &x1, &x2, &y1, &y2, &z1, &z2, 0);
if (action != ADJUST_LOCK && cursheet->mark1x==-1 && action != ADJUST_LOCK && locked(cursheet,cursheet->curx,cursheet->cury,cursheet->curz))
{
line_msg(_("Cell attribute:"),_("Cell is locked"));
return;
}
switch ((int)action)
{
/* 0 -- adjust left */ /*{{{*/
case ADJUST_LEFT:
{
if (cursheet->mark1x != -1 && line_ok(_("Make block left-adjusted:"), 0) <= 0) break;
for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) setadjust(cursheet,x,y,z,LEFT);
break;
}
/*}}}*/
/* 1 -- adjust right */ /*{{{*/
case ADJUST_RIGHT:
{
if (cursheet->mark1x != -1 && line_ok(_("Make block right-adjusted:"), 0) <= 0) break;
for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) setadjust(cursheet,x,y,z,RIGHT);
break;
}
/*}}}*/
/* 2 -- adjust centered */ /*{{{*/
case ADJUST_CENTER:
{
if (cursheet->mark1x != -1 && line_ok(_("Make block centered:"), 0) <= 0) break;
for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) setadjust(cursheet,x,y,z,CENTER);
break;
}
/*}}}*/
/* 3 -- set scientific notation flag */ /*{{{*/
case ADJUST_SCIENTIFIC:
{
int n;
if (cursheet->mark1x==-1) n = !getscientific(cursheet,x1,y1,z1);
else n = line_ok(_("Make block notation scientific:"), getscientific(cursheet,x1,y1,z1));
if (n >= 0) for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) setscientific(cursheet,x,y,z,n);
break;
}
/*}}}*/
/* 5 -- set precision */ /*{{{*/
case ADJUST_PRECISION:
{
size_t ex,offx;
int n;
offx=0;
ex=0;
n=getprecision(cursheet,x1,y1,z1);
do if (line_numedit(&n,cursheet->mark1x==-1 ? _("Precision for cell:") : _("Precision for block:"),&ex,&offx)==-1) return; while (n!=-1 && (n==0 || n>20));
for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) setprecision(cursheet,x,y,z,n);
break;
}
/*}}}*/
/* 6 -- shadow */ /*{{{*/
case ADJUST_SHADOW:
{
int n;
if (cursheet->mark1x==-1) n = !shadowed(cursheet,x1,y1,z1);
else n = line_ok(_("Shadow block:"), shadowed(cursheet,x1,y1,z1));
if (x1 == 0 && n == 1) {
line_msg(_("Shadow cell:"),_("You can not shadow cells in column 0"));
break;
}
if (n >= 0) {
for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z)
{
int rx;
if (n==0) for (rx=x+1; shadowed(cursheet,rx,y,z); ++rx) shadow(cursheet,rx,y,z,0);
else if (x>0) shadow(cursheet,x,y,z,1);
}
}
break;
}
/*}}}*/
/* 7 -- transparent */ /*{{{*/
case ADJUST_TRANSPARENT:
{
int n;
if (cursheet->mark1x==-1) n = !transparent(cursheet,x1,y1,z1);
else n = line_ok(_("Make block transparent:"), transparent(cursheet,x1,y1,z1));
if (n >= 0) for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) maketrans(cursheet,x,y,z,n);
break;
}
/*}}}*/
/* 8 -- bold */ /*{{{*/
case ADJUST_BOLD:
{
int n;
if (cursheet->mark1x==-1) n = !isbold(cursheet,x1,y1,z1);
else n = line_ok(_("Make block bold:"), isbold(cursheet,x1,y1,z1));
if (n >= 0) for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) bold(cursheet,x,y,z,n);
break;
}
/*}}}*/
/* 9 -- underline */ /*{{{*/
case ADJUST_UNDERLINE:
{
int n;
if (cursheet->mark1x==-1) n = !underlined(cursheet,x1,y1,z1);
else n = line_ok(_("Make block underlined:"), underlined(cursheet,x1,y1,z1));
if (n >= 0) for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) underline(cursheet,x,y,z,n);
break;
}
/*}}}*/
/* 1 -- edit label and goto end */ /*{{{*/
case ADJUST_LABEL:
{
do_label(cursheet);
return;
}
/*}}}*/
/* 2 -- lock */ /*{{{*/
case ADJUST_LOCK:
{
int n;
if (cursheet->mark1x==-1) n = !locked(cursheet,x1,y1,z1);
else n = line_ok(_("Lock block:"), locked(cursheet,x1,y1,z1));
if (n >= 0) for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) lockcell(cursheet,x,y,z,n);
break;
}
/*}}}*/
/* 3 -- ignore */ /*{{{*/
case ADJUST_IGNORE:
{
int n;
if (cursheet->mark1x==-1) n = !ignored(cursheet,x1,y1,z1);
else n = line_ok(_("Ignore values of all cells in this block:"), ignored(cursheet,x1,y1,z1));
if (n >= 0) for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) igncell(cursheet,x,y,z,n);
break;
}
/*}}}*/
/* default -- should not happen */ /*{{{*/
default: assert(0);
/*}}}*/
}
if (c>=0)
{
if (cursheet->mark1x==-1) redraw_cell(cursheet, cursheet->curx, cursheet->cury, cursheet->curz);
else redraw_sheet(cursheet);
}
forceupdate(cursheet);
return;
}
/*}}}*/
/* do_savexdr -- save sheet as XDR file */ /*{{{*/
static int do_savexdr(Sheet *cursheet, const char *name)
{
char buf[PATH_MAX];
const char *msg;
unsigned int count;
if (!name) {
name = cursheet->name;
if (strcmp(name+strlen(name)-3,".tp")) {
snprintf(buf, sizeof(buf), "%s.tp", name);
name = buf;
}
}
if ((msg = savexdr(cursheet, name, &count))) {
line_msg(_("Save sheet to XDR file:"), msg);
return -2;
}
snprintf(buf, sizeof(buf), _("%u cells written"), count);
if (!batch) line_msg(_("Save sheet to XDR file:"),buf);
return -1;
}
/*}}}*/
/* do_saveport -- save sheet as portable ASCII file */ /*{{{*/
static int do_saveport(Sheet *cursheet, const char *name)
{
char buf[PATH_MAX];
const char *msg;
unsigned int count;
if (!name) name = cursheet->name;
if ((msg = saveport(cursheet, name, &count))) {
line_msg(_("Save sheet to ASCII file:"),msg);
return -2;
}
snprintf(buf, sizeof(buf), _("%u cells written"), count);
if (!batch) line_msg(_("Save sheet to ASCII file:"),buf);
return -1;
}
/*}}}*/
/* do_savetbl -- save sheet as tbl file */ /*{{{*/
static int do_savetbl(Sheet *cursheet, const char *name)
{
char buf[PATH_MAX];
const char *msg;
int standalone=0;
int x1,y1,z1,x2,y2,z2;
unsigned int count;
get_mark(cursheet, &x1, &x2, &y1, &y2, &z1, &z2, 1);
if (!name) {
name = cursheet->name;
if ((standalone=line_ok(_("Save as stand-alone document:"),1))<0) return standalone;
}
if ((msg = savetbl(cursheet, name, !standalone, x1, y1, z1, x2, y2, z2, &count))) {
line_msg(_("Save in tbl format to file:"),msg);
return -2;
}
snprintf(buf, sizeof(buf), _("%u cells written"), count);
if (!batch) line_msg(_("Save in tbl format to file:"), buf);
return -1;
}
/*}}}*/
/* do_savelatex -- save sheet as LaTeX file */ /*{{{*/
static int do_savelatex(Sheet *cursheet, const char *name)
{
char buf[PATH_MAX];
const char *msg;
int standalone=0;
int x1,y1,z1,x2,y2,z2;
unsigned int count;
get_mark(cursheet, &x1, &x2, &y1, &y2, &z1, &z2, 1);
if (!name) {
name = cursheet->name;
if ((standalone=line_ok(_("Save as stand-alone document:"),1))<0) return standalone;
}
if ((msg = savelatex(cursheet, name, !standalone, x1, y1, z1, x2, y2, z2, &count))) {
line_msg(_("Save in LaTeX format to file:"),msg);
return -2;
}
snprintf(buf, sizeof(buf), _("%u cells written"), count);
if (!batch) line_msg(_("Save in LaTeX format to file:"), buf);
return -1;
}
/*}}}*/
/* do_savecontext -- save sheet as ConTeXt file */ /*{{{*/
static int do_savecontext(Sheet *cursheet, const char *name)
{
char buf[PATH_MAX];
const char *msg;
int standalone=0;
int x1,y1,z1,x2,y2,z2;
unsigned int count;
get_mark(cursheet, &x1, &x2, &y1, &y2, &z1, &z2, 1);
if (!name) {
name = cursheet->name;
if ((standalone=line_ok(_("Save as stand-alone document:"),1))<0) return standalone;
}
if ((msg = savecontext(cursheet, name, !standalone, x1, y1, z1, x2, y2, z2, &count))) {
line_msg(_("Save in ConTeXt format to file:"),msg);
return -2;
}
snprintf(buf, sizeof(buf), _("%u cells written"), count);
if (!batch) line_msg(_("Save in ConTeXt format to file:"), buf);
return -1;
}
/*}}}*/
/* do_savehtml -- save sheet as HTML file */ /*{{{*/
static int do_savehtml(Sheet *cursheet, const char *name)
{
char buf[PATH_MAX];
const char *msg;
int standalone=0;
int x1,y1,z1,x2,y2,z2;
unsigned int count;
get_mark(cursheet, &x1, &x2, &y1, &y2, &z1, &z2, 1);
if (!name) {
name = cursheet->name;
if ((standalone=line_ok(_("Save as stand-alone document:"),1))<0) return standalone;
}
if ((msg = savehtml(cursheet, name, !standalone, x1, y1, z1, x2, y2, z2, &count))) {
line_msg(_("Save in HTML format to file:"),msg);
return -2;
}
snprintf(buf, sizeof(buf), _("%u cells written"), count);
if (!batch) line_msg(_("Save in HTML format to file:"), buf);
return -1;
}
/*}}}*/
/* do_savetext -- save sheet as formatted text file */ /*{{{*/
static int do_savetext(Sheet *cursheet, const char *name)
{
char buf[PATH_MAX];
const char *msg;
int x1,y1,z1,x2,y2,z2;
unsigned int count;
get_mark(cursheet, &x1, &x2, &y1, &y2, &z1, &z2, 1);
if (!name) name = cursheet->name;
if ((msg = savetext(cursheet, name, x1, y1, z1, x2, y2, z2, &count))) {
line_msg(_("Save in plain text format to file:"),msg);
return -2;
}
snprintf(buf, sizeof(buf), _("%u cells written"), count);
if (!batch) line_msg(_("Save in plain text format to file:"), buf);
return -1;
}
/*}}}*/
/* do_savecsv -- save sheet as CSV file */ /*{{{*/
static int do_savecsv(Sheet *cursheet, const char *name)
{
char buf[PATH_MAX];
const char *msg;
int x1,y1,z1,x2,y2,z2;
unsigned int count;
int sep = 0;
const char seps[4] = ",;\t";
get_mark(cursheet, &x1, &x2, &y1, &y2, &z1, &z2, 1);
if (!name) {
MenuChoice menu[4];
name = cursheet->name;
menu[0].str=mystrmalloc(_("cC)omma (,)")); menu[0].c='\0';
menu[1].str=mystrmalloc(_("sS)emicolon (;)")); menu[1].c='\0';
menu[2].str=mystrmalloc(_("tT)ab (\\t)")); menu[2].c='\0';
menu[3].str=(char*)0;
sep=line_menu(_("Choose separator:"),menu,0);
if (sep < 0) return sep;
}
if ((msg = savecsv(cursheet, name, seps[sep], x1, y1, z1, x2, y2, z2, &count))) {
line_msg(_("Save in CSV format to file:"),msg);
return -2;
}
snprintf(buf, sizeof(buf), _("%u cells written"), count);
if (!batch) line_msg(_("Save in CSV format to file:"), buf);
return -1;
}
/*}}}*/
/* do_loadxdr -- load sheet from XDR file */ /*{{{*/
static int do_loadxdr(Sheet *cursheet)
{
const char *msg;
if ((msg=loadxdr(cursheet,cursheet->name))!=(const char*)0) line_msg(_("Load sheet from XDR file:"),msg);
return -1;
}
/*}}}*/
/* do_loadport -- load sheet from portable ASCII file */ /*{{{*/
static int do_loadport(Sheet *cursheet)
{
const char *msg;
/*}}}*/
if ((msg=loadport(cursheet,cursheet->name))!=(const char*)0) line_msg(_("Load sheet from ASCII file:"),msg);
return -1;
}
/*}}}*/
/* do_loadsc -- load sheet from SC file */ /*{{{*/
static int do_loadsc(Sheet *cursheet)
{
const char *msg;
if ((msg=loadsc(cursheet,cursheet->name))!=(const char*)0) line_msg(_("Load sheet from SC file:"),msg);
return -1;
}
/*}}}*/
/* do_loadwk1 -- load sheet from WK1 file */ /*{{{*/
static int do_loadwk1(Sheet *cursheet)
{
const char *msg;
if ((msg=loadwk1(cursheet,cursheet->name))!=(const char*)0) line_msg(_("Load sheet from WK1 file:"),msg);
return -1;
}
/*}}}*/
/* do_loadcsv -- load/merge sheet from CSV file */ /*{{{*/
static int do_loadcsv(Sheet *cursheet)
{
const char *msg;
if ((msg=loadcsv(cursheet,cursheet->name))!=(const char*)0) line_msg(_("Load sheet from CSV file:"),msg);
return -1;
}
/*}}}*/
/* do_mark -- set mark */ /*{{{*/
void do_mark(Sheet *cursheet, int force)
{
if (force==0)
{
if (!cursheet->marking && cursheet->mark1x==-1) force=1;
else if (cursheet->marking) force=2;
else force=3;
}
switch (force)
{
case 1:
{
cursheet->mark1x=cursheet->mark2x=cursheet->curx;
cursheet->mark1y=cursheet->mark2y=cursheet->cury;
cursheet->mark1z=cursheet->mark2z=cursheet->curz;
cursheet->marking=1;
break;
}
case 2:
{
cursheet->marking=0;
break;
}
case 3:
{
cursheet->mark1x=-1;
break;
}
default: assert(0);
}
}
/*}}}*/
static int do_name(Sheet *cursheet);
/* do_save -- save sheet */ /*{{{*/
static int do_save(Sheet *cursheet)
{
const char *ext = cursheet->name;
if (ext==(char*)0) return do_name(cursheet);
ext += strlen(ext)-1;
if (!strcmp(ext-3, ".tpa")) return do_saveport(cursheet, NULL);
if (!strcmp(ext-3, ".tbl")) return do_savetbl(cursheet, NULL);
if (!strcmp(ext-5, ".latex")) return do_savelatex(cursheet, NULL);
if (!strcmp(ext-4, ".html")) return do_savehtml(cursheet, NULL);
if (!strcmp(ext-3, ".csv")) return do_savecsv(cursheet, NULL);
if (!strcmp(ext-3, ".txt")) return do_savetext(cursheet, NULL);
if (!strcmp(ext-3, ".tex")) return do_savecontext(cursheet, NULL);
return do_savexdr(cursheet, NULL);
}
/*}}}*/
/* do_name -- (re)name sheet */ /*{{{*/
static int do_name(Sheet *cursheet)
{
const char *name;
name = line_file(cursheet->name, _("Teapot \t*.tp\nTeapot ASCII \t*.tpa\ntbl \t*.tbl\nLaTeX \t*.latex\nHTML \t*.html\nCSV \t*.csv\nFormatted ASCII \t*.txt\nConTeXt \t*.tex"), _("New file name:"), 1);
if (!name) return -1;
if (cursheet->name!=(char*)0) free(cursheet->name);
cursheet->name=strdup(name);
return do_save(cursheet);
}
/*}}}*/
/* do_load -- load sheet */ /*{{{*/
static int do_load(Sheet *cursheet)
{
const char *name, *ext;
if (doanyway(cursheet, _("Sheet modified, load new file anyway?")) != 1) return -1;
name = line_file(cursheet->name, _("Teapot \t*.tp\nTeapot ASCII \t*.tpa\nSC Spreadsheet Calculator \t*.sc\nLotus 1-2-3 \t*.wk1\nCSV \t*.csv"), _("Load sheet:"), 0);
if (!name) return -1;
if (cursheet->name!=(char*)0) free(cursheet->name);
cursheet->name=strdup(name);
ext = name+strlen(name)-1;
if (!strcmp(ext-3, ".tpa")) return do_loadport(cursheet);
if (!strcmp(ext-2, ".sc")) return do_loadsc(cursheet);
if (!strcmp(ext-3, ".wk1")) return do_loadwk1(cursheet);
if (!strcmp(ext-3, ".csv")) return do_loadcsv(cursheet);
return do_loadxdr(cursheet);
}
/*}}}*/
/* do_clear -- clear block */ /*{{{*/
static int do_clear(Sheet *sheet)
{
/* variables */ /*{{{*/
int x,y,z;
int x1,y1,z1;
int x2,y2,z2;
int c;
/*}}}*/
if (sheet->mark1x==-1 && locked(sheet,sheet->curx,sheet->cury,sheet->curz)) line_msg(_("Clear cell:"),_("Cell is locked"));
else
{
if (sheet->mark1x!=-1)
{
if ((c=line_ok(_("Clear block:"),0))<0) return c;
else if (c!=1) return -1;
}
get_mark(sheet, &x1, &x2, &y1, &y2, &z1, &z2, 0);
for (x=x1; x<=x2; ++x) for (y=y1; y<=y2; ++y) for (z=z1; z<=z2; ++z) freecell(sheet,x,y,z);
cachelabels(sheet);
forceupdate(sheet);
}
return -1;
}
/*}}}*/
/* do_insert -- insert block */ /*{{{*/
static int do_insert(Sheet *sheet)
{
/* variables */ /*{{{*/
int x1,y1,z1,x2,y2,z2,reply;
/*}}}*/
/* ask for direction of insertation */ /*{{{*/
{
MenuChoice menu[4];
menu[0].str=mystrmalloc(_("cC)olumn")); menu[0].c='\0';
menu[1].str=mystrmalloc(_("rR)ow")); menu[1].c='\0';
menu[2].str=mystrmalloc(_("dD)epth")); menu[2].c='\0';
menu[3].str=(char*)0;
reply=line_menu(_("Insert:"),menu,0);
free(menu[0].str);
free(menu[1].str);
free(menu[2].str);
if (reply<0) return reply;
}
/*}}}*/
if (sheet->mark1x==-1)
/* ask if current cell or whole dimension should be used */ /*{{{*/
{
/* variables */ /*{{{*/
MenuChoice menu[3];
int r;
/*}}}*/
/* show menu */ /*{{{*/
switch (reply)
{
case 0: menu[0].str=mystrmalloc(_("wW)hole column")); break;
case 1: menu[0].str=mystrmalloc(_("wW)hole line")); break;
case 2: menu[0].str=mystrmalloc(_("wW)hole sheet")); break;
default: assert(0);
}
menu[1].str=mystrmalloc(_("sS)ingle cell")); menu[1].c='\0';
menu[2].str=(char*)0;
r=line_menu(_("Insert:"),menu,0);
free(menu[0].str);
free(menu[1].str);
/*}}}*/
switch (r)
{
/* 0 -- use whole dimension */ /*{{{*/
case 0:
{
switch (reply)
{
/* 0 -- use whole column */ /*{{{*/
case 0:
{
x1=x2=sheet->curx;
y1=0; y2=sheet->dimy;
z1=z2=sheet->curz;
break;
}
/*}}}*/
/* 1 -- use whole line */ /*{{{*/
case 1:
{
x1=0; x2=sheet->dimx;
y1=y2=sheet->cury;
z1=z2=sheet->curz;
break;
}
/*}}}*/
/* 2 -- use whole layer */ /*{{{*/
case 2:
{
x1=0; x2=sheet->dimx;
y1=0; y2=sheet->dimy;
z1=z2=sheet->curz;
break;
}
/*}}}*/
/* default -- should not happen */ /*{{{*/
default: assert(0);
/*}}}*/
}
break;
}
/*}}}*/
/* 1 -- use current cell */ /*{{{*/
case 1:
{
x1=x2=sheet->curx;
y1=y2=sheet->cury;
z1=z2=sheet->curz;
break;
}
/*}}}*/
/* -2,-1 -- go up or abort */ /*{{{*/
case -2:
case -1: return r;
/*}}}*/
/* default -- should not happen */ /*{{{*/
default: assert(0);
/*}}}*/
}
}
/*}}}*/
else
/* range is the marked cube */ /*{{{*/
{
get_mark(sheet, &x1, &x2, &y1, &y2, &z1, &z2, 0);
}
/*}}}*/
switch (reply)
{
/* 0 -- columns */ /*{{{*/
case 0: insertcube(sheet,x1,y1,z1,x2,y2,z2,IN_X); break;
/*}}}*/
/* 1 -- rows */ /*{{{*/
case 1: insertcube(sheet,x1,y1,z1,x2,y2,z2,IN_Y); break;
/*}}}*/
/* 2 -- depth */ /*{{{*/
case 2: insertcube(sheet,x1,y1,z1,x2,y2,z2,IN_Z); break;
/*}}}*/
}
return 0;
}
/*}}}*/
/* do_delete -- delete block */ /*{{{*/
static int do_delete(Sheet *sheet)
{
/* variables */ /*{{{*/
int x1,y1,z1,x2,y2,z2,reply;
/*}}}*/
firstmenu:
/* ask for direction of deletion */ /*{{{*/
{
MenuChoice menu[4];
menu[0].str=mystrmalloc(_("cC)olumn")); menu[0].c='\0';
menu[1].str=mystrmalloc(_("rR)ow")); menu[1].c='\0';
menu[2].str=mystrmalloc(_("dD)epth")); menu[2].c='\0';
menu[3].str=(char*)0;
reply=line_menu(_("Delete:"),menu,0);
free(menu[0].str);
free(menu[1].str);