view src/clojure/contrib/repl_utils/javadoc.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) Christophe Grand, November 2008. All rights reserved.
3 ; The use and distribution terms for this software are covered by the
4 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5 ; which can be found in the file epl-v10.html at the root of this
6 ; distribution.
7 ; By using this software in any fashion, you are agreeing to be bound by
8 ; the terms of this license.
9 ; You must not remove this notice, or any other, from this software.
11 ; thanks to Stuart Sierra
13 ; a repl helper to quickly open javadocs.
15 (def *feeling-lucky-url* "http://www.google.com/search?btnI=I%27m%20Feeling%20Lucky&q=allinurl:")
16 (def *feeling-lucky* true)
18 (def
19 ^{:doc "Ref to a list of local paths for Javadoc-generated HTML
20 files."}
21 *local-javadocs* (ref (list)))
23 (def *core-java-api*
24 (if (= "1.5" (System/getProperty "java.specification.version"))
25 "http://java.sun.com/j2se/1.5.0/docs/api/"
26 "http://java.sun.com/javase/6/docs/api/"))
28 (def
29 ^{:doc "Ref to a map from package name prefixes to URLs for remote
30 Javadocs."}
31 *remote-javadocs*
32 (ref (sorted-map
33 "java." *core-java-api*
34 "javax." *core-java-api*
35 "org.ietf.jgss." *core-java-api*
36 "org.omg." *core-java-api*
37 "org.w3c.dom." *core-java-api*
38 "org.xml.sax." *core-java-api*
39 "org.apache.commons.codec." "http://commons.apache.org/codec/api-release/"
40 "org.apache.commons.io." "http://commons.apache.org/io/api-release/"
41 "org.apache.commons.lang." "http://commons.apache.org/lang/api-release/")))
43 (defn add-local-javadoc
44 "Adds to the list of local Javadoc paths."
45 [path]
46 (dosync (commute *local-javadocs* conj path)))
48 (defn add-remote-javadoc
49 "Adds to the list of remote Javadoc URLs. package-prefix is the
50 beginning of the package name that has docs at this URL."
51 [package-prefix url]
52 (dosync (commute *remote-javadocs* assoc package-prefix url)))
54 (defn find-javadoc-url
55 "Searches for a URL for the given class name. Tries
56 *local-javadocs* first, then *remote-javadocs*. Returns a string."
57 {:tag String}
58 [^String classname]
59 (let [file-path (.replace classname \. File/separatorChar)
60 url-path (.replace classname \. \/)]
61 (if-let [file ^File (first
62 (filter #(.exists ^File %)
63 (map #(File. (str %) (str file-path ".html"))
64 @*local-javadocs*)))]
65 (-> file .toURI str)
66 ;; If no local file, try remote URLs:
67 (or (some (fn [[prefix url]]
68 (when (.startsWith classname prefix)
69 (str url url-path ".html")))
70 @*remote-javadocs*)
71 ;; if *feeling-lucky* try a web search
72 (when *feeling-lucky* (str *feeling-lucky-url* url-path ".html"))))))
74 (defn javadoc
75 "Opens a browser window displaying the javadoc for the argument.
76 Tries *local-javadocs* first, then *remote-javadocs*."
77 [class-or-object]
78 (let [^Class c (if (instance? Class class-or-object)
79 class-or-object
80 (class class-or-object))]
81 (if-let [url (find-javadoc-url (.getName c))]
82 (browse-url url)
83 (println "Could not find Javadoc for" c))))