Creating and Editing Commands

Commands allow you to manipulate expressions. Each command defines the function that will be executed when the command is applied to the highlighted expression. Commands typically also have a shortcut key assigned to them.

Commands provide a connection between a keystroke and a function that will act on the highlighted content. Each library can have it's own list of commands. The main document can also contains definitions of commands, though creating commands in the document is less commonly used.

Commands are objects with no parameters, they have the following properties:

  • Commands are created in the Commands namespace.
  • Commands have type Command.
  • Commands have to have a Function property in order to do anything. The Function property determines which function is applied to the current highlight when the command is executed.
  • Commands can have a shortcut key assigned to them. A shortcut key as assigned to a command using the ShortcutKey property. The shortcut key can be a Key, a Keystroke or a Character object.
  • Commands can also have a CommandParameters property assigned to them. This property controls the expressions that are passed into the function defined in the Function property. See Command Parameter Objects for more details.
  • Commands can have a rendering. The rendering will determine how the command looks when it appears in a list of commands. For example, the All command shows a list of other commands.

Examples can be seen in the Command objects module under the Objects folder in the Core and Math libraries (among others). For example, the Simplify command is defined in the Core library and looks like this:

This command contains two properties. First, it has a Function property to define the function that will be called when the command is executed. Second, it contains a ShortcutKey property that defines a keystroke that will invoke the command. The shortcut key can be one of three different types of objects. It can be a Character (in the Core namespace), it can be a Key object, or it can be a Keystroke object (in the Core namespace) as in the CtrlInsert command. You can create the Character object using the single quote key. You can create the Keystroke object by creating on of the key modifiers, like Shift or Control and then typing "+" to create the Keystroke object. You can also create both the Character and Keystroke objects by typing the name of the object and selecting it from the autocomplete popup.

The Keystroke object is used when you want to include a modifier like Control. Finally, the shortcut can just be a Key object, as in the Delete command:

The Delete command also includes a CommandParameters property that controls the expression(s) passed to the DeleteCommand function when the command is executed. For details on how this works, see Command Parameter Objects

To understand how highlighted expressions are passed into the command functions and how the results are used to update the document, see Functions Executed by Commands.