%D \module
%D   [       file=core-ver,
%D        version=2000.05.09,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Verbatim,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%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 Core Macros / Verbatim}

\startmessages  dutch  library: verbatims
  title: typen
      1: file -- bestaat niet 
\stopmessages

\startmessages  english  library: verbatims
  title: verbatim
      1: file -- does not exist 
\stopmessages

\startmessages  german  library: verbatims
  title: verbatim
      1: Datei -- existiert nicht 
\stopmessages

\startmessages  czech  library: verbatims
  title: verbatim
      1: soubor -- neexistuje
\stopmessages

\startmessages  italian  library: verbatims
  title: verbatim
      1: il file -- non esiste
\stopmessages

\startmessages  norwegian  library: verbatims
  title: verbatim
      1: fil -- eksisterer ikke 
\stopmessages

\startmessages  romanian  library: verbatims
  title: verbatim
      1: fisierul -- nu exista
\stopmessages

\unprotect

% \type{ <crlf> char} geeft bagger

%D We are going to embed the general verbatim support macros in
%D a proper environment. First we show the common setup
%D macro, so we know what features are supported. The options
%D are hooked into the support macros via the \type{\obey}
%D macros.

\def\prettyidentifier {TEX}
\def\prettypalet      {}

\def\installprettytype%
  {\dodoubleargument\doinstallprettytype}

