summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/java-ini.mkii
blob: 2e62ca166f42af28165873359d68b2e1d1d47031 (plain)
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
%D \module
%D   [       file=java-ini,
%D        version=1998.01.30,
%D          title=\CONTEXT\ JavaScript Macros,
%D       subtitle=Initialization,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt JavaScript Macros / Initialization}

% BUG: preamble zonder used/used en split

% todo: lua sanitizer

% JavaScript support is under development. In the near future
% a slightly different model will be used. The JScode stuff
% will probably become just auto function inclusion and the
% JS_* things will disappear. First I have to find a way to
% deal with global variables so the 'uses' thing will remain.

% ook p{ref}
% documentation should be corrected to JS(

% Also, obeylines will be supported.

\unprotect

%D \JAVA\ support is not implemented as a generic support
%D module. The main reason for this is that passing system
%D variables to a \JAVASCRIPT\ is closely related to other core
%D macros.

%D \TEX\ is not the right tool to check the \JAVA\ code; the
%D most we can do is reporting some passed variables:

\newif\iftraceJScode \traceJScodefalse

\let\traceJScode\traceJScodetrue

%D A bit out of place, but not dangerous:

\bgroup
\catcode127=\@@letter
\gdef\delcharacter{^^7f}
\egroup

%D The number of passed variables is minimalized by setting the
%D next switch.

\newif\ifminimalizeJScode    \minimalizeJScodetrue

%D \macros
%D   {JS*}
%D
%D Because \JAVASCRIPT's are activated by the user, for
%D instance by activating on a button, their support is closely
%D related to the referencing mechanism. Integration takes
%D place by
%D
%D \starttyping
%D \goto{calculate total}[Sum()]
%D \stoptyping
%D
%D The \type{()} classify this as a script. If they are absent,
%D the keyword is treated as a normal reference.
%D
%D One can pass arguments to such a script by saying:
%D
%D \starttyping
%D \goto{calculate total}[Sum(1.5,2.3)]
%D \stoptyping
%D
%D References are passed by using the \type{R{}} classifier.
%D
%D \starttyping
%D \goto{calculate total}[Sum(1.5,2.3,R{overflow})]
%D \stoptyping
%D
%D The last call calls the script \type{Sum} and passes the
%D next set of variables:
%D
%D \starttyping
%D JS_S_1="1.5";
%D JS_S_2="2.3";
%D JS_R_3="overflow";
%D JS_P_3=3;
%D \stoptyping
%D
%D The first two parameters are just strings, the third one
%D however is treated as a reference and results in passing the
%D reference (if needed this references is prefixed) and the
%D (real) page number. The alternative:
%D
%D \starttyping
%D \goto{calculate total}[JS(Sum{V{1.5},V{2.3},R{overflow}})]
%D \stoptyping
%D
%D does a verbose passing:
%D
%D \starttyping
%D JS_V_1=1.5;
%D JS_V_2=2.3;
%D JS_R_3="overflow";
%D JS_P_3=3;
%D \stoptyping

%D We will also support direct function calls. In that case
%D no intermediate variables are used.

%D \macros
%D   {startJScode}
%D
%D A piece of \JAVASCRIPT\ code is defined by saying:
%D
%D \starttyping
%D \startJScode{SomeScript}
%D   var Item=this.getField("item");
%D          N=Item.getArray();
%D      Total=this.getField("total");
%D   Total.value=0;
%D   for (j=0; j<N.length; j++)
%D     { if (N[j].value!="")
%D       { Total.value += N[j].value } } ;
%D   if ((JS_N>0) && (JS_R_1!=""))
%D     { gotoNamedDest(JS_R_1) };
%D \stopJScode
%D \stoptyping
%D
%D Such a piece of code is closely related to the interpreter
%D used. Watch the last two lines, here the script adapts
%D itself to the presence of a reference.
%D
%D While
%D
%D \starttyping
%D \startJScode{name}
%D   name = 4 ;
%D \stopJScode
%D \stoptyping
%D
%D assumes uses no preamble or presumes that the preamble is
%D always loaded, the next definition also tells \CONTEXT\ to
%D actually include the preamble needed.
%D
%D \starttyping
%D \startJScode{uses} uses {later}
%D   uses = 6 ;
%D \stopJScode
%D \stoptyping

\long\def\startJScode#1 #2
  {\doifelse{#2}{uses}
     {\dostartJScodeA{#1}}
     {\dostartJScodeB{#1} #2 }}

\long\def\dostartJScodeA#1#2 #3\stopJScode
  {\long\setgvalue{\r!java#1}{\do{#2}{#3}}}

\long\def\dostartJScodeB#1#2\stopJScode
  {\long\setgvalue{\r!java#1}{\do{}{#2}}}

\let\stopJScode\relax

%D \macros
%D   {presetJScode}
%D
%D The code can be retrieved by saying
%D
%D \starttyping
%D \presetJScode{SomeScript}{template}
%D \stoptyping
%D
%D Such a template is a comma separated list, where
%D individual entries can optionally be transformed by
%D \type{R{}} and \type{V{}}.
%D
%D After this call, the code is available in \type{\JScode}.

\newif\ifdirectJScode

\def\presetJScode#1#2% #1=operation #2=arguments
  {\setverbosecscharacters
   \def\par{\delcharacter}% was: { }
   \scratchcounter\zerocount
   \globallet\JScode\empty
   \def\do##1##2%
     {\doifelse{##2}{!}\directJScodetrue\directJScodefalse}%
   \getvalue{\r!java#1}%
   \edef\!!stringa{#2}%
   \ifx\!!stringa\empty \else
     \processcommacommand[\!!stringa]\dopresetJSvariables
   \fi
   \def\docommand##1%
     {\doifundefinedelse{\r!java\r!java##1}
        {\showmessage\m!javascript2{##1}}
        {\useJSpreamblenow{##1}}}%
%        {\doglobal\increment\currentJSpreamble
%         \doglobal\addtocommalist{##1}\allJSpreambles}}%
   \def\do##1##2%
     {\xdef\JScode{\ifdirectJScode#1(\JScode)\else\JScode##2\fi}%
     %\xdef\JScode{JS\string_N=\the\scratchcounter;\JScode}%
      \processcommalist[##1]\docommand}%
   \getvalue{\r!java#1}}

\def\dopresetJSvariables#1%
  {\advance\scratchcounter \plusone
   \donefalse
   \dodopresetJSvariables#1\end}%

\def\dodopresetJSvariables
  {\doifnextcharelse R\dodopresetJSrefvariables
  {\doifnextcharelse V\dodopresetJSvervariables
  {\doifnextcharelse S\dodopresetJSstrvariables
                      \dodopresetJSrawvariables}}}

\def\dodopresetJSrefvariables R#1\end
  {\doifreferencefoundelse{#1}
     {\donetrue \dododopresetJSvariables R{\referenceprefix#1}%
      \donefalse\dododopresetJSvariables P{\currentrealreference}}
     {\unknownreference{#1}}%
   \ifminimalizeJScode \else
     \donetrue\dododopresetJSvariables S{#1}%
   \fi}

\def\dodopresetJSvervariables V#1\end
  {\donefalse\dododopresetJSvariables V{#1}%
   \ifminimalizeJScode \else
     \donetrue\dododopresetJSvariables S{#1}%
   \fi}

\def\dodopresetJSstrvariables S#1\end
  {\donetrue\dododopresetJSvariables S{#1}}

\def\dodopresetJSrawvariables #1\end
  {\donetrue\dododopresetJSvariables S{#1}}

\def\JSprefix#1%
  {JS\string_#1\string_\the\scratchcounter}

\def\dododopresetJSvariables#1#2%
  {\iftraceJScode
     \writestatus{JavaScript}{\JSprefix#1=#2}
     \xdef\JScode{\JScode console.println("\JSprefix#1=#2"); }%
   \fi
   \ifdirectJScode
     \xdef\JScode{\ifx\JScode\empty\else\JScode,\fi\ifdone"#2"\else#2\fi}%
   \else
     \xdef\JScode{\JScode\JSprefix#1=\ifdone"#2"\else#2\fi; }%
   \fi}

%D \macros
%D   {startJSpreamble, flushJSpreamble}
%D
%D One can define insert \JAVASCRIPT\ code at the document level
%D by using:
%D
%D \starttyping
%D \startJSpreamble{oeps}
%D   oeps = 1 ;
%D \stopJSpreamble
%D \stoptyping
%D
%D which is the same as:
%D
%D \starttyping
%D \startJSpreamble{now} used now
%D   now = 2 ;
%D \stopJSpreamble
%D \stoptyping
%D
%D while the next definition is only included when actually
%D used.
%D
%D \starttyping
%D \startJSpreamble{later} used later
%D   later = 3 ;
%D \stopJSpreamble
%D \stoptyping
%D
%D This command may be used more that once, but always before
%D the first page is shipped out.

\newif\ifoneJSpreamble \oneJSpreamblefalse

\let\allJSpreambles\empty
\newcounter\nofJSpreambles
\newcounter\currentJSpreamble

\long\def\startJSpreamble#1 #2 %
  {\bgroup           % we need to restore the catcodes
   \restoreendofline % just in case it happens while reading lists
   \doifelse{#2}{used}
     {\dostartJSpreamble#1 }
     {\dostartJSpreamble#1 now #2 }}

\long\def\dostartJSpreamble#1 #2 %
  {\processaction
     [#2]
     [     later=>\chardef\JSstatus\zerocount,%
             now=>\chardef\JSstatus\plusone  ,%
      \s!default=>\chardef\JSstatus\plustwo  ,%
      \s!unknown=>\chardef\JSstatus\plustwo  ]%
    \ifaddJSlinebreaks
      \obeylines  \let\obeyedline \normalpar
      \obeyspaces \let\obeyedspace\normalspace
    \fi
    \dodostartJSpreamble{#1}}

\long\def\dodostartJSpreamble#1#2\stopJSpreamble
  {\presetJSfunctions #2function ()\end
   \long\setgvalue{\r!java\r!java#1}{#2}%
   \ifcase\JSstatus \else
     \useJSpreamblenow{#1}%
   \fi
   \egroup}

%D \macros
%D   {setJSpreamble, addtoJSpreamble}
%D
%D In addition to the previous preamble definitions, we can
%D set a preamble \quote {in||line} and add tokens to a
%D preamble.

\def\setJSpreamble#1#2%
  {\doifundefined{\r!java\r!java#1}
     {\setgvalue{\r!java\r!java#1}{#2;}%
      \doglobal\increment\currentJSpreamble
      \doglobal\addtocommalist{#1}\allJSpreambles}}

\def\addtoJSpreamble#1#2%
  {\doifdefinedelse{\r!java\r!java#1}
     {\edef\!!stringa{\r!java\r!java#1}%
      \edef\!!stringb{\csname\!!stringa\endcsname}%
      \@EA\setgvalue\@EA\!!stringa\@EA{\!!stringb #2;}}
     {\setJSpreamble{#1}{#2}}}

%D \macros
%D   {useJSpreamblenow}
%D
%D The next macro can be used to force inclusion of postponed
%D \JAVASCRIPT\ preambles.

\def\useJSpreamblenow#1%
  {\doglobal\increment\currentJSpreamble
   \doglobal\addtocommalist{#1}\allJSpreambles}

%D Because we want to check for valid calls, we preload the
%D functions. This means that we can call them directly as
%D well as indirectly when defined by \type {\startJScode} etc.

% \long\def\presetJSfunctions#1function #2(#3)%
%   {\doifelsenothing{#2}
%      {\long\def\presetJSfunctions##1\end{}}
%      {\stripspaces\from#2\to\ascii
%       \doifundefined{\r!java\ascii}{\setgvalue{\r!java\ascii}{\do{}{!}}}}%
%    \presetJSfunctions}

\long\def\presetJSfunctions#1function#2(#3)%
  {\doifelse{#2}\space
     {\long\def\presetJSfunctions##1\end{}}
     {\stripspaces\from#2\to\ascii
      \doifundefined{\r!java\ascii}{\setgvalue{\r!java\ascii}{\do{}{!}}}}%
   \presetJSfunctions}

\def\getJSpreamble#1%
  {\getvalue{\r!java\r!java#1}}

\def\presetJSpreamble
  {\ifx\allJSpreambles\empty\else
     \bgroup
     \setverbosecscharacters
     \obeyspaces \let\obeyedspace\normalspace
     \def\par{\delcharacter}% was: { }
     \globallet\JSpreamble\empty
     \def\@@collectedJSpreamble{\r!java\r!java collected}%
     \letvalue{\@@collectedJSpreamble}=\empty
     \def\docommand##1%
       {\xdef\JScode{\getvalue{\r!java\r!java##1}}%
        \ifoneJSpreamble % \global\letcdcsname
          \@EA\setxvalue\@EA\@@collectedJSpreamble\@EA
            {\csname\@@collectedJSpreamble\endcsname\JScode}%
        \else
          \setxvalue{\r!java\r!java##1}{\JScode}%
        \fi}%
     \processcommacommand[\allJSpreambles]\docommand
     \ifoneJSpreamble
       \gdef\allJSpreambles{collected}%
     \fi
     \globallet\presetJSpreamble\relax
     \egroup
   \fi}

\def\flushJSpreamble
  {\iflocation\ifx\allJSpreambles\empty\else
     \ifcase\nofJSpreambles\else\ifnum\nofJSpreambles=\currentJSpreamble
       \bgroup
       \presetJSpreamble
       \expanded{\doflushJSpreamble{\allJSpreambles}}%
       \globallet\flushJSpreamble\relax
       \globallet\allJSpreambles\empty
       \egroup
     \fi\fi
   \fi\fi}

\def\finalflushJSpreamble
  {\iflocation
     \flushJSpreamble
     \ifcase\currentJSpreamble\relax\else
       \savecurrentvalue\nofJSpreambles\currentJSpreamble
       \globallet\currentJSpreamble\nofJSpreambles
     \fi
   \fi}

\prependtoks \flushJSpreamble      \to \everyshipout
\prependtoks \finalflushJSpreamble \to \everylastshipout

%D \macros
%D   {doPSsanitizeJScode}
%D
%D Before the code can be passed to the (\POSTSCRIPT\ or \PDF)
%D output file, some precautions must be made concerning the
%D use of \type{(} and~\type{)}. Here we use a beautiful
%D \type{\aftergroup} trick I discovered in the \TABLE\ format.

\def\doPSsanitizeJScode#1\to#2%
  {\begingroup
   \scratchcounter\zerocount % \aftergroup counter
   \aftergroup\xdef
   \aftergroup#2%
   \aftergroup{%
   \expanded{\defconvertedargument\noexpand\JScode{#1}}%
   \expandafter\handletokens\JScode\with\dodoPSsanitizeJScode
   \aftergroup}%
   \endgroup
   \iftraceJScode
     \writestatus{JS trace}{#2}%
   \fi}

%D I started with:
%D
%D \starttyping
%D \def\dodoPSsanitizeJScode#1%
%D   {\aftergroup\string
%D    \if#1(%
%D      \expandafter\aftergroup\csname#1\endcsname
%D    \else\if#1)%
%D      \expandafter\aftergroup\csname#1\endcsname
%D    \else\if#1;%
%D      \aftergroup;\aftergroup\string\expandafter\aftergroup\
%D    \else
%D      \expandafter\aftergroup#1%
%D    \fi\fi\fi
%D    \advance\scratchcounter by 1
%D    \ifnum\scratchcounter=500
%D      \expandafter\dododoPSsanitizeJScode
%D    \fi}
%D \stoptyping
%D
%D For pretty printing purposes, we need some way to signal
%D \TEX\ macros. Therefore we introduce a special keyword
%D \type{TEX}. When followed by a space, this keyword is
%D ignored, that is, filtered from the stream. Now we have:

\chardef\JSisTEX  \zerocount
\chardef\JScomment\zerocount

\newif\ifaddJSlinebreaks \addJSlinebreakstrue

\def\flushJSisTEX
  {\ifcase\JSisTEX
   \or \aftergroup T%
   \or \aftergroup T\aftergroup E%
   \or \aftergroup T\aftergroup E\aftergroup X%
   \fi
   \chardef\JSisTEX\zerocount}

% \def\doJSlinebreak
%  {\ifaddJSlinebreaks
%     \aftergroup\string\aftergroup\n%
%   \fi}
%
% \def\dodoPSsanitizeJScode#1%  % input stack>500 & TEX check
%   {\if#1/%
%      \ifnum\JScomment=0
%        \chardef\JScomment\plusone
%      \else\ifnum\JScomment=1
%        \chardef\JScomment\plustwo
%      \fi\fi
%    \else
%      \ifnum\JScomment=1
%        \aftergroup/%
%        \chardef\JScomment\zerocount
%      \fi
%      \ifnum\JScomment=2
%        \if#1\delcharacter
%          \chardef\JScomment\zerocount
%        \fi
%      \else
%        \if#1\delcharacter
%          \flushJSisTEX\doJSlinebreak
%        \else\if#1(%
%          \flushJSisTEX\aftergroup\string\expandafter\aftergroup\csname#1\endcsname
%        \else\if#1)%
%          \flushJSisTEX\aftergroup\string\expandafter\aftergroup\csname#1\endcsname
%        \else\if#1;%
%          \flushJSisTEX\aftergroup;\doJSlinebreak
%        \else\if#1T%
%          \ifnum\JSisTEX=0 \chardef\JSisTEX\plusone   \else\flushJSisTEX\aftergroup T\fi
%        \else\if#1E%
%          \ifnum\JSisTEX=1 \chardef\JSisTEX\plustwo   \else\flushJSisTEX\aftergroup E\fi
%        \else\if#1X%
%          \ifnum\JSisTEX=2 \chardef\JSisTEX\plusthree \else\flushJSisTEX\aftergroup X\fi
%        \else\if#1\normalspace
%          \ifnum\JSisTEX=3 \chardef\JSisTEX\zerocount \else\flushJSisTEX\aftergroup#1\fi
%        \else
%          \flushJSisTEX\aftergroup\string\expandafter\aftergroup#1%
%        \fi\fi\fi\fi\fi\fi\fi\fi
%      \fi
%    \fi
%    \dododoPSsanitizeJScode}

% todo: "http:\\" -> simple. maar wel \" afvangen
%
% use new pdftex escape mechanism or make fully expandable version, not used that often btw

\chardef\JSstring\zerocount

\def\doJSlinebreak
 {\chardef\JScomment\zerocount
  \chardef\JSstring\zerocount
  \ifaddJSlinebreaks
    \aftergroup\string\aftergroup\n%
  \fi}

\def\dodoPSsanitizeJScode#1%  % input stack>500 & TEX check
  {\if#1/%
     \ifnum\JSstring=0
       \ifnum\JScomment=0
         \chardef\JScomment\plusone
       \else\ifnum\JScomment=1
         \chardef\JScomment\plustwo
       \fi\fi
     \else
       \aftergroup/%
     \fi
   \else
     \ifnum\JScomment=1
       \aftergroup/%
       \chardef\JScomment\zerocount
     \fi
     % is the delchar trick still needed?
     \ifnum\JScomment=2
       \ifnum`#1=13 % brrr
         \doJSlinebreak
       \else\if#1\par
         \doJSlinebreak
       \else\if#1\delcharacter
         \doJSlinebreak
       \fi\fi\fi
     \else
       \ifnum`#1=13 % brrr
         \flushJSisTEX\doJSlinebreak
       \else\if#1\par
         \flushJSisTEX\doJSlinebreak
       \else\if#1\delcharacter
         \flushJSisTEX\doJSlinebreak
       \else\if#1(%
         \flushJSisTEX\aftergroup\string\expandafter\aftergroup\csname#1\endcsname
       \else\if#1)%
         \flushJSisTEX\aftergroup\string\expandafter\aftergroup\csname#1\endcsname
       %\else\if#1;%
       %  \flushJSisTEX\aftergroup;\doJSlinebreak
       \else\if#1T%
         \ifnum\JSisTEX=0 \chardef\JSisTEX\plusone   \else\flushJSisTEX\aftergroup T\fi
       \else\if#1E%
         \ifnum\JSisTEX=1 \chardef\JSisTEX\plustwo   \else\flushJSisTEX\aftergroup E\fi
       \else\if#1X%
         \ifnum\JSisTEX=2 \chardef\JSisTEX\plusthree \else\flushJSisTEX\aftergroup X\fi
       \else\if#1\normalspace
         \ifnum\JSisTEX=3 \chardef\JSisTEX\zerocount \else\flushJSisTEX\aftergroup#1\fi
       \else
          % todo: "test\"test"
         \if#1"%
           \ifcase\JSstring
             \chardef\JSstring\plusone
           \else
             \chardef\JSstring\zerocount
           \fi
         \fi
         \flushJSisTEX\aftergroup\string\expandafter\aftergroup#1%
       \fi\fi\fi\fi\fi\fi\fi\fi\fi % \fi
     \fi
   \fi
   \dododoPSsanitizeJScode}

%D Close reading learns that one line comments (\type{// ...})
%D are removed from the stream. This permits switching in
%D pretty printing \JAVASCRIPT\ sources as well as saves
%D some bytes.

%D The magic 500 in the next hack prevents the input stack from
%D overflowing when large scripts are sanitized.

\def\dododoPSsanitizeJScode
  {\ifcase\JSisTEX\ifcase\JScomment
     \advance\scratchcounter \plusone
   \fi\fi
   \ifnum\scratchcounter=500
     \expandafter\dodododoPSsanitizeJScode
   \fi}

\def\dodododoPSsanitizeJScode
  {\let\next={%
   \aftergroup}%
   \endgroup
   \begingroup
   \aftergroup\xdef
   \aftergroup\sanitizedJScode
   \aftergroup{%
   \aftergroup\sanitizedJScode
   \let\next=}}

%D The macro \type{\doPSsanitizeJScode} converts its argument
%D into the macro \type{\sanitizedJScode}, thereby prefixing
%D each \type{(} and \type{)} by a slash.

%D Hooking this mechanism into the general \CONTEXT\ reference
%D mechanism does not take much effort:

\definespecialtest{JS}%
  {\doifdefinedelse{\r!java\currentreferenceoperation}}

\definespeciallocation{JS}#1#2%
  {\iflocation
     \bgroup
       \bgroup
         \presetJScode
           \currentreferenceoperation
           \currentreferencearguments
       \egroup
       \dohandlegoto
         {#2}%
         {\dostartgotoJS\buttonwidth\buttonheight\JScode}%
         {\dostopgotoJS}%
     \egroup
   \else
     {#2}%
   \fi}

%D \macros
%D   {useJSscripts}
%D
%D In due time, users will build their collections of scripts,
%D which can be used (loaded) when applicable. Although not all
%D public, we will provide some general purpose scripts,
%D collected in files with names like \type{java-...}. One can
%D load these scripts with \type{\useJSscripts}, like:
%D
%D \starttyping
%D \useJSscripts[fld]
%D \stoptyping
%D
%D The not so complicated implementation of this macro is:

\def\dodouseJSscripts#1%
  {\doifelse{#1}\v!reset
     {\let\allJSpreambles\empty}
     {\doifundefined{\c!file\f!javascriptprefix#1}
        {\startnointerference
           \letgvalueempty{\c!file\f!javascriptprefix#1}%
           \makeshortfilename[\f!javascriptprefix#1]%
           \startreadingfile
             \readsysfile{\shortfilename.\mksuffix}
               {\showmessage\m!javascript1{#1}}
               {\readsysfile{\shortfilename.tex}
                  {\showmessage\m!javascript1{#1}}
                  \donothing}%
           \stopreadingfile
         \stopnointerference}}}

\def\douseJSscripts[#1][#2]%
  {\processcommalist[#1]\dodouseJSscripts
   \processcommalist[#2]\useJSpreamblenow}

\def\useJSscripts
  {\dodoubleempty\douseJSscripts}

\protect \endinput