Monday, September 26, 2022

Making Runtime Proceedure Script Modifications.

When using Gnocl widgets only a single callback script is permitted. This differs somewhat to the underlying Gtk bindings api which allows for a chain of callback functions to be defined.  

Rather than trying to produce a convoluted means of setting complex callbacks in C, simple changes can be made during application runtime because Tcl, as a dynamic language, allows everything to be reconfigurable.

Using a simple procedure which takes three arguments: the name of the procedure to be modified, the script modifications and a flag specifying append or prepend, the following proof of concept script can created.  


# !/bin/sh
# the next line restarts using tclsh \

exec tclsh "$0" "$@"

proc p1 {str} { puts $str }

proc procModify {proc script {mode -append} } {

   set args [info args $proc]
   set body [string trim [info body $proc]]

   set box

    if { $mode == "-prepend" } {
        set body $script\n$body
    } else {
        set body $body\n$script
    }

    proc $proc $args $body

}

procModify p1 "puts DONE!"
procModify p1 "puts READY!" -prepend

p1 HI