2

I'm looking for a way to simulate minibuffer input. So, some-func takes some input from the minibuffer and does something with it. The problem is that I have to call some-func from some other function calling-func and I need to do it interactively so I cant just pass an argument.

(defun some-func (arg)
  (interactive "*sEnter something: ")
  ;; Do something with arg
  )

(defun calling-func ()
  (call-interactively 'some-func)
  ;; Type to minibuffer
  )

Any ideas?

Thanks!

rejeep
  • 649
  • 1
  • 6
  • 18

3 Answers3

5

It might be interesting to explore why you need to call the other function interactively... but that's not what you asked.

Here's an example of "calling" a function interactively and sending text to the minibuffer. You just use Emacs keyboard macros:

(defun my-call-find-file (something)
  "An example of how to have emacs 'interact' with the minibuffer
use a kbd macro"
  (interactive "sEnter something:")
  (let ((base-vector [?\M-x ?f ?i ?n ?d ?- ?f ?i ?l ?e return]))
    ;; create new macro of the form
    ;; M-x find-file RET <userinput> RET
    (execute-kbd-macro (vconcat base-vector 
                                (string-to-vector something) 
                                (vector 'return)))))

The relevant documentation are Keyboard Macros and Functions for Vectors.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • I guess I can use something like this. I'll try it out a bit. You ask why I need to call it interactively. I don't *need* to. But it's better. I'm trying some testing. So I want to simulate a user. I can just call the function, but that wouldn't be how the user would interact with it. – rejeep Jan 15 '10 at 21:49
  • Well, the kbd macro is essentially how the user interacts with it. It's just a stream of key presses being sent to Emacs. – Trey Jackson Jan 15 '10 at 22:27
  • The more you learn about Emacs, the more you realize how brilliant it is. Sometimes you doubt if something is possible and it always turns out that it is. – rejeep Jan 15 '10 at 22:35
1

I've been mixing around with the macro stuff. Consider these different cases:

1) When the whole vector is all together it works!

(defun a ()
  (interactive)
  (execute-kbd-macro [?\M-x ?l ?i ?n ?u ?m ?- ?m ?o ?d ?e return]))

2) But when I split it up, it doesn't.

(defun a ()
  (interactive)
  (b)
  (c)
  (d))

(defun b ()
  (execute-kbd-macro [?\M-x]))

(defun c ()
  (execute-kbd-macro [?l ?i ?n ?u ?m ?- ?m ?o ?d ?e]))

(defun d ()
  (execute-kbd-macro (vector 'return)))

3) Running it as a string does not work either.

(defun a ()
  (interactive)
  (execute-kbd-macro (string-to-vector "M-x linum-mode RET")))

(defun a ()
  (interactive)
  (execute-kbd-macro "M-x linum-mode RET"))

I actually need to chain the events together. So, do I need to use vconcat on vectors then?

rejeep
  • 649
  • 1
  • 6
  • 18
0

How about the following:

(defun calling-func ()
  (interactive)
  (call-interactively 'some-func)
  ;; Type to minibuffer
  )

That is, use an empty interactive specification, and pick up the transitive specification via call-interactively.

If that was in fact what you were asking for, there's a nearly identical answer here.

Community
  • 1
  • 1
seh
  • 14,999
  • 2
  • 48
  • 58
  • I don't think this will help me since I still would have to type something and I can't do that. I have to simulate that. See comment to Trey Jackson for reason. – rejeep Jan 15 '10 at 21:51
  • Ah, I thought that you wanted `calling-func` to behave with the exact same interactive interface as `some-func`, without specifying its interactive interface again. – seh Jan 15 '10 at 22:29