\def\doinstallprettytype[#1][#2]% map #1 onto #2 
  {\uppercasestring#1\to\asciiA
   \uppercasestring#2\to\asciiB
   \setevalue{\??ty\??ty\asciiA}{\asciiB}}

\def\setupprettiesintype#1%
  {\uppercasestring#1\to\ascii   
   \edef\prettyidentifier%
     {\ifundefined{\??ty\??ty\ascii}TEX%
      \else\getvalue{\??ty\??ty\ascii}\fi}%
   \doifundefined{setuppretty\prettyidentifier type}%
     {\bgroup
      \setbox0\hbox % get rid of spaces when in-line \newpretty loading
        {\restorecatcodes % also needed when loading during \newpretty 
         \startreadingfile % restore < and > if needed
         \lowercasestring verb-\prettyidentifier.tex\to\filename
         \readsysfile\filename\donothing\donothing
         \stopreadingfile}%
      \egroup}%
   \doifdefinedelse{setuppretty\prettyidentifier type}%
     {\let\uncatcodecharacters\uncatcodeallcharacters % ugly, should be switch
      \def\setupprettytype{\getvalue{setuppretty\prettyidentifier type}}}
     {\let\setupprettytype\relax}}
  
\installprettytype [RAW]        [RAW]

\installprettytype [TEX]        [TEX]

\installprettytype [PERL]       [PL]
\installprettytype [PL]         [PL]
\installprettytype [PM]         [PL]

\installprettytype [METAPOST]   [MP]
\installprettytype [METAFONT]   [MP]
\installprettytype [MP]         [MP]
\installprettytype [MF]         [MP]

\installprettytype [JAVASCRIPT] [JS]
\installprettytype [JAVA]       [JV]
\installprettytype [JS]         [JS]
\installprettytype [JV]         [JV]

\installprettytype [SQL]        [SQL]

\installprettytype [PASCAL]     [PAS]
\installprettytype [PAS]        [PAS]
\installprettytype [MODULA]     [PAS]
\installprettytype [MOD]        [PAS]

\installprettytype [EIFFEL]     [EIF]
\installprettytype [EIF]        [EIF]
\installprettytype [E]          [EIF]

\installprettytype [XML]        [XML]

\installnewpretty M {\setupprettiesintype {MP}\setupprettytype}
\installnewpretty P {\setupprettiesintype {PL}\setupprettytype}
\installnewpretty T {\setupprettiesintype{TEX}\setupprettytype}
\installnewpretty J {\setupprettiesintype {JV}\setupprettytype}
\installnewpretty S {\setupprettiesintype{SQL}\setupprettytype}
\installnewpretty W {\setupprettiesintype{PAS}\setupprettytype} % Wirth
\installnewpretty I {\setupprettiesintype{EIF}\setupprettytype} % E taken
\installnewpretty X {\setupprettiesintype{XML}\setupprettytype}

 \def\setupcommonverbatim#1%
  {\eightbitcharactersfalse % obey regime / encoding 
   %
   \def\prettyidentifier{TEX}%
   %
   \doifelsevalue{#1\c!tekst}\v!ja
     \naturaltextexttrue
     \naturaltextextfalse
   \def\prettyidentifierfont{\getvalue{#1\c!icommando}}%
   \def\prettyvariablefont  {\getvalue{#1\c!vcommando}}%
   \def\prettynaturalfont   {\getvalue{#1\c!ccommando}}%
   %
   \doifvalue{#1\c!spatie}\v!aan{\def\obeyspaces{\setcontrolspaces}}%
   %doifvalue{#1\c!tab}   \v!aan{\def\obeytabs  {\settabskips}}%
   \doifnotvalue{#1\c!tab}\v!uit{\def\obeytabs  {\settabskips}}%
   \doifvalue{#1\c!tab} \s!ascii{\chardef\tabskipmode2}% quit on >127 
   \doifvalue{#1\c!pagina}\v!nee{\def\obeypages {\ignorepages}}%
   %
   \ExpandFirstAfter\processaction
     [\getvalue{#1\c!regels}]
     [        \v!ja=>\obeybreakpoints,
      \v!afgebroken=>\obeyhyphens]%
   %
   \ExpandFirstAfter\processaction
     [\getvalue{#1\c!optie}]
     [     \v!geen=>\let\obeycharacters\relax,
          \v!kleur=>\setupprettiesintype{TEX}%
                    \let\obeycharacters\setupprettytype
                    \let\obeytabs\ignoretabs,
        \v!normaal=>\let\obeycharacters\setupgroupedtype,
      \v!commandos=>\def\obeycharacters{\setupcommandsintype{#1}}%
                    \let\obeytabs\ignoretabs,
         \v!schuin=>\let\obeycharacters\setupslantedtype
                    \let\obeytabs\ignoretabs,
        \s!unknown=>\setupprettiesintype{\getvalue{#1\c!optie}}%
                    \let\obeycharacters\setupprettytype
                    \let\obeytabs\ignoretabs]%
   \def\verbatimfont{\getvalue{#1\c!letter}}%
   \setupverbatimcolor{#1}}

%D The verbatim commands have a rather long and turbulent
%D history. Most users of \CONTEXT\ probably will never use
%D some of the features, but I've kept in mind that when one is
%D writing a users manual, about everything can and undoubtly
%D will be subject to a verbatim treatment.
%D
%D Verbatim command are very sensitive to argument processing,
%D which is a direct result of the \CATCODES\ being fixed at
%D reading time. With our growing understanding of \TEX,
%D especially of the mechanism that can be used for looking
%D ahead and manipulating \CATCODES, the verbatim support
%D became more and more advanced and natural.
%D
%D Typesetting inline verbatim can be accomplished by
%D \type{\type}, which in this sentence was typeset by saying
%D just \type{\type{\type}}, which in turn was typeset by
%D \onbekend. Using the normal grouping characters \type{{}} is
%D the most natural way of using this command.
%D
%D A second, more or less redundant, alternative is delimiting
%D the argument with an own character. This method was
%D implemented in the context of a publication in the \MAPS,
%D where this way of delimiting is recognized by \LATEX\ users.
%D
%D The third, more original alternative, is the one using
%D \type{<<} and \type{>>} as delimiters. This alternative can
%D be used in situations where slanted typeseting is needed.

% todo: we can use \letter... here: 

\def\lesscharacter {<}
\def\morecharacter {>}

\chardef\texescape     = `\\
\chardef\leftargument  = `\{
\chardef\rightargument = `\}

%D \macros
%D   {type}
%D
%D We define \type{\type} as a protected command. First we
%D set the catcodes of \type{<} and \type{>} and then we start
%D looking ahead.

%\unexpanded\def\type%
%  {\bgroup
%   \catcode`\<=\@@other
%   \catcode`\>=\@@other
%   \futurelet\next\dotype}

\unexpanded\def\type%
  {\dotype\empty}
   
\def\dotype#1%
  {\bgroup
   \strut % new, enables leading space in \type { abc } at par start
   \edef\@@currenttype{#1}%
   \catcode`\<=\@@other
   \catcode`\>=\@@other
   \futurelet\next\dodotype}

%D Next we distinguish between the three alternatives and call
%D for the appropriate macros.

% \def\dotype%
%   {\ifx\next\bgroup
%      \initializetype
%      \initializetypegrouping
%      \def\next%
%        {\afterassignment\protectfirsttype\let\next=}%
%    \else\if\next<%
%      \doifelse{\@@tyoptie}{\v!geen}
%        {\initializetype
%         \setupnotypegrouping
%         \def\next%
%           {\let\next=}}
%        {\def\next<##1%
%           {\initializetype
%            \if##1<%
%            \else
%              \setupalternativetypegrouping
%              ##1%
%            \fi}}%
%    \else
%      \def\next##1%
%        {\initializetype
%         \catcode`##1=\@@endgroup}%
%    \fi\fi
%    \verbatimcolor
%    \next}
% 
% \def\dodotype%
%   {\ifx\next\bgroup
%      \initializetype
%      \verbatimcolor
%      \initializetypegrouping
%      \def\next%
%        {\afterassignment\protectfirsttype\let\next=}%
%    \else\if\next<%
%      \doifelsevalue{\??ty\@@currenttype\c!optie}{\v!geen}
%        {\initializetype
%         \verbatimcolor
%         \setupnotypegrouping
%         \def\next%
%           {\let\next=}}
%        {\def\next<##1%
%           {\initializetype
%            \verbatimcolor
%            \if##1<%
%            \else
%              \setupalternativetypegrouping
%              ##1%
%            \fi}}%
%    \else
%      \def\next##1%
%        {\initializetype
%         \verbatimcolor
%         \catcode`##1=\@@endgroup}%
%    \fi\fi
%    \next}

\def\dodotypeA
  {\initializetype
   \initializetypegrouping
   \verbatimcolor 
   \afterassignment\protectfirsttype\let\next=}

\def\dodotypeB
  {\initializetype
   \setupnotypegrouping
   \verbatimcolor 
   \let\next=}

\def\dodotypeC<#1%
  {\initializetype
   \verbatimcolor 
   \if#1<\else
     \setupalternativetypegrouping#1%
   \fi}

\def\dodotypeD#1%
  {\initializetype
   \verbatimcolor 
   \catcode`#1=\@@endgroup}

\def\dodotype%
  {\ifx\next\bgroup
     \@EA\dodotypeA
   \else\if\next<%
     \doifelsevalue{\??ty\@@currenttype\c!optie}\v!geen
       {\@EAEAEA\dodotypeB}{\@EAEAEA\dodotypeC}%
   \else
     \@EAEAEA\dodotypeD
   \fi\fi}

\bgroup
\catcode`\[=\@@begingroup
\catcode`\]=\@@endgroup
\catcode`\{=\@@active
\catcode`\}=\@@active
\gdef\initializetypegrouping%
  [\catcode`\{=\@@active
   \catcode`\}=\@@endgroup         % otherwise things go wrong ...
   \def\activerightargument%
     [\rightargument
      \egroup]%
   \def\activeleftargument%
     [\bgroup
      \leftargument               %% this way TeXEdit can check: { 
      \catcode`\}=\@@active        % ... in alignments (tables)
      \let}=\activerightargument]%
   \let{=\activeleftargument]%    %% this way TeXEdit can check: } 
\egroup

\bgroup
\catcode`\<=\@@active
\catcode`\>=\@@active
\gdef\setupalternativetypegrouping
  {\catcode`\<=\@@active
   \catcode`\>=\@@active
   \def<{\bgroup\switchslantedtype}%
   \def>{\egroup}}
\egroup

\def\setupnotypegrouping
  {\catcode`\<=\@@begingroup
   \catcode`\>=\@@endgroup}

%D When writing the manual to \CONTEXT\ and documenting this 
%D source we needed to typeset \type{<<} and \type{>>}. Because 
%D we wanted to do this in the natural way, we've adapted the 
%D original definition a bit. We still show the original 
%D because we think it's shows a bit better what we are 
%D doing. 
%D 
%D \starttypen 
%D \bgroup
%D \catcode`\<=\@@active
%D \catcode`\>=\@@active
%D \gdef\setupgroupedtype%
%D   {\catcode`\<=\@@active
%D    \catcode`\>=\@@active
%D    \def<%
%D      {\def\do%
%D         {\ifx\next<%
%D            \def\next{\bgroup\switchslantedtype\let\next=}%
%D          \else
%D            \let\next=\lesscharacter
%D          \fi
%D          \next}%
%D       \futurelet\next\do}%
%D    \def>%
%D      {\def\do%
%D         {\ifx\next>%
%D            \def\next{\egroup\let\next=}%
%D          \else
%D            \let\next=\morecharacter
%D          \fi
%D          \next}%
%D       \futurelet\next\do}}
%D \egroup
%D \stoptypen
%D
%D The final implementation looks a bit further and treats the 
%D lone \type{<<} and \type{>>} a bit different. The \type 
%D {\hbox{}} prevents ligatures, which unfortunately turn up 
%D in Lucida fonts. 

\def\doenterdoublelesstype
  {\ifx\next\egroup
      \lesscharacter\hbox{}\lesscharacter 
   \else
     \bgroup\switchslantedtype
     \let\doenterdoublemoretype\egroup
   \fi}

\def\doenterdoublemoretype
  {\def\doenterdoubletype
     {\ifx\next\egroup
        \morecharacter\hbox{}\morecharacter
      \fi}}

\bgroup
\catcode`\<=\@@active
\catcode`\>=\@@active
\gdef\setupgroupedtype
  {\catcode`\<=\@@active
   \catcode`\>=\@@active
   \def\doless
     {\ifx<\next
        \def\next
          {\def\enterdoubletype
             {\futurelet\next\doenterdoublelesstype}%
           \afterassignment\enterdoubletype
           \let\next=}%
      \else
        \let\next=\lesscharacter
      \fi
      \next}%
   \def\domore
     {\ifx>\next
        \def\next
          {\def\enterdoubletype
             {\futurelet\next\doenterdoublemoretype}%
           \afterassignment\enterdoubletype
           \let\next=}%
      \else
        \let\next\morecharacter
      \fi
      \next}%
   \def<{\futurelet\next\doless}%
   \def>{\futurelet\next\domore}}
\egroup

\newif\ifslantedtypeactivated
\newif\ifslantedtypepermitted

\def\switchslantedtype
  {\ifslantedtypepermitted
     \ifslantedtypeactivated
       \slantedtypeactivatedfalse\tttf
     \else
       \slantedtypeactivatedtrue\ttsl
     \fi
   \fi}

%\def\setupcommandsintype#1%
%  {\setupgroupedtype
%   \edef\!!stringa{\getvalue{#1\c!escape}}%
%   \@EA\catcode\@EA`\!!stringa=\@@escape}

\def\setupcommandsintype#1% can also be \string\ 
  {\setupgroupedtype
   \edef\\{\getvalue{#1\c!escape}}%
   \letvalue{\\}=\\% for instance \/=/ 
   \@EA\catcode\@EA`\\=\@@escape
   \def\BTEX##1\ETEX##2% ##2 gobbles active space 
     {\naturaltextext##1\unskip\relax}}

%D The following lines show what happens when we set 
%D \type {option=commands}. 
%D 
%D \startbuffer
%D \starttyping
%D test//test test/BTEX \footnote{test test test}/ETEX test
%D test//test test/BTEX \footnote{test test test}/ETEX  test
%D test test test/BTEX \bf(nota bene)/ETEX test
%D test test test /BTEX \bf(nota bene)/ETEX  test
%D \stoptyping
%D \stopbuffer
%D 
%D % \bgroup\setuptyping[option=commands]\getbuffer\egroup
%D 
%D this was keyed in as: 
%D 
%D \typebuffer

\def\setupslantedtype
  {\setupgroupedtype
   \slantedtypepermittedtrue}

\bgroup
\catcode`\<=\active
\catcode`\>=\active
\gdef\doprotectfirsttype
  {\ifx\next<%
     \let\next\relax
   \else\ifx\next\bgroup
     \let\next\relax
   \else\ifx\next\egroup % takes care of \type{} 
     \let\next\relax
   \else\ifx\next\activeleftargument
     \let\next\relax
   \else
     \let\next\string
   \fi\fi\fi\fi
   \next}
\egroup

\def\protectfirsttype%
  {\futurelet\next\doprotectfirsttype}

%D The neccessary initializations are done by calling
%D \type{\initializetype} which in return calls for the support
%D macro \type{\setupinlineverbatim}.

%\def\initializetype%
%  {\let\obeylines=\ignorelines
%   \setupcommonverbatim\??ty
%   \setupinlineverbatim}

\let\@@currenttype\empty

\def\initializetype
  {\let\obeylines\ignorelines
   \setupcommonverbatim{\??ty\@@currenttype}%
   \setupinlineverbatim}

%D \macros
%D   {setuptype}
%D
%D Some characteristics of \type{\type} can be set up by:

%\def\setuptype%
%  {\dodoubleargument\getparameters[\??ty]}

\def\setuptype
  {\dodoubleempty\dosetuptype}

\def\dosetuptype[#1][#2]%
  {\ifsecondargument
     \getparameters[\??ty#1][#2]%
   \else
     \getparameters[\??ty][#1]%
   \fi}

%D The setups for inline verbatim default to:

\setuptype
  [\c!spatie=\v!uit,
    \c!kleur=,
   \c!letter=\tt\tf, % \tttf gives problems with {\tx \type...}
   \c!pagina=\v!nee,
      \c!tab=\v!nee,
    \c!optie=\v!normaal]

%D \macros
%D   {typ,obeyhyphens,obeybreakpoints}
%D
%D Although it's not clear from the macros, one character trait
%D of this macros, which are build on top of the support
%D module, is that they don't hyphenate. We therefore offer the
%D alternative \type{\typ}. The current
%D implementation works all right, but a decent hyphenation
%D support of \type{\tt} text will be implemented soon.

\def\obeyhyphens
  {\def\obeyedspace{\hskip\spaceskip}%
   \edef\savedfont{\the\font}%
   \ttsl\hyphenchar\font=45
   \tttf\hyphenchar\font=45
   \savedfont
   \spaceskip.5em\!!plus.25em\!!minus.25em\relax}

\def\obeybreakpoints
  {\def\obeyedspace{\hskip\spaceskip}%
   \spaceskip.5em
   \veryraggedright}

%\unexpanded\def\typ
%  {\bgroup
%   \obeyhyphens
%   \futurelet\next\dotype}

\unexpanded\def\typ
  {\bgroup
   \obeyhyphens
   \futurelet\next\dodotype}

%D \macros
%D   {tex,arg,mat,dis}
%D
%D Sometimes, for instance when we pass verbatim text as an
%D argument, the fixed \CATCODES\ interfere with our wishes. An
%D experimental implementation of character by character
%D processing of verbatim text did overcome this limitation,
%D but we've decided not to use that slow and sometimes
%D troublesome solution. Instead we stick to some 'old'
%D \CONTEXT\ macros for typesetting typical \TEX\ characters.
%D
%D The next implementation is more clear but less versatile,
%D so we treated it for a beter one.
%D
%D \starttypen
%D \def\dospecialtype#1#2%
%D   {\bgroup
%D    \initializetype
%D    \catcode`\{=\@@begingroup
%D    \catcode`\}=\@@endgroup
%D    \def\dospecialtype%
%D      {\def\dospecialtype{#2\egroup}%
%D       \bgroup
%D       \aftergroup\dospecialtype
%D       #1}%
%D    \afterassignment\dospecialtype
%D    \let\next=}
%D
%D \unexpanded\def\tex{\dospecialtype\texescape\relax}
%D \unexpanded\def\arg{\dospecialtype\leftargument\rightargument}
%D \unexpanded\def\mat{\dospecialtype\$\$}
%D \unexpanded\def\dis{\dospecialtype{\$\$}{\$\$}}
%D \stoptypen
%D
%  %D For sometime we used the better but less readable is 
%  %D alternative
%  %D 
%  %D \starttypen
%  %D \def\doprocessgroup#1#2#3%
%  %D   {\bgroup
%  %D    #1%
%  %D    \def\doprocessgroup%
%  %D      {\def\doprocessgroup{#3\egroup}%
%  %D       \bgroup
%  %D       \aftergroup\doprocessgroup
%  %D       #2}%
%  %D    \afterassignment\doprocessgroup
%  %D    \let\next=}
%  %D 
%  %D \def\setgroupedtype%
%  %D   {\initializetype
%  %D    \catcode`\{=\@@begingroup
%  %D    \catcode`\}=\@@endgroup}
%  %D 
%  %D \unexpanded\def\tex{\doprocessgroup\setgroupedtype\texescape\relax}
%  %D \unexpanded\def\arg{\doprocessgroup\setgroupedtype\leftargument\rightargument}
%  %D \unexpanded\def\mat{\doprocessgroup\setgroupedtype\$\$}
%  %D \unexpanded\def\dis{\doprocessgroup\setgroupedtype{\$\$}{\$\$}}
%  %D \stoptypen
%  %D 
%D
%D But since \type{\groupedcommand} became available, we use
%D however

\def\setgroupedtype
  {\initializetype
   \catcode`\{=\@@begingroup
   \catcode`\}=\@@endgroup}

\unexpanded\def\tex{\groupedcommand{\setgroupedtype\texescape}{\relax}}
\unexpanded\def\arg{\groupedcommand{\setgroupedtype\leftargument}{\rightargument}}
\unexpanded\def\mat{\groupedcommand{\setgroupedtype\$}{\$}}
\unexpanded\def\dis{\groupedcommand{\setgroupedtype\$\$}{\$\$}}

%D \macros
%D   {starttyping}
%D
%D Display verbatim is realized far more easy, which is mostly
%D due to the fact that we use \type{\stop...} as delimiter.
%D The implementation inherits  some features, for instance the
%D support of linenumbering, which can best be studied in the
%D documented support module. 

\def\initializetyping#1%
  {%\donefalse
   \switchtobodyfont[\getvalue{\??tp#1\c!korps}]%
   \donefalse
   \scratchskip\getvalue{\??tp#1\c!onevenmarge}\relax
   \ifzeropt\scratchskip\else\donetrue\fi
   \scratchskip\getvalue{\??tp#1\c!evenmarge}\relax
   \ifzeropt\scratchskip\else\donetrue\fi
   \ifdone
     \def\doopenupverbatimline%
       {\getpagestatus
        \ifrightpage 
          \hskip\getvalue{\??tp#1\c!onevenmarge}\relax
        \else
          \hskip\getvalue{\??tp#1\c!evenmarge}\relax
        \fi}%
   \else
     \doadaptleftskip{\getvalue{\??tp#1\c!marge}}%
   \fi
   \doifdefinedelse{\??bo\getvalue{\??tp#1\c!blanko}}
     {\edef\!!stringa{\csname\??bo\csname\??tp#1\c!blanko\endcsname\endcsname}}
     {\edef\!!stringa{\csname\??tp#1\c!blanko\endcsname}}%
   \processaction
     [\!!stringa]
     [\v!standaard=>\scratchskip\tussenwit,
          \v!klein=>\scratchskip\blankokleinmaat,
         \v!middel=>\scratchskip\blankomiddelmaat,
          \v!groot=>\scratchskip\blankogrootmaat,
     \v!halveregel=>\scratchskip.5\baselineskip,
          \v!regel=>\scratchskip\baselineskip,
           \v!geen=>\scratchskip\zeropoint,
        \s!unknown=>\scratchskip\commalistelement]%
   \ifgridsnapping
     \ifdim\scratchskip=.5\baselineskip\relax  
       \edef\verbatimbaselineskip{\the\scratchskip}% new 
     \else
       \edef\verbatimbaselineskip{\the\baselineskip}% 
     \fi
   \else
     \edef\verbatimbaselineskip{\the\scratchskip}% 
   \fi
   \setupcommonverbatim{\??tp#1}}

%D The basic display verbatim commands are defined in an
%D indirect way. As we will see, they are a specific case of a
%D more general mechanism.

\def\dostarttyping#1%
  {\getvalue{\??tp#1\c!voor}%
   \startopelkaar % includes \bgroup
   \initializetyping{#1}%
   \startverbatimcolor
   \expandafter\processdisplayverbatim\expandafter{\s!stop#1}}

\def\dostoptyping#1%
  {\stopverbatimcolor
   \stopopelkaar  % includes \egroup
   \getvalue{\??tp#1\c!na}%
   \doifvalue{\??tp#1\c!springvolgendein}\v!nee\noindentation}

%D \macros
%D   {setuptyping}
%D
%D The setup of typing accepts two arguments. The optional
%D first one identifies the user defined ones. If only one
%D argument is given, the values apply to both the standard
%D command \type{\starttyping} and \type{\typefile}.

%\indirect\def\setuptyping\dodoubleempty[#1][#2]%
%  {\iffirstargument
%     \getparameters[\??tp#1][#2]%
%   \else
%     \getparameters[\??tp][#1]%
%   \fi}
%
%\doubleemptied\def\setuptyping[#1][#2]%
%  {\iffirstargument
%     \getparameters[\??tp#1][#2]%
%   \else
%     \getparameters[\??tp][#1]%
%   \fi}

\def\dosetuptyping[#1][#2]%
  {\ifsecondargument
     \getparameters[\??tp#1][#2]%
   \else
     \getparameters[\??tp][#1]%
   \fi}

\def\setuptyping
  {\dodoubleempty\dosetuptyping}

%D The setups for display verbatim and file verbatim are
%D shared. One can adapt the extra defined typing environments,
%D but they also default to the values below. Watch the 
%D alternative escape character. 

\setuptyping
  [          \c!voor=\blanko,
               \c!na=\blanko,
            \c!korps=,
            \c!kleur=,
           \c!spatie=\v!uit,  
           \c!pagina=\v!nee,
              \c!tab=\s!ascii,
            \c!optie=\v!geen,
            \c!palet=colorpretty,
            \c!tekst=\v!nee,
           \c!letter=\tttf, 
        \c!icommando=\ttsl,
        \c!vcommando=,
        \c!ccommando=\tttf,
 \c!springvolgendein=\v!ja,
            \c!marge=\!!zeropoint,
        \c!evenmarge=\!!zeropoint, 
      \c!onevenmarge=\!!zeropoint,
           \c!blanko=\v!regel,
           \c!escape=/, % beware \string\ , should also be accepted
           \c!nummeren=\v!nee,
           \c!regels=]

%D \macros
%D   {definetype}
%D
%D Specific inline verbatim commands can be defined with the 
%D following command. 

\def\definetype%
  {\dodoubleempty\dodefinetype}

\def\dodefinetype[#1][#2]%
  {\unexpanded\setvalue{#1}{\dotype{#1}}%
   \copyparameters
     [\??ty#1][\??ty]
     [\c!spatie,\c!kleur,\c!letter,\c!pagina,\c!tab,\c!optie]%
   \getparameters[\??ty#1][#2]}

%D \macros
%D   {definetyping}
%D
%D For most users the standard \type{\start}||\type{\stop}||pair
%D will suffice, but for documentation purposes the next
%D definition command can be of use:
%D
%D \starttypen
%D \definetyping[extratyping][margin=3em]
%D
%D \startextratyping
%D these extra ones are indented by 1 em
%D \stopextratyping
%D \stoptypen
%D
%D The definitions default to the standard typing values. 

\def\presettyping[#1][#2]% 
  {\copyparameters
     [\??tp#1][\??tp]
     [\c!voor,\c!na,\c!spatie,\c!pagina,\c!tab,\c!kleur,\c!letter,
      \c!tekst,\c!icommando,\c!vcommando,\c!ccommando,\c!korps,
      \c!optie,\c!palet,\c!marge,\c!evenmarge,\c!onevenmarge,
      \c!springvolgendein,\c!blanko,\c!escape,\c!regels,\c!nummeren]%
   \getparameters
     [\??tp#1]
     [#2]}

\def\dodefinetyping[#1][#2]%
  {\setvalue{\e!start#1}{\dostarttyping{#1}}%
   \setvalue{\e!stop #1}{\dostoptyping {#1}}%
   \presettyping[#1][#2]}

\def\definetyping
  {\dodoubleempty\dodefinetyping}

\definetyping[\v!typen]

\definetyping[RAW]   [\c!optie=RAW]
\definetyping[MP]    [\c!optie=MP]
\definetyping[PL]    [\c!optie=PL]
\definetyping[PM]    [\c!optie=PL]
\definetyping[JS]    [\c!optie=JS]
\definetyping[JV]    [\c!optie=JV]
\definetyping[SQL]   [\c!optie=SQL]
\definetyping[TEX]   [\c!optie=TEX]
\definetyping[PAS]   [\c!optie=PAS]
\definetyping[PASCAL][\c!optie=PAS]
\definetyping[MOD]   [\c!optie=PAS]
\definetyping[MODULA][\c!optie=PAS]
\definetyping[DELPHI][\c!optie=PAS]
\definetyping[EIFFEL][\c!optie=EIF]
\definetyping[XML]   [\c!optie=XML]

%D We use the \CONTEXT\ color system for switching to and from
%D color mode. We can always redefine these colors afterwards.

\definecolor [colorprettyone]   [r=.9, g=.0, b=.0] % red
\definecolor [colorprettytwo]   [r=.0, g=.8, b=.0] % green
\definecolor [colorprettythree] [r=.0, g=.0, b=.9] % blue
\definecolor [colorprettyfour]  [r=.8, g=.8, b=.6] % yellow

\definecolor [grayprettyone]    [s=.30]
\definecolor [grayprettytwo]    [s=.45]
\definecolor [grayprettythree]  [s=.60]
\definecolor [grayprettyfour]   [s=.75]

\definepalet
  [colorpretty]
  [  prettyone=colorprettyone,
     prettytwo=colorprettytwo,
   prettythree=colorprettythree,
    prettyfour=colorprettyfour]

\definepalet
  [graypretty]
  [  prettyone=grayprettyone,
     prettytwo=grayprettytwo,
   prettythree=grayprettythree,
    prettyfour=grayprettyfour]

% ---> naar verb-* ? of predefine als undefined? 

\definepalet [TEXcolorpretty]    [colorpretty]
\definepalet [TEXgraypretty]     [graypretty]
\definepalet [PLcolorpretty]     [colorpretty]
\definepalet [PLgraypretty]      [graypretty]
\definepalet [PMcolorpretty]     [colorpretty]
\definepalet [PMgraypretty]      [graypretty]
\definepalet [MPcolorpretty]     [colorpretty]
\definepalet [MPgraypretty]      [graypretty]
\definepalet [JVcolorpretty]     [colorpretty]
\definepalet [JVgraypretty]      [graypretty]
\definepalet [JScolorpretty]     [colorpretty]
\definepalet [JSgraypretty]      [graypretty]
\definepalet [SQLcolorpretty]    [colorpretty]
\definepalet [SQLgraypretty]     [graypretty]
\definepalet [PAScolorpretty]    [colorpretty]
\definepalet [PASgraypretty]     [graypretty]
\definepalet [EIFcolorpretty]    [colorpretty]
\definepalet [EIFgraypretty]     [graypretty]
\definepalet [XMLcolorpretty]    [colorpretty]
\definepalet [XMLgraypretty]     [graypretty]

% Some real in-depth knowlegde of palets: `palet:color' it is!

%D We can use some core color commands. These are faster than
%D the standard color switching ones and work ok on a line by 
%D line basis.
%D
%D \starttypen
%D \def\setupverbatimcolor#1%
%D   {\edef\prettypalet{\prettyidentifier\getvalue{#1\c!palet}}%
%D    \def\beginofpretty[##1]{\startcolormode{\prettypalet:##1}}%
%D    \def\endofpretty       {\stopcolormode}}
%D \stoptypen
%D
%D Since we support a global color too, the folowing 
%D definition is better: 

\def\setupverbatimcolor#1% fast and local versus slow and global 
  {\doifelsevaluenothing{#1\c!kleur} 
     {\def\beginofpretty[##1]{\startcolormode{\prettypalet:##1}}%
      \let\endofpretty        \restorecolormode % \stopcolormode
      \let\startverbatimcolor \relax
      \let\stopverbatimcolor  \relax
      \let\verbatimcolor      \relax}
     {\def\beginofpretty[##1]{\startcolor[\prettypalet:##1]}% 
      \let\endofpretty        \stopcolor
      \def\startverbatimcolor{\startcolor[\getvalue{#1\c!kleur}]}%
      \let\stopverbatimcolor  \stopcolor
      \def\verbatimcolor     {\getvalue{\getvalue{#1\c!kleur}}}}%
   \doifelsevaluenothing{#1\c!palet}
     {\let\prettypalet\empty
      \let\endofpretty\relax
      \def\beginofpretty[##1]{}}
     {\edef\prettypalet{\prettyidentifier\getvalue{#1\c!palet}}}}

\let\prettypalet       \empty
\let\startverbatimcolor\relax
\let\stopverbatimcolor \relax
\let\verbatimcolor     \relax

%D In the verbatim module, there are some examples given of 
%D the more obscure features of the verbatim environments.
%D
%D \startbuffer
%D \startTEX
%D \def\mathematics#1%         % usage: \type {\mathematics{x^2}}
%D   {\ifmmode#1\else$#1$\fi}  % becomes: \mathematics{x^2}
%D \stopTEX
%D \stopbuffer
%D 
%D \typebuffer 
%D 
%D This gives, as can be expected: 
%D 
%D \haalbuffer
%D   
%D When we want to see some typeset \TEX\ too, we can say: 
%D
%D \startbuffer
%D \startTEX
%D \def\mathematics#1%         %%\ N  usage: \type {\mathematics{x^2}}
%D   {\ifmmode#1\else$#1$\fi}  %%\ N  becomes: \mathematics{x^2}
%D \stopTEX
%D \stopbuffer
%D
%D \typebuffer 
%D
%D or: 
%D
%D \haalbuffer
%D
%D In a similar way: 
%D
%D \startbuffer
%D \startSQL
%D select *       -- indeed, here we {\em do} select 
%D   from tableA
%D   where 1 = 2
%D \stopSQL
%D \stopbuffer
%D
%D \typebuffer 
%D
%D gives: 
%D
%D \haalbuffer 
%D
%D The next examples sow how we can directly call for natural
%D \TEX\ comments:  
%D 
%D \startbuffer
%D \setuptyping
%D   [TEX]
%D   [tekst=ja]
%D 
%D \startTEX
%D \def\mathematics#1%         % usage: \type {\mathematics{x^2}}
%D   {\ifmmode#1\else$#1$\fi}  % becomes: \mathematics{x^2}
%D \stopTEX
%D 
%D \setuptyping
%D   [SQL]
%D   [tekst=ja,palet=,icommando=\bf,vcommando=,ccommando=\it]
%D 
%D \startSQL
%D select *       -- indeed, here we {\em do} select 
%D   from tableA
%D   where 1 = 2
%D \stopSQL
%D 
%D \setuptyping
%D   [SQL]
%D   [ccommando=\tf\underbar]
%D 
%D \startSQL
%D select *       -- indeed, here we {\em do} select 
%D   from tableA
%D   where 1 = 2
%D \stopSQL
%D \stopbuffer
%D
%D \typebuffer
%D
%D Now watch: 
%D
%D \haalbuffer
%D
%D The natural \TEX\ typesetting was introduced when Tobias 
%D and Berend started using verbatim \JAVASCRIPT\ and \SQL.

%D \macros
%D   {EveryPar, EveryLine, iflinepar}
%D
%D One of the features of these commands is the support of
%D \type{\EveryPar}, \type{\EveryLine} and \type{\iflinepar}.
%D In the documentation of the verbatim support module we give
%D some examples of line- and paragraph numbering using these
%D macros.

%D \macros
%D   {typefile}
%D
%D Typesetting files verbatim (for the moment) only supports
%D colorization of \TEX\ sources as valid option. The other
%D setup values are inherited from display verbatim.
%D The implementation of \type{\typefile} is straightforward:

% \presettyping[\v!file][]
% 
% \definecomplexorsimple\typefile
% 
% \def\simpletypefile
%   {\complextypefile[\v!file]}
% 
% % \def\complextypefile[#1]#2%
% %   {\getvalue{\??tp#1\c!voor}%
% %    \doiflocfileelse{#2}
% %      {\startopelkaar % includes \bgroup
% %       \doifinset{\getvalue{\??tp#1\c!optie}}{\v!commandos,\v!schuin,\v!normaal}
% %         {\setuptyping[#1][\c!optie=\v!geen]}%
% %       \doifvalue{\??tp#1\c!optie}{\v!kleur}
% %         {\expandafter\aftersplitstring#2\at.\to\prettyidentifier
% %          \letvalue{\??tp#1\c!optie}=\prettyidentifier}%
% %       \initializetyping{#1}%
% %       \startverbatimcolor
% %       \processfileverbatim{\pathplusfile\f!currentpath{#2}}%
% %       \stopverbatimcolor
% %       \stopopelkaar}  % includes \egroup
% %      {{\tttf[\makemessage{\m!verbatims}{1}{#2}]}% 
% %       \showmessage{\m!verbatims}{1}{#2}}%
% %    \getvalue{\??tp#1\c!na}}
% 
% \def\complextypefile[#1]#2%
%   {\getvalue{\??tp#1\c!voor}%
%    \doiflocfileelse{#2}
%      {\startopelkaar % includes \bgroup
%       \doifinset{\getvalue{\??tp#1\c!optie}}{\v!commandos,\v!schuin,\v!normaal}
%         {\setuptyping[#1][\c!optie=\v!geen]}%
%       \doifvalue{\??tp#1\c!optie}\v!kleur
%         {\expandafter\aftersplitstring#2\at.\to\prettyidentifier
%          \letvalue{\??tp#1\c!optie}\prettyidentifier}%
%       \initializetyping{#1}%
%       \startverbatimcolor
%       \makelocreadfilename{#2}%
%       \processfileverbatim\readfilename 
%      %\processfileverbatim{\pathplusfile\f!currentpath{#2}}%
%       \stopverbatimcolor
%       \stopopelkaar}  % includes \egroup
%      {\bgroup
%       \expanded{\convertargument#2}\to\ascii
%       \tttf[\makemessage\m!verbatims1\ascii]% 
%       \showmessage\m!verbatims1\ascii
%       \egroup}%
%    \getvalue{\??tp#1\c!na}}

% new feature (not yet 100\% ok) 
%
% \setuptyping[file][numbering=file]
%
% \typefile[start=2,nlines=3]{zapf}
% \typefile[start=continue,nlines=13]{zapf}
% \typefile{zapf}
%
% \setuptyping[file][numbering=line]
%
% \typefile[start=4,step=3]{zapf}
% \typefile{zapf}

\presettyping[\v!file][]

\def\typefile
  {\dodoubleempty\dotypefile}

\def\dotypefile[#1][#2]#3%
  {\ifsecondargument
     \dodotypefile[#1][#2]{#3}%
   \else\iffirstargument
     \doifassignmentelse{#1}
       {\dodotypefile[\v!file][#1]{#3}}
       {\dodotypefile[#1][]{#3}}%
   \else
     \dodotypefile[\v!file][]{#3}%
   \fi\fi}

\def\dosetuptypelinenumbering#1#2%
  {\setuptyping[#1][\c!start=,\c!stop=,\c!stap=,\c!nregels=,#2]%
   \doifelsevalue{\??tp#1\c!nummeren}\v!file
     {\stelregelnummerenin[\c!methode=\v!file]%
      \donetrue}
     {\doifelsevalue{\??tp#1\c!nummeren}\v!regel
        {% \stelregelnummerenin defaults start/step to 1/1, so we need
         \doifvaluenothing{\??tp#1\c!start}{\setvalue{\??tp#1\c!start}{1}}%
         \doifvaluenothing{\??tp#1\c!stap }{\setvalue{\??tp#1\c!stap }{1}}%
         \stelregelnummerenin
           [\c!methode=\v!type,
            \c!start=\getvalue{\??tp#1\c!start},
            \c!stap=\getvalue{\??tp#1\c!stap}]%
         \donetrue}
        {\donefalse}}%
   \ifdone
     \def\beginofverbatimlines{\startregelnummeren}%
     \def\endofverbatimlines  {\stopregelnummeren }%
   \fi}

\def\dodotypefile[#1][#2]#3%
  {\getvalue{\??tp#1\c!voor}%
   \doiflocfileelse{#3}
     {\startopelkaar % includes \bgroup
      \dosetuptypelinenumbering{#1}{#2}%
      \doifinset{\getvalue{\??tp#1\c!optie}}{\v!commandos,\v!schuin,\v!normaal}
        {\setuptyping[#1][\c!optie=\v!geen]}%
      \doifvalue{\??tp#1\c!optie}\v!kleur
        {\expandafter\aftersplitstring#3\at.\to\prettyidentifier
         \letvalue{\??tp#1\c!optie}\prettyidentifier}%
      \initializetyping{#1}%
      \startverbatimcolor
      \makelocreadfilename{#3}%
      \doifundefinedelse{\??tp#3\v!globaal\c!start}
        {\scratchcounter\zerocount}
        {\scratchcounter\getvalue{\??tp#3\v!globaal\c!start}}%
      \advance\scratchcounter\plusone
      \setxvalue{\??tp#3\v!globaal\c!start}{\the\scratchcounter}%
      \doifelsevaluenothing{\??tp#1\c!start}
        {\processfileverbatim\readfilename}
        {\doifvalue{\??tp#1\c!start}\v!verder
           {\setevalue{\??tp#1\c!start}%
              {\getvalue{\??tp#3\v!globaal\c!start}}}%
         \doifelsevaluenothing{\??tp#1\c!stop}
           {\doifelsevaluenothing{\??tp#1\c!nregels}
              {\processfileverbatim\readfilename}
              {\scratchcounter\getvalue{\??tp#1\c!start}%
               \advance\scratchcounter\getvalue{\??tp#1\c!nregels}%
               \advance\scratchcounter\minusone
               \setxvalue{\??tp#3\v!globaal\c!start}%
                 {\the\scratchcounter}%
               \processfilelinesverbatim\readfilename
                 {\getvalue{\??tp#1\c!start}}
                 {\getvalue{\??tp#3\v!globaal\c!start}}}}%
           {\processfilelinesverbatim\readfilename
              {\getvalue{\??tp#1\c!start}}
              {\getvalue{\??tp#1\c!stop }}}}%
      \stopverbatimcolor
      \stopopelkaar}  % includes \egroup
     {\bgroup
      \expanded{\convertargument#3}\to\ascii
      \tttf[\makemessage\m!verbatims1\ascii]%
      \showmessage\m!verbatims1\ascii
      \egroup}%
   \getvalue{\??tp#1\c!na}}

%D \macros 
%D   {filename}
%D
%D Typesetting filenames in monospaced fonts is possible with
%D  
%D \starttypen
%D \filename{here/there/filename.suffix}
%D \stoptypen
%D
%D The definition is not that spectacular. 

\unexpanded\def\filename#1{{\tttf\hyphenatedfile{#1}}}

%D This leaves some settings: 

\permitshiftedendofverbatim
\optimizeverbatimtrue

%D And a bonus macro: 

\def\verbatim#1{\convertargument#1\to\ascii\ascii}

\protect \endinput
