The insert-char function wants a character, but substring returns a string. They're not the same type, and a character in elisp is essentially an integer. Let's convert the hexadecimal charcode
into an integer before passing it to insert-char
.
(defun insert-char-c (charcode)
"Parse a hexadecimal charcode and insert that character."
(interactive "scharcode: ")
(insert-char (cl-parse-integer charcode :radix 16)))
UPDATE: Credit to @rpluim for this, but insert-char
when used interactively already knows how to parse hexadecimal values (and more). You could rebind C-S-u to insert-char
like this.
(global-set-key (kbd "C-S-u") #'insert-char)
UPDATE #2: @Latex_xetaL had an interesting request in the comments.
I don't want to press RET.
I came up with a way to read a 4 key sequence, and after it reads those 4 keys, it will interpret what you typed as a hexadecimal number and pass it on to insert-char
. No RET needed.
(defun insert-char-4 ()
"Read 4 keyboard inputs, interpret it as a hexadecimal number, and insert it as a character."
(interactive)
(let* ((k1 (read-key-sequence "____"))
(k2 (read-key-sequence (concat k1 "___")))
(k3 (read-key-sequence (concat k1 k2 "__")))
(k4 (read-key-sequence (concat k1 k2 k3 "_")))
(charcode (cl-parse-integer (concat k1 k2 k3 k4) :radix 16)))
(insert-char charcode)
(message (concat k1 k2 k3 k4 " => " (char-to-string charcode)))))
- The prompt starts as "____" and as you type the next 4 hexadecimal digits, it'll fill in.
- I don't know if 4 hexadecimal digits will be enough, but let's go with this for now.
- The
message
at the end is to show you what you typed and what was inserted.
Also, this is my first time using read-key-sequence
. If there's a better way to do this, let me know. I'm learning as I go.