summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicardo Wurmus <rekado@elephly.net>2018-04-06 14:40:44 +0200
committerRicardo Wurmus <rekado@elephly.net>2018-04-06 14:40:44 +0200
commitefd3a67e9a72bfe37cb69f88da0151fc2d98d5f3 (patch)
tree5678d4d4de3f1af0f559e6b1957f0acfd28a64a1
parent7991e188c17bc91a45fc277e487834200bc1783d (diff)
Let transform-modifier take PROC instead of VALUE.
-rw-r--r--pict.scm26
1 files changed, 19 insertions, 7 deletions
diff --git a/pict.scm b/pict.scm
index db5df15..957910a 100644
--- a/pict.scm
+++ b/pict.scm
@@ -179,17 +179,29 @@ of transform lists LST."
(string-append key "(" val ")")))
lst)))
-(define (transform-modifier key value)
+(define (transform-modifier key proc)
+ "Return a function that replaces KEY in the transforms of a pict
+with the return value of PROC applied to the current value. If KEY
+does not exist it is added with PROC applied to #F."
(attribute-modifier
'transform
(lambda (transform-str)
(transform-list->string
- (cons (list key value)
- (filter (match-lambda
- ((k _)
- (not (string=? k key)))
- (_ '()))
- (transform-string->list (or transform-str ""))))))))
+ (match (fold (lambda (item state)
+ (match item
+ ((k v)
+ (if (string=? k key)
+ (cons #t ; replaced!
+ (cons (list key (proc v))
+ (cdr state)))
+ (cons (car state)
+ (cons item (cdr state)))))))
+ '(#f . ())
+ (transform-string->list (or transform-str "")))
+ ((#f . items)
+ (cons (list key (proc #f))
+ items))
+ ((_ . items) items))))))
(define (style-string->list styles)
"Split the styles string into a list of lists from keys to values."