Interactive procedure editor

For people who like to work with Tcl/Tk interactively, but don't like having to rerun their entire script when they fix one procedure (maybe because your script initializes variables you don't want to reinitialize), here is a little script that will let you redefine individual procedures on the fly.

namespace eval editonthefly {
    set window .edit
    proc edit {cmd} {
        variable window
        destroy $window
        if ![llength [info commands ::$cmd]] {
            puts "Error: $cmd not found."
        } else {
            toplevel $window
            entry $window.name -width 30
            entry $window.args -width 30
            text $window.body -width 60 -height 10 -relief raised -bd 1
            frame $window.b
            grid $window.name $window.args -sticky we
            grid $window.body - -sticky news
            grid $window.b - -sticky w
            $window.name insert 0 $cmd
            $window.args insert 0 [info args ::$cmd]
            $window.body insert 1.0 [info body ::$cmd]
            grid columnconfigure $window 1 -weight 1
            grid rowconfigure    $window 1 -weight 1
            button $window.b.cancel -text "Close" -command "destroy $window"
            button $window.b.save -text "Save" -command "editonthefly::Run"
            button $window.b.done -text "Save and Close" -command "editonthefly::Run; destroy $window"
            pack $window.b.cancel $window.b.save $window.b.done -side left
        }
    }
    proc Run {} {
        variable window
        set name [$window.name get]
        set args [$window.args get]
        set body [string trimright [$window.body get 1.0 end]]
        set s "proc ::$name {$args} {$body}"
        eval $s
    }
}
proc edit {cmd} {editonthefly::edit $cmd}

To use it, you can load this script, and then type "edit command" in the prompt to edit command. Pressing "Close" will close the window without altering the command, though it won't revert changes you made if you pressed Save before. Only one edit window can be open at a time.

--lihtox, 4Jun2012