Posts Tagged ‘jedit’

Format JSON with jEdit

Thursday, January 14th, 2010

Sometimes I have to debug or check a json response from the server: I just paste the response to jEdit and reformat it with a macro (yes, I know about the online json beautifier and I think we did have a firebug extension for this, but still, it’s fun to see how flexible javascript macros are):

Download the json2.js from json.org, place it into your lib directory (see: startup.js), then use this ridiculously simple macro to reformat a valid javascript object (in buffer or selection):

(function(){
if (buffer.isReadOnly())
return;

var text = "",
selections = textArea.getSelection(),
bufferSelected = false,
obj;

if (selections.length == 1) {
text = textArea.getSelectedText(selections[0]);
} else {
text = textArea.getText();
bufferSelected = true;
}

obj = eval("(" + text + ")");
include(MACROLIBPATH + "json.org/json2.js");
textArea[bufferSelected ? "setText" : "setSelectedText"](JSON.stringify(obj, null, '\t'));
})();

You can download this and some of my other macros from here.

Writing jEdit macros in Java (in Eclipse)

Wednesday, December 30th, 2009

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.

Zen Coding – Expand Abbreviations for jEdit

Sunday, December 27th, 2009

Zen Coding didn’t excite me too much since I have some pretty similar macros/superabbrevs myself, but its dombuilder is quite nice I must admit. I’m not really interested in writing a fully working jEdit connector for all the ZC macros, but I did convert the expander to jEdit; download here.

Macro is in javascript, requires the JavaScriptShell plugin.

jEdit JavaScriptShell macro – write jEdit macros in javacsript

Friday, December 25th, 2009

I have compiled a fixed version for the JavaScriptShell macro, so that the menu bug I reported three months ago is now fixed here. This is version 0.3, all open bugs are fixed (mostly by others), is tested against Jedit current stable (4.3) and 4.4pre1 (svn trunk; btw I could not reproduce Satoda’s bug with the compiled version). Download it, place it into the jars directory of the settings directory and write nice javascript macros!

Jedit javascript macro filename problem

Monday, October 19th, 2009

I still have it, but now I really think I nailed it here. Originally reported here and I just freaking can’t believe that the one who closed this bug could not reproduce it.

EDIT: Thanks, Steve Gough – then it wasn’t my imagination :) Now we can expect much better javascript macros with Jedit.