File: query_url.lua

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (79 lines) | stat: -rw-r--r-- 2,456 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
-- Authors: Reuben Thomas <rrt@sc3d.org>
-- License: GPL, version 2
-- Last Changed: 2005
--
-- Prompt for a URL and open it
-- Use Opera bookmarks for completion
-- (c) Reuben Thomas 2005 (rrt@sc3d.org); released under the GPL
--
-- Completes on full URL, name of bookmark, or URL with initial http[s]://[www.] stripped
--
-- Use something like:
--
--    kpress(MOD2.."F3", 'query_url(_)'),
--
-- The browser defaults to "sensible-browser"; configure below:

local browser = "sensible-browser"


-- Read Opera bookmarks
function readOperaBookmarks ()
  local bookmarks, names = {}, {}

  -- Parse bookmarks file
  -- Make a list of bookmarks and their names for use as completions.
  -- Also make a map of names to URLs.
  local f = io.lines (os.getenv ("HOME") .. "/.opera/opera6.adr")
  local nameTag = "\tNAME="
  local nameOff = string.len (nameTag) + 1
  local namePat = "^" .. nameTag
  local urlTag = "\tURL="
  local urlOff = string.len (urlTag) + 1
  local urlPat = "^" .. urlTag
  local l = f ()
  while l do
    if string.find (l, namePat) then
      local name = string.sub (l, nameOff)
      l = f ()
      if string.find (l, urlPat) then
        local url = string.sub (l, urlOff)
        table.insert (bookmarks, url)
        -- TODO: Make the name and stripped URL processing below
        -- independent of browsing Opera's bookmarks, and add other
        -- bookmark formats
        local urlIndex = # (bookmarks)
        names[name] = urlIndex
        table.insert (bookmarks, name)
        local strippedUrl = string.gsub (url, "^https?://", "")
        strippedUrl = string.gsub (strippedUrl, "^www\.", "")
        if url ~= strippedUrl then
          names[strippedUrl] = urlIndex
          table.insert (bookmarks, strippedUrl)
        end
      end
    else
      l = f ()
    end
  end

  return bookmarks, names
end

-- Prompt for a URL, matching by name or URL
function query_url (mplex)
  local bookmarks, names = readOperaBookmarks ()
  local function handler (mplex, str)
    -- If str is the name of a URL, get the url instead
    local index = names[str]
    if index then
      str = bookmarks[index]
    end
    -- FIXME: Ion needs a standard way of invoking a browser
    ioncore.exec_on (mplex, browser .. ' "' .. str .. '"')
  end
  local function completor (wedln, what)
    wedln:set_completions (mod_query.complete_from_list (bookmarks, what))
  end
  mod_query.query (mplex, "URL: ", nil, handler, completor)
end