|
5 | 5 | import fnmatch
|
6 | 6 | import re
|
7 | 7 |
|
8 |
| -# on plugin_load read settings |
9 |
| -# on plugin_load create notes directory if it does not exists |
| 8 | +def settings(): |
| 9 | + return sublime.load_settings('Notes.sublime-settings') |
10 | 10 |
|
11 | 11 | class NotesListCommand(sublime_plugin.ApplicationCommand):
|
12 |
| - def __init__(self): |
13 |
| - self.notes_dir = os.path.expanduser("~/Dropbox/Notes/") |
14 | 12 |
|
15 |
| - def find_notes(self, file): |
16 |
| - if fnmatch.fnmatch(file, '*.note'): |
17 |
| - return re.sub('\.note$', '', file) |
| 13 | + def run(self): |
| 14 | + root = settings().get("root") |
| 15 | + window = sublime.active_window() |
| 16 | + self.notes_dir = os.path.expanduser(root) |
| 17 | + self.file_list = self.find_notes() |
| 18 | + window.show_quick_panel([f[0] for f in self.file_list], self.open_note) |
| 19 | + |
| 20 | + def find_notes(self): |
| 21 | + note_files = [] |
| 22 | + for path, subdirs, files in os.walk(self.notes_dir): |
| 23 | + for name in files: |
| 24 | + for ext in settings().get("note_file_extensions"): |
| 25 | + if fnmatch.fnmatch(name, "*." + ext): |
| 26 | + note_files.append( [re.sub('\.' + ext + '$', '', name), os.path.join(path, name)] ) |
| 27 | + return note_files |
18 | 28 |
|
19 | 29 | def open_note(self, index):
|
20 | 30 | if index == -1:
|
21 | 31 | return
|
22 |
| - file = os.path.join(self.notes_dir, self.file_list[index] + ".note") |
| 32 | + file = self.file_list[index][-1] |
23 | 33 | # self.window.run_command("new_pane",{"move": True})
|
24 | 34 | view = sublime.active_window().open_file(file)
|
25 | 35 |
|
26 |
| - def run(self): |
27 |
| - window = sublime.active_window() |
28 |
| - self.file_list = [self.find_notes(file) for file in os.listdir(self.notes_dir)] |
29 |
| - window.show_quick_panel(self.file_list, self.open_note) |
30 |
| - |
31 | 36 |
|
32 | 37 | class NotesNewCommand(sublime_plugin.ApplicationCommand):
|
33 |
| - def __init__(self): |
34 |
| - self.notes_dir = os.path.expanduser("~/Dropbox/Notes/") |
| 38 | + |
| 39 | + def run(self, title=None): |
| 40 | + root = settings().get("root") |
| 41 | + self.notes_dir = os.path.expanduser(root) |
| 42 | + |
| 43 | + self.window = sublime.active_window() |
| 44 | + if title is None: |
| 45 | + self.window.show_input_panel("Title", "", self.create_note, None, None) |
| 46 | + else: |
| 47 | + self.create_note(title) |
35 | 48 |
|
36 | 49 | def create_note(self, title):
|
37 | 50 | file = os.path.join(self.notes_dir, title + ".note")
|
38 | 51 | if not os.path.exists(file):
|
39 | 52 | open(file, 'w+').close()
|
40 | 53 | view = sublime.active_window().open_file(file)
|
| 54 | + self.insert_title_scheduled = False |
| 55 | + self.insert_title(title, view) |
41 | 56 |
|
42 |
| - def run(self, title=None): |
43 |
| - self.window = sublime.active_window() |
44 |
| - if title is None: |
45 |
| - self.window.show_input_panel("Title", "", self.create_note, None, None) |
| 57 | + def insert_title(self, title, view): |
| 58 | + if view.is_loading(): |
| 59 | + if not self.insert_title_scheduled: |
| 60 | + self.insert_title_scheduled = True |
| 61 | + sublime.set_timeout(lambda: self.insert_title(title, view), 100) |
| 62 | + return |
46 | 63 | else:
|
47 |
| - self.create_note(title) |
| 64 | + view.run_command("note_insert_title", {"title": title}) |
| 65 | + |
| 66 | + |
| 67 | +class NoteInsertTitleCommand(sublime_plugin.TextCommand): |
| 68 | + def run(self, edit, **kwargs): |
| 69 | + header = "# " + kwargs["title"].capitalize() + "\n" |
| 70 | + self.view.insert(edit, 0, header) |
48 | 71 |
|
| 72 | +def plugin_loaded(): |
| 73 | + # creating root if it does not exist |
| 74 | + root = settings().get("root") |
| 75 | + if not os.path.exists(root): |
| 76 | + os.makedirs(root) |
0 commit comments