-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateSavePrompt.py
52 lines (40 loc) · 1.58 KB
/
CreateSavePrompt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sublime, sublime_plugin
import os
class CreateSavePromptCommand(sublime_plugin.TextCommand):
def onFileEntered(self, location):
if os.path.isdir(location):
sublime.error_message("CreateSavePrompt doesnt save directories. Yet.")
else:
if os.path.lexists(location):
overwrite = sublime.ok_cancel_dialog("Destination exists. Overwrite?", "Overwrite")
if overwrite:
try:
from send2trash import send2trash # ST2
except:
from .send2trash import send2trash # ST3
send2trash(location)
else:
return
with open(location, 'w', encoding='utf8') as f:
f.write(self.view.substr(sublime.Region(0, self.view.size())))
self.view.set_scratch(True)
self.view.window().run_command('close')
self.window().open_file(location)
self.window().run_command('hide_panel')
def run(self, edit):
if self.view.file_name():
# file exists, simply save it
self.view.run_command("save")
else:
self.s = sublime.load_settings("CreateSavePrompt.sublime-settings")
use_first_line_as_file = self.s.get("use_first_line_as_file")
folders = sublime.active_window().folders()
if len(folders) == 0:
home_dir = os.path.expanduser("~")
else:
home_dir = folders[0]
if use_first_line_as_file:
home_dir = os.path.join(home_dir, self.view.substr(self.view.line(0)))
self.window().show_input_panel("File Location:", home_dir, self.onFileEntered, None, None)
def window(self):
return sublime.active_window()