]> git.phdru.name Git - dotfiles.git/blob - .vim/python/completion.py
.vim/rc: Adapt python code to both python and python3
[dotfiles.git] / .vim / python / completion.py
1 # From http://slobin.pp.ru/vim/_vimrc.html
2 # Adapted to python3 by Oleg Broytman <phd@phdru.name>
3
4 import sys, rlcompleter, unicodedata, vim
5 from itertools import *
6 vim_complete = rlcompleter.Completer().complete
7
8 def vim_comp_list():
9   """Implementation of CompList() function"""
10   arglead = vim.eval("a:ArgLead")
11   fence = int(vim.eval("match(a:ArgLead, '\(\w\|\.\)*$')"))
12   left, right = arglead[:fence], arglead[fence:]
13   try:
14     completions = (vim_complete(right, i) for i in count())
15     candidates = list(takewhile(bool, completions))
16   except NameError:
17     candidates = []
18   suggestions = [left + x for x in candidates]
19   vim.command("return " + repr(suggestions))
20
21 def vim_calc(command):
22   """Implementation of :Calc command"""
23   global _
24   try:
25     result = eval(command)
26   except SyntaxError:
27     if sys.version_info[0] == 2:
28       exec(command in globals(), locals())
29     else:
30       exec(command, globals(), locals())
31   else:
32     if result != None:
33       print(result)
34       _ = result
35       xx = ''.join('\\x%02x' % ord(x) for x in str(_))
36       vim.command('let @" = "%s"' % xx)
37
38 def vim_pydo(command):
39   """Implementation of :Pydo command"""
40   codeobj = compile(command, "command", "eval")
41   line1 = vim.current.range.start
42   line2 = vim.current.range.end
43   delta = 0
44   for numz in range(line1, line2+1):
45     line = vim.current.buffer[numz-delta]
46     if sys.version_info[0] == 2:
47       uline = unicode(line, vim.eval('&fileencoding'))
48     else:
49       uline = line
50     num = numz + 1
51     words = line.split()
52     result = eval(codeobj, globals(), locals())
53     if result is None or result is False:
54       del vim.current.buffer[numz-delta]
55       delta += 1
56       continue
57     if isinstance(result, list) or isinstance(result, tuple):
58       result = " ".join(map(str, result))
59     else:
60       result = str(result)
61     vim.current.buffer[numz-delta] = result
62
63 def vim_unicode_name():
64   try:
65     char = vim.eval("matchstr(getline('.'), '.', col('.') - 1)")
66     if sys.version_info[0] == 2:
67         print(unicodedata.name(char.decode(vim.eval("&encoding"))))
68     else:
69         print(unicodedata.name(char))
70   except (AttributeError, ValueError) as target:
71     print("%s: %s" % (target.__class__.__name__, target))