diff options
Diffstat (limited to 'support/latexindent/LatexIndent/IfElseFi.pm')
-rw-r--r-- | support/latexindent/LatexIndent/IfElseFi.pm | 122 |
1 files changed, 68 insertions, 54 deletions
diff --git a/support/latexindent/LatexIndent/IfElseFi.pm b/support/latexindent/LatexIndent/IfElseFi.pm index e090845828..22b3d2eb0f 100644 --- a/support/latexindent/LatexIndent/IfElseFi.pm +++ b/support/latexindent/LatexIndent/IfElseFi.pm @@ -1,4 +1,5 @@ package LatexIndent::IfElseFi; + # 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 @@ -23,7 +24,7 @@ use LatexIndent::Switches qw/$is_m_switch_active $is_t_switch_active $is_tt_swit use LatexIndent::LogFile qw/$logger/; use LatexIndent::Heading qw/$allHeadingsRegexp/; use Exporter qw/import/; -our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 +our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 our @EXPORT_OK = qw/find_ifelsefi construct_ifelsefi_regexp $ifElseFiBasicRegExp/; our $ifElseFiCounter; our $ifElseFiRegExp; @@ -32,7 +33,7 @@ our $ifElseFiBasicRegExp = qr/\\if/; # store the regular expresssion for matching and replacing the \if...\else...\fi statements # note: we search for \else separately in an attempt to keep this regexp a little more managable -sub construct_ifelsefi_regexp{ +sub construct_ifelsefi_regexp { my $ifElseFiNameRegExp = qr/${${$mainSettings{fineTuning}}{ifElseFi}}{name}/; $ifElseFiRegExp = qr/ ( @@ -58,12 +59,12 @@ sub construct_ifelsefi_regexp{ /sx; } -sub find_ifelsefi{ +sub find_ifelsefi { my $self = shift; - while( ${$self}{body} =~ m/$ifElseFiRegExp\h*($trailingCommentRegExp)?/){ + while ( ${$self}{body} =~ m/$ifElseFiRegExp\h*($trailingCommentRegExp)?/ ) { - ${$self}{body} =~ s/ + ${$self}{body} =~ s/ $ifElseFiRegExp(\h*)($trailingCommentRegExp)? / # create a new IfElseFi object @@ -97,58 +98,70 @@ sub find_ifelsefi{ ${@{${$self}{children}}[-1]}{replacementText}.($10?$10:q()).($11?$11:q()); /xse; - } + } return; } -sub post_indentation_check{ +sub post_indentation_check { + # needed to remove leading horizontal space before \else my $self = shift; # loop through \else and \or - foreach ({regExp=>qr/\\else/},{regExp=>qr/\\or/}){ + foreach ( { regExp => qr/\\else/ }, { regExp => qr/\\or/ } ) { my %else = %{$_}; - if(${$self}{body} =~ m/^\h*$else{regExp}/sm - and - !(${$self}{body} =~ m/^\h*$else{regExp}/s and ${$self}{linebreaksAtEnd}{begin}==0) - ){ - $logger->trace("*Adding surrounding indentation to $else{regExp} statement(s) ('${$self}{surroundingIndentation}')") if $is_t_switch_active; + if ( ${$self}{body} =~ m/^\h*$else{regExp}/sm + and !( ${$self}{body} =~ m/^\h*$else{regExp}/s and ${$self}{linebreaksAtEnd}{begin} == 0 ) ) + { + $logger->trace( + "*Adding surrounding indentation to $else{regExp} statement(s) ('${$self}{surroundingIndentation}')") + if $is_t_switch_active; ${$self}{body} =~ s/^\h*($else{regExp})/${$self}{surroundingIndentation}$1/smg; } } return; } -sub tasks_particular_to_each_object{ +sub tasks_particular_to_each_object { my $self = shift; # check for existence of \else statement, and associated line break information $self->check_for_else_statement( - # else name regexp - elseNameRegExp=>qr|\\else|, - # else statements name - ElseStartsOnOwnLine=>"ElseStartsOnOwnLine", - # end statements - ElseFinishesWithLineBreak=>"ElseFinishesWithLineBreak", - # for the YAML settings storage - storageNameAppend=>"else", - # logfile information - logName=>"else", - ); - + + # else name regexp + elseNameRegExp => qr|\\else|, + + # else statements name + ElseStartsOnOwnLine => "ElseStartsOnOwnLine", + + # end statements + ElseFinishesWithLineBreak => "ElseFinishesWithLineBreak", + + # for the YAML settings storage + storageNameAppend => "else", + + # logfile information + logName => "else", + ); + # check for existence of \or statement, and associated line break information $self->check_for_else_statement( - # else name regexp - elseNameRegExp=>qr|\\or|, - # else statements name - ElseStartsOnOwnLine=>"OrStartsOnOwnLine", - # end statements - ElseFinishesWithLineBreak=>"OrFinishesWithLineBreak", - # for the YAML settings storage - storageNameAppend=>"or", - # logfile information - logName=>"or", - ); + + # else name regexp + elseNameRegExp => qr|\\or|, + + # else statements name + ElseStartsOnOwnLine => "OrStartsOnOwnLine", + + # end statements + ElseFinishesWithLineBreak => "OrFinishesWithLineBreak", + + # for the YAML settings storage + storageNameAppend => "or", + + # logfile information + logName => "or", + ); # search for headings (important to do this before looking for commands!) $self->find_heading if ${$self}{body} =~ m/$allHeadingsRegexp/s; @@ -158,46 +171,48 @@ sub tasks_particular_to_each_object{ } -sub indent_begin{ +sub indent_begin { my $self = shift; - # line break checks after \if statement, can get messy if we + + # line break checks after \if statement, can get messy if we # have, for example # \ifnum # something # which might be changed into # \ifnumsomething # which is undeserible - if (defined ${$self}{BodyStartsOnOwnLine} - and ${$self}{BodyStartsOnOwnLine}==-1 - and ${$self}{body} !~ m/^(\h|\\|(?:!-!))/s - ){ + if ( defined ${$self}{BodyStartsOnOwnLine} + and ${$self}{BodyStartsOnOwnLine} == -1 + and ${$self}{body} !~ m/^(\h|\\|(?:!-!))/s ) + { ${$self}{begin} .= " "; } } -sub wrap_up_statement{ +sub wrap_up_statement { my $self = shift; # line break checks *after* \end{statement} - if (defined ${$self}{EndFinishesWithLineBreak} - and ${$self}{EndFinishesWithLineBreak}==-1 - ) { - # add a single horizontal space after the child id, otherwise we can end up + if ( defined ${$self}{EndFinishesWithLineBreak} + and ${$self}{EndFinishesWithLineBreak} == -1 ) + { + # add a single horizontal space after the child id, otherwise we can end up # with things like - # before: + # before: # \fi # text # after: # \fitext - $logger->trace("*Adding a single space after \\fi statement (otherwise \\fi can be comined with next line of text in an unwanted way)") if $is_t_switch_active; - ${$self}{end} =${$self}{end}." "; + $logger->trace( + "*Adding a single space after \\fi statement (otherwise \\fi can be comined with next line of text in an unwanted way)" + ) if $is_t_switch_active; + ${$self}{end} = ${$self}{end} . " "; } $logger->trace("*Finished indenting ${$self}{name}") if $is_t_switch_active; return $self; } - -sub create_unique_id{ +sub create_unique_id { my $self = shift; $ifElseFiCounter++; @@ -206,5 +221,4 @@ sub create_unique_id{ return; } - 1; |