mSL Code Tidier

By Conscious on Jul 11, 2012

Basically, a dialog to try and clean up code, removing "untidy-looking" practises such as multiple close brackets on a line, using the pipe for 2 lines on one line, etc.
I made this on a whim, and really haven't done extensive testing. I am sure that there will be bugs, and things I've missed that should be added. Please tell me these! I will follow up on all comments and adjust the script accordingly:

Note: I was indenting the code with tabs, however I found that this caused mIRC to go crazy in the script editor, and indenting with double spaces did not work (it just remained un-indented). If anyone has a solution to this let me know.

/beautify to start, or right click in channel/pm/status windows.

Use at your own risk. Always keep a backup copy, at least until this script is tested more extensively.

alias beautify dialog -m code_table code_table
menu query,channel,status {
  Beautify:beautify
}
dialog code_table {
  title "mIRC Code Tidier"
  size -1 -1 296 262
  option dbu
  edit "", 1, 1 2 295 247, multi return autohs autovs hsbar vsbar
  button "Clean!", 2, 130 249 37 12
}
on *:DIALOG:code_table:sclick:2:{
  var %x 1, %ticks $ticks
  savebuf -o code_table 1 %ticks $+ .txt
  if ($lines(%ticks $+ .txt) > 0) {
    beautify_file %ticks $+ .txt
    did -r code_table 1
    loadbuf -o code_table 1 %ticks $+ .txt
  }
  .remove %ticks $+ .txt
}
alias beautify_file {
  var %f $1,%w @ $+ %f
  window -hn %w
  loadbuf %w %f
  var %l 1,%commented $false
  while (%l <= $line(%w,0)) {
    var %t $regsubex(tab,$line(%w,%l),/^(\s+)(\S.+)$/g,\2)
    if ($regex($line(%w,%l),/^(\s+)(\S)/g)) rline %w %l %t
    if (!$regex($line(%w,%l),/\S/)) dline %w %l
    elseif ($regex($line(%w,%l),/^\/\*/)) || (%commented) {
      var %commented $true
      inc %l
    }
    elseif ($regex($line(%w,%l),/^\73/)) inc %l
    elseif ($regex($line(%w,%l),/^\*\//)) && (%commented) {
      var %commented $false
      inc %l
    }
    elseif ($regex(%t,/^(.+?)\s\174\s(.+?)$/)) {
      rline %w %l $regml(1)
      iline %w $calc(%l + 1) $regml(2)
    }
    elseif ($regex(%t,/^((?:.+?)(?::|\s))\173(\s+?\S(?:.+)?)$/)) {
      rline %w %l $regml(1) $+ $chr(123)
      iline %w $calc(%l + 1) $regml(2)
    }
    elseif ($regex(%t,/^on ([^:]+)\72((?:[A-Z]+)?(?:[a-z]+)(?:[^:]+)?)\72(.+)$/)) rline %w %l on $regml(1) $+ : $+ $upper($regml(2)) $+ : $+ $regml(3)
    elseif ($regex(%t,/^(.+?)\s\175(.+)?$/)) {
      if ($regex(q,$regml(1),/\S/)) {
        rline %w %l $regml(1)
        iline %w $calc(%l + 1) $chr(125) $regml(2)
      }
      else {
        iline %w $calc(%l + 1) $chr(125) $regml(2)
        dline %w %l
      }
    }
    elseif ($regex(%t,/^on (.+)\72\s+?\173/)) rline %w %l on $regml(1) $+ : $+ $chr(123)
    else inc %l
  }
  var %l 1,%tab 0,%com 0,%comment false
  while (%l <= $line(%w,0)) {
    var %temp $false
    if ($regex($line(%w,%l),/^\/\*)) {
      var %comment $true,%temp $true
      rline %w %l $str($chr(9),%tab) $+ $line(%w,%l)
    }
    elseif ($regex($line(%w,%l),/^\*\//)) {
      var %comment $false,%temp $true
      rline %w %l $str($chr(9),%tab) $+ $line(%w,%l)
    }
    elseif ($regex($line(%w,%l),/^\73/)) rline %w %l $str($chr(32),%tab) $+ $line(%w,%l)
    else {
      if ($regex($line(%w,%l),/\175$/)) {
        ;dec %tab
        var %temp $true
        if (%com == 1) && (menu* !iswm $line(%w,$calc(%l - 2))) {
          rline %w $calc(%l - 2) $remove($regsubex($line(%w,$calc(%l - 2)),/^(.+)\173$/,\1 $line(%w,$calc(%l - 1))),$chr(9))
          dline %w $calc(%l - 1)
          dline %w $calc(%l - 1)
          dec %l 2
          var %com 0
        }
      }
      rline %w %l $str($chr(9),%tab) $+ $line(%w,%l)
      if ($regex($line(%w,%l),/\173$/)) {
        ;inc %tab
        var %com 0
        var %temp $true
      }
    }
    inc %l
    if (!%temp) inc %com
  }
  write -c %f
  savebuf %w %f
  window -c %w
}

Comments

Sign in to comment.
thegingon   -  Jul 22, 2012

that destroy my script , thx to got i have the script in hawkee

 Respond  
Conscious   -  Jul 20, 2012

Also changed the script. For some reason it was missing several lines o.o

 Respond  
Conscious   -  Jul 20, 2012

@blackvenomm666 What script were you plugging into it?

 Respond  
blackvenomm666   -  Jul 20, 2012

kept getting /rline: insufficient parameters (line 50, script42)

 Respond  
blackvenomm666   -  Jul 20, 2012

i just tested it and it didn't actually do anything to my snippet

 Respond  
Conscious   -  Jul 20, 2012
 Respond  
blackvenomm666   -  Jul 17, 2012

i haven't tested it maybe later after i get some sleep but seems decent enough. nice idea for sure

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.