% Definitions for the package Tcode specific to python. This file acts on top of Tcode.tex %

%The user may have redefined \ThisIsLanguage to ignore its argument. Therefore, in this file \TcodeLang may be py or empty.
\ThisIsLanguage{py}

\ifdefined\TcodepycodeIncluded
  \ReloadLang@py
  \exP\endinput
\fi

\endlinechar=-1 %Do not insert anything at end of lines. Saves as plenty of % and possible bugs in the definitions that follow
\def\expandonce#1{\exP\unexpanded\exP{#1}} %#1 must be a single token
\def\TcodeLang@py{py} %For testing against \TcodeLang in macros.

\newtoks\Tcodepy@Reload@S

\input Tcode_pycode-str.tex %Sets \Tcodepy@Reload@S

%%%%%%%%%*******  Commands common to the inline and displayed environments  *******%%%%%%%%%

%%%   Look for string prefixes and handle them   %%%

% May modify \FormatToApply
\def\CheckForStrpyPrefix{
  \ifcsname StrpyPrefix@\the\WordToPrint\endcsname%If this is defined, it is a string prefix
	\let\ifStrPrefix\iftrue\let\fi\fi
	\letcs\FormatToApply{StrpyPrefix@\the\WordToPrint}
	\letcs\FormatNextString{py@NextString@\the\WordToPrint}
  \fi
}

%Modified from Tcode
\def\PrePrintLongHook@py#1{
  \ifx(#1\exP\OpenParenpyTasks\else
  \ifx"#1\exP\CheckForStrpyPrefix\else
  \ifx'#1\exP\CheckForStrpyPrefix
  \fi\fi\fi
  \ifStrPrefix\StrPrefixTasks
  \else\exP\PrePrintHook\fi
}
\let\PrePrintLongHook\PrePrintLongHook@py

%Words preceding a (
{\xdef\FORMATpyBIT{\csFormat{BuiltInType}}}
\if*\TcodeLang*
  \def\OpenParenpyTasks{
    \ifx\FormatToApply\undefined\@FormatToApply{\FormatpyPreParen}
    \else\ifx\FormatToApply\FORMATpyBIT\def\FormatToApply{\FormatBuiltInFunction}
    \fi\fi
    \show\parsecode
  }
\else
  \def\OpenParenpyTasks{
    \ifx\FormatToApply\undefined\@FormatToApply{\FormatpyPreParen}
    \else\ifx\FormatToApply\FORMATpyBIT\def\FormatToApply{\FormatpyBuiltInFunction}
    \fi\fi
  }
\fi

%%%****   Comments   ****%%%

\def\pyCommentLine{\bgroup\SetuppyComment\pyDoCommentLine}
\def\pyDoCommentLine#1\end{\##1\egroup\@parsecode\end}
\catcode`\^^M=13 %
\def\pyDoCommentLineM#1^^M{\##1\egroup\@parsecode^^M}
\catcode`\^^M=5 %

% Comment setup. Comments appear mostly in displayed mode.
{\xdef\SetuppyCommentNoEsc{\csFormat{Comment}}}
% Add \SetEscapeChar to the setup of Comment lines...
\toks0={\catcode`\{=1 \catcode`\}=2 }
\edef\SetuppyCommentEsc{\expandonce\SetuppyCommentNoEsc\the\toks0 \expandonce\MakeAtEscape}
%... and make it the default.
\let\SetuppyComment\SetuppyCommentEsc

%By default @ acts as escape and { } group. This deactivates that.
\def\CodeNoEscInComments{\let\SetuppyComment\SetuppyCommentNoEsc}
\def\CodeEscInComments{\let\SetuppyComment\SetuppyCommentEsc} %And this activates it
%Names with "py" within, for multilingual documents and users who prefer it that way
\let\CodeNoEscInpyComments\CodeNoEscInComments
\let\CodeEscInpyComments\CodeEscInComments

%%%   Handling of NoLetter's   %%%

\TcodeLetSpecial'=\pyStringSimple
\TcodeLetSpecial"=\pyStringDouble
\TcodeLetSpecial#=\pyCommentLine

%% CodeDefineCommands

%Modified from Tcode
\def\CodeDefineCommands@py{
  \let\pyDoStringS=\CodeParsepyStringSimple
  \let\pyDoStringD=\CodeParsepyStringDouble
  \let\SetupNormalStrpyCodes\relax
  \let\SetupStrpyCodes\SetupNormalStrpyCodes
  \let\pyDoCommentLine\pyDoCommentLineM
}
\let\CodeDefineCommands\CodeDefineCommands@py

%%%%%%%*******  Saving for double inclusion  *******%%%%%%%

\exP\def\exP\ReloadLang@py\exP{
  \the\Tcodepy@Reload@S
  \let\PrePrintLongHook\PrePrintLongHook@py
  \let\CodeDefineCommands\CodeDefineCommands@py
}

\endlinechar=`\^^M % This ' ' prevented TeX from continuing expanding past the end of this file (new the final \let would stop it)

%%%*******  Provide defaults for the ones that are needed  *******%%%

\if*\TcodeLang*
  \unless\ifdefined\FormatString			\let\FormatString\relax\fi
  \unless\ifdefined\FormatStringWide		\def\FormatStringWide{\FormatString}\fi
  \unless\ifdefined\FormatStringRaw		\def\FormatStringRaw{\FormatString}\fi
  \unless\ifdefined\FormatWidePrefix		\protected\def\FormatWidePrefix{\FormatStringWide}\fi
  \unless\ifdefined\FormatRawPrefix		\protected\def\FormatRawPrefix{\FormatStringRaw}\fi
  \unless\ifdefined\FormatFormatInString	\def\FormatFormatInString{\FormatAtEsc}\fi
  \unless\ifdefined\FormatpyPreParen
    \def\FormatpyPreParen{Function}
    \unless\ifdefined\FormatFunction  \let\FormatFunction\relax\fi
  \fi
\else
  \unless\ifdefined\FormatpyString		\let\FormatpyString\relax\fi
  \unless\ifdefined\FormatpyStringWide	\def\FormatpyStringWide{\FormatpyString}\fi
  \unless\ifdefined\FormatpyStringRaw		\def\FormatpyStringRaw{\FormatpyString}\fi
  \unless\ifdefined\FormatpyWidePrefix		\protected\def\FormatpyWidePrefix{\FormatpyStringWide}\fi
  \unless\ifdefined\FormatpyRawPrefix		\protected\def\FormatpyRawPrefix{\FormatpyStringRaw}\fi
  \unless\ifdefined\FormatpyFormatInString	\def\FormatpyFormatInString{\FormatpyAtEsc}\fi
  \unless\ifdefined\FormatpyPreParen
    \def\FormatpyPreParen{Function}
    \unless\ifdefined\FormatpyFunction  \let\FormatpyFunction\relax\fi
  \fi
\fi

%For single language use
\def\FormatPreParen#1{\def\FormatpyPreParen{#1}}

\let\TcodepycodeIncluded\relax
