branch: externals/auctex-cont-latexmk
commit 146dfe235088049acc20949eee0d2c30fd384fad
Merge: 447b9f3be2 d052e767e6
Author: Paul Nelson <ultr...@gmail.com>
Commit: Paul Nelson <ultr...@gmail.com>

    Merge remote-tracking branch 'origin/main'
---
 .gitignore         |  1 +
 README.org         | 23 +++++++++++++++++++++++
 czm-tex-compile.el | 47 ++++++++++++++++++++++++++++++++---------------
 3 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/.gitignore b/.gitignore
index 206569dc66..7ab62451bb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,3 +9,4 @@
 
 # Undo-tree save-files
 *.~undo-tree
+/*.html
diff --git a/README.org b/README.org
new file mode 100644
index 0000000000..f43bd2fce5
--- /dev/null
+++ b/README.org
@@ -0,0 +1,23 @@
+#+title: czm-preview.el: Convenience functions for compiling LaTeX
+#+author: Paul Nelson
+
+* Overview
+This package provides convenience functions that support a LaTeX workflow 
where =latexmk= continuously compiles the document in the background.
+
+* Configuration
+Download this repository, install using =M-x package-install-file= (or 
package-vc-install, straight, elpaca, ...), and add something like the 
following to your [[https://www.emacswiki.org/emacs/InitFile][init file]]:
+#+begin_src elisp
+(use-package czm-tex-compile
+    :bind
+    ("C-c k" . czm-tex-compile)
+    ("s-]" . czm-tex-compile-next-error)
+    ("s-[" . czm-tex-compile-previous-error))
+#+end_src
+Replace the keybindings with whatever you prefer (or delete them and just run 
the commands via =M-x=).
+
+* Usage
+
+- If you run =czm-tex-compile= in a TeX buffer visiting "foo.tex", it starts 
an =eshell= buffer =*eshell-foo*= in the background that continuously compiles 
the current document.  Use =M-x customize-variable czm-tex-compile-command= to 
customize the compilation command.  If you run the same command again, then it 
switches to the =eshell= buffer.
+- =czm-tex-compile-next-error= and =czm-tex-compile-previous-error= navigate 
the error and warning messages encountered in the log file produced by 
=latexmk=, jumping to the corresponding positions in the tex buffer.
+
+That's all.  I prefer this workflow to the alternative in which one compiles 
the document manually via =TeX-command-master= (=C-c C-c=) and navigates the 
warning/error messages using =next-error= (=M-n=) and =previous-error (=M-p=).  
It also gives a handy way to keep the .aux files up-to-date; I take advantage 
of this feature in the packages 
[[https://github.com/ultronozm/czm-preview.el][czm-preview.el]] and 
[[https://github.com/ultronozm/czm-tex-fold.el][czm-tex-fold.el]] to annotate 
th [...]
diff --git a/czm-tex-compile.el b/czm-tex-compile.el
index 5553dfefa1..eafc85f0c9 100644
--- a/czm-tex-compile.el
+++ b/czm-tex-compile.el
@@ -1,11 +1,11 @@
-;;; czm-tex-compile.el --- Convenience functions compiling LaTeX  -*- 
lexical-binding: t; -*-
+;;; czm-tex-compile.el --- Convenience functions for compiling LaTeX  -*- 
lexical-binding: t; -*-
 
 ;; Copyright (C) 2023  Paul D. Nelson
 
 ;; Author: Paul D. Nelson <nelson.paul.da...@gmail.com>
 ;; Version: 0.0
 ;; URL: https://github.com/ultronozm/czm-tex-compile.el
-;; Package-Requires: ((emacs "29.1") (auctex "11.86.1"))
+;; Package-Requires: ((emacs "29.1"))
 ;; Keywords: tex
 
 ;; This program is free software; you can redistribute it and/or modify
@@ -50,6 +50,17 @@
   :type 'string
   :group 'czm-tex-compile)
 
+(defun czm-tex-compile--kill-buffer-hook ()
+  "Hook to kill the eshell buffer when the LaTeX buffer is killed."
+  (when (string-match "\\([^\.]+\\)\.tex" (buffer-name))
+    (let* ((name (match-string 1 (buffer-name)))
+           (bufname (concat "*eshell-" name "*")))
+      (when (get-buffer bufname)
+        (let ((kill-buffer-query-functions '()))
+          (with-current-buffer bufname
+            (eshell-interrupt-process))
+          (kill-buffer bufname))))))
+
 ;;;###autoload
 (defun czm-tex-compile ()
   "Compile the current LaTeX document in an eshell buffer.
@@ -64,6 +75,7 @@ name of the current LaTeX file."
       (if (get-buffer bufname)
          (switch-to-buffer bufname)
        (save-window-excursion
+          (add-hook 'kill-buffer-hook #'czm-tex-compile--kill-buffer-hook)
          (eshell "new")
          (rename-buffer bufname)
          (insert (concat czm-tex-compile-command " " name ".tex"))
@@ -84,23 +96,22 @@ Used for navigating LaTeX warnings in the log file."
 
 ;; TODO: look for the line <name>.bbl in the file, and don't jump to
 ;; line numbers found in log entries beyond that point (just display
-;; them).  Also, use insert-file-contents rather than
-;; find-file-noselect, etc.  Similarly, do the same for .aux files in
-;; your other packages (tex-util, preview).
+;; them).
+
+(defvar czm-tex-compile--debug nil
+  "Whether to print debugging information.")
 
 (defun czm-tex-compile--navigate-log-error (direction)
   "Helper function to navigate warnings in the log file.
 DIRECTION should be either \='next or \='previous."
   (let* ((tex-file (buffer-file-name))
-               (log-file (concat (file-name-sans-extension tex-file)
-                           ".log"))
-               (already-open (find-buffer-visiting log-file))
-               (buf (or already-open (find-file-noselect log-file)))
-               (file-modification-time (nth 5 (file-attributes log-file)))
-               (last-navigation-time (car czm-tex-compile--log-state))
-               (log-pos (cdr czm-tex-compile--log-state))
-               line description)
-    (with-current-buffer buf
+        (log-file (concat (file-name-sans-extension tex-file) ".log"))
+        (file-modification-time (nth 5 (file-attributes log-file)))
+        (last-navigation-time (car czm-tex-compile--log-state))
+        (log-pos (cdr czm-tex-compile--log-state))
+        line description)
+    (with-temp-buffer
+      (insert-file-contents log-file)
       (save-excursion)
       (if (or (null last-navigation-time)
                     (time-less-p last-navigation-time file-modification-time))
@@ -171,7 +182,13 @@ DIRECTION should be either \='next or \='previous."
           (widen))
         (goto-char pos)
         (recenter)))
-    (message (or description "No further errors or warnings."))))
+    (message
+     (concat (or description "No further errors or warnings.")
+             (when czm-tex-compile--debug
+               " -- "
+               (format "%s" (cdr czm-tex-compile--log-state)))))))
+
+
 
 ;;;###autoload
 (defun czm-tex-compile-previous-error ()

Reply via email to