\NeedsTeXFormat{LaTeX2e}[2020-10-01]
\RequirePackage{l3keys2e}
\ProvidesExplPackage
  {projlib-author}
  {2022/02/24} {}
  {Enhanced author information block}

\keys_define:nn { projlib-author }
  {
    , amsfashion  .bool_set:N = \l__projlib_author_amsfashion_bool
    , amsfashion  .initial:n  = { false }
    , ams-fashion .bool_set:N = \l__projlib_author_amsfashion_bool
    , unknown     .code:n     = {}
  }
\ProcessKeysOptions { projlib-author }

\cs_if_exist:NT \subjclass { \endinput }

\tl_new:N   \g__projlib_author_font_author_tl
\tl_new:N   \g__projlib_author_font_address_tl
\tl_new:N   \g__projlib_author_font_curraddr_tl
\tl_new:N   \g__projlib_author_font_email_tl

\tl_gset:Nn \g__projlib_author_font_author_tl      { \normalsize \normalfont \scshape }
\tl_gset:Nn \g__projlib_author_font_address_tl     { \small \normalfont \itshape }
\tl_gset:Nn \g__projlib_author_font_curraddr_tl    { \small \normalfont \itshape }
\tl_gset:Nn \g__projlib_author_font_email_tl       { \small \normalfont \ttfamily }

\dim_new:N  \g__projlib_author_length_innersep_dim
\dim_new:N  \g__projlib_author_length_sep_dim
\dim_new:N  \g__projlib_author_length_aftersep_dim

\dim_set:Nn \g__projlib_author_length_innersep_dim { 0.3 \baselineskip }
\dim_set:Nn \g__projlib_author_length_sep_dim      { 0.5 \baselineskip }
\dim_set:Nn \g__projlib_author_length_aftersep_dim { 0.3 \baselineskip }


\bool_new:N \g__projlib_author_affil_already_exist_bool

