-
Notifications
You must be signed in to change notification settings - Fork 13
/
tamgucalc_mc.tmg
2260 lines (2098 loc) · 62.4 KB
/
tamgucalc_mc.tmg
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
/@
Project : Spreadsheet
filename : tamgucalc.tmg
Date : 2020/06/15
Purpose : spreadsheet for terminals with Lisp expressions
Programmer : Claude ROUX ([email protected])
@/
/@
Help:
This spreadsheet accepts different sorts of input:
a) Lisp expressions, which should start with a '('
b) Regular formulas, which should start with a '=' and ends with a ';'
c) Long strings, which are introduced with "'"
d) Float numbers, which you input directly
@/
if (version() < "Tamgu 1.2020.07.03.10") {
_sys.cls();
println();
println("Please, accept all our apologies...");
println("Alas, your version of Tamgu is too old to run 'tamgucalc'...");
println("We needed to adjust tamgu to run some of the new stuff in tamgucalc...");
print("Please update it at: ");
_sys.colors(7,31,107);
print("https://github.com/naver/tamgu/releases");
_sys.colors(0,0,0);
println("\n");
_exit();
}
//USE -b to switch to black mode
bool chooseblack = false;
//displaying indexes
bool displayindexes = true;
//displaying the message zone
bool displayingmessage = true;
if (_OS == "WINDOWS")
chooseblack = true;
//The default value for cells
string defaultvalue = ".";
//The Lisp interpreter
lisp lisp_interpreter;
string msgerr;
//Color definition to highlight parenthesis balancing
string colorparenth = _sys.colors(7,31,49, false);
string colorsel = _sys.colors(1, 31, 49, false);
string colorform = _sys.colors(1,30,103, false);
string colornrm = _sys.colors(0,0,0,false);
mapsf definedvalues = {"_pi ":_pi, "_tau ":_tau, "_phi ":_phi, "_e ":_e };
//We keep 6 lines below the grid for input and message display
int inputsection = 6;
//Initialisation: coords contains your terminal dimensions
ivector coords = _sys.screensize();
//columnsize is the size of a column in character. You can modify this value
int columnsize = 10;
//We initialise the number of lines and columns according to your terminal size and columnsize
//We keep 'inputsection' lines below the last line to display messages
int x_viewsize = coords[0]-inputsection;
//The number of columns depends on the number of columns on the screen / the chosen columnsize
int y_viewsize = coords[1]/columnsize;
//We keep these values for comparison when the size is modified
int x_maxview = x_viewsize;
int y_maxview = y_viewsize;
//These two values define the actual matrix size, which is different from viewsize
//If the view port is larger than 101 and 51, then the matrix is equal to the viewport
int x_max = max(257,x_maxview);
int y_max = max(33,y_maxview);
//codeline is used to position messages
//'inputsection' lines down after the last line of the matrix
int codeline = x_viewsize+inputsection;
//Actual boundaries of the table
//These bounderies define the square in which values have been entered
int x_bound=0;
int y_bound=0;
//v_matrix is the visualisation matrix
vector v_matrix;
//mat is the actual matrix in which computing is done...
fmatrix mat(x_max,y_max);
//formulas records the different Lisp expressions
mapss formulas;
mapss regularformulas;
mapss referenceformulas;
map functions;
//the line 0 and the column 0 are used to display coordinates...
int i = 1;
int j = 1;
int I, J;
//Since, the matrix might be larger than the viewport,
//we need to record the offsets to position 0,0
int off_x = 0;
int off_y = 0;
bool modifying = false;
//----------------------------------------------------------------------
//inputs is used to handle modifications on: filenames and size.
//If you want to enrich tamgucalc, you might add a new key
mapss inputs;
string inputkey;
string inputvalue;
inputs["Size"] = ""+(x_viewsize-1)+":"+(y_viewsize-1);
inputs["Go"] = "1,1";
inputs["Column"] = columnsize;
string option;
//exploring the arguments:
//possible values are: -i, -n, -b and a filename
for (string e in _args) {
if (_args[e] == "-h") {
_sys.showcursor(true);
println();
println("-i: hide the indexes / -n: hide the messages / -b: black screen mode / file");
println();
_exit();
}
if (_args[e] == "-i") {
displayindexes = false;
}
elif (_args[e] == "-n") {
displayingmessage = false;
_sys.showcursor(false);
}
elif (_args[e] == "-b") {
chooseblack = true;
}
else {
option = e;
}
}
inputs["File"] = option;
if (option != "")
inputs["Data"] = option+".csv";
else
inputs["Data"] = "";
//----------------------------------------------------------------------
// Resizing Event
//----------------------------------------------------------------------
bool resized = true;
function resizing(int r, int c) {
//We only resize, if no manual resizing has been done
if (!resized)
return;
x_viewsize = r - inputsection;
y_viewsize = c / columnsize;
codeline = x_viewsize + inputsection;
x_maxview = x_viewsize;
y_maxview = y_viewsize;
//These two values define the actual matrix size, which is different from viewsize
//If the view port is larger than 101 and 51, then the matrix is equal to the viewport
_sys.cls();
displayall(off_x, off_y);
showelement(i, j, off_x, off_y);
}
if (version() >= "Tamgu 1.2020.07.14.11") {
//This for the case people are still using an old version of Tamgu
//that does not provide resizing
_eval("_sys.resizecallback(resizing);");
}
//----------------------------------------------------------------------
// PREDEFINED METHODS (You can add your own here)
// You can use loadin as well: loadin(pathname)
// To load your own functions from an external file
//----------------------------------------------------------------------
//A few predefined methods
//IMPORTANT: When you define Lisp Functions in regular code
//The first "(" must be preceded with a "\"
\(defun average (x) (/ (sum x) (size x)))
\(defun standard_deviation(x)
(setq avg (average x))
(sqrt (/
(sum
(_map (lambda (x)
(* (- x avg) (- x avg)))
x)
)
(size x)
)
)
)
//Same with taskell syntax...
function std_var(fvector v) {
float avg = v.sum()/v.size();
return <sqrt </ <sum . map (\x->(x-avg)*(x-avg)) v> <size v>>>;
}
//You can call a regular function from a Lisp formula:
// (fillcolumn mat[:2][1:6] 1 10): fill column 10, starting at row 1 with values from mat[:2][1:6]
//fill a row with values, column c is fixed, it defines the starting point
function fillcolumn(vector v, int r, int c) {
int vi = 0;
r = max(1,r);
c = max(1,c);
r = min(r,x_max);
c = min(c,y_max);
for (int i in <r, r+v.size()>) {
v_matrix[i][c] = v[vi];
dispscreen(i, c, off_x, off_y);
mat[i][c] = float(v[vi]);
vi++;
}
return ("C"+r+","+c);
}
//fill a column with values, row r is fixed, it defines the starting point
function fillrow(vector v, int r, int c) {
int vi = 0;
r = max(1,r);
c = max(1,c);
r = min(r,x_max);
c = min(c,y_max);
for (int i in <c, c+v.size()>) {
v_matrix[r][i] = v[vi];
dispscreen(r, i, off_x, off_y);
mat[r][i] = float(v[vi]);
vi++;
}
return ("R"+r+","+c);
}
//Return all values up to a certain value: (from m[:1] 0)
function upto(fvector v, float val) {
int vi = val in v;
if (vi == -1)
return [];
//If the value is not found, the list is empty...
return (v[:vi]);
}
//------------------------------------------------------------------
//This function is used to replace specific labels with their UTF8 symbol
function compactstring(string s, int p) {
if (s[p-7:p] == "lambda ")
s[p-7:p] = "λ ";
elif (s[p-5:p] == "sqrt ")
s[p-5:p] = "√ ";
elif (s[p-5:p] == "cbrt ")
s[p-5:p] = "∛ ";
elif (s[p-4:p] == "_pi ")
s[p-4:p] = "π ";
elif (s[p-5:p] == "_tau ")
s[p-5:p] = "τ ";
elif (s[p-5:p] == "_phi ")
s[p-5:p] = "φ ";
elif (s[p-3:p] == "_e ")
s[p-3:p] = "ℯ ";
elif (s[p-2:p] == "/ ")
s[p-2:p] = "÷ ";
elif (s[p-4:p] == "**2 ")
s[p-4:p] = "²";
elif (s[p-4:p] == "**3 ")
s[p-4:p] = "³";
elif (s[p-3:p] == "** ")
s[p-3:p] = "**";
elif (s[p-2:p] == "* ")
s[p-2:p] = "× ";
elif (s[p-4:p] =="^^2 ")
s[p-4:p] = "²";
elif (s[p-4:p] =="^^3 ")
s[p-4:p] = "³";
return s;
}
function formulacolor() {
if (chooseblack)
_sys.colors(0,33,40);
else
_sys.colors(0,39,47);
}
function indexcolor() {
if (chooseblack)
_sys.colors(1,36,40);
else
_sys.colors(7,96,40);
}
function selectioncolor() {
if (chooseblack)
_sys.colors(1,31,40);
else
_sys.colors(7,31,49);
}
function defuncolor() {
if (chooseblack)
_sys.colors(1,92,49);
else
_sys.colors(7,94,107);
}
function stringcolor() {
if (chooseblack)
_sys.colors(1,92,49);
else
_sys.colors(0,94,49);
}
function normalcolor() {
_sys.colors(0,0,0);
}
//initialisation of v_matrix
function initialisation() {
int i,j;
for (i in <x_max>) {
v_matrix[i] = [];
for (j in <y_max>) {
mat[i:j] = 0;
if (!i) {
v_matrix[i].push(string(j));
}
elif (!j) {
v_matrix[i].push(string(i));
}
else {
v_matrix[i].push(defaultvalue);
}
}
}
}
//Loading a file
function readtable(string f) {
resized = true;
int i,j;
for (i in <1,x_max>) {
for (j in <1,y_max>) {
v_matrix[i][j] = defaultvalue;
mat[i:j] = 0;
}
}
try {
svector sv;
svector cut;
int nb,xm, ym;
string s,k,e;
sv.read(f);
bool beg = true;
i=0;
float val;
for (s in sv) {
cut=s.split("\t");
if (beg == true) {
//we have stored some parameters in the three first cells
x_viewsize = cut[0];
y_viewsize = cut[1];
columnsize = cut[2];
xm = cut[3];
ym = cut[4];
if (xm != x_max or ym != y_max)
resized = false;
x_max = xm;
y_max = ym;
x_bound = cut[5];
y_bound = cut[6];
inputs["Size"] = ""+(x_viewsize-1)+":"+(y_viewsize-1);
inputs["Column"] = columnsize;
codeline = x_viewsize+inputsection;
mat.dimension(x_max,y_max);
beg=false;
//we need to reset them
cut[0] = '0';
cut[1] = '1';
cut[2] = '2';
cut[3] = '3';
cut[4] = '4';
cut[5] = '5';
cut[6] = '6';
}
j = 0;
for (e in cut) {
e=e.trim();
if (e[0] == "(") {
k = i+"_"+j;
formulas[k] = e;
v_matrix[i][j] = defaultvalue;
mat[i:j] = 0;
}
elif (e[0] == '=' and e[-1] == ";") {
k = i+"_"+j;
regularformulas[k] = e;
v_matrix[i][j] = defaultvalue;
mat[i:j] = 0;
}
else {
v_matrix[i][j] = e;
if (isdigit(e[0]) or e[0]=='-') {
val = e;
mat[i:j] = val;
}
}
j++;
}
i++;
}
displayall(0,0);
evaluation(0,0);
}
catch {
initialisation();
displayall(0,0);
}
}
//Writing the visual matrix content to a file
function writetable(string f) {
if (f=="") {
displaymessage("Abort");
return;
}
string k;
int x_bound = x_bound;
int y_bound=max(y_bound,7);
displaymessage("Writing: "+f);
file sv(f,"w");
int i,j;
string w;
v_matrix[0][0] = string(x_viewsize);
v_matrix[0][1] = string(y_viewsize);
v_matrix[0][2] = string(columnsize);
v_matrix[0][3] = string(x_max);
v_matrix[0][4] = string(y_max);
v_matrix[0][5] = string(x_bound);
v_matrix[0][6] = string(y_bound);
for (i in <x_bound>) {
for (j in <y_bound>) {
if (j)
sv.write("\t");
k = i+"_"+j;
if (formulas.test(k))
sv.write(formulas[k]);
elif (regularformulas.test(k))
sv.write(regularformulas[k]);
else {
w = v_matrix[i][j];
sv.write(w.trim());
}
}
sv.write("\n");
}
v_matrix[0][0] = "0";
v_matrix[0][1] = "1";
v_matrix[0][2] = "2";
v_matrix[0][3] = "3";
v_matrix[0][4] = "4";
v_matrix[0][5] = "5";
v_matrix[0][6] = "6";
}
//Writing only data to a CSV file. The result of formulas is stored
function writecsv(string f) {
if (f=="") {
displaymessage("Abort");
return;
}
if ('.csv' not in f)
f+=".csv";
displaymessage("Writing (csv): "+f);
file sv(f,"w");
int i,j;
string cell;
for (i in <1,x_bound>) {
for (j in <1,y_bound>) {
if (j!=1)
sv.write("\t");
cell = v_matrix[i][j].trim();
if (cell[-1] == '!')
cell=cell[:-1];
if (!cell[0].isdigit() and cell[0] != '-') {
if (cell[0] == "'")
cell=cell[1:];
sv.write(cell.json());
}
else
sv.write(cell);
}
sv.write("\n");
}
}
//------------------- The Code ---------
function replacewithfloat(string str) {
ivector iv = r"%d+(.%d+(e({-+})%d+))" in str;
string s;
int b,e;
for (int i in <iv.size()-1,0,-2>) {
b = iv[i-1];
e = iv[i];
s = str[b:e];
if ("." in s)
continue;
if (str[e] in " );" and not str[b-1].isalpha()) {
str[b:e] = s+".0";
}
}
//this is a piece of code for intervals, where
//the the final index should be one more...
svector coord = r"%[?+%]%[%d+:%d+%]" in str;
for (s in coord) {
//We look for the last number in the string with a ":"
b = 1+s[-r":%d+"][1:];
str[s] = s[:-":"]+b+"]";
}
return str;
}
function evaluation(int off_x, int off_y) {
if (formulas.size()==0)
return;
string ky;
int i,j;
msgerr="";
string msg;
self val;
float value;
bool stop=false;
string recompiled;
string cell;
int r;
//We record functions beforehand
for (ky in formulas) {
cell = formulas[ky];
try {
i = ky[:"_"][:-1];
j = ky["_":][1:];
if (referenceformulas[ky] != cell) {
recompiled = "Recompiled: "+ky;
referenceformulas[ky] = cell;
//We only reaevaluate a function, if it has been modified
if ("defun " in cell) {
v_matrix[i][j] = "#"+cell[" ":"("][:-1].trim();
mat[i][j] = 0;
lisp_interpreter.eval(cell);
}
else {
//We create a function on the fly, to avoid recompiling again and again
//the code...
//We have replaced lambda with λ for compactness reason, we need to
//replace it back to compile it...
cell = cell.replace("λ","lambda");
cell = replacewithfloat(cell);
cell = "(defun F_"+ky+"() " + cell+")";
//this is a piece of code for intervals, where
//the the final index should be one more...
functions[ky] = lisp_interpreter.eval(cell);
}
}
}
catch(msg) {
msgerr=msg;
//We do not engage then in computing
ky["_"] = ",";
if ("at line" in msgerr)
msgerr[" at line":] = "at cell: "+ky;
else
msgerr += " at cell: "+ky;
v_matrix[i][j] = "#FCERR";
mat[i][j] = 0;
dispscreen(i, j, off_x, off_y);
}
}
int loop;
string fnct;
call Fnct;
int mx = max(regularformulas.size(), formulas.size());
//The top loop is due to the interdependencies between the formulas
while (!stop) {
//If we cannot get out of the main loop, it means trouble...
//One of the formulas is in cross-dependency with another...
//F1 needs F2 to compute and F2 needs also F1... stalemate...
if (loop > mx)
break;
stop=true;
loop++;
for (ky in regularformulas) {
try {
//We remove the "=" and the ";"
cell=regularformulas[ky].trim();
i = ky[:"_"][:-1];
j = ky["_":][1:];
if (cell[0] == '=' and cell[-1] == ';') {
cell=cell[1:-1];
cell = replacewithfloat(cell);
val = _eval(cell);
if (val.float() != mat[i:j])
stop = false;
mat[i:j] = val.float();
v_matrix[i][j] = val.string();
v_matrix[i][j]+=";";
dispscreen(i, j, off_x, off_y);
}
else {
msgerr=msg;
ky["_"] = ",";
msgerr += "Incomplete formula at cell: "+ky;
v_matrix[i][j] = "#ERR;";
mat[i][j] = 0;
dispscreen(i, j, off_x, off_y);
}
}
catch(msg) {
msgerr=msg;
ky["_"] = ",";
if ("at line" in msgerr)
msgerr[" at line":] = "at cell: "+ky;
else
msgerr += " at cell: "+ky;
v_matrix[i][j] = "#ERR;";
mat[i][j] = 0;
dispscreen(i, j, off_x, off_y);
}
}
for (ky in formulas) {
//functions are skipped
if ("defun " in formulas[ky])
continue;
//We have recorded formulas as function: F_row_col
cell="(F_"+ky+")";
//We extract the cell position, where the formula is stored
i = ky[:"_"][:-1];
j = ky["_":][1:];
try {
Fnct = functions[ky];
val = Fnct();
if (val.float() != mat[i:j])
stop = false;
mat[i:j] = val.float();
v_matrix[i][j] = val.string();
v_matrix[i][j]+="!";
dispscreen(i, j, off_x, off_y);
}
catch(msg) {
msgerr=msg;
ky["_"] = ",";
if ("at line" in msgerr)
msgerr[" at line":] = "at cell: "+ky;
else
msgerr += " at cell: "+ky;
v_matrix[i][j] = "#ERR";
mat[i][j] = 0;
dispscreen(i, j, off_x, off_y);
}
}
}
cell="";
//displayall(off_x, off_y);
if (recompiled != "") {
if (msgerr != "")
msgerr += " "+recompiled;
else
msgerr = recompiled;
}
if (msgerr)
displayerr(msgerr);
}
//Display methods
//Display one element, we check its size
function dispelementregular(int i,int j, int off_x, int off_y) {
string u = v_matrix[i+off_x][j+off_y];
if (u[0] == "'") {
print(u);
return;
}
if (u.size() >= columnsize-1)
u=u[:columnsize-2]+"_";
print(u);
}
function dispelement(int i,int j, int off_x, int off_y) {
string u = v_matrix[i+off_x][j+off_y];
if (u[0] == "'") {
print(u);
return;
}
if (u[-1] == "!" or u[-1] == ';')
formulacolor();
elif (u[0] == "#")
defuncolor();
elif (!u[0].isdigit() && u[0] != "-" && u != defaultvalue)
stringcolor();
if (u.size() >= columnsize-1)
u=u[:columnsize-2]+"_";
print(u);
normalcolor();
}
//Disply the element if it is on screen
function dispscreen(int x, int y, int off_x, int off_y) {
if (x <= off_x or x >= (x_viewsize + off_x) or y <= off_y or y >= (y_viewsize + off_y))
return;
//We are in the viewport, we can display...
_sys.row_column(x - off_x + 1, (y - off_y)*columnsize);
string u = v_matrix[x][y];
if (u[0] == "'") {
print(u);
return;
}
if (u[-1] == "!" or u[-1] == ';')
formulacolor();
elif(u[0] == "#")
defuncolor();
elif(!u[0].isdigit() && u[0] != "-" && u != defaultvalue)
stringcolor();
if (u.size() >= columnsize - 1)
u = u[:columnsize - 2] + "_";
print(u);
normalcolor();
int lg = columnsize-u.size()-1;
if (lg > 0) {
u="";
u.padding(lg, " ");
print(u);
}
}
function clearelement(int i, int j,int off_x, int off_y) {
_sys.row_column(i+1,columnsize*j);
string u = v_matrix[i+off_x][j+off_y];
if (u[0] == "'") {
print(u);
return;
}
if (u[-1] == "!" or u[-1] == ';')
formulacolor();
elif (u[0] == "#")
defuncolor();
elif (!u[0].isdigit() && u[0] != "-" && u != defaultvalue)
stringcolor();
if (u.size() >= columnsize-1)
u=u[:columnsize-2]+"_";
print(u);
normalcolor();
}
function displayline(int off_x, int off_y, int row) {
if (displayindexes) {
_sys.row_column(row+1,1);
indexcolor();
dispelementregular(row,0, off_x, 0);
normalcolor();
}
for (int jcol in <1,y_viewsize>) {
_sys.row_column(row+1,columnsize*jcol);
dispelement(row,jcol, off_x, off_y);
}
}
//Displaying all elements on screen
function displayall(int off_x, int off_y) {
int i,j;
_sys.cls();
if (displayindexes) {
indexcolor();
//display column numbers
for (i in <x_viewsize>) {
_sys.row_column(i+1,1);
dispelementregular(i,0,off_x,0);
}
//display line numbers
for (j in <y_viewsize>) {
_sys.row_column(1,columnsize*j);
dispelementregular(0,j,0,off_y);
}
_sys.row_column(1,1);
print("T");
normalcolor();
print(" ");
}
for (i in <1,x_viewsize>) {
for (j in <1,y_viewsize>) {
_sys.row_column(i+1,columnsize*j);
dispelement(i,j, off_x, off_y);
}
}
}
function clearzonemessage() {
_sys.row_column(codeline-5,0);
_sys.eraseline(2);
_sys.row_column(codeline-4,0);
_sys.eraseline(2);
_sys.row_column(codeline-3,0);
_sys.eraseline(2);
_sys.row_column(codeline-2,0);
_sys.eraseline(2);
_sys.row_column(codeline-1,0);
_sys.eraseline(2);
_sys.row_column(codeline,0);
_sys.eraseline(2);
}
//Error message is displayed on line 4
function displayerr(string s) {
clearzonemessage();
if (displayingmessage) {
_sys.row_column(codeline-4,0);
print(s);
}
else
_sys.row_column(1,1);
}
//Messages are displayed on line 3
function displaymessage(string s) {
clearzonemessage();
if (displayingmessage) {
_sys.row_column(codeline-3,0);
print(s);
}
else
_sys.row_column(1,1);
}
function displayhelp(string s) {
clearzonemessage();
_sys.row_column(codeline-4,0);
print(s);
}
function displayprompt(string s) {
if (displayingmessage) {
_sys.row_column(codeline-1,0);
_sys.eraseline(2);
print(s);
}
else
_sys.row_column(1,1);
}
function pureselection(int i, int j, int off_x, int off_y) {
_sys.row_column(i+1,columnsize*j);
//We use a specific color to show that it has been selected
selectioncolor();
dispelementregular(i,j, off_x, off_y);
//Colors are reset, we then display on the last line the current value
normalcolor();
}
//Highlight the current element
function showelement(int i, int j, int off_x, int off_y) {
//We check if we are in a formula mode
int I = i+off_x;
int J = j+off_y;
string display = I+","+J+" "+inputkey+": "+inputvalue;
//we use spaces to clean the cell first
int lg = columnsize - v_matrix[I][J].size()-1;
//Then we position our cursor again to display our value
_sys.row_column(i+1,columnsize*j);
//We use a specific color to show that it has been selected
selectioncolor();
dispelementregular(i,j, off_x, off_y);
//Colors are reset, we then display on the last line the current value
normalcolor();
if (lg > 0) {
string space;
space.padding(lg, " ");
print(space);
}
displayprompt(display);
}
//----------------------------------------------------------------------------------------------------
//MAIN LOOP
//If a filename is provided, we try to read the file, otherwise we initialise our structure
initialisation();
if (inputs["File"]=="")
displayall(0,0);
else
readtable(inputs["File"]);
string msgform="%1Arrows%2 select / %1Enter%2 to add cell / %1..%2 or %1: %2 define range / %1Balance parentheses%2 to exit;";
msgform=msgform.format(colorsel,colornrm);
string helpmsg = @"%1Ctrl-s%2: Save / %1Ctrl-w%2: Save as / %1Ctrl-f%2: Save Data / %1Ctrl-l%2: Load / %1Ctrl-g%2: Goto
%1Ctrl-r%2: Grid Size / %1Ctrl-t%2: Column Size / %1Ctrl-d%2: Delete / %1Ctrl-k%2: Copy / %1Ctrl-x%2: Move
%1Ctrl-i%2: Hide Indexes / %1Ctrl-n%2: Hide Message
%1_pi%2: π / %1_tau%2: τ / %1_phi%2: φ / %1_e%2: ℯ / %1sum%2 / %1product%2 / %1(..)%2: Lisp formula / %1=..;%2: regular formula"@;
helpmsg = helpmsg.format(colorsel,colornrm);
string msgbase = "%1Ctrl-u%2: Help / %1Ctrl-e%2: Edit / %1Ctrl-b%2: Black Mode / %1Ctrl-q%2: Quit";
msgbase = msgbase.format(colorsel,colornrm);
displaymessage(msgbase);
string msgedit = "%1Edit Mode%2... %1Esc%2=abort / %1Enter%2=record";
msgedit = msgedit.format(colorsel,colornrm);
//Triggering mouse action
//The _sys.reset() deactivates mouse action
_sys.inmouse();
int posinstring;
string dsp;
int firstclickx, firstclicky;
int lastselx,lastsely;
bool tracking=false;
i = 1;
j = 1;
string ky = i+"_"+j;
string backup;
int formi, formj, sz;
bool buildingformula=false;
bool field = false;
bool isselected = false;
string currentkey;
string currentvalue;
string s;
function handlingarrows(string s) {
int bi = i;
int bj = j;
//this is the section for editing values on prompt line
if (modifying) {
if (s == _sys_keyright)
posinstring++;
elif (s == _sys_keyleft)
posinstring--;
else
return false;
if (posinstring > inputvalue.size()+1)
posinstring = inputvalue.size()+1;
if (posinstring <= 0)
posinstring = 1;
return true;
}
int ei = i;
int ej = j;
if (s == _sys_keyright)
j++;
elif (s == _sys_keyleft)
j--;
elif (s == _sys_keyup)
i--;
elif (s == _sys_keydown)
i++;
else
return false;
//moving the cursor in the grid
//We clear the selection for the current element
clearelement(ei, ej, off_x, off_y);
bool redisplay=false;
int scroll=0;
if (i < 1) {
i=1;
if (off_x > 0) {
off_x--;
scroll=-1;
}
}
else {
if (i >= x_viewsize) {
i = x_viewsize-1;
if ((off_x+i) < x_max-1) {