emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Define capture template with dynamic id target
@ 2011-06-09 18:18 Darlan Cavalcante Moreira
  2011-06-10  5:31 ` David Maus
  0 siblings, 1 reply; 4+ messages in thread
From: Darlan Cavalcante Moreira @ 2011-06-09 18:18 UTC (permalink / raw)
  To: Orgmode Mailing List


Hello List,

I'm trying to create a few templates for org capture and I have found a
weird behavior with the ID target type. It works OK if I use the ID
directly like below
#+begin_src emacs-lisp
  (id "Junho2011Contas")
#+end_src
but it does not find the ID if I try to get it from a function, such as
#+begin_src emacs-lisp
  (id (get-me-an-org-id-for-the-month "Contas"))
#+end_src

All the get-me-an-org-id-for-the-month function does is returning
"MonthnameYearArgument", in this case it returns "Junho2011Contas". The
definition is given below
#+begin_src emacs-lisp
  (defun get-me-an-org-id-for-the-month (categoryName)
    "Used only in my template for the expenses of the month. It
  return a suitable ID for the month sub-headline."
    (interactive)
    (concat (get-current-month) (get-current-year) categoryName)
    )
#+end_src


--
Darlan

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

* Re: Define capture template with dynamic id target
  2011-06-09 18:18 Define capture template with dynamic id target Darlan Cavalcante Moreira
@ 2011-06-10  5:31 ` David Maus
  2011-06-10 15:50   ` Darlan Cavalcante Moreira
  0 siblings, 1 reply; 4+ messages in thread
From: David Maus @ 2011-06-10  5:31 UTC (permalink / raw)
  To: Darlan Cavalcante Moreira; +Cc: Orgmode Mailing List

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

At Thu, 09 Jun 2011 15:18:00 -0300,
Darlan Cavalcante Moreira wrote:
>
>
> Hello List,
>
> I'm trying to create a few templates for org capture and I have found a
> weird behavior with the ID target type. It works OK if I use the ID
> directly like below
> #+begin_src emacs-lisp
>   (id "Junho2011Contas")
> #+end_src
> but it does not find the ID if I try to get it from a function, such as
> #+begin_src emacs-lisp
>   (id (get-me-an-org-id-for-the-month "Contas"))
> #+end_src

Org capture does not support executing a function in the ID target spec.

C-h v org-capture-templates RET

target       Specification of where the captured item should be placed.
             In Org-mode files, targets usually define a node.  Entries will
             become children of this node, other types will be added to the
             table or list in the body of this node.

             Most target specifications contain a file name.  If that file
             name is the empty string, it defaults to `org-default-notes-file'.
             A file can also be given as a variable, function, or Emacs Lisp
             form.

             Valid values are:

             (file "path/to/file")
                 Text will be placed at the beginning or end of that file

             (id "id of existing org entry")
                 File as child of this entry, or in the body of the entry

             ...

You might use the `function' target spec

             (function function-finding-location)
                Most general way, write your own function to find both
                file and location

HTH,
  -- David
--
OpenPGP... 0x99ADB83B5A4478E6
Jabber.... dmjena@jabber.org
Email..... dmaus@ictsoc.de

[-- Attachment #2: Type: application/pgp-signature, Size: 230 bytes --]

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

* Re: Define capture template with dynamic id target
  2011-06-10  5:31 ` David Maus
@ 2011-06-10 15:50   ` Darlan Cavalcante Moreira
  2011-06-11  8:06     ` Aankhen
  0 siblings, 1 reply; 4+ messages in thread
From: Darlan Cavalcante Moreira @ 2011-06-10 15:50 UTC (permalink / raw)
  To: David Maus; +Cc: Orgmode Mailing List


Thanks David,

I tried to follow your suggestion, but I found two problems (maybe because
I know little about lisp).

For instance, suppose I have a test.org file with the follow content
--8<---------------cut here---------------start------------->8---
* 2011
  Every headline has an ID, but I have omitted here for brevity
*** May
***** Sub-headline
      bla bla bla
*** June
***** Sub-headline
      bla bla bla
--8<---------------cut here---------------end--------------->8---

I want the capture process to add an entry to the Sub-headline of June. If
I just use the file+headline and specify "Sub-headline" then it will add to
the Sub-headline in May. That's why I tried using IDs in the first
place. Also, every month I create a new month headline with the
Sub-headline and the capture process should add an entry to that
instead. That is the reason I wanted to get the ID from a function, instead
of just writing it in the capture template.


As far as I understand if I use the file+function target then the function
must return the headline name, but how can I say that I want the
Sub-headline of June and not of May? [first problem]

I found an org-id-find function that returns something like
"(filename . characterPosition)". Therefore, If there is a way to specify a
position where org should start looking for the headline then I could use
that to go to the correct Sub-headline.


Also, the file+headline target will add the entry as a child of the
specified headline, but file+function seems to add the entry as a sibling
of the headline returned by the function. [second problem] Is this intended
behaviour or is it a bug?

--
Thanks again,
Darlan


At Fri, 10 Jun 2011 07:31:43 +0200,
David Maus <dmaus@ictsoc.de> wrote:
> 
> [1  <text/plain; US-ASCII (7bit)>]
> At Thu, 09 Jun 2011 15:18:00 -0300,
> Darlan Cavalcante Moreira wrote:
> >
> >
> > Hello List,
> >
> > I'm trying to create a few templates for org capture and I have found a
> > weird behavior with the ID target type. It works OK if I use the ID
> > directly like below
> > #+begin_src emacs-lisp
> >   (id "Junho2011Contas")
> > #+end_src
> > but it does not find the ID if I try to get it from a function, such as
> > #+begin_src emacs-lisp
> >   (id (get-me-an-org-id-for-the-month "Contas"))
> > #+end_src
> 
> Org capture does not support executing a function in the ID target spec.
> 
> C-h v org-capture-templates RET
> 
> target       Specification of where the captured item should be placed.
>              In Org-mode files, targets usually define a node.  Entries will
>              become children of this node, other types will be added to the
>              table or list in the body of this node.
> 
>              Most target specifications contain a file name.  If that file
>              name is the empty string, it defaults to `org-default-notes-file'.
>              A file can also be given as a variable, function, or Emacs Lisp
>              form.
> 
>              Valid values are:
> 
>              (file "path/to/file")
>                  Text will be placed at the beginning or end of that file
> 
>              (id "id of existing org entry")
>                  File as child of this entry, or in the body of the entry
> 
>              ...
> 
> You might use the `function' target spec
> 
>              (function function-finding-location)
>                 Most general way, write your own function to find both
>                 file and location
> 
> HTH,
>   -- David
> --
> OpenPGP... 0x99ADB83B5A4478E6
> Jabber.... dmjena@jabber.org
> Email..... dmaus@ictsoc.de
> [2  <application/pgp-signature (7bit)>]
> 

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

* Re: Define capture template with dynamic id target
  2011-06-10 15:50   ` Darlan Cavalcante Moreira
@ 2011-06-11  8:06     ` Aankhen
  0 siblings, 0 replies; 4+ messages in thread
From: Aankhen @ 2011-06-11  8:06 UTC (permalink / raw)
  To: Darlan Cavalcante Moreira, David Maus, Org mailing list

Hi Darlan,

On Fri, Jun 10, 2011 at 21:20, Darlan Cavalcante Moreira
<darcamo@gmail.com> wrote:
> Thanks David,
>
> I tried to follow your suggestion, but I found two problems (maybe because
> I know little about lisp).
>
> For instance, suppose I have a test.org file with the follow content
> --8<---------------cut here---------------start------------->8---
> * 2011
>  Every headline has an ID, but I have omitted here for brevity
> *** May
> ***** Sub-headline
>      bla bla bla
> *** June
> ***** Sub-headline
>      bla bla bla
> --8<---------------cut here---------------end--------------->8---
>
> I want the capture process to add an entry to the Sub-headline of June. If
> I just use the file+headline and specify "Sub-headline" then it will add to
> the Sub-headline in May. That's why I tried using IDs in the first
> place. Also, every month I create a new month headline with the
> Sub-headline and the capture process should add an entry to that
> instead. That is the reason I wanted to get the ID from a function, instead
> of just writing it in the capture template.
>
>
> As far as I understand if I use the file+function target then the function
> must return the headline name, but how can I say that I want the
> Sub-headline of June and not of May? [first problem]
>
> I found an org-id-find function that returns something like
> "(filename . characterPosition)". Therefore, If there is a way to specify a
> position where org should start looking for the headline then I could use
> that to go to the correct Sub-headline.
>
>
> Also, the file+headline target will add the entry as a child of the
> specified headline, but file+function seems to add the entry as a sibling
> of the headline returned by the function. [second problem] Is this intended
> behaviour or is it a bug?
> [snip]

Looking at the code, the function doesn’t need to return anything; it
just needs to place point where you want the new headline to appear.
Therefore, you can use ‘org-find-olp’ to locate the entry:

,----[ C-h f org-find-olp RET ]
| org-find-olp is a compiled Lisp function in `org.el'.
|
| (org-find-olp PATH &optional THIS-BUFFER)
|
| Return a marker pointing to the entry at outline path OLP.
| If anything goes wrong, throw an error.
| You can wrap this call to catch the error like this:
|
|   (condition-case msg
|       (org-mobile-locate-entry (match-string 4))
|     (error (nth 1 msg)))
|
| The return value will then be either a string with the error message,
| or a marker if everything is OK.
|
| If THIS-BUFFER is set, the outline path does not contain a file,
| only headings.
`----

So the code would look something like this:

,----
| (org-find-olp '("2011" "May" "Sub-headline") t)
`----

Aankhen

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

end of thread, other threads:[~2011-06-11  8:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-09 18:18 Define capture template with dynamic id target Darlan Cavalcante Moreira
2011-06-10  5:31 ` David Maus
2011-06-10 15:50   ` Darlan Cavalcante Moreira
2011-06-11  8:06     ` Aankhen

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 read-only IMAP folder(s) and NNTP newsgroup(s).