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