1%D \module 2%D [ file=luat-usr, 3%D version=2005.08.11,% moved from luat-ini 4%D title=\CONTEXT\ Lua Macros, 5%D subtitle=Initialization, 6%D author=Hans Hagen, 7%D date=\currentdate, 8%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}] 9%C 10%C This module is part of the \CONTEXT\ macro||package and is 11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for 12%C details. 13 14\writestatus{loading}{ConTeXt Lua Macros / Userspace} 15 16\unprotect 17 18\registerctxluafile{luat-usr}{} 19 20%D A few goodies: 21%D 22%D \startbuffer 23%D \luascript { context("foo 1:") context(i) } 24%D \luathread {test} { i = 10 context("bar 1:") context(i) } 25%D \luathread {test} { context("bar 2:") context(i) } 26%D \luathread {test} {} 27%D \luathread {test} { context("bar 3:") context(i) } 28%D \luascript { context("foo 2:") context(i) } 29%D \stopbuffer 30%D 31%D \typebuffer \startlines \getbuffer \stoplines 32 33\let\luascript 34\let\luathread 35%let\clfscanner\clf_clfscanner 36%def\clfscanner#1{\csname clf_#1\endcsname} 37 38%D \macros 39%D {definenamedlua} 40%D 41%D We provide an interface for defining instances. We don't have the definers yet 42%D so we do itlow level: 43 44\def\s!lua {lua} 45\def\s!code {code} 46\def\s!data {data} 47\def\s!start{start} 48\def\s!stop {stop} 49 50%D Beware: because \type {\expanded} is een convert command, the error 51%D message will show \type{<inserted text>} as part of the message. 52 53\installsystemnamespace{luacode} 54 55\unexpanded\def\definenamedlua 56 {\bgroup 57 \dodoubleempty\syst_definenamedlua} 58 59\unexpanded\def\syst_definenamedlua[#1][#2]% no optional arg handling here yet / we could use numbers instead (more efficient) 60 {\iffirstargument 61 \ifcsname\??luacode#1\endcsname \else 62 % 63 \edef\fullname{lua_code_#1}% 64 % 65 {#1}{#2}{\fullname}% 66 % 67 \unexpanded\expandafter\xdef\csname\s!start#1\s!code\endcsname 68 {\begingroup 69 \obeylualines 70 \obeyluatokens 71 \csname\??luacode#1\endcsname}% 72 % 73 \expandafter\glet\csname\s!stop#1\s!code\endcsname\relax 74 % 75 \normalexpanded{\xdef\csname\??luacode#1\endcsname##1\csname\s!stop#1\s!code\endcsname}% 76 {\noexpand\expandafter\endgroup 77 \noexpand\expandafter 78 \expandafter\noexpand\csname clf_\fullname\endcsname 79 \noexpand\expandafter{\noexpand\normalexpanded{##1}}}% 80 % 81 \expandafter\glet\csname#1\s!code\expandafter\endcsname\csname clf_\fullname\endcsname 82 \fi 83 \fi 84 \egroup} 85 86%D We predefine a few. 87 88% \definenamedlua[module][module instance] % not needed 89 90\definenamedlua[user] [private user] 91\definenamedlua[third] [third party module] 92\definenamedlua[isolated][isolated] 93 94%D In practice this works out as follows: 95%D 96%D \startbuffer 97%D \startluacode 98%D context("LUA") 99%D \stopluacode 100%D 101%D \startusercode 102%D global.context("USER 1") 103%D context.par() 104%D context("USER 2") 105%D context.par() 106%D if characters then 107%D context("ACCESS directly") 108%D elseif global.characters then 109%D context("ACCESS via global") 110%D else 111%D context("NO ACCESS at all") 112%D end 113%D context.par() 114%D if bogus then 115%D context("ACCESS directly") 116%D elseif global.bogus then 117%D context("ACCESS via global") 118%D else 119%D context("NO ACCESS at all") 120%D end 121%D context.par() 122%D \stopusercode 123%D \stopbuffer 124%D 125%D \typebuffer 126 127\protect \endinput 128 |