Jedit macros can end up pretty verbose and complex and I really hate copy pasting. So, the solution: first set up a directory where the macros can be stored (see my previous post for that), then let’s define a require function in the startup.bsh file:
void require(View view, String filename) {
String script = file_get_contents(MACROLIBPATH + filename + ".bsh");
result = BeanShell.eval(view, BeanShell.getNameSpace(), script);
}
As you might have realized, I used file_get_contents, which is “not really java”; let’s fix it:
String file_get_contents(String filename) {
String s = "";
String ret = "";
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while((s = br.readLine()) != null) { ret += s + "\n"; }
fr.close();
return ret;
}
Yes, I know, my beanshell scripting knowledge is not that great, but hey, it works…
Now for an example macro (call it example.bsh and save it into your macrolib directory):
prettyFunction(view, message) {
Macros.message(view, "Hi! You got a message: " + message);
}
String prettyMessage = "it works!";
Now this should work (in another macro):
require(view, "example");
prettyFunction(view, prettyMessage);
In case you know a solution to the scattered view parameters, let me know.