%%%==============================================================================
%% Copyright 2022 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.0.1beta (2022/09/06)
%%
%% The list of files that compose this work can be find in the README.md file at
%% https://ctan.org/pkg/democodetools
%%
%%%==============================================================================
\NeedsTeXFormat{LaTeX2e}[2022/06/01]
\def\tc@tmp@packname{democodetools}
\def\tc@tmp@altpackname{democodetools}
\def\tc@tmp@packdesc{Demo Code Tools}
\def\tc@tmp@packdate{2022/09/06}
\def\tc@tmp@packversion{1.0.1beta}
\let\@exp\expandafter
\@exp\edef\csname\tc@tmp@altpackname version\endcsname{\tc@tmp@packversion}
\@exp\@exp\@exp\ProvidesPackage\@exp\@exp\@exp{\@exp\tc@tmp@packname\@exp}\@exp[\tc@tmp@packdate\space v\tc@tmp@packversion\space\tc@tmp@packdesc]

\RequirePackage{democodelisting}




\setdclisting{settexcs2={Macro,Arg,xarg,oarg,marg,parg,Key,Keylst,DescribeMacro,DescribeKey,DescribeValue,DescribeOption,DescribeArg},setkeywd2={Envs,Macros,Args,Syntax,Keys,Values,Options}}


\def\dc@keyvaluestr{Value}
\def\dc@keyvaluemeanstr{Meaning}
\def\dc@keydefaultstr{Default}
\def\dc@csfontsize{\small}


