diff --git a/common/base.scm b/common/base.scm index d1e6775..29a40c8 100644 --- a/common/base.scm +++ b/common/base.scm @@ -136,7 +136,7 @@ (hash-table-ref/default *handlers* name #f)) (define (interactive-eval sexp) - (let-values ((vals (eval sexp (param:environment)))) ;; TODO environment + (let-values ((vals (eval sexp ($current-environment)))) vals)) (define *presentations* (make-hash-table)) @@ -167,7 +167,7 @@ The secondary value indicates the absence of an entry." object)))) (define (replace-readtime-lookup-presented-object-or-lose string) - "For presentations, emacs passes something that common lisp evals at read time. The resulting object is very different than what gerbil or gambits #. tries to do, so we do everything at run time" + "For presentations, emacs passes something that common lisp evals at read time. The resulting object is very different than what gerbil or gambits #. tries to do, so strip the #. and pattern-match to evaluate this form at runtime." (let* ((pattern "#.(swank:lookup-presented-object-or-lose ") (start (string-contains string pattern))) (if start @@ -175,6 +175,20 @@ The secondary value indicates the absence of an entry." (string-replace string "" start (+ 2 start))) string))) +(define (replace-lookup-presented-object-or-lose form) + (define (recons form x y) + (if (and (eq? x (car form)) + (eq? y (cdr form))) + form + (cons x y))) + (if (pair? form) + (if (eq? (car form) 'swank:lookup-presented-object-or-lose) + (swank:lookup-presented-object-or-lose (cadr form)) + (recons form + (replace-lookup-presented-object-or-lose (car form)) + (replace-lookup-presented-object-or-lose (cdr form)))) + form)) + (define last-presentation-id 0) (define (next-presentation-id) (set! last-presentation-id (+ last-presentation-id 1)) @@ -264,7 +278,7 @@ The secondary value indicates the absence of an entry." (if (and (list? x) (eq? (car x) 'quote)) (cond ((string? (cadr x)) - (cadr x)) + (read-from-string (cadr x))) ((eq? (cadr x) 'nil) "(user)") (else ;; TODO @@ -285,7 +299,8 @@ The secondary value indicates the absence of an entry." (define (find-expression-containing-swank-cursor-marker expr) (define (f expr exit) (if (list? expr) - (if (member '|swank::%cursor-marker%| expr) + (if (or (member '|swank::%cursor-marker%| expr) + (member 'swank::%cursor-marker% expr)) expr (let ((res (find (lambda (ex) (f ex exit)) @@ -296,17 +311,20 @@ The secondary value indicates the absence of an entry." (define (highlight-at-cursor signature expr) (let* ((form (find-expression-containing-swank-cursor-marker (cdr expr))) - (index (list-index (lambda (el) (eq? el '|swank::%cursor-marker%|)) form))) + (index (list-index (lambda (el) + (or (eq? el '|swank::%cursor-marker%|) + (eq? el 'swank::%cursor-marker%))) + form))) (if index (wrap-item/index (improper->proper-list signature) (- index 1) '===> '<===) '()))) (define (find-string-before-swank-cursor-marker expr) (let ((ex (find-expression-containing-swank-cursor-marker expr))) - (if ex - (if (string? (car ex)) - (car ex) - #f)))) + (and ex + (if (string? (car ex)) + (car ex) + #f)))) (define (wrap-item/index lst index before-marker after-marker) (let loop ((i 0) @@ -527,7 +545,7 @@ The secondary value indicates the absence of an entry." (stream)))))) (define (binding-value symbol) - (call/cc (lambda (k) (with-exception-handler (lambda (c) (k #f)) (lambda () (eval symbol (param:environment))))))) + (call/cc (lambda (k) (with-exception-handler (lambda (c) (k #f)) (lambda () (eval symbol ($current-environment))))))) (define (object-documentation name object) (let ((doc ($binding-documentation object))) @@ -787,6 +805,13 @@ The secondary value indicates the absence of an entry." (write-message `(:inspect ,(inspect-object object) nil nil)) #t) +(define (environment-name-as-string environment) + (write-to-string ($environment-name environment))) +(define (package-string->environment string) + (if (string=? string "COMMON-LISP-USER") + (interaction-environment) + ($environment (read-from-string string)))) + ;;;; swank image support (define-record-type swank-image (make-swank-image type filename data string) diff --git a/common/handlers.scm b/common/handlers.scm index d5bcf03..38827bb 100644 --- a/common/handlers.scm +++ b/common/handlers.scm @@ -12,15 +12,16 @@ ((define-slime-handler (name . params) body0 body1 ...) (register-slime-handler! 'name (lambda params body0 body1 ...))))) -(define-slime-handler (:emacs-rex sexp env-name thread id) +(define-slime-handler (:emacs-rex sexp package-string thread id) (call-with-current-continuation (lambda (exit) (parameterize ((param:abort (lambda (message) (exit `(:return (:abort ,message) ,id)))) - (param:environment ($environment env-name)) + (param:environment + (package-string->environment package-string)) (param:current-id id)) - `(:return (:ok ,(process-form sexp env-name)) + `(:return (:ok ,(process-form sexp package-string)) ,id))))) (define-slime-handler (swank:connection-info) @@ -31,7 +32,8 @@ :machine (:instance "host" :type "X86-64") :features (:swank) :modules ("SWANK-ARGLISTS" "SWANK-REPL" "SWANK-PRESENTATIONS") - :package (:name "(user)" :prompt "(user)") + :package (:name ,(environment-name-as-string ($current-environment)) + :prompt ,(environment-name-as-string ($current-environment))) :version "2.28")) (define-slime-handler (swank:swank-require packages) @@ -49,11 +51,16 @@ (swank:lookup-presented-object num)) (define-slime-handler (swank-repl:create-repl . args) - (list "(user)" "(user)")) + (list (environment-name-as-string ($current-environment)) + (environment-name-as-string ($current-environment)))) (define-slime-handler (swank-repl:listener-eval form) (let* ((form (replace-readtime-lookup-presented-object-or-lose form)) - (results ($output-to-repl (lambda () (interactive-eval (cons 'begin (read-all (open-input-string form)))))))) + (results ($output-to-repl (lambda () + (interactive-eval + (cons 'begin + (replace-lookup-presented-object-or-lose + (read-all (open-input-string form))))))))) (for-each (lambda (val) (if (presentations?) (present val ':repl-result) @@ -87,7 +94,7 @@ `(:compilation-result nil t 0.001 nil nil))) (define-slime-handler (swank:load-file filename) - (let ((results ($output-to-repl (lambda () (load filename (param:environment)))))) + (let ((results ($output-to-repl (lambda () (load filename ($current-environment)))))) 'loaded)) (define-slime-handler (swank:set-package name) diff --git a/guile-swank.scm b/guile-swank.scm index fc1745b..06aae48 100644 --- a/guile-swank.scm +++ b/guile-swank.scm @@ -26,6 +26,8 @@ vector->list) (ice-9 exceptions) (ice-9 pretty-print) + (only (ice-9 session) + procedure-arguments) (system vm frame) (system vm program) (only (system repl debug) diff --git a/specific/bigloo.scm b/specific/bigloo.scm index 86d583c..4c6f0b8 100644 --- a/specific/bigloo.scm +++ b/specific/bigloo.scm @@ -83,8 +83,12 @@ (define ($error-description error) error) +(define ($environment-name environment) + '(user)) (define ($environment name) (interaction-environment)) +(define ($current-environment) + (interaction-environment)) (define $pretty-print pp) (define string->utf8 (lambda (x) x)) (define utf8->string (lambda (x) x)) diff --git a/specific/chez.ss b/specific/chez.ss index e5d4f16..a9ac70b 100644 --- a/specific/chez.ss +++ b/specific/chez.ss @@ -142,6 +142,9 @@ ;; (write val o) ;; (get-output-string o))) +(define ($environment-name environment) + '(user)) + (define ($environment env-name) ;; don't support any changes (interaction-environment)) diff --git a/specific/chibi.scm b/specific/chibi.scm index b0aa13b..5517871 100644 --- a/specific/chibi.scm +++ b/specific/chibi.scm @@ -114,9 +114,15 @@ (define ($set-package name) (list "(user)" "(user)")) +(define ($environment-name environment) + '(user)) + (define ($environment name) (interaction-environment)) +(define ($current-environment) + (interaction-environment)) + (define ($condition-trace condition) '()) diff --git a/specific/chicken.scm b/specific/chicken.scm index 2bb39a3..88db627 100644 --- a/specific/chicken.scm +++ b/specific/chicken.scm @@ -64,9 +64,15 @@ (define ($set-package name) (list "(user)" "(user)")) +(define ($environment-name environment) + '(user)) + (define ($environment name) (interaction-environment)) +(define ($current-environment) + (interaction-environment)) + (define ($condition-trace condition) '()) diff --git a/specific/cyclone.ss b/specific/cyclone.ss index 0149f3a..aa74c57 100644 --- a/specific/cyclone.ss +++ b/specific/cyclone.ss @@ -37,9 +37,15 @@ (define ($set-package name) (list "(user)" "(user)")) +(define ($environment-name environment) + '(user)) + (define ($environment name) (interaction-environment)) +(define ($current-environment) + (interaction-environment)) + (define ($condition-trace condition) '()) diff --git a/specific/gauche.scm b/specific/gauche.scm index 5cec749..97c9c4c 100644 --- a/specific/gauche.scm +++ b/specific/gauche.scm @@ -58,9 +58,14 @@ (list "(user)" "(user)")) (define env (interaction-environment)) +(define ($environment-name environment) + '(user)) (define ($environment name) env) +(define ($current-environment) + (interaction-environment)) + (define ($condition-trace condition) '()) diff --git a/specific/gerbil.scm b/specific/gerbil.scm index fa39622..1160e0c 100644 --- a/specific/gerbil.scm +++ b/specific/gerbil.scm @@ -91,8 +91,13 @@ (let ((name (string->symbol env-name))) (find (lambda (e) (eq? (expander-context-id e) name)) (filter module-context? (map cdr (hash->list (current-expander-module-registry))))))))) +(define ($environment-name environment) + '(user)) (define $environment env-name->environment) +(define ($current-environment) + (interaction-environment)) + (define (environment-bindings env-name) (let ((env (env-name->environment env-name))) (if env diff --git a/specific/guile.scm b/specific/guile.scm index e39e224..29dac53 100644 --- a/specific/guile.scm +++ b/specific/guile.scm @@ -52,19 +52,25 @@ (write error o) (get-output-string o))) +(define repl-output-port + (make-soft-port + (vector (lambda (c) + (write-message `(:write-string ,(string c)))) + (lambda (s) + (write-message `(:write-string ,s))) + (lambda () #f) + #f + (lambda () #f) + #f) + "w")) + (define ($output-to-repl thunk) - ;; basic implementation, print all output at the end, this should - ;; be replaced with a custom output port - (let ((o (open-output-string))) - (with-output-to-port o (lambda () - (with-error-to-port o (lambda () - (let-values ((x (thunk))) - (swank/write-string (get-output-string o) #f) - (apply values x)))))))) + (with-output-to-port repl-output-port + (lambda () + (with-error-to-port repl-output-port thunk)))) (define (env-name->environment env-name) - ;; TODO - (interaction-environment)) + (resolve-module env-name)) (define (environment-bindings env) (define (env-binds env) (let ((results '())) @@ -143,7 +149,9 @@ (set! stored-frames frames) (vector->list (vector-map (lambda (fr) - (format #f "~a" (frame-procedure-name fr))) + (format #f "~a ~a" + (frame-procedure-name fr) + (frame-arguments fr))) frames)))) @@ -183,8 +191,31 @@ (define ($handle-condition exception) (invoke-sldb exception)) +(define (procedure-parameters procedure) + (let ((arguments (procedure-arguments procedure))) + (and arguments + (let ((required (cdar arguments)) + (optional (cdadr arguments)) + (keyword (cdaddr arguments)) + (allow-other-keys? (cdar (cdddr arguments))) + (rest (cdar (cddddr arguments)))) + (append required + (cons '#:optional optional) + (cons '#:key keyword) + (if allow-other-keys? + (list '#:allow-other-keys?) + '()) + (cons '#:rest rest)))))) + (define ($function-parameters-and-documentation name) - (cons #f #f)) + (let ((thing (module-variable (current-module) + (string->symbol name)))) + (if (and thing + (procedure? (variable-ref thing))) + (let ((procedure (variable-ref thing))) + (cons (cons (string->symbol name) (procedure-parameters procedure)) + (procedure-documentation procedure))) + (cons #f #f)))) (define (get-valid-module-name name) (with-input-from-string name read)) @@ -195,8 +226,13 @@ (set-current-module mod))) (list name name)) -(define ($environment name) - (interaction-environment)) +(define ($environment-name environment) + (module-name environment)) + +(define $environment env-name->environment) + +(define ($current-environment) + (current-module)) (define $pretty-print pretty-print) diff --git a/specific/kawa.scm b/specific/kawa.scm index 9fec311..df77d13 100644 --- a/specific/kawa.scm +++ b/specific/kawa.scm @@ -106,9 +106,15 @@ (define ($set-package name) (list "(user)" "(user)")) +(define ($environment-name environment) + '(user)) + (define ($environment name) (interaction-environment)) +(define ($current-environment) + (interaction-environment)) + (define ($condition-trace condition) (map *:toString ($condition-links condition))) diff --git a/specific/larceny.scm b/specific/larceny.scm index d2b938d..f58c374 100644 --- a/specific/larceny.scm +++ b/specific/larceny.scm @@ -52,9 +52,15 @@ (define ($set-package name) (list "(user)" "(user)")) +(define ($environment-name environment) + '(user)) + (define ($environment name) (interaction-environment)) +(define ($current-environment) + (interaction-environment)) + (define ($condition-trace condition) '()) diff --git a/specific/mit.scm b/specific/mit.scm index cec1d9d..cf2ea75 100644 --- a/specific/mit.scm +++ b/specific/mit.scm @@ -721,7 +721,7 @@ (define ($function-parameters-and-documentation name) ;; TODO (let ((binding #f)) - (cons (procedure-parameters (string->symbol name) ($environment param:environment)) + (cons (procedure-parameters (string->symbol name) ($current-environment)) ($binding-documentation binding)))) (define (string-replace s1 s2 start1 end1) ;; . start2+end2 @@ -743,9 +743,23 @@ (loop (cdr n) (cdr v))))))) (else #f))) +(define ($set-package name) + (ge ($environment (read-from-string name))) + (list name name)) + +(define ($environment-name environment) + (if (eq? unknown-environment environment) + unknown-environment + (let ((package (environment->package environment))) + (if package + (package/name package) + (string->symbol (string anonymous-package-prefix (object-hash environment))))))) + (define ($environment env-name) - ;; TODO - (interaction-environment)) + (package/environment (find-package env-name #t))) + +(define ($current-environment) + (nearest-repl/environment)) (define ($error-description condition) (condition/report-string condition)) @@ -762,10 +776,7 @@ (define (env->pstring env) (if (eq? unknown-environment env) "unknown environment" - (let ((package (environment->package env))) - (if package - (write-to-string (package/name package)) - (string anonymous-package-prefix (object-hash env)))))) + (write-to-string ($environment-name env)))) (define (with-exception-handler handler thunk) (bind-condition-handler (list condition-type:serious-condition) diff --git a/specific/racket.ss b/specific/racket.ss index d998f3c..030fd98 100755 --- a/specific/racket.ss +++ b/specific/racket.ss @@ -71,9 +71,15 @@ (define ($set-package name) (list "(user)" "(user)")) +(define ($environment-name environment) + '(user)) + (define ($environment name) (interaction-environment)) +(define ($current-environment) + (interaction-environment)) + (define ($condition-trace condition) '())