%D \module
%D   [       file=m-arabtex,  % was font-arb,
%D        version=2003.02.22, % 1999.11.06,
%D          title=\CONTEXT\ Modules,
%D       subtitle=Arabic,
%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.

% I still need to hook in some features into the setup
% macro. I also have to (re)define farsi etc. in ways
% similar to arab, so that we have dedicated environments.
%
% keywords needed: vocalize transscribe
%
% \startarabic[option=vocalize] % or vocalize=yes
% ......
% \stoparabic

\writestatus{loading}{ConTeXt Font Macros / ArabTeX support}

%D At the \NTG\ 10\high{th} anniversary meeting Klaus Lagally
%D introduced the audience to arabic typesetting, and after
%D that I knew that some day I really had to look into his
%D generic package. And then, sort of simultaniously Maarten
%D Wisse and Imran Ahsan Nyazee asked me if \CONTEXT\ could
%D support \ARABTEX, a package that provides right to left
%D typesetting of (several variants of) arab and hebrew.
%D Having implemented support for chinese a few weeks before,
%D I could not resist to build in support for arab and hebrew
%D too. Writing support for languages that don't give me any
%D cue on how to pronounce their script, is kind of special.

%D This is a beta version, since I still have to take care of some
%D macros that conflict with existing stuff.

\unprotect

\definesystemvariable{ARABTEX}

\def\setupARABTEX
  {\getparameters[\??ARABTEX]}

\setupARABTEX
  [\s!rscale=1.2,
   \c!before=,
   \c!after=,
   \c!inner=\setarab,
   \c!style=\setarabicfont\fontstylesuffix]

%D A few (maybe too) simple hooks into the font mechanism. The
%D hook into the language module is not yet done.

% \unexpanded\def\setarabicfont#1% rscale
%   {\scratchdimen\@@ARABTEXrscale\bodyfontsize
%    \font\arbfont\truefontname{Arabic#1} at \currentfontscale\scratchdimen
%    \setx@skels
%    \newfonttrue
%    \arbfont}
%
% more modern
%
% \unexpanded\def\setarabicfont#1% rscale
%   {\definefont[arbfont][Arabic#1 sa \@@ARABTEXrscale]%
%    \setx@skels
%    \newfonttrue
%    \arbfont}
%
% more efficient

\unexpanded\def\setarabicfont#1%
  {\setx@skels
   \newfonttrue
   \definedfont[Arabic#1 sa \ARABTEXparameter\s!rscale]}

%D Just to be compatible with Arab\TEX\ we define:

\unexpanded\def\nash   {\setarabicfont\s!Regular}
\unexpanded\def\nashbf {\setarabicfont\s!Bold   }
\unexpanded\def\pnash  {\setarabicfont\s!Regular}
\unexpanded\def\pnashbf{\setarabicfont\s!Bold   }
\unexpanded\def\xnash  {\setarabicfont\s!Regular}
\unexpanded\def\xnashbf{\setarabicfont\s!Bold   }

%D The display arabic environment (will be an installable
%D object) uses a few conditionals. Let's do it the
%D \CONTEXT\ way and define an anvironment that we later can
%D adapt.

\newif\if@ignore
\newif\if@endpe

\def\setupARABTEXalternative[#1]%
  {\dodoubleempty\getparameters[\??ARABTEX#1]}

\def\defineARABTEXalternative
  {\dodoubleempty\dodefineARABTEXalternative}

\def\dodefineARABTEXalternative[#1][#2]%
  {\iffirstargument % \startarab is defined but used already
     \getparameters
       [\??ARABTEX#1]
       [\c!before=\@@ARABTEXbefore,
        \c!after=\@@ARABTEXafter,
        \c!inner=\@@ARABTEXinner,
        \c!style=\@@ARABTEXstyle,
        \s!rscale=\@@ARABTEXrscale,
        #2]%
     \setvalue   {\e!start#1}{\displayARABTEXalternative{#1}}%
     \unexpanded\setvalue{#1}{\inlineARABTEXalternative {#1}}%
     \unexpanded\def\RL{\getvalue{#1}}%
     \unexpanded\def\LR{\a@LR}%
     \let\R=\RL
     \let\L=\LR
     \expandafter\all@wcmd\csname\e!stop#1\endcsname
   \else
     \defineARABTEXalternative[arabic]%
   \fi}

\def\initializeARABTEXinternals
  {\ARABTEXparameter\c!inner
   \the\everyARABTEXpreset}

\newtoks \everyARABTEXpreset

\let\currentARABTEXalternative\empty

\def\ARABTEXparameter#1%
  {\csname\??ARABTEX\currentARABTEXalternative#1\endcsname}

\def\inlineARABTEXalternative#1#2%
  {\bgroup
   \edef\currentARABTEXalternative{#1}%
   \initializeARABTEXinternals
   \a@RL{#2}%
   \egroup}

\def\displayARABTEXalternative#1%
  {\dodoubleempty\dostartARABTEXalternative[#1]}

\def\dostartARABTEXalternative[#1][#2]%
  {\begingroup
   \edef\currentARABTEXalternative{#1}%
   \getparameters[\??ARABTEX\currentARABTEXalternative][#2]%
   \ifnum1<0\ARABTEXparameter\c!n\relax
     \startcolumns
   \else
     \ARABTEXparameter\c!before
   \fi
   \initializeARABTEXinternals
   \initializeARABTEXend
   \arabtext
   \initializeARABTEXalternative}

\def\initializeARABTEXalternative
  {\ARABTEXparameter\c!style}

\def\initializeARABTEXend% \CONTEXT\ does use \end quite differently
  {\long\def\end##1%
     {\endarabtext
      \ifnum1<0\ARABTEXparameter\c!n\relax
        \stopcolumns
      \else
        \ARABTEXparameter\c!after
      \fi
      \endgroup}%
   \let\a@l@end\end
   \letvalue{\e!stop\currentARABTEXalternative}=\end
   \long\def\end##1%
     {\endarabtext
      \endgroup
      \if@ignore\global\@ignorefalse\expandafter\ignorespaces\fi}}

%D Arabic verbatim.

\def\typearab{\a@@verb}

%D Some \LATEX\ macros.

\def \makeatletter{\unprotect}
\def \makeatother {\protect}
\def \typeout     {\writestatus{arabtex}}

%D We have to save some macros.

\let\ARABTEXversion=\empty

\def\startloadingARABTEX% ugly hacks
  {\catcode`!=12
   \catcode`?=12
   \pushmacro\output        \let \output        \scratchtoks
   \pushmacro\LaTeX         \let \LaTeX         \undefined
   \pushmacro\CJK           \let \CJK           \undefined
%    \pushmacro\peek@token    \let \peek@token    \undefined
%    \pushmacro\edmacloaded   \let \edmacloaded   \undefined
   \pushmacro\year          \let \year          \normalyear
   \pushmacro\month         \let \month         \normalmonth
   \pushmacro\day           \let \day           \normalday
   \pushmacro\input         \def \input    ##1 {\normalinput ##1 }
   \pushmacro\linewidth
   \pushmacro\datum         \def\datum         {\toks0}
   \pushmacro\version       \def\version       {\toks2}
   \pushmacro\theversion    \let\theversion     \ARABTEXversion
   \pushmacro\emphasize
   \pushmacro\cap}

\def\stoploadingARABTEX
  {\catcode`!=11
   \catcode`?=11
   \popmacro\cap
   \popmacro\emphasize
   \popmacro\theversion
   \popmacro\version
   \popmacro\datum
   \popmacro\linewidth
   \popmacro\input
   \popmacro\day
   \popmacro\month
   \popmacro\year
%    \popmacro\edmacloaded
%    \popmacro\peek@token
   \popmacro\CJK
   \popmacro\LaTeX
   \popmacro\output}

%D We save some macros:

\startloadingARABTEX

%D When loading \ARABTEX\ we have to set back the~! and~?.

\input arabtex.sty

%D Since \ARABTEX\ has its own \type {\cap}, we save the
%D new meaning. We also redefine some \PLAIN\ macros, which
%D happen to have a different meaning in \LATEX.

\let\ARABTEXversion\theversion
\let\ARABTEXcap    \cap

\appendtoks
  \let\cap\ARABTEXcap
\to \everyARABTEXpreset

\def\ARABTEXsh@ft#1%
  {\dimen@.00#1ex
   \multiply\dimen@\slantperpoint
   \kern-.0156\dimen@}

\appendtoks
  \let\sh@ft\ARABTEXsh@ft
\to \everyARABTEXpreset

\def\ARABTEXd#1%
  {{\o@lign{\relax#1\crcr\hidewidth\sh@ft{10}%
    .\hidewidth}}}

\def\ARABTEXb#1%
  {{\o@lign{\relax#1\crcr\hidewidth\sh@ft{29}%
    \vbox to.2ex{\hbox{\char22}\vss}\hidewidth}}}

\appendtoks
  \let\b\ARABTEXb
  \let\d\ARABTEXd
\to \everyARABTEXpreset

\def \ARABTEXbreakA {\hfill\break}
\def \ARABTEXbreakB {\break}
\edef\ARABTEXbar    {\string|}
\def \ARABTEXcomma  {\relax\ifmmode\mskip\thinmuskip\else\thinspace\fi}

\appendtoks
  \let\\=\ARABTEXbreakA
  \let\|=\ARABTEXbreakB
  \let |=\ARABTEXbar
  \let\,=\ARABTEXcomma
\to \everyARABTEXpreset

\let\ARABTEXprotect\relax

\appendtoks
  \let\protect\ARABTEXprotect
\to \everyARABTEXpreset

%D Now we can pop the saved macros.

\stoploadingARABTEX

%D Ah, we have to get rid of some \type {\protect} stuff but
%D to permit testing we add it in the \CONTEXT\ way.

% \bgroup
% \catcode`\<=\@other
% \unexpanded\gdef\a@ins
%   {\ifmmode
%      \expandafter<%
%    \else
%      \dontleavehmode \bgroup
%      \arab@codes \set@arabfont \@waslafalse \@wasfalse
%      \expandafter\arab@insert
%    \fi}
% \unexpanded\gdef\<{\a@ins}
% \catcode`\<=\active
% \global\let<=\a@ins
% \egroup
%
% cleaner:

\defineactivecharacter < {\a@ins}  \unexpanded\gdef\<{\a@ins}%

\def\normal@a@ins
  {\dontleavehmode % context prefers this instead of \leavevmode
   \bgroup
   \arab@codes
   \set@arabfont
   \@waslafalse
   \@wasfalse
   \arab@insert}

\unexpanded\gdef\a@ins
  {\mathortext<\normal@a@ins}

%D We also need to register a few macros:

\all@wcmd\initializeARABTEXalternative % no argument, internal command
\all@wcmd\tx                           % no argument, small font
\all@wcmd\txx                          % no argument, smaller font

%D We also hook it into the presetter.

\appendtoks
  \let\normaltx \tx \def\tx {\normaltx \setarabicfont\fontstylesuffix}%
  \let\normaltxx\txx\def\txx{\normaltxx\setarabicfont\fontstylesuffix}%
\to \everyARABTEXpreset

%D The main definitions are:

\definefontsynonym [ArabicRegular] [xnsh14]
\definefontsynonym [ArabicBold]    [xnsh14bf]

\defineARABTEXalternative
  [arabic]
  [\c!inner=\setarab,
   \c!style=\setarabicfont\fontstylesuffix]

\defineARABTEXalternative
  [farsi]
  [\c!inner=\setfarsi,
   \c!style=\setarabicfont\fontstylesuffix]

\defineARABTEXalternative
  [urdu]
  [\c!inner=\seturdu,
   \c!style=\setarabicfont\fontstylesuffix]

\defineARABTEXalternative
  [maghribi]
  [\c!inner=\setmaghribi,
   \c!style=\setarabicfont\fontstylesuffix]

%D Apart from such definitions, one can adapt the settings
%D using \type {\setupARABTEXalternative}.
%D
%D A few years ago at the Holland Festivities, I attended {\em
%D The Cave}, one of the most impressive combinations of music
%D and video I know. This composition of Steve Reich (music)
%D and .. (video) concentrates on the common grounds of arabs
%D and jews: their ancestor Abram. Listening to the \CDROM's
%D of {\em The Cave}, provided me the right ambiance for
%D filling in the details of this module. In {\em The Cave},
%D interviews, music, and |<|believe it or not|>| rhythmic
%D typography are the cornerstones. Remembering those big
%D screens, it strikes me that like music, \TEX\ too is a
%D perfect instrument to cross cultural and linguistic
%D borders. So, let's load Hebrew support as well:

\unexpanded\def\sethebrewfont#1%
  {\setx@skels
   \newfonttrue
   \definedfont[Hebrew#1 sa \ARABTEXparameter\s!rscale]}

% \let \setheb \sethebrew

\unexpanded\def\pheb  {\sethebrewfont\s!Regular}
\unexpanded\def\phebbf{\sethebrewfont\s!Bold}

% \startloadingARABTEX

%   \ReadFile{hebtex.sty}
%   \ReadFile{apatch.sty}
%   \ReadFile{hepatch.sty}

% \stoploadingARABTEX

\definefontsynonym [HebrewRegular] [hclassic]
\definefontsynonym [HebrewBold]    [hcaption]

\defineARABTEXalternative
  [hebrew]
  [\c!inner=\sethebrew,
   \c!style=\sethebrewfont\fontstylesuffix]

%D Now we're done:

\protect \endinput

% everyoutput : \charsubdefmax \arab@charsubdefmax

% \defineconversion [abjad] [\abj@d]
%
% voetnoten verbatim lijsten indexen tabellen uitlijnen
%
% \v!hoofdstuk=al-fa.slu
%    \v!inhoud=al-mu.htawayAtu
%   \v!figuren=qA'imaTu al-.suwaru
%  \v!tabellen=qA'imaTu al-^gadAwilu
% \v!grafieken=qA'imaTu al-rusUmu
%     \v!index=al-fihrisu
%   \v!bijlage=al-mul.haqu

% \usemodule[arabtex]
%
% \usetypescript[postscript] \switchtotypeface[postscript]
%
% \setarab \novocalize
%
% \starttext
%
% \placecontent
%
% \section{\<mu.hammad>} % short arabic use \< .. >
% \section{\<mu.hammad>} % short arabic use \< .. >
%
% \startarabic
% mu.hammad 'i_d q"aAm zay"d" + i_d yaqUm zyd + A_d zyd q"aAm
%
% mu.hammad 'i_d q"aAm zay"d" + i_d yaqUm zyd + A_d zyd q"aAm
% \stoparabic
%
% \section{\<al-maq.sad>}
%
% \startarabic
% mu.hammad 'i_d q"aAm zay"d" + i_d yaqUm zyd + A_d zyd q"aAm
% \stoparabic
%
% \stoptext