branch: externals/org
commit 6f78cd797cff6d5c42f22c1c1f879520a629dc52
Author: Derek Chen-Becker <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    lisp/org-mobile.el: Fix numeric priorities with org-mobile
    
    * lisp/org.el (org-priority-value-regexp, org-priority-regexp,
    org-set-regexps-and-options): Move `org-priority-value-regexp' and
    `org-priority-regexp' alongside other early regexps to prevent a "reference
    to free variable" warning.  Update `org-set-regexps-and-options' to use the
    existing `org-priority-value-regexp' for the priority value pattern in the
    `org-complex-heading-regexp' and `org-complex-heading-regexp-format'
    regexps so that all valid priority values are matched.
    
    * lisp/org-mobile.el (org-mobile-edit): Fix handling of numeric priorities
    by parsing with `org-priority-to-value' and using the full priority
    substring from the cookie for the current value.
    
    * testing/lisp/test-org-mobile.el: Add a new file for unit tests related to
    the `org-mobile' functionality.  Add a unit test to exercise handling of
    numeric priorities with the `org-mobile-edit' function.
---
 lisp/org-mobile.el              |  4 ++--
 lisp/org.el                     | 40 ++++++++++++++++++++--------------------
 testing/lisp/test-org-mobile.el | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 22 deletions(-)

diff --git a/lisp/org-mobile.el b/lisp/org-mobile.el
index da3b42f4f5..d76aef4608 100644
--- a/lisp/org-mobile.el
+++ b/lisp/org-mobile.el
@@ -1035,13 +1035,13 @@ be returned that indicates what went wrong."
      ((eq what 'priority)
       (let ((case-fold-search nil))
        (when (looking-at org-complex-heading-regexp)
-         (let ((current (and (match-end 3) (substring (match-string 3) 2 3))))
+         (let ((current (and (match-end 3) (substring (match-string 3) 2 -1))))
            (cond
             ((equal current new) t)    ;no action required
             ((or (equal current old)
                  (eq org-mobile-force-mobile-change t)
                  (memq 'tags org-mobile-force-mobile-change))
-             (org-priority (and new (string-to-char new))))
+             (org-priority (and new (org-priority-to-value new))))
             (t (error "Priority was expected to be %s, but is %s"
                       old current)))))))
 
diff --git a/lisp/org.el b/lisp/org.el
index c803eef450..7905b79ed1 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -123,6 +123,24 @@ sure that we are at the beginning of the line.")
   "Matches a headline, putting stars and text into groups.
 Stars are put in group 1 and the trimmed body in group 2.")
 
+(defvar org-priority-value-regexp "[A-Z]\\|[0-9]\\|[1-5][0-9]\\|6[0-4]"
+  "Regular expression matching valid priority values.
+The priority value must be a capital Latin
+alphabetic character, A through Z, or can be an integer value in the range 0
+through 64.")
+
+(defvar org-priority-regexp
+  (format ".*?\\(\\[#\\(%s\\)\\] ?\\)" org-priority-value-regexp)
+  "Regular expression matching the priority indicator.
+A priority indicator can be e.g. [#A] or [#1].
+The value of the priority cookie must be a capital Latin
+alphabetic character, A through Z, or can be an integer value in
+the range 0 through 64.
+This regular expression matches these groups:
+0 : the whole match, e.g. \"TODO [#A] Hack\"
+1 : the priority cookie, e.g. \"[#A]\"
+2 : the value of the priority cookie, e.g. \"A\".")
+
 (declare-function calendar-check-holidays "holidays" (date))
 (declare-function cdlatex-environment "ext:cdlatex" (environment item))
 (declare-function cdlatex-math-symbol "ext:cdlatex")
@@ -4591,14 +4609,14 @@ related expressions."
              org-complex-heading-regexp
              (concat "^\\(\\*+\\)"
                      "\\(?: +" org-todo-regexp "\\)?"
-                     "\\(?: +\\(\\[#.\\]\\)\\)?"
+                     (format "\\(?: +\\(\\[#\\(?:%s\\)\\]\\)\\)?" 
org-priority-value-regexp)
                      "\\(?: +\\(.*?\\)\\)??"
                      "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?"
                      "[ \t]*$")
              org-complex-heading-regexp-format
              (concat "^\\(\\*+\\)"
                      "\\(?: +" org-todo-regexp "\\)?"
-                     "\\(?: +\\(\\[#.\\]\\)\\)?"
+                     (format "\\(?: +\\(\\[#\\(?:%s\\)\\]\\)\\)?" 
org-priority-value-regexp)
                      "\\(?: +"
                       ;; Headline might be commented
                       "\\(?:" org-comment-string " +\\)?"
@@ -11302,24 +11320,6 @@ from the `before-change-functions' in the current 
buffer."
 
 ;;;; Priorities
 
-(defvar org-priority-value-regexp "[A-Z]\\|[0-9]\\|[1-5][0-9]\\|6[0-4]"
-  "Regular expression matching valid priority values.
-The priority value must be a capital Latin
-alphabetic character, A through Z, or can be an integer value in the range 0
-through 64.")
-
-(defvar org-priority-regexp
-  (format ".*?\\(\\[#\\(%s\\)\\] ?\\)" org-priority-value-regexp)
-  "Regular expression matching the priority indicator.
-A priority indicator can be e.g. [#A] or [#1].
-The value of the priority cookie must be a capital Latin
-alphabetic character, A through Z, or can be an integer value in
-the range 0 through 64.
-This regular expression matches these groups:
-0 : the whole match, e.g. \"TODO [#A] Hack\"
-1 : the priority cookie, e.g. \"[#A]\"
-2 : the value of the priority cookie, e.g. \"A\".")
-
 (defun org-priority-valid-cookie-string-p (priority)
   "Return t if the PRIORITY is a valid priority cookie, nil otherwise."
   (cond
diff --git a/testing/lisp/test-org-mobile.el b/testing/lisp/test-org-mobile.el
new file mode 100644
index 0000000000..7b9275824e
--- /dev/null
+++ b/testing/lisp/test-org-mobile.el
@@ -0,0 +1,41 @@
+;;; test-org-mobile.el --- Tests for org-mobile.el -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2025 Derek Chen-Becker
+
+;; Authors: Derek Chen-Becker <[email protected]>
+
+;; This file is not part of GNU Emacs.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Unit tests for Org Mobile library.
+
+;;; Code:
+
+(require 'org-mobile)
+
+(ert-deftest test-org-mobile/org-mobile-edit ()
+  "Test `org-mobile-edit' functionality."
+  (should
+   (equal "* [#42] H"
+          (let ((org-priority-highest 40)
+                (org-priority-lowest 50))
+            (org-test-with-temp-text "* [#48] H"
+              (org-mobile-edit 'priority "48" "42")
+              (buffer-string))))))
+
+(provide 'test-org-mobile)
+;;; test-org-mobile.el ends here

Reply via email to