Debian Codegraph @

texk/texlive/linked_scripts/latexindent/latexindent.pl

raw
1 #!/usr/bin/env perl
2 #
3 # latexindent.pl, version 3.24.7, 2025-08-15
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # See http://www.gnu.org/licenses/.
16 #
17 # Chris Hughes, 2017-2025
18 #
19 # For all communication, please visit: https://github.com/cmhughes/latexindent.pl
20
21 use strict;
22 use warnings;
23 use FindBin; # help find defaultSettings.yaml
24 use Getopt::Long; # to get the switches/options/flags
25
26 # use lib to make sure that @INC contains the latexindent directory
27 use lib $FindBin::RealBin;
28 use LatexIndent::Document;
29
30 use utf8;
31 use Encode qw/ encode decode find_encoding /;
32
33 use LatexIndent::LogFile qw/$logger/;
34 use LatexIndent::UTF8CmdLineArgsFileOperation
35 qw/commandlineargs_with_encode @new_args/;
36
37 commandlineargs_with_encode();
38 my $commandlineargs = join( ", ", @ARGV );
39
40 # get the options
41 my %switches = ( readLocalSettings => 0 );
42
43 GetOptions(
44 "version|v" => \$switches{version},
45 "vversion|vv" => \$switches{vversion},
46 "silent|s" => \$switches{silentMode},
47 "trace|t" => \$switches{trace},
48 "ttrace|tt" => \$switches{ttrace},
49 "local|l:s" => \$switches{readLocalSettings},
50 "yaml|y=s" => \$switches{yaml},
51 "onlydefault|d" => \$switches{onlyDefault},
52 "overwrite|w" => \$switches{overwrite},
53 "overwriteIfDifferent|wd" => \$switches{overwriteIfDifferent},
54 "outputfile|o=s" => \$switches{outputToFile},
55 "modifylinebreaks|m" => \$switches{modifyLineBreaks},
56 "logfile|g=s" => \$switches{logFileName},
57 "help|h" => \$switches{showhelp},
58 "cruft|c=s" => \$switches{cruftDirectory},
59 "screenlog|sl" => \$switches{screenlog},
60 "replacement|r" => \$switches{replacement},
61 "onlyreplacement|rr" => \$switches{onlyreplacement},
62 "replacementrespectverb|rv" => \$switches{replacementRespectVerb},
63 "check|k" => \$switches{check},
64 "checkv|kv" => \$switches{checkverbose},
65 "lines|n=s" => \$switches{lines},
66 "GCString" => \$switches{GCString},
67 );
68
69 $logger = LatexIndent::Logger->new();
70 our $consoleoutcp;
71 if ( $FindBin::Script eq 'latexindent.exe' ) {
72 require Win32;
73 import Win32;
74
75 my $encoding_sys = Win32::GetACP()
76 ; #https://stackoverflow.com/a/63868721HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
77 $consoleoutcp = Win32::GetConsoleOutputCP();
78 Win32::SetConsoleOutputCP(65001);
79
80 $logger->info("*ANSI Code Page: $encoding_sys");
81 if ( $switches{screenlog} ) {
82 print "INFO: ANSI Code Page: $encoding_sys\n"
83 ; #The values of ACP in the registry
84 print "INFO: Current console output code page: $consoleoutcp \n";
85 print "INFO: Change the current console output code page to 65001\n";
86 }
87 }
88
89 if ( $switches{screenlog} ) {
90 $logger->info("*Command line:");
91 $logger->info("@new_args");
92 $logger->info( "Command line arguments:\n" . $commandlineargs );
93 print "INFO: Command line:\n @new_args\n";
94 print " Command line arguments:\n " . $commandlineargs . "\n\n";
95 }
96
97 # conditionally load the GCString module
98 eval "use Unicode::GCString" if $switches{GCString};
99
100 # check local settings doesn't interfere with reading the file;
101 # this can happen if the script is called as follows:
102 #
103 # latexindent.pl -l myfile.tex
104 #
105 # in which case, the GetOptions routine mistakes myfile.tex
106 # as the optional parameter to the l flag.
107 #
108 # In such circumstances, we correct the mistake by assuming that
109 # the only argument is the file to be indented, and place it in @ARGV
110 if ( $switches{readLocalSettings} and scalar(@ARGV) < 1 ) {
111 push( @ARGV, $switches{readLocalSettings} );
112 $switches{readLocalSettings} = '';
113 }
114
115 # allow STDIN as input, if a filename is not present
116 unshift( @ARGV, '-' ) unless @ARGV;
117
118 my $document = bless(
119 {
120 name => "mainDocument",
121 modifyLineBreaksYamlName => "mainDocument",
122 switches => \%switches
123 },
124 "LatexIndent::Document"
125 );
126 $document->latexindent( \@ARGV );
127
128 if ( $FindBin::Script eq 'latexindent.exe' ) {
129 Win32::SetConsoleOutputCP($consoleoutcp);
130 if ( $switches{screenlog} ) {
131 print
132 "\n\nINFO: Restore the console output code page: $consoleoutcp\n";
133 }
134 }
135 exit(0);