使用 let-bound 变量渗透“set-process-sentinel"层次结构 [英] Penetrating the `set-process-sentinel` hierarchy with let-bound variables

查看:17
本文介绍了使用 let-bound 变量渗透“set-process-sentinel"层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有想出一种方法来穿透 set-process-sentinel 层次结构,其中 let 绑定的变量在函数开始时定义 -- 只有缓冲区局部或全局变量可以穿透它.let-bound 变量可以到达第一个 start-process,但这是它们可以穿透而不会因无法识别而被拒绝的范围 -- let-bound 变量在开始时定义函数似乎无法穿透以(lambda (pe) . .开头的部分.有没有人能想出办法做到这一点,包括strong> 穿透嵌套哨兵,就像下面的例子一样?

I have never been able to come up with a method to penetrate the set-process-sentinel hierarchy with let-bound variables defined at the outset of the function -- only buffer-local or global variables can penetrate it. Let-bound variables can reach the first start-process, but that is as far as they can penetrate without being rejected due to being unrecognized -- let-bound variables defined at the outset of the function do not appear to be able to penetrate the section that begins with (lambda (p e) . . .. Can anyone think of a way to do it, including penetrating nested sentinels like in the example below?

(set-process-sentinel 
  (start-process
    "my-process-name-one"
     "*OUTPUT-BUFFER*"
    "/path/to/executable"
    "argument-one"
    "argument-two"
    "argument-three")
  (lambda (p e) (when (= 0 (process-exit-status p))
    (set-process-sentinel 
      (start-process
        "my-process-name-two"
        nil ;; example of not using an output buffer
        "/path/to/executable"
        "argument-one"
        "argument-two"
        "argument-three")
      (lambda (p e) (when (= 0 (process-exit-status p))
        (set-process-sentinel 
          (start-process . . . ))))))))

推荐答案

问题是 Emacs Lisp 变量绑定默认是动态的.也就是说,当一个函数被求值时,绑定变量不是在定义函数的环境中查找,而是在调用函数的环境中查找.

The problem is that Emacs Lisp variable bindings are dynamic by default. That is, when a function is evaluated, bound variables are looked up not in the environment where the function was defined, but in the environment where the function was called.

Emacs 24 或更高版本本机支持词法绑定(即,该函数看到绑定在函数定义周围的变量),但由于它改变了现有代码的语义,您需要启用它明确地.通常这是通过在 .el 文件的第一行添加文件局部变量设置来完成的:

Emacs 24 or later supports lexical binding (that is, the function sees the variables that were bound around the function definition) natively, but since it alters the semantics of existing code you need to enable it explicitly. Usually this is done by adding a file local variable setting to the first line of the .el file:

;; -*- lexical-binding: t; -*-

另一种选择是使用 cl 库中的 lexical-let.这也适用于早期的 Emacs 版本.请注意,通过这种方式,您明确指定了哪些变量应该具有词法绑定,因此诸如 (lexical-let ((foo foo)) ...) 这样的代码并不少见 — foo 是一个需要结转"到函数中的现有变量.

Another alternative is to use lexical-let from the cl library. This works in earlier Emacs versions as well. Note that in this way you explicitly specify which variables should have lexical binding, so code such as (lexical-let ((foo foo)) ...) is not uncommon — foo is an existing variable which needs to be "carried over" into the function.

这篇关于使用 let-bound 变量渗透“set-process-sentinel"层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