                     text -> string CF>string str!               
                 ] if
                 window world-focus :> gadget
-                gadget support-input-methods? [
-                    gadget preedit? [
-                        gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
-                    ] when
-                    str gadget user-input* drop                    
-                    f gadget preedit-selection-mode?<<
-                ] [ 
-                    str window user-input
-                ] if
+                gadget [
+                    gadget support-input-methods? [                        
+                        replacementRange location>> NSNotFound = [                        
+                            gadget editor-caret first
+                            dup gadget editor-line
+                            [ 
+                                replacementRange location>> >codepoint-index
+                                2array gadget set-caret
+                            ] [
+                                replacementRange [ location>> ] [ length>> ] bi + 
+                                >codepoint-index
+                                2array gadget set-mark
+                            ] 2bi                        
+                        ] unless
+                        gadget preedit? [
+                            gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
+                        ] when
+                        str gadget user-input* drop                    
+                        f gadget preedit-selection-mode?<<
+                    ] [ 
+                        str window user-input
+                    ] if
+                ] when
             ] when
         ] ;
-    
+
     METHOD: char hasMarkedText [
             self window :> window
             window [
                 window world-focus :> gadget
-                gadget preedit? [ 1 ] [ 0 ] if
+                gadget [
+                    gadget preedit? [ 1 ] [ 0 ] if
+                ] [ 0 ] if
             ] [ 0 ] if
         ] ;
 

            self window :> window
            window [
                window world-focus :> gadget
                gadget support-input-methods? [
                    gadget preedit? [
                        gadget {
                            [ preedit-start>> second ]
                            [ preedit-end>> second ]
                            [ preedit-start>> first ] [ editor-line ]
                        } cleave subseq
                        gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
                        gadget user-input* drop
                    ] when
                    f gadget preedit-selection-mode?<<
                ] when
            ] when
        ] ;

            self window :> window
            window [
                window world-focus :> gadget
                gadget [
                    gadget support-input-methods? [
                        gadget editor-caret first gadget editor-line :> str
                        gadget preedit? [
                            str
                            gadget
                            [ preedit-selected-start>> second ]
                            [ preedit-start>> second ] 
                            bi - >utf16-index                        ! location
                            gadget
                            [ preedit-selected-end>> second ]
                            [ preedit-selected-start>> second ]
                            bi [ str swap >utf16-index ] bi@ -       ! length
                        ] [                            
                            str gadget editor-caret second >utf16-index 0
                        ] if
                    ] [ 0 0 ] if
                ] [ 0 0 ] if
            ] [ 0 0 ] if 
            <NSRange>
        ] ;
   
    METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
                                     replacementRange: NSRange replacementRange [           
            self window :> window
            window [
                window world-focus :> gadget
                gadget [
                    { } clone :> underlines!
                    "" 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 replacementRange update-marked-text
                        underlines gadget preedit-underlines<<
                    ] when
                ] when
            ] when            
        ] ;
