You are here: Lua Scripting Resources eGuide > Lua Scripting API Reference Guide > 2D Editor Library > setExpression

3.16 setExpression

D2Editor:setExpression(text[, cursor[, selection]])

Sets the text content of the text editor. The cursor position is set to 1 (beginning of text), -1 (end of text), or a value from 1 to the text length plus 1. Text can be selected by specifying a selection index that indicates the end of the selection. If the selection = -1, no text is selected. If the cursor < -1 or selection < -1, an error is returned. If unspecified, both the cursor and the selection start default to -1. Returns the text editor object.

Note

All backslashes sent to the editor must be doubled. This is in addition to the standard escape rule for special characters. As a result, the string required to get the editor to
show home\stuff\work is “home\\\\stuff\\\\work”.

Usage

Cursor and selection positions are the borders between characters, not the character positions. The following code snippet highlights the characters “string to se” and places the cursor before the ’s’ in “string”.

Listing 3.5: Example 1 for D2Editor:setExpression

str = 'This is a test string to see it working.'
d2e, error = D2Editor.newRichText():resize(100, 100)
result, error = d2e:setExpression(str, 16, 28)

2DEditor output: This is a test |string to see it working.

The following code snippet highlights the characters “string to se” and places the cursor before the second ’e’ in “see”.

Listing 3.6: Example 2 for D2Editor:setExpression

str = 'This is a test string to see it working.'
d2e, error = D2Editor.newRichText():resize(100, 100)
result, error = d2e:setExpression(str, 28, 16)

2DEditor output: This is a test string to se|e it working.

Introduced in platform.apiLevel = '2.0'