Hi all,
2011/9/13 Stefan Monnier
<monnier@iro.umontreal.ca>
>> IMO native tab completion for subprocess REPLs is a common enough need
>> that the basic machinery should be handled by Emacs itself, i.e.
>> probably the comint library. I'm somewhat surprised there is no such
>> code yet (or is there, outside Emacs perhaps?).
> I agree with the exposed above, many modern REPLs comint can interact with
> have TAB completion out of the box. We should use that when it's available
> instead of duplicating efforts.
I agree that would be good.
You can check the gdb completion code in gud.el for an example.
The current shell completion machinery of my python.el works pretty much like the gdb does, the thing is that on the python shell this approach is likely to break on multi-line statements.
Good news is that this weekend I got back to try stuff again to get native TAB support on the comint process, and while travelling back to my hometown from the PyCon Argentina (the Bus ride was getting boring) I found something that made the shell react pretty much like the real thing when sending a raw tab: I started the comint process in the same way ansi-term would, using sh + stty.
Here is the quick snippet:
(defun python-shell-make-comint (cmd proc-name &optional pop)
"Create a python shell comint buffer.
CMD is the python command to be executed and PROC-NAME is the
process name the comint buffer will get. After the comint buffer
is created the `inferior-python-mode' is activated. If POP is
non-nil the buffer is shown."
(save-excursion
(let* ((proc-buffer-name (format "*%s*" proc-name))
(process-environment (python-shell-calculate-process-environment))
(exec-path (python-shell-calculate-exec-path)))
(when (not (comint-check-proc proc-buffer-name))
(let* ((cmdlist (split-string-and-unquote cmd))
(sh-executable (executable-find "sh"))
(stty-executable (executable-find "stty"))
(buffer (if (and sh-executable stty-executable)
(apply 'make-comint proc-name
sh-executable nil "-c"
(format "%s -nl echo rows %d columns %d sane 2>/dev/null;\
if [ $1 = .. ]; then shift; fi; exec \"$@\""
stty-executable
(window-height) (1- (window-width)))
".."
(car cmdlist) (cdr cmdlist))
(apply 'make-comint proc-name (car cmdlist) nil
(cdr cmdlist))))
(current-buffer (current-buffer)))
(with-current-buffer buffer
(inferior-python-mode)
(python-util-clone-local-variables current-buffer))))
(when pop
(pop-to-buffer proc-buffer-name))
proc-buffer-name)))
After that, when I evaled:
(process-send-string (get-buffer-process (current-buffer)) "Tru ")
The shell got updated and said True.
If I evaled:
(process-send-string (get-buffer-process (current-buffer)) "T ")
TabError True TypeError
Got returned and the shell displayed correctly as it does on a full featured terminal.
I need to work a little more on the extra bookkeeping involved into getting comint know about the stuff that got inserted in the process but this got me really close to have it working as intended.
What worries me is the Window support for this, is there something similar like stty there? What I'm thinking is a good approach would be that when sh and stty are not found via `executable-find', fallback to the old style completion.
I'll continue working on this in the following days (hopefully I'll get lucky). After that we can see if there is some kind of improvement we can do in the comint library itself.
Regards,
Fabián E. Gallina