summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/luat-usr.mkiv
blob: 92d40010c0fce39900336fc53954984611293e79 (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
%D \module
%D   [       file=luat-usr,
%D        version=2005.08.11,% moved from luat-ini
%D          title=\CONTEXT\ Lua 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 Lua Macros / Userspace}

\unprotect

\registerctxluafile{luat-usr}{1.001}

%D A few goodies:
%D
%D \startbuffer
%D \luascript        {        context("foo 1:") context(i) }
%D \luathread {test} { i = 10 context("bar 1:") context(i) }
%D \luathread {test} {        context("bar 2:") context(i) }
%D \luathread {test} {}
%D \luathread {test} {        context("bar 3:") context(i) }
%D \luascript        {        context("foo 2:") context(i) }
%D \stopbuffer
%D
%D \typebuffer \startlines \getbuffer \stoplines

\let\luascript \clf_luascript
\let\luathread \clf_luathread
%let\clfscanner\clf_clfscanner
%def\clfscanner#1{\csname clf_#1\endcsname}

%D \macros
%D   {definenamedlua}
%D
%D We provide an interface for defining instances:

\def\s!lua  {lua}
\def\s!code {code}
\def\s!data {data}
%def\s!start{start}
%def\s!stop {stop}

%D Beware: because \type {\expanded} is een convert command, the error
%D message will show \type{<inserted text>} as part of the message.

\installsystemnamespace{luacode}

\unexpanded\def\definenamedlua
  {\bgroup
   \dodoubleempty\syst_definenamedlua}

\unexpanded\def\syst_definenamedlua[#1][#2]% no optional arg handling here yet / we could use numbers instead (more efficient)
  {\iffirstargument
     \ifcsname\??luacode#1\endcsname \else
       %
       \edef\fullname{lua_code_#1}%
       %
       \clf_registernamedlua{#1}{#2}{\fullname}%
       %
       \unexpanded\expandafter\xdef\csname\s!start#1\s!code\endcsname
         {\begingroup
          \obeylualines
          \obeyluatokens
          \csname\??luacode#1\endcsname}%
       %
       \global\expandafter\let\csname\s!stop#1\s!code\endcsname\relax
       %
       \normalexpanded{\xdef\csname\??luacode#1\endcsname##1\csname\s!stop#1\s!code\endcsname}%
         {\noexpand\expandafter\endgroup
            \noexpand\expandafter
            \expandafter\noexpand\csname clf_\fullname\endcsname
            \noexpand\expandafter{\noexpand\normalexpanded{##1}}}%
       %
       \global\expandafter\let\csname#1\s!code\expandafter\endcsname\csname clf_\fullname\endcsname
     \fi
   \fi
   \egroup}

%D We predefine a few.

% \definenamedlua[module][module instance] % not needed

\definenamedlua[user]    [private user]
\definenamedlua[third]   [third party module]
\definenamedlua[isolated][isolated]

%D In practice this works out as follows:
%D
%D \startbuffer
%D \startluacode
%D context("LUA")
%D \stopluacode
%D
%D \startusercode
%D global.context("USER 1")
%D context.par()
%D context("USER 2")
%D context.par()
%D if characters then
%D     context("ACCESS directly")
%D elseif global.characters then
%D     context("ACCESS via global")
%D else
%D     context("NO ACCESS at all")
%D end
%D context.par()
%D if bogus then
%D     context("ACCESS directly")
%D elseif global.bogus then
%D     context("ACCESS via global")
%D else
%D     context("NO ACCESS at all")
%D end
%D context.par()
%D \stopusercode
%D \stopbuffer
%D
%D \typebuffer

\protect \endinput