[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Plugins
. . -> [Subject]  MUSH softcode Formatter Plugin
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?
(New message)
Subject: MUSH softcode Formatter Plugin
Your forum user name.
Register forum user name
Your forum password.
Forgotten password?
Message to be posted (in English, please)
Maximum of 6000 characters. Text only please, no HTML.
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...

Subject review (reverse sequence)

Posted by Krenath   USA  (74 posts)  [Biography] bio
Date Thu 01 May 2003 02:17 PM (UTC)  quote  ]

Amended on Thu 01 May 2003 02:18 PM (UTC) by Krenath

If anyone's wondering about the way the code works, all it does is make multiple passes over the string 'tokenizing' as it goes.

On each pass, it looks for the innermost functions, replaces the commas, parenthesis, braces and brackets with 'placeholders' so it can later remember where all these things came from. By replacing all these characters out, it can, on the next pass, concentrate on the next-innermost function, easily locating it because the current innermost commas, parenthesis, braces, and brackets are guaranteed to be related to each other.

In some cases, while replacing punctuation with 'tokens', indent and outdent codes are inserted.

Once the string has been combed over and no more commas, braces, or brackets can be found within the string, a second subroutine is used to change all the tokens back to their original characters, then makes a pass through the string to format the string according to the embedded indent/outdent/carriage return codes.

The resulting string is then appended to the notepad window, followed by a dash on a line by itself.

Since the code appends to the notepad, one can actualy type 'ex <object>/*' and a mush like PennMUSH will @decompile the entire object, sending each line to the Notepad window in a formatted form.

As the script is not compiled and the source is provided, if your formatting preferences differ from mine (or your regular expression pattern skills are greater) you can freely modify the included code to format as you like it.

I'm still trying to figure out how to write a regular expression pattern that matches a function with any number of parameters (such as switch()). I'd prefer to have switch() formatted as


...but I'm still working on that. :)

- Krenath from
bDv TrekMUSH
[Go to top] top

Posted by Nick Gammon   Australia  (19,666 posts)  [Biography] bio   Forum Administrator
Date Wed 30 Apr 2003 11:18 PM (UTC)  quote  ]

Amended on Wed 30 Apr 2003 11:21 PM (UTC) by Nick Gammon

Sorry, think I corrupted it - can you repost in one piece and after doing the quote forum codes?

Update - think I might have fixed it, it seems to load OK now.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (19,666 posts)  [Biography] bio   Forum Administrator
Date Wed 30 Apr 2003 11:14 PM (UTC)  quote  ]
Krenath, I have fixed it so you can do unlimited length posts, and amended your original plugin into one piece.

I suggest for pasting code you use the Notepad "quote forum codes" function to "escape" out backslashes and square brackets, or you might find things like [b] becoming bold text, and not executing correctly.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Neva   USA  (117 posts)  [Biography] bio
Date Wed 30 Apr 2003 10:44 PM (UTC)  quote  ]
Have you suggested that to Greg? :) I only brought it up because I figured it'd be easier than trying to program the same stuff all over again--if you're doing it differently, that's obviously not the same story, but it was just an idea.
[Go to top] top

Posted by Krenath   USA  (74 posts)  [Biography] bio
Date Wed 30 Apr 2003 10:11 PM (UTC)  quote  ]
Well, with MUSH Formatter being a compiled program (and therefore unmodifiable by me) and with precious few options for exactly how individual functions would be formatted, I decided a scripted version would be more in line with what I wanted. Sure, I dont have a nice interface that lets you easily say "nest and indent all parameters for the if() function" but it still makes it possible.

MUSH Formatter would be a much more convenient program if you could configure each of the recognized functions in its Functions menu and instruct the Formatter whether you want it all on one line or nested and indented.

- Krenath from
bDv TrekMUSH
[Go to top] top

Posted by Neva   USA  (117 posts)  [Biography] bio
Date Wed 30 Apr 2003 06:19 PM (UTC)  quote  ]
I don't know much about this kind of thing, granted, but might you get better formatting easier if you used something like Greg Kempe's formatting program? It's supposed to have some way of interfacing with stuff like this. (URL: http://mform.kempe.net/ )
[Go to top] top

Posted by Krenath   USA  (74 posts)  [Biography] bio
Date Wed 30 Apr 2003 04:22 PM (UTC)  quote  ]

Amended on Fri 02 May 2003 12:07 AM (UTC) by Krenath


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<!-- Saved on Tuesday, April 29, 2003, 11:01 AM -->
<!-- MuClient version 3.41 -->

