Home > Archive > Tcl > December 2007 > text widget: unexpected delete
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
text widget: unexpected delete
|
|
| koloska 2007-12-17, 7:21 pm |
| Hello,
before I file a bug report, I want to check if this really is a bug.
To demonstrate the unexpected behaviour, use this code
pack [text .t]
.t insert end "1234\n5678"
now you have two lines of text with the insertion cursor just after
the last inserted character. Now I want to delete the second (and
last) line:
.t delete 2.0 end
and what I expect is, that the insert cursor after this operation
remains at the start of the second line. But it lingers at the end of
the first one ...
It works fine if I instead of the index 'end' use '2.end'.
So what is unexpected in my opinion is, that a delete operation
deletes before the given index.
.t delete 1.0 end
.t insert end "123\n4"
.t delete 2.0
only deletes the '4' but now
.t delete 2.0
deletes the lineend of the first line!
I am stymied -- is this a bug or a feature? (I have tested with 8.5b1
and 8.4.11 on linux)
Thanks
Uwe Koloska
| |
| Bryan Oakley 2007-12-17, 7:21 pm |
| koloska wrote:
> Hello,
>
> before I file a bug report, I want to check if this really is a bug.
> To demonstrate the unexpected behaviour, use this code
>
> pack [text .t]
> .t insert end "1234\n5678"
>
> now you have two lines of text with the insertion cursor just after
> the last inserted character. Now I want to delete the second (and
> last) line:
>
> .t delete 2.0 end
>
> and what I expect is, that the insert cursor after this operation
> remains at the start of the second line. But it lingers at the end of
> the first one ...
>
> It works fine if I instead of the index 'end' use '2.end'.
>
> So what is unexpected in my opinion is, that a delete operation
> deletes before the given index.
>
> .t delete 1.0 end
> .t insert end "123\n4"
> .t delete 2.0
>
> only deletes the '4' but now
> .t delete 2.0
> deletes the lineend of the first line!
>
> I am stymied -- is this a bug or a feature? (I have tested with 8.5b1
> and 8.4.11 on linux)
I think it's a feature, and the rationalization is this:
when you say "delete 2.0 end", you are deleting the trailing newline.
Thus, the \n at the end of 1.0 becomes the final trailing newline.
Because the original insert was at "end" and the insertion cursor can't
be after the final trailing newline it is placed just before that newline.
If, however, you do "delete 2.0 end-1c", it works as you expect, as does
"delete 2.0 2.end", because you don't delete the final trailing newline.
If you read between the lines, this is documented with the following
statements in the man page:
"pathname insert index chars ?tagList chars tagList ...? ...If index
refers to the end of the text (the character after the last newline)
then the new text is inserted just before the last newline instead"
....
"It is not allowable to delete characters in a way that would leave the
text without a newline as the last character"
If you can't delete the trailing newline, and an insert at the end
happens before the trailing newline, it makes sense that the insertion
cursor appears before the trailing newline since that is where the
insert will take place.
--
Bryan Oakley
http://www.tclscripting.com
| |
| koloska 2007-12-17, 7:21 pm |
| Hello Brian,
thank you for this explanation!
On 17 Dez., 15:50, Bryan Oakley <oak...@bardo.clearlight.com> wrote:
> I think it's a feature, and the rationalization is this:
>
> when you say "delete 2.0 end", you are deleting the trailing newline.
> Thus, the \n at the end of 1.0 becomes the final trailing newline.
> Because the original insert was at "end" and the insertion cursor can't
> be after the final trailing newline it is placed just before that newline.
And this only goes with
.t delete 2.0
if the second line is empty. Then the last newline will be deleted and
cause there must be a trailing newline the insertion cursor is placed
just before the remaining newline.
> If, however, you do "delete 2.0 end-1c", it works as you expect, as does
> "delete 2.0 2.end", because you don't delete the final trailing newline.
So if I want to delete just one character I have to test wether there
is at least one character?
If not a bug, it is unexpected that the last newline is deleted and
the insertion cursor placed before an already existing newline ...
What I expect:
- there is always a newline at the end of text
- I am not allowed to delete this trailing newline
- any newline I have inserted into the text are not this last one
So if I have
.t delete 1.0 end
.t insert end "1\n\n"
I expect the text containing "1\n\n" with the last \n being the one
that has to be there. So if I do
.t delete 2.0
nothing is deleted because the last newline is not allowed to be
deleted.
But what happened, if I have understand you correctly, is that the
last newline will be deleted and because there have to be a final
newline the cursor is repositioned ...
Hmm very crazy and my poor brain goes round and round and round ...
Yours
Uwe Koloska
| |
| Bryan Oakley 2007-12-17, 7:21 pm |
| koloska wrote:
> ...
> But what happened, if I have understand you correctly, is that the
> last newline will be deleted and because there have to be a final
> newline the cursor is repositioned ...
>
But the cursor is *not* repositioned. At least by my way of interpreting
the situation. The cursor was just before the trailing newline before
the delete, and it was just before the trailing newline after the delete.
Admittedly this is a bit of a special case and I think you'll just have
to accept the fact that tcl handles that very last newline a bit specially.
> Hmm very crazy and my poor brain goes round and round and round ...
What is the real problem you are having? Sometimes when people show
contrived examples it doesn't accurately reflect the real problem they
are having and I suspect this is one of those cases.
What is it you are trying to do that is being affected by this behavior?
--
Bryan Oakley
http://www.tclscripting.com
| |
| koloska 2007-12-17, 7:21 pm |
| Hello Bryan,
excuse me for mistyping your name ...
Bryan Oakley schrieb:
> What is the real problem you are having? Sometimes when people show
> contrived examples it doesn't accurately reflect the real problem they
> are having and I suspect this is one of those cases.
>
> What is it you are trying to do that is being affected by this behavior?
The quest for this for me unexpected behavior was from the bug I have
found in my autocomplete package http://wiki.tcl.tk/20491. There a
word immediately after a newline has deleted the newline before it
when it was substituted by the auto completed variant.
To find the erroneous code I had to examine what happens and find this
(for me) unexpected behaviour. The last typed word is found by finding
the wordstart and wordend from the current cursorposition with the tag
modifiers.
So this was really a real example ;-)
And to be true: I don't really know, why the code is working now --
but it _is_ cleaner and it works ;-)
Maybe I have deleted one character more then the lenght of the word?
Anyway: I don't understand the reason for this next example even with
your concise description.
.t delete 1.0 end
.t insert end "123\n"
.t delete 2.0
Why is there anything deleted? In fact the first newline?
The index 2.0 points at the trailing newline. This is not allowed to
be deleted. So why delete anything at all?
Yes, in the forward direction the situation for the insert cursor is
the same -- there is the trailing newline, just as before. But
backwards a newline has vanished -- and this in my oppinion is not
correct.
And for real world uses: If I delete characters with the delete Key
(as opposed to backspace) it always deletes to the right but for the
last empty line: there it deletes to the left.
Yours truly
Uwe Koloska
| |
| koloska 2007-12-17, 7:21 pm |
| Hello Bryan,
excuse me for mistyping your name ...
Bryan Oakley schrieb:
> What is the real problem you are having? Sometimes when people show
> contrived examples it doesn't accurately reflect the real problem they
> are having and I suspect this is one of those cases.
>
> What is it you are trying to do that is being affected by this behavior?
The quest for this for me unexpected behavior was from the bug I have
found in my autocomplete package http://wiki.tcl.tk/20491. There a
word immediately after a newline has deleted the newline before it
when it was substituted by the auto completed variant.
To find the erroneous code I had to examine what happens and find this
(for me) unexpected behaviour. The last typed word is found by finding
the wordstart and wordend from the current cursorposition with the tag
modifiers.
So this was really a real example ;-)
And to be true: I don't really know, why the code is working now --
but it _is_ cleaner and it works ;-)
Maybe I have deleted one character more then the lenght of the word?
Anyway: I don't understand the reason for this next example even with
your concise description.
.t delete 1.0 end
.t insert end "123\n"
.t delete 2.0
Why is there anything deleted? In fact the first newline?
The index 2.0 points at the trailing newline. This is not allowed to
be deleted. So why delete anything at all?
Yes, in the forward direction the situation for the insert cursor is
the same -- there is the trailing newline, just as before. But
backwards a newline has vanished -- and this in my oppinion is not
correct.
And for real world uses: If I delete characters with the delete Key
(as opposed to backspace) it always deletes to the right but for the
last empty line: there it deletes to the left.
Yours truly
Uwe Koloska
| |
| keithv 2007-12-17, 10:21 pm |
| On Dec 17, 9:50 am, Bryan Oakley <oak...@bardo.clearlight.com> wrote:
> koloska wrote:
>
>
>
>
>
>
>
>
>
>
>
> I think it's a feature, and the rationalization is this:
>
> when you say "delete 2.0 end", you are deleting the trailing newline.
> Thus, the \n at the end of 1.0 becomes the final trailing newline.
> Because the original insert was at "end" and the insertion cursor can't
> be after the final trailing newline it is placed just before that newline.
FWIW, I vaguely remember fighting a real ugly battle with an
early version of the text widget (circa 1996) which didn't have
indestructible trailing newline. I don't remember the exact
details anymore, but not having that newline made my task a
real XXXXX. I remember porting the program to use the new
text widget and getting to delete a lot of code.
Keith
| |
| Koen Danckaert 2007-12-18, 8:16 am |
| On Dec 17, 4:27 pm, koloska <kolo...@voiceinterconnect.de> wrote:
> So if I want to delete just one character I have to test wether there
> is at least one character?
>
> If not a bug, it is unexpected that the last newline is deleted and
> the insertion cursor placed before an already existing newline ...
Yes, it is in fact a (small) bug. It's easiest to see this in
interactive mode: create a text widget, and press <Return> a few times
to create some empty lines. Now, with the cursor still at the end of
the text, press <Delete> a few times. You will see that the cursor
goes up again, which it shouldn't do since <Delete> is supposed to
delete only characters after the cursor.
--Koen
| |
| Koen Danckaert 2007-12-20, 10:13 pm |
| On Dec 18, 12:03 pm, Koen Danckaert <koen.n...@gmail.com> wrote:
> On Dec 17, 4:27 pm, koloska <kolo...@voiceinterconnect.de> wrote:
>
>
>
> Yes, it is in fact a (small) bug. It's easiest to see this in
> interactive mode: create a text widget, and press <Return> a few times
> to create some empty lines. Now, with the cursor still at the end of
> the text, press <Delete> a few times. You will see that the cursor
> goes up again, which it shouldn't do since <Delete> is supposed to
> delete only characters after the cursor.
>
> --Koen
I've submitted a patch for this: #1854913.
(https://sourceforge.net/tracker/index.php?
func=detail&aid=1854913&group_id=12997&atid=312997)
--Koen
|
|
|
|
|