Creating Package-Specific Context Menus - Maple Help

Package-Specific Context Entries

 Overview This example outlines how to create package-specific context entries.   The Context Panel provides easy clickable access to many commands in Maple. In Maple's user interface, when your cursor is on either input or output for 2-D math expressions (or output from 1-D math expressions), context-sensitive operations can be found in the Context Panel on the right side of the screen.   In order to make sure that as many people as possible can access commands in your package, it is important to consider adding one or more of your commands to the context-sensitive infrastructure. The following example will show you how that's possible.

Getting Started

Here is a quick example of a package module that has three exports:
squared: a procedure that takes an integer and squares it

cubed: a procedure that takes an integer and cubes it

parsestring: a procedure that takes a string and attempts to parse it

Here is the output of the package exports.

 > MyPackage:-squared(3);
 ${9}$ (1.1)
 > MyPackage:-cubed(3);
 ${27}$ (1.2)
 > MyPackage:-parsestring("3");
 ${3}$ (1.3)

There are several important parts of building individual entries for the context panel.

Most importantly, you want to know:

 • WHAT to call the entry; the label to show in the context panel
 • WHICH command you want to use; the command to apply when the label is clicked
 • WHEN to show the entry; under what type conditions the option is shown
 • HOW you will help the user use it; the tooltip to help describe what the command does

Here is a short example of using the command where you add an entry to the context panel that negates a boolean value:

 > Entries[Add](   "Negate",                              #label to show   "not %EXPR",                           #command to apply   boolean,                               #Type conditions   'helpstring'="Negate the expression"): #tooltip to show

Try clicking on the output below and using the "Negate" option from the context panel.

 > true;
 ${\mathrm{true}}$ (2.1)

Using "Negate" gives the following:

 > not (2.1);
 ${\mathrm{false}}$ (2.2)

The most important part of specifying a context panel entry is determining WHEN to show it. Typically, you should try to show context-sensitive entries whenever Maple detects input that is of the same type as is expected for your routines. For example, the above module expects type integer for the procedures squared and cubed, and type string for the procedure parsestring.

Here is another example using the module above. In this example, you will add a new entry called "Square" that is shown for an integer input.

 > Entries[Add](   "Square",                              #label to show   ":-MyPackage:-squared(%EXPR)",         #command to apply   integer,                               #Type conditions   'helpstring'="Square the expression"): #tooltip to show

Click on the output below, then use the "Square" option from the context panel.

 > 3;
 ${3}$ (2.3)

Using "Square" gives the following:

 > :-MyPackage:-squared((2.3));
 ${9}$ (2.4)

Since the example for cubed will be similar to squared, you can skip that and do one more example for the parsestring procedure.

 > Entries[Add](   "Parse string",                        #label to show   ":-MyPackage:-parsestring(%EXPR)",     #command to apply   string,                                #Type conditions   'helpstring'="Parse the string"):      #tooltip to show
 > "3";
 ${"3"}$ (2.5)
 > :-MyPackage:-parsestring((2.5));
 ${3}$ (2.6)
 >

Adding Context Panel Entries to a Package

Next, add the entries for each of the procedures created above to the code for the package module. For a package implemented as a module, new and relevant context-sensitive entries can be created inside the module's ModuleLoad export; this will cause the new context panel entries to be automatically created upon the first invocation or use of the package.

Before adding the entries for the procedures to the package module, make a copy of the current context panel. You can now freely customize the copied panel.

Note: The added entries will not be visible in the Maple context panel until you execute the Install command.

Queries can be useful while determining when to show your context-sensitive items. New context panel entries can be configured so as to only appear under specific circumstances - such as when the package is loaded (using with).

 > pCM:-Queries:-Add(              "Is MyPackage Loaded",                  proc()                   member( ':-MyPackage', packages() );                end proc);

 > pCM:-Entries:-AddMultiple(                 [        "Square",                           ":-MyPackage:-squared(%EXPR)",                           integer,                           'helpstring'="Square the expression" ],                   [        "Cube",                           ":-MyPackage:-cubed(%EXPR)",                           integer,                           'helpstring'="Cube the expression" ],                 [        "Parse string",                           ":-MyPackage:-parsestring(%EXPR)",                           string,                           'helpstring'="Parse the string"],                   'submenu'=["MyPackage"],              #Add a sub-folder for MyPackage                   'active'="Is MyPackage Loaded");      #the active option tests whether some condition is met( i.e. if "Is MyPackage loaded"? );
 ${449}{,}{466}{,}{22746}$ (3.1)

The Install command adds the entries to the context panel:

Next, load the package to activate the entries:

 > with(MyPackage):
 > 3;
 ${3}$ (3.2)

Finally, you can put this together in the original procedure, creating a package that contains its own context-sensitive panel code. In order for the context-sensitive code to be available when a package is run, it is required that the code for the actions are added to the ModuleLoad section of the module. If a ModuleLoad local or export is present, then this procedure is called when the module is read from the Maple repository in which it is located (in the code below, this is done manually by calling ModuleLoad() ).

 > restart;

When MyPackage is loaded, the ModuleLoad section is initialized, making the commands available in the context-sensitive system.

 > with(MyPackage);
 $\left[{\mathrm{cubed}}{,}{\mathrm{parsestring}}{,}{\mathrm{squared}}\right]$ (3.3)
 > 3;
 ${3}$ (3.4)

Thus, clicking on 3 now shows a submenu for "MyPackage" as well as several available commands.

 > :-MyPackage:-cubed((3.4));
 ${27}$ (3.5)
 >

There are more examples that discuss package authoring and context panels in Maple, including:

 Creating a New Package Workbook Adding an Entry to the Context Menu Module