9.5. Adding macros to menu, assigning shortcuts

For convenience, two more directives are available for adding a macro to TrEd's User-defined menu and for assigning a keyboard shortcut to macro.

To connect the my_first_macro with a new menu item labeled My First Macro under the User-defined menu, the following directive should appear in some macro file read by TrEd:

#insert my_first_macro as menu My First Macro

Actually the words as and menu may be omitted, so it is possible to write the above directive also as:

#insert my_first_macro as My First Macro

or even

#insert my_first_macro My First Macro

To make the macro accessible via a keyboard shortcut, the #bind directive may be used. Thus, to “bind” the my_first_macro macro to say Ctrl+Alt+Del keyboard shortcut, the following line should appear somewhere in the macro file:

#bind my_first_macro to key Ctrl+Alt+Del

Again, the to and key words may be omitted.

Warning

Using the shortcut chosen here as an example is probbably not a good idea, as it is a shortcut that restarts the system on many computers. Remember, that only shortcuts which are not interpreted by the operating system or desktop environment may be assigned to TrEd's macros and work correctly.

If both the menu item and keyboard shortcut are needed for a macro, the following abbreviated form can be used:

#bind my_first_macro to key Ctrl+Alt+Del menu My First Macro

If more than one keyboard shortcuts or menu items are connected with the same macro, any of them may be used to invoke the macro. If more than one macro was assigned the same keyboard shortcut, the latest binding appearing in the macro files is used. Also, if two or more macros with the same name appear in the loaded macro files, the last one is used.

#unbind-key Ctrl+Alt+Del

The above directive may be used to remove an existing key-bindning from current binding context(s). It is especially useful for disabling certain functionality and bindings of an incuded macro file.

#remove-menu My First Macro

The above directive may be used to remove an existing menu item from current binding context(s). It is especially useful for disabling certain functionality and bindings of an incuded macro file.

When an interactively invoked macro returns, TrEd automatically activates the node referenced by variable $this, redraws displayed tress according to the value of the $Redraw variable changes file status to modified (as indicated by the state of the button) unless variable $FileChanged is altered in the way described below (both variables are definded in tred.def, see Section 9.7).

The $Redraw variable may has the following values:

file (the default)

redraw all windows displaying current file

tree

redraw all windows displaying current tree

win

redraw current window

all

redraw all windows

tie

redraw all windows if TrEd windows are tied otherwise redraw only the current one

none

disables redrawing

To prevent TrEd from considering the file modified by a specific macro, the macro must explicitly set a special variable named $FileChanged to zero.

To make things even more complicated, btred, the off-line variant of TrEd, never saves any processed file unless the variable $FileChanged has been explicitely set to 1 or -S command-line switch is used.

Note

If the state of the file was considered modified and not saved when a macro was interactively invoked, any changes to the $FileChanged variable made by the macro are ignored. Macro may force the file status to saved by setting $forceFileSaved variable to 1.

Note

For historical reasons, there exists an alias for $FileChanged named $FileNotSaved. These variables are identical.