rlm@10: ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and rlm@10: ;; distribution terms for this software are covered by the Eclipse Public rlm@10: ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can rlm@10: ;; be found in the file epl-v10.html at the root of this distribution. By rlm@10: ;; using this software in any fashion, you are agreeing to be bound by the rlm@10: ;; terms of this license. You must not remove this notice, or any other, rlm@10: ;; from this software. rlm@10: rlm@10: ;; scgilardi (gmail) rlm@10: ;; 23 April 2008 rlm@10: rlm@10: ;; DEPRECATED in 1.2: dir and print-dir. Use dir and dir-fn in rlm@10: ;; clojure.repl. rlm@10: rlm@10: (ns rlm@10: ^{:author "Stephen C. Gilardi", rlm@10: :doc "Namespace utilities rlm@10: rlm@10: get-ns returns the namespace named by a symbol or throws rlm@10: if the namespace does not exist rlm@10: rlm@10: ns-vars returns a sorted seq of symbols naming public vars rlm@10: in a namespace rlm@10: rlm@10: print-docs prints documentation for the public vars in a rlm@10: namespace rlm@10: rlm@10: immigrate Create a public var in this namespace for each rlm@10: public var in the namespaces named by ns-names. rlm@10: From James Reeves rlm@10: rlm@10: vars returns a sorted seq of symbols naming public vars rlm@10: in a namespace (macro) rlm@10: rlm@10: docs prints documentation for the public vars in a rlm@10: namespace (macro)"} rlm@10: clojure.contrib.ns-utils rlm@10: (:use clojure.contrib.except)) rlm@10: rlm@10: ;; Namespace Utilities rlm@10: rlm@10: (defn get-ns rlm@10: "Returns the namespace named by ns-sym or throws if the rlm@10: namespace does not exist" rlm@10: [ns-sym] rlm@10: (let [ns (find-ns ns-sym)] rlm@10: (throw-if (not ns) "Unable to find namespace: %s" ns-sym) rlm@10: ns)) rlm@10: rlm@10: (defn ns-vars rlm@10: "Returns a sorted seq of symbols naming public vars in rlm@10: a namespace" rlm@10: [ns] rlm@10: (sort (map first (ns-publics ns)))) rlm@10: rlm@10: (defn print-dir rlm@10: "Prints a sorted directory of public vars in a namespace" rlm@10: {:deprecated "1.2"} rlm@10: [ns] rlm@10: (doseq [item (ns-vars ns)] rlm@10: (println item))) rlm@10: rlm@10: (defn print-docs rlm@10: "Prints documentation for the public vars in a namespace" rlm@10: [ns] rlm@10: (doseq [item (ns-vars ns)] rlm@10: (print-doc (ns-resolve ns item)))) rlm@10: rlm@10: ;; Convenience rlm@10: rlm@10: (defmacro vars rlm@10: "Returns a sorted seq of symbols naming public vars in rlm@10: a namespace" rlm@10: [nsname] rlm@10: `(ns-vars (get-ns '~nsname))) rlm@10: rlm@10: (defmacro dir rlm@10: "Prints a sorted directory of public vars in a namespace" rlm@10: {:deprecated "1.2"} rlm@10: [nsname] rlm@10: `(print-dir (get-ns '~nsname))) rlm@10: rlm@10: (defmacro docs rlm@10: "Prints documentation for the public vars in a namespace" rlm@10: [nsname] rlm@10: `(print-docs (get-ns '~nsname))) rlm@10: rlm@10: (defn immigrate rlm@10: "Create a public var in this namespace for each public var in the rlm@10: namespaces named by ns-names. The created vars have the same name, root rlm@10: binding, and metadata as the original except that their :ns metadata rlm@10: value is this namespace." rlm@10: [& ns-names] rlm@10: (doseq [ns ns-names] rlm@10: (require ns) rlm@10: (doseq [[sym var] (ns-publics ns)] rlm@10: (let [sym (with-meta sym (assoc (meta var) :ns *ns*))] rlm@10: (if (.hasRoot var) rlm@10: (intern *ns* sym (.getRoot var)) rlm@10: (intern *ns* sym))))))