等待编译完成

时间:2013-08-14 19:56:56

标签: emacs elisp

我正在使用compile从源代码树中使用mercurial“hg pull”提取新文件。 我在执行拉取之前执行所有缓冲区的保存,并希望在编译“拉”完成后“刷新所有打开的缓冲区”。 我尝试了编译完成函数,但发现添加到列表中的函数将在“每次”编译后执行。由于我使用编译来搜索ID“gid”,我不想在每次搜索时刷新打开的文件。 如何在编译内部“仅”刷新打开的文件之前等待编译完成,而不是在命令之外的每个编译中。 这是代码:

; From http://www.emacswiki.org/emacs/CompileCommand
(defun compile-pkg (&optional command startdir)
  "Compile a package, moving up to the parent directory
  containing configure.ac, if it exists. Start in startdir if defined,
  else start in the current directory."
  (interactive)
  (let ((dirname) (dir-buffer nil))
    (setq startdir (expand-file-name (if startdir startdir ".")))
    (setq command  (if command command compile-command))
    (setq dirname (upward-find-file "Makefile" startdir))
;    (setq dirname (if dirname dirname (upward-find-file "Makefile" startdir)))
;    (setq dirname (if dirname dirname (expand-file-name ".")))
    ; We've now worked out where to start. Now we need to worry about
    ; calling compile in the right directory
    (save-excursion
      (setq dir-buffer (find-file-noselect dirname))
      (set-buffer dir-buffer)
      (compile command)
      (kill-buffer dir-buffer)
      )))

(defun upward-find-file (filename &optional startdir)
  "Move up directories until we find a certain filename. If we
  manage to find it, return the containing directory. Else if we
  get to the toplevel directory and still can't find it, return
  nil. Start at startdir or . if startdir not given"
  (let ((dirname (expand-file-name
                  (if startdir startdir ".")))
        (found nil) ; found is set as a flag to leave loop if we find it
        (top nil))  ; top is set when we get
                    ; to / so that we only check it once
    ; While we've neither been at the top last time nor have we found
    ; the file.
    (while (not (or found top))
      ; If we're at / set top flag.
      (if (string= (expand-file-name dirname) "/")
          (setq top t))
      ; Check for the file
      (if (file-exists-p (expand-file-name filename dirname))
          (setq found t)
        ; If not, move up a directory
        (setq dirname (expand-file-name ".." dirname))))
    ; return statement
    (if found (concat dirname "/") nil)))

(defun compile-hgpull ()
  (interactive)
  (save-all-buffers)
  (compile-pkg "hg pull -u")
; if (compile finished) -> (revert-all-buffers)
  )

(global-set-key [f1]    'compile-hgpull) 

2 个答案:

答案 0 :(得分:1)

编译是异步的。所以,你有两个选择。

一,不要使用编译。而是使用其他方法之一来调用shell命令,例如shell-command或start-process或call-process。我认为这可能是首选;我不明白为什么你需要在这里使用编译。

二,设置编译完成功能。

答案 1 :(得分:0)

如果要同步运行shell命令,然后查看其输出,则使用shell-command-to-string可能比compile更容易。