summaryrefslogtreecommitdiff
path: root/metapost/context/base/mpiv/mp-luas.mpiv
blob: c3079824773d67a235b18d16761962e968bba76c (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
%D \module
%D   [       file=mp-luas.mpiv,
%D        version=2014.04.14,
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=\LUA,
%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.

if known context_luas : endinput ; fi ;

% When I prototyped the runscript primitive I was just thinking of a usage like
% the original \directlua primitive in luatex: genererate something and pipe
% that back to metapost, and have access to some internals. Instead of compiling
% the code a the metapost end here we delegate that to the lua end. Only strings
% get passed. Of course in the end the real usage got a bit beyong the intended
% usage. So, in addition to some definitions here there are and will be use in
% other metafun modules too. Of course in retrospect I should have done this five
% years earlier.

boolean context_luas ; context_luas := true ;

% First variant:
%
% let lua = runscript ;
%
% Second variant:
%
% vardef lua (text t) =
%     runscript(for s = t : s & endfor "")
% enddef;
%
% Third variant:
%
% vardef lua (text t) =
%     runscript("" for s = t :
%         if string s :
%             & s
%         elseif numeric s :
%             & decimal s
%         elseif boolean s :
%             & if s : "true" else "false" fi
%         fi endfor)
% enddef;
%
% Fourth variant:

vardef mlib_luas_luacall(text t) =
    runscript("" for s = t :
        if string s :
            & s
        elseif numeric s :
            & decimal s
        elseif boolean s :
            & if s : "true" else : "false" fi
        fi endfor
    )
enddef ;

vardef mlib_luas_lualist(expr c)(text t) =
    save b ; boolean b ; b := false ;
    runscript(c & "(" for s = t :
        if b :
            & ","
        else :
            hide(b := true)
        fi
        if string s :
            & ditto & s & ditto
        elseif numeric s :
            & decimal s
        elseif boolean s :
            & if s : "true" else : "false" fi
        fi endfor & ")"
    )
enddef ;

def luacall = mlib_luas_luacall enddef ; % why no let

vardef lualist@#(text t) = mlib_luas_lualist(str @#)(t) enddef ;

string mlib_luas_s ; % saves save/restore

vardef lua@#(text t) =
    mlib_luas_s := str @# ;
    if length(mlib_luas_s) > 0 :
        mlib_luas_lualist(mlib_luas_s,t)
    else :
        mlib_luas_luacall(t)
    fi
enddef ;

vardef MP@#(text t) =
    mlib_luas_lualist("MP." & str @#,t)
enddef ;