user4035
user4035

Reputation: 23729

Elisp close *Async Shell Command* window after the command finishes

I made a function, that runs pdflatex compiler to produce a pdf file for the currently edited tex file:

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

Is it possible to close the window *Async Shell Command* after the process exits, if it exited successfully?

Upvotes: 0

Views: 518

Answers (2)

user4035
user4035

Reputation: 23729

Here is the working code:

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

Upvotes: 2

Daimrod
Daimrod

Reputation: 5020

If you want to run asynchronous process programmatically go for it. That is, use the lower level functions.

(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 starts a process asynchronously and set-process-sentinel defines the function triggered when the process status changes.

Upvotes: 3

Related Questions