Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Interactive-form invoked with specified arg

3 views
Skip to first unread message

gentsquash

unread,
Jun 4, 2013, 12:31:08 PM6/4/13
to
Consider a FORM which prompts the user to type a character.
E.g, FORM might be

(y-or-n-p "Are you there?")
or
(query-replace "wierd" "weird") .

Is there a way to programmatically invoke FORM with the
character already specified? E.g

(invoke-as-if-I-typed (query-replace "wierd" "weird") "!")

behaves as if I invoked (query-replace "wierd" "weird"),
then, when prompted, typed "!".
-Jonathan

Emanuel Berg

unread,
Jun 4, 2013, 1:17:02 PM6/4/13
to
gentsquash <gents...@gmail.com> writes:

> (y-or-n-p "Are you there?")
> or
> (query-replace "wierd" "weird") .
>
> Is there a way to programmatically invoke FORM with the
> character already specified? E.g
>
> (invoke-as-if-I-typed (query-replace "wierd" "weird") "!")
>
> behaves as if I invoked (query-replace "wierd" "weird"), then,
> when prompted, typed "!".

Well, why would you want to emulate typing? If you told us the
overall goal perhaps we could find a better solution. Because
intuitively, emulate typing seems a bit strange, as typing *in the
first place* most often is an interface to invoking functions and
passing arguments.

You might check out `C-h f call-interactively'.
--
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573

gentsquash

unread,
Jun 4, 2013, 7:20:11 PM6/4/13
to
On Jun 4, 1:17pm, Emanuel Berg <embe8...@student.uu.se> wrote:

> ... intuitively, emulate typing seems a bit strange...

Not to me. Keyboard macros are a mechanism to convert
typed-actions into programmatic code. E.g, I could use

(execute-kbd-macro
[?\M-x ?q ?u ?e ?r ?y ?- ?r ?e ?p ?l ?a ?c ?e return ?a ?t
return ?@ return ?!] )

in place of my desired

(invoke-as-if-I-typed (query-replace "at" "@") "!")

However, firstly, this is clumsy. Secondly, it
doesn't allow me to pass-in the fnc-name,
e.g sometimes `query-replace',
sometimes `query-replace-regexp',
sometimes `dired-do-query-replace-regexp'.

> ...
> You might check out `C-h f call-interactively'.

I looked at it before I posted. If someone can explain to me
how it does what I want, then I'll be grateful.

As to why I seek `invoke-as-if-I-typed', there are
many reasons, but here is one. Imagine a fnc FOO that
sequentially invokes a bunch of fncs that will
prompt the user. While debugging FOO, I'd like to
make some of the branches in the execution tree [the
tree is controlled by user-input] to be executed
automatically, while I debug the other parts.

-Jonathan

Emanuel Berg

unread,
Jun 4, 2013, 7:53:01 PM6/4/13
to
gentsquash <gents...@gmail.com> writes:

> I looked at it before I posted. If someone can explain to me
> how it does what I want, then I'll be grateful.

Well, take replace-string for example. If you want to call it from
Elisp, it looks like this:

(replace-string FROM-STRING TO-STRING &optional DELIMITED START
END)

In the below function, you see how it is combined with
call-interactively. The result is the same as if the user would
have hit `M-x replace-string'. But, you don't have to write a
whole new interface (that does the same thing).

(defun replace-str ()
"Like `replace-string', but reset point when done."
(interactive)
(save-excursion
(call-interactively 'replace-string) ))

But, as for your purposes, now that you have described them in
more detail, I don't think it is the solution.

Stefan Monnier

unread,
Jun 4, 2013, 10:45:14 PM6/4/13
to help-gn...@gnu.org
> (invoke-as-if-I-typed (query-replace "wierd" "weird") "!")

How 'bout

(let ((unread-command-events '(?!)))
(query-replace "wierd" "weird"))


-- Stefan


gentsquash

unread,
Jun 5, 2013, 12:49:20 PM6/5/13
to
Ah -that's the ticket! (Strange expression in English -I
wonder where it comes from?)

Thank you, both, Stefan and Emanuel. -Jonathan

gentsquash

unread,
Jun 5, 2013, 1:18:11 PM6/5/13
to
Making Stefan's solution a macro, I am now using
this in my code:

(defmacro invoke-with-typing (FORM &rest CHARS)
"JK:05Jun2013: Executes FORM, which would normally prompt
the user for input. Here, CHARS are characters sent to the
form, as-if the user had typed them.

USAGE: (invoke-with-typing (query-replace \"wierd\" \"STRANGE\") ?y ?
n ?! )
will replace the first occurrence of \"wierd\", not the second,
and
all the remaining occurrences

Note that FORM needs to be a form, rather than evaluate to a form."
`(let ((unread-command-events ',CHARS)) ,FORM)
)

--Jonathan
0 new messages