Browse Source

ob: Fix RESULTS indentation

* lisp/ob-core.el (org-babel-parse-src-block-match): Compute indentation
  taking into consideration tab width.
(org-babel-where-is-src-block-result): Do not assume indentation
consists of white space characters only.

* testing/lisp/test-ob.el (test-ob/preserve-results-indentation): Add
  test.

Reported-by: Jarmo Hurri <jarmo.hurri@iki.fi>
<http://permalink.gmane.org/gmane.emacs.orgmode/100403>
Nicolas Goaziou 9 years ago
parent
commit
6626dfb30b
2 changed files with 37 additions and 21 deletions
  1. 10 11
      lisp/ob-core.el
  2. 27 10
      testing/lisp/test-ob.el

+ 10 - 11
lisp/ob-core.el

@@ -1438,17 +1438,16 @@ specified in the properties of the current outline entry."
 (defvar org-src-preserve-indentation) ;; declare defcustom from org-src
 (defun org-babel-parse-src-block-match ()
   "Parse the results from a match of the `org-babel-src-block-regexp'."
-  (let* ((block-indentation (length (match-string 1)))
-	 (lang (org-no-properties (match-string 2)))
+  (let* ((block-indentation (string-width (match-string 1)))
+	 (lang (org-match-string-no-properties 2))
          (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
 	 (switches (match-string 3))
-         (body (org-no-properties
-		(let* ((body (match-string 5))
-		       (sub-length (- (length body) 1)))
-		  (if (and (> sub-length 0)
-			   (string= "\n" (substring body sub-length)))
-		      (substring body 0 sub-length)
-		    (or body "")))))
+         (body (let* ((body (org-match-string-no-properties 5))
+		      (sub-length (- (length body) 1)))
+		 (if (and (> sub-length 0)
+			  (string= "\n" (substring body sub-length)))
+		     (substring body 0 sub-length)
+		   (or body ""))))
 	 (preserve-indentation (or org-src-preserve-indentation
 				   (save-match-data
 				     (string-match "-i\\>" switches)))))
@@ -1972,8 +1971,8 @@ following the source block."
 	(goto-char end)
 	(unless beg
 	  (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
+	(when (wholenump indent) (indent-to indent))
 	(insert (concat
-		 (when (wholenump indent) (make-string indent ? ))
 		 "#+" org-babel-results-keyword
 		 (when hash
 		   (if org-babel-hash-show-time
@@ -1984,7 +1983,7 @@ following the source block."
 		 (when name (concat " " name)) "\n"))
 	(unless beg (insert "\n") (backward-char))
 	(beginning-of-line 0)
-	(if hash (org-babel-hide-hash))
+	(when hash (org-babel-hide-hash))
 	(point)))))
 
 (defvar org-block-regexp)

+ 27 - 10
testing/lisp/test-ob.el

@@ -1289,16 +1289,33 @@ echo \"$data\"
 (ert-deftest test-ob/preserve-results-indentation ()
   "Preserve indentation when executing a src block."
   (should
-   (equal '(2 2)
-	  (org-test-with-temp-text
-	      "  #+begin_src emacs-lisp\n  (+ 1 1)\n  #+end_src"
-	    (org-babel-execute-src-block)
-	    (buffer-string)
-	    (let ((case-fold-search t)) (search-forward "#+results:"))
-	    ;; Check if both #+RESULTS: keyword and actual results are
-	    ;; indented by 2 columns.
-	    (list (org-get-indentation)
-		  (progn (forward-line) (org-get-indentation)))))))
+   (equal
+    '(2 2)
+    (org-test-with-temp-text "  #+begin_src emacs-lisp\n(+ 1 1)\n  #+end_src"
+      (org-babel-execute-src-block)
+      (let ((case-fold-search t)) (search-forward "RESULTS"))
+      (list (org-get-indentation)
+	    (progn (forward-line) (org-get-indentation))))))
+  (should
+   (equal
+    '(2 2)
+    (org-test-with-temp-text
+	"  #+name: block\n  #+begin_src emacs-lisp\n(+ 1 1)\n  #+end_src"
+      (org-babel-execute-src-block)
+      (let ((case-fold-search t)) (search-forward "RESULTS"))
+      (list (org-get-indentation)
+	    (progn (forward-line) (org-get-indentation))))))
+  ;; Don't get fooled by TAB-based indentation.
+  (should
+   (equal
+    '(6 6)
+    (org-test-with-temp-text
+	"\t  #+begin_src emacs-lisp\n\t  (+ 1 1)\n\t  #+end_src"
+      (setq tab-width 4)
+      (org-babel-execute-src-block)
+      (let ((case-fold-search t)) (search-forward "RESULTS"))
+      (list (org-get-indentation)
+	    (progn (forward-line) (org-get-indentation)))))))
 
 (ert-deftest test-ob/safe-header-args ()
   "Detect safe and unsafe header args."