blob: 3fe5a493e0b853a844ef3bc369f86f3558f5a024 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002" Language: Makefile
3" Maintainer: Nikolai Weibull <now@bitwi.se>
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004" Latest Revision: 2006-04-26
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()
12setlocal indentkeys=!^F,o,O
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 Moolenaar57657d82006-04-21 22:12:41 +000019let s:rule_rx = '^[^ \t#:][^#:]*:\{1,2}\%([^=:]\|$\)'
20let s:continuation_rx = '\\$'
21let s:assignment_rx = '^\s*\h\w*\s*+\==\s*\zs.*\\$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
Bram Moolenaarf193fff2006-04-27 00:02:13 +000023" TODO: Deal with comments, string, and all kinds of other crap, e.g., defines.
24" TODO: Unwrap the whole logic of this function into something that requires a
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000025" lot less 'return's.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026function GetMakeIndent()
Bram Moolenaar42eeac32005-06-29 22:40:58 +000027 let lnum = v:lnum - 1
28 if lnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 return 0
30 endif
31
Bram Moolenaarf193fff2006-04-27 00:02:13 +000032 " Figure out if the previous line is part of a rule or not. If it is, then
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000033 " we more or less just indent by a 'tabstop', the previous' lines indent, or
Bram Moolenaarf193fff2006-04-27 00:02:13 +000034 " remove all indent if the current line is itself a rule. Also, if the line
35 " in question is part of a continuation-line set constituting the rule line
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000036 " itself, we indent by either a 'shiftwidth', if the line is the first in the
Bram Moolenaarf193fff2006-04-27 00:02:13 +000037 " continuation, or use the indent of the previous line, if not.
38 while lnum > 0
39 let line = getline(lnum)
40 if line[0] != "\t"
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000041 " We found a non-shell-command line, i.e., one that doesn't have a
Bram Moolenaarf193fff2006-04-27 00:02:13 +000042 " leading tab.
43 if line =~ s:rule_rx
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000044 " The line looks like a rule line, so we must therefore either be inside a
45 " rule or we are a continuation line to that rule line.
46 if line =~ s:continuation_rx
47 " Ah, the rule line was continued, so look up the last continuation
48 " line that's above the current line.
49 while line =~ s:continuation_rx && lnum < v:lnum
50 let lnum += 1
51 let line = getline(lnum)
52 endwhile
53 let lnum -= 1
54 let line = getline(lnum)
55 endif
Bram Moolenaar57657d82006-04-21 22:12:41 +000056
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000057 " If the line that we've found is right above the current line, deal
58 " with it specifically.
59 if lnum == v:lnum - 1
60 " If it was continued, indent the current line by a shiftwidth, as it
61 " is the first to follow it. Otherwise, depending on if the current
62 " line is a rule line, i.e, a rule line following another rule line,
63 " then indent to the left margin. Otherwise, the current line is the
64 " first shell-command line in the rule, so indent by a 'tabstop'
65 if line =~ s:continuation_rx
66 return &sw
67 else
68 return getline(v:lnum) =~ s:rule_rx ? 0 : &ts
69 endif
70 else
71 " If the previous line was a continuation line, then unless it was
72 " itself a part of a continuation line, add a 'shiftwidth''s worth of
73 " indent. Otherwise, just use the indent of the previous line.
74 " Otherwise, if the previous line wasn't a continuation line, check
75 " if the one above it was. If it was then indent to whatever level
76 " the 'owning' line had. Otherwise, indent to the previous line's
77 " level.
78 let lnum = v:lnum - 1
79 let line = getline(lnum)
80 if line =~ s:continuation_rx
81 let pnum = v:lnum - 2
82 let pine = getline(pnum)
83 if pine =~ s:continuation_rx
84 return indent(lnum)
85 else
86 return indent(lnum) + &sw
87 endif
88 else
89 let lnum = v:lnum - 2
90 let line = getline(lnum)
91 if line =~ s:continuation_rx
92 while lnum > 0
93 if line !~ s:continuation_rx
94 let lnum += 1
95 let line = getline(lnum)
96 break
97 endif
98 let lnum -= 1
99 let line = getline(lnum)
100 endwhile
101 " We've found the owning line. Indent to it's level.
102 return indent(lnum)
103 else
104 return indent(v:lnum - 1)
105 endif
106 endif
107 endif
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000108 endif
109
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000110 " The line wasn't a rule line, so the current line is part of a series
111 " of tab-indented lines that don't belong to any rule.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000112 break
113 endif
114 let lnum -= 1
115 endwhile
116
117 " If the line before the one we are currently indenting ended with a
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000118 " continuation, then try to figure out what 'owns' that line and indent
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000119 " appropriately.
120 let lnum = v:lnum - 1
121 let line = getline(lnum)
122 if line =~ s:continuation_rx
123 let indent = indent(lnum)
124 if line =~ s:assignment_rx
125 " The previous line is a continuation line that begins a variable-
126 " assignment expression, so set the indent to just beyond the whitespace
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000127 " following the assignment operator ('=').
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000128 call cursor(lnum, 1)
129 if search(s:assignment_rx, 'W') != 0
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000130 let indent = virtcol('.') - 1
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000131 endif
132 endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000133
134 " The previous line didn't constitute an assignment, so just indent to
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000135 " whatever level it had.
136 return indent
137 endif
138
139 " If the line above the line above the current line ended was continued,
140 " then the line above the current line was part of a continued line. Find
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000141 " the 'owning' line and indent to its level.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000142 let lnum = v:lnum - 2
143 let line = getline(lnum)
144 if line =~ s:continuation_rx
145 while lnum > 0
146 if line !~ s:continuation_rx
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000147 let lnum += 1
148 let line = getline(lnum)
149 break
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000150 endif
Bram Moolenaar57657d82006-04-21 22:12:41 +0000151 let lnum -= 1
152 let line = getline(lnum)
153 endwhile
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000154 " We've found the owning line. Indent to it's level.
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000155 return indent(lnum)
156 endif
Bram Moolenaar57657d82006-04-21 22:12:41 +0000157
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000158 " If nothing else caught on, then check if this line is a rule line. If it
159 " is, indent it to the left margin. Otherwise, simply use the indent of the
160 " previous line.
161 let line = getline(v:lnum)
162 if line =~ s:rule_rx
163 return 0
164 else
165 return indent(v:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167endfunction