%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 \aliased\let\dotagsetdelimitedsymbol  \gobbleoneargument \fi
\ifdefined\dotagsetsubsentencesymbol \else \aliased\let\dotagsetsubsentencesymbol\gobbleoneargument \fi

\lettonothing\p_delimited_left
\lettonothing\p_delimited_location
\lettonothing\p_delimited_margin
\lettonothing\p_delimited_nextleft
\lettonothing\p_delimited_nextright
\lettonothing\p_delimited_repeat
\lettonothing\p_delimited_right

% Kind of special ...

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

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

\defcsname\??hyphenmarksign\v!normal\endcsname
  {\enforced\let\textmodehyphen\normalhyphen
   \enforced\let\textmodehyphendiscretionary\normalhyphendiscretionary}

\defcsname\??hyphenmarksign\v!wide\endcsname
  {\enforced\let\textmodehyphen\composedhyphen
   \enforced\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]

\permanent\protected\def\normalhyphen
  {\hbox{\directsymbol\empty\c!hyphen}}

\permanent\protected\def\composedhyphen
  {\hbox{\directsymbol\empty\c!compoundhyphen}}

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

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

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

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

\permanent\immutable\integerdef\compoundhyphencode\privatecharactercode{compoundhyphen}

\pushoverloadmode

\permanent\protected\def\compoundhyphenfont
  {\char\compoundhyphencode}                 % tuned for font

\permanent\protected\def\compoundhyphenfake
  {\char\hyphenasciicode
  %\kern-.10775\emwidth                      % mkiv (.25\exheight)
  %\kern-.5\fontcharwd\font\hyphenasciicode  % for a while
   \kern{-\fontcharwd\font\hyphenasciicode/3}% okay for most
   \char\hyphenasciicode}

\permanent\protected\def\compoundhyphen
  {\hbox\bgroup
     \iffontchar\font\compoundhyphencode
       \compoundhyphenfont
     \else
       \compoundhyphenfake
     \fi
   \egroup}

\popoverloadmode

% ... till here.

\setnewconstant\boundarycharactermode\plusone

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

\permanent\protected\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

\permanent\protected\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

\permanent\protected\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}]

\newinteger\c_typo_subsentence_nesting

\mutable\let\beforesubsentence\donothing % maybe just a parameter
\mutable\let\aftersubsentence \donothing % maybe just a parameter

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

\newconditional\c_typo_subsentence_cleanup  \c_typo_subsentence_cleanup\conditionaltrue

\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}

\permanent\protected\def\midsentence
  {\dostarttaggednodetail\t!subsentencesymbol
   \dotagsetsubsentencesymbol\s!middle
   \symbol[\c!midsentence]%
   \dostoptagged}

\newboundary\c_typo_subsentence_boundary

\permanent\protected\def\beginofsubsentence
  {\beforesubsentence
   \ifnum\lastboundary=\c_typo_subsentence_boundary
     \unboundary
     \kern\hspaceamount\currentusedlanguage{intersentence}%
   \fi
   \global\advanceby\c_typo_subsentence_nesting\plusone
   \ifnum\c_typo_subsentence_nesting=\plusone
     \dontleavehmode
   \fi
   \dostarttaggednodetail\t!subsentence
   \dostarttaggednodetail\t!subsentencesymbol
   \dotagsetsubsentencesymbol\s!left
   \symbol[\ifodd\c_typo_subsentence_nesting\c!leftsentence\else\c!leftsubsentence\fi]%
   \dostoptagged
   \dostarttaggednodetail\t!subsentencecontent
   \typo_subsentence_cleanup_start}

\permanent\protected\def\endofsubsentence % relax prevents space gobbling
  {\typo_subsentence_cleanup_stop
   \dostoptagged
   \dostarttaggednodetail\t!subsentencesymbol
   \dotagsetsubsentencesymbol\s!right
   \symbol[\ifodd\c_typo_subsentence_nesting\c!rightsentence\else\c!rightsubsentence\fi]%
   \dostoptagged
   \dostoptagged
   \global\advanceby\c_typo_subsentence_nesting\minusone
   \unskip
   \boundary\c_typo_subsentence_boundary
   \aftersubsentence}

