% Package for CFA Research Lab.
%
% Made by combining and updating various macro files people had made.
%
% Internal commands are prefixed with "\cfalab@".

% I don't know what the oldest LaTeX2e version with everything needed is.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{cfalab}[2020/03/24 v0.1 CFA Laboratory LaTeX Tools]

% Other packages required.
\RequirePackage{etoolbox}
\RequirePackage{listings}
\RequirePackage{xspace}

% Symbols: All symbols are zero argument robust commands with special rules
% about the space following the c.s. token. Normally the space might be
% re-added according to the rules of the xspace package. They may be followed
% by a star (which the command will consume) to disable this behaviour.

% \newsymbolcmd{<command>}{<replacement text>}
%     Defines <command> to be a symbol that has the given <replacement text>.
\newrobustcmd*\newsymbolcmd[2]{\newrobustcmd{#1}{\cfalab@symbol{#2}}}
\def\cfalab@symbol#1{\@ifnextchar*{#1\cfalab@eatstar}{#1\xspace}}
\def\cfalab@eatstar*{}

% Cforall with the forall symbol.
\newsymbolcmd\CFA{\textsf{C}\raisebox{\depth}{\rotatebox{180}{\textsf{A}}}}

% The CFA listings language. Based off of ANCI C and including GCC extensions.
% The UW dialect is the default (and only so far) dialect of the language.
\lstdefinelanguage[UW]{CFA}[ANSI]{C}{
    morekeywords={_Alignas, _Alignof, __alignof, __alignof__, asm, __asm,
        __asm__, __attribute, __attribute__, auto, _Bool, catch, catchResume,
        choose, _Complex, __complex, __complex__, __const, __const__,
        coroutine, disable, dtype, enable, exception, __extension__,
        fallthrough, fallthru, finally, __float80, float80, __float128,
        float128, forall, ftype, generator, _Generic, _Imaginary, __imag,
        __imag__, inline, __inline, __inline__, __int128, int128, __label__,
        monitor, mutex, _Noreturn, one_t, or, otype, restrict, resume,
        __restrict, __restrict__, __signed, __signed__, _Static_assert,
        suspend, thread, _Thread_local, throw, throwResume, timeout, trait,
        try, ttype, typeof, __typeof, __typeof__, virtual, __volatile,
        __volatile__, waitfor, when, with, zero_t
    },
    moredirectives={defined,include_next},
}
\lstset{defaultdialect={[UW]CFA}}

% The cfalab style defines some common settings useful in different languages.
\lstdefinestyle{cfalab}{%
    columns=fullflexible,
    basicstyle=\linespread{0.9}\tt,
    stringstyle=\tt,
}

% \code*[<escape character>]{<code>}
%     Use the listings package to format a snipit of <code>.
%     The <escape character> must be a character that does not appear in
%     <code> and defaults to a backtick.
\newcommand*\codeC[2][\`]{\lstinline[language=C]#1#2#1}
\newcommand*\codeCFA[2][\`]{\lstinline[language=CFA]#1#2#1}

% \settextunderscore{(new|old)}
%     Redefines the underscore either as a new repersentation or the old one.
%     Not that some other packages (ex. hyperref) can override this. Set it
%     up after loading them.
\let\cfalab@textunderscore@old=\textunderscore
\newcommand\cfalab@textunderscore@new{%
    \leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
\newcommand\settextunderscore[1]{%
    \renewcommand\textunderscore{\csuse{cfalab@textunderscore@#1}}}

% This is executed very early in the \begin{document} code.
\AtEndPreamble{
  \@ifpackageloaded{hyperref}{
    % Convert symbols to pdf compatable forms when required.
    \pdfstringdefDisableCommands{
      \def\CFA{CFA}
    }
  }{}
}

\endinput
