Function: todo-set-item-priority
todo-set-item-priority is an interactive and byte-compiled function
defined in todo-mode.el.gz.
Signature
(todo-set-item-priority &optional ITEM CAT NEW ARG)
Documentation
Prompt for and set ITEM's priority in CATegory.
Interactively, ITEM is the todo item at point, CAT is the current category, and the priority is a number between 1 and the number of items in the category. Non-interactively, non-nil NEW means ITEM is a new item and the lowest priority is one more than the number of items in CAT.
The new priority is set either interactively by prompt or by a
numerical prefix argument, or noninteractively by argument ARG,
whose value can be either of the symbols raise or lower,
meaning to raise or lower the item's priority by one.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/calendar/todo-mode.el.gz
(defun todo-set-item-priority (&optional item cat new arg)
"Prompt for and set ITEM's priority in CATegory.
Interactively, ITEM is the todo item at point, CAT is the current
category, and the priority is a number between 1 and the number
of items in the category. Non-interactively, non-nil NEW means
ITEM is a new item and the lowest priority is one more than the
number of items in CAT.
The new priority is set either interactively by prompt or by a
numerical prefix argument, or noninteractively by argument ARG,
whose value can be either of the symbols `raise' or `lower',
meaning to raise or lower the item's priority by one."
(interactive)
(unless (and (or (called-interactively-p 'any) (memq arg '(raise lower)))
;; Noop if point is not on a todo (i.e. not done) item.
(or (todo-done-item-p) (looking-at "^$")
;; On done items separator.
(save-excursion (beginning-of-line)
(looking-at todo-category-done))))
(let* ((item (or item (todo-item-string)))
(marked (todo-marked-item-p))
(cat (or cat (cond ((eq major-mode 'todo-mode)
(todo-current-category))
((eq major-mode 'todo-filtered-items-mode)
(let* ((regexp1
(concat todo-date-string-start
todo-date-pattern
"\\( " diary-time-regexp "\\)?"
(regexp-quote todo-nondiary-end)
"?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
(save-excursion
(re-search-forward regexp1 nil t)
(match-string-no-properties 1)))))))
(count 1)
(curnum (save-excursion
(let ((curstart
;; If point is in done items section or not on an
;; item, use position of first todo item to avoid
;; the while-loop.
(or (and (not (todo-done-item-section-p))
(todo-item-start))
(point-min))))
(goto-char (point-min))
(while (/= (point) curstart)
(setq count (1+ count))
(todo-forward-item))
count)))
(todo (cond ((or (memq arg '(raise lower))
(eq major-mode 'todo-filtered-items-mode))
(save-excursion
(let ((count curnum))
(while (looking-at todo-item-start)
(setq count (1+ count))
(todo-forward-item))
count)))
((eq major-mode 'todo-mode)
(todo-get-count 'todo cat))))
(maxnum (if new (1+ todo) todo))
(prompt (format "Set item priority (1-%d): " maxnum))
(priority (cond ((and (not arg) (numberp current-prefix-arg))
current-prefix-arg)
((and (eq arg 'raise) (>= curnum 1))
(1- curnum))
((and (eq arg 'lower) (<= curnum maxnum))
(1+ curnum)))))
(and (called-interactively-p 'any)
priority ; Check further only if arg or prefix arg was passed.
(or (< priority 1) (> priority maxnum))
(user-error (format "Priority must be an integer between 1 and %d"
maxnum)))
(unless (and priority
(/= priority curnum)
(or (and (eq arg 'raise) (zerop priority))
(and (eq arg 'lower) (>= priority maxnum))))
;; When moving item to another category, show the category before
;; prompting for its priority.
(unless (or arg (called-interactively-p 'any))
(todo-category-number cat)
;; If done items in category are visible, keep them visible.
(let ((done todo-show-with-done))
(when (> (buffer-size) (- (point-max) (point-min)))
(save-excursion
(goto-char (point-min))
(setq done (re-search-forward todo-done-string-start nil t))))
(let ((todo-show-with-done done))
;; Keep current item or top of moved to category in view
;; while setting priority.
(save-excursion (todo-category-select)))))
;; Prompt for priority only when the category has at least one
;; todo item or when passing the current priority as prefix arg.
(when (and (or (not priority) (= priority curnum))
(> maxnum 1))
(let* ((read-number-history (mapcar #'number-to-string
(if (eq todo-default-priority
'first)
(number-sequence maxnum 1 -1)
(number-sequence 1 maxnum))))
(history-add-new-input nil)
(candidate (or priority
(read-number prompt
(if (eq todo-default-priority
'first)
1 maxnum))))
(success nil))
(while (not success)
(setq prompt
(cond
((and (= candidate curnum)
;; Allow same priority in a different category
;; (only possible when called non-interactively).
(called-interactively-p 'any))
"New priority must be different from current priority: ")
(t (when (or (< candidate 1) (> candidate maxnum))
(format "Priority must be an integer between 1 and %d: "
maxnum)))))
(when prompt (setq candidate (read-number prompt)))
(unless prompt (setq priority candidate success t)))))
;; In Top Priorities buffer, an item's priority can be changed
;; wrt items in another category, but not wrt items in the same
;; category.
(when (eq major-mode 'todo-filtered-items-mode)
(let* ((regexp2 (concat todo-date-string-start todo-date-pattern
"\\( " diary-time-regexp "\\)?"
(regexp-quote todo-nondiary-end)
"?\\(?1:" (regexp-quote cat) "\\)"))
(end (cond ((< curnum priority)
(save-excursion (todo-item-end)))
((> curnum priority)
(save-excursion (todo-item-start)))))
(match (save-excursion
(cond ((< curnum priority)
(todo-forward-item (1+ (- priority curnum)))
(when (re-search-backward regexp2 end t)
(match-string-no-properties 1)))
((> curnum priority)
(todo-backward-item (- curnum priority))
(when (re-search-forward regexp2 end t)
(match-string-no-properties 1)))))))
(when match
(user-error (concat "Cannot reprioritize items from the same "
"category in this mode, only in Todo mode")))))
(let ((inhibit-read-only t))
;; Interactively or with non-nil ARG, relocate the item within its
;; category.
(when (or arg (called-interactively-p 'any))
(todo-remove-item))
(goto-char (point-min))
(when priority
(unless (= priority 1)
(todo-forward-item (1- priority))
;; When called from todo-item-undone and the highest priority is
;; chosen, this advances point to the first done item, so move
;; it up to the empty line above the done items separator.
(when (looking-back (concat "^"
(regexp-quote todo-category-done)
"\n")
(line-beginning-position 0))
(todo-backward-item))))
(todo-insert-with-overlays item)
;; If item was marked, restore the mark.
(and marked
(let* ((ov (todo-get-overlay 'prefix))
(pref (overlay-get ov 'before-string)))
(overlay-put ov 'before-string
(concat todo-item-mark pref)))))))))