%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
%%
%% The contents of this file are covered under the licence agreement in the
%% file "LICENCE" distributed with Cforall.
%%
%% common.tex --
%%
%% Author           : Peter A. Buhr
%% Created On       : Sat Apr  9 10:06:17 2016
%% Last Modified By : Peter A. Buhr
%% Last Modified On : Tue Apr 18 17:16:13 2017
%% Update Count     : 270
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setlength{\textheight}{9in}
%\oddsidemargin 0.0in
\renewcommand{\topfraction}{0.8}		% float must be greater than X of the page before it is forced onto its own page
\renewcommand{\bottomfraction}{0.8}		% float must be greater than X of the page before it is forced onto its own page
\renewcommand{\floatpagefraction}{0.8}	% float must be greater than X of the page before it is forced onto its own page
\renewcommand{\textfraction}{0.0}		% the entire page maybe devoted to floats with no text on the page at all

\lefthyphenmin=4						% hyphen only after 4 characters
\righthyphenmin=4

\usepackage[ignoredisplayed]{enumitem}	% do not affect trivlist
\setlist{labelsep=1ex}% global
\setlist[itemize]{topsep=0.5ex,parsep=0.25ex,itemsep=0.25ex,listparindent=\parindent,leftmargin=\parindent}% global
\setlist[itemize,1]{label=\textbullet}% local
%\renewcommand{\labelitemi}{{\raisebox{0.25ex}{\footnotesize$\bullet$}}}
\setlist[enumerate]{listparindent=\parindent}% global
\setlist[enumerate,2]{leftmargin=\parindent,labelsep=*,align=parleft,label=\alph*.}% local
\setlist[description]{itemsep=0pt,listparindent=\parindent,leftmargin=\parindent,labelsep=1.5ex}

% Names used in the document.

\newcommand{\CFA}{C$\mathbf\forall$\xspace} % Cforall symbolic name
\newcommand{\CFL}{Cforall\xspace} % Cforall symbolic name
\newcommand{\CC}{\rm C\kern-.1em\hbox{+\kern-.25em+}\xspace} % C++ symbolic name
\newcommand{\CCeleven}{\rm C\kern-.1em\hbox{+\kern-.25em+}11\xspace} % C++11 symbolic name
\newcommand{\CCfourteen}{\rm C\kern-.1em\hbox{+\kern-.25em+}14\xspace} % C++14 symbolic name
\newcommand{\CCseventeen}{\rm C\kern-.1em\hbox{+\kern-.25em+}17\xspace} % C++17 symbolic name
\newcommand{\CCtwenty}{\rm C\kern-.1em\hbox{+\kern-.25em+}20\xspace} % C++20 symbolic name
\newcommand{\Celeven}{C11\xspace} % C11 symbolic name
\newcommand{\Csharp}{C\raisebox{-0.7ex}{\Large$^\sharp$}\xspace} % C# symbolic name

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\makeatletter
% parindent is relative, i.e., toggled on/off in environments like itemize, so store the value for
% use rather than use \parident directly.
\newlength{\parindentlnth}
\setlength{\parindentlnth}{\parindent}

\newlength{\gcolumnposn}				% temporary hack because lstlisting does not handle tabs correctly
\newlength{\columnposn}
\setlength{\gcolumnposn}{2.5in}
\setlength{\columnposn}{\gcolumnposn}
\newcommand{\C}[2][\@empty]{\ifx#1\@empty\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\hfill\makebox[\textwidth-\columnposn][l]{\lst@commentstyle{#2}}}
\newcommand{\CRT}{\global\columnposn=\gcolumnposn}

% allow escape sequence in lstinline
%\usepackage{etoolbox}
%\patchcmd{\lsthk@TextStyle}{\let\lst@DefEsc\@empty}{}{}{\errmessage{failed to patch}}

\usepackage{pslatex}					% reduce size of san serif font
\usepackage{relsize}					% must be after change to small or selects old size

% reduce size of chapter/section titles
\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
        \large\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 5\p@
    \fi
    \interlinepenalty\@M
    \Large \bfseries #1\par\nobreak
    \vskip 50\p@
  }}
\def\@makeschapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \Large \bfseries  #1\par\nobreak
    \vskip 50\p@
  }}
\renewcommand\section{\@startsection{section}{1}{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}{1.75ex \@plus .2ex}{\normalfont\large\bfseries}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}{-3.25ex \@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}{-2.5ex \@plus -1ex \@minus -.2ex}{1.0ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}{-2.0ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries}}

