Skip to content

Commit

Permalink
text wrap: *new feature*, text wrap comments, following #389
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhughes committed Dec 29, 2022
1 parent 4b25870 commit 939bd9b
Show file tree
Hide file tree
Showing 69 changed files with 1,405 additions and 105 deletions.
26 changes: 20 additions & 6 deletions LatexIndent/Document.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use LatexIndent::BlankLines qw/protect_blank_lines unprotect_blank_lines condens
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/;
use LatexIndent::Wrap qw/text_wrap/;
use LatexIndent::Wrap qw/text_wrap text_wrap_comment_blocks/;
use LatexIndent::TrailingComments
qw/remove_trailing_comments put_trailing_comments_back_in add_comment_symbol construct_trailing_comment_regexp/;
use LatexIndent::HorizontalWhiteSpace qw/remove_trailing_whitespace remove_leading_space/;
Expand Down Expand Up @@ -302,8 +302,9 @@ sub process_body_of_text {
and ${ $mainSettings{modifyLineBreaks}{oneSentencePerLine} }{manipulateSentences}
and ${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{when} eq 'after' )
{
$logger->trace("*one-sentence-per-line text wrapping routine, textWrapOptions:when set to 'after'") if $is_tt_switch_active;
$self->one_sentence_per_line( textWrap => 1);
$logger->trace("*one-sentence-per-line text wrapping routine, textWrapOptions:when set to 'after'")
if $is_tt_switch_active;
$self->one_sentence_per_line( textWrap => 1 );
}

# option for text wrap
Expand All @@ -315,16 +316,23 @@ sub process_body_of_text {
{
$self->text_wrap();
}

# option for comment text wrap
$self->text_wrap_comment_blocks()
if ($is_m_switch_active
and ${ ${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{comments} }{wrap}
and ${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{when} eq 'after' );
return;
}

sub find_objects {
my $self = shift;

# one sentence per line: sentences are objects, as of V3.5.1
$self->one_sentence_per_line( textWrap => (${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{when} eq 'before'))
if ($is_m_switch_active
and ${ $mainSettings{modifyLineBreaks}{oneSentencePerLine} }{manipulateSentences});
$self->one_sentence_per_line(
textWrap => ( ${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{when} eq 'before' ) )
if ( $is_m_switch_active
and ${ $mainSettings{modifyLineBreaks}{oneSentencePerLine} }{manipulateSentences} );

# text wrapping
#
Expand All @@ -347,6 +355,12 @@ sub find_objects {
$self->verbatim_modify_line_breaks( when => "afterTextWrap" );
}

# option for comment text wrap
$self->text_wrap_comment_blocks()
if ($is_m_switch_active
and ${ ${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{comments} }{wrap}
and ${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{when} eq 'before' );

# search for environments
$logger->trace('*looking for ENVIRONMENTS') if $is_t_switch_active;
$self->find_environments if ${$self}{body} =~ m/$environmentBasicRegExp/s;
Expand Down
82 changes: 51 additions & 31 deletions LatexIndent/GetYamlSettings.pm
Original file line number Diff line number Diff line change
Expand Up @@ -117,56 +117,73 @@ sub yaml_read_settings {

# we'll need the home directory a lot in what follows
my $homeDir = File::HomeDir->my_home;
$logger->info("*YAML reading settings") unless $switches{onlyDefault};
$logger->info("*YAML reading settings") unless $switches{onlyDefault};

my $indentconfig = undef;
if (defined $ENV{LATEXINDENT_CONFIG} && !$switches{onlyDefault}) {
if(-f $ENV{LATEXINDENT_CONFIG}) {
if ( defined $ENV{LATEXINDENT_CONFIG} && !$switches{onlyDefault} ) {
if ( -f $ENV{LATEXINDENT_CONFIG} ) {
$indentconfig = $ENV{LATEXINDENT_CONFIG};
$logger->info('The $LATEXINDENT_CONFIG variable was detected.');
$logger->info('The value of $LATEXINDENT_CONFIG is: "' . $ENV{LATEXINDENT_CONFIG} . '"');
} else {
$logger->info( 'The value of $LATEXINDENT_CONFIG is: "' . $ENV{LATEXINDENT_CONFIG} . '"' );
}
else {
$logger->warn('*The $LATEXINDENT_CONFIG variable is assigned, but does not point to a file!');
$logger->warn('The value of $LATEXINDENT_CONFIG is: "' . $ENV{LATEXINDENT_CONFIG} . '"');
$logger->warn( 'The value of $LATEXINDENT_CONFIG is: "' . $ENV{LATEXINDENT_CONFIG} . '"' );
}
}
if (!defined $indentconfig && !$switches{onlyDefault}) {
# see all possible values of $^O here: https://perldoc.perl.org/perlport#Unix and https://perldoc.perl.org/perlport#DOS-and-Derivatives
if ($^O eq "linux") {
if (defined $ENV{XDG_CONFIG_HOME} && -f "$ENV{XDG_CONFIG_HOME}/latexindent/indentconfig.yaml") {
if ( !defined $indentconfig && !$switches{onlyDefault} ) {

# see all possible values of $^O here: https://perldoc.perl.org/perlport#Unix and https://perldoc.perl.org/perlport#DOS-and-Derivatives
if ( $^O eq "linux" ) {
if ( defined $ENV{XDG_CONFIG_HOME} && -f "$ENV{XDG_CONFIG_HOME}/latexindent/indentconfig.yaml" ) {
$indentconfig = "$ENV{XDG_CONFIG_HOME}/latexindent/indentconfig.yaml";
$logger->info('The $XDG_CONFIG_HOME variable and the config file in "' . "$ENV{XDG_CONFIG_HOME}/latexindent/indentconfig.yaml" . '" were recognized');
$logger->info('The value of $XDG_CONFIG_HOME is: "' . $ENV{XDG_CONFIG_HOME} . '"');
} elsif (-f "$homeDir/.config/latexindent/indentconfig.yaml") {
$logger->info( 'The $XDG_CONFIG_HOME variable and the config file in "'
. "$ENV{XDG_CONFIG_HOME}/latexindent/indentconfig.yaml"
. '" were recognized' );
$logger->info( 'The value of $XDG_CONFIG_HOME is: "' . $ENV{XDG_CONFIG_HOME} . '"' );
}
elsif ( -f "$homeDir/.config/latexindent/indentconfig.yaml" ) {
$indentconfig = "$homeDir/.config/latexindent/indentconfig.yaml";
$logger->info('The config file in "' . "$homeDir/.config/latexindent/indentconfig.yaml" . '" was recognized');
$logger->info(
'The config file in "' . "$homeDir/.config/latexindent/indentconfig.yaml" . '" was recognized' );
}
} elsif ($^O eq "darwin") {
if (-f "$homeDir/Library/Preferences/latexindent/indentconfig.yaml") {
}
elsif ( $^O eq "darwin" ) {
if ( -f "$homeDir/Library/Preferences/latexindent/indentconfig.yaml" ) {
$indentconfig = "$homeDir/Library/Preferences/latexindent/indentconfig.yaml";
$logger->info('The config file in "' . "$homeDir/Library/Preferences/latexindent/indentconfig.yaml" . '" was recognized');
$logger->info( 'The config file in "'
. "$homeDir/Library/Preferences/latexindent/indentconfig.yaml"
. '" was recognized' );
}
} elsif ($^O eq "MSWin32" || $^O eq "cygwin") {
if (defined $ENV{LOCALAPPDATA} && -f "$ENV{LOCALAPPDATA}/latexindent/indentconfig.yaml") {
}
elsif ( $^O eq "MSWin32" || $^O eq "cygwin" ) {
if ( defined $ENV{LOCALAPPDATA} && -f "$ENV{LOCALAPPDATA}/latexindent/indentconfig.yaml" ) {
$indentconfig = "$ENV{LOCALAPPDATA}/latexindent/indentconfig.yaml";
$logger->info('The $LOCALAPPDATA variable and the config file in "' . "$ENV{LOCALAPPDATA}" . '\latexindent\indentconfig.yaml" were recognized');
$logger->info('The value of $LOCALAPPDATA is: "' . $ENV{LOCALAPPDATA} . '"');
} elsif (-f "$homeDir/AppData/Local/latexindent/indentconfig.yaml") {
$logger->info( 'The $LOCALAPPDATA variable and the config file in "'
. "$ENV{LOCALAPPDATA}"
. '\latexindent\indentconfig.yaml" were recognized' );
$logger->info( 'The value of $LOCALAPPDATA is: "' . $ENV{LOCALAPPDATA} . '"' );
}
elsif ( -f "$homeDir/AppData/Local/latexindent/indentconfig.yaml" ) {
$indentconfig = "$homeDir/AppData/Local/latexindent/indentconfig.yaml";
$logger->info('The config file in "' . "$homeDir" . '\AppData\Local\latexindent\indentconfig.yaml" was recognized');
$logger->info( 'The config file in "'
. "$homeDir"
. '\AppData\Local\latexindent\indentconfig.yaml" was recognized' );
}
}

# if $indentconfig is still not defined, fallback to the location in $homeDir
if ( !defined $indentconfig ) {

# if all of these don't exist check home directly, with the non hidden file
$indentconfig = (-f "$homeDir/indentconfig.yaml") ? "$homeDir/indentconfig.yaml" : undef;
$indentconfig = ( -f "$homeDir/indentconfig.yaml" ) ? "$homeDir/indentconfig.yaml" : undef;

# if indentconfig.yaml doesn't exist, check for the hidden file, .indentconfig.yaml
$indentconfig = (-f "$homeDir/.indentconfig.yaml") ? "$homeDir/.indentconfig.yaml" : undef;
$logger->info('The config file in "' . "$indentconfig" . '" was recognized') if defined $indentconfig;
$indentconfig = ( -f "$homeDir/.indentconfig.yaml" ) ? "$homeDir/.indentconfig.yaml" : undef;
$logger->info( 'The config file in "' . "$indentconfig" . '" was recognized' ) if defined $indentconfig;
}
}


# messages for indentconfig.yaml and/or .indentconfig.yaml
if ( defined $indentconfig && -f $indentconfig && !$switches{onlyDefault} ) {

Expand Down Expand Up @@ -219,7 +236,8 @@ sub yaml_read_settings {
else {
if ( $switches{onlyDefault} ) {
$logger->info("*-d switch active: only default settings requested");
$logger->info("not reading USER settings from $indentconfig") if ( defined $indentconfig && -e $indentconfig );
$logger->info("not reading USER settings from $indentconfig")
if ( defined $indentconfig && -e $indentconfig );
$logger->info("Ignoring the -l switch: $switches{readLocalSettings} (you used the -d switch)")
if ( $switches{readLocalSettings} );
$logger->info("Ignoring the -y switch: $switches{yaml} (you used the -d switch)") if ( $switches{yaml} );
Expand Down Expand Up @@ -733,9 +751,11 @@ sub yaml_read_settings {
# columns: 100
# when: after <!------
#
if ($is_m_switch_active and ${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{when} eq 'after'
and ${ $mainSettings{modifyLineBreaks}{oneSentencePerLine} }{sentenceIndent} =~ m/\h+/ ){
$logger->warn("*one-sentence-per-line *ignoring* sentenceIndent, as text wrapping set to 'after'");
if ( $is_m_switch_active
and ${ $mainSettings{modifyLineBreaks}{textWrapOptions} }{when} eq 'after'
and ${ $mainSettings{modifyLineBreaks}{oneSentencePerLine} }{sentenceIndent} =~ m/\h+/ )
{
$logger->warn("*one-sentence-per-line *ignoring* sentenceIndent, as text wrapping set to 'after'");
${ $mainSettings{modifyLineBreaks}{oneSentencePerLine} }{sentenceIndent} = q();
}

Expand Down
20 changes: 17 additions & 3 deletions LatexIndent/Sentence.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ our @EXPORT_OK = qw/one_sentence_per_line/;
our $sentenceCounter;

sub one_sentence_per_line {
my $self = shift;
my $self = shift;
my %input = @_;

$logger->trace(
"*One sentence per line regular expression construction: (see oneSentencePerLine: manipulateSentences)")
if $is_t_switch_active;

#
#
# sentences FOLLOW
#
my $sentencesFollow = q();
Expand Down Expand Up @@ -106,7 +106,7 @@ sub one_sentence_per_line {
$logger->trace("Sentences follow regexp:") if $is_tt_switch_active;
$logger->trace($sentencesFollow) if $is_tt_switch_active;

#
#
# sentences BEGIN with
#
my $sentencesBeginWith = q();
Expand Down Expand Up @@ -275,6 +275,20 @@ sub one_sentence_per_line {
$replacementText;
/xsge;

#
# remove spaces between trailing comments
#
#
# from:
#
# % first comment %second comment
# ^
# into:
#
# % first comment%second comment
#
${$self}{body} =~ s/($trailingCommentRegExp)\h($trailingCommentRegExp)/$1$2/sg;

if ( ${ $mainSettings{modifyLineBreaks}{oneSentencePerLine} }{sentenceIndent} !~ m/\h+/ ) {

# loop back through the sentenceStorage and replace with the sentence, adjusting line breaks
Expand Down
12 changes: 7 additions & 5 deletions LatexIndent/TrailingComments.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use LatexIndent::LogFile qw/$logger/;
use Data::Dumper;
use Exporter qw/import/;
our @EXPORT_OK
= qw/remove_trailing_comments put_trailing_comments_back_in $trailingCommentRegExp add_comment_symbol construct_trailing_comment_regexp/;
= qw/remove_trailing_comments put_trailing_comments_back_in $trailingCommentRegExp add_comment_symbol construct_trailing_comment_regexp @trailingComments/;
our @trailingComments;
our $commentCounter = 0;
our $trailingCommentRegExp;
Expand All @@ -39,13 +39,17 @@ sub add_comment_symbol {

# add a trailing comment token after, for example, a square brace [
# or a curly brace { when, for example, BeginStartsOnOwnLine == 2
my $self = shift;
my $self = shift;
my %input = @_;

my $commentValue = ( defined $input{value} ? $input{value} : q() );

# increment the comment counter
$commentCounter++;

# store the comment -- without this, it won't get processed correctly at the end
push( @trailingComments, { id => $tokens{trailingComment} . $commentCounter . $tokens{endOfToken}, value => q() } );
push( @trailingComments,
{ id => $tokens{trailingComment} . $commentCounter . $tokens{endOfToken}, value => $commentValue } );

# log file info
$logger->trace("*Updating trailing comment array") if $is_t_switch_active;
Expand Down Expand Up @@ -145,8 +149,6 @@ sub put_trailing_comments_back_in {
$logger->trace("Comment not at end of line $trailingcommentID, moving it to end of line")
if $is_t_switch_active;
${$self}{body} =~ s/%$trailingcommentID(.*)$/$1%$trailingcommentValue/m;
#####if(${$self}{body} =~ m/%$trailingcommentID\h*[^%]+?$/mx){
##### $logger->trace("Comment not at end of line $trailingcommentID, moving it to end of line") if $is_t_switch_active;
}
else {
${$self}{body} =~ s/%$trailingcommentID/%$trailingcommentValue/;
Expand Down
Loading

0 comments on commit 939bd9b

Please sign in to comment.