2

使用elisp创建repeated schedule time

 3 years ago
source link: https://www.lujun9972.win/blog/2019/03/24/%E4%BD%BF%E7%94%A8elisp%E5%88%9B%E5%BB%BArepeated-schedule-time/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用elisp创建repeated schedule time

我们都直到使用 (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>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK