forked from aziz/PlainNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.py
161 lines (131 loc) · 5.27 KB
/
notes.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import sublime, sublime_plugin
import os, fnmatch, re
from gzip import GzipFile
from pickle import load, dump
ST3 = int(sublime.version()) >= 3000
def settings():
return sublime.load_settings('Notes.sublime-settings')
def file_id(path):
return os.path.relpath(path, root)
class NotesListCommand(sublime_plugin.ApplicationCommand):
def run(self):
root = os.path.normpath(os.path.expanduser(settings().get("root")))
window = sublime.active_window()
self.notes_dir = os.path.expanduser(root)
self.file_list = self.find_notes(root)
window.show_quick_panel([f[0] for f in self.file_list], self.open_note)
def find_notes(self, root):
note_files = []
for path, subdirs, files in os.walk(self.notes_dir, topdown=False):
relpath = os.path.relpath(path, root)
print(relpath)
for name in files:
for ext in settings().get("note_file_extensions"):
if (relpath != ".archive" and relpath != ".brain") and fnmatch.fnmatch(name, "*." + ext):
note_files.append((re.sub('\.' + ext + '$', '', name),
os.path.join(path, name),
os.path.getmtime(os.path.join(path, name))
))
note_files.sort(key=lambda item: item[2], reverse=True)
return note_files
def open_note(self, index):
if index == -1:
return
file_path = self.file_list[index][1]
def open_and_activate():
view = sublime.active_window().open_file(file_path, sublime.ENCODED_POSITION)
f_id = file_id(file_path)
if db.get(f_id) and db[f_id]["color_scheme"]:
view.settings().set("color_scheme", db[f_id]["color_scheme"])
view.settings().set("is_note", True)
sublime.set_timeout(open_and_activate, 0)
class NotesNewCommand(sublime_plugin.ApplicationCommand):
def run(self, title=None):
root = settings().get("root")
self.notes_dir = os.path.expanduser(root)
self.window = sublime.active_window()
if title is None:
self.window.show_input_panel("Title", "", self.create_note, None, None)
else:
self.create_note(title)
def create_note(self, title):
file = os.path.join(self.notes_dir, title + ".note")
if not os.path.exists(file):
open(file, 'w+').close()
view = sublime.active_window().open_file(file)
self.insert_title_scheduled = False
self.insert_title(title, view)
def insert_title(self, title, view):
if view.is_loading():
if not self.insert_title_scheduled:
self.insert_title_scheduled = True
sublime.set_timeout(lambda: self.insert_title(title, view), 100)
return
else:
view.run_command("note_insert_title", {"title": title})
class NotesEvents(sublime_plugin.EventListener):
def on_load_async(self, view):
if view.settings().get("is_note") or not view.file_name():
return
if os.path.realpath(view.file_name()).startswith(root):
f_id = file_id(view.file_name())
if db.get(f_id) and db[f_id]["color_scheme"]:
view.settings().set("color_scheme", db[f_id]["color_scheme"])
view.settings().set("is_note", True)
class NoteInsertTitleCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
header = "# " + kwargs["title"].capitalize() + "\n"
self.view.insert(edit, 0, header)
class NoteChangeColorCommand(sublime_plugin.WindowCommand):
def run(self):
self.colors = ["Orange", "Yellow", "Green", "GreenLight", "Blue", "BlueLight", "Purple", "Pink", "Gray", "White"]
self.window = sublime.active_window()
self.original_cs = self.window.active_view().settings().get("color_scheme")
current_color = os.path.basename(self.original_cs).replace("Sticky-","").replace(".tmTheme", "")
# show_quick_panel(items, on_done, <flags>, <selected_index>, <on_highlighted>)
self.window.show_quick_panel(self.colors, self.on_select, 0, self.colors.index(current_color), self.on_highlight)
def on_select(self, index):
global db
if index == -1:
self.window.active_view().settings().set("color_scheme", self.original_cs)
else:
path = os.path.join("Packages" , "PlainNotes", "Color Schemes", "Sticky-" + self.colors[index] + ".tmTheme")
view = self.window.active_view()
view.settings().set("color_scheme", path)
f_id = file_id(view.file_name())
if not db.get(f_id):
db[f_id] = {}
db[f_id]["color_scheme"] = path
save_to_brain()
def on_highlight(self, index):
path = os.path.join("Packages" , "PlainNotes", "Color Schemes", "Sticky-" + self.colors[index] + ".tmTheme")
self.window.active_view().settings().set("color_scheme", path)
def is_enabled(self):
return self.window.active_view().settings().get("syntax").endswith("Note.tmLanguage")
def save_to_brain():
print("SAVING TO DISK-----------------")
print(db)
gz = GzipFile(db_file, 'wb')
dump(db, gz, -1)
gz.close()
def plugin_loaded():
global db, root, db_file
# creating root if it does not exist
root = os.path.normpath(os.path.expanduser(settings().get("root")))
if not os.path.exists(root):
os.makedirs(root)
# open db
db = {}
db_file = os.path.join(root, '.brain', 'brain.bin.gz')
try:
os.makedirs(os.path.dirname(db_file))
except:
pass
try:
gz = GzipFile(db_file, 'rb')
db = load(gz)
gz.close()
except:
db = {}
if not ST3:
plugin_loaded()