[metapost] Can I use interactive mode in Lua?

Hans Hagen pragma at wxs.nl
Mon Oct 16 23:08:26 CEST 2017


On 10/16/2017 10:10 PM, Rui Zhang wrote:
>> 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.

in general ; is the end of a statement but mp being a macro language 
that can happen in macros as well so ...

anyway, the lua interface of the library is not meant for interactive 
usage so you have to accept that you quit on an error .. normally the 
message points to where to look

Hans


-- 

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the metapost mailing list