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


:: >codepoint-index ( str utf16-index -- codepoint-index )
    0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
    
:: >utf16-index ( str codepoint-index -- utf16-index )
    0 codepoint-index str subseq utf16n encode length 2 / >integer ;


        gadget earlier-caret/mark dup
        gadget preedit-start<<
        0 1 2array v+ gadget preedit-end<<
    ] unless

    gadget preedit? [
        gadget remove-preedit-text
    ] when

    gadget earlier-caret/mark dup
    gadget preedit-start<<
    0 str length 2array v+ gadget preedit-end<<
