I conjured up the following code while working on some Vim emulation feature:
If you press ; followed by Return then the cursor will jump to the end of the line and insert a semicolon.
(global-set-key (kbd ";") 'insert-or-append)
(defun insert-or-append ()
"If the user enters <return>, then jump to end of line and append a semicolon,
otherwise insert user input at the position of the cursor"
(interactive)
(let ((char-read (read-char-exclusive))
(trigger ";"))
(if (eql ?\r char-read)
(progn
(end-of-line)
(insert trigger))
(insert (this-command-keys)))))
This function works alright, but everything is hardcoded. I would prefer to make it more generic. Ideally, I’d like to specify a kbd-macro (for example (kbd "<return>")) as argument and compare it to the result of (read-char). However, kbd returns a symbol and (read-char) returns a character code. I’ve been going over the Emacs documentation, but was unable to find a conversion.
Is there a way to compare the two? Or is there an easier approach?
What about this: