{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Readers.LaTeX.Macro
  ( macroDef
  )
where
import Text.Pandoc.Extensions (Extension(..))
import Text.Pandoc.Logging (LogMessage(MacroAlreadyDefined))
import Text.Pandoc.Readers.LaTeX.Parsing
import Text.Pandoc.Readers.LaTeX.Types
import Text.Pandoc.Class
import Text.Pandoc.Shared (safeRead)
import Text.Pandoc.Parsing hiding (blankline, mathDisplay, mathInline,
                            optional, space, spaces, withRaw, (<|>))
import Control.Applicative ((<|>), optional)
import qualified Data.Map as M
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.List.NonEmpty as NonEmpty
import Data.List.NonEmpty (NonEmpty(..))

macroDef :: (PandocMonad m, Monoid a) => (Text -> a) -> LP m a
macroDef :: (Text -> a) -> LP m a
macroDef Text -> a
constructor = do
    (()
_, [Tok]
s) <- LP m () -> LP m ((), [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw (LP m ()
commandDef LP m () -> LP m () -> LP m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m ()
environmentDef)
    (Text -> a
constructor ([Tok] -> Text
untokenize [Tok]
s) a -> LP m () -> LP m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$
      Extension -> LP m ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParserT s st m ()
guardDisabled Extension
Ext_latex_macros)
     LP m a -> LP m a -> LP m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> a -> LP m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
forall a. Monoid a => a
mempty
  where commandDef :: LP m ()
commandDef = do
          [(Text, Macro)]
nameMacroPairs <- LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newcommand LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
            LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *).
PandocMonad m =>
LP m [(Text, Macro)] -> LP m [(Text, Macro)]
checkGlobal (LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
letmacro LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
edefmacro LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
defmacro LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newif)
          Extension -> LP m ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParserT s st m ()
guardDisabled Extension
Ext_latex_macros LP m () -> LP m () -> LP m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
            ((Text, Macro) -> LP m ()) -> [(Text, Macro)] -> LP m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text, Macro) -> LP m ()
forall (m :: * -> *). PandocMonad m => (Text, Macro) -> LP m ()
insertMacro [(Text, Macro)]
nameMacroPairs
        environmentDef :: LP m ()
environmentDef = do
          Maybe (Text, Macro, Macro)
mbenv <- LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *).
PandocMonad m =>
LP m (Maybe (Text, Macro, Macro))
newenvironment
          case Maybe (Text, Macro, Macro)
mbenv of
            Maybe (Text, Macro, Macro)
Nothing -> () -> LP m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Just (Text
name, Macro
macro1, Macro
macro2) ->
              Extension -> LP m ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParserT s st m ()
guardDisabled Extension
Ext_latex_macros LP m () -> LP m () -> LP m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                do (Text, Macro) -> LP m ()
forall (m :: * -> *). PandocMonad m => (Text, Macro) -> LP m ()
insertMacro (Text
name, Macro
macro1)
                   (Text, Macro) -> LP m ()
forall (m :: * -> *). PandocMonad m => (Text, Macro) -> LP m ()
insertMacro (Text
"end" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name, Macro
macro2)
        -- @\newenvironment{envname}[n-args][default]{begin}{end}@
        -- is equivalent to
        -- @\newcommand{\envname}[n-args][default]{begin}@
        -- @\newcommand{\endenvname}@

insertMacro :: PandocMonad m => (Text, Macro) -> LP m ()
insertMacro :: (Text, Macro) -> LP m ()
insertMacro (Text
name, macro' :: Macro
macro'@(Macro MacroScope
GlobalScope ExpansionPoint
_ [ArgSpec]
_ Maybe [Tok]
_ [Tok]
_)) =
  (LaTeXState -> LaTeXState) -> LP m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> LP m ())
-> (LaTeXState -> LaTeXState) -> LP m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s ->
     LaTeXState
s{ sMacros :: NonEmpty (Map Text Macro)
sMacros = (Map Text Macro -> Map Text Macro)
-> NonEmpty (Map Text Macro) -> NonEmpty (Map Text Macro)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NonEmpty.map (Text -> Macro -> Map Text Macro -> Map Text Macro
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
name Macro
macro') (LaTeXState -> NonEmpty (Map Text Macro)
sMacros LaTeXState
s) }
insertMacro (Text
name, macro' :: Macro
macro'@(Macro MacroScope
GroupScope ExpansionPoint
_ [ArgSpec]
_ Maybe [Tok]
_ [Tok]
_)) =
  (LaTeXState -> LaTeXState) -> LP m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> LP m ())
