Tk Source Code

Attachment Details
Login
Overview

Artifact ID: 52052b34079fef5f2b42fa24cac81cee2ad262d9b0a09b3976a258b1a89b8b45
Ticket: 59d6d5f6c049c611f1528af0c4c989e6c48e5d15
Date: 2026-10-16 07:09:34
User: anonymous
Artifact Attached: 8f9ba1404048b378750ee8d4c39b7976523acb6ec01ee2da8004b1a17d063327
Filename:patch.txt
Description:Here is the pure-Tcl patch, generated as a unified diff against `library/text.tcl` from the Tk trunk (commit 8d193183). The logic of the Python fix has been transposed to the right place: rather than re-binding all mouse events, it is enough to fix `::tk::TextClosestGap`, since that is what provides the boundary to `TextButton1` (anchor), `TextSelectTo` (drag), `TextPasteSelection`, and auto-scan. The default behavior (for unpointed text) is unchanged; only the boundary is "snapped" to the edges of the glyph cluster (base character + zero-width combining marks).
Content:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
--- library/text.tcl
+++ library/text.tcl
@@ -484,11 +484,37 @@
     }
 }
 
+# ::tk::TextIsCombining --
+# Internal helper: returns 1 if the character is a zero-width combining
+# or bidi-control character (combining diacritics, Hebrew points and
+# accents, Arabic vowel points, bidi embedding and directional marks,
+# variation selectors), 0 otherwise.  Such characters render no pixels
+# of their own: they are drawn over the preceding base character, so all
+# the indices of a cluster (base character + its marks) occupy the same
+# pixels on screen.
+#
+# Arguments:
+# ch -		The character to test (a string of length 0 or 1).
+
+proc ::tk::TextIsCombining {ch} {
+    regexp -- {[\u0300-\u036F\u0483-\u0489\u0591-\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7-\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u0900-\u0903\u093A-\u093C\u0941-\u0948\u094D\u0951-\u0957\u200B-\u200F\u202A-\u202E\u2060-\u2064\uFE00-\uFE0F\uFE20-\uFE2F]} $ch
+}
+
 # ::tk::TextClosestGap --
 # Given x and y coordinates, this procedure finds the closest boundary
 # between characters to the given coordinates and returns the index
 # of the character just after the boundary.
 #
+# The boundary is snapped to the edge of a grapheme cluster (a base
+# character plus its combining marks).  The previous implementation split
+# at half the width of the character under the pointer; when that
+# character is a zero-width combining mark (e.g. Hebrew nikkud or teamim
+# in pointed RTL text), the boundary fell *inside* a cluster, where
+# several indices map to the same pixels.  With a bidi rendering each
+# pointer movement could then oscillate between those indices, making
+# the mouse selection "dance" while dragging.  Snapping every boundary to
+# a cluster edge makes the selection visually stable.
+#
 # Arguments:
 # w -		The text window.
 # x -		X-coordinate within the window.
@@ -505,12 +531,55 @@
     # out of the widget.
     # The bbox of a newline extends to the right edge of the widget, and
     # the gap after it is on the next line. [13aabdb3c1]
-    if {$y < [lindex [$w dlineinfo $pos] 1] ||
-	    $x - [lindex $bbox 0] < [lindex $bbox 2]/2 ||
-	    [$w get $pos] eq "\n"} {
+    if {$y < [lindex [$w dlineinfo $pos] 1] || [$w get $pos] eq "\n"} {
    return $pos
     }
-    $w index "$pos + 1 char"
+    # Back up to the start of the cluster: skip over the zero-width
+    # marks preceding (and possibly under) the pointer.  Do not cross a
+    # line boundary while backing up.
+    set cstart $pos
+    set line [lindex [split $pos .] 0]
+    while {[::tk::TextIsCombining [string index [$w get $cstart] 0]]} {
+	set prev [$w index "$cstart - 1 char"]
+	if {$prev eq $cstart || [lindex [split $prev .] 0] ne $line} {
+	    break
+	}
+	set cstart $prev
+    }
+    # A cluster of marks with no base character before it on this line
+    # (e.g. bidi embedding characters at the start of an RTL line) is
+    # attached to the following base character.
+    while {[::tk::TextIsCombining [string index [$w get $cstart] 0]]} {
+	set next [$w index "$cstart + 1 char"]
+	if {$next eq $cstart} {
+	    break
+	}
+	set cstart $next
+    }
+    set bbox [$w bbox $cstart]
+    if {$bbox eq ""} {
+	return $pos
+    }
+    # Same half-width test as before, but on the base character of the
+    # cluster: the resulting boundary is a cluster edge, stable on
+    # screen.
+    if {$x - [lindex $bbox 0] < [lindex $bbox 2]/2} {
+	return $cstart
+    }
+    # End of the cluster: the first non-mark character after the base
+    # character, so the selection boundary never splits a base character
+    # from its marks.
+    set last $cstart
+    while {1} {
+	set next [$w index "$last + 1 char"]
+	if {$next eq $last} {
+	    return [$w index "$last + 1 char"]
+	}
+	if {![::tk::TextIsCombining [string index [$w get $next] 0]]} {
+	    return $next
+	}
+	set last $next
+    }
 }
 
 # ::tk::TextButton1 --