Code cleanup (no more byte compile warnings!)

This commit is contained in:
TEC 2023-07-05 22:52:46 +08:00
parent a68eb8ded2
commit fe4329c71a
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 297 additions and 266 deletions

View File

@ -23,6 +23,68 @@
(require 'cl-lib)
(require 'eieio))
(require 'ox)
(require 'ox-latex)
(require 'transient)
(require 'magit)
;; Customisations
(defgroup orgdiff nil
"Generating Org document diffs."
:group 'org
:prefix "orgdiff-")
(defcustom orgdiff-latexdiff-postprocess-hooks nil
"Post-processing functions which are run on the latexdiff result."
:type 'hook)
(defcustom orgdiff-latexdiff-executable "latexdiff"
"Path to the latexdiff executable to use on the generated tex files."
:type 'string)
(defcustom orgdiff-latex-compiler-priorities
'("pdflatex" "xelatex" "lualatex")
"A list of compiler priorities, ascending.
If two documents require different compilers, the higher priority
compiler will be used."
:type '(repeat string))
(defcustom orgdiff-latex-compile-command
(delq nil (list "latexmk"
"-f"
"-pdf"
"-%compiler"
(and (string-match-p "-shell-escape" (car org-latex-pdf-process))
"-shell-escape")
"-interaction=nonstopmode"
"%texfile"))
"Compile command as a list in the form (\"CMD\" \"ARGS\"...).
\"%compiler\" is replaced with the requested compiler, and
\"%texfile\" replaced with the path to the .tex file."
:type '(repeat string))
;; Helper variables
(defvar orgdiff--base-dir nil "The current directory for diffing files.")
(defvar orgdiff--rev1 nil "The first revision.")
(defvar orgdiff--export-processes nil)
(defvar orgdiff--latexdiff-file-1 nil)
(defvar orgdiff--latexdiff-file-2 nil)
(defvar orgdiff--latexdiff-flatten nil)
(defvar orgdiff--rev1dir nil)
(defvar orgdiff--rev2dir nil)
(defvar orgdiff--rev1file nil)
(defvar orgdiff--rev2file nil)
(defvar orgdiff--difffile nil)
(defvar orgdiff--export-processes nil)
;; Transient setup
(defclass orgdiff--transient-lisp-variable-formatted (transient-variable)
((reader :initform #'transient-lisp-variable--reader)
(always-read :initform t)
@ -58,78 +120,76 @@
(defmacro orgdiff--define-infix (key name description type default
&rest reader)
"Define infix with KEY, NAME, DESCRIPTION, TYPE, DEFAULT and READER as arguments."
`(progn
(defcustom ,(intern (concat "orgdiff-" name)) ,default
,description
:type ,type
:group 'orgdiff)
(transient-define-infix ,(intern (concat "orgdiff--set-" name)) ()
"Set `orgdiff--theme' from a popup buffer."
:class 'orgdiff--transient-lisp-variable-formatted
:variable ',(intern (concat "orgdiff-" name))
:key ,key
:description ,description
:argument ,(concat "--" name)
:reader (lambda (&rest _) ,@reader))))
(declare (indent 5) (doc-string 3))
(let ((infix-var (intern (format "orgdiff-%s" name))))
`(progn
(defcustom ,infix-var ,default
,(concat description ".")
:type ,type
:group 'orgdiff)
(transient-define-infix ,(intern (format "orgdiff--set-%s" name)) ()
,(format "Set `%s' from a popup buffer." infix-var)
:class 'orgdiff--transient-lisp-variable-formatted
:variable ',infix-var
:key ,key
:description ,description
:argument ,(format "--%s" name)
:reader (lambda (&rest _) ,@reader))))))
(orgdiff--define-infix
"-1" "file-1" "First file"
'(choice string boolean) nil
(read-file-name "First file: " nil nil t nil
(lambda (f)
(or (equal (file-name-extension f) "org")
(string-match-p "/$" f)))))
(orgdiff--define-infix
"-1" file-1 "First file"
'(choice string boolean) nil
(read-file-name "First file: " nil nil t nil
(lambda (f)
(or (equal (file-name-extension f) "org")
(string-match-p "/$" f)))))
(orgdiff--define-infix
"-2" "file-2" "Second file"
'(choice string boolean) nil
(read-file-name "Second file: " nil orgdiff-file-2 t nil
(lambda (f)
(or (and (equal (file-name-extension f) "org")
(message f)
(not (string= f orgdiff-file-1)))
(string-match-p "/$" f)))))
(orgdiff--define-infix
"-2" file-2 "Second file"
'(choice string boolean) nil
(read-file-name "Second file: " nil orgdiff-file-2 t nil
(lambda (f)
(or (and (equal (file-name-extension f) "org")
(message f)
(not (string= f orgdiff-file-1)))
(string-match-p "/$" f)))))
(orgdiff--define-infix
"-r" "git-revisions" "Git revision"
'(choice string boolean) nil
(run-at-time
nil nil
(lambda ()
(transient--suspend-override)
(magit-log-select
(lambda (r1)
(let ((r1tag (magit-rev-name r1)))
(when r1tag (setq r1tag (replace-regexp-in-string "tags/" "" r1tag)))
(setq orgdiff--rev1 (or r1tag r1)))
(magit-log-select
(lambda (r2)
(let ((r2tag (magit-rev-name r2)))
(when r2tag (setq r2tag (replace-regexp-in-string "tags/" "" r2tag)))
(setq r2 (or r2tag r2)))
(setq orgdiff-git-revisions (substring-no-properties (concat orgdiff--rev1 ".." r2)))
(transient--resume-override)
(orgdiff-transient))
(concat (propertize "Second revision (" 'face 'transient-heading)
(propertize "newer" 'face 'transient-argument)
(propertize "): " 'face 'transient-heading)
"type %p to select commit at point, %q to use the current working state instead")
(lambda ()
(setq orgdiff-git-revisions (substring-no-properties orgdiff--rev1))
(transient--resume-override)
(orgdiff-transient)
)))
(concat (propertize "First revision (" 'face 'transient-heading)
(propertize "older" 'face 'transient-argument)
(propertize "): " 'face 'transient-heading)
"type %p to select commit at point, %q to not compare any revisions")
(lambda ()
(transient--resume-override)
(orgdiff-transient)))))
nil))
(defvar orgdiff--base-dir nil "The current directory for diffing files.")
(defvar orgdiff--rev1 nil "The first revision.")
(orgdiff--define-infix
"-r" git-revisions "Git revision"
'(choice string boolean) nil
(run-at-time
nil nil
(lambda ()
(transient--suspend-override)
(magit-log-select
(lambda (r1)
(let ((r1tag (magit-rev-name r1)))
(when r1tag (setq r1tag (replace-regexp-in-string "tags/" "" r1tag)))
(setq orgdiff--rev1 (or r1tag r1)))
(magit-log-select
(lambda (r2)
(let ((r2tag (magit-rev-name r2)))
(when r2tag (setq r2tag (replace-regexp-in-string "tags/" "" r2tag)))
(setq r2 (or r2tag r2)))
(setq orgdiff-git-revisions (substring-no-properties (concat orgdiff--rev1 ".." r2)))
(transient--resume-override)
(orgdiff--transient))
(concat (propertize "Second revision (" 'face 'transient-heading)
(propertize "newer" 'face 'transient-argument)
(propertize "): " 'face 'transient-heading)
"type %p to select commit at point, %q to use the current working state instead")
(lambda ()
(setq orgdiff-git-revisions (substring-no-properties orgdiff--rev1))
(transient--resume-override)
(orgdiff--transient))))
(concat (propertize "First revision (" 'face 'transient-heading)
(propertize "older" 'face 'transient-argument)
(propertize "): " 'face 'transient-heading)
"type %p to select commit at point, %q to not compare any revisions")
(lambda ()
(transient--resume-override)
(orgdiff--transient)))))
nil)
;;;###autoload
(defun orgdiff ()
@ -171,9 +231,9 @@
(setq orgdiff-git-revisions nil)))
(setq orgdiff-git-revisions nil))
(orgdiff-transient))
(orgdiff--transient))
(transient-define-prefix orgdiff-transient ()
(transient-define-prefix orgdiff--transient ()
["Files"
(orgdiff--set-file-1)
(orgdiff--set-file-2)]
@ -191,7 +251,7 @@
(setq orgdiff-file-1 orgdiff-file-2
orgdiff-file-2 old1))
(unless no-transient
(orgdiff-transient)))
(orgdiff--transient)))
;;; simple diff
@ -199,7 +259,7 @@
(interactive)
(when (and orgdiff-file-2 (not orgdiff-file-1))
(orgdiff--swap-files t))
(orgdiff-extract-revisions)
(orgdiff--extract-revisions)
(ediff-files orgdiff--rev1file orgdiff--rev2file))
;;; latexdiff
@ -207,101 +267,108 @@
(defun orgdiff--latexdiff ()
(interactive)
(when orgdiff--export-processes
(user-error "A latexdiff is currently in progress. Try `orgdiff-latexdiff-abort' if something seems wrong."))
(user-error "A latexdiff is currently in progress. Try `orgdiff--latexdiff-abort' if something seems wrong."))
(when (and orgdiff-file-2 (not orgdiff-file-1))
(orgdiff--swap-files t))
(setq orgdiff-latexdiff-file-1 (when orgdiff-file-1 (concat (file-name-sans-extension orgdiff-file-1) ".tex"))
orgdiff-latexdiff-file-2 (when orgdiff-file-2 (concat (file-name-sans-extension orgdiff-file-2) ".tex"))
orgdiff-latexdiff-flatten (when (or orgdiff-git-revisions
(not (and orgdiff-file-1 orgdiff-file-2
(string= (file-name-directory orgdiff-file-1)
(file-name-directory orgdiff-file-2)))))
t))
(orgdiff-latexdiff-transient))
(setq orgdiff--latexdiff-file-1
(and orgdiff-file-1
(concat (file-name-sans-extension orgdiff-file-1) ".tex"))
orgdiff--latexdiff-file-2
(and orgdiff-file-2
(concat (file-name-sans-extension orgdiff-file-2) ".tex"))
orgdiff--latexdiff-flatten
(and (or orgdiff-git-revisions
(not (and orgdiff-file-1 orgdiff-file-2
(string= (file-name-directory orgdiff-file-1)
(file-name-directory orgdiff-file-2)))))
t))
(orgdiff--latexdiff-transient))
(defface orgdiff-latexdiff-red
(defface orgdiff--latexdiff-red
'((t :foreground "#cd3d3e"))
"Used to preview latexdiff's red.")
(defface orgdiff-latexdiff-blue
(defface orgdiff--latexdiff-blue
'((t :foreground "#4078f2"))
"Used to preview latexdiff's red.")
(setq orgdiff-latexdiff-flags
`(("--type"
(UNDERLINE .
,(concat (propertize "discarded" 'face '((:strike-through t) orgdiff-latexdiff-red))
" " (propertize "added" 'face 'orgdiff-latexdiff-blue)
" " (propertize "(default)" 'face 'shadow)))
(CTRADITIONAL .
,(concat (propertize "[.."'face 'orgdiff-latexdiff-red)
(propertize "1"'face '((:height 0.6) orgdiff-latexdiff-red))
(propertize "]"'face 'orgdiff-latexdiff-red)
" " (propertize "added" 'face '((:height 1.15) variable-pitch orgdiff-latexdiff-blue))))
(TRADITIONAL .
,(concat "[.." (propertize "1" 'face '(:height 0.6)) "]"
" " (propertize "added" 'face '((:height 1.15) variable-pitch))))
(CFONT .
,(concat (propertize "discarded" 'face '((:height 0.7) orgdiff-latexdiff-red))
" " (propertize "added" 'face '((:height 1.15) variable-pitch orgdiff-latexdiff-blue))))
(FONTSTRIKE .
,(concat (propertize "discarded" 'face '((:strike-through t) small))
" " (propertize "added" 'face '((:height 1.15) variable-pitch))))
(CHANGEBAR . ,(propertize "no markup, change marks in margins" 'face 'font-lock-doc-face))
(CCHANGEBAR .
,(concat (propertize "CHANGEBAR" 'face 'font-lock-function-name-face)
(propertize " + " 'face 'font-lock-comment-face)
(propertize "discarded" 'face 'orgdiff-latexdiff-red)
" " (propertize "added" 'face 'orgdiff-latexdiff-blue)))
(CFONTCHBAR .
,(concat (propertize "CHANGEBAR" 'face 'font-lock-function-name-face)
(propertize " + " 'face 'font-lock-comment-face)
(propertize "discarded" 'face '(small orgdiff-latexdiff-red))
" " (propertize "added" 'face '((:height 1.15) variable-pitch orgdiff-latexdiff-blue))))
(CULINECHBAR .
,(concat (propertize "CHANGEBAR" 'face 'font-lock-function-name-face)
(propertize " + " 'face 'font-lock-comment-face)
(propertize "discarded" 'face '((:strike-through t) orgdiff-latexdiff-red))
" " (propertize "added" 'face 'orgdiff-latexdiff-blue)))
(INVISIBLE . "added")
(BOLD .
,(propertize "added" 'face 'bold))
(PDFCOMMENT .
,(concat (propertize "discarded text in PDF comment, " 'face 'font-lock-doc-face) (propertize "added" 'face 'underline))))
("--subtype"
(SAFE . ,(propertize "No additional markup (default, reccomended)" 'face 'font-lock-doc-face))
(MARGIN . ,(propertize "Mark start and end of changed block with symbol in margin" 'face 'font-lock-doc-face))
(COLOR .
,(concat (propertize "deleted passages" 'face 'orgdiff-latexdiff-red)
" " (propertize "added passages" 'face 'orgdiff-latexdiff-blue)))
(ZLABEL . ,(propertize "Highlight changed pages, requires post-processing" 'face 'font-lock-doc-face))
(ONLYCHANGEDPAGE . ,(propertize "(Also) Highlights changed pages, no post-processing but dodgy floats" 'face 'font-lock-doc-face)))
("--floattype"
(FLOATSAFE . "")
(TRADITIONALSAFE . "")
(IDENTICAL . ""))
("--math-markup"
(off . "Supress markup in math environments. Only show new version")
(whole . "Any change causes the whole equation to be marked as changed")
(coarse . ,(concat "Coarse granularity. Use when content and order being changed" (propertize " (default)" 'face 'shadow)))
(fine . "Detect and mark up small changes. Suitable if minor changes (e.g. typo fixes) are expected"))
("--graphics-markup"
(off . "No highlighting for figures")
(new-only . ,(concat "Surround new/changed figures with a blue frame" (propertize " (default)" 'face 'shadow)))
(both . "Surround new/changed figures with a blue frame, and shrink and cross out deleted figures"))))
(defvar orgdiff--latexdiff-flags
`(("--type"
(UNDERLINE .
,(concat (propertize "discarded" 'face '((:strike-through t) orgdiff--latexdiff-red))
" " (propertize "added" 'face 'orgdiff--latexdiff-blue)
" " (propertize "(default)" 'face 'shadow)))
(CTRADITIONAL .
,(concat (propertize "[.."'face 'orgdiff--latexdiff-red)
(propertize "1"'face '((:height 0.6) orgdiff--latexdiff-red))
(propertize "]"'face 'orgdiff--latexdiff-red)
" " (propertize "added" 'face '((:height 1.15) variable-pitch orgdiff--latexdiff-blue))))
(TRADITIONAL .
,(concat "[.." (propertize "1" 'face '(:height 0.6)) "]"
" " (propertize "added" 'face '((:height 1.15) variable-pitch))))
(CFONT .
,(concat (propertize "discarded" 'face '((:height 0.7) orgdiff--latexdiff-red))
" " (propertize "added" 'face '((:height 1.15) variable-pitch orgdiff--latexdiff-blue))))
(FONTSTRIKE .
,(concat (propertize "discarded" 'face '((:strike-through t) small))
" " (propertize "added" 'face '((:height 1.15) variable-pitch))))
(CHANGEBAR . ,(propertize "no markup, change marks in margins" 'face 'font-lock-doc-face))
(CCHANGEBAR .
,(concat (propertize "CHANGEBAR" 'face 'font-lock-function-name-face)
(propertize " + " 'face 'font-lock-comment-face)
(propertize "discarded" 'face 'orgdiff--latexdiff-red)
" " (propertize "added" 'face 'orgdiff--latexdiff-blue)))
(CFONTCHBAR .
,(concat (propertize "CHANGEBAR" 'face 'font-lock-function-name-face)
(propertize " + " 'face 'font-lock-comment-face)
(propertize "discarded" 'face '(small orgdiff--latexdiff-red))
" " (propertize "added" 'face '((:height 1.15) variable-pitch orgdiff--latexdiff-blue))))
(CULINECHBAR .
,(concat (propertize "CHANGEBAR" 'face 'font-lock-function-name-face)
(propertize " + " 'face 'font-lock-comment-face)
(propertize "discarded" 'face '((:strike-through t) orgdiff--latexdiff-red))
" " (propertize "added" 'face 'orgdiff--latexdiff-blue)))
(INVISIBLE . "added")
(BOLD .
,(propertize "added" 'face 'bold))
(PDFCOMMENT .
,(concat (propertize "discarded text in PDF comment, " 'face 'font-lock-doc-face) (propertize "added" 'face 'underline))))
("--subtype"
(SAFE . ,(propertize "No additional markup (default, reccomended)" 'face 'font-lock-doc-face))
(MARGIN . ,(propertize "Mark start and end of changed block with symbol in margin" 'face 'font-lock-doc-face))
(COLOR .
,(concat (propertize "deleted passages" 'face 'orgdiff--latexdiff-red)
" " (propertize "added passages" 'face 'orgdiff--latexdiff-blue)))
(ZLABEL . ,(propertize "Highlight changed pages, requires post-processing" 'face 'font-lock-doc-face))
(ONLYCHANGEDPAGE . ,(propertize "(Also) Highlights changed pages, no post-processing but dodgy floats" 'face 'font-lock-doc-face)))
("--floattype"
(FLOATSAFE . "")
(TRADITIONALSAFE . "")
(IDENTICAL . ""))
("--math-markup"
(off . "Supress markup in math environments. Only show new version")
(whole . "Any change causes the whole equation to be marked as changed")
(coarse . ,(concat "Coarse granularity. Use when content and order being changed" (propertize " (default)" 'face 'shadow)))
(fine . "Detect and mark up small changes. Suitable if minor changes (e.g. typo fixes) are expected"))
("--graphics-markup"
(off . "No highlighting for figures")
(new-only . ,(concat "Surround new/changed figures with a blue frame" (propertize " (default)" 'face 'shadow)))
(both . "Surround new/changed figures with a blue frame, and shrink and cross out deleted figures")))
"LaTeXdiff flags and recognised values they may take.")
(defun orgdiff-latexdiff-prompt-flag (flag)
(let* ((max-key-width (thread-last (cdr (assoc flag orgdiff-latexdiff-flags))
(mapcar #'car)
(mapcar #'symbol-name)
(mapcar #'length)
(seq-max)))
(defun orgdiff--latexdiff-prompt-flag (flag)
(let* ((max-key-width
(thread-last (cdr (assoc flag orgdiff--latexdiff-flags))
(mapcar #'car)
(mapcar #'symbol-name)
(mapcar #'length)
(seq-max)))
(options
(mapcar (lambda (opt)
(concat (propertize (symbol-name (car opt)) 'face 'transient-value)
(make-string (- max-key-width (length (symbol-name (car opt))) -2) ? )
(cdr opt)))
(cdr (assoc flag orgdiff-latexdiff-flags))))
(cdr (assoc flag orgdiff--latexdiff-flags))))
(selection
(completing-read (concat flag ": ")
options
@ -313,44 +380,43 @@
(point-min)
(cdr (bounds-of-thing-at-point 'word)))))))
(eval-when-compile
(orgdiff--define-infix
"-a" "latexdiff-async" "Export Org to TeX asyncronously"
'boolean nil
(not orgdiff-latexdiff-async))
(orgdiff--define-infix
"-a" latexdiff-async "Export Org to TeX asyncronously"
'boolean nil
(not orgdiff-latexdiff-async))
(orgdiff--define-infix
"-t" "latexdiff-type" "Type"
'symbol 'UNDERLINE
(orgdiff-latexdiff-prompt-flag "--type"))
(orgdiff--define-infix
"-s" "latexdiff-subtype" "Sub-type"
'symbol 'SAFE
(orgdiff-latexdiff-prompt-flag "--subtype"))
(orgdiff--define-infix
"-f" "latexdiff-floattype" "Float type"
'symbol 'FLOATSAFE
(orgdiff-latexdiff-prompt-flag "--floattype"))
(orgdiff--define-infix
"-t" latexdiff-type "Type"
'symbol 'UNDERLINE
(orgdiff--latexdiff-prompt-flag "--type"))
(orgdiff--define-infix
"-s" latexdiff-subtype "Sub-type"
'symbol 'SAFE
(orgdiff--latexdiff-prompt-flag "--subtype"))
(orgdiff--define-infix
"-f" latexdiff-floattype "Float type"
'symbol 'FLOATSAFE
(orgdiff--latexdiff-prompt-flag "--floattype"))
(orgdiff--define-infix
"-F" "latexdiff-flatten" "Flatten includes"
'boolean nil
(not orgdiff-latexdiff-flatten))
(orgdiff--define-infix
"-F" latexdiff-flatten "Flatten includes"
'boolean nil
(not orgdiff--latexdiff-flatten))
(orgdiff--define-infix
"-S" "latexdiff-allow-spaces" "Allow spaces"
'boolean nil
(not orgdiff-latexdiff-allow-spaces))
(orgdiff--define-infix
"-m" "latexdiff-math-markup" "Math markup"
'symbol 'coarse
(orgdiff-latexdiff-prompt-flag "--math-markup"))
(orgdiff--define-infix
"-g" "latexdiff-graphics-markup" "Graphics markup"
'symbol 'new-only
(orgdiff-latexdiff-prompt-flag "--graphics-markup")))
(orgdiff--define-infix
"-S" latexdiff-allow-spaces "Allow spaces"
'boolean nil
(not orgdiff-latexdiff-allow-spaces))
(orgdiff--define-infix
"-m" latexdiff-math-markup "Math markup"
'symbol 'coarse
(orgdiff--latexdiff-prompt-flag "--math-markup"))
(orgdiff--define-infix
"-g" latexdiff-graphics-markup "Graphics markup"
'symbol 'new-only
(orgdiff--latexdiff-prompt-flag "--graphics-markup"))
(transient-define-prefix orgdiff-latexdiff-transient ()
(transient-define-prefix orgdiff--latexdiff-transient ()
["Processing"
(orgdiff--set-latexdiff-async)
(orgdiff--set-latexdiff-flatten)
@ -368,71 +434,62 @@
;; Citation markup?
]
["Action"
("l" "run latexdiff, compile, and open PDF" orgdiff-latexdiff--action-pdf-open)
("L" "run latexdiff only" orgdiff-latexdiff--action-latex)
;; ("P" "PDF" orgdiff-latexdiff--action-pdf)
;; ("p" "run latexdiff, and compile" orgdiff-latexdiff--action-latex)
("l" "run latexdiff, compile, and open PDF" orgdiff--latexdiff--action-pdf-open)
("L" "run latexdiff only" orgdiff--latexdiff--action-latex)
;; ("P" "PDF" orgdiff--latexdiff--action-pdf)
;; ("p" "run latexdiff, and compile" orgdiff--latexdiff--action-latex)
])
(defvar orgdiff-latexdiff--completion-action nil)
(defvar orgdiff--latexdiff--completion-action nil)
(defun orgdiff-latexdiff--action-latex ()
(defun orgdiff--latexdiff--action-latex ()
(interactive)
(setq orgdiff-latexdiff--completion-action '(:format latex :action nil))
(orgdiff-latexdiff-execute-pt1))
(setq orgdiff--latexdiff--completion-action '(:format latex :action nil))
(orgdiff--latexdiff-execute-pt1))
(defun orgdiff-latexdiff--action-latex-open ()
(defun orgdiff--latexdiff--action-latex-open ()
(interactive)
(setq orgdiff-latexdiff--completion-action '(:format latex :action open))
(orgdiff-latexdiff-execute-pt1))
(setq orgdiff--latexdiff--completion-action '(:format latex :action open))
(orgdiff--latexdiff-execute-pt1))
(defun orgdiff-latexdiff--action-pdf ()
(defun orgdiff--latexdiff--action-pdf ()
(interactive)
(setq orgdiff-latexdiff--completion-action '(:format pdf :action nil))
(orgdiff-latexdiff-execute-pt1))
(setq orgdiff--latexdiff--completion-action '(:format pdf :action nil))
(orgdiff--latexdiff-execute-pt1))
(defun orgdiff-latexdiff--action-pdf-open ()
(defun orgdiff--latexdiff--action-pdf-open ()
(interactive)
(setq orgdiff-latexdiff--completion-action '(:format pdf :action open))
(orgdiff-latexdiff-execute-pt1))
(setq orgdiff--latexdiff--completion-action '(:format pdf :action open))
(orgdiff--latexdiff-execute-pt1))
;;; Actually building the diff
(defcustom orgdiff-latexdiff-executable "latexdiff"
"Path to the latexdiff executable to use on the generated tex files.")
(defun orgdiff-latexdiff-execute-pt1 ()
(orgdiff-extract-revisions)
(orgdiff-latex-create-from-org)
(defun orgdiff--latexdiff-execute-pt1 ()
(orgdiff--extract-revisions)
(orgdiff--latex-create-from-org)
(unless (executable-find orgdiff-latexdiff-executable)
(user-error "Could not locate the latexdiff executable!"))
(orgdiff-latexdiff-wait-for-export-then #'orgdiff-latexdiff-execute-pt2))
(orgdiff--latexdiff-wait-for-export-then #'orgdiff--latexdiff-execute-pt2))
(defun orgdiff-latexdiff-execute-pt2 ()
(defun orgdiff--latexdiff-execute-pt2 ()
(unless
(and (file-exists-p (concat (file-name-sans-extension orgdiff--rev1file) ".tex"))
(file-exists-p (concat (file-name-sans-extension orgdiff--rev2file) ".tex")))
(user-error "Error! Org files were not sucessfully exported to LaTeX"))
(orgdiff-latexdiff-expand)
(orgdiff-latexdiff-do-diff)
(pcase (plist-get orgdiff-latexdiff--completion-action :format)
(orgdiff--latexdiff-expand)
(orgdiff--latexdiff-do-diff)
(pcase (plist-get orgdiff--latexdiff--completion-action :format)
('latex
(let ((dest (concat (file-name-directory orgdiff-file-1)
(file-name-nondirectory orgdiff--difffile))))
(rename-file orgdiff--difffile dest t)
(when (eq 'open (plist-get orgdiff-latexdiff--completion-action :action))
(when (eq 'open (plist-get orgdiff--latexdiff--completion-action :action))
(find-file-other-window dest))))
('pdf
(orgdiff-latexdiff-compile)
(orgdiff-latexdiff-wait-for-export-then #'orgdiff-latexdiff-handle-pdf))))
(orgdiff--latexdiff-compile)
(orgdiff--latexdiff-wait-for-export-then #'orgdiff--latexdiff-handle-pdf))))
(defvar orgdiff--rev1dir nil)
(defvar orgdiff--rev2dir nil)
(defvar orgdiff--rev1file nil)
(defvar orgdiff--rev2file nil)
(defun orgdiff-extract-revisions ()
(defun orgdiff--extract-revisions ()
(setq orgdiff--rev1dir nil
orgdiff--rev2dir nil)
(when orgdiff-git-revisions
@ -456,9 +513,7 @@
orgdiff--rev2dir)
(or orgdiff-file-2 orgdiff-file-1))))
(defvar orgdiff--export-processes nil)
(defun orgdiff-latex-create-from-org ()
(defun orgdiff--latex-create-from-org ()
(dolist (file (list orgdiff--rev1file orgdiff--rev2file))
(with-temp-buffer
(setq buffer-file-name file
@ -474,7 +529,7 @@
(set-buffer-modified-p nil))))
(message "%s%s" (propertize "Orgdiff" 'face 'bold) ": Exporting Org files to TeX..."))
(defun orgdiff-latexdiff-wait-for-export-then (then)
(defun orgdiff--latexdiff-wait-for-export-then (then)
(if orgdiff--export-processes
(progn
(setq orgdiff--export-processes
@ -482,10 +537,10 @@
(lambda (proc)
(if (memq (process-status proc) '(exit signal failed)) nil proc))
orgdiff--export-processes)))
(run-at-time 0.5 nil #'orgdiff-latexdiff-wait-for-export-then then))
(run-at-time 0.5 nil #'orgdiff--latexdiff-wait-for-export-then then))
(funcall then)))
(defun orgdiff-latexdiff-abort ()
(defun orgdiff--latexdiff-abort ()
(interactive)
(when orgdiff--export-processes
(ignore-errors (mapcar #'kill-process orgdiff--export-processes))
@ -493,18 +548,13 @@
(setq orgdiff--export-processes nil)
(org-export-stack-clear)))
(defun orgdiff-latexdiff-expand ()
(when orgdiff-latexdiff-flatten
(defun orgdiff--latexdiff-expand ()
(when orgdiff--latexdiff-flatten
(message "%s%s" (propertize "Orgdiff" 'face 'bold) ": Flattening tex files...")
(call-process "latexpand" nil nil nil orgdiff--rev1file)
(call-process "latexpand" nil nil nil orgdiff--rev2file)))
(defvar orgdiff--difffile nil)
(defcustom orgdiff-latexdiff-postprocess-hooks nil
"Post-processing functions which are run on the latexdiff result.")
(defun orgdiff-latexdiff-do-diff ()
(defun orgdiff--latexdiff-do-diff ()
(message "%s%s" (propertize "Orgdiff" 'face 'bold) ": latexdiff-ing tex files...")
(with-temp-buffer
(apply #'call-process orgdiff-latexdiff-executable nil '(t nil) nil
@ -512,7 +562,7 @@
"-t" (symbol-name orgdiff-latexdiff-type)
"-s" (symbol-name orgdiff-latexdiff-subtype)
"-f" (symbol-name orgdiff-latexdiff-floattype)
(when orgdiff-latexdiff-allow-spaces "--allow-spaces")
(and orgdiff-latexdiff-allow-spaces "--allow-spaces")
(format "--math-markup=%s" orgdiff-latexdiff-math-markup)
(format "--graphics-markup=%s" orgdiff-latexdiff-graphics-markup)
(expand-file-name (concat (file-name-sans-extension orgdiff--rev1file) ".tex"))
@ -537,26 +587,7 @@
(setq buffer-file-name orgdiff--difffile)
(save-buffer 0))))
(defcustom orgdiff-latex-compiler-priorities
'("pdflatex" "xelatex" "lualatex")
"A list of compiler priorities, ascending.
If two documents require different compilers, the higher priority
compiler will be used.")
(defcustom orgdiff-latex-compile-command
(delq nil (list "latexmk"
"-f"
"-pdf"
"-%compiler"
(when (string-match-p "-shell-escape" (car org-latex-pdf-process))
"-shell-escape")
"-interaction=nonstopmode"
"%texfile"))
"Compile command as a list in the form (\"CMD\" \"ARGS\"...).
\"%compiler\" is replaced with the requested compiler, and
\"%texfile\" replaced with the path to the .tex file.")
(defun orgdiff-latexdiff-compile ()
(defun orgdiff--latexdiff-compile ()
(let* ((compilers (mapcar
(lambda (file)
(with-temp-buffer
@ -574,17 +605,17 @@ compiler will be used.")
(mapcar
(lambda (arg)
(thread-last arg
(replace-regexp-in-string "%compiler" compiler)
(replace-regexp-in-string "%texfile"
(expand-file-name orgdiff--difffile))))
(replace-regexp-in-string "%compiler" compiler)
(replace-regexp-in-string "%texfile"
(expand-file-name orgdiff--difffile))))
orgdiff-latex-compile-command))
(default-directory (file-name-directory orgdiff--difffile)))
(message "%s%s" (propertize "Orgdiff" 'face 'bold) ": compiling diff file...")
(push
(apply #'start-process "orgdiff-latexdiff-compile" "*orgdiff-latexdiff-compile*" compile-command)
(apply #'start-process "orgdiff--latexdiff-compile" "*orgdiff--latexdiff-compile*" compile-command)
orgdiff--export-processes)))
(defun orgdiff-latexdiff-handle-pdf ()
(defun orgdiff--latexdiff-handle-pdf ()
(message "%s%s" (propertize "Orgdiff" 'face 'bold) ": diff created.")
;; Cleanup
(when org-latex-remove-logfiles
@ -604,7 +635,7 @@ compiler will be used.")
(unless (file-exists-p pdf-file)
(user-error "Error! Diff PDF was not produced"))
(rename-file pdf-file dest t)
(when (eq 'open (plist-get orgdiff-latexdiff--completion-action :action))
(when (eq 'open (plist-get orgdiff--latexdiff--completion-action :action))
(find-file-other-window dest))))
(provide 'orgdiff)