%\iffalse
% !TEX program  = pdflatex
% !TEX encoding = ISO-8859-1
%<*internal>
\iffalse
%</internal>
%<*readme>
---------------------------------------------------------------
The keys3opts2 package --- Using keys3 for LaTeX2e options
Maintained by Joseph Wright 
E-mail: joseph.wright@morningstar2.co.uk
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
---------------------------------------------------------------

The keys3 package is an experimental implementation of key
management for LaTeX3.  The entire package is written in
expl3 code rather than in traditional TeX/LaTeX.  The 
keys3opts2 package provides a mechanism to allow load-time
option processed using keys3 in LaTeX2e.

%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
---------------------------------------------------------------
The keys3opts2 package --- Using keys3 for LaTeX2e options
Maintained by Joseph Wright 
E-mail: joseph.wright@morningstar2.co.uk
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
---------------------------------------------------------------

EXPERIMENTAL CODE

Do not distribute this file without also distributing the
source files specified above.

Do not distribute a modified version of this file.

\endpreamble
\postamble

Copyright (C) 2008-2009 by
  Joseph Wright <joseph.wright@morningstar2.co.uk>

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 work is "maintained" (as per LPPL maintenance status) by
  Joseph Wright.

This work consists of the file  keys3opts2.dtx
          and the derived files keys3opts2.pdf,
                                keys3opts2.sty and
                                keys3opts2.ins.

\endpostamble
\usedir{tex/latex/keys3}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/keys3}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*driver|package>
\RequirePackage{l3names}
\GetIdInfo$Id: keys3opts2.dtx 93 2009-05-03 20:34:46Z josephwright $
  {Using keys3 for LaTeX2e options}
%</driver|package>
%\fi
%\iffalse
%<*driver>
%\fi
\ProvidesFile{\filename.\filenameext}
  [\filedate\space v\fileversion\space\filedescription]
%\iffalse
\documentclass[full]{l3doc}
\begin{document}
  \DocInput{\filename.\filenameext}
\end{document}
%</driver>
% \fi
%
% \title{The \textsf{keys3opts2} package^^A
%   \thanks{This file has version number \fileversion, last
%     revised \filedate.}\\
% Using \textsf{keys3} for \LaTeXe\ options}
% \author{Joseph Wright^^A
%   \thanks{E-mail: joseph.wright@morningstar2.co.uk}}
% \date{\filedate}
% \maketitle
%
%\section{Package options}
%
% The methods that \LaTeX3 will use to process package options are not
% clear at the present time.  However, in order to fully test the
% \pkg{expl3} bundle fully, it is useful to be able to work with
% the \LaTeXe\ system but using \LaTeX3 methods as far as possible.
%
% The \pkg{keys3} package provides an experimental method for
% managing key--value input using \pkg{expl3} syntax. It is
% self-contained and does not use any \LaTeXe methods. On the other
% hand, \pkg{keys3opts2} is intended as a ``bridge'' between the
% \LaTeXe\ kernel and \textsf{keys3}. It allows keys to be created
% using \pkg{keys3} and processed by \LaTeXe\ as package or class
% options.
%
%\subsection{functions}
%
% \begin{function}{\keys_options_ltx:n}
%   \begin{syntax}
%     "\keys_options_ltx:n" <module>
%   \end{syntax}
%   The only macro for outside use. Processes the current option list
%   using the currently defined keys, checking if they exist for
%   <module>.
% \end{function}
%
% \begin{function}{
%   \keys_options_ltx_cls:n|
%   \keys_options_ltx_pkg:n
% }
%   \begin{syntax}
%     "\keys_options_ltx_cls:n" <option>
%   \end{syntax}
%   Helper macros for processing class and package options,
%   respectively.
% \end{function}
%
% \begin{function}{\keys_options_ltx_strip_eq:w}
%   \begin{syntax}
%     "\keys_options_ltx_strip_eq:w" <option> "\q_stop"
%   \end{syntax}
%   Used to strip ``\texttt{=}'' out of options.
% \end{function}
%
% \begin{variable}{\l_keys_options_ltx_clist}
%   Storage area for option list.
% \end{variable}
%
%\subsection{Implementation}
%
% Almost everything here is based heavily on \pkg{pgfopts}, which is
% itself a port of the appropriate code from \pkg{kvoptions} to work
% with \pkg{pgfkeys}.
%
% As this package is an add-on to \pkg{keys3}, the same internal
% module name (|\keys_|) is used here as in the main package. The lead 
% off as usual.
%    \begin{macrocode}
%<*package>
\ProvidesExplPackage
  {\filename}{\filedate}{\fileversion}{\filedescription}
