Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: VisualBasic (ft=vb) / Basic (ft=basic) / SaxBasic (ft=vb) |
| 3 | " Author: Johannes Zellner <johannes@zellner.org> |
| 4 | " Last Change: Tue, 27 Apr 2004 14:54:59 CEST |
| 5 | |
| 6 | if exists("b:did_indent") |
| 7 | finish |
| 8 | endif |
| 9 | let b:did_indent = 1 |
| 10 | |
| 11 | setlocal indentexpr=VbGetIndent(v:lnum) |
| 12 | setlocal indentkeys& |
| 13 | setlocal indentkeys+==~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,~=loop,<:> |
| 14 | |
| 15 | " Only define the function once. |
| 16 | if exists("*VbGetIndent") |
| 17 | finish |
| 18 | endif |
| 19 | |
| 20 | fun! VbGetIndent(lnum) |
| 21 | " labels and preprocessor get zero indent immediately |
| 22 | let this_line = getline(a:lnum) |
| 23 | let LABELS_OR_PREPROC = '^\s*\(\<\k\+\>:\s*$\|#.*\)' |
| 24 | if this_line =~? LABELS_OR_PREPROC |
| 25 | return 0 |
| 26 | endif |
| 27 | |
| 28 | " Find a non-blank line above the current line. |
| 29 | " Skip over labels and preprocessor directives. |
| 30 | let lnum = a:lnum |
| 31 | while lnum > 0 |
| 32 | let lnum = prevnonblank(lnum - 1) |
| 33 | let previous_line = getline(lnum) |
| 34 | if previous_line !~? LABELS_OR_PREPROC |
| 35 | break |
| 36 | endif |
| 37 | endwhile |
| 38 | |
| 39 | " Hit the start of the file, use zero indent. |
| 40 | if lnum == 0 |
| 41 | return 0 |
| 42 | endif |
| 43 | |
| 44 | let ind = indent(lnum) |
| 45 | |
| 46 | " Add |
| 47 | if previous_line =~? '^\s*\<\(begin\|\%(\%(private\|public\|friend\)\s\+\)\=\%(function\|sub\|property\)\|select\|case\|default\|if\>.\{-}\<then\>\s*$\|else\|elseif\|do\|for\|while\|enum\|with\)\>' |
| 48 | let ind = ind + &sw |
| 49 | endif |
| 50 | |
| 51 | " Subtract |
| 52 | if this_line =~? '^\s*\<end\>\s\+\<select\>' |
| 53 | if previous_line !~? '^\s*\<select\>' |
| 54 | let ind = ind - 2 * &sw |
| 55 | else |
| 56 | " this case is for an empty 'select' -- 'end select' |
| 57 | " (w/o any case statements) like: |
| 58 | " |
| 59 | " select case readwrite |
| 60 | " end select |
| 61 | let ind = ind - &sw |
| 62 | endif |
| 63 | elseif this_line =~? '^\s*\<\(end\|else\|until\|loop\|next\|wend\)\>' |
| 64 | let ind = ind - &sw |
| 65 | elseif this_line =~? '^\s*\<\(case\|default\)\>' |
| 66 | if previous_line !~? '^\s*\<select\>' |
| 67 | let ind = ind - &sw |
| 68 | endif |
| 69 | endif |
| 70 | |
| 71 | return ind |
| 72 | endfun |