summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2018-02-04 10:04:17 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2018-02-04 10:04:17 +0100
commitf500d7c7f6b82be5f543cd974084adf531e93e69 (patch)
tree5a0fbafe88f7be80e88d9429f3f6fb050f2ce213
parent878f2ae68bd2150de21a0f5cc0d21c82355e4d4b (diff)
downloadorg-mode-f500d7c7f6b82be5f543cd974084adf531e93e69.tar.gz
org-element: Fix example and src block interpreter.
* lisp/org-element.el (org-element-example-block-interpreter): (org-element-src-block-interpreter): Correctly handle indentation. * testing/lisp/test-org-element.el (test-org-element/example-block-interpreter): Add tests. Reported-by: Yasushi SHOJI <yasushi.shoji@gmail.com> <http://lists.gnu.org/r/emacs-orgmode/2018-02/msg00006.html>
-rw-r--r--lisp/org-element.el37
-rw-r--r--testing/lisp/test-org-element.el33
2 files changed, 46 insertions, 24 deletions
diff --git a/lisp/org-element.el b/lisp/org-element.el
index 6bd0091..cab647f 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -1980,14 +1980,21 @@ containing `:begin', `:end', `:number-lines', `:preserve-indent',
(defun org-element-example-block-interpreter (example-block _)
"Interpret EXAMPLE-BLOCK element as Org syntax."
(let ((switches (org-element-property :switches example-block))
- (value (org-element-property :value example-block)))
+ (value
+ (let ((val (org-element-property :value example-block)))
+ (cond
+ ((or org-src-preserve-indentation
+ (org-element-property :preserve-indent example-block))
+ val)
+ ((= 0 org-edit-src-content-indentation)
+ (org-remove-indentation val))
+ (t
+ (let ((ind (make-string org-edit-src-content-indentation ?\s)))
+ (replace-regexp-in-string "^[ \t]*\\S-"
+ (concat ind "\\&")
+ (org-remove-indentation val))))))))
(concat "#+begin_example" (and switches (concat " " switches)) "\n"
- (org-element-normalize-string
- (org-escape-code-in-string
- (if (or org-src-preserve-indentation
- (org-element-property :preserve-indent example-block))
- value
- (org-remove-indentation value))))
+ (org-element-normalize-string (org-escape-code-in-string value))
"#+end_example")))
@@ -2509,14 +2516,14 @@ Assume point is at the beginning of the block."
(org-remove-indentation val))
(t
(let ((ind (make-string org-edit-src-content-indentation ?\s)))
- (replace-regexp-in-string
- "^" ind (org-remove-indentation val))))))))
- (concat (format "#+begin_src%s\n"
- (concat (and lang (concat " " lang))
- (and switches (concat " " switches))
- (and params (concat " " params))))
- (org-element-normalize-string (org-escape-code-in-string value))
- "#+end_src")))
+ (replace-regexp-in-string "^[ \t]*\\S-"
+ (concat ind "\\&")
+ (org-remove-indentation val))))))))
+ (format "#+begin_src%s\n%s#+end_src"
+ (concat (and lang (concat " " lang))
+ (and switches (concat " " switches))
+ (and params (concat " " params)))
+ (org-element-normalize-string (org-escape-code-in-string value)))))
;;;; Table
diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el
index 3641cd5..1daecb4 100644
--- a/testing/lisp/test-org-element.el
+++ b/testing/lisp/test-org-element.el
@@ -2798,25 +2798,40 @@ CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
(ert-deftest test-org-element/example-block-interpreter ()
"Test example block interpreter."
;; Without switches.
- (should (equal (org-test-parse-and-interpret
- "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE")
- "#+begin_example\nTest\n#+end_example\n"))
+ (should (equal "#+begin_example\nTest\n#+end_example\n"
+ (let ((org-src-preserve-indentation t))
+ (org-test-parse-and-interpret
+ "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
;; With switches.
(should
- (equal (org-test-parse-and-interpret
- "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE")
- "#+begin_example -n -k\n(+ 1 1)\n#+end_example\n"))
+ (equal "#+begin_example -n -k\n(+ 1 1)\n#+end_example\n"
+ (let ((org-src-preserve-indentation t))
+ (org-test-parse-and-interpret
+ "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE"))))
;; Preserve code escaping.
(should
(equal
- (org-test-parse-and-interpret
- "#+BEGIN_EXAMPLE\n,* Headline\n,#+KEYWORD: value\nText\n#+END_EXAMPLE")
+ (let ((org-src-preserve-indentation t))
+ (org-test-parse-and-interpret
+ "#+BEGIN_EXAMPLE\n,* Headline\n,#+KEYWORD: value\nText\n#+END_EXAMPLE"))
"#+begin_example\n,* Headline\n,#+KEYWORD: value\nText\n#+end_example\n"))
;; Accept missing final newline in value.
(should
(equal
"#+begin_example\nTest\n#+end_example\n"
- (org-element-interpret-data '(example-block (:value "Test"))))))
+ (let ((org-src-preserve-indentation t))
+ (org-element-interpret-data '(example-block (:value "Test"))))))
+ ;; Handle indentation.
+ (should (equal "#+begin_example\n Test\n#+end_example\n"
+ (let ((org-src-preserve-indentation nil)
+ (org-edit-src-content-indentation 2))
+ (org-test-parse-and-interpret
+ "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
+ (should (equal "#+begin_example\n Test\n#+end_example\n"
+ (let ((org-src-preserve-indentation t)
+ (org-edit-src-content-indentation 2))
+ (org-test-parse-and-interpret
+ "#+BEGIN_EXAMPLE\n Test\n#+END_EXAMPLE")))))
(ert-deftest test-org-element/export-block-interpreter ()
"Test export block interpreter."