%% Listings environments for Archive of Numerical Software.
%%
%% The following listings environments are provided:
%%
%% c++        for C++ code
%% python     for Python code
%% cython     for Cython 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
%% latexcode  for LaTeX code
%% fortran    for FORTRAN code
%%
%% This style file is based on original work by Olivier Verdier
%% <olivier.verdier@math.ntnu.no> (pythonhighlight.sty),
%% with contributions from Johan Hake <johan.hake@gmail.com>.
%%
%% Modified for ANS by Anders Logg <logg@simula.no>, 2011.
%%
%% Modified by Hans Petter Langtangen <hpl@simula.no>, 2011, to
%% have pairs c++ (with C++ title) and c++:nt (no title),
%% python and python:nt, etc. (It is not always desirable to
%% have a title with the language.) Added Cython and Fortran style.
%%
%% First added:   2011-05-16
%% Last modified: 2011-06-03

\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]{\hspace{9cm}\small\textit{#1}\hspace{0.1cm}}
%\newcommand{\codetitlestyle}[1]{\small\textit{#1}\hspace{0.1cm}}
\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}

% Define the common style to be shared by all languages
\lstdefinestyle{c@mmon}{%
showtabs=true,                       % show tabs in strings using underscore
tab=,
tabsize=2,                           % tab means 2 spaces
basicstyle=\ttfamily\footnotesize,   % fonts used for the code
breaklines=true,                     % break lines
breakatwhitespace=true,              % let linebreaks happen at whitespace
showspaces=false,                    % true: show spaces with a particular underscore
aboveskip=1ex,
frame=trbl,                          % top+right+bottom+left (TB draws double lines at top + bottom)
%framerule=0.4pt                     % thickness of frame
rulecolor=\color{black},             % frame color
backgroundcolor=\color{yellow!10},
xleftmargin=5pt,
xrightmargin=5pt,
%numbers=left,                       % put line numbers on the left
%stepnumber=2,                       % stepnumber=1 numbers each line, =n every n lines
}
\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}}
\lstnewenvironment{latexcode:nt}{\lstset{language=[LaTeX]TeX,style=c@mmon}}{}

%--- 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}}{}
\lstnewenvironment{c++:nt}[1][]{\lstset{style=cppstyle}}{}
\newcommand{\cppinline}{\lstinline[style=cppstyle,basicstyle=\ttfamily]}

%--- Python and Cython ---

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


 \lstdefinestyle{cythonstyle}{
 language=cython,
 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,cdef,cpdef,yield,del,elif ,else,%
 except,exec,finally,for,from,global,if,import,in,%
 lambda,not,or,pass,print,raise,return,try,while,assert,cdef,cpdef},
 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,float,int,ndarray,complex,double},
 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}}{}
%\lstnewenvironment{python}[1][]{\lstset{style=pythonstyle,title={\codetitlestyle{Python code }},belowcaptionskip=\belowtitleskip}}{}
\lstnewenvironment{python:nt}[1][]{\lstset{style=pythonstyle}}{}
\newcommand{\pythoninline}{\lstinline[style=pythonstyle,basicstyle=\ttfamily]}


\newcommand{\inputcython}[1]{\lstinputlisting[style=cythonstyle,title={\codetitlestyle{Cython code }},belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{cython}[1][]{\lstset{style=cythonstyle,title={\codetitlestyle{Cython code }},belowcaptionskip=\belowtitleskip}}{}
\lstnewenvironment{cython:nt}[1][]{\lstset{style=cythonstyle}}{}
\newcommand{\cythoninline}{\lstinline[style=cythonstyle,basicstyle=\ttfamily]}

%--- FORTRAN ---

% Note: this is basically FORTRAN 77, not 90, 95, 2003/2008.
\lstdefinestyle{fortranstyle}{
language=fortran,
style=c@mmon,
stringstyle=\color{stringcolour},
showstringspaces=false,
alsoletter={1234567890},
keywordstyle=\color{keywordcolour}\bfseries,
emph={auto,do,while,if,else,end,endif,enddo,%
subroutine,function,format,program},
emphstyle=\color{blue}\bfseries,
emphstyle=[2]\color{keywordcolour},
emph={[3]double,real,precision,integer,logical},
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}%
,%
commentstyle=\color{commentcolour}\slshape,
morecomment=[l][commentstyle]{C},  % need ! as well for F90
morestring=[b][stringstyle]",
morestring=[d][stringstyle]'
}

\newcommand{\inputfortran}[1]{\lstinputlisting[style=fortranstyle, title={\codetitlestyle{FORTRAN code}}, belowcaptionskip=\belowtitleskip]{#1}}
\lstnewenvironment{fortran}[1][]{\lstset{style=fortranstyle, title={\codetitlestyle{FORTRAN code}}, belowcaptionskip=\belowtitleskip}}{}
\lstnewenvironment{fortran:nt}[1][]{\lstset{style=fortranstyle}}{}
\newcommand{\fortraninline}{\lstinline[style=fortranstyle,basicstyle=\ttfamily]}


%--- 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}}{}
\lstnewenvironment{xml:nt}[1][]{\lstset{style=xmlstyle}}{}

%--- 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{bash:nt}[1][]{\lstset{style=bashstyle}}{}
\lstnewenvironment{csh}[1][]{\lstset{style=bashstyle, title={\codetitlestyle{Csh code}}, belowcaptionskip=\belowtitleskip}}{}
\lstnewenvironment{csh:nt}[1][]{\lstset{style=bashstyle}}{}

%--- 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}}{}
\lstnewenvironment{swigcode:nt}[1][]{\lstset{style=cppstyle}}{}

%--- 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}}{}
\lstnewenvironment{uflcode:nt}[1][]{\lstset{style=pythonstyle}}{}

%--- MATLAB ---

% Note: problem with too large width of frame
\lstnewenvironment{matlab}[1][]{
\lstset{
language=matlab,
basicstyle=\ttfamily\small,
backgroundcolor=\color{yellow!10}
frame=trbl,
rulecolor=\color{black!40},
emphstyle=\color{blue},
commentstyle=\color{commentcolour}\slshape,
keywordstyle=\color{keywordcolour}\bfseries,
}}{}

%--- 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}}{}
\lstnewenvironment{anycode:nt}[1][]{\lstset{style=anycodestyle}}{}

%--- Output from running programs ---

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

%--- 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}}{}
\lstnewenvironment{gencode:nt}[1][]{\lstset{style=gencodestyle}}{}
