summaryrefslogtreecommitdiff
path: root/support/latexindent/LatexIndent
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2022-03-26 03:00:32 +0000
committerNorbert Preining <norbert@preining.info>2022-03-26 03:00:32 +0000
commitc441617a2cd82c0b0907d0106dbf047987ffd97d (patch)
tree6e98859c2dabc00a6413a5f217b93a2f9b902dfa /support/latexindent/LatexIndent
parentd636f5360c4ea795ed5560ea0a7524da61c01491 (diff)
CTAN sync 202203260300
Diffstat (limited to 'support/latexindent/LatexIndent')
-rw-r--r--support/latexindent/LatexIndent/AlignmentAtAmpersand.pm47
-rw-r--r--support/latexindent/LatexIndent/BackUpFileProcedure.pm21
-rw-r--r--support/latexindent/LatexIndent/Document.pm80
-rw-r--r--support/latexindent/LatexIndent/FileExtension.pm82
-rw-r--r--support/latexindent/LatexIndent/Item.pm2
-rw-r--r--support/latexindent/LatexIndent/Lines.pm3
-rw-r--r--support/latexindent/LatexIndent/LogFile.pm67
-rw-r--r--support/latexindent/LatexIndent/Logger.pm1
-rw-r--r--support/latexindent/LatexIndent/ModifyLineBreaks.pm1
-rw-r--r--support/latexindent/LatexIndent/Sentence.pm13
-rw-r--r--support/latexindent/LatexIndent/TrailingComments.pm3
-rw-r--r--support/latexindent/LatexIndent/Version.pm4
-rw-r--r--support/latexindent/LatexIndent/Wrap.pm3
13 files changed, 256 insertions, 71 deletions
diff --git a/support/latexindent/LatexIndent/AlignmentAtAmpersand.pm b/support/latexindent/LatexIndent/AlignmentAtAmpersand.pm
index 8f67d62c58..6607bf6c9d 100644
--- a/support/latexindent/LatexIndent/AlignmentAtAmpersand.pm
+++ b/support/latexindent/LatexIndent/AlignmentAtAmpersand.pm
@@ -16,20 +16,18 @@ package LatexIndent::AlignmentAtAmpersand;
# For all communication, please visit: https://github.com/cmhughes/latexindent.pl
use strict;
use warnings;
-use utf8;
-use Unicode::GCString;
use Data::Dumper;
use Exporter qw/import/;
use List::Util qw/max min sum/;
use LatexIndent::TrailingComments qw/$trailingCommentRegExp/;
-use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active/;
+use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active %switches/;
use LatexIndent::GetYamlSettings qw/%mainSettings/;
use LatexIndent::Tokens qw/%tokens/;
use LatexIndent::LogFile qw/$logger/;
use LatexIndent::HiddenChildren qw/%familyTree/;
use LatexIndent::Verbatim qw/%verbatimStorage/;
our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321
-our @EXPORT_OK = qw/align_at_ampersand find_aligned_block double_back_slash_else main_formatting individual_padding multicolumn_padding multicolumn_pre_check multicolumn_post_check dont_measure hidden_child_cell_row_width hidden_child_row_width /;
+our @EXPORT_OK = qw/align_at_ampersand find_aligned_block double_back_slash_else main_formatting individual_padding multicolumn_padding multicolumn_pre_check multicolumn_post_check dont_measure hidden_child_cell_row_width hidden_child_row_width get_column_width/;
our $alignmentBlockCounter;
our @cellStorage; # two-dimensional storage array containing the cell information
our @formattedBody; # array for the new body
@@ -38,6 +36,7 @@ our @maxColumnWidth;
our @maxDelimiterWidth;
sub find_aligned_block{
+
my $self = shift;
return unless (${$self}{body} =~ m/(?!<\\)%\*\h*\\begin\{/s);
@@ -246,7 +245,7 @@ sub align_at_ampersand{
# for the *previous* cell
my $spanningOffSet = ($spanning > 0 ? $spanning - 1 : 0);
${$cellStorage[$rowCounter][$columnCounter - $spanningOffSet]}{delimiter} = $1;
- ${$cellStorage[$rowCounter][$columnCounter - $spanningOffSet]}{delimiterLength} = Unicode::GCString->new($1)->columns();
+ ${$cellStorage[$rowCounter][$columnCounter - $spanningOffSet]}{delimiterLength} = &get_column_width($1);
# keep track of maximum delimiter width
$maxDelimiterWidth[$columnCounter - $spanningOffSet] =
@@ -273,7 +272,7 @@ sub align_at_ampersand{
# basic cell storage
$cellStorage[$rowCounter][$columnCounter]
- = ({width=>Unicode::GCString->new($column)->columns(),
+ = ({width=>&get_column_width($column),
entry=>$column,
type=>($numberOfAmpersands>0 ? "X" : "*"),
groupPadding=>0,
@@ -443,7 +442,7 @@ sub align_at_ampersand{
if( (${$self}{begin} eq '{' | ${$self}{begin} eq '[') and ${$self}{parentBegin}){
$lengthOfBegin = ${$self}{parentBegin}."{";
}
- ${$self}{indentation} = " " x Unicode::GCString->new($lengthOfBegin)->columns();
+ ${$self}{indentation} = " " x (&get_column_width($lengthOfBegin));
$logger->trace("Adjusting indentation of ${$self}{name} in AlignAtAmpersand routine") if($is_t_switch_active);
}
}
@@ -540,7 +539,7 @@ sub main_formatting {
# objective (2): calculate row width and update maximumRowWidth
# objective (2): calculate row width and update maximumRowWidth
# objective (2): calculate row width and update maximumRowWidth
- my $rowWidth = Unicode::GCString->new($tmpRow)->columns();
+ my $rowWidth = &get_column_width($tmpRow);
# possibly update rowWidth if there are hidden children; see test-cases/alignment/hidden-child1.tex and friends
$rowWidth = $self->hidden_child_row_width($tmpRow,$rowCount,$rowWidth) if(${$self}{measureHiddenChildren} or ${$self}{measureVerbatim});
@@ -1434,12 +1433,12 @@ sub hidden_child_cell_row_width{
if($bodyLineBreaks>0){
my $maxRowWidthWithinCell = 0;
foreach(split("\n",$tmpCellEntry)){
- my $currentRowWidth = Unicode::GCString->new($_)->columns();
+ my $currentRowWidth = &get_column_width($_);
$maxRowWidthWithinCell = $currentRowWidth if ($currentRowWidth > $maxRowWidthWithinCell );
}
${$cellStorage[$rowCounter][$columnCounter]}{width} = $maxRowWidthWithinCell;
} else {
- ${$cellStorage[$rowCounter][$columnCounter]}{width} = Unicode::GCString->new($tmpCellEntry)->columns();
+ ${$cellStorage[$rowCounter][$columnCounter]}{width} = &get_column_width($tmpCellEntry);
}
}
@@ -1481,7 +1480,7 @@ sub hidden_child_row_width{
if( (${$self}{begin} eq '{' | ${$self}{begin} eq '[') and ${$self}{parentBegin}){
$beginToMeasure = ${$self}{parentBegin}."{";
}
- $lengthOfBegin = Unicode::GCString->new($beginToMeasure)->columns();
+ $lengthOfBegin = &get_column_width($beginToMeasure);
$tmpRow = $beginToMeasure.$tmpRow if $rowCount == 0;
}
@@ -1492,7 +1491,7 @@ sub hidden_child_row_width{
for my $hiddenChildToMeasure (@{${$self}{measureHiddenChildren}}){
if($tmpRow=~m/(^.*)?$hiddenChildToMeasure/m and defined $familyTree{$hiddenChildToMeasure}{bodyForMeasure}){
my $partBeforeId = $1;
- my $lengthPartBeforeId = Unicode::GCString->new($partBeforeId)->columns();
+ my $lengthPartBeforeId = &get_column_width($partBeforeId);
foreach (@{$familyTree{$hiddenChildToMeasure}{ancestors}}){
if (${$_}{ancestorID} eq ${$self}{id}){
@@ -1536,17 +1535,17 @@ sub hidden_child_row_width{
my $maxRowWidth = 0;
foreach(split("\n",$tmpRow)){
- my $currentRowWidth = Unicode::GCString->new($_)->columns();
+ my $currentRowWidth = &get_column_width($_);
$maxRowWidth = $currentRowWidth if ($currentRowWidth > $maxRowWidth );
}
$rowWidth = $maxRowWidth;
} else {
- $rowWidth = Unicode::GCString->new($tmpRow)->columns();
+ $rowWidth = &get_column_width($tmpRow);
}
} elsif (!${${$self}{linebreaksAtEnd}}{begin}
and ${$cellStorage[0][0]}{type} eq "X"
and ${$cellStorage[0][0]}{measureThis}){
- $rowWidth = Unicode::GCString->new($tmpRow)->columns();
+ $rowWidth = &get_column_width($tmpRow);
}
# possibly draw ruler to log file
@@ -1567,7 +1566,7 @@ sub draw_ruler_to_logfile{
$logger->trace("*tmpRow:");
foreach(split("\n",$tmpRow)){
- my $currentRowWidth = Unicode::GCString->new($_)->columns();
+ my $currentRowWidth = &get_column_width($_);
$logger->trace("$_ \t(length: $currentRowWidth)");
}
@@ -1579,4 +1578,20 @@ sub draw_ruler_to_logfile{
$logger->trace($ruler);
}
+
+sub get_column_width{
+
+ my $stringToBeMeasured = $_[0];
+
+ # default length measurement
+ # credit/reference: https://perldoc.perl.org/perlunicook#%E2%84%9E-33:-String-length-in-graphemes
+ unless ($switches{GCString}){
+ my $count = 0;
+ while ($stringToBeMeasured =~ /\X/g) { $count++ }
+ return $count;
+ }
+
+ # if GCString actice, then use Unicode::GCString
+ return Unicode::GCString->new($stringToBeMeasured)->columns();
+}
1;
diff --git a/support/latexindent/LatexIndent/BackUpFileProcedure.pm b/support/latexindent/LatexIndent/BackUpFileProcedure.pm
index e74b3fd75f..bcc2bb0fb3 100644
--- a/support/latexindent/LatexIndent/BackUpFileProcedure.pm
+++ b/support/latexindent/LatexIndent/BackUpFileProcedure.pm
@@ -22,14 +22,14 @@ use LatexIndent::LogFile qw/$logger/;
use File::Basename; # to get the filename and directory path
use File::Copy; # to copy the original file to backup (if overwrite option set)
use Exporter qw/import/;
-our @EXPORT_OK = qw/create_back_up_file/;
+our @EXPORT_OK = qw/create_back_up_file check_if_different/;
# copy main file to a back up in the case of the overwrite switch being active
sub create_back_up_file{
my $self = shift;
- return unless($switches{overwrite});
+ return unless(${$self}{overwrite});
# if we want to over write the current file create a backup first
$logger->info("*Backup procedure (-w flag active):");
@@ -147,4 +147,21 @@ sub create_back_up_file{
exit(5);
}
}
+
+sub check_if_different{
+ my $self = shift;
+
+ if (${$self}{originalBody} eq ${$self}{body}){
+ $logger->info("*-wd switch active");
+ $logger->info("Original body matches indented body, NOT overwriting, no back up files created");
+ return;
+ }
+
+ # otherwise, continue
+ $logger->info("*-wd switch active");
+ $logger->info("Original body is *different* from indented body");
+ $logger->info("activating overwrite switch, -w");
+ ${$self}{overwrite}=1;
+ $self->create_back_up_file;
+}
1;
diff --git a/support/latexindent/LatexIndent/Document.pm b/support/latexindent/LatexIndent/Document.pm
index 2ad655a56a..34eb16919b 100644
--- a/support/latexindent/LatexIndent/Document.pm
+++ b/support/latexindent/LatexIndent/Document.pm
@@ -17,7 +17,7 @@ package LatexIndent::Document;
use strict;
use warnings;
use Data::Dumper;
-use utf8;
+use File::Basename; # to get the filename and directory path
use open ':std', ':encoding(UTF-8)';
# gain access to subroutines in the following modules
@@ -29,7 +29,7 @@ use LatexIndent::Lines qw/lines_body_selected_lines lines_verbatim_create_line_b
use LatexIndent::Replacement qw/make_replacements/;
use LatexIndent::GetYamlSettings qw/yaml_read_settings yaml_modify_line_breaks_settings yaml_get_indentation_settings_for_this_object yaml_poly_switch_get_every_or_custom_value yaml_get_indentation_information yaml_get_object_attribute_for_indentation_settings yaml_alignment_at_ampersand_settings %mainSettings /;
use LatexIndent::FileExtension qw/file_extension_check/;
-use LatexIndent::BackUpFileProcedure qw/create_back_up_file/;
+use LatexIndent::BackUpFileProcedure qw/create_back_up_file check_if_different/;
use LatexIndent::BlankLines qw/protect_blank_lines unprotect_blank_lines condense_blank_lines/;
use LatexIndent::ModifyLineBreaks qw/modify_line_breaks_body modify_line_breaks_end modify_line_breaks_end_after remove_line_breaks_begin adjust_line_breaks_end_parent verbatim_modify_line_breaks/;
use LatexIndent::Sentence qw/one_sentence_per_line/;
@@ -77,14 +77,72 @@ sub new{
sub latexindent{
my $self = shift;
+ my @fileNames = @{$_[0]};
+
+ my $check_switch_status_across_files = 0;
+
+ my $file_extension_status_across_files = 0;
+
+ # one-time operations
$self->store_switches;
- $self->process_switches;
+ ${$self}{fileName} = $fileNames[0];
+ $self->process_switches(\@fileNames);
$self->yaml_read_settings;
- $self->file_extension_check;
- $self->operate_on_file;
+
+ ${$self}{multipleFiles} = 1 if ( (scalar (@fileNames)) >1 );
+
+ my $fileCount = 0;
+
+ # per-file operations
+ foreach (@fileNames) {
+ $fileCount++;
+ if ( (scalar (@fileNames)) >1 ){
+ $logger->info("*Filename: $_ (".$fileCount." of ".(scalar (@fileNames)).")") ;
+ }
+ ${$self}{fileName} = $_;
+ ${$self}{cruftDirectory} = $switches{cruftDirectory}||(dirname ${$self}{fileName});
+
+ # file existence/extension checks
+ my $file_existence = $self->file_extension_check;
+ if ($file_existence>0){
+ $file_extension_status_across_files=$file_existence;
+ next
+ }
+
+ # overwrite and overwriteIfDifferent switches, per file
+ ${$self}{overwrite} = $switches{overwrite};
+ ${$self}{overwriteIfDifferent} = $switches{overwriteIfDifferent};
+
+ # the main operations
+ $self->operate_on_file;
+
+ # keep track of check status across files
+ $check_switch_status_across_files = 1 if ($is_check_switch_active and ${$self}{originalBody} ne ${$self}{body});
+ }
+
+ # check switch summary across multiple files
+ if ( $is_check_switch_active and (scalar (@fileNames)) >1 ){
+ if($check_switch_status_across_files){
+ $logger->info("*check switch across multiple files: differences to report from at least one file");
+ } else {
+ $logger->info("*check switch across multiple files: no differences to report");
+ }
+ }
+
+ # logging of existence check
+ if ($file_extension_status_across_files>2){
+ $logger->warn("*at least one of the files you specified does not exist or could not be read");
+ }
+
+ # output the log file information
+ $self->output_logfile();
+
+ if ($file_extension_status_across_files>2){
+ exit($file_extension_status_across_files);
+ }
# check switch active, and file changed, gives different exit code
- if ($is_check_switch_active and ${$self}{originalBody} ne ${$self}{body}){
+ if ($check_switch_status_across_files){
exit(1);
}
}
@@ -124,6 +182,7 @@ sub operate_on_file{
$self->put_verbatim_back_in (match=>"just-commands");
$self->make_replacements(when=>"after") if ($is_r_switch_active and !$is_rv_switch_active);
${$self}{body} =~ s/\r\n/\n/sg if $mainSettings{dos2unixlinebreaks};
+ $self->check_if_different if ${$self}{overwriteIfDifferent};
}
$self->output_indented_text;
return
@@ -152,23 +211,20 @@ sub output_indented_text{
$logger->info("*Output routine:");
# if -overwrite is active then output to original fileName
- if($switches{overwrite}) {
+ if(${$self}{overwrite}){
$logger->info("Overwriting file ${$self}{fileName}");
open(OUTPUTFILE,">",${$self}{fileName});
print OUTPUTFILE ${$self}{body};
close(OUTPUTFILE);
} elsif($switches{outputToFile}) {
- $logger->info("Outputting to file $switches{outputToFile}");
- open(OUTPUTFILE,">",$switches{outputToFile});
+ $logger->info("Outputting to file ${$self}{outputToFile}");
+ open(OUTPUTFILE,">",${$self}{outputToFile});
print OUTPUTFILE ${$self}{body};
close(OUTPUTFILE);
} else {
$logger->info("Not outputting to file; see -w and -o switches for more options.");
}
- # output the log file information
- $self->output_logfile();
-
# output to screen, unless silent mode
print ${$self}{body} unless $switches{silentMode};
diff --git a/support/latexindent/LatexIndent/FileExtension.pm b/support/latexindent/LatexIndent/FileExtension.pm
index a9ff031493..6dc2b69641 100644
--- a/support/latexindent/LatexIndent/FileExtension.pm
+++ b/support/latexindent/LatexIndent/FileExtension.pm
@@ -16,7 +16,6 @@ package LatexIndent::FileExtension;
# For all communication, please visit: https://github.com/cmhughes/latexindent.pl
use strict;
use warnings;
-use utf8;
use PerlIO::encoding;
use open ':std', ':encoding(UTF-8)';
use File::Basename; # to get the filename and directory path
@@ -65,22 +64,34 @@ sub file_extension_check{
}
}
unless($fileFound){
- $logger->fatal("*I couldn't find a match for $fileName in fileExtensionPreference (see defaultSettings.yaml)");
- foreach (@fileExtensions ){
- $logger->fatal("I searched for $fileName$_");
+ if (defined ${$self}{multipleFiles}){
+ $logger->warn("*I couldn't find a match for $fileName in fileExtensionPreference (see defaultSettings.yaml)");
+ $logger->warn("moving on, no indendation done for ${$self}{fileName}.");
+ return 3;
+ } else {
+ $logger->fatal("*I couldn't find a match for $fileName in fileExtensionPreference (see defaultSettings.yaml)");
+ foreach (@fileExtensions ){
+ $logger->fatal("I searched for $fileName$_");
+ }
+ $logger->fatal("but couldn't find any of them.\nConsider updating fileExtensionPreference.");
+ $logger->fatal("*Exiting, no indendation done.");
+ $self->output_logfile();
+ exit(3);
}
- $logger->fatal("but couldn't find any of them.\nConsider updating fileExtensionPreference.");
- $logger->fatal("*Exiting, no indendation done.");
- $self->output_logfile();
- exit(3);
}
} else {
# if the file has a recognised extension, check that the file exists
unless( -e $fileName ){
- $logger->fatal("*I couldn't find $fileName, are you sure it exists?");
- $logger->fatal("Exiting, no indendation done.");
- $self->output_logfile();
- exit(3);
+ if (defined ${$self}{multipleFiles}){
+ $logger->warn("*I couldn't find $fileName, are you sure it exists?");
+ $logger->warn("moving on, no indendation done for ${$self}{fileName}.");
+ return 3;
+ } else {
+ $logger->fatal("*I couldn't find $fileName, are you sure it exists?");
+ $logger->fatal("Exiting, no indendation done.");
+ $self->output_logfile();
+ exit(3);
+ }
}
}
}
@@ -93,15 +104,17 @@ sub file_extension_check{
$logger->info("*-o switch active: output file check");
+ ${$self}{outputToFile} = $switches{outputToFile};
+
if ($fileName eq "-" and $switches{outputToFile} =~ m/^\+/){
$logger->info("STDIN input mode active, -o switch is removing all + symbols");
- $switches{outputToFile} =~ s/\+//g;
+ ${$self}{outputToFile} =~ s/\+//g;
}
# 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: $switches{outputToFile}");
- $switches{outputToFile} = ${$self}{baseName}.$1;
- $logger->info("output file is now: $switches{outputToFile}");
+ $logger->info("-o switch called with + symbol at the beginning: ${$self}{outputToFile}");
+ ${$self}{outputToFile} = ${$self}{baseName}.$1;
+ $logger->info("output file is now: ${$self}{outputToFile}");
}
my $strippedFileExtension = ${$self}{fileExtension};
@@ -109,20 +122,20 @@ sub file_extension_check{
$strippedFileExtension = "tex" if ($strippedFileExtension eq "");
# grab the name, directory, and extension of the output file
- my ($name, $dir, $ext) = fileparse($switches{outputToFile}, $strippedFileExtension);
+ my ($name, $dir, $ext) = fileparse(${$self}{outputToFile}, $strippedFileExtension);
# 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: $switches{outputToFile}");
- $switches{outputToFile} = $name.($name=~m/\.\z/ ? q() : ".").$strippedFileExtension;
- $logger->info("Updated to $switches{outputToFile} as the file extension of the input file is $strippedFileExtension");
+ ${$self}{outputToFile} = $name.($name=~m/\.\z/ ? q() : ".").$strippedFileExtension;
+ $logger->info("Updated to ${$self}{outputToFile} as the file extension of the input file is $strippedFileExtension");
}
# the -o file name might end with ++ in which case we wish to search for existence,
# and then increment accordingly
$name =~ s/\.$//;
if($name =~ m/\+\+$/){
- $logger->info("-o switch called with file name ending with ++: $switches{outputToFile}");
+ $logger->info("-o switch called with file name ending with ++: ${$self}{outputToFile}");
$name =~ s/\+\+$//;
$name = ${$self}{baseName} if ($name eq "");
my $outputFileCounter = 0;
@@ -134,7 +147,7 @@ sub file_extension_check{
$fileName = $name.$outputFileCounter.".".$strippedFileExtension;
}
$logger->info("$fileName does not exist, and will be the output file");
- $switches{outputToFile} = $fileName;
+ ${$self}{outputToFile} = $fileName;
}
}
@@ -144,10 +157,16 @@ sub file_extension_check{
my $openFilePossible=1;
open(MAINFILE, $fileName) or ($openFilePossible=0);
if($openFilePossible==0){
- $logger->fatal("*$fileName exists, but could not open it");
- $logger->fatal("Exiting, no indendation done.");
- $self->output_logfile();
- exit(4);
+ if (defined ${$self}{multipleFiles}){
+ $logger->warn("*$fileName exists, but could not open it");
+ $logger->warn("moving on, no indendation done for $fileName");
+ return 4;
+ } else {
+ $logger->fatal("*$fileName exists, but could not open it");
+ $logger->fatal("Exiting, no indendation done.");
+ $self->output_logfile();
+ exit(4);
+ }
}
push(@lines,$_) while(<MAINFILE>);
close(MAINFILE);
@@ -163,9 +182,18 @@ sub file_extension_check{
${$self}{body} = join("",@lines);
}
- # necessary extra storage if check switch is active
- if ($is_check_switch_active){
+ # necessary extra storage if
+ #
+ # check switch is active
+ #
+ # or
+ #
+ # $switches{overwriteIfDifferent}
+ #
+ if ($is_check_switch_active or $switches{overwriteIfDifferent}){
${$self}{originalBody} = ${$self}{body};
}
+
+ return 0;
}
1;
diff --git a/support/latexindent/LatexIndent/Item.pm b/support/latexindent/LatexIndent/Item.pm
index d9b191d62f..315e2c35e0 100644
--- a/support/latexindent/LatexIndent/Item.pm
+++ b/support/latexindent/LatexIndent/Item.pm
@@ -35,6 +35,8 @@ our $itemRegExp;
sub construct_list_of_items{
my $self = shift;
+ $listOfItems = q();
+
# put together a list of the items
while( my ($item,$lookForThisItem)= each %{$mainSettings{itemNames}}){
$listOfItems .= ($listOfItems eq "")?"$item":"|$item" if($lookForThisItem);
diff --git a/support/latexindent/LatexIndent/Lines.pm b/support/latexindent/LatexIndent/Lines.pm
index edfe811564..d1ee631b08 100644
--- a/support/latexindent/LatexIndent/Lines.pm
+++ b/support/latexindent/LatexIndent/Lines.pm
@@ -30,6 +30,9 @@ sub lines_body_selected_lines {
# strip all space from lines switch
$switches{lines} =~ s/\h//sg;
+ # convert multiple - into single
+ $switches{lines} =~ s/-+/-/sg;
+
$logger->info("*-n,--lines switch is active, operating on lines $switches{lines}");
$logger->info("number of lines in file: ".($#lines+1));
$logger->info("*interpretting $switches{lines}");
diff --git a/support/latexindent/LatexIndent/LogFile.pm b/support/latexindent/LatexIndent/LogFile.pm
index 692bfddbdb..fa6df78828 100644
--- a/support/latexindent/LatexIndent/LogFile.pm
+++ b/support/latexindent/LatexIndent/LogFile.pm
@@ -61,6 +61,9 @@ usage: latexindent.pl [options] [file]
latexindent.pl -o=outputfile.tex myfile.tex
-w, --overwrite
overwrite the current file; a backup will be made, but still be careful
+ -wd, --overwriteIfDifferent
+ overwrite the current file IF the indented text is different from original;
+ a backup will be made, but still be careful
-s, --silent
silent mode: no output will be given to the terminal
-t, --trace
@@ -103,6 +106,9 @@ usage: latexindent.pl [options] [file]
only operate on selected lines; sample usage:
latexindent.pl --lines 3-5 myfile.tex
latexindent.pl --lines 3-5,7-10 myfile.tex
+ --GCString
+ loads the Unicode::GCString module for the align-at-ampersand routine
+ Note: this requires the Unicode::GCString module to be installed on your system
ENDQUOTE
;
exit(0);
@@ -110,6 +116,7 @@ ENDQUOTE
# if we've made it this far, the processing of switches and logging begins
my $self = shift;
+ my @fileNames = @{$_[0]};
$logger = LatexIndent::Logger->new();
@@ -134,7 +141,16 @@ ENDQUOTE
$logger->info($time);
if (${$self}{fileName} ne "-"){
- $logger->info("Filename: ${$self}{fileName}");
+ # multiple filenames or not
+ if ( (scalar (@fileNames)) >1 ){
+ $logger->info("Filenames:");
+ foreach (@fileNames) {
+ $logger->info(" $_");
+ }
+ $logger->info("total number of files: ".(scalar (@fileNames)));
+ } else {
+ $logger->info("Filename: ${$self}{fileName}");
+ }
} else {
$logger->info("Reading input from STDIN");
if (-t STDIN) {
@@ -155,7 +171,8 @@ ENDQUOTE
$logger->info("-tt|--ttrace: TTrace mode active (you have used either -tt or --ttrace)") if($switches{ttrace});
$logger->info("-s|--silent: Silent mode active (you have used either -s or --silent)") if($switches{silentMode});
$logger->info("-d|--onlydefault: Only defaultSettings.yaml will be used (you have used either -d or --onlydefault)") if($switches{onlyDefault});
- $logger->info("-w|--overwrite: Overwrite mode active, will make a back up of ${$self}{fileName} first") if($switches{overwrite});
+ $logger->info("-w|--overwrite: Overwrite mode active, will make a back up before overwriting") if($switches{overwrite});
+ $logger->info("-wd|--overwriteIfDifferent: will overwrite ONLY if indented text is different") if($switches{overwriteIfDifferent});
$logger->info("-l|--localSettings: Read localSettings YAML file") if($switches{readLocalSettings});
$logger->info("-y|--yaml: YAML settings specified via command line") if($switches{yaml});
$logger->info("-o|--outputfile: output to file") if($switches{outputToFile});
@@ -167,18 +184,58 @@ ENDQUOTE
$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});
$logger->info("-n|--lines mode: will only operate on specific lines $switches{lines}") if($switches{lines});
+ $logger->info("--GCString switch active, loading Unicode::GCString module") if($switches{GCString});
# check if overwrite and outputfile are active similtaneously
if($switches{overwrite} and $switches{outputToFile}){
- $logger->info("Options check, -w and -o specified\nYou have called latexindent.pl with both -o and -w\noutput to file) will take priority, and -w (over write) will be ignored");
+ $logger->info("*Options check: -w and -o specified");
+ $logger->info("You have called latexindent.pl with both -o and -w");
+ $logger->info("The -o switch will take priority, and -w (overwrite) will be ignored");
+ $switches{overwrite}=0;
+ }
+
+ # check if overwrite and outputfile are active similtaneously
+ if($switches{overwrite} and $switches{overwriteIfDifferent}){
+ $logger->info("*Options check: -w and -wd specified");
+ $logger->info("You have called latexindent.pl with both -w and -wd.");
+ $logger->info("The -wd switch will take priority, and -w (overwrite) will be ignored");
$switches{overwrite}=0;
}
- $logger->info("*Directory for backup files and $logfileName: ${$self}{cruftDirectory}");
+ # check if overwriteIfDifferent and outputfile are active similtaneously
+ if($switches{overwriteIfDifferent} and $switches{outputToFile}){
+ $logger->info("*Options check: -wd and -o specified");
+ $logger->info("You have called latexindent.pl with both -o and -wd");
+ $logger->info("The -o switch will take priority, and -wd (overwriteIfDifferent) will be ignored");
+ $switches{overwriteIfDifferent}=0;
+ }
+
+ # multiple files with the -o switch needs care
+ #
+ # example
+ #
+ # latexindent.pl *.tex -o myfile.tex
+ #
+ # would result in only the final file being written to myfile.tex
+ #
+ # So, if -o switch does *not* match having a + symbol at the beginning, then
+ # we ignore it, and turn it off
+ #
+ if ( (scalar @fileNames>1) and $switches{outputToFile} and ($switches{outputToFile} !~ m/^h*\+/) ){
+ $logger->warn("*-o switch specified as single file, but multiple files given as input");
+ $logger->warn("ignoring your specification -o $switches{outputToFile}");
+ $logger->warn("perhaps you migh specify it using, for example, -o=++ or -o=+myoutput");
+ $switches{outputToFile} =0;
+ }
+
+ $logger->info("*Directory for backup files and $logfileName:");
+ $logger->info("${$self}{cruftDirectory}");
# output location of modules
if($FindBin::Script eq 'latexindent.pl' or ($FindBin::Script eq 'latexindent.exe' and $switches{trace} )) {
- my @listOfModules = ('FindBin', 'YAML::Tiny', 'File::Copy', 'File::Basename', 'Getopt::Long','File::HomeDir','Unicode::GCString');
+ my @listOfModules = ('FindBin', 'YAML::Tiny', 'File::Copy', 'File::Basename', 'Getopt::Long','File::HomeDir');
+ push(@listOfModules,'Unicode::GCString') if $switches{GCString};
+
$logger->info("*Perl modules are being loaded from the following directories:");
foreach my $moduleName (@listOfModules) {
(my $file = $moduleName) =~ s|::|/|g;
diff --git a/support/latexindent/LatexIndent/Logger.pm b/support/latexindent/LatexIndent/Logger.pm
index d3f9d0f48d..ebad76a2d5 100644
--- a/support/latexindent/LatexIndent/Logger.pm
+++ b/support/latexindent/LatexIndent/Logger.pm
@@ -26,6 +26,7 @@ our @logFileLines;
sub info{
my $self = shift;
my $logfileline = shift;
+ return unless (defined $logfileline);
if ($logfileline =~ m/^\*/s){
$logfileline =~ s/^\*/INFO: /s;
$logfileline =~ s/^/ /mg;
diff --git a/support/latexindent/LatexIndent/ModifyLineBreaks.pm b/support/latexindent/LatexIndent/ModifyLineBreaks.pm
index 63f6c2d03f..4d3356183b 100644
--- a/support/latexindent/LatexIndent/ModifyLineBreaks.pm
+++ b/support/latexindent/LatexIndent/ModifyLineBreaks.pm
@@ -272,6 +272,7 @@ sub verbatim_modify_line_breaks{
${$self}{body} =~ s/\h*${$child}{id}/$trailingCharacterToken\n${$child}{id}/s;
}
}
+
# after text wrap poly-switch check
if ($input{when} eq "afterTextWrap"){
$logger->trace("*post text wrap poly-switch check for EndFinishesWithLineBreak") if $is_t_switch_active ;
diff --git a/support/latexindent/LatexIndent/Sentence.pm b/support/latexindent/LatexIndent/Sentence.pm
index 9370f55886..f2120ee7f8 100644
--- a/support/latexindent/LatexIndent/Sentence.pm
+++ b/support/latexindent/LatexIndent/Sentence.pm
@@ -192,18 +192,19 @@ sub one_sentence_per_line{
my $trailing = ($5?$5:q()).($6?$6:q()).($7?$7:q());
my $lineBreaksAtEnd = ($6? 1 : ($7?1:0) );
my $trailingComments = q();
- # remove trailing comments from within the body of the sentence
if (${$mainSettings{modifyLineBreaks}{oneSentencePerLine}}{removeSentenceLineBreaks}){
+ # remove trailing comments from within the body of the sentence
while($middle =~ m|$trailingCommentRegExp|){
$middle =~ s|\h*($trailingCommentRegExp)||s;
$trailingComments .= $1;
}
+ # remove line breaks from within a sentence
+ $middle =~ s|
+ (?!\A) # not at the *beginning* of a match
+ (\h*)\R # possible horizontal space, then line break
+ |$1?$1:" ";|esgx
}
- # remove line breaks from within a sentence
- $middle =~ s|
- (?!\A) # not at the *beginning* of a match
- (\h*)\R # possible horizontal space, then line break
- |$1?$1:" ";|esgx if ${$mainSettings{modifyLineBreaks}{oneSentencePerLine}}{removeSentenceLineBreaks};
+ $middle =~ s|\h{2,}| |sg if ${$mainSettings{modifyLineBreaks}{oneSentencePerLine}}{multipleSpacesToSingle};
$middle =~ s|$tokens{blanklines}\h*\R?|$tokens{blanklines}\n|sg;
$logger->trace("follows: $beginning") if $is_tt_switch_active;
$logger->trace("sentence: $middle") if $is_tt_switch_active;
diff --git a/support/latexindent/LatexIndent/TrailingComments.pm b/support/latexindent/LatexIndent/TrailingComments.pm
index 82ff154c87..8bab5bc130 100644
--- a/support/latexindent/LatexIndent/TrailingComments.pm
+++ b/support/latexindent/LatexIndent/TrailingComments.pm
@@ -54,6 +54,9 @@ sub add_comment_symbol{
sub remove_trailing_comments{
my $self = shift;
+
+ $commentCounter = 0;
+
$logger->trace("*Storing trailing comments")if $is_t_switch_active;
my $notPreceededBy = qr/${${$mainSettings{fineTuning}}{trailingComments}}{notPreceededBy}/;
diff --git a/support/latexindent/LatexIndent/Version.pm b/support/latexindent/LatexIndent/Version.pm
index 7727a270e9..dd996edb7f 100644
--- a/support/latexindent/LatexIndent/Version.pm
+++ b/support/latexindent/LatexIndent/Version.pm
@@ -19,6 +19,6 @@ use warnings;
use Exporter qw/import/;
our @EXPORT_OK = qw/$versionNumber $versionDate/;
-our $versionNumber = '3.16';
-our $versionDate = '2022-03-13';
+our $versionNumber = '3.17';
+our $versionDate = '2022-03-25';
1
diff --git a/support/latexindent/LatexIndent/Wrap.pm b/support/latexindent/LatexIndent/Wrap.pm
index a2b8383d94..45a30fb2d0 100644
--- a/support/latexindent/LatexIndent/Wrap.pm
+++ b/support/latexindent/LatexIndent/Wrap.pm
@@ -18,6 +18,7 @@ use strict;
use warnings;
use Text::Wrap;
use LatexIndent::Tokens qw/%tokens/;
+use LatexIndent::AlignmentAtAmpersand qw/get_column_width/;
use LatexIndent::TrailingComments qw/$trailingCommentRegExp/;
use LatexIndent::GetYamlSettings qw/%mainSettings/;
use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active $is_m_switch_active/;
@@ -266,7 +267,7 @@ sub text_wrap{
if($textWrapBlockStorageValue =~m/$verbatimID/s){
# measure length
- my $verbatimLength = Unicode::GCString->new($verbatimThing)->columns();
+ my $verbatimLength = &get_column_width($verbatimThing);
# create temporary ID, and check that it is not contained in the body
my $verbatimTmpID = join '' => map $set[rand @set], 1 .. $verbatimLength;