From 22ef7519e5fdf725048091df2f33b8b433db1807 Mon Sep 17 00:00:00 2001 From: Felix Hao Date: Thu, 29 Jun 2017 16:10:09 +0800 Subject: [PATCH] add a command to change color scheme --- Default.sublime-commands | 4 ++ Main.sublime-menu | 4 ++ bootstrap.py | 100 +++++++++++++++++++++++++++++++++++++++ messages.json | 10 +++- messages/2.2.3.md | 1 + 5 files changed, 118 insertions(+), 1 deletion(-) diff --git a/Default.sublime-commands b/Default.sublime-commands index a5b694ef..f9f2e5c6 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -140,5 +140,9 @@ { "same_level" : false } + }, + { + "caption": "MarkdownEditing: Change color scheme...", + "command": "mde_color_activate" } ] diff --git a/Main.sublime-menu b/Main.sublime-menu index c6586934..ba27d0e6 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -15,6 +15,10 @@ "caption": "README", "command": "open_file" }, + { + "caption": "Change color scheme...", + "command": "mde_color_activate" + }, { "caption": "-" }, diff --git a/bootstrap.py b/bootstrap.py index 91fbbfa5..f4411593 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -1,4 +1,11 @@ import sys +import sublime +import sublime_plugin +import re +try: + from MarkdownEditing.mdeutils import * +except ImportError: + from mdeutils import * package_name = 'MarkdownEditing' @@ -27,12 +34,99 @@ def enable_native_markdown_package(): save_ingored_packages(ignored_packages) +def choose_color_theme(window): + window = window or sublime.active_window() + view = window.new_file() + view.run_command('append', {'characters': '''# A sample Markdown document + +This is a sample document so you can preview the color themes. + +## I am a second-level header + +Markdown supports _italics_, __bold__, and ___bold italics___ style. + +There are also inline styles like `inline code in monospace font` and ~~strikethrough style~~. __There may be ~~strikethroughed text~~ or `code text` inside bold text.__ _And There may be ~~strikethroughed text~~ or `code text` inside italic text._ + +To reference something from a URL, [Named Links][links] and [Inline links](https://example.com/index.html) are of great help. Sometimes ![A picture][sample image] is worth a thousand words. + +There are two types of lists, numbered and unnumbered. + +1. Item 1 +2. Item 2 +3. Item 3 + +* Item A + - Sub list + + Sub sub list + + Sub sub list 2 + - Sub list 2 +* Item B +* Item C + +## Fenced code + +You can write fenced code inside three backticks. + +```javascript +function fibo(n) { + fibo.mem = fibo.mem || []; // I am some comment + return fibo.mem[n] || fibo.mem[n] = n <= 1 ? 1 : fibo(n - 1) + fibo(n - 2); +} +``` + +## The following section is used to define named links + +[links]: https://example.com/index.html +[sample image]: https://example.com/sample.png +'''}) + view.set_syntax_file('Packages/MarkdownEditing/Markdown.tmLanguage') + default_mde_scheme = sublime.load_settings('Markdown.sublime-settings').get('color_scheme') or 'Packages/MarkdownEditing/MarkdownEditor.tmTheme' + print(default_mde_scheme) + view.settings().set('color_scheme', default_mde_scheme) + view.set_read_only(True) + view.set_scratch(True) + + global_scheme = sublime.load_settings('Preferences.sublime-settings').get('color_scheme') + themes = ['Packages/MarkdownEditing/MarkdownEditor.tmTheme', + 'Packages/MarkdownEditing/MarkdownEditor-Focus.tmTheme', + 'Packages/MarkdownEditing/MarkdownEditor-Yellow.tmTheme', + 'Packages/MarkdownEditing/MarkdownEditor-Dark.tmTheme', + 'Packages/MarkdownEditing/MarkdownEditor-ArcDark.tmTheme', + global_scheme] + + themes_display = [re.search('[^/]+(?=\.tmTheme$)', s).group(0) + (' (Current)' if s == default_mde_scheme else '') + (' (Global)' if s == global_scheme else '') for s in themes] + + def set_scheme(scheme): + view.settings().set('color_scheme', scheme) + sublime.load_settings('Markdown.sublime-settings').set('color_scheme', scheme) + + def on_done(index): + if index == -1: + set_scheme(default_mde_scheme) + elif index == len(themes) - 1: + set_scheme(global_scheme) + else: + set_scheme(themes[index]) + sublime.save_settings('Markdown.sublime-settings') + view.close() + + def on_highlighted(index): + if index == len(themes) - 1: + set_scheme(global_scheme) + else: + set_scheme(themes[index]) + + window.show_quick_panel(themes_display, on_done, flags=sublime.KEEP_OPEN_ON_FOCUS_LOST, on_highlight=on_highlighted) + + def plugin_loaded(): from package_control import events if events.install(package_name): # Native package causes some conflicts. disable_native_markdown_package() + # Prmopts to select a color theme + choose_color_theme() def plugin_unloaded(): @@ -46,3 +140,9 @@ def plugin_unloaded(): if sys.version_info < (3,): plugin_loaded() unload_handler = plugin_unloaded + + +class MdeColorActivateCommand(MDETextCommand): + + def run(self, edit): + choose_color_theme(self.view.window()) diff --git a/messages.json b/messages.json index e1f466f9..f45e2820 100644 --- a/messages.json +++ b/messages.json @@ -15,5 +15,13 @@ "2.1.2": "messages/2.1.2.md", "2.1.3": "messages/2.1.3.md", "2.1.4": "messages/2.1.4.md", - "2.1.5": "messages/2.1.5.md" + "2.1.5": "messages/2.1.5.md", + "2.1.6": "messages/2.1.6.md", + "2.1.7": "messages/2.1.7.md", + "2.1.8": "messages/2.1.8.md", + "2.1.9": "messages/2.1.9.md", + "2.2.0": "messages/2.2.0.md", + "2.2.1": "messages/2.2.1.md", + "2.2.2": "messages/2.2.2.md", + "2.2.3": "messages/2.2.3.md" } diff --git a/messages/2.2.3.md b/messages/2.2.3.md index a3ddab65..360080c2 100644 --- a/messages/2.2.3.md +++ b/messages/2.2.3.md @@ -13,6 +13,7 @@ feedback you can use [GitHub issues][issues]. * You can now disable some of the key bindings through configuration. For example, if you want to disable the "shift+tab" => "fold current section" key binding, add `"mde.keymap_disable.fold_section": true` to your user config. For a full list of such key bindings, check the list below. * Organize References command now sorts references by referencing order rather than alphebetic order. * Code block highlight for clojure. +* You can use `MarkdownEditing: Change color scheme...` command in command palette to preview and change the color scheme you are using for markdown. ### Configurable Key Bindings