I'm playing around a bit with the \tl_set_rescan:Nnn function (originally to make this answer more concise), but I'm struggling to make even pretty simple uses of this function work.
Say we want to make all \ characters letters and spaces have their usual catcode. As far as I understand, the following code should produce identical outputs:
\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\group_begin:
\char_set_catcode_escape:N \~
~char_set_catcode_letter:N ~\
~char_set_catcode_space:n {32}
~tl_set:Nn ~l_tmpa_tl {<\verb|\LaTeX| \LaTeX>}
~tl_show:N ~l_tmpa_tl
~group_end:
%%%%%%%%%%
\tl_set_rescan:Nnn \l_tmpa_tl
{ \char_set_catcode_space:n {32} \char_set_catcode_letter:N \\ }
{<\verb|\LaTeX| \LaTeX>}
\tl_show:N \l_tmpa_tl
\ExplSyntaxOff
\end{document}
outputs
> \l_tmpa_tl=<\verb|\LaTeX| \LaTeX>.
> \l_tmpa_tl=<\verb |\LaTeX |\LaTeX >.
The result of the first token list is correct, \ was made a letter character and thus no extra spaces are output after the control sequences. However, in the rescan attempt the control sequences do still exist. Also note the missing space after the second |.
How do you make the second version produce the expected result? Or perhaps a bit more broad, as these functions don't seem to work well with verbatim input, what are the designated use cases for them?
\newlinecharbefore and after using\tl_set_rescan:Nnn(which internally assigns other values to\newlinechar). – Pablo González L Jun 19 '19 at 11:28\tl_set_rescan:Nnnmore carefully, as it implies the second argument is read in before the new catcodes are applied. The documentation before reads as if it would be absorbed under the new catcode regime to me. So those functions seem to be useless when dealing with verbatim code then. – siracusa Jun 19 '19 at 22:58expl3functions, by design, don't do catcode changes while grabbing an argument precisely to avoid unexpected behaviour of a function. Which makes sense: you use a function without the need to worry that this one or that one will grab its contents verbatim or under some peculiar catcode regime. Of course, if you do need that, then you have to work yourself around this feature to get the catcodes right. – Phelype Oleinik Jun 20 '19 at 09:55expl3should definitely have some basic support for handling verbatim material like in your wrapper, IMHO. The thing is, if you do all the catcode setting yourself, you don't need\tl_set_rescan:Nnnanymore. Actually, replacing it by\tl_set:Nn #1 {#3}in your implementation gives the same result. – siracusa Jun 21 '19 at 00:28\tl_analysis_show:Nfunction. :) – siracusa Jun 22 '19 at 00:23|can be added with a~circumventing the lack of space in expl3 syntax. The space after the control sequences is a harder problem, because when TeX reads the token list it inserts those spaces, but when it makes\a letter it doesn't know it has to remove that space. I think you'd need some kind of post-processing in this case... – Phelype Oleinik Jun 22 '19 at 00:38