From 43a2f1aff0404e6b2ac7708c10537912f30f9b86 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 5 Apr 2020 22:35:21 +0200 Subject: [PATCH] utils: Handle Booleans when reading Org-mode tables. --- utils-tests.rkt | 2 ++ utils.rkt | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/utils-tests.rkt b/utils-tests.rkt index 5331f79..7fc79fc 100644 --- a/utils-tests.rkt +++ b/utils-tests.rkt @@ -45,6 +45,8 @@ (check-equal? (string->any "14") 14) (check-equal? (read-org-sexp "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))") '(("a" "(and a b)") ("b" "(or b (not a))"))) + (check-equal? (read-org-sexp "(#t \"#t\" \"#t \" '(1 2 \"#f\"))") + '(#t #t "#t " '(1 2 #f))) (check-equal? (unstringify-pairs '(("a" . "1") ("b" . "(and a (not b))"))) '((a . 1) (b . (and a (not b))))) (check-equal? (unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))"))) diff --git a/utils.rkt b/utils.rkt index 3a72096..ef9ddae 100644 --- a/utils.rkt +++ b/utils.rkt @@ -197,9 +197,22 @@ (define (string->any str) (with-input-from-string str (λ () (read)))) +;;; Given a sexp, converts all "#f" to #f and "#t" to #t. +;;; +;;; When I read Org-mode tables, I pump them through a call to the +;;; prin1 because the elisp sexp seems incompatible with Racket. On +;;; the other hand, Racket Booleans seem to upset elisp a little, so +;;; prin1 wraps them in additional double quotes. This function +;;; removes those quotes. +(define/match (handle-org-booleans datum) + [("#t") #t] + [("#f") #f] + [((? list?)) (map handle-org-booleans datum)] + [ (_) datum]) + ;;; Reads a sexp from a string produced by Org-mode for a named table. ;;; See example.org for examples. -(define (read-org-sexp str) (string->any str)) +(define read-org-sexp (compose handle-org-booleans string->any)) ;;; A shortcut for read-org-sexp. (define unorg read-org-sexp)