2

org 9.2 used to support this feature. when (org-capture 0), it used to place the selected template entry as a sibling of heading at point. in org 9.3 however, it fully respects the heading level defined in the template.

How can I automatically shift the level of the template heading to match the heading at point?

6
  • This sounds to me like a bug: I bisected and found out that it was introduced by this commit about a year ago. I'll report a bug on the mailing list and add a pointer here to track its progress. – NickD Aug 3 '20 at 17:32
  • 2
  • Thanks for looking into it and reporting. didn't figure it would be a bug but a feature. – Joann Morris Aug 7 '20 at 3:08
  • That's what I think, but the maintainers may disagree :) But it's out there, so we'll see. – NickD Aug 7 '20 at 3:12
  • This has been fixed (supposedly: I have not tested the fix) with this commit. – NickD Sep 14 '20 at 15:35
0

modified from File org-capture entry under current headline at point

  ;; Create ‘C-u 2 M-x org-capture’ command to refile org-capture template under
  ;; headline at point.
  (defvar my-org-capture-rfloc nil
    "Holds the RFLOC argument to pass to ‘org-refile’.")
  (defun my-org-capture-under-headline (&optional goto keys under-heading)
    "Capture a headline using ‘org-capture’, according to the template you \
  select, and then immediately refile that headline under the headline at the \
  current point.  GOTO and KEYS are passed to ‘org-capture’."
    (interactive "P")
    (unwind-protect
        (progn
          (unless (eq major-mode 'org-mode)
            (user-error "Must be called from an Org-mode buffer"))
          ;; Capture refile target at point. For format see
          ;; ‘org-refile-target-table’.
          (setq my-org-capture-rfloc
                (list
                 (org-display-outline-path t t nil t)
                 (buffer-file-name (buffer-base-buffer))
                 nil
                 (org-with-wide-buffer
                  (org-back-to-heading t)
                  (point-marker))))
          (funcall #'org-capture goto keys)
          (when my-org-capture-rfloc
            (save-excursion 
              (org-capture-goto-last-stored)
              ;; Refile last-captured target under the headline stored earlier.
              (org-refile nil nil my-org-capture-rfloc)
              ;; Ensure point is at the newly-captured and refiled headline.
              (org-refile-goto-last-stored)
              (unless under-heading
                (outline-promote)))
            ))
      ;; Ensure ‘my-org-capture-rfloc’ is reset.
      (setq my-org-capture-rfloc nil)))
  (defun my-org-capture-under-headline-prefix (_orig-fn &rest _args)
    "Provide an additional “C-u 2” prefix arg to `org-capture'.

  When ‘org-capture’ is called with this prefix argument, it will capture a
  headline according to the template you select, and then immediately refile that
  headline under the headline at the current point."
    (let ((goto (nth 0 _args)))
      (if (equal goto 2)
          (apply #'my-org-capture-under-headline _args)
        (apply _orig-fn _args))))
  (advice-add 'org-capture :around #'my-org-capture-under-headline-prefix)

under-heading controls whether the new heading is a sibling or a child of the current heading.

 (global-set-key (kbd "C-c k") (lambda () (interactive) (my-org-capture-under-headline 0 "b" nil))) ;;as a sibling of current heading
  (global-set-key (kbd "C-c j") (lambda () (interactive) (my-org-capture-under-headline 0 "b" t))) ;; as a child of current heading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.