\unprotect

% \footnotereferenceto in core-not.tex
% Remove the decrement which hides the problem within a page but breaks
% references in subsequent pages.

\def\footnotereferenceto
  {%\global\advance\crossreferencenumber\minusone\relax % else problem, needs further testing
   \rawreference\s!fnt{\s!fnt:t:\internalfootreference}{}}

% \dostartnote in core-note.tex
% The first thing after \startpushnote is expanded twice, which is not
% good when it generates two calls to \footnotereferenceto.

\def\dostartnote% nog gobble als in pagebody
  {\bgroup
   \settrue\processingnote
  %\restorecatcodes % to be tested first
   \iftypesettinglines % otherwise problems with \type <crlf> {xxx}
     \ignorelines % makes footnotes work in \startlines ... \stoplines
   \fi
   \ifnotesymbol
     \dolastnotesymbol
   \else
     \unskip\unskip
     \globallet\lastnotesymbol\dolastnotesymbol
   \fi
   \startlocalnoteinsert
     \doif{\noteparameter\c!scope}\v!page{\floatingpenalty\maxdimen}% experiment     \penalty\notepenalty
     \forgetall
     \setnotebodyfont
     \redoconvertfont % to undo \undo calls in in headings etc
     \splittopskip\strutht  % not actually needed here
     \splitmaxdepth\strutdp % not actually needed here
     \leftmargindistance\noteparameter\c!margindistance
     \rightmargindistance\leftmargindistance
     \ifnum\noteparameter\c!n=\zerocount % no ifcase new 31-07-99 ; always ?
       \doifnotinset{\noteparameter\c!width}{\v!fit,\v!broad}\setnotehsize
     \fi
     \startpushnote
       {\ifx\lastnotenumber\empty \else
          \preparefullnumber{\??vn\currentnote}\lastnotenumber\preparednumber
          \doifelse{\noteparameter\c!interaction}\v!no
            {\noteparameter\c!numbercommand
               {\preparednumber\domovednote\v!nextpage\v!previouspage}}%
            {\gotobox{\noteparameter\c!command % was \c!numbercommand, but compatible
               {\preparednumber\domovednote\v!nextpage\v!previouspage}}%
               [\s!fnt:f:\internalfootreference]}%
        \fi}%
     \doifelse{\noteparameter\c!interaction}\v!no
       {\ifconditional\pagewisenotes
          \footnotereferenceto
        \fi}%
       {\footnotereferenceto}%
     \bgroup
     \postponenotes
     \aftergroup\dostopnote
     \begstrut
     \let\next}

% \dodowritereference in core-ref.tex
% Needs immediate write to prevent refs getting out of order

\def\dodowritereference#1#2#3\end#4#5#6%
  {\bgroup
   \global\advance\crossreferencenumber \plusone\relax
   \if#1-\if#2:%
     \let\referenceprefix\empty
     \xdef\lastreference{#3}%
   \else
     % \xdef\lastreference{#1#2#3}% here we loose the space
   \fi\else
     % \xdef\lastreference{#1#2#3}% here we loose the space
   \fi
   \ifx\lastreference\empty \else
     \doiffirstreferenceoccurance\lastreference
       {\thisisdestination{\referenceprefix\lastreference}}%
     \referenceinfo>\lastreference
%      \edef\dododowritereference
%        {\writeutilitycommand
%           {\mainreference{\referenceprefix}{\lastreference}{#4}{#5}{#6}}}%
%      \dododowritereference
     \expanded{\immediatewriteutilitycommand{\noexpand\mainreference{\referenceprefix}{\lastreference}{#4}{#5}{#6}}}%
   \fi
   \egroup}

% \crossreferencenumber in core-ref.tex
% Should be initalised to zero, not one.

\newcount\crossreferencenumber \crossreferencenumber=0

% \dogetreferenceelements in core-ref.tex
% Fix the case where we reference the current reference

\def\dogetreferenceelements#1#2#3#4#5%
  {\chardef\currentreferencetype=\ifx#1\relax0\else#1\fi\relax
   \ifnum\currentreferencetype<\plustwo
     \edef\currentpagereference{#2}%
     \let \currentdatareference\empty
     \edef\currentlocationreference{#2}%
     \ifx\currentpagereference   \empty
       \let\currentfolioreference\folio
     \else
       \def \currentpagereference {\referencepagenumber[#2]}%
       \edef\currentfolioreference{\dosplitofffoliopart[#2]}%
     \fi
     \edef\currentrealreference{#3}%
     \settextreferences#4\end
     \ifnum0#5>\crossreferencenumber
       \forwardreferencefalse
     \else
       \forwardreferencetrue
     \fi
   \else
     \let \currentlocationreference\empty
     \edef\currentrealreference {#3}%
     \def \currentdatareference {#2}%
     \let \currentfolioreference\folio
     \settextreferences#4\end
     \forwardreferencefalse
   \fi
   \ifodd\currentreferencetype
     \realreferencepagefalse
   \else
     \docheckrealreferencepage\currentrealreference
     \ifrealreferencepage \else
       \docheckrealreferencepage\currentdatareference
     \fi
   \fi}

\protect


\setuphead[chapter][page=no]

\def\Test#1{%
  \DoTest#1\par
  \backslash somewhere test case - pairs of +/- (expected/actual) should match.\footnote{\DoTest#1}\par
  \DoTest#1\par
}

\def\DoTest#1#2#3#4#5#6#7#8{%
  \the\crossreferencenumber\hskip2em%
  #1\somewhere{+}{-}[A]\hskip2em%
  #2\somewhere{+}{-}[B]\hskip2em%
  #3\somewhere{+}{-}[C]\hskip2em%
  #4\somewhere{+}{-}[D]\hskip2em%
  #5\somewhere{+}{-}[E]\hskip2em%
  #6\somewhere{+}{-}[F]\hskip2em%
  #7\somewhere{+}{-}[G]\hskip2em%
  #8\somewhere{+}{-}[H]%
}

\starttext
\Test{--------}
\chapter[A]{A}
\Test{+-------}
\section[B]{B}
\Test{++------}
\page
\chapter[C]{C}
\Test{+++-----}
\section[D]{D}
\Test{++++----}
% For better test coverage, no \page here
\chapter[E]{E}
\Test{+++++---}
\section[F]{F}
\Test{++++++--}
\page
\chapter[G]{G}
\Test{+++++++-}
\section[H]{H}
\Test{++++++++}
\stoptext


