将Emacs的输入事件转换为键代码

将Emacs的输入事件转换为键代码,emacs,elisp,Emacs,Elisp,在使用一些Vim仿真功能时,我产生了以下代码: 如果你按;后跟Return,然后光标将跳转到行尾并插入分号 (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 th

在使用一些Vim仿真功能时,我产生了以下代码:

如果你按;后跟Return,然后光标将跳转到行尾并插入分号

(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)))))
此函数工作正常,但所有内容都是硬编码的。我更愿意让它更通用。理想情况下,我希望指定一个kbd宏,例如kbd作为参数,并将其与读取字符的结果进行比较。但是,kbd返回一个符号,read char返回一个字符代码。我查阅了Emacs文档,但找不到转换

有没有办法比较这两者?或者有更简单的方法吗?

那么:

(global-set-key (kbd "RET") 'electric-inline-comment)

(defun electric-inline-comment ()
  (interactive "*")
  (if (and (eq last-command 'self-insert-command)
           (looking-back ";"))
      (progn
        (delete-region (match-beginning 0) (match-end 0))
        (end-of-line)
        (insert ";"))
    (newline)))
那么这个呢:

(global-set-key (kbd "RET") 'electric-inline-comment)

(defun electric-inline-comment ()
  (interactive "*")
  (if (and (eq last-command 'self-insert-command)
           (looking-back ";"))
      (progn
        (delete-region (match-beginning 0) (match-end 0))
        (end-of-line)
        (insert ";"))
    (newline)))

好主意,我喜欢。我不知道回头看看好主意,我喜欢。我不知道该如何回首往事