typo-del.mkiv / last modification: 2020-01-30 14:16
%D \module
%D   [       file=typo-del, % moved from core-mis,
%D        version=20110112,
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Delimited Content,
%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.

% todo: textstyle|color for inline \quotation etc

\writestatus{loading}{ConTeXt Typesetting Macros / Delimited Content}

\unprotect

\ifdefined\dotagsetdelimitedsymbol   \else \let\dotagsetdelimitedsymbol  \gobbleoneargument \fi
\ifdefined\dotagsetsubsentencesymbol \else \let\dotagsetsubsentencesymbol\gobbleoneargument \fi

% THIS IS OBSOLETE:

\installcorenamespace{hyphenmarksign} % let's not waste a setuphandler (yet)

\unexpanded\def\setuphyphenmark[#1]% sign=normal|wide
  {\getdummyparameters[#1]%
   \expandnamespaceparameter\??hyphenmarksign\dummyparameter\c!sign\v!normal}

\setvalue{\??hyphenmarksign\v!normal}%
  {\let\textmodehyphen\normalhyphen
   \let\textmodehyphendiscretionary\normalhyphendiscretionary}

\setvalue{\??hyphenmarksign\v!wide}%
  {\let\textmodehyphen\composedhyphen
   \let\textmodehyphendiscretionary\composedhyphendiscretionary}

\setuphyphenmark[\c!sign=\v!wide]

\definesymbol[\c!lefthyphen]  [\languageparameter\c!lefthyphen]
\definesymbol[\c!righthyphen] [\languageparameter\c!righthyphen]
\definesymbol[\c!hyphen]      [\languageparameter\c!hyphen]

\unexpanded\def\normalhyphen
  {\hbox{\directsymbol\empty\c!hyphen}}

\unexpanded\def\composedhyphen
  {\hbox{\directsymbol\empty\c!compoundhyphen}}

\unexpanded\def\normalhyphendiscretionary
  {\discretionary
     {\hbox{\directsymbol\empty\c!righthyphen}}
     {\hbox{\directsymbol\empty\c!lefthyphen}}
     {\hbox{\directsymbol\empty\c!hyphen}}}

\unexpanded\def\composedhyphendiscretionary
  {\discretionary
     {\hbox{\directsymbol\empty\c!rightcompoundhyphen}}
     {\hbox{\directsymbol\empty\c!leftcompoundhyphen}}
     {\hbox{\directsymbol\empty\c!compoundhyphen}}}

\let\textmodehyphen             \composedhyphen
\let\textmodehyphendiscretionary\composedhyphendiscretionary

\definesymbol[\c!leftcompoundhyphen]  [\languageparameter\c!leftcompoundhyphen]
\definesymbol[\c!rightcompoundhyphen] [\languageparameter\c!rightcompoundhyphen]
\definesymbol[\c!compoundhyphen]      [\languageparameter\c!compoundhyphen]

% TILL HERE

\setnewconstant\boundarycharactermode\plusone

% old:       skip symbol skip
% new: bound skip symbol skip bound

\unexpanded\def\midboundarycharacter#1#2%
  {\ifcase\boundarycharactermode
   \or
     \removeunwantedspaces
     \wordboundary
     \hskip\hspaceamount\currentusedlanguage{#2}%
     \usedlanguageparameter#1%
     \hskip\hspaceamount\currentusedlanguage{#2}%
     \wordboundary
     \ignorespaces
   \or
     \usedlanguageparameter#1%
   \fi
   \boundarycharactermode\plusone}

% old: symbol nobreak skip
% new: symbol nobreak skip wordboundary

\unexpanded\def\leftboundarycharacter#1#2%
  {\ifcase\boundarycharactermode
   \or
     \usedlanguageparameter#1%
     \nobreak
     \hskip\hspaceamount\currentusedlanguage{#2}% why not a kern
     \wordboundary
   \or
     \usedlanguageparameter#1%
   \fi
   \boundarycharactermode\plusone}

% old: preword         skip symbol
% new: bound   nobreak skip symbol

\unexpanded\def\rightboundarycharacter#1#2%
  {\ifcase\boundarycharactermode
   \or
     \wordboundary
     \nobreak
     \hskip\hspaceamount\currentusedlanguage{#2}% why not a kern
     \usedlanguageparameter#1%
   \or
     \usedlanguageparameter#1%
   \fi
   \boundarycharactermode\plusone}

\definehspace [sentence]      [\zeropoint]
\definehspace [intersentence] [.250\emwidth]

\definesymbol
  [\c!midsentence]
  [\midboundarycharacter\c!midsentence{sentence}]

\definesymbol
  [\c!leftsentence]
  [\leftboundarycharacter\c!leftsentence{sentence}]

\definesymbol
  [\c!rightsentence]
  [\rightboundarycharacter\c!rightsentence{sentence}]

\definesymbol
  [\c!leftsubsentence]
  [\leftboundarycharacter\c!leftsubsentence{sentence}]

\definesymbol
  [\c!rightsubsentence]
  [\rightboundarycharacter\c!rightsubsentence{sentence}]

\newsignal \d_typo_subsentence_signal
\newcount  \c_typo_subsentence_nesting

\let\beforesubsentence\donothing
\let\aftersubsentence \donothing

% todo: make this language option
%
% \def\beforesubsentence{\removeunwantedspaces}
% \def\aftersubsentence {\ignorespaces}

\newconditional\c_typo_subsentence_cleanup  \settrue\c_typo_subsentence_cleanup

\def\typo_subsentence_cleanup_start
  {\ifconditional\c_typo_subsentence_cleanup
     \expandafter\ignorespaces
   \fi}

\def\typo_subsentence_cleanup_stop
  {\ifconditional\c_typo_subsentence_cleanup
     \removeunwantedspaces
   \fi}

\unexpanded\def\midsentence
  {\dostarttagged\t!subsentencesymbol\empty
   \dotagsetsubsentencesymbol\s!middle
   \symbol[\c!midsentence]%
   \dostoptagged}

\unexpanded\def\beginofsubsentence
  {\beforesubsentence
   \ifdim\lastkern=\d_typo_subsentence_signal
     \unskip
     \kern\hspaceamount\currentusedlanguage{intersentence}%
   \fi
   \global\advance\c_typo_subsentence_nesting\plusone
   \ifnum\c_typo_subsentence_nesting=\plusone
     \dontleavehmode % was \leaveoutervmode
   \fi
   \dostarttagged\t!subsentence\empty % no chain
   \dostarttagged\t!subsentencesymbol\empty
   \dotagsetsubsentencesymbol\s!left
   \symbol[\ifodd\c_typo_subsentence_nesting\c!leftsentence\else\c!leftsubsentence\fi]%
   \dostoptagged
   \dostarttagged\t!subsentencecontent\empty
   \typo_subsentence_cleanup_start}

\unexpanded\def\endofsubsentence % relax prevents space gobbling
  {\typo_subsentence_cleanup_stop
   \dostoptagged
   \dostarttagged\t!subsentencesymbol\empty
   \dotagsetsubsentencesymbol\s!right
   \symbol[\ifodd\c_typo_subsentence_nesting\c!rightsentence\else\c!rightsubsentence\fi]%
   \dostoptagged
   \dostoptagged
   \global\advance\c_typo_subsentence_nesting\minusone
   \unskip
   \kern\d_typo_subsentence_signal\relax
   \aftersubsentence}

\unexpanded\def\beginofsubsentencespacing % relax prevents space gobbling
  {\kern\d_typo_subsentence_signal\relax}% \ignorespaces}

\unexpanded\def\endofsubsentencespacing
  {\ifdim\lastkern=\d_typo_subsentence_signal
     \unskip
     \hskip\hspaceamount\currentusedlanguage{intersentence}%
     % no good, actually language dependent:
     % \ignorespaces
   \else
     \unskip
   \fi}

%D \startbuffer
%D test |<|test |<|test|>| test|>| test \par
%D test|<|test|<|test|>|test|>|test \par
%D test |<||<|test|>||>| test \par
%D test \directdiscretionary{<}test\directdiscretionary{>} test \par
%D \stopbuffer
%D
%D \typebuffer
%D \getbuffer

%unexpanded\def\startsubsentence{\beginofsubsentence\prewordbreak\beginofsubsentencespacing\typo_subsentence_cleanup_start}
%unexpanded\def\stopsubsentence {\typo_subsentence_cleanup_stop\endofsubsentencespacing\prewordbreak\endofsubsentence}
%unexpanded\def\subsentence     {\groupedcommandcs\startsubsentence\stopsubsentence}
%unexpanded\def\midsubsentence  {\typo_subsentence_cleanup_start\prewordbreak\midsentence\prewordbreak\typo_subsentence_cleanup_stop}

\unexpanded\def\startsubsentence{\beginofsubsentence\wordboundary\beginofsubsentencespacing\wordboundary\typo_subsentence_cleanup_start}
\unexpanded\def\stopsubsentence {\typo_subsentence_cleanup_stop\wordboundary\endofsubsentencespacing\wordboundary\endofsubsentence}
\unexpanded\def\subsentence     {\groupedcommandcs\startsubsentence\stopsubsentence}
\unexpanded\def\midsubsentence  {\typo_subsentence_cleanup_start\wordboundary\midsentence\wordboundary\typo_subsentence_cleanup_stop}

\definehspace [quotation]      [\zeropoint]
\definehspace [interquotation] [.125em]

%definehspace [quote]  [\zeropoint]
%definehspace [speech] [\zeropoint]

\definehspace [quote]  [\hspaceamount\currentusedlanguage{quotation}]
\definehspace [speech] [\hspaceamount\currentusedlanguage{quotation}]

\definesymbol
  [\c!leftquotation]
  [\leftboundarycharacter\c!leftquotation{quotation}]

\definesymbol
  [\c!rightquotation]
  [\rightboundarycharacter\c!rightquotation{quotation}]

\definesymbol
  [\c!nextleftquotation]
  [\rightboundarycharacter\c!leftquotation{quotation}]

\definesymbol
  [\c!nextrightquotation]
  [\leftboundarycharacter\c!rightquotation{quotation}]

\definesymbol
  [\c!leftquote]
  [\leftboundarycharacter\c!leftquote{quote}]

\definesymbol
  [\c!rightquote]
  [\rightboundarycharacter\c!rightquote{quote}]

\definesymbol
  [\c!leftspeech]
  [\leftboundarycharacter\c!leftspeech{speech}]

\definesymbol
  [\c!rightspeech]
  [\rightboundarycharacter\c!rightspeech{speech}]

\definesymbol
  [\c!middlespeech]
  [\leftboundarycharacter\c!middlespeech{speech}]

\appendtoks
    \def\quotation#1{"#1"}%
    \def\quote    #1{'#1'}%
\to \everysimplifycommands

%D The next features was so desperately needed by Giuseppe Bilotta that he made a
%D module for it. Since this is a typical example of core functionality, I decided
%D to extend the low level quotation macros in such a way that a speech feature
%D could be build on top of it. The speech opening and closing symbols are defined
%D per language. Italian is an example of a language that has them set.

\newsignal\d_typo_delimited_signal

\let\currentdelimitedtext\s!unknown

\installglobalmacrostack\currentdelimitedtext

\let\delimitedtextlevel\!!zerocount

\def\c_typo_delimited_nesting{\csname\??delimitedtextlevel\currentparentdelimitedtext\endcsname}

% the \setlanguageparameter macro sets but we are ungrouped .. only used here
%
% \currentusedlanguage
% \usedlanguageparameter

%D The optional argument can be a language, a narrower spec, or a outer:inner language
%D specification.
%D
%D \starttabulate
%D     \NC [en]    \NC {\tttf en} \quotation[en]   {{\tttf <en=\currentlanguage>} something french} \NC \NR
%D     \NC [fr]    \NC {\tttf en} \quotation[fr]   {{\tttf <fr=\currentlanguage>} something french} \NC \NR
%D     \NC [fr:]   \NC {\tttf fr} \quotation[fr:]  {{\tttf <en=\currentlanguage>} something french} \NC \NR
%D     \NC [:fr]   \NC {\tttf en} \quotation[:fr]  {{\tttf <fr=\currentlanguage>} something french} \NC \NR
%D     \NC [fr:fr] \NC {\tttf fr} \quotation[fr:fr]{{\tttf <fr=\currentlanguage>} something french} \NC \NR
%D     \NC [en:fr] \NC {\tttf en} \quotation[en:fr]{{\tttf <fr=\currentlanguage>} something french} \NC \NR
%D     \NC [fr:en] \NC {\tttf fr} \quotation[fr:en]{{\tttf <en=\currentlanguage>} something french} \NC \NR
%D \stoptabulate

\let\currentdelimitedlanguage\empty

\installglobalmacrostack\currentdelimitedlanguage

\def\typo_delimited_set_language_nop
  {\setusedlanguage{\delimitedtextparameter\c!language}}

\def\typo_delimited_set_language_yes
  {\doiflanguageelse\m_delimited_argument
     \typo_delimited_set_language_yes_a
     {\doifelseinstring:\m_delimited_argument
        \typo_delimited_set_language_yes_b
        \typo_delimited_set_language_nop}}

\def\typo_delimited_set_language_yes_b
  {\splitatcolon\m_delimited_argument\outerdelimitedlanguage\innerdelimitedlanguage
   \ifx\outerdelimitedlanguage\empty
     \typo_delimited_set_language_nop
   \else
     \doiflanguageelse\outerdelimitedlanguage
       {\setusedlanguage\outerdelimitedlanguage}%
       \typo_delimited_set_language_nop
   \fi
   \ifx\innerdelimitedlanguage\empty
   \else
     \doiflanguageelse\innerdelimitedlanguage
       {\let\currentdelimitedlanguage\innerdelimitedlanguage}%
       \donothing
   \fi
   \let\m_delimited_argument\empty}

\def\typo_delimited_set_language_yes_a
  {\let\currentdelimitedlanguage\m_delimited_argument
   \let\m_delimited_argument\empty}

\def\typo_delimited_push#1#2%
  {\push_macro_currentdelimitedtext     % can we combine these two
   \push_macro_currentdelimitedlanguage % the language used for hyphenation
   \edef\currentdelimitedtext{#1}%
   \edef\m_delimited_argument{#2}%
   \ifx\m_delimited_argument\empty
     \typo_delimited_set_language_nop
   \else
     \typo_delimited_set_language_yes
   \fi
   \let\currentparentdelimitedtext\currentdelimitedtext
   \global\advance\c_typo_delimited_nesting\plusone
   \edef\delimitedtextlevel{\number\c_typo_delimited_nesting}%
   \normalexpanded{\chaintocurrentdelimitedtext{\currentparentdelimitedtext:\delimitedtextlevel}}%
   \edef\currentdelimitedtext{\currentparentdelimitedtext:\delimitedtextlevel}}

\def\typo_delimited_pop
  {\global\advance\c_typo_delimited_nesting\minusone
   \pop_macro_currentdelimitedlanguage
   \pop_macro_currentdelimitedtext}

\installcorenamespace{delimitedtext}
\installcorenamespace{delimitedtextlevel}

\installcommandhandler \??delimitedtext {delimitedtext} \??delimitedtext

\appendtoks
    \expandafter\newcount\csname\??delimitedtextlevel\currentdelimitedtext\endcsname
    \setuevalue{\currentdelimitedtext        }{\delimitedtext[\currentdelimitedtext]}%
    \setuevalue{\e!start\currentdelimitedtext}{\startdelimitedtext[\currentdelimitedtext]}%
    \setuevalue{\e!stop \currentdelimitedtext}{\stopdelimitedtext}%
\to \everydefinedelimitedtext

\setupdelimitedtext
  [\c!location=\v!margin, % \v!text \v!paragraph
   \c!spacebefore=,
   \c!spaceafter=\delimitedtextparameter\c!spacebefore,
   \c!style=,
   \c!color=,
   \c!leftmargin=\zeropoint,
   \c!rightmargin=\delimitedtextparameter\c!leftmargin,
   \c!indentnext=\v!yes,
   \c!before=,
   \c!after=,
   \c!left=,
   \c!right=,
  %\c!level=0,
   \c!method=,
  %\c!language=\v!local,
   \c!repeat=\v!no]

\def\typo_delimited_repeat_indeed
  {\relax\ifcase\delimitedtextlevel\else
     \typo_delimited_handle_middle\c!middle
   \fi}

\let\typo_delimited_repeat\relax

\unexpanded\def\startdelimitedtext
  {\dodoubleempty\typo_delimited_text_start}

\unexpanded\def\typo_delimited_text_start[#1][#2]%
  {\begingroup
   \typo_delimited_push{#1}{#2}%
   \dostarttaggedchained\t!delimitedblock\currentdelimitedtext\??delimitedtext
   \edef\p_delimited_method{\delimitedtextparameter\c!method}%
   \ifx\p_delimited_method\s!font
     \expandafter\typo_delimited_start_font
   \else
     \expandafter\typo_delimited_start_other
   \fi}

\let\typo_delimited_stop\relax % hooks into \everypar

\def\typo_delimited_start_font
  {\let\typo_delimited_stop\typo_delimitedtexts_finish_font
   \dostarttagged\t!delimitedsymbol\empty
   \dotagsetdelimitedsymbol\s!left
   \delimitedtextparameter\c!left
   \dostoptagged
   \ignorespaces}

\newconditional\c_typo_delimited_repeating

\def\typo_delimited_start_other
  {\edef\p_delimited_repeat{\delimitedtextparameter\c!repeat}%
   \ifx\p_delimited_repeat\v!yes
     \let\typo_delimited_repeat\typo_delimited_repeat_indeed
   \else
     \let\typo_delimited_repeat\relax
   \fi
   \setfalse\c_typo_delimited_repeating
   \edef\p_delimited_location{\delimitedtextparameter\c!location}%
   \ifx\p_delimited_location\v!paragraph
     \singleexpandafter\typo_delimited_start_par
   \else\ifx\p_delimited_location\v!margin
     \doubleexpandafter\typo_delimited_start_par
   \else
     \doubleexpandafter\typo_delimited_start_txt
   \fi\fi}

\def\typo_delimitedtexts_finish_font
  {\removeunwantedspaces % again ?
   \dostarttagged\t!delimitedsymbol\empty
   \dotagsetdelimitedsymbol\s!right
   \delimitedtextparameter\c!right
   \dostoptagged}

\def\typo_delimited_show_language_indeed#1#2%
  {\begingroup
   \infofont
   \setbox\scratchbox\hpack{\lower\strutht\hbox to \zeropoint{\darkred#1\currentlanguage:\currentdelimitedlanguage#2}}%
   \vsmashbox\scratchbox
   \box\scratchbox
   \endgroup}

\let\typo_delimited_show_language\gobbletwoarguments

\installtextracker{delimited.language}
  {\let\typo_delimited_show_language\typo_delimited_show_language_indeed}
  {\let\typo_delimited_show_language\gobbletwoarguments}

\def\typo_delimited_start_content
  {\dostarttagged\t!delimitedcontent\empty
   \begingroup
   \douselanguageparameter\currentdelimitedlanguage
   \typo_delimited_show_language<\hss
   \ignorespaces}

\def\typo_delimited_stop_content
  {\removeunwantedspaces
   \removelastskip % redundant
   \typo_delimited_show_language\hss<%
   \endgroup
   \dostoptagged}

\def\typo_delimited_start_par
  {\dosingleempty\typo_delimited_start_par_indeed}

\let\typo_delimited_stop_par_indeed\endgraf

\def\typo_delimited_start_par_indeed[#1]%
  {\let\typo_delimited_stop\typo_delimited_stop_par
   \edef\p_delimited_spacebefore{\delimitedtextparameter\c!spacebefore}%
   \ifx\p_delimited_spacebefore\empty \else
     \blank[\p_delimited_spacebefore]%
   \fi
   \delimitedtextparameter\c!before
   \iffirstargument
     \edef\m_delimited_argument{#1}%
   \fi
   \ifx\m_delimited_argument\empty
     \let\m_delimited_argument\m_delimited_argument
   \fi
   \ifx\m_delimited_argument\empty
     \endgraf
     \doadaptleftskip {\delimitedtextparameter\c!leftmargin}%
     \doadaptrightskip{\delimitedtextparameter\c!rightmargin}%
     \let\typo_delimited_stop_par_indeed\endgraf
   \else % backward compatible direct directive
     \startnarrower[\m_delimited_argument]%
     \let\typo_delimited_stop_par_indeed\stopnarrower
   \fi
   % so far
   \push_macro_checkindentation
   \useindentingparameter\delimitedtextparameter
   %
   \begingroup
   \usedelimitedtextstyleandcolor\c!style\c!color
   %
   \edef\p_delimited_left     {\delimitedtextparameter{\c!left}}%
   \edef\p_delimited_right    {\delimitedtextparameter{\c!right}}%
   \edef\p_delimited_nextleft {\delimitedtextparameter{\c!nextleft}}%
   \edef\p_delimited_nextright{\delimitedtextparameter{\c!nextright}}%
   %
   \leftdelimitedtextmark
   %
   \setnextleftdelimitedtextmark
   \setnextrightdelimitedtextmark
   %
   \typo_delimited_start_content}

\def\typo_delimited_stop_par
  {\typo_delimited_stop_content
   \rightdelimitedtextmark
   \carryoverpar\endgroup % new per 2013-01-21 ... please left floats
   \pop_macro_checkindentation
   \typo_delimited_stop_par_indeed
   \delimitedtextparameter\c!after
   \edef\p_delimited_spaceafter{\delimitedtextparameter\c!spaceafter}%
   \ifx\p_delimited_spaceafter\empty \else
     \blank[\p_delimited_spaceafter]%
   \fi
   \useindentnextparameter\delimitedtextparameter
   \aftergroup\dorechecknextindentation}% AM: This was missing!

\def\typo_delimited_start_txt
  {\let\typo_delimited_stop\typo_delimited_stop_txt
   \begingroup
   \usedelimitedtextstyleandcolor\c!style\c!color
   \typo_delimited_handle_left\c!left
   \typo_delimited_start_content}

\def\typo_delimited_stop_txt
  {\typo_delimited_stop_content
   \typo_delimited_handle_right\c!right
   \endgroup}

\unexpanded\def\stopdelimitedtext
  {\typo_delimited_stop
   \dostoptagged
   \typo_delimited_pop
   \endgroup}

\unexpanded\def\delimitedtext
  {\dodoubleempty\typo_delimited_text}

\unexpanded\def\typo_delimited_text[#1][#2]%
  {\dontleavehmode % following ones can be omited
   \typo_delimited_push{#1}{#2}%
   \edef\p_delimited_method{\delimitedtextparameter\c!method}%
   \ifx\p_delimited_method\s!font
     \expandafter\typo_delimited_fontdriven
   \else
     \expandafter\typo_delimited_other
   \fi}

\def\typo_delimited_other
  {\edef\p_delimited_location{\delimitedtextparameter\c!location}%
   \ifx\p_delimited_location\v!paragraph
     \singleexpandafter\typo_delimited_par
   \else\ifx\p_delimited_location\v!margin
     \doubleexpandafter\typo_delimited_par
   \else
     \doubleexpandafter\typo_delimited_txt
   \fi\fi}

% shortcuts

\unexpanded\def\startdelimited{\startdelimitedtext}
\unexpanded\def\stopdelimited {\stopdelimitedtext}  % no let, dynamically assigned
           \def\delimited     {\delimitedtext}

% todo: \dostarttagged\t!nothing\empty % for left/right boxes

%D We have 4 different location and symbol handlers (two pairs):
%D
%D \starttyping
%D \input tufte \startquotation \input tufte \stopquotation
%D
%D \setupdelimitedtext
%D   [quotation]
%D   [nextleft=right,
%D    nextright=left]
%D
%D \input tufte \startquotation \input tufte \stopquotation
%D
%D \setupdelimitedtext
%D   [quotation]
%D   [nextleft={\symbol[nextleftquotation]},
%D    nextright={\symbol[nextrightquotation]}]
%D
%D \input tufte \startquotation \input tufte \stopquotation
%D \stoptyping

\unexpanded\def\setnextleftdelimitedtextmark
  {\ifx\p_delimited_nextleft\empty
      % nothing
   \else\ifx\p_delimited_nextleft\v!left
     \typo_delimited_nextleft_symbol\p_delimited_left
   \else\ifx\p_delimited_nextleft\v!right
     \typo_delimited_nextleft_symbol\p_delimited_right
   \else
     \typo_delimited_nextleft_symbol\p_delimited_nextleft
   \fi\fi\fi}

\unexpanded\def\setnextrightdelimitedtextmark
  {\ifx\p_delimited_nextright\empty
      % nothing
   \else\ifx\p_delimited_nextright\v!right
     \typo_delimited_nextright_symbol\p_delimited_right
   \else\ifx\p_delimited_nextright\v!left
     \typo_delimited_nextright_symbol\p_delimited_left
   \else
     \typo_delimited_nextright_symbol\p_delimited_nextright
   \fi\fi\fi}

\unexpanded\def\leftdelimitedtextmark
  {\ifx\p_delimited_left\empty
      % nothing
   \else
     \typo_delimited_left_symbol\p_delimited_left
   \fi}

\unexpanded\def\rightdelimitedtextmark
  {\ifx\p_delimited_right\empty
      % nothing
   \else
      \typo_delimited_right_symbol\p_delimited_right
   \fi}

\def\typo_delimited_left_symbol#1%
  {\dostarttagged\t!delimitedsymbol\empty
   \dotagsetdelimitedsymbol\s!left
   \setbox\scratchbox\hbox{\usedelimitedtextstyleandcolor\c!symstyle\c!symcolor#1}%
   \dontleavehmode
   \edef\p_delimited_margin{\delimitedtextparameter\c!location}%
   \ifx\p_delimited_margin\v!margin
      \hskip-\wd\scratchbox
   \fi
   \box\scratchbox
   \dostoptagged}

\def\typo_delimited_right_symbol#1%
  {\dostarttagged\t!delimitedsymbol\empty
   \dotagsetdelimitedsymbol\s!right
   \hsmash{\usedelimitedtextstyleandcolor\c!symstyle\c!symcolor#1}%
   \dostoptagged}

\def\typo_delimited_nextleft_symbol#1%
  {\let\typo_delimited_reset_next_symbol\typo_delimited_reset_next_symbol_indeed
   \localleftbox\bgroup
     \swapmacros\leftboundarycharacter\rightboundarycharacter
     \boundarycharactermode\plusone
     \typo_delimited_left_symbol#1%
   \egroup}

\def\typo_delimited_nextright_symbol#1%
  {\let\typo_delimited_reset_next_symbol\typo_delimited_reset_next_symbol_indeed
   \localrightbox\bgroup
     \swapmacros\leftboundarycharacter\rightboundarycharacter
     \boundarycharactermode\plusone
     \typo_delimited_right_symbol#1%
   \egroup}

\unexpanded\def\typo_delimited_reset_next_symbol_indeed
  {\localleftbox {}%
   \localrightbox{}}%

\let\typo_delimited_reset_next_symbol\relax

\appendtoks
    \typo_delimited_reset_next_symbol
\to \everyforgetall

% \starttext
%    \hyphenatedword{groepsvrijstellingsverordeningen}\par
%    \hyphenatedword{\quote{groepsvrijstellingsverordeningen}}\par
%     \dorecurse{100}{\hskip300pt\hskip\recurselevel pt test \quote{xxx xxxx}.\par}
%     \page \setuppapersize[A5][A4]
%     \quotation {overly beautiful pusillanimous sesquipedalian
%     longwinded} test test test test test test test test test test test
%     test test test test test test test test test test test test test
%     test test test test test test test test test test test test test
%     test test test test test test test test test test test test test
%     test test test
% \stoptext

% We have no real test case for this and it's broken already for a while,
% even in \MKII. Maybe we should to this in \LUA. Only Italian has the
% middlespeech parameter set.

\def\typo_delimited_handle_middle#1% special case
  {\ifconditional\c_typo_delimited_repeating
     \begingroup
     \usedelimitedtextstyleandcolor\c!symstyle\c!symcolor
     \setbox\scratchbox\hbox{\delimitedtextparameter#1}%
     \ifdim\wd\scratchbox>\zeropoint
       \ifdim\lastkern=\d_typo_delimited_signal
         \unkern
         \hskip\hspaceamount\currentusedlanguage{interquotation}%
       \else % maybe an option:
        %\edef\p_delimited_margin{\delimitedtextparameter\c!location}%
        %\ifx\p_delimited_margin\v!margin
        %   \hskip-\wd\scratchbox
        %\fi
       \fi
       \strut % new, needed below
       \dostarttagged\t!delimitedsymbol\empty
       \dotagsetdelimitedsymbol\s!middle
       \delimitedtextparameter#1% unhbox\scratchbox
       \dostoptagged
     % \penalty\plustenthousand % else overfull boxes, but that's better than dangling periods
       \kern\d_typo_delimited_signal % +- \prewordbreak
     \fi
     \endgroup
   \else
     \settrue\c_typo_delimited_repeating
   \fi}

\def\typo_delimited_handle_left#1%
  {\begingroup
   \usedelimitedtextstyleandcolor\c!symstyle\c!symcolor
   \setbox\scratchbox\hbox{\delimitedtextparameter#1}%
   \ifdim\wd\scratchbox>\zeropoint
     \ifdim\lastkern=\d_typo_delimited_signal
       \unkern
       \hskip\hspaceamount\currentusedlanguage{interquotation}%
     \else\ifdim\lastskip=\d_typo_delimited_signal
       \unskip
       \hskip\hspaceamount\currentusedlanguage{interquotation}%
     \fi\fi
   % \strut % new, needed below
   % \ifhmode % else funny pagebeaks
   %   \penalty\plustenthousand
   %   \hskip\zeropoint % == \prewordbreak
   % \fi
     \strut % new, needed below
     \dostarttagged\t!delimitedsymbol\empty
     \dotagsetdelimitedsymbol\s!left
     \delimitedtextparameter#1% unhbox\scratchbox
     \dostoptagged
     \penalty\plustenthousand % new per 2013-03-09 WS mailing list
     \hskip\d_typo_delimited_signal % +- \prewordbreak
   \fi
   \endgroup}

\def\typo_delimited_handle_right#1%
  {\begingroup
   \usedelimitedtextstyleandcolor\c!symstyle\c!symcolor
   \setbox\scratchbox\hbox{\delimitedtextparameter#1}%
   \ifdim\wd\scratchbox>\zeropoint
     \ifdim\lastkern=\d_typo_delimited_signal
       \unkern
       \penalty\plustenthousand
       \hskip\hspaceamount\currentusedlanguage{interquotation}%
     \else\ifdim\lastskip=\d_typo_delimited_signal
       \unskip
       \penalty\plustenthousand
       \hskip\hspaceamount\currentusedlanguage{interquotation}%
     \fi\fi
     \ifhmode % else funny pagebeaks
       \penalty\plustenthousand
       \hskip\zeropoint % == \prewordbreak
     \fi
     \strut % new, needed below
     \dostarttagged\t!delimitedsymbol\empty
     \dotagsetdelimitedsymbol\s!right
     \delimitedtextparameter#1% unhbox\scratchbox
     \dostoptagged
     \kern\d_typo_delimited_signal % +- \prewordbreak
   \fi
   \endgroup}

\unexpanded\def\typo_delimited_par
  {\groupedcommand
     {\dostarttaggedchained\t!delimited\currentdelimitedtext\??delimitedtext % block?
      \usedelimitedtextstyleandcolor\c!style\c!color
      \typo_delimited_handle_left\c!left
      \typo_delimited_start_content}
     {\typo_delimited_stop_content
      \typo_delimited_handle_right\c!right
      \removelastskip % hm
      \dostoptagged
      \typo_delimited_pop}}

\unexpanded\def\typo_delimited_txt
  {\edef\p_left_right{\delimitedtextparameter\c!left\delimitedtextparameter\c!right}%
   \ifx\p_left_right\empty
     \expandafter\typo_delimited_attributed
   \else
     \expandafter\typo_delimited_quoted
   \fi}

\def\typo_delimited_quoted
  {\dontleavehmode
   \begingroup
   \dostarttaggedchained\t!delimited\currentdelimitedtext\??delimitedtext
   \typo_delimited_handle_left\c!left
   \usedelimitedtextstyleandcolor\c!style\c!color
   \typo_delimited_start_content
   \bgroup
   \aftergroup\typo_delimited_quoted_e
   \let\next=}

\def\typo_delimited_quoted_e
  {\typo_delimited_stop_content
   \typo_delimited_handle_right\c!right
   \removelastskip % ?
   \dostoptagged
   \typo_delimited_pop
   \endgroup}

\def\typo_delimited_attributed
  {\dontleavehmode
   \begingroup
   \dostarttaggedchained\t!delimited\currentdelimitedtext\??delimitedtext
   \usedelimitedtextstyleandcolor\c!style\c!color
   \typo_delimited_start_content
   \bgroup
   \aftergroup\typo_delimited_attributed_e
   \let\next=}

\def\typo_delimited_attributed_e
  {\typo_delimited_stop_content
   \dostoptagged
   \typo_delimited_pop
   \endgroup}

\def\typo_delimited_fontdriven
  {\dontleavehmode
   \begingroup
   \dostarttaggedchained\t!delimited\currentdelimitedtext\??delimitedtext
   \usedlanguageparameter{\c!left\currentparentdelimitedtext}% was: \currentdelimitedtext
   \usedelimitedtextstyleandcolor\c!style\c!color
   \typo_delimited_start_content
   \bgroup
   \aftergroup\typo_delimited_fontdriven_e
   \let\next=}

\def\typo_delimited_fontdriven_e
  {\typo_delimited_stop_content
   \usedlanguageparameter{\c!right\currentparentdelimitedtext}% was: \currentdelimitedtext
   \dostoptagged
   \typo_delimited_pop
   \endgroup}

% testcase for nesting:
%
% \quotation{... \quotation{...} ...}
% \startquotation ... \startquotation... \quotation{...} \stopquotation\space ...\stopquotation
% \setupdelimitedtext[quotation][1][left=(,right=)]
% \setupdelimitedtext[quotation][2][left={[},right={]}]
% \setupdelimitedtext[quotation][3][left=\{,right=\}]
% \quotation{... \quotation{...} ...}
% \startquotation ... \startquotation... \quotation{...} \stopquotation\space ...\stopquotation

\definedelimitedtext
  [\v!quotation]
  [\c!left={\symbol[\c!leftquotation]},
   \c!right={\symbol[\c!rightquotation]},
   \c!leftmargin=\v!standard]

\definedelimitedtext
  [\v!quote][\v!quotation]

\setupdelimitedtext
  [\v!quote]
  [\c!location=\v!text,
   \c!left={\symbol[\c!leftquote]},
   \c!right={\symbol[\c!rightquote]}]

\definedelimitedtext
   [\v!blockquote][\v!quotation]

\setupdelimitedtext
  [\v!blockquote]
  [\c!left=,
   \c!right=]

\definedelimitedtext
  [\v!speech][\v!quotation]

\setupdelimitedtext
  [\v!speech]
  [\c!repeat=\v!yes,
   \c!left={\symbol[\c!leftspeech]},
   \c!middle={\symbol[\c!middlespeech]},
   \c!right={\symbol[\c!rightspeech]}]

\definedelimitedtext
  [\v!aside]
  [\c!left={\symbol[\c!leftsentence]},
   \c!right={\symbol[\c!rightsentence]}]

% how do we call an tight quote
%
% \definedelimitedtext
%    [\v!quotation][\v!quotation]
%
% \setupdelimitedtext
%   [\v!quotation]
%   [\c!indentnext=\v!no,
%    \c!spacebefore=\v!nowhite]

\unexpanded\def\setupquotation{\setupdelimitedtext[\v!quotation]}
\unexpanded\def\setupquote    {\setupdelimitedtext[\v!quote]}

\protect \endinput