| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | " Vim indent file | 
					
						
							| 
									
										
										
										
											2017-03-05 17:04:09 +01:00
										 |  |  | " Language:             Makefile | 
					
						
							|  |  |  | " Previous Maintainer:  Nikolai Weibull <now@bitwi.se> | 
					
						
							|  |  |  | " Latest Revision:      2007-05-07 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if exists("b:did_indent") | 
					
						
							|  |  |  |   finish | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | let b:did_indent = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setlocal indentexpr=GetMakeIndent() | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  | setlocal indentkeys=!^F,o,O,<:>,=else,=endif | 
					
						
							| 
									
										
										
										
											2006-04-27 00:02:13 +00:00
										 |  |  | setlocal nosmartindent | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if exists("*GetMakeIndent") | 
					
						
							|  |  |  |   finish | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  | let s:comment_rx = '^\s*#' | 
					
						
							| 
									
										
										
										
											2006-04-21 22:12:41 +00:00
										 |  |  | let s:rule_rx = '^[^ \t#:][^#:]*:\{1,2}\%([^=:]\|$\)' | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  | let s:continued_rule_rx = '^[^#:]*:\{1,2}\%([^=:]\|$\)' | 
					
						
							| 
									
										
										
										
											2006-04-21 22:12:41 +00:00
										 |  |  | let s:continuation_rx = '\\$' | 
					
						
							| 
									
										
										
										
											2021-05-02 17:19:11 +02:00
										 |  |  | let s:assignment_rx = '^\s*\h\w*\s*[+:?]\==\s*\zs.*\\$' | 
					
						
							|  |  |  | let s:folded_assignment_rx = '^\s*\h\w*\s*[+:?]\==' | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  | " TODO: This needs to be a lot more restrictive in what it matches. | 
					
						
							|  |  |  | let s:just_inserted_rule_rx = '^\s*[^#:]\+:\{1,2}$' | 
					
						
							|  |  |  | let s:conditional_directive_rx = '^ *\%(ifn\=\%(eq\|def\)\|else\)\>' | 
					
						
							|  |  |  | let s:end_conditional_directive_rx = '^\s*\%(else\|endif\)\>' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function s:remove_continuation(line) | 
					
						
							|  |  |  |   return substitute(a:line, s:continuation_rx, "", "") | 
					
						
							|  |  |  | endfunction | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function GetMakeIndent() | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |   " TODO: Should this perhaps be v:lnum -1? | 
					
						
							|  |  |  | "  let prev_lnum = prevnonblank(v:lnum - 1) | 
					
						
							|  |  |  |   let prev_lnum = v:lnum - 1 | 
					
						
							|  |  |  |   if prev_lnum == 0 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |     return 0 | 
					
						
							|  |  |  |   endif | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |   let prev_line = getline(prev_lnum) | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |   let prev_prev_lnum = prev_lnum - 1 | 
					
						
							|  |  |  |   let prev_prev_line = prev_prev_lnum != 0 ? getline(prev_prev_lnum) : "" | 
					
						
							| 
									
										
										
										
											2006-04-27 00:02:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |   " TODO: Deal with comments.  In comments, continuations aren't interesting. | 
					
						
							|  |  |  |   if prev_line =~ s:continuation_rx | 
					
						
							|  |  |  |     if prev_prev_line =~ s:continuation_rx | 
					
						
							|  |  |  |       return indent(prev_lnum) | 
					
						
							|  |  |  |     elseif prev_line =~ s:rule_rx | 
					
						
							| 
									
										
										
										
											2017-03-16 17:41:02 +01:00
										 |  |  |       return shiftwidth() | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |     elseif prev_line =~ s:assignment_rx | 
					
						
							|  |  |  |       call cursor(prev_lnum, 1) | 
					
						
							| 
									
										
										
										
											2006-04-27 00:02:13 +00:00
										 |  |  |       if search(s:assignment_rx, 'W') != 0 | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |         return virtcol('.') - 1 | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         " TODO: ? | 
					
						
							| 
									
										
										
										
											2017-03-16 17:41:02 +01:00
										 |  |  |         return shiftwidth() | 
					
						
							| 
									
										
										
										
											2006-04-27 00:02:13 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       " TODO: OK, this might be a continued shell command, so perhaps indent | 
					
						
							|  |  |  |       " properly here?  Leave this out for now, but in the next release this | 
					
						
							|  |  |  |       " should be using indent/sh.vim somehow. | 
					
						
							|  |  |  |       "if prev_line =~ '^\t' " s:rule_command_rx | 
					
						
							|  |  |  |       "  if prev_line =~ '^\s\+[@-]\%(if\)\>' | 
					
						
							|  |  |  |       "    return indent(prev_lnum) + 2 | 
					
						
							|  |  |  |       "  endif | 
					
						
							|  |  |  |       "endif | 
					
						
							| 
									
										
										
										
											2017-03-16 17:41:02 +01:00
										 |  |  |       return indent(prev_lnum) + shiftwidth() | 
					
						
							| 
									
										
										
										
											2006-04-27 00:02:13 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |   elseif prev_prev_line =~ s:continuation_rx | 
					
						
							|  |  |  |     let folded_line = s:remove_continuation(prev_prev_line) . ' ' . s:remove_continuation(prev_line) | 
					
						
							|  |  |  |     let lnum = prev_prev_lnum - 1 | 
					
						
							|  |  |  |     let line = getline(lnum) | 
					
						
							|  |  |  |     while line =~ s:continuation_rx | 
					
						
							|  |  |  |       let folded_line = s:remove_continuation(line) . ' ' . folded_line | 
					
						
							| 
									
										
										
										
											2006-04-21 22:12:41 +00:00
										 |  |  |       let lnum -= 1 | 
					
						
							|  |  |  |       let line = getline(lnum) | 
					
						
							|  |  |  |     endwhile | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |     let folded_lnum = lnum + 1 | 
					
						
							|  |  |  |     if folded_line =~ s:rule_rx | 
					
						
							|  |  |  |       if getline(v:lnum) =~ s:rule_rx | 
					
						
							|  |  |  |         return 0 | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         return &ts | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  | "    elseif folded_line =~ s:folded_assignment_rx | 
					
						
							|  |  |  |       if getline(v:lnum) =~ s:rule_rx | 
					
						
							|  |  |  |         return 0 | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         return indent(folded_lnum) | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  | "    else | 
					
						
							|  |  |  | "      " TODO: ? | 
					
						
							|  |  |  | "      return indent(prev_lnum) | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |   elseif prev_line =~ s:rule_rx | 
					
						
							|  |  |  |     if getline(v:lnum) =~ s:rule_rx | 
					
						
							|  |  |  |       return 0 | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       return &ts | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |   elseif prev_line =~ s:conditional_directive_rx | 
					
						
							| 
									
										
										
										
											2017-03-16 17:41:02 +01:00
										 |  |  |     return shiftwidth() | 
					
						
							| 
									
										
										
										
											2006-04-27 00:02:13 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |     let line = getline(v:lnum) | 
					
						
							|  |  |  |     if line =~ s:just_inserted_rule_rx | 
					
						
							|  |  |  |       return 0 | 
					
						
							|  |  |  |     elseif line =~ s:end_conditional_directive_rx | 
					
						
							| 
									
										
										
										
											2017-03-16 17:41:02 +01:00
										 |  |  |       return v:lnum - 1 == 0 ? 0 : indent(v:lnum - 1) - shiftwidth() | 
					
						
							| 
									
										
										
										
											2007-05-10 17:26:28 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       return v:lnum - 1 == 0 ? 0 : indent(v:lnum - 1) | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | endfunction |