% \iffalse
%% File: l3final.dtx Copyright (C) 1990-2006 LaTeX3 project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version.  The latest version
%% of this license is in the file
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% This file is part of the ``expl3 bundle'' (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%%    http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%%   Snapshots taken from the repository represent work in progress and may
%%   not work or may contain conflicting material!  We therefore ask
%%   people _not_ to put them into distributions, archives, etc. without
%%   prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%
%<*driver|package>
\RequirePackage{l3names}
%</driver|package>
%<*-ini>
%\fi
\GetIdInfo$Id: l3final.dtx 1857 2010-03-21 21:32:09Z joseph $
          {L3 Experimental final module}
%\iffalse
%</-ini>
%<*driver>
%\fi
\ProvidesFile{\filename.\filenameext}
  [\filedate\space v\fileversion\space\filedescription]
%\iffalse
\documentclass[full]{l3doc}
\begin{document}
\DocInput{\filename.\filenameext}
\end{document}
%</driver>
%\fi
%
%\section{Finalising \textsf{expl3}}
%
% This module is the end of \pkg{expl3}, and finishes off the 
% format. It also contains the loading code for the rest of \LaTeX3,
% along with those parts of the document structure which need to be
% in place for the rest of \LaTeX3 to load successfully.
% 
%\begin{function}{
%  \execute_at_document_begin:n |
%  \execute_at_document_end:n   | 
%  \execute_at_text_begin:n     |
%  \execute_at_text_end:n       |
%} 
%  \begin{syntax}
%    "\execute_at_document_begin:n" \marg{code}
%  \end{syntax}
%  Executes \meta{code} at the stated place in the document. 
%  \cs{execute_at_document_begin:n} code is executed at the 
%  beginning of |\begin{document}|, while \cs{execute_at_text_begin:n}
%  is at the end of |\begin{document}|, \emph{i.e}.~the start of the
%  text. In the same way, \cs{execute_at_text_end:n} code is run
%  by |\end{document}| immediately, while 
%  \cs{execute_at_document_end:n} is the final part of the document 
%  before calling \cs{tex_end:D}.
%  \begin{texnote}
%    \cs{execute_at_text_begin:n} is similar to \cs{AtBeginDocument}
%    in \LaTeXe, while \cs{execute_at_text_end:n} is similar to
%    \cs{AtEndDocument}.
%  \end{texnote}
%\end{function}
%
%\begin{variable}{
%  \g_execute_document_begin_tl |
%  \g_execute_document_end_tl   | 
%  \g_execute_text_begin_tl     |
%  \g_execute_text_end_tl       |
%} 
% The \meta{code} stored by the \cs{execute_at\ldots} functions is
% stored in these token registers.
%\end{variable}
% 
%\begin{function}{\par} 
%  \begin{syntax}
%    "\par"
%  \end{syntax}
%  \cs{par} needs to be defined, as \TeX\ uses it in some runaway 
%  situations.
%\end{function}
%
%\subsection{Minimal document support}
%
% Some temporary material to allow testing of the \pkg{xpackages} in
% \texttt{initex} mode.
% 
%\begin{macro}{\g_execute_document_begin_tl}
%\begin{macro}{\g_execute_document_end_tl}
%\begin{macro}{\g_execute_text_begin_tl}
%\begin{macro}{\g_execute_text_end_tl}
% Token registers to hold material to be executed at the start
% and end of the document.
%    \begin{macrocode}
%<*initex>
\tl_new:N \g_execute_document_begin_tl
\tl_new:N \g_execute_document_end_tl
\tl_new:N \g_execute_text_begin_tl
\tl_new:N \g_execute_text_end_tl
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%
%\begin{macro}{\execute_at_document_begin:n}
%\begin{macro}{\execute_at_document_end:n}
%\begin{macro}{\execute_at_text_begin:n}
%\begin{macro}{\execute_at_text_end:n}
% Four commands which add material to the token registers. In each
% case, the commands are set up to self-deactivate as the first thing
% that will happen when the hooks are executed.
%    \begin{macrocode}
\cs_new_protected:Npn \execute_at_document_begin:n #1 {
  \tl_gput_right:Nn \g_execute_document_begin_tl {#1}
}
\cs_new_protected:Npn \execute_at_document_end:n #1 {
  \tl_gput_right:Nn \g_execute_document_end_tl {#1}
}
\cs_new_protected:Npn \execute_at_text_begin:n #1 {
  \tl_gput_right:Nn \g_execute_text_begin_tl {#1}
}
\cs_new_protected:Npn \execute_at_text_end:n #1 {
  \tl_gput_right:Nn \g_execute_text_end_tl {#1}
}
\execute_at_document_begin:n {
  \tl_gclear:N \g_execute_document_begin_tl
  \cs_set_eq:NN \execute_at_document_begin:n \use:n
}
\execute_at_document_end:n {
  \tl_gclear:N \g_execute_document_end_tl
  \cs_set_eq:NN \execute_at_document_end:n \use:n
}
\execute_at_text_begin:n {
  \tl_gclear:N \g_execute_text_begin_tl
  \cs_set_eq:NN \execute_at_text_begin:n \use:n
}
\execute_at_text_end:n {
  \tl_gclear:N \g_execute_text_end_tl
  \cs_set_eq:NN \execute_at_text_end:n \use:n
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro} 
% 
% Two \emph{temporary} functions to allow testing: these are 
% very basic and just do enough for things to work! The real
% implementation of the document environment will end up somewhere
% else in the end. 
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \document {
  \tl_use:N \g_execute_document_begin_tl
  % Code will go here
  \tl_use:N \g_execute_text_begin_tl
}
\cs_new_protected_nopar:Npn \enddocument {
  \tl_use:N \g_execute_text_end_tl
  % Code will go here
  \tl_use:N \g_execute_document_end_tl
  \tex_end:D
}
%    \end{macrocode}
%    
% No-ops for \cs{begin} and \cs{end}, so that they can be used in
% error messages, \emph{etc}.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \begin #1 { }
\cs_new_protected_nopar:Npn \end #1 { }
%    \end{macrocode}
%
%\subsection{Higher-level \LaTeX3}
%
% Here, the rest of \LaTeX3 is loaded in a `chain'.
%    \begin{macrocode}
\clist_map_inline:nn 
  {
    xparse    ,
    xtemplate ,
  } 
  { 
    \file_if_exist:nT { #1 .ltx }
      { \tex_input:D #1 .ltx ~ }
  }
%    \end{macrocode}
%
%\subsection{Dumping the format}
%
%\begin{macro}{\par}
% \TeX\ has a nasty habit of inserting a command with the name \cs{par}
% so we had better make sure that that command at least has a definition.
%    \begin{macrocode}
\cs_set_eq:NN \par \tex_par:D
%    \end{macrocode}
%\end{macro}
%
% Until the start of the document, \cs{tex_everypar:D} is set up to
% print an error if there is any text. Using \cs{tex_nullfont:D} means
% that whitespace is not important between material in the preamble.
%    \begin{macrocode}
\msg_kernel_new:nnnn { document } { missing-begin-document }
  {Missing \begin{document}.}
  {
    You've tried to print something before \begin{document}\\%
    This probably means there's a mistake somewhere in your source.%
  }
\tex_everypar:D { 
  \msg_kernel_error:nn { document } { missing-begin-document } 
}
\tex_nullfont:D
%    \end{macrocode}
%
%\begin{macro}{\ExplSyntaxOff}
% We re-define \cs{ExplSyntaxOff} as the version that comes through 
% from \pkg{l3names} is defined using \TeX\ primitives, which we no
% longer have! Here, we can also make a fixed decision about the 
% category codes to fix.
%    \begin{macrocode}
\cs_set_protected_nopar:Npn \ExplSyntaxOff {
  \intexpr_if_odd:nT { \ExplSyntaxStatus } 
    {
      \tl_set:Nn \ExplSyntaxStatus { 0 }
      \char_set_catcode:nn { 126 } { 13 }
      \char_set_catcode:nn { 32 } { 10 }
      \char_set_catcode:nn { 9 } { 10 }
      \char_set_catcode:nn { 95 } { 8 }
      \char_set_catcode:nn { 58 } { 12 }
      \tex_endlinechar:D = 13 \scan_stop:
    }
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\latex_format_dump:}
% The last action to take is to dump the format.
%    \begin{macrocode}
\use:n {
  \ExplSyntaxOff
  \tex_dump:D
}
%</initex>
%    \end{macrocode}
%
%\endinput
