示例#1
0
def grepall():
    filelst = proj_openall.filelist()
    command(":call NextWin()")
    pat = eval("input('pattern: ')")
    if pat:
        try:
            command(":vimgrep %s %s" % (pat, join(filelst)))
        except error:
            pass  # print "Not Found"
示例#2
0
def genpydoc():
    """Generate docs."""
    html = potl = False
    if int(eval("exists('a:1')")):
        if eval("a:1") == "html":
            html = True
        elif eval("a:1") == "potl":
            potl = True

    filelst = [f for f in proj_openall.filelist() if f.endswith(".py")]

    # get project or dir name
    pname = "Project"
    l = current.line
    if l.strip():
        if l.startswith("---"):
            pname = l.strip("- ").capitalize()
        else:
            l = l.strip(' '+os.sep)
            pname = l.split(os.sep)[-1].capitalize()

    out = []
    for fn in filelst:
        # import module
        path = expanduser(fn).split(sep)
        path, fname = join(path[:-1], sep), path[-1]
        sys.path.append(path)
        m = __import__(fname.split('.')[0])
        reload(m)   # if file was changed, it's not autoreloaded even if function is rerun
        memb = getmembers(m)

        # make a list of class / def names
        src = open(expanduser(fn)).read()
        lst = re.findall(r"\n\s*(class|def) ([a-zA-Z0-9_]+)(:|\()", src)
        lst = [x[1] for x in lst]

        # filename and docstring
        if html:
            out.extend( ["<h4>%s</h4>" % fname, ''] )
        elif potl:
            out.extend( ['· '+fn] )
        else:
            out.append('#'*78)
            out.extend( ['# '+fn, ''] )
        mdoc = getdoc(m).split('\n')

        if html:
            if len(mdoc) == 1: out.extend( ['<p>%s</p>' % mdoc[0],  "<br /><br />"] )
            else: out.extend(["<br/><p>"] + [join(mdoc, "<br/>")] + ["</p><br/>", "<br/><br/>"])
        elif potl:
            if len(mdoc) == 1:
                out.extend( [' '*4 + '"""%s"""' % mdoc[0]] )
            else:
                quote = [' '*4 + '"""']
                out.extend(quote + [' '*4 + ln for ln in mdoc] + quote)
        else:
            if len(mdoc) == 1: out.extend( ['"""%s"""' % mdoc[0],  ''] )
            else: out.extend( ['"""'] + mdoc + ['"""', ''] )

        # output each class and def and their docstrings
        last = None
        for item in lst:
            if hasattr(m, item):
                item = getattr(m, item)
                last = item
            else:
                item = getattr(last, item)

            cls_def = getsourcelines(item)[0][0]
            indent = cls_def.index(cls_def.lstrip())
            if html: indentsp = '&nbsp;'*indent
            else: indentsp = ' '*(indent+4)

            # does not support multiline defs but they are bad style?
            if html:
                out.append("%s<div id='def'>%s</div>" % (indentsp, cls_def))
                indentsp = '&nbsp;'*(indent+4)*2
            elif potl:
                out.append(' '*4 + '· ' + cls_def)
            else:
                out.append(cls_def)

            # output docstring with indentation
            idoc = getdoc(item)
            if idoc:
                idoc = idoc.split('\n')
                if html:
                    if len(idoc) == 1:
                        out.append('%s<p>%s</p><br />' % (indentsp, idoc[0]))
                    else:
                        idoc = [indentsp + l for l in idoc]
                        quote = ['%s<br />' % indentsp]
                        out.extend(quote +  ["<p>%s</p><br />" % join(idoc)] + quote)
                else:
                    if potl: indentsp += ' '*4
                    if len(idoc) == 1:
                        out.append('%s"""%s"""' % (indentsp, idoc[0]))
                    else:
                        idoc = [indentsp + l for l in idoc]
                        quote = ['%s"""' % indentsp]
                        out.extend(quote + idoc + quote)

            if html: out.append("<br />")
            elif not potl: out.append('')

        if potl:
            pass
        elif not html:
            out.extend(['', ''])

    if not out: return

    if html:
        fp = open(outfn % pname, 'w')
        fp.write(htmltpl % (pname, pname, join(out, '\n')))
        fp.close()
        os.system("%s %s" % (browser, outfn % pname))
    else:
        # go to main window area, create new buffer, write output, set filetype
        command("call NextWin()")
        command("new")
        current.buffer[:] = out
        command("normal gg")
        if potl:
            command("set ft=potl bt=nofile")
        else:
            command("set ft=python bt=nofile")
示例#3
0
def unload_all():
    filelst = proj_openall.filelist()
    if not filelst:
        l = current.line.strip()
        if l and not l.startswith('#'):
            filelst = [l]

    if not filelst: return

    # make list of all listed buffers
    bnames = [b.name for b in buffers if b.name and not b.name.endswith(".proj")]
    tmp = [x.split('/')[-1] for x in bnames]

    bnums = []
    for bn in bnames:
        try: bnums.append(eval("bufnr('%s')" % bn))
        except: continue
    bnums = [b for b in bnums if int(eval("buflisted(%s)" % b)) > 0]
    project_buf = eval("bufnr('%')")
    command("call NextWin()")
    currb = eval("bufnr('%')")

    # make list of buf numbers we need to unload
    flbnums = []
    for bn in filelst:
        try: flbnums.append(eval("bufnr('%s')" % bn))
        except: continue

    flbnums = [n for n in flbnums if n != '-1']

    # if current buffer needs to be unloaded, we need to switch to some other buffer
    # to preserve windows layout
    switch_to = None
    for bn in bnums:
        if bn not in flbnums:
            switch_to = bn
            break

    # unload buffers
    for fn in filelst:
        try: bnr = eval("bufnr('%s')" % expanduser(fn))
        except: continue
        if int(eval("buflisted(%s)" % bnr)):
            command(":b %s" % bnr)
            if not int(eval("&modified")):
                if switch_to: command(":b %s" % switch_to)
                else: command("bnext")

                try:
                    command("bdelete %s" % bnr)
                except:
                    pass
            else:
                print "'%s' has unsaved changes" % fn

    # try to switch to original buffer; go back to project window
    if currb not in flbnums:
        command(":b %s" % currb)
    for x in range(10):
        command("call NextWin()")
        if eval("bufnr('%')") == project_buf:
            break