dots-of-war/files/.config/nvim/fnl/utils.fnl

92 lines
2.8 KiB
Text
Raw Normal View History

2021-04-03 14:14:56 +00:00
(module utils
2021-05-08 17:31:04 +00:00
{autoload {a aniseed.core
fennel aniseed.fennel
nvim aniseed.nvim}
2021-04-03 14:14:56 +00:00
require-macros [macros]})
2021-04-23 14:46:45 +00:00
(defn plugin-installed? [name]
(~= nil (. packer_plugins name)))
2021-04-23 14:46:45 +00:00
2021-05-08 21:41:47 +00:00
(defn all [f xs]
(not (a.some (not (f $1)))))
2021-04-23 14:46:45 +00:00
2021-05-08 20:24:53 +00:00
(defn single-to-list [x]
"Returns the list given to it. If given a single value, wraps it in a list"
(if (a.table? x) x [x]))
2021-05-01 13:20:02 +00:00
2021-04-23 14:46:45 +00:00
(defn contains? [list elem]
2021-05-01 13:20:02 +00:00
(or (a.some #(= elem $1) list)) false)
(defn filter-table [f t]
(collect [k v (pairs t)]
(when (f k v)
(values k v))))
2021-04-23 14:46:45 +00:00
(defn without-keys [keys t]
2021-05-01 13:20:02 +00:00
(filter-table #(not (contains? keys $1)) t))
2021-04-23 14:46:45 +00:00
2021-05-08 20:24:53 +00:00
(defn keymap [modes from to ?opts]
"Set a mapping in the given modes, and some optional parameters, defaulting to {:noremap true :silent true}.
2021-04-23 14:46:45 +00:00
If :buffer is set, uses buf_set_keymap rather than set_keymap"
2021-05-08 21:54:36 +00:00
(let [full-opts (->> (or ?opts {})
(a.merge {:noremap true :silent true})
(without-keys [:buffer]))]
2021-05-08 20:24:53 +00:00
(each [_ mode (ipairs (single-to-list modes))]
2021-05-08 21:54:36 +00:00
(if (-?> ?opts (. :buffer))
(nvim.buf_set_keymap 0 mode from to full-opts)
(nvim.set_keymap mode from to full-opts)))))
2021-04-23 14:46:45 +00:00
(defn del-keymap [mode from ?buf-local]
"Remove a keymap. Arguments: mode, mapping, bool if mapping should be buffer-local."
(if ?buf-local
(nvim.buf_del_keymap 0 mode from)
(nvim.del_keymap mode from)))
2021-04-03 14:14:56 +00:00
2021-05-08 18:18:22 +00:00
(defn- safe-require-plugin-config [name]
(xpcall
#(require name)
#(a.println (.. "Error sourcing " name ":\n" (fennel.traceback $1)))))
(defn use [...]
"Iterates through the arguments as pairs and calls packer's function for
2021-05-08 17:31:04 +00:00
each of them. Works around Fennel not liking mixed associative and sequential
2021-05-08 18:18:22 +00:00
tables as well.
Additionally sources the file set in the :mod field of the plugin options"
(let [pkgs [...]
packer (require "packer")]
2021-05-08 17:31:04 +00:00
(packer.startup
(fn [use]
(each-pair [name opts pkgs]
2021-05-08 21:54:36 +00:00
(-?> opts (. :mod) (safe-require-plugin-config))
2021-05-08 17:31:04 +00:00
(use (a.assoc opts 1 name)))))))
2021-05-02 15:15:33 +00:00
2021-04-03 14:14:56 +00:00
2021-05-08 21:41:47 +00:00
(defn buffer-content [bufnr]
"Returns a table of lines in the given buffer"
(vim.api.nvim_buf_get_lines bufnr 0 -1 false))
2021-05-02 11:54:58 +00:00
(defn surround-if-present [a mid b]
(if mid
(.. a mid b)
""))
2021-04-03 14:14:56 +00:00
2021-05-02 11:54:58 +00:00
(defn highlight [group-arg colset]
2021-04-04 13:04:29 +00:00
(let [default { :fg "NONE" :bg "NONE" :gui "NONE"}
2021-05-08 20:24:53 +00:00
opts (a.merge default colset)]
(each [_ group (ipairs (single-to-list group-arg))]
2021-04-30 14:41:57 +00:00
(nvim.command (.. "hi! "group" guifg='"opts.fg"' guibg='"opts.bg"' gui='"opts.gui"'")))))
2021-04-03 17:43:27 +00:00
2021-05-02 11:54:58 +00:00
(defn highlight-add [group-arg colset]
2021-05-08 20:24:53 +00:00
(each [_ group (ipairs (single-to-list group-arg))]
(nvim.command
(.. "hi! "
group
(surround-if-present " guibg='"colset.bg"'")
(surround-if-present " guifg='"colset.fg"'")
(surround-if-present " gui='"colset.gui"'")))))
2021-05-02 11:54:58 +00:00
2021-04-03 17:43:27 +00:00
2021-04-04 13:04:29 +00:00
(defn comp [f g]
(fn [...]
(f (g ...))))