Org mode捕获

5

我正在尝试为org-capture制作一个“class”模板。

我的目标是轻松地创建这种类型的条目:

* TODO <course>: Week <week> Lecture <number>
  SCHEDULED: %^T
** TODO prepare for class: <course>-<week>-<number>
   SCHEDULED: <two days before T> DEADLINE: <one day before T>
** TODO review class: <course>-<week>-<number>
   SCHEDULED: <one day after T> DEADLINE: <two days after T>

目前,我有这个模板。

(setq org-capture-templates
   '(
    ("c" "Class" entry (file "~/sydbox/personal/workflow/class.txt")
         "* TODO %^{Course}: Week %^{Week} Lecture %^{Number}\n  SCHEDULED: %(org-insert-time-stamp (org-read-date nil t nil nil nil \" \"))\n location: %^{location} %?\n** TODO %\\1: prepare lecture %\\3 from week %\\2\n   DEADLINE: %(org-insert-time-stamp (org-read-date nil t \"-1d\")) SCHEDULED: %(org-insert-time-stamp (org-read-date nil t \"-2d\"))\n** TODO %\\1: review lecture %\\3 from week %\\2\n   DEADLINE: %(org-insert-time-stamp (org-read-date nil t \"+2d\")) SCHEDULED: %(org-insert-time-stamp (org-read-date nil t \"+1d\"))\n")
    ("e" "Exercise session" entry (file "~/sydbox/personal/workflow/class.txt")
     ))

然而,现在我不知道如何输入日期。应该提示课程的日期和时间(仅一次)。


这是一个相关的线程,可能会让某人在当前线程中对这个有趣的问题有所启发:https://dev59.com/lGsz5IYBdhLWcg3wTl-r 这个问题的最终答案可能会使用变量org-last-changed-timestamp,它存储了上次使用的时间戳,因此用户不必再次选择日期。 - lawlist
请注意:本当前问题/线程也已经在Superuser上发布了:http://superuser.com/questions/788329/org-mode-captureOrg模式捕获 - lawlist
这是我几个月前编写/修改的一些代码链接,可以实现自定义创建org-mode条目:https://dev59.com/1WEh5IYBdhLWcg3wbjAW#22419713。我意识到原帖作者可能想通过使用`org-capture-templates`来保持简单;然而,我想指出还有其他方法可以实现相同的目标。 - lawlist
1个回答

5
以下代码适用于我。它首先定义了一个自定义函数来创建模板 (org-capture-class),根据需要计算日期,并且比内联字符串更易于阅读 (请注意,它依赖于 cl-lib,所以请确保已经加载)。然后在模板中调用该函数,如果任何字段为空,则将其设置为 nil (已根据评论进行更新)。
(defun org-capture-class ()
  "Capture a class template for org-capture."
  (cl-labels ((update (date days)
                      (format-time-string
                       (car org-time-stamp-formats)
                       (seconds-to-time (+ (time-to-seconds date)
                                           (* days 86400))))))
    (let ((course   (read-string "Course: " nil nil '(nil)))
          (week     (read-string "Week: " nil nil '(nil)))
          (lecture  (read-string "Lecture No.: " nil nil '(nil)))
          (date     (org-read-date nil t))
          (location (read-string "Location: " nil nil '(nil))))
      (when (and course week lecture date location)
        (concat (format "* TODO %s: Week %s Lecture %s\n"
                        course week lecture)
                (format "  SCHEDULED: %s\n" (update date 0))
                (format "  Location: %s %%?\n" location)
                (format "** TODO %s: prepare lecture %s from week %s\n"
                        course lecture week)
                (format "   DEADLINE: %s SCHEDULED: %s\n"
                        (update date -1) (update date -2))
                (format "** TODO %s: review lecture %s from week %s\n"
                        course lecture week)
                (format "   DEADLINE: %s SCHEDULED: %s\n"
                        (update date 2) (update date 1)))))))

(setq org-capture-templates
   '(("c" "Class" entry
      (file "~/sydbox/personal/workflow/class.txt")
      #'org-capture-class)
     ("e" "Exercise session" entry
      (file "~/sydbox/personal/workflow/class.txt"))))

哇!谢谢。然而,这似乎不起作用。尝试构建日程视图。所有的时间表和截止日期都在同一天。 - Syd Kerckhove
啊,我误解了你的代码想要做什么。更新后的版本应该按照你在评论中解释的那样工作:时间表和截止日期现在围绕课程日期适当地提前或延迟1或2天。 - Dan
不客气!我本来想更多地了解org-capture,这个谜题提供了一个很好的借口。另外,除非你在等待是否有其他答案(如果是这样,我不会生气),否则请考虑手动授予悬赏以将问题从“特色”选项卡中移除。 - Dan
我9小时前尝试过,但是我还没有成功。如果我能帮你写一些Haskell代码,请告诉我! - Syd Kerckhove

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接