Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs function after asynchonous command

I currently use emacs for making and editing LaTeX documents. When compiling, I use an external program to compile into pdf. Right now, with the following code in my .emacs file, emacs will start compiling the document into a pdf whenever I save the file.

(defun auto-compile-latex ()
  (save-window-excursion
    (async-shell-command (format "cd %s; scons -u" default-directory))))
(add-hook 'LaTeX-mode-hook '(lambda ()
     (add-hook 'after-save-hook 'auto-compile-latex nil 'make-it-local)))

I prefer this over M-x compile, because I am more in the habit of saving, and it launches in the background, allowing me to continue working. However, I do not get a prominent notification when the compilation process finishes.

Ideally, I would want to run the following function whenever a compilation process finishes.

(defun latex-compilation-status (exit-code)
  (if (/= exit-code 0)
      (setq mode-name (propertize mode-name 'face 'font-lock-warning-face))
    (setq mode-name (propertize mode-name 'face 'mode-line-highlight))))

That way, I can have the color in the mode line automatically change depending on whether the compilation was successful or not. However, looking through the emacs documentation, I have not found any mention of a hook that gets run after async-shell-command completes. I know that there is the message in the minibuffer stating the exit status of the subprocess, but if I am typing at the time, it is often hard to notice.

Alternatively, I could wait for the shell command to complete, then change the color immediately. However, this then makes the entirety of emacs freeze while compiling, which is not desired.

How would I go about having this indication applied at the end of the compilation, without having emacs freeze during the process?

like image 531
Eldritch Cheese Avatar asked Jan 24 '26 19:01

Eldritch Cheese


2 Answers

You should try the command async-start from https://github.com/jwiegley/emacs-async

like image 104
Nicolas Dudebout Avatar answered Jan 28 '26 10:01

Nicolas Dudebout


Thank you. I ended up getting it using a modified version of lawlist's code. This now has changes the color when starting to compile, then changes it again to indicate success or failure.

;Automatically compile any latex documents when saved.
(defun auto-compile-latex ()
  (setq mode-name (propertize mode-name 'face 'font-lock-string-face))
  (set-process-sentinel
   (start-process-shell-command "latex-compile" "latex-compile"
                                (format "cd %s; scons -u" default-directory))
   'latex-compile-sentinel))
;Change the color after compilation.  Still need to find the right hook to add it to.
(defun latex-compile-sentinel (process event)
  (if (string-equal event "finished\n")
      (setq mode-name (propertize mode-name 'face 'mode-line-highlight))
    (setq mode-name (propertize mode-name 'face 'font-lock-warning-face))))
;Hooks for latex-mode
(add-hook 'LaTeX-mode-hook '(lambda ()
   (add-hook 'after-save-hook 'auto-compile-latex nil 'make-it-local)))

As an aside, the emacs-async package did not work for this use. I assume that this is because emacs-async starts the secondary functions in a separate process, with some of the variables not propagated back to the parent process.

like image 25
Eldritch Cheese Avatar answered Jan 28 '26 10:01

Eldritch Cheese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!