Browse Source

org-src: Preserve tab characters unrelated to indentation

* lisp/org-src.el (org-edit-src-code): Always preserve tabs not related
  to indentation.

* testing/lisp/test-org-src.el (test-org-src/preserve-tabs): New test.
Nicolas Goaziou 5 years ago
parent
commit
50a18201c7
2 changed files with 40 additions and 6 deletions
  1. 5 6
      lisp/org-src.el
  2. 35 0
      testing/lisp/test-org-src.el

+ 5 - 6
lisp/org-src.el

@@ -851,13 +851,12 @@ name of the sub-editing buffer."
 	    `(lambda ()
 	       (unless ,(or org-src-preserve-indentation
 			    (org-element-property :preserve-indent element))
-		 (untabify (point-min) (point-max))
 		 (when (> org-edit-src-content-indentation 0)
-		   (let ((ind (make-string org-edit-src-content-indentation
-					   ?\s)))
-		     (while (not (eobp))
-		       (unless (looking-at "[ \t]*$") (insert ind))
-		       (forward-line)))))
+		   (while (not (eobp))
+		     (unless (looking-at "[ \t]*$")
+		       (indent-line-to (+ (org-get-indentation)
+					  org-edit-src-content-indentation)))
+		     (forward-line))))
 	       (org-escape-code-in-region (point-min) (point-max))))
        (and code (org-unescape-code-in-string code)))
       ;; Finalize buffer.

+ 35 - 0
testing/lisp/test-org-src.el

@@ -98,5 +98,40 @@ blah
 #+end_src
 ")))))
 
+(ert-deftest test-org-src/preserve-tabs ()
+  "Editing block preserve tab characters."
+  ;; With `org-src-preserve-indentation' set to nil.
+  (should
+   (equal "
+#+begin_src emacs-lisp
+  This is a tab: 	.
+#+end_src"
+	  (org-test-with-temp-text
+	      "
+#+begin_src emacs-lisp
+<point>This is a tab: 	.
+#+end_src"
+	    (let ((org-edit-src-content-indentation 2)
+		  (org-src-preserve-indentation nil))
+	      (org-edit-special)
+	      (org-edit-src-exit)
+	      (buffer-string)))))
+  ;; With `org-src-preserve-indentation' set to t.
+  (should
+   (equal "
+#+begin_src emacs-lisp
+This is a tab: 	.
+#+end_src"
+	  (org-test-with-temp-text
+	      "
+#+begin_src emacs-lisp
+<point>This is a tab: 	.
+#+end_src"
+	    (let ((org-edit-src-content-indentation 2)
+		  (org-src-preserve-indentation t))
+	      (org-edit-special)
+	      (org-edit-src-exit)
+	      (buffer-string))))))
+
 (provide 'test-org-src)
 ;;; test-org-src.el ends here