blob: e50b703b20fbe8c041476e54199f99db72452605 [file] [log] [blame]
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001" Vim indent file
2" Language: ld(1) script
Bram Moolenaar57657d82006-04-21 22:12:41 +00003" Maintainer: Nikolai Weibull <now@bitwi.se>
4" Latest Revision: 2006-04-19
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005
6if exists("b:did_indent")
7 finish
8endif
9let b:did_indent = 1
10
11setlocal indentexpr=GetLDIndent()
12setlocal indentkeys=0{,0},!^F,o,O
13
14if exists("*GetLDIndent")
15 finish
16endif
17
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000018function s:prevnonblanknoncomment(lnum)
19 let lnum = a:lnum
20 while lnum > 1
21 let lnum = prevnonblank(lnum)
22 let line = getline(lnum)
23 if line =~ '\*/'
24 while lnum > 1 && line !~ '/\*'
25 let lnum -= 1
26 endwhile
27 if line =~ '^\s*/\*'
28 let lnum -= 1
29 else
30 break
31 endif
32 else
33 break
34 endif
35 endwhile
36 return lnum
37endfunction
38
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000039function s:count_braces(lnum, count_open)
40 let n_open = 0
41 let n_close = 0
42 let line = getline(a:lnum)
43 let pattern = '[{}]'
44 let i = match(line, pattern)
45 while i != -1
46 if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'ld\%(Comment\|String\)'
47 if line[i] == '{'
48 let n_open += 1
49 elseif line[i] == '}'
50 if n_open > 0
51 let n_open -= 1
52 else
53 let n_close += 1
54 endif
55 endif
56 endif
57 let i = match(line, pattern, i + 1)
58 endwhile
59 return a:count_open ? n_open : n_close
60endfunction
61
62function GetLDIndent()
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000063 let line = getline(v:lnum)
64 if line =~ '^\s*\*'
65 return cindent(v:lnum)
66 elseif line =~ '^\s*}'
67 return indent(v:lnum) - &sw
68 endif
69
70 let pnum = s:prevnonblanknoncomment(v:lnum - 1)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000071 if pnum == 0
72 return 0
73 endif
74
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000075 let ind = indent(pnum) + s:count_braces(pnum, 1) * &sw
76
77 let pline = getline(pnum)
78 if pline =~ '}\s*$'
79 let ind -= (s:count_braces(pnum, 0) - (pline =~ '^\s*}' ? 1 : 0)) * &sw
80 endif
81
82 return ind
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000083endfunction