%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part of the ptx2pdf macro package for formatting USFM text
% copyright (c) 2007 by SIL International
% written by Jonathan Kew
%
% Permission is hereby granted, free of charge, to any person obtaining  
% a copy of this software and associated documentation files (the  
% "Software"), to deal in the Software without restriction, including  
% without limitation the rights to use, copy, modify, merge, publish,  
% distribute, sublicense, and/or sell copies of the Software, and to  
% permit persons to whom the Software is furnished to do so, subject to  
% the following conditions:
%
% The above copyright notice and this permission notice shall be  
% included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
% NONINFRINGEMENT. IN NO EVENT SHALL SIL INTERNATIONAL BE LIABLE FOR  
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% Except as contained in this notice, the name of SIL International  
% shall not be used in advertising or otherwise to promote the sale,  
% use or other dealings in this Software without prior written  
% authorization from SIL International.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Macros to deal with Scripture references (book, chapter, verse) and running headers

% These macros work on the reference information in the format "Book:C:V"
% that is embedded in the \mark at each verse number.

% Note that "V" may be a verse range, if the USFM data included bridged verses such as "\v 12-15".
% Therefore, we have to do some extra work to extract individual verse numbers.

\newif\ifOmitChapterNumberRH % make this true to omit the ch # (eg for single-chapter books)

