forked from luakit/luakit-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
160 lines (142 loc) · 6.1 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
--------------------------------------------------------------------------------
-- © 2012 Mason Larobina (mason-l) <[email protected]> --
-- © 2012 luakit-plugins development team --
-- Source code available under GNU GPLv2 or above --
-- Plugins (external add-ons) for [Luakit browser framework] modules --
-- management infrastructure. v0.1.0-pre --
--------------------------------------------------------------------------------
local io = io
local lfs = require("lfs")
local dofile = dofile
local type = type
local table = table
local pairs = pairs
local string = string
local require = require
local print = print
local capi = { luakit = luakit }
module("plugins")
plugins_dir = capi.luakit.config_dir .. "/plugins/"
rcfile = plugins_dir .. "rc.lua"
plugins_to_load = {}
policy = "manual" -- To prevent automatic load on old rc.lua configs.
-- Get current list of usable plugins.
-- Method: listing all "<plugins_dir>/(*)/init.lua" catalogues and returning
-- a table of their names as we use in rc.lua for manual selection.
-- Additionally, file 'companions' mean auxiliary parts that also could be
-- loaded but are not vital for function of the main extension module.
plugins_list = function(path)
local plugins_path = path
if (not path) or (type(path) ~= "string") then
plugins_path = plugins_dir
end
-- Getting list of dirs which contain "init.lua" files:
local dirs = {}
for entry in lfs.dir(plugins_path) do
if entry ~= "." and entry ~= ".." then
local luaentry = plugins_path .. entry .. "/init.lua"
local attr = lfs.attributes (luaentry)
if ( type(attr) == "table" ) and ( attr.mode == "file" ) then
-- Is a loadable one.
dirs[entry] = true
do -- Check for companions:
local companions = plugins_path .. entry .. "/companions"
local attr = lfs.attributes (companions)
if ( type(attr) == "table" ) and ( attr.mode == "file" ) then
local compfile = io.open (companions, "r")
if compfile then
for line in compfile:lines() do
if line ~= "" then
-- Check for Lua file with companion's name.
local companion = plugins_path .. entry .. "/" .. line .. ".lua"
local attr = lfs.attributes (companion)
if ( type(attr) == "table" ) and ( attr.mode == "file" ) then
-- Module companion file exists.
dirs[entry .. "." .. line] = true
end
end
end
compfile:close()
end
end
end
end
end
end
return dirs
end
rcfile_check = function(filename, fix, valid_plugins)
-- Check if file name is a non-empty string:
if not filename or type(filename) ~= "string" or filename > "" then
-- error("plugins: incorrect rcfile path given.")
filename = rcfile
end
-- Now test if the file exists:
rcfile_mode, err_msg = lfs.attributes(filename, "mode")
if rcfile_mode and rcfile_mode == "file" then
-- Yes, exists. Do not check its content.
return true
else
-- Can restore the default list of plugins if `fix’ is not false or nil.
if not fix then
return false
else
local default_rc_content_template = [=[
-- This is Luakit.plugins minimal options file.
-- To restore the default configuration simply delete this file
-- and restart luakit
plugins.policy = "automatic" -- Choose "manual" to enable selection below.
-- By default is set to "automatic" to load
-- everything ignoring manual selection.
plugins.plugins_to_load = {
{plugins}
}
]=]
local plugin_line_template = [=[ "{pluginid}"]=]
local line_subs, rc_lines = {}, {}
for plugin_id, _ in pairs (valid_plugins) do
local line_subs = { pluginid = plugin_id }
local plugin_line_template = string.gsub( plugin_line_template, "{(%w+)}", line_subs )
table.insert( rc_lines, plugin_line_template )
end
local default_rc_content = string.gsub( default_rc_content_template, "{(%w+)}", { plugins = table.concat(rc_lines, ",\n") } )
local rc = io.open( filename, "w" )
rc:write (default_rc_content)
rc:close ()
return true
end
end
return false
end
load_plugins = function()
print ("Initializing plugins: scanning subfolders for valid entries…")
local valid_plugins = plugins_list (plugins_dir)
print ("plugins: reading rc.lua…")
if rcfile_check (rcfile, true, valid_plugins) then
print ("plugins: rc file exists.")
else
print ("plugins: rc file does not exist; aborting.")
error ("plugins: rc file does not exist; aborting.")
end
-- Read rcfile with plugins list:
local rc = dofile (rcfile)
-- TODO: Refactor this ugly code into something efficient.
if policy ~= nil and policy == "automatic" then
plugins_to_load = {}
for plugin_id in pairs(valid_plugins) do
table.insert(plugins_to_load, plugin_id)
end
end
-- Import plugins:
for _, plugin_id in pairs(plugins_to_load) do
if valid_plugins[plugin_id] then
print ("Initializing plugin '" .. plugin_id .. "'…")
require ("plugins." .. plugin_id)
else
-- Ignore it.
print ("Ignore plugin '" .. plugin_id .. "'.")
end
end
print ("Initializing plugins: done.")
end
load_plugins()