view src/clojure/contrib/ns_utils.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
2 ;; distribution terms for this software are covered by the Eclipse Public
3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
4 ;; be found in the file epl-v10.html at the root of this distribution. By
5 ;; using this software in any fashion, you are agreeing to be bound by the
6 ;; terms of this license. You must not remove this notice, or any other,
7 ;; from this software.
9 ;; scgilardi (gmail)
10 ;; 23 April 2008
12 ;; DEPRECATED in 1.2: dir and print-dir. Use dir and dir-fn in
13 ;; clojure.repl.
15 (ns
16 ^{:author "Stephen C. Gilardi",
17 :doc "Namespace utilities
19 get-ns returns the namespace named by a symbol or throws
20 if the namespace does not exist
22 ns-vars returns a sorted seq of symbols naming public vars
23 in a namespace
25 print-docs prints documentation for the public vars in a
26 namespace
28 immigrate Create a public var in this namespace for each
29 public var in the namespaces named by ns-names.
30 From James Reeves
32 vars returns a sorted seq of symbols naming public vars
33 in a namespace (macro)
35 docs prints documentation for the public vars in a
36 namespace (macro)"}
37 clojure.contrib.ns-utils
38 (:use clojure.contrib.except))
40 ;; Namespace Utilities
42 (defn get-ns
43 "Returns the namespace named by ns-sym or throws if the
44 namespace does not exist"
45 [ns-sym]
46 (let [ns (find-ns ns-sym)]
47 (throw-if (not ns) "Unable to find namespace: %s" ns-sym)
48 ns))
50 (defn ns-vars
51 "Returns a sorted seq of symbols naming public vars in
52 a namespace"
53 [ns]
54 (sort (map first (ns-publics ns))))
56 (defn print-dir
57 "Prints a sorted directory of public vars in a namespace"
58 {:deprecated "1.2"}
59 [ns]
60 (doseq [item (ns-vars ns)]
61 (println item)))
63 (defn print-docs
64 "Prints documentation for the public vars in a namespace"
65 [ns]
66 (doseq [item (ns-vars ns)]
67 (print-doc (ns-resolve ns item))))
69 ;; Convenience
71 (defmacro vars
72 "Returns a sorted seq of symbols naming public vars in
73 a namespace"
74 [nsname]
75 `(ns-vars (get-ns '~nsname)))
77 (defmacro dir
78 "Prints a sorted directory of public vars in a namespace"
79 {:deprecated "1.2"}
80 [nsname]
81 `(print-dir (get-ns '~nsname)))
83 (defmacro docs
84 "Prints documentation for the public vars in a namespace"
85 [nsname]
86 `(print-docs (get-ns '~nsname)))
88 (defn immigrate
89 "Create a public var in this namespace for each public var in the
90 namespaces named by ns-names. The created vars have the same name, root
91 binding, and metadata as the original except that their :ns metadata
92 value is this namespace."
93 [& ns-names]
94 (doseq [ns ns-names]
95 (require ns)
96 (doseq [[sym var] (ns-publics ns)]
97 (let [sym (with-meta sym (assoc (meta var) :ns *ns*))]
98 (if (.hasRoot var)
99 (intern *ns* sym (.getRoot var))
100 (intern *ns* sym))))))