Bug correcton in links module.
[berthome:berthome.git] / links.lua
1 local awful = require ("awful")
2 local naughty = require ("naughty")
3 local string = string
4 local selection = selection
5
6 module ("links")
7
8 config = {}
9 config.prompt = nil
10 config.open_in_term = nil
11
12 -- Many of what's used here should have been loaded already!
13 local spawn = awful.util.spawn
14 local gsub = string.gsub
15 local match = string.match
16
17 local xbrowser="xdg-open"
18 local term_browser="w3m"
19
20 -- {{{ Helper functions
21
22 local function url_encode (str)
23    if str then
24       local function repchar (c)
25          return string.format ("%%%02X", string.byte (c))
26       end
27       str = gsub (str, "\n", "\r\n")
28       str = gsub (str, "([^%w ])", repchar)
29       str = gsub (str, " ", "+")
30    end
31    return str
32 end
33
34 local function prmpt (prompt_text, cache, fun)
35    return
36    function (from_selection)
37       -- Let's check it now, for now:
38       if not config.prompt or not config.open_in_term then 
39          naughty.notify ({ text = "Links module is not properly configured!" })
40          return nil
41       end
42
43       if from_selection then
44          local str = selection ()
45          if str == "" then
46             naughty.notify ({ text = "Need a selection!" })
47          else
48             fun (str)
49          end
50       else
51          config.prompt (prompt_text, fun, nil, cache)
52       end
53    end
54 end
55
56 -- }}}
57
58 -- {{{ Hack for some publishing site at verimag...
59
60 -- Basic check: we have an adress:
61 local function is_an_uri (str) return match (str, "^[^/]*://[^/]+.*$") end
62
63 local function open_gate6_link (uri)
64    local function append_host (hostname, to_append)
65       return gsub (hostname, "([^/]*://[^/]+)(.*)", "%1"..to_append.."%2")
66    end
67
68    if not is_an_uri (uri) then
69       naughty.notify ({ text = "Need a valid URI!" })
70       return false
71    end
72
73    local cmd = xbrowser.." \""..append_host (uri, ".gate6.inist.fr").."\""
74    naughty.notify ({ text = cmd })
75    spawn (cmd)
76 end
77
78 query_open_gate6_link =
79    prmpt ("Address: ", nil, open_gate6_link)
80
81 -- }}}
82
83 --{{{ Translations and language tools...
84
85 function find_translation (langs, from_selection)
86    prmpt ("Word: ",
87           awful.util.getdir ("cache").."/history_transl_"..langs,
88           function (string)
89              config.open_in_term (term_browser.." \"http://www.wordreference.com/"..
90                                   langs.."/"..string.."\"")
91           end) (from_selection)
92 end
93
94 find_fr_conj = 
95    prmpt ("Verb: ", 
96           awful.util.getdir ("cache").."/history_conj_fr",
97           function (string)
98              config.open_in_term (term_browser..
99                                   " \"http://leconjugueur.com/php5/index.php?v="..
100                                      url_encode (string).."\"")
101           end)
102
103 function find_synonym (lang, from_selection)
104    local req
105    if lang == "fr" then
106       req = "fr/search?r="
107    else
108       req = "en/search?b=1&r=" 
109    end
110    -- XXX: Note I have a huge problem with accents in prompts...
111    prmpt ("Word: ", 
112           awful.util.getdir ("cache").."/history_synonym_"..lang,
113           function (string)
114              config.open_in_term (term_browser..
115                                   " -I iso-8859-1 \"http://dico.isc.cnrs.fr/dico/"..req..
116                                      url_encode (string).. "\"")
117           end) (from_selection)
118 end
119
120 --}}}