我有一个Emacs的elisp脚本,如果用户点击Ctrl+ ,我想做一些清理工作G.我使用'read-event'来捕获所有事件,但这并没有捕获Ctrl+ G.当Ctrl+ G被击中时,它就会停止执行.
在XEmacs中,当您调用next-command-event时,它将为您提供所有事件,包括用户何时命中Ctrl+ G.在Emacs中必须有一些等价物.
Tre*_*son 14
您可以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)))))
Run Code Online (Sandbox Code Playgroud)
注意: with-local-quit返回最后一个表达式的值,或者nil如果C-g按下,则确保在C-g未按下时返回非零值.我发现退出的elisp文档很有用.相关区域是非本地退出,具体而言unwind-protect,它不仅适用于退出.
condition-case并unwind-protect在这里很有帮助. condition-case让你"捕获""例外",其中一个是退出:
(condition-case
(while t) ; never terminates
(quit (message "C-g was pressed")))
Run Code Online (Sandbox Code Playgroud)
您还可以捕获其他错误,例如"错误".
unwind-protect就像最后一样; 它将执行"体形"然后"展开形式".但是,无论"正文形式"是否成功运行,都会执行"展开形式":
(unwind-protect
(while t)
(message "Done with infinite loop"))
Run Code Online (Sandbox Code Playgroud)
你想要unwind-protect在你的情况下.