Hi list,

Since I had to edit more and more Matlab code in university, I
scratched an itch a had, with ftplugins/matlab.vim. It didn't feature
code folding. Now it does. ...it's good enough for me anyway.

I don't know where to send such additions, so I'm just sending to the
users list now.

Folding is not enabled per default (as it automatically switches to
fdm=expr - and people using fdm=marker for everything might not like
that).

To enable it, do:
    let g:matlab_fold_enable = 1

Comments welcome; and please direct me to the right destination, if
the users list is just wrong for things like this.

Regards, Frank

[snip]
" Vim filetype plugin file
" Language:     matlab
" Maintainer:   Jake Wasserman <jwasserman at gmail dot com>
" Last Changed: 2006 Jan 12

if exists("b:did_ftplugin")
        finish
endif
let b:did_ftplugin = 1

let s:save_cpo = &cpo
set cpo-=C

if exists("loaded_matchit")
        let s:conditionalEnd = '\(([^()]*\)[EMAIL 
PROTECTED]<end\>\([^()]*)\)[EMAIL PROTECTED]'
        let b:match_words = '\<if\>\|\<while\>\|\<for\>\|\<switch\>:' .
                \ s:conditionalEnd . ',\<if\>:\<elseif\>:\<else\>:' .
                \ s:conditionalEnd
endif

setlocal suffixesadd=.m
setlocal suffixes+=.asv

let b:undo_ftplugin = "setlocal suffixesadd< suffixes< "
        \ . "| unlet! b:match_words"

let &cpo = s:save_cpo

if exists("g:matlab_fold_enable") && g:matlab_fold_enable > 0
        setlocal fdm=expr
        setlocal foldexpr=Matlab_Fold(v:lnum)
endif

function! Matlab_Fold(lnum)
        let line = getline(a:lnum)
        let lvl = foldlevel(a:lnum - 1)

        if line =~ '^% --- '
                " the GUIDE thing adds comments like this in front of callbacks
                " don't fold them
                return '0'
        endif

        if line =~ '^\S*function[^(]*(.*)\s*$'
                " start of a function
                return '>1'
        endif

        let nline = getline(a:lnum + 1)
        if nline =~ '^\S*function[^(]*(.*)\s*$'
                " next line is a function, so the current fold stops
                return '<1'
        endif

        if lvl == 1 && line =~ '^\S*%' && nline =~ '^\S*%'
                " fold multiline comments
                return '>2'
        endif

        if lvl != 1 && getline(a:lnum - 1) =~ '^\S*%*' && line !~ '^\S*%'
                " end multiline comment folds
                return '<2'
        endif

        return '='
endfunction
[snap]

Or as a unified diff, if you guys prefer:

[snip]
--- vim72/ftplugin/matlab.vim   2008-10-20 18:25:25.000000000 +0200
+++ /home/hawk/etc/vim/ftplugin/matlab.vim      2008-11-09 15:07:04.000000000 
+0100
@@ -26,5 +26,41 @@
 
 let &cpo = s:save_cpo
 
+if exists("g:matlab_fold_enable") && g:matlab_fold_enable > 0
+       setlocal fdm=expr
+       setlocal foldexpr=Matlab_Fold(v:lnum)
+endif
 
+function! Matlab_Fold(lnum)
+       let line = getline(a:lnum)
+       let lvl = foldlevel(a:lnum - 1)
 
+       if line =~ '^% --- '
+               " the GUIDE thing adds comments like this in front of callbacks
+               " don't fold them
+               return '0'
+       endif
+
+       if line =~ '^\S*function[^(]*(.*)\s*$'
+               " start of a function
+               return '>1'
+       endif
+
+       let nline = getline(a:lnum + 1)
+       if nline =~ '^\S*function[^(]*(.*)\s*$'
+               " next line is a function, so the current fold stops
+               return '<1'
+       endif
+
+       if lvl == 1 && line =~ '^\S*%' && nline =~ '^\S*%'
+               " fold multiline comments
+               return '>2'
+       endif
+
+       if lvl != 1 && getline(a:lnum - 1) =~ '^\S*%*' && line !~ '^\S*%'
+               " end multiline comment folds
+               return '<2'
+       endif
+
+       return '='
+endfunction
[snap]

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to