summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2018-09-29 22:42:17 +0200
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2018-10-03 18:44:34 +0200
commitbb36c0c8ab5b0948f50f2f46ffc5f36b5327128d (patch)
tree42a2714420a89143259295238adb3f2524abdcbd
parent3cb4ffe957d203d3c382d0ffbcfca1ee0d5d0318 (diff)
downloadorg-mode-bb36c0c8ab5b0948f50f2f46ffc5f36b5327128d.tar.gz
Refactor `org-babel-load-file'
* lisp/org.el (org-babel-load-file): Refactor code.
-rw-r--r--lisp/org.el38
1 files changed, 14 insertions, 24 deletions
diff --git a/lisp/org.el b/lisp/org.el
index 11d0514..191a168 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -242,35 +242,25 @@ Stars are put in group 1 and the trimmed body in group 2.")
(fmakunbound
(intern (concat "org-babel-expand-body:" lang)))))))
-(declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
+
;;;###autoload
(defun org-babel-load-file (file &optional compile)
"Load Emacs Lisp source code blocks in the Org FILE.
This function exports the source code using `org-babel-tangle'
-and then loads the resulting file using `load-file'. With prefix
-arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
-file to byte-code before it is loaded."
+and then loads the resulting file using `load-file'. With
+optional prefix argument COMPILE, the tangled Emacs Lisp file is
+byte-compiled before it is loaded."
(interactive "fFile to load: \nP")
- (let* ((age (lambda (file)
- (float-time
- (time-subtract (current-time)
- (nth 5 (or (file-attributes (file-truename file))
- (file-attributes file)))))))
- (base-name (file-name-sans-extension file))
- (exported-file (concat base-name ".el")))
- ;; tangle if the Org file is newer than the elisp file
- (unless (and (file-exists-p exported-file)
- (> (funcall age file) (funcall age exported-file)))
- ;; Tangle-file traversal returns reversed list of tangled files
- ;; and we want to evaluate the first target.
- (setq exported-file
- (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
- (message "%s %s"
- (if compile
- (progn (byte-compile-file exported-file 'load)
- "Compiled and loaded")
- (progn (load-file exported-file) "Loaded"))
- exported-file)))
+ (let* ((tangled-file (concat (file-name-sans-extension file) ".el")))
+ ;; Tangle only if the Org file is newer than the Elisp file.
+ (unless (org-file-newer-than-p tangled-file (nth 5 (file-attributes file)))
+ (org-babel-tangle-file file tangled-file "emacs-lisp"))
+ (if compile
+ (progn
+ (byte-compile-file tangled-file 'load)
+ (message "Compiled and loaded %s" tangled-file))
+ (load-file tangled-file)
+ (message "Loaded %s" tangled-file))))
(defcustom org-babel-load-languages '((emacs-lisp . t))
"Languages which can be evaluated in Org buffers.