def on_complete(self, dummy, modifier): # First split all the text in words text = self._entry.get_text() pos = self._entry.get_position() words = list(self._re_complete.finditer(text)) wordsstr = [] for word in words: spec = self._complete_word_match(word) wordsstr.append(spec[0]) # Find out at which word the cursor actually is # Examples: # * hello world| # * hello| world # * |hello world # * hello wor|ld # * hello | world # * "hello world|" posidx = None for idx in range(0, len(words)): spec = self._complete_word_match(words[idx]) if words[idx].start(0) > pos: # Empty space, new completion wordsstr.insert(idx, '') words.insert(idx, None) posidx = idx break elif spec[2] == pos: # At end of word, resume completion posidx = idx break elif spec[1] <= pos and spec[2] > pos: # In middle of word, do not complete return True if posidx == None: wordsstr.append('') words.append(None) posidx = len(wordsstr) - 1 # First word completes a command, if not in any special 'mode' # otherwise, relay completion to the command, or complete by advice # from the 'mode' (prompt) cmds = commands.Commands() if not self._command_state and posidx == 0: # Complete the first command ret = commands.completion.command(words=wordsstr, idx=posidx) else: complete = None realidx = posidx if not self._command_state: # Get the command first cmd = commands.completion.single_command(wordsstr, 0) realidx -= 1 ww = wordsstr[1:] else: cmd = self._command_state.top() ww = wordsstr if cmd: complete = cmd.autocomplete_func() if not complete: return True # 'complete' contains a dict with arg -> func to do the completion # of the named argument the command (or stack item) expects args, varargs = cmd.args() # Remove system arguments s = ['argstr', 'args', 'entry', 'view'] args = list(filter(lambda x: not x in s, args)) if realidx < len(args): arg = args[realidx] elif varargs: arg = '*' else: return True if not arg in complete: return True func = complete[arg] try: spec = utils.getargspec(func) if not ww: ww = [''] kwargs = {'words': ww, 'idx': realidx, 'view': self._view} if not spec.keywords: for k in list(kwargs.keys()): if not k in spec.args: del kwargs[k] ret = func(**kwargs) except Exception as e: # Can be number of arguments, or return values or simply buggy # modules print(e) traceback.print_exc() return True if not ret or not ret[0]: return True res = ret[0] completed = ret[1] if len(ret) > 2: after = ret[2] else: after = ' ' # Replace the word if words[posidx] == None: # At end of everything, just append spec = None self._entry.insert_text(completed, self._entry.get_text_length()) self._entry.set_position(-1) else: spec = self._complete_word_match(words[posidx]) self._entry.delete_text(spec[1], spec[2]) self._entry.insert_text(completed, spec[1]) self._entry.set_position(spec[1] + len(completed)) if len(res) == 1: # Full completion lastpos = self._entry.get_position() if not isinstance(res[0], commands.module.Module) or not res[0].commands(): if words[posidx] and after == ' ' and ( words[posidx].group(2) != None or words[posidx].group(3) != None): lastpos = lastpos + 1 self._entry.insert_text(after, lastpos) self._entry.set_position(lastpos + 1) elif completed == wordsstr[posidx] or not res[0].method: self._entry.insert_text('.', lastpos) self._entry.set_position(lastpos + 1) if self._info_window: self._info_window.destroy() else: # Show popup with completed items if self._info_window: self._info_window.clear() ret = [] for x in res: if isinstance(x, commands.method.Method): ret.append('<b>' + saxutils.escape(x.name) + '</b> (<i>' + x.oneline_doc() + '</i>)') else: ret.append(str(x)) self.info_show("\n".join(ret), True) return True
def on_complete(self, dummy, modifier): # First split all the text in words text = self._entry.get_text() pos = self._entry.get_position() words = list(self._re_complete.finditer(text)) wordsstr = [] for word in words: spec = self._complete_word_match(word) wordsstr.append(spec[0]) # Find out at which word the cursor actually is # Examples: # * hello world| # * hello| world # * |hello world # * hello wor|ld # * hello | world # * "hello world|" posidx = None for idx in xrange(0, len(words)): spec = self._complete_word_match(words[idx]) if words[idx].start(0) > pos: # Empty space, new completion wordsstr.insert(idx, '') words.insert(idx, None) posidx = idx break elif spec[2] == pos: # At end of word, resume completion posidx = idx break elif spec[1] <= pos and spec[2] > pos: # In middle of word, do not complete return True if posidx == None: wordsstr.append('') words.append(None) posidx = len(wordsstr) - 1 # First word completes a command, if not in any special 'mode' # otherwise, relay completion to the command, or complete by advice # from the 'mode' (prompt) cmds = commands.Commands() if not self._command_state and posidx == 0: # Complete the first command ret = commands.completion.command(words=wordsstr, idx=posidx) else: complete = None if not self._command_state: # Get the command first cmd = commands.completion.single_command(wordsstr, 0) else: cmd = self._command_state.top() if cmd: complete = cmd.autocomplete_func() if not complete: return True # 'complete' contains a dict with arg -> func to do the completion # of the named argument the command (or stack item) expects args, varargs = cmd.args() # Remove system arguments s = ['argstr', 'args', 'entry', 'view'] args = filter(lambda x: not x in s, args) if posidx - 1 < len(args): arg = args[posidx - 1] elif varargs: arg = '*' else: return True if not arg in complete: return True func = complete[arg] try: spec = utils.getargspec(func) kwargs = { 'words': wordsstr[1:], 'idx': posidx - 1, 'view': self._view } if not spec.keywords: for k in kwargs.keys(): if not k in spec.args: del kwargs[k] ret = func(**kwargs) except Exception, e: # Can be number of arguments, or return values or simply buggy # modules print e traceback.print_exc() return True
def on_complete(self, dummy, modifier): # First split all the text in words text = self._entry.get_text() pos = self._entry.get_position() words = list(self._re_complete.finditer(text)) wordsstr = [] for word in words: spec = self._complete_word_match(word) wordsstr.append(spec[0]) # Find out at which word the cursor actually is # Examples: # * hello world| # * hello| world # * |hello world # * hello wor|ld # * hello | world # * "hello world|" posidx = None for idx in xrange(0, len(words)): spec = self._complete_word_match(words[idx]) if words[idx].start(0) > pos: # Empty space, new completion wordsstr.insert(idx, '') words.insert(idx, None) posidx = idx break elif spec[2] == pos: # At end of word, resume completion posidx = idx break elif spec[1] <= pos and spec[2] > pos: # In middle of word, do not complete return True if posidx == None: wordsstr.append('') words.append(None) posidx = len(wordsstr) - 1 # First word completes a command, if not in any special 'mode' # otherwise, relay completion to the command, or complete by advice # from the 'mode' (prompt) cmds = commands.Commands() if not self._command_state and posidx == 0: # Complete the first command ret = commands.completion.command(words=wordsstr, idx=posidx) else: complete = None realidx = posidx if not self._command_state: # Get the command first cmd = commands.completion.single_command(wordsstr, 0) realidx -= 1 ww = wordsstr[1:] else: cmd = self._command_state.top() ww = wordsstr if cmd: complete = cmd.autocomplete_func() if not complete: return True # 'complete' contains a dict with arg -> func to do the completion # of the named argument the command (or stack item) expects args, varargs = cmd.args() # Remove system arguments s = ['argstr', 'args', 'entry', 'view'] args = filter(lambda x: not x in s, args) if realidx < len(args): arg = args[realidx] elif varargs: arg = '*' else: return True if not arg in complete: return True func = complete[arg] try: spec = utils.getargspec(func) if not ww: ww = [''] kwargs = {'words': ww, 'idx': realidx, 'view': self._view} if not spec.keywords: for k in kwargs.keys(): if not k in spec.args: del kwargs[k] ret = func(**kwargs) except Exception, e: # Can be number of arguments, or return values or simply buggy # modules print e traceback.print_exc() return True
def on_complete(self, dummy, modifier): # First split all the text in words text = self._entry.get_text() pos = self._entry.get_position() words = list(self._re_complete.finditer(text)) wordsstr = [] for word in words: spec = self._complete_word_match(word) wordsstr.append(spec[0]) # Find out at which word the cursor actually is # Examples: # * hello world| # * hello| world # * |hello world # * hello wor|ld # * hello | world # * "hello world|" posidx = None for idx in range(0, len(words)): spec = self._complete_word_match(words[idx]) if words[idx].start(0) > pos: # Empty space, new completion wordsstr.insert(idx, "") words.insert(idx, None) posidx = idx break elif spec[2] == pos: # At end of word, resume completion posidx = idx break elif spec[1] <= pos and spec[2] > pos: # In middle of word, do not complete return True if posidx == None: wordsstr.append("") words.append(None) posidx = len(wordsstr) - 1 # First word completes a command, if not in any special 'mode' # otherwise, relay completion to the command, or complete by advice # from the 'mode' (prompt) cmds = commands.Commands() if not self._command_state and posidx == 0: # Complete the first command ret = commands.completion.command(words=wordsstr, idx=posidx) else: complete = None realidx = posidx if not self._command_state: # Get the command first cmd = commands.completion.single_command(wordsstr, 0) realidx -= 1 ww = wordsstr[1:] else: cmd = self._command_state.top() ww = wordsstr if cmd: complete = cmd.autocomplete_func() if not complete: return True # 'complete' contains a dict with arg -> func to do the completion # of the named argument the command (or stack item) expects args, varargs = cmd.args() # Remove system arguments s = ["argstr", "args", "entry", "view"] args = list(filter(lambda x: not x in s, args)) if realidx < len(args): arg = args[realidx] elif varargs: arg = "*" else: return True if not arg in complete: return True func = complete[arg] try: spec = utils.getargspec(func) if not ww: ww = [""] kwargs = {"words": ww, "idx": realidx, "view": self._view} if not spec.keywords: for k in list(kwargs.keys()): if not k in spec.args: del kwargs[k] ret = func(**kwargs) except Exception as e: # Can be number of arguments, or return values or simply buggy # modules print(e) traceback.print_exc() return True if not ret or not ret[0]: return True res = ret[0] completed = ret[1] if len(ret) > 2: after = ret[2] else: after = " " # Replace the word if words[posidx] == None: # At end of everything, just append spec = None self._entry.insert_text(completed, self._entry.get_text_length()) self._entry.set_position(-1) else: spec = self._complete_word_match(words[posidx]) self._entry.delete_text(spec[1], spec[2]) self._entry.insert_text(completed, spec[1]) self._entry.set_position(spec[1] + len(completed)) if len(res) == 1: # Full completion lastpos = self._entry.get_position() if not isinstance(res[0], commands.module.Module) or not res[0].commands(): if ( words[posidx] and after == " " and (words[posidx].group(2) != None or words[posidx].group(3) != None) ): lastpos = lastpos + 1 self._entry.insert_text(after, lastpos) self._entry.set_position(lastpos + 1) elif completed == wordsstr[posidx] or not res[0].method: self._entry.insert_text(".", lastpos) self._entry.set_position(lastpos + 1) if self._info_window: self._info_window.destroy() else: # Show popup with completed items if self._info_window: self._info_window.clear() ret = [] for x in res: if isinstance(x, commands.method.Method): ret.append("<b>" + saxutils.escape(x.name) + "</b> (<i>" + x.oneline_doc() + "</i>)") else: ret.append(str(x)) self.info_show("\n".join(ret), True) return True
def func_props(self): if not self._func_props: # Introspect the function arguments self._func_props = utils.getargspec(self.method) return self._func_props