\RequirePackage{keys3}
%    \end{macrocode}
%
%\begin{macro}{\l_keys_options_ltx_clist}
% A simple storage area.
%    \begin{macrocode}
\clist_new:N \l_keys_options_ltx_clist
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\keys_options_ltx:n}
% As this is a minimal testing package, a lot of the complexity of the
% normal \LaTeX\ options is ignored. Here, the package insists a path
% is given for the keys. First, a path may need to be added to the
% argument given. The path we need to process is then stored in
% \cs{l_keys_current_key_tl}.
%    \begin{macrocode}
\cs_new:Nn \keys_options_ltx:n {
  \tl_set:Nx \l_keys_current_key_tl {#1}
  \keys_path_add:N \l_keys_current_key_tl
  \clist_clear:N \l_keys_options_ltx_clist
  \cs_if_eq:NNTF \@currext \@clsextension {
    \cs_if_free:cF { opt@ \@currname . \@currext } {
      \clist_map_function:cN { opt@ \@currname . \@currext }
        \keys_options_ltx_cls:n
    }
  }{
    \cs_if_free:NF \@classoptionslist {
      \clist_map_function:NN \@classoptionslist \keys_options_ltx_pkg:n
    }
    \cs_if_free:cF { opt@ \@currname . \@currext } {
      \exp_args:NNc \clist_concat:NNN \l_keys_options_ltx_clist
        { opt@ \@currname . \@currext } \l_keys_options_ltx_clist
    }
  }
  \clist_put_left:Nn \l_keys_options_ltx_clist { #1 /.cd: }
  \exp_args:NV \keys_manage:n \l_keys_options_ltx_clist
  \AtEndOfPackage { \cs_set_eq:NN \@unprocessedoptions \scan_stop: }
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\keys_options_ltx_cls:n}
% This macro checks if the current class option is a known key. To do
% that, any ``\texttt{=}'' part has to be removed first.
%    \begin{macrocode}
\cs_new:Nn \keys_options_ltx_cls:n {
  \keys_if_cmd_exist:nTF {
    \l_keys_current_key_tl / \keys_options_ltx_strip_eq:w #1 = \q_stop
  }{
    \clist_put_right:Nn \l_keys_options_ltx_clist {#1}
  }{
    \clist_put_right:Nn \@unusedoptionlist {#1}
  }
}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\keys_options_ltx_pkg:n}
% For packages, options need to be removed from the global list. 
%    \begin{macrocode}
\cs_new:Nn \keys_options_ltx_pkg:n {
  \keys_if_cmd_really_exist:nT {
    \l_keys_current_key_tl / \keys_options_ltx_strip_eq:w #1 = \q_stop
  }{
    \clist_put_right:Nn \l_keys_options_ltx_clist {#1}
    \clist_remove_element:Nn \@unusedoptionlist {#1}
  }
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\keys_options_ltx_strip_eq:w}
% A simple method to remove an equals sign plus any trailing data.
%    \begin{macrocode}
\cs_new:Npn \keys_options_ltx_strip_eq:w #1 = #2 \q_stop {#1}
%</package>
%    \end{macrocode}
%\end{macro}
%
%\Finale
