%D \module
%D   [       file=lang-url,
%D        version=2008.01.22, % used to be lang-mis
%D          title=\CONTEXT\ Language Macros,
%D       subtitle=Language Options,
%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.

\unprotect

%D This is rather old code. The following solution was used
%D for a long time and is kind of built-up over the years.

\ifx\\\undefined \let\\\crlf \fi

%D \macros
%D   {hyphenatedurl}
%D
%D For those who want to put full \URL's in a text, we offer
%D
%D \startbuffer
%D \hyphenatedurl{http://optimist.optimist/optimist/optimist.optimist#optimist}
%D \stopbuffer
%D
%D \typebuffer
%D
%D which breaks at the appropriate places. Watch the \type{#}
%D hack.
%D
%D When passed as argument, like in \type {\goto}, one needs
%D to substitute a \type {\\} for each \type{#}.
%D
%D \startbuffer
%D \hyphenatedurl{http://this.is.a.rather/strange/reference#indeed}
%D \stopbuffer
%D
%D \typebuffer

\ifx\urlsplitmode\undefined \chardef\urlsplitmode\plusone \fi

% 0 => don't split
% 1 => . : na, rest voor
% 2 => alles na
% 3 => alles voor

% \bgroup \catcode`\~=\active \catcode`\/=\active
%
% \unexpanded\gdef\hyphenatedurl#1% {}{} handles accents
%   {\bgroup
%    \ifnum\hyphenpenalty<10000 \else
%      \def\discretionary##1##2##3{##1\allowbreak##2}%
%    \fi
%    \obeyhyphens
%    \def\splitbefore##1%
%      {\setbox\scratchbox=\hbox{##1{}{}}%
%       \ifcase\urlsplitmode
%         \box\scratchbox
%       \or
%         \postwordbreak\box\scratchbox\prewordbreak
%       \or
%         \prewordbreak\discretionary{\box\scratchbox}{}{\box\scratchbox}\prewordbreak
%       \else
%         \postwordbreak\box\scratchbox\prewordbreak
%       \fi}%
%    \def\splitafter##1%
%      {\ifcase\urlsplitmode
%         ##1{}{}%
%       \or
%         \prewordbreak\discretionary{##1{}{}}{}{##1{}{}}\prewordbreak
%       \or
%         \prewordbreak\discretionary{##1{}{}}{}{##1{}{}}\prewordbreak
%       \else
%         \prewordbreak\discretionary{}{##1{}{}}{##1{}{}}\prewordbreak
%       \fi}%
%    \def\splitanyway##1%
%      {\prewordbreak##1\prewordbreak}%
%    \def\flushurl%
%      {\savedurl\let\savedurl\empty}%
%    \def\\%
%      {\spliturl\#}%
%    \let\~=\lettertilde\let~=\~%
%    \let\/=\letterslash\let/=\/%
%    \let\savedurl\empty
%    \scratchcounter\zerocount % used for hyphenmethod
%    \handletokens#1\with\scanurl\savedurl
%    \egroup}
%
% \egroup

% \chardef\urlhyphenmethod=0

% \def\scanurl#1%
%   {\advance\scratchcounter\plusone
%    \ifx#1\blankspace
%      \flushurl\splitanyway\normalspace
%    \else\ifx#1\ %
%      \flushurl\splitanyway\normalspace
%    \else\ifx#1\space
%      \flushurl\splitanyway\normalspace
%    \else\ifx#1\~%
%      \flushurl\splitbefore\~%
%    \else\ifx#1\#%
%      \flushurl\splitbefore\#%
%    \else\ifx#1\&%
%      \flushurl\splitbefore\&%
%    \else\ifx#1\%%
%      \flushurl\splitbefore\%%
%    \else\ifx#1\_%
%      \flushurl\splitbefore\_%
%    \else\if\noexpand#1\relax
%      #1%
%    \else\ifnum\catcode`#1=8
%      \flushurl\splitbefore\_%
%    \else\ifnum\catcode`#1=6
%      \flushurl\splitbefore\#%
%    \else\ifnum\catcode`#1=4
%      \flushurl\splitbefore\&%
%    \else\expandafter\if\string#1\lettertilde
%      \flushurl\splitbefore\~%
%    \else\expandafter\if\string#1\letterpercent
%      \flushurl\splitbefore\%%
%    \else\expandafter\if\string#1\letterunderscore
%      \flushurl\splitbefore\_%
%    \else\expandafter\if\string#1\letterquestionmark
%      \flushurl\splitafter\letterquestionmark
%    \else\expandafter\if\string#1\letterat
%      \flushurl\splitafter\letterat
%    \else\expandafter\if\string#1\letterslash
%      \edef\savedurl{\savedurl\letterslash}%
%    \else\expandafter\if\string#1+%
%      \flushurl\splitafter+%
%    \else\expandafter\if\string#1:%
%      \flushurl\splitafter:%
%    \else\expandafter\if\string#1.%
%      \flushurl\splitafter.%
%    \else\expandafter\if\string#1(%
%      \flushurl\splitbefore(%
%    \else\expandafter\if\string#1)%
%      \flushurl\splitafter)%
%    \else
%      \ifx\savedurl\empty\else
%        \splitbefore\savedurl
%        \let\savedurl\empty
%      \fi
%      \ifcase\urlhyphenmethod
%        \string#1%
%      \else
%        \ifnum\scratchcounter>\plusthree % so, \http: will not break
%          \edef\savedurl{\string#1}%
%        \else
%          \string#1%
%        \fi
%      \fi
%    \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

%D The following approach takes more resources but is cleaner (after all, we now
%D have huge \TEX's. It's is basically a \TEX\ version of the \MKIV\ variant.

\newtoks\everyhyphenatedurl

\appendtoks
   \let\&\letterampersand
   \let\#\letterhash
   \let\~\lettertilde
   \let\\\letterbackslash
   \let\$\letterdollar
   \let\^\letterhat
   \let\_\letterunderscore
   \let\{\letterleftbrace
   \let\}\letterrightbrace
   \let\|\letterbar
   \let~=\lettertilde
   \let|=\letterbar
\to \everyhyphenatedurl

\def\hyphenatedurlseparator{} % \periodcentered

\def\dohyphenatedurlnormal#1{\char#1\relax}%
\def\dohyphenatedurlafter #1{\char#1\discretionary{}{\hyphenatedurlseparator}{}}%
\def\dohyphenatedurlbefore#1{\discretionary{\hyphenatedurlseparator}{}{}\char#1\relax}%

% 0=normal 1=before 2=after

\def\sethyphenatedurlnormal#1{\expandafter\chardef\csname url @ #1\endcsname\zerocount}
\def\sethyphenatedurlbefore#1{\expandafter\chardef\csname url @ #1\endcsname\plusone  }
\def\sethyphenatedurlafter #1{\expandafter\chardef\csname url @ #1\endcsname\plustwo  }

\sethyphenatedurlbefore !
\sethyphenatedurlbefore "
\sethyphenatedurlbefore \letterhash
\sethyphenatedurlbefore \letterdollar
\sethyphenatedurlbefore \letterpercent
\sethyphenatedurlbefore \letterampersand
\sethyphenatedurlbefore (
\sethyphenatedurlbefore *
\sethyphenatedurlbefore +
\sethyphenatedurlbefore ,
\sethyphenatedurlbefore -
\sethyphenatedurlbefore .
\sethyphenatedurlbefore /
\sethyphenatedurlbefore :
\sethyphenatedurlbefore ;
\sethyphenatedurlbefore <
\sethyphenatedurlbefore =
\sethyphenatedurlbefore >
\sethyphenatedurlbefore ?
\sethyphenatedurlbefore @
\sethyphenatedurlbefore [
\sethyphenatedurlbefore \letterbackslash
\sethyphenatedurlbefore ^
\sethyphenatedurlbefore _
\sethyphenatedurlbefore `
\sethyphenatedurlbefore \letteropenbrace
\sethyphenatedurlbefore \letterbar
\sethyphenatedurlbefore \lettertilde

\sethyphenatedurlafter '
\sethyphenatedurlafter )
\sethyphenatedurlafter ]
\sethyphenatedurlafter \letterclosebrace

\unexpanded \def\hyphenatedurl#1%
  {\dontleavehmode
   \begingroup
   \the\everyhyphenatedurl
   \edef\ascii{#1}%
   \expanded{\handletokens{\detokenize\expandafter{\ascii}}}\with\dohyphenatedurl
   \endgroup}

\def\dohyphenatedurl#1%
  {\ifcase\executeifdefined{url @ #1}\zerocount\relax
     \expandafter\dohyphenatedurlnormal
   \or
     \expandafter\dohyphenatedurlbefore
   \or
     \expandafter\dohyphenatedurlafter
   \fi{\number`#1}}

% maybe ... to be finished
%
% \def\hyphenatedstring#1%
%   {\bgroup
%    \nohyphens
%    \def\next##1{##1\doif{##1}{-}{\allowbreak}}%
%    \handletokens#1\with\next
%    \egroup}
%
% {\hsize1cm\hyphenatedstring{ABXXXXXXXXXXC-12345-12345}}

%D \macros
%D   {hyphenatedfilename}
%D
%D For the moment we treat filenames in a similar way,
%D
%D \starttyping
%D \hyphenatedfilename{here/there/filename.suffix}
%D \stoptyping

\ifx\hyphenatedfilename\undefined \let\hyphenatedfilename\hyphenatedurl \fi

% \def\test#1%
%   {\dontleavehmode
%    \begingroup
%    \tttf
%    \hyphenatedurl {%
%      \letterampersand #1\letterampersand #1\letterampersand #1\letterampersand #1\letterampersand
%      \letterhash      #1\letterhash      #1\letterpercent   #1\letterslash     #1\letterampersand
%    }%
%    \endgroup}

% \dorecurse{100}{\test{a} \test{ab} \test{abc} \test{abcd} \test{abcde} \test{abcdef}}

\protect  \endinput

% \bgroup

% \gdef\lettercolon{:}

% \catcode`\:=\active
% \catcode`\^=\active
% \catcode`\/=\active
% \catcode`\~=\active

% \gdef\theurlcolon  {\nobreak\hbox{\lettercolon}\allowbreak}
% \gdef\theurlslash#1{\nobreak\hbox{\letterslash}\ifx#1\relax\else\ifnum`/=\expandafter`\string#1\else\allowbreak\fi#1\fi}
% \gdef\theurlhat    {\allowbreak\hbox{\letterhat}\nobreak}
% \gdef\theurltilde  {\allowbreak\hbox{\lettertilde}\nobreak}

% \gdef\ForMojcaWhoLikesHacks#1%
%   {\dontleavehmode
%    \begingroup
%    \mathcode`\:="8000 \let:\theurlcolon
%    \mathcode`\^="8000 \let^\theurlhat
%    \mathcode`\/="8000 \let/\theurlslash
%    \mathcode`\~="8000 \let~\theurltilde
%    \everymath\emptytoks
%    \mathsurround\zeropoint$\tf#1\relax$%
%    \endgroup}
% \egroup

% \hsize 1mm \ForMojcaWhoLikesHacks{http://www.sil.org//silesr/}
