diff --git a/doc/lua-scripting.txt b/doc/lua-scripting.txt index fc71b12b..5cda553d 100644 --- a/doc/lua-scripting.txt +++ b/doc/lua-scripting.txt @@ -146,7 +146,7 @@ goto_url_hook (url, current_url):: that ELinks should follow, or `nil` to cancel the operation. follow_url_hook (url):: - This hook is passed the URL that Links is about to follow. It should + This hook is passed the URL that ELinks is about to follow. It should return a string (the URL modified or unmodified), or `nil` to stop ELinks following the URL @@ -173,7 +173,7 @@ lua_console_hook (string):: `someprogram`. - `return "eval", "somefunction(1+2)"` will attempt to call the Lua function `somefunction` with an argument, 3. - - `return "goto_url", "http://www.bogus.com"` will ask Links to visit + - `return "goto_url", "http://www.bogus.com"` will ask ELinks to visit the URL "http://www.bogus.com". - `return nil` will do nothing. @@ -188,7 +188,7 @@ Functions As well as providing hooks, ELinks provides some functions in addition to the standard Lua functions. -Note: The standard Lua function `os.setlocale` affects ELinks' idea of +NOTE: The standard Lua function `os.setlocale` affects ELinks' idea of the system locale, which ELinks uses for the "System" charset, for the "System" language, and for formatting dates. This may however have to be changed in a future version of ELinks, in order to properly support @@ -219,21 +219,28 @@ current_document_formatted ([width]):: pipe_read (command):: Executes `command` and reads in all the data from stdout, until there is no more. This is a hack, because for some reason the standard Lua - function `read` seems to crash ELinks when used in pipe-reading mode. + function `file:read` seems to crash ELinks when used in pipe-reading + mode. execute (string):: Executes shell commands `string` without waiting for it to exit. Beware that you must not read or write to stdin and stdout. And unlike the - standard Lua function of the same name, the return value is - meaningless. + standard Lua function `os.execute`, the return value is meaningless. tmpname ():: Returns a unique name for a temporary file, or `nil` if no such name is available. The returned string includes the - directory name. This function does not create the file and - does not guarantee exclusive access to it: the caller must - handle the possibility that another process creates the file - and begins using it while this function is returning. + directory name. Unlike the standard Lua function + `os.tmpname`, this one generates ELinks-related names + (currently with "elinks" at the beginning of the name). ++ +WARNING: The `tmpname` function does not create the file and does not + guarantee exclusive access to it: the caller must handle the + possibility that another process creates the file and begins + using it while this function is returning. Failing to do this + may expose you to symlink attacks by other users. To avoid + the risk, use `io.tmpfile` instead; unfortunately, it does not + tell you the name of the file. bind_key (keymap, keystroke, function):: Currently, `keymap` must be the string `"main"`. Keystroke is a @@ -329,49 +336,48 @@ dialog box, and it will convert them to the URL I actually want to visit. As a bonus, it allows me perform some searches on sites like Google without loading up the front page first. +TIP: The ``URI rewriting'' feature of ELinks handles many of the same +tasks as the Lua hook shown here, and you can conveniently configure +it via the option manager. It is not quite as versatile, though. + ------------------------------------------------------------------------------- function match (prefix, url) - return strsub (url, 1, strlen (prefix)) == prefix + return string.sub (url, 1, string.len (prefix)) == prefix end function strip (str) - return gsub (str, "^%s*(.-)%s*$", "%1") + return string.gsub (str, "^%s*(.-)%s*$", "%1") end function plusify (str) - return gsub (str, "%s", "+") + return string.gsub (str, "%s", "+") end function goto_url_hook (url, current_url) -- Google search (e.g. ,gg unix browsers). if match (",gg", url) then - url = plusify (strip (strsub (url, 4))) + url = plusify (strip (string.sub (url, 4))) return "http://www.google.com/search?q="..url.."&btnG=Google+Search" -- Freshmeat search. elseif match (",fm", url) then - url = plusify (strip (strsub (url, 4))) + url = plusify (strip (string.sub (url, 4))) return "http://www.freshmeat.net/search/?q="..url - -- Appwatch search (e.g. ,aw lynx). - elseif match (",aw", url) then - url = plusify (strip (strsub (url, 4))) - return "http://www.appwatch.com/Linux/Users/find?q="..url - -- Dictionary.com search (e.g. ,dict congenial). elseif match (",dict", url) then - url = plusify (strip (strsub (url, 6))) + url = plusify (strip (string.sub (url, 6))) return "http://www.dictionary.com/cgi-bin/dict.pl?db=%2A&term="..url -- RPM search (e.g. ,rpm links). elseif match (",rpm", url) then - url = plusify (strip (strsub (url, 5))) + url = plusify (strip (string.sub (url, 5))) return "http://www.rpmfind.net/linux/rpm2html/search.php?query=" ..url.."&submit=Search+..." -- Netcraft.com search (e.g. ,whatis www.google.com). elseif match (",whatis", url) then - url = plusify (strip (strsub (url, 8))) + url = plusify (strip (string.sub (url, 8))) return "http://uptime.netcraft.com/up/graph/?host="..url -- LinuxToday home page. @@ -380,7 +386,7 @@ function goto_url_hook (url, current_url) -- Weather forecast for Melbourne, Australia. elseif match (",forecast", url) then - return "http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDF02V00.txt" + return "http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV10450.txt" -- Unmatched else @@ -389,9 +395,6 @@ function goto_url_hook (url, current_url) end ------------------------------------------------------------------------------- -(Note that this was noticably enhanced and rewritten in the ELinks standart -hooks.) - Expanding ~ (tilde) ^^^^^^^^^^^^^^^^^^^ @@ -402,21 +405,16 @@ and `~user/zappo`, like in the shell and other Unix programs. ------------------------------------------------------------------------------- --- Home directory: If you do not enable system functions, you will need --- to set the following to your home directory. - -home_dir = (getenv and getenv ("HOME")) or "/home/MYSELF" - function goto_url_hook (url, current_url) . . -- Expand ~ to home directories. elseif match ("~", url) then - if strsub(url, 2, 2) == "/" then -- ~/foo - return home_dir..strsub(url, 2) - else -- ~foo/bar - return "/home/"..strsub(url, 2) + if string.sub(url, 2, 2) == "/" then -- ~/foo + return os.getenv ("HOME")..string.sub(url, 2) + else -- ~foo/bar + return "/home/"..string.sub(url, 2) end . @@ -436,16 +434,16 @@ use ELinks Lua support to manipulate the HTML before it reaches the parser. linuxtoday.com ++++++++++++++ -Note: This recipe is out of date. +NOTE: This recipe is out of date for the web site. Linux Today has two problems when viewed in ELinks: the useless columns on the left and the right and all the text appears in cyan. Here is a quick recipe to fix that: ------------------------------------------------------------------------------- --- Plain strfind (no metacharacters) +-- Plain string.find (no metacharacters) function sstrfind (s, pattern) - return strfind (s, pattern, 1, 1) + return string.find (s, pattern, 1, true) end function pre_format_html_hook (url, html) @@ -453,15 +451,15 @@ function pre_format_html_hook (url, html) -- and change the font colour to white. if sstrfind (url, "linuxtoday.com") then if sstrfind (url, "news_story") then - html = gsub (html, '