<!-- Plugin "MUSHCodeFormatter" generated by Plugin Wizard -->

   purpose="Grabs attributes and formats them in the Notepad window"
   date_written="2003-04-29 10:56:22"
<description trim="y">
This plugin will create appropriate aliases and triggers so that examining an attribute in the world will open/append to a notepad window and display formatted softcode instead of one giant chunk.

The formatting isn't the greatest, but is still being worked on.  It's better than a giant lump of code, however

Written by Krenath


<!--  Get our standard constants -->

<include name="constants.vbs"/>

<!--  Triggers  -->

   match="FugueEdit &gt; *"

<!--  Aliases  -->

   match="ex */*"
  <send>@dec/tf %1/%2</send>

<!--  Script  -->


Sub OnEdit(a,b,c)
        world.AppendToNotepad "Code Editor (Ctrl-Shift-S to send to world)",ufmt(fmt(c(1)))
End Sub

Function fmt(String)
Dim Changed,OldString
Changed = False
'tokens for comma, parenthesis, bracket, brace &cm &lp &rp &lb %rb &lc &rc
'tokens for indent & outdent &id &od
'token for cr keeps current intentlevel in mind &cr
        String = replace(String,"/","&fs")
        String = replace(String,"\","&bs")
                Changed = False
                OldString = String      
                ' Replace leftover wrapping chars with tokens
                String = regreplace(String , "^([-_.A-Z@a-z&0-9*]*) (#[0-9]*)=(.*)" , "$1 $2&eq&cr$3" , "attrb")
                String = regreplace(string , pFix("\(<param>\)") , "&lp$1&rp" , "paren") 
                String = regreplace(string , pFix("\[<param>\]") , "&lb$1&rb" , "brack") 
                String = regreplace(string , pFix("\{<param>\}") , "&lc$1&rc" , "brace") 
                String = replace(string,"] [","]%b&cr[") 
                String = replace(string,"]%b[","]%b&cr[") 
                String = replace(string,"][","]&cr[") 
                string = replace(string,"%b","&sp")
                String = replace(string,"%r","&rr&cr")

                'handle If/else indentation
                String = regreplace(string,pFix("ifelse\(<param>,<param>,<param>\)"),"if(&tb$1&cm&id&cr$2&od&cr&cm&id&cr$3&od&cr)","if1  ") 
                String = regreplace(string,pFix("if\(<param>,<param>,<param>\)"),"if(&tb$1&cm&id&cr$2&od&cr&cm&id&cr$3&od&cr)","if2  ") 
                String = regreplace(string,pFix("if\(<param>,<param>\)"),"if(&tb$1&cm&id&cr$2&od&cr)","if3  ") 

                'handle iter
                String = regreplace(string,pFix("iter\(<param>,<param>,<param>,<param>\)"),"iter(&tb$1&cm&id&cr$2&od&cr&cm&id&cr$3&od&cr&cm&id&cr$4&od&cr)","iter4") 
                String = regreplace(string,pFix("iter\(<param>,<param>,<param>\)"),"iter(&tb$1&cm&id&cr$2&od&cr&cm&id&cr$3&od&cr)","iter3") 
                String = regreplace(string,pFix("iter\(<param>,<param>\)"),"iter(&tb$1&cm&id&cr$2&od&cr)","iter2") 
                'handle switches that should have been ifs
                String = regreplace(string,pFix("switch\(<param>,1,<param>,<param2>\)"),"if(&tb$1&cm&id&cr$3&od&cr&cm&id&cr$4&od&cr)","swif2")
                String = regreplace(string,pFix("switch\(<param>,1,<param>\)"),"if(&tb$1&cm&id&cr$3&od&cr)","swif1")
                'handle switches
                String = regreplace(string,pFix("switch\(<param>,<param>,<param>,<param>,<param>\)"),"switch(&tb$1&cm&id&cr$2&cm$3&cr&cm$4&cm$5&od&cr)","swit5 ") 
                String = regreplace(string,pFix("switch\(<param>,<param>,<param>,<param>\)"),"switch(&tb$1&cm&id&cr$2&cm$3&cr&cm$4&od&cr)","swit4") 
                String = regreplace(string,pFix("switch\(<param>,<param>,<param>\)"),"switch(&tb$1&id&cr&cm$2&cm$3&od&cr)","swit3") 

                'Replace commas out of innermost functions.
                String = regreplace(string,pfix("\(<param>,<param>,<param>,<param>\)"),"($1&cm$2&cm$3&cm$4)","4prm,") 
                String = regreplace(string,pfix("\(<param>,<param>,<param>\)"),"($1&cm$2&cm$3)","3prm,") 
                String = regreplace(string,pfix("\(<param>,<param>\)"),"($1&cm$2)","2prm,") 
                If String <> OldString then Changed=true
        Loop While Changed

        fmt = String
End Function

Function ufmt(string)
        Dim x,indentlvl,Step,xi,xo,xc
        string = replace(string,"&tb",vbTab)
        string = replace(string,"&bs","\")
        string = replace(string,"&fs","/")
        string = replace(string,"&eq","=")
        string = replace(string,"&lp","(")
        string = replace(string,"&rp",")")
        string = replace(string,"&lb","[")
        string = replace(string,"&rb","]")
        string = replace(string,"&lc","{")
        string = replace(string,"&rc","}")
        string = replace(string,"&cm",",")
        string = replace(string,"&rr","%r")
        string = replace(string,"&sp","%b")

        While ( instr(string,"&id")>0 or instr(string,"&od")>0 or instr(string,"&cr")>0 ) and X<100
                xi = instr(string,"&id")
                xo = instr(string,"&od")
                xc = instr(string,"&cr")
                IF xi > 0 and (xo=0 or xo>xi) and (xc=0 or xc>xi) then
                        'world.note "indent"
                        indentlvl = indentlvl + 1
                        string = replace(string,"&id","",1,1) 'perform one replace only
                ElseIF xo > 0 and (xi=0 or xi>xo) and (xc=0 or xc>xo) then
                        'world.note "outdent"
                        indentlvl = indentlvl -1
                        string = replace(string,"&od","",1,1) 'perform one replace only
                ElseIF xc > 0 and (xi=0 or xi>xc) and (xo=0 or xo>xc) then
                        'world.note "c/r"
                        string = replace(string,"&cr",vbcrlf & tab(indentlvl),1,1) 'perform one replace only
                End IF

        ufmt = string & vbcrlf & vbcrlf & "-" & vbcrlf & vbcrlf
End Function

Function pFix(String)
        pFix = replace(string,"<,param>","([^][{}()]*)")
        pFix = replace(string,"<param>","([^][{}(),]*)")
End Function

Function RegReplace(sText,sOldPattern,sNewPattern,comment)
'Follows same syntax as normal Replace, but allows for some really impressive stunts.
        Dim RegEx
        Set RegEx                       = new RegExp
        RegEx.Pattern           = sOldPattern
        RegEx.IgnoreCase        = True
        RegEx.Global            = True
        RegReplace                      = RegEx.Replace(sText,sNewPattern)
        Set RegEx                       = Nothing
        'if RegReplace <> stext then 
                'world.colourtell "red","black",comment
                'World.note " > " & RegReplace
        'end if
End Function

Function Tab(qty)
        Dim i,temp
        for i = 1 to qty
                temp = temp & vbTab
        tab = temp
End Function


<!--  Plugin help  -->


Sub OnHelp (sName, sLine, wildcards)
  world.Note world.GetPluginInfo (world.GetPluginID, 3)
End Sub


- Krenath from
bDv TrekMUSH
[Go to top] top

Posted by Krenath   USA  (74 posts)  [Biography] bio
Date Wed 30 Apr 2003 04:17 PM (UTC)  quote  ]

Amended on Wed 30 Apr 2003 04:23 PM (UTC) by Krenath

Thie following plugin tries to accomplish some rudimentary formatting of an attribute grabbed from a MUSH.

It included an alias for ex */* and a trigger on FugueEdit > * like the 'grab' plugin does. This differes from the 'grab' plugin in the following ways:

1. Opens a new notepad window or appends to the window if already open.

2. Outputs formatted MUSH softcode to the notepad with each line separated by a dash on a line by itself.

Already built into MUSHclient's notepad is a convenient feature for sending the formatted softcode back to the MUSH. But you'll first want to configure "Paste To World" to handle Formatted Softcode and probably ensure that "confirm on each paste to world" is disabled. You may or may not want to echo the pasted code to the output window. I turn that option off.

Using this plugin, it's possible to examine a lengthy, complex attribute, switch to the notepad window and make whatever changes you want, and simply type control-shift-s to save the changes back to the MUSH. I can pretend that unformatted softcode no longer exists :)

It's posted in two parts since it was too large to post as a single message. Yay.

- Krenath from
bDv TrekMUSH
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.


Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]