Suppose I am trying to write a major mode. In this mode, certain ordinary characters should have special "electric" behavior when typed, but only in some contexts (e.g. in code but not in strings or comments). The definition of "some contexts" is very complicated, but it's neatly summarized by the syntax highlighting: if the character
x, inserted at point, would be given a particular face by font-lock, then the characters shouldn't have electric behavior, otherwise they should.
So I'm imagining that I could somehow write
(defun mymode-maybe-electric-squiggle () (interactive) (if (or (not mymode-electricity) ; user option (memq (faces-of-hypothetically-inserted-character ?x) 'mymode-literal-face)) (call-interactively 'self-insert-command) ;; else do the electric thing ))
But I can't figure out how to write
x shouldn't go on the undo list, and the buffer shouldn't visibly have an
x in it for a moment while I give font-lock a chance to run, and how would I give font-lock a chance to run in the middle of a command, anyway?
(For purposes of this question, please assume that there isn't any other feasible way to make the decision.)