-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnanote.py
189 lines (162 loc) · 8.29 KB
/
nanote.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import curses
import re
from editor import Editor
nonwords = ' .,;-_'
def main():
running = True
import settings
default_note = settings.args['default_note']
editor = Editor(default_note)
end_state = None
while running:
try:
editor.draw_screen()
cy, cx = editor.cursor
c = editor.screen.getch()
handled_key = False
for key, shortcut in settings.shortcuts:
if c == ord(key)-64:
if shortcut == 'quit':
if editor.altered:
result = editor.dialog('Save before quitting? (Y or N)', yesno=True)
if result is None:
pass
elif result:
editor.save_note(editor.current_note)
running = False
else: running = False
else: running = False
elif shortcut == 'save':
editor.save_note(editor.current_note)
if editor.current_note == '**settings**':
settings = reload(settings)
elif shortcut == 'goto note':
result = editor.dialog('Enter the name of the note to load (^C to cancel):')
if result:
editor.load_note(result)
elif shortcut == 'forward':
editor.forward()
elif shortcut == 'back':
editor.back()
elif shortcut == 'new note':
editor.load_note(None)
elif shortcut == 'settings':
editor.load_note('**settings**')
elif shortcut == 'find':
result = editor.dialog('Enter regex to search (^C to cancel):', editor.last_search)
if result:
editor.find_next(result)
elif shortcut == 'paste':
if editor.cuts:
editor.alter()
if cy == len(editor.buffer): editor.buffer.append('')
editor.buffer = editor.buffer[:cy] + editor.cuts + editor.buffer[cy:]
editor.correct_cursor(cy+len(editor.cuts), cx)
editor.cutting = False
elif shortcut == 'cut':
if cy < len(editor.buffer):
editor.alter()
if editor.cutting:
editor.cuts += [editor.buffer[cy]]
else:
editor.cuts = [editor.buffer[cy]]
editor.buffer = editor.buffer[:cy] + (editor.buffer[cy+1:] if cy < len(editor.buffer)-1 else [])
editor.correct_cursor(cy, 0)
editor.cutting = True
handled_key = True
if not handled_key:
if c == curses.KEY_UP:
editor.correct_cursor(cy-1, min(cx, len(editor.buffer[cy-1]) if 0 <= cy-1 < len(editor.buffer) else 0))
editor.cutting = False
elif c == curses.KEY_DOWN:
editor.correct_cursor(cy+1, min(cx, len(editor.buffer[cy+1]) if 0 < cy+1 < len(editor.buffer) else 0))
editor.cutting = False
elif c == curses.KEY_LEFT:
editor.correct_cursor(cy, cx-1)
elif c == curses.KEY_RIGHT:
editor.correct_cursor(cy, cx+1)
elif c == curses.KEY_LEFT + 279:
# ctrl left
first = True
while first or not (cx==0 or (cy < len(editor.buffer) and len(editor.buffer[cy])>0 and editor.buffer[cy][cx-1] in nonwords
and (cx >= len(editor.buffer[cy]) or not editor.buffer[cy][cx] in nonwords))):
first = False
editor.correct_cursor(cy, cx-1)
cy, cx = editor.cursor
elif c == curses.KEY_RIGHT + 293:
# ctrl right
first = True
while first or not (cx==0 or (cy < len(editor.buffer) and len(editor.buffer[cy])>0 and editor.buffer[cy][cx-1] in nonwords
and (cx >= len(editor.buffer[cy]) or not editor.buffer[cy][cx] in nonwords))):
first = False
editor.correct_cursor(cy, cx+1)
cy, cx = editor.cursor
elif c == ord('\n'):
follow_link = False
for pos, text in editor.links:
if pos <= cx < pos+len(text):
follow_link = text[2:-2]
current_note = follow_link
editor.load_note(current_note)
if not follow_link:
editor.alter()
if cy == len(editor.buffer):
editor.buffer.append('')
else:
editor.buffer = (editor.buffer[:cy] + [editor.buffer[cy][:cx]] +
[editor.buffer[cy][cx:]] + editor.buffer[cy+1:])
editor.correct_cursor(cy+1, 0)
elif c == curses.KEY_BACKSPACE:
editor.correct_cursor(cy, cx-1)
editor.del_char(cy, cx-1)
elif c == curses.KEY_DC:
editor.alter()
editor.del_char(cy, cx)
elif c == ord('\t'):
# tab
tab = ' '*settings.args['tab_width']
if cy == len(editor.buffer):
editor.buffer.append(tab)
editor.alter()
else:
editor.buffer[cy] = tab + editor.buffer[cy]
editor.alter()
editor.correct_cursor(cy, cx+len(tab))
elif c == 353:
# shift+tab
tab = ' '*settings.args['tab_width']
if cy < len(editor.buffer) and editor.buffer[cy].startswith(tab):
editor.buffer[cy] = editor.buffer[cy][len(tab):]
editor.correct_cursor(cy, cx-len(tab))
elif c == curses.KEY_HOME:
if cx == 0 and cy < len(editor.buffer) and editor.buffer[cy]:
stripped = editor.buffer[cy].lstrip(' ')
cx = len(editor.buffer[cy]) - len(stripped)
editor.correct_cursor(cy, cx)
else:
editor.correct_cursor(cy, 0)
elif c == curses.KEY_END:
if cy < len(editor.buffer):
editor.correct_cursor(cy, len(editor.buffer[cy]))
elif c == curses.KEY_PPAGE:
editor.correct_cursor(cy-editor.height, 0)
elif c == curses.KEY_NPAGE:
editor.correct_cursor(cy+editor.height, 0)
# TODO: c<255? not all of those are good characters
elif 0 < c < 255:
editor.alter()
if cy > len(editor.buffer)-1: editor.buffer += ['']
editor.buffer[cy] = editor.buffer[cy][:cx] + chr(c) + editor.buffer[cy][cx:]
editor.correct_cursor(cy, cx+1)
#editor.status = str(c)
except KeyboardInterrupt:
running = False
except Exception as e:
#raise
editor.status = 'exception: %s' % e
end_state = editor.status
#running = False
editor.end_app()
if end_state: print end_state
if __name__ == '__main__':
main()