view src/clojure/repl.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) Chris Houser, Dec 2008. All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
4 ; which can be found in the file CPL.TXT at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
9 ; Utilities meant to be used interactively at the REPL
11 (ns
12 #^{:author "Chris Houser, Christophe Grand, Stephen Gilardi, Michel Salim, Christophe Grande"
13 :doc "Utilities meant to be used interactively at the REPL"}
14 clojure.repl
15 (:import (java.io LineNumberReader InputStreamReader PushbackReader)
16 (clojure.lang RT Reflector)))
18 ;; ----------------------------------------------------------------------
19 ;; Examine Clojure functions (Vars, really)
21 (defn source-fn
22 "Returns a string of the source code for the given symbol, if it can
23 find it. This requires that the symbol resolve to a Var defined in
24 a namespace for which the .clj is in the classpath. Returns nil if
25 it can't find the source. For most REPL usage, 'source' is more
26 convenient.
28 Example: (source-fn 'filter)"
29 [x]
30 (when-let [v (resolve x)]
31 (when-let [filepath (:file (meta v))]
32 (when-let [strm (.getResourceAsStream (RT/baseLoader) filepath)]
33 (with-open [rdr (LineNumberReader. (InputStreamReader. strm))]
34 (dotimes [_ (dec (:line (meta v)))] (.readLine rdr))
35 (let [text (StringBuilder.)
36 pbr (proxy [PushbackReader] [rdr]
37 (read [] (let [i (proxy-super read)]
38 (.append text (char i))
39 i)))]
40 (read (PushbackReader. pbr))
41 (str text)))))))
43 (defmacro source
44 "Prints the source code for the given symbol, if it can find it.
45 This requires that the symbol resolve to a Var defined in a
46 namespace for which the .clj is in the classpath.
48 Example: (source filter)"
49 [n]
50 `(println (or (source-fn '~n) (str "Source not found"))))
52 (defn apropos
53 "Given a regular expression or stringable thing, return a seq of
54 all definitions in all currently-loaded namespaces that match the
55 str-or-pattern."
56 [str-or-pattern]
57 (let [matches? (if (instance? java.util.regex.Pattern str-or-pattern)
58 #(re-find str-or-pattern (str %))
59 #(.contains (str %) (str str-or-pattern)))]
60 (mapcat (fn [ns]
61 (filter matches? (keys (ns-publics ns))))
62 (all-ns))))
64 (defn dir-fn
65 "Returns a sorted seq of symbols naming public vars in
66 a namespace"
67 [ns]
68 (sort (map first (ns-publics (the-ns ns)))))
70 (defmacro dir
71 "Prints a sorted directory of public vars in a namespace"
72 [nsname]
73 `(doseq [v# (dir-fn '~nsname)]
74 (println v#)))