-> (LaTeXState -> LaTeXState) -> LP m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s ->
     LaTeXState
s{ sMacros :: NonEmpty (Map Text Macro)
sMacros = Text -> Macro -> Map Text Macro -> Map Text Macro
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
name Macro
macro' (NonEmpty (Map Text Macro) -> Map Text Macro
forall a. NonEmpty a -> a
NonEmpty.head (LaTeXState -> NonEmpty (Map Text Macro)
sMacros LaTeXState
s)) Map Text Macro -> [Map Text Macro] -> NonEmpty (Map Text Macro)
forall a. a -> [a] -> NonEmpty a
:|
                      NonEmpty (Map Text Macro) -> [Map Text Macro]
forall a. NonEmpty a -> [a]
NonEmpty.tail (LaTeXState -> NonEmpty (Map Text Macro)
sMacros LaTeXState
s) }

lookupMacro :: PandocMonad m => Text -> LP m Macro
lookupMacro :: Text -> LP m Macro
lookupMacro Text
name = do
   Map Text Macro
macros :| [Map Text Macro]
_ <- LaTeXState -> NonEmpty (Map Text Macro)
sMacros (LaTeXState -> NonEmpty (Map Text Macro))
-> ParsecT [Tok] LaTeXState m LaTeXState
-> ParsecT [Tok] LaTeXState m (NonEmpty (Map Text Macro))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
   case Text -> Map Text Macro -> Maybe Macro
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
name Map Text Macro
macros of
     Just Macro
m -> Macro -> LP m Macro
forall (m :: * -> *) a. Monad m => a -> m a
return Macro
m
     Maybe Macro
Nothing -> String -> LP m Macro
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Macro not found"

letmacro :: PandocMonad m => LP m [(Text, Macro)]
letmacro :: LP m [(Text, Macro)]
letmacro = do
  Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"let"
  LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m [(Text, Macro)] -> LP m [(Text, Macro)])
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
    Tok SourcePos
_ (CtrlSeq Text
name) Text
_ <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
    LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok))
-> LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall a b. (a -> b) -> a -> b
$ Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'='
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    -- we first parse in verbatim mode, and then expand macros,
    -- because we don't want \let\foo\bar to turn into
    -- \let\foo hello if we have previously \def\bar{hello}
    Tok
target <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
singleChar
    case Tok
target of
      (Tok SourcePos
_ (CtrlSeq Text
name') Text
_) ->
         (do Macro
m <- Text -> LP m Macro
forall (m :: * -> *). PandocMonad m => Text -> LP m Macro
lookupMacro Text
name'
             [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(Text
name, Macro
m)])
         LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(Text
name,
                    MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenDefined [] Maybe [Tok]
forall a. Maybe a
Nothing [Tok
target])]
      Tok
_ -> [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(Text
name, MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenDefined [] Maybe [Tok]
forall a. Maybe a
Nothing [Tok
target])]

checkGlobal :: PandocMonad m => LP m [(Text, Macro)] -> LP m [(Text, Macro)]
checkGlobal :: LP m [(Text, Macro)] -> LP m [(Text, Macro)]
checkGlobal LP m [(Text, Macro)]
p =
  (Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"global" LP m Tok -> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
      (((Text, Macro) -> (Text, Macro))
-> [(Text, Macro)] -> [(Text, Macro)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Text
n, Macro MacroScope
_ ExpansionPoint
expand [ArgSpec]
arg Maybe [Tok]
optarg [Tok]
contents) ->
                (Text
n, MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GlobalScope ExpansionPoint
expand [ArgSpec]
arg Maybe [Tok]
optarg [Tok]
contents)) ([(Text, Macro)] -> [(Text, Macro)])
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m [(Text, Macro)]
p))
   LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
p

edefmacro :: PandocMonad m => LP m [(Text, Macro)]
edefmacro :: LP m [(Text, Macro)]
edefmacro = do
  MacroScope
