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