% textnotes.sty
% put textual notes at end of volume
% reference by page and line number

% patch lineno.sty 

\RequirePackage{lineno}
\RequirePackage{ifthen}

\newcommand*{\typonoteprint}[2]{#1}
\newcommand*{\transnoteprint}[2]{#1}
\newcommand*{\varnoteprint}[2]{#1}

\newcounter{lineref}
\def\markthecurrentline{\refstepcounter{lineref}%
  \linelabel{lineref:\thelineref}%
  \hypertarget{anchor:lineref:\thelineref}{}}
\newcommand{\lref}[1]{\hyperlink{anchor:#1}{\ref*{#1}}}
\newcommand{\lpageref}[1]{\hyperlink{anchor:#1}{\pageref*{#1}}}
\def\themarkedline{\protect\lref{lineref:\thelineref}}
\def\themarkedlinepage{\protect\lpageref{lineref:\thelineref}}

\long\def\x@addto@macro#1#2{%
  \begingroup
    \toks@\expandafter{#1}%
    \xdef#1{\the\toks@#2}%
  \endgroup}

% Redefine \@LN@postlabel to so it expands argument
% \def\@LN@postlabel#1{\g@addto@macro\@LN@labellist{#1\@lt}%
\def\@LN@postlabel#1{%
  \x@addto@macro\@LN@labellist{#1}%
  \g@addto@macro\@LN@labellist{\@lt}%
  \vadjust{\penalty-\@Mllbcodepen}}


% don't actually print line numbers
\def\invisiblelinenumbers{\let\VisibleLineNumber\LineNumber%
\def\LineNumber{}}
\def\visiblelinenumbers{\let\LineNumber\VisibleLineNumber}
\invisiblelinenumbers

% rest copied/modified pagenote code from mempatch.sty

\newcounter{textnote}
\renewcommand{\thetextnote}{\arabic{textnote}}
\setcounter{textnote}{0}

\newcommand*{\maketextnote}{%
  \newwrite\@txnotefile
  \immediate\openout\@txnotefile=\jobname.tnt
  \def\textnote{\@bsphack\begingroup
    \@sanitize
    \m@m@wrtxnote}%
  \def\textnoteline{\@bsphack\begingroup
    \@sanitize
    \m@m@wrtxnoteline}%
  \typeout{Writing textual note file \jobname.tnt}}

\newcommand*{\txnchap}{\f@rtoc}
\newcommand*{\txnschap}{\f@rbdy}

% textnote stuff
% where we use lineno to get the line number

% We write \textnoteentry{num}{id}{note}{page}{line}
\newcommand{\m@m@wrtxnote}[2][]{%
  \refstepcounter{textnote}%
%  \markthecurrentline
%  \notenumintext{\thetextnote}%
%   \ifm@mpn@new@chap
%     \global\m@mpn@new@chapfalse
%     \addtotextnotes{\string\textnotesubhead{\@chapapp}{\thechapter}{\pnchap}}%
%   \fi
%   \ifm@mpn@new@schap
%     \global\m@mpn@new@schapfalse
%     \addtotextnotes{\string\textnotesubhead{\@chapapp}{}{\pnschap}}%
%   \fi
  \protected@write\@txnotefile{}%
%   {\string\textnoteentry{\thepagenote}{#1}{#2}{\thepage}}%
    {\string\textnoteentry{\thetextnote}{#1}{#2}{\themarkedlinepage}{\themarkedline}}%
  \endgroup
  \@esphack}

\def\textnote{\@bsphack\begingroup \@sanitize\m@m@textnote}
\newcommand{\m@m@textnote}[2][]{\endgroup\@esphack}


% textnoteline stuff
% where we pass the line number by hand

% We write \textnoteentry{num}{id}{note}{page}{line}
\newcommand{\m@m@wrtxnoteline}[3][]{%
  \protected@write\@txnotefile{}
    {\string\textnoteentry{\thetextnote}{#1}{#2}{\thepage}{#3}}%
  \endgroup
  \@esphack}

\def\textnote{\@bsphack\begingroup \@sanitize\m@m@textnoteline}
\newcommand{\m@m@textnoteline}[2][]{\endgroup\@esphack}


\newcommand{\addtotextnotes}[1]{%
  \protected@write\@txnotefile{}{#1}}

% #4 = page
% #5 = line
% #2 = id
% #3 = note
\newcommand{\textnoteentry}[5]{\item[#4:#5\hfill] #2\lemmasep #3}

\providecommand*{\textnotesname}{Textual Notes}
\newcommand*{\textnotedivision}{\chapter{\textnotesname}}

\newcommand*{\printtextnotes}{\@ifstar{\@sprinttextnotes}{\@printtextnotes}}
\newcommand*{\txnofilewarn}{%
  \ClassWarning{textnotes}{There is no .tnt file}}

% Fix this if textnotes after item
\newcommand*{\@sprinttextnotes}{%
% \notedivision
\IfFileExists{\jobname.tnt}{%
  \immediate\closeout\@txnotefile
  \input{\jobname.tnt}%
  \immediate\openout\@txnotefile=\jobname.tnt%
  }{%
  \txnofilewarn
}}

\newcommand*{\@printtextnotes}{%
%  \textnotedivision 
  \IfFileExists{\jobname.tnt}{%
    \immediate\closeout\@txnotefile \input{\jobname.tnt}%
  }{%
    \txnofilewarn } }

\newcommand*{\textnotelistlabel}[1]{\upshape #1\hfil}

\newcommand*{\lemmasep}{\mbox{]\hskip-2pt]}\quad}

\newenvironment{textnotelist}{\list{}{%
  \setlength{\labelwidth}{1em}%
  \setlength{\leftmargin}{1.5em}%
  \setlength{\topsep}{3pt}%
  \itemindent\z@%
  \listparindent\parindent% standard paragraph indent
  \itemsep\z@% no space between items
  \parsep\z@% no spacing between paragraphs
  %\advance\itemindent-\labelwidth
  \let\makelabel\textnotelistlabel}}{\endlist}

%\newcommand{\textnotesubhead}[3]{\section*{#3}\par\begin{multicols}{2}}

\def\starttextnotes{%
    \addtotextnotes{\protect\begin{textnotelist}}%
      \pagewiselinenumbers} 
\def\endtextnotes{%
      \addtotextnotes{\protect\end{textnotelist}\protect\end{multicols}\protect\par}%
  \nolinenumbers}
%\newcommand{\textnotehead}[2]{\addtotextnotes{\protect\sectionb{#1}{#2}\mbox{}\protect\nopagebreak\protect\par\protect\nopagebreak\protect\begin{multicols}{2}}}
\newcommand{\textnotehead}[2]{\addtotextnotes{\protect\def\protect\secnumlength{1.5cm}\protect\begin{multicols}{2}[\protect\subsectionb{#1}{#2}]}}

% typonote
% #1 (optional) source
% #2 copy text
% #3 corrected text (printed in main text)
\newcommand{\typonote}[3][]{%
  \markthecurrentline % save line number
  \penalty\@M \hskip\z@skip % allow hyphens
  \ifthenelse{\equal{#1}{}}{%
    \typonoteprint{#3}{#2 CT}%  print in text
    \textnote[{#3}]{#2 CT}}{%
    \typonoteprint{#3}{#1\lemmasep #2 CT}%  print in text
    \textnote[{#3\ #1}]{#2 CT}}}

% typonoteline
% #1 (optional) source
% #2 copy text
% #3 corrected text (printed in main text)
% #4 line number
\newcommand{\typonoteline}[4][]{%
\ifthenelse{\equal{#1}{}}{%
  \typonoteprint{#3}{#2 CT}%  print in text
  \textnoteline[{#3}]{#2 CT}{#4}}{%
  \typonoteprint{#3}{#1\lemmasep #2 CT}%  print in text
  \textnoteline[{#3\ #1}]{#2 CT}{#4}}%
}

% transnote
% #1 (optional) source of replaced text, defaults to CT
% #2 copy text
% #3 revised text (printed in main text)
\newcommand{\transnote}[3][CT]{%
  \markthecurrentline % save line number
  \penalty\@M \hskip\z@skip % allow hyphens
  \ifthenelse{\equal{#1}{}}{%
    \textnote[{#3}]{#2}}{%
    \textnote[{#3}]{#2 #1}}%
  \transnoteprint{#3}{#2 #1}}%  print in text}


% transnoteline
% #1 (optional) source
% #2 copy text
% #3 revised text (printed in main text)
% #4 line number
\newcommand{\transnoteline}[4][CT]{%
\ifthenelse{\equal{#1}{}}{%
  \textnoteline[{#3}]{#2}{#4}}{%
  \textnoteline[{#3}]{#2 #1}{#4}}%
\transnoteprint{#3}{#2 #1}}%  print in text}

% varnote
% #1 (optional) source
% #2 variant texts
% #3 copy text (printed in main text)
\newcommand{\varnote}[3][CT]{%
  \markthecurrentline % save line number
  \penalty\@M \hskip\z@skip % allow hyphens
    \varnoteprint{#3}{#1\lemmasep #2}%  print in text
    \textnote[{#3\ #1}]{#2}}

% varnoteline
% #1 (optional) source
% #2 copy text
% #3 corrected text (printed in main text)
% #4 line number
\newcommand{\varnoteline}[4][]{%
  \ifthenelse{\equal{#1}{}}{%
    \varnoteprint{#3}{#2}%  print in text
    \textnoteline[{#3}]{#2}{#4}}{%
    \varnoteprint{#3}{#1\lemmasep #2}%  print in text
    \textnoteline[{#3\ #1}]{#2}{#4}}%
}

\endinput
%%
%% End of file `textnotes.sty'.
