% This file is only intended to be input by Tcode_pycode.tex

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

%Have these defined to anything. To check whether a word is any of L, u, u8 or U.
%We take advantage and define them to the format we want for each case.
%local-global in oder to avoid \csFormat defining anything
{
\xdef\FormatStrpyDefault{\csFormat{String}} %The default, for strings without prefix
\xdef\StrpyPrefix@u{\csFormat{WidePrefix}}
\xdef\StrpyPrefix@U{\csFormat{WidePrefix}}
\xdef\StrpyPrefix@r{\csFormat{RawPrefix}}
\xdef\StrpyPrefix@R{\csFormat{RawPrefix}}
\xdef\StrpyPrefix@f{\csFormat{FormattedPrefix}}
%Define the format of the string corresponding with each prefix
\xdef\py@NextString@u{\csFormat{StringWide}}
\xdef\py@NextString@U{\csFormat{StringWide}}
\xdef\py@NextString@r{\csFormat{StringRaw}}
\xdef\py@NextString@R{\csFormat{StringRaw}}
\xdef\py@NextString@f{\csFormat{String}}
}

\Tcodepy@Reload@S{
  \let\FormatNextString\FormatStrpyDefault
  \let\ifStrPrefix\iffalse

  \def\StrPrefixTasks{\let\ifStrPrefix\iffalse\exP\PrePrintStrPrefixHook} %Put things here so that inside the hook there is just one token
  \def\PrePrintStrPrefixHook{
    \if f\the\WordToPrint\let\SetupStrpyCodes\SetupFormattedpyString\fi
    \PrePrintHook
  }
}
\the\Tcodepy@Reload@S

% String setup and { } escape.
\def\SetupNormalStrpyCodes{\catcode`\{=12 \catcode`\}=12 }
\def\restoreSetupStrpyCodes{\let\SetupStrpyCodes\SetupNormalStrpyCodes}
\restoreSetupStrpyCodes

{
\catcode`\{=12 \catcode`\}=12 \catcode`\[=1 \catcode`\]=2
\xdef\temp#1}[[\csFormat[FormatInString]{#1}]] % Consume the argument up to, and including, }
%We do not allow nested {}. It could be done by making { and } modify a counter and resetting the format
] % to \FormatString when a } puts the counter to zero.
\let\OpenBraceInpyString\temp
{
\catcode`\{=13 \catcode`\[=1
\gdef\temp[\aftergroup\restoreSetupStrpyCodes %Restore the normal behaviour
   \catcode`{=\active \let{=\OpenBraceInpyString \catcode`\}=\cat@xii}
}
\let\SetupFormattedpyString\temp

% Strings
\def\pyStringSimple{\bgroup\SetupStrpyCodes\pyDoStringS}
\def\pyStringDouble{\bgroup\SetupStrpyCodes\pyDoStringD}
\def\pyDoStringS#1'{\SetEscapeChars\FormatNextString '#1'\egroup\let\FormatNextString\FormatStrpyDefault\@parsecode}
\def\pyDoStringD#1"{\SetEscapeChars\FormatNextString "#1"\egroup\let\FormatNextString\FormatStrpyDefault\@parsecode}

%%%   Strings in Code  %%%

\catcode`\^^M=13 %

% Typically, a ^^M or \^^M does not appear inside a string. For this reason we defer the somewhat long setup for
%the hypothetical next line til we meet an actual ^^M or \^^M. The last ^^M here will use the original definition, and the other gets forgotten
\def\EOLinpyString{\xdef\CContinueInTheMiddle{\expandonce\SetupStrpyCodes\expandonce\FormatNextString}\egroup\bgroup^^M}
\def\CodeParsepyStringSimple#1'{\let^^M\EOLinpyString\FormatNextString '#1'\egroup\global\let\FormatNextString\FormatStrpyDefault\@parsecode}
\def\CodeParsepyStringDouble#1"{\let^^M\EOLinpyString\FormatNextString "#1"\egroup\global\let\FormatNextString\FormatStrpyDefault\@parsecode}

\catcode`\^^M=5 %
