Tk Source Code

View Ticket
Login
Ticket UUID: dd9667635d4d286858b1237835502a1d8bd07a82
Title: text anchor not set
Type: Bug Version: 8.6.7
Submitter: anonymous Created on: 2017-09-19 23:08:16
Subsystem: 18. [text] Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2017-10-11 06:37:38
Resolution: Fixed Closed By: fvogel
    Closed on: 2017-10-11 06:37:38
Description:
On text widget with an amount of bindings, creation of the anchor mark for selections (anchor name due to the ::tk::TextAnchor command) can be done automatically too late. I got an error on that when moving cursor after selecting whole text:

bad text index "tk::anchor1"
bad text index "tk::anchor1"
    while executing
"$w compare $new < $anchorname"
    (procedure "tk::TextKeySelect" line 11)
    invoked from within
"tk::TextKeySelect .p.t.content [tk::TextUpDownLine .p.t.content -1]"
    (command bound to event)

Solution for this is creating anchor mark for each text widget (e.g. immediately after creating text widgets); using it like:

set anchor_name [tk::TextAnchor $wdg]
if {!($anchor_name in [$wdg mark names])} {
  $wdg mark set $anchor_name "1.0"
}

The main issue here is that documentation did not warn me that anything like this may happen.
User Comments: fvogel added on 2017-10-11 06:37:38:

[40829bd17b] strives to keep the same test names in all branches:

1. rename test 38.15 to become 37.1 (same name for this test now in revised_text as in core-8-6-branch and trunk)

2. shift all tests 38.* -> 39.*, and 37.* -> 38.* in revised_text branch (these are tests present only in the revised_text branch).


fvogel added on 2017-10-07 16:01:11:

trunk branch now merged to revised_text branch.

The bug was not present in revised_text, therefore I didn't merge the fix itself (the added code in text.tcl, see [ba5ef805]), but I merged the added test 37.1 (renamed to 38.15 in revised_text branch).


fvogel added on 2017-10-07 15:12:37:
Merged to core-8-6-branch and trunk.

fvogel added on 2017-09-30 17:35:50:

dgp: Does [eb1000ea0e] answer your request?


dgp added on 2017-09-28 18:18:19:
Some changes to the test suite surrounding this bug
have changed the way the [bgerror] command is manipulated.

Please consider revising these tests to stop using [bgerror]
and use the 8.5+ [interp bgerror] facility instead.

kvutza added on 2017-09-24 20:20:04:
Thanks, I have it currently via use of [tk::TextAnchor $wdg], being put in a catch block, since I was not sure that it will remain valid (exactly as you warn me).
I'll probably switch it to the use of Control-space at some point, according to your advice.

fvogel added on 2017-09-24 19:55:13:

In both the legacy widget and the revised version, there is no public interface to the selection anchor, except that the Select key and Control-Space move it to the insert cursor position (this is documented). There is no way I know of to move the selection anchor elsewhere then at the insert cursor place, at least when using a public interface of the text widget.

However, in both versions, the selection anchor of a text widget named $w can be set at $newAnchorIndex through:

  $w mark set [tk::TextAnchor $w] $newAnchorIndex
This is similar to what <Control-space> is bound to. Warning: This uses calls private to Tk. Use at your own risks, since there is no guarantee that in the future the selection anchor will still be managed through a mark, or that [tk::TextAnchor] will still exist.

Something safer, using only public features, for the use case you describe would be:

  - save the current insert cursor place
  - move insert cursor where you want the selection anchor to be
  - event generate $w <Control-space>
  - restore the insert cursor at its saved position


kvutza added on 2017-09-24 19:02:44:
My reason for having some access to the anchor (whatever way it is implemented) is for ability to set/adjust it. E.g. after doing some select-all action, I prefer to have the fixed-end anchor to be on the selection end that is further from current insertion cursor. Otherwise next actions:

* having cursor on a text end
* making select-all by Ctrl+something
* holding Shift key and moving cursor by a key

could lead to having (nearly) empty selection, while expecting just slightly lessened (from the all-text) selection.

And would not get the anchor placing stalled when I do stuff on mouse-clicks by myself only?

If I can state (in whatever way) in the revised text branch that I want to have the anchor (or its substitute) at some specific place, it is OK for me. I can happily live without tk::anchorN marks :-)

And to make it clear: I'm looking forward to the revised-text branch, since it contains a whole lot of new cool stuff.

fvogel added on 2017-09-24 14:43:10:
The legacy text widget uses anchor names such as tk::anchorN with N being an integer. Therefore these mark names are in the tk:: namespace, which indicates they are internal to Tk. Nevertheless these marks are returned by "mark names" et al., which is incorrect since the selection anchor being a mark is an implementation detail that should be hidden to the user.

The revised text widget however makes use of a generated mark to implement the selection anchor. Generated marks are invisible (not returned by "mark names" for instance), which makes them completely internal to Tk.

All in all I'm not conviced that how the selection anchor is internally implemented needs to be documented to the user of a text widget.

kvutza added on 2017-09-24 01:23:37:
Cool, thanks. Still having a feeling that we could get some notice in documentation that such marks do exist. Someone may even want to set them specifically; but it is probably not that important.

