diff --git a/src/Typst/Evaluate.hs b/src/Typst/Evaluate.hs index b58cded..b7567a3 100644 --- a/src/Typst/Evaluate.hs +++ b/src/Typst/Evaluate.hs @@ -67,7 +67,10 @@ evaluateTypst operations = initialEvalState :: EvalState m initialEvalState = - emptyEvalState { evalIdentifiers = [(BlockScope, standardModule')] } + emptyEvalState { evalIdentifiers = [(BlockScope, mempty)] + , evalMathIdentifiers = [(BlockScope, mathModule <> symModule)] + , evalStandardIdentifiers = [(BlockScope, standardModule')] + } where standardModule' = M.insert "eval" evalFunction standardModule evalFunction = makeFunction $ do @@ -239,12 +242,9 @@ pElt = do [("level", VInteger (fromIntegral level))] } Equation display ms -> inBlock BlockScope $ do - importModule mathModule - importModule symModule oldMath <- evalMath <$> getState updateState $ \st -> st {evalMath = True} content <- pInnerContents ms - updateState $ \st -> st {evalMath = oldMath} element "equation" Arguments @@ -254,7 +254,7 @@ pElt = do [ ("block", VBoolean display), ("numbering", VNone) ] - } + } <* updateState (\st -> st {evalMath = oldMath}) MFrac numexp denexp -> do let handleParens (MGroup (Just "(") (Just ")") xs) = MGroup Nothing Nothing xs handleParens x = x @@ -1098,9 +1098,7 @@ addIdentifier ident val = do case identifiers of [] -> fail "Empty evalIdentifiers" ((s, i) : is) -> updateState $ \st -> - st - { evalIdentifiers = (s, M.insert ident val i) : is - } + st { evalIdentifiers = (s, M.insert ident val i) : is } updateIdentifier :: Monad m => Identifier -> Val -> MP m () updateIdentifier ident val = do diff --git a/src/Typst/Module/Standard.hs b/src/Typst/Module/Standard.hs index 1aa5938..160faff 100644 --- a/src/Typst/Module/Standard.hs +++ b/src/Typst/Module/Standard.hs @@ -643,7 +643,10 @@ applyPureFunction (Function f) vals = initialEvalState :: MonadFail m => EvalState m initialEvalState = - emptyEvalState { evalIdentifiers = [(BlockScope, standardModule)] } + emptyEvalState { evalIdentifiers = [(BlockScope, mempty)] + , evalMathIdentifiers = [(BlockScope, mathModule <> symModule)] + , evalStandardIdentifiers = [(BlockScope, standardModule)] + } -- mDigitsRev, mDigits from the unmaintained digits package -- https://hackage.haskell.org/package/digits-0.3.1 diff --git a/src/Typst/Types.hs b/src/Typst/Types.hs index 6a13a1a..a20d536 100644 --- a/src/Typst/Types.hs +++ b/src/Typst/Types.hs @@ -606,6 +606,12 @@ data Operations m = data EvalState m = EvalState { evalIdentifiers :: [(Scope, M.Map Identifier Val)], -- first item is current block, then superordinate block, etc. + -- The standard identifiers and the identifiers that + -- are imported by default into math contexts are special, + -- since both can be overridden by user-defined identifiers. + -- So, we store them separately. + evalStandardIdentifiers :: [(Scope, M.Map Identifier Val)], + evalMathIdentifiers :: [(Scope, M.Map Identifier Val)], evalCounters :: M.Map Counter Integer, evalMath :: Bool, evalShowRules :: [ShowRule], @@ -618,6 +624,8 @@ data EvalState m = EvalState emptyEvalState :: EvalState m emptyEvalState = EvalState { evalIdentifiers = [], + evalStandardIdentifiers = [], + evalMathIdentifiers = [], evalCounters = mempty, evalMath = False, evalShowRules = [], @@ -977,4 +985,12 @@ lookupIdentifier ident = do go ((_, i) : is) = case M.lookup ident i of Just v -> pure v Nothing -> go is - getState >>= go . evalIdentifiers + identifiers <- evalIdentifiers <$> getState + mathIdentifiers <- evalMathIdentifiers <$> getState + standardIdentifiers <- evalStandardIdentifiers <$> getState + math <- evalMath <$> getState + go $ case identifiers of + -- in math mode, we insert the sym and math modules right before + -- the final module (standard module) + (_:_) | math -> identifiers ++ mathIdentifiers ++ standardIdentifiers + _ -> identifiers ++ standardIdentifiers diff --git a/test/out/bugs/math-realize-02.out b/test/out/bugs/math-realize-02.out index 5fae342..e3eb697 100644 --- a/test/out/bugs/math-realize-02.out +++ b/test/out/bugs/math-realize-02.out @@ -341,21 +341,21 @@ document(body: { text(body: [ text(body: [Inline ]), math.equation(block: false, body: { text(body: [2]), - text(body: [|]), - text(body: [(]), - text(body: [α]), - text(body: [,]), - math.lr(body: ({ [(], - text(body: [M]), - text(body: [+]), - math.equation(block: false, - body: { text(body: [a]), - math.attach(b: none, - base: text(body: [b]), - t: text(body: [2])) }, - numbering: none), - [)] })), - text(body: [)]) }, + math.equation(block: true, + body: { text(body: [α]), + math.attach(b: none, + base: math.lr(body: ({ [(], + text(body: [M]), + text(body: [+]), + math.equation(block: false, + body: { text(body: [a]), + math.attach(b: none, + base: text(body: [b]), + t: text(body: [2])) }, + numbering: none), + [)] })), + t: text(body: [2])) }, + numbering: none) }, numbering: none), text(body: [.]), parbreak(), @@ -409,21 +409,21 @@ document(body: { text(body: [ ]), math.equation(block: true, body: { text(body: [2]), - text(body: [|]), - text(body: [(]), - text(body: [α]), - text(body: [,]), - math.lr(body: ({ [(], - text(body: [M]), - text(body: [+]), - math.equation(block: false, - body: { text(body: [a]), - math.attach(b: none, - base: text(body: [b]), - t: text(body: [2])) }, - numbering: none), - [)] })), - text(body: [)]) }, + math.equation(block: true, + body: { text(body: [α]), + math.attach(b: none, + base: math.lr(body: ({ [(], + text(body: [M]), + text(body: [+]), + math.equation(block: false, + body: { text(body: [a]), + math.attach(b: none, + base: text(body: [b]), + t: text(body: [2])) }, + numbering: none), + [)] })), + t: text(body: [2])) }, + numbering: none) }, numbering: none), text(body: [ ]),