summaryrefslogtreecommitdiffstats
path: root/runtime/indent/make.vim
blob: 58504929f2b2a8726504759d7badd4d4e8b3b2e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
" Vim indent file
" Language:         Makefile
" Maintainer:       Nikolai Weibull <now@bitwi.se>
" Latest Revision:  2006-04-19

if exists("b:did_indent")
  finish
endif
let b:did_indent = 1

setlocal indentexpr=GetMakeIndent()
setlocal indentkeys=!^F,o,O

if exists("*GetMakeIndent")
  finish
endif

let s:rule_rx = '^[^ \t#:][^#:]*:\{1,2}\%([^=:]\|$\)'
let s:continuation_rx = '\\$'
let s:assignment_rx = '^\s*\h\w*\s*+\==\s*\zs.*\\$'

function GetMakeIndent()
  let lnum = v:lnum - 1
  if lnum == 0
    return 0
  endif

  let line = getline(lnum)
  let ind = indent(lnum)

  if line =~ s:rule_rx
    return ind + &ts
  elseif line =~ s:continuation_rx
    while lnum > 0 && line =~ s:continuation_rx && line !~ s:assignment_rx
      let lnum -= 1
      let line = getline(lnum)
    endwhile
    if line =~ s:assignment_rx
      call cursor(lnum, 1)
      return search(s:assignment_rx, 'W') != 0 ? virtcol('.') - 1 : 0
    else
      return 0
    endif
  else
    let pnum = lnum - 1
    if pnum == 0
      return ind
    endif

    return getline(pnum) =~ s:continuation_rx ? 0 : ind
  endif
endfunction