[metapost] Can I use interactive mode in Lua?

Rui Zhang zrui16 at hotmail.com
Mon Oct 16 22:10:59 CEST 2017


>local mpkpse = kpse.new("mpost")
>
>local mp = mplib.new {
>     job_name    = "mpout",
>     interaction = "scroll",
>     find_file   = function(name, mode, type)
>         if mode == "r" then
>             return mpkpse:find_file(name, type)
>         end
>         return name
>     end,
>}
>
>local t = [[
>     input plain ;
>     beginfig(1) ;
>         scrollmode ;
>         draw fullcircle ;
>         foo := "whatever1" ;
>         draw fullcircle ;
>     endfig ;
>     end.
>]]
>
>for s in string.gmatch(t,"[^\n\r]+") do
>     local res = mp:execute(s)
>     if res.status == 1 then
>         print("warning: " .. (res.term or ""))
>     elseif res.status == 2 then
>         print("error: " .. (res.term or ""))
>         break;
>     elseif res.status == 3 then
>         print("fatal: " .. (res.term or ""))
>         break;
>     end
>end
>
>you have to quit as the error state is kept even when you feed some 
>intermediate line into the lib

Thank you for the solution.  But this does not work if I have
a statement broken into several lines.
For example, this should compile with standalone mpost:

    draw (0,0)
    for i = 1 upto 10:
      -- (i, 0)
    endfor;

If somehow I can determine where the end of a statement is,
the problem might be solved.  But it sounds like a bad idea
to reinvent a parser for this purpose.


More information about the metapost mailing list