%
% Print the first reference from the mark data
%
\newif\ifOmitBookRef
\def\s@tf@rstr@f#1{\traceifset{s@tf@rstr@f}\edef\t@st{#1}%
 \trace{H}{s@tf@rstr@f #1}%
 \x@\let\x@\rh@ifOmitChapterNumberRH\csname ifOmitChapterNumberRH\endcsname
 \let\@@firstref\empty
 \ifx\t@st\empty\else
  \ifx\t@st\t@tle\else\ifx\t@st\intr@\else
    \trace{H}{Extracting 1st ref from "\t@st"}\x@\extr@ctfirst\t@st\relax\relax
    \ifcsname ifOmitChapterNumberRH@\detokenize{\@book}\endcsname
      \x@\let\csname rh@ifOmitChapterNumberRH\x@\endcsname\csname ifOmitChapterNumberRH@\detokenize{\@book}\endcsname
    \else 
      \trace{H}{OmitChapterNumberRH@\detokenize{\@book} doesn't exist}%
    \fi\relax
    \edef\@@firstref{\ifx\@book\empty\else
      \ifOmitBookRef\else\@book\fi
      \ifOmitChapterNumberRH\else\bkchs@p\@chapter\fi
      \ifVerseRefs
        \ifOmitChapterNumberRH\bkchs@p\else\cvs@p\fi
        \@verse
      \fi
    \fi}%
  \fi\fi
 \fi
 \let\ifOmitChapterNumberRH\rh@ifOmitChapterNumberRH
 \traceifcheck{s@tf@rstr@f}%
}
\def\f@rstref#1{\s@tf@rstr@f{#1}\@@firstref}

%
% Print the last reference from the mark data
% (same as above unless there are bridged verses)
%
\def\l@stref#1{\traceifset{l@stref}\edef\t@st{#1}%
 \trace{H}{l@stref #1}%
 \x@\let\x@\rh@ifOmitChapterNumberRH\csname ifOmitChapterNumberRH\endcsname
 \ifx\t@st\empty\else\ifx\t@st\t@tle\else\ifx\t@st\intr@\else
     \x@\extr@ctlast\t@st\relax
     \ifx\@@book\empty\else
       \ifcsname ifOmitChapterNumberRH@\detokenize{\@@book}\endcsname
         \x@\let\x@\rh@ifOmitChapterNumberRH\csname ifOmitChapterNumberRH@\detokenize{\@@book}\endcsname
       \fi
       \fi
       \ifx\@@book\t@tle\else
         \ifOmitBookRef\else\@@book\fi\ifOmitChapterNumberRH\else\bkchs@p\@@chapter\fi
         \ifVerseRefs\ifOmitChapterNumberRH\bkchs@p\else\cvs@p\fi\@@verse\fi
       \fi
       
     \fi
 \fi\fi\fi\traceifcheck{l@stref}%
 \let\ifOmitChapterNumberRH\rh@ifOmitChapterNumberRH
}

%
% Print the range of references from a pair of marks
%
\edef\emptym@rk{::}
\def\r@ngerefs#1#2{%
 \traceifset{r@ngerefs}%
 %\tracingassigns=1\tracingifs=1
 \edef\t@st{#1}\ifx\t@st\t@tle\x@\extr@ctfirst\emptym@rk\relax\else\ifx\t@st\empty\x@\extr@ctfirst\emptym@rk\relax\else\ifx\t@st\intr@\x@\extr@ctfirst\emptym@rk\relax\else\x@\extr@ctfirst\t@st\relax\fi\fi\fi
 \edef\t@st{#2}\ifx\t@st\t@tle\x@\extr@ctlast\emptym@rk\relax\else\ifx\t@st\empty\x@\extr@ctlast\emptym@rk\relax\else\ifx\t@st\intr@\x@\extr@ctfirst\emptym@rk\relax\else\x@\extr@ctlast\t@st\relax\fi\fi\fi
 \trace{v}{r@ngerefs \@book=\@chapter\cvs@p\@verse, \@@book=\@@chapter\cvs@p\@@verse}%
 \let\rh@ifOmitChapterNumberRH\ifOmitChapterNumberRH
 \let\if@OmitChapterNumberRH\ifOmitChapterNumberRH % For second book
 \ifx\@book\relax\else
   \ifcsname ifOmitChapterNumberRH@\detokenize{\@book}\endcsname
     \x@\let\csname ifOmitChapterNumberRH\x@\endcsname\csname ifOmitChapterNumberRH@\detokenize{\@book}\endcsname
   \fi
 \fi
 \ifcsname ifOmitChapterNumberRH@\detokenize{\@@book}\endcsname
   \x@\let\csname if@OmitChapterNumberRH\x@\endcsname\csname ifOmitChapterNumberRH@\detokenize{\@@book}\endcsname
 \fi
 \ifx\@book\empty\else
  \ifOmitBookRef\else\@book\fi
  \ifVerseRefs
   \ifOmitChapterNumberRH\else
    \ifx\@chapter\empty\else\bkchs@p\@chapter\cvs@p\fi\@verse
    \ifx\@book\@@book
     \ifx\@chapter\@@chapter
      \ifx\@verse\@@verse\else\ranges@p\@@verse\fi
     \else
      \rangechs@p\if@OmitChapterNumberRH\else\@@chapter\cvs@p\fi\@@verse
     \fi
    \else
     \rangechs@p\ifOmitBookRef\else\@@book\fi\bkchs@p\if@OmitChapterNumberRH\else\@@chapter\cvs@p\fi\@@verse
    \fi
   \fi
  \else
   \ifx\@book\@@book
    \ifOmitChapterNumberRH\else
     \ifx\@chapter\empty\else
      \bkchs@p\@chapter
      \ifx\@chapter\@@chapter\else\ifx\@@chapter\empty\else
       \setbox0=\hbox{\tracinglostchars=0
       \global\c@untA=0\@chapter \global\c@untB=0\@@chapter}%
       \advance\c@untA by 1
       \ifnum\c@untA=\c@untB \pairs@p \else \rangechs@p \fi
       \@@chapter
      \fi\fi
     \fi
    \fi
   \else
    \if@OmitChapterNumberRH
      \rangechs@p \@@book
    \else
      \bkchs@p\@chapter \rangechs@p \@@book \bkchs@p\@@chapter
    \fi
   \fi
  \fi
 \fi
 \traceifcheck{r@ngerefs}%
 \let\ifOmitChapterNumberRH\rh@ifOmitChapterNumberRH
 %\tracingassigns=0
}
\newcount\c@untA \newcount\c@untB

%
% extract the starting reference of a (possible) range
% putting the result into \@book, \@chapter, \@verse
%
\catcode`-=11
\def\extr@ctfirst#1:#2:#3\relax{%
 \def\@book{#1}\def\@chapter{#2}\def\t@st{#3}%
 \catcode`-=11\x@\spl@tverses\t@st --\relax\catcode`-=12
 \edef\@verse{\v@rsefrom}}

%
% extract the ending reference of a (possible) range
% putting the result into \@@book, \@@chapter, \@@verse
%
\def\extr@ctlast#1:#2:#3\relax{%
 \def\@@book{#1}\def\@@chapter{#2}\def\t@st{#3}%
 \catcode`-=11\x@\spl@tverses\t@st --\relax\catcode`-=12
 \edef\@@verse{\v@rseto}}

%
% split a possible verse range on hyphen, setting \v@rsefrom and \v@rseto
%
\def\getcatcodes#1#2\E{#1=\the\catcode`#1 \space \ifx #2\relax\else\getcatcodes #2\E\E\fi}
\catcode`\-=11
\def\spl@tverses#1-#2-#3\relax{%
 \edef\v@rsefrom{#1}\edef\v@rseto{#2}%
 \trace{v}{spl@tverses #1 - #2 - #3, \getcatcodes#1\E\E}%
 \ifx\v@rseto\empty\let\v@rseto=\v@rsefrom\fi}
\catcode`-=12
\newif\ifVerseRefs % whether to include verse numbers, or only book+chapter

%
% Specify separators to use when constructing references
%
\def\ranges@p{\hbox{\RangeSeparator}} % box this to avoid possible bidi problems
\def\rangechs@p{\hbox{\RangeChapSeparator}} % box this to avoid possible bidi problems
\def\bkchs@p{\hbox{\BookChapSeparator}} % box this to avoid possible bidi problems
%\def\pairs@p{,\kern.2em}
\let\pairs@p\ranges@p % box to use when it's e.g. 1,2 (normally the same as 1-3)
\def\cvs@p{\hbox{\ChapterVerseSeparator}}
\def\endash{\char"2013\relax}
\let\RangeChar\endash
\def\BookChapSeparator{\ }
\def\RangeSeparator{\kern.1em\RangeChar\kern.1em} % what to put between first - last of a range, when it's verse-verse
\def\RangeChapSeparator{\kern.1em\RangeChar\kern.1em} % what to put between first - last of a range, when it's chapter:verse-chapter:verse

\def\ChapterVerseSeparator{\kern.02em.\kern.02em} % what to put between chapter:verse

%
% Running headers/footers that may use the references defined above
%


% define \headline for use by the output routine
\headline={\hbox to \textwidth{%
 \mcpush{P}{h}%
 \s@tfont{h}{h}%
 \ifm@rksonpage\trace{H}{marks on page pfm:\p@gefirstmark -> \p@gebotmark}\else\trace{H}{No marks on page. old pfm:\p@gefirstmark}\gdef\p@gefirstmark{}\fi
 \edef\t@st{\p@gefirstmark}% check first mark on page to see if this is a "title" page
 \trace{h}{page \the\pageno (h), first mark=\t@st \space p@geone\ifp@geone true\else false\fi}%
 \global\rhr@letrue
 \op@ninghooks{start}{h}{h}%
 \ifnum\ifx\t@st\empty\ifp@geone 0\else 1\fi\else\ifx\t@st\intr@\ifp@geone 0\else 1\fi\else 0\fi\fi =1
  \trace{h}{no p@gefirstmark (\t@st). Page 1 is \ifp@geone True\else False\fi}%
    \ifodd\pageno
      \the\noVoddhead
      \trace{h}{using noVoddhead}% 
    \else
      \ifDoubleSided \trace{h}{using noVevenhead}\the\noVevenhead \else \trace{h}{single sided: using noVoddhead}\the\noVoddhead\fi
    \fi
  %\global\rhr@lefalse
 \else
    \trace{h}{Testing for \t@tle\space in \t@st}%
    \ifnum\ifx\t@st\t@tle 1\else\ifp@geone 1\else 0\fi\fi =1
      \ifodd\pageno\the\titleoddhead\trace{h}{using titleoddhead}\else\the\titleevenhead\trace{h}{using titleevenhead}\fi
      \global\rhr@lefalse
    \else
      \ifodd\pageno
        \the\oddhead
      \else
        \ifDoubleSided \the\evenhead \else \the\oddhead\fi
      \fi
    \fi
%  \fi
 \fi
 \cl@singhooks{end}{h}{h}%
 \mcpop
}}

% default headers are made of three components, placed left, center and right
\newtoks\oddhead
\newtoks\evenhead
\newtoks\titleoddhead
\newtoks\titleevenhead
\newtoks\noVoddhead
\newtoks\noVevenhead
\newtoks\oddfoot
\newtoks\evenfoot
\newtoks\titleevenfoot
\newtoks\titleoddfoot
\newtoks\noVoddfoot
\newtoks\noVevenfoot
\def\RF@even@set{%
	\ifx\RFtitleevenleft\undefined\let\RF@left\RFtitleleft\else\let\RF@left\RFtitleevenleft\fi
	\ifx\RFtitleevenright\undefined\let\RF@right\RFtitleright\else\let\RF@center\RFtitleevenright\fi
	\ifx\RFtitleevencenter\undefined\let\RF@center\RFtitlecenter\else\let\RF@center\RFtitleevencenter\fi
}
\def\RF@odd@set{%
	\ifx\RFtitleoddleft\undefined\let\RF@left\RFtitleleft\else\let\RF@left\RFtitleoddleft\fi
	\ifx\RFtitleoddright\undefined\let\RF@right\RFtitleright\else\let\RF@right\RFtitleoddright\fi
	\ifx\RFtitleoddcenter\undefined\let\RF@center\RFtitlecenter\else\let\RF@center\RFtitleoddcenter\fi
}
\def\RH@even@set{%
	\ifx\RHtitleevenleft\undefined\let\RH@left\RHtitleleft\else\let\RH@left\RHtitleevenleft\fi
	\ifx\RHtitleevenright\undefined\let\RH@right\RHtitleright\else\let\RH@center\RHtitleevenright\fi
	\ifx\RHtitleevencenter\undefined\let\RH@center\RHtitlecenter\else\let\RH@center\RHtitleevencenter\fi
}
\def\RH@odd@set{%
	\ifx\RHtitleoddleft\undefined\let\RH@left\RHtitleleft\else\let\RH@left\RHtitleoddleft\fi
	\ifx\RHtitleoddright\undefined\let\RH@right\RHtitleright\else\let\RH@right\RHtitleoddright\fi
	\ifx\RHtitleoddcenter\undefined\let\RH@center\RHtitlecenter\else\let\RH@center\RHtitleoddcenter\fi
}

\def\sethe@ddbl#1#2#3#4{%
  \csname #1#2#3\endcsname={\csname #4@#2@set\endcsname\lshiftc@lumn{\ifRTL 1\else 0\fi}\rlap{\hbox{\csname #4#1#2left\endcsname}}\hfil\rshiftc@l@mn{\ifRTL 1\else 0\fi}\csname #4#1#2center\endcsname\lshiftc@l@mn{\ifRTL 0\else 1\fi}\hfil\llap{\hbox{\csname #4#1#2right\endcsname}}\rshiftc@lumn{\ifRTL 0\else 1\fi}}%
}

\def\sethe@dsgl#1#2#3#4{%
  \csname #1#2#3\endcsname={\csname #4@#2@set\endcsname\lshiftc@lumn{\the\pageno}\rlap{\hbox{\csname #4#1#2left\endcsname}}\hfil\csname #4#1#2center\endcsname\hfil\llap{\hbox{\csname #4#1#2right\endcsname}}\rshiftc@lumn{\the\pageno}}%
}
\def\@sethe@d#1{\edef\tmp{#1}%
  \expandafter\@@sethe@d\tmp\relax\relax\relax}

