%This language support file for julia for the package Tcode is incomplete.

\newcount\catcodebeforejulia \catcodebeforejulia=\catcode`@

\input Tcode.tex %The core
\ThisIsLanguage{julia} %Only needed if julia is mixed with other languages in the same document
\input Tutils-fastfonts-2.tex %In case this is used with Latex, for considerably faster font switch in the snippets. Comment this out
					%if you don't like it. We use the "2" version because we are using also bold (\Cbold below).

%%---------------------------------------------------------------------------------------------

\newtoks\Tjulia@Reload@T
\Tjulia@Reload@T{%
  \def\OtherCharsInIdent{\catcode`@=11 }%Macros begin with @.
  \def\NumberCatcodes{\catcode`.=\cat@xi} %Keep _ as letter. Tcode's default redefines it here to nonletter
}

\DeclareLineComment{#}
\DeclareBlockComment{#=}{=#}

\TcodeDefineSpecial){)\julia@check@transpose}
\TcodeDefineSpecial]{]\julia@check@transpose}
%If one or more ' follow a ) or ], print them and continue. (They do not open strings)
\def\julia@check@transpose#1{\ifx#1''\exP\julia@check@transpose\else\def\next{\@parsecode#1}\exP\next\fi}

%Do not use a simple \DeclareStringLike{'}{CharLiteral}. ''' must work
\TcodeDefineSpecial'#1{\bgroup\useFormat{CharLiteral}'#1\julia@close@CharLit}
\def\julia@close@CharLit#1'{#1'\egroup\@parsecode}
% Strings
\def\julia@StringDouble{\bgroup\SetEscapeChars\MultiLineFormat{String}\futurelet\firstchar\julia@DoStringD} %If the first char is ", we'll check for a third ".
\def\julia@EndString{\egroup\@parsecode}
\def\julia@DoStringD#1"{"#1"\ifx\firstchar"\exP\julia@CheckThreeD \else \exP\julia@EndString\fi}
\def\julia@CheckThreeD#1{\ifx"#1\exP\julia@DoStrDThree\else\exP\julia@EndString\fi #1}
\def\julia@DoStrDThree"#1"""{"#1"""\julia@EndString}

\TcodeLetSpecial"=\julia@StringDouble
% End of strings

\def\testjuliamacro#1@#2\if{\def\temp{#1}\ifx\temp\empty}
%Recognise @macros and functions(
\def\PrePrintLongHook@julia#1{%
  \ifx\FormatToApply\undefined
    \exP\testjuliamacro\the\WordToPrint @\if
      \@FormatToApply{Macro}%
    \else\ifx(#1\@FormatToApply{Function}%
    \fi\fi
  \fi
  \PrePrintHook
}

%Allow a ! within names, but only if it is not the first character
\def\Ncode@Word@julia{\catcode`!=\cat@xi \NcodeParseWord@kernel}

%Strip a ! closing a word if an = follows immediately

\def\EndWord@julia@normal#1{%
  \HookAndPrintWord{#1}%May modify \parsecode
  \exP\parsecode\exP #1%First expand \fi, then \parsecode #1
}
\def\EndWord@julia@excl#1{%Seldom happens
  \WordToPrint\exP{\temp}%\temp has been saved by \testjuliaexcl below
  \HookAndPrintWord=%We (just) need the @macro check from \PrePrintLongHook@julia
  \let\EndWord@julia@\EndWord@julia@normal
  \exP\parsecode\exP!\exP #1%First expand \fi, then \parsecode!#1. The reinstated ! has cat 12.
}
\let\EndWord@julia@\EndWord@julia@normal

{\catcode`\!=11
%Here, #2 may be either empty or !\relax
\gdef\testjuliaexcl#1!\relax#2\if{\if !#2\def\temp{#1}}%\temp keeps the name without the final !

\gdef\NcodeEndWord@julia#1{%
  \let\parsecode\julia@check@transpose
  \catcode`!=\cat@xii
  \ifx#1=\exP\testjuliaexcl\the\WordToPrint\relax !\relax\if %If the next char is =, strip a closing ! from the name
     \let\EndWord@julia@\EndWord@julia@excl
  \fi\fi
  \EndWord@julia@#1%
}
}

\def\ReloadLang@julia{%Only needed if several languages are used in the same document and changes are not local
  \the\Tjulia@Reload@T
  \let\PrePrintLongHook\PrePrintLongHook@julia
  \let\NcodeParseWord\Ncode@Word@julia
  \let\NcodeEndWord\NcodeEndWord@julia
}
\ReloadLang@julia %But we must execute those \let here, of course.

\catcode`@=\catcodebeforejulia

\unless\ifdefined\color
   \input pdfcolor-rg.tex %Comment out if you prefer to include a colour package later
\fi

%%---------------------------------------------------------------------------------------------

%\input juliahighlighting_1.tex %Rather than writing the definitions directly here

\input highlighting_common.tex %Selects \DefineColor according to what colour package (or none) has been loaded

\DefineColor{julia-comment}{0.4, 0.52, 0.4} %We name colours like this in order to avoid clashes with other language packages
\DefineColor{julia-module}{0.3, 0.06, 0}
\DefineColor{julia-type}{0, 0, 0.5}
\DefineColor{julia-constant}{0.2, 0.2, 1}
\DefineColor{julia-function}{0.45, 0.1, 0.6}
\DefineColor{julia-macro}{0.34, 0.07, 0.45}

%Assume red is already defined
\TcodeDefineFormat{Comment}{\color{julia-comment}}
\TcodeDefineFormat{String}{\color{red}}
\TcodeDefineFormat{CharLiteral}{\color{red}}
\TcodeDefineFormat{Keyword}{\Cbold}
\TcodeDefineFormat{Module}{\color{julia-module}}
\TcodeDefineFormat{Type}{\color{julia-type}}
\TcodeDefineFormat{NamedConstant}{\color{julia-constant}}
\TcodeDefineFormat{Function}{\color{julia-function}}
\TcodeDefineFormat{Macro}{\Cbold\color{julia-macro}}
\TcodeDefineSubcategory{Exception}{Type}

%Now define \Cbold
\unless\ifdefined\Cbold % If we are in Latex, Tutils-fastfonts-2.tex above already defines it.
  \ifdefined\bfseries \let\Cbold\bfseries
  \else \let\Cbold\bf
  \fi
\fi

%%---------------------------------------------------------------------------------------------

\input juliawords.tex
