Code Edit Region - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : CodeEditRegion

Code Edit Region

Insert a code edit region in a Standard Maple worksheet or document.

 

Description

Steps to Inserting and Collapsing the Code Edit Region

Selecting a Programming Language (MapleSim Users)

Syntax and Error Checking

Copying and Pasting Embedded Components

Syntax Templates

Importing and Exporting Code with the Code Editor

Executing Code within the Code Editor

Changing the Appearance of a Code Edit Region

Code Edit Region Properties

Description

• 

The code edit region lets you enter Maple commands and programs in one confined region. If you have MapleSim installed, Modelica syntax is also available to you.

• 

You can execute your code and then collapse the region to hide it. The editor includes many features to make it easier to write, read, and debug Maple code, such as syntax highlighting, error checking, bracket matching, command completion, automatic text wrapping, optional line number display, and automatic indenting. The editor is available for managing Startup Code, inline Code Edit Regions, and code attached to Embedded Components.

• 

When writing a program within the code edit region, you can press Enter to advance the cursor to the next line. Indenting is done automatically. After entering the first line of a new module, procedure, or if/then statement, subsequent lines are automatically indented.

• 

You can use command completion within this region to reduce the amount of typing. Invoking command completion in the code edit region is done by typing the first few characters of a Maple command in the code editor. This displays a popup list of matching commands to choose from.

• 

The code edit region can be embedded in the Standard Maple worksheet or document.

• 

You can enter or exit the Code Edit Region using up and down arrow keys.

• 

To display properties for the code edit region, click  to open the Code Edit Region context panel.

Steps to Inserting and Collapsing the Code Edit Region

1. 

To insert a code edit region, from the Maple worksheet toolbar, click the Insert Code Edit Region button ( ). Alternatively, from the Insert menu, select Code Edit Region.

2. 

Enter your program or commands. A comment may be included before the program (see step 3).

3. 

To collapse the code edit region, click the region, then in the Code Edit Region context panel, clear the check box for Expanded.  Alternatively, press Alt + C (Command + Alt + C, for Mac), or use View > Collapse Code Edit Region.
When a code edit region is collapsed, an icon displays within the worksheet or document marking the code edit region, and all but the first line of the procedure will be suppressed. The first line of the procedure displays to the right of the icon. A comment may be included before the procedure to display a descriptive heading instead of the first line of the procedure. To suppress the printing of the first line of the procedure, insert a blank line on the first line of the code edit region.

4. 

To expand the region, click the collapsed icon    , then from the Code Edit Region context panel check Expanded. Alternatively you can expand the region by selecting Expand Code Edit Region from the View menu, or by pressing Alt + X (Command + Alt + X, for Mac).

Selecting a Programming Language (MapleSim Users)

If you have MapleSim installed, you can choose between the Maple programming language or the Modelica programming language.

To select a programming language for the code editor, do the following:

1. 

Click in the code edit region.

2. 

In the Code Edit Region context panel select either Maple or Modelica from the language list.

Syntax and Error Checking

The Code Editor contains the following syntax and error checking features:

• 

Code is checked for errors continuously as you type.

• 

Syntax errors are displayed inline.

• 

Syntax Highlighting. Maple keywords and operators are colored to help spot typographical errors and identify code blocks.

• 

Bracket Matching. Typing a new bracket character or positioning the cursor next to an existing one produces a visual highlight on its counterpart.

• 

Quote Matching:  Mismatched or missing single-, double-, and back-quote characters are highlighted.

Controlling Syntax Checks

You can set the syntax checking level using Edit > Check Syntax Level or through the Display tab of the Options Dialog.  Select one of the following three options:

• 

Display only severe errors.

• 

Display severe and serious errors (default)

• 

Display warnings as well as severe and serious errors.

When you change this setting, the new level applied to all code editors in the document, including startup code.

Syntax checking is done using mint, and the syntax checking levels correspond to infolevel 1, 2, or 3 for mint.

By default, automatic syntax checking is enabled. This provides continuous syntax checking.  To turn this feature off, clear the check box Check Syntax Automatically in the Edit menu. As an alternative, perform a syntax check: From the Edit menu, select Check Syntax Now. It is recommended that you check the syntax before saving, so that your startup code doesn't prevent Maple from successfully opening. You can also turn automatic syntax checking on or off through the Options dialog.

Copying and Pasting Embedded Components

In a standard Maple worksheet, you can paste embedded components into the code editor. If you copy an embedded component from the worksheet and then paste it into any code editor (startup code, embedded component code, or a code edit region), the name of the component will be inserted (prefixed with "%", to conform to the convention for DocumentTools:-Do).

To copy and paste an embedded component into a code edit region:

1. 

In your standard Maple worksheet, select the embedded component that you want to copy.

2. 

Right-click the embedded component, then select Copy from the context menu. Alternatively, press Ctrl + C to copy the embedded component.

3. 

Right-click inside the code edit region, then select Paste from the context menu. Alternatively, press Ctrl + V to copy the embedded component.

4. 

The name of the embedded component, preceded by the % symbol, is copied to the code edit region.

 

Note: In any code editor, invoking command completion on something beginning with "%" will bring up a list of embedded components whose names start with whatever comes after the "%". (Invoking completion on "%" with no suffix will list all of the components in the worksheet.)

Syntax Templates

Syntax templates are available in the Code Editor for automatic completion of certain common programming tasks, such as if statements. The following tables outline the available templates for Maple and Modelica syntax and how to invoke them. Note that for the entries in the To Insert... column in each table, the pipe character (|) represents the position of the cursor after the template has been inserted.

To insert...

Type this...

if  |  then

    

     end if:

"if" followed by Ctrl + Shift + Space

if  | then

     

else

         

end if:

"ife" followed by Ctrl + Shift + Space

