Tk Source Code

View Ticket
Login
2026-10-13
04:13
Fix [5d983ebf48] and [3d6de81648]: speed up the RTL auto-detection of the text widget and keep its pending state per widget. Merge text-rtl-detection (Serhiy Storchaka, with nab-text-rtl-scan): text-46.1 to 46.6, textTag-19.1 to 19.5. check-in: 3a6ffd3b user: nab tags: trunk, main
2026-10-08
14:10 • Ticket [5d983ebf] Text widget: RTL auto-detection makes mapping and editing slow status still Open with 3 other changes artifact: ea35293e user: serhiy.storchaka
14:08
Fix [5d983ebf48] and [3d6de81648]: speed up the RTL auto-detection of the text widget and fix its per-widget state. Combines branches text-rtl-detect-speed and nab-text-rtl-scan. The text is searched once by the widget (search -all -regexp) instead of a Tcl loop on every line, and the tk_rtl_right tag is changed only where it differs. RTL characters now include all characters with Unicode Bidi_Class R or AL (without U+FEFF), not only U+0590-U+08FF. The pending flag is per widget, and a text recreated under the same name is checked when mapped. check-in: c47ce655 user: serhiy.storchaka tags: text-rtl-detection
13:24 • Closed ticket [3d6de816]: Text widget: RTL detection rescans the whole text at each keystroke plus 6 other changes artifact: e447be16 user: nab
13:18 • Ticket [3d6de816]: 3 changes artifact: fdd9373c user: serhiy.storchaka
12:26 • New ticket [3d6de816]. artifact: 727f21b5 user: nab

Ticket UUID: 3d6de81648a124e371a2a99512fa0b92a8889311
Title: Text widget: RTL detection rescans the whole text at each keystroke
Type: Bug Version: trunk
Submitter: nab Created on: 2026-10-08 12:26:08
Subsystem: 18. [text] Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-10-08 13:24:34
Resolution: Duplicate Closed By: nab
    Closed on: 2026-10-08 13:24:34
Description:

Since 8ce823d20c, tk::TextInsert and tk_textPaste post ::tk::TextDetectAndTagRtl at idle time. This procedure checks every line of the widget in Tcl (get, tk::TextIsRtlLine character by character, tag remove and tag add), so each keystroke costs a time proportional to the length of the text: about 2 ms with 100 lines, 13 ms with 1000 lines and 110 to 170 ms with 10000 lines on macOS, 300 ms on Windows.

Three other problems:

  1. The pending flag ::tk::Priv(rtl_detect_pending) is shared by all text widgets. If a text is destroyed before its idle check, the flag is never cleared and the other texts are not checked until a new text is mapped.
  2. For the same reason, when two texts are edited before the idle check, only the first one is checked.
  3. The "already checked" flag is a global variable named after the widget path (__tk_text_rtl_auto_$w) and is never unset: a text recreated with the same name is not checked when it is mapped.

Proposed fix: branch nab-text-rtl-scan, check-in 5aae2fcb27:

  • the widget searches its whole text once (search -all -elide -regexp), with a pattern that accepts exactly the lines accepted by tk::TextIsRtlLine; the tk_rtl_right tag is changed only where it differs;
  • the pending state is kept per widget;
  • a text is checked at Map time when it does not have the tk_rtl_right tag yet.

With 10000 lines, a keystroke now costs 3.6 ms instead of 170 ms with Latin text, and 23 ms instead of 128 ms with Hebrew text (macOS).

New tests text-46.1 to text-46.6. text-46.1, 46.2, 46.3 and 46.5 fail before the fix (46.5: the tag stayed on a line that became blank). The text tests pass (1797, 0 failed).

Not changed: the detection still runs only after typing or pasting (not after deleting, undo, or an insertion by the program), and a line with digits, parentheses or quotes is not considered right-to-left.

Checked on macOS (aqua) only.

User Comments: nab added on 2026-10-08 13:24:34:

Sorry, I missed 5d983ebf48: this ticket is a duplicate. Yes, please combine the two in one branch, and thank you for testing on X11 and Windows.

A few notes for the combination:

  • 5aae2fcb27 already exits early: $w search -elide -regexp looks for one RTL character, without copying the whole text into a Tcl string at each keystroke.
  • The tag is changed only where it differs: when nothing changed, no tag remove or tag add runs, so no line metrics are invalidated.
  • In the search of the whole text, \s and \x00-\x20 inside a bracket expression also match the newline, so a match runs into the next lines: wrong lines, and about 3 s for 10000 Hebrew lines. This is why ::tk::TextRtlLinePattern lists the Unicode white space explicitly. With your wider set, the pattern should use ::tk::RtlChars; text-46.4 compares the tagged lines with tk::TextIsRtlLine on random lines and will catch a difference.
  • At Map time, a text counts as already checked when it has the tk_rtl_right tag, and the pending flag is cleared at the start of the idle check, also when the widget no longer exists. So no <Destroy> binding is needed.
  • textTag-19.2/19.3 and text-46.2/46.3 test the same cases.
  • U+FE70-U+FEFF also covers U+FEFF (ZERO WIDTH NO-BREAK SPACE, Bidi_Class BN): a line holding only a BOM would be right-justified.

I close this ticket as a duplicate of 5d983ebf48.


serhiy.storchaka added on 2026-10-08 13:18:57:

This duplicates [5d983ebf48], with a fix in branch text-rtl-detect-speed since 2026-09-24: per-widget pending and detection state, the scan skipped when the text has no RTL characters, and RTL characters extended to all of Bidi_Class R and AL. Your single search -all -regexp over the whole text is better for the cost of a keystroke. I propose to combine the two in one branch: your search and tests text-46.x with the wider character set and the early exit from mine. I can do it, and test it on X11 and Windows.