Archive for August, 2011

RSyntaxTextArea 1.5.2 Released

Saturday, August 27th, 2011

A minor update of RSyntaxTextArea and AutoComplete are now available on SourceForge and the Subversion repository.  Here’s what’s new:

  • Simplified the anti-aliasing API.
  • Java syntax highlighting updates to support Java 7 (binary literals, underscores in int literals).
  • Added an API to render tab lines (aka indent guides).
  • Fixed single-quoted strings in MXML.
  • Added miscellaneous keywords to Unix shell and Windows batch scanners.
  • Minor bug fixes.

Nothing earth-shattering.  The most noticeable update (and it’s not that noticeable, to me at least) is the anti-aliasing updates.  Not only is the API simplified (don’t know what I was thinking when I originally created it), but RSTA will now automatically pick the “best” anti-aliasing method to match the AA done by your OS, according to Java2D.  Font rendering has always been a sore spot for Swing applications, but with the later Java 6 updates, it’s actually gotten really, really good (at least on Windows).  Text rendering now (practically) matches that seen in native apps.  RSTA now handles specifying the AA hints the “right” way to look at nice as possible.

The next release will likely see more drastic changes, and will come along-side updates to the RSTALanguageSupport library as well.

Scripted Macros in next RText

Tuesday, August 23rd, 2011

In the next RText release, the old macro functionality – which recorded keystrokes in the editor component for playback – is being replaced with something much more powerful.  If you’re using Java 6 or later, you’ll be able to write scripts in Rhino or Groovy (Groovy support requires the embeddable Groovy jar to be placed on RText’s classpath), assign them shortcuts, then use them like any other action in the RText menu bar!  These scripts will run in the main RText process, and will have programmatic access to the entire application.  This truly allows you to extend RText in any way you can think of.

To create a scripted macro, choose “New Macro…” from the Macros menu:

New Macro menu item

New Macro menu item

The name of your macro must be unique among all macros, and will be the menu item text.  The description field is optional, and will display in RText’s status bar when you mouse-over the menu item.  Give the macro a keyboard shortcut (if you want to), select the scripting language you want to use, and click OK to begin creating your macro.  (Note that if you choose to use Groovy, you’ll be reminded in the dialog that the embeddable Groovy jar needs to be placed in the RText install, and RText needs to be restarted to pick it up.  Rhino works out-of-the-box with Java 6 JVM’s since it comes standard).

A script template will open in RText:

A rather large comment at the top describes the macro functionality, as well as two pre-defined variables you can use to manipulate the running RText instance:

In this file, you simply write out your macro.  As an example, below is simple Rhino script for a (very crude, and in fact mostly incorrect) word counter macro.  It counts the number of “words” in the selected text in the current editor, and displays it in a popup dialog.  Note the focus here isn’t on the actual correctness of the algorithm, but rather the overall macro structure:

Word Count Macro

Word Count Macro

Important things to take away are:

  1. Macros are run on the EDT, so you are free to call any Swing-related methods you want without wrapping them in SwingUtilities.invokeLater().
  2. It’s a good idea to wrap your macros in calls to textArea.beginAtomicEdit() and textArea.endAtomicEdit() if you will be manipulating the text in textArea.  The latter method should also be in a “finally” block, in case any unexpected exceptions are thrown.  These methods cause any action done in your macro to be remembered as a “single operation” for undo/redo purposes.  Failure to do this simply means that, if you edit the text in the text component, you may have to press Ctrl+Z multiple times to get what the macro did un-done, depending on what exactly you’re doing.  In the example above, we didn’t actually edit the text area’s contents, so beginAtomicEdit()/endAtomicEdit() weren’t strictly necessary, but I left them there for instruction.

Pretty cool, huh?  If you assigned this macro a shortcut, you can run it by simply typing said shortcut (be sure to select some text first to watch it work!).

Editing or removing macros can be easily be done in the “Macros” panel of the Options dialog.  This panel is easily accessed via the “Macros” -> “Edit Macros…” menu item.

Macros in Options dialog

Macros in Options dialog

Here you can add new macros, remove old ones, or modify existing ones (modifying either their shortcut/description, or the actual code itself).