Python论坛  - 讨论区

标题:[python-chinese] ANN:能够显示python calltips的VIM脚本(上次忘了带附件)

2004年08月28日 星期六 13:21

tocer tootoo at yeah.net
Sat Aug 28 13:21:58 HKT 2004

版本:0.3

运行平台:linux ,windows,VIM
          我只在中文win2000+vim6.3中测试通过。别的平台,别的vim版本没有测试,如果也能通过,告诉我一声。

特性:
    1、在vim里编码的时候,可以随着你的输入,自动显示模块或者函数名称和用法,就像idle或者pywin里的calltips,不过我这个脚本还能显示用法和帮助。
    2、只支持python语言。

用法:
    1、打开一个python文件,然后pyfile <路径>pyCallTips.py,如果没有报错,就能运行了,在python文件中import的模块和方法,都能够随着你的输入显示出来。支持内建函数,试着输入 dir 看看。或者放入vimrc文件中,自动运行。
    2、如果新增import 或者 from 。。import语句,需要按刷新一下,这样才能显示出新增的模块和方法

如果有问题,在这里提出来,我会尽量回答的。如果好使,也说一声,让我也高兴高兴。
-------------- next part --------------
"""
	PythonCallTips  0.3
	(C) Copyright 2004 by tocer deng dm-info at 163.com
					

	In a new bottow window, display help doc strings of word under
    the cursor by scanning the modules in the current file.

    It requier Vim compiled with "+python and MUST set iskeyword+=. ".
    
    It work well in my box in Win2000 and Vim6.3. If it work in other 
    platform or other version of Vim, let me know.
    
	Usage: 
        1.  open a python file, and execute:
             :pyfile  pyCallTips.py
            of cource be sure Vim can find it. the call tip would by displayed
            in the bottom window.
            Or put the command into .vimrc to autorun
        2.  if you input new "import xxx" or "from xxx import yyy" statement,
            you would press "F4" key to refresh it. Do it only, the new module's 
            calltips can be displayed.
		3.  You can change the key mapping at the bottom of this file as desired.

	TODO: 1.Read a Vim's WORD, and pick up right form
            e.g. "helpBuffer[1].append" --> "helpBuffer.append" 
          2.Declare variable as object in the comment, so analyzing it,
            and  import the object. Then sequence and dictionay's methed 
            help doc string could be display.

    English is not my native language, so there may be many mistake of expression.
    if you have any question, please mail to dm-info AT 163.COM
    Welcom to fix bug:)
"""

import vim
import __builtin__
from string import letters

def GetWordUnderCursor():
    """ Returns word under the current buffer's cursor.
    """
    return vim.eval('expand("")') 

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 help buffer
    '''
    helpBuffer[:] = None
    docs = GetHelp(GetWordUnderCursor())

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

    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
else:
    vim.command("silent botright new Python help")
    vim.command("set buftype=nofile")
    vim.command("set nonumber")
    vim.command("resize 5")
    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()")
ImportObject()

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

2004年08月28日 星期六 13:54

GreyRoar GreyRoar at tom.com
Sat Aug 28 13:54:24 HKT 2004

hoxide,您好!

	好像pydiction有类似的功能,tocer说的那个应该是pyCallTips,一个新提交的脚本。你在vim.sf.net就可以找到它们

======= 2004-08-28 12:41:00 您在来信中写道:=======

>tocer,您好!
>
>	好东西啊~~~ 在什么地方?
>
>======= 2004-08-28 11:14:38 您在来信中写道:=======
>
>>版本:0.3
>>
>>运行平台:linux ,windows,VIM
>>          我只在中文win2000+vim6.3中测试通过。别的平台,别的vim版本没有测试,如果也能通过,告诉我一声。
>>
>>特性:
>>    1、在vim里编码的时候,可以随着你的输入,自动显示模块或者函数名称和用法,就像idle或者pywin里的calltips,不过我这个脚本还能显示用法和帮助。
>>    2、只支持python语言。
>>
>>用法:
>>    1、打开一个python文件,然后pyfile <路径>pyCallTips.py,如果没有报错,就能运行了,在python文件中import的模块和方法,都能够随着你的输入显示出来。支持内建函数,试着输入 dir 看看。或者放入vimrc文件中,自动运行。
>>    2、如果新增import 或者 from 。。import语句,需要按刷新一下,这样才能显示出新增的模块和方法
>>
>>如果有问题,在这里提出来,我会尽量回答的。如果好使,也说一声,让我也高兴高兴。_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>
>
>= = = = = = = = = = = = = = = = = = = =
>			
>
>        致
>礼!
> 
>				 
>        hoxide
>        hoxide_dirac at yahoo.com.cn
>          2004-08-28
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

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

        致
礼!
 
				 
        GreyRoar
        GreyRoar at tom.com
          2004-08-28






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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号