it looks like something went avry in the interaction of SWI Prolog with Emacs (27.x on Windows in my case). Essentially, if you run swipl as an inferior prolog, you lose the prompt ?- . Any idea about how to fix this?
On Linux, I usually run Prolog in an Emacs terminal (shell), and start it with the option --no-tty. And I don’t see the problem you describe … BUT I’ve noticed problems with ctrl-D when running emacs on WSL, even when running in a shell. My guess is that the Windows terminal is getting in the way - maybe the latest version of WSL is better? (I haven’t upgraded - I prefer to just avoid Windows in general.)
Another possibility - use the graphical interface with native Windows (or Linux).
In any case the point is to ensure that run-prolog works properly in Emacs. It does, but somehow the prompt is lost. My suspicion is that SWI Prolog now does something funky with color an that that confuses the prompt regexp.
You can disable the color output by setting the Prolog flag color_term to false. More likely is that it doesn’t think it is connected to a tty. You can change that using set_stream(user_input, tty(true)) if I recall correctly. All in all it is complicated. Different Emacs inferior process modes identify themselves differently
I’ve verified that the INSIDE_EMACS environment variable exists in Emacs 26.3 and 28.0.50.
If you want to be more precise about the environment, you can verify that the variable is of the form '28.0.50,comint' (and it’s an atom, not a string).
Probably the check on INSIDE_EMACS, is an overkill, but checking TERM seems TRT to do. It definitively works properly in cmd, PowerShell, and git bash on Windows 10 (I presume it will work elsewhere).
Last minor thingy: the regexp for the prompt in prolog-mode.el is probably outdated (it looses the query number). Oh well…
There are many other possible values for TERM, such as xterm-256color (which is what I get from the standard Ubuntu terminal). But for both xterm and xterm-256color, I see that tty(true) is already set (there was a discussion about this a while ago, and swipl sets tty if isatty() is true (more-or-less; I’m sure there are other complications).
Anyway, for my purposes, the check for INSIDE_EMACS suffices.
BTW, you can use an atom instead of string for getenv/2, if you wish; the result seems to always be an atom.
On Unix-like systems you normally do not need such hacks. Any application that wants to embed another interactive application typically does so using a pseudo tty and the Unix system call isatty() will tell us. In Windows there is (AFAIK) no good alternative to this. Well, we can find out we are connected to a proper Windows console, but a console is not used for running Prolog inside Emacs.
I have pushed this patch to setup the tty when we detect Emacs on Windows. Not tested.
So, it would apear that the isatty() test doesn’t do what you expect in an Emacs window. (This applies for both running in a shell and in an inferior Prolog window.)
tty_control and whether or not the user_input is a tty has different effects. tty_control allows Prolog to change the TTY settings to setup single keystroke interaction to (notably) the tracer and toplevel. This doesn’t work under Emacs. The user_inputtty property tells the system this is a tty, so it should prompt (and try to setup other stuff that is useful when talking to a human.
The system tries to detect Emacs at startup, but this code was broken. Possibly 8.3.15 is better. We’ll see
Please check 8.3.15 without any user settings and report your findings. Hopefully it is fixed. Else the report should provide input for a next round refining the rules
The following is copied from my Emacs window (emacs 28.0.50, built from git://git.sv.gnu.org/emacs.git)
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.15)
?- member(X,[1,2,3]).
X = 1 ;
X = 2 ;
X = 3.
?- current_prolog_flag(tty_control, Z).
Z = false.