Meta/indent-cpp-directive: C preprocessor directive indentation rules
Junio C Hamano committed
Jul 15, 2025 at 14:19 UTC
f4840fd2243ec8d6fc6cd985bcc4c891bd728c1b
1 file changed
+32
indent-cpp-directive.perl
new
+32
@@ -0,0 +1,32 @@
1
+#!/usr/bin/perl
2
+
3
+use strict;
4
+use warnings;
5
+
6
+my $indent_level = -1;
7
+
8
+sub emit {
9
+ my $indent = $indent_level <= 0 ? "" : " " x $indent_level;
10
+ printf "#%s%s", $indent, $_;
11
+}
12
+
13
+while (<>) {
14
+ unless (s/^\s*#\s*//) {
15
+ print;
16
+ next;
17
+ }
18
+
19
+ if (/^if/) {
20
+ emit($_);
21
+ $indent_level++;
22
+ } elsif (/^el/) {
23
+ $indent_level--;
24
+ emit($_);
25
+ $indent_level++;
26
+ } elsif (/^endif/) {
27
+ $indent_level--;
28
+ emit($_);
29
+ } else {
30
+ emit($_);
31
+ }
32
+}