%D \module
%D   [       file=char-act,
%D        version=2006.12.05,
%D          title=\CONTEXT\ Character Support,
%D       subtitle=Active,
%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.

\writestatus{loading}{ConTeXt Character Support / Active}

\unprotect

\ifdefined\page_otr_fill_and_eject_page \else \let\page_otr_fill_and_eject_page\relax \fi % forward reference

%D \macros
%D   {obeyedspace, obeyedtab, obeyedline, obeyedpage}
%D
%D We have followed Knuth in naming macros that make \SPACE,
%D \NEWLINE\ and \NEWPAGE\ active and assigning them
%D \type{\obeysomething}, but first we set some default values.

% These are expandable!

\def\obeyedspace {\space}
\def\obeyedtab   {\obeyedspace}
\def\obeyedline  {\par}
\def\obeyedpage  {\page_otr_fill_and_eject_page}

%D \macros
%D   {controlspace,setcontrolspaces}
%D
%D First we define \type{\obeyspaces}. When we want visible
%D spaces (control spaces) we only have to adapt the definition
%D of \type{\obeyedspace} to:

\chardef\asciispacechar\spaceasciicode % a real space character

\unexpanded\def\naturalspace{\asciispacechar}
\unexpanded\def\controlspace{\hbox{\asciispacechar}}               % rather tex, we need the unicode value
\unexpanded\def\normalspaces{\catcode\spaceasciicode\spacecatcode}

% \bgroup
%     \catcode\spaceasciicode\activecatcode
%     \unexpanded\gdef\obeyspaces      {\catcode\spaceasciicode\activecatcode\def {\obeyedspace }}
%     \unexpanded\gdef\setcontrolspaces{\catcode\spaceasciicode\activecatcode\def {\controlspace}}
% \egroup

%unexpanded\def\obeyspaces      {\catcode\spaceasciicode\activecatcode\letcharcode\spaceasciicode\obeyedspace }
\unexpanded\def\setcontrolspaces{\catcode\spaceasciicode\activecatcode\letcharcode\spaceasciicode\controlspace}

%D \macros
%D   {obeytabs, obeylines, obeypages,ignoretabs, ignorelines, ignorepages}
%D
%D Next we take care of \NEWLINE\ and \NEWPAGE\ and because we
%D want to be able to typeset listings that contain \TAB, we
%D have to handle those too. Because we have to redefine the
%D \NEWPAGE\ character locally, we redefine the meaning of
%D this (often already) active character.

% \expandafter\def\activeformfeedtoken{\par}

\letcharcode\formfeedasciicode\par

%D The following indirect definitions enable us to implement
%D all kind of \type{\obeyed} handlers.

% \unexpanded\def\obeytabs   {\catcode\tabasciicode      \activecatcode\expandafter\def\activetabtoken      {\obeyedtab }}
% \unexpanded\def\obeylines  {\catcode\endoflineasciicode\activecatcode\expandafter\def\activeendoflinetoken{\obeyedline}}
% \unexpanded\def\obeypages  {\catcode\formfeedasciicode \activecatcode\expandafter\def\activeformfeedtoken {\obeyedpage}}

% \unexpanded\def\ignoretabs {\catcode\tabasciicode      \activecatcode\expandafter\def\activetabtoken      {\obeyedspace}}
% \unexpanded\def\ignorelines{\catcode\endoflineasciicode\activecatcode\expandafter\def\activeendoflinetoken{\obeyedspace}}
% \unexpanded\def\ignorepages{\catcode\formfeedasciicode \ignorecatcode}
% \unexpanded\def\ignoreeofs {\catcode\endoffileasciicode\ignorecatcode}

% but ... as we don't want to freeze to \obeyedspace etc which can be set after \obeyspaces, we
% use an idirectness

\def\_obeyed_space_{\obeyedspace}
\def\_obeyed_tab_  {\obeyedtab}
\def\_obeyed_line_ {\obeyedline}
\def\_obeyed_page_ {\obeyedpage}

\unexpanded\def\obeyspaces {\catcode\spaceasciicode    \activecatcode\letcharcode\spaceasciicode    \_obeyed_space_}
\unexpanded\def\obeytabs   {\catcode\tabasciicode      \activecatcode\letcharcode\tabasciicode      \_obeyed_tab_}
\unexpanded\def\obeylines  {\catcode\endoflineasciicode\activecatcode\letcharcode\endoflineasciicode\_obeyed_line_}
\unexpanded\def\obeypages  {\catcode\formfeedasciicode \activecatcode\letcharcode\formfeedasciicode \_obeyed_page_}

\unexpanded\def\ignoretabs {\catcode\tabasciicode      \activecatcode\letcharcode\tabasciicode      \_obeyed_space_}
\unexpanded\def\ignorelines{\catcode\endoflineasciicode\activecatcode\letcharcode\endoflineasciicode\_obeyed_space_}
\unexpanded\def\ignorepages{\catcode\formfeedasciicode \ignorecatcode}
\unexpanded\def\ignoreeofs {\catcode\endoffileasciicode\ignorecatcode}

\unexpanded\def\setcontrolspaces{\catcode\spaceasciicode\activecatcode\letcharcode\spaceasciicode\_control_space_}

%D \macros
%D   {naturaltextext}
%D
%D When one uses \ETEX, switching to normal \TEX\ is possible
%D too. We also introduce a switch that can be used in the
%D drivers and set in higher level shell macros.

\unexpanded\def\naturaltextext#1\relax % this command will become obsolete
  {\begingroup
   \def\ascii{#1}%
   \setcatcodetable\ctxcatcodes
   \prettynaturalfont{\scantextokens\expandafter{\ascii}\ifhmode\unskip\fi}%
   \endgroup}

\endinput \protect
