Search code examples
elisporg-mode

Add a decorator function to org-capture-template


I wrote a org-capture-templates which add entries to appropriate headings as:

** Plan
** Writing

The org-capture-templates as:

(setq org-capture-templates
      '(("p" "Plan" entry
         (file+function "~/Documents/OrgMode/ORG/Master/todo.today.org"
                        (my-org-goto-last-headline "\\*\\* Plan"))
         "* TODO %i%?")
        ("w" "Writing" entry
         (file+function "~/Documents/OrgMode/ORG/Master/todo.today.org"
                        (my-org-goto-last-headline "\\*\\* Writing"))
         "* %i%? \n%T")))

my-org-goto-last-headline was defined as a decorator:

(defun my-org-goto-last-headline (heading)
  (defun nested ()
    "Move point to the last headline in file matching \"** Headline\"."
    (end-of-buffer)
    (re-search-backward heading))
  `nested)

Additionally, I set lexical-binding: t

Unfortunately, it report errors like:

org-capture-set-target-location: Invalid function: (my-org-goto-last-headline "\\*\\* Plan")

How could I apply a decorator in such a org-capture-template?


Solution

  • The function format should be (file+function "filename" function-finding-location).

    You are not providing a function itself. You are giving it a list (function argument). Since it is not being executed you have that error.

    You could write it in backquote and evaluate the function that returns the function object.

    (setq org-capture-templates
          `(("p" "Plan" entry
             (file+function "~/Documents/OrgMode/ORG/Master/todo.today.org"
                            ,(my-org-goto-last-headline "\\*\\* Plan"))
             "* TODO %i%?")
            ("w" "Writing" entry
             (file+function "~/Documents/OrgMode/ORG/Master/todo.today.org"
                            ,(my-org-goto-last-headline "\\*\\* Writing"))
             "* %i%? \n%T")))
    

    You will also need to quote the return nested in my-org- function.