Change LUAKIT_LUA_LIB_PATH to LUAKIT_INSTALL_PATH
[luakit.git] / rc.lua
blobe82149eebb52f3d538f4eb84eaa84d0b6c0f1f8c
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
17 -- Variable definitions
18 HOMEPAGE = "http://luakit.org/"
19 --HOMEPAGE = "http://github.com/mason-larobina/luakit"
20 SCROLL_STEP = 20
21 MAX_CMD_HISTORY = 100
22 MAX_SRCH_HISTORY = 100
24 -- Luakit theme
25 theme = theme or {
26 -- Default settings
27 font = "monospace normal 9",
28 fg = "#fff",
29 bg = "#000",
31 -- General settings
32 statusbar_fg = "#fff",
33 statusbar_bg = "#000",
34 inputbar_fg = "#000",
35 inputbar_bg = "#fff",
37 -- Specific settings
38 loaded_fg = "#33AADD",
39 tablabel_fg = "#999",
40 tablabel_bg = "#111",
41 selected_tablabel_fg = "#fff",
42 selected_tablabel_bg = "#000",
44 -- Enforce a minimum tab width of 30 characters to prevent longer tab
45 -- titles overshadowing small tab titles when things get crowded.
46 tablabel_format = "%-30s",
49 widget.add_signal("new", function(wi)
50 wi:add_signal("init", function(wi)
51 if wi.type == "window" then
52 wi:add_signal("destroy", function ()
53 -- Call the quit function if this was the last window left
54 if #luakit.windows == 0 then luakit.quit() end
55 end)
56 end
57 end)
58 end)
60 -- Search engines
61 search_engines = {
62 google = "http://google.com/search?q={0}",
63 imdb = "http://imdb.com/find?s=all&q={0}",
64 sourceforge = "http://sf.net/search/?words={0}"
67 -- Add key bindings to be used across all windows
68 mode_binds = {
69 -- bind.buf(Pattern, function (w, buffer, opts) .. end, opts),
70 -- bind.key({Modifiers}, Key name, function (w, opts) .. end, opts),
71 all = {
72 bind.key({}, "Escape", function (w) w:set_mode() end),
73 bind.key({"Control"}, "[", function (w) w:set_mode() end),
75 normal = {
76 bind.key({}, "i", function (w) w:set_mode("insert") end),
77 bind.key({}, ":", function (w) w:set_mode("command") end),
79 -- Scrolling
80 bind.key({}, "h", function (w) w:scroll_horiz("-"..SCROLL_STEP.."px") end),
81 bind.key({}, "j", function (w) w:scroll_vert ("+"..SCROLL_STEP.."px") end),
82 bind.key({}, "k", function (w) w:scroll_vert ("-"..SCROLL_STEP.."px") end),
83 bind.key({}, "l", function (w) w:scroll_horiz("+"..SCROLL_STEP.."px") end),
84 bind.key({}, "Left", function (w) w:scroll_horiz("-"..SCROLL_STEP.."px") end),
85 bind.key({}, "Down", function (w) w:scroll_vert ("+"..SCROLL_STEP.."px") end),
86 bind.key({}, "Up", function (w) w:scroll_vert ("-"..SCROLL_STEP.."px") end),
87 bind.key({}, "Right", function (w) w:scroll_horiz("+"..SCROLL_STEP.."px") end),
88 bind.buf("^gg$", function (w) w:scroll_vert("0%") end),
89 bind.buf("^G$", function (w) w:scroll_vert("100%") end),
90 bind.buf("^[\-\+]?[0-9]+[%%G]$", function (w, b) w:scroll_vert(string.match(b, "^([\-\+]?%d+)[%%G]$") .. "%") end),
92 -- Clipboard
93 bind.key({}, "p", function (w) w:navigate(luakit.selection()) end),
94 bind.key({}, "P", function (w) w:new_tab(luakit.selection()) end),
96 -- Commands
97 bind.buf("^o$", function (w, c) w:enter_cmd(":open ") end),
98 bind.buf("^t$", function (w, c) w:enter_cmd(":tabopen ") end),
99 bind.buf("^,g$", function (w, c) w:enter_cmd(":websearch google ") end),
101 -- Searching
102 bind.key({}, "/", function (w) w:start_search(true) end),
103 bind.key({}, "?", function (w) w:start_search(false) end),
104 bind.key({}, "n", function (w) w:search(nil, true) end),
105 bind.key({}, "N", function (w) w:search(nil, false) end),
107 -- History
108 bind.buf("^[0-9]*H$", function (w, b) w:back (tonumber(string.match(b, "^(%d*)H$") or 1)) end),
109 bind.buf("^[0-9]*L$", function (w, b) w:forward(tonumber(string.match(b, "^(%d*)L$") or 1)) end),
111 -- Tab
112 bind.buf("^[0-9]*gT$", function (w, b) w:prev_tab(tonumber(string.match(b, "^(%d*)gT$") or 1)) end),
113 bind.buf("^[0-9]*gt$", function (w, b) w:next_tab(tonumber(string.match(b, "^(%d*)gt$") or 1)) end),
114 bind.buf("^gH$", function (w) w:new_tab(HOMEPAGE) end),
115 bind.buf("^d$", function (w) w:close_tab() end),
117 bind.buf("^gh$", function (w) w:navigate(HOMEPAGE) end),
118 bind.buf("^ZZ$", function (w) luakit.quit() end),
120 command = {
121 bind.key({"Shift"}, "Insert", function (w) w:insert_cmd(luakit.selection()) end),
122 bind.key({}, "Up", function (w) w:cmd_hist_prev() end),
123 bind.key({}, "Down", function (w) w:cmd_hist_next() end),
124 bind.key({}, "Tab", function (w) w:cmd_completion() end),
125 bind.key({"Control"}, "w", function (w) w:del_word() end),
126 bind.key({"Control"}, "u", function (w) w:del_line() end),
128 search = {
129 bind.key({}, "Up", function (w) w:srch_hist_prev() end),
130 bind.key({}, "Down", function (w) w:srch_hist_next() end),
132 insert = { },
135 -- Commands
136 commands = {
137 -- bind.cmd({Command, Alias1, ...}, function (w, arg, opts) .. end, opts),
138 bind.cmd({"open", "o"}, function (w, a) w:navigate(a) end),
139 bind.cmd({"tabopen", "t"}, function (w, a) w:new_tab(a) end),
140 bind.cmd({"back" }, function (w, a) w:back(tonumber(a) or 1) end),
141 bind.cmd({"forward", "f"}, function (w, a) w:forward(tonumber(a) or 1) end),
142 bind.cmd({"scroll" }, function (w, a) w:scroll_vert(a) end),
143 bind.cmd({"quit", "q"}, function (w) luakit.quit() end),
144 bind.cmd({"close", "c"}, function (w) w:close_tab() end),
145 bind.cmd({"websearch", "ws"}, function (w, e, s) w:websearch(e, s) end),
148 -- Build and pack window widgets
149 function build_window()
150 -- Create a table for widgets and state variables for a window
151 local w = {
152 win = window(),
153 ebox = eventbox(),
154 layout = vbox(),
155 tabs = notebook(),
156 -- Tab bar widgets
157 tbar = {
158 layout = hbox(),
159 ebox = eventbox(),
160 titles = { },
162 -- Status bar widgets
163 sbar = {
164 layout = hbox(),
165 ebox = eventbox(),
166 -- Left aligned widgets
167 l = {
168 layout = hbox(),
169 ebox = eventbox(),
170 uri = label(),
171 loaded = label(),
173 -- Fills space between the left and right aligned widgets
174 filler = label(),
175 -- Right aligned widgets
176 r = {
177 layout = hbox(),
178 ebox = eventbox(),
179 buf = label(),
180 tabi = label(),
181 scroll = label(),
184 -- Input bar widgets
185 ibar = {
186 layout = hbox(),
187 ebox = eventbox(),
188 prompt = label(),
189 input = entry(),
193 -- Assemble window
194 w.ebox:set_child(w.layout)
195 w.win:set_child(w.ebox)
197 -- Pack tab bar
198 local t = w.tbar
199 t.ebox:set_child(t.layout, false, false, 0)
200 w.layout:pack_start(t.ebox, false, false, 0)
202 -- Pack notebook
203 w.layout:pack_start(w.tabs, true, true, 0)
205 -- Pack left-aligned statusbar elements
206 local l = w.sbar.l
207 l.layout:pack_start(l.uri, false, false, 0)
208 l.layout:pack_start(l.loaded, false, false, 0)
209 l.ebox:set_child(l.layout)
211 -- Pack right-aligned statusbar elements
212 local r = w.sbar.r
213 r.layout:pack_start(r.buf, false, false, 0)
214 r.layout:pack_start(r.tabi, false, false, 0)
215 r.layout:pack_start(r.scroll, false, false, 0)
216 r.ebox:set_child(r.layout)
218 -- Pack status bar elements
219 local s = w.sbar
220 s.layout:pack_start(l.ebox, false, false, 0)
221 s.layout:pack_start(s.filler, true, true, 0)
222 s.layout:pack_start(r.ebox, false, false, 0)
223 s.ebox:set_child(s.layout)
224 w.layout:pack_start(s.ebox, false, false, 0)
226 -- Pack input bar
227 local i = w.ibar
228 i.layout:pack_start(i.prompt, false, false, 0)
229 i.layout:pack_start(i.input, true, true, 0)
230 i.ebox:set_child(i.layout)
231 w.layout:pack_start(i.ebox, false, false, 0)
233 -- Other settings
234 i.input.show_frame = false
235 w.tabs.show_tabs = false
236 l.loaded:hide()
237 l.uri.selectable = true
239 return w
242 function attach_window_signals(w)
243 -- Attach notebook widget signals
244 w.tabs:add_signal("page-added", function(nbook, view, idx)
245 w:update_tab_count(idx)
246 w:update_tab_labels()
247 end)
249 w.tabs:add_signal("switch-page", function(nbook, view, idx)
250 w:update_tab_count(idx)
251 w:update_win_title(view)
252 w:update_uri(view)
253 w:update_progress(view)
254 w:update_tab_labels(idx)
255 end)
257 -- Attach window widget signals
258 w.win:add_signal("key-press", function(win, mods, key)
259 -- Reset command line completion
260 if w:get_mode() == "command" and key ~= "Tab" and w.compl_start then
261 w:update_uri()
262 w.compl_index = 0
265 if w:hit(mods, key) then
266 return true
268 end)
270 w.win:add_signal("mode-changed", function(win, mode)
271 w:update_binds(mode)
272 w.cmd_hist_cursor = nil
274 -- If a user aborts a search return to the original position
275 if w.search_start_marker then
276 w:get_current():set_scroll_vert(w.search_start_marker)
277 w.search_start_marker = nil
280 if mode == "normal" then
281 w.ibar.prompt:hide()
282 w.ibar.input:hide()
283 elseif mode == "insert" then
284 w.ibar.input:hide()
285 w.ibar.input.text = ""
286 w.ibar.prompt.text = "-- INSERT --"
287 w.ibar.prompt:show()
288 elseif mode == "command" then
289 w.ibar.prompt:hide()
290 w.ibar.input.text = ":"
291 w.ibar.input:show()
292 w.ibar.input:focus()
293 w.ibar.input:set_position(-1)
294 elseif mode == "search" then
295 w.ibar.prompt:hide()
296 w.ibar.input:show()
297 else
298 w.ibar.prompt.text = ""
299 w.ibar.input.text = ""
301 end)
303 -- Attach inputbar widget signals
304 w.ibar.input:add_signal("changed", function()
305 local text = w.ibar.input.text
306 -- Auto-exit "command" mode if you backspace or delete the ":"
307 -- character at the start of the input box when in "command" mode.
308 if w:is_mode("command") and not string.match(text, "^:") then
309 w:set_mode()
310 elseif w:is_mode("search") then
311 if string.match(text, "^[\?\/]") then
312 w:search(string.sub(text, 2), (string.sub(text, 1, 1) == "/"))
313 else
314 w:clear_search()
315 w:set_mode()
318 end)
320 w.ibar.input:add_signal("activate", function()
321 local text = w.ibar.input.text
322 if w:is_mode("command") then
323 w:cmd_hist_add(text)
324 w:match_cmd(string.sub(text, 2))
325 w:set_mode()
326 elseif w:is_mode("search") then
327 w:srch_hist_add(text)
328 w:search(string.sub(text, 2), string.sub(text, 1, 1) == "/")
329 -- User doesn't want to return to start position
330 w.search_start_marker = nil
331 w:set_mode()
332 w.ibar.prompt.text = text
333 w.ibar.prompt:show()
335 end)
338 -- Attach signal handlers to a new tab's webview
339 function attach_webview_signals(w, view)
340 view:add_signal("title-changed", function (v)
341 w:update_tab_labels()
342 if w:is_current(v) then
343 w:update_win_title(v)
345 end)
347 view:add_signal("property::uri", function(v)
348 w:update_tab_labels()
349 if w:is_current(v) then
350 w:update_uri(v)
352 end)
354 view:add_signal("key-press", function ()
355 -- Only allow key press events to hit the webview if the user is in
356 -- "insert" mode.
357 if not w:is_mode("insert") then
358 return true
360 end)
362 view:add_signal("load-start", function (v)
363 if w:is_current(v) then
364 w:update_progress(v, 0)
365 w:set_mode()
367 end)
369 view:add_signal("progress-update", function (v)
370 if w:is_current(v) then
371 w:update_progress(v)
373 end)
375 view:add_signal("expose", function(v)
376 if w:is_current(v) then
377 w:update_scroll(v)
379 end)
382 -- Parses scroll amounts of the form:
383 -- Relative: "+20%", "-20%", "+20px", "-20px"
384 -- Absolute: 20, "20%", "20px"
385 -- And returns an absolute value.
386 function parse_scroll(current, max, value)
387 if string.match(value, "^%d+px$") then
388 return tonumber(string.match(value, "^(%d+)px$"))
389 elseif string.match(value, "^%d+%%$") then
390 return math.ceil(max * (tonumber(string.match(value, "^(%d+)%%$")) / 100))
391 elseif string.match(value, "^[\-\+]%d+px") then
392 return current + tonumber(string.match(value, "^([\-\+]%d+)px"))
393 elseif string.match(value, "^[\-\+]%d+%%$") then
394 return math.ceil(current + (max * (tonumber(string.match(value, "^([\-\+]%d+)%%$")) / 100)))
395 else
396 print("E: unable to parse scroll amount:", value)
400 -- Helper functions which operate on a windows widget structure
401 window_helpers = {
402 -- Return the widget in the currently active tab
403 get_current = function(w) return w.tabs:atindex(w.tabs:current()) end,
404 -- Check if given widget is the widget in the currently active tab
405 is_current = function(w, wi) return w.tabs:indexof(wi) == w.tabs:current() end,
407 -- Wrappers around the mode plugin
408 set_mode = function(w, name) mode.set(w.win, name) end,
409 get_mode = function(w) return mode.get(w.win) end,
410 is_mode = function(w, name) return name == w:get_mode() end,
411 is_any_mode = function(w, t, name) return util.table.hasitem(t, name or w:get_mode()) end,
413 -- Wrappers around the view:get_prop & view:set_prop methods
414 get = function (w, prop, view)
415 if not view then view = w:get_current() end
416 return view:get_prop(prop)
417 end,
419 set = function (w, prop, val, view)
420 if not view then view = w:get_current() end
421 view:set_prop(prop, val)
422 end,
424 get_tab_title = function (w, view)
425 if not view then view = w:get_current() end
426 return view:get_prop("title") or view.uri or "(Untitled)"
427 end,
429 navigate = function(w, uri, view)
430 local v = view or w:get_current()
431 if v then
432 v.uri = uri
433 else
434 return w:new_tab(uri)
436 end,
438 new_tab = function(w, uri)
439 local view = webview()
440 w.tabs:append(view)
441 attach_webview_signals(w, view)
442 if uri then view.uri = uri end
443 view.show_scrollbars = false
444 w:update_tab_count()
445 end,
447 -- close the current tab
448 close_tab = function(w)
449 view = w:get_current()
450 if not view then return end
451 w.tabs:remove(view)
452 view:destroy()
453 w:update_tab_count()
454 end,
456 -- Wrapper around the bind plugin's hit method
457 hit = function (w, mods, key)
458 local caught, newbuf = bind.hit(w.binds or {}, mods, key, w.buffer, w:is_mode("normal"), w)
459 w.buffer = newbuf
460 w:update_buf()
461 return caught
462 end,
464 -- Wrapper around the bind plugin's match_cmd method
465 match_cmd = function (w, buffer)
466 return bind.match_cmd(commands, buffer, w)
467 end,
469 -- enter command or characters into command line
470 enter_cmd = function(w, cmd)
471 local i = w.ibar.input
472 w:set_mode("command")
473 i.text = cmd
474 i:set_position(-1)
475 end,
477 -- insert a string into the command line at the current cursor position
478 insert_cmd = function(w, str)
479 if not str then return nil end
480 local i = w.ibar.input
481 local text = i.text
482 local pos = i:get_position()
483 local left, right = string.sub(text, 1, pos), string.sub(text, pos+1)
484 i.text = left .. str .. right
485 i:set_position(pos + #str + 1)
486 end,
488 -- search engine wrapper
489 websearch = function(w, args)
490 local sep = string.find(args, " ")
491 local engine = string.sub(args, 1, sep-1)
492 local search = string.sub(args, sep+1)
493 if not search_engines[engine] then
494 print("E: No matching search engine found:", engine)
495 return 0
497 local uri = string.gsub(search_engines[engine], "{%d}", search)
498 return w:navigate(uri)
499 end,
501 -- Command line completion of available commands
502 cmd_completion = function(w)
503 local i = w.ibar.input
504 local s = w.sbar.l.uri
505 local cmpl = {}
507 -- Get last completion (is reset on key press other than <Tab>)
508 if not w.compl_start or w.compl_index == 0 then
509 w.compl_start = "^" .. string.sub(i.text, 2)
510 w.compl_index = 1
513 -- Get suitable commands
514 for _, b in ipairs(commands) do
515 for _, c in pairs(b.commands) do
516 if c and string.match(c, w.compl_start) then
517 table.insert(cmpl, c)
522 table.sort(cmpl)
524 if #cmpl > 0 then
525 local text = ""
526 for index, comp in pairs(cmpl) do
527 if index == w.compl_index then
528 i.text = ":" .. comp .. " "
529 i:set_position(-1)
531 if text ~= "" then
532 text = text .. " | "
534 text = text .. comp
537 -- cycle through all possible completions
538 if w.compl_index == #cmpl then
539 w.compl_index = 1
540 else
541 w.compl_index = w.compl_index + 1
543 s.text = text
545 end,
547 del_word = function(w)
548 local i = w.ibar.input
549 local text = i.text
550 local pos = i:get_position()
551 if text and #text > 1 and pos > 1 then
552 local left, right = string.sub(text, 2, pos), string.sub(text, pos+1)
553 if not string.find(left, "%s") then
554 left = ""
555 elseif string.find(left, "%w+%s*$") then
556 left = string.sub(left, 0, string.find(left, "%w+%s*$") - 1)
557 elseif string.find(left, "%W+%s*$") then
558 left = string.sub(left, 0, string.find(left, "%W+%s*$") - 1)
560 i.text = string.sub(text, 1, 1) .. left .. right
561 i:set_position(#left + 2)
563 end,
565 del_line = function(w)
566 local i = w.ibar.input
567 if i.text ~= ":" then
568 i.text = ":"
569 i:set_position(-1)
571 end,
573 -- Search history adding
574 srch_hist_add = function(w, srch)
575 if not w.srch_hist then w.srch_hist = {} end
576 -- Check overflow
577 if #w.srch_hist > ((MAX_SRCH_HISTORY or 100) + 5) then
578 while #w.srch_hist > (MAX_SRCH_HISTORY or 100) do
579 table.remove(w.srch_hist, 1)
582 table.insert(w.srch_hist, srch)
583 end,
585 -- Search history traversing
586 srch_hist_prev = function(w)
587 if not w.srch_hist then w.srch_hist = {} end
588 if not w.srch_hist_cursor then
589 w.srch_hist_cursor = #w.srch_hist + 1
590 w.srch_hist_current = w.ibar.input.text
592 local c = w.srch_hist_cursor - 1
593 if w.srch_hist[c] then
594 w.srch_hist_cursor = c
595 w.ibar.input.text = w.srch_hist[c]
596 w.ibar.input:set_position(-1)
598 end,
600 srch_hist_next = function(w)
601 if not w.srch_hist then w.srch_hist = {} end
602 local c = (w.srch_hist_cursor or #w.srch_hist) + 1
603 if w.srch_hist[c] then
604 w.srch_hist_cursor = c
605 w.ibar.input.text = w.srch_hist[c]
606 w.ibar.input:set_position(-1)
607 elseif w.srch_hist_current then
608 w.srch_hist_cursor = nil
609 w.ibar.input.text = w.srch_hist_current
610 w.ibar.input:set_position(-1)
612 end,
614 -- Command history adding
615 cmd_hist_add = function(w, cmd)
616 if not w.cmd_hist then w.cmd_hist = {} end
617 -- Make sure history doesn't overflow
618 if #w.cmd_hist > ((MAX_CMD_HISTORY or 100) + 5) then
619 while #w.cmd_hist > (MAX_CMD_HISTORY or 100) do
620 table.remove(w.cmd_hist, 1)
623 table.insert(w.cmd_hist, cmd)
624 end,
626 -- Command history traversing
627 cmd_hist_prev = function(w)
628 if not w.cmd_hist then w.cmd_hist = {} end
629 if not w.cmd_hist_cursor then
630 w.cmd_hist_cursor = #w.cmd_hist + 1
631 w.cmd_hist_current = w.ibar.input.text
633 local c = w.cmd_hist_cursor - 1
634 if w.cmd_hist[c] then
635 w.cmd_hist_cursor = c
636 w.ibar.input.text = w.cmd_hist[c]
637 w.ibar.input:set_position(-1)
639 end,
641 cmd_hist_next = function(w)
642 if not w.cmd_hist then w.cmd_hist = {} end
643 local c = (w.cmd_hist_cursor or #w.cmd_hist) + 1
644 if w.cmd_hist[c] then
645 w.cmd_hist_cursor = c
646 w.ibar.input.text = w.cmd_hist[c]
647 w.ibar.input:set_position(-1)
648 elseif w.cmd_hist_current then
649 w.cmd_hist_cursor = nil
650 w.ibar.input.text = w.cmd_hist_current
651 w.ibar.input:set_position(-1)
653 end,
655 -- Searching functions
656 start_search = function(w, forward)
657 -- Clear previous search results
658 w:clear_search()
659 w:set_mode("search")
660 local i = w.ibar.input
661 if forward then
662 i.text = "/"
663 else
664 i.text = "?"
666 i:focus()
667 i:set_position(-1)
668 end,
670 search = function(w, text, forward)
671 local view = w:get_current()
672 local text = text or w.last_search
673 if forward == nil then forward = true end
674 local case_sensitive = false
675 local wrap = true
677 if not text or #text == 0 then
678 w:clear_search()
679 return nil
682 w.last_search = text
683 if w.searching_forward == nil then
684 w.searching_forward = forward
685 w.search_start_marker = view:get_scroll_vert()
686 else
687 -- Invert the direction if originally searching in reverse
688 forward = (w.searching_forward == forward)
691 view:search(text, case_sensitive, forward, wrap);
692 end,
694 clear_search = function (w)
695 w:get_current():clear_search()
696 -- Clear search state
697 w.last_search = nil
698 w.searching_forward = nil
699 w.search_start_marker = nil
700 end,
702 -- Webview scroll functions
703 scroll_vert = function(w, value, view)
704 if not view then view = w:get_current() end
705 local cur, max = view:get_scroll_vert()
706 if type(value) == "string" then
707 value = parse_scroll(cur, max, value)
709 view:set_scroll_vert(value)
710 end,
712 scroll_horiz = function(w, value)
713 if not view then view = w:get_current() end
714 local cur, max = view:get_scroll_horiz()
715 if type(value) == "string" then
716 value = parse_scroll(cur, max, value)
718 view:set_scroll_horiz(value)
719 end,
721 -- Tab traversing functions
722 next_tab = function(w, n)
723 w.tabs:switch((((n or 1) + w.tabs:current() -1) % w.tabs:count()) + 1)
724 end,
725 prev_tab = function(w, n)
726 w.tabs:switch(((w.tabs:current() - (n or 1) -1) % w.tabs:count()) + 1)
727 end,
728 goto_tab = function(w, n)
729 w.tabs:switch(n)
730 end,
732 -- History traversing functions
733 back = function(w, n, view)
734 (view or w:get_current()):go_back(n or 1)
735 end,
736 forward = function(w, n, view)
737 (view or w:get_current()):go_forward(n or 1)
738 end,
740 -- GUI content update functions
741 update_tab_count = function (w, i, t)
742 w.sbar.r.tabi.text = string.format("[%d/%d]", i or w.tabs:current(), t or w.tabs:count())
743 end,
745 update_win_title = function (w, view)
746 if not view then view = w:get_current() end
747 local title = view:get_prop("title")
748 local uri = view.uri
749 if not title and not uri then
750 w.win.title = "luakit"
751 else
752 w.win.title = (title or "luakit") .. " - " .. (uri or "about:blank")
754 end,
756 update_uri = function (w, view, uri)
757 if not view then view = w:get_current() end
758 w.sbar.l.uri.text = (uri or view.uri or "about:blank")
759 end,
761 update_progress = function (w, view, p)
762 if not view then view = w:get_current() end
763 if not p then p = view:get_prop("progress") end
764 if not view:loading() or p == 1 then
765 w.sbar.l.loaded:hide()
766 else
767 w.sbar.l.loaded:show()
768 w.sbar.l.loaded.text = string.format("(%d%%)", p * 100)
770 end,
772 update_scroll = function (w, view)
773 if not view then view = w:get_current() end
774 local val, max = view:get_scroll_vert()
775 if max == 0 then val = "All"
776 elseif val == 0 then val = "Top"
777 elseif val == max then val = "Bot"
778 else val = string.format("%2d%%", (val/max) * 100)
780 w.sbar.r.scroll.text = val
781 end,
783 update_buf = function (w)
784 if w.buffer then
785 w.sbar.r.buf.text = string.format(" %-3s", w.buffer)
786 w.sbar.r.buf:show()
787 else
788 w.sbar.r.buf:hide()
790 end,
792 update_binds = function (w, mode)
793 -- Generate the list of active key & buffer binds for this mode
794 w.binds = util.table.join(mode_binds[mode], mode_binds.all)
795 -- Clear & hide buffer
796 w.buffer = nil
797 w:update_buf()
798 end,
800 -- Tab label functions
801 make_tab_label = function (w, pos)
802 local t = {
803 label = label(),
804 sep = label(),
805 ebox = eventbox(),
806 layout = hbox(),
808 t.label.font = theme.tablabel_font or theme.font
809 t.layout:pack_start(t.label, true, true, 0)
810 t.layout:pack_start(t.sep, false, false, 0)
811 t.ebox:set_child(t.layout)
812 t.ebox:add_signal("clicked", function(e) w.tabs:switch(pos) end)
813 return t
814 end,
816 destroy_tab_label = function(w, t)
817 if not t then t = table.remove(w.tbar.titles) end
818 for _, wi in pairs(t) do
819 wi:destroy()
821 end,
823 update_tab_labels = function (w, current)
824 local tb = w.tbar
825 local count, current = w.tabs:count(), current or w.tabs:current()
826 tb.ebox:hide()
828 -- Leave the tablist hidden if there is only one tab open
829 if count <= 1 then
830 return nil
833 if count ~= #tb.titles then
834 -- Grow the number of labels
835 while count > #tb.titles do
836 local t = w:make_tab_label(#tb.titles + 1)
837 tb.layout:pack_start(t.ebox, true, true, 0)
838 table.insert(tb.titles, t)
840 -- Prune number of labels
841 while count < #tb.titles do
842 w:destroy_tab_label()
846 if count ~= 0 then
847 for i = 1, count do
848 local t = tb.titles[i]
849 local title = " " ..i.. " "..w:get_tab_title(w.tabs:atindex(i))
850 t.label.text = string.format(theme.tablabel_format or "%s", title)
851 w:apply_tablabel_theme(t, i == current)
854 tb.ebox:show()
855 end,
857 -- Theme functions
858 apply_tablabel_theme = function (w, t, selected, atheme)
859 local theme = atheme or theme
860 if selected then
861 t.label.fg = theme.selected_tablabel_fg or theme.tablabel_fg or theme.fg
862 t.ebox.bg = theme.selected_tablabel_bg or theme.tablabel_bg or theme.bg
863 else
864 t.label.fg = theme.tablabel_fg or theme.fg
865 t.ebox.bg = theme.tablabel_bg or theme.bg
867 end,
869 apply_window_theme = function (w, atheme)
870 local theme = atheme or theme
871 local s, i, t = w.sbar, w.ibar, w.tbar
872 local fg, bg, font = theme.fg, theme.bg, theme.font
874 -- Set foregrounds
875 for wi, v in pairs({
876 [s.l.uri] = theme.uri_fg or theme.statusbar_fg or fg,
877 [s.l.loaded] = theme.loaded_fg or theme.statusbar_fg or fg,
878 [s.r.buf] = theme.buf_fg or theme.statusbar_fg or fg,
879 [s.r.tabi] = theme.tabi_fg or theme.statusbar_fg or fg,
880 [s.r.scroll] = theme.scroll_fg or theme.statusbar_fg or fg,
881 [i.prompt] = theme.prompt_fg or theme.inputbar_fg or fg,
882 [i.input] = theme.input_fg or theme.inputbar_fg or fg,
883 }) do wi.fg = v end
885 -- Set backgrounds
886 for wi, v in pairs({
887 [s.l.ebox] = theme.statusbar_bg or bg,
888 [s.r.ebox] = theme.statusbar_bg or bg,
889 [s.ebox] = theme.statusbar_bg or bg,
890 [i.ebox] = theme.inputbar_bg or bg,
891 [i.input] = theme.input_bg or theme.inputbar_bg or bg,
892 }) do wi.bg = v end
894 -- Set fonts
895 for wi, v in pairs({
896 [s.l.uri] = theme.uri_font or theme.statusbar_font or font,
897 [s.l.loaded] = theme.loaded_font or theme.statusbar_font or font,
898 [s.r.buf] = theme.buf_font or theme.statusbar_font or font,
899 [s.r.tabi] = theme.tabi_font or theme.statusbar_font or font,
900 [s.r.scroll] = theme.scroll_font or theme.statusbar_font or font,
901 [i.prompt] = theme.prompt_font or theme.inputbar_font or font,
902 [i.input] = theme.input_font or theme.inputbar_font or font,
903 }) do wi.font = v end
904 end,
907 -- Create new window
908 function new_window(uris)
909 local w = build_window()
911 -- Pack the window table full of the common helper functions
912 for k, v in pairs(window_helpers) do w[k] = v end
914 attach_window_signals(w)
916 -- Apply window theme
917 w:apply_window_theme()
919 -- Populate notebook with tabs
920 for _, uri in ipairs(uris or {}) do
921 w:new_tab(uri)
924 -- Make sure something is loaded
925 if w.tabs:count() == 0 then
926 w:new_tab(HOMEPAGE)
929 -- Set initial mode
930 w:set_mode()
932 return w
935 new_window(uris)
937 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80