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 faces-of-hypothetically-inserted-character
. The 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.)
post-self-insert-hook
and put there a function which checks ifx
was just inserted (with the additional conditions you want). This will be more likely to correctly interact with other forms of electricity!x
is inserted. I'm trying to make something special happen, upon insertion of several different characters none of which isx
, whenx
would be given a particular face if it had been the character inserted.x
in my comment with~
(or whatever is the "squigle" you're trying to electrify). I posted it as a comment because it is not an answer to your question, only a side remark about the definition you posted.~
(or whatever) has syntactic significance in itself and will probably be given a different face than the onex
would be.post-self-insert-hook
, I'm going to look at whether it will help.