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