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
}