aboutsummaryrefslogtreecommitdiff
path: root/modules/ns-utils
diff options
context:
space:
mode:
Diffstat (limited to 'modules/ns-utils')
-rw-r--r--modules/ns-utils/pom.xml21
-rw-r--r--modules/ns-utils/src/main/clojure/clojure/contrib/ns_utils.clj106
2 files changed, 127 insertions, 0 deletions
diff --git a/modules/ns-utils/pom.xml b/modules/ns-utils/pom.xml
new file mode 100644
index 00000000..bbb86402
--- /dev/null
+++ b/modules/ns-utils/pom.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+ http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.clojure.contrib</groupId>
+ <artifactId>parent</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ <relativePath>../parent</relativePath>
+ </parent>
+ <artifactId>ns-utils</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>org.clojure.contrib</groupId>
+ <artifactId>except</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+</project> \ No newline at end of file
diff --git a/modules/ns-utils/src/main/clojure/clojure/contrib/ns_utils.clj b/modules/ns-utils/src/main/clojure/clojure/contrib/ns_utils.clj
new file mode 100644
index 00000000..ba8c43ce
--- /dev/null
+++ b/modules/ns-utils/src/main/clojure/clojure/contrib/ns_utils.clj
@@ -0,0 +1,106 @@
+;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
+;; distribution terms for this software are covered by the Eclipse Public
+;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
+;; be found in the file epl-v10.html at the root of this distribution. By
+;; using this software in any fashion, you are agreeing to be bound by the
+;; terms of this license. You must not remove this notice, or any other,
+;; from this software.
+;;
+;; ns-utils
+;;
+;; Namespace Utilities
+;;
+;; 'get-ns' returns the namespace named by a symbol or throws
+;; if the namespace does not exist
+;;
+;; 'ns-vars' returns a sorted seq of symbols naming public vars
+;; in a namespace
+;;
+;; 'print-dir' prints a sorted directory of public vars in a
+;; namespace
+;;
+;; 'print-docs' prints documentation for the public vars in a
+;; namespace
+;;
+;; 'immigrate' Create a public var in this namespace for each
+;; public var in the namespaces named by ns-names.
+;; From James Reeves
+;; Convenience
+;;
+;; 'vars' returns a sorted seq of symbols naming public vars
+;; in a namespace (macro)
+;;
+;; 'dir' prints a sorted directory of public vars in a
+;; namespace (macro)
+;;
+;; 'docs' prints documentation for the public vars in a
+;; namespace (macro)
+;;
+;; scgilardi (gmail)
+;; 23 April 2008
+
+(ns
+ ^{:author "Stephen C. Gilardi",
+ :doc "Namespace utilities"}
+ clojure.contrib.ns-utils
+ (:use clojure.contrib.except))
+
+;; Namespace Utilities
+
+(defn get-ns
+ "Returns the namespace named by ns-sym or throws if the
+ namespace does not exist"
+ [ns-sym]
+ (let [ns (find-ns ns-sym)]
+ (throw-if (not ns) "Unable to find namespace: %s" ns-sym)
+ ns))
+
+(defn ns-vars
+ "Returns a sorted seq of symbols naming public vars in
+ a namespace"
+ [ns]
+ (sort (map first (ns-publics ns))))
+
+(defn print-dir
+ "Prints a sorted directory of public vars in a namespace"
+ [ns]
+ (doseq [item (ns-vars ns)]
+ (println item)))
+
+(defn print-docs
+ "Prints documentation for the public vars in a namespace"
+ [ns]
+ (doseq [item (ns-vars ns)]
+ (print-doc (ns-resolve ns item))))
+
+;; Convenience
+
+(defmacro vars
+ "Returns a sorted seq of symbols naming public vars in
+ a namespace"
+ [nsname]
+ `(ns-vars (get-ns '~nsname)))
+
+(defmacro dir
+ "Prints a sorted directory of public vars in a namespace"
+ [nsname]
+ `(print-dir (get-ns '~nsname)))
+
+(defmacro docs
+ "Prints documentation for the public vars in a namespace"
+ [nsname]
+ `(print-docs (get-ns '~nsname)))
+
+(defn immigrate
+ "Create a public var in this namespace for each public var in the
+ namespaces named by ns-names. The created vars have the same name, root
+ binding, and metadata as the original except that their :ns metadata
+ value is this namespace."
+ [& ns-names]
+ (doseq [ns ns-names]
+ (require ns)
+ (doseq [[sym var] (ns-publics ns)]
+ (let [sym (with-meta sym (assoc (meta var) :ns *ns*))]
+ (if (.hasRoot var)
+ (intern *ns* sym (.getRoot var))
+ (intern *ns* sym))))))