diff --git a/modules/config/literate/autoload.el b/modules/config/literate/autoload.el index ecb096aa6..93880736a 100644 --- a/modules/config/literate/autoload.el +++ b/modules/config/literate/autoload.el @@ -1,7 +1,14 @@ ;;; config/literate/autoload.el -*- lexical-binding: t; -*- ;;;###autoload -(defun +literate/compile (&optional load) - "TODO" - (interactive "P") - (+literate-compile load)) +(defalias '+literate/reload #'doom/reload) + +;;;###autoload +(defun +literate|recompile-maybe () + "Recompile config.org if we're editing an org file in our DOOMDIR. + +We assume any org file in `doom-private-dir' is connected to your literate +config, and should trigger a recompile if changed." + (when (and (eq major-mode 'org-mode) + (file-in-directory-p buffer-file-name doom-private-dir)) + (+literate-tangle 'force))) diff --git a/modules/config/literate/init.el b/modules/config/literate/init.el index ce02535b2..57133cc6d 100644 --- a/modules/config/literate/init.el +++ b/modules/config/literate/init.el @@ -4,34 +4,41 @@ (expand-file-name "config.org" doom-private-dir) "The file path of your literate config file.") -(defvar +literate-config-dest-file - (expand-file-name "config.el" doom-private-dir) +(defvar +literate-config-cache-file + (expand-file-name "literate-last-compile" doom-cache-dir) "The file path that `+literate-config-file' will be tangled to, then byte-compiled from.") ;; -(defun +literate-compile (&optional load) - "Tangles & compiles `+literate-config-file' if it has changed. If LOAD is -non-nil, load it too!" - (let ((org +literate-config-file) - (el +literate-config-dest-file)) - (when (file-newer-than-file-p org el) +(defun +literate-tangle (&optional force-p) + "Tangles `+literate-config-file' if it has changed." + (let ((default-directory doom-private-dir) + (org +literate-config-file)) + (when (or force-p (file-newer-than-file-p org +literate-config-cache-file)) (message "Compiling your literate config...") - ;; We tangle in a separate, blank process because loading it here would - ;; load all of :lang org (very expensive!). We only need ob-tangle. - (or (zerop (call-process - "emacs" nil nil nil - "-q" "--batch" "-l" "ob-tangle" "--eval" - (format "(org-babel-tangle-file \"%s\" \"%s\" \"emacs-lisp\")" - org el))) + (or (and (if (fboundp 'org-babel-tangle-file) + (org-babel-tangle-file org nil "emacs-lisp") + ;; We tangle in a separate, blank process because loading it here + ;; would load all of :lang org (very expensive!). + (zerop (call-process + "emacs" nil nil nil + "-q" "--batch" "-l" "ob-tangle" "--eval" + (format "(org-babel-tangle-file %S nil \"emacs-lisp\")" + org)))) + ;; Write the cache file to serve as our mtime cache + (with-temp-file +literate-config-cache-file t)) (warn "There was a problem tangling your literate config!")) (message "Done!")))) ;; Let 'er rip! -(+literate-compile) - +(+literate-tangle doom-reloading-p) ;; No need to load the resulting file. Doom will do this for us after all ;; modules have finished loading. + + +;; Recompile our literate config if we modify it +(after! org + (add-hook 'after-save-hook #'+literate|recompile-maybe))