5ee4afcbf1273701c96f30c9dc64245cdcb436fc
[pkgsrc.git] / wip / erlang / patches / patch-ai
1 $NetBSD: patch-ai,v 1.2 2009/12/15 12:07:57 asau Exp $
2
3 --- lib/gs/src/tool_utils.erl.orig      2009-03-12 14:23:23.000000000 +0200
4 +++ lib/gs/src/tool_utils.erl   2010-02-24 14:39:44.000000000 +0200
5 @@ -40,6 +40,9 @@
6                }).
7  
8  
9 +%% Browser executable list (openURL command line protocol required)
10 +-define(BROWSERS, ["netscape", "mozilla", "MozillaFirebird", "opera"]).
11 +
12  %%----------------------------------------------------------------------
13  %% open_help(Parent, File)
14  %%   Parent = gsobj()  (GS root object or parent window)
15 @@ -80,7 +83,7 @@
16                       {unix,Type} ->
17                            case Type of
18                                 darwin -> "open " ++ File;
19 -                               _Else -> "netscape -remote \"openURL(file:" ++ File ++ ")\""
20 +                               _Else -> unix_url_command("file:" ++ File)
21                           end;
22                       {win32,_AnyType} ->
23                           "start " ++ filename:nativename(File);
24 @@ -95,7 +98,7 @@
25                       {unix,Type} ->
26                            case Type of
27                                 darwin -> "open " ++ File;
28 -                               _Else -> "netscape -remote \"openURL(file:" ++ File ++ ")\""
29 +                               _Else -> unix_url_command("file:" ++ File)
30                           end;
31                       {win32,_AnyType} ->
32                           "netscape.exe -h " ++ regexp:gsub(File,"\\\\","/");
33 @@ -432,3 +435,53 @@
34      [Last];
35  insert_newlines(Other) ->
36      Other.
37 +
38 +%% find_browser(BrowserList) => string() | false
39 +%%   BrowserList - [string()]
40 +%% Given a list of basenames, find the first available executable.
41 +
42 +find_browser([]) ->
43 +    false;
44 +
45 +find_browser([H | T]) ->
46 +    case os:find_executable(H) of
47 +        false ->
48 +          find_browser(T);
49 +        Browser ->
50 +          Browser
51 +    end.
52 +
53 +%% unix_url_command(URL) => string()
54 +%%   URL - string()
55 +%% Open an URL, using a browser which supports the openURL command
56 +%% line protocol. If no browser is found, the empty string will be
57 +%% returned.
58 +
59 +unix_url_command(URL) ->
60 +    Template = "BROWSER -remote \"openURL(" ++ URL ++ ")\" || BROWSER " ++ URL ++ "&",
61 +
62 +    case os:getenv("BROWSER") of
63 +       false ->
64 +           %% look for a compatible browser
65 +           case find_browser(?BROWSERS) of
66 +               false ->
67 +                   "";
68 +               Browser ->
69 +                   case regexp:gsub(Template, "BROWSER", Browser) of
70 +                       {ok, Command, 0} ->
71 +                           %% Template does not contain "BROWSER" placeholder
72 +                           "";
73 +                       {ok, Command, _} ->
74 +                           Command
75 +                   end
76 +           end;
77 +
78 +       Value ->
79 +           case regexp:gsub(Template, "BROWSER", Value) of
80 +               {ok, Command2, 0} ->
81 +                   %% no placeholder
82 +                   "";
83 +               {ok, Command2, _} ->
84 +                   Command2
85 +           end
86 +    end.