All that remains is the actual change or definition of a binding and some way to remember the bindings the next time the application is run. The BindDefine procedure attempts a bind command that uses the contents of the entries. If it succeeds, then the edit window is removed by unpacking it.
The bindings are saved by Bind_Save as a series of Tcl commands that define the bindings. It is crucial that the list command be used to construct the commands properly.
Bind_Read uses the source command to read the saved commands. The application must call Bind_Read as part of its initialization to get the customized bindings for the widget or class. It also must provide a way to invoke Bind_Interface, such as a button, menu entry, or key binding.
Example 43-7 Defining and saving bindings.
proc BindDefine { f } {
if [catch {
bind [$f.top.e get] [$f.edit.key.e get]
[$f.edit.cmd.e get]
} err] {
Status $err
} else {
# Remove the edit window
pack forget $f.edit
}
}
proc Bind_Save { dotfile args } {
set out [open $dotfile.new w]
foreach w $args {
foreach seq [bind $w] {
# Output a Tcl command
puts $out [list bind $w $seq [bind $w $seq]]
}
}
close $out
file rename -force $dotfile.new $dotfile
}
proc Bind_Read { dotfile } {
if [catch {
if [file exists $dotfile] {
# Read the saved Tcl commands
source $dotfile
}
} err] {
Status "Bind_Read $dotfile failed: $err"
}
}