#!/usr/bin/luakit -c | |
-------------------------------------------------------------------- | |
-- 2011 (C) Mason Larobina <mason.larobina@gmail.com> -- | |
-- Browser in under 128 lines of code? (my attempt) -- | |
-- See: http://lateral.netmanagers.com.ar/weblog/posts/BB948.html -- | |
-------------------------------------------------------------------- | |
local homepage = "http://luakit.org/" | |
-- Create the browser window | |
local win, vbox = widget{type="window"}, widget{type="vbox"} | |
local urlbar, view = widget{type="entry"}, widget{type="webview"} | |
vbox:pack_start(view, true, true, 0) | |
vbox:pack_start(urlbar, false, false, 0) | |
win:set_child(vbox) | |
win:show() | |
view:focus() | |
-- Quit on window destroy | |
win:add_signal("destroy", luakit.quit) | |
-- On webview navigate update the address bar | |
view:add_signal("property::uri", function () urlbar.text = view.uri end) | |
function smart_go(uri) | |
if uri == "about:blank" or string.match(uri, "^https?://") | |
then view.uri = uri else view.uri = "http://google.com/search?q="..uri end | |
end | |
-- Navigate webview to url in address bar on Return press | |
urlbar:add_signal("activate", function () smart_go(urlbar.text) end) | |
urlbar:add_signal("key-press", function (_, _, key) | |
if key == "Escape" then view:focus() return true end end) | |
function has(table, item) | |
for _, v in ipairs(table) do | |
if v == item then return true end | |
end | |
end | |
function scroll(vert, value) | |
local c,m=(vert and view.get_scroll_vert or view.get_scroll_horiz)(view) | |
if type(value) == "string" then value = c + value -- relative | |
elseif value == -1 then value = m end -- bottom of page | |
(vert and view.set_scroll_vert or view.set_scroll_horiz)(view, value) | |
end | |
function zoom(value) | |
local zoom = view:get_property("zoom-level") | |
if type(value) == "string" then value = zoom + value end | |
view:set_property("zoom-level", value) | |
end | |
-- Set initial mode (this is going to be a modal browser) | |
local mode = "normal" | |
-- Hardcoded bindings | |
view:add_signal("key-press", function (_, mods, key) | |
-- Auto remove Shift | |
local Control, Alt, Shift = has(mods, "Control"), has(mods, "Mod1"), false | |
if has(mods, "Shift") then | |
if #key == 1 then key = string.upper(key) else Shift = true end | |
end | |
-- Return to normal mode on escape | |
if key == "Escape" or (Control and key == "[") then | |
mode = "normal" | |
return true | |
end | |
-- Capture key presses in insert mode | |
if mode == "insert" then return false end | |
if mode == "normal" and not Control and not Shift and not Alt then | |
if key == "h" then scroll(false, "-40") | |
elseif key == "j" then scroll(true, "+40") | |
elseif key == "k" then scroll(true, "-40") | |
elseif key == "l" then scroll(false, "+40") | |
elseif key == "G" then scroll(true, -1) | |
elseif key == "g" then mode = "g" -- Extended go | |
elseif key == "0" then scroll(false, 0) | |
elseif key == "$" then scroll(false, -1) | |
elseif key == "r" then view:reload() | |
elseif key == "p" then smart_go(luakit.get_selection() or "about:blank") | |
elseif key == "H" then view:go_back(1) | |
elseif key == "L" then view:go_forward(1) | |
elseif key == "z" then mode = "z" -- Zoom | |
elseif key == "d" or key == "D" then luakit.quit() | |
elseif key == "o" then urlbar:focus() | |
end | |
elseif mode == "normal" and Control and not Shift and not Alt then | |
if key == "c" then view:stop() end | |
elseif mode == "g" and not Control and not Shift and not Alt then | |
if key == "h" then view.uri = homepage | |
elseif key == "g" then scroll(true, 0) | |
end | |
mode = "" -- ignore further keys | |
elseif mode == "z" and not Control and not Shift and not Alt then | |
if key == "i" then zoom("+0.1") | |
elseif key == "o" then zoom("-0.1") | |
elseif key == "z" then zoom(1.0) | |
end | |
mode = "" -- ignore further keys | |
end | |
return true | |
end) | |
-- Go to homepage | |
view.uri = homepage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment