From 6e50ecad9973423d284216e4ee287271627d4468 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Fri, 26 Aug 2011 09:42:56 -0400 Subject: [PATCH] Add `$1', `$2', ..., `$5' as last-value references. --- collects/tests/xrepl/xrepl.rkt | 2 ++ collects/xrepl/xrepl.rkt | 32 +++++++++++++++++++------------- collects/xrepl/xrepl.scrbl | 9 ++++++--- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/collects/tests/xrepl/xrepl.rkt b/collects/tests/xrepl/xrepl.rkt index 1af636c0f6..40afaf2f72 100644 --- a/collects/tests/xrepl/xrepl.rkt +++ b/collects/tests/xrepl/xrepl.rkt @@ -87,6 +87,8 @@ 4 -> «(list ^ ^^ ^^^ ^^^^)» '(4 3 2 1) + -> «(list $1 $2 $3 $4 $5)» + '((4 3 2 1) 4 3 2 1) -> «(collect-garbage)» -> «^» ; ^: saved value #1 was garbage-collected [,bt for context] diff --git a/collects/xrepl/xrepl.rkt b/collects/xrepl/xrepl.rkt index 263d3f47fa..9272818264 100644 --- a/collects/xrepl/xrepl.rkt +++ b/collects/xrepl/xrepl.rkt @@ -3,10 +3,10 @@ ;; ---------------------------------------------------------------------------- ;; customization -(define toplevel-prefix (make-parameter "-")) ; when not in a module -(define saved-values-number (make-parameter 5)) -(define saved-values-char (make-parameter #\^)) -(define wrap-column (make-parameter 79)) +(define toplevel-prefix (make-parameter "-")) ; when not in a module +(define saved-values-number (make-parameter 5)) +(define saved-values-patterns (make-parameter '("^" "$~a"))) +(define wrap-column (make-parameter 79)) ;; TODO: when there's a few more of these, make them come from the prefs ;; ---------------------------------------------------------------------------- @@ -1209,16 +1209,22 @@ (define last-saved-names+state (make-parameter '(#f #f #f))) (define (get-saved-names) - (define last (last-saved-names+state)) - (define last-num (cadr last)) - (define last-char (caddr last)) - (define cur-num (saved-values-number)) - (define cur-char (saved-values-char)) - (if (and (equal? last-num cur-num) (equal? last-char cur-char)) + (define last (last-saved-names+state)) + (define last-num (cadr last)) + (define last-ptrns (caddr last)) + (define cur-num (saved-values-number)) + (define cur-ptrns (saved-values-patterns)) + (if (and (equal? last-num cur-num) (equal? last-ptrns cur-ptrns)) (car last) - (let ([new (for/list ([i (in-range (saved-values-number))]) - (string->symbol (make-string (add1 i) (saved-values-char))))]) - (last-saved-names+state (list new cur-num cur-char)) + (let ([new + (for*/list ([i (in-range 1 (add1 (saved-values-number)))] + [p (in-list cur-ptrns)]) + (string->symbol + (cond + [(= 1 (string-length p)) (make-string i (string-ref p 0))] + [(regexp-match? #rx"^[^~]*~a[^~]*$" p) (format p i)] + [else (error 'saved-names "bad name pattern: ~e" p)])))]) + (last-saved-names+state (list new cur-num cur-ptrns)) new))) ;; see comment at the top of this module for the below hair diff --git a/collects/xrepl/xrepl.scrbl b/collects/xrepl/xrepl.scrbl index dd3f5493de..68b019eaf6 100644 --- a/collects/xrepl/xrepl.scrbl +++ b/collects/xrepl/xrepl.scrbl @@ -451,9 +451,12 @@ The rationale for this is that @racketidfont{^} always refers to the last @emph{printed} result, @racketidfont{^^} to the one before that, etc. -The bindings are made available only if they are not already defined. -This means that if you have code that uses these names, it will continue -to work as usual. +In addition to these names, XREPL also binds @racketidfont{$1}, +@racketidfont{$2}, ..., @racketidfont{$5} to the same references, so you +can choose the style that you like. All of these bindings are made +available only if they are not already defined. This means that if you +have code that uses these names, it will continue to work as usual (and +it will shadow the saved value binding). The bindings are identifier macros that expand to the literal saved values; so referring to a saved value that is missing (because not