> Not just src-blocks, I think.
> Also other kinds of verbatim blocks: example blocks, latex environments,
> and comment blocks.

All block elements which are not greater elements, so verse and export as well.
Here's a cleaned up version of the patch.

> In the future, there will be :value-being/:value-end property to avoid
> the skip-chars-backwad dance, but not yet.
> See https://git.sr.ht/~yantar92/org-mode/commit/f8e2eeae21e99492cb

Should we add a comment noting this, or are you keeping track in your branch?

LRA


Mar 29, 2025, 11:19 by yanta...@posteo.net:

> l...@phdk.org writes:
>
>> 1. `org-indent-line' indents the end tag of some blocks to match the
>> begin tag, but doesn't do so with src blocks, among others.
>>
>> MRE: With the pointer before =#+end_src=  in the following, do `M-x 
>> org-indent-line'
>>   #+begin_src fundamental
>>     foo
>> #+end_src
>> Expected: line is indented by 2
>> Actual: line is indented by 4
>>
>> I thought it was a bit weird since the code for calculating the src
>> block content indent does handle it if the block itself is indented, and
>> it worked fine for other block types. It seems this is because src
>> blocks (+ export, example, et al) aren't greater elements and don't
>> have a `contents-end' property.
>>
>
> Not just src-blocks, I think.
> Also other kinds of verbatim blocks: example blocks, latex environments,
> and comment blocks.
>
> Note that indenting region does indent #+end_... tag.
>
>> I was looking at fixing it for all cases but realized it would require
>> messing with the element parsers which I'm honestly really not looking
>> to get into, but I've attached a draft patch that works for src
>> blocks. It's a pretty theoretical problem, but if you think it's worth
>> fixing and that the patch makes sense I'd be happy to clean it up and
>> add a test.
>>
>
> Yes, I do think that it is worth fixing.
>
>> -      ;; POS is after contents in a greater element.  Indent like
>> -      ;; the beginning of the element.
>> -      ((and (memq type org-element-greater-elements)
>> -            (let ((cend (org-element-contents-end element)))
>> +      ;; POS is after contents in a greater element or src-block.
>> +      ;; Indent like the beginning of the element.
>> +      ((and (or (memq type org-element-greater-elements)
>> +                    (org-element-type-p element 'src-block))
>> +            (let ((cend (or (org-element-contents-end element)
>> +                                (org-with-wide-buffer
>> +                             (goto-char (org-element-end element))
>> +                             (skip-chars-backward " \r\t\n")
>> +                             (line-beginning-position)))))
>>
>
> Looks reasonable, other than that the same should probably be done for
> other kinds of verbatim blocks.
>
> In the future, there will be :value-being/:value-end property to avoid
> the skip-chars-backwad dance, but not yet.
> See https://git.sr.ht/~yantar92/org-mode/commit/f8e2eeae21e99492cb
>
> -- 
> Ihor Radchenko // yantar92,
> Org mode maintainer,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>
>

>From 079bcfbd0d6480bf6acf86b4e27f52da3d766d82 Mon Sep 17 00:00:00 2001
From: Lukas Rudd Andersen <l...@phdk.org>
Date: Fri, 28 Mar 2025 03:16:09 +0100
Subject: [PATCH] org--get-expected-indentation: Indent end tag of blocks of
 all types

* lisp/org.el (org--get-expected-indentation): Add all block types to
  the check for indenting end tags.

* testing/lisp/test-org.el (test-org/indent-line): Add tests.

Link: https://list.orgmode.org/877c48jg29.fsf@localhost/
TINYCHANGE
---
 lisp/org.el              | 14 ++++++++++----
 testing/lisp/test-org.el | 18 ++++++++++--------
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index ec6aa7580..04769443b 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -19363,10 +19363,16 @@ ELEMENT."
 	  ;; and contents.
 	  ((and post-affiliated (= (line-beginning-position) post-affiliated))
 	   (org--get-expected-indentation element t))
-	  ;; POS is after contents in a greater element.  Indent like
-	  ;; the beginning of the element.
-	  ((and (memq type org-element-greater-elements)
-		(let ((cend (org-element-contents-end element)))
+	  ;; POS is after contents in a greater element or other block.
+	  ;; Indent like the beginning of the element.
+	  ((and (or (memq type org-element-greater-elements)
+                    (memq type '(comment-block example-block export-block
+                                               src-block verse-block)))
+		(let ((cend (or (org-element-contents-end element)
+                                (org-with-wide-buffer
+			         (goto-char (org-element-end element))
+			         (skip-chars-backward " \r\t\n")
+			         (line-beginning-position)))))
 		  (and cend (<= cend pos))))
 	   ;; As a special case, if point is at the end of a footnote
 	   ;; definition or an item, indent like the very last element
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index a55c4162e..eefc9b692 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -1184,14 +1184,16 @@ Otherwise, evaluate RESULT as an sexp and return its result."
     (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
       (let ((org-adapt-indentation t)) (org-indent-line))
       (org-get-indentation))))
-  ;; After the end of the contents of a greater element, indent like
-  ;; the beginning of the element.
-  (should
-   (= 1
-      (org-test-with-temp-text
-	  " #+BEGIN_CENTER\n  Contents\n<point>#+END_CENTER"
-	(org-indent-line)
-	(org-get-indentation))))
+  ;; After the end of the contents of a greater element or other
+  ;; block, indent like the beginning of the element.
+  (mapcar (lambda (type)
+	    (should
+	     (= 1
+		(org-test-with-temp-text
+		    (format " #+BEGIN_%1$s\n  Contents\n<point>#+END_%1$s" type)
+		  (org-indent-line)
+		  (current-indentation)))))
+	  '("CENTER" "COMMENT" "EXAMPLE" "EXPORT" "SRC" "VERSE"))
   ;; On blank lines after a paragraph, indent like its last non-empty
   ;; line.
   (should
-- 
2.47.2

Reply via email to