prompt-toolkit / pyvim

Pure Python Vim clone.
BSD 3-Clause "New" or "Revised" License
2.52k stars 160 forks source link

editor_buffer.filename is None: when i try to make execution (F9) work with python 2.7 and 3.8 on a Mac #136

Open rasimuddinvs opened 3 years ago

rasimuddinvs commented 3 years ago

File "/Users/xxxx/Library/Python/2.7/lib/python/site-packages/prompt_toolkit/key_binding/key_processor.py", line 275, in process_keys self._process_coroutine.send(key_press) File "/Users/xxxx/Library/Python/2.7/lib/python/site-packages/prompt_toolkit/key_binding/key_processor.py", line 182, in _process self._call_handler(matches[-1], key_sequence=buffer[:]) File "/Users/xxxx/Library/Python/2.7/lib/python/site-packages/prompt_toolkit/key_binding/key_processor.py", line 325, in _call_handler handler.call(event) File "/Users/xxxx/Library/Python/2.7/lib/python/site-packages/prompt_toolkit/key_binding/key_bindings.py", line 79, in call return self.handler(event) File "/Users/xxxx/.pyvimrc", line 67, in save_and_execute_python_file if editor_buffer.filename is None:

thuibr commented 2 years ago

To fix this, make the following changes:

New import at the top:

from prompt_toolkit.application import run_in_terminal

Changes to the function that F9 binds to:


    @editor.add_key_binding(Keys.F9)
    def save_and_execute_python_file(event):
        """
        F9: Execute the current Python file.
        """
        # Save buffer first.
        editor_buffer = editor.current_editor_buffer

        if editor_buffer is not None:
            if editor_buffer.location is None:
                editor.show_message("File doesn't have a filename. Please save first.")
                return
            else:
                editor_buffer.write()

        # Now run the Python interpreter. But use
        # `CommandLineInterface.run_in_terminal` to go to the background and
        # not destroy the window layout.
        def execute():
            call(['python3', editor_buffer.location])
            six.moves.input('Press enter to continue...')

        run_in_terminal(execute)
thuibr commented 2 years ago

See https://github.com/prompt-toolkit/pyvim/pull/140