Skip to content

Commit ef00c7c

Browse files
jafingerhutstuarthalloway
authored andcommitted
On non-Mac OS system, use xdg-open in browse-url if available
This allows the usage of freedesktop.org's xdg-open which is present on basically any Linux and BSD system nowadays. Another slight improvement is that on Mac OS X, /usr/bin/open is tried first, so no icon pops up in the dock due to the JVM switching to becoming a GUI app (unless it was a GUI app before browse-url was called). This patch has been tested on the following OSes, all with 1.6.0_<something> JVMs: Mac OS X 10.6.8 Windows XP SP3, both from CMD.EXE and in a Cygwin bash shell Ubuntu Linux 10.04 LTS The test consisted of the following commands typed into a REPL, run inside the Clojure root directory where readme.txt and epl-v10.html files are: (use 'clojure.java.browse) (browse-url "http://www.yahoo.com") (browse-url "readme.txt") (browse-url "epl-v10.html") Signed-off-by: Stuart Halloway <[email protected]>
1 parent 9b1db94 commit ef00c7c

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/clj/clojure/java/browse.clj

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,34 @@
1010
^{:author "Christophe Grand",
1111
:doc "Start a web browser from Clojure"}
1212
clojure.java.browse
13-
(:require [clojure.java.shell :as sh])
13+
(:require [clojure.java.shell :as sh]
14+
[clojure.string :as str])
1415
(:import (java.net URI)))
1516

1617
(defn- macosx? []
1718
(-> "os.name" System/getProperty .toLowerCase
1819
(.startsWith "mac os x")))
1920

20-
(def ^:dynamic *open-url-script* (when (macosx?) "/usr/bin/open"))
21+
(defn- xdg-open-loc []
22+
;; try/catch needed to mask exception on Windows without Cygwin
23+
(let [which-out (try (:out (sh/sh "which" "xdg-open"))
24+
(catch Exception e ""))]
25+
(if (= which-out "")
26+
nil
27+
(str/trim-newline which-out))))
28+
29+
(defn- open-url-script-val []
30+
(if (macosx?)
31+
"/usr/bin/open"
32+
(xdg-open-loc)))
33+
34+
;; We could assign (open-url-script-val) to *open-url-script* right
35+
;; away in the def below, but clojure.java.shell/sh creates a future
36+
;; that causes a long wait for the JVM to exit during Clojure compiles
37+
;; (unless we can somehow here make it call (shutdown-agents) later).
38+
;; Better to initialize it when we first need it, in browse-url.
39+
40+
(def ^:dynamic *open-url-script* (atom :uninitialized))
2141

2242
(defn- open-url-in-browser
2343
"Opens url (a string) in the default system web browser. May not
@@ -47,6 +67,10 @@
4767
"Open url in a browser"
4868
{:added "1.2"}
4969
[url]
50-
(or (open-url-in-browser url)
51-
(when *open-url-script* (sh/sh *open-url-script* (str url)) true)
52-
(open-url-in-swing url)))
70+
(let [script @*open-url-script*
71+
script (if (= :uninitialized script)
72+
(reset! *open-url-script* (open-url-script-val))
73+
script)]
74+
(or (when script (sh/sh script (str url)) true)
75+
(open-url-in-browser url)
76+
(open-url-in-swing url))))

0 commit comments

Comments
 (0)