\NewDocumentCommand{\MarginNote}{+m}{\marginpar{{\scriptsize\hfill#1}}}


%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
\DeclareDocumentCommand{\Meta}{m}{\ensuremath\langle#1\ensuremath\rangle}

%%%
%%% In text arg
%%%
\gdef\dc@ArgHack{}
\NewDocumentCommand{\dc@arg}{st+O{Code}O{}mmm}{{\MetaFmt[#3]{\dc@ArgHack#5\csname #4\endcsname{{\IfBooleanT{#1}{\bfseries}\IfBooleanTF{#2}{#6}{\detokenize{#6}}}}#7}}\gdef\dc@ArgHack{\,}}
\NewDocumentCommand{\oarg}{O{Meta}m}{\dc@arg+[Oarg][#1]{[}{#2}{]}}
\NewDocumentCommand{\marg}{O{Meta}m}{\dc@arg+[Marg][#1]{\{}{#2}{\}}}
\NewDocumentCommand{\parg}{O{Meta}m}{\dc@arg+[Parg][#1]{(}{#2}{)}}
\NewDocumentCommand{\xarg}{O{Meta}m}{\dc@arg+[Xarg][#1]{<}{#2}{>}}
\NewDocumentCommand{\Arg}{m}{\def\dc@ArgHack{}\dc@arg+[Marg][Meta]{}{#1}{}}

\NewDocumentCommand{\dc@x}{O{}O{}m}{#1\dc@arg+[#2][]{}{#3}{}}% single mandatory value => \dc@arg
\NewDocumentCommand{\dc@xhack}{O{}m}{\dc@x[\dc@XHack][#1]{#2}\def\dc@XHack{\,, }}% single mandatory value => \dc@arg
\NewDocumentCommand{\dc@xlst}{O{}O{}m}{\gdef\dc@ArgHack{}\def\dc@XHack{}\forcsvlist{\dc@xhack[#2]}{#3}\ifblank{#1}{}{\hfill\dc@keydefaultstr:~{\bfseries\Key{#1}}}}%single KEY... LIST of ValueS... default Value (if any)


\NewDocumentCommand{\Key}{O{}m}{\dc@x[#1][Value]{#2}}
\NewDocumentCommand{\Keylst}{O{}m}{\dc@xlst[#1][Value]{#2}}

\NewDocumentCommand{\KeyUse}{mm}{\Key{#1}\space $=$ \Meta{#2}}


\NewDocumentCommand{\Env}{O{}m}{\dc@x[#1][Env]{#2}}
\NewDocumentCommand{\Envlst}{O{}m}{\dc@xlst[#1][Env]{#2}}

\NewDocumentCommand{\Pack}{O{}m}{\dc@x[#1][Pack]{#2}}
\NewDocumentCommand{\Packlst}{O{}m}{\dc@xlst[#1][Pack]{#2}}

\NewDocumentCommand{\Value}{O{}m}{\dc@x[#1][Value]{#2}}
\NewDocumentCommand{\Valuelst}{O{}m}{\dc@xlst[#1][Value]{#2}}

\NewDocumentCommand{\Option}{O{}m}{\dc@x[#1][Option]{#2}}
\NewDocumentCommand{\Optionlst}{O{}m}{\dc@xlst[#1][Option]{#2}}


%%%
%%% In text macro
%%%
\NewDocumentCommand{\Macro}{t!mD<>{}O{}m}{{\MetaFmt[Code]{\expandafter\zap@space\detokenize{#2#3} \@empty}}\IfBooleanTF{#1}{#5}{\gdef\dc@ArgHack{\,}\forcsvlist{\oarg}{#4}\forcsvlist{\marg}{#5}}}%


\NewDocumentCommand{\dc@Describe}{st+O{Key}O{Meta}m}{%
    \IfBooleanTF{#2}%
	    {\item[{\dc@arg+[#3][#4]{}{#5}{}}]}%
		{\item[]~\hspace{-0.6675em}\IfBooleanTF{#1}%
			{\dc@margincode*+[#3]{{\dc@arg+[#3][#4]{}{#5}{}}}}%
			{\dc@margincode+[#3]{{\dc@arg+[#3][#4]{}{#5}{}}}}}%
}%

\NewDocumentCommand{\DescribeArg}{st+O{Key}m}{%
    \IfBooleanTF{#2}%
		    {\dc@Describe+[#3][Meta]{#4}}%
				{\IfBooleanTF{#1}%
				    {\dc@Describe*[#3][Meta]{#4}}%
						{\dc@Describe[#3][Meta]{#4}}}%
}%
\NewDocumentCommand{\DescribeKey}{st+O{Key}m}{%
    \IfBooleanTF{#2}%
		{\dc@Describe+[#3][]{#4}}%
		{\IfBooleanTF{#1}%
			{\dc@Describe*[#3][]{#4}}%
			{\dc@Describe[#3][]{#4}}}%
}%
\let\DescribeValue\DescribeKey
\let\DescribeOption\DescribeKey
\let\DescribePackage\DescribeKey

\NewDocumentCommand{\DescribeMacro}{st!t+mO{}m}{%
    \IfBooleanTF{#3}%
	    {\item[{\Macro{#4}{}}]}%
		{\item[]~\hspace{-0.6675em}\IfBooleanTF{#1}%
			{\dc@margincode*{#4}}%
			{\dc@margincode{#4}}}\IfBooleanTF{#2}{{\Macro!{}[]{#6}}}{{\Macro{}[#5]{#6}}}%
}%

\SetEnumitemKey{marginlst}{leftmargin=0mm,labelindent=0mm,listparindent=0mm,labelwidth=0mm,labelsep=0mm}
\SetEnumitemKey{intextlst}{leftmargin=7.5mm,labelindent=0mm,listparindent=0mm,labelwidth=20mm,labelsep=2.5mm}

\NewDocumentCommand{\dc@margincode}{st+O{Code}m}{\ifvmode\leavevmode\else\fi{\marginpar{\raggedleft\MetaFmt[#3]{\IfBooleanT{#1}{\bfseries}\vspace{-1.35ex}\IfBooleanTF{#2}{#4}{\expandafter\zap@space\detokenize{#4} \@empty}\vspace{0.3ex}}}}}

\NewDocumentEnvironment{Macros}{m}{%
      \ifvmode\vspace{1ex}\leavevmode\else\fi{\marginpar{\vspace*{-1.755ex}~\color{gray}\hrule\vspace{0.3ex}}\forcsvlist\dc@margincode{#1}\marginpar{\vspace{-1.1ex}~\color{gray}\hrule}}%
  }{}
	\NewDocumentEnvironment{Envs}{m}{\begin{Macros}{#1}}{\end{Macros}}%

\NewDocumentEnvironment{Syntax}{}{\begingroup\setlength{\parindent}{0pt}\obeylines\hspace{-0.382em}\dc@csfontsize}{\endgroup}

\NewDocumentEnvironment{Args}{}{\begin{description}[nosep,marginlst]\vspace{0.5ex}}{\end{description}}
\NewDocumentEnvironment{Args+}{}{\begin{description}[nosep,intextlst]\vspace{0.5ex}}{\end{description}}
\NewDocumentEnvironment{Keys}{}{\begin{Args}}{\end{Args}}
\NewDocumentEnvironment{Keys+}{}{\begin{Args+}}{\end{Args+}}
\NewDocumentEnvironment{Values}{}{\begin{Args}}{\end{Args}}
\NewDocumentEnvironment{Values+}{}{\begin{Args+}}{\end{Args+}}
\NewDocumentEnvironment{Options}{}{\begin{Args}}{\end{Args}}
\NewDocumentEnvironment{Options+}{}{\begin{Args+}}{\end{Args+}}


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


\colorlet{dc@ColorCode}{blue!40!black}
\colorlet{dc@ColorMacro}{blue!40!black}
\colorlet{dc@ColorMarg}{gray!60!black}
\colorlet{dc@ColorOarg}{gray!90!black}
\colorlet{dc@ColorParg}{gray!60!black}
\colorlet{dc@ColorXarg}{gray!60!black}
\colorlet{dc@ColorDefaultVal}{blue!60!black}
\colorlet{dc@ColorDefault}{blue!60!black}
\colorlet{dc@ColorKeys}{teal!40!black}
\colorlet{dc@ColorValue}{teal!40!black}
\colorlet{dc@ColorKey}{teal!40!black}
\colorlet{dc@ColorOptions}{green!30!black}
\colorlet{dc@ColorOthers}{green!30!black}
\colorlet{dc@ColorEnv}{green!30!black}
\colorlet{dc@ColorPack}{green!30!black}



\def\dc@metafont{\dc@csfontsize\ttfamily}

\DeclareKeys[MetaFormat]{
    Oarg.usage = general,
	Oarg.code = {\def\dc@Metafmt{\color{dc@ColorOarg}\dc@metafont}},
	%
    Marg.usage = general,
	Marg.code = {\def\dc@Metafmt{\color{dc@ColorMarg}\dc@metafont}},
	%
    Parg.usage = general,
	Parg.code = {\def\dc@Metafmt{\color{dc@ColorParg}\dc@metafont}},
	%
    Xarg.usage = general,
	Xarg.code = {\def\dc@Metafmt{\color{dc@ColorXarg}\dc@metafont}},
	%
    Macro.usage = general,
	Macro.code = {\def\dc@Metafmt{\color{dc@ColorCode}\dc@metafont}},
	%
    Code.usage = general,
	Code.code = {\def\dc@Metafmt{\color{dc@ColorCode}\dc@metafont}},
	%
    Key.usage = general,
	Key.code = {\def\dc@Metafmt{\color{dc@ColorKey}\dc@metafont\slshape}},
	%
    KeyVal.usage = general,
	KeyVal.code = {\def\dc@Metafmt{\color{dc@ColorKeyVal}\dc@metafont\slshape}},
	%
    Option.usage = general,
	Option.code = {\def\dc@Metafmt{\color{dc@ColorOptions}\dc@metafont}},
	%
    Default.usage = general,
	Default.code = {\def\dc@Metafmt{\color{dc@ColorDefault}\dc@metafont}},
	%
    Value.usage = general,
	Value.code = {\def\dc@Metafmt{\color{dc@ColorValue}\dc@metafont\slshape}},
	%
    Env.usage = general,
    Env.code = {\def\dc@Metafmt{\color{dc@ColorEnv}\dc@metafont\slshape}},
	%
    Pack.usage = general,
    Pack.code = {\def\dc@Metafmt{\color{dc@ColorPack}\dc@metafont\slshape}},
	}
\DeclareUnknownKeyHandler[MetaFormat]{\def\dc@Metafmt{\color{dc@ColorOthers}\ttfamily}}

\NewDocumentCommand{\MetaFmt}{sO{}}{\ifblank{#2}{}{\dc@@MetaFmt{#2}}\IfBooleanT{#1}{\bfseries}\dc@Metafmt}
\NewDocumentCommand{\dc@@MetaFmt}{m}{\SetKeys[MetaFormat]{#1}}
\dc@@MetaFmt{Code}


\titleformat{\chapter}{\sffamily\bfseries\selectfont\large}{\thechapter}{2ex}{}
\titleformat{name=\chapter,numberless}{\sffamily\bfseries\selectfont\large\center}{}{0pt}{}
\titlespacing{\chapter}{0pt}{\tc@chapbefore}{\tc@chapafter}


\def\tc@chapbefore{18pt plus 4pt minus 4pt}
\def\tc@chapafter{12pt plus 3pt minus 3pt}
\def\tc@secbefore{12pt plus 3pt minus 3pt}
\def\tc@secafter{8pt plus 2pt minus 2pt}

\titleformat{\section}[hang]{\normalfont\selectfont\large}{\thesection}{.5em}{}
\titlespacing{\section}{0pt}{\tc@secbefore}{\tc@secafter}
\titleformat{\subsection}[hang]{\normalfont\sffamily\slshape\selectfont}{\thesubsection}{.5em}{}

\titleformat{name=\section,numberless}[hang]{\normalfont\large\bfseries\scshape\selectfont\center}{}{0pt}{}
\titlespacing{name=\section,numberless}{0pt}{\tc@secbefore}{\tc@secafter}



\reversemarginpar


\NewDocumentCommand{\dcAuthor}{m}{\def\dc@author{#1}}
\NewDocumentCommand{\dcDate}{m}{\def\dc@date{#1}}
\NewDocumentCommand{\dcTitle}{m}{\def\dc@title{#1}}

\NewDocumentCommand\dcMakeTitle{}{\par
	\begingroup
	\renewcommand\thefootnote{\@fnsymbol\c@footnote}%
	\def\@makefnmark{\rlap{\@textsuperscript{\normalfont\@thefnmark}}}%
	\long\def\@makefntext##1{\parindent 1em\noindent
		\hb@xt@1.8em{%
			\hss\@textsuperscript{\normalfont\@thefnmark}}##1}%
	\if@twocolumn
	\ifnum \col@number=\@ne
	\dc@maketitle
	\else
	\twocolumn[\dc@maketitle]%
	\fi
	\else
	\newpage
	\global\@topnum\z@   % Prevents figures from going at top of page.
	\dc@maketitle
	\fi
	\thispagestyle{plain}\@thanks
	\endgroup
}
\def\dc@maketitle{%
	\newpage
	\null
	\vskip 2em%
	\begin{center}%
		\let \footnote \thanks
		{\LARGE \dc@title \par}%
		\vskip 1.5em%
		{\large
			\lineskip .5em%
			\begin{tabular}[t]{c}%
				\dc@author
			\end{tabular}\par}%
		\vskip 1em%
		{\large \dc@date}%
	\end{center}%
	\par
	\vskip 1.5em}

\NewDocumentEnvironment{dcAbstract}{}{%
	\if@twocolumn
	\section*{\abstractname}%
	\else
	\small
	\begin{center}%
		{\bfseries \abstractname\vspace{-.5em}\vspace{\z@}}%
	\end{center}%
	\quotation
	\fi}{\if@twocolumn\else\endquotation\fi}



\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}

\NewDocumentCommand{\bigtab}{}{
\begin{center}\begin{tabular}
  {>{\def\colunit{pt}}l<{\convertto{\rowunit}{1\colunit}}
   >{\def\colunit{mm}}l<{\convertto{\rowunit}{1\colunit}}
   >{\def\colunit{cm}}l<{\convertto{\rowunit}{1\colunit}}
   >{\def\colunit{ex}}l<{\convertto{\rowunit}{1\colunit}}
   >{\def\colunit{em}}l<{\convertto{\rowunit}{1\colunit}}
   >{\def\colunit{bp}}l<{\convertto{\rowunit}{1\colunit}}
   >{\def\colunit{dd}}l<{\convertto{\rowunit}{1\colunit}}
   >{\def\colunit{pc}}l<{\convertto{\rowunit}{1\colunit}}
%   >{\def\colunit{in}}l<{\convertto{\rowunit}{1\colunit}}
   >{\bfseries}l}
\multicolumn{1}{l}{\bfseries 1pt} & \multicolumn{1}{l}{\bfseries 1mm} & \multicolumn{1}{l}{\bfseries 1cm} & \multicolumn{1}{l}{\bfseries 1ex} & \multicolumn{1}{l}{\bfseries 1em} & \multicolumn{1}{l}{\bfseries 1bp} & \multicolumn{1}{l}{\bfseries 1dd} & \multicolumn{1}{l}{\bfseries 1pc} & \\%\multicolumn{1}{l}{\bfseries 1in} & \\
\gdef\rowunit{pt} & & & & & & & &  \rowunit\\
\gdef\rowunit{mm} & & & & & & & &  \rowunit\\
\gdef\rowunit{cm} & & & & & & & &  \rowunit\\
\gdef\rowunit{ex} & & & & & & & &  \rowunit\\
\gdef\rowunit{em} & & & & & & & & \rowunit\\
\gdef\rowunit{bp} & & & & & & & &  \rowunit\\
\gdef\rowunit{dd} & & & & & & & &  \rowunit\\
\gdef\rowunit{pc} & & & & & & & &  \rowunit\\
%\gdef\rowunit{in} & & & & & & & & & \rowunit\\
\end{tabular}\end{center}
}



\endinput