On 2026-05-21 19:57, Jacob S. Gordon wrote:
> Please find a small patch attached that should fix this behaviour.

I should have known this would be symmetric, i.e. there’s another bug
with the start of the block, where the selection

  some [example text]

leads to a even worse block:

  some #+begin_example
  example text#+end_example

Please find a v2 attached and apologies for the noise.

Thanks,

-- 
Jacob S. Gordon
[email protected]
Please don’t send me HTML emails or MS Office/Apple iWork documents.
https://useplaintext.email/#etiquette
https://www.fsf.org/campaigns/opendocument
From 4a485f6b417f8a5efb5bcd3d4250bb81df02ea7b Mon Sep 17 00:00:00 2001
From: "Jacob S. Gordon" <[email protected]>
Date: Thu, 21 May 2026 16:20:00 -0400
Subject: [PATCH v2] ; org-babel-examplify-region: Correct insertion of blocks

When the region is not preceded or followed by a newline, the
'#+begin' and '#+end' markers were not placed on their own lines,
leading to a syntax error.

* lisp/ob-core.el (org-babel-examplify-region): Ensure that example
block markers appear on their own lines.
* testing/lisp/test-ob.el (test-ob/preserve-results-indentation):
Remove trailing newline.
(test-ob/org-babel-examplify-region-block): Add tests.

Reported-by: Kristoffer <[email protected]>
Link: https://list.orgmode.org/orgmode/[email protected]/
---
 lisp/ob-core.el         |  5 ++++-
 testing/lisp/test-ob.el | 24 ++++++++++++++++++++++--
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 823b27b72..f55b91244 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2996,6 +2996,7 @@ (defun org-babel-examplify-region (beg end &optional results-switches inline)
 		   (forward-line 0) (insert ": ") (forward-line 1)))
 		(t
 		 (goto-char beg)
+		 (unless (bolp) (insert "\n"))
 		 (insert (if results-switches
 			     (format "%s%s\n"
 				     (funcall maybe-cap "#+begin_example")
@@ -3004,7 +3005,9 @@ (defun org-babel-examplify-region (beg end &optional results-switches inline)
 		 (let ((p (point)))
 		   (if (markerp end) (goto-char end) (forward-char (- end beg)))
 		   (org-escape-code-in-region p (point)))
-		 (insert (funcall maybe-cap "#+end_example\n")))))))))
+		 (unless (bolp) (insert "\n"))
+		 (insert (funcall maybe-cap "#+end_example"))
+		 (unless (eolp) (insert "\n")))))))))
 
 (defun org-babel-update-block-body (new-body)
   "Update the body of the current code block to NEW-BODY."
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index e94ba6ee3..54c7b866b 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -1772,8 +1772,7 @@ (ert-deftest test-ob/preserve-results-indentation ()
    7
    8
    9
-   #+end_example
-"
+   #+end_example"
     (org-test-with-temp-text
 	"   #+begin_src emacs-lisp :results output
    (dotimes (i 10) (princ i) (princ \"\\n\"))
@@ -2892,6 +2891,27 @@ (ert-deftest test-ob/keep-case ()
     (let ((case-fold-search nil))
       (should (looking-at-p "#\\+BEGIN_SRC")))))
 
+(ert-deftest test-ob/org-babel-examplify-region-block ()
+  "Ensure correct wrapping of example blocks."
+  (let* ((org-babel-min-lines-for-block-output 0)
+         (prefix "text to ")
+         (region "make an example of")
+         (text (concat prefix region))
+         (offset (length prefix)))
+    (org-test-with-temp-text text
+      (let* ((min (point-min))
+             (start (+ min offset)))
+        (org-babel-examplify-region start (pos-eol))
+        (goto-char min)
+        (should
+         (string= (buffer-substring-no-properties min (pos-eol))
+                  prefix))
+        (forward-line)
+        (let ((eap (org-element-at-point)))
+          (should (org-element-type-p eap 'example-block))
+          (should (string= (org-element-property :value eap)
+                           (concat region "\n"))))))))
+
 (provide 'test-ob)
 
 ;;; test-ob.el ends here

base-commit: 1f5bba867b25b212b4d6d081879110e696d31813
-- 
Jacob S. Gordon
[email protected]
Please don’t send me HTML emails or MS Office/Apple iWork documents.
https://useplaintext.email/#etiquette
https://www.fsf.org/campaigns/opendocument

Reply via email to