我在组织文件中有以下形式的任务列表
* Tasks
** task 1
** task 2
并希望在我可以轻松计时的* Tasks
下创建子任务(例如。 task 1
、task 2
等在调用C-u org-clock-in
时弹出。我正在考虑使用 org-clock-history-push
来构建任务列表,但没有遇到一种简单的方法来遍历组织模式标头树来创建任务? 我正在查看此链接,但希望有一个替代/组织 API 可以实现这一目标。
我也会对一种在不加载org-mode
的情况下保存destop-sessions
的时钟历史记录的方法感兴趣,因为我尝试向desktop-globals-to-save
添加org-clock-marker
和org-clock-history
的尝试没有奏效。
我最终从链接的帖子中获得了灵感,写了自己的文章。这是将创建和添加未"完成org-clock-history
"的任务的代码,以便可以通过 C-u org-clock-in
轻松访问它们。
(defun my-org-header-list (&optional header-re level buffer items)
"Get the headers of an org buffer (default current buffer). Optionally,
narrows to headers matching HEADER-RE under nesting LEVEL (defaults all
headers). Returns plist list of headers with specified values in ITEMS when defined.
Defaults to header text, location, level, todo status.
See `org-element-all-elements' for possible item types."
(setq level (or level 0)) ;default all headers
(save-restriction
(with-current-buffer (or buffer (current-buffer))
(goto-char (point-min))
(and header-re
(re-search-forward header-re nil nil 1)
(org-narrow-to-element))
(let ((tree (org-element-parse-buffer 'headline)))
(list :buffer (current-buffer)
:headers
(org-element-map tree 'headline
(lambda (el)
(when (< level (org-element-property :level el))
(or (and items
(cl-loop for sym in items
nconc (list sym (org-element-property sym el))))
(list :raw-value (org-element-property :raw-value el) ;text
:begin (org-element-property :begin el) ;start
:end (org-element-property :end el) ;end
:level (org-element-property :level el) ;depth
:todo-keyword (org-element-property :todo-keyword el))
)))))))))
;; construct list of tasks to choose from in clock history buffer
;; #<marker at 19296 in org-clock.el>
(defun my-org-clock-create (headers)
"Create clock tasks for headers."
(let ((buf (plist-get headers :buffer)))
(cl-loop for h in (plist-get headers :headers)
when (not (string= "DONE" (plist-get h :todo-keyword)))
do (org-clock-history-push (plist-get h :begin) buf))))
;; generate a list of TODO tasks nested under Tasks heading from
;; BUFFER-NAME
;;;###autoload
(defun my-org-clock-in (arg &optional buffer-name)
(interactive "P")
(my-org-clock-create
(my-org-header-list
(if arg (read-string "Task group: ") "Tasks") 1 (or buffer-name "gtd.org")
'(:raw-value :begin :todo-keyword))))