%run with pdftex or luatex

%This example is intended mostly for package developers. It exhibits the effects of a
% stepwise inclusion of the different layers that compose a Tcode package.

\ifdefined\outputmode
    \outputmode=1
    \input luaotfload.sty
    \input Tcode-examples-otfont.tex
    \input Tcode-examples-OTpalatino.tex
\else
    \input Tcode-examples-palatino.tex
\fi
\unless\ifdefined\theend
  \let\theend\end
  \def\begin#1{\csname #1\endcsname} \def\end#1{\csname end#1\endcsname}
\fi

\ifdefined\sizex\sizex
\else\tenrm\fi
\parindent=0pt

\input Tcode.tex
\TcodeMarker{`}
\input pdfcolor-rg.tex

No format will be applied to the following code becasue only the kernel \ncode{Tcode.tex} has been included.
We define \ncode{\\FormatString} as bold and apply it to the first snippet. We define the format for
numbers as blue. We have activated the \ifdefined\outputmode“dollar”\else``dollar''\fi, so identifiers marked with it will appear in italics.
\TcodeDefineFormat{Number}{\color{blue}}

\def\FormatString{\bf}

Some code: \Formatcode\FormatString{int32_t a=30;} \ident{float f}. \ncode{false || b!=3}

\begin{Code}
uint`N_t(X) "short string" u"Wide string"
0b11'10'11'01 // binary C number
'1'2 /* character literal @{'1'} followed by integer literal @2 */
sqrtf((float)2.0);
\end{Code}

\def\ThisIsLanguage#1{} %No "C" infix. Only do this if you know what you are doing
\input Tutils-removelang.tex % And if so, we can input this (just some optimization, no change in the output)
\input Tcode_Ccode.tex
\TcodeDefineFormat{Comment}{\colorraw{0.5 0.2 0.2}}

We have added now the C language package, but no syntax highlighting definitions. We define the format
for comments as greysh red. Note that now the numbers are recognized properly and strings and character
literals get the format "String" that we defined previously.

\Code
uint`N_t(X) "short string" u"Wide string"
0b11'10'11'01 // binary number
'1'2 /* character literal @{'1'} followed by integer literal @2 */
sqrtf((float)2.0);
\endCode

\input Chighlighting_iso.tex
\input Cwords.tex
\def\FormatAtEsc{\Cbold}

Now we have input a syntax highlighting definitions file and further defined the escape within comments
as bold.

\Code
uint`N_t(X) "short string" u"Wide string"
0b11'10'11'01 // binary C number
'1'2 /* character literal @{'1'} followed by integer literal @2 */
sqrtf((float)2.0);
\endCode

Now we specify that pp-directives should have a blue background.

{
\protected\def\FormatPPDirective#1{\Clight\setbox1\hbox{#1}\hbox to 0pt{\PdfColorRaw{0.8 0.9 1}\vrule height10pt width\wd1\hss}#1}
\begin{Code}
	#define hash_hash # ## #
#	define STR(a) # a
  #    define in_between(a) STR(a)
	#define NULL(c, d) in_between(c hash_hash d)
#undef in_between
	
#if __has_include(«\FormatString <optional.h>»)
in_between(a)
#	include <optional.h>
#embed <bitmap.bmp>
#if 1<2 && 2>3
#??
\end{Code}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
\catcode`@=11
\def\f@size{10}
\input fswitch_bera09.tex
\let\DoDollar\itlight
\def\SelectCodeFont{\Clight}

Now we have input a typewriter font package optimzed for use alongside Tcode.

{\MakeWord{uint}{Typedef}
\begin{Code}\def\gee{$\ge$}
for(uint j=i--; j;){
	double auxb=aux**pN;
	double *pB=pN;
	uint k=j--;  //Here we know @j is \gee\ 1
	do{k--; *pNb++ += auxb**pB++;} while(k>=1);
	*pN++=auxb;
}
\end{Code}
}

For the next snippet we say that comments should be typeset in regular italics, and escaped
text within coments in tt-italics.

{\def\f@size{9}
\DefColor{green2}{0.1, 0.4, 0.1}
\def\FormatComment{\nineit\PdfFill{green2}}
\def\FormatAtEsc{\itbold}

\begin{Code}
x=x*x;    // The square of a number
x=i++;    // equivalent to @{x = i, i++;}
a= ~0;    /* Set @a to all bits one */
\end{Code}
}

Set a thick frame and place an empty snippet:
\CodeFramewd=2pt
\begin{Code}
\end{Code}

\DefColor{gray95}{.95, .95, .95}
\CsnippetColor
\def\CsnippetBkg{gray95}
\CodeFramewd=0pt

\def\HandleFileInclude#1{{\FormatString\Cbold <#1>}}
%The following makes the name boxed
%\def\HandleFileInclude#1{\vbox{\hbox{\offinterlineskip\vrule%
%  \vbox{\hrule\kern2pt\setbox0\hbox{\kern2pt\Clight #1\kern2pt}\dimen0=\dp0\box0 \kern-\dimen0\kern2pt\hrule}%
%\vrule}\kern-2.4pt}}

Now we set no frame and a wider right clearance. We also make file names for \ncode{#include}
directives within {\tt <>} appear bold. But those enclosed in {\tt ""} remain unchanged.
This also shows how the C package distinguishes \ncode{#} marks that introduce a directive from
those that do not.

{\CodeRightInd=30pt
\begin{Code}
	#define hash_hash # ## #
#	define STR(a) # a
  #    define in_between(a) STR(a)

#if __has_include(«\FormatString <optional.h>»)
#	include <optional.h>
#endif

#include "definitions.h"
#embed "bitmap.bmp"
\end{Code}}

Line splicing.

\CsnippetNoColor
\CodeLeftInd=20pt
\begin{Code}
u"\
"
u8"\"\
a
line three"
"\
"
a = b/c; // Normal comment
d // multiline\ 
    comment
e /*
k\

j\"
*/
\end{Code}

\def\PPLineHook{\let\Clight\itlight \let\Cbold\itbold}
\MakeWord{func}{Function}

%Do not break this block of code
\begin{Code}\CNolinebreaks
//Formatted pp-lines
#  define in_between(a) STR(a)
void func(int n);
#include "common.h"
\end{Code}

\theend
