0

I'd like to define a minor mode in which, when I write something, point is not moved and the characters are instead inserted after point. I thought that would be easy: just add a hook after insert-char calling backward-char, so I wrote

(add-hook #'backward-char #'insert-char)                                                    

and nothing happened. I'm assuming this is because typing a character normally calls insert-char interactively. Then I noticed that even if it would have worked, the hook would probably call backward-char before insert-char, right? So, how do I do this: add a hook calling backward-char after a character is typed each time?

1
  • 1
    backward-char is not a hook (i.e. a variable that holds a list of functions and gets run at a specific point of a function). Most hooks are called something-hook, so one way to find out about them is to do C-h v -hook TAB to get the completions, The completion buffer will be big (there are lots of hooks that match this name - and some that don't so it is not completely reliable), but you can switch to the window with a couple of C-x o keys and then move around doing C-h v on anything that looks promising. It's not fast but it's faster than trying to assign to a random name.
    – NickD
    Feb 2 at 15:53

1 Answer 1

1

Inserting characters into the buffer is normally done via self-insert-command, which has a hook called post-self-insert-hook that you can use:

(defun backward-char-after-insert ()
    (backward-char))
(add-hook 'post-self-insert-hook 'backward-char-after-insert 100 t)

Here I deliberately set the local value of the hook, since I'm assuming you don't want this behaviour everywhere :-)

Of course there are commands that insert text into the buffer without passing through self-insert-command, so this might not get you 100% of what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.