summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2024-03-16 20:09:04 +0000
committerKarl Berry <karl@freefriends.org>2024-03-16 20:09:04 +0000
commit1d06d9457548a52ff903cfdad6260867033fd5fd (patch)
treeffd982654c807e2b0c966a13d5221f8f344b2fe4 /Master/texmf-dist/scripts
parent1eb566c18a9d63c253c14291552086e4119ec2db (diff)
latexindent (16mar24)
git-svn-id: svn://tug.org/texlive/trunk@70666 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts')
-rw-r--r--Master/texmf-dist/scripts/latexindent/LatexIndent/BackUpFileProcedure.pm18
-rw-r--r--Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm30
-rw-r--r--Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm27
-rw-r--r--Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm17
-rw-r--r--Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm18
-rw-r--r--Master/texmf-dist/scripts/latexindent/LatexIndent/UTF8CmdLineArgsFileOperation.pm209
-rw-r--r--Master/texmf-dist/scripts/latexindent/LatexIndent/Version.pm4
-rwxr-xr-xMaster/texmf-dist/scripts/latexindent/defaultSettings.yaml2
-rwxr-xr-xMaster/texmf-dist/scripts/latexindent/latexindent.pl48
9 files changed, 327 insertions, 46 deletions
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/BackUpFileProcedure.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/BackUpFileProcedure.pm
index 7828c44be3d..f88304fc6cf 100644
--- a/Master/texmf-dist/scripts/latexindent/LatexIndent/BackUpFileProcedure.pm
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/BackUpFileProcedure.pm
@@ -26,6 +26,10 @@ use Exporter qw/import/;
use Encode qw/decode/;
our @EXPORT_OK = qw/create_back_up_file check_if_different/;
+use LatexIndent::UTF8CmdLineArgsFileOperation
+ qw/copy_with_encode exist_with_encode open_with_encode zero_with_encode read_yaml_with_encode/;
+use utf8;
+
# copy main file to a backup in the case of the overwrite switch being active
sub create_back_up_file {
@@ -36,7 +40,7 @@ sub create_back_up_file {
# if we want to overwrite the current file create a backup first
$logger->info("*Backup procedure (-w flag active):");
- my $fileName = decode( "utf-8", ${$self}{fileName} );
+ my $fileName = ${$self}{fileName};
# grab the file extension preferences
my %fileExtensionPreference = %{ $mainSettings{fileExtensionPreference} };
@@ -49,7 +53,7 @@ sub create_back_up_file {
my $backupFileNoExt = basename( ${$self}{fileName}, @fileExtensions );
# add the user's backup directory to the backup path
- $backupFileNoExt = decode( "utf-8", "${$self}{cruftDirectory}/$backupFileNoExt" );
+ $backupFileNoExt = "${$self}{cruftDirectory}/$backupFileNoExt";
# local variables, determined from the YAML settings
my $onlyOneBackUp = $mainSettings{onlyOneBackUp};
@@ -74,7 +78,7 @@ sub create_back_up_file {
# if the file already exists, increment the number until either
# the file does not exist, or you reach the maximal number of backups
- while ( -e ( $backupFile . $backupCounter ) and $backupCounter != ( $maxNumberOfBackUps - 1 ) ) {
+ while ( exist_with_encode( $backupFile . $backupCounter ) and $backupCounter != ( $maxNumberOfBackUps - 1 ) ) {
$logger->info("$backupFile$backupCounter already exists, incrementing by 1 (see maxNumberOfBackUps)");
$backupCounter++;
}
@@ -83,7 +87,7 @@ sub create_back_up_file {
# if the backup file already exists, output some information in the log file
# and proceed to cycleThroughBackUps if the latter is set
- if ( -e $backupFile ) {
+ if ( exist_with_encode($backupFile) ) {
if ($onlyOneBackUp) {
$logger->info("$backupFile will be overwritten (see onlyOneBackUp)");
}
@@ -105,9 +109,9 @@ sub create_back_up_file {
$newBackupFile = $backupFileNoExt . $backupExtension . ( $i - 1 );
# check that the oldBackupFile exists
- if ( -e $oldBackupFile ) {
+ if ( exist_with_encode($oldBackupFile) ) {
$logger->info("Copying $oldBackupFile to $newBackupFile...");
- if ( !( copy( $oldBackupFile, $newBackupFile ) ) ) {
+ if ( !( copy_with_encode( $oldBackupFile, $newBackupFile ) ) ) {
$logger->fatal("*Could not write to backup file $newBackupFile. Please check permissions.");
$logger->fatal("Exiting, no indentation done.");
$self->output_logfile();
@@ -122,7 +126,7 @@ sub create_back_up_file {
# output these lines to the log file
$logger->info("Backing up $fileName to $backupFile...");
$logger->info("$fileName will be overwritten after indentation");
- if ( !( copy( $fileName, $backupFile ) ) ) {
+ if ( !( copy_with_encode( $fileName, $backupFile ) ) ) {
$logger->fatal("*Could not write to backup file $backupFile. Please check permissions.");
$logger->fatal("Exiting, no indentation done.");
$self->output_logfile();
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm
index 04c8e638dc0..b6d2fa582a6 100644
--- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm
@@ -19,7 +19,8 @@ use strict;
use warnings;
use Data::Dumper;
use File::Basename; # to get the filename and directory path
-use open ':std', ':encoding(UTF-8)';
+
+#use open ':std', ':encoding(UTF-8)';
use Encode qw/decode/;
# gain access to subroutines in the following modules
@@ -73,6 +74,10 @@ use LatexIndent::Heading qw/find_heading construct_headings_levels $allHead
use LatexIndent::FileContents qw/find_file_contents_environments_and_preamble/;
use LatexIndent::Preamble;
+use LatexIndent::UTF8CmdLineArgsFileOperation
+ qw/copy_with_encode exist_with_encode open_with_encode zero_with_encode read_yaml_with_encode/;
+use utf8;
+
sub new {
# Create new objects, with optional key/value pairs
@@ -98,7 +103,7 @@ sub latexindent {
# one-time operations
$self->store_switches;
- ${$self}{fileName} = decode( "utf-8", $fileNames[0] );
+ ${$self}{fileName} = $fileNames[0];
$self->process_switches( \@fileNames );
$self->yaml_read_settings;
@@ -231,19 +236,18 @@ sub output_indented_text {
if ( ${$self}{overwrite} ) {
# diacritics in file names (highlighted in https://github.com/cmhughes/latexindent.pl/pull/439)
- ${$self}{fileName} = decode( "utf-8", ${$self}{fileName} );
+ ${$self}{fileName} = ${$self}{fileName};
$logger->info("Overwriting file ${$self}{fileName}");
- open( OUTPUTFILE, ">", ${$self}{fileName} );
- print OUTPUTFILE ${$self}{body};
- close(OUTPUTFILE);
+ my $OUTPUTFILE = open_with_encode( '>:encoding(UTF-8)', ${$self}{fileName} );
+ print $OUTPUTFILE ${$self}{body};
+ close($OUTPUTFILE);
}
elsif ( $switches{outputToFile} ) {
-
$logger->info("Outputting to file ${$self}{outputToFile}");
- open( OUTPUTFILE, ">", ${$self}{outputToFile} );
- print OUTPUTFILE ${$self}{body};
- close(OUTPUTFILE);
+ my $OUTPUTFILE = open_with_encode( '>:encoding(UTF-8)', ${$self}{outputToFile} );
+ print $OUTPUTFILE ${$self}{body};
+ close($OUTPUTFILE);
}
else {
$logger->info("Not outputting to file; see -w and -o switches for more options.");
@@ -268,10 +272,10 @@ sub output_logfile {
if ${ $mainSettings{logFilePreferences} }{showGitHubInfoFooter};
# open log file
- my $logfileName = $switches{logFileName} || "indent.log";
- my $logfile;
+ my $logfileName = $switches{logFileName} || "indent.log";
my $logfilePossible = 1;
- open( $logfile, ">", "${$self}{cruftDirectory}/$logfileName" ) or $logfilePossible = 0;
+ my $logfile = open_with_encode( '>:encoding(UTF-8)', "${$self}{cruftDirectory}/$logfileName" )
+ or $logfilePossible = 0;
if ($logfilePossible) {
foreach my $line ( @{LatexIndent::Logger::logFileLines} ) {
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm
index bd909b2f60c..64b5f0a37e5 100644
--- a/Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm
@@ -18,7 +18,8 @@ package LatexIndent::FileExtension;
use strict;
use warnings;
use PerlIO::encoding;
-use open ':std', ':encoding(UTF-8)';
+
+#use open ':std', ':encoding(UTF-8)';
use File::Basename; # to get the filename and directory path
use Exporter qw/import/;
use Encode qw/decode/;
@@ -27,6 +28,10 @@ use LatexIndent::Switches qw/%switches $is_check_switch_active/;
use LatexIndent::LogFile qw/$logger/;
our @EXPORT_OK = qw/file_extension_check/;
+use LatexIndent::UTF8CmdLineArgsFileOperation
+ qw/copy_with_encode exist_with_encode open_with_encode zero_with_encode read_yaml_with_encode/;
+use utf8;
+
sub file_extension_check {
my $self = shift;
@@ -59,7 +64,7 @@ sub file_extension_check {
# loop through the known file extensions (see @fileExtensions)
foreach (@fileExtensions) {
- if ( -e $fileName . $_ ) {
+ if ( exist_with_encode( $fileName . $_ ) ) {
$logger->info("$fileName$_ found!");
$fileName .= $_;
$logger->info("Updated fileName to $fileName");
@@ -91,7 +96,7 @@ sub file_extension_check {
}
else {
# if the file has a recognised extension, check that the file exists
- unless ( -e $fileName ) {
+ unless ( exist_with_encode($fileName) ) {
if ( defined ${$self}{multipleFiles} ) {
$logger->warn("*I couldn't find $fileName, are you sure it exists?");
$logger->warn("moving on, no indentation done for ${$self}{fileName}.");
@@ -120,7 +125,7 @@ sub file_extension_check {
# note, related:
#
# git config --add core.quotePath false
- ${$self}{outputToFile} = decode( "utf-8", $switches{outputToFile} );
+ ${$self}{outputToFile} = $switches{outputToFile};
if ( $fileName eq "-" and $switches{outputToFile} =~ m/^\+/ ) {
$logger->info("STDIN input mode active, -o switch is removing all + symbols");
@@ -130,7 +135,7 @@ sub file_extension_check {
# the -o file name might begin with a + symbol
if ( $switches{outputToFile} =~ m/^\+(.*)/ and $1 ne "+" ) {
$logger->info("-o switch called with + symbol at the beginning: ${$self}{outputToFile}");
- ${$self}{outputToFile} = decode( "utf-8", ${$self}{baseName} . $1 );
+ ${$self}{outputToFile} = ${$self}{baseName} . $1;
$logger->info("output file is now: ${$self}{outputToFile}");
}
@@ -143,9 +148,9 @@ sub file_extension_check {
# if there is no extension, then add the extension from the file to be operated upon
if ( !$ext ) {
- $logger->info(
- "-o switch called with file name without extension: " . decode( "utf-8", $switches{outputToFile} ) );
+ $logger->info( "-o switch called with file name without extension: " . $switches{outputToFile} );
${$self}{outputToFile} = $name . ( $name =~ m/\.\z/ ? q() : "." ) . $strippedFileExtension;
+
$logger->info(
"Updated to ${$self}{outputToFile} as the file extension of the input file is $strippedFileExtension");
}
@@ -160,7 +165,7 @@ sub file_extension_check {
my $outputFileCounter = 0;
my $fileName = $name . $outputFileCounter . "." . $strippedFileExtension;
$logger->info("will search for existence and increment counter, starting with $fileName");
- while ( -e $fileName ) {
+ while ( exist_with_encode($fileName) ) {
$logger->info("$fileName exists, incrementing counter");
$outputFileCounter++;
$fileName = $name . $outputFileCounter . "." . $strippedFileExtension;
@@ -174,7 +179,7 @@ sub file_extension_check {
my @lines;
if ( $fileName ne "-" ) {
my $openFilePossible = 1;
- open( MAINFILE, $fileName ) or ( $openFilePossible = 0 );
+ my $MAINFILE = open_with_encode( '<:encoding(UTF-8)', $fileName ) or ( $openFilePossible = 0 );
if ( $openFilePossible == 0 ) {
if ( defined ${$self}{multipleFiles} ) {
$logger->warn("*$fileName exists, but could not open it");
@@ -188,8 +193,8 @@ sub file_extension_check {
exit(4);
}
}
- push( @lines, $_ ) while (<MAINFILE>);
- close(MAINFILE);
+ push( @lines, $_ ) while (<$MAINFILE>);
+ close($MAINFILE);
}
else {
push( @lines, $_ ) while (<>);
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm
index 7f3f507076d..9e5ff7a8538 100644
--- a/Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm
@@ -34,6 +34,10 @@ our $defaultSettings;
# master yaml settings is a hash, global to this module
our %mainSettings;
+use LatexIndent::UTF8CmdLineArgsFileOperation
+ qw/copy_with_encode exist_with_encode open_with_encode zero_with_encode read_yaml_with_encode/;
+use utf8;
+
# previously found settings is a hash, global to this module
our %previouslyFoundSettings;
@@ -344,14 +348,15 @@ sub yaml_read_settings {
}
# diacritics in YAML names (highlighted in https://github.com/cmhughes/latexindent.pl/pull/439)
- $_ = decode( "utf-8", $_ );
+ #$_ = decode( "utf-8", $_ );
+ $_ = $_;
# check for existence and non-emptiness
- if ( ( -e $_ ) and !( -z $_ ) ) {
+ if ( exist_with_encode($_) and !( zero_with_encode($_) ) ) {
$logger->info("Adding $_ to YAML read paths");
push( @absPaths, "$_" );
}
- elsif ( !( -e $_ ) ) {
+ elsif ( !( exist_with_encode($_) ) ) {
if (( $_ =~ m/localSettings|latexindent/s
and !( -e 'localSettings.yaml' )
and !( -e '.localSettings.yaml' )
@@ -373,9 +378,9 @@ sub yaml_read_settings {
foreach my $settings (@absPaths) {
# check that the settings file exists and that it isn't empty
- if ( -e $settings and !( -z $settings ) ) {
+ if ( exist_with_encode($settings) and !( zero_with_encode($settings) ) ) {
$logger->info("Reading USER settings from $settings");
- $userSettings = YAML::Tiny->read("$settings");
+ $userSettings = read_yaml_with_encode("$settings");
# if we can read userSettings
if ($userSettings) {
@@ -513,7 +518,7 @@ sub yaml_read_settings {
else {
# otherwise keep going, but put a warning in the log file
$logger->warn("*$homeDir/indentconfig.yaml");
- if ( -z $settings ) {
+ if ( zero_with_encode($settings) ) {
$logger->info("specifies $settings but this file is EMPTY -- not reading from it");
}
else {
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm
index b72655b686c..7418f1024ca 100644
--- a/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm
@@ -27,6 +27,12 @@ use Encode qw/decode/;
our @EXPORT_OK = qw/process_switches $logger/;
our $logger;
+use utf8;
+binmode( STDOUT, ":encoding(utf8)" );
+
+use LatexIndent::UTF8CmdLineArgsFileOperation
+ qw/copy_with_encode exist_with_encode open_with_encode zero_with_encode read_yaml_with_encode isdir_with_encode mkdir_with_encode/;
+
sub process_switches {
# -v switch is just to show the version number
@@ -133,10 +139,10 @@ ENDQUOTE
my $cruftDirectoryCreation = 0;
# if cruft directory does not exist, create it
- if ( !( -d ${$self}{cruftDirectory} ) ) {
- eval { make_path( ${$self}{cruftDirectory} ) };
+ if ( !( isdir_with_encode( ${$self}{cruftDirectory} ) ) ) {
+ eval { mkdir_with_encode( ${$self}{cruftDirectory} ) };
if ($@) {
- $logger->fatal( "*Could not create cruft directory " . decode( "utf-8", ${$self}{cruftDirectory} ) );
+ $logger->fatal( "*Could not create cruft directory " . ${$self}{cruftDirectory} );
$logger->fatal("Exiting, no indentation done.");
$self->output_logfile();
exit(6);
@@ -147,7 +153,7 @@ ENDQUOTE
my $logfileName = ( $switches{cruftDirectory} ? ${$self}{cruftDirectory} . "/" : '' )
. ( $switches{logFileName} || "indent.log" );
- $logfileName = decode( "utf-8", $logfileName );
+ $logfileName = $logfileName;
# details of the script to log file
$logger->info("*$FindBin::Script version $versionNumber, $versionDate, a script to indent .tex files");
@@ -204,6 +210,8 @@ ENDQUOTE
$logger->info("-c|--cruft: cruft directory") if ( $switches{cruftDirectory} );
$logger->info("-r|--replacement: replacement mode") if ( $switches{replacement} );
$logger->info("-rr|--onlyreplacement: *only* replacement mode, no indentation") if ( $switches{onlyreplacement} );
+ $logger->info("-rv|--replacementrespectverb replacement mode, respecting verbatim")
+ if ( $switches{replacementRespectVerb} );
$logger->info("-k|--check mode: will exit with 0 if document body unchanged, 1 if changed") if ( $switches{check} );
$logger->info("-kv|--check mode verbose: as in check mode, but outputs diff to screen")
if ( $switches{checkverbose} );
@@ -253,7 +261,7 @@ ENDQUOTE
}
$logger->info("*Directory for backup files and $logfileName:");
- $logger->info( $switches{cruftDirectory} ? decode( "utf-8", ${$self}{cruftDirectory} ) : ${$self}{cruftDirectory} );
+ $logger->info( $switches{cruftDirectory} ? ${$self}{cruftDirectory} : ${$self}{cruftDirectory} );
$logger->info("cruft directory creation: ${$self}{cruftDirectory}") if $cruftDirectoryCreation;
# output location of modules
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/UTF8CmdLineArgsFileOperation.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/UTF8CmdLineArgsFileOperation.pm
new file mode 100644
index 00000000000..a2ba0b1d2f7
--- /dev/null
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/UTF8CmdLineArgsFileOperation.pm
@@ -0,0 +1,209 @@
+package LatexIndent::UTF8CmdLineArgsFileOperation;
+
+use strict;
+use warnings;
+use feature qw( say state );
+use utf8;
+use Config qw( %Config );
+use Encode qw( decode encode );
+
+use Exporter qw/import/;
+our @EXPORT_OK
+ = qw/commandlineargs_with_encode @new_args copy_with_encode exist_with_encode open_with_encode zero_with_encode read_yaml_with_encode isdir_with_encode mkdir_with_encode/;
+
+sub copy_with_encode {
+ use File::Copy;
+ my ( $source, $destination ) = @_;
+
+ if ( $FindBin::Script eq 'latexindent.exe' ) {
+ require Win32::Unicode::File;
+ Win32::Unicode::File->import(qw(copyW));
+ copyW( $source, $destination, 1 );
+ }
+ else {
+ copy( $source, $destination );
+ }
+}
+
+sub exist_with_encode {
+ my ($filename) = @_;
+
+ if ( $FindBin::Script eq 'latexindent.exe' ) {
+ require Win32::Unicode::File;
+ Win32::Unicode::File->import(qw(statW));
+ return statW($filename);
+ }
+ else {
+ return -e $filename;
+ }
+}
+
+sub zero_with_encode {
+ my ($filename) = @_;
+
+ if ( $FindBin::Script eq 'latexindent.exe' ) {
+ require Win32::Unicode::File;
+ Win32::Unicode::File->import(qw(file_size));
+ my $size = file_size($filename);
+ if ($size) {
+ return 0;
+ }
+ else {
+ return 1;
+ }
+ }
+ else {
+ return -z $filename;
+ }
+}
+
+sub open_with_encode {
+ my $mode = shift;
+ my $filename = shift;
+ my $fh;
+
+ if ( $FindBin::Script eq 'latexindent.exe' ) {
+ require Win32::Unicode::File;
+ Win32::Unicode::File->import;
+ $fh = Win32::Unicode::File->new;
+ open $fh, $mode, $filename or die "Can't open file: $!";
+ return $fh;
+ }
+ else {
+ open( $fh, $mode, $filename ) or die "Can't open file: $!";
+ return $fh;
+ }
+}
+
+sub read_yaml_with_encode {
+ use YAML::Tiny;
+ my $filename = shift;
+
+ my $fh = open_with_encode( '<:encoding(UTF-8)', $filename ) or die $!;
+ my $yaml_string = join( "", <$fh> );
+ return YAML::Tiny->read_string($yaml_string);
+}
+
+sub isdir_with_encode {
+ my $path = shift;
+
+ if ( $FindBin::Script eq 'latexindent.exe' ) {
+ require Win32::Unicode::File;
+ Win32::Unicode::File->import(qw(file_type));
+
+ return file_type( 'd', $path );
+ }
+ else {
+ return -d $path;
+ }
+}
+
+sub mkdir_with_encode {
+ my $path = shift;
+
+ if ( $FindBin::Script eq 'latexindent.exe' ) {
+ require Win32::Unicode::Dir;
+ Win32::Unicode::Dir->import(qw(mkdirW));
+
+ mkdirW($path) or die "Cannot create directory $path: $!";
+ }
+ else {
+ require File::Path;
+ File::Path->import(qw(make_path));
+
+ my $created = make_path($path);
+ die "Cannot create directory $path" unless $created;
+ }
+}
+
+#https://stackoverflow.com/a/63868721
+#https://stackoverflow.com/a/44489228
+sub commandlineargs_with_encode {
+ if ( $FindBin::Script eq 'latexindent.exe' ) {
+ require Win32::API;
+ import Win32::API qw( ReadMemory );
+
+ #use open ':std', ':encoding('.do { require Win32; "cp".Win32::GetConsoleOutputCP() }.')';
+
+ use constant ptr_size => $Config{ptrsize};
+
+ use constant ptr_pack_format => ptr_size == 8 ? 'Q'
+ : ptr_size == 4 ? 'L'
+ : die("Unrecognized ptrsize\n");
+
+ use constant ptr_win32api_type => ptr_size == 8 ? 'Q'
+ : ptr_size == 4 ? 'N'
+ : die("Unrecognized ptrsize\n");
+
+ sub lstrlenw {
+ my ($ptr) = @_;
+
+ state $lstrlenw = Win32::API->new( 'kernel32', 'lstrlenW', ptr_win32api_type, 'i' )
+ or die($^E);
+
+ return $lstrlenw->Call($ptr);
+ }
+
+ sub decode_lpcwstr {
+ my ($ptr) = @_;
+ return undef if !$ptr;
+
+ my $num_chars = lstrlenw($ptr)
+ or return '';
+
+ return decode( 'UTF-16le', ReadMemory( $ptr, $num_chars * 2 ) );
+ }
+
+ # Returns true on success. Returns false and sets $^E on error.
+ sub localfree {
+ my ($ptr) = @_;
+
+ state $localfree = Win32::API->new( 'kernel32', 'LocalFree', ptr_win32api_type, ptr_win32api_type )
+ or die($^E);
+
+ return $localfree->Call($ptr) == 0;
+ }
+
+ sub getcommandline {
+ state $getcommandline = Win32::API->new( 'kernel32', 'GetCommandLineW', '', ptr_win32api_type )
+ or die($^E);
+
+ return decode_lpcwstr( $getcommandline->Call() );
+ }
+
+ # Returns a reference to an array on success. Returns undef and sets $^E on error.
+ sub commandlinetoargv {
+ my ($cmd_line) = @_;
+
+ state $commandlinetoargv = Win32::API->new( 'shell32', 'CommandLineToArgvW', 'PP', ptr_win32api_type )
+ or die($^E);
+
+ my $cmd_line_encoded = encode( 'UTF-16le', $cmd_line . "\0" );
+ my $num_args_buf = pack( 'i', 0 ); # Allocate space for an "int".
+
+ my $arg_ptrs_ptr = $commandlinetoargv->Call( $cmd_line_encoded, $num_args_buf )
+ or return undef;
+
+ my $num_args = unpack( 'i', $num_args_buf );
+ my @args = map { decode_lpcwstr($_) } unpack ptr_pack_format . '*',
+ ReadMemory( $arg_ptrs_ptr, ptr_size * $num_args );
+
+ localfree($arg_ptrs_ptr);
+ return \@args;
+ }
+
+ my $cmd_line = getcommandline();
+ our @new_args = $cmd_line;
+ $cmd_line =~ s/(^(.*?)\.pl\"? )//g;
+ $cmd_line =~ s/(^(.*?)\.exe\"? )//g;
+ my $args = commandlinetoargv($cmd_line) or die("CommandLineToArgv: $^E\n");
+ @ARGV = @{$args};
+ }
+ else {
+ my $encodingObject = "utf-8";
+ @ARGV = map { decode( $encodingObject, $_ ) } @ARGV;
+ our @new_args = @ARGV;
+ }
+}
+
+1;
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Version.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Version.pm
index f56a438ec04..5da34c49b70 100644
--- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Version.pm
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Version.pm
@@ -20,6 +20,6 @@ use warnings;
use Exporter qw/import/;
our @EXPORT_OK = qw/$versionNumber $versionDate/;
-our $versionNumber = '3.23.6';
-our $versionDate = '2024-01-17';
+our $versionNumber = '3.23.7';
+our $versionDate = '2024-03-16';
1
diff --git a/Master/texmf-dist/scripts/latexindent/defaultSettings.yaml b/Master/texmf-dist/scripts/latexindent/defaultSettings.yaml
index a00474e1430..221bdb6b5d0 100755
--- a/Master/texmf-dist/scripts/latexindent/defaultSettings.yaml
+++ b/Master/texmf-dist/scripts/latexindent/defaultSettings.yaml
@@ -1,5 +1,5 @@
#
-# latexindent.pl, version 3.23.6, 2024-01-17
+# latexindent.pl, version 3.23.7, 2024-03-16
#
# defaultSettings.yaml, the default settings for latexindent.pl
#
diff --git a/Master/texmf-dist/scripts/latexindent/latexindent.pl b/Master/texmf-dist/scripts/latexindent/latexindent.pl
index e5c034dd77d..dbb083cc48b 100755
--- a/Master/texmf-dist/scripts/latexindent/latexindent.pl
+++ b/Master/texmf-dist/scripts/latexindent/latexindent.pl
@@ -1,6 +1,6 @@
#!/usr/bin/env perl
#
-# latexindent.pl, version 3.23.6, 2024-01-17
+# latexindent.pl, version 3.23.7, 2024-03-16
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -27,6 +27,16 @@ use Getopt::Long; # to get the switches/options/flags
use lib $FindBin::RealBin;
use LatexIndent::Document;
+use utf8;
+use Encode qw/ encode decode find_encoding /;
+
+use LatexIndent::LogFile qw/$logger/;
+use LatexIndent::UTF8CmdLineArgsFileOperation
+ qw/commandlineargs_with_encode @new_args/;
+
+commandlineargs_with_encode();
+my $commandlineargs = join( ", ", @ARGV );
+
# get the options
my %switches = ( readLocalSettings => 0 );
@@ -56,6 +66,34 @@ GetOptions(
"GCString" => \$switches{GCString},
);
+$logger = LatexIndent::Logger->new();
+our $consoleoutcp;
+if ( $FindBin::Script eq 'latexindent.exe' ) {
+ require Win32;
+ import Win32;
+
+ my $encoding_sys = Win32::GetACP()
+ ; #https://stackoverflow.com/a/63868721HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
+ $consoleoutcp = Win32::GetConsoleOutputCP();
+ Win32::SetConsoleOutputCP(65001);
+
+ $logger->info("*ANSI Code Page: $encoding_sys");
+ if ( $switches{screenlog} ) {
+ print "INFO: ANSI Code Page: $encoding_sys\n"
+ ; #The values of ACP in the registry
+ print "INFO: Current console output code page: $consoleoutcp \n";
+ print "INFO: Change the current console output code page to 65001\n";
+ }
+}
+
+if ( $switches{screenlog} ) {
+ $logger->info("*Command line:");
+ $logger->info("@new_args");
+ $logger->info( "Command line arguments:\n" . $commandlineargs );
+ print "INFO: Command line:\n @new_args\n";
+ print " Command line arguments:\n " . $commandlineargs . "\n\n";
+}
+
# conditionally load the GCString module
eval "use Unicode::GCString" if $switches{GCString};
@@ -86,4 +124,12 @@ my $document = bless(
"LatexIndent::Document"
);
$document->latexindent( \@ARGV );
+
+if ( $FindBin::Script eq 'latexindent.exe' ) {
+ Win32::SetConsoleOutputCP($consoleoutcp);
+ if ( $switches{screenlog} ) {
+ print
+ "\n\nINFO: Restore the console output code page: $consoleoutcp\n";
+ }
+}
exit(0);