* lisp/ox-latex.el (org-latex-patch-synctex): New function
(org-latex-export-to-pdf): Call it, if appropriate

This teaches the latex backend how to patch synctex files.  The patch
will be done automatically if exporting directly to pdf.  If not, the
user must call the org-latex-patch-synctex function (because it relies
on the synctex file, which will only be generated when pdflatex is run)

Touch the file after modifying the synctex, to trigger the pdf viewer to
re-read it.
---
 lisp/ox-latex.el | 64 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 8 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 8727adc..43c5fde 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -115,6 +115,7 @@
 (eval-when-compile (require 'cl))
 (require 'ox)
 (require 'ox-publish)
+(require 'dired)                       ; for dired-touch-program
 
 (defvar org-latex-default-packages-alist)
 (defvar org-latex-packages-alist)
@@ -1150,6 +1151,43 @@ just outside of it."
      (funcall search-refs element))
    ""))
 
+(defun org-latex-patch-synctex (&optional subtreep)
+  (cond
+   ((not org-export-concordance)
+        (message "No concordance, not patching."))
+   ((not (file-exists-p
+         (org-export-output-file-name ".synctex.gz" subtreep)))
+    (message "No synctex file found, not patching."))
+   (t
+    (let* ((conc org-export-concordance)
+          (file-base (org-export-output-file-name "." subtreep))
+           (buf (find-file-noselect (concat file-base "synctex.gz"))))
+      (with-current-buffer buf
+       (let ((max-index 0)
+             the-index extra-path)
+         (goto-char (point-min))
+         (while (re-search-forward "^Input:\\([0-9]+\\):" nil t)
+           (setq max-index (max max-index (string-to-int (match-string 1)))))
+         (goto-char (point-min))
+         (when (re-search-forward (concat "^Input:\\([0-9]+\\):\\(.*\\)"
+                                          (regexp-quote file-base) "tex$")
+                                  nil t)
+           (setq the-index (string-to-int (match-string 1)))
+           (setq extra-path (match-string 2))
+           (goto-char (line-end-position))
+           (insert (format "\nInput:%s:%s%sorg" (1+ max-index) extra-path 
file-base)))
+         (goto-char (point-min))
+         (while (re-search-forward (format "^[vhxkgr$[)]\\(%s\\),\\([0-9]+\\):"
+                                           the-index)
+                                   nil t)
+            (replace-match (int-to-string (1+ max-index)) nil t nil 1)
+           (replace-match
+            (int-to-string
+             (org-export--read-concordance conc (string-to-int (match-string 
2))))
+            nil t nil 2))
+         (save-buffer)))
+      (kill-buffer buf)))))
+
 
 
 ;;; Template
@@ -2918,14 +2956,24 @@ Return PDF file's name."
       (let ((outfile (org-export-output-file-name ".tex" subtreep)))
        (org-export-async-start
            (lambda (f) (org-export-add-to-stack f 'latex))
-         `(expand-file-name
-           (org-latex-compile
-            (org-export-to-file
-             'latex ,outfile ,subtreep ,visible-only ,body-only
-             ',ext-plist)))))
-    (org-latex-compile
-     (org-latex-export-to-latex
-      nil subtreep visible-only body-only ext-plist))))
+         `(let ((pdf-file
+                 (expand-file-name
+                  (org-latex-compile
+                   (org-export-to-file
+                    'latex ,outfile ,subtreep ,visible-only ,body-only
+                    ',ext-plist)))))
+            (when org-export-with-concordance
+              (org-latex-patch-synctex subtreep)
+              (call-process dired-touch-program nil nil nil pdf-file))
+            pdf-file)))
+    (let ((pdf-file
+          (org-latex-compile
+           (org-latex-export-to-latex
+            nil subtreep visible-only body-only ext-plist))))
+      (when org-export-with-concordance
+       (org-latex-patch-synctex subtreep)
+        (call-process dired-touch-program nil nil nil pdf-file))
+      pdf-file)))
 
 (defun org-latex-compile (texfile &optional snippet)
   "Compile a TeX file.
-- 
1.8.2


Reply via email to