DarwinAwardWinner / with-simulated-input

Test your interactive elisp functions non-interactively!
GNU General Public License v3.0
38 stars 4 forks source link

Does not work with org-capture? (edit: maybe better as "how do I insert text?") #8

Closed Trevoke closed 4 years ago

Trevoke commented 4 years ago

Hi, I'm trying the following:

    (with-simulated-input '("i" (insert "foobar") "C-c C-c")
                          (org-capture))

And that seems to properly create a capture (I have i as a capture key shortcut, and C-c C-c to finalize the capture) but it does not insert "foobar" in the capture. What am I missing?

DarwinAwardWinner commented 4 years ago

If all you're doing is inserting text, you can just include it in the string of keystrokes, as such:

(with-simulated-input '("i foobar C-c C-c")
                      (org-capture))

(Note that there's no significance to the space between "i" and "foobar". If in doubt, run the string through kbd. If you need to actually type a space, use SPC, e.g. "i hello SPC world C-c C-c".)

However, the code you showed should generally also work, unless org-capture is doing something other than just having you enter text normally. So try the above code, and if it doesn't work, then it's probably a bug or inherent limitation in with-simulated-input.

Trevoke commented 4 years ago

Yeah -- it does not work.

I need to do this:

(org-capture nil "i") ;; I tell org to capture in the same way as interactively, using the `i` shortcut
(insert "foobar") ;; I insert "foobar"
(org-capture-finalize) ;; this is C-c C-c

It's unfortunate because I really wanted to be able to use with-simulated-input for this. Can we make this a feature request?

For future reference, this is the source code for org-capture: https://code.orgmode.org/bzg/org-mode/src/master/lisp/org-capture.el#L594

DarwinAwardWinner commented 4 years ago

Ok, I think I know why you're having this issue. Refer to this part of the documentation for with-simulated-input:

If BODY does not consume all the input events in KEYS, the remaining input events in KEYS are discarded, and any remaining Lisp forms in KEYS are never evaluated.

It looks like the org-capture function returns after reading just the "i", at which point with-simulated-input is done and the rest of the input is ignored. The purpose of with-simulated-input is to perform tasks that can't be performed with imperative code. However, your reply indicates that the org-capture workflow is perfectly usable with simple imperative code, so the extra complexity of with-simulated-input is extraneous.

If the org-capture command encompassed the functionality of both org-capture and org-capture-finalize and did not return until after the finalization, then you might need with-simulated-input to properly interact with it programmatically. But that's not the case: org-capture seems already designed with programmatic use in mind.

Trevoke commented 4 years ago

Ohh.... That makes sense. Gotcha. Yes, I misunderstood what with-simulated-input did. Thanks.

DarwinAwardWinner commented 4 years ago

I suppose it would be useful to have a debug mode that warns when input is left unconsumed.