%% Copyright (C) 2018 The University of Sheffield
%%               2018-2021 The University of Paris-Saclay
%%               2019-2021 The University of Exeter
%%
%% License:
%%   This program can be redistributed and/or modified under the terms
%%   of the LaTeX Project Public License Distributed from CTAN
%%   archives in directory macros/latex/base/lppl.txt; either
%%   version 1.3c of the License, or (at your option) any later version.
%%   OR
%%   The 2-clause BSD-style license.
%%
%%   SPDX-License-Identifier: LPPL-1.3c+ OR BSD-2-Clause
\usepackage{listings}
\usepackage{listingsutf8}
\usepackage{tikz}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{skins}
\usepackage{xstring}

\definecolor{OliveGreen}    {cmyk}{0.64,0,0.95,0.40}
\definecolor{BrickRed}      {cmyk}{0,0.89,0.94,0.28}
\definecolor{Blue}          {cmyk}{1,1,0,0}
\definecolor{CornflowerBlue}{cmyk}{0.65,0.13,0,0}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% <antiquotations>
%% Hack: re-defining tag types for supporting highlighting of antiquotations
\gdef\lst@tagtypes{s}
\gdef\lst@TagKey#1#2{%
    \lst@Delim\lst@tagstyle #2\relax
        {Tag}\lst@tagtypes #1%
                     {\lst@BeginTag\lst@EndTag}%
        \@@end\@empty{}}
