1 -- Luakit configuration file, more information at http://luakit.org/
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"
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
32 ["enable-scripts"] = false,
33 ["enable-plugins"] = false,
34 ["enable-private-browsing"] = false,
35 ["user-stylesheet-uri"] = "",
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,
50 font
= "monospace normal 9",
55 statusbar_fg
= "#fff",
56 statusbar_bg
= "#000",
61 loaded_fg
= "#33AADD",
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
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
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),
102 bind
.key({}, "Escape", function (w
) w
:set_mode() end),
103 bind
.key({"Control"}, "[", function (w
) w
:set_mode() end),
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
115 bind
.but({}, 8, function (w
) w
:back() end),
116 bind
.but({}, 9, function (w
) w
:forward() end),
119 bind
.key({}, "i", function (w
) w
:set_mode("insert") end),
120 bind
.key({}, ":", function (w
) w
:set_mode("command") end),
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),
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),
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),
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),
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),
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),
171 bind
.key({}, "f", function (w
) w
:set_mode("follow") end),
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),
183 bind
.key({}, "Up", function (w
) w
:srch_hist_prev() end),
184 bind
.key({}, "Down", function (w
) w
:srch_hist_next() end),
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
228 -- Status bar widgets
232 -- Left aligned widgets
239 -- Fills space between the left and right aligned widgets
241 -- Right aligned widgets
260 w
.ebox
:set_child(w
.layout
)
261 w
.win
:set_child(w
.ebox
)
265 t
.ebox
:set_child(t
.layout
, false, false, 0)
266 w
.layout
:pack_start(t
.ebox
, false, false, 0)
269 w
.layout
:pack_start(w
.tabs
, true, true, 0)
271 -- Pack left-aligned statusbar elements
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
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
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)
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)
300 i
.input
.show_frame
= false
301 w
.tabs
.show_tabs
= false
303 l
.uri
.selectable
= true
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()
315 w
.tabs
:add_signal("switch-page", function (nbook
, view
, idx
)
316 w
:update_tab_count(idx
)
317 w
:update_win_title(view
)
319 w
:update_progress(view
)
320 w
:update_tab_labels(idx
)
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
331 if w
:hit(mods
, key
) then
336 w
.win
:add_signal("mode-changed", function (win
, mode
)
337 local i
, p
= w
.ibar
.input
, w
.ibar
.prompt
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
357 elseif mode
== "insert" then
360 p
.text
= "-- INSERT --"
362 elseif mode
== "command" then
368 elseif mode
== "search" then
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
382 w
.ibar
.prompt
.text
= ""
383 w
.ibar
.input
.text
= ""
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
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) == "/"))
401 elseif w
:is_mode("follow") then
402 w
:eval_js(string.format("update(%q)", w
.ibar
.input
.text
))
406 w
.ibar
.input
:add_signal("activate", function()
407 local text
= w
.ibar
.input
.text
408 if w
:is_mode("command") then
410 w
:match_cmd(string.sub(text
, 2))
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
418 w
.ibar
.prompt
.text
= util
.escape(text
)
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
)
433 view
:add_signal("property::uri", function (v
)
434 w
:update_tab_labels()
435 if w
:is_current(v
) then
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
)
446 view
:add_signal("link-unhover", function (v
)
447 if w
:is_current(v
) then
452 view
:add_signal("form-active", function ()
456 view
:add_signal("root-active", function ()
460 view
:add_signal("key-press", function ()
461 -- Only allow key press events to hit the webview if the user is in
463 if not w
:is_mode("insert") then
468 view
:add_signal("button-release", function (v
, mods
, button
)
469 if w
:hit(mods
, button
) then
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
478 if status
== "provisional" then
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
)
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
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
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
)
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
533 view
:add_signal("property::progress", function (v
)
534 if w
:is_current(v
) then
539 view
:add_signal("expose", function (v
)
540 if w
:is_current(v
) then
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)))
560 print("E: unable to parse scroll amount:", value
)
564 -- Helper functions which operate on a windows widget structure
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
)
583 set
= function (w
, prop
, val
, view
)
584 if not view
then view
= w
:get_current() end
585 view
:set_prop(prop
, val
)
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)"
593 navigate
= function (w
, uri
, view
)
594 local v
= view
or w
:get_current()
598 return w
:new_tab(uri
)
602 reload
= function (w
, view
)
603 if not view
then view
= w
:get_current() end
607 new_tab
= function (w
, uri
)
608 local view
= webview()
611 attach_webview_signals(w
, view
)
612 if uri
then view
.uri
= uri
end
613 view
.show_scrollbars
= false
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
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)")
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")
638 return w
:eval_js(script
, file
, view
)
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
)
649 -- Wrapper around the bind plugin's match_cmd method
650 match_cmd
= function (w
, buffer
)
651 return bind
.match_cmd(commands
, buffer
, w
)
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
)
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")
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
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)
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
)
689 local uri
= string.gsub(search_engines
[engine
], "{%d}", search
)
690 return w
:navigate(uri
)
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
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)
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
)
718 for index
, comp
in pairs(cmpl
) do
719 if index
== w
.compl_index
then
720 i
.text
= ":" .. comp
.. " "
729 -- cycle through all possible completions
730 if w
.compl_index
== #cmpl
then
733 w
.compl_index
= w
.compl_index
+ 1
735 s
.text
= util
.escape(text
)
739 del_word
= function (w
)
740 local i
= w
.ibar
.input
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
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)
757 del_line
= function (w
)
758 local i
= w
.ibar
.input
759 if i
.text
~= ":" then
765 -- Search history adding
766 srch_hist_add
= function (w
, srch
)
767 if not w
.srch_hist
then w
.srch_hist
= {} end
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
)
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)
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)
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
)
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)
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)
847 -- Searching functions
848 start_search
= function (w
, forward
)
849 -- Clear previous search results
852 local i
= w
.ibar
.input
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
869 if not text
or #text
== 0 then
875 if w
.searching_forward
== nil then
876 w
.searching_forward
= forward
877 w
.search_start_marker
= view
:get_scroll_vert()
879 -- Invert the direction if originally searching in reverse
880 forward
= (w
.searching_forward
== forward
)
883 view
:search(text
, case_sensitive
, forward
, wrap
);
886 clear_search
= function (w
)
887 w
:get_current():clear_search()
888 -- Clear search state
890 w
.searching_forward
= nil
891 w
.search_start_marker
= nil
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
)
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
)
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
)
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)
924 prev_tab
= function (w
, n
)
925 w
.tabs
:switch(((w
.tabs
:current() - (n
or 1) -1) % w
.tabs
:count()) + 1)
927 goto_tab
= function (w
, n
)
931 -- History traversing functions
932 back
= function (w
, n
, view
)
933 (view
or w
:get_current()):go_back(n
or 1)
935 forward
= function (w
, n
, view
)
936 (view
or w
:get_current()):go_forward(n
or 1)
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())
944 update_win_title
= function (w
, view
)
945 if not view
then view
= w
:get_current() end
946 local title
= view
:get_prop("title")
948 if not title
and not uri
then
949 w
.win
.title
= "luakit"
951 w
.win
.title
= (title
or "luakit") .. " - " .. (uri
or "about:blank")
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"))
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()
966 w
.sbar
.l
.loaded
:show()
967 w
.sbar
.l
.loaded
.text
= string.format("(%d%%)", p
* 100)
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
982 update_buf
= function (w
)
984 w
.sbar
.r
.buf
.text
= util
.escape(string.format(" %-3s", w
.buffer
))
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
999 -- Tab label functions
1000 make_tab_label
= function (w
, pos
)
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
)
1016 w
:close_tab(w
.tabs
:atindex(pos
))
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
1030 update_tab_labels
= function (w
, current
)
1032 local count
, current
= w
.tabs
:count(), current
or w
.tabs
:current()
1035 -- Leave the tablist hidden if there is only one tab open
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()
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
)
1065 apply_tablabel_theme
= function (w
, t
, selected
, atheme
)
1066 local theme
= atheme
or theme
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
1071 t
.label
.fg
= theme
.tablabel_fg
or theme
.fg
1072 t
.ebox
.bg
= theme
.tablabel_bg
or theme
.bg
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
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
,
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
,
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
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
1131 -- Make sure something is loaded
1132 if w
.tabs
:count() == 0 then
1144 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80