如何使用自定义TODO关键字顺序应用org-sort-entries?

问题描述

更具体地说,我想使用与在待办状态之间切换的顺序不同的关键字顺序。

我知道我可以使用org-sort-entries nil ?O(或?o)对todo关键字进行排序,这几乎可以得到我想要的东西。但是,我想使用其他关键字排序。我在org-todo-keywords中定义的关键字顺序是我想在关键字状态之间进行切换的顺序;但这不是我要用来排序任务的顺序。如果我通过更改org-todo-keywords解决此问题,则状态切换(Shift +箭头键)将无法按照我想要的方式工作。是否可以使用其他关键字顺序进行排序与​​状态切换?

我怀疑有一种使用org-sort-entries'GETKEY-FUNCCOMPARE-FUNC参数的方法,尽管我不十分了解elisp才能把它写在脑海里。有没有更简单的方法-仅凭?o就能做到吗?

作为奖励,如果列表中的标题根本没有NO todo关键字,我希望能够指定它们按排序顺序所处的位置。例如,我可能希望我的订单为(无关键字),“ STARTED”,“ Todo”,“ DONE”,以便最终得到这样的排序行:

* Project A
** Reference for this project: projectA-url.com
** STARTED Write the docs
** STARTED Build the second bit
** Todo Build the third bit
** DONE Set up the infrastructure
** DONE Build the first bit

解决方法

最后,我找不到使用?o的任何方法。如果有人可以提出解决办法,那将是对我的问题的更好答案。

我确实使用?fGETKEY-FUNC实现了这一点。看起来像:

(defun todosort-key-func ()
  (cl-position
   (nth 2 (org-heading-components))
   '(nil "STARTED" "TODO" "DONE")
   :test 'equal
   )
  )

(org-map-entries
  '(org-sort-entries nil ?f 'todosort-key-func)
  "LEVEL=1" 'file)
  )

这里(nth 2 (org-heading-components)正在检索该项目的TODO关键字(可能有更好的方法吗?),而cl-position在硬编码列表中找到其索引,因此我们按指定顺序按关键字排序。

编辑:我还发现我有一个错误,如果我的一个项目还没有任务,则整个org-map-entries调用会部分失败,因为org-sort-entries会抛出“ Nothing to排序”错误。我希望如果我们尝试对一个空列表进行排序,我们只是返回一个空列表,而不是停止!我对此很陌生,因此可能有更好的方法,但是我通过将整个内容包装在condition-case中来捕获错误来解决此问题,例如:

(condition-case nil
  (org-map-entries
    '(org-sort-entries nil ?f 'todosort-key-func)
    "LEVEL=1" 'file)
    )
  (user-error (message "user-error,probably empty sort list,continuing"))
  )