On Mar 31, 2006, at 11:54 AM, Scott Haneda wrote:
Second issue, is that in most cases, my variable names are not
always the
same length, but I like to line them up on the "=" sign:
Test = "test";
Thistest = "test";
Is there any way to automatically do this as well?
I have a little perl script called alignEquals.pl that handles this
in my UNIX Filters. I've assigned it to cmd-shift-equals. Just
highlight the section of code you want aligned and hit cmd-shift-
equals and it will replace the existing block with the newly aligned
block. It's not foolproof, but pretty handy.
#!/usr/bin/perl
#-----------------------------------------------------------------------
--------
# Copyright (c) 2005, Custom Visuals, LLC. All rights reserved.
#-----------------------------------------------------------------------
--------
# Author: Mike Schienle
# $RCSfile: alignEquals.pl,v $
# $Revision: 1.1 $
# Orig: 2005/12/11 21:24:06
# $Date: 2006/03/31 20:36:07 $
#-----------------------------------------------------------------------
--------
# Purpose: Process a file with a basic format and align all equals signs
# History:
#-----------------------------------------------------------------------
--------
# $Log: alignEquals.pl,v $
# Revision 1.1 2006/03/31 20:36:07 mgs
# Initial entry
#
#-----------------------------------------------------------------------
--------
use strict;
use warnings;
# the format is expected to be like this
# $val = 10;
# @list = qw/1 2 3 4/;
# @$arrayRef = $data;
# it will align the lines like this
# $val = 10;
# @list = qw/1 2 3 4/;
# @$arrayRef = $data;
my ($file, $line, @data, @outData, $maxLen, $lead, @posArr, $lPos);
$file = shift;
$maxLen = 0;
open(FILE, "< $file")
or die "Unable to open $file: $!";
# loop through the file, building an output array
while (<FILE>) {
$line = $_;
chomp $line;
# allow empty lines to be included in selection
if ((length($line) > 0) && ($line =~ "=")) {
# handle leading whitespace - uses first = line only
unless (defined $lPos) {
push @posArr, pos $line, "\n" while ($line =~ m/\S/g);
$lead = substr($line, 0, $posArr[0] - 1);
}
$line = trim($line);
# split on equal sign
@data = split("=", $line);
# get the max length of the first column for later
alignment
$data[0] = trim($data[0]);
$maxLen = length($data[0]) if (length($data[0]) > $maxLen);
push @outData, join "=", @data;
}
else {
push @outData, trim($line);
}
}
close(FILE);
$lead = '' unless defined $lead;
# add a space to maxLen
$maxLen++;
# loop through the array so we can align first column
foreach (@outData) {
@data = split "=", $_;
# will be undefined if length of element is 0
$data[0] = sprintf "%-${maxLen}s", $data[0] if defined $data[0];
print $lead, join "=", @data;
print "\n";
}
sub trim {
my @out = @_;
for (@out) {
s/^\s+//;
s/\s+$//;
}
return wantarray ? @out : $out[0];
}
Mike Schienle
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <[EMAIL PROTECTED]>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to: <[EMAIL PROTECTED]>