\NewCommandCopy \projlib_author_backup_author:w \author
\RenewDocumentCommand \author { m }
  {
    \hook_gput_code:nnn { cmd/@author/after } { projlib-author }
      {
        \phantom {A} \\[-\baselineskip]
        \skip_vertical:n { \g__projlib_author_length_sep_dim }
        { \g__projlib_author_font_author_tl #1 }
        \bool_set_false:N \g__projlib_author_affil_already_exist_bool
        \\ \phantom {}
      }
  }

\ProvideDocumentCommand \address { m }
  {
    \hook_gput_code:nnn { cmd/@author/after } { projlib-author }
      {
        \phantom {A} \\[-\baselineskip]
        \bool_if:NF \g__projlib_author_affil_already_exist_bool
          {
            \skip_vertical:n { \g__projlib_author_length_innersep_dim }
          }
        \bool_set_true:N \g__projlib_author_affil_already_exist_bool
        { \g__projlib_author_font_address_tl #1 }
        \\ \phantom {}
      }
  }

\ProvideDocumentCommand \curraddr { m }
  {
    \hook_gput_code:nnn { cmd/@author/after } { projlib-author }
      {
        \phantom {A} \\[-\baselineskip]
        \bool_if:NF \g__projlib_author_affil_already_exist_bool
          {
            \skip_vertical:n { \g__projlib_author_length_innersep_dim }
          }
        \bool_set_true:N \g__projlib_author_affil_already_exist_bool
        { \g__projlib_author_font_curraddr_tl #1 }
        \\ \phantom {}
      }
  }

\ProvideDocumentCommand \email { m }
  {
    \hook_gput_code:nnn { cmd/@author/after } { projlib-author }
      {
        \phantom {A} \\[-\baselineskip]
        \bool_if:NF \g__projlib_author_affil_already_exist_bool
          {
            \skip_vertical:n { \g__projlib_author_length_innersep_dim }
          }
        \bool_set_true:N \g__projlib_author_affil_already_exist_bool
        { \g__projlib_author_font_email_tl #1 }
        \\ \phantom {}
      }
  }

\RequirePackage { regexpatch }

\xpatchcmd { \@maketitle } { \begin{tabular}[t]{c} } {} {} { \FAIL }
\xpatchcmd { \@maketitle } { \end{tabular} } {} {} { \FAIL }

\tl_set:cn {@author} {}

\hook_gput_code:nnn { cmd/@maketitle/before } { projlib-author }
  {
    \hook_gput_code:nnn { cmd/@author/before } { projlib-author }
      {
        \skip_vertical:n { - \g__projlib_author_length_sep_dim }
      }
    \hook_gput_code:nnn { cmd/@author/after } { projlib-author }
      {
        \skip_vertical:n { - \baselineskip }
        \bool_if:NT \g__projlib_author_affil_already_exist_bool
          {
            \skip_vertical:n { \g__projlib_author_length_aftersep_dim }
          }
      }
  }

\bool_if:NT \l__projlib_author_amsfashion_bool
  {
    \RequirePackage{ projlib-language }

    \tl_new:N   \g_projlib_author_keywordsname_style_tl
    \tl_gset:Nn \g_projlib_author_keywordsname_style_tl {}

    \projlib_langauge_define_multilingual_text:Nn \keywordsname
      {
        EN = \textsc{Key~words~and~phrases}         ,
        FR = \textsc{Mots~et~phrases~clés}          ,
        DE = \textsc{Schlüsselwörter~und~Phrasen}   ,
        IT = \textsc{Parole~e~frasi~chiave}         ,
        PT = \textsc{Palavras~e~frases~chave}       ,
        BR = \textsc{Palavras~e~frases~chave}       ,
        ES = \textsc{Palabras~y~frases~clave}       ,
        CN = 关键词                                 ,
        TC = 關鍵詞                                 ,
        JP = キーワード                             ,
        RU = Ключевые~слова~и~фразы                 ,
      }

    \tl_new:N \g__projlib_author_keywords_tl
    \NewDocumentCommand \keywords { m }
      {
        \tl_gset:Nn \g__projlib_author_keywords_tl { #1 }
      }

    \tl_new:N \g__projlib_author_dedicatory_tl
    \NewDocumentCommand \dedicatory { m }
      {
        \tl_gset:Nn \g__projlib_author_dedicatory_tl { #1 }
      }

    \hook_gput_code:nnn { begindocument/before } { projlib-author }
      {
        \NewCommandCopy \projlib_author_backup_footnotemark: \footnotemark
        \tl_gset:Nn \footnotemark {}

        \NewCommandCopy \projlib_author_backup_thanks:w \thanks
        \cs_undefine:N \thanks

        % measure the width of "*"
        \hbox_set:Nn \l_tmpa_box { * }
        \dim_const:Nn \c__projlib_author_length_width_of_asterisk_dim { \box_wd:N \l_tmpa_box }

        \cs_new_nopar:Nn \projlib_author_pre_thanks:n
          {
            \projlib_author_backup_thanks:w
              {
                \skip_horizontal:n { - \c__projlib_author_length_width_of_asterisk_dim }
                #1
              }
          }

        \NewDocumentCommand \thanks { m }
          {
            \tl_if_blank:eF { #1 }
              {
                \hook_gput_code:nnn { cmd/@title/after } { projlib-author }
                  {
                    \projlib_author_pre_thanks:n { #1 }
                  }
              }
          }

        \NewCommandCopy \projlib_author_backup_my_author:w \author
        \RenewDocumentCommand \author { m }
          {
            \projlib_author_backup_my_author:w
              {
                \RenewCommandCopy \footnotemark \projlib_author_backup_footnotemark:
                \RenewCommandCopy \thanks \projlib_author_backup_thanks:w
                #1
                \cs_undefine:N \footnotemark
              }
          }
      }

    \NewCommandCopy \projlib_author_backup_abstract: \abstract
    \NewCommandCopy \projlib_author_backup_endabstract: \endabstract
    \cs_undefine:N \abstract
    \cs_undefine:N \endabstract

    \bool_new:N \g__projlib_author_already_maketitle_bool
    \bool_gset_false:N \g__projlib_author_already_maketitle_bool
    \bool_new:N \g__projlib_author_abstract_before_maketitle_bool
    \bool_gset_false:N \g__projlib_author_abstract_before_maketitle_bool

    \RequirePackage{ scontents }
    \newenvsc{abstract}[store-env=projlib-author-abstract,print-env=false]

    \hook_gput_code:nnn { env/abstract/after } { projlib-author }
      {
        \bool_if:NTF \g__projlib_author_already_maketitle_bool
          {
            \projlib_author_backup_abstract:
              \getstored{projlib-author-abstract}
            \projlib_author_backup_endabstract:
          }
          {
            \bool_gset_true:N \g__projlib_author_abstract_before_maketitle_bool
          }
      }

    \hook_gput_code:nnn { begindocument/before } { projlib-author }
      {
        \makeatletter
        \xpatchcmd { \@maketitle } { \@date }
          {
            \tl_if_blank:eTF { \@date } { \skip_vertical:n { -2 \baselineskip } } { \@date }
            \tl_if_blank:eF { \g__projlib_author_dedicatory_tl }
              {
                \tl_if_blank:eT { \@date } { \skip_vertical:n { 2 \baselineskip } }
                \par
                \skip_vertical:n { -\parskip }
                \skip_vertical:n { \baselineskip }
                \textit{ \g__projlib_author_dedicatory_tl }
                \tl_if_blank:eT { \@date }
                  {
                    \skip_vertical:n { -\parskip }
                    \skip_vertical:n { \dim_eval:n { -1.7 \baselineskip } }
                  }
              }
          } {} { \FAIL }
        \makeatother

        \hook_gput_code:nnn { cmd/maketitle/before } { projlib-author }
          {
            \tl_if_blank:eF { \g__projlib_author_keywords_tl }
              {
                \projlib_author_pre_thanks:n
                  {
                    \group_begin:
                      \g_projlib_author_keywordsname_style_tl
                      \keywordsname .
                    \group_end:
                    \c_space_tl
                    \g__projlib_author_keywords_tl
                  }
              }
          }

        \hook_gput_code:nnn { cmd/maketitle/after } { projlib-author }
          {
            \bool_if:NT \g__projlib_author_abstract_before_maketitle_bool
              {
                \projlib_author_backup_abstract:
                  \getstored{projlib-author-abstract}
                \projlib_author_backup_endabstract:
              }
            \cs_undefine:N \address
            \cs_undefine:N \curraddr
            \cs_undefine:N \email
            \cs_undefine:N \dedicatory
            \cs_undefine:N \keywords
            \bool_gset_true:N \g__projlib_author_already_maketitle_bool
          }
      }

      \tl_new:N \l_projlib_author_subjclassname_tl
      \tl_new:N \l_projlib_author_subjclass_tl
      \msg_new:nnn { projlib-author }
        { unknown-msc-edition }
        { Unknown~edition~(#1)~of~Mathematics~Subject~Classification;~using~'2020' }
      \NewDocumentCommand \subjclass { O{2020} m }
        {
          \tl_set:Nn \l_projlib_author_subjclass_tl { #2 }
          \tl_if_exist:cTF { subjclassname_#1 }
            {
              \tl_set:Nn \l_projlib_author_subjclassname_tl { \tl_use:c { subjclassname_#1 } }
            }
            {
              \msg_warning:nnn { projlib-author } { unknown-msc-edition } { #1 }
              \tl_set:Nn \l_projlib_author_subjclassname_tl { \tl_use:c { subjclassname_2020 } }
            }
          \projlib_author_pre_thanks:n
            {
              \textit{ \l_projlib_author_subjclassname_tl . }
              \enspace
              \l_projlib_author_subjclass_tl .
            }
        }
      \tl_const:cn { subjclassname_1991 } { \textup{1991}~Mathematics~Subject~Classification }
      \tl_const:cn { subjclassname_2000 } { \textup{2000}~Mathematics~Subject~Classification }
      \tl_const:cn { subjclassname_2010 } { \textup{2010}~Mathematics~Subject~Classification }
      \tl_const:cn { subjclassname_2020 } { \textup{2020}~Mathematics~Subject~Classification }
  }

\endinput
%%
%% End of file `projlib-author.sty'.
