代码之家  ›  专栏  ›  技术社区  ›  efunneko

如何在Emacs中截取ctrl-g

  •  6
  • efunneko  · 技术社区  · 17 年前

    我有一个emacs的elisp脚本,如果用户点击,我想在其中做一些清理。 Ctrl键 + G . 我使用“read event”捕获所有事件,但这不捕获 Ctrl键 + G . 什么时候? Ctrl键 + G 被击中,它只是停止执行。

    在XEMAC中,当您调用下一个命令事件时,它将为您提供所有事件,包括用户点击时 Ctrl键 + G . 在Emacs中必须有一些等价物。

    2 回复  |  直到 8 年前
        1
  •  12
  •   Felipe Lema    8 年前

    你可以使用 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返回最后一个表达式的值,或者 nil 如果 C-G 被按下了,所以一定要在没有时返回非零的东西。 C-G 被按下。我在上找到了elisp文档 quitting 有用的。相关区域是 nonlocal exits 特别是 unwind-protect 这不仅适用于退出。

        2
  •  5
  •   jrockway    17 年前

    condition-case 和 unwind-protect 在这里很有用。 情况 允许您“捕获”“异常”,其中quit是一个:

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

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

    放松保护 就像最后一样,它将执行“身体形态”,然后执行“放松形态”。但是,无论“body form”是否成功运行,都会执行“unwind form”:

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

    你想要 放松保护 以你为例。