             self window :> window
             window [
                 window world-focus :> gadget
-                gadget preedit? [
-                    gadget [ preedit-start>> second ] [ preedit-end>> second ] bi >= [ 
-                        NSNotFound 0 
-                    ] [
-                        gadget preedit-start>> first gadget editor-line :> str
-                        str gadget preedit-start>> second >utf16-index dup  ! location
-                        str gadget preedit-end>> second >utf16-index swap - ! length
-                    ] if
+                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>

            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
        ] ;

                    text -> string CF>string str!               
                ] if
                window world-focus :> gadget
                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 [
                    gadget preedit? [ 1 ] [ 0 ] if
                ] [ 0 ] if
            ] [ 0 ] if
        ] ;

