blob: 4483efdbd89c2cb2a952b177cab992823403d243 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar214641f2017-03-05 17:04:09 +01002" Language: Makefile
3" Previous Maintainer: Nikolai Weibull <now@bitwi.se>
4" Latest Revision: 2007-05-07
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006if exists("b:did_indent")
7 finish
8endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009let b:did_indent = 1
10
11setlocal indentexpr=GetMakeIndent()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000012setlocal indentkeys=!^F,o,O,<:>,=else,=endif
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013setlocal nosmartindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
Bram Moolenaar071d4272004-06-13 20:20:40 +000015if exists("*GetMakeIndent")
16 finish
17endif
18
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000019let s:comment_rx = '^\s*#'
Bram Moolenaar57657d82006-04-21 22:12:41 +000020let s:rule_rx = '^[^ \t#:][^#:]*:\{1,2}\%([^=:]\|$\)'
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000021let s:continued_rule_rx = '^[^#:]*:\{1,2}\%([^=:]\|$\)'
Bram Moolenaar57657d82006-04-21 22:12:41 +000022let s:continuation_rx = '\\$'
Bram Moolenaar4c295022021-05-02 17:19:11 +020023let s:assignment_rx = '^\s*\h\w*\s*[+:?]\==\s*\zs.*\\$'
24let s:folded_assignment_rx = '^\s*\h\w*\s*[+:?]\=='
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000025" TODO: This needs to be a lot more restrictive in what it matches.
26let s:just_inserted_rule_rx = '^\s*[^#:]\+:\{1,2}$'
27let s:conditional_directive_rx = '^ *\%(ifn\=\%(eq\|def\)\|else\)\>'
28let s:end_conditional_directive_rx = '^\s*\%(else\|endif\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000030function s:remove_continuation(line)
31 return substitute(a:line, s:continuation_rx, "", "")
32endfunction
33
Bram Moolenaar071d4272004-06-13 20:20:40 +000034function GetMakeIndent()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000035 " TODO: Should this perhaps be v:lnum -1?
36" let prev_lnum = prevnonblank(v:lnum - 1)
37 let prev_lnum = v:lnum - 1
38 if prev_lnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000039 return 0
40 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000041 let prev_line = getline(prev_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000043 let prev_prev_lnum = prev_lnum - 1
44 let prev_prev_line = prev_prev_lnum != 0 ? getline(prev_prev_lnum) : ""
Bram Moolenaar57657d82006-04-21 22:12:41 +000045
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000046 " TODO: Deal with comments. In comments, continuations aren't interesting.
47 if prev_line =~ s:continuation_rx
48 if prev_prev_line =~ s:continuation_rx
49 return indent(prev_lnum)
50 elseif prev_line =~ s:rule_rx
Bram Moolenaar036986f2017-03-16 17:41:02 +010051 return shiftwidth()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000052 elseif prev_line =~ s:assignment_rx
53 call cursor(prev_lnum, 1)
Bram Moolenaarf193fff2006-04-27 00:02:13 +000054 if search(s:assignment_rx, 'W') != 0
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000055 return virtcol('.') - 1
56 else
57 " TODO: ?
Bram Moolenaar036986f2017-03-16 17:41:02 +010058 return shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +000059 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000060 else
61 " TODO: OK, this might be a continued shell command, so perhaps indent
62 " properly here? Leave this out for now, but in the next release this
63 " should be using indent/sh.vim somehow.
64 "if prev_line =~ '^\t' " s:rule_command_rx
65 " if prev_line =~ '^\s\+[@-]\%(if\)\>'
66 " return indent(prev_lnum) + 2
67 " endif
68 "endif
Bram Moolenaar036986f2017-03-16 17:41:02 +010069 return indent(prev_lnum) + shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +000070 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000071 elseif prev_prev_line =~ s:continuation_rx
72 let folded_line = s:remove_continuation(prev_prev_line) . ' ' . s:remove_continuation(prev_line)
73 let lnum = prev_prev_lnum - 1
74 let line = getline(lnum)
75 while line =~ s:continuation_rx
76 let folded_line = s:remove_continuation(line) . ' ' . folded_line
Bram Moolenaar57657d82006-04-21 22:12:41 +000077 let lnum -= 1
78 let line = getline(lnum)
79 endwhile
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000080 let folded_lnum = lnum + 1
81 if folded_line =~ s:rule_rx
82 if getline(v:lnum) =~ s:rule_rx
83 return 0
84 else
85 return &ts
86 endif
87 else
88" elseif folded_line =~ s:folded_assignment_rx
89 if getline(v:lnum) =~ s:rule_rx
90 return 0
91 else
92 return indent(folded_lnum)
93 endif
94" else
95" " TODO: ?
96" return indent(prev_lnum)
97 endif
98 elseif prev_line =~ s:rule_rx
99 if getline(v:lnum) =~ s:rule_rx
100 return 0
101 else
102 return &ts
103 endif
104 elseif prev_line =~ s:conditional_directive_rx
Bram Moolenaar036986f2017-03-16 17:41:02 +0100105 return shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000106 else
Bram Moolenaar2bb8df22007-05-10 17:26:28 +0000107 let line = getline(v:lnum)
108 if line =~ s:just_inserted_rule_rx
109 return 0
110 elseif line =~ s:end_conditional_directive_rx
Bram Moolenaar036986f2017-03-16 17:41:02 +0100111 return v:lnum - 1 == 0 ? 0 : indent(v:lnum - 1) - shiftwidth()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +0000112 else
113 return v:lnum - 1 == 0 ? 0 : indent(v:lnum - 1)
114 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116endfunction