Add `bind.but({mods}, num, func)` for mouse bindings & add mouse binds
[luakit.git] / rc.lua
blob6eaa8a71c0d12049c8aa395490ae0d752eb4ab5e
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
23 --HTTPPROXY = "http://example.com:3128"
25 -- Setup download directory
26 DOWNLOAD_DIR = luakit.get_special_dir("DOWNLOAD") or (os.getenv("HOME") .. "/downloads")
28 -- Luakit theme
29 theme = theme or {
30 -- Default settings
31 font = "monospace normal 9",
32 fg = "#fff",
33 bg = "#000",
35 -- General settings
36 statusbar_fg = "#fff",
37 statusbar_bg = "#000",
38 inputbar_fg = "#000",
39 inputbar_bg = "#fff",
41 -- Specific settings
42 loaded_fg = "#33AADD",
43 tablabel_fg = "#999",
44 tablabel_bg = "#111",
45 selected_tablabel_fg = "#fff",
46 selected_tablabel_bg = "#000",
48 -- Enforce a minimum tab width of 30 characters to prevent longer tab
49 -- titles overshadowing small tab titles when things get crowded.
50 tablabel_format = "%-30s",
53 widget.add_signal("new", function (wi)
54 wi:add_signal("init", function (wi)
55 if wi.type == "window" then
56 wi:add_signal("destroy", function ()
57 -- Call the quit function if this was the last window left
58 if #luakit.windows == 0 then luakit.quit() end
59 end)
60 end
61 end)
62 end)
64 -- Search engines
65 search_engines = {
66 google = "http://google.com/search?q={0}",
67 imdb = "http://imdb.com/find?s=all&q={0}",
68 sourceforge = "http://sf.net/search/?words={0}",
71 -- Add key bindings to be used across all windows
72 mode_binds = {
73 -- bind.buf(Pattern, function (w, buffer, opts) .. end, opts),
74 -- bind.key({Modifiers}, Key name, function (w, opts) .. end, opts),
75 -- bind.but({Modifiers}, Button Number, function (w, opts) .. end, opts),
76 all = {
77 bind.key({}, "Escape", function (w) w:set_mode() end),
78 bind.key({"Control"}, "[", function (w) w:set_mode() end),
80 -- Mouse bindings
81 bind.but({}, 2, function (w)
82 -- Open hovered uri in new tab
83 local uri = w:get_current().hovered_uri
84 if uri then w:new_tab(uri)
85 else -- Open selection in current tab
86 uri = luakit.selection()
87 if uri then w:get_current().uri = uri end
88 end
89 end),
90 bind.but({}, 8, function (w) w:back() end),
91 bind.but({}, 9, function (w) w:forward() end),
93 normal = {
94 bind.key({}, "i", function (w) w:set_mode("insert") end),
95 bind.key({}, ":", function (w) w:set_mode("command") end),
97 -- Scrolling
98 bind.key({}, "h", function (w) w:scroll_horiz("-"..SCROLL_STEP.."px") end),
99 bind.key({}, "j", function (w) w:scroll_vert ("+"..SCROLL_STEP.."px") end),
100 bind.key({}, "k", function (w) w:scroll_vert ("-"..SCROLL_STEP.."px") end),
101 bind.key({}, "l", function (w) w:scroll_horiz("+"..SCROLL_STEP.."px") end),
102 bind.key({}, "Left", function (w) w:scroll_horiz("-"..SCROLL_STEP.."px") end),
103 bind.key({}, "Down", function (w) w:scroll_vert ("+"..SCROLL_STEP.."px") end),
104 bind.key({}, "Up", function (w) w:scroll_vert ("-"..SCROLL_STEP.."px") end),
105 bind.key({}, "Right", function (w) w:scroll_horiz("+"..SCROLL_STEP.."px") end),
106 bind.buf("^gg$", function (w) w:scroll_vert("0%") end),
107 bind.buf("^G$", function (w) w:scroll_vert("100%") end),
108 bind.buf("^[\-\+]?[0-9]+[%%G]$", function (w, b) w:scroll_vert(string.match(b, "^([\-\+]?%d+)[%%G]$") .. "%") end),
110 -- Clipboard
111 bind.key({}, "p", function (w) w:navigate(luakit.selection()) end),
112 bind.key({}, "P", function (w) w:new_tab(luakit.selection()) end),
114 -- Commands
115 bind.buf("^o$", function (w, c) w:enter_cmd(":open ") end),
116 bind.buf("^t$", function (w, c) w:enter_cmd(":tabopen ") end),
117 bind.buf("^,g$", function (w, c) w:enter_cmd(":websearch google ") end),
119 -- Searching
120 bind.key({}, "/", function (w) w:start_search(true) end),
121 bind.key({}, "?", function (w) w:start_search(false) end),
122 bind.key({}, "n", function (w) w:search(nil, true) end),
123 bind.key({}, "N", function (w) w:search(nil, false) end),
125 -- History
126 bind.buf("^[0-9]*H$", function (w, b) w:back (tonumber(string.match(b, "^(%d*)H$") or 1)) end),
127 bind.buf("^[0-9]*L$", function (w, b) w:forward(tonumber(string.match(b, "^(%d*)L$") or 1)) end),
129 -- Tab
130 bind.buf("^[0-9]*gT$", function (w, b) w:prev_tab(tonumber(string.match(b, "^(%d*)gT$") or 1)) end),
131 bind.buf("^[0-9]*gt$", function (w, b) w:next_tab(tonumber(string.match(b, "^(%d*)gt$") or 1)) end),
132 bind.buf("^gH$", function (w) w:new_tab(HOMEPAGE) end),
133 bind.buf("^d$", function (w) w:close_tab() end),
135 bind.key({}, "r", function (w) w:reload() end),
136 bind.buf("^gh$", function (w) w:navigate(HOMEPAGE) end),
137 bind.buf("^ZZ$", function (w) luakit.quit() end),
139 -- Link following
140 bind.key({}, "f", function (w) w:set_mode("follow") end),
143 command = {
144 bind.key({"Shift"}, "Insert", function (w) w:insert_cmd(luakit.selection()) end),
145 bind.key({}, "Up", function (w) w:cmd_hist_prev() end),
146 bind.key({}, "Down", function (w) w:cmd_hist_next() end),
147 bind.key({}, "Tab", function (w) w:cmd_completion() end),
148 bind.key({"Control"}, "w", function (w) w:del_word() end),
149 bind.key({"Control"}, "u", function (w) w:del_line() end),
151 search = {
152 bind.key({}, "Up", function (w) w:srch_hist_prev() end),
153 bind.key({}, "Down", function (w) w:srch_hist_next() end),
155 insert = { },
158 -- Commands
159 commands = {
160 -- bind.cmd({Command, Alias1, ...}, function (w, arg, opts) .. end, opts),
161 bind.cmd({"open", "o" }, function (w, a) w:navigate(a) end),
162 bind.cmd({"tabopen", "t" }, function (w, a) w:new_tab(a) end),
163 bind.cmd({"back" }, function (w, a) w:back(tonumber(a) or 1) end),
164 bind.cmd({"forward", "f" }, function (w, a) w:forward(tonumber(a) or 1) end),
165 bind.cmd({"scroll" }, function (w, a) w:scroll_vert(a) end),
166 bind.cmd({"quit", "q" }, function (w) luakit.quit() end),
167 bind.cmd({"close", "c" }, function (w) w:close_tab() end),
168 bind.cmd({"websearch", "ws"}, function (w, e, s) w:websearch(e, s) end),
169 bind.cmd({"reload", }, function (w) w:reload() end),
172 function set_http_options(w)
173 local proxy = HTTPPROXY or os.getenv("http_proxy")
174 if proxy then w:set('proxy-uri', proxy) end
175 w:set('user-agent', 'luakit')
176 -- Uncomment the following options if you want to enable SSL certs validation.
177 -- w:set('ssl-ca-file', '/etc/certs/ca-certificates.crt')
178 -- w:set('ssl-strict', true)
181 -- Build and pack window widgets
182 function build_window()
183 -- Create a table for widgets and state variables for a window
184 local w = {
185 win = window(),
186 ebox = eventbox(),
187 layout = vbox(),
188 tabs = notebook(),
189 -- Tab bar widgets
190 tbar = {
191 layout = hbox(),
192 ebox = eventbox(),
193 titles = { },
195 -- Status bar widgets
196 sbar = {
197 layout = hbox(),
198 ebox = eventbox(),
199 -- Left aligned widgets
200 l = {
201 layout = hbox(),
202 ebox = eventbox(),
203 uri = label(),
204 loaded = label(),
206 -- Fills space between the left and right aligned widgets
207 filler = label(),
208 -- Right aligned widgets
209 r = {
210 layout = hbox(),
211 ebox = eventbox(),
212 buf = label(),
213 tabi = label(),
214 scroll = label(),
217 -- Input bar widgets
218 ibar = {
219 layout = hbox(),
220 ebox = eventbox(),
221 prompt = label(),
222 input = entry(),
226 -- Assemble window
227 w.ebox:set_child(w.layout)
228 w.win:set_child(w.ebox)
230 -- Pack tab bar
231 local t = w.tbar
232 t.ebox:set_child(t.layout, false, false, 0)
233 w.layout:pack_start(t.ebox, false, false, 0)
235 -- Pack notebook
236 w.layout:pack_start(w.tabs, true, true, 0)
238 -- Pack left-aligned statusbar elements
239 local l = w.sbar.l
240 l.layout:pack_start(l.uri, false, false, 0)
241 l.layout:pack_start(l.loaded, false, false, 0)
242 l.ebox:set_child(l.layout)
244 -- Pack right-aligned statusbar elements
245 local r = w.sbar.r
246 r.layout:pack_start(r.buf, false, false, 0)
247 r.layout:pack_start(r.tabi, false, false, 0)
248 r.layout:pack_start(r.scroll, false, false, 0)
249 r.ebox:set_child(r.layout)
251 -- Pack status bar elements
252 local s = w.sbar
253 s.layout:pack_start(l.ebox, false, false, 0)
254 s.layout:pack_start(s.filler, true, true, 0)
255 s.layout:pack_start(r.ebox, false, false, 0)
256 s.ebox:set_child(s.layout)
257 w.layout:pack_start(s.ebox, false, false, 0)
259 -- Pack input bar
260 local i = w.ibar
261 i.layout:pack_start(i.prompt, false, false, 0)
262 i.layout:pack_start(i.input, true, true, 0)
263 i.ebox:set_child(i.layout)
264 w.layout:pack_start(i.ebox, false, false, 0)
266 -- Other settings
267 i.input.show_frame = false
268 w.tabs.show_tabs = false
269 l.loaded:hide()
270 l.uri.selectable = true
272 return w
275 function attach_window_signals(w)
276 -- Attach notebook widget signals
277 w.tabs:add_signal("page-added", function (nbook, view, idx)
278 w:update_tab_count(idx)
279 w:update_tab_labels()
280 end)
282 w.tabs:add_signal("switch-page", function (nbook, view, idx)
283 w:update_tab_count(idx)
284 w:update_win_title(view)
285 w:update_uri(view)
286 w:update_progress(view)
287 w:update_tab_labels(idx)
288 end)
290 -- Attach window widget signals
291 w.win:add_signal("key-press", function (win, mods, key)
292 -- Reset command line completion
293 if w:get_mode() == "command" and key ~= "Tab" and w.compl_start then
294 w:update_uri()
295 w.compl_index = 0
298 if w:hit(mods, key) then
299 return true
301 end)
303 w.win:add_signal("mode-changed", function (win, mode)
304 local i, p = w.ibar.input, w.ibar.prompt
306 w:update_binds(mode)
307 w.cmd_hist_cursor = nil
309 -- Clear following hints if the user exits follow mode
310 if w.showing_hints then
311 w:eval_js("clear();");
312 w.showing_hints = false
315 -- If a user aborts a search return to the original position
316 if w.search_start_marker then
317 w:get_current():set_scroll_vert(w.search_start_marker)
318 w.search_start_marker = nil
321 if mode == "normal" then
322 p:hide()
323 i:hide()
324 elseif mode == "insert" then
325 i:hide()
326 i.text = ""
327 p.text = "-- INSERT --"
328 p:show()
329 elseif mode == "command" then
330 p:hide()
331 i.text = ":"
332 i:show()
333 i:focus()
334 i:set_position(-1)
335 elseif mode == "search" then
336 p:hide()
337 i:show()
338 elseif mode == "follow" then
339 w:eval_js_from_file(util.find_data("scripts/follow.js"))
340 w:eval_js("clear(); show_hints();")
341 w.showing_hints = true
342 p.text = "Follow:"
343 p:show()
344 i.text = ""
345 i:show()
346 i:focus()
347 i:set_position(-1)
348 else
349 w.ibar.prompt.text = ""
350 w.ibar.input.text = ""
352 end)
354 -- Attach inputbar widget signals
355 w.ibar.input:add_signal("changed", function()
356 local text = w.ibar.input.text
357 -- Auto-exit "command" mode if you backspace or delete the ":"
358 -- character at the start of the input box when in "command" mode.
359 if w:is_mode("command") and not string.match(text, "^:") then
360 w:set_mode()
361 elseif w:is_mode("search") then
362 if string.match(text, "^[\?\/]") then
363 w:search(string.sub(text, 2), (string.sub(text, 1, 1) == "/"))
364 else
365 w:clear_search()
366 w:set_mode()
368 elseif w:is_mode("follow") then
369 w:eval_js(string.format("update(%q)", w.ibar.input.text))
371 end)
373 w.ibar.input:add_signal("activate", function()
374 local text = w.ibar.input.text
375 if w:is_mode("command") then
376 w:cmd_hist_add(text)
377 w:match_cmd(string.sub(text, 2))
378 w:set_mode()
379 elseif w:is_mode("search") then
380 w:srch_hist_add(text)
381 w:search(string.sub(text, 2), string.sub(text, 1, 1) == "/")
382 -- User doesn't want to return to start position
383 w.search_start_marker = nil
384 w:set_mode()
385 w.ibar.prompt.text = util.escape(text)
386 w.ibar.prompt:show()
388 end)
391 -- Attach signal handlers to a new tab's webview
392 function attach_webview_signals(w, view)
393 view:add_signal("title-changed", function (v)
394 w:update_tab_labels()
395 if w:is_current(v) then
396 w:update_win_title(v)
398 end)
400 view:add_signal("property::uri", function (v)
401 w:update_tab_labels()
402 if w:is_current(v) then
403 w:update_uri(v)
405 end)
407 view:add_signal("link-hover", function (v, link)
408 if w:is_current(v) and link then
409 w.sbar.l.uri.text = "Link: " .. util.escape(link)
411 end)
413 view:add_signal("link-unhover", function (v)
414 if w:is_current(v) then
415 w:update_uri(v)
417 end)
419 view:add_signal("form-active", function ()
420 w:set_mode("insert")
421 end)
423 view:add_signal("root-active", function ()
424 w:set_mode()
425 end)
427 view:add_signal("key-press", function ()
428 -- Only allow key press events to hit the webview if the user is in
429 -- "insert" mode.
430 if not w:is_mode("insert") then
431 return true
433 end)
435 view:add_signal("button-release", function (v, mods, button)
436 if w:hit(mods, button) then
437 return true
439 end)
441 view:add_signal("load-status", function (v, status)
442 if w:is_current(v) then
443 w:update_progress(v)
444 if status == "provisional" then
445 w:set_mode()
448 end)
450 -- 'link' contains the download link
451 -- 'mime' contains the mime type that is requested
452 -- return TRUE to accept or FALSE to reject
453 view:add_signal("mime-type-decision", function (v, link, mime)
454 if w:is_current(v) then
455 if luakit.verbose then print(string.format("Requested link: %s (%s)", link, mime)) end
457 -- i.e. block binary files like *.exe
458 if string.match(mime, "application/octet-stream") then
459 return false
462 end)
464 -- 'link' contains the download link
465 -- 'filename' contains the suggested filename (from server or webkit)
466 view:add_signal("download-request", function (v, link, filename)
467 if w:is_current(v) and filename then
468 -- Make download dir
469 os.execute(string.format("mkdir -p %q", DOWNLOAD_DIR))
471 local dl = DOWNLOAD_DIR .. "/" .. filename
472 local wget = string.format("wget -q %q -O %q &", link, dl)
473 if luakit.verbose then print("Launching: " .. wget) end
474 os.execute(wget)
476 end)
478 view:add_signal("property::progress", function (v)
479 if w:is_current(v) then
480 w:update_progress(v)
482 end)
484 view:add_signal("expose", function (v)
485 if w:is_current(v) then
486 w:update_scroll(v)
488 end)
491 -- Parses scroll amounts of the form:
492 -- Relative: "+20%", "-20%", "+20px", "-20px"
493 -- Absolute: 20, "20%", "20px"
494 -- And returns an absolute value.
495 function parse_scroll(current, max, value)
496 if string.match(value, "^%d+px$") then
497 return tonumber(string.match(value, "^(%d+)px$"))
498 elseif string.match(value, "^%d+%%$") then
499 return math.ceil(max * (tonumber(string.match(value, "^(%d+)%%$")) / 100))
500 elseif string.match(value, "^[\-\+]%d+px") then
501 return current + tonumber(string.match(value, "^([\-\+]%d+)px"))
502 elseif string.match(value, "^[\-\+]%d+%%$") then
503 return math.ceil(current + (max * (tonumber(string.match(value, "^([\-\+]%d+)%%$")) / 100)))
504 else
505 print("E: unable to parse scroll amount:", value)
509 -- Helper functions which operate on a windows widget structure
510 window_helpers = {
511 -- Return the widget in the currently active tab
512 get_current = function (w) return w.tabs:atindex(w.tabs:current()) end,
513 -- Check if given widget is the widget in the currently active tab
514 is_current = function (w, wi) return w.tabs:indexof(wi) == w.tabs:current() end,
516 -- Wrappers around the mode plugin
517 set_mode = function (w, name) mode.set(w.win, name) end,
518 get_mode = function (w) return mode.get(w.win) end,
519 is_mode = function (w, name) return name == w:get_mode() end,
520 is_any_mode = function (w, t, name) return util.table.hasitem(t, name or w:get_mode()) end,
522 -- Wrappers around the view:get_prop & view:set_prop methods
523 get = function (w, prop, view)
524 if not view then view = w:get_current() end
525 return view:get_prop(prop)
526 end,
528 set = function (w, prop, val, view)
529 if not view then view = w:get_current() end
530 view:set_prop(prop, val)
531 end,
533 get_tab_title = function (w, view)
534 if not view then view = w:get_current() end
535 return view:get_prop("title") or view.uri or "(Untitled)"
536 end,
538 navigate = function (w, uri, view)
539 local v = view or w:get_current()
540 if v then
541 v.uri = uri
542 else
543 return w:new_tab(uri)
545 end,
547 reload = function (w, view)
548 if not view then view = w:get_current() end
549 view:reload()
550 end,
552 new_tab = function (w, uri)
553 local view = webview()
554 w.tabs:append(view)
555 set_http_options(w)
556 attach_webview_signals(w, view)
557 if uri then view.uri = uri end
558 view.show_scrollbars = false
559 w:update_tab_count()
560 end,
562 -- close the current tab
563 close_tab = function (w)
564 view = w:get_current()
565 if not view then return end
566 w.tabs:remove(view)
567 view:destroy()
568 w:update_tab_count()
569 end,
571 -- evaluate javascript code and return string result
572 eval_js = function (w, script, file, view)
573 if not view then view = w:get_current() end
574 return view:eval_js(script, file or "(buffer)")
575 end,
577 -- evaluate javascript code from file and return string result
578 eval_js_from_file = function (w, file, view)
579 local fh, err = io.open(file)
580 if not fh then return error(err) end
581 local script = fh:read("*a")
582 fh:close()
583 return w:eval_js(script, file, view)
584 end,
586 -- Wrapper around the bind plugin's hit method
587 hit = function (w, mods, key)
588 local caught, newbuf = bind.hit(w.binds or {}, mods, key, w.buffer, w:is_mode("normal"), w)
589 w.buffer = newbuf
590 w:update_buf()
591 return caught
592 end,
594 -- Wrapper around the bind plugin's match_cmd method
595 match_cmd = function (w, buffer)
596 return bind.match_cmd(commands, buffer, w)
597 end,
599 -- enter command or characters into command line
600 enter_cmd = function (w, cmd)
601 local i = w.ibar.input
602 w:set_mode("command")
603 i.text = cmd
604 i:set_position(-1)
605 end,
607 -- insert a string into the command line at the current cursor position
608 insert_cmd = function (w, str)
609 if not str then return nil end
610 local i = w.ibar.input
611 local text = i.text
612 local pos = i:get_position()
613 local left, right = string.sub(text, 1, pos), string.sub(text, pos+1)
614 i.text = left .. str .. right
615 i:set_position(pos + #str + 1)
616 end,
618 -- search engine wrapper
619 websearch = function (w, args)
620 local sep = string.find(args, " ")
621 local engine = string.sub(args, 1, sep-1)
622 local search = string.sub(args, sep+1)
623 if not search_engines[engine] then
624 print("E: No matching search engine found:", engine)
625 return 0
627 local uri = string.gsub(search_engines[engine], "{%d}", search)
628 return w:navigate(uri)
629 end,
631 -- Command line completion of available commands
632 cmd_completion = function (w)
633 local i = w.ibar.input
634 local s = w.sbar.l.uri
635 local cmpl = {}
637 -- Get last completion (is reset on key press other than <Tab>)
638 if not w.compl_start or w.compl_index == 0 then
639 w.compl_start = "^" .. string.sub(i.text, 2)
640 w.compl_index = 1
643 -- Get suitable commands
644 for _, b in ipairs(commands) do
645 for _, c in pairs(b.commands) do
646 if c and string.match(c, w.compl_start) then
647 table.insert(cmpl, c)
652 table.sort(cmpl)
654 if #cmpl > 0 then
655 local text = ""
656 for index, comp in pairs(cmpl) do
657 if index == w.compl_index then
658 i.text = ":" .. comp .. " "
659 i:set_position(-1)
661 if text ~= "" then
662 text = text .. " | "
664 text = text .. comp
667 -- cycle through all possible completions
668 if w.compl_index == #cmpl then
669 w.compl_index = 1
670 else
671 w.compl_index = w.compl_index + 1
673 s.text = util.escape(text)
675 end,
677 del_word = function (w)
678 local i = w.ibar.input
679 local text = i.text
680 local pos = i:get_position()
681 if text and #text > 1 and pos > 1 then
682 local left, right = string.sub(text, 2, pos), string.sub(text, pos+1)
683 if not string.find(left, "%s") then
684 left = ""
685 elseif string.find(left, "%w+%s*$") then
686 left = string.sub(left, 0, string.find(left, "%w+%s*$") - 1)
687 elseif string.find(left, "%W+%s*$") then
688 left = string.sub(left, 0, string.find(left, "%W+%s*$") - 1)
690 i.text = string.sub(text, 1, 1) .. left .. right
691 i:set_position(#left + 2)
693 end,
695 del_line = function (w)
696 local i = w.ibar.input
697 if i.text ~= ":" then
698 i.text = ":"
699 i:set_position(-1)
701 end,
703 -- Search history adding
704 srch_hist_add = function (w, srch)
705 if not w.srch_hist then w.srch_hist = {} end
706 -- Check overflow
707 if #w.srch_hist > ((MAX_SRCH_HISTORY or 100) + 5) then
708 while #w.srch_hist > (MAX_SRCH_HISTORY or 100) do
709 table.remove(w.srch_hist, 1)
712 table.insert(w.srch_hist, srch)
713 end,
715 -- Search history traversing
716 srch_hist_prev = function (w)
717 if not w.srch_hist then w.srch_hist = {} end
718 if not w.srch_hist_cursor then
719 w.srch_hist_cursor = #w.srch_hist + 1
720 w.srch_hist_current = w.ibar.input.text
722 local c = w.srch_hist_cursor - 1
723 if w.srch_hist[c] then
724 w.srch_hist_cursor = c
725 w.ibar.input.text = w.srch_hist[c]
726 w.ibar.input:set_position(-1)
728 end,
730 srch_hist_next = function (w)
731 if not w.srch_hist then w.srch_hist = {} end
732 local c = (w.srch_hist_cursor or #w.srch_hist) + 1
733 if w.srch_hist[c] then
734 w.srch_hist_cursor = c
735 w.ibar.input.text = w.srch_hist[c]
736 w.ibar.input:set_position(-1)
737 elseif w.srch_hist_current then
738 w.srch_hist_cursor = nil
739 w.ibar.input.text = w.srch_hist_current
740 w.ibar.input:set_position(-1)
742 end,
744 -- Command history adding
745 cmd_hist_add = function (w, cmd)
746 if not w.cmd_hist then w.cmd_hist = {} end
747 -- Make sure history doesn't overflow
748 if #w.cmd_hist > ((MAX_CMD_HISTORY or 100) + 5) then
749 while #w.cmd_hist > (MAX_CMD_HISTORY or 100) do
750 table.remove(w.cmd_hist, 1)
753 table.insert(w.cmd_hist, cmd)
754 end,
756 -- Command history traversing
757 cmd_hist_prev = function (w)
758 if not w.cmd_hist then w.cmd_hist = {} end
759 if not w.cmd_hist_cursor then
760 w.cmd_hist_cursor = #w.cmd_hist + 1
761 w.cmd_hist_current = w.ibar.input.text
763 local c = w.cmd_hist_cursor - 1
764 if w.cmd_hist[c] then
765 w.cmd_hist_cursor = c
766 w.ibar.input.text = w.cmd_hist[c]
767 w.ibar.input:set_position(-1)
769 end,
771 cmd_hist_next = function (w)
772 if not w.cmd_hist then w.cmd_hist = {} end
773 local c = (w.cmd_hist_cursor or #w.cmd_hist) + 1
774 if w.cmd_hist[c] then
775 w.cmd_hist_cursor = c
776 w.ibar.input.text = w.cmd_hist[c]
777 w.ibar.input:set_position(-1)
778 elseif w.cmd_hist_current then
779 w.cmd_hist_cursor = nil
780 w.ibar.input.text = w.cmd_hist_current
781 w.ibar.input:set_position(-1)
783 end,
785 -- Searching functions
786 start_search = function (w, forward)
787 -- Clear previous search results
788 w:clear_search()
789 w:set_mode("search")
790 local i = w.ibar.input
791 if forward then
792 i.text = "/"
793 else
794 i.text = "?"
796 i:focus()
797 i:set_position(-1)
798 end,
800 search = function (w, text, forward)
801 local view = w:get_current()
802 local text = text or w.last_search
803 if forward == nil then forward = true end
804 local case_sensitive = false
805 local wrap = true
807 if not text or #text == 0 then
808 w:clear_search()
809 return nil
812 w.last_search = text
813 if w.searching_forward == nil then
814 w.searching_forward = forward
815 w.search_start_marker = view:get_scroll_vert()
816 else
817 -- Invert the direction if originally searching in reverse
818 forward = (w.searching_forward == forward)
821 view:search(text, case_sensitive, forward, wrap);
822 end,
824 clear_search = function (w)
825 w:get_current():clear_search()
826 -- Clear search state
827 w.last_search = nil
828 w.searching_forward = nil
829 w.search_start_marker = nil
830 end,
832 -- Webview scroll functions
833 scroll_vert = function(w, value, view)
834 if not view then view = w:get_current() end
835 local cur, max = view:get_scroll_vert()
836 if type(value) == "string" then
837 value = parse_scroll(cur, max, value)
839 view:set_scroll_vert(value)
840 end,
842 scroll_horiz = function(w, value)
843 if not view then view = w:get_current() end
844 local cur, max = view:get_scroll_horiz()
845 if type(value) == "string" then
846 value = parse_scroll(cur, max, value)
848 view:set_scroll_horiz(value)
849 end,
851 -- Tab traversing functions
852 next_tab = function (w, n)
853 w.tabs:switch((((n or 1) + w.tabs:current() -1) % w.tabs:count()) + 1)
854 end,
855 prev_tab = function (w, n)
856 w.tabs:switch(((w.tabs:current() - (n or 1) -1) % w.tabs:count()) + 1)
857 end,
858 goto_tab = function (w, n)
859 w.tabs:switch(n)
860 end,
862 -- History traversing functions
863 back = function (w, n, view)
864 (view or w:get_current()):go_back(n or 1)
865 end,
866 forward = function (w, n, view)
867 (view or w:get_current()):go_forward(n or 1)
868 end,
870 -- GUI content update functions
871 update_tab_count = function (w, i, t)
872 w.sbar.r.tabi.text = string.format("[%d/%d]", i or w.tabs:current(), t or w.tabs:count())
873 end,
875 update_win_title = function (w, view)
876 if not view then view = w:get_current() end
877 local title = view:get_prop("title")
878 local uri = view.uri
879 if not title and not uri then
880 w.win.title = "luakit"
881 else
882 w.win.title = (title or "luakit") .. " - " .. (uri or "about:blank")
884 end,
886 update_uri = function (w, view, uri)
887 if not view then view = w:get_current() end
888 w.sbar.l.uri.text = util.escape((uri or (view and view.uri) or "about:blank"))
889 end,
891 update_progress = function (w, view, p)
892 if not view then view = w:get_current() end
893 if not p then p = view:get_prop("progress") end
894 if not view:loading() or p == 1 then
895 w.sbar.l.loaded:hide()
896 else
897 w.sbar.l.loaded:show()
898 w.sbar.l.loaded.text = string.format("(%d%%)", p * 100)
900 end,
902 update_scroll = function (w, view)
903 if not view then view = w:get_current() end
904 local val, max = view:get_scroll_vert()
905 if max == 0 then val = "All"
906 elseif val == 0 then val = "Top"
907 elseif val == max then val = "Bot"
908 else val = string.format("%2d%%", (val/max) * 100)
910 w.sbar.r.scroll.text = val
911 end,
913 update_buf = function (w)
914 if w.buffer then
915 w.sbar.r.buf.text = util.escape(string.format(" %-3s", w.buffer))
916 w.sbar.r.buf:show()
917 else
918 w.sbar.r.buf:hide()
920 end,
922 update_binds = function (w, mode)
923 -- Generate the list of active key & buffer binds for this mode
924 w.binds = util.table.join(mode_binds[mode], mode_binds.all)
925 -- Clear & hide buffer
926 w.buffer = nil
927 w:update_buf()
928 end,
930 -- Tab label functions
931 make_tab_label = function (w, pos)
932 local t = {
933 label = label(),
934 sep = label(),
935 ebox = eventbox(),
936 layout = hbox(),
938 t.label.font = theme.tablabel_font or theme.font
939 t.layout:pack_start(t.label, true, true, 0)
940 t.layout:pack_start(t.sep, false, false, 0)
941 t.ebox:set_child(t.layout)
942 t.ebox:add_signal("button-press", function (e, m, b)
943 if b == 1 then
944 w.tabs:switch(pos)
945 return true
947 end)
948 return t
949 end,
951 destroy_tab_label = function (w, t)
952 if not t then t = table.remove(w.tbar.titles) end
953 for _, wi in pairs(t) do
954 wi:destroy()
956 end,
958 update_tab_labels = function (w, current)
959 local tb = w.tbar
960 local count, current = w.tabs:count(), current or w.tabs:current()
961 tb.ebox:hide()
963 -- Leave the tablist hidden if there is only one tab open
964 if count <= 1 then
965 return nil
968 if count ~= #tb.titles then
969 -- Grow the number of labels
970 while count > #tb.titles do
971 local t = w:make_tab_label(#tb.titles + 1)
972 tb.layout:pack_start(t.ebox, true, true, 0)
973 table.insert(tb.titles, t)
975 -- Prune number of labels
976 while count < #tb.titles do
977 w:destroy_tab_label()
981 if count ~= 0 then
982 for i = 1, count do
983 local t = tb.titles[i]
984 local title = " " ..i.. " "..w:get_tab_title(w.tabs:atindex(i))
985 t.label.text = util.escape(string.format(theme.tablabel_format or "%s", title))
986 w:apply_tablabel_theme(t, i == current)
989 tb.ebox:show()
990 end,
992 -- Theme functions
993 apply_tablabel_theme = function (w, t, selected, atheme)
994 local theme = atheme or theme
995 if selected then
996 t.label.fg = theme.selected_tablabel_fg or theme.tablabel_fg or theme.fg
997 t.ebox.bg = theme.selected_tablabel_bg or theme.tablabel_bg or theme.bg
998 else
999 t.label.fg = theme.tablabel_fg or theme.fg
1000 t.ebox.bg = theme.tablabel_bg or theme.bg
1002 end,
1004 apply_window_theme = function (w, atheme)
1005 local theme = atheme or theme
1006 local s, i, t = w.sbar, w.ibar, w.tbar
1007 local fg, bg, font = theme.fg, theme.bg, theme.font
1009 -- Set foregrounds
1010 for wi, v in pairs({
1011 [s.l.uri] = theme.uri_fg or theme.statusbar_fg or fg,
1012 [s.l.loaded] = theme.loaded_fg or theme.statusbar_fg or fg,
1013 [s.r.buf] = theme.buf_fg or theme.statusbar_fg or fg,
1014 [s.r.tabi] = theme.tabi_fg or theme.statusbar_fg or fg,
1015 [s.r.scroll] = theme.scroll_fg or theme.statusbar_fg or fg,
1016 [i.prompt] = theme.prompt_fg or theme.inputbar_fg or fg,
1017 [i.input] = theme.input_fg or theme.inputbar_fg or fg,
1018 }) do wi.fg = v end
1020 -- Set backgrounds
1021 for wi, v in pairs({
1022 [s.l.ebox] = theme.statusbar_bg or bg,
1023 [s.r.ebox] = theme.statusbar_bg or bg,
1024 [s.ebox] = theme.statusbar_bg or bg,
1025 [i.ebox] = theme.inputbar_bg or bg,
1026 [i.input] = theme.input_bg or theme.inputbar_bg or bg,
1027 }) do wi.bg = v end
1029 -- Set fonts
1030 for wi, v in pairs({
1031 [s.l.uri] = theme.uri_font or theme.statusbar_font or font,
1032 [s.l.loaded] = theme.loaded_font or theme.statusbar_font or font,
1033 [s.r.buf] = theme.buf_font or theme.statusbar_font or font,
1034 [s.r.tabi] = theme.tabi_font or theme.statusbar_font or font,
1035 [s.r.scroll] = theme.scroll_font or theme.statusbar_font or font,
1036 [i.prompt] = theme.prompt_font or theme.inputbar_font or font,
1037 [i.input] = theme.input_font or theme.inputbar_font or font,
1038 }) do wi.font = v end
1039 end,
1042 -- Create new window
1043 function new_window(uris)
1044 local w = build_window()
1046 -- Pack the window table full of the common helper functions
1047 for k, v in pairs(window_helpers) do w[k] = v end
1049 attach_window_signals(w)
1051 -- Apply window theme
1052 w:apply_window_theme()
1054 -- Populate notebook with tabs
1055 for _, uri in ipairs(uris or {}) do
1056 w:new_tab(uri)
1059 -- Make sure something is loaded
1060 if w.tabs:count() == 0 then
1061 w:new_tab(HOMEPAGE)
1064 -- Set initial mode
1065 w:set_mode()
1067 return w
1070 new_window(uris)
1072 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80