Python论坛  - 讨论区

标题:[python-chinese] ANN: pyCallTips 0.5 released

2004年08月30日 星期一 16:17

tocer tootoo at yeah.net
Mon Aug 30 16:17:11 HKT 2004

介绍:这个脚本(见附件)用于Vim,类似于代码提示。

0.5改动:
1、增强了0.3的功能
2、修正了0.3中的一些错误
3、增加了5个选择键  ...,可以选择calltips 窗口的候选词,感觉有点像输入法了。用用就知道了
-------------- next part --------------
'''
	PythonCallTips  0.5
    (C) Copyright 2004 by tocer deng   MAIL: write2tocer at homtmail.com
					
	This script simualate code calltips in a new bottow window of Vim.
    In fact, it display python help doc strings of word under the cursor
    by scanning the imported modules in the current file.

    It require Python and Vim compiled with "+python" 
    and MAKE SURE "set iskeyword+=."
    
    It work well in my box in Win2000 + Vim6.3 + python2.3. 
    If it can work in other platform or other version of Vim, let me know.
    Note: as I know, Vim6.2 come into conflict with Python2.3, becase Vim6.2
          is compiled with Python2.1. you can update vim version to 6.3.
          Maybe yours not.
          
	Install:
        1. The easiest method is that throwing it into path $VIRUNTIME/plugin.
        2. the easier method is that putting below the sentence into "vimrc" file.
           (in linux, '/' instead of '\\'). So once you open a python file, the 
           script begin running. (SUGGESTION)
           
           autocmd FileType python pyfile \\pyCallTips.py
           
           note: is the path of pyCallTips.py.
           
        3. Or opening a python file you requied, and execute:
             
             :pyfile  pyCallTips.py
             
        Notes: 
           1. It split a new little window in the bottom in Vim when the script
              begin running. This is the python calltips windows.
           2. DO NOT "close" the window, otherwise it can't work.
            
     Usage: coding and coding and nothing:)
            
        1. Once you type new "import xxx" or "from xxx import yyy" statement,
           you MUST press "F4" key. I call it "refresh key". This key tell the
           script read the whole file again to get modules and methods.
           Otherwise the calltips don't be display.
            
        2. Except "F4", the script also remap five keys:  ...  as
           "auto complete key". As you know, Vim use  and  to 
           complete word automatically. But when you input word like 
           "module.method(xxx).y", you wish Vim could complete the rest part the
           word automatically. In fatc, the Vim can't.(I have think for a long 
           time, but still don't know how to do. If you know, mail to me,thanks)
           So, I deside to remap other keys to complete word automatically.
           using  ...  key to select which one to complete from
           calltips window. If you still don't see, try it yourself.
        3. If the keys that the script remap come into conflict with yours,
           you can change the key mapping in this file as desired.

	TODO: 
        1. Make all builtin object type display its calltips.
           I think declare them in somewhere, and analyzing them.
           
        2. If a method evaluate to a variable, it should display calltips also.
        
        3. Solve  can't complete automatically. see below.
           But it's urgent.

        4. Probably it extend other function, even language like python. 
           But I know python only now. Maybe you can do it:)

    DEBUG:
          1. The script don't know which one a variable is:
             list, dict, tuple or stings

    English is not my native language, so there may be many mistakes of expression.
    If you have any question, feel free to mail to write2tocer at homtmail.com
    If you enjoy it, mail me too, tell me where are you from if you don't mind.
    I'm happy to share your joy.
'''

import vim
import __builtin__
from string import letters
from sys import path

def __GetWordUnderCursor():
    """ Returns word under the current buffer's cursor.
    """
    stack = []
    leftword = rightword = popword = word = ''
    WORD = vim.eval('expand("")')  #return a big WORD

    #seperate the WORD into left part and right part
    rindex = WORD.rfind('.')
    if rindex == -1:      #if a WORD is not include '.'
        leftWORD = ''
        rightWORD = WORD
    else:
        leftWORD = WORD[:rindex]        
        rightWORD = WORD[rindex+1:]
    #print "WORD=%s, leftWORD=%s rightWORD=%s" % (WORD, leftWORD, rightWORD)

    #analyzing left part of the WORD
    for char in leftWORD:
        if char in letters + '.':
            popword += char
            continue
        elif char == '(' or char == '[':
            stack.append(popword)
            popword = ''
            continue
        elif char == ')' or char == ']':
            leftword=stack.pop()
            popword = ''
            continue
        else:
            popword = ''
    if popword != '': leftword = popword  
    #print "leftword=%s" % leftword

    #analyzing right part of the WORD
    for char in rightWORD:
        if char not in letters:
            break
        rightword += char
    #print "rightword=%s" % rightword
    
    if leftword != '':
        word = "%s.%s" % (leftword, rightword)
    else:
        word = rightword
    print word

    return word

def AutoComplete(index):
    try:
        #get first field of index line of helpBuffer
        if helpBuffer[index-1].find('__builtin__') == 0:  #see if builtin function
            autoPart = helpBuffer[index-1].split()[0][len(__GetWordUnderCursor())+12:]  #11: len('__biultin__.')
        else:
            autoPart = helpBuffer[index-1].split()[0][len(__GetWordUnderCursor()):]
        vim.command('execute "normal a' + autoPart + '"')
    except:
        #print "auto complete ERROR"
        pass
    vim.command('startinsert!')   #from normal mode into insert mode
    #print autoPart

