Tk Source Code

Artifact [34130e86]
Login

Artifact 34130e86cc87060332f483d584475c38a0a97f986308f67673898ed4bdb83ad6:

Attachment "text-cluster-snap.patch" to ticket [4e6dcf25] added by Arnault01 2026-10-30 01:41:29. (unpublished)Also attachment "text-cluster-snap.patch" to ticket [59d6d5f6] added by Arnault01 2026-10-28 03:07:42. (unpublished)
--- a/library/text.tcl
+++ b/library/text.tcl
@@ -505,9 +505,49 @@
     # 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
+    }
+    # Keep the selection boundary out of grapheme clusters (a base
+    # character plus its combining marks, e.g. the nikkud and teamim of
+    # pointed Hebrew): a cluster occupies a single position on screen
+    # although it spans several indices, and a boundary landing inside it
+    # makes the mouse selection oscillate between those indices while
+    # dragging (the "dancing selection" bug).  Snap the boundary to the
+    # edge of the cluster, using the ICU-based tk::startOfCluster /
+    # tk::endOfCluster commands (UAX #29).  Only possible when libicu
+    # was found at start-up; without it the behavior is unchanged.
+    if {[llength [info commands ::tk::startOfCluster]]} {
+	# $cend is the first cluster boundary after $pos, so the cluster
+	# [$cstart, $cend) is the one containing $pos.  When $pos is itself
+	# a cluster boundary, $cstart == $pos and we fall through to the
+	# historical half-width behavior below.
+	set cend [TextNextPos $w $pos tk::endOfCluster]
+	if {[$w compare $cend != $pos]} {
+		set cstart [TextPrevPos $w $cend tk::startOfCluster]
+		if {[$w compare $cstart != $pos]} {
+			# $pos is strictly inside a cluster: apply the half-width
+			# test to the base character of the cluster, and return
+			# the nearest cluster edge.
+			set bb [$w bbox $cstart]
+			if {$bb eq ""} {
+				set bb $bbox
+			}
+			if {$x - [lindex $bb 0] < [lindex $bb 2]/2} {
+				return $cstart
+			}
+			return $cend
+		}
+		# $pos is the start of a multi-character cluster: the gap after
+		# this cluster is $cend, NOT "$pos + 1 char" which would land
+		# inside the cluster (on a combining mark).
+		if {$x - [lindex $bbox 0] >= [lindex $bbox 2]/2} {
+			return $cend
+		}
+		return $pos
+	}
+    }
+    if {$x - [lindex $bbox 0] < [lindex $bbox 2]/2} {
 	return $pos
     }
     $w index "$pos + 1 char"