The following program is from https://www.cs.bu.edu/teaching/tool/emacs/programming/
#include <iostream>
int main()
{
int howmany;
std::cout << "Enter a number: ";
std::cin >> howmany;
std::cout << "howmany=" << howmany << std::endl;
return 0;
}
It compiled and ran in Emacs shell:
SPC c C (compile)
SPC ! (shell-command)
Shell command: ./total
The program did not stop and prompt for input. The program just blows by the "std::cin >> howmany;" and assigns 0 to howmany. I did not enter any input. For one second the mini buffer said:
Enter a number: 0
The *Messages* buffer says:
user-error: Minibuffer window is not active
Enter a number: howmany=0
The executable works fine when run from Bash shell:
$ ./total
Enter a number: 2
howmany=2
Why does std::cin work in Bash shell, but not in Emacs shell?
https://www.gnu.org/software/emacs/manual/html_node/efaq-w32/Subprocess-buffering.html says:
having an explicit flag to control their buffering behavior,
typically -i for interactive, or by a special environment variable.
Would that fix it? Where does the -i flag go?
I am using Emacs 25.2.1 with Evil Spacemacs and Ivy on Linux.
UPDATE_1
The interactive program works when shell-commend is called with asynchronously option:
SPC SPC (counsel-M-x) async-shell-command
Enter a number: 2
howmany=2
From Emacs help describe-function shell-command:
shell-command is an interactive compiled Lisp function in ‘simple.el’.
...
If COMMAND ends in ‘&’, execute it asynchronously.
The output appears in the buffer ‘*Async Shell Command*’.
That buffer is in shell mode. You can also use
‘async-shell-command’ that automatically adds ‘&’.
Otherwise, COMMAND is executed synchronously. The output appears in
the buffer ‘*Shell Command Output*’.
Why would interactive program not work on a synchronous system? https://en.wikipedia.org/wiki/Asynchronous_system
I am thinking of binding async-shell-command
to SPC !
.
Is there a disadvantage to always running async-shell-command
instead of the default shell-command
?
shell-command
isn't intended for use with interactive programs like yours. You should try usingshell
,term
oransi-term
for these. – user12563 Jun 25 '17 at 13:56