Tk Source Code

View Ticket
Login
Ticket UUID: 55660624f7de49cca846ca3d66f94084c99d9229
Title: Block cursor in text widget may hide the character underneath
Type: Bug Version: core-8-6-branch
Submitter: fvogel Created on: 2025-06-01 20:27:14
Subsystem: 18. [text] Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-05-05 12:20:30
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-05-05 12:20:30
Description:

The -blockcursor widget option controls the drawing of the cursor. However, drawing the cursor as a solid blinking block is not exactly performed as in real or emulated terminals. The character at the cursor position is always drawn in it's foreground color, i.e. not in "reverse video", which can lead to unwanted visual effects and even hide the character entirely, when the cursor is in its on-state.

This was first reported by Christian Werner as part of [5d0bc3cfec] (see Issue 3. in my comment there dated 2025-05-31 14:25:43, and the discussion that stemmed from this later).

When this gets fixed, [1d39bb1448|this commit] mentioning the problem in the [text] man page should be backed out.

User Comments: jan.nijtmans added on 2026-05-05 12:20:30:

Now [f3c27ce5df1cab40|fixed] in the revised_text branch too, using Csaba's suggestion. Thanks!


nemethi (claiming to be Csaba Nemethi) added on 2026-04-15 08:38:27:

Maybe, but somebody would have to implement that new option before we can judge which method needs less overhead. I know almost nothing about the implementation of the legacy and revised text widgets, hence I cannot tell how hard or easy it would be to port the code related to the -insertforeground option from the revised text widget to the "normal" one.


jan.nijtmans added on 2026-04-15 08:17:35:

Thanks, Scaba!

This sounds like a good solution. I'm even wondering: Would introducing a -insertforeground option in trunk (would need a TIP) help to make the implementation simpler for trunk?


nemethi (claiming to be Csaba Nemethi) added on 2026-04-12 17:23:06:

Jan, I have examined your question, with the following result:

Porting this fix to the revised_text branch would probably be possible, but appears to be quite difficult. OTOH, it turns out that its functionality is already present in the revised text, due to the latter's -insertforeground and -showinsertforeground options.

The -insertforeground option specifies the color to use as foreground for the characters in the area covered by the insertion cursor. The default is the same as for the -background option. This color will only be used if the -showinsertforeground option is enabled.

The -showinsertforeground option specifies whether to use the value of the -insertforeground option for the foreground of characters covered by the insert cursor. Per default this option is currently disabled.

From the above it follows that for a revised text widget, the "reverse video" effect for the block cursor can be achieved by simply specifying

-showinsertforeground 1

While this is quite simple, it can even become unnecessary by changing the default value of the -showinsertforeground option from 0 to 1. This leads to the following

Proposal: Apply the straightforward changes shown in the following diff output:

Index: doc/text.n
==================================================================
--- doc/text.n
+++ doc/text.n
@@ -214,12 +214,13 @@
 U+2010), it will not be used for hyphenation (but may be used for line
 wrapping, depending on the value of option \fB\-wrap\fR).
 .OP \-insertforeground insertForeground InsertForeground
 Specifies the color to use as foreground for the characters in the area
 covered by the insertion cursor. This color will not be overridden with
-tagging options. Note that this color will not be used unless option
-\fB\-showinsertforeground\fR is enabled.
+tagging options. The default is the same as for the \fB\-background\fR
+option. Note that this color will only be used if the
+\fB\-showinsertforeground\fR option is enabled.
 .OP \-insertunfocussed insertUnfocussed InsertUnfocussed
 .VS 8.6
 Specifies how to display the insertion cursor when the widget does not have
 the focus. Must be \fBnone\fR (the default) which means to not display the
 cursor, \fBhollow\fR which means to display a hollow box, or \fBsolid\fR which
@@ -306,12 +307,12 @@
 line symbol). This symbol does not belong to the text content and cannot
 be edited or selected. Per default the end of text symbol will not be shown.
 .OP \-showinsertforeground showInsertForeground ShowInsertForeground
 Use color option \fB\-insertforeground\fR for the foreground of characters
 covered by the insert cursor if this option is enabled, otherwise the color
