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.
This commit is contained in:
Mathieu PATUREL
2019-11-13 13:55:16 +11:00
commit 9a8ac3886e
3 changed files with 75 additions and 0 deletions

64
MarkdownLivePreview.py Normal file
View File

@ -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()

View File

@ -0,0 +1,7 @@
[
{
"caption": "MarkdownLivePreview: Open Preview",
"command": "open_markdown_preview"
}
]

4
utils.py Normal file
View File

@ -0,0 +1,4 @@
import sublime
def get_settings():
return sublime.get_settings("MarkdownLivePreview.sublime-settings")