scope <- (MacroScope
GroupScope MacroScope
-> ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m MacroScope
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"edef")
       ParsecT [Tok] LaTeXState m MacroScope
-> ParsecT [Tok] LaTeXState m MacroScope
-> ParsecT [Tok] LaTeXState m MacroScope
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (MacroScope
GlobalScope MacroScope
-> ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m MacroScope
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"xdef")
  (Text
name, [Tok]
contents) <- LP m (Text, [Tok]) -> LP m (Text, [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m (Text, [Tok]) -> LP m (Text, [Tok]))
-> LP m (Text, [Tok]) -> LP m (Text, [Tok])
forall a b. (a -> b) -> a -> b
$ do
    Tok SourcePos
_ (CtrlSeq Text
name) Text
_ <- ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
    -- we first parse in verbatim mode, and then expand macros,
    -- because we don't want \let\foo\bar to turn into
    -- \let\foo hello if we have previously \def\bar{hello}
    [Tok]
contents <- LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    (Text, [Tok]) -> LP m (Text, [Tok])
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
name, [Tok]
contents)
  -- expand macros
  [Tok]
contents' <- LP m [Tok] -> [Tok] -> LP m [Tok]
forall (m :: * -> *) a. PandocMonad m => LP m a -> [Tok] -> LP m a
parseFromToks (ParsecT [Tok] LaTeXState m Tok -> LP m [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok) [Tok]
contents
  [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
scope ExpansionPoint
ExpandWhenDefined [] Maybe [Tok]
forall a. Maybe a
Nothing [Tok]
contents')]

defmacro :: PandocMonad m => LP m [(Text, Macro)]
defmacro :: LP m [(Text, Macro)]
defmacro = do
  -- we use withVerbatimMode, because macros are to be expanded
  -- at point of use, not point of definition
  MacroScope
scope <- (MacroScope
GroupScope MacroScope
-> ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m MacroScope
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"def")
       ParsecT [Tok] LaTeXState m MacroScope
-> ParsecT [Tok] LaTeXState m MacroScope
-> ParsecT [Tok] LaTeXState m MacroScope
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (MacroScope
GlobalScope MacroScope
-> ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m MacroScope
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"gdef")
  LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m [(Text, Macro)] -> LP m [(Text, Macro)])
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
    Tok SourcePos
_ (CtrlSeq Text
name) Text
_ <- ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
    [ArgSpec]
argspecs <- ParsecT [Tok] LaTeXState m ArgSpec
-> ParsecT [Tok] LaTeXState m [ArgSpec]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT [Tok] LaTeXState m ArgSpec
forall (m :: * -> *). PandocMonad m => LP m ArgSpec
argspecArg ParsecT [Tok] LaTeXState m ArgSpec
-> ParsecT [Tok] LaTeXState m ArgSpec
-> ParsecT [Tok] LaTeXState m ArgSpec
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT [Tok] LaTeXState m ArgSpec
forall (m :: * -> *). PandocMonad m => LP m ArgSpec
argspecPattern)
    [Tok]
contents <- LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
scope ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
forall a. Maybe a
Nothing [Tok]
contents)]

-- \newif\iffoo' defines:
-- \iffoo to be \iffalse
-- \footrue to be a command that defines \iffoo to be \iftrue
-- \foofalse to be a command that defines \iffoo to be \iffalse
newif :: PandocMonad m => LP m [(Text, Macro)]
newif :: LP m [(Text, Macro)]
newif = do
  Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"newif"
  LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m [(Text, Macro)] -> LP m [(Text, Macro)])
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
    Tok SourcePos
pos (CtrlSeq Text
name) Text
_ <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
    -- \def\iffoo\iffalse
    -- \def\footrue{\def\iffoo\iftrue}
    -- \def\foofalse{\def\iffoo\iffalse}
    let base :: Text
base = Int -> Text -> Text
T.drop Int
2 Text
name
    [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [ (Text
name, MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
                    [SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"iffalse") Text
"\\iffalse"])
           , (Text
base Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"true",
                   MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
                   [ SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"def") Text
"\\def"
                   , SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
name) (Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name)
                   , SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"iftrue") Text
