2

My goal is to get org-capture to store captured notes in a local "notes.org" file located in the same directory as the current working buffer.

I set the template like so:

(setq org-capture-templates
       '(("x"
     "local notes" entry (file+headline (concat ,(file-name-directory buffer-file-name) "notes.org") "Copied regions")
     "* %^{Title} %U \n %i")
    )
       )

Alas, when invoking the template i get Invalid file location: nil.

How should this be done?

Erik
  • 123
  • 9
  • I successfully used this syntax for several years under Emacs 24 and 25, but it fails under Emacs 26. Evidently the restriction to a function noted below was introduced with this version of Emacs. – Liam Dec 27 '19 at 16:35
  • Does this answer your question? [How to evaluate the variables before adding them to a list?](https://emacs.stackexchange.com/questions/7481/how-to-evaluate-the-variables-before-adding-them-to-a-list) – Drew Jan 08 '20 at 05:10
  • If you want to use `defun` then [advice-add](https://emacs.stackexchange.com/questions/41950/send-org-capture-tasks-to-different-files-depending-on-which-file-org-capture-i) will give you the answer – yuskam May 06 '21 at 14:14

2 Answers2

6

As the docs say, you can supply a function (with no arguments) to set the file here. Thus:

(setq org-capture-templates
   '(("x"  "local notes" entry 
     (file+headline (lambda () (concat (file-name-directory buffer-file-name) "notes.org")) "Copied regions")
       "* %^{Title} %U \n %i")
))

does the job.

Fran Burstall
  • 3,665
  • 10
  • 18
  • Many thanks! How come `(concat (file-name-directory buffer-file-name) "notes.org")` is not accapted as a function? – Erik Jun 21 '18 at 07:46
  • Because it isn't a function! Functions are either pre-defined using `defun` or on the fly with `lambda`. – Fran Burstall Jun 21 '18 at 18:09
0

Change the leading apostrophe to a backquote on the template list. It will work then, no lambda needed!

Joel Reymont
  • 101
  • 1
  • In that case the question becomes a duplicate of https://emacs.stackexchange.com/q/7481/105. – Drew Jan 08 '20 at 05:13