-of the characters will not change when coverd by the insertion cursor. Per
-default this option is disabled.
+of the characters will not change when covered by the insertion cursor. Per
+default this option is enabled.
 .OP \-spacemode spaceMode SpaceMode
 Specifies the mode how spaces (U+0020) will be displayed. Default is
 \fBnone\fR, this means that spaces will be displayed like any character, but
 when breaking lines at a space this space will be gobbled if option
 \fB\-wrap\fR is set to \fBword\fR or \fBcodepoint\fR (this is the common

Index: generic/tkText.c
==================================================================
--- generic/tkText.c
+++ generic/tkText.c
@@ -307,11 +307,11 @@
     {TK_OPTION_BOOLEAN, "-showendofline", "showEndOfLine", "ShowEndOfLine",
 	"0", TCL_INDEX_NONE, offsetof(TkText, showEndOfLine), TK_OPTION_VAR(bool), 0, TK_TEXT_LINE_GEOMETRY},
     {TK_OPTION_BOOLEAN, "-showendoftext", "showEndOfText", "ShowEndOfText",
 	"0", TCL_INDEX_NONE, offsetof(TkText, showEndOfText), TK_OPTION_VAR(bool), 0, TK_TEXT_LINE_GEOMETRY},
     {TK_OPTION_BOOLEAN, "-showinsertforeground", "showInsertForeground", "ShowInsertForeground",
-	"0", TCL_INDEX_NONE, offsetof(TkText, showInsertFgColor), TK_OPTION_VAR(bool), 0, 0},
+	"1", TCL_INDEX_NONE, offsetof(TkText, showInsertFgColor), TK_OPTION_VAR(bool), 0, 0},
     {TK_OPTION_STRING_TABLE, "-spacemode", "spaceMode", "SpaceMode",
 	"none", TCL_INDEX_NONE, offsetof(TkText, spaceMode), TK_OPTION_ENUM_VAR, spaceModeStrings, TK_TEXT_LINE_GEOMETRY},
     {TK_OPTION_PIXELS, "-spacing1", "spacing1", "Spacing",
 	DEF_TEXT_SPACING1, offsetof(TkText, spacing1Obj), TCL_INDEX_NONE, 0, 0 , TK_TEXT_LINE_GEOMETRY},
     {TK_OPTION_PIXELS, "-spacing2", "spacing2", "Spacing",

That is, change a single character in the file generic/tkText.c and update the man page accordingly. With this patch, the block cursor will, per default, no longer hide the character underneath.


nemethi (claiming to be Csaba Nemethi) added on 2026-04-10 09:55:53:

I think so. Right now I have no time, but I will examine it ASAP (within the next few days).


jan.nijtmans added on 2026-04-10 08:42:59:

Many thanks, Csaba!

Would it be possible to bring over the same fix to the revised_text branch as well?


nemethi (claiming to be Csaba Nemethi) added on 2026-04-01 16:24:57:

Fixed in core-8-6-branch, core-9-0-branch, and trunk. See commits [ddeb13a3], [fc61e7f7], and [ef466dbd].


oehhar added on 2026-03-30 13:16:27:

Ok, thanks.


nemethi (claiming to be Csaba Nemethi) added on 2026-03-30 12:28:18:

Harald, please let me do the merge myself when the CI is clean. The new version has to land in all three branches core-8-6-branch, core-9-0-branch, and trunk, and I have already prepared them for this action. For example, the trunk version should use bool for a few variables which must be of type int in the two other branches.


oehhar added on 2026-03-30 11:55:50:

Great work Christian and Csaba, we appreciate. I will merge if CI is clean.

Thanks for all, Harald


nemethi (claiming to be Csaba Nemethi) added on 2026-03-30 11:35:01:

I have just committed a slightly improved version, which runs for me as expected (see commit [65bcb9be]. This version handles the "-insertunfocussed solid" setting in the same way as "-blockcursor 1".

IMHO, the branch bug-55660624f7 is now ready to merge.


oehhar added on 2026-03-27 11:41:18:

The opinion "ugly" was by Francois. Perhaps, others have other opinions. Csaba, Jan?

Thanks for all, Harald


chw added on 2026-03-27 10:13:59:
Harald,

so we have an ugly solution for a nice problem, which makes the
advertised feature finally at least usable, which it was not in
the first place.

A choice between pest and cholera, just saying. So let me quote
Bryce from Max Headroom:

"Oh, yes", said Bryce. "But you know it really isn’t my problem.
My brief was to stop channel switching. I mean I only invent
the bomb – I don't drop it. Ha ha!"

oehhar added on 2026-03-27 08:34:48:

Please allow me to ping on the Block Cursor patch block by Christian. When Francois left, this was forgotten.

Are there any opinions on this?

The "parent" patch about this [5d0bc3cfec] was merged by Francois (Thanks again).

Thanks for all, Harald


fvogel added on 2025-06-01 20:38:42:

Christian has proposed a patch to fix this, see [875cbd9ff2].

This patch is creating, at display time, a temporary chunk with a style having a graphics context foreground color pulled from the text widget background color (picked from its border attribute) instead of from the foreground color of the text. As a result the character "under" the block cursor is drawn in the background color of the text widget instead of its foreground color, which reveals this character since the cursor is drawn with the foreground color of the widget.

IMHO the proposed patch is quite ugly and should be refined before being considered for a merge.