From ea9d25cb5b79fd3a5f0d934d1003074597a28b28 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 17 Feb 2016 16:58:49 -0500 Subject: [PATCH] adds support for renaming to import-vars --- src/potemkin/namespaces.clj | 41 ++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/potemkin/namespaces.clj b/src/potemkin/namespaces.clj index 320e725..876464c 100644 --- a/src/potemkin/namespaces.clj +++ b/src/potemkin/namespaces.clj @@ -74,29 +74,36 @@ (link-vars ~vr (var ~n)) ~vr)))) +(defn- unravel* [ns x] + (let [[sym new-name] (if (sequential? x) + x + [x x])] + [(symbol + (str ns + (when-let [n (namespace sym)] + (str "." n))) + (name sym)) + new-name])) + +(defn- unravel [x] + (if (sequential? x) + (->> x + rest + (map #(unravel* (first x) %))) + [[x x]])) + (defmacro import-vars "Imports a list of vars from other namespaces." [& syms] - (let [unravel (fn unravel [x] - (if (sequential? x) - (->> x - rest - (mapcat unravel) - (map - #(symbol - (str (first x) - (when-let [n (namespace %)] - (str "." n))) - (name %)))) - [x])) - syms (mapcat unravel syms)] + (let [syms (mapcat unravel syms)] `(do ~@(map - (fn [sym] + (fn [[sym new-name]] (let [vr (resolve sym) m (meta vr)] (cond - (:macro m) `(import-macro ~sym) - (:arglists m) `(import-fn ~sym) - :else `(import-def ~sym)))) + (:macro m) `(import-macro ~sym ~new-name) + (:arglists m) `(import-fn ~sym ~new-name) + :else `(import-def ~sym ~new-name)))) syms)))) +