This repository has been archived on 2024-10-28. You can view files and clone it, but cannot push or open issues or pull requests.
nvim-config/fnl/macros.fnl

71 lines
2.1 KiB
Fennel

;;; macros.fnl - useful macros
;; Helpful keymaping functions
(lambda bind! [mode key cmd ?a1 ?a2]
(let [desc (or ?a2 ?a1)
buf (if (and ?a1 ?a2) ?a1 ?a2)
opts { :noremap true
:silent true
:buffer buf }]
(when desc
(tset opts :desc desc))
`(vim.keymap.set ,mode ,key ,cmd ,opts)))
;; Better autocommands
(lambda hook! [hooks ?patterns callback]
(let [hook_table (if (= (type hooks) :table)
hooks
[ hooks ])
pattern_table (if (not ?patterns)
[]
(= (type ?patterns) :table)
?patterns
[ ?patterns ])]
(var group "config-hook")
(each [_ hook (ipairs hook_table)]
(set group (.. group "-" hook)))
(each [_ pattern (ipairs pattern_table)]
(set group (.. group "-" pattern)))
`(vim.api.nvim_create_autocmd ,hook_table
{ :group
(vim.api.nvim_create_augroup ,group
{ :clear true })
:pattern ,pattern_table
:callback ,callback })))
;; Nicer macro for use
(lambda use! [repo ...]
(local output [ repo ])
(var last_key nil)
(each [_ val (ipairs [...])]
(if last_key
(do (tset output last_key val)
(set last_key nil))
(set last_key val)))
`(use ,output))
;; Can be passed to :config of `use!'
;; to call a plugin's `setup' function
(lambda setup! [pkg ...] (local output [ ])
(var last_key nil)
(each [_ val (ipairs [...])]
(if last_key
(do (tset output last_key val)
(set last_key nil))
(set last_key val)))
`((. (require ,pkg) :setup) ,output))
;; Call module function
(lambda module-call! [mod func ...]
`((. (require ,mod) ,func) ,...))
;; Return module function
(lambda module-fn! [mod func]
`(. (require ,mod) ,func))
{: bind!
: hook!
: use!
: setup!
: module-call!
: module-fn!}