使用elisp創建repeated schedule time

原文地址:https://www.lujun9972.win/blog/2019/03/24/使用elisp創建repeated-schedule-time/index.html

我們都直到使用 (org-schedule ARG &optional TIME) 函數能夠給任務分配一個規劃的任務開始時間。 下面是關於 org-schedule 的函數說明

(org-schedule ARG &optional TIME)

Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
With one universal prefix argument, remove any scheduling date from the item.
With two universal prefix arguments, prompt for a delay cookie.
With argument TIME, scheduled at the corresponding date.  TIME can
either be an Org date like "2011-07-24" or a delta like "+2d".

但是從它的doc-string中你應該看不出來它其實可以用來給任務分配一個 repeated schedule time.

一個 repeated schedule time 由兩部分組成,一個是 規劃的任務開始時間,一個是 重複的時間間隔. 比如 SCHEDULED: <2005-10-01 Sat +1m> 就表示 該任務規劃的任務開始時間是 2005-10-01,同時它是一個月度重複任務

但是若你試着使用 (org-schedule 4 "2005-10-01 +1m"),你會發現生成的規劃時間並不帶有 +1m 這個重複的部分。

* test
SCHEDULED: <2005-10-01 六>

通過查看 org-schedule 的代碼你會發現它實際調用的 org--deadline-or-schedule 函數來生成規劃日期,而 org--deadline-or-schedule 中有這麼一段代碼

;; Save repeater cookie from either TIME or current scheduled
;; time stamp.  We are going to insert it back at the end of
;; the process.
(repeater (or (and (org-string-nw-p time)
                   ;; We use `org-repeat-re' because we need
                   ;; to tell the difference between a real
                   ;; repeater and a time delta, e.g. "+2d".
                   (string-match org-repeat-re time)
                   (match-string 1 time))
              (and (org-string-nw-p old-date)
                   (string-match "\\([.+-]+[0-9]+[hdwmy]\
\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\)"
                                 old-date)
                   (match-string 1 old-date)))))

也就是說,要想保留 重複時間間隔 部分的信息,要求輸入的 TIME 符合 org-repeate-re 的正則表達式。

org-repeate-re 的默認值爲:

org-repeat-re is a variable defined in ‘org.el’.
Its value is
"[[<][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^]>
]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"

  This variable may be risky if used as a file-local variable.

Documentation:
Regular expression for specifying repeated events.
After a match, group 1 contains the repeat expression.

也就是需要以 <[ 開頭. 因此,要讓 org-schedule 生成repeated schedule time只需要將時間寫入 <>[] 中即可:

比如將上面語句改成 (org-schedule 4 "<2005-10-01 +1m>"), 就能看到結果

* test
  SCHEDULED: <2005-10-01 六 +1m>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章