\permanent\protected\def\beginofsubsentencespacing % relax prevents space gobbling
  {\boundary\c_typo_subsentence_boundary}% \ignorespaces}

\permanent\protected\def\endofsubsentencespacing
  {\ifnum\lastboundary=\c_typo_subsentence_boundary
     \unboundary
     \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

\permanent\protected\def\startsubsentence{\beginofsubsentence\wordboundary\beginofsubsentencespacing\wordboundary\typo_subsentence_cleanup_start}
\permanent\protected\def\stopsubsentence {\typo_subsentence_cleanup_stop\wordboundary\endofsubsentencespacing\wordboundary\endofsubsentence}
\permanent\protected\def\subsentence     {\groupedcommandcs\startsubsentence\stopsubsentence}
\permanent\protected\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
    \enforced\permanent\def\quotation#1{"#1"}%
    \enforced\permanent\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.

\mutable\let\currentdelimitedtext      \s!unknown
\mutable\let\currentparentdelimitedtext\currentdelimitedtext

\installglobalmacrostack\currentdelimitedtext

\mutable\let\delimitedtextlevel\!!zerocount

\permanent\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

\mutable\lettonothing\currentdelimitedlanguage
\mutable\lettonothing\innerdelimitedlanguage
\mutable\lettonothing\outerdelimitedlanguage

\installglobalmacrostack\currentdelimitedlanguage

\lettonothing\m_delimited_argument

\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
   \ifempty\outerdelimitedlanguage
     \typo_delimited_set_language_nop
   \else
     \doiflanguageelse\outerdelimitedlanguage
       {\setusedlanguage\outerdelimitedlanguage}%
       \typo_delimited_set_language_nop
   \fi
   \ifempty\innerdelimitedlanguage\else
     \doiflanguageelse\innerdelimitedlanguage
       {\let\currentdelimitedlanguage\innerdelimitedlanguage}%
       \donothing
   \fi
   \lettonothing\m_delimited_argument}

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

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

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

\installcorenamespace{delimitedtext}
\installcorenamespace{delimitedtextlevel}

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

\appendtoks
    \expandafter\newinteger\csname\??delimitedtextlevel\currentdelimitedtext\endcsname
    \protected\frozen\instance\edefcsname\currentdelimitedtext        \endcsname{\delimitedtext[\currentdelimitedtext]}%
    \protected\frozen\instance\edefcsname\e!start\currentdelimitedtext\endcsname{\startdelimitedtext[\currentdelimitedtext]}%
    \protected\frozen\instance\edefcsname\e!stop \currentdelimitedtext\endcsname{\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}

\lettonothing\typo_delimited_repeat

