Andreas Leitgeb a écrit le 28/06/2015 22:44 :
> So, while "end" marks the very last position (after the final \n),
> insertion is coded to correct such a position, and instead insert
> just before the final \n.
Yes and (reading the man page again) it is even documented in the
"insert" command section: "[...] 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.[...]"
> Other operations like retrieving text actually can deal with this
> off-the-end "end" in that they just include the final \n if the
> given position range includes it.
Yes, correct. For instance:
package req Tk; pack [text .t] ; .t get 1.0 end
returns a \n character whereas one would naturally expect the empty
string. The empty (nothing was inserted) text widget always has a \n
in it...!
I have always thought that this internal historical requirement for a
final \n in the text widget should have exactly zero exposure to the
user at all. Even to the programmer in Tcl/Tk language this should not
be exposed. If the underlying B-tree implementation of the text widget
requires this trailing \n, then fine, but it should be completely
hidden at the Tk level. It's not the case, which leads to unexpected
quirks and hair-pulling corner cases.
Maintainers escape the real problem by *very* carefully reading the
man pages and claiming that these cases are documented. And this
statement is true, they are!
Oh well, I have just used such an escape door :-) ! But it does not
really satisfy me because it's a lazy man solution: the real man
solution should be to tackle down this \n and hide it from the Tk
programming level completely. No, forget it, I'm not ready to launch
such a revolution...! This would be huge and complex work and would
certainly break existing user code in the wild (starting with mine!).
So let's live with this more.
> The patch contains (on the "-" side) what was the reason for this
> irregularity in the first place: it was the idea that a deletion
> including the final \n is an attempt to delete a whole line, so the
> \n before the deleted block shall become the new final \n and some
> tags shifted accordingly.
Indeed. You express this original intention so clearly that I think I
will:
- add this as a comment in the source code
- add a note in the "delete" section of the text widget man page
- and as a consequence reject the patch proposed in bug [2886436fff],
by relying once again on the fact that this will now be documented
So far I did not apply this patch because I couldn't figure out who
was right: the patch submitter or the existing source code. Now the
way ahead is clearer to me.
> The problem is not really with the mapping of marks to positions,
> but with some design-decision (of keeping all lines \n-terminated)
> causing some surprise (shifting marks) in unexpected places.
Absolutely, as fully agreed above.
> So, what remains is hope that perhaps a comment be added to the
> text-docs, hinting users that end-1char is more likely the mark they
> want for "deletion to the end" than end itself.
Wilco, following your proposal in your other reply.
I suggest this discussion be followed up in the bug report:
http://core.tcl.tk/tk/tktview?name=2886436fff
along with the branch [bug-2886436fff] that I will soon create in the
fossil repository to deal with this (documentation) issue.
Thanks!
François