Browse Source

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>
Nicolas Goaziou 1 year ago
parent
commit
f500d7c7f6
2 changed files with 46 additions and 24 deletions
  1. 22 15
      lisp/org-element.el
  2. 24 9
      testing/lisp/test-org-element.el

+ 22 - 15
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

+ 24 - 9
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."