##// END OF EJS Templates
stock completer for hg (mercurial)
vivainio -
r445:a94977c0
parent child
Show More
@@ -1,133 +1,155
1 """ Tab completion support for a couple of linux package managers
1 """ Tab completion support for a couple of linux package managers
2
2
3 This is also an example of how to write custom completer plugins
3 This is also an example of how to write custom completer plugins
4 or hooks.
4 or hooks.
5
5
6 Practical use:
6 Practical use:
7
7
8 [ipython]|1> import ipy_linux_package_managers
8 [ipython]|1> import ipy_linux_package_managers
9 [ipython]|2> apt-get u<<< press tab here >>>
9 [ipython]|2> apt-get u<<< press tab here >>>
10 update upgrade
10 update upgrade
11 [ipython]|2> apt-get up
11 [ipython]|2> apt-get up
12
12
13 """
13 """
14 import IPython.ipapi
14 import IPython.ipapi
15 import glob,os,shlex,sys
15 import glob,os,shlex,sys
16
16
17 ip = IPython.ipapi.get()
17 ip = IPython.ipapi.get()
18
18
19 def apt_completers(self, event):
19 def apt_completers(self, event):
20 """ This should return a list of strings with possible completions.
20 """ This should return a list of strings with possible completions.
21
21
22 Note that all the included strings that don't start with event.symbol
22 Note that all the included strings that don't start with event.symbol
23 are removed, in order to not confuse readline.
23 are removed, in order to not confuse readline.
24
24
25 """
25 """
26 # print event # dbg
26 # print event # dbg
27
27
28 # commands are only suggested for the 'command' part of package manager
28 # commands are only suggested for the 'command' part of package manager
29 # invocation
29 # invocation
30
30
31 cmd = (event.line + "<placeholder>").rsplit(None,1)[0]
31 cmd = (event.line + "<placeholder>").rsplit(None,1)[0]
32 # print cmd
32 # print cmd
33 if cmd.endswith('apt-get') or cmd.endswith('yum'):
33 if cmd.endswith('apt-get') or cmd.endswith('yum'):
34 return ['update', 'upgrade', 'install', 'remove']
34 return ['update', 'upgrade', 'install', 'remove']
35
35
36 # later on, add dpkg -l / whatever to get list of possible
36 # later on, add dpkg -l / whatever to get list of possible
37 # packages, add switches etc. for the rest of command line
37 # packages, add switches etc. for the rest of command line
38 # filling
38 # filling
39
39
40 raise IPython.ipapi.TryNext
40 raise IPython.ipapi.TryNext
41
41
42
42
43 # re_key specifies the regexp that triggers the specified completer
43 # re_key specifies the regexp that triggers the specified completer
44
44
45 ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get')
45 ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get')
46 ip.set_hook('complete_command', apt_completers, re_key = '.*yum')
46 ip.set_hook('complete_command', apt_completers, re_key = '.*yum')
47
47
48 pkg_cache = None
48 pkg_cache = None
49
49
50 def module_completer(self,event):
50 def module_completer(self,event):
51 """ Give completions after user has typed 'import' """
51 """ Give completions after user has typed 'import' """
52
52
53 # only a local version for py 2.4, pkgutil has no walk_packages() there
53 # only a local version for py 2.4, pkgutil has no walk_packages() there
54 if sys.version_info < (2,5):
54 if sys.version_info < (2,5):
55 for el in [f[:-3] for f in glob.glob("*.py")]:
55 for el in [f[:-3] for f in glob.glob("*.py")]:
56 yield el
56 yield el
57 return
57 return
58
58
59 global pkg_cache
59 global pkg_cache
60 import pkgutil,imp,time
60 import pkgutil,imp,time
61 #current =
61 #current =
62 if pkg_cache is None:
62 if pkg_cache is None:
63 print "\n\n[Standby while scanning modules, this can take a while]\n\n"
63 print "\n\n[Standby while scanning modules, this can take a while]\n\n"
64 pkg_cache = list(pkgutil.walk_packages())
64 pkg_cache = list(pkgutil.walk_packages())
65
65
66 already = set()
66 already = set()
67 for ld, name, ispkg in pkg_cache:
67 for ld, name, ispkg in pkg_cache:
68 if name.count('.') < event.symbol.count('.') + 1:
68 if name.count('.') < event.symbol.count('.') + 1:
69 if name not in already:
69 if name not in already:
70 already.add(name)
70 already.add(name)
71 yield name + (ispkg and '.' or '')
71 yield name + (ispkg and '.' or '')
72 return
72 return
73
73
74 ip.set_hook('complete_command', module_completer, str_key = 'import')
74 ip.set_hook('complete_command', module_completer, str_key = 'import')
75 ip.set_hook('complete_command', module_completer, str_key = 'from')
75 ip.set_hook('complete_command', module_completer, str_key = 'from')
76
76
77 svn_commands = """\
77 svn_commands = """\
78 add blame praise annotate ann cat checkout co cleanup commit ci copy
78 add blame praise annotate ann cat checkout co cleanup commit ci copy
79 cp delete del remove rm diff di export help ? h import info list ls
79 cp delete del remove rm diff di export help ? h import info list ls
80 lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit
80 lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit
81 pe propget pget pg proplist plist pl propset pset ps resolved revert
81 pe propget pget pg proplist plist pl propset pset ps resolved revert
82 status stat st switch sw unlock update
82 status stat st switch sw unlock update
83 """
83 """
84
84
85 def svn_completer(self,event):
85 def svn_completer(self,event):
86 if len((event.line + 'placeholder').split()) > 2:
86 if len((event.line + 'placeholder').split()) > 2:
87 # the rest are probably file names
87 # the rest are probably file names
88 return ip.IP.Completer.file_matches(event.symbol)
88 return ip.IP.Completer.file_matches(event.symbol)
89
89
90 return svn_commands.split()
90 return svn_commands.split()
91
91
92 ip.set_hook('complete_command', svn_completer, str_key = 'svn')
92 ip.set_hook('complete_command', svn_completer, str_key = 'svn')
93
93
94 hg_commands = """
95 add addremove annotate archive backout branch branches bundle cat
96 clone commit copy diff export grep heads help identify import incoming
97 init locate log manifest merge outgoing parents paths pull push
98 qapplied qclone qcommit qdelete qdiff qfold qguard qheader qimport
99 qinit qnew qnext qpop qprev qpush qrefresh qrename qrestore qsave
100 qselect qseries qtop qunapplied recover remove rename revert rollback
101 root serve showconfig status strip tag tags tip unbundle update verify
102 version
103 """
104
105 def hg_completer(self,event):
106 """ Completer for mercurial commands """
107 if len((event.line + 'placeholder').split()) > 2:
108 # the rest are probably file names
109 return ip.IP.Completer.file_matches(event.symbol)
110
111 return hg_commands.split()
112
113 ip.set_hook('complete_command', hg_completer, str_key = 'hg')
114
115
94 def runlistpy(self, event):
116 def runlistpy(self, event):
95 comps = shlex.split(event.line)
117 comps = shlex.split(event.line)
96 relpath = (len(comps) > 1 and comps[-1] or '')
118 relpath = (len(comps) > 1 and comps[-1] or '')
97
119
98 #print "rp",relpath # dbg
120 #print "rp",relpath # dbg
99 lglob = glob.glob
121 lglob = glob.glob
100 isdir = os.path.isdir
122 isdir = os.path.isdir
101 if relpath.startswith('~'):
123 if relpath.startswith('~'):
102 relpath = os.path.expanduser(relpath)
124 relpath = os.path.expanduser(relpath)
103 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*')
125 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*')
104 if isdir(f)]
126 if isdir(f)]
105 pys = [f.replace('\\','/') for f in lglob(relpath+'*.py')]
127 pys = [f.replace('\\','/') for f in lglob(relpath+'*.py')]
106 return dirs + pys
128 return dirs + pys
107
129
108 ip.set_hook('complete_command', runlistpy, str_key = '%run')
130 ip.set_hook('complete_command', runlistpy, str_key = '%run')
109
131
110 def cd_completer(self, event):
132 def cd_completer(self, event):
111 relpath = event.symbol
133 relpath = event.symbol
112
134
113 if '-b' in event.line:
135 if '-b' in event.line:
114 # return only bookmark completions
136 # return only bookmark completions
115 bkms = self.db.get('bookmarks',{})
137 bkms = self.db.get('bookmarks',{})
116 return bkms.keys()
138 return bkms.keys()
117
139
118 if event.symbol == '-':
140 if event.symbol == '-':
119 # jump in directory history by number
141 # jump in directory history by number
120 ents = ['-%d [%s]' % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
142 ents = ['-%d [%s]' % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
121 if len(ents) > 1:
143 if len(ents) > 1:
122 return ents
144 return ents
123 return []
145 return []
124
146
125
147
126 if relpath.startswith('~'):
148 if relpath.startswith('~'):
127 relpath = os.path.expanduser(relpath).replace('\\','/')
149 relpath = os.path.expanduser(relpath).replace('\\','/')
128 found = [f.replace('\\','/')+'/' for f in glob.glob(relpath+'*') if os.path.isdir(f)]
150 found = [f.replace('\\','/')+'/' for f in glob.glob(relpath+'*') if os.path.isdir(f)]
129 if not found:
151 if not found:
130 return [relpath]
152 return [relpath]
131 return found
153 return found
132
154
133 ip.set_hook('complete_command', cd_completer, str_key = '%cd')
155 ip.set_hook('complete_command', cd_completer, str_key = '%cd')
General Comments 0
You need to be logged in to leave comments. Login now