racket: displaying images also during evaluations
[geiser.git] / scheme / racket / geiser / eval.rkt
blob9b510cfa9baa9ba652a3a16abb38406b36b5419f
1 ;;; eval.rkt -- evaluation
3 ;; Copyright (C) 2009, 2010, 2011, 2012 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Start date: Sun Apr 26, 2009 00:44
12 #lang racket
14 (provide eval-in
15          load-file
16          macroexpand
17          add-to-load-path
18          make-repl-reader)
21 (require geiser/enter geiser/modules geiser/images)
22 (require errortrace/errortrace-lib)
24 (define last-result (void))
26 (define last-namespace (make-parameter (current-namespace)))
28 (define (exn-key e)
29   (vector-ref (struct->vector e) 0))
31 (define (set-last-error e)
32   (set! last-result `((error (key . ,(exn-key e)))))
33   (display (exn-message e))
34   (newline) (newline)
35   (parameterize ([error-context-display-depth 10])
36     (print-error-trace (current-output-port) e)))
38 (define (write-value v)
39   (with-output-to-string
40     (lambda () (maybe-write-image v))))
42 (define (set-last-result . vs)
43   (set! last-result `((result ,@(map write-value vs)))))
45 (define (call-with-result thunk)
46   (set-last-result (void))
47   (let ([output
48          (with-output-to-string
49            (lambda ()
50              (parameterize ([current-error-port (current-output-port)])
51                (with-handlers ([exn? set-last-error])
52                  (call-with-values thunk set-last-result)))))])
53     (append last-result `((output . ,output)))))
55 (define (eval-in form spec lang)
56   (write (call-with-result
57           (lambda ()
58             (eval form (module-spec->namespace spec lang)))))
59   (newline))
61 (define (load-file file)
62   (load-module file (current-output-port) (last-namespace)))
64 (define (macroexpand form . all)
65   (let ([all (and (not (null? all)) (car all))])
66     (with-output-to-string
67       (lambda ()
68         (pretty-print (syntax->datum ((if all expand expand-once) form)))))))
70 (define (add-to-load-path p)
71   (when (string? p)
72     (let ([p (string->path p)]
73           [cps (current-library-collection-paths)])
74       (unless (member p cps)
75         (current-library-collection-paths
76          (cons p cps)))))
77   #t)
79 (define (make-repl-reader reader)
80   (lambda ()
81     (last-namespace (current-namespace))
82     (reader)))