Sublime Forum

Macros with window-level actions

#1

I’m trying to achieve something really simple… i need to bind two actions to a single key in ST3, the first one is to save the current buffer, and the second one is to execute a bit more complex transfer-to-REPL command (which can be packed into a macro for simpler handling).

My problem is that macros can’t execute window-level commands, so i can’t save the file from inside the macro, and using a plugin (like Chain of Command) i can save it, but can’t pass arguments to the commands in the “chain”, so i can’t execute the REPL-transfer directly or from another macro.

I’m shocked that something so simple can be achieved, so, if someone knows a way to do it, it’ll be really appreciated :slight_smile:

1 Like

#2

You can write a couple of lines of Python to accomplish this:

import sublime_plugin

class MyComboCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.window.run_command("command_1_name")
        self.window.run_command("command_2_name")

Then bind a key binding or command palette entry to "my_combo".

2 Likes

#3

I have this laying around:

import sublime_plugin

class RunCommandsCommand(sublime_plugin.TextCommand):
    def run(self, edit, commands):
        for command in commands:
            self.view.run_command(command["command"], command["args"])

I use it instead of macros for TextCommands:

{
        "keys": ["ctrl+left"],
        "command": "run_commands",
        "args": {
            "commands": [
                {
                    "command": "move",
                    "args": {
                        "by": "stops",
                        "forward": false,
                        "word_end": true
                    }
                },
                {
                    "command": "expand_selection",
                    "args": {
                        "to": "word"
                    }
                }
            ]
        },
        "context": [
            { "key": "selection_empty", "operator": "equal", "operand": false }
        ]
    },

All we need to do to make it fully generic is determine whether a particular command (by name) is a TextCommand, WindowCommand, or ApplicationCommand. Is there a sane way to do that?

1 Like

#4

Maybe? There are these undocumented lists:

sublime_plugin.application_command_classes
sublime_plugin.window_command_classes
sublime_plugin.text_command_classes

Then there are also built-in commands that don’t show up anywhere in sublime_plugin, so you’ll have to hard-code a list for that.

0 Likes