KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method for WR GET WORDS in 4D Write Pro
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: May 19, 2017

Below is a method that is the 4D Write Pro equivalent to the WR GET WORDS for the old 4D Write (https://doc.4d.com/4Dv16/4D-Write/16/WR-GET-WORDS.301-3205800.en.html):

    "This command has no effect on the current selection.

    If the selection begins in the middle of a word (or between the last character of a word and the next following space), beginSel will return the position of the first character of that word.
    If the selection ends in the middle of a word, there are two possible cases:
    If the word is followed by a space, endSel will include the space and smartCutPaste will return 1.
    If the word is not followed by a space, endSel will include the last character of the word and smartCutPaste will return 0."

The result will be in the new rangeObj format which contains 3 private read-only attributes (wk range start, wk range end and wk range owner).
// Util_WP_GET_WORDS
//
// Utility Method to be used with 4D Write Pro area to return
// range object of full words based on selection or cursor position
//
// Parameters:
// $1 - Pointer to 4D Write Pro Area
//
// Output
// $0 - A range object containing the start and end position of the full words in the 4D Write Pro area
// look at 4D Write Pro Documenation for more information on this object
//

C_POINTER($1;$WP_areaPtr)
C_OBJECT($0)

C_LONGINT($rangeStart)
C_LONGINT($rangeEnd)
C_LONGINT($foundStart)
C_LONGINT($foundLen)
C_LONGINT($foundStart2)
C_LONGINT($foundLen2)
C_TEXT($regexPatternStart)
C_TEXT($regexPatternEnd)
C_TEXT($fullText)
C_OBJECT($selectionObj)

$WP_areaPtr:=$1

$regexPatternStart:="(\\S|\\s)*\\W"
$regexPatternEnd:="\\S*\\w"

$selectionObj:=WP Get selection($WP_areaPtr->)

$rangeStart:=OB Get($selectionObj;"rangeStart")
$rangeEnd:=OB Get($selectionObj;"rangeEnd")

$fullText:=ST Get plain text($WP_areaPtr->)

Match regex($regexPatternStart;Substring($fullText;1;$rangeStart);1;$foundStart;$foundLen)
Match regex($regexPatternEnd;Substring($fullText;$rangeEnd;Length($fullText)-$rangeEnd);1;$foundStart2;$foundLen2)

$0:=WP Get range($WP_areaPtr->;$foundStart+$foundLen;$rangeEnd+$foundLen2)

To use the command, simply place the cursor in a word or highlight some text then pass the 4D Write Pro area's pointer to the method to get the rangeObj back.

The following example will obtain the range of the words where the cursor is placed or highlight is placed into the rangeObj, then the rangeObj is used with WP SELECT to highlight the full word(s):
C_POINTER($WP_areaPtr)

$WP_areaPtr:=OBJECT Get pointer(Object named;$WP_areaName)
$words:=Util_WP_GET_WORDS ($WP_areaPtr)
WP SELECT($WP_areaPtr->;$words)