proc( | )

end proc:

"proc" followed by Ctrl + Shift + Space

module()

           export |

end module:

"module" followed by Ctrl + Shift + Space

To insert...

Type this...

block  |

 

protected

             

equation

                 

end ___;

"block" followed by Ctrl + Shift + Space

model  |

        

protected

  

equation

                        

end ___;

"model" followed by Ctrl + Shift + Space

function  |

        

protected

   

algorithm

   

end ___;

"function" followed by Ctrl + Shift + Space

connector  |

     

end ___;

"connector" followed by Ctrl + Shift + Space

package  |

   

end ___;

"package" followed by Ctrl + Shift + Space

record  |  

 

end ___;

"record" followed by Ctrl + Shift + Space

for  |  loop

 

end for;

"for" followed by Ctrl + Shift + Space

if  |  then

 

end if;

"if" followed by Ctrl + Shift + Space

if  |  then

 

else

 

end if;

"ife" followed by Ctrl + Shift + Space

if  |  then

 

elseif ___ then

           

else

 

end if;

"ifei" followed by Ctrl + Shift + Space

when  |  then

  

end  when;

"when" followed by Ctrl + Shift + Space

when  |  then

 

elsewhen ___  then

   

end when;

"whene" followed by Ctrl + Shift + Space

while  |  loop..

 

end while;

"while" followed by Ctrl + Shift + Space

loop

      |

end loop;

"loop" followed by Ctrl + Shift + Space

Importing and Exporting Code with the Code Editor

You can import code from an MPL file or other text format directly into your code editor, or export the contents of a code editor to a text file.

To import or export Maple code using the Code Editor:

1. 

Open the Code Editor by clicking the Startup Code button ( ) from the toolbar.

2. 

In the Code Editor window, select File, then Import Code (or Export Code).

3. 

Locate and specify your target file.

4. 

Click OK.

To export an entire worksheet that includes a code edit region to an MPL file, export the worksheet as Maple Input.  For details on what is maintained, see Translation of Maple Worksheets to Maple Input.

Executing Code within the Code Editor

You can execute the code inside the code edit region, whether it is collapsed or expanded.

 Executing Code in an Expanded Code Edit Region

When expanded, execute the code contained in the code edit region by doing the following:

1. 

Click anywhere inside the code edit region or select the code edit region.

2. 

Click the execute button ( ) from the toolbar.

3. 

The code in the code edit region is executed. Any output from this code is displayed in the worksheet, below the code edit region.

Alternatively,

• 

Click anywhere inside the code edit region and use Evaluate > Execute Code or any of the following key combinations:

– 

 Ctrl + E (Command + E, for Mac)

– 

 Ctrl + = (Command + =, for Mac)

– 

 Alt + Enter

Executing Code in a Collapsed Code Edit Region

When collapsed, execute the code contained in the code edit region by doing the following:

1. 

If the code edit region is collapsed, click on the code editor icon.

2. 

The code in the code edit region is executed. Any output from this code is displayed in the worksheet, below the code edit region.

 

Notes: 

• 

The option to execute code in the code edit region is not available if you select Modelica as your programming language.

• 

You can set autoexecute on a code edit region by selecting Set Selection to Autoexecute from the Evaluate menu or by right-clicking in the marker area and selecting Autoexecute->Set Selection to Autoexecute. For more information on setting the autoexecute feature, see Setting the Autoexecute Feature.

Changing the Appearance of a Code Edit Region

You can hide the border or line numbers in a code edit region.

To hide or show the border or line numbers:

1. 

Click inside the code edit region.

2. 

From the Code Edit Region Properties in the Context Panel, select or clear the check boxes corresponding to the options you want to display or hide.

3. 

Click OK.

By default, code edit regions have an auto-fit property, meaning the code edit region's width fits your Maple window and the code edit region automatically expands as lines of code are added.

If you turn off the auto-fit property, you can control the options for the width and height of the code edit region and whether automatic text wrapping is used in the Code Edit Region Properties in the Context Panel.

Code Edit Region Properties

• 

The following table describes the control and use of the code edit region options.

  

An x in the Get column indicates that the option can be read, that is, retrieved by using the DocumentTools[GetProperty] tool.

  

An x in the Set column indicates that the option can be written, that is, set by using the DocumentTools[SetProperty] tool.

  

 

Option

Get

Set

Option Type

autofit

x

x

true or false

codeLineNumbers

x

x

true or false

expanded

x

x

true or false

pixelHeight

x

x

positive integer

pixelWidth

x

x

positive integer

showBorders

x

x

true or false

type

x

 

string

value

x

x

string

visible

x

x

true or false

  

 

  

autofit 

  

Specifies if the code edit region's width fits your Maple window and the code edit region automatically expands as lines of code are added. By default, the value is true. Note: The autofit feature only stays enabled as long as you don't resize the window manually.

  

codeLineNumbers

  

Display (true) or hide (false) line numbers.

  

expanded 

  

Specifies if the code edit region component is expanded or collapsed. By default, the value is true.

  

pixelHeight 

  

The height of the expanded code edit region, in pixels. By default, the value is 500. This option is only used if autofit=false.

  

pixelWidth 

  

The width of the expanded code edit region, in pixels. By default, the value is 200. This option is only used if autofit=false.

  

showBorders

  

Display (true) or hide (false) borders.

  

type 

  

The type of the component. This returns "CodeEditRegion".

  

value

  

The contents of the code edit region.

  

visible 

  

Specifies if the code edit region component is visible. By default, the value is true.

See Also

comment

DocumentTools

DocumentTools[GetProperty]

DocumentTools[SetProperty]

Embedded Components

for

Maple Worksheet Toolbar

module

proc

Setting the Autoexecute Feature

Startup Code