Writing jEdit macros in Java (in Eclipse)

Right now I don’t know which one sucks more: doing complex beanshell macros in a text editor or doing a proper plugin with an ant deploy script and a reloader (helper) plugin. So, here comes another crappy solution: writing jEdit macros in pure java, with real content assist and code completion.

  1. in the plugin manager install the JavaMacros plugin
  2. in Eclipse create a jEdit_Java_Macros java project
  3. Project Properties: Java Build Path: Libraries: Add External Jars —-» add jedit.jar and JavaMacros.jar
  4. create package: macros.CompiledMacros
  5. create a CompiledMacros subfolder in your personal macros dir
  6. create a class (HelloWorld for example); at the interfaces section add “MacroClass”; you can add a constructor or a main method, but it doesn’t make a difference
  7. now, oh my, you would think about setting the bin path to the Macros folder in the settings directory (Default output folder), but don’t do that – that will just mess up your macros folder, instead use this jEdit macro to automatically copy, reload and execute the compiled class file:
macroName = "CompiledMacros/HelloWorld";
from = "Q:/dev/workspace-java-jedit/jEdit_Java_Macros/bin/macros/";
to = "Q:/.jedit/macros/";

void reloadClassMacro(macroName, from, to) {
import java.io.*;
from += macroName + ".class";
to += macroName + ".class";

in = new FileInputStream(from);
out = new FileOutputStream(to);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
in.close();
out.close();
Macros.loadMacros();
macro = Macros.getMacro(macroName);
macro.invoke(view);
}

reloadClassMacro(macroName, from, to);
return;

Now assign this macro to key in jEdit and it will automatically rerun the class macro. Since class macros are tied to a given location (meaning that if you copy the class file to another directory or you rename it it will not work) this method is useful for developing macros; in the end one might want to copy the final code into a bsh file (with cleaning out the class wrapper of course).

Finally, if you happen to be on Windows you can use this autohotkey script to “call” the macro from within Eclipse (I call the AHK script with Win+F12 (#F12) and I assigned the classloader to F12 in jEdit so that’s why I forward F12 there):

#F12::
Process, Exist, eclipse.exe
If ErrorLevel
{
WinActivate, ahk_pid %ErrorLevel%
Send ^s
}
IfWinExist jEdit
{
WinActivate
Send {F12}
}
return

EDIT: no, I was wrong, this sucks too; I can’t add internal classes, like eventlisteners… thank you very much, jEdit.

Tags: ,

Comments are closed.