From 47efd56fd41d6ef7370e0fe8a872341f87f7287f Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 5 Jun 2018 16:09:03 +0200 Subject: [PATCH] compile/literate: refactor & recompile on save And add autoloaded commands. This may need work to ensure you don't spawn multiple instances of Emacs all vying to compile the same file. --- modules/config/literate/autoload.el | 23 ++++++++++++++ modules/config/literate/config.el | 48 ++++++++++++++++------------- 2 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 modules/config/literate/autoload.el diff --git a/modules/config/literate/autoload.el b/modules/config/literate/autoload.el new file mode 100644 index 000000000..d4b711b4c --- /dev/null +++ b/modules/config/literate/autoload.el @@ -0,0 +1,23 @@ +;;; config/literate/autoload.el -*- lexical-binding: t; -*- + +;;;###autoload +(add-hook 'org-mode-hook #'+literate|enable-compile-on-save) + +;;;###autoload +(defun +literate|enable-compile-on-save () + "TODO" + (add-hook 'after-save-hook #'+literate|compile-on-save nil 'local)) + +;;;###autoload +(defun +literate|compile-on-save () + "TODO" + (when (and (eq major-mode 'org-mode) + buffer-file-name + (file-in-directory-p buffer-file-name ))) + (+literate/compile)) + +;;;###autoload +(defun +literate/compile (&optional load) + "TODO" + (interactive "P") + (+literate-compile load)) diff --git a/modules/config/literate/config.el b/modules/config/literate/config.el index 47223c057..328a1fa89 100644 --- a/modules/config/literate/config.el +++ b/modules/config/literate/config.el @@ -1,31 +1,37 @@ ;;; config/literate/config.el -*- lexical-binding: t; -*- -(defvar +literate-config-file "config.org" - "The literate config file, searched for in `doom-private-dir' (unless this is -an absolute path).") +(defvar +literate-config-file + (expand-file-name "config.org" doom-private-dir) + "The file path of your literate config file.") -(defvar +literate-config-dest-file "config.el" - "The file that `+literate-config-file' will be tangled to, then byte-compiled -from.") +(defvar +literate-config-dest-file + (expand-file-name "config.el" doom-private-dir) + "The file path that `+literate-config-file' will be tangled to, then +byte-compiled from.") ;; -(let ((org (expand-file-name +literate-config-file doom-private-dir)) - (elc (expand-file-name (concat +literate-config-dest-file "c") doom-private-dir))) - ;; If config is pre-compiled, then load that - (when (file-newer-than-file-p org elc) - ;; We tangle in a separate, blank process because loading it here would load - ;; all of :lang org, which will be more expensive than it needs to be. - (or (zerop (call-process - "emacs" nil nil nil - "-q" "--batch" "-l" "ob-tangle" "--eval" - (format "(org-babel-tangle-file \"%s\" \"%s\" \"emacs-lisp\")" - org +literate-config-dest-file))) - (error "There was a problem tangling your literate config!")) +(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) + (elc (concat +literate-config-dest-file "c"))) + ;; If config is pre-compiled, then load that + (when (file-newer-than-file-p org elc) + ;; We tangle in a separate, blank process because loading it here would load + ;; all of :lang org, which will be more expensive than it needs to be. + (or (zerop (call-process + "emacs" nil nil nil + "-q" "--batch" "-l" "ob-tangle" "--eval" + (format "(org-babel-tangle-file \"%s\" \"%s\" \"emacs-lisp\")" + org +literate-config-dest-file))) + (error "There was a problem tangling your literate config!")) + ;; Then byte-compile it! + (require 'bytecomp) + (byte-compile-file +literate-config-dest-file load)))) - ;; Then byte-compile it! - (require 'bytecomp) - (byte-compile-file +literate-config-dest-file))) +;; Let 'er rip! +(+literate-compile) ;; No need to load the resulting file. Doom will do this for us after all ;; modules have finished loading.