diff --git a/Origami.sublime-settings b/Origami.sublime-settings index 269b256..aa71849 100644 --- a/Origami.sublime-settings +++ b/Origami.sublime-settings @@ -1,4 +1,22 @@ { + /** + * The "syntax_grouping" key determines if you want + * Origami to attempt to group files with the same syntax + * into the same pane. The default if "false". + * + * The "enabled_syntaxes" key holds a list of syntaxes + * that qualify for grouping. Each syntax should be entered as + * a string. A blank list means all syntaxes qualify. If you are + * unsure of the correct syntax name, you can open the Sublime console + * and enter the following command: + * sublime.active_window().active_view().settings().get('syntax') + * The name of the tmLanguage file is what you want. + * Ex// ["json", "python"] + */ + "syntax_grouping": false, + "enabled_syntaxes": [], + + /** * The "saved_layouts" key holds a list of previously * saved layout settings. These can be set using the diff --git a/README.markdown b/README.markdown index 2d06096..53b9c5e 100644 --- a/README.markdown +++ b/README.markdown @@ -42,6 +42,8 @@ You can have Origami automatically zoom the active pane by setting `origami_auto Origami can also automatically close a pane for you once you've closed the last file in it. Just set `origami_auto_close_empty_panes` to true in your user preferences. +Origami can also automatically move files of the same syntax into the same pane group when opening them. Set `syntax_grouping` to `true` in your user or project settings. If you only want specific syntax types to be automatically grouped, you can selectively enable them using the `enabled_syntaxes` list in your user or project settings. (If you don't know the syntax name to use, you can open the Sublime console with CTRL+~ and type `sublime.active_window().active_view().settings().get('syntax')`. Copy the name of the _tmLanguage_ file without the path parameters. + Install ------- diff --git a/origami.py b/origami.py index b802b3d..6767e2a 100644 --- a/origami.py +++ b/origami.py @@ -1,6 +1,7 @@ from __future__ import division import sublime, sublime_plugin import copy +import os from functools import partial XMIN, YMIN, XMAX, YMAX = list(range(4)) @@ -62,6 +63,27 @@ def fixed_set_layout_no_focus_change(window, layout): active_group = window.active_group() window.set_layout(layout) +def get_setting(view, key, default=None): + """ + Get a Sublime Text setting value, starting in the project-specific + settings file, then the user-specific settings file, and finally + the package-specific settings file. Also accepts an optional default. + """ + try: + settings = view.settings() + if settings.has('Origami'): + s = settings.get('Origami').get(key) + if s and len(s) > 0: + return s + else: + pass + else: + pass + except: + pass + global_settings = sublime.load_settings('Origami.sublime-settings') + return global_settings.get(key, default) + class PaneCommand(sublime_plugin.WindowCommand): "Abstract base class for commands." @@ -763,3 +785,29 @@ def on_activated(self, view): self.running = True sublime.set_timeout(lambda: self.delayed_zoom(view, fraction), 0) + +class AutoSwitchPane(sublime_plugin.EventListener): + def get_syntax_name(self, view): + syntax_path = view.settings().get('syntax') + syntax_name = os.path.splitext(os.path.basename(syntax_path))[0].lower() + return syntax_name + + def on_load(self, view): + syntax_grouping = get_setting(view, 'syntax_grouping') + enabled_syntaxes = [s.lower() for s in get_setting(view, 'enabled_syntaxes')] + + if syntax_grouping: + w = sublime.active_window() + view = w.active_view() + views = w.views() + current_syntax = self.get_syntax_name(view) + + if views and (len(enabled_syntaxes) == 0 or current_syntax in enabled_syntaxes): + for v in views: + syntax = self.get_syntax_name(v) + group = w.get_view_index(v)[0] + + if syntax and syntax == current_syntax: + w.set_view_index(view, group, 0) + w.focus_view(view) +