Repository: xonsh/xonsh

Issue:
Tab completing on empty line with prompt_toolkit kills terminal
Arch Linux with xonsh 0.2.2 and using prompt toolkit.  Hitting TAB on an empty line crashes the terminal (rxvt-unicode).  I can see the traceback for a split second but then the terminal is gone.  

This doesn't happen using readline.



Discussion:
This is probably a simple fix to limit the number of completions displayed in prompt-toolkit.  This is probably a couple of character change in PromptToolkitShell, if you wanna take a stab at this.

My laptop has the same issue but without crashing the terminal, so I have a traceback now.  I'm going to dig in and try to fix this but leaving it here for completion.

``` Python
Traceback (most recent call last):
  File "/home/gil/miniconda3/bin/xonsh", line 3, in <module>
    main()
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/main.py", line 101, in main
    shell.cmdloop()
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/prompt_toolkit_shell.py", line 69, in cmdloop
    display_completions_in_columns=True)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/shortcuts.py", line 433, in prompt
    document = cli.run(reset_current_buffer=False)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/interface.py", line 327, in run
    self.eventloop.run(self.input, self.create_eventloop_callbacks())
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/eventloop/posix.py", line 87, in run
    inputstream.feed(data)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/terminal/vt100_input.py", line 312, in feed
    self._input_parser.send(c)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/terminal/vt100_input.py", line 264, in _input_parser_generator
    self._call_handler(match, prefix)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/terminal/vt100_input.py", line 293, in _call_handler
    self.feed_key_callback(KeyPress(key, insert_text))
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/interface.py", line 780, in feed_key
    self._active_cli.input_processor.feed_key(key_press)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/key_binding/input_processor.py", line 172, in feed_key
    self._process_coroutine.send(key_press)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/key_binding/input_processor.py", line 144, in _process
    self._call_handler(matches[-1], key_sequence=buffer)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/key_binding/input_processor.py", line 187, in _call_handler
    handler.call(event)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/prompt_toolkit/key_binding/registry.py", line 30, in call
    return self._handler(event)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/prompt_toolkit_key_bindings.py", line 34, in _
    event.cli.current_buffer.insert_text(env['INDENT'])
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/environ.py", line 253, in __getitem__
    val = self._d[key]
KeyError: 'INDENT'
Exception ignored in: <bound method PromptToolkitShell.__del__ of <xonsh.prompt_toolkit_shell.PromptToolkitShell object at 0x7f0a9bdd2048>>
Traceback (most recent call last):
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/prompt_toolkit_shell.py", line 54, in __del__
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/prompt_toolkit_shell.py", line 32, in teardown_history
AttributeError: 'module' object has no attribute '__xonsh_env__'
```

Ahhh that `env['INDENT']` should probably be `env.get('INDENT')`, at the very least.


Resolving patch:
diff --git a/xonsh/prompt_toolkit_key_bindings.py b/xonsh/prompt_toolkit_key_bindings.py
--- a/xonsh/prompt_toolkit_key_bindings.py
+++ b/xonsh/prompt_toolkit_key_bindings.py
@@ -15,7 +15,7 @@ class TabShouldInsertIndentFilter(Filter):
     def __call__(self, cli):
         before_cursor = cli.current_buffer.document.current_line_before_cursor
 
-        return bool(not before_cursor or before_cursor.isspace())
+        return bool(before_cursor.isspace())
 
 
 def load_xonsh_bindings(key_bindings_manager):
@@ -31,6 +31,6 @@ def _(event):
         If there are only whitespaces before current cursor position insert
         indent instead of autocompleting.
         """
-        event.cli.current_buffer.insert_text(env['INDENT'])
+        event.cli.current_buffer.insert_text(env.get('INDENT'))
 
 


Test patch: