Meta/indent-cpp-directive: C preprocessor directive indentation rules

This commit is contained in:
Junio C Hamano
2025-07-15 14:19:20 -07:00
parent e8bdc59325
commit f4840fd224

32
indent-cpp-directive.perl Executable file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/perl
use strict;
use warnings;
my $indent_level = -1;
sub emit {
my $indent = $indent_level <= 0 ? "" : " " x $indent_level;
printf "#%s%s", $indent, $_;
}
while (<>) {
unless (s/^\s*#\s*//) {
print;
next;
}
if (/^if/) {
emit($_);
$indent_level++;
} elsif (/^el/) {
$indent_level--;
emit($_);
$indent_level++;
} elsif (/^endif/) {
$indent_level--;
emit($_);
} else {
emit($_);
}
}