Permalink
Browse files

added apropos (per Michel Salim, plus re support)

  • Loading branch information...
1 parent bbe248f commit 4f9a78d13217a8de4c29221d74aca0a67cec8c1a @stuarthalloway stuarthalloway committed Apr 12, 2010
@@ -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
@@ -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)))))

0 comments on commit 4f9a78d

Please sign in to comment.