summaryrefslogtreecommitdiff
path: root/tex/context/base/strc-ref.tex
diff options
context:
space:
mode:
Diffstat (limited to 'tex/context/base/strc-ref.tex')
-rw-r--r--tex/context/base/strc-ref.tex1905
1 files changed, 1905 insertions, 0 deletions
diff --git a/tex/context/base/strc-ref.tex b/tex/context/base/strc-ref.tex
new file mode 100644
index 000000000..23fc3e01e
--- /dev/null
+++ b/tex/context/base/strc-ref.tex
@@ -0,0 +1,1905 @@
+%D \module
+%D [ file=strc-ref,
+%D version=2008.10.20,
+%D title=\CONTEXT\ Structure Macros,
+%D subtitle=Cross Referencing,
+%D author=Hans Hagen,
+%D date=\currentdate,
+%D copyright=PRAGMA-ADE / Hans Hagen]
+%C
+%C This module is part of the \CONTEXT\ macro||package and is
+%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
+%C details.
+
+\writestatus{loading}{ConTeXt Structure Macros / Cross Referencing}
+
+\registerctxluafile{strc-ref}{1.001}
+
+\unprotect
+
+%D This module is a (partial) rewrite of core-ref.tex for \MKIV. As
+%D such it will be a moving target for a while.
+
+%D Later we will do a further cleanup and move much of the code to
+%D \LUA\ (i.e.\ better backend integration).
+
+\let\mainreference\gobblefivearguments
+
+% this will go when we got rid of the tuo file
+
+\let\currentfolioreference \!!zerocount % only used in xml-fo
+\let\resetreferences \relax
+\let\setreferences \relax
+\let\showcurrentreference \relax
+\let\setexecutecommandcheck\gobbletwoarguments
+
+\def\s!full{full}
+\def\s!text{text}
+\def\s!page{page}
+
+% todo : unknown/illegal reference no arg
+% todo : +n pages check on 'samepage' (contrastcolor)
+% todo : multiple text in reference
+
+% Makes more sense to build action data first, especially now
+% openaction etc are supported.
+%
+% \definespecial\doexecuteactionchain w h
+% \definespecial\dosetgotolocation
+% \definespecial\dosetexecuteJScode
+% ...
+
+%D This module deals with referencing. In \CONTEXT\ referencing
+%D is one of the core features, although at a first glance
+%D probably nobody will notice. This is good, because
+%D referencing should be as hidden as possible.
+%D
+%D In paper documents, referencing comes down to cross
+%D referencing, but in their interactive counterparts, is also
+%D involves navigation. Many features implemented here are
+%D therefore closely related to navigation.
+%D
+%D Many \CONTEXT\ commands can optionally be fed with a
+%D reference. Such a reference, when called upon, returns the
+%D number of a figure, table, chapter etc, a piece of text, or
+%D a pagenumber.
+%D
+%D There are three ways of defining a reference:
+%D
+%D \starttyping
+%D \pagereference[here]
+%D \textreference[here]{some text}
+%D \stoptyping
+%D
+%D the third alternative combines them in:
+%D
+%D \starttyping
+%D \reference[here]{some text}
+%D \stoptyping
+
+\def\textreference {\dosingleargument\dotextreference}
+\def\pagereference {\dosingleargument\dopagereference}
+\def\reference {\dosingleargument\doreference }
+
+%D These are implemented in a low level form as:
+
+\def\dotextreference[#1]{\dosetreference\s!text{#1}}
+\def\dopagereference[#1]{\dosetreference\s!page{#1}{}}
+\def\doreference [#1]{\dosetreference\s!full{#1}}
+
+%D Actually there is not much difference between a text and a
+%D full reference, but it's the concept that counts. The low
+%D level implementation is:
+
+\newcount\crossreferencenumber
+
+\def\dofinishfullreference#1#2%
+ {\normalexpanded{\noexpand\ctxlatelua{jobreferences.enhance("#1","#2")}}%
+ \referenceinfo>{#1\letterbar#2}}
+
+\let\dofinishpagereference\dofinishfullreference
+
+\def\dofinishtextreference#1#2%
+ {\normalexpanded{\noexpand\ctxlatelua{jobreferences.enhance("#1","#2",{})}}%
+ \referenceinfo>{#1\letterbar#2}}
+
+\def\dosetreference#1#2#3% kind labels text -> todo: userdata
+ {\ifreferencing
+ \global\advance\crossreferencenumber\plusone
+ \edef\currentreferencekind{#1}%
+ \edef\currentreferencelabels{#2}%
+ \edef\currentreferenceexpansion{\@@rfexpansion}% {\referenceparameter\c!expansion}
+ \ifx\currentreferencelabels\empty \else
+ \ifx\currentreferenceexpansion\s!xml
+ \xmlstartraw
+ \xdef\currentreferencetext{#3}%
+ \xmlstopraw
+ \globallet\currentreferencecoding\s!xml
+ \else
+ \ifx\currentreferenceexpansion\v!yes
+ \xdef\currentreferencetext{#3}%
+ \else
+ \xdef\currentreferencetext{\detokenize{#3}}%
+ \fi
+ \globallet\currentreferencecoding\s!tex
+ \fi
+ \setnextinternalreference
+ \ctxlua {
+ jobreferences.set("\currentreferencekind", "\referenceprefix","\currentreferencelabels",
+ {
+ references = {
+ internal = \nextinternalreference,
+ block = "\currentstructureblock",
+ section = structure.sections.currentid(),
+ },
+ metadata = {
+ kind = "#1",
+ catcodes = \the\catcodetable,
+ xmlroot = \ifx\currentreferencecoding\s!xml "\xmldocument" \else nil \fi, % only useful when text
+ },
+ entries = {
+ text = \!!bs\currentreferencetext\!!es
+ }
+ })
+ jobreferences.setinternalreference("\referenceprefix","\currentreferencelabels",\nextinternalreference)
+ }%
+ \fi
+ \fi}
+
+%D For compatibility we provide:
+
+\def\rawreference #1#2#3{\dosetreference\s!full{#2}{#3}} % tag, labels, text
+\def\rawpagereference #1#2{\dosetreference\s!page{#2}{}} % tag, labels
+\def\rawtextreference#1#2#3{\dosetreference\s!text{#2}{#3}} % tag, labels, text
+
+\def\defaultreferencepage#1{[[[#1]]]}
+\def\defaultreferencetext#1{[[[#1]]]}
+
+%D These macros depend on three other ones,
+%D \type {\makesectionformat}, that generated \type
+%D {\sectionformat}, \type {\pagenumber}. The not yet used
+%D argument \type{#1} is a tag that specifies the type of
+%D reference.
+
+%D \macros
+%D {everyreference}
+%D
+%D For rather tricky purposes, one can assign sanitizing
+%D macros to \type{\everyreference} (no longer that relevant).
+
+\newevery \everyreference \relax
+
+%D This is really needed, since for instance Polish has a
+%D different alphabet and needs accented entries in registers.
+
+\appendtoks
+ \cleanupfeatures
+\to \everyreference
+
+%D We did not yet discuss prefixing. Especially in interactive
+%D documents, it's not always easy to keep track of duplicate
+%D references. The prefix mechanism, which we will describe
+%D later on, solves this problem. By (automatically) adding a
+%D prefix one keeps references local, but the global ones in
+%D view. To enable this feature, we explictly split the prefix
+%D from the reference.
+
+\let\referenceprefix\empty
+
+%D For a long time the only way to access an external file was
+%D to use the file prefix (\type {somefile::}. However, when
+%D you split up a document, redefining the references may be
+%D such a pain, that another approach is feasible. By setting
+%D the \type {autofile} variable to \type {yes} or \type
+%D {page}, you can access the reference directly.
+%D
+%D \starttabulate[||||]
+%D \NC filename::tag \NC page(filename::pnum) \NC tag \NC\NR
+%D \NC $\star$ \NC \NC \NC\NR
+%D \NC $\star$ \NC $\star$ \NC $\star$ \NC\NR
+%D \NC \NC $\star$ \NC \NC\NR
+%D \stoptabulate
+
+\def\usereferences[#1]%
+ {\writestatus\m!systems{references from other files are handled automatically}}
+
+%D As mentioned we will also use the cross reference mechanism
+%D for navigational purposes. The main reason for this is that
+%D we want to treat both categories alike:
+%D
+%D \starttyping
+%D \goto{go back}[PreviousJump]
+%D \goto{colofon}[colofon page]
+%D \stoptyping
+%D
+%D Here \type{PreviousJump} is handled by the viewer, while the
+%D \type{colofon page} reference is, apart from hyperlinking, a
+%D rather normal reference.
+%D
+%D We already saw that cross refences are written to and read
+%D from a file. The pure navigational ones don't need to be
+%D written to file, but both for fast processing and
+%D transparant integration, they are saved internally as a sort
+%D of reference. We can easily distinguish such system
+%D references from real cross reference ones by their tag.
+%D
+%D We also use the odd/even characteristic to determine the
+%D page state.
+
+\let\currentrealreference \empty
+\let\currentpagereference \empty
+\let\currenttextreference \empty
+\let\currentreferenceorder \empty
+\let\currentsubtextreference \empty
+\let\currentsubsubtextreference\empty
+
+%D System references only have one component:
+
+\newif\ifforwardreference
+\newif\ifrealreferencepage
+
+\def\docheckrealreferencepage#1% todo
+ {\doifnumberelse{#1}
+ {\ifnum#1=\realpageno
+ \realreferencepagetrue
+ \else
+ \realreferencepagefalse
+ \fi}
+ {\realreferencepagefalse}}
+
+%D Text references can contain more than one entry and
+%D therefore we check for
+%D
+%D \starttyping
+%D {entry}
+%D \stoptyping
+%D
+%D or
+%D
+%D \starttyping
+%D {{entry}{entry}{entry}}
+%D \stoptyping
+%D
+%D and split accordingly.
+
+% todo:
+
+\def\doifforwardreferenceelse#1#2% todo
+ {\iffalse}
+
+%D Cross references appear as numbers (figure~1.1, chapter~2)
+%D or pagenumbers (page~2, page 3--2), and are called with
+%D \type{\in} and \type{\at}. In interactive documents we also
+%D have \type{\goto}, \type{\button} and alike. These are more
+%D versatile and look like:
+%D
+%D \starttyping
+%D \goto[reference]
+%D \goto[outer reference::]
+%D \goto[outer reference::inner reference]
+%D \goto[operation(argument)]
+%D \goto[operation(action{argument,argument})]
+%D \goto[action]
+%D \goto[action{argument}]
+%D \stoptyping
+%D
+%D The first one is a normal reference, the second and third
+%D are references to a file or \URL. The brace delimited
+%D references for instance refer to a \JAVASCRIPT. The last
+%D example shows that we can pass arguments to the actions.
+%D
+%D When we split off the components of such a reference, the
+%D results are available in:
+%D
+%D \starttyping
+%D \currentreferencespecial
+%D \currentreferenceoperation
+%D \currentreferencearguments
+%D \currentinnerreference
+%D \currentouterreference
+%D \currentfullreference
+%D \stoptyping
+
+\newif\ifreferencefound
+
+\let\currentfullreference \empty
+\let\currentreferencespecial \empty
+\let\currentreferenceoperation\empty
+\let\currentreferencearguments\empty
+\let\currentouterreference \empty
+\let\currentinnerreference \empty
+
+\def\setreferencevariables#1#2#3#4#5%
+ {\def\currentreferencespecial {#1}%
+ \def\currentreferenceoperation{#2}%
+ \def\currentreferencearguments{#3}%
+ \def\currentouterreference {#4}%
+ \def\currentinnerreference {#5}}
+
+%D Now we've come to the testing step. As we can see below,
+%D this macro does bit more than testing: it also resolves
+%D the reference. This means that whenever we test for the
+%D existance of a reference at an outer level, we have all the
+%D relevant properties of that reference avaliable inside the
+%D true branche~(\type{#2}).
+%D
+%D The prefix has to do with localizing references. When a
+%D prefix is set, looking for a reference comes to looking for
+%D the prefixed one, and when not found, looking for the non
+%D prefixed one. Consider for instance the prefix set to
+%D \type{sidetrack}.
+%D
+%D \starttyping
+%D \pagereference[important]
+%D \pagereference[unimportant]
+%D \setupreferencing[prefix=sidetrack]
+%D \pagereference[important]
+%D \stoptyping
+%D
+%D results in saving (writing) the references
+%D
+%D \starttyping
+%D ...{}{important}
+%D ...{}{unimportant}
+%D ...{sidetrack}{important}...
+%D \stoptyping
+%D
+%D Now when we call for \type{unimportant}, we will indeed get
+%D the pagenumber associated to this reference. But when we
+%D call for \type{important}, while the prefix is still set, we
+%D will get the pagenumber bound to the prefixed one.
+%D
+%D {\em Some day, when processing time and memory are no longer
+%D performance factors, we will introduce multi||level
+%D prefixes.}
+%D
+%D Before we start analyzing, I introduce a general
+%D definition macro. Consider:
+%D
+%D \starttyping
+%D \goto{do}[JS(My_Script{"test",123}),titlepage]
+%D \stoptyping
+%D
+%D This can also be achieved by:
+%D
+%D \starttyping
+%D \definereference[startup][JS(My_Script{"test",123}),titlepage]
+%D \goto{do}[REF(startup)]
+%D \stoptyping
+%D
+%D Now is this is a handy feature or not?
+%D
+%D \showsetup{definereference}
+%D
+%D We can trace references by setting the next switch to
+%D true.
+
+\def\definereference
+ {\dodoubleempty\dodefinereference}
+
+\def\dodefinereference[#1][#2]%
+ {\ctxlua{jobreferences.define("\referenceprefix","#1",\!!bs\detokenize{#2}\!!es)}}
+
+\def\resetreference[#1]%
+ {\ctxlua{jobreferences.reset("\referenceprefix","#1")}}
+
+\def\setpagereference#1#2% name, specification
+ {\ctxlua{jobreferences.define("","#1",\!!bs\v!page(\luaescapestring{#2})\!!es)}}
+
+%D Chained references are defined as:
+%D
+%D \starttyping
+%D \goto{somewhere}[JS(somescript),nextpage,JS(anotherscript)]
+%D \stoptyping
+%D
+%D Actually supporting chains is up to the special driver. Here
+%D we only provide the hooks.
+
+\newif \ifsecondaryreference
+\newcount\nofsecondaryreferences
+
+% the counter stuff should move to the (mkiv) backend
+
+\def\doifreferencefoundelse#1%
+ {\ctxlua{jobreferences.doifelse("\referenceprefix","#1")}}
+
+\def\doprocessreferenceelse#1#2#3%
+ {\doresetgotowhereever
+ \nofsecondaryreferences\zerocount
+ \def\primaryreferencefoundaction {\secondaryreferencefalse#2}%
+ \def\secondaryreferencefoundaction{\advance\nofsecondaryreferences\plusone\secondaryreferencetrue#2}%
+ \def\referenceunknownaction {#3}%
+ \ctxlua{jobreferences.handle("\referenceprefix","#1")}%
+ \doresetgotowhereever} % to prevent problems with direct goto's
+
+%D The inner case is simple. Only two cases have to be taken
+%D care of:
+%D
+%D \starttyping
+%D \goto{some text}[reference]
+%D \goto{some text}[prefix:reference]
+%D \stoptyping
+%D
+%D References to other files however are treated strict or
+%D tolerant, depending on their loading and availability:
+%D
+%D \starttyping
+%D \useexternaldocument[somefile][filename][a nice description]
+%D
+%D \goto{checked reference}[somefile::reference]
+%D \goto{unchecked reference}[somefile::]
+%D \goto{unchecked reference}[anotherfile::reference]
+%D \stoptyping
+%D
+%D An unknown reference is reported on the screen, in the log
+%D file and, when enabled, in the left margin of the text.
+
+\def\reportreferenceerror#1#2% only once (keep track in lua)
+ {\ifinpagebody \else
+ \doifconcepttracing{\doifsomething{#2}{\inleft{\infofont\doboundtext{#2}{\dimexpr\leftmarginwidth-2em\relax}{..}->}}}%
+ \fi
+ \showmessage\m!references{#1}{[\referenceprefix][#2]}}
+
+\def\unknownreference{\reportreferenceerror1}
+\def\illegalreference{\reportreferenceerror4}
+
+%D When a reference is not found, we typeset a placeholder
+%D (two glyphs are often enough to represent the reference
+%D text).
+
+\def\dummyreference{{\tttf ??}}
+
+%D To prevent repetitive messages concerning a reference
+%D being defined, we set such an unknown reference to an empty
+%D one after the first encounter.
+
+%D Sometimes we want to temporary put a reference out of
+%D order. An example can be found in the menu macros.
+%D
+%D \starttyping
+%D \doifreferencepermittedelse{reference}{set}{true}{false}
+%D \stoptyping
+%D
+%D The second argument can be a comma seperated list.
+
+\let\permittedreferences\empty
+
+ \def\doifreferencepermittedelse#1#2#3% ref found notfound
+ {\doprocessreferenceelse{#1}
+ {\donetrue
+ \ifx\permittedreferences\empty \else
+ \docheckifreferencepermitted{#1}%
+ \fi
+ \ifdone#2\else#3\fi}
+ {#3\unknownreference{#1}}}
+
+ \def\docheckifreferencepermitted#1%
+ {\ifx\currentinnerreference\empty
+ \ifx\currentouterreference\empty \else
+ \doifinstring{\currentouterreference::}\permittedreferences\donefalse
+ \fi
+ \else\ifx\currentouterreference\empty
+ \doifinstring{\currentinnerreference}\permittedreferences\donefalse
+ \else
+ \doifinstring{\currentouterreference::\currentinnerreference}\permittedreferences\donefalse
+ \fi\fi}
+
+%D Apart from cross references supplied by the user, \CONTEXT\
+%D generates cross references itself. Most of them are not
+%D saved as a reference, but stored with their source, for
+%D instance a list or an index entry. Such automatically
+%D generated, for the user invisible, references are called
+%D {\em internal references}. The user supplied ones are
+%D labeled as {\em external references}.
+%D
+%D A second important characteristic is that when we want to
+%D support different backends (viewers), we need to support
+%D named destinations as well as page numbers. I invite readers
+%D to take a glance at the special driver modules to understand
+%D the fine points of this. As a result we will deal with {\em
+%D locations} as well as {\em real page numbers}. We explictly
+%D call this pagenumber a real one, because it is independant
+%D of the page numbering scheme used in the document.
+%D
+%D One of the reasons for \CONTEXT\ being the first \TEX\ base
+%D macropackage to support sophisticated interactive \PDF\
+%D files, lays in the mere fact that real page numbers are
+%D available in most two pass data, like references, list data
+%D and index entries.
+%D
+%D We will speak of \type{thisis...} when we are marking a
+%D location, and \type{goto...} when we point to such a
+%D location. The latter one can be seen as a hyperlink to the
+%D former one. In the next macros one we use constructs like:
+%D
+%D \starttyping
+%D \dostart...
+%D \dostop...
+%D \stoptyping
+%D
+%D Such macros are used to invoke the relevant specials from
+%D the special driver modules (see \type{spec-ini}). The flag
+%D \type{\iflocation} signals if we're in interactive mode.
+
+\def\thisisdestination#1% destination
+ {\iflocation \ifusepagedestinations \else
+ \dostartthisislocation{#1}\dostopthisislocation
+ \fi \fi}
+
+\def\thisisrealpage#1% pagenumber
+ {\iflocation
+ \dostartthisisrealpage{#1}\dostopthisisrealpage
+ \fi}
+
+%D The previous tho macros were easy ones, opposite to their
+%D counterparts. A common component in these is:
+%D
+%D \starttyping
+%D \dohandlegoto{..}{..}{..}
+%D \stoptyping
+%D
+%D Here data can be whatever needs highlighting, e.g. {\em
+%D figure 2.4}, and the start and stop entries handle the
+%D specials. The two \DIMENSIONS\ \type{\buttonwidth} and
+%D \type{\buttonheight} have to be set when handling the
+%D data~(\type{#2}).
+
+\ifx\buttonheight\undefined \newdimen\buttonheight \fi
+\ifx\buttonwidth \undefined \newdimen\buttonwidth \fi
+
+\def\gotodestination#1#2#3#4#5% url file destination page data
+ {\iflocation
+ \ifusepagedestinations
+ \gotorealpage{#1}{#2}{\number#4}{#5}%
+ \else
+ \dohandlegoto
+ {#5}%
+ {\the\everyreference\dostartgotolocation\buttonwidth\buttonheight{#1}{#2}{#3}{\number#4}}%
+ {\dostopgotolocation}%
+ \fi
+ \else
+ {#5}%
+ \fi}
+
+ \def\gotorealpage#1#2#3#4% url file page data
+ {\iflocation
+ \dohandlegoto
+ {#4}%
+ {\dostartgotorealpage\buttonwidth\buttonheight{#1}{#2}{\number#3}}%
+ {\dostopgotorealpage}%
+ \else
+ {#4}%
+ \fi}
+
+\def\gotoinnerpage#1#2% page data
+ {\iflocation
+ \dohandlegoto
+ {#2}%
+ {\dostartgotorealpage\buttonwidth\buttonheight\empty\empty{\number#1}}%
+ {\dostopgotorealpage}%
+ \else
+ {#2}%
+ \fi}
+
+\def\gotoouterfilepage#1#2#3% file page data
+ {\iflocation
+ \dohandlegoto
+ {#3}%
+ {\dostartgotorealpage\buttonwidth\buttonheight\empty{#1}{\number#2}}%
+ {\dostopgotorealpage}%
+ \else
+ {#3}%
+ \fi}
+
+%D \macros
+%D {setreferencefilename}
+%D
+%D This command can be used in the special drivers to
+%D uppercase filenames. This is needed when one wants to
+%D produce \CDROM's conforming to ISO9660. We consider is the
+%D savest to enable this feature by default. We cannot handle
+%D uppercase here, since the suffix is handled in the special
+%D driver. Conversion is taken care of by:
+%D
+%D \starttyping
+%D \setreferencefilename somefilename\to\SomeFileName
+%D \stoptyping
+
+\chardef\referencefilecase=0
+
+ \def\setreferencefilename#1\to#2%
+ {\ifcase\referencefilecase
+ \edef#2{#1}%
+ \or
+ \uppercasestring#1\to#2%
+ \or
+ \lowercasestring#1\to#2%
+ \else
+ \edef#2{#1}%
+ \fi}
+
+%D Internal references can best be set using the next few
+%D macros. Setting such references to unique values is
+%D completely up to the macros that call them.
+%D
+%D \starttyping
+%D \thisissomeinternal{tag}{identifier}
+%D \gotosomeinternal {tag}{identifier}{pagenumber}{text}
+%D \stoptyping
+
+\def\thisissomeinternal#1#2% tag reference
+ {\doifsomething{#2}{\thisisdestination{#1:#2}}}
+
+\def\gotosomeinternal#1#2% #3#4
+ {\gotodestination\empty\empty{#1:#2}}
+
+%D An automatic mechanism is provided too:
+%D
+%D \starttyping
+%D \thisisnextinternal{tag}
+%D \gotonextinternal {tag}{number}{pagenumber}{text}
+%D \stoptyping
+%D
+%D The first macro increments a counter. The value of this
+%D counter is available in the macro \type{\nextinternalreference}
+%D and should be saved somewhere (for instance in a file) for
+%D future reference. The second argument of
+%D \type {\gotonextinternal} takes such a saved number. One can
+%D turn on tracing these references, in which case the
+%D references are a bit more verbose.
+
+\newcount\locationcount
+
+\newif\ifinternalnamedreferences \internalnamedreferencestrue
+
+\def\nextinternalreference
+ {\the\locationcount}
+
+\def\setnextinternalreference
+ {\global\advance\locationcount\plusone}
+
+\def\thisisnextinternal#1% #1 will be removed when we are done with mkiv
+ {\ifinternalnamedreferences
+ \thisisdestination{\s!aut:\nextinternalreference}%
+ \fi}
+
+\def\insertnextinternal#1%
+ {\ifinternalnamedreferences
+ \thisisdestination{\s!aut:\number#1}%
+ \fi}
+
+\def\gotonextinternal#1#2#3#4% #1 will be removed when we are done with mkiv
+ {\ifinternalnamedreferences
+ \gotodestination\empty\empty{\s!aut:#2}{#3}{#4}%
+ \else
+ \gotorealpage\empty\empty{#3}{#4}%
+ \fi}
+
+%D We already went through a lot of problems to sort out what
+%D kind of reference we're dealing with. Sorting out the user
+%D supplied cross references (show/goto this or that) as well
+%D as user supplied system references (invoke this or that) is
+%D already taken care of in the test routine, but we still have
+%D to direct the request to the right (first) routine.
+
+\def\gotolocation#1#2{\doprocessreferenceelse{#1}{#2}{\unknownreference{#1}}} % obsolete
+
+%D An inner reference refers to some place in the document
+%D itself.
+
+ \def\gotoinnerlocation#1% #2%
+ {\gotodestination\empty\empty{\referenceprefix\currentinnerreference}\currentrealreference} % {#2}
+
+\def\gotoinner#1#2#3% prefix inner page data
+ {\gotodestination\empty\empty{#1#2}{#3}} % {#4}
+
+%D The outer location refers to another document, specified as
+%D file or \URL.
+
+ \def\gotoouterlocation#1#2% % page checken!
+ {\bgroup
+ \let\referenceprefix\empty
+ \setouterlocation\currentouterreference
+ \ifx\currentinnerreference\empty
+ \gotorealpage\otherURL\otherfile1{#2}%
+ \else
+ \gotodestination\otherURL\otherfile\currentinnerreference\currentrealreference{#2}%
+ \fi
+ \egroup}
+
+\def\gotoouterfile#1#2% file location page data #3 #4
+ {\doifelsenothing{#2}{\gotorealpage\empty{#1}}{\gotodestination\empty{#1}{#2}}}
+
+\def\gotoouterfilepage#1% file page data
+ {\gotorealpage\empty{#1}\empty}
+
+\def\gotoouterfilelocation% file location page data
+ {\gotodestination\empty}
+
+\def\gotoouterurl#1#2% url args data #2
+ {\gotodestination{#1}\empty{#2}1}
+
+%D Special locations are those that are accessed by saying
+%D things like:
+%D
+%D \starttyping
+%D \goto{calculate total}[JS(summarize{10,23,56}]
+%D \stoptyping
+%D
+%D After several intermediate steps this finally arrives at
+%D the next macro and expands into (simplified):
+%D
+%D \starttyping
+%D \gotoJSlocation{total{summarize{10,23,56}}}{calculate total}
+%D \stoptyping
+%D
+%D The first argument is the full reference, the second one
+%D is the text, in some kind of manipulated form. In practice
+%D we split references, so we get:
+%D
+%D \starttyping
+%D \gotoJSlocation{summarize{10,23,56}}{calculate}
+%D \gotoJSlocation{summarize{10,23,56}}{total}
+%D \stoptyping
+%D
+%D where \type{calculate} and \type{total} are colored, boxed
+%D or whatever \type{\goto} is told to do.
+%D
+%D The macro \type{\gotoJSlocation} can use \type
+%D {\currentreferenceoperation} (in our example
+%D \type{summarize}) and \type{\currentreference} (here
+%D being \type {10,23,56}) to perform its task.
+
+ \def\gotospeciallocation
+ {\executeifdefined{goto\currentreferencespecial location}\gobbleoneargument}
+
+%D Such special macros can be defined by:
+
+ \def\definespeciallocation#1%
+ {\setvalue{goto#1location}}
+
+%D The associated test is to be defined by:
+
+\def\definespecialtest#1%
+ {\setvalue{\s!do:\v!test:#1}}
+
+%D This \type{\def} alike macro is to be used as:
+%D
+%D \starttyping
+%D \definespeciallocation{JS}#1#2{... #1 ... #2 ...}
+%D \stoptyping
+%D
+%D In module \type {java-ini} one can see that \type
+%D {\gotoJSlocation} looks much like the previous goto
+%D definitions.
+
+%D In this module we define three system references: one for
+%D handling navigational, viewer specific, commands, another
+%D for jumping to special pages, like the first or last one,
+%D and a third reference for linking tree like lists, like
+%D tables of contents. The latter two adapt themselves to the
+%D current state.
+%D
+%D An example of an action is:
+%D
+%D \starttyping
+%D \goto{some action}[PreviousJump]
+%D \stoptyping
+%D
+%D as well as:
+%D
+%D \starttyping
+%D \goto{some text}[\v!action(PreviousJump]
+%D \stoptyping
+
+% compatibility hack
+
+\def\setglobalsystemreference#1#2#3{\definereference[#2][\v!action(#3)]}
+
+% action actions
+
+\def\gotoactionspecial#1#2#3#4% special operation arguments data
+ {\begingroup
+ \iflocation
+ \dohandlegoto
+ {#4}%
+ {\dostartexecutecommand\buttonwidth\buttonheight{#2}{#3}}%
+ {\dostopexecutecommand}%
+ \else
+ #4%
+ \fi
+ \endgroup}
+
+\def\gotopagespecial#1#2#3#4% page(n) page(+n) page(-n) page(file::1)
+ {\begingroup
+ \iflocation
+ \doifnonzeropositiveelse{#2}
+ {\doifinstringelse+{#2}
+ {\edef\currenttargetpage{\the\numexpr\realpageno#2}}
+ {\doifinstringelse-{#2}
+ {\edef\currenttargetpage{\the\numexpr\realpageno#2}}
+ {\edef\currenttargetpage{#2}}}}%
+ {\edef\currenttargetpage{1}}%
+ \docheckrealreferencepage\currenttargetpage % new
+ \gotorealpage\empty\empty\currenttargetpage{#4}%
+ \else
+ #4%
+ \fi
+ \endgroup}
+
+%D It is possible to disable the writing of references to the
+%D utility file by setting:
+
+\newif\ifreferencing \referencingtrue
+
+%D One can also activate an automatic prefix mechanism. By
+%D setting the \type{\prefix} variable to \type{+}, the prefix
+%D is incremented, when set to \type{-} or empty, the prefix is
+%D reset. Other values become the prefix.
+
+\newcount\prefixcounter
+
+%D These settings are accomplished by:
+%D
+%D \showsetup{setupreferencing}
+%D
+%D In interactive documents verbose references don't always
+%D make sense (what is a page number in an unnumbered
+%D document). By setting the \type{interaction} variable, one
+%D can influences the way interactive references are set.
+
+\chardef\autocrossfilereferences=0
+
+\def\setupreferencing
+ {\dosingleargument\dosetupreferencing}
+
+\def\dosetupreferencing[#1]%
+ {\getparameters
+ [\??rf]
+ [\c!prefix=\s!unknown,#1]%
+ \processaction
+ [\@@rfstate]
+ [ \v!stop=>\referencingfalse,
+ \v!start=>\referencingtrue]%
+ \processaction
+ [\@@rfinteraction]
+ [ \v!all=>\let\dowantedreference\docompletereference,
+ \v!label=>\let\dowantedreference\dolabelonlyreference,
+ \v!text=>\let\dowantedreference\dotextonlyreference,
+ \v!symbol=>\let\dowantedreference\dosymbolreference]%
+ \chardef\autocrossfilereferences\zerocount
+ \processaction
+ [\@@rfautofile]
+ [ \v!yes=>\chardef\autocrossfilereferences\plusone,
+ \v!page=>\chardef\autocrossfilereferences\plustwo]%
+ \chardef\referencefilecase\zerocount
+ \processaction[\@@rfconvertfile]
+ [ \v!yes=>\chardef\referencefilecase\plusone,
+ \v!big=>\chardef\referencefilecase\plusone,
+ \v!small=>\chardef\referencefilecase\plustwo]%
+ \setupreferenceprefix[\@@rfprefix]%
+ \doifelse\@@rfglobal\v!yes
+ {\settrue \autoglobalfilereferences}
+ {\setfalse\autoglobalfilereferences}}
+
+\def\incrementreferenceprefix{+}
+\def\decrementreferenceprefix{-}
+
+\def\setupreferenceprefix[#1]%
+ {\edef\@@rfprefix{#1}%
+ \ifx\@@rfprefix\empty
+ \let\referenceprefix\empty
+ \else\ifx\@@rfprefix\incrementreferenceprefix
+ \advance\prefixcounter \plusone % should be global
+ \edef\referenceprefix{\the\prefixcounter:}%
+ \let\@@rfprefix\s!unknown
+ \else\ifx\@@rfprefix\decrementreferenceprefix
+ \let\referenceprefix\empty
+ \let\@@rfprefix\s!unknown
+ \else\ifx\@@rfprefix\s!unknown
+ % forget about it
+ \else
+ \edef\referenceprefix{\@@rfprefix:}%
+ \fi\fi\fi\fi}
+
+%D \macros
+%D {handlereferenceactions,
+%D collectreferenceactions}
+%D
+%D Sometimes we need to pass the actions connected to
+%D references to variables instead of rectangular areas on
+%D which one can click. The next macro collects the actions
+%D and passes them to a handle. This is a rather dreadfull
+%D hack!
+%D
+%D \starttyping
+%D \handlereferenceactions{references}\handle
+%D \stoptyping
+%D
+%D So, \type {\handle} does the final job, which in for
+%D instance the \PDF\ drivers comes down to doing something
+%D with \type {\lastPDFaction}.
+
+\newif\ifcollectreferenceactions
+
+\def\handlereferenceactions#1#2%
+ {\doifsomething{#1}
+ {\bgroup
+ \collectreferenceactionstrue
+ \doprocessreferenceelse{#1}{#2}{\unknownreference{#1}}%
+ \egroup}}
+
+%D The most straightforward way of retrieving references is
+%D using \type{\ref}. Consider the reference:
+%D
+%D \startbuffer
+%D \reference[my ref]{{Look}{Here}{I am}}
+%D \stopbuffer
+%D
+%D \typebuffer
+%D
+%D \getbuffer
+%D
+%D We can ask for upto five reference components:
+%D
+%D \startbuffer
+%D user page reference: \ref[p][my ref]
+%D text reference: \ref[t][my ref]
+%D real page reference: \ref[r][my ref]
+%D sub text reference: \ref[s][my ref]
+%D extra text reference: \ref[e][my ref]
+%D \stopbuffer
+%D
+%D \typebuffer
+%D
+%D And get back:
+%D
+%D \startlines
+%D \getbuffer
+%D \stoplines
+
+\def\ref{\dodoubleargument\doref}
+
+\def\reftypep{\currentpagereference}
+\def\reftypet{\currenttextreference}
+\def\reftyper{\currentrealreference}
+\def\reftypes{\currentsubtextreference}
+\def\reftypee{\currentsubsubtextreference}
+
+\def\doref[#1][#2]%
+ {\ifsecondargument
+% \doifreferencefoundelse{#2}
+% {\executeifdefined{reftype#1}\reftypep}
+% {\unknownreference{#2}\dummyreference}%
+ \else
+ \dummyreference
+ \fi}
+
+%D We can typeset a reference using \type{\in}, \type{\at} and
+%D \type{\about} and goto specific locations using
+%D \type{\goto}. The last one does not make that much sense in
+%D a paper document. To complicate things, \PLAIN\ \TEX\ also
+%D implements an \type {\in} but fortunately that one only
+%D makes sense in math mode.
+%D
+%D Typesetting the reference is a bit more complicated than one
+%D would at first sight expect. This is due to the fact that we
+%D distinguish three (five) alternative calls:
+%D
+%D \placefigure
+%D [here][three calls]
+%D {Three alternatives reference calls.}
+%D {\startcombination[1*3]
+%D {\framed{\type{ \in }}} {a}
+%D {\framed{\type{ \at }}} {b}
+%D {\framed{\type{\goto}}} {c}
+%D \stopcombination}
+%D
+%D \startbuffer
+%D \in figure[fig:three calls]
+%D \in{figure}[fig:three calls]
+%D \in figure a[fig:three calls]
+%D \in{figure}{a}[fig:three calls]
+%D figure~\in[fig:three calls]
+%D \stopbuffer
+%D
+%D \typebuffer
+%D
+%D This turns up as:
+%D
+%D \startlines
+%D \getbuffer
+%D \stoplines
+%D
+%D The dual \type{{}} results in a split reference. In a
+%D document meant for paper, one is tempted to use the last
+%D (most straightforward) alternative. When a document is also
+%D meant voor electronic distribution, the former alternatives
+%D have preference, because everything between the \type{\in}
+%D and~\type{[} becomes active (and when asked for, typeset
+%D in a different color and typeface).
+
+\definecommand in {\dospecialin}
+\definecommand at {\dospecialat}
+\definecommand about {\dospecialabout}
+\definecommand from {\dospecialfrom}
+\definecommand over {\dospecialabout} % needed here, else math problems
+
+\def\currentreferencenumber{\ctxlua{jobreferences.filter("number")}}
+\def\currentreferencepage {\ctxlua{jobreferences.filter("page")}}
+\def\currentreferencetitle {\ctxlua{jobreferences.filter("title")}}
+
+\unexpanded\def\dospecialin{\doinatreference\currentreferencenumber}
+\unexpanded\def\dospecialat{\doinatreference\currentreferencepage}
+
+\def\doinatreference#1%
+ {\doifnextoptionalelse{\dodoinatreference{#1}{}}{\dodoinatreference{#1}}}
+
+\def\dodoinatreference#1%
+ {\def\dododoinatreference{\dodododoinatreference{#1}}%
+ \futurelet\next\dododoinatreference}
+
+\unexpanded\def\dospecialabout[#1]%
+ {\dontleavehmode
+ \bgroup
+ \@@rfleft
+ \doprocessreferenceelse{#1}
+ {\let\crlf\space
+ \let\\\space
+ \let\dogotofixed\dogotospace
+ \dogotospace{\limitatetext\currentreferencetitle\@@rfwidth\unknown}[#1]}
+ {\unknownreference{#1}\dummyreference}%
+ \@@rfright
+ \referenceinfo{<}{#1}%
+ \egroup}
+
+%D We arrived at the last step. Before we do the typesetting,
+%D we forget all previous (paragraph bound) settings and make
+%D sure that we remain in horizontal mode. Next we choose
+%D among the several representations.
+
+%D The previously discussed setup macro lets us specify the
+%D representation of references. A symbol reference does not
+%D show the specific data, like the number of a figure, but
+%D shows one of: \hbox {$^\goforwardcharacter$
+%D $^\gobackwardcharacter$ $^\gonowherecharacter$}, depending
+%D on the direction to go.
+
+ \def\dosymbolreference#1#2[#3]% todo
+ {\bgroup
+ \setupsymbolset[\@@iasymbolset]%
+ \removelastskip
+ \ifx\currentreferencespecial\empty
+ \ifx\currentouterreference\empty
+ \ifnum0\currentrealreference=\zerocount
+ \ifhmode\strut\high{\symbol[\v!nowhere]}\fi
+ \else\ifnum0\currentrealreference>\realpageno
+ \dodosymbolreference{#2}{\high{\symbol[\v!next]}}%
+ \else\ifnum0\currentrealreference<\realpageno
+ \dodosymbolreference{#2}{\high{\symbol[\v!previous]}}%
+ \else
+ \ifhmode\strut\high{\symbol[\v!nowhere]}\fi
+ \fi\fi\fi
+ \else
+ \gotoouterlocation{#3}{\showlocation{\high{\symbol[\v!somewhere]}}}%
+ \fi
+ \else
+ \gotospeciallocation{#3}{\showlocation{\high{\symbol[\v!somewhere]}}}%
+ \fi
+ \egroup}
+
+ \def\dodosymbolreference#1#2% todo
+ {#1\hbox{\gotorealpage\empty\empty\currentrealreference{\dolocationattributes\??ia\c!style\c!color{#2}}}}
+
+%D The other alternatives just conform their names: only the
+%D label, only the text, or the label and the text.
+
+\def\dounknownreference#1#2[#3]%
+ {\unknownreference{#3}\dotextprefix{#2}\dummyreference}%
+
+\def\docompletereference#1#2[#3]%
+ {\iflocationsplit
+ \doifsomespaceelse{#2}\dogotospace\dogotofixed{\dotextprefix{#2}#1}[#3]%
+ \else
+ \dogotofixed{\dotextprefix{#2}#1}[#3]%
+ \fi}
+
+\def\dolabelonlyreference#1#2[#3]%
+ {\doifsomespaceelse{#2}
+ {\doifsomething{#2}{\dogotospace{#2}[#3]}}
+ {\dogotofixed{\dotextprefix{#2}}[#3]}}
+
+\def\dotextonlyreference#1#2[#3]%
+ {\dotextprefix{#2}\dogotofixed{#1}[#3]}
+
+\let\dowantedreference\docompletereference
+
+%D \macros
+%D {definereferenceformat}
+%D
+%D The next few macros were made for for David Arnold and Taco
+%D Hoekwater. They can be used for predefining reference
+%D texts, and thereby stimulate efficiency.
+%D
+%D [more documentation will be added]
+%D
+%D \starttyping
+%D \definereferenceformat[informula] [left=(,right=),text=formula]
+%D \definereferenceformat[informulas] [left=(,right=),text=formulas]
+%D \definereferenceformat[andformula] [left=(,right=),text=and]
+%D \definereferenceformat[andformulas][left=(,right=),text=and]
+%D
+%D \informula [b] and \informula [for:c]
+%D the \informula {formulas}[b] \informula {and} [for:c]
+%D the \informulas {formulas}[b] \informula {and} [for:c]
+%D the \informulas [b] \informula {en} [for:c]
+%D the \informulas [b] \andformula [for:c]
+%D \stoptyping
+%D
+%D Instead of a text, one can specify a label, which should
+%D be defined with \type {\setuplabeltext}.
+
+% todo: inherit
+
+\def\definereferenceformat
+ {\dodoubleargument\dodefinereferenceformat}
+
+\def\dodefinereferenceformat[#1][#2]%
+ {\iffirstargument
+ \getparameters[\??rf#1]
+ [\c!left=, % of the number
+ \c!right=, % of the number
+ \c!text=, % before the number
+ \c!label=, % can be {left}{right}
+ \c!command=\in,
+ #2]%
+ \unexpanded\setvalue{#1}%
+ {\dontleavehmode\doexecutereferenceformat{#1}}%
+ \fi}
+
+\def\noexecutelabelreferenceformat#1%
+ {\doifvaluesomething{\??rf#1\c!text}
+ {\gdef\textofreference{\csname\??rf#1\c!text\endcsname}}%
+ \csname\??rf#1\c!command\endcsname}
+
+\def\doexecutelabelreferenceformat#1%
+ {\csname\??rf#1\c!command\endcsname
+ {\leftlabeltext {\csname\??rf#1\c!label\endcsname}}%
+ {\rightlabeltext{\csname\??rf#1\c!label\endcsname}}}
+
+\def\doexecutereferenceformat#1%
+ {\gdef\leftofreference {\csname\??rf#1\c!left \endcsname}%
+ \gdef\rightofreference{\csname\??rf#1\c!right\endcsname}%
+ \global\let\textofreference\empty % otherwise ~ added
+ \doifelsevaluenothing{\??rf#1\c!label}
+ \noexecutelabelreferenceformat\doexecutelabelreferenceformat{#1}}
+
+\let\leftofreference \relax
+\let\rightofreference\relax
+\let\textofreference \relax
+
+% fails on metafun {\leftofreference#1\ignorespaces#3\removeunwantedspaces\rightofreference}{#2}[#4]%
+
+\def\dodododoinatreference#1#2#3[#4]% no \removeunwantedspaces (fails on metafun)
+ {\ifx\next\bgroup
+ \dododododoinatreference{\leftofreference#1\ignorespaces#3\rightofreference}{#2}[#4]%
+ \else
+ \dododododoinatreference{\leftofreference#1\rightofreference}{#2#3}[#4]%
+ \fi}
+
+\let\dosymbolreference\dowantedreference
+
+\def\dododododoinatreference#1#2[#3]%
+ {\dontleavehmode % replaces \leaveoutervmode
+ \begingroup
+ \forgetall
+ \postponenotes
+ \doifreferencefoundelse{#3}
+ {\doifelsenothing{#1}\dosymbolreference\dowantedreference{#1}{#2}[#3]}%
+ {\dounknownreference{#1}{#2}[#3]}%
+ \referenceinfo<{#3}%
+ \endgroup}
+
+
+%D In interactive documents going to a specific location is not
+%D bound to cross references. The \type{\goto} commands can be
+%D used to let users access another part of the document. In
+%D this respect, interactive tables of contents and registers
+%D can be considered goto's. Because in fact a \type{\goto} is
+%D just a reference without reference specific data, the
+%D previous macros are implemented using the goto
+%D functionality.
+%D
+%D \showsetup{goto}
+%D
+%D One important chaacteristic is that the first argument of
+%D \type{\goto} (and therefore \type{\at} and \type{\in} is
+%D split at spaces. This means that, although hyphenation is
+%D prevented, long references can cross line endings.
+
+\newif\ifsharesimilarreferences \sharesimilarreferencestrue
+\newcount\similarreference % 0=noppes 1=create/refer 2,3,..=refer
+
+\unexpanded\def\goto#1#2%
+ {\dogoto{#1}#2}
+
+\def\dogoto#1[#2]%
+ {\dontleavehmode
+ \bgroup
+ \postponenotes
+ % todo: handle empty #1
+ \doifelsenothing{#1}
+ {\dosymbolreference{}{}[#2]}
+ {\dogotospace{#1}[#2]}%
+ \egroup
+ \referenceinfo{<}{#2}}
+
+% inefficient, we need to save the shared one (just reuse last command in lua)
+
+\def\dogotospace#1[#2]%
+ {\iflocationsplit
+ \ifsecondaryreference
+ \setbox\scratchbox\hbox % will change anyway
+ \fi % due to space insertion
+ {\let\dogotospace\dogotofixed
+ \iflocation
+ \def\processisolatedword##1%
+ {\ifisolatedwords\ifsharesimilarreferences
+ \global\advance\similarreference \plusone
+ \fi\fi
+ \hbox\bgroup
+ \doprocessreferenceelse{#2}{##1\presetgoto}{\unknownreference{#2}##1\relax}%
+ \egroup}%
+ \doattributes\??ia\c!style\c!color{\processisolatedwords{#1}\processisolatedword}%
+ \else
+ #1\relax % \relax prevents #1's next macros from gobbling \fi
+ \fi}%
+ \else
+ \iflocation
+ \hbox{\doattributes\??ia\c!style\c!color{\doprocessreferenceelse{#2}{#1\presetgoto}{\unknownreference{#2}#1\relax}}}%
+ \else
+ #1\relax % \relax prevents #1's next macros from gobbling \fi
+ \fi
+ \fi
+ \global\similarreference\zerocount}
+
+\def\dogotofixed#1[#2]%
+ {{\iflocation
+ \hbox{\doattributes\??ia\c!style\c!color{\doprocessreferenceelse{#2}{#1\presetgoto}{\unknownreference{#2}#1\relax}}}%
+ \else
+ #1%
+ \fi}}
+
+%D In case the auto split feature is not needed or even not
+%D even wanted, \type{\gotobox} can be used.
+
+\unexpanded\def\gotobox#1[#2]%
+ {\dontleavehmode
+ \bgroup
+ \locationstrutfalse
+ \doprocessreferenceelse{#2}
+ {\dogotofixed{#1}[#2]}
+ {\hbox{\unknownreference{#2}#1}}%
+ \referenceinfo{<}{#2}%
+ \egroup}
+
+%D An reference to another document can be specified as a file
+%D or as an \URL. Both are handled by the same mechanism and
+%D can be issued by saying something like:
+%D
+%D \starttyping
+%D \goto[dictionary::the letter a]
+%D \stoptyping
+%D
+%D One can imagine that many references to such a dictionary
+%D are made, so in most cases such a document reference in an
+%D indirect one.
+%D
+%D \showsetup{useexternaldocument}
+%D
+%D For example:
+%D
+%D \starttyping
+%D \useexternaldocument
+%D [dictionary][engldict]
+%D [The Famous English Dictionary]
+%D \stoptyping
+%D
+%D The next macro implements these relations, and also take
+%D care of loading the document specific references.
+%D
+%D The \URL\ alternative takes four arguments:
+%D
+%D \showsetup{useURL}
+%D
+%D like:
+%D
+%D \starttyping
+%D \useURL
+%D [dictionary][http://www.publisher.com/public][engldict]
+%D [The Famous English Dictionary]
+%D \stoptyping
+%D
+%D Several specifications are possible:
+%D
+%D \starttyping
+%D \useURL [id] [url] [file] [description]
+%D \useURL [id] [url] [file]
+%D \useURL [id] [url]
+%D \stoptyping
+%D
+%D This time we don't load the references when no file is
+%D specified. This is logical when one keeps in mind that a
+%D valid \URL\ can also be a mail address.
+
+\def\usefile{\dotripleargument\dousefile}
+\def\useurl {\doquadrupleempty\douseurl}
+
+\let\useURL \useurl
+\let\useexternaldocument\usefile
+
+\def\douseurl[#1][#2][#3][#4]%
+ {\ctxlua{jobreferences.urls.define("#1",\!!bs\detokenize{#2}\!!es,\!!bs\detokenize{#3}\!!es,\!!bs\detokenize{#4}\!!es)}}
+
+\def\dousefile[#1][#2][#3]%
+ {\ctxlua{jobreferences.files.define("#1",\!!bs\detokenize{#2}\!!es,\!!bs\detokenize{#3}\!!es)}}
+
+% \doifsomething\@@urstyle{\let\@@iastyle\@@urstyle\let\@@urstyle\empty}%
+% \doifsomething\@@urcolor{\let\@@iacolor\@@urcolor\let\@@urcolor\empty}%
+
+%D \macros
+%D {url,setupurl}
+%D
+%D We also have: \type{\url} for directly calling the
+%D description. So we can say:
+%D
+%D \starttyping
+%D \useURL [one] [http://www.test.nl]
+%D \useURL [two] [http://www.test.nl] [] [Some Site]
+%D
+%D \url[one] or \from[two] or \goto{Whatever Site}[URL(two)]
+%D \stoptyping
+%D
+%D An \URL\ can be set up with
+%D
+%D \showsetup{setupurl}
+
+\def\setupurl
+ {\dodoubleargument\getparameters[\??ur]}
+
+\unexpanded\def\url[#1]%
+ {\dontleavehmode
+ \begingroup
+ \dosetfontattribute\??ur\c!style
+ \dosetcolorattribute\??ur\c!color
+ \ctxlua{jobreferences.urls.get("#1","\@@uralternative","\@@urspace")}%
+ \dostopattributes
+ \endgroup}
+
+%D This macro is hooked into a support macro, and thereby
+%D \URL's break ok, according to the setting of a switch,
+%D
+%D \startbuffer
+%D \useURL
+%D [test]
+%D [sentence_sentence%sentence#sentence~sentence/sentence//sentence:sentence.sentence]
+%D \stopbuffer
+%D
+%D \typebuffer
+%D
+%D Such an \URL\ is, depending on the settings, hyphenated as:
+%D
+%D \getbuffer
+%D
+%D \startlinecorrection
+%D \hbox to \hsize
+%D {\hss\en
+%D \setupreferencing[urlalternative=both]%
+%D \vbox{\hsize.25cm\hbox{\bf both}\prewordbreak\url[test]}%
+%D \hss
+%D \setupreferencing[urlalternative=before]%
+%D \vbox{\hsize.25cm\hbox{\bf before}\prewordbreak\url[test]}%
+%D \hss
+%D \setupreferencing[urlalternative=after]%
+%D \vbox{\hsize.25cm\hbox{\bf after}\prewordbreak\url[test]}%
+%D \hss}
+%D \stoplinecorrection
+%D
+%D By setting \type{urlspace=yes} one can get slightly better
+%D spacing when using very long \URL's.
+%D
+%D When defining the external source of information, one can
+%D also specify a suitable name (the last argument). This name
+%D can be called upon with:
+%D
+%D \showsetup{from}
+
+\def\dospecialfrom
+ {\dosingleempty\dodospecialfrom}
+
+\def\dodospecialfrom[#1]%
+ {\dontleavehmode\ctxlua{jobreferences.from("#1","","")}}
+
+%D We also support:
+%D
+%D \starttyping
+%D \goto{some text}[file(identifier{location}]
+%D \stoptyping
+%D
+%D which is completely equivalent with
+%D
+%D \starttyping
+%D \goto{some text}[identifier::location]
+%D \stoptyping
+
+\def\gotofilespecial#1#2#3#4% special operation arguments data
+ {\begingroup\iflocation\gotoouterfile{#2}{#3}{#4}\else#4\fi\endgroup}
+
+\def\gotourlspecial#1#2#3#4% special operation arguments data
+ {\begingroup\iflocation\gotoouterurl{#2}{#3}{#4}\else#4\fi\endgroup}
+
+%D A special case of references are those to programs. These,
+%D very system dependant references are implemented by abusing
+%D some of the previous macros.
+%D
+%D \showsetup{setupprograms}
+%D \showsetup{defineprogram}
+%D \showsetup{program} % changed functionality !
+%D
+%D The latter gives access to the description of the program,
+%D being the last argument to the definition command.
+
+% also lua, like urls and files
+
+\def\setupprograms
+ {\dodoubleargument\getparameters[\??pr]}
+
+\def\defineprogram
+ {\dotripleargument\dodefineprogram}
+
+\def\dodefineprogram[#1][#2][#3]%
+ {\ctxlua{jobreferences.programs.define("#1","#2","#3")}}
+
+\def\program[#1]% incompatible, more consistent, hardy used anyway
+ {\dontleavehmode
+ \begingroup
+ \dosetfontattribute\??pr\c!style
+ \dosetcolorattribute\??pr\c!color
+ \ctxlua{jobreferences.programs.get("#1","\@@pralternative","\@@prspace")}%
+ \endgroup}
+
+% needs an update: program(abc{arg})
+
+\def\gotoprogramspecial#1#2#3#4% special operation arguments data
+ {\begingroup
+ \iflocation
+ \dohandlegoto
+ {#4}%
+ {\dostartrunprogram\buttonwidth\buttonheight{\@@prdirectory#2}{#3}}%
+ {\dostoprunprogram}%
+ \else
+ #4%
+ \fi
+ \endgroup}
+
+%D As we can see, we directly use the special reference
+%D mechanism, which means that
+%D
+%D \starttyping
+%D \goto{some text}[program(name{args})]
+%D \stoptyping
+%D
+%D is valid.
+
+%D The next macro provides access to the actual pagenumbers.
+%D When documenting and sanitizing the original reference
+%D macros, I decided to keep the present meaning as well as to
+%D make this meaning available as a special reference method.
+%D So now one can use:
+%D
+%D \starttyping
+%D \gotopage{some text}[location]
+%D \gotopage{some text}[number]
+%D \gotopage{some text}[file::number]
+%D \stoptyping
+%D
+%D as well as:
+%D
+%D \starttyping
+%D \goto{some text}[page(location)]
+%D \goto{some text}[page(number)]
+%D \goto{some text}[file::page(number)]
+%D \stoptyping
+%D
+%D Here location is a keyword like \type{nextpage}.
+%D
+%D \showsetup{gotopage}
+
+\def\definepage
+ {\dodoubleargument\dodefinepage}
+
+\def\dodefinepage[#1][#2]%
+ {\definereference[#1][page(#1)]}
+
+\def\gotopage#1[#2]%
+ {\goto{#1}[\v!page(#2)]}
+
+%D The previous definitions are somewhat obsolete so we don't
+%D use it here.
+
+%D A still very rudimentary|/|experimental forward|/|backward
+%D reference mechanism is provided by the macro \type{\atpage}:
+%D
+%D \starttyping
+%D ... \somewhere{backward text}{forward text}[someref] ...
+%D ... \atpage[someref] ...
+%D \stoptyping
+%D
+%D In future versions there will be more sophisticated
+
+%D support, also suitable for references to floating bodies.
+
+\def\analysedreference#1%
+ {\ctxlua{jobreferences.analysis("\referenceprefix","#1")}}
+
+\unexpanded\def\somewhere#1#2#3[#4]% #3 gobbles space around #2 % todo
+ {\dontleavehmode
+ \ifcase\analysedreference{#4}\relax
+ \unknownreference{#4}#1/#2%
+ \or
+ \doifelsenothing{#2}{\dosymbolreference{}{}[#4]}{\dogotospace{#2}[#4]}%
+ \or % forward
+ \doifelsenothing{#1}{\dosymbolreference{}{}[#4]}{\dogotospace{#1}[#4]}%
+ \or % backward
+ \doifelsenothing{#2}{\dosymbolreference{}{}[#4]}{\dogotospace{#2}[#4]}%
+ \fi
+ \referenceinfo{<}{#4}}
+
+\unexpanded\def\atpage[#1]% todo
+ {\dontleavehmode
+% \docheckrealreferencepage{}%
+% \doifreferencefoundelse{#1}
+% {\ifrealreferencepage
+% \ifforwardreference
+% \dogotofixed{\labeltext\v!hencefore}[#1]%
+% \else
+% \dogotofixed{\labeltext\v!hereafter}[#1]%
+% \fi
+% \else
+% \dogotofixed{\labeltexts\v!atpage\currentpagereference}[#1]%
+% \fi}
+% {\unknownreference{#1}%
+% \labeltexts\v!page\dummyreference}%
+ \referenceinfo{<}{#1}}
+
+%D We can cross link documents by using:
+%D
+%D \showsetup{coupledocument}
+%D
+%D like:
+%D
+%D \starttyping
+%D \coupledocument[print][somefile][chapter,section]
+%D \stoptyping
+%D
+%D After which when applicable, we have available the
+%D references:
+%D
+%D \starttyping
+%D \goto{print version}[print::chapter]
+%D \stoptyping
+%D
+%D and alike. The title placement definition macros have a
+%D key \type{file}, which is interpreted as the file to jump
+%D to, that is, when one clicks on the title.
+
+\newif\ifautocrossdocument
+
+\def\coupledocument
+ {\doquadrupleempty\docoupledocument}
+
+\def\docoupledocument[#1][#2][#3][#4]% [name] [file] [sections] [description]
+ {\ifthirdargument
+ % this will be done differently (when it's needed)
+ \fi}
+
+%D Buttons are just what their names says: things that can be
+%D clicked (pushed) on. They are similar to \type{\goto},
+%D except that the text argument is not interpreted.
+%D Furthermore one can apply anything to them that can be done
+%D with \type{\framed}.
+%D
+%D \startbuffer
+%D \button[width=3cm,height=1.5cm]{Exit}[ExitViewer]
+%D \stopbuffer
+%D
+%D \typebuffer
+%D
+%D gives
+%D
+%D \getbuffer
+%D
+%D This command is formally specified as:
+%D
+%D \showsetup{button}
+%D
+%D The characteristics can be set with:
+%D
+%D \showsetup{setupbuttons}
+
+\def\setupbuttons
+ {\dodoubleargument\getparameters[\??bt]}
+
+\definecomplexorsimpleempty\button
+
+\def\complexbutton
+ {\docomplexbutton\??bt}
+
+\presetlocalframed[\??bt]
+
+\long\def\docomplexbutton#1[#2]#3#4% get rid of possible space before [#4]
+ {\dodocomplexbutton#1[#2]{#3}#4} % #4 == [
+
+\def\buttonframed{\dodoubleempty\localframed[\??bt]} % goodie
+
+\long\def\dodocomplexbutton#1[#2]#3[#4]% #3 can contain [] -> {#3} later
+ {\begingroup
+ \doifvalue{#1\c!state}\v!stop\locationfalse
+ \iflocation
+ \resetgoto
+ \ConvertConstantAfter\doifelse{#3}\v!none\hphantom\hbox
+ {\doifelsenothing{#4}
+ {\setlocationboxnop#1[#2]{#3}[#4]}
+ {\doifreferencefoundelse{#4} % INEFFICIENT
+ {\setlocationboxyes#1[#2]{#3}[#4]}
+ {\unknownreference{#4}%
+ \setlocationboxnop#1[#2]{#3}[#4]}}}%
+ \fi
+ \endgroup}
+
+%D Interaction buttons, in fact a row of tiny buttons, are
+%D typically only used for navigational purposed. The next
+%D macro builds such a row based on a specification list.
+%D
+%D \startbuffer
+%D \interactionbuttons
+%D [width=\hsize][page,PreviousJump,ExitViewer]
+%D \stopbuffer
+%D
+%D \typebuffer
+%D
+%D gives
+%D
+%D \getbuffer
+%D
+%D Apart from individual entries, one can use \type{page} and
+%D \type {subpage} as shortcuts to their four associated buttons.
+%D The symbols are derived from the symbols linked to the
+%D entries.
+
+% does not work well with for instance SomeRef{whatever}
+
+\def\interactionbuttons
+ {\dodoubleempty\dointeractionbuttons}
+
+\def\dointeractionbuttons[#1][#2]% er is een verdeel macro \horizontalfractions
+ {\iflocation
+ % BUG: fails when frame=off; best is to rewrite this macro
+ \bgroup
+ \doif\@@ibstate\v!stop\locationfalse
+ \iflocation
+ \ifsecondargument
+ \setupinteractionbar[#1]%
+ \checkinteractionbar{1.5em}\v!broad\!!zeropoint % brrrrr
+ \setbox2\hbox{\localframed[\??ib][\c!background=]{\symbol[\@@iasymbolset][\v!previouspage]}}%
+ \!!heighta\ht2 % needed because we default to nothing
+ \setupinteractionbar[\c!strut=\v!no]%
+ \setinteractionparameter\c!width\!!zeropoint
+ \!!counta\zerocount % new, was 1
+ \processallactionsinset
+ [#2]
+ [ \v!page=>\advance\!!counta 4,
+ \v!subpage=>\advance\!!counta 4,
+ \s!unknown=>\advance\!!counta 1]%
+ \ifdim\@@ibwidth=\zeropoint
+ \!!widtha2em
+ \advance\!!widtha \@@ibdistance % new
+ \!!widthb\!!counta\!!widtha
+ \advance\!!widthb -\@@ibdistance % new
+ \else
+ \!!widtha\@@ibwidth
+ \!!widthb\@@ibdistance % new
+ \multiply\!!widthb \!!counta % new
+ \advance\!!widthb -\@@ibdistance % new
+ \advance\!!widtha -\!!widthb % new
+ \divide\!!widtha \!!counta
+ \!!widthb\@@ibwidth
+ \fi
+ \def\goto##1% clash ?
+ {\setnostrut
+ \edef\localreference{##1}%
+ \normalexpanded{\noexpand\dodocomplexbutton\??ib[\c!height=\the\!!heighta,\c!width=\the\!!widtha]}%
+ {\dontleavehmode\symbol[\@@iasymbolset][\localreference]}%
+ [\localreference]%
+ \hss}%
+ \hbox to \!!widthb
+ {\processallactionsinset
+ [#2]
+ [ \v!page=>\goto\v!firstpage
+ \goto\v!nextpage
+ \goto\v!previouspage
+ \goto\v!lastpage,
+ \v!subpage=>\goto\v!firstsubpage
+ \goto\v!nextsubpage
+ \goto\v!previoussubpage
+ \goto\v!lastsubpage,
+ \s!unknown=>\goto\commalistelement]%
+ \unskip}%
+ \else
+ \interactionbuttons[][#1]%
+ \fi
+ \fi
+ \egroup
+ \fi}
+
+%D \macros
+%D {overlaybutton}
+%D
+%D For converience we provide:
+%D
+%D \starttyping
+%D \overlaybutton[reference]
+%D \stoptyping
+%D
+%D This command can be used to define overlays an/or can be
+%D used in the whatevertext areas, like:
+%D
+%D \starttyping
+%D \defineoverlay[PrevPage][\overlaybutton{PrevPage}]
+%D \setupbackgrounds[page][background=PrevPage]
+%D \setuptexttexts[\overlaybutton{NextPage}]
+%D \stoptyping
+%D
+%D For practical reasons, this macro accepts square brackets
+%D as well as braces.
+
+\definecomplexorsimple\overlaybutton
+
+\def\simpleoverlaybutton#1%
+ {\complexoverlaybutton[#1]}
+
+\def\complexoverlaybutton[#1]%
+ {\iflocation
+ \doprocessreferenceelse{#1}
+ {\overlayfakebox {#1}}
+ {\unknownreference{#1}}%
+ \fi}
+
+\def\overlayfakebox#1%
+ {\hbox
+ {\setbox\scratchbox\null
+ \wd\scratchbox\overlaywidth
+ \ht\scratchbox\overlayheight
+ \locationstrutfalse
+ \box\scratchbox}}
+
+%D \macros
+%D {dotextprefix}
+%D
+%D In previous macros we used \type {\dotextprefix} to
+%D generate a space between a label and a number.
+%D
+%D \starttyping
+%D \dotextprefix{text}
+%D \stoptyping
+%D
+%D Only when \type {text} is not empty, a space is inserted.
+
+\def\dotextprefix#1%
+ {\begingroup
+ \global\labeltextdonefalse % this is an ugly dependancy,
+ \setbox\scratchbox\hbox{#1}% to be solved some day
+ \ifdim\wd\scratchbox>\zeropoint
+ \unhbox\scratchbox
+ \iflabeltextdone\else\@@rfseparator\fi
+ \else
+ \unhbox\scratchbox
+ \fi
+ \endgroup}
+
+%D In the next settings we see some variables that were not
+%D used here and that concern the way the pagenumbers refered
+%D to are typeset.
+
+\setupreferencing
+ [\c!state=\v!start,
+ \c!autofile=\v!no,
+ \v!part\c!number=\v!yes,
+ \v!chapter\c!number=\v!no,
+ \c!interaction=\v!all,
+ \c!convertfile=\v!no,
+ %\c!strut=\v!no, % some day an option
+ \c!prefix=,
+ \c!width=.75\makeupwidth,
+ \c!left=\quotation\bgroup,
+ \c!right=\egroup,
+ \c!global=\v!no,
+ \c!expansion=\v!no,
+ \c!separator=\nonbreakablespace]
+
+\setupurl
+ [\c!alternative=\v!both,
+ \c!space=\v!no,
+ \c!style=\v!type,
+ \c!color=]
+
+\setupprograms
+ [\c!directory=,
+ \c!alternative=\v!both,
+ \c!space=\v!no,
+ \c!style=\v!type,
+ \c!color=]
+
+\definereference [\v!CloseDocument ] [action(close)]
+\definereference [\v!ExitViewer ] [action(exit)]
+\definereference [\v!FirstPage ] [action(first)]
+\definereference [\v!LastPage ] [action(last)]
+\definereference [\v!NextJump ] [action(forward)]
+\definereference [\v!NextPage ] [action(next)]
+\definereference [\v!PauseMovie ] [action(pausemovie)]
+\definereference [\v!PauseSound ] [action(pausesound)]
+\definereference [\v!PauseRendering ] [action(pauserendering)]
+\definereference [\v!PreviousJump ] [action(backward)]
+\definereference [\v!PreviousPage ] [action(previous)]
+\definereference [\v!PrintDocument ] [action(print)]
+\definereference [\v!SaveForm ] [action(exportform)]
+\definereference [\v!LoadForm ] [action(importform)]
+\definereference [\v!ResetForm ] [action(resetform)]
+\definereference [\v!ResumeMovie ] [action(resumemovie)]
+\definereference [\v!ResumeSound ] [action(resumesound)]
+\definereference [\v!ResumeRendering ] [action(resumerendering)]
+\definereference [\v!SaveDocument ] [action(save)]
+\definereference [\v!SaveNamedDocument] [action(savenamed)]
+\definereference [\v!OpenNamedDocument] [action(opennamed)]
+\definereference [\v!SearchDocument ] [action(search)]
+\definereference [\v!SearchAgain ] [action(searchagain)]
+\definereference [\v!StartMovie ] [action(startmovie)]
+\definereference [\v!StartSound ] [action(startsound)]
+\definereference [\v!StartRendering ] [action(startrendering)]
+\definereference [\v!StopMovie ] [action(stopmovie)]
+\definereference [\v!StopSound ] [action(stopsound)]
+\definereference [\v!StopRendering ] [action(stoprendering)]
+\definereference [\v!SubmitForm ] [action(submitform)]
+\definereference [\v!ToggleViewer ] [action(toggle)]
+\definereference [\v!ViewerHelp ] [action(help)]
+\definereference [\v!HideField ] [action(hide)]
+\definereference [\v!ShowField ] [action(show)]
+\definereference [\v!GotoPage ] [action(gotopage)]
+\definereference [\v!GotoPage ] [action(gotopage)]
+\definereference [\v!Query ] [action(query)]
+\definereference [\v!QueryAgain ] [action(queryagain)]
+\definereference [\v!FitWidth ] [action(fitwidth)]
+\definereference [\v!FitHeight ] [action(fitheight)]
+\definereference [\v!ShowThumbs ] [action(thumbnails)]
+\definereference [\v!ShowBookmarks ] [action(bookmarks)]
+
+\definereference [\v!firstpage] [page(\firstpage)]
+\definereference [\v!previouspage] [page(\prevpage)]
+\definereference [\v!nextpage] [page(\nextpage)]
+\definereference [\v!lastpage] [page(\lastpage)]
+\definereference [\v!firstsubpage] [page(\firstsubpage)]
+\definereference [\v!previoussubpage] [page(\prevsubpage)]
+\definereference [\v!nextsubpage] [page(\nextsubpage)]
+\definereference [\v!lastsubpage] [page(\lastsubpage)]
+\definereference [\v!first] [page(\firstpage)]
+\definereference [\v!previous] [page(\prevpage)]
+\definereference [\v!next] [page(\nextpage)]
+\definereference [\v!last] [page(\lastpage)]
+\definereference [\v!first\v!sub] [page(\firstsubpage)]
+\definereference [\v!previous\v!sub] [page(\prevsubpage)]
+\definereference [\v!next\v!sub] [page(\nextsubpage)]
+\definereference [\v!last\v!sub] [page(\lastsubpage)]
+
+%D We cannot set up buttons (not yet, this one calls a menu macro):
+
+\protect \endinput