\setupinteraction[state=start]

\ctxlua{dofile("glossarium.lua")}

% Use \gloss#1#2 somewhere before \starttext, in a secondary file if you like.
% #1: gloss reference key (used as index)
% #2: the entry (explanation)
\def\gloss#1#2{%
  \ctxlua{gloss.newgloss("#1", "\luaescapestring{#2}")}
}

\def\dousegloss[#1]#2{%
  \ctxlua{gloss.usegloss("#2", "#1")}%
}

% Use this in your text to create a reference to the glossary.
% #1: optional, If this is the main passage where you explain the entry in detail,
%     the glossary will link back here. (Just make it non-empty.)
% #2: the gloss key
\def\usegloss{\dosingleempty\dousegloss}


% This is meant to create the glossary after the text:
% The gloss key is typeset in bold face and will reference the point in the
% main text where \usegloss was called with a nonempty first arg.
\def\placeglossary{\ctxlua{gloss.place_glossary()}}

\starttext

\gloss{glossary}{%
  A collection of glosses; a list with explanations of abstruse,
  antiquated, dialectal, or technical terms; a partial dictionary. (As defined by
  M.~Saunders)%
}

\gloss{index}{%
  An index (plural: indexes) is a list of words or phrases ('headings') and
  associated pointers ('locators') to where useful material relating to that
  heading can be found in a document.
  (As def'ed by the English Wikipedia)
}

test \usegloss{glossary} test.
\page
Here \usegloss{index} just occurs.
\page
The glossary entry \usegloss[exp]{index} is explained here.
\page
Here \usegloss{index} just occurs again.
\page
Here we'll deal with \usegloss[exp]{glossary} in detail.
\page
Here \usegloss{index} just occurs again.
\page
Here \usegloss{index} just occurs again.
\page
Here we mention \usegloss{glossary}.
\page
Here \usegloss{index} just occurs again.
\page

\placeglossary



\stoptext
