%%
%% This is file `mpostinl.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% mpostinl.dtx  (with options: `package')
%% 
%% Copyright (C) 2010-2017 Niklas Beisert
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%% 
\NeedsTeXFormat{LaTeX2e}[1996/12/01]
\ProvidesPackage{mpostinl}[2017/01/04 v1.0 metapost inline figures]

\RequirePackage{verbatim}
\RequirePackage{graphicx}
\RequirePackage{keyval}

\newif\ifmpi@infile\mpi@infilefalse
\newif\ifmpi@inbody\mpi@inbodyfalse

\def\mpostfilename{\jobname}
\def\mpi@nowname{\jobname-tmp}
\def\mpi@extension{mps}
\def\mpi@template#1{\mpostfilename-#1.\mpi@extension}

\newcounter{mpi@count}
\def\thempi@count{\arabic{mpi@count}}

\newif\ifmpi@draft\mpi@draftfalse
\newif\ifmpi@latex\mpi@latextrue
\newif\ifmpi@fonts\mpi@fontsfalse
\newif\ifmpi@write\mpi@writetrue
\newif\ifmpi@compile\mpi@compiletrue
\newif\ifmpi@twice\mpi@twicefalse
\newif\ifmpi@lineno\mpi@linenofalse
\newif\ifmpi@labelnames\mpi@labelnamesfalse
\newif\ifmpi@nowactive\mpi@nowactivefalse
\newif\ifmpi@now\mpi@nowfalse
\newif\ifmpi@nowkeep\mpi@nowkeepfalse
\newif\ifmpi@include\mpi@includefalse
\newif\ifmpi@defglobal\mpi@defglobalfalse

\def\mpi@mpostmem{}
\def\mpi@mpostcompiler{mpost}
\def\mpi@texcompiler{latex}
\def\mpi@latexclass{article}
\def\mpi@latexoptions{}
\def\mpi@documentclass{\@backslashchar documentclass%
  \mpi@latexoptions{\mpi@latexclass}}

\def\mpi@warncompile{\ifmpi@compile\ifeof18%
  \PackageWarning{mpostinl}{write18 disabled, %
    manual metapost compiling required}{}%
  \global\mpi@compilefalse\fi\fi}

