Org: latex, improve tangled file embedding

Fix implementation to work with latest Org, and make the description a
tad more ... descriptive.
This commit is contained in:
TEC 2021-06-08 04:28:23 +08:00
parent c77ba18e71
commit 3de648e7a2
Signed by: tec
GPG Key ID: 779591AFDB81F06C
1 changed files with 9 additions and 42 deletions

View File

@ -7709,53 +7709,20 @@ access.
#+end_src
In the "universal preamble", we already embed the source =.org= file, but it would
be nice to embed all the tangled files. This is fairly easy to accomplish using
a cannibalised version of ~org-babel-tangle~ which just collects the file names of
each block that is tangled.
#+begin_src emacs-lisp
(defun org-babel-tangle-files ()
"All files that may be tangled to.
Uses a stripped-down version of `org-babel-tangle'"
(let (files)
(save-excursion
(mapc ;; map over all languages
(lambda (by-lang)
(let* ((lang (car by-lang))
(specs (cdr by-lang))
(ext (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang)))
(mapc
(lambda (spec)
(let ((get-spec (lambda (name) (cdr (assoc name (nth 4 spec))))))
(let* ((tangle (funcall get-spec :tangle))
(base-name (cond
((string= "yes" tangle)
(file-name-sans-extension
(nth 1 spec)))
((string= "no" tangle) nil)
((> (length tangle) 0) tangle)))
(file-name (when base-name
;; decide if we want to add ext to base-name
(if (and ext (string= "yes" tangle))
(concat base-name "." ext) base-name))))
(push file-name files))))
specs)))
(org-babel-tangle-collect-blocks)))
(delq nil (cl-delete-duplicates files :test #'string=))))
#+end_src
From here it is trivial to map each file to a form which embeds the file if it
exists.
be nice to embed all the tangled files. This is fairly easy to accomplish by
mapping each tangled file to a form which embeds the file if it exists.
#+begin_src emacs-lisp
(defun org-latex-embed-tangled-files ()
"Return a string that uses embedfile to embed all tangled files."
(mapconcat
(lambda (tangle-file)
(format "\\IfFileExists{%1$s}{\\embedfile[desc=A tangled file]{%1$s}}{}"
(->> tangle-file
(replace-regexp-in-string "\\\\" "\\\\\\\\")
(replace-regexp-in-string "~" "\\\\string~"))))
(org-babel-tangle-files)
(format "\\IfFileExists{%1$s}{\\embedfile[desc=Tangled %2$s file]{%1$s}}{}"
(->> (car tangle-file)
(replace-regexp-in-string "\\\\" "\\\\\\\\")
(replace-regexp-in-string "~" "\\\\string~"))
(cdr tangle-file)))
(mapcar (lambda (f-block) (cons (car f-block) (caadr f-block)))
(org-babel-tangle-collect-blocks)) ; all files being tangled to
"\n"))
#+end_src