% 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.
%
% Access to new basic LaTeX tools and other low level commands.
\RequirePackage{etoolbox}
% Code formatting tools and environments.
\RequirePackage{listings}
% Automatically adds spaces.
\RequirePackage{xspace}
% Improved reference tools.
\RequirePackage[nospace]{varioref}

% 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}}}}
% C++ with kerning. (No standard number support.)
\newsymbolcmd\Cpp{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}}

% This is executed very early in the \begin{document} code, before the
% document's contents but after packages are loaded.
\AtEndPreamble{
  \@ifpackageloaded{hyperref}{
    % Convert symbols to pdf compatable forms when required.
    \pdfstringdefDisableCommands{
      \def\CFA{CFA}
      \def\Cpp{C++}
      \def\lstinline{}
    }
  }{}
}

% \colour{<colour>}{<text>}
% Just \color but using the LaTeX style instead of TeX style command.
\newcommand*\colour[2]{{\color{#1}#2}}

% \code*{<code>}
% Use the listings package to format a snipit of <code>.
\newrobustcmd*\codeCFA[1]{\lstinline[language=CFA]{#1}}
\newrobustcmd*\codeC[1]{\lstinline[language=C]{#1}}
\newrobustcmd*\codeCpp[1]{\lstinline[language=C++]{#1}}
\newrobustcmd*\codePy[1]{\lstinline[language=Python]{#1}}

% Use the listings package to format a block of CFA code.
% Extra listings options can be passed in as an optional argument.
\lstnewenvironment{cfa}[1][]{\lstset{language=CFA}\lstset{#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}}}

% 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}}

% Create an internal paragraph indent amount. This is used internally to
% mimic the standard indent even when it has been overriden in the document.
\newlength\cfalab@parindent
\deflength\cfalab@parindent{\parindent}

% The cfacommon style has many useful defaults for CFA and other types of
% code. Use the listings option "style=cfacommon" to load them.
\lstdefinestyle{cfacommon}{
  columns=fullflexible,
  basicstyle=\linespread{0.9}\sf,
  stringstyle=\tt,
  tabsize=5,
  % Indent code to paragraph indentation.
  xleftmargin=\cfalab@parindent,
  % Allow ASCII characters in the range 128-255.
  extendedchars=true,
  % This allows you to use "math mode" to insert LaTeX into the code.
  % Use \( and \) if you need to insert math mode inside that code.
  escapechar=\$,
  % Disable LaTeX math escape in CFA code $...$
  mathescape=false,
  keepspaces=true,
  % Do not show spaces with cup.
  showstringspaces=false,
  % Show blank lines at end of code.
  showlines=true,
  % Spacing above/below code block.
  aboveskip=4pt,belowskip=0pt,
  numberstyle=\footnotesize\sf,
  % Replace/adjust listing characters that look bad in sanserif.
  literate={-}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.75ex}{0.1ex}}}}1
    {^}{\raisebox{0.6ex}{$\scriptscriptstyle\land\,$}}1
    {~}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}}1 {`}{\ttfamily\upshape\hspace*{-0.1ex}`}1
    {<-}{$\leftarrow$}2 {=>}{$\Rightarrow$}2
    {->}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.8ex}{0.075ex}}}\kern-0.2ex\textgreater}2,
}

% common.tex Compatablity ===================================================
% Below this line is for compatability with the old common.tex file.

% Backwards compatable way to activate the cfacommon style.
\newcommand{\CFAStyle}{\lstset{style=cfacommon}}

% A couple of abbreviations are provided. Just ones someone liked.
%
% Abbreviation formatting commands (renew to customize):
\newcommand{\abbrevFont}{\textit}
%
% Abbreviations that, if not followed by a comma or colon, add a comma.
\newrobustcmd*\cfalab@abbrev@comma{%
  \@ifnextchar{,}{}{\@ifnextchar{:}{}{,\xspace}}}
\providerobustcmd*\eg{\abbrevFont{e}.\abbrevFont{g}.\cfalab@abbrev@comma}
\providerobustcmd*\ie{\abbrevFont{i}.\abbrevFont{e}.\cfalab@abbrev@comma}
%
% Abbreviations that, if not followed by a period, add a period.
\newrobustcmd*\cfalab@abbrev@period{\@ifnextchar{.}{}{.\xspace}}
\providerobustcmd*\etc{\abbrevFont{etc}\cfalab@abbrev@period}
\providerobustcmd*\etal{\abbrevFont{et}~\abbrevFont{al}\cfalab@abbrev@period}
\providerobustcmd*\viz{\abbrevFont{viz}\cfalab@abbrev@period}

\endinput