\def\defineheads{
  \trace{M}{Defining heads for \mainBodyColumns \space columns}%
  \ifnum\mainBodyColumns=2
    \let\@@sethe@d\sethe@ddbl
  \else
    \let\@@sethe@d\sethe@dsgl
  \fi
  \edef\@list{{odd}{head}{RH},{odd}{foot}{RF},{even}{head}{RH},{even}{foot}{RF}}%
  \def\d@##1\E{%
    \@sethe@d{{}##1}
    \@sethe@d{{noV}##1}
    \@sethe@d{{title}##1}
  }\x@\cstackdown\@list,\E
}


% \footline is similar except it doesn't have to place a border graphic
\footline={{%
 \mcpush{P}{h}%
 \mcpush{C}{zfooter}%
 \s@tfont{zfooter}{zfooter+h}%
 \ifm@rksonpage\trace{H}{marks on page pfm:\p@gefirstmark}\else\trace{H}{No marks on page. old pfm:\p@gefirstmark}\gdef\p@gefirstmark{}\fi
 \edef\t@st{\p@gefirstmark}% check first mark on page to see if this is a "title" page
 \trace{h}{page \the\pageno (f), first mark=\t@st}%
 \ifnum\ifx\t@st\empty\ifp@geone 0\else 1\fi\else 0\fi =1
   \ifodd\pageno \the\noVoddfoot \else 
     \ifDoubleSided \the\noVevenfoot \else \the\noVoddfoot \fi\fi
 \else
   \ifnum\ifx\t@st\t@tle 1\else\ifp@geone 1\else 0\fi\fi =1
     \ifodd\pageno\the\titleoddfoot\else\the\titleevenfoot\fi
     \else
       \ifodd\pageno \the\oddfoot \else
         \ifDoubleSided \the\evenfoot \else \the\oddfoot \fi
 \fi\fi\fi
 \mcpop\mcpop
}}

\def\s@treffont{\let\tmp@dstat\c@rrdstat\ifuseRightMarks\setc@rdstat{R}\else\setc@rdstat{L}\fi\s@tfont{h}{h}\setc@rdstat{\tmp@dstat}}
\def\headfootL#1{\headfoot L{#1}}
\def\headfootR#1{\headfoot R{#1}}
\def\headfoot#1#2{\bgroup\let\tmp@dstat\c@rrdstat\setc@rdstat{#1}\x@\the\csname diglot#1ho@ks\endcsname\ifRTL\beginR\fi\s@tfont{h}{h}#2\ifRTL\endR\fi\setc@rdstat{\tmp@dstat}\egroup}

%
% user-level macros for use within the running header
%
\def\firstref{\ifRTL\beginR\fi\bgroup\ifdiglot\s@treffont\fi\f@rstref{\p@gefirstmark}\egroup\ifRTL\endR\fi}
\def\lastref{\ifRTL\beginR\fi\bgroup\ifdiglot\s@treffont\fi\l@stref{\p@gebotmark}\egroup\ifRTL\endR\fi}
\def\rangeref{\ifRTL\beginR\fi\bgroup\ifdiglot\s@treffont\fi\r@ngerefs{\p@gefirstmark}{\p@gebotmark}\egroup\ifRTL\endR\fi}


\def\header@defs#1{%polyglot references
  \def@cseq{firstref#1}{\headfoot #1{\f@rstref{\csname first#1mark\endcsname}}}
  \def@cseq{lastref#1}{\headfoot #1{\l@stref{\csname bot#1mark\endcsname}}}
  \def@cseq{rangeref#1}{\headfoot #1{\r@ngerefs{\csname first#1mark\endcsname}{\csname bot#1mark\endcsname}}}
  \def@cseq{pagenumber#1}{\headfoot #1{\folio}}
  \def@cseq{usdate#1}{\headfoot #1{\usdate}}
  \def@cseq{ukdate#1}{\headfoot #1{\ukdate}}
  \def@cseq{isodate#1}{\headfoot #1{\isodate}}
  \def@cseq{hrsmins#1}{\headfoot #1{\hrsmins}}
  \def@cseq{timestamp#1}{\headfoot #1{\timestamp}}
}

\let\pagenumber=\folio

\def\ROMANnumeral#1{\expandafter\uppercase\expandafter{\romannumeral#1}}
\def\usdate{\number\month/\number\day/\number\year}
\def\ukdate{\number\day/\number\month/\number\year}
\def\isodate{\number\year-\ifnum\month<10 0\fi\number\month-\ifnum\day<10 0\fi\number\day}

\header@defs{L}
\header@defs{R}
%
% default settings of the running header components
%
\def\RHoddleft{\empty}
\def\RHoddcenter{\rangeref}
\def\RHoddright{\pagenumber}

\def\RHevenleft{\pagenumber}
\def\RHevencenter{\rangeref}
\def\RHevenright{\empty}

\def\RHnoVoddleft{\empty}
\def\RHnoVoddcenter{\empty}
\def\RHnoVoddright{\pagenumber}

\def\RHnoVevenleft{\pagenumber}
\def\RHnoVevencenter{\empty}
\def\RHnoVevenright{\empty}

\def\RHtitleleft{\empty}
\def\RHtitlecenter{\empty}
\def\RHtitleright{\empty}

\def\RFoddleft{\empty}
\def\RFoddcenter{\empty}
\def\RFoddright{\empty}

\def\RFevenleft{\empty}
\def\RFevencenter{\empty}
\def\RFevenright{\empty}

\def\RFnoVoddleft{\empty}
\def\RFnoVoddcenter{\empty}
\def\RFnoVoddright{\empty}

\def\RFnoVevenleft{\empty}
\def\RFnoVevencenter{\empty}
\def\RFnoVevenright{\empty}

\def\RFtitleleft{\empty}
\def\RFtitleevencenter{\pagenumber}
\def\RFtitleoddcenter{\pagenumber}
\def\RFtitleright{\empty}

\def\HEADFOOTset#1#2#3#4#5#6#7#8{%
  \x@\gdef\csname R#1#2evenleft\endcsname{#3}%
  \x@\gdef\csname R#1#2evencenter\endcsname{#4}%
  \x@\gdef\csname R#1#2evenright\endcsname{#5}%
  \x@\gdef\csname R#1#2oddleft\endcsname{#6}%
  \x@\gdef\csname R#1#2oddcenter\endcsname{#7}%
  \x@\gdef\csname R#1#2oddright\endcsname{#8}}%
%usage: #1 H or F (head or foot)
% #2 {}, {title} {noV}
% #3-#8 Left,centre,right / left, centre,right for even,odd pages.

\endinput