% index macros
\newcommand{\italic}[1]{\emph{\hyperpage{#1}}}
\newcommand{\definition}[1]{\textbf{\hyperpage{#1}}}
\newcommand{\see}[1]{\emph{see} #1}

% Define some commands that produce formatted index entries suitable for cross-references.
% ``\spec'' produces entries for specifications of entities.  ``\impl'' produces entries for their
% implementations, and ``\use'' for their uses.

%  \newcommand{\bold}[1]{{\bf #1}}
%  \def\spec{\@bsphack\begingroup
%             \def\protect##1{\string##1\space}\@sanitize
%             \@wrxref{|bold}}
\def\impl{\@bsphack\begingroup
          \def\protect##1{\string##1\space}\@sanitize
          \@wrxref{|definition}}
\newcommand{\indexcode}[1]{{\lstinline$#1$}}
\def\use{\@bsphack\begingroup
         \def\protect##1{\string##1\space}\@sanitize
         \@wrxref{|hyperpage}}
\def\@wrxref#1#2{\let\thepage\relax
    \xdef\@gtempa{\write\@indexfile{\string
    \indexentry{#2@{\lstinline$#2$}#1}{\thepage}}}\endgroup\@gtempa
    \if@nobreak \ifvmode\nobreak\fi\fi\@esphack}
%\newcommand{\use}[1]{\index{#1@{\lstinline$#1$}}}
%\newcommand{\impl}[1]{\index{\protect#1@{\lstinline$\protect#1$}|definition}}

% inline text and lowercase index: \Index{inline and lowercase index text}
\newcommand{\Index}{\@ifstar\@sIndex\@Index}
% inline text and as-in index: \Index[as-is index text]{inline text}
\newcommand{\@Index}[2][\@empty]{\lowercase{\def\temp{#2}}#2\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi}
% inline text but index with different as-is text: \Index[index text]{inline text}
\newcommand{\@sIndex}[2][\@empty]{#2\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi}

% inline text and code index (cannot use �)
\newcommand{\Indexc}[1]{\lstinline$#1$\index{#1@\lstinline$#1$}}
% code index (cannot use �)
\newcommand{\indexc}[1]{\index{#1@\lstinline$#1$}}

% Denote newterms in particular font and index them without particular font and in lowercase, e.g., \newterm{abc}.
% The option parameter provides an index term different from the new term, e.g., \newterm[\texttt{abc}]{abc}
% The star version does not lowercase the index information, e.g., \newterm*{IBM}.
\newcommand{\newtermFontInline}{\emph}
\newcommand{\newterm}{\@ifstar\@snewterm\@newterm}
\newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi}
\newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi}

% Latin abbreviation
\newcommand{\abbrevFont}{\textit}	% set empty for no italics
\newcommand*{\eg}{%
	\@ifnextchar{,}{\abbrevFont{e}.\abbrevFont{g}.}%
		{\@ifnextchar{:}{\abbrevFont{e}.\abbrevFont{g}.}%
			{\abbrevFont{e}.\abbrevFont{g}.,\xspace}}%
}%
\newcommand*{\ie}{%
	\@ifnextchar{,}{\abbrevFont{i}.\abbrevFont{e}.}%
		{\@ifnextchar{:}{\abbrevFont{i}.\abbrevFont{e}.}%
			{\abbrevFont{i}.\abbrevFont{e}.,\xspace}}%
}%
\newcommand*{\etc}{%
	\@ifnextchar{.}{\abbrevFont{etc}}%
        {\abbrevFont{etc}.\xspace}%
}%
\newcommand{\etal}{%
	\@ifnextchar{.}{\abbrevFont{et~al}}%
	        {\abbrevFont{et al}.\xspace}%
}%
\makeatother

\newenvironment{quote2}{%
	\list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=4pt\parsep=0pt\leftmargin=\parindent\rightmargin\leftmargin}%
	\item\relax
}{%
	\endlist
}% quote2

\newenvironment{rationale}{%
  \begin{quote2}\noindent$\Box$\enspace
}{%
  \hfill\enspace$\Box$\end{quote2}
}%

% blocks and titles
\newcommand{\define}[1]{\emph{#1\/}\index{#1}}
\newcommand{\rewriterules}{\paragraph{Rewrite Rules}~\par\noindent}
\newcommand{\examples}{\paragraph{Examples}~\par\noindent}
\newcommand{\semantics}{\paragraph{Semantics}~\par\noindent}
\newcommand{\constraints}{\paragraph{Constraints}~\par\noindent}
\newcommand{\predefined}{\paragraph{Predefined Identifiers}~\par\noindent}

% BNF macros
\newenvironment{syntax}{
\paragraph{Syntax}
\begin{quote2}
\begin{description}[noitemsep,leftmargin=\parindentlnth]
}{
\end{description}
\end{quote2}
}
% \def\syntax{\paragraph{Syntax}\trivlist\parindent=.5in\item[\hskip.5in]}
%\let\endsyntax=\endtrivlist
\newcommand{\lhs}[1]{\item[\emph{#1:}\index{#1@{\emph{#1}}|italic}]~\ignorespaces}
\newcommand{\oldlhs}[1]{\item[\emph{#1:}\index{#1@{\emph{#1}}|italic}~\dots]~}
\newcommand{\rhs}{\hfil\newline}
\newcommand{\nonterm}[1]{\emph{#1}\index{#1@{\emph{#1}}|italic}}
\newcommand{\opt}{$_{opt}$\ }

\usepackage{varioref}                 % extended references
% adjust varioref package with default "section" and "page" titles, and optional title with faraway page numbers
% \VRef{label} => Section 2.7, \VPageref{label} => page 17
% \VRef[Figure]{label} => Figure 3.4, \VPageref{label} => page 17
\renewcommand{\reftextfaceafter}{\unskip}
\renewcommand{\reftextfacebefore}{\unskip}
\renewcommand{\reftextafter}{\unskip}
\renewcommand{\reftextbefore}{\unskip}
\renewcommand{\reftextfaraway}[1]{\unskip, p.~\pageref{#1}}
\renewcommand{\reftextpagerange}[2]{\unskip, pp.~\pageref{#1}--\pageref{#2}}
\newcommand{\VRef}[2][Section]{\ifx#1\@empty\else{#1}\nobreakspace\fi\vref{#2}}
\newcommand{\VPageref}[2][page]{\ifx#1\@empty\else{#1}\nobreakspace\fi\pageref{#2}}

\let\Oldthebibliography\thebibliography
\renewcommand\thebibliography[1]{
  \Oldthebibliography{#1}
  \setlength{\parskip}{0pt}				% reduce vertical spacing between references
  \setlength{\itemsep}{5pt plus 0.3ex}
}%

% Go programming language: https://github.com/julienc91/listings-golang/blob/master/listings-golang.sty
\lstdefinelanguage{Golang}{
	morekeywords=[1]{package,import,func,type,struct,return,defer,panic,recover,select,var,const,iota,},%
	morekeywords=[2]{string,uint,uint8,uint16,uint32,uint64,int,int8,int16,int32,int64,
		bool,float32,float64,complex64,complex128,byte,rune,uintptr, error,interface},%
	morekeywords=[3]{map,slice,make,new,nil,len,cap,copy,close,true,false,delete,append,real,imag,complex,chan,},%
	morekeywords=[4]{for,break,continue,range,goto,switch,case,fallthrough,if,else,default,},%
	morekeywords=[5]{Println,Printf,Error,},%
	sensitive=true,%
	morecomment=[l]{//},%
	morecomment=[s]{/*}{*/},%
	morestring=[b]',%
	morestring=[b]",%
	morestring=[s]{`}{`},%
}

% CFA programming language, based on ANSI C (with some gcc additions)
\lstdefinelanguage{Pseudo}{
	morekeywords={string,uint,int,bool,float},%
	sensitive=true,%
	morecomment=[l]{//},%
	morecomment=[s]{/*}{*/},%
	morestring=[b]',%
	morestring=[b]",%
	morestring=[s]{`}{`},%
}%

% CFA programming language, based on ANSI C (with some gcc additions)
\lstdefinelanguage{CFA}[ANSI]{C}{
	morekeywords={_Alignas,_Alignof,__alignof,__alignof__,asm,__asm,__asm__,_At,_Atomic,__attribute,__attribute__,auto,
		_Bool,catch,catchResume,choose,_Complex,__complex,__complex__,__const,__const__,disable,dtype,enable,__extension__,
		fallthrough,fallthru,finally,forall,ftype,_Generic,_Imaginary,inline,__label__,lvalue,_Noreturn,one_t,otype,restrict,_Static_assert,
		_Thread_local,throw,throwResume,trait,try,ttype,typeof,__typeof,__typeof__,zero_t},
}%

\newcommand{\CFADefaults}{%
\lstset{
language=CFA,
columns=fullflexible,
basicstyle=\linespread{0.9}\sf,							% reduce line spacing and use sanserif font
stringstyle=\tt,										% use typewriter font
tabsize=4,												% 4 space tabbing
xleftmargin=\parindentlnth,								% indent code to paragraph indentation
extendedchars=true,										% allow ASCII characters in the range 128-255
escapechar=�,											% LaTeX escape in CFA code �...� (section symbol), emacs: C-q M-'
mathescape=true,										% LaTeX math escape in CFA code $...$
keepspaces=true,										%
showstringspaces=false,									% do not show spaces with cup
showlines=true,											% show blank lines at end of code
aboveskip=4pt,											% spacing above/below code block
belowskip=3pt,
% replace/adjust listing characters that look bad in sanserif
literate={-}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.8ex}{0.075ex}}}}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,
moredelim=**[is][\color{red}]{�}{�},					% red highlighting �...� (registered trademark symbol) emacs: C-q M-.
moredelim=**[is][\color{blue}]{�}{�},					% blue highlighting �...� (sharp s symbol) emacs: C-q M-_
moredelim=**[is][\color{OliveGreen}]{�}{�}, 			% green highlighting �...� (cent symbol) emacs: C-q M-"
moredelim=[is][\lstset{keywords={}}]{�}{�}, 			% keyword escape �...� (pilcrow symbol) emacs: C-q M-^
}% lstset
}% CFADefaults
\newcommand{\CFAStyle}{%
\CFADefaults
% inline code �...� (copyright symbol) emacs: C-q M-)
\lstMakeShortInline�									% single-character for \lstinline
}% CFAStyle

\lstnewenvironment{cfa}[1][]
{\CFADefaults\lstset{#1}}
{}


% Local Variables: %
% tab-width: 4 %
% fill-column: 100 %
% End: %
