%D \module
%D   [       file=buff-ver, % was core-ver
%D        version=2000.05.09,
%D          title=\CONTEXT\ Buffer Macros,
%D       subtitle=Verbatim,
%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.

% check after-first and before-last breaks

\writestatus{loading}{ConTeXt Buffer Macros / Verbatim}

\registerctxluafile{buff-ver}{}

\unprotect

\startcontextdefinitioncode

\definesystemattribute[verbatimline][public]

\appendtoksonce
    \c_attr_verbatimline\attributeunsetvalue
\to \everyforgetall

%D Initializations.

\installcorenamespace{typinglines}
\installcorenamespace{typingspace}
\installcorenamespace{typingblank} % needs checking ... used?

\newtoks\everyinitializeverbatim

\appendtoks
    \the\everyresettypesetting
    \resetfontfeature
    \resetcharacterspacing
    \resetbreakpoints
    \nohyphencollapsing
\to \everyinitializeverbatim

\permanent\protected\def\setverbatimspaceskip % to be checked: must happen after font switch
  {\spaceskip\fontcharwd\font`x\relax
   \xspaceskip\spaceskip}

\defcsname\??typinglines\v!no        \endcsname{\buff_verbatim_ignore_hyphens}
\defcsname\??typinglines\v!normal    \endcsname{\buff_verbatim_ignore_hyphens}
\defcsname\??typinglines\v!yes       \endcsname{\buff_verbatim_obey_breakpoints}
\defcsname\??typinglines\v!hyphenated\endcsname{\buff_verbatim_obey_hyphens}

\defcsname\??typingspace\v!on        \endcsname{\enforced\let\obeyedspace\specialcontrolspace}
\defcsname\??typingspace\v!stretch   \endcsname{\enforced\let\obeyedspace\specialstretchedspace}
\letcsname\??typingspace\v!normal    \endcsname\donothing
\defcsname\??typingspace\v!fixed     \endcsname{\enforced\let\obeyedspace\specialfixedspace}

\defcsname\??typingblank\v!standard  \endcsname{\s_spac_whitespace_parskip}
\defcsname\??typingblank\v!small     \endcsname{\smallskipamount}
\defcsname\??typingblank\v!medium    \endcsname{\medskipamount}
\defcsname\??typingblank\v!big       \endcsname{\bigskipamount}
\defcsname\??typingblank\v!halfline  \endcsname{.5\baselineskip}
\defcsname\??typingblank\v!line      \endcsname{\baselineskip}
\defcsname\??typingblank\v!none      \endcsname{\zeropoint}

\protected\def\buff_verbatim_obey_hyphens
  {}

\protected\def\buff_verbatim_obey_breakpoints
  {\nohyphens
   \veryraggedright}

\protected\def\buff_verbatim_ignore_hyphens
  {\nohyphens}

\def\buff_verbatim_initialize_type_one
  {\enforced\let\obeylines\ignorelines
   \edef\p_buff_option{\typeparameter\c!option}%
   \ifx\p_buff_option\v!none
     \usetypestyleandcolor\c!style\c!color
   \orelse\ifempty\p_buff_option
     \usetypestyleandcolor\c!style\c!color
   \else
     \usetypestyleparameter\c!style % no color
   \fi
   \setcatcodetable\vrbcatcodes}

\def\buff_verbatim_initialize_type_two
  {\spaceskip.5\emwidth\relax
   \enforced\let\obeyedspace\specialobeyedspace
   \enforced\let\controlspace\specialcontrolspace
   \edef\p_buff_lines{\typeparameter\c!lines}%
   \begincsname\??typinglines\p_buff_lines\endcsname
   \edef\p_buff_space{\typeparameter\c!space}%
   \begincsname\??typingspace\p_buff_space\endcsname
   \relax\the\everyinitializeverbatim\relax}

\permanent\protected\def\doinitializeverbatim % for use elsewhere .. temp hack (see lxml-ini)
  {\buff_verbatim_initialize_type_one
   \buff_verbatim_initialize_type_two}

\let\buff_verbatim_set_line_margin\relax

\def\buff_verbatim_set_line_margin_indeed
  {\hskip\doifelseoddpage{\typingparameter\c!oddmargin}{\typingparameter\c!evenmargin}\relax}

\def\buff_verbatim_initialize_typing_one
  {\usebodyfontparameter\typingparameter
   \scratchskip\typingparameter\c!oddmargin\relax
   \ifzeropt\scratchskip \else
     \let\buff_verbatim_set_line_margin\buff_verbatim_set_line_margin_indeed
   \fi
   \scratchskip\typingparameter\c!evenmargin\relax
   \ifzeropt\scratchskip \else
     \let\buff_verbatim_set_line_margin\buff_verbatim_set_line_margin_indeed
   \fi
   \ifrelax\buff_verbatim_set_line_margin
     \doadaptleftskip{\typingparameter\c!margin}%
   \fi
   \edef\p_buff_option{\typingparameter\c!option}%
   \ifx\p_buff_option\v!none
     \usetypingstyleandcolor\c!style\c!color
   \orelse\ifempty\p_buff_option
     \usetypingstyleandcolor\c!style\c!color
   \else
     \usetypingstyleparameter\c!style % no color !
   \fi
   \usealignparameter\typingparameter}

\def\buff_verbatim_initialize_typing_two
  {\spaceskip.5\emwidth\relax
   \enforced\let\obeyedspace\specialobeyedspace
   \enforced\let\controlspace\specialcontrolspace
   \edef\p_buff_lines{\typingparameter\c!lines}%
   \begincsname\??typinglines\p_buff_lines\endcsname
   \edef\p_buff_space{\typingparameter\c!space}%
   \begincsname\??typingspace\p_buff_space\endcsname
   \relax\the\everyinitializeverbatim\relax}

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

\installcorenamespace{type}

\installcommandhandler \??type {type} \??type

\appendtoks
    \frozen\instance\setuevalue{\currenttype}{\buff_verbatim_type{\currenttype}}%
\to \everydefinetype

\appendtoks
   \buff_verbatim_initialize_visualizer{\typeparameter\c!option}%
\to \everysetuptype

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

\installcorenamespace{typing}

\installcommandhandler \??typing {typing} \??typing

\appendtoks
    \frozen\instance\setuevalue{\e!start\currenttyping}{\buff_verbatim_typing_start{\currenttyping}}%
    \frozen\instance\setuevalue{\e!stop \currenttyping}{\buff_verbatim_typing_stop {\currenttyping}}%
    \ifempty\currenttypingparent
      \normalexpanded{\definelinenumbering[\currenttyping]}%
    \else
      \normalexpanded{\definelinenumbering[\currenttyping][\currenttypingparent]}%
    \fi
\to \everydefinetyping

\appendtoks
   \buff_verbatim_initialize_visualizer{\typingparameter\c!option}% we can check at the tex end
\to \everysetuptyping

\def\buff_verbatim_initialize_visualizer#1%
  {\ifproductionrun\clf_loadvisualizer{#1}\fi}

\permanent\protected\def\doifelsevisualizer#1%
  {\clf_doifelsevisualizer{#1}}

\aliased\let\doifvisualizerelse\doifelsevisualizer

%D Verbatim command are very sensitive to argument processing, which is a direct
%D result of the \CATCODES\ being fixed at reading time.
%D
%D Typesetting inline verbatim can be accomplished by \type {\type}, which in this
%D sentence was typeset by saying just \type {\type {\type}}, which in turn was
%D typeset by \unknown. Using the normal grouping characters \type {{}} is the most
%D natural way of using this command.
%D
%D A second, more or less redundant, alternative is delimiting the argument with an
%D own character. This method was implemented in the context of a publication in the
%D \MAPS, where this way of delimiting is recognized by \LATEX\ users.
%D
%D The third, more original alternative, is the one using \type {<<} and \type {>>}
%D as delimiters. This alternative can be used in situations where slanted
%D typeseting is needed.
%D
%D \macros
%D   {type}
%D
%D We define \type {\type} as a protected command. This command has several
%D invocations: grouped, wirt boundary characters, and with font switches.
%D
%D \starttyping
%D normal: \par \type{xx<<..xx..<<xx <<xx>> >>..>>xx} \par \type<<....>> \par \type<<..<<xx>>..>> \par
%D normal: \par \type{xx<..xx..<xx <slanted> >..>xx} \par \type{<....>} \par \type{<..<xx>..>}
%D \setuptype[option=slanted]
%D slanted: \par \type{xx<<..sl..<<xx <<sl>> xx>>..sl..>>xx} \par \type<<..xx..>> \par \type<<..<<sl>>..>> \par
%D slanted: \par \type{xx<<..sl..<xx <sl> xx>..sl..>>xx} \par \type<<..xx..>> \par \type<<..<sl>..>> \par
%D \setuptype[option=none]
%D none: \par \type{xx<<..xx..<<xx <<xx>> >>..>>xx} \par \type<<....>> \par \type<<..<<xx>>..>> \par
%D \stoptyping
%D
%D When writing the manual to \CONTEXT\ and documenting this source we needed to
%D typeset \type {<<} and \type {>>}. Because we wanted to do this in the natural
%D way, we've adapted the original definition a bit. This implementation went
%D through several live cycles. The final implementation looks a bit further and
%D treats the lone \type {<<} and \type {>>} a bit different. The \type {\null}
%D prevents ligatures, which unfortunately turn up in Lucida fonts.
%D
%D The following lines show what happens when we set \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

% Beware, the command is supposed to do something like \framed and not manipulate
% content i.e. it gets no argument but something b/egrouped instead:

\def\buff_verbatim_left_of_type
  {\typeparameter\c!left
   \typeparameter\c!command\bgroup}

\def\buff_verbatim_right_of_type
  {\egroup
   \typeparameter\c!right}

\permanent\protected\def\type{\buff_verbatim_type\empty}
\permanent\protected\def\typ {\buff_verbatim_typ \empty}

\protected\def\buff_verbatim_type#1%
  {\dontleavehmode
   \bgroup
   \edef\currenttype{#1}%
   \buff_verbatim_type_indeed}

\def\buff_verbatim_typ#1%
  {\dontleavehmode
   \bgroup
   \edef\currenttype{#1}%
   \lettypeparameter\c!lines\v!hyphenated
   \enforced\let\specialobeyedspace\specialstretchedspace
   \buff_verbatim_type_indeed}

\tolerant\def\buff_verbatim_type_indeed[#1]%
  {\ifarguments\or\setupcurrenttype[#1]\fi
   \buff_verbatim_left_of_type
   \futureexpand\bgroup\buff_verbatim_type_a\buff_verbatim_type_one}

\def\buff_verbatim_type_one
  {\catcode`<=\othercatcode % old precaution
   \catcode`>=\othercatcode % old precaution
   \futureexpandis<\buff_verbatim_type_b\buff_verbatim_type_c}

\def\buff_verbatim_type_three
  {\if\next<%
     \expandafter\buff_verbatim_type_b
   \else
     \expandafter\buff_verbatim_type_c
   \fi}

\def\buff_verbatim_type_a
  {\buff_verbatim_initialize_type_one
   \setcatcodetable\tpacatcodes
   \buff_verbatim_type_normal}

\def\buff_verbatim_type_b#1%
  {\buff_verbatim_initialize_type_one
   \setcatcodetable\tpbcatcodes
   \buff_verbatim_type_nested}

\def\buff_verbatim_type_c#1%
  {\buff_verbatim_initialize_type_one
   \setcatcodetable\vrbcatcodes
   \def\next##1#1{\buff_verbatim_type_normal{##1}}%
   \next}

\def\buff_verbatim_type_normal#1%
  {\buff_verbatim_initialize_type_two
   \dostarttaggedchained\t!verbatim\currenttype\??type
   \clf_type
     data    {\detokenize{#1}}%
     tab     {\typeparameter\c!tab}%
     method  {\p_buff_option}%
     compact {\typeparameter\c!compact}% % none | all | last (all needed in tabulate etc for manuals)
     escape  {\typeparameter\c!escape}%  % new but rather useless imo (escaping in general is not used much)
   % nature  {inline}% is default
   \relax
   \dostoptagged
   \buff_verbatim_right_of_type
   \egroup}

\def\buff_verbatim_type_nested#1%
  {\buff_verbatim_initialize_type_two
   \dostarttaggedchained\t!verbatim\currenttype\??type
   \clf_type
     data   {\detokenize{#1}}%
     tab    {\typeparameter\c!tab}%
     method {\p_buff_option}% % extra visualizer (maybe: nested,\typeparameter\c!option)
     escape {\typeparameter\c!escape}%  % new but rather useless imo (escaping in general is not used much)
   % nature {inline}% is default
     method {nested}%
   \relax
   \dostoptagged
   \buff_verbatim_right_of_type
   \egroup
   \gobbleoneargument} % grab last >

%D The \type {compact} option can come in handy in the case of inline argument passing
%D
%D \starttyping
%D \definetype[TeXcodeA][option=TEX]
%D \definetype[TeXcodeB][option=TEX,compact=all]
%D \definetype[TeXcodeC][option=TEX,compact=absolute]
%D
%D \def\argA#1{\TeXcodeA{{#1}}}
%D \def\argB#1{\TeXcodeB{{#1}}}
%D \def\argC#1{\TeXcodeC{{#1}}}
%D \stoptyping

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

\permanent\protected\def\specialfixedspace     {\kern\interwordspace\relax}
\permanent\protected\def\specialobeyedspace    {\hskip\interwordspace\relax} % better than spaceskip
\permanent\protected\def\specialstretchedspace {\hskip.5\interwordspace\s!plus.125\interwordspace\relax} % \interwordstretch can be zero
\permanent\protected\def\specialcontrolspace   {\normalcontrolspace\allowbreak} % uses fallback

\permanent\protected\def\explicitfixedspace    {\asciispacechar}
\permanent\protected\def\explicitobeyedspace   {\asciispacechar\allowbreak}
\permanent\protected\def\explicitstretchedspace{\asciispacechar\hskip\zeropoint\s!plus.125\interwordspace\relax}%
\permanent\protected\def\explicitcontrolspace  {\optionalcontrolspace\allowbreak} % uses asciispace

\appendtoks
    \enforced\protected\def\obeyedspace{\hskip\zeropoint\relax\asciispacechar\hskip\zeropoint\relax}%
\to \everyenableelements

\permanent\protected\def\obeyhyphens
  {\enforced\let\obeyedspace \specialobeyedspace % maybe \specialstretchedspace
   \enforced\let\controlspace\specialcontrolspace
   \spaceskip.25\emwidth\relax} % hm a bit of stretch !

\permanent\protected\def\obeybreakpoints
  {\ignorehyphens
   \veryraggedright}

\permanent\protected\def\ignorehyphens
  {\nohyphens
   \enforced\let\obeyedspace \specialobeyedspace
   \enforced\let\controlspace\specialcontrolspace
   \spaceskip.5\emwidth\relax}

%D \macros
%D   {tex,arg,mat,dis,astype}
%D
%D Sometimes, for instance when we pass verbatim text as an argument, the fixed
%D \CATCODES\ interfere with our wishes. An experimental implementation of character
%D by character processing of verbatim text did overcome this limitation, but we've
%D decided not to use that slow and sometimes troublesome solution. Instead we stick
%D to some 'old' \CONTEXT\ macros for typesetting typical \TEX\ characters.

\immutable\def\lesscharacter    {<} % obsolete
\immutable\def\morecharacter    {>} % obsolete

\immutable\let\texescape        \textbackslash
\immutable\let\leftargument     \textbraceleft
\immutable\let\rightargument    \textbraceright
\immutable\let\inlinemathmarker \textdollar
\immutable\def\displaymathmarker{\textdollar\textdollar}

\def\buff_verbatim_special_type#1#2#% # gobbles spaces
  {\dontleavehmode\bgroup
   \buff_verbatim_initialize_type_one
   \catcode\leftbraceasciicode \begingroupcatcode
   \catcode\rightbraceasciicode\endgroupcatcode
   #1%
   \bgroup % else wrong font for #2
   \aftergroup#2%
   \aftergroup\egroup
   \let\nexttoken}

\mutable\protected\def\tex{\buff_verbatim_special_type\texescape        \relax}
\mutable\protected\def\arg{\buff_verbatim_special_type\leftargument     \rightargument}
\mutable\protected\def\mat{\buff_verbatim_special_type\inlinemathmarker \inlinemathmarker}
\mutable\protected\def\dis{\buff_verbatim_special_type\displaymathmarker\displaymathmarker}

\aliased\let\normaltexttex\tex
\aliased\let\normaltextarg\arg
\aliased\let\normaltextmat\mat
\aliased\let\normaltextdis\dis

\permanent\protected\def\astype
  {\dontleavehmode
   \bgroup\usetypestyleandcolor\c!style\c!color\let\nexttoken}

\permanent\protected\def\asciistr#1% used in some old styles
  {\dontleavehmode\begingroup
   \dostarttagged\t!verbatim\empty
   \usetypestyleandcolor\c!style\c!color\detokenize{#1}%
   \dostoptagged
   \endgroup}

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

\mutable\let\beginofverbatimlines\relax % hooks
\mutable\let\endofverbatimlines  \relax % hooks

\protected\def\buff_verbatim_typing_start#1% tricky non standard lookahead
  {\begingroup
   \edef\currenttyping{#1}%
   \obeylines
   \futureexpandis[\buff_verbatim_typing_start_yes\buff_verbatim_typing_start_nop}

\def\buff_verbatim_typing_start_nop
  {\typingparameter\c!before\relax
   \startpacked[\v!blank]%
   \buff_verbatim_setup_line_numbering
   \buff_verbatim_initialize_typing_one
   \buff_verbatim_setup_keep_together
   \normalexpanded{\buff_verbatim_type_block{\e!start\currenttyping}{\e!stop\currenttyping}}}

\def\buff_verbatim_typing_start_yes[#1]%
  {\typingparameter\c!before\relax
   \startpacked[\v!blank]%
   \ifcondition\validassignment{#1}%
     \setupcurrenttyping[#1]%
   \else
     \doif\v!continue{#1}{\lettypingparameter\c!continue\v!yes}%
   \fi
   \buff_verbatim_setup_line_numbering
   \buff_verbatim_initialize_typing_one
   \buff_verbatim_setup_keep_together
   \normalexpanded{\buff_verbatim_type_block{\e!start\currenttyping}{\e!stop\currenttyping}}}

% \startnamedtyping[#1]

\protected\def\buff_verbatim_setup_keep_together
  {\edef\p_keeptogether{\typingparameter\c!keeptogether}%
   \ifx\p_keeptogether\v!yes
     \settrue \c_buff_optimize_linebreaks
   \else
     \setfalse\c_buff_optimize_linebreaks
   \fi}

\protected\def\buff_verbatim_type_block#1#2%
  {\edef\p_strip{\typingparameter\c!strip}%
   \normalexpanded{\buff_pickup
     {_typing_}%
     {#1}%
     {#2}%
     {}%
     {\buff_verbatim_type_block_verbatim_indeed{#1}{#2}}%
     \ifx\p_strip\v!no\zerocount\else\plusone\fi}}

\protected\def\buff_verbatim_type_block_verbatim_indeed#1#2%
  {\buff_verbatim_initialize_typing_two
   \dostarttaggedchained\t!verbatimblock\currenttyping\??typing
   \beginofverbatimlines
   \dostarttagged\t!verbatimlines\empty
   \clf_typebuffer
     name    {_typing_}%
     strip   {\typingparameter\c!strip}%
     range   {\typingparameter\c!range}%
     tab     {\typingparameter\c!tab}%
     method  {\p_buff_option}%
     escape  {\typingparameter\c!escape}%
     nature  {display}%
   \relax
   \dostoptagged
   \endofverbatimlines
   \dostoptagged
   \begincsname#2\endcsname}

\protected\def\buff_verbatim_typing_stop#1% hm, currenttyping
  {\stoppacked
   \typingparameter\c!after\relax
   \useindentnextparameter\typingparameter
   \endgroup
   \dorechecknextindentation}

%D Line numbering for files is combined with filtering, while display verbatim has
%D the ability to continue.
%D
%D \starttyping
%D \typefile[numbering=file,start=10,stop=12]{test.tex}
%D
%D \definetyping[code][numbering=line]
%D
%D \starttext
%D \startcode
%D ...
%D ...
%D \stopcode
%D
%D \startcode[start=continue]
%D ...
%D ...
%D \stopcode
%D
%D \startcode[start=10]
%D ...
%D \stopcode
%D \stoptyping

%D Ranges:
%D
%D \starttyping
%D % label:start:one
%D
%D \def\MyMacro
%D   {just an example}
%D
%D % label:stop:one
%D
%D \starttext
%D
%D % \typefile[file][range={3,6}] {whatever.tex}
%D % \typefile[file][range={3,+2}]{whatever.tex}
%D
%D \typefile[file][range={label:start:one,label:stop:one}]{whatever.tex}
%D
%D \stoptext
%D \stoptyping

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

% [category] [settings] {name} % for historic reasons, all filenames are {}

\mutable\let\currenttypingfile\empty

\appendtoks
    \frozen\instance\setuevalue{\e!type\currenttyping\v!file}{\typefile[\currenttyping]}%
\to \everydefinetyping

\permanent\tolerant\protected\def\typefile[#1]#*[#2]#:#3%
  {\begingroup
   \ifparameter#2\or
     \setuptyping[#1][#2]%
     \buff_verbatim_type_file_checked{#1}{#3}%
   \orelse\ifparameter#1\or
     \ifhastok={#1}%
       \setuptyping[\v!file][#1]%
       \buff_verbatim_type_file_checked\v!file{#3}%
     \else
       \buff_verbatim_type_file_checked{#1}{#3}%
     \fi
   \else
     \buff_verbatim_type_file_checked\v!file{#3}%
   \fi
   \useindentnextparameter\typingparameter % needs checking
   \endgroup
   \dorechecknextindentation} % needs checking

\def\buff_verbatim_type_file_checked#1#2%
  {\doifelsetypingfile{#2}
     {\buff_verbatim_type_file_indeed{#1}\currenttypingfile}
     {\showmessage\m!verbatims1{#2}}}

\permanent\protected\def\doifelsetypingfile#1%
  {\edef\currenttypingfile{\locfilename{#1}}%
   \ifempty\currenttypingfile
     \edef\currenttypingfile{\locfilename{#1.tex}}% downward compatible
   \fi
   \ifempty\currenttypingfile
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi}

\aliased\let\doiftypingfileelse\doifelsetypingfile

\def\buff_verbatim_type_file_indeed#1#2% category name
  {\edef\currenttyping{#1}%
   \typingparameter\c!before\relax
   \startpacked[\v!blank]
   \buff_verbatim_setup_line_numbering
   \buff_verbatim_initialize_typing_one
   \buff_verbatim_initialize_typing_two
   \dostarttaggedchained\t!verbatimblock\currenttyping\??typing
   \beginofverbatimlines
   \dostarttagged\t!verbatimlines\empty
   \clf_typefile
      name   {#2}%
      strip  {\typingparameter\c!strip}%
      range  {\typingparameter\c!range}%
      regime {\currentregime}%
      tab    {\typingparameter\c!tab}%
      method {\p_buff_option}%
      nature {display}%
   \relax
   \dostoptagged
   \endofverbatimlines
   \dostoptagged
   \stoppacked
   \typingparameter\c!after\relax}

%D Line numbering:

\newconditional\verbatimnumberinglines

\permanent\protected\def\beginofverbatimlinenumbering
  {\startlinenumbering
     [\currenttyping]%
     [\c!continue=\typingparameter\c!continue,
        \c!method=\v!type,
         \c!start=\typingparameter\c!start,
          \c!stop=\typingparameter\c!stop, % ?
          \c!step=\typingparameter\c!step]}

\permanent\protected\def\endofverbatimlinenumbering
  {\stoplinenumbering}

\def\buff_verbatim_setup_line_numbering
  {\ifdefined\startlinenumbering
     \edef\p_numbering{\typingparameter\c!numbering}%
     \ifx\p_numbering\v!file
       \setuplinenumbering[\currenttyping][\c!method=\v!file]%
       \settrue\verbatimnumberinglines
     \orelse\ifx\p_numbering\v!line
       \settrue\verbatimnumberinglines
     \else
       \setfalse\verbatimnumberinglines
     \fi
     \ifconditional\verbatimnumberinglines
       \enforced\let\beginofverbatimlines\beginofverbatimlinenumbering
       \enforced\let\endofverbatimlines  \endofverbatimlinenumbering
     \fi
   \fi}

%D \macros
%D   {filename}
%D
%D Typesetting filenames in monospaced fonts is possible with
%D
%D \starttyping
%D \filename{here/there/filename.suffix}
%D \stoptyping
%D
%D The definition is not that spectacular.

\permanent\protected\def\filename#1{{\tttf\hyphenatedfilename{#1}}}

%D \macros
%D   {verbatim}
%d
%D And a bonus macro, an unexpanded detokenize:

\permanent\protected\def\verbatim#1{\detokenize{#1}}

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

% \tttf   gives problems with {\tx \type...}
% \tt\tf  does not adapt to e.g. \bf

\setuptyping
  [\c!before=\blank,
   \c!after=\blank,
  %\c!keeptogether=yes, % this will become default (when tested enough)
  %\c!bodyfont=,
  %\c!color=,
   \c!space=\v!off,
   \c!lines=\v!no,
   \c!page=\v!no,
   \c!tab=\v!yes, % what is this: \s!ascii ?
   \c!option=\v!none,
   \c!text=\v!no,
   \c!style=\tt,
   \c!indentnext=\v!yes,
   \c!margin=\zeropoint,
   \c!evenmargin=\zeropoint,
   \c!oddmargin=\zeropoint,
   \c!blank=\v!line,
  %\c!escape=, % yes | no | {START,STOP} | default when yes: {BTEX,ETEX}
   \c!numbering=\v!no,
  %\c!range=,
   \c!start=1,
  %\c!stop=,
   \c!step=1,
  %\c!continue=,
   \c!strip=\v!no] % auto or number

\definetyping[\v!typing]

% the \zeropoint forces the number to the margin but also works ok in text

\setuplinenumbering[\v!typing][\c!location=\v!left,\c!width=\zeropoint]

\setuptyping       [\v!file]  [\s!parent=\??typing       \v!typing] % we don't want \start..\stop overload
\setuplinenumbering[\v!file]  [\s!parent=\??linenumbering\v!typing]

\setuptyping       [\v!buffer][\s!parent=\??typing       \v!file]   % we don't want \start..\stop overload
\setuplinenumbering[\v!buffer][\s!parent=\??linenumbering\v!file]

%D The setups for inline verbatim default to:

\setuptype
  [\c!space=\v!off,
   \c!lines=\v!no,
  %\c!color=,
   \c!style=\tt,
  %\c!option=\v!normal,
   \c!page=\v!no,
   \c!tab=\v!yes,
   \c!compact=\v!all]

%D Buffers

% [name] [settings] | [name] | [settings]

\permanent\protected\def\typebuffer
  {\begingroup
   \let\currenttyping\v!buffer
   \buff_verbatim_type_buffer}

\permanent\protected\def\typedefinedbuffer[#1]%
  {\buff_verbatim_type_defined_buffer[\v!buffer][\thedefinedbuffer{#1}]}%

\appendtoks
    \frozen\instance\setuevalue{\e!type\currentbuffer}{\buff_verbatim_type_defined_buffer[\v!buffer][\currentdefinedbuffer]}%
\to \everydefinebuffer

\appendtoks % \e!buffer
    \frozen\instance\setuevalue{\e!type\currenttyping\v!buffer}{\buff_verbatim_type_buffer_class{\currenttyping}}%
\to \everydefinetyping

\tolerant\protected\def\buff_verbatim_type_buffer[#1]#*[#2]%
  {\ifarguments
     \buff_verbatim_type_buffer_indeed\currenttyping\empty% []
   \or
     \ifhastok={#1}%
       \setupcurrenttyping[#1]%
       \buff_verbatim_type_buffer_indeed\currenttyping\empty% [settings]
     \else
       \processcommalist[#1]{\buff_verbatim_type_buffer_indeed\currenttyping}% [name]
     \fi
   \or
     \setupcurrenttyping[#2]%
     \processcommalist[#1]{\buff_verbatim_type_buffer_indeed\currenttyping}% [name] [settings]
   \fi
   \useindentnextparameter\typingparameter
   \endgroup
   \dorechecknextindentation}

\protected\def\buff_verbatim_type_buffer_class#1%
  {\begingroup
   \edef\currenttyping{#1}%
   \buff_verbatim_type_buffer}

\tolerant\protected\def\buff_verbatim_type_defined_buffer[#1]#*[#2]#*[#3]% category name settings
  {\begingroup
   \ifarguments\or\or\or
     \setuptyping[#1][#3]%
   \fi
   \buff_verbatim_type_buffer_indeed{#1}{#2}%
   \useindentnextparameter\typingparameter
   \endgroup
   \dorechecknextindentation}

\protected\def\buff_verbatim_type_buffer_indeed#1#2% category name
  {\edef\currenttyping{#1}%
   \typingparameter\c!before\relax
   \startpacked[\v!blank]
   \buff_verbatim_setup_line_numbering
   \buff_verbatim_initialize_typing_one
   \buff_verbatim_initialize_typing_two
   \dostarttaggedchained\t!verbatimblock{#1}\??typing
   \beginofverbatimlines
   \dostarttagged\t!verbatimlines\empty
   \clf_typebuffer
     name   {#2}%
     strip  {\typingparameter\c!strip}%
     range  {\typingparameter\c!range}%
     regime {\currentregime}%
     tab    {\typingparameter\c!tab}%
     method {\p_buff_option}%
     escape {\typingparameter\c!escape}%
     nature {display}%
   \relax
   \dostoptagged
   \endofverbatimlines
   \dostoptagged
   \stoppacked
   \typingparameter\c!after\relax}

\permanent\tolerant\protected\def\typeinlinebuffer[#1]#,[#2]%
  {\dontleavehmode
   \begingroup
   \enforced\let\setupcurrenttyping\setupcurrenttype % a terrible hack but it saves code
   \let\currenttype\empty
   \ifparameters
     \buff_verbatim_type_buffer_indeed_inline\currenttyping\empty% []
   \or
     \ifhastok={#1}%
       \setupcurrenttyping[#1]%
       \buff_verbatim_type_buffer_indeed_inline\currenttyping\empty% [settings]
     \else
       \processcommalist[#1]{\buff_verbatim_type_buffer_indeed_inline\currenttyping}% [name]
     \fi
   \or
     \setupcurrenttyping[#2]%
     \processcommalist[#1]{\buff_verbatim_type_buffer_indeed_inline\currenttyping}% [name] [settings]
   \fi
   \endgroup}

\protected\def\buff_verbatim_type_buffer_indeed_inline#1#2% category name
  {\edef\currenttype{#1}%
   \buff_verbatim_initialize_type_one
   \dostarttaggedchained\t!verbatim\currenttype\??type
   \clf_typebuffer
     name   {#2}%
     strip  {\v!yes}%
     regime {\currentregime}%
     method {\p_buff_option}%
     nature {inline}% is default anyway
   \relax
   \dostoptagged}

% get     : before/after of buffer
% typing  : before/after of typing
% process : no before/after (special case anyway)

% the next one is obsolete (bad name and used nowhere) so if it stays
% the name will change

\permanent\tolerant\protected\def\processbuffer[#1]#*[#2]%
  {\writestatus{nota bene}{\string\processbuffer\space is obsolete}%
   \begingroup
   \ifarguments
     \buff_verbatim_process_indeed\v!buffer\empty% []
   \or
    %\ifcondition\validassignment{#1}%
     \ifhastok={#1}%
       \setuptyping[\v!buffer][#1]%
       \buff_verbatim_process_indeed\v!buffer\empty% [settings]
     \else
       \processcommalist[#1]{\buff_verbatim_process_indeed\v!buffer}% [name]
     \fi
   \or
     \setuptyping[\v!buffer][#2]%
     \processcommalist[#1]{\buff_verbatim_process_indeed\v!buffer}% [name] [settings]
   \fi
   \endgroup}

\def\buff_verbatim_process_indeed#1#2%
  {\edef\currenttyping{#1}%
   \clf_processbuffer
     name   {#2}%
     strip  {\typingparameter\c!strip}%
     tab    {\typingparameter\c!tab}%
   % method {\p_buff_option}%
     nature {direct}%
   \relax}

% so far for obsolete

% line numbering, keep broken lines together

\newcount\nofverbatimlines

\protected\def\buff_verbatim_begin_of_line
  {\ifconditional\verbatimnumberinglines
     \global\advance\nofverbatimlines\plusone
     \c_attr_verbatimline\nofverbatimlines
   \fi
   \noindent
   \buff_verbatim_set_line_margin
   \the\everyline % maybe also after starttagged
   \strut % after starttagged, else break !
   \dostarttagged\t!verbatimline\empty}

\protected\def\buff_verbatim_end_of_line
  {\dostoptagged
   \obeyedline % still needed?
   \par
   \ifconditional\verbatimnumberinglines
     \c_attr_verbatimline\attributeunsetvalue
   \fi}

\protected\def\buff_verbatim_empty_line
  {\dostoptagged
   \ifconditional\verbatimnumberinglines
     \par\strut\par % this will be an option where we use a signal instead of a strut
   \else
     \blank[\typingparameter\c!blank]%
   \fi
   \dostarttagged\t!verbatimlines\empty}

% hooks (todo: frozen):

\newcount      \c_buff_verbatim_noflines
\newcount      \c_buff_verbatim_current
\newconditional\c_buff_optimize_linebreaks

\permanent\protected\def\doverbatimspace           {\obeyedspace}

\permanent\protected\def\doinlineverbatimstart     {}
\permanent\protected\def\doinlineverbatimstop      {}
\permanent\protected\def\doinlineverbatimbeginline {}
\permanent\protected\def\doinlineverbatimnewline   {\obeyedspace}
\permanent\protected\def\doinlineverbatimemptyline {\obeyedspace}

\permanent\protected\def\dodisplayverbatimstart    {\advance\c_buff_verbatim_current\plusone
                                                    \buff_verbatim_begin_of_line}
\permanent\protected\def\dodisplayverbatimstop     {\buff_verbatim_end_of_line}
\permanent\protected\def\dodisplayverbatimbeginline{\advance\c_buff_verbatim_current\plusone
                                                    \buff_verbatim_begin_of_line}
\permanent\protected\def\dodisplayverbatimnewline  {\buff_verbatim_end_of_line
                                                    \par
                                                    \ifconditional\c_buff_optimize_linebreaks
                                                      \buff_verbatim_inject_breaks
                                                    \fi}
\permanent\protected\def\dodisplayverbatimemptyline{\buff_verbatim_empty_line}

\protected\def\buff_verbatim_inject_breaks
  {\ifnum\c_buff_verbatim_current=\plusone
     \nobreak
   \orelse\ifnum\c_buff_verbatim_noflines=\plusthree
     \nobreak
   \orelse\ifnum\c_buff_verbatim_current=\numexpr\c_buff_verbatim_noflines-\plusone\relax
     \nobreak
   \fi}

\permanent\protected\def\dodisplayverbatiminitialize#1%
  {\forgetparindent % maybe more
   \c_buff_verbatim_noflines#1\relax
   \c_buff_verbatim_current\zerocount}

\loadmarkfile{buff-imp-default}    % preloaded as otherwise spurious spaces inline due to loading
\loadmarkfile{buff-imp-nested}     % preloaded as otherwise spurious spaces inline due to loading
\loadmarkfile{buff-imp-escaped}    % for completeness

%D To be considered:

% \installcorenamespace{visualizer}
%
% \installcommandhandler \??visualizer {visualizer} \??visualizer
%
% \appendtoks
%     \frozen\instance\setuevalue{\currentvisualizer}{\buff_verbatim_visualize{\currentvisualizer}}%
% \to \everydefinevisualizer
%
% \protected\def\buff_verbatim_visualize#1%
%   {\bgroup
%    \def\currentvisualizer{#1}%
%    \usevisualizerstyleandcolor\c!style\c!color
%    \let\next}

\appendtoks
    \enforced\def\type#1{\detokenize\expandafter{\csstring#1}}% or maybe detokenize
    \enforced\def\tex #1{\letterbackslash#1}%
\to \everysimplifycommands

\stopcontextdefinitioncode

%D This is \type {\asciimode} without the double comment hackery:

\permanent\protected\def\literalmode{\setcatcodetable\txtcatcodes}

\protect \endinput
