PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/javadoc/src/main/clojure/clojure/contrib/javadoc/browse.clj

https://github.com/cpulsford/clojure-contrib
Clojure | 51 lines | 32 code | 7 blank | 12 comment | 4 complexity | 15711eb52850ca6443e5df6da17c6112 MD5 | raw file
  1. ;;; browse.clj -- start a web browser from Clojure
  2. ; Copyright (c) Christophe Grand, December 2008. All rights reserved.
  3. ; The use and distribution terms for this software are covered by the
  4. ; 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 this
  6. ; distribution.
  7. ; By using this software in any fashion, you are agreeing to be bound by
  8. ; the terms of this license.
  9. ; You must not remove this notice, or any other, from this software.
  10. (ns
  11. ^{:author "Christophe Grand",
  12. :deprecated "1.2"
  13. :doc "Start a web browser from Clojure"}
  14. clojure.contrib.javadoc.browse
  15. (:require [clojure.contrib.shell :as sh])
  16. (:import (java.net URI)))
  17. (defn- macosx? []
  18. (-> "os.name" System/getProperty .toLowerCase
  19. (.startsWith "mac os x")))
  20. (def *open-url-script* (when (macosx?) "/usr/bin/open"))
  21. (defn open-url-in-browser
  22. "Opens url (a string) in the default system web browser. May not
  23. work on all platforms. Returns url on success, nil if not
  24. supported."
  25. [url]
  26. (try
  27. (when (clojure.lang.Reflector/invokeStaticMethod "java.awt.Desktop"
  28. "isDesktopSupported" (to-array nil))
  29. (-> (clojure.lang.Reflector/invokeStaticMethod "java.awt.Desktop"
  30. "getDesktop" (to-array nil))
  31. (.browse (URI. url)))
  32. url)
  33. (catch ClassNotFoundException e
  34. nil)))
  35. (defn open-url-in-swing
  36. "Opens url (a string) in a Swing window."
  37. [url]
  38. ; the implementation of this function resides in another namespace to be loaded "on demand"
  39. ; this fixes a bug on mac os x where requiring repl-utils turns the process into a GUI app
  40. ; see http://code.google.com/p/clojure-contrib/issues/detail?id=32
  41. (require 'clojure.contrib.javadoc.browse-ui)
  42. ((find-var 'clojure.contrib.javadoc.browse-ui/open-url-in-swing) url))
  43. (defn browse-url [url]
  44. (or (open-url-in-browser url) (when *open-url-script* (sh/sh *open-url-script* (str url)) true) (open-url-in-swing url)))