-
Notifications
You must be signed in to change notification settings - Fork 8
/
lab.ijs
418 lines (409 loc) · 8.13 KB
/
lab.ijs
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
coclass 'jlab'
LABFILE=: LABDEF=: ''
LABVER=: 1
LAB805=: '~addons/labs/labs/labs805.ijs'
isinteger=: (-: <.) ::0:
matches=: <@[ = #@[ {.each ]
plurals=: ] , (1: ~: [) # 's'"_
toLF=: }. @ ; @: (LF&, each)
assert=: 3 : 0
'' assert y
:
if. -. 0 e. y do. return. end.
msg=. x
if. 0=#msg do.
j=. 'There is a problem with the lab.',LF,LF
msg=. j,'Try re-loading J and starting it again.'
end.
echo msg
laberror=. 13!:8@1:
laberror''
)
closegraphics=: 3 : 0
w=. wdforms''
w=. w #~ ({."1 w) e. ;:'plot viewmat'
if. 0 = #w do. EMPTY return. end.
nms=. ~.{."1 w
if. 1 < #nms do. wnd=. 'graphics' else. wnd=. 0 pick nms end.
echo LF,'Closing ',wnd,' window',((1<#w) # 's'),'...',LF
if. (<'viewmat') e. nms do. closeall_jviewmat_'' end.
w=. w #~ ({."1 w) = <'plot'
{{ wd 'psel ',y,';pclose' }} each 1{"1 w
EMPTY
)
closeviewmat=: 3 : 0
closeall_jviewmat_ ::] ''
EMPTY
)
delnb=: 3 : 0
y }.~ 3 + 'NB. ' -: 4 {. y
)
getkey=: 3 : 0
hdr=. 0 pick y
tag=. (1 + hdr i. ':') {. hdr
typ=. 6 { tag
ndx=. 1 { I. tag matches y
key=. typ, toLF }. ndx {. y
len=. ndx+1
dat=. len }. y
key;dat;len
)
gettitle=: 3 : 0
txt=. (<0;1) {:: LABDEF
if. 'Lab:' -: 4 {.txt do.
deb 4 }. txt
else.
LABFILE
end.
)
getversion=: 3 : 0
dat=. a: -.~ 'b' fread y
if. 0=#dat do. 1 return. end.
a=. 'LABTITLE=:' -: 10 {. (0 pick dat) -. ' '
b=. (<'Lab Section') e. 11 {.each dat
a +: b
)
iscolon=: 3 : 0
t=. ;: :: 0: y
if. t-:0 do. return. end.
if. (<'define') e. t do.
1 + (2 {. t) -: (,'0');'define' return.
end.
i=. t i. <,':'
if. i=0 do. 0 return. end.
if. -. (,each ':0') -: (i+0 1){t,'';'' do. 0 return. end.
t=. t }.~ -'NB.' -: 3 {. _1 pick t
1 + t -: ,each '0:0'
)
isfilename=: 3 : 0
*./ (2=3!:0 y),(0<#y),':'~:{.y
)
isnb=: 3 : 0
'NB.'-:3{.dlb y
)
isnbkey=: 3 : 0
('NB.labprepare:'-:14 {. y) +. ('NB.labscript:'-:13 {. y)
)
isnote=: 3 : 0
'Note ' -: 5 {. y
)
isnblab=: 3 : 0
('NB.lab'-:6{.y) +. 'NB.spx'-:6{.y
)
remcomments=: 3 : 0
dat=. dlb each y
msk=. (<'NB.') = 3 {.each dat
bal=. (' ' -.~ 3 }. ]) each msk#dat
bal=. (2 < +/@(*./\)@(e.&'-=')) &> bal
y #~ -. msk expand bal
)
remnote=: 3 : 0
len=. 1 + y i. <,')'
(len }. y);len
)
runprepare=: 3 : 0
cocurrent 'base'
0!:100 y [ 4!:55<'y'
)
runscript=: 3 : 0
SCRIPT=: SCRIPT, y,LF
)
j=. <;._2 (0 : 0)
& &
" "
< <
> >
)
f=. ({.&>j),LF
t=. (2 }.each j),<'<br/>'
tohtml=: rplc & (f;"0 t)
f=. 3 : 'lab_jlab_ 0'
plot_jctrl_fkey_jwplot_=: f f.
jvm_jctrl_fkey_jviewmat_=: f f.
graph_jctrl_fkey_jzgraph_=: f f.
LABCHAPTER=: 0 : 0
pc labchapter closeok;pn "Lab Chapter";
cc labid static;
cc labinfo static;cn " ";
bin hv;
minwh 240 200;cc listbox listbox;
bin zv;
cc ok button;cn "OK";
cc cancel button;cn "Cancel";
bin sz;
pas 4 2;pcenter;
rem form end;
)
LABCHAPTER6=: 0 : 0
pc labchapter closeok;pn "Lab Selection";
xywh 270 68 80 24;cc ok button leftmove rightmove;cn "OK";
xywh 270 98 80 24;cc cancel button leftmove rightmove;cn "Cancel";
xywh 12 70 240 200;cc listbox listbox ws_border ws_vscroll rightmove bottommove;
xywh 14 16 332 22;cc labid static;
xywh 14 44 240 22;cc labinfo static;cn " ";
pas 4 2;pcenter;
rem form end;
)
labfmt=: 4 : 0
DEL,(":x),'. ',y,DEL
)
labchapter_run=: 3 : 0
wd IFJNET{::LABCHAPTER;LABCHAPTER6
s=. #y
wd 'set labid text *',(gettitle''),' (',(":s),(s plurals ' chapter'),')'
wd 'set labinfo text *Select a chapter:'
d=. (1+i.#y) labfmt each y
wd 'set listbox items ',;d
wd 'setfocus listbox'
wd 'pshow;'
)
labchapter=: 3 : 0
val=. dltb }.y
msk=. (<'chapter')={."1 LABDEF
if. -. 1 e. msk do.
echo 'There are no chapters in the lab' return.
end.
if. 0=#val do.
chp=. {:"1 msk#LABDEF
if. IFQT do.
labchapter_run chp
else.
def=. chp ,each <'''',LF
echo }: ;'lab'':'&, each def
end.
return.
end.
ndx=. LABDEF i. 'chapter';val
if. 0=#ndx do.
echo 'chapter not found: ',val
else.
LABPOS=: ndx
lab 0
end.
EMPTY
)
labchapter_listbox_button=: 3 : 0
ndx=. 1 + ".listbox_select
LABPOS=: ndx i.~ +/\ (<'chapter')={."1 LABDEF
wd 'pclose'
echo ''
lab 0
)
labchapter_enter=: labchapter_ok_button=: labchapter_listbox_button
labchapter_close=: labchapter_cancel=: labchapter_cancel_button=: wd bind 'pclose'
lab=: 3 : 0
if. isfilename y do. labopen y return. end.
if. LABVER=0 do. lab805 y return. end.
if. ''-:y do. labstatus'' return. end.
if. ':'={.y do. labchapter y return. end.
labjump y
)
lab805=: 3 : 0
require LAB805
if. y -: ':' do. y=. 1 end.
lab_jlab805_ y
)
labcheckdef=: 3 : 0
if. 0=#LABDEF do. 1[echo 'first open a lab' else. 0 end.
)
labcheckpos=: 3 : 0
if. LABPOS > {:LABNDX do. 1[echo 'end of lab' else. 0 end.
)
labdefs=: 3 : 0
SCRIPT=: LABFILE=: LABDEF=: ''
LABNOS=: LABNEXT=: LABNDX=: ''
LABVER=: 1
)
labhtml=: 3 : 0
if. IFJHS do.
jhtml_jhs_ y
else.
wd 'sm html *',y
end.
)
labhr=: 3 : 0
if. IFJHS do. jhtml_jhs_'<hr/>' else. echo 80$'_' end.
)
labjump=: 3 : 0
if. labcheckdef'' do. return. end.
if. -. isinteger y do.
echo 'lab jumps should be 1 or 2 integers' return.
end.
if. 1=#y do.
if. y=0 do. labnext '' return. end.
if. y<0 do. labprev y return. end.
end.
if. 1 e. (2 < #y), (1 > y), ({.y) > #LABDEF do.
echo 'lab jumps should be in range 1 to ',":#LABDEF return.
end.
if. 1=#y do.
lab 0$LABPOS=: <:{.y return.
end.
'bgn end'=. y
if. end < bgn do.
echo 'second jump should be after the first' return.
end.
end=. end <. #LABDEF
labnext1 LABNEXT=: <: bgn + i. 1 + end - bgn
)
labkey=: 3 : 0
typ=. {.y
val=. }.y
if. typ='p' do.
runprepare val
else.
runscript val
end.
)
lablatex=: 3 : 0
if. IFJHS do.
jhtml_jhs_'<img src="http://latex.codecogs.com/svg.latex?',y,'" border="0"/>'
else.
echo 'latex: ',y
end.
)
labnext=: 3 : 0
if. labcheckdef'' do. return. end.
if. labcheckpos'' do. return. end.
ind=. LABNDX i. LABPOS
while. 1 do.
if. ind = #LABDEF do. return. end.
'tag val'=. ind{LABDEF
if. -. tag -: 'key' do. break. end.
labkey val
ind=. ind+1
end.
LABPOS=: LABPOS+1
if. (tag -: 'txt') *. 0=#val do.
if. IFJHS do. echo ' ' else. echo ' ' end.
labnext '' return.
end.
if. tag -: 'cmd' do. labrun val return. end.
select. tag
case. 'html' do.
labhtml val
case. 'hr' do.
labhr''
case. 'latex' do.
lablatex val
case. 'chapter' do.
echo 'Chapter: ',val
case. 'section' do.
echo 'Chapter: ',val
case. 'txt' do.
labtxt val
end.
labnext1''
)
labnext1=: 3 : 0
if. #LABNEXT do.
LABPOS=: {.LABNEXT
LABNEXT=: }.LABNEXT
lab 0
end.
EMPTY
)
labprev=: 3 : 0
LABPOS=: 0 >. LABPOS + {.y
if. labcheckpos'' do. return. end.
lab 0
)
labrun=: 3 : 0
IMMX_jlab_=: y
9!:27 'empty labnext1_jlab_ 0!:111 IMMX_jlab_'
9!:29 [ 1
)
labstatus=: 3 : 0
if. labcheckdef'' do. return. end.
echo (":LABPOS+1),' of ',(":#LABDEF),' in ',LABFILE
)
labtxt=: 3 : 0
if. IFJHS do.
echo y
else.
echo y
end.
)
labopen=: 3 : 0
labdefs''
if. -. fexist y do. echo 'not found: ',y return. end.
LABVER=: getversion y
if. LABVER=0 do. lab805 y return. end.
rc=. labparse y
if. -. rc -: 0 do.
echo rc
labdefs'' return.
end.
LABFILE=: y
LABPOS=: 0
if. IFJHS do.
ADVANCE_jijx_=: 'lab'
a=. 'ctrl+. or menu > advances'
if. 3 = nc <'labopened_jhs_' do.
labopened_jhs_ y
end.
elseif. IFQT do.
a=. 'ctrl+j advances'
elseif. 1 do.
a=. 'lab 0 NB. advances (create a shortcut key!)'
end.
echo a
labstatus''
lab 0
EMPTY
)
labparse=: 3 : 0
dat=. 'b' fread y
if. dat -: _1 do. 'not found: ',y return. end.
dat=. dtb each remcomments dat
res=. i. 0 2
nos=. i. 0
pos=. 0
while. #dat do.
txt=. dlb 0 pick dat
if. isnote txt do.
'dat len'=. remnote dat
pos=. pos + len continue.
end.
if. isnbkey txt do.
'key dat len'=. getkey dat
res=. res,'key';key
nos=. nos, pos
pos=. pos + len continue.
end.
nos=. nos, pos
dat=. }.dat
pos=. pos + 1
if. 0=#txt do.
res=. res,'txt';'' continue.
end.
if. t=. iscolon txt do.
ndx=. 1 + dat i. <,')'
if. t=1 do.
res=. res,'cmd';toLF (<txt),ndx{.dat
else.
res=. res,'txt';toLF }: ndx {.dat
end.
dat=. ndx }. dat
pos=. pos + ndx continue.
end.
if. isnblab txt do.
ndx=. txt i. ':'
res=. res,(6}.ndx{.txt);(ndx+1)}.txt continue.
end.
if. isnb txt do.
ndx=. 0 i.~ *./\ (isnb &> dat) > isnblab &> dat
res=. res,'txt';toLF delnb each (<txt), ndx {. dat
dat=. ndx }. dat
pos=. pos + ndx continue.
end.
res=. res,'cmd';txt
end.
LABDEF=: res
LABNOS=: nos
LABNDX=: ({.-]) +/\. (<'key') ~: {."1 LABDEF
0
)
lab_z_=: lab_jlab_
spx_z_=: lab_jlab_