|
1 ----------------- |
|
2 -- Keybindings -- |
|
3 ----------------- |
|
4 |
|
5 binds = {} |
|
6 |
|
7 -- Binding aliases |
|
8 local key, buf, but, cmd = lousy.bind.key, lousy.bind.buf, lousy.bind.but, lousy.bind.cmd |
|
9 |
|
10 -- Globals or defaults that are used in binds |
|
11 local scroll_step = globals.scroll_step or 20 |
|
12 local zoom_step = globals.zoom_step or 0.1 |
|
13 local homepage = globals.homepage or "http://luakit.org" |
|
14 |
|
15 -- Add key bindings to be used across all windows in the given modes. |
|
16 binds.mode_binds = { |
|
17 -- buf(Pattern, function (w, buffer, opts) .. end, opts), |
|
18 -- key({Modifiers}, Key name, function (w, opts) .. end, opts), |
|
19 -- but({Modifiers}, Button num, function (w, opts) .. end, opts), |
|
20 all = { |
|
21 key({}, "Escape", function (w) w:set_mode() end), |
|
22 key({"Control"}, "[", function (w) w:set_mode() end), |
|
23 |
|
24 but({}, 8, function (w) w:back() end), |
|
25 but({}, 9, function (w) w:forward() end), |
|
26 }, |
|
27 normal = { |
|
28 key({}, "i", function (w) w:set_mode("insert") end), |
|
29 key({}, ":", function (w) w:set_mode("command") end), |
|
30 |
|
31 -- Scrolling |
|
32 key({}, "j", function (w) w:scroll_vert("+"..scroll_step.."px") end), |
|
33 key({}, "k", function (w) w:scroll_vert("-"..scroll_step.."px") end), |
|
34 key({}, "h", function (w) w:scroll_horiz("-"..scroll_step.."px") end), |
|
35 key({}, "l", function (w) w:scroll_horiz("+"..scroll_step.."px") end), |
|
36 key({"Control"}, "d", function (w) w:scroll_page(0.5) end), |
|
37 key({"Control"}, "u", function (w) w:scroll_page(-0.5) end), |
|
38 key({"Control"}, "f", function (w) w:scroll_page(1.0) end), |
|
39 key({"Control"}, "b", function (w) w:scroll_page(-1.0) end), |
|
40 buf("^gg$", function (w) w:scroll_vert("0%") end), |
|
41 buf("^G$", function (w) w:scroll_vert("100%") end), |
|
42 buf("^[\-\+]?[0-9]+[%%G]$", function (w, b) w:scroll_vert(string.match(b, "^([\-\+]?%d+)[%%G]$") .. "%") end), |
|
43 |
|
44 -- Traditional scrolling commands |
|
45 key({}, "Down", function (w) w:scroll_vert("+"..scroll_step.."px") end), |
|
46 key({}, "Up", function (w) w:scroll_vert("-"..scroll_step.."px") end), |
|
47 key({}, "Left", function (w) w:scroll_horiz("-"..scroll_step.."px") end), |
|
48 key({}, "Right", function (w) w:scroll_horiz("+"..scroll_step.."px") end), |
|
49 key({}, "Page_Down", function (w) w:scroll_page(1.0) end), |
|
50 key({}, "Page_Up", function (w) w:scroll_page(-1.0) end), |
|
51 key({}, "Home", function (w) w:scroll_vert("0%") end), |
|
52 key({}, "End", function (w) w:scroll_vert("100%") end), |
|
53 |
|
54 -- Zooming |
|
55 buf("^z0$", function (w) w:zoom_reset() end), |
|
56 buf("^zI$", function (w) w:zoom_in(zoom_step) end), |
|
57 buf("^zO$", function (w) w:zoom_out(zoom_step) end), |
|
58 key({"Control"}, "+", function (w) w:zoom_in(zoom_step) end), |
|
59 key({"Control"}, "-", function (w) w:zoom_out(zoom_step) end), |
|
60 |
|
61 -- Clipboard |
|
62 key({}, "p", function (w) w:navigate(luakit.get_selection()) end), |
|
63 key({}, "P", function (w) w:new_tab(luakit.get_selection()) end), |
|
64 buf("^yy$", function (w) luakit.set_selection((w:get_current() or {}).uri or "") end), |
|
65 buf("^yt$", function (w) luakit.set_selection(w.win.title) end), |
|
66 |
|
67 -- Commands |
|
68 buf("^o$", function (w, c) w:enter_cmd(":open ") end), |
|
69 buf("^t$", function (w, c) w:enter_cmd(":tabopen ") end), |
|
70 buf("^w$", function (w, c) w:enter_cmd(":winopen ") end), |
|
71 buf("^O$", function (w, c) w:enter_cmd(":open " .. ((w:get_current() or {}).uri or "")) end), |
|
72 buf("^T$", function (w, c) w:enter_cmd(":tabopen " .. ((w:get_current() or {}).uri or "")) end), |
|
73 buf("^W$", function (w, c) w:enter_cmd(":winopen " .. ((w:get_current() or {}).uri or "")) end), |
|
74 buf("^,g$", function (w, c) w:enter_cmd(":websearch google ") end), |
|
75 |
|
76 -- Debian search shorcut access |
|
77 buf("^\\dbug$", function (w, c) w:enter_cmd(":websearch debbugs ") end), |
|
78 buf("^\\dpts$", function (w, c) w:enter_cmd(":websearch dpts ") end), |
|
79 buf("^\\dpkg$", function (w, c) w:enter_cmd(":websearch dpkg ") end), |
|
80 |
|
81 -- Searching |
|
82 key({}, "/", function (w) w:start_search("/") end), |
|
83 key({}, "?", function (w) w:start_search("?") end), |
|
84 key({}, "n", function (w) w:search(nil, true) end), |
|
85 key({}, "N", function (w) w:search(nil, false) end), |
|
86 |
|
87 -- History |
|
88 buf("^[0-9]*H$", function (w, b) w:back (tonumber(string.match(b, "^(%d*)H$") or 1)) end), |
|
89 buf("^[0-9]*L$", function (w, b) w:forward(tonumber(string.match(b, "^(%d*)L$") or 1)) end), |
|
90 key({}, "b", function (w) w:back() end), |
|
91 key({}, "XF86Back", function (w) w:back() end), |
|
92 key({}, "XF86Forward", function (w) w:forward() end), |
|
93 |
|
94 -- Tab |
|
95 key({"Control"}, "Page_Up", function (w) w:prev_tab() end), |
|
96 key({"Control"}, "Page_Down", function (w) w:next_tab() end), |
|
97 buf("^[0-9]*gT$", function (w, b) w:prev_tab(tonumber(string.match(b, "^(%d*)gT$") or 1)) end), |
|
98 buf("^[0-9]*gt$", function (w, b) w:next_tab(tonumber(string.match(b, "^(%d*)gt$") or 1)) end), |
|
99 buf("^gH$", function (w) w:new_tab(homepage) end), |
|
100 buf("^d$", function (w) w:close_tab() end), |
|
101 |
|
102 key({}, "r", function (w) w:reload() end), |
|
103 buf("^gh$", function (w) w:navigate(homepage) end), |
|
104 |
|
105 -- Window |
|
106 buf("^ZZ$", function (w) w:close_win() end), |
|
107 buf("^D$", function (w) w:close_win() end), |
|
108 |
|
109 -- Link following |
|
110 key({}, "f", function (w) w:set_mode("follow") end), |
|
111 |
|
112 -- Bookmarking |
|
113 key({}, "B", function (w) w:enter_cmd(":bookmark " .. ((w:get_current() or {}).uri or "http://") .. " ") end), |
|
114 buf("^gb$", function (w) w:navigate(bookmarks.dump_html()) end), |
|
115 buf("^gB$", function (w) w:new_tab (bookmarks.dump_html()) end), |
|
116 |
|
117 -- Mouse bindings |
|
118 but({}, 2, function (w) |
|
119 -- Open hovered uri in new tab |
|
120 local uri = w:get_current().hovered_uri |
|
121 if uri then w:new_tab(uri) |
|
122 else -- Open selection in current tab |
|
123 uri = luakit.get_selection() |
|
124 if uri then w:get_current().uri = uri end |
|
125 end |
|
126 end), |
|
127 }, |
|
128 command = { |
|
129 key({"Shift"}, "Insert", function (w) w:insert_cmd(luakit.get_selection()) end), |
|
130 key({}, "Up", function (w) w:cmd_hist_prev() end), |
|
131 key({}, "Down", function (w) w:cmd_hist_next() end), |
|
132 key({}, "Tab", function (w) w:cmd_completion() end), |
|
133 key({"Control"}, "w", function (w) w:del_word() end), |
|
134 key({"Control"}, "u", function (w) w:del_line() end), |
|
135 }, |
|
136 search = { |
|
137 key({}, "Up", function (w) w:srch_hist_prev() end), |
|
138 key({}, "Down", function (w) w:srch_hist_next() end), |
|
139 }, |
|
140 insert = { }, |
|
141 } |
|
142 |
|
143 -- Command bindings which are matched in the "command" mode from text |
|
144 -- entered into the input bar. |
|
145 binds.commands = { |
|
146 -- cmd({Command, Alias1, ...}, function (w, arg, opts) .. end, opts), |
|
147 cmd({"open", "o" }, function (w, a) w:navigate(a) end), |
|
148 cmd({"tabopen", "t" }, function (w, a) w:new_tab(a) end), |
|
149 cmd({"winopen", "w" }, function (w, a) window.new{a} end), |
|
150 cmd({"back" }, function (w, a) w:back(tonumber(a) or 1) end), |
|
151 cmd({"forward", "f" }, function (w, a) w:forward(tonumber(a) or 1) end), |
|
152 cmd({"scroll" }, function (w, a) w:scroll_vert(a) end), |
|
153 cmd({"quit", "q" }, function (w) luakit.quit() end), |
|
154 cmd({"close", "c" }, function (w) w:close_tab() end), |
|
155 cmd({"websearch", "ws" }, function (w, e, s) w:websearch(e, s) end), |
|
156 cmd({"reload", }, function (w) w:reload() end), |
|
157 cmd({"viewsource", "vs" }, function (w) w:toggle_source(true) end), |
|
158 cmd({"viewsource!", "vs!"}, function (w) w:toggle_source() end), |
|
159 cmd({"bookmark", "bm" }, function (w, a) |
|
160 local args = lousy.util.string.split(a) |
|
161 local uri = table.remove(args, 1) |
|
162 bookmarks.add(uri, args) |
|
163 end), |
|
164 } |
|
165 |
|
166 -- Helper functions which are added to the window struct |
|
167 binds.helper_methods = { |
|
168 -- Navigate current view or open new tab |
|
169 navigate = function (w, uri, view) |
|
170 if not view then view = w:get_current() end |
|
171 if view then |
|
172 view.uri = uri |
|
173 else |
|
174 return w:new_tab(uri) |
|
175 end |
|
176 end, |
|
177 |
|
178 -- search engine wrapper |
|
179 websearch = function (w, args) |
|
180 local sep = string.find(args, " ") |
|
181 local engine = string.sub(args, 1, sep-1) |
|
182 local search = string.sub(args, sep+1) |
|
183 search = string.gsub(search, "^%s*(.-)%s*$", "%1") |
|
184 if not search_engines[engine] then |
|
185 return error("No matching search engine found: " .. engine) |
|
186 end |
|
187 local uri = string.gsub(search_engines[engine], "{%d}", search) |
|
188 return w:navigate(uri) |
|
189 end, |
|
190 |
|
191 -- Tab traversing functions |
|
192 next_tab = function (w, n) |
|
193 w.tabs:switch((((n or 1) + w.tabs:current() -1) % w.tabs:count()) + 1) |
|
194 end, |
|
195 |
|
196 prev_tab = function (w, n) |
|
197 w.tabs:switch(((w.tabs:current() - (n or 1) -1) % w.tabs:count()) + 1) |
|
198 end, |
|
199 |
|
200 goto_tab = function (w, n) |
|
201 w.tabs:switch(n) |
|
202 end, |
|
203 |
|
204 -- If argument is form-active or root-active, emits signal. Ignores all |
|
205 -- other signals. |
|
206 emit_form_root_active_signal = function (w, s) |
|
207 if s == "form-active" then |
|
208 w:get_current():emit_signal("form-active") |
|
209 elseif s == "root-active" then |
|
210 w:get_current():emit_signal("root-active") |
|
211 end |
|
212 end, |
|
213 } |
|
214 |
|
215 -- Insert webview method lookup on window structure |
|
216 table.insert(window.indexes, 1, function (w, k) |
|
217 -- Lookup bind helper method |
|
218 return binds.helper_methods[k] |
|
219 end) |
|
220 |
|
221 -- vim: et:sw=4:ts=8:sts=4:tw=80 |