Merge branch 'domain-settings' into develop
[luakit.git] / rc.lua
blob58fd0664bfdf054a5169eeb082422a27f78df18d
1 -- Luakit configuration file, more information at http://luakit.org/
3 require("math")
4 require("mode")
5 require("bind")
7 -- Widget construction aliases
8 function eventbox() return widget{type="eventbox"} end
9 function hbox() return widget{type="hbox"} end
10 function label() return widget{type="label"} end
11 function notebook() return widget{type="notebook"} end
12 function vbox() return widget{type="vbox"} end
13 function webview() return widget{type="webview"} end
14 function window() return widget{type="window"} end
15 function entry() return widget{type="entry"} end
18 -- Variable definitions
19 HOMEPAGE = "http://luakit.org/"
20 --HOMEPAGE = "http://github.com/mason-larobina/luakit"
21 SCROLL_STEP = 20
22 MAX_CMD_HISTORY = 100
23 MAX_SRCH_HISTORY = 100
24 --HTTPPROXY = "http://example.com:3128"
26 -- Setup download directory
27 DOWNLOAD_DIR = luakit.get_special_dir("DOWNLOAD") or (os.getenv("HOME") .. "/downloads")
29 -- Per-domain webview properties
30 domain_props = { --[[
31 ["all"] = {
32 ["enable-scripts"] = false,
33 ["enable-plugins"] = false,
34 ["enable-private-browsing"] = false,
35 ["user-stylesheet-uri"] = "",
37 ["youtube.com"] = {
38 ["enable-scripts"] = true,
39 ["enable-plugins"] = true,
41 ["forums.archlinux.org"] = {
42 ["user-stylesheet-uri"] = luakit.data_home .. "/styles/dark.css",
43 ["enable-private-browsing"] = true,
44 }, ]]--
47 -- Luakit theme
48 theme = theme or {
49 -- Default settings
50 font = "monospace normal 9",
51 fg = "#fff",
52 bg = "#000",
54 -- General settings
55 statusbar_fg = "#fff",
56 statusbar_bg = "#000",
57 inputbar_fg = "#000",
58 inputbar_bg = "#fff",
60 -- Specific settings
61 loaded_fg = "#33AADD",
62 tablabel_fg = "#999",
63 tablabel_bg = "#111",
64 selected_tablabel_fg = "#fff",
65 selected_tablabel_bg = "#000",
67 -- Enforce a minimum tab width of 30 characters to prevent longer tab
68 -- titles overshadowing small tab titles when things get crowded.
69 tablabel_format = "%-30s",
72 -- Small util functions
73 function info(...) if luakit.verbose then print(string.format(...)) end end
75 widget.add_signal("new", function (wi)
76 wi:add_signal("init", function (wi)
77 if wi.type == "window" then
78 wi:add_signal("destroy", function ()
79 -- Call the quit function if this was the last window left
80 if #luakit.windows == 0 then luakit.quit() end
81 end)
82 end
83 end)
84 end)
86 -- Search engines
87 search_engines = {
88 debbugs = "http://bugs.debian.org/{0}",
89 google = "http://google.com/search?q={0}",
90 imdb = "http://imdb.com/find?s=all&q={0}",
91 sourceforge = "http://sf.net/search/?words={0}",
94 -- Add key bindings to be used across all windows
95 mode_binds = {
96 -- bind.buf(Pattern, function (w, buffer, opts) .. end, opts),
97 -- bind.key({Modifiers}, Key name, function (w, opts) .. end, opts),
98 -- bind.but({Modifiers}, Button num, function (w, opts) .. end, opts),
99 all = {
100 bind.key({}, "Escape", function (w) w:set_mode() end),
101 bind.key({"Control"}, "[", function (w) w:set_mode() end),
103 -- Mouse bindings
104 bind.but({}, 2, function (w)
105 -- Open hovered uri in new tab
106 local uri = w:get_current().hovered_uri
107 if uri then w:new_tab(uri)
108 else -- Open selection in current tab
109 uri = luakit.get_selection()
110 if uri then w:get_current().uri = uri end
112 end),
113 bind.but({}, 8, function (w) w:back() end),
114 bind.but({}, 9, function (w) w:forward() end),
116 normal = {
117 bind.key({}, "i", function (w) w:set_mode("insert") end),
118 bind.key({}, ":", function (w) w:set_mode("command") end),
120 -- Scrolling
121 bind.key({}, "h", function (w) w:scroll_horiz("-"..SCROLL_STEP.."px") end),
122 bind.key({}, "j", function (w) w:scroll_vert ("+"..SCROLL_STEP.."px") end),
123 bind.key({}, "k", function (w) w:scroll_vert ("-"..SCROLL_STEP.."px") end),
124 bind.key({}, "l", function (w) w:scroll_horiz("+"..SCROLL_STEP.."px") end),
125 bind.key({}, "Left", function (w) w:scroll_horiz("-"..SCROLL_STEP.."px") end),
126 bind.key({}, "Down", function (w) w:scroll_vert ("+"..SCROLL_STEP.."px") end),
127 bind.key({}, "Up", function (w) w:scroll_vert ("-"..SCROLL_STEP.."px") end),
128 bind.key({}, "Right", function (w) w:scroll_horiz("+"..SCROLL_STEP.."px") end),
129 bind.key({"Control"}, "d", function (w) w:scroll_page(0.5) end),
130 bind.key({"Control"}, "u", function (w) w:scroll_page(-0.5) end),
131 bind.key({"Control"}, "f", function (w) w:scroll_page(1.0) end),
132 bind.key({"Control"}, "b", function (w) w:scroll_page(-1.0) end),
133 bind.buf("^gg$", function (w) w:scroll_vert("0%") end),
134 bind.buf("^G$", function (w) w:scroll_vert("100%") end),
135 bind.buf("^[\-\+]?[0-9]+[%%G]$", function (w, b) w:scroll_vert(string.match(b, "^([\-\+]?%d+)[%%G]$") .. "%") end),
137 -- Clipboard
138 bind.key({}, "p", function (w) w:navigate(luakit.get_selection()) end),
139 bind.key({}, "P", function (w) w:new_tab(luakit.get_selection()) end),
140 bind.buf("^yy$", function (w) luakit.set_selection(w:get_current().uri) end),
141 bind.buf("^yt$", function (w) luakit.set_selection(w.win.title) end),
143 -- Commands
144 bind.buf("^o$", function (w, c) w:enter_cmd(":open ") end),
145 bind.buf("^t$", function (w, c) w:enter_cmd(":tabopen ") end),
146 bind.buf("^,g$", function (w, c) w:enter_cmd(":websearch google ") end),
148 -- Searching
149 bind.key({}, "/", function (w) w:start_search(true) end),
150 bind.key({}, "?", function (w) w:start_search(false) end),
151 bind.key({}, "n", function (w) w:search(nil, true) end),
152 bind.key({}, "N", function (w) w:search(nil, false) end),
154 -- History
155 bind.buf("^[0-9]*H$", function (w, b) w:back (tonumber(string.match(b, "^(%d*)H$") or 1)) end),
156 bind.buf("^[0-9]*L$", function (w, b) w:forward(tonumber(string.match(b, "^(%d*)L$") or 1)) end),
158 -- Tab
159 bind.buf("^[0-9]*gT$", function (w, b) w:prev_tab(tonumber(string.match(b, "^(%d*)gT$") or 1)) end),
160 bind.buf("^[0-9]*gt$", function (w, b) w:next_tab(tonumber(string.match(b, "^(%d*)gt$") or 1)) end),
161 bind.buf("^gH$", function (w) w:new_tab(HOMEPAGE) end),
162 bind.buf("^d$", function (w) w:close_tab() end),
164 bind.key({}, "r", function (w) w:reload() end),
165 bind.buf("^gh$", function (w) w:navigate(HOMEPAGE) end),
166 bind.buf("^ZZ$", function (w) luakit.quit() end),
168 -- Link following
169 bind.key({}, "f", function (w) w:set_mode("follow") end),
172 command = {
173 bind.key({"Shift"}, "Insert", function (w) w:insert_cmd(luakit.get_selection()) end),
174 bind.key({}, "Up", function (w) w:cmd_hist_prev() end),
175 bind.key({}, "Down", function (w) w:cmd_hist_next() end),
176 bind.key({}, "Tab", function (w) w:cmd_completion() end),
177 bind.key({"Control"}, "w", function (w) w:del_word() end),
178 bind.key({"Control"}, "u", function (w) w:del_line() end),
180 search = {
181 bind.key({}, "Up", function (w) w:srch_hist_prev() end),
182 bind.key({}, "Down", function (w) w:srch_hist_next() end),
184 insert = { },
187 -- Commands
188 commands = {
189 -- bind.cmd({Command, Alias1, ...}, function (w, arg, opts) .. end, opts),
190 bind.cmd({"open", "o" }, function (w, a) w:navigate(a) end),
191 bind.cmd({"tabopen", "t" }, function (w, a) w:new_tab(a) end),
192 bind.cmd({"back" }, function (w, a) w:back(tonumber(a) or 1) end),
193 bind.cmd({"forward", "f" }, function (w, a) w:forward(tonumber(a) or 1) end),
194 bind.cmd({"scroll" }, function (w, a) w:scroll_vert(a) end),
195 bind.cmd({"quit", "q" }, function (w) luakit.quit() end),
196 bind.cmd({"close", "c" }, function (w) w:close_tab() end),
197 bind.cmd({"websearch", "ws" }, function (w, e, s) w:websearch(e, s) end),
198 bind.cmd({"reload", }, function (w) w:reload() end),
199 bind.cmd({"viewsource", "vs" }, function (w) w:toggle_source(true) end),
200 bind.cmd({"viewsource!", "vs!"}, function (w) w:toggle_source() end),
203 function set_http_options(w)
204 local proxy = HTTPPROXY or os.getenv("http_proxy")
205 if proxy then w:set('proxy-uri', proxy) end
206 w:set('user-agent', 'luakit')
207 -- Uncomment the following options if you want to enable SSL certs validation.
208 -- w:set('ssl-ca-file', '/etc/certs/ca-certificates.crt')
209 -- w:set('ssl-strict', true)
212 -- Build and pack window widgets
213 function build_window()
214 -- Create a table for widgets and state variables for a window
215 local w = {
216 win = window(),
217 ebox = eventbox(),
218 layout = vbox(),
219 tabs = notebook(),
220 -- Tab bar widgets
221 tbar = {
222 layout = hbox(),
223 ebox = eventbox(),
224 titles = { },
226 -- Status bar widgets
227 sbar = {
228 layout = hbox(),
229 ebox = eventbox(),
230 -- Left aligned widgets
231 l = {
232 layout = hbox(),
233 ebox = eventbox(),
234 uri = label(),
235 loaded = label(),
237 -- Fills space between the left and right aligned widgets
238 filler = label(),
239 -- Right aligned widgets
240 r = {
241 layout = hbox(),
242 ebox = eventbox(),
243 buf = label(),
244 tabi = label(),
245 scroll = label(),
248 -- Input bar widgets
249 ibar = {
250 layout = hbox(),
251 ebox = eventbox(),
252 prompt = label(),
253 input = entry(),
257 -- Assemble window
258 w.ebox:set_child(w.layout)
259 w.win:set_child(w.ebox)
261 -- Pack tab bar
262 local t = w.tbar
263 t.ebox:set_child(t.layout, false, false, 0)
264 w.layout:pack_start(t.ebox, false, false, 0)
266 -- Pack notebook
267 w.layout:pack_start(w.tabs, true, true, 0)
269 -- Pack left-aligned statusbar elements
270 local l = w.sbar.l
271 l.layout:pack_start(l.uri, false, false, 0)
272 l.layout:pack_start(l.loaded, false, false, 0)
273 l.ebox:set_child(l.layout)
275 -- Pack right-aligned statusbar elements
276 local r = w.sbar.r
277 r.layout:pack_start(r.buf, false, false, 0)
278 r.layout:pack_start(r.tabi, false, false, 0)
279 r.layout:pack_start(r.scroll, false, false, 0)
280 r.ebox:set_child(r.layout)
282 -- Pack status bar elements
283 local s = w.sbar
284 s.layout:pack_start(l.ebox, false, false, 0)
285 s.layout:pack_start(s.filler, true, true, 0)
286 s.layout:pack_start(r.ebox, false, false, 0)
287 s.ebox:set_child(s.layout)
288 w.layout:pack_start(s.ebox, false, false, 0)
290 -- Pack input bar
291 local i = w.ibar
292 i.layout:pack_start(i.prompt, false, false, 0)
293 i.layout:pack_start(i.input, true, true, 0)
294 i.ebox:set_child(i.layout)
295 w.layout:pack_start(i.ebox, false, false, 0)
297 -- Other settings
298 i.input.show_frame = false
299 w.tabs.show_tabs = false
300 l.loaded:hide()
301 l.uri.selectable = true
303 return w
306 function attach_window_signals(w)
307 -- Attach notebook widget signals
308 w.tabs:add_signal("page-added", function (nbook, view, idx)
309 w:update_tab_count(idx)
310 w:update_tab_labels()
311 end)
313 w.tabs:add_signal("switch-page", function (nbook, view, idx)
314 w:update_tab_count(idx)
315 w:update_win_title(view)
316 w:update_uri(view)
317 w:update_progress(view)
318 w:update_tab_labels(idx)
319 end)
321 -- Attach window widget signals
322 w.win:add_signal("key-press", function (win, mods, key)
323 -- Reset command line completion
324 if w:get_mode() == "command" and key ~= "Tab" and w.compl_start then
325 w:update_uri()
326 w.compl_index = 0
329 if w:hit(mods, key) then
330 return true
332 end)
334 w.win:add_signal("mode-changed", function (win, mode)
335 local i, p = w.ibar.input, w.ibar.prompt
337 w:update_binds(mode)
338 w.cmd_hist_cursor = nil
340 -- Clear following hints if the user exits follow mode
341 if w.showing_hints then
342 w:eval_js("clear();");
343 w.showing_hints = false
346 -- If a user aborts a search return to the original position
347 if w.search_start_marker then
348 w:get_current():set_scroll_vert(w.search_start_marker)
349 w.search_start_marker = nil
352 if mode == "normal" then
353 p:hide()
354 i:hide()
355 elseif mode == "insert" then
356 i:hide()
357 i.text = ""
358 p.text = "-- INSERT --"
359 p:show()
360 elseif mode == "command" then
361 p:hide()
362 i.text = ":"
363 i:show()
364 i:focus()
365 i:set_position(-1)
366 elseif mode == "search" then
367 p:hide()
368 i:show()
369 elseif mode == "follow" then
370 w:eval_js_from_file(util.find_data("scripts/follow.js"))
371 w:eval_js("clear(); show_hints();")
372 w.showing_hints = true
373 p.text = "Follow:"
374 p:show()
375 i.text = ""
376 i:show()
377 i:focus()
378 i:set_position(-1)
379 else
380 w.ibar.prompt.text = ""
381 w.ibar.input.text = ""
383 end)
385 -- Attach inputbar widget signals
386 w.ibar.input:add_signal("changed", function()
387 local text = w.ibar.input.text
388 -- Auto-exit "command" mode if you backspace or delete the ":"
389 -- character at the start of the input box when in "command" mode.
390 if w:is_mode("command") and not string.match(text, "^:") then
391 w:set_mode()
392 elseif w:is_mode("search") then
393 if string.match(text, "^[\?\/]") then
394 w:search(string.sub(text, 2), (string.sub(text, 1, 1) == "/"))
395 else
396 w:clear_search()
397 w:set_mode()
399 elseif w:is_mode("follow") then
400 w:eval_js(string.format("update(%q)", w.ibar.input.text))
402 end)
404 w.ibar.input:add_signal("activate", function()
405 local text = w.ibar.input.text
406 if w:is_mode("command") then
407 w:cmd_hist_add(text)
408 w:match_cmd(string.sub(text, 2))
409 w:set_mode()
410 elseif w:is_mode("search") then
411 w:srch_hist_add(text)
412 w:search(string.sub(text, 2), string.sub(text, 1, 1) == "/")
413 -- User doesn't want to return to start position
414 w.search_start_marker = nil
415 w:set_mode()
416 w.ibar.prompt.text = util.escape(text)
417 w.ibar.prompt:show()
419 end)
422 -- Attach signal handlers to a new tab's webview
423 function attach_webview_signals(w, view)
424 view:add_signal("property::title", function (v)
425 w:update_tab_labels()
426 if w:is_current(v) then
427 w:update_win_title(v)
429 end)
431 view:add_signal("property::uri", function (v)
432 w:update_tab_labels()
433 if w:is_current(v) then
434 w:update_uri(v)
436 end)
438 view:add_signal("link-hover", function (v, link)
439 if w:is_current(v) and link then
440 w.sbar.l.uri.text = "Link: " .. util.escape(link)
442 end)
444 view:add_signal("link-unhover", function (v)
445 if w:is_current(v) then
446 w:update_uri(v)
448 end)
450 view:add_signal("form-active", function ()
451 w:set_mode("insert")
452 end)
454 view:add_signal("root-active", function ()
455 w:set_mode()
456 end)
458 view:add_signal("key-press", function ()
459 -- Only allow key press events to hit the webview if the user is in
460 -- "insert" mode.
461 if not w:is_mode("insert") then
462 return true
464 end)
466 view:add_signal("button-release", function (v, mods, button)
467 if w:hit(mods, button) then
468 return true
470 end)
472 -- Update progress widgets & set default mode on navigate
473 view:add_signal("load-status", function (v, status)
474 if w:is_current(v) then
475 w:update_progress(v)
476 if status == "provisional" then
477 w:set_mode()
480 end)
482 -- Domain properties
483 view:add_signal("load-status", function (v, status)
484 if status == "committed" then
485 local domain = string.match(v.uri, "^%a+://([^/]*)/?")
486 if string.match(domain, "^www.") then domain = string.sub(domain, 5) end
487 local props = util.table.join(domain_props.all or {}, domain_props[domain] or {})
488 for k, v in pairs(props) do
489 info("Domain prop: %s = %s (%s)", k, tostring(v), domain)
490 view:set_prop(k, v)
493 end)
495 -- 'link' contains the download link
496 -- 'mime' contains the mime type that is requested
497 -- return TRUE to accept or FALSE to reject
498 view:add_signal("mime-type-decision", function (v, link, mime)
499 info("Requested link: %s (%s)", link, mime)
500 -- i.e. block binary files like *.exe
501 --if mime == "application/octet-stream" then
502 -- return false
503 --end
504 end)
506 -- 'link' contains the download link
507 -- 'filename' contains the suggested filename (from server or webkit)
508 view:add_signal("download-request", function (v, link, filename)
509 if not filename then return end
510 -- Make download dir
511 os.execute(string.format("mkdir -p %q", DOWNLOAD_DIR))
512 local dl = DOWNLOAD_DIR .. "/" .. filename
513 local wget = string.format("wget -q %q -O %q", link, dl)
514 info("Launching: %s", wget)
515 luakit.spawn(wget)
516 end)
518 -- 'link' contains the download link
519 -- 'reason' contains the reason of the request (i.e. "link-clicked")
520 -- return TRUE to handle the request by yourself or FALSE to proceed
521 -- with default behaviour
522 view:add_signal("new-window-decision", function (v, link, reason)
523 info("New window decision: %s (%s)", link, reason)
524 if reason == "link-clicked" then
525 new_window({ link })
526 return true
528 w:new_tab(link)
529 end)
531 view:add_signal("property::progress", function (v)
532 if w:is_current(v) then
533 w:update_progress(v)
535 end)
537 view:add_signal("expose", function (v)
538 if w:is_current(v) then
539 w:update_scroll(v)
541 end)
544 -- Parses scroll amounts of the form:
545 -- Relative: "+20%", "-20%", "+20px", "-20px"
546 -- Absolute: 20, "20%", "20px"
547 -- And returns an absolute value.
548 function parse_scroll(current, max, value)
549 if string.match(value, "^%d+px$") then
550 return tonumber(string.match(value, "^(%d+)px$"))
551 elseif string.match(value, "^%d+%%$") then
552 return math.ceil(max * (tonumber(string.match(value, "^(%d+)%%$")) / 100))
553 elseif string.match(value, "^[\-\+]%d+px") then
554 return current + tonumber(string.match(value, "^([\-\+]%d+)px"))
555 elseif string.match(value, "^[\-\+]%d+%%$") then
556 return math.ceil(current + (max * (tonumber(string.match(value, "^([\-\+]%d+)%%$")) / 100)))
557 else
558 print("E: unable to parse scroll amount:", value)
562 -- Helper functions which operate on a windows widget structure
563 window_helpers = {
564 -- Return the widget in the currently active tab
565 get_current = function (w) return w.tabs:atindex(w.tabs:current()) end,
566 -- Check if given widget is the widget in the currently active tab
567 is_current = function (w, wi) return w.tabs:indexof(wi) == w.tabs:current() end,
569 -- Wrappers around the mode plugin
570 set_mode = function (w, name) mode.set(w.win, name) end,
571 get_mode = function (w) return mode.get(w.win) end,
572 is_mode = function (w, name) return name == w:get_mode() end,
573 is_any_mode = function (w, t, name) return util.table.hasitem(t, name or w:get_mode()) end,
575 -- Wrappers around the view:get_prop & view:set_prop methods
576 get = function (w, prop, view)
577 if not view then view = w:get_current() end
578 return view:get_prop(prop)
579 end,
581 set = function (w, prop, val, view)
582 if not view then view = w:get_current() end
583 view:set_prop(prop, val)
584 end,
586 get_tab_title = function (w, view)
587 if not view then view = w:get_current() end
588 return view:get_prop("title") or view.uri or "(Untitled)"
589 end,
591 navigate = function (w, uri, view)
592 local v = view or w:get_current()
593 if v then
594 v.uri = uri
595 else
596 return w:new_tab(uri)
598 end,
600 reload = function (w, view)
601 if not view then view = w:get_current() end
602 view:reload()
603 end,
605 new_tab = function (w, uri)
606 local view = webview()
607 w.tabs:append(view)
608 set_http_options(w)
609 attach_webview_signals(w, view)
610 if uri then view.uri = uri end
611 view.show_scrollbars = false
612 w:update_tab_count()
613 end,
615 -- close the current tab
616 close_tab = function (w, view)
617 if not view then view = w:get_current() end
618 if not view then return end
619 w.tabs:remove(view)
620 view:destroy()
621 w:update_tab_count()
622 end,
624 -- evaluate javascript code and return string result
625 eval_js = function (w, script, file, view)
626 if not view then view = w:get_current() end
627 return view:eval_js(script, file or "(buffer)")
628 end,
630 -- evaluate javascript code from file and return string result
631 eval_js_from_file = function (w, file, view)
632 local fh, err = io.open(file)
633 if not fh then return error(err) end
634 local script = fh:read("*a")
635 fh:close()
636 return w:eval_js(script, file, view)
637 end,
639 -- Wrapper around the bind plugin's hit method
640 hit = function (w, mods, key)
641 local caught, newbuf = bind.hit(w.binds or {}, mods, key, w.buffer, w:is_mode("normal"), w)
642 w.buffer = newbuf
643 w:update_buf()
644 return caught
645 end,
647 -- Wrapper around the bind plugin's match_cmd method
648 match_cmd = function (w, buffer)
649 return bind.match_cmd(commands, buffer, w)
650 end,
652 -- Toggle source view
653 toggle_source = function (w, show, view)
654 if not view then view = w:get_current() end
655 if show == nil then show = not view:get_view_source() end
656 view:set_view_source(show)
657 end,
659 -- enter command or characters into command line
660 enter_cmd = function (w, cmd)
661 local i = w.ibar.input
662 w:set_mode("command")
663 i.text = cmd
664 i:set_position(-1)
665 end,
667 -- insert a string into the command line at the current cursor position
668 insert_cmd = function (w, str)
669 if not str then return nil end
670 local i = w.ibar.input
671 local text = i.text
672 local pos = i:get_position()
673 local left, right = string.sub(text, 1, pos), string.sub(text, pos+1)
674 i.text = left .. str .. right
675 i:set_position(pos + #str + 1)
676 end,
678 -- search engine wrapper
679 websearch = function (w, args)
680 local sep = string.find(args, " ")
681 local engine = string.sub(args, 1, sep-1)
682 local search = string.sub(args, sep+1)
683 if not search_engines[engine] then
684 print("E: No matching search engine found:", engine)
685 return 0
687 local uri = string.gsub(search_engines[engine], "{%d}", search)
688 return w:navigate(uri)
689 end,
691 -- Command line completion of available commands
692 cmd_completion = function (w)
693 local i = w.ibar.input
694 local s = w.sbar.l.uri
695 local cmpl = {}
697 -- Get last completion (is reset on key press other than <Tab>)
698 if not w.compl_start or w.compl_index == 0 then
699 w.compl_start = "^" .. string.sub(i.text, 2)
700 w.compl_index = 1
703 -- Get suitable commands
704 for _, b in ipairs(commands) do
705 for _, c in pairs(b.commands) do
706 if c and string.match(c, w.compl_start) then
707 table.insert(cmpl, c)
712 table.sort(cmpl)
714 if #cmpl > 0 then
715 local text = ""
716 for index, comp in pairs(cmpl) do
717 if index == w.compl_index then
718 i.text = ":" .. comp .. " "
719 i:set_position(-1)
721 if text ~= "" then
722 text = text .. " | "
724 text = text .. comp
727 -- cycle through all possible completions
728 if w.compl_index == #cmpl then
729 w.compl_index = 1
730 else
731 w.compl_index = w.compl_index + 1
733 s.text = util.escape(text)
735 end,
737 del_word = function (w)
738 local i = w.ibar.input
739 local text = i.text
740 local pos = i:get_position()
741 if text and #text > 1 and pos > 1 then
742 local left, right = string.sub(text, 2, pos), string.sub(text, pos+1)
743 if not string.find(left, "%s") then
744 left = ""
745 elseif string.find(left, "%w+%s*$") then
746 left = string.sub(left, 0, string.find(left, "%w+%s*$") - 1)
747 elseif string.find(left, "%W+%s*$") then
748 left = string.sub(left, 0, string.find(left, "%W+%s*$") - 1)
750 i.text = string.sub(text, 1, 1) .. left .. right
751 i:set_position(#left + 2)
753 end,
755 del_line = function (w)
756 local i = w.ibar.input
757 if i.text ~= ":" then
758 i.text = ":"
759 i:set_position(-1)
761 end,
763 -- Search history adding
764 srch_hist_add = function (w, srch)
765 if not w.srch_hist then w.srch_hist = {} end
766 -- Check overflow
767 if #w.srch_hist > ((MAX_SRCH_HISTORY or 100) + 5) then
768 while #w.srch_hist > (MAX_SRCH_HISTORY or 100) do
769 table.remove(w.srch_hist, 1)
772 table.insert(w.srch_hist, srch)
773 end,
775 -- Search history traversing
776 srch_hist_prev = function (w)
777 if not w.srch_hist then w.srch_hist = {} end
778 if not w.srch_hist_cursor then
779 w.srch_hist_cursor = #w.srch_hist + 1
780 w.srch_hist_current = w.ibar.input.text
782 local c = w.srch_hist_cursor - 1
783 if w.srch_hist[c] then
784 w.srch_hist_cursor = c
785 w.ibar.input.text = w.srch_hist[c]
786 w.ibar.input:set_position(-1)
788 end,
790 srch_hist_next = function (w)
791 if not w.srch_hist then w.srch_hist = {} end
792 local c = (w.srch_hist_cursor or #w.srch_hist) + 1
793 if w.srch_hist[c] then
794 w.srch_hist_cursor = c
795 w.ibar.input.text = w.srch_hist[c]
796 w.ibar.input:set_position(-1)
797 elseif w.srch_hist_current then
798 w.srch_hist_cursor = nil
799 w.ibar.input.text = w.srch_hist_current
800 w.ibar.input:set_position(-1)
802 end,
804 -- Command history adding
805 cmd_hist_add = function (w, cmd)
806 if not w.cmd_hist then w.cmd_hist = {} end
807 -- Make sure history doesn't overflow
808 if #w.cmd_hist > ((MAX_CMD_HISTORY or 100) + 5) then
809 while #w.cmd_hist > (MAX_CMD_HISTORY or 100) do
810 table.remove(w.cmd_hist, 1)
813 table.insert(w.cmd_hist, cmd)
814 end,
816 -- Command history traversing
817 cmd_hist_prev = function (w)
818 if not w.cmd_hist then w.cmd_hist = {} end
819 if not w.cmd_hist_cursor then
820 w.cmd_hist_cursor = #w.cmd_hist + 1
821 w.cmd_hist_current = w.ibar.input.text
823 local c = w.cmd_hist_cursor - 1
824 if w.cmd_hist[c] then
825 w.cmd_hist_cursor = c
826 w.ibar.input.text = w.cmd_hist[c]
827 w.ibar.input:set_position(-1)
829 end,
831 cmd_hist_next = function (w)
832 if not w.cmd_hist then w.cmd_hist = {} end
833 local c = (w.cmd_hist_cursor or #w.cmd_hist) + 1
834 if w.cmd_hist[c] then
835 w.cmd_hist_cursor = c
836 w.ibar.input.text = w.cmd_hist[c]
837 w.ibar.input:set_position(-1)
838 elseif w.cmd_hist_current then
839 w.cmd_hist_cursor = nil
840 w.ibar.input.text = w.cmd_hist_current
841 w.ibar.input:set_position(-1)
843 end,
845 -- Searching functions
846 start_search = function (w, forward)
847 -- Clear previous search results
848 w:clear_search()
849 w:set_mode("search")
850 local i = w.ibar.input
851 if forward then
852 i.text = "/"
853 else
854 i.text = "?"
856 i:focus()
857 i:set_position(-1)
858 end,
860 search = function (w, text, forward)
861 local view = w:get_current()
862 local text = text or w.last_search
863 if forward == nil then forward = true end
864 local case_sensitive = false
865 local wrap = true
867 if not text or #text == 0 then
868 w:clear_search()
869 return nil
872 w.last_search = text
873 if w.searching_forward == nil then
874 w.searching_forward = forward
875 w.search_start_marker = view:get_scroll_vert()
876 else
877 -- Invert the direction if originally searching in reverse
878 forward = (w.searching_forward == forward)
881 view:search(text, case_sensitive, forward, wrap);
882 end,
884 clear_search = function (w)
885 w:get_current():clear_search()
886 -- Clear search state
887 w.last_search = nil
888 w.searching_forward = nil
889 w.search_start_marker = nil
890 end,
892 -- Webview scroll functions
893 scroll_vert = function (w, value, view)
894 if not view then view = w:get_current() end
895 local cur, max = view:get_scroll_vert()
896 if type(value) == "string" then
897 value = parse_scroll(cur, max, value)
899 view:set_scroll_vert(value)
900 end,
902 scroll_horiz = function (w, value, view)
903 if not view then view = w:get_current() end
904 local cur, max = view:get_scroll_horiz()
905 if type(value) == "string" then
906 value = parse_scroll(cur, max, value)
908 view:set_scroll_horiz(value)
909 end,
911 -- vertical scroll of a multiple of the view_size
912 scroll_page = function (w, value, view)
913 if not view then view = w:get_current() end
914 local cur, max, size = view:get_scroll_vert()
915 view:set_scroll_vert(cur + size * value)
916 end,
918 -- Tab traversing functions
919 next_tab = function (w, n)
920 w.tabs:switch((((n or 1) + w.tabs:current() -1) % w.tabs:count()) + 1)
921 end,
922 prev_tab = function (w, n)
923 w.tabs:switch(((w.tabs:current() - (n or 1) -1) % w.tabs:count()) + 1)
924 end,
925 goto_tab = function (w, n)
926 w.tabs:switch(n)
927 end,
929 -- History traversing functions
930 back = function (w, n, view)
931 (view or w:get_current()):go_back(n or 1)
932 end,
933 forward = function (w, n, view)
934 (view or w:get_current()):go_forward(n or 1)
935 end,
937 -- GUI content update functions
938 update_tab_count = function (w, i, t)
939 w.sbar.r.tabi.text = string.format("[%d/%d]", i or w.tabs:current(), t or w.tabs:count())
940 end,
942 update_win_title = function (w, view)
943 if not view then view = w:get_current() end
944 local title = view:get_prop("title")
945 local uri = view.uri
946 if not title and not uri then
947 w.win.title = "luakit"
948 else
949 w.win.title = (title or "luakit") .. " - " .. (uri or "about:blank")
951 end,
953 update_uri = function (w, view, uri)
954 if not view then view = w:get_current() end
955 w.sbar.l.uri.text = util.escape((uri or (view and view.uri) or "about:blank"))
956 end,
958 update_progress = function (w, view, p)
959 if not view then view = w:get_current() end
960 if not p then p = view:get_prop("progress") end
961 if not view:loading() or p == 1 then
962 w.sbar.l.loaded:hide()
963 else
964 w.sbar.l.loaded:show()
965 w.sbar.l.loaded.text = string.format("(%d%%)", p * 100)
967 end,
969 update_scroll = function (w, view)
970 if not view then view = w:get_current() end
971 local val, max = view:get_scroll_vert()
972 if max == 0 then val = "All"
973 elseif val == 0 then val = "Top"
974 elseif val == max then val = "Bot"
975 else val = string.format("%2d%%", (val/max) * 100)
977 w.sbar.r.scroll.text = val
978 end,
980 update_buf = function (w)
981 if w.buffer then
982 w.sbar.r.buf.text = util.escape(string.format(" %-3s", w.buffer))
983 w.sbar.r.buf:show()
984 else
985 w.sbar.r.buf:hide()
987 end,
989 update_binds = function (w, mode)
990 -- Generate the list of active key & buffer binds for this mode
991 w.binds = util.table.join(mode_binds[mode], mode_binds.all)
992 -- Clear & hide buffer
993 w.buffer = nil
994 w:update_buf()
995 end,
997 -- Tab label functions
998 make_tab_label = function (w, pos)
999 local t = {
1000 label = label(),
1001 sep = label(),
1002 ebox = eventbox(),
1003 layout = hbox(),
1005 t.label.font = theme.tablabel_font or theme.font
1006 t.layout:pack_start(t.label, true, true, 0)
1007 t.layout:pack_start(t.sep, false, false, 0)
1008 t.ebox:set_child(t.layout)
1009 t.ebox:add_signal("button-release", function (e, m, b)
1010 if b == 1 then
1011 w.tabs:switch(pos)
1012 return true
1013 elseif b == 2 then
1014 w:close_tab(w.tabs:atindex(pos))
1015 return true
1017 end)
1018 return t
1019 end,
1021 destroy_tab_label = function (w, t)
1022 if not t then t = table.remove(w.tbar.titles) end
1023 for _, wi in pairs(t) do
1024 wi:destroy()
1026 end,
1028 update_tab_labels = function (w, current)
1029 local tb = w.tbar
1030 local count, current = w.tabs:count(), current or w.tabs:current()
1031 tb.ebox:hide()
1033 -- Leave the tablist hidden if there is only one tab open
1034 if count <= 1 then
1035 return nil
1038 if count ~= #tb.titles then
1039 -- Grow the number of labels
1040 while count > #tb.titles do
1041 local t = w:make_tab_label(#tb.titles + 1)
1042 tb.layout:pack_start(t.ebox, true, true, 0)
1043 table.insert(tb.titles, t)
1045 -- Prune number of labels
1046 while count < #tb.titles do
1047 w:destroy_tab_label()
1051 if count ~= 0 then
1052 for i = 1, count do
1053 local t = tb.titles[i]
1054 local title = " " ..i.. " "..w:get_tab_title(w.tabs:atindex(i))
1055 t.label.text = util.escape(string.format(theme.tablabel_format or "%s", title))
1056 w:apply_tablabel_theme(t, i == current)
1059 tb.ebox:show()
1060 end,
1062 -- Theme functions
1063 apply_tablabel_theme = function (w, t, selected, atheme)
1064 local theme = atheme or theme
1065 if selected then
1066 t.label.fg = theme.selected_tablabel_fg or theme.tablabel_fg or theme.fg
1067 t.ebox.bg = theme.selected_tablabel_bg or theme.tablabel_bg or theme.bg
1068 else
1069 t.label.fg = theme.tablabel_fg or theme.fg
1070 t.ebox.bg = theme.tablabel_bg or theme.bg
1072 end,
1074 apply_window_theme = function (w, atheme)
1075 local theme = atheme or theme
1076 local s, i, t = w.sbar, w.ibar, w.tbar
1077 local fg, bg, font = theme.fg, theme.bg, theme.font
1079 -- Set foregrounds
1080 for wi, v in pairs({
1081 [s.l.uri] = theme.uri_fg or theme.statusbar_fg or fg,
1082 [s.l.loaded] = theme.loaded_fg or theme.statusbar_fg or fg,
1083 [s.r.buf] = theme.buf_fg or theme.statusbar_fg or fg,
1084 [s.r.tabi] = theme.tabi_fg or theme.statusbar_fg or fg,
1085 [s.r.scroll] = theme.scroll_fg or theme.statusbar_fg or fg,
1086 [i.prompt] = theme.prompt_fg or theme.inputbar_fg or fg,
1087 [i.input] = theme.input_fg or theme.inputbar_fg or fg,
1088 }) do wi.fg = v end
1090 -- Set backgrounds
1091 for wi, v in pairs({
1092 [s.l.ebox] = theme.statusbar_bg or bg,
1093 [s.r.ebox] = theme.statusbar_bg or bg,
1094 [s.ebox] = theme.statusbar_bg or bg,
1095 [i.ebox] = theme.inputbar_bg or bg,
1096 [i.input] = theme.input_bg or theme.inputbar_bg or bg,
1097 }) do wi.bg = v end
1099 -- Set fonts
1100 for wi, v in pairs({
1101 [s.l.uri] = theme.uri_font or theme.statusbar_font or font,
1102 [s.l.loaded] = theme.loaded_font or theme.statusbar_font or font,
1103 [s.r.buf] = theme.buf_font or theme.statusbar_font or font,
1104 [s.r.tabi] = theme.tabi_font or theme.statusbar_font or font,
1105 [s.r.scroll] = theme.scroll_font or theme.statusbar_font or font,
1106 [i.prompt] = theme.prompt_font or theme.inputbar_font or font,
1107 [i.input] = theme.input_font or theme.inputbar_font or font,
1108 }) do wi.font = v end
1109 end,
1112 -- Create new window
1113 function new_window(uris)
1114 local w = build_window()
1116 -- Pack the window table full of the common helper functions
1117 for k, v in pairs(window_helpers) do w[k] = v end
1119 attach_window_signals(w)
1121 -- Apply window theme
1122 w:apply_window_theme()
1124 -- Populate notebook with tabs
1125 for _, uri in ipairs(uris or {}) do
1126 w:new_tab(uri)
1129 -- Make sure something is loaded
1130 if w.tabs:count() == 0 then
1131 w:new_tab(HOMEPAGE)
1134 -- Set initial mode
1135 w:set_mode()
1137 return w
1140 new_window(uris)
1142 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80