\permanent\tolerant\protected\def\startdelimitedtext[#1]#*[#2]%
  {\begingroup
   \typo_delimited_push{#1}{#2}%
   \dostarttaggedchained\t!delimitedblock\currentparentdelimitedtext\currentdelimitedtext\??delimitedtext
   \edef\p_method{\delimitedtextparameter\c!method}%
   \ifx\p_method\v!font
     \expandafter\typo_delimited_start_font
   \orelse\ifx\p_method\v!paragraph
     \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
   \dostarttaggednodetail\t!delimitedsymbol
   \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
     \lettonothing\typo_delimited_repeat
   \fi
   \c_typo_delimited_repeating\conditionalfalse
   \edef\p_delimited_location{\delimitedtextparameter\c!location}%
   \ifx\p_delimited_location\v!paragraph
     \expandafter\typo_delimited_start_par
   \orelse\ifx\p_delimited_location\v!margin
     \expandafter\typo_delimited_start_par
   \else
     \expandafter\typo_delimited_start_txt
   \fi}

\def\typo_delimitedtexts_finish_font
  {\removeunwantedspaces % again ?
   \dostarttaggednodetail\t!delimitedsymbol
   \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
  {\dostarttaggednodetail\t!delimitedcontent
   \begingroup
   \douselanguageparameter\currentdelimitedlanguage
   \typo_delimited_show_language<\hss
   \ignorespaces}

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

\tolerant\def\typo_delimited_start_par[#1]%
  {\let\typo_delimited_stop\typo_delimited_stop_par
   \checkedblank[\delimitedtextparameter\c!spacebefore]%
   \delimitedtextparameter\c!before
   \edef\m_delimited_argument{#1}%
   \ifempty\m_delimited_argument
     \let\m_delimited_argument\m_delimited_argument
   \fi
   \ifempty\m_delimited_argument
     \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
   %
   \begingroup
   \usealignparameter\delimitedtextparameter
   \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}

\let\typo_delimited_stop_par_indeed\endgraf

\def\typo_delimited_stop_par
  {\typo_delimited_stop_content
   \rightdelimitedtextmark
   \carryoverpar\endgroup
   \endgraf
   \endgroup
   \pop_macro_checkindentation
   \typo_delimited_stop_par_indeed
   \delimitedtextparameter\c!after
   \checkedblank[\delimitedtextparameter\c!spaceafter]%
   \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}

\permanent\protected\def\stopdelimitedtext
  {\typo_delimited_stop
   \dostoptagged
   \typo_delimited_pop
   \endgroup}

\permanent\tolerant\protected\def\delimitedtext[#1]#*[#2]%
  {\dontleavehmode % following ones can be omited
   \typo_delimited_push{#1}{#2}%
   \edef\p_method{\delimitedtextparameter\c!method}%
   \ifx\p_method\v!font
     \expandafter\typo_delimited_fontdriven
   \orelse\ifx\p_method\v!text
     \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
     \expandafter\typo_delimited_par
   \orelse\ifx\p_delimited_location\v!margin
     \expandafter\typo_delimited_par
   \else
     \expandafter\typo_delimited_txt
   \fi}

% shortcuts

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

% todo: \dostarttaggednodetail\t!nothing % 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

\permanent\protected\def\setnextleftdelimitedtextmark
  {\ifempty\p_delimited_nextleft
      % nothing
   \orelse\ifx\p_delimited_nextleft\v!left
     \typo_delimited_nextleft_symbol\p_delimited_left
   \orelse\ifx\p_delimited_nextleft\v!right
     \typo_delimited_nextleft_symbol\p_delimited_right
   \else
     \typo_delimited_nextleft_symbol\p_delimited_nextleft
   \fi}

\permanent\protected\def\setnextrightdelimitedtextmark
  {\ifempty\p_delimited_nextright
      % nothing
   \orelse\ifx\p_delimited_nextright\v!right
     \typo_delimited_nextright_symbol\p_delimited_right
   \orelse\ifx\p_delimited_nextright\v!left
     \typo_delimited_nextright_symbol\p_delimited_left
   \else
     \typo_delimited_nextright_symbol\p_delimited_nextright
   \fi}

\permanent\protected\def\leftdelimitedtextmark
  {\ifempty\p_delimited_left
      % nothing
   \else
     \typo_delimited_left_symbol\p_delimited_left
   \fi}

\permanent\protected\def\rightdelimitedtextmark
  {\ifempty\p_delimited_right
      % nothing
   \else
      \typo_delimited_right_symbol\p_delimited_right
   \fi}

\def\typo_delimited_left_symbol#1%
  {% a hack: we need to make sure that the par counter is incremented for tagging / export
   \dontleavehmode
   % oterwise we get a break because the symbol has a different par number
   \dostarttaggednodetail\t!delimitedsymbol
   \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
      \kern-\wd\scratchbox
   \fi
   \unhbox\scratchbox
   \dostoptagged}

\def\typo_delimited_right_symbol#1%
  {\dostarttaggednodetail\t!delimitedsymbol
   \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
     \enforced\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
     \enforced\swapmacros\leftboundarycharacter\rightboundarycharacter
     \boundarycharactermode\plusone
     \typo_delimited_right_symbol#1%
   \egroup}

\protected\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.

\newboundary\c_typo_delimited_boundary

\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
       \ifnum\lastboundary=\c_typo_delimited_boundary
         \unboundary
         \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
       \dostarttaggednodetail\t!delimitedsymbol
       \dotagsetdelimitedsymbol\s!middle
       \delimitedtextparameter#1% unhbox\scratchbox
       \dostoptagged
     % \penalty\plustenthousand % else overfull boxes, but that's better than dangling periods
       \boundary\c_typo_delimited_boundary
     \fi
     \endgroup
   \else
     \c_typo_delimited_repeating\conditionaltrue
   \fi}

\def\typo_delimited_handle_left#1%
  {\begingroup
   \usedelimitedtextstyleandcolor\c!symstyle\c!symcolor
   \setbox\scratchbox\hbox{\delimitedtextparameter#1}%
   \ifdim\wd\scratchbox>\zeropoint
     \ifnum\lastboundary=\c_typo_delimited_boundary
       \unboundary
       \hskip\hspaceamount\currentusedlanguage{interquotation}%
     \fi
   % \strut % new, needed below
   % \ifhmode % else funny pagebeaks
   %   \penalty\plustenthousand
   %   \hskip\zeroskip % == \prewordbreak
   % \fi
     \strut % new, needed below
     \dostarttaggednodetail\t!delimitedsymbol
     \dotagsetdelimitedsymbol\s!left
     \delimitedtextparameter#1% unhbox\scratchbox
     \dostoptagged
     \boundary\c_typo_delimited_boundary
   \fi
   \endgroup}

\def\typo_delimited_handle_right#1%
  {\begingroup
   \usedelimitedtextstyleandcolor\c!symstyle\c!symcolor
   \setbox\scratchbox\hbox{\delimitedtextparameter#1}%
   \ifdim\wd\scratchbox>\zeropoint
     \ifnum\lastboundary=\c_typo_delimited_boundary
       \unboundary
       \penalty\plustenthousand
       \hskip\hspaceamount\currentusedlanguage{interquotation}%
     \fi
     \ifhmode % else funny pagebeaks
       \penalty\plustenthousand
       \hskip\zeroskip % == \prewordbreak
     \fi
     \strut % new, needed below
     \dostarttaggednodetail\t!delimitedsymbol
     \dotagsetdelimitedsymbol\s!right
     \delimitedtextparameter#1% unhbox\scratchbox
     \dostoptagged
     \boundary\c_typo_delimited_boundary
   \fi
   \endgroup}

\protected\def\typo_delimited_par
  {\groupedcommand
     {\dostarttaggedchained\t!delimited\currentparentdelimitedtext\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}}

\lettonothing\p_left_right

\protected\def\typo_delimited_txt
  {\edef\p_left_right{\delimitedtextparameter\c!left\delimitedtextparameter\c!right}%
   \ifempty\p_left_right
     \expandafter\typo_delimited_attributed
   \else
     \expandafter\typo_delimited_quoted
   \fi}

\def\typo_delimited_quoted
  {\dontleavehmode
   \begingroup
   \dostarttaggedchained\t!delimited\currentparentdelimitedtext\currentdelimitedtext\??delimitedtext
   \usedelimitedtextstyleandcolor\c!style\c!color
   \typo_delimited_handle_left\c!left
   \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\currentparentdelimitedtext\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\currentparentdelimitedtext\currentdelimitedtext\??delimitedtext
   \usedelimitedtextstyleandcolor\c!style\c!color
   \usedlanguageparameter{\c!left\currentparentdelimitedtext}% was: \currentdelimitedtext
   \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]

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

\protect \endinput
