<?xml version="1.0" encoding="iso-8859-1"?>
<!--
A tiny plugin for reloading other plugins by typing a command.
==============================================================

Type "reload plugin <pluginName>" to reload any other installed plugin.

After you have done this once, the plugin will remember the name of the
last plugin you reloaded (saved across sessions too) and you can press
Ctrl+Alt+Shift+R to reload it again.

-->
<!DOCTYPE muclient>
<muclient>

<plugin
	name="Plugin_Reloader"
	author="Ross Grams"
	purpose="Easily(?) reloading plugins."
	id="cc69a4535a8549670ec82ffb"
	language="Lua"
	save_state="y"
	date_written="2019-02-15 13:13:35"
	requires="5.05"
	version="1.0"
	>
</plugin>

<aliases>
	<alias
		script="reloadPlugin"
		match="^reload plugin (.*)?$"
		enabled="y"
		regexp="y"
		sequence="100"
		ignore_case="y"
	></alias>
</aliases>

<script>
<![CDATA[

local errors = {
	[error_code.eOK] = "Plugin reload successful.",
	[error_code.eNoSuchPlugin] = "No plugin named '%s' is installed.",
	[error_code.eBadParameter] = "We can't reload ourself.",
	[error_code.ePluginFileNotFound] = "Plugin file not found.",
	[error_code.eProblemsLoadingPlugin] = "Parsing or other problem in plugin file - see error messages."
}

local hotkey = "Ctrl+Alt+Shift+R"

local loadLastDummyName = "&& Load last &&"
local lastPlugin

function reloadPlugin(name, line, wildcards)
	local pluginName = wildcards[1]
	if pluginName == loadLastDummyName then
		if lastPlugin then
			pluginName = lastPlugin
		else
			local msg = "Reload Plugin (hotkey): No remembered plugin to load."
			ColourNote("yellow", "", msg)
			return
		end
	end
	ColourNote("yellow", "", "Reload Plugin: " .. pluginName)
	local err, msg = pcall(ReloadPlugin, pluginName)
	local s = errors[msg] or ("Unknown Error: " .. tostring(msg))
	local msgColor = (msg == error_code.eOK) and "yellow" or "orangered"
	ColourNote(msgColor, "", "   " .. string.format(s, pluginName))
	if msg == error_code.eOK then  lastPlugin = pluginName  end
end

local function init()
	lastPlugin = GetVariable("lastPlugin")
	Accelerator(hotkey, "reload plugin " .. loadLastDummyName)
end

local function final()
	Accelerator(hotkey, "", 0) -- Reset hotkey.
end

function OnPluginSaveState()
	if lastPlugin then  SetVariable("lastPlugin", lastPlugin)  end
end

function OnPluginInstall()  init()  end
function OnPluginEnable()  init()  end
function OnPluginClose()  final()  end
function OnPluginDisable()  final() end

]]>
</script>

</muclient>