Skip to content

Commit

Permalink
Let's make a macro!
Browse files Browse the repository at this point in the history
This macro takes a namespace and a list of identifiers and creates
definitions for them.
  • Loading branch information
mwunsch committed Apr 25, 2017
1 parent e3a7e98 commit cabda55
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion ffi/unsafe/introspection.rkt
Expand Up @@ -8,7 +8,10 @@
(only-in racket/list index-of filter-map)
(only-in racket/string string-join string-replace)
(only-in racket/function curry curryr)
(for-syntax racket/base syntax/parse))
(for-syntax racket/base
racket/syntax
(only-in racket/function curry curryr)
(only-in racket/string string-replace)))

(define-ffi-definer define-gir (ffi-lib "libgirepository-1.0"))

Expand Down Expand Up @@ -768,3 +771,21 @@

(define (introspection namespace [version #f])
(gir-require namespace version))

(define-syntax (gir/require stx)
(syntax-case stx ()
[(_ namespace (names ...))
(let ([c-id->racket-id (lambda (str)
((compose1 string->symbol
(curryr string-replace "_" "-")
string-downcase)
(regexp-replace* #rx"([a-z]+)([A-Z]+)" str "\\1-\\2")))]
[string-names (map (compose1 symbol->string syntax-e)
(syntax->list #'(names ...)))])
(with-syntax ([(pretty-names ...) (map (curry format-id stx "~a") (map c-id->racket-id string-names))])
#`(begin
(define pretty-names
(or (gir-find-by-name namespace 'names)
(raise-argument-error 'pretty-names
(format "(gir-member/c ~v)" namespace)
'names))) ...)))]))

0 comments on commit cabda55

Please sign in to comment.