%D \module
%D   [       file=strc-bkm,
%D        version=2009.04.01,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Bookmarks,
%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.

\writestatus{loading}{ConTeXt Structure Macros / Bookmarks}

\registerctxluafile{strc-bkm}{1.001}

\unprotect

%D Bookmarks are a very viewer dependent feature. They are mostly used
%D as additional table of contents and therefore relate directly to lists.
%D
%D A bookmark list is added to the document only when interaction is
%D enabled. The given lists are bookmarked and a second argument specifies
%D the opened bookmark trees.
%D
%D \starttyping
%D \placebookmarks
%D   [chapter,section,subsection,mylist]
%D   [chapter]
%D \stoptyping
%D
%D You can overloads the last set bookmark in a sectioning command:
%D
%D \starttyping
%D \chapter {the first chapter}
%D \bookmark {the first bookmark}
%D \stoptyping
%D
%D However, in practice you can better use \type {\startchapter} and set the
%D \type {bookmark} parameter.
%D
%D You can add entries to the bookmarklist:
%D
%D \starttyping
%D \bookmark[mylist]{whatever}
%D \stoptyping
%D
%D Use force to get titles in the bookmarklist. This is somewhat tricky as
%D one does not want "Contents" in a table of contents but it has to be in
%D the bookmark list.

\installcorenamespace{bookmark}

\installsetuponlycommandhandler \??bookmark {bookmark} % installdirectparametersethandler

\setupbookmark
  [\c!force=\v!no,    % it's easier to force that to inhibit
   \c!number=\v!yes]  % might become v!no

\let\setupbookmarks\setupbookmark

\unexpanded\def\bookmark
  {\dosingleempty\strc_bookmarks_bookmark}

\def\strc_bookmarks_bookmark
  {\iflocation
     \expandafter\strc_bookmarks_bookmark_yes
   \else
     \expandafter\strc_bookmarks_bookmark_nop
   \fi}

\def\strc_bookmarks_bookmark_yes[#1]#2%
  {\begingroup
   \simplifycommands
   \ctxcommand{overloadbookmark("#1",\!!bs\detokenize\expandafter{\normalexpanded{#2}}\!!es)}%
   \endgroup}

\def\strc_bookmarks_bookmark_nop[#1]#2%
  {}

\unexpanded\def\placebookmarks
  {\dotripleempty\strc_bookmarks_place}

\def\strc_bookmarks_place
  {\iflocation
     \expandafter\strc_bookmarks_place_yes
   \else
     \expandafter\gobblethreeoptionals
   \fi}

\let\m_bookmarks_names \empty
\let\m_bookmarks_opened\empty

\def\strc_bookmarks_place_yes[#1][#2][#3]%
  {\begingroup
   \edef\m_bookmarks_names{#1}%
   \edef\m_bookmarks_opened{#2}%
   \ifx\m_bookmarks_names\empty
     \edef\m_bookmarks_names{\namedlistparameter\v!content\c!list}%
   \fi
   \ifx\m_bookmarks_names\empty
     \let\m_bookmarks_names\v!all
   \fi
   \ifthirdargument
     \setupcurrentbookmark[#3]% no every so not all possible
   \else\ifsecondargument
     \doifassignmentelse{#2}{\let\m_bookmarks_opened\empty\setupcurrentbookmark[#2]}\donothing
   \fi\fi
   \ctxcommand{registerbookmark {
      names  = "\m_bookmarks_names",
      opened = "\m_bookmarks_opened",
      force  = "\bookmarkparameter\c!force",
      number = "\bookmarkparameter\c!number",
    }}%
   \endgroup}

\appendtoks
    \ctxcommand{setupbookmarks {
        separatorset  = "\bookmarkparameter\c!numberseparatorset",
        conversionset = "\bookmarkparameter\c!numberconversionset",
        starter       = \!!bs\bookmarkparameter\c!numberstarter\!!es,
        stopper       = \!!bs\bookmarkparameter\c!numberstopper\!!es,
        segments      = "\bookmarkparameter\c!numbersegments",
    }}%
\to \everysetupbookmark

\protect \endinput

% \starttext
% \setupinteraction[state=start]\setupinteractionscreen[option=bookmark]
% \placebookmarks[chapter,section,subsection][chapter]
% \chapter{First}
% \bookmark{The First Indeed}
% \section{alpha}
% \bookmark[chapter]{The First Indeed Again}
% \section{beta}
% \chapter{Second}
% \bookmark{The Second Indeed}
% \section{gamma \tex{radiation}}
% \subsection{a}
% \subsection{b}
% \section{delta}
% \section{epsilon}
% \chapter{Third \relax}
% \chapter{我〈能吞下玻璃而不傷身〉體。} % whatever that means
% \chapter{Idris Samawi Hamid ادريس سماوي حامد}
% \stoptext
