%% Listings environments for Archive of Numerical Software.
%%
%% The following listings environments are provided:
%%
%% c++        for C++ code
%% python     for Python code
%% xml        for XML code
%% bash       for BASH code
%% swigcode   for SWIG code
%% uflcode    for UFL code
%% matlab     for MATLAB code
%% anycode    for generic code
%% progoutput for program output
%% gencode    for generated code
%%
%% This style file is based on original work by Olivier Verdier,
%% with contributions from Johan Hake.
%%
%% Modified for ANS by Anders Logg, 2011.
%%
%% First added:   2011-05-16
%% Last modified: 2013-10-11

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{anslistings}[2009/03/28 code highlighting; provided by Olivier Verdier <olivier@maths.lth.se>]

% Load required packages
\RequirePackage{listings}
\RequirePackage{xcolor}

% Basic setup
\renewcommand{\lstlistlistingname}{Code Listings}
\renewcommand{\lstlistingname}{Code Listing}
\newcommand{\codetitlestyle}[1]{\small\textit{#1}}
\newcommand{\belowtitleskip}{2pt}
\newcommand{\captionposition}{t}
\newcommand{\framemargin}{0.5ex}
\newcommand{\literatecolour}{\textcolor{literatecolour}}

% Colors
\definecolor{gray}{gray}{0.5}
\colorlet{commentcolour}{green!50!black}
\colorlet{stringcolour}{red!60!black}
\colorlet{keywordcolour}{magenta!90!black}
\colorlet{exceptioncolour}{yellow!50!red}
\colorlet{commandcolour}{blue!60!black}
\colorlet{numpycolour}{blue!60!green}
\colorlet{literatecolour}{magenta!90!black}
\colorlet{promptcolour}{green!50!black}
\colorlet{specmethodcolour}{violet}
\colorlet{indendifiercolour}{green!70!white}

% Common style
\lstdefinestyle{c@mmon}{%
showtabs=true,
tab=,
tabsize=2,
basicstyle=\ttfamily\footnotesize,
breaklines=true,
breakatwhitespace=true,
aboveskip=1ex,
frame=trbl,
rulecolor=\color{black},
backgroundcolor=\color{yellow!10},
xleftmargin=5pt,
xrightmargin=5pt,
numbers=left
}
\lstset{style=c@mmon}

%--- Typesetting LaTeX ---
\lstnewenvironment{latexcode}{\lstset{language=[LaTeX]TeX,style=c@mmon}}{}
\newcommand{\inputlatex}[1]{\lstset{language=[LaTeX]TeX,style=c@mmon}%
  \lstinputlisting{#1}}

%--- C++ ---

\lstdefinestyle{cppstyle}{
language=c++,
style=c@mmon,
stringstyle=\color{stringcolour},
showstringspaces=false,
alsoletter={1234567890},
keywordstyle=\color{keywordcolour}\bfseries,
emph={auto,const,struct,%
break,continue,else,for,switch,void,%
case,default,enum,goto,register,sizeof,typedef,%
char,do,extern,if,return,static,union,while,%
asm,dynamic_cast,namespace,reinterpret_cast,try,%
bool,explicit,new,static_cast,typeid,volatile,%
catch,operator,template,typename,%
class,friend,private,this,using,%
const_cast,inline,public,throw,virtual,%
delete,mutable,protected,wchar_t,%
or,and,xor,not,assert},
emphstyle=\color{blue}\bfseries,
emph={[2]true, false, NULL},
emphstyle=[2]\color{keywordcolour},
emph={[3]double,float,int,short,unsigned,long,signed},
emphstyle=[3]\color{blue},
emph={[4]1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
emph={[4]cos,sin,tan,acos,asin,atan,atan2,cosh,sinh,tanh,%
exp,frexp,ldexp,log,log10,modf,pow,sqrt,ceil,fabs,floor,fmod},
emphstyle=[4]\color{numpycolour},
literate=*%
{:}{{\literatecolour:}}{1}%
{=}{{\literatecolour=}}{1}%
{-}{{\literatecolour-}}{1}%
{+}{{\literatecolour+}}{1}%
{*}{{\literatecolour*}}{1}%
{!}{{\literatecolour!}}{1}%
{[}{{\literatecolour[}}{1}%
{]}{{\literatecolour]}}{1}%
{<}{{\literatecolour<}}{1}%
{>}{{\literatecolour>}}{1}%
{>>>}{{\textcolor{promptcolour}{>>>}}}{1}%
,%
commentstyle=\color{commentcolour}\slshape,
morecomment=[l][commentstyle]{//},
morecomment=[s][commentstyle]{/*}{*/},
morestring=[b][stringstyle]",
morestring=[d][stringstyle]'
}

\newcommand{\inputcpp}[1]{\lstinputlisting[style=cppstyle, title={\codetitlestyle{C++ code}}, belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{c++}[1][]{\lstset{style=cppstyle, title={\codetitlestyle{C++ code}}, belowcaptionskip=\belowtitleskip}}{}
\newcommand{\cpp}{\lstinline[style=cppstyle,basicstyle=\ttfamily]}

%--- Typesetting Python ---

 \lstdefinestyle{pythonstyle}{
 language=python,
 showtabs=true,
 tab=,
 tabsize=2,
 basicstyle=\ttfamily\footnotesize,
 stringstyle=\color{stringcolour},
 showstringspaces=false,
 alsoletter={1234567890},
 otherkeywords={\ , \}, \{, \%, \&, \|},
 keywordstyle=\color{keywordcolour}\bfseries,
 emph={and,break,class,continue,def,yield,del,elif ,else,%
 except,exec,finally,for,from,global,if,import,in,%
 lambda,not,or,pass,print,raise,return,try,while,assert},
 emphstyle=\color{blue}\bfseries,
 emph={[2]True, False, None},
 emphstyle=[2]\color{keywordcolour},
 emph={[3]object,type,isinstance,copy,deepcopy,zip,enumerate,reversed,list,len,dict,tuple,xrange,append,execfile,real,imag,reduce,str,repr},
 emphstyle=[3]\color{commandcolour},
 emph={Exception,NameError,IndexError,SyntaxError,TypeError,ValueError,OverflowError,ZeroDivisionError},
 emphstyle=\color{exceptioncolour}\bfseries,
 commentstyle=\color{commentcolour}\slshape,
 emph={[4]ode, fsolve, sqrt, exp, sin, cos, arccos, pi,  array, norm, solve, dot, arange, , isscalar, max, sum, flatten, shape, reshape, find, any, all, abs, plot, linspace, legend, quad, polyval,polyfit, hstack, concatenate,vstack,column_stack,empty,zeros,ones,rand,vander,grid,pcolor,eig,eigs,eigvals,svd,qr,tan,det,logspace,roll,min,mean,cumsum,cumprod,diff,vectorize,lstsq,cla,eye,xlabel,ylabel,squeeze},
 emphstyle=[4]\color{numpycolour},
 emph={[5]__init__,__add__,__mul__,__div__,__sub__,__call__,__getitem__,__setitem__,__eq__,__ne__,__nonzero__,__rmul__,__radd__,__repr__,__str__,__get__,__truediv__,__pow__,__name__,__future__,__all__},
 emphstyle=[5]\color{specmethodcolour},
 emph={[6]assert,range,yield},
 emphstyle=[6]\color{keywordcolour}\bfseries,
 literate=*%
 {:}{{\literatecolour:}}{1}%
 {=}{{\literatecolour=}}{1}%
 {-}{{\literatecolour-}}{1}%
 {+}{{\literatecolour+}}{1}%
 {*}{{\literatecolour*}}{1}%
 {/}{{\literatecolour/}}{1}%
 {!}{{\literatecolour!}}{1}%
 {[}{{\literatecolour[}}{1}%
 {]}{{\literatecolour]}}{1}%
 {<}{{\literatecolour<}}{1}%
 {>}{{\literatecolour>}}{1}%
 {>>>}{{\textcolor{promptcolour}{>>>}}}{1}%
 ,%
 breaklines=true,
 breakatwhitespace= true,
 aboveskip=1ex,
 frame=trbl,
 framesep=.3ex,
 rulecolor=\color{black!40},
 backgroundcolor=\color{yellow!10}
}

\newcommand{\inputpython}[1]{\lstinputlisting[style=pythonstyle,title={\codetitlestyle{Python code }},belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{python}[1][]{\lstset{style=pythonstyle,title={\codetitlestyle{Python code }},belowcaptionskip=\belowtitleskip}}{}
\newcommand{\pyth}{\lstinline[style=pythonstyle,basicstyle=\ttfamily]}

%--- Typesetting XML ---

\lstdefinestyle{xmlstyle}{
language=xml,
showtabs=true,
tab=,
tabsize=2,
basicstyle=\ttfamily\footnotesize,
stringstyle=\color{stringcolour},
showstringspaces=false,
alsoletter={1234567890},
emphstyle=\color{exceptioncolour}\bfseries,
commentstyle=\color{commentcolour}\slshape,
breaklines=true,
breakatwhitespace= true,
aboveskip=1ex,
frame=trbl,
framesep=.3ex,
rulecolor=\color{black!40},
backgroundcolor=\color{yellow!10}
}

\newcommand{\inputxml}[1]{\lstinputlisting[style=xmlstyle, title={\codetitlestyle{XML code}}, belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{xml}[1][]{\lstset{style=xmlstyle, title={\codetitlestyle{XML code}}, belowcaptionskip=\belowtitleskip}}{}

%--- Typesetting Bash ---

\lstdefinestyle{bashstyle}{
language=bash,
showtabs=true,
tab=,
tabsize=2,
basicstyle=\ttfamily\footnotesize,
stringstyle=\color{stringcolour},
showstringspaces=false,
alsoletter={1234567890},
otherkeywords={\ , \}, \{, \%, \&, \|},
emphstyle=\color{exceptioncolour}\bfseries,
commentstyle=\color{commentcolour}\slshape,
breaklines=true,
breakatwhitespace= true,
aboveskip=1ex,
frame=trbl,
framesep=.3ex,
rulecolor=\color{black!40},
backgroundcolor=\color{yellow!10}
}

\newcommand{\inputbash}[1]{\lstinputlisting[style=bashstyle, title={\codetitlestyle{Bash code}}, belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{bash}[1][]{\lstset{style=bashstyle, title={\codetitlestyle{Bash code}}, belowcaptionskip=\belowtitleskip}}{}
\lstnewenvironment{csh}[1][]{\lstset{style=bashstyle, title={\codetitlestyle{Csh code}}, belowcaptionskip=\belowtitleskip}}{}

%--- SWIG ---

\newcommand{\inputswig}[1]{\lstinputlisting[style=cppstyle, title={\codetitlestyle{SWIG code}}, belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{swigcode}[1][]{\lstset{style=cppstyle, title={\codetitlestyle{SWIG code}}, belowcaptionskip=\belowtitleskip}}{}

%--- UFL ---

\newcommand{\inputufl}[1]{\lstinputlisting[style=pythonstyle, title={\codetitlestyle{UFL code}}, belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{uflcode}[1][]{\lstset{style=pythonstyle, title={\codetitlestyle{UFL code}}, belowcaptionskip=\belowtitleskip}}{}

%--- MATLAB ---

\lstdefinestyle{matlabstyle}{
language=matlab,
basicstyle=\ttfamily\small,
backgroundcolor=\color{blue!10},
frame=trbl,
rulecolor=\color{black!40},
emphstyle=\color{blue},
commentstyle=\color{commentcolour}\slshape,
keywordstyle=\color{keywordcolour}\bfseries,
}

\lstnewenvironment{matlab}[1][]{\lstset{style=matlabstyle,title={\codetitlestyle{MATLAB code }},belowcaptionskip=\belowtitleskip}}{}

\newcommand{\inputmatlab}[1]{\lstinputlisting[style=matlabstyle,title={\codetitlestyle{MATLAB code }},belowcaptionskip=\belowtitleskip]{#1}}

%--- Other ---

\lstdefinestyle{anycodestyle}{
language=bash,
showtabs=true,
tab=,
tabsize=2,
basicstyle=\ttfamily\footnotesize,
stringstyle=\color{stringcolour},
showstringspaces=false,
alsoletter={1234567890},
emphstyle=\color{exceptioncolour}\bfseries,
commentstyle=\color{commentcolour}\slshape,
breaklines=true,
breakatwhitespace= true,
aboveskip=1ex,
frame=trbl,
framesep=.3ex,
rulecolor=\color{black!40},
backgroundcolor=\color{yellow!10}
}

\newcommand{\inputanycode}[1]{\lstinputlisting[style=anycodestyle, title={\codetitlestyle{Code}}, belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{anycode}[1][]{\lstset{style=anycodestyle, title={\codetitlestyle{Code}}, belowcaptionskip=\belowtitleskip}}{}

%--- Output from running programs ---

\lstnewenvironment{progoutput}[1][]{\lstset{style=gencodestyle, title={\codetitlestyle{Output}}, belowcaptionskip=\belowtitleskip}}{}

%--- Generated code ---

\lstdefinestyle{gencodestyle}{
language=bash,
showtabs=true,
tab=,
tabsize=2,
basicstyle=\ttfamily\footnotesize,
stringstyle=\color{stringcolour},
showstringspaces=false,
alsoletter={1234567890},
emphstyle=\color{exceptioncolour}\bfseries,
commentstyle=\color{commentcolour}\slshape,
breaklines=true,
breakatwhitespace= true,
aboveskip=1ex,
frame=trbl,
framesep=.3ex,
rulecolor=\color{black!40},
backgroundcolor=\color{yellow!10}
}

\newcommand{\inputgencode}[1]{\lstinputlisting[style=cppstyle, title={\codetitlestyle{Generated code}}, belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{gencode}[1][]{\lstset{style=gencodestyle, title={\codetitlestyle{Generated code}}, belowcaptionskip=\belowtitleskip}}{}
