Changeset 7179


Ignore:
Timestamp:
01/26/11 09:21:15 (14 years ago)
Author:
Mathieu Morlighem
Message:

Better matlab indentation

Location:
issm/trunk/externalpackages/vim/addons/vim
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk/externalpackages/vim/addons/vim/indent/matlab.vim

    r4230 r7179  
    22" Language:     Matlab
    33" Maintainer:   Fabrice Guy <fabrice.guy at gmail dot com>
    4 " Last Change:  2008 Oct 15
     4" Last Change:  2009 Nov 23 - Added support for if/end block on the same line
    55
    66" Only load this indent file when no other was loaded.
     
    99endif
    1010let b:did_indent = 1
    11 let s:onlySubfunctionExists = 0
     11let s:functionWithoutEndStatement = 0
    1212
    1313setlocal indentexpr=GetMatlabIndent()
     
    6363  let curind = indent(plnum)
    6464  if s:IsMatlabContinuationLine(v:lnum - 1)
    65     let curind = curind + &softtabstop
     65    let curind = curind + &sw
    6666  endif
    6767  " Add a 'shiftwidth' after classdef, properties, switch, methods, events,
    68   " function, if, while, for, otherwise, case, tic, try, catch, else, elseif
    69   if getline(plnum) =~ '^\s*\(classdef\|properties\|switch\|methods\|events\|function\|if\|while\|for\|otherwise\|case\|tic\|try\|catch\|else\|elseif\)\>'
    70     let curind = curind + &softtabstop
     68  " function, if, while, for, otherwise, case, try, catch, else, elseif
     69  if getline(plnum) =~ '^\s*\(classdef\|properties\|switch\|methods\|events\|function\|if\|while\|for\|otherwise\|case\|try\|catch\|else\|elseif\)\>'
     70    let curind = curind + &sw
    7171    " In Matlab we have different kind of functions
    7272    " - the main function (the function with the same name than the filename)
     
    7474    " - the functions defined in methods (for classes)
    7575    " - subfunctions
    76     " For the moment the main function (located line 1) doesn't produce any indentation in the
    77     " code (default behavior in the Matlab editor) and the other kind of
    78     " functions indent the code
     76    " Principles for the indentation :
     77    " - all the function keywords are indented (corresponding to the
     78    "   'indent all functions' in the Matlab Editor)
     79    " - if we have only subfonctions (ie if the main function doesn't have
     80    "   any mayching end), then each function is dedented
    7981    if getline(plnum)  =~ '^\s*\function\>'
    80       " If it is the main function
    81       if plnum == 1
     82      let pplnum = plnum - 1
     83      while pplnum > 1 && (getline(pplnum) =~ '^\s*%')
     84        let pplnum = pplnum - 1
     85      endwhile
     86      " If it is the main function, determine if function has a matching end
     87      " or not
     88      if pplnum <= 1
    8289        " look for a matching end :
    83         " - if we find a matching end everything is fine
    84         " - if not, then all other functions are subfunctions
     90        " - if we find a matching end everything is fine : end of functions
     91        "   will be dedented when 'end' is reached
     92        " - if not, then all other functions are subfunctions : 'function'
     93        "   keyword has to be dedended
     94        let old_lnum = v:lnum
     95        let motion = plnum . "gg"
     96        execute "normal" . motion
    8597        normal %
    8698        if getline(line('.')) =~ '^\s*end'
    87           let s:onlySubfunctionExists = 0
     99          let s:functionWithoutEndStatement = 0
    88100        else
    89           let s:onlySubfunctionExists = 1
     101          let s:functionWithoutEndStatement = 1
    90102        endif
    91103        normal %
    92         let curind = curind - &softtabstop
    93       else
    94         " it is a subfunction without matching end : dedent
    95         if s:onlySubfunctionExists
    96           let curind = curind - &softtabstop
    97         endif
     104        let motion = old_lnum . "gg"
     105        execute "normal" . motion
    98106      endif
     107    endif
     108    " if the for-end block (or while-end) is on the same line : dedent
     109    if getline(plnum)  =~ '\<end[,;]*\s*\(%.*\)\?$'
     110      let curind = curind - &sw
    99111    endif
    100112  endif
    101113
    102   " Subtract a 'shiftwidth' on a else, elseif, end, catch, otherwise, case,
    103   " toc
    104   if getline(v:lnum) =~ '^\s*\(else\|elseif\|end\|catch\|otherwise\|case\|toc\)\>'
    105     let curind = curind - &softtabstop
     114  " Subtract a 'shiftwidth' on a else, elseif, end, catch, otherwise, case
     115  if getline(v:lnum) =~ '^\s*\(else\|elseif\|end\|catch\|otherwise\|case\)\>'
     116    let curind = curind - &sw
    106117  endif
    107118  " No indentation in a subfunction
    108   if getline(v:lnum)  =~ '^\s*\function\>' && s:onlySubfunctionExists
    109     let curind = curind - &softtabstop
     119  if getline(v:lnum)  =~ '^\s*\function\>' && s:functionWithoutEndStatement
     120    let curind = curind - &sw
    110121  endif
    111 
    112122  " First case after a switch : indent
    113123  if getline(v:lnum) =~ '^\s*case'
     
    116126    endwhile
    117127    if getline(plnum) =~ '^\s*switch'
    118       let curind = indent(plnum) + &softtabstop
     128      let curind = indent(plnum) + &sw
    119129    endif
    120130  endif
     
    125135    if getline(v:lnum) =~ '^\s*end'
    126136      normal %
    127       if getline(line('.')) =~ "switch"
    128         let curind = curind - &softtabstop
     137      if getline(line('.')) =~ '^\s*switch'
     138        let curind = curind - &sw
    129139      endif
    130140      normal %
  • issm/trunk/externalpackages/vim/addons/vim/syntax/matlab.vim

    r4230 r7179  
    55" Last Change:  2008 Oct 16 : added try/catch/rethrow and class statements
    66"               2008 Oct 28 : added highlighting for most of Matlab functions
     7"               2009 Nov 23 : added 'todo' keyword in the matlabTodo keywords
     8"               (for doxygen support)
    79
    810" For version 5.x: Clear all syntax items
     
    1517
    1618syn keyword matlabStatement             return function
    17 syn keyword matlabConditional           switch case else elseif end if otherwise for while
    18 syn keyword matlabRepeat                do break continue
     19syn keyword matlabConditional           switch case else elseif end if otherwise break continue
     20syn keyword matlabRepeat                do for while
    1921syn keyword matlabStorageClass          classdef methods properties events persistent global
    2022syn keyword matlabExceptions            try catch rethrow throw
     
    5456
    5557syn match matlabComment                 "%.*$"  contains=matlabTodo,matlabTab
    56 syn region matlabBlockComment        start=+%{+    end=+%}+
     58syn region matlabBlockComment        start=+%{+    end=+%}+ contains=matlabBlockComment
    5759
    5860
     
    306308
    307309
    308 syn match matlabError   "-\=\<\d\+\.\d\+\.[^*/\\^]"
    309 syn match matlabError   "-\=\<\d\+\.\d\+[eEdD][-+]\=\d\+\.\([^*/\\^]\)"
    310 
    311310" Define the default highlighting.
    312311" For version 5.7 and earlier: only when not done already
     
    332331  HiLink matlabFloat                    Float
    333332  HiLink matlabConstant                 Constant
    334   HiLink matlabError                    Error
    335333  HiLink matlabImplicit                 matlabStatement
    336334  HiLink matlabStatement                Statement
Note: See TracChangeset for help on using the changeset viewer.