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