def __GetHelp(word):   
    '''Get methods's  doc strings.'''
    
    helpDoc = []
    spacing=16
    if len(word.split('.')) == 1:
        s = '__builtin__'
        m = word
    else:
        rindex= word.rfind('.')
        s = word[:rindex]
        m = word[rindex+1:]
    try:
        object = eval(s)
    except:
        return []
    joinLineFunc = lambda s: " ".join(s.split())

    methodList = [method for method in dir(object) \
                         if method.find('__') != 0 and \
                            method.find(m) == 0    and \
                            '__doc__' in dir(getattr(object, method))]
    helpDoc.extend(["%s.%s %s" %
                    (s,method.ljust(spacing),
                     joinLineFunc(str(getattr(object, method).__doc__)))
                     for method in methodList])
    return helpDoc

def ImportObject():
    '''Check module and function in whole current buffer,
       and import it.
    '''            
    importList = []
    for line in vim.current.buffer:
        words = line.split()
        if len(words) > 0 and (words[0] == 'import' or words[0] == 'from'):
            try:
                exec(line.strip())  in globals()
            except:
                print "Error import : %s" % line
    return 

def CallTips():
    '''Display help doc string in Python calltips buffer
    '''
    helpBuffer[:] = None
    docs = __GetHelp(__GetWordUnderCursor())

    #print docs
    for line in docs:
        helpBuffer.append(line)
    del helpBuffer[0]

    #from normal mode into insert mode
    y, x = vim.current.window.cursor
    if len(vim.current.line) > x + 1:
        vim.command('normal l')
        vim.command('startinsert')
    else:
        vim.command('startinsert!')
        

if 'helpBuffer' in dir():
        helpBuffer[:] = None  #clear help buffer
        ImportObject()
else:
    vim.command("silent botright new Python CallTips Windows")
    vim.command("set buftype=nofile")
    vim.command("set nonumber")
    vim.command("resize 5")
    vim.command("set noswapfile")
    helpBuffer = vim.current.buffer
    vim.command("wincmd p")   #switch back window
    #mapping "a-zA-Z." keys
    for letter in letters+'.':    
        vim.command("inoremap  %s %s:python CallTips()" \
                    % (letter, letter))
    #mapping "Back Space" keys
    vim.command("inoremap   :python CallTips()")

    #mapping "F4" keys
    vim.command("inoremap   :python ImportObject()")

    #mapping "Alt 1" key
    vim.command("inoremap   :python AutoComplete(1)")

    #mapping "Alt 2" key
    vim.command("inoremap   :python AutoComplete(2)")

    #mapping "Alt 3" key
    vim.command("inoremap   :python AutoComplete(3)")

    #mapping "Alt 4" key
    vim.command("inoremap   :python AutoComplete(4)")

    #mapping "Alt 5" key
    vim.command("inoremap   :python AutoComplete(5)")

    path.extend(['.','..'])  #add current path and parent path
    del path, letter

    ImportObject()

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月30日 星期一 16:18

Gary Jia gary at exoweb.net
Mon Aug 30 16:18:31 HKT 2004

哦,呵呵,没问题,等我可以了,就业邀请大家,不过我这个人向来运气差,所有需要
凭运气的事都跟我没有关系,
长这么大还没有中过彩票呢,呵呵,连5元钱都没有得过,呵呵。 

-----邮件原件-----
发件人: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] 代表 Jerry Marx
发送时间: 2004年8月30日 16:10
收件人: python-chinese at lists.python.cn
主题: Re: 答复: [python-chinese]Gmail邀请

Gary Jia,您好!

	过一段时间如果发现label下面有一行红字提示你说你可以邀请你的朋友的时
候
	就可以了.不过好像是随机出现的.

======= 2004-08-30 15:48:37 您在来信中写道:=======

>我已经申请了,但是还没有看见在那儿邀请别人,等我找到了,就告诉大家。
>
>-----邮件原件-----
>发件人: python-chinese-bounces at lists.python.cn
>[mailto:python-chinese-bounces at lists.python.cn] 代表 Liming_Do
>发送时间: 2004年8月30日 15:39
>收件人: python-chinese at lists.python.cn
>主题: 答复: [python-chinese]Gmail邀请
>
>哎,等我发完信,黄瓜菜都没啦。
>哪位老大还有Gmail的邀请?
>
>-----原始邮件-----
>发件人: python-chinese-bounces at lists.python.cn
>[mailto:python-chinese-bounces at lists.python.cn]代表 Jerry Marx
>发送时间: 2004年8月30日 13:59
>收件人: python-chinese at lists.python.cn
>主题: Re: [python-chinese]Gmail邀请
>
>
>Jerry Marx,您好!
>
>	已经送完了  :)
>	大家检查一下email
>
>======= 2004-08-30 13:05:59 您在来信中写道:=======
>
>>python-chinese,您好!
>>
>>	有需要gmail的兄弟们发信到pythoner at gmail.com
>>	为数不多,先到先得,请使用你的常用信箱来发.
>>	不过不要使用hotmail和yahoo.
>>
>>        致
>>礼!
>> 				
>>
>>        Jerry Marx
>>        zhiyuan.ma at enorbus.com.cn
>>          2004-08-30
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>
>
>= = = = = = = = = = = = = = = = = = = =
>			
>
>        致
>礼!
> 
>				 
>        Jerry Marx
>        zhiyuan.ma at enorbus.com.cn
>          2004-08-30
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        Jerry Marx
        zhiyuan.ma at enorbus.com.cn
          2004-08-30


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号