From 42b39b1a08299e3157c74bd80e9741a27f31c6d6 Mon Sep 17 00:00:00 2001
From: Derek Chen-Becker <oss@chen-becker.org>
Date: Tue, 8 Jul 2025 07:21:22 -0600
Subject: [PATCH] lisp/org.el: Handle numeric priorities in `org-entry-put'

* lisp/org.el (org-entry-put): Utilize `org-priority-to-value' to parse
priority values so that numeric priorites are handled properly.
* testing/lisp/test-org.el (test-org/entry-put): Add unit tests to cover
reported error cases, including a valid numeric priority, a two-digit
numeric priority, and a numeric priority outside of the defined range.

Reported-by: "Tommy Jollyboat" <jollyboatbros@gmail.com>
Link: https://list.orgmode.org/orgmode/CANDZv_N394TYMy30-KuEQV+eZ0FEkm4GSjTrkUtDViK_-DkX-Q@mail.gmail.com/
---
 lisp/org.el              |  2 +-
 testing/lisp/test-org.el | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/lisp/org.el b/lisp/org.el
index 0a406d7cc..00d6db073 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -13376,7 +13376,7 @@ decreases scheduled or deadline date by one day."
 	 (org-todo value)
 	 (when org-auto-align-tags (org-align-tags)))
         ((equal property "PRIORITY")
-	 (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
+	 (org-priority (if (org-string-nw-p value) (org-priority-to-value value) ?\s))
 	 (when org-auto-align-tags (org-align-tags)))
         ((equal property "SCHEDULED")
 	 (forward-line)
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 36dea35b7..51c3ec477 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -7355,6 +7355,29 @@ Paragraph<point>"
 	  (org-test-with-temp-text "* [#B] H"
 	    (org-entry-put (point) "PRIORITY" nil)
 	    (buffer-string))))
+  (should ;; Set a numeric priority
+   (equal "* [#3] H"
+          (org-test-with-temp-text "* H"
+                                   (let ((org-priority-lowest 3)
+                                         (org-priority-highest 1)
+                                         (org-priority-default 2))
+                                     (org-entry-put (point) "PRIORITY" "3")
+                                     (buffer-string)))))
+  (should ;; Set a numeric priority with double digits
+   (equal "* [#20] H"
+          (org-test-with-temp-text "* H"
+                                   (let ((org-priority-lowest 20)
+                                         (org-priority-highest 1)
+                                         (org-priority-default 2))
+                                     (org-entry-put (point) "PRIORITY" "20")
+                                     (buffer-string)))))
+  (should-error ;; Attempt to set a priority outside of the highest/lowest range
+   (org-test-with-temp-text "* H"
+                            (let ((org-priority-lowest 3)
+                                  (org-priority-highest 1)
+                                  (org-priority-default 2))
+                              (org-entry-put (point) "PRIORITY" "42")
+                              (buffer-string))))
   ;; Set "SCHEDULED" property.
   (should
    (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
-- 
2.43.0

