Pythonising the vim (e.g. syntax popups) -> vimpst

sjdevnull at yahoo.com sjdevnull at yahoo.com
Thu Nov 10 12:36:43 EST 2005


Christoph Haas wrote:
> But Vim scripting looked even evil for me... and
> I've been working with Perl for a decade. :)

Vim scripting is nasty, but thankfully you don't really need to use it
any more. You can write all your code in python with just a one-line
hook to map it to a key.

On the topic, I did something like this that you might find handy; the
end result is that if you type
  a = myfunc(
then the arguments for myfunc are displayed in the status line.  If you
hit <F1>, then the full python docs (if any) are shown (I'm working on
having that show surrounding comments/docstrings for followed
functions, but that code is busted up something awful at the moment and
I haven't gone back to look at it).

It's a complete work in progress, it could be terribly broken.  I'll
try to get it more polished/functional and put it up on sourceforge or
somewhere.

The order of lookups for prototypes is as follows:
1. Try to follow tags; if there is a tag, grab the line in question
(and any further lines until reaching a ":").
2. Failing that, look in a help dictionary; I generated mine from the
info version of the Python docs, using a simple Python script.
3. Fail and show nothing.

A couple of caveats:
1. This uses the command area.  So you either need to turn off
"showmode" or set "cmdheight" to 2 or more; otherwise, the help text
will be clobbered immediately by the INSERT prompt.
2. The help dictionary is a dumb idea, it should be in a gdbm file or
something--right now it increases the size of a running gvim by 600KB
or so after the first time I use the help function (and that first
lookup is slow) because it's reading it a huge chunk of the vim docs.
3. The code abuses the preview window, which I don't care about because
I never use the preview window for anything else.  I would expect that
the user-visible effect of this will be to close any preview windows
you have open any time you type an open-paren.
4. It really works best in gvim; in a command-line vim it may recenter
your windows and have other jumpiness.  I tried for a while to figure
out a workaround and gave up.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

Vim code (put in ~/.vimrc)

function! DocParen()
    py vimrc.print_help()
    return "("
endfunction
inoremap <F1> <C-O>:echo b:helpString<CR>
inoremap ( <C-R>=DocParen()<CR><C-O>:echo b:shortHelp<CR>

Python code (put in ~/.vim/vimrc.py)
import vim

def cur_x():
    return vim.current.window.cursor[1]


def cur_y():
    return vim.current.window.cursor[0]

def snag_help(current):
    vim.command("let v:errmsg=''")
    vim.command("silent! ptag %s"%current)
    if vim.eval("v:errmsg"):
        return ""
    vim.command("silent! winc P")


    help = vim.current.line.strip()

    if help.startswith("def") or help.startswith("class"):
        while not vim.current.line.strip().endswith(":"):
            vim.command("+")
            help = help + " "+vim.current.line.strip()


    try:
        help = help.split("def ", 1)[1]
    except:
        pass
    if help.endswith(":"):
        help = help[:-1]
    vim.command("silent! winc p")
    vim.command("silent! winc z")
    return help


def print_help():
    global docDict
    if not docDict:
        import pyhelp
        docDict = pyhelp.docDict
    currWord = vim.current.line.strip()
    for ch in " .":
        try:
            currWord = currWord.split(ch)[-1]
        except:
            pass


    help snag_help(currWord)


    if not help:
        try:
            help = docDict[currWord]
        except:
            help = ""
    if help.find("(self, ") > 0:
        help=help.replace("(self, ", "(")
    if '"' in help:
        help=help.replace('"', '\\"')
    shortHelp = help
    if len(vim.windows) == 1:
        truncate = vim.current.window.width-20
    else:
        truncate = int(vim.eval("&columns")) -1
    if len(help)>truncate:
        shortHelp=help[:truncate]
    vim.command('let b:helpString="%s"'%help)
    vim.command('let b:shortHelp="%s"'%shortHelp)




More information about the Python-list mailing list