From 757d85974d6b7911f766ae371404144ec3bdce34 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sat, 13 Jul 2019 21:40:12 +0000 Subject: latexindent (13jul19) git-svn-id: svn://tug.org/texlive/trunk@51635 c570f23f-e606-0410-a88d-b1316a301751 --- .../LatexIndent/AlignmentAtAmpersand.pm | 21 ++- .../scripts/latexindent/LatexIndent/Arguments.pm | 42 +++-- .../scripts/latexindent/LatexIndent/Command.pm | 11 +- .../scripts/latexindent/LatexIndent/Document.pm | 64 ++++--- .../scripts/latexindent/LatexIndent/Else.pm | 45 +++-- .../scripts/latexindent/LatexIndent/Environment.pm | 56 +++--- .../latexindent/LatexIndent/FileExtension.pm | 15 +- .../latexindent/LatexIndent/GetYamlSettings.pm | 19 +- .../scripts/latexindent/LatexIndent/IfElseFi.pm | 4 +- .../scripts/latexindent/LatexIndent/Indent.pm | 83 +++++---- .../LatexIndent/KeyEqualsValuesBraces.pm | 15 +- .../scripts/latexindent/LatexIndent/LogFile.pm | 11 ++ .../latexindent/LatexIndent/MandatoryArgument.pm | 4 +- .../latexindent/LatexIndent/ModifyLineBreaks.pm | 204 ++++++++++++--------- .../LatexIndent/NamedGroupingBracesBrackets.pm | 19 +- .../latexindent/LatexIndent/OptionalArgument.pm | 4 +- .../scripts/latexindent/LatexIndent/Replacement.pm | 89 +++++++++ .../scripts/latexindent/LatexIndent/Sentence.pm | 2 +- .../scripts/latexindent/LatexIndent/Switches.pm | 9 +- .../LatexIndent/UnNamedGroupingBracesBrackets.pm | 18 +- .../scripts/latexindent/LatexIndent/Version.pm | 4 +- .../scripts/latexindent/defaultSettings.yaml | 198 ++++++++++++++------ .../texmf-dist/scripts/latexindent/latexindent.pl | 5 +- 23 files changed, 631 insertions(+), 311 deletions(-) create mode 100644 Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm (limited to 'Master/texmf-dist/scripts/latexindent') diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/AlignmentAtAmpersand.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/AlignmentAtAmpersand.pm index 7a4a9727c89..8b8e387d095 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/AlignmentAtAmpersand.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/AlignmentAtAmpersand.pm @@ -27,7 +27,7 @@ use LatexIndent::GetYamlSettings qw/%masterSettings/; use LatexIndent::Tokens qw/%tokens/; use LatexIndent::LogFile qw/$logger/; our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 -our @EXPORT_OK = qw/align_at_ampersand find_aligned_block/; +our @EXPORT_OK = qw/align_at_ampersand find_aligned_block double_back_slash_else/; our $alignmentBlockCounter; sub find_aligned_block{ @@ -258,7 +258,7 @@ sub align_at_ampersand{ } # output some of the info so far to the log file - $logger->trace("*Alignment at ampersand routine") if $is_t_switch_active; + $logger->trace("*Alignment at ampersand routine for ${$self}{name} (see lookForAlignDelims)") if $is_t_switch_active; $logger->trace("Maximum column sizes of horizontally stripped formatted block (${$self}{name}): @maximumColumnWidth") if $is_t_switch_active; $logger->trace("align at ampersand: ${$self}{lookForAlignDelims}") if $is_t_switch_active; $logger->trace("align at \\\\: ${$self}{alignDoubleBackSlash}") if $is_t_switch_active; @@ -505,4 +505,21 @@ sub align_at_ampersand{ ${$self}{body} =~ s/\h*\R$//s if !${$self}{linebreaksAtEnd}{body}; } +sub double_back_slash_else{ + my $self = shift; + + # check for existence of \\ statement, and associated line break information + $self->check_for_else_statement( + # else name regexp + elseNameRegExp=>qr/${${$masterSettings{fineTuning}}{modifyLineBreaks}}{doubleBackSlash}/, + # else statements name: note that DBS stands for 'Double Back Slash' + ElseStartsOnOwnLine=>"DBSStartsOnOwnLine", + # end statements + ElseFinishesWithLineBreak=>"DBSFinishesWithLineBreak", + # for the YAML settings storage + storageNameAppend=>"DBS", + # logfile information + logName=>"double-back-slash-block (for align at ampersand, see lookForAlignDelims)", + ); +} 1; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Arguments.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Arguments.pm index 7c84a0e65f3..96450fdaaf2 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Arguments.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Arguments.pm @@ -24,7 +24,7 @@ use LatexIndent::LogFile qw/$logger/; use Data::Dumper; use Exporter qw/import/; our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 -our @EXPORT_OK = qw/get_arguments_regexp find_opt_mand_arguments get_numbered_arg_regexp construct_arguments_regexp $optAndMandRegExp/; +our @EXPORT_OK = qw/get_arguments_regexp find_opt_mand_arguments construct_arguments_regexp $optAndMandRegExp comma_else/; our $ArgumentCounter; our $optAndMandRegExp; our $optAndMandRegExpWithLineBreaks; @@ -222,13 +222,6 @@ sub create_unique_id{ return; } -sub get_numbered_arg_regexp{ - - # for example #1 #2, etc - my $numberedArgRegExp = qr/(?:#\d\h*;?,?\/?)+/; - return $numberedArgRegExp; -} - sub get_arguments_regexp{ my $self = shift; @@ -240,11 +233,9 @@ sub get_arguments_regexp{ # some calls to this routine need to account for the linebreaks at the end, some do not my $lineBreaksAtEnd = (defined ${input}{mode} and ${input}{mode} eq 'lineBreaksAtEnd')?'\R*':q(); - # for example #1 #2, etc - my $numberedArgRegExp = $self->get_numbered_arg_regexp; - - # beamer special - my $beamerRegExp = qr/\<.*?\>/; + # arguments Before, by default, includes beamer special and numbered arguments, for example #1 #2, etc + my $argumentsBefore = qr/${${$masterSettings{fineTuning}}{arguments}}{before}/; + my $argumentsBetween = qr/${${$masterSettings{fineTuning}}{arguments}}{between}/; # commands are allowed strings between arguments, e.g node, decoration, etc, specified in stringsAllowedBetweenArguments my $stringsBetweenArguments = q(); @@ -272,7 +263,7 @@ sub get_arguments_regexp{ return qr/ ( # capture into $1 (?: - (?:\h|\R|$blankLineToken|$trailingCommentRegExp|$numberedArgRegExp|$beamerRegExp|_|\^|$stringsBetweenArguments)* + (?:\h|\R|$blankLineToken|$trailingCommentRegExp|$argumentsBefore|$argumentsBetween|$stringsBetweenArguments)* (?: (?: \h* # 0 or more spaces @@ -332,7 +323,7 @@ sub get_arguments_regexp{ # NOT followed by (?! (?: - (?:\h|\R|$blankLineToken|$trailingCommentRegExp|$numberedArgRegExp|$beamerRegExp|$stringsBetweenArguments)* # 0 or more h-space, blanklines, trailing comments + (?:\h|\R|$blankLineToken|$trailingCommentRegExp|$argumentsBefore|$stringsBetweenArguments)* # 0 or more h-space, blanklines, trailing comments (?: (?:(?check_for_else_statement( + # else name regexp + elseNameRegExp=>qr/${${$masterSettings{fineTuning}}{modifyLineBreaks}}{comma}/, + # else statements name + ElseStartsOnOwnLine=>"CommaStartsOnOwnLine", + # end statements + ElseFinishesWithLineBreak=>"CommaFinishesWithLineBreak", + # for the YAML settings storage + storageNameAppend=>"comma", + # logfile information + logName=>"comma block, see CommaStartsOnOwnLine and CommaFinishesWithLineBreak", + ); +} 1; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Command.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Command.pm index 2ba44bd5a62..a877a6295c1 100755 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Command.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Command.pm @@ -64,11 +64,14 @@ sub construct_command_regexp{ # details to log file $logger->trace("*The special command names regexp is: $commandNameSpecialRegExp (see commandNameSpecial)") if $is_t_switch_active; + # read from fine tuning + my $commandNameRegExp = qr/${${$masterSettings{fineTuning}}{commands}}{name}/; + # construct the command regexp $commandRegExp = qr/ (\\|\\@|@) ( - [+a-zA-Z@\*0-9_\:]+?|$commandNameSpecialRegExp # lowercase|uppercase letters, @, *, numbers + $commandNameRegExp|$commandNameSpecialRegExp # lowercase|uppercase letters, @, *, numbers ) (\h*) (\R*)? @@ -203,4 +206,10 @@ sub align_at_ampersand{ return; } +sub double_back_slash_else{ + # need an empty routine here for commands; see + # test-cases/alignment/command-align.tex for example + return; +} + 1; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm index b4441b99455..7f6694d0493 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Document.pm @@ -21,8 +21,9 @@ use utf8; use open ':std', ':encoding(UTF-8)'; # gain access to subroutines in the following modules -use LatexIndent::Switches qw/storeSwitches %switches $is_m_switch_active $is_t_switch_active $is_tt_switch_active/; +use LatexIndent::Switches qw/storeSwitches %switches $is_m_switch_active $is_t_switch_active $is_tt_switch_active $is_r_switch_active $is_rr_switch_active $is_rv_switch_active/; use LatexIndent::LogFile qw/processSwitches $logger/; +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 yaml_get_textwrap_removeparagraphline_breaks %masterSettings yaml_get_columns/; use LatexIndent::FileExtension qw/file_extension_check/; use LatexIndent::BackUpFileProcedure qw/create_back_up_file/; @@ -34,15 +35,15 @@ use LatexIndent::HorizontalWhiteSpace qw/remove_trailing_whitespace remove_leadi use LatexIndent::Indent qw/indent wrap_up_statement determine_total_indentation indent_begin indent_body indent_end_statement final_indentation_check get_surrounding_indentation indent_children_recursively check_for_blank_lines_at_beginning put_blank_lines_back_in_at_beginning add_surrounding_indentation_to_begin_statement post_indentation_check/; use LatexIndent::Tokens qw/token_check %tokens/; use LatexIndent::HiddenChildren qw/find_surrounding_indentation_for_children update_family_tree get_family_tree check_for_hidden_children/; -use LatexIndent::AlignmentAtAmpersand qw/align_at_ampersand find_aligned_block/; +use LatexIndent::AlignmentAtAmpersand qw/align_at_ampersand find_aligned_block double_back_slash_else/; use LatexIndent::DoubleBackSlash qw/dodge_double_backslash un_dodge_double_backslash/; # code blocks use LatexIndent::Verbatim qw/put_verbatim_back_in find_verbatim_environments find_noindent_block find_verbatim_commands find_verbatim_special verbatim_common_tasks/; -use LatexIndent::Environment qw/find_environments $environmentBasicRegExp/; +use LatexIndent::Environment qw/find_environments $environmentBasicRegExp construct_environments_regexp/; use LatexIndent::IfElseFi qw/find_ifelsefi construct_ifelsefi_regexp $ifElseFiBasicRegExp/; use LatexIndent::Else qw/check_for_else_statement/; -use LatexIndent::Arguments qw/get_arguments_regexp find_opt_mand_arguments get_numbered_arg_regexp construct_arguments_regexp/; +use LatexIndent::Arguments qw/get_arguments_regexp find_opt_mand_arguments construct_arguments_regexp comma_else/; use LatexIndent::OptionalArgument qw/find_optional_arguments/; use LatexIndent::MandatoryArgument qw/find_mandatory_arguments get_mand_arg_reg_exp/; use LatexIndent::RoundBrackets qw/find_round_brackets/; @@ -83,28 +84,34 @@ sub operate_on_file{ $self->create_back_up_file; $self->token_check; - $self->construct_regular_expressions; - $self->find_noindent_block; - $self->find_verbatim_commands; - $self->find_aligned_block; - $self->remove_trailing_comments; - $self->find_verbatim_environments; - $self->find_verbatim_special; - $self->verbatim_modify_line_breaks if $is_m_switch_active; - $self->text_wrap if ($is_m_switch_active and !${$masterSettings{modifyLineBreaks}{textWrapOptions}}{perCodeBlockBasis} and ${$masterSettings{modifyLineBreaks}{textWrapOptions}}{columns}>1); - $self->protect_blank_lines; - $self->remove_trailing_whitespace(when=>"before"); - $self->find_file_contents_environments_and_preamble; - $self->dodge_double_backslash; - $self->remove_leading_space; - $self->process_body_of_text; - $self->remove_trailing_whitespace(when=>"after"); - $self->condense_blank_lines; - $self->unprotect_blank_lines; - $self->un_dodge_double_backslash; - $self->put_verbatim_back_in (match=>"everything-except-commands"); - $self->put_trailing_comments_back_in; - $self->put_verbatim_back_in (match=>"just-commands"); + $self->make_replacements(when=>"before") if ($is_r_switch_active and !$is_rv_switch_active); + unless ($is_rr_switch_active){ + $self->construct_regular_expressions; + $self->find_noindent_block; + $self->find_verbatim_commands; + $self->find_aligned_block; + $self->remove_trailing_comments; + $self->find_verbatim_environments; + $self->find_verbatim_special; + $self->verbatim_modify_line_breaks if $is_m_switch_active; + $self->make_replacements(when=>"before") if $is_rv_switch_active; + $self->text_wrap if ($is_m_switch_active and !${$masterSettings{modifyLineBreaks}{textWrapOptions}}{perCodeBlockBasis} and ${$masterSettings{modifyLineBreaks}{textWrapOptions}}{columns}>1); + $self->protect_blank_lines; + $self->remove_trailing_whitespace(when=>"before"); + $self->find_file_contents_environments_and_preamble; + $self->dodge_double_backslash; + $self->remove_leading_space; + $self->process_body_of_text; + $self->remove_trailing_whitespace(when=>"after"); + $self->condense_blank_lines; + $self->unprotect_blank_lines; + $self->un_dodge_double_backslash; + $self->make_replacements(when=>"after") if $is_rv_switch_active; + $self->put_verbatim_back_in (match=>"everything-except-commands"); + $self->put_trailing_comments_back_in; + $self->put_verbatim_back_in (match=>"just-commands"); + $self->make_replacements(when=>"after") if ($is_r_switch_active and !$is_rv_switch_active); + } $self->output_indented_text; return } @@ -112,6 +119,7 @@ sub operate_on_file{ sub construct_regular_expressions{ my $self = shift; $self->construct_trailing_comment_regexp; + $self->construct_environments_regexp; $self->construct_ifelsefi_regexp; $self->construct_list_of_items; $self->construct_special_begin; @@ -335,7 +343,7 @@ sub tasks_common_to_each_object{ $self->adjust_replacement_text_line_breaks_at_end; # modify line breaks on body and end statements - $self->modify_line_breaks_body if $is_m_switch_active; + $self->modify_line_breaks_body if ($is_m_switch_active and defined ${$self}{BodyStartsOnOwnLine}); # modify line breaks end statements $self->modify_line_breaks_end if $is_m_switch_active; @@ -343,6 +351,8 @@ sub tasks_common_to_each_object{ # check the body for current children $self->check_for_hidden_children if ${$self}{body} =~ m/$tokens{beginOfToken}/; + # double back slash poly-switch check + $self->double_back_slash_else if ($is_m_switch_active and ${$self}{lookForAlignDelims}); return; } diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Else.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Else.pm index 10b1cc4d41b..5a367c784fc 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Else.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Else.pm @@ -55,27 +55,32 @@ sub check_for_else_statement{ while(${$self}{body} =~ m/$elseRegExp(\h*)($trailingCommentRegExp)?/){ ${$self}{body} =~ s/$elseRegExp(\h*)($trailingCommentRegExp)? / - # create a new IfElseFi object + # create a new Else object my $else = LatexIndent::Else->new(begin=>$1, - name=>${$self}{name}, - storageNameAppend=>$input{storageNameAppend}, - body=>$3, - end=>q(), - linebreaksAtEnd=>{ - begin=>$2?1:0, - body=>0, - end=>0, - }, - aliases=>{ - # begin statements - BeginStartsOnOwnLine=>$input{ElseStartsOnOwnLine}, - # end statements - BodyStartsOnOwnLine=>$input{ElseFinishesWithLineBreak}, - }, - modifyLineBreaksYamlName=>${$self}{modifyLineBreaksYamlName}, - endImmediatelyFollowedByComment=>0, - horizontalTrailingSpace=>q(), - ); + name=>${$self}{name}, + storageNameAppend=>$input{storageNameAppend}, + body=>$3, + end=>q(), + linebreaksAtEnd=>{ + begin=>$2?1:0, + body=>0, + end=>0, + }, + aliases=>{ + # begin statements + BeginStartsOnOwnLine=>$input{ElseStartsOnOwnLine}, + # end statements + BodyStartsOnOwnLine=>$input{ElseFinishesWithLineBreak}, + }, + modifyLineBreaksYamlName=>${$self}{modifyLineBreaksYamlName}, + endImmediatelyFollowedByComment=>0, + horizontalTrailingSpace=>q(), + # mandatory and optional arguments have a parent, which we need + # to detail for double back slash poly-switches + # (see test-cases alignment command-align.tex, for example) + parent=>(${$self}{parent}?${$self}{parent}:"none"), + ); + # log file output $logger->trace("*$input{logName} found: ${$self}{name}")if $is_t_switch_active; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Environment.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Environment.pm index 2c74f47554e..401cbecbee2 100755 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Environment.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Environment.pm @@ -28,35 +28,41 @@ use LatexIndent::Special qw/$specialBeginAndBracesBracketsBasicRegExp/; use Data::Dumper; use Exporter qw/import/; our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 -our @EXPORT_OK = qw/find_environments $environmentBasicRegExp/; +our @EXPORT_OK = qw/find_environments $environmentBasicRegExp construct_environments_regexp/; our $environmentCounter; our $environmentBasicRegExp = qr/\\begin\{/; +our $environmentRegExp; # store the regular expresssion for matching and replacing the \begin{}...\end{} statements -our $environmentRegExp = qr/ - ( - \\begin\{ - ( - [a-zA-Z@\*0-9_\\]+ # lowercase|uppercase letters, @, *, numbers - ) # environment name captured into $2 - \} # \begin{} statement - \h* # horizontal space - (\R*)? # possible line breaks (into $3) - ) # begin statement captured into $1 - ( - (?: # cluster-only (), don't capture - (?! # don't include \begin in the body - (?:\\begin) # cluster-only (), don't capture - ). # any character, but not \\begin - )*? # non-greedy - (\R*)? # possible line breaks (into $5) - ) # environment body captured into $4 - ( - \\end\{\2\} # \end{} statement - ) # captured into $6 - (\h*)? # possibly followed by horizontal space - (\R)? # possibly followed by a line break - /sx; +sub construct_environments_regexp{ + + # read from fine tuning + my $environmentNameRegExp = qr/${${$masterSettings{fineTuning}}{environments}}{name}/; + $environmentRegExp = qr/ + ( + \\begin\{ + ( + $environmentNameRegExp # lowercase|uppercase letters, @, *, numbers + ) # environment name captured into $2 + \} # \begin{} statement + \h* # horizontal space + (\R*)? # possible line breaks (into $3) + ) # begin statement captured into $1 + ( + (?: # cluster-only (), don't capture + (?! # don't include \begin in the body + (?:\\begin) # cluster-only (), don't capture + ). # any character, but not \\begin + )*? # non-greedy + (\R*)? # possible line breaks (into $5) + ) # environment body captured into $4 + ( + \\end\{\2\} # \end{} statement + ) # captured into $6 + (\h*)? # possibly followed by horizontal space + (\R)? # possibly followed by a line break + /sx; +} sub find_environments{ my $self = shift; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm index 93f8016f030..716598de777 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/FileExtension.pm @@ -35,28 +35,23 @@ sub file_extension_check{ # grab the filename my $fileName = ${$self}{fileName}; + # see if an extension exists for the fileName + my ($name,$dir,$ext) = fileparse($fileName,qr/\..[^.]*$/); + # grab the file extension preferences my %fileExtensionPreference= %{$masterSettings{fileExtensionPreference}}; # sort the file extensions by preference my @fileExtensions = sort { $fileExtensionPreference{$a} <=> $fileExtensionPreference{$b} } keys(%fileExtensionPreference); - # get the base file name, allowing for different extensions (possibly no extension) - my ($name, $dir, $ext) = fileparse($fileName, @fileExtensions); + # store the base name ${$self}{baseName} = $name; - # check to make sure given file type is supported - if( -e $fileName and !$ext ){ - my $message = "The file $fileName exists , but the extension does not correspond to any given in fileExtensionPreference; consinder updating fileExtensionPreference."; - $logger->fatal("*$message"); - die($message); - } - # if no extension, search according to fileExtensionPreference if ($fileName ne "-"){ if (!$ext) { $logger->info("*File extension work:"); - $logger->info("latexindent called to act upon $fileName with a file extension;\nsearching for file with an extension in the following order (see fileExtensionPreference):"); + $logger->info("latexindent called to act upon $fileName without a file extension;\nsearching for file with an extension in the following order (see fileExtensionPreference):"); $logger->info(join("\n",@fileExtensions)); my $fileFound = 0; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm index 97cb36df823..3029043f23f 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm @@ -256,6 +256,23 @@ sub yaml_read_settings{ } } } + } elsif (ref($firstLevelValue) eq "ARRAY") { + # update amalgamate in master settings + if(ref(${$firstLevelValue}[0]) eq "HASH" and defined ${$firstLevelValue}[0]{amalgamate}){ + ${$masterSettings{$firstLevelKey}[0]}{amalgamate} = ${$firstLevelValue}[0]{amalgamate}; + shift @{$firstLevelValue} if ${$masterSettings{$firstLevelKey}[0]}{amalgamate}; + } + + # if amalgamate is set to 1, then append + if(${$masterSettings{$firstLevelKey}[0]}{amalgamate}){ + # loop through the other settings + foreach (@{$firstLevelValue}){ + push (@{$masterSettings{$firstLevelKey}},$_); + } + } else { + # otherwise overwrite + $masterSettings{$firstLevelKey} = $firstLevelValue; + } } else { $masterSettings{$firstLevelKey} = $firstLevelValue; } @@ -375,7 +392,7 @@ sub yaml_read_settings{ # loop through each of the settings specified in the -y switch foreach(@yamlSettings){ # split each value at semi-colon - my (@keysValues) = split(/(?=1 - # then we will need to add a line break (==1), a comment (==2) or another blank line (==3) - if(${$child}{BeginStartsOnOwnLine}>=1 and !$IDFirstNonWhiteSpaceCharacter){ - # by default, assume that no trailing comment token is needed - my $trailingCharacterToken = q(); - if(${$child}{BeginStartsOnOwnLine}==2){ - $logger->trace("Removing space immediately before ${$child}{id}, in preparation for adding % ($BeginStringLogFile == 2)") if $is_t_switch_active; - ${$self}{body} =~ s/\h*${$child}{idRegExp}/${$child}{id}/s; - $logger->trace("Adding a % at the end of the line that ${$child}{begin} is on, then a linebreak ($BeginStringLogFile == 2)") if $is_t_switch_active; - $trailingCharacterToken = "%".$self->add_comment_symbol; - } elsif (${$child}{BeginStartsOnOwnLine}==3){ - $logger->trace("Adding a blank line at the end of the line that ${$child}{begin} is on, then a linebreak ($BeginStringLogFile == 3)") if $is_t_switch_active; - $trailingCharacterToken = "\n".(${$masterSettings{modifyLineBreaks}}{preserveBlankLines}?$tokens{blanklines}:q()); - } else { - $logger->trace("Adding a linebreak at the beginning of ${$child}{begin} (see $BeginStringLogFile)") if $is_t_switch_active; + # + # Blank line poly-switch notes (==4) + # + # when BeginStartsOnOwnLine=4 we adopt the following approach: + # temporarily change BeginStartsOnOwnLine to -1, make adjustments + # temporarily change BeginStartsOnOwnLine to 3, make adjustments + # + # we use an array, @polySwitchValues to facilitate this + my @polySwitchValues = (${$child}{BeginStartsOnOwnLine}==4)?(-1,3):(${$child}{BeginStartsOnOwnLine}); + + foreach(@polySwitchValues){ + # if BeginStartsOnOwnLine is 4, then we hack + # $IDFirstNonWhiteSpaceCharacter + # to be 0 on the second time through (poly-switch set to 3) + $IDFirstNonWhiteSpaceCharacter = 0 if (${$child}{BeginStartsOnOwnLine}==4 and $_==3); + + # if the child ID is not the first character and BeginStartsOnOwnLine>=1 + # then we will need to add a line break (==1), a comment (==2) or another blank line (==3) + if($_>=1 and !$IDFirstNonWhiteSpaceCharacter){ + # by default, assume that no trailing comment token is needed + my $trailingCharacterToken = q(); + if($_==2){ + $logger->trace("Removing space immediately before ${$child}{id}, in preparation for adding % ($BeginStringLogFile == 2)") if $is_t_switch_active; + ${$self}{body} =~ s/\h*${$child}{idRegExp}/${$child}{id}/s; + $logger->trace("Adding a % at the end of the line that ${$child}{begin} is on, then a linebreak ($BeginStringLogFile == 2)") if $is_t_switch_active; + $trailingCharacterToken = "%".$self->add_comment_symbol; + } elsif ($_==3){ + $logger->trace("Adding a blank line at the end of the line that ${$child}{begin} is on, then a linebreak ($BeginStringLogFile == 3)") if $is_t_switch_active; + $trailingCharacterToken = "\n".(${$masterSettings{modifyLineBreaks}}{preserveBlankLines}?$tokens{blanklines}:q()); + } else { + $logger->trace("Adding a linebreak at the beginning of ${$child}{begin} (see $BeginStringLogFile)") if $is_t_switch_active; + } + + # the trailing comment/linebreak magic + ${$child}{begin} = "$trailingCharacterToken\n".${$child}{begin}; + $child->add_surrounding_indentation_to_begin_statement; + + # remove surrounding indentation ahead of % + ${$child}{begin} =~ s/^(\h*)%/%/ if($_==2); + } elsif ($_==-1 and $IDFirstNonWhiteSpaceCharacter){ + # finally, if BeginStartsOnOwnLine == -1 then we might need to *remove* a blank line(s) + # important to check we don't move the begin statement next to a blank-line-token + my $blankLineToken = $tokens{blanklines}; + if(${$self}{body} !~ m/$blankLineToken\R*\h*${$child}{idRegExp}/s){ + $logger->trace("Removing linebreak before ${$child}{begin} (see $BeginStringLogFile in ${$child}{modifyLineBreaksYamlName} YAML)") if $is_t_switch_active; + ${$self}{body} =~ s/(\h*)(?:\R*|\h*)+${$child}{idRegExp}/$1${$child}{id}/s; + } else { + $logger->trace("Not removing linebreak ahead of ${$child}{begin}, as blank-line-token present (see preserveBlankLines)") if $is_t_switch_active; + } } - - # the trailing comment/linebreak magic - ${$child}{begin} = "$trailingCharacterToken\n".${$child}{begin}; - $child->add_surrounding_indentation_to_begin_statement; - - # remove surrounding indentation ahead of % - ${$child}{begin} =~ s/^(\h*)%/%/ if(${$child}{BeginStartsOnOwnLine}==2); - } elsif (${$child}{BeginStartsOnOwnLine}==-1 and $IDFirstNonWhiteSpaceCharacter){ - # finally, if BeginStartsOnOwnLine == -1 then we might need to *remove* a blank line(s) - # important to check we don't move the begin statement next to a blank-line-token - my $blankLineToken = $tokens{blanklines}; - if(${$self}{body} !~ m/$blankLineToken\R*\h*${$child}{idRegExp}/s){ - $logger->trace("Removing linebreak before ${$child}{begin} (see $BeginStringLogFile in ${$child}{modifyLineBreaksYamlName} YAML)") if $is_t_switch_active; - ${$self}{body} =~ s/(\h*)(?:\R*|\h*)+${$child}{idRegExp}/$1${$child}{id}/s; - } else { - $logger->trace("Not removing linebreak ahead of ${$child}{begin}, as blank-line-token present (see preserveBlankLines)") if $is_t_switch_active; - } - } + } } $logger->trace(Dumper(\%{$child})) if($is_tt_switch_active); diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/KeyEqualsValuesBraces.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/KeyEqualsValuesBraces.pm index 0e939731015..590d9acb8c5 100755 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/KeyEqualsValuesBraces.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/KeyEqualsValuesBraces.pm @@ -17,6 +17,7 @@ package LatexIndent::KeyEqualsValuesBraces; use strict; use warnings; use LatexIndent::Tokens qw/%tokens/; +use LatexIndent::GetYamlSettings qw/%masterSettings/; use LatexIndent::TrailingComments qw/$trailingCommentRegExp/; use LatexIndent::Switches qw/$is_m_switch_active $is_t_switch_active $is_tt_switch_active/; use LatexIndent::LogFile qw/$logger/; @@ -37,21 +38,19 @@ sub construct_key_equals_values_regexp{ # blank line token my $blankLineToken = $tokens{blanklines}; + # read from fine tuning + my $keyEqualsValuesBracesBrackets = qr/${${$masterSettings{fineTuning}}{keyEqualsValuesBracesBrackets}}{name}/; + my $keyEqualsValuesBracesBracketsFollow = qr/${${$masterSettings{fineTuning}}{keyEqualsValuesBracesBrackets}}{follow}/; + # store the regular expresssion for matching and replacing $key_equals_values_bracesRegExp = qr/ ( - (?: - (?:(?info("-m|--modifylinebreaks: modify line breaks") if($switches{modifyLineBreaks}); $logger->info("-g|--logfile: logfile name") if($switches{logFileName}); $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}); # check if overwrite and outputfile are active similtaneously if($switches{overwrite} and $switches{outputToFile}){ diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/MandatoryArgument.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/MandatoryArgument.pm index 0b641231f2a..b5b3ac8de6f 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/MandatoryArgument.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/MandatoryArgument.pm @@ -18,7 +18,7 @@ use strict; use warnings; use LatexIndent::Tokens qw/%tokens/; 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 $is_m_switch_active /; use LatexIndent::LogFile qw/$logger/; use LatexIndent::IfElseFi qw/$ifElseFiBasicRegExp/; use LatexIndent::Special qw/$specialBeginBasicRegExp/; @@ -128,6 +128,8 @@ sub tasks_particular_to_each_object{ # search for special begin/end $self->find_special if ${$self}{body} =~ m/$specialBeginBasicRegExp/s; + # comma poly-switch check + $self->comma_else if $is_m_switch_active; } 1; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm index a0763456e02..0f33f88b834 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm @@ -28,27 +28,34 @@ use LatexIndent::LogFile qw/$logger/; our @EXPORT_OK = qw/modify_line_breaks_body modify_line_breaks_end adjust_line_breaks_end_parent remove_line_breaks_begin text_wrap remove_paragraph_line_breaks construct_paragraph_reg_exp text_wrap_remove_paragraph_line_breaks verbatim_modify_line_breaks/; our $paragraphRegExp = q(); - sub modify_line_breaks_body{ my $self = shift; + # + # Blank line poly-switch notes (==4) + # + # when BodyStartsOnOwnLine=4 we adopt the following approach: + # temporarily change BodyStartsOnOwnLine to -1, make adjustments + # temporarily change BodyStartsOnOwnLine to 3, make adjustments + # switch BodyStartsOnOwnLine back to 4 # add a line break after \begin{statement} if appropriate - if(defined ${$self}{BodyStartsOnOwnLine}){ + my @polySwitchValues = (${$self}{BodyStartsOnOwnLine}==4)?(-1,3):(${$self}{BodyStartsOnOwnLine}); + foreach(@polySwitchValues){ my $BodyStringLogFile = ${$self}{aliases}{BodyStartsOnOwnLine}||"BodyStartsOnOwnLine"; - if(${$self}{BodyStartsOnOwnLine}>=1 and !${$self}{linebreaksAtEnd}{begin}){ + if($_>=1 and !${$self}{linebreaksAtEnd}{begin}){ # if the statement doesn't finish with a line break, # then we have different actions based upon the value of BodyStartsOnOwnLine: # BodyStartsOnOwnLine == 1 just add a new line # BodyStartsOnOwnLine == 2 add a comment, and then new line # BodyStartsOnOwnLine == 3 add a blank line, and then new line - if(${$self}{BodyStartsOnOwnLine}==1){ + if($_==1){ # modify the begin statement - $logger->trace("Adding a linebreak at the end of begin, ${$self}{begin} (see $BodyStringLogFile)") if $is_t_switch_active; + $logger->trace("Adding a linebreak at the end of begin statement ${$self}{begin} (see $BodyStringLogFile)") if $is_t_switch_active; ${$self}{begin} .= "\n"; ${$self}{linebreaksAtEnd}{begin} = 1; $logger->trace("Removing leading space from body of ${$self}{name} (see $BodyStringLogFile)") if $is_t_switch_active; ${$self}{body} =~ s/^\h*//; - } elsif(${$self}{BodyStartsOnOwnLine}==2){ + } elsif($_==2){ # by default, assume that no trailing comment token is needed my $trailingCommentToken = q(); if(${$self}{body} !~ m/^\h*$trailingCommentRegExp/s){ @@ -63,7 +70,7 @@ sub modify_line_breaks_body{ } else { $logger->trace("Even though $BodyStringLogFile == 2, ${$self}{begin} already finishes with a %, so not adding another.") if $is_t_switch_active; } - } elsif (${$self}{BodyStartsOnOwnLine}==3){ + } elsif ($_==3){ my $trailingCharacterToken = q(); $logger->trace("Adding a blank line at the end of begin ${$self}{begin} followed by a linebreak ($BodyStringLogFile == 3)") if $is_t_switch_active; ${$self}{begin} =~ s/\h*$//; @@ -72,12 +79,12 @@ sub modify_line_breaks_body{ $logger->trace("Removing leading space from body of ${$self}{name} (see $BodyStringLogFile)") if $is_t_switch_active; ${$self}{body} =~ s/^\h*//; } - } elsif (${$self}{BodyStartsOnOwnLine}==-1 and ${$self}{linebreaksAtEnd}{begin}){ + } elsif ($_==-1 and ${$self}{linebreaksAtEnd}{begin}){ # remove line break *after* begin, if appropriate $self->remove_line_breaks_begin; } } - } +} sub remove_line_breaks_begin{ my $self = shift; @@ -90,86 +97,111 @@ sub remove_line_breaks_begin{ sub modify_line_breaks_end{ my $self = shift; - # possibly modify line break *before* \end{statement} - if(defined ${$self}{EndStartsOnOwnLine}){ - my $EndStringLogFile = ${$self}{aliases}{EndStartsOnOwnLine}||"EndStartsOnOwnLine"; - if(${$self}{EndStartsOnOwnLine}>=1 and !${$self}{linebreaksAtEnd}{body}){ - # if the statement doesn't finish with a line break, - # then we have different actions based upon the value of EndStartsOnOwnLine: - # EndStartsOnOwnLine == 1 just add a new line - # EndStartsOnOwnLine == 2 add a comment, and then new line - # EndStartsOnOwnLine == 3 add a blank line, and then new line - $logger->trace("Adding a linebreak at the end of body (see $EndStringLogFile)") if $is_t_switch_active; - - # by default, assume that no trailing character token is needed - my $trailingCharacterToken = q(); - if(${$self}{EndStartsOnOwnLine}==2){ - $logger->trace("Adding a % immediately after body of ${$self}{name} ($EndStringLogFile==2)") if $is_t_switch_active; - $trailingCharacterToken = "%".$self->add_comment_symbol; - ${$self}{body} =~ s/\h*$//s; - } elsif (${$self}{EndStartsOnOwnLine}==3) { - $logger->trace("Adding a blank line immediately after body of ${$self}{name} ($EndStringLogFile==3)") if $is_t_switch_active; - $trailingCharacterToken = "\n".(${$masterSettings{modifyLineBreaks}}{preserveBlankLines}?$tokens{blanklines}:q()); - ${$self}{body} =~ s/\h*$//s; - } - - # modified end statement - if(${$self}{body} =~ m/^\h*$/s and ${$self}{BodyStartsOnOwnLine} >=1 ){ - ${$self}{linebreaksAtEnd}{body} = 0; - } else { - ${$self}{body} .= "$trailingCharacterToken\n"; - ${$self}{linebreaksAtEnd}{body} = 1; - } - } elsif (${$self}{EndStartsOnOwnLine}==-1 and ${$self}{linebreaksAtEnd}{body}){ - # remove line break *after* body, if appropriate - - # check to see that body does *not* finish with blank-line-token, - # if so, then don't remove that final line break - if(${$self}{body} !~ m/$tokens{blanklines}$/s){ - $logger->trace("Removing linebreak at the end of body (see $EndStringLogFile)") if $is_t_switch_active; - ${$self}{body} =~ s/\R*$//sx; - ${$self}{linebreaksAtEnd}{body} = 0; - } else { - $logger->trace("Blank line token found at end of body (${$self}{name}), see preserveBlankLines, not removing line break before ${$self}{end}") if $is_t_switch_active; + # + # Blank line poly-switch notes (==4) + # + # when EndStartsOnOwnLine=4 we adopt the following approach: + # temporarily change EndStartsOnOwnLine to -1, make adjustments + # temporarily change EndStartsOnOwnLine to 3, make adjustments + # switch EndStartsOnOwnLine back to 4 + # + + my @polySwitchValues =(defined ${$self}{EndStartsOnOwnLine} and ${$self}{EndStartsOnOwnLine}==4) ?(-1,3):(${$self}{EndStartsOnOwnLine}); + foreach(@polySwitchValues){ + # possibly modify line break *before* \end{statement} + if(defined ${$self}{EndStartsOnOwnLine}){ + my $EndStringLogFile = ${$self}{aliases}{EndStartsOnOwnLine}||"EndStartsOnOwnLine"; + if($_>=1 and !${$self}{linebreaksAtEnd}{body}){ + # if the statement doesn't finish with a line break, + # then we have different actions based upon the value of EndStartsOnOwnLine: + # EndStartsOnOwnLine == 1 just add a new line + # EndStartsOnOwnLine == 2 add a comment, and then new line + # EndStartsOnOwnLine == 3 add a blank line, and then new line + $logger->trace("Adding a linebreak at the end of body (see $EndStringLogFile)") if $is_t_switch_active; + + # by default, assume that no trailing character token is needed + my $trailingCharacterToken = q(); + if($_==2){ + $logger->trace("Adding a % immediately after body of ${$self}{name} ($EndStringLogFile==2)") if $is_t_switch_active; + $trailingCharacterToken = "%".$self->add_comment_symbol; + ${$self}{body} =~ s/\h*$//s; + } elsif ($_==3) { + $logger->trace("Adding a blank line immediately after body of ${$self}{name} ($EndStringLogFile==3)") if $is_t_switch_active; + $trailingCharacterToken = "\n".(${$masterSettings{modifyLineBreaks}}{preserveBlankLines}?$tokens{blanklines}:q()); + ${$self}{body} =~ s/\h*$//s; + } + + # modified end statement + if(${$self}{body} =~ m/^\h*$/s and (defined ${$self}{BodyStartsOnOwnLine}) and ${$self}{BodyStartsOnOwnLine} >=1 ){ + ${$self}{linebreaksAtEnd}{body} = 0; + } else { + ${$self}{body} .= "$trailingCharacterToken\n"; + ${$self}{linebreaksAtEnd}{body} = 1; + } + } elsif ($_==-1 and ${$self}{linebreaksAtEnd}{body}){ + # remove line break *after* body, if appropriate + + # check to see that body does *not* finish with blank-line-token, + # if so, then don't remove that final line break + if(${$self}{body} !~ m/$tokens{blanklines}$/s){ + $logger->trace("Removing linebreak at the end of body (see $EndStringLogFile)") if $is_t_switch_active; + ${$self}{body} =~ s/\R*$//sx; + ${$self}{linebreaksAtEnd}{body} = 0; + } else { + $logger->trace("Blank line token found at end of body (${$self}{name}), see preserveBlankLines, not removing line break before ${$self}{end}") if $is_t_switch_active; + } } - } + } } - # possibly modify line break *after* \end{statement} - if(defined ${$self}{EndFinishesWithLineBreak} - and ${$self}{EndFinishesWithLineBreak}>=1 - and !${$self}{linebreaksAtEnd}{end}){ - # if the statement doesn't finish with a line break, - # then we have different actions based upon the value of EndFinishesWithLineBreak: - # EndFinishesWithLineBreak == 1 just add a new line - # EndFinishesWithLineBreak == 2 add a comment, and then new line - # EndFinishesWithLineBreak == 3 add a blank line, and then new line - my $EndStringLogFile = ${$self}{aliases}{EndFinishesWithLineBreak}||"EndFinishesWithLineBreak"; - if(${$self}{EndFinishesWithLineBreak}==1){ - $logger->trace("Adding a linebreak at the end of ${$self}{end} ($EndStringLogFile==1)") if $is_t_switch_active; - ${$self}{linebreaksAtEnd}{end} = 1; - - # modified end statement - ${$self}{replacementText} .= "\n"; - } elsif(${$self}{EndFinishesWithLineBreak}==2){ - if(${$self}{endImmediatelyFollowedByComment}){ - # no need to add a % if one already exists - $logger->trace("Even though $EndStringLogFile == 2, ${$self}{end} is immediately followed by a %, so not adding another; not adding line break.") if $is_t_switch_active; - } else { - # otherwise, create a trailing comment, and tack it on - $logger->trace("Adding a % immediately after, ${$self}{end} ($EndStringLogFile==2)") if $is_t_switch_active; - my $trailingCommentToken = "%".$self->add_comment_symbol; - ${$self}{end} =~ s/\h*$//s; - ${$self}{replacementText} .= "$trailingCommentToken\n"; - ${$self}{linebreaksAtEnd}{end} = 1; - } - } elsif(${$self}{EndFinishesWithLineBreak}==3){ - $logger->trace("Adding a blank line at the end of ${$self}{end} ($EndStringLogFile==3)") if $is_t_switch_active; - ${$self}{linebreaksAtEnd}{end} = 1; - - # modified end statement - ${$self}{replacementText} .= (${$masterSettings{modifyLineBreaks}}{preserveBlankLines}?$tokens{blanklines}:"\n")."\n"; - } + # + # Blank line poly-switch notes (==4) + # + # when EndFinishesWithLineBreak=4 we adopt the following approach: + # temporarily change EndFinishesWithLineBreak to -1, make adjustments + # temporarily change EndFinishesWithLineBreak to 3, make adjustments + # switch EndFinishesWithLineBreak back to 4 + # + @polySwitchValues =(defined ${$self}{EndFinishesWithLineBreak} and ${$self}{EndFinishesWithLineBreak}==4) ?(-1,3):(${$self}{EndFinishesWithLineBreak}); + foreach(@polySwitchValues){ + last if !(defined $_); + ${$self}{linebreaksAtEnd}{end} = 0 if($_==3 and (defined ${$self}{EndFinishesWithLineBreak} and ${$self}{EndFinishesWithLineBreak}==4)); + + # possibly modify line break *after* \end{statement} + if(defined $_ and $_>=1 + and !${$self}{linebreaksAtEnd}{end}){ + # if the statement doesn't finish with a line break, + # then we have different actions based upon the value of EndFinishesWithLineBreak: + # EndFinishesWithLineBreak == 1 just add a new line + # EndFinishesWithLineBreak == 2 add a comment, and then new line + # EndFinishesWithLineBreak == 3 add a blank line, and then new line + my $EndStringLogFile = ${$self}{aliases}{EndFinishesWithLineBreak}||"EndFinishesWithLineBreak"; + if($_==1){ + $logger->trace("Adding a linebreak at the end of ${$self}{end} ($EndStringLogFile==1)") if $is_t_switch_active; + ${$self}{linebreaksAtEnd}{end} = 1; + + # modified end statement + ${$self}{replacementText} .= "\n"; + } elsif($_==2){ + if(${$self}{endImmediatelyFollowedByComment}){ + # no need to add a % if one already exists + $logger->trace("Even though $EndStringLogFile == 2, ${$self}{end} is immediately followed by a %, so not adding another; not adding line break.") if $is_t_switch_active; + } else { + # otherwise, create a trailing comment, and tack it on + $logger->trace("Adding a % immediately after, ${$self}{end} ($EndStringLogFile==2)") if $is_t_switch_active; + my $trailingCommentToken = "%".$self->add_comment_symbol; + ${$self}{end} =~ s/\h*$//s; + ${$self}{replacementText} .= "$trailingCommentToken\n"; + ${$self}{linebreaksAtEnd}{end} = 1; + } + } elsif($_==3){ + $logger->trace("Adding a blank line at the end of ${$self}{end} ($EndStringLogFile==3)") if $is_t_switch_active; + ${$self}{linebreaksAtEnd}{end} = 1; + + # modified end statement + ${$self}{replacementText} .= (${$masterSettings{modifyLineBreaks}}{preserveBlankLines}?$tokens{blanklines}:"\n")."\n"; + } + } } } diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/NamedGroupingBracesBrackets.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/NamedGroupingBracesBrackets.pm index 4508943ddfa..f20e23ca547 100755 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/NamedGroupingBracesBrackets.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/NamedGroupingBracesBrackets.pm @@ -17,6 +17,7 @@ package LatexIndent::NamedGroupingBracesBrackets; use strict; use warnings; use LatexIndent::Tokens qw/%tokens/; +use LatexIndent::GetYamlSettings qw/%masterSettings/; use LatexIndent::TrailingComments qw/$trailingCommentRegExp/; use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active/; use LatexIndent::LogFile qw/$logger/; @@ -33,18 +34,22 @@ sub construct_grouping_braces_brackets_regexp{ # grab the arguments regexp my $optAndMandRegExp = $self->get_arguments_regexp; + # read from fine tuning + my $NamedGroupingBracesBracketsRegExp = qr/${${$masterSettings{fineTuning}}{NamedGroupingBracesBrackets}}{name}/; + my $NamedGroupingFollowRegExp = qr/${${$masterSettings{fineTuning}}{NamedGroupingBracesBrackets}}{follow}/; + # store the regular expresssion for matching and replacing $grouping_braces_regexp = qr/ ( - \h|\R|\{|\[|\$|\)|\( + $NamedGroupingFollowRegExp ) ( - [0-9\.a-zA-Z@\*><]+? # lowercase|uppercase letters, @, *, numbers, forward slash, dots - ) # $2 name - (\h*) # $3 h-space - (\R*) # $4 linebreaks - ($optAndMandRegExp) # $5 mand|opt arguments (at least one) - (\R)? # $8 linebreak + $NamedGroupingBracesBracketsRegExp # lowercase|uppercase letters, @, *, numbers, forward slash, dots + ) # $2 name + (\h*) # $3 h-space + (\R*) # $4 linebreaks + ($optAndMandRegExp) # $5 mand|opt arguments (at least one) + (\R)? # $8 linebreak /sx; # something {value} grouping braces with trailing comment diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/OptionalArgument.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/OptionalArgument.pm index 60e03ca91d4..fdaa549a7ae 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/OptionalArgument.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/OptionalArgument.pm @@ -18,7 +18,7 @@ use strict; use warnings; use LatexIndent::Tokens qw/%tokens/; 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 $is_m_switch_active /; use LatexIndent::LogFile qw/$logger/; use LatexIndent::IfElseFi qw/$ifElseFiBasicRegExp/; use LatexIndent::Special qw/$specialBeginBasicRegExp/; @@ -113,6 +113,8 @@ sub tasks_particular_to_each_object{ # search for special begin/end $self->find_special if ${$self}{body} =~ m/$specialBeginBasicRegExp/s; + # comma poly-switch check + $self->comma_else if $is_m_switch_active; } 1; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm new file mode 100644 index 00000000000..1bc51caed2d --- /dev/null +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm @@ -0,0 +1,89 @@ +package LatexIndent::Replacement; +# 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# See http://www.gnu.org/licenses/. +# +# Chris Hughes, 2017 +# +# For all communication, please visit: https://github.com/cmhughes/latexindent.pl +use strict; +use warnings; +use LatexIndent::Tokens qw/%tokens/; +use LatexIndent::TrailingComments qw/$trailingCommentRegExp/; +use LatexIndent::GetYamlSettings qw/%masterSettings/; +use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active $is_rr_switch_active/; +use LatexIndent::LogFile qw/$logger/; +use Data::Dumper; +use Exporter qw/import/; +our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 +our @EXPORT_OK = qw/make_replacements/; + +sub make_replacements{ + my $self = shift; + my %input = @_; + if ($is_t_switch_active and !$is_rr_switch_active){ + $logger->trace("*Replacement mode *$input{when}* indentation: -r") ; + } elsif ($is_t_switch_active and $is_rr_switch_active) { + $logger->trace("*Replacement mode, -rr switch is active") if $is_t_switch_active ; + } + + my @replacements = @{$masterSettings{replacements}}; + + foreach ( @replacements ){ + next if !(${$_}{this} or ${$_}{substitution}); + + # default value of "lookForThis" is 1 + ${$_}{lookForThis} = 1 if( !(defined ${$_}{lookForThis})); + + # move on if this one shouldn't be looked for + next if(!${$_}{lookForThis}); + + # default value of "when" is before + ${$_}{when} = "before" if( !(defined ${$_}{when}) or $is_rr_switch_active); + + # update to the logging file + if($is_t_switch_active and (${$_}{when} eq $input{when})){ + $logger->trace("-"); + $logger->trace("this: ${$_}{this}") if(${$_}{this}); + $logger->trace("that: ${$_}{that}") if(${$_}{that}); + $logger->trace("substitution: ${$_}{substitution}") if(${$_}{substitution}); + $logger->trace("when: ${$_}{when}"); + } + + # perform the substitutions + if(${$_}{when} eq $input{when}){ + $logger->warn("*You have specified both 'this' and 'substitution'; the 'substitution' field will be ignored") if(${$_}{this} and ${$_}{substitution}); + if(${$_}{this}){ + # *string* replacement + # *string* replacement + # *string* replacement + my $this = qq{${$_}{this}}; + my $that = (defined ${$_}{that}) ? qq{${$_}{that}} : q(); + my $index_match = index(${$self}{body}, $this); + while ( $index_match != -1 ) { + substr (${$self}{body}, $index_match, length($this), $that ); + $index_match = index(${$self}{body}, $this); + } + } else { + # *regex* replacement + # *regex* replacement + # *regex* replacement + + # https://stackoverflow.com/questions/12423337/how-to-pass-a-replacing-regex-as-a-command-line-argument-to-a-perl-script + my $body = ${$self}{body}; + eval("\$body =~ ${$_}{substitution}"); + ${$self}{body} = $body ; + } + } + } +} + +1; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Sentence.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Sentence.pm index 48ae0379d6c..cc11997e36f 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Sentence.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Sentence.pm @@ -129,7 +129,7 @@ sub one_sentence_per_line{ $sentencesEndWithEachPart = qr/\./; } elsif($sentencesEndWithEachPart eq "betterFullStop"){ $logger->trace("sentence ENDS with *better* full stop (see oneSentencePerLine:sentencesEndWith:betterFullStop") if $is_t_switch_active; - $sentencesEndWithEachPart = qr/(?:\.\)(?!\h*[a-z]))|(?:(?trace("sentence ENDS with exclamation mark (see oneSentencePerLine:sentencesEndWith:exclamationMark)") if $is_t_switch_active; $sentencesEndWithEachPart = qr/!/; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Switches.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Switches.pm index a5b07936a6d..d738a4b36b6 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Switches.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Switches.pm @@ -17,11 +17,14 @@ package LatexIndent::Switches; use strict; use warnings; use Exporter qw/import/; -our @EXPORT_OK = qw/%switches storeSwitches $is_m_switch_active $is_t_switch_active $is_tt_switch_active/; +our @EXPORT_OK = qw/%switches storeSwitches $is_m_switch_active $is_t_switch_active $is_tt_switch_active $is_r_switch_active $is_rr_switch_active $is_rv_switch_active/; our %switches; our $is_m_switch_active; our $is_t_switch_active; our $is_tt_switch_active; +our $is_r_switch_active; +our $is_rr_switch_active; +our $is_rv_switch_active; sub storeSwitches{ my $self = shift; @@ -32,6 +35,10 @@ sub storeSwitches{ $is_t_switch_active = defined $switches{trace}?$switches{trace}: 0; $is_tt_switch_active = defined $switches{ttrace}?$switches{ttrace}: 0; $is_t_switch_active = $is_tt_switch_active ? $is_tt_switch_active : $is_t_switch_active; + $is_r_switch_active = defined $switches{replacement}?$switches{replacement}: 0; + $is_rr_switch_active = defined $switches{onlyreplacement}?$switches{onlyreplacement}: 0; + $is_rv_switch_active = defined $switches{replacementRespectVerb}?$switches{replacementRespectVerb}: 0; + $is_r_switch_active = ($is_rr_switch_active|$is_rv_switch_active) ? ($is_rr_switch_active|$is_rv_switch_active) : $is_r_switch_active; delete ${$self}{switches}; } 1; diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/UnNamedGroupingBracesBrackets.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/UnNamedGroupingBracesBrackets.pm index 0c18484649a..eea6ee6747a 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/UnNamedGroupingBracesBrackets.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/UnNamedGroupingBracesBrackets.pm @@ -17,6 +17,7 @@ package LatexIndent::UnNamedGroupingBracesBrackets; use strict; use warnings; use LatexIndent::Tokens qw/%tokens/; +use LatexIndent::GetYamlSettings qw/%masterSettings/; use LatexIndent::TrailingComments qw/$trailingCommentRegExp/; use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active/; use LatexIndent::LogFile qw/$logger/; @@ -36,8 +37,9 @@ sub construct_unnamed_grouping_braces_brackets_regexp{ # blank line token my $blankLineToken = $tokens{blanklines}; - # for example #1 #2, etc - my $numberedArgRegExp = $self->get_numbered_arg_regexp; + # arguments Before, by default, includes beamer special and numbered arguments, for example #1 #2, etc + my $argumentsBefore = qr/${${$masterSettings{fineTuning}}{arguments}}{before}/; + my $UnNamedGroupingFollowRegExp = qr/${${$masterSettings{fineTuning}}{UnNamedGroupingBracesBrackets}}{follow}/; # store the regular expresssion for matching and replacing $un_named_grouping_braces_RegExp = qr/ @@ -48,18 +50,18 @@ sub construct_unnamed_grouping_braces_brackets_regexp{ | (?:(?<]+?' + follow: '\h|\R|\{|\[|\$|\)|\(' + UnNamedGroupingBracesBrackets: + follow: '\{|\[|,|&|\)|\(|\$' + arguments: + before: '(?:#\d\h*;?,?\/?)+|\<.*?\>' + between: '_|\^|\*' + modifyLineBreaks: + betterFullStop: '(?:\.\)(?!\h*[a-z]))|(?:(?\$switches{showhelp}, "cruft|c=s"=>\$switches{cruftDirectory}, "screenlog|sl"=>\$switches{screenlog}, + "replacement|r"=>\$switches{replacement}, + "onlyreplacement|rr"=>\$switches{onlyreplacement}, + "replacementrespectverb|rv"=>\$switches{replacementRespectVerb}, ); # check local settings doesn't interfer with reading the file; -- cgit v1.2.3