emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Can `org-capture` templates be made to result in a sub-heading of the current heading?
@ 2023-05-10 21:04 Tim Visher
  2023-05-11 12:42 ` Tim Visher
  2023-05-12 12:08 ` Ihor Radchenko
  0 siblings, 2 replies; 6+ messages in thread
From: Tim Visher @ 2023-05-10 21:04 UTC (permalink / raw)
  To: Emacs Org Mode mailing list

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

Hey everyone,

Can `org-capture` templates be made to result in a sub-heading of the
current heading?

So

```
* This Week
** TODO A TODO Item

   [2023-05-05 Fri 10:47]

   A description <point's here>
```

and I whack my capture keychord and get

```
* This Week
** TODO A TODO Item

   [2023-05-05 Fri 10:47]

   A description
*** [2023-05-10 Wed 17:02]

    [2023-05-10 Wed 17:02]

    <point's here>
```

I have a tendency to make these items for longer running tasks that I want
to keep a journal on. The visibility cycling makes it easier to see my
progress over time.

Thanks in advance!

-- Tim Visher

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

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

* Re: Can `org-capture` templates be made to result in a sub-heading of the current heading?
  2023-05-10 21:04 Can `org-capture` templates be made to result in a sub-heading of the current heading? Tim Visher
@ 2023-05-11 12:42 ` Tim Visher
  2023-05-11 13:05   ` Tim Visher
  2023-05-12 12:08 ` Ihor Radchenko
  1 sibling, 1 reply; 6+ messages in thread
From: Tim Visher @ 2023-05-11 12:42 UTC (permalink / raw)
  To: Emacs Org Mode mailing list

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

On Wed, May 10, 2023 at 5:04 PM Tim Visher <tim.visher@gmail.com> wrote:

> Can `org-capture` templates be made to result in a sub-heading of the
> current heading?
>
> So
>
> ```
> * This Week
> ** TODO A TODO Item
>
>    [2023-05-05 Fri 10:47]
>
>    A description <point's here>
> ```
>
> and I whack my capture keychord and get
>
> ```
> * This Week
> ** TODO A TODO Item
>
>    [2023-05-05 Fri 10:47]
>
>    A description
> *** [2023-05-10 Wed 17:02]
>
>     [2023-05-10 Wed 17:02]
>
>     <point's here>
> ```
>

It's worth noting that with a capture template like

```
("twj" "TODO Work TODO Journal" entry
 (file+headline "~/Documents/todo.org" "Inbox")
 "* %U

   %U

   %?")
```

If I do the usual `M-0 M-x org-capture` with point in the original spot it
behaves exactly as I want it to. My goal is to get it to behave that way
just by invoking the capture template.

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

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

* Re: Can `org-capture` templates be made to result in a sub-heading of the current heading?
  2023-05-11 12:42 ` Tim Visher
@ 2023-05-11 13:05   ` Tim Visher
  2023-05-11 13:10     ` Tim Visher
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Visher @ 2023-05-11 13:05 UTC (permalink / raw)
  To: Emacs Org Mode mailing list

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

On Thu, May 11, 2023 at 8:42 AM Tim Visher <tim.visher@gmail.com> wrote:

> On Wed, May 10, 2023 at 5:04 PM Tim Visher <tim.visher@gmail.com> wrote:
>
>> Can `org-capture` templates be made to result in a sub-heading of the
>> current heading?
>>
>> So
>>
>> ```
>> * This Week
>> ** TODO A TODO Item
>>
>>    [2023-05-05 Fri 10:47]
>>
>>    A description <point's here>
>> ```
>>
>> and I whack my capture keychord and get
>>
>> ```
>> * This Week
>> ** TODO A TODO Item
>>
>>    [2023-05-05 Fri 10:47]
>>
>>    A description
>> *** [2023-05-10 Wed 17:02]
>>
>>     [2023-05-10 Wed 17:02]
>>
>>     <point's here>
>> ```
>>
>
> It's worth noting that with a capture template like
>
> ```
> ("twj" "TODO Work TODO Journal" entry
>  (file+headline "~/Documents/todo.org" "Inbox")
>  "* %U
>
>    %U
>
>    %?")
> ```
>
> If I do the usual `M-0 M-x org-capture` with point in the original spot it
> behaves exactly as I want it to. My goal is to get it to behave that way
> just by invoking the capture template.
>

OK after poking around in `org-capture-set-target-location` I think I have
this sorted. Please let me know if I'm doing something obviously silly. :)

```
(defun timvisher--org-capture-sub-heading-insertion-point
    ()
  (insert "\n")
  (forward-char)
  (org-capture-put :exact-position (point) :insert-here t))

;; Embedded in the capture templates list
("twj" "TODO Work TODO Journal" entry
 (function timvisher--org-capture-sub-heading-insertion-point)
 "* %U

  %U

  %?")
```

The key was finding that it wasn't enough to merely move point to the
proper location. I _also_ needed to call `org-capture-put`. The docs on
this subject are a little misleading, IMHO.

‘(function function-finding-location)’
>      Most general way: write your own function which both visits
>      the file and moves point to the right location.


Empirically it seems like you have to visit the file, move point to the
right location, _and_ call `org-capture-put`.

-- Tim Visher

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

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

* Re: Can `org-capture` templates be made to result in a sub-heading of the current heading?
  2023-05-11 13:05   ` Tim Visher
@ 2023-05-11 13:10     ` Tim Visher
  0 siblings, 0 replies; 6+ messages in thread
From: Tim Visher @ 2023-05-11 13:10 UTC (permalink / raw)
  To: Emacs Org Mode mailing list

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

On Thu, May 11, 2023 at 9:05 AM Tim Visher <tim.visher@gmail.com> wrote:

> On Thu, May 11, 2023 at 8:42 AM Tim Visher <tim.visher@gmail.com> wrote:
>
>> On Wed, May 10, 2023 at 5:04 PM Tim Visher <tim.visher@gmail.com> wrote:
>>
>>> Can `org-capture` templates be made to result in a sub-heading of the
>>> current heading?
>>>
>>> So
>>>
>>> ```
>>> * This Week
>>> ** TODO A TODO Item
>>>
>>>    [2023-05-05 Fri 10:47]
>>>
>>>    A description <point's here>
>>> ```
>>>
>>> and I whack my capture keychord and get
>>>
>>> ```
>>> * This Week
>>> ** TODO A TODO Item
>>>
>>>    [2023-05-05 Fri 10:47]
>>>
>>>    A description
>>> *** [2023-05-10 Wed 17:02]
>>>
>>>     [2023-05-10 Wed 17:02]
>>>
>>>     <point's here>
>>> ```
>>>
>>
>> It's worth noting that with a capture template like
>>
>> ```
>> ("twj" "TODO Work TODO Journal" entry
>>  (file+headline "~/Documents/todo.org" "Inbox")
>>  "* %U
>>
>>    %U
>>
>>    %?")
>> ```
>>
>> If I do the usual `M-0 M-x org-capture` with point in the original spot
>> it behaves exactly as I want it to. My goal is to get it to behave that way
>> just by invoking the capture template.
>>
>
> OK after poking around in `org-capture-set-target-location` I think I have
> this sorted. Please let me know if I'm doing something obviously silly. :)
>
> ```
> (defun timvisher--org-capture-sub-heading-insertion-point
>     ()
>   (insert "\n")
>   (forward-char)
>   (org-capture-put :exact-position (point) :insert-here t))
>
> ;; Embedded in the capture templates list
> ("twj" "TODO Work TODO Journal" entry
>  (function timvisher--org-capture-sub-heading-insertion-point)
>  "* %U
>
>   %U
>
>   %?")
> ```
>

Minor correction. I need to both insert a newline _and *leave*_ point where
it was or I don't get a sub-heading at the proper level (the current level
of the previous journal entry). If I don't insert the newline then
cancelling the entry also pulls the next heading up into the text of the
current heading.

