Hey everyone,

While looking into [a bug] recently, I noticed that if the region only
includes part of a line, ‘org-insert-structure-template’ will wrap the
rest of it too.  For example, with the region ‘[]’:

  a [part of] a sentence

the block becomes

  a 
  #+begin_quote
  part of a sentence
  #+end_quote

instead of what I think is promised by the docstring:

  a 
  #+begin_quote
  part of
  #+end_quote
   a sentence

I’ve attached a patch which fixes this.

[a bug]
<https://list.orgmode.org/[email protected]>

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 31e315235e43d6439fcb3b17eddab5dba65ec0cb Mon Sep 17 00:00:00 2001
From: "Jacob S. Gordon" <[email protected]>
Date: Wed, 27 May 2026 16:20:00 -0400
Subject: [PATCH v1] ; org-insert-structure-template: Don't include parts
 outside of region

* lisp/org.el (org-insert-structure-template): Don't wrap the rest of
the line when the region doesn't include it.
* testing/lisp/test-org.el (test-org/insert-structure-template): Add
test.
---
 lisp/org.el              | 1 +
 testing/lisp/test-org.el | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/lisp/org.el b/lisp/org.el
index cb36497f4..dafdefed5 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -9561,6 +9561,7 @@ (defun org-insert-structure-template (type)
 	  (goto-char region-end)
 	  ;; Ignore empty lines at the end of the region.
 	  (skip-chars-backward " \r\t\n")
+	  (unless (eolp) (insert "\n") (forward-line -1))
 	  (end-of-line))
 	(unless (bolp) (insert "\n"))
 	(indent-to column)
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 4ba7aa560..16fba782a 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -5838,6 +5838,14 @@ (ert-deftest test-org/insert-structure-template ()
 	      (end-of-line)
 	      (org-insert-structure-template "foo")
 	      (buffer-string))))
+  ;; Include only parts of the line within the region.
+  (should
+   (string= "a \n#+begin_quote\npart of\n#+end_quote\n a sentence"
+	    (org-test-with-temp-text "a <point>part of a sentence"
+	      (transient-mark-mode 1)
+	      (mark-word 2)
+	      (org-insert-structure-template "quote")
+	      (buffer-string))))
   ;; Middle of paragraph.
   (should
    (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"

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