ld.vim (1867B)
1 " Vim indent file 2 " Language: ld(1) script 3 " Maintainer: Doug Kearns <dougkearns@gmail.com> 4 " Previous Maintainer: Nikolai Weibull <now@bitwi.se> 5 " Last Change: 24 Sep 2021 6 7 if exists("b:did_indent") 8 finish 9 endif 10 let b:did_indent = 1 11 12 setlocal indentexpr=GetLDIndent() 13 setlocal indentkeys=0{,0},!^F,o,O 14 setlocal nosmartindent 15 16 let b:undo_indent = "setl inde< indk< si<" 17 18 if exists("*GetLDIndent") 19 finish 20 endif 21 22 function s:prevnonblanknoncomment(lnum) 23 let lnum = a:lnum 24 while lnum > 1 25 let lnum = prevnonblank(lnum) 26 let line = getline(lnum) 27 if line =~ '\*/' 28 while lnum > 1 && line !~ '/\*' 29 let lnum -= 1 30 endwhile 31 if line =~ '^\s*/\*' 32 let lnum -= 1 33 else 34 break 35 endif 36 else 37 break 38 endif 39 endwhile 40 return lnum 41 endfunction 42 43 function s:count_braces(lnum, count_open) 44 let n_open = 0 45 let n_close = 0 46 let line = getline(a:lnum) 47 let pattern = '[{}]' 48 let i = match(line, pattern) 49 while i != -1 50 if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'ld\%(Comment\|String\)' 51 if line[i] == '{' 52 let n_open += 1 53 elseif line[i] == '}' 54 if n_open > 0 55 let n_open -= 1 56 else 57 let n_close += 1 58 endif 59 endif 60 endif 61 let i = match(line, pattern, i + 1) 62 endwhile 63 return a:count_open ? n_open : n_close 64 endfunction 65 66 function GetLDIndent() 67 let line = getline(v:lnum) 68 if line =~ '^\s*\*' 69 return cindent(v:lnum) 70 elseif line =~ '^\s*}' 71 return indent(v:lnum) - shiftwidth() 72 endif 73 74 let pnum = s:prevnonblanknoncomment(v:lnum - 1) 75 if pnum == 0 76 return 0 77 endif 78 79 let ind = indent(pnum) + s:count_braces(pnum, 1) * shiftwidth() 80 81 let pline = getline(pnum) 82 if pline =~ '}\s*$' 83 let ind -= (s:count_braces(pnum, 0) - (pline =~ '^\s*}' ? 1 : 0)) * shiftwidth() 84 endif 85 86 return ind 87 endfunction