\def\mpi@group{mpi@}
\DeclareOption{final}{\mpi@draftfalse}
\define@key{\mpi@group}{draft}[true]{\csname mpi@draft#1\endcsname}
\define@key{\mpi@group}{write}[true]{\csname mpi@write#1\endcsname}
\define@key{\mpi@group}{latex}[true]{\csname mpi@latex#1\endcsname%
  \ifmpi@latex%
    \def\mpi@texcompiler{latex}%
  \else%
    \def\mpi@texcompiler{tex}%
  \fi}
\define@key{\mpi@group}{compile}[true]{\csname mpi@compile#1\endcsname}
\define@key{\mpi@group}{twice}[true]{\csname mpi@twice#1\endcsname}
\define@key{\mpi@group}{fonts}[true]{\csname mpi@fonts#1\endcsname}
\define@key{\mpi@group}{lineno}[true]{\csname mpi@lineno#1\endcsname}
\define@key{\mpi@group}{labelnames}[true]{\csname mpi@labelnames#1\endcsname}
\define@key{\mpi@group}{compiler}{\def\mpi@texcompiler{#1}}
\define@key{\mpi@group}{mem}{\def\mpi@mpostmem{#1}}
\define@key{\mpi@group}{command}{\def\mpi@mpostcompiler{#1}}
\define@key{\mpi@group}{class}{\def\mpi@latexclass{#1}}
\define@key{\mpi@group}{classopt}{\def\mpi@latexoptions{[#1]}}
\define@key{\mpi@group}{now}[true]{\csname mpi@nowactive#1\endcsname}
\define@key{\mpi@group}{nowall}[true]{\csname mpi@now#1\endcsname}
\define@key{\mpi@group}{nowkeep}[true]{\csname mpi@nowkeep#1\endcsname}
\define@key{\mpi@group}{globaldef}[true]{\csname mpi@defglobal#1\endcsname}
\define@key{\mpi@group}{extension}{\def\mpi@extension{#1}}
\define@key{\mpi@group}{template}{\def\mpi@template##1{#1}}
\define@key{\mpi@group}{numberwithin}{%
  \@addtoreset{mpi@count}{#1}%
  \def\thempi@count{\arabic{#1}-\arabic{mpi@count}}%
}

\DeclareOption*{\expandafter\setkeys\expandafter\mpi@group%
  \expandafter{\CurrentOption}}

\ProcessOptions
\mpi@warncompile

\def\mpi@empty{}

\begingroup\catcode`\"=12\relax\gdef\mpi@dblquotchar{"}\endgroup

\newwrite\mpi@out
\newwrite\mpi@outnow

\def\mpi@write#1{\ifmpi@write\immediate\write\mpi@out{#1}\fi}

\newtoks\mpi@buf
\newtoks\mpi@defbuf
\newtoks\mpi@nowbuf
\mpi@defbuf={}

\def\mpi@addto#1#2{\global#1=\expandafter{\the#1#2}}
\def\mpi@addtoexp#1#2{\expandafter\mpi@addto\expandafter#1\expandafter{#2}}

\def\mpi@clearbuf{\global\mpi@buf={}}
\def\mpi@addbufexp#1{\mpi@addtoexp\mpi@buf{#1^^J}}
\def\mpi@addbuf#1{{\protected@edef\mpi@tmp{#1}\mpi@addbufexp\mpi@tmp}}

\def\mpi@stripext#1{\edef\mpi@tmp{#1}\expandafter%
  \mpi@stripstart\expandafter{\mpi@tmp}}
\def\mpi@ifeq#1#2#3#4{\def\mpi@tmpa{#1}\def\mpi@tmpb{#2}%
  \ifx\mpi@tmpa\mpi@tmpb#3\else#4\fi}
\def\mpi@stripstart#1{\mpi@stripfor{\@gobble}#1.\@@.}
\def\mpi@stripfor#1#2.#3.{%
  \begingroup%
  \mpi@ifeq{#3}{\@@}{%
    \def\mpi@tmp{\def\mpi@stripped{#1.#2}}%
    \mpi@ifeq{#1}{\@gobble}{}{%
      \mpi@ifeq{#2}{eps}{\def\mpi@tmp{\def\mpi@stripped{#1}}}{}%
      \mpi@ifeq{#2}{mps}{\def\mpi@tmp{\def\mpi@stripped{#1}}}{}%
      \expandafter\mpi@ifeq\expandafter{\mpi@extension}{#2}%
        {\def\mpi@tmp{\def\mpi@stripped{#1}}}{}%
    }%
  }{\def\mpi@tmp{\mpi@stripfor{#1.#2}#3.}}%
  \expandafter\endgroup\mpi@tmp%
}

\newcommand{\mpostplaceholder}[2][]{\parbox[c]{1in}{%
  \hrule\vrule\hfill%
  \parbox[c]{0pt}{\rule{0cm}{0.6in}}\makebox[0pt][c]{\scriptsize\tt #2}%
  \hfill\vrule\hrule}}

\newcommand{\mpi@graphics}[2][]{%
  \IfFileExists{#2}%
    {\edef\mpi@tmp{#2}\includegraphics[#1]{\mpi@tmp}}%
    {\typeout{graphics file `#2' missing}\mpostplaceholder[file]{#2}}%
}

\newcommand{\mpi@verbatim}{%
  \@bsphack%
  \let\do\@makeother\dospecials%
  \catcode`\^^M\active%
  \def\verbatim@processline{\mpi@addbufexp{\the\verbatim@line}}%
  \verbatim@start%
}

\newcommand{\mpi@putlineno}{%
  \ifmpi@lineno%
    \mpi@addbuf{\@percentchar---------------------------------------}%
    \mpi@addbuf{\@percentchar%
      \ifx\currfilename\@undefined\else\currfilename\space\fi%
      l.\the\inputlineno}%
  \fi%
}

\newcommand{\mpi@beginfig}[1]{%
  \mpi@addbuf{filenametemplate \mpi@dblquotchar#1\mpi@dblquotchar;}%
  \mpi@addbuf{beginfig(\arabic{mpi@count})}%
}

\newcommand{\mpi@endfig}{%
  \mpi@addbuf{endfig;}%
}

\newcommand{\mpi@declaredoc}{%
  \ifmpi@latex%
    \mpi@putlineno%
    \mpi@addbuf{verbatimtex}%
    \mpi@addbuf{\mpi@documentclass}%
    \mpi@addbuf{etex}%
  \fi%
}

\newcommand{\mpi@begindoc}{%
  \ifmpi@latex%
    \mpi@putlineno%
    \mpi@addbuf{verbatimtex}%
    \mpi@addbuf{\@backslashchar begin{document}}%
    \mpi@addbuf{etex}%
  \fi%
}

\newcommand{\mpi@enddoc}{%
  \ifmpi@latex%
    \mpi@putlineno%
    \mpi@addbuf{verbatimtex}%
    \mpi@addbuf{\@backslashchar end{document}}%
    \mpi@addbuf{etex}%
  \fi%
}

\newcommand{\mpi@writehead}{%
  \ifmpi@fonts\mpi@addbuf{prologues:=3;}\else\mpi@addbuf{prologues:=2;}\fi%
  \ifmpi@draft\mpi@addbuf{draft:=1;}\fi%
  \mpi@addbuf{}%
  \mpi@putlineno%
  \ifx\mpi@compiler\mpi@empty\else%
    \mpi@addbuf{verbatimtex}%
    \mpi@addbuf{\@percentchar &\mpi@texcompiler}%
    \mpi@addbuf{etex}%
    \mpi@addbuf{}%
  \fi%
  \mpi@declaredoc%
  \mpi@addbuf{}%
}

\newcommand{\mpi@beginfile}{%
  \ifx\mpi@mpostmem\mpi@empty\else%
    \mpi@addbuf{\@percentchar &\mpi@mpostmem}%
  \fi%
  \mpi@addbuf{\@percentchar generated from file `\jobname' by mpostinl.sty}%
  \ifmpi@include\else%
    \mpi@writehead%
    \mpi@addbufexp{\the\mpi@defbuf}%
  \fi%
}

\newcommand{\mpi@endfile}{%
  \mpi@putlineno%
  \ifmpi@include\else%
    \mpi@addbuf{end}%
  \fi%
}

\newcommand{\mpi@startfile}{%
  \ifmpi@infile\else%
    \ifx\mpostfilename\mpi@empty%
      \PackageError{mpostinl}{no filename provided to write to}{}%
    \fi%
    \global\mpi@infiletrue%
    \ifmpi@write\immediate\openout\mpi@out\mpostfilename.mp\fi%
    \mpi@clearbuf%
    \mpi@beginfile%
    \mpi@write{\the\mpi@buf}%
    \ifmpi@include%
      \mpi@clearbuf%
      \mpi@putlineno%
      \mpi@addbuf{input \mpostfilename}%
      \mpi@addtoexp\mpi@defbuf{\the\mpi@buf^^J}%
    \else%
      \ifmpi@nowactive%
        \global\mpi@nowbuf={}%
        \mpi@addtoexp\mpi@nowbuf{\the\mpi@buf^^J}%
      \fi%
    \fi%
  \fi%
}

\newcommand{\mpi@startcontent}{%
  \mpi@startfile%
  \ifmpi@inbody\else%
    \global\mpi@inbodytrue%
    \mpi@clearbuf%
    \mpi@begindoc%
    \mpi@write{\the\mpi@buf}%
    \ifmpi@nowactive\mpi@addtoexp\mpi@nowbuf{\the\mpi@buf^^J}\fi%
  \fi%
}

\newcommand{\mpi@compile}[1]{%
  \ifmpi@write\ifmpi@compile%
    \def\mpi@imode{}%
    \ifcase\the\interactionmode%
      \def\mpi@imode{-interaction=batchmode}\or%
      \def\mpi@imode{-interaction=nonstopmode}\or%
      \def\mpi@imode{-interaction=scrollmode}\or%
      \def\mpi@imode{-interaction=errorstopmode}\fi%
    \def\mpi@execute{\mpi@mpostcompiler\space%
      \mpi@imode\space%
      \ifx\mpi@mpostmem\mpi@empty\else -mem=\mpi@mpostmem\space\fi%
      -tex=\mpi@texcompiler\space#1}%
    \immediate\write18{\mpi@execute}%
    \ifmpi@twice%
      \immediate\write18{\mpi@execute}%
    \fi%
  \fi\fi%
}

\newcommand{\mpi@closefile}{%
  \ifmpi@infile%
    \mpi@clearbuf%
    \ifmpi@inbody%
      \mpi@enddoc%
      \mpi@addbuf{}%
    \fi%
    \mpi@endfile%
    \mpi@write{\the\mpi@buf}%
    \ifmpi@write\immediate\closeout\mpi@out\fi%
    \ifmpi@inbody\mpi@compile{\mpostfilename.mp}\fi%
    \global\mpi@infilefalse%
    \global\let\mpostfilename\mpi@empty%
    \global\mpi@inbodyfalse%
    \setcounter{mpi@count}{0}%
  \fi%
}

\newcommand{\mpi@processnow}{%
  \ifmpi@nowactive\ifmpi@write\ifmpi@compile%
    \ifmpi@nowkeep%
      \mpi@stripext{\mpi@figfile}%
      \edef\mpi@nowname{\mpi@stripped}%
    \fi%
    \immediate\openout\mpi@outnow\mpi@nowname.mp%
    \immediate\write\mpi@outnow{\the\mpi@nowbuf}%
    \immediate\write\mpi@outnow{\the\mpi@buf}%
    \mpi@clearbuf%
    \mpi@enddoc%
    \mpi@addbuf{}%
    \mpi@endfile%
    \immediate\write\mpi@outnow{\the\mpi@buf}%
    \immediate\closeout\mpi@outnow%
    \mpi@compile{\mpi@nowname.mp}%
  \fi\fi\fi%
}

\AtEndDocument{\mpi@closefile}

\newcommand{\mpostsetup}[1]{%
  \setkeys\mpi@group{#1}%
  \mpi@warncompile%
}

\newif\ifmpi@deftex
\define@key{mpi@def}{tex}[true]{\csname mpi@deftex#1\endcsname}
\define@key{mpi@def}{global}[true]{\csname mpi@defglobal#1\endcsname}

\newenvironment{mpostdef}[1][]{%
  \mpi@deftexfalse%
  \setkeys{mpi@def}{#1}%
  \ifmpi@defglobal\else\ifmpi@deftex\ifmpi@include%
    \PackageWarning{mpostinl}{tex definitions within an include file %
      will be ignored by mpost; switching to global definition}{}%
    \mpi@defglobaltrue%
  \fi\fi\fi%
  \ifmpi@defglobal\else%
    \mpi@startfile%
  \fi%
  \mpi@clearbuf%
  \mpi@putlineno%
  \ifmpi@deftex%
    \mpi@addbuf{verbatimtex}%
  \fi%
  \mpi@verbatim%
}%
{%
  \ifmpi@deftex%
    \mpi@addbuf{etex}%
  \fi%
  \ifmpi@defglobal%
    \mpi@addtoexp\mpi@defbuf{\the\mpi@buf^^J}%
    \ifmpi@include\else\ifmpi@infile%
      \mpi@write{\the\mpi@buf}%
      \ifmpi@nowactive%
        \mpi@addtoexp\mpi@nowbuf{\the\mpi@buf^^J}%
      \fi%
    \fi\fi%
  \else%
    \mpi@write{\the\mpi@buf}%
    \ifmpi@include\else\ifmpi@nowactive%
      \mpi@addtoexp\mpi@nowbuf{\the\mpi@buf^^J}%
    \fi\fi%
  \fi%
  \@esphack%
}

\newif\ifmpi@figshow
\define@key{mpi@fig}{show}[true]{\csname mpi@figshow#1\endcsname}
\define@key{mpi@fig}{twice}[true]{\csname mpi@twice#1\endcsname}
\define@key{mpi@fig}{file}{\def\mpi@figfile{#1}}
\define@key{mpi@fig}{label}{\def\mpi@figlabel{#1}}
\define@key{mpi@fig}{opt}{\edef\mpi@figopt{[#1]}}
\define@key{mpi@fig}{now}[true]{\csname mpi@now#1\endcsname}

\newenvironment{mpostfig}[1][]{%
  \ifmpi@include%
    \PackageError{mpostinl}{cannot write figure to include file}{}%
  \fi%
  \def\mpi@figfile{}%
  \def\mpi@figlabel{}%
  \def\mpi@figopt{}%
  \mpi@figshowfalse%
  \setkeys{mpi@fig}{#1}%
  \ifx\mpi@figlabel\mpi@empty\ifx\mpi@figfile\mpi@empty\mpi@figshowtrue\fi\fi%
  \ifmpi@labelnames\ifx\mpi@figfile\mpi@empty\ifx\mpi@figlabel\mpi@empty\else%
    \edef\mpi@figfile{\mpi@template{\mpi@figlabel}}%
  \fi\fi\fi%
  \ifx\mpi@figfile\mpi@empty%
    \addtocounter{mpi@count}{1}%
    \edef\mpi@figfile{\mpi@template{\thempi@count}}%
  \fi%
  \ifx\mpi@figlabel\mpi@empty\else%
    \expandafter\ifx\csname mpi@l@\mpi@figlabel\endcsname\relax\else%
      \PackageWarning{mpostinl}{label `\mpi@figlabel' already defined; %
        overwriting}{}%
    \fi%
    \expandafter\xdef\csname mpi@l@\mpi@figlabel\endcsname{\mpi@figfile}%
  \fi%
  \mpi@startcontent%
  \mpi@clearbuf%
  \mpi@putlineno%
  \mpi@beginfig{\mpi@figfile}%
  \mpi@verbatim%
}%
{%
  \mpi@endfig%
  \mpi@write{\the\mpi@buf}%
  \ifmpi@now%
    \mpi@processnow%
  \fi%
  \@esphack%
  \ifmpi@figshow%
    \expandafter\mpi@graphics\mpi@figopt{\mpi@figfile}%
  \fi%
}

\newcommand{\mpostuse}[2][]{%
  \expandafter\ifx\csname mpi@l@#2\endcsname\relax%
    \PackageWarning{mpostinl}{unknown label `#2'}{}%
    \mpostplaceholder[label]{#2}%
  \else%
    \mpi@graphics[#1]{\csname mpi@l@#2\endcsname}%
  \fi%
}

\newcommand{\mpostgetname}[1]{%
  \expandafter\ifx\csname mpi@l@#1\endcsname\relax%
    \PackageWarning{mpostinl}{unknown label `#1'}{}%
    \let\mpostfigurename\relax%
  \else%
    \edef\mpostfigurename{\csname mpi@l@#1\endcsname}%
  \fi%
}

\define@key{mpi@file}{include}[true]{\csname mpi@include#1\endcsname}

\newcommand{\mpostfile}[2][]{%
  \mpi@closefile%
  \mpi@includefalse%
  \setkeys{mpi@file}{#1}%
  \xdef\mpostfilename{#2}%
}

\newcommand{\mpostdone}{\mpi@closefile}

\endinput
%%
%% End of file `mpostinl.sty'.
