Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 84 lines (75 sloc) 3.336 kb
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
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)))
15
b9b1a09 Rich Hickey require dynamically rebindable vars be explicitly declared dynamic, via ...
richhickey authored
16 (def ^:dynamic *feeling-lucky-url* "http://www.google.com/search?btnI=I%27m%20Feeling%20Lucky&q=allinurl:")
17 (def ^:dynamic *feeling-lucky* true)
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
18
b9b1a09 Rich Hickey require dynamically rebindable vars be explicitly declared dynamic, via ...
richhickey authored
19 (def ^:dynamic *local-javadocs* (ref (list)))
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
20
b9b1a09 Rich Hickey require dynamically rebindable vars be explicitly declared dynamic, via ...
richhickey authored
21 (def ^:dynamic *core-java-api*
be9ff49 Stuart Halloway CLJ-846 detect java 7 javadoc
stuarthalloway authored
22 (case (System/getProperty "java.specification.version")
23 "1.5" "http://java.sun.com/j2se/1.5.0/docs/api/"
24 "1.6" "http://java.sun.com/javase/6/docs/api/"
25 "http://java.sun.com/javase/7/docs/api/"))
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
26
b9b1a09 Rich Hickey require dynamically rebindable vars be explicitly declared dynamic, via ...
richhickey authored
27 (def ^:dynamic *remote-javadocs*
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
28 (ref (sorted-map
29 "java." *core-java-api*
30 "javax." *core-java-api*
31 "org.ietf.jgss." *core-java-api*
32 "org.omg." *core-java-api*
33 "org.w3c.dom." *core-java-api*
34 "org.xml.sax." *core-java-api*
35 "org.apache.commons.codec." "http://commons.apache.org/codec/api-release/"
36 "org.apache.commons.io." "http://commons.apache.org/io/api-release/"
37 "org.apache.commons.lang." "http://commons.apache.org/lang/api-release/")))
38
39 (defn add-local-javadoc
40 "Adds to the list of local Javadoc paths."
89ed54e Stuart Halloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
41 {:added "1.2"}
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
42 [path]
43 (dosync (commute *local-javadocs* conj path)))
44
45 (defn add-remote-javadoc
46 "Adds to the list of remote Javadoc URLs. package-prefix is the
47 beginning of the package name that has docs at this URL."
89ed54e Stuart Halloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
48 {:added "1.2"}
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
49 [package-prefix url]
50 (dosync (commute *remote-javadocs* assoc package-prefix url)))
51
89ed54e Stuart Halloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
52 (defn- javadoc-url
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
53 "Searches for a URL for the given class name. Tries
54 *local-javadocs* first, then *remote-javadocs*. Returns a string."
89ed54e Stuart Halloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
55 {:tag String,
56 :added "1.2"}
57 [^String classname]
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
58 (let [file-path (.replace classname \. File/separatorChar)
59 url-path (.replace classname \. \/)]
89ed54e Stuart Halloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
60 (if-let [file ^File (first
61 (filter #(.exists ^File %)
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
62 (map #(File. (str %) (str file-path ".html"))
63 @*local-javadocs*)))]
64 (-> file .toURI str)
65 ;; If no local file, try remote URLs:
66 (or (some (fn [[prefix url]]
67 (when (.startsWith classname prefix)
68 (str url url-path ".html")))
69 @*remote-javadocs*)
70 ;; if *feeling-lucky* try a web search
71 (when *feeling-lucky* (str *feeling-lucky-url* url-path ".html"))))))
72
73 (defn javadoc
74 "Opens a browser window displaying the javadoc for the argument.
75 Tries *local-javadocs* first, then *remote-javadocs*."
89ed54e Stuart Halloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
76 {:added "1.2"}
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
77 [class-or-object]
89ed54e Stuart Halloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
78 (let [^Class c (if (instance? Class class-or-object)
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
79 class-or-object
80 (class class-or-object))]
89ed54e Stuart Halloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
81 (if-let [url (javadoc-url (.getName c))]
68658ce Stuart Halloway everything javadoc needs from contrib
stuarthalloway authored
82 (browse-url url)
83 (println "Could not find Javadoc for" c))))
Something went wrong with that request. Please try again.