请测试特定待办状态的存在以及与今天不等的截止日期的最佳方法是什么?
org-state
是一个“[h] ook,它在TODO项目状态发生变化后运行”。因此,除非我实际上改变待办事项的状态,否则我认为我不能使用(string-equal org-state "Next Action")
。我列出的每个string-equal
代码行均被拒绝:Symbol's value as variable is void:
org-todo
和org-deadline
。截止日期是todo状态下面出现的一条线,因此在测试两种条件是否存在时也可能会出现问题。
(defun if-next-action-not-today-then-promote ()
(interactive)
(goto-char (point-min))
(while
(re-search-forward "^\*\* Next Action" nil t)
(when (and (string-equal org-todo "Next Action") (not (string-equal org-deadline "<%<%Y-%m-%d %a>>")) )
(message "You have satisfied the two conditions . . . proceeding.")
(org-todo "Active") ;; change state to active
(org-deadline nil "<%<%Y-%m-%d %a>>") ;; change deadline to today
)
)
)
示例* .org配置文件。
* TASKS
** Active [#A] First task due today. :lawlist:
DEADLINE: <2013-07-11 Thu >
** Active [#A] Second task due today. :lawlist:
DEADLINE: <2013-07-11 Thu >
** Next Action [#E] Test One -- make Active with deadline today. :lawlist:
DEADLINE: <2013-07-31 Wed >
** Next Action [#E] Test Two -- make Active with deadline today. :lawlist:
DEADLINE: <2013-07-31 Wed >
编辑:以下是Jonathan Leech-Pepin在下面的答案中提出的功能的修改。在答案的第一稿中,由于nil
的问题,我无法从变量deadline
获得(org-element-timestamp-interpreter . . . nil)
以外的任何内容 - 完全消除该引用似乎可以解决问题。我一路上设置了消息,这样我就能更好地理解发生了什么。我使用if
代替unless
,因为我想要一条else
消息让我知道条件没有达到,但功能仍然正常工作。我已经运行了几个测试,包括将它包装到re-search-forward
类型的函数中并且它非常好用 - 包装函数可以通过多种方式完成,但这超出了这个线程的范围 - 例如,{{ 1}}和(&optional from-state to-state)
再然后(interactive)
向下;或(setq . . .)
和(from-state to-state)
。
(interactive (list (setq . . .)
答案 0 :(得分:1)
只要您使用Org 7.9.2
或更高版本,以下内容就可以使用。我通过调整截止日期和待办事项状态来测试它。
(defun zin/org-test-deadline (from-state to-state)
"Change headline from FROM-STATE to TO-STATE if the deadline is
not already set to today."
(interactive "sChange from state: \nsChange to state: ")
(unless (org-at-heading-p)
(org-back-to-heading))
(let* ((element (org-element-at-point))
(todo-state (org-element-property :todo-keyword
element))
;; Do not give an error if there is no deadline
(deadline (ignore-errors
(time-to-days
(org-time-string-to-time
(org-element-timestamp-interpreter
(org-element-property :deadline
element) nil)))))
(today (time-to-days (current-time))))
(unless (or
;; Is there a deadline
(not deadline)
;; Is the deadline today
(eq today deadline)
;; Is the todo state the wrong one
(not (string= todo-state from-state)))
(org-todo to-state)
(org-deadline nil "."))))
然后,您可以将其绑定或将其包装在定义from-state
和to-state
的函数中。如果以交互方式调用,它也会提示两个州。