Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Message
| OK, I think this will work:
 |
To save and install the Tintin_Macro_Adder plugin do this:
- Copy between the lines below (to the Clipboard)
- Open a text editor (such as Notepad) and paste the plugin into it
- Save to disk on your PC, preferably in your plugins directory, as Tintin_Macro_Adder.xml
- Go to the MUSHclient File menu -> Plugins
- Click "Add"
- Choose the file Tintin_Macro_Adder.xml (which you just saved in step 3) as a plugin
- Click "Close"
|
[EDIT]: Amended to fix problem with FixVariables not being in the script space.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Thursday, April 29, 2010, 7:28 AM -->
<!-- MuClient version 4.51 -->
<!-- Plugin "Tintin_Macro_Adder" generated by Plugin Wizard -->
<muclient>
<plugin
name="Tintin_Macro_Adder"
author="Nick Gammon"
id="3c626e5d9343e81ef69d9f93"
language="Lua"
purpose="Lets you add macro keys"
save_state="y"
date_written="2010-04-29 12:55"
requires="4.50"
version="1.1"
>
<description trim="y">
<![CDATA[
Type: #macro {key} {action}
eg. #macro {alt+f} {cast @heal}
Variables can be included in the actions and will be expanded when the key is hit.
Macros are saved for next time.
Type: '#macrolist' for a list of macros.
Type "Tintin_Macro_Adder:help" for this help.
]]>
</description>
</plugin>
<!-- Aliases -->
<aliases>
<alias
match="#macro {*} {*}"
enabled="y"
sequence="100"
script="AddMacro"
>
</alias>
<alias
script="MacroList"
match="#macrolist"
enabled="y"
sequence="100"
>
</alias>
<alias
script="Execute_Macro"
match="#execute_macro: *"
enabled="y"
send_to="12"
omit_from_output="y"
omit_from_command_history="y"
sequence="100"
>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
require "serialize"
macros = {} -- in case none exist yet
function FixVariables (what)
local errors = {} -- no errors yet
-- function to do the replacements for string.gsub
local function replace_variables (s)
s = string.sub (s, 2) -- remove the @
replacement = GetPluginVariable ("", s) -- look up variable in global script space
if not replacement then -- not there, add to errors list
table.insert (errors, s)
return
end -- not there
return replacement -- return variable
end -- replace_variables
-- replace all variables starting with @
command = string.gsub (what, "@%a[%w_]*", replace_variables)
-- report any errors
if table.getn (errors) > 0 then
for k, v in ipairs (errors) do
ColourNote ("white", "red", "Variable '" .. v .. "' does not exist")
end -- for
return
end -- error in replacing
Execute (command)
end -- function FixVariables
function AddMacro (name, line, wildcards)
macro_key = wildcards [1]
what_do_do = wildcards [2]
Accelerator (macro_key , "#execute_macro: " .. what_do_do)
ColourNote ("red", "white", macro_key .." now executes: ".. what_do_do)
end -- function AddMacro
function OnPluginInstall ()
assert (loadstring (GetVariable ("macros") or "")) ()
-- add the accelerator keys back
for _, k in ipairs (macros) do
local key, action = string.match (k, "^(%w+%+%w+) = (.*)$")
if key then
Accelerator (key, action)
end -- if
end -- for loop
end -- OnPluginInstall
function OnPluginSaveState ()
macros = AcceleratorList () or {}
SetVariable ("macros", "macros = " .. serialize.save_simple (macros))
end -- function OnPluginSaveState
function MacroList (name, line, wildcards)
macros = AcceleratorList () or {}
table.sort (macros)
for _, k in ipairs (macros) do
local key, action = string.match (k, "^(%w+%+%w+) = (.*)$")
if key then
AcceleratorTo (key, action, sendto)
action = string.match (action, '^#execute_macro: (.*)$')
print (key, '=', action)
end -- if
end -- for loop
end -- function MacroList
function Execute_Macro (name, line, wildcards)
FixVariables (wildcards [1])
end -- function Execute_Macro
]]>
</script>
<!-- Plugin help -->
<aliases>
<alias
script="OnHelp"
match="Tintin_Macro_Adder:help"
enabled="y"
>
</alias>
</aliases>
<script>
<![CDATA[
function OnHelp ()
world.Note (world.GetPluginInfo (world.GetPluginID (), 3))
end
]]>
</script>
</muclient>
This skips the creation of aliases altogether.
For example:
#macro {ctrl+g} {kick @target}
It replies:
ctrl+g now executes: kick @target
Now if 'target' variable is not defined, and you type ctrl+g you see:
Variable 'target' does not exist
However if it *does* exist you see:
You can list your macros:
#macrolist
Alt+F = c @heal
Ctrl+G = kick @target
Ctrl+I = sigh
The existing macros are saved to the plugin state file, eg.
<variable name="macros">macros = {
[1] = "Ctrl+G = #execute_macro: kick @target",
[2] = "Ctrl+I = #execute_macro: sigh",
[3] = "Alt+F = #execute_macro: c @heal",
}</variable>
This is done in the OnPluginSaveState function, and they are loaded back in when the plugin is installed.
The macros are send an alias (#execute_macro) with the rest of the line after it. This alias calls the FixVariables function to replace the @variable sequence at the time you hit the accelerator. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|