Re: Using closures as commands/button callbacks...

From: Tom Lord <lord_at_cygnus.com>
Date: Thu, 8 Jun 1995 12:59:38 -0700

        But when I started messing around with STk, I found that the actions
        were quoted, like in emacs-lisp, which I think is wrong.

In the STk-inspired Tk binding of GNU's interpreter, Guile, you can
pass an anonymous Scheme procedure in the callback position of a
widget configuration call, "bind" call, or "after" call.

This is made possible by a small modification to libtk called
"canonical commands". Canonical commands are described in
http://www.cygnus.com/library/ctr/guile.html.

-t

p.s.: here is some code illustrating this feature, taken from a silly
      Guile demo program:

    (let ((locked #f))
      (bind '.c "<Button-3>"
            (tcl-lambda ("%x %y" (number x) (number y))
              (if locked
                  (set! locked #f)
                  (begin
                    (start-moves x y)
                    (set! locked #t)))
              ""))
      (bind '.c "<Motion>"
             (tcl-lambda ("%x %y" (number x) (number y))
                (if locked (move-to x y)) "")))
    

Note that "tcl-lambda" is just syntax for "lambda" that adds support
for `type declarations' on formals. These declarations expand into
code that converts strings passed by Tcl into handier Scheme types.
In this case, the declarations also contain a string that tells how to
map the arguments passed by the Tk event handler onto the Scheme
formals. Apart from that, the two anonymous procedures are ordinary
Scheme, capturing variables in the lexical context such as "locked",
and persisting against GC only until the bindings created by this
expression become invalid.
Received on Thu Jun 08 1995 - 22:01:13 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST