Subject: Re: [PATCH] Ensure indent-region argument order is correct

From: Morgan Willcock <[email-masked]>
Date: Mon, 01 Jan 2024 17:04:30 +0000

Attached is a replacement patch which I believe fixes the issue with the
region selection direction and with indentation potentially being
incorrectly applied to text that is inserted by the template.

Previously when text was being inserted at the region start it was
actually going into region, which means that it will have been possible
for template contents to have accidentally been indented along with the
region that the user selected.  I've changed the marker insertion type
for the start boundary, which should preserve the original indentation
of text which was inserted by the template, i.e. if text inserted by the
template should be indented this is now back under control of the
template rather than applied by accident.

The changes do mean that the indentation of existing templates may
change, but I think this will only be for templates which accidentally
omitted the indentation directives because indentation was mistakenly
applied as part of the region.

>From e667cd5abbdc7788726ec0da29b95b24a44a7923 Mon Sep 17 00:00:00 2001
From: Morgan Willcock <[email-masked]>
Date: Sun, 31 Dec 2023 20:47:17 +0000
Subject: [PATCH] Ensure indent-region arguments and boundaries are correct

* lisp/tempo.el: Set marker type for tempo-region-start to move when
  text is inserted at its position.  This prevents the template from
  inserting text into the region.

* lisp/tempo.el (tempo-insert): Call indent-region with the stored
region markers to ensure that the start and end arguments are used in
the correct order.
---
 lisp/tempo.el | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lisp/tempo.el b/lisp/tempo.el
index 45dc24dcc97..56f8a8389a2 100644
--- a/lisp/tempo.el
+++ b/lisp/tempo.el
@@ -197,6 +197,9 @@ tempo-named-insertions
 
 (defvar-local tempo-region-start (make-marker)
   "Region start when inserting around the region.")
+;; Insertion by the template at the region start position should move
+;; the marker to preserve the original region contents.
+(set-marker-insertion-type tempo-region-start t)
 
 (defvar-local tempo-region-stop (make-marker)
   "Region stop when inserting around the region.")
@@ -333,7 +336,8 @@ tempo-insert
     (`(r> . ,rest) (if on-region
                        (progn
                          (goto-char tempo-region-stop)
-                         (indent-region (mark) (point) nil))
+                         (indent-region tempo-region-start
+                                        tempo-region-stop))
                        (tempo-insert-prompt-compat rest)))
     (`(s ,name) (tempo-insert-named name))
     (`(l . ,rest) (dolist (elt rest) (tempo-insert elt on-region)))
@@ -344,7 +348,7 @@ tempo-insert
     ('r> (if on-region
 	     (progn
 	       (goto-char tempo-region-stop)
-	       (indent-region (mark) (point) nil))
+	       (indent-region tempo-region-start tempo-region-stop))
 	   (tempo-insert-mark (point-marker))))
     ('> (indent-according-to-mode))
     ('& (if (not (or (= (current-column) 0)
-- 
2.39.2