blob: 4d1838b3aa000cf7b994492c67324cf9121bf80d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar6e649222021-10-04 21:32:54 +01002" Language: Makefile
3" Maintainer: Doug Kearns <dougkearns@gmail.com>
4" Previous Maintainer: Nikolai Weibull <now@bitwi.se>
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +01005" Last Change: 2022 Apr 06
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
Bram Moolenaar071d4272004-06-13 20:20:40 +00007if exists("b:did_indent")
8 finish
9endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010let b:did_indent = 1
11
12setlocal indentexpr=GetMakeIndent()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000013setlocal indentkeys=!^F,o,O,<:>,=else,=endif
Bram Moolenaarf193fff2006-04-27 00:02:13 +000014setlocal nosmartindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010016let b:undo_indent = "setl inde< indk< si<"
Bram Moolenaar6e649222021-10-04 21:32:54 +010017
Bram Moolenaar071d4272004-06-13 20:20:40 +000018if exists("*GetMakeIndent")
19 finish
20endif
21
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000022let s:comment_rx = '^\s*#'
Bram Moolenaar57657d82006-04-21 22:12:41 +000023let s:rule_rx = '^[^ \t#:][^#:]*:\{1,2}\%([^=:]\|$\)'
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000024let s:continued_rule_rx = '^[^#:]*:\{1,2}\%([^=:]\|$\)'
Bram Moolenaar57657d82006-04-21 22:12:41 +000025let s:continuation_rx = '\\$'
Bram Moolenaar4c295022021-05-02 17:19:11 +020026let s:assignment_rx = '^\s*\h\w*\s*[+:?]\==\s*\zs.*\\$'
27let s:folded_assignment_rx = '^\s*\h\w*\s*[+:?]\=='
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000028" TODO: This needs to be a lot more restrictive in what it matches.
29let s:just_inserted_rule_rx = '^\s*[^#:]\+:\{1,2}$'
30let s:conditional_directive_rx = '^ *\%(ifn\=\%(eq\|def\)\|else\)\>'
31let s:end_conditional_directive_rx = '^\s*\%(else\|endif\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000033function s:remove_continuation(line)
34 return substitute(a:line, s:continuation_rx, "", "")
35endfunction
36
Bram Moolenaar071d4272004-06-13 20:20:40 +000037function GetMakeIndent()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000038 " TODO: Should this perhaps be v:lnum -1?
39" let prev_lnum = prevnonblank(v:lnum - 1)
40 let prev_lnum = v:lnum - 1
41 if prev_lnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 return 0
43 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000044 let prev_line = getline(prev_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000045
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000046 let prev_prev_lnum = prev_lnum - 1
47 let prev_prev_line = prev_prev_lnum != 0 ? getline(prev_prev_lnum) : ""
Bram Moolenaar57657d82006-04-21 22:12:41 +000048
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000049 " TODO: Deal with comments. In comments, continuations aren't interesting.
50 if prev_line =~ s:continuation_rx
51 if prev_prev_line =~ s:continuation_rx
52 return indent(prev_lnum)
53 elseif prev_line =~ s:rule_rx
Bram Moolenaar036986f2017-03-16 17:41:02 +010054 return shiftwidth()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000055 elseif prev_line =~ s:assignment_rx
56 call cursor(prev_lnum, 1)
Bram Moolenaarf193fff2006-04-27 00:02:13 +000057 if search(s:assignment_rx, 'W') != 0
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000058 return virtcol('.') - 1
59 else
60 " TODO: ?
Bram Moolenaar036986f2017-03-16 17:41:02 +010061 return shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +000062 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000063 else
64 " TODO: OK, this might be a continued shell command, so perhaps indent
65 " properly here? Leave this out for now, but in the next release this
66 " should be using indent/sh.vim somehow.
67 "if prev_line =~ '^\t' " s:rule_command_rx
68 " if prev_line =~ '^\s\+[@-]\%(if\)\>'
69 " return indent(prev_lnum) + 2
70 " endif
71 "endif
Bram Moolenaar036986f2017-03-16 17:41:02 +010072 return indent(prev_lnum) + shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +000073 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000074 elseif prev_prev_line =~ s:continuation_rx
75 let folded_line = s:remove_continuation(prev_prev_line) . ' ' . s:remove_continuation(prev_line)
76 let lnum = prev_prev_lnum - 1
77 let line = getline(lnum)
78 while line =~ s:continuation_rx
79 let folded_line = s:remove_continuation(line) . ' ' . folded_line
Bram Moolenaar57657d82006-04-21 22:12:41 +000080 let lnum -= 1
81 let line = getline(lnum)
82 endwhile
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000083 let folded_lnum = lnum + 1
84 if folded_line =~ s:rule_rx
85 if getline(v:lnum) =~ s:rule_rx
86 return 0
87 else
88 return &ts
89 endif
90 else
91" elseif folded_line =~ s:folded_assignment_rx
92 if getline(v:lnum) =~ s:rule_rx
93 return 0
94 else
95 return indent(folded_lnum)
96 endif
97" else
98" " TODO: ?
99" return indent(prev_lnum)
100 endif
101 elseif prev_line =~ s:rule_rx
102 if getline(v:lnum) =~ s:rule_rx
103 return 0
104 else
105 return &ts
106 endif
107 elseif prev_line =~ s:conditional_directive_rx
Bram Moolenaar036986f2017-03-16 17:41:02 +0100108 return shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000109 else
Bram Moolenaar2bb8df22007-05-10 17:26:28 +0000110 let line = getline(v:lnum)
111 if line =~ s:just_inserted_rule_rx
112 return 0
113 elseif line =~ s:end_conditional_directive_rx
Bram Moolenaar036986f2017-03-16 17:41:02 +0100114 return v:lnum - 1 == 0 ? 0 : indent(v:lnum - 1) - shiftwidth()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +0000115 else
116 return v:lnum - 1 == 0 ? 0 : indent(v:lnum - 1)
117 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119endfunction