睡在emacs lisp
(insert (current-time-string))
(sleep-for 5)
(insert (current-time-string))
Mx eval -buffer
,两个时间字符串插入5秒钟。
M-x eval-buffer
, two time strings are inserted with 5 secs apart
一些comint代码(添加钩子和开始进程)
some comint code (that add hook, and start process)
(sleep-for 60) ;delay a bit for process to finish
(insert "ZZZ")
Mx eval-buffer
,ZZZ立即插入,没有任何时间延迟
M-x eval-buffer
, "ZZZ" is inserted right away, without any time delay
可能发生了什么? btw,它是Win XP上的Emacs 23.2
what might have happened? btw, it's Emacs 23.2 on Win XP
如果你想做的是等待一个进程完成,你应该可能不会使用 sleep-for
。而不是异步地调用进程:
If all you want to do is to wait for a process to finish, you should probably not use sleep-for
at all. Instead call the process synchronously, not asynchronously:
这样,Emacs将阻止,直到进程完成。
This way, Emacs will block until the process has finished.
如果你必须(或真的想)使用一个异步的过程,例如因为它需要很长时间,并且你不希望Emacs在这段时间内冻结(你说60秒,这是相当长的时间),那么等待进程完成的正确方法是使用哨兵。哨兵是一个回调,每当进程的状态发生变化时都会被调用,例如,当终止时,它将被调用。
If you must (or really want to) use an asynchronous process, for instance because it takes very long and you don't want Emacs to freeze during that time (you speak of 60 seconds, which is quite long), then the right way to wait for the process to finish is by using a sentinel. A sentinel is a callback that gets called whenever the status of a process changes, e.g., when it terminates.
(defun my-start-process ()
"Returns a process object of an asynchronous process."
...)
(defun my-on-status-change (process status)
"Callback that receives notice for every change of the `status' of `process'."
(cond ((string= status "finished\n") (insert "ZZZ"))
(t (do-something-else))))
;; run process with callback
(let ((process (my-start-process)))
(when process
(set-process-sentinel process 'my-on-status-change)))