latex3
latex3 copied to clipboard
Running code then restoring values of some variables
Internally to the "write" handling we use a badly-named command \__kernel_iow_with:Nnn which sets an integer #1 to the value #2, then runs the code #3, then restores the original value of #1. Both assignments to #1 are done locally.
In l3keys we have situations where we want to save the current module before setting some keys in another module, then coming back to the original module. This is done by some juggling of arguments like \exp_args:no { \tl_set:Nn \X { NEW } CODE \tl_set:Nn \X } { \X } (well, \exp_args:no doesn't exist, but that's another discussion). It would be good to provide a command to restore one or more variables to their original values. Without too much thought, the idea could be:
\cs_new_protected:Npn \prg_restore:nn #1#2
{
\use:ne {#2}
{ \@@_restore_loop:n #1 { ? \prg_break: } \prg_break_point: }
}
\cs_new:Npn \@@_restore_loop:n #1
{
\use_none:n #1
\token_if_expandable:NTF #1
{ \exp_not:n { \tl_set:Nn #1 } { \exp_not:o {#1} } }
{ #1 = \tex_the:D #1 \scan_stop: }
\@@_restore_loop:n
}