binds.lua

Wed, 01 Sep 2010 04:03:42 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 01 Sep 2010 04:03:42 +0100
changeset 2
1e9553b2f9a2
parent 1
4d7540af8518
permissions
-rw-r--r--

Add note about required patch

-----------------
-- Keybindings --
-----------------

binds = {}

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

-- Globals or defaults that are used in binds
local scroll_step = globals.scroll_step or 20
local zoom_step   = globals.zoom_step or 0.1
local homepage    = globals.homepage or "http://google.co.uk/"

-- Add key bindings to be used across all windows in the given modes.
binds.mode_binds = {
     -- buf(Pattern,                    function (w, buffer, opts) .. end, opts),
     -- key({Modifiers}, Key name,      function (w, opts)         .. end, opts),
     -- but({Modifiers}, Button num,    function (w, opts)         .. end, opts),
    all = {
        key({},          "Escape",      function (w) w:set_mode("normal") end),
        key({"Control"}, "e",           function (w) w:set_mode("command") end),
        key({"Control"}, "n",           function (w) w:set_mode("normal") end),
        
        key({"Control"}, "x",           function (w) w:close_tab() end),

        key({"Control"}, "c",           function (w) if w:get_mode() ~= "normal" then w:set_mode("normal"); end end),

	-- Location
        key({"Control"}, "t",           function (w) w:new_tab(homepage); end),

	-- Location
        key({"Control"}, "l",           function (w) w:set_mode("url-entry"); end),
        
        -- Web search
        key({"Control"}, "k",           function (w) w:enter_cmd(":websearch google ") end);
        
        -- Back and forth
	key({"Mod1"},    "Left",        function (w) w:back()    end);
	key({"Mod1"},    "Right",       function (w) w:forward()    end);
	
	-- Tab/buffer switching
	key({"Mod1"},    ".",           function (w) w:next_tab(1)    end);
	key({"Mod1"},    ",",           function (w) w:prev_tab(1)    end);
        key({"Control"}, "Page_Up",     function (w) w:prev_tab() end),
        key({"Control"}, "Page_Down",   function (w) w:next_tab() end),
	
        key({"Control"}, "p",           function (w) w:navigate(luakit.get_selection("c")) end),
        key({"Control"}, "P",           function (w) w:new_tab(luakit.get_selection("c"))  end),
        key({"Control"}, "y",           function (w) luakit.set_selection((w:get_current() or {}).uri or "", "c") end),
        key({"Control"}, "Y",           function (w) luakit.set_selection(w.win.title, "c") end),

        key({"Control"}, "r",           function (w) w:reload() end),

	key({"Mod1"},    "Home",        function (w) w:navigate(homepage)  end);

        key({"Control"}, "+",           function (w) w:zoom_in(zoom_step)  end),
        key({"Control"}, "-",           function (w) w:zoom_out(zoom_step) end),

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

        key({},          "XF86Back",    function (w) w:back() end),
        key({},          "XF86Forward", function (w) w:forward() end),
        
        -- Link following
        key({"Mod1"},    "f",           function (w) w:set_mode("follow") end),

        -- Bookmarking
        key({"Control"}, "d",           function (w) end),
        key({"Control"}, "b",           function (w) w:navigate(bookmarks.dump_html()) end),
        key({"Control"}, "B",           function (w) w:new_tab(bookmarks.dump_html()) end),

        -- Mouse bindings
        but({},          2,             function (w)
                                            -- Open hovered uri in new tab
                                            local uri = w:get_current().hovered_uri
                                            if uri then w:new_tab(uri)
                                            else -- Open selection in current tab
                                                uri = luakit.get_selection()
                                                if uri then w:get_current().uri = uri end
                                            end
                                        end),
    },
    
    normal = {
    },
    
    command = {
        key({"Shift"},   "Insert",      function (w) w:insert_cmd(luakit.get_selection()) end),
        key({},          "Up",          function (w) w:cmd_hist_prev() end),
        key({},          "Down",        function (w) w:cmd_hist_next() end),
        key({},          "Tab",         function (w) w:cmd_completion() end),
        key({"Control"}, "w",           function (w) w:del_word() end),
        key({"Control"}, "u",           function (w) w:del_line() end),
    },
    search = {
        key({},          "Up",          function (w) w:srch_hist_prev() end),
        key({},          "Down",        function (w) w:srch_hist_next() end),
    },
    ["url-entry"] = {
        key({"Control"}, "l",          function (w) w.ibar.input:set_position(-1); end),
    },
    insert = { },
}

-- Command bindings which are matched in the "command" mode from text
-- entered into the input bar.
binds.commands = {
 -- cmd({Command, Alias1, ...},         function (w, arg, opts) .. end, opts),
    cmd({"open",        "o"  },         function (w, a)    w:navigate(a) end),
    cmd({"tabopen",     "t"  },         function (w, a)    w:new_tab(a) end),
    cmd({"winopen",     "w"  },         function (w, a)    window.new{a} end),
    cmd({"back"              },         function (w, a)    w:back(tonumber(a) or 1) end),
    cmd({"forward",     "f"  },         function (w, a)    w:forward(tonumber(a) or 1) end),
    cmd({"scroll"            },         function (w, a)    w:scroll_vert(a) end),
    cmd({"quit",        "q"  },         function (w)       luakit.quit() end),
    cmd({"close",       "c"  },         function (w)       w:close_tab() end),
    cmd({"websearch",   "ws" },         function (w, e, s) w:websearch(e, s) end),
    cmd({"reload",           },         function (w)       w:reload() end),
    cmd({"viewsource",  "vs" },         function (w)       w:toggle_source(true) end),
    cmd({"viewsource!", "vs!"},         function (w)       w:toggle_source() end),
    cmd({"bookmark",    "bm" },         function (w, a)
                                            local args = lousy.util.string.split(a)
                                            local uri = table.remove(args, 1)
                                            bookmarks.add(uri, args)
                                        end),
}

-- Helper functions which are added to the window struct
binds.helper_methods = {
    -- Navigate current view or open new tab
    navigate = function (w, uri, view)
        if not view then view = w:get_current() end
        if view then
            view.uri = uri
        else
            return w:new_tab(uri)
        end
    end,

    -- search engine wrapper
    websearch = function (w, args)
        local sep = string.find(args, " ")
        local engine = string.sub(args, 1, sep-1)
        local search = string.sub(args, sep+1)
        search = string.gsub(search, "^%s*(.-)%s*$", "%1")
        if not search_engines[engine] then
            return error("No matching search engine found: " .. engine)
        end
        local uri = string.gsub(search_engines[engine], "{%d}", search)
        return w:navigate(uri)
    end,

    -- Tab traversing functions
    next_tab = function (w, n)
        w.tabs:switch((((n or 1) + w.tabs:current() -1) % w.tabs:count()) + 1)
    end,

    prev_tab = function (w, n)
        w.tabs:switch(((w.tabs:current() - (n or 1) -1) % w.tabs:count()) + 1)
    end,

    goto_tab = function (w, n)
        w.tabs:switch(n)
    end,

    -- If argument is form-active or root-active, emits signal. Ignores all
    -- other signals.
    emit_form_root_active_signal = function (w, s)
        if s == "form-active" then
            w:get_current():emit_signal("form-active")
        elseif s == "root-active" then
            w:get_current():emit_signal("root-active")
        end
    end,
}

-- Insert webview method lookup on window structure
table.insert(window.indexes, 1, function (w, k)
    -- Lookup bind helper method
    return binds.helper_methods[k]
end)

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

mercurial