Chinaunix首页 | 论坛 | 博客
  • 博客访问: 361549
  • 博文数量: 42
  • 博客积分: 3970
  • 博客等级: 中校
  • 技术积分: 714
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-13 15:31
文章分类

全部博文(42)

文章存档

2009年(19)

2008年(23)

我的朋友

分类: LINUX

2008-03-22 00:14:30

字体变小 字体变大
下载python_fold插件,解压后是python_fold.vim文件,
放入plugin目录下。即可实现Python代码的折叠支持。再次打开Python脚本时会发现所有的代码已经折叠了,其中还现了折叠部分拥有的行数。在折叠的行按下zo可以打开折叠,按下zc会折叠上代码。(
在 vimrc
set foldmethod=indent



 
zo 將游標所在處的折疊打開。open。
zc 將游標所在處已打開的內容再度折疊起來。close。
zr 將全文的所有折疊依層次通通打開。reduce。
zm 將全文已打開的折疊依層次通通再折疊起來。more。
zR 作用和 zr 同,但會打開含巢狀折疊(折疊中又還有折疊)的所有折疊。
zM 作用和 zm 同,但對於巢狀折疊亦有作用。
zi 這是個切換,是折疊與不折疊指令間的切換。
zn 打開全文的所有折疊。fold none。
zN 這是 zn 的相對指令,回復所有的折疊。
" Vim folding file
" Language:    Python
" Author:    Jorrit Wiersma (foldexpr), Max Ischenko (foldtext), Robert
" Ames (line counts)
" Last Change:    2005 Jul 14
" Version:    2.3
" Bug fix:    Drexler Christopher, Tom Schumm, Geoff Gerrietts


setlocal foldmethod=expr
setlocal foldexpr=GetPythonFold(v:lnum)
setlocal foldtext=PythonFoldText()


function! PythonFoldText()
  let line = getline(v:foldstart)
  let nnum = nextnonblank(v:foldstart + 1)
  let nextline = getline(nnum)
  if nextline =~ '^\s\+"""$'
    let line = line . getline(nnum + 1)
  elseif nextline =~ '^\s\+"""'
    let line = line . ' ' . matchstr(nextline, '"""\zs.\{-}\ze\("""\)\?$')
  elseif nextline =~ '^\s\+"[^"]\+"$'
    let line = line . ' ' . matchstr(nextline, '"\zs.*\ze"')
  elseif nextline =~ '^\s\+pass\s*$'
    let line = line . ' pass'
  endif
  let size = 1 + v:foldend - v:foldstart
  if size < 10
    let size = " " . size
  endif
  if size < 100
    let size = " " . size
  endif
  if size < 1000
    let size = " " . size
  endif
  return size . " lines: " . line
endfunction


function! GetPythonFold(lnum)
    " Determine folding level in Python source
    "
    let line = getline(a:lnum)
    let ind  = indent(a:lnum)

    " Ignore blank lines
    if line =~ '^\s*$'
    return "="
    endif

    " Ignore triple quoted strings
    if line =~ "(\"\"\"|''')"
    return "="
    endif

    " Ignore continuation lines
    if line =~ '\\$'
    return '='
    endif

    " Support markers
    if line =~ '{{{'
    return "a1"
    elseif line =~ '}}}'
    return "s1"
    endif

    " Classes and functions get their own folds
    if line =~ '^\s*\(class\|def\)\s'
    return ">" . (ind / &sw + 1)
    endif

    let pnum = prevnonblank(a:lnum - 1)

    if pnum == 0
    " Hit start of file
    return 0
    endif

    " If the previous line has foldlevel zero, and we haven't increased
    " it, we should have foldlevel zero also
    if foldlevel(pnum) == 0
    return 0
    endif

    " The end of a fold is determined through a difference in indentation
    " between this line and the next.
    " So first look for next line
    let nnum = nextnonblank(a:lnum + 1)
    if nnum == 0
    return "="
    endif

    " First I check for some common cases where this algorithm would
    " otherwise fail. (This is all a hack)
    let nline = getline(nnum)
    if nline =~ '^\s*\(except\|else\|elif\)'
    return "="
    endif

    " Python programmers love their readable code, so they're usually
    " going to have blank lines at the ends of functions or classes
    " If the next line isn't blank, we probably don't need to end a fold
    if nnum == a:lnum + 1
    return "="
    endif

    " If next line has less indentation we end a fold.
    " This ends folds that aren't there a lot of the time, and this sometimes
    " confuses vim.  Luckily only rarely.
    let nind = indent(nnum)
    if nind < ind
    return "<" . (nind / &sw + 1)
    endif

    " If none of the above apply, keep the indentation
    return "="

endfunction


阅读(3084) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~