Browse Source

ob-core: Fix improper results indentation

* lisp/ob-core.el (org-babel-insert-result): Fix improper results
  indentation.
* testing/lisp/test-ob.el (test-ob/preserve-results-indentation): Add
  test.

Reported-by: Chunyang Xu <mail@xuchunyang.me>
<http://lists.gnu.org/archive/html/emacs-orgmode/2017-08/msg00308.html>
Nicolas Goaziou 7 years ago
parent
commit
24a76fbe57
2 changed files with 73 additions and 20 deletions
  1. 22 19
      lisp/ob-core.el
  2. 51 1
      testing/lisp/test-ob.el

+ 22 - 19
lisp/ob-core.el

@@ -2269,21 +2269,22 @@ INFO may provide the values of these header arguments (in the
 		 ((member "prepend" result-params))) ; already there
 		 ((member "prepend" result-params))) ; already there
 		(setq results-switches
 		(setq results-switches
 		      (if results-switches (concat " " results-switches) ""))
 		      (if results-switches (concat " " results-switches) ""))
-		(let ((wrap (lambda (start finish &optional no-escape no-newlines
-				      inline-start inline-finish)
-			      (when inline
-				(setq start inline-start)
-				(setq finish inline-finish)
-				(setq no-newlines t))
-			      (goto-char end)
-			      (insert (concat finish (unless no-newlines "\n")))
-			      (goto-char beg)
-			      (insert (concat start (unless no-newlines "\n")))
-			      (unless no-escape
-				(org-escape-code-in-region (min (point) end) end))
-			      (goto-char end)
-			      (unless no-newlines (goto-char (point-at-eol)))
-			      (setq end (point-marker))))
+		(let ((wrap
+		       (lambda (start finish &optional no-escape no-newlines
+				 inline-start inline-finish)
+			 (when inline
+			   (setq start inline-start)
+			   (setq finish inline-finish)
+			   (setq no-newlines t))
+			 (let ((before-finish (marker-position end)))
+			   (goto-char end)
+			   (insert (concat finish (unless no-newlines "\n")))
+			   (goto-char beg)
+			   (insert (concat start (unless no-newlines "\n")))
+			   (unless no-escape
+			     (org-escape-code-in-region
+			      (min (point) before-finish) before-finish))
+			   (goto-char end))))
 		      (tabulablep
 		      (tabulablep
 		       (lambda (r)
 		       (lambda (r)
 			 ;; Non-nil when result R can be turned into
 			 ;; Non-nil when result R can be turned into
@@ -2337,7 +2338,7 @@ INFO may provide the values of these header arguments (in the
 		    (insert (org-macro-escape-arguments
 		    (insert (org-macro-escape-arguments
 			     (org-babel-chomp result "\n"))))
 			     (org-babel-chomp result "\n"))))
 		   (t (goto-char beg) (insert result)))
 		   (t (goto-char beg) (insert result)))
-		  (setq end (point-marker))
+		  (setq end (copy-marker (point) t))
 		  ;; possibly wrap result
 		  ;; possibly wrap result
 		  (cond
 		  (cond
 		   ((assq :wrap (nth 2 info))
 		   ((assq :wrap (nth 2 info))
@@ -2374,11 +2375,12 @@ INFO may provide the values of these header arguments (in the
 		   ((and (not (funcall tabulablep result))
 		   ((and (not (funcall tabulablep result))
 			 (not (member "file" result-params)))
 			 (not (member "file" result-params)))
 		    (let ((org-babel-inline-result-wrap
 		    (let ((org-babel-inline-result-wrap
-			   ;; Hard code {{{results(...)}}} on top of customization.
+			   ;; Hard code {{{results(...)}}} on top of
+			   ;; customization.
 			   (format "{{{results(%s)}}}"
 			   (format "{{{results(%s)}}}"
 				   org-babel-inline-result-wrap)))
 				   org-babel-inline-result-wrap)))
-		      (org-babel-examplify-region beg end results-switches inline)
-		      (setq end (point))))))
+		      (org-babel-examplify-region
+		       beg end results-switches inline)))))
 		;; Possibly indent results in par with #+results line.
 		;; Possibly indent results in par with #+results line.
 		(when (and (not inline) (numberp indent) (> indent 0)
 		(when (and (not inline) (numberp indent) (> indent 0)
 			   ;; In this case `table-align' does the work
 			   ;; In this case `table-align' does the work
@@ -2391,6 +2393,7 @@ INFO may provide the values of these header arguments (in the
 			(message "Code block returned no value.")
 			(message "Code block returned no value.")
 		      (message "Code block produced no output."))
 		      (message "Code block produced no output."))
 		  (message "Code block evaluation complete.")))
 		  (message "Code block evaluation complete.")))
+	    (set-marker end nil)
 	    (when outside-scope (narrow-to-region visible-beg visible-end))
 	    (when outside-scope (narrow-to-region visible-beg visible-end))
 	    (set-marker visible-beg nil)
 	    (set-marker visible-beg nil)
 	    (set-marker visible-end nil)))))))
 	    (set-marker visible-end nil)))))))

+ 51 - 1
testing/lisp/test-ob.el

@@ -1405,7 +1405,57 @@ echo \"$data\"
       (org-babel-execute-src-block)
       (org-babel-execute-src-block)
       (let ((case-fold-search t)) (search-forward "RESULTS"))
       (let ((case-fold-search t)) (search-forward "RESULTS"))
       (list (org-get-indentation)
       (list (org-get-indentation)
-	    (progn (forward-line) (org-get-indentation)))))))
+	    (progn (forward-line) (org-get-indentation))))))
+  ;; Properly indent examplified blocks.
+  (should
+   (equal
+    "   #+begin_example
+   0
+   1
+   2
+   3
+   4
+   5
+   6
+   7
+   8
+   9
+   #+end_example
+"
+    (org-test-with-temp-text
+	"   #+begin_src emacs-lisp :results output
+   (dotimes (i 10) (princ i) (princ \"\\n\"))
+   #+end_src"
+      (org-babel-execute-src-block)
+      (search-forward "begin_example")
+      (downcase
+       (buffer-substring-no-properties (line-beginning-position)
+				       (point-max))))))
+  ;; Properly indent "org" blocks.
+  (should
+   (equal
+    "   #+begin_src org
+   0
+   1
+   2
+   3
+   4
+   5
+   6
+   7
+   8
+   9
+   #+end_src
+"
+    (org-test-with-temp-text
+	"   #+begin_src emacs-lisp :results output org
+   (dotimes (i 10) (princ i) (princ \"\\n\"))
+   #+end_src"
+      (org-babel-execute-src-block)
+      (search-forward "begin_src org")
+      (downcase
+       (buffer-substring-no-properties (line-beginning-position)
+				       (point-max)))))))
 
 
 (ert-deftest test-ob/safe-header-args ()
 (ert-deftest test-ob/safe-header-args ()
   "Detect safe and unsafe header args."
   "Detect safe and unsafe header args."