diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2010-05-25 22:36:31 -0400 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-05-28 07:52:54 -0400 |
commit | 68658ce44f0678dc3940b9a3537ba60466ad7a8b (patch) | |
tree | 2181b8a46321b67946e492fc047f10ea0b7f9c5e | |
parent | d26b89a3af76653a3c13c8afbf65389529d6fc57 (diff) |
everything javadoc needs from contrib
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r-- | src/clj/clojure/java/browse.clj | 49 | ||||
-rw-r--r-- | src/clj/clojure/java/browse_ui.clj | 31 | ||||
-rw-r--r-- | src/clj/clojure/java/javadoc.clj | 83 | ||||
-rw-r--r-- | src/clj/clojure/java/shell.clj (renamed from src/clj/clojure/shell.clj) | 3 |
4 files changed, 164 insertions, 2 deletions
diff --git a/src/clj/clojure/java/browse.clj b/src/clj/clojure/java/browse.clj new file mode 100644 index 00000000..a65b4bcd --- /dev/null +++ b/src/clj/clojure/java/browse.clj @@ -0,0 +1,49 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +(ns + #^{:author "Christophe Grand", + :doc "Start a web browser from Clojure"} + clojure.java.browse + (:require [clojure.java.shell :as sh]) + (:import (java.net URI))) + +(defn- macosx? [] + (-> "os.name" System/getProperty .toLowerCase + (.startsWith "mac os x"))) + +(def *open-url-script* (when (macosx?) "/usr/bin/open")) + +(defn- open-url-in-browser + "Opens url (a string) in the default system web browser. May not + work on all platforms. Returns url on success, nil if not + supported." + [url] + (try + (when (clojure.lang.Reflector/invokeStaticMethod "java.awt.Desktop" + "isDesktopSupported" (to-array nil)) + (-> (clojure.lang.Reflector/invokeStaticMethod "java.awt.Desktop" + "getDesktop" (to-array nil)) + (.browse (URI. url))) + url) + (catch ClassNotFoundException e + nil))) + +(defn- open-url-in-swing + "Opens url (a string) in a Swing window." + [url] + ; the implementation of this function resides in another namespace to be loaded "on demand" + ; this fixes a bug on mac os x where the process turns into a GUI app + ; see http://code.google.com/p/clojure-contrib/issues/detail?id=32 + (require 'clojure.contrib.javadoc.browse-ui) + ((find-var 'clojure.contrib.javadoc.browse-ui/open-url-in-swing) url)) + +(defn browse-url [url] + (or (open-url-in-browser url) + (when *open-url-script* (sh/sh *open-url-script* (str url)) true) + (open-url-in-swing url))) diff --git a/src/clj/clojure/java/browse_ui.clj b/src/clj/clojure/java/browse_ui.clj new file mode 100644 index 00000000..a0d15a3a --- /dev/null +++ b/src/clj/clojure/java/browse_ui.clj @@ -0,0 +1,31 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +(ns + #^{:author "Christophe Grand", + :doc "Helper namespace for clojure.java.browse. + Prevents console apps from becoming GUI unnecessarily."} + clojure.java.browse-ui) + +(defn open-url-in-swing + "Opens url (a string) in a Swing window." + [url] + (let [htmlpane (javax.swing.JEditorPane. url)] + (.setEditable htmlpane false) + (.addHyperlinkListener htmlpane + (proxy [javax.swing.event.HyperlinkListener] [] + (hyperlinkUpdate [#^javax.swing.event.HyperlinkEvent e] + (when (= (.getEventType e) (. javax.swing.event.HyperlinkEvent$EventType ACTIVATED)) + (if (instance? javax.swing.text.html.HTMLFrameHyperlinkEvent e) + (-> htmlpane .getDocument (.processHTMLFrameHyperlinkEvent e)) + (.setPage htmlpane (.getURL e))))))) + (doto (javax.swing.JFrame.) + (.setContentPane (javax.swing.JScrollPane. htmlpane)) + (.setBounds 32 32 700 900) + (.show)))) + diff --git a/src/clj/clojure/java/javadoc.clj b/src/clj/clojure/java/javadoc.clj new file mode 100644 index 00000000..e64ecf1a --- /dev/null +++ b/src/clj/clojure/java/javadoc.clj @@ -0,0 +1,83 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. +(ns + ^{:author "Christophe Grand, Stuart Sierra", + :doc "A repl helper to quickly open javadocs."} + clojure.java.javadoc + (:use [clojure.java.browse :only (browse-url)] ) + (:import + (java.io File))) + +(def *feeling-lucky-url* "http://www.google.com/search?btnI=I%27m%20Feeling%20Lucky&q=allinurl:") +(def *feeling-lucky* true) + +(def + #^{:doc "Ref to a list of local paths for Javadoc-generated HTML files."} + *local-javadocs* (ref (list))) + +(def *core-java-api* + (if (= "1.5" (System/getProperty "java.specification.version")) + "http://java.sun.com/j2se/1.5.0/docs/api/" + "http://java.sun.com/javase/6/docs/api/")) + +(def + #^{:doc "Ref to a map from package name prefixes to URLs for remote + Javadocs."} + *remote-javadocs* + (ref (sorted-map + "java." *core-java-api* + "javax." *core-java-api* + "org.ietf.jgss." *core-java-api* + "org.omg." *core-java-api* + "org.w3c.dom." *core-java-api* + "org.xml.sax." *core-java-api* + "org.apache.commons.codec." "http://commons.apache.org/codec/api-release/" + "org.apache.commons.io." "http://commons.apache.org/io/api-release/" + "org.apache.commons.lang." "http://commons.apache.org/lang/api-release/"))) + +(defn add-local-javadoc + "Adds to the list of local Javadoc paths." + [path] + (dosync (commute *local-javadocs* conj path))) + +(defn add-remote-javadoc + "Adds to the list of remote Javadoc URLs. package-prefix is the + beginning of the package name that has docs at this URL." + [package-prefix url] + (dosync (commute *remote-javadocs* assoc package-prefix url))) + +(defn- find-javadoc-url + "Searches for a URL for the given class name. Tries + *local-javadocs* first, then *remote-javadocs*. Returns a string." + {:tag String} + [#^String classname] + (let [file-path (.replace classname \. File/separatorChar) + url-path (.replace classname \. \/)] + (if-let [file #^File (first + (filter #(.exists #^File %) + (map #(File. (str %) (str file-path ".html")) + @*local-javadocs*)))] + (-> file .toURI str) + ;; If no local file, try remote URLs: + (or (some (fn [[prefix url]] + (when (.startsWith classname prefix) + (str url url-path ".html"))) + @*remote-javadocs*) + ;; if *feeling-lucky* try a web search + (when *feeling-lucky* (str *feeling-lucky-url* url-path ".html")))))) + +(defn javadoc + "Opens a browser window displaying the javadoc for the argument. + Tries *local-javadocs* first, then *remote-javadocs*." + [class-or-object] + (let [#^Class c (if (instance? Class class-or-object) + class-or-object + (class class-or-object))] + (if-let [url (find-javadoc-url (.getName c))] + (browse-url url) + (println "Could not find Javadoc for" c)))) diff --git a/src/clj/clojure/shell.clj b/src/clj/clojure/java/shell.clj index b8d04d74..ae96797f 100644 --- a/src/clj/clojure/shell.clj +++ b/src/clj/clojure/java/shell.clj @@ -10,7 +10,7 @@ #^{:author "Chris Houser, Stuart Halloway", :doc "Conveniently launch a sub-process providing its stdin and collecting its stdout"} - clojure.shell + clojure.java.shell (:use [clojure.java.io :only (as-file)]) (:import (java.io InputStreamReader OutputStreamWriter))) @@ -68,7 +68,6 @@ collecting its stdout"} (map? arg) (into-array String (map (fn [[k v]] (str (as-env-key k) "=" v)) arg)) true arg)) - (defn sh "Passes the given strings to Runtime.exec() to launch a sub-process. |