Skip to content
Browse files

Issue #47 attempted fix. Use updated browse-url implementation

  • Loading branch information...
1 parent 5b56eee commit 8f6f319dcb825a5c2c1a98412b1f95f81be8331d @jafingerhut jafingerhut committed
Showing with 100 additions and 10 deletions.
  1. +10 −10 src/clj_ns_browser/browser.clj
  2. +90 −0 src/clj_ns_browser/web.clj
View
20 src/clj_ns_browser/browser.clj
@@ -11,7 +11,7 @@
[seesaw.dnd]
[seesaw.bind :as b]
[seesaw.rsyntax]
- [clojure.java.browse]
+ [clj-ns-browser.web]
[clojure.java.shell]
[clojure.java.io :as io]
[clojure.pprint]
@@ -373,24 +373,24 @@
(add-app-action :go-github-wiki-action
(action :name "Clj-NS-Browser Wiki..."
- :handler (fn [a] (future (clojure.java.browse/browse-url
+ :handler (fn [a] (future (clj-ns-browser.web/browse-url
"https://github.com/franks42/clj-ns-browser/wiki")))))
(add-app-action :go-github-action
(action :name "Clj-NS-Browser GitHub..."
- :handler (fn [a] (future (clojure.java.browse/browse-url
+ :handler (fn [a] (future (clj-ns-browser.web/browse-url
"https://github.com/franks42/clj-ns-browser")))))
(add-app-action :go-clojure.org-action
(action :name "Clojure.org..."
- :handler (fn [a] (future (clojure.java.browse/browse-url "http://clojure.org")))))
+ :handler (fn [a] (future (clj-ns-browser.web/browse-url "http://clojure.org")))))
(add-app-action :go-clojuredocs-action
(action :name "ClojureDocs..."
- :handler (fn [a] (future (clojure.java.browse/browse-url "http://clojuredocs.org")))))
+ :handler (fn [a] (future (clj-ns-browser.web/browse-url "http://clojuredocs.org")))))
(add-app-action :go-cheatsheet-action
(action :name "Clojure CheatSheet..."
- :handler (fn [a] (future (clojure.java.browse/browse-url "http://homepage.mac.com/jafingerhut/files/cheatsheet-clj-1.3.0-v1.4-tooltips/cheatsheet-full.html")))))
+ :handler (fn [a] (future (clj-ns-browser.web/browse-url "http://homepage.mac.com/jafingerhut/files/cheatsheet-clj-1.3.0-v1.4-tooltips/cheatsheet-full.html")))))
(add-app-action :go-jira-action
(action :name "JIRA..."
- :handler (fn [a] (future (clojure.java.browse/browse-url
+ :handler (fn [a] (future (clj-ns-browser.web/browse-url
"http://dev.clojure.org/jira/browse/CLJ")))))
(add-app-action :go-stackoverflow-action
(action :name "Stackoverflow..."
@@ -400,7 +400,7 @@
(if-let [fqn-name (fqname fqn)]
(let [ns-n-class (ns-name-class-str fqn-name)
sname (if (nil? (second ns-n-class)) (first ns-n-class)(second ns-n-class))]
- (future (clojure.java.browse/browse-url
+ (future (clj-ns-browser.web/browse-url
(str "http://stackoverflow.com/search?q=clojure+" sname))))))))))
(add-app-action :go-about-action
(action :name "About..."
@@ -466,7 +466,7 @@
(let [id (partial select-id (to-root e))]
(when-let [s (selection (id :doc-ta))]
(let [url (subs (config (id :doc-ta) :text) (first s) (second s))]
- (future (doall (clojure.java.browse/browse-url url)))))))))
+ (future (doall (clj-ns-browser.web/browse-url url)))))))))
(add-app-action :manual-refresh-browser-action
(action :name "Manual Refresh"
@@ -1004,7 +1004,7 @@
(case o
("Examples" "See alsos" "Comments")
(when-let [url (clojuredocs-url fqn)]
- (clojure.java.browse/browse-url url))
+ (clj-ns-browser.web/browse-url url))
(bdoc* fqn))))))))
;;
View
90 src/clj_ns_browser/web.clj
@@ -0,0 +1,90 @@
+;; This file was created by copying src/clj/clojure/java/browse.clj
+;; from the Clojure source code, then applying the patch file
+;; clj-896-browse-url-uses-xdg-open-patch2.txt to it (available at the
+;; URL below), and then changing the namespace name.
+
+;; http://dev.clojure.org/jira/browse/CLJ-896
+
+;; The intent is that if and when Clojure's
+;; clojure.java.browser/browse-url has been updated appropriately,
+;; this file and namespace can be removed completely.
+
+;; Until then, there are platforms, like Lubuntu, where the built-in
+;; browse-url uses a very poor HTML renderer.
+
+; 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"}
+ clj-ns-browser.web
+ (:require [clojure.java.shell :as sh]
+ [clojure.string :as str])
+ (:import (java.net URI)))
+
+(defn- macosx? []
+ (-> "os.name" System/getProperty .toLowerCase
+ (.startsWith "mac os x")))
+
+(defn- xdg-open-loc []
+ ;; try/catch needed to mask exception on Windows without Cygwin
+ (let [which-out (try (:out (sh/sh "which" "xdg-open"))
+ (catch Exception e ""))]
+ (if (= which-out "")
+ nil
+ (str/trim-newline which-out))))
+
+(defn- open-url-script-val []
+ (if (macosx?)
+ "/usr/bin/open"
+ (xdg-open-loc)))
+
+;; We could assign (open-url-script-val) to *open-url-script* right
+;; away in the def below, but clojure.java.shell/sh creates a future
+;; that causes a long wait for the JVM to exit during Clojure compiles
+;; (unless we can somehow here make it call (shutdown-agents) later).
+;; Better to initialize it when we first need it, in browse-url.
+
+(def ^:dynamic *open-url-script* (atom :uninitialized))
+
+(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.java.browse-ui)
+ ((find-var 'clojure.java.browse-ui/open-url-in-swing) url))
+
+(defn browse-url
+ "Open url in a browser"
+ {:added "1.2"}
+ [url]
+ (let [script @*open-url-script*
+ script (if (= :uninitialized script)
+ (reset! *open-url-script* (open-url-script-val))
+ script)]
+ (or (when script (sh/sh script (str url)) true)
+ (open-url-in-browser url)
+ (open-url-in-swing url))))

0 comments on commit 8f6f319

Please sign in to comment.
Something went wrong with that request. Please try again.