diff --git a/syntax/jpythonfold.vim b/ftplugin/python.vim similarity index 84% rename from syntax/jpythonfold.vim rename to ftplugin/python.vim index fa6e98f..2f264a1 100644 --- a/syntax/jpythonfold.vim +++ b/ftplugin/python.vim @@ -1,7 +1,7 @@ -" Fold routines for python code, version 3.2 -" Source: http://www.vim.org/scripts/script.php?script_id=2527 -" Last Change: 2009 Feb 25 +" Fold routines for python code +" Version: 3.3 " Author: Jurjen Bos +" Maintainer: Mohammed Badran " Bug fixes and helpful comments: Grissiom, David Froger, Andrew McNabb " Principles: @@ -12,15 +12,15 @@ " other lines outside a def/class are folded together as a group " for algorithm, see bottom of script -" - optionally, you can get empty lines between folds, see (***) -" - another option is to ignore non-python files see (**) -" - you can also modify the def/class check, -" allowing for multiline def and class definitions see (*) +" - optionally, you can get empty lines between folds, see (***) TODO: +" implement this option +" TODO: contact author about changes +" - optionally, you can get custom python fold text " Note for vim 7 users: -" Vim 6 line numbers always take 8 columns, while vim 7 has a numberwidth variable -" you can change the 8 below to &numberwidth if you have vim 7, -" this is only really useful when you plan to use more than 8 columns (i.e. never) +" Vim 6 line numbers always take 8 columns, while vim 7 has a numberwidth +" variable you can change the 8 below to &numberwidth if you have vim 7, this +" is only really useful when you plan to use more than 8 columns (i.e. never) " Note for masochists trying to read this: " I wanted to keep the functions short, so I replaced occurences of @@ -30,23 +30,14 @@ " if condition | statement " wherever I found that useful -" (*) -" class definitions are supposed to ontain a colon on the same line. -" function definitions are *not* required to have a colon, to allow for multiline defs. -" I you disagree, use instead of the pattern '^\s*\(class\s.*:\|def\s\)' -" to enforce : for defs: '^\s*\(class\|def\)\s.*:' -" you'll have to do this in two places. let s:defpat = '^\s*\(@\|class\s.*:\|def\s\)' -" (**) Ignore non-python files -" Commented out because some python files are not recognized by Vim -"if &filetype != 'python' -" finish -"endif - setlocal foldmethod=expr setlocal foldexpr=GetPythonFold(v:lnum) -setlocal foldtext=PythonFoldText() + +if exists('g:jpythonfold_CustomFoldText') && g:jpythonfold_CustomFoldText + setlocal foldtext=PythonFoldText() +endif function! PythonFoldText() let fs = v:foldstart @@ -74,7 +65,8 @@ function! PythonFoldText() "expand tabs (mail me if you have tabstop>10) let onetab = strpart(' ', 0, &tabstop) let line = substitute(line, '\t', onetab, 'g') - return strpart(line.spcs, 0, w-strlen(size)-7).'.'.size.' lines' + let fix = strlen(line) - strlen(substitute(line, ".", "x", "g")) " fix for non-ascii symbols + return strpart(line.spcs, 0, w-strlen(size)-7+fix).'.'.size.' lines' endfunction function! GetBlockIndent(lnum) @@ -129,7 +121,7 @@ function! GetPythonFold(lnum) endif " Case E***: empty lines fold with previous " (***) change '=' to -1 if you want empty lines/comment out of a fold - elseif line == '' | return '=' + elseif line == '' | return exists("g:jpythonfold_Compact") && !g:jpythonfold_Compact ? -1 : '=' endif " now we need the indent from previous let p = prevnonblank(a:lnum-1) @@ -192,18 +184,21 @@ function! GetPythonFold(lnum) endif endfunction -" higher foldlevel theory -" There are five kinds of statements: S (code), D (def/class), E (empty), C (comment) +" Higher Foldlevel Theory +" +" There are five kinds of statements: S (code), D (def/class), E (empty), C +" (comment). " Note that a decorator statement (beginning with @) counts as definition, -" but that of a sequence of @,@,@,def only the first one counts -" This means that a definiion only counts if not preceded by a decorator +" but that of a sequence of @,@,@,def only the first one counts. +" This means that a definition only counts if not preceded by a decorator. " There are two kinds of folds: R (regular), G (global statements) -" There are five indent situations with respect to the previous non-emtpy non-comment line: -" > (indent), < (dedent), = (same); < and = combine with 0 (indent is zero) -" Note: if the previous line is class/def, its indent is interpreted as one higher +" There are five indent situations with respect to the previous non-empty +" non-comment line: > (indent), < (dedent), = (same); < and = combine with 0 +" (indent is zero). Note: if the previous line is class/def, its indent is +" interpreted as one higher. " There are three indent situations with respect to the next (non-E non-C) line: " > (dedent), < (indent), = (same)