-
Notifications
You must be signed in to change notification settings - Fork 6
/
pythonvi.py
1198 lines (1149 loc) · 46.6 KB
/
pythonvi.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This is my implementaion of the classic vi editor
The plan is to support only the very basic functionality and commands
"""
import curses
import curses.ascii
import sys
import copy
import signal
import re
import string
debug = True
if debug:
logfile = open("log.txt", "a")
def writelog(*argv):
if debug:
logline = " ".join(["%s"%arg for arg in argv])
logfile.write(logline+"\n")
class LineBeyondScreenError(Exception):
"Raised when the line or cursor is beyond the screen and needs scrolling"
class EditOp(object):
"""Edit operation:
* edit_type: insert/delete/replace
* object_type: char/line
* cnt: the number of objects operated on
* pos: the position when the operation happened
* value: could be chars/single line/lines
* replacement: only used by replace ops
* backwards: boolean, true for delete command with backspace
"""
def __init__(self, editor, etype, otype, pos):
self.editor = editor
self.edit_type = etype
self.object_type = otype
self.pos = pos
self.value = ""
self.backwards = False
def reverse(self):
reverse_op = copy.copy(self)
if self.edit_type == "insert":
reverse_op.edit_type = "delete"
elif self.edit_type == "delete":
reverse_op.edit_type = "insert"
else: # replace, just swap value and replacement
reverse_op.value = self.replacement
reverse_op.replacement = self.value
reverse_op.apply()
def apply(self):
_buffer = self.editor.buffer
y, x = self.pos
if self.object_type == "char":
if self.edit_type == "delete":
if "\n" in self.value:
segments = self.value.split("\n")
if y<len(_buffer)-1:
_buffer[y] = _buffer[y][:x]+_buffer[y+1][len(segments[1]):]
del _buffer[y+1]
else:
_buffer[y] = _buffer[y][:x]
else:
_buffer[y] = _buffer[y][:x]+_buffer[y][x+len(self.value):]
elif self.edit_type == "insert":
if "\n" in self.value:
segments = self.value.split("\n")
oldline = _buffer[y]
_buffer[y] = oldline[:x]+segments[0]
_buffer.insert(y+1, segments[1]+oldline[x:])
else:
_buffer[y] = _buffer[y][:x] + self.value + _buffer[y][x:]
else:
# TODO: need to handle carriage return
_buffer[y] = _buffer[y][:x] + self.replacement + _buffer[y][x+len(self.value):]
else: # line
assert isinstance(self.value, list) # invariant
if self.edit_type == "delete":
del _buffer[y:y+len(self.value)]
elif self.edit_type =="insert":
for line in reversed(self.value):
_buffer.insert(y, line)
else:
del _buffer[y:y+len(self.value)]
for line in reversed(self.replacement):
_buffer.insert(y, line)
# after apply, editor should refresh view
def append_edit(self, char):
self.value += char
class EditList(object):
def __init__(self, editor):
self.editor = editor
self.edits = []
self.cursor = -1
def get_pos(self):
if self.cursor<0:
return None
op = self.edits[self.cursor]
return op.pos
def undo(self):
"When return False, editor should display Error in status line"
if self.cursor < 0:
return False
op = self.edits[self.cursor]
op.reverse()
self.cursor -= 1
return True
def redo(self):
if self.cursor == len(self.edits)-1:
return False
self.cursor += 1
op = self.edits[self.cursor]
op.apply()
return True
def repeat(self):
if self.cursor<0:
return False
# First remove the ops after cursor
del self.edits[self.cursor+1:]
new_op = copy.copy(self.edits[self.cursor])
# the op should be applied at current position
new_op.pos = self.editor.pos
new_op.apply()
self.edits.append(new_op)
self.cursor += 1
return True
def commitEdit(self, op):
del self.edits[self.cursor+1:]
# for backward delete, correct the pos and value
if op.backwards:
op.value = op.value[::-1]
op.pos = op.pos[0], op.pos[1]-len(op.value)
# The editop should have been applied before commit
self.edits.append(op)
self.cursor += 1
class ClipBoard(object):
def __init__(self):
self.type = "char" # either char or line
self.value = ""
def store(self, value):
if isinstance(value, basestring):
self.type = "char"
else:
self.type = "line"
self.value = value
def retrieve(self):
return self.type, self.value
class Editor(object):
def __init__(self, f, buf):
self.outfile = f
self.buffer = buf
self.editop = None
self.editlist = EditList(self)
self.config = {
"expandtab": True,
"tabspaces": 4,
}
def main_loop(self, stdscr):
self.scr = stdscr
self.maxy, self.maxx = stdscr.getmaxyx()
# This is the model part of MVC
self.topline = 0
self.line_heights = []
self.screen_lines = 0
self.mode = "command"
self.command_editing = False
self.pos = (0,0) # line and column of buffer
self.partial = "" # partial cmd in command mode
self.status_line = "-- COMMAND --"
self.commandline = "" # command entered in command line at the bottom of screen
self.checkpoint = -1 # pointer into the editlist where save happens
self.searchpos = (0,0)
self.searchkw = None
self.searchdir = "forward"
self.clipboard = ClipBoard()
self.showlineno = False
# render the initial screen
self.refresh()
self.refresh_command_line()
self.refresh_cursor()
while True:
ch = self.scr.getch()
if not self.do_command(ch):
break
@property
def dirty(self):
return self.editlist.cursor != self.checkpoint
def commit_current_edit(self):
if self.editop:
self.editlist.commitEdit(self.editop)
self.editop = None
def start_new_char_edit(self, etype, pos):
# check if old edit is committed
if self.editop:
self.editlist.commitEdit(self.editop)
self.editop = EditOp(self, etype, "char", pos)
def do_command(self, ch):
if self.mode == "editing":
self.handle_editing(ch)
else:
if self.command_editing:
self.handle_editing_command(ch)
else:
self.handle_command(ch)
return True
def reindent_line(self, lineno):
pass
def parse_command_after_char(self, ch):
# The tuple should be ordered by descending length,
# for a command, its suffix should always be after itself
chr_cmd_tuples = [
("gg", "goto_first_line"),
("dw", "delete_word"),
("dW", "delete_term"),
("yw", "yank_word"),
("yW", "yank_term"),
("dd", "delete_line"),
("yy", "yank_line"),
("r", "replace_char"),
("i", "insert_mode"),
("I", "insert_line_start"),
("o", "insert_line_after"),
("O", "insert_line_before"),
("a", "insert_after"),
("A", "insert_line_end"),
("s", "delete_char_insert"),
("S", "delete_line_insert"),
("D", "delete_line_end"),
("u", "undo"),
(".", "repeat_edit"),
("^", "goto_line_start"),
("0", "goto_line_start"),
("$", "goto_line_end"),
("-", "goto_prev_line_start"),
("+", "goto_next_line_start"),
("H", "goto_first_screen_line"),
("L", "goto_last_screen_line"),
("M", "goto_middle_screen_line"),
("G", ":goto_line"),
("w", "next_word_start"),
("W", "next_term_start"),
("e", "word_end"),
("E", "term_end"),
("b", "word_start"),
("B", "term_start"),
(":", "command_edit_mode"),
("~", "switch_case"),
("x", "delete_char"),
("X", "delete_last_char"),
("%", "match_pair"),
("/", "search_mode"),
("?", "reverse_search_mode"),
("n", "search_next"),
("N", "search_previous"),
("Y", "yank_line"),
("p", "paste_after"),
("P", "paste_before"),
]
chr_cmd_map = dict(chr_cmd_tuples)
meta_cmd_map = {
curses.ascii.DC2: "redo", # CTRL + R
curses.ascii.ACK: "next_page", # CTRL + F
curses.ascii.STX: "prev_page", # CTRL + B
curses.ascii.ENQ: "scroll_down", # CTRL + E
curses.ascii.EM: "scroll_up", # CTRL + Y
curses.KEY_DC: "delete_char",
curses.KEY_BACKSPACE: "backup_char",
curses.KEY_HOME: "goto_line_start",
curses.KEY_END: "goto_line_end",
curses.KEY_PPAGE: "prev_page",
curses.KEY_NPAGE:"next_page",
127: "backup_char", # on Mac, backspace key doesn't map to curses.KEY_BACKSPACE
}
if curses.ascii.isprint(ch):
self.partial += chr(ch)
for t in chr_cmd_tuples:
if self.partial.endswith(t[0]):
cmd = t[1]
if cmd.startswith(":"):
cmd = cmd[1:]
# meaning this command has a number before it
regex = re.compile(".*?([0-9]+)$") # non-greedy
mo = regex.match(self.partial[:-1])
self.partial = ""
if mo:
number = int(mo.group(1))
return (cmd, number)
else:
return cmd
elif cmd=="replace_char":
ch = self.scr.getch()
self.partial = ""
if curses.ascii.isprint(ch):
return (cmd, chr(ch))
else:
return None
else:
# reset the partial after find a command
self.partial = ""
return cmd
else:
# when meet a meta key, clear the partial command
self.partial = ""
if ch in meta_cmd_map:
return meta_cmd_map[ch]
return None
def is_direction_char(self, ch):
return (ch in (curses.KEY_DOWN, curses.KEY_UP, curses.KEY_LEFT, curses.KEY_RIGHT)
or (ch in (ord('h'), ord('j'), ord('k'), ord('l'))
and self.mode=="command" and not self.command_editing))
def advance_word(self, s, idx, direction="forward"):
wordchars = string.letters + string.digits + "_"
if idx >= len(s) or idx <0: return idx
step = 1 if direction=="forward" else -1
if s[idx] in wordchars:
while idx>=0 and idx < len(s) and s[idx] in wordchars : idx += step
elif s[idx] == " ":
while idx>=0 and idx < len(s) and s[idx] == " ": idx += step
else:
while id>=0 and idx < len(s) and s[idx] not in wordchars and s[idx] != " ": idx+=step
return idx
def advance_term(self, s, idx, direction="forward"):
step = 1 if direction=="forward" else -1
while idx>=0 and idx < len(s) and s[idx] != " ": idx +=step
return idx
def advance_spaces(self, s, idx, direction="forward"):
step = 1 if direction=="forward" else -1
while idx>=0 and idx < len(s) and s[idx]==" ": idx+=step
return idx
def advance_one_char(self, pos, direction="forward"):
y, x = pos
if direction=="forward":
x = x+1
if x >= len(self.buffer[y]):
if y==len(self.buffer)-1:
return None
else:
y+=1
while len(self.buffer[y])==0: y+=1
if y>=len(self.buffer): return None
else: return y, 0
else:
return y, x
else:
x = x - 1
if x < 0:
if y == 0:
return None
else:
y -= 1
while len(self.buffer[y])==0: y-=1
if y<0: return None
else: return y, len(self.buffer[y])-1
else:
return y, x
def handle_command(self, ch):
if self.is_direction_char(ch):
self.handle_cursor_move(ch)
return
cmd = self.parse_command_after_char(ch)
if not cmd: return
# if is a tuple, set the parameter
if isinstance(cmd, tuple):
parameter = cmd[1]
cmd = cmd[0]
else:
parameter = None
### commands for switching to editing mode ###
if cmd == "insert_line_after":
self.buffer.insert(self.pos[0]+1, "")
self.pos = (self.pos[0]+1, 0)
self.refresh()
self.refresh_cursor()
elif cmd == "insert_line_before":
self.buffer.insert(self.pos[0], "")
self.pos = (self.pos[0], 0)
self.refresh()
self.refresh_cursor()
elif cmd == "insert_line_start":
self.pos = (self.pos[0], 0)
self.refresh_cursor()
elif cmd == "insert_after":
if self.buffer[self.pos[0]]:
self.pos = (self.pos[0], self.pos[1]+1)
self.refresh_cursor()
elif cmd == "insert_line_end":
self.pos = (self.pos[0], len(self.buffer[self.pos[0]]))
self.refresh_cursor()
elif cmd == "delete_char_insert":
s = self.buffer[self.pos[0]]
if s:
char = s[self.pos[1]]
s = s[:self.pos[1]]+s[self.pos[1]+1:]
self.buffer[self.pos[0]] = s
self.refresh()
self.refresh_cursor()
self.editop = EditOp(self, "delete", "char", self.pos)
self.editop.value = char
self.commit_current_edit()
elif cmd == "delete_line_insert":
s = self.buffer[self.pos[0]]
if s:
oldline = self.buffer[self.pos[0]]
self.buffer[self.pos[0]] = ""
self.pos = self.pos[0], 0
self.refresh()
self.refresh_cursor()
self.editop = EditOp(self, "replace", "char", (self.pos[0], 0))
self.editop.value = oldline
self.editop.replacement = ""
self.commit_current_edit()
### commands for moving cursor position ###
elif cmd == "goto_line_start":
self.pos = (self.pos[0], 0)
self.refresh_cursor()
elif cmd == "goto_line_end":
self.pos = (self.pos[0], max(len(self.buffer[self.pos[0]])-1, 0))
self.refresh_cursor()
elif cmd == "goto_prev_line_start":
if self.pos[0]>0:
y, x = self.pos
self.pos = (y-1, 0)
self.refresh_cursor()
elif cmd == "goto_next_line_start":
if self.pos[0]<len(self.buffer)-1:
y, x = self.pos
self.pos = (y+1, 0)
self.refresh_cursor()
elif cmd == "goto_first_screen_line":
self.pos = self.topline, 0
self.refresh_cursor()
elif cmd == "goto_last_screen_line":
self.pos = self.topline +self.screen_lines-1, 0
self.refresh_cursor()
elif cmd == "goto_middle_screen_line":
self.pos = self.topline + self.screen_lines/2, 0
self.refresh_cursor()
elif cmd == "next_page":
if self.topline == len(self.buffer)-1:
return
self.topline = self.topline + self.screen_lines -1
self.refresh()
self.pos = (self.topline, 0)
self.refresh_cursor()
elif cmd == "prev_page":
if self.topline == 0:
return
# need to calculate how many line we need to scroll up
idx = self.topline
line_cnt = 0
while idx>=0:
line_cnt += len(self.buffer[idx])/self.maxx+1
if line_cnt > self.maxy-1:
break
idx -= 1
idx = min(idx+1, self.topline)
self.topline = idx
self.refresh()
self.pos = (self.topline + self.screen_lines-1, 0)
self.refresh_cursor()
elif cmd == "scroll_down":
if self.topline == len(self.buffer)-1:
return
self.topline = self.topline + 1
self.refresh()
if self.pos[0] < self.topline:
# if the cursor is beyond top of screen, move cursor to topline, keep the x pos
xpos = max(0, min(self.pos[1], len(self.buffer[self.topline])-1))
self.pos = (self.topline, xpos)
self.refresh_cursor()
elif cmd == "scroll_up":
if self.topline == 0:
return
self.topline = self.topline - 1
self.refresh()
# to make it simple, when cursor is beyond the bottom, move it to the start of last line
if self.pos[0] >= self.topline+self.screen_lines-1:
self.pos = (self.topline+self.screen_lines-1, 0)
self.refresh_cursor()
elif cmd=="goto_line" or cmd=="goto_first_line":
if cmd=="goto_first_line":
lineno = 0
elif not parameter or parameter>len(self.buffer):
# go to last line
lineno = len(self.buffer)-1
else:
lineno = parameter-1
self.topline = lineno
self.refresh()
self.pos = (lineno, 0)
self.refresh_cursor()
elif cmd == "next_word_start":
s = self.buffer[self.pos[0]]
idx = self.pos[1]
idx = self.advance_word(s, idx)
idx = self.advance_spaces(s, idx)
if idx >= len(s):
if self.pos[0] < len(self.buffer)-1:
self.pos = self.pos[0]+1, 0
else:
self.pos = self.pos[0], idx
self.refresh_cursor()
elif cmd == "next_term_start":
s = self.buffer[self.pos[0]]
idx = self.pos[1]
idx = self.advance_term(s, idx)
idx = self.advance_spaces(s, idx)
if idx >= len(s):
if self.pos[0] < len(self.buffer)-1:
self.pos = self.pos[0]+1, 0
else:
self.pos = self.pos[0], idx
self.refresh_cursor()
elif cmd == "word_end":
s = self.buffer[self.pos[0]]
idx = self.pos[1]+1
if idx >= len(s):
if self.pos[0] < len(self.buffer)-1:
self.pos = self.pos[0]+1, 0
s = self.buffer[self.pos[0]]
idx = 0
idx = self.advance_word(s, idx)
self.pos = self.pos[0], max(idx-1, 0)
self.refresh_cursor()
elif cmd == "term_end":
s = self.buffer[self.pos[0]]
idx = self.pos[1]+1
if idx >= len(s):
if self.pos[0] < len(self.buffer)-1:
self.pos = self.pos[0]+1, 0
s = self.buffer[self.pos[0]]
idx = 0
idx = self.advance_spaces(s, idx)
idx = self.advance_term(s, idx)
self.pos = self.pos[0], max(idx-1, 0)
self.refresh_cursor()
elif cmd == "word_start":
s = self.buffer[self.pos[0]]
idx = self.pos[1]-1
if idx < 0:
if self.pos[0]>0:
# back up to last line and recurse
lineno = self.pos[0]-1
self.pos = lineno, len(self.buffer[lineno])
self.handle_command(ch)
return
if s[idx] == " ":
idx = self.advance_spaces(s, idx, "backwards")
# this is duplicate code, need a way to dedup
if idx < 0:
if self.pos[0]>0:
# back up to last line and recurse
lineno = self.pos[0]-1
self.pos = lineno, len(self.buffer[lineno])
self.handle_command(ch)
return
idx = self.advance_word(s, idx, "backwards")
self.pos = self.pos[0], idx+1
self.refresh_cursor()
elif cmd == "term_start":
s = self.buffer[self.pos[0]]
idx = self.pos[1]-1
if idx < 0:
if self.pos[0]>0:
# back up to last line and recurse
lineno = self.pos[0]-1
self.pos = lineno, len(self.buffer[lineno])
self.handle_command(ch)
return
if s[idx] == " ":
idx = self.advance_spaces(s, idx, "backwards")
# this is duplicate code, need a way to dedup
if idx < 0:
if self.pos[0]>0:
# back up to last line and recurse
lineno = self.pos[0]-1
self.pos = lineno, len(self.buffer[lineno])-1
self.handle_command(ch)
return
idx = self.advance_term(s, idx, "backwards")
self.pos = self.pos[0], idx+1
self.refresh_cursor()
elif cmd == "backup_char":
self.pos = self.pos[0], max(self.pos[1]-1,0)
self.refresh_cursor()
### commands for managing edits history ###
elif cmd == "undo": # for undo
pos = self.editlist.get_pos()
if not self.editlist.undo():
self.flash_status_line("-- Already at the first edit --")
else:
self.pos = pos
self.refresh()
self.refresh_cursor()
elif cmd=="redo": # Ctrl+R for redo
if not self.editlist.redo():
self.flash_status_line("-- Already at the last edit --")
else:
self.pos = self.editlist.get_pos()
self.refresh()
self.refresh_cursor()
elif cmd=="repeat_edit":
if self.editlist.repeat():
self.pos = self.editlist.get_pos()
self.refresh()
self.refresh_cursor()
### commands for entering command editing mode (Ex mode) ###
elif cmd in ("command_edit_mode", "search_mode", "reverse_search_mode"):
self.command_editing = True
self.commandline = chr(ch)
self.refresh_command_line()
### edit commands ###
elif cmd == "switch_case":
line = self.buffer[self.pos[0]]
ch = line[self.pos[1]]
if ch in string.letters:
line = line[:self.pos[1]]+ch.swapcase()+line[self.pos[1]+1:]
self.buffer[self.pos[0]] = line
self.refresh()
# add edit operation to list
self.editop = EditOp(self, "replace", "char", self.pos)
self.editop.value = ch
self.editop.replacement = ch.swapcase()
self.commit_current_edit()
if self.pos[1]<len(line)-1:
self.pos = self.pos[0], self.pos[1]+1
self.refresh_cursor()
elif cmd in ("delete_char", "delete_last_char"):
self.handle_delete_char(ch)
elif cmd in ("delete_word", "yank_word"):
s = self.buffer[self.pos[0]]
idx = self.pos[1]
idx = self.advance_word(s, idx)
idx = self.advance_spaces(s, idx)
if cmd =="delete_word":
self.buffer[self.pos[0]] = s[:self.pos[1]]+s[idx:]
self.refresh()
if self.pos[1]<idx:
deleted = s[self.pos[1]:idx]
self.clipboard.store(deleted)
if cmd =="delete_word":
self.start_new_char_edit("delete", self.pos)
self.editop.append_edit(deleted)
self.commit_current_edit()
if cmd =="delete_word":
if self.pos[1] >= len(s):
newx = self.pos[1]-1
else:
newx = self.pos[1]
self.pos = self.pos[0], max(newx, 0)
self.refresh_cursor()
elif cmd in ("delete_term", "yank_term"):
s = self.buffer[self.pos[0]]
idx = self.pos[1]
idx = self.advance_term(s, idx)
idx = self.advance_spaces(s, idx)
if cmd =="delete_term":
self.buffer[self.pos[0]] = s[:self.pos[1]]+s[idx:]
self.refresh()
if self.pos[1]<idx:
deleted = s[self.pos[1]:idx]
self.clipboard.store(deleted)
if cmd =="delete_term":
self.start_new_char_edit("delete", self.pos)
self.editop.append_edit(deleted)
self.commit_current_edit()
if cmd =="delete_term":
if idx >= len(s):
newx = self.pos[1]-1
else:
newx = self.pos[1]
self.pos = self.pos[0], max(newx, 0)
self.refresh_cursor()
elif cmd == "delete_line":
if not len(self.buffer) or (len(self.buffer)==1 and not self.buffer[0]): return
oldline = self.buffer[self.pos[0]]
self.clipboard.store([oldline]) # deleted lines are stored in clipboard
del self.buffer[self.pos[0]]
if not self.buffer: # should preserve at least one blank line
self.buffer =[""]
self.editop = EditOp(self, "replace", "line", self.pos)
self.editop.replacement = [""]
else:
self.editop = EditOp(self, "delete", "line", self.pos)
self.editop.value = [oldline]
self.commit_current_edit()
self.refresh()
if self.pos[0] >= len(self.buffer):
y = len(self.buffer)-1
else:
y = self.pos[0]
x = min(self.pos[1], len(self.buffer[y])-1)
self.pos = y, max(x, 0)
self.refresh_cursor()
elif cmd == "delete_line_end":
s = self.buffer[self.pos[0]]
if s:
oldpos = self.pos
deleted = s[self.pos[1]:]
self.buffer[self.pos[0]] = s[:self.pos[1]]
self.pos = self.pos[0], max(self.pos[1]-1, 0)
self.refresh()
self.refresh_cursor()
self.clipboard.store(deleted)
self.editop = EditOp(self, "delete", "char", oldpos)
self.editop.value = deleted
self.commit_current_edit()
elif cmd == "replace_char":
line = self.buffer[self.pos[0]]
oldchar = line[self.pos[1]]
line = line[:self.pos[1]]+parameter+line[self.pos[1]+1:]
self.buffer[self.pos[0]] = line
self.refresh()
self.refresh_cursor()
# add replace operation
self.editop = EditOp(self, "replace", "char", self.pos)
self.editop.value = oldchar
self.editop.replacement = parameter
self.commit_current_edit()
### search related commands ###
elif cmd == "match_pair":
chrs = "()[]{}"
matcher = {"(":")", ")":"(", "[":"]", "]":"[","{":"}", "}":"{"}
char = self.buffer[self.pos[0]][self.pos[1]]
if char not in chrs: return
if char in "([{":
direction = "forward"
else:
direction = "backward"
pos = self.pos
depth = 1
while True:
pos = self.advance_one_char(pos, direction)
# import pdb;pdb.set_trace()
if not pos:
self.flash_status_line("--No matching symbol found--")
break
current = self.buffer[pos[0]][pos[1]]
if current == char: depth+=1
elif current == matcher[char]:
depth -=1
if depth == 0:
self.pos = pos
self.refresh_cursor()
break
elif cmd in ("search_next", "search_previous"):
if cmd=="search_next":
direction = self.searchdir
else:
direction = "forward" if self.searchdir == "backward" else "backward"
pos = self.search_for_keyword(direction)
if not pos:
self.flash_status_line("--Search pattern not found--")
return
self.pos = pos
self.refresh_cursor()
### copy paste commands ###
elif cmd == "yank_line":
self.clipboard.store([self.buffer[self.pos[0]]])
elif cmd in ("paste_before", "paste_after"):
t, v = self.clipboard.retrieve()
if not v: return
if t=="char":
line = self.buffer[self.pos[0]]
insertx = self.pos[1] if cmd=="paste_before" else self.pos[1]+1
self.buffer[self.pos[0]] = line[:insertx] + v +line[insertx:]
insert_pos = self.pos[0], insertx
if not line:
self.pos = self.pos[0], len(v) -1
else:
self.pos = self.pos[0], self.pos[1] + len(v)
# commit the edit operation
self.start_new_char_edit("insert", insert_pos)
self.editop.value = v
self.commit_current_edit()
else:
assert isinstance(v, list)
inserty = self.pos[0] if cmd =="paste_before" else self.pos[0]+1
for line in reversed(v):
self.buffer.insert(inserty, line)
self.pos = inserty, 0
self.editop = EditOp(self, "insert", "line", self.pos)
self.editop.value = v
self.commit_current_edit()
self.refresh()
self.refresh_cursor()
# Check if needs to switching to editing mode
if "insert" in cmd:
self.mode = "editing"
self.refresh_command_line()
def save_file(self):
assert self.outfile is not None
self.outfile.truncate(0)
# Seek is absolutely necessary as truncate does NOT modify file position
self.outfile.seek(0)
for line in self.buffer:
self.outfile.write(line)
self.outfile.write("\n")
self.outfile.flush()
# save the editlist cursor
self.checkpoint = self.editlist.cursor
def handle_editing_command(self, ch):
if curses.ascii.isprint(ch):
self.commandline += chr(ch)
self.refresh_command_line()
elif ch==curses.KEY_BACKSPACE or ch==127:
self.commandline = self.commandline[:-1]
self.refresh_command_line()
elif ch==27 or ch==curses.ascii.ETX: # ESC, back to command mode
self.command_editing = False
self.commandline = ""
self.refresh_command_line()
self.refresh_cursor()
elif ch==10: # new line \n
self.command_editing = False
if self.commandline.startswith(":"):
commandline = self.commandline[1:].strip()
if commandline in ("q", "q!"): # handle quit commands
if commandline == "q" and self.dirty:
self.flash_status_line("--Unsaved Changes--")
else:
raise SystemExit()
elif commandline in ("w", "wq"): # handle write and write/quit without filename
if self.outfile:
self.save_file()
if commandline == "wq":
raise SystemExit()
else:
self.flash_status_line("--File saved--")
else:
self.flash_status_line("--Target file not specified--")
elif commandline.startswith("w ") or commandline.startswith("wq "):
parts = commandline.split()
if len(parts)>2:
self.flash_status_line("--Only one file name allowed--")
else:
cmd, filename = parts
try:
self.outfile = open(filename, "w")
except Exception as e:
self.flash_status_line("--File open fails: %s--"%e)
else:
self.save_file()
if cmd == "wq":
raise SystemExit()
else:
self.flash_status_line("--File saved--")
elif commandline in ("nu", "nonu"):
self.showlineno = (commandline=="nu")
if self.showlineno:
self.maxx = self.scr.getmaxyx()[1] - self.get_lineno_width()
else:
self.maxx = self.scr.getmaxyx()[1]
self.refresh()
self.refresh_cursor()
else:
self.flash_status_line("-- Command not recognized --")
elif self.commandline.startswith("/") or self.commandline.startswith("?"):
keyword = self.commandline[1:]
try:
keyword_regex = re.compile(keyword)
except:
self.flash_status_line("--Invalid search pattern string--")
return
self.searchpos = self.pos
self.searchkw = keyword_regex
if self.commandline.startswith("/"):
self.searchdir = "forward"
else:
self.searchdir = "backward"
pos = self.search_for_keyword(self.searchdir)
if not pos:
self.flash_status_line("--Search pattern not found--")
return
self.pos = pos
self.refresh_cursor()
self.refresh_command_line()
self.refresh_cursor()
def search_for_keyword(self, direction="forward"):
# return a position where the keyword is found, or return None if not found
y, x = self.searchpos
# First make sure the start_pos is valid position, if invalid, fall back to 0,0
if y>=len(self.buffer) or (x>=len(self.buffer[y]) and x!=0):
y, x = 0, 0
step = 1 if direction=="forward" else -1
visited = []
while True: # iterate over the lines
visited.append((y, x))
line = self.buffer[y][x:] if direction=="forward" else self.buffer[y][:x]
if line:
if direction == "forward":
mo = self.searchkw.search(line) # left to right
if mo:
self.searchpos = y, x+mo.end()
if self.searchpos[1] == len(self.buffer[y]):
self.searchpos = (y+step)%len(self.buffer), 0
return (y, x+mo.start())
else: # backward
# to search right to left for regex: http://stackoverflow.com/questions/33232729/how-to-search-for-the-last-occurrence-of-a-regular-expression-in-a-string-in-pyt
starts = [mo.start() for mo in self.searchkw.finditer(line)]
if starts:
self.searchpos = y, starts[-1]
return (y, starts[-1])
y = y+step
if y >=len(self.buffer):
y = 0
self.flash_status_line("--Search hit BOTTOM, continuing at TOP--")
elif y<0:
y = len(self.buffer)-1
self.flash_status_line("--Search hit TOP, continuing at BOTTOM--")
x = 0 if direction=="forward" else len(self.buffer[y])
if (y, x) in visited: # revisit a visited position, meaning the doc has been scanned in full
return None
def handle_cursor_move(self, ch):
# finish the last edit if exists
self.commit_current_edit()
y, x = self.pos
# print y, x
if ch in (curses.KEY_UP, ord('k')) and y > 0:
y = y-1
last_char = len(self.buffer[y])
if self.mode == "command" and last_char>0:
last_char = last_char-1
x = min(x, last_char)
self.pos = (y, x)
# if y<self.topline:
# self.topline -= 1
# self.refresh()
self.refresh_cursor()
elif ch in (curses.KEY_DOWN, ord('j')) and y < len(self.buffer)-1:
y = y + 1
last_char = len(self.buffer[y])
if self.mode == "command" and last_char>0:
last_char = last_char-1
x = min(x, last_char)
self.pos = (y, x)
self.refresh_cursor()
elif ch in (curses.KEY_LEFT, ord('h')) and x>0:
self.pos = (y, x-1)
self.refresh_cursor()
elif ch in (curses.KEY_RIGHT, ord('l')):
last_char = len(self.buffer[y])
if self.mode == "command" and last_char>0:
last_char = last_char-1
if x<last_char:
self.pos = (y, x+1)
self.refresh_cursor()
def handle_delete_char(self, ch):
if (not self.editop or not self.editop.edit_type == "delete"
or (self.editop.backwards and ch==curses.KEY_DC)
or (not self.editop.backwards and ch==curses.KEY_BACKSPACE)
or ch==120 or ch==88):
self.start_new_char_edit("delete", self.pos)
if ch==curses.KEY_BACKSPACE or ch==88:
self.editop.backwards = True
y, x = self.pos
if ch==curses.KEY_DC or ch==120: # del or x
if x == len(self.buffer[y]):
if y < len(self.buffer)-1:
# delete the \n at the end of a line
self.editop.append_edit("\n")
if ch==120: self.clipboard.store("\n")
self.buffer[y] = self.buffer[y] + self.buffer[y+1]
del self.buffer[y+1]
self.commit_current_edit()
# else, last line, last char, ignore
else:
char = self.buffer[y][x]
self.editop.append_edit(char)