% \iffalse meta-comment
%
% Copyright (C) 2019 
% Gau, Syu ( GauSyu@Gmail.com )
%
% ----------------------------------------------------------------------
%
% 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. This version of this license is in
%    http://www.latex-project.org/lppl/lppl-1-3c.txt
% and the latest version of this license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.3 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 
%    Gau, Syu ( GauSyu@Gmail.com ).
%
% ----------------------------------------------------------------------
%
%<*internal>
\iffalse
%</internal>
%<*readme>

# secnum

This package provides a macro \setsecnum to format section numbering intuitively.

## Installation

The installation is the same as usual Tex packages.

0. Put the source file `secnum.dtx` in a empty folder and go to there.

1. Run the following to create the package file `secnum.sty` (as well as this file `README.md`)

       XeTeX secnum.dtx

2. Move the following file into proper directories searched by TeX.
   The recommended directory is

       tex/latex/secnum

3. To produce the documentation run the following

       XeLaTeX secnum.dtx

4. The recommended directory for the documentation is

       doc/latex/secnum

## Download

One can also download the generated files from the [github release](https://github.com/GauSyu/secnum/releases).

%</readme>
%<*internal>
\fi
\begingroup
  \def\nameoflatexe{LaTeX2e}
\expandafter\endgroup\ifx\nameoflatexe\fmtname\else
\csname fi\endcsname
%</internal>
%<*install>

\input docstrip.tex

\preamble

Copyright (C) 2019 
Gau, Syu ( GauSyu@Gmail.com )
 
----------------------------------------------------------------------
 
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. This version of this license is in
   http://www.latex-project.org/lppl/lppl-1-3c.txt
and the latest version of this license is in
   http://www.latex-project.org/lppl.txt
and version 1.3 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 
  Gau, Syu ( GauSyu@Gmail.com ).
 
----------------------------------------------------------------------

\endpreamble

\keepsilent

\generate{
  \usedir{tex/latex/secnum}
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
  \nopreamble\nopostamble
  \file{README.md}{\from{\jobname.dtx}{readme}}
}
\Msg{*************************************************************}
\Msg{*                                                           *}
\Msg{* To finish the installation you have to move the following *}
\Msg{* file into proper directories searched by TeX:             *}
\Msg{*                                                           *}
\Msg{* The recommended directory is TDS:tex/latex/secnum         *}
\Msg{*                                                           *}
\Msg{*     secnum.sty                                            *}
\Msg{*                                                           *}
\Msg{* To produce the documentation run the file secnum.dtx      *}
\Msg{* through XeLaTeX.                                          *}
\Msg{*                                                           *}
\Msg{* Happy TeXing!                                             *}
\Msg{*                                                           *}
\Msg{*************************************************************}
\endbatchfile
%</install>
%<*internal>
\fi
%</internal>
%<*driver>
\ProvidesFile{secnum.dtx}
\documentclass{l3doc}
\usepackage{indentfirst}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{secnum}
\setsecnum{A}
\ExplSyntaxOn
\cs_set_eq:NN \__codedoc_macro_end_style:n \use_none:n
\ExplSyntaxOff
\DeclareRobustCommand \opt {\texttt}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \GetFileInfo{\jobname.sty}
% \DoNotIndex{\cs}
%
% \title{ The \pkg{\jobname} package }
% \author{ Gau, Syu }
% \date{ \textit{Last Update:} \filedate }
%
% \maketitle
%
% \begin{abstract}
%   The package \pkg{\jobname} provides a marco \cs{setsecnum} 
%   which allows user to format section numbering intuitively.
% \end{abstract}
%
% \tableofcontents
% \begin{documentation}
% \section{Usage}
%   Before using the macro, load the package in preamble.
%  \begin{verbatim}
%  \usepackage{secnum}
%  \end{verbatim}
%   Then, one can format the section numbering by using the 
%   marco \cs{setsecnum} in preamble.
%  \begin{function}{\setsecnum}
%  \begin{syntax}
%    \cs{setsecnum} \meta{num format}
%  \end{syntax}
%    A typical \meta{num format} is like this:
%  \begin{center}
%    \verb|1.1.1|
%  \end{center}
%    It consists of some syntax abbrs of numbering formats, 
%    reffering the follows,
%  \begin{center}
%   \begin{tabular}{ccccc}
%    \toprule
%     A & a & I & i & 1 \\
%    \midrule
%     \cs{Alph} & \cs{alph} & \cs{Roman} & \cs{roman} & \cs{arabic} \\
%    \bottomrule
%   \end{tabular}
%  \end{center}
%   and some separators, which can be any character except 
%   the abbrs and special characters such as barces ``\verb|{}|'', 
%   comma ``\verb|,|'', space ``\verb*| |'', etc.
%  \end{function}
%
% \section{Process}
%   The process of the macro \cs{setsecnum} can be 
%   explained as follows.
% \begin{enumerate}[label=Step \arabic*.]
%   \item The main function eats the input, saying \opt{I.1.a}, 
%         and stores it in a token list.
%   \item Replace abbrs by macros.
%         In our example, it results 
%         ``\cs{Roman}\opt{.}\cs{arabic}\opt{.}\cs{alph}''
%   \item Split this token list into a sequence by macros. 
%         In our example, it results ``\cs{Roman}'', 
%         ``\opt{.}\cs{arabic}'' and ``\opt{.}\cs{alph}''.
%   \item Store those codes in indivial containers.
%   \item Use them to renew \cs{thesection}, \cs{thesubsection}, 
%         \cs{thesubsubsection} etc. 
%         provided there is no \cs{chapter}.
% \end{enumerate}
%   
% \end{documentation}
%
% \StopEventually{}
% 
% \begin{implementation}
% \section{Implementation}
%   The following is the implementation. 
%   Users can ignore.
% \subsection{Preparations}
%   This document class uses \LaTeX3. 
%   Therefore, the packages \pkg{expl3}, \pkg{xparse} 
%   and \pkg{l3keys2e} are needed and should use 
%   \cs{ProvidesExplClass} rather than \cs{ProvidesClass}.
%
%    \begin{macrocode}
%<*package>
%<@@=syu>
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage{secnum}{2020/01/01}{}
  { An intuitive way to format section numbering }
\RequirePackage{xparse}
%    \end{macrocode}
%
%  \begin{variable}{ \l__syu_secnum_tl, \l__syu_secnum_seq }
%   The two variables are used to store the formatting information.
%    \begin{macrocode}
\tl_new:N \l__syu_secnum_tl
\seq_new:N \l__syu_secnum_seq
%    \end{macrocode}
%  \end{variable}
%
%  \begin{variable}{ 
%    \g__syu_chapter_tl, 
%    \g__syu_section_tl,
%    \g__syu_subsection_tl,
%    \g__syu_subsubsection_tl,
%    \g__syu_paragraph_tl,
%    \g__syu_subparagraph_tl
%   }
%   The following variables are used to store 
%   the individal formatting codes.
%    \begin{macrocode}
\tl_new:N \g__syu_chapter_tl
\tl_new:N \g__syu_section_tl
\tl_new:N \g__syu_subsection_tl
\tl_new:N \g__syu_subsubsection_tl
\tl_new:N \g__syu_paragraph_tl
\tl_new:N \g__syu_subparagraph_tl
%    \end{macrocode}
%  \end{variable}
%
%  \begin{variable}{ \g__syu_if_thechapter_int }
%   This \meta{integer} encodes if \cs{thechapter} is defined.
%    \begin{macrocode}
\int_new:N \g__syu_if_thechapter_int
%    \end{macrocode}
%   If \cs{thechapter} is defined, it is $1$.
%    \begin{macrocode}
\if_cs_exist:N \thechapter
  \int_gset:Nn \g__syu_if_thechapter_int 1
%    \end{macrocode}
%   Otherwise, it is $0$.
%    \begin{macrocode}
\else:
  \int_gset:Nn \g__syu_if_thechapter_int 0
\fi:
%    \end{macrocode}
%  \end{variable}
%
% \subsection{Main function}
% 
%  \begin{macro}{\setsecnum}
%   Here is the definition of the main function \cs{setsecnum}.
%    \begin{macrocode}
\DeclareDocumentCommand{\setsecnum}{m}
  {
%    \end{macrocode}
%   Store the input in.
%    \begin{macrocode}
    \tl_set:Nn \l__syu_secnum_tl {#1}
%    \end{macrocode}
%   Replace syntax abbrs by corresponding macros.
%    \begin{macrocode}
    \syu_secnum_unabbr:N \l__syu_secnum_tl
%    \end{macrocode}
%   Split into a sequence by macros.
%    \begin{macrocode}
    \syu_split_by_macros:NN \l__syu_secnum_tl \l__syu_secnum_seq
%    \end{macrocode}
%   Read formatting information.
%    \begin{macrocode}
    \syu_secnum_from_seq:N \l__syu_secnum_seq
%    \end{macrocode}
%   Set the \opt{secnumdepth}.
%    \begin{macrocode}
    \setcounter{secnumdepth}{ \seq_count:N \l__syu_secnum_seq }
%    \end{macrocode}
%   Format numberings.
%    \begin{macrocode}
    \syu_secnum:
  }
%    \end{macrocode}
%  \end{macro}
%
% \subsection{Unabbravation}
%  \begin{variable}{ \syu_secnum_unabbr:N }
%   This function replace the abbrs in a \meta{tl var}  
%   by expansions.
%    \begin{macrocode}
\cs_new_protected:Npn \syu_secnum_unabbr:N #1
  {
    \regex_replace_all:nnN {A} {\c{Alph}} #1
    \regex_replace_all:nnN {a} {\c{alph}} #1
    \regex_replace_all:nnN {I} {\c{Roman}} #1
    \regex_replace_all:nnN {i} {\c{roman}} #1
    \regex_replace_all:nnN {1} {\c{arabic}} #1
  }
%    \end{macrocode}
%  \end{variable}
%
% \subsection{Split to sequence}
%  \begin{variable}{ \syu_split_by_macros:NN }
%   This function split a \meta{tl var} into a \meta{sequence} 
%   by macros.
%    \begin{macrocode}
\cs_new_protected:Npn \syu_split_by_macros:NN #1 #2
  {
    \tl_clear:N \l_tmpa_tl
    \seq_clear:N #2
    \tl_map_inline:Nn #1 
      {
        \tl_put_right:Nn \l_tmpa_tl ##1
        \__syu_if_macro:nT ##1 
          {
            \seq_put_right:NV #2 \l_tmpa_tl 
            \tl_clear:N \l_tmpa_tl
          }
      }
  }
%    \end{macrocode}
%  \end{variable}
%   But how to see if an \meta{item} in the token list is a macro?
%  \begin{variable}{ \g__syu_macro_tl }
%   This \meta{tl var} stores the first five characters 
%   of the meaning of any macro, 
%   i.e. \opt{macro} (watch out its catcode). 
%   The idea is to creat a \meta{tl var} and then set 
%   its value to be the first five characters of its meaning.
%    \begin{macrocode}
\tl_new:N \g__syu_macro_tl
\tl_set:Nx \g__syu_macro_tl { \meaning \g__syu_macro_tl }
\tl_gset:Nx \g__syu_macro_tl { \tl_range:Nnn \g__syu_macro_tl {1}{5} }
%    \end{macrocode}
%  \end{variable}
%  \begin{variable}{ \__syu_if_macro:nT, 
%  \__syu_if_macro:nF, \__syu_if_macro:nTF, }
%   Then, define a conditional testing if the input is a macro. 
%   Note that I use \cs{if_meaning} rather than \cs{tl_if_eq:NNTF}.
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \__syu_if_macro:n #1 { T , F , TF }
  {
    \group_begin:
      \tl_set:Nx \l_tmpa_tl {\meaning #1}
      \tl_set:Nx \l_tmpa_tl {\tl_range:Nnn \l_tmpa_tl {1} {5}}
%    \end{macrocode}
%   This is a trick to keep \cs{l_tmpa_tl} in the current local group
%    \begin{macrocode}
    \exp_after:wN
    \group_end:
%    \end{macrocode}
%   while throwing the comparison result out.
%    \begin{macrocode}
      \if_meaning:w \l_tmpa_tl \g__syu_macro_tl
        \prg_return_true:
      \else:
        \prg_return_false:
      \fi:
  }
%    \end{macrocode}
%  \end{variable}
%
% \subsection{Read formatting info}
%  \begin{variable}{ \syu_secnum_from_seq:N  }
%   Read the formatting info from given \meta{sequence}.
%    \begin{macrocode}
\cs_new_protected:Npn \syu_secnum_from_seq:N #1
  {
%    \end{macrocode}
%   Use \cs{tl_gset:Nx} since: 1, these data are global and 
%   2: I need them eating the fully expanded results.
%    \begin{macrocode}
    \tl_gset:Nx \g__syu_chapter_tl 
      { \seq_item:Nn #1 { \g__syu_if_thechapter_int } }
    \tl_gset:Nx \g__syu_section_tl 
      { \seq_item:Nn #1 { 1 + \g__syu_if_thechapter_int } }
    \tl_gset:Nx \g__syu_subsection_tl
      { \seq_item:Nn #1 { 2 + \g__syu_if_thechapter_int } }
    \tl_gset:Nx \g__syu_subsubsection_tl
      { \seq_item:Nn #1 { 3 + \g__syu_if_thechapter_int } }
    \tl_gset:Nx \g__syu_paragraph_tl
      { \seq_item:Nn #1 { 4 + \g__syu_if_thechapter_int } }
    \tl_gset:Nx \g__syu_subparagraph_tl
      { \seq_item:Nn #1 { 5 + \g__syu_if_thechapter_int } }
  }
%    \end{macrocode}
%  \end{variable}
%
% \subsection{Formatting}
%  \begin{variable}{ \syu_secnum:  }
%   Formatting section numbering.
%    \begin{macrocode}
\cs_new:Nn \syu_secnum: 
  {
%    \end{macrocode}
%   When \cs{thechapter} is defined, start from it.
%    \begin{macrocode}
    \if_cs_exist:N \thechapter
      \renewcommand*{\thechapter}
        { \g__syu_chapter_tl {chapter} }
      \renewcommand*{\thesection}
        { \thechapter
          \g__syu_section_tl {section} }
%    \end{macrocode}
%   Otherwise start from \cs{thesection}.
%    \begin{macrocode}
    \else:
      \renewcommand*{\thesection}
        { \g__syu_section_tl {section} }
    \fi:
%    \end{macrocode}
%   The rest levels.
%    \begin{macrocode}
      \renewcommand*{\thesubsection}
        { \thesection
          \g__syu_subsection_tl {subsection} }
      \renewcommand*{\thesubsubsection}
        { \thesubsection
          \g__syu_subsubsection_tl {subsubsection} }
      \renewcommand*{\theparagraph}
        { \thesubsubsection
          \g__syu_paragraph_tl {paragraph} }
      \renewcommand*{\thesubparagraph}
        { \theparagraph
          \g__syu_subparagraph_tl {subparagraph} }
  }
%    \end{macrocode}
%  \end{variable}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%\end{implementation}
%
%
% \changes{}{2020/01/01}{Correct the package name.}
%
% \Finale
\endinput