| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Message
| I have had some queries about adding aliases "simply" from the command window, rather than using the configuration screens.
The scripts below will do this, in conjunction with three aliases you can add (see the comments in bold for what they are). These will behave similarly to the TinTin aliases, however because of different implementations they will not always be the same. It should be close enough in many cases.
To install:
- Add three aliases, following the instructions in the comments below, in bold type.
- Copy the code below and paste into your script file (VBscript), enabling scripting if necessary.
To use:
Add an alias
#alias gb get bread bag
#alias shop {north;north;north;east;in}
#heal cast heal %1
If you use wildcard substitutions (eg. %1, %2) then the script will automatically put the appropriate wildcard symbols (asterisks) into the generated alias match string.
Delete an alias
#unalias gb
List aliases
#alias - Lists all aliases
#alias get - Lists all aliases matching 'get'
'------------------------------------------------
' Script to add an alias from the command window
'
' Examples:
'
' #alias gb get bread bag
' #alias ws {wake;stand}
' #alias heal cast 'heal' %1
'
' Note - the alias is one word (A-Z, 0-9, underscore) and sends
' the rest of the line. Wildcards are automatically generated
' where required to supply text to the replacement strings %1
' and so on. By using braces and semicolons you can send
' multiple lines.
'
' Alias: ^\#alias ([A-Za-z0-9_]+) (.+)$
' Send: (nothing)
' Enabled: checked
' Regular expression: checked
' Label: AddAnAlias
' Script: AddAnAlias
'-------------------------------------------------
sub AddAnAlias (strName, strLine, aryWildcards)
Dim iResult, iMaxWildcard, iCount, sAliasName
Dim sAlias, sReplacement, sError
sAlias = Trim (aryWildcards (1))
' if alias text is in the form {xxx} remove the braces
if Left (sAlias, 1) = "{" and Right (sAlias, 1) = "}" then
sAlias = mid (sAlias, 2, Len (sAlias) - 2)
end if
sAliasName = sAlias
sReplacement = Trim (aryWildcards (2))
' if replacement text is in the form {a; b}
' 1. replace ';' by newlines
' 2. remove the braces
if Left (sReplacement, 1) = "{" and Right (sReplacement, 1) = "}" then
sReplacement = world.replace (sReplacement, ";", vbCrLf, true)
sReplacement = mid (sReplacement, 2, Len (sReplacement) - 2)
end if ' of braces
' count wildcards so we know how many to put into the alias
' we will look for %1, %2 and so on, up to %9
iMaxWildcard = 0
for iCount = 1 to 9
if instr (aryWildcards (2), "%" & iCount) > 0 then
iMaxWildcard = iCount
end if
next
' allow for %r - the rest of the line - by doing one more wildcard
if instr (aryWildcards (2), "%r") > 0 then
iMaxWildcard = iMaxWildcard + 1
sReplacement = world.replace (sReplacement, "%r", "%" & iMaxWildcard, true)
end if ' he had a %r
' now add the wildcards the text to be matched
for iCount = 1 to iMaxWildcard
sAlias = sAlias & " *"
next
' add the alias
iResult = World.addalias (sAliasName, sAlias, sReplacement, 513, "")
' notify the user the alias has been added, or of the error
if iResult = 0 then
World.note "Alias '" & sAliasName & "' added"
else
select case iResult
case 30008 sError = "The alias name " & sAliasName & " is not valid"
case 30011 sError = "The alias " & sAliasName & " already exists"
case 30012 sError = "The alias 'match' string cannot be empty"
case else sError = "Code = " & iResult
end select
World.note "** Alias not added: " & sError
end if ' error adding
end sub
'------------------------------------------------
' Script to remove an alias from the command window
'
' Example:
'
' #unalias gb
'
' Note - the alias is one word (A-Z, 0-9, underscore)
'
' Alias: ^\#unalias ([A-Za-z0-9_]+)$
' Send: (nothing)
' Enabled: checked
' Regular expression: checked
' Label: RemoveAnAlias
' Script: RemoveAnAlias
'-------------------------------------------------
sub RemoveAnAlias (strName, strLine, aryWildcards)
Dim iResult, sAlias, sError
sAlias = Trim (aryWildcards (1))
' if alias text is in the form {xxx} remove the braces
if Left (sAlias, 1) = "{" and Right (sAlias, 1) = "}" then
sAlias = mid (sAlias, 2, Len (sAlias) - 2)
end if ' we had braces
' add the alias
iResult = World.DeleteAlias (sAlias)
' notify the user the alias has been deleted, or of the error
if iResult = 0 then
World.note "Alias '" & sAlias & "' deleted"
else
select case iResult
case 30008 sError = "The alias name " & sAlias & " is not valid"
case 30010 sError = "The alias " & sAlias & " does not exist"
case else sError = "Code = " & iResult
end select
World.note "** Alias not deleted: " & sError
end if ' error deleting
end sub
'------------------------------------------------
' Script to display all aliases, or all matching a string
'
' Example:
'
' #alias
' #alias get
'
' Note - you can specify a "wildcard" (eg. 'get') which
' will only display aliases with that word in them
' somewhere.
'
' Alias: ^\#alias( [A-Za-z0-9_]+)?$
' Send: (nothing)
' Enabled: checked
' Regular expression: checked
' Label: ShowAllAliases
' Script: ShowAllAliases
'-------------------------------------------------
sub ShowAllAliases (strName, strLine, aryWildcards)
dim mylist, i, sSend, sScript, sMatch, sCount, sPlural
sMatch = Trim (lCase (aryWildcards (1))) ' what to match on
' get alias list
mylist = world.GetAliasList ' list of all aliases (with labels)
sCount = 0
' show list, possibly matching on supplied string (wildcard 1)
if not IsEmpty (mylist) then
for i = lbound (mylist) to ubound (mylist)
if sMatch = "" or Instr (lCase (mylist (i)), sMatch) > 0 then
' we have a match - see what the alias sends
sSend = world.getaliasinfo (mylist (i), 2)
if sSend <> "" then
sSend = " = " & sSend
end if ' have something to send
' get the script, if any
sScript = world.getaliasinfo (mylist (i), 3)
if sScript <> "" then
sScript = " (Script: " & sScript & ")"
end if ' have a script
' display the results
world.note mylist (i) & sSend & sScript
sCount = sCount + 1 ' and count them
end if ' wanting to see this one
next
End If
' make our English look a bit better: alias/aliases
if sCount > 1 then
sPlural = "es"
else
sPlural = ""
end if
' show count, so that if no aliases match they will at least
' see something
if sMatch = "" then
world.note sCount & " alias" & sPlural & " found (with labels)"
else
world.note sCount & " alias" & sPlural & " matched the label '" & _
sMatch & "'"
end if
end sub
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|