This question led to a new package:
xpeek
I’ve been developing my xpeek package with lots of help from you folks on TeX.SX. It’s intended to help write commands which, like the familiar \xspace, look ahead into the input stream and choose what to do based on what follows. Although the interface needs to change, it has enough functionality to implement something like xspace, or the intelligent italic-correction from LaTeX's \textit.
Which means it has the same familiar limitation as \textit, best illustrated by example:
\textit{foof}\xspace. \textit{foof}\xspace! % bad italic correction before "." \def\nocorrlist{.,\xspace} \textit{foof}\xspace. \textit{foof}\xspace! % no italic correction before "!" Thinking about the problem, it seems I need the ability to scan ahead in the input stream, ignoring tokens from one list while looking for tokens from another. In Expl3 terms, I’m hoping to define something like \peek_inlist_ignore_auxlist:nnTF.
As an initial step toward this end, I’m trying to simply scan ahead for the next input token not on the ignore list. For example:
\def\ignorelist{ ,.;:!} \newcommand{\nextnonpunc}{\scanignoring{\ignorelist}`\scanfound'} The next non-punctuation mark is \nextnonpunc.,;:! xyz yielding
The next non-punctuation mark is ‘x’.,;:! xyz
(Actually, since I’m using Expl3, the syntax would be
\ExplSyntaxOn \tl_const:Nn \ignorelist {~,.;:!} \NewDocumentCommand { \nextnonpunc } { } { \peek_ignore_list:N \ignorelist `\l_peek_token' } \ExplSyntaxOff or something like that.)
So how can I implement \peek_ignore_list:N?
The direction I’m considering is to read ahead, consuming tokens. Each token read is added to a save-list and compared to the ignore-list. If it’s on the ignore-list, continue; otherwise put the save-list back on the input stream and stop.
Does this sound reasonable so far?
To consume tokens one-by-one, I built this function:
\cs_new_protected:Npn \peek_meaning_really_remove:NTF #1 #2 #3 { \peek_meaning_remove:NTF #1 { #2 } { \peek_meaning_remove:NT \l_peek_token { #3 } } } but the question above still stands: How can I implement \peek_ignore_list:N?


\futureletand test for\bgrouplike 2e's\@ifnextcharbut futurelet _consumes the token, leaving it's definition in the token that it defines, but this means that if (say) you see anaand want to put it back you don;t know it was anacharacter just a\csnamethat has been\lettoa. that may or may not matter, depending on what the tokens that you put back are supposed to do, but in general you don't know that.\xspace” is ignorable.\futureletconsumes the token"?\futurelet\tmp\mymacro athenais removed from the input stream. But as you are tying to politely point out, that's false. Maybe I'll go back to MathML:-)