Copy & Paste on textwidgets copies both elided and non elided text. I want only non elided text. This is the solution I found:
proc getText_noElide {w start end} {
set erg {}
# find tags with -elide 1
set elideTags {}
foreach el [$w tag names] {
if {[$w tag cget $el -elide] eq {1}} {
lappend elideTags $el
}
}
# if there is any tag which elides text
if {[llength $elideTags] > 0} {
set elide 0
foreach {key value index} [$w dump -tag -text [$w index $start] [$w index $end]] {
switch -exact -- $key {
tagon {
if {$value eq {hide}} {
set elide 1
}
}
text {
if {!$elide} {
append erg $value
}
}
tagoff {
if {$value eq {hide}} {
set elide 0
}
}
}
}
} else {
# when nothing is elided
append erg [$w get $start $end]
}
return $erg
}
proc tk_textCopy_noElide {w} {
if {![catch {set data [getText_noElide $w sel.first sel.last]}]} {
clipboard clear -displayof $w
clipboard append -displayof $w $data
}
return
}
proc tk_textCut_noElide {w} {
if {![catch {set data [getText_noElide $w sel.first sel.last]}]} {
clipboard clear -displayof $w
clipboard append -displayof $w $data
$w delete sel.first sel.last
}
return
}
proc tk_textPaste_noElide {w} {
tk_textPaste $w
return
}
# procedure to change the binding of the textwidget.
proc textwidget_CuP_noElide {w} {
foreach el [bind Text] {
bind $w $el [bind Text $el]
}
bindtags $w [lreplace [set t [bindtags $w]] [set i [lsearch $t Text]] $i]
bind $w <<Cut>> {tk_textCut_noElide %W}
bind $w <<Copy>> {tk_textCopy_noElide %W}
bind $w <<Paste>> {tk_textPaste_noElide %W}
return
}
text .w
pack .w
textwidget_CuP_noElide .w
.w tag configure hide -elide 1
.w insert end "The old mann\n"
.w insert end "and the sea\n" hide
.w insert end "by Ernest Hemmingway\n"
# Try strg-c:
# The old mann
# by Ernest Hemmingway
.w tag configure hide -elide 0
# Try strg-c:
# The old mann
# and the sea
# by Ernest Hemmingway