JEdit + Beanshell = Super Macro Bros
Let's learn in a few minutes how to write macros for this popular editor
JEdit
This fantastic editor, written in Java, is a wonderful choice for programmers in search of an ultra-flexible tool for source editing, its countless features include:
- syntax highlighting for hundreds of languages;
- has many plug-in targeted specifically at programmers;
- uses Java underneath (through the Beanshell) as scripting language;
- the search & replace function uses Java regular expressions and Beanshell, making it possible to do virtually any kind of editing.
Beanshell
Beanshell is a scripting engine based on an "augmented" Java syntax:
- type declarations are not mandatory;
- automatic set/get wrapping;
- can normally interact with host application's objects;
- can use external libraries as in any compiled Java program.
One minute tutorial
After having installed JEdit, let's proceed with our first script.
We will create a script that substitutes special XML charachters with their corresponding "entity references"; this operation is called escaping, and is necessary if we want to display those special characters in a web page, such as in code snippets. These are the translations we have to do (in order):
| Character | Translated to |
|---|---|
| & | & |
| < | < |
| > | > |
| " | " |
As you guessed this process can be cumbersome to do by hand, not to mention the risk of doing errors. So let's write a macro to handle it!
Fire up JEdit and copy'n'paste the code below in a new file:
JEditTextArea textArea = view.getTextArea();
String text = textArea.getText();
text = text.replaceAll("&", "&");
text = text.replaceAll("<", "<");
text = text.replaceAll(">", ">");
text = text.replaceAll("\"", """);
textArea.setText(text);
Things to note
- use of the public (and well documented) JEdit APIs:
JEditTextAreaclasse is part of those; - use of Java regular expressions, available since 1.4 version.
Under JEdit root directory we can find a sub-folder called "macros": save here your file, and call it EscapeXML.bsh, then click on top menu "Macros --> Rescan Macros". Our script will appear under the "Macros" sub-menu, ready to be used!
Let's try it on an XML file: copy'n'paste the code below in a new file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<elemento attributo="valore">
Prova caratteri escape < > & "
</elemento>
</root>
then click on "macros --> mymacros --> EscapeXML.bsh": et voilà, our xml source has been "escaped":
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<elemento attributo="valore">
Prova caratteri escape &lt; &gt; &amp; &quot;
</elemento>
</root>
The nice thing is that the snippets you see on this page have been escaped by this very script!
Implementing undo/redo
Sorry, you don't have to implement anything because JEdit does it for you...
Development
Obviously this script can be enhanced in many ways:
- performance: how can we handle very big files? (hint: escape line by line...)
- escape only selected text; beware that JEdit has many selection schemes: normal selection, rectangular selection, multiple...
- why not making a macro for Java strings escaping? There is a JakartaCommons plugin that makes Jakarta-Commons Lang (and other) library available for use by plug-ins and macros.
- dimitri's blog
- Aggiungi un commento
- 2156 letture
-
- Italian
