diff --git a/lisp/org.el b/lisp/org.el index ce68f4692..69e333c84 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -11305,8 +11305,9 @@ or a character." (user-error "Priority commands are disabled")) (setq action (or action 'set)) (let ((nump (< org-priority-lowest 65)) + (saved-position (point)) current new news have remove) - (save-excursion + (org-back-to-heading t) (when (looking-at org-priority-regexp) (let ((ms (match-string 2))) @@ -11337,7 +11338,7 @@ or a character." (= (upcase org-priority-lowest) org-priority-lowest)) (setq new (upcase new))) (cond ((equal new ?\s) (setq remove t)) - ((or (< (upcase new) org-priority-highest) (> (upcase new) org-priority-lowest)) + ((or (> (upcase new) org-priority-highest) (< (upcase new) org-priority-lowest)) (user-error (if nump "Priority must be between `%s' and `%s'" @@ -11364,8 +11365,8 @@ or a character." org-priority-default (1+ org-priority-default)))))) (t (user-error "Invalid action"))) - (when (or (< (upcase new) org-priority-highest) - (> (upcase new) org-priority-lowest)) + (when (or (> (upcase new) org-priority-highest) + (< (upcase new) org-priority-lowest)) (if (and (memq action '(up down)) (not have) (not (eq last-command this-command))) ;; `new' is from default priority @@ -11377,12 +11378,26 @@ or a character." ;; Numerical priorities are limited to 64, beyond that number, ;; assume the priority cookie is a character. (setq news (if (> new 64) (format "%c" new) (format "%s" new))) + ;; Actually setting the priority isn’t so straightforward + ;; There are several cases (if have + ;; ① have and remove (if remove (replace-match "" t t nil 1) - (replace-match news t t nil 2)) + ;; ② have and not remove, i.e., update + (let ((case-fold-search nil)) (looking-at org-priority-regexp)) + (if (match-end 2) + (progn + (goto-char (match-end 2)) + (delete-region (match-beginning 2) (match-end 2)) + (insert news)) + (goto-char (match-end 1)) + (insert "[#" news "] ")) + ) + ;; ③ don’t have and remove — nonsense (if remove (user-error "No priority cookie found in line") + ;; ④ don’t have and not remove, i.e., insert (let ((case-fold-search nil)) (looking-at org-todo-line-regexp)) (if (match-end 2) (progn @@ -11390,7 +11405,8 @@ or a character." (insert " [#" news "]")) (goto-char (match-beginning 3)) (insert "[#" news "] ")))) - (org-align-tags)) + (org-align-tags) + (goto-char saved-position) (if remove (message "Priority removed") (message "Priority of current item set to %s" news)))))