%%%==============================================================================
%% Copyright 2023-present by Alceu Frigeri
%%
%% This work may be distributed and/or modified under the conditions of
%%
%% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt),
%%   version 1.3c (or later), and/or
%% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html),
%%   version 3 (or later)
%%
%% This work has the LPPL maintenance status *maintained*.
%%
%% The Current Maintainer of this work is Alceu Frigeri
%%
%% This is version {1.11} {2025/09/18}
%%
%% The list of files that compose this work can be found in the README.md file at
%% https://ctan.org/pkg/codedescribe
%%
%%%==============================================================================
\NeedsTeXFormat{LaTeX2e}[2022/06/01]

\ProvidesExplPackage
    {codelisting}
    {2025/09/18}
    {1.11}
    {LaTeX Code Listing}

%%%%%%%
%%%
%%% Just an attempt of having my packages info in a regular way
%%% Idea being: { <pck-name> / pkg info } for each and all.
%%%
%%%%%%%
\keys_define:nn { codelisting / pkg info}
  {
     name        .code:n = {codelisting} ,
     prefix      .code:n = {codelist} ,
     date        .code:n = {2025/09/18} ,
     version     .code:n = {1.11} ,
     description .code:n = {\LaTeX~Code~Listing}
  }
\cs_if_exist:NF \PkgInfo 
  {
    \NewDocumentCommand \PkgInfo {mm} { \keys_set:nn {#1 / pkg info}{#2} } 
    \NewDocumentCommand \PkgDescription {m} 
      { \noindent Package~ \textbf{\PkgInfo{#1}{name}}~Version:~\PkgInfo{#1}{version}~ -~ \PkgInfo{#1}{date}\par \emph{\PkgInfo{#1}{description}}~\par } 
  }  
%%%%%%%
%%% End of cut-n-paste
%%%%%%%

\RequirePackage{listings,scontents}
\RequirePackage{xpeekahead}

\msg_new:nnnn {codelist} {invalid key}
  {
    (ID:#1)~Key (#2) ~not~defined!
  }
  {
    You~tried~to~use~a~non~defined~key:#2.
    ~Error~Code~ ID:<#1>.
  }

\dim_new:N \l__codelist_demo_parindent_dim
\dim_new:N \l__codelist_org_parindent_dim

\dim_set:Nn\l__codelist_org_parindent_dim{\parindent}

\colorlet{c__codelist_lightred_color}{white!97!red}
\colorlet{c__codelist_lightgreen_color}{white!97!green}
\colorlet{c__codelist_lightblue_color}{white!97!blue}
\colorlet{c__codelist_lightmagenta_color}{white!97!magenta}
\colorlet{c__codelist_lightyellow_color}{white!97!yellow}
\colorlet{c__codelist_lightteal_color}{white!97!teal}
\colorlet{c__codelist_lightcyan_color}{white!97!cyan}

\colorlet{c__codelist_lightgray_color}{white!97!black}
\colorlet{c__codelist_gray_color}{gray}
\definecolor{c__codelist_grayblack_color}{rgb}{0.8,0.8,0.8}
\definecolor{c__codelist_blackgray_color}{rgb}{0.8,0.8,0.8}

\colorlet{c__codelist_darkred_color}{red!30!black!100}
\colorlet{c__codelist_darkgreen_color}{green!30!black!100}
\colorlet{c__codelist_darkblue_color}{blue!30!black!100}
\colorlet{c__codelist_darkmagenta_color}{magenta!30!black!100}
\colorlet{c__codelist_darkyellow_color}{yellow!30!black!100}
\colorlet{c__codelist_darkteal_color}{teal!30!black!100}
\colorlet{c__codelist_darkcyan_color}{cyan!30!black!100}

\colorlet{c__codelist_redishblack_color}{red!50!black!100}
\colorlet{c__codelist_greenishblack_color}{green!50!black!100}
\colorlet{c__codelist_blueishblack_color}{blue!50!black!100}
\colorlet{c__codelist_magentaishblack_color}{magenta!50!black!100}
\colorlet{c__codelist_yellowishblack_color}{yellow!50!black!100}
\colorlet{c__codelist_tealishblack_color}{teal!50!black!100}
\colorlet{c__codelist_cyanishblack_color}{cyan!50!black!100}

\colorlet{c__codelist_redblack_color}{red!75!black!100}
\colorlet{c__codelist_greenblack_color}{green!75!black!100}
\colorlet{c__codelist_blueblack_color}{blue!75!black!100}
\colorlet{c__codelist_magentablack_color}{magenta!75!black!100}
\colorlet{c__codelist_yellowblack_color}{yellow!75!black!100}
\colorlet{c__codelist_tealblack_color}{teal!75!black!100}
\colorlet{c__codelist_cyanblack_color}{cyan!75!black!100}

\colorlet{c__codelist_grayred_color}{red!30!gray!100}
\colorlet{c__codelist_graygreen_color}{green!30!gray!100}
\colorlet{c__codelist_grayblue_color}{blue!30!gray!100}
\colorlet{c__codelist_graymagenta_color}{magenta!30!gray!100}
\colorlet{c__codelist_grayyellow_color}{yellow!30!gray!100}
\colorlet{c__codelist_grayteal_color}{teal!30!gray!100}
\colorlet{c__codelist_graycyan_color}{cyan!30!gray!100}

\colorlet{c__codelist_redishgray_color}{red!50!gray!100}
\colorlet{c__codelist_greenishgray_color}{green!50!gray!100}
\colorlet{c__codelist_blueishgray_color}{blue!50!gray!100}
\colorlet{c__codelist_magentaishgray_color}{magenta!50!gray!100}
\colorlet{c__codelist_yellowishgray_color}{yellow!50!gray!100}
\colorlet{c__codelist_tealishgray_color}{teal!50!gray!100}
\colorlet{c__codelist_cyanishgray_color}{cyan!50!gray!100}

\colorlet{c__codelist_redgray_color}{red!75!gray!100}
\colorlet{c__codelist_greengray_color}{green!75!gray!100}
\colorlet{c__codelist_bluegray_color}{blue!75!gray!100}
\colorlet{c__codelist_magentagray_color}{magenta!75!gray!100}
\colorlet{c__codelist_yellowgray_color}{yellow!75!gray!100}
\colorlet{c__codelist_tealgray_color}{teal!75!gray!100}
\colorlet{c__codelist_cyangray_color}{cyan!75!gray!100}

\tl_set:Nn \l__codelist_bckgnd_color_default_tl{c__codelist_lightgray_color}%
\tl_set:Nn \l__codelist_string_color_default_tl{c__codelist_darkteal_color}
\tl_set:Nn \l__codelist_comment_color_default_tl{c__codelist_darkgreen_color}

\tl_set:Nn \l__codelist_texcsA_color_default_tl{c__codelist_darkblue_color}
\tl_set:Nn \l__codelist_texcsB_color_default_tl{c__codelist_blueishblack_color}
\tl_set:Nn \l__codelist_texcsC_color_default_tl{c__codelist_blueblack_color}
\tl_set:Nn \l__codelist_texcsD_color_default_tl{c__codelist_blueishgray_color}

\tl_set:Nn \l__codelist_keywdA_color_default_tl{c__codelist_darkcyan_color}
\tl_set:Nn \l__codelist_keywdB_color_default_tl{c__codelist_cyanishblack_color}
\tl_set:Nn \l__codelist_keywdC_color_default_tl{c__codelist_cyanblack_color}
\tl_set:Nn \l__codelist_keywdD_color_default_tl{c__codelist_cyanishgray_color}

\tl_set:Nn \l__codelist_emphA_color_default_tl{c__codelist_darkred_color}
\tl_set:Nn \l__codelist_emphB_color_default_tl{c__codelist_redishblack_color}
\tl_set:Nn \l__codelist_emphC_color_default_tl{c__codelist_redblack_color}
\tl_set:Nn \l__codelist_emphD_color_default_tl{c__codelist_redishgray_color}

\tl_set:Nn \l__codelist_rule_color_default_tl{c__codelist_gray_color}
\tl_set:Nn \l__codelist_number_color_default_tl{c__codelist_gray_color}

\keys_define:nn {codelist / options} {
  settexcs      .usage:n   =  general,
  settexcs      .code:n    =  { \tl_set:Nn \l__codelist_texcsA_tl {#1} },
  settexcs      .default:n =   ,
  
  texcs         .usage:n   =   general,
  texcs         .code:n    =  { \tl_put_right:Nn \l__codelist_texcsA_tl { , #1} },
  texcs         .default:n =  ,

  texcsstyle    .usage:n   =   general,
  texcsstyle    .code:n    =  { \tl_set:Nn \l__codelist_texcsAstyle_tl {#1} },
  texcsstyle    .default:n =  \bfseries\color{ \l__codelist_texcsA_color_default_tl },

  settexcs2     .usage:n   =   general,
  settexcs2     .code:n    =  { \tl_set:Nn \l__codelist_texcsB_tl {#1} },
  settexcs2     .default:n =  ,

  texcs2        .usage:n   =   general,
  texcs2        .code:n    =  { \tl_put_right:Nn \l__codelist_texcsB_tl { , #1} },
  texcs2        .default:n =  ,

  texcs2style   .usage:n   =   general,
  texcs2style   .code:n    =  { \tl_set:Nn \l__codelist_texcsBstyle_tl {#1} },
  texcs2style   .default:n =  \bfseries\color{ \l__codelist_texcsB_color_default_tl } ,

  settexcs3     .usage:n   =   general,
  settexcs3     .code:n    =  { \tl_set:Nn \l__codelist_texcsC_tl {#1} },
  settexcs3     .default:n =  ,

  texcs3        .usage:n   =   general,
  texcs3        .code:n    =  { \tl_put_right:Nn \l__codelist_texcsC_tl { , #1} },
  texcs3        .default:n =  ,

  texcs3style   .usage:n   =   general,
  texcs3style   .code:n    =  { \tl_set:Nn \l__codelist_texcsCstyle_tl {#1} },
  texcs3style   .default:n =  \bfseries\color{ \l__codelist_texcsC_color_default_tl } ,

  settexcs4     .usage:n   =   general,
  settexcs4     .code:n    =  { \tl_set:Nn \l__codelist_texcsD_tl {#1} },
  settexcs4     .default:n =  ,

  texcs4        .usage:n   =   general,
  texcs4        .code:n    =  { \tl_put_right:Nn \l__codelist_texcsD_tl { , #1} },
  texcs4        .default:n =  ,

  texcs4style   .usage:n   =   general,
  texcs4style   .code:n    =  { \tl_set:Nn \l__codelist_texcsDstyle_tl {#1} },
  texcs4style   .default:n =  \bfseries\color{ \l__codelist_texcsD_color_default_tl } ,

  setkeywd      .usage:n   =   general,
  setkeywd      .code:n    =  { \tl_set:Nn \l__codelist_keywdA_tl {#1} },
  setkeywd      .default:n =  ,

  keywd         .usage:n   =   general,
  keywd         .code:n    =  { \tl_put_right:Nn \l__codelist_keywdA_tl { , #1} },
  keywd         .default:n =  ,

  keywdstyle    .usage:n   =   general,
  keywdstyle    .code:n    =  { \tl_set:Nn \l__codelist_keywdstyle_tl {#1} },
  keywdstyle    .default:n =  \bfseries\color{ \l__codelist_keywdA_color_default_tl } ,

  setkeywd2     .usage:n   =   general,
  setkeywd2     .code:n    =  { \tl_set:Nn \l__codelist_keywdB_tl {#1} },
  setkeywd2     .default:n =  ,

  keywd2        .usage:n   =   general,
  keywd2        .code:n    =  { \tl_put_right:Nn \l__codelist_keywdB_tl { , #1} },
  keywd2        .default:n =  ,

  keywd2style   .usage:n   =   general,
  keywd2style   .code:n    =  { \tl_set:Nn \l__codelist_keywdBstyle_tl {#1} },
  keywd2style   .default:n =  \bfseries\color{ \l__codelist_keywdB_color_default_tl } ,

  setkeywd3     .usage:n   =   general,
  setkeywd3     .code:n    =  { \tl_set:Nn \l__codelist_keywdC_tl {#1} },
  setkeywd3     .default:n =  ,

  keywd3        .usage:n   =   general,
  keywd3        .code:n    =  { \tl_put_right:Nn \l__codelist_keywdC_tl { , #1} },
  keywd3        .default:n =  ,

  keywd3style   .usage:n   =   general,
  keywd3style   .code:n    =  { \tl_set:Nn \l__codelist_keywdCstyle_tl {#1} },
  keywd3style   .default:n =  \bfseries\color{ \l__codelist_keywdC_color_default_tl } ,

  setkeywd4     .usage:n   =   general,
  setkeywd4     .code:n    =  { \tl_set:Nn \l__codelist_keywdD_tl {#1} },
  setkeywd4     .default:n =  ,

  keywd4        .usage:n   =   general,
  keywd4        .code:n    =  { \tl_put_right:Nn \l__codelist_keywdD_tl { , #1} },
  keywd4        .default:n =  ,

  keywd4style   .usage:n   =   general,
  keywd4style   .code:n    =  { \tl_set:Nn \l__codelist_keywdDstyle_tl {#1} },
  keywd4style   .default:n =  \bfseries\color{ \l__codelist_keywdD_color_default_tl } ,

  setemph       .usage:n   =   general,
  setemph       .code:n    =  { \tl_set:Nn \l__codelist_emphA_tl {#1} },
  setemph       .default:n =  ,

  emph          .usage:n   =   general,
  emph          .code:n    =  { \tl_put_right:Nn \l__codelist_emphA_tl { , #1} },
  emph          .default:n =  ,

  emphstyle     .usage:n   =   general,
  emphstyle     .code:n    =  { \tl_set:Nn \l__codelist_emphAstyle_tl {#1} },
  emphstyle     .default:n =  \bfseries\color{ \l__codelist_emphA_color_default_tl } ,

  setemph2      .usage:n   =   general,
  setemph2      .code:n    =  { \tl_set:Nn \l__codelist_emphB_tl {#1} },
  setemph2      .default:n =  ,

  emph2         .usage:n   =   general,
  emph2         .code:n    =  { \tl_put_right:Nn \l__codelist_emphB_tl { , #1} },
  emph2         .default:n =  ,

  emph2style    .usage:n   =   general,
  emph2style    .code:n    =  { \tl_set:Nn \l__codelist_emphBstyle_tl {#1} },
  emph2style    .default:n =  \bfseries\color{ \l__codelist_emphB_color_default_tl } ,

  setemph3      .usage:n   =   general,
  setemph3      .code:n    =  { \tl_set:Nn \l__codelist_emphC_tl {#1} },
  setemph3      .default:n =  ,

  emph3         .usage:n   =   general,
  emph3         .code:n    =  { \tl_put_right:Nn \l__codelist_emphC_tl { , #1} },
  emph3         .default:n =  ,

  emph3style    .usage:n   =   general,
  emph3style    .code:n    =  { \tl_set:Nn \l__codelist_emphCstyle_tl {#1} },
  emph3style    .default:n =  \bfseries\color{ \l__codelist_emphC_color_default_tl } ,

  setemph4      .usage:n   =   general,
  setemph4      .code:n    =  { \tl_set:Nn \l__codelist_emphD_tl {#1} },
  setemph4      .default:n =  ,

  emph4         .usage:n   =   general,
  emph4         .code:n    =  { \tl_put_right:Nn \l__codelist_emphD_tl { , #1} },
  emph4         .default:n =  ,

  emph4style    .usage:n   =   general,
  emph4style    .code:n    =  { \tl_set:Nn \l__codelist_emphDstyle_tl {#1} },
  emph4style    .default:n =  \bfseries\color{ \l__codelist_emphD_color_default_tl } ,

  stringstyle   .usage:n   =   general,
  stringstyle   .code:n    =  { \tl_set:Nn \l__codelist_stringstyle_tl {#1} },
  stringstyle   .default:n =  \color{ \l__codelist_string_color_default_tl } ,

  commentstyle  .usage:n   =   general,
  commentstyle  .code:n    =  { \tl_set:Nn \l__codelist_commentstyle_tl {#1} },
  commentstyle  .default:n =  \color{ \l__codelist_comment_color_default_tl } ,

  bckgndcolor   .usage:n   =   general,
  bckgndcolor   .code:n    =  { \tl_set:Nn \l__codelist_bckgndcolor_tl {#1} },
  bckgndcolor   .default:n =  \color{ \l__codelist_bckgnd_color_default_tl } ,

  rulecolor     .usage:n   =   general,
  rulecolor     .code:n    =  { \tl_set:Nn \l__codelist_rulecolor_tl {#1} },
  rulecolor     .default:n =  \color{ \l__codelist_rule_color_default_tl } ,

  numbers       .usage:n   =   general,
  numbers       .code:n    =  { \tl_set:Nn \l__codelist_numbers_tl {#1} },
  numbers       .default:n =  none,

  numberstyle   .usage:n   =   general,
  numberstyle   .code:n    =  { \tl_set:Nn \l__codelist_numberstyle_tl {#1} },
  numberstyle   .default:n =  \tiny\color{ \l__codelist_number_color_default_tl },

  parindent     .usage:n   =   general,
  parindent     .code:n    =  { \dim_set:Nn\l__codelist_demo_parindent_dim{#1} },
  parindent     .default:n =  \l__codelist_org_parindent_dim,

	codeprefix    .usage:n   =   general,
	codeprefix    .code:n    =  { \tl_set:Nn \l__codelist_codeprefix_tl {#1} },
  codeprefix    .default:n =  \LaTeX\  Code:,

	resultprefix  .usage:n   =   general,
	resultprefix  .code:n    =  { \tl_set:Nn \l__codelist_resultprefix_tl {#1} },
  resultprefix  .default:n =  \LaTeX\  Result:,

  ruleht        .usage:n   =   general,
  ruleht        .code:n    =  { \tl_set:Nn \l__codelist_ruleheight_tl {#1} },
  ruleht        .default:n =  1,
  
  letter        .usage:n   =   general,
  letter        .code:n    =  { \tl_set:Nn \l__codelist_alsoletter_tl {#1} },
  letter        .default:n =  { _ , : , @ },
  
  other        .usage:n    =   general,
  other        .code:n     =  { \tl_set:Nn \l__codelist_alsoother_tl {#1} },
  other        .default:n  =  { },
  
  basicstyle    .usage:n   =   general,
  basicstyle    .code:n    =  { \tl_set:Nn \l__codelist_basicstyle_tl {#1} },
  basicstyle    .default:n =  \footnotesize\ttfamily ,

  default       .usage:n   =   general,
  default       .meta:n    = {
      settexcs     , texcsstyle  , 
      settexcs2    , texcs2style , 
      settexcs3    , texcs3style ,
      settexcs4    , texcs4style ,
		  setkeywd     , keywdstyle  , 
      setkeywd2    , keywd2style , 
      setkeywd3    , keywd3style ,
      setkeywd4    , keywd4style ,
		  setemph      , emphstyle   , 
      setemph2     , emph2style  , 
      setemph3     , emph3style  ,
      setemph4     , emph4style  ,
		  stringstyle  , 
      commentstyle , 
      bckgndcolor  , rulecolor   ,
		  numbers      , numberstyle ,
      parindent    ,
      codeprefix   , resultprefix ,
      ruleht       , 
      letter       ,
      other        ,
      basicstyle   ,
  },
}

%%%%%%
%%% This one can't be protected
%%%%%%
\cs_new:Npn \__codelist_set_options:n #1 {\keys_set:nn {codelist / options}{#1}}

\__codelist_set_options:n{default}


%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%
%%%
%%%  From doctools.dtx file
%%% lstlistings 'definitions'
%%%
%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%

%%% listings
%
\lstdefinestyle{lstStyleDefault}{
                              %%% appearance
   ,basicstyle=\small\ttfamily % Standardschrift
                              %%%  Space and placement
   ,floatplacement=tbp        % is used as float place specifier
   ,aboveskip=\medskipamount  % define the space above and
   ,belowskip=\medskipamount  % below displayed listings.
   ,lineskip=0pt              % specifies additional space between lines in listings.
   ,boxpos=c                  % c,b,t
   ,showlines=false           % prints empty lines at the end of listings
                              %%% characters
   ,extendedchars=true        % allows or prohibits extended characters
                              % in listings, that means (national)
                              % characters of codes 128-255.
   ,upquote=true              % determines printing of quotes
   ,tabsize=2,                % chars of tab
   ,showtabs=false            % do not show tabs
   ,showspaces=false          % do not show spaces
   ,showstringspaces=false    % do not show blank spaces in string
                              %%% Line numbers
   ,numbers=none              % left, right, none
                              %%% Captions
   ,numberbychapter=true      %
   ,captionpos=b              % t,b
   ,abovecaptionskip=\smallskipamount % the vertical space respectively above
   ,belowcaptionskip=\smallskipamount % or below each caption
                              %%% Margins and line shape
   ,linewidth=\linewidth      % defines the base line width for listings.
   ,xleftmargin=0pt           % extra margins
   ,xrightmargin=0pt          %
   ,resetmargins=false        % indention from list environments like enumerate
                              % or itemize is reset, i.e. not used.
   ,breaklines=true           % line breaking of long lines.
   ,breakatwhitespace=false   % allows line breaks only at white space.
   ,breakindent=0pt           % is the indention of the second, third, ...
                              % line of broken lines.
   ,breakautoindent=true      % apply intendation
   ,columns=flexible          %
   ,keepspaces=true           %
}


\lstdefinestyle{lstDocStyleBase}{
                              %%% base style
   ,style=lstStyleDefault

   ,commentstyle=\slshape
                              %%% Line numbers
   ,numbers=left              % left, right, none
   ,stepnumber=1              % seperation between numbers
   ,numberfirstline=false     % number first line always
                              %
   ,numberstyle={\l__codelist_numberstyle_tl}    % style of numbers
   ,numbersep=5pt             % distance to text
   ,numberblanklines=true     %
                              %%% language
   ,language = [LaTeX]TeX
                              %%% commands
                              % LaTeX programming
   ,moretexcs={setlength,usepackage,newcommand,renewcommand,providecommand,RequirePackage,SelectInputMappings,ifpdftex,ifpdfoutput,AtBeginEnvironment,ProvidesPackage},
   ,moretexcs={maketitle,text,includegraphics,chapter,section,subsection,
     subsubsection,paragraph,textmu,enquote,blockquote,ding,mathds,ifcsdef,Bra,Ket,Braket,subcaption,lettrine,mdfsetup,captionof,listoffigures,listoftables,tableofcontents,appendix}
   ,moretexcs={newcolumntype,rowfont,taburowcolors,rowcolor,rowcolors,bottomrule,
     toprule,midrule,}
   ,moretexcs={hypersetup}
   ,moretexcs={gls,printglossary,glsadd,newglossaryentry,newacronym}
   ,moretexcs={mainmatter,frontmatter,geometry,KOMAoptions,setkomafont,addtokomafont}
   ,moretexcs={si,SI,sisetup,unit,unitfrac,micro}
   ,moretexcs={newblock,ExecuteBibliographyOptions,addbibresource}
   ,moretexcs={operatorname,frac,sfrac,dfrac,DeclareMathOperator,mathcal,underset}
   ,moretexcs={democodefile,package,cs,command,env,DemoError,PrintDemo}
   ,moretexcs={theadstart,tbody,tsubheadstart,tsubhead,tend}
   ,moretexcs={DefineCodeSection,SetCodeSection,BeginCodeSection,EndCodeSection}
   ,moretexcs={IfDefined,IfUndefined,IfElseDefined,IfElseUndefined,IfMultDefined,IfNotDraft,IfNotDraftElse,IfDraft,IfPackageLoaded,IfElsePackageLoaded,IfPackageNotLoaded,IfPackagesLoaded,IfPackagesNotLoaded,ExecuteAfterPackage,ExecuteBeforePackage,IfTikzLibraryLoaded,IfColumntypeDefined,IfColumntypesDefined,IfColorDefined,IfColorsDefined,IfMathVersionDefined,SetTemplateDefinition,UseDefinition,IfFileExists,iflanguage}
   ,moretexcs={setuptablefontsize,tablefontsize,setuptablestyle,tablestyle,  setuptablecolor,tablecolor,disablealternatecolors,   tablealtcolored,tbegin,tbody,tend,thead, theadstart,tsubheadstart,tsubhead,theadrow,tsubheadrow,resettablestyle,theadend,tsubheadend,tableitemize,PreserveBackslash}
   ,moretexcs={todo,missingfigure}
   ,moretexcs={lstloadlanguages,lstdefinestyle,lstset}
   ,moretexcs={indexsetup}
   ,moretexcs={newglossarystyle,glossarystyle,deftranslation,newglossary}
   ,moretexcs={usetikzlibrary}
   ,moretexcs={definecolor,colorlet}
   ,moretexcs={captionsetup,DeclareCaptionStyle}
   ,moretexcs={floatsetup}
   ,moretexcs={EnableCrossrefs,DisableCrossrefs,PageIndex,CodelineIndex,CodelineNumbered}
   ,moretexcs={cref,Cref,vref,eqnref,figref,tabref,secref,chapref}
}

%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%
%%%
%%%  From doctools.dtx file
%%% lstlistings 'definitions'
%%%
%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%

\lstloadlanguages{[LaTeX]TeX}
\lstdefinestyle{codestyle}{
  style        =  lstDocStyleBase,
  alsoletter   =  {\l__codelist_alsoletter_tl},
  alsoother    =  {\l__codelist_alsoother_tl},
  numbers      =   none, %\l__codelist_numbers_tl,
  rulecolor    =      {\l__codelist_rulecolor_tl},
	backgroundcolor  =  {\l__codelist_bckgndcolor_tl},
  stringstyle  =      {\l__codelist_stringstyle_tl},
  commentstyle =      {\l__codelist_commentstyle_tl},
  framerule    =   0pt,                        %0.4pt,
	nolol        =   true,
	frame        =   none,
	aboveskip    =   0pt,%-2ex,
  belowskip    =   0pt,%-2ex,
	basicstyle   =   {\l__codelist_basicstyle_tl}, %%\footnotesize\ttfamily ,  
	rulesep      =   0pt,
	breaklines   =   true,
	breakindent  =   3em,
	moretexcs    =      {\l__codelist_texcsA_tl},
  moretexcs    =  [2] {\l__codelist_texcsB_tl},
  moretexcs    =  [3] {\l__codelist_texcsC_tl},
  moretexcs    =  [4] {\l__codelist_texcsD_tl},
  texcsstyle   = *    {\l__codelist_texcsAstyle_tl},
	texcsstyle   = *[2] {\l__codelist_texcsBstyle_tl},
	texcsstyle   = *[3] {\l__codelist_texcsCstyle_tl},
	texcsstyle   = *[4] {\l__codelist_texcsDstyle_tl},
	morekeywords =      {\l__codelist_keywdA_tl},
	morekeywords =  [2] {\l__codelist_keywdB_tl},
	morekeywords =  [3] {\l__codelist_keywdC_tl},
	morekeywords =  [4] {\l__codelist_keywdD_tl},
  keywordstyle =      {\l__codelist_keywdstyle_tl},
	keywordstyle =  [2] {\l__codelist_keywdBstyle_tl},
	keywordstyle =  [3] {\l__codelist_keywdCstyle_tl},
	keywordstyle =  [4] {\l__codelist_keywdDstyle_tl},
	emph         =      {\l__codelist_emphA_tl},
	emph         =  [2] {\l__codelist_emphB_tl},
	emph         =  [3] {\l__codelist_emphC_tl},
	emph         =  [4] {\l__codelist_emphD_tl},
  emphstyle    =      {\l__codelist_emphAstyle_tl},
  emphstyle    =  [2] {\l__codelist_emphBstyle_tl},
  emphstyle    =  [3] {\l__codelist_emphCstyle_tl},
  emphstyle    =  [4] {\l__codelist_emphDstyle_tl},
}
\lstset{style=codestyle}

\cs_generate_variant:Nn \keys_set:nn {nx}
%%%
%%% extending scontents package options !!!!
%%% changing it's default key handling from \msg_error to 'store-env'.
%%%
\keys_define:nn { scontents / scontents}
  {
  	st.meta:nn          = { scontents } { store-env = {#1} },
    st.value_required:n = true,
  	store-at.meta:nn          = { scontents } { store-env = {#1} },
    store-at.value_required:n = true,
    unknown . undefine: ,
    unknown . code:n = { \keys_set:nx {scontents}{ store-env = {\l_keys_key_str}} }
 }

\newenvsc{codestore}


%%%%%%%%%
%%%
%%% yep, the <verbatimsc> environment gets redefined 
%%% each and every time (to change the 'listings' defaults/highlights !
%%%
%%%
%%%%%%%%%

\cs_undefine:c{verbatimsc}
\cs_undefine:c{verbatimsc@@}
\cs_undefine:c{verbatimsc@}
\cs_undefine:c{endverbatimsc}
\lstnewenvironment{verbatimsc}{\lstset{style=codestyle}}{}

%%%%%%
%%% This one can't be protected
%%%%%%
\cs_new:Npn \__codelist_set_verbsc:n #1
  {
      \cs_undefine:c{verbatimsc}
      \cs_undefine:c{verbatimsc@@}
      \cs_undefine:c{verbatimsc@}
      \cs_undefine:c{endverbatimsc}
      
  		\__codelist_set_options:n{#1}

      \lstnewenvironment{verbatimsc}
        {
          \exp_args:Ne \lstset
            {
              style=codestyle,
              alsoletter    =     {\l__codelist_alsoletter_tl},
              alsoother     =     {\l__codelist_alsoother_tl},
              moretexcs     =     {\l__codelist_texcsA_tl},
              moretexcs     =[2]  {\l__codelist_texcsB_tl},
              moretexcs     =[3]  {\l__codelist_texcsC_tl},
              moretexcs     =[4]  {\l__codelist_texcsD_tl},
        		  morekeywords  =     {\l__codelist_keywdA_tl},
              morekeywords  =[2]  {\l__codelist_keywdB_tl},
              morekeywords  =[3]  {\l__codelist_keywdC_tl},
              morekeywords  =[4]  {\l__codelist_keywdD_tl},
        		  emph          =     {\l__codelist_emphA_tl}, 
              emph          =[2]  {\l__codelist_emphB_tl}, 
              emph          =[3]  {\l__codelist_emphC_tl},
              emph          =[4]  {\l__codelist_emphD_tl},
        	    numbers       =     \l__codelist_numbers_tl
            }
        }
        {}
  }

%%% !!! DEPRECATED !!!
\NewDocumentEnvironment {verbsc} {m}
  {
      \cs_undefine:c{verbatimsc}
      \cs_undefine:c{verbatimsc@@}
      \cs_undefine:c{verbatimsc@}
      \cs_undefine:c{endverbatimsc}
      
  		\__codelist_set_options:n{#1}

      \lstnewenvironment{verbatimsc}
        {
          \exp_args:Ne \lstset
            {
              style=codestyle,
              moretexcs     =     {\l__codelist_texcsA_tl},
              moretexcs     =[2]  {\l__codelist_texcsB_tl},
              moretexcs     =[3]  {\l__codelist_texcsC_tl},
        		  morekeywords  =     {\l__codelist_keywdA_tl},
              morekeywords  =[2]  {\l__codelist_keywdB_tl},
              morekeywords  =[3]  {\l__codelist_keywdC_tl},
        		  emph          =     {\l__codelist_emphA_tl}, 
              emph          =[2]  {\l__codelist_emphB_tl}, 
              emph          =[3]  {\l__codelist_emphC_tl},
        	    numbers       =     \l__codelist_numbers_tl
            }
        }
        {}
  }
  {  } %environment's end


\NewDocumentCommand \setcodekeys {m}
  {
    \keys_set:nn {codelist / options}{#1}
  }

\NewDocumentCommand \setnewcodekey {mm}
  {
    \keys_define:nn {codelist / options}
      {
        #1 .meta:nn = {codelist / options} { #2 } ,
        #1 .value_forbidden:n = true ,
      }
  }  

\cs_new_protected:Npn \__codelist_ts_settings:nn #1#2
  {
    \bool_if:NTF #1
      { % paragraph wide OR one below the other
        \dim_set:Nn \l__codedesc_width_dim {\l__codedesc_textcolwidth_dim}
        \dim_set:Nn \l__codedesc_offset_dim {0pt}
        \tl_set:Nn \l__codedesc_rule_in_tl {\__codedesc_rule:n {\l__codelist_ruleheight_tl}}
        \tl_set:Nn \l__codedesc_rule_out_tl {}
      }
      { % half as wide OR side-by-side
        \dim_set:Nn \l__codedesc_width_dim {0.5\l__codedesc_textcolwidth_dim}
        \dim_set:Nn \l__codedesc_offset_dim {0.5\l__codedesc_width_dim}
        \tl_set:Nn \l__codedesc_rule_in_tl {}
        \tl_set:Nn \l__codedesc_rule_out_tl {\__codedesc_rule:n {\l__codelist_ruleheight_tl}}
      }
  
      \__codelist_set_verbsc:n {#2}
      \tl_if_blank:VTF \l__codelist_codeprefix_tl
        {
          \vcoffin_set:Nnn \__codelist_code_label_coffin {\l__codedesc_width_dim}
            {}
        }
        {
          \vcoffin_set:Nnn \__codelist_code_label_coffin {\l__codedesc_width_dim}
            {
              \raggedright
              \l__codelist_codeprefix_tl
            }
        }
        
%       \coffin_show_structure:N \__codelist_code_label_coffin

      \tl_if_blank:VTF \l__codelist_resultprefix_tl
        {
          \vcoffin_set:Nnn \__codelist_demo_label_coffin {\l__codedesc_width_dim}
            {}
        }
        {
          \vcoffin_set:Nnn \__codelist_demo_label_coffin {\l__codedesc_width_dim}
            {
              \raggedright
              \l__codelist_resultprefix_tl
            }
        }
  }

\cs_new_protected:Npn \__codelist_codecoffin:nnnn #1#2#3#4
  {
    \vcoffin_set:Nnn \__codelist_code_coffin {\l__codedesc_width_dim}
      {
        \raggedright #4
        \tl_if_blank:nTF {#2}
          { #3 {#1} }
          { #3 [#2]{#1} }
      }
  }

\cs_new_protected:Npn \__codelist_democoffin:nn #1#2
  {
    \vcoffin_set:Nnn \__codelist_demo_coffin {\l__codedesc_width_dim}
      { 
        \l__codedesc_rule_in_tl
        \group_begin:
          \skip_vertical:n {\l__codedesc_largerskip_dim}
          \setlength\parindent{\l__codelist_demo_parindent_dim}
          \tl_if_blank:nTF {#2}
            { \getstored {#1} }
            { \getstored [#2]{#1} }
          \skip_vertical:n {\l__codedesc_tinyskip_dim}
        \group_end:
        \l__codedesc_rule_in_tl
      }
  }

\cs_new_protected:Npn \__codelist_ts_code:nnnnn #1#2#3#4#5
  {
    \__codedesc_set_textcolwidth:
    \group_begin:
      \__codelist_ts_settings:nn {#1}{#2}
      \__codelist_codecoffin:nnnn {#3}{#4}{#5}{}
      
        \coffin_join:NnnNnnnn
          \__codelist_code_label_coffin {l}{b}
          \__codelist_code_coffin {l}{t}
          {0pt}{-1ex}
        \skip_vertical:n{-1ex}\noindent
        \coffin_typeset:Nnnnn \__codelist_code_label_coffin {l}{t}{\l__codedesc_offset_dim}{0pt}
        \bool_if:NTF \l__codelist_tscode_peek_bool
          { \skip_vertical:n {\l__codedesc_large_skip} }
          { \skip_vertical:n {\l__codedesc_largeline_skip} }
    \group_end: 
  }


\xpeekahead_set:Nnnn \__codelist_cmd_peek_tscode:
  {
    \c{typesetcode} | \c{tscode} | \c{typesetmergedcode} | \c{tsmergedcode} | 
    \c{typesetdemo} | \c{tsdemo} | \c{typesetresult} | \c{tsresult} 
  }
  { 
   \bool_set_true:N  \l__codelist_tscode_peek_bool
    \l__codelist_tscode_tl:
  }
  { 
    \bool_set_false:N \l__codelist_tscode_peek_bool
    \l__codelist_tscode_tl:
  }    
  

\bool_new:N \l__codelist_tscode_peek_bool

\tl_new:N \l__codelist_tscode_tl:


\NewDocumentCommand{\tscode}{sO{}mO{}}
  {
    \tl_set:Nn \l__codelist_tscode_tl: { \__codelist_ts_code:nnnnn {#1}{#2}{#3}{#4}{\typestored} }
    \xpeekahead_cmd_peek:N \__codelist_cmd_peek_tscode:
  }


\cs_new_eq:NN \typesetcode \tscode 


\NewDocumentCommand{\tsmergedcode}{sO{}m}
  {
    \tl_set:Nn \l__codelist_tscode_tl: { \__codelist_ts_code:nnnnn {#1}{#2}{#3}{}{\mergesc[typestored,print-cmd]} }
    \xpeekahead_cmd_peek:N \__codelist_cmd_peek_tscode:
  }
  
\cs_new_eq:NN \typesetmergedcode \tsmergedcode

\coffin_new:N \__codelist_code_coffin
\coffin_new:N \__codelist_code_label_coffin
\coffin_new:N \__codelist_demo_coffin
\coffin_new:N \__codelist_demo_label_coffin

\dim_new:N \l__codedesc_width_dim
\dim_new:N \l__codedesc_offset_dim

\cs_new_protected:Npn \__codelist_ts_democode:nnnn #1#2#3#4
  {
    \__codedesc_set_textcolwidth:
    \group_begin:
      \par\setlength\parindent{0pt}
      \__codelist_ts_settings:nn {#1}{#2}
      
      \__codelist_codecoffin:nnnn {#3}{#4}{\typestored}{ \skip_vertical:n {0.1ex} }
      \__codelist_democoffin:nn {#3}{#4}

      \bool_if:NTF #1
        { % one below the other
          \coffin_join:NnnNnnnn
            \__codelist_code_label_coffin {l}{b}
            \__codelist_code_coffin {l}{t}
            {0pt}{-1ex}
          \skip_vertical:n {-1ex}
          \coffin_typeset:Nnnnn \__codelist_code_label_coffin {l}{t}{0pt}{0pt}
          \skip_vertical:n {1ex}
          \coffin_join:NnnNnnnn
            \__codelist_demo_label_coffin {l}{b}
            \__codelist_demo_coffin {l}{t}
            {0pt}{-1ex}
          \coffin_typeset:Nnnnn \__codelist_demo_label_coffin {l}{t}{0pt}{0pt}
          \bool_if:NTF \l__codelist_tscode_peek_bool
            { \skip_vertical:n {\l__codedesc_large_skip} }
            { \skip_vertical:n {\l__codedesc_largeline_skip} }
        }
        { % side-by-side
          \coffin_join:NnnNnnnn
            \__codelist_code_label_coffin {r}{b}
            \__codelist_demo_label_coffin {l}{b}
            {0pt}{0pt}    
          \coffin_join:NnnNnnnn
            \__codelist_code_coffin {r}{vc}
            \__codelist_demo_coffin {l}{vc}
            {0pt}{0pt}    
        
          \coffin_typeset:Nnnnn \__codelist_code_label_coffin {l}{t}{0pt}{0pt}
          \skip_vertical:n {1ex}
          \l__codedesc_rule_out_tl
          \coffin_typeset:Nnnnn \__codelist_code_coffin {l}{t}{0pt}{0pt}
          \skip_vertical:n {0.25ex}
          \l__codedesc_rule_out_tl
        \bool_if:NTF \l__codelist_tscode_peek_bool
          { \skip_vertical:n {\l__codedesc_large_skip} }
          { \skip_vertical:n {\l__codedesc_largeline_skip} }
        }
    \group_end:
  }
  
\NewDocumentCommand{\tsdemo}{sO{}mO{}}
  {
    \tl_set:Nn \l__codelist_tscode_tl: { \__codelist_ts_democode:nnnn {#1}{#2}{#3}{#4} }
    \xpeekahead_cmd_peek:N \__codelist_cmd_peek_tscode:
    
  }


\cs_new_eq:NN \typesetdemo \tsdemo 

\NewDocumentCommand{\tsexec}{mO{}}
  {
    \tl_if_blank:nTF {#2}
      { \getstored{#1} }
      { \getstored[#2]{#1} }
  }

\cs_new_protected:Npn \__codelist_ts_demo:nnnn #1#2#3#4
  {
    \__codedesc_set_textcolwidth:
    \group_begin:
      \par\setlength\parindent{0pt}
      \__codelist_ts_settings:nn {#1}{#2}
      
      \tl_set:Nn \l__codedesc_rule_in_tl {\__codedesc_rule:n {\l__codelist_ruleheight_tl}}
    
      \__codelist_democoffin:nn {#3}{#4}

      \coffin_join:NnnNnnnn
        \__codelist_demo_label_coffin {l}{b}
        \__codelist_demo_coffin {l}{t}
        {0pt}{-1ex}
      \coffin_typeset:Nnnnn \__codelist_demo_label_coffin {l}{t}{\l__codedesc_offset_dim}{0pt}
        \bool_if:NTF \l__codelist_tscode_peek_bool
          { \skip_vertical:n {\l__codedesc_large_skip} }
          { \skip_vertical:n {\l__codedesc_largeline_skip} }
    \group_end:
  }

\NewDocumentCommand{\tsresult}{sO{}mO{}}
  {
    \tl_set:Nn \l__codelist_tscode_tl: { \__codelist_ts_demo:nnnn {#1}{#2}{#3}{#4} }
    \xpeekahead_cmd_peek:N \__codelist_cmd_peek_tscode:
  }

\cs_new_eq:NN \typesetresult \tsresult 


%%%
%%% from https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings#Encoding_issue
\lstset{literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
	{ã}{{\~{a}}}1 {õ}{{\~{o}}}1
	{Ã}{{\~{A}}}1 {Õ}{{\~{O}}}1
	{ñ}{{\~{n}}}1 {Ñ}{{\~{N}}}1
  {ý}{{\'{y}}}1 {Ý}{{\'{Y}}}1  {ÿ}{{\"{y}}}1 {Ÿ}{{\"{Y}}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\EUR}}1 {£}{{\pounds}}1
}
  
\endinput
