00001 import sys
00002 import os
00003 import re
00004 import fnmatch
00005
00006 import clnch_ini
00007
00008 migemo_object = None
00009
00010 class IncrementalSearch:
00011
00012 def __init__( self ):
00013 self.isearch_value = u''
00014 self.isearch_type = clnch_ini.get( "MISC", "isearch_type", "partial" )
00015 self.migemo_pattern = None
00016 self.migemo_re_pattern = u""
00017 self.migemo_re_object = None
00018 self.migemo_re_result = None
00019
00020 def fnmatch( self, name, pattern, isearch_type=None ):
00021
00022 global migemo_object
00023
00024 if isearch_type==None:
00025 isearch_type=self.isearch_type
00026
00027
00028 if isearch_type=="migemo":
00029 if pattern.lower()==pattern:
00030 isearch_type="partial"
00031
00032 if isearch_type=="partial":
00033 return fnmatch.fnmatch( name, '*'+pattern+'*' )
00034
00035 elif isearch_type=="inaccurate":
00036 new_pattern = "*"
00037 for ch in pattern:
00038 new_pattern += ch + "*"
00039 return fnmatch.fnmatch( name, new_pattern )
00040
00041 elif isearch_type=="migemo":
00042
00043
00044 if migemo_object==None:
00045 import migemo
00046 dict_path = os.path.join( os.path.split(sys.argv[0])[0], 'dict/migemo-dict' )
00047 migemo_object = migemo.Migemo(dict_path)
00048
00049
00050 if self.migemo_pattern != pattern:
00051
00052 re_pattern = migemo_object.query(pattern)
00053
00054 try:
00055 self.migemo_re_object = re.compile( re_pattern, re.IGNORECASE )
00056 except Exception, e:
00057
00058
00059
00060 if 0:
00061 print u"正規表現のエラー :", e
00062 return True
00063
00064 self.migemo_pattern = pattern
00065 self.migemo_re_pattern = re_pattern
00066
00067 re_result = self.migemo_re_object.search(name)
00068 if re_result:
00069 self.migemo_re_result = re_result
00070 return re_result!=None
00071
00072 else:
00073 return fnmatch.fnmatch( name, pattern+'*' )
00074
00075 def cursorUp( self, get_string, length, select, scroll_pos, visible_height, margin=0 ):
00076 for i in xrange( select-1, -1, -1 ):
00077 if self.fnmatch( get_string(i), self.isearch_value ):
00078 select = i
00079 break
00080 return select
00081
00082 def cursorDown( self, get_string, length, select, scroll_pos, visible_height, margin=0 ):
00083 for i in xrange( select+1, length ):
00084 if self.fnmatch( get_string(i), self.isearch_value ):
00085 select = i
00086 break
00087 return select
00088
00089 def cursorPageUp( self, get_string, length, select, scroll_pos, visible_height, margin=0 ):
00090
00091 last_found = -1
00092
00093 for begin, end in ( ( select-1, scroll_pos-1+margin ), ( scroll_pos-1, scroll_pos-1-visible_height++margin ) ):
00094
00095 for i in xrange( begin, max(end,-1), -1 ):
00096 if self.fnmatch( get_string(i), self.isearch_value ):
00097 last_found = i
00098
00099 if last_found!=-1:
00100 break
00101
00102 if last_found==-1:
00103 return self.cursorUp( get_string, length, select, scroll_pos, visible_height, margin )
00104
00105 return last_found
00106
00107 def cursorPageDown( self, get_string, length, select, scroll_pos, visible_height, margin=0 ):
00108
00109 last_found = -1
00110
00111 for begin, end in ( ( select+1, scroll_pos+visible_height-margin ), ( scroll_pos+visible_height, scroll_pos+visible_height+visible_height-margin ) ):
00112
00113 for i in xrange( begin, min(end,length) ):
00114 if self.fnmatch( get_string(i), self.isearch_value ):
00115 last_found = i
00116
00117 if last_found!=-1:
00118 break
00119
00120 if last_found==-1:
00121 return self.cursorDown( get_string, length, select, scroll_pos, visible_height, margin )
00122
00123 return last_found
00124