2

在处理一些 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 "<return>"))作为参数并将其与(read-char). 但是,kbd 返回一个符号并(read-char)返回一个字符代码。我一直在查看 Emacs 文档,但找不到转换。

有没有办法比较两者?还是有更简单的方法?

4

1 回答 1

4

那这个呢:

(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)))
于 2012-08-15T10:43:18.780 回答