summaryrefslogtreecommitdiff
path: root/fnl/util.fnl
blob: a80f0eb1b56398804161e540a7124ab581c8c753 (plain)
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
;; Utility functions.
(module util {autoload {a aniseed.core nvim aniseed.nvim : packer}})

(def- path (.. (vim.fn.stdpath :data) :/site/pack/packer/start))

(def- installed-plugins (vim.fn.readdir path))

(def- configured-plugins (require :plugins))

(defn- parse-plugins-names [plugins] (var names [])
       (each [plugin _ (pairs plugins)]
         (table.insert names (string.match plugin "/(.*)"))) names)

(defn- contains [ys xs] (var equal true)
       (each [_ x (pairs xs)]
         (if (not= (vim.tbl_contains ys x) true)
             (set equal false))) equal)

(defn- deep-equal [xs ys] (and (contains xs ys) (contains ys xs)))

(defn- synchronized [configured installed]
       (and (= (vim.tbl_count configured) (vim.tbl_count installed))
            (deep-equal configured installed)))

(defn- install []
       (packer.startup (fn [use]
                         (each [plugin opts (pairs configured-plugins)]
                           (use (a.assoc opts 1 plugin))))))

(defn load-plugins [] (install)
      (if (not= (synchronized (parse-plugins-names configured-plugins)
                              installed-plugins) true)
          (packer.sync)
          (require :config)))

(defn load-plugin [name]
      (let [(ok? plugin) (pcall require name)]
        (if ok?
            plugin
            (vim.notify (.. "Could not load config: " val-or-err)
                        vim.log.levels.WARN))))

(defn use-config [plugin config]
      (let [plugin (load-plugin plugin)]
        (plugin.setup config)))