Just a thought -
This should not really be about "keystrokes", but about commands - no matter how they are executed, no? If I understand correctly, you are really looking for a way to tell whether a given command that gets invoked modifies the buffer.
If so, then maybe do something like this:
Put a function on pre-command-hook
that (1) records the current value of (buffer-modified-p)
and then (2) uses set-modified-p
to make the buffer unmodified.
Put a function on post-command-hook
that (1) checks whether the command modified the buffer (by comparing the recorded value against the new current value of (buffer-modified-p)
), then acts accordingly, and (2) if the new value is nil
then resets to the pre-change value using set-modified-p
.
Kind of heavy-handed (it checks for each command that is invoked), but it might be worth a try.
If the "acts accordingly" part is just a no-op for all buffer modifying commands, and if it is also a no-op for many non-modifying commands (e.g. because you just did it on a previous non-modifying command), then at least you will not be doing that "acts accordingly" action more than necessary. But the checking part alone might be expensive/overkill (slow things down too much) - try it to see.
self-insert-command
to cancel and reschedule a timer instead of using an idle timer. This way, even if the user does something, the timer is not reset unless the "something" is text insertion.after-change-functions
is a general mechanism for reacting to text changes.post-command-hook
. That will trigger on everything, so you may need to do some filtering on what triggered it before taking whatever action you want.buffer-chars-modified-tick
function may be useful.