% Package for CFA Research Lab.
% (Now more a personal collection and testing grounds for common.sty.)
%
% This is a collection of commands everyone working on CFA related documents
% should find useful. So mostly programming language related tools.
%
% 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}

% Tip for commands that end with \xspace: if the default is not correct then
% follow the command with {} to disable \xspace, use '{} ' to force add a
% space and '{}<whatever-follows>' to force remove one.
%
% \CFA
% Cforall with the forall symbol.
\newrobustcmd\CFA{\textsf{C\raisebox{\depth}{\rotatebox{180}{A}}}\xspace}
% \Cpp[<std>]
% C++ symbol name. You may optionally provide <std> to specify a standard.
\newrobustcmd\Cpp[1][\xspace]{C++#1}

% 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{}
      \def\code#1#2{#2}
    }
  }{}
}

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

% \code{<language>}{<code>}
% Use the listings package to format the snipit of <code> in <language>.
\newrobustcmd*\code[2]{\lstinline[language=#1]{#2}}

% \begin{cfa}[<options>]
% \end{cfa}
% 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.7ex}{\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,
}

% \snake{<identifier>}
% Improves writing of snake case (or any convention that uses _) by allowing
% line breaks after _. Disables commands inside the block and formats the
% identifier to look like code.
\newcommand*\snake[1]{\snakefont{\expandafter\snake@next\detokenize{#1}\@nil}}

% \snakefont{<text>}
% Command used by \snake, you may renew the command to change its formating.
\newcommand*\snakefont[1]{\texttt{#1}}

% Thanks Manuel of TeX Stack exchange. (I got the base pattern from one of
% their answers.) Note: \@nil should never be defined.
\newcommand*\snake@next[1]{\ifx\@nil#1\else
  \expandafter\ifx\string_#1\string_\allowbreak\else#1\fi
  \expandafter\snake@next\fi
}

% These somehow control how much of a page can be a floating element before
% the float is forced onto its own page.   
\renewcommand{\topfraction}{0.8}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\floatpagefraction}{0.8}
% Sort of the reverse, I think it is the minimum amount of text that can
% be on a page before its all removed. (0 for always fix what you can.)
\renewcommand{\textfraction}{0.0}

% 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
