-- Parse a control sequence and try to expand or execute it.
-- If no definition is known, return the token unmodified.
ctrlseq :: HandleTeXIO m => LexerT m [Token]
ctrlseq = do
  t@(CtrlSeq name active) <- ctrlseqNoExpand
  st <- getState
  case lookupMacroCmd (name, active) st of
    Just m@MacroCmdUser{} -> [] <$ expand m
    Just (MacroCmdPrim p)
      | p `elem` primitiveConstants -> return [t]
      | otherwise -> executePrimitive p
    Just (MacroCmdChar ch cc) -> [] <$ prependTokens [TeXChar ch cc]
    Nothing -> return [t]

---------------------------------------- Execute TeX primitives

-- | Meanings of primitives.
--
-- These internal executable commands cannot be changed by the user.
ctrlseq :: HandleTeXIO m => LexerT m [Token]
ctrlseq = do
  t@(CtrlSeq name active) <- ctrlseqNoExpand
  st <- getState
  case lookupMacroCmd (name, active) st of
    Just m@MacroCmdUser{} -> [] <$ expand m
    Just (MacroCmdPrim p)
      | p `elem` primitiveConstants -> return [t]
      | otherwise -> executePrimitive p
    Just (MacroCmdChar ch cc) -> [] <$ prependTokens [TeXChar ch cc]
    Nothing -> return [t]

---------------------------------------- Execute TeX primitives

-- | Meanings of primitives.
--
-- These internal executable commands cannot be changed by the user.
ctrlseq = do
  t@(CtrlSeq name active) <- ctrlseqNoExpand
  st <- getState
  case lookupMacroCmd (name, active) st of
    Just m@MacroCmdUser{} -> [] <$ expand m
    Just (MacroCmdPrim p)
      | p `elem` primitiveConstants -> return [t]
      | otherwise -> executePrimitive p
    Just (MacroCmdChar ch cc) -> [] <$ prependTokens [TeXChar ch cc]
    Nothing -> return [t]

---------------------------------------- Execute TeX primitives

-- | Meanings of primitives.
--
-- These internal executable commands cannot be changed by the user.