meta-ini.lmt /size: 4795 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['meta-ini'] = {
2    version   = 1.001,
3    comment   = "companion to meta-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local tonumber = tonumber
10local format = string.format
11local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
12local P, Cs, R, S, C, Cc = lpeg.P, lpeg.Cs, lpeg.R, lpeg.S, lpeg.C, lpeg.Cc
13
14metapost       = metapost or { }
15local metapost = metapost
16local context  = context
17
18-- local colorhash = attributes.list[attributes.private('color')]
19-- local textype   = tex.type
20-- local MPcolor   = context.MPcolor
21--
22-- do
23--
24--     local dimenorname  =
25--         lpegpatterns.validdimen / function(s)
26--             context("\\the\\dimexpr %s",s)
27--         end
28--       + (C(lpegpatterns.float) + Cc(1)) * lpegpatterns.space^0 * P("\\") * C(lpegpatterns.letter^1) / function(f,s)
29--             local t = textype(s)
30--             if t == "dimen" then
31--                 context("\\the\\dimexpr %s\\%s\\relax",f,s)
32--             elseif t == "count" then
33--                 context("\\the\\numexpr \\%s * %s\\relax",s,f) -- <n>\scratchcounter is not permitted
34--             end
35--         end
36--
37--     local splitter = lpeg.splitat("::",true)
38--
39--     interfaces.implement {
40--         name      = "prepareMPvariable",
41--         arguments = "string",
42--         actions   = function(v)
43--             if v == "" then
44--              -- MPcolor("black")
45--                 context("black")
46--             else
47--                 local typ, var = lpegmatch(splitter,v)
48--                 if not var then
49--                     -- parse
50--                     if colorhash[v] then
51--                      -- MPcolor(v)
52--                         context("%q",var)
53--                     elseif tonumber(v) then
54--                         context(v)
55--                     elseif not lpegmatch(dimenorname,v) then
56--                         context("\\number %s",v) -- 0.4 ...
57--                     end
58--                 elseif typ == "d" then -- to be documented
59--                     -- dimension
60--                     context("\\the\\dimexpr %s\\relax",var)
61--                 elseif typ == "n" then -- to be documented
62--                     -- number
63--                     context("\\the\\numexpr %s\\relax",var)
64--                 elseif typ == "s" then -- to be documented
65--                     -- string
66--                  -- context(var)
67--                     context("%q",var)
68--                 elseif typ == "c" then -- to be documented
69--                     -- color
70--                  -- MPcolor(var)
71--                     context("%q",var)
72--                 else
73--                     context(var)
74--                 end
75--             end
76--         end
77--     }
78--
79-- end
80
81do
82
83    local ctx_mathematics = context.mathematics
84
85    -- function metapost.formatnumber(f,n) -- just lua format
86    --     f = gsub(f,"@(%d)","%%.%1")
87    --     f = gsub(f,"@","%%")
88    --     f = format(f,tonumber(n) or 0)
89    --     f = gsub(f,"e([%+%-%d]+)",function(s)
90    --         return format("\\times10^{%s}",tonumber(s) or s) -- strips leading zeros
91    --     end)
92    --     context.mathematics(f)
93    -- end
94
95    -- formatters["\\times10^{%N}"](s) -- strips leading zeros too
96
97    local one = Cs((P("@")/"%%." * (R("09")^1) + P("@")/"%%" + 1)^0)
98    local two = Cs((P("e")/"" * ((S("+-")^0 * R("09")^1) / function(s)
99     -- return format("\\times10^{%s}",tonumber(s) or s)
100        return "\\times10^{" .. (tonumber(s) or s) .."}"
101    end) + 1)^1)
102
103    -- local two = Cs((P("e")/"" * ((S("+-")^0 * R("09")^1) / formatters["\\times10^{%N}"]) + 1)^1)
104
105    function metapost.formatnumber(fmt,n) -- just lua format
106        ctx_mathematics(lpegmatch(two,format(lpegmatch(one,fmt),n)))
107    end
108
109end
110
111do
112
113    -- this is an old pass-data-to-tex mechanism
114
115    local ctx_printtable = context.printtable
116
117    local data = false
118
119    function mp.mf_start_saving_data(n)
120        data = { }
121    end
122
123    function mp.mf_stop_saving_data()
124        if data then
125            -- nothing
126        end
127    end
128
129    function mp.mf_finish_saving_data()
130        if data then
131            -- nothing
132        end
133    end
134
135    function mp.mf_save_data(str)
136        if data then
137            data[#data+1] = str
138        end
139    end
140
141    local function getdata()
142        if data then
143            ctx_printtable(data,"\r")
144        end
145    end
146
147    interfaces.implement {
148        name      = "getMPdata",
149        public    = true,
150        protected = true,
151        actions   = getdata,
152    }
153
154    interfaces.implement {
155        name    = "rawMPdata",
156        public  = true,
157        actions = getdata,
158    }
159
160end
161