]> code.delx.au - gnu-emacs/blobdiff - lisp/org/ob-exp.el
Update copyright year to 2016
[gnu-emacs] / lisp / org / ob-exp.el
index 74581421591a554a088abf19a0b6dd7b323a67a4..c04e1307314824bc0f05ef8501d4f6f8bd4ad4c4 100644 (file)
@@ -1,6 +1,6 @@
 ;;; ob-exp.el --- Exportation of org-babel source blocks
 
-;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
+;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
 
 ;; Authors: Eric Schulte
 ;;     Dan Davison
@@ -24,6 +24,7 @@
 
 ;;; Code:
 (require 'ob-core)
+(require 'org-src)
 (eval-when-compile
   (require 'cl))
 
@@ -52,7 +53,7 @@
 (defcustom org-export-babel-evaluate t
   "Switch controlling code evaluation during export.
 When set to nil no code will be evaluated as part of the export
-process.  When set to 'inline-only, only inline code blocks will
+process.  When set to `inline-only', only inline code blocks will
 be executed."
   :group 'org-babel
   :version "24.1"
@@ -97,7 +98,7 @@ be executed."
 
 (defun org-babel-exp-src-block (&rest headers)
   "Process source block for export.
-Depending on the 'export' headers argument, replace the source
+Depending on the `export' headers argument, replace the source
 code block like this:
 
 both ---- display the code and the results
@@ -169,8 +170,12 @@ this template."
                            (backward-char)
                            (save-match-data (org-element-context))))
                 (type (org-element-type element))
-                (beg-el (org-element-property :begin element))
-                (end-el (org-element-property :end element)))
+                (begin (copy-marker (org-element-property :begin element)))
+                (end (copy-marker
+                      (save-excursion
+                        (goto-char (org-element-property :end element))
+                        (skip-chars-backward " \r\t\n")
+                        (point)))))
            (case type
              (inline-src-block
               (let* ((info (org-babel-parse-inline-src-block-match))
@@ -181,24 +186,21 @@ this template."
                           (org-babel-expand-noweb-references
                            info (org-babel-exp-get-export-buffer))
                         (nth 1 info)))
-                (goto-char beg-el)
+                (goto-char begin)
                 (let ((replacement (org-babel-exp-do-export info 'inline)))
                   (if (equal replacement "")
                       ;; Replacement code is empty: remove inline src
                       ;; block, including extra white space that
                       ;; might have been created when inserting
                       ;; results.
-                      (delete-region beg-el
-                                     (progn (goto-char end-el)
+                      (delete-region begin
+                                     (progn (goto-char end)
                                             (skip-chars-forward " \t")
                                             (point)))
                     ;; Otherwise: remove inline src block but
                     ;; preserve following white spaces.  Then insert
                     ;; value.
-                    (delete-region beg-el
-                                   (progn (goto-char end-el)
-                                          (skip-chars-backward " \t")
-                                          (point)))
+                    (delete-region begin end)
                     (insert replacement)))))
              ((babel-call inline-babel-call)
               (let* ((lob-info (org-babel-lob-get-info))
@@ -229,8 +231,8 @@ this template."
                 ;; results.
                 (if (equal rep "")
                     (delete-region
-                     beg-el
-                     (progn (goto-char end-el)
+                     begin
+                     (progn (goto-char end)
                             (if (not (eq type 'babel-call))
                                 (progn (skip-chars-forward " \t") (point))
                               (skip-chars-forward " \r\t\n")
@@ -238,25 +240,17 @@ this template."
                   ;; Otherwise, preserve following white
                   ;; spaces/newlines and then, insert replacement
                   ;; string.
-                  (goto-char beg-el)
-                  (delete-region beg-el
-                                 (progn (goto-char end-el)
-                                        (skip-chars-backward " \r\t\n")
-                                        (point)))
+                  (goto-char begin)
+                  (delete-region begin end)
                   (insert rep))))
              (src-block
-              (let* ((match-start (match-beginning 0))
-                     ;; Make sure we don't remove any blank lines
-                     ;; after the block when replacing it.
-                     (block-end (save-excursion
-                                  (goto-char end-el)
-                                  (skip-chars-backward " \r\t\n")
-                                  (line-end-position)))
+              (let* ((match-start (copy-marker (match-beginning 0)))
                      (ind (org-get-indentation))
                      (headers
                       (cons
                        (org-element-property :language element)
-                       (let ((params (org-element-property :parameters element)))
+                       (let ((params (org-element-property :parameters
+                                                           element)))
                          (and params (org-split-string params "[ \t]+"))))))
                 ;; Take care of matched block: compute replacement
                 ;; string.  In particular, a nil REPLACEMENT means
@@ -264,21 +258,31 @@ this template."
                 ;; string should remove the block.
                 (let ((replacement (progn (goto-char match-start)
                                           (org-babel-exp-src-block headers))))
-                  (cond ((not replacement) (goto-char block-end))
+                  (cond ((not replacement) (goto-char end))
                         ((equal replacement "")
-                         (delete-region beg-el end-el))
+                         (goto-char end)
+                         (skip-chars-forward " \r\t\n")
+                         (beginning-of-line)
+                         (delete-region begin (point)))
                         (t
                          (goto-char match-start)
-                         (delete-region (point) block-end)
+                         (delete-region (point)
+                                        (save-excursion (goto-char end)
+                                                        (line-end-position)))
                          (insert replacement)
-                         (if (org-element-property :preserve-indent element)
+                         (if (or org-src-preserve-indentation
+                                 (org-element-property :preserve-indent
+                                                       element))
                              ;; Indent only the code block markers.
                              (save-excursion (skip-chars-backward " \r\t\n")
                                              (indent-line-to ind)
                                              (goto-char match-start)
                                              (indent-line-to ind))
                            ;; Indent everything.
-                           (indent-rigidly match-start (point) ind))))))))))))))
+                           (indent-rigidly match-start (point) ind)))))
+                (set-marker match-start nil))))
+           (set-marker begin nil)
+           (set-marker end nil)))))))
 
 (defun org-babel-in-example-or-verbatim ()
   "Return true if point is in example or verbatim code.
@@ -308,7 +312,7 @@ The function respects the value of the :exports header argument."
             (org-babel-exp-code info)))))
 
 (defcustom org-babel-exp-code-template
-  "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
+  "#+BEGIN_SRC %lang%switches%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
@@ -318,6 +322,7 @@ 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
+ switches -- the switches associated to the code block
  flags ----- the flags passed to the code block
 
 In addition to the keys mentioned above, every header argument
@@ -340,11 +345,14 @@ replaced with its value."
    org-babel-exp-code-template
    `(("lang"  . ,(nth 0 info))
      ("body"  . ,(org-escape-code-in-string (nth 1 info)))
+     ("switches" . ,(let ((f (nth 3 info)))
+                     (and (org-string-nw-p f) (concat " " f))))
+     ("flags" . ,(let ((f (assq :flags (nth 2 info))))
+                  (and f (concat " " (cdr f)))))
      ,@(mapcar (lambda (pair)
                 (cons (substring (symbol-name (car pair)) 1)
                       (format "%S" (cdr pair))))
               (nth 2 info))
-     ("flags" . ,(let ((f (nth 3 info))) (when f (concat " " f))))
      ("name"  . ,(or (nth 4 info) "")))))
 
 (defun org-babel-exp-results (info type &optional silent hash)