-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfennel-loader.lua
More file actions
539 lines (539 loc) · 15 KB
/
fennel-loader.lua
File metadata and controls
539 lines (539 loc) · 15 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
-- [nfnl] fnl/fennel-loader.fnl
local dev_fennel_path = (vim.env.HOME .. "/Projects/Home/Fennel/Fennel")
local specials_registry = {}
local macro_registry = {}
local fennel_module_prefixes = {"fennel", "nfnl.fennel", "conjure.aniseed.deps.fennel", "conjure.aniseed.fennel", "conjure.nfnl.fennel"}
local fennel_submodules = {"compiler", "parser", "specials", "utils", "view", "repl", "friend", "binary"}
local function find_nix_fennel_path()
local fennel_bin = vim.fn.exepath("fennel")
if (fennel_bin and (fennel_bin ~= "")) then
local resolved = vim.fn.resolve(fennel_bin)
local store_path = resolved:match("^(.*/[^/]+)/bin/fennel$")
if store_path then
local lua_path = (store_path .. "/share/lua/5.1")
if (1 == vim.fn.isdirectory(lua_path)) then
return lua_path
else
return nil
end
else
return nil
end
else
return nil
end
end
local function has_discard_support_3f(fennel_path)
local test_path = (fennel_path .. "/?.lua")
local saved_path = package.path
package.path = (test_path .. ";" .. package.path)
local ok, fennel = pcall(require, "fennel")
package.path = saved_path
if ok then
for k, _ in pairs(package.loaded) do
if k:match("^fennel%.?") then
package.loaded[k] = nil
else
end
end
else
end
if ok then
local eval_ok, result = pcall(fennel.eval, "(+ 1 #_2 3)")
return (eval_ok and (result == 4))
else
return nil
end
end
local function find_custom_fennel()
if ((1 == vim.fn.isdirectory(dev_fennel_path)) and has_discard_support_3f(dev_fennel_path)) then
return dev_fennel_path
else
local nix_path = find_nix_fennel_path()
if (nix_path and has_discard_support_3f(nix_path)) then
return nix_path
else
return nil
end
end
end
local function fennel_module_key_3f(k)
if (type(k) == "string") then
local match_3f = false
for _, prefix in ipairs(fennel_module_prefixes) do
match_3f = (match_3f or (k == prefix) or vim.startswith(k, (prefix .. ".")))
end
return match_3f
else
return nil
end
end
local function sync_fennel_submodules_21(prefix)
for _, sub in ipairs(fennel_submodules) do
local standard_key = ("fennel." .. sub)
local target_key = (prefix .. "." .. sub)
local mod = package.loaded[standard_key]
if mod then
package.loaded[target_key] = mod
else
end
end
return nil
end
local function purge_fennel_modules()
for _, tbl in ipairs({"loaded", "preload"}) do
for k, _0 in pairs(package[tbl]) do
if fennel_module_key_3f(k) then
package[tbl][k] = nil
else
end
end
end
return nil
end
local function sync_fennel_modules(fennel)
local f = (fennel or package.loaded.fennel)
if (f and (type(f) == "table")) then
for _, sub in ipairs(fennel_submodules) do
pcall(require, ("fennel." .. sub))
end
for _, prefix in ipairs(fennel_module_prefixes) do
package.loaded[prefix] = f
sync_fennel_submodules_21(prefix)
end
return nil
else
return nil
end
end
local function patch_table(t, registry)
if ((type(t) == "table") and (type(registry) == "table")) then
for name, target in pairs(registry) do
local val = (t[target] or target)
if (val and not t[name]) then
t[name] = val
else
end
end
return true
else
return nil
end
end
local function clear_table(t, registry)
if ((type(t) == "table") and (type(registry) == "table")) then
for name, _ in pairs(registry) do
t[name] = nil
end
return true
else
return nil
end
end
local function patch_scope(scope)
if (type(scope) == "table") then
local _17_
do
local t_16_ = scope
if (nil ~= t_16_) then
t_16_ = t_16_.specials
else
end
_17_ = t_16_
end
patch_table(_17_, specials_registry)
local _20_
do
local t_19_ = scope
if (nil ~= t_19_) then
t_19_ = t_19_.macros
else
end
_20_ = t_19_
end
patch_table(_20_, macro_registry)
return scope
else
return nil
end
end
local function clear_scope(scope, specials, macro_aliases)
if (type(scope) == "table") then
local _24_
do
local t_23_ = scope
if (nil ~= t_23_) then
t_23_ = t_23_.specials
else
end
_24_ = t_23_
end
clear_table(_24_, specials)
local _27_
do
local t_26_ = scope
if (nil ~= t_26_) then
t_26_ = t_26_.macros
else
end
_27_ = t_26_
end
clear_table(_27_, macro_aliases)
return scope
else
return nil
end
end
local function patch_specials_module()
local ok, specials = pcall(require, "fennel.specials")
if (ok and (type(specials) == "table")) then
return patch_table(specials, specials_registry)
else
return nil
end
end
local function clear_specials_module(specials_registry_snapshot)
local ok, specials = pcall(require, "fennel.specials")
if (ok and (type(specials) == "table")) then
return clear_table(specials, specials_registry_snapshot)
else
return nil
end
end
local function apply_registries()
do
local ok_compiler, compiler = pcall(require, "fennel.compiler")
if ok_compiler then
local function _33_()
local t_32_ = compiler
if (nil ~= t_32_) then
t_32_ = t_32_.scopes
else
end
if (nil ~= t_32_) then
t_32_ = t_32_.global
else
end
return t_32_
end
patch_scope(_33_())
local function _37_()
local t_36_ = compiler
if (nil ~= t_36_) then
t_36_ = t_36_.scopes
else
end
if (nil ~= t_36_) then
t_36_ = t_36_.compiler
else
end
return t_36_
end
patch_scope(_37_())
else
end
end
return patch_specials_module()
end
local function merge_registry_21(registry, defs)
if (type(defs) == "table") then
for name, value in pairs(defs) do
local structured_3f = ((type(value) == "table") and ((value.value ~= nil) or (value.clone ~= nil)))
local _41_
if structured_3f then
_41_ = (value.value or value.clone)
else
_41_ = value
end
registry[name] = _41_
end
return nil
else
return nil
end
end
local function registry_snapshot(registry)
local copy = {}
for name, value in pairs((registry or {})) do
copy[name] = value
end
return copy
end
local function clear_registry_21(registry)
for name, _ in pairs((registry or {})) do
registry[name] = nil
end
return registry
end
local function reset_registries_21()
local previous_specials = registry_snapshot(specials_registry)
local previous_macros = registry_snapshot(macro_registry)
do
local ok_compiler, compiler = pcall(require, "fennel.compiler")
if ok_compiler then
local _45_
do
local t_44_ = compiler
if (nil ~= t_44_) then
t_44_ = t_44_.scopes
else
end
if (nil ~= t_44_) then
t_44_ = t_44_.global
else
end
_45_ = t_44_
end
clear_scope(_45_, previous_specials, previous_macros)
local _49_
do
local t_48_ = compiler
if (nil ~= t_48_) then
t_48_ = t_48_.scopes
else
end
if (nil ~= t_48_) then
t_48_ = t_48_.compiler
else
end
_49_ = t_48_
end
clear_scope(_49_, previous_specials, previous_macros)
else
end
end
clear_specials_module(previous_specials)
clear_registry_21(specials_registry)
return clear_registry_21(macro_registry)
end
local function register_macro_defs(defs)
merge_registry_21(specials_registry, defs.specials)
merge_registry_21(macro_registry, defs.macros)
return apply_registries()
end
local function registered_defs()
return {specials = registry_snapshot(specials_registry), macros = registry_snapshot(macro_registry)}
end
local function nfnl_output_lua_path()
local nfnl_lua_dir = (vim.fn.stdpath("data") .. "/nfnl/lua")
return (nfnl_lua_dir .. "/?.lua;" .. nfnl_lua_dir .. "/?/init.lua")
end
local function prepend_package_path_21(path_prefix)
if not package.path:match(vim.pesc(path_prefix)) then
package.path = (path_prefix .. ";" .. package.path)
return nil
else
return nil
end
end
local function ensure_runtime_plugin_21(plugin_name)
return pcall(vim.cmd.packadd, {args = {plugin_name}})
end
local function runtime_file(plugin_name, file_name)
ensure_runtime_plugin_21(plugin_name)
local candidates = vim.api.nvim_get_runtime_file(file_name, true)
local preferred_pattern = ("/" .. vim.pesc(plugin_name) .. "/" .. vim.pesc(file_name) .. "$")
local _54_
do
local found_path = nil
for _, path in ipairs(candidates) do
found_path = (found_path or (path:match(preferred_pattern) and path))
end
_54_ = found_path
end
return (_54_ or candidates[1])
end
local function module_name__3esource_path(module_name, _3fconfig_dir)
local rel_path = ("fnl/" .. module_name:gsub("%.", "/") .. ".fnl")
local runtime_paths = vim.api.nvim_get_runtime_file(rel_path, true)
local _55_
do
local config_dir = _3fconfig_dir
if config_dir then
local config_path = (config_dir .. "/" .. rel_path)
if (1 == vim.fn.filereadable(config_path)) then
_55_ = config_path
else
_55_ = nil
end
else
_55_ = nil
end
end
local or_59_ = _55_ or runtime_paths[1]
if not or_59_ then
local config_path = (vim.fn.stdpath("config") .. "/" .. rel_path)
if (1 == vim.fn.filereadable(config_path)) then
or_59_ = config_path
else
or_59_ = nil
end
end
return or_59_
end
local function macro_defs_3f(defs)
return ((type(defs) == "table") and ((type(defs.specials) == "table") or (type(defs.macros) == "table")))
end
local function ensure_source_preload_21(module_name, _3fconfig_dir)
if not package.searchpath(module_name, package.path) then
local source_path = module_name__3esource_path(module_name, _3fconfig_dir)
if source_path then
local function _62_()
local fennel = require("fennel")
local ok, defs = pcall(fennel.dofile, source_path)
if ok then
return defs
else
return error(("fennel-loader: Failed to preload " .. module_name .. " from " .. source_path .. ": " .. defs), 0)
end
end
package.preload[module_name] = _62_
return nil
else
return nil
end
else
return nil
end
end
local function load_macro_module_from_source(module_name, _3fconfig_dir)
local source_path = module_name__3esource_path(module_name, _3fconfig_dir)
if source_path then
local fennel = require("fennel")
local ok, defs = pcall(fennel.dofile, source_path)
if (ok and macro_defs_3f(defs)) then
package.loaded[module_name] = defs
return defs
else
return nil
end
else
return nil
end
end
local function register_macros_from_module(module_name, _3fconfig_dir)
package.loaded[module_name] = nil
local ok, defs = pcall(require, module_name)
local resolved_defs
if (ok and macro_defs_3f(defs)) then
resolved_defs = defs
else
resolved_defs = load_macro_module_from_source(module_name, _3fconfig_dir)
end
if resolved_defs then
return register_macro_defs(resolved_defs)
else
return vim.notify(("fennel-loader: Failed to load macros from module " .. module_name .. ": " .. tostring(defs)), vim.log.levels.WARN)
end
end
local function fallback_macro_modules_available(_3fconfig_dir)
local modules = {"macros.jp", "macros.typed-fennel"}
for _, module_name in ipairs(modules) do
ensure_source_preload_21(module_name, _3fconfig_dir)
end
return modules
end
local function inject_all_global_macros(_3fconfig_dir)
prepend_package_path_21(nfnl_output_lua_path())
reset_registries_21()
local fallback_3f = not package.searchpath("macros.init", package.path)
local modules
if fallback_3f then
modules = fallback_macro_modules_available(_3fconfig_dir)
else
modules = {"macros.init", "macros.jp"}
end
if not fallback_3f then
ensure_source_preload_21("macros.init", _3fconfig_dir)
else
end
ensure_source_preload_21("macros.typed-fennel", _3fconfig_dir)
for _, module_name in ipairs(modules) do
if ((module_name == "macros.init") or (module_name == "macros.jp") or (module_name == "macros.typed-fennel")) then
package.loaded[module_name] = nil
else
end
end
if fallback_3f then
for _, module_name in ipairs(modules) do
register_macros_from_module(module_name, _3fconfig_dir)
end
return nil
else
return register_macros_from_module("macros.init", _3fconfig_dir)
end
end
local function typed_fennel_plugin_parent()
local init_macros_path = runtime_file("typed-fennel", "init-macros.fnl")
if init_macros_path then
return vim.fs.dirname(vim.fs.dirname(init_macros_path))
else
return nil
end
end
local function typed_fennel_path()
local plugin_parent = typed_fennel_plugin_parent()
if plugin_parent then
return (plugin_parent .. "/?/init.fnl")
else
return nil
end
end
local function typed_fennel_macro_path()
local plugin_parent = typed_fennel_plugin_parent()
if plugin_parent then
return (plugin_parent .. "/?/init-macros.fnl")
else
return nil
end
end
local function config_fennel_path(config_dir)
return (config_dir .. "/?.fnl;" .. config_dir .. "/?/init.fnl;" .. config_dir .. "/fnl/?.fnl;" .. config_dir .. "/fnl/?/init.fnl")
end
local function prepend_search_path_21(fennel, key, path_prefix)
local current = fennel[key]
if ((type(current) == "string") and not current:match(vim.pesc(path_prefix))) then
fennel[key] = (path_prefix .. ";" .. current)
return nil
else
return nil
end
end
local function setup_fennel_paths(fennel, _3fconfig_dir)
local config_dir = (_3fconfig_dir or vim.fn.stdpath("config"))
if (type(fennel) == "table") then
prepend_search_path_21(fennel, "path", config_fennel_path(config_dir))
do
local typed_fennel_path0 = typed_fennel_path()
if typed_fennel_path0 then
prepend_search_path_21(fennel, "path", typed_fennel_path0)
else
end
end
local macro_path = typed_fennel_macro_path()
if macro_path then
return prepend_search_path_21(fennel, "macro-path", macro_path)
else
return nil
end
else
return nil
end
end
local function inject_custom_fennel()
local fennel_path = find_custom_fennel()
if not fennel_path then
error(("Custom Fennel with #_ support not found.\n" .. "Checked: " .. dev_fennel_path .. "\n" .. "Also checked PATH for Nix-built fennel."))
else
end
purge_fennel_modules()
if not package.path:match(vim.pesc(fennel_path)) then
package.path = (fennel_path .. "/?.lua;" .. package.path)
else
end
local fennel = require("fennel")
sync_fennel_modules(fennel)
apply_registries()
return fennel
end
return {["sync-fennel-modules"] = sync_fennel_modules, ["inject-custom-fennel"] = inject_custom_fennel, ["inject-all-global-macros"] = inject_all_global_macros, ["registered-defs"] = registered_defs, ["typed-fennel-macro-path"] = typed_fennel_macro_path, ["setup-fennel-paths"] = setup_fennel_paths}