summaryrefslogtreecommitdiff
path: root/tex/context/base/mkii/core-uti.mkii
blob: bd2fa46a0ff70993d5867b3d6a852f1c02bbb576 (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
%D \module
%D   [       file=core-uti,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Utility File Handling,
%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 Core Macros / Utility File Handling}

\unprotect

% todo : safe lan etc too
% todo : load all commands at once (tok)
% todo : merge status info patch into tui file (language, encoding, etc),

% Utility-file
%
% De onderstaande macro's ondersteunen het gebruik van de
% zogeheten utility-file. Alle extern onder te brengen
% informatie wordt opgeslagen in de file \jobname.tui, tenzij
% er selectief pagina's worden gezet. In dat geval wordt de
% file \jobname.tmp gebruikt. Informatie wordt ingelezen uit
% de file \jobname.tuo, welke door TeXUtil wordt aangemaakt.

\edef\utilityversion{1998.07.07} % was: 1996.03.15  % status variables
\edef\utilityversion{1998.12.20} % was: 1998.07.07  % index attributes
\edef\utilityversion{2003.07.19} % was: 1998.12.20  % object pages
\edef\utilityversion{2006.06.23} % was: 2003.07.19  % -- instead of :
\edef\utilityversion{2006.09.21} % pt in pos
\edef\utilityversion{2008.10.14} % moved more to lua in mkiv

% Bepaalde commando's worden als string weggeschreven. Deze
% zijn aan het eind van deze file gedefinieerd.

% Om een opbouw van spaties te voorkomen (???) moet ^^M een
% andere betekenis krijgen:
%
% \catcode`\^^M=14 (comment)
%
% read file
%
% \catcode`\^^M=5  (end of line)

\newwrite\utility@tui
\newif\ifutilitydone

\ifx\sectionseparator\undefined \def\sectionseparator{-} \fi

\def\@@utilityerrormessage
  {\showmessage\m!systems8\empty
   \globallet\@@utilityerrormessage\relax}

\def\thisisutilityversion#1%
  {\doifelse\utilityversion{#1}%
     {\checksectionseparator}
     {\@@utilityerrormessage\resetutilities\endinput}}

\def\checksectionseparator                  % catches backward compatibility conflict
  {}% \doifnot\sectionseparator:\endinput}  % this dependency may go in a few years

\def\dosplitofffoliopart[#1--#2--#3]{#3}

\def\thisissectionseparator#1%
  {\bgroup
   \globallet\checksectionseparator\relax
   \defconvertedcommand \asciia\sectionseparator
   \defconvertedargument\asciib{#1}%
   \expanded{\gdef\noexpand\dosplitofffoliopart[####1\sectionseparator
     \sectionseparator####2\sectionseparator\sectionseparator####3]{####3}}%
   \ifx\asciia\asciib
     \egroup
   \else
     \egroup
     % todo \@@utilityerrormessage
     \resetutilities
     \endinput
   \fi}

\def\writeutility         {\write\utility@tui}
\def\writeutilitycommand#1{\write\utility@tui{c \string#1}}

% less tokens
%
% \def\immediatewriteutility       {\immediate\writeutility}
% \def\immediatewriteutilitycommand{\immediate\writeutilitycommand}
%
% more flexible (for overloading)

\def\immediatewriteutility         {\immediate\write\utility@tui}
\def\immediatewriteutilitycommand#1{\immediate\write\utility@tui{c \string#1}}

% as in:

\def\cwriteutility#1%
  {\write\utility@tui{\noexpand\checkedutility{\number\nofshipouts}{#1}}}

\def\cwriteutilitycommand#1%
  {\write\utility@tui{\noexpand\checkedutility{\number\nofshipouts}{c \string#1}}}

\let\checkedutility\secondoftwoarguments

\def\docheckedutility#1#2{\ifnum#1=\nofshipouts#2\else\letterpercent\fi}

\prependtoks
  \let\checkedutility\docheckedutility
\to \everybeforeshipout

% Better use marks.

\newtoks \everyopenutilities
\newtoks \everycloseutilities
\newtoks \everycheckutilities

\def\openutilities {\the\everyopenutilities } % \global\everyopenutilities\emptytoks
\def\closeutilities{\the\everycloseutilities}
\def\checkutilities{\the\everycheckutilities}

\appendtoks
  \let\writeutility                \cwriteutility
  \let\writeutilitycommand         \cwriteutilitycommand
 %\let\immediatewriteutility       \cimmediatewriteutility
 %\let\immediatewriteutilitycommand\cimmediatewriteutilitycommand
  \let\checkutilities              \relax
\to \everycheckutilities

\appendtoks
  \immediate\openout\utility@tui\jobname.\f!inputextension
  \immediatewriteutilitycommand{\thisissectionseparator{\sectionseparator}}% for the moment
  \immediatewriteutilitycommand{\thisisutilityversion  {\utilityversion  }}% in this order
\to \everyopenutilities

\appendtoks
%   \immediate\closeout\utility@tui % niet echt nodig
  \reportutilityproblems
  % should be a message :
  \let\writeutilitycommand         \gobbleoneargument
  \let\writeutility                \gobbleoneargument
  \let\immediatewriteutilitycommand\gobbleoneargument
  \let\immediatewriteutility       \gobbleoneargument
\to \everycloseutilities

% \def\reopenutilities
%   {\immediate\closeout\utility@tui
%    \openutilities}

\def\abortutilitygeneration
  {\immediatewriteutilitycommand\utilitygenerationaborted
   \immediatewriteutility{q {quit}}}

\def\utilitygenerationaborted
  {\showmessage\m!systems{21}\empty
   \globallet\utilitygenerationaborted\endinput
   \gdef\reportutilityproblems{\showmessage\m!systems{22}\empty}%
   \endinput}

\def\savecurrentvalue#1#2%
  {\immediatewriteutilitycommand{\initializevariable\string#1{#2}}}

\let\initializevariable\gdef

\appendtoks
    \globallet\initializevariable\gobbletwoarguments
\to \everyafterutilityread

\let\reportutilityproblems\relax

\newtoks\utilityresetlist

\def\addutilityreset#1%
  {\@EA\appendtoks\csname\s!reset#1\endcsname\to\utilityresetlist}

\def\resetutilities
  {\the\utilityresetlist}

% #1=type #2=file #3=melding #4=voor #5=na
%
% Er wordt gegroepeerd. Als binnen een lijst (bijvoorbeeld) de
% \leftskip is aangepast, maar nog geen \par is gegeven, dan
% geldt buiten de groep de oude \leftskip. Aan #5 kan dan
% ook \par worden meegegeven om de paragraaf af te sluiten.

\newif\ifdoinpututilities
\newif\ifunprotectutilities   % voor't geval er \v!xxxxxx's zijn

\def\currentutilityfilename{\jobname}

% we need to pop and push, else problems with reading
% utility files (toc) in xml mode and (e.g.) in a toc
% entry doing a doifmode
%
% the following is not ok because we have no way to signal
% xml content (yet), so for the moment we use this:

\appendtoks
    \ifprocessingXML
        \processingXMLfalse
        \enableXML
        \catcode`\\=\@@escape
        \catcode`\{=\@@begingroup
        \catcode`\}=\@@endgroup
        \catcode`\%=\@@comment\relax
    \fi
\to \everybeforeutilityread

% \edef\testbytesequence
%   {\rawcharacter{7}%
%    \rawcharacter{27}%
%    %rawcharacter{227}% invalid in xetex, which expects utf
%    \rawcharacter{195}\rawcharacter{128}} % valid utf code
%
% \def\thisisbytesequence#1%
%   {\ifx\testbytesequence\empty\else
%      \defconvertedcommand\testbytesequence\testbytesequence
%      \defconvertedargument\ascii{#1}%
%      \ifx\testbytesequence\ascii \else
%        \writestatus\m!systems{possible problem with 8 bit output}%
%      \fi
%    \fi
%    \global\let\thisisbytesequence\gobbleoneargument}
%
% \ifnum\texengine=\xetexengine
%     \let\testbytesequence\empty
% \fi
%
% \appendtoks
%   \immediatewriteutilitycommand{\thisisbytesequence{\testbytesequence}}%
% \to \everyopenutilities

\let\testbytesequence  \empty             % keep this
\let\thisisbytesequence\gobbleoneargument % keep this

\long\def\doutilities#1#2#3#4#5% % introduceren in utility file
  {\resetutilities
   % more than one utility thing can be handled in one pass,
   % for instance lists, so we process ##1 as list
   \def\douticommand##1{\csname\s!set##1\endcsname}%
   \processcommacommand[#1]\douticommand
   \begingroup
   \def\currentutilityfilename{#2}%
   \notesenabledfalse
   \doinpututilitiestrue
   \global\utilitydonefalse
   \pushendofline % geeft problemen zodra andere file wordt ingelezen
   \pushcatcodetable
   \setcatcodetable\ctxcatcodes
   \ifunprotectutilities % nog nodig ?
     \unprotect
   \fi
   #4%
   \the\everybeforeutilityread
   \readjobfile{#2.\f!outputextension}\donothing\donothing
   \the\everyafterutilityread
   \popcatcodetable
   #5%
   \relax
   \ifunprotectutilities
     \protect
   \fi
   \popendofline
   \ifutilitydone\else
     \doifsomething{#3}
       {\showmessage\m!systems9{{#3}}%
        \doifconcepttracing
          {\blank
           \setmessagetext\m!systems9{{#3}}%
           \type{[\currentmessagetext]}%
           \blank}}%
   \fi
   \endgroup}

% Default-instellingen (verborgen)

\prependtoks \resetutilities \to \everyjob

% Experiment
%
% \installprogram{Hello World}
% \installprogram[hw]{Hello World}
% \installedprogram[hw]

\def\installprogram
  {\dosingleempty\doinstallprogram}

\def\doinstallprogram[#1]#2%
  {\doifelsenothing{#1}
     {\dodoinstallprogram{#2}}
     {\setvalue{\??up#1}{\dodoinstallprogram{#2}}}}

\def\dodoinstallprogram#1%
  {\immediatewriteutility{e p {#1}}}

\def\installedprogram[#1]%
  {\getvalue{\??up#1}}

% \writeplugindata{texutil}{{alpha}}
% \writeplugindata{texutil}{{beta}}
% \writeplugindata{texutil}{{gamma}}
% \writeplugindata{texutil}{{delta}}
%
% \loadplugindata {plugintest}

\def\immediatewriteplugindata#1#2%
  {\immediatewriteutility{p u {#1} #2}}

\def\writeplugindata#1#2%
  {\writeutility{p u {#1} #2}}

\def\loadplugindata#1%
  {\doutilities{#1}\jobname\empty\relax\relax}

% \plugincommand{\command{}{}{}}
%
% this way we can catch undefined commands

\long\def\plugincommand#1%
  {\doplugincommand#1\relax}

\long\def\doplugincommand#1%
  {\ifx#1\undefined
     \expandafter\noplugincommand
   \else
     \expandafter#1%
   \fi}

\long\def\noplugincommand#1\relax
  {}

% \addutilityreset{plugintest}
%
% \def\resetplugintest{\let\plugintest\gobbletwoarguments}
% \def\setplugintest  {\let\plugintest\writestatus}
%
% \installplugin
%   {plugintest}
%   {\let\plugintest\gobbletwoarguments}
%   {\let\plugintest\writestatus}

\long\def\installplugin#1#2#3%
  {\addutilityreset          {#1}%
   \long\setvalue{\s!reset#1}{#2}%
   \long\setvalue{\s!set  #1}{#3}}

\protect \endinput