Single Execution Macros

At this point, we have covered some basic techniques for writing one-line or other short macros. As noted earlier, you can save a BeanShell script of any length as a text file with the .bsh extension. There are three other ways jEdit lets you use BeanShell quickly, without saving a script to storage, on a "one time only" basis. You will find them in the Utilities menu.

Utilities>BeanShell>Evaluate BeanShell Expression displays a text input dialog that asks you to type a single line of BeanShell commands. You can type more than one BeanShell statement so long as each of them ends with a semicolon. If BeanShell successfully interprets your input, a message box will appear with the return value of the last statement. You can do the same thing using the BeanShell interpreter provided with the Console plugin; the return value will appear in the output window.

Utilities>BeanShell>Evaluate For Selected Lines displays a text input dialog that asks you to type a single line of BeanShell commands. The commands are evaluated for each line of the selection. In addition to the standard set of variables, this command defines the following:

Try typing an expression like (line + 1) + ": " + text in the Evaluate For Selected Lines dialog box. This will add a line number to each selected line beginning with the number 1.

The BeanShell expression you enter will be evaluated and substituted in place of the entire text of a selected line. If you want to leave the line's current text as an element of the modified line, you must include the defined variable text as part of the BeanShell expression that you enter.

Utilities>BeanShell>Evaluate Selection evaluates the selected text as a BeanShell script and replaces it with the return value of the statement.

Using Evaluate Selection is an easy way to do arithmetic calculations inline while editing. BeanShell uses numbers and arithmetic operations in an ordinary, intuitive way.

Try typing an expression like (3745*856)+74 in the buffer, select it, and choose Utilities>BeanShell>Evaluate Selection. The selected text will be replaced by the answer, 3205794.