How to fix capitalization of function names in scripts

Posted by Nick Gammon on Thu 29 Dec 2005 07:26 PM — 3 posts, 12,822 views.

Australia Forum Administrator #0
Recently I had occasion to convert a VBscript plugin to Lua. An interesting problem that occurred doing that was that VBscript is not case-sensitive when using inbuilt MUSHclient functions. For example, you can use:


world.setvariable ("a", 42)


However the correct capitalization is actually:


world.SetVariable ("a", 42)


If you have to convert a script like that, or simply want to make your scripts more "correct" you can use the new Global Replace feature of the notepad to quickly do it in one step.

To assist in this process I added a new Lua utility function utils.functionlist, which returns a table of all internal function names. This appears in version 3.73 of MUSHclient. However if you are using a slightly older version you could simply use a table of all known functions.

Here is how you do it.

Use the Notepad's Search -> Global Replace dialog, entering:


Find Pattern: [%a%d]+
Replacement: f
Script:

t1 = utils.functionlist ()
t2 = {}

-- keys are lower-case functions, values are original functions

table.foreach (t1, function (k, v) t2 [string.lower (v)] = v end )

function f (str)
  return t2 [string.lower (str)] or str
end -- function f


Taking as an example some code that Shadowfyr posted recently:



Before replacement:

sub ResetTmr (name, output, wildcards)
  dim Icnm ' Names.
  dim Tms ' Current times.
  dim Ada
  Ada = split(world.getvariable("Ada"),",")
  Icnm = split(world.getvariable("Icnm"),",")
  Tms = split(world.getvariable("Tms"),",")
  dim count,test
  test = 0
  for count = 0 to ubound(Icnm)
    if Icnm(count) = name then
      Tms(count) = 0
      if ada(count) > 0 then
        test = 1
      end if
    end if
  next
  world.setvariable "Tms", join(Tms,",")
  if test then
    world.enabletrigger "CatchStat", 1
    world.send "stats"
  end if
end sub

After replacement:

sub ResetTmr (name, output, wildcards)
  dim Icnm ' Names.
  dim Tms ' Current times.
  dim Ada
  Ada = split(world.GetVariable("Ada"),",")
  Icnm = split(world.GetVariable("Icnm"),",")
  Tms = split(world.GetVariable("Tms"),",")
  dim count,test
  test = 0
  for count = 0 to ubound(Icnm)
    if Icnm(count) = name then
      Tms(count) = 0
      if ada(count) > 0 then
        test = 1
      end if
    end if
  next
  world.SetVariable "Tms", join(Tms,",")
  if test then
    world.EnableTrigger "CatchStat", 1
    world.Send "stats"
  end if
end sub


The other thing you could do is replace the "world.function" calls by omitting "world.". You could do that like this:


Find Pattern: [Ww]orld.([%a%d]+)
Replacement: %1


Now the results look like this:


sub ResetTmr (name, output, wildcards)
  dim Icnm ' Names.
  dim Tms ' Current times.
  dim Ada
  Ada = split(GetVariable("Ada"),",")
  Icnm = split(GetVariable("Icnm"),",")
  Tms = split(GetVariable("Tms"),",")
  dim count,test
  test = 0
  for count = 0 to ubound(Icnm)
    if Icnm(count) = name then
      Tms(count) = 0
      if ada(count) > 0 then
        test = 1
      end if
    end if
  next
  SetVariable "Tms", join(Tms,",")
  if test then
    EnableTrigger "CatchStat", 1
    Send "stats"
  end if
end sub
Amended on Thu 29 Dec 2005 11:09 PM by Nick Gammon
Australia Forum Administrator #1
If you want to try this out today, without waiting for the new version, here is a replacement for the first line of the script, that has all current function names in it:


