Repository navigation
Expand file tree
/
Copy pathckit_textwidget.py
More file actions
3562 lines (2686 loc) · 127 KB
/
Copy pathckit_textwidget.py
File metadata and controls
3562 lines (2686 loc) · 127 KB
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 os
import re
import math
import bisect
import cProfile
import pyauto
from ckit import ckitcore
from ckit import ckit_textwindow
from ckit import ckit_widget
from ckit import ckit_command
from ckit import ckit_theme
from ckit import ckit_key
from ckit import ckit_misc
from ckit import ckit_userconfig
from ckit import ckit_resource
from ckit.ckit_const import *
## @addtogroup textwidget テキスト編集ウィジェット機能
## @{
#--------------------------------------------------------------------
Line = ckitcore.Line
DEFAULT_LINEEND = "\r\n" # FIXME : カスタマイズ
#--------------------------------------------------------------------
import re
import struct
lexer_debug = False
class Token:
stock_SimpleTextLine = struct.pack("ii",0,0)
@staticmethod
def pack( tokens ):
if tokens == [(0,0)]:
#print( "simple text line", id(Token.stock_SimpleTextLine) )
return Token.stock_SimpleTextLine
s = b""
for token in tokens:
s += struct.pack("ii",token[0],token[1])
return s
@staticmethod
def unpack( s ):
tokens = []
for i in range( 0, len(s), 8 ):
tokens.append( struct.unpack( "ii", s[i:i+8] ) )
return tokens
Token_Text = 0
Token_Keyword = 1
Token_Name = 2
Token_Number = 3
Token_String = 4
Token_Preproc = 5
Token_Comment = 6
Token_Space = 7
Token_Error = -1
def rootContext():
return 'root'
## シンタックス分析クラスのベースクラス
class Lexer:
def __init__(self):
pass
def lex( self, ctx, line, detail ):
pass
## テキストファイル用のシンタックス分析クラス
class TextLexer(Lexer):
tokens = [ ( 0, Token_Text ) ]
def __init__(self):
Lexer.__init__(self)
def lex( self, ctx, line, detail ):
if detail:
return TextLexer.tokens, ctx
else:
return ctx
## 正規表現のテーブルを使ったシンタックス分析クラスのベースクラス
class RegexLexer(Lexer):
Detail = 1<<0 # detailモードの場合のみ使用するルール
def __init__(self):
Lexer.__init__(self)
self.rule_map = {}
self.rule_map_ctx = {}
self.re_flags = 0
self.compiled = False
def _compile(self):
for k in self.rule_map.keys():
rule_list = self.rule_map[k]
for i in range(len(rule_list)):
rule = rule_list[i]
rule = list(rule)
if len(rule)<=2:
rule.append(None)
if len(rule)<=3:
rule.append(0)
if rule[0]!=None:
rule[0] = re.compile( rule[0], self.re_flags )
rule_list[i] = rule
assert( not( isinstance( rule[2], tuple ) or isinstance( rule[2], list ) ) )
if rule[0]==None or (rule[3] & RegexLexer.Detail)==0:
if not k in self.rule_map_ctx:
self.rule_map_ctx[k] = []
self.rule_map_ctx[k].append(rule)
self.compiled = True
def lex( self, ctx, line, detail ):
if not self.compiled:
self._compile()
if lexer_debug:
print( "line :", line )
tokens = []
pos = 0
def getRuleList():
if detail:
return self.rule_map[ctx]
else:
return self.rule_map_ctx[ctx]
rule_list = getRuleList()
while pos<=len(line):
if lexer_debug:
print( " context :", ctx )
last = pos==len(line)
nearest_rule = None
nearest_result = None
default_rule = None
for rule in rule_list:
if rule[0]!=None:
re_result = rule[0].search( line, pos )
if re_result:
if nearest_result==None or re_result.start()<nearest_result.start():
nearest_result = re_result
nearest_rule = rule
if re_result.start()==pos:
break
else:
default_rule = rule
break
else:
if pos<len(line):
tokens.append( ( pos, Error ) )
break
if lexer_debug:
print( " nearest_rule :", nearest_rule )
print( " default_rule :", default_rule )
if default_rule:
if len(tokens)==0 or tokens[-1][1]!=default_rule[1]:
if pos<len(line):
if lexer_debug:
print( " %d, %d (default rule)" % (pos, default_rule[1]) )
tokens.append( ( pos, default_rule[1] ) )
if nearest_rule:
pos = nearest_result.start()
rule = nearest_rule
if isinstance( rule[1], tuple ) or isinstance( rule[1], list ):
action_list = rule[1]
for i in range(len(action_list)):
if len(tokens)==0 or tokens[-1][1]!=action_list[i]:
if pos<len(line):
if lexer_debug:
print( " %d, %d (group %d)" % (pos, action_list[i], i) )
tokens.append( ( pos, action_list[i] ) )
pos += len( nearest_result.group(i+1) )
else:
if len(tokens)==0 or tokens[-1][1]!=rule[1]:
if pos<len(line):
if lexer_debug:
print( " %d, %d" % (pos, rule[1]) )
tokens.append( ( pos, rule[1] ) )
pos += len( nearest_result.group(0) )
if rule[2]:
ctx = rule[2]
rule_list = getRuleList()
if lexer_debug:
print( " context ->", ctx )
if default_rule and not nearest_rule:
break
if last:
break
if detail:
return tokens, ctx
else:
return ctx
#--------------------------------------------------------------------
class CompletionResult:
def __init__( self, items, selection, left, right ):
self.items = items
self.selection = selection
self.left = left
self.right = right
class Completion:
def __init__(self):
pass
def getCandidateList( self, window, edit, point ):
return None
class WordCompletion(Completion):
def __init__(self):
Completion.__init__(self)
def getCandidateList( self, window, edit, point ):
word_left = point.wordLeft()
word_right = point.wordRight()
hint = edit.getText( word_left, point )
tail = edit.getText( point, word_right )
if not re.fullmatch( "[a-zA-Z0-9_]+", hint ):
return None
# 単語の途中にキャレットがあるときは補完しない
if re.match( "[a-zA-Z0-9_]+", tail ):
return None
items = set()
re_patern = re.compile( "\\b" + hint + "[a-zA-Z0-9_]+" )
for edit in window.edit_list:
# 速度のため、行数が大きすぎるファイルは無視
if len(edit.doc.lines) > 10000:
continue
for line in edit.doc.lines:
s = line.s
# 速度のため、長すぎる行は無視
if len(s) > 1000:
continue
pos = 0
while pos<len(s):
re_result = re_patern.search(s,pos)
if re_result:
items.add( re_result.group(0) )
pos += 1
else:
break
items = list(items)
items.sort()
return CompletionResult( items, 0, word_left, point )
#--------------------------------------------------------------------
## TextWidget の動作モードのベースクラス
class Mode:
name = "-"
tab_by_space = False
tab_width = 4
show_tab = True
show_space = False
show_wspace = True
show_lineend = True
show_fileend = True
cancel_selection_on_copy = False
copy_line_if_not_selected = True
cut_line_if_not_selected = True
wordbreak = ckit_misc.WordBreak(
(
"\"!@#$%^&*()+|~-=\\`[]{};:',./<>?", # 0
ckit_misc.UnicodeRange('0','z'), # 1
ckit_misc.UnicodeRange('、','〟'), # 2
ckit_misc.UnicodeRange('ぁ','ゖ'), # 3
" \t", # 4
),
{
-1 : [ 0, 1, 2 ],
0 : [ -1, 1, 2, 3 ],
1 : [ -1, 0, 2, 3 ],
2 : [ -1, 0, 1, 3 ],
3 : [ -1, 0, 1, 2 ],
4 : [ -1, 0, 1, 2, 3 ],
}
)
bracket = (
'{}',
'()',
'[]',
'<>',
)
def __init__(self):
pass
def __str__(self):
return self.name
def executeCommand( self, name, info ):
try:
command = getattr( self, "command_" + name )
except AttributeError:
return False
command(info)
return True
def enumCommand(self):
for attr in dir(self):
if attr.startswith("command_"):
yield attr[ len("command_") : ]
@staticmethod
def staticconfigure(window):
pass
def configure( self, edit ):
pass
class TextMode(Mode):
name = "text"
def __init__(self):
Mode.__init__(self)
self.lexer = TextLexer()
self.completion = WordCompletion()
@staticmethod
def staticconfigure(window):
Mode.staticconfigure(window)
ckit_userconfig.callConfigFunc("staticconfigure_TextMode",window)
def configure( self, edit ):
Mode.configure( self, edit )
ckit_userconfig.callConfigFunc("configure_TextMode",self,edit)
#--------------------------------------------------------------------
class UndoInfo:
def __init__(self):
self.old_text = None
self.old_text_left = None
self.old_text_right = None
self.old_anchor = None
self.old_cursor = None
self.new_text = None
self.new_text_left = None
self.new_text_right = None
self.new_anchor = None
self.new_cursor = None
def __str__(self):
return "UndoInfo( %s,%s, %s,%s )" % ( self.old_text_left, self.old_text_right, self.new_text_left, self.new_text_right )
class AtomicUndoInfo:
def __init__(self):
self.old_anchor = None
self.old_cursor = None
self.new_anchor = None
self.new_cursor = None
self.items = []
def __str__(self):
return "AtomicUndoInfo( %s,%s, %s,%s )" % ( self.old_anchor, self.old_cursor, self.new_anchor, self.new_cursor )
## TextWidget の文書中の位置を示すクラス
class Point:
def __init__( self, edit, line=0, index=0 ):
self.edit = edit
self.line = line
self.index = index
def copy(self):
return Point( self.edit, self.line, self.index )
def __eq__(self,other):
return isinstance(other,Point) and self.line==other.line and self.index==other.index
def __ne__(self,other):
return not isinstance(other,Point) or self.line!=other.line or self.index!=other.index
def __gt__(self,other):
if self.line>other.line:
return True
if self.line<other.line:
return False
return self.index>other.index
def __ge__(self,other):
if self.line>other.line:
return True
if self.line<other.line:
return False
return self.index>=other.index
def __lt__(self,other):
if self.line<other.line:
return True
if self.line>other.line:
return False
return self.index<other.index
def __le__(self,other):
if self.line<other.line:
return True
if self.line>other.line:
return False
return self.index<=other.index
def __str__(self):
return "Point(%d,%d)" % (self.line,self.index)
def __repr__(self):
return "Point(%d,%d)" % (self.line,self.index)
def left( self, block_mode=False ):
point = self.copy()
if point.index>0:
point.index -= 1
else:
if not block_mode and point.line>0:
point.line -= 1
point.index = len(self.edit.doc.lines[point.line].s)
return point
def right( self, block_mode=False ):
point = self.copy()
if block_mode or point.index<len(self.edit.doc.lines[point.line].s):
point.index += 1
else:
if point.line<len(self.edit.doc.lines)-1:
point.line += 1
point.index = 0
return point
def wordLeft( self, use_type_bounds=True ):
point = self.copy()
if point.index>0:
line = self.edit.doc.lines[point.line]
point.index = self.edit.doc.mode.wordbreak( line.s, point.index, -1, use_type_bounds=use_type_bounds )
else:
if point.line>0:
point.line -= 1
line = self.edit.doc.lines[point.line]
point.index = self.edit.doc.mode.wordbreak( line.s, len(line.s), -1, use_type_bounds=use_type_bounds )
return point
def wordRight( self, use_type_bounds=True ):
point = self.copy()
if point.index<len(self.edit.doc.lines[point.line].s):
line = self.edit.doc.lines[point.line]
point.index = self.edit.doc.mode.wordbreak( line.s, point.index, +1, use_type_bounds=use_type_bounds )
else:
if point.line<len(self.edit.doc.lines)-1:
point.line += 1
line = self.edit.doc.lines[point.line]
point.index = self.edit.doc.mode.wordbreak( line.s, 0, +1, use_type_bounds=use_type_bounds )
return point
def tabLeft(self):
point = self.copy()
tab_width = self.edit.doc.mode.tab_width
column = self.edit.getColumnFromIndex( point.line, point.index )
column = max( ( column - 1 ) // tab_width * tab_width, 0 )
point.index = self.edit.getIndexFromColumn( point.line, column )
return point
def up( self, step=1, block_mode=False ):
point = self.copy()
if point.line-step>=0:
point.line -= step
point.index = self.edit.getIndexFromColumn( point.line, self.edit.ideal_column, block_mode=block_mode )
else:
point.line = 0
point.index = 0
return point
def down( self, step=1, block_mode=False ):
point = self.copy()
if point.line+step<=len(self.edit.doc.lines)-1:
point.line += step
point.index = self.edit.getIndexFromColumn( point.line, self.edit.ideal_column, block_mode=block_mode )
else:
point.line = len(self.edit.doc.lines)-1
point.index = len(self.edit.doc.lines[point.line].s)
return point
def lineBegin(self):
point = self.copy()
point.index = 0
return point
def lineEnd(self):
point = self.copy()
point.index = len(self.edit.doc.lines[point.line].s)
return point
def lineFirstGraph(self):
point = self.copy()
s = self.edit.doc.lines[point.line].s
s2 = s.lstrip(" \t")
point.index = len(s)-len(s2)
return point
def correspondingBracket( self, bracket_pair_list ):
point = self.copy()
s = self.edit.doc.lines[point.line].s
for bracket_pair in bracket_pair_list:
if point.index<len(s) and bracket_pair[0]==s[point.index]:
step = 1
depth = 1
inside = False
point = point.right()
break
elif point.index<len(s) and bracket_pair[1]==s[point.index]:
step = -1
depth = 1
inside = True
break
elif point.index>0 and bracket_pair[0]==s[point.index-1]:
step = 1
depth = 1
inside = True
break
elif point.index>0 and bracket_pair[1]==s[point.index-1]:
step = -1
depth = 1
inside = False
point = point.left()
break
else:
return point
num_checked_lines = 0
while True:
s = self.edit.doc.lines[point.line].s
if step>0:
pos1 = s.find( bracket_pair[0], point.index )
pos2 = s.find( bracket_pair[1], point.index )
else:
pos1 = s.rfind( bracket_pair[0], 0, point.index )
pos2 = s.rfind( bracket_pair[1], 0, point.index )
if pos1>=0 and (pos2<0 or int(math.copysign(1,pos2-pos1))==step):
depth += step
point.index = pos1
elif pos2>=0 and (pos1<0 or int(math.copysign(1,pos1-pos2))==step):
depth -= step
point.index = pos2
else:
if step>0:
point = point.lineEnd()
else:
point = point.lineBegin()
# 1000行チェックしても見つからなかったら諦める
num_checked_lines += 1
if num_checked_lines > 1000:
# FIXME : エラーメッセージをステータスバーに表示するべき
return self.copy()
if depth<=0 : break
if step>0:
new_point = point.right()
else:
new_point = point.left()
# 末端まで到達
if new_point==point:
return self.copy()
point = new_point
if (step>0 and not inside) or (step<0 and inside):
point = point.right()
return point
## TextWidget の選択範囲を示すクラス
class Selection:
def __init__( self, doc ):
self.pos = [ doc.pointDocumentBegin(), doc.pointDocumentBegin() ]
self.direction = 0
self.block_mode = False
def left(self):
return self.pos[0].copy()
def right(self):
return self.pos[1].copy()
def cursor(self):
if self.direction==1:
return self.pos[1].copy()
else:
return self.pos[0].copy()
def anchor(self):
if self.direction==1:
return self.pos[0].copy()
else:
return self.pos[1].copy()
def setSelection( self, anchor, cursor ):
anchor = anchor.copy()
cursor = cursor.copy()
if anchor==cursor:
self.direction = 0
self.pos = [ anchor, cursor ]
elif anchor<cursor:
self.direction = 1
self.pos = [ anchor, cursor ]
else:
self.direction = -1
self.pos = [ cursor, anchor ]
def setBlockMode( self, block_mode ):
self.block_mode = block_mode
## TextWidget の文書を示すクラス
class Document:
def __init__( self, filename, mode, encoding=None ):
if filename:
filename = ckit_misc.normPath(filename)
self.lines = [ Line("") ]
self.encoding = ckit_misc.TextEncoding('utf-8')
self.lineend = DEFAULT_LINEEND
self.filename = filename
self.timestamp = None
self.readonly = False
self.bg_color_name = None
self.diff_mode = False
self.undo_list = []
self.redo_list = []
self.atomic_undo = None
self.modcount = 0
# Documentの変更を所属する全てのTextWidgetに通知するためのハンドラ
self.text_modified_handler_list = []
self.bookmark_handler_list = []
self.mode = None
self.lexer = None
self.lex_ctx_dirty_top = 0
self.lex_token_dirty_top = 0
self.minor_mode_list = []
if filename and os.path.exists(filename):
with open( filename, "rb" ) as fd:
self.readFile( fd, encoding )
if ckit_misc.getFileAttribute(filename) & ckit_misc.FILE_ATTRIBUTE_READONLY:
self.setReadOnly(True)
# 大きいファイルは TextMode にする ( 100000 行 or 10 MB )
fd.seek( 0, os.SEEK_END )
file_size = fd.tell()
if len(self.lines) > 100000 or file_size > 10 * 1024 * 1024:
# FIXME : メッセージを表示するべき
mode = TextMode()
self.setMode(mode)
self.clearFileModified()
def getName(self):
if self.filename:
return os.path.basename(self.filename)
else:
return "untitled"
def getFullpath(self):
return self.filename
def isModified(self):
return self.modcount==None or self.modcount!=0
def clearModified(self):
for line in self.lines:
line.modified = False
self.modcount = 0
def isFileModified(self):
if not self.filename : return False
try:
timestamp = os.stat(self.filename).st_mtime
except WindowsError:
return False
return self.timestamp != timestamp
def clearFileModified(self):
if self.filename:
try:
self.timestamp = os.stat(self.filename).st_mtime
except WindowsError:
self.timestamp = None
def setMode( self, mode ):
self.mode = mode
self.lexer = mode.lexer
for line in self.lines:
line.ctx = None
line.tokens = None
self.lex_ctx_dirty_top = 0
self.lex_token_dirty_top = 0
def appendMinorMode( self, mode ):
self.minor_mode_list.append(mode)
def removeMinorMode( self, name ):
for mode in self.minor_mode_list:
if mode.name == name:
self.minor_mode_list.remove(mode)
return
else:
raise ValueError(name)
def clearMinorMode(self):
self.minor_mode_list.clear()
def hasMinorMode( self, name ):
for mode in self.minor_mode_list:
if mode.name == name:
return True
else:
return False
def setReadOnly( self, readonly ):
self.readonly = readonly
if self.readonly:
self.setBGColor("readonly_bg")
def setBGColor( self, bg_color_name ):
self.bg_color_name = bg_color_name
def readFile( self, fd, encoding=None ):
self.lines = []
self.undo_list = []
self.redo_list = []
self.modcount = 0
fd.seek( 0, os.SEEK_SET )
data = fd.read()
detected_encoding = ckit_misc.detectTextEncoding( data, ascii_as="utf-8" )
if detected_encoding.bom:
data = data[ len(detected_encoding.bom) : ]
if not encoding:
encoding = detected_encoding
if encoding.encoding:
data = data.decode( encoding=encoding.encoding, errors='replace' )
# 波ダッシュ → 全角チルダ
if encoding.encoding=="cp932":
data = data.replace( "\u301c", "\uff5e" )
self.encoding = encoding
else:
raise UnicodeError
lines = data.splitlines(True)
self.lines = [ Line(line) for line in lines ]
# 空か、改行で終わっている場合は、最後に行を追加する
if len(self.lines)==0 or self.lines[-1].end:
self.lines.append( Line("") )
if len(self.lines)>0 and self.lines[0].end:
self.lineend = self.lines[0].end
else:
self.lineend = DEFAULT_LINEEND
self.lex_ctx_dirty_top = 0
self.lex_token_dirty_top = 0
def writeFile( self, fd ):
if self.encoding.bom:
fd.write( self.encoding.bom )
for line in self.lines:
s = line.s
# 全角チルダ → 波ダッシュ
if self.encoding.encoding=="cp932":
s = s.replace( "\uff5e", "\u301c" )
s = s.encode( self.encoding.encoding, 'replace' )
fd.write(s)
end = line.end.encode( self.encoding.encoding )
fd.write(end)
def updateSyntaxContext( self, stop=None, max_lex=None ):
#print( "updateSyntaxContext", stop, max_lex )
start = self.lex_ctx_dirty_top
if stop==None:
stop = len(self.lines)
else:
stop = min( stop, len(self.lines) )
if max_lex==None:
max_lex = len(self.lines)
if self.lex_ctx_dirty_top==None or self.lex_ctx_dirty_top>=stop:
return
line = start
num_lex = 0
prev_line_dirty = (self.lines[line].ctx==None)
while True:
if self.lines[line].ctx==None or prev_line_dirty:
if line==0:
ctx = rootContext()
else:
prev_line = self.lines[line-1]
ctx = self.lexer.lex( prev_line.ctx, prev_line.s, False )
if self.lines[line].ctx != ctx:
self.lines[line].ctx = ctx
self.lines[line].tokens = None
prev_line_dirty = True
else:
prev_line_dirty = False
num_lex += 1
line += 1
if line>=stop or num_lex>=max_lex:
break
if line<len(self.lines):
if prev_line_dirty:
self.lines[line].ctx = None
self.lex_ctx_dirty_top = line
else:
self.lex_ctx_dirty_top = None
def updateSyntaxTokens( self, start=None, stop=None, max_lex=None ):
#print( "updateSyntaxTokens", start, stop, max_lex )
if start==None:
start = 0
if stop==None:
stop = len(self.lines)
else:
stop = min( stop, len(self.lines) )
if max_lex==None:
max_lex = len(self.lines)
if self.lex_token_dirty_top==None or self.lex_token_dirty_top>=stop:
return
num_lex = 0
for line in range( start, stop ):
if self.lines[line].tokens==None:
ctx = self.lines[line].ctx
assert( ctx != None )
tokens, tmp = self.lexer.lex( ctx, self.lines[line].s, True )
self.lines[line].tokens = Token.pack( tokens )
num_lex += 1
if num_lex>=max_lex:
break
if start<=self.lex_token_dirty_top and line==len(self.lines)-1:
self.lex_token_dirty_top = None
def fillSyntaxContextAndTokens( self, start, stop, ctx, tokens ):
if start==None:
start = 0
if stop==None:
stop = len(self.lines)
else:
stop = min( stop, len(self.lines) )
for line in range( start, stop ):
self.lines[line].ctx = ctx
self.lines[line].tokens = Token.pack(tokens)
def updateSyntaxTimer(self):
if not isinstance( self.lexer, TextLexer ):
self.updateSyntaxContext( max_lex=1000 )
self.updateSyntaxTokens( start=self.lex_token_dirty_top, max_lex=100 )
def isSyntaxDirty(self):
return (self.lex_ctx_dirty_top!=None or self.lex_token_dirty_top!=None)
#--------------------------------------------------------------------
## TextWidget の検索条件を保存し実行するクラス
class Search:
def __init__( self, text, word, case, regex ):
self.text = text
self.word = word
self.case = case