config/literate: expand #+INCLUDE directives

It's surprising that tangling doesn't expand #+INCLUDE directives. It's
so useful for literate configs I decided to expand them manually before
tangling (and relative to DOOMDIR, unless given an absolute path).
This commit is contained in:
Henrik Lissner 2020-07-25 22:33:52 -04:00
parent b2787a9426
commit 711e687709
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -10,28 +10,38 @@
byte-compiled from.")
;;;###autoload
(defun +literate-tangle-h (&optional force-p)
(defun +literate-tangle-h ()
"Tangles `+literate-config-file' if it has changed."
(let ((default-directory doom-private-dir))
(when (or (file-newer-than-file-p +literate-config-file
+literate-config-cache-file)
force-p)
(print! (start "Compiling your literate config..."))
(print-group!
(let* ((org (expand-file-name +literate-config-file))
(dest (concat (file-name-sans-extension +literate-config-file) ".el"))
(output (get-buffer-create "*org-tangle*")))
(unwind-protect
;; We tangle in a separate, blank process because loading it here
;; would load all of :lang org (very expensive!).
(and (require 'ob-tangle)
(letf! (defun message (msg &rest args)
(when msg
(print! (info "%s") (apply #'format msg args))))
(org-babel-tangle-file org dest))
;; Write the cache file to serve as our mtime cache
(with-temp-file +literate-config-cache-file))
(kill-buffer output)))))))
(print! (start "Compiling your literate config..."))
(print-group!
(let* ((default-directory doom-private-dir)
(org (expand-file-name +literate-config-file))
(dest (concat (file-name-sans-extension +literate-config-file) ".el"))
(backup (make-temp-file "config.org.backup")))
(and (require 'ox)
(require 'ob-tangle)
(unwind-protect
(letf! ((defun message (msg &rest args)
(when msg
(print! (info "%s") (apply #'format msg args))))
;; Prevent infinite recursion due to
;; recompile-on-save hooks later.
(org-mode-hook nil))
;; We do the ol' switcheroo because `org-babel-tangle'
;; writes changes to the current file, which would be
;; imposing on the user.
(copy-file org backup t)
(with-current-buffer (find-file-noselect org)
;; Tangling doesn't expand #+INCLUDE directives, so we
;; do it ourselves, since includes are so useful for
;; literate configs!
(org-export-expand-include-keyword)
(org-babel-tangle nil dest))
t)
(ignore-errors (copy-file backup org t))
(ignore-errors (delete-file backup)))
;; Write an empty file to serve as our mtime cache
(with-temp-file +literate-config-cache-file)))))
;;;###autoload
(add-hook 'org-mode-hook #'+literate-enable-recompile-h)
@ -51,4 +61,4 @@ byte-compiled from.")
We assume any org file in `doom-private-dir' is connected to your literate
config, and should trigger a recompile if changed."
(when (file-in-directory-p buffer-file-name doom-private-dir)
(+literate-tangle-h 'force)))
(+literate-tangle-h)))