/
modebase.py
659 lines (520 loc) · 19.3 KB
/
modebase.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import re
import itertools
import unicodedata
import collections
from kaa import doc_re
import kaa
from kaa import keyboard, editmode
import kaa.log
from kaa import highlight
from kaa import theme
from kaa import screen
from kaa import addon
class SearchOption:
RE = doc_re
def __init__(self):
self.text = ''
self.replace_to = ''
self.ignorecase = True
self.word = False
self.regex = False
def clone(self):
ret = self.__class__()
ret.text = self.text
ret.replace_to = self.replace_to
ret.ignorecase = self.ignorecase
ret.word = self.word
ret.regex = self.regex
return ret
def get_regex(self):
text = self.text
if not self.regex:
text = self.RE.escape(text)
if self.word:
text = r'\b' + text + r'\b'
opt = self.RE.MULTILINE
if self.ignorecase:
opt += self.RE.IGNORECASE
regex = self.RE.compile(text, opt)
return regex
SearchOption.LAST_SEARCH = SearchOption()
class ModeBase:
(UNDO_INSERT,
UNDO_REPLACE,
UNDO_DELETE) = range(3)
CLOSE_ON_DEL_WINDOW = True
SCREEN_NOWRAP = False
NO_WRAPINDENT = False
SCREEN_BUILD_ENTIRE_ROWS = False
SHOW_LINENO = False
SHOW_BLANK_LINE = False
USE_UNDO = True
DOCUMENT_MODE = False
HIGHLIGHT_CURSOR_ROW = False
SHOW_CURSOR = True
tab_width = 8
indent_width = 4
indent_tab = False
auto_indent = True
closed = False
current_lastcommands = None
theme = None
highlight = None
_check_fileupdate = 0
_last_autoindent = None
@classmethod
def update_fileinfo(cls, fileinfo, document=None):
pass
def __init__(self):
self.commands = {}
self.is_availables = {}
self.keybind = keyboard.KeyBind()
self.keybind_vi_commandmode = keyboard.KeyBind()
self.keybind_vi_visualmode = keyboard.KeyBind()
self.keybind_vi_visuallinewisemode = keyboard.KeyBind()
self.init_keybind()
self.init_addon_keys()
self.init_commands()
self.init_addon_commands()
self.init_menu()
self.themes = []
self.init_themes()
self.init_addon_themes()
self._build_theme()
kaa.app.translate_theme(self.theme)
self.tokenizers = []
self.init_tokenizers()
self.stylemap = {}
self.stylenamemap = {}
self.highlight = highlight.Highlighter(self.tokenizers)
self.setup()
def setup(self):
pass
def close(self):
self.document = None
self.closed = True
self.keybind.clear()
self.theme = None
self.commands.clear()
self.commands = None
self.is_availables = None
self.highlight.close()
self.highlight = None
self.tokenizers = None
self.stylemap = None
def _build_style_map(self):
self.stylemap[0] = self.theme.get_style('default')
for tokenid in self.highlight.tokenids:
assert tokenid not in self.stylemap
token = self.highlight.get_token(tokenid)
if token:
stylename = token.get_stylename(tokenid)
style = self.theme.get_style(stylename)
self.stylemap[tokenid] = style
self.stylenamemap[style.name] = tokenid
else:
style = self.theme.get_style('default')
self.stylemap[tokenid] = style
self.stylenamemap[style.name] = tokenid
def on_set_document(self, document):
self.document = document
self.document.styles.setints(0, len(self.document.styles), 0)
self._build_style_map()
self.document.use_undo(self.USE_UNDO)
def on_document_updated(self, pos, inslen, dellen):
self._last_autoindent = None
if self.highlight:
self.highlight.updated(self.document, pos, inslen, dellen)
def on_file_saved(self, fileinfo):
pass
def on_add_window(self, wnd):
self.editmode_insert(wnd)
def on_del_window(self, wnd):
if len(self.document.wnds) == 1:
if self.DOCUMENT_MODE and self.document.fileinfo:
self.document.fileinfo.save_screenloc(wnd.screen.pos)
def on_focus(self, wnd):
kaa.app.show_cursor(self.is_cursor_visible())
# relocate cursor
wnd.cursor.setpos(wnd.cursor.pos)
def register_keys(self, keybind, keys):
for d in keys:
keybind.add_keybind(d)
def init_keybind(self):
pass
def get_class_name(self):
modulename = self.__class__.__module__
name = '.'.join((modulename, self.__class__.__name__))
return name
def init_addon_keys(self):
name = self.get_class_name()
keydef = addon.get_addon(name, 'keybind')
for editmode, keys in keydef:
if editmode == 'input':
self.keybind.add_keybind(keys)
elif editmode == 'command':
self.keybind_vi_commandmode.add_keybind(keys)
elif editmode == 'visual':
self.keybind_vi_visualmode.add_keybind(keys)
elif editmode == 'visualline':
self.keybind_vi_visuallinewisemode.add_keybind(keys)
def init_commands(self):
from kaa.commands import (editorcommand, editmodecommand, vicommand)
self.register_commandobj(editorcommand.CursorCommands())
self.register_commandobj(editorcommand.EditCommands())
self.register_commandobj(editorcommand.CodeCommands())
self.register_commandobj(editorcommand.SelectionCommands())
self.register_commandobj(editorcommand.SearchCommands())
self.register_commandobj(editmodecommand.EditModeCommands())
self.register_commandobj(vicommand.ViCommands())
for name in dir(self):
attr = getattr(self, name)
if hasattr(attr, 'COMMAND_ID') and callable(attr):
self.commands[getattr(attr, 'COMMAND_ID')] = attr
def init_addon_commands(self):
name = self.get_class_name()
commands = addon.get_addon(name, 'commands')
for commandid, f in commands:
self.commands[commandid] = f
def init_menu(self):
pass
def init_themes(self):
pass
def init_addon_themes(self):
name = self.get_class_name()
themes = addon.get_addon(name, 'themes')
self.themes.extend(themes)
def init_tokenizers(self):
pass
def register_commandobj(self, cmds):
self.commands.update(cmds.get_commands())
self.is_availables.update(cmds.get_commands_is_enable())
def get_command(self, commandid):
ret = kaa.app.get_command(commandid)
if ret:
return ret
is_available = self.is_availables.get(commandid, None)
cmd = self.commands.get(commandid, None)
return (is_available, cmd)
def editmode_insert(self, wnd):
wnd.set_editmode(editmode.EditMode())
def editmode_replace(self, wnd):
wnd.set_editmode(editmode.ReplaceMode())
def editmode_visual(self, wnd):
wnd.set_editmode(editmode.VisualMode())
def editmode_visual_linewise(self, wnd):
wnd.set_editmode(editmode.VisualLinewiseMode())
def editmode_command(self, wnd):
wnd.set_editmode(editmode.CommandMode())
def get_styleid(self, stylename):
if stylename == 'default':
return 0
styleid = self.stylenamemap.get(stylename, None)
if styleid is not None:
return styleid
if not self.stylemap:
ret = 1
else:
ret = max(self.stylemap.keys()) + 1
style = self.theme.get_style(stylename)
self.stylemap[ret] = style
self.stylenamemap[stylename] = ret
return ret
def select_theme(self, theme_name, themes):
theme = themes.get(theme_name, None)
if theme is None:
theme = themes[kaa.app.DEFAULT_THEME]
return theme
def _build_theme(self):
theme_name = kaa.app.get_current_theme()
self.theme = theme.Theme([])
for t in self.themes:
self.theme.update(self.select_theme(theme_name, t))
self.theme.finish_update()
def get_style(self, tokenid):
ret = self.stylemap.get(tokenid, None)
if ret is None:
ret = self.theme.get_style('default')
return ret
def get_menu(self, itemname):
return self.menu.get(itemname, None)
def is_cursor_visible(self):
return self.SHOW_CURSOR
def on_keypressed(self, wnd, event, s, commands, candidate):
return s, commands, candidate
def filter_string(self, wnd, s):
return s
def on_edited(self, wnd):
pass
def on_str(self, wnd, s, overwrite=False):
self.put_string(wnd, s, overwrite=overwrite)
wnd.screen.selection.clear()
if kaa.app.macro.is_recording():
kaa.app.macro.record_string(s)
if self.highlight:
# run highlighter a bit to display changes immediately.
self.highlight.update_style(self.document, batch=50)
def add_lastcommands(self, commandids):
if self.current_lastcommands is not None:
self.current_lastcommands.extend(commandids)
def on_commands(self, wnd, commandids):
try:
if callable(commandids):
commandids(wnd)
return
self.current_lastcommands = []
for commandid in commandids:
is_available, command = self.get_command(commandid)
if not command:
msg = 'command {!r} is not registered.'.format(commandid)
kaa.app.messagebar.set_message(msg)
kaa.log.error(msg)
return
command(wnd)
if kaa.app.macro.is_recording():
kaa.app.macro.record(command)
if not getattr(command, 'NORERUN', False):
self.current_lastcommands.append(commandid)
if self.current_lastcommands:
kaa.app.lastcommands = self.current_lastcommands
finally:
wnd.editmode.clear_repeat()
self.current_lastcommands = None
def on_esc_pressed(self, wnd, event):
pass
def on_cursor_located(self, wnd, pos, y, x):
pass
def insert_string(self, wnd, pos, s, update_cursor=True):
"""Insert string"""
cur_pos = wnd.cursor.pos
wnd.document.insert(pos, s)
if update_cursor:
wnd.cursor.setpos(wnd.cursor.pos + len(s))
wnd.cursor.savecol()
if wnd.document.undo:
wnd.document.undo.add(self.UNDO_INSERT, pos, s,
cur_pos, wnd.cursor.pos)
self.on_edited(wnd)
def replace_string(self, wnd, pos, posto, s, update_cursor=True):
"""Replace string"""
cur_pos = wnd.cursor.pos
deled = wnd.document.gettext(pos, posto)
wnd.document.replace(pos, posto, s)
if update_cursor:
wnd.cursor.setpos(pos + len(s))
wnd.cursor.savecol()
if wnd.document.undo:
wnd.document.undo.add(self.UNDO_REPLACE, pos, posto, s,
deled, cur_pos, wnd.cursor.pos)
self.on_edited(wnd)
def delete_string(self, wnd, pos, posto, update_cursor=True):
"""Delete string"""
cur_pos = wnd.cursor.pos
if pos < posto:
deled = wnd.document.gettext(pos, posto)
wnd.document.delete(pos, posto)
if update_cursor:
wnd.cursor.setpos(pos)
wnd.cursor.savecol()
if wnd.document.undo:
wnd.document.undo.add(self.UNDO_DELETE, pos, posto, deled,
cur_pos, wnd.cursor.pos)
self.on_edited(wnd)
def replace_rect(self, wnd, repto):
with wnd.document.undo_group():
(posfrom, posto, colfrom, colto
) = wnd.screen.selection.get_rect_range()
for s in repto:
if posto <= posfrom:
break
sel = wnd.screen.selection.get_col_string(
posfrom, colfrom, colto)
if sel:
f, t, org = sel
if org.endswith('\n'):
t = max(f, t - 1)
self.replace_string(wnd, f, t, s)
posto += (len(s) - (t - f))
posfrom = wnd.document.geteol(posfrom)
def put_string(self, wnd, s, overwrite=False):
s = self.filter_string(wnd, s)
if wnd.screen.selection.is_selected():
if wnd.screen.selection.is_rectangular():
if '\n' not in s:
self.replace_rect(wnd, itertools.repeat(s))
else:
self.replace_rect(wnd, s.split('\n'))
else:
sel = wnd.screen.selection.get_selrange()
f, t = sel
self.replace_string(wnd, f, t, s)
else:
if not overwrite:
self.insert_string(wnd, wnd.cursor.pos, s)
else:
eol = wnd.document.get_line_to(wnd.cursor.pos)
posto = min(wnd.cursor.pos+len(s), eol)
self.replace_string(wnd, wnd.cursor.pos, posto, s)
def update_charattr(self, wnd):
if wnd.charattrs:
wnd.charattrs.clear()
wnd.screen.style_updated()
return True
def on_idle(self):
if self.closed:
return
ret = self.run_highlight()
return ret
def get_line_overlays(self):
return {}
HIGHLIGHTBATCH = 300
def run_highlight(self):
if self.highlight:
return self.highlight.update_style(
self.document,
batch=self.HIGHLIGHTBATCH)
def _split_chars(self, begin, end):
"""split characters by character category."""
s = self.document.gettext(begin, end)
for key, chars in itertools.groupby(s, unicodedata.category):
chars = ''.join(chars)
end = begin + len(chars)
yield begin, end, chars, key
begin = end
RE_WORDCHAR = r"(?P<L_WORDCHAR>[a-zA-Z0-9_]+)"
RE_WHITESPACE = r"(?P<Z_WHITESPACE>[\t ]+)"
RE_LF = r"(?P<_LF>\n)"
RE_HIRAGANA = r"(?P<L_HIRAGANA>[\u3040-\u309f\u30fc]+)"
RE_KATAKANA = r"(?P<L_KATAKANA>[\u30a0-\u30ff\u30fc]+)"
RE_SPLITWORD = doc_re.compile('|'.join((
RE_WORDCHAR, RE_WHITESPACE, RE_LF, RE_HIRAGANA, RE_KATAKANA)))
def split_word(self, begin, all=False):
"""yield word in the document until line ends"""
for m in self.RE_SPLITWORD.finditer(self.document, pos=begin):
# split unmatched characters by character category.
f, t = m.span()
if f != begin:
yield from self._split_chars(begin, f)
begin = t
yield (f, t, m.group(), m.lastgroup)
# finish if we reach '\n'
if not all and m.lastgroup == '_LF':
return
yield from self._split_chars(begin, self.document.endpos())
def get_word_at(self, pos):
tol = self.document.gettol(pos)
ret = None
for f, t, s, cg in self.split_word(tol):
if t <= pos:
if cg != 'Z_WHITESPACE':
if t == pos and cg[0] == 'L':
return (f, t, cg)
else:
ret = (f, t, cg)
elif cg == '_LF':
break
else:
if cg == 'Z_WHITESPACE':
if f == pos:
return ret
else:
return
return (f, t, cg)
if ret and ret[1] == pos:
return ret
def get_word_list(self):
words = collections.OrderedDict()
for f, t, s, cg in self.split_word(0, all=True):
if cg in {'Z_WHITESPACE', '_LF'}:
continue
if cg[0] in 'LMN': # Letter, Mark, Number
if len(s) > 2:
words[s] = None
return list(words.keys())
def search_next(self, pos, searchinfo):
regex = searchinfo.get_regex()
pos = min(max(0, pos), self.document.endpos())
m = regex.search(self.document, pos)
return m
def search_prev(self, pos, searchinfo):
regex = searchinfo.get_regex()
last = None
for m in regex.finditer(self.document, 0):
span = m.span()
if span[1] >= pos:
break
last = m
return last
def get_line_sel(self, wnd):
doc = wnd.document
sel = wnd.screen.selection.get_selrange()
if not sel:
f = t = wnd.cursor.pos
else:
f, t = sel
tol = doc.gettol(f)
t_tol = doc.gettol(t)
if t != t_tol:
eol = doc.geteol(t)
else:
eol = t
return tol, eol
def get_indent_range(self, pos):
tol = self.document.gettol(pos)
regex = doc_re.compile(self.RE_WHITESPACE)
m = regex.match(self.document, tol)
if m:
return m.span()
else:
return (tol, tol)
def build_indent_str(self, col):
if self.indent_tab:
ctab = col // self.tab_width
cspc = col % self.tab_width
return '\t' * ctab + ' ' * cspc
else:
return ' ' * col
def cancel_auto_indent(self, wnd):
if self._last_autoindent:
f, t = (min(max(0, p), self.document.endpos())
for p in self._last_autoindent)
self._last_autoindent = None
if f != t and wnd.cursor.pos == t:
e = self.document.get_line_break(wnd.cursor.pos)
if e == t:
s = self.document.gettext(f, t)
m = re.match(self.RE_WHITESPACE, s)
if m and m.group() == s:
self.delete_string(
wnd, f, t, update_cursor=False)
wnd.cursor.setpos(f)
def on_auto_indent(self, wnd):
pos = wnd.cursor.pos
f, t = self.get_indent_range(pos)
indent = self.document.gettext(f, min(pos, t))
if pos <= t:
b = self.document.get_line_break(pos)
self.insert_string(wnd, f, '\n', update_cursor=False)
wnd.cursor.setpos(wnd.cursor.pos + 1)
wnd.cursor.savecol()
# if new line don't hava non-ws char,
# thie line should be subject to cancel-indent.
if t == b:
self._last_autoindent = (f + 1, wnd.cursor.pos)
else:
indent = '\n' + indent
self.insert_string(wnd, pos, indent, update_cursor=False)
wnd.cursor.setpos(pos + len(indent))
wnd.cursor.savecol()
self._last_autoindent = (pos + 1, wnd.cursor.pos)
wnd.cursor.savecol()
def calc_cols(self, f, t):
chars = self.document.gettext(f, t)
(dispchrs, dispcols, positions,
intervals) = screen.translate_chars(f, chars, self.tab_width,
kaa.app.config.AMBIGUOUS_WIDTH)
return sum(dispcols)
def on_global_next(self, wnd):
return
def on_global_prev(self, wnd):
return