         underlines first first 2 2array 1array  ! thickness: 2
     ] [ underlines ] if ;
 
-:: update-marked-text ( gadget str range -- )
+:: update-marked-text ( gadget str selectedRange replacementRange -- )
+    replacementRange location>>  NSNotFound = not     ! [ 
+    replacementRange length>> NSNotFound = not and [  ! erase this line
+        gadget editor-caret first
+        dup gadget editor-line
+        [ 
+            replacementRange length>> ! location>>
+            >codepoint-index
+            2array gadget set-caret
+        ] [
+            replacementRange length>> 1 + ! [ location>> ] [ length>> ] bi +
+            >codepoint-index
+            2array gadget set-mark
+        ] 2bi
+        gadget earlier-caret/mark dup
+        gadget preedit-start<<
+        0 1 2array v+ gadget preedit-end<<
+    ] when
+
     gadget preedit? [
         gadget remove-preedit-text
     ] when
+    
     gadget earlier-caret/mark dup
     gadget preedit-start<<
     0 str length 2array v+ gadget preedit-end<<
     str gadget temp-im-input drop
     gadget preedit-start>>
-    0 str range location>> >codepoint-index
-    2array v+
+    0 str selectedRange location>> >codepoint-index 2array v+
     dup gadget preedit-selected-start<<
     0
-    range location>> dup range length>> + [ 2 * ] bi@
-    str utf16n encode subseq utf16n decode length
+    selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
+    [ str swap >codepoint-index ] bi@ -
     2array v+
     dup gadget preedit-selected-end<<
-    dup gadget set-caret gadget set-mark                    
+    dup gadget set-caret gadget set-mark          
     gadget preedit-start>> gadget preedit-end>> = [
         gadget remove-preedit-info 
     ] when ;

            self window :> window
            window [
                window world-focus :> gadget
                gadget support-input-methods? [
                    gadget preedit? [
                        gadget preedit-start>> first gadget editor-line :> str
                        str
                        gadget
                        [ preedit-selected-start>> second ]
                        [ preedit-start>> second ] bi - >utf16-index              ! location
                        str gadget preedit-selected-end>> second >utf16-index
                        str gadget preedit-selected-start>> second >utf16-index - ! length
                    ] [ gadget earlier-caret/mark second 0 ] if
                ] [ 0 0 ] if
            ] [ 0 0 ] if 
            <NSRange>
        ] ;
    
    METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
                                     replacementRange: NSRange replacementRange [           
            self window :> window
            { } clone :> underlines!
            window [
                window world-focus :> gadget
                "" clone :> str!
                text NSString -> class -> isKindOfClass: 0 = not [
                    text CF>string str!               
                ] [
                    text -> string CF>string str!               
                    gadget support-input-methods? [
                        gadget text selectedRange make-preedit-underlines underlines!
                    ] when
                ] if
                gadget support-input-methods? [
                    gadget str selectedRange update-marked-text                   
                    underlines gadget preedit-underlines<<
                ] when
            ] when            
        ] ;

            self window :> window
            window [
                window world-focus :> gadget
                gadget [
                    gadget preedit? [
                        gadget [ preedit-start>> second ] [ preedit-end>> second ] bi >= [ 
                            NSNotFound 0 
                        ] [
                            gadget preedit-start>> first gadget editor-line :> str
                            gadget
                            [ preedit-start>> second ]                 ! location
                            [ preedit-end>> second ]
                            bi [ str swap >utf16-index ] bi@ over -    ! length
                        ] if
                    ] [  NSNotFound 0 ] if
                ] [  NSNotFound 0 ] if
            ] [ NSNotFound 0 ] if
            <NSRange>
