blob: aeea4de741d4772cbe16e44fa2af32d287980008 [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
Eisuke Kawashima7bc98802025-04-22 20:20:46 +02006" 2025 Apr 22 by Vim Project: do not indent after special targets #17183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008if exists("b:did_indent")
9 finish
10endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011let b:did_indent = 1
12
13setlocal indentexpr=GetMakeIndent()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000014setlocal indentkeys=!^F,o,O,<:>,=else,=endif
Bram Moolenaarf193fff2006-04-27 00:02:13 +000015setlocal nosmartindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010017let b:undo_indent = "setl inde< indk< si<"
Bram Moolenaar6e649222021-10-04 21:32:54 +010018
Bram Moolenaar071d4272004-06-13 20:20:40 +000019if exists("*GetMakeIndent")
20 finish
21endif
22
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000023let s:comment_rx = '^\s*#'
Bram Moolenaar57657d82006-04-21 22:12:41 +000024let s:rule_rx = '^[^ \t#:][^#:]*:\{1,2}\%([^=:]\|$\)'
Eisuke Kawashima7bc98802025-04-22 20:20:46 +020025" .PHONY, .DELETE_ON_ERROR, etc
26let s:rule_special = '^\.[A-Z_]\+\s*:'
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000027let s:continued_rule_rx = '^[^#:]*:\{1,2}\%([^=:]\|$\)'
Bram Moolenaar57657d82006-04-21 22:12:41 +000028let s:continuation_rx = '\\$'
Bram Moolenaar4c295022021-05-02 17:19:11 +020029let s:assignment_rx = '^\s*\h\w*\s*[+:?]\==\s*\zs.*\\$'
30let s:folded_assignment_rx = '^\s*\h\w*\s*[+:?]\=='
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000031" 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\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000036function s:remove_continuation(line)
37 return substitute(a:line, s:continuation_rx, "", "")
38endfunction
39
Bram Moolenaar071d4272004-06-13 20:20:40 +000040function GetMakeIndent()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000041 " TODO: Should this perhaps be v:lnum -1?
42" let prev_lnum = prevnonblank(v:lnum - 1)
43 let prev_lnum = v:lnum - 1
44 if prev_lnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 return 0
46 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000047 let prev_line = getline(prev_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000049 let prev_prev_lnum = prev_lnum - 1
50 let prev_prev_line = prev_prev_lnum != 0 ? getline(prev_prev_lnum) : ""
Bram Moolenaar57657d82006-04-21 22:12:41 +000051
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000052 " TODO: Deal with comments. In comments, continuations aren't interesting.
53 if prev_line =~ s:continuation_rx
54 if prev_prev_line =~ s:continuation_rx
55 return indent(prev_lnum)
Eisuke Kawashima7bc98802025-04-22 20:20:46 +020056 elseif prev_line =~ s:rule_rx && prev_line !~ s:rule_special
Bram Moolenaar036986f2017-03-16 17:41:02 +010057 return shiftwidth()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000058 elseif prev_line =~ s:assignment_rx
59 call cursor(prev_lnum, 1)
Bram Moolenaarf193fff2006-04-27 00:02:13 +000060 if search(s:assignment_rx, 'W') != 0
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000061 return virtcol('.') - 1
62 else
63 " TODO: ?
Bram Moolenaar036986f2017-03-16 17:41:02 +010064 return shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +000065 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000066 else
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
Bram Moolenaar036986f2017-03-16 17:41:02 +010075 return indent(prev_lnum) + shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +000076 endif
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000077 elseif prev_prev_line =~ s:continuation_rx
78 let folded_line = s:remove_continuation(prev_prev_line) . ' ' . s:remove_continuation(prev_line)
79 let lnum = prev_prev_lnum - 1
80 let line = getline(lnum)
81 while line =~ s:continuation_rx
82 let folded_line = s:remove_continuation(line) . ' ' . folded_line
Bram Moolenaar57657d82006-04-21 22:12:41 +000083 let lnum -= 1
84 let line = getline(lnum)
85 endwhile
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000086 let folded_lnum = lnum + 1
Eisuke Kawashima7bc98802025-04-22 20:20:46 +020087 if folded_line =~ s:rule_rx && prev_line !~ s:rule_special
88 if getline(v:lnum) =~ s:rule_rx && prev_line !~ s:rule_special
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000089 return 0
90 else
91 return &ts
92 endif
93 else
94" elseif folded_line =~ s:folded_assignment_rx
Eisuke Kawashima7bc98802025-04-22 20:20:46 +020095 if getline(v:lnum) =~ s:rule_rx && prev_line !~ s:rule_special
Bram Moolenaar2bb8df22007-05-10 17:26:28 +000096 return 0
97 else
98 return indent(folded_lnum)
99 endif
100" else
101" " TODO: ?
102" return indent(prev_lnum)
103 endif
Eisuke Kawashima7bc98802025-04-22 20:20:46 +0200104 elseif prev_line =~ s:rule_rx && prev_line !~ s:rule_special
105 if getline(v:lnum) =~ s:rule_rx && prev_line !~ s:rule_special
Bram Moolenaar2bb8df22007-05-10 17:26:28 +0000106 return 0
107 else
108 return &ts
109 endif
110 elseif prev_line =~ s:conditional_directive_rx
Bram Moolenaar036986f2017-03-16 17:41:02 +0100111 return shiftwidth()
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000112 else
Bram Moolenaar2bb8df22007-05-10 17:26:28 +0000113 let line = getline(v:lnum)
114 if line =~ s:just_inserted_rule_rx
115 return 0
116 elseif line =~ s:end_conditional_directive_rx
Bram Moolenaar036986f2017-03-16 17:41:02 +0100117 return v:lnum - 1 == 0 ? 0 : indent(v:lnum - 1) - shiftwidth()
Bram Moolenaar2bb8df22007-05-10 17:26:28 +0000118 else
119 return v:lnum - 1 == 0 ? 0 : indent(v:lnum - 1)
120 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122endfunction