From 711e687709a484221e5ebf5c2abdd1bd8bd0362a Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 25 Jul 2020 22:33:52 -0400 Subject: [PATCH] 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). --- modules/config/literate/autoload.el | 54 +++++++++++++++++------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/modules/config/literate/autoload.el b/modules/config/literate/autoload.el index c84a8a22f..84c54df38 100644 --- a/modules/config/literate/autoload.el +++ b/modules/config/literate/autoload.el @@ -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)))