%D \module
%D   [       file=strc-tnt,
%D        version=2019.05.30, % based on older code
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Text Notes,
%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 \macros
%D   {definetextnote,setuptextnote}
%D
%D \startbuffer
%D \definetextnote
%D   [textnote]
%D
%D \startbuffer
%D Test test test \textnote [n=5] {alpha}. test test test test tets test test
%D \textnote [n=10] {beta}. Test test test test tets test test \textnote [n=12]
%D {gamma}. Test test test test tets test test \textnote [n=24] {delta}. Test test
%D test test test test \textnote {epsilon} test test \textnote [n=*] {zeta}.
%D \stopbuffer
%D
%D \blank {\setuptextnote[empty=yes]    \getbuffer\par} \blank
%D \blank {\setuptextnote[empty=number] \getbuffer\par} \blank
%D \blank {\setuptextnote[empty=none]   \getbuffer\par} \blank
%D \blank {                             \getbuffer\par} \blank
%D
%D \blank[2*big]
%D
%D \placenotes[textnote:note][criterium=text]
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\installcorenamespace{textnote}

\installcommandhandler \??textnote {textnote} \??textnote

\definebar
  [\v!textnote:\v!underbar]
  [\v!underbar]

\definenote
  [\v!textnote:\v!note]

\setuptextnote
  [\c!rule=\v!textnote:\v!underbar,
   \c!note=\v!textnote:\v!note,
   \c!n=10] % * will use the real space

\appendtoks
    \frozen\instance\protected\edefcsname\currenttextnote\endcsname{\educ_textnote[\currenttextnote]}%
\to \everydefinetextnote

\tolerant\protected\def\educ_textnote[#1]#*[#S#2]#:#3%
  {\dontleavehmode
   \begingroup
   \def\currenttextnote{#1}%
   \ifparameter#2\or\setupcurrenttextnote[#2]\fi
   \edef\p_n{\textnoteparameter\c!n}%
   \edef\p_empty{\textnoteparameter\c!empty}%
   \edef\currentbar{\textnoteparameter\c!rule}%
   \edef\currentnote{\textnoteparameter\c!note}%
   \ifx\p_n\wildcardsymbol
     \donefalse
     \ifx\p_empty\v!yes
       \donetrue
     \orelse\ifx\p_empty\v!number
       \donetrue
     \orelse\ifx\p_empty\v!none
       \donetrue
     \fi
     \ifdone
       \setupbar[\currentbar][\c!empty=\v!yes]%
     \fi
     \inlinebar[\currentbar]\bgroup
       \wordboundary#3%
       \ifx\p_empty\v!yes
         \setnotetext[\currentnote]{#3}%
       \orelse\ifx\p_empty\v!number
         \runninghbox{\resetbar\setnote[\currentnote]{#3}}%
       \orelse\ifx\p_empty\v!none
         \setupnote[\currentnote][\c!location=\v!none]%
         \runninghbox{\resetbar\setnote[\currentnote]{#3}}%
       \fi
     \egroup
   \else
     \inlinebar[\currentbar]\bgroup
       \scratchcounter\numexpr\p_n/\plustwo\relax
       \ifx\p_empty\v!yes
         \interwordspacesbefore\scratchcounter
         \setnotetext[\currentnote]{#3}%
         \interwordspacesafter\scratchcounter
       \orelse\ifx\p_empty\v!number
         \interwordspacesbefore\scratchcounter
         \zwnj\runninghbox{\resetbar\setnote[\currentnote]{#3}}\zwnj
         \interwordspacesafter\scratchcounter
       \orelse\ifx\p_empty\v!none
         \setupnote[\currentnote][\c!location=\v!none]%
         \interwordspacesbefore\scratchcounter
         \zwnj\runninghbox{\resetbar\setnote[\currentnote]{#3}}\zwnj
         \interwordspacesafter\scratchcounter
       \else
          #3%
       \fi
     \egroup
   \fi
   \endgroup}

\protect \endinput

