On Jan 27, 12:50 pm, "Edward K. Ream" <[email protected]> wrote:

I believe I can now demonstrate that the problem is in Qt.  Change
clearselect as follows:

def clearselect():

    # w.textCursor().removeSelectedText() # works

    # Leo's approach
    if 1:
        # The "real" approach.  We select a position "by hand"
        c = w.textCursor()
        i,j = c.selectionStart(),c.selectionEnd()
        print('one',i,j)
        c.setPosition(i)
        moveCount = abs(j-i)
        c.movePosition(c.Right,c.KeepAnchor,moveCount)
        w.setTextCursor(c)
        i,j = c.selectionStart(),c.selectionEnd()
        print('two',i,j)
        c.removeSelectedText() # or w.setPlainText--the result is the
same.
    else:
        text = unicode(w.toPlainText())
        c = w.textCursor()
        i,j = c.selectionStart(),c.selectionEnd()
        w.setPlainText(text[:i]+text[j:])

We are intending to use c.movePosition to create a selection.  This is
essential, in general, because many of Leo's commands create the
selection.  That is, we can't rely on an already-existing selection.

We expect that the selection reported by the two statements:

    print('one',i,j)
    print('two',i,j)

will be identical.  Indeed, they are for most characters, but not for
the problematic characters. In that case the second j is one or more
too big.  So there are only two choices:

1. There is something wrong with the following that is hidden in plain
sight:

    c = w.textCursor()
    c.setPosition(i)
    moveCount = abs(j-i) # can't be off-by-one, because it almost
always works.
    c.movePosition(c.Right,c.KeepAnchor,moveCount)
    w.setTextCursor(c)

2. There is a bug in c.movePosition.

I see nothing wrong with the code in 1, especially considering that it
usually works.  The demo does not know anything, and in general can
not know anything, about the byte indices underlying the calculations
performed by c.movePosition.

Edward

P.S.  The code above might be dubious for right-to-left scripts, but
we aren't dealing with that here.

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en.

Reply via email to