Search code examples
emacselisp

Wait on shell command to finish before executing another elisp command


I am trying to do the following:

  1. open a shell in emacs and cd into project folder
  2. Execute a mercurial pull from repository.
  3. "wait" until the pull finishes
  4. Communicate that the pull is finished to elisp command which fired up the shell command to begin with so it could carry on with compile command to build the tree.

is there a way/code that would let me know when the shell command has finished before starting a new elisp command from inside .emacs file?


Solution

  • @Francesco previously answered a similar question at the following link: https://stackoverflow.com/a/18707182/2112489

    I discovered that using start-process as a let-bound variable caused it to be run immediately, rather than waiting until the more desirable moment further on down in the function. I also discovered that set-process-sentinel prevents using let-bound variables previously defined. Rather than using let-bound variables or global variables, another option would be to use a buffer-local variable -- e.g., (defvar my-local-variable nil "This is my local variable.") (make-variable-buffer-local 'my-local-variable) -- and set it within the function -- e.g., (setq my-local-variable "hello-world"). Although most examples that I have seen factor out the sentinel, I like to include everything into just one function -- e.g., such as the following snippet demonstrates:

    (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 . . . ))))))))