\ExplSyntaxOn

\newdimen\@AE@maxwidth
\newdimen\@AE@Lmargin
\newdimen\@AE@labelwidth
\newdimen\@AE@Lskip
\@AE@labelwidth=2\zw
\def\@AE@labelform#1{(\@arabic{#1})}
\def\@AE@makelabel#1{\hbox to \@AE@labelwidth{\hss (\@arabic{#1})}}
\def\@AE@itemsep{1\zw}

\bool_new:N \l__AE_noauto_bool
\bool_set_false:N \l__AE_noauto_bool
\int_new:N \l__AE_item_count_int
\int_new:N \l__AE_itemcnt_before_setup_int
\int_new:N \l__AE_boxcnt_noauto_int
\int_new:N \l__AE_boxcnt_int

\def\autoenumi{\@ifnextchar[\@autoenumi{\@autoenumi[]}}
\def\@autoenumi[#1]{%
  \def\@autoenumi@option{#1}%
  \if@noskipsec \leavevmode \fi
  \if@inlabel
    \box_set_eq:Nc \l_tmpa_box { box\@labels }%
    \if@newlist \global\@newlistfalse \fi
    \global\@inlabelfalse\everypar{}
    \dim_set_eq:NN \@AE@Lmargin \@totalleftmargin
  \else
    \box_clear:N \l_tmpa_box
    \dim_zero:N \@AE@Lmargin
  \fi
  \ifhmode \par \fi
  \int_gzero:N \l__AE_item_count_int
  \int_gzero:N \l__AE_boxcnt_int
  \int_gzero:N \l__AE_itemcnt_before_setup_int
  \int_gzero:N \l__AE_boxcnt_noauto_int
  \let\item\@AE@item
  \dim_gzero:N \@AE@maxwidth
  \dim_set_eq:NN \@AE@Lskip \leftskip
  \hbox_set:Nw \l_tmpb_box
}

\def\@AE@item{\@ifnextchar[{\@AE@enditem\@AE@startitem}{\@AE@enditem\@AE@startitem[auto]}}
\def\@AE@enditem{%
  \unskip\hbox_set_end:
  \bool_if:NTF \l__AE_noauto_bool {%
    \hbox_set:cn { l__AE_noauto_\int_use:N \l__AE_boxcnt_noauto_int _box } {%
      \int_compare:nNnT {\l__AE_item_count_int} = {1} { \box_use:N \l_tmpa_box }
      \@AE@makelabel\l__AE_item_count_int
      \hskip\@AE@itemsep
      \hbox_unpack_drop:c { l__AE_noauto_\int_use:N \l__AE_boxcnt_noauto_int _box }
    }%
    \@endautoenumi@setup
    \leftskip\dimexpr\@AE@Lskip + \@AE@labelwidth\relax
    \noindent\hskip-\@AE@labelwidth
    \hbox_unpack_drop:c { l__AE_noauto_\int_use:N \l__AE_boxcnt_noauto_int _box }\par
    \int_zero:N \l__AE_boxcnt_int
    \dim_set_eq:NN \leftskip \@AE@Lskip
    \dim_zero:N \@AE@maxwidth
    \bool_set_false:N \l__AE_noauto_bool
  }{%
    \int_compare:nNnT {\l__AE_item_count_int} > {0} {%
      \hbox_set:cn { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box } {%
        \int_compare:nNnT {\l__AE_item_count_int} = {1} { \box_use:N \l_tmpa_box }%
        \@AE@makelabel\l__AE_item_count_int\hskip\zw
        \hbox_unpack_drop:c { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box }
      }%
      \dim_compare:nNnT { \@AE@maxwidth } < {%
        \box_wd:c { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box } 
      }%
      {%
        \dim_set:Nn { \@AE@maxwidth } {%
          \box_wd:c { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box }
        }
      }%
    }
  }
}
\def\@AE@startitem[#1]{%
  \@ifundefined{@AE@startitem@#1}{\@AE@startitem@auto}{\csname @AE@startitem@#1\endcsname}%
}
\def\@AE@startitem@auto{%
  \int_incr:N \l__AE_item_count_int
  \int_incr:N \l__AE_boxcnt_int
  \int_incr:N \l__AE_itemcnt_before_setup_int
  \box_if_exist:cF { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box } {
    \box_new:c { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box }
  }
  \hbox_set:cw { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box } \ignorespaces
}
\def\@AE@startitem@noauto{%
  \int_incr:N \l__AE_item_count_int
  \int_incr:N \l__AE_boxcnt_noauto_int
  \bool_set_true:N \l__AE_noauto_bool
  \box_if_exist:cF { l__AE_noauto_\int_use:N \l__AE_boxcnt_noauto_int _box } {
    \box_new:c { l__AE_noauto_\int_use:N \l__AE_boxcnt_noauto_int _box }
  }
  \hbox_set:cw { l__AE_noauto_\int_use:N \l__AE_boxcnt_noauto_int _box } \ignorespaces
}

\def\endautoenumi{\@endautoenumi}
\def\@endautoenumi{\@AE@enditem\@endautoenumi@setup\@endpetrue}
\def\@endautoenumi@setup{%
  \exp_args:Nne \str_if_eq:nnTF { adjust } { \@autoenumi@option }
    { \@endautoenumi@setup@\@endautoenumi@columnadjust } { \@endautoenumi@setup@\relax }
}
\def\@endautoenumi@setup@#1{%
  \dim_set_eq:NN \@AE@Lskip \leftskip
  \dim_set:Nn \dimen@ {\@AE@itemsep}%
  \int_set:Nn \l_tmpa_int {%
    \fp_eval:n {%
      max( floor((\linewidth + \dimen@) / (\@AE@maxwidth + \dimen@)), 1)
    }
  }
  #1
  \dim_set:Nn \l_tmpa_dim { ( \linewidth + \dimen@ ) / \l_tmpa_int - \dimen@ }
  \int_zero:N \l__AE_boxcnt_int 
  \int_zero:N \l_tmpb_int 
  \box_clear:N \l_tmpb_box
  \int_while_do:nNnn {\l__AE_itemcnt_before_setup_int} > {0} {%
    \int_incr:N \l__AE_boxcnt_int
    \int_decr:N \l__AE_itemcnt_before_setup_int
    \box_if_exist:cT { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box } {
      \hbox_set_to_wd:Nnn \l_tmpa_box { \l_tmpa_dim } {%
        \box_use:c { l__AE_auto_\int_use:N \l__AE_boxcnt_int _box }\hss
      }
    }
    \hbox_set:Nn \l_tmpb_box {
      \hbox_unpack_drop:N \l_tmpb_box \box_use:N \l_tmpa_box \hskip\@AE@itemsep
    }
    \int_incr:N \l_tmpb_int
    \int_compare:nNnF {\l_tmpb_int} < {\l_tmpa_int} {
      \hbox_to_wd:nn {\hsize} {
        \hskip\dimexpr\@AE@Lmargin+\leftskip+\leftmargin\relax
        \hbox_unpack_drop:N \l_tmpb_box\hss
      }
      \int_zero:N \l_tmpb_int
    }%
  }%
  \int_compare:nNnT {\l_tmpb_int} > {0} {%
    \hbox_to_wd:nn {\hsize} {
      \hskip\dimexpr\@AE@Lmargin+\leftskip+\leftmargin\relax
      \hbox_unpack_drop:N \l_tmpb_box\hss}%
  }%
}

\int_new:N \l__AE_adjust_int
\def\@endautoenumi@columnadjust{
  \int_compare:nNnT { \l__AE_itemcnt_before_setup_int } > {0} {
    \int_set_eq:NN \l__AE_adjust_int \l_tmpa_int
    \int_set:Nn \l_tmpa_int {
      \fp_eval:n {%
        ceil(\l__AE_itemcnt_before_setup_int / ceil(\l__AE_itemcnt_before_setup_int / \l__AE_adjust_int))
      }
    }
  }
}

\ExplSyntaxOff