summaryrefslogtreecommitdiff
path: root/tex/context/base/core-uti.mkiv
blob: 6883f2227c0eee4f7d48fe86c416d1277790c8dc (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
%D \module
%D   [       file=core-uti,
%D        version=2006.09.19,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Utility File Handling,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\unprotect

\registerctxluafile{core-uti}{1.001}

%D We need a way to pass strings safely to \LUA\ without the
%D need for tricky escaping. Compare:
%D
%D \starttyping
%D \ctxlua {something("anything tricky can go here")}
%D \ctxlua {something([\luastringsep[anything tricky can go here]\luastringsep])}
%D \stoptyping

\def\luastringsep{===} % this permits \typefile{self} otherwise nested b/e sep problems

\edef\!!bs{[\luastringsep[}
\edef\!!es{]\luastringsep]}

%D We have a the following available as primitive so there is no need
%D for it:
%D
%D \starttyping
%D \long\edef\luaescapestring#1{\!!bs#1\!!es}
%D \stoptyping

%D Next we implement a few helpers:

\newwrite\utility@tua

\prependtoks
    \def         \writeutilitytua          {\write\utility@tua}
    \def\immediatewriteutilitytua{\immediate\write\utility@tua}
\to \everyjob

\let         \writeutilitytua\gobbleoneargument
\let\immediatewriteutilitytua\gobbleoneargument

\appendtoks
    \immediate\openout\utility@tua=\jobname.tua
    \immediatewriteutilitytua{-- file\space\space\space: \jobname}%
    \immediatewriteutilitytua{-- format\space:           \contextformat}%
    \immediatewriteutilitytua{-- stamp\space\space:      \contextversion}%
    \immediatewriteutilitytua{-- escape\space:           \!!bs\space...\space\!!es}%
    \immediatewriteutilitytua{-- version:                \utilityversion}%
    \immediatewriteutilitytua{}%
    \immediatewriteutilitytua{do}%
    \immediatewriteutilitytua{if job and job.version and not job.version == "\utilityversion" then return end}%
    \immediatewriteutilitytua{if not job then job = { } end}%
    \immediatewriteutilitytua{job.version = "\utilityversion"}%
\to \everyopenutilities

\appendtoks
    \immediatewriteutilitytua{end}%
   %immediate\closeout\utility@tua
\to \everycloseutilities

% The next file can be in lua or luc format:

\appendtoks
    \ctxlua { do
        if not job then job = { } end
        job.version = "\utilityversion"
        local settings = loadfile("\jobname.tuc")
        if settings then settings() end
    end}%
\to \everyjob

% variables

\appendtoks
    \immediatewriteutilitytua{if not job.variables then job.variables = { } end}%
\to \everyopenutilities

\def\savecurrentvalue#1#2%
  {\immediatewriteutilitytua{job.initializevariable("\strippedcsname#1","#2")}}

% temp

\let\thisisbytesequence\gobbleoneargument

% wrong place but we need to have it someplace

\appendtoks
    \ctxlua{input.storage.dump()}%
\to \everydump

\appendtoks
    \ctxlua{input.storage.finalize()}%
\to \everyfinalizeluacode

\protect \endinput