0

This question is similar to this one Is it possible to chain key binding commands in sublime text 2? Some years have passed since that question (and the answers given), and I'm using Sublime Text 3 (not 2), so I believe this new question is justified.

I've setup a custom keybind:

{
    "keys": ["f5"],
    "command": "project_venv_repl"
}

to run the project_venv_repl.py script:

import sublime_plugin


class ProjectVenvReplCommand(sublime_plugin.TextCommand):
    """
    Starts a SublimeREPL, attempting to use project's specified
    python interpreter.

    Instructions to make this file work taken from:
    https://stackoverflow.com/a/25002696/1391441
    """

    def run(self, edit, open_file='$file'):
        """Called on project_venv_repl command"""
        cmd_list = [self.get_project_interpreter(), '-i', '-u']

        if open_file:
            cmd_list.append(open_file)

        self.repl_open(cmd_list=cmd_list)

    def get_project_interpreter(self):
        """Return the project's specified python interpreter, if any"""
        settings = self.view.settings()
        return settings.get('python_interpreter', '/usr/bin/python')

    def repl_open(self, cmd_list):
        """Open a SublimeREPL using provided commands"""
        self.view.window().run_command(
            'repl_open', {
                'encoding': 'utf8',
                'type': 'subprocess',
                'cmd': cmd_list,
                'cwd': '$file_path',
                'syntax': 'Packages/Python/Python.tmLanguage'
            }
        )

This runs the opened file in a SublimeREPL when the f5 key is pressed.

What I need is a way to mimic the "Build" shortcut (Ctrl+B). This is: when the f5 key is pressed, the current (opened) file should be saved before running the project_venv_repl command.

Can this instruction be added to the project_venv_repl.py script, or to the command line in the keybind definition?

Community
  • 1
  • 1
Gabriel
  • 40,504
  • 73
  • 230
  • 404

1 Answers1

2

There's no need to do anything fancy. If you just want to save the current view before running the REPL, edit your ProjectVenvReplCommand class's run() method (which is called when the project_venv_repl command is executed) and add the following line at the beginning:

self.view.run_command("save")

This will silently save the current view unless it has not been saved before, in which case a Save As... dialog will open just like usual.

If you want to save all open files in the window, you can use this code:

for open_view in self.view.window().views():
    open_view.run_command("save")
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Matt, I came across an issue with this answer today that I hadn't noticed before. The `self.view.run_command("save")` command will save the current (opened) file perfectly, but not any other opened file with unsaved changes. I tried changing it to `self.view.run_command("save_all")` but it still only saves the current file before running. Can this be done at all? – Gabriel Sep 30 '16 at 18:45
  • 1
    @Gabriel see my edit - you just need to iterate over all the views in the current window. – MattDMo Oct 01 '16 at 17:37
  • Sorry to take this long to reply Matt, I had to do a clean OS install. I tried your solution, and although it does save all the files with unsaved changes in the current view, it also shows a pop-up asking me to save the file from where I launched the REPL, or something similar: `*REPL* [/home/gabriel/.pyenv/versions/test-env/bin/python -i -u /home/gabriel/Github/test.py]`. If I select *Cancel*, the REPL executes normally (with the unsaved files now saved), but that pop-up is not something I want to be closing every time I run my script. Perhaps I should open a new question? – Gabriel Oct 03 '16 at 13:58
  • I've made a separate question [here](http://stackoverflow.com/questions/39852604/save-all-files-before-running-custom-command-in-sublime3). – Gabriel Oct 04 '16 at 12:40