Chapter 9.  Macros and Macro Programming

This chapter describes how to use macros with SlickEdit Core/

There are two types of macros in SlickEdit® Core:

Recorded Macros

You can automate repetitive tasks by recording a series of SlickEdit® Core operations in a macro. After you create a macro, you can run it, save it, bind it to a key sequence, and/or modify the macro's source code.

Recording a macro generates Slick-C® code for performing the action being recorded. Therefore, recording a macro is also a useful way to discover and implement Slick-C code that controls the behavior of SlickEdit Core.

Topics in this section are:

Common Macro Operations

Macros can be recorded, executed, and saved from the Macro menu, or you can use commands or predefined key bindings to perform macro operations:

  • To start or end macro recording, from the main menu, click Macro → Record Macro or Macro → Stop Recording Macro, respectively. Alternately, you can toggle recording on and off with one of the following methods:

    • Click the recording indicator REC, located along the bottom edge of the editor. When a macro is being recorded, the recording indicator is active (not dimmed).

    • In CUA emulation, press Ctrl+F11 (the key binding associated with the record_macro_toggle command).

    • On the SlickEdit Core command line, type record_macro_toggle.

    See Recording a Macro for more information.

  • To run the last macro that you recorded, click Macro → Execute last-macro, press Ctrl+F12, or use the record_macro_end_execute command. See Running a Recorded Macro for more information.

  • To display a list of your recorded macros, from which you can edit, run, delete, or bind to a key sequence, click Macro → List Macros, or use the list_macros command.

Note

List Macros only shows your "saved" macros, not your last recorded macro or macros created using execute_last_macro_key.

Recording a Macro

To record a macro, simply start the recording, enter the keystrokes you want to record, then end the recording. The instructions below outline the steps.

  1. From the main menu, click Macro → Record Macro (or use one of the toggle methods to start recording, as described under Recorded Macros above).

  2. Enter the keystrokes that you want to record. For example, to record a macro of the cursor moving three spaces to the right, press the right arrow key three times. You can also change a configuration option, view settings, or expand a code template during macro recording.

  3. When you have finished recording the macro, end recording by clicking Macro → Stop Recording Macro (or the same toggle you used in Step 1). The Save Macro Dialog is displayed.

    Tip

    • For recorded macros you don't need to track, perhaps for immediate or one-time use, SlickEdit Core provides a way to stop macro recording and instantly bind the macro to a key sequence. This allows you to keep a set of recent, unnamed macro recordings instead of having just one "last recorded macro". See Binding Macros Using execute_last_macro_key for more information.

    • If you're recording the macro to discover Slick-C® code (see Using Macros to Discover and Control Options), click Edit (or press Alt+E) at this time to view the source code. If you choose to view/edit the source code now, you will need to save it by using Macro → Save last-macro prior to recording a new macro or exiting the editor. See Saving and Editing Recorded Macros for more details.

  4. Specify the name for the macro in the Macro Name text box.

  5. Select any desired options. Leave the default settings if you aren't sure what to select. See Save Macro Dialog for more information on these advanced options.

  6. If you plan to use the macro frequently, we recommend creating a key binding for it. To create the key binding, click Save and Bind to Key, then see Binding Recorded Macros to Keys for more information.

Alternately, click Save. The List Macros Dialog is displayed, from which you can run the macro, edit the source, delete it, or bind it to a key sequence.

Binding Recorded Macros to Keys

To use recorded macros most effectively, create key bindings for them so they can be executed quickly when you want to use them. Macros can be bound through the Key Bindings option screen (see Binding Macros Using the Key Bindings Option Screen), or by using the instant "stop recording and bind" method associated with the execute_last_macro_key command (see Binding Macros Using execute_last_macro_key).

Binding Macros Using the Key Bindings Option Screen

To create a key binding for a recorded macro, you can either click the Save and Bind to Key button on the Save Macro dialog that appears automatically after you end recording, or at any time you can use the Bind to Key button on the List Macros Dialog (Macro → List Macros or list_macros command). Clicking either button displays the Key Bindings option screen with the macro selected, so you can add a key binding.

Note

You can also display the Key Bindings option screen by clicking Window → SlickEdit Preferences → Keyboard → Key Bindings, or by using the gui_keybindings command. However, if you display the screen in this manner, it will show a list of all commands and user-recorded macros. To view your recorded macros, click on the Recorded column header to sort and display items with a "Yes" (which indicates these are recorded macros). A more convenient method is to use the Bind to Key button on the List Macros dialog to only show recorded macros on the Key Bindings option screen.

Creating bindings for recorded macros works the same as creating bindings for SlickEdit Core commands. Click Add to initiate the binding, then specify the key sequence or mouse event to use. See Managing Bindings for more information about creating, editing, and removing bindings.

Binding Macros Using execute_last_macro_key

