-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcfiler_isearch.py
135 lines (98 loc) · 4.82 KB
/
cfiler_isearch.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
import sys
import os
import re
import fnmatch
import ckit
import cfiler_debug
migemo_object = None
class IncrementalSearch:
def __init__( self, ini ):
self.isearch_value = ''
self.isearch_type = ini.get( "MISC", "isearch_type" )
self.migemo_pattern = None
self.migemo_re_pattern = ""
self.migemo_re_object = None
self.migemo_re_result = None
def fnmatch( self, name, pattern, isearch_type=None ):
global migemo_object
if isearch_type==None:
isearch_type=self.isearch_type
# migemoは大文字小文字が混在しているときだけ有効
if isearch_type=="migemo":
if pattern.lower()==pattern:
isearch_type="partial"
if isearch_type!="migemo":
self.migemo_re_result = None
if isearch_type=="partial":
return fnmatch.fnmatch( name, '*'+pattern+'*' )
elif isearch_type=="inaccurate":
new_pattern = "*"
for ch in pattern:
new_pattern += ch + "*"
return fnmatch.fnmatch( name, new_pattern )
elif isearch_type=="migemo":
# 初めて migemo が必要になったときに遅延ロードする
if migemo_object==None:
dll_path = os.path.join( ckit.getAppExePath(), 'lib' )
dict_path = os.path.join( ckit.getAppExePath(), 'dict' )
try:
migemo_object = ckit.Migemo( dll_path, dict_path )
if not migemo_object.isDictionaryReady():
print( "ERROR : Migemo の辞書ファイルの読み込みに失敗しました" )
except ValueError:
return fnmatch.fnmatch( name, '*'+pattern+'*' )
# 検索パターンが変更になったときだけクエリーをかける
if self.migemo_pattern != pattern:
re_pattern = migemo_object.query(pattern)
try:
self.migemo_re_object = re.compile( re_pattern, re.IGNORECASE )
except Exception as e:
# FIXME:
# migemo_re_pattern のなかに | や + などの正規表現の特殊文字が入っていたときはエラーになってしまう。
# 例外が発生したときに True を返すことで無理やり対応するが、正しい対応方法を模索する。
cfiler_debug.printErrorInfo()
if 0:
print( "正規表現のエラー :", e )
return True
self.migemo_pattern = pattern
self.migemo_re_pattern = re_pattern
re_result = self.migemo_re_object.search(name)
if re_result:
self.migemo_re_result = re_result
return re_result!=None
else:
return fnmatch.fnmatch( name, pattern+'*' )
def cursorUp( self, get_string, length, select, scroll_pos, visible_height, margin=0 ):
for i in range( select-1, -1, -1 ):
if self.fnmatch( get_string(i), self.isearch_value ):
select = i
break
return select
def cursorDown( self, get_string, length, select, scroll_pos, visible_height, margin=0 ):
for i in range( select+1, length ):
if self.fnmatch( get_string(i), self.isearch_value ):
select = i
break
return select
def cursorPageUp( self, get_string, length, select, scroll_pos, visible_height, margin=0 ):
last_found = -1
for begin, end in ( ( select-1, scroll_pos-1+margin ), ( scroll_pos-1, scroll_pos-1-visible_height++margin ) ):
for i in range( begin, max(end,-1), -1 ):
if self.fnmatch( get_string(i), self.isearch_value ):
last_found = i
if last_found!=-1:
break
if last_found==-1:
return self.cursorUp( get_string, length, select, scroll_pos, visible_height, margin )
return last_found
def cursorPageDown( self, get_string, length, select, scroll_pos, visible_height, margin=0 ):
last_found = -1
for begin, end in ( ( select+1, scroll_pos+visible_height-margin ), ( scroll_pos+visible_height, scroll_pos+visible_height+visible_height-margin ) ):
for i in range( begin, min(end,length) ):
if self.fnmatch( get_string(i), self.isearch_value ):
last_found = i
if last_found!=-1:
break
if last_found==-1:
return self.cursorDown( get_string, length, select, scroll_pos, visible_height, margin )
return last_found