命令完成后,Elisp关闭* Async Sh​​ell Command *窗口

时间:2013-04-20 11:33:57

标签: asynchronous elisp

我创建了一个函数,它运行pdflatex编译器为当前编辑的tex文件生成pdf文件:

(defun latex-compile ()
  "Runs pdflatex on current file"
  (interactive)
  (let ((file-name (shell-quote-argument (buffer-file-name))))
    (async-shell-command (concat "pdflatex " file-name))))

是否有可能在流程退出后关闭窗口*Async Shell Command*,如果它已成功退出?

2 个答案:

答案 0 :(得分:3)

如果要以编程方式运行异步进程,请执行此操作。 也就是说,使用较低级别的功能。

(set-process-sentinel (start-process "pdflatex"
                                     "*async pdflatex*"
                                     "pdflatex" filename)
                      (lambda (process event)
                        (cond ((string-match-p "finished" event)
                               (kill-buffer "*async pdflatex*"))
                              ((string-match-p "\\(exited\\|dumped\\)" event)
                               (message "Something wrong happened while running pdflatex")
                               (when (yes-or-no-p "Something wrong happened while running pdflatex, see the errors?")
                                 (switch-to-buffer "*async pdflatex*"))))))

start-process以异步方式启动进程,set-process-sentinel定义进程状态更改时触发的函数。

答案 1 :(得分:2)

以下是工作代码:

(defun latex-sentinel (process event)
      (message event)
      (cond ((string-match-p "finished" event)
                  (progn
                        (kill-buffer "*async pdflatex*")
                        (message "pdflatex done")))))

(defun latex-compile ()
      "Runs pdflatex on current file"
      (interactive)
      (let* ((file-name (shell-quote-argument (buffer-file-name)))
            (process (start-process-shell-command
                           "pdflatex"
                           "*async pdflatex*"
                           (concat "pdflatex " file-name))))
            (set-process-sentinel process 'latex-sentinel)))