Mercurial > lasercutter
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 the4 ; 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 this6 ; distribution.7 ; By using this software in any fashion, you are agreeing to be bound by8 ; the terms of this license.9 ; You must not remove this notice, or any other, from this software.11 ; thanks to Stuart Sierra13 ; 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 (def19 ^{:doc "Ref to a list of local paths for Javadoc-generated HTML20 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 (def29 ^{:doc "Ref to a map from package name prefixes to URLs for remote30 Javadocs."}31 *remote-javadocs*32 (ref (sorted-map33 "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-javadoc44 "Adds to the list of local Javadoc paths."45 [path]46 (dosync (commute *local-javadocs* conj path)))48 (defn add-remote-javadoc49 "Adds to the list of remote Javadoc URLs. package-prefix is the50 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-url55 "Searches for a URL for the given class name. Tries56 *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 (first62 (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 search72 (when *feeling-lucky* (str *feeling-lucky-url* url-path ".html"))))))74 (defn javadoc75 "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-object80 (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))))