Home

De Franciscis Dimitri

Homepage & Laboratorio Creativo

Primary links

  • Homepage
  • Curriculum Vitae
  • Servizi
  • Fotografia
  • Libri e guide
  • Contattami
Home Blog dimitri's blog

JEdit + Beanshell = Super Macro Bros

Submitted by dimitri on Mar, 03/02/2009 - 14:30
  • Informatica
  • beanshell
  • escape
  • java
  • jedit
  • macro
  • xml

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
& &
< &lt;
> &gt;
" &quot;

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("&", "&amp;");
text = text.replaceAll("<", "&lt;");
text = text.replaceAll(">", "&gt;");
text = text.replaceAll("\"", "&quot;");
textArea.setText(text);

Things to note

  • use of the public (and well documented) JEdit APIs: JEditTextArea classe 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 &lt; &gt; &amp; &quot;
</elemento>
</root>

then click on "macros --> mymacros --> EscapeXML.bsh": et voilà, our xml source has been "escaped":

&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;root&gt;
&lt;elemento attributo=&quot;valore&quot;&gt;
Prova caratteri escape &amp;lt; &amp;gt; &amp;amp; &amp;quot;
&lt;/elemento&gt;
&lt;/root&gt;

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
  • 1313 letture
  • Italian

FeedFlare

Navigazione

  • Contenuti recenti
  • Cerca
  • Tags
  • Articoli più letti

De Franciscis Dimitri feeds

  • De Franciscis Dimitri - Homepage &amp; Laboratorio Creativo

Secondary links

  • Note legali
  • Pubblicità su www.megadix.it

Copyright 2007 De Franciscis Dimitri - p.iva 05327790969

website monitoring service

Web4Web

RoopleTheme