Add $1', $2', ..., `$5' as last-value references.

This commit is contained in:
Eli Barzilay 2011-08-26 09:42:56 -04:00
parent 15211fbb42
commit 6e50ecad99
3 changed files with 27 additions and 16 deletions

View File

@ -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]

View File

@ -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

View File

@ -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