LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2008, 02:45 AM   #1
dongli
LQ Newbie
 
Registered: Feb 2008
Posts: 8

Rep: Reputation: 0
[help] python fold script


Hi, everyone

I want to create myself python folding style, for example

0
1 class Whatever:
1
2 def __init__(self):
2
2 pass
0
1 def whatever():
1
1 pass

so I edit my ~/.vim/ftplugin/python.vim, and add the following lines:

Code:
function! Python_fold_level(lnum)
    let l:line = getline(a:lnum)
    let l:indent = indent(a:lnum)
    " folding for blank line
    if l:line =~ "^$"
        if getline(a:lnum-1) =~ "^$"
            return "="
        endif
        let l:i = 1
        while 1
            let l:next_line = getline(a:lnum+i)
            " start without space
            if l:next_line =~ "^\s\@!"
                return 0
            endif
            " start with indent
            if indent(a:lnum+i) != 0
                return "="
            endif
            let l:i += 1
        endwhile
    endif
    " folding for class
    if l:line =~ "^class"
        return 1
    endif
    " folding for function
    if l:line =~ "^\s*def"
        return "a1"
    endif
    if l:indent != 0
        return "="
    endif
endfunction
setlocal foldexpr=Python_fold_level(v:lnum)
setlocal foldmethod=expr
setlocal foldclose=all
but the effect is not what I want, it looks like

0
1 class Whatever:
1
1 def __init__(self):
1
1 pass
1
2 def whatever():
2
2 pass

This is an incomplete script, please help me check it out.

Thanks for help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Do you Fold? drf99 SUSE / openSUSE 2 01-07-2008 08:32 AM
What does fold mean? colinstu General 3 05-26-2007 04:41 PM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM
python script LinuxLala Programming 14 04-07-2004 06:19 AM
Fold it like you stole it!! orange400 General 2 10-28-2003 08:00 AM


All times are GMT -5. The time now is 05:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration