]>
Commit | Line | Data |
---|---|---|
3cd56758 | 1 | -- Example hooks.lua file, put in ~/.links/ as hooks.lua. |
2 | -- $Id$ | |
3 | ||
4 | ||
5 | ---------------------------------------------------------------------- | |
6 | -- Local configuration | |
7 | ---------------------------------------------------------------------- | |
8 | ||
9 | -- ** IMPORTANT ** | |
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'. | |
14 | ||
15 | enable_systems_functions () | |
16 | ||
17 | -- openfile = nil -- may open files in write mode | |
18 | -- readfrom = nil -- reading from pipe can execute commands | |
19 | -- writeto = nil | |
20 | -- appendto = nil | |
21 | -- pipe_read = nil | |
22 | -- remove = nil | |
23 | -- rename = nil | |
24 | -- execute = nil | |
25 | -- exit = nil | |
26 | ||
27 | -- Home directory: If you do not enable system functions, you will | |
28 | -- need to set the following to your home directory. | |
29 | ||
30 | home_dir = (getenv and getenv ("HOME")) or "/home/MYSELF" | |
31 | hooks_file = home_dir.."/.links/hooks.lua" | |
32 | ||
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. | |
36 | ||
37 | pause = '; echo -ne "\\n\\e[1;32mPress ENTER to continue...\\e[0m"; read' | |
38 | ||
39 | -- When starting Netscape: Set to `nil' if you do not want to open a | |
40 | -- new window for each document. | |
41 | ||
42 | netscape_new_window = 1 | |
43 | ||
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. | |
50 | ||
51 | mangle_blank_alt = nil | |
52 | ||
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") | |
56 | ||
57 | wwwoffle = nil | |
58 | ||
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. | |
62 | ||
63 | bookmark_addon = nil | |
64 | ||
65 | ||
66 | ---------------------------------------------------------------------- | |
67 | -- case-insensitive gsub | |
68 | ---------------------------------------------------------------------- | |
69 | ||
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. | |
73 | ||
74 | function gisub (s, pat, repl, n) | |
75 | pat = gsub (pat, '(%a)', | |
76 | function (v) return '['..strupper(v)..strlower(v)..']' end) | |
77 | if n then | |
78 | return gsub (s, pat, repl, n) | |
79 | else | |
80 | return gsub (s, pat, repl) | |
81 | end | |
82 | end | |
83 | ||
84 | ||
85 | ---------------------------------------------------------------------- | |
86 | -- goto_url_hook | |
87 | ---------------------------------------------------------------------- | |
88 | ||
89 | function match (prefix, url) | |
90 | return strsub (url, 1, strlen (prefix)) == prefix | |
91 | end | |
92 | ||
93 | function strip (str) | |
94 | return gsub (str, "^%s*(.-)%s*$", "%1") | |
95 | end | |
96 | ||
97 | function plusify (str) | |
98 | return gsub (str, "%s", "+") | |
99 | end | |
100 | ||
101 | function goto_url_hook (url, current_url) | |
102 | ||
103 | -- XXX: Use a table instead of if ... else ... else ... | |
104 | ||
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" | |
109 | ||
110 | -- Freshmeat search. | |
111 | elseif match (",fm", url) then | |
112 | url = plusify (strip (strsub (url, 4))) | |
113 | return "http://www.freshmeat.net/search/?q="..url | |
114 | ||
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 | |
119 | ||
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 | |
124 | ||
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+..." | |
130 | ||
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 | |
135 | ||
136 | -- LinuxToday home page. | |
137 | elseif match (",lt", url) then | |
138 | return "http://linuxtoday.com/" | |
139 | ||
140 | -- User Friendly daily static. | |
141 | elseif match (",uf", url) then | |
142 | return "http://www.userfriendly.org/static/" | |
143 | ||
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" | |
147 | ||
148 | -- Local network web server. | |
149 | elseif match (",local", url) then | |
150 | return "http://desky/" | |
151 | ||
152 | -- WWWoffle caches. | |
153 | elseif match (",cache", url) and wwwoffle then | |
154 | return wwwoffle.."/#indexes" | |
155 | ||
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) | |
160 | else -- ~foo/bar | |
161 | return "/home/"..strsub(url, 2) | |
162 | end | |
163 | ||
164 | -- Unmatched. | |
165 | else | |
166 | return url | |
167 | end | |
168 | end | |
169 | ||
170 | ||
171 | ----------------------------------------------------------------------- | |
172 | -- follow_url_hook | |
173 | --------------------------------------------------------------------- | |
174 | ||
175 | function follow_url_hook (url) | |
176 | -- Using bookmark addon. | |
177 | if bookmark_addon then | |
178 | if bm_is_category (url) then | |
179 | return nil | |
180 | else | |
181 | return bm_get_bookmark_url (url) or url | |
182 | end | |
183 | ||
184 | -- Not using bookmark addon. | |
185 | else | |
186 | return url | |
187 | end | |
188 | end | |
189 | ||
190 | ||
191 | ---------------------------------------------------------------------- | |
192 | -- pre_format_html_hook | |
193 | ---------------------------------------------------------------------- | |
194 | ||
195 | -- Plain strfind (no metacharacters). | |
196 | function sstrfind (s, pattern) | |
197 | return strfind (s, pattern, 1, 1) | |
198 | end | |
199 | ||
200 | function pre_format_html_hook (url, html) | |
201 | local ret = nil | |
202 | ||
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") | |
208 | remove (tmp) | |
209 | ret = 1 | |
210 | end | |
211 | ||
212 | -- Mangle ALT="" in IMG tags. | |
213 | if mangle_blank_alt then | |
214 | local n | |
215 | html, n = gisub (html, '(<img.-) alt=""', '%1 alt=" "') | |
216 | ret = ret or (n > 0) | |
217 | end | |
218 | ||
219 | -- These quick 'n dirty patterns don't maintain proper HTML. | |
220 | ||
221 | -- linuxtoday.com | |
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) | |
226 | else | |
227 | html = gsub (html, 'WIDTH="120">\n<TR.+</TABLE></TD>', '>', 1) | |
228 | end | |
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) | |
233 | ||
234 | -- linuxgames.com | |
235 | elseif sstrfind (url, "linuxgames.com") then | |
236 | return gsub (html, "<CENTER>.-</center>", "", 1) | |
237 | ||
238 | -- dictionary.com | |
239 | elseif sstrfind (url, "dictionary.com/cgi-bin/dict.pl") then | |
240 | local t = { t = "" } | |
241 | local _, n = gsub (html, "resultItemStart %-%-%>(.-)%<%!%-%- resultItemEnd", | |
242 | function (x) %t.t = %t.t.."<tr><td>"..x.."</td></tr>" end) | |
243 | if n == 0 then | |
244 | -- we've already mangled this page before | |
245 | return html | |
246 | else | |
247 | return "<html><head><title>Dictionary.com lookup</title></head>".. | |
248 | "<body><table border=1 cellpadding=5>"..t.t.."</table>".. | |
249 | "</body></html>" | |
250 | end | |
251 | end | |
252 | ||
253 | return ret and html | |
254 | end | |
255 | ||
256 | ||
257 | ---------------------------------------------------------------------- | |
258 | -- Miscellaneous functions, accessed with the Lua Console. | |
259 | ---------------------------------------------------------------------- | |
260 | ||
261 | -- Reload this file (hooks.lua) from within Links. | |
262 | function reload () | |
263 | dofile (hooks_file) | |
264 | end | |
265 | ||
266 | -- Helper function. | |
267 | function catto (output) | |
268 | local doc = current_document_formatted (79) | |
269 | if doc then writeto (output) write (doc) writeto () end | |
270 | end | |
271 | ||
272 | -- Print the current document using `lpr'. | |
273 | function lpr () | |
274 | -- You must compile Lua with `popen' support for pipes to work. | |
275 | -- See `config' in the Lua distribution. | |
276 | catto ("|lpr") | |
277 | end | |
278 | ||
279 | -- Print the current document using `enscript'. | |
280 | function enscript () | |
281 | catto ("|enscript -fCourier8") | |
282 | end | |
283 | ||
284 | -- Ask WWWoffle to monitor the current page for us. | |
285 | -- This only works when called from lua_console_hook, below. | |
286 | function monitor () | |
287 | if wwwoffle then | |
288 | return "goto_url", wwwoffle.."/monitor-options/?"..current_url () | |
289 | else | |
290 | return nil | |
291 | end | |
292 | end | |
293 | ||
294 | -- Email the current document, using Mutt (http://www.mutt.org). | |
295 | -- This only works when called from lua_console_hook, below. | |
296 | function mutt () | |
297 | local tmp = tmpname () | |
298 | writeto (tmp) write (current_document ()) writeto () | |
299 | tinsert (tmp_files, tmp) | |
300 | return "run", "mutt -a "..tmp | |
301 | end | |
302 | ||
303 | -- Open current document in Netscape. | |
304 | function 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 &") | |
308 | end | |
309 | ||
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. | |
313 | function resize () | |
314 | return "run", "eval resize" | |
315 | end | |
316 | ||
317 | -- Table of expressions which are recognised by our lua_console_hook. | |
318 | console_hook_functions = { | |
319 | reload = "reload ()", | |
320 | lpr = "lpr ()", | |
321 | enscript = "enscript ()", | |
322 | monitor = monitor, | |
323 | mutt = mutt, | |
324 | netscape = "netscape ()", | |
325 | nuts = "netscape ()", | |
326 | resize = resize | |
327 | } | |
328 | ||
329 | function lua_console_hook (expr) | |
330 | local x = console_hook_functions[expr] | |
331 | if type (x) == "function" then | |
332 | return x () | |
333 | else | |
334 | return "eval", x or expr | |
335 | end | |
336 | end | |
337 | ||
338 | ||
339 | ---------------------------------------------------------------------- | |
340 | -- quit_hook | |
341 | ---------------------------------------------------------------------- | |
342 | ||
343 | -- We need to delete the temporary files that we create. | |
344 | if not tmp_files then | |
345 | tmp_files = {} | |
346 | end | |
347 | ||
348 | function quit_hook () | |
349 | if bookmark_addon then | |
350 | bm_save_bookmarks () | |
351 | end | |
352 | ||
353 | if tmp_files and remove then | |
354 | tmp_files.n = nil | |
355 | for i,v in tmp_files do remove (v) end | |
356 | end | |
357 | end | |
358 | ||
359 | ||
360 | ---------------------------------------------------------------------- | |
361 | -- Examples of keybinding | |
362 | ---------------------------------------------------------------------- | |
363 | ||
364 | -- Bind Ctrl-H to a "Home" page. | |
365 | ||
366 | -- bind_key ("main", "Ctrl-H", | |
367 | -- function () return "goto_url", "http://www.google.com/" end) | |
368 | ||
369 | -- Bind Alt-p to print. | |
370 | ||
371 | -- bind_key ("main", "Alt-p", lpr) | |
372 | ||
373 | -- Bind Alt-m to toggle ALT="" mangling. | |
374 | ||
375 | -- bind_key ("main", "Alt-m", | |
376 | -- function () mangle_blank_alt = not mangle_blank_alt end) | |
377 | ||
378 | ||
379 | ---------------------------------------------------------------------- | |
380 | -- Bookmark addon | |
381 | ---------------------------------------------------------------------- | |
382 | ||
383 | if bookmark_addon then | |
384 | ||
385 | dofile ("/usr/share/elinks/elinks-bm.lua") | |
386 | ||
387 | -- Add/change any bookmark options here. | |
388 | ||
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 | |
392 | bm_load_bookmarks () | |
393 | end | |
394 | ||
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) | |
402 | ||
403 | end | |
404 | ||
405 | ||
406 | -- vim: shiftwidth=4 softtabstop=4 |