From 9a8ac3886ef1e0204575be88c645bb2b6ffd874a Mon Sep 17 00:00:00 2001 From: Mathieu PATUREL Date: Wed, 13 Nov 2019 13:55:16 +1100 Subject: [PATCH] Open current markdown file in a new window It works for saved and unsaved files. Maybe unsaved file's content should be written to a temporary file in case we crash, so that the user doesn't lose all it's content. --- MarkdownLivePreview.py | 64 ++++++++++++++++++++++++++++ MarkdownLivePreview.sublime-commands | 7 +++ utils.py | 4 ++ 3 files changed, 75 insertions(+) create mode 100644 MarkdownLivePreview.py create mode 100644 MarkdownLivePreview.sublime-commands create mode 100644 utils.py diff --git a/MarkdownLivePreview.py b/MarkdownLivePreview.py new file mode 100644 index 0000000..1e28f1c --- /dev/null +++ b/MarkdownLivePreview.py @@ -0,0 +1,64 @@ +import sublime +import sublime_plugin + +from .utils import * + +def plugin_loaded(): + pass + +class MdlpInsertCommand(sublime_plugin.TextCommand): + + def run(self, edit, point, string): + self.view.insert(edit, point, string) + +class OpenMarkdownPreviewCommand(sublime_plugin.TextCommand): + + def run(self, edit): + + """ If the file is saved exists on disk, we close it, and reopen it in a new + window. Otherwise, we copy the content, erase it all (to close the file without + a dialog) and re-insert it into a new view into a new window """ + + original_view = self.view + file_name = original_view.file_name() + + syntax_file = original_view.settings().get('syntax') + + if file_name is None: + + # the file isn't saved, we need to restore the content manually + total_region = sublime.Region(0, original_view.size()) + content = original_view.substr(total_region) + original_view.erase(edit, total_region) + original_view.close() + + # FIXME: save the document to a temporary file, so that if we crash, + # the user doesn't lose what he wrote + + else: + original_view.close() + + sublime.run_command('new_window') + window = sublime.active_window() + + window.run_command('set_layout', { + 'cols': [0.0, 0.5, 1.0], + 'rows': [0.0, 1.0], + 'cells': [[0, 0, 1, 1], [1, 0, 2, 1]] + }) + + window.focus_group(0) + if file_name: + new_view = window.open_file(file_name) + else: + new_view = window.new_file() + new_view.run_command('mdlp_insert', {'point': 0, 'string': content}) + + new_view.set_syntax_file(syntax_file) + + def is_enabled(self): + # FIXME: is this the best way there is to check if the current syntax is markdown? + # should we only support default markdown? + # what about "md"? + return 'markdown' in self.view.settings().get('syntax').lower() + diff --git a/MarkdownLivePreview.sublime-commands b/MarkdownLivePreview.sublime-commands new file mode 100644 index 0000000..cdca348 --- /dev/null +++ b/MarkdownLivePreview.sublime-commands @@ -0,0 +1,7 @@ +[ + + { + "caption": "MarkdownLivePreview: Open Preview", + "command": "open_markdown_preview" + } +] \ No newline at end of file diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..e4339f7 --- /dev/null +++ b/utils.py @@ -0,0 +1,4 @@ +import sublime + +def get_settings(): + return sublime.get_settings("MarkdownLivePreview.sublime-settings") \ No newline at end of file