Add wikipedia & luakit.org to the list of search engines
[luakit.git] / rc.lua
blob4b382ddf6d52aa4a432dd83935c8cca1ff6f74c8
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 luakit = "http://luakit.org/search/index/luakit?q={0}",
89 google = "http://google.com/search?q={0}",
90 wikipedia = "http://en.wikipedia.org/wiki/Special:Search?search={0}",
91 debbugs = "http://bugs.debian.org/{0}",
92 imdb = "http://imdb.com/find?s=all&q={0}",
93 sourceforge = "http://sf.net/search/?words={0}",
96 -- Add key bindings to be used across all windows
97 mode_binds = {
98 -- bind.buf(Pattern, function (w, buffer, opts) .. end, opts),
99 -- bind.key({Modifiers}, Key name, function (w, opts) .. end, opts),
100 -- bind.but({Modifiers}, Button num, function (w, opts) .. end, opts),
101 all = {
102 bind.key({}, "Escape", function (w) w:set_mode() end),
103 bind.key({"Control"}, "[", function (w) w:set_mode() end),
105 -- Mouse bindings
106 bind.but({}, 2, function (w)
107 -- Open hovered uri in new tab
108 local uri = w:get_current().hovered_uri
109 if uri then w:new_tab(uri)
110 else -- Open selection in current tab
111 uri = luakit.get_selection()
112 if uri then w:get_current().uri = uri end
114 end),
115 bind.but({}, 8, function (w) w:back() end),
116 bind.but({}, 9, function (w) w:forward() end),
118 normal = {
119 bind.key({}, "i", function (w) w:set_mode("insert") end),
120 bind.key({}, ":", function (w) w:set_mode("command") end),
122 -- Scrolling
123 bind.key({}, "h", function (w) w:scroll_horiz("-"..SCROLL_STEP.."px") end),
124 bind.key({}, "j", function (w) w:scroll_vert ("+"..SCROLL_STEP.."px") end),
125 bind.key({}, "k", function (w) w:scroll_vert ("-"..SCROLL_STEP.."px") end),
126 bind.key({}, "l", function (w) w:scroll_horiz("+"..SCROLL_STEP.."px") end),
127 bind.key({}, "Left", function (w) w:scroll_horiz("-"..SCROLL_STEP.."px") end),
128 bind.key({}, "Down", function (w) w:scroll_vert ("+"..SCROLL_STEP.."px") end),
129 bind.key({}, "Up", function (w) w:scroll_vert ("-"..SCROLL_STEP.."px") end),
130 bind.key({}, "Right", function (w) w:scroll_horiz("+"..SCROLL_STEP.."px") end),
131 bind.key({"Control"}, "d", function (w) w:scroll_page(0.5) end),
132 bind.key({"Control"}, "u", function (w) w:scroll_page(-0.5) end),
133 bind.key({"Control"}, "f", function (w) w:scroll_page(1.0) end),
134 bind.key({"Control"}, "b", function (w) w:scroll_page(-1.0) end),
135 bind.buf("^gg$", function (w) w:scroll_vert("0%") end),
136 bind.buf("^G$", function (w) w:scroll_vert("100%") end),
137 bind.buf("^[\-\+]?[0-9]+[%%G]$", function (w, b) w:scroll_vert(string.match(b, "^([\-\+]?%d+)[%%G]$") .. "%") end),
139 -- Clipboard
140 bind.key({}, "p", function (w) w:navigate(luakit.get_selection()) end),
141 bind.key({}, "P", function (w) w:new_tab(luakit.get_selection()) end),
142 bind.buf("^yy$", function (w) luakit.set_selection(w:get_current().uri) end),
143 bind.buf("^yt$", function (w) luakit.set_selection(w.win.title) end),
145 -- Commands
146 bind.buf("^o$", function (w, c) w:enter_cmd(":open ") end),
147 bind.buf("^t$", function (w, c) w:enter_cmd(":tabopen ") end),
148 bind.buf("^,g$", function (w, c) w:enter_cmd(":websearch google ") end),
150 -- Searching
151 bind.key({}, "/", function (w) w:start_search(true) end),
152 bind.key({}, "?", function (w) w:start_search(false) end),
153 bind.key({}, "n", function (w) w:search(nil, true) end),
154 bind.key({}, "N", function (w) w:search(nil, false) end),
156 -- History
157 bind.buf("^[0-9]*H$", function (w, b) w:back (tonumber(string.match(b, "^(%d*)H$") or 1)) end),
158 bind.buf("^[0-9]*L$", function (w, b) w:forward(tonumber(string.match(b, "^(%d*)L$") or 1)) end),
160 -- Tab
161 bind.buf("^[0-9]*gT$", function (w, b) w:prev_tab(tonumber(string.match(b, "^(%d*)gT$") or 1)) end),
162 bind.buf("^[0-9]*gt$", function (w, b) w:next_tab(tonumber(string.match(b, "^(%d*)gt$") or 1)) end),
163 bind.buf("^gH$", function (w) w:new_tab(HOMEPAGE) end),
164 bind.buf("^d$", function (w) w:close_tab() end),
166 bind.key({}, "r", function (w) w:reload() end),
167 bind.buf("^gh$", function (w) w:navigate(HOMEPAGE) end),
168 bind.buf("^ZZ$", function (w) luakit.quit() end),
170 -- Link following
171 bind.key({}, "f", function (w) w:set_mode("follow") end),
174 command = {
175 bind.key({"Shift"}, "Insert", function (w) w:insert_cmd(luakit.get_selection()) end),
176 bind.key({}, "Up", function (w) w:cmd_hist_prev() end),
177 bind.key({}, "Down", function (w) w:cmd_hist_next() end),
178 bind.key({}, "Tab", function (w) w:cmd_completion() end),
179 bind.key({"Control"}, "w", function (w) w:del_word() end),
180 bind.key({"Control"}, "u", function (w) w:del_line() end),
182 search = {
183 bind.key({}, "Up", function (w) w:srch_hist_prev() end),
184 bind.key({}, "Down", function (w) w:srch_hist_next() end),
186 insert = { },
189 -- Commands
190 commands = {
191 -- bind.cmd({Command, Alias1, ...}, function (w, arg, opts) .. end, opts),
192 bind.cmd({"open", "o" }, function (w, a) w:navigate(a) end),
193 bind.cmd({"tabopen", "t" }, function (w, a) w:new_tab(a) end),
194 bind.cmd({"back" }, function (w, a) w:back(tonumber(a) or 1) end),
195 bind.cmd({"forward", "f" }, function (w, a) w:forward(tonumber(a) or 1) end),
196 bind.cmd({"scroll" }, function (w, a) w:scroll_vert(a) end),
197 bind.cmd({"quit", "q" }, function (w) luakit.quit() end),
198 bind.cmd({"close", "c" }, function (w) w:close_tab() end),
199 bind.cmd({"websearch", "ws" }, function (w, e, s) w:websearch(e, s) end),
200 bind.cmd({"reload", }, function (w) w:reload() end),
201 bind.cmd({"viewsource", "vs" }, function (w) w:toggle_source(true) end),
202 bind.cmd({"viewsource!", "vs!"}, function (w) w:toggle_source() end),
205 function set_http_options(w)
206 local proxy = HTTPPROXY or os.getenv("http_proxy")
207 if proxy then w:set('proxy-uri', proxy) end
208 w:set('user-agent', 'luakit')
209 -- Uncomment the following options if you want to enable SSL certs validation.
210 -- w:set('ssl-ca-file', '/etc/certs/ca-certificates.crt')
211 -- w:set('ssl-strict', true)
214 -- Build and pack window widgets
215 function build_window()
216 -- Create a table for widgets and state variables for a window
217 local w = {
218 win = window(),
219 ebox = eventbox(),
220 layout = vbox(),
221 tabs = notebook(),
222 -- Tab bar widgets
223 tbar = {
224 layout = hbox(),
225 ebox = eventbox(),
226 titles = { },
228 -- Status bar widgets
229 sbar = {
230 layout = hbox(),
231 ebox = eventbox(),
232 -- Left aligned widgets
233 l = {
234 layout = hbox(),
235 ebox = eventbox(),
236 uri = label(),
237 loaded = label(),
239 -- Fills space between the left and right aligned widgets
240 filler = label(),
241 -- Right aligned widgets
242 r = {
243 layout = hbox(),
244 ebox = eventbox(),
245 buf = label(),
246 tabi = label(),
247 scroll = label(),
250 -- Input bar widgets
251 ibar = {
252 layout = hbox(),
253 ebox = eventbox(),
254 prompt = label(),
255 input = entry(),
259 -- Assemble window
260 w.ebox:set_child(w.layout)
261 w.win:set_child(w.ebox)
263 -- Pack tab bar
264 local t = w.tbar
265 t.ebox:set_child(t.layout, false, false, 0)
266 w.layout:pack_start(t.ebox, false, false, 0)
268 -- Pack notebook
269 w.layout:pack_start(w.tabs, true, true, 0)
271 -- Pack left-aligned statusbar elements
272 local l = w.sbar.l
273 l.layout:pack_start(l.uri, false, false, 0)
274 l.layout:pack_start(l.loaded, false, false, 0)
275 l.ebox:set_child(l.layout)
277 -- Pack right-aligned statusbar elements
278 local r = w.sbar.r
279 r.layout:pack_start(r.buf, false, false, 0)
280 r.layout:pack_start(r.tabi, false, false, 0)
281 r.layout:pack_start(r.scroll, false, false, 0)
282 r.ebox:set_child(r.layout)
284 -- Pack status bar elements
285 local s = w.sbar
286 s.layout:pack_start(l.ebox, false, false, 0)
287 s.layout:pack_start(s.filler, true, true, 0)
288 s.layout:pack_start(r.ebox, false, false, 0)
289 s.ebox:set_child(s.layout)
290 w.layout:pack_start(s.ebox, false, false, 0)
292 -- Pack input bar
293 local i = w.ibar
294 i.layout:pack_start(i.prompt, false, false, 0)
295 i.layout:pack_start(i.input, true, true, 0)
296 i.ebox:set_child(i.layout)
297 w.layout:pack_start(i.ebox, false, false, 0)
299 -- Other settings
300 i.input.show_frame = false
301 w.tabs.show_tabs = false
302 l.loaded:hide()
303 l.uri.selectable = true
305 return w
308 function attach_window_signals(w)
309 -- Attach notebook widget signals
310 w.tabs:add_signal("page-added", function (nbook, view, idx)
311 w:update_tab_count(idx)
312 w:update_tab_labels()
313 end)
315 w.tabs:add_signal("switch-page", function (nbook, view, idx)
316 w:update_tab_count(idx)
317 w:update_win_title(view)
318 w:update_uri(view)
319 w:update_progress(view)
320 w:update_tab_labels(idx)
321 end)
323 -- Attach window widget signals
324 w.win:add_signal("key-press", function (win, mods, key)
325 -- Reset command line completion
326 if w:get_mode() == "command" and key ~= "Tab" and w.compl_start then
327 w:update_uri()
328 w.compl_index = 0
331 if w:hit(mods, key) then
332 return true
334 end)
336 w.win:add_signal("mode-changed", function (win, mode)
337 local i, p = w.ibar.input, w.ibar.prompt
339 w:update_binds(mode)
340 w.cmd_hist_cursor = nil
342 -- Clear following hints if the user exits follow mode
343 if w.showing_hints then
344 w:eval_js("clear();");
345 w.showing_hints = false
348 -- If a user aborts a search return to the original position
349 if w.search_start_marker then
350 w:get_current():set_scroll_vert(w.search_start_marker)
351 w.search_start_marker = nil
354 if mode == "normal" then
355 p:hide()
356 i:hide()
357 elseif mode == "insert" then
358 i:hide()
359 i.text = ""
360 p.text = "-- INSERT --"
361 p:show()
362 elseif mode == "command" then
363 p:hide()
364 i.text = ":"
365 i:show()
366 i:focus()
367 i:set_position(-1)
368 elseif mode == "search" then
369 p:hide()
370 i:show()
371 elseif mode == "follow" then
372 w:eval_js_from_file(util.find_data("scripts/follow.js"))
373 w:eval_js("clear(); show_hints();")
374 w.showing_hints = true
375 p.text = "Follow:"
376 p:show()
377 i.text = ""
378 i:show()
379 i:focus()
380 i:set_position(-1)
381 else
382 w.ibar.prompt.text = ""
383 w.ibar.input.text = ""
385 end)
387 -- Attach inputbar widget signals
388 w.ibar.input:add_signal("changed", function()
389 local text = w.ibar.input.text
390 -- Auto-exit "command" mode if you backspace or delete the ":"
391 -- character at the start of the input box when in "command" mode.
392 if w:is_mode("command") and not string.match(text, "^:") then
393 w:set_mode()
394 elseif w:is_mode("search") then
395 if string.match(text, "^[\?\/]") then
396 w:search(string.sub(text, 2), (string.sub(text, 1, 1) == "/"))
397 else
398 w:clear_search()
399 w:set_mode()
401 elseif w:is_mode("follow") then
402 w:eval_js(string.format("update(%q)", w.ibar.input.text))
404 end)
406 w.ibar.input:add_signal("activate", function()
407 local text = w.ibar.input.text
408 if w:is_mode("command") then
409 w:cmd_hist_add(text)
410 w:match_cmd(string.sub(text, 2))
411 w:set_mode()
412 elseif w:is_mode("search") then
413 w:srch_hist_add(text)
414 w:search(string.sub(text, 2), string.sub(text, 1, 1) == "/")
415 -- User doesn't want to return to start position
416 w.search_start_marker = nil
417 w:set_mode()
418 w.ibar.prompt.text = util.escape(text)
419 w.ibar.prompt:show()
421 end)
424 -- Attach signal handlers to a new tab's webview
425 function attach_webview_signals(w, view)
426 view:add_signal("property::title", function (v)
427 w:update_tab_labels()
428 if w:is_current(v) then
429 w:update_win_title(v)
431 end)
433 view:add_signal("property::uri", function (v)
434 w:update_tab_labels()
435 if w:is_current(v) then
436 w:update_uri(v)
438 end)
440 view:add_signal("link-hover", function (v, link)
441 if w:is_current(v) and link then
442 w.sbar.l.uri.text = "Link: " .. util.escape(link)
444 end)
446 view:add_signal("link-unhover", function (v)
447 if w:is_current(v) then
448 w:update_uri(v)
450 end)
452 view:add_signal("form-active", function ()
453 w:set_mode("insert")
454 end)
456 view:add_signal("root-active", function ()
457 w:set_mode()
458 end)
460 view:add_signal("key-press", function ()
461 -- Only allow key press events to hit the webview if the user is in
462 -- "insert" mode.
463 if not w:is_mode("insert") then
464 return true
466 end)
468 view:add_signal("button-release", function (v, mods, button)
469 if w:hit(mods, button) then
470 return true
472 end)
474 -- Update progress widgets & set default mode on navigate
475 view:add_signal("load-status", function (v, status)
476 if w:is_current(v) then
477 w:update_progress(v)
478 if status == "provisional" then
479 w:set_mode()
482 end)
484 -- Domain properties
485 view:add_signal("load-status", function (v, status)
486 if status == "committed" then
487 local domain = string.match(v.uri, "^%a+://([^/]*)/?")
488 if string.match(domain, "^www.") then domain = string.sub(domain, 5) end
489 local props = util.table.join(domain_props.all or {}, domain_props[domain] or {})
490 for k, v in pairs(props) do
491 info("Domain prop: %s = %s (%s)", k, tostring(v), domain)
492 view:set_prop(k, v)
495 end)
497 -- 'link' contains the download link
498 -- 'mime' contains the mime type that is requested
499 -- return TRUE to accept or FALSE to reject
500 view:add_signal("mime-type-decision", function (v, link, mime)
501 info("Requested link: %s (%s)", link, mime)
502 -- i.e. block binary files like *.exe
503 --if mime == "application/octet-stream" then
504 -- return false
505 --end
506 end)
508 -- 'link' contains the download link
509 -- 'filename' contains the suggested filename (from server or webkit)
510 view:add_signal("download-request", function (v, link, filename)
511 if not filename then return end
512 -- Make download dir
513 os.execute(string.format("mkdir -p %q", DOWNLOAD_DIR))
514 local dl = DOWNLOAD_DIR .. "/" .. filename
515 local wget = string.format("wget -q %q -O %q", link, dl)
516 info("Launching: %s", wget)
517 luakit.spawn(wget)
518 end)
520 -- 'link' contains the download link
521 -- 'reason' contains the reason of the request (i.e. "link-clicked")
522 -- return TRUE to handle the request by yourself or FALSE to proceed
523 -- with default behaviour
524 view:add_signal("new-window-decision", function (v, link, reason)
525 info("New window decision: %s (%s)", link, reason)
526 if reason == "link-clicked" then
527 new_window({ link })
528 return true
530 w:new_tab(link)
531 end)
533 view:add_signal("property::progress", function (v)
534 if w:is_current(v) then
535 w:update_progress(v)
537 end)
539 view:add_signal("expose", function (v)
540 if w:is_current(v) then
541 w:update_scroll(v)
543 end)
546 -- Parses scroll amounts of the form:
547 -- Relative: "+20%", "-20%", "+20px", "-20px"
548 -- Absolute: 20, "20%", "20px"
549 -- And returns an absolute value.
550 function parse_scroll(current, max, value)
551 if string.match(value, "^%d+px$") then
552 return tonumber(string.match(value, "^(%d+)px$"))
553 elseif string.match(value, "^%d+%%$") then
554 return math.ceil(max * (tonumber(string.match(value, "^(%d+)%%$")) / 100))
555 elseif string.match(value, "^[\-\+]%d+px") then
556 return current + tonumber(string.match(value, "^([\-\+]%d+)px"))
557 elseif string.match(value, "^[\-\+]%d+%%$") then
558 return math.ceil(current + (max * (tonumber(string.match(value, "^([\-\+]%d+)%%$")) / 100)))
559 else
560 print("E: unable to parse scroll amount:", value)
564 -- Helper functions which operate on a windows widget structure
565 window_helpers = {
566 -- Return the widget in the currently active tab
567 get_current = function (w) return w.tabs:atindex(w.tabs:current()) end,
568 -- Check if given widget is the widget in the currently active tab
569 is_current = function (w, wi) return w.tabs:indexof(wi) == w.tabs:current() end,
571 -- Wrappers around the mode plugin
572 set_mode = function (w, name) mode.set(w.win, name) end,
573 get_mode = function (w) return mode.get(w.win) end,
574 is_mode = function (w, name) return name == w:get_mode() end,
575 is_any_mode = function (w, t, name) return util.table.hasitem(t, name or w:get_mode()) end,
577 -- Wrappers around the view:get_prop & view:set_prop methods
578 get = function (w, prop, view)
579 if not view then view = w:get_current() end
580 return view:get_prop(prop)
581 end,
583 set = function (w, prop, val, view)
584 if not view then view = w:get_current() end
585 view:set_prop(prop, val)
586 end,
588 get_tab_title = function (w, view)
589 if not view then view = w:get_current() end
590 return view:get_prop("title") or view.uri or "(Untitled)"
591 end,
593 navigate = function (w, uri, view)
594 local v = view or w:get_current()
595 if v then
596 v.uri = uri
597 else
598 return w:new_tab(uri)
600 end,
602 reload = function (w, view)
603 if not view then view = w:get_current() end
604 view:reload()
605 end,
607 new_tab = function (w, uri)
608 local view = webview()
609 w.tabs:append(view)
610 set_http_options(w)
611 attach_webview_signals(w, view)
612 if uri then view.uri = uri end
613 view.show_scrollbars = false
614 w:update_tab_count()
615 end,
617 -- close the current tab
618 close_tab = function (w, view)
619 if not view then view = w:get_current() end
620 if not view then return end
621 w.tabs:remove(view)
622 view:destroy()
623 w:update_tab_count()
624 end,
626 -- evaluate javascript code and return string result
627 eval_js = function (w, script, file, view)
628 if not view then view = w:get_current() end
629 return view:eval_js(script, file or "(buffer)")
630 end,
632 -- evaluate javascript code from file and return string result
633 eval_js_from_file = function (w, file, view)
634 local fh, err = io.open(file)
635 if not fh then return error(err) end
636 local script = fh:read("*a")
637 fh:close()
638 return w:eval_js(script, file, view)
639 end,
641 -- Wrapper around the bind plugin's hit method
642 hit = function (w, mods, key)
643 local caught, newbuf = bind.hit(w.binds or {}, mods, key, w.buffer, w:is_mode("normal"), w)
644 w.buffer = newbuf
645 w:update_buf()
646 return caught
647 end,
649 -- Wrapper around the bind plugin's match_cmd method
650 match_cmd = function (w, buffer)
651 return bind.match_cmd(commands, buffer, w)
652 end,
654 -- Toggle source view
655 toggle_source = function (w, show, view)
656 if not view then view = w:get_current() end
657 if show == nil then show = not view:get_view_source() end
658 view:set_view_source(show)
659 end,
661 -- enter command or characters into command line
662 enter_cmd = function (w, cmd)
663 local i = w.ibar.input
664 w:set_mode("command")
665 i.text = cmd
666 i:set_position(-1)
667 end,
669 -- insert a string into the command line at the current cursor position
670 insert_cmd = function (w, str)
671 if not str then return nil end
672 local i = w.ibar.input
673 local text = i.text
674 local pos = i:get_position()
675 local left, right = string.sub(text, 1, pos), string.sub(text, pos+1)
676 i.text = left .. str .. right
677 i:set_position(pos + #str + 1)
678 end,
680 -- search engine wrapper
681 websearch = function (w, args)
682 local sep = string.find(args, " ")
683 local engine = string.sub(args, 1, sep-1)
684 local search = string.sub(args, sep+1)
685 if not search_engines[engine] then
686 print("E: No matching search engine found:", engine)
687 return 0
689 local uri = string.gsub(search_engines[engine], "{%d}", search)
690 return w:navigate(uri)
691 end,
693 -- Command line completion of available commands
694 cmd_completion = function (w)
695 local i = w.ibar.input
696 local s = w.sbar.l.uri
697 local cmpl = {}
699 -- Get last completion (is reset on key press other than <Tab>)
700 if not w.compl_start or w.compl_index == 0 then
701 w.compl_start = "^" .. string.sub(i.text, 2)
702 w.compl_index = 1
705 -- Get suitable commands
706 for _, b in ipairs(commands) do
707 for _, c in pairs(b.commands) do
708 if c and string.match(c, w.compl_start) then
709 table.insert(cmpl, c)
714 table.sort(cmpl)
716 if #cmpl > 0 then
717 local text = ""
718 for index, comp in pairs(cmpl) do
719 if index == w.compl_index then
720 i.text = ":" .. comp .. " "
721 i:set_position(-1)
723 if text ~= "" then
724 text = text .. " | "
726 text = text .. comp
729 -- cycle through all possible completions
730 if w.compl_index == #cmpl then
731 w.compl_index = 1
732 else
733 w.compl_index = w.compl_index + 1
735 s.text = util.escape(text)
737 end,
739 del_word = function (w)
740 local i = w.ibar.input
741 local text = i.text
742 local pos = i:get_position()
743 if text and #text > 1 and pos > 1 then
744 local left, right = string.sub(text, 2, pos), string.sub(text, pos+1)
745 if not string.find(left, "%s") then
746 left = ""
747 elseif string.find(left, "%w+%s*$") then
748 left = string.sub(left, 0, string.find(left, "%w+%s*$") - 1)
749 elseif string.find(left, "%W+%s*$") then
750 left = string.sub(left, 0, string.find(left, "%W+%s*$") - 1)
752 i.text = string.sub(text, 1, 1) .. left .. right
753 i:set_position(#left + 2)
755 end,
757 del_line = function (w)
758 local i = w.ibar.input
759 if i.text ~= ":" then
760 i.text = ":"
761 i:set_position(-1)
763 end,
765 -- Search history adding
766 srch_hist_add = function (w, srch)
767 if not w.srch_hist then w.srch_hist = {} end
768 -- Check overflow
769 if #w.srch_hist > ((MAX_SRCH_HISTORY or 100) + 5) then
770 while #w.srch_hist > (MAX_SRCH_HISTORY or 100) do
771 table.remove(w.srch_hist, 1)
774 table.insert(w.srch_hist, srch)
775 end,
777 -- Search history traversing
778 srch_hist_prev = function (w)
779 if not w.srch_hist then w.srch_hist = {} end
780 if not w.srch_hist_cursor then
781 w.srch_hist_cursor = #w.srch_hist + 1
782 w.srch_hist_current = w.ibar.input.text
784 local c = w.srch_hist_cursor - 1
785 if w.srch_hist[c] then
786 w.srch_hist_cursor = c
787 w.ibar.input.text = w.srch_hist[c]
788 w.ibar.input:set_position(-1)
790 end,
792 srch_hist_next = function (w)
793 if not w.srch_hist then w.srch_hist = {} end
794 local c = (w.srch_hist_cursor or #w.srch_hist) + 1
795 if w.srch_hist[c] then
796 w.srch_hist_cursor = c
797 w.ibar.input.text = w.srch_hist[c]
798 w.ibar.input:set_position(-1)
799 elseif w.srch_hist_current then
800 w.srch_hist_cursor = nil
801 w.ibar.input.text = w.srch_hist_current
802 w.ibar.input:set_position(-1)
804 end,
806 -- Command history adding
807 cmd_hist_add = function (w, cmd)
808 if not w.cmd_hist then w.cmd_hist = {} end
809 -- Make sure history doesn't overflow
810 if #w.cmd_hist > ((MAX_CMD_HISTORY or 100) + 5) then
811 while #w.cmd_hist > (MAX_CMD_HISTORY or 100) do
812 table.remove(w.cmd_hist, 1)
815 table.insert(w.cmd_hist, cmd)
816 end,
818 -- Command history traversing
819 cmd_hist_prev = function (w)
820 if not w.cmd_hist then w.cmd_hist = {} end
821 if not w.cmd_hist_cursor then
822 w.cmd_hist_cursor = #w.cmd_hist + 1
823 w.cmd_hist_current = w.ibar.input.text
825 local c = w.cmd_hist_cursor - 1
826 if w.cmd_hist[c] then
827 w.cmd_hist_cursor = c
828 w.ibar.input.text = w.cmd_hist[c]
829 w.ibar.input:set_position(-1)
831 end,
833 cmd_hist_next = function (w)
834 if not w.cmd_hist then w.cmd_hist = {} end
835 local c = (w.cmd_hist_cursor or #w.cmd_hist) + 1
836 if w.cmd_hist[c] then
837 w.cmd_hist_cursor = c
838 w.ibar.input.text = w.cmd_hist[c]
839 w.ibar.input:set_position(-1)
840 elseif w.cmd_hist_current then
841 w.cmd_hist_cursor = nil
842 w.ibar.input.text = w.cmd_hist_current
843 w.ibar.input:set_position(-1)
845 end,
847 -- Searching functions
848 start_search = function (w, forward)
849 -- Clear previous search results
850 w:clear_search()
851 w:set_mode("search")
852 local i = w.ibar.input
853 if forward then
854 i.text = "/"
855 else
856 i.text = "?"
858 i:focus()
859 i:set_position(-1)
860 end,
862 search = function (w, text, forward)
863 local view = w:get_current()
864 local text = text or w.last_search
865 if forward == nil then forward = true end
866 local case_sensitive = false
867 local wrap = true
869 if not text or #text == 0 then
870 w:clear_search()
871 return nil
874 w.last_search = text
875 if w.searching_forward == nil then
876 w.searching_forward = forward
877 w.search_start_marker = view:get_scroll_vert()
878 else
879 -- Invert the direction if originally searching in reverse
880 forward = (w.searching_forward == forward)
883 view:search(text, case_sensitive, forward, wrap);
884 end,
886 clear_search = function (w)
887 w:get_current():clear_search()
888 -- Clear search state
889 w.last_search = nil
890 w.searching_forward = nil
891 w.search_start_marker = nil
892 end,
894 -- Webview scroll functions
895 scroll_vert = function (w, value, view)
896 if not view then view = w:get_current() end
897 local cur, max = view:get_scroll_vert()
898 if type(value) == "string" then
899 value = parse_scroll(cur, max, value)
901 view:set_scroll_vert(value)
902 end,
904 scroll_horiz = function (w, value, view)
905 if not view then view = w:get_current() end
906 local cur, max = view:get_scroll_horiz()
907 if type(value) == "string" then
908 value = parse_scroll(cur, max, value)
910 view:set_scroll_horiz(value)
911 end,
913 -- vertical scroll of a multiple of the view_size
914 scroll_page = function (w, value, view)
915 if not view then view = w:get_current() end
916 local cur, max, size = view:get_scroll_vert()
917 view:set_scroll_vert(cur + size * value)
918 end,
920 -- Tab traversing functions
921 next_tab = function (w, n)
922 w.tabs:switch((((n or 1) + w.tabs:current() -1) % w.tabs:count()) + 1)
923 end,
924 prev_tab = function (w, n)
925 w.tabs:switch(((w.tabs:current() - (n or 1) -1) % w.tabs:count()) + 1)
926 end,
927 goto_tab = function (w, n)
928 w.tabs:switch(n)
929 end,
931 -- History traversing functions
932 back = function (w, n, view)
933 (view or w:get_current()):go_back(n or 1)
934 end,
935 forward = function (w, n, view)
936 (view or w:get_current()):go_forward(n or 1)
937 end,
939 -- GUI content update functions
940 update_tab_count = function (w, i, t)
941 w.sbar.r.tabi.text = string.format("[%d/%d]", i or w.tabs:current(), t or w.tabs:count())
942 end,
944 update_win_title = function (w, view)
945 if not view then view = w:get_current() end
946 local title = view:get_prop("title")
947 local uri = view.uri
948 if not title and not uri then
949 w.win.title = "luakit"
950 else
951 w.win.title = (title or "luakit") .. " - " .. (uri or "about:blank")
953 end,
955 update_uri = function (w, view, uri)
956 if not view then view = w:get_current() end
957 w.sbar.l.uri.text = util.escape((uri or (view and view.uri) or "about:blank"))
958 end,
960 update_progress = function (w, view, p)
961 if not view then view = w:get_current() end
962 if not p then p = view:get_prop("progress") end
963 if not view:loading() or p == 1 then
964 w.sbar.l.loaded:hide()
965 else
966 w.sbar.l.loaded:show()
967 w.sbar.l.loaded.text = string.format("(%d%%)", p * 100)
969 end,
971 update_scroll = function (w, view)
972 if not view then view = w:get_current() end
973 local val, max = view:get_scroll_vert()
974 if max == 0 then val = "All"
975 elseif val == 0 then val = "Top"
976 elseif val == max then val = "Bot"
977 else val = string.format("%2d%%", (val/max) * 100)
979 w.sbar.r.scroll.text = val
980 end,
982 update_buf = function (w)
983 if w.buffer then
984 w.sbar.r.buf.text = util.escape(string.format(" %-3s", w.buffer))
985 w.sbar.r.buf:show()
986 else
987 w.sbar.r.buf:hide()
989 end,
991 update_binds = function (w, mode)
992 -- Generate the list of active key & buffer binds for this mode
993 w.binds = util.table.join(mode_binds[mode], mode_binds.all)
994 -- Clear & hide buffer
995 w.buffer = nil
996 w:update_buf()
997 end,
999 -- Tab label functions
1000 make_tab_label = function (w, pos)
1001 local t = {
1002 label = label(),
1003 sep = label(),
1004 ebox = eventbox(),
1005 layout = hbox(),
1007 t.label.font = theme.tablabel_font or theme.font
1008 t.layout:pack_start(t.label, true, true, 0)
1009 t.layout:pack_start(t.sep, false, false, 0)
1010 t.ebox:set_child(t.layout)
1011 t.ebox:add_signal("button-release", function (e, m, b)
1012 if b == 1 then
1013 w.tabs:switch(pos)
1014 return true
1015 elseif b == 2 then
1016 w:close_tab(w.tabs:atindex(pos))
1017 return true
1019 end)
1020 return t
1021 end,
1023 destroy_tab_label = function (w, t)
1024 if not t then t = table.remove(w.tbar.titles) end
1025 for _, wi in pairs(t) do
1026 wi:destroy()
1028 end,
1030 update_tab_labels = function (w, current)
1031 local tb = w.tbar
1032 local count, current = w.tabs:count(), current or w.tabs:current()
1033 tb.ebox:hide()
1035 -- Leave the tablist hidden if there is only one tab open
1036 if count <= 1 then
1037 return nil
1040 if count ~= #tb.titles then
1041 -- Grow the number of labels
1042 while count > #tb.titles do
1043 local t = w:make_tab_label(#tb.titles + 1)
1044 tb.layout:pack_start(t.ebox, true, true, 0)
1045 table.insert(tb.titles, t)
1047 -- Prune number of labels
1048 while count < #tb.titles do
1049 w:destroy_tab_label()
1053 if count ~= 0 then
1054 for i = 1, count do
1055 local t = tb.titles[i]
1056 local title = " " ..i.. " "..w:get_tab_title(w.tabs:atindex(i))
1057 t.label.text = util.escape(string.format(theme.tablabel_format or "%s", title))
1058 w:apply_tablabel_theme(t, i == current)
1061 tb.ebox:show()
1062 end,
1064 -- Theme functions
1065 apply_tablabel_theme = function (w, t, selected, atheme)
1066 local theme = atheme or theme
1067 if selected then
1068 t.label.fg = theme.selected_tablabel_fg or theme.tablabel_fg or theme.fg
1069 t.ebox.bg = theme.selected_tablabel_bg or theme.tablabel_bg or theme.bg
1070 else
1071 t.label.fg = theme.tablabel_fg or theme.fg
1072 t.ebox.bg = theme.tablabel_bg or theme.bg
1074 end,
1076 apply_window_theme = function (w, atheme)
1077 local theme = atheme or theme
1078 local s, i, t = w.sbar, w.ibar, w.tbar
1079 local fg, bg, font = theme.fg, theme.bg, theme.font
1081 -- Set foregrounds
1082 for wi, v in pairs({
1083 [s.l.uri] = theme.uri_fg or theme.statusbar_fg or fg,
1084 [s.l.loaded] = theme.loaded_fg or theme.statusbar_fg or fg,
1085 [s.r.buf] = theme.buf_fg or theme.statusbar_fg or fg,
1086 [s.r.tabi] = theme.tabi_fg or theme.statusbar_fg or fg,
1087 [s.r.scroll] = theme.scroll_fg or theme.statusbar_fg or fg,
1088 [i.prompt] = theme.prompt_fg or theme.inputbar_fg or fg,
1089 [i.input] = theme.input_fg or theme.inputbar_fg or fg,
1090 }) do wi.fg = v end
1092 -- Set backgrounds
1093 for wi, v in pairs({
1094 [s.l.ebox] = theme.statusbar_bg or bg,
1095 [s.r.ebox] = theme.statusbar_bg or bg,
1096 [s.ebox] = theme.statusbar_bg or bg,
1097 [i.ebox] = theme.inputbar_bg or bg,
1098 [i.input] = theme.input_bg or theme.inputbar_bg or bg,
1099 }) do wi.bg = v end
1101 -- Set fonts
1102 for wi, v in pairs({
1103 [s.l.uri] = theme.uri_font or theme.statusbar_font or font,
1104 [s.l.loaded] = theme.loaded_font or theme.statusbar_font or font,
1105 [s.r.buf] = theme.buf_font or theme.statusbar_font or font,
1106 [s.r.tabi] = theme.tabi_font or theme.statusbar_font or font,
1107 [s.r.scroll] = theme.scroll_font or theme.statusbar_font or font,
1108 [i.prompt] = theme.prompt_font or theme.inputbar_font or font,
1109 [i.input] = theme.input_font or theme.inputbar_font or font,
1110 }) do wi.font = v end
1111 end,
1114 -- Create new window
1115 function new_window(uris)
1116 local w = build_window()
1118 -- Pack the window table full of the common helper functions
1119 for k, v in pairs(window_helpers) do w[k] = v end
1121 attach_window_signals(w)
1123 -- Apply window theme
1124 w:apply_window_theme()
1126 -- Populate notebook with tabs
1127 for _, uri in ipairs(uris or {}) do
1128 w:new_tab(uri)
1131 -- Make sure something is loaded
1132 if w.tabs:count() == 0 then
1133 w:new_tab(HOMEPAGE)
1136 -- Set initial mode
1137 w:set_mode()
1139 return w
1142 new_window(uris)
1144 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80