Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 83 lines (74 sloc) 3.237 kb
68658ce @stuarthalloway 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
16 (def *feeling-lucky-url* "http://www.google.com/search?btnI=I%27m%20Feeling%20Lucky&q=allinurl:")
17 (def *feeling-lucky* true)
18
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
19 (def *local-javadocs* (ref (list)))
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
20
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/"))
25
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
26 (def *remote-javadocs*
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
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/")))
37
38 (defn add-local-javadoc
39 "Adds to the list of local Javadoc paths."
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
40 {:added "1.2"}
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
41 [path]
42 (dosync (commute *local-javadocs* conj path)))
43
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."
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
47 {:added "1.2"}
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
48 [package-prefix url]
49 (dosync (commute *remote-javadocs* assoc package-prefix url)))
50
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
51 (defn- javadoc-url
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
52 "Searches for a URL for the given class name. Tries
53 *local-javadocs* first, then *remote-javadocs*. Returns a string."
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
54 {:tag String,
55 :added "1.2"}
56 [^String classname]
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
57 (let [file-path (.replace classname \. File/separatorChar)
58 url-path (.replace classname \. \/)]
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
59 (if-let [file ^File (first
60 (filter #(.exists ^File %)
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
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"))))))
71
72 (defn javadoc
73 "Opens a browser window displaying the javadoc for the argument.
74 Tries *local-javadocs* first, then *remote-javadocs*."
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
75 {:added "1.2"}
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
76 [class-or-object]
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
77 (let [^Class c (if (instance? Class class-or-object)
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
78 class-or-object
79 (class class-or-object))]
89ed54e @stuarthalloway tweaking what gets exposed (javadoc #357)
stuarthalloway authored
80 (if-let [url (javadoc-url (.getName c))]
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
81 (browse-url url)
82 (println "Could not find Javadoc for" c))))
Something went wrong with that request. Please try again.