]> code.delx.au - gnu-emacs/blobdiff - lisp/org/ob-exp.el
Sync Org 7.9.2 from the commit tagged "release_7.9.2" in Org's Git repo.
[gnu-emacs] / lisp / org / ob-exp.el
index 0f0cca3d94b5f02a473b1958ea1a2ed18113dee0..d17fd3475ae1eacef030d7fed7ec805df406329d 100644 (file)
 (defvar org-current-export-file)
 (defvar org-babel-lob-one-liner-regexp)
 (defvar org-babel-ref-split-regexp)
+(defvar org-list-forbidden-blocks)
+
 (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-non-block-elements))
+(declare-function org-heading-components "org" ())
+(declare-function org-link-search "org" (s &optional type avoid-pos stealth))
+(declare-function org-fill-template "org" (template alist))
+(declare-function org-in-verbatim-emphasis "org" ())
+(declare-function org-in-block-p "org" (names))
+(declare-function org-between-regexps-p "org" (start-re end-re &optional lim-up lim-down))
 
+(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))
 
 (defcustom org-export-babel-evaluate t
@@ -47,28 +55,33 @@ process."
   :type 'boolean)
 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
 
+(defun org-babel-exp-get-export-buffer ()
+  "Return the current export buffer if possible."
+  (cond
+   ((bufferp org-current-export-file) org-current-export-file)
+   (org-current-export-file (get-file-buffer org-current-export-file))
+   ('otherwise
+    (error "Requested export buffer when `org-current-export-file' is nil"))))
+
 (defmacro org-babel-exp-in-export-file (lang &rest body)
   (declare (indent 1))
   `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
          (heading (nth 4 (ignore-errors (org-heading-components))))
-         (link (when org-current-export-file
-                 (org-make-link-string
-                  (if heading
-                      (concat org-current-export-file "::" heading)
-                    org-current-export-file))))
-         (export-buffer (current-buffer)) results)
-     (when link
+         (export-buffer (current-buffer))
+         (original-buffer (org-babel-exp-get-export-buffer)) results)
+     (when original-buffer
        ;; resolve parameters in the original file so that
        ;; headline and file-wide parameters are included, attempt
        ;; to go to the same heading in the original file
-       (set-buffer (get-file-buffer org-current-export-file))
+       (set-buffer original-buffer)
        (save-restriction
-        (condition-case nil
-            (let ((org-link-search-inhibit-query t))
-              (org-open-link-from-string link))
-          (error (when heading
-                   (goto-char (point-min))
-                   (re-search-forward (regexp-quote heading) nil t))))
+        (when heading
+          (condition-case nil
+              (let ((org-link-search-inhibit-query t))
+                (org-link-search heading))
+            (error (when heading
+                     (goto-char (point-min))
+                     (re-search-forward (regexp-quote heading) nil t)))))
         (setq results ,@body))
        (set-buffer export-buffer)
        results)))
@@ -108,15 +121,25 @@ none ----- do not display either code or results upon export"
                    (if (boundp lang-headers) (eval lang-headers) nil)
                    raw-params))))
          (setf hash (org-babel-sha1-hash info)))
-       ;; expand noweb references in the original file
-       (setf (nth 1 info)
-             (if (and (cdr (assoc :noweb (nth 2 info)))
-                      (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
-                 (org-babel-expand-noweb-references
-                  info (get-file-buffer org-current-export-file))
-               (nth 1 info)))
        (org-babel-exp-do-export info 'block hash)))))
 
+(defcustom org-babel-exp-call-line-template
+  ""
+  "Template used to export call lines.
+This template may be customized to include the call line name
+with any export markup.  The template is filled out using
+`org-fill-template', and the following %keys may be used.
+
+ line --- call line
+
+An example value would be \"\\n: call: %line\" to export the call line
+wrapped in a verbatim environment.
+
+Note: the results are inserted separately after the contents of
+this template."
+  :group 'org-babel
+  :type 'string)
+
 (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."
@@ -147,7 +170,7 @@ none ----- do not display either code or results upon export"
                          (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))
+                              info (org-babel-exp-get-export-buffer))
                            (nth 1 info)))
                    (let ((code-replacement (save-match-data
                                              (org-babel-exp-do-export
@@ -163,22 +186,24 @@ none ----- do not display either code or results upon export"
                   (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)))))
+                  (results (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-no-properties
+                                       (concat ":var results="
+                                               (mapconcat #'identity
+                                                          (butlast lob-info)
+                                                          " ")))))
+                                    "" nil (car (last lob-info)))
+                              'lob)))
+                  (rep (org-fill-template
+                        org-babel-exp-call-line-template
+                        `(("line"  . ,(nth 0 lob-info))))))
              (if inlinep
                  (save-excursion
                    (goto-char inline-start)
@@ -202,26 +227,58 @@ org-mode text."
 (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."
-  (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
-                       (when (not (and session (equal "none" session)))
-                         (org-babel-exp-results info type 'silent))))
-        (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
+  (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
+                              (when (not (and session (equal "none" session)))
+                                (org-babel-exp-results info type 'silent)))))
+       (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
     (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
-      ('none (silently) (clean) "")
-      ('code (silently) (clean) (org-babel-exp-code info))
+      ('none (funcall silently) (funcall clean) "")
+      ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
       ('results (org-babel-exp-results info type nil hash) "")
       ('both (org-babel-exp-results info type nil hash)
             (org-babel-exp-code info)))))
 
+(defcustom org-babel-exp-code-template
+  "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
+  "Template used to export the body of code blocks.
+This template may be customized to include additional information
+such as the code block name, or the values of particular header
+arguments.  The template is filled out using `org-fill-template',
+and the following %keys may be used.
+
+ lang ------ the language of the code block
+ name ------ the name of the code block
+ body ------ the body of the code block
+ flags ----- the flags passed to the code block
+
+In addition to the keys mentioned above, every header argument
+defined for the code block may be used as a key and will be
+replaced with its value."
+  :group 'org-babel
+  :type 'string)
+
 (defun org-babel-exp-code (info)
   "Return the original code block formatted for export."
+  (setf (nth 1 info)
+       (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
+           (replace-regexp-in-string
+            (org-babel-noweb-wrap) "" (nth 1 info))
+         (if (org-babel-noweb-p (nth 2 info) :export)
+             (org-babel-expand-noweb-references
+              info (org-babel-exp-get-export-buffer))
+           (nth 1 info))))
   (org-fill-template
-   "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
+   org-babel-exp-code-template
    `(("lang"  . ,(nth 0 info))
-     ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
      ("body"  . ,(if (string= (nth 0 info) "org")
                     (replace-regexp-in-string "^" "," (nth 1 info))
-                  (nth 1 info))))))
+                  (nth 1 info)))
+     ,@(mapcar (lambda (pair)
+                (cons (substring (symbol-name (car pair)) 1)
+                      (format "%S" (cdr pair))))
+              (nth 2 info))
+     ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
+     ("name"  . ,(or (nth 4 info) "")))))
 
 (defun org-babel-exp-results (info type &optional silent hash)
   "Evaluate and return the results of the current code block for export.
@@ -232,11 +289,16 @@ inhibit insertion of results into the buffer."
   (when (and org-export-babel-evaluate
             (not (and hash (equal hash (org-babel-current-result-hash)))))
     (let ((lang (nth 0 info))
-         (body (nth 1 info)))
+         (body (if (org-babel-noweb-p (nth 2 info) :eval)
+                   (org-babel-expand-noweb-references
+                    info (org-babel-exp-get-export-buffer))
+                 (nth 1 info)))
+         (info (copy-sequence info)))
       ;; skip code blocks which we can't evaluate
       (when (fboundp (intern (concat "org-babel-execute:" lang)))
        (org-babel-eval-wipe-error-buffer)
        (prog1 nil
+         (setf (nth 1 info) body)
          (setf (nth 2 info)
                (org-babel-exp-in-export-file lang
                  (org-babel-process-params