The ContextMenu Package
The ContextMenu package is a suite of tools for controlling and customizing the display of context-sensitive menus in Maple. When a user selects a Maple expression, a menu appears in the Context Panel with a customized set of actions that can be performed on this expression. This is a context-sensitive menu. The user may then choose one of the listed actions and the associated command is invoked on the selected object. With the ContextMenu package, you have the option of building an entirely new set of context-sensitive menus or adding to the existing set of menus. The package enables you to add entries to context menus, disable and re-enable existing menu entries, set the criteria governing when an entry should appear, and programmatically test the behavior of your customized menu system.
with(ContextMenu);
CurrentContext,Install,New,Save,Test
The following is a brief introduction to some of the key features of the ContextMenu package. For more information, consult the ContextMenu help page.
Building a Context Menu module
The information necessary for building a set of context-sensitive menus is encapsulated in a context menu module. This is a Maple module with a specific set of package commands for adding to and customizing the menu.
The built-in context menu module is accessible as ContextMenu[CurrentContext] .
type( CurrentContext, `module` );
true
To create a customized context menu system, you must begin by creating a context menu module. This can be done in either of two ways.
First, you can create a brand new, empty module with the ContextMenu[New] command.
CM1 := New():
type(CM1, `module`);
Alternatively, you can make a copy of an existing context menu module and add to the entries already present, with the module's Copy command. The following example illustrates how to make a copy of the Maple built-in context menu module, ContextMenu[CurrentContext]:
CM2 := CurrentContext[Copy]():
type(CM2, `module`);
Customizing a Context Menu module
Adding An Entry
Having created a context menu module, you naturally want to customize it. Adding an entry to the context menu with the Add command is quite easy.
CM1[Entries][Add]( "Multiply by 2", "2 * %EXPR", numeric);
0
The above entry has added an entry "Multiply by 2" to the set of entries in the context menu module CM1. The third parameter, numeric, indicates that this menu entry should appear in the context menu only if the object that was selected had this type. The second parameter describes the action (multiplication by 2) that should be printed in the worksheet if this entry is selected. The symbol %EXPR is a special placeholder for the selected object.
Submenus and Batch Additions
It is useful to group related menu actions in a submenu. You can specify a submenu with the optional submenu parameter to Add. The following places the "List To Set" menu entry in a submenu, which is accessible from the top-level menu by the name "Conversions".
CM1[Entries][Add]( "List To Set", "convert(%EXPR, set)", list, submenu=["Conversions"] ):
However, when filling a submenu with several entries, it becomes tedious to supply the submenu parameter each time, so the tool to use is AddMultiple. This allows you to add several menu entries at once while passing the same optional parameters to each.
CM1[Entries][AddMultiple]( ["List To Array", "Array( %EXPR )", list], ["List To Expression Sequence", "op(%EXPR)", list], ["List To Product", "convert(%EXPR, `*`)", list], ["List To Sum", "convert(%EXPR, `+`)", list], submenu=["Conversions"] ):
Installation, Use, and Saving
Having customized your context menu module, you can now install it as the active menu system with the
ContextMenu[Install] command. After installation, your customized menus should appear when
selecting Maple output.
Install(CM1);
Test that the context menu has been properly installed by selecting the output of the following commands.
2.7182818;
2.7182818
[1, 3.0, q, Pi, 7/2, {a,c}];
1,3.0,q,π,72,a,c
Once you have customized your context menu system, you can save it to a Maple repository for later access. This is accomplished with the ContextMenu[Save] command.
Note: For the following example to work, the directory "/home/maple/lib" must be replaced with the name of lib directory of the Maple installation or with the name of a directory containing a writable Maple archive, see ?march.
Save(CM1, "/home/maple/lib");
Further Customization: Inside a Context Menu module
To learn more of the features offered by the ContextMenu package, it is necessary to acquaint yourself with the structure of a context menu module.
with(CM1);
Categories,Copy,Entries,EntryGenerators,HandleExpression,Queries
Entries
The most important part of a context menu module is, understandably, its collection of menu entries. The Entries subpackage of a context menu module stores the entries to be displayed together with their criteria for display. The most frequently used commands are Add and AddMultiple , which permit new entries to be added to the context menu system.
exports(CM1[Entries]);
Add,AddMultiple,Disable,Enable,Get,Find,Remove
Also useful are Disable, which prevents an entry from appearing in a context menu until explicitly re-enabled, and Enable, which enables or re-enables a disabled entry.
CM1[Entries][Disable]( "Multiply by 2" ); CM1[Entries][Disable]( ["Conversions", "List To Sum"] );
CM1[Entries][Enable]( ["Conversions", "List To Sum"] );
Queries
A query is a Maple procedure that retrieves some piece of information about the selected object. Queries are generally used to specify additional tests, beyond the type check, for a menu entry to pass in order to be displayed. However, they may also be used by other queries and by entry generators. The most frequently used commands in the Queries subpackage are Add and Run.
CM1[Queries][Add]( "Variables", proc(e) indets(e, 'name') end proc );
CM1[Queries][Add]( "NumVariables", proc(e) nops(CM1:-Queries:-Run("Variables", e)) end proc );
EntryGenerators
When writing context-sensitive menus, it is frequently necessary to request some piece of information from the user that is dependent on the selected expression in some way. For example, given the expression x*y, if you select a menu entry Differentiate, you might expect to be asked to choose which of the two variables to differentiate by.
This functionality is provided by the EntryGenerators subpackage. Essentially, an entry generator is a procedure that accepts an expression and returns a list of menu entries. The following demonstrates the creation and use of an entry generator. Like the placeholder %EXPR, you can specify placeholders for values returned by an entry generator with the names %ARG1, %ARG2, ....
CM1[EntryGenerators][Add]( "VariableList", proc(e) local x; [seq([ convert(x, 'string'), [x] ], x=indets(e, 'name'))] end proc );
CM1[Entries][Add]( "Differentiate", "diff( %EXPR, %ARG1 )", algebraic, entry_generator = "VariableList" ):
You can test that the entry generator above works properly by selecting the output below.
alpha*sin(omega*x+y);
α⁢sin⁡ω⁢x+y
See Also
ContextMenu, ContextMenu[CurrentContext], ContextMenu[Install] , ContextMenu[New]
Return to Index for Example Worksheets
Download Help Document