Sign in to follow this  
Followers 0
Goutetsu

Perl Script To Tab Autoit Scripts

3 posts in this topic

I started using AutoIt a few days ago. Well, I was using Exe2Aut to look at other

people's scripts, and I came across a few who removed the tabs to make their files

smaller. Obviously, this made the scripts hard to read and I didn't want to go through

and tab them all so I wrote this perl script to do it for me, as it is oh so good at

handling text. I don't know if anyone else will have any use for this, but you never

know. I have only tested this in Linux, but it should work fine in windows and other

OSes.

At the moment it works like this:

perl autoittab.pl <autoitscript>

The tabbed code is printed to the screen so I suggest you redirect to another file:

perl autoittab.pl <untabbed script> > <new tabbed script>

Note: These commands assume you are using some sort of perl for windows

from the command line. Though the commands will also work in *nix type OSes.

#!/usr/bin/perl
# Make sure we use somewhat proper code.
use strict;
#$numtabs holds our current "tab level"
my $numtabs = 0; 

#Read all files listed on the command line
#line by line.
while(<>)
{
  #Remove all tabs at the beginning of the line
  s/^\t*//;
  
  #If the line begins with if, elseif, else, or endif (case insensitive)
  if(/^(?i:if|elseif|else|endif)\b/)
  { #handle the if structure
    handle_ifs($_); 
  }elsif(/^(?i:do|until)\b/) #same as above but with do/until loops
  {
    handle_dos($_); 
  }elsif(/^(?i:for|next)\b/)
  {
    handle_fors($_); 
  }elsif(/^(?i:while|wend)\b/)
  {
    handle_whiles($_); 
  }elsif(/^(?i:func|endfunc)\b/)
  {
    handle_funcs($_); 
  }elsif(/^(?i:select|case|endselect)\b/)
  {
    handle_selects($_); 
  }else
  { #Otherwise, we just have a line of code
    print(("\t" x $numtabs) . $_);
  }
}  

#"\t" x $numtabs prints $numbtabs tabs
sub handle_selects
{
  if(/^select\b/i)
  {
    print(("\t" x $numtabs) . $_);
    $numtabs += 2;
  }
  if(/^case\b/i)
  {
    $numtabs--;
    print(("\t" x $numtabs) . $_);
    $numtabs++;
  }
  if(/^endselect\b/i)
  {
    $numtabs -= 2;
    print(("\t" x $numtabs) . $_);
  }
}
  

sub handle_ifs
{
  if(/^if\b/i)
  {
    print(("\t" x $numtabs) . $_);
    unless(/^If\s+(.*?)\s+Then\s+([\$\w]+.*)/i)
    {
      $numtabs++;
    }
  }
  if(/^elseif\b/i)
  {
    $numtabs--;
    print(("\t" x $numtabs) . $_);
    $numtabs++;
  }
  if(/^else\b/i)
  {
    $numtabs--;
    print(("\t" x $numtabs) . $_);
    $numtabs++;
  }
  if(/^endif\b/i)
  {
    $numtabs--;
    print(("\t" x $numtabs) . $_);
  }
}

sub handle_dos
{  
  print(("\t" x $numtabs++) . $_) if(/^do\b/i);
  print(("\t" x --$numtabs) . $_)  if(/^until\b/i);
}
sub handle_fors
{
  print(("\t" x $numtabs++) . $_) if(/^for\b/i);
  print(("\t" x --$numtabs) . $_)  if(/^next\b/i);
}

sub handle_whiles
{
  print(("\t" x $numtabs++) . $_) if(/^while\b/i);
  print(("\t" x --$numtabs) . $_)  if(/^wend\b/i);
}

sub handle_funcs
{
  print(("\t" x $numtabs++) . $_) if(/^func\b/i);
  print(("\t" x --$numtabs) . $_)  if(/^endfunc\b/i);
}

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Have a look at Tidy before you put alot of time in it :D

Autoit3 Tidy: http://www.hiddensoft.com/forum/index.php?...indpost&p=12782

-or-

Scite Editor + Tidy Single setup: http://www.hiddensoft.com/fileman/users/jdeb/setupscite.exe

Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Tidy looks nice. I usually code stuff myself if I can, so I didn't search for anything to format scripts for me. Oh well, maybe somewill make use of the code elsewhere. :D

Edited by Goutetsu

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0