webview.lua

Wed, 01 Sep 2010 03:53:30 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 01 Sep 2010 03:53:30 +0100
changeset 0
98e4b0c9fcac
child 1
4d7540af8518
permissions
-rw-r--r--

Initial commit of default luakit config from 2010.08.30

0
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 --------------------------
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- WebKit WebView class --
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 --------------------------
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- Webview class table
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 webview = {}
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- Table of functions which are called on new webview widgets.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 webview.init_funcs = {
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 -- Set global properties
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 set_global_props = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 -- Set proxy options
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local proxy = globals.http_proxy or os.getenv("http_proxy")
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if proxy then view:set_prop('proxy-uri', proxy) end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 view:set_prop('user-agent', globals.useragent)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 -- Set ssl options
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if globals.ssl_strict ~= nil then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 view:set_prop('ssl-strict', globals.ssl_strict)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if globals.ca_file and os.exists(globals.ca_file) then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 view:set_prop('ssl-ca-file', globals.ca_file)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- Warning: update the following variable if 'ssl-ca-file' is
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 -- changed anywhere else.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 w.checking_ssl = true
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- Update window and tab titles
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 title_update = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 view:add_signal("property::title", function (v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 w:update_tab_labels()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if w:is_current(v) then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 w:update_win_title()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 -- Update uri label in statusbar
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 uri_update = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 view:add_signal("property::uri", function (v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 w:update_tab_labels()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 if w:is_current(v) then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 w:update_uri(v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 -- Update scroll widget
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 scroll_update = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 view:add_signal("expose", function (v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if w:is_current(v) then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 w:update_scroll(v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 -- Update progress widget
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 progress_update = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 for _, sig in ipairs({"load-status", "property::progress"}) do
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 view:add_signal(sig, function (v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if w:is_current(v) then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 w:update_progress(v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 w:update_ssl(v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 -- Display hovered link in statusbar
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 link_hover_display = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 view:add_signal("link-hover", function (v, link)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if w:is_current(v) and link then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 w.sbar.l.uri.text = "Link: " .. lousy.util.escape(link)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 view:add_signal("link-unhover", function (v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if w:is_current(v) then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 w:update_uri(v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 -- Clicking a form field automatically enters insert mode
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 form_insert_mode = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 view:add_signal("form-active", function ()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 (w.search_state or {}).marker = nil
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 w:set_mode("insert")
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 view:add_signal("root-active", function ()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 (w.search_state or {}).marker = nil
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 w:set_mode()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 -- Stop key events hitting the webview if the user isn't in insert mode
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 mode_key_filter = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 view:add_signal("key-press", function ()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if not w:is_mode("insert") then return true end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 -- Try to match a button event to a users button binding else let the
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 -- press hit the webview.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 button_bind_match = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 -- Match button press
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 view:add_signal("button-release", function (v, mods, button)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 (w.search_state or {}).marker = nil
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 if w:hit(mods, button) then return true end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 -- Reset the mode on navigation
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 mode_reset_on_nav = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 view:add_signal("load-status", function (v, status)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 if w:is_current(v) and status == "provisional" then w:set_mode() end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 -- Domain properties
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 domain_properties = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 view:add_signal("load-status", function (v, status)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 if status ~= "provisional" then return end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local domain = (v.uri and string.match(v.uri, "^%a+://([^/]*)/?")) or "about:blank"
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 if string.match(domain, "^www.") then domain = string.sub(domain, 5) end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 local props = lousy.util.table.join(domain_props.all or {}, domain_props[domain] or {})
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 for k, v in pairs(props) do
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 info("Domain prop: %s = %s (%s)", k, tostring(v), domain)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 view:set_prop(k, v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 -- Action to take on mime type decision request.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 mime_decision = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 -- Return true to accept or false to reject from this signal.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 view:add_signal("mime-type-decision", function (v, link, mime)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 info("Requested link: %s (%s)", link, mime)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 -- i.e. block binary files like *.exe
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 --if mime == "application/octet-stream" then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 -- return false
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 --end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 -- Action to take on window open request.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 window_decision = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 -- 'link' contains the download link
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 -- 'reason' contains the reason of the request (i.e. "link-clicked")
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 -- return TRUE to handle the request by yourself or FALSE to proceed
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 -- with default behaviour
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 view:add_signal("new-window-decision", function (v, link, reason)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 info("New window decision: %s (%s)", link, reason)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 if reason == "link-clicked" then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 window.new({ link })
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 return true
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 w:new_tab(link)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 create_webview = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 -- Return a newly created webview in a new tab
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 view:add_signal("create-web-view", function (v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 return w:new_tab()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 -- Action to take on download request.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 download_request = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 -- 'link' contains the download link
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 -- 'filename' contains the suggested filename (from server or webkit)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 view:add_signal("download-request", function (v, link, filename)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 if not filename then return end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 -- Make download dir
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 os.execute(string.format("mkdir -p %q", globals.download_dir))
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 local dl = globals.download_dir .. "/" .. filename
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 local wget = string.format("wget -q %q -O %q", link, dl)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 info("Launching: %s", wget)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 luakit.spawn(wget)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 -- Creates context menu popup from table (and nested tables).
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 -- Use `true` for menu separators.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 populate_popup = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 view:add_signal("populate-popup", function (v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 return {
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 true,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 { "_Toggle Source", function () w:toggle_source() end },
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 { "_Zoom", {
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 { "Zoom _In", function () w:zoom_in(globals.zoom_step) end },
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 { "Zoom _Out", function () w:zoom_out(globals.zoom_step) end },
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 true,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 { "Zoom _Reset", function () w:zoom_reset() end }, }, },
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 }
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 -- Action to take on resource request.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 resource_request_decision = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 view:add_signal("resource-request-starting", function(v, uri)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 if luakit.verbose then print("Requesting: "..uri) end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 -- Return false to cancel the request.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 }
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 -- These methods are present when you index a window instance and no window
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 -- method is found in `window.methods`. The window then checks if there is an
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 -- active webview and calls the following methods with the given view instance
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 -- as the first argument. All methods must take `view` & `w` as the first two
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 -- arguments.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 webview.methods = {
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 reload = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 view:reload()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 -- Property functions
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 get = function (view, w, k)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 return view:get_prop(k)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 set = function (view, w, k, v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 view:set_prop(k, v)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 -- evaluate javascript code and return string result
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 eval_js = function (view, w, script, file)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 return view:eval_js(script, file or "(inline)")
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 -- evaluate javascript code from file and return string result
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 eval_js_from_file = function (view, w, file)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 local fh, err = io.open(file)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 if not fh then return error(err) end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 local script = fh:read("*a")
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 fh:close()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 return view:eval_js(script, file)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 -- close the current tab
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 close_tab = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 w.tabs:remove(view)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 view.uri = "about:blank"
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 view:destroy()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 w:update_tab_count()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 w:update_tab_labels()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 -- Toggle source view
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 toggle_source = function (view, w, show)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 if show == nil then show = not view:get_view_source() end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 view:set_view_source(show)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 -- Zoom functions
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 zoom_in = function (view, w, step)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 view:set_prop("zoom-level", view:get_prop("zoom-level") + step)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 zoom_out = function (view, w, step)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 local value = view:get_prop("zoom-level") - step
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 view:set_prop("zoom-level", ((value > 0.01) and value) or 0.01)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 zoom_reset = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 view:set_prop("zoom-level", 1.0)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 -- Searching functions
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 start_search = function (view, w, text)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 if string.match(text, "^[\?\/]") then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 w:set_mode("search")
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 local i = w.ibar.input
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 i.text = text
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 i:focus()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 i:set_position(-1)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 else
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 return error("invalid search term, must start with '?' or '/'")
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 search = function (view, w, text, forward)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 if forward == nil then forward = true end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 -- Get search state (or new state)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 if not w.search_state then w.search_state = {} end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 local s = w.search_state
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 -- Get search term
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 text = text or s.last_search
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 if not text or #text == 0 then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 return w:clear_search()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 s.last_search = text
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 if s.forward == nil then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 -- Haven't searched before, save some state.
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 s.forward = forward
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 s.marker = view:get_scroll_vert()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 else
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 -- Invert direction if originally searching in reverse
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 forward = (s.forward == forward)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 view:search(text, false, forward, true);
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 clear_search = function (view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 view:clear_search()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 w.search_state = {}
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 -- Webview scroll functions
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 scroll_vert = function (view, w, value)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 local cur, max = view:get_scroll_vert()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 if type(value) == "string" then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 value = lousy.util.parse_scroll(cur, max, value)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 view:set_scroll_vert(value)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 scroll_horiz = function (view, w, value)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 local cur, max = view:get_scroll_horiz()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 if type(value) == "string" then
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 value = lousy.util.parse_scroll(cur, max, value)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 view:set_scroll_horiz(value)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 -- vertical scroll of a multiple of the view_size
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 scroll_page = function (view, w, value)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 local cur, max, size = view:get_scroll_vert()
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 view:set_scroll_vert(cur + (size * value))
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 -- History traversing functions
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 back = function (view, w, n)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 view:go_back(n or 1)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 forward = function (view, w, n)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 view:go_forward(n or 1)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 end,
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 }
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 function webview.new(w, uri)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 local view = widget{type = "webview"}
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 -- Call webview init functions
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 for k, func in pairs(webview.init_funcs) do
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 func(view, w)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 if uri then view.uri = uri end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 view.show_scrollbars = false
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 return view
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 -- Insert webview method lookup on window structure
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 table.insert(window.indexes, 1, function (w, k)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 -- Get current webview
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 local view = w.tabs:atindex(w.tabs:current())
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 if not view then return end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 -- Lookup webview method
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 local func = webview.methods[k]
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 if not func then return end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 -- Return webview method wrapper function
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 return function (_, ...) return func(view, w, ...) end
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 end)
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372
98e4b0c9fcac Initial commit of default luakit config from 2010.08.30
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 -- vim: et:sw=4:ts=8:sts=4:tw=80

mercurial