binds.lua

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://google.co.uk/"
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("normal") end),
22
        key({"Control"}, "e",           function (w) w:set_mode("command") end),
23
        key({"Control"}, "n",           function (w) w:set_mode("normal") end),
24
        
25
        key({"Control"}, "x",           function (w) w:close_tab() end),
26
 
27
        key({"Control"}, "c",           function (w) if w:get_mode() ~= "normal" then w:set_mode("normal"); end end),
28
 
29
	-- Location
30
        key({"Control"}, "t",           function (w) w:new_tab(homepage); end),
31
 
32
	-- Location
33
        key({"Control"}, "l",           function (w) w:set_mode("url-entry"); end),
34
        
35
        -- Web search
36
        key({"Control"}, "k",           function (w) w:enter_cmd(":websearch google ") end);
37
        
38
        -- Back and forth
39
	key({"Mod1"},    "Left",        function (w) w:back()    end);
40
	key({"Mod1"},    "Right",       function (w) w:forward()    end);
41
	
42
	-- Tab/buffer switching
43
	key({"Mod1"},    ".",           function (w) w:next_tab(1)    end);
44
	key({"Mod1"},    ",",           function (w) w:prev_tab(1)    end);
45
        key({"Control"}, "Page_Up",     function (w) w:prev_tab() end),
46
        key({"Control"}, "Page_Down",   function (w) w:next_tab() end),
47
	
48
        key({"Control"}, "p",           function (w) w:navigate(luakit.get_selection("c")) end),
49
        key({"Control"}, "P",           function (w) w:new_tab(luakit.get_selection("c"))  end),
50
        key({"Control"}, "y",           function (w) luakit.set_selection((w:get_current() or {}).uri or "", "c") end),
51
        key({"Control"}, "Y",           function (w) luakit.set_selection(w.win.title, "c") end),
52
 
53
        key({"Control"}, "r",           function (w) w:reload() end),
54
 
55
	key({"Mod1"},    "Home",        function (w) w:navigate(homepage)  end);
56
 
57
        key({"Control"}, "+",           function (w) w:zoom_in(zoom_step)  end),
58
        key({"Control"}, "-",           function (w) w:zoom_out(zoom_step) end),
59
 
60
        but({},          8,             function (w) w:back()    end),
61
        but({},          9,             function (w) w:forward() end),
62
 
63
        key({},          "XF86Back",    function (w) w:back() end),
64
        key({},          "XF86Forward", function (w) w:forward() end),
65
        
66
        -- Link following
67
        key({"Mod1"},    "f",           function (w) w:set_mode("follow") end),
68
 
69
        -- Bookmarking
70
        key({"Control"}, "d",           function (w) end),
71
        key({"Control"}, "b",           function (w) w:navigate(bookmarks.dump_html()) end),
72
        key({"Control"}, "B",           function (w) w:new_tab(bookmarks.dump_html()) end),
73
 
74
        -- Mouse bindings
75
        but({},          2,             function (w)
76
                                            -- Open hovered uri in new tab
77
                                            local uri = w:get_current().hovered_uri
78
                                            if uri then w:new_tab(uri)
79
                                            else -- Open selection in current tab
80
                                                uri = luakit.get_selection()
81
                                                if uri then w:get_current().uri = uri end
82
                                            end
83
                                        end),
84
    },
85
    
86
    normal = {
87
    },
88
    
89
    command = {
90
        key({"Shift"},   "Insert",      function (w) w:insert_cmd(luakit.get_selection()) end),
91
        key({},          "Up",          function (w) w:cmd_hist_prev() end),
92
        key({},          "Down",        function (w) w:cmd_hist_next() end),
93
        key({},          "Tab",         function (w) w:cmd_completion() end),
94
        key({"Control"}, "w",           function (w) w:del_word() end),
95
        key({"Control"}, "u",           function (w) w:del_line() end),
96
    },
