public
Last active

Sub-100 line browser using luakit

  • Download Gist
gistfile1.lua
Lua
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
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.