%D \module
%D   [       file=enco-ini,
%D        version=2007.02.19, % 2000.12.27, % 1998.12.03,
%D          title=\CONTEXT\ Encoding Macros,
%D       subtitle=Initialization,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This is stripped down version of the original enco-ini.tex file. For more details
%D you might want to study the \MKII\ file but since \LUATEX\ is unicode inside we
%D need less code. When we moved to \LMTX\ again some disappeared and the composition
%D macros were upgraded in the process.
%D
%D An historic note: When dealing with characters we have four cases to take into
%D account when moving from \MKII\ to \MKIV:
%D
%D \starttabulate[|l|l|]
%D     \NC \type {<byte 200>}   \NC ref to slot 200 in current font \NC \NR
%D     \NC \type {\char 200}    \NC ref to slot 200 in current font \NC \NR
%D     \NC \type {<active 200>} \NC can (e.g.) map to another slot in current font \NC \NR
%D     \NC \type {\namedglyph}  \NC can map to some slot in some font \NC \NR
%D \stoptabulate
%D
%D Using case 2 for special characters is doomed to fail because we are not going to
%D intercept these on the fly as happens automatically with traditional font
%D encoding handling. We could do that in a node pass but it's not worth the effort
%D because we seldom use this case in a document source.

\writestatus{loading}{ConTeXt Encoding Macros / Initialization}

\unprotect

%D This one is obsolete but maybe sometimes still used in styles:

\aliased\let\defaultencoding\s!default

%D \macros
%D   {defineaccent, definecharacter, definecommand}
%D
%D Some of these are used at the \LUA\ end but the names will change.

\installcorenamespace{accents}

%D These might go away:

\permanent\protected\def\defineaccent#1 #2 #3 %
  {\dodefineaccentcommand{#1}%
   \dodefineaccent{#1}{#2}{#3}}

\permanent\protected\def\dodefineaccentcommand#1%
  {\ifcsname\string#1\endcsname\else
     \edefcsname\string#1\endcsname{\noexpand\dohandleaccent{\string#1}}%
   \fi}

\permanent\protected\def\dodefineaccent#1#2#3% no spaces, used low level
  {\defcsname\??accents\string#1\string#2\empty\endcsname{#3}}

\permanent\protected\def\dohandleaccent#1#2% expandable because we want them in the tuc file
  {\csname\??accents
     \ifcsname\??accents\string#1#2\empty\endcsname
       \string#1#2\empty
     \orelse\ifcsname\??accents\string#1\string#2\empty\endcsname
       \string#1\string#2\empty
     \else
       \endcsname#2\csname\??accents % very dirty trick: ignore accent but keep char
     \fi
   \endcsname}

\aliased\letcsname\??accents\endcsname\empty

%D Because now have this (\type {\chr} issues a one|-|time warning):
%D
%D \stopbuffer
%D \chr {e acute}
%D \chr {i circumflex}
%D \^{i} \^i \^{\i}
%D \^{a} \"{a}
%D \withcircumflex{a}
%D \withdiaeresis{a}
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\permanent\protected\def\dodefinecombine#1#2%
  {\ifcsname\string#1\endcsname\else
     \permanent\protected\defcsname with#1\endcsname##1{\chr{##1#2}}%
   \fi}

%D The following dirty trick is needed to catch \asciimath{\^{1/5}log}, although:
%D do we?

\permanent\protected\def\definecharacter#1 #2 %
  {\doifelsenumber{\string#2}
     {\edefcsname\string#1\endcsname{\tocharacter#2}}
     {\protected\defcsname\string#1\endcsname{#2}}}

%D specials: \aa \ae \cc \i \ij \l \o \oe \sz \par
%D SPECIALS: \AA \AE \CC \j \IJ \L \O \OE \SZ \par

\permanent\protected\def\definecommand#1 #2 %
  {\protected\defcsname\string#1\endcsname{#2}}

\permanent\protected\def\dodefinecommand#1#2% \O \L \AE ...
  {% not permanent as these are kind of obsolete
   \frozen\protected\defcsname\string#1\endcsname{#2}}

%D We set up some things in \LUA:

\clf_defineaccents % one time

%D This defined commands like \type {\withgrave} and \type {\withcaronbelow} and
%D some of these have aliases. Below ee also alias the traditional one character
%D accent commands.
%D
%D \starttabulate[||||]
%D     \NC \getvalue{agrave}        \NC \withgrave       {a} \NC \`{a} \NC \NR
%D     \NC \getvalue{aacute}        \NC \withacute       {a} \NC \'{a} \NC \NR
%D     \NC \getvalue{acircumflex}   \NC \withcircumflex  {a} \NC \^{a} \NC \NR
%D     \NC \getvalue{atilde}        \NC \withtilde       {a} \NC \~{a} \NC \NR
%D     \NC \getvalue{amacron}       \NC \withmacron      {a} \NC \={a} \NC \NR
%D     \NC \getvalue{ebreve}        \NC \withbreve       {e} \NC \u{e} \NC \NR
%D     \NC \getvalue{cdotaccent}    \NC \withdot         {c} \NC \.{c} \NC \NR
%D     \NC \getvalue{ediaeresis}    \NC \withdieresis    {e} \NC \"{e} \NC \NR
%D     \NC \getvalue{uring}         \NC \withring        {u} \NC \r{u} \NC \NR
%D     \NC \getvalue{uhungarumlaut} \NC \withhungarumlaut{u} \NC \H{u} \NC \NR
%D     \NC \getvalue{ecaron}        \NC \withcaron       {e} \NC \v{e} \NC \NR
%D     \NC \getvalue{ecedilla}      \NC \withcedilla     {e} \NC \c{e} \NC \NR
%D     \NC \getvalue{eogonek}       \NC \withogonek      {e} \NC \k{e} \NC \NR
%D \stoptabulate
%D
%D Now we alias these traditional shortcuts:

\pushoverloadmode

\suggestedalias \` \withgrave
\suggestedalias \' \withacute
\suggestedalias \^ \withcircumflex
\suggestedalias \~ \withtilde
\suggestedalias \= \withmacron
\suggestedalias \u \withbreve
\suggestedalias \. \withdot
\suggestedalias \" \withdieresis
\suggestedalias \r \withring
\suggestedalias \H \withdoubleacute
\suggestedalias \v \withcaron
\suggestedalias \c \withcedilla
\suggestedalias \k \withogonek
\suggestedalias \d \withdotbelow

\popoverloadmode

% From \type {enco-com} we had these, maybe that's better:

% \aliased\let\AE\AEligature   \aliased\let\ae\aeligature
% \aliased\let\OE\OEligature   \aliased\let\oe\oeligature
% \aliased\let\IJ\IJligature   \aliased\let\ij\ijligature
% \aliased\let\AA\textAngstrom \aliased\let\aa\aring
% \aliased\let\CC\Ccedilla     \aliased\let\cc\ccedilla
% \aliased\let\L \Lslash       \aliased\let\l \lslash
% \aliased\let\O \Oslash       \aliased\let\o \oslash
% \aliased\let\SZ\Eszett       \aliased\let\sz\eszett    % \aliased\let\SS\Ssharp

% %D \macros
% %D   {everyuppercase, everylowercase, everysanitize}
%
% \newtoks \everyuppercase
% \newtoks \everylowercase
% \newtoks \everysanitize

%D Accent handling (try to avoid this):

% \buildtextaccent\greekdasia\greekalphamacron
% \buildtextaccent\textacute q

%D We can use offsets in \LMTX\ but even that makes no sense because we have
%D a virtual feature already in \MKIV. These builders are depricated:

\newbox\b_enco_accent

% \permanent\protected\def\buildmathaccent#1% Don't use this!
%   {\mathaccent#1 }

\permanent\protected\def\buildtextaccent#1#2% we could do all at the lua end
  {\dontleavehmode\begingroup               % but that's no fun (yet)
   \setbox\scratchboxone\hbox{#1}% accent
   \setbox\scratchboxtwo\hbox{#2}% character
   \scratchheight{\ht\scratchboxtwo-\ht\scratchboxone}%
   \scratchdepth {\dp\scratchboxtwo-\dp\scratchboxone}%
   \scratchwidth \wd\scratchboxtwo
   \hbox to \wd\ifdim\wd\scratchboxone>\wd\scratchboxtwo\scratchboxone\else\scratchboxtwo\fi\bgroup
     \hss\box\scratchboxtwo\hss
     \hskip-\scratchwidth
     \hss
     \ifdim\ht\scratchboxone>\exheight
       % top accent
       \raise{\scratchheight+\exheight/3}%
     \else
       \lower{-\scratchdepth-\exheight/3}%
     \fi
     \box\scratchboxone
     \hss
   \egroup
   \endgroup}

\permanent\protected\def\bottomaccent#1#2#3#4#5% down right slantcorrection accent char
  {\dontleavehmode % why this align mess
   \vtop
     {\forgetall
      \baselineskip\zeroskip
      \lineskip#1\relax
      \everycr\emptytoks
      \tabskip\zeroskip
      \lineskiplimit\zeropoint
      \setbox0\hbox{#4}%
      \halign
        {##\crcr\hbox{#5}\crcr
         \hskip\hideskip % \hidewidth
         \hskip#2\wd0
         \hskip-#3\slantperpoint % in plain 1ex * dimenless value
         \vpack to .2\exheight{\box0\vss}%
         \hskip\hideskip % \hidewidth
         \crcr}}}

\permanent\protected\def\buildtextmacron     {\bottomaccent{.25ex}{0}{15}{\textmacron}}
\permanent\protected\def\buildtextbottomdot  {\bottomaccent{.25ex}{0}{5}{\textbottomdot}}
\permanent\protected\def\buildtextcedilla    {\bottomaccent{0ex}{0}{5}{\textcedilla}}
\permanent\protected\def\buildtextogonek     {\bottomaccent{-.1ex}{.5}{0}{\textogonek}}
\permanent\protected\def\buildtextbottomcomma{\bottomaccent{.15ex}{0}{5}{\tx,}}

\permanent\protected\def\topaccent#1#2#3#4#5% down right slantcorrection accent char
  {\dontleavehmode
   \bgroup
     \setbox0\hbox{#4}%
     \setbox2\hbox{#5}%
     \hbox to \wd2 \bgroup
        \hss\copy2\hss
        \hskip-\wd2
        \hss\hskip#2\wd0\hskip-#3\slantperpoint\raise#1\hbox{#4}\hss
     \egroup
   \egroup}

\permanent\protected\def\buildtextgrave
  {\topaccent{0pt}{0}{15}{\textgrave}} % e.g.

\permanent\protected\def\definemathaccent#1 #2%
  {\defcsname#1\endcsname{\mathaccent#2 }} % can be redefined

% from enco-def:

% \aliased\let\i\dotlessi
% \aliased\let\j\dotlessj

% \aliased\let\P\paragraphmark \aliased\let\textP\paragraphmark % obsolete (surfaced in bibliographic files)
% \aliased\let\S\sectionmark   \aliased\let\textS\sectionmark   % obsolete (surfaced in bibliographic files)

\permanent\cdef\eszett  {ß} \permanent\cdef\Eszett  {SS} \aliased\let\Ssharp\Eszett
\permanent\cdef\lslash  {ł} \permanent\cdef\Lslash  {Ł}
\permanent\cdef\dslash  {đ} \permanent\cdef\Dslash  {Đ}
%permanent\cdef\oslash  {ø} %permanent\cdef\Oslash  {Ø} % clashes with math: use \Ostroke
\permanent\cdef\dcroat  {đ} \permanent\cdef\Dcroat  {Đ}
\permanent\cdef\kcedilla{ķ} \permanent\cdef\Kcedilla{Ķ}
\permanent\cdef\lcedilla{ļ} \permanent\cdef\Lcedilla{Ļ}
\permanent\cdef\ncedilla{ņ} \permanent\cdef\Ncedilla{Ņ}
\permanent\cdef\rcedilla{ŗ} \permanent\cdef\Rcedilla{Ŗ}
\permanent\cdef\aumlaut {ä} \permanent\cdef\Aumlaut {Ä}
\permanent\cdef\eumlaut {ë} \permanent\cdef\Eumlaut {Ë}
\permanent\cdef\iumlaut {ï} \permanent\cdef\Iumlaut {Ï}
\permanent\cdef\oumlaut {ö} \permanent\cdef\Oumlaut {Ö}
\permanent\cdef\uumlaut {ü} \permanent\cdef\Uumlaut {Ü}

%D For old times sake we keep these (obsolete):

\permanent\cdef\textflorin{ƒ} \permanent\cdef\florin  {ƒ}
\permanent\cdef\pound     {£} \permanent\cdef\sterling{£}
\permanent\cdef\promille  {‰} \permanent\cdef\permille{‰}

%D These are kind of \TEX\ specific

\pushoverloadmode

\permanent\protected\def\ampersand{\mathortext\mathampersand\textampersand}

\aliased\let\percent\textpercent
\aliased\let\procent\textpercent
\aliased\let\dollar \textdollar
\aliased\let\hash   \texthash

\popoverloadmode

%D some more: what with freezing here?

\ifdefined\softhyphen \else
    \aliased\let\softhyphen\explicitdiscretionary
\fi

%D The softhyhen is never used but we keep the definition below:

\aliased\let\hyphen \softhyphen % never used

% But we ditch these:

% \def\hyphen           {\softhyphen} % never used
% \def\compoundwordmark {\hyphen}
% \def\cwm              {\hyphen}
% \def\nonbreakinghyphen{\hyphen}
% \def\breakinghyphen   {\hyphen\prewordbreak}

%D Quotes \unknown\ we keep these funny names because they indicate where we come
%D from \unknown\ long long ago when we had no real useful names for them. Some
%D ancient styles might still use them.

\aliased\let\lowerleftsingleninequote \quotesinglebase % ‚
\aliased\let\lowerleftdoubleninequote \quotedblbase    % „
\aliased\let\lowerrightsingleninequote\quotesinglebase % ‚
\aliased\let\lowerrightdoubleninequote\quotedblbase    % „

\aliased\let\upperleftsingleninequote \quoteright      % ’
\aliased\let\upperleftdoubleninequote \quotedblright   % ”
\aliased\let\upperrightsingleninequote\quoteright      % ’
\aliased\let\upperrightdoubleninequote\quotedblright   % ”

\aliased\let\upperleftsinglesixquote  \quoteleft       % ‘
\aliased\let\upperleftdoublesixquote  \quotedblleft    % “
\aliased\let\upperrightsinglesixquote \quoteleft       % ‘
\aliased\let\upperrightdoublesixquote \quotedblleft    % “

\aliased\let\leftsubguillemot         \guilsingleleft  % ‹
\aliased\let\rightsubguillemot        \guilsingleright % ›

% Some left-overs that will go away!

\permanent\protected\def\textblacksquare{\dontleavehmode\hpack{\vrule\s!width.3\s!em\s!height.4\s!em\s!depth-.1\s!em}}
%permanent\protected\def\schwa          {\hbox{\rotate[\c!rotation=180,\c!location=\v!high]{\hbox{e}}}}
\permanent\protected\def\schwagrave     {\buildtextgrave\schwa}

\installcorenamespace{controlspace}

\permanent\protected\def\fallbackcontrolspace % beware, current font, we also need to honor color
  {\hbox to \interwordspace \bgroup
     \hss
     \ifcsname\??controlspace\number\interwordspace\endcsname
       \csname\??controlspace\number\interwordspace\endcsname
     \else
       \enco_fast_control_space_define % only regular
     \fi
     \textcontrolspace
     \hss
   \egroup}

\protected\def\enco_fast_control_space_define
  {\scratchdimen\interwordspace
   \definedfont[LMTypewriter-Regular at \the\dimexpr\currentfontbodyscale\dimexpr\fontbody]% see font-sym.mkiv
   \gletcsname\??controlspace\number\scratchdimen\endcsname\lastrawfontcall}

\permanent\protected\def\normalcontrolspace
  {\iffontchar\font\numexpr\expandafter`\textcontrolspace\relax
     \textcontrolspace
   \else
     \fallbackcontrolspace
   \fi}

\aliased\let\textvisiblespace\normalcontrolspace

\permanent\protected\def\optionalcontrolspace
  {\iffontchar\font\numexpr\expandafter`\textcontrolspace\relax
     \textcontrolspace
   \else
     \asciispacechar % used for export !
   \fi}

%D A smaller and bolder variant, more like the math and monospaced ones.

\permanent\protected\def\fakeunderscore
  {\relax\ifmmode
     \vrule
       \s!depth    .12\fontexheight\mathstylefont\normalmathstyle
       \s!width \fontinterwordspace\mathstylefont\normalmathstyle
       \s!height\zeropoint
     \relax
   \else
     \dontleavehmode\hbox\bgroup
       \vrule
         \s!depth    .12\fontexheight\font
         \s!width \fontinterwordspace\font
         \s!height\zeropoint
      \egroup
   \fi}

\permanent\protected\def\fakeunderscores{\enforced\let\_\fakeunderscore}
\permanent\protected\def\textunderscores{\enforced\let\_\textunderscore}

\textunderscores

\ifdefined\mathunderscore \else \aliased\let\mathunderscore\fakeunderscore \fi
\ifdefined\textunderscore \else \aliased\let\textunderscore\fakeunderscore \fi

\permanent\protected\def\normalunderscore{\ifmmode\mathunderscore\else\textunderscore\fi}

\pushoverloadmode
    \enforced\let\_\normalunderscore
\popoverloadmode

%D To be sorted out:

\pushoverloadmode

\permanent\protected\def\textminus
  {\char
     \iffontchar\font"2012 "2012 \orelse % figuredash
     \iffontchar\font"2013 "2013 \orelse % endash
     \iffontchar\font"2212 "2212 \else   % math minus
                           "002D \fi}    % hyphen

\permanent\protected\def\textplus
  {\char"002B } % plus

\popoverloadmode

%D Moved from core-mis:

% \permanent\protected\def\celsius   #1{#1\mathematics{^\circ}C}
% \permanent\protected\def\inch        {\mathematics{\prime\prime}} % was: \hbox{\rm\char125\relax}
% \permanent\protected\def\fraction#1#2{\mathematics{#1\over#2}}

\permanent\protected\def\celsius   #1{#1\iffontchar\font`℃℃\else\mathematics{^\circ}C\fi}   % 0x2103
%permanent\protected\def\inch        {\iffontchar\font`″″\else\mathematics{\prime\prime}\fi} % 0x2103
\permanent\protected\def\fraction#1#2{\mathematics{\Uabove exact .125\exheight {#1}{#2}}}

% \permanent\protected\def\fakeminute
%   {\dontleavehmode
%    \begingroup
%    \doifelseitalic
%      {\scratchwidth\fontcharwd\font\singlequoteasciicode
%       \hpack \s!to 1.2\scratchwidth \bgroup
%         \glyphslant\plustwohundred
%         \glyph \s!xoffset -0.6\scratchwidth\singlequoteasciicode
%         \hss
%       \egroup}%
%      {\scratchwidth\fontcharwd\font\singlequoteasciicode
%       \hpack \s!to 1.1\scratchwidth \bgroup
%         \glyphslant\plusfivehundred
%         \glyph \s!xoffset -\scratchwidth\singlequoteasciicode
%         \hss
%       \egroup}%
%    \endgroup}

% \permanent\protected\def\fakesecond
%   {\dontleavehmode
%    \begingroup
%    \doifelseitalic
%      {\scratchwidth\fontcharwd\font\singlequoteasciicode
%       \hpack \s!to 1.7\scratchwidth \bgroup
%         \glyphslant\plustwohundred
%         \glyph \s!xoffset -0.6\scratchwidth\singlequoteasciicode
%         \glyph \s!xoffset -1.1\scratchwidth\singlequoteasciicode
%         \hss
%       \egroup}%
%      {\scratchwidth\fontcharwd\font\singlequoteasciicode
%       \hpack \s!to 1.6\scratchwidth \bgroup
%         \glyphslant\plusfivehundred
%         \glyph \s!xoffset    -\scratchwidth\singlequoteasciicode
%         \glyph \s!xoffset -1.5\scratchwidth\singlequoteasciicode
%         \hss
%       \egroup}%
%    \endgroup}

\permanent\protected\def\fakeminute{\startimath\mathsurround\zeropoint\mathatom \mathunspacedcode{}\prime      \stopimath}
\permanent\protected\def\fakesecond{\startimath\mathsurround\zeropoint\mathatom \mathunspacedcode{}\doubleprime\stopimath}

\permanent\protected\def\mathminute{\begingroup\resetmathcollapsing\textminute\endgroup}
\permanent\protected\def\mathsecond{\begingroup\resetmathcollapsing\textsecond\endgroup}

\permanent\protected\def\minute{\ifmmode\mathminute\orelse\iffontchar\font\textminute\textminute\else\fakeminute\fi}
\permanent\protected\def\second{\ifmmode\mathsecond\orelse\iffontchar\font\textsecond\textsecond\else\fakesecond\fi}

\aliased\let\foot\minute % $12\foot + 2$ $12\foot\foot + 2$ $12\inch + 2$
\aliased\let\inch\second

%D \startbuffer
%D \startlines
%D     x\periods x
%D     x\periods[10]x
%D     x\periods[n=10,symbol={,}]x
%D     x\periods[n=4,symbol={!!},width=1em]x
%D     x\periods[n=4,symbol={!!},width=fit]x
%D     x\periods[n=4,symbol={!!},width=fit,distance=1em]x
%D     x\unknown x
%D \stoplines
%D \stopbuffer
%D
%D \typbuffer \getbuffer

\installcorenamespace {periods}

\installsetuponlycommandhandler \??periods {periods}

\setupperiods
  [\c!n=3,
   \c!width=.5\emwidth, % can also be \v!fit
   \c!distance=.25\emwidth,
   \c!symbol=.]

\permanent\protected\def\periods
  {\dontleavehmode
   \hbox\bgroup
   \doifelsenextoptional\enco_periods_yes\enco_periods_nop}

\protected\def\enco_periods_yes[#1]%
  {\doifelseassignment{#1}
     {\setupcurrentperiods[#1]%
      \scratchcounter{\periodsparameter\c!n}}
     {\scratchcounter{\ifempty{#1}\periodsparameter\c!n\else#1\fi}}%
   \enco_periods_finish}

\protected\def\enco_periods_nop
  {\scratchcounter{\periodsparameter\c!n}%
   \enco_periods_finish}

\protected\def\enco_periods_finish
  {\edef\p_width{\periodsparameter\c!width}%
   \ifx\p_width\v!fit % can be \ifcstoks (*)
     \enco_periods_finish_fit
   \else
     \enco_periods_finish_width
   \fi
   \egroup}

\protected\def\enco_periods_finish_width
  {\setbox\scratchbox\hbox to {\p_width}% can be \lastnamedcs (*)
     {\hss\periodsparameter\c!symbol\hss}%
   \dorecurse\scratchcounter{\copy\scratchbox}}

\protected\def\enco_periods_finish_fit
  {\edef\p_symbol{\periodsparameter\c!symbol}%
   \scratchdistance{\periodsparameter\c!distance}%
   \hskip\scratchdistance
   \dorecurse\scratchcounter{\p_symbol\hskip\scratchdistance}}

\permanent\protected\def\unknown
  {\dontleavehmode
   \hbox\bgroup
   \enco_periods_nop}

%D Left-overs:

\appendtoks
    \enforced\let\buildtextaccent\secondoftwoarguments
\to \everysimplifycommands

%D See enco-ini.mkivc for a definition of the obsolete \type {\t}.

\protect \endinput
