% \iffalse meta-comment
%
%% regulatory-attachments.dtx
%% Copyright 2024-2026 E. Nijenhuis
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status ‘maintained’.
%
% The Current Maintainer of this work is E. Nijenhuis.
%
% This work consists of the files listed in the meta-comment of
% regulatory-struct.dtx.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{regulatory-attachments.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{regulatory-attachments}
%<package>    [2026/09/10 1.0.0 Xerdi's Regulatory Package (Attachments)]
%
%<*driver>
\documentclass[10pt,english]{ltxdoc}
%! suppress = InclusionLoop
\usepackage{regulatory}
\usepackage{tabularx}
\usepackage[english,dutch]{babel}
\input{regulatory-preamble}
\newcommand\translation[2]{#1}
\begin{document}
    \selectlanguage{english}
    \DocInput{regulatory-attachments.dtx}
\end{document}
%</driver>
% \fi
%
% \subsection{\texorpdfstring{\package{regulatory-attachments}}{regulatory-attachments}}
% \setcounter{CodelineNo}{0}
%
% \subsubsection{\translation{Prerequisites}{Vereisten}}
%
% A reference to another document is a reference first, so this module builds on \package{regulatory-ref},
% which loads \package{regulatory-struct} in turn. The definitions of another document land in a glossary
% of their own, which is why \package{regulatory-defs} is needed as well. Both \package{xr-hyper} and \package{zref-xr} are already
% loaded by \package{regulatory-struct}, since they have to be loaded before \package{hyperref}.
% Options are handed over to \package{regulatory-struct}, which administers them for the whole bundle.
% \iffalse
%<*package>
% \fi
%    \begin{macrocode}
\@ifpackageloaded{regulatory-struct}{}{%
    \DeclareOption*{\PassOptionsToPackage{\CurrentOption}{regulatory-struct}}%
    \ProcessOptions\relax
}
\RequirePackage{regulatory-ref}
\RequirePackage{regulatory-defs}

\DeclareTranslationFallback{of the}{of the}
\DeclareTranslationFallback{see}{see}
%    \end{macrocode}
%
% Definitions of other documents are kept in a glossary type of their own, so they don't get mixed up with
% the definitions of the document itself.
%    \begin{macrocode}
\newglossary*{externals}{Externals}
%    \end{macrocode}
%
% \subsubsection{\translation{Artifacts}{Artefacten}}
%
% \begin{macro}{\newartifact}
% An artifact holds everything known about another document: where its file is, how it is referred to, and
% what a PDF viewer should say about it once it is attached. The artifacts of a document live in one
% property list, keyed by \meta{name} and field, and the fields a document may give are listed rather than
% left open: a field this package does not know is a misspelling of one that it does, and it used to be
% stored under the wrong name and read back as nothing.
%
% Two of the fields are not for a document to set, and they are held apart from the ones that are. They are
% accepted, because a document written against an earlier release may give one and stopping such a document
% would be the worse of the two errors, and they are left out of the list the error message offers, because
% that list is read as an invitation.
%
% \texttt{referred} is set by \cmd{\documentfootnote} to record that an artifact has had its footnote, and
% a document that sets it by hand suppresses a footnote it never saw. \texttt{url} is reserved: it is
% stored and readable with \cmd{\artifacturl}, and nothing in this bundle reads it. It was meant to name
% where the document may be found, in the footnote beside the attachment, and that is not implemented. It
% is kept rather than removed so that the name stays free for exactly that, and it is documented as
% reserved rather than as an option, since an option that does nothing is a promise.
%    \begin{macrocode}
\newcommand\artifact@footnote@noop[1]{#1}
\newcommand\artifact@def@label@default[1]{~(\GetTranslation{see} #1)}

\ExplSyntaxOn
\prop_new:N \g__regulatory_artifact_prop
\cs_generate_variant:Nn \prop_item:Nn  { Ne }
\cs_generate_variant:Nn \prop_gput:Nnn { Nen }
\cs_generate_variant:Nn \prop_get:NnNF { NeNF }
\cs_generate_variant:Nn \prop_if_in:NnTF { NeTF }

\clist_const:Nn \c__regulatory_artifact_keys_clist
  {
    name , filename , path , ref~label , def~label , footnote , footnote~label ,
    defs , author , subject , description
  }

\clist_const:Nn \c__regulatory_artifact_reserved_clist { url , referred }

\cs_new_protected:Npn \__regulatory_artifact_put:nnn #1#2#3
  { \prop_gput:Nen \g__regulatory_artifact_prop { #1 / #2 } { #3 } }

\cs_new_protected:Npn \__regulatory_artifact_set:nnn #1#2#3
  {
    \clist_if_in:NnTF \c__regulatory_artifact_keys_clist { #2 }
      { \__regulatory_artifact_put:nnn { #1 } { #2 } { #3 } }
      {
        \clist_if_in:NnTF \c__regulatory_artifact_reserved_clist { #2 }
          { \__regulatory_artifact_put:nnn { #1 } { #2 } { #3 } }
          {
            \msg_error:nnnn { regulatory-attachments } { unknown-field }
              { #1 } { #2 }
          }
      }
  }

\cs_new_protected:Npn \__regulatory_artifact_bare:nn #1#2
  { \msg_error:nnnn { regulatory-attachments } { field-without-value } { #1 } { #2 } }

\msg_new:nnn { regulatory-attachments } { unknown-field }
  {
    The~artifact~`#1'~was~given~a~field~`#2',~which~this~package~does~not~know.~\\
    The~fields~are:~\clist_use:Nn \c__regulatory_artifact_keys_clist { ,~ }.
  }

\msg_new:nnn { regulatory-attachments } { field-without-value }
  { The~field~`#2'~of~artifact~`#1'~was~given~no~value. }

\NewDocumentCommand \newartifact { m m }
  {
    \__regulatory_artifact_put:nnn { #1 } { name }           { #1 }
    \__regulatory_artifact_put:nnn { #1 } { filename }       { #1.pdf }
    \__regulatory_artifact_put:nnn { #1 } { path }           { ./ }
    \__regulatory_artifact_put:nnn { #1 } { ref~label }      { }
    \__regulatory_artifact_put:nnn { #1 } { def~label }      { \artifact@def@label@default }
    \__regulatory_artifact_put:nnn { #1 } { footnote }       { true }
    \__regulatory_artifact_put:nnn { #1 } { footnote~label } { \artifact@footnote@noop }
    \__regulatory_artifact_put:nnn { #1 } { url }            { }
    \__regulatory_artifact_put:nnn { #1 } { referred }       { false }
    \__regulatory_artifact_put:nnn { #1 } { defs }           { }
    \__regulatory_artifact_put:nnn { #1 } { author }         { <author> }
    \__regulatory_artifact_put:nnn { #1 } { subject }        { <subject> }
    \__regulatory_artifact_put:nnn { #1 } { description }    { <description> }
    \keyval_parse:nnn
      { \__regulatory_artifact_bare:nn { #1 } }
      { \__regulatory_artifact_set:nnn { #1 } }
      { #2 }
  }

\cs_new_protected:Npn \artifact@ifknown #1
  { \prop_if_in:NeTF \g__regulatory_artifact_prop { #1 / name } }

\cs_new_protected:Npn \artifact@ifuncomposable #1
  { \prop_if_in:NeTF \g__regulatory_artifact_prop { #1 / uncomposable } }

\cs_new_protected:Npn \artifact@setuncomposable #1
  { \__regulatory_artifact_put:nnn { #1 } { uncomposable } { true } }

\cs_new_protected:Npn \artifact@setreferred #1
  { \__regulatory_artifact_put:nnn { #1 } { referred } { true } }
\ExplSyntaxOff
%    \end{macrocode}
% \end{macro}
%
% The accessors of an artifact, of which the full name and the full filename are the ones prefixed with the
% path of the artifact.
%    \begin{macrocode}
\ExplSyntaxOn
\cs_new:Npn \@aget #1#2 { \prop_item:Ne \g__regulatory_artifact_prop { #1 / #2 } }
\cs_new_protected:Npn \artifact@setfootnotelabel #1
  {
    \prop_get:NeNF \g__regulatory_artifact_prop { #1 / footnote~label }
      \artifactfootnotelabel
      { \cs_set_eq:NN \artifactfootnotelabel \artifact@footnote@noop }
  }
\ExplSyntaxOff
\newcommand\artifactname[1]{\@aget{#1}{name}}
\newcommand\artifactfullname[1]{\@aget{#1}{path}\@aget{#1}{name}}
\newcommand\artifactfilename[1]{\@aget{#1}{filename}}
\newcommand\artifactfullfilename[1]{\@aget{#1}{path}\@aget{#1}{filename}}
\newcommand\artifactfootnote[1]{\@aget{#1}{footnote}}
%    \end{macrocode}
% The reader of the reserved \texttt{url} field. It gives back what was stored and nothing else reads it;
% see \cmd{\newartifact} above.
%    \begin{macrocode}
\newcommand\artifacturl[1]{\@aget{#1}{url}}
\newcommand\artifactreferred[1]{\@aget{#1}{referred}}
\newcommand\artifactdefs[1]{\@aget{#1}{defs}}
\newcommand\artifactauthor[1]{\@aget{#1}{author}}
\newcommand\artifactsubject[1]{\@aget{#1}{subject}}
\newcommand\artifactdescription[1]{\@aget{#1}{description}}
%    \end{macrocode}
%
% \subsubsection{\translation{Embedding files}{Bestanden invoegen}}
%
% \begin{macro}{\regulatory@embed}
% Embeds the file of \#1 under the name \#2, as an object named \#3 with description \#4. The file is
% registered as an associated file of the document with relationship \texttt{/Source}, and it is added to
% the embedded files of the catalog, so that PDF viewers list it as an attachment.
%    \begin{macrocode}
\ExplSyntaxOn
\str_new:N \l__regulatory_desc_str
\str_new:N \l__regulatory_ext_str
%    \end{macrocode}
%
% PDF/A-3 requires the MIME type of every embedded file, and prescribes
% \texttt{application/octet-stream} whenever it is unknown. \package{l3pdf} looks the type up by
% extension and only warns when it finds none, so the prescribed fallback is filled in here.
%    \begin{macrocode}
\cs_new_protected:Npn \__regulatory_mimetype:n #1
  {
    \file_parse_full_name:nNNN {#1}
      \l_tmpa_str \l_tmpb_str \l__regulatory_ext_str
    \prop_if_in:NVF \g_pdffile_mimetypes_prop \l__regulatory_ext_str
      {
        \pdfdict_put:nne {l_pdffile}{Subtype}
          { \pdf_name_from_unicode_e:n {application/octet-stream} }
      }
  }

\cs_new_protected:Npn \__regulatory_embed:nnnn #1#2#3#4
  {
    \group_begin:
      \pdfdict_put:nnn {l_pdffile/Filespec}{AFRelationship}{/Source}
      \__regulatory_mimetype:n {#1}
      \tl_if_empty:nF {#4}
        {
          \pdf_string_from_unicode:nnN {utf16/string}{#4}\l__regulatory_desc_str
          \pdfdict_put:nne {l_pdffile/Filespec}{Desc}{\l__regulatory_desc_str}
        }
      \pdffile_embed_file:nnn {#1}{#2}{regulatory/attach/#3}
      \pdfmanagement_add:nne {Catalog/Names/EmbeddedFiles}
        {#3}{\pdf_object_ref:n{regulatory/attach/#3}}
      \pdfmanagement_add:nne {Catalog}{AF}
        {\pdf_object_ref:n{regulatory/attach/#3}}
    \group_end:
  }
%    \end{macrocode}
%
% The description arrives expanded, all four arguments alike. The string conversion of
% \package{l3pdf} assumes the purifying step has already been done, and says so in its own
% source, so a description handed over unexpanded is written into the PDF as the tokens it is
% made of: every attachment used to carry \cmd{\artifactdescription}\marg{label} as its
% description rather than the description itself, which is what a viewer shows beside the file
% in its attachment pane.
%    \begin{macrocode}
\cs_generate_variant:Nn \__regulatory_embed:nnnn {eeee}
\cs_new_protected:Npn \regulatory@embed #1#2#3#4
  { \__regulatory_embed:eeee {#1}{#2}{#3}{#4} }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\regulatory@attach@annot}
% The other route of \option{attachmentlink}: a file attachment annotation of exactly the size of
% \#2, with \#2 drawn over it, so that the text is what the reader sees and the annotation is what
% the reader opens. \verb|\pdfannot_box:nnnn| advances by nothing of its own, so the text that
% follows lands exactly where the annotation is.
% The appearance is the empty one, since the page already draws everything there
% is to see; an appearance there has to be, because the A-standards ask one of every annotation
% that is not a \texttt{Popup}, a \texttt{Link} or of zero size. The \texttt{/Contents} is the
% description the file was embedded with, which is what a screen reader announces.
%    \begin{macrocode}
\box_new:N \l__regulatory_attach_box
\tl_new:N \l__regulatory_attach_desc_tl
\str_new:N \l__regulatory_attach_desc_str
\cs_new:Npn \regulatory@attach@appearance { }
\bool_lazy_and:nnT
  { \cs_if_exist_p:N \pdfmanagement_if_active_p: }
  { \cs_if_exist_p:N \pdf_object_new:n }
  {
    \pdfmanagement_if_active:T
      {
        \pdf_object_new:n { regulatory/attach/blank }
        \AddToHook { shipout/firstpage }
          {
            \pdf_object_write:nnn { regulatory/attach/blank } { stream }
              { { /Type~/XObject /Subtype~/Form /BBox~[0~0~1~1] } { } }
          }
        \cs_gset:Npn \regulatory@attach@appearance
          { /AP~<<~/N~\pdf_object_ref:n { regulatory/attach/blank }~>> }
      }
  }
\cs_new_protected:Npn \regulatory@attach@place #1#2
  {
    \pdfannot_box:nnne
      { \box_wd:N \l__regulatory_attach_box }
      { \box_ht:N \l__regulatory_attach_box }
      { \box_dp:N \l__regulatory_attach_box }
      {
        /Subtype~/FileAttachment
        /FS~\pdf_object_ref:n { regulatory/attach/ #1 }
        /F~4
        \tl_if_empty:NF \l__regulatory_attach_desc_tl
          { /Contents~\l__regulatory_attach_desc_str }
        \regulatory@attach@appearance
        #2
      }
  }
\cs_new_protected:Npn \regulatory@attach@annot #1#2
  {
    \tl_set:Ne \l__regulatory_attach_desc_tl
      { \cs_if_exist_use:cF { regulatory@desc@ #1 } { } }
    \pdf_string_from_unicode:nVN {utf16/string}
      \l__regulatory_attach_desc_tl \l__regulatory_attach_desc_str
    \hbox_set:Nn \l__regulatory_attach_box { #2 }
    \mode_leave_vertical:
    \cs_if_exist:NTF \tag_if_active:TF
      { \tag_if_active:TF { \regulatory@attach@tagged {#1} }
                          { \regulatory@attach@place {#1} { } } }
      { \regulatory@attach@place {#1} { } }
    \box_use:N \l__regulatory_attach_box
  }
\cs_new_protected:Npn \regulatory@attach@tagged #1
  {
        \tag_mc_end_push:
        \tag_struct_begin:n { tag=Annot }
        \regulatory@attach@place {#1}
          { /StructParent~\tag_struct_parent_int: }
        \exp_args:Nee
        \tag_struct_insert_annot:nn
          { \pdfannot_ref_last: } { \tag_struct_parent_int: }
        \tag_struct_end:
        \tag_mc_begin_pop:n {}
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\regulatory@attachmentlink}
% Turns \#2 into whatever \option{attachmentlink} says: a link that opens the embedded file named
% \#1 in a new window, or the annotation above.
%
% For the link, the destination is the first page of the target, and it is not optional: an
% embedded go-to action without a \texttt{/D} is not one, and a viewer that reads it has nowhere
% to go. Not every viewer goes there even so\,---\,\texttt{GoToE} is the one file-opening action
% poppler does not implement, so in the viewers built on it the file is reachable through the
% attachment pane and not through this link. That is what the other route is for.
%
% The link also carries the file in its \texttt{/AF}. An action is something a consumer has to
% perform to learn what it points at; an associated file is something it can read. This route
% names the file indirectly, by a key in the name tree of the document, so the entry says
% declaratively what the action says only when it is followed. The other route needs none: a file
% attachment annotation names its file in \texttt{/FS} already, and an \texttt{/AF} beside it
% would say the same thing twice.
%    \begin{macrocode}
\NewDocumentCommand \regulatory@attachmentlink { m m }
  {
    \str_if_eq:VnTF \regulatory@attachkind { annotation }
      { \regulatory@attach@annot {#1} {#2} }
      {
        \pdfannot_link_begin:nnw {user}
          {
            /Subtype/Link/F~4
            /AF~[~\pdf_object_ref:n { regulatory/attach/ #1 }~]
            /A<</S/GoToE/D~[0~/Fit]/NewWindow~true/T<</R/C/N~(#1)>>>>
          }
        #2
        \pdfannot_link_end:n {user}
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\regulatory@ifembed}
% Embedding a file is only possible while the PDF management of \LaTeX{} is active, which a document
% activates with \cmd{\DocumentMetadata} in front of its \cmd{\documentclass}. Without it,
% the file embedding commands of \package{l3pdf} do not even exist, so every attachment falls back to its plain
% text and the document is told once what it is missing.
%    \begin{macrocode}
\cs_if_exist:NTF \pdffile_embed_file:nnn
  { \cs_new_eq:NN \regulatory@ifembed \use_i:nn }
  { \cs_new_eq:NN \regulatory@ifembed \use_ii:nn }
\ExplSyntaxOff

\regulatory@ifembed{}{%
    \PackageWarning{regulatory}{%
        Attachments need the PDF management of LaTeX.\MessageBreak
        Put \string\DocumentMetadata{} before \string\documentclass\MessageBreak
        to activate it. Attachments are typeset as plain text%
    }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\regulatory@embedonce}
% A document referred to more than once is embedded only the first time, of which the object name is kept
% for all following links.
%    \begin{macrocode}
\newcommand\regulatory@embedonce[1]{%
    \expandafter\ifx\csname regulatory@embedded@#1\endcsname\relax
        \regulatory@embed
            {\artifactfullfilename{#1}}%
            {\artifactfilename{#1}}%
            {regulatory-#1}%
            {\artifactdescription{#1}}%
        \expandafter\xdef\csname regulatory@desc@regulatory-#1\endcsname
            {\artifactdescription{#1}}%
        \expandafter\xdef\csname regulatory@embedded@#1\endcsname{regulatory-#1}%
    \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\documentattachment}
% Attaches the file of artifact \meta{label} and prints \meta{link text} as the link to it. A missing file
% is no reason to fail; its link text is printed verbatim instead.
%    \begin{macrocode}
\newcommand\documentattachment[2]{%
    \def\@tmpfile{\artifactfullfilename{#1}}%
    \regulatory@ifembed{%
        \IfFileExists{\@tmpfile}{%
            \regulatory@embedonce{#1}%
            \expandafter\expandafter\expandafter\regulatory@attachmentlink
                \expandafter\expandafter\expandafter
                {\csname regulatory@embedded@#1\endcsname}{#2}%
        }%
        {\texttt{#2}}%
    }{\texttt{#2}}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\attachdocument}
% Attaches any file by its \meta{filename}, without an artifact of its own, with \meta{description} as the
% description of the attachment.
%    \begin{macrocode}
\newcommand\attachdocument[3][]{%
    \regulatory@ifembed{%
        \IfFileExists{#2}{%
            \expandafter\ifx\csname regulatory@plain@#2\endcsname\relax
                \regulatory@embed {#2}{#2}{regulatory-#2}{#1}%
                \expandafter\xdef\csname regulatory@desc@regulatory-#2\endcsname{#1}%
                \expandafter\xdef\csname regulatory@plain@#2\endcsname{regulatory-#2}%
            \fi
            \expandafter\expandafter\expandafter\regulatory@attachmentlink
                \expandafter\expandafter\expandafter
                {\csname regulatory@plain@#2\endcsname}{#3}%
        }%
        {\texttt{#3}}%
    }{\texttt{#3}}%
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{\translation{Mentioning the source}{Bronvermelding}}
%
% \begin{macro}{\documentlabel}
% How a reference mentions the document it belongs to, which defaults to the subject of the artifact with
% a connective before it: \enquote{of the Example}, \enquote{van de Voorbeeldakte}.
%
% That default only works in a language that leaves a noun alone. German does not, and it is the case that
% settles the design. Its connective is a genitive that reshapes the word it governs: measured in the
% German text of the General Data Protection Regulation, \enquote{der Verordnung} sixteen times and
% \enquote{der Richtlinie} twenty-nine, but \enquote{des Vertrags} and \enquote{des Beschlusses}\,---\,not
% \emph{des Vertrag}. So the ending belongs to the noun and not to the connective, and no rule over a
% connective and a subject in the nominative can produce the phrase. Handing the artifact a determiner
% would not do it either; the determiner is the half that is easy.
%
% What is left is to state the phrase, which \texttt{ref label} does and always did. A language that
% cannot compose one therefore declares its connective empty, and a reference that then has no
% \texttt{ref label} to fall back on says so once per artifact rather than quietly wording it in another
% language.
%    \begin{macrocode}
\newcommand\documentlabel[1]{%
    \def\@@label{\@aget{#1}{ref label}}%
    \ifthenelse{\equal{\@@label}{}}{%
        \edef\@@of{\GetTranslation{of the}}%
        \ifx\@@of\@empty
            \artifact@warnuncomposable{#1}%
            \artifactsubject{#1}%
        \else
            \@@of\space\artifactsubject{#1}%
        \fi
    }{\@@label}%
}

\newcommand\artifact@warnuncomposable[1]{%
    \artifact@ifuncomposable{#1}{}{%
        \artifact@setuncomposable{#1}%
        \PackageWarning{regulatory}{%
            A reference to `#1' is worded in \languagename\space by naming its\MessageBreak
            subject alone: that language inflects the noun a connective\MessageBreak
            governs, so this bundle cannot build the phrase. Give the\MessageBreak
            artifact a `ref label' with the phrase in it. Reported}%
    }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\documentfootnote}
% Places the footnote of the first occurrence of a reference or a definition of another document, with the
% document itself attached to it. The artifact is marked as referred, so that following occurrences don't
% repeat the footnote.
%    \begin{macrocode}
\newcommand\documentfootnote[2][]{%
    \artifact@ifknown{#2}{%
        \artifact@setreferred{#2}%
        \artifact@setfootnotelabel{#2}%
        \ifthenelse{\equal{#1}{}}{%
            \def\artifact@description{\artifactsubject{#2}}%
        }{%
            \def\artifact@description{#1}%
        }%
        \footnote{\artifactfootnotelabel{\documentattachment{#2}{\artifact@description}}}%
    }{\PackageWarning{regulatory}{Artifact does not exists '#2'}\footnote{DOCUMENT NOT FOUND '#2'}}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\aref@postlink@setup}
% This is where a complete reference of \package{regulatory-ref} gets its source mentioned. Labels of the
% document itself carry no \texttt{externaldocument} property, in which case nothing is added at all.
%    \begin{macrocode}
\renewcommand\aref@postlink@setup[1]{%
    \zref@ifrefcontainsprop{#1}{externaldocument}{%
        \filename@parse{\expanded{\zref@extract{#1}{externaldocument}}}%
        \edef\@aref@filelabel{\expanded{\filename@base}}%
        \def\@aref@postlink{%
            , \documentlabel{\@aref@filelabel}%
            \ifthenelse{\equal{\artifactfootnote{\@aref@filelabel}}{true}\and\equal{\artifactreferred{\@aref@filelabel}}{false}}{%
                \documentfootnote{\@aref@filelabel}%
            }{}%
        }%
    }{%
        \def\@aref@postlink{}%
    }%
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{\translation{Linking documents}{Documenten koppelen}}
%
% \begin{macro}{\loadextdefs}
% Loads the definitions of \meta{src} under the \texttt{externals} type and the category \meta{category},
% which is the name of the artifact they belong to. Every first occurrence of such a definition mentions its
% source, unless the document was already referred to.
%    \begin{macrocode}
\newcommand\loadextdefs[2][externals]{%
    \ifregulatory@bibtogls%
        \GlsXtrLoadResources[type=externals,src={#2},sort={nl-NL},category={#1}]%
    \else%
        \loadglsentries[externals]{#2}%
    \fi%
    \glsdefpostlink{#1}{%
        \ifthenelse{\equal{\artifactreferred{#1}}{true}}{}{%
            \@aget{#1}{def label}{%
                \artifactsubject{#1}%
                \documentfootnote{#1}%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\regulatory@extdefs@nohyper}
% A definition of another document is printed in that other document, not in this one, so the anchor its
% citation would point at is not here. \package{glossaries} links to it all the same, and the result is a
% link that goes nowhere: measured on the examples, the PDF of the second one holds a link to
% \texttt{glo:def1} and no destination by that name, and the HTML of it holds an \texttt{href} to a
% fragment that lives in the other file.
%
% The hyperlink is therefore withdrawn for the \texttt{externals} type, in the hook \package{glossaries}
% offers for exactly this, after the options of a citation are set and before it is typeset. The citation
% itself stays, and so does the mention of the document it comes from, which is what tells a reader where
% to look\,---\,the same thing \package{regulatory-html} does for a reference to a provision of another
% document.
%    \begin{macrocode}
\def\regulatory@externalstype{externals}
\renewcommand*{\glslinkpostsetkeys}{%
    \edef\regulatory@thisdeftype{\glsentrytype{\glslabel}}%
    \ifx\regulatory@thisdeftype\regulatory@externalstype%
        \setkeys{glslink}{hyper=false}%
    \fi%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\refdocument}
% Declares another \package{regulatory} document to refer to, of which the labels are read with
% \cmd{\zexternaldocument} of \package{zref-xr} and prefixed with \meta{prefix}.
%    \begin{macrocode}
\newcommand\refdocument[3][ext-]{%
    \newartifact{#2}{footnote=false,#3}%
    \zexternaldocument[#1]{\artifactfullname{#2}}[#2]%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\masterdocument}
% Like \cmd{\refdocument}, but with the definitions of the other document loaded as well and with the
% document itself attached at the first occurrence of a reference or a definition. With \package{bib2gls},
% the hyperlinks of those definitions are aimed at the other document.
%    \begin{macrocode}
\newcommand\masterdocument[3][ext-]{%
    \newartifact{#2}{#3}%
    \zexternaldocument[#1]{\artifactfullname{#2}}[#2]%
    \ifthenelse{\equal{\artifactdefs{#2}}{}}{%
        \PackageWarning{regulatory}{No definitions given to the master document. \the\gls commands will therefore not work.}%
    }{%
        \loadextdefs[#2]{\artifactdefs{#2}}%
        \ifregulatory@bibtogls%
            \glssetcategoryattribute{#2}{targeturl}{\artifactfilename{#2}}%
            \glssetcategoryattribute{#2}{targetname}{\glolinkprefix\glslabel}%
        \fi%
    }%
}
%    \end{macrocode}
% \iffalse
%</package>
% \fi
% \end{macro}
%
% \Finale
%
