branch: elpa/geiser-racket
commit 7de178d2dc8afd8c7d61b002c9825ba75bbf62ed
Author: Jose Antonio Ortega Ruiz <j...@gnu.org>
Commit: Jose Antonio Ortega Ruiz <j...@gnu.org>

    Racket: show images with print, write and display
    
    Up to now, we were only displaying images when printed as values by the
    REPL, but not when image values were explicitly print-ed, write-d or
    display-ed.  This patch solves that problem by installing (semi)
    appropriate port-{print,write,display}-handler.  This is still and
    incomplete solution in that those handlers (as well as the already
    installed current-print-handler) don't recurse over a value's structure
    and won't produce images embedded in other data structures, as discussed
    in issue #49.
---
 geiser/images.rkt | 11 +++++++++--
 geiser/user.rkt   | 13 ++++++++++++-
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/geiser/images.rkt b/geiser/images.rkt
index ddc0286..2cdcadb 100644
--- a/geiser/images.rkt
+++ b/geiser/images.rkt
@@ -1,6 +1,6 @@
 ;;; images.rkt -- support for image handline
 
-;; Copyright (C) 2012 Jose Antonio Ortega Ruiz
+;; Copyright (C) 2012, 2014 Jose Antonio Ortega Ruiz
 
 ;; This program is free software; you can redistribute it and/or
 ;; modify it under the terms of the Modified BSD License. You should
@@ -14,7 +14,10 @@
 #lang racket/base
 
 (require racket/file file/convertible racket/pretty)
-(provide image-cache maybe-print-image maybe-write-image)
+(provide image-cache
+         maybe-print-image
+         maybe-write-image
+         make-port-print-handler)
 
 (define image-cache
   (let ([ensure-dir (lambda (dir)
@@ -44,3 +47,7 @@
 
 (define (maybe-write-image value)
   (write (or (maybe-save-image value) value)))
+
+(define (make-port-print-handler ph)
+  (lambda (value port . rest)
+    (apply ph (or (maybe-save-image value) value) port rest)))
diff --git a/geiser/user.rkt b/geiser/user.rkt
index a35574b..c566309 100644
--- a/geiser/user.rkt
+++ b/geiser/user.rkt
@@ -119,12 +119,22 @@
 
 (define (geiser-loader) (module-loader (current-load/use-compiled)))
 
+(define (install-print-handler handler)
+  (let ([p (current-output-port)])
+    (handler p (make-port-print-handler (handler p)))))
+
+(define (install-print-handlers)
+  (for-each install-print-handler (list port-print-handler
+                                        port-write-handler
+                                        port-display-handler)))
+
 (define (init-geiser-repl)
   (compile-enforce-module-constants #f)
   (current-load/use-compiled (geiser-loader))
   (preload-help)
   (current-prompt-read (geiser-prompt-read geiser-prompt))
-  (current-print maybe-print-image))
+  (current-print maybe-print-image)
+  (install-print-handlers))
 
 (define (run-geiser-repl in out enforce-module-constants)
   (parameterize [(compile-enforce-module-constants enforce-module-constants)
@@ -134,6 +144,7 @@
                  (current-load/use-compiled (geiser-loader))
                  (current-prompt-read (geiser-prompt-read geiser-prompt))
                  (current-print maybe-print-image)]
+    (install-print-handlers)
     (preload-help)
     (read-eval-print-loop)))
 

Reply via email to