robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
c4942880be
master
1" Vim indent file 2" Language: VisualBasic (ft=vb) / Basic (ft=basic) / SaxBasic (ft=vb) 3" Author: Johannes Zellner <johannes@zellner.org> 4" Maintainer: Michael Soyka (mssr953@gmail.com) 5" Last Change: Fri, 18 Jun 2004 07:22:42 CEST 6" Small update 2010 Jul 28 by Maxim Kim 7" 2022/12/15: add support for multiline statements. 8" 2022/12/21: move VbGetIndent from global to script-local scope 9" 2022/12/26: recognize "Type" keyword 10 11if exists ( "b:did_indent" ) 12finish 13endif 14let b: did_indent = 1 15 16setlocal autoindent 17setlocal indentexpr = s :VbGetIndent(v:lnum) 18setlocal indentkeys & 19setlocal indentkeys += =~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,=~loop 20 21let b: undo_indent = "set ai< indentexpr< indentkeys<" 22 23" Only define the function once. 24if exists ( "*s:VbGetIndent" ) 25finish 26endif 27 28function s: VbGetIndent ( lnum ) 29let this_lnum = a: lnum 30let this_line = getline ( this_lnum ) 31 32" labels and preprocessor get zero indent immediately 33let LABELS_OR_PREPROC = '^\s*\(\<\k\+\>:\s*$\|#.*\)' 34if this_line =~? LABELS_OR_PREPROC 35return 0 36endif 37 38" Get the current value of "shiftwidth" 39let bShiftwidth = shiftwidth () 40 41" Find a non-blank line above the current line. 42" Skip over labels and preprocessor directives. 43let lnum = this_lnum 44while lnum > 0 45let lnum = prevnonblank ( lnum - 1 ) 46let previous_line = getline ( lnum ) 47if previous_line !~? LABELS_OR_PREPROC 48break 49endif 50endwhile 51 52" Hit the start of the file, use zero indent. 53if lnum == 0 54return 0 55endif 56 57" Variable "previous_line" now contains the text in buffer line "lnum". 58 59" Multi-line statements have the underscore character at end-of-line: 60" 61" object.method(arguments, _ 62" arguments, _ 63" arguments) 64" 65" and require extra logic to determine the correct indentation. 66" 67" Case 1: Line "lnum" is the first line of a multiline statement. 68" Line "lnum" will have a trailing underscore character 69" but the preceding non-blank line does not. 70" Line "this_lnum" will be indented relative to "lnum". 71" 72" Case 2: Line "lnum" is the last line of a multiline statement. 73" Line "lnum" will not have a trailing underscore character 74" but the preceding non-blank line will. 75" Line "this_lnum" will have the same indentation as the starting 76" line of the multiline statement. 77" 78" Case 3: Line "lnum" is neither the first nor last line. 79" Lines "lnum" and "lnum-1" will have a trailing underscore 80" character. 81" Line "this_lnum" will have the same indentation as the preceding 82" line. 83" 84" No matter which case it is, the starting line of the statement must be 85" found. It will be assumed that multiline statements cannot have 86" intermingled comments, statement labels, preprocessor directives or 87" blank lines. 88" 89let lnum_is_continued = ( previous_line =~ '_$' ) 90if lnum > 1 91let before_lnum = prevnonblank ( lnum - 1 ) 92let before_previous_line = getline ( before_lnum ) 93else 94let before_lnum = 0 95let before_previous_line = "" 96endif 97 98if before_previous_line !~ '_$' 99" Variable "previous_line" contains the start of a statement. 100" 101let ind = indent ( lnum ) 102if lnum_is_continued 103let ind += bShiftwidth 104endif 105elseif ! lnum_is_continued 106" Line "lnum" contains the last line of a multiline statement. 107" Need to find where this multiline statement begins 108" 109while before_lnum > 0 110let before_lnum -= 1 111if getline ( before_lnum ) !~ '_$' 112let before_lnum += 1 113break 114endif 115endwhile 116if before_lnum == 0 117let before_lnum = 1 118endif 119let previous_line = getline ( before_lnum ) 120let ind = indent ( before_lnum ) 121else 122" Line "lnum" is not the first or last line of a multiline statement. 123" 124let ind = indent ( lnum ) 125endif 126 127" Add 128if previous_line =~? '^\s*\<\(begin\|\%(\%(private\|public\|friend\)\s\+\)\=\%(function\|sub\|property\|enum\|type\)\|select\|case\|default\|if\|else\|elseif\|do\|for\|while\|with\)\>' 129let ind = ind + bShiftwidth 130endif 131 132" Subtract 133if this_line =~? '^\s*\<end\>\s\+\<select\>' 134if previous_line !~? '^\s*\<select\>' 135let ind = ind - 2 * bShiftwidth 136else 137" this case is for an empty 'select' -- 'end select' 138" (w/o any case statements) like: 139" 140" select case readwrite 141" end select 142let ind = ind - bShiftwidth 143endif 144elseif this_line =~? '^\s*\<\(end\|else\|elseif\|until\|loop\|next\|wend\)\>' 145let ind = ind - bShiftwidth 146elseif this_line =~? '^\s*\<\(case\|default\)\>' 147if previous_line !~? '^\s*\<select\>' 148let ind = ind - bShiftwidth 149endif 150endif 151 152return ind 153endfunction 154 155" vim:sw=4