13

I've read the documentation on how to make interactive calls from within Elisp, but I still can't figure out how to pass the universal argument when using call-interactively on a command that recognizes the universal argument.

More specifically, I want to implement a function that, under certain conditions, should call the shell command interactively with the universal argument, so that, as a result, the prompt

Shell buffer (default *shell*): 

will be visible in the minibuffer, and, once this prompt gets a response, the specified buffer will be created (if it doesn't exist already), and made the current buffer.


FWIW, I tried the following:

(universal-argument)
(call-interactively 'shell)

and

(setq prefix-arg (list 4))
(call-interactively 'shell)

...but I never saw the prompt Shell buffer (default *shell*):; instead, in all cases, the shell command ran exactly as if it had been called without the universal argument. (I really don't know what I'm doing here, so my blind attempts above were made with conscious disregard of the documentation's prescient advice.)

Drew
  • 75,699
  • 9
  • 109
  • 225
kjo
  • 3,145
  • 14
  • 42

1 Answers1

14

According to shell's interactive form, as long as current-prefix-arg is non-nil, shell will ask user a buffer to use, so you can set current-prefix-arg to non-nil (4 is used in following as an example):

(let ((current-prefix-arg 4))
  (call-interactively 'shell))

or simulate executing shell with a prefix 4 (M-4 M-x shell):

(execute-extended-command 4 "shell")
xuchunyang
  • 14,302
  • 1
  • 18
  • 39
  • Just a tiny quibble: if one is going to use a truthy value other than plain old `t`, shouldn't that be `(list 4)` (or `'(4)` if you prefer)? I realize that the current version of `shell` does not care about the difference between `4` and `(list 4)` (it cares only that the value is non-`nil`), but future versions might. – kjo Apr 15 '16 at 14:57
  • 2
    `4` simulates `M-4 M-x shell`, `'(4)` simulates `C-u M-x shell` – npostavs Apr 15 '16 at 15:08