fvogel added on 2017-09-23 07:13:05:

Test suite a bit improved, it's now cleaner.

See bugfix branch bug-dd9667635d


fvogel added on 2017-09-22 20:28:15:

I have proposed a fix [ba5ef805] and have added a non-regression test.


fvogel added on 2017-09-22 06:31:41:

OK, I think I see what's going on here. The following snippet triggers what you see:

package require Tk
text .t
.t insert end "Hello world!"
.t tag add sel 1.0 end
event generate .t <<SelectPrevLine>>

This is not a documentation bug, I will fix it in the source code.

Thanks for the precise report!


kvutza added on 2017-09-21 22:59:00:
And one more notice on that. If I did "starting a selection from no-selection state, by holding shift key and moving the cursor by an arrow key" (that is without previous onset of selection by any means), the error cease to occur then. 

Meaning that when doing the action sequence that did lead to error previously, the error did not occur if made that above stuff in between. I expect that Tcl/Tk did implicitly put the anchorN in due to the in-between action.

kvutza added on 2017-09-21 22:50:41:
I should state what were exactly the sequences that did lead to the errors:

* Had written several text lines to a text widget.
* Pressed "Control-a" that I have bound for (removing current selection and doing) select all (had tried it for both "1.0"-to-"end" and "1.0"-to-"end - 1 chars" selections).
* Pressed shift key (I forgot to state this previously, sorry) and kept it held.
* Pressed a key with any arrow (while still holding shift key, and while the whole text was still selected).

The intended action was changing the selected area (according to the moving cursor), but once I hit the (any) arrow-key, the error popped up to me (instead of doing selection changes).

If I understand it correctly, Tcl/Tk was trying to decide how to move the volatile end of the selection (or how to change it generally). And it uses "anchorname" for denoting the fixed end of the selection.

It may be that since I had (previously) issues with astray mouse clicks (that ticket on hard-to-reach line, name=13aabdb3c1), I did put in explicit binding for mouse clicks (since that solved that line-reaching issue for me). And since at that moment I did not know about the "anchorN" stuff yet, I had no dealing with anchors there at all (while implicit Tk's text binding probably deal with that, me too now).

kvutza added on 2017-09-21 22:21:12:
Hi, when I had that issue (before making a workaround), I saved the text I got in error messages at several cases here; copying the cases below. Notice that anchor1/anchor2 is for upper/lower text widgets in the script, not actually correlated with arrow key types.


bad text index "tk::anchor1"
bad text index "tk::anchor1"
    while executing
"$w compare $new < $anchorname"
    (procedure "tk::TextKeySelect" line 11)
    invoked from within
"tk::TextKeySelect .p.t.content [tk::TextUpDownLine .p.t.content -1]"
    (command bound to event)


bad text index "tk::anchor2"
bad text index "tk::anchor2"
    while executing
"$w compare $new < $anchorname"
    (procedure "tk::TextKeySelect" line 11)
    invoked from within
"tk::TextKeySelect .p.t.content [tk::TextUpDownLine .p.t.content 1]"
    (command bound to event)


bad text index "tk::anchor1"
bad text index "tk::anchor1"
    while executing
"$w compare $new < $anchorname"
    (procedure "tk::TextKeySelect" line 11)
    invoked from within
"tk::TextKeySelect .p.t.content [.p.t.content index {insert - 1displayindices}]"
    (command bound to event)


bad text index "tk::anchor2"
bad text index "tk::anchor2"
    while executing
"$w compare $new < $anchorname"
    (procedure "tk::TextKeySelect" line 11)
    invoked from within
"tk::TextKeySelect .p.t.content [.p.t.content index {insert + 1displayindices}]"
    (command bound to event)

fvogel added on 2017-09-21 20:45:58:
It is not immediately obvious to me what is the relation between anchors and tk::TextUpDownLine that triggers on the up/down arrow keys.

Can you show the full error that you see when hitting one of those keys (please remove first your workaround from your code for this test)?

Please also show the full error when you hit a left or right arrow key. What you just said does not clearly correlate with the code I'm reading here (unless you *select*+left or right, correct?).

anonymous added on 2017-09-20 22:40:24:
I could not reproduce it on short scripts. In the affected script it occurred for any key arrow then, with "[tk::TextUpDownLine .p.t.content -1]" for upwards/downwards keys, and with "[.p.t.content index {insert - 1displayindices}]" instead of that for leftwards/rightwards keys. Notice that the script (where I got that issue) had no mentioning of "tk::TextAnchor" at that time.

It is "Minima" (a simplistic UI for "Maxima") coded by myself for myself:-)
http://minima.tangloid.net/
You'll need to have Maxima installed if you wanna try it, though just looking at the script obviously does not need it.

As for now, the error is not triggered, since I have that explicit-setting code in there. If you'll look in that script, notice that I haven't used Tcl/Tk for some ten years; used it again now, since needed to hack a UI in a fast pace. Thus I used ancient features of Tcl only. And those issues that I've reported recently here, are based on my experience with coding this script.

fvogel added on 2017-09-20 18:32:40:
Can you show us a script triggering that error?