From 0f36b6d658ad6f24aeb6bd90857b3f5c7ef3f5df Mon Sep 17 00:00:00 2001 From: MendelGusmao Date: Fri, 9 May 2014 19:17:51 -0300 Subject: [PATCH] Add support for finding and grouping installed themes. --- Default (Linux).sublime-keymap | 4 +- Default (OSX).sublime-keymap | 4 +- Default (Windows).sublime-keymap | 4 +- Default.sublime-commands | 14 ++++ QuickThemes.py | 79 ++++++++++++++---- QuickThemes.sublime-settings | 134 ++++++++++++++++--------------- README.md | 6 +- 7 files changed, 162 insertions(+), 83 deletions(-) diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap index d3a1a23..8f5397f 100644 --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -1,4 +1,6 @@ [ { "keys": ["super+alt+0"], "command": "quick_themes", "args": {"action": "inc"}}, - { "keys": ["super+alt+9"], "command": "quick_themes", "args": {"action": "dec"}} + { "keys": ["super+alt+9"], "command": "quick_themes", "args": {"action": "dec"}}, + { "keys": ["super+alt+-"], "command": "quick_themes", "args": {"action": "group"}}, + { "keys": ["super+alt+="], "command": "quick_themes", "args": {"action": "reload"}} ] \ No newline at end of file diff --git a/Default (OSX).sublime-keymap b/Default (OSX).sublime-keymap index d3a1a23..8f5397f 100644 --- a/Default (OSX).sublime-keymap +++ b/Default (OSX).sublime-keymap @@ -1,4 +1,6 @@ [ { "keys": ["super+alt+0"], "command": "quick_themes", "args": {"action": "inc"}}, - { "keys": ["super+alt+9"], "command": "quick_themes", "args": {"action": "dec"}} + { "keys": ["super+alt+9"], "command": "quick_themes", "args": {"action": "dec"}}, + { "keys": ["super+alt+-"], "command": "quick_themes", "args": {"action": "group"}}, + { "keys": ["super+alt+="], "command": "quick_themes", "args": {"action": "reload"}} ] \ No newline at end of file diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap index d3a1a23..8f5397f 100644 --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap @@ -1,4 +1,6 @@ [ { "keys": ["super+alt+0"], "command": "quick_themes", "args": {"action": "inc"}}, - { "keys": ["super+alt+9"], "command": "quick_themes", "args": {"action": "dec"}} + { "keys": ["super+alt+9"], "command": "quick_themes", "args": {"action": "dec"}}, + { "keys": ["super+alt+-"], "command": "quick_themes", "args": {"action": "group"}}, + { "keys": ["super+alt+="], "command": "quick_themes", "args": {"action": "reload"}} ] \ No newline at end of file diff --git a/Default.sublime-commands b/Default.sublime-commands index d1e775a..f40c371 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -12,5 +12,19 @@ { "action": "dec" } + }, + { + "caption": "QuickThemes: change group", + "command": "quick_themes", "args": + { + "action": "group" + } + }, + { + "caption": "QuickThemes: reload themes", + "command": "quick_themes", "args": + { + "action": "reload" + } } ] \ No newline at end of file diff --git a/QuickThemes.py b/QuickThemes.py index e5c258c..5d35edb 100644 --- a/QuickThemes.py +++ b/QuickThemes.py @@ -1,16 +1,27 @@ import sublime import sublime_plugin import re - +import fnmatch +import os class QuickThemesCommand(sublime_plugin.WindowCommand): - def theme_name_status_message(self, full_name): + def theme_name(self, full_name): try: - match = re.search("([^/]+).tmTheme|.sublime-theme$", full_name) - theme_name = match.group(1) + match = re.search("Packages/(.*)/(.*).tmTheme|.sublime-theme$", full_name) + theme_group = match.group(1) + theme_name = match.group(2) if theme_name: - sublime.status_message(theme_name) + return (theme_name, theme_group) + except: + try: + return (full_name.split("/")[-1]) + except: + return full_name + + def theme_name_status_message(self, full_name): + try: + sublime.status_message("%s (%s)" % self.theme_name(full_name)) except: pass @@ -28,48 +39,63 @@ def run(self, action): relevant_settings = {} qt_defaults = qt_settings.get("quick_themes_defaults") # dict - qt_themes = qt_settings.get("quick_themes") # list + qt_group_selection = int(qt_settings.get("quick_themes_group_selection", 0)) # int qt_selection = int(qt_settings.get("quick_themes_selection", 0)) # int + qt_themes = qt_settings.get("quick_themes") # list for option in qt_defaults: relevant_settings[option] = full_settings.get(option) mismatch = self.get_mismatch( - qt_themes[qt_selection], relevant_settings) + qt_themes[qt_group_selection][qt_selection], relevant_settings) if len(mismatch) > 0: """ There is a mismatch between the selected quicktheme and the current base theme settings. Check to see whether any of the other quickthemes match. """ match = False - for theme in qt_themes: + for theme in qt_themes[qt_group_selection]: test = qt_defaults test.update(theme) if self.get_mismatch(test, relevant_settings).len == 0: match = True """ Found a match, so update the selection setting. """ - qt_selection = qt_themes.index(theme) + qt_selection = qt_themes[qt_group_selection].index(theme) break if match is False: """ No match found, so add a new quicktheme to preserve current settings. """ - qt_themes.append(relevant_settings) + qt_themes[qt_group_selection].append(relevant_settings) qt_selection = len(qt_settings["quick_themes"]) - 1 if action == "inc": qt_selection += 1 - if qt_selection > len(qt_themes) - 1: + if qt_selection > len(qt_themes[qt_group_selection]) - 1: qt_selection = 0 - else: + elif action == "dec": qt_selection -= 1 if qt_selection < 0: - qt_selection = len(qt_themes) - 1 + qt_selection = len(qt_themes[qt_group_selection]) - 1 + elif action == "group": + qt_selection = 0 + qt_group_selection += 1 + if qt_group_selection > len(qt_themes) - 1: + qt_group_selection = 0 + elif action == "reload": + qt_themes = self.find_themes() + + try: + test = qt_themes[qt_group_selection][qt_selection] + except: + qt_selection = 0 + qt_group_selection = 0 - writeable_settings = dict(qt_defaults, **qt_themes[qt_selection]) + writeable_settings = dict(qt_defaults, **qt_themes[qt_group_selection][qt_selection]) for option in writeable_settings: full_settings.set(option, writeable_settings[option]) + qt_settings.set("quick_themes_group_selection", int(qt_group_selection)) qt_settings.set("quick_themes_selection", int(qt_selection)) qt_settings.set("quick_themes", qt_themes) @@ -77,3 +103,28 @@ def run(self, action): sublime.save_settings("Base File.sublime-settings") self.theme_name_status_message(writeable_settings["color_scheme"]) + + def find_themes(self): + groups = {} + themes = [] + + for root, dirnames, filenames in os.walk(sublime.packages_path()): + matches = fnmatch.filter(filenames, "*.tmTheme") + + for filename in matches: + theme_path = os.path.join(root, filename) + theme_name = self.theme_name(theme_path) + + if theme_name is None: + continue + + if theme_name[1] not in groups: + groups[theme_name[1]] = [] + + groups[theme_name[1]].append({"color_scheme": theme_path}) + + for group in sorted(groups, lambda a, b: cmp(a.lower(), b.lower())): + groups[group].append({}) + themes.append(groups[group]) + + return themes diff --git a/QuickThemes.sublime-settings b/QuickThemes.sublime-settings index 4b3274d..eef154f 100644 --- a/QuickThemes.sublime-settings +++ b/QuickThemes.sublime-settings @@ -13,6 +13,10 @@ // with two or three themes. // I use a muted, high contrast, and bright theme to match my environments. + + // quick_themes_group_selection: + // The key of the currently selected group. + "quick_themes_group_selection": 0, // quick_themes_selection: // The index of the currently selected theme. @@ -32,69 +36,71 @@ // quick_themes: // List of themes. The empty first theme provides access to defaults. "quick_themes": [ - {}, // use defaults! - { - "color_scheme": "Packages/Color Scheme - Default/Amy.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Blackboard.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Cobalt.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Dawn.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Eiffel.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Espresso Libre.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/IDLE.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/LAZY.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Mac Classic.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/MagicWB (Amiga).tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Monokai Bright.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Pastels on Dark.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Slush & Poppies.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Solarized (Dark).tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Solarized (Light).tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/SpaceCadet.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Sunburst.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Twilight.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/Zenburnesque.tmTheme" - }, - { - "color_scheme": "Packages/Color Scheme - Default/iPlastic.tmTheme" - } + [ + {}, // use defaults! + { + "color_scheme": "Packages/Color Scheme - Default/Amy.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Blackboard.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Cobalt.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Dawn.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Eiffel.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Espresso Libre.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/IDLE.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/LAZY.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Mac Classic.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/MagicWB (Amiga).tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Monokai Bright.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Pastels on Dark.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Slush & Poppies.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Solarized (Dark).tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Solarized (Light).tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/SpaceCadet.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Sunburst.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Twilight.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/Zenburnesque.tmTheme" + }, + { + "color_scheme": "Packages/Color Scheme - Default/iPlastic.tmTheme" + } + ] ] } \ No newline at end of file diff --git a/README.md b/README.md index 08a196b..4b5d090 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ # Info -QuickThemes allows you to easily cycle through any combination of Sublime Text 2 preferences. The obvious use is for changing color schemes, themes, and fonts simultaneously, but any of the ST2 preferences are available. +QuickThemes allows you to easily cycle through any combination of Sublime Text 2 preferences. The obvious use is for changing color schemes, themes, and fonts simultaneously, but any of the ST2 preferences are available. It can search for installed themes and group them by its directories' names. # Usage -Super+Alt+0: Cycle up +Super+Alt+=: Search for installed themes (will overwrite quick_themes) +Super+Alt+-: Change group +Super+Alt+0: Cycle up Super+Alt+9: Cycle down Alternatively, you can switch themes via Super+Shift+P and fuzzy search, e.g. "Super+Shift+P qtn" for next theme, "Super+Shift+P qtp" for previous theme.