```
(defun timvisher--org-capture-sub-heading-insertion-point
    ()
  (insert "\n")
  (org-capture-put :exact-position (point) :insert-here t))
…
```

-- Tim Visher

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

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

* Re: Can `org-capture` templates be made to result in a sub-heading of the current heading?
  2023-05-10 21:04 Can `org-capture` templates be made to result in a sub-heading of the current heading? Tim Visher
  2023-05-11 12:42 ` Tim Visher
@ 2023-05-12 12:08 ` Ihor Radchenko
  2023-05-12 14:24   ` Tim Visher
  1 sibling, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2023-05-12 12:08 UTC (permalink / raw)
  To: Tim Visher; +Cc: Emacs Org Mode mailing list

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

> Can `org-capture` templates be made to result in a sub-heading of the
> current heading?

Just use target `here' (as symbol).

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: Can `org-capture` templates be made to result in a sub-heading of the current heading?
  2023-05-12 12:08 ` Ihor Radchenko
@ 2023-05-12 14:24   ` Tim Visher
  0 siblings, 0 replies; 6+ messages in thread
From: Tim Visher @ 2023-05-12 14:24 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Emacs Org Mode mailing list

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

On Fri, May 12, 2023 at 8:05 AM Ihor Radchenko <yantar92@posteo.net> wrote:

> Tim Visher <tim.visher@gmail.com> writes:
> > Can `org-capture` templates be made to result in a sub-heading of the
> > current heading?
>
> Just use target `here' (as symbol).
>

Thanks for the suggestion, Ihor.

Do you have an example of this actually working? I'm on `Org mode version
9.6.5` which looks to be the latest available from GNU ELPA and there seems
to be a dispute between `org-capture-set-target-location` which understands
<https://git.savannah.gnu.org/cgit/emacs/org-mode.git/tree/lisp/org-capture.el?id=30b790f42bd0de95ca15d927e27b9125b6549043#n993>
that the result of `(org-capture-get :target)` can return a symbol rather
than a list vs. `org-capture` itself which assumes
<https://git.savannah.gnu.org/cgit/emacs/org-mode.git/tree/lisp/org-capture.el?id=30b790f42bd0de95ca15d927e27b9125b6549043#n719>
that target will _always_ be a list.

My testing code:

```
(timvisher--comment
 ;; Context
 (defmacro timvisher--comment
     (&rest _)
   nil)

 (defmacro wcb (buffer &rest body)
   `(with-current-buffer ,buffer
      ,@body))

 ;; Demo code
 (setq org-capture-templates
       '(("1" "1" entry here "* 1")
         ("2" "2" entry (here) "* 2")))

 ;; Fails with `Capture template ‘1’: Wrong type argument: listp, here`
 ;; because of `… (car (org-capture-get :target)) …`
 (wcb "todo.org"
      (org-capture nil "1"))

 ;; Fails with `Invalid capture target specification: (here)` because
 ;; `(pcase (or target (org-capture-get :target)) …)` doesn't have a case
 ;; for `(here)`
 (wcb "todo.org"
      (org-capture nil "2"))
 )
```

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

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

end of thread, other threads:[~2023-05-12 14:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-10 21:04 Can `org-capture` templates be made to result in a sub-heading of the current heading? Tim Visher
2023-05-11 12:42 ` Tim Visher
2023-05-11 13:05   ` Tim Visher
2023-05-11 13:10     ` Tim Visher
2023-05-12 12:08 ` Ihor Radchenko
2023-05-12 14:24   ` Tim Visher

Code repositories for project(s) associated with this public 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).