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.
This commit is contained in:
Henrik Lissner 2018-06-05 16:09:03 +02:00
parent f5fe36af23
commit 47efd56fd4
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 50 additions and 21 deletions

View file

@ -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))

View file

@ -1,17 +1,21 @@
;;; config/literate/config.el -*- lexical-binding: t; -*- ;;; config/literate/config.el -*- lexical-binding: t; -*-
(defvar +literate-config-file "config.org" (defvar +literate-config-file
"The literate config file, searched for in `doom-private-dir' (unless this is (expand-file-name "config.org" doom-private-dir)
an absolute path).") "The file path of your literate config file.")
(defvar +literate-config-dest-file "config.el" (defvar +literate-config-dest-file
"The file that `+literate-config-file' will be tangled to, then byte-compiled (expand-file-name "config.el" doom-private-dir)
from.") "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)) (defun +literate-compile (&optional load)
(elc (expand-file-name (concat +literate-config-dest-file "c") doom-private-dir))) "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 ;; If config is pre-compiled, then load that
(when (file-newer-than-file-p org elc) (when (file-newer-than-file-p org elc)
;; We tangle in a separate, blank process because loading it here would load ;; We tangle in a separate, blank process because loading it here would load
@ -22,10 +26,12 @@ from.")
(format "(org-babel-tangle-file \"%s\" \"%s\" \"emacs-lisp\")" (format "(org-babel-tangle-file \"%s\" \"%s\" \"emacs-lisp\")"
org +literate-config-dest-file))) org +literate-config-dest-file)))
(error "There was a problem tangling your literate config!")) (error "There was a problem tangling your literate config!"))
;; Then byte-compile it! ;; Then byte-compile it!
(require 'bytecomp) (require 'bytecomp)
(byte-compile-file +literate-config-dest-file))) (byte-compile-file +literate-config-dest-file load))))
;; Let 'er rip!
(+literate-compile)
;; No need to load the resulting file. Doom will do this for us after all ;; No need to load the resulting file. Doom will do this for us after all
;; modules have finished loading. ;; modules have finished loading.