The execute_last_macro_key command provides functionality to stop macro recording and instantly bind the macro to a key sequence. This feature is convenient for recorded macros you want to use perhaps immediately or one-time only, and don't need to track. It allows you to keep a set of recent, unnamed macro recordings instead of having just one "last recorded macro", similar to a feature provided by early text editors that supported macro recording, such as the EVE and Edt editors on the Vax (VMS).

Unlike other SlickEdit Core commands that we document, execute_last_macro_key is not intended to be used on the command line®instead, you use a key binding that is automatically assigned when you press it to stop macro recording.

To bind a macro to a key sequence using this method, start recording the macro and enter the keystrokes you want to record. Then press Ctrl+Shift+F12,key where key stands for keys 0 through 9, A-Z, or F1-F12, to stop recording the macro and instantly bind it to the key sequence you just pressed.

Note

The prefix key sequence Ctrl+Shift+F12 works in all emulations except SlickEdit Core text mode edition. In that emulation, the prefix key sequence is Ctrl+Shift+T.

Each macro that you record and bind using this feature is saved to a new file named lastmac<key>.e, located in your configuration directory, where <key> matches the key you used when creating the binding (keys 0-9, A-Z, or F1-F12). These files can be helpful for determining what was recorded, because if you use this method to bind a recorded macro, you will not have an opportunity to name the macro or see a list of macros created with this method (they will not appear in the List Macros dialog or Key Bindings option screen).

Running a Recorded Macro

If you have saved the macro and created a key binding for it, the easiest way to run it is to simply press the associated key sequence. You can also run it by:

  • Typing the name of the macro in the SlickEdit® Core command line then pressing Enter.

  • Using the List Macros Dialog (Macro → List Macrosor list_macros command)'select the macro and click Run.

You can run the last macro that you recorded, whether it was saved or not, by clicking Macro → Execute last-macro (Ctrl+F12 or execute_last_macro command).

Saving and Editing Recorded Macros

When a recorded macro is saved, the source code of the macro is appended to the vusrmacs.e user macros file located in your configuration directory.

To edit a macro that has previously been recorded and saved, from the main menu, click Macro → List Macros (or use the list_macros command) to display the List Macros Dialog. The list box on the left displays a list of your recorded macros. Select the macro you want to edit, then click Edit. The vusrmacs.e file opens in the editor. Save the file when you're done making edits.

If you are using recorded macros to discover Slick-C® code (see Using Macros to Discover and Control Options), you can view/edit the source of a macro that you have just recorded but have not yet saved. After creating a new recorded macro, you are prompted with the Save Macro Dialog. Instead of naming the macro and saving it, click Edit (or press Alt+E) to view the source. Aneweditor window named lastmac.e, which is the name of the file that contains the source of the last macro that was recorded, is opened showing the macro's source code. If you make edits, you will need to save the changes by clicking Macros → Save last-macro. The Save Macro dialog is displayed again so you can name the macro and then click Save, which appends the new code to the user macros file (vusrmacs.e). Or click Save and Bind to Key to save and create a key binding for the macro (see Binding Macros Using the Key Bindings Option Screen).

Each macro recorded and bound using execute_last_macro_key is saved in a file named lastmac<key>.e, and the corresponding compiled byte code is saved in lastmac<key>.ex, where <key> matches the key you used when creating the binding (keys 0-9, A-Z, or F1-F12). Both files are located in your configuration directory. To edit a macro bound using this method, open the .e file for the macro you want to edit, make and save the changes, then from the main menu, click Macro → Load Module (F12 or gui_load command). Find and select the .e file you just edited and click Open. The message Module(s) loaded appears on the message line, and SlickEdit Core will now honor the changes you made to the .e file when you use the corresponding key sequence.

Deleting Recorded Macros

To delete a macro that has been recorded and saved, from the main menu, click Macro → List Macros (or use the list_macros command). Select the macro you want to delete, and click Delete.

To delete a macro that you recorded and bound to a key sequence using execute_last_macro_key, browse to your configuration directory and delete lastmac<key>.e and its corresponding lastmac<key>.ex file, where <key> matches the key you used when creating the binding (keys 0-9, A-Z, or F1-F12).

Using Macros to Discover and Control Options

Recording macros provides a good starting point for discovering variables in Slick-C® code that control the behavior of SlickEdit® Core.

Since responses to dialog boxes (such as when you select/clear options) are recorded as Slick-C source, you can use recorded macros to discover and change these variables quickly. For example, perhaps you frequently switch line insert styles. The Line insert style option is located on the General Editing options page. Instead of every time clicking Window → SlickEdit Preferences → Editing → General, then selecting the option, you can record those steps as a macro and bind it to a key sequence. Now you have an easy way to toggle a feature on and off.

You can also view the source of a recorded macro without naming or saving it, if you just want to see the code. See Saving and Editing Recorded Macros for more information.