t1 = {
  "Accelerator",
  "AcceleratorList",
  "Activate",
  "ActivateClient",
  "ActivateNotepad",
  "AddAlias",
  "AddMapperComment",
  "AddTimer",
  "AddToMapper",
  "AddTrigger",
  "AddTriggerEx",
  "AdjustColour",
  "ANSI",
  "AnsiNote",
  "AppendToNotepad",
  "ArrayClear",
  "ArrayCount",
  "ArrayCreate",
  "ArrayDelete",
  "ArrayDeleteKey",
  "ArrayExists",
  "ArrayExport",
  "ArrayExportKeys",
  "ArrayGet",
  "ArrayGetFirstKey",
  "ArrayGetLastKey",
  "ArrayImport",
  "ArrayKeyExists",
  "ArrayListAll",
  "ArrayListKeys",
  "ArrayListValues",
  "ArraySet",
  "ArraySize",
  "Base64Decode",
  "Base64Encode",
  "BoldColour",
  "BroadcastPlugin",
  "CallPlugin",
  "ChatAcceptCalls",
  "ChatCall",
  "ChatCallzChat",
  "ChatDisconnect",
  "ChatDisconnectAll",
  "ChatEverybody",
  "ChatGetID",
  "ChatGroup",
  "ChatID",
  "ChatMessage",
  "ChatNameChange",
  "ChatNote",
  "ChatPasteEverybody",
  "ChatPasteText",
  "ChatPeekConnections",
  "ChatPersonal",
  "ChatPing",
  "ChatRequestConnections",
  "ChatSendFile",
  "ChatStopAcceptingCalls",
  "ChatStopFileTransfer",
  "CloseLog",
  "CloseNotepad",
  "ColourNameToRGB",
  "ColourNote",
  "ColourTell",
  "Connect",
  "CreateGUID",
  "CustomColourBackground",
  "CustomColourText",
  "Debug",
  "DeleteAlias",
  "DeleteAliasGroup",
  "DeleteAllMapItems",
  "DeleteCommandHistory",
  "DeleteGroup",
  "DeleteLastMapItem",
  "DeleteOutput",
  "DeleteTemporaryAliases",
  "DeleteTemporaryTimers",
  "DeleteTemporaryTriggers",
  "DeleteTimer",
  "DeleteTimerGroup",
  "DeleteTrigger",
  "DeleteTriggerGroup",
  "DeleteVariable",
  "DiscardQueue",
  "Disconnect",
  "DoAfter",
  "DoAfterNote",
  "DoAfterSpecial",
  "DoAfterSpeedWalk",
  "DoCommand",
  "EchoInput",
  "EnableAlias",
  "EnableAliasGroup",
  "EnableGroup",
  "EnableMapping",
  "EnablePlugin",
  "EnableTimer",
  "EnableTimerGroup",
  "EnableTrigger",
  "EnableTriggerGroup",
  "ErrorDesc",
  "EvaluateSpeedwalk",
  "Execute",
  "ExportXML",
  "FixupEscapeSequences",
  "FixupHTML",
  "GenerateName",
  "GetAlias",
  "GetAliasInfo",
  "GetAliasList",
  "GetAliasOption",
  "GetAliasWildcard",
  "GetAlphaOption",
  "GetAlphaOptionList",
  "GetChatInfo",
  "GetChatList",
  "GetChatOption",
  "GetClipboard",
  "GetCommand",
  "GetCommandList",
  "GetConnectDuration",
  "GetCurrentValue",
  "GetDefaultValue",
  "GetEntity",
  "GetFrame",
  "GetHostAddress",
  "GetHostName",
  "GetInfo",
  "GetInternalCommandsList",
  "GetLineCount",
  "GetLineInfo",
  "GetLinesInBufferCount",
  "GetLoadedValue",
  "GetMainWindowPosition",
  "GetMapColour",
  "GetMappingCount",
  "GetMappingItem",
  "GetMappingString",
  "GetNotepadLength",
  "GetNotepadText",
  "GetNotepadWindowPosition",
  "GetNotes",
  "GetNoteStyle",
  "GetOption",
  "GetOptionList",
  "GetPluginAliasInfo",
  "GetPluginAliasList",
  "GetPluginID",
  "GetPluginInfo",
  "GetPluginList",
  "GetPluginName",
  "GetPluginTimerInfo",
  "GetPluginTimerList",
  "GetPluginTriggerInfo",
  "GetPluginTriggerList",
  "GetPluginVariable",
  "GetPluginVariableList",
  "GetQueue",
  "GetReceivedBytes",
  "GetRecentLines",
  "GetScriptTime",
  "GetSelectionEndColumn",
  "GetSelectionEndLine",
  "GetSelectionStartColumn",
  "GetSelectionStartLine",
  "GetSentBytes",
  "GetStyleInfo",
  "GetSysColor",
  "GetSystemMetrics",
  "GetTimer",
  "GetTimerInfo",
  "GetTimerList",
  "GetTimerOption",
  "GetTrigger",
  "GetTriggerInfo",
  "GetTriggerList",
  "GetTriggerOption",
  "GetTriggerWildcard",
  "GetUdpPort",
  "GetUniqueID",
  "GetUniqueNumber",
  "GetVariable",
  "GetVariableList",
  "GetWorld",
  "GetWorldById",
  "GetWorldID",
  "GetWorldIdList",
  "GetWorldList",
  "GetWorldWindowPosition",
  "GetWorldWindowPositionX",
  "GetXMLEntity",
  "Hash",
  "Help",
  "Hyperlink",
  "ImportXML",
  "Info",
  "InfoBackground",
  "InfoClear",
  "InfoColour",
  "InfoFont",
  "IsAlias",
  "IsConnected",
  "IsLogOpen",
  "IsPluginInstalled",
  "IsTimer",
  "IsTrigger",
  "LoadPlugin",
  "LogInput",
  "LogNotes",
  "LogOutput",
  "LogSend",
  "MakeRegularExpression",
  "MapColour",
  "MapColourList",
  "Mapping",
  "MoveMainWindow",
  "MoveNotepadWindow",
  "MoveWorldWindow",
  "MoveWorldWindowX",
  "MtRand",
  "MtSrand",
  "NormalColour",
  "Note",
  "NoteColour",
  "NoteColourBack",
  "NoteColourFore",
  "NoteColourName",
  "NoteColourRGB",
  "NoteHr",
  "NotepadColour",
  "NotepadFont",
  "NoteStyle",
  "Open",
  "OpenLog",
  "PasteCommand",
  "Pause",
  "PickColour",
  "PluginSupports",
  "PushCommand",
  "Queue",
  "ReadNamesFile",
  "Redraw",
  "ReloadPlugin",
  "RemoveBacktracks",
  "RemoveMapReverses",
  "Replace",
  "ReplaceNotepad",
  "Reset",
  "ResetStatusTime",
  "ResetTimer",
  "ResetTimers",
  "ReverseSpeedwalk",
  "RGBColourToName",
  "Save",
  "SaveNotepad",
  "SaveState",
  "SelectCommand",
  "Send",
  "SendImmediate",
  "SendNoEcho",
  "SendPush",
  "SendToNotepad",
  "SetAliasOption",
  "SetAlphaOption",
  "SetChanged",
  "SetChatOption",
  "SetClipboard",
  "SetCommand",
  "SetEntity",
  "SetInputFont",
  "SetNotes",
  "SetOption",
  "SetOutputFont",
  "SetStatus",
  "SetTimerOption",
  "SetTriggerOption",
  "SetVariable",
  "ShowInfoBar",
  "Simulate",
  "Sound",
  "SpeedWalkDelay",
  "SpellCheck",
  "SpellCheckCommand",
  "StripANSI",
  "Tell",
  "Trace",
  "TraceOut",
  "TranslateGerman",
  "Trim",
  "UdpListen",
  "UdpPortList",
  "UdpSend",
  "Version",
  "WorldAddress",
  "WorldName",
  "WorldPort",
  "WriteLog",
}
Amended on Thu 29 Dec 2005 07:44 PM by Nick Gammon
Australia Forum Administrator #2
Of course, you could adapt the idea slightly to fix up things like "if" to "If" (for VBscript), "endif" to "EndIf" and so on.