1 -- Example hooks.lua file, put in ~/.links/ as hooks.lua.
5 ----------------------------------------------------------------------
7 ----------------------------------------------------------------------
10 -- For security reasons, systems functions are not enabled by default.
11 -- To do so, uncomment the following line, but be careful about
12 -- including unknown code. Individual functions may be disabled by
13 -- assigning them to `nil'.
15 enable_systems_functions ()
17 -- openfile = nil -- may open files in write mode
18 -- readfrom = nil -- reading from pipe can execute commands
27 -- Home directory: If you do not enable system functions, you will
28 -- need to set the following to your home directory.
30 home_dir = (getenv and getenv ("HOME")) or "/home/MYSELF"
31 hooks_file = home_dir.."/.links/hooks.lua"
33 -- Pausing: When external programs are run, sometimes we need to pause
34 -- to see the output. This is the string we append to the command
35 -- line to do that. You may customise it if you wish.
37 pause = '; echo -ne "\\n\\e[1;32mPress ENTER to continue...\\e[0m"; read'
39 -- When starting Netscape: Set to `nil' if you do not want to open a
40 -- new window for each document.
42 netscape_new_window = 1
44 -- Make ALT="" into ALT=" ": Makes web pages with superfluous
45 -- images look better. However, even if you disable the "Display links
46 -- to images" option, single space links to such images will appear.
47 -- To enable, set the following to 1. If necessary, you can change
48 -- this while in Links using the Lua Console, then reload the page.
49 -- See also the keybinding section at the end of the file.
51 mangle_blank_alt = nil
53 -- WWWoffle is a proxy/cache system designed for dial-up users.
54 -- If you have it, set this to the machine:port where it is installed
55 -- (e.g. "http://localhost:8080")
59 -- If you set this to non-`nil', the bookmark addon will be loaded,
60 -- and actions will be bound to my key bindings. Change them at the
61 -- bottom of the file.
66 ----------------------------------------------------------------------
67 -- case-insensitive gsub
68 ----------------------------------------------------------------------
70 -- Please note that this is not completely correct yet.
71 -- It will not handle pattern classes like %a properly.
72 -- XXX: Fix this to handle pattern classes.
74 function gisub (s, pat, repl, n)
75 pat = gsub (pat, '(%a)',
76 function (v) return '['..strupper(v)..strlower(v)..']' end)
78 return gsub (s, pat, repl, n)
80 return gsub (s, pat, repl)
85 ----------------------------------------------------------------------
87 ----------------------------------------------------------------------
89 function match (prefix, url)
90 return strsub (url, 1, strlen (prefix)) == prefix
94 return gsub (str, "^%s*(.-)%s*$", "%1")
97 function plusify (str)
98 return gsub (str, "%s", "+")
101 function goto_url_hook (url, current_url)
103 -- XXX: Use a table instead of if ... else ... else ...
105 -- Google search (e.g. ,gg unix browsers).
106 if match (",gg", url) then
107 url = plusify (strip (strsub (url, 4)))
108 return "http://www.google.com/search?q="..url.."&btnG=Google+Search"
111 elseif match (",fm", url) then
112 url = plusify (strip (strsub (url, 4)))
113 return "http://www.freshmeat.net/search/?q="..url
115 -- Appwatch search (e.g. ,aw lynx).
116 elseif match (",aw", url) then
117 url = plusify (strip (strsub (url, 4)))
118 return "http://www.appwatch.com/Linux/Users/find?q="..url
120 -- Dictionary.com search (e.g. ,dict congenial).
121 elseif match (",dict", url) then
122 url = plusify (strip (strsub (url, 6)))
123 return "http://www.dictionary.com/cgi-bin/dict.pl?db=%2A&term="..url
125 -- RPM search (e.g. ,rpm links).
126 elseif match (",rpm", url) then
127 url = plusify (strip (strsub (url, 5)))
128 return "http://www.rpmfind.net/linux/rpm2html/search.php?query="
129 ..url.."&submit=Search+..."
131 -- Netcraft.com search (e.g. ,whatis www.google.com).
132 elseif match (",whatis", url) then
133 url = plusify (strip (strsub (url, 8)))
134 return "http://uptime.netcraft.com/up/graph/?host="..url
136 -- LinuxToday home page.
137 elseif match (",lt", url) then
138 return "http://linuxtoday.com/"
140 -- User Friendly daily static.
141 elseif match (",uf", url) then
142 return "http://www.userfriendly.org/static/"
144 -- Weather forecast for Melbourne, Australia.
145 elseif match (",forecast", url) then
146 return "http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDF02V00.txt"
148 -- Local network web server.
149 elseif match (",local", url) then
150 return "http://desky/"
153 elseif match (",cache", url) and wwwoffle then
154 return wwwoffle.."/#indexes"
156 -- Expand ~ to home directories.
157 elseif match ("~", url) then
158 if strsub(url, 2, 2) == "/" then -- ~/foo
159 return home_dir..strsub(url, 2)
161 return "/home/"..strsub(url, 2)
171 -----------------------------------------------------------------------
173 ---------------------------------------------------------------------
175 function follow_url_hook (url)
176 -- Using bookmark addon.
177 if bookmark_addon then
178 if bm_is_category (url) then
181 return bm_get_bookmark_url (url) or url
184 -- Not using bookmark addon.
191 ----------------------------------------------------------------------
192 -- pre_format_html_hook
193 ----------------------------------------------------------------------
195 -- Plain strfind (no metacharacters).
196 function sstrfind (s, pattern)
197 return strfind (s, pattern, 1, 1)
200 function pre_format_html_hook (url, html)
203 -- Handle gzip'd files within reasonable size.
204 if strfind (url, "%.gz$") and strlen (html) < 65536 then
205 local tmp = tmpname ()
206 writeto (tmp) write (html) writeto ()
207 html = pipe_read ("(gzip -dc "..tmp.." || cat "..tmp..") 2>/dev/null")
212 -- Mangle ALT="" in IMG tags.
213 if mangle_blank_alt then
215 html, n = gisub (html, '(<img.-) alt=""', '%1 alt=" "')
219 -- These quick 'n dirty patterns don't maintain proper HTML.
222 if sstrfind (url, "linuxtoday.com") then
223 if sstrfind (url, "news_story") then
224 html = gsub (html, '<TABLE CELLSPACING="0".-</TABLE>', '', 1)
225 html = gsub (html, '<TR BGCOLOR="#FFF.-</TR></TABLE>', '', 1)
227 html = gsub (html, 'WIDTH="120">\n<TR.+</TABLE></TD>', '>', 1)
229 html = gsub (html, '<A HREF="http://www.internet.com.-</A>', '')
230 html = gsub (html, "<IFRAME.-</IFRAME>", "")
231 -- emphasis in text is lost
232 return gsub (html, 'text="#002244"', 'text="#001133"', 1)
235 elseif sstrfind (url, "linuxgames.com") then
236 return gsub (html, "<CENTER>.-</center>", "", 1)
239 elseif sstrfind (url, "dictionary.com/cgi-bin/dict.pl") then
241 local _, n = gsub (html, "resultItemStart %-%-%>(.-)%<%!%-%- resultItemEnd",
242 function (x) %t.t = %t.t.."<tr><td>"..x.."</td></tr>" end)
244 -- we've already mangled this page before
247 return "<html><head><title>Dictionary.com lookup</title></head>"..
248 "<body><table border=1 cellpadding=5>"..t.t.."</table>"..
257 ----------------------------------------------------------------------
258 -- Miscellaneous functions, accessed with the Lua Console.
259 ----------------------------------------------------------------------
261 -- Reload this file (hooks.lua) from within Links.
267 function catto (output)
268 local doc = current_document_formatted (79)
269 if doc then writeto (output) write (doc) writeto () end
272 -- Print the current document using `lpr'.
274 -- You must compile Lua with `popen' support for pipes to work.
275 -- See `config' in the Lua distribution.
279 -- Print the current document using `enscript'.
281 catto ("|enscript -fCourier8")
284 -- Ask WWWoffle to monitor the current page for us.
285 -- This only works when called from lua_console_hook, below.
288 return "goto_url", wwwoffle.."/monitor-options/?"..current_url ()
294 -- Email the current document, using Mutt (http://www.mutt.org).
295 -- This only works when called from lua_console_hook, below.
297 local tmp = tmpname ()
298 writeto (tmp) write (current_document ()) writeto ()
299 tinsert (tmp_files, tmp)
300 return "run", "mutt -a "..tmp
303 -- Open current document in Netscape.
305 local new = netscape_new_window and ",new_window" or ""
306 execute ("( netscape -remote 'openURL("..current_url ()..new..")'"
307 .." || netscape '"..current_url ().."' ) 2>/dev/null &")
310 -- If Links is ever the wrong size in your terminal emulator run
311 -- this to set the LINES and COLUMNS shell variables properly.
312 -- This only works when called from lua_console_hook, below.
314 return "run", "eval resize"
317 -- Table of expressions which are recognised by our lua_console_hook.
318 console_hook_functions = {
319 reload = "reload ()",
321 enscript = "enscript ()",
324 netscape = "netscape ()",
325 nuts = "netscape ()",
329 function lua_console_hook (expr)
330 local x = console_hook_functions[expr]
331 if type (x) == "function" then
334 return "eval", x or expr
339 ----------------------------------------------------------------------
341 ----------------------------------------------------------------------
343 -- We need to delete the temporary files that we create.
344 if not tmp_files then
348 function quit_hook ()
349 if bookmark_addon then
353 if tmp_files and remove then
355 for i,v in tmp_files do remove (v) end
360 ----------------------------------------------------------------------
361 -- Examples of keybinding
362 ----------------------------------------------------------------------
364 -- Bind Ctrl-H to a "Home" page.
366 -- bind_key ("main", "Ctrl-H",
367 -- function () return "goto_url", "http://www.google.com/" end)
369 -- Bind Alt-p to print.
371 -- bind_key ("main", "Alt-p", lpr)
373 -- Bind Alt-m to toggle ALT="" mangling.
375 -- bind_key ("main", "Alt-m",
376 -- function () mangle_blank_alt = not mangle_blank_alt end)
379 ----------------------------------------------------------------------
381 ----------------------------------------------------------------------
383 if bookmark_addon then
385 dofile ("/usr/share/elinks/elinks-bm.lua")
387 -- Add/change any bookmark options here.
389 -- Be careful not to load bookmarks if this script is being
390 -- reloaded while in Links, or we will lose unsaved changes.
391 if not bm_bookmarks or getn (bm_bookmarks) == 0 then
395 -- My bookmark key bindings.
396 bind_key ('main', 'a', bm_add_bookmark)
397 bind_key ('main', 's', bm_view_bookmarks)
398 bind_key ('main', 'Alt-e', bm_edit_bookmark)
399 bind_key ('main', 'Alt-d', bm_delete_bookmark)
400 bind_key ('main', 'Alt-k', bm_move_bookmark_up)
401 bind_key ('main', 'Alt-j', bm_move_bookmark_down)
406 -- vim: shiftwidth=4 softtabstop=4