def install_default_auto_identifier(self, obj): if not obj.ini.has_key('auto_default'): return r = obj.ini.auto_default.values() if not r: return slist = [] for i in r: func = self.get_function(i) if func: try: result = func(self.editor) if result: slist.extend(result) except: error.error('Execute %s.%s failed!' % (func.module_name, func.function_name)) error.traceback() return False else: if not isinstance(i, list): slist.append(i) else: slist.extend(i) d = self.editor.default_auto_identifier for i in slist: if len(i) <= 1: continue k = i[0].upper() s = d.setdefault(k, []) if i not in s: s.append(i) for k, v in d.items(): d[k].sort(lambda x, y:cmp(x.upper(), y.upper()))
def get_function(self, modstring): func = None if not isinstance(modstring, list) and modstring.startswith('@'): module, function = modstring[1:].rsplit('.', 1) if self.acpmodules.has_key(module): mod = self.acpmodules[module] if self.need_reinstall_module(mod): mod = reload(mod) self.set_modules_time(mod) else: try: mod = __import__(module, [], [], ['']) self.set_modules_time(mod) except: error.error("Can't load the module " + module) error.traceback() return False self.acpmodules[module] = mod func = getattr(mod, function, None) if not callable(func): func = None else: func.module_name = module func.function_name = function return func
def install_default_auto_identifier(self, obj): if not obj.ini.has_key('auto_default'): return r = obj.ini.auto_default.values() if not r: return slist = [] for i in r: func = self.get_function(i) if func: try: result = func(self.editor) if result: slist.extend(result) except: error.error('Execute %s.%s failed!' % (func.module_name, func.function_name)) error.traceback() return False else: if not isinstance(i, list): slist.append(i) else: slist.extend(i) d = self.editor.default_auto_identifier for i in slist: if len(i) <= 1: continue k = i[0].upper() s = d.setdefault(k, []) if i not in s: s.append(i) for k, v in d.items(): d[k].sort(lambda x, y: cmp(x.upper(), y.upper()))
def loadEasySyntaxItems(self): if not self.syntaxitems: self.initSyntaxItems() else: def get_id(s): return s.replace(' ', '_').lower() for name, style, display in self.syntaxitems: _id = get_id(name) i = 0 old_id = _id while 1: if i: _id = "%s%d" % (old_id, i) if not self._syntaxitems.has_key(_id): self._syntaxitems[_id] = SyntaxItem( _id, name, style, display) self._syntaxnames.append(_id) break else: error.error( tr('The name [%s] is already existed in lexer [%s]' ) % (name, self.name)) i += 1
def init_accelerator(win, accellist, editoraccellist): ini = common.get_config_file_obj(onelevel=True) keylist = {} for mid, v in accellist.items(): keys, func = v if not keys: continue if not keys in keylist: keylist[keys] = (mid, 'main') else: error.error('There is already %s defined! Please check.' % keys) for mid, v in editoraccellist.items(): keys, func = v if not keys in keylist: keylist[keys] = (mid, 'editor') else: error.error('There is already %s defined! Please check.' % keys) #mid can be a mainframe menu ID or a mainframe function name #which should only has one parameter for mid, hotkey in ini.main_hotkey.items(): _id, _t = keylist.get(hotkey, ('', '')) if _id: if _t == 'main': keys, func = accellist[_id] accellist[_id] = ('', func) else: keys, func = editoraccellist[_id] editoraccellist[_id] = ('', func) if mid in editoraccellist: keys, func = editoraccellist[mid] del editoraccellist[mid] accellist[mid] = (hotkey, func) elif mid in accellist: keys, func = accellist[mid] accellist[mid] = (hotkey, func) #mid can be a editor menu ID or a editor function name #which should only has one parameter for mid, hotkey in ini.editor_hotkey.items(): _id, _t = keylist.get(hotkey, ('', '')) if _id: if _t == 'main': keys, func = accellist[_id] accellist[_id] = ('', func) else: keys, func = editoraccellist[_id] editoraccellist[_id] = ('', func) if accellist.has_key(mid): keys, func = accellist[mid] del accellist[mid] editoraccellist[mid] = (hotkey, func) elif editoraccellist.has_key(mid): keys, func = editoraccellist[mid] editoraccellist[mid] = (hotkey, func)
def addSyntaxItem(self, name, dispname, wx_const, defaultstylestring): if not self._syntaxitems.has_key(name): self._syntaxitems[name] = SyntaxItem(name, dispname, wx_const, defaultstylestring) self._syntaxnames.append(name) else: error.error( tr('The name [%s] is already existed in lexer [%s]') % (name, self.name))
def convert_key(self, keydef): f = 0 ikey = 0 for k in keydef.split('+'): uk = k.upper() if uk == 'CTRL': f |= wx.stc.STC_SCMOD_CTRL elif uk == 'ALT': f |= wx.stc.STC_SCMOD_ALT elif uk == 'SHIFT': f |= wx.stc.STC_SCMOD_SHIFT elif keylist.has_key(uk): ikey = keylist[uk] elif len(uk) == 1: ikey = ord(uk) else: error.error("[TextEditor] Undefined char [%s]" % uk) continue return f, ikey
def loadEasySyntaxItems(self): if not self.syntaxitems: self.initSyntaxItems() else: def get_id(s): return s.replace(' ', '_').lower() for name, style, display in self.syntaxitems: _id = get_id(name) i = 0 old_id = _id while 1: if i: _id = "%s%d" % (old_id, i) if not self._syntaxitems.has_key(_id): self._syntaxitems[_id] = SyntaxItem(_id, name, style, display) self._syntaxnames.append(_id) break else: error.error(tr('The name [%s] is already existed in lexer [%s]') % (name, self.name)) i += 1
def parse_result(self, assistant_obj, text, start, end, key='', matchtext='', line=None, matchobj=None): """ return True if success return False is failed """ win = self.editor text = text[0] if (text and text[0] == '@') or (assistant_obj.ini.has_key('auto_funcs') and assistant_obj.ini.auto_funcs.has_key(text)): #maybe a function if text[0] != '@': modstring = assistant_obj.auto_funcs.get(text) else: modstring = text func = self.get_function(modstring) if func: try: flag, result = func(win, matchobj) if isinstance(result, tuple): result = list(result) result.sort(lambda x, y:cmp(x.upper(), y.upper())) elif isinstance(result, list): result.sort(lambda x, y:cmp(x.upper(), y.upper())) except: error.error('Execute %s.%s failed!' % (func.module_name, func.function_name)) error.traceback() return False if flag == 'replace': if win.AutoCompActive(): self.postcall(win.AutoCompCancel) if isinstance(result, list): def f(): win.BeginUndoAction() # win.inputassistant_obj = assistant_obj pos = win.PositionFromLine(line) win.replace_strings = matchtext win.word_len = pos + matchobj.start(), pos + matchobj.end() win.UserListShow(list_type, " ".join(result)) win.EndUndoAction() self.postcall(f) return True else: def f(): win.BeginUndoAction() win.SetTargetStart(start) win.SetTargetEnd(end) win.ReplaceTarget('') win.GotoPos(start) self.settext(result) win.EndUndoAction() self.postcall(f) return True elif flag == 'append': if win.AutoCompActive(): wx.CallAfter(win.AutoCompCancel) if isinstance(result, list): def f(): win.BeginUndoAction() # win.inputassistant_obj = assistant_obj win.replace_strings = matchtext win.word_len = win.GetCurrentPos(), -1 win.UserListShow(list_type, " ".join(result)) win.EndUndoAction() self.postcall(f) return True else: def f(): win.BeginUndoAction() self.settext(result) win.EndUndoAction() self.postcall(f) return True elif flag == "blank": return True else: error.error("Can't recognize result type " + flag) error.traceback() return True return False
def parse_result(self, assistant_obj, text, start, end, key='', matchtext='', line=None, matchobj=None): """ return True if success return False is failed """ win = self.editor text = text[0] if (text and text[0] == '@') or ( assistant_obj.ini.has_key('auto_funcs') and assistant_obj.ini.auto_funcs.has_key(text)): #maybe a function if text[0] != '@': modstring = assistant_obj.auto_funcs.get(text) else: modstring = text func = self.get_function(modstring) if func: try: flag, result = func(win, matchobj) if isinstance(result, tuple): result = list(result) result.sort(lambda x, y: cmp(x.upper(), y.upper())) elif isinstance(result, list): result.sort(lambda x, y: cmp(x.upper(), y.upper())) except: error.error('Execute %s.%s failed!' % (func.module_name, func.function_name)) error.traceback() return False if flag == 'replace': if win.AutoCompActive(): self.postcall(win.AutoCompCancel) if isinstance(result, list): def f(): win.BeginUndoAction() # win.inputassistant_obj = assistant_obj pos = win.PositionFromLine(line) win.replace_strings = matchtext win.word_len = pos + matchobj.start( ), pos + matchobj.end() win.UserListShow(list_type, " ".join(result)) win.EndUndoAction() self.postcall(f) return True else: def f(): win.BeginUndoAction() win.SetTargetStart(start) win.SetTargetEnd(end) win.ReplaceTarget('') win.GotoPos(start) self.settext(result) win.EndUndoAction() self.postcall(f) return True elif flag == 'append': if win.AutoCompActive(): wx.CallAfter(win.AutoCompCancel) if isinstance(result, list): def f(): win.BeginUndoAction() # win.inputassistant_obj = assistant_obj win.replace_strings = matchtext win.word_len = win.GetCurrentPos(), -1 win.UserListShow(list_type, " ".join(result)) win.EndUndoAction() self.postcall(f) return True else: def f(): win.BeginUndoAction() self.settext(result) win.EndUndoAction() self.postcall(f) return True elif flag == "blank": return True else: error.error("Can't recognize result type " + flag) error.traceback() return True return False
def addSyntaxItem(self, name, dispname, wx_const, defaultstylestring): if not self._syntaxitems.has_key(name): self._syntaxitems[name] = SyntaxItem(name, dispname, wx_const, defaultstylestring) self._syntaxnames.append(name) else: error.error(tr('The name [%s] is already existed in lexer [%s]') % (name, self.name))
def ColumnEditAction(win, event, col, begin, end, in_key_down=False): """if dealed then return True""" char = event.GetKeyCode() alt = event.AltDown() shift = event.ShiftDown() ctrl = event.ControlDown() line = win.GetCurrentLine() f = None if in_key_down: if not alt and not shift and not ctrl: if char == wx.WXK_RETURN: return True elif char == wx.WXK_DELETE: def func(win, line): if win.GetCurrentPos() < win.GetLineEndPosition(line) and win.GetLineEndPosition(line) > 0: win.execute_key('DEL') f = func elif char == wx.WXK_TAB: def func(win, line): win.execute_key('TAB') f = func elif char == wx.WXK_BACK: def func(win, line): col = win.GetCurrentPos() - win.PositionFromLine(line) if col == 0: if win.GetLineEndPosition(line) > 0: win.execute_key('DEL') else: win.execute_key(wx.stc.STC_CMD_DELETEBACK) f = func else: return False else: return False else: if not ((31 <char < 127) or char > wx.WXK_PAGEDOWN): return False i = 0 win.BeginUndoAction() try: lastline = win.GetCurrentLine() while begin+i <= end: delta = win.PositionFromLine(begin+i) + col - win.GetLineEndPosition(begin+i) if delta > 0: win.GotoPos(win.GetLineEndPosition(begin+i)) win.AddText(' '*delta) else: win.GotoPos(win.PositionFromLine(begin+i) + col) if f: f(win, begin+i) else: if 31 <char < 127: win.AddText(chr(char)) else: try: win.AddText(unichr(char)) except: error.error("Conver %d to unichar failed" % char) error.traceback() break if begin + i == lastline: lastpos = win.GetCurrentPos() i += 1 win.GotoPos(lastpos) finally: win.EndUndoAction() return True
def ColumnEditAction(win, event, col, begin, end, in_key_down=False): """if dealed then return True""" char = event.GetKeyCode() alt = event.AltDown() shift = event.ShiftDown() ctrl = event.ControlDown() line = win.GetCurrentLine() f = None if in_key_down: if not alt and not shift and not ctrl: if char == wx.WXK_RETURN: return True elif char == wx.WXK_DELETE: def func(win, line): if win.GetCurrentPos() < win.GetLineEndPosition( line) and win.GetLineEndPosition(line) > 0: win.execute_key('DEL') f = func elif char == wx.WXK_TAB: def func(win, line): win.execute_key('TAB') f = func elif char == wx.WXK_BACK: def func(win, line): col = win.GetCurrentPos() - win.PositionFromLine(line) if col == 0: if win.GetLineEndPosition(line) > 0: win.execute_key('DEL') else: win.execute_key(wx.stc.STC_CMD_DELETEBACK) f = func else: return False else: return False else: if not ((31 < char < 127) or char > wx.WXK_PAGEDOWN): return False i = 0 win.BeginUndoAction() try: lastline = win.GetCurrentLine() while begin + i <= end: delta = win.PositionFromLine( begin + i) + col - win.GetLineEndPosition(begin + i) if delta > 0: win.GotoPos(win.GetLineEndPosition(begin + i)) win.AddText(' ' * delta) else: win.GotoPos(win.PositionFromLine(begin + i) + col) if f: f(win, begin + i) else: if 31 < char < 127: win.AddText(chr(char)) else: try: win.AddText(unichr(char)) except: error.error("Conver %d to unichar failed" % char) error.traceback() break if begin + i == lastline: lastpos = win.GetCurrentPos() i += 1 win.GotoPos(lastpos) finally: win.EndUndoAction() return True