97
    search = {
98
        key({},          "Up",          function (w) w:srch_hist_prev() end),
99
        key({},          "Down",        function (w) w:srch_hist_next() end),
100
    },
101
    ["url-entry"] = {
102
        key({"Control"}, "l",          function (w) w.ibar.input:set_position(-1); end),
103
    },
104
    insert = { },
105
}
106
 
107
-- Command bindings which are matched in the "command" mode from text
108
-- entered into the input bar.
109
binds.commands = {
110
 -- cmd({Command, Alias1, ...},         function (w, arg, opts) .. end, opts),
111
    cmd({"open",        "o"  },         function (w, a)    w:navigate(a) end),
112
    cmd({"tabopen",     "t"  },         function (w, a)    w:new_tab(a) end),
113
    cmd({"winopen",     "w"  },         function (w, a)    window.new{a} end),
114
    cmd({"back"              },         function (w, a)    w:back(tonumber(a) or 1) end),
115
    cmd({"forward",     "f"  },         function (w, a)    w:forward(tonumber(a) or 1) end),
116
    cmd({"scroll"            },         function (w, a)    w:scroll_vert(a) end),
117
    cmd({"quit",        "q"  },         function (w)       luakit.quit() end),
118
    cmd({"close",       "c"  },         function (w)       w:close_tab() end),
119
    cmd({"websearch",   "ws" },         function (w, e, s) w:websearch(e, s) end),
120
    cmd({"reload",           },         function (w)       w:reload() end),
121
    cmd({"viewsource",  "vs" },         function (w)       w:toggle_source(true) end),
122
    cmd({"viewsource!", "vs!"},         function (w)       w:toggle_source() end),
123
    cmd({"bookmark",    "bm" },         function (w, a)
124
                                            local args = lousy.util.string.split(a)
125
                                            local uri = table.remove(args, 1)
126
                                            bookmarks.add(uri, args)
127
                                        end),
128
}
129
 
130
-- Helper functions which are added to the window struct
131
binds.helper_methods = {
132
    -- Navigate current view or open new tab
133
    navigate = function (w, uri, view)
134
        if not view then view = w:get_current() end
135
        if view then
136
            view.uri = uri
137
        else
138
            return w:new_tab(uri)
139
        end
140
    end,
141
 
142
    -- search engine wrapper
143
    websearch = function (w, args)
144
        local sep = string.find(args, " ")
145
        local engine = string.sub(args, 1, sep-1)
146
        local search = string.sub(args, sep+1)
147
        search = string.gsub(search, "^%s*(.-)%s*$", "%1")
148
        if not search_engines[engine] then
149
            return error("No matching search engine found: " .. engine)
150
        end
151
        local uri = string.gsub(search_engines[engine], "{%d}", search)
152
        return w:navigate(uri)
153
    end,
154
 
155
    -- Tab traversing functions
156
    next_tab = function (w, n)
157
        w.tabs:switch((((n or 1) + w.tabs:current() -1) % w.tabs:count()) + 1)
158
    end,
159
 
160
    prev_tab = function (w, n)
161
        w.tabs:switch(((w.tabs:current() - (n or 1) -1) % w.tabs:count()) + 1)
162
    end,
163
 
164
    goto_tab = function (w, n)
165
        w.tabs:switch(n)
166
    end,
167
 
168
    -- If argument is form-active or root-active, emits signal. Ignores all
169
    -- other signals.
170
    emit_form_root_active_signal = function (w, s)
171
        if s == "form-active" then
172
            w:get_current():emit_signal("form-active")
173
        elseif s == "root-active" then
174
            w:get_current():emit_signal("root-active")
175
        end
176
    end,
177
}
178
 
179
-- Insert webview method lookup on window structure
180
table.insert(window.indexes, 1, function (w, k)
181
    -- Lookup bind helper method
182
    return binds.helper_methods[k]
183
end)
184
 
185
-- vim: et:sw=4:ts=8:sts=4:tw=80