Interactively enter headline under which to place an entry using capture

前端 未结 4 1126
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 00:29

Using capture templates like the one below, I can add entries to different headlines in a file. How can I manually enter a headline during capture, instead of setting up eac

4条回答
  •  逝去的感伤
    2021-01-05 00:57

    I wrote a function to be used with file+function which will prompt for a location on capture.

    It uses the internal prompting function of org-refile so we get completions of headings in the prompt (with maxlevels overridden to 9). When the user enters an unknown heading, it creates it at the end of the file.

    (defun org-ask-location ()
      (let* ((org-refile-targets '((nil :maxlevel . 9)))
             (hd (condition-case nil
                     (car (org-refile-get-location nil nil t t))
                   (error (car org-refile-history)))))
        (goto-char (point-min))
        (outline-next-heading)
        (if (re-search-forward
             (format org-complex-heading-regexp-format (regexp-quote hd))
             nil t)
            (goto-char (point-at-bol))
          (goto-char (point-max))
          (or (bolp) (insert "\n"))
          (insert "* " hd "\n")))
        (end-of-line))
    

    In your case, you use it like this:

    (setq org-capture-templates
     '(("l" "Log" entry
        (file+function "c:/Org/log.org" org-ask-location)
        "\n\n** %?\n<%<%Y-%m-%d %a %T>>"
        :empty-lines 1))
    

提交回复
热议问题