    {-# OPTIONS_GHC -Wunused-imports #-}

    module Agda.Interaction.Highlighting.LaTeX.Backend
      ( latexBackend
      ) where

    import Agda.Interaction.Highlighting.LaTeX.Base
      ( LaTeXOptions(..)
      , MonadLogLaTeX(logLaTeX)
      , logMsgToText
      , generateLaTeXIO
      , prepareCommonAssets
      )

    import Control.DeepSeq
    import Control.Monad.Trans (MonadIO)

    import           Data.Functor ( (<&>) )
    import qualified Data.Map     as Map
    import           Data.Map     ( Map )
    import qualified Data.Text    as T

    import GHC.Generics (Generic)

    import System.FilePath ( (</>) )

    import Agda.Compiler.Backend (Backend,Backend_boot(..), Backend',Backend'_boot(..), Definition, Recompile(..))
    import Agda.Compiler.Common (curIF, IsMain(IsMain, NotMain))

    import Agda.Interaction.Options
      ( ArgDescr(NoArg, ReqArg)
      , CommandLineOptions ( optGHCiInteraction, optPragmaOptions )
      , optCountClusters
      , Flag
      , OptDescr(..)
      )

    import Agda.Syntax.Position (mkRangeFile, rangeFilePath)
    import Agda.Syntax.TopLevelModuleName (TopLevelModuleName, projectRoot)

    import Agda.TypeChecking.Monad
      ( HasOptions(commandLineOptions)
      , MonadDebug
      , stModuleToSourceId
      , useTC
      , ReadTCState
      , reportS
      , MonadFileId
      , srcFilePath
      )

    import Agda.Utils.FileName (filePath, mkAbsolute)

    ------------------------------------------------------------------------
    -- * Main.

    -- Command-line flag options, prior to e.g. path resolution and validation.
    data LaTeXFlags = LaTeXFlags
      { LaTeXFlags -> [Char]
    latexFlagOutDir        :: FilePath
      , LaTeXFlags -> Maybe [Char]
    latexFlagSourceFile    :: Maybe FilePath
      , LaTeXFlags -> Bool
    latexFlagGenerateLaTeX :: Bool
        -- ^ Are we going to try to generate LaTeX at all?
      } deriving (LaTeXFlags -> LaTeXFlags -> Bool
    (LaTeXFlags -> LaTeXFlags -> Bool)
    -> (LaTeXFlags -> LaTeXFlags -> Bool) -> Eq LaTeXFlags
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: LaTeXFlags -> LaTeXFlags -> Bool
    == :: LaTeXFlags -> LaTeXFlags -> Bool
    $c/= :: LaTeXFlags -> LaTeXFlags -> Bool
    /= :: LaTeXFlags -> LaTeXFlags -> Bool
    Eq, (forall x. LaTeXFlags -> Rep LaTeXFlags x)
    -> (forall x. Rep LaTeXFlags x -> LaTeXFlags) -> Generic LaTeXFlags
    forall x. Rep LaTeXFlags x -> LaTeXFlags
    forall x. LaTeXFlags -> Rep LaTeXFlags x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. LaTeXFlags -> Rep LaTeXFlags x
    from :: forall x. LaTeXFlags -> Rep LaTeXFlags x
    $cto :: forall x. Rep LaTeXFlags x -> LaTeXFlags
    to :: forall x. Rep LaTeXFlags x -> LaTeXFlags
    Generic)

    instance NFData LaTeXFlags

    -- | The default output directory for LaTeX.

    defaultLaTeXDir :: FilePath
    defaultLaTeXDir :: [Char]
    defaultLaTeXDir = [Char]
    "latex"

    defaultLaTeXFlags :: LaTeXFlags
    defaultLaTeXFlags :: LaTeXFlags
    defaultLaTeXFlags = LaTeXFlags
      { latexFlagOutDir :: [Char]
    latexFlagOutDir        = [Char]
    defaultLaTeXDir
      , latexFlagSourceFile :: Maybe [Char]
    latexFlagSourceFile    = Maybe [Char]
    forall a. Maybe a
    Nothing
      , latexFlagGenerateLaTeX :: Bool
    latexFlagGenerateLaTeX = Bool
    False
      }

    latexFlagsDescriptions :: [OptDescr (Flag LaTeXFlags)]
    latexFlagsDescriptions :: [OptDescr (Flag LaTeXFlags)]
    latexFlagsDescriptions =
      [ [Char]
    -> [[Char]]
    -> ArgDescr (Flag LaTeXFlags)
    -> [Char]
    -> OptDescr (Flag LaTeXFlags)
    forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
    Option []     [[Char]
    "latex"] (Flag LaTeXFlags -> ArgDescr (Flag LaTeXFlags)
    forall a. a -> ArgDescr a
    NoArg Flag LaTeXFlags
    latexFlag)
                      [Char]
    "generate LaTeX with highlighted source code"
      , [Char]
    -> [[Char]]
    -> ArgDescr (Flag LaTeXFlags)
    -> [Char]
    -> OptDescr (Flag LaTeXFlags)
    forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
    Option []     [[Char]
    "latex-dir"] (([Char] -> Flag LaTeXFlags) -> [Char] -> ArgDescr (Flag LaTeXFlags)
    forall a. ([Char] -> a) -> [Char] -> ArgDescr a
    ReqArg [Char] -> Flag LaTeXFlags
    latexDirFlag [Char]
    "DIR")
                      ([Char]
    "directory in which LaTeX files are placed (default: " [Char] -> [Char] -> [Char]
    forall a. [a] -> [a] -> [a]
    ++
                        [Char]
    defaultLaTeXDir [Char] -> [Char] -> [Char]
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    ")")
      ]

    latexFlag :: Flag LaTeXFlags
    latexFlag :: Flag LaTeXFlags
    latexFlag LaTeXFlags
    o = Flag LaTeXFlags
    forall a. a -> OptM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Flag LaTeXFlags -> Flag LaTeXFlags
    forall a b. (a -> b) -> a -> b
    $ LaTeXFlags
    o { latexFlagGenerateLaTeX = True }

    latexDirFlag :: FilePath -> Flag LaTeXFlags
    latexDirFlag :: [Char] -> Flag LaTeXFlags
    latexDirFlag [Char]
    d LaTeXFlags
    o = Flag LaTeXFlags
    forall a. a -> OptM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Flag LaTeXFlags -> Flag LaTeXFlags
    forall a b. (a -> b) -> a -> b
    $ LaTeXFlags
    o { latexFlagOutDir = d }

    data LaTeXCompileEnv = LaTeXCompileEnv LaTeXFlags
    data LaTeXModuleEnv  = LaTeXModuleEnv LaTeXOptions
    data LaTeXModule     = LaTeXModule
    data LaTeXDef        = LaTeXDef

    latexBackend :: Backend
    latexBackend :: Backend
    latexBackend = Backend'_boot
      Definition
      TCM
      LaTeXFlags
      LaTeXCompileEnv
      LaTeXModuleEnv
      LaTeXModule
      LaTeXDef
    -> Backend
    forall opts definition (tcm :: * -> *) env menv mod def.
    NFData opts =>
    Backend'_boot definition tcm opts env menv mod def
    -> Backend_boot definition tcm
    Backend Backend'_boot
      Definition
      TCM
      LaTeXFlags
      LaTeXCompileEnv
      LaTeXModuleEnv
      LaTeXModule
      LaTeXDef
    latexBackend'

    latexBackend' :: Backend' LaTeXFlags LaTeXCompileEnv LaTeXModuleEnv LaTeXModule LaTeXDef
    latexBackend' :: Backend'_boot
      Definition
      TCM
      LaTeXFlags
      LaTeXCompileEnv
      LaTeXModuleEnv
      LaTeXModule
      LaTeXDef
    latexBackend' = Backend'
      { backendName :: BackendName
    backendName           = BackendName
    "LaTeX"
      , backendVersion :: Maybe BackendName
    backendVersion        = Maybe BackendName
    forall a. Maybe a
    Nothing
      , options :: LaTeXFlags
    options               = LaTeXFlags
    defaultLaTeXFlags
      , commandLineFlags :: [OptDescr (Flag LaTeXFlags)]
    commandLineFlags      = [OptDescr (Flag LaTeXFlags)]
    latexFlagsDescriptions
      , isEnabled :: LaTeXFlags -> Bool
    isEnabled             = LaTeXFlags -> Bool
    latexFlagGenerateLaTeX
      , preCompile :: LaTeXFlags -> TCM LaTeXCompileEnv
    preCompile            = LaTeXFlags -> TCM LaTeXCompileEnv
    forall (m :: * -> *).
    Applicative m =>
    LaTeXFlags -> m LaTeXCompileEnv
    preCompileLaTeX
      , preModule :: LaTeXCompileEnv
    -> IsMain
    -> TopLevelModuleName
    -> Maybe [Char]
    -> TCM (Recompile LaTeXModuleEnv LaTeXModule)
    preModule             = LaTeXCompileEnv
    -> IsMain
    -> TopLevelModuleName
    -> Maybe [Char]
    -> TCM (Recompile LaTeXModuleEnv LaTeXModule)
    forall (m :: * -> *).
    (HasOptions m, ReadTCState m, MonadFileId m) =>
    LaTeXCompileEnv
    -> IsMain
    -> TopLevelModuleName
    -> Maybe [Char]
    -> m (Recompile LaTeXModuleEnv LaTeXModule)
    preModuleLaTeX
      , compileDef :: LaTeXCompileEnv
    -> LaTeXModuleEnv -> IsMain -> Definition -> TCM LaTeXDef
    compileDef            = LaTeXCompileEnv
    -> LaTeXModuleEnv -> IsMain -> Definition -> TCM LaTeXDef
    forall (m :: * -> *).
    Applicative m =>
    LaTeXCompileEnv
    -> LaTeXModuleEnv -> IsMain -> Definition -> m LaTeXDef
    compileDefLaTeX
      , postModule :: LaTeXCompileEnv
    -> LaTeXModuleEnv
    -> IsMain
    -> TopLevelModuleName
    -> [LaTeXDef]
    -> TCM LaTeXModule
    postModule            = LaTeXCompileEnv
    -> LaTeXModuleEnv
    -> IsMain
    -> TopLevelModuleName
    -> [LaTeXDef]
    -> TCM LaTeXModule
    forall (m :: * -> *).
    (MonadDebug m, ReadTCState m, MonadIO m) =>
    LaTeXCompileEnv
    -> LaTeXModuleEnv
    -> IsMain
    -> TopLevelModuleName
    -> [LaTeXDef]
    -> m LaTeXModule
    postModuleLaTeX
      , postCompile :: LaTeXCompileEnv
    -> IsMain -> Map TopLevelModuleName LaTeXModule -> TCM ()
    postCompile           = LaTeXCompileEnv
    -> IsMain -> Map TopLevelModuleName LaTeXModule -> TCM ()
    forall (m :: * -> *).
    Applicative m =>
    LaTeXCompileEnv
    -> IsMain -> Map TopLevelModuleName LaTeXModule -> m ()
    postCompileLaTeX
      , scopeCheckingSuffices :: Bool
    scopeCheckingSuffices = Bool
    True
      , mayEraseType :: QName -> TCM Bool
    mayEraseType          = TCM Bool -> QName -> TCM Bool
    forall a b. a -> b -> a
    const (TCM Bool -> QName -> TCM Bool) -> TCM Bool -> QName -> TCM Bool
    forall a b. (a -> b) -> a -> b
    $ Bool -> TCM Bool
    forall a. a -> TCM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False
      , backendInteractTop :: Maybe (BackendCommandTop TCM)
    backendInteractTop    = Maybe (BackendCommandTop TCM)
    forall a. Maybe a
    Nothing
      , backendInteractHole :: Maybe (BackendCommandHole TCM)
    backendInteractHole   = Maybe (BackendCommandHole TCM)
    forall a. Maybe a
    Nothing
      }

    -- | A wrapper to implement 'MonadLogLaTeX'.
    newtype LogLaTeXDebugT m a = LogLaTeXDebugT { forall (m :: * -> *) a. LogLaTeXDebugT m a -> m a
    runLogLaTeXDebugT :: m a }
      deriving ((forall a b. (a -> b) -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b)
    -> (forall a b. a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a)
    -> Functor (LogLaTeXDebugT m)
    forall a b. a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a
    forall a b. (a -> b) -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b
    forall (m :: * -> *) a b.
    Functor m =>
    a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a
    forall (m :: * -> *) a b.
    Functor m =>
    (a -> b) -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b
    forall (f :: * -> *).
    (forall a b. (a -> b) -> f a -> f b)
    -> (forall a b. a -> f b -> f a) -> Functor f
    $cfmap :: forall (m :: * -> *) a b.
    Functor m =>
    (a -> b) -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b
    fmap :: forall a b. (a -> b) -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b
    $c<$ :: forall (m :: * -> *) a b.
    Functor m =>
    a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a
    <$ :: forall a b. a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a
    Functor, Functor (LogLaTeXDebugT m)
    Functor (LogLaTeXDebugT m) =>
    (forall a. a -> LogLaTeXDebugT m a)
    -> (forall a b.
        LogLaTeXDebugT m (a -> b)
        -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b)
    -> (forall a b c.
        (a -> b -> c)
        -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m c)
    -> (forall a b.
        LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b)
    -> (forall a b.
        LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a)
    -> Applicative (LogLaTeXDebugT m)
    forall a. a -> LogLaTeXDebugT m a
    forall a b.
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a
    forall a b.
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b
    forall a b.
    LogLaTeXDebugT m (a -> b)
    -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b
    forall a b c.
    (a -> b -> c)
    -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m c
    forall (f :: * -> *).
    Functor f =>
    (forall a. a -> f a)
    -> (forall a b. f (a -> b) -> f a -> f b)
    -> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
    -> (forall a b. f a -> f b -> f b)
    -> (forall a b. f a -> f b -> f a)
    -> Applicative f
    forall (m :: * -> *). Applicative m => Functor (LogLaTeXDebugT m)
    forall (m :: * -> *) a. Applicative m => a -> LogLaTeXDebugT m a
    forall (m :: * -> *) a b.
    Applicative m =>
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a
    forall (m :: * -> *) a b.
    Applicative m =>
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b
    forall (m :: * -> *) a b.
    Applicative m =>
    LogLaTeXDebugT m (a -> b)
    -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b
    forall (m :: * -> *) a b c.
    Applicative m =>
    (a -> b -> c)
    -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m c
    $cpure :: forall (m :: * -> *) a. Applicative m => a -> LogLaTeXDebugT m a
    pure :: forall a. a -> LogLaTeXDebugT m a
    $c<*> :: forall (m :: * -> *) a b.
    Applicative m =>
    LogLaTeXDebugT m (a -> b)
    -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b
    <*> :: forall a b.
    LogLaTeXDebugT m (a -> b)
    -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b
    $cliftA2 :: forall (m :: * -> *) a b c.
    Applicative m =>
    (a -> b -> c)
    -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m c
    liftA2 :: forall a b c.
    (a -> b -> c)
    -> LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m c
    $c*> :: forall (m :: * -> *) a b.
    Applicative m =>
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b
    *> :: forall a b.
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b
    $c<* :: forall (m :: * -> *) a b.
    Applicative m =>
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a
    <* :: forall a b.
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m a
    Applicative, Applicative (LogLaTeXDebugT m)
    Applicative (LogLaTeXDebugT m) =>
    (forall a b.
     LogLaTeXDebugT m a
     -> (a -> LogLaTeXDebugT m b) -> LogLaTeXDebugT m b)
    -> (forall a b.
        LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b)
    -> (forall a. a -> LogLaTeXDebugT m a)
    -> Monad (LogLaTeXDebugT m)
    forall a. a -> LogLaTeXDebugT m a
    forall a b.
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b
    forall a b.
    LogLaTeXDebugT m a
    -> (a -> LogLaTeXDebugT m b) -> LogLaTeXDebugT m b
    forall (m :: * -> *). Monad m => Applicative (LogLaTeXDebugT m)
    forall (m :: * -> *) a. Monad m => a -> LogLaTeXDebugT m a
    forall (m :: * -> *) a b.
    Monad m =>
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b
    forall (m :: * -> *) a b.
    Monad m =>
    LogLaTeXDebugT m a
    -> (a -> LogLaTeXDebugT m b) -> LogLaTeXDebugT m b
    forall (m :: * -> *).
    Applicative m =>
    (forall a b. m a -> (a -> m b) -> m b)
    -> (forall a b. m a -> m b -> m b)
    -> (forall a. a -> m a)
    -> Monad m
    $c>>= :: forall (m :: * -> *) a b.
    Monad m =>
    LogLaTeXDebugT m a
    -> (a -> LogLaTeXDebugT m b) -> LogLaTeXDebugT m b
    >>= :: forall a b.
    LogLaTeXDebugT m a
    -> (a -> LogLaTeXDebugT m b) -> LogLaTeXDebugT m b
    $c>> :: forall (m :: * -> *) a b.
    Monad m =>
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b
    >> :: forall a b.
    LogLaTeXDebugT m a -> LogLaTeXDebugT m b -> LogLaTeXDebugT m b
    $creturn :: forall (m :: * -> *) a. Monad m => a -> LogLaTeXDebugT m a
    return :: forall a. a -> LogLaTeXDebugT m a
    Monad, Monad (LogLaTeXDebugT m)
    Monad (LogLaTeXDebugT m) =>
    (forall a. IO a -> LogLaTeXDebugT m a)
    -> MonadIO (LogLaTeXDebugT m)
    forall a. IO a -> LogLaTeXDebugT m a
    forall (m :: * -> *).
    Monad m =>
    (forall a. IO a -> m a) -> MonadIO m
    forall (m :: * -> *). MonadIO m => Monad (LogLaTeXDebugT m)
    forall (m :: * -> *) a. MonadIO m => IO a -> LogLaTeXDebugT m a
    $cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> LogLaTeXDebugT m a
    liftIO :: forall a. IO a -> LogLaTeXDebugT m a
    MonadIO)

    instance MonadDebug m => MonadLogLaTeX (LogLaTeXDebugT m) where
      logLaTeX :: LogMessage -> LogLaTeXDebugT m ()
    logLaTeX = m () -> LogLaTeXDebugT m ()
    forall (m :: * -> *) a. m a -> LogLaTeXDebugT m a
    LogLaTeXDebugT (m () -> LogLaTeXDebugT m ())
    -> (LogMessage -> m ()) -> LogMessage -> LogLaTeXDebugT m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ([Char] -> VerboseLevel -> [Char] -> m ()
    forall a (m :: * -> *).
    (ReportS a, MonadDebug m) =>
    [Char] -> VerboseLevel -> a -> m ()
    forall (m :: * -> *).
    MonadDebug m =>
    [Char] -> VerboseLevel -> [Char] -> m ()
    reportS [Char]
    "compile.latex" VerboseLevel
    1) ([Char] -> m ()) -> (LogMessage -> [Char]) -> LogMessage -> m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . BackendName -> [Char]
    T.unpack (BackendName -> [Char])
    -> (LogMessage -> BackendName) -> LogMessage -> [Char]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . LogMessage -> BackendName
    logMsgToText

    -- Resolve the raw flags into usable LaTeX options.
    resolveLaTeXOptions :: (HasOptions m, ReadTCState m, MonadFileId m)
      => LaTeXFlags
      -> TopLevelModuleName
      -> m LaTeXOptions
    resolveLaTeXOptions :: forall (m :: * -> *).
    (HasOptions m, ReadTCState m, MonadFileId m) =>
    LaTeXFlags -> TopLevelModuleName -> m LaTeXOptions
    resolveLaTeXOptions LaTeXFlags
    flags TopLevelModuleName
    moduleName = do
      options <- m CommandLineOptions
    forall (m :: * -> *). HasOptions m => m CommandLineOptions
    commandLineOptions
      modFiles <- useTC stModuleToSourceId
      let msrc = TopLevelModuleName
    -> Map TopLevelModuleName SourceFile -> Maybe SourceFile
    forall k a. Ord k => k -> Map k a -> Maybe a
    Map.lookup TopLevelModuleName
    moduleName Map TopLevelModuleName SourceFile
    modFiles
      mf <- traverse srcFilePath msrc
      let
        mSrcFileName = Maybe AbsolutePath
    mf Maybe AbsolutePath
    -> (AbsolutePath -> RangeFile) -> Maybe RangeFile
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \ AbsolutePath
    f ->
          AbsolutePath -> Maybe TopLevelModuleName -> RangeFile
    mkRangeFile ([Char] -> AbsolutePath
    mkAbsolute (AbsolutePath -> [Char]
    filePath AbsolutePath
    f)) (TopLevelModuleName -> Maybe TopLevelModuleName
    forall a. a -> Maybe a
    Just TopLevelModuleName
    moduleName)
          -- TODO:    ^^^^^^^^^^^^^^^^^^^^^^^^^ can this just be `f`?
        countClusters = PragmaOptions -> Bool
    optCountClusters (PragmaOptions -> Bool)
    -> (CommandLineOptions -> PragmaOptions)
    -> CommandLineOptions
    -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . CommandLineOptions -> PragmaOptions
    optPragmaOptions (CommandLineOptions -> Bool) -> CommandLineOptions -> Bool
    forall a b. (a -> b) -> a -> b
    $ CommandLineOptions
    options
        latexDir = LaTeXFlags -> [Char]
    latexFlagOutDir LaTeXFlags
    flags
        -- FIXME: This reliance on emacs-mode to decide whether to interpret the output location as project-relative or
        -- cwd-relative is gross. Also it currently behaves differently for JSON mode :-/
        -- And it prevents us from doing a real "one-time" setup.
        outDir = case (Maybe RangeFile
    mSrcFileName, CommandLineOptions -> Bool
    optGHCiInteraction CommandLineOptions
    options) of
          (Just RangeFile
    sourceFile, Bool
    True) ->
            AbsolutePath -> [Char]
    filePath (AbsolutePath -> TopLevelModuleName -> AbsolutePath
    projectRoot (RangeFile -> AbsolutePath
    rangeFilePath RangeFile
    sourceFile) TopLevelModuleName
    moduleName) [Char] -> [Char] -> [Char]
    </>
            [Char]
    latexDir
          (Maybe RangeFile, Bool)
    _ -> [Char]
    latexDir
      return LaTeXOptions
        { latexOptOutDir         = outDir
        , latexOptSourceFileName = mSrcFileName
        , latexOptCountClusters  = countClusters
        }

    preCompileLaTeX
      :: Applicative m
      => LaTeXFlags
      -> m LaTeXCompileEnv
    preCompileLaTeX :: forall (m :: * -> *).
    Applicative m =>
    LaTeXFlags -> m LaTeXCompileEnv
    preCompileLaTeX LaTeXFlags
    flags = LaTeXCompileEnv -> m LaTeXCompileEnv
    forall a. a -> m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (LaTeXCompileEnv -> m LaTeXCompileEnv)
    -> LaTeXCompileEnv -> m LaTeXCompileEnv
    forall a b. (a -> b) -> a -> b
    $ LaTeXFlags -> LaTeXCompileEnv
    LaTeXCompileEnv LaTeXFlags
    flags

    preModuleLaTeX
      :: (HasOptions m, ReadTCState m, MonadFileId m)
      => LaTeXCompileEnv
      -> IsMain
      -> TopLevelModuleName
      -> Maybe FilePath
      -> m (Recompile LaTeXModuleEnv LaTeXModule)
    preModuleLaTeX :: forall (m :: * -> *).
    (HasOptions m, ReadTCState m, MonadFileId m) =>
    LaTeXCompileEnv
    -> IsMain
    -> TopLevelModuleName
    -> Maybe [Char]
    -> m (Recompile LaTeXModuleEnv LaTeXModule)
    preModuleLaTeX (LaTeXCompileEnv LaTeXFlags
    flags) IsMain
    isMain TopLevelModuleName
    moduleName Maybe [Char]
    _ifacePath = case IsMain
    isMain of
      IsMain
    IsMain  -> LaTeXModuleEnv -> Recompile LaTeXModuleEnv LaTeXModule
    forall menv mod. menv -> Recompile menv mod
    Recompile (LaTeXModuleEnv -> Recompile LaTeXModuleEnv LaTeXModule)
    -> (LaTeXOptions -> LaTeXModuleEnv)
    -> LaTeXOptions
    -> Recompile LaTeXModuleEnv LaTeXModule
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . LaTeXOptions -> LaTeXModuleEnv
    LaTeXModuleEnv (LaTeXOptions -> Recompile LaTeXModuleEnv LaTeXModule)
    -> m LaTeXOptions -> m (Recompile LaTeXModuleEnv LaTeXModule)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LaTeXFlags -> TopLevelModuleName -> m LaTeXOptions
    forall (m :: * -> *).
    (HasOptions m, ReadTCState m, MonadFileId m) =>
    LaTeXFlags -> TopLevelModuleName -> m LaTeXOptions
    resolveLaTeXOptions LaTeXFlags
    flags TopLevelModuleName
    moduleName
      IsMain
    NotMain -> Recompile LaTeXModuleEnv LaTeXModule
    -> m (Recompile LaTeXModuleEnv LaTeXModule)
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Recompile LaTeXModuleEnv LaTeXModule
     -> m (Recompile LaTeXModuleEnv LaTeXModule))
    -> Recompile LaTeXModuleEnv LaTeXModule
    -> m (Recompile LaTeXModuleEnv LaTeXModule)
    forall a b. (a -> b) -> a -> b
    $ LaTeXModule -> Recompile LaTeXModuleEnv LaTeXModule
    forall menv mod. mod -> Recompile menv mod
    Skip LaTeXModule
    LaTeXModule

    compileDefLaTeX
      :: Applicative m
      => LaTeXCompileEnv
      -> LaTeXModuleEnv
      -> IsMain
      -> Definition
      -> m LaTeXDef
    compileDefLaTeX :: forall (m :: * -> *).
    Applicative m =>
    LaTeXCompileEnv
    -> LaTeXModuleEnv -> IsMain -> Definition -> m LaTeXDef
    compileDefLaTeX LaTeXCompileEnv
    _cenv LaTeXModuleEnv
    _menv IsMain
    _main Definition
    _def = LaTeXDef -> m LaTeXDef
    forall a. a -> m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure LaTeXDef
    LaTeXDef

    postModuleLaTeX
      :: (MonadDebug m, ReadTCState m, MonadIO m)
      => LaTeXCompileEnv
      -> LaTeXModuleEnv
      -> IsMain
      -> TopLevelModuleName
      -> [LaTeXDef]
      -> m LaTeXModule
    postModuleLaTeX :: forall (m :: * -> *).
    (MonadDebug m, ReadTCState m, MonadIO m) =>
    LaTeXCompileEnv
    -> LaTeXModuleEnv
    -> IsMain
    -> TopLevelModuleName
    -> [LaTeXDef]
    -> m LaTeXModule
    postModuleLaTeX LaTeXCompileEnv
    _cenv (LaTeXModuleEnv LaTeXOptions
    latexOpts) IsMain
    _main TopLevelModuleName
    _moduleName [LaTeXDef]
    _defs = do
      i <- m Interface
    forall (m :: * -> *). ReadTCState m => m Interface
    curIF
      runLogLaTeXDebugT do
        -- FIXME: It would be better to do "prepareCommonAssets" in @preCompileLaTeX@, but because
        -- the output directory depends on the module-relative project root (when in emacs-mode),
        -- we can't do that until we see the module.
        -- However, for now that is OK because we only generate LaTeX for the main module.
        prepareCommonAssets (latexOptOutDir latexOpts)
        generateLaTeXIO latexOpts i
      return LaTeXModule

    postCompileLaTeX
      :: Applicative m
      => LaTeXCompileEnv
      -> IsMain
      -> Map TopLevelModuleName LaTeXModule
      -> m ()
    postCompileLaTeX :: forall (m :: * -> *).
    Applicative m =>
    LaTeXCompileEnv
    -> IsMain -> Map TopLevelModuleName LaTeXModule -> m ()
    postCompileLaTeX LaTeXCompileEnv
    _cenv IsMain
    _main Map TopLevelModuleName LaTeXModule
    _modulesByName = () -> m ()
    forall a. a -> m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ()