%D \enableregime[il1]
%D \module
%D	[file=t-jtex,
%D	version=2006.0,
%D	title=\CONTEXT\ User Module,
%D	subtitle=Miscellaneous Macros,
%D	author=Jyrki Ruuskanen,
%D	date=\currentdate,
%D	copyright=Jyrki Ruuskanen]

\writestatus{loading}{J's TeX Extensions}

\unprotect

% add \currentsecondlanguage variable
\def\secondlanguage[#1]{\def\currentsecondlanguage{#1}}

%%% handle multilingual texts (and uppercase)
% usage: \setText[lang]{key}{value}
\def\setText{\futurelet\testchar\maybeLangSetText}
\def\maybeLangSetText%
	{\ifx[\testchar \let\next\langSetText
	\else \let\next\defaultSetText \fi \next}
\def\langSetText[#1]#2#3{\def\name{#2@#1}\def\bigname{#2@#1@uppercase}
	\expandafter\def\csname \name \endcsname{#3}
	\expandafter\def\csname \bigname \endcsname{\uppercase{#3}}}
\def\defaultSetText#1#2{\langSetText[\currentmainlanguage]{#1}{#2}}

% usage: \getText{key}
\def\langGetText[#1]#2{\def\name{#2@#1}%
	\expandafter\ifx\csname \name \endcsname\relax
	\name missing!\else\csname \name \endcsname\fi}
\def\getText#1{\def\name{#1@\currentlanguage}%
	\expandafter\ifx\csname \name \endcsname\relax
	\langGetText[\currentmainlanguage]{#1}
	\else\langGetText[\currentlanguage]{#1}\fi}

% usage: \getUppercaseText{key}
\def\langGetUppercaseText[#1]#2{\def\name{#2@#1@uppercase}%
	\expandafter\ifx\csname \name \endcsname\relax
	\name missing!\else\csname \name \endcsname\fi}
\def\getUppercaseText#1{\def\name{#1@\currentlanguage @uppercase}%
	\expandafter\ifx\csname \name \endcsname\relax
	\langGetUppercaseText[\currentmainlanguage]{#1}
	\else\langGetUppercaseText[\currentlanguage]{#1}\fi}

% usage: \startTextBuffer[lang]{key}...
\def\startTextBuffer{\futurelet\testchar\maybeLangStartTextBuffer}
\def\maybeLangStartTextBuffer%
	{\ifx[\testchar \let\next\langStartTextBuffer
	\else \let\next\defaultStartTextBuffer \fi \next}
\def\langStartTextBuffer[#1]#2{\startbuffer[#2@#1]}
\def\defaultStartTextBuffer#1{\langStartTextBuffer[\currentmainlanguage]{#1}}

% NOTE doesn't work, use \stopbuffer for now
% usage: ...\stopTextBuffer
\let\stopTextBuffer=\stopbuffer

% usage: \getTextBuffer{key}
\def\getTextBuffer#1{\getbuffer[#1@\currentlanguage]}

%%% handle abbreviations
% usage: \setAbbreviation[key]{abbr}{meaning}{description}
\newtoks\usedAbbreviations
\newtoks\abbreviations
\def\setAbbreviation{\futurelet\testchar\maybeOptSetAbbreviation}
\def\maybeOptSetAbbreviation%
	{\ifx[\testchar \let\next\optSetAbbreviation
	\else \let\next\defaultSetAbbreviation \fi \next}
\def\optSetAbbreviation[#1]#2#3#4%
	{\addToList[\abbreviations]{#1}
	\expandafter\def\csname#1\endcsname%
		{#2\writeutilitycommand{\addToList[\string\usedAbbreviations]{#1}}}
	\setAsKeyAndValue[meaning]{#3}{#1}
	\setAsKeyAndValue[description]{#4}{#1}}
\def\defaultSetAbbreviation#1#2#3{\optSetAbbreviation[#1]{#1}{#2}{#3}}

% usage: \getMeaning{key}
\def\getMeaning#1{\def\meaning{#1@meaning}%
	\csname \meaning \endcsname}
\let\infull=\getMeaning

% usage: \getDescription{key}
\def\getDescription#1{\def\description{#1@description}%
	\csname \description \endcsname}

% usage: \printAbbreviation{key}
\def\printAbbreviation#1%
	{{\bf\csname#1\endcsname} (\getMeaning{#1}). \getDescription{#1}\par}

%%% handle references
% usage: \setReference[key]{cite}{description}
\newtoks\usedReferences
\newtoks\references
\def\setReference[#1]#2#3{\def\citename{#1@cite}%
	\addToList[\references]{#1}
	\expandafter\def\csname\citename\endcsname%
		{#2\writeutilitycommand{\addToList[\string\usedReferences]{#1}}}
	\setAsKeyAndValue[description]{#3}{#1}}
	
% usage: \cite[key]
\def\cite[#1]{\def\citename{#1@cite}%
	\csname \citename \endcsname}

% usage: \printReference{key}
\def\printReference#1%
	{\getDescription{#1}\par}

%%% token list operations
% usage: \addToList[\list]{value}
\def\addToList[#1]#2{\let\\=\relax
	\edef\act{\noexpand#1={\the#1 \\{#2}}}%
	\act}

% usage: \executeList[\list]{\command#1}
\def\executeList[#1]#2{\def\\##1{#2{##1}}%
	\the#1}

% usage: \executeListIntersection[\listone][\listtwo]{\command#1}
\def\executeOnMatch#1#2{\def\comparison{#2@comparison}%
	\expandafter\ifx\csname \comparison \endcsname\relax
	\else#1{#2} \fi}
\def\executeListIntersection[#1][#2]#3%
	{\executeList[#2]{\setAsKeyAndValue[comparison]{true}}
	\executeList[#1]{\executeOnMatch{#3}}}

% usage: \addToAssociative[\list]{value}{key}
\def\addToAssociative[#1]#2#3{\let\\=\relax
	\edef\act{\noexpand#1={\the#1 \\{#2}{#3}}}%
	\act}

% usage: \executeAssociative[\list]{\command#1#2}
\def\executeAssociative[#1]#2{\def\\##1##2{#2{##1}{##2}}%
	\the#1}

% usage: \setAsKeyAndValue[suffix]{value}{key}
\def\setAsKeyAndValue{\futurelet\testchar\maybeSetAsKeyAndValueWithSuffix}
\def\maybeSetAsKeyAndValueWithSuffix%
	{\ifx[\testchar \let\next\setAsKeyAndValueWithSuffix
	\else \let\next\setAsKeyAndValueWithoutSuffix \fi \next}
\def\setAsKeyAndValueWithSuffix[#1]#2#3{\def\name{#3@#1}%
	\expandafter\def\csname \name \endcsname{#2}}
\def\setAsKeyAndValueWithoutSuffix#1#2{\def\name{#2}%
	\expandafter\def\csname \name \endcsname{#1}}

% usage: \getFromAssociative[\list]{key}
\def\getFromAssociative[#1]#2{\def\name{#2@temp}%
	\executeAssociative[#1]{\setAsKeyAndValue[temp]}
	\csname \name \endcsname}

\protect
