10

Ctrl我有一个 Emacs 的 elisp 脚本,如果用户点击+ ,我想对其进行一些清理G。我使用 'read-event' 来捕获所有事件,但这不会捕获Ctrl+ G。当Ctrl+G被击中时,它只是停止执行。

在 XEmacs 中,当您调用 next-command-event 时,它将为您提供所有事件,包括用户点击Ctrl+的时间G。在 Emacs 中必须有一些等价物。

4

2 回答 2

15

您可以使用with-local-quit来确定是否C-g被按下:

按照efunneko的建议,编辑了吞咽退出的解决方案。

(defun my-c-g-test ()
  "test catching control-g"
  (interactive)
  (let ((inhibit-quit t))
    (unless (with-local-quit
              (y-or-n-p "arg you gonna type C-g?")
              t)
      (progn
        (message "you hit C-g")
        (setq quit-flag nil)))))

注意: with-local-quit 返回最后一个表达式的值,或者nilifC-g被按下,所以当 no 被按下时一定要返回非 nilC-g的值。我发现关于戒烟的 elisp 文档很有用。一个相关的领域是非本地出口,特别是unwind-protect,它不仅仅适用于退出。

于 2009-05-19T19:17:27.397 回答
8

condition-case并且unwind-protect在这里很有帮助。 condition-case让您“捕捉”“异常”,其中退出是其中之一:

(condition-case
    (while t) ; never terminates
  (quit (message "C-g was pressed")))

您还可以捕获其他错误,例如“错误”。

unwind-protect就像最后一样;它将执行“body forms”,然后执行“unwind forms”。但是,无论“主体表单”是否成功运行,都会执行“展开表单”:

(unwind-protect
    (while t)
  (message "Done with infinite loop"))

你想要unwind-protect在你的情况下。

于 2009-07-09T08:44:20.520 回答