Actions define procedures that can be bound to a menu item, a toolbar button or a keyboard shortcut. Most plugin Actions [4] are short scripts written in BeanShell, jEdit's macro scripting language. These scripts either direct the action themselves, delegate to a method in one of the plugin's classes that encapsulates the action, or do a little of both. The scripts are usually short; elaborate action protocols are usually contained in compiled code, rather than an interpreted macro script, to speed execution.
Actions are defined by creating an XML file entitled
actions.xml
and placing it in the plugin JAR
file.
The actions.xml
file from the
QuickNotepad plugin looks as follows:
<ACTIONS> <ACTION NAME="quicknotepad.choose-file"> <CODE> wm.addDockableWindow(QuickNotepadPlugin.NAME); wm.getDockableWindow(QuickNotepadPlugin.NAME).chooseFile(); </CODE> </ACTION> <ACTION NAME="quicknotepad.save-file"> <CODE> wm.addDockableWindow(QuickNotepadPlugin.NAME); wm.getDockableWindow(QuickNotepadPlugin.NAME).saveFile(); </CODE> </ACTION> <ACTION NAME="quicknotepad.copy-to-buffer"> <CODE> wm.addDockableWindow(QuickNotepadPlugin.NAME); wm.getDockableWindow(QuickNotepadPlugin.NAME).copyToBuffer(); </CODE> </ACTION> </ACTIONS>
This file defines three actions. They each use a built-in variable
wm
, which refers to the current view's
DockableWindowManager
. Whenever you need
to obtain a reference to the current dockable, or create a new one, this
is the class to use. We use the method addDockable() followed
by getDockable()
to create if necessary, and then bring up
the QuickNotepad plugin dockable. This will be docked or floating,
depending on how it was last used.
When an action is invoked, the BeanShell scripts address the
plugin through static methods, or if instance data is needed, the
current
View
, its
DockableWindowManager
, and the plugin
object return by the getDockable()
method.
If you are unfamiliar with BeanShell code, you may nevertheless
notice that the code statements bear a strong resemblance to Java code,
with one exception: the variable view
is never
assigned any value.
For complete answers to this and other BeanShell mysteries, see
Part III, “Writing Macros”; two observations will suffice
here. First, the variable view
is predefined by
jEdit's implementation of BeanShell to refer to the current
View
object. Second, the BeanShell scripting
language is based upon Java syntax, but allows variables to be typed at
run time, so explicit types for variables need not be declared.
A formal description of each element of the
actions.xml
file can be found in the documentation
of the
ActionSet
class.
[4] Some plugins, such as Sidekick, Console, and ProjectViewer, create pure Java EditAction-derived Actions, based which services are available, or which files are found in a certain path. However, this is an advanced topic you can explore further in the source and API docs of those plugins.