summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/luat-usr.mkxl
blob: b266044cea850c18317ec625bd0af116841658d5 (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
%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}{autosuffix}

%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

% \luascript % defined at the lua end
% \luathread % defined at the lua end

%D \macros
%D   {definenamedlua}
%D
%D We provide an interface for defining instances. We don't have the definers yet
%D so we do it low level:

\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 message will
%D show \type{<inserted text>} as part of the message.

\installsystemnamespace{luacode}

\permanent\tolerant\protected\def\definenamedlua[#1]#*[#2]%
  {\ifarguments \orelse \ifcsname\??luacode#1\endcsname \else
     \bgroup
     %
     \edef\fullname{lua_code_#1}%
     %
     \clf_registernamedlua{#1}{#2}{\fullname}%
     %
     \frozen\instance\protected\xdefcsname\s!start#1\s!code\endcsname
       {\begingroup
        \obeylualines
        \obeyluatokens
        \csname\??luacode#1\endcsname}%
     %
     \aliased\gletcsname\s!stop#1\s!code\endcsname\relax
     %
     \normalexpanded{\xdefcsname\??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}}}%
     \frozen\instance\gletcsname#1\s!code\expandafter\endcsname\csname clf_\fullname\endcsname
     \egroup
   \fi}

%D We predefine a few.

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

\appendtoks
    \definenamedlua[user]    [private user]
    \definenamedlua[third]   [third party module]
    \definenamedlua[isolated][isolated]
\to \everyjob

%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