如何测试组织状态“xyz”截止日期不等于今天 [英] How to test for org-todo state "xyz" with deadline not equal to today

查看:122
本文介绍了如何测试组织状态“xyz”截止日期不等于今天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么最好的方法来测试一个特定的待办事项的状态和截止日期不等于今天?

What is the best way, please, to test for the existence of a specific todo state and a deadline that is not equal to today?

org-state 是在TODO项目状态更改后运行的[h] ook。所以除非我实际上改变了一个todo的状态,我不认为我可以使用(string-equal org-stateNext Action)。以下列出的每个 string-equal 代码行被拒绝:变量的符号值为void: org-todo org-deadline 。截止日期是出现在托多州之下的一条线,因此在测试两种条件的存在时也可能会出现问题。

org-state is a "[h]ook which is run after the state of a TODO item was changed". So unless I'm actually changing the state of a todo, I don't think I can use (string-equal org-state "Next Action"). Each of my string-equal lines of code listed below are rejected: Symbol's value as variable is void: org-todo and org-deadline. The deadline is a line that appears underneath the todo state, so that may also pose a problem when testing for the existence of both conditions.

(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配置文件。


Sample *.org configuration file.

* 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 使用(org-element-timestamp-interpreter ...。nil) - 完全删除该引用似乎可以纠正问题。我在路上设置了一些信息,以便更好地了解发生了什么。我使用,如果而不是,除非,因为我想要一个 else 消息让我知道条件不符合,但功能仍然正常工作。我已经运行了几个测试,包括将它包装成一个重新搜索转发类型函数,它的工作非常好 - 一个包装函数可以通过几种方法,但是超出了这个线程的范围 - 例如(&可选从状态到状态)(交互式)然后(setq ...)进一步下降;或(从状态到状态)(interactive(list(setq。))


The following is a modification of the function proposed by Jonathan Leech-Pepin in his answer further down below. In the first draft of the answer, I was unable to obtain anything but nil from the variable deadline due to a problem with (org-element-timestamp-interpreter . . . nil) -- completely eliminating that reference appears to correct the issue. I set up messages along the way so that I could better understand what was happening. I used if instead of unless because I wanted an else message to let me know that the conditions were not met but that the function was nevertheless working correctly. I've run several tests, including wrapping it into an re-search-forward type function and it works very nicely -- a wrapped function can be done several ways, but that is beyond the scope of this thread -- e.g., (&optional from-state to-state) and (interactive) and then (setq . . .) further down; or (from-state to-state) and (interactive (list (setq . . .).

(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))

      ;; "ignore-errors" avoids throwing an error message if there is no deadline.
      (deadline
        (ignore-errors
        (time-to-days
        (org-time-string-to-time
        (org-element-property :deadline element) ))))

      (today (time-to-days (current-time))) )

    (message "This is the element variable:  %s" element)

    (message "This is the today variable:  %s" today)

    (message "This is the deadline variable:  %s" deadline)

    (message "This is the todo-state variable:  %s" todo-state)

    (message "This is the from-state variable:  %s" from-state)

    (message "This is the to-state variable:  %s" to-state)

    (if (not (eq today deadline))
      (message "The deadline is not today.")
      (message "Today is the deadline."))

    (if (and
      (not (eq today deadline)) ;; condition -- deadline not equal to today
      (string= todo-state from-state) ) ;; condition -- todo-state equals from-state
        (progn ;; Process following list if conditions were met.
          (org-todo to-state)
          (org-deadline nil ".")
          (message "The conditions were met, so we did everything that was required.") )
      (message "The conditions were not met, so nothing has been done."))
  ))


推荐答案

只要你使用组织7.9.2 以上应该可以工作。我也通过调整截止日期和状态来测试它。

As long as you are using Org 7.9.2 or greater the following should work. I tested it as well as I could by adjusting deadlines and todo states.

(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 。如果以交互方式进行调用,它也将提示两个状态。

You can then bind this or wrap it in a function that defines from-state and to-state. It will also prompt for the two states if called interactively.

这篇关于如何测试组织状态“xyz”截止日期不等于今天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