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

问题描述

我从来没有能够提出一种方法来渗透 set-process-sentinel 层次结构,其中let-bound变量定义在功能 - 只有缓冲区本地或全局变量可以渗透。允许变量可以达到第一个开始进程,但是尽可能地渗透而不被拒绝,因为被无法识别 - 允许变量在函数开头定义的似乎不能穿透以(lambda(pe)...开始的部分。。任何人都可以想到一种方法,包括穿透嵌套哨兵,如下面的示例

(启动进程
my-process-name-one
* OUTPUT-BUFFER *
/ path / to / executable
参数一个
参数 - 两个
参数三)
(lambda(pe) (= 0(process-exit-status p))
(set-process-sentinel
(start-process
my-process-name-two
nil ;;不使用输出缓冲区的例子
/ path / to / executable
argument-one
argument-two
argument-three)
(lambda(pe)(when(= 0(process-exit-status p))
(set-process-sentinel
(start-process。 。 。 ))))))))


解决方案

问题是默认情况下,Emacs Lisp变量绑定是动态的。也就是说,当函数被评估时,在定义函数的环境中查找绑定变量不是,而在调用该函数的环境中。



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

  ;; -  *  - 词法绑定:t; -  *  -  

另一个选择是使用 lexical-let cl 库。这在早期的Emacs版本中也起作用。请注意,通过这种方式,您明确指定哪些变量应具有词法绑定,因此,代码(例如(lexical-let((foo foo))...)并不罕见 - foo 是一个现有的变量,需要继承到函数中。


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 . . . ))))))))
解决方案

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 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; -*-

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`层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:01