96 lines
2.9 KiB
Fennel
96 lines
2.9 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
|
|
;; :config acts like use-package's :init and :config options,
|
|
;; taking in the following lisp expressions as its arguments
|
|
(lambda use! [repo ...]
|
|
(local output [ repo ])
|
|
(var last_key nil)
|
|
(var config_mode false)
|
|
(var has_config false)
|
|
(var config_entries (list `lambda []))
|
|
(each [_ val (ipairs [...])]
|
|
(if config_mode
|
|
(if (= (type val) :string)
|
|
(set config_mode false)
|
|
(table.insert config_entries val))
|
|
last_key
|
|
(do (tset output last_key val)
|
|
(set last_key nil))
|
|
(= val :config)
|
|
(do (set config_mode true)
|
|
(set has_config true))
|
|
(set last_key val)))
|
|
(if has_config
|
|
(tset output :config config_entries))
|
|
`(use ,output))
|
|
|
|
;; 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))
|
|
|
|
;; Basically does what the Emacs function of the same name does.
|
|
;; Wraps the code inside with a `winsaveview' and `winrestview'
|
|
(lambda save-excursion! [...]
|
|
(let [args [...]
|
|
len (# args)]
|
|
(tset args len `(let [retvar# ,(. args len)]
|
|
(vim.fn.winrestview save#)
|
|
retvar#))
|
|
`(let [save# (vim.fn.winsaveview)]
|
|
,(unpack args))))
|
|
|
|
{: bind!
|
|
: hook!
|
|
: use!
|
|
: setup!
|
|
: module-call!
|
|
: module-fn!
|
|
: save-excursion!}
|