Tk and newLISP

updated Nov 27th, 2009

Normally newLISP uses the TclTk frontend newlisp-tk to run Tk based graphics applications. The following simple wrapper lets you run Tk based graphics programs just using the the console application newlisp on Linux/UNIX and a Tcl/Tk installation on your system.

The program calls the TclTk windows shell wish and communicates with it via two stdio pipes. This approach leads to a much faster integration between newLISP and Tcl/Tk compared with newlisp-tk approach via Tcp/Ip network sockets.

Try this to run swarm.lsp :

On Linux/Unix/OSX

./runtk swarm.lsp

On Windows

newlisp runtk swarm.lsp

The listing of runtk

For syntax highlighted version click here.

; --- runtk v 1.4 updated for 10.1 LM Nov 27th, 2009 ---
; original version bye Fanda: 
; Run programs written for newlisp-tk without without it
; Only newLISP and a installation of Tcl/Tk is required.
; EXAMPLE on Linux/UNIX and Win32:
;   runtk myprog.lsp
; myprog.lsp could also be directly embedded
; inside this wrapper (see bottom of this file).

; setup communications to Tcl/Tk
(map set '(myin tcout) (pipe))
(map set '(tcin myout) (pipe))
(process "/usr/bin/wish" tcin tcout)

; tk function to pass commands to Tcl/Tk 
(define (tk)
  (write-line myout (append "if { [catch { puts ["
              (apply string (args)) "] }] } { "
              [text] tk_messageBox -message $errorInfo; exit }

  (let (str "")
    (while (starts-with (setq str (read-line myin)) "newLISP:")
      (eval-string ((length "newLISP: ") -1 str)))
(global 'tk) 

;; this is for compatibility with newlisp-tk for running
;; the Demo.lsp programs distributed with newlisp-tk
;; for new programs just use 'puts' without the 'Newlisp' wrapper
(tk "proc Newlisp { command } { puts $command }")  

;; exit when main window is closed
(tk "bind . <Destroy> {puts {(exit)}}") 

;; get program from command line or insert the program here
(load (main-args 2)) 

;; process incoming newLISP requests
(while (read-line myin)
    (eval-string (current-line))) 

;; eof

Back to Tips&Tricks