"\\iftrue"
                   ])
           , (Text
base Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"false",
                   MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
                   [ SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"def") Text
"\\def"
                   , SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
name) (Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name)
                   , SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"iffalse") Text
"\\iffalse"
                   ])
           ]

argspecArg :: PandocMonad m => LP m ArgSpec
argspecArg :: LP m ArgSpec
argspecArg = do
  Tok SourcePos
_ (Arg Int
i) Text
_ <- (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isArgTok
  ArgSpec -> LP m ArgSpec
forall (m :: * -> *) a. Monad m => a -> m a
return (ArgSpec -> LP m ArgSpec) -> ArgSpec -> LP m ArgSpec
forall a b. (a -> b) -> a -> b
$ Int -> ArgSpec
ArgNum Int
i

argspecPattern :: PandocMonad m => LP m ArgSpec
argspecPattern :: LP m ArgSpec
argspecPattern =
  [Tok] -> ArgSpec
Pattern ([Tok] -> ArgSpec)
-> ParsecT [Tok] LaTeXState m [Tok] -> LP m ArgSpec
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m [Tok]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ((Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok (\(Tok SourcePos
_ TokType
toktype' Text
txt) ->
                              (TokType
toktype' TokType -> TokType -> Bool
forall a. Eq a => a -> a -> Bool
== TokType
Symbol Bool -> Bool -> Bool
|| TokType
toktype' TokType -> TokType -> Bool
forall a. Eq a => a -> a -> Bool
== TokType
Word) Bool -> Bool -> Bool
&&
                              (Text
txt Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"{" Bool -> Bool -> Bool
&& Text
txt Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"\\" Bool -> Bool -> Bool
&& Text
txt Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"}")))

newcommand :: PandocMonad m => LP m [(Text, Macro)]
newcommand :: LP m [(Text, Macro)]
newcommand = do
  Tok SourcePos
pos (CtrlSeq Text
mtype) Text
_ <- Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"newcommand" LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                             Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"renewcommand" LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                             Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"providecommand" LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                             Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareMathOperator" LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                             Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareRobustCommand"
  LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m [(Text, Macro)] -> LP m [(Text, Macro)])
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
    Tok SourcePos
_ (CtrlSeq Text
name) Text
txt <- do
      LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'*')
      LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
        (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' LP m Tok
-> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces ParsecT [Tok] LaTeXState m () -> LP m Tok -> LP m Tok
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq LP m Tok -> ParsecT [Tok] LaTeXState m () -> LP m Tok
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}')
    ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Int
numargs <- Int
-> ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Int
0 (ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int)
-> ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT [Tok] LaTeXState m Int
forall (m :: * -> *). PandocMonad m => LP m Int
bracketedNum
    let argspecs :: [ArgSpec]
argspecs = (Int -> ArgSpec) -> [Int] -> [ArgSpec]
forall a b. (a -> b) -> [a] -> [b]
map Int -> ArgSpec
ArgNum [Int
1..Int
numargs]
    ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Maybe [Tok]
optarg <- Maybe [Tok]
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Maybe [Tok]
forall a. Maybe a
Nothing (ParsecT [Tok] LaTeXState m (Maybe [Tok])
 -> ParsecT [Tok] LaTeXState m (Maybe [Tok]))
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall a b. (a -> b) -> a -> b
$ [Tok] -> Maybe [Tok]
forall a. a -> Maybe a
Just ([Tok] -> Maybe [Tok])
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
    ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    [Tok]
contents' <- ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    let contents :: [Tok]
contents =
         case Text
mtype of
              Text
"DeclareMathOperator" ->
                 SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"mathop") Text
"\\mathop"
                 Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"{"
                 Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"mathrm") Text
"\\mathrm"
                 Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"{"
                 Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: ([Tok]
contents' [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++
                   [ SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"}", SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"}" ])
              Text
_                     -> [Tok]
contents'
    let macro :: Macro
macro = MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
optarg [Tok]
contents
    (do Text -> LP m Macro
forall (m :: * -> *). PandocMonad m => Text -> LP m Macro
lookupMacro Text
name
        case Text
mtype of
          Text
"providecommand" -> [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return []
          Text
"renewcommand" -> [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, Macro
macro)]
          Text
_ -> [] [(Text, Macro)]
-> ParsecT [Tok] LaTeXState m () -> LP m [(Text, Macro)]
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ LogMessage -> ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (Text -> SourcePos -> LogMessage
MacroAlreadyDefined Text
txt SourcePos
pos))
      LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(Text
name, Macro
macro)]

newenvironment :: PandocMonad m => LP m (Maybe (Text, Macro, Macro))
newenvironment :: LP m (Maybe (Text, Macro, Macro))
newenvironment = do
  SourcePos
pos <- ParsecT [Tok] LaTeXState m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
  Tok SourcePos
_ (CtrlSeq Text
mtype) Text
_ <- Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"newenvironment" LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                             Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"renewenvironment" LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                             Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"provideenvironment"
  LP m (Maybe (Text, Macro, Macro))
-> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m (Maybe (Text, Macro, Macro))
 -> LP m (Maybe (Text, Macro, Macro)))
-> LP m (Maybe (Text, Macro, Macro))
-> LP m (Maybe (Text, Macro, Macro))
forall a b. (a -> b) -> a -> b
$ do
    LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok))
-> LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall a b. (a -> b) -> a -> b
$ Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'*'
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Text
name <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Int
numargs <- Int
-> ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Int
0 (ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int)
-> ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT [Tok] LaTeXState m Int
forall (m :: * -> *). PandocMonad m => LP m Int
bracketedNum
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Maybe [Tok]
optarg <- Maybe [Tok]
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Maybe [Tok]
forall a. Maybe a
Nothing (ParsecT [Tok] LaTeXState m (Maybe [Tok])
 -> ParsecT [Tok] LaTeXState m (Maybe [Tok]))
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall a b. (a -> b) -> a -> b
$ [Tok] -> Maybe [Tok]
forall a. a -> Maybe a
Just ([Tok] -> Maybe [Tok])
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
    let argspecs :: [ArgSpec]
argspecs = (Int -> ArgSpec) -> [Int] -> [ArgSpec]
forall a b. (a -> b) -> [a] -> [b]
map (\Int
i -> Int -> ArgSpec
ArgNum Int
i) [Int
1..Int
numargs]
    [Tok]
startcontents <- LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    [Tok]
endcontents <- LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    -- we need the environment to be in a group so macros defined
    -- inside behave correctly:
    let bg :: Tok
bg = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"bgroup") Text
"\\bgroup "
    let eg :: Tok
eg = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"egroup") Text
"\\egroup "
    let result :: (Text, Macro, Macro)
result = (Text
name,
                    MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
optarg
                      (Tok
bgTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:[Tok]
startcontents),
                    MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
                      ([Tok]
endcontents [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok
eg]))
    (do Text -> LP m Macro
forall (m :: * -> *). PandocMonad m => Text -> LP m Macro
lookupMacro Text
name
        case Text
mtype of
          Text
"provideenvironment" -> Maybe (Text, Macro, Macro) -> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Text, Macro, Macro)
forall a. Maybe a
Nothing
          Text
"renewenvironment" -> Maybe (Text, Macro, Macro) -> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. Monad m => a -> m a
return ((Text, Macro, Macro) -> Maybe (Text, Macro, Macro)
forall a. a -> Maybe a
Just (Text, Macro, Macro)
result)
          Text
_ -> do
             LogMessage -> LP m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> LP m ()) -> LogMessage -> LP m ()
forall a b. (a -> b) -> a -> b
$ Text -> SourcePos -> LogMessage
MacroAlreadyDefined Text
name SourcePos
pos
             Maybe (Text, Macro, Macro) -> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Text, Macro, Macro)
forall a. Maybe a
Nothing)
      LP m (Maybe (Text, Macro, Macro))
-> LP m (Maybe (Text, Macro, Macro))
-> LP m (Maybe (Text, Macro, Macro))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (Text, Macro, Macro) -> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. Monad m => a -> m a
return ((Text, Macro, Macro) -> Maybe (Text, Macro, Macro)
forall a. a -> Maybe a
Just (Text, Macro, Macro)
result)

bracketedNum :: PandocMonad m => LP m Int
bracketedNum :: LP m Int
bracketedNum = do
  Text
ds <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
  case Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
ds of
       Just Int
i -> Int -> LP m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
       Maybe Int
_      -> Int -> LP m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0