robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
2ea14c0cf4
master
1" Vim indent file 2" Language: Makefile 3" Maintainer: Doug Kearns <dougkearns@gmail.com> 4" Previous Maintainer: Nikolai Weibull <now@bitwi.se> 5" Last Change: 2022 Apr 06 6" 2025 Apr 22 by Vim Project: do not indent after special targets #17183 7 8if exists ( "b:did_indent" ) 9finish 10endif 11let b: did_indent = 1 12 13setlocal indentexpr = GetMakeIndent () 14setlocal indentkeys = !^F,o,O,<:>,=else,=endif 15setlocal nosmartindent 16 17let b: undo_indent = "setl inde< indk< si<" 18 19if exists ( "*GetMakeIndent" ) 20finish 21endif 22 23let s: comment_rx = '^\s*#' 24let s: rule_rx = '^[^ \t#:][^#:]*:\{1,2}\%([^=:]\|$\)' 25" .PHONY, .DELETE_ON_ERROR, etc 26let s: rule_special = '^\.[A-Z_]\+\s*:' 27let s: continued_rule_rx = '^[^#:]*:\{1,2}\%([^=:]\|$\)' 28let s: continuation_rx = '\\$' 29let s: assignment_rx = '^\s*\h\w*\s*[+:?]\==\s*\zs.*\\$' 30let s: folded_assignment_rx = '^\s*\h\w*\s*[+:?]\==' 31" TODO: This needs to be a lot more restrictive in what it matches. 32let s: just_inserted_rule_rx = '^\s*[^#:]\+:\{1,2}$' 33let s: conditional_directive_rx = '^ *\%(ifn\=\%(eq\|def\)\|else\)\>' 34let s: end_conditional_directive_rx = '^\s*\%(else\|endif\)\>' 35 36function s: remove_continuation ( line ) 37return substitute ( a: line , s: continuation_rx , "" , "" ) 38endfunction 39 40function GetMakeIndent () 41" TODO: Should this perhaps be v:lnum -1? 42" let prev_lnum = prevnonblank(v:lnum - 1) 43let prev_lnum = v: lnum - 1 44if prev_lnum == 0 45return 0 46endif 47let prev_line = getline ( prev_lnum ) 48 49let prev_prev_lnum = prev_lnum - 1 50let prev_prev_line = prev_prev_lnum != 0 ? getline ( prev_prev_lnum ) : "" 51 52" TODO: Deal with comments. In comments, continuations aren't interesting. 53if prev_line =~ s: continuation_rx 54if prev_prev_line =~ s: continuation_rx 55return indent ( prev_lnum ) 56elseif prev_line =~ s: rule_rx && prev_line !~ s: rule_special 57return shiftwidth () 58elseif prev_line =~ s: assignment_rx 59call cursor ( prev_lnum , 1 ) 60if search ( s: assignment_rx , 'W' ) != 0 61return virtcol ( '.' ) - 1 62else 63" TODO: ? 64return shiftwidth () 65endif 66else 67" TODO: OK, this might be a continued shell command, so perhaps indent 68" properly here? Leave this out for now, but in the next release this 69" should be using indent/sh.vim somehow. 70"if prev_line =~ '^\t' " s:rule_command_rx 71" if prev_line =~ '^\s\+[@-]\%(if\)\>' 72" return indent(prev_lnum) + 2 73" endif 74"endif 75return indent ( prev_lnum ) + shiftwidth () 76endif 77elseif prev_prev_line =~ s: continuation_rx 78let folded_line = s: remove_continuation ( prev_prev_line ) . ' ' . s: remove_continuation ( prev_line ) 79let lnum = prev_prev_lnum - 1 80let line = getline ( lnum ) 81while line =~ s: continuation_rx 82let folded_line = s: remove_continuation ( line ) . ' ' . folded_line 83let lnum -= 1 84let line = getline ( lnum ) 85endwhile 86let folded_lnum = lnum + 1 87if folded_line =~ s: rule_rx && prev_line !~ s: rule_special 88if getline ( v: lnum ) =~ s: rule_rx && prev_line !~ s: rule_special 89return 0 90else 91return & ts 92endif 93else 94" elseif folded_line =~ s:folded_assignment_rx 95if getline ( v: lnum ) =~ s: rule_rx && prev_line !~ s: rule_special 96return 0 97else 98return indent ( folded_lnum ) 99endif 100" else 101" " TODO: ? 102" return indent(prev_lnum) 103endif 104elseif prev_line =~ s: rule_rx && prev_line !~ s: rule_special 105if getline ( v: lnum ) =~ s: rule_rx && prev_line !~ s: rule_special 106return 0 107else 108return & ts 109endif 110elseif prev_line =~ s: conditional_directive_rx 111return shiftwidth () 112else 113let line = getline ( v: lnum ) 114if line =~ s: just_inserted_rule_rx 115return 0 116elseif line =~ s: end_conditional_directive_rx 117return v: lnum - 1 == 0 ? 0 : indent ( v: lnum - 1 ) - shiftwidth () 118else 119return v: lnum - 1 == 0 ? 0 : indent ( v: lnum - 1 ) 120endif 121endif 122endfunction