用 emacs-async 写 elisp 函数需要注意什么?

用 emacs-async 写 elisp 函数需要注意什么?

怎样 async 调用同一个文件内定义的其它函数?

有没有用 emacs-async 写的比较好的包可供参考?

看 async-test.el 不过现在学用emacs26 的thread比较好些。

我短期内不会转移到 emacs 26 上,至少得等到 spacemacs 官方宣布支持 emacs 26 才行。

test 我看了,几种用法过于简单了,没有覆盖更复杂的函数调用的问题

I prefer to learn from reference manuals. I “dive into” each paragraph, and “come up for air” between paragraphs. When I get to the end of a paragraph, I assume that that subject is done, finished, that I know everything I need (with the possible exception of the case when the next paragraph starts talking about it in more detail). I expect that a well written reference manual will not have a lot of redundancy, and that it will have excellent pointers to the (one) place where the information I want is.

你需要实现什么样的调用

比如说我在写一个包,其中有的函数是 sync 执行的,有些函数是 async 执行的(使用 emacs-async),我怎样在 async-start 语句内调用同一个包内定义的其它函数呢?

你要先理清 用async调用是什么目的,你调用同一个包里其它的函数的返回值会在async-start用到吗,没理解清楚你的目的,不过直接在async-start的START-FUNC里调用不行吗

是的,会用到

不行,因为子进程没有这个函数的定义

需要 require:

require 这个包自身吗?

参考这个

1 个赞

这样不好吧:

(defun foo-bar ()
  ;; ...
  )

(defun foo-bar-async ()
  (async-start `(lambda ()
                  (require 'foo) ;; require 自身
                  (foo-bar)
                  ;; ...
                  )))

(provide 'foo)

我没试过,不知道会有什么问题。

我尝试过 load 自身,会报错

这个不错,被选答案回答的非常详尽了

这个答案确实非常 hacky,(误)这是我第一次看到必须用 set (而不是 setq)和 fset 的场合。当然我的 elisp-fu 还是太弱了。

async 说白了就是新开一个“emacs -q” 来处理,你要运行你自己的函数的话,需要手动设置 load-path,require 包, 然后调用函数

单文件包,就一个函数需要 async 运行,不想另开一文件了。勾选答案里的链接里有解决办法,不用 require

补充一点,不只是 START-FUNC,在 FINISH-FUNC 中使用局部变量也要这么处理(FINISH-FUNC 中可以直接使用之前定义的函数,但是局部变量必须处理,不然就是 void variable)。这么说可能不太好理解,等我有空了补充代码块说明。

你有打算用 emacs-async 来干嘛吗?(如解决实际的需求)

变量没问题(只需要绑定变量的话也可用自带的 async-inject-variables),但应该只能处理简单的函数。比如:如果这个函数调用的了非 Emacs 内置的函数就不行了。除此之外应该还有别的因素使得这个办法不可靠。