% MIT License
%
% Copyright (c) 2019-2023 Magnus Lie Hetland
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.
%
\def \pseudoversion {1.2.2}
\def \pseudodate    {2023/01/30}
\RequirePackage{expl3}
\ProvidesExplPackage
    {pseudo}
    {\pseudodate}
    {\pseudoversion}
    {Straightforward pseudocode}
\RequirePackage{xparse}
\RequirePackage{pgfkeys}
\RequirePackage{array, xcolor, colortbl}
\RequirePackage{l3keys2e}
\RequirePackage{aliascnt}
\bool_new:N \c__pseudo_beamer_bool
\@ifclassloaded{beamer}
    {\bool_set_true:N  \c__pseudo_beamer_bool}
    {\bool_set_false:N \c__pseudo_beamer_bool}
\newcounter{pseudoenv}
\newcounter{pseudoline}[pseudoenv]
\int_new:N \g__pseudo_last_saved_line_int
\dim_new:N  \pseudoindentlength
\int_new:N  \g__pseudo_indent_level_int
\int_new:N  \l__pseudo_initial_indent_level_int
\bool_new:N \l__pseudo_in_eol_bool
\cs_generate_variant:Nn \quark_if_no_value:nTF    { VTF    }
\cs_generate_variant:Nn \tl_if_novalue:nTF        { VTF    }
\cs_generate_variant:Nn \tl_set:Nn                { Ne     }
\exp_args_generate:n                              { NNVNNV }
\cs_new:Npn \__pseudo_undef_col:n #1 {
    \tl_set_eq:cN { NC@find@ \token_to_str:N #1 } \scan_stop:
}
\cs_new:Npn \__pseudo_def_col:nn #1 #2 {
    \__pseudo_undef_col:n { #1 }
    \newcolumntype  { #1 } { #2 }
}
\cs_new:Npn \__pseudo_meta_new_cmd:NNnn #1 #2 #3 #4 {
    \tl_set:Nn \l_tmpa_tl {pseudo \cs_to_str:N #2}
    \exp_args:Nc
        #1 \l_tmpa_tl #3 {#4}
    \cs_if_free:NT #2 {\cs_gset_eq:Nc #2 \l_tmpa_tl}
}
\cs_new:Npn \__pseudo_new_cmd:Nnn #1 #2 #3 {
    \__pseudo_meta_new_cmd:NNnn
    \NewDocumentCommand #1 {{#2}} {
        #3
    }
}
\cs_new:Npn \__pseudo_new_ecmd:Nnn #1 #2 #3 {
    \__pseudo_meta_new_cmd:NNnn
    \def #1 {#2} {
        #3
    }
}
\cs_new:Npn \__pseudo_new_dec:nn #1 #2 {
    \tl_set:Nn \l_tmpa_tl { DeclarePseudo #1 }
    \exp_args:Nc
    \DeclareDocumentCommand \l_tmpa_tl { mm } {
        \DeclareDocumentCommand ##1 { } {
            \use:c { #2 } { ##2 }
        }
    }
}
\cs_new:Npn \__pseudo_per_char:nnn #1 #2 #3 {
    \peek_charcode_remove:NTF { #1 } {
        #2
        \__pseudo_per_char:nnn{#1}{#2}{#3}
    } {
        #3
    }
}
\cs_new:Npn \__pseudo_set_indent_length: {
    \quark_if_no_value:VTF \l__pseudo_indent_length_tl {
        \hbox_set:Nn \l_tmpa_box { \l__pseudo_indent_text_tl }
        \dim_set:Nn \pseudoindentlength { \box_wd:N \l_tmpa_box }
    } {
        \dim_set:Nn \pseudoindentlength \l__pseudo_indent_length_tl
    }
}
\cs_new:Npn \__pseudo_indent:N #1 {
    \tl_if_novalue:VTF \l__pseudo_indent_mark_tl {
        \skip_horizontal:n{ \pseudoindentlength * #1 }
    } {
        \group_begin:
        \color{\l__pseudo_indent_color_tl}
        \int_step_inline:nn { \g__pseudo_indent_level_int } {
            \l__pseudo_indent_mark_tl
            \skip_horizontal:n{ \pseudoindentlength }
        }
        \group_end:
    }
    \ignorespaces
}
\cs_new:Npn \__pseudo_drop_ctr:n #1 {
    \cs_undefine:c { c@ #1 }
}
\cs_new:Npn \__pseudo_copy_ctr:nn #1 #2 {
    \__pseudo_drop_ctr:n { #1 }
    \newaliascnt   { #1 } { #2 }
}
\cs_new:Npn \__pseudo_star_setup: {
    \cs_if_exist:cT { c@ * } {
        \__pseudo_copy_ctr:nn { __pseudo_orig_* } { * }
    }
    \__pseudo_copy_ctr:nn { * } { pseudoline }
    \group_insert_after:N \__pseudo_star_reset:
}
\cs_new:Npn \__pseudo_star_reset: {
    \cs_if_exist:cT { c@ __pseudo_orig_* } {
        \__pseudo_copy_ctr:nn { * } { __pseudo_orig_* }
        \cs_undefine:c { c@ __pseudo_orig_* }
    }
}
\cs_new:Npn \__pseudo_save_label: {
    \int_set:Nn \l_tmpa_int {\arabic{pseudoline}}
    \int_compare:nF {\l_tmpa_int = \g__pseudo_last_saved_line_int} {
        \addtocounter{pseudoline}{-1}
        \refstepcounter{pseudoline}
        \int_gset_eq:NN \g__pseudo_last_saved_line_int \l_tmpa_int
    }
}
\DeclareDocumentCommand \pseudosavelabel { } {
    \__pseudo_save_label:
}
\cs_new:Npn \__pseudo_cs_gsave_as:NN #1 #2 {
    \cs_gset_eq:NN #2 #1
    \group_insert_after:N \cs_gset_eq:NN
    \group_insert_after:N #1
    \group_insert_after:N #2
}
\cs_new:Npn \__pseudo_skip_pars:n #1 {
    \peek_meaning_remove_ignore_spaces:NTF \par {
        \__pseudo_skip_pars:n { #1 }
    } {
        #1
    }
}
\__pseudo_new_cmd:Nnn \nf {   } { \normalfont          }
\__pseudo_new_cmd:Nnn \tn { m } { \textnormal { #1 }   }
\__pseudo_new_cmd:Nnn \kw { m } { \textnormal {\kwfont { #1 } } }
\__pseudo_new_cmd:Nnn \cn { m } { \textnormal {\cnfont { #1 } } }
\__pseudo_new_cmd:Nnn \id { m } { \textnormal {\idfont { #1 } } }
\cs_new:Npn \__pseudo_b_or_bx: {
    \group_begin:
    \cs_if_exist:NT \@defaultsubs {
        \__pseudo_cs_gsave_as:NN \@defaultsubs \__pseudo_defaultsubs
        \cs_gset_eq:NN \@defaultsubs \relax
    }
    \cs_gset:Nn \__pseudo_b_or_bx: { \fontseries{b}\selectfont }
    \__pseudo_b_or_bx:
    \cs_if_exist:NT \@defaultsubs {
        \cs_gset_eq:NN \__pseudo_b_or_bx: \bfseries
    }
    \group_end:
    \__pseudo_b_or_bx:
}
\cs_new:Npn \__pseudo_fmt_pr:n #1 {
    \textnormal{\prfont{ #1 }}
}
\NewDocumentCommand \__pseudo_parse_paren_args { +d() } {
    \IfNoValueF { #1 } {
        \ensuremath{ ( #1 ) }
    }
}
\NewDocumentCommand \__pseudo_parse_bracket_or_paren_args { +o } {
    \IfNoValueTF { #1 } {
        \__pseudo_parse_paren_args
    } {
        \ensuremath{ [ #1 ] }
    }
}
\__pseudo_new_cmd:Nnn \pr { m } {
    \__pseudo_fmt_pr:n { #1 }
    \peek_catcode:NTF { ~ } { } {
        \__pseudo_parse_paren_args
    }
}
\cs_new:Npn \__pseudo_fmt_fn:n #1 {
    \textnormal{\fnfont{ #1 }}
}
\__pseudo_new_cmd:Nnn \fn { m } {
    \__pseudo_fmt_fn:n { #1 }
    \peek_catcode:NTF { ~ } { } {
        \__pseudo_parse_bracket_or_paren_args
    }
}
\__pseudo_new_ecmd:Nnn \hd { #1 ( #2 ) } {
    \multicolumn{2}
        {\pseudohdpreamble}
        {\__pseudo_fmt_pr:n{#1}\ensuremath{(#2)}}
        \tl_set_eq:NN \l__pseudo_extra_space_tl \l__pseudo_hd_space_tl
}
\__pseudo_new_cmd:Nnn \st { +m } {
    \textnormal {
    \l__pseudo_st_left_tl {\stfont{#1}} \l__pseudo_st_right_tl }
}
\__pseudo_new_cmd:Nnn \ct { +m } {
    \textnormal {
    \l__pseudo_ct_left_tl {\ctfont{#1}} \l__pseudo_ct_right_tl }
}
\def \pseudohl {
    \rowcolor{\pseudohlcolor}
}
\__pseudo_new_dec:nn { Comment    } { ct }
\__pseudo_new_dec:nn { Constant   } { cn }
\__pseudo_new_dec:nn { Function   } { fn }
\__pseudo_new_dec:nn { Identifier } { id }
\__pseudo_new_dec:nn { Keyword    } { kw }
\__pseudo_new_dec:nn { Normal     } { tn }
\__pseudo_new_dec:nn { Procedure  } { pr }
\__pseudo_new_dec:nn { String     } { st }
\NewDocumentCommand \eqs { } {
    \group_begin:
    \muskip_set:Nn \l_tmpa_muskip \l__pseudo_eqs_pad_tl
    \muskip_set:Nn \l_tmpb_muskip \l__pseudo_eqs_sep_tl
    \hbox_set:Nn   \l_tmpa_box {\(=\)}
    \box_scale:Nnn \l_tmpa_box {\l__pseudo_eqs_scale_fp}{1}
    \mathrel{
        \tex_mskip:D     \l_tmpa_muskip
        \box_use:N       \l_tmpa_box
        \tex_mskip:D     \l_tmpb_muskip
        \box             \l_tmpa_box
        \tex_mskip:D     \l_tmpa_muskip
    }
    \group_end:
}
\cs_gset_eq:NN \c__pseudo_orig_eq_cs \=
\DeclareDocumentCommand \= { m } {
    \tl_if_eq:nnTF { #1 } { = } {
        \eqs
    } {
        \c__pseudo_orig_eq_cs{#1}
    }
}
\cs_gset_eq:NN \__pseudo_eq: \=
\NewDocumentCommand \dts { } {
    \nolinebreak
    \mathinner {
        \ldotp
        \ldotp
    }
    \nolinebreak
}
\cs_gset_eq:NN \c__pseudo_dot_cs \.
\DeclareDocumentCommand \. { m } {
    \tl_if_eq:nnTF { #1 } { . } {
        \dts
    } {
        \c__pseudo_dot_cs { #1 }
    }
}
\NewDocumentCommand \rng { } {
    \nolinebreak
    \mathinner { : }
    \nolinebreak
}
\keys_define:nn { pseudo/package } {
    kw              .bool_gset:N    = \g__pseudo_kw_bool,
    kw              .default:n      = true
}
\ProcessKeysOptions{ pseudo/package }
\keys_define:nn { pseudo } {
    init            .tl_set:N       = \l__pseudo_init_tl,
    init-append     .code:n         = {
        \tl_put_right:Nn \l__pseudo_init_tl {#1}
    },
    init-prepend    .code:n         = {
        \tl_put_left:Nn \l__pseudo_init_tl {#1}
    },
    font            .tl_set:N       = \pseudofont,
    font            .initial:n      = \normalfont,
    hpad            .meta:n         = {
        hpad-val = { #1 },
        hl-warn = false,
    },
    hpad            .default:n      = 0.3em,
    hpad-val        .tl_set:N       = \l__pseudo_hpad_tl,
    hpad-val        .initial:n      = 0.0em,
    hsep            .tl_set:N       = \l__pseudo_hsep_tl,
    hsep            .initial:n      = .75em,
    left-margin     .tl_set:N       = \l__pseudo_left_margin_tl,
    left-margin     .initial:n      = 0pt,
    label           .tl_set:N       = \l__pseudo_label_tl,
    label           .initial:n      = \arabic*,
    label-align     .code:n         =
        \__pseudo_def_col:nn{ \pseudolabelalign }{#1},
    label-align     .initial:n      = r,
    ref             .tl_set:N       = \thepseudoline,
    ref             .default:n      = \l__pseudo_label_tl,
    indent-length   .tl_set:N       = \l__pseudo_indent_length_tl,
    indent-length   .initial:V      = \q_no_value,
    indent-text     .tl_set:N       = \l__pseudo_indent_text_tl,
    indent-text     .initial:n      = { \pseudofont\kw{else}\ },
    indent-mark     .tl_set:N       = \l__pseudo_indent_mark_tl,
    indent-mark     .initial:x      = \c_novalue_tl,
    indent-mark     .default:n      =
        \skip_horizontal:n { \l__pseudo_indent_mark_shift_tl }
        \tex_vrule:D width \l__pseudo_indent_mark_wd_tl
        \skip_horizontal:n {
            -\l__pseudo_indent_mark_wd_tl
            -\l__pseudo_indent_mark_shift_tl
        },
    indent-mark-width .tl_set:N     = \l__pseudo_indent_mark_wd_tl,
    indent-mark-width .initial:n    = \c__pseudo_semithick_dim,
    indent-mark-shift .tl_set:N     = \l__pseudo_indent_mark_shift_tl,
    indent-mark-shift .initial:n    = 0pt,
    indent-mark-shift .default:n    = .5em,
    indent-mark-color .tl_set:N     = \l__pseudo_indent_color_tl,
    indent-mark-color .initial:n    = lightgray,
    indent-level    .int_set:N      = \l__pseudo_initial_indent_level_int,
    kwfont          .tl_set:N       = \kwfont,
    kwfont          .initial:n      = \__pseudo_b_or_bx:,
    kw              .meta:n         = { font = \kwfont },
    kw              .value_forbidden:n = true,
    hl-warn         .bool_set:N     = \l__pseudo_hl_warn_bool,
    hl-warn         .initial:n      = true,
    hl-warn         .default:n      = true,
    hl-warn-code    .code:n         = {
        \bool_if:nT \l__pseudo_hl_warn_bool {
            \msg_warning:nn { pseudo } { hl-without-hpad }
        }
    },
    hl              .meta:n         = {
        hl-warn-code,
        bol-prepend = \pseudohl
    },
    hl              .value_forbidden:n = true,
    bol             .tl_set:N       = \l__pseudo_bol_tl,
    bol-append      .code:n         = {
        \tl_put_right:Nn \l__pseudo_bol_tl {#1}
    },
    bol-prepend     .code:n         = {
        \tl_put_left:Nn \l__pseudo_bol_tl {#1}
    },
    eol             .tl_set:N       = \l__pseudo_eol_tl,
    eol-append      .code:n         = {
        \tl_put_right:Nn \l__pseudo_eol_tl {#1}
    },
    eol-prepend     .code:n         = {
        \tl_put_left:Nn \l__pseudo_eol_tl {#1}
    },
    pause           .meta:n         = ,
    pause           .value_forbidden:n = true,
    cnfont          .tl_set:N       = \cnfont,
    cnfont          .initial:n      = \textsc,
    idfont          .tl_set:N       = \idfont,
    idfont          .initial:n      = \textit,
    stfont          .tl_set:N       = \stfont,
    stfont          .initial:n      = \textnormal,
    st-left         .tl_set:N       = \l__pseudo_st_left_tl,
    st-left         .initial:n      = ``,
    st-right        .tl_set:N       = \l__pseudo_st_right_tl,
    st-right        .initial:n      = '',
    prfont          .tl_set:N       = \prfont,
    prfont          .initial:n      = \cnfont,
    fnfont          .tl_set:N       = \fnfont,
    fnfont          .initial:n      = \idfont,
    ctfont          .tl_set:N       = \ctfont,
    ctfont          .initial:n      = \textit,
    ct-left         .tl_set:N       = \l__pseudo_ct_left_tl,
    ct-left         .initial:n      = (,
    ct-right        .tl_set:N       = \l__pseudo_ct_right_tl,
    ct-right        .initial:n      = ),
    hl-color        .tl_set:N       = \pseudohlcolor,
    hl-color        .initial:n      = black!12,
    dim-color       .tl_set:N       = \pseudodimcolor,
    dim-color       .initial:n      = \pseudohlcolor,
    dim             .meta:n         = {
        bol-append   = \color{\pseudodimcolor},
        setup-append = \color{\pseudodimcolor}
    },
    line-height     .tl_set:N       = \l__pseudo_line_height_tl,
    line-height     .initial:n      = 1,
    extra-space     .tl_set:N       = \l__pseudo_extra_space_tl,
    extra-space     .initial:n      = 0pt,
    hd-space        .tl_set:N       = \l__pseudo_hd_space_tl,
    hd-space        .initial:n      = 0pt,
    hd-space        .default:n      = 0.41386ex,
    start           .tl_set:N       = \l__pseudo_start_tl,
    start           .initial:n      = 1,
    preamble        .code:n         =
        \__pseudo_def_col:nn{ \pseudopreamble }{#1},
    preamble        .initial:n      = {
        >{ \pseudohpad }
        \pseudolabelalign
        >{ \pseudosetup }
        l
        <{ \pseudohpad }
    },
    setup           .tl_set:N       = \l__pseudo_setup_tl,
    setup           .initial:n      = {
        \pseudoindent \pseudofont \pseudosavelabel
    },
    setup-append    .code:n         = {
        \tl_put_right:Nn \l__pseudo_setup_tl {#1}
    },
    setup-prepend   .code:n         = {
        \tl_put_left:Nn \l__pseudo_setup_tl {#1}
    },
    hd-preamble     .code:n         =
        \__pseudo_def_col:nn{ \pseudohdpreamble }{#1},
    hd-preamble     .initial:n      = {
        >{ \pseudohpad } l <{ \pseudohpad }
    },
    prefix          .tl_set:N       = \pseudoprefix,
    prefix          .initial:n      = {
        \pseudobol \stepcounter* \pseudolabel &
    },
    pos             .tl_set:N       = \pseudopos,
    pos             .initial:n      = t,
    prevdepth       .tl_set:N       = \l__pseudo_prevdepth_tl,
    prevdepth       .initial:n      = .3 \baselineskip,
    begin-tabular   .tl_set:N       = \l__pseudo_begin_tabular_tl,
    begin-tabular   .initial:n      =
        \begin{tabular}[\pseudopos]{\pseudopreamble},
    end-tabular     .tl_set:N       = \l__pseudo_end_tabular_tl,
    end-tabular     .initial:n      = \end{tabular},
    topsep          .tl_set:N       = \l__pseudo_topsep_tl,
    topsep          .initial:n      = { \topsep },
    partopsep       .tl_set:N       = \l__pseudo_partopsep_tl,
    partopsep       .initial:n      = { \partopsep },
    compact         .meta:n         = {
        compact-val  = { #1 },
        compact-def  = true,
        compact-code = { #1 },
    },
    compact         .default:n      = true,
    compact-val     .bool_set:N     = \l__pseudo_compact_bool,
    compact-def     .bool_set:N     = \l__pseudo_compact_def_bool,
    compact-code    .code:n         = {
        \bool_if:nT { \l__pseudo_compact_bool } {
            \tl_clear:N \pseudopos
        }
    },
    eqs-scale       .fp_set:N       = \l__pseudo_eqs_scale_fp,
    eqs-scale       .initial:n      = 0.6785,
    eqs-sep         .tl_set:N       = \l__pseudo_eqs_sep_tl,
    eqs-sep         .initial:n      = 0.63mu,
    eqs-pad         .tl_set:N       = \l__pseudo_eqs_pad_tl,
    eqs-pad         .initial:n      = 0.28mu,
}
\bool_if:NT \g__pseudo_kw_bool {
    \keys_set:nn { pseudo } { kw }
}
\bool_if:NT \c__pseudo_beamer_bool {
    \keys_define:nn { pseudo } {
        pause .meta:n = { eol-append = \pause }
    }
}
\cs_new:Npn \__pseudo_keys_set_overlay:nnn #1 #2 #3 {
    \bool_if:NT \c__pseudo_beamer_bool {
        \tl_if_novalue:nF { #1 } {
            \only<#1>{ \keys_set:nn { #2 } { #3 } }
        }
    }
}
\msg_new:nnn { pseudo } { unknown-key } {
    Unknown~key~'#1'~ignored.
}
\tl_new:N \l__pseudo_overlay_tl
\keys_define:nn { pseudo } {
    unknown .code:n = {
        \group_begin:
        \int_zero:N \l_tmpa_int
        \int_zero:N \l_tmpb_int
        \tl_clear:N \l_tmpa_tl
        \tl_clear:N \l_tmpb_tl
        \tl_map_inline:Nn \l_keys_key_tl {
            \tl_if_eq:nnTF { ##1 } { < } {
                \int_incr:N \l_tmpa_int
                \int_compare:nF { \l_tmpb_int == 0 } {
                    \int_incr:N \l_tmpa_int
                }
                \tl_set_eq:NN \l_tmpb_tl \l_tmpa_tl
                \tl_clear:N \l_tmpa_tl
            } {
            \tl_if_eq:nnTF { ##1 } { > } {
                \int_incr:N \l_tmpb_int
                \tl_set_eq:NN \l__pseudo_overlay_tl \l_tmpa_tl
                \tl_clear:N \l_tmpa_tl
            } {
                \tl_put_right:Nn \l_tmpa_tl { ##1 }
            } }
        }
        \bool_if:nTF {
            \int_compare_p:n { \l_tmpa_int == \l_tmpb_int == 1 }
            &&
            \tl_if_empty_p:N \l_tmpa_tl
        } {
            \tl_if_blank:nF{ #1 } {
                \tl_put_right:Nn \l_tmpb_tl {= #1}
            }
            \tl_set:Nn \l_tmpa_tl {
                \__pseudo_keys_set_overlay:nnn
            }
            \tl_put_right:Nx \l_tmpa_tl { { \l__pseudo_overlay_tl } }
            \tl_put_right:Nn \l_tmpa_tl { { pseudo           } }
            \tl_put_right:Nx \l_tmpa_tl { { \l_tmpb_tl       } }
        } {
            \tl_clear:N \l_tmpa_tl
            \bool_if:nTF {
                \bool_lazy_and_p:nn
                    { \l__pseudo_in_eol_bool       }
                    { \tl_if_blank_p:n { #1 } }
            } {
                \tl_set_rescan:Nno
                    \l__pseudo_extra_space_tl { }
                    { \l_keys_key_str  }
            } {
                \msg_error:nnx
                    { pseudo } { unknown-key }
                    { \l_keys_path_str }
            }
        }
        \exp_args:NNNVNNV
            \group_end:
            \tl_set:Nn \l__pseudo_extra_space_tl \l__pseudo_extra_space_tl
            \tl_set:Nn \l_tmpa_tl           \l_tmpa_tl
        \l_tmpa_tl
    }
}
\cs_new:Npn \__pseudo_set:n #1 { \keys_set:nn { pseudo } { #1 } }
\cs_new:Npn \__pseudo_inc_indent: {
    \int_gincr:N \g__pseudo_indent_level_int
}
\cs_new:Npn \__pseudo_dec_indent: {
    \int_compare:nNnT \g__pseudo_indent_level_int > 0 {
        \int_gdecr:N \g__pseudo_indent_level_int
    }
}
\cs_new:Npn \__pseudo_eol_handle_args:nnn #1 #2 #3 {
    \bool_set_true:N \l__pseudo_in_eol_bool
    \__pseudo_keys_set_overlay:nnn { #2 } { pseudo } { hl }
    \keys_set:nn { pseudo } { #3 }
    \tl_gset_eq:NN \pseudolabel   \l__pseudo_label_tl
    \tl_gset_eq:NN \pseudobol     \l__pseudo_bol_tl
    \tl_gset_eq:NN \pseudoeol     \l__pseudo_eol_tl
    \tl_gset_eq:NN \pseudosetup   \l__pseudo_setup_tl
    \IfBooleanTF { #1 } {
        \tl_gclear:N \g__pseudo_cur_prefix_tl
    } {
        \tl_gset_eq:NN \g__pseudo_cur_prefix_tl \pseudoprefix
    }
}
\NewDocumentCommand \__pseudo_eol_tail { s d<> +O{ } } {
    \__pseudo_eol_handle_args:nnn{#1}{#2}{#3}
    \dim_compare:nNnTF \l__pseudo_extra_space_tl = { 0pt } {
        \tl_set_eq:NN \l_tmpa_tl \tabularnewline
    } {
        \tl_set:Nx \l_tmpa_tl {
            \exp_not:N \tabularnewline [ \l__pseudo_extra_space_tl ]
        }
    }
    \__pseudo_skip_pars:n {
        \peek_meaning_ignore_spaces:NTF \end {
            \l_tmpa_tl
        } {
            \pseudoeol
            \l_tmpa_tl
            \__pseudo_bol:
        }
    }
}
\cs_new:Npn \__pseudo_eol: {
    \__pseudo_per_char:nnn { + } {
        \__pseudo_inc_indent:
    } {
    \__pseudo_per_char:nnn { - } {
        \__pseudo_dec_indent:
    } {
        \__pseudo_eol_tail
    } }
}
\cs_new:Npn \__pseudo_bol: {
    \g__pseudo_cur_prefix_tl
}
\NewDocumentCommand \pseudohpad { } {
    \skip_horizontal:n { \l__pseudo_hpad_tl - \tabcolsep }
}
\NewDocumentCommand \pseudoindent { } {
    \__pseudo_indent:N { \g__pseudo_indent_level_int }
}
\NewDocumentCommand \RestorePseudoBackslash { } {
    \cs_gset_eq:NN \\ \__pseudo_eol:
}
\NewDocumentCommand \RestorePseudoEq { } {
    \cs_gset_eq:NN \= \__pseudo_eq:
}
\NewDocumentCommand \pseudoset { +m }
    { \__pseudo_set:n { #1 } }
\NewDocumentCommand \pseudodefinestyle { m +m } {
    \keys_define:nn { pseudo } {
        #1 .meta:n = {
            #2
        }
    }
}
\NewDocumentEnvironment { pseudo } { +o s d<> +O{ } } {
    \group_begin:
    \__pseudo_cs_gsave_as:NN \\ \c__pseudo_saved_cr_cs
    \__pseudo_cs_gsave_as:NN \= \c__pseudo_saved_eq_cs
    \RestorePseudoEq
    \int_set:Nn \g__pseudo_last_saved_line_int {\arabic{pseudoline}}
    \__pseudo_star_setup:
    \IfNoValueF { #1 } {
        \pseudoset { #1 }
    }
    \__pseudo_set_indent_length:
    \bool_if:NF \l__pseudo_compact_def_bool {
        \bool_set:Nn \l__pseudo_compact_bool {
            \mode_if_horizontal_p: && \mode_if_inner_p:
        }
    }
    \bool_if:nF { \l__pseudo_compact_bool } {
        \skip_set:Nn \l_tmpa_skip {
            \l__pseudo_topsep_tl
        }
        \mode_if_vertical:TF {
            \skip_add:Nn \l_tmpa_skip { \l__pseudo_partopsep_tl }
        } {
            \unskip \par
        }
        \addvspace { \l_tmpa_skip }
        \noindent
        \skip_horizontal:n{ \dim_eval:n { \l__pseudo_left_margin_tl } }
    }
    \dim_set:Nn   \tabcolsep    { \l__pseudo_hsep_tl / 2 }
    \tl_set_eq:NN \arraystretch \l__pseudo_line_height_tl
    \stepcounter{pseudoenv}
    \setcounter{pseudoline}{\l__pseudo_start_tl}
    \addtocounter{pseudoline}{-1}
    \tl_use:N \l__pseudo_init_tl
    \tl_use:N \l__pseudo_begin_tabular_tl
    \tex_noalign:D {
        \RestorePseudoBackslash
        \int_gset_eq:NN \g__pseudo_indent_level_int
                        \l__pseudo_initial_indent_level_int
        \__pseudo_eol_handle_args:nnn{#2}{#3}{#4}
    }
    \__pseudo_bol:
} {
    \tl_use:N \l__pseudo_end_tabular_tl
    \bool_if:nTF { \l__pseudo_compact_bool } {
        \group_end:
    } {
        \mode_if_vertical:F {
            \unskip \par
            \group_insert_after:N \@endparenv
        }
        \addvspace{ \l_tmpa_skip }
        \exp_args:NNNV
            \group_end:
            \dim_set:Nn \prevdepth \l__pseudo_prevdepth_tl
    }
}
\pseudodefinestyle{ starred }{
    preamble = {
        >{\pseudohpad\pseudoindent\pseudofont}
        l
        <{\pseudohpad}
    },
    prefix = {\pseudobol},
}
\NewDocumentEnvironment { pseudo* } { +O{} } {
    \begin{pseudo}[starred, #1]
} {
    \end{pseudo}
}
\@ifpackageloaded { booktabs } { } {
    \dim_const:Nn \aboverulesep       { .40ex }
    \dim_const:Nn \belowrulesep       { .65ex }
    \dim_const:Nn \heavyrulewidth     { .08em }
    \dim_const:Nn \lightrulewidth     { .05em }
}
\dim_const:Nn \c__pseudo_very_thin_dim     { 0.2pt }
\dim_const:Nn \c__pseudo_thin_dim          { 0.4pt }
\dim_const:Nn \c__pseudo_semithick_dim     { 0.6pt }
\cs_new:Npn \__pseudo_prevdepth_adjustment: {
    \par
    \dim_compare:nNnF \prevdepth < \c_zero_dim {
        \dim_compare:nNnT \prevdepth < { .3 \baselineskip } {
            \skip_vertical:n { .3 \baselineskip - \prevdepth }
            \skip_vertical:N \c_zero_dim
        }
    }
}
\pseudodefinestyle { in-float } {
}
\tl_new:N \l__pseudo_float_init_tl
\pgfqkeys { /tcb/pseudo } {
    init/.code = {
        \tl_set:Nn \l__pseudo_float_init_tl { #1 }
    },
boxruled/.style = {
    floatplacement = tbp,
    before~upper = {
        \dim_set:Nn \parskip    { .3 \baselineskip }
        \dim_set:Nn \topsep     { .2 \baselineskip }
        \dim_set:Nn \partopsep  { 0pt              }
        \dim_set:Nn \prevdepth  { .3 \baselineskip }
        \RestorePseudoEq
        \pseudoset { in-float }
        \l__pseudo_float_init_tl
    },
    after~upper = \__pseudo_prevdepth_adjustment:,
    beforeafter~skip~balanced = \l__pseudo_topsep_tl,
    boxsep              = 0pt,
    toptitle            =   \belowrulesep,
    bottomtitle         =   \aboverulesep,
    top                 = 2 \belowrulesep,
    bottom              = 2 \aboverulesep,
    left                = 2 \belowrulesep,
    right               = 2 \belowrulesep,
    fonttitle           = \bfseries,
    description~font    = \normalfont,
    before~title        =
        \dim_set:Nn \prevdepth  { .3 \baselineskip },
    after~title         = \__pseudo_prevdepth_adjustment:,
    separator~sign      = {
        \normalfont
        \skip_horizontal:n { .5em - \fontdimen2\font\space }
    },
    sharp~corners,
    colback             = white,
    colbacktitle        = white,
    coltitle            = black,
    colframe            = black,
    boxrule             = \c__pseudo_thin_dim,
    titlerule           = \c__pseudo_very_thin_dim,
},
ruled/.style = {
    pseudo/boxruled,
    empty,
    boxrule             = 0pt,
    toprule             = \heavyrulewidth,
    titlerule           = \lightrulewidth,
    bottomrule          = \heavyrulewidth,
    left                = 0pt,
    right               = 0pt,
    titlerule~style     = draw,
    borderline~north    = {\heavyrulewidth}{0pt}{black},
    borderline~south    = {\lightrulewidth}{0pt}{black},
},
booktabs/.style = {
    pseudo/ruled,
    no~borderline,
    bottomrule          = \heavyrulewidth,
    borderline~horizontal = {\heavyrulewidth}{0pt}{black}
},
boxed/.style = {
    pseudo/boxruled,
    empty,
    titlerule           = 0pt,
    borderline          = {\c__pseudo_thin_dim}{0pt}{black},
    toptitle            = 1.5 \belowrulesep,
    bottomtitle         = 0pt,
    top                 = 0.3 \baselineskip,
},
tworuled/.style = {
    pseudo/boxed,
    no~borderline,
    left                = 0pt,
    right               = 0pt,
    boxrule             = 0pt,
    toprule             = \heavyrulewidth,
    bottomrule          = \heavyrulewidth,
    borderline~horizontal = {\heavyrulewidth}{0pt}{black},
},
filled/.style = {
    pseudo/boxruled,
    tile,
    colback             = \pseudohlcolor,
    colbacktitle        = lightgray,
    bottom              = 2 \aboverulesep + \c__pseudo_thin_dim,
    left                = 2 \belowrulesep + \c__pseudo_thin_dim,
    right               = 2 \belowrulesep + \c__pseudo_thin_dim,
}
}
\msg_new:nnn { pseudo } { useinstead } {
    The~#1 command~(used~\msg_line_context:)~is~deprecated;~
    use~#2 instead.
}
\cs_new:Npn \__pseudo_use_instead:nn #1 #2 {
    \msg_warning:nnnn { pseudo } { useinstead } { #1 } { #2 }
}
\NewDocumentCommand \pseudoslash { } {
    \__pseudo_use_instead:nn \pseudoslash \RestorePseudoBackslash
    \RestorePseudoBackslash
}
\NewDocumentCommand \pseudoeq { } {
    \__pseudo_use_instead:nn \pseudoeq \RestorePseudoEq
    \RestorePseudoEq
}
\msg_new:nnn { pseudo } { hl-without-hpad } {
    hl~used~without~hpad~\msg_line_context:.
}
\pgfkeysifdefined { /tcb/beforeafter~skip~balanced/.@cmd } { } {
\pgfqkeys { /tcb } {
    before~skip~balanced/.style = { before = {
        \int_compare:nNnF { \lastnodetype } = { -1 } {
            \par
            \mode_if_vertical:T {
                \__pseudo_if_minipage:
                    \dim_compare:nNnTF \parskip > \c_zero_dim {
                        \addvspace{ -\parskip }
                    }
                \else:
                    \bool_lazy_or:nnTF {
                        \dim_compare_p:nNn
                            \prevdepth < \c_zero_dim
                    } {
                        \dim_compare_p:nNn
                            \prevdepth > { .3 \baselineskip }
                    } {
                        \addvspace { \skip_eval:n {
                            #1 - \parskip
                        } }
                    } {
                        \addvspace { \skip_eval:n {
                            #1 + .3 \baselineskip
                            - \prevdepth - \parskip
                        } }
                    }
                \fi:
                \nointerlineskip
            }
        }
        \dim_set_eq:NN \lineskip \c_zero_dim
        \noindent
    } },
    after~skip~balanced/.style = { after = {
        \par
        \mode_if_vertical:T {
            \dim_set:Nn \prevdepth { .3\baselineskip }
            \addvspace { \skip_eval:n { #1 - \parskip } }
        }
    } },
    beforeafter~skip~balanced/.style = {
        before~skip~balanced = { #1 },
        after~skip~balanced  = { #1 }
    }
}
\RequirePackage{etoolbox}
\AtEndPreamble {
    \@ifpackageloaded { tcolorbox } {
        \tl_set_eq:NN \__pseudo_orig_tcbopt \tcb@apply@box@options
        \def \tcb@apply@box@options #1 {
            \__pseudo_orig_tcbopt { #1 }
            \tl_set_eq:NN \__pseudo_if_minipage: \if@minipage
        }
    } { }
}
}
\AtEndPreamble {
    \@ifpackageloaded { tcolorbox } {
        \cs_new:Npn \__pseudo_vmaj:n #1 { \__pseudo_vmaj_aux:w #1 \q_stop }
        \cs_new:Npn \__pseudo_vmaj_aux:w #1 . #2 \q_stop { #1 }
        \tl_set:Nx \l_tmpa_tl {
            \exp_args:No \__pseudo_vmaj:n \tcb@version
        }
        \int_compare:nNnT \l_tmpa_tl < 5 {
            \tcbuselibrary { hooks }
            \tcbset {
                pseudo/boxruled/.append~style = {
                    before~upper~app = \vspace { -\parskip }
                }
            }
        }
    }
}
