emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-attach-attach in an org-capture template?
@ 2021-04-29 14:08 Tim Visher
  2021-04-30 13:12 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Tim Visher @ 2021-04-29 14:08 UTC (permalink / raw)
  To: Emacs Org Mode mailing list

[-- Attachment #1: Type: text/plain, Size: 2573 bytes --]

Hi Everyone,

*TL;DR:* I've recently started using org-capture and I'd like to run
org-attach-attach during the expansion of a few of my templates. Doing so
from a template function seems not to work (meaning a function in a
template string called with %(…)). What's the best way to go about this?
------------------------------

I've recently started to explore org-capture and it's pretty great. I'm
interested in having one of my templates search for a file somewhere and,
if it finds one, add it to the current template as an attachment.

My initial test template looks something like:

(defun attach-file
    ()
  (org-attach-attach (expand-file-name "a file")
                     nil
                     'cp))

(setq org-capture-templates
      '("T" "TEST" entry
        (file+headline "~/x.org" "Inbox")
        "* TODO Foo%(attach-file)
  %U"))

The idea being that attach-file would call org-attach-attach if it could
find a file and always expand to nothing.

Upon testing that I see the following (org 9.4.5-g287af2, emacs 27.2 (build
1, x86_64-apple-darwin18.7.0)):

** TODO Foo%![Error: (error bug: org-id-get expects a file-visiting buffer)]
   :PROPERTIES:
   :ID:       05837793-5A4F-42FC-AB50-5165FA7ECC4B
   :END:

   [2021-04-29 Thu 09:52]

If I then call org-attach manually on the resulting entry I can attach a
file fine to it.

I'm aware that I can make the template an elisp function and do the work
all internally. That's probably what I'll start doing presently. But it's a
little annoying that I then need to reimplement org-capture's built-in
template expansion in some way (like %U for the current inactive timestamp).

I also believe I could do this with one of the org-capture hooks but
examining them I didn't see the obvious right one to add my function to. I
would think it would be the org-capture-prepare-finalize-hook and I may
just give that a try. I further assume then that I'd need to to apply the
advice here
<https://www.reddit.com/r/emacs/comments/9n3vxn/in_org_mode_how_can_i_make_a_postcapture_hook_run/>
and
check for what template I'm in with (plist-get org-capture-plist :key) or
similar.

So my question is whether there's anything I can do to get org-attach-attach to
recognize the file it's in. I'm assuming that when it's expanding it's in a
temporary buffer of some kind which is why it's failing.

All thoughts and advice are most welcome. Thanks in advance! :)

--

In Christ,

Timmy V.

https://blog.twonegatives.com
http://five.sentenc.es

[-- Attachment #2: Type: text/html, Size: 5283 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: org-attach-attach in an org-capture template?
  2021-04-29 14:08 org-attach-attach in an org-capture template? Tim Visher
@ 2021-04-30 13:12 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2021-04-30 13:12 UTC (permalink / raw)
  To: Tim Visher; +Cc: Emacs Org Mode mailing list

Tim Visher <tim.visher@gmail.com> writes:

> I also believe I could do this with one of the org-capture hooks but
> examining them I didn't see the obvious right one to add my function to. I
> would think it would be the org-capture-prepare-finalize-hook and I may
> just give that a try.

Capture hooks should be the right place to do what your want. I
recommend org-capture-before-finalize-hook since it will not run if you
abort the capture for any reason. Otherwise, you may attach the file,
abort capture (via C-c C-k or because of some error), and your attached
file will hang somewhere unreferenced. Your first idea with
%()-expansion have the same problem.

> ... I further assume then that I'd need to to apply the
> advice here
> <https://www.reddit.com/r/emacs/comments/9n3vxn/in_org_mode_how_can_i_make_a_postcapture_hook_run/>
> and
> check for what template I'm in with (plist-get org-capture-plist :key) or
> similar.

I recommend using doct [1] with :before-finalize keyword. It will take
care that your function runs for the right template.

[1] https://github.com/progfolio/doct#hooks

> So my question is whether there's anything I can do to get org-attach-attach to
> recognize the file it's in. I'm assuming that when it's expanding it's in a
> temporary buffer of some kind which is why it's failing.

You are right, template expansion is done in separate "*Capture*" buffer
using org-capture-fill-template. The buffer is not associated with any
file.

Best,
Ihor


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-30 13:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-29 14:08 org-attach-attach in an org-capture template? Tim Visher
2021-04-30 13:12 ` Ihor Radchenko

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).