From 852a4f4faffff798d4c7dfc32c6a957cb23b1efc 2006-11-02 09:51:53 From: vivainio Date: 2006-11-02 09:51:53 Subject: [PATCH] %cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo' --- diff --git a/IPython/Extensions/ipy_stock_completers.py b/IPython/Extensions/ipy_stock_completers.py index 645dea4..eb6bb3c 100755 --- a/IPython/Extensions/ipy_stock_completers.py +++ b/IPython/Extensions/ipy_stock_completers.py @@ -12,6 +12,7 @@ update upgrade """ import IPython.ipapi +import glob,os,shlex ip = IPython.ipapi.get() @@ -91,4 +92,28 @@ def svn_completer(self,event): return svn_commands.split() -ip.set_hook('complete_command', svn_completer, str_key = 'svn') \ No newline at end of file +ip.set_hook('complete_command', svn_completer, str_key = 'svn') + +def runlistpy(self, event): + comps = shlex.split(event.line) + relpath = (len(comps) > 1 and comps[-1] or '') + + print "rp",relpath + if relpath.startswith('~'): + relpath = os.path.expanduser(relpath) + dirs = [f.replace('\\','/') + "/" for f in glob.glob(relpath+'*') if os.path.isdir(f)] + pys = [f.replace('\\','/') for f in glob.glob(relpath+'*.py')] + return dirs + pys + +ip.set_hook('complete_command', runlistpy, str_key = '%run') + +def listdirs(self, event): + relpath = event.symbol + if relpath.startswith('~'): + relpath = os.path.expanduser(relpath).replace('\\','/') + found = [f.replace('\\','/')+'/' for f in glob.glob(relpath+'*') if os.path.isdir(f)] + if not found: + return [relpath] + return found + +ip.set_hook('complete_command', listdirs, str_key = '%cd') \ No newline at end of file diff --git a/IPython/completer.py b/IPython/completer.py index 27915df..1ea29b1 100644 --- a/IPython/completer.py +++ b/IPython/completer.py @@ -540,8 +540,19 @@ class IPCompleter(Completer): event.symbol = text cmd = line.split(None,1)[0] event.command = cmd + #print "\ncustom:{%s]\n" % event # dbg + + # for foo etc, try also to find completer for %foo + if not cmd.startswith(self.magic_escape): + try_magic = self.custom_completers.s_matches( + self.magic_escape + cmd) + else: + try_magic = [] + + for c in itertools.chain( self.custom_completers.s_matches(cmd), + try_magic, self.custom_completers.flat_matches(self.lbuf)): # print "try",c # dbg try: diff --git a/doc/ChangeLog b/doc/ChangeLog index adc85a5..cfe25cd 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2006-11-02 Ville Vainio + + * ipy_stock_completers.py: Add %run and %cd completers. + + * completer.py: Try running custom completer for both + "foo" and "%foo" if the command is just "foo". + 2006-10-31 Ville Vainio * strdispatch.py, completer.py, ipy_stock_completers.py: