1 ;;; user.rkt -- global bindings visible to geiser users
3 ;; Copyright (C) 2010, 2011, 2012, 2013 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: Wed Mar 31, 2010 22:24
14 (provide init-geiser-repl run-geiser-server start-geiser)
16 (require (for-syntax racket/base)
25 (define top-namespace (current-namespace))
26 (define last-entered (make-parameter ""))
28 (define (do-enter mod name)
30 (current-namespace (module->namespace mod))
33 (define (enter! mod stx)
35 (current-namespace top-namespace)
37 [(symbol? mod) (do-enter mod (symbol->string mod))]
41 (path-string? (cadr mod))) (do-enter mod (cadr mod))]
42 [(path-string? mod) (do-enter `(file ,mod) mod)]
43 [else (raise-syntax-error
45 "not a valid module path, and not #f"
49 (define orig-loader (current-load/use-compiled))
50 (define geiser-loader (module-loader orig-loader))
53 (define geiser-main (module->namespace 'geiser))
54 (define (eval-here form) (eval form geiser-main))
60 (cond [(equal? form '(unquote apply))
61 (let* ([proc (eval-here (read))]
62 [args (map eval-here (read))]
63 [ev (lambda () (apply proc args))])
64 (eval-in `(,ev) mod lang))]
65 [else ((geiser:eval lang) form mod)])))))
67 (define ((geiser-read prompt))
69 (flush-output (current-error-port))
70 (flush-output (current-output-port))
71 (let* ([in ((current-get-interaction-input-port))]
72 [form ((current-read-interaction) (object-name in) in)])
74 [(uq cmd) (eq? 'unquote (syntax-e #'uq))
75 (case (syntax-e #'cmd)
76 [(start-geiser) (datum->syntax #f `(list 'port ,(start-geiser)))]
77 [(enter) (enter! (read) #'cmd)]
78 [(geiser-eval) (geiser-eval)]
79 [(geiser-no-values) (datum->syntax #f (void))]
80 [(add-to-load-path) (add-to-load-path (read))]
81 [(set-image-cache) (image-cache (read))]
82 [(image-cache) (image-cache)]
83 [(gcd) (current-directory)]
84 [(cd) (current-directory (read))]
90 (let ([m (namespace->module-name (current-namespace) (last-entered))])
91 (printf "racket@~a> " (regexp-replace* " " m "_")))))
93 (define (geiser-prompt-read prompt)
94 (make-repl-reader (geiser-read prompt)))
96 (define (init-geiser-repl)
97 (compile-enforce-module-constants #f)
98 (current-load/use-compiled geiser-loader)
99 (current-prompt-read (geiser-prompt-read geiser-prompt))
100 (current-print maybe-print-image))
102 (define (run-geiser-repl in out enforce-module-constants)
103 (parameterize [(compile-enforce-module-constants enforce-module-constants)
104 (current-input-port in)
105 (current-output-port out)
106 (current-error-port out)
107 (current-load/use-compiled geiser-loader)
108 (current-prompt-read (geiser-prompt-read geiser-prompt))
109 (current-print maybe-print-image)]
110 (read-eval-print-loop)))
112 (define server-channel (make-channel))
114 (define (run-geiser-server port enforce-module-constants (hostname #f))
117 (run-geiser-repl in out enforce-module-constants))
121 (let ([lsner (tcp-listen p 4 #f hostname)])
122 (let-values ([(_ p __ ___) (tcp-addresses lsner #t)])
123 (channel-put server-channel p)
126 (define (start-geiser (port 0) (hostname #f) (enforce-module-constants #f))
127 (thread (lambda () (run-geiser-server port enforce-module-constants hostname)))
128 (channel-get server-channel))