%%
%% This is file `conteq.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% conteq.dtx  (with options: `package')
%% conteq.dtx  (with options: `layouts')
%% ----------------------------------------------------------------
%% conteq --- continued equalities
%% E-mail: mail@joachim-breitner.de
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage
  {conteq}              % Package name
  {2013/05/23}          % Release date
  {0.1}                 % Release version
  {Various layouts for continued equalities} % Description










\RequirePackage{amsmath}
\RequirePackage{environ}

\msg_new:nnn
    {conteq}
    {empty}
    { \msg_warning_text:n{conteq}:~Empty~conteq~environment~\msg_line_context: }

\msg_new:nnn
    {conteq}
    {ignoreddata}
    { \msg_warning_text:n{conteq}:~Ignored~text~\msg_line_context: }

\msg_new:nnn
    {conteq}
    {undefinedlayout}
    { \msg_critical_text:n{conteq}:~Undefined~layout~``#1''~\msg_line_context: }

\tl_new:N \l_conteq_default_layout_tl
\tl_new:N \l_conteq_layout_tl
\tl_new:N \l_conteq_body_tl
\tl_new:N \l_conteq_lines_seq
\tl_new:N \l_conteq_cells_seq
\tl_new:N \l_conteq_head_tl
\tl_new:N \l_conteq_lastline_tl
\tl_new:N \l_conteq_rhs_tl
\tl_new:N \l_conteq_expl_tl
\tl_new:N \l_conteq_result_tl

\cs_new_protected:Nn \conteq_set_default_layout:n {
  \tl_set:Nn \l_conteq_default_layout_tl {#1}
}
\cs_new_eq:NN \ConteqSetDefaultLayout \conteq_set_default_layout:n
\cs_new_protected:Nn \conteq_define_layout:nnnnnnn {
  \cs_new_protected:cn {conteq_#1_begin:} { #2 }
  \cs_new_protected:cn {conteq_#1_lhs:n} { #3 }
  \cs_new_protected:cn {conteq_#1_rhs_only:n} { #4 }
  \cs_new_protected:cn {conteq_#1_rhs_expl:nn} { #5 }
  \cs_new_protected:cn {conteq_#1_nl:} { #6 }
  \cs_new_protected:cn {conteq_#1_end:} { #7 }
}
\cs_new_eq:NN \ConteqDefineLayout \conteq_define_layout:nnnnnnn
\cs_new:Npn \conteq_args_once:Nn #1#2 {
   \exp_not:o {#1 {#2}}
}
\cs_generate_variant:Nn \conteq_args_once:Nn {NV}

\cs_new:Npn \conteq_args_once:Nnn #1#2#3 {
   \exp_not:o {#1 {#2}{#3}}
}
\cs_generate_variant:Nn \conteq_args_once:Nnn {NVV}
\cs_new_protected:Npn \conteq_print_line:Nnn #1#2#3 {
  \seq_set_split:Nnn \l_conteq_cells_seq { & } {#2}
  \seq_pop_left:NN \l_conteq_cells_seq \l_conteq_rhs_tl
  \tl_clear:N \l_conteq_expl_tl
  \seq_if_empty:NF \l_conteq_cells_seq {
    \seq_pop_left:NN \l_conteq_cells_seq \l_conteq_expl_tl
  }
  \tl_if_blank:VTF \l_conteq_expl_tl {
    \tl_put_right:Nx #1 {
      \conteq_args_once:NV \conteq_rhs_only:n \l_conteq_rhs_tl
    }
  }{
    \tl_set:Nx \l_tmpa_tl {\exp_not:N \text {\exp_not:N \ConteqExplStyle {\exp_not:V \l_conteq_expl_tl}}}
    \tl_put_right:Nx #1 {
      \conteq_args_once:NVV \conteq_rhs_expl:nn \l_conteq_rhs_tl \l_tmpa_tl
    }
  }
  \seq_if_empty:NF \l_conteq_cells_seq {
    \msg_warning:nn{conteq}{ignoreddata}
  }
  \tl_put_right:Nn #1 {#3}
}
\cs_generate_variant:Nn \conteq_print_line:Nnn {Nno}
\cs_generate_variant:Nn \conteq_print_line:Nnn {NVn}

\NewEnviron{conteq}[1][\l_conteq_default_layout_tl]{
  \tl_set:NV \l_conteq_body_tl \BODY

  \tl_if_blank:oT \l_conteq_body_tl{
      \msg_warning:nn{conteq}{empty}
  }

  \tl_set:Nn \l_conteq_layout_tl {#1}
  \cs_if_exist:cTF {conteq_ \l_conteq_layout_tl _ begin:}{
    \clist_map_inline:nn{begin:, lhs:n,rhs_only:n, rhs_expl:nn, nl:, end:}{
      \cs_set_eq:cc {conteq_##1}{conteq_\l_conteq_layout_tl _##1}
    }
  }{
    \msg_critical:nnx{conteq}{undefined layout}{\l_conteq_layout_tl}
  }

  \seq_set_split:NnV \l_conteq_lines_seq { \\ } \l_conteq_body_tl

  \tl_clear:N \l_conteq_result_tl


  \int_case:nnn {\seq_count:N \l_conteq_lines_seq}
  {
    {1} {
      \tl_put_right:NV \l_conteq_result_tl \conteq_begin:
      \tl_put_right:NV \l_conteq_result_tl \l_conteq_body_tl
      \tl_put_right:NV \l_conteq_result_tl \conteq_end:
    }
  }{

    \seq_pop_left:NN \l_conteq_lines_seq \l_conteq_head_tl
    \seq_pop_right:NN \l_conteq_lines_seq \l_conteq_lastline_tl

    \tl_put_right:NV \l_conteq_result_tl \conteq_begin:
    \tl_put_right:Nx \l_conteq_result_tl {
      \conteq_args_once:NV \conteq_lhs:n \l_conteq_head_tl
    }
    \seq_map_inline:Nn \l_conteq_lines_seq {
      \conteq_print_line:Nno \l_conteq_result_tl {##1} {\conteq_nl:}
    }
    \conteq_print_line:NVn \l_conteq_result_tl \l_conteq_lastline_tl {}
    \tl_put_right:No \l_conteq_result_tl \conteq_end:
  }
  % Use this for debugging
  %\tl_show:N \l_conteq_result_tl
  \tl_use:N \l_conteq_result_tl
}










\ConteqDefineLayout
    {plain}
    {\begin{align*}}
    { #1 }
    { & #1 }
    { & #1 && #2 }
    { \\ }
    {\end{align*}}
\ConteqSetDefaultLayout{plain}

\ConteqDefineLayout
    {explline}
    {\begin{align*}}
    { #1 }
    { & #1 }
    { & #1 \\ & \mathrel{\phantom{=}} #2 }
    { \\ }
    {\end{align*}}

\ConteqDefineLayout
    {headline}
    {\begin{align*}}
    { &\mathrel{\phantom{=}} #1 \\ }
    { & #1 }
    { & #1 && #2 }
    { \\ }
    {\end{align*}}

\ConteqDefineLayout
    {onecolumn}
    {\begin{align*}}
    { &\mathrel{\phantom{=}} #1 \\ }
    { & #1 }
    { & #1 \\ & \mathrel{\phantom{=}} #2 }
    { \\ }
    {\end{align*}}

\ConteqDefineLayout
    {oneline}
    {\[}
    { #1 }
    { #1 }
    { #1 }
    {}
    {\]}

\newcommand{\ConteqExplStyle}[1]{\{~#1~\}}

%% 
%% Copyright (C) 2013 by Joachim Breitner <mail@joachim-breitner.de>
%% 
%% This work 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
%% Joachim Breitner.
%% 
%% This work consists of the file  conteq.dtx
%% and the derived files           conteq.ins,
%%                                 conteq.pdf and
%%                                 conteq.sty.
%% 
%%
%% End of file `conteq.sty'.
