diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2010-04-12 17:17:29 -0400 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-04-12 17:17:29 -0400 |
commit | 4f9a78d13217a8de4c29221d74aca0a67cec8c1a (patch) | |
tree | df1a52315a9f0cde9323654d576aa840df4c80c9 | |
parent | bbe248f90e6ec33d5e85d2267fa1caf8c7cb99a7 (diff) |
added apropos (per Michel Salim, plus re support)
-rw-r--r-- | src/main/clojure/clojure/contrib/repl_utils.clj | 18 | ||||
-rw-r--r-- | src/test/clojure/clojure/contrib/test_repl_utils.clj | 21 |
2 files changed, 36 insertions, 3 deletions
diff --git a/src/main/clojure/clojure/contrib/repl_utils.clj b/src/main/clojure/clojure/contrib/repl_utils.clj index 309e1bac..ebe8e699 100644 --- a/src/main/clojure/clojure/contrib/repl_utils.clj +++ b/src/main/clojure/clojure/contrib/repl_utils.clj @@ -9,15 +9,15 @@ ; Utilities meant to be used interactively at the REPL (ns - #^{:author "Chris Houser, Christophe Grand, Stephen Gilardi", + #^{:author "Chris Houser, Christophe Grand, Stephen Gilardi, Michel Salim", :doc "Utilities meant to be used interactively at the REPL"} clojure.contrib.repl-utils (:import (java.io File LineNumberReader InputStreamReader PushbackReader) (java.lang.reflect Modifier Method Constructor) (clojure.lang RT Compiler Compiler$C)) + (:require [clojure.contrib.string :as s]) (:use [clojure.contrib.seq :only (indexed)] - [clojure.contrib.javadoc.browse :only (browse-url)] - [clojure.contrib.string :as s :only ()])) + [clojure.contrib.javadoc.browse :only (browse-url)])) ;; ---------------------------------------------------------------------- ;; Examine Java classes @@ -125,6 +125,18 @@ [n] `(println (or (get-source '~n) (str "Source not found")))) +(defn apropos + "Given a regular expression or stringable thing, return a seq of +all definitions in all currently-loaded namespaces that match the +str-or-pattern." + [str-or-pattern] + (let [matches? (if (instance? java.util.regex.Pattern str-or-pattern) + #(re-find str-or-pattern (str %)) + #(s/substring? (str str-or-pattern) (str %)))] + (mapcat (fn [ns] + (filter matches? (keys (ns-publics ns)))) + (all-ns)))) + ;; ---------------------------------------------------------------------- ;; Handle Ctrl-C keystrokes diff --git a/src/test/clojure/clojure/contrib/test_repl_utils.clj b/src/test/clojure/clojure/contrib/test_repl_utils.clj new file mode 100644 index 00000000..ac8376e8 --- /dev/null +++ b/src/test/clojure/clojure/contrib/test_repl_utils.clj @@ -0,0 +1,21 @@ +(ns clojure.contrib.test-repl-utils + (:use clojure.test + clojure.contrib.repl-utils + [clojure.contrib.seq :only (includes?)])) + +(deftest test-apropos + (testing "with a regular expression" + (is (= '[defmacro] (apropos #"^defmacro$"))) + (is (includes? (apropos #"def.acr.") 'defmacro)) + (is (= [] (apropos #"nothing-has-this-name")))) + + + (testing "with a string" + (is (includes? (apropos "defmacro") 'defmacro)) + (is (includes? (apropos "efmac") 'defmacro)) + (is (= [] (apropos "nothing-has-this-name")))) + + (testing "with a symbol" + (is (includes? (apropos 'defmacro) 'defmacro)) + (is (includes? (apropos 'efmac) 'defmacro)) + (is (= [] (apropos 'nothing-has-this-name))))) |