summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/minim-mp/minim-mp.sty
blob: 948c6034b716a2fc1e348a5b89af11b0a1a347d7 (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

\ProvidesPackage{minim-mp}[2023/03/03 Run MetaPost code from LaTeX]

\input minim-mp

% work around latex’s \protect mechanism
\let\mnm@protect=\protect
\everymaketext{\let\protect=\mnm@protect}

% the defaultfont is the current \normalfont
\def\mnm@setnormalfont{{\normalfont
    \xdef\mnm@normalfont{\expandafter\csstring\the\font}}}

% global instance configuration
\everymp0{pre}{mp}{defaultfont:="\mnm@normalfont";}
\everymp0{pre}{tex}{%
    \mnm@setnormalfont
    \let\mpcolor = \minimpcolor
    \let\protect = \noexpand}
\everymp0{post}{tex}{%
    \let\protect=\mnm@protect}

% a one-off metapost environment
\newenvironment{metapost}[1][]{\begingroup
     \catcodetable\csname minim:mp:catcodes:mpcode\endcsname
     \csname:metapost:\endcsname[#1]}
    {\csname end:metapost:\endcsname}
\NewDocumentEnvironment{:metapost:}{O{}+b}{\endgroup
    \directmetapost[#1]{#2}}{}

% separate metapost instances
\newcommand\newmetapostenvironment[2][]{%
    \newmetapostinstance[#1]\mnm@tmp
    \expandafter\let\csname #2@instance\endcsname\mnm@tmp
    \everymp\mnm@tmp{post}{tex}{%
        \getallmpimages \csname #2@instance\endcsname}
    \newenvironment{#2}{\begingroup
         \catcodetable\csname minim:mp:catcodes:mpcode\endcsname
         \csname:#2:\endcsname}
        {\csname end:#2:\endcsname}%
    \NewDocumentEnvironment{:#2:}{+b}{\endgroup
        \runmetapost \csname #2@instance\endcsname{##1}}{}}

% \mpcolor support
\def\minimpcolor#1#{\dominimpcolor{#1}}
\def\dominimpcolor#1#2{%
    \directlua{ tex.scantoks('mpcolorspectoks', 0, '#1{#2}')
                tex.runtoks 'mpcolorruntoks' }
    (\the\mpcolorvaltoks)}
\newtoks\mpcolorspectoks \newtoks\mpcolorruntoks \newtoks\mpcolorvaltoks
\mpcolorruntoks{\expandafter\extractcolorspec\the\mpcolorspectoks\mptmpcolor
    \expandafter\mpcolorvaltoks\expandafter\@gobble\mptmpcolor}

% as a rather fragile drop-in replacement for luamplib
\DeclareOption{luamplib}{%
    \def\mplibtextextlabel#1{} % always enabled
    \def\mplibshowlog#1{\directlua{
        require 'minim-mp'.on_line = %
            \ifcsname minimp@switch@#1\endcsname
                true \else false \fi }}
    \def\mplibcodeinherit#1{%
        \ifcsname minimp@switch@#1\endcsname
            \newmetapostenvironment[jobname='\jobname']{@mplibcode}
            \let\mplibcode=\@mplibcode
            \let\endmplibcode=\@mplibcode
        \else
            \let\mplibcode=\metapost
            \let\endmplibcode=\endmetapost
        \fi}
    \mplibcodeinherit{disable}
    \def\mplibsetformat#1{\directlua{
        require 'minim-mp'.default_format = '#1.mp' }}
    \def\mplibnumbersystem#1{\directlua{
        require 'minim-mp'.default_math = '#1' }}
    \newcommand\everymplib[2][0]{%
        \everymp{#1}{pre}{mp}{defaultfont:="\mnm@normalfont";#2}}
    \newcommand\everyendmplib[2][0]{%
        \everymp{#1}{post}{mp}{#2}}
} \ProcessOptions*

\let\minimp@switch@@enable=\relax
\let\minimp@switch@@enabled=\relax
\let\minimp@switch@@true=\relax
\let\minimp@switch@@yes=\relax