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

92 lines
2.7 KiB
Plaintext
Raw Normal View History

2022-11-30 22:27:40 -08:00
;;; 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
2022-11-30 22:27:40 -08:00
[ hooks ])
pattern_table (if (not ?patterns)
[]
(= (type ?patterns) :table)
?patterns
2022-11-30 22:27:40 -08:00
[ ?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
2022-11-30 22:27:40 -08:00
{ :clear true })
:pattern ,pattern_table
2022-11-30 22:27:40 -08:00
: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))
2022-11-30 22:27:40 -08:00
(lambda use! [repo ...]
(local output [ repo ])
(var last_key nil)
(var config_mode false)
(var has_config false)
(var config_entries (list `lambda []))
2022-11-30 22:27:40 -08:00
(each [_ val (ipairs [...])]
(if config_mode
(if (= (type val) :string)
(set config_mode false)
(table.insert config_entries val))
last_key
2022-11-30 22:27:40 -08:00
(do (tset output last_key val)
(set last_key nil))
(= val :config)
(do (set config_mode true)
(set has_config true))
2022-11-30 22:27:40 -08:00
(set last_key val)))
(if has_config
(tset output :config config_entries))
2022-11-30 22:27:40 -08:00
`(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!
2022-11-30 22:27:40 -08:00
: hook!
: use!
: setup!
: module-call!
: module-fn!}