You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

从Org子树创建可org-clock计时任务的技术实现咨询

Solution: Quick Clock-In Menu for Org-Mode Tasks

I’ve got a straightforward solution using Org-mode’s built-in API to create that quick selection menu when you run C-u org-clock-in. Here’s how to set it up:

Step 1: Define the Custom Task Selection Function

This function will locate your * Tasks heading, collect all its direct subheadings, and let you pick one to clock into:

(defun my-org-clock-in-tasks ()
  "Clock in to a direct subheading of the * Tasks node.
Adds the selected task to org-clock-history for future access."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    ;; Locate the * Tasks heading in the current file
    (if (search-forward-regexp "^\\*+\\s-+Tasks\\s-*$" nil t)
        (progn
          (org-back-to-heading)
          ;; Collect all direct subheadings (level = parent level +1)
          (let ((tasks (org-map-entries 
                        (lambda () (cons (org-get-heading t) (point)))
                        nil 
                        'tree 
                        (1+ (org-current-level)))))
            (if tasks
                (let* ((selected (completing-read "Select task to clock in: " tasks nil t))
                       (task-pos (cdr (assoc selected tasks))))
                  (goto-char task-pos)
                  (org-clock-in)
                  ;; Add the task to clock history for quick access later
                  (org-clock-history-push (point)))
              (message "No direct subheadings found under * Tasks.")))
      (message "* Tasks node not found in this file."))))

Step 2: Hook It to C-u org-clock-in

Add this advice to make the universal argument trigger your custom selection menu, while keeping the regular org-clock-in behavior intact:

(defadvice org-clock-in (around my-org-clock-in-tasks activate)
  "When called with universal argument, offer * Tasks subheadings for clock-in."
  (if current-prefix-arg
      (my-org-clock-in-tasks)
    ad-do-it))

How to Use It

  1. Add both code blocks to your Emacs init file (~/.emacs or ~/.emacs.d/init.el).
  2. Restart Emacs or evaluate the code with M-x eval-region (select the code and run this command).
  3. Now, when you hit C-u org-clock-in, you’ll see a prompt with all direct subheadings under your * Tasks node. Select one, and it’ll start the clock automatically.

Possible Extensions

Want to tweak this further? Here are some ideas:

  • Include nested tasks: Remove the level filter in org-map-entries (delete the fourth argument (1+ (org-current-level))) to collect all tasks under * Tasks, no matter how deep they are.
  • Support multiple files: Modify the search to scan all your agenda files instead of just the current one. Use org-agenda-files and loop through each file with find-file.
  • Add task metadata: Adjust the org-get-heading call to include tags or IDs (e.g., (org-get-heading t t t t) to get the full heading with tags) for clearer selection.
  • Auto-focus on Tasks: If you always want to clock into tasks, bind the custom function to a dedicated key like C-c t with (global-set-key (kbd "C-c t") 'my-org-clock-in-tasks).

内容的提问来源于stack exchange,提问作者Rorschach

火山引擎 最新活动