From c5d9197700257f1505a70c0cfee0aa83619a4399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= Date: Sat, 17 Jun 2023 19:07:50 +0200 Subject: [PATCH] org.el (org-indent-line): Fix to the indentation inside src blocks * lisp/org.el (org-indent-line): When indenting a line inside a src block, preindent it with the common block indentation before calling native indentation. * testing/lisp/test-org-src.el (test-org-src/org-return-indents): Test indentation inside src blocks, when calling `org-return'. --- lisp/org.el | 19 +++++++++++++------ testing/lisp/test-org-src.el | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 4273385bf..38d13f111 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -19083,20 +19083,27 @@ Also align node properties according to `org-property-format'." (org-with-point-at (org-element-property :end element) (skip-chars-backward " \t\n") (line-beginning-position)))) - ;; At the beginning of a blank line, do some preindentation. This - ;; signals org-src--edit-element to preserve the indentation on exit - (when (and (looking-at-p "^[[:space:]]*$") - (not org-src-preserve-indentation)) - (let (block-content-ind some-ind) + ;; Do some preindentation, to add the common block + ;; indentation to the current line. + (when (not org-src-preserve-indentation) + (let ((current-ind (org-current-text-indentation)) + block-content-ind some-ind) (org-with-point-at (org-element-property :begin element) (setq block-content-ind (+ (org-current-text-indentation) org-edit-src-content-indentation)) + ;; Check that the first line of the block has the + ;; minimal indentation (forward-line) (save-match-data (re-search-forward "^[ \t]*\\S-" nil t)) (backward-char) (setq some-ind (if (looking-at-p "#\\+end_src") block-content-ind (org-current-text-indentation)))) - (indent-line-to (min block-content-ind some-ind)))) + ;; If the current line is the first one and does not + ;; have the minimal indentation, we do not preindent, + ;; since it could break the relative indentation with + ;; respect to the following lines + (when (< current-ind (min block-content-ind some-ind)) + (indent-line-to (min block-content-ind some-ind))))) (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))) (t (let ((column (org--get-expected-indentation element nil))) diff --git a/testing/lisp/test-org-src.el b/testing/lisp/test-org-src.el index 42edb364a..4d943927d 100644 --- a/testing/lisp/test-org-src.el +++ b/testing/lisp/test-org-src.el @@ -144,6 +144,40 @@ This is a tab:\t. (org-edit-src-exit) (buffer-string)))))) +(ert-deftest test-org-src/org-return-indents () + "Calling `org-return' indents newline." + (should + (equal " +#+begin_src emacs-lisp + (or c1\n \n c2) +#+end_src" + (org-test-with-temp-text + " +#+begin_src emacs-lisp + (or c1 + c2) +#+end_src" + (let ((org-edit-src-content-indentation 2) + (org-src-tab-acts-natively t) + (org-src-preserve-indentation nil)) + (org-return t) + (buffer-string))))) + (should + (equal " +#+begin_src emacs-lisp + (or c1 + c2) +#+end_src" + (org-test-with-temp-text + " +#+begin_src emacs-lisp + (or c1 c2) +#+end_src" + (let ((org-edit-src-content-indentation 2) + (org-src-preserve-indentation nil)) + (org-return t) + (buffer-string)))))) + (ert-deftest test-org-src/preserve-empty-lines () "Editing block preserves empty lines." (should -- 2.41.0