3

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?

4

4 回答 4

1

您应该尝试async-start来自https://github.com/jwiegley/emacs-async的命令

于 2013-09-13T22:57:52.533 回答
0

set-process-sentinel这是基于先前有用答案的另一个选项Francesco。但是,请务必小心使用let绑定来定义 a start-process([在我的非专业人士看来] 是明确的“不,不”),因为这会导致该过程在其时间之前立即开始。[仅供参考: Nicolas过去让我摆脱了很多麻烦,所以请务必仔细看看他的解决方案。]

Emacs:如果 latexmk 完成正常,则显示 pdf,否则显示错误

您只需将您的功能添加到处理成功的功能的最后一部分 - 即(when (= 0 (process-exit-status p)) . . .

(defun latexmk ()
  ".latexmkrc contains the following entries (WITHOUT the four backslashes):
  $pdflatex = 'pdflatex -file-line-error -synctex=1 %O %S && (cp \"%D\" \"%R.pdf\")';
  $pdf_mode = 1;
  $out_dir = '/tmp';"
(interactive)
  (setq tex-file buffer-file-name)
  (setq pdf-file (concat "/tmp/" (car (split-string
    (file-name-nondirectory buffer-file-name) "\\.")) ".pdf"))
  (setq line (format "%d" (line-number-at-pos)))
  (setq skim "/Applications/Skim.app/Contents/SharedSupport/displayline")
  (setq tex-output (concat "*" (file-name-nondirectory buffer-file-name) "*") )
  (setq latexmk "/usr/local/texlive/2012/texmf-dist/scripts/latexmk/latexmk.pl")
  (setq latexmkrc "/Users/HOME/.0.data/.0.emacs/.latexmkrc")
  (if (buffer-modified-p)
    (save-buffer))
  (delete-other-windows)
  (set-window-buffer (split-window-horizontally) (get-buffer-create tex-output))
  (with-current-buffer tex-output (erase-buffer))
  (set-process-sentinel 
    (start-process "compile" tex-output latexmk "-r" latexmkrc tex-file)
    (lambda (p e) (when (= 0 (process-exit-status p))
      (start-process "displayline" nil skim "-b" line pdf-file tex-file)
      (switch-to-buffer (get-file-buffer tex-file))
      (if (get-buffer-process (get-buffer tex-output))
        (process-kill-without-query (get-buffer-process (get-buffer tex-output))))
      (kill-buffer tex-output)
      (delete-other-windows)))))
于 2013-09-14T02:35:39.773 回答
0

你也可以安排你auto-compile-latex的跑步compile

于 2013-09-16T02:38:31.480 回答
0

谢谢你。我最终使用修改版的 lawlist 代码得到了它。这现在在开始编译时改变了颜色,然后再次改变它以指示成功或失败。

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

顺便说一句,emacs-async 包不适用于此用途。我认为这是因为 emacs-async 在单独的进程中启动辅助函数,其中一些变量没有传播回父进程。

于 2013-09-14T22:04:11.700 回答