How to test if the current file has changed in elisp compatible with latexmk

479 views Asked by At

I am using the following function to run latexmk on my latex-file in emacs:

(defun my-run-latex ()
  (interactive)
  (if (buffer-modified-p)
      (progn  
        (setq TeX-save-query nil) 
        (TeX-save-document (TeX-master-file))
        (TeX-command "Latexmk" 'TeX-master-file -1))
    (TeX-view)))

(Taken from https://stackoverflow.com/a/14699078/406686).

Suppose I have a simple document (test.tex) with some errors in it like:

\documentclass{article}

\begin{document}
\error1
\error2
\end{document}

Now if I press for example Space and then Backspace (or doing any change and undo it) and then run my-run-latex latexmk runs and says that all targets are up to date. The problem is that then I loose the error list, so TeX-next-error will not have any effect.

I guess the problem could be solved by replacing (buffer-modified-p) by something which prevents to run latexmk in this case (best by the same test latexmk does to check if the file changed since the last run). Any idea how to do this?

1

There are 1 answers

3
giordano On BEST ANSWER

latexmk uses hashing to determine whether a file was changed. The hash algorithm used is md5, it isn't completely secure but this isn't really important in this respect. So you can use a hash-based test instead of (buffer-modified-p). The following code should work:

(setq current-buffer-hash nil)
(make-variable-buffer-local 'current-buffer-hash)
(defun my-run-latex ()
  (interactive)
  (if (equal current-buffer-hash
         (setq current-buffer-hash (secure-hash 'md5 (current-buffer))))
      (TeX-view)
    (setq TeX-save-query nil)
    (TeX-save-document (TeX-master-file))
    (TeX-command "Latexmk" 'TeX-master-file -1)))

As pointed out by @student, the function secure-hash was introduced in Emacs 24.2. For previous versions one can use (md5 (current-buffer)).