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

From: Morgan Willcock <[email-masked]>
Date: Sun, 31 Dec 2023 21:20:11 +0000

Tags: patch

The attached patch fixes an issue with Tempo template insertion, where
the region start is assumed to be a position before the region end.  If
the user selects a region in a backwards direction the correct
indentation will not be applied:

  (require 'tempo)
  (tempo-define-template "indent-region-test" '(r>))

  (with-temp-buffer
    (emacs-lisp-mode)
    (insert "    one\n    two\n    three")
    (goto-char (point-min))
    (set-mark-command nil)
    (goto-char (point-max))
    (tempo-template-indent-region-test)
    (buffer-string))

  ;; => "one\ntwo\nthree"

  (with-temp-buffer
    (emacs-lisp-mode)
    (insert "    one\n    two\n    three")
    (goto-char (point-max))
    (set-mark-command nil)
    (goto-char (point-min))
    (tempo-template-indent-region-test)
    (buffer-string))

  ;; => "    one\n    two\nthree"

The start and end positions are already identified and stored using
separate variables, so the patch just uses these values instead of
assuming the point and mark order.  I've also removed passing nil as the
third argument since the argument is optional.

In GNU Emacs 29.1 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.16.0, Xaw3d scroll bars) of 2023-07-31 built on inspiron
Windowing system distributor 'The X.Org Foundation', version 11.0.12101007
System Description: Debian GNU/Linux 12 (bookworm)

Configured using:
 'configure --with-native-compilation --with-cairo --with-json
 --with-xml2 --with-x-toolkit=lucid'

>From 4d7fdafeee5f6540d5e39549fa0a9927176171bb 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 argument order is correct

* 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 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lisp/tempo.el b/lisp/tempo.el
index 45dc24dcc97..1b6cc4476e1 100644
--- a/lisp/tempo.el
+++ b/lisp/tempo.el
@@ -333,7 +333,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 +345,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