summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/latexindent
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2014-12-07 00:59:56 +0000
committerKarl Berry <karl@freefriends.org>2014-12-07 00:59:56 +0000
commite1de1645f43fb9336c0c19a3589b553ca59e3963 (patch)
treecc5d849f59f3f4efbb690fbcda6e0ef37498df9c /Master/texmf-dist/scripts/latexindent
parentc2e02cd8865266a299d7d6149dea876ed0503074 (diff)
latexindent (5dec14)
git-svn-id: svn://tug.org/texlive/trunk@35757 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/latexindent')
-rwxr-xr-xMaster/texmf-dist/scripts/latexindent/defaultSettings.yaml62
-rwxr-xr-xMaster/texmf-dist/scripts/latexindent/latexindent.pl711
2 files changed, 575 insertions, 198 deletions
diff --git a/Master/texmf-dist/scripts/latexindent/defaultSettings.yaml b/Master/texmf-dist/scripts/latexindent/defaultSettings.yaml
index 457629ce863..799c081f4dc 100755
--- a/Master/texmf-dist/scripts/latexindent/defaultSettings.yaml
+++ b/Master/texmf-dist/scripts/latexindent/defaultSettings.yaml
@@ -34,6 +34,18 @@ onlyOneBackUp: 0
# many backups are possible, unless onlyOneBackUp is switched on
maxNumberOfBackUps: 0
+# some users may wish to cycle through back up files, for example,
+# with maxNumberOfBackUps: 4, they may wish to delete the oldest
+# back up file, and keep only the most recent.
+#
+# copy myfile.bak1 to myfile.bak0
+# copy myfile.bak2 to myfile.bak1
+# copy myfile.bak3 to myfile.bak2
+# copy myfile.bak4 to myfile.bak3
+#
+# the back up will be written to myfile.bak4
+cycleThroughBackUps: 0
+
# indent preamble
indentPreamble: 0
@@ -53,6 +65,7 @@ removeTrailingWhitespace: 0
lookForAlignDelims:
tabular: 1
tabularx: 1
+ longtable: 1
array: 1
matrix: 1
bmatrix: 1
@@ -64,7 +77,6 @@ lookForAlignDelims:
aligned: 1
cases: 1
dcases: 1
- pmatrix: 1
listabla: 1
# if you have indent rules for particular environments
@@ -75,6 +87,7 @@ indentRules:
anotherenvironment: "\t\t\t\t"
chapter: " "
section: " "
+ item: " "
# verbatim environments- environments specified
# in this hash table will not be changed at all!
@@ -143,6 +156,53 @@ indentAfterHeadings:
indent: 0
level: 7
+# if you want the script to look for \item commands
+# and format it, as follows (for example),
+# \begin{itemize}
+# \item content here
+# next line is indented
+# next line is indented
+# \item another item
+# \end{itemize}
+# then populate indentAfterItems. See also itemNames
+indentAfterItems:
+ itemize: 1
+ enumerate: 1
+ list: 1
+
+# if you want to use other names for your items (such as, for example, part)
+# then populate them here- note that you can trick latexindent.pl
+# into indenting all kinds of commands (within environments specified in
+# indentAfterItems) using this technique.
+itemNames:
+ item: 1
+ myitem: 1
+
+# if you want to indent if, else, fi constructs such as, for example,
+#
+# \ifnum#1=2
+# something
+# \else
+# something else
+# \fi
+#
+# then populate them in constructIfElseFi
+constructIfElseFi:
+ ifnum: 1
+ ifdim: 1
+ ifodd: 1
+ ifvmode: 1
+ ifhmode: 1
+ ifmmode: 1
+ ifinner: 1
+ if: 1
+ ifcat: 1
+ ifx: 1
+ ifvoid: 1
+ ifeof: 1
+ iftrue: 1
+ ifcase: 1
+
# *** NOTE ***
# If you have specified alwaysLookforSplitBraces: 1
# and alwaysLookforSplitBrackets: 1 then you don't need
diff --git a/Master/texmf-dist/scripts/latexindent/latexindent.pl b/Master/texmf-dist/scripts/latexindent/latexindent.pl
index 9376117c324..d023ef014d0 100755
--- a/Master/texmf-dist/scripts/latexindent/latexindent.pl
+++ b/Master/texmf-dist/scripts/latexindent/latexindent.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# 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
@@ -13,14 +13,29 @@
#
# For details of how to use this file, please see readme.txt
-# load packages/modules
+# load packages/modules: assume strict and warnings are part of every perl distribution
use strict;
-use warnings;
-use FindBin; # help find defaultSettings.yaml
-use YAML::Tiny; # interpret defaultSettings.yaml
+use warnings;
+
+# check the other modules are available
+foreach my $moduleName ('FindBin','YAML::Tiny','File::Copy','File::Basename','Getopt::Long','File::HomeDir')
+{
+ # references:
+ # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
+ # http://stackoverflow.com/questions/1917261/how-can-i-dynamically-include-perl-modules-without-using-eval
+ eval {
+ (my $file = $moduleName) =~ s|::|/|g;
+ require $file . '.pm';
+ $moduleName->import();
+ 1;
+ } or die "$moduleName Perl Module not currently installed; please install the module, and then try running latexindent.pl again; exiting";
+}
+
+# now that we have confirmed the modules are available, load them
+use FindBin; # help find defaultSettings.yaml
+use YAML::Tiny; # interpret defaultSettings.yaml and other potential settings files
use File::Copy; # to copy the original file to backup (if overwrite option set)
use File::Basename; # to get the filename and directory path
-#use Getopt::Std;
use Getopt::Long; # to get the switches/options/flags
use File::HomeDir; # to get users home directory, regardless of OS
@@ -32,7 +47,7 @@ my $tracingMode;
my $readLocalSettings;
my $onlyDefault;
my $showhelp;
-my $cruftDirectory='./';
+my $cruftDirectory;
GetOptions ("w"=>\$overwrite,
"o"=>\$outputToFile,
@@ -44,12 +59,10 @@ GetOptions ("w"=>\$overwrite,
"c=s"=>\$cruftDirectory,
);
-die "Could not find directory $cruftDirectory\nExiting, no indentation done." if(!(-d $cruftDirectory));
-
# version number
-my $versionNumber = "1.11R";
+my $versionNumber = "2.0R";
-# Check the number of input arguments- if it is 0 then simply
+# Check the number of input arguments- if it is 0 then simply
# display the list of options (like a manual)
if(scalar(@ARGV) < 1 or $showhelp)
{
@@ -70,6 +83,11 @@ ENDQUOTE
exit(2);
}
+# set up default for cruftDirectory using the one from the input file,
+# unless it has been specified using -c="/some/directory"
+$cruftDirectory=dirname $ARGV[0] unless(defined($cruftDirectory));
+
+die "Could not find directory $cruftDirectory\nExiting, no indentation done." if(!(-d $cruftDirectory));
# we'll be outputting to the logfile and to standard output
my $logfile;
@@ -97,12 +115,12 @@ ENDQUOTE
# a quick options check
if($outputToFile and $overwrite)
{
- print $logfile <<ENDQUOTE
+ print $logfile <<ENDQUOTE
-WARNING:
+WARNING:
\t You have called latexindent.pl with both -o and -w
\t -o (output to file) will take priority, and -w (over write) will be ignored
-
+
ENDQUOTE
;
$overwrite = 0;
@@ -115,7 +133,7 @@ if(scalar(@ARGV)>2)
ERROR:
\t You're calling latexindent.pl with more than two file names
-\t The script can take at MOST two file names, but you
+\t The script can take at MOST two file names, but you
\t need to call it with the -o switch; for example
\t latexindent.pl -o originalfile.tex outputfile.tex
@@ -144,7 +162,7 @@ ENDQUOTE
exit(2);
}
-# if the script is called with the -o switch, then check that
+# if the script is called with the -o switch, then check that
# a second file is present in the call, e.g
# latexindent.pl -o myfile.tex output.tex
if($outputToFile and scalar(@ARGV)==1)
@@ -173,9 +191,9 @@ $defaultSettings = YAML::Tiny->read( "$FindBin::RealBin/defaultSettings.yaml" );
if(!$defaultSettings)
{
for my $fh ($out,$logfile) {
- print $fh <<ENDQUOTE
+ print $fh <<ENDQUOTE
ERROR There seems to be a yaml formatting error in defaultSettings.yaml
- Please check it for mistakes- you can find a working version at https://github.com/cmhughes/latexindent.plx
+ Please check it for mistakes- you can find a working version at https://github.com/cmhughes/latexindent.pl
if you would like to overwrite your current version
Exiting, no indendation done.
@@ -195,6 +213,7 @@ my $indentPreamble = $defaultSettings->[0]->{indentPreamble};
my $onlyOneBackUp = $defaultSettings->[0]->{onlyOneBackUp};
my $maxNumberOfBackUps = $defaultSettings->[0]->{maxNumberOfBackUps};
my $removeTrailingWhitespace = $defaultSettings->[0]->{removeTrailingWhitespace};
+my $cycleThroughBackUps = $defaultSettings->[0]->{cycleThroughBackUps};
# hash variables
my %lookForAlignDelims= %{$defaultSettings->[0]->{lookForAlignDelims}};
@@ -206,6 +225,9 @@ my %checkunmatchedELSE= %{$defaultSettings->[0]->{checkunmatchedELSE}};
my %checkunmatchedbracket= %{$defaultSettings->[0]->{checkunmatchedbracket}};
my %noAdditionalIndent= %{$defaultSettings->[0]->{noAdditionalIndent}};
my %indentAfterHeadings= %{$defaultSettings->[0]->{indentAfterHeadings}};
+my %indentAfterItems= %{$defaultSettings->[0]->{indentAfterItems}};
+my %itemNames= %{$defaultSettings->[0]->{itemNames}};
+my %constructIfElseFi= %{$defaultSettings->[0]->{constructIfElseFi}};
# need new hashes to store the user and local data before
# overwriting the default
@@ -218,6 +240,9 @@ my %checkunmatchedELSEUSER;
my %checkunmatchedbracketUSER;
my %noAdditionalIndentUSER;
my %indentAfterHeadingsUSER;
+my %indentAfterItemsUSER;
+my %itemNamesUSER;
+my %constructIfElseFiUSER;
# for printing the user and local settings to the log file
my %dataDump;
@@ -230,7 +255,7 @@ my $userSettings;
# get information about user settings- first check if indentconfig.yaml exists
my $indentconfig = File::HomeDir->my_home . "/indentconfig.yaml";
-if ( -e $indentconfig and !$onlyDefault )
+if ( -e $indentconfig and !$onlyDefault )
{
print $logfile "Reading path information from ",File::HomeDir->my_home,"/indentconfig.yaml\n";
@@ -248,13 +273,13 @@ if ( -e $indentconfig and !$onlyDefault )
else
{
print $logfile <<ENDQUOTE
-WARNING: $indentconfig
+WARNING: $indentconfig
contains some invalid .yaml formatting- unable to read from it.
- No user settings loaded.
+ No user settings loaded.
ENDQUOTE
;
}
-}
+}
else
{
if($onlyDefault)
@@ -275,16 +300,16 @@ else
my $directoryName = dirname $ARGV[0];
# add local settings to the paths, if appropriate
-if ( (-e "$directoryName/localSettings.yaml") and $readLocalSettings and !(-z "$directoryName/localSettings.yaml"))
+if ( (-e "$directoryName/localSettings.yaml") and $readLocalSettings and !(-z "$directoryName/localSettings.yaml"))
{
- print $logfile "\nAdding ./localSettings.yaml to paths\n\n";
+ print $logfile "\nAdding $directoryName/localSettings.yaml to paths\n\n";
push(@absPaths,"$directoryName/localSettings.yaml");
}
-elsif ( !(-e "$directoryName/localSettings.yaml") and $readLocalSettings)
+elsif ( !(-e "$directoryName/localSettings.yaml") and $readLocalSettings)
{
print $logfile "WARNING\n\t$directoryName/localSettings.yaml not found\n";
print $logfile "\tcarrying on without it.\n";
-}
+}
# read in the settings from each file
foreach my $settings (@absPaths)
@@ -312,8 +337,9 @@ foreach my $settings (@absPaths)
$onlyOneBackUp = $userSettings->[0]->{onlyOneBackUp} if defined($userSettings->[0]->{onlyOneBackUp});
$maxNumberOfBackUps = $userSettings->[0]->{maxNumberOfBackUps} if defined($userSettings->[0]->{maxNumberOfBackUps});
$removeTrailingWhitespace = $userSettings->[0]->{removeTrailingWhitespace} if defined($userSettings->[0]->{removeTrailingWhitespace});
+ $cycleThroughBackUps = $userSettings->[0]->{cycleThroughBackUps} if defined($userSettings->[0]->{cycleThroughBackUps});
- # hash variables - note that each one requires two lines,
+ # hash variables - note that each one requires two lines,
# one to read in the data, one to put the keys&values in correctly
%lookForAlignDelimsUSER= %{$userSettings->[0]->{lookForAlignDelims}} if defined($userSettings->[0]->{lookForAlignDelims});
@@ -340,6 +366,14 @@ foreach my $settings (@absPaths)
%indentAfterHeadingsUSER= %{$userSettings->[0]->{indentAfterHeadings}} if defined($userSettings->[0]->{indentAfterHeadings});
@indentAfterHeadings{ keys %indentAfterHeadingsUSER } = values %indentAfterHeadingsUSER if (%indentAfterHeadingsUSER);
+ %indentAfterItemsUSER= %{$userSettings->[0]->{indentAfterItems}} if defined($userSettings->[0]->{indentAfterItems});
+ @indentAfterItems{ keys %indentAfterItemsUSER } = values %indentAfterItemsUSER if (%indentAfterItemsUSER);
+
+ %itemNamesUSER= %{$userSettings->[0]->{itemNames}} if defined($userSettings->[0]->{itemNames});
+ @itemNames{ keys %itemNamesUSER } = values %itemNamesUSER if (%itemNamesUSER);
+
+ %constructIfElseFiUSER= %{$userSettings->[0]->{constructIfElseFi}} if defined($userSettings->[0]->{constructIfElseFi});
+ @constructIfElseFi{ keys %constructIfElseFiUSER } = values %constructIfElseFiUSER if (%constructIfElseFiUSER);
}
else
{
@@ -371,13 +405,14 @@ if ($overwrite)
print $logfile "\nBackup procedure:\n";
# original name of file
my $filename = $ARGV[0];
- # copy it
- my $backupFile = $filename;
+
+ # get the base file name, allowing for different extensions
+ my $backupFile = basename($filename,(".tex",".sty",".cls"));
# add the user's backup directory to the backup path
$backupFile = "$cruftDirectory/$backupFile";
- # if both ($onlyOneBackUp and $maxNumberOfBackUps) then we have
+ # if both ($onlyOneBackUp and $maxNumberOfBackUps) then we have
# a conflict- er on the side of caution and turn off onlyOneBackUp
if($onlyOneBackUp and $maxNumberOfBackUps>1)
{
@@ -386,7 +421,7 @@ if ($overwrite)
$onlyOneBackUp = 0;
}
- # if the user has specified that $maxNumberOfBackUps = 1 then
+ # if the user has specified that $maxNumberOfBackUps = 1 then
# they only want one backup
if($maxNumberOfBackUps==1)
{
@@ -407,7 +442,7 @@ if ($overwrite)
# be overwritten each time
if($onlyOneBackUp)
{
- $backupFile =~ s/\.tex/$backupExtension/;
+ $backupFile .= $backupExtension;
print $logfile "\t copying $filename to $backupFile\n";
print $logfile "\t $backupFile was overwritten\n\n" if (-e $backupFile);
}
@@ -415,7 +450,7 @@ if ($overwrite)
{
# start with a backup file .bak0 (or whatever $backupExtension is present)
my $backupCounter = 0;
- $backupFile =~ s/\.tex$/$backupExtension$backupCounter/;
+ $backupFile .= $backupExtension.$backupCounter;
# if it exists, then keep going: .bak0, .bak1, ...
while (-e $backupFile or $maxNumberOfBackUps>1)
@@ -423,6 +458,36 @@ if ($overwrite)
if($backupCounter==$maxNumberOfBackUps)
{
print $logfile "\t maxNumberOfBackUps reached ($maxNumberOfBackUps)\n";
+
+ # some users may wish to cycle through back up files, e.g:
+ # copy myfile.bak1 to myfile.bak0
+ # copy myfile.bak2 to myfile.bak1
+ # copy myfile.bak3 to myfile.bak2
+ #
+ # current back up is stored in myfile.bak4
+ if($cycleThroughBackUps)
+ {
+ print $logfile "\t cycleThroughBackUps detected (see cycleThroughBackUps) \n";
+ for(my $i=1;$i<=$maxNumberOfBackUps;$i++)
+ {
+ # remove number from backUpFile
+ my $oldBackupFile = $backupFile;
+ $oldBackupFile =~ s/$backupExtension.*/$backupExtension/;
+ my $newBackupFile = $oldBackupFile;
+
+ # add numbers back on
+ $oldBackupFile .= $i;
+ $newBackupFile .= $i-1;
+
+ # check that the oldBackupFile exists
+ if(-e $oldBackupFile){
+ print $logfile "\t\t copying $oldBackupFile to $newBackupFile \n";
+ copy($oldBackupFile,$newBackupFile) or die "Could not write to backup file $backupFile. Please check permissions. Exiting.\n";
+ }
+ }
+ }
+
+ # rest maxNumberOfBackUps
$maxNumberOfBackUps=1 ;
last; # break out of the loop
}
@@ -444,7 +509,7 @@ if ($overwrite)
copy($filename,$backupFile) or die "Could not write to backup file $backupFile. Please check permissions. Exiting.\n";
}
-if(!($outputToFile or $overwrite))
+if(!($outputToFile or $overwrite))
{
print $logfile "Just out put to the terminal :)\n\n" if !$silentMode ;
}
@@ -460,28 +525,28 @@ my $inverbatim=0; # $inverbatim: switch to determine if in
my $delimiters=0; # $delimiters: switch that governs if
# we need to check for & or not
my $matchedbraces=0; # $matchedbraces: counter to see if { }
- # are matched; it will be
- # positive if too many {
+ # are matched; it will be
+ # positive if too many {
# negative if too many }
# 0 if matched
my $matchedBRACKETS=0; # $matchedBRACKETS: counter to see if [ ]
- # are matched; it will be
- # positive if too many {
+ # are matched; it will be
+ # positive if too many {
# negative if too many }
# 0 if matched
my $commandname; # $commandname: either \parbox, \marginpar,
# or anything else from %checkunmatched
my $commanddetails; # $command details: a scalar that stores
- # details about the command
+ # details about the command
# that splits {} across lines
-my $countzeros; # $countzeros: a counter that helps
+my $countzeros; # $countzeros: a counter that helps
# when determining if we're in
# an else construct
-my $lookforelse=0; # $lookforelse: a boolean to help determine
- # if we need to look for an
+my $lookforelse=0; # $lookforelse: a boolean to help determine
+ # if we need to look for an
# else construct
-my $trailingcomments; # $trailingcomments stores the comments at the end of
- # a line
+my $trailingcomments; # $trailingcomments stores the comments at the end of
+ # a line
my $lineCounter=0; # $lineCounter keeps track of the line number
my $inIndentBlock=0; # $inindentblock: switch to determine if in
# a inindentblock or not
@@ -492,26 +557,26 @@ my $headingLevel=0; # $headingLevel: scalar that stores which heading
my @indent; # @indent: stores current level of indentation
my @lines; # @lines: stores the newly indented lines
my @block; # @block: stores blocks that have & delimiters
-my @commandstore; # @commandstore: stores commands that
+my @commandstore; # @commandstore: stores commands that
# have split {} across lines
-my @commandstorebrackets; # @commandstorebrackets: stores commands that
+my @commandstorebrackets; # @commandstorebrackets: stores commands that
# have split [] across lines
-my @mainfile; # @mainfile: stores input file; used to
+my @mainfile; # @mainfile: stores input file; used to
# grep for \documentclass
my @headingStore; # @headingStore: stores headings: chapter, section, etc
-my @indentNames; # @indentNames: keeps names of commands and
+my @indentNames; # @indentNames: keeps names of commands and
# environments that have caused
# indentation to increase
-my @environmentStack; # @environmentStack: stores the (nested) names
+my @environmentStack; # @environmentStack: stores the (nested) names
# of environments
-# check to see if the current file has \documentclass, if so, then
+# check to see if the current file has \documentclass, if so, then
# it's the main file, if not, then it doesn't have preamble
open(MAINFILE, $ARGV[0]) or die "Could not open input file";
@mainfile=<MAINFILE>;
close(MAINFILE);
-# if the MAINFILE doesn't have a \documentclass statement, then
+# if the MAINFILE doesn't have a \documentclass statement, then
# it shouldn't have preamble
if(scalar(@{[grep(m/^\s*\\documentclass/, @mainfile)]})==0)
{
@@ -532,7 +597,7 @@ while(<MAINFILE>)
{
# increment the line counter
$lineCounter++;
-
+
# tracing mode
print $logfile "\n" if($tracingMode and !($inpreamble or $inverbatim or $inIndentBlock));
@@ -542,10 +607,10 @@ while(<MAINFILE>)
{
# if not, remove all leading spaces and tabs
# from the current line, assuming it isn't empty
- #s/^\ *//;
- #s/^\s+//;
- #s/^\t+//;
- s/^\t*// if($_ !~ /^((\s*)|(\t*))*$/);
+ #s/^\ *//;
+ #s/^\s+//;
+ #s/^\t+//;
+ s/^\t*// if($_ !~ /^((\s*)|(\t*))*$/);
s/^\s*// if($_ !~ /^((\s*)|(\t*))*$/);
# tracing mode
@@ -580,19 +645,28 @@ while(<MAINFILE>)
}
}
+ # \END{ENVIRONMENTS}, or CLOSING } or CLOSING ]
+ # \END{ENVIRONMENTS}, or CLOSING } or CLOSING ]
+ # \END{ENVIRONMENTS}, or CLOSING } or CLOSING ]
+
+ print $logfile "Line $lineCounter\t << PHASE 1: looking for reasons to DECREASE indentation of CURRENT line \n" if($tracingMode);
# check to see if we have \end{something} or \]
&at_end_of_env_or_eq() unless ($inpreamble or $inIndentBlock);
- # check to see if we're at the end of a noindent
+ # check to see if we have %* \end{something} for alignment blocks
+ # outside of environments
+ &end_command_with_alignment();
+
+ # check to see if we're at the end of a noindent
# block %\end{noindent}
&at_end_noindent();
# only check for unmatched braces if we're not in
# a verbatim-like environment or in the preamble or in a
- # noIndentBlock
- if(!($inverbatim or $inpreamble or $inIndentBlock))
+ # noIndentBlock or in a delimiter block
+ if(!($inverbatim or $inpreamble or $inIndentBlock or $delimiters))
{
- # The check for closing } and ] relies on counting, so
+ # The check for closing } and ] relies on counting, so
# we have to remove trailing comments so that any {, }, [, ]
# that are found after % are not counted
#
@@ -615,13 +689,19 @@ while(<MAINFILE>)
# we're not starting another command that has split braces (nesting)
&end_command_or_key_unmatched_braces();
- # check to see if we're at the end of a command that splits
+ # check to see if we're at the end of a command that splits
# [ ] across lines
&end_command_or_key_unmatched_brackets();
- # check for a heading
+ # check for a heading such as \chapter, \section, etc
&indent_heading();
+ # check for \item
+ &indent_item();
+
+ # check for \else or \fi
+ &indent_if_else_fi();
+
# add the trailing comments back to the end of the line
if(scalar($trailingcomments))
{
@@ -630,21 +710,23 @@ while(<MAINFILE>)
$_ = $_ . $trailingcomments."\n" ;
# tracing mode
- print $logfile "Line $lineCounter\t counting braces/brackets complete: added trailing comments back on $trailingcomments\n" if($tracingMode);
+ print $logfile "Line $lineCounter\t counting braces/brackets complete\n" if($tracingMode);
+ print $logfile "Line $lineCounter\t Adding trailing comments back on: $trailingcomments\n" if($tracingMode);
# empty the trailingcomments
$trailingcomments='';
}
+ # remove trailing whitespace
+ if ($removeTrailingWhitespace)
+ {
+ print $logfile "Line $lineCounter\t removing trailing whitespace (see removeTrailingWhitespace)\n" if ($tracingMode);
+ s/\s+$/\n/;
+ }
}
- # remove trailing whitespace
- if ($removeTrailingWhitespace)
- {
- print $logfile "Line $lineCounter\t removing trailing whitespace\n" if ($tracingMode);
- s/\s+$/\n/;
- }
-
+ # ADD CURRENT LEVEL OF INDENTATION
+ # ADD CURRENT LEVEL OF INDENTATION
# ADD CURRENT LEVEL OF INDENTATION
# (unless we're in a delimiter-aligned block)
if(!$delimiters)
@@ -666,7 +748,7 @@ while(<MAINFILE>)
}
push(@lines,$_);
# tracing mode
- print $logfile "Line $lineCounter\t Adding current level of indentation: ",join(", ",@indentNames),"\n" if($tracingMode);
+ print $logfile "Line $lineCounter\t || PHASE 2: Adding current level of indentation: ",join(", ",@indentNames),"\n" if($tracingMode);
}
}
else
@@ -678,21 +760,39 @@ while(<MAINFILE>)
print $logfile "Line $lineCounter\t In delimeter block, waiting for block formatting\n" if($tracingMode);
}
- # only check for new environments or commands if we're
+ # \BEGIN{ENVIRONMENT} or OPEN { or OPEN [
+ # \BEGIN{ENVIRONMENT} or OPEN { or OPEN [
+ # \BEGIN{ENVIRONMENT} or OPEN { or OPEN [
+
+ # only check for new environments or commands if we're
# not in a verbatim-like environment or in the preamble
# or in a noIndentBlock, or delimiter block
if(!($inverbatim or $inpreamble or $inIndentBlock or $delimiters))
{
- # check if we are in a
+ print $logfile "Line $lineCounter\t >> PHASE 3: looking for reasons to INCREASE indentation of SUBSEQUENT lines \n" if($tracingMode);
+
+ # check if we are in a
# % \begin{noindent}
# block; this is similar to a verbatim block, the user
- # may not want some blocks of code to be touched
+ # may not want some blocks of code to be touched
#
# IMPORTANT: this needs to go before the trailing comments
# are removed!
&at_beg_noindent();
+ # check for
+ # %* \begin{tabular}
+ # which might be used to align blocks that contain delimeters that
+ # are NOT contained in an alignment block in the usual way, e.g
+ # \matrix{
+ # %* \begin{tabular}
+ # 1 & 2 \\
+ # 3 & 4 \\
+ # %* \end{tabular}
+ # }
+ &begin_command_with_alignment();
+
# remove trailing comments so that any {, }, [, ]
# that are found after % are not counted
#
@@ -706,7 +806,7 @@ while(<MAINFILE>)
# tracing mode
print $logfile "Line $lineCounter\t Removing trailing comments for brace count (line is already stored)\n" if($tracingMode);
- # check to see if we have \begin{something} or \[
+ # check to see if we have \begin{something} or \[
&at_beg_of_env_or_eq();
# check to see if we have \parbox, \marginpar, or
@@ -714,7 +814,7 @@ while(<MAINFILE>)
# specified in %checkunmatched hash table
&start_command_or_key_unmatched_braces();
- # check for an else statement
+ # check for an else statement (braces, not \else)
&check_for_else();
# check for a command that splits [] across lines
@@ -723,6 +823,12 @@ while(<MAINFILE>)
# check for a heading
&indent_after_heading();
+ # check for \item
+ &indent_after_item();
+
+ # check for \if or \else command
+ &indent_after_if_else_fi();
+
# tracing mode
print $logfile "Line $lineCounter\t Environments: ",join(", ",@environmentStack),"\n" if($tracingMode and scalar(@environmentStack));
}
@@ -737,7 +843,7 @@ print $logfile "Line Count of indented $ARGV[0]: ",scalar(@lines);
if(scalar(@mainfile) != scalar(@lines))
{
print $logfile <<ENDQUOTE
-WARNING: \t line count of original file and indented file does
+WARNING: \t line count of original file and indented file does
\t not match- consider reverting to a back up, see $backupExtension;
ENDQUOTE
;
@@ -771,6 +877,162 @@ close($logfile);
exit;
+sub indent_if_else_fi{
+ # PURPOSE: set indentation of line that contains \else, \fi command
+ #
+ #
+
+ # @indentNames could be empty -- if so, exit
+ return 0 unless(@indentNames);
+
+ # look for \fi
+ if( $_ =~ m/^\s*\\fi/ and $constructIfElseFi{$indentNames[-1]})
+ {
+ # tracing mode
+ print $logfile "Line $lineCounter\t \\fi command found, matching: \\",$indentNames[-1], "\n" if($tracingMode);
+ &decrease_indent($indentNames[-1]);
+ }
+ elsif( ($_ =~ m/^\s*\\else/ or $_ =~ m/^\s*\\or/) and $constructIfElseFi{$indentNames[-1]})
+ {
+ # tracing mode
+ print $logfile "Line $lineCounter\t \\else command found, matching: \\",$indentNames[-1], "\n" if($tracingMode);
+ print $logfile "Line $lineCounter\t decreasing indent, but indentNames is still \\",join(", ",@indentNames), "\n" if($tracingMode);
+ pop(@indent);
+ }
+}
+
+sub indent_after_if_else_fi{
+ # PURPOSE: set indentation *after* \if construct such as
+ #
+ # \ifnum\x=2
+ # <stuff>
+ # <stuff>
+ # \else
+ # <stuff>
+ # <stuff>
+ # \fi
+ #
+ # How to read /^\s*\\(if.*?)(\s|\\|\#)
+ #
+ # ^\s* begins with multiple spaces (possibly none)
+ # \\(if.*?)(\s|\\|\#) matches \if... up to either a
+ # space, a \, or a #
+ if( $_ =~ m/^\s*\\(if.*?)(\s|\\|\#)/ and $constructIfElseFi{$1})
+ {
+ # tracing mode
+ print $logfile "Line $lineCounter\t ifelsefi construct found: $1 \n" if($tracingMode);
+ &increase_indent($1);
+ }
+ elsif(@indentNames)
+ {
+ if( ($_ =~ m/^\s*\\else/ or $_ =~ m/^\s*\\or/ ) and $constructIfElseFi{$indentNames[-1]})
+ {
+ # tracing mode
+ print $logfile "Line $lineCounter\t setting indent *after* \\else or \\or command found for $indentNames[-1] \n" if($tracingMode);
+ &increase_indent($indentNames[-1]);
+ # don't want to store the name of the \if construct twice
+ # so remove the second copy
+ pop(@indentNames);
+ }
+ }
+ }
+
+sub indent_item{
+ # PURPOSE: this subroutine sets the indentation for the item *itself*
+
+ if( $_ =~ m/^\s*\\(.*?)(\[|\s)/ and $itemNames{$1} and $indentAfterItems{$environmentStack[-1]})
+ {
+ # tracing mode
+ print $logfile "Line $lineCounter\t $1 found within ",$environmentStack[-1]," environment (see indentAfterItems and itemNames)\n" if($tracingMode);
+ if($itemNames{$indentNames[-1]})
+ {
+ print $logfile "Line $lineCounter\t $1 found- neutralizing indentation from previous ",$indentNames[-1],"\n" if($tracingMode);
+ &decrease_indent($1);
+ }
+ }
+
+}
+
+sub indent_after_item{
+ # PURPOSE: Set the indentation *after* the item
+ # This matches a line that begins with
+ #
+ # \item
+ # \item[
+ # \myitem
+ # \myitem[
+ #
+ # or anything else specified in itemNames
+ #
+ if( $_ =~ m/^\s*\\(.*?)(\[|\s)/
+ and $itemNames{$1}
+ and $indentAfterItems{$environmentStack[-1]})
+ {
+ # tracing mode
+ print $logfile "Line $lineCounter\t $1 found within ",$environmentStack[-1]," environment (see indentAfterItems and itemNames)\n" if($tracingMode);
+ &increase_indent($1);
+ }
+}
+sub begin_command_with_alignment{
+ # PURPOSE: This matches
+ # %* \begin{tabular}
+ # with any number of spaces (possibly none) between
+ # the * and \begin{noindent}.
+ #
+ # the comment symbol IS indended!
+ #
+ # This is to align blocks that contain delimeters that
+ # are NOT contained in an alignment block in the usual way, e.g
+ # \matrix{
+ # %* \begin{tabular}
+ # 1 & 2 \\
+ # 3 & 4 \\
+ # %* \end{tabular}
+ # }
+
+ if( $_ =~ m/^\s*%\*\s*\\begin{(.*?)}/ and $lookForAlignDelims{$1})
+ {
+ $delimiters=1;
+ # tracing mode
+ print $logfile "Line $lineCounter\t Delimiter environment started: $1 (see lookForAlignDelims)\n" if($tracingMode);
+ }
+}
+
+sub end_command_with_alignment{
+ # PURPOSE: This matches
+ # %* \end{tabular}
+ # with any number of spaces (possibly none) between
+ # the * and \end{tabular} (or any other name used from
+ # lookFroAlignDelims)
+ #
+ # Note: the comment symbol IS indended!
+ #
+ # This is to align blocks that contain delimeters that
+ # are NOT contained in an alignment block in the usual way, e.g
+ # \matrix{
+ # %* \begin{tabular}
+ # 1 & 2 \\
+ # 3 & 4 \\
+ # %* \end{tabular}
+ # }
+
+ if( $_ =~ m/^\s*%\*\s*\\end{(.*?)}/ and $lookForAlignDelims{$1})
+ {
+ # same subroutine used at the end of regular tabular, align, etc
+ # environments
+ if($delimiters)
+ {
+ &print_aligned_block();
+ }
+ else
+ {
+ # tracing mode
+ print $logfile "Line $lineCounter\t FYI: did you mean to start a delimiter block on a previous line? \n" if($tracingMode);
+ print $logfile "Line $lineCounter\t perhaps using %* \\begin{$1}\n" if($tracingMode);
+ }
+ }
+}
+
sub indent_heading{
# PURPOSE: This matches
# \part
@@ -783,7 +1045,7 @@ sub indent_heading{
#
# and anything else listed in indentAfterHeadings
#
- # This subroutine specifies the indentation for the
+ # This subroutine specifies the indentation for the
# heading itself, i.e the line that has \chapter, \section etc
if( $_ =~ m/^\s*\\(.*?)(\[|{)/ and $indentAfterHeadings{$1})
{
@@ -796,15 +1058,15 @@ sub indent_heading{
# local scalar
my $previousHeadingLevel = $headingLevel;
- # if current heading level < old heading level,
+ # if current heading level < old heading level,
if($currentHeading{level}<$previousHeadingLevel)
{
- # decrease indentation, but only if
- # specified in indentHeadings. Note that this check
- # needs to be done here- decrease_indent won't
+ # decrease indentation, but only if
+ # specified in indentHeadings. Note that this check
+ # needs to be done here- decrease_indent won't
# check a nested hash
-
- if(scalar(@headingStore))
+
+ if(scalar(@headingStore))
{
while($currentHeading{level}<$previousHeadingLevel and scalar(@headingStore))
{
@@ -823,7 +1085,7 @@ sub indent_heading{
}
elsif($currentHeading{level}==$previousHeadingLevel)
{
- if(scalar(@headingStore))
+ if(scalar(@headingStore))
{
my $higherHeadingName = pop(@headingStore);
my %higherLevelHeading = %{$indentAfterHeadings{$higherHeadingName}};
@@ -852,7 +1114,7 @@ sub indent_after_heading{
#
# and anything else listed in indentAfterHeadings
#
- # This subroutine is specifies the indentation for
+ # This subroutine is specifies the indentation for
# the text AFTER the heading, i.e the body of conent
# in each \chapter, \section, etc
if( $_ =~ m/^\s*\\(.*?)(\[|{)/ and $indentAfterHeadings{$1})
@@ -880,7 +1142,7 @@ sub at_end_noindent{
# This is for blocks of code that the user wants
# to leave untouched- similar to verbatim blocks
- if( $_ =~ m/^%\s*\\end{(.*?)}/ and $noIndentBlock{$1})
+ if( $_ =~ m/^%\s*\\end{(.*?)}/ and $noIndentBlock{$1})
{
$inIndentBlock=0;
# tracing mode
@@ -888,6 +1150,8 @@ sub at_end_noindent{
}
}
+
+
sub at_beg_noindent{
# PURPOSE: This matches
# % \begin{noindent}
@@ -899,7 +1163,7 @@ sub at_beg_noindent{
# This is for blocks of code that the user wants
# to leave untouched- similar to verbatim blocks
- if( $_ =~ m/^%\s*\\begin{(.*?)}/ and $noIndentBlock{$1})
+ if( $_ =~ m/^%\s*\\begin{(.*?)}/ and $noIndentBlock{$1})
{
$inIndentBlock = 1;
# tracing mode
@@ -908,11 +1172,11 @@ sub at_beg_noindent{
}
sub start_command_or_key_unmatched_brackets{
- # PURPOSE: This matches
+ # PURPOSE: This matches
# \pgfplotstablecreatecol[...
#
- # or any other command/key that has brackets [ ]
- # split across lines specified in the
+ # or any other command/key that has brackets [ ]
+ # split across lines specified in the
# hash tables, %checkunmatchedbracket
#
# How to read: ^\s*(\\)?(.*?)(\[\s*)
@@ -924,7 +1188,7 @@ sub start_command_or_key_unmatched_brackets{
# ((?<!\\)\[\s*) match [ possibly leading with spaces
# but it WON'T match \[
- if ($_ =~ m/^\s*(\\)?(.*?)(\s*(?<!\\)\[)/
+ if ($_ =~ m/^\s*(\\)?(.*?)(\s*(?<!\\)\[)/
and (scalar($checkunmatchedbracket{$2})
or $alwaysLookforSplitBrackets)
)
@@ -957,13 +1221,13 @@ sub start_command_or_key_unmatched_brackets{
}
sub end_command_or_key_unmatched_brackets{
- # PURPOSE: Check for the closing BRACKET of a command that
+ # PURPOSE: Check for the closing BRACKET of a command that
# splits its BRACKETS across lines, such as
#
# \pgfplotstablecreatecol[ ...
#
# It works by checking if we have any entries
- # in the array @commandstorebrackets, and making
+ # in the array @commandstorebrackets, and making
# sure that we're not starting another command/key
# that has split BRACKETS (nesting).
#
@@ -971,8 +1235,8 @@ sub end_command_or_key_unmatched_brackets{
#
# We count the number of [ and ADD to the counter
# ] and SUBTRACT to the counter
- if(scalar(@commandstorebrackets)
- and !($_ =~ m/^\s*(\\)?(.*?)(\s*\[)/
+ if(scalar(@commandstorebrackets)
+ and !($_ =~ m/^\s*(\\)?(.*?)(\s*\[)/
and (scalar($checkunmatchedbracket{$2})
or $alwaysLookforSplitBrackets))
and $_ !~ m/^\s*%/
@@ -1013,7 +1277,7 @@ sub end_command_or_key_unmatched_brackets{
}
sub start_command_or_key_unmatched_braces{
- # PURPOSE: This matches
+ # PURPOSE: This matches
# \parbox{...
# \parbox[..]..{
# empty header/.style={
@@ -1021,7 +1285,7 @@ sub start_command_or_key_unmatched_braces{
# etc
#
# or any other command/key that has BRACES
- # split across lines specified in the
+ # split across lines specified in the
# hash tables, %checkunmatched, %checkunmatchedELSE
#
# How to read: ^\s*(\\)?(.*?)(\[|{|\s)
@@ -1032,8 +1296,8 @@ sub start_command_or_key_unmatched_braces{
# (.*?) non-greedy character match and store the result
# (\[|}|=|(\s*\\)) either [ or { or = or space \
- if ($_ =~ m/^\s*(\\)?(.*?)(\[|{|=|(\s*\\))/
- and (scalar($checkunmatched{$2})
+ if ($_ =~ m/^\s*(\\)?(.*?)(\[|{|=|(\s*\\))/
+ and (scalar($checkunmatched{$2})
or scalar($checkunmatchedELSE{$2})
or $alwaysLookforSplitBraces)
)
@@ -1079,7 +1343,7 @@ sub start_command_or_key_unmatched_braces{
{
# if $matchedbraces < 0 then we must be matching
# braces from a previous split-braces command
-
+
# keep matching { OR }, and don't match \{ or \}
while ($_ =~ m/(((?<!\\){)|((?<!\\)}))/g)
{
@@ -1087,6 +1351,9 @@ sub start_command_or_key_unmatched_braces{
# store the match, either { or }
my $braceType = $1;
+ # exit the loop if @commandstore is empty
+ last if(!@commandstore);
+
# get the details of the most recent command name
$commanddetails = pop(@commandstore);
$commandname = $commanddetails->{'commandname'};
@@ -1119,7 +1386,7 @@ sub start_command_or_key_unmatched_braces{
}
else
{
- # otherwise we need to put the command back for the
+ # otherwise we need to put the command back for the
# next brace count
push(@commandstore,{commandname=>$commandname,
matchedbraces=>$matchedbraces,
@@ -1132,17 +1399,17 @@ sub start_command_or_key_unmatched_braces{
}
sub end_command_or_key_unmatched_braces{
- # PURPOSE: Check for the closing BRACE of a command that
+ # PURPOSE: Check for the closing BRACE of a command that
# splits its BRACES across lines, such as
#
# \parbox{ ...
#
# or one of the tikz keys, such as
- #
+ #
# empty header/.style={
#
# It works by checking if we have any entries
- # in the array @commandstore, and making
+ # in the array @commandstore, and making
# sure that we're not starting another command/key
# that has split BRACES (nesting).
#
@@ -1150,9 +1417,9 @@ sub end_command_or_key_unmatched_braces{
#
# We count the number of { and ADD to the counter
# } and SUBTRACT to the counter
- if(scalar(@commandstore)
- and !($_ =~ m/^\s*(\\)?(.*?)(\[|{|=|(\s*\\))/
- and (scalar($checkunmatched{$2})
+ if(scalar(@commandstore)
+ and !($_ =~ m/^\s*(\\)?(.*?)(\[|{|=|(\s*\\))/
+ and (scalar($checkunmatched{$2})
or scalar($checkunmatchedELSE{$2})
or $alwaysLookforSplitBraces))
and $_ !~ m/^\s*%/
@@ -1164,6 +1431,9 @@ sub end_command_or_key_unmatched_braces{
# store the match, either { or }
my $braceType = $1;
+ # exit the loop if @commandstore is empty
+ last if(!@commandstore);
+
# get the details of the most recent command name
$commanddetails = pop(@commandstore);
$commandname = $commanddetails->{'commandname'};
@@ -1223,17 +1493,17 @@ sub check_for_else{
# else this
# }
#
- # so we need to look for the else bit, and set
+ # so we need to look for the else bit, and set
# the indentation appropriately.
#
# We only perform this check if there's something
- # in the array @commandstore, and if
- # the line itself is not a command, or comment,
+ # in the array @commandstore, and if
+ # the line itself is not a command, or comment,
# and if it begins with {
- if(scalar(@commandstore)
- and !($_ =~ m/^\s*(\\)?(.*?)(\[|{|=)/
- and (scalar($checkunmatched{$2})
+ if(scalar(@commandstore)
+ and !($_ =~ m/^\s*(\\)?(.*?)(\[|{|=)/
+ and (scalar($checkunmatched{$2})
or scalar($checkunmatchedELSE{$2})
or $alwaysLookforSplitBraces))
and $_ =~ m/^\s*{/
@@ -1272,11 +1542,11 @@ sub at_beg_of_env_or_eq{
# \[
#
# It also checks to see if the current environment
- # should have alignment delimiters; if so, we need to turn
- # ON the $delimiter switch
+ # should have alignment delimiters; if so, we need to turn
+ # ON the $delimiter switch
# How to read
- # m/^\s*(\$)?\\begin{(.*?)}/
+ # m/^\s*(\$)?\\begin{(.*?)}/
#
# ^ beginning of a line
# \s* any white spaces (possibly none)
@@ -1289,16 +1559,19 @@ sub at_beg_of_env_or_eq{
# ^ beginning of a line
# \s* any white spaces (possibly none)
# () empty just so that $1 and $2 are defined
- # (\\\[) \[ there are lots of \ because both \ and [ need escaping
+ # (\\\[) \[ there are lots of \ because both \ and [ need escaping
+ # \\begin{\\?(.*?)} \begin{something} where something could start
+ # with a backslash, e.g \my@env@ which can happen
+ # in a style or class file, for example
- if( ( ( $_ =~ m/^\s*(\$)?\\begin{(.*?)}/ and $_ !~ m/\\end{$2}/)
+ if( ( ( $_ =~ m/^\s*(\$)?\\begin{\\?(.*?)}/ and $_ !~ m/\\end{$2}/)
or ($_=~ m/^\s*()(\\\[)/ and $_ !~ m/\\\]/) )
and $_ !~ m/^\s*%/ )
{
# tracing mode
print $logfile "Line $lineCounter\t \\begin{environment} found: $2 \n" if($tracingMode);
- # increase the indentation
+ # increase the indentation
&increase_indent($2);
# check for verbatim-like environments
@@ -1345,12 +1618,14 @@ sub at_end_of_env_or_eq{
# or
# \]
#
+ # Note: environmentname can begin with a backslash
+ # which might happen in a sty or cls file.
+ #
# It also checks to see if the current environment
- # had alignment delimiters; if so, we need to turn
- # OFF the $delimiter switch
-
- if( ($_ =~ m/^\s*\\end{(.*?)}/ or $_=~ m/^(\\\])/)
- and $_ !~ m/\s*^%/)
+ # had alignment delimiters; if so, we need to turn
+ # OFF the $delimiter switch
+
+ if( ($_ =~ m/^\s*\\end{\\?(.*?)}/ or $_=~ m/^(\\\])/) and $_ !~ m/\s*^%/)
{
# check if we're at the end of a verbatim-like environment
@@ -1361,79 +1636,110 @@ sub at_end_of_env_or_eq{
print $logfile "Line $lineCounter\t \\end{verbatim-like} found: $1, switching off verbatim \n" if($tracingMode);
print $logfile "Line $lineCounter\t removing leading spaces \n" if($tracingMode);
- #s/^\ *//;
- s/^\t+// if($_ ne "");
- s/^\s+// if($_ ne "");
+ #s/^\ *//;
+ s/^\t+// if($_ ne "");
+ s/^\s+// if($_ ne "");
}
- # check to see if \end{environment} fits with most recent \begin{...}
- my $previousEnvironment = pop(@environmentStack);
-
- # check to see if we need to turn off alignment
- # delimiters and output the current block
- if($lookForAlignDelims{$1} and ($previousEnvironment eq $1))
+ # check if we're in an environment that is looking
+ # to indent after each \item
+ if(scalar(@indentNames) and $itemNames{$indentNames[-1]})
{
- $delimiters=0;
+ &decrease_indent($indentNames[-1]);
+ }
- # tracing mode
- print $logfile "Line $lineCounter\t Delimiter body FINISHED: $1\n" if($tracingMode);
+ # some commands contain \end{environmentname}, which
+ # can cause a problem if \begin{environmentname} was not
+ # started previously; if @environmentStack is empty,
+ # then we don't need to check for \end{environmentname}
+ if(@environmentStack)
+ {
+ # check to see if \end{environment} fits with most recent \begin{...}
+ my $previousEnvironment = pop(@environmentStack);
- # print the current FORMATTED block
- @block = &format_block(@block);
- foreach $line (@block)
- {
- # add the indentation and add the
- # each line of the formatted block
- # to the output
- # unless this would only create trailing whitespace and the
- # corresponding option is set
- unless ($line =~ m/^$/ and $removeTrailingWhitespace)
- {
- $line = join("",@indent).$line;
- }
- push(@lines,$line);
- }
- # empty the @block, very important!
- @block=();
- }
+ # check to see if we need to turn off alignment
+ # delimiters and output the current block
+ if($lookForAlignDelims{$1} and ($previousEnvironment eq $1))
+ {
+ &print_aligned_block();
+ }
- # tracing mode
- print $logfile "Line $lineCounter\t \\end{envrionment} found: $1 \n" if($tracingMode and !$verbatimEnvironments{$1});
+ # tracing mode
+ print $logfile "Line $lineCounter\t \\end{envrionment} found: $1 \n" if($tracingMode and !$verbatimEnvironments{$1});
- # check to see if \end{environment} fits with most recent \begin{...}
- if($previousEnvironment eq $1)
- {
- # decrease the indentation (if appropriate)
- &decrease_indent($1);
- }
- else
- {
- # otherwise put the environment name back on the stack
- push(@environmentStack,$previousEnvironment);
- print $logfile "Line $lineCounter\t WARNING: \\end{$1} found on its own line, not matched to \\begin{$previousEnvironment}\n" unless ($delimiters or $inverbatim or $inIndentBlock or $1 eq "\\\]");
- }
+ # check to see if \end{environment} fits with most recent \begin{...}
+ if($previousEnvironment eq $1)
+ {
+ # decrease the indentation (if appropriate)
+ &decrease_indent($1);
+ }
+ else
+ {
+ # otherwise put the environment name back on the stack
+ push(@environmentStack,$previousEnvironment);
+ print $logfile "Line $lineCounter\t WARNING: \\end{$1} found on its own line, not matched to \\begin{$previousEnvironment}\n" unless ($delimiters or $inverbatim or $inIndentBlock or $1 eq "\\\]");
+ }
- # need a special check for \[ and \]
- if($1 eq "\\\]")
- {
- &decrease_indent($1);
- pop(@environmentStack);
+ # need a special check for \[ and \]
+ if($1 eq "\\\]")
+ {
+ &decrease_indent($1);
+ pop(@environmentStack);
+ }
}
- # if we're at the end of the document, we remove all current
- # indentation- this is especially prominent in examples that
+ # if we're at the end of the document, we remove all current
+ # indentation- this is especially prominent in examples that
# have headings, and the user has chosen to indentAfterHeadings
if($1 eq "document" and !(grep(/filecontents/, @indentNames)) and !$inpreamble and !$delimiters and !$inverbatim and !$inIndentBlock)
{
- @indent=();
- @indentNames=();
- print $logfile "Line $lineCounter\t \\end{$1} found- emptying indent array \n" unless ($delimiters or $inverbatim or $inIndentBlock or $1 eq "\\\]");
+ @indent=();
+ @indentNames=();
+
+ # tracing mode
+ if($tracingMode)
+ {
+ print $logfile "Line $lineCounter\t \\end{$1} found- emptying indent array \n" unless ($delimiters or $inverbatim or $inIndentBlock or $1 eq "\\\]");
+ }
}
}
}
+sub print_aligned_block{
+ # PURPOSE: this subroutine does a few things related
+ # to printing blocks of code that contain
+ # delimiters, such as align, tabular, etc
+ #
+ # It does the following
+ # - turns off delimiters switch
+ # - processes the block
+ # - deletes the block
+ $delimiters=0;
+
+ # tracing mode
+ print $logfile "Line $lineCounter\t Delimiter body FINISHED: $1\n" if($tracingMode);
+
+ # print the current FORMATTED block
+ @block = &format_block(@block);
+ foreach $line (@block)
+ {
+ # add the indentation and add the
+ # each line of the formatted block
+ # to the output
+ # unless this would only create trailing whitespace and the
+ # corresponding option is set
+ unless ($line =~ m/^$/ and $removeTrailingWhitespace)
+ {
+ $line = join("",@indent).$line;
+ }
+ push(@lines,$line);
+ }
+ # empty the @block, very important!
+ @block=();
+}
+
sub format_block{
- # PURPOSE: Format a delimited environment such as the
+ # PURPOSE: Format a delimited environment such as the
# tabular or align environment that contains &
#
# INPUT: @block array containing unformatted block
@@ -1448,7 +1754,7 @@ sub format_block{
print $logfile "\t\tFormatting alignment block\n" if($tracingMode);
# step the line counter back to the beginning of the block-
- # it will be increased back to the end of the block in the
+ # it will be increased back to the end of the block in the
# loop later on: foreach $row (@tmpblock)
$lineCounter -= scalar(@block);
@@ -1479,7 +1785,7 @@ sub format_block{
my %stringsize=();
# loop through the block and count & per line- store the biggest
- # NOTE: this needs to be done in its own block so that
+ # NOTE: this needs to be done in its own block so that
# we can know what the maximum number of & in the block is
foreach $row (@block)
{
@@ -1512,7 +1818,7 @@ sub format_block{
# tracing mode
print $logfile "\t\tmaximum number of & in any row: $maxNumberAmpersands\n" if($tracingMode);
-
+
# loop through the lines in the @block
foreach $row (@block)
{
@@ -1560,11 +1866,11 @@ sub format_block{
{
# increment column counter
$aligncolcounter++;
-
+
# remove leading and trailing space from element
$column =~ s/^\s+//;
$column =~ s/\s+$//;
-
+
# assign string size to the array
$stringsize{$alignrowcounter.$aligncolcounter}=length($column);
if(length($column)==0)
@@ -1583,7 +1889,7 @@ sub format_block{
}
}
-
+
# put $linebreak back on the string, now that
# the measurement has been done
$tmpstring .= $linebreak;
@@ -1598,7 +1904,7 @@ sub format_block{
}
else
{
- # if there are no & then use the
+ # if there are no & then use the
# NOFORMATTING token
# remove leading space
s/^\s+//;
@@ -1632,16 +1938,16 @@ sub format_block{
# the numbers represent how wide each column is
# the s represents string
# the & needs to be inserted
-
+
# join up the maximum string lengths using "s %-"
$fmtstring = join("s & %-",@maxmstringsize);
-
+
# add an s to the end, and a newline
$fmtstring .= "s ";
-
+
# add %- to the beginning
$fmtstring = "%-".$fmtstring;
-
+
# process the @tmpblock of aligned material
foreach $row (@tmpblock)
{
@@ -1689,7 +1995,7 @@ sub format_block{
# remove trailing whitespace
if ($removeTrailingWhitespace)
{
- print $logfile "Line $lineCounter\t removing trailing whitespace from delimiter aligned line\n" if ($tracingMode);
+ print $logfile "\t\tLine $lineCounter\t removing trailing whitespace from delimiter aligned line\n" if ($tracingMode);
$tmpstring =~ s/\s+$/\n/;
}
@@ -1711,7 +2017,7 @@ sub increase_indent{
my $command = pop(@_);
- # if the user has specified $indentRules{$command} and
+ # if the user has specified $indentRules{$command} and
# $noAdditionalIndent{$command} then they are a bit confused-
# we remove the $indentRules{$command} and assume that they
# want $noAdditionalIndent{$command}
@@ -1726,7 +2032,7 @@ sub increase_indent{
delete $indentRules{$command};
}
- # if the command is in verbatimEnvironments and in indentRules then
+ # if the command is in verbatimEnvironments and in indentRules then
# remove it from %indentRules hash
# to avoid any further confusion
if($indentRules{$command} and $verbatimEnvironments{$command})
@@ -1745,7 +2051,7 @@ sub increase_indent{
push(@indent, $indentRules{$command});
# tracing mode
print $logfile "Line $lineCounter\t increasing indent using rule for $command (see indentRules)\n" if($tracingMode);
- push(@indentNames,"$command (rule)");
+ push(@indentNames,"$command");
}
else
{
@@ -1753,7 +2059,7 @@ sub increase_indent{
if(!($noAdditionalIndent{$command} or $verbatimEnvironments{$command} or $inverbatim))
{
push(@indent, $defaultIndent);
- push(@indentNames,"$command (default)");
+ push(@indentNames,"$command");
# tracing mode
print $logfile "Line $lineCounter\t increasing indent using defaultIndent\n" if($tracingMode);
}
@@ -1775,10 +2081,21 @@ sub decrease_indent{
if(!($noAdditionalIndent{$command} or $verbatimEnvironments{$command} or $inverbatim))
{
+ print $logfile "Line $lineCounter\t removing ", $indentNames[-1], " from indentNames\n" if($tracingMode);
pop(@indent);
pop(@indentNames);
# tracing mode
- print $logfile "Line $lineCounter\t decreasing indent to: ",join(", ",@indentNames),"\n" if($tracingMode);
+ if($tracingMode)
+ {
+ if(@indentNames)
+ {
+ print $logfile "Line $lineCounter\t decreasing indent to: ",join(", ",@indentNames),"\n" ;
+ }
+ else
+ {
+ print $logfile "Line $lineCounter\t indent now empty \n";
+ }
+ }
}
}