diff --git a/Shell Turtlestein.sublime-settings b/Shell Turtlestein.sublime-settings index 0271659..8ded259 100644 --- a/Shell Turtlestein.sublime-settings +++ b/Shell Turtlestein.sublime-settings @@ -4,6 +4,7 @@ "surround_cmd": ["", ""], "exec_args": {}, "cmd_settings": [], + "active_file_folder": false, // run commands under active file folder "input_widget": { // overridden for silly non-unixy OSes "syntax": "Packages/ShellScript/Shell-Unix-Generic.tmLanguage" diff --git a/shell_turtlestein.py b/shell_turtlestein.py index be24de8..2ef8017 100644 --- a/shell_turtlestein.py +++ b/shell_turtlestein.py @@ -3,10 +3,15 @@ from functools import partial +def settings(): + return sublime.load_settings('Shell Turtlestein.sublime-settings') + + def cwd_for_window(window): """ Return the working directory in which the window's commands should run. + If "active_file_folder" settings is true, return active file folder In the common case when the user has one folder open, return that. Otherwise, return one of the following (in order of preference): 1) One of the open folders, preferring a folder containing the active @@ -15,17 +20,18 @@ def cwd_for_window(window): 3) The user's home directory. """ folders = window.folders() - if len(folders) == 1: + active_view = window.active_view() + active_file_name = active_view.file_name() if active_view else '' + if active_file_name and settings().get('active_file_folder'): + return os.path.dirname(active_file_name) + elif len(folders) == 1: return folders[0] else: - active_view = window.active_view() - active_file_name = active_view.file_name() if active_view else None - if not active_file_name: - return folders[0] if len(folders) else os.path.expanduser("~") for folder in folders: if active_file_name.startswith(folder): return folder - return os.path.dirname(active_file_name) + default = folders and folder[0] or os.path.dirname(active_file_name) + return default or os.path.expanduser("~") def abbreviate_user(path): @@ -39,10 +45,6 @@ def abbreviate_user(path): return path -def settings(): - return sublime.load_settings('Shell Turtlestein.sublime-settings') - - def cmd_settings(cmd): """ Return the default settings with settings for the command merged in @@ -88,6 +90,7 @@ def run_cmd(cwd, cmd, wait, input_str=None): subprocess.Popen(cmd, cwd=cwd, shell=shell) return (False, None) + def show_in_output_panel(message): window = sublime.active_window() panel_name = 'shell_turtlestein' @@ -97,6 +100,7 @@ def show_in_output_panel(message): panel.end_edit(edit) window.run_command('show_panel', {'panel': 'output.' + panel_name}) + class ShellPromptCommand(sublime_plugin.WindowCommand): """ Prompt the user for a shell command to run in the window's directory @@ -118,7 +122,6 @@ def run(self, **args): for (setting, value) in list(settings().get('input_widget').items()): inputview.settings().set(setting, value) - def on_done(self, cwd, cmd_str): cmd = parse_cmd(cmd_str) if not cmd['input'] and cmd['output'] == '|': @@ -194,6 +197,7 @@ def run(self, edit, region_start=None, region_end=None, text=None): else: self.view.insert(edit, 0, text) + class SubprocessInCwdCommand(sublime_plugin.WindowCommand): """ Launch a subprocess using the window's working directory @@ -212,6 +216,7 @@ def run(self, cmd=None, wait=False): active_input_row = -1 + def callback_with_history(callback, cmd_history, input_text): if callback: cmd = input_text.split("\n")[active_input_row] @@ -220,6 +225,7 @@ def callback_with_history(callback, cmd_history, input_text): cmd_history.append(cmd) return callback(cmd) + def show_input_panel_with_readline(window, caption, cmd_history, on_done, on_change, on_cancel): global active_input_row @@ -231,6 +237,7 @@ def show_input_panel_with_readline(window, caption, cmd_history, view.show(view.size()) return view + class ReadlineHistoryChange(sublime_plugin.TextCommand): def run_(self, someIntNotUsed, args): # Override default run_ so that an edit isn't created. @@ -244,6 +251,7 @@ def run(self, movement, movement_args): global active_input_row active_input_row, _ = self.view.rowcol(self.view.sel()[0].b) + class LeftDeleteOnLine(sublime_plugin.TextCommand): def run(self, edit): if self.view.rowcol(self.view.sel()[0].b)[1]: