Hey Kristoffer,

On 2021-11-20 20:18, Kristoffer wrote:
> I've been testing out org-babel-examplify-region in my bookshelf
> file to mark excerpts from books, but there's an issue I have
> encountered. If there is a #+end_example after the currently marked
> region, once you run org-babel-examplify-region, it does not create
> a #+end_example at the end of the marked region properly unless the
> last line marked includes the line end.

Please find a small patch attached that should fix this behaviour.
The conditional newline at the end of the example block prevents
this selection:

  [example text]
  over two lines

from becoming:

  #+begin_example
  example text
  #+end_example

  over two lines

instead of:

  #+begin_example
  example text
  #+end_example
  over two lines

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 9dab6ccc218012b9e47d5e0af7208e6a6a0a6b83 Mon Sep 17 00:00:00 2001
From: "Jacob S. Gordon" <[email protected]>
Date: Thu, 21 May 2026 16:20:00 -0400
Subject: [PATCH v1] ; org-babel-examplify-region: Handle regions without a
 final newline

When the region doesn't contain a final newline, the '#+end_example'
might not appear on its own line, leading to a syntax error.

* lisp/ob-core.el (org-babel-examplify-region): Add newline to regions
missing them when inserting blocks.
* testing/lisp/test-ob.el (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         |  4 +++-
 testing/lisp/test-ob.el | 22 ++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 823b27b72..47b78b5fb 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -3004,7 +3004,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..d5b5a8b8d 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -2892,6 +2892,28 @@ (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 when the region doesn't have a final newline."
+  (let* ((org-babel-min-lines-for-block-output 0)
+         (text "text to make an example of")
+         (select-up-to (- (length text) 2))
+         (region (substring text 0 select-up-to))
+         (excluded (substring text select-up-to)))
+    (org-test-with-temp-text text
+      (let* ((min (point-min))
+             (max (+ min select-up-to)))
+        (org-babel-examplify-region min max)
+        (goto-char min)
+        (let* ((eap (org-element-at-point))
+               (contents (org-element-property :value eap))
+               (end (org-element-property :end eap)))
+          (should (org-element-type-p (org-element-at-point)
+                                      'example-block))
+          (should (string= contents
+                           (concat region "\n")))
+          (should (string= (buffer-substring end (point-max))
+                           excluded)))))))
+
 (provide 'test-ob)
 
 ;;; test-ob.el ends here

base-commit: d34e34f3e6c191147ec0e1d3c0a6f9cc41bf6b6d
-- 
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