|
10 | 10 | ^{:author "Christophe Grand", |
11 | 11 | :doc "Start a web browser from Clojure"} |
12 | 12 | clojure.java.browse |
13 | | - (:require [clojure.java.shell :as sh]) |
| 13 | + (:require [clojure.java.shell :as sh] |
| 14 | + [clojure.string :as str]) |
14 | 15 | (:import (java.net URI))) |
15 | 16 |
|
16 | 17 | (defn- macosx? [] |
17 | 18 | (-> "os.name" System/getProperty .toLowerCase |
18 | 19 | (.startsWith "mac os x"))) |
19 | 20 |
|
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)) |
21 | 41 |
|
22 | 42 | (defn- open-url-in-browser |
23 | 43 | "Opens url (a string) in the default system web browser. May not |
|
47 | 67 | "Open url in a browser" |
48 | 68 | {:added "1.2"} |
49 | 69 | [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