\ExplSyntaxOn
\int_new:N \g__flmx_bookmark_int
\tl_new:N \l__flmx_heading_name_tl
\tl_new:N \l__flmx_heading_places_tl
\tl_new:N \g__flmx_heading_places_tl
\cs_generate_variant:Nn \tl_set:Nn { Ne }
\cs_generate_variant:Nn \tl_put_right:Nn { Ne }
\cs_new_protected:Npn \__flmx_heading_places:
{
\tl_clear:N \l__flmx_heading_places_tl
\flmxIfKey { anchor }
{
\tl_put_right:Ne \l__flmx_heading_places_tl
{ \exp_not:N \flmxTarget { \exp_not:v { l__flmx_key_anchor_tl } } }
}
{ }
\str_if_eq:eeT { \flmxKey { toc } } { true }
{
\flmxIfKey { anchor }
{ \tl_set:Ne \l__flmx_heading_name_tl { flmxbookmark:\flmxKey { anchor } } }
{
\int_gincr:N \g__flmx_bookmark_int
\tl_set:Ne \l__flmx_heading_name_tl
{ flmxbookmark.\int_use:N \g__flmx_bookmark_int }
}
\tl_put_right:Ne \l__flmx_heading_places_tl
{
\exp_not:N \pdfbookmark [ \flmxKey { level } ]
{ \flmxIfKey { plain } { \exp_not:v { l__flmx_key_plain_tl } } { } }
{ \exp_not:V \l__flmx_heading_name_tl }
}
}
\int_case:nn { \flmxKey { level } }
{
{ 0 }
{
\tl_put_right:Ne \l__flmx_heading_places_tl
{ \exp_not:N \markboth { \__flmx_heading_mark: } { } }
}
{ 1 }
{
\tl_put_right:Ne \l__flmx_heading_places_tl
{ \exp_not:N \markright { \__flmx_heading_mark: } }
}
}
}
\cs_new:Npn \__flmx_heading_mark:
{
\flmxIfKey { alt }
{ \exp_not:v { l__flmx_key_alt_tl } }
{ \flmxIfKey { plain } { \exp_not:v { l__flmx_key_plain_tl } } { } }
}
\cs_new_protected:Npn \__flmx_heading_level:nn #1#2
{
\__flmx_heading_places:
\tl_gset_eq:NN \g__flmx_heading_places_tl \l__flmx_heading_places_tl
\__flmx_heading_table:nn { \int_min:nn {#1} { 5 } } {#2}
}
\cs_new_protected:Npn \flmx@headingplace
{
\tl_use:N \g__flmx_heading_places_tl
\tl_gclear:N \g__flmx_heading_places_tl
}
\cs_new_protected:Npn \__flmx_heading_table:nn #1#2
{
\int_case:nn {#1}
{
{ 0 }
{
\par \addpenalty { -\@highpenalty }
\@startsection { flmxheading } { 0 } { \z@ }
{ -3.5ex \@plus -1ex \@minus -.2ex } { 2.3ex \@plus .2ex }
{ \normalfont \huge \bfseries } * { \flmx@headingplace #2 }
}
{ 1 }
{
\@startsection { flmxheading } { 1 } { \z@ }
{ -3.5ex \@plus -1ex \@minus -.2ex } { 2.3ex \@plus .2ex }
{ \normalfont \Large \bfseries } * { \flmx@headingplace #2 }
}
{ 2 }
{
\@startsection { flmxheading } { 2 } { \z@ }
{ -3.25ex \@plus -1ex \@minus -.2ex } { 1.5ex \@plus .2ex }
{ \normalfont \large \bfseries } * { \flmx@headingplace #2 }
}
{ 3 }
{
\@startsection { flmxheading } { 3 } { \z@ }
{ -3.25ex \@plus -1ex \@minus -.2ex } { 1.5ex \@plus .2ex }
{ \normalfont \normalsize \bfseries } * { \flmx@headingplace #2 }
}
{ 4 }
{
\@startsection { flmxheading } { 4 } { \z@ }
{ 3.25ex \@plus 1ex \@minus .2ex } { -1em }
{ \normalfont \normalsize \bfseries } * { \flmx@headingplace #2 }
}
{ 5 }
{
\@startsection { flmxheading } { 5 } { \parindent }
{ 3.25ex \@plus 1ex \@minus .2ex } { -1em }
{ \normalfont \normalsize \bfseries } * { \flmx@headingplace #2 }
}
}
}
\NewDocumentCommand \flmxHeadingLevel { m +m }
{ \__flmx_heading_level:nn {#1} {#2} }
\NewDocumentCommand \flmxHeading { O { } +m }
{
\group_begin:
\__flmx_set_keys:n {#1}
\__flmx_find:nn { heading } { kind, level }
\use:c { \l__flmx_found_tl } {#2}
\use:e
{
\group_end:
\exp_not:N \everypar { \exp_not:V \everypar }
\legacy_if:nTF { @afterindent }
{ \exp_not:N \@afterindenttrue }
{ \exp_not:N \@afterindentfalse }
\exp_not:N \cs_set:Npn \exp_not:N \@currentlabelname
{ \exp_not:V \@currentlabelname }
\legacy_if:nT { @noskipsec }
{
\exp_not:N \cs_set:Npn \exp_not:N \@svsechd
{ \exp_not:V \@svsechd }
}
}
}
\flmxDefineHeadingDefault { \flmxHeadingLevel { \flmxKey { level } } {#1} }
\NewDocumentCommand \flmxRuninHeading { O { } +m }
{ \__flmx_command:nnnn { runinheading } { kind, level } {#1} {#2} }
\flmxDefineRuninHeadingDefault
{
\flmxIfKey { plain }
{ \tl_set:Nv \@currentlabelname { l__flmx_key_plain_tl } }
{ }
\__flmx_heading_places: \tl_use:N \l__flmx_heading_places_tl #1
}
\ExplSyntaxOff