\lst@Key{tag}\relax{\lst@TagKey\@empty{#1}}
\lst@Key{tagstyle}{}{\def\lst@tagstyle{#1}}
\lst@AddToHook{EmptyStyle}{\let\lst@tagstyle\@empty}
\gdef\lst@BeginTag{%
    \lst@DelimOpen
        \lst@ifextags\else
        {\let\lst@ifkeywords\iftrue
         \lst@ifmarkfirstintag \lst@firstintagtrue \fi}}
\lst@AddToHookExe{ExcludeDelims}{\let\lst@ifextags\iffalse}
\gdef\lst@EndTag{\lst@DelimClose\lst@ifextags\else}
\lst@Key{usekeywordsintag}t[t]{\lstKV@SetIf{#1}\lst@ifusekeysintag}
\lst@Key{markfirstintag}f[t]{\lstKV@SetIf{#1}\lst@ifmarkfirstintag}
\gdef\lst@firstintagtrue{\global\let\lst@iffirstintag\iftrue}
\global\let\lst@iffirstintag\iffalse
 \lst@AddToHook{PostOutput}{\lst@tagresetfirst}
 \lst@AddToHook{Output}
     {\gdef\lst@tagresetfirst{\global\let\lst@iffirstintag\iffalse}}
 \lst@AddToHook{OutputOther}{\gdef\lst@tagresetfirst{}}
\lst@AddToHook{Output}
    {\ifnum\lst@mode=\lst@tagmode
         \lst@iffirstintag \let\lst@thestyle\lst@gkeywords@sty \fi
         \lst@ifusekeysintag\else \let\lst@thestyle\lst@gkeywords@sty\fi
     \fi}
\lst@NewMode\lst@tagmode
\gdef\lst@Tag@s#1#2\@empty#3#4#5{%
    \lst@CArg #1\relax\lst@DefDelimB {}{}%
        {\ifnum\lst@mode=\lst@tagmode \expandafter\@gobblethree \fi}%
        #3\lst@tagmode{#5}%
    \lst@CArg #2\relax\lst@DefDelimE {}{}{}#4\lst@tagmode}%
\gdef\lst@BeginCDATA#1\@empty{%
    \lst@TrackNewLines \lst@PrintToken
    \lst@EnterMode\lst@GPmode{}\let\lst@ifmode\iffalse
    \lst@mode\lst@tagmode #1\lst@mode\lst@GPmode\relax\lst@modetrue}
%
\def\beginlstdelim#1#2#3%
{%
    \def\endlstdelim{\texttt{\textbf{\color{black!60}#2}}\egroup}%
    \ttfamily\textbf{\color{black!60}#1}\bgroup\rmfamily\color{#3}\aftergroup\endlstdelim%
}
%% </antiquotations>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% <isar>
\providecolor{isar}{named}{blue}
\renewcommand{\isacommand}[1]{\textcolor{OliveGreen!60}{\ttfamily\bfseries #1}}
\newcommand{\inlineisarbox}[1]{#1}
\NewTColorBox[]{isarbox}{}{
     ,boxrule=0pt
     ,boxsep=0pt
     ,colback=white!90!isar
     ,enhanced jigsaw
     ,borderline west={2pt}{0pt}{isar!60!black}
     ,sharp corners
     %,before skip balanced=0.5\baselineskip plus 2pt % works only with Tex Live 2020 and later
     ,enlarge top by=0mm
     ,enhanced
     ,overlay={\node[draw,fill=isar!60!black,xshift=0pt,anchor=north
       east,font=\bfseries\footnotesize\color{white}]
                at (frame.north east) {Isar};}
}
%% </isar>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% <out>
\providecolor{out}{named}{green}
\newtcblisting{out}[1][]{%
      listing only%
     ,boxrule=0pt
     ,boxsep=0pt
     ,colback=white!90!out
     ,enhanced jigsaw
     ,borderline west={2pt}{0pt}{out!60!black}
     ,sharp corners
     % ,before skip=10pt
     % ,after skip=10pt
     ,enlarge top by=0mm
     ,enhanced
     ,overlay={\node[draw,fill=out!60!black,xshift=0pt,anchor=north
       east,font=\bfseries\footnotesize\color{white}]
                at (frame.north east) {Document};}
        ,listing options={
           breakatwhitespace=true
          ,columns=flexible%
          ,basicstyle=\small\rmfamily
          ,mathescape
          ,#1
      }
  }%
%% </out>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% <sml>
\lstloadlanguages{ML}
\providecolor{sml}{named}{red}
\lstdefinestyle{sml}{
   ,escapechar=ë%
   ,basicstyle=\ttfamily%
   ,commentstyle=\itshape%
   ,keywordstyle=\bfseries\color{CornflowerBlue}%
   ,ndkeywordstyle=\color{green}%
   ,language=ML
%  ,literate={%
%     {<@>}{@}1%
%    }
   ,keywordstyle=[6]{\itshape}%
   ,morekeywords=[6]{args_type}%
   ,tag=**[s]{@\{}{\}}%
   ,tagstyle=\color{CornflowerBlue}%
   ,markfirstintag=true%
}%
\def\inlinesml{\lstinline[style=sml,breaklines=true,breakatwhitespace=true]}
\newtcblisting{sml}[1][]{%
      listing only%
     ,boxrule=0pt
     ,boxsep=0pt
     ,colback=white!90!sml
     ,enhanced jigsaw
     ,borderline west={2pt}{0pt}{sml!60!black}
     ,sharp corners
     % ,before skip=10pt
     % ,after skip=10pt
     ,enlarge top by=0mm
     ,enhanced
     ,overlay={\node[draw,fill=sml!60!black,xshift=0pt,anchor=north
       east,font=\bfseries\footnotesize\color{white}]
                at (frame.north east) {SML};}
        ,listing options={
          style=sml
          ,columns=flexible%
          ,basicstyle=\small\ttfamily
          ,#1
      }
  }%
%% </sml>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% <latex>
\lstloadlanguages{TeX}
\providecolor{ltx}{named}{yellow}
\lstdefinestyle{lltx}{language=[AlLaTeX]TeX,
  ,basicstyle=\ttfamily%
  ,showspaces=false%
  ,escapechar=ë
  ,showlines=false%
  ,morekeywords={newisadof}
 %  ,keywordstyle=\bfseries%
   % Defining 2-keywords
   ,keywordstyle=[1]{\color{BrickRed!60}\bfseries}%
   % Defining 3-keywords
   ,keywordstyle=[2]{\color{OliveGreen!60}\bfseries}%
   % Defining 4-keywords
   ,keywordstyle=[3]{\color{black!60}\bfseries}%
   % Defining 5-keywords
   ,keywordstyle=[4]{\color{Blue!70}\bfseries}%
   % Defining 6-keywords
   ,keywordstyle=[5]{\itshape}%
  % 
}
\lstdefinestyle{ltx}{style=lltx,
                basicstyle=\ttfamily\small}%
\def\inlineltx{\lstinline[style=ltx, breaklines=true,columns=fullflexible]}
% see
% https://tex.stackexchange.com/questions/247643/problem-with-tcblisting-first-listed-latex-command-is-missing
\NewTCBListing{ltx}{ !O{} }{%
     listing only%
     ,boxrule=0pt
     ,boxsep=0pt
     ,colback=white!90!ltx
     ,enhanced jigsaw
     ,borderline west={2pt}{0pt}{ltx!60!black}
     ,sharp corners
     % ,before skip=10pt
     % ,after skip=10pt
     ,enlarge top by=0mm
     ,enhanced
     ,overlay={\node[draw,fill=ltx!60!black,xshift=0pt,anchor=north
       east,font=\bfseries\footnotesize\color{white}]
                at (frame.north east) {\LaTeX};}
      ,listing options={
             style=lltx,
            ,columns=flexible%
            ,basicstyle=\small\ttfamily
            ,#1
       }
  }%
%% </latex>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% <bash>
\providecolor{bash}{named}{black}
\lstloadlanguages{bash}
\lstdefinestyle{bash}{%
   language=bash
  ,escapechar=ë
  ,basicstyle=\ttfamily%
  ,showspaces=false%
  ,showlines=false%
  ,columns=flexible%
 %  ,keywordstyle=\bfseries%
   % Defining 2-keywords
   ,keywordstyle=[1]{\color{BrickRed!60}\bfseries}%
   % Defining 3-keywords
   ,keywordstyle=[2]{\color{OliveGreen!60}\bfseries}%
   % Defining 4-keywords
   ,keywordstyle=[3]{\color{black!60}\bfseries}%
   % Defining 5-keywords
   ,keywordstyle=[4]{\color{Blue!80}\bfseries}%
   ,alsoletter={*,-,:,~,/}
   ,morekeywords=[4]{}%
   % Defining 6-keywords
   ,keywordstyle=[5]{\itshape}%
  % 
}
\def\inlinebash{\lstinline[style=bash, breaklines=true,columns=fullflexible]}
\newcommand\@isabsolutepath[3]{%
  \StrLeft{#1}{1}[\firstchar]%
  \IfStrEq{\firstchar}{/}{#2}{#3}%
}

\newcommand{\@homeprefix}[1]{%
  \ifthenelse{\equal{#1}{}}{\textasciitilde}{\textasciitilde/}%
}

\newcommand{\prompt}[1]{%
  \color{Blue!80}\textbf{\texttt{%
    achim@logicalhacking:{\@isabsolutepath{#1}{#1}{\@homeprefix{#1}#1}}\$}}%
}
\newtcblisting{bash}[1][]{%
      listing only%
     ,boxrule=0pt
     ,boxsep=0pt
     ,colback=white!90!bash
     ,enhanced jigsaw
     ,borderline west={2pt}{0pt}{bash!60!black}
     ,sharp corners
     % ,before skip=10pt
     % ,after skip=10pt
     ,enlarge top by=0mm
     ,enhanced
     ,overlay={\node[draw,fill=bash!60!black,xshift=0pt,anchor=north
       east,font=\bfseries\footnotesize\color{white}]
                at (frame.north east) {Bash};}
     ,listing options={
           style=bash
           ,columns=flexible%
           ,breaklines=true%
           ,prebreak=\mbox{\space\textbackslash}%
           ,basicstyle=\small\ttfamily%
           ,#1
      }
  }%
%% </bash>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% <config>
\providecolor{config}{named}{gray}
\newtcblisting{config}[2][]{%
      listing only%
     ,boxrule=0pt
     ,boxsep=0pt
     ,colback=white!90!config
     ,enhanced jigsaw
     ,borderline west={2pt}{0pt}{config!60!black}
     ,sharp corners
     % ,before skip=10pt
     % ,after skip=10pt
     ,enlarge top by=0mm
     ,enhanced
     ,overlay={\node[draw,fill=config!60!black,xshift=0pt,anchor=north
       east,font=\bfseries\footnotesize\color{white}]
                at (frame.north east) {#2};}
        ,listing options={
           breakatwhitespace=true
          ,columns=flexible%
          ,basicstyle=\small\ttfamily
          ,mathescape
          ,#1
      }
  }%
%% </config>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 


