|
import sublime, sublime_plugin |
|
|
|
# Takes an array of commands (same as those you'd provide to a key binding) with |
|
# an optional condition, context (defaults to view commands) & runs each command in order. |
|
# Valid condition is 'selected_text'. |
|
# Valid contexts are 'text', 'window', and 'app' for running a TextCommand, |
|
# WindowCommands, or ApplicationCommand respectively. |
|
class RunMultipleCommandsCommand(sublime_plugin.TextCommand): |
|
def exec_command(self, command): |
|
if not 'command' in command: |
|
raise Exception('No command name provided.') |
|
|
|
# Check that the condition for this command is met. |
|
if 'condition' in command: |
|
condition = command['condition'] |
|
if condition == 'selected_text': |
|
selected_text = False |
|
for r in self.view.sel(): |
|
if not r.empty(): |
|
selected_text = True |
|
if selected_text == False: |
|
return |
|
|
|
args = None |
|
if 'args' in command: |
|
args = command['args'] |
|
|
|
# default context is the view since it's easiest to get the other contexts |
|
# from the view |
|
context = self.view |
|
if 'context' in command: |
|
context_name = command['context'] |
|
if context_name == 'window': |
|
context = context.window() |
|
elif context_name == 'app': |
|
context = sublime |
|
elif context_name == 'text': |
|
pass |
|
else: |
|
raise Exception('Invalid command context "'+context_name+'".') |
|
|
|
# skip args if not needed |
|
if args is None: |
|
context.run_command(command['command']) |
|
else: |
|
context.run_command(command['command'], args) |
|
|
|
def run(self, edit, commands = None): |
|
if commands is None: |
|
return # not an error |
|
for command in commands: |
|
self.exec_command(command) |
In case it's not clear, the problem with the
"find_selected_text"
setting is that it works when your text is on one line, but doesn't work when the text happens to span more than one line.That inconsistency in behavior is absolutely awful and breaks my flow. Sometimes I press Cmd+F and it does what I want, and sometimes it does something else, and I have to stop and think about what just happened... If it's not consistent, I can't rely on it.