# quail: TeX input method: change UTF8 to tex and back: solution & problems

Stephen J. Turnbull stephen at xemacs.org
Mon Jan 7 19:50:06 EST 2008

Aidan Kehoe writes:

> What you need to do is rewrite the code to use #'posix-search-forward, which
> guarantees that it will return the longest match (that is, it will always
> try to match "\\infty" if possible, and only then look for "\in".)

I don't think so: this code is almost surely trying each car in order.
Something like:

(progn
(search-forward (regexp-opt (mapcar #'car replacement-alist)))
(goto-char (match-begin 0))
(when (looking-at target-string)
(replace-match replacement-string)))

Note to Uwe: one problem you're facing here is that SGML entities have
a fairly reliable terminator character, the semicolon.  TeX does not,
so you might try replacing target-string' in the logic above with
(concat target-string "\\>")'.

> You might also want to look into the #'regexp-opt function, which,
> given a list of strings,

regex-opt won't work here, it doesn't know anything about internal
grouping, so there's no way to trigger the replacement of "\in" rather
than "\infty".  In fact, I doubt that Emacs regexp groups can
simultaneously express all the relevant string matches for
#r"\in$$t\|fty$$".