aboutsummaryrefslogtreecommitdiff
path: root/config.dot/luakit.link/binds.lua
blob: cac303fb8622f37a9d2b2fd85d40dacf6f9d627f (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
-----------------
-- Keybindings --
-----------------

-- Binding aliases
local key, buf, but = lousy.bind.key, lousy.bind.buf, lousy.bind.but
local cmd, any = lousy.bind.cmd, lousy.bind.any

-- Util aliases
local match, join = string.match, lousy.util.table.join
local strip, split = lousy.util.string.strip, lousy.util.string.split

-- Globals or defaults that are used in binds
local scroll_step = globals.scroll_step or 20
local more, less = "+"..scroll_step.."px", "-"..scroll_step.."px"
local zoom_step = globals.zoom_step or 0.1

-- Add binds to a mode
function add_binds(mode, binds, before)
    assert(binds and type(binds) == "table", "invalid binds table type: " .. type(binds))
    mode = type(mode) ~= "table" and {mode} or mode
    for _, m in ipairs(mode) do
        local mdata = get_mode(m)
        if mdata and before then
            mdata.binds = join(binds, mdata.binds or {})
        elseif mdata then
            mdata.binds = mdata.binds or {}
            for _, b in ipairs(binds) do table.insert(mdata.binds, b) end
        else
            new_mode(m, { binds = binds })
        end
    end
end

-- Add commands to command mode
function add_cmds(cmds, before)
    add_binds("command", cmds, before)
end

-- Adds the default menu widget bindings to a mode
menu_binds = {
    -- Navigate items
    key({},          "j",       function (w) w.menu:move_down() end),
    key({},          "k",       function (w) w.menu:move_up()   end),
    key({},          "Down",    function (w) w.menu:move_down() end),
    key({},          "Up",      function (w) w.menu:move_up()   end),
    key({},          "Tab",     function (w) w.menu:move_down() end),
    key({"Shift"},   "Tab",     function (w) w.menu:move_up()   end),
}

-- Add binds to special mode "all" which adds its binds to all modes.
add_binds("all", {
    key({},          "Escape",  function (w) w:set_mode() end),
    key({"Control"}, "[",       function (w) w:set_mode() end),

    -- Mouse bindings
    but({},     8,  function (w) w:back()     end),
    but({},     9,  function (w) w:forward()  end),

    -- Open link in new tab or navigate to selection
    but({},     2,  function (w, m)
        -- Ignore button 2 clicks in form fields
        if not m.context.editable then
            -- Open hovered uri in new tab
            local uri = w.view.hovered_uri
            if uri then
                w:new_tab(uri, false)
            else -- Open selection in current tab
                uri = luakit.selection.primary
                if uri then w:navigate(w:search_open(uri)) end
            end
        end
    end),

    -- Open link in new tab when Ctrl-clicked.
    but({"Control"}, 1, function (w, m)
        local uri = w.view.hovered_uri
        if uri then
            w:new_tab(uri, false)
        end
    end),

    -- Zoom binds
    but({"Control"}, 4, function (w, m) w:zoom_in()  end),
    but({"Control"}, 5, function (w, m) w:zoom_out() end),

    -- Horizontal mouse scroll binds
    but({"Shift"},   4, function (w, m) w:scroll{ x = less } end),
    but({"Shift"},   5, function (w, m) w:scroll{ x = more } end),
})

add_binds("normal", {
    -- Autoparse the `[count]` before a binding and re-call the hit function
    -- with the count removed and added to the opts table.
    any(function (w, m)
        local count, buf
        if m.buffer then
            count = string.match(m.buffer, "^(%d+)")
        end
        if count then
            buf = string.sub(m.buffer, #count + 1, (m.updated_buf and -2) or -1)
            local opts = join(m, {count = tonumber(count)})
            opts.buffer = (#buf > 0 and buf) or nil
            if lousy.bind.hit(w, m.binds, m.mods, m.key, opts) then
                return true
            end
        end
        return false
    end),

    key({},          "i",           function (w) w:set_mode("insert")  end),
    key({},          ":",           function (w) w:set_mode("command") end),

    -- Scrolling
    key({},          "j",           function (w) w:scroll{ y = more    } end),
    key({},          "k",           function (w) w:scroll{ y = less    } end),
    key({},          "h",           function (w) w:scroll{ x = less    } end),
    key({},          "l",           function (w) w:scroll{ x = more    } end),
    key({},          "^",           function (w) w:scroll{ x = "0%"    } end),
    key({},          "$",           function (w) w:scroll{ x = "100%"  } end),
    key({"Control"}, "e",           function (w) w:scroll{ y = more    } end),
    key({"Control"}, "y",           function (w) w:scroll{ y = less    } end),
    key({"Control"}, "d",           function (w) w:scroll{ y = "+0.5p" } end),
    key({"Control"}, "u",           function (w) w:scroll{ y = "-0.5p" } end),
    key({"Control"}, "f",           function (w) w:scroll{ y = "+1.0p" } end),
    key({"Control"}, "b",           function (w) w:scroll{ y = "-1.0p" } end),
    key({},          "space",       function (w) w:scroll{ y = "+1.0p" } end),
    key({"Shift"},   "space",       function (w) w:scroll{ y = "-1.0p" } end),
    key({},          "BackSpace",   function (w) w:scroll{ y = "-1.0p" } end),

    -- Specific scroll
    buf("^gg$",                     function (w, b, m) w:scroll{ y = m.count.."%" } end, {count = 0}),
    buf("^G$",                      function (w, b, m) w:scroll{ y = m.count.."%" } end, {count = 100}),

    -- Traditional scrolling commands
    key({},          "Down",        function (w) w:scroll{ y = more    } end),
    key({},          "Up",          function (w) w:scroll{ y = less    } end),
    key({},          "Left",        function (w) w:scroll{ x = less    } end),
    key({},          "Right",       function (w) w:scroll{ x = more    } end),
    key({},          "Page_Down",   function (w) w:scroll{ y = "+1.0p" } end),
    key({},          "Page_Up",     function (w) w:scroll{ y = "-1.0p" } end),
    key({},          "Home",        function (w) w:scroll{ y = "0%"    } end),
    key({},          "End",         function (w) w:scroll{ y = "100%"  } end),
    key({},          "$",           function (w) w:scroll{ x = "100%"  } end),
    key({},          "0",           function (w, m)
                                        if not m.count then w:scroll{ y = "0%" } else return false end
                                    end),

    -- Zooming
    key({},          "+",           function (w, m)    w:zoom_in(zoom_step  * m.count)       end, {count=1}),
    key({},          "-",           function (w, m)    w:zoom_out(zoom_step * m.count)       end, {count=1}),
    key({},          "=",           function (w, m)    w:zoom_set() end),
    buf("^z[iI]$",                  function (w, b, m) w:zoom_in(zoom_step  * m.count, b == "zI") end, {count=1}),
    buf("^z[oO]$",                  function (w, b, m) w:zoom_out(zoom_step * m.count, b == "zO") end, {count=1}),
    -- Zoom reset or specific zoom ([count]zZ for full content zoom)
    buf("^z[zZ]$",                  function (w, b, m) w:zoom_set(m.count/100, b == "zZ") end, {count=100}),

    -- Fullscreen
    key({},          "F11",         function (w)
                                        w.win.fullscreen = not w.win.fullscreen
                                    end),

    -- Clipboard
    key({},          "p",           function (w)
                                        local uri = luakit.selection.primary
                                        if uri then w:navigate(w:search_open(uri)) else w:error("Empty selection.") end
                                    end),
    key({},          "P",           function (w, m)
                                        local uri = luakit.selection.primary
                                        if not uri then w:error("Empty selection.") return end
                                        for i = 1, m.count do w:new_tab(w:search_open(uri)) end
                                    end, {count = 1}),

    -- Yanking
    buf("^yy$",                     function (w)
                                        local uri = string.gsub(w.view.uri or "", " ", "%%20")
                                        luakit.selection.primary = uri
                                        luakit.selection.clipboard = uri
                                        w:notify("Yanked uri: " .. uri)
                                    end),

    buf("^yt$",                     function (w)
                                        local title = w.view.title
                                        luakit.selection.primary = title
                                        luakit.selection.clipboard = uri
                                        w:notify("Yanked title: " .. title)
                                    end),

    -- Commands
    key({"Control"}, "a",           function (w)    w:navigate(w:inc_uri(1)) end),
    key({"Control"}, "x",           function (w)    w:navigate(w:inc_uri(-1)) end),
    buf("^o$",                      function (w, c) w:enter_cmd(":open ")    end),
    buf("^t$",                      function (w, c) w:enter_cmd(":tabopen ") end),
    buf("^w$",                      function (w, c) w:enter_cmd(":winopen ") end),
    buf("^O$",                      function (w, c) w:enter_cmd(":open "    .. (w.view.uri or "")) end),
    buf("^T$",                      function (w, c) w:enter_cmd(":tabopen " .. (w.view.uri or "")) end),
    buf("^W$",                      function (w, c) w:enter_cmd(":winopen " .. (w.view.uri or "")) end),
    buf("^,g$",                     function (w, c) w:enter_cmd(":open google ") end),
    buf("^,d$",                     function (w, c) w:enter_cmd(":open duckduckgo ") end),
    buf("^,h$",                     function (w, c) w:enter_cmd(":open duckduckgo !hackage "
) end),
    buf("^,n$",                     function (w, c) w:enter_cmd(":open netflix "
) end),

    -- History
    key({},          "H",           function (w, m) w:back(m.count)    end),
    key({},          "L",           function (w, m) w:forward(m.count) end),
    key({},          "b",           function (w, m) w:back(m.count)    end),
    key({},          "XF86Back",    function (w, m) w:back(m.count)    end),
    key({},          "XF86Forward", function (w, m) w:forward(m.count) end),
    key({"Control"}, "o",           function (w, m) w:back(m.count)    end),
    key({"Control"}, "i",           function (w, m) w:forward(m.count) end),

    -- Tab
    key({"Control"}, "Page_Up",     function (w)       w:prev_tab() end),
    key({"Control"}, "Page_Down",   function (w)       w:next_tab() end),
    key({"Control"}, "Tab",         function (w)       w:next_tab() end),
    key({"Shift","Control"}, "Tab", function (w)       w:prev_tab() end),
    buf("^gT$",                     function (w, b, m) w:prev_tab(m.count) end, {count=1}),
    buf("^gt$",                     function (w, b, m) if not w:goto_tab(m.count) then w:next_tab() end end, {count=0}),

    key({"Control"}, "t",           function (w)    w:new_tab(globals.homepage) end),
    key({"Control"}, "w",           function (w)    w:close_tab()       end),
    key({},          "d",           function (w, m) for i=1,m.count do w:close_tab()      end end, {count=1}),

    key({},          "<",           function (w, m) w.tabs:reorder(w.view, w.tabs:current() - m.count) end, {count=1}),
    key({},          ">",           function (w, m) w.tabs:reorder(w.view, (w.tabs:current() + m.count) % w.tabs:count()) end, {count=1}),
    key({"Mod1"},    "Page_Up",     function (w, m) w.tabs:reorder(w.view, w.tabs:current() - m.count) end, {count=1}),
    key({"Mod1"},    "Page_Down",   function (w, m) w.tabs:reorder(w.view, (w.tabs:current() + m.count) % w.tabs:count()) end, {count=1}),

    buf("^gH$",                     function (w, b, m) for i=1,m.count do w:new_tab(globals.homepage) end end, {count=1}),
    buf("^gh$",                     function (w)       w:navigate(globals.homepage) end),

    -- Open tab from current tab history
    buf("^gy$",                     function (w) w:new_tab(w.view.history or "") end),

    key({},          "r",           function (w) w:reload() end),
    key({},          "R",           function (w) w:reload(true) end),
    key({"Control"}, "c",           function (w) w:stop() end),

    -- Config reloading
    key({"Control", "Shift"}, "R",  function (w) w:restart() end),

    -- Window
    buf("^ZZ$",                     function (w) w:save_session() w:close_win() end),
    buf("^ZQ$",                     function (w) w:close_win() end),
    buf("^D$",                      function (w) w:close_win() end),

    -- Enter passthrough mode
    key({"Control"}, "z",           function (w) w:set_mode("passthrough") end),

    -- Quit shortcut
    key({},          "q",           function (w) w:save_session() w:close_win() end),
})

add_binds("insert", {
    key({"Control"}, "z",           function (w) w:set_mode("passthrough") end),
})

add_binds({"command", "search"}, {
    key({"Shift"},   "Insert",  function (w) w:insert_cmd(luakit.selection.primary) end),
    key({"Control"}, "w",       function (w) w:del_word() end),
    key({"Control"}, "u",       function (w) w:del_line() end),
    key({"Control"}, "h",       function (w) w:del_backward_char() end),
    key({"Control"}, "d",       function (w) w:del_forward_char() end),
    key({"Control"}, "a",       function (w) w:beg_line() end),
    key({"Control"}, "e",       function (w) w:end_line() end),
    key({"Control"}, "f",       function (w) w:forward_char() end),
    key({"Control"}, "b",       function (w) w:backward_char() end),
    key({"Mod1"},    "f",       function (w) w:forward_word() end),
    key({"Mod1"},    "b",       function (w) w:backward_word() end),
})

-- Switching tabs with Mod1+{1,2,3,...}
mod1binds = {}
for i=1,10 do
    table.insert(mod1binds,
        key({"Mod1"}, tostring(i % 10), function (w) w.tabs:switch(i) end))
end
add_binds("normal", mod1binds)

-- Command bindings which are matched in the "command" mode from text
-- entered into the input bar.
add_cmds({
    -- Detect bangs (I.e. ":command! <args>")
    buf("^%S+!", function (w, cmd, opts)
        local cmd, args = string.match(cmd, "^(%S+)!+(.*)")
        if cmd then
            opts = join(opts, { bang = true })
            return lousy.bind.match_cmd(w, opts.binds, cmd .. args, opts)
        end
    end),

 -- cmd({command, alias1, ...}, function (w, arg, opts) .. end, opts),
 -- cmd("co[mmand]",            function (w, arg, opts) .. end, opts),
    cmd("c[lose]",              function (w) w:close_tab() end),
    cmd("print",                function (w) w:eval_js("print()", "rc.lua") end),
    cmd("reload",               function (w) w:reload() end),
    cmd("restart",              function (w) w:restart() end),
    cmd("write",                function (w) w:save_session() end),
    cmd("noh[lsearch]",         function (w) w:clear_search() end),

    cmd("back",                 function (w, a) w:back(tonumber(a) or 1) end),
    cmd("f[orward]",            function (w, a) w:forward(tonumber(a) or 1) end),
    cmd("inc[rease]",           function (w, a) w:navigate(w:inc_uri(tonumber(a) or 1)) end),
    cmd("o[pen]",               function (w, a) w:navigate(w:search_open(a)) end),
    cmd("t[abopen]",            function (w, a) w:new_tab(w:search_open(a)) end),
    cmd("w[inopen]",            function (w, a) window.new{w:search_open(a)} end),
    cmd({"javascript",   "js"}, function (w, a) w:eval_js(a, "javascript") end),

    cmd("q[uit]",               function (w, a, o) w:close_win(o.bang) end),
    cmd({"viewsource",  "vs" }, function (w, a, o) w:toggle_source(not o.bang and true or nil) end),
    cmd({"writequit", "wq"},    function (w, a, o) w:save_session() w:close_win(o.bang) end),

    cmd("lua", function (w, a)
        if a then
            local ret = assert(loadstring("return function(w) return "..a.." end"))()(w)
            if ret then print(ret) end
        else
            w:set_mode("lua")
        end
    end),

    cmd("dump", function (w, a)
        local fname = string.gsub(w.win.title, '[^%w%.%-]', '_')..'.html' -- sanitize filename
        local file = a or luakit.save_file("Save file", w.win, xdg.download_dir or '.', fname)
        if file then
            local fd = assert(io.open(file, "w"), "failed to open: " .. file)
            local html = assert(w:eval_js("document.documentElement.outerHTML", "dump"), "Unable to get HTML")
            assert(fd:write(html), "unable to save html")
            io.close(fd)
            w:notify("Dumped HTML to: " .. file)
        end
    end),
})

-- vim: et:sw=4:ts=8:sts=4:tw=80