evaluate all executables in buffer order on export
authorEric Schulte <eric.schulte@gmx.com>
Mon, 23 Jan 2012 17:33:12 +0000 (10:33 -0700)
committerEric Schulte <eric.schulte@gmx.com>
Mon, 23 Jan 2012 17:39:38 +0000 (10:39 -0700)
* lisp/ob-exp.el (org-babel-exp-non-block-elements): Map over both
  inline src blocks and call lines on export.

lisp/ob-exp.el
testing/examples/babel.org
testing/lisp/test-ob-exp.el
testing/lisp/test-ob.el
testing/org-test.el

index 6f93600..06df1a1 100644 (file)
@@ -34,8 +34,7 @@
 (defvar org-babel-ref-split-regexp)
 (declare-function org-babel-lob-get-info "ob-lob" ())
 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
-(add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
-(add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
+(add-to-list 'org-export-interblocks '(src org-babel-exp-non-block-elements))
 
 (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
 
@@ -117,33 +116,73 @@ none ----- do not display either code or results upon export"
                (nth 1 info)))
        (org-babel-exp-do-export info 'block hash)))))
 
-(defun org-babel-exp-inline-src-blocks (start end)
-  "Process inline source blocks between START and END for export.
-See `org-babel-exp-src-block' for export options, currently the
-options and are taken from `org-babel-default-inline-header-args'."
+(defvar org-babel-default-lob-header-args)
+(defun org-babel-exp-non-block-elements (start end)
+  "Process inline source and call lines between START and END for export."
   (interactive)
   (save-excursion
     (goto-char start)
-    (while (and (< (point) end)
-                (re-search-forward org-babel-inline-src-block-regexp end t))
-      (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
-            (params (nth 2 info)))
-       (save-match-data
-         (goto-char (match-beginning 2))
+    (unless (markerp end)
+      (let ((m (make-marker)))
+       (set-marker m end (current-buffer))
+       (setq end m)))
+    (let ((rx (concat "\\("  org-babel-inline-src-block-regexp
+                     "\\|" org-babel-lob-one-liner-regexp "\\)")))
+      (while (and (< (point) (marker-position end))
+                 (re-search-forward rx end t))
+       (if (save-excursion
+             (goto-char (match-beginning 0))
+             (looking-at org-babel-inline-src-block-regexp))
+           (progn
+             (forward-char 1)
+             (let* ((info (save-match-data
+                            (org-babel-parse-inline-src-block-match)))
+                    (params (nth 2 info)))
+               (save-match-data
+                 (goto-char (match-beginning 2))
+                 (unless (org-babel-in-example-or-verbatim)
+                   ;; expand noweb references in the original file
+                   (setf (nth 1 info)
+                         (if (and (cdr (assoc :noweb params))
+                                  (string= "yes" (cdr (assoc :noweb params))))
+                             (org-babel-expand-noweb-references
+                              info (get-file-buffer org-current-export-file))
+                           (nth 1 info)))
+                   (let ((code-replacement (save-match-data
+                                             (org-babel-exp-do-export
+                                              info 'inline))))
+                     (if code-replacement
+                         (replace-match code-replacement nil nil nil 1)
+                       (org-babel-examplize-region (match-beginning 1)
+                                                   (match-end 1))
+                       (forward-char 2)))))))
          (unless (org-babel-in-example-or-verbatim)
-           ;; expand noweb references in the original file
-           (setf (nth 1 info)
-                 (if (and (cdr (assoc :noweb params))
-                          (string= "yes" (cdr (assoc :noweb params))))
-                     (org-babel-expand-noweb-references
-                      info (get-file-buffer org-current-export-file))
-                   (nth 1 info)))
-           (let ((code-replacement (save-match-data
-                                     (org-babel-exp-do-export info 'inline))))
-             (if code-replacement
-                 (replace-match code-replacement nil nil nil 1)
-               (org-babel-examplize-region (match-beginning 1) (match-end 1))
-               (forward-char 2)))))))))
+           (let* ((lob-info (org-babel-lob-get-info))
+                  (inlinep (match-string 11))
+                  (inline-start (match-end 11))
+                  (inline-end (match-end 0))
+                  (rep (let ((lob-info (org-babel-lob-get-info)))
+                         (save-match-data
+                           (org-babel-exp-do-export
+                            (list "emacs-lisp" "results"
+                                  (org-babel-merge-params
+                                   org-babel-default-header-args
+                                   org-babel-default-lob-header-args
+                                   (org-babel-params-from-properties)
+                                   (org-babel-parse-header-arguments
+                                    (org-babel-clean-text-properties
+                                     (concat ":var results="
+                                             (mapconcat #'identity
+                                                        (butlast lob-info)
+                                                        " ")))))
+                                  "" nil (car (last lob-info)))
+                            'lob)))))
+             (if inlinep
+                 (save-excursion
+                   (goto-char inline-start)
+                   (delete-region inline-start inline-end)
+                   (insert rep))
+               (replace-match rep t t)))))))))
 
 (defun org-babel-in-example-or-verbatim ()
   "Return true if point is in example or verbatim code.
@@ -158,47 +197,6 @@ org-mode text."
       (org-in-block-p org-list-forbidden-blocks)
       (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
 
-(defvar org-babel-default-lob-header-args)
-(defun org-babel-exp-lob-one-liners (start end)
-  "Process Library of Babel calls between START and END for export.
-See `org-babel-exp-src-block' for export options. Currently the
-options are taken from `org-babel-default-header-args'."
-  (interactive)
-  (save-excursion
-    (goto-char start)
-    (unless (markerp end)
-      (let ((m (make-marker)))
-       (set-marker m end (current-buffer))
-       (setq end m)))
-    (while (and (< (point) (marker-position end))
-               (re-search-forward org-babel-lob-one-liner-regexp end t))
-      (unless (org-babel-in-example-or-verbatim)
-       (let* ((lob-info (org-babel-lob-get-info))
-              (inlinep (match-string 11))
-              (inline-start (match-end 11))
-              (inline-end (match-end 0))
-              (rep (let ((lob-info (org-babel-lob-get-info)))
-                     (save-match-data
-                       (org-babel-exp-do-export
-                        (list "emacs-lisp" "results"
-                              (org-babel-merge-params
-                               org-babel-default-header-args
-                               org-babel-default-lob-header-args
-                               (org-babel-params-from-properties)
-                               (org-babel-parse-header-arguments
-                                (org-babel-clean-text-properties
-                                 (concat ":var results="
-                                         (mapconcat #'identity
-                                                    (butlast lob-info) " ")))))
-                              "" nil (car (last lob-info)))
-                        'lob)))))
-         (if inlinep
-             (save-excursion
-               (goto-char inline-start)
-               (delete-region inline-start inline-end)
-               (insert rep))
-           (replace-match rep t t)))))))
-
 (defun org-babel-exp-do-export (info type &optional hash)
   "Return a string with the exported content of a code block.
 The function respects the value of the :exports header argument."
index bb9cab9..dfe8d14 100644 (file)
@@ -308,3 +308,29 @@ src_sh{echo 2} blocks on the src_emacs-lisp{"same"} line
 #+name:
 [[file:./cv.cls]]
 
+* in order evaluation on export
+  :PROPERTIES:
+  :exports: results
+  :ID:       96cc7073-97ec-4556-87cf-1f9bffafd317
+  :END:
+
+First.
+#+name: foo-for-order-of-evaluation
+#+begin_src emacs-lisp :var it=1
+  (push it *evaluation-collector*)
+#+end_src
+
+Second
+#+begin_src emacs-lisp
+  (push 2 *evaluation-collector*)
+#+end_src
+
+Third src_emacs-lisp{(push 3 *evaluation-collector*)}
+
+Fourth
+#+call: foo-for-order-of-evaluation(4)
+
+Fifth
+#+begin_src emacs-lisp
+  (push 5 *evaluation-collector*)
+#+end_src
index a0182a1..8899e0d 100644 (file)
 ;;     (org-mark-subtree)
 ;;     (org-export-as-latex nil)))
 
+(ert-deftest ob-exp/evaluate-all-executables-in-order ()
+  (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
+    (org-narrow-to-subtree)
+    (let (*evaluation-collector*)
+      (org-export-as-ascii nil nil nil 'string)
+      (should (equal '(5 4 3 2 1) *evaluation-collector*)))))
+
 (provide 'test-ob-exp)
 
 ;;; test-ob-exp.el ends here
index e607c06..3eaedfc 100644 (file)
     (let ((test-point (point)))
       (should (fboundp 'org-babel-get-inline-src-block-matches))
       (should (re-search-forward "src_" nil t)) ;; 1
-      (should (= (+ test-point 138) (match-end 0)))
       (should (org-babel-get-inline-src-block-matches))
       (should (re-search-forward "}" nil (point-at-bol))) ;; 1
       (should-not (org-babel-get-inline-src-block-matches))
index 6c11bf3..bed92a0 100644 (file)
@@ -146,6 +146,7 @@ currently executed.")
         (save-restriction ,@body)))
      (unless visited-p
        (kill-buffer to-be-removed))))
+(def-edebug-spec org-test-at-id (form body))
 
 (defmacro org-test-in-example-file (file &rest body)
   "Execute body in the Org-mode example file."