Search code examples
linuxunixtcltk-toolkitvi

How can I copy and paste variable in tcl into vi-editor using middle mousewheel click?


[proc_selProc.tcl]

proc selProc {str offset count} {
return [string range $str $offset [expr {$offset + $count -1}]]
}

set myVar Hello
selection handle -selection . [list selproc $myVar]
selection own .

tool > source ./proc_selPorc.tcl

And paste it into vi-editor using middle-mousewheel click.
It worked well at first.
[vi aa.rpt]
Hello

However, when I try as below, I still get "Hello" not "AAA".

set myVar AAA 
selection handle . [list selproc $myVar] 
selection own .

[vi aa.rpt]
Hello ;# <- not printed "AAA"

what should I do?
I tried many ways, but couldn't slove it.

selection clear ; selection get ; PRIMARY selectipn etc ...


Solution

  • Strangely it seems that the selection handler cannot be changed. Something keeps using the first command provided for a window. This looks like a bug to me. It does work if you explicitly remove the old handler first:

    set myVar Hello
    selection handle . [list selProc $myVar]
    selection own .
    
    selection handle . {}
    set myVar AAA 
    selection handle . [list selProc $myVar] 
    selection own .
    

    However, it may be better to use a different approach. Put the text you want to place on the clipboard in a global variable and have the handler pick that up:

    proc selProc {offset count} {
        global selection
        return [string range $selection $offset [expr {$offset + $count -1}]]
    }
    
    set selection Hello
    selection handle . selProc
    selection own .
    # Middle-click pastes "Hello"
    set selection AAA
    selection own .
    # Middle click pastes "AAA"
    

    Update: This is a bug. When a handler for the STRING type is installed, a copy of the handler is installed for the UTF8_STRING type. However, when the handler is changed, only the STRING version is updated. The UTF8_STRING version is left alone. Presumably that's the one picked up by the middle mouse-click.

    Knowing this, another solution could be to specify the -type UTF8_STRING option:

    set myVar Hello
    selection handle -type UTF8_STRING . [list selProc $myVar]
    
    set myVar AAA 
    selection handle -type UTF8_STRING . [list selProc $myVar] 
    

    I have created a bug report for this issue.