[metapost] Can I use interactive mode in Lua?

Hans Hagen pragma at wxs.nl
Mon Oct 16 09:59:59 CEST 2017


On 10/15/2017 9:51 PM, Rui Zhang wrote:
> On Oct 15 16:06:50, Hans Hagen wrote:
>> when a macro package uses the library then it has to make sure it
>> reports the errors which are available in the result table
> 
> Sorry, I don't get it. I mean if I have some errors in the
> metapost source, the standalone mpost program will stop
> at the first error and ask me what to do, and for example
> I can type X to terminate it.
> With mplib, it will not stop and always generating all errors.
> 
> Here is what I tried.  I used mplib directly in Lua, so it has
> nothing to do with macro packages.
> 
>      local mpkpse = kpse.new('mpost')
>      local mp = mplib.new {
>        job_name='mpout',
>        find_file = function(name, mode, type_)
>          if mode == 'r' then
>            return mpkpse:find_file(name, type_)
>          end
>          return name
>        end,
>      }
>      res = mp:execute [[
>      input plain;
>      beginfig(1);
>        draw error % this line contains an error
>        draw fullcircle;
>      endfig;
>      ]]
>      print(res.term)
> 
> I got:
> 
> This is MetaPost, Version 2.000
>   (d:/texlive/2017/texmf-dist/metapost/base/plain.mp
> Preloading the plain mem file, version 1.005)
> 
> 
>>> error
> ! Improper `addto'.
> <to be read again>
>                     withpen
> draw->...:also(EXPR0)else:doublepath(EXPR0)withpen
>                                                    .currentpen.fi._op_
> <to be read again>
>                     addto
> draw->addto
>             .currentpicture.if.picture(EXPR0):also(EXPR0)else:doublepath(EXPR...
> <to be read again>
>                     ;
> <*>       draw fullcircle;
> 
> This expression should have specified a known path.
> So I'll not change anything just now.
> 
> ! Extra tokens will be flushed.
> <to be read again>
>                     addto
> draw->addto
>             .currentpicture.if.picture(EXPR0):also(EXPR0)else:doublepath(EXPR...
> <to be read again>
>                     ;
> <*>       draw fullcircle;
> 
> I've just read as much of that statement as I could fathom,
> so a semicolon should have been next. It's very puzzling...
> but I'll try to get myself back together, by ignoring
> everything up to the next `;'. Please insert a semicolon
> now in front of anything that you don't want me to delete.
> (See Chapter 27 of The METAFONTbook for an example.)
> 
> (EOF)
> 
> This is too much for me.  It did not stop and kept
> processing the remaining of the code.
> If I use standalone mpost, I get
> 
> This is MetaPost, version 2.000 (TeX Live 2017/W32TeX) (kpathsea version 6.2.3)
> (d:/texlive/2017/texmf-dist/metapost/base/mpost.mp (d:/texlive/2017/texmf-dist/metapost/base/plain.mp
> Preloading the plain mem file, version 1.005) ) (./mpost1.mp
>>> error
> ! Improper `addto'.
> <to be read again>
>                     withpen
> draw->...:also(EXPR0)else:doublepath(EXPR0)withpen
>                                                    .currentpen.fi._op_
> <to be read again>
>                     addto
> draw->addto
>             .currentpicture.if.picture(EXPR0):also(EXPR0)else:doublepath(EXPR...
> <to be read again>
>                     ;
> l.3   draw fullcircle;
> 
> ?
> 
> which stops at the first error and gives an interactive prompt.
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

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