emacs - 如何在与latexmk兼容的elisp中测试当前文件是否已更改

标签 emacs latex elisp auctex latexmk

我正在使用以下函数在 emacs 中的 latex 文件上运行 latexmk:

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

(摘自 https://stackoverflow.com/a/14699078/406686 )。

假设我有一个简单的文档 (test.tex),其中有一些错误,例如:

\documentclass{article}

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

现在,如果我按例如空格键,然后按退格键(或进行任何更改并撤消它),然后运行 ​​my-run-latex latexmk 运行并说所有目标都是最新的。问题是我松开了错误列表,所以 TeX-next-error 不会有任何效果。

我想这个问题可以通过替换 (buffer-modified-p) 来解决,在这种情况下可以防止运行 latexmk(最好通过相同的测试 latexmk 检查文件是否更改自上次运行以来)。知道如何做到这一点吗?

最佳答案

latexmk 使用 hashing以确定文件是否已更改。使用的散列算法是md5 ,它并不完全安全,但这在这方面并不是很重要。所以你可以使用 hash-based测试而不是 (buffer-modified-p)。以下代码应该有效:

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

正如@student 所指出的,secure-hash 函数是在 Emacs 24.2 中引入的。对于以前的版本,可以使用 (md5 (current-buffer))

关于emacs - 如何在与latexmk兼容的elisp中测试当前文件是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18135366/

相关文章:

Emacs org-capture 模板在行首扩展

emacs - Emacs Lisp 的 lexical-let 何时会泄漏内存?

latex - 每个报价的垂直线

file - 如何在 emacs 中创建空文件?

emacs - 如何向组织模式时间戳添加秒数

matlab - 如何在组织模式下在同一文件中定义matlab函数

Python Pweave 到 LaTeX

latex - LaTeX 中的定理编号

emacs - 在括号对后将 Emacs 设置为智能自动行?

emacs - 更改 emacs 文本模式的边距