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

3.5 getExpressionSelection

D2Editor:getExpressionSelection()

Returns three values: the contents of the text editor as a UTF-8 encoded string, the cursor position as an integer, and the selection start as an integer.

Usage

Cursor and selection positions are the borders between characters, not the position of the characters. The following code snippets serve as examples.

Listing 3.2: Example 1 for getExpressionSelection()

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

-- The getExpressionSelection() are results are:
str = 'This is a test string to see it working.'
pos = 16
-- (right before the 's' in "string")
sel = 28 -- (between the two e's in "see")

Listing 3.3: Example 2 for getExpressionSelection()

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

-- The getExpressionSelection() are results are:
str = 'This is a test string to see it working.'
pos = 28
-- (between the two e's in "see")
sel = 16 -- (right before the 's' in "string")

Introduced in platform.apiLevel = '2.0'