-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit2.py
2069 lines (1688 loc) · 58.6 KB
/
edit2.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
import Constants
import curses
from curses import ascii
import traceback
from curses import textpad
import math
import functools
import json
import copy
"""
edit.py provides the data structures and associated
functions to edit
Essentially functions as a mini text editor
Meant to be used as a plug in addition to the flash card system as an
an editor to fix up cards and cards, as well as to add user notes
Opertions:
Planned:
Insert character(s)
Delete character(s)
Scroll Up
Scroll Down
Scroll Left
Scroll Right
Enter (\n)
FUTURE/OPTIONAL
[opt] HOTKEYING/CTRL+Keys
[opt] Copy and Paste
[opt] Cut and Paste
[opt] Help
[opt] Far Right
[opt] Far Left
[opt] Top
[opt] Bottom
NO SUPPORT PLANNED;
Overwrite characters
REPRESENTATION INVARIANT/CLASS INVARIANT
String Buffer List:
1. Each String is at most 80 characters long
2. If a string has a \n or a \r in it, even if it is less than 80 characters
in length, the data is sent is sent to the next line instead
3. Each string must be stored in the same order it was stored in
"""
def strike(text):
"""
ASCII strikethrough, found on stack overflow from some nice guys over there
# https://stackoverflow.com/questions/25244454/
# python-create-strikethrough-strikeout-overstrike-string-type
"""
result = ''
for c in text:
result = result + c + '\u0336'
return result
def remove_highlight_color(buffer, color_num):
"""
remove_highlight_color(buffer) removes all color_pair(color_num)
form all chars in the buffer
REQUIRESL color_num not equal to defaul 0!!!!
Returns buffer
color_num is an int between 0 and len(color_pairs) - 1
"""
assert (color_num != 0)
for row in buffer:
for char_list in row:
if curses.color_pair(color_num) in char_list:
char_list.remove(curses.color_pair(color_num))
return buffer
def find(word, buffer, color_num):
"""
find(word, buffer, color_num) is the list of list of locations in the buffer
corresponding to places where word matches buffer
color_num is the color to change the highlihgting in a find to
it is between 0 and len(color_pairs) - 1 inclusive
Returns: same buffer
word is string
buffer is a triply nested list, with inner list containing at least a string
REQUIRES: word does not have \n or \r or \t or any escape characters in it
RAISES: assertion error if \n or \r ot \t found
REQUIRES: if word is empty string "" then returns buffer with
the coloring cleared out
"""
assert("\n" not in word)
assert("\r" not in word)
assert("\t" not in word)
if word == "":
return remove_highlight_color(buffer, color_num)
matches = find_matches(word, buffer)
for hit_locs in matches:
for i, j in hit_locs:
char_list = buffer[i][j]
if curses.color_pair(color_num) not in char_list:
char_list.append(curses.color_pair(color_num))
return buffer
def find_matches(word, buffer):
"""
Returns a list of lists positions in buffer where word matches the characters
in buffer, if no matches then
empty list reutnred
REQUIRES: word is a string and has length >= 1 and buffer
is a list of list of lists, where the first element of the inner most
list must be a string
"""
matches = []
w = word[0]
for i in range(len(buffer)):
row = buffer[i]
for j in range(len(row)):
char_list = row[j]
char = char_list[0]
if char == w:
res, hit_locs = check_match(word, buffer, i, j)
if res:
matches.append(hit_locs)
return matches
def check_match(word, buffer, i, j):
"""
check_match(word, chars, i) checks that word completely matches
the characters at chars[i,...len(word) - 1 + i] of wrapping in to the next line
and returns a tuple of true and the list of match positions
otherwise if no match than false and [].
Requires: i, j in buffer e.g, 0 <= i < len(buffer) and word[0] == buffer[i]
and 0 <= j < len(buffer[i])
Word need not be in chars and can be longer than chars or extend
pass chars end
i is row
j is column in buffer
"""
def check_match_helper(word, buffer, i, j, hit_locs):
if word == "":
return (True, hit_locs)
if i >= len(buffer):
return (False, [])
w = word[0]
c = buffer[i][j][0]
if w != c:
return (False, [])
hit_locs.append((i, j))
remaining_word = word[1:]
if j == len(buffer[i]) - 1:
return check_match_helper(remaining_word, buffer, (i + 1), j, hit_locs)
return check_match_helper(remaining_word, buffer, i, (j + 1), hit_locs)
return check_match_helper(word, buffer, i, j, [])
def init_buffer(string):
"""
initializes a buffer based on the string
"""
return split_text(add_style(clean_text(string)))
def buffer_is_empty(buffer):
return buffer == []
def clean_text(string):
"""
clean_text(string) replaces tabs with appropriate number of spaces not going
over column limit using TAB_SIZE as defined
Returns new string with tabs removed and replaced appropriately
REQUIRES: TAB SIZE <= LINE LENGTH
"""
counter = 0
new_string = ""
for c in string:
if c == "\n" or c == "\r":
new_string += c
counter = 0
elif c == "\t" and counter + Constants.TAB_SIZE - 1 > Constants.LINE_LENGTH - 1:
# tab replaced with as many spaces as possible before hitting right bound
new_string += (Constants.LINE_LENGTH - counter) * " "
counter = 0
elif counter == Constants.LINE_LENGTH - 1:
new_string += c
counter = 0
elif c == "\t":
rem = Constants.TAB_SIZE - counter % Constants.TAB_SIZE
new_string += rem * " "
counter += rem
else:
new_string += c
counter += 1
return new_string
def add_style(string):
"""
add_style(str) turns str into a list of lists with style inserted
"""
final_split = []
for c in string:
final_split.append([c, curses.A_NORMAL])
return final_split
def remove_style(style_list):
"""
remove_style(style_list) restores the style list to a string
"""
s = ""
for l in style_list:
s += l[0]
return s
def buffer_repr(buffer):
"""
buffer_repr(buffer) converts the buffer to a list - string represrntation
"""
new_list = []
for l in buffer:
new_list.append(remove_style(l))
return new_list
def split_text(text):
"""
split_text(text) splits text into a list of strings satisfying the Representation
Invariant
PROPERTY: INverse of Join text
REQUIRES: Split text to initialize string buffer list data structure
"""
def split_text_helper(text, str_list):
l = len(text)
if l <= Constants.LINE_LENGTH:
return split_at_newline(text, str_list)
i = 0
while (i < l):
if (i == (Constants.LINE_LENGTH - 1)):
first_half = text[0: (i + 1)]
second_half = text[(i + 1): l]
return split_text_helper(second_half, str_list + [first_half])
elif (text[i][0] == "\n" or text[i][0] == "\r"):
first_half = text[0: (i + 1)]
second_half = text[(i + 1): l]
return split_text_helper(second_half, str_list + [first_half])
i += 1
return split_text_helper(text, [])
def split_at_newline(text, str_list):
"""
split_at_newline(text, str_list) splits text into constituent strings in
a list separated by \n or \r
If text is empty, returns empty list
REQUIRES: text has less than or equal to 80 characters including all \n and \r
"""
if text == []:
return str_list
l = len(text)
i = 0
while (i < l):
if (text[i][0] == "\n") or (text[i][0] == "\r"):
remainder = text[(i + 1): l]
cut_portion = text[0:(i + 1)]
return split_at_newline(remainder, str_list + [cut_portion])
i += 1
return str_list + [text]
def join_text(str_list):
"""
join_text(text) joins list of strings into a string of text
PROPERTY: INverse of split_text
"""
new_list = []
for l in str_list:
new_list += l
return new_list
def insert(c, x, y, str_list):
"""
insert(c, x, y, str_list) inserts a character c at the yth row from the top and the xth
character from the left in that row, given the rows in str_list
and returns the new formatted string buffer list
REQUIRES: X and Y are valid coordinates in the str_list
e.g. X, Y actually in str_list and not outside
"""
s = str_list[y]
l_s = len(s)
s_new = s[0: x] + [c] + s[x: l_s]
str_list[y] = s_new
new_text = join_text(str_list)
new_str_list = split_text(new_text)
return new_str_list
def retrieve_text(x1, y1, x2, y2, str_list):
"""
retrieve_text(x1, y1, x2, y2, str_list)
retrieves the strings of text from str_list bounded from
x1, y1 to x2, y2. Returns a string inclusive of x1 AND x2
that is includes both ending locations
USE: FOR COPY AND CUT
REQUORES: x1, y1 must come sequentially after x2, yx2
REQUIRES: x1 and x2 are valid locations in str_list
"""
assert (x1 <= x2 and y1 == y2) or (y1 < y2)
def retrieve_text_helper(x1, y1, x2, y2, str_list, acc):
# same line (one line)
if (y1 == y2):
buffer = str_list[y1]
s = buffer[x1: (x2 + 1)]
return acc + [s]
# two or more lines
first_line = str_list[y1]
l = len(first_line)
s = first_line[x1: l]
return retrieve_text_helper(0, (y1 + 1), x2, y2, str_list, acc + [s])
return join_text(retrieve_text_helper(x1, y1, x2, y2, str_list, []))
def bulk_insert(s, x, y, str_list):
"""
bulk_insert(s, x, y, str_list) adds in all characters in s beginning
at x, y in str_list
Returns modified str_list after all bulk insertion is complete
USe: Copy/Cut
"""
s_rev = s[::-1]
for i in range(len(s_rev)):
c = s_rev[i]
str_list = insert(c, x, y, str_list)
return str_list
def delete(x, y, str_list):
"""
delete(x, y, str_list) deletes the character at the yth row from the top and the xth
character from the left in that row, given the rows in str_list
and returns the new formatted string buffer list.
If str_list is empty, will act as NOP, no action
REQUIRES: X and Y are valid coordinates in the str_list
e.g. X, Y actually in str_list and not outside
"""
if str_list == []:
return str_list
s = str_list[y]
s_new = s[0: x] + s[(x + 1):]
str_list[y] = s_new
new_text = join_text(str_list)
new_str_list = split_text(new_text)
return new_str_list
def bulk_delete(s, x, y, str_list):
"""
bulk_delete(s, x, y, str_list) removes all characters in s beginning
at x, y in str_list
Returns the modified str_list after the bulk_delete has finished
Use: Cut
"""
for _ in range(len(s)):
str_list = delete(x, y, str_list)
return str_list
def buffer_add_row(buffer, y, c):
buffer[y] += [[c, curses.A_NORMAL]]
return buffer
def get_max_min(x1, y1, x2, y2):
"""
get_max_min(x1, y1, x2, y2) returns two tuples,
such that the first tuple has either y1 == y2 and x1 <= x2
or y1 < y2
"""
if y1 == y2:
tempx1 = x1
tempx2 = x2
x1 = min(tempx1, tempx2)
x2 = max(tempx1, tempx2)
elif y1 > y2:
tempx, tempy = x1, y1
x1, y1 = x2, y2
x2, y2 = tempx, tempy
return ((x1, y1), (x2, y2))
def insert_style_buffer(start_tup, end_tup, buffer, style):
x1, y1 = start_tup
x2, y2 = end_tup
if y1 == y2:
row = buffer[y1]
for i in list(range(x1, (x2 + 1), 1)):
c_list = row[i]
if style in c_list:
c_list.remove(style)
else:
c_list.append(style)
return buffer
else:
row = buffer[y1]
l = len(row)
for i in list(range(x1, l, 1)):
c_list = row[i]
if style in c_list:
c_list.remove(style)
else:
c_list.append(style)
next_tup = (0, y1 + 1)
return insert_style_buffer(
next_tup, end_tup, buffer, style)
def insert_color_buffer(start_tup, end_tup, buffer, color):
x1, y1 = start_tup
x2, y2 = end_tup
if y1 == y2:
row = buffer[y1]
for i in list(range(x1, (x2 + 1), 1)):
c_list = row[i]
# 1 index is color
c_list[1] = color
return buffer
else:
row = buffer[y1]
l = len(row)
for i in list(range(x1, l, 1)):
c_list = row[i]
# 1 index is color
c_list[1] = color
next_tup = (0, y1 + 1)
return insert_style_buffer(
next_tup, end_tup, buffer, color)
def get_word(buffer, x, y):
pass
def legal_macro_commands(op):
if op == Constants.UP:
return False
elif op == Constants.DOWN:
return False
elif op == Constants.RIGHT:
return False
elif op == Constants.LEFT:
return False
elif op == Constants.GO_RIGHT:
return False
elif op == Constants.GO_LEFT:
return False
elif op == Constants.GO_TOP:
return False
elif op == Constants.GO_BOTTOM:
return False
elif op == Constants.UP_ONE_PAGE:
return False
elif op == Constants.DOWN_ONE_PAGE:
return False
elif op == Constants.UNDO:
return False
elif op == Constants.REDO:
return False
elif op == Constants.BOLD:
return False
elif op == Constants.HIGHLIGHT:
return False
elif op == Constants.UNDERLINE:
return False
elif op == Constants.EXIT_EDITOR:
return False
elif op == Constants.MACRO_RECORD:
return False
elif op == Constants.MACRO_RUN:
return False
# colors
elif op == Constants.COLOR_BLACK:
return False
elif op == Constants.COLOR_CYAN:
return False
elif op == Constants.COLOR_GREEN:
return False
elif op == Constants.COLOR_YELLOW:
return False
elif op == Constants.COLOR_RED:
return False
elif op == Constants.COPY:
return False
elif op == Constants.CUT:
return False
elif op == Constants.PASTE:
return False
elif op == Constants.ESCAPE:
return False
elif op == Constants.COPY_ALL:
return False
elif op == Constants.COPY_WORD:
return False
elif op == Constants.SETB1:
return False
elif op == Constants.SETB2:
return False
elif op == Constants.SETB3:
return False
elif op == Constants.SETB4:
return False
elif op == Constants.SETB5:
return False
elif op == Constants.JUMPB1:
return False
elif op == Constants.JUMPB2:
return False
elif op == Constants.JUMPB3:
return False
elif op == Constants.JUMPB4:
return False
elif op == Constants.JUMPB5:
return False
elif op == Constants.FIND:
return False
return True
class Screen:
"""
Screen is an object representing the editing screen.
str_list is the list of string buffers
camera_level is the row in the str_list that marks the top of the editing
box
h is the height of the screen[h >= 0]
w is the width of the screen[w >= 0]
ulx is upper left x coordinate
uly is upper left y coordinate
cursor is a tuple containing an x - y pair of where the mouse
is in:
w > Cursor.x >= 0
and
h > Cursor.y >= 0
Can only animate portion of camera from camera_level to
camera_level + h - 1, inclusive of both first and last lines
copy_loc2 cannot be anothing but None None if copy_loc1 is None None
If the buffer list becomes empty, replace it with an empty string
automatically
IMPORTANT: the screen y coordinate is always between camera_level
and camera_level + h - 1 inclusive
TO BE USED IN A STRUCT MANNER
DIRECTION: X increases left yo right
Y increases top down
DISPLAY:
"""
def __init__(self, string, h=Constants.NROWS, w=Constants.NCOLS, ulx=0, uly=0, cursor=(0, 0)):
"""
Initializes a screen object for use as a struct
DIRECTION: X increases left yo right
Y increases top down
"""
self.buffer = init_buffer(string)
self.h = h
self.w = w
self.ulx = 0
self.uly = 0
self.cursor = (0, 0) # (ulx, uly) # cursor = (0, 0)??
self.screen_cursor = (0, 0) # (ulx, uly)
self.camera_level = 0 # uly
self.bookmarks = Constants.NUM_BOOKMARKS * [None]
self.copy_loc1 = (None, None)
self.copy_loc2 = (None, None)
self.cut_loc1 = (None, None)
self.cut_loc2 = (None, None)
self.copy_buffer = None
self.exit_editor = False
self.bold_loc1 = (None, None)
self.bold_loc2 = (None, None)
self.highlight1 = (None, None)
self.highlight2 = (None, None)
self.underline1 = (None, None)
self.underline2 = (None, None)
self.copy_all = None
self.color1 = (None, None)
self.color2 = (None, None)
self.color = None
self.save_pointer = 0
self.save_buffer = [copy.deepcopy(self.buffer)]
self.saved_cursor = [copy.deepcopy(self.cursor)]
self.redo_pointer = 0
self.redo_buffer = [copy.deepcopy(self.buffer)]
self.redo_cursor = [copy.deepcopy(self.cursor)]
self.macro_buffer = []
self.start_macro = False
# FIND STUFF
self.edit_find = False # edit mode find updates
self.using_find = False # whether we can enter finding in search bar
self.find_buffer = "" # value shown in search bar
self.find_x = 0 # the further x column of find
self.find_y = Constants.NUM_LINES
def enter_find(self):
self.using_find = True
self.edit_find = False
self.find_buffer = ""
self.find_x = 0
def enter_edit_find(self):
self.using_find = False
self.edit_find = True
def leave_edit_find(self):
self.using_find = False
self.edit_find = False
self.find_buffer = ""
self.find_x = 0
self.buffer = remove_highlight_color(self.buffer, color_num=11)
def update_find(self, op, c):
"""
update_find(self, c)
requires self.using_find to be true to call
"""
if op == Constants.DELETE:
l = len(self.find_buffer)
if l == 0:
return
self.find_buffer = self.find_buffer[:(l-1)]
self.find_x -= 1
return
elif op == Constants.RETURN:
# do not reset self.find_buffer or find_x to keep showing the find
# on screen
self.buffer = find(self.find_buffer, self.buffer, color_num=11)
self.enter_edit_find()
return
# tab no-op
elif op == Constants.TAB:
return
# is character
self.find_buffer += c
self.find_x += 1
def update_edit_find(self):
self.buffer = remove_highlight_color(self.buffer, color_num=11)
self.buffer = find(self.find_buffer, self.buffer, color_num=11)
def update_edit_history(self, undo_occurred):
if undo_occurred:
return
self.save_pointer += 1
self.save_buffer += [copy.deepcopy(self.buffer)]
self.saved_cursor += [copy.deepcopy(self.cursor)]
if self.save_pointer >= Constants.SAVE_TRACK_LENGTH:
diff = self.save_pointer - Constants.SAVE_TRACK_LENGTH + 1
self.save_buffer = self.save_buffer[diff:]
self.saved_cursor = self.saved_cursor[diff:]
self.save_pointer -= diff
# at every update, the redo buffer is updated as well
self.redo_pointer = 0
self.redo_buffer = [copy.deepcopy(self.buffer)]
self.redo_cursor = [copy.deepcopy(self.cursor)]
def update_undo(self):
# reset redo
self.add_redo(self.buffer, self.cursor)
# if no edits made on current line, no op
if self.save_pointer == 0:
self.buffer = self.save_buffer[self.save_pointer]
# move cursor
self.cursor = self.saved_cursor[self.save_pointer]
self.save_buffer = [copy.deepcopy(self.buffer)]
self.saved_cursor = [copy.deepcopy(self.cursor)]
return
self.save_pointer -= 1
self.buffer = self.save_buffer[self.save_pointer]
# move cursor
self.cursor = self.saved_cursor[self.save_pointer]
# update saved buffer
l = len(self.save_buffer)
self.save_buffer = self.save_buffer[:(l - 1)]
# update saved cursor
self.saved_cursor = self.saved_cursor[:(l - 1)]
def flush_undo(self, buffer, cursor):
self.save_pointer = 0
self.saved_cursor = [copy.deepcopy(cursor)]
self.save_buffer = [copy.deepcopy(buffer)]
def add_redo(self, buffer, cursor):
self.redo_pointer += 1
self.redo_cursor += [copy.deepcopy(cursor)]
self.redo_buffer += [copy.deepcopy(buffer)]
if self.redo_pointer >= Constants.SAVE_TRACK_LENGTH:
diff = self.redo_pointer - Constants.SAVE_TRACK_LENGTH + 1
self.redo_buffer = self.redo_buffer[diff:]
self.redo_cursor = self.redo_cursor[diff:]
self.redo_pointer -= diff
def update_redo(self):
# once a redo occurs, undo is cleared
self.flush_undo(self.buffer, self.cursor)
# if no edits made on current line, no op
if self.redo_pointer == 0:
self.buffer = self.redo_buffer[self.redo_pointer]
# move cursor
self.cursor = self.redo_cursor[self.redo_pointer]
self.redo_buffer = [copy.deepcopy(self.buffer)]
self.redo_cursor = [copy.deepcopy(self.cursor)]
return
self.redo_pointer -= 1
self.buffer = self.redo_buffer[self.redo_pointer]
# move cursor
self.cursor = self.redo_cursor[self.redo_pointer]
# update saved buffer
l = len(self.redo_buffer)
self.redo_buffer = self.redo_buffer[:(l - 1)]
# update saved cursor
self.redo_cursor = self.redo_cursor[:(l - 1)]
def reset_cut_copy(self):
"""
reset_cut_copy(self) resets all stores and buffers when copyiny
or cutting is terminated/escaped
"""
self.reset_paste()
self.copy_buffer = None
self.copy_all = None
def reset_paste(self):
"""
reset_copy_paste(self) resets all cut locations
"""
self.copy_loc1 = (None, None)
self.copy_loc2 = (None, None)
self.cut_loc1 = (None, None)
self.cut_loc2 = (None, None)
def update_cut(self):
x, y = self.cursor
if self.cut_loc1 == (None, None):
self.cut_loc1 = (x, y)
return
else:
self.cut_loc2 = (x, y)
if self.cut_loc2 != (None, None):
x1, y1 = self.cut_loc1
x2, y2 = self.cut_loc2
if y1 == y2:
tempx1 = x1
tempx2 = x2
x1 = min(tempx1, tempx2)
x2 = max(tempx1, tempx2)
elif y1 > y2:
tempx, tempy = x1, y1
x1, y1 = x2, y2
x2, y2 = tempx, tempy
buffer = self.buffer
l_buff = len(buffer)
if y1 >= l_buff or y2 >= l_buff:
self.reset_paste()
return
s1 = buffer[y1]
s2 = buffer[y2]
l_1 = len(s1)
l_2 = len(s2)
if x1 >= l_1 or x2 >= l_2:
self.reset_paste()
return
last_length = len(self.buffer[y2])
s = retrieve_text(x1, y1, x2, y2, self.buffer)
self.copy_buffer = s
self.buffer = bulk_delete(s, x1, y1, self.buffer)
self.reset_paste()
# move to beginning of cut
if x1 != 0:
self.cursor = (x1 - 1, y1)
if x2 == last_length - 1:
self.buffer = buffer_add_row(self.buffer, y1, "\n")
elif x1 == 0 and y1 == 0:
self.cursor = (0, 0)
else:
buffer = self.buffer
s = buffer[y1 - 1]
l = len(s)
self.cursor = (l - 1, y1 - 1)
if x2 == last_length - 1:
self.buffer = buffer_add_row(self.buffer, y1 - 1, "\n")
def update_copy(self):
x, y = self.cursor
if self.copy_loc1 == (None, None):
self.copy_loc1 = (x, y)
else:
self.copy_loc2 = (x, y)
if self.copy_loc2 != (None, None):
x1, y1 = self.copy_loc1
x2, y2 = self.copy_loc2
if y1 == y2:
tempx1 = x1
tempx2 = x2
x1 = min(tempx1, tempx2)
x2 = max(tempx1, tempx2)
elif y1 > y2:
tempx, tempy = x1, y1
x1, y1 = x2, y2
x2, y2 = tempx, tempy
buffer = self.buffer
l_buff = len(buffer)
if y1 >= l_buff or y2 >= l_buff:
self.reset_paste()
return
s1 = buffer[y1]
s2 = buffer[y2]
l_1 = len(s1)
l_2 = len(s2)
if x1 >= l_1 or x2 >= l_2:
self.reset_paste()
return
self.copy_buffer = retrieve_text(
x1, y1, x2, y2, self.buffer)
def update_paste(self):
s = self.copy_buffer
if s != None:
x, y = self.cursor
self.buffer = bulk_insert(s, x, y, self.buffer)
self.reset_paste()
return
def update_delete(self):
"""
update_delete(self) updates the cursor and buffer after
a character is deleted where the cursor was
Requires: cursor lies on a character
"""
x, y = self.cursor
str_list = self.buffer
new_str_list = delete(x, y, str_list)
# move x key back one if possible
if x != 0:
self.cursor = (max(x - 1, 0), y)
# otherwise push key up a row
elif y != 0:
buffer = str_list[y - 1]
l = len(buffer)
self.cursor = (l - 1, y - 1)
else:
self.cursor = (0, max(y - 1, 0))
self.buffer = new_str_list
def update_return(self):
"""
update_return(self) adds in a newline where the cursor is
Requires: cursor lies on a character
"""
x, y = self.cursor
str_list = self.buffer
new_str_list = insert(["\n", curses.A_NORMAL], x, y, str_list)
# buffer = new_str_list[y]
# l = len(buffer)
# if y == (l - 1):
# # rule - insert space after a newline so can access
# new_str_list = new_str_list + [" "]
# TRY NEW
# self.cursor = (x, y)
# TRY NEW
self.cursor = (0, y + 1)
self.buffer = new_str_list
def update_insert(self, c):
"""
update_insert(self) adds in a new character where the cursor is
and maintains invariant aftr
Requires: cursor lies on a character
"""
x, y = self.cursor
str_list = self.buffer
new_str_list = insert([c, curses.A_NORMAL], x, y, str_list)
num_rows = len(new_str_list)
buffer = new_str_list[y]
l = len(buffer)
if x < (l - 1):
self.cursor = (x + 1, y)
elif y < (num_rows - 1):
self.cursor = (0, y + 1)
else:
self.cursor = (x, y)
self.buffer = new_str_list
def jump_to_bookmark(self, bookmark_n):
"""
jump_to_bookmark(self